blob: 51fee6446d05dd72e36f98cbcfd7f314d063a2e3 [file] [log] [blame]
Jens Wiklander52c798e2015-12-07 14:37:10 +01001/*
Harrison Mutai8eba0142025-05-27 10:39:02 +00002 * Copyright (c) 2015-2025, 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>
Harrison Mutai8eba0142025-05-27 10:39:02 +000012#if TRANSFER_LIST
Raymond Maobb653862023-10-04 09:58:29 -070013#include <lib/transfer_list.h>
Harrison Mutai8eba0142025-05-27 10:39:02 +000014#endif
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000015#include <plat/common/platform.h>
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +010016#if ENABLE_RME
Mathieu Poirier63a48ca2024-08-16 09:44:09 -060017#ifdef PLAT_qemu
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +010018#include <qemu_pas_def.h>
Mathieu Poirier63a48ca2024-08-16 09:44:09 -060019#elif PLAT_qemu_sbsa
20#include <qemu_sbsa_pas_def.h>
21#endif /* PLAT_qemu */
22#endif /* ENABLE_RME */
Mathieu Poirier61c3ade2024-09-27 15:27:25 -060023#ifdef PLAT_qemu_sbsa
24#include <sbsa_platform.h>
25#endif
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000026
Jens Wiklander52c798e2015-12-07 14:37:10 +010027#include "qemu_private.h"
28
Chen Baozif7d9aa82023-02-20 10:50:15 +000029#define MAP_BL31_TOTAL MAP_REGION_FLAT( \
30 BL31_BASE, \
31 BL31_END - BL31_BASE, \
32 MT_MEMORY | MT_RW | EL3_PAS)
33#define MAP_BL31_RO MAP_REGION_FLAT( \
34 BL_CODE_BASE, \
35 BL_CODE_END - BL_CODE_BASE, \
36 MT_CODE | EL3_PAS), \
37 MAP_REGION_FLAT( \
38 BL_RO_DATA_BASE, \
39 BL_RO_DATA_END \
40 - BL_RO_DATA_BASE, \
41 MT_RO_DATA | EL3_PAS)
42
Chen Baozi097a43a2023-03-12 20:58:04 +080043#if USE_COHERENT_MEM
Chen Baozif7d9aa82023-02-20 10:50:15 +000044#define MAP_BL_COHERENT_RAM MAP_REGION_FLAT( \
45 BL_COHERENT_RAM_BASE, \
46 BL_COHERENT_RAM_END \
47 - BL_COHERENT_RAM_BASE, \
48 MT_DEVICE | MT_RW | EL3_PAS)
Chen Baozi097a43a2023-03-12 20:58:04 +080049#endif
Chen Baozif7d9aa82023-02-20 10:50:15 +000050
Jean-Philippe Brucker8d83b592025-02-11 14:10:34 +000051#if ENABLE_RME
52#if (RME_GPT_BITLOCK_BLOCK == 0)
53#define BITLOCK_BASE UL(0)
54#define BITLOCK_SIZE UL(0)
55#else
56
57/*
58 * Number of bitlock_t entries in the gpt_bitlock array for this platform's
59 * Protected Physical Size. One 8-bit bitlock_t entry covers
60 * 8 * RME_GPT_BITLOCK_BLOCK * 512MB.
61 */
62#if (PLAT_QEMU_PPS > (RME_GPT_BITLOCK_BLOCK * SZ_512M * UL(8)))
63#define BITLOCKS_NUM (PLAT_QEMU_PPS / \
64 (RME_GPT_BITLOCK_BLOCK * SZ_512M * UL(8)))
65#else
66#define BITLOCKS_NUM 1
67#endif
68
69static bitlock_t gpt_bitlock[BITLOCKS_NUM];
70#define BITLOCK_BASE (uintptr_t)gpt_bitlock
71#define BITLOCK_SIZE sizeof(gpt_bitlock)
72#endif /* RME_GPT_BITLOCK_BLOCK */
73#endif /* ENABLE_RME */
74
Jens Wiklander52c798e2015-12-07 14:37:10 +010075/*
Jens Wiklander52c798e2015-12-07 14:37:10 +010076 * Placeholder variables for copying the arguments that have been passed to
77 * BL3-1 from BL2.
78 */
79static entry_point_info_t bl32_image_ep_info;
80static entry_point_info_t bl33_image_ep_info;
Jean-Philippe Bruckerf304bd62023-09-07 17:33:22 +010081#if ENABLE_RME
82static entry_point_info_t rmm_image_ep_info;
83#endif
Harrison Mutai8eba0142025-05-27 10:39:02 +000084static struct transfer_list_header __maybe_unused *bl31_tl;
Jens Wiklander52c798e2015-12-07 14:37:10 +010085
86/*******************************************************************************
87 * Perform any BL3-1 early platform setup. Here is an opportunity to copy
John Tsichritzisd653d332018-09-14 10:34:57 +010088 * parameters passed by the calling EL (S-EL1 in BL2 & EL3 in BL1) before
Jens Wiklander52c798e2015-12-07 14:37:10 +010089 * they are lost (potentially). This needs to be done before the MMU is
90 * initialized so that the memory layout can be used while creating page
91 * tables. BL2 has flushed this information to memory, so we are guaranteed
92 * to pick up good data.
93 ******************************************************************************/
Jens Wiklandere22b91e2018-09-04 14:07:19 +020094void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
95 u_register_t arg2, u_register_t arg3)
Jens Wiklander52c798e2015-12-07 14:37:10 +010096{
Harrison Mutai8eba0142025-05-27 10:39:02 +000097 bool __maybe_unused is64 = false;
98 uint64_t __maybe_unused hval;
Raymond Mao33e41b72024-12-27 06:58:24 -080099
Jens Wiklander52c798e2015-12-07 14:37:10 +0100100 /* Initialize the console to provide early debug support */
Michalis Pappascca6cb72018-03-04 15:43:38 +0800101 qemu_console_init();
Jens Wiklander52c798e2015-12-07 14:37:10 +0100102
Marcin Juszkiewiczb6839fb2023-05-10 10:03:01 +0200103/* Platform names have to be lowercase. */
104#ifdef PLAT_qemu_sbsa
Mathieu Poirier61c3ade2024-09-27 15:27:25 -0600105 sbsa_platform_init();
Marcin Juszkiewiczb6839fb2023-05-10 10:03:01 +0200106#endif
107
Fu Weic2f78442017-05-27 21:21:42 +0800108 /*
109 * Check params passed from BL2
110 */
Jens Wiklandere22b91e2018-09-04 14:07:19 +0200111 bl_params_t *params_from_bl2 = (bl_params_t *)arg0;
Fu Weic2f78442017-05-27 21:21:42 +0800112
113 assert(params_from_bl2);
114 assert(params_from_bl2->h.type == PARAM_BL_PARAMS);
115 assert(params_from_bl2->h.version >= VERSION_2);
116
117 bl_params_node_t *bl_params = params_from_bl2->head;
118
119 /*
Jean-Philippe Bruckerf304bd62023-09-07 17:33:22 +0100120 * Copy BL33, BL32 and RMM (if present), entry point information.
Fu Weic2f78442017-05-27 21:21:42 +0800121 * They are stored in Secure RAM, in BL2's address space.
122 */
123 while (bl_params) {
Harrison Mutai8eba0142025-05-27 10:39:02 +0000124#if defined(__aarch64__) && TRANSFER_LIST
Raymond Mao33e41b72024-12-27 06:58:24 -0800125 if (bl_params->image_id == BL31_IMAGE_ID &&
126 GET_RW(bl_params->ep_info->spsr) == MODE_RW_64)
127 is64 = true;
Harrison Mutai8eba0142025-05-27 10:39:02 +0000128#endif /* defined(__aarch64__) && TRANSFER_LIST */
Fu Weic2f78442017-05-27 21:21:42 +0800129 if (bl_params->image_id == BL32_IMAGE_ID)
130 bl32_image_ep_info = *bl_params->ep_info;
131
Jean-Philippe Bruckerf304bd62023-09-07 17:33:22 +0100132#if ENABLE_RME
133 if (bl_params->image_id == RMM_IMAGE_ID)
134 rmm_image_ep_info = *bl_params->ep_info;
135#endif
136
Fu Weic2f78442017-05-27 21:21:42 +0800137 if (bl_params->image_id == BL33_IMAGE_ID)
138 bl33_image_ep_info = *bl_params->ep_info;
139
140 bl_params = bl_params->next_params_info;
141 }
142
143 if (!bl33_image_ep_info.pc)
144 panic();
Jean-Philippe Bruckerf304bd62023-09-07 17:33:22 +0100145#if ENABLE_RME
146 if (!rmm_image_ep_info.pc)
147 panic();
148#endif
Raymond Maobb653862023-10-04 09:58:29 -0700149
Harrison Mutai8eba0142025-05-27 10:39:02 +0000150#if TRANSFER_LIST
151 if (!transfer_list_check_header((void *)arg3))
Raymond Mao33e41b72024-12-27 06:58:24 -0800152 return;
153
154 if (is64)
155 hval = TRANSFER_LIST_HANDOFF_X1_VALUE(REGISTER_CONVENTION_VERSION);
156 else
157 hval = TRANSFER_LIST_HANDOFF_R1_VALUE(REGISTER_CONVENTION_VERSION);
158
159 if (arg1 != hval)
160 return;
Harrison Mutai8eba0142025-05-27 10:39:02 +0000161#endif
Raymond Mao33e41b72024-12-27 06:58:24 -0800162
163 bl31_tl = (void *)arg3; /* saved TL address from BL2 */
Jens Wiklander52c798e2015-12-07 14:37:10 +0100164}
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +0100165
166#if ENABLE_RME
Mathieu Poirier63a48ca2024-08-16 09:44:09 -0600167#if PLAT_qemu
168/*
169 * The GPT library might modify the gpt regions structure to optimize
170 * the layout, so the array cannot be constant.
171 */
172static pas_region_t pas_regions[] = {
173 QEMU_PAS_ROOT,
174 QEMU_PAS_SECURE,
175 QEMU_PAS_GPTS,
176 QEMU_PAS_NS0,
177 QEMU_PAS_REALM,
178 QEMU_PAS_NS1,
179};
180
181static inline void bl31_adjust_pas_regions(void) {}
182#elif PLAT_qemu_sbsa
183/*
184 * The GPT library might modify the gpt regions structure to optimize
185 * the layout, so the array cannot be constant.
186 */
187static pas_region_t pas_regions[] = {
188 QEMU_PAS_ROOT,
189 QEMU_PAS_SECURE,
190 QEMU_PAS_GPTS,
191 QEMU_PAS_REALM,
192 QEMU_PAS_NS0,
193};
194
195static void bl31_adjust_pas_regions(void)
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +0100196{
Mathieu Poirier63a48ca2024-08-16 09:44:09 -0600197 uint64_t base_addr = 0, total_size = 0;
198 struct platform_memory_data data;
199 uint32_t node;
200
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +0100201 /*
Mathieu Poirier63a48ca2024-08-16 09:44:09 -0600202 * The amount of memory supported by the SBSA platform is dynamic
203 * and dependent on user input. Since the configuration of the GPT
204 * needs to reflect the system memory, QEMU_PAS_NS0 needs to be set
205 * based on the information found in the device tree.
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +0100206 */
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +0100207
Mathieu Poirier63a48ca2024-08-16 09:44:09 -0600208 for (node = 0; node < sbsa_platform_num_memnodes(); node++) {
209 data = sbsa_platform_memory_node(node);
210
211 if (data.nodeid == 0) {
212 base_addr = data.addr_base;
213 }
214
215 total_size += data.addr_size;
216 }
217
218 /* Index '4' correspond to QEMU_PAS_NS0, see pas_regions[] above */
219 pas_regions[4].base_pa = base_addr;
220 pas_regions[4].size = total_size;
221}
222#endif /* PLAT_qemu */
223
224static void bl31_plat_gpt_setup(void)
225{
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +0100226 /*
227 * Initialize entire protected space to GPT_GPI_ANY. With each L0 entry
228 * covering 1GB (currently the only supported option), then covering
229 * 256TB of RAM (48-bit PA) would require a 2MB L0 region. At the
230 * moment we use a 8KB table, which covers 1TB of RAM (40-bit PA).
231 */
Jean-Philippe Brucker8d83b592025-02-11 14:10:34 +0000232 if (gpt_init_l0_tables(PLAT_QEMU_GPCCR_PPS, PLAT_QEMU_L0_GPT_BASE,
233 PLAT_QEMU_L0_GPT_SIZE) < 0) {
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +0100234 ERROR("gpt_init_l0_tables() failed!\n");
235 panic();
236 }
237
Mathieu Poirier63a48ca2024-08-16 09:44:09 -0600238 bl31_adjust_pas_regions();
239
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +0100240 /* Carve out defined PAS ranges. */
241 if (gpt_init_pas_l1_tables(GPCCR_PGS_4K,
242 PLAT_QEMU_L1_GPT_BASE,
243 PLAT_QEMU_L1_GPT_SIZE,
244 pas_regions,
245 (unsigned int)(sizeof(pas_regions) /
246 sizeof(pas_region_t))) < 0) {
247 ERROR("gpt_init_pas_l1_tables() failed!\n");
248 panic();
249 }
250
251 INFO("Enabling Granule Protection Checks\n");
252 if (gpt_enable() < 0) {
253 ERROR("gpt_enable() failed!\n");
254 panic();
255 }
256}
257#endif
Jens Wiklander52c798e2015-12-07 14:37:10 +0100258
259void bl31_plat_arch_setup(void)
260{
Chen Baozif7d9aa82023-02-20 10:50:15 +0000261 const mmap_region_t bl_regions[] = {
262 MAP_BL31_TOTAL,
263 MAP_BL31_RO,
Chen Baozi097a43a2023-03-12 20:58:04 +0800264#if USE_COHERENT_MEM
Chen Baozif7d9aa82023-02-20 10:50:15 +0000265 MAP_BL_COHERENT_RAM,
Chen Baozi097a43a2023-03-12 20:58:04 +0800266#endif
Jean-Philippe Brucker721b83d2023-09-07 18:13:07 +0100267#if ENABLE_RME
268 MAP_GPT_L0_REGION,
269 MAP_GPT_L1_REGION,
270 MAP_RMM_SHARED_MEM,
271#endif
Chen Baozif7d9aa82023-02-20 10:50:15 +0000272 {0}
273 };
274
275 setup_page_tables(bl_regions, plat_qemu_get_mmap());
276
277 enable_mmu_el3(0);
Jean-Philippe Brucker4453ba92023-09-07 18:47:48 +0100278
279#if ENABLE_RME
Jean-Philippe Brucker997dda02024-08-16 09:49:38 +0100280 /* Initialise and enable granule protection after MMU. */
281 bl31_plat_gpt_setup();
282
Jean-Philippe Brucker4453ba92023-09-07 18:47:48 +0100283 /*
284 * Initialise Granule Protection library and enable GPC for the primary
285 * processor. The tables have already been initialized by a previous BL
286 * stage, so there is no need to provide any PAS here. This function
287 * sets up pointers to those tables.
288 */
Jean-Philippe Brucker8d83b592025-02-11 14:10:34 +0000289 if (gpt_runtime_init(BITLOCK_BASE, BITLOCK_SIZE) < 0) {
Jean-Philippe Brucker4453ba92023-09-07 18:47:48 +0100290 ERROR("gpt_runtime_init() failed!\n");
291 panic();
292 }
293#endif /* ENABLE_RME */
294
Jens Wiklander52c798e2015-12-07 14:37:10 +0100295}
296
Maxim Uvarova0b85c22020-12-14 10:17:44 +0000297static void qemu_gpio_init(void)
298{
299#ifdef SECURE_GPIO_BASE
300 pl061_gpio_init();
301 pl061_gpio_register(SECURE_GPIO_BASE, 0);
302#endif
303}
304
Jens Wiklander52c798e2015-12-07 14:37:10 +0100305void bl31_platform_setup(void)
306{
Hongbo Zhang32338ec2018-04-19 13:06:07 +0800307 plat_qemu_gic_init();
Maxim Uvarova0b85c22020-12-14 10:17:44 +0000308 qemu_gpio_init();
Jens Wiklander52c798e2015-12-07 14:37:10 +0100309}
310
311unsigned int plat_get_syscnt_freq2(void)
312{
Marcin Juszkiewiczb9a35532024-04-22 17:27:56 +0200313 return read_cntfrq_el0();
Jens Wiklander52c798e2015-12-07 14:37:10 +0100314}
315
316/*******************************************************************************
317 * Return a pointer to the 'entry_point_info' structure of the next image
318 * for the security state specified. BL3-3 corresponds to the non-secure
319 * image type while BL3-2 corresponds to the secure image type. A NULL
320 * pointer is returned if the image does not exist.
321 ******************************************************************************/
322entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
323{
324 entry_point_info_t *next_image_info;
325
326 assert(sec_state_is_valid(type));
Jean-Philippe Bruckerf304bd62023-09-07 17:33:22 +0100327 if (type == NON_SECURE) {
328 next_image_info = &bl33_image_ep_info;
329 }
330#if ENABLE_RME
331 else if (type == REALM) {
332 next_image_info = &rmm_image_ep_info;
333 }
334#endif
335 else {
336 next_image_info = &bl32_image_ep_info;
337 }
338
Jens Wiklander52c798e2015-12-07 14:37:10 +0100339 /*
340 * None of the images on the ARM development platforms can have 0x0
341 * as the entrypoint
342 */
343 if (next_image_info->pc)
344 return next_image_info;
345 else
346 return NULL;
347}
Raymond Maobb653862023-10-04 09:58:29 -0700348
349void bl31_plat_runtime_setup(void)
350{
Raymond Maobb653862023-10-04 09:58:29 -0700351#if TRANSFER_LIST
352 if (bl31_tl) {
353 /*
Raymond Maoac158782024-12-27 07:20:25 -0800354 * Relocate the TL from S to NS memory before EL3 exit
Raymond Maobb653862023-10-04 09:58:29 -0700355 * to reflect all changes in TL done by BL32
356 */
Raymond Maoac158782024-12-27 07:20:25 -0800357 if (!transfer_list_relocate(bl31_tl, (void *)FW_NS_HANDOFF_BASE,
358 bl31_tl->max_size))
359 ERROR("Relocate TL to NS memory failed\n");
Raymond Maobb653862023-10-04 09:58:29 -0700360 }
361#endif
Jens Wiklander7ad51de2024-03-01 09:07:19 +0100362
363 console_flush();
364 console_switch_state(CONSOLE_FLAG_RUNTIME);
Raymond Maobb653862023-10-04 09:58:29 -0700365}