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>
26 static struct cpuidle_driver pseries_idle_driver
= {
27 .name
= "pseries_idle",
31 static int max_idle_state __read_mostly
;
32 static struct cpuidle_state
*cpuidle_state_table __read_mostly
;
33 static u64 snooze_timeout __read_mostly
;
34 static bool snooze_timeout_en __read_mostly
;
36 static int snooze_loop(struct cpuidle_device
*dev
,
37 struct cpuidle_driver
*drv
,
42 set_thread_flag(TIF_POLLING_NRFLAG
);
44 pseries_idle_prolog();
46 snooze_exit_time
= get_tb() + snooze_timeout
;
48 while (!need_resched()) {
51 if (likely(snooze_timeout_en
) && get_tb() > snooze_exit_time
) {
53 * Task has not woken up but we are exiting the polling
54 * loop anyway. Require a barrier after polling is
55 * cleared to order subsequent test of need_resched().
57 clear_thread_flag(TIF_POLLING_NRFLAG
);
64 clear_thread_flag(TIF_POLLING_NRFLAG
);
68 pseries_idle_epilog();
73 static void check_and_cede_processor(void)
76 * Ensure our interrupt state is properly tracked,
77 * also checks if no interrupt has occurred while we
80 if (prep_irq_for_idle()) {
82 #ifdef CONFIG_TRACE_IRQFLAGS
83 /* Ensure that H_CEDE returns with IRQs on */
84 if (WARN_ON(!(mfmsr() & MSR_EE
)))
91 * XCEDE: Extended CEDE states discovered through the
92 * "ibm,get-systems-parameter" RTAS call with the token
97 * Section 7.3.16 System Parameters Option of PAPR version 2.8.1 has a
98 * table with all the parameters to ibm,get-system-parameters.
99 * CEDE_LATENCY_TOKEN corresponds to the token value for Cede Latency
100 * Settings Information.
102 #define CEDE_LATENCY_TOKEN 45
105 * If the platform supports the cede latency settings information system
106 * parameter it must provide the following information in the NULL terminated
109 * a. The first byte is the length āNā of each cede latency setting record minus
110 * one (zero indicates a length of 1 byte).
112 * b. For each supported cede latency setting a cede latency setting record
113 * consisting of the first āNā bytes as per the following table.
115 * -----------------------------
118 * -----------------------------
119 * | Cede Latency | 1 Byte |
120 * | Specifier Value | |
121 * -----------------------------
122 * | Maximum wakeup | |
123 * | latency in | 8 Bytes |
125 * -----------------------------
126 * | Responsive to | |
127 * | external | 1 Byte |
129 * -----------------------------
131 * This version has cede latency record size = 10.
133 * The structure xcede_latency_payload represents a) and b) with
134 * xcede_latency_record representing the table in b).
136 * xcede_latency_parameter is what gets returned by
137 * ibm,get-systems-parameter RTAS call when made with
138 * CEDE_LATENCY_TOKEN.
140 * These structures are only used to represent the data obtained by the RTAS
141 * call. The data is in big-endian.
143 struct xcede_latency_record
{
145 __be64 latency_ticks
;
149 // Make space for 16 records, which "should be enough".
150 struct xcede_latency_payload
{
152 struct xcede_latency_record records
[16];
155 struct xcede_latency_parameter
{
157 struct xcede_latency_payload payload
;
161 static unsigned int nr_xcede_records
;
162 static struct xcede_latency_parameter xcede_latency_parameter __initdata
;
164 static int __init
parse_cede_parameters(void)
166 struct xcede_latency_payload
*payload
;
167 u32 total_xcede_records_size
;
168 u8 xcede_record_size
;
172 ret
= rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
173 NULL
, CEDE_LATENCY_TOKEN
, __pa(&xcede_latency_parameter
),
174 sizeof(xcede_latency_parameter
));
176 pr_err("xcede: Error parsing CEDE_LATENCY_TOKEN\n");
180 payload_size
= be16_to_cpu(xcede_latency_parameter
.payload_size
);
181 payload
= &xcede_latency_parameter
.payload
;
183 xcede_record_size
= payload
->record_size
+ 1;
185 if (xcede_record_size
!= sizeof(struct xcede_latency_record
)) {
186 pr_err("xcede: Expected record-size %lu. Observed size %u.\n",
187 sizeof(struct xcede_latency_record
), xcede_record_size
);
191 pr_info("xcede: xcede_record_size = %d\n", xcede_record_size
);
194 * Since the payload_size includes the last NULL byte and the
195 * xcede_record_size, the remaining bytes correspond to array of all
196 * cede_latency settings.
198 total_xcede_records_size
= payload_size
- 2;
199 nr_xcede_records
= total_xcede_records_size
/ xcede_record_size
;
201 for (i
= 0; i
< nr_xcede_records
; i
++) {
202 struct xcede_latency_record
*record
= &payload
->records
[i
];
203 u64 latency_ticks
= be64_to_cpu(record
->latency_ticks
);
204 u8 wake_on_irqs
= record
->wake_on_irqs
;
205 u8 hint
= record
->hint
;
207 pr_info("xcede: Record %d : hint = %u, latency = 0x%llx tb ticks, Wake-on-irq = %u\n",
208 i
, hint
, latency_ticks
, wake_on_irqs
);
214 #define NR_DEDICATED_STATES 2 /* snooze, CEDE */
215 static u8 cede_latency_hint
[NR_DEDICATED_STATES
];
217 static int dedicated_cede_loop(struct cpuidle_device
*dev
,
218 struct cpuidle_driver
*drv
,
223 pseries_idle_prolog();
224 get_lppaca()->donate_dedicated_cpu
= 1;
225 old_latency_hint
= get_lppaca()->cede_latency_hint
;
226 get_lppaca()->cede_latency_hint
= cede_latency_hint
[index
];
229 check_and_cede_processor();
232 get_lppaca()->donate_dedicated_cpu
= 0;
233 get_lppaca()->cede_latency_hint
= old_latency_hint
;
235 pseries_idle_epilog();
240 static int shared_cede_loop(struct cpuidle_device
*dev
,
241 struct cpuidle_driver
*drv
,
245 pseries_idle_prolog();
248 * Yield the processor to the hypervisor. We return if
249 * an external interrupt occurs (which are driven prior
250 * to returning here) or if a prod occurs from another
251 * processor. When returning here, external interrupts
254 check_and_cede_processor();
257 pseries_idle_epilog();
263 * States for dedicated partition case.
265 static struct cpuidle_state dedicated_states
[NR_DEDICATED_STATES
] = {
270 .target_residency
= 0,
271 .enter
= &snooze_loop
},
276 .target_residency
= 100,
277 .enter
= &dedicated_cede_loop
},
281 * States for shared partition case.
283 static struct cpuidle_state shared_states
[] = {
288 .target_residency
= 0,
289 .enter
= &snooze_loop
},
291 .name
= "Shared Cede",
292 .desc
= "Shared Cede",
294 .target_residency
= 100,
295 .enter
= &shared_cede_loop
},
298 static int pseries_cpuidle_cpu_online(unsigned int cpu
)
300 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
302 if (dev
&& cpuidle_get_driver()) {
303 cpuidle_pause_and_lock();
304 cpuidle_enable_device(dev
);
305 cpuidle_resume_and_unlock();
310 static int pseries_cpuidle_cpu_dead(unsigned int cpu
)
312 struct cpuidle_device
*dev
= per_cpu(cpuidle_devices
, cpu
);
314 if (dev
&& cpuidle_get_driver()) {
315 cpuidle_pause_and_lock();
316 cpuidle_disable_device(dev
);
317 cpuidle_resume_and_unlock();
323 * pseries_cpuidle_driver_init()
325 static int pseries_cpuidle_driver_init(void)
328 struct cpuidle_driver
*drv
= &pseries_idle_driver
;
330 drv
->state_count
= 0;
332 for (idle_state
= 0; idle_state
< max_idle_state
; ++idle_state
) {
333 /* Is the state not enabled? */
334 if (cpuidle_state_table
[idle_state
].enter
== NULL
)
337 drv
->states
[drv
->state_count
] = /* structure copy */
338 cpuidle_state_table
[idle_state
];
340 drv
->state_count
+= 1;
346 static void __init
fixup_cede0_latency(void)
348 struct xcede_latency_payload
*payload
;
352 min_latency_us
= dedicated_states
[1].exit_latency
; // CEDE latency
354 if (parse_cede_parameters())
357 pr_info("cpuidle: Skipping the %d Extended CEDE idle states\n",
360 payload
= &xcede_latency_parameter
.payload
;
361 for (i
= 0; i
< nr_xcede_records
; i
++) {
362 struct xcede_latency_record
*record
= &payload
->records
[i
];
363 u64 latency_tb
= be64_to_cpu(record
->latency_ticks
);
364 u64 latency_us
= DIV_ROUND_UP_ULL(tb_to_ns(latency_tb
), NSEC_PER_USEC
);
367 pr_warn("cpuidle: xcede record %d has an unrealistic latency of 0us.\n", i
);
369 if (latency_us
< min_latency_us
)
370 min_latency_us
= latency_us
;
374 * By default, we assume that CEDE(0) has exit latency 10us,
375 * since there is no way for us to query from the platform.
377 * However, if the wakeup latency of an Extended CEDE state is
378 * smaller than 10us, then we can be sure that CEDE(0)
379 * requires no more than that.
381 * Perform the fix-up.
383 if (min_latency_us
< dedicated_states
[1].exit_latency
) {
385 * We set a minimum of 1us wakeup latency for cede0 to
386 * distinguish it from snooze
388 u64 cede0_latency
= 1;
390 if (min_latency_us
> cede0_latency
)
391 cede0_latency
= min_latency_us
- 1;
393 dedicated_states
[1].exit_latency
= cede0_latency
;
394 dedicated_states
[1].target_residency
= 10 * (cede0_latency
);
395 pr_info("cpuidle: Fixed up CEDE exit latency to %llu us\n",
402 * pseries_idle_probe()
403 * Choose state table for shared versus dedicated partition
405 static int pseries_idle_probe(void)
408 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
411 if (firmware_has_feature(FW_FEATURE_SPLPAR
)) {
413 * Use local_paca instead of get_lppaca() since
414 * preemption is not disabled, and it is not required in
415 * fact, since lppaca_ptr does not need to be the value
416 * associated to the current CPU, it can be from any CPU.
418 if (lppaca_shared_proc(local_paca
->lppaca_ptr
)) {
419 cpuidle_state_table
= shared_states
;
420 max_idle_state
= ARRAY_SIZE(shared_states
);
422 fixup_cede0_latency();
423 cpuidle_state_table
= dedicated_states
;
424 max_idle_state
= NR_DEDICATED_STATES
;
429 if (max_idle_state
> 1) {
430 snooze_timeout_en
= true;
431 snooze_timeout
= cpuidle_state_table
[1].target_residency
*
437 static int __init
pseries_processor_idle_init(void)
441 retval
= pseries_idle_probe();
445 pseries_cpuidle_driver_init();
446 retval
= cpuidle_register(&pseries_idle_driver
, NULL
);
448 printk(KERN_DEBUG
"Registration of pseries driver failed.\n");
452 retval
= cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN
,
453 "cpuidle/pseries:online",
454 pseries_cpuidle_cpu_online
, NULL
);
456 retval
= cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD
,
457 "cpuidle/pseries:DEAD", NULL
,
458 pseries_cpuidle_cpu_dead
);
460 printk(KERN_DEBUG
"pseries_idle_driver registered\n");
464 device_initcall(pseries_processor_idle_init
);