1 // SPDX-License-Identifier: GPL-2.0
3 * cpuidle-pseries - idle state cpuidle driver.
4 * Adapted from drivers/idle/intel_idle.c and
5 * drivers/acpi/processor_idle.c
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/moduleparam.h>
13 #include <linux/cpuidle.h>
14 #include <linux/cpu.h>
15 #include <linux/notifier.h>
19 #include <asm/machdep.h>
20 #include <asm/firmware.h>
21 #include <asm/runlatch.h>
22 #include <asm/plpar_wrappers.h>
24 struct cpuidle_driver pseries_idle_driver
= {
25 .name
= "pseries_idle",
29 static int max_idle_state __read_mostly
;
30 static struct cpuidle_state
*cpuidle_state_table __read_mostly
;
31 static u64 snooze_timeout __read_mostly
;
32 static bool snooze_timeout_en __read_mostly
;
34 static inline void idle_loop_prolog(unsigned long *in_purr
)
37 *in_purr
= mfspr(SPRN_PURR
);
39 * Indicate to the HV that we are idle. Now would be
40 * a good time to find other work to dispatch.
42 get_lppaca()->idle
= 1;
45 static inline void idle_loop_epilog(unsigned long in_purr
)
49 wait_cycles
= be64_to_cpu(get_lppaca()->wait_state_cycles
);
50 wait_cycles
+= mfspr(SPRN_PURR
) - in_purr
;
51 get_lppaca()->wait_state_cycles
= cpu_to_be64(wait_cycles
);
52 get_lppaca()->idle
= 0;
57 static int snooze_loop(struct cpuidle_device
*dev
,
58 struct cpuidle_driver
*drv
,
61 unsigned long in_purr
;
64 set_thread_flag(TIF_POLLING_NRFLAG
);
66 idle_loop_prolog(&in_purr
);
68 snooze_exit_time
= get_tb() + snooze_timeout
;
70 while (!need_resched()) {
73 if (likely(snooze_timeout_en
) && get_tb() > snooze_exit_time
) {
75 * Task has not woken up but we are exiting the polling
76 * loop anyway. Require a barrier after polling is
77 * cleared to order subsequent test of need_resched().
79 clear_thread_flag(TIF_POLLING_NRFLAG
);
86 clear_thread_flag(TIF_POLLING_NRFLAG
);
90 idle_loop_epilog(in_purr
);
95 static void check_and_cede_processor(void)
98 * Ensure our interrupt state is properly tracked,
99 * also checks if no interrupt has occurred while we
102 if (prep_irq_for_idle()) {
104 #ifdef CONFIG_TRACE_IRQFLAGS
105 /* Ensure that H_CEDE returns with IRQs on */
106 if (WARN_ON(!(mfmsr() & MSR_EE
)))
112 static int dedicated_cede_loop(struct cpuidle_device
*dev
,
113 struct cpuidle_driver
*drv
,
116 unsigned long in_purr
;
118 idle_loop_prolog(&in_purr
);
119 get_lppaca()->donate_dedicated_cpu
= 1;
122 check_and_cede_processor();
125 get_lppaca()->donate_dedicated_cpu
= 0;
127 idle_loop_epilog(in_purr
);
132 static int shared_cede_loop(struct cpuidle_device
*dev
,
133 struct cpuidle_driver
*drv
,
136 unsigned long in_purr
;
138 idle_loop_prolog(&in_purr
);
141 * Yield the processor to the hypervisor. We return if
142 * an external interrupt occurs (which are driven prior
143 * to returning here) or if a prod occurs from another
144 * processor. When returning here, external interrupts
147 check_and_cede_processor();
150 idle_loop_epilog(in_purr
);
156 * States for dedicated partition case.
158 static struct cpuidle_state dedicated_states
[] = {
163 .target_residency
= 0,
164 .enter
= &snooze_loop
},
169 .target_residency
= 100,
170 .enter
= &dedicated_cede_loop
},
174 * States for shared partition case.
176 static struct cpuidle_state shared_states
[] = {
181 .target_residency
= 0,
182 .enter
= &snooze_loop
},
184 .name
= "Shared Cede",
185 .desc
= "Shared Cede",
187 .target_residency
= 100,
188 .enter
= &shared_cede_loop
},
191 static int pseries_cpuidle_cpu_online(unsigned int cpu
)
193 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
195 if (dev
&& cpuidle_get_driver()) {
196 cpuidle_pause_and_lock();
197 cpuidle_enable_device(dev
);
198 cpuidle_resume_and_unlock();
203 static int pseries_cpuidle_cpu_dead(unsigned int cpu
)
205 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
207 if (dev
&& cpuidle_get_driver()) {
208 cpuidle_pause_and_lock();
209 cpuidle_disable_device(dev
);
210 cpuidle_resume_and_unlock();
216 * pseries_cpuidle_driver_init()
218 static int pseries_cpuidle_driver_init(void)
221 struct cpuidle_driver
*drv
= &pseries_idle_driver
;
223 drv
->state_count
= 0;
225 for (idle_state
= 0; idle_state
< max_idle_state
; ++idle_state
) {
226 /* Is the state not enabled? */
227 if (cpuidle_state_table
[idle_state
].enter
== NULL
)
230 drv
->states
[drv
->state_count
] = /* structure copy */
231 cpuidle_state_table
[idle_state
];
233 drv
->state_count
+= 1;
240 * pseries_idle_probe()
241 * Choose state table for shared versus dedicated partition
243 static int pseries_idle_probe(void)
246 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
249 if (firmware_has_feature(FW_FEATURE_SPLPAR
)) {
251 * Use local_paca instead of get_lppaca() since
252 * preemption is not disabled, and it is not required in
253 * fact, since lppaca_ptr does not need to be the value
254 * associated to the current CPU, it can be from any CPU.
256 if (lppaca_shared_proc(local_paca
->lppaca_ptr
)) {
257 cpuidle_state_table
= shared_states
;
258 max_idle_state
= ARRAY_SIZE(shared_states
);
260 cpuidle_state_table
= dedicated_states
;
261 max_idle_state
= ARRAY_SIZE(dedicated_states
);
266 if (max_idle_state
> 1) {
267 snooze_timeout_en
= true;
268 snooze_timeout
= cpuidle_state_table
[1].target_residency
*
274 static int __init
pseries_processor_idle_init(void)
278 retval
= pseries_idle_probe();
282 pseries_cpuidle_driver_init();
283 retval
= cpuidle_register(&pseries_idle_driver
, NULL
);
285 printk(KERN_DEBUG
"Registration of pseries driver failed.\n");
289 retval
= cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN
,
290 "cpuidle/pseries:online",
291 pseries_cpuidle_cpu_online
, NULL
);
293 retval
= cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD
,
294 "cpuidle/pseries:DEAD", NULL
,
295 pseries_cpuidle_cpu_dead
);
297 printk(KERN_DEBUG
"pseries_idle_driver registered\n");
301 device_initcall(pseries_processor_idle_init
);