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
14 #include <efi_loader.h>
15 #include <linux/libfdt.h>
16 #include <linux/arm-smccc.h>
17 #include <linux/errno.h>
18 #include <linux/printk.h>
19 #include <linux/psci.h>
21 #define DRIVER_NAME "psci"
23 #define PSCI_METHOD_HVC 1
24 #define PSCI_METHOD_SMC 2
26 int __efi_runtime_data psci_method;
28 unsigned long __efi_runtime invoke_psci_fn
29 (unsigned long function_id, unsigned long arg0,
30 unsigned long arg1, unsigned long arg2)
32 struct arm_smccc_res res;
35 * In the __efi_runtime we need to avoid the switch statement. In some
36 * cases the compiler creates lookup tables to implement switch. These
37 * tables are not correctly relocated when SetVirtualAddressMap is
40 if (psci_method == PSCI_METHOD_SMC)
41 arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
42 else if (psci_method == PSCI_METHOD_HVC)
43 arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
45 res.a0 = PSCI_RET_DISABLED;
49 static int psci_bind(struct udevice *dev)
51 /* No SYSTEM_RESET support for PSCI 0.1 */
52 if (device_is_compatible(dev, "arm,psci-0.2") ||
53 device_is_compatible(dev, "arm,psci-1.0")) {
56 /* bind psci-sysreset optionally */
57 ret = device_bind_driver(dev, "psci-sysreset", "psci-sysreset",
60 pr_debug("PSCI System Reset was not bound.\n");
66 static int psci_probe(struct udevice *dev)
68 DECLARE_GLOBAL_DATA_PTR;
71 method = fdt_stringlist_get(gd->fdt_blob, dev_of_offset(dev), "method",
74 pr_warn("missing \"method\" property\n");
78 if (!strcmp("hvc", method)) {
79 psci_method = PSCI_METHOD_HVC;
80 } else if (!strcmp("smc", method)) {
81 psci_method = PSCI_METHOD_SMC;
83 pr_warn("invalid \"method\" property: %s\n", method);
91 * void do_psci_probe() - probe PSCI firmware driver
93 * Ensure that psci_method is initialized.
95 static void __maybe_unused do_psci_probe(void)
99 uclass_get_device_by_name(UCLASS_FIRMWARE, DRIVER_NAME, &dev);
102 #if IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET)
103 efi_status_t efi_reset_system_init(void)
109 void __efi_runtime EFIAPI efi_reset_system(enum efi_reset_type reset_type,
110 efi_status_t reset_status,
111 unsigned long data_size,
114 if (reset_type == EFI_RESET_COLD ||
115 reset_type == EFI_RESET_WARM ||
116 reset_type == EFI_RESET_PLATFORM_SPECIFIC) {
117 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
118 } else if (reset_type == EFI_RESET_SHUTDOWN) {
119 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
124 #endif /* IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) */
126 #ifdef CONFIG_PSCI_RESET
127 void reset_misc(void)
130 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
132 #endif /* CONFIG_PSCI_RESET */
134 #ifdef CONFIG_CMD_POWEROFF
135 int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
139 puts("poweroff ...\n");
140 udelay(50000); /* wait 50 ms */
142 disable_interrupts();
143 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
146 log_err("Power off not supported on this platform\n");
147 return CMD_RET_FAILURE;
151 static const struct udevice_id psci_of_match[] = {
152 { .compatible = "arm,psci" },
153 { .compatible = "arm,psci-0.2" },
154 { .compatible = "arm,psci-1.0" },
158 U_BOOT_DRIVER(psci) = {
160 .id = UCLASS_FIRMWARE,
161 .of_match = psci_of_match,