4 * Copyright 2015 IBM Corp.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/types.h>
14 #include <linux/slab.h>
16 #include <linux/device.h>
17 #include <linux/cpu.h>
19 #include <asm/firmware.h>
20 #include <asm/machdep.h>
22 #include <asm/cputhreads.h>
23 #include <asm/cpuidle.h>
24 #include <asm/code-patching.h>
30 /* Power ISA 3.0 allows for stop states 0x0 - 0xF */
31 #define MAX_STOP_STATE 0xF
33 static u32 supported_cpuidle_states
;
35 static int pnv_save_sprs_for_deep_states(void)
41 * hid0, hid1, hid4, hid5, hmeer and lpcr values are symmetric across
42 * all cpus at boot. Get these reg values of current cpu and use the
43 * same across all cpus.
45 uint64_t lpcr_val
= mfspr(SPRN_LPCR
) & ~(u64
)LPCR_PECE1
;
46 uint64_t hid0_val
= mfspr(SPRN_HID0
);
47 uint64_t hid1_val
= mfspr(SPRN_HID1
);
48 uint64_t hid4_val
= mfspr(SPRN_HID4
);
49 uint64_t hid5_val
= mfspr(SPRN_HID5
);
50 uint64_t hmeer_val
= mfspr(SPRN_HMEER
);
52 for_each_possible_cpu(cpu
) {
53 uint64_t pir
= get_hard_smp_processor_id(cpu
);
54 uint64_t hsprg0_val
= (uint64_t)&paca
[cpu
];
56 if (!cpu_has_feature(CPU_FTR_ARCH_300
)) {
58 * HSPRG0 is used to store the cpu's pointer to paca.
59 * Hence last 3 bits are guaranteed to be 0. Program
60 * slw to restore HSPRG0 with 63rd bit set, so that
61 * when a thread wakes up at 0x100 we can use this bit
62 * to distinguish between fastsleep and deep winkle.
63 * This is not necessary with stop/psscr since PLS
64 * field of psscr indicates which state we are waking
69 rc
= opal_slw_set_reg(pir
, SPRN_HSPRG0
, hsprg0_val
);
73 rc
= opal_slw_set_reg(pir
, SPRN_LPCR
, lpcr_val
);
77 /* HIDs are per core registers */
78 if (cpu_thread_in_core(cpu
) == 0) {
80 rc
= opal_slw_set_reg(pir
, SPRN_HMEER
, hmeer_val
);
84 rc
= opal_slw_set_reg(pir
, SPRN_HID0
, hid0_val
);
88 rc
= opal_slw_set_reg(pir
, SPRN_HID1
, hid1_val
);
92 rc
= opal_slw_set_reg(pir
, SPRN_HID4
, hid4_val
);
96 rc
= opal_slw_set_reg(pir
, SPRN_HID5
, hid5_val
);
105 static void pnv_alloc_idle_core_states(void)
108 int nr_cores
= cpu_nr_cores();
109 u32
*core_idle_state
;
112 * core_idle_state - First 8 bits track the idle state of each thread
113 * of the core. The 8th bit is the lock bit. Initially all thread bits
114 * are set. They are cleared when the thread enters deep idle state
115 * like sleep and winkle. Initially the lock bit is cleared.
116 * The lock bit has 2 purposes
117 * a. While the first thread is restoring core state, it prevents
118 * other threads in the core from switching to process context.
119 * b. While the last thread in the core is saving the core state, it
120 * prevents a different thread from waking up.
122 for (i
= 0; i
< nr_cores
; i
++) {
123 int first_cpu
= i
* threads_per_core
;
124 int node
= cpu_to_node(first_cpu
);
126 core_idle_state
= kmalloc_node(sizeof(u32
), GFP_KERNEL
, node
);
127 *core_idle_state
= PNV_CORE_IDLE_THREAD_BITS
;
129 for (j
= 0; j
< threads_per_core
; j
++) {
130 int cpu
= first_cpu
+ j
;
132 paca
[cpu
].core_idle_state_ptr
= core_idle_state
;
133 paca
[cpu
].thread_idle_state
= PNV_THREAD_RUNNING
;
134 paca
[cpu
].thread_mask
= 1 << j
;
138 update_subcore_sibling_mask();
140 if (supported_cpuidle_states
& OPAL_PM_LOSE_FULL_CONTEXT
)
141 pnv_save_sprs_for_deep_states();
144 u32
pnv_get_supported_cpuidle_states(void)
146 return supported_cpuidle_states
;
148 EXPORT_SYMBOL_GPL(pnv_get_supported_cpuidle_states
);
150 static void pnv_fastsleep_workaround_apply(void *info
)
156 rc
= opal_config_cpu_idle_state(OPAL_CONFIG_IDLE_FASTSLEEP
,
157 OPAL_CONFIG_IDLE_APPLY
);
163 * Used to store fastsleep workaround state
164 * 0 - Workaround applied/undone at fastsleep entry/exit path (Default)
165 * 1 - Workaround applied once, never undone.
167 static u8 fastsleep_workaround_applyonce
;
169 static ssize_t
show_fastsleep_workaround_applyonce(struct device
*dev
,
170 struct device_attribute
*attr
, char *buf
)
172 return sprintf(buf
, "%u\n", fastsleep_workaround_applyonce
);
175 static ssize_t
store_fastsleep_workaround_applyonce(struct device
*dev
,
176 struct device_attribute
*attr
, const char *buf
,
179 cpumask_t primary_thread_mask
;
183 if (kstrtou8(buf
, 0, &val
) || val
!= 1)
186 if (fastsleep_workaround_applyonce
== 1)
190 * fastsleep_workaround_applyonce = 1 implies
191 * fastsleep workaround needs to be left in 'applied' state on all
192 * the cores. Do this by-
193 * 1. Patching out the call to 'undo' workaround in fastsleep exit path
194 * 2. Sending ipi to all the cores which have at least one online thread
195 * 3. Patching out the call to 'apply' workaround in fastsleep entry
197 * There is no need to send ipi to cores which have all threads
198 * offlined, as last thread of the core entering fastsleep or deeper
199 * state would have applied workaround.
201 err
= patch_instruction(
202 (unsigned int *)pnv_fastsleep_workaround_at_exit
,
205 pr_err("fastsleep_workaround_applyonce change failed while patching pnv_fastsleep_workaround_at_exit");
210 primary_thread_mask
= cpu_online_cores_map();
211 on_each_cpu_mask(&primary_thread_mask
,
212 pnv_fastsleep_workaround_apply
,
216 pr_err("fastsleep_workaround_applyonce change failed while running pnv_fastsleep_workaround_apply");
220 err
= patch_instruction(
221 (unsigned int *)pnv_fastsleep_workaround_at_entry
,
224 pr_err("fastsleep_workaround_applyonce change failed while patching pnv_fastsleep_workaround_at_entry");
228 fastsleep_workaround_applyonce
= 1;
235 static DEVICE_ATTR(fastsleep_workaround_applyonce
, 0600,
236 show_fastsleep_workaround_applyonce
,
237 store_fastsleep_workaround_applyonce
);
240 * The default stop state that will be used by ppc_md.power_save
241 * function on platforms that support stop instruction.
243 static u64 pnv_default_stop_val
;
244 static u64 pnv_default_stop_mask
;
245 static bool default_stop_found
;
248 * Used for ppc_md.power_save which needs a function with no parameters
250 static void power9_idle(void)
252 power9_idle_stop(pnv_default_stop_val
, pnv_default_stop_mask
);
256 * First deep stop state. Used to figure out when to save/restore
257 * hypervisor context.
259 u64 pnv_first_deep_stop_state
= MAX_STOP_STATE
;
262 * psscr value and mask of the deepest stop idle state.
263 * Used when a cpu is offlined.
265 static u64 pnv_deepest_stop_psscr_val
;
266 static u64 pnv_deepest_stop_psscr_mask
;
267 static bool deepest_stop_found
;
270 * pnv_cpu_offline: A function that puts the CPU into the deepest
271 * available platform idle state on a CPU-Offline.
273 unsigned long pnv_cpu_offline(unsigned int cpu
)
277 u32 idle_states
= pnv_get_supported_cpuidle_states();
279 if (cpu_has_feature(CPU_FTR_ARCH_300
) && deepest_stop_found
) {
280 srr1
= power9_idle_stop(pnv_deepest_stop_psscr_val
,
281 pnv_deepest_stop_psscr_mask
);
282 } else if (idle_states
& OPAL_PM_WINKLE_ENABLED
) {
283 srr1
= power7_winkle();
284 } else if ((idle_states
& OPAL_PM_SLEEP_ENABLED
) ||
285 (idle_states
& OPAL_PM_SLEEP_ENABLED_ER1
)) {
286 srr1
= power7_sleep();
287 } else if (idle_states
& OPAL_PM_NAP_ENABLED
) {
288 srr1
= power7_nap(1);
290 /* This is the fallback method. We emulate snooze */
291 while (!generic_check_cpu_restart(cpu
)) {
303 * Power ISA 3.0 idle initialization.
305 * POWER ISA 3.0 defines a new SPR Processor stop Status and Control
306 * Register (PSSCR) to control idle behavior.
309 * ----------------------------------------------------------
310 * | PLS | /// | SD | ESL | EC | PSLL | /// | TR | MTL | RL |
311 * ----------------------------------------------------------
312 * 0 4 41 42 43 44 48 54 56 60
315 * Bits 0:3 - Power-Saving Level Status (PLS). This field indicates the
316 * lowest power-saving state the thread entered since stop instruction was
319 * Bit 41 - Status Disable(SD)
320 * 0 - Shows PLS entries
321 * 1 - PLS entries are all 0
323 * Bit 42 - Enable State Loss
324 * 0 - No state is lost irrespective of other fields
325 * 1 - Allows state loss
327 * Bit 43 - Exit Criterion
328 * 0 - Exit from power-save mode on any interrupt
329 * 1 - Exit from power-save mode controlled by LPCR's PECE bits
331 * Bits 44:47 - Power-Saving Level Limit
332 * This limits the power-saving level that can be entered into.
334 * Bits 60:63 - Requested Level
335 * Used to specify which power-saving level must be entered on executing
339 int validate_psscr_val_mask(u64
*psscr_val
, u64
*psscr_mask
, u32 flags
)
344 * psscr_mask == 0xf indicates an older firmware.
345 * Set remaining fields of psscr to the default values.
346 * See NOTE above definition of PSSCR_HV_DEFAULT_VAL
348 if (*psscr_mask
== 0xf) {
349 *psscr_val
= *psscr_val
| PSSCR_HV_DEFAULT_VAL
;
350 *psscr_mask
= PSSCR_HV_DEFAULT_MASK
;
355 * New firmware is expected to set the psscr_val bits correctly.
356 * Validate that the following invariants are correctly maintained by
358 * - ESL bit value matches the EC bit value.
359 * - ESL bit is set for all the deep stop states.
361 if (GET_PSSCR_ESL(*psscr_val
) != GET_PSSCR_EC(*psscr_val
)) {
362 err
= ERR_EC_ESL_MISMATCH
;
363 } else if ((flags
& OPAL_PM_LOSE_FULL_CONTEXT
) &&
364 GET_PSSCR_ESL(*psscr_val
) == 0) {
365 err
= ERR_DEEP_STATE_ESL_MISMATCH
;
372 * pnv_arch300_idle_init: Initializes the default idle state, first
373 * deep idle state and deepest idle state on
376 * @np: /ibm,opal/power-mgt device node
377 * @flags: cpu-idle-state-flags array
378 * @dt_idle_states: Number of idle state entries
379 * Returns 0 on success
381 static int __init
pnv_power9_idle_init(struct device_node
*np
, u32
*flags
,
384 u64
*psscr_val
= NULL
;
385 u64
*psscr_mask
= NULL
;
386 u32
*residency_ns
= NULL
;
387 u64 max_residency_ns
= 0;
390 psscr_val
= kcalloc(dt_idle_states
, sizeof(*psscr_val
), GFP_KERNEL
);
391 psscr_mask
= kcalloc(dt_idle_states
, sizeof(*psscr_mask
), GFP_KERNEL
);
392 residency_ns
= kcalloc(dt_idle_states
, sizeof(*residency_ns
),
395 if (!psscr_val
|| !psscr_mask
|| !residency_ns
) {
400 if (of_property_read_u64_array(np
,
401 "ibm,cpu-idle-state-psscr",
402 psscr_val
, dt_idle_states
)) {
403 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr in DT\n");
408 if (of_property_read_u64_array(np
,
409 "ibm,cpu-idle-state-psscr-mask",
410 psscr_mask
, dt_idle_states
)) {
411 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-psscr-mask in DT\n");
416 if (of_property_read_u32_array(np
,
417 "ibm,cpu-idle-state-residency-ns",
418 residency_ns
, dt_idle_states
)) {
419 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-residency-ns in DT\n");
425 * Set pnv_first_deep_stop_state, pnv_deepest_stop_psscr_{val,mask},
426 * and the pnv_default_stop_{val,mask}.
428 * pnv_first_deep_stop_state should be set to the first stop
429 * level to cause hypervisor state loss.
431 * pnv_deepest_stop_{val,mask} should be set to values corresponding to
432 * the deepest stop state.
434 * pnv_default_stop_{val,mask} should be set to values corresponding to
435 * the shallowest (OPAL_PM_STOP_INST_FAST) loss-less stop state.
437 pnv_first_deep_stop_state
= MAX_STOP_STATE
;
438 for (i
= 0; i
< dt_idle_states
; i
++) {
440 u64 psscr_rl
= psscr_val
[i
] & PSSCR_RL_MASK
;
442 if ((flags
[i
] & OPAL_PM_LOSE_FULL_CONTEXT
) &&
443 (pnv_first_deep_stop_state
> psscr_rl
))
444 pnv_first_deep_stop_state
= psscr_rl
;
446 err
= validate_psscr_val_mask(&psscr_val
[i
], &psscr_mask
[i
],
449 report_invalid_psscr_val(psscr_val
[i
], err
);
453 if (max_residency_ns
< residency_ns
[i
]) {
454 max_residency_ns
= residency_ns
[i
];
455 pnv_deepest_stop_psscr_val
= psscr_val
[i
];
456 pnv_deepest_stop_psscr_mask
= psscr_mask
[i
];
457 deepest_stop_found
= true;
460 if (!default_stop_found
&&
461 (flags
[i
] & OPAL_PM_STOP_INST_FAST
)) {
462 pnv_default_stop_val
= psscr_val
[i
];
463 pnv_default_stop_mask
= psscr_mask
[i
];
464 default_stop_found
= true;
468 if (unlikely(!default_stop_found
)) {
469 pr_warn("cpuidle-powernv: No suitable default stop state found. Disabling platform idle.\n");
471 ppc_md
.power_save
= power9_idle
;
472 pr_info("cpuidle-powernv: Default stop: psscr = 0x%016llx,mask=0x%016llx\n",
473 pnv_default_stop_val
, pnv_default_stop_mask
);
476 if (unlikely(!deepest_stop_found
)) {
477 pr_warn("cpuidle-powernv: No suitable stop state for CPU-Hotplug. Offlined CPUs will busy wait");
479 pr_info("cpuidle-powernv: Deepest stop: psscr = 0x%016llx,mask=0x%016llx\n",
480 pnv_deepest_stop_psscr_val
,
481 pnv_deepest_stop_psscr_mask
);
484 pr_info("cpuidle-powernv: Requested Level (RL) value of first deep stop = 0x%llx\n",
485 pnv_first_deep_stop_state
);
494 * Probe device tree for supported idle states
496 static void __init
pnv_probe_idle_states(void)
498 struct device_node
*np
;
503 np
= of_find_node_by_path("/ibm,opal/power-mgt");
505 pr_warn("opal: PowerMgmt Node not found\n");
508 dt_idle_states
= of_property_count_u32_elems(np
,
509 "ibm,cpu-idle-state-flags");
510 if (dt_idle_states
< 0) {
511 pr_warn("cpuidle-powernv: no idle states found in the DT\n");
515 flags
= kcalloc(dt_idle_states
, sizeof(*flags
), GFP_KERNEL
);
517 if (of_property_read_u32_array(np
,
518 "ibm,cpu-idle-state-flags", flags
, dt_idle_states
)) {
519 pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-flags in DT\n");
523 if (cpu_has_feature(CPU_FTR_ARCH_300
)) {
524 if (pnv_power9_idle_init(np
, flags
, dt_idle_states
))
528 for (i
= 0; i
< dt_idle_states
; i
++)
529 supported_cpuidle_states
|= flags
[i
];
534 static int __init
pnv_init_idle_states(void)
537 supported_cpuidle_states
= 0;
539 if (cpuidle_disable
!= IDLE_NO_OVERRIDE
)
542 pnv_probe_idle_states();
544 if (!(supported_cpuidle_states
& OPAL_PM_SLEEP_ENABLED_ER1
)) {
546 (unsigned int *)pnv_fastsleep_workaround_at_entry
,
549 (unsigned int *)pnv_fastsleep_workaround_at_exit
,
553 * OPAL_PM_SLEEP_ENABLED_ER1 is set. It indicates that
554 * workaround is needed to use fastsleep. Provide sysfs
555 * control to choose how this workaround has to be applied.
557 device_create_file(cpu_subsys
.dev_root
,
558 &dev_attr_fastsleep_workaround_applyonce
);
561 pnv_alloc_idle_core_states();
563 if (supported_cpuidle_states
& OPAL_PM_NAP_ENABLED
)
564 ppc_md
.power_save
= power7_idle
;
569 machine_subsys_initcall(powernv
, pnv_init_idle_states
);