18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _ASM_POWERPC_CPUIDLE_H
38c2ecf20Sopenharmony_ci#define _ASM_POWERPC_CPUIDLE_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_POWERNV
68c2ecf20Sopenharmony_ci/* Thread state used in powernv idle state management */
78c2ecf20Sopenharmony_ci#define PNV_THREAD_RUNNING              0
88c2ecf20Sopenharmony_ci#define PNV_THREAD_NAP                  1
98c2ecf20Sopenharmony_ci#define PNV_THREAD_SLEEP                2
108c2ecf20Sopenharmony_ci#define PNV_THREAD_WINKLE               3
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/*
138c2ecf20Sopenharmony_ci * Core state used in powernv idle for POWER8.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * The lock bit synchronizes updates to the state, as well as parts of the
168c2ecf20Sopenharmony_ci * sleep/wake code (see kernel/idle_book3s.S).
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * Bottom 8 bits track the idle state of each thread. Bit is cleared before
198c2ecf20Sopenharmony_ci * the thread executes an idle instruction (nap/sleep/winkle).
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * Then there is winkle tracking. A core does not lose complete state
228c2ecf20Sopenharmony_ci * until every thread is in winkle. So the winkle count field counts the
238c2ecf20Sopenharmony_ci * number of threads in winkle (small window of false positives is okay
248c2ecf20Sopenharmony_ci * around the sleep/wake, so long as there are no false negatives).
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci * When the winkle count reaches 8 (the COUNT_ALL_BIT becomes set), then
278c2ecf20Sopenharmony_ci * the THREAD_WINKLE_BITS are set, which indicate which threads have not
288c2ecf20Sopenharmony_ci * yet woken from the winkle state.
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci#define NR_PNV_CORE_IDLE_LOCK_BIT		28
318c2ecf20Sopenharmony_ci#define PNV_CORE_IDLE_LOCK_BIT			(1ULL << NR_PNV_CORE_IDLE_LOCK_BIT)
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define PNV_CORE_IDLE_WINKLE_COUNT_SHIFT	16
348c2ecf20Sopenharmony_ci#define PNV_CORE_IDLE_WINKLE_COUNT		0x00010000
358c2ecf20Sopenharmony_ci#define PNV_CORE_IDLE_WINKLE_COUNT_BITS		0x000F0000
368c2ecf20Sopenharmony_ci#define PNV_CORE_IDLE_THREAD_WINKLE_BITS_SHIFT	8
378c2ecf20Sopenharmony_ci#define PNV_CORE_IDLE_THREAD_WINKLE_BITS	0x0000FF00
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define PNV_CORE_IDLE_THREAD_BITS       	0x000000FF
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/*
428c2ecf20Sopenharmony_ci * ============================ NOTE =================================
438c2ecf20Sopenharmony_ci * The older firmware populates only the RL field in the psscr_val and
448c2ecf20Sopenharmony_ci * sets the psscr_mask to 0xf. On such a firmware, the kernel sets the
458c2ecf20Sopenharmony_ci * remaining PSSCR fields to default values as follows:
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci * - ESL and EC bits are to 1. So wakeup from any stop state will be
488c2ecf20Sopenharmony_ci *   at vector 0x100.
498c2ecf20Sopenharmony_ci *
508c2ecf20Sopenharmony_ci * - MTL and PSLL are set to the maximum allowed value as per the ISA,
518c2ecf20Sopenharmony_ci *    i.e. 15.
528c2ecf20Sopenharmony_ci *
538c2ecf20Sopenharmony_ci * - The Transition Rate, TR is set to the Maximum value 3.
548c2ecf20Sopenharmony_ci */
558c2ecf20Sopenharmony_ci#define PSSCR_HV_DEFAULT_VAL    (PSSCR_ESL | PSSCR_EC |		    \
568c2ecf20Sopenharmony_ci				PSSCR_PSLL_MASK | PSSCR_TR_MASK |   \
578c2ecf20Sopenharmony_ci				PSSCR_MTL_MASK)
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#define PSSCR_HV_DEFAULT_MASK   (PSSCR_ESL | PSSCR_EC |		    \
608c2ecf20Sopenharmony_ci				PSSCR_PSLL_MASK | PSSCR_TR_MASK |   \
618c2ecf20Sopenharmony_ci				PSSCR_MTL_MASK | PSSCR_RL_MASK)
628c2ecf20Sopenharmony_ci#define PSSCR_EC_SHIFT    20
638c2ecf20Sopenharmony_ci#define PSSCR_ESL_SHIFT   21
648c2ecf20Sopenharmony_ci#define GET_PSSCR_EC(x)   (((x) & PSSCR_EC) >> PSSCR_EC_SHIFT)
658c2ecf20Sopenharmony_ci#define GET_PSSCR_ESL(x)  (((x) & PSSCR_ESL) >> PSSCR_ESL_SHIFT)
668c2ecf20Sopenharmony_ci#define GET_PSSCR_RL(x)   ((x) & PSSCR_RL_MASK)
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define ERR_EC_ESL_MISMATCH		-1
698c2ecf20Sopenharmony_ci#define ERR_DEEP_STATE_ESL_MISMATCH	-2
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define PNV_IDLE_NAME_LEN    16
748c2ecf20Sopenharmony_cistruct pnv_idle_states_t {
758c2ecf20Sopenharmony_ci	char name[PNV_IDLE_NAME_LEN];
768c2ecf20Sopenharmony_ci	u32 latency_ns;
778c2ecf20Sopenharmony_ci	u32 residency_ns;
788c2ecf20Sopenharmony_ci	u64 psscr_val;
798c2ecf20Sopenharmony_ci	u64 psscr_mask;
808c2ecf20Sopenharmony_ci	u32 flags;
818c2ecf20Sopenharmony_ci	bool valid;
828c2ecf20Sopenharmony_ci};
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ciextern struct pnv_idle_states_t *pnv_idle_states;
858c2ecf20Sopenharmony_ciextern int nr_pnv_idle_states;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ciunsigned long pnv_cpu_offline(unsigned int cpu);
888c2ecf20Sopenharmony_ciint validate_psscr_val_mask(u64 *psscr_val, u64 *psscr_mask, u32 flags);
898c2ecf20Sopenharmony_cistatic inline void report_invalid_psscr_val(u64 psscr_val, int err)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	switch (err) {
928c2ecf20Sopenharmony_ci	case ERR_EC_ESL_MISMATCH:
938c2ecf20Sopenharmony_ci		pr_warn("Invalid psscr 0x%016llx : ESL,EC bits unequal",
948c2ecf20Sopenharmony_ci			psscr_val);
958c2ecf20Sopenharmony_ci		break;
968c2ecf20Sopenharmony_ci	case ERR_DEEP_STATE_ESL_MISMATCH:
978c2ecf20Sopenharmony_ci		pr_warn("Invalid psscr 0x%016llx : ESL cleared for deep stop-state",
988c2ecf20Sopenharmony_ci			psscr_val);
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci#endif
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#endif
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci#endif
106