Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Beniamino Galvani | c7757d4 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com> |
| 4 | * |
Beniamino Galvani | c7757d4 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 5 | * Secure monitor calls. |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 9 | #include <command.h> |
| 10 | #include <env.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Neil Armstrong | 0ef8e40 | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 12 | #include <asm/arch/sm.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 13 | #include <asm/cache.h> |
Simon Glass | 25a5818 | 2020-05-10 11:40:06 -0600 | [diff] [blame] | 14 | #include <asm/ptrace.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 15 | #include <linux/bitops.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 16 | #include <linux/err.h> |
Beniamino Galvani | c7757d4 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 17 | #include <linux/kernel.h> |
Neil Armstrong | b1dd7de | 2019-08-06 17:28:36 +0200 | [diff] [blame] | 18 | #include <dm.h> |
| 19 | #include <linux/bitfield.h> |
| 20 | #include <regmap.h> |
| 21 | #include <syscon.h> |
Beniamino Galvani | c7757d4 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 22 | |
| 23 | #define FN_GET_SHARE_MEM_INPUT_BASE 0x82000020 |
| 24 | #define FN_GET_SHARE_MEM_OUTPUT_BASE 0x82000021 |
| 25 | #define FN_EFUSE_READ 0x82000030 |
| 26 | #define FN_EFUSE_WRITE 0x82000031 |
Neil Armstrong | 0ef8e40 | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 27 | #define FN_CHIP_ID 0x82000044 |
Beniamino Galvani | c7757d4 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 28 | |
| 29 | static void *shmem_input; |
| 30 | static void *shmem_output; |
| 31 | |
| 32 | static void meson_init_shmem(void) |
| 33 | { |
| 34 | struct pt_regs regs; |
| 35 | |
| 36 | if (shmem_input && shmem_output) |
| 37 | return; |
| 38 | |
| 39 | regs.regs[0] = FN_GET_SHARE_MEM_INPUT_BASE; |
| 40 | smc_call(®s); |
| 41 | shmem_input = (void *)regs.regs[0]; |
| 42 | |
| 43 | regs.regs[0] = FN_GET_SHARE_MEM_OUTPUT_BASE; |
| 44 | smc_call(®s); |
| 45 | shmem_output = (void *)regs.regs[0]; |
| 46 | |
| 47 | debug("Secure Monitor shmem: 0x%p 0x%p\n", shmem_input, shmem_output); |
| 48 | } |
| 49 | |
| 50 | ssize_t meson_sm_read_efuse(uintptr_t offset, void *buffer, size_t size) |
| 51 | { |
| 52 | struct pt_regs regs; |
| 53 | |
| 54 | meson_init_shmem(); |
| 55 | |
| 56 | regs.regs[0] = FN_EFUSE_READ; |
| 57 | regs.regs[1] = offset; |
| 58 | regs.regs[2] = size; |
| 59 | |
| 60 | smc_call(®s); |
| 61 | |
| 62 | if (regs.regs[0] == 0) |
| 63 | return -1; |
| 64 | |
| 65 | memcpy(buffer, shmem_output, min(size, regs.regs[0])); |
| 66 | |
| 67 | return regs.regs[0]; |
| 68 | } |
Neil Armstrong | 0ef8e40 | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 69 | |
| 70 | #define SM_CHIP_ID_LENGTH 119 |
| 71 | #define SM_CHIP_ID_OFFSET 4 |
| 72 | #define SM_CHIP_ID_SIZE 12 |
| 73 | |
| 74 | int meson_sm_get_serial(void *buffer, size_t size) |
| 75 | { |
| 76 | struct pt_regs regs; |
| 77 | |
| 78 | meson_init_shmem(); |
| 79 | |
| 80 | regs.regs[0] = FN_CHIP_ID; |
| 81 | regs.regs[1] = 0; |
| 82 | regs.regs[2] = 0; |
| 83 | |
| 84 | smc_call(®s); |
| 85 | |
| 86 | memcpy(buffer, shmem_output + SM_CHIP_ID_OFFSET, |
| 87 | min_t(size_t, size, SM_CHIP_ID_SIZE)); |
| 88 | |
| 89 | return 0; |
| 90 | } |
Neil Armstrong | 559e625 | 2019-08-06 17:21:02 +0200 | [diff] [blame] | 91 | |
Neil Armstrong | b1dd7de | 2019-08-06 17:28:36 +0200 | [diff] [blame] | 92 | #define AO_SEC_SD_CFG15 0xfc |
| 93 | #define REBOOT_REASON_MASK GENMASK(15, 12) |
| 94 | |
| 95 | int meson_sm_get_reboot_reason(void) |
| 96 | { |
| 97 | struct regmap *regmap; |
| 98 | int nodeoffset; |
| 99 | ofnode node; |
| 100 | unsigned int reason; |
| 101 | |
| 102 | /* find the offset of compatible node */ |
| 103 | nodeoffset = fdt_node_offset_by_compatible(gd->fdt_blob, -1, |
| 104 | "amlogic,meson-gx-ao-secure"); |
| 105 | if (nodeoffset < 0) { |
| 106 | printf("%s: failed to get amlogic,meson-gx-ao-secure\n", |
| 107 | __func__); |
| 108 | return -ENODEV; |
| 109 | } |
| 110 | |
| 111 | /* get regmap from the syscon node */ |
| 112 | node = offset_to_ofnode(nodeoffset); |
| 113 | regmap = syscon_node_to_regmap(node); |
| 114 | if (IS_ERR(regmap)) { |
| 115 | printf("%s: failed to get regmap\n", __func__); |
| 116 | return -EINVAL; |
| 117 | } |
| 118 | |
| 119 | regmap_read(regmap, AO_SEC_SD_CFG15, &reason); |
| 120 | |
| 121 | /* The SMC call is not used, we directly use AO_SEC_SD_CFG15 */ |
| 122 | return FIELD_GET(REBOOT_REASON_MASK, reason); |
| 123 | } |
| 124 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 125 | static int do_sm_serial(struct cmd_tbl *cmdtp, int flag, int argc, |
Neil Armstrong | 559e625 | 2019-08-06 17:21:02 +0200 | [diff] [blame] | 126 | char *const argv[]) |
| 127 | { |
| 128 | ulong address; |
| 129 | int ret; |
| 130 | |
| 131 | if (argc < 2) |
| 132 | return CMD_RET_USAGE; |
| 133 | |
| 134 | address = simple_strtoul(argv[1], NULL, 0); |
| 135 | |
| 136 | ret = meson_sm_get_serial((void *)address, SM_CHIP_ID_SIZE); |
| 137 | if (ret) |
| 138 | return CMD_RET_FAILURE; |
| 139 | |
| 140 | return CMD_RET_SUCCESS; |
| 141 | } |
| 142 | |
Neil Armstrong | b1dd7de | 2019-08-06 17:28:36 +0200 | [diff] [blame] | 143 | #define MAX_REBOOT_REASONS 14 |
| 144 | |
| 145 | static const char *reboot_reasons[MAX_REBOOT_REASONS] = { |
| 146 | [REBOOT_REASON_COLD] = "cold_boot", |
| 147 | [REBOOT_REASON_NORMAL] = "normal", |
| 148 | [REBOOT_REASON_RECOVERY] = "recovery", |
| 149 | [REBOOT_REASON_UPDATE] = "update", |
| 150 | [REBOOT_REASON_FASTBOOT] = "fastboot", |
| 151 | [REBOOT_REASON_SUSPEND_OFF] = "suspend_off", |
| 152 | [REBOOT_REASON_HIBERNATE] = "hibernate", |
| 153 | [REBOOT_REASON_BOOTLOADER] = "bootloader", |
| 154 | [REBOOT_REASON_SHUTDOWN_REBOOT] = "shutdown_reboot", |
| 155 | [REBOOT_REASON_RPMBP] = "rpmbp", |
| 156 | [REBOOT_REASON_CRASH_DUMP] = "crash_dump", |
| 157 | [REBOOT_REASON_KERNEL_PANIC] = "kernel_panic", |
| 158 | [REBOOT_REASON_WATCHDOG_REBOOT] = "watchdog_reboot", |
| 159 | }; |
| 160 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 161 | static int do_sm_reboot_reason(struct cmd_tbl *cmdtp, int flag, int argc, |
| 162 | char *const argv[]) |
Neil Armstrong | b1dd7de | 2019-08-06 17:28:36 +0200 | [diff] [blame] | 163 | { |
| 164 | const char *reason_str; |
| 165 | char *destarg = NULL; |
| 166 | int reason; |
| 167 | |
| 168 | if (argc > 1) |
| 169 | destarg = argv[1]; |
| 170 | |
| 171 | reason = meson_sm_get_reboot_reason(); |
| 172 | if (reason < 0) |
| 173 | return CMD_RET_FAILURE; |
| 174 | |
| 175 | if (reason >= MAX_REBOOT_REASONS || |
| 176 | !reboot_reasons[reason]) |
| 177 | reason_str = "unknown"; |
| 178 | else |
| 179 | reason_str = reboot_reasons[reason]; |
| 180 | |
| 181 | if (destarg) |
| 182 | env_set(destarg, reason_str); |
| 183 | else |
| 184 | printf("reboot reason: %s (%x)\n", reason_str, reason); |
| 185 | |
| 186 | return CMD_RET_SUCCESS; |
| 187 | } |
| 188 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 189 | static struct cmd_tbl cmd_sm_sub[] = { |
Neil Armstrong | 559e625 | 2019-08-06 17:21:02 +0200 | [diff] [blame] | 190 | U_BOOT_CMD_MKENT(serial, 2, 1, do_sm_serial, "", ""), |
Neil Armstrong | b1dd7de | 2019-08-06 17:28:36 +0200 | [diff] [blame] | 191 | U_BOOT_CMD_MKENT(reboot_reason, 1, 1, do_sm_reboot_reason, "", ""), |
Neil Armstrong | 559e625 | 2019-08-06 17:21:02 +0200 | [diff] [blame] | 192 | }; |
| 193 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 194 | static int do_sm(struct cmd_tbl *cmdtp, int flag, int argc, |
Neil Armstrong | 559e625 | 2019-08-06 17:21:02 +0200 | [diff] [blame] | 195 | char *const argv[]) |
| 196 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 197 | struct cmd_tbl *c; |
Neil Armstrong | 559e625 | 2019-08-06 17:21:02 +0200 | [diff] [blame] | 198 | |
| 199 | if (argc < 2) |
| 200 | return CMD_RET_USAGE; |
| 201 | |
| 202 | /* Strip off leading 'sm' command argument */ |
| 203 | argc--; |
| 204 | argv++; |
| 205 | |
| 206 | c = find_cmd_tbl(argv[0], &cmd_sm_sub[0], ARRAY_SIZE(cmd_sm_sub)); |
| 207 | |
| 208 | if (c) |
| 209 | return c->cmd(cmdtp, flag, argc, argv); |
| 210 | else |
| 211 | return CMD_RET_USAGE; |
| 212 | } |
| 213 | |
| 214 | U_BOOT_CMD( |
| 215 | sm, 5, 0, do_sm, |
| 216 | "Secure Monitor Control", |
Neil Armstrong | b1dd7de | 2019-08-06 17:28:36 +0200 | [diff] [blame] | 217 | "serial <address> - read chip unique id to memory address\n" |
| 218 | "sm reboot_reason [name] - get reboot reason and store to to environment" |
Neil Armstrong | 559e625 | 2019-08-06 17:21:02 +0200 | [diff] [blame] | 219 | ); |