1b4315306SDan Handley /* 286e4859aSRohit Mathew * Copyright (c) 2015-2024, 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> 2209d40e0eSAntonio Nino Diaz #ifdef SPD_opteed 2309d40e0eSAntonio Nino Diaz #include <lib/optee_utils.h> 2409d40e0eSAntonio Nino Diaz #endif 2509d40e0eSAntonio Nino Diaz #include <lib/utils.h> 26bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h> 2709d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 2809d40e0eSAntonio Nino Diaz 29b4315306SDan Handley /* Data structure which holds the extents of the trusted SRAM for BL2 */ 30b4315306SDan Handley static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE); 31b4315306SDan Handley 32a07c101aSManish V Badarkhe /* Base address of fw_config received from BL1 */ 33d74c6b83SJimmy Brisson static uintptr_t config_base; 34a07c101aSManish V Badarkhe 35caf4eca1SSoby Mathew /* 3604e06973SManish V Badarkhe * Check that BL2_BASE is above ARM_FW_CONFIG_LIMIT. This reserved page is 37c099cd39SSoby Mathew * for `meminfo_t` data structure and fw_configs passed from BL1. 38caf4eca1SSoby Mathew */ 3904e06973SManish V Badarkhe CASSERT(BL2_BASE >= ARM_FW_CONFIG_LIMIT, assert_bl2_base_overflows); 40caf4eca1SSoby Mathew 41a8aa7fecSYatharth Kochar /* Weak definitions may be overridden in specific ARM standard platform */ 420c306cc0SSoby Mathew #pragma weak bl2_early_platform_setup2 43a8aa7fecSYatharth Kochar #pragma weak bl2_platform_setup 44a8aa7fecSYatharth Kochar #pragma weak bl2_plat_arch_setup 45a8aa7fecSYatharth Kochar #pragma weak bl2_plat_sec_mem_layout 46a8aa7fecSYatharth Kochar 474bb72c47SZelalem Aweke #if ENABLE_RME 484bb72c47SZelalem Aweke #define MAP_BL2_TOTAL MAP_REGION_FLAT( \ 494bb72c47SZelalem Aweke bl2_tzram_layout.total_base, \ 504bb72c47SZelalem Aweke bl2_tzram_layout.total_size, \ 514bb72c47SZelalem Aweke MT_MEMORY | MT_RW | MT_ROOT) 524bb72c47SZelalem Aweke #else 53d323af9eSDaniel Boulby #define MAP_BL2_TOTAL MAP_REGION_FLAT( \ 54d323af9eSDaniel Boulby bl2_tzram_layout.total_base, \ 55d323af9eSDaniel Boulby bl2_tzram_layout.total_size, \ 56d323af9eSDaniel Boulby MT_MEMORY | MT_RW | MT_SECURE) 574bb72c47SZelalem Aweke #endif /* ENABLE_RME */ 584a581b06SDimitris Papastamos 59490eeb04SDaniel Boulby #pragma weak arm_bl2_plat_handle_post_image_load 604a581b06SDimitris Papastamos 61b4315306SDan Handley /******************************************************************************* 62b4315306SDan Handley * BL1 has passed the extents of the trusted SRAM that should be visible to BL2 63b4315306SDan Handley * in x0. This memory layout is sitting at the base of the free trusted SRAM. 64b4315306SDan Handley * Copy it to a safe location before its reclaimed by later BL2 functionality. 65b4315306SDan Handley ******************************************************************************/ 6604e06973SManish V Badarkhe void arm_bl2_early_platform_setup(uintptr_t fw_config, 676c77e749SSandrine Bailleux struct meminfo *mem_layout) 68b4315306SDan Handley { 6908ec77c7SGovindraj Raja int __maybe_unused ret; 7008ec77c7SGovindraj Raja 71b4315306SDan Handley /* Initialize the console to provide early debug support */ 7288a0523eSAntonio Nino Diaz arm_console_boot_init(); 73b4315306SDan Handley 74b4315306SDan Handley /* Setup the BL2 memory layout */ 75b4315306SDan Handley bl2_tzram_layout = *mem_layout; 76b4315306SDan Handley 77d74c6b83SJimmy Brisson config_base = fw_config; 789814bfc1SLouis Mayencourt 79b4315306SDan Handley /* Initialise the IO layer and register platform IO devices */ 80b4315306SDan Handley plat_arm_io_setup(); 81ef1daa42SManish V Badarkhe 82ef1daa42SManish V Badarkhe /* Load partition table */ 83ef1daa42SManish V Badarkhe #if ARM_GPT_SUPPORT 8408ec77c7SGovindraj Raja ret = gpt_partition_init(); 8508ec77c7SGovindraj Raja if (ret != 0) { 8608ec77c7SGovindraj Raja ERROR("GPT partition initialisation failed!\n"); 8708ec77c7SGovindraj Raja panic(); 8808ec77c7SGovindraj Raja } 89ef1daa42SManish V Badarkhe 9008ec77c7SGovindraj Raja #endif /* ARM_GPT_SUPPORT */ 91b4315306SDan Handley } 92b4315306SDan Handley 930c306cc0SSoby Mathew void bl2_early_platform_setup2(u_register_t arg0, u_register_t arg1, u_register_t arg2, u_register_t arg3) 94b4315306SDan Handley { 95cab0b5b0SSoby Mathew arm_bl2_early_platform_setup((uintptr_t)arg0, (meminfo_t *)arg1); 96cab0b5b0SSoby Mathew 9718e279ebSSoby Mathew generic_delay_timer_init(); 98b4315306SDan Handley } 99b4315306SDan Handley 100b4315306SDan Handley /* 1016e79f9fdSSoby Mathew * Perform BL2 preload setup. Currently we initialise the dynamic 1026e79f9fdSSoby Mathew * configuration here. 103b4315306SDan Handley */ 1046e79f9fdSSoby Mathew void bl2_plat_preload_setup(void) 105b4315306SDan Handley { 106cab0b5b0SSoby Mathew arm_bl2_dyn_cfg_init(); 107ef1daa42SManish V Badarkhe 1082f1177b2SManish V Badarkhe #if ARM_GPT_SUPPORT && !PSA_FWU_SUPPORT 1092f1177b2SManish V Badarkhe /* Always use the FIP from bank 0 */ 1102f1177b2SManish V Badarkhe arm_set_fip_addr(0U); 1112f1177b2SManish V Badarkhe #endif /* ARM_GPT_SUPPORT && !PSA_FWU_SUPPORT */ 1126e79f9fdSSoby Mathew } 113cab0b5b0SSoby Mathew 1146e79f9fdSSoby Mathew /* 1156e79f9fdSSoby Mathew * Perform ARM standard platform setup. 1166e79f9fdSSoby Mathew */ 1176e79f9fdSSoby Mathew void arm_bl2_platform_setup(void) 1186e79f9fdSSoby Mathew { 119deb4b3a6SZelalem Aweke #if !ENABLE_RME 120b4315306SDan Handley /* Initialize the secure environment */ 121b4315306SDan Handley plat_arm_security_setup(); 122deb4b3a6SZelalem Aweke #endif 123f145403cSRoberto Vargas 124f145403cSRoberto Vargas #if defined(PLAT_ARM_MEM_PROT_ADDR) 125638b034cSRoberto Vargas arm_nor_psci_do_static_mem_protect(); 126f145403cSRoberto Vargas #endif 127b4315306SDan Handley } 128b4315306SDan Handley 129b4315306SDan Handley void bl2_platform_setup(void) 130b4315306SDan Handley { 131b4315306SDan Handley arm_bl2_platform_setup(); 132b4315306SDan Handley } 133b4315306SDan Handley 134b4315306SDan Handley /******************************************************************************* 135deb4b3a6SZelalem Aweke * Perform the very early platform specific architectural setup here. 136deb4b3a6SZelalem Aweke * When RME is enabled the secure environment is initialised before 137deb4b3a6SZelalem Aweke * initialising and enabling Granule Protection. 138deb4b3a6SZelalem Aweke * This function initialises the MMU in a quick and dirty way. 139b4315306SDan Handley ******************************************************************************/ 140b4315306SDan Handley void arm_bl2_plat_arch_setup(void) 141b4315306SDan Handley { 142b65dfe40SSandrine Bailleux #if USE_COHERENT_MEM 143b65dfe40SSandrine Bailleux /* Ensure ARM platforms don't use coherent memory in BL2. */ 144d323af9eSDaniel Boulby assert((BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE) == 0U); 145b4315306SDan Handley #endif 146d323af9eSDaniel Boulby 147d323af9eSDaniel Boulby const mmap_region_t bl_regions[] = { 148d323af9eSDaniel Boulby MAP_BL2_TOTAL, 1492ecaafd2SDaniel Boulby ARM_MAP_BL_RO, 1501eb735d7SRoberto Vargas #if USE_ROMLIB 1511eb735d7SRoberto Vargas ARM_MAP_ROMLIB_CODE, 1521eb735d7SRoberto Vargas ARM_MAP_ROMLIB_DATA, 1531eb735d7SRoberto Vargas #endif 154a07c101aSManish V Badarkhe ARM_MAP_BL_CONFIG_REGION, 155c8720729SZelalem Aweke #if ENABLE_RME 156c8720729SZelalem Aweke ARM_MAP_L0_GPT_REGION, 157c8720729SZelalem Aweke #endif 158d323af9eSDaniel Boulby {0} 159d323af9eSDaniel Boulby }; 160d323af9eSDaniel Boulby 161deb4b3a6SZelalem Aweke #if ENABLE_RME 162deb4b3a6SZelalem Aweke /* Initialise the secure environment */ 163deb4b3a6SZelalem Aweke plat_arm_security_setup(); 164deb4b3a6SZelalem Aweke #endif 1650916c38dSRoberto Vargas setup_page_tables(bl_regions, plat_arm_get_mmap()); 1666fe8aa2fSYatharth Kochar 167402b3cf8SJulius Werner #ifdef __aarch64__ 168deb4b3a6SZelalem Aweke #if ENABLE_RME 169deb4b3a6SZelalem Aweke /* BL2 runs in EL3 when RME enabled. */ 170deb4b3a6SZelalem Aweke assert(get_armv9_2_feat_rme_support() != 0U); 171deb4b3a6SZelalem Aweke enable_mmu_el3(0); 172f19dc624Sjohpow01 173f19dc624Sjohpow01 /* Initialise and enable granule protection after MMU. */ 174*341df6afSRohit Mathew arm_gpt_setup(); 175deb4b3a6SZelalem Aweke #else 176b5fa6563SSandrine Bailleux enable_mmu_el1(0); 177deb4b3a6SZelalem Aweke #endif 178402b3cf8SJulius Werner #else 179402b3cf8SJulius Werner enable_mmu_svc_mon(0); 1806fe8aa2fSYatharth Kochar #endif 1811eb735d7SRoberto Vargas 1821eb735d7SRoberto Vargas arm_setup_romlib(); 183b4315306SDan Handley } 184b4315306SDan Handley 185b4315306SDan Handley void bl2_plat_arch_setup(void) 186b4315306SDan Handley { 187a07c101aSManish V Badarkhe const struct dyn_cfg_dtb_info_t *tb_fw_config_info; 188a07c101aSManish V Badarkhe 189b4315306SDan Handley arm_bl2_plat_arch_setup(); 190a07c101aSManish V Badarkhe 191a07c101aSManish V Badarkhe /* Fill the properties struct with the info from the config dtb */ 192d74c6b83SJimmy Brisson fconf_populate("FW_CONFIG", config_base); 193a07c101aSManish V Badarkhe 194a07c101aSManish V Badarkhe /* TB_FW_CONFIG was also loaded by BL1 */ 195a07c101aSManish V Badarkhe tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID); 196a07c101aSManish V Badarkhe assert(tb_fw_config_info != NULL); 197a07c101aSManish V Badarkhe 198a07c101aSManish V Badarkhe fconf_populate("TB_FW", tb_fw_config_info->config_addr); 199b4315306SDan Handley } 200b4315306SDan Handley 20107570d59SYatharth Kochar int arm_bl2_handle_post_image_load(unsigned int image_id) 202a8aa7fecSYatharth Kochar { 203a8aa7fecSYatharth Kochar int err = 0; 204a8aa7fecSYatharth Kochar bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id); 20554661cd2SSummer Qin #ifdef SPD_opteed 20654661cd2SSummer Qin bl_mem_params_node_t *pager_mem_params = NULL; 20754661cd2SSummer Qin bl_mem_params_node_t *paged_mem_params = NULL; 20854661cd2SSummer Qin #endif 209466bb285SZelalem assert(bl_mem_params != NULL); 210a8aa7fecSYatharth Kochar 211a8aa7fecSYatharth Kochar switch (image_id) { 212402b3cf8SJulius Werner #ifdef __aarch64__ 213a8aa7fecSYatharth Kochar case BL32_IMAGE_ID: 21454661cd2SSummer Qin #ifdef SPD_opteed 21554661cd2SSummer Qin pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID); 21654661cd2SSummer Qin assert(pager_mem_params); 21754661cd2SSummer Qin 21854661cd2SSummer Qin paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID); 21954661cd2SSummer Qin assert(paged_mem_params); 22054661cd2SSummer Qin 22154661cd2SSummer Qin err = parse_optee_header(&bl_mem_params->ep_info, 22254661cd2SSummer Qin &pager_mem_params->image_info, 22354661cd2SSummer Qin &paged_mem_params->image_info); 22454661cd2SSummer Qin if (err != 0) { 22554661cd2SSummer Qin WARN("OPTEE header parse error.\n"); 22654661cd2SSummer Qin } 22754661cd2SSummer Qin #endif 228a8aa7fecSYatharth Kochar bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl32_entry(); 229a8aa7fecSYatharth Kochar break; 2306fe8aa2fSYatharth Kochar #endif 231a8aa7fecSYatharth Kochar 232a8aa7fecSYatharth Kochar case BL33_IMAGE_ID: 233a8aa7fecSYatharth Kochar /* BL33 expects to receive the primary CPU MPID (through r0) */ 234a8aa7fecSYatharth Kochar bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr(); 235a8aa7fecSYatharth Kochar bl_mem_params->ep_info.spsr = arm_get_spsr_for_bl33_entry(); 236a8aa7fecSYatharth Kochar break; 237a8aa7fecSYatharth Kochar 238a8aa7fecSYatharth Kochar #ifdef SCP_BL2_BASE 239a8aa7fecSYatharth Kochar case SCP_BL2_IMAGE_ID: 240a8aa7fecSYatharth Kochar /* The subsequent handling of SCP_BL2 is platform specific */ 241a8aa7fecSYatharth Kochar err = plat_arm_bl2_handle_scp_bl2(&bl_mem_params->image_info); 242a8aa7fecSYatharth Kochar if (err) { 243a8aa7fecSYatharth Kochar WARN("Failure in platform-specific handling of SCP_BL2 image.\n"); 244a8aa7fecSYatharth Kochar } 245a8aa7fecSYatharth Kochar break; 246a8aa7fecSYatharth Kochar #endif 247649c48f5SJonathan Wright default: 248649c48f5SJonathan Wright /* Do nothing in default case */ 249649c48f5SJonathan Wright break; 250a8aa7fecSYatharth Kochar } 251a8aa7fecSYatharth Kochar 252a8aa7fecSYatharth Kochar return err; 253a8aa7fecSYatharth Kochar } 254a8aa7fecSYatharth Kochar 25507570d59SYatharth Kochar /******************************************************************************* 25607570d59SYatharth Kochar * This function can be used by the platforms to update/use image 25707570d59SYatharth Kochar * information for given `image_id`. 25807570d59SYatharth Kochar ******************************************************************************/ 259490eeb04SDaniel Boulby int arm_bl2_plat_handle_post_image_load(unsigned int image_id) 26007570d59SYatharth Kochar { 26146789a7cSBalint Dobszay #if defined(SPD_spmd) && BL2_ENABLE_SP_LOAD 262cb3b5344SManish Pandey /* For Secure Partitions we don't need post processing */ 263cb3b5344SManish Pandey if ((image_id >= (MAX_NUMBER_IDS - MAX_SP_IDS)) && 264cb3b5344SManish Pandey (image_id < MAX_NUMBER_IDS)) { 265cb3b5344SManish Pandey return 0; 266cb3b5344SManish Pandey } 267cb3b5344SManish Pandey #endif 26807570d59SYatharth Kochar return arm_bl2_handle_post_image_load(image_id); 26907570d59SYatharth Kochar } 270