162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef _ASM_POWERPC_CPUIDLE_H
362306a36Sopenharmony_ci#define _ASM_POWERPC_CPUIDLE_H
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#ifdef CONFIG_PPC_POWERNV
662306a36Sopenharmony_ci/* Thread state used in powernv idle state management */
762306a36Sopenharmony_ci#define PNV_THREAD_RUNNING              0
862306a36Sopenharmony_ci#define PNV_THREAD_NAP                  1
962306a36Sopenharmony_ci#define PNV_THREAD_SLEEP                2
1062306a36Sopenharmony_ci#define PNV_THREAD_WINKLE               3
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci/*
1362306a36Sopenharmony_ci * Core state used in powernv idle for POWER8.
1462306a36Sopenharmony_ci *
1562306a36Sopenharmony_ci * The lock bit synchronizes updates to the state, as well as parts of the
1662306a36Sopenharmony_ci * sleep/wake code (see kernel/idle_book3s.S).
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci * Bottom 8 bits track the idle state of each thread. Bit is cleared before
1962306a36Sopenharmony_ci * the thread executes an idle instruction (nap/sleep/winkle).
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci * Then there is winkle tracking. A core does not lose complete state
2262306a36Sopenharmony_ci * until every thread is in winkle. So the winkle count field counts the
2362306a36Sopenharmony_ci * number of threads in winkle (small window of false positives is okay
2462306a36Sopenharmony_ci * around the sleep/wake, so long as there are no false negatives).
2562306a36Sopenharmony_ci *
2662306a36Sopenharmony_ci * When the winkle count reaches 8 (the COUNT_ALL_BIT becomes set), then
2762306a36Sopenharmony_ci * the THREAD_WINKLE_BITS are set, which indicate which threads have not
2862306a36Sopenharmony_ci * yet woken from the winkle state.
2962306a36Sopenharmony_ci */
3062306a36Sopenharmony_ci#define NR_PNV_CORE_IDLE_LOCK_BIT		28
3162306a36Sopenharmony_ci#define PNV_CORE_IDLE_LOCK_BIT			(1ULL << NR_PNV_CORE_IDLE_LOCK_BIT)
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#define PNV_CORE_IDLE_WINKLE_COUNT_SHIFT	16
3462306a36Sopenharmony_ci#define PNV_CORE_IDLE_WINKLE_COUNT		0x00010000
3562306a36Sopenharmony_ci#define PNV_CORE_IDLE_WINKLE_COUNT_BITS		0x000F0000
3662306a36Sopenharmony_ci#define PNV_CORE_IDLE_THREAD_WINKLE_BITS_SHIFT	8
3762306a36Sopenharmony_ci#define PNV_CORE_IDLE_THREAD_WINKLE_BITS	0x0000FF00
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci#define PNV_CORE_IDLE_THREAD_BITS       	0x000000FF
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci/*
4262306a36Sopenharmony_ci * ============================ NOTE =================================
4362306a36Sopenharmony_ci * The older firmware populates only the RL field in the psscr_val and
4462306a36Sopenharmony_ci * sets the psscr_mask to 0xf. On such a firmware, the kernel sets the
4562306a36Sopenharmony_ci * remaining PSSCR fields to default values as follows:
4662306a36Sopenharmony_ci *
4762306a36Sopenharmony_ci * - ESL and EC bits are to 1. So wakeup from any stop state will be
4862306a36Sopenharmony_ci *   at vector 0x100.
4962306a36Sopenharmony_ci *
5062306a36Sopenharmony_ci * - MTL and PSLL are set to the maximum allowed value as per the ISA,
5162306a36Sopenharmony_ci *    i.e. 15.
5262306a36Sopenharmony_ci *
5362306a36Sopenharmony_ci * - The Transition Rate, TR is set to the Maximum value 3.
5462306a36Sopenharmony_ci */
5562306a36Sopenharmony_ci#define PSSCR_HV_DEFAULT_VAL    (PSSCR_ESL | PSSCR_EC |		    \
5662306a36Sopenharmony_ci				PSSCR_PSLL_MASK | PSSCR_TR_MASK |   \
5762306a36Sopenharmony_ci				PSSCR_MTL_MASK)
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci#define PSSCR_HV_DEFAULT_MASK   (PSSCR_ESL | PSSCR_EC |		    \
6062306a36Sopenharmony_ci				PSSCR_PSLL_MASK | PSSCR_TR_MASK |   \
6162306a36Sopenharmony_ci				PSSCR_MTL_MASK | PSSCR_RL_MASK)
6262306a36Sopenharmony_ci#define PSSCR_EC_SHIFT    20
6362306a36Sopenharmony_ci#define PSSCR_ESL_SHIFT   21
6462306a36Sopenharmony_ci#define GET_PSSCR_EC(x)   (((x) & PSSCR_EC) >> PSSCR_EC_SHIFT)
6562306a36Sopenharmony_ci#define GET_PSSCR_ESL(x)  (((x) & PSSCR_ESL) >> PSSCR_ESL_SHIFT)
6662306a36Sopenharmony_ci#define GET_PSSCR_RL(x)   ((x) & PSSCR_RL_MASK)
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#define ERR_EC_ESL_MISMATCH		-1
6962306a36Sopenharmony_ci#define ERR_DEEP_STATE_ESL_MISMATCH	-2
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci#ifndef __ASSEMBLY__
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci#define PNV_IDLE_NAME_LEN    16
7462306a36Sopenharmony_cistruct pnv_idle_states_t {
7562306a36Sopenharmony_ci	char name[PNV_IDLE_NAME_LEN];
7662306a36Sopenharmony_ci	u32 latency_ns;
7762306a36Sopenharmony_ci	u32 residency_ns;
7862306a36Sopenharmony_ci	u64 psscr_val;
7962306a36Sopenharmony_ci	u64 psscr_mask;
8062306a36Sopenharmony_ci	u32 flags;
8162306a36Sopenharmony_ci	bool valid;
8262306a36Sopenharmony_ci};
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ciextern struct pnv_idle_states_t *pnv_idle_states;
8562306a36Sopenharmony_ciextern int nr_pnv_idle_states;
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ciunsigned long pnv_cpu_offline(unsigned int cpu);
8862306a36Sopenharmony_ciint __init validate_psscr_val_mask(u64 *psscr_val, u64 *psscr_mask, u32 flags);
8962306a36Sopenharmony_cistatic inline void report_invalid_psscr_val(u64 psscr_val, int err)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	switch (err) {
9262306a36Sopenharmony_ci	case ERR_EC_ESL_MISMATCH:
9362306a36Sopenharmony_ci		pr_warn("Invalid psscr 0x%016llx : ESL,EC bits unequal",
9462306a36Sopenharmony_ci			psscr_val);
9562306a36Sopenharmony_ci		break;
9662306a36Sopenharmony_ci	case ERR_DEEP_STATE_ESL_MISMATCH:
9762306a36Sopenharmony_ci		pr_warn("Invalid psscr 0x%016llx : ESL cleared for deep stop-state",
9862306a36Sopenharmony_ci			psscr_val);
9962306a36Sopenharmony_ci	}
10062306a36Sopenharmony_ci}
10162306a36Sopenharmony_ci#endif
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci#endif
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci#endif
106