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
15 #include <efi_loader.h>
16 #include <linux/libfdt.h>
17 #include <linux/arm-smccc.h>
18 #include <linux/errno.h>
19 #include <linux/printk.h>
20 #include <linux/psci.h>
22 #define DRIVER_NAME "psci"
24 #define PSCI_METHOD_HVC 1
25 #define PSCI_METHOD_SMC 2
27 int __efi_runtime_data psci_method;
29 unsigned long __efi_runtime invoke_psci_fn
30 (unsigned long function_id, unsigned long arg0,
31 unsigned long arg1, unsigned long arg2)
33 struct arm_smccc_res res;
36 * In the __efi_runtime we need to avoid the switch statement. In some
37 * cases the compiler creates lookup tables to implement switch. These
38 * tables are not correctly relocated when SetVirtualAddressMap is
41 if (psci_method == PSCI_METHOD_SMC)
42 arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
43 else if (psci_method == PSCI_METHOD_HVC)
44 arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
46 res.a0 = PSCI_RET_DISABLED;
50 static int psci_bind(struct udevice *dev)
52 /* No SYSTEM_RESET support for PSCI 0.1 */
53 if (device_is_compatible(dev, "arm,psci-0.2") ||
54 device_is_compatible(dev, "arm,psci-1.0")) {
57 /* bind psci-sysreset optionally */
58 ret = device_bind_driver(dev, "psci-sysreset", "psci-sysreset",
61 pr_debug("PSCI System Reset was not bound.\n");
67 static int psci_probe(struct udevice *dev)
69 DECLARE_GLOBAL_DATA_PTR;
72 method = fdt_stringlist_get(gd->fdt_blob, dev_of_offset(dev), "method",
75 pr_warn("missing \"method\" property\n");
79 if (!strcmp("hvc", method)) {
80 psci_method = PSCI_METHOD_HVC;
81 } else if (!strcmp("smc", method)) {
82 psci_method = PSCI_METHOD_SMC;
84 pr_warn("invalid \"method\" property: %s\n", method);
92 * void do_psci_probe() - probe PSCI firmware driver
94 * Ensure that psci_method is initialized.
96 static void __maybe_unused do_psci_probe(void)
100 uclass_get_device_by_name(UCLASS_FIRMWARE, DRIVER_NAME, &dev);
103 #if IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET)
104 efi_status_t efi_reset_system_init(void)
110 void __efi_runtime EFIAPI efi_reset_system(enum efi_reset_type reset_type,
111 efi_status_t reset_status,
112 unsigned long data_size,
115 if (reset_type == EFI_RESET_COLD ||
116 reset_type == EFI_RESET_WARM ||
117 reset_type == EFI_RESET_PLATFORM_SPECIFIC) {
118 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
119 } else if (reset_type == EFI_RESET_SHUTDOWN) {
120 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
125 #endif /* IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) */
127 #ifdef CONFIG_PSCI_RESET
128 void reset_misc(void)
131 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
133 #endif /* CONFIG_PSCI_RESET */
135 #ifdef CONFIG_CMD_POWEROFF
136 int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
140 puts("poweroff ...\n");
141 udelay(50000); /* wait 50 ms */
143 disable_interrupts();
144 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
147 log_err("Power off not supported on this platform\n");
148 return CMD_RET_FAILURE;
152 static const struct udevice_id psci_of_match[] = {
153 { .compatible = "arm,psci" },
154 { .compatible = "arm,psci-0.2" },
155 { .compatible = "arm,psci-1.0" },
159 U_BOOT_DRIVER(psci) = {
161 .id = UCLASS_FIRMWARE,
162 .of_match = psci_of_match,