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 | |
Javier Almansa Sobrino | ed93259 | 2023-01-24 12:50:41 +0000 | [diff] [blame] | 6 | #include <assert.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 7 | #include <debug.h> |
| 8 | #include <errno.h> |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 9 | #include <gic.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 10 | #include <host_defs.h> |
| 11 | #include <host_utils.h> |
| 12 | #include <plat_common.h> |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 13 | #include <rmm_el3_ifc.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 14 | #include <string.h> |
| 15 | #include <xlat_tables.h> |
| 16 | |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 17 | static struct sysreg_data sysregs[SYSREG_MAX_CBS]; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 18 | static unsigned int installed_cb_idx; |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 19 | static unsigned int current_cpuid; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 20 | |
| 21 | /* |
| 22 | * Allocate memory to emulate physical memory to initialize the |
| 23 | * granule library. |
| 24 | */ |
| 25 | static unsigned char granules_buffer[HOST_MEM_SIZE] __aligned(GRANULE_SIZE); |
| 26 | |
| 27 | /* |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 28 | * Define and set the Boot Interface arguments. |
| 29 | */ |
| 30 | static unsigned char el3_rmm_shared_buffer[PAGE_SIZE] __aligned(PAGE_SIZE); |
| 31 | |
| 32 | /* |
| 33 | * Create a basic boot manifest. |
| 34 | */ |
| 35 | static struct rmm_core_manifest *boot_manifest = |
| 36 | (struct rmm_core_manifest *)el3_rmm_shared_buffer; |
| 37 | |
| 38 | /* |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 39 | * Generic callback to access a sysreg for reading. |
| 40 | */ |
| 41 | static u_register_t sysreg_rd_cb(u_register_t *reg) |
| 42 | { |
| 43 | return *reg; |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * Generic callback to access a sysreg for writing. |
| 48 | */ |
| 49 | static void sysreg_wr_cb(u_register_t val, u_register_t *reg) |
| 50 | { |
| 51 | *reg = val; |
| 52 | } |
| 53 | |
| 54 | struct sysreg_cb *host_util_get_sysreg_cb(char *name) |
| 55 | { |
| 56 | for (unsigned int i = 0U; i < SYSREG_MAX_CBS; i++) { |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 57 | if (strncmp(name, &sysregs[i].name[0], |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 58 | MAX_SYSREG_NAME_LEN) == 0) { |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | * Get a pointer to the register value for the |
| 62 | * current CPU. |
| 63 | */ |
| 64 | sysregs[i].callbacks.reg = |
| 65 | &(sysregs[i].value[current_cpuid]); |
| 66 | return &sysregs[i].callbacks; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | return (struct sysreg_cb *)NULL; |
| 71 | } |
| 72 | |
| 73 | int host_util_set_sysreg_cb(char *name, rd_cb_t rd_cb, wr_cb_t wr_cb, |
| 74 | u_register_t init) |
| 75 | { |
| 76 | if (installed_cb_idx < SYSREG_MAX_CBS) { |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 77 | sysregs[installed_cb_idx].callbacks.rd_cb = rd_cb; |
| 78 | sysregs[installed_cb_idx].callbacks.wr_cb = wr_cb; |
| 79 | sysregs[installed_cb_idx].callbacks.reg = |
| 80 | (u_register_t *)NULL; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 81 | |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 82 | for (unsigned int i = 0U; i < MAX_CPUS; i++) { |
| 83 | sysregs[installed_cb_idx].value[i] = init; |
| 84 | } |
| 85 | |
| 86 | (void)strncpy(&(sysregs[installed_cb_idx].name[0]), |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 87 | &name[0], MAX_SYSREG_NAME_LEN); |
| 88 | |
| 89 | /* |
| 90 | * Add a string termination character in case the |
| 91 | * name were truncated. |
| 92 | */ |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 93 | sysregs[installed_cb_idx].name[MAX_SYSREG_NAME_LEN] = '\0'; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 94 | |
| 95 | ++installed_cb_idx; |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | return -ENOMEM; |
| 101 | } |
| 102 | |
| 103 | void host_util_reset_all_sysreg_cb(void) |
| 104 | { |
| 105 | |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 106 | (void)memset((void *)sysregs, 0, |
| 107 | sizeof(struct sysreg_data) * SYSREG_MAX_CBS); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 108 | |
| 109 | installed_cb_idx = 0U; |
| 110 | } |
| 111 | |
| 112 | int host_util_set_default_sysreg_cb(char *name, u_register_t init) |
| 113 | { |
| 114 | return host_util_set_sysreg_cb(name, &sysreg_rd_cb, |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 115 | &sysreg_wr_cb, init); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | unsigned long host_util_get_granule_base(void) |
| 119 | { |
| 120 | return (unsigned long)granules_buffer; |
| 121 | } |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 122 | |
| 123 | void host_util_set_cpuid(unsigned int cpuid) |
| 124 | { |
| 125 | assert(cpuid < MAX_CPUS); |
| 126 | |
| 127 | current_cpuid = cpuid; |
| 128 | } |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 129 | |
| 130 | unsigned char *host_util_get_el3_rmm_shared_buffer(void) |
| 131 | { |
| 132 | return el3_rmm_shared_buffer; |
| 133 | } |
| 134 | |
| 135 | void host_util_setup_sysreg_and_boot_manifest(void) |
| 136 | { |
| 137 | int ret; |
| 138 | |
| 139 | /* |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame^] | 140 | * Initialize ID_AA64DFR0_EL1 with PMUVer field to PMUv3p7. |
| 141 | * (ID_AA64DFR0_EL1.PMUVer, bits [11:8] set to 7) |
| 142 | */ |
| 143 | ret = host_util_set_default_sysreg_cb("id_aa64dfr0_el1", |
| 144 | INPLACE(ID_AA64DFR0_EL1_PMUVer, 7UL)); |
| 145 | |
| 146 | /* |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 147 | * Initialize ID_AA64MMFR0_EL1 with a physical address |
| 148 | * range of 48 bits (PARange bits set to 0b0101) |
| 149 | */ |
| 150 | ret = host_util_set_default_sysreg_cb("id_aa64mmfr0_el1", |
| 151 | INPLACE(ID_AA64MMFR0_EL1_PARANGE, 5UL)); |
| 152 | |
| 153 | /* |
| 154 | * Initialize ICH_VTR_EL2 with 6 preemption bits. |
| 155 | * (PREbits is equal number of preemption bits minus one) |
| 156 | */ |
| 157 | ret = host_util_set_default_sysreg_cb("ich_vtr_el2", |
| 158 | INPLACE(ICH_VTR_EL2_PRE_BITS, 5UL)); |
| 159 | |
| 160 | /* SCTLR_EL2 is reset to zero */ |
| 161 | ret = host_util_set_default_sysreg_cb("sctlr_el2", 0UL); |
| 162 | |
| 163 | /* TPIDR_EL2 is reset to zero */ |
| 164 | ret = host_util_set_default_sysreg_cb("tpidr_el2", 0UL); |
| 165 | |
Mate Toth-Pal | 40193e4 | 2023-03-01 13:17:48 +0100 | [diff] [blame] | 166 | /* ID_AA64ISAR0.RNDR is reset to 1 */ |
| 167 | ret = host_util_set_default_sysreg_cb("id_aa64isar0_el1", |
| 168 | INPLACE(ID_AA64ISAR0_EL1_RNDR, 1UL)); |
| 169 | |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 170 | /* |
Mate Toth-Pal | 2611c55 | 2023-02-28 10:28:32 +0100 | [diff] [blame] | 171 | * Add callback to elr_el2 so that the realm entry point can be accessed |
| 172 | * by host_run_realm |
| 173 | */ |
| 174 | ret = host_util_set_default_sysreg_cb("elr_el2", 0UL); |
| 175 | |
| 176 | /* |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame^] | 177 | * Set number of event counters implemented to 31. |
| 178 | * (PMCR_EL0.N, bits [15:11] set to 31) |
| 179 | */ |
| 180 | ret = host_util_set_default_sysreg_cb("pmcr_el0", |
| 181 | INPLACE(PMCR_EL0_N, 31UL)); |
| 182 | |
| 183 | /* |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 184 | * Only check the return value of the last callback setup, to detect |
| 185 | * if we are out of callback slots. |
| 186 | */ |
| 187 | if (ret != 0) { |
| 188 | panic(); |
| 189 | } |
| 190 | |
| 191 | /* Initialize the boot manifest */ |
| 192 | boot_manifest->version = RMM_EL3_IFC_SUPPORTED_VERSION; |
| 193 | boot_manifest->plat_data = (uintptr_t)NULL; |
| 194 | } |