Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | 573a381 | 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 | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 10 | #include <command.h> |
Simon Glass | 9d92245 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 11 | #include <dm.h> |
Simon Glass | 36bf446 | 2019-11-14 12:57:42 -0700 | [diff] [blame] | 12 | #include <irq_func.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 14 | #include <dm/lists.h> |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 15 | #include <efi_loader.h> |
Igor Opaniuk | b7135b0 | 2021-04-01 02:01:53 +0300 | [diff] [blame] | 16 | #include <sysreset.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 17 | #include <linux/delay.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 18 | #include <linux/libfdt.h> |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 19 | #include <linux/arm-smccc.h> |
| 20 | #include <linux/errno.h> |
Masahiro Yamada | af4e6d3 | 2017-11-29 15:04:40 +0900 | [diff] [blame] | 21 | #include <linux/printk.h> |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 22 | #include <linux/psci.h> |
Michal Simek | 10e4d64 | 2020-08-13 12:43:22 +0200 | [diff] [blame] | 23 | #include <asm/system.h> |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 24 | |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 25 | #define DRIVER_NAME "psci" |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 26 | |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 27 | #define PSCI_METHOD_HVC 1 |
| 28 | #define PSCI_METHOD_SMC 2 |
| 29 | |
Igor Opaniuk | b7135b0 | 2021-04-01 02:01:53 +0300 | [diff] [blame] | 30 | /* |
| 31 | * While a 64-bit OS can make calls with SMC32 calling conventions, for some |
| 32 | * calls it is necessary to use SMC64 to pass or return 64-bit values. |
| 33 | * For such calls PSCI_FN_NATIVE(version, name) will choose the appropriate |
| 34 | * (native-width) function ID. |
| 35 | */ |
| 36 | #if defined(CONFIG_ARM64) |
| 37 | #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN64_##name |
| 38 | #else |
| 39 | #define PSCI_FN_NATIVE(version, name) PSCI_##version##_FN_##name |
| 40 | #endif |
| 41 | |
Yann Gautier | 5cc7df7 | 2020-07-17 14:20:15 +0200 | [diff] [blame] | 42 | #if CONFIG_IS_ENABLED(EFI_LOADER) |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 43 | int __efi_runtime_data psci_method; |
Yann Gautier | 5cc7df7 | 2020-07-17 14:20:15 +0200 | [diff] [blame] | 44 | #else |
| 45 | int psci_method __attribute__ ((section(".data"))); |
| 46 | #endif |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 47 | |
| 48 | unsigned long __efi_runtime invoke_psci_fn |
| 49 | (unsigned long function_id, unsigned long arg0, |
| 50 | unsigned long arg1, unsigned long arg2) |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 51 | { |
| 52 | struct arm_smccc_res res; |
| 53 | |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 54 | /* |
| 55 | * In the __efi_runtime we need to avoid the switch statement. In some |
| 56 | * cases the compiler creates lookup tables to implement switch. These |
| 57 | * tables are not correctly relocated when SetVirtualAddressMap is |
| 58 | * called. |
| 59 | */ |
| 60 | if (psci_method == PSCI_METHOD_SMC) |
| 61 | arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res); |
| 62 | else if (psci_method == PSCI_METHOD_HVC) |
| 63 | arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res); |
| 64 | else |
| 65 | res.a0 = PSCI_RET_DISABLED; |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 66 | return res.a0; |
| 67 | } |
| 68 | |
Igor Opaniuk | b7135b0 | 2021-04-01 02:01:53 +0300 | [diff] [blame] | 69 | static int psci_features(u32 psci_func_id) |
| 70 | { |
| 71 | return invoke_psci_fn(PSCI_1_0_FN_PSCI_FEATURES, |
| 72 | psci_func_id, 0, 0); |
| 73 | } |
| 74 | |
| 75 | static u32 psci_0_2_get_version(void) |
| 76 | { |
| 77 | return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0); |
| 78 | } |
| 79 | |
| 80 | static bool psci_is_system_reset2_supported(void) |
| 81 | { |
| 82 | int ret; |
| 83 | u32 ver; |
| 84 | |
| 85 | ver = psci_0_2_get_version(); |
| 86 | |
| 87 | if (PSCI_VERSION_MAJOR(ver) >= 1) { |
| 88 | ret = psci_features(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2)); |
| 89 | |
| 90 | if (ret != PSCI_RET_NOT_SUPPORTED) |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | return false; |
| 95 | } |
| 96 | |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 97 | static int psci_bind(struct udevice *dev) |
| 98 | { |
| 99 | /* No SYSTEM_RESET support for PSCI 0.1 */ |
Simon Glass | 911f3ae | 2017-05-18 20:08:57 -0600 | [diff] [blame] | 100 | if (device_is_compatible(dev, "arm,psci-0.2") || |
| 101 | device_is_compatible(dev, "arm,psci-1.0")) { |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 102 | int ret; |
| 103 | |
| 104 | /* bind psci-sysreset optionally */ |
| 105 | ret = device_bind_driver(dev, "psci-sysreset", "psci-sysreset", |
| 106 | NULL); |
| 107 | if (ret) |
Masahiro Yamada | af4e6d3 | 2017-11-29 15:04:40 +0900 | [diff] [blame] | 108 | pr_debug("PSCI System Reset was not bound.\n"); |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int psci_probe(struct udevice *dev) |
| 115 | { |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 116 | const char *method; |
| 117 | |
Michal Simek | 10e4d64 | 2020-08-13 12:43:22 +0200 | [diff] [blame] | 118 | #if defined(CONFIG_ARM64) |
| 119 | if (current_el() == 3) |
| 120 | return -EINVAL; |
| 121 | #endif |
| 122 | |
Jon Hunter | fb264ef | 2020-06-18 12:54:38 +0100 | [diff] [blame] | 123 | method = ofnode_read_string(dev_ofnode(dev), "method"); |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 124 | if (!method) { |
Masahiro Yamada | af4e6d3 | 2017-11-29 15:04:40 +0900 | [diff] [blame] | 125 | pr_warn("missing \"method\" property\n"); |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 126 | return -ENXIO; |
| 127 | } |
| 128 | |
| 129 | if (!strcmp("hvc", method)) { |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 130 | psci_method = PSCI_METHOD_HVC; |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 131 | } else if (!strcmp("smc", method)) { |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 132 | psci_method = PSCI_METHOD_SMC; |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 133 | } else { |
Masahiro Yamada | af4e6d3 | 2017-11-29 15:04:40 +0900 | [diff] [blame] | 134 | pr_warn("invalid \"method\" property: %s\n", method); |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 135 | return -EINVAL; |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 141 | /** |
| 142 | * void do_psci_probe() - probe PSCI firmware driver |
| 143 | * |
| 144 | * Ensure that psci_method is initialized. |
| 145 | */ |
| 146 | static void __maybe_unused do_psci_probe(void) |
| 147 | { |
| 148 | struct udevice *dev; |
| 149 | |
| 150 | uclass_get_device_by_name(UCLASS_FIRMWARE, DRIVER_NAME, &dev); |
| 151 | } |
| 152 | |
| 153 | #if IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) |
| 154 | efi_status_t efi_reset_system_init(void) |
| 155 | { |
| 156 | do_psci_probe(); |
| 157 | return EFI_SUCCESS; |
| 158 | } |
| 159 | |
| 160 | void __efi_runtime EFIAPI efi_reset_system(enum efi_reset_type reset_type, |
| 161 | efi_status_t reset_status, |
| 162 | unsigned long data_size, |
| 163 | void *reset_data) |
| 164 | { |
| 165 | if (reset_type == EFI_RESET_COLD || |
| 166 | reset_type == EFI_RESET_WARM || |
| 167 | reset_type == EFI_RESET_PLATFORM_SPECIFIC) { |
| 168 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0); |
| 169 | } else if (reset_type == EFI_RESET_SHUTDOWN) { |
| 170 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0); |
| 171 | } |
| 172 | while (1) |
| 173 | ; |
| 174 | } |
| 175 | #endif /* IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) */ |
| 176 | |
| 177 | #ifdef CONFIG_PSCI_RESET |
| 178 | void reset_misc(void) |
| 179 | { |
| 180 | do_psci_probe(); |
| 181 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0); |
| 182 | } |
| 183 | #endif /* CONFIG_PSCI_RESET */ |
| 184 | |
Igor Opaniuk | b7135b0 | 2021-04-01 02:01:53 +0300 | [diff] [blame] | 185 | void psci_sys_reset(u32 type) |
| 186 | { |
| 187 | bool reset2_supported; |
| 188 | |
| 189 | do_psci_probe(); |
| 190 | |
| 191 | reset2_supported = psci_is_system_reset2_supported(); |
| 192 | |
| 193 | if (type == SYSRESET_WARM && reset2_supported) { |
| 194 | /* |
| 195 | * reset_type[31] = 0 (architectural) |
| 196 | * reset_type[30:0] = 0 (SYSTEM_WARM_RESET) |
| 197 | * cookie = 0 (ignored by the implementation) |
| 198 | */ |
| 199 | invoke_psci_fn(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2), 0, 0, 0); |
| 200 | } else { |
| 201 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | void psci_sys_poweroff(void) |
| 206 | { |
| 207 | do_psci_probe(); |
| 208 | |
| 209 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0); |
| 210 | } |
| 211 | |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 212 | #ifdef CONFIG_CMD_POWEROFF |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 213 | int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 214 | { |
| 215 | do_psci_probe(); |
| 216 | |
| 217 | puts("poweroff ...\n"); |
| 218 | udelay(50000); /* wait 50 ms */ |
| 219 | |
| 220 | disable_interrupts(); |
| 221 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0); |
| 222 | enable_interrupts(); |
| 223 | |
| 224 | log_err("Power off not supported on this platform\n"); |
| 225 | return CMD_RET_FAILURE; |
| 226 | } |
| 227 | #endif |
| 228 | |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 229 | static const struct udevice_id psci_of_match[] = { |
| 230 | { .compatible = "arm,psci" }, |
| 231 | { .compatible = "arm,psci-0.2" }, |
| 232 | { .compatible = "arm,psci-1.0" }, |
| 233 | {}, |
| 234 | }; |
| 235 | |
| 236 | U_BOOT_DRIVER(psci) = { |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 237 | .name = DRIVER_NAME, |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 238 | .id = UCLASS_FIRMWARE, |
| 239 | .of_match = psci_of_match, |
| 240 | .bind = psci_bind, |
| 241 | .probe = psci_probe, |
| 242 | }; |