Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
707827f3 | 2 | /* |
962e7bd4 | 3 | * cpuidle-pseries - idle state cpuidle driver. |
707827f3 DD |
4 | * Adapted from drivers/idle/intel_idle.c and |
5 | * drivers/acpi/processor_idle.c | |
6 | * | |
7 | */ | |
8 | ||
9 | #include <linux/kernel.h> | |
10 | #include <linux/module.h> | |
11 | #include <linux/init.h> | |
12 | #include <linux/moduleparam.h> | |
13 | #include <linux/cpuidle.h> | |
14 | #include <linux/cpu.h> | |
16aaaff6 | 15 | #include <linux/notifier.h> |
707827f3 DD |
16 | |
17 | #include <asm/paca.h> | |
18 | #include <asm/reg.h> | |
707827f3 DD |
19 | #include <asm/machdep.h> |
20 | #include <asm/firmware.h> | |
3f67d962 | 21 | #include <asm/runlatch.h> |
212bebb4 | 22 | #include <asm/plpar_wrappers.h> |
707827f3 DD |
23 | |
24 | struct cpuidle_driver pseries_idle_driver = { | |
1ca80944 DL |
25 | .name = "pseries_idle", |
26 | .owner = THIS_MODULE, | |
707827f3 DD |
27 | }; |
28 | ||
624e46d0 NP |
29 | static int max_idle_state __read_mostly; |
30 | static struct cpuidle_state *cpuidle_state_table __read_mostly; | |
31 | static u64 snooze_timeout __read_mostly; | |
32 | static bool snooze_timeout_en __read_mostly; | |
707827f3 | 33 | |
1ca80944 | 34 | static inline void idle_loop_prolog(unsigned long *in_purr) |
707827f3 | 35 | { |
d8c6ad31 | 36 | ppc64_runlatch_off(); |
707827f3 DD |
37 | *in_purr = mfspr(SPRN_PURR); |
38 | /* | |
39 | * Indicate to the HV that we are idle. Now would be | |
40 | * a good time to find other work to dispatch. | |
41 | */ | |
42 | get_lppaca()->idle = 1; | |
43 | } | |
44 | ||
1ca80944 | 45 | static inline void idle_loop_epilog(unsigned long in_purr) |
707827f3 | 46 | { |
7ffcf8ec AB |
47 | u64 wait_cycles; |
48 | ||
49 | wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles); | |
50 | wait_cycles += mfspr(SPRN_PURR) - in_purr; | |
51 | get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles); | |
707827f3 | 52 | get_lppaca()->idle = 0; |
d8c6ad31 | 53 | |
d8c6ad31 | 54 | ppc64_runlatch_on(); |
707827f3 DD |
55 | } |
56 | ||
57 | static int snooze_loop(struct cpuidle_device *dev, | |
58 | struct cpuidle_driver *drv, | |
59 | int index) | |
60 | { | |
61 | unsigned long in_purr; | |
78eaa10f | 62 | u64 snooze_exit_time; |
707827f3 | 63 | |
3fc5ee92 NP |
64 | set_thread_flag(TIF_POLLING_NRFLAG); |
65 | ||
1ca80944 | 66 | idle_loop_prolog(&in_purr); |
83dac594 | 67 | local_irq_enable(); |
78eaa10f | 68 | snooze_exit_time = get_tb() + snooze_timeout; |
707827f3 | 69 | |
b69dbba0 | 70 | while (!need_resched()) { |
83dac594 DD |
71 | HMT_low(); |
72 | HMT_very_low(); | |
7ded4291 NP |
73 | if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) { |
74 | /* | |
75 | * Task has not woken up but we are exiting the polling | |
76 | * loop anyway. Require a barrier after polling is | |
77 | * cleared to order subsequent test of need_resched(). | |
78 | */ | |
79 | clear_thread_flag(TIF_POLLING_NRFLAG); | |
80 | smp_mb(); | |
78eaa10f | 81 | break; |
7ded4291 | 82 | } |
707827f3 DD |
83 | } |
84 | ||
707827f3 | 85 | HMT_medium(); |
83dac594 | 86 | clear_thread_flag(TIF_POLLING_NRFLAG); |
83dac594 | 87 | |
ced54c08 NP |
88 | local_irq_disable(); |
89 | ||
1ca80944 DL |
90 | idle_loop_epilog(in_purr); |
91 | ||
707827f3 DD |
92 | return index; |
93 | } | |
94 | ||
7230c564 BH |
95 | static void check_and_cede_processor(void) |
96 | { | |
97 | /* | |
be2cf20a BH |
98 | * Ensure our interrupt state is properly tracked, |
99 | * also checks if no interrupt has occurred while we | |
100 | * were soft-disabled | |
7230c564 | 101 | */ |
be2cf20a | 102 | if (prep_irq_for_idle()) { |
7230c564 | 103 | cede_processor(); |
be2cf20a BH |
104 | #ifdef CONFIG_TRACE_IRQFLAGS |
105 | /* Ensure that H_CEDE returns with IRQs on */ | |
106 | if (WARN_ON(!(mfmsr() & MSR_EE))) | |
107 | __hard_irq_enable(); | |
108 | #endif | |
109 | } | |
7230c564 BH |
110 | } |
111 | ||
707827f3 DD |
112 | static int dedicated_cede_loop(struct cpuidle_device *dev, |
113 | struct cpuidle_driver *drv, | |
114 | int index) | |
115 | { | |
116 | unsigned long in_purr; | |
707827f3 | 117 | |
1ca80944 | 118 | idle_loop_prolog(&in_purr); |
707827f3 DD |
119 | get_lppaca()->donate_dedicated_cpu = 1; |
120 | ||
707827f3 | 121 | HMT_medium(); |
7230c564 | 122 | check_and_cede_processor(); |
707827f3 | 123 | |
ced54c08 | 124 | local_irq_disable(); |
707827f3 | 125 | get_lppaca()->donate_dedicated_cpu = 0; |
1ca80944 DL |
126 | |
127 | idle_loop_epilog(in_purr); | |
128 | ||
707827f3 DD |
129 | return index; |
130 | } | |
131 | ||
132 | static int shared_cede_loop(struct cpuidle_device *dev, | |
133 | struct cpuidle_driver *drv, | |
134 | int index) | |
135 | { | |
136 | unsigned long in_purr; | |
707827f3 | 137 | |
1ca80944 | 138 | idle_loop_prolog(&in_purr); |
707827f3 DD |
139 | |
140 | /* | |
141 | * Yield the processor to the hypervisor. We return if | |
142 | * an external interrupt occurs (which are driven prior | |
143 | * to returning here) or if a prod occurs from another | |
144 | * processor. When returning here, external interrupts | |
145 | * are enabled. | |
146 | */ | |
7230c564 | 147 | check_and_cede_processor(); |
707827f3 | 148 | |
ced54c08 | 149 | local_irq_disable(); |
1ca80944 DL |
150 | idle_loop_epilog(in_purr); |
151 | ||
707827f3 DD |
152 | return index; |
153 | } | |
154 | ||
155 | /* | |
156 | * States for dedicated partition case. | |
157 | */ | |
bf7f61f2 | 158 | static struct cpuidle_state dedicated_states[] = { |
707827f3 DD |
159 | { /* Snooze */ |
160 | .name = "snooze", | |
161 | .desc = "snooze", | |
707827f3 DD |
162 | .exit_latency = 0, |
163 | .target_residency = 0, | |
164 | .enter = &snooze_loop }, | |
165 | { /* CEDE */ | |
166 | .name = "CEDE", | |
167 | .desc = "CEDE", | |
83dac594 DD |
168 | .exit_latency = 10, |
169 | .target_residency = 100, | |
707827f3 DD |
170 | .enter = &dedicated_cede_loop }, |
171 | }; | |
172 | ||
173 | /* | |
174 | * States for shared partition case. | |
175 | */ | |
bf7f61f2 | 176 | static struct cpuidle_state shared_states[] = { |
f2ac428e NP |
177 | { /* Snooze */ |
178 | .name = "snooze", | |
179 | .desc = "snooze", | |
180 | .exit_latency = 0, | |
181 | .target_residency = 0, | |
182 | .enter = &snooze_loop }, | |
707827f3 DD |
183 | { /* Shared Cede */ |
184 | .name = "Shared Cede", | |
185 | .desc = "Shared Cede", | |
f2ac428e NP |
186 | .exit_latency = 10, |
187 | .target_residency = 100, | |
707827f3 DD |
188 | .enter = &shared_cede_loop }, |
189 | }; | |
190 | ||
529351fd | 191 | static int pseries_cpuidle_cpu_online(unsigned int cpu) |
707827f3 | 192 | { |
529351fd | 193 | struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu); |
16aaaff6 | 194 | |
852d8cb1 | 195 | if (dev && cpuidle_get_driver()) { |
529351fd SAS |
196 | cpuidle_pause_and_lock(); |
197 | cpuidle_enable_device(dev); | |
198 | cpuidle_resume_and_unlock(); | |
199 | } | |
200 | return 0; | |
201 | } | |
852d8cb1 | 202 | |
529351fd SAS |
203 | static int pseries_cpuidle_cpu_dead(unsigned int cpu) |
204 | { | |
205 | struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu); | |
852d8cb1 | 206 | |
529351fd SAS |
207 | if (dev && cpuidle_get_driver()) { |
208 | cpuidle_pause_and_lock(); | |
209 | cpuidle_disable_device(dev); | |
210 | cpuidle_resume_and_unlock(); | |
707827f3 | 211 | } |
529351fd | 212 | return 0; |
707827f3 DD |
213 | } |
214 | ||
215 | /* | |
216 | * pseries_cpuidle_driver_init() | |
217 | */ | |
218 | static int pseries_cpuidle_driver_init(void) | |
219 | { | |
220 | int idle_state; | |
221 | struct cpuidle_driver *drv = &pseries_idle_driver; | |
222 | ||
223 | drv->state_count = 0; | |
224 | ||
bf7f61f2 DD |
225 | for (idle_state = 0; idle_state < max_idle_state; ++idle_state) { |
226 | /* Is the state not enabled? */ | |
707827f3 DD |
227 | if (cpuidle_state_table[idle_state].enter == NULL) |
228 | continue; | |
229 | ||
230 | drv->states[drv->state_count] = /* structure copy */ | |
231 | cpuidle_state_table[idle_state]; | |
232 | ||
707827f3 DD |
233 | drv->state_count += 1; |
234 | } | |
235 | ||
236 | return 0; | |
237 | } | |
238 | ||
707827f3 DD |
239 | /* |
240 | * pseries_idle_probe() | |
241 | * Choose state table for shared versus dedicated partition | |
242 | */ | |
243 | static int pseries_idle_probe(void) | |
244 | { | |
245 | ||
e8bb3e00 DD |
246 | if (cpuidle_disable != IDLE_NO_OVERRIDE) |
247 | return -ENODEV; | |
248 | ||
b69dbba0 | 249 | if (firmware_has_feature(FW_FEATURE_SPLPAR)) { |
2b038cbc BL |
250 | /* |
251 | * Use local_paca instead of get_lppaca() since | |
252 | * preemption is not disabled, and it is not required in | |
253 | * fact, since lppaca_ptr does not need to be the value | |
254 | * associated to the current CPU, it can be from any CPU. | |
255 | */ | |
256 | if (lppaca_shared_proc(local_paca->lppaca_ptr)) { | |
b69dbba0 | 257 | cpuidle_state_table = shared_states; |
bf7f61f2 DD |
258 | max_idle_state = ARRAY_SIZE(shared_states); |
259 | } else { | |
b69dbba0 | 260 | cpuidle_state_table = dedicated_states; |
bf7f61f2 DD |
261 | max_idle_state = ARRAY_SIZE(dedicated_states); |
262 | } | |
b69dbba0 DD |
263 | } else |
264 | return -ENODEV; | |
707827f3 | 265 | |
78eaa10f SB |
266 | if (max_idle_state > 1) { |
267 | snooze_timeout_en = true; | |
268 | snooze_timeout = cpuidle_state_table[1].target_residency * | |
269 | tb_ticks_per_usec; | |
270 | } | |
707827f3 DD |
271 | return 0; |
272 | } | |
273 | ||
274 | static int __init pseries_processor_idle_init(void) | |
275 | { | |
276 | int retval; | |
277 | ||
278 | retval = pseries_idle_probe(); | |
279 | if (retval) | |
280 | return retval; | |
281 | ||
282 | pseries_cpuidle_driver_init(); | |
b69dbba0 | 283 | retval = cpuidle_register(&pseries_idle_driver, NULL); |
707827f3 DD |
284 | if (retval) { |
285 | printk(KERN_DEBUG "Registration of pseries driver failed.\n"); | |
286 | return retval; | |
287 | } | |
288 | ||
529351fd SAS |
289 | retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, |
290 | "cpuidle/pseries:online", | |
291 | pseries_cpuidle_cpu_online, NULL); | |
292 | WARN_ON(retval < 0); | |
293 | retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD, | |
294 | "cpuidle/pseries:DEAD", NULL, | |
295 | pseries_cpuidle_cpu_dead); | |
296 | WARN_ON(retval < 0); | |
707827f3 | 297 | printk(KERN_DEBUG "pseries_idle_driver registered\n"); |
707827f3 DD |
298 | return 0; |
299 | } | |
300 | ||
12431c64 | 301 | device_initcall(pseries_processor_idle_init); |