Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2015 ARM Limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #define pr_fmt(fmt) "psci: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/arm-smccc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/cpuidle.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/linkage.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/psci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <uapi/linux/psci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <asm/cpuidle.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <asm/cputype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <asm/system_misc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <asm/smp_plat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <asm/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * While a 64-bit OS can make calls with SMC32 calling conventions, for some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * calls it is necessary to use SMC64 to pass or return 64-bit values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * For such calls PSCI_FN_NATIVE(version, name) will choose the appropriate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * (native-width) function ID.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #ifdef CONFIG_64BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define PSCI_FN_NATIVE(version, name)	PSCI_##version##_FN64_##name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define PSCI_FN_NATIVE(version, name)	PSCI_##version##_FN_##name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * The CPU any Trusted OS is resident on. The trusted OS may reject CPU_OFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * calls to its resident CPU, so we must avoid issuing those. We never migrate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * a Trusted OS even if it claims to be capable of migration -- doing so will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * require cooperation with a Trusted OS driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static int resident_cpu = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) struct psci_operations psci_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static enum arm_smccc_conduit psci_conduit = SMCCC_CONDUIT_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) bool psci_tos_resident_on(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	return cpu == resident_cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) typedef unsigned long (psci_fn)(unsigned long, unsigned long,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 				unsigned long, unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static psci_fn *invoke_psci_fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) static struct psci_0_1_function_ids psci_0_1_function_ids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) struct psci_0_1_function_ids get_psci_0_1_function_ids(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	return psci_0_1_function_ids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #define PSCI_0_2_POWER_STATE_MASK		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 				(PSCI_0_2_POWER_STATE_ID_MASK | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 				PSCI_0_2_POWER_STATE_TYPE_MASK | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 				PSCI_0_2_POWER_STATE_AFFL_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) #define PSCI_1_0_EXT_POWER_STATE_MASK		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 				(PSCI_1_0_EXT_POWER_STATE_ID_MASK | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 				PSCI_1_0_EXT_POWER_STATE_TYPE_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) static u32 psci_cpu_suspend_feature;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static bool psci_system_reset2_supported;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static inline bool psci_has_ext_power_state(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	return psci_cpu_suspend_feature &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 				PSCI_1_0_FEATURES_CPU_SUSPEND_PF_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) bool psci_has_osi_support(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	return psci_cpu_suspend_feature & PSCI_1_0_OS_INITIATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static inline bool psci_power_state_loses_context(u32 state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	const u32 mask = psci_has_ext_power_state() ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 					PSCI_1_0_EXT_POWER_STATE_TYPE_MASK :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 					PSCI_0_2_POWER_STATE_TYPE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	return state & mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) bool psci_power_state_is_valid(u32 state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	const u32 valid_mask = psci_has_ext_power_state() ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			       PSCI_1_0_EXT_POWER_STATE_MASK :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			       PSCI_0_2_POWER_STATE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	return !(state & ~valid_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static unsigned long __invoke_psci_fn_hvc(unsigned long function_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			unsigned long arg0, unsigned long arg1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			unsigned long arg2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	struct arm_smccc_res res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	return res.a0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static unsigned long __invoke_psci_fn_smc(unsigned long function_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			unsigned long arg0, unsigned long arg1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			unsigned long arg2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	struct arm_smccc_res res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	return res.a0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static int psci_to_linux_errno(int errno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	switch (errno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	case PSCI_RET_SUCCESS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	case PSCI_RET_NOT_SUPPORTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	case PSCI_RET_INVALID_PARAMS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	case PSCI_RET_INVALID_ADDRESS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	case PSCI_RET_DENIED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static u32 psci_0_1_get_version(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	return PSCI_VERSION(0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static u32 psci_0_2_get_version(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int psci_set_osi_mode(bool enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	unsigned long suspend_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	suspend_mode = enable ? PSCI_1_0_SUSPEND_MODE_OSI :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			PSCI_1_0_SUSPEND_MODE_PC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	err = invoke_psci_fn(PSCI_1_0_FN_SET_SUSPEND_MODE, suspend_mode, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	return psci_to_linux_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static int __psci_cpu_suspend(u32 fn, u32 state, unsigned long entry_point)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	err = invoke_psci_fn(fn, state, entry_point, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	return psci_to_linux_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static int psci_0_1_cpu_suspend(u32 state, unsigned long entry_point)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	return __psci_cpu_suspend(psci_0_1_function_ids.cpu_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 				  state, entry_point);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static int psci_0_2_cpu_suspend(u32 state, unsigned long entry_point)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	return __psci_cpu_suspend(PSCI_FN_NATIVE(0_2, CPU_SUSPEND),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 				  state, entry_point);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static int __psci_cpu_off(u32 fn, u32 state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	err = invoke_psci_fn(fn, state, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	return psci_to_linux_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static int psci_0_1_cpu_off(u32 state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	return __psci_cpu_off(psci_0_1_function_ids.cpu_off, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static int psci_0_2_cpu_off(u32 state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	return __psci_cpu_off(PSCI_0_2_FN_CPU_OFF, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static int __psci_cpu_on(u32 fn, unsigned long cpuid, unsigned long entry_point)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	err = invoke_psci_fn(fn, cpuid, entry_point, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	return psci_to_linux_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static int psci_0_1_cpu_on(unsigned long cpuid, unsigned long entry_point)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	return __psci_cpu_on(psci_0_1_function_ids.cpu_on, cpuid, entry_point);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static int psci_0_2_cpu_on(unsigned long cpuid, unsigned long entry_point)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	return __psci_cpu_on(PSCI_FN_NATIVE(0_2, CPU_ON), cpuid, entry_point);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static int __psci_migrate(u32 fn, unsigned long cpuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	err = invoke_psci_fn(fn, cpuid, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	return psci_to_linux_errno(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static int psci_0_1_migrate(unsigned long cpuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	return __psci_migrate(psci_0_1_function_ids.migrate, cpuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static int psci_0_2_migrate(unsigned long cpuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	return __psci_migrate(PSCI_FN_NATIVE(0_2, MIGRATE), cpuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static int psci_affinity_info(unsigned long target_affinity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		unsigned long lowest_affinity_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	return invoke_psci_fn(PSCI_FN_NATIVE(0_2, AFFINITY_INFO),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			      target_affinity, lowest_affinity_level, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static int psci_migrate_info_type(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static unsigned long psci_migrate_info_up_cpu(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	return invoke_psci_fn(PSCI_FN_NATIVE(0_2, MIGRATE_INFO_UP_CPU),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			      0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static void set_conduit(enum arm_smccc_conduit conduit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	switch (conduit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	case SMCCC_CONDUIT_HVC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		invoke_psci_fn = __invoke_psci_fn_hvc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	case SMCCC_CONDUIT_SMC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		invoke_psci_fn = __invoke_psci_fn_smc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		WARN(1, "Unexpected PSCI conduit %d\n", conduit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	psci_conduit = conduit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static int get_set_conduit_method(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	const char *method;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	pr_info("probing for conduit method from DT.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	if (of_property_read_string(np, "method", &method)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		pr_warn("missing \"method\" property\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if (!strcmp("hvc", method)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		set_conduit(SMCCC_CONDUIT_HVC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	} else if (!strcmp("smc", method)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		set_conduit(SMCCC_CONDUIT_SMC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		pr_warn("invalid \"method\" property: %s\n", method);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static int psci_sys_reset(struct notifier_block *nb, unsigned long action,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			  void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if ((reboot_mode == REBOOT_WARM || reboot_mode == REBOOT_SOFT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	    psci_system_reset2_supported) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		 * reset_type[31] = 0 (architectural)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		 * reset_type[30:0] = 0 (SYSTEM_WARM_RESET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		 * cookie = 0 (ignored by the implementation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		invoke_psci_fn(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2), 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static struct notifier_block psci_sys_reset_nb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	.notifier_call = psci_sys_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	.priority = 129,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static void psci_sys_poweroff(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static int __init psci_features(u32 psci_func_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	return invoke_psci_fn(PSCI_1_0_FN_PSCI_FEATURES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			      psci_func_id, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) #ifdef CONFIG_CPU_IDLE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static int psci_suspend_finisher(unsigned long state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	u32 power_state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	return psci_ops.cpu_suspend(power_state, __pa_function(cpu_resume));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) int psci_cpu_suspend_enter(u32 state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	if (!psci_power_state_loses_context(state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		ret = psci_ops.cpu_suspend(state, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		ret = cpu_suspend(state, psci_suspend_finisher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static int psci_system_suspend(unsigned long unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	return invoke_psci_fn(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			      __pa_function(cpu_resume), 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) static int psci_system_suspend_enter(suspend_state_t state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	return cpu_suspend(0, psci_system_suspend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) static const struct platform_suspend_ops psci_suspend_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	.valid          = suspend_valid_only_mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	.enter          = psci_system_suspend_enter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) static void __init psci_init_system_reset2(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	ret = psci_features(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	if (ret != PSCI_RET_NOT_SUPPORTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		psci_system_reset2_supported = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static void __init psci_init_system_suspend(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	if (!IS_ENABLED(CONFIG_SUSPEND))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	ret = psci_features(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	if (ret != PSCI_RET_NOT_SUPPORTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		suspend_set_ops(&psci_suspend_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) static void __init psci_init_cpu_suspend(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	int feature = psci_features(PSCI_FN_NATIVE(0_2, CPU_SUSPEND));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (feature != PSCI_RET_NOT_SUPPORTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		psci_cpu_suspend_feature = feature;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)  * Detect the presence of a resident Trusted OS which may cause CPU_OFF to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)  * return DENIED (which would be fatal).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static void __init psci_init_migrate(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	unsigned long cpuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	int type, cpu = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	type = psci_ops.migrate_info_type();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	if (type == PSCI_0_2_TOS_MP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		pr_info("Trusted OS migration not required\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	if (type == PSCI_RET_NOT_SUPPORTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		pr_info("MIGRATE_INFO_TYPE not supported.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	if (type != PSCI_0_2_TOS_UP_MIGRATE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	    type != PSCI_0_2_TOS_UP_NO_MIGRATE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	cpuid = psci_migrate_info_up_cpu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	if (cpuid & ~MPIDR_HWID_BITMASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 			cpuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	cpu = get_logical_index(cpuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	resident_cpu = cpu >= 0 ? cpu : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static void __init psci_init_smccc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	u32 ver = ARM_SMCCC_VERSION_1_0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	int feature;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	feature = psci_features(ARM_SMCCC_VERSION_FUNC_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	if (feature != PSCI_RET_NOT_SUPPORTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		u32 ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		ret = invoke_psci_fn(ARM_SMCCC_VERSION_FUNC_ID, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		if (ret >= ARM_SMCCC_VERSION_1_1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 			arm_smccc_version_init(ret, psci_conduit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 			ver = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	 * Conveniently, the SMCCC and PSCI versions are encoded the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	 * same way. No, this isn't accidental.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	pr_info("SMC Calling Convention v%d.%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		PSCI_VERSION_MAJOR(ver), PSCI_VERSION_MINOR(ver));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) static void __init psci_0_2_set_functions(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	pr_info("Using standard PSCI v0.2 function IDs\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	psci_ops = (struct psci_operations){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		.get_version = psci_0_2_get_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		.cpu_suspend = psci_0_2_cpu_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		.cpu_off = psci_0_2_cpu_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		.cpu_on = psci_0_2_cpu_on,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		.migrate = psci_0_2_migrate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		.affinity_info = psci_affinity_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		.migrate_info_type = psci_migrate_info_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	register_restart_handler(&psci_sys_reset_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	pm_power_off = psci_sys_poweroff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)  * Probe function for PSCI firmware versions >= 0.2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) static int __init psci_probe(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	u32 ver = psci_0_2_get_version();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	pr_info("PSCIv%d.%d detected in firmware.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 			PSCI_VERSION_MAJOR(ver),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 			PSCI_VERSION_MINOR(ver));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		pr_err("Conflicting PSCI version detected.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	psci_0_2_set_functions();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	psci_init_migrate();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	if (PSCI_VERSION_MAJOR(ver) >= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		psci_init_smccc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		psci_init_cpu_suspend();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		psci_init_system_suspend();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		psci_init_system_reset2();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) typedef int (*psci_initcall_t)(const struct device_node *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)  * PSCI init function for PSCI versions >=0.2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)  * Probe based on PSCI PSCI_VERSION function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) static int __init psci_0_2_init(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	err = get_set_conduit_method(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	 * Starting with v0.2, the PSCI specification introduced a call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	 * (PSCI_VERSION) that allows probing the firmware version, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	 * that PSCI function IDs and version specific initialization
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	 * can be carried out according to the specific version reported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	 * by firmware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	return psci_probe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)  * PSCI < v0.2 get PSCI Function IDs via DT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) static int __init psci_0_1_init(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	u32 id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	err = get_set_conduit_method(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	pr_info("Using PSCI v0.1 Function IDs from DT\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	psci_ops.get_version = psci_0_1_get_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	if (!of_property_read_u32(np, "cpu_suspend", &id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 		psci_0_1_function_ids.cpu_suspend = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		psci_ops.cpu_suspend = psci_0_1_cpu_suspend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	if (!of_property_read_u32(np, "cpu_off", &id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		psci_0_1_function_ids.cpu_off = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		psci_ops.cpu_off = psci_0_1_cpu_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	if (!of_property_read_u32(np, "cpu_on", &id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		psci_0_1_function_ids.cpu_on = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		psci_ops.cpu_on = psci_0_1_cpu_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	if (!of_property_read_u32(np, "migrate", &id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		psci_0_1_function_ids.migrate = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		psci_ops.migrate = psci_0_1_migrate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) static int __init psci_1_0_init(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	err = psci_0_2_init(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	if (psci_has_osi_support()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 		pr_info("OSI mode supported.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 		/* Default to PC mode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		psci_set_osi_mode(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) static const struct of_device_id psci_of_match[] __initconst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	{ .compatible = "arm,psci",	.data = psci_0_1_init},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	{ .compatible = "arm,psci-0.2",	.data = psci_0_2_init},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	{ .compatible = "arm,psci-1.0",	.data = psci_1_0_init},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) int __init psci_dt_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	const struct of_device_id *matched_np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	psci_initcall_t init_fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	if (!np || !of_device_is_available(np))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	init_fn = (psci_initcall_t)matched_np->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	ret = init_fn(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) #ifdef CONFIG_ACPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)  * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)  * explicitly clarified in SBBR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) int __init psci_acpi_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	if (!acpi_psci_present()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 		pr_info("is not implemented in ACPI.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	pr_info("probing for conduit method from ACPI.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	if (acpi_psci_use_hvc())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 		set_conduit(SMCCC_CONDUIT_HVC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 		set_conduit(SMCCC_CONDUIT_SMC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	return psci_probe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) #endif