Commit | Line | Data |
---|---|---|
707827f3 DD |
1 | /* |
2 | * processor_idle - idle state cpuidle driver. | |
3 | * Adapted from drivers/idle/intel_idle.c and | |
4 | * drivers/acpi/processor_idle.c | |
5 | * | |
6 | */ | |
7 | ||
8 | #include <linux/kernel.h> | |
9 | #include <linux/module.h> | |
10 | #include <linux/init.h> | |
11 | #include <linux/moduleparam.h> | |
12 | #include <linux/cpuidle.h> | |
13 | #include <linux/cpu.h> | |
16aaaff6 | 14 | #include <linux/notifier.h> |
707827f3 DD |
15 | |
16 | #include <asm/paca.h> | |
17 | #include <asm/reg.h> | |
707827f3 DD |
18 | #include <asm/machdep.h> |
19 | #include <asm/firmware.h> | |
ae3a197e | 20 | #include <asm/runlatch.h> |
707827f3 DD |
21 | |
22 | #include "plpar_wrappers.h" | |
23 | #include "pseries.h" | |
24 | ||
25 | struct cpuidle_driver pseries_idle_driver = { | |
26 | .name = "pseries_idle", | |
27 | .owner = THIS_MODULE, | |
28 | }; | |
29 | ||
30 | #define MAX_IDLE_STATE_COUNT 2 | |
31 | ||
32 | static int max_idle_state = MAX_IDLE_STATE_COUNT - 1; | |
33 | static struct cpuidle_device __percpu *pseries_cpuidle_devices; | |
34 | static struct cpuidle_state *cpuidle_state_table; | |
35 | ||
707827f3 DD |
36 | static inline void idle_loop_prolog(unsigned long *in_purr, ktime_t *kt_before) |
37 | { | |
38 | ||
39 | *kt_before = ktime_get_real(); | |
40 | *in_purr = mfspr(SPRN_PURR); | |
41 | /* | |
42 | * Indicate to the HV that we are idle. Now would be | |
43 | * a good time to find other work to dispatch. | |
44 | */ | |
45 | get_lppaca()->idle = 1; | |
46 | } | |
47 | ||
48 | static inline s64 idle_loop_epilog(unsigned long in_purr, ktime_t kt_before) | |
49 | { | |
50 | get_lppaca()->wait_state_cycles += mfspr(SPRN_PURR) - in_purr; | |
51 | get_lppaca()->idle = 0; | |
52 | ||
53 | return ktime_to_us(ktime_sub(ktime_get_real(), kt_before)); | |
54 | } | |
55 | ||
56 | static int snooze_loop(struct cpuidle_device *dev, | |
57 | struct cpuidle_driver *drv, | |
58 | int index) | |
59 | { | |
60 | unsigned long in_purr; | |
61 | ktime_t kt_before; | |
62 | unsigned long start_snooze; | |
63 | long snooze = drv->states[0].target_residency; | |
64 | ||
65 | idle_loop_prolog(&in_purr, &kt_before); | |
66 | ||
67 | if (snooze) { | |
68 | start_snooze = get_tb() + snooze * tb_ticks_per_usec; | |
69 | local_irq_enable(); | |
70 | set_thread_flag(TIF_POLLING_NRFLAG); | |
71 | ||
72 | while ((snooze < 0) || (get_tb() < start_snooze)) { | |
73 | if (need_resched() || cpu_is_offline(dev->cpu)) | |
74 | goto out; | |
75 | ppc64_runlatch_off(); | |
76 | HMT_low(); | |
77 | HMT_very_low(); | |
78 | } | |
79 | ||
80 | HMT_medium(); | |
81 | clear_thread_flag(TIF_POLLING_NRFLAG); | |
82 | smp_mb(); | |
83 | local_irq_disable(); | |
84 | } | |
85 | ||
86 | out: | |
87 | HMT_medium(); | |
88 | dev->last_residency = | |
89 | (int)idle_loop_epilog(in_purr, kt_before); | |
90 | return index; | |
91 | } | |
92 | ||
7230c564 BH |
93 | static void check_and_cede_processor(void) |
94 | { | |
95 | /* | |
be2cf20a BH |
96 | * Ensure our interrupt state is properly tracked, |
97 | * also checks if no interrupt has occurred while we | |
98 | * were soft-disabled | |
7230c564 | 99 | */ |
be2cf20a | 100 | if (prep_irq_for_idle()) { |
7230c564 | 101 | cede_processor(); |
be2cf20a BH |
102 | #ifdef CONFIG_TRACE_IRQFLAGS |
103 | /* Ensure that H_CEDE returns with IRQs on */ | |
104 | if (WARN_ON(!(mfmsr() & MSR_EE))) | |
105 | __hard_irq_enable(); | |
106 | #endif | |
107 | } | |
7230c564 BH |
108 | } |
109 | ||
707827f3 DD |
110 | static int dedicated_cede_loop(struct cpuidle_device *dev, |
111 | struct cpuidle_driver *drv, | |
112 | int index) | |
113 | { | |
114 | unsigned long in_purr; | |
115 | ktime_t kt_before; | |
116 | ||
117 | idle_loop_prolog(&in_purr, &kt_before); | |
118 | get_lppaca()->donate_dedicated_cpu = 1; | |
119 | ||
120 | ppc64_runlatch_off(); | |
121 | HMT_medium(); | |
7230c564 | 122 | check_and_cede_processor(); |
707827f3 DD |
123 | |
124 | get_lppaca()->donate_dedicated_cpu = 0; | |
125 | dev->last_residency = | |
126 | (int)idle_loop_epilog(in_purr, kt_before); | |
127 | return index; | |
128 | } | |
129 | ||
130 | static int shared_cede_loop(struct cpuidle_device *dev, | |
131 | struct cpuidle_driver *drv, | |
132 | int index) | |
133 | { | |
134 | unsigned long in_purr; | |
135 | ktime_t kt_before; | |
136 | ||
137 | idle_loop_prolog(&in_purr, &kt_before); | |
138 | ||
139 | /* | |
140 | * Yield the processor to the hypervisor. We return if | |
141 | * an external interrupt occurs (which are driven prior | |
142 | * to returning here) or if a prod occurs from another | |
143 | * processor. When returning here, external interrupts | |
144 | * are enabled. | |
145 | */ | |
7230c564 | 146 | check_and_cede_processor(); |
707827f3 DD |
147 | |
148 | dev->last_residency = | |
149 | (int)idle_loop_epilog(in_purr, kt_before); | |
150 | return index; | |
151 | } | |
152 | ||
153 | /* | |
154 | * States for dedicated partition case. | |
155 | */ | |
156 | static struct cpuidle_state dedicated_states[MAX_IDLE_STATE_COUNT] = { | |
157 | { /* Snooze */ | |
158 | .name = "snooze", | |
159 | .desc = "snooze", | |
160 | .flags = CPUIDLE_FLAG_TIME_VALID, | |
161 | .exit_latency = 0, | |
162 | .target_residency = 0, | |
163 | .enter = &snooze_loop }, | |
164 | { /* CEDE */ | |
165 | .name = "CEDE", | |
166 | .desc = "CEDE", | |
167 | .flags = CPUIDLE_FLAG_TIME_VALID, | |
168 | .exit_latency = 1, | |
169 | .target_residency = 10, | |
170 | .enter = &dedicated_cede_loop }, | |
171 | }; | |
172 | ||
173 | /* | |
174 | * States for shared partition case. | |
175 | */ | |
176 | static struct cpuidle_state shared_states[MAX_IDLE_STATE_COUNT] = { | |
177 | { /* Shared Cede */ | |
178 | .name = "Shared Cede", | |
179 | .desc = "Shared Cede", | |
180 | .flags = CPUIDLE_FLAG_TIME_VALID, | |
181 | .exit_latency = 0, | |
182 | .target_residency = 0, | |
183 | .enter = &shared_cede_loop }, | |
184 | }; | |
185 | ||
8ea959a1 DD |
186 | void update_smt_snooze_delay(int cpu, int residency) |
187 | { | |
188 | struct cpuidle_driver *drv = cpuidle_get_driver(); | |
189 | struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu); | |
190 | ||
191 | if (cpuidle_state_table != dedicated_states) | |
192 | return; | |
193 | ||
194 | if (residency < 0) { | |
195 | /* Disable the Nap state on that cpu */ | |
196 | if (dev) | |
197 | dev->states_usage[1].disable = 1; | |
198 | } else | |
199 | if (drv) | |
200 | drv->states[0].target_residency = residency; | |
201 | } | |
202 | ||
16aaaff6 DD |
203 | static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n, |
204 | unsigned long action, void *hcpu) | |
707827f3 | 205 | { |
16aaaff6 | 206 | int hotcpu = (unsigned long)hcpu; |
707827f3 | 207 | struct cpuidle_device *dev = |
16aaaff6 DD |
208 | per_cpu_ptr(pseries_cpuidle_devices, hotcpu); |
209 | ||
852d8cb1 DD |
210 | if (dev && cpuidle_get_driver()) { |
211 | switch (action) { | |
212 | case CPU_ONLINE: | |
213 | case CPU_ONLINE_FROZEN: | |
214 | cpuidle_pause_and_lock(); | |
16aaaff6 | 215 | cpuidle_enable_device(dev); |
852d8cb1 DD |
216 | cpuidle_resume_and_unlock(); |
217 | break; | |
218 | ||
219 | case CPU_DEAD: | |
220 | case CPU_DEAD_FROZEN: | |
221 | cpuidle_pause_and_lock(); | |
222 | cpuidle_disable_device(dev); | |
223 | cpuidle_resume_and_unlock(); | |
224 | break; | |
225 | ||
226 | default: | |
227 | return NOTIFY_DONE; | |
16aaaff6 | 228 | } |
707827f3 | 229 | } |
16aaaff6 | 230 | return NOTIFY_OK; |
707827f3 DD |
231 | } |
232 | ||
16aaaff6 DD |
233 | static struct notifier_block setup_hotplug_notifier = { |
234 | .notifier_call = pseries_cpuidle_add_cpu_notifier, | |
235 | }; | |
236 | ||
707827f3 DD |
237 | /* |
238 | * pseries_cpuidle_driver_init() | |
239 | */ | |
240 | static int pseries_cpuidle_driver_init(void) | |
241 | { | |
242 | int idle_state; | |
243 | struct cpuidle_driver *drv = &pseries_idle_driver; | |
244 | ||
245 | drv->state_count = 0; | |
246 | ||
247 | for (idle_state = 0; idle_state < MAX_IDLE_STATE_COUNT; ++idle_state) { | |
248 | ||
249 | if (idle_state > max_idle_state) | |
250 | break; | |
251 | ||
252 | /* is the state not enabled? */ | |
253 | if (cpuidle_state_table[idle_state].enter == NULL) | |
254 | continue; | |
255 | ||
256 | drv->states[drv->state_count] = /* structure copy */ | |
257 | cpuidle_state_table[idle_state]; | |
258 | ||
707827f3 DD |
259 | drv->state_count += 1; |
260 | } | |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
265 | /* pseries_idle_devices_uninit(void) | |
266 | * unregister cpuidle devices and de-allocate memory | |
267 | */ | |
268 | static void pseries_idle_devices_uninit(void) | |
269 | { | |
270 | int i; | |
271 | struct cpuidle_device *dev; | |
272 | ||
273 | for_each_possible_cpu(i) { | |
274 | dev = per_cpu_ptr(pseries_cpuidle_devices, i); | |
275 | cpuidle_unregister_device(dev); | |
276 | } | |
277 | ||
278 | free_percpu(pseries_cpuidle_devices); | |
279 | return; | |
280 | } | |
281 | ||
282 | /* pseries_idle_devices_init() | |
283 | * allocate, initialize and register cpuidle device | |
284 | */ | |
285 | static int pseries_idle_devices_init(void) | |
286 | { | |
287 | int i; | |
288 | struct cpuidle_driver *drv = &pseries_idle_driver; | |
289 | struct cpuidle_device *dev; | |
290 | ||
291 | pseries_cpuidle_devices = alloc_percpu(struct cpuidle_device); | |
292 | if (pseries_cpuidle_devices == NULL) | |
293 | return -ENOMEM; | |
294 | ||
295 | for_each_possible_cpu(i) { | |
296 | dev = per_cpu_ptr(pseries_cpuidle_devices, i); | |
297 | dev->state_count = drv->state_count; | |
298 | dev->cpu = i; | |
299 | if (cpuidle_register_device(dev)) { | |
300 | printk(KERN_DEBUG \ | |
301 | "cpuidle_register_device %d failed!\n", i); | |
302 | return -EIO; | |
303 | } | |
304 | } | |
305 | ||
306 | return 0; | |
307 | } | |
308 | ||
309 | /* | |
310 | * pseries_idle_probe() | |
311 | * Choose state table for shared versus dedicated partition | |
312 | */ | |
313 | static int pseries_idle_probe(void) | |
314 | { | |
315 | ||
316 | if (!firmware_has_feature(FW_FEATURE_SPLPAR)) | |
317 | return -ENODEV; | |
318 | ||
e8bb3e00 DD |
319 | if (cpuidle_disable != IDLE_NO_OVERRIDE) |
320 | return -ENODEV; | |
321 | ||
707827f3 DD |
322 | if (max_idle_state == 0) { |
323 | printk(KERN_DEBUG "pseries processor idle disabled.\n"); | |
324 | return -EPERM; | |
325 | } | |
326 | ||
327 | if (get_lppaca()->shared_proc) | |
328 | cpuidle_state_table = shared_states; | |
329 | else | |
330 | cpuidle_state_table = dedicated_states; | |
331 | ||
332 | return 0; | |
333 | } | |
334 | ||
335 | static int __init pseries_processor_idle_init(void) | |
336 | { | |
337 | int retval; | |
338 | ||
339 | retval = pseries_idle_probe(); | |
340 | if (retval) | |
341 | return retval; | |
342 | ||
343 | pseries_cpuidle_driver_init(); | |
344 | retval = cpuidle_register_driver(&pseries_idle_driver); | |
345 | if (retval) { | |
346 | printk(KERN_DEBUG "Registration of pseries driver failed.\n"); | |
347 | return retval; | |
348 | } | |
349 | ||
350 | retval = pseries_idle_devices_init(); | |
351 | if (retval) { | |
352 | pseries_idle_devices_uninit(); | |
353 | cpuidle_unregister_driver(&pseries_idle_driver); | |
354 | return retval; | |
355 | } | |
356 | ||
16aaaff6 | 357 | register_cpu_notifier(&setup_hotplug_notifier); |
707827f3 DD |
358 | printk(KERN_DEBUG "pseries_idle_driver registered\n"); |
359 | ||
360 | return 0; | |
361 | } | |
362 | ||
363 | static void __exit pseries_processor_idle_exit(void) | |
364 | { | |
365 | ||
852d8cb1 | 366 | unregister_cpu_notifier(&setup_hotplug_notifier); |
707827f3 DD |
367 | pseries_idle_devices_uninit(); |
368 | cpuidle_unregister_driver(&pseries_idle_driver); | |
369 | ||
370 | return; | |
371 | } | |
372 | ||
373 | module_init(pseries_processor_idle_init); | |
374 | module_exit(pseries_processor_idle_exit); | |
375 | ||
376 | MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>"); | |
377 | MODULE_DESCRIPTION("Cpuidle driver for POWER"); | |
378 | MODULE_LICENSE("GPL"); |