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/machdep.h>
18 #include <asm/firmware.h>
19 #include <asm/runlatch.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 * Ensure our interrupt state is properly tracked,
103 * also checks if no interrupt has occurred while we
106 if (prep_irq_for_idle()) {
108 #ifdef CONFIG_TRACE_IRQFLAGS
109 /* Ensure that H_CEDE returns with IRQs on */
110 if (WARN_ON(!(mfmsr() & MSR_EE
)))
116 static int dedicated_cede_loop(struct cpuidle_device
*dev
,
117 struct cpuidle_driver
*drv
,
120 unsigned long in_purr
;
123 idle_loop_prolog(&in_purr
, &kt_before
);
124 get_lppaca()->donate_dedicated_cpu
= 1;
126 ppc64_runlatch_off();
128 check_and_cede_processor();
130 get_lppaca()->donate_dedicated_cpu
= 0;
131 dev
->last_residency
=
132 (int)idle_loop_epilog(in_purr
, kt_before
);
136 static int shared_cede_loop(struct cpuidle_device
*dev
,
137 struct cpuidle_driver
*drv
,
140 unsigned long in_purr
;
143 idle_loop_prolog(&in_purr
, &kt_before
);
146 * Yield the processor to the hypervisor. We return if
147 * an external interrupt occurs (which are driven prior
148 * to returning here) or if a prod occurs from another
149 * processor. When returning here, external interrupts
152 check_and_cede_processor();
154 dev
->last_residency
=
155 (int)idle_loop_epilog(in_purr
, kt_before
);
160 * States for dedicated partition case.
162 static struct cpuidle_state dedicated_states
[MAX_IDLE_STATE_COUNT
] = {
166 .flags
= CPUIDLE_FLAG_TIME_VALID
,
168 .target_residency
= 0,
169 .enter
= &snooze_loop
},
173 .flags
= CPUIDLE_FLAG_TIME_VALID
,
175 .target_residency
= 10,
176 .enter
= &dedicated_cede_loop
},
180 * States for shared partition case.
182 static struct cpuidle_state shared_states
[MAX_IDLE_STATE_COUNT
] = {
184 .name
= "Shared Cede",
185 .desc
= "Shared Cede",
186 .flags
= CPUIDLE_FLAG_TIME_VALID
,
188 .target_residency
= 0,
189 .enter
= &shared_cede_loop
},
192 int pseries_notify_cpuidle_add_cpu(int cpu
)
194 struct cpuidle_device
*dev
=
195 per_cpu_ptr(pseries_cpuidle_devices
, cpu
);
196 if (dev
&& cpuidle_get_driver()) {
197 cpuidle_disable_device(dev
);
198 cpuidle_enable_device(dev
);
204 * pseries_cpuidle_driver_init()
206 static int pseries_cpuidle_driver_init(void)
209 struct cpuidle_driver
*drv
= &pseries_idle_driver
;
211 drv
->state_count
= 0;
213 for (idle_state
= 0; idle_state
< MAX_IDLE_STATE_COUNT
; ++idle_state
) {
215 if (idle_state
> max_idle_state
)
218 /* is the state not enabled? */
219 if (cpuidle_state_table
[idle_state
].enter
== NULL
)
222 drv
->states
[drv
->state_count
] = /* structure copy */
223 cpuidle_state_table
[idle_state
];
225 if (cpuidle_state_table
== dedicated_states
)
226 drv
->states
[drv
->state_count
].target_residency
=
227 __get_cpu_var(smt_snooze_delay
);
229 drv
->state_count
+= 1;
235 /* pseries_idle_devices_uninit(void)
236 * unregister cpuidle devices and de-allocate memory
238 static void pseries_idle_devices_uninit(void)
241 struct cpuidle_device
*dev
;
243 for_each_possible_cpu(i
) {
244 dev
= per_cpu_ptr(pseries_cpuidle_devices
, i
);
245 cpuidle_unregister_device(dev
);
248 free_percpu(pseries_cpuidle_devices
);
252 /* pseries_idle_devices_init()
253 * allocate, initialize and register cpuidle device
255 static int pseries_idle_devices_init(void)
258 struct cpuidle_driver
*drv
= &pseries_idle_driver
;
259 struct cpuidle_device
*dev
;
261 pseries_cpuidle_devices
= alloc_percpu(struct cpuidle_device
);
262 if (pseries_cpuidle_devices
== NULL
)
265 for_each_possible_cpu(i
) {
266 dev
= per_cpu_ptr(pseries_cpuidle_devices
, i
);
267 dev
->state_count
= drv
->state_count
;
269 if (cpuidle_register_device(dev
)) {
271 "cpuidle_register_device %d failed!\n", i
);
280 * pseries_idle_probe()
281 * Choose state table for shared versus dedicated partition
283 static int pseries_idle_probe(void)
286 if (!firmware_has_feature(FW_FEATURE_SPLPAR
))
289 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
292 if (max_idle_state
== 0) {
293 printk(KERN_DEBUG
"pseries processor idle disabled.\n");
297 if (get_lppaca()->shared_proc
)
298 cpuidle_state_table
= shared_states
;
300 cpuidle_state_table
= dedicated_states
;
305 static int __init
pseries_processor_idle_init(void)
309 retval
= pseries_idle_probe();
313 pseries_cpuidle_driver_init();
314 retval
= cpuidle_register_driver(&pseries_idle_driver
);
316 printk(KERN_DEBUG
"Registration of pseries driver failed.\n");
320 retval
= pseries_idle_devices_init();
322 pseries_idle_devices_uninit();
323 cpuidle_unregister_driver(&pseries_idle_driver
);
327 printk(KERN_DEBUG
"pseries_idle_driver registered\n");
332 static void __exit
pseries_processor_idle_exit(void)
335 pseries_idle_devices_uninit();
336 cpuidle_unregister_driver(&pseries_idle_driver
);
341 module_init(pseries_processor_idle_init
);
342 module_exit(pseries_processor_idle_exit
);
344 MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
345 MODULE_DESCRIPTION("Cpuidle driver for POWER");
346 MODULE_LICENSE("GPL");