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>
23 #include <asm/plpar_wrappers.h>
25 struct cpuidle_driver pseries_idle_driver
= {
26 .name
= "pseries_idle",
30 static int max_idle_state __read_mostly
;
31 static struct cpuidle_state
*cpuidle_state_table __read_mostly
;
32 static u64 snooze_timeout __read_mostly
;
33 static bool snooze_timeout_en __read_mostly
;
35 static int snooze_loop(struct cpuidle_device
*dev
,
36 struct cpuidle_driver
*drv
,
41 set_thread_flag(TIF_POLLING_NRFLAG
);
43 pseries_idle_prolog();
45 snooze_exit_time
= get_tb() + snooze_timeout
;
47 while (!need_resched()) {
50 if (likely(snooze_timeout_en
) && get_tb() > snooze_exit_time
) {
52 * Task has not woken up but we are exiting the polling
53 * loop anyway. Require a barrier after polling is
54 * cleared to order subsequent test of need_resched().
56 clear_thread_flag(TIF_POLLING_NRFLAG
);
63 clear_thread_flag(TIF_POLLING_NRFLAG
);
67 pseries_idle_epilog();
72 static void check_and_cede_processor(void)
75 * Ensure our interrupt state is properly tracked,
76 * also checks if no interrupt has occurred while we
79 if (prep_irq_for_idle()) {
81 #ifdef CONFIG_TRACE_IRQFLAGS
82 /* Ensure that H_CEDE returns with IRQs on */
83 if (WARN_ON(!(mfmsr() & MSR_EE
)))
89 static int dedicated_cede_loop(struct cpuidle_device
*dev
,
90 struct cpuidle_driver
*drv
,
94 pseries_idle_prolog();
95 get_lppaca()->donate_dedicated_cpu
= 1;
98 check_and_cede_processor();
101 get_lppaca()->donate_dedicated_cpu
= 0;
103 pseries_idle_epilog();
108 static int shared_cede_loop(struct cpuidle_device
*dev
,
109 struct cpuidle_driver
*drv
,
113 pseries_idle_prolog();
116 * Yield the processor to the hypervisor. We return if
117 * an external interrupt occurs (which are driven prior
118 * to returning here) or if a prod occurs from another
119 * processor. When returning here, external interrupts
122 check_and_cede_processor();
125 pseries_idle_epilog();
131 * States for dedicated partition case.
133 static struct cpuidle_state dedicated_states
[] = {
138 .target_residency
= 0,
139 .enter
= &snooze_loop
},
144 .target_residency
= 100,
145 .enter
= &dedicated_cede_loop
},
149 * States for shared partition case.
151 static struct cpuidle_state shared_states
[] = {
156 .target_residency
= 0,
157 .enter
= &snooze_loop
},
159 .name
= "Shared Cede",
160 .desc
= "Shared Cede",
162 .target_residency
= 100,
163 .enter
= &shared_cede_loop
},
166 static int pseries_cpuidle_cpu_online(unsigned int cpu
)
168 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
170 if (dev
&& cpuidle_get_driver()) {
171 cpuidle_pause_and_lock();
172 cpuidle_enable_device(dev
);
173 cpuidle_resume_and_unlock();
178 static int pseries_cpuidle_cpu_dead(unsigned int cpu
)
180 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
182 if (dev
&& cpuidle_get_driver()) {
183 cpuidle_pause_and_lock();
184 cpuidle_disable_device(dev
);
185 cpuidle_resume_and_unlock();
191 * pseries_cpuidle_driver_init()
193 static int pseries_cpuidle_driver_init(void)
196 struct cpuidle_driver
*drv
= &pseries_idle_driver
;
198 drv
->state_count
= 0;
200 for (idle_state
= 0; idle_state
< max_idle_state
; ++idle_state
) {
201 /* Is the state not enabled? */
202 if (cpuidle_state_table
[idle_state
].enter
== NULL
)
205 drv
->states
[drv
->state_count
] = /* structure copy */
206 cpuidle_state_table
[idle_state
];
208 drv
->state_count
+= 1;
215 * pseries_idle_probe()
216 * Choose state table for shared versus dedicated partition
218 static int pseries_idle_probe(void)
221 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
224 if (firmware_has_feature(FW_FEATURE_SPLPAR
)) {
226 * Use local_paca instead of get_lppaca() since
227 * preemption is not disabled, and it is not required in
228 * fact, since lppaca_ptr does not need to be the value
229 * associated to the current CPU, it can be from any CPU.
231 if (lppaca_shared_proc(local_paca
->lppaca_ptr
)) {
232 cpuidle_state_table
= shared_states
;
233 max_idle_state
= ARRAY_SIZE(shared_states
);
235 cpuidle_state_table
= dedicated_states
;
236 max_idle_state
= ARRAY_SIZE(dedicated_states
);
241 if (max_idle_state
> 1) {
242 snooze_timeout_en
= true;
243 snooze_timeout
= cpuidle_state_table
[1].target_residency
*
249 static int __init
pseries_processor_idle_init(void)
253 retval
= pseries_idle_probe();
257 pseries_cpuidle_driver_init();
258 retval
= cpuidle_register(&pseries_idle_driver
, NULL
);
260 printk(KERN_DEBUG
"Registration of pseries driver failed.\n");
264 retval
= cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN
,
265 "cpuidle/pseries:online",
266 pseries_cpuidle_cpu_online
, NULL
);
268 retval
= cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD
,
269 "cpuidle/pseries:DEAD", NULL
,
270 pseries_cpuidle_cpu_dead
);
272 printk(KERN_DEBUG
"pseries_idle_driver registered\n");
276 device_initcall(pseries_processor_idle_init
);