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
)
38 *in_purr
= mfspr(SPRN_PURR
);
40 * Indicate to the HV that we are idle. Now would be
41 * a good time to find other work to dispatch.
43 get_lppaca()->idle
= 1;
46 static inline void idle_loop_epilog(unsigned long in_purr
)
48 get_lppaca()->wait_state_cycles
+= mfspr(SPRN_PURR
) - in_purr
;
49 get_lppaca()->idle
= 0;
52 static int snooze_loop(struct cpuidle_device
*dev
,
53 struct cpuidle_driver
*drv
,
56 unsigned long in_purr
;
59 idle_loop_prolog(&in_purr
);
61 set_thread_flag(TIF_POLLING_NRFLAG
);
63 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;
104 ppc64_runlatch_off();
106 check_and_cede_processor();
108 get_lppaca()->donate_dedicated_cpu
= 0;
110 idle_loop_epilog(in_purr
);
115 static int shared_cede_loop(struct cpuidle_device
*dev
,
116 struct cpuidle_driver
*drv
,
119 unsigned long in_purr
;
121 idle_loop_prolog(&in_purr
);
124 * Yield the processor to the hypervisor. We return if
125 * an external interrupt occurs (which are driven prior
126 * to returning here) or if a prod occurs from another
127 * processor. When returning here, external interrupts
130 check_and_cede_processor();
132 idle_loop_epilog(in_purr
);
138 * States for dedicated partition case.
140 static struct cpuidle_state dedicated_states
[MAX_IDLE_STATE_COUNT
] = {
144 .flags
= CPUIDLE_FLAG_TIME_VALID
,
146 .target_residency
= 0,
147 .enter
= &snooze_loop
},
151 .flags
= CPUIDLE_FLAG_TIME_VALID
,
153 .target_residency
= 100,
154 .enter
= &dedicated_cede_loop
},
158 * States for shared partition case.
160 static struct cpuidle_state shared_states
[MAX_IDLE_STATE_COUNT
] = {
162 .name
= "Shared Cede",
163 .desc
= "Shared Cede",
164 .flags
= CPUIDLE_FLAG_TIME_VALID
,
166 .target_residency
= 0,
167 .enter
= &shared_cede_loop
},
170 void update_smt_snooze_delay(int cpu
, int residency
)
172 struct cpuidle_driver
*drv
= cpuidle_get_driver();
173 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
175 if (cpuidle_state_table
!= dedicated_states
)
179 /* Disable the Nap state on that cpu */
181 dev
->states_usage
[1].disable
= 1;
184 drv
->states
[1].target_residency
= residency
;
187 static int pseries_cpuidle_add_cpu_notifier(struct notifier_block
*n
,
188 unsigned long action
, void *hcpu
)
190 int hotcpu
= (unsigned long)hcpu
;
191 struct cpuidle_device
*dev
=
192 per_cpu_ptr(pseries_cpuidle_devices
, hotcpu
);
194 if (dev
&& cpuidle_get_driver()) {
197 case CPU_ONLINE_FROZEN
:
198 cpuidle_pause_and_lock();
199 cpuidle_enable_device(dev
);
200 cpuidle_resume_and_unlock();
204 case CPU_DEAD_FROZEN
:
205 cpuidle_pause_and_lock();
206 cpuidle_disable_device(dev
);
207 cpuidle_resume_and_unlock();
217 static struct notifier_block setup_hotplug_notifier
= {
218 .notifier_call
= pseries_cpuidle_add_cpu_notifier
,
222 * pseries_cpuidle_driver_init()
224 static int pseries_cpuidle_driver_init(void)
227 struct cpuidle_driver
*drv
= &pseries_idle_driver
;
229 drv
->state_count
= 0;
231 for (idle_state
= 0; idle_state
< MAX_IDLE_STATE_COUNT
; ++idle_state
) {
233 if (idle_state
> max_idle_state
)
236 /* is the state not enabled? */
237 if (cpuidle_state_table
[idle_state
].enter
== NULL
)
240 drv
->states
[drv
->state_count
] = /* structure copy */
241 cpuidle_state_table
[idle_state
];
243 drv
->state_count
+= 1;
249 /* pseries_idle_devices_uninit(void)
250 * unregister cpuidle devices and de-allocate memory
252 static void pseries_idle_devices_uninit(void)
255 struct cpuidle_device
*dev
;
257 for_each_possible_cpu(i
) {
258 dev
= per_cpu_ptr(pseries_cpuidle_devices
, i
);
259 cpuidle_unregister_device(dev
);
262 free_percpu(pseries_cpuidle_devices
);
266 /* pseries_idle_devices_init()
267 * allocate, initialize and register cpuidle device
269 static int pseries_idle_devices_init(void)
272 struct cpuidle_driver
*drv
= &pseries_idle_driver
;
273 struct cpuidle_device
*dev
;
275 pseries_cpuidle_devices
= alloc_percpu(struct cpuidle_device
);
276 if (pseries_cpuidle_devices
== NULL
)
279 for_each_possible_cpu(i
) {
280 dev
= per_cpu_ptr(pseries_cpuidle_devices
, i
);
281 dev
->state_count
= drv
->state_count
;
283 if (cpuidle_register_device(dev
)) {
285 "cpuidle_register_device %d failed!\n", i
);
294 * pseries_idle_probe()
295 * Choose state table for shared versus dedicated partition
297 static int pseries_idle_probe(void)
300 if (!firmware_has_feature(FW_FEATURE_SPLPAR
))
303 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
306 if (max_idle_state
== 0) {
307 printk(KERN_DEBUG
"pseries processor idle disabled.\n");
311 if (get_lppaca()->shared_proc
)
312 cpuidle_state_table
= shared_states
;
314 cpuidle_state_table
= dedicated_states
;
319 static int __init
pseries_processor_idle_init(void)
323 retval
= pseries_idle_probe();
327 pseries_cpuidle_driver_init();
328 retval
= cpuidle_register_driver(&pseries_idle_driver
);
330 printk(KERN_DEBUG
"Registration of pseries driver failed.\n");
334 retval
= pseries_idle_devices_init();
336 pseries_idle_devices_uninit();
337 cpuidle_unregister_driver(&pseries_idle_driver
);
341 register_cpu_notifier(&setup_hotplug_notifier
);
342 printk(KERN_DEBUG
"pseries_idle_driver registered\n");
347 static void __exit
pseries_processor_idle_exit(void)
350 unregister_cpu_notifier(&setup_hotplug_notifier
);
351 pseries_idle_devices_uninit();
352 cpuidle_unregister_driver(&pseries_idle_driver
);
357 module_init(pseries_processor_idle_init
);
358 module_exit(pseries_processor_idle_exit
);
360 MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
361 MODULE_DESCRIPTION("Cpuidle driver for POWER");
362 MODULE_LICENSE("GPL");