1dc179ad4d15bf94f80babb4450740482e2d3ef6
[oweals/u-boot.git] / drivers / firmware / psci.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017 Masahiro Yamada <yamada.masahiro@socionext.com>
4  *
5  * Based on drivers/firmware/psci.c from Linux:
6  * Copyright (C) 2015 ARM Limited
7  */
8
9 #include <common.h>
10 #include <command.h>
11 #include <dm.h>
12 #include <irq_func.h>
13 #include <log.h>
14 #include <dm/lists.h>
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>
21
22 #define DRIVER_NAME "psci"
23
24 #define PSCI_METHOD_HVC 1
25 #define PSCI_METHOD_SMC 2
26
27 int __efi_runtime_data psci_method;
28
29 unsigned long __efi_runtime invoke_psci_fn
30                 (unsigned long function_id, unsigned long arg0,
31                  unsigned long arg1, unsigned long arg2)
32 {
33         struct arm_smccc_res res;
34
35         /*
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
39          * called.
40          */
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);
45         else
46                 res.a0 = PSCI_RET_DISABLED;
47         return res.a0;
48 }
49
50 static int psci_bind(struct udevice *dev)
51 {
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")) {
55                 int ret;
56
57                 /* bind psci-sysreset optionally */
58                 ret = device_bind_driver(dev, "psci-sysreset", "psci-sysreset",
59                                          NULL);
60                 if (ret)
61                         pr_debug("PSCI System Reset was not bound.\n");
62         }
63
64         return 0;
65 }
66
67 static int psci_probe(struct udevice *dev)
68 {
69         DECLARE_GLOBAL_DATA_PTR;
70         const char *method;
71
72         method = fdt_stringlist_get(gd->fdt_blob, dev_of_offset(dev), "method",
73                                     0, NULL);
74         if (!method) {
75                 pr_warn("missing \"method\" property\n");
76                 return -ENXIO;
77         }
78
79         if (!strcmp("hvc", method)) {
80                 psci_method = PSCI_METHOD_HVC;
81         } else if (!strcmp("smc", method)) {
82                 psci_method = PSCI_METHOD_SMC;
83         } else {
84                 pr_warn("invalid \"method\" property: %s\n", method);
85                 return -EINVAL;
86         }
87
88         return 0;
89 }
90
91 /**
92  * void do_psci_probe() - probe PSCI firmware driver
93  *
94  * Ensure that psci_method is initialized.
95  */
96 static void __maybe_unused do_psci_probe(void)
97 {
98         struct udevice *dev;
99
100         uclass_get_device_by_name(UCLASS_FIRMWARE, DRIVER_NAME, &dev);
101 }
102
103 #if IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET)
104 efi_status_t efi_reset_system_init(void)
105 {
106         do_psci_probe();
107         return EFI_SUCCESS;
108 }
109
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,
113                                            void *reset_data)
114 {
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);
121         }
122         while (1)
123                 ;
124 }
125 #endif /* IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) */
126
127 #ifdef CONFIG_PSCI_RESET
128 void reset_misc(void)
129 {
130         do_psci_probe();
131         invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
132 }
133 #endif /* CONFIG_PSCI_RESET */
134
135 #ifdef CONFIG_CMD_POWEROFF
136 int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
137 {
138         do_psci_probe();
139
140         puts("poweroff ...\n");
141         udelay(50000); /* wait 50 ms */
142
143         disable_interrupts();
144         invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
145         enable_interrupts();
146
147         log_err("Power off not supported on this platform\n");
148         return CMD_RET_FAILURE;
149 }
150 #endif
151
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" },
156         {},
157 };
158
159 U_BOOT_DRIVER(psci) = {
160         .name = DRIVER_NAME,
161         .id = UCLASS_FIRMWARE,
162         .of_match = psci_of_match,
163         .bind = psci_bind,
164         .probe = psci_probe,
165 };