2 * processor_idle - idle state cpuidle driver.
3 * Adapted from drivers/idle/intel_idle.c and
4 * drivers/acpi/processor_idle.c
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>
14 #include <linux/notifier.h>
18 #include <asm/machdep.h>
19 #include <asm/firmware.h>
20 #include <asm/runlatch.h>
22 #include "plpar_wrappers.h"
25 struct cpuidle_driver pseries_idle_driver = {
26 .name = "pseries_idle",
30 #define MAX_IDLE_STATE_COUNT 2
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;
36 static inline void idle_loop_prolog(unsigned long *in_purr, ktime_t *kt_before)
39 *kt_before = ktime_get();
40 *in_purr = mfspr(SPRN_PURR);
42 * Indicate to the HV that we are idle. Now would be
43 * a good time to find other work to dispatch.
45 get_lppaca()->idle = 1;
48 static inline s64 idle_loop_epilog(unsigned long in_purr, ktime_t kt_before)
50 get_lppaca()->wait_state_cycles += mfspr(SPRN_PURR) - in_purr;
51 get_lppaca()->idle = 0;
53 return ktime_to_us(ktime_sub(ktime_get(), kt_before));
56 static int snooze_loop(struct cpuidle_device *dev,
57 struct cpuidle_driver *drv,
60 unsigned long in_purr;
64 idle_loop_prolog(&in_purr, &kt_before);
66 set_thread_flag(TIF_POLLING_NRFLAG);
68 while ((!need_resched()) && cpu_online(cpu)) {
75 clear_thread_flag(TIF_POLLING_NRFLAG);
79 (int)idle_loop_epilog(in_purr, kt_before);
83 static void check_and_cede_processor(void)
86 * Ensure our interrupt state is properly tracked,
87 * also checks if no interrupt has occurred while we
90 if (prep_irq_for_idle()) {
92 #ifdef CONFIG_TRACE_IRQFLAGS
93 /* Ensure that H_CEDE returns with IRQs on */
94 if (WARN_ON(!(mfmsr() & MSR_EE)))
100 static int dedicated_cede_loop(struct cpuidle_device *dev,
101 struct cpuidle_driver *drv,
104 unsigned long in_purr;
107 idle_loop_prolog(&in_purr, &kt_before);
108 get_lppaca()->donate_dedicated_cpu = 1;
110 ppc64_runlatch_off();
112 check_and_cede_processor();
114 get_lppaca()->donate_dedicated_cpu = 0;
115 dev->last_residency =
116 (int)idle_loop_epilog(in_purr, kt_before);
120 static int shared_cede_loop(struct cpuidle_device *dev,
121 struct cpuidle_driver *drv,
124 unsigned long in_purr;
127 idle_loop_prolog(&in_purr, &kt_before);
130 * Yield the processor to the hypervisor. We return if
131 * an external interrupt occurs (which are driven prior
132 * to returning here) or if a prod occurs from another
133 * processor. When returning here, external interrupts
136 check_and_cede_processor();
138 dev->last_residency =
139 (int)idle_loop_epilog(in_purr, kt_before);
144 * States for dedicated partition case.
146 static struct cpuidle_state dedicated_states[MAX_IDLE_STATE_COUNT] = {
150 .flags = CPUIDLE_FLAG_TIME_VALID,
152 .target_residency = 0,
153 .enter = &snooze_loop },
157 .flags = CPUIDLE_FLAG_TIME_VALID,
159 .target_residency = 100,
160 .enter = &dedicated_cede_loop },
164 * States for shared partition case.
166 static struct cpuidle_state shared_states[MAX_IDLE_STATE_COUNT] = {
168 .name = "Shared Cede",
169 .desc = "Shared Cede",
170 .flags = CPUIDLE_FLAG_TIME_VALID,
172 .target_residency = 0,
173 .enter = &shared_cede_loop },
176 void update_smt_snooze_delay(int cpu, int residency)
178 struct cpuidle_driver *drv = cpuidle_get_driver();
179 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
181 if (cpuidle_state_table != dedicated_states)
185 /* Disable the Nap state on that cpu */
187 dev->states_usage[1].disable = 1;
190 drv->states[1].target_residency = residency;
193 static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n,
194 unsigned long action, void *hcpu)
196 int hotcpu = (unsigned long)hcpu;
197 struct cpuidle_device *dev =
198 per_cpu_ptr(pseries_cpuidle_devices, hotcpu);
200 if (dev && cpuidle_get_driver()) {
203 case CPU_ONLINE_FROZEN:
204 cpuidle_pause_and_lock();
205 cpuidle_enable_device(dev);
206 cpuidle_resume_and_unlock();
210 case CPU_DEAD_FROZEN:
211 cpuidle_pause_and_lock();
212 cpuidle_disable_device(dev);
213 cpuidle_resume_and_unlock();
223 static struct notifier_block setup_hotplug_notifier = {
224 .notifier_call = pseries_cpuidle_add_cpu_notifier,
228 * pseries_cpuidle_driver_init()
230 static int pseries_cpuidle_driver_init(void)
233 struct cpuidle_driver *drv = &pseries_idle_driver;
235 drv->state_count = 0;
237 for (idle_state = 0; idle_state < MAX_IDLE_STATE_COUNT; ++idle_state) {
239 if (idle_state > max_idle_state)
242 /* is the state not enabled? */
243 if (cpuidle_state_table[idle_state].enter == NULL)
246 drv->states[drv->state_count] = /* structure copy */
247 cpuidle_state_table[idle_state];
249 drv->state_count += 1;
255 /* pseries_idle_devices_uninit(void)
256 * unregister cpuidle devices and de-allocate memory
258 static void pseries_idle_devices_uninit(void)
261 struct cpuidle_device *dev;
263 for_each_possible_cpu(i) {
264 dev = per_cpu_ptr(pseries_cpuidle_devices, i);
265 cpuidle_unregister_device(dev);
268 free_percpu(pseries_cpuidle_devices);
272 /* pseries_idle_devices_init()
273 * allocate, initialize and register cpuidle device
275 static int pseries_idle_devices_init(void)
278 struct cpuidle_driver *drv = &pseries_idle_driver;
279 struct cpuidle_device *dev;
281 pseries_cpuidle_devices = alloc_percpu(struct cpuidle_device);
282 if (pseries_cpuidle_devices == NULL)
285 for_each_possible_cpu(i) {
286 dev = per_cpu_ptr(pseries_cpuidle_devices, i);
287 dev->state_count = drv->state_count;
289 if (cpuidle_register_device(dev)) {
291 "cpuidle_register_device %d failed!\n", i);
300 * pseries_idle_probe()
301 * Choose state table for shared versus dedicated partition
303 static int pseries_idle_probe(void)
306 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
309 if (cpuidle_disable != IDLE_NO_OVERRIDE)
312 if (max_idle_state == 0) {
313 printk(KERN_DEBUG "pseries processor idle disabled.\n");
317 if (get_lppaca()->shared_proc)
318 cpuidle_state_table = shared_states;
320 cpuidle_state_table = dedicated_states;
325 static int __init pseries_processor_idle_init(void)
329 retval = pseries_idle_probe();
333 pseries_cpuidle_driver_init();
334 retval = cpuidle_register_driver(&pseries_idle_driver);
336 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
340 retval = pseries_idle_devices_init();
342 pseries_idle_devices_uninit();
343 cpuidle_unregister_driver(&pseries_idle_driver);
347 register_cpu_notifier(&setup_hotplug_notifier);
348 printk(KERN_DEBUG "pseries_idle_driver registered\n");
353 static void __exit pseries_processor_idle_exit(void)
356 unregister_cpu_notifier(&setup_hotplug_notifier);
357 pseries_idle_devices_uninit();
358 cpuidle_unregister_driver(&pseries_idle_driver);
363 module_init(pseries_processor_idle_init);
364 module_exit(pseries_processor_idle_exit);
366 MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
367 MODULE_DESCRIPTION("Cpuidle driver for POWER");
368 MODULE_LICENSE("GPL");