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>
21 #include <asm/plpar_wrappers.h>
23 struct cpuidle_driver pseries_idle_driver
= {
24 .name
= "pseries_idle",
28 #define MAX_IDLE_STATE_COUNT 2
30 static int max_idle_state
= MAX_IDLE_STATE_COUNT
- 1;
31 static struct cpuidle_device __percpu
*pseries_cpuidle_devices
;
32 static struct cpuidle_state
*cpuidle_state_table
;
34 static inline void idle_loop_prolog(unsigned long *in_purr
)
36 *in_purr
= mfspr(SPRN_PURR
);
38 * Indicate to the HV that we are idle. Now would be
39 * a good time to find other work to dispatch.
41 get_lppaca()->idle
= 1;
44 static inline void idle_loop_epilog(unsigned long in_purr
)
48 wait_cycles
= be64_to_cpu(get_lppaca()->wait_state_cycles
);
49 wait_cycles
+= mfspr(SPRN_PURR
) - in_purr
;
50 get_lppaca()->wait_state_cycles
= cpu_to_be64(wait_cycles
);
51 get_lppaca()->idle
= 0;
54 static int snooze_loop(struct cpuidle_device
*dev
,
55 struct cpuidle_driver
*drv
,
58 unsigned long in_purr
;
61 idle_loop_prolog(&in_purr
);
63 set_thread_flag(TIF_POLLING_NRFLAG
);
65 while ((!need_resched()) && cpu_online(cpu
)) {
72 clear_thread_flag(TIF_POLLING_NRFLAG
);
75 idle_loop_epilog(in_purr
);
80 static void check_and_cede_processor(void)
83 * Ensure our interrupt state is properly tracked,
84 * also checks if no interrupt has occurred while we
87 if (prep_irq_for_idle()) {
89 #ifdef CONFIG_TRACE_IRQFLAGS
90 /* Ensure that H_CEDE returns with IRQs on */
91 if (WARN_ON(!(mfmsr() & MSR_EE
)))
97 static int dedicated_cede_loop(struct cpuidle_device
*dev
,
98 struct cpuidle_driver
*drv
,
101 unsigned long in_purr
;
103 idle_loop_prolog(&in_purr
);
104 get_lppaca()->donate_dedicated_cpu
= 1;
106 ppc64_runlatch_off();
108 check_and_cede_processor();
110 get_lppaca()->donate_dedicated_cpu
= 0;
112 idle_loop_epilog(in_purr
);
117 static int shared_cede_loop(struct cpuidle_device
*dev
,
118 struct cpuidle_driver
*drv
,
121 unsigned long in_purr
;
123 idle_loop_prolog(&in_purr
);
126 * Yield the processor to the hypervisor. We return if
127 * an external interrupt occurs (which are driven prior
128 * to returning here) or if a prod occurs from another
129 * processor. When returning here, external interrupts
132 check_and_cede_processor();
134 idle_loop_epilog(in_purr
);
140 * States for dedicated partition case.
142 static struct cpuidle_state dedicated_states
[MAX_IDLE_STATE_COUNT
] = {
146 .flags
= CPUIDLE_FLAG_TIME_VALID
,
148 .target_residency
= 0,
149 .enter
= &snooze_loop
},
153 .flags
= CPUIDLE_FLAG_TIME_VALID
,
155 .target_residency
= 100,
156 .enter
= &dedicated_cede_loop
},
160 * States for shared partition case.
162 static struct cpuidle_state shared_states
[MAX_IDLE_STATE_COUNT
] = {
164 .name
= "Shared Cede",
165 .desc
= "Shared Cede",
166 .flags
= CPUIDLE_FLAG_TIME_VALID
,
168 .target_residency
= 0,
169 .enter
= &shared_cede_loop
},
172 void update_smt_snooze_delay(int cpu
, int residency
)
174 struct cpuidle_driver
*drv
= cpuidle_get_driver();
175 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
177 if (cpuidle_state_table
!= dedicated_states
)
181 /* Disable the Nap state on that cpu */
183 dev
->states_usage
[1].disable
= 1;
186 drv
->states
[1].target_residency
= residency
;
189 static int pseries_cpuidle_add_cpu_notifier(struct notifier_block
*n
,
190 unsigned long action
, void *hcpu
)
192 int hotcpu
= (unsigned long)hcpu
;
193 struct cpuidle_device
*dev
=
194 per_cpu_ptr(pseries_cpuidle_devices
, hotcpu
);
196 if (dev
&& cpuidle_get_driver()) {
199 case CPU_ONLINE_FROZEN
:
200 cpuidle_pause_and_lock();
201 cpuidle_enable_device(dev
);
202 cpuidle_resume_and_unlock();
206 case CPU_DEAD_FROZEN
:
207 cpuidle_pause_and_lock();
208 cpuidle_disable_device(dev
);
209 cpuidle_resume_and_unlock();
219 static struct notifier_block setup_hotplug_notifier
= {
220 .notifier_call
= pseries_cpuidle_add_cpu_notifier
,
224 * pseries_cpuidle_driver_init()
226 static int pseries_cpuidle_driver_init(void)
229 struct cpuidle_driver
*drv
= &pseries_idle_driver
;
231 drv
->state_count
= 0;
233 for (idle_state
= 0; idle_state
< MAX_IDLE_STATE_COUNT
; ++idle_state
) {
235 if (idle_state
> max_idle_state
)
238 /* is the state not enabled? */
239 if (cpuidle_state_table
[idle_state
].enter
== NULL
)
242 drv
->states
[drv
->state_count
] = /* structure copy */
243 cpuidle_state_table
[idle_state
];
245 drv
->state_count
+= 1;
251 /* pseries_idle_devices_uninit(void)
252 * unregister cpuidle devices and de-allocate memory
254 static void pseries_idle_devices_uninit(void)
257 struct cpuidle_device
*dev
;
259 for_each_possible_cpu(i
) {
260 dev
= per_cpu_ptr(pseries_cpuidle_devices
, i
);
261 cpuidle_unregister_device(dev
);
264 free_percpu(pseries_cpuidle_devices
);
268 /* pseries_idle_devices_init()
269 * allocate, initialize and register cpuidle device
271 static int pseries_idle_devices_init(void)
274 struct cpuidle_driver
*drv
= &pseries_idle_driver
;
275 struct cpuidle_device
*dev
;
277 pseries_cpuidle_devices
= alloc_percpu(struct cpuidle_device
);
278 if (pseries_cpuidle_devices
== NULL
)
281 for_each_possible_cpu(i
) {
282 dev
= per_cpu_ptr(pseries_cpuidle_devices
, i
);
283 dev
->state_count
= drv
->state_count
;
285 if (cpuidle_register_device(dev
)) {
287 "cpuidle_register_device %d failed!\n", i
);
296 * pseries_idle_probe()
297 * Choose state table for shared versus dedicated partition
299 static int pseries_idle_probe(void)
302 if (!firmware_has_feature(FW_FEATURE_SPLPAR
))
305 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
308 if (max_idle_state
== 0) {
309 printk(KERN_DEBUG
"pseries processor idle disabled.\n");
313 if (lppaca_shared_proc(get_lppaca()))
314 cpuidle_state_table
= shared_states
;
316 cpuidle_state_table
= dedicated_states
;
321 static int __init
pseries_processor_idle_init(void)
325 retval
= pseries_idle_probe();
329 pseries_cpuidle_driver_init();
330 retval
= cpuidle_register_driver(&pseries_idle_driver
);
332 printk(KERN_DEBUG
"Registration of pseries driver failed.\n");
336 retval
= pseries_idle_devices_init();
338 pseries_idle_devices_uninit();
339 cpuidle_unregister_driver(&pseries_idle_driver
);
343 register_cpu_notifier(&setup_hotplug_notifier
);
344 printk(KERN_DEBUG
"pseries_idle_driver registered\n");
349 static void __exit
pseries_processor_idle_exit(void)
352 unregister_cpu_notifier(&setup_hotplug_notifier
);
353 pseries_idle_devices_uninit();
354 cpuidle_unregister_driver(&pseries_idle_driver
);
359 module_init(pseries_processor_idle_init
);
360 module_exit(pseries_processor_idle_exit
);
362 MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
363 MODULE_DESCRIPTION("Cpuidle driver for POWER");
364 MODULE_LICENSE("GPL");