1b4315306SDan Handley /* 28187b95eSJayanth Dodderi Chidanand * Copyright (c) 2015-2025, Arm Limited and Contributors. All rights reserved. 3b4315306SDan Handley * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5b4315306SDan Handley */ 6b4315306SDan Handley 7a8aa7fecSYatharth Kochar #include <assert.h> 8b4315306SDan Handley #include <string.h> 909d40e0eSAntonio Nino Diaz 1009d40e0eSAntonio Nino Diaz #include <platform_def.h> 1109d40e0eSAntonio Nino Diaz 12deb4b3a6SZelalem Aweke #include <arch_features.h> 1309d40e0eSAntonio Nino Diaz #include <arch_helpers.h> 1409d40e0eSAntonio Nino Diaz #include <common/bl_common.h> 1509d40e0eSAntonio Nino Diaz #include <common/debug.h> 1609d40e0eSAntonio Nino Diaz #include <common/desc_image_load.h> 1709d40e0eSAntonio Nino Diaz #include <drivers/generic_delay_timer.h> 18ef1daa42SManish V Badarkhe #include <drivers/partition/partition.h> 199814bfc1SLouis Mayencourt #include <lib/fconf/fconf.h> 2082869675SManish V Badarkhe #include <lib/fconf/fconf_dyn_cfg_getter.h> 21f19dc624Sjohpow01 #include <lib/gpt_rme/gpt_rme.h> 22a5566f65SHarrison Mutai #if TRANSFER_LIST 23b5d0740eSHarrison Mutai #include <transfer_list.h> 24a5566f65SHarrison Mutai #endif 2509d40e0eSAntonio Nino Diaz #ifdef SPD_opteed 2609d40e0eSAntonio Nino Diaz #include <lib/optee_utils.h> 2709d40e0eSAntonio Nino Diaz #endif 2809d40e0eSAntonio Nino Diaz #include <lib/utils.h> 29bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h> 3009d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 3109d40e0eSAntonio Nino Diaz 32b4315306SDan Handley /* Data structure which holds the extents of the trusted SRAM for BL2 */ 33b4315306SDan Handley static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE); 34b4315306SDan Handley 35a07c101aSManish V Badarkhe /* Base address of fw_config received from BL1 */ 369c11ed7eSHarrison Mutai static uintptr_t config_base __unused; 37a07c101aSManish V Badarkhe 38caf4eca1SSoby Mathew /* 3904e06973SManish V Badarkhe * Check that BL2_BASE is above ARM_FW_CONFIG_LIMIT. This reserved page is 40c099cd39SSoby Mathew * for `meminfo_t` data structure and fw_configs passed from BL1. 41caf4eca1SSoby Mathew */ 429c11ed7eSHarrison Mutai #if TRANSFER_LIST 439c11ed7eSHarrison Mutai CASSERT(BL2_BASE >= PLAT_ARM_EL3_FW_HANDOFF_BASE + PLAT_ARM_FW_HANDOFF_SIZE, 449c11ed7eSHarrison Mutai assert_bl2_base_overflows); 458d5c7627SDivin Raj #elif !RESET_TO_BL2 4604e06973SManish V Badarkhe CASSERT(BL2_BASE >= ARM_FW_CONFIG_LIMIT, assert_bl2_base_overflows); 479c11ed7eSHarrison Mutai #endif /* TRANSFER_LIST */ 48caf4eca1SSoby Mathew 49a8aa7fecSYatharth Kochar /* Weak definitions may be overridden in specific ARM standard platform */ 500c306cc0SSoby Mathew #pragma weak bl2_early_platform_setup2 51a8aa7fecSYatharth Kochar #pragma weak bl2_platform_setup 52a8aa7fecSYatharth Kochar #pragma weak bl2_plat_arch_setup 53a8aa7fecSYatharth Kochar #pragma weak bl2_plat_sec_mem_layout 54a8aa7fecSYatharth Kochar 554bb72c47SZelalem Aweke #define MAP_BL2_TOTAL MAP_REGION_FLAT( \ 564bb72c47SZelalem Aweke bl2_tzram_layout.total_base, \ 574bb72c47SZelalem Aweke bl2_tzram_layout.total_size, \ 58875423deSOlivier Deprez MT_MEMORY | MT_RW | EL3_PAS) 594a581b06SDimitris Papastamos 60490eeb04SDaniel Boulby #pragma weak arm_bl2_plat_handle_post_image_load 614a581b06SDimitris Papastamos 62d5705719SHarrison Mutai struct transfer_list_header *secure_tl __unused; 63a5566f65SHarrison Mutai 64b4315306SDan Handley /******************************************************************************* 65b4315306SDan Handley * BL1 has passed the extents of the trusted SRAM that should be visible to BL2 66b4315306SDan Handley * in x0. This memory layout is sitting at the base of the free trusted SRAM. 67b4315306SDan Handley * Copy it to a safe location before its reclaimed by later BL2 functionality. 68b4315306SDan Handley ******************************************************************************/ 698187b95eSJayanth Dodderi Chidanand void arm_bl2_early_platform_setup(u_register_t arg0, u_register_t arg1, 708187b95eSJayanth Dodderi Chidanand u_register_t arg2, u_register_t arg3) 71b4315306SDan Handley { 729c11ed7eSHarrison Mutai struct transfer_list_entry *te __unused; 7308ec77c7SGovindraj Raja int __maybe_unused ret; 7408ec77c7SGovindraj Raja 75b4315306SDan Handley /* Initialize the console to provide early debug support */ 7688a0523eSAntonio Nino Diaz arm_console_boot_init(); 77b4315306SDan Handley 789c11ed7eSHarrison Mutai #if TRANSFER_LIST 798187b95eSJayanth Dodderi Chidanand secure_tl = (struct transfer_list_header *)arg3; 809c11ed7eSHarrison Mutai 81abdb953bSHarrison Mutai te = transfer_list_find(secure_tl, TL_TAG_SRAM_LAYOUT); 829c11ed7eSHarrison Mutai assert(te != NULL); 839c11ed7eSHarrison Mutai 849c11ed7eSHarrison Mutai bl2_tzram_layout = *(meminfo_t *)transfer_list_entry_data(te); 859c11ed7eSHarrison Mutai transfer_list_rem(secure_tl, te); 869c11ed7eSHarrison Mutai #else 878187b95eSJayanth Dodderi Chidanand config_base = (uintptr_t)arg0; 889c11ed7eSHarrison Mutai 89b4315306SDan Handley /* Setup the BL2 memory layout */ 908187b95eSJayanth Dodderi Chidanand bl2_tzram_layout = *(meminfo_t *)arg1; 918187b95eSJayanth Dodderi Chidanand #endif /* TRANSFER_LIST */ 929814bfc1SLouis Mayencourt 93b4315306SDan Handley /* Initialise the IO layer and register platform IO devices */ 94b4315306SDan Handley plat_arm_io_setup(); 95ef1daa42SManish V Badarkhe 96ef1daa42SManish V Badarkhe /* Load partition table */ 97ef1daa42SManish V Badarkhe #if ARM_GPT_SUPPORT 9808ec77c7SGovindraj Raja ret = gpt_partition_init(); 9908ec77c7SGovindraj Raja if (ret != 0) { 10008ec77c7SGovindraj Raja ERROR("GPT partition initialisation failed!\n"); 10108ec77c7SGovindraj Raja panic(); 10208ec77c7SGovindraj Raja } 103ef1daa42SManish V Badarkhe 10408ec77c7SGovindraj Raja #endif /* ARM_GPT_SUPPORT */ 105b4315306SDan Handley } 106b4315306SDan Handley 1078187b95eSJayanth Dodderi Chidanand void bl2_early_platform_setup2(u_register_t arg0, u_register_t arg1, 1088187b95eSJayanth Dodderi Chidanand u_register_t arg2, u_register_t arg3) 109b4315306SDan Handley { 1108187b95eSJayanth Dodderi Chidanand arm_bl2_early_platform_setup(arg0, arg1, arg2, arg3); 111cab0b5b0SSoby Mathew 11218e279ebSSoby Mathew generic_delay_timer_init(); 113b4315306SDan Handley } 114b4315306SDan Handley 115b4315306SDan Handley /* 1166e79f9fdSSoby Mathew * Perform BL2 preload setup. Currently we initialise the dynamic 1176e79f9fdSSoby Mathew * configuration here. 118b4315306SDan Handley */ 1196e79f9fdSSoby Mathew void bl2_plat_preload_setup(void) 120b4315306SDan Handley { 121a5566f65SHarrison Mutai #if TRANSFER_LIST 122f019c801SHarrison Mutai /* Assume the secure TL hasn't been initialised if BL2 is running at EL3. */ 123f019c801SHarrison Mutai #if RESET_TO_BL2 124d5705719SHarrison Mutai secure_tl = transfer_list_ensure((void *)PLAT_ARM_EL3_FW_HANDOFF_BASE, 125f019c801SHarrison Mutai PLAT_ARM_FW_HANDOFF_SIZE); 126f019c801SHarrison Mutai 127f019c801SHarrison Mutai if (secure_tl == NULL) { 128f019c801SHarrison Mutai ERROR("Secure transfer list initialisation failed!\n"); 129f019c801SHarrison Mutai panic(); 130f019c801SHarrison Mutai } 131f019c801SHarrison Mutai #endif 132a5566f65SHarrison Mutai arm_transfer_list_dyn_cfg_init(secure_tl); 133a5566f65SHarrison Mutai #else 134973e0b7fSDivin Raj #if ARM_FW_CONFIG_LOAD_ENABLE 135973e0b7fSDivin Raj arm_bl2_el3_plat_config_load(); 136973e0b7fSDivin Raj #endif /* ARM_FW_CONFIG_LOAD_ENABLE */ 137cab0b5b0SSoby Mathew arm_bl2_dyn_cfg_init(); 138a5566f65SHarrison Mutai #endif 139ef1daa42SManish V Badarkhe 1402f1177b2SManish V Badarkhe #if ARM_GPT_SUPPORT && !PSA_FWU_SUPPORT 1412f1177b2SManish V Badarkhe /* Always use the FIP from bank 0 */ 1422f1177b2SManish V Badarkhe arm_set_fip_addr(0U); 1432f1177b2SManish V Badarkhe #endif /* ARM_GPT_SUPPORT && !PSA_FWU_SUPPORT */ 1446e79f9fdSSoby Mathew } 145cab0b5b0SSoby Mathew 1466e79f9fdSSoby Mathew /* 1476e79f9fdSSoby Mathew * Perform ARM standard platform setup. 1486e79f9fdSSoby Mathew */ 1496e79f9fdSSoby Mathew void arm_bl2_platform_setup(void) 1506e79f9fdSSoby Mathew { 151deb4b3a6SZelalem Aweke #if !ENABLE_RME 152b4315306SDan Handley /* Initialize the secure environment */ 153b4315306SDan Handley plat_arm_security_setup(); 154deb4b3a6SZelalem Aweke #endif 155f145403cSRoberto Vargas 156f145403cSRoberto Vargas #if defined(PLAT_ARM_MEM_PROT_ADDR) 157638b034cSRoberto Vargas arm_nor_psci_do_static_mem_protect(); 158f145403cSRoberto Vargas #endif 159b4315306SDan Handley } 160b4315306SDan Handley 161b4315306SDan Handley void bl2_platform_setup(void) 162b4315306SDan Handley { 163b4315306SDan Handley arm_bl2_platform_setup(); 164b4315306SDan Handley } 165b4315306SDan Handley 166b4315306SDan Handley /******************************************************************************* 167deb4b3a6SZelalem Aweke * Perform the very early platform specific architectural setup here. 168deb4b3a6SZelalem Aweke * When RME is enabled the secure environment is initialised before 169deb4b3a6SZelalem Aweke * initialising and enabling Granule Protection. 170deb4b3a6SZelalem Aweke * This function initialises the MMU in a quick and dirty way. 171b4315306SDan Handley ******************************************************************************/ 172b4315306SDan Handley void arm_bl2_plat_arch_setup(void) 173b4315306SDan Handley { 174b65dfe40SSandrine Bailleux #if USE_COHERENT_MEM 175b65dfe40SSandrine Bailleux /* Ensure ARM platforms don't use coherent memory in BL2. */ 176d323af9eSDaniel Boulby assert((BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE) == 0U); 177b4315306SDan Handley #endif 178d323af9eSDaniel Boulby 179d323af9eSDaniel Boulby const mmap_region_t bl_regions[] = { 180d323af9eSDaniel Boulby MAP_BL2_TOTAL, 1812ecaafd2SDaniel Boulby ARM_MAP_BL_RO, 1821eb735d7SRoberto Vargas #if USE_ROMLIB 1831eb735d7SRoberto Vargas ARM_MAP_ROMLIB_CODE, 1841eb735d7SRoberto Vargas ARM_MAP_ROMLIB_DATA, 1851eb735d7SRoberto Vargas #endif 1869c11ed7eSHarrison Mutai #if !TRANSFER_LIST 187a07c101aSManish V Badarkhe ARM_MAP_BL_CONFIG_REGION, 1889c11ed7eSHarrison Mutai #endif /* TRANSFER_LIST */ 189c8720729SZelalem Aweke #if ENABLE_RME 190c8720729SZelalem Aweke ARM_MAP_L0_GPT_REGION, 191c8720729SZelalem Aweke #endif 192d323af9eSDaniel Boulby { 0 } 193d323af9eSDaniel Boulby }; 194d323af9eSDaniel Boulby 195deb4b3a6SZelalem Aweke #if ENABLE_RME 196deb4b3a6SZelalem Aweke /* Initialise the secure environment */ 197deb4b3a6SZelalem Aweke plat_arm_security_setup(); 198deb4b3a6SZelalem Aweke #endif 1990916c38dSRoberto Vargas setup_page_tables(bl_regions, plat_arm_get_mmap()); 2006fe8aa2fSYatharth Kochar 201402b3cf8SJulius Werner #ifdef __aarch64__ 202deb4b3a6SZelalem Aweke #if ENABLE_RME 203deb4b3a6SZelalem Aweke /* BL2 runs in EL3 when RME enabled. */ 204aaaf2cc3SSona Mathew assert(is_feat_rme_present()); 205deb4b3a6SZelalem Aweke enable_mmu_el3(0); 206f19dc624Sjohpow01 207f19dc624Sjohpow01 /* Initialise and enable granule protection after MMU. */ 208341df6afSRohit Mathew arm_gpt_setup(); 209deb4b3a6SZelalem Aweke #else 210b5fa6563SSandrine Bailleux enable_mmu_el1(0); 211deb4b3a6SZelalem Aweke #endif 212402b3cf8SJulius Werner #else 213402b3cf8SJulius Werner enable_mmu_svc_mon(0); 2146fe8aa2fSYatharth Kochar #endif 2151eb735d7SRoberto Vargas 2161eb735d7SRoberto Vargas arm_setup_romlib(); 217b4315306SDan Handley } 218b4315306SDan Handley 219b4315306SDan Handley void bl2_plat_arch_setup(void) 220b4315306SDan Handley { 2219c11ed7eSHarrison Mutai const struct dyn_cfg_dtb_info_t *tb_fw_config_info __unused; 2229c11ed7eSHarrison Mutai struct transfer_list_entry *te __unused; 223b4315306SDan Handley arm_bl2_plat_arch_setup(); 224a07c101aSManish V Badarkhe 2259c11ed7eSHarrison Mutai #if TRANSFER_LIST 226ada4e59dSHarrison Mutai #if CRYPTO_SUPPORT 227ada4e59dSHarrison Mutai te = arm_transfer_list_set_heap_info(secure_tl); 2289c11ed7eSHarrison Mutai transfer_list_rem(secure_tl, te); 229ada4e59dSHarrison Mutai #endif /* CRYPTO_SUPPORT */ 2309c11ed7eSHarrison Mutai #else 231a07c101aSManish V Badarkhe /* Fill the properties struct with the info from the config dtb */ 232d74c6b83SJimmy Brisson fconf_populate("FW_CONFIG", config_base); 233a07c101aSManish V Badarkhe 234a07c101aSManish V Badarkhe /* TB_FW_CONFIG was also loaded by BL1 */ 235a07c101aSManish V Badarkhe tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID); 236a07c101aSManish V Badarkhe assert(tb_fw_config_info != NULL); 237a07c101aSManish V Badarkhe 238a07c101aSManish V Badarkhe fconf_populate("TB_FW", tb_fw_config_info->config_addr); 239ada4e59dSHarrison Mutai #endif /* TRANSFER_LIST */ 240b4315306SDan Handley } 241b4315306SDan Handley 24207570d59SYatharth Kochar int arm_bl2_handle_post_image_load(unsigned int image_id) 243a8aa7fecSYatharth Kochar { 244a8aa7fecSYatharth Kochar int err = 0; 245a8aa7fecSYatharth Kochar bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id); 24654661cd2SSummer Qin #ifdef SPD_opteed 24754661cd2SSummer Qin bl_mem_params_node_t *pager_mem_params = NULL; 24854661cd2SSummer Qin bl_mem_params_node_t *paged_mem_params = NULL; 24954661cd2SSummer Qin #endif 250466bb285SZelalem assert(bl_mem_params != NULL); 251a8aa7fecSYatharth Kochar 252a8aa7fecSYatharth Kochar switch (image_id) { 253402b3cf8SJulius Werner #ifdef __aarch64__ 254a8aa7fecSYatharth Kochar case BL32_IMAGE_ID: 25554661cd2SSummer Qin #ifdef SPD_opteed 25654661cd2SSummer Qin pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID); 25754661cd2SSummer Qin assert(pager_mem_params); 25854661cd2SSummer Qin 25954661cd2SSummer Qin paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID); 26054661cd2SSummer Qin assert(paged_mem_params); 26154661cd2SSummer Qin 26254661cd2SSummer Qin err = parse_optee_header(&bl_mem_params->ep_info, 26354661cd2SSummer Qin &pager_mem_params->image_info, 26454661cd2SSummer Qin &paged_mem_params->image_info); 26554661cd2SSummer Qin if (err != 0) { 26654661cd2SSummer Qin WARN("OPTEE header parse error.\n"); 26754661cd2SSummer Qin } 26854661cd2SSummer Qin #endif 269*01907f3fSHarrison Mutai bl_mem_params->ep_info.spsr = arm_get_spsr(BL32_IMAGE_ID); 270a8aa7fecSYatharth Kochar break; 2716fe8aa2fSYatharth Kochar #endif 272a8aa7fecSYatharth Kochar 273a8aa7fecSYatharth Kochar case BL33_IMAGE_ID: 2748946bb03SHarrison Mutai #if !USE_KERNEL_DT_CONVENTION 275a8aa7fecSYatharth Kochar /* BL33 expects to receive the primary CPU MPID (through r0) */ 276a8aa7fecSYatharth Kochar bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr(); 2778946bb03SHarrison Mutai #endif /* !USE_KERNEL_DT_CONVENTION */ 278*01907f3fSHarrison Mutai bl_mem_params->ep_info.spsr = arm_get_spsr(BL33_IMAGE_ID); 279a8aa7fecSYatharth Kochar break; 280a8aa7fecSYatharth Kochar 281a8aa7fecSYatharth Kochar #ifdef SCP_BL2_BASE 282a8aa7fecSYatharth Kochar case SCP_BL2_IMAGE_ID: 283a8aa7fecSYatharth Kochar /* The subsequent handling of SCP_BL2 is platform specific */ 284a8aa7fecSYatharth Kochar err = plat_arm_bl2_handle_scp_bl2(&bl_mem_params->image_info); 285a8aa7fecSYatharth Kochar if (err) { 286a8aa7fecSYatharth Kochar WARN("Failure in platform-specific handling of SCP_BL2 image.\n"); 287a8aa7fecSYatharth Kochar } 288a8aa7fecSYatharth Kochar break; 289a8aa7fecSYatharth Kochar #endif 290649c48f5SJonathan Wright default: 291649c48f5SJonathan Wright /* Do nothing in default case */ 292649c48f5SJonathan Wright break; 293a8aa7fecSYatharth Kochar } 294a8aa7fecSYatharth Kochar 295a8aa7fecSYatharth Kochar return err; 296a8aa7fecSYatharth Kochar } 297a8aa7fecSYatharth Kochar 29807570d59SYatharth Kochar /******************************************************************************* 29907570d59SYatharth Kochar * This function can be used by the platforms to update/use image 30007570d59SYatharth Kochar * information for given `image_id`. 30107570d59SYatharth Kochar ******************************************************************************/ 302490eeb04SDaniel Boulby int arm_bl2_plat_handle_post_image_load(unsigned int image_id) 30307570d59SYatharth Kochar { 30446789a7cSBalint Dobszay #if defined(SPD_spmd) && BL2_ENABLE_SP_LOAD 305cb3b5344SManish Pandey /* For Secure Partitions we don't need post processing */ 306cb3b5344SManish Pandey if ((image_id >= (MAX_NUMBER_IDS - MAX_SP_IDS)) && 307cb3b5344SManish Pandey (image_id < MAX_NUMBER_IDS)) { 308cb3b5344SManish Pandey return 0; 309cb3b5344SManish Pandey } 310cb3b5344SManish Pandey #endif 311a5566f65SHarrison Mutai 312a5566f65SHarrison Mutai #if TRANSFER_LIST 31300c353c4SYeoreum Yun if (image_id == HW_CONFIG_ID || image_id == TOS_FW_CONFIG_ID) { 31400c353c4SYeoreum Yun /* 31500c353c4SYeoreum Yun * Refresh the now stale checksum following loading of 31600c353c4SYeoreum Yun * HW_CONFIG or TOS_FW_CONFIG into the TL. 31700c353c4SYeoreum Yun */ 318fe94a21aSHarrison Mutai transfer_list_update_checksum(secure_tl); 319a5566f65SHarrison Mutai } 320a5566f65SHarrison Mutai #endif /* TRANSFER_LIST */ 321a5566f65SHarrison Mutai 32207570d59SYatharth Kochar return arm_bl2_handle_post_image_load(image_id); 32307570d59SYatharth Kochar } 324a5566f65SHarrison Mutai 325a5566f65SHarrison Mutai void arm_bl2_setup_next_ep_info(bl_mem_params_node_t *next_param_node) 326a5566f65SHarrison Mutai { 327cca1b72bSHarrison Mutai entry_point_info_t *ep __unused; 328b30d9043SHarrison Mutai 329b5d0740eSHarrison Mutai #if TRANSFER_LIST 330b30d9043SHarrison Mutai /* 331b30d9043SHarrison Mutai * Information might have been added to the TL before this (i.e. event log) 332b30d9043SHarrison Mutai * make sure the checksum is up to date. 333b30d9043SHarrison Mutai */ 334b30d9043SHarrison Mutai transfer_list_update_checksum(secure_tl); 335b30d9043SHarrison Mutai 336cca1b72bSHarrison Mutai ep = transfer_list_set_handoff_args(secure_tl, 337cca1b72bSHarrison Mutai &next_param_node->ep_info); 338cca1b72bSHarrison Mutai assert(ep != NULL); 339a5566f65SHarrison Mutai 340fe94a21aSHarrison Mutai arm_transfer_list_populate_ep_info(next_param_node, secure_tl); 341b5d0740eSHarrison Mutai #endif 342a5566f65SHarrison Mutai } 343