blob: 5dc8038a760081fef41c80acdfdc1760da3a38e9 [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];
Soby Mathewb4c6df42022-11-09 11:13:29 +000018static unsigned int installed_cb_idx;
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000019static unsigned int current_cpuid;
Soby Mathewb4c6df42022-11-09 11:13:29 +000020
21/*
22 * Allocate memory to emulate physical memory to initialize the
23 * granule library.
24 */
25static unsigned char granules_buffer[HOST_MEM_SIZE] __aligned(GRANULE_SIZE);
26
27/*
Mate Toth-Palaead06f2023-03-02 10:17:09 +010028 * Define and set the Boot Interface arguments.
29 */
30static unsigned char el3_rmm_shared_buffer[PAGE_SIZE] __aligned(PAGE_SIZE);
31
32/*
33 * Create a basic boot manifest.
34 */
35static struct rmm_core_manifest *boot_manifest =
36 (struct rmm_core_manifest *)el3_rmm_shared_buffer;
37
38/*
Soby Mathewb4c6df42022-11-09 11:13:29 +000039 * Generic callback to access a sysreg for reading.
40 */
41static 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 */
49static void sysreg_wr_cb(u_register_t val, u_register_t *reg)
50{
51 *reg = val;
52}
53
54struct sysreg_cb *host_util_get_sysreg_cb(char *name)
55{
56 for (unsigned int i = 0U; i < SYSREG_MAX_CBS; i++) {
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000057 if (strncmp(name, &sysregs[i].name[0],
Soby Mathewb4c6df42022-11-09 11:13:29 +000058 MAX_SYSREG_NAME_LEN) == 0) {
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000059
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 Mathewb4c6df42022-11-09 11:13:29 +000067 }
68 }
69
70 return (struct sysreg_cb *)NULL;
71}
72
73int 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 Sobrino9929a792022-11-22 16:11:13 +000077 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 Mathewb4c6df42022-11-09 11:13:29 +000081
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000082 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 Mathewb4c6df42022-11-09 11:13:29 +000087 &name[0], MAX_SYSREG_NAME_LEN);
88
89 /*
90 * Add a string termination character in case the
91 * name were truncated.
92 */
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000093 sysregs[installed_cb_idx].name[MAX_SYSREG_NAME_LEN] = '\0';
Soby Mathewb4c6df42022-11-09 11:13:29 +000094
95 ++installed_cb_idx;
96
97 return 0;
98 }
99
100 return -ENOMEM;
101}
102
103void host_util_reset_all_sysreg_cb(void)
104{
105
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +0000106 (void)memset((void *)sysregs, 0,
107 sizeof(struct sysreg_data) * SYSREG_MAX_CBS);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000108
109 installed_cb_idx = 0U;
110}
111
112int 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 Sobrino9929a792022-11-22 16:11:13 +0000115 &sysreg_wr_cb, init);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000116}
117
118unsigned long host_util_get_granule_base(void)
119{
120 return (unsigned long)granules_buffer;
121}
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +0000122
123void host_util_set_cpuid(unsigned int cpuid)
124{
125 assert(cpuid < MAX_CPUS);
126
127 current_cpuid = cpuid;
128}
Mate Toth-Palaead06f2023-03-02 10:17:09 +0100129
130unsigned char *host_util_get_el3_rmm_shared_buffer(void)
131{
132 return el3_rmm_shared_buffer;
133}
134
135void host_util_setup_sysreg_and_boot_manifest(void)
136{
137 int ret;
138
139 /*
140 * Initialize ID_AA64MMFR0_EL1 with a physical address
141 * range of 48 bits (PARange bits set to 0b0101)
142 */
143 ret = host_util_set_default_sysreg_cb("id_aa64mmfr0_el1",
144 INPLACE(ID_AA64MMFR0_EL1_PARANGE, 5UL));
145
146 /*
147 * Initialize ICH_VTR_EL2 with 6 preemption bits.
148 * (PREbits is equal number of preemption bits minus one)
149 */
150 ret = host_util_set_default_sysreg_cb("ich_vtr_el2",
151 INPLACE(ICH_VTR_EL2_PRE_BITS, 5UL));
152
153 /* SCTLR_EL2 is reset to zero */
154 ret = host_util_set_default_sysreg_cb("sctlr_el2", 0UL);
155
156 /* TPIDR_EL2 is reset to zero */
157 ret = host_util_set_default_sysreg_cb("tpidr_el2", 0UL);
158
Mate Toth-Pal40193e42023-03-01 13:17:48 +0100159 /* ID_AA64ISAR0.RNDR is reset to 1 */
160 ret = host_util_set_default_sysreg_cb("id_aa64isar0_el1",
161 INPLACE(ID_AA64ISAR0_EL1_RNDR, 1UL));
162
Mate Toth-Palaead06f2023-03-02 10:17:09 +0100163 /*
164 * Only check the return value of the last callback setup, to detect
165 * if we are out of callback slots.
166 */
167 if (ret != 0) {
168 panic();
169 }
170
171 /* Initialize the boot manifest */
172 boot_manifest->version = RMM_EL3_IFC_SUPPORTED_VERSION;
173 boot_manifest->plat_data = (uintptr_t)NULL;
174}