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 int dedicated_cede_loop(struct cpuidle_device
*dev
,
100 struct cpuidle_driver
*drv
,
103 unsigned long in_purr
;
106 idle_loop_prolog(&in_purr
, &kt_before
);
107 get_lppaca()->donate_dedicated_cpu
= 1;
109 ppc64_runlatch_off();
113 get_lppaca()->donate_dedicated_cpu
= 0;
114 dev
->last_residency
=
115 (int)idle_loop_epilog(in_purr
, kt_before
);
119 static int shared_cede_loop(struct cpuidle_device
*dev
,
120 struct cpuidle_driver
*drv
,
123 unsigned long in_purr
;
126 idle_loop_prolog(&in_purr
, &kt_before
);
129 * Yield the processor to the hypervisor. We return if
130 * an external interrupt occurs (which are driven prior
131 * to returning here) or if a prod occurs from another
132 * processor. When returning here, external interrupts
137 dev
->last_residency
=
138 (int)idle_loop_epilog(in_purr
, kt_before
);
143 * States for dedicated partition case.
145 static struct cpuidle_state dedicated_states
[MAX_IDLE_STATE_COUNT
] = {
149 .flags
= CPUIDLE_FLAG_TIME_VALID
,
151 .target_residency
= 0,
152 .enter
= &snooze_loop
},
156 .flags
= CPUIDLE_FLAG_TIME_VALID
,
158 .target_residency
= 10,
159 .enter
= &dedicated_cede_loop
},
163 * States for shared partition case.
165 static struct cpuidle_state shared_states
[MAX_IDLE_STATE_COUNT
] = {
167 .name
= "Shared Cede",
168 .desc
= "Shared Cede",
169 .flags
= CPUIDLE_FLAG_TIME_VALID
,
171 .target_residency
= 0,
172 .enter
= &shared_cede_loop
},
175 int pseries_notify_cpuidle_add_cpu(int cpu
)
177 struct cpuidle_device
*dev
=
178 per_cpu_ptr(pseries_cpuidle_devices
, cpu
);
179 if (dev
&& cpuidle_get_driver()) {
180 cpuidle_disable_device(dev
);
181 cpuidle_enable_device(dev
);
187 * pseries_cpuidle_driver_init()
189 static int pseries_cpuidle_driver_init(void)
192 struct cpuidle_driver
*drv
= &pseries_idle_driver
;
194 drv
->state_count
= 0;
196 for (idle_state
= 0; idle_state
< MAX_IDLE_STATE_COUNT
; ++idle_state
) {
198 if (idle_state
> max_idle_state
)
201 /* is the state not enabled? */
202 if (cpuidle_state_table
[idle_state
].enter
== NULL
)
205 drv
->states
[drv
->state_count
] = /* structure copy */
206 cpuidle_state_table
[idle_state
];
208 if (cpuidle_state_table
== dedicated_states
)
209 drv
->states
[drv
->state_count
].target_residency
=
210 __get_cpu_var(smt_snooze_delay
);
212 drv
->state_count
+= 1;
218 /* pseries_idle_devices_uninit(void)
219 * unregister cpuidle devices and de-allocate memory
221 static void pseries_idle_devices_uninit(void)
224 struct cpuidle_device
*dev
;
226 for_each_possible_cpu(i
) {
227 dev
= per_cpu_ptr(pseries_cpuidle_devices
, i
);
228 cpuidle_unregister_device(dev
);
231 free_percpu(pseries_cpuidle_devices
);
235 /* pseries_idle_devices_init()
236 * allocate, initialize and register cpuidle device
238 static int pseries_idle_devices_init(void)
241 struct cpuidle_driver
*drv
= &pseries_idle_driver
;
242 struct cpuidle_device
*dev
;
244 pseries_cpuidle_devices
= alloc_percpu(struct cpuidle_device
);
245 if (pseries_cpuidle_devices
== NULL
)
248 for_each_possible_cpu(i
) {
249 dev
= per_cpu_ptr(pseries_cpuidle_devices
, i
);
250 dev
->state_count
= drv
->state_count
;
252 if (cpuidle_register_device(dev
)) {
254 "cpuidle_register_device %d failed!\n", i
);
263 * pseries_idle_probe()
264 * Choose state table for shared versus dedicated partition
266 static int pseries_idle_probe(void)
269 if (!firmware_has_feature(FW_FEATURE_SPLPAR
))
272 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
275 if (max_idle_state
== 0) {
276 printk(KERN_DEBUG
"pseries processor idle disabled.\n");
280 if (get_lppaca()->shared_proc
)
281 cpuidle_state_table
= shared_states
;
283 cpuidle_state_table
= dedicated_states
;
288 static int __init
pseries_processor_idle_init(void)
292 retval
= pseries_idle_probe();
296 pseries_cpuidle_driver_init();
297 retval
= cpuidle_register_driver(&pseries_idle_driver
);
299 printk(KERN_DEBUG
"Registration of pseries driver failed.\n");
303 retval
= pseries_idle_devices_init();
305 pseries_idle_devices_uninit();
306 cpuidle_unregister_driver(&pseries_idle_driver
);
310 printk(KERN_DEBUG
"pseries_idle_driver registered\n");
315 static void __exit
pseries_processor_idle_exit(void)
318 pseries_idle_devices_uninit();
319 cpuidle_unregister_driver(&pseries_idle_driver
);
324 module_init(pseries_processor_idle_init
);
325 module_exit(pseries_processor_idle_exit
);
327 MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
328 MODULE_DESCRIPTION("Cpuidle driver for POWER");
329 MODULE_LICENSE("GPL");