blob: a94cd57932de90e060adc41db7dc884fb4fa81fb [file] [log] [blame]
Jens Wiklander52c798e2015-12-07 14:37:10 +01001/*
Jean-Philippe Brucker721b83d2023-09-07 18:13:07 +01002 * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
Jens Wiklander52c798e2015-12-07 14:37:10 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Jens Wiklander52c798e2015-12-07 14:37:10 +01005 */
6
7#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009#include <common/bl_common.h>
Maxim Uvarova0b85c22020-12-14 10:17:44 +000010#include <drivers/arm/pl061_gpio.h>
Jean-Philippe Brucker4453ba92023-09-07 18:47:48 +010011#include <lib/gpt_rme/gpt_rme.h>
Raymond Maobb653862023-10-04 09:58:29 -070012#include <lib/transfer_list.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000013#include <plat/common/platform.h>
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +010014#if ENABLE_RME
Mathieu Poirier63a48ca2024-08-16 09:44:09 -060015#ifdef PLAT_qemu
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +010016#include <qemu_pas_def.h>
Mathieu Poirier63a48ca2024-08-16 09:44:09 -060017#elif PLAT_qemu_sbsa
18#include <qemu_sbsa_pas_def.h>
19#endif /* PLAT_qemu */
20#endif /* ENABLE_RME */
Mathieu Poirier61c3ade2024-09-27 15:27:25 -060021#ifdef PLAT_qemu_sbsa
22#include <sbsa_platform.h>
23#endif
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000024
Jens Wiklander52c798e2015-12-07 14:37:10 +010025#include "qemu_private.h"
26
Chen Baozif7d9aa82023-02-20 10:50:15 +000027#define MAP_BL31_TOTAL MAP_REGION_FLAT( \
28 BL31_BASE, \
29 BL31_END - BL31_BASE, \
30 MT_MEMORY | MT_RW | EL3_PAS)
31#define MAP_BL31_RO MAP_REGION_FLAT( \
32 BL_CODE_BASE, \
33 BL_CODE_END - BL_CODE_BASE, \
34 MT_CODE | EL3_PAS), \
35 MAP_REGION_FLAT( \
36 BL_RO_DATA_BASE, \
37 BL_RO_DATA_END \
38 - BL_RO_DATA_BASE, \
39 MT_RO_DATA | EL3_PAS)
40
Chen Baozi097a43a2023-03-12 20:58:04 +080041#if USE_COHERENT_MEM
Chen Baozif7d9aa82023-02-20 10:50:15 +000042#define MAP_BL_COHERENT_RAM MAP_REGION_FLAT( \
43 BL_COHERENT_RAM_BASE, \
44 BL_COHERENT_RAM_END \
45 - BL_COHERENT_RAM_BASE, \
46 MT_DEVICE | MT_RW | EL3_PAS)
Chen Baozi097a43a2023-03-12 20:58:04 +080047#endif
Chen Baozif7d9aa82023-02-20 10:50:15 +000048
Jens Wiklander52c798e2015-12-07 14:37:10 +010049/*
Jens Wiklander52c798e2015-12-07 14:37:10 +010050 * Placeholder variables for copying the arguments that have been passed to
51 * BL3-1 from BL2.
52 */
53static entry_point_info_t bl32_image_ep_info;
54static entry_point_info_t bl33_image_ep_info;
Jean-Philippe Bruckerf304bd62023-09-07 17:33:22 +010055#if ENABLE_RME
56static entry_point_info_t rmm_image_ep_info;
57#endif
Raymond Maobb653862023-10-04 09:58:29 -070058static struct transfer_list_header *bl31_tl;
Jens Wiklander52c798e2015-12-07 14:37:10 +010059
60/*******************************************************************************
61 * Perform any BL3-1 early platform setup. Here is an opportunity to copy
John Tsichritzisd653d332018-09-14 10:34:57 +010062 * parameters passed by the calling EL (S-EL1 in BL2 & EL3 in BL1) before
Jens Wiklander52c798e2015-12-07 14:37:10 +010063 * they are lost (potentially). This needs to be done before the MMU is
64 * initialized so that the memory layout can be used while creating page
65 * tables. BL2 has flushed this information to memory, so we are guaranteed
66 * to pick up good data.
67 ******************************************************************************/
Jens Wiklandere22b91e2018-09-04 14:07:19 +020068void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
69 u_register_t arg2, u_register_t arg3)
Jens Wiklander52c798e2015-12-07 14:37:10 +010070{
Raymond Mao33e41b72024-12-27 06:58:24 -080071 bool is64 = false;
72 uint64_t hval;
73
Jens Wiklander52c798e2015-12-07 14:37:10 +010074 /* Initialize the console to provide early debug support */
Michalis Pappascca6cb72018-03-04 15:43:38 +080075 qemu_console_init();
Jens Wiklander52c798e2015-12-07 14:37:10 +010076
Marcin Juszkiewiczb6839fb2023-05-10 10:03:01 +020077/* Platform names have to be lowercase. */
78#ifdef PLAT_qemu_sbsa
Mathieu Poirier61c3ade2024-09-27 15:27:25 -060079 sbsa_platform_init();
Marcin Juszkiewiczb6839fb2023-05-10 10:03:01 +020080#endif
81
Fu Weic2f78442017-05-27 21:21:42 +080082 /*
83 * Check params passed from BL2
84 */
Jens Wiklandere22b91e2018-09-04 14:07:19 +020085 bl_params_t *params_from_bl2 = (bl_params_t *)arg0;
Fu Weic2f78442017-05-27 21:21:42 +080086
87 assert(params_from_bl2);
88 assert(params_from_bl2->h.type == PARAM_BL_PARAMS);
89 assert(params_from_bl2->h.version >= VERSION_2);
90
91 bl_params_node_t *bl_params = params_from_bl2->head;
92
93 /*
Jean-Philippe Bruckerf304bd62023-09-07 17:33:22 +010094 * Copy BL33, BL32 and RMM (if present), entry point information.
Fu Weic2f78442017-05-27 21:21:42 +080095 * They are stored in Secure RAM, in BL2's address space.
96 */
97 while (bl_params) {
Raymond Mao33e41b72024-12-27 06:58:24 -080098#ifdef __aarch64__
99 if (bl_params->image_id == BL31_IMAGE_ID &&
100 GET_RW(bl_params->ep_info->spsr) == MODE_RW_64)
101 is64 = true;
102#endif
Fu Weic2f78442017-05-27 21:21:42 +0800103 if (bl_params->image_id == BL32_IMAGE_ID)
104 bl32_image_ep_info = *bl_params->ep_info;
105
Jean-Philippe Bruckerf304bd62023-09-07 17:33:22 +0100106#if ENABLE_RME
107 if (bl_params->image_id == RMM_IMAGE_ID)
108 rmm_image_ep_info = *bl_params->ep_info;
109#endif
110
Fu Weic2f78442017-05-27 21:21:42 +0800111 if (bl_params->image_id == BL33_IMAGE_ID)
112 bl33_image_ep_info = *bl_params->ep_info;
113
114 bl_params = bl_params->next_params_info;
115 }
116
117 if (!bl33_image_ep_info.pc)
118 panic();
Jean-Philippe Bruckerf304bd62023-09-07 17:33:22 +0100119#if ENABLE_RME
120 if (!rmm_image_ep_info.pc)
121 panic();
122#endif
Raymond Maobb653862023-10-04 09:58:29 -0700123
Raymond Mao33e41b72024-12-27 06:58:24 -0800124 if (!TRANSFER_LIST ||
125 !transfer_list_check_header((void *)arg3))
126 return;
127
128 if (is64)
129 hval = TRANSFER_LIST_HANDOFF_X1_VALUE(REGISTER_CONVENTION_VERSION);
130 else
131 hval = TRANSFER_LIST_HANDOFF_R1_VALUE(REGISTER_CONVENTION_VERSION);
132
133 if (arg1 != hval)
134 return;
135
136 bl31_tl = (void *)arg3; /* saved TL address from BL2 */
Jens Wiklander52c798e2015-12-07 14:37:10 +0100137}
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +0100138
139#if ENABLE_RME
Mathieu Poirier63a48ca2024-08-16 09:44:09 -0600140#if PLAT_qemu
141/*
142 * The GPT library might modify the gpt regions structure to optimize
143 * the layout, so the array cannot be constant.
144 */
145static pas_region_t pas_regions[] = {
146 QEMU_PAS_ROOT,
147 QEMU_PAS_SECURE,
148 QEMU_PAS_GPTS,
149 QEMU_PAS_NS0,
150 QEMU_PAS_REALM,
151 QEMU_PAS_NS1,
152};
153
154static inline void bl31_adjust_pas_regions(void) {}
155#elif PLAT_qemu_sbsa
156/*
157 * The GPT library might modify the gpt regions structure to optimize
158 * the layout, so the array cannot be constant.
159 */
160static pas_region_t pas_regions[] = {
161 QEMU_PAS_ROOT,
162 QEMU_PAS_SECURE,
163 QEMU_PAS_GPTS,
164 QEMU_PAS_REALM,
165 QEMU_PAS_NS0,
166};
167
168static void bl31_adjust_pas_regions(void)
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +0100169{
Mathieu Poirier63a48ca2024-08-16 09:44:09 -0600170 uint64_t base_addr = 0, total_size = 0;
171 struct platform_memory_data data;
172 uint32_t node;
173
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +0100174 /*
Mathieu Poirier63a48ca2024-08-16 09:44:09 -0600175 * The amount of memory supported by the SBSA platform is dynamic
176 * and dependent on user input. Since the configuration of the GPT
177 * needs to reflect the system memory, QEMU_PAS_NS0 needs to be set
178 * based on the information found in the device tree.
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +0100179 */
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +0100180
Mathieu Poirier63a48ca2024-08-16 09:44:09 -0600181 for (node = 0; node < sbsa_platform_num_memnodes(); node++) {
182 data = sbsa_platform_memory_node(node);
183
184 if (data.nodeid == 0) {
185 base_addr = data.addr_base;
186 }
187
188 total_size += data.addr_size;
189 }
190
191 /* Index '4' correspond to QEMU_PAS_NS0, see pas_regions[] above */
192 pas_regions[4].base_pa = base_addr;
193 pas_regions[4].size = total_size;
194}
195#endif /* PLAT_qemu */
196
197static void bl31_plat_gpt_setup(void)
198{
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +0100199 /*
200 * Initialize entire protected space to GPT_GPI_ANY. With each L0 entry
201 * covering 1GB (currently the only supported option), then covering
202 * 256TB of RAM (48-bit PA) would require a 2MB L0 region. At the
203 * moment we use a 8KB table, which covers 1TB of RAM (40-bit PA).
204 */
Mathieu Poirier154aee92024-06-03 10:38:42 -0600205 if (gpt_init_l0_tables(PLATFORM_GPCCR_PPS, PLAT_QEMU_L0_GPT_BASE,
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +0100206 PLAT_QEMU_L0_GPT_SIZE +
207 PLAT_QEMU_GPT_BITLOCK_SIZE) < 0) {
208 ERROR("gpt_init_l0_tables() failed!\n");
209 panic();
210 }
211
Mathieu Poirier63a48ca2024-08-16 09:44:09 -0600212 bl31_adjust_pas_regions();
213
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +0100214 /* Carve out defined PAS ranges. */
215 if (gpt_init_pas_l1_tables(GPCCR_PGS_4K,
216 PLAT_QEMU_L1_GPT_BASE,
217 PLAT_QEMU_L1_GPT_SIZE,
218 pas_regions,
219 (unsigned int)(sizeof(pas_regions) /
220 sizeof(pas_region_t))) < 0) {
221 ERROR("gpt_init_pas_l1_tables() failed!\n");
222 panic();
223 }
224
225 INFO("Enabling Granule Protection Checks\n");
226 if (gpt_enable() < 0) {
227 ERROR("gpt_enable() failed!\n");
228 panic();
229 }
230}
231#endif
Jens Wiklander52c798e2015-12-07 14:37:10 +0100232
233void bl31_plat_arch_setup(void)
234{
Chen Baozif7d9aa82023-02-20 10:50:15 +0000235 const mmap_region_t bl_regions[] = {
236 MAP_BL31_TOTAL,
237 MAP_BL31_RO,
Chen Baozi097a43a2023-03-12 20:58:04 +0800238#if USE_COHERENT_MEM
Chen Baozif7d9aa82023-02-20 10:50:15 +0000239 MAP_BL_COHERENT_RAM,
Chen Baozi097a43a2023-03-12 20:58:04 +0800240#endif
Jean-Philippe Brucker721b83d2023-09-07 18:13:07 +0100241#if ENABLE_RME
242 MAP_GPT_L0_REGION,
243 MAP_GPT_L1_REGION,
244 MAP_RMM_SHARED_MEM,
245#endif
Chen Baozif7d9aa82023-02-20 10:50:15 +0000246 {0}
247 };
248
249 setup_page_tables(bl_regions, plat_qemu_get_mmap());
250
251 enable_mmu_el3(0);
Jean-Philippe Brucker4453ba92023-09-07 18:47:48 +0100252
253#if ENABLE_RME
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +0100254 /* Initialise and enable granule protection after MMU. */
255 bl31_plat_gpt_setup();
256
Jean-Philippe Brucker4453ba92023-09-07 18:47:48 +0100257 /*
258 * Initialise Granule Protection library and enable GPC for the primary
259 * processor. The tables have already been initialized by a previous BL
260 * stage, so there is no need to provide any PAS here. This function
261 * sets up pointers to those tables.
262 */
263 if (gpt_runtime_init() < 0) {
264 ERROR("gpt_runtime_init() failed!\n");
265 panic();
266 }
267#endif /* ENABLE_RME */
268
Jens Wiklander52c798e2015-12-07 14:37:10 +0100269}
270
Maxim Uvarova0b85c22020-12-14 10:17:44 +0000271static void qemu_gpio_init(void)
272{
273#ifdef SECURE_GPIO_BASE
274 pl061_gpio_init();
275 pl061_gpio_register(SECURE_GPIO_BASE, 0);
276#endif
277}
278
Jens Wiklander52c798e2015-12-07 14:37:10 +0100279void bl31_platform_setup(void)
280{
Hongbo Zhang32338ec2018-04-19 13:06:07 +0800281 plat_qemu_gic_init();
Maxim Uvarova0b85c22020-12-14 10:17:44 +0000282 qemu_gpio_init();
Jens Wiklander52c798e2015-12-07 14:37:10 +0100283}
284
285unsigned int plat_get_syscnt_freq2(void)
286{
Marcin Juszkiewiczb9a35532024-04-22 17:27:56 +0200287 return read_cntfrq_el0();
Jens Wiklander52c798e2015-12-07 14:37:10 +0100288}
289
290/*******************************************************************************
291 * Return a pointer to the 'entry_point_info' structure of the next image
292 * for the security state specified. BL3-3 corresponds to the non-secure
293 * image type while BL3-2 corresponds to the secure image type. A NULL
294 * pointer is returned if the image does not exist.
295 ******************************************************************************/
296entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
297{
298 entry_point_info_t *next_image_info;
299
300 assert(sec_state_is_valid(type));
Jean-Philippe Bruckerf304bd62023-09-07 17:33:22 +0100301 if (type == NON_SECURE) {
302 next_image_info = &bl33_image_ep_info;
303 }
304#if ENABLE_RME
305 else if (type == REALM) {
306 next_image_info = &rmm_image_ep_info;
307 }
308#endif
309 else {
310 next_image_info = &bl32_image_ep_info;
311 }
312
Jens Wiklander52c798e2015-12-07 14:37:10 +0100313 /*
314 * None of the images on the ARM development platforms can have 0x0
315 * as the entrypoint
316 */
317 if (next_image_info->pc)
318 return next_image_info;
319 else
320 return NULL;
321}
Raymond Maobb653862023-10-04 09:58:29 -0700322
323void bl31_plat_runtime_setup(void)
324{
Raymond Maobb653862023-10-04 09:58:29 -0700325#if TRANSFER_LIST
326 if (bl31_tl) {
327 /*
328 * update the TL from S to NS memory before jump to BL33
329 * to reflect all changes in TL done by BL32
330 */
331 memcpy((void *)FW_NS_HANDOFF_BASE, bl31_tl, bl31_tl->max_size);
332 }
333#endif
Jens Wiklander7ad51de2024-03-01 09:07:19 +0100334
335 console_flush();
336 console_switch_state(CONSOLE_FLAG_RUNTIME);
Raymond Maobb653862023-10-04 09:58:29 -0700337}