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 __read_mostly;
29 static struct cpuidle_state *cpuidle_state_table __read_mostly;
30 static u64 snooze_timeout __read_mostly;
31 static bool snooze_timeout_en __read_mostly;
33 static inline void idle_loop_prolog(unsigned long *in_purr)
36 *in_purr = mfspr(SPRN_PURR);
38 * Indicate to the HV that we are idle. Now would be
39 * a good time to find other work to dispatch.
41 get_lppaca()->idle = 1;
44 static inline void idle_loop_epilog(unsigned long in_purr)
48 wait_cycles = be64_to_cpu(get_lppaca()->wait_state_cycles);
49 wait_cycles += mfspr(SPRN_PURR) - in_purr;
50 get_lppaca()->wait_state_cycles = cpu_to_be64(wait_cycles);
51 get_lppaca()->idle = 0;
58 static int snooze_loop(struct cpuidle_device *dev,
59 struct cpuidle_driver *drv,
62 unsigned long in_purr;
65 set_thread_flag(TIF_POLLING_NRFLAG);
67 idle_loop_prolog(&in_purr);
69 snooze_exit_time = get_tb() + snooze_timeout;
71 while (!need_resched()) {
74 if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) {
76 * Task has not woken up but we are exiting the polling
77 * loop anyway. Require a barrier after polling is
78 * cleared to order subsequent test of need_resched().
80 clear_thread_flag(TIF_POLLING_NRFLAG);
87 clear_thread_flag(TIF_POLLING_NRFLAG);
89 idle_loop_epilog(in_purr);
94 static void check_and_cede_processor(void)
97 * Ensure our interrupt state is properly tracked,
98 * also checks if no interrupt has occurred while we
101 if (prep_irq_for_idle()) {
103 #ifdef CONFIG_TRACE_IRQFLAGS
104 /* Ensure that H_CEDE returns with IRQs on */
105 if (WARN_ON(!(mfmsr() & MSR_EE)))
111 static int dedicated_cede_loop(struct cpuidle_device *dev,
112 struct cpuidle_driver *drv,
115 unsigned long in_purr;
117 idle_loop_prolog(&in_purr);
118 get_lppaca()->donate_dedicated_cpu = 1;
121 check_and_cede_processor();
123 get_lppaca()->donate_dedicated_cpu = 0;
125 idle_loop_epilog(in_purr);
130 static int shared_cede_loop(struct cpuidle_device *dev,
131 struct cpuidle_driver *drv,
134 unsigned long in_purr;
136 idle_loop_prolog(&in_purr);
139 * Yield the processor to the hypervisor. We return if
140 * an external interrupt occurs (which are driven prior
141 * to returning here) or if a prod occurs from another
142 * processor. When returning here, external interrupts
145 check_and_cede_processor();
147 idle_loop_epilog(in_purr);
153 * States for dedicated partition case.
155 static struct cpuidle_state dedicated_states[] = {
160 .target_residency = 0,
161 .enter = &snooze_loop },
166 .target_residency = 100,
167 .enter = &dedicated_cede_loop },
171 * States for shared partition case.
173 static struct cpuidle_state shared_states[] = {
175 .name = "Shared Cede",
176 .desc = "Shared Cede",
178 .target_residency = 0,
179 .enter = &shared_cede_loop },
182 static int pseries_cpuidle_cpu_online(unsigned int cpu)
184 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
186 if (dev && cpuidle_get_driver()) {
187 cpuidle_pause_and_lock();
188 cpuidle_enable_device(dev);
189 cpuidle_resume_and_unlock();
194 static int pseries_cpuidle_cpu_dead(unsigned int cpu)
196 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
198 if (dev && cpuidle_get_driver()) {
199 cpuidle_pause_and_lock();
200 cpuidle_disable_device(dev);
201 cpuidle_resume_and_unlock();
207 * pseries_cpuidle_driver_init()
209 static int pseries_cpuidle_driver_init(void)
212 struct cpuidle_driver *drv = &pseries_idle_driver;
214 drv->state_count = 0;
216 for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
217 /* Is the state not enabled? */
218 if (cpuidle_state_table[idle_state].enter == NULL)
221 drv->states[drv->state_count] = /* structure copy */
222 cpuidle_state_table[idle_state];
224 drv->state_count += 1;
231 * pseries_idle_probe()
232 * Choose state table for shared versus dedicated partition
234 static int pseries_idle_probe(void)
237 if (cpuidle_disable != IDLE_NO_OVERRIDE)
240 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
241 if (lppaca_shared_proc(get_lppaca())) {
242 cpuidle_state_table = shared_states;
243 max_idle_state = ARRAY_SIZE(shared_states);
245 cpuidle_state_table = dedicated_states;
246 max_idle_state = ARRAY_SIZE(dedicated_states);
251 if (max_idle_state > 1) {
252 snooze_timeout_en = true;
253 snooze_timeout = cpuidle_state_table[1].target_residency *
259 static int __init pseries_processor_idle_init(void)
263 retval = pseries_idle_probe();
267 pseries_cpuidle_driver_init();
268 retval = cpuidle_register(&pseries_idle_driver, NULL);
270 printk(KERN_DEBUG "Registration of pseries driver failed.\n");
274 retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
275 "cpuidle/pseries:online",
276 pseries_cpuidle_cpu_online, NULL);
278 retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
279 "cpuidle/pseries:DEAD", NULL,
280 pseries_cpuidle_cpu_dead);
282 printk(KERN_DEBUG "pseries_idle_driver registered\n");
286 device_initcall(pseries_processor_idle_init);