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
;
30 static u64 snooze_timeout
;
31 static bool snooze_timeout_en
;
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 idle_loop_prolog(&in_purr
);
67 set_thread_flag(TIF_POLLING_NRFLAG
);
68 snooze_exit_time
= get_tb() + snooze_timeout
;
70 while (!need_resched()) {
73 if (snooze_timeout_en
&& get_tb() > snooze_exit_time
)
78 clear_thread_flag(TIF_POLLING_NRFLAG
);
81 idle_loop_epilog(in_purr
);
86 static void check_and_cede_processor(void)
89 * Ensure our interrupt state is properly tracked,
90 * also checks if no interrupt has occurred while we
93 if (prep_irq_for_idle()) {
95 #ifdef CONFIG_TRACE_IRQFLAGS
96 /* Ensure that H_CEDE returns with IRQs on */
97 if (WARN_ON(!(mfmsr() & MSR_EE
)))
103 static int dedicated_cede_loop(struct cpuidle_device
*dev
,
104 struct cpuidle_driver
*drv
,
107 unsigned long in_purr
;
109 idle_loop_prolog(&in_purr
);
110 get_lppaca()->donate_dedicated_cpu
= 1;
113 check_and_cede_processor();
115 get_lppaca()->donate_dedicated_cpu
= 0;
117 idle_loop_epilog(in_purr
);
122 static int shared_cede_loop(struct cpuidle_device
*dev
,
123 struct cpuidle_driver
*drv
,
126 unsigned long in_purr
;
128 idle_loop_prolog(&in_purr
);
131 * Yield the processor to the hypervisor. We return if
132 * an external interrupt occurs (which are driven prior
133 * to returning here) or if a prod occurs from another
134 * processor. When returning here, external interrupts
137 check_and_cede_processor();
139 idle_loop_epilog(in_purr
);
145 * States for dedicated partition case.
147 static struct cpuidle_state dedicated_states
[] = {
152 .target_residency
= 0,
153 .enter
= &snooze_loop
},
158 .target_residency
= 100,
159 .enter
= &dedicated_cede_loop
},
163 * States for shared partition case.
165 static struct cpuidle_state shared_states
[] = {
167 .name
= "Shared Cede",
168 .desc
= "Shared Cede",
170 .target_residency
= 0,
171 .enter
= &shared_cede_loop
},
174 static int pseries_cpuidle_cpu_online(unsigned int cpu
)
176 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
178 if (dev
&& cpuidle_get_driver()) {
179 cpuidle_pause_and_lock();
180 cpuidle_enable_device(dev
);
181 cpuidle_resume_and_unlock();
186 static int pseries_cpuidle_cpu_dead(unsigned int cpu
)
188 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
190 if (dev
&& cpuidle_get_driver()) {
191 cpuidle_pause_and_lock();
192 cpuidle_disable_device(dev
);
193 cpuidle_resume_and_unlock();
199 * pseries_cpuidle_driver_init()
201 static int pseries_cpuidle_driver_init(void)
204 struct cpuidle_driver
*drv
= &pseries_idle_driver
;
206 drv
->state_count
= 0;
208 for (idle_state
= 0; idle_state
< max_idle_state
; ++idle_state
) {
209 /* Is the state not enabled? */
210 if (cpuidle_state_table
[idle_state
].enter
== NULL
)
213 drv
->states
[drv
->state_count
] = /* structure copy */
214 cpuidle_state_table
[idle_state
];
216 drv
->state_count
+= 1;
223 * pseries_idle_probe()
224 * Choose state table for shared versus dedicated partition
226 static int pseries_idle_probe(void)
229 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
232 if (firmware_has_feature(FW_FEATURE_SPLPAR
)) {
233 if (lppaca_shared_proc(get_lppaca())) {
234 cpuidle_state_table
= shared_states
;
235 max_idle_state
= ARRAY_SIZE(shared_states
);
237 cpuidle_state_table
= dedicated_states
;
238 max_idle_state
= ARRAY_SIZE(dedicated_states
);
243 if (max_idle_state
> 1) {
244 snooze_timeout_en
= true;
245 snooze_timeout
= cpuidle_state_table
[1].target_residency
*
251 static int __init
pseries_processor_idle_init(void)
255 retval
= pseries_idle_probe();
259 pseries_cpuidle_driver_init();
260 retval
= cpuidle_register(&pseries_idle_driver
, NULL
);
262 printk(KERN_DEBUG
"Registration of pseries driver failed.\n");
266 retval
= cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN
,
267 "cpuidle/pseries:online",
268 pseries_cpuidle_cpu_online
, NULL
);
270 retval
= cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD
,
271 "cpuidle/pseries:DEAD", NULL
,
272 pseries_cpuidle_cpu_dead
);
274 printk(KERN_DEBUG
"pseries_idle_driver registered\n");
278 device_initcall(pseries_processor_idle_init
);