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
13 #include <efi_loader.h>
14 #include <linux/libfdt.h>
15 #include <linux/arm-smccc.h>
16 #include <linux/errno.h>
17 #include <linux/printk.h>
18 #include <linux/psci.h>
20 #define DRIVER_NAME "psci"
22 #define PSCI_METHOD_HVC 1
23 #define PSCI_METHOD_SMC 2
25 int __efi_runtime_data psci_method;
27 unsigned long __efi_runtime invoke_psci_fn
28 (unsigned long function_id, unsigned long arg0,
29 unsigned long arg1, unsigned long arg2)
31 struct arm_smccc_res res;
34 * In the __efi_runtime we need to avoid the switch statement. In some
35 * cases the compiler creates lookup tables to implement switch. These
36 * tables are not correctly relocated when SetVirtualAddressMap is
39 if (psci_method == PSCI_METHOD_SMC)
40 arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
41 else if (psci_method == PSCI_METHOD_HVC)
42 arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
44 res.a0 = PSCI_RET_DISABLED;
48 static int psci_bind(struct udevice *dev)
50 /* No SYSTEM_RESET support for PSCI 0.1 */
51 if (device_is_compatible(dev, "arm,psci-0.2") ||
52 device_is_compatible(dev, "arm,psci-1.0")) {
55 /* bind psci-sysreset optionally */
56 ret = device_bind_driver(dev, "psci-sysreset", "psci-sysreset",
59 pr_debug("PSCI System Reset was not bound.\n");
65 static int psci_probe(struct udevice *dev)
67 DECLARE_GLOBAL_DATA_PTR;
70 method = fdt_stringlist_get(gd->fdt_blob, dev_of_offset(dev), "method",
73 pr_warn("missing \"method\" property\n");
77 if (!strcmp("hvc", method)) {
78 psci_method = PSCI_METHOD_HVC;
79 } else if (!strcmp("smc", method)) {
80 psci_method = PSCI_METHOD_SMC;
82 pr_warn("invalid \"method\" property: %s\n", method);
90 * void do_psci_probe() - probe PSCI firmware driver
92 * Ensure that psci_method is initialized.
94 static void __maybe_unused do_psci_probe(void)
98 uclass_get_device_by_name(UCLASS_FIRMWARE, DRIVER_NAME, &dev);
101 #if IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET)
102 efi_status_t efi_reset_system_init(void)
108 void __efi_runtime EFIAPI efi_reset_system(enum efi_reset_type reset_type,
109 efi_status_t reset_status,
110 unsigned long data_size,
113 if (reset_type == EFI_RESET_COLD ||
114 reset_type == EFI_RESET_WARM ||
115 reset_type == EFI_RESET_PLATFORM_SPECIFIC) {
116 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
117 } else if (reset_type == EFI_RESET_SHUTDOWN) {
118 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
123 #endif /* IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) */
125 #ifdef CONFIG_PSCI_RESET
126 void reset_misc(void)
129 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
131 #endif /* CONFIG_PSCI_RESET */
133 #ifdef CONFIG_CMD_POWEROFF
134 int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
138 puts("poweroff ...\n");
139 udelay(50000); /* wait 50 ms */
141 disable_interrupts();
142 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
145 log_err("Power off not supported on this platform\n");
146 return CMD_RET_FAILURE;
150 static const struct udevice_id psci_of_match[] = {
151 { .compatible = "arm,psci" },
152 { .compatible = "arm,psci-0.2" },
153 { .compatible = "arm,psci-1.0" },
157 U_BOOT_DRIVER(psci) = {
159 .id = UCLASS_FIRMWARE,
160 .of_match = psci_of_match,