blob: 03544d76ed4ad3980b3ce1b6a47ac1d72120d316 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada836c55d2017-04-14 11:10:24 +09002/*
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
Masahiro Yamada836c55d2017-04-14 11:10:24 +09007 */
8
9#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -060010#include <command.h>
Simon Glass11c89f32017-05-17 17:18:03 -060011#include <dm.h>
Etienne Carrierefe243802022-06-01 10:27:32 +020012#include <efi_loader.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070013#include <irq_func.h>
Etienne Carriere73b4eaf2022-06-01 10:27:33 +020014#include <linker_lists.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Igor Opaniuk3696c092021-04-01 02:01:53 +030016#include <sysreset.h>
Etienne Carrierefe243802022-06-01 10:27:32 +020017#include <asm/system.h>
Etienne Carriere73b4eaf2022-06-01 10:27:33 +020018#include <dm/device-internal.h>
Etienne Carrierefe243802022-06-01 10:27:32 +020019#include <dm/lists.h>
Masahiro Yamada836c55d2017-04-14 11:10:24 +090020#include <linux/arm-smccc.h>
Etienne Carrierefe243802022-06-01 10:27:32 +020021#include <linux/delay.h>
Masahiro Yamada836c55d2017-04-14 11:10:24 +090022#include <linux/errno.h>
Etienne Carrierefe243802022-06-01 10:27:32 +020023#include <linux/libfdt.h>
Masahiro Yamada8fa57ed2017-11-29 15:04:40 +090024#include <linux/printk.h>
Masahiro Yamada836c55d2017-04-14 11:10:24 +090025#include <linux/psci.h>
26
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020027#define DRIVER_NAME "psci"
Masahiro Yamada836c55d2017-04-14 11:10:24 +090028
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020029#define PSCI_METHOD_HVC 1
30#define PSCI_METHOD_SMC 2
Masahiro Yamada836c55d2017-04-14 11:10:24 +090031
Igor Opaniuk3696c092021-04-01 02:01:53 +030032/*
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.
37 */
38#if defined(CONFIG_ARM64)
39#define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN64_##name
40#else
41#define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN_##name
42#endif
43
Yann Gautier2aa7fc62020-07-17 14:20:15 +020044#if CONFIG_IS_ENABLED(EFI_LOADER)
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020045int __efi_runtime_data psci_method;
Yann Gautier2aa7fc62020-07-17 14:20:15 +020046#else
Marek BehĂșn4bebdd32021-05-20 13:23:52 +020047int psci_method __section(".data");
Yann Gautier2aa7fc62020-07-17 14:20:15 +020048#endif
Masahiro Yamada836c55d2017-04-14 11:10:24 +090049
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020050unsigned long __efi_runtime invoke_psci_fn
51 (unsigned long function_id, unsigned long arg0,
52 unsigned long arg1, unsigned long arg2)
Masahiro Yamada836c55d2017-04-14 11:10:24 +090053{
54 struct arm_smccc_res res;
55
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020056 /*
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
60 * called.
61 */
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);
66 else
67 res.a0 = PSCI_RET_DISABLED;
Masahiro Yamada836c55d2017-04-14 11:10:24 +090068 return res.a0;
69}
70
Igor Opaniukecb99d02021-05-06 17:34:27 +030071static int request_psci_features(u32 psci_func_id)
Igor Opaniuk3696c092021-04-01 02:01:53 +030072{
73 return invoke_psci_fn(PSCI_1_0_FN_PSCI_FEATURES,
74 psci_func_id, 0, 0);
75}
76
77static u32 psci_0_2_get_version(void)
78{
79 return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
80}
81
82static bool psci_is_system_reset2_supported(void)
83{
84 int ret;
85 u32 ver;
86
87 ver = psci_0_2_get_version();
88
89 if (PSCI_VERSION_MAJOR(ver) >= 1) {
Igor Opaniukecb99d02021-05-06 17:34:27 +030090 ret = request_psci_features(PSCI_FN_NATIVE(1_1,
91 SYSTEM_RESET2));
Igor Opaniuk3696c092021-04-01 02:01:53 +030092
93 if (ret != PSCI_RET_NOT_SUPPORTED)
94 return true;
95 }
96
97 return false;
98}
99
Etienne Carriere73b4eaf2022-06-01 10:27:33 +0200100static 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)
105{
106 arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res);
107}
108
109static 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)
114{
115 arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res);
116}
117
118static int bind_smccc_features(struct udevice *dev, int psci_method)
119{
120 struct psci_plat_data *pdata = dev_get_plat(dev);
121 struct arm_smccc_feature *feature;
122 size_t feature_cnt, n;
123
124 if (!IS_ENABLED(CONFIG_ARM_SMCCC_FEATURES))
125 return 0;
126
127 /*
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
132 * from PSCI 1.0.
133 */
134 if (!device_is_compatible(dev, "arm,psci-1.0") ||
135 PSCI_VERSION_MAJOR(psci_0_2_get_version()) == 0)
136 return 0;
137
Weizhao Ouyang7f734c02024-03-04 14:42:40 +0000138 if (request_psci_features(ARM_SMCCC_VERSION) ==
Etienne Carriere73b4eaf2022-06-01 10:27:33 +0200139 PSCI_RET_NOT_SUPPORTED)
140 return 0;
141
Weizhao Ouyang7f734c02024-03-04 14:42:40 +0000142 if (invoke_psci_fn(ARM_SMCCC_VERSION, 0, 0, 0) < ARM_SMCCC_VERSION_1_1)
143 return 0;
144
Etienne Carriere73b4eaf2022-06-01 10:27:33 +0200145 if (psci_method == PSCI_METHOD_HVC)
146 pdata->invoke_fn = smccc_invoke_hvc;
147 else
148 pdata->invoke_fn = smccc_invoke_smc;
149
150 feature_cnt = ll_entry_count(struct arm_smccc_feature, arm_smccc_feature);
151 feature = ll_entry_start(struct arm_smccc_feature, arm_smccc_feature);
152
153 for (n = 0; n < feature_cnt; n++, feature++) {
154 const char *drv_name = feature->driver_name;
155 struct udevice *dev2;
156 int ret;
157
158 if (!feature->is_supported || !feature->is_supported(pdata->invoke_fn))
159 continue;
160
161 ret = device_bind_driver(dev, drv_name, drv_name, &dev2);
162 if (ret) {
163 pr_warn("%s was not bound: %d, ignore\n", drv_name, ret);
164 continue;
165 }
166
167 dev_set_parent_plat(dev2, dev_get_plat(dev));
168 }
169
170 return 0;
171}
172
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900173static int psci_bind(struct udevice *dev)
174{
175 /* No SYSTEM_RESET support for PSCI 0.1 */
Simon Glass54cbcc82017-05-18 20:08:57 -0600176 if (device_is_compatible(dev, "arm,psci-0.2") ||
177 device_is_compatible(dev, "arm,psci-1.0")) {
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900178 int ret;
179
180 /* bind psci-sysreset optionally */
181 ret = device_bind_driver(dev, "psci-sysreset", "psci-sysreset",
182 NULL);
183 if (ret)
Masahiro Yamada8fa57ed2017-11-29 15:04:40 +0900184 pr_debug("PSCI System Reset was not bound.\n");
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900185 }
186
Etienne Carriere73b4eaf2022-06-01 10:27:33 +0200187 /* From PSCI v1.0 onward we can discover services through ARM_SMCCC_FEATURE */
188 if (IS_ENABLED(CONFIG_ARM_SMCCC_FEATURES) && device_is_compatible(dev, "arm,psci-1.0"))
189 dev_or_flags(dev, DM_FLAG_PROBE_AFTER_BIND);
190
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900191 return 0;
192}
193
194static int psci_probe(struct udevice *dev)
195{
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900196 const char *method;
197
Michal Simekd1803532020-08-13 12:43:22 +0200198#if defined(CONFIG_ARM64)
199 if (current_el() == 3)
200 return -EINVAL;
201#endif
202
Jon Hunter52b72c12020-06-18 12:54:38 +0100203 method = ofnode_read_string(dev_ofnode(dev), "method");
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900204 if (!method) {
Masahiro Yamada8fa57ed2017-11-29 15:04:40 +0900205 pr_warn("missing \"method\" property\n");
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900206 return -ENXIO;
207 }
208
209 if (!strcmp("hvc", method)) {
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +0200210 psci_method = PSCI_METHOD_HVC;
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900211 } else if (!strcmp("smc", method)) {
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +0200212 psci_method = PSCI_METHOD_SMC;
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900213 } else {
Masahiro Yamada8fa57ed2017-11-29 15:04:40 +0900214 pr_warn("invalid \"method\" property: %s\n", method);
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900215 return -EINVAL;
216 }
217
Etienne Carriere73b4eaf2022-06-01 10:27:33 +0200218 return bind_smccc_features(dev, psci_method);
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900219}
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +0200220
221/**
222 * void do_psci_probe() - probe PSCI firmware driver
223 *
224 * Ensure that psci_method is initialized.
225 */
226static void __maybe_unused do_psci_probe(void)
227{
228 struct udevice *dev;
229
230 uclass_get_device_by_name(UCLASS_FIRMWARE, DRIVER_NAME, &dev);
231}
232
233#if IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET)
234efi_status_t efi_reset_system_init(void)
235{
236 do_psci_probe();
237 return EFI_SUCCESS;
238}
239
240void __efi_runtime EFIAPI efi_reset_system(enum efi_reset_type reset_type,
241 efi_status_t reset_status,
242 unsigned long data_size,
243 void *reset_data)
244{
245 if (reset_type == EFI_RESET_COLD ||
246 reset_type == EFI_RESET_WARM ||
247 reset_type == EFI_RESET_PLATFORM_SPECIFIC) {
248 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
249 } else if (reset_type == EFI_RESET_SHUTDOWN) {
250 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
251 }
252 while (1)
253 ;
254}
255#endif /* IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) */
256
257#ifdef CONFIG_PSCI_RESET
258void reset_misc(void)
259{
260 do_psci_probe();
261 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
262}
263#endif /* CONFIG_PSCI_RESET */
264
Igor Opaniuk3696c092021-04-01 02:01:53 +0300265void psci_sys_reset(u32 type)
266{
267 bool reset2_supported;
268
269 do_psci_probe();
270
271 reset2_supported = psci_is_system_reset2_supported();
272
273 if (type == SYSRESET_WARM && reset2_supported) {
274 /*
275 * reset_type[31] = 0 (architectural)
276 * reset_type[30:0] = 0 (SYSTEM_WARM_RESET)
277 * cookie = 0 (ignored by the implementation)
278 */
279 invoke_psci_fn(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2), 0, 0, 0);
280 } else {
281 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
282 }
283}
284
285void psci_sys_poweroff(void)
286{
287 do_psci_probe();
288
289 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
290}
291
Michal Simekf92175c2021-07-13 16:53:46 +0200292#if IS_ENABLED(CONFIG_CMD_POWEROFF) && !IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF)
Simon Glassed38aef2020-05-10 11:40:03 -0600293int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +0200294{
295 do_psci_probe();
296
297 puts("poweroff ...\n");
298 udelay(50000); /* wait 50 ms */
299
300 disable_interrupts();
301 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
302 enable_interrupts();
303
304 log_err("Power off not supported on this platform\n");
305 return CMD_RET_FAILURE;
306}
307#endif
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900308
309static const struct udevice_id psci_of_match[] = {
310 { .compatible = "arm,psci" },
311 { .compatible = "arm,psci-0.2" },
312 { .compatible = "arm,psci-1.0" },
313 {},
314};
315
316U_BOOT_DRIVER(psci) = {
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +0200317 .name = DRIVER_NAME,
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900318 .id = UCLASS_FIRMWARE,
319 .of_match = psci_of_match,
320 .bind = psci_bind,
321 .probe = psci_probe,
Etienne Carriere73b4eaf2022-06-01 10:27:33 +0200322#ifdef CONFIG_ARM_SMCCC_FEATURES
323 .plat_auto = sizeof(struct psci_plat_data),
324#endif
Peng Fana6fb3e82023-04-06 18:23:17 +0800325 .flags = DM_FLAG_PRE_RELOC,
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900326};