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>
17 #include <asm/system.h>
18 #include <asm/machdep.h>
19 #include <asm/firmware.h>
21 #include "plpar_wrappers.h"
24 struct cpuidle_driver pseries_idle_driver = {
25 .name = "pseries_idle",
29 #define MAX_IDLE_STATE_COUNT 2
31 static int max_idle_state = MAX_IDLE_STATE_COUNT - 1;
32 static struct cpuidle_device __percpu *pseries_cpuidle_devices;
33 static struct cpuidle_state *cpuidle_state_table;
35 void update_smt_snooze_delay(int snooze)
37 struct cpuidle_driver *drv = cpuidle_get_driver();
39 drv->states[0].target_residency = snooze;
42 static inline void idle_loop_prolog(unsigned long *in_purr, ktime_t *kt_before)
45 *kt_before = ktime_get_real();
46 *in_purr = mfspr(SPRN_PURR);
48 * Indicate to the HV that we are idle. Now would be
49 * a good time to find other work to dispatch.
51 get_lppaca()->idle = 1;
54 static inline s64 idle_loop_epilog(unsigned long in_purr, ktime_t kt_before)
56 get_lppaca()->wait_state_cycles += mfspr(SPRN_PURR) - in_purr;
57 get_lppaca()->idle = 0;
59 return ktime_to_us(ktime_sub(ktime_get_real(), kt_before));
62 static int snooze_loop(struct cpuidle_device *dev,
63 struct cpuidle_driver *drv,
66 unsigned long in_purr;
68 unsigned long start_snooze;
69 long snooze = drv->states[0].target_residency;
71 idle_loop_prolog(&in_purr, &kt_before);
74 start_snooze = get_tb() + snooze * tb_ticks_per_usec;
76 set_thread_flag(TIF_POLLING_NRFLAG);
78 while ((snooze < 0) || (get_tb() < start_snooze)) {
79 if (need_resched() || cpu_is_offline(dev->cpu))
87 clear_thread_flag(TIF_POLLING_NRFLAG);
95 (int)idle_loop_epilog(in_purr, kt_before);
99 static void check_and_cede_processor(void)
102 * Interrupts are soft-disabled at this point,
103 * but not hard disabled. So an interrupt might have
104 * occurred before entering NAP, and would be potentially
105 * lost (edge events, decrementer events, etc...) unless
106 * we first hard disable then check.
109 if (get_paca()->irq_happened == 0)
113 static int dedicated_cede_loop(struct cpuidle_device *dev,
114 struct cpuidle_driver *drv,
117 unsigned long in_purr;
120 idle_loop_prolog(&in_purr, &kt_before);
121 get_lppaca()->donate_dedicated_cpu = 1;
123 ppc64_runlatch_off();
125 check_and_cede_processor();
127 get_lppaca()->donate_dedicated_cpu = 0;
128 dev->last_residency =
129 (int)idle_loop_epilog(in_purr, kt_before);
133 static int shared_cede_loop(struct cpuidle_device *dev,
134 struct cpuidle_driver *drv,
137 unsigned long in_purr;
140 idle_loop_prolog(&in_purr, &kt_before);
143 * Yield the processor to the hypervisor. We return if
144 * an external interrupt occurs (which are driven prior
145 * to returning here) or if a prod occurs from another
146 * processor. When returning here, external interrupts
149 check_and_cede_processor();
151 dev->last_residency =
152 (int)idle_loop_epilog(in_purr, kt_before);
157 * States for dedicated partition case.
159 static struct cpuidle_state dedicated_states[MAX_IDLE_STATE_COUNT] = {
163 .flags = CPUIDLE_FLAG_TIME_VALID,
165 .target_residency = 0,
166 .enter = &snooze_loop },
170 .flags = CPUIDLE_FLAG_TIME_VALID,
172 .target_residency = 10,
173 .enter = &dedicated_cede_loop },
177 * States for shared partition case.
179 static struct cpuidle_state shared_states[MAX_IDLE_STATE_COUNT] = {
181 .name = "Shared Cede",
182 .desc = "Shared Cede",
183 .flags = CPUIDLE_FLAG_TIME_VALID,
185 .target_residency = 0,
186 .enter = &shared_cede_loop },
189 int pseries_notify_cpuidle_add_cpu(int cpu)
191 struct cpuidle_device *dev =
192 per_cpu_ptr(pseries_cpuidle_devices, cpu);
193 if (dev && cpuidle_get_driver()) {
194 cpuidle_disable_device(dev);
195 cpuidle_enable_device(dev);
201 * pseries_cpuidle_driver_init()
203 static int pseries_cpuidle_driver_init(void)
206 struct cpuidle_driver *drv = &pseries_idle_driver;
208 drv->state_count = 0;
210 for (idle_state = 0; idle_state < MAX_IDLE_STATE_COUNT; ++idle_state) {
212 if (idle_state > max_idle_state)
215 /* is the state not enabled? */
216 if (cpuidle_state_table[idle_state].enter == NULL)
219 drv->states[drv->state_count] = /* structure copy */
220 cpuidle_state_table[idle_state];
222 if (cpuidle_state_table == dedicated_states)
223 drv->states[drv->state_count].target_residency =
224 __get_cpu_var(smt_snooze_delay);
226 drv->state_count += 1;
232 /* pseries_idle_devices_uninit(void)
233 * unregister cpuidle devices and de-allocate memory
235 static void pseries_idle_devices_uninit(void)
238 struct cpuidle_device *dev;
240 for_each_possible_cpu(i) {
241 dev = per_cpu_ptr(pseries_cpuidle_devices, i);
242 cpuidle_unregister_device(dev);
245 free_percpu(pseries_cpuidle_devices);
249 /* pseries_idle_devices_init()
250 * allocate, initialize and register cpuidle device
252 static int pseries_idle_devices_init(void)
255 struct cpuidle_driver *drv = &pseries_idle_driver;
256 struct cpuidle_device *dev;
258 pseries_cpuidle_devices = alloc_percpu(struct cpuidle_device);
259 if (pseries_cpuidle_devices == NULL)
262 for_each_possible_cpu(i) {
263 dev = per_cpu_ptr(pseries_cpuidle_devices, i);
264 dev->state_count = drv->state_count;
266 if (cpuidle_register_device(dev)) {
268 "cpuidle_register_device %d failed!\n", i);
277 * pseries_idle_probe()
278 * Choose state table for shared versus dedicated partition
280 static int pseries_idle_probe(void)
283 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
286 if (cpuidle_disable != IDLE_NO_OVERRIDE)
289 if (max_idle_state == 0) {
290 printk(KERN_DEBUG "pseries processor idle disabled.\n");
294 if (get_lppaca()->shared_proc)
295 cpuidle_state_table = shared_states;
297 cpuidle_state_table = dedicated_states;
302 static int __init pseries_processor_idle_init(void)
306 retval = pseries_idle_probe();
310 pseries_cpuidle_driver_init();
311 retval = cpuidle_register_driver(&pseries_idle_driver);
313 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
317 retval = pseries_idle_devices_init();
319 pseries_idle_devices_uninit();
320 cpuidle_unregister_driver(&pseries_idle_driver);
324 printk(KERN_DEBUG "pseries_idle_driver registered\n");
329 static void __exit pseries_processor_idle_exit(void)
332 pseries_idle_devices_uninit();
333 cpuidle_unregister_driver(&pseries_idle_driver);
338 module_init(pseries_processor_idle_init);
339 module_exit(pseries_processor_idle_exit);
341 MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
342 MODULE_DESCRIPTION("Cpuidle driver for POWER");
343 MODULE_LICENSE("GPL");