Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 4 | */ |
| 5 | |
Soby Mathew | a694cca | 2023-04-24 03:42:45 +0100 | [diff] [blame] | 6 | #include <arch_helpers.h> |
Javier Almansa Sobrino | ed93259 | 2023-01-24 12:50:41 +0000 | [diff] [blame] | 7 | #include <assert.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 8 | #include <debug.h> |
| 9 | #include <errno.h> |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 10 | #include <gic.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 11 | #include <host_defs.h> |
| 12 | #include <host_utils.h> |
| 13 | #include <plat_common.h> |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 14 | #include <rmm_el3_ifc.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 15 | #include <string.h> |
Mate Toth-Pal | 0e93651 | 2023-10-19 15:02:20 +0200 | [diff] [blame] | 16 | #include <utils_def.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 17 | #include <xlat_tables.h> |
| 18 | |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 19 | static struct sysreg_data sysregs[SYSREG_MAX_CBS]; |
Javier Almansa Sobrino | 48d68a7 | 2023-03-02 12:26:39 +0000 | [diff] [blame] | 20 | static struct sysreg_data sysregs_snapshot[SYSREG_MAX_CBS]; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 21 | static unsigned int installed_cb_idx; |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 22 | static unsigned int current_cpuid; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | * Allocate memory to emulate physical memory to initialize the |
| 26 | * granule library. |
| 27 | */ |
AlexeiFedorov | 037add6 | 2024-10-30 15:53:05 +0000 | [diff] [blame^] | 28 | IF_NCBMC(static) unsigned char host_dram_buffer[HOST_DRAM_SIZE] |
| 29 | __aligned(GRANULE_SIZE); |
| 30 | IF_NCBMC(static) unsigned char host_dev_ncoh_buffer[HOST_NCOH_DEV_SIZE] |
| 31 | __aligned(GRANULE_SIZE); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 32 | |
| 33 | /* |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 34 | * Define and set the Boot Interface arguments. |
| 35 | */ |
| 36 | static unsigned char el3_rmm_shared_buffer[PAGE_SIZE] __aligned(PAGE_SIZE); |
| 37 | |
| 38 | /* |
| 39 | * Create a basic boot manifest. |
| 40 | */ |
| 41 | static struct rmm_core_manifest *boot_manifest = |
| 42 | (struct rmm_core_manifest *)el3_rmm_shared_buffer; |
| 43 | |
| 44 | /* |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 45 | * Generic callback to access a sysreg for reading. |
| 46 | */ |
| 47 | static u_register_t sysreg_rd_cb(u_register_t *reg) |
| 48 | { |
| 49 | return *reg; |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * Generic callback to access a sysreg for writing. |
| 54 | */ |
| 55 | static void sysreg_wr_cb(u_register_t val, u_register_t *reg) |
| 56 | { |
| 57 | *reg = val; |
| 58 | } |
| 59 | |
| 60 | struct sysreg_cb *host_util_get_sysreg_cb(char *name) |
| 61 | { |
| 62 | for (unsigned int i = 0U; i < SYSREG_MAX_CBS; i++) { |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 63 | if (strncmp(name, &sysregs[i].name[0], |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 64 | MAX_SYSREG_NAME_LEN) == 0) { |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Get a pointer to the register value for the |
| 68 | * current CPU. |
| 69 | */ |
| 70 | sysregs[i].callbacks.reg = |
| 71 | &(sysregs[i].value[current_cpuid]); |
| 72 | return &sysregs[i].callbacks; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | return (struct sysreg_cb *)NULL; |
| 77 | } |
| 78 | |
| 79 | int host_util_set_sysreg_cb(char *name, rd_cb_t rd_cb, wr_cb_t wr_cb, |
| 80 | u_register_t init) |
| 81 | { |
| 82 | if (installed_cb_idx < SYSREG_MAX_CBS) { |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 83 | sysregs[installed_cb_idx].callbacks.rd_cb = rd_cb; |
| 84 | sysregs[installed_cb_idx].callbacks.wr_cb = wr_cb; |
| 85 | sysregs[installed_cb_idx].callbacks.reg = |
| 86 | (u_register_t *)NULL; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 87 | |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 88 | for (unsigned int i = 0U; i < MAX_CPUS; i++) { |
| 89 | sysregs[installed_cb_idx].value[i] = init; |
| 90 | } |
| 91 | |
| 92 | (void)strncpy(&(sysregs[installed_cb_idx].name[0]), |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 93 | &name[0], MAX_SYSREG_NAME_LEN); |
| 94 | |
| 95 | /* |
| 96 | * Add a string termination character in case the |
| 97 | * name were truncated. |
| 98 | */ |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 99 | sysregs[installed_cb_idx].name[MAX_SYSREG_NAME_LEN] = '\0'; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 100 | |
| 101 | ++installed_cb_idx; |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | return -ENOMEM; |
| 107 | } |
| 108 | |
Javier Almansa Sobrino | 48d68a7 | 2023-03-02 12:26:39 +0000 | [diff] [blame] | 109 | void host_util_take_sysreg_snapshot(void) |
| 110 | { |
| 111 | memcpy((void *)&sysregs_snapshot[0], (void *)&sysregs[0], |
| 112 | sizeof(struct sysreg_data) * SYSREG_MAX_CBS); |
| 113 | } |
| 114 | |
| 115 | void host_util_restore_sysreg_snapshot(void) |
| 116 | { |
| 117 | memcpy((void *)&sysregs[0], (void *)&sysregs_snapshot[0], |
| 118 | sizeof(struct sysreg_data) * SYSREG_MAX_CBS); |
| 119 | } |
| 120 | |
| 121 | void host_util_zero_sysregs_and_cbs(void) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 122 | { |
| 123 | |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 124 | (void)memset((void *)sysregs, 0, |
| 125 | sizeof(struct sysreg_data) * SYSREG_MAX_CBS); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 126 | |
| 127 | installed_cb_idx = 0U; |
| 128 | } |
| 129 | |
| 130 | int host_util_set_default_sysreg_cb(char *name, u_register_t init) |
| 131 | { |
| 132 | return host_util_set_sysreg_cb(name, &sysreg_rd_cb, |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 133 | &sysreg_wr_cb, init); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | unsigned long host_util_get_granule_base(void) |
| 137 | { |
AlexeiFedorov | 037add6 | 2024-10-30 15:53:05 +0000 | [diff] [blame^] | 138 | return (unsigned long)host_dram_buffer; |
| 139 | } |
| 140 | |
| 141 | unsigned long host_util_get_dev_granule_base(void) |
| 142 | { |
| 143 | return (unsigned long)host_dev_ncoh_buffer; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 144 | } |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 145 | |
| 146 | void host_util_set_cpuid(unsigned int cpuid) |
| 147 | { |
| 148 | assert(cpuid < MAX_CPUS); |
| 149 | |
| 150 | current_cpuid = cpuid; |
| 151 | } |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 152 | |
| 153 | unsigned char *host_util_get_el3_rmm_shared_buffer(void) |
| 154 | { |
| 155 | return el3_rmm_shared_buffer; |
| 156 | } |
| 157 | |
| 158 | void host_util_setup_sysreg_and_boot_manifest(void) |
| 159 | { |
| 160 | int ret; |
| 161 | |
| 162 | /* |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 163 | * Initialize ID_AA64DFR0_EL1 with PMUVer field to PMUv3p7. |
Soby Mathew | 0685a15 | 2024-09-20 16:04:04 +0100 | [diff] [blame] | 164 | * (ID_AA64DFR0_EL1.PMUVer, bits [11:8] set to 7). |
| 165 | * Also setup minimum allowed by architecture number of watchpoints |
| 166 | * (ID_AA64DFR0_EL1.WRPs, bits [15:12] set to 1) |
| 167 | * and breakpoints. |
| 168 | * (ID_AA64DFR0_EL1.BRPs, bits [23:20] set to 1) |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 169 | */ |
Chuyue Luo | c06f262 | 2023-11-03 15:18:14 +0000 | [diff] [blame] | 170 | (void)host_util_set_default_sysreg_cb("id_aa64dfr0_el1", |
Soby Mathew | 0685a15 | 2024-09-20 16:04:04 +0100 | [diff] [blame] | 171 | INPLACE(ID_AA64DFR0_EL1_PMUVer, 7UL) | |
| 172 | INPLACE(ID_AA64DFR0_EL1_WRPs, 1UL) | |
| 173 | INPLACE(ID_AA64DFR0_EL1_BRPs, 1UL)); |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 174 | |
| 175 | /* |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 176 | * Initialize ID_AA64MMFR0_EL1 with a physical address |
| 177 | * range of 48 bits (PARange bits set to 0b0101) and |
| 178 | * support for 52bits PA size with 4KB granularity; |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 179 | */ |
Chuyue Luo | c06f262 | 2023-11-03 15:18:14 +0000 | [diff] [blame] | 180 | (void)host_util_set_default_sysreg_cb("id_aa64mmfr0_el1", |
Javier Almansa Sobrino | 2fa8abe | 2023-06-06 13:18:17 +0100 | [diff] [blame] | 181 | INPLACE(ID_AA64MMFR0_EL1_PARANGE, 5UL) | |
| 182 | INPLACE(ID_AA64MMFR0_EL1_TGRAN4, |
Javier Almansa Sobrino | 59f0ef6 | 2023-05-17 12:21:02 +0100 | [diff] [blame] | 183 | ID_AA64MMFR0_EL1_TGRAN4_LPA2) | |
| 184 | INPLACE(ID_AA64MMFR0_EL1_TGRAN4_2, |
| 185 | ID_AA64MMFR0_EL1_TGRAN4_2_TGRAN4)); |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * Initialize ICH_VTR_EL2 with 6 preemption bits. |
| 189 | * (PREbits is equal number of preemption bits minus one) |
| 190 | */ |
Chuyue Luo | c06f262 | 2023-11-03 15:18:14 +0000 | [diff] [blame] | 191 | (void)host_util_set_default_sysreg_cb("ich_vtr_el2", |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 192 | INPLACE(ICH_VTR_EL2_PRE_BITS, 5UL)); |
| 193 | |
| 194 | /* SCTLR_EL2 is reset to zero */ |
Chuyue Luo | c06f262 | 2023-11-03 15:18:14 +0000 | [diff] [blame] | 195 | (void)host_util_set_default_sysreg_cb("sctlr_el2", 0UL); |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 196 | |
Javier Almansa Sobrino | c173f1e | 2023-03-21 12:53:08 +0000 | [diff] [blame] | 197 | /* HCR_EL2 is reset to zero */ |
Chuyue Luo | c06f262 | 2023-11-03 15:18:14 +0000 | [diff] [blame] | 198 | (void)host_util_set_default_sysreg_cb("hcr_el2", 0UL); |
Javier Almansa Sobrino | c173f1e | 2023-03-21 12:53:08 +0000 | [diff] [blame] | 199 | |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 200 | /* TPIDR_EL2 is reset to zero */ |
Chuyue Luo | c06f262 | 2023-11-03 15:18:14 +0000 | [diff] [blame] | 201 | (void)host_util_set_default_sysreg_cb("tpidr_el2", 0UL); |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 202 | |
Mate Toth-Pal | 40193e4 | 2023-03-01 13:17:48 +0100 | [diff] [blame] | 203 | /* ID_AA64ISAR0.RNDR is reset to 1 */ |
Chuyue Luo | c06f262 | 2023-11-03 15:18:14 +0000 | [diff] [blame] | 204 | (void)host_util_set_default_sysreg_cb("id_aa64isar0_el1", |
Mate Toth-Pal | 40193e4 | 2023-03-01 13:17:48 +0100 | [diff] [blame] | 205 | INPLACE(ID_AA64ISAR0_EL1_RNDR, 1UL)); |
| 206 | |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 207 | /* |
Mate Toth-Pal | 2611c55 | 2023-02-28 10:28:32 +0100 | [diff] [blame] | 208 | * Add callback to elr_el2 so that the realm entry point can be accessed |
| 209 | * by host_run_realm |
| 210 | */ |
Chuyue Luo | c06f262 | 2023-11-03 15:18:14 +0000 | [diff] [blame] | 211 | (void)host_util_set_default_sysreg_cb("elr_el2", 0UL); |
Mate Toth-Pal | 2611c55 | 2023-02-28 10:28:32 +0100 | [diff] [blame] | 212 | |
| 213 | /* |
Soby Mathew | a694cca | 2023-04-24 03:42:45 +0100 | [diff] [blame] | 214 | * Add callback to esr_el2 so that the realm exceptions can be handled. |
| 215 | */ |
Chuyue Luo | c06f262 | 2023-11-03 15:18:14 +0000 | [diff] [blame] | 216 | (void)host_util_set_default_sysreg_cb("esr_el2", 0UL); |
Soby Mathew | a694cca | 2023-04-24 03:42:45 +0100 | [diff] [blame] | 217 | |
| 218 | /* |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 219 | * Set number of event counters implemented to 31. |
| 220 | * (PMCR_EL0.N, bits [15:11] set to 31) |
| 221 | */ |
Chuyue Luo | c06f262 | 2023-11-03 15:18:14 +0000 | [diff] [blame] | 222 | (void)host_util_set_default_sysreg_cb("pmcr_el0", |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 223 | INPLACE(PMCR_EL0_N, 31UL)); |
| 224 | |
AlexeiFedorov | 862f96c | 2024-03-01 16:26:48 +0000 | [diff] [blame] | 225 | /* |
| 226 | * Set DCZID_EL0 register with DZP = 0 and |
Soby Mathew | 309e537 | 2024-03-11 11:13:52 +0000 | [diff] [blame] | 227 | * BS = 0b101 as GRANULE_SIZE on CBMC platform is 7. |
AlexeiFedorov | 862f96c | 2024-03-01 16:26:48 +0000 | [diff] [blame] | 228 | */ |
| 229 | (void)host_util_set_default_sysreg_cb("dczid_el0", |
Soby Mathew | 309e537 | 2024-03-11 11:13:52 +0000 | [diff] [blame] | 230 | INPLACE(DCZID_EL0_BS, 5UL)); |
AlexeiFedorov | 862f96c | 2024-03-01 16:26:48 +0000 | [diff] [blame] | 231 | |
Soby Mathew | 76e9b45 | 2023-05-16 15:11:34 +0100 | [diff] [blame] | 232 | /* Set ISR_EL1 to 0 */ |
| 233 | ret = host_util_set_default_sysreg_cb("isr_el1", 0UL); |
| 234 | |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 235 | /* |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 236 | * Only check the return value of the last callback setup, to detect |
| 237 | * if we are out of callback slots. |
| 238 | */ |
| 239 | if (ret != 0) { |
| 240 | panic(); |
| 241 | } |
| 242 | |
| 243 | /* Initialize the boot manifest */ |
Soby Mathew | da3ecb1 | 2023-05-22 16:56:38 +0100 | [diff] [blame] | 244 | boot_manifest->version = RMM_EL3_MANIFEST_MAKE_VERSION( |
| 245 | RMM_EL3_MANIFEST_VERS_MAJOR, |
| 246 | RMM_EL3_MANIFEST_VERS_MINOR); |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 247 | boot_manifest->plat_data = (uintptr_t)NULL; |
| 248 | } |
Soby Mathew | a694cca | 2023-04-24 03:42:45 +0100 | [diff] [blame] | 249 | |
| 250 | int host_util_rec_run(unsigned long *regs) |
| 251 | { |
| 252 | unsigned long pc = read_elr_el2(); |
| 253 | realm_entrypoint_t realm_ep = (void *)pc; |
| 254 | |
| 255 | write_esr_el2(0x0); |
| 256 | return realm_ep(regs); |
| 257 | } |
| 258 | |
| 259 | int host_util_rsi_helper(realm_entrypoint_t ep) |
| 260 | { |
| 261 | /* Reduce the ep by 0x4 as RMM will advance_pc as part of handling RSI */ |
| 262 | write_elr_el2((u_register_t) ep - 0x4); |
| 263 | write_esr_el2(ESR_EL2_EC_SMC); |
| 264 | |
| 265 | return ARM_EXCEPTION_SYNC_LEL; |
| 266 | } |