1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Masahiro Yamada <yamada.masahiro@socionext.com>
5 * Based on drivers/firmware/psci.c from Linux:
6 * Copyright (C) 2015 ARM Limited
12 #include <efi_loader.h>
14 #include <linker_lists.h>
17 #include <asm/system.h>
18 #include <dm/device-internal.h>
20 #include <linux/arm-smccc.h>
21 #include <linux/delay.h>
22 #include <linux/errno.h>
23 #include <linux/libfdt.h>
24 #include <linux/printk.h>
25 #include <linux/psci.h>
27 #define DRIVER_NAME "psci"
29 #define PSCI_METHOD_HVC 1
30 #define PSCI_METHOD_SMC 2
33 * While a 64-bit OS can make calls with SMC32 calling conventions, for some
34 * calls it is necessary to use SMC64 to pass or return 64-bit values.
35 * For such calls PSCI_FN_NATIVE(version, name) will choose the appropriate
36 * (native-width) function ID.
38 #if defined(CONFIG_ARM64)
39 #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN64_##name
41 #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN_##name
44 #if CONFIG_IS_ENABLED(EFI_LOADER)
45 int __efi_runtime_data psci_method;
47 int psci_method __section(".data");
50 unsigned long __efi_runtime invoke_psci_fn
51 (unsigned long function_id, unsigned long arg0,
52 unsigned long arg1, unsigned long arg2)
54 struct arm_smccc_res res;
57 * In the __efi_runtime we need to avoid the switch statement. In some
58 * cases the compiler creates lookup tables to implement switch. These
59 * tables are not correctly relocated when SetVirtualAddressMap is
62 if (psci_method == PSCI_METHOD_SMC)
63 arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
64 else if (psci_method == PSCI_METHOD_HVC)
65 arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
67 res.a0 = PSCI_RET_DISABLED;
71 static int request_psci_features(u32 psci_func_id)
73 return invoke_psci_fn(PSCI_1_0_FN_PSCI_FEATURES,
77 static u32 psci_0_2_get_version(void)
79 return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
82 static bool psci_is_system_reset2_supported(void)
87 ver = psci_0_2_get_version();
89 if (PSCI_VERSION_MAJOR(ver) >= 1) {
90 ret = request_psci_features(PSCI_FN_NATIVE(1_1,
93 if (ret != PSCI_RET_NOT_SUPPORTED)
100 static void smccc_invoke_hvc(unsigned long a0, unsigned long a1,
101 unsigned long a2, unsigned long a3,
102 unsigned long a4, unsigned long a5,
103 unsigned long a6, unsigned long a7,
104 struct arm_smccc_res *res)
106 arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res);
109 static void smccc_invoke_smc(unsigned long a0, unsigned long a1,
110 unsigned long a2, unsigned long a3,
111 unsigned long a4, unsigned long a5,
112 unsigned long a6, unsigned long a7,
113 struct arm_smccc_res *res)
115 arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res);
118 static int bind_smccc_features(struct udevice *dev, int psci_method)
120 struct psci_plat_data *pdata = dev_get_plat(dev);
121 struct arm_smccc_feature *feature;
122 size_t feature_cnt, n;
124 if (!IS_ENABLED(CONFIG_ARM_SMCCC_FEATURES))
128 * SMCCC features discovery invoke SMCCC standard function ID
129 * ARM_SMCCC_ARCH_FEATURES but this sequence requires that this
130 * standard ARM_SMCCC_ARCH_FEATURES function ID itself is supported.
131 * It is queried here with invoking PSCI_FEATURES known available
134 if (!device_is_compatible(dev, "arm,psci-1.0") ||
135 PSCI_VERSION_MAJOR(psci_0_2_get_version()) == 0)
138 if (request_psci_features(ARM_SMCCC_ARCH_FEATURES) ==
139 PSCI_RET_NOT_SUPPORTED)
142 if (psci_method == PSCI_METHOD_HVC)
143 pdata->invoke_fn = smccc_invoke_hvc;
145 pdata->invoke_fn = smccc_invoke_smc;
147 feature_cnt = ll_entry_count(struct arm_smccc_feature, arm_smccc_feature);
148 feature = ll_entry_start(struct arm_smccc_feature, arm_smccc_feature);
150 for (n = 0; n < feature_cnt; n++, feature++) {
151 const char *drv_name = feature->driver_name;
152 struct udevice *dev2;
155 if (!feature->is_supported || !feature->is_supported(pdata->invoke_fn))
158 ret = device_bind_driver(dev, drv_name, drv_name, &dev2);
160 pr_warn("%s was not bound: %d, ignore\n", drv_name, ret);
164 dev_set_parent_plat(dev2, dev_get_plat(dev));
170 static int psci_bind(struct udevice *dev)
172 /* No SYSTEM_RESET support for PSCI 0.1 */
173 if (device_is_compatible(dev, "arm,psci-0.2") ||
174 device_is_compatible(dev, "arm,psci-1.0")) {
177 /* bind psci-sysreset optionally */
178 ret = device_bind_driver(dev, "psci-sysreset", "psci-sysreset",
181 pr_debug("PSCI System Reset was not bound.\n");
184 /* From PSCI v1.0 onward we can discover services through ARM_SMCCC_FEATURE */
185 if (IS_ENABLED(CONFIG_ARM_SMCCC_FEATURES) && device_is_compatible(dev, "arm,psci-1.0"))
186 dev_or_flags(dev, DM_FLAG_PROBE_AFTER_BIND);
191 static int psci_probe(struct udevice *dev)
195 #if defined(CONFIG_ARM64)
196 if (current_el() == 3)
200 method = ofnode_read_string(dev_ofnode(dev), "method");
202 pr_warn("missing \"method\" property\n");
206 if (!strcmp("hvc", method)) {
207 psci_method = PSCI_METHOD_HVC;
208 } else if (!strcmp("smc", method)) {
209 psci_method = PSCI_METHOD_SMC;
211 pr_warn("invalid \"method\" property: %s\n", method);
215 return bind_smccc_features(dev, psci_method);
219 * void do_psci_probe() - probe PSCI firmware driver
221 * Ensure that psci_method is initialized.
223 static void __maybe_unused do_psci_probe(void)
227 uclass_get_device_by_name(UCLASS_FIRMWARE, DRIVER_NAME, &dev);
230 #if IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET)
231 efi_status_t efi_reset_system_init(void)
237 void __efi_runtime EFIAPI efi_reset_system(enum efi_reset_type reset_type,
238 efi_status_t reset_status,
239 unsigned long data_size,
242 if (reset_type == EFI_RESET_COLD ||
243 reset_type == EFI_RESET_WARM ||
244 reset_type == EFI_RESET_PLATFORM_SPECIFIC) {
245 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
246 } else if (reset_type == EFI_RESET_SHUTDOWN) {
247 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
252 #endif /* IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) */
254 #ifdef CONFIG_PSCI_RESET
255 void reset_misc(void)
258 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
260 #endif /* CONFIG_PSCI_RESET */
262 void psci_sys_reset(u32 type)
264 bool reset2_supported;
268 reset2_supported = psci_is_system_reset2_supported();
270 if (type == SYSRESET_WARM && reset2_supported) {
272 * reset_type[31] = 0 (architectural)
273 * reset_type[30:0] = 0 (SYSTEM_WARM_RESET)
274 * cookie = 0 (ignored by the implementation)
276 invoke_psci_fn(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2), 0, 0, 0);
278 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
282 void psci_sys_poweroff(void)
286 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
289 #if IS_ENABLED(CONFIG_CMD_POWEROFF) && !IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF)
290 int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
294 puts("poweroff ...\n");
295 udelay(50000); /* wait 50 ms */
297 disable_interrupts();
298 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
301 log_err("Power off not supported on this platform\n");
302 return CMD_RET_FAILURE;
306 static const struct udevice_id psci_of_match[] = {
307 { .compatible = "arm,psci" },
308 { .compatible = "arm,psci-0.2" },
309 { .compatible = "arm,psci-1.0" },
313 U_BOOT_DRIVER(psci) = {
315 .id = UCLASS_FIRMWARE,
316 .of_match = psci_of_match,
319 #ifdef CONFIG_ARM_SMCCC_FEATURES
320 .plat_auto = sizeof(struct psci_plat_data),
322 .flags = DM_FLAG_PRE_RELOC,