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_state
*cpuidle_state_table
;
33 static inline void idle_loop_prolog(unsigned long *in_purr
)
35 *in_purr
= mfspr(SPRN_PURR
);
37 * Indicate to the HV that we are idle. Now would be
38 * a good time to find other work to dispatch.
40 get_lppaca()->idle
= 1;
43 static inline void idle_loop_epilog(unsigned long in_purr
)
47 wait_cycles
= be64_to_cpu(get_lppaca()->wait_state_cycles
);
48 wait_cycles
+= mfspr(SPRN_PURR
) - in_purr
;
49 get_lppaca()->wait_state_cycles
= cpu_to_be64(wait_cycles
);
50 get_lppaca()->idle
= 0;
53 static int snooze_loop(struct cpuidle_device
*dev
,
54 struct cpuidle_driver
*drv
,
57 unsigned long in_purr
;
60 idle_loop_prolog(&in_purr
);
62 set_thread_flag(TIF_POLLING_NRFLAG
);
64 while ((!need_resched()) && cpu_online(cpu
)) {
71 clear_thread_flag(TIF_POLLING_NRFLAG
);
74 idle_loop_epilog(in_purr
);
79 static void check_and_cede_processor(void)
82 * Ensure our interrupt state is properly tracked,
83 * also checks if no interrupt has occurred while we
86 if (prep_irq_for_idle()) {
88 #ifdef CONFIG_TRACE_IRQFLAGS
89 /* Ensure that H_CEDE returns with IRQs on */
90 if (WARN_ON(!(mfmsr() & MSR_EE
)))
96 static int dedicated_cede_loop(struct cpuidle_device
*dev
,
97 struct cpuidle_driver
*drv
,
100 unsigned long in_purr
;
102 idle_loop_prolog(&in_purr
);
103 get_lppaca()->donate_dedicated_cpu
= 1;
105 ppc64_runlatch_off();
107 check_and_cede_processor();
109 get_lppaca()->donate_dedicated_cpu
= 0;
111 idle_loop_epilog(in_purr
);
116 static int shared_cede_loop(struct cpuidle_device
*dev
,
117 struct cpuidle_driver
*drv
,
120 unsigned long in_purr
;
122 idle_loop_prolog(&in_purr
);
125 * Yield the processor to the hypervisor. We return if
126 * an external interrupt occurs (which are driven prior
127 * to returning here) or if a prod occurs from another
128 * processor. When returning here, external interrupts
131 check_and_cede_processor();
133 idle_loop_epilog(in_purr
);
139 * States for dedicated partition case.
141 static struct cpuidle_state dedicated_states
[MAX_IDLE_STATE_COUNT
] = {
145 .flags
= CPUIDLE_FLAG_TIME_VALID
,
147 .target_residency
= 0,
148 .enter
= &snooze_loop
},
152 .flags
= CPUIDLE_FLAG_TIME_VALID
,
154 .target_residency
= 100,
155 .enter
= &dedicated_cede_loop
},
159 * States for shared partition case.
161 static struct cpuidle_state shared_states
[MAX_IDLE_STATE_COUNT
] = {
163 .name
= "Shared Cede",
164 .desc
= "Shared Cede",
165 .flags
= CPUIDLE_FLAG_TIME_VALID
,
167 .target_residency
= 0,
168 .enter
= &shared_cede_loop
},
171 void update_smt_snooze_delay(int cpu
, int residency
)
173 struct cpuidle_driver
*drv
= cpuidle_get_driver();
174 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
176 if (cpuidle_state_table
!= dedicated_states
)
180 /* Disable the Nap state on that cpu */
182 dev
->states_usage
[1].disable
= 1;
185 drv
->states
[1].target_residency
= residency
;
188 static int pseries_cpuidle_add_cpu_notifier(struct notifier_block
*n
,
189 unsigned long action
, void *hcpu
)
191 int hotcpu
= (unsigned long)hcpu
;
192 struct cpuidle_device
*dev
=
193 per_cpu_ptr(cpuidle_devices
, hotcpu
);
195 if (dev
&& cpuidle_get_driver()) {
198 case CPU_ONLINE_FROZEN
:
199 cpuidle_pause_and_lock();
200 cpuidle_enable_device(dev
);
201 cpuidle_resume_and_unlock();
205 case CPU_DEAD_FROZEN
:
206 cpuidle_pause_and_lock();
207 cpuidle_disable_device(dev
);
208 cpuidle_resume_and_unlock();
218 static struct notifier_block setup_hotplug_notifier
= {
219 .notifier_call
= pseries_cpuidle_add_cpu_notifier
,
223 * pseries_cpuidle_driver_init()
225 static int pseries_cpuidle_driver_init(void)
228 struct cpuidle_driver
*drv
= &pseries_idle_driver
;
230 drv
->state_count
= 0;
232 for (idle_state
= 0; idle_state
< MAX_IDLE_STATE_COUNT
; ++idle_state
) {
234 if (idle_state
> max_idle_state
)
237 /* is the state not enabled? */
238 if (cpuidle_state_table
[idle_state
].enter
== NULL
)
241 drv
->states
[drv
->state_count
] = /* structure copy */
242 cpuidle_state_table
[idle_state
];
244 drv
->state_count
+= 1;
251 * pseries_idle_probe()
252 * Choose state table for shared versus dedicated partition
254 static int pseries_idle_probe(void)
257 if (!firmware_has_feature(FW_FEATURE_SPLPAR
))
260 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
263 if (max_idle_state
== 0) {
264 printk(KERN_DEBUG
"pseries processor idle disabled.\n");
268 if (lppaca_shared_proc(get_lppaca()))
269 cpuidle_state_table
= shared_states
;
271 cpuidle_state_table
= dedicated_states
;
276 static int __init
pseries_processor_idle_init(void)
280 retval
= pseries_idle_probe();
284 pseries_cpuidle_driver_init();
285 retval
= cpuidle_register(&pseries_idle_driver
, NULL
);
287 printk(KERN_DEBUG
"Registration of pseries driver failed.\n");
291 register_cpu_notifier(&setup_hotplug_notifier
);
292 printk(KERN_DEBUG
"pseries_idle_driver registered\n");
297 static void __exit
pseries_processor_idle_exit(void)
300 unregister_cpu_notifier(&setup_hotplug_notifier
);
301 cpuidle_unregister(&pseries_idle_driver
);
306 module_init(pseries_processor_idle_init
);
307 module_exit(pseries_processor_idle_exit
);
309 MODULE_AUTHOR("Deepthi Dharwar <deepthi@linux.vnet.ibm.com>");
310 MODULE_DESCRIPTION("Cpuidle driver for POWER");
311 MODULE_LICENSE("GPL");