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>
27 static struct cpuidle_driver pseries_idle_driver
= {
28 .name
= "pseries_idle",
32 static int max_idle_state __read_mostly
;
33 static struct cpuidle_state
*cpuidle_state_table __read_mostly
;
34 static u64 snooze_timeout __read_mostly
;
35 static bool snooze_timeout_en __read_mostly
;
38 int snooze_loop(struct cpuidle_device
*dev
, struct cpuidle_driver
*drv
,
43 set_thread_flag(TIF_POLLING_NRFLAG
);
45 pseries_idle_prolog();
46 raw_local_irq_enable();
47 snooze_exit_time
= get_tb() + snooze_timeout
;
48 dev
->poll_time_limit
= false;
50 while (!need_resched()) {
53 if (likely(snooze_timeout_en
) && get_tb() > snooze_exit_time
) {
55 * Task has not woken up but we are exiting the polling
56 * loop anyway. Require a barrier after polling is
57 * cleared to order subsequent test of need_resched().
59 dev
->poll_time_limit
= true;
60 clear_thread_flag(TIF_POLLING_NRFLAG
);
67 clear_thread_flag(TIF_POLLING_NRFLAG
);
69 raw_local_irq_disable();
71 pseries_idle_epilog();
76 static __cpuidle
void check_and_cede_processor(void)
79 * Ensure our interrupt state is properly tracked,
80 * also checks if no interrupt has occurred while we
83 if (prep_irq_for_idle()) {
85 #ifdef CONFIG_TRACE_IRQFLAGS
86 /* Ensure that H_CEDE returns with IRQs on */
87 if (WARN_ON(!(mfmsr() & MSR_EE
)))
94 * XCEDE: Extended CEDE states discovered through the
95 * "ibm,get-systems-parameter" RTAS call with the token
100 * Section 7.3.16 System Parameters Option of PAPR version 2.8.1 has a
101 * table with all the parameters to ibm,get-system-parameters.
102 * CEDE_LATENCY_TOKEN corresponds to the token value for Cede Latency
103 * Settings Information.
105 #define CEDE_LATENCY_TOKEN 45
108 * If the platform supports the cede latency settings information system
109 * parameter it must provide the following information in the NULL terminated
112 * a. The first byte is the length āNā of each cede latency setting record minus
113 * one (zero indicates a length of 1 byte).
115 * b. For each supported cede latency setting a cede latency setting record
116 * consisting of the first āNā bytes as per the following table.
118 * -----------------------------
121 * -----------------------------
122 * | Cede Latency | 1 Byte |
123 * | Specifier Value | |
124 * -----------------------------
125 * | Maximum wakeup | |
126 * | latency in | 8 Bytes |
128 * -----------------------------
129 * | Responsive to | |
130 * | external | 1 Byte |
132 * -----------------------------
134 * This version has cede latency record size = 10.
136 * The structure xcede_latency_payload represents a) and b) with
137 * xcede_latency_record representing the table in b).
139 * xcede_latency_parameter is what gets returned by
140 * ibm,get-systems-parameter RTAS call when made with
141 * CEDE_LATENCY_TOKEN.
143 * These structures are only used to represent the data obtained by the RTAS
144 * call. The data is in big-endian.
146 struct xcede_latency_record
{
148 __be64 latency_ticks
;
152 // Make space for 16 records, which "should be enough".
153 struct xcede_latency_payload
{
155 struct xcede_latency_record records
[16];
158 struct xcede_latency_parameter
{
160 struct xcede_latency_payload payload
;
164 static unsigned int nr_xcede_records
;
165 static struct xcede_latency_parameter xcede_latency_parameter __initdata
;
167 static int __init
parse_cede_parameters(void)
169 struct xcede_latency_payload
*payload
;
170 u32 total_xcede_records_size
;
171 u8 xcede_record_size
;
175 ret
= rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
176 NULL
, CEDE_LATENCY_TOKEN
, __pa(&xcede_latency_parameter
),
177 sizeof(xcede_latency_parameter
));
179 pr_err("xcede: Error parsing CEDE_LATENCY_TOKEN\n");
183 payload_size
= be16_to_cpu(xcede_latency_parameter
.payload_size
);
184 payload
= &xcede_latency_parameter
.payload
;
186 xcede_record_size
= payload
->record_size
+ 1;
188 if (xcede_record_size
!= sizeof(struct xcede_latency_record
)) {
189 pr_err("xcede: Expected record-size %lu. Observed size %u.\n",
190 sizeof(struct xcede_latency_record
), xcede_record_size
);
194 pr_info("xcede: xcede_record_size = %d\n", xcede_record_size
);
197 * Since the payload_size includes the last NULL byte and the
198 * xcede_record_size, the remaining bytes correspond to array of all
199 * cede_latency settings.
201 total_xcede_records_size
= payload_size
- 2;
202 nr_xcede_records
= total_xcede_records_size
/ xcede_record_size
;
204 for (i
= 0; i
< nr_xcede_records
; i
++) {
205 struct xcede_latency_record
*record
= &payload
->records
[i
];
206 u64 latency_ticks
= be64_to_cpu(record
->latency_ticks
);
207 u8 wake_on_irqs
= record
->wake_on_irqs
;
208 u8 hint
= record
->hint
;
210 pr_info("xcede: Record %d : hint = %u, latency = 0x%llx tb ticks, Wake-on-irq = %u\n",
211 i
, hint
, latency_ticks
, wake_on_irqs
);
217 #define NR_DEDICATED_STATES 2 /* snooze, CEDE */
218 static u8 cede_latency_hint
[NR_DEDICATED_STATES
];
221 int dedicated_cede_loop(struct cpuidle_device
*dev
, struct cpuidle_driver
*drv
,
226 pseries_idle_prolog();
227 get_lppaca()->donate_dedicated_cpu
= 1;
228 old_latency_hint
= get_lppaca()->cede_latency_hint
;
229 get_lppaca()->cede_latency_hint
= cede_latency_hint
[index
];
232 check_and_cede_processor();
234 raw_local_irq_disable();
235 get_lppaca()->donate_dedicated_cpu
= 0;
236 get_lppaca()->cede_latency_hint
= old_latency_hint
;
238 pseries_idle_epilog();
244 int shared_cede_loop(struct cpuidle_device
*dev
, struct cpuidle_driver
*drv
,
248 pseries_idle_prolog();
251 * Yield the processor to the hypervisor. We return if
252 * an external interrupt occurs (which are driven prior
253 * to returning here) or if a prod occurs from another
254 * processor. When returning here, external interrupts
257 check_and_cede_processor();
259 raw_local_irq_disable();
260 pseries_idle_epilog();
266 * States for dedicated partition case.
268 static struct cpuidle_state dedicated_states
[NR_DEDICATED_STATES
] = {
273 .target_residency
= 0,
274 .enter
= &snooze_loop
,
275 .flags
= CPUIDLE_FLAG_POLLING
},
280 .target_residency
= 100,
281 .enter
= &dedicated_cede_loop
},
285 * States for shared partition case.
287 static struct cpuidle_state shared_states
[] = {
292 .target_residency
= 0,
293 .enter
= &snooze_loop
,
294 .flags
= CPUIDLE_FLAG_POLLING
},
296 .name
= "Shared Cede",
297 .desc
= "Shared Cede",
299 .target_residency
= 100,
300 .enter
= &shared_cede_loop
},
303 static int pseries_cpuidle_cpu_online(unsigned int cpu
)
305 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
307 if (dev
&& cpuidle_get_driver()) {
308 cpuidle_pause_and_lock();
309 cpuidle_enable_device(dev
);
310 cpuidle_resume_and_unlock();
315 static int pseries_cpuidle_cpu_dead(unsigned int cpu
)
317 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
319 if (dev
&& cpuidle_get_driver()) {
320 cpuidle_pause_and_lock();
321 cpuidle_disable_device(dev
);
322 cpuidle_resume_and_unlock();
328 * pseries_cpuidle_driver_init()
330 static int pseries_cpuidle_driver_init(void)
333 struct cpuidle_driver
*drv
= &pseries_idle_driver
;
335 drv
->state_count
= 0;
337 for (idle_state
= 0; idle_state
< max_idle_state
; ++idle_state
) {
338 /* Is the state not enabled? */
339 if (cpuidle_state_table
[idle_state
].enter
== NULL
)
342 drv
->states
[drv
->state_count
] = /* structure copy */
343 cpuidle_state_table
[idle_state
];
345 drv
->state_count
+= 1;
351 static void __init
fixup_cede0_latency(void)
353 struct xcede_latency_payload
*payload
;
354 u64 min_xcede_latency_us
= UINT_MAX
;
357 if (parse_cede_parameters())
360 pr_info("cpuidle: Skipping the %d Extended CEDE idle states\n",
363 payload
= &xcede_latency_parameter
.payload
;
366 * The CEDE idle state maps to CEDE(0). While the hypervisor
367 * does not advertise CEDE(0) exit latency values, it does
368 * advertise the latency values of the extended CEDE states.
369 * We use the lowest advertised exit latency value as a proxy
370 * for the exit latency of CEDE(0).
372 for (i
= 0; i
< nr_xcede_records
; i
++) {
373 struct xcede_latency_record
*record
= &payload
->records
[i
];
374 u8 hint
= record
->hint
;
375 u64 latency_tb
= be64_to_cpu(record
->latency_ticks
);
376 u64 latency_us
= DIV_ROUND_UP_ULL(tb_to_ns(latency_tb
), NSEC_PER_USEC
);
379 * We expect the exit latency of an extended CEDE
380 * state to be non-zero, it to since it takes at least
381 * a few nanoseconds to wakeup the idle CPU and
382 * dispatch the virtual processor into the Linux
385 * So we consider only non-zero value for performing
386 * the fixup of CEDE(0) latency.
388 if (latency_us
== 0) {
389 pr_warn("cpuidle: Skipping xcede record %d [hint=%d]. Exit latency = 0us\n",
394 if (latency_us
< min_xcede_latency_us
)
395 min_xcede_latency_us
= latency_us
;
398 if (min_xcede_latency_us
!= UINT_MAX
) {
399 dedicated_states
[1].exit_latency
= min_xcede_latency_us
;
400 dedicated_states
[1].target_residency
= 10 * (min_xcede_latency_us
);
401 pr_info("cpuidle: Fixed up CEDE exit latency to %llu us\n",
402 min_xcede_latency_us
);
408 * pseries_idle_probe()
409 * Choose state table for shared versus dedicated partition
411 static int __init
pseries_idle_probe(void)
414 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
417 if (firmware_has_feature(FW_FEATURE_SPLPAR
)) {
418 if (lppaca_shared_proc()) {
419 cpuidle_state_table
= shared_states
;
420 max_idle_state
= ARRAY_SIZE(shared_states
);
423 * Use firmware provided latency values
424 * starting with POWER10 platforms. In the
425 * case that we are running on a POWER10
426 * platform but in an earlier compat mode, we
427 * can still use the firmware provided values.
429 * However, on platforms prior to POWER10, we
430 * cannot rely on the accuracy of the firmware
431 * provided latency values. On such platforms,
432 * go with the conservative default estimate
435 if (cpu_has_feature(CPU_FTR_ARCH_31
) || pvr_version_is(PVR_POWER10
))
436 fixup_cede0_latency();
437 cpuidle_state_table
= dedicated_states
;
438 max_idle_state
= NR_DEDICATED_STATES
;
443 if (max_idle_state
> 1) {
444 snooze_timeout_en
= true;
445 snooze_timeout
= cpuidle_state_table
[1].target_residency
*
451 static int __init
pseries_processor_idle_init(void)
455 retval
= pseries_idle_probe();
459 pseries_cpuidle_driver_init();
460 retval
= cpuidle_register(&pseries_idle_driver
, NULL
);
462 printk(KERN_DEBUG
"Registration of pseries driver failed.\n");
466 retval
= cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN
,
467 "cpuidle/pseries:online",
468 pseries_cpuidle_cpu_online
, NULL
);
470 retval
= cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD
,
471 "cpuidle/pseries:DEAD", NULL
,
472 pseries_cpuidle_cpu_dead
);
474 printk(KERN_DEBUG
"pseries_idle_driver registered\n");
478 device_initcall(pseries_processor_idle_init
);