blob: 60f1577a099c1dbd81aded21014fed75093aee30 [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
4 */
5
Soby Mathewa694cca2023-04-24 03:42:45 +01006#include <arch_helpers.h>
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +00007#include <assert.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +00008#include <debug.h>
9#include <errno.h>
Mate Toth-Palaead06f2023-03-02 10:17:09 +010010#include <gic.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000011#include <host_defs.h>
12#include <host_utils.h>
13#include <plat_common.h>
Mate Toth-Palaead06f2023-03-02 10:17:09 +010014#include <rmm_el3_ifc.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000015#include <string.h>
Mate Toth-Pal0e936512023-10-19 15:02:20 +020016#include <utils_def.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000017#include <xlat_tables.h>
18
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000019static struct sysreg_data sysregs[SYSREG_MAX_CBS];
Javier Almansa Sobrino48d68a72023-03-02 12:26:39 +000020static struct sysreg_data sysregs_snapshot[SYSREG_MAX_CBS];
Soby Mathewb4c6df42022-11-09 11:13:29 +000021static unsigned int installed_cb_idx;
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000022static unsigned int current_cpuid;
Soby Mathewb4c6df42022-11-09 11:13:29 +000023
24/*
25 * Allocate memory to emulate physical memory to initialize the
26 * granule library.
27 */
Mate Toth-Pal0e936512023-10-19 15:02:20 +020028IF_NCBMC(static) unsigned char granules_buffer[HOST_MEM_SIZE] __aligned(GRANULE_SIZE);
Soby Mathewb4c6df42022-11-09 11:13:29 +000029
30/*
Mate Toth-Palaead06f2023-03-02 10:17:09 +010031 * Define and set the Boot Interface arguments.
32 */
33static unsigned char el3_rmm_shared_buffer[PAGE_SIZE] __aligned(PAGE_SIZE);
34
35/*
36 * Create a basic boot manifest.
37 */
38static struct rmm_core_manifest *boot_manifest =
39 (struct rmm_core_manifest *)el3_rmm_shared_buffer;
40
41/*
Soby Mathewb4c6df42022-11-09 11:13:29 +000042 * Generic callback to access a sysreg for reading.
43 */
44static 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 */
52static void sysreg_wr_cb(u_register_t val, u_register_t *reg)
53{
54 *reg = val;
55}
56
57struct sysreg_cb *host_util_get_sysreg_cb(char *name)
58{
59 for (unsigned int i = 0U; i < SYSREG_MAX_CBS; i++) {
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000060 if (strncmp(name, &sysregs[i].name[0],
Soby Mathewb4c6df42022-11-09 11:13:29 +000061 MAX_SYSREG_NAME_LEN) == 0) {
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000062
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 Mathewb4c6df42022-11-09 11:13:29 +000070 }
71 }
72
73 return (struct sysreg_cb *)NULL;
74}
75
76int 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 Sobrino9929a792022-11-22 16:11:13 +000080 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 Mathewb4c6df42022-11-09 11:13:29 +000084
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000085 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 Mathewb4c6df42022-11-09 11:13:29 +000090 &name[0], MAX_SYSREG_NAME_LEN);
91
92 /*
93 * Add a string termination character in case the
94 * name were truncated.
95 */
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000096 sysregs[installed_cb_idx].name[MAX_SYSREG_NAME_LEN] = '\0';
Soby Mathewb4c6df42022-11-09 11:13:29 +000097
98 ++installed_cb_idx;
99
100 return 0;
101 }
102
103 return -ENOMEM;
104}
105
Javier Almansa Sobrino48d68a72023-03-02 12:26:39 +0000106void 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
112void 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
118void host_util_zero_sysregs_and_cbs(void)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000119{
120
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +0000121 (void)memset((void *)sysregs, 0,
122 sizeof(struct sysreg_data) * SYSREG_MAX_CBS);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000123
124 installed_cb_idx = 0U;
125}
126
127int 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 Sobrino9929a792022-11-22 16:11:13 +0000130 &sysreg_wr_cb, init);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000131}
132
133unsigned long host_util_get_granule_base(void)
134{
135 return (unsigned long)granules_buffer;
136}
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +0000137
138void host_util_set_cpuid(unsigned int cpuid)
139{
140 assert(cpuid < MAX_CPUS);
141
142 current_cpuid = cpuid;
143}
Mate Toth-Palaead06f2023-03-02 10:17:09 +0100144
145unsigned char *host_util_get_el3_rmm_shared_buffer(void)
146{
147 return el3_rmm_shared_buffer;
148}
149
150void host_util_setup_sysreg_and_boot_manifest(void)
151{
152 int ret;
153
154 /*
AlexeiFedoroveaec0c42023-02-01 18:13:32 +0000155 * Initialize ID_AA64DFR0_EL1 with PMUVer field to PMUv3p7.
156 * (ID_AA64DFR0_EL1.PMUVer, bits [11:8] set to 7)
157 */
Chuyue Luoc06f2622023-11-03 15:18:14 +0000158 (void)host_util_set_default_sysreg_cb("id_aa64dfr0_el1",
AlexeiFedoroveaec0c42023-02-01 18:13:32 +0000159 INPLACE(ID_AA64DFR0_EL1_PMUVer, 7UL));
160
161 /*
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000162 * 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-Palaead06f2023-03-02 10:17:09 +0100165 */
Chuyue Luoc06f2622023-11-03 15:18:14 +0000166 (void)host_util_set_default_sysreg_cb("id_aa64mmfr0_el1",
Javier Almansa Sobrino2fa8abe2023-06-06 13:18:17 +0100167 INPLACE(ID_AA64MMFR0_EL1_PARANGE, 5UL) |
168 INPLACE(ID_AA64MMFR0_EL1_TGRAN4,
Javier Almansa Sobrino59f0ef62023-05-17 12:21:02 +0100169 ID_AA64MMFR0_EL1_TGRAN4_LPA2) |
170 INPLACE(ID_AA64MMFR0_EL1_TGRAN4_2,
171 ID_AA64MMFR0_EL1_TGRAN4_2_TGRAN4));
Mate Toth-Palaead06f2023-03-02 10:17:09 +0100172
173 /*
174 * Initialize ICH_VTR_EL2 with 6 preemption bits.
175 * (PREbits is equal number of preemption bits minus one)
176 */
Chuyue Luoc06f2622023-11-03 15:18:14 +0000177 (void)host_util_set_default_sysreg_cb("ich_vtr_el2",
Mate Toth-Palaead06f2023-03-02 10:17:09 +0100178 INPLACE(ICH_VTR_EL2_PRE_BITS, 5UL));
179
180 /* SCTLR_EL2 is reset to zero */
Chuyue Luoc06f2622023-11-03 15:18:14 +0000181 (void)host_util_set_default_sysreg_cb("sctlr_el2", 0UL);
Mate Toth-Palaead06f2023-03-02 10:17:09 +0100182
Javier Almansa Sobrinoc173f1e2023-03-21 12:53:08 +0000183 /* HCR_EL2 is reset to zero */
Chuyue Luoc06f2622023-11-03 15:18:14 +0000184 (void)host_util_set_default_sysreg_cb("hcr_el2", 0UL);
Javier Almansa Sobrinoc173f1e2023-03-21 12:53:08 +0000185
Mate Toth-Palaead06f2023-03-02 10:17:09 +0100186 /* TPIDR_EL2 is reset to zero */
Chuyue Luoc06f2622023-11-03 15:18:14 +0000187 (void)host_util_set_default_sysreg_cb("tpidr_el2", 0UL);
Mate Toth-Palaead06f2023-03-02 10:17:09 +0100188
Mate Toth-Pal40193e42023-03-01 13:17:48 +0100189 /* ID_AA64ISAR0.RNDR is reset to 1 */
Chuyue Luoc06f2622023-11-03 15:18:14 +0000190 (void)host_util_set_default_sysreg_cb("id_aa64isar0_el1",
Mate Toth-Pal40193e42023-03-01 13:17:48 +0100191 INPLACE(ID_AA64ISAR0_EL1_RNDR, 1UL));
192
Mate Toth-Palaead06f2023-03-02 10:17:09 +0100193 /*
Mate Toth-Pal2611c552023-02-28 10:28:32 +0100194 * Add callback to elr_el2 so that the realm entry point can be accessed
195 * by host_run_realm
196 */
Chuyue Luoc06f2622023-11-03 15:18:14 +0000197 (void)host_util_set_default_sysreg_cb("elr_el2", 0UL);
Mate Toth-Pal2611c552023-02-28 10:28:32 +0100198
199 /*
Soby Mathewa694cca2023-04-24 03:42:45 +0100200 * Add callback to esr_el2 so that the realm exceptions can be handled.
201 */
Chuyue Luoc06f2622023-11-03 15:18:14 +0000202 (void)host_util_set_default_sysreg_cb("esr_el2", 0UL);
Soby Mathewa694cca2023-04-24 03:42:45 +0100203
204 /*
AlexeiFedoroveaec0c42023-02-01 18:13:32 +0000205 * Set number of event counters implemented to 31.
206 * (PMCR_EL0.N, bits [15:11] set to 31)
207 */
Chuyue Luoc06f2622023-11-03 15:18:14 +0000208 (void)host_util_set_default_sysreg_cb("pmcr_el0",
AlexeiFedoroveaec0c42023-02-01 18:13:32 +0000209 INPLACE(PMCR_EL0_N, 31UL));
210
AlexeiFedorov862f96c2024-03-01 16:26:48 +0000211 /*
212 * Set DCZID_EL0 register with DZP = 0 and
Soby Mathew309e5372024-03-11 11:13:52 +0000213 * BS = 0b101 as GRANULE_SIZE on CBMC platform is 7.
AlexeiFedorov862f96c2024-03-01 16:26:48 +0000214 */
215 (void)host_util_set_default_sysreg_cb("dczid_el0",
Soby Mathew309e5372024-03-11 11:13:52 +0000216 INPLACE(DCZID_EL0_BS, 5UL));
AlexeiFedorov862f96c2024-03-01 16:26:48 +0000217
Soby Mathew76e9b452023-05-16 15:11:34 +0100218 /* Set ISR_EL1 to 0 */
219 ret = host_util_set_default_sysreg_cb("isr_el1", 0UL);
220
AlexeiFedoroveaec0c42023-02-01 18:13:32 +0000221 /*
Mate Toth-Palaead06f2023-03-02 10:17:09 +0100222 * 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 Mathewda3ecb12023-05-22 16:56:38 +0100230 boot_manifest->version = RMM_EL3_MANIFEST_MAKE_VERSION(
231 RMM_EL3_MANIFEST_VERS_MAJOR,
232 RMM_EL3_MANIFEST_VERS_MINOR);
Mate Toth-Palaead06f2023-03-02 10:17:09 +0100233 boot_manifest->plat_data = (uintptr_t)NULL;
234}
Soby Mathewa694cca2023-04-24 03:42:45 +0100235
236int 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
245int 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}