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> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 16 | #include <linux/delay.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 17 | #include <linux/libfdt.h> |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 18 | #include <linux/arm-smccc.h> |
| 19 | #include <linux/errno.h> |
Masahiro Yamada | af4e6d3 | 2017-11-29 15:04:40 +0900 | [diff] [blame] | 20 | #include <linux/printk.h> |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 21 | #include <linux/psci.h> |
Michal Simek | 10e4d64 | 2020-08-13 12:43:22 +0200 | [diff] [blame] | 22 | #include <asm/system.h> |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 23 | |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 24 | #define DRIVER_NAME "psci" |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 25 | |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 26 | #define PSCI_METHOD_HVC 1 |
| 27 | #define PSCI_METHOD_SMC 2 |
| 28 | |
Yann Gautier | 5cc7df7 | 2020-07-17 14:20:15 +0200 | [diff] [blame] | 29 | #if CONFIG_IS_ENABLED(EFI_LOADER) |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 30 | int __efi_runtime_data psci_method; |
Yann Gautier | 5cc7df7 | 2020-07-17 14:20:15 +0200 | [diff] [blame] | 31 | #else |
| 32 | int psci_method __attribute__ ((section(".data"))); |
| 33 | #endif |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 34 | |
| 35 | unsigned long __efi_runtime invoke_psci_fn |
| 36 | (unsigned long function_id, unsigned long arg0, |
| 37 | unsigned long arg1, unsigned long arg2) |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 38 | { |
| 39 | struct arm_smccc_res res; |
| 40 | |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 41 | /* |
| 42 | * In the __efi_runtime we need to avoid the switch statement. In some |
| 43 | * cases the compiler creates lookup tables to implement switch. These |
| 44 | * tables are not correctly relocated when SetVirtualAddressMap is |
| 45 | * called. |
| 46 | */ |
| 47 | if (psci_method == PSCI_METHOD_SMC) |
| 48 | arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res); |
| 49 | else if (psci_method == PSCI_METHOD_HVC) |
| 50 | arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res); |
| 51 | else |
| 52 | res.a0 = PSCI_RET_DISABLED; |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 53 | return res.a0; |
| 54 | } |
| 55 | |
| 56 | static int psci_bind(struct udevice *dev) |
| 57 | { |
| 58 | /* No SYSTEM_RESET support for PSCI 0.1 */ |
Simon Glass | 911f3ae | 2017-05-18 20:08:57 -0600 | [diff] [blame] | 59 | if (device_is_compatible(dev, "arm,psci-0.2") || |
| 60 | device_is_compatible(dev, "arm,psci-1.0")) { |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 61 | int ret; |
| 62 | |
| 63 | /* bind psci-sysreset optionally */ |
| 64 | ret = device_bind_driver(dev, "psci-sysreset", "psci-sysreset", |
| 65 | NULL); |
| 66 | if (ret) |
Masahiro Yamada | af4e6d3 | 2017-11-29 15:04:40 +0900 | [diff] [blame] | 67 | pr_debug("PSCI System Reset was not bound.\n"); |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int psci_probe(struct udevice *dev) |
| 74 | { |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 75 | const char *method; |
| 76 | |
Michal Simek | 10e4d64 | 2020-08-13 12:43:22 +0200 | [diff] [blame] | 77 | #if defined(CONFIG_ARM64) |
| 78 | if (current_el() == 3) |
| 79 | return -EINVAL; |
| 80 | #endif |
| 81 | |
Jon Hunter | fb264ef | 2020-06-18 12:54:38 +0100 | [diff] [blame] | 82 | method = ofnode_read_string(dev_ofnode(dev), "method"); |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 83 | if (!method) { |
Masahiro Yamada | af4e6d3 | 2017-11-29 15:04:40 +0900 | [diff] [blame] | 84 | pr_warn("missing \"method\" property\n"); |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 85 | return -ENXIO; |
| 86 | } |
| 87 | |
| 88 | if (!strcmp("hvc", method)) { |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 89 | psci_method = PSCI_METHOD_HVC; |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 90 | } else if (!strcmp("smc", method)) { |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 91 | psci_method = PSCI_METHOD_SMC; |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 92 | } else { |
Masahiro Yamada | af4e6d3 | 2017-11-29 15:04:40 +0900 | [diff] [blame] | 93 | pr_warn("invalid \"method\" property: %s\n", method); |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 94 | return -EINVAL; |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 100 | /** |
| 101 | * void do_psci_probe() - probe PSCI firmware driver |
| 102 | * |
| 103 | * Ensure that psci_method is initialized. |
| 104 | */ |
| 105 | static void __maybe_unused do_psci_probe(void) |
| 106 | { |
| 107 | struct udevice *dev; |
| 108 | |
| 109 | uclass_get_device_by_name(UCLASS_FIRMWARE, DRIVER_NAME, &dev); |
| 110 | } |
| 111 | |
| 112 | #if IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) |
| 113 | efi_status_t efi_reset_system_init(void) |
| 114 | { |
| 115 | do_psci_probe(); |
| 116 | return EFI_SUCCESS; |
| 117 | } |
| 118 | |
| 119 | void __efi_runtime EFIAPI efi_reset_system(enum efi_reset_type reset_type, |
| 120 | efi_status_t reset_status, |
| 121 | unsigned long data_size, |
| 122 | void *reset_data) |
| 123 | { |
| 124 | if (reset_type == EFI_RESET_COLD || |
| 125 | reset_type == EFI_RESET_WARM || |
| 126 | reset_type == EFI_RESET_PLATFORM_SPECIFIC) { |
| 127 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0); |
| 128 | } else if (reset_type == EFI_RESET_SHUTDOWN) { |
| 129 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0); |
| 130 | } |
| 131 | while (1) |
| 132 | ; |
| 133 | } |
| 134 | #endif /* IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) */ |
| 135 | |
| 136 | #ifdef CONFIG_PSCI_RESET |
| 137 | void reset_misc(void) |
| 138 | { |
| 139 | do_psci_probe(); |
| 140 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0); |
| 141 | } |
| 142 | #endif /* CONFIG_PSCI_RESET */ |
| 143 | |
| 144 | #ifdef CONFIG_CMD_POWEROFF |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 145 | 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] | 146 | { |
| 147 | do_psci_probe(); |
| 148 | |
| 149 | puts("poweroff ...\n"); |
| 150 | udelay(50000); /* wait 50 ms */ |
| 151 | |
| 152 | disable_interrupts(); |
| 153 | invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0); |
| 154 | enable_interrupts(); |
| 155 | |
| 156 | log_err("Power off not supported on this platform\n"); |
| 157 | return CMD_RET_FAILURE; |
| 158 | } |
| 159 | #endif |
| 160 | |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 161 | static const struct udevice_id psci_of_match[] = { |
| 162 | { .compatible = "arm,psci" }, |
| 163 | { .compatible = "arm,psci-0.2" }, |
| 164 | { .compatible = "arm,psci-1.0" }, |
| 165 | {}, |
| 166 | }; |
| 167 | |
| 168 | U_BOOT_DRIVER(psci) = { |
Heinrich Schuchardt | 81ea008 | 2018-10-18 12:29:40 +0200 | [diff] [blame] | 169 | .name = DRIVER_NAME, |
Masahiro Yamada | 573a381 | 2017-04-14 11:10:24 +0900 | [diff] [blame] | 170 | .id = UCLASS_FIRMWARE, |
| 171 | .of_match = psci_of_match, |
| 172 | .bind = psci_bind, |
| 173 | .probe = psci_probe, |
| 174 | }; |