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