blob: 8b0b08eafdf4bb1bf0eb80beaaa89a6299931af5 [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
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +00006#include <assert.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +00007#include <debug.h>
8#include <errno.h>
Mate Toth-Palaead06f2023-03-02 10:17:09 +01009#include <gic.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000010#include <host_defs.h>
11#include <host_utils.h>
12#include <plat_common.h>
Mate Toth-Palaead06f2023-03-02 10:17:09 +010013#include <rmm_el3_ifc.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000014#include <string.h>
15#include <xlat_tables.h>
16
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000017static struct sysreg_data sysregs[SYSREG_MAX_CBS];
Javier Almansa Sobrino48d68a72023-03-02 12:26:39 +000018static struct sysreg_data sysregs_snapshot[SYSREG_MAX_CBS];
Soby Mathewb4c6df42022-11-09 11:13:29 +000019static unsigned int installed_cb_idx;
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000020static unsigned int current_cpuid;
Soby Mathewb4c6df42022-11-09 11:13:29 +000021
22/*
23 * Allocate memory to emulate physical memory to initialize the
24 * granule library.
25 */
26static unsigned char granules_buffer[HOST_MEM_SIZE] __aligned(GRANULE_SIZE);
27
28/*
Mate Toth-Palaead06f2023-03-02 10:17:09 +010029 * Define and set the Boot Interface arguments.
30 */
31static unsigned char el3_rmm_shared_buffer[PAGE_SIZE] __aligned(PAGE_SIZE);
32
33/*
34 * Create a basic boot manifest.
35 */
36static struct rmm_core_manifest *boot_manifest =
37 (struct rmm_core_manifest *)el3_rmm_shared_buffer;
38
39/*
Soby Mathewb4c6df42022-11-09 11:13:29 +000040 * Generic callback to access a sysreg for reading.
41 */
42static u_register_t sysreg_rd_cb(u_register_t *reg)
43{
44 return *reg;
45}
46
47/*
48 * Generic callback to access a sysreg for writing.
49 */
50static void sysreg_wr_cb(u_register_t val, u_register_t *reg)
51{
52 *reg = val;
53}
54
55struct sysreg_cb *host_util_get_sysreg_cb(char *name)
56{
57 for (unsigned int i = 0U; i < SYSREG_MAX_CBS; i++) {
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000058 if (strncmp(name, &sysregs[i].name[0],
Soby Mathewb4c6df42022-11-09 11:13:29 +000059 MAX_SYSREG_NAME_LEN) == 0) {
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000060
61 /*
62 * Get a pointer to the register value for the
63 * current CPU.
64 */
65 sysregs[i].callbacks.reg =
66 &(sysregs[i].value[current_cpuid]);
67 return &sysregs[i].callbacks;
Soby Mathewb4c6df42022-11-09 11:13:29 +000068 }
69 }
70
71 return (struct sysreg_cb *)NULL;
72}
73
74int host_util_set_sysreg_cb(char *name, rd_cb_t rd_cb, wr_cb_t wr_cb,
75 u_register_t init)
76{
77 if (installed_cb_idx < SYSREG_MAX_CBS) {
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000078 sysregs[installed_cb_idx].callbacks.rd_cb = rd_cb;
79 sysregs[installed_cb_idx].callbacks.wr_cb = wr_cb;
80 sysregs[installed_cb_idx].callbacks.reg =
81 (u_register_t *)NULL;
Soby Mathewb4c6df42022-11-09 11:13:29 +000082
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000083 for (unsigned int i = 0U; i < MAX_CPUS; i++) {
84 sysregs[installed_cb_idx].value[i] = init;
85 }
86
87 (void)strncpy(&(sysregs[installed_cb_idx].name[0]),
Soby Mathewb4c6df42022-11-09 11:13:29 +000088 &name[0], MAX_SYSREG_NAME_LEN);
89
90 /*
91 * Add a string termination character in case the
92 * name were truncated.
93 */
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000094 sysregs[installed_cb_idx].name[MAX_SYSREG_NAME_LEN] = '\0';
Soby Mathewb4c6df42022-11-09 11:13:29 +000095
96 ++installed_cb_idx;
97
98 return 0;
99 }
100
101 return -ENOMEM;
102}
103
Javier Almansa Sobrino48d68a72023-03-02 12:26:39 +0000104void host_util_take_sysreg_snapshot(void)
105{
106 memcpy((void *)&sysregs_snapshot[0], (void *)&sysregs[0],
107 sizeof(struct sysreg_data) * SYSREG_MAX_CBS);
108}
109
110void host_util_restore_sysreg_snapshot(void)
111{
112 memcpy((void *)&sysregs[0], (void *)&sysregs_snapshot[0],
113 sizeof(struct sysreg_data) * SYSREG_MAX_CBS);
114}
115
116void host_util_zero_sysregs_and_cbs(void)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000117{
118
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +0000119 (void)memset((void *)sysregs, 0,
120 sizeof(struct sysreg_data) * SYSREG_MAX_CBS);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000121
122 installed_cb_idx = 0U;
123}
124
125int host_util_set_default_sysreg_cb(char *name, u_register_t init)
126{
127 return host_util_set_sysreg_cb(name, &sysreg_rd_cb,
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +0000128 &sysreg_wr_cb, init);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000129}
130
131unsigned long host_util_get_granule_base(void)
132{
133 return (unsigned long)granules_buffer;
134}
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +0000135
136void host_util_set_cpuid(unsigned int cpuid)
137{
138 assert(cpuid < MAX_CPUS);
139
140 current_cpuid = cpuid;
141}
Mate Toth-Palaead06f2023-03-02 10:17:09 +0100142
143unsigned char *host_util_get_el3_rmm_shared_buffer(void)
144{
145 return el3_rmm_shared_buffer;
146}
147
148void host_util_setup_sysreg_and_boot_manifest(void)
149{
150 int ret;
151
152 /*
AlexeiFedoroveaec0c42023-02-01 18:13:32 +0000153 * Initialize ID_AA64DFR0_EL1 with PMUVer field to PMUv3p7.
154 * (ID_AA64DFR0_EL1.PMUVer, bits [11:8] set to 7)
155 */
156 ret = host_util_set_default_sysreg_cb("id_aa64dfr0_el1",
157 INPLACE(ID_AA64DFR0_EL1_PMUVer, 7UL));
158
159 /*
Mate Toth-Palaead06f2023-03-02 10:17:09 +0100160 * Initialize ID_AA64MMFR0_EL1 with a physical address
161 * range of 48 bits (PARange bits set to 0b0101)
162 */
163 ret = host_util_set_default_sysreg_cb("id_aa64mmfr0_el1",
164 INPLACE(ID_AA64MMFR0_EL1_PARANGE, 5UL));
165
166 /*
167 * Initialize ICH_VTR_EL2 with 6 preemption bits.
168 * (PREbits is equal number of preemption bits minus one)
169 */
170 ret = host_util_set_default_sysreg_cb("ich_vtr_el2",
171 INPLACE(ICH_VTR_EL2_PRE_BITS, 5UL));
172
173 /* SCTLR_EL2 is reset to zero */
174 ret = host_util_set_default_sysreg_cb("sctlr_el2", 0UL);
175
Javier Almansa Sobrinoc173f1e2023-03-21 12:53:08 +0000176 /* HCR_EL2 is reset to zero */
177 ret = host_util_set_default_sysreg_cb("hcr_el2", 0UL);
178
Mate Toth-Palaead06f2023-03-02 10:17:09 +0100179 /* TPIDR_EL2 is reset to zero */
180 ret = host_util_set_default_sysreg_cb("tpidr_el2", 0UL);
181
Mate Toth-Pal40193e42023-03-01 13:17:48 +0100182 /* ID_AA64ISAR0.RNDR is reset to 1 */
183 ret = host_util_set_default_sysreg_cb("id_aa64isar0_el1",
184 INPLACE(ID_AA64ISAR0_EL1_RNDR, 1UL));
185
Mate Toth-Palaead06f2023-03-02 10:17:09 +0100186 /*
Mate Toth-Pal2611c552023-02-28 10:28:32 +0100187 * Add callback to elr_el2 so that the realm entry point can be accessed
188 * by host_run_realm
189 */
190 ret = host_util_set_default_sysreg_cb("elr_el2", 0UL);
191
192 /*
AlexeiFedoroveaec0c42023-02-01 18:13:32 +0000193 * Set number of event counters implemented to 31.
194 * (PMCR_EL0.N, bits [15:11] set to 31)
195 */
196 ret = host_util_set_default_sysreg_cb("pmcr_el0",
197 INPLACE(PMCR_EL0_N, 31UL));
198
199 /*
Mate Toth-Palaead06f2023-03-02 10:17:09 +0100200 * Only check the return value of the last callback setup, to detect
201 * if we are out of callback slots.
202 */
203 if (ret != 0) {
204 panic();
205 }
206
207 /* Initialize the boot manifest */
208 boot_manifest->version = RMM_EL3_IFC_SUPPORTED_VERSION;
209 boot_manifest->plat_data = (uintptr_t)NULL;
210}