Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 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 |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 7 | */ |
| 8 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 9 | #include <command.h> |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 10 | #include <dm.h> |
Etienne Carriere | fe24380 | 2022-06-01 10:27:32 +0200 | [diff] [blame] | 11 | #include <efi_loader.h> |
Simon Glass | 8f3f761 | 2019-11-14 12:57:42 -0700 | [diff] [blame] | 12 | #include <irq_func.h> |
Etienne Carriere | 73b4eaf | 2022-06-01 10:27:33 +0200 | [diff] [blame] | 13 | #include <linker_lists.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Igor Opaniuk | 3696c09 | 2021-04-01 02:01:53 +0300 | [diff] [blame] | 15 | #include <sysreset.h> |
Etienne Carriere | fe24380 | 2022-06-01 10:27:32 +0200 | [diff] [blame] | 16 | #include <asm/system.h> |
Etienne Carriere | 73b4eaf | 2022-06-01 10:27:33 +0200 | [diff] [blame] | 17 | #include <dm/device-internal.h> |
Etienne Carriere | fe24380 | 2022-06-01 10:27:32 +0200 | [diff] [blame] | 18 | #include <dm/lists.h> |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 19 | #include <linux/arm-smccc.h> |
Etienne Carriere | fe24380 | 2022-06-01 10:27:32 +0200 | [diff] [blame] | 20 | #include <linux/delay.h> |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 21 | #include <linux/errno.h> |
Etienne Carriere | fe24380 | 2022-06-01 10:27:32 +0200 | [diff] [blame] | 22 | #include <linux/libfdt.h> |
Masahiro Yamada | 8fa57ed | 2017-11-29 15:04:40 +0900 | [diff] [blame] | 23 | #include <linux/printk.h> |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 24 | #include <linux/psci.h> |
| 25 | |
Heinrich Schuchardt | 26f09d0 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 26 | #define DRIVER_NAME "psci" |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 27 | |
Heinrich Schuchardt | 26f09d0 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 28 | #define PSCI_METHOD_HVC 1 |
| 29 | #define PSCI_METHOD_SMC 2 |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 30 | |
Igor Opaniuk | 3696c09 | 2021-04-01 02:01:53 +0300 | [diff] [blame] | 31 | /* |
| 32 | * While a 64-bit OS can make calls with SMC32 calling conventions, for some |
| 33 | * calls it is necessary to use SMC64 to pass or return 64-bit values. |
| 34 | * For such calls PSCI_FN_NATIVE(version, name) will choose the appropriate |
| 35 | * (native-width) function ID. |
| 36 | */ |
| 37 | #if defined(CONFIG_ARM64) |
| 38 | #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN64_##name |
| 39 | #else |
| 40 | #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN_##name |
| 41 | #endif |
| 42 | |
Yann Gautier | 2aa7fc6 | 2020-07-17 14:20:15 +0200 | [diff] [blame] | 43 | #if CONFIG_IS_ENABLED(EFI_LOADER) |
Heinrich Schuchardt | 26f09d0 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 44 | int __efi_runtime_data psci_method; |
Yann Gautier | 2aa7fc6 | 2020-07-17 14:20:15 +0200 | [diff] [blame] | 45 | #else |
Marek BehĂșn | 4bebdd3 | 2021-05-20 13:23:52 +0200 | [diff] [blame] | 46 | int psci_method __section(".data"); |
Yann Gautier | 2aa7fc6 | 2020-07-17 14:20:15 +0200 | [diff] [blame] | 47 | #endif |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 48 | |
Heinrich Schuchardt | 26f09d0 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 49 | unsigned long __efi_runtime invoke_psci_fn |
| 50 | (unsigned long function_id, unsigned long arg0, |
| 51 | unsigned long arg1, unsigned long arg2) |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 52 | { |
| 53 | struct arm_smccc_res res; |
| 54 | |
Heinrich Schuchardt | 26f09d0 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 55 | /* |
| 56 | * In the __efi_runtime we need to avoid the switch statement. In some |
| 57 | * cases the compiler creates lookup tables to implement switch. These |
| 58 | * tables are not correctly relocated when SetVirtualAddressMap is |
| 59 | * called. |
| 60 | */ |
| 61 | if (psci_method == PSCI_METHOD_SMC) |
| 62 | arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res); |
| 63 | else if (psci_method == PSCI_METHOD_HVC) |
| 64 | arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res); |
| 65 | else |
| 66 | res.a0 = PSCI_RET_DISABLED; |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 67 | return res.a0; |
| 68 | } |
| 69 | |
Igor Opaniuk | ecb99d0 | 2021-05-06 17:34:27 +0300 | [diff] [blame] | 70 | static int request_psci_features(u32 psci_func_id) |
Igor Opaniuk | 3696c09 | 2021-04-01 02:01:53 +0300 | [diff] [blame] | 71 | { |
| 72 | return invoke_psci_fn(PSCI_1_0_FN_PSCI_FEATURES, |
| 73 | psci_func_id, 0, 0); |
| 74 | } |
| 75 | |
| 76 | static u32 psci_0_2_get_version(void) |
| 77 | { |
| 78 | return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0); |
| 79 | } |
| 80 | |
| 81 | static bool psci_is_system_reset2_supported(void) |
| 82 | { |
| 83 | int ret; |
| 84 | u32 ver; |
| 85 | |
| 86 | ver = psci_0_2_get_version(); |
| 87 | |
| 88 | if (PSCI_VERSION_MAJOR(ver) >= 1) { |
Igor Opaniuk | ecb99d0 | 2021-05-06 17:34:27 +0300 | [diff] [blame] | 89 | ret = request_psci_features(PSCI_FN_NATIVE(1_1, |
| 90 | SYSTEM_RESET2)); |
Igor Opaniuk | 3696c09 | 2021-04-01 02:01:53 +0300 | [diff] [blame] | 91 | |
| 92 | if (ret != PSCI_RET_NOT_SUPPORTED) |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | return false; |
| 97 | } |
| 98 | |
Etienne Carriere | 73b4eaf | 2022-06-01 10:27:33 +0200 | [diff] [blame] | 99 | static void smccc_invoke_hvc(unsigned long a0, unsigned long a1, |
| 100 | unsigned long a2, unsigned long a3, |
| 101 | unsigned long a4, unsigned long a5, |
| 102 | unsigned long a6, unsigned long a7, |
| 103 | struct arm_smccc_res *res) |
| 104 | { |
| 105 | arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res); |
| 106 | } |
| 107 | |
| 108 | static void smccc_invoke_smc(unsigned long a0, unsigned long a1, |
| 109 | unsigned long a2, unsigned long a3, |
| 110 | unsigned long a4, unsigned long a5, |
| 111 | unsigned long a6, unsigned long a7, |
| 112 | struct arm_smccc_res *res) |
| 113 | { |
| 114 | arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res); |
| 115 | } |
| 116 | |
| 117 | static int bind_smccc_features(struct udevice *dev, int psci_method) |
| 118 | { |
| 119 | struct psci_plat_data *pdata = dev_get_plat(dev); |
| 120 | struct arm_smccc_feature *feature; |
| 121 | size_t feature_cnt, n; |
| 122 | |
| 123 | if (!IS_ENABLED(CONFIG_ARM_SMCCC_FEATURES)) |
| 124 | return 0; |
| 125 | |
| 126 | /* |
| 127 | * SMCCC features discovery invoke SMCCC standard function ID |
| 128 | * ARM_SMCCC_ARCH_FEATURES but this sequence requires that this |
| 129 | * standard ARM_SMCCC_ARCH_FEATURES function ID itself is supported. |
| 130 | * It is queried here with invoking PSCI_FEATURES known available |
| 131 | * from PSCI 1.0. |
| 132 | */ |
| 133 | if (!device_is_compatible(dev, "arm,psci-1.0") || |
| 134 | PSCI_VERSION_MAJOR(psci_0_2_get_version()) == 0) |
| 135 | return 0; |
| 136 | |
Weizhao Ouyang | 7f734c0 | 2024-03-04 14:42:40 +0000 | [diff] [blame] | 137 | if (request_psci_features(ARM_SMCCC_VERSION) == |
Etienne Carriere | 73b4eaf | 2022-06-01 10:27:33 +0200 | [diff] [blame] | 138 | PSCI_RET_NOT_SUPPORTED) |
| 139 | return 0; |
| 140 | |
Weizhao Ouyang | 7f734c0 | 2024-03-04 14:42:40 +0000 | [diff] [blame] | 141 | if (invoke_psci_fn(ARM_SMCCC_VERSION, 0, 0, 0) < ARM_SMCCC_VERSION_1_1) |
| 142 | return 0; |
| 143 | |
Etienne Carriere | 73b4eaf | 2022-06-01 10:27:33 +0200 | [diff] [blame] | 144 | if (psci_method == PSCI_METHOD_HVC) |
| 145 | pdata->invoke_fn = smccc_invoke_hvc; |
| 146 | else |
| 147 | pdata->invoke_fn = smccc_invoke_smc; |
| 148 | |
| 149 | feature_cnt = ll_entry_count(struct arm_smccc_feature, arm_smccc_feature); |
| 150 | feature = ll_entry_start(struct arm_smccc_feature, arm_smccc_feature); |
| 151 | |
| 152 | for (n = 0; n < feature_cnt; n++, feature++) { |
| 153 | const char *drv_name = feature->driver_name; |
| 154 | struct udevice *dev2; |
| 155 | int ret; |
| 156 | |
| 157 | if (!feature->is_supported || !feature->is_supported(pdata->invoke_fn)) |
| 158 | continue; |
| 159 | |
| 160 | ret = device_bind_driver(dev, drv_name, drv_name, &dev2); |
| 161 | if (ret) { |
| 162 | pr_warn("%s was not bound: %d, ignore\n", drv_name, ret); |
| 163 | continue; |
| 164 | } |
| 165 | |
| 166 | dev_set_parent_plat(dev2, dev_get_plat(dev)); |
| 167 | } |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 172 | static int psci_bind(struct udevice *dev) |
| 173 | { |
| 174 | /* No SYSTEM_RESET support for PSCI 0.1 */ |
Simon Glass | 54cbcc8 | 2017-05-18 20:08:57 -0600 | [diff] [blame] | 175 | if (device_is_compatible(dev, "arm,psci-0.2") || |
| 176 | device_is_compatible(dev, "arm,psci-1.0")) { |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 177 | int ret; |
| 178 | |
| 179 | /* bind psci-sysreset optionally */ |
| 180 | ret = device_bind_driver(dev, "psci-sysreset", "psci-sysreset", |
| 181 | NULL); |
| 182 | if (ret) |
Masahiro Yamada | 8fa57ed | 2017-11-29 15:04:40 +0900 | [diff] [blame] | 183 | pr_debug("PSCI System Reset was not bound.\n"); |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 184 | } |
| 185 | |
Etienne Carriere | 73b4eaf | 2022-06-01 10:27:33 +0200 | [diff] [blame] | 186 | /* From PSCI v1.0 onward we can discover services through ARM_SMCCC_FEATURE */ |
| 187 | if (IS_ENABLED(CONFIG_ARM_SMCCC_FEATURES) && device_is_compatible(dev, "arm,psci-1.0")) |
| 188 | dev_or_flags(dev, DM_FLAG_PROBE_AFTER_BIND); |
| 189 | |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static int psci_probe(struct udevice *dev) |
| 194 | { |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 195 | const char *method; |
| 196 | |
Michal Simek | d180353 | 2020-08-13 12:43:22 +0200 | [diff] [blame] | 197 | #if defined(CONFIG_ARM64) |
| 198 | if (current_el() == 3) |
| 199 | return -EINVAL; |
| 200 | #endif |
| 201 | |
Jon Hunter | 52b72c1 | 2020-06-18 12:54:38 +0100 | [diff] [blame] | 202 | method = ofnode_read_string(dev_ofnode(dev), "method"); |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 203 | if (!method) { |
Masahiro Yamada | 8fa57ed | 2017-11-29 15:04:40 +0900 | [diff] [blame] | 204 | pr_warn("missing \"method\" property\n"); |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 205 | return -ENXIO; |
| 206 | } |
| 207 | |
| 208 | if (!strcmp("hvc", method)) { |
Heinrich Schuchardt | 26f09d0 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 209 | psci_method = PSCI_METHOD_HVC; |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 210 | } else if (!strcmp("smc", method)) { |
Heinrich Schuchardt | 26f09d0 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 211 | psci_method = PSCI_METHOD_SMC; |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 212 | } else { |
Masahiro Yamada | 8fa57ed | 2017-11-29 15:04:40 +0900 | [diff] [blame] | 213 | pr_warn("invalid \"method\" property: %s\n", method); |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 214 | return -EINVAL; |
| 215 | } |
| 216 | |
Etienne Carriere | 73b4eaf | 2022-06-01 10:27:33 +0200 | [diff] [blame] | 217 | return bind_smccc_features(dev, psci_method); |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 218 | } |
Heinrich Schuchardt | 26f09d0 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 219 | |
| 220 | /** |
| 221 | * void do_psci_probe() - probe PSCI firmware driver |
| 222 | * |
| 223 | * Ensure that psci_method is initialized. |
| 224 | */ |
| 225 | static void __maybe_unused do_psci_probe(void) |
| 226 | { |
| 227 | struct udevice *dev; |
| 228 | |
| 229 | uclass_get_device_by_name(UCLASS_FIRMWARE, DRIVER_NAME, &dev); |
| 230 | } |
| 231 | |
| 232 | #if IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) |
| 233 | efi_status_t efi_reset_system_init(void) |
| 234 | { |
| 235 | do_psci_probe(); |
| 236 | return EFI_SUCCESS; |
| 237 | } |
| 238 | |
| 239 | void __efi_runtime EFIAPI efi_reset_system(enum efi_reset_type reset_type, |
| 240 | efi_status_t reset_status, |
| 241 | unsigned long data_size, |
| 242 | void *reset_data) |
| 243 | { |
| 244 | if (reset_type == EFI_RESET_COLD || |
| 245 | reset_type == EFI_RESET_WARM || |
| 246 | reset_type == EFI_RESET_PLATFORM_SPECIFIC) { |
| 247 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0); |
| 248 | } else if (reset_type == EFI_RESET_SHUTDOWN) { |
| 249 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0); |
| 250 | } |
| 251 | while (1) |
| 252 | ; |
| 253 | } |
| 254 | #endif /* IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) */ |
| 255 | |
| 256 | #ifdef CONFIG_PSCI_RESET |
| 257 | void reset_misc(void) |
| 258 | { |
| 259 | do_psci_probe(); |
| 260 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0); |
| 261 | } |
| 262 | #endif /* CONFIG_PSCI_RESET */ |
| 263 | |
Igor Opaniuk | 3696c09 | 2021-04-01 02:01:53 +0300 | [diff] [blame] | 264 | void psci_sys_reset(u32 type) |
| 265 | { |
| 266 | bool reset2_supported; |
| 267 | |
| 268 | do_psci_probe(); |
| 269 | |
| 270 | reset2_supported = psci_is_system_reset2_supported(); |
| 271 | |
| 272 | if (type == SYSRESET_WARM && reset2_supported) { |
| 273 | /* |
| 274 | * reset_type[31] = 0 (architectural) |
| 275 | * reset_type[30:0] = 0 (SYSTEM_WARM_RESET) |
| 276 | * cookie = 0 (ignored by the implementation) |
| 277 | */ |
| 278 | invoke_psci_fn(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2), 0, 0, 0); |
| 279 | } else { |
| 280 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | void psci_sys_poweroff(void) |
| 285 | { |
| 286 | do_psci_probe(); |
| 287 | |
| 288 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0); |
| 289 | } |
| 290 | |
Michal Simek | f92175c | 2021-07-13 16:53:46 +0200 | [diff] [blame] | 291 | #if IS_ENABLED(CONFIG_CMD_POWEROFF) && !IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF) |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 292 | int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Heinrich Schuchardt | 26f09d0 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 293 | { |
| 294 | do_psci_probe(); |
| 295 | |
| 296 | puts("poweroff ...\n"); |
| 297 | udelay(50000); /* wait 50 ms */ |
| 298 | |
| 299 | disable_interrupts(); |
| 300 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0); |
| 301 | enable_interrupts(); |
| 302 | |
| 303 | log_err("Power off not supported on this platform\n"); |
| 304 | return CMD_RET_FAILURE; |
| 305 | } |
| 306 | #endif |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 307 | |
| 308 | static const struct udevice_id psci_of_match[] = { |
| 309 | { .compatible = "arm,psci" }, |
| 310 | { .compatible = "arm,psci-0.2" }, |
| 311 | { .compatible = "arm,psci-1.0" }, |
| 312 | {}, |
| 313 | }; |
| 314 | |
| 315 | U_BOOT_DRIVER(psci) = { |
Heinrich Schuchardt | 26f09d0 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 316 | .name = DRIVER_NAME, |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 317 | .id = UCLASS_FIRMWARE, |
| 318 | .of_match = psci_of_match, |
| 319 | .bind = psci_bind, |
| 320 | .probe = psci_probe, |
Etienne Carriere | 73b4eaf | 2022-06-01 10:27:33 +0200 | [diff] [blame] | 321 | #ifdef CONFIG_ARM_SMCCC_FEATURES |
| 322 | .plat_auto = sizeof(struct psci_plat_data), |
| 323 | #endif |
Peng Fan | a6fb3e8 | 2023-04-06 18:23:17 +0800 | [diff] [blame] | 324 | .flags = DM_FLAG_PRE_RELOC, |
Masahiro Yamada | 836c55d | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 325 | }; |