2 * cpuidle-pseries - 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 static int max_idle_state
;
29 static struct cpuidle_state
*cpuidle_state_table
;
31 static inline void idle_loop_prolog(unsigned long *in_purr
)
34 *in_purr
= mfspr(SPRN_PURR
);
36 * Indicate to the HV that we are idle. Now would be
37 * a good time to find other work to dispatch.
39 get_lppaca()->idle
= 1;
42 static inline void idle_loop_epilog(unsigned long in_purr
)
46 wait_cycles
= be64_to_cpu(get_lppaca()->wait_state_cycles
);
47 wait_cycles
+= mfspr(SPRN_PURR
) - in_purr
;
48 get_lppaca()->wait_state_cycles
= cpu_to_be64(wait_cycles
);
49 get_lppaca()->idle
= 0;
56 static int snooze_loop(struct cpuidle_device
*dev
,
57 struct cpuidle_driver
*drv
,
60 unsigned long in_purr
;
62 idle_loop_prolog(&in_purr
);
64 set_thread_flag(TIF_POLLING_NRFLAG
);
66 while (!need_resched()) {
72 clear_thread_flag(TIF_POLLING_NRFLAG
);
75 idle_loop_epilog(in_purr
);
80 static void check_and_cede_processor(void)
83 * Ensure our interrupt state is properly tracked,
84 * also checks if no interrupt has occurred while we
87 if (prep_irq_for_idle()) {
89 #ifdef CONFIG_TRACE_IRQFLAGS
90 /* Ensure that H_CEDE returns with IRQs on */
91 if (WARN_ON(!(mfmsr() & MSR_EE
)))
97 static int dedicated_cede_loop(struct cpuidle_device
*dev
,
98 struct cpuidle_driver
*drv
,
101 unsigned long in_purr
;
103 idle_loop_prolog(&in_purr
);
104 get_lppaca()->donate_dedicated_cpu
= 1;
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
[] = {
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
[] = {
163 .name
= "Shared Cede",
164 .desc
= "Shared Cede",
165 .flags
= CPUIDLE_FLAG_TIME_VALID
,
167 .target_residency
= 0,
168 .enter
= &shared_cede_loop
},
171 static int pseries_cpuidle_add_cpu_notifier(struct notifier_block
*n
,
172 unsigned long action
, void *hcpu
)
174 int hotcpu
= (unsigned long)hcpu
;
175 struct cpuidle_device
*dev
=
176 per_cpu(cpuidle_devices
, hotcpu
);
178 if (dev
&& cpuidle_get_driver()) {
181 case CPU_ONLINE_FROZEN
:
182 cpuidle_pause_and_lock();
183 cpuidle_enable_device(dev
);
184 cpuidle_resume_and_unlock();
188 case CPU_DEAD_FROZEN
:
189 cpuidle_pause_and_lock();
190 cpuidle_disable_device(dev
);
191 cpuidle_resume_and_unlock();
201 static struct notifier_block setup_hotplug_notifier
= {
202 .notifier_call
= pseries_cpuidle_add_cpu_notifier
,
206 * pseries_cpuidle_driver_init()
208 static int pseries_cpuidle_driver_init(void)
211 struct cpuidle_driver
*drv
= &pseries_idle_driver
;
213 drv
->state_count
= 0;
215 for (idle_state
= 0; idle_state
< max_idle_state
; ++idle_state
) {
216 /* Is the state not enabled? */
217 if (cpuidle_state_table
[idle_state
].enter
== NULL
)
220 drv
->states
[drv
->state_count
] = /* structure copy */
221 cpuidle_state_table
[idle_state
];
223 drv
->state_count
+= 1;
230 * pseries_idle_probe()
231 * Choose state table for shared versus dedicated partition
233 static int pseries_idle_probe(void)
236 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
239 if (firmware_has_feature(FW_FEATURE_SPLPAR
)) {
240 if (lppaca_shared_proc(get_lppaca())) {
241 cpuidle_state_table
= shared_states
;
242 max_idle_state
= ARRAY_SIZE(shared_states
);
244 cpuidle_state_table
= dedicated_states
;
245 max_idle_state
= ARRAY_SIZE(dedicated_states
);
253 static int __init
pseries_processor_idle_init(void)
257 retval
= pseries_idle_probe();
261 pseries_cpuidle_driver_init();
262 retval
= cpuidle_register(&pseries_idle_driver
, NULL
);
264 printk(KERN_DEBUG
"Registration of pseries driver failed.\n");
268 register_cpu_notifier(&setup_hotplug_notifier
);
269 printk(KERN_DEBUG
"pseries_idle_driver registered\n");
273 device_initcall(pseries_processor_idle_init
);