xref: /linux/drivers/firmware/psci/psci.c (revision 56bf733415b1660afb11e5aaf4a9ba353e45998e)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e720a6c8SUlf Hansson /*
3e720a6c8SUlf Hansson  *
4e720a6c8SUlf Hansson  * Copyright (C) 2015 ARM Limited
5e720a6c8SUlf Hansson  */
6e720a6c8SUlf Hansson 
7e720a6c8SUlf Hansson #define pr_fmt(fmt) "psci: " fmt
8e720a6c8SUlf Hansson 
9e720a6c8SUlf Hansson #include <linux/acpi.h>
10e720a6c8SUlf Hansson #include <linux/arm-smccc.h>
11e720a6c8SUlf Hansson #include <linux/cpuidle.h>
123137f2e6SDmitry Baryshkov #include <linux/debugfs.h>
13e720a6c8SUlf Hansson #include <linux/errno.h>
14e720a6c8SUlf Hansson #include <linux/linkage.h>
15e720a6c8SUlf Hansson #include <linux/of.h>
16e720a6c8SUlf Hansson #include <linux/pm.h>
17e720a6c8SUlf Hansson #include <linux/printk.h>
18e720a6c8SUlf Hansson #include <linux/psci.h>
19e720a6c8SUlf Hansson #include <linux/reboot.h>
20e720a6c8SUlf Hansson #include <linux/slab.h>
21e720a6c8SUlf Hansson #include <linux/suspend.h>
22e720a6c8SUlf Hansson 
23e720a6c8SUlf Hansson #include <uapi/linux/psci.h>
24e720a6c8SUlf Hansson 
25e720a6c8SUlf Hansson #include <asm/cpuidle.h>
26e720a6c8SUlf Hansson #include <asm/cputype.h>
276e085e0aSWill Deacon #include <asm/hypervisor.h>
28e720a6c8SUlf Hansson #include <asm/system_misc.h>
29e720a6c8SUlf Hansson #include <asm/smp_plat.h>
30e720a6c8SUlf Hansson #include <asm/suspend.h>
31e720a6c8SUlf Hansson 
32e720a6c8SUlf Hansson /*
33e720a6c8SUlf Hansson  * While a 64-bit OS can make calls with SMC32 calling conventions, for some
34e720a6c8SUlf Hansson  * calls it is necessary to use SMC64 to pass or return 64-bit values.
35e720a6c8SUlf Hansson  * For such calls PSCI_FN_NATIVE(version, name) will choose the appropriate
36e720a6c8SUlf Hansson  * (native-width) function ID.
37e720a6c8SUlf Hansson  */
38e720a6c8SUlf Hansson #ifdef CONFIG_64BIT
39e720a6c8SUlf Hansson #define PSCI_FN_NATIVE(version, name)	PSCI_##version##_FN64_##name
40e720a6c8SUlf Hansson #else
41e720a6c8SUlf Hansson #define PSCI_FN_NATIVE(version, name)	PSCI_##version##_FN_##name
42e720a6c8SUlf Hansson #endif
43e720a6c8SUlf Hansson 
44e720a6c8SUlf Hansson /*
45e720a6c8SUlf Hansson  * The CPU any Trusted OS is resident on. The trusted OS may reject CPU_OFF
46e720a6c8SUlf Hansson  * calls to its resident CPU, so we must avoid issuing those. We never migrate
47e720a6c8SUlf Hansson  * a Trusted OS even if it claims to be capable of migration -- doing so will
48e720a6c8SUlf Hansson  * require cooperation with a Trusted OS driver.
49e720a6c8SUlf Hansson  */
50e720a6c8SUlf Hansson static int resident_cpu = -1;
51f2ae9706SSudeep Holla struct psci_operations psci_ops;
52f2ae9706SSudeep Holla static enum arm_smccc_conduit psci_conduit = SMCCC_CONDUIT_NONE;
53e720a6c8SUlf Hansson 
psci_tos_resident_on(int cpu)54e720a6c8SUlf Hansson bool psci_tos_resident_on(int cpu)
55e720a6c8SUlf Hansson {
56e720a6c8SUlf Hansson 	return cpu == resident_cpu;
57e720a6c8SUlf Hansson }
58e720a6c8SUlf Hansson 
59e720a6c8SUlf Hansson typedef unsigned long (psci_fn)(unsigned long, unsigned long,
60e720a6c8SUlf Hansson 				unsigned long, unsigned long);
61e720a6c8SUlf Hansson static psci_fn *invoke_psci_fn;
62e720a6c8SUlf Hansson 
6382ac62d1SDavid Brazdil static struct psci_0_1_function_ids psci_0_1_function_ids;
64e720a6c8SUlf Hansson 
get_psci_0_1_function_ids(void)656df3e144SDavid Brazdil struct psci_0_1_function_ids get_psci_0_1_function_ids(void)
666df3e144SDavid Brazdil {
676df3e144SDavid Brazdil 	return psci_0_1_function_ids;
686df3e144SDavid Brazdil }
696df3e144SDavid Brazdil 
70e720a6c8SUlf Hansson #define PSCI_0_2_POWER_STATE_MASK		\
71e720a6c8SUlf Hansson 				(PSCI_0_2_POWER_STATE_ID_MASK | \
72e720a6c8SUlf Hansson 				PSCI_0_2_POWER_STATE_TYPE_MASK | \
73e720a6c8SUlf Hansson 				PSCI_0_2_POWER_STATE_AFFL_MASK)
74e720a6c8SUlf Hansson 
75e720a6c8SUlf Hansson #define PSCI_1_0_EXT_POWER_STATE_MASK		\
76e720a6c8SUlf Hansson 				(PSCI_1_0_EXT_POWER_STATE_ID_MASK | \
77e720a6c8SUlf Hansson 				PSCI_1_0_EXT_POWER_STATE_TYPE_MASK)
78e720a6c8SUlf Hansson 
79e720a6c8SUlf Hansson static u32 psci_cpu_suspend_feature;
804302e381SSudeep Holla static bool psci_system_reset2_supported;
81e720a6c8SUlf Hansson 
psci_has_ext_power_state(void)82e720a6c8SUlf Hansson static inline bool psci_has_ext_power_state(void)
83e720a6c8SUlf Hansson {
84e720a6c8SUlf Hansson 	return psci_cpu_suspend_feature &
85e720a6c8SUlf Hansson 				PSCI_1_0_FEATURES_CPU_SUSPEND_PF_MASK;
86e720a6c8SUlf Hansson }
87e720a6c8SUlf Hansson 
psci_has_osi_support(void)8849fdcd75SUlf Hansson bool psci_has_osi_support(void)
8960dd1eadSUlf Hansson {
9060dd1eadSUlf Hansson 	return psci_cpu_suspend_feature & PSCI_1_0_OS_INITIATED;
9160dd1eadSUlf Hansson }
9260dd1eadSUlf Hansson 
psci_power_state_loses_context(u32 state)93e720a6c8SUlf Hansson static inline bool psci_power_state_loses_context(u32 state)
94e720a6c8SUlf Hansson {
95e720a6c8SUlf Hansson 	const u32 mask = psci_has_ext_power_state() ?
96e720a6c8SUlf Hansson 					PSCI_1_0_EXT_POWER_STATE_TYPE_MASK :
97e720a6c8SUlf Hansson 					PSCI_0_2_POWER_STATE_TYPE_MASK;
98e720a6c8SUlf Hansson 
99e720a6c8SUlf Hansson 	return state & mask;
100e720a6c8SUlf Hansson }
101e720a6c8SUlf Hansson 
psci_power_state_is_valid(u32 state)1029ffeb6d0SLorenzo Pieralisi bool psci_power_state_is_valid(u32 state)
103e720a6c8SUlf Hansson {
104e720a6c8SUlf Hansson 	const u32 valid_mask = psci_has_ext_power_state() ?
105e720a6c8SUlf Hansson 			       PSCI_1_0_EXT_POWER_STATE_MASK :
106e720a6c8SUlf Hansson 			       PSCI_0_2_POWER_STATE_MASK;
107e720a6c8SUlf Hansson 
108e720a6c8SUlf Hansson 	return !(state & ~valid_mask);
109e720a6c8SUlf Hansson }
110e720a6c8SUlf Hansson 
111393e2ea3SMark Rutland static __always_inline unsigned long
__invoke_psci_fn_hvc(unsigned long function_id,unsigned long arg0,unsigned long arg1,unsigned long arg2)112393e2ea3SMark Rutland __invoke_psci_fn_hvc(unsigned long function_id,
113e720a6c8SUlf Hansson 		     unsigned long arg0, unsigned long arg1,
114e720a6c8SUlf Hansson 		     unsigned long arg2)
115e720a6c8SUlf Hansson {
116e720a6c8SUlf Hansson 	struct arm_smccc_res res;
117e720a6c8SUlf Hansson 
118e720a6c8SUlf Hansson 	arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
119e720a6c8SUlf Hansson 	return res.a0;
120e720a6c8SUlf Hansson }
121e720a6c8SUlf Hansson 
122393e2ea3SMark Rutland static __always_inline unsigned long
__invoke_psci_fn_smc(unsigned long function_id,unsigned long arg0,unsigned long arg1,unsigned long arg2)123393e2ea3SMark Rutland __invoke_psci_fn_smc(unsigned long function_id,
124e720a6c8SUlf Hansson 		     unsigned long arg0, unsigned long arg1,
125e720a6c8SUlf Hansson 		     unsigned long arg2)
126e720a6c8SUlf Hansson {
127e720a6c8SUlf Hansson 	struct arm_smccc_res res;
128e720a6c8SUlf Hansson 
129e720a6c8SUlf Hansson 	arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
130e720a6c8SUlf Hansson 	return res.a0;
131e720a6c8SUlf Hansson }
132e720a6c8SUlf Hansson 
psci_to_linux_errno(int errno)133393e2ea3SMark Rutland static __always_inline int psci_to_linux_errno(int errno)
134e720a6c8SUlf Hansson {
135e720a6c8SUlf Hansson 	switch (errno) {
136e720a6c8SUlf Hansson 	case PSCI_RET_SUCCESS:
137e720a6c8SUlf Hansson 		return 0;
138e720a6c8SUlf Hansson 	case PSCI_RET_NOT_SUPPORTED:
139e720a6c8SUlf Hansson 		return -EOPNOTSUPP;
140e720a6c8SUlf Hansson 	case PSCI_RET_INVALID_PARAMS:
141e720a6c8SUlf Hansson 	case PSCI_RET_INVALID_ADDRESS:
142e720a6c8SUlf Hansson 		return -EINVAL;
143e720a6c8SUlf Hansson 	case PSCI_RET_DENIED:
144e720a6c8SUlf Hansson 		return -EPERM;
1459b924f4fSYang Li 	}
146e720a6c8SUlf Hansson 
147e720a6c8SUlf Hansson 	return -EINVAL;
148e720a6c8SUlf Hansson }
149e720a6c8SUlf Hansson 
psci_0_1_get_version(void)150e6dd9d89SDavid Brazdil static u32 psci_0_1_get_version(void)
151e6dd9d89SDavid Brazdil {
152e6dd9d89SDavid Brazdil 	return PSCI_VERSION(0, 1);
153e6dd9d89SDavid Brazdil }
154e6dd9d89SDavid Brazdil 
psci_0_2_get_version(void)155e6dd9d89SDavid Brazdil static u32 psci_0_2_get_version(void)
156e720a6c8SUlf Hansson {
157e720a6c8SUlf Hansson 	return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
158e720a6c8SUlf Hansson }
159e720a6c8SUlf Hansson 
psci_set_osi_mode(bool enable)16010942019SUlf Hansson int psci_set_osi_mode(bool enable)
16149fdcd75SUlf Hansson {
16210942019SUlf Hansson 	unsigned long suspend_mode;
16349fdcd75SUlf Hansson 	int err;
16449fdcd75SUlf Hansson 
16510942019SUlf Hansson 	suspend_mode = enable ? PSCI_1_0_SUSPEND_MODE_OSI :
16610942019SUlf Hansson 			PSCI_1_0_SUSPEND_MODE_PC;
16710942019SUlf Hansson 
16810942019SUlf Hansson 	err = invoke_psci_fn(PSCI_1_0_FN_SET_SUSPEND_MODE, suspend_mode, 0, 0);
169998fcd00SDmitry Baryshkov 	if (err < 0)
17043235168SJohan Hovold 		pr_info(FW_BUG "failed to set %s mode: %d\n",
17143235168SJohan Hovold 				enable ? "OSI" : "PC", err);
17249fdcd75SUlf Hansson 	return psci_to_linux_errno(err);
17349fdcd75SUlf Hansson }
17449fdcd75SUlf Hansson 
175393e2ea3SMark Rutland static __always_inline int
__psci_cpu_suspend(u32 fn,u32 state,unsigned long entry_point)176393e2ea3SMark Rutland __psci_cpu_suspend(u32 fn, u32 state, unsigned long entry_point)
177e720a6c8SUlf Hansson {
178e720a6c8SUlf Hansson 	int err;
179e720a6c8SUlf Hansson 
180e720a6c8SUlf Hansson 	err = invoke_psci_fn(fn, state, entry_point, 0);
181e720a6c8SUlf Hansson 	return psci_to_linux_errno(err);
182e720a6c8SUlf Hansson }
183e720a6c8SUlf Hansson 
184393e2ea3SMark Rutland static __always_inline int
psci_0_1_cpu_suspend(u32 state,unsigned long entry_point)185393e2ea3SMark Rutland psci_0_1_cpu_suspend(u32 state, unsigned long entry_point)
1860bc7474fSDavid Brazdil {
18782ac62d1SDavid Brazdil 	return __psci_cpu_suspend(psci_0_1_function_ids.cpu_suspend,
1880bc7474fSDavid Brazdil 				  state, entry_point);
1890bc7474fSDavid Brazdil }
1900bc7474fSDavid Brazdil 
191393e2ea3SMark Rutland static __always_inline int
psci_0_2_cpu_suspend(u32 state,unsigned long entry_point)192393e2ea3SMark Rutland psci_0_2_cpu_suspend(u32 state, unsigned long entry_point)
1930bc7474fSDavid Brazdil {
1940bc7474fSDavid Brazdil 	return __psci_cpu_suspend(PSCI_FN_NATIVE(0_2, CPU_SUSPEND),
1950bc7474fSDavid Brazdil 				  state, entry_point);
1960bc7474fSDavid Brazdil }
1970bc7474fSDavid Brazdil 
__psci_cpu_off(u32 fn,u32 state)1980bc7474fSDavid Brazdil static int __psci_cpu_off(u32 fn, u32 state)
199e720a6c8SUlf Hansson {
200e720a6c8SUlf Hansson 	int err;
201e720a6c8SUlf Hansson 
202e720a6c8SUlf Hansson 	err = invoke_psci_fn(fn, state, 0, 0);
203e720a6c8SUlf Hansson 	return psci_to_linux_errno(err);
204e720a6c8SUlf Hansson }
205e720a6c8SUlf Hansson 
psci_0_1_cpu_off(u32 state)2060bc7474fSDavid Brazdil static int psci_0_1_cpu_off(u32 state)
2070bc7474fSDavid Brazdil {
20882ac62d1SDavid Brazdil 	return __psci_cpu_off(psci_0_1_function_ids.cpu_off, state);
2090bc7474fSDavid Brazdil }
2100bc7474fSDavid Brazdil 
psci_0_2_cpu_off(u32 state)2110bc7474fSDavid Brazdil static int psci_0_2_cpu_off(u32 state)
2120bc7474fSDavid Brazdil {
2130bc7474fSDavid Brazdil 	return __psci_cpu_off(PSCI_0_2_FN_CPU_OFF, state);
2140bc7474fSDavid Brazdil }
2150bc7474fSDavid Brazdil 
__psci_cpu_on(u32 fn,unsigned long cpuid,unsigned long entry_point)2160bc7474fSDavid Brazdil static int __psci_cpu_on(u32 fn, unsigned long cpuid, unsigned long entry_point)
217e720a6c8SUlf Hansson {
218e720a6c8SUlf Hansson 	int err;
219e720a6c8SUlf Hansson 
220e720a6c8SUlf Hansson 	err = invoke_psci_fn(fn, cpuid, entry_point, 0);
221e720a6c8SUlf Hansson 	return psci_to_linux_errno(err);
222e720a6c8SUlf Hansson }
223e720a6c8SUlf Hansson 
psci_0_1_cpu_on(unsigned long cpuid,unsigned long entry_point)2240bc7474fSDavid Brazdil static int psci_0_1_cpu_on(unsigned long cpuid, unsigned long entry_point)
2250bc7474fSDavid Brazdil {
22682ac62d1SDavid Brazdil 	return __psci_cpu_on(psci_0_1_function_ids.cpu_on, cpuid, entry_point);
2270bc7474fSDavid Brazdil }
2280bc7474fSDavid Brazdil 
psci_0_2_cpu_on(unsigned long cpuid,unsigned long entry_point)2290bc7474fSDavid Brazdil static int psci_0_2_cpu_on(unsigned long cpuid, unsigned long entry_point)
2300bc7474fSDavid Brazdil {
2310bc7474fSDavid Brazdil 	return __psci_cpu_on(PSCI_FN_NATIVE(0_2, CPU_ON), cpuid, entry_point);
2320bc7474fSDavid Brazdil }
2330bc7474fSDavid Brazdil 
__psci_migrate(u32 fn,unsigned long cpuid)2340bc7474fSDavid Brazdil static int __psci_migrate(u32 fn, unsigned long cpuid)
235e720a6c8SUlf Hansson {
236e720a6c8SUlf Hansson 	int err;
237e720a6c8SUlf Hansson 
238e720a6c8SUlf Hansson 	err = invoke_psci_fn(fn, cpuid, 0, 0);
239e720a6c8SUlf Hansson 	return psci_to_linux_errno(err);
240e720a6c8SUlf Hansson }
241e720a6c8SUlf Hansson 
psci_0_1_migrate(unsigned long cpuid)2420bc7474fSDavid Brazdil static int psci_0_1_migrate(unsigned long cpuid)
2430bc7474fSDavid Brazdil {
24482ac62d1SDavid Brazdil 	return __psci_migrate(psci_0_1_function_ids.migrate, cpuid);
2450bc7474fSDavid Brazdil }
2460bc7474fSDavid Brazdil 
psci_0_2_migrate(unsigned long cpuid)2470bc7474fSDavid Brazdil static int psci_0_2_migrate(unsigned long cpuid)
2480bc7474fSDavid Brazdil {
2490bc7474fSDavid Brazdil 	return __psci_migrate(PSCI_FN_NATIVE(0_2, MIGRATE), cpuid);
2500bc7474fSDavid Brazdil }
2510bc7474fSDavid Brazdil 
psci_affinity_info(unsigned long target_affinity,unsigned long lowest_affinity_level)252e720a6c8SUlf Hansson static int psci_affinity_info(unsigned long target_affinity,
253e720a6c8SUlf Hansson 		unsigned long lowest_affinity_level)
254e720a6c8SUlf Hansson {
255e720a6c8SUlf Hansson 	return invoke_psci_fn(PSCI_FN_NATIVE(0_2, AFFINITY_INFO),
256e720a6c8SUlf Hansson 			      target_affinity, lowest_affinity_level, 0);
257e720a6c8SUlf Hansson }
258e720a6c8SUlf Hansson 
psci_migrate_info_type(void)259e720a6c8SUlf Hansson static int psci_migrate_info_type(void)
260e720a6c8SUlf Hansson {
261e720a6c8SUlf Hansson 	return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0);
262e720a6c8SUlf Hansson }
263e720a6c8SUlf Hansson 
psci_migrate_info_up_cpu(void)264e720a6c8SUlf Hansson static unsigned long psci_migrate_info_up_cpu(void)
265e720a6c8SUlf Hansson {
266e720a6c8SUlf Hansson 	return invoke_psci_fn(PSCI_FN_NATIVE(0_2, MIGRATE_INFO_UP_CPU),
267e720a6c8SUlf Hansson 			      0, 0, 0);
268e720a6c8SUlf Hansson }
269e720a6c8SUlf Hansson 
set_conduit(enum arm_smccc_conduit conduit)270a5520eacSMark Rutland static void set_conduit(enum arm_smccc_conduit conduit)
271e720a6c8SUlf Hansson {
272e720a6c8SUlf Hansson 	switch (conduit) {
273a5520eacSMark Rutland 	case SMCCC_CONDUIT_HVC:
274e720a6c8SUlf Hansson 		invoke_psci_fn = __invoke_psci_fn_hvc;
275e720a6c8SUlf Hansson 		break;
276a5520eacSMark Rutland 	case SMCCC_CONDUIT_SMC:
277e720a6c8SUlf Hansson 		invoke_psci_fn = __invoke_psci_fn_smc;
278e720a6c8SUlf Hansson 		break;
279e720a6c8SUlf Hansson 	default:
280e720a6c8SUlf Hansson 		WARN(1, "Unexpected PSCI conduit %d\n", conduit);
281e720a6c8SUlf Hansson 	}
282e720a6c8SUlf Hansson 
283f2ae9706SSudeep Holla 	psci_conduit = conduit;
284e720a6c8SUlf Hansson }
285e720a6c8SUlf Hansson 
get_set_conduit_method(const struct device_node * np)28644f665b6SSami Tolvanen static int get_set_conduit_method(const struct device_node *np)
287e720a6c8SUlf Hansson {
288e720a6c8SUlf Hansson 	const char *method;
289e720a6c8SUlf Hansson 
290e720a6c8SUlf Hansson 	pr_info("probing for conduit method from DT.\n");
291e720a6c8SUlf Hansson 
292e720a6c8SUlf Hansson 	if (of_property_read_string(np, "method", &method)) {
293e720a6c8SUlf Hansson 		pr_warn("missing \"method\" property\n");
294e720a6c8SUlf Hansson 		return -ENXIO;
295e720a6c8SUlf Hansson 	}
296e720a6c8SUlf Hansson 
297e720a6c8SUlf Hansson 	if (!strcmp("hvc", method)) {
298a5520eacSMark Rutland 		set_conduit(SMCCC_CONDUIT_HVC);
299e720a6c8SUlf Hansson 	} else if (!strcmp("smc", method)) {
300a5520eacSMark Rutland 		set_conduit(SMCCC_CONDUIT_SMC);
301e720a6c8SUlf Hansson 	} else {
302e720a6c8SUlf Hansson 		pr_warn("invalid \"method\" property: %s\n", method);
303e720a6c8SUlf Hansson 		return -EINVAL;
304e720a6c8SUlf Hansson 	}
305e720a6c8SUlf Hansson 	return 0;
306e720a6c8SUlf Hansson }
307e720a6c8SUlf Hansson 
psci_sys_reset(struct notifier_block * nb,unsigned long action,void * data)3082bb565bbSGuenter Roeck static int psci_sys_reset(struct notifier_block *nb, unsigned long action,
3092bb565bbSGuenter Roeck 			  void *data)
310e720a6c8SUlf Hansson {
3114302e381SSudeep Holla 	if ((reboot_mode == REBOOT_WARM || reboot_mode == REBOOT_SOFT) &&
3124302e381SSudeep Holla 	    psci_system_reset2_supported) {
3134302e381SSudeep Holla 		/*
3144302e381SSudeep Holla 		 * reset_type[31] = 0 (architectural)
3154302e381SSudeep Holla 		 * reset_type[30:0] = 0 (SYSTEM_WARM_RESET)
3164302e381SSudeep Holla 		 * cookie = 0 (ignored by the implementation)
3174302e381SSudeep Holla 		 */
3184302e381SSudeep Holla 		invoke_psci_fn(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2), 0, 0, 0);
3194302e381SSudeep Holla 	} else {
320e720a6c8SUlf Hansson 		invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
321e720a6c8SUlf Hansson 	}
3222bb565bbSGuenter Roeck 
3232bb565bbSGuenter Roeck 	return NOTIFY_DONE;
3244302e381SSudeep Holla }
325e720a6c8SUlf Hansson 
3262bb565bbSGuenter Roeck static struct notifier_block psci_sys_reset_nb = {
3272bb565bbSGuenter Roeck 	.notifier_call = psci_sys_reset,
3282bb565bbSGuenter Roeck 	.priority = 129,
3292bb565bbSGuenter Roeck };
3302bb565bbSGuenter Roeck 
psci_sys_poweroff(void)331e720a6c8SUlf Hansson static void psci_sys_poweroff(void)
332e720a6c8SUlf Hansson {
333e720a6c8SUlf Hansson 	invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
334e720a6c8SUlf Hansson }
335e720a6c8SUlf Hansson 
psci_features(u32 psci_func_id)3363137f2e6SDmitry Baryshkov static int psci_features(u32 psci_func_id)
337e720a6c8SUlf Hansson {
338e720a6c8SUlf Hansson 	return invoke_psci_fn(PSCI_1_0_FN_PSCI_FEATURES,
339e720a6c8SUlf Hansson 			      psci_func_id, 0, 0);
340e720a6c8SUlf Hansson }
341e720a6c8SUlf Hansson 
3423137f2e6SDmitry Baryshkov #ifdef CONFIG_DEBUG_FS
3433137f2e6SDmitry Baryshkov 
3443137f2e6SDmitry Baryshkov #define PSCI_ID(ver, _name) \
3453137f2e6SDmitry Baryshkov 	{ .fn = PSCI_##ver##_FN_##_name, .name = #_name, }
3463137f2e6SDmitry Baryshkov #define PSCI_ID_NATIVE(ver, _name) \
3473137f2e6SDmitry Baryshkov 	{ .fn = PSCI_FN_NATIVE(ver, _name), .name = #_name, }
3483137f2e6SDmitry Baryshkov 
3493137f2e6SDmitry Baryshkov /* A table of all optional functions */
3503137f2e6SDmitry Baryshkov static const struct {
3513137f2e6SDmitry Baryshkov 	u32 fn;
3523137f2e6SDmitry Baryshkov 	const char *name;
3533137f2e6SDmitry Baryshkov } psci_fn_ids[] = {
3543137f2e6SDmitry Baryshkov 	PSCI_ID_NATIVE(0_2, MIGRATE),
3553137f2e6SDmitry Baryshkov 	PSCI_ID(0_2, MIGRATE_INFO_TYPE),
3563137f2e6SDmitry Baryshkov 	PSCI_ID_NATIVE(0_2, MIGRATE_INFO_UP_CPU),
3573137f2e6SDmitry Baryshkov 	PSCI_ID(1_0, CPU_FREEZE),
3583137f2e6SDmitry Baryshkov 	PSCI_ID_NATIVE(1_0, CPU_DEFAULT_SUSPEND),
3593137f2e6SDmitry Baryshkov 	PSCI_ID_NATIVE(1_0, NODE_HW_STATE),
3603137f2e6SDmitry Baryshkov 	PSCI_ID_NATIVE(1_0, SYSTEM_SUSPEND),
3613137f2e6SDmitry Baryshkov 	PSCI_ID(1_0, SET_SUSPEND_MODE),
3623137f2e6SDmitry Baryshkov 	PSCI_ID_NATIVE(1_0, STAT_RESIDENCY),
3633137f2e6SDmitry Baryshkov 	PSCI_ID_NATIVE(1_0, STAT_COUNT),
3643137f2e6SDmitry Baryshkov 	PSCI_ID_NATIVE(1_1, SYSTEM_RESET2),
3653137f2e6SDmitry Baryshkov 	PSCI_ID(1_1, MEM_PROTECT),
3663137f2e6SDmitry Baryshkov 	PSCI_ID_NATIVE(1_1, MEM_PROTECT_CHECK_RANGE),
3673137f2e6SDmitry Baryshkov };
3683137f2e6SDmitry Baryshkov 
psci_debugfs_read(struct seq_file * s,void * data)3693137f2e6SDmitry Baryshkov static int psci_debugfs_read(struct seq_file *s, void *data)
3703137f2e6SDmitry Baryshkov {
3713137f2e6SDmitry Baryshkov 	int feature, type, i;
3723137f2e6SDmitry Baryshkov 	u32 ver;
3733137f2e6SDmitry Baryshkov 
3743137f2e6SDmitry Baryshkov 	ver = psci_ops.get_version();
3753137f2e6SDmitry Baryshkov 	seq_printf(s, "PSCIv%d.%d\n",
3763137f2e6SDmitry Baryshkov 		   PSCI_VERSION_MAJOR(ver),
3773137f2e6SDmitry Baryshkov 		   PSCI_VERSION_MINOR(ver));
3783137f2e6SDmitry Baryshkov 
3793137f2e6SDmitry Baryshkov 	/* PSCI_FEATURES is available only starting from 1.0 */
3803137f2e6SDmitry Baryshkov 	if (PSCI_VERSION_MAJOR(ver) < 1)
3813137f2e6SDmitry Baryshkov 		return 0;
3823137f2e6SDmitry Baryshkov 
3833137f2e6SDmitry Baryshkov 	feature = psci_features(ARM_SMCCC_VERSION_FUNC_ID);
3843137f2e6SDmitry Baryshkov 	if (feature != PSCI_RET_NOT_SUPPORTED) {
3853137f2e6SDmitry Baryshkov 		ver = invoke_psci_fn(ARM_SMCCC_VERSION_FUNC_ID, 0, 0, 0);
3863137f2e6SDmitry Baryshkov 		seq_printf(s, "SMC Calling Convention v%d.%d\n",
3873137f2e6SDmitry Baryshkov 			   PSCI_VERSION_MAJOR(ver),
3883137f2e6SDmitry Baryshkov 			   PSCI_VERSION_MINOR(ver));
3893137f2e6SDmitry Baryshkov 	} else {
3903137f2e6SDmitry Baryshkov 		seq_puts(s, "SMC Calling Convention v1.0 is assumed\n");
3913137f2e6SDmitry Baryshkov 	}
3923137f2e6SDmitry Baryshkov 
3933137f2e6SDmitry Baryshkov 	feature = psci_features(PSCI_FN_NATIVE(0_2, CPU_SUSPEND));
3943137f2e6SDmitry Baryshkov 	if (feature < 0) {
3953137f2e6SDmitry Baryshkov 		seq_printf(s, "PSCI_FEATURES(CPU_SUSPEND) error (%d)\n", feature);
3963137f2e6SDmitry Baryshkov 	} else {
3973137f2e6SDmitry Baryshkov 		seq_printf(s, "OSI is %ssupported\n",
3983137f2e6SDmitry Baryshkov 			   (feature & BIT(0)) ? "" : "not ");
3993137f2e6SDmitry Baryshkov 		seq_printf(s, "%s StateID format is used\n",
4003137f2e6SDmitry Baryshkov 			   (feature & BIT(1)) ? "Extended" : "Original");
4013137f2e6SDmitry Baryshkov 	}
4023137f2e6SDmitry Baryshkov 
4033137f2e6SDmitry Baryshkov 	type = psci_ops.migrate_info_type();
4043137f2e6SDmitry Baryshkov 	if (type == PSCI_0_2_TOS_UP_MIGRATE ||
4053137f2e6SDmitry Baryshkov 	    type == PSCI_0_2_TOS_UP_NO_MIGRATE) {
4063137f2e6SDmitry Baryshkov 		unsigned long cpuid;
4073137f2e6SDmitry Baryshkov 
4083137f2e6SDmitry Baryshkov 		seq_printf(s, "Trusted OS %smigrate capable\n",
4093137f2e6SDmitry Baryshkov 			   type == PSCI_0_2_TOS_UP_NO_MIGRATE ? "not " : "");
4103137f2e6SDmitry Baryshkov 		cpuid = psci_migrate_info_up_cpu();
4113137f2e6SDmitry Baryshkov 		seq_printf(s, "Trusted OS resident on physical CPU 0x%lx (#%d)\n",
4123137f2e6SDmitry Baryshkov 			   cpuid, resident_cpu);
4133137f2e6SDmitry Baryshkov 	} else if (type == PSCI_0_2_TOS_MP) {
4143137f2e6SDmitry Baryshkov 		seq_puts(s, "Trusted OS migration not required\n");
4153137f2e6SDmitry Baryshkov 	} else {
4163137f2e6SDmitry Baryshkov 		if (type != PSCI_RET_NOT_SUPPORTED)
4173137f2e6SDmitry Baryshkov 			seq_printf(s, "MIGRATE_INFO_TYPE returned unknown type (%d)\n", type);
4183137f2e6SDmitry Baryshkov 	}
4193137f2e6SDmitry Baryshkov 
4203137f2e6SDmitry Baryshkov 	for (i = 0; i < ARRAY_SIZE(psci_fn_ids); i++) {
4213137f2e6SDmitry Baryshkov 		feature = psci_features(psci_fn_ids[i].fn);
4223137f2e6SDmitry Baryshkov 		if (feature == PSCI_RET_NOT_SUPPORTED)
4233137f2e6SDmitry Baryshkov 			continue;
4243137f2e6SDmitry Baryshkov 		if (feature < 0)
4253137f2e6SDmitry Baryshkov 			seq_printf(s, "PSCI_FEATURES(%s) error (%d)\n",
4263137f2e6SDmitry Baryshkov 				   psci_fn_ids[i].name, feature);
4273137f2e6SDmitry Baryshkov 		else
4283137f2e6SDmitry Baryshkov 			seq_printf(s, "%s is supported\n", psci_fn_ids[i].name);
4293137f2e6SDmitry Baryshkov 	}
4303137f2e6SDmitry Baryshkov 
4313137f2e6SDmitry Baryshkov 	return 0;
4323137f2e6SDmitry Baryshkov }
4333137f2e6SDmitry Baryshkov 
psci_debugfs_open(struct inode * inode,struct file * f)4343137f2e6SDmitry Baryshkov static int psci_debugfs_open(struct inode *inode, struct file *f)
4353137f2e6SDmitry Baryshkov {
4363137f2e6SDmitry Baryshkov 	return single_open(f, psci_debugfs_read, NULL);
4373137f2e6SDmitry Baryshkov }
4383137f2e6SDmitry Baryshkov 
4393137f2e6SDmitry Baryshkov static const struct file_operations psci_debugfs_ops = {
4403137f2e6SDmitry Baryshkov 	.owner = THIS_MODULE,
4413137f2e6SDmitry Baryshkov 	.open = psci_debugfs_open,
4423137f2e6SDmitry Baryshkov 	.release = single_release,
4433137f2e6SDmitry Baryshkov 	.read = seq_read,
4443137f2e6SDmitry Baryshkov 	.llseek = seq_lseek
4453137f2e6SDmitry Baryshkov };
4463137f2e6SDmitry Baryshkov 
psci_debugfs_init(void)4473137f2e6SDmitry Baryshkov static int __init psci_debugfs_init(void)
4483137f2e6SDmitry Baryshkov {
449cef13929SMarc Zyngier 	if (!invoke_psci_fn || !psci_ops.get_version)
450cef13929SMarc Zyngier 		return 0;
451cef13929SMarc Zyngier 
4523137f2e6SDmitry Baryshkov 	return PTR_ERR_OR_ZERO(debugfs_create_file("psci", 0444, NULL, NULL,
4533137f2e6SDmitry Baryshkov 						   &psci_debugfs_ops));
4543137f2e6SDmitry Baryshkov }
late_initcall(psci_debugfs_init)4553137f2e6SDmitry Baryshkov late_initcall(psci_debugfs_init)
4563137f2e6SDmitry Baryshkov #endif
4573137f2e6SDmitry Baryshkov 
458e720a6c8SUlf Hansson #ifdef CONFIG_CPU_IDLE
459393e2ea3SMark Rutland static noinstr int psci_suspend_finisher(unsigned long state)
4600865d20cSUlf Hansson {
4619ffeb6d0SLorenzo Pieralisi 	u32 power_state = state;
462393e2ea3SMark Rutland 	phys_addr_t pa_cpu_resume;
463393e2ea3SMark Rutland 
464393e2ea3SMark Rutland 	pa_cpu_resume = __pa_symbol_nodebug((unsigned long)cpu_resume);
4650865d20cSUlf Hansson 
466167d0da2SSami Tolvanen 	return psci_ops.cpu_suspend(power_state, pa_cpu_resume);
4670865d20cSUlf Hansson }
4680865d20cSUlf Hansson 
psci_cpu_suspend_enter(u32 state)4699ffeb6d0SLorenzo Pieralisi int psci_cpu_suspend_enter(u32 state)
470e720a6c8SUlf Hansson {
471e720a6c8SUlf Hansson 	int ret;
472e720a6c8SUlf Hansson 
473c9223b61SMarc Zyngier 	if (!psci_power_state_loses_context(state)) {
474c9223b61SMarc Zyngier 		struct arm_cpuidle_irq_context context;
475c9223b61SMarc Zyngier 
47619235e47SPeter Zijlstra 		ct_cpuidle_enter();
477c9223b61SMarc Zyngier 		arm_cpuidle_save_irq_context(&context);
4789ffeb6d0SLorenzo Pieralisi 		ret = psci_ops.cpu_suspend(state, 0);
479c9223b61SMarc Zyngier 		arm_cpuidle_restore_irq_context(&context);
48019235e47SPeter Zijlstra 		ct_cpuidle_exit();
481c9223b61SMarc Zyngier 	} else {
48219235e47SPeter Zijlstra 		/*
48319235e47SPeter Zijlstra 		 * ARM64 cpu_suspend() wants to do ct_cpuidle_*() itself.
48419235e47SPeter Zijlstra 		 */
48519235e47SPeter Zijlstra 		if (!IS_ENABLED(CONFIG_ARM64))
48619235e47SPeter Zijlstra 			ct_cpuidle_enter();
48719235e47SPeter Zijlstra 
4889ffeb6d0SLorenzo Pieralisi 		ret = cpu_suspend(state, psci_suspend_finisher);
48919235e47SPeter Zijlstra 
49019235e47SPeter Zijlstra 		if (!IS_ENABLED(CONFIG_ARM64))
49119235e47SPeter Zijlstra 			ct_cpuidle_exit();
492c9223b61SMarc Zyngier 	}
493e720a6c8SUlf Hansson 
494e720a6c8SUlf Hansson 	return ret;
495e720a6c8SUlf Hansson }
496e720a6c8SUlf Hansson #endif
497e720a6c8SUlf Hansson 
psci_system_suspend(unsigned long unused)498e720a6c8SUlf Hansson static int psci_system_suspend(unsigned long unused)
499e720a6c8SUlf Hansson {
500*e7c3696dSSudeep Holla 	int err;
501607289a7SSami Tolvanen 	phys_addr_t pa_cpu_resume = __pa_symbol(cpu_resume);
502167d0da2SSami Tolvanen 
503*e7c3696dSSudeep Holla 	err = invoke_psci_fn(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND),
504167d0da2SSami Tolvanen 			      pa_cpu_resume, 0, 0);
505*e7c3696dSSudeep Holla 	return psci_to_linux_errno(err);
506e720a6c8SUlf Hansson }
507e720a6c8SUlf Hansson 
psci_system_suspend_enter(suspend_state_t state)508e720a6c8SUlf Hansson static int psci_system_suspend_enter(suspend_state_t state)
509e720a6c8SUlf Hansson {
510e720a6c8SUlf Hansson 	return cpu_suspend(0, psci_system_suspend);
511e720a6c8SUlf Hansson }
512e720a6c8SUlf Hansson 
513e720a6c8SUlf Hansson static const struct platform_suspend_ops psci_suspend_ops = {
514e720a6c8SUlf Hansson 	.valid          = suspend_valid_only_mem,
515e720a6c8SUlf Hansson 	.enter          = psci_system_suspend_enter,
516e720a6c8SUlf Hansson };
517e720a6c8SUlf Hansson 
psci_init_system_reset2(void)5184302e381SSudeep Holla static void __init psci_init_system_reset2(void)
5194302e381SSudeep Holla {
5204302e381SSudeep Holla 	int ret;
5214302e381SSudeep Holla 
5224302e381SSudeep Holla 	ret = psci_features(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2));
5234302e381SSudeep Holla 
5244302e381SSudeep Holla 	if (ret != PSCI_RET_NOT_SUPPORTED)
5254302e381SSudeep Holla 		psci_system_reset2_supported = true;
5264302e381SSudeep Holla }
5274302e381SSudeep Holla 
psci_init_system_suspend(void)528e720a6c8SUlf Hansson static void __init psci_init_system_suspend(void)
529e720a6c8SUlf Hansson {
530e720a6c8SUlf Hansson 	int ret;
531e720a6c8SUlf Hansson 
532e720a6c8SUlf Hansson 	if (!IS_ENABLED(CONFIG_SUSPEND))
533e720a6c8SUlf Hansson 		return;
534e720a6c8SUlf Hansson 
535e720a6c8SUlf Hansson 	ret = psci_features(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND));
536e720a6c8SUlf Hansson 
537e720a6c8SUlf Hansson 	if (ret != PSCI_RET_NOT_SUPPORTED)
538e720a6c8SUlf Hansson 		suspend_set_ops(&psci_suspend_ops);
539e720a6c8SUlf Hansson }
540e720a6c8SUlf Hansson 
psci_init_cpu_suspend(void)541e720a6c8SUlf Hansson static void __init psci_init_cpu_suspend(void)
542e720a6c8SUlf Hansson {
5430bc7474fSDavid Brazdil 	int feature = psci_features(PSCI_FN_NATIVE(0_2, CPU_SUSPEND));
544e720a6c8SUlf Hansson 
545e720a6c8SUlf Hansson 	if (feature != PSCI_RET_NOT_SUPPORTED)
546e720a6c8SUlf Hansson 		psci_cpu_suspend_feature = feature;
547e720a6c8SUlf Hansson }
548e720a6c8SUlf Hansson 
549e720a6c8SUlf Hansson /*
550e720a6c8SUlf Hansson  * Detect the presence of a resident Trusted OS which may cause CPU_OFF to
551e720a6c8SUlf Hansson  * return DENIED (which would be fatal).
552e720a6c8SUlf Hansson  */
psci_init_migrate(void)553e720a6c8SUlf Hansson static void __init psci_init_migrate(void)
554e720a6c8SUlf Hansson {
555e720a6c8SUlf Hansson 	unsigned long cpuid;
556e720a6c8SUlf Hansson 	int type, cpu = -1;
557e720a6c8SUlf Hansson 
558e720a6c8SUlf Hansson 	type = psci_ops.migrate_info_type();
559e720a6c8SUlf Hansson 
560e720a6c8SUlf Hansson 	if (type == PSCI_0_2_TOS_MP) {
561e720a6c8SUlf Hansson 		pr_info("Trusted OS migration not required\n");
562e720a6c8SUlf Hansson 		return;
563e720a6c8SUlf Hansson 	}
564e720a6c8SUlf Hansson 
565e720a6c8SUlf Hansson 	if (type == PSCI_RET_NOT_SUPPORTED) {
566e720a6c8SUlf Hansson 		pr_info("MIGRATE_INFO_TYPE not supported.\n");
567e720a6c8SUlf Hansson 		return;
568e720a6c8SUlf Hansson 	}
569e720a6c8SUlf Hansson 
570e720a6c8SUlf Hansson 	if (type != PSCI_0_2_TOS_UP_MIGRATE &&
571e720a6c8SUlf Hansson 	    type != PSCI_0_2_TOS_UP_NO_MIGRATE) {
572e720a6c8SUlf Hansson 		pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type);
573e720a6c8SUlf Hansson 		return;
574e720a6c8SUlf Hansson 	}
575e720a6c8SUlf Hansson 
576e720a6c8SUlf Hansson 	cpuid = psci_migrate_info_up_cpu();
577e720a6c8SUlf Hansson 	if (cpuid & ~MPIDR_HWID_BITMASK) {
578e720a6c8SUlf Hansson 		pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n",
579e720a6c8SUlf Hansson 			cpuid);
580e720a6c8SUlf Hansson 		return;
581e720a6c8SUlf Hansson 	}
582e720a6c8SUlf Hansson 
583e720a6c8SUlf Hansson 	cpu = get_logical_index(cpuid);
584e720a6c8SUlf Hansson 	resident_cpu = cpu >= 0 ? cpu : -1;
585e720a6c8SUlf Hansson 
586e720a6c8SUlf Hansson 	pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid);
587e720a6c8SUlf Hansson }
588e720a6c8SUlf Hansson 
psci_init_smccc(void)589e720a6c8SUlf Hansson static void __init psci_init_smccc(void)
590e720a6c8SUlf Hansson {
591e720a6c8SUlf Hansson 	u32 ver = ARM_SMCCC_VERSION_1_0;
592e720a6c8SUlf Hansson 	int feature;
593e720a6c8SUlf Hansson 
594e720a6c8SUlf Hansson 	feature = psci_features(ARM_SMCCC_VERSION_FUNC_ID);
595e720a6c8SUlf Hansson 
596e720a6c8SUlf Hansson 	if (feature != PSCI_RET_NOT_SUPPORTED) {
597e720a6c8SUlf Hansson 		u32 ret;
598e720a6c8SUlf Hansson 		ret = invoke_psci_fn(ARM_SMCCC_VERSION_FUNC_ID, 0, 0, 0);
599ad5a57dfSSudeep Holla 		if (ret >= ARM_SMCCC_VERSION_1_1) {
600f2ae9706SSudeep Holla 			arm_smccc_version_init(ret, psci_conduit);
601e720a6c8SUlf Hansson 			ver = ret;
602e720a6c8SUlf Hansson 		}
603e720a6c8SUlf Hansson 	}
604e720a6c8SUlf Hansson 
605e720a6c8SUlf Hansson 	/*
606e720a6c8SUlf Hansson 	 * Conveniently, the SMCCC and PSCI versions are encoded the
607e720a6c8SUlf Hansson 	 * same way. No, this isn't accidental.
608e720a6c8SUlf Hansson 	 */
609e720a6c8SUlf Hansson 	pr_info("SMC Calling Convention v%d.%d\n",
610e720a6c8SUlf Hansson 		PSCI_VERSION_MAJOR(ver), PSCI_VERSION_MINOR(ver));
611e720a6c8SUlf Hansson 
612e720a6c8SUlf Hansson }
613e720a6c8SUlf Hansson 
psci_0_2_set_functions(void)614e720a6c8SUlf Hansson static void __init psci_0_2_set_functions(void)
615e720a6c8SUlf Hansson {
616e720a6c8SUlf Hansson 	pr_info("Using standard PSCI v0.2 function IDs\n");
617e720a6c8SUlf Hansson 
6180bc7474fSDavid Brazdil 	psci_ops = (struct psci_operations){
6190bc7474fSDavid Brazdil 		.get_version = psci_0_2_get_version,
6200bc7474fSDavid Brazdil 		.cpu_suspend = psci_0_2_cpu_suspend,
6210bc7474fSDavid Brazdil 		.cpu_off = psci_0_2_cpu_off,
6220bc7474fSDavid Brazdil 		.cpu_on = psci_0_2_cpu_on,
6230bc7474fSDavid Brazdil 		.migrate = psci_0_2_migrate,
6240bc7474fSDavid Brazdil 		.affinity_info = psci_affinity_info,
6250bc7474fSDavid Brazdil 		.migrate_info_type = psci_migrate_info_type,
6260bc7474fSDavid Brazdil 	};
627e720a6c8SUlf Hansson 
6282bb565bbSGuenter Roeck 	register_restart_handler(&psci_sys_reset_nb);
629e720a6c8SUlf Hansson 
630e720a6c8SUlf Hansson 	pm_power_off = psci_sys_poweroff;
631e720a6c8SUlf Hansson }
632e720a6c8SUlf Hansson 
633e720a6c8SUlf Hansson /*
634e720a6c8SUlf Hansson  * Probe function for PSCI firmware versions >= 0.2
635e720a6c8SUlf Hansson  */
psci_probe(void)636e720a6c8SUlf Hansson static int __init psci_probe(void)
637e720a6c8SUlf Hansson {
638e6dd9d89SDavid Brazdil 	u32 ver = psci_0_2_get_version();
639e720a6c8SUlf Hansson 
640e720a6c8SUlf Hansson 	pr_info("PSCIv%d.%d detected in firmware.\n",
641e720a6c8SUlf Hansson 			PSCI_VERSION_MAJOR(ver),
642e720a6c8SUlf Hansson 			PSCI_VERSION_MINOR(ver));
643e720a6c8SUlf Hansson 
644e720a6c8SUlf Hansson 	if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) {
645e720a6c8SUlf Hansson 		pr_err("Conflicting PSCI version detected.\n");
646e720a6c8SUlf Hansson 		return -EINVAL;
647e720a6c8SUlf Hansson 	}
648e720a6c8SUlf Hansson 
649e720a6c8SUlf Hansson 	psci_0_2_set_functions();
650e720a6c8SUlf Hansson 
651e720a6c8SUlf Hansson 	psci_init_migrate();
652e720a6c8SUlf Hansson 
653e720a6c8SUlf Hansson 	if (PSCI_VERSION_MAJOR(ver) >= 1) {
654e720a6c8SUlf Hansson 		psci_init_smccc();
655e720a6c8SUlf Hansson 		psci_init_cpu_suspend();
656e720a6c8SUlf Hansson 		psci_init_system_suspend();
6574302e381SSudeep Holla 		psci_init_system_reset2();
6586e085e0aSWill Deacon 		kvm_init_hyp_services();
659e720a6c8SUlf Hansson 	}
660e720a6c8SUlf Hansson 
661e720a6c8SUlf Hansson 	return 0;
662e720a6c8SUlf Hansson }
663e720a6c8SUlf Hansson 
664e720a6c8SUlf Hansson typedef int (*psci_initcall_t)(const struct device_node *);
665e720a6c8SUlf Hansson 
666e720a6c8SUlf Hansson /*
667e720a6c8SUlf Hansson  * PSCI init function for PSCI versions >=0.2
668e720a6c8SUlf Hansson  *
669e720a6c8SUlf Hansson  * Probe based on PSCI PSCI_VERSION function
670e720a6c8SUlf Hansson  */
psci_0_2_init(const struct device_node * np)67144f665b6SSami Tolvanen static int __init psci_0_2_init(const struct device_node *np)
672e720a6c8SUlf Hansson {
673e720a6c8SUlf Hansson 	int err;
674e720a6c8SUlf Hansson 
675e720a6c8SUlf Hansson 	err = get_set_conduit_method(np);
676e720a6c8SUlf Hansson 	if (err)
677d036b5cfSUlf Hansson 		return err;
678d036b5cfSUlf Hansson 
679e720a6c8SUlf Hansson 	/*
680e720a6c8SUlf Hansson 	 * Starting with v0.2, the PSCI specification introduced a call
681e720a6c8SUlf Hansson 	 * (PSCI_VERSION) that allows probing the firmware version, so
682e720a6c8SUlf Hansson 	 * that PSCI function IDs and version specific initialization
683e720a6c8SUlf Hansson 	 * can be carried out according to the specific version reported
684e720a6c8SUlf Hansson 	 * by firmware
685e720a6c8SUlf Hansson 	 */
686d036b5cfSUlf Hansson 	return psci_probe();
687e720a6c8SUlf Hansson }
688e720a6c8SUlf Hansson 
689e720a6c8SUlf Hansson /*
690e720a6c8SUlf Hansson  * PSCI < v0.2 get PSCI Function IDs via DT.
691e720a6c8SUlf Hansson  */
psci_0_1_init(const struct device_node * np)69244f665b6SSami Tolvanen static int __init psci_0_1_init(const struct device_node *np)
693e720a6c8SUlf Hansson {
694e720a6c8SUlf Hansson 	u32 id;
695e720a6c8SUlf Hansson 	int err;
696e720a6c8SUlf Hansson 
697e720a6c8SUlf Hansson 	err = get_set_conduit_method(np);
698e720a6c8SUlf Hansson 	if (err)
699d036b5cfSUlf Hansson 		return err;
700e720a6c8SUlf Hansson 
701e720a6c8SUlf Hansson 	pr_info("Using PSCI v0.1 Function IDs from DT\n");
702e720a6c8SUlf Hansson 
703e6dd9d89SDavid Brazdil 	psci_ops.get_version = psci_0_1_get_version;
704e6dd9d89SDavid Brazdil 
705e720a6c8SUlf Hansson 	if (!of_property_read_u32(np, "cpu_suspend", &id)) {
70682ac62d1SDavid Brazdil 		psci_0_1_function_ids.cpu_suspend = id;
7070bc7474fSDavid Brazdil 		psci_ops.cpu_suspend = psci_0_1_cpu_suspend;
708e720a6c8SUlf Hansson 	}
709e720a6c8SUlf Hansson 
710e720a6c8SUlf Hansson 	if (!of_property_read_u32(np, "cpu_off", &id)) {
71182ac62d1SDavid Brazdil 		psci_0_1_function_ids.cpu_off = id;
7120bc7474fSDavid Brazdil 		psci_ops.cpu_off = psci_0_1_cpu_off;
713e720a6c8SUlf Hansson 	}
714e720a6c8SUlf Hansson 
715e720a6c8SUlf Hansson 	if (!of_property_read_u32(np, "cpu_on", &id)) {
71682ac62d1SDavid Brazdil 		psci_0_1_function_ids.cpu_on = id;
7170bc7474fSDavid Brazdil 		psci_ops.cpu_on = psci_0_1_cpu_on;
718e720a6c8SUlf Hansson 	}
719e720a6c8SUlf Hansson 
720e720a6c8SUlf Hansson 	if (!of_property_read_u32(np, "migrate", &id)) {
72182ac62d1SDavid Brazdil 		psci_0_1_function_ids.migrate = id;
7220bc7474fSDavid Brazdil 		psci_ops.migrate = psci_0_1_migrate;
723e720a6c8SUlf Hansson 	}
724e720a6c8SUlf Hansson 
725d036b5cfSUlf Hansson 	return 0;
726e720a6c8SUlf Hansson }
727e720a6c8SUlf Hansson 
psci_1_0_init(const struct device_node * np)72844f665b6SSami Tolvanen static int __init psci_1_0_init(const struct device_node *np)
72960dd1eadSUlf Hansson {
73060dd1eadSUlf Hansson 	int err;
73160dd1eadSUlf Hansson 
73260dd1eadSUlf Hansson 	err = psci_0_2_init(np);
73360dd1eadSUlf Hansson 	if (err)
73460dd1eadSUlf Hansson 		return err;
73560dd1eadSUlf Hansson 
73649fdcd75SUlf Hansson 	if (psci_has_osi_support()) {
73760dd1eadSUlf Hansson 		pr_info("OSI mode supported.\n");
73860dd1eadSUlf Hansson 
73949fdcd75SUlf Hansson 		/* Default to PC mode. */
74010942019SUlf Hansson 		psci_set_osi_mode(false);
74149fdcd75SUlf Hansson 	}
74249fdcd75SUlf Hansson 
74360dd1eadSUlf Hansson 	return 0;
74460dd1eadSUlf Hansson }
74560dd1eadSUlf Hansson 
746e720a6c8SUlf Hansson static const struct of_device_id psci_of_match[] __initconst = {
747e720a6c8SUlf Hansson 	{ .compatible = "arm,psci",	.data = psci_0_1_init},
748e720a6c8SUlf Hansson 	{ .compatible = "arm,psci-0.2",	.data = psci_0_2_init},
74960dd1eadSUlf Hansson 	{ .compatible = "arm,psci-1.0",	.data = psci_1_0_init},
750e720a6c8SUlf Hansson 	{},
751e720a6c8SUlf Hansson };
752e720a6c8SUlf Hansson 
psci_dt_init(void)753e720a6c8SUlf Hansson int __init psci_dt_init(void)
754e720a6c8SUlf Hansson {
755e720a6c8SUlf Hansson 	struct device_node *np;
756e720a6c8SUlf Hansson 	const struct of_device_id *matched_np;
757e720a6c8SUlf Hansson 	psci_initcall_t init_fn;
758d036b5cfSUlf Hansson 	int ret;
759e720a6c8SUlf Hansson 
760e720a6c8SUlf Hansson 	np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
761e720a6c8SUlf Hansson 
762e720a6c8SUlf Hansson 	if (!np || !of_device_is_available(np))
763e720a6c8SUlf Hansson 		return -ENODEV;
764e720a6c8SUlf Hansson 
765e720a6c8SUlf Hansson 	init_fn = (psci_initcall_t)matched_np->data;
766d036b5cfSUlf Hansson 	ret = init_fn(np);
767d036b5cfSUlf Hansson 
768d036b5cfSUlf Hansson 	of_node_put(np);
769d036b5cfSUlf Hansson 	return ret;
770e720a6c8SUlf Hansson }
771e720a6c8SUlf Hansson 
772e720a6c8SUlf Hansson #ifdef CONFIG_ACPI
773e720a6c8SUlf Hansson /*
774e720a6c8SUlf Hansson  * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's
775e720a6c8SUlf Hansson  * explicitly clarified in SBBR
776e720a6c8SUlf Hansson  */
psci_acpi_init(void)777e720a6c8SUlf Hansson int __init psci_acpi_init(void)
778e720a6c8SUlf Hansson {
779e720a6c8SUlf Hansson 	if (!acpi_psci_present()) {
780e720a6c8SUlf Hansson 		pr_info("is not implemented in ACPI.\n");
781e720a6c8SUlf Hansson 		return -EOPNOTSUPP;
782e720a6c8SUlf Hansson 	}
783e720a6c8SUlf Hansson 
784e720a6c8SUlf Hansson 	pr_info("probing for conduit method from ACPI.\n");
785e720a6c8SUlf Hansson 
786e720a6c8SUlf Hansson 	if (acpi_psci_use_hvc())
787a5520eacSMark Rutland 		set_conduit(SMCCC_CONDUIT_HVC);
788e720a6c8SUlf Hansson 	else
789a5520eacSMark Rutland 		set_conduit(SMCCC_CONDUIT_SMC);
790e720a6c8SUlf Hansson 
791e720a6c8SUlf Hansson 	return psci_probe();
792e720a6c8SUlf Hansson }
793e720a6c8SUlf Hansson #endif
794