2 * cpuidle-pseries - 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/plpar_wrappers.h>
22 struct cpuidle_driver pseries_idle_driver = {
23 .name = "pseries_idle",
27 #define MAX_IDLE_STATE_COUNT 2
29 static int max_idle_state = MAX_IDLE_STATE_COUNT - 1;
30 static struct cpuidle_device __percpu *pseries_cpuidle_devices;
31 static struct cpuidle_state *cpuidle_state_table;
33 static inline void idle_loop_prolog(unsigned long *in_purr)
35 *in_purr = mfspr(SPRN_PURR);
37 * Indicate to the HV that we are idle. Now would be
38 * a good time to find other work to dispatch.
40 get_lppaca()->idle = 1;
43 static inline void idle_loop_epilog(unsigned long in_purr)
47 wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles);
48 wait_cycles += mfspr(SPRN_PURR) - in_purr;
49 get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles);
50 get_lppaca()->idle = 0;
53 static int snooze_loop(struct cpuidle_device *dev,
54 struct cpuidle_driver *drv,
57 unsigned long in_purr;
60 idle_loop_prolog(&in_purr);
62 set_thread_flag(TIF_POLLING_NRFLAG);
64 while ((!need_resched()) && cpu_online(cpu)) {
70 clear_thread_flag(TIF_POLLING_NRFLAG);
73 idle_loop_epilog(in_purr);
78 static void check_and_cede_processor(void)
81 * Ensure our interrupt state is properly tracked,
82 * also checks if no interrupt has occurred while we
85 if (prep_irq_for_idle()) {
87 #ifdef CONFIG_TRACE_IRQFLAGS
88 /* Ensure that H_CEDE returns with IRQs on */
89 if (WARN_ON(!(mfmsr() & MSR_EE)))
95 static int dedicated_cede_loop(struct cpuidle_device *dev,
96 struct cpuidle_driver *drv,
99 unsigned long in_purr;
101 idle_loop_prolog(&in_purr);
102 get_lppaca()->donate_dedicated_cpu = 1;
105 check_and_cede_processor();
107 get_lppaca()->donate_dedicated_cpu = 0;
109 idle_loop_epilog(in_purr);
114 static int shared_cede_loop(struct cpuidle_device *dev,
115 struct cpuidle_driver *drv,
118 unsigned long in_purr;
120 idle_loop_prolog(&in_purr);
123 * Yield the processor to the hypervisor. We return if
124 * an external interrupt occurs (which are driven prior
125 * to returning here) or if a prod occurs from another
126 * processor. When returning here, external interrupts
129 check_and_cede_processor();
131 idle_loop_epilog(in_purr);
137 * States for dedicated partition case.
139 static struct cpuidle_state dedicated_states[MAX_IDLE_STATE_COUNT] = {
143 .flags = CPUIDLE_FLAG_TIME_VALID,
145 .target_residency = 0,
146 .enter = &snooze_loop },
150 .flags = CPUIDLE_FLAG_TIME_VALID,
152 .target_residency = 100,
153 .enter = &dedicated_cede_loop },
157 * States for shared partition case.
159 static struct cpuidle_state shared_states[MAX_IDLE_STATE_COUNT] = {
161 .name = "Shared Cede",
162 .desc = "Shared Cede",
163 .flags = CPUIDLE_FLAG_TIME_VALID,
165 .target_residency = 0,
166 .enter = &shared_cede_loop },
169 void update_smt_snooze_delay(int cpu, int residency)
171 struct cpuidle_driver *drv = cpuidle_get_driver();
172 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
174 if (cpuidle_state_table != dedicated_states)
178 /* Disable the Nap state on that cpu */
180 dev->states_usage[1].disable = 1;
183 drv->states[1].target_residency = residency;
186 static int pseries_cpuidle_add_cpu_notifier(struct notifier_block *n,
187 unsigned long action, void *hcpu)
189 int hotcpu = (unsigned long)hcpu;
190 struct cpuidle_device *dev =
191 per_cpu_ptr(pseries_cpuidle_devices, hotcpu);
193 if (dev && cpuidle_get_driver()) {
196 case CPU_ONLINE_FROZEN:
197 cpuidle_pause_and_lock();
198 cpuidle_enable_device(dev);
199 cpuidle_resume_and_unlock();
203 case CPU_DEAD_FROZEN:
204 cpuidle_pause_and_lock();
205 cpuidle_disable_device(dev);
206 cpuidle_resume_and_unlock();
216 static struct notifier_block setup_hotplug_notifier = {
217 .notifier_call = pseries_cpuidle_add_cpu_notifier,
221 * pseries_cpuidle_driver_init()
223 static int pseries_cpuidle_driver_init(void)
226 struct cpuidle_driver *drv = &pseries_idle_driver;
228 drv->state_count = 0;
230 for (idle_state = 0; idle_state < MAX_IDLE_STATE_COUNT; ++idle_state) {
232 if (idle_state > max_idle_state)
235 /* is the state not enabled? */
236 if (cpuidle_state_table[idle_state].enter == NULL)
239 drv->states[drv->state_count] = /* structure copy */
240 cpuidle_state_table[idle_state];
242 drv->state_count += 1;
248 /* pseries_idle_devices_uninit(void)
249 * unregister cpuidle devices and de-allocate memory
251 static void pseries_idle_devices_uninit(void)
254 struct cpuidle_device *dev;
256 for_each_possible_cpu(i) {
257 dev = per_cpu_ptr(pseries_cpuidle_devices, i);
258 cpuidle_unregister_device(dev);
261 free_percpu(pseries_cpuidle_devices);
265 /* pseries_idle_devices_init()
266 * allocate, initialize and register cpuidle device
268 static int pseries_idle_devices_init(void)
271 struct cpuidle_driver *drv = &pseries_idle_driver;
272 struct cpuidle_device *dev;
274 pseries_cpuidle_devices = alloc_percpu(struct cpuidle_device);
275 if (pseries_cpuidle_devices == NULL)
278 for_each_possible_cpu(i) {
279 dev = per_cpu_ptr(pseries_cpuidle_devices, i);
280 dev->state_count = drv->state_count;
282 if (cpuidle_register_device(dev)) {
284 "cpuidle_register_device %d failed!\n", i);
293 * pseries_idle_probe()
294 * Choose state table for shared versus dedicated partition
296 static int pseries_idle_probe(void)
299 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
302 if (cpuidle_disable != IDLE_NO_OVERRIDE)
305 if (max_idle_state == 0) {
306 printk(KERN_DEBUG "pseries processor idle disabled.\n");
310 if (lppaca_shared_proc(get_lppaca()))
311 cpuidle_state_table = shared_states;
313 cpuidle_state_table = dedicated_states;
318 static int __init pseries_processor_idle_init(void)
322 retval = pseries_idle_probe();
326 pseries_cpuidle_driver_init();
327 retval = cpuidle_register_driver(&pseries_idle_driver);
329 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
333 retval = pseries_idle_devices_init();
335 pseries_idle_devices_uninit();
336 cpuidle_unregister_driver(&pseries_idle_driver);
340 register_cpu_notifier(&setup_hotplug_notifier);
341 printk(KERN_DEBUG "pseries_idle_driver registered\n");
346 static void __exit pseries_processor_idle_exit(void)
349 unregister_cpu_notifier(&setup_hotplug_notifier);
350 pseries_idle_devices_uninit();
351 cpuidle_unregister_driver(&pseries_idle_driver);
356 module_init(pseries_processor_idle_init);
357 module_exit(pseries_processor_idle_exit);
359 MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
360 MODULE_DESCRIPTION("Cpuidle driver for POWER");
361 MODULE_LICENSE("GPL");