1b4315306SDan Handley /*
2abdb953bSHarrison Mutai * 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
7d323af9eSDaniel Boulby #include <assert.h>
809d40e0eSAntonio Nino Diaz
94adb10c1SIsla Mitchell #include <platform_def.h>
1009d40e0eSAntonio Nino Diaz
1109d40e0eSAntonio Nino Diaz #include <arch.h>
1209d40e0eSAntonio Nino Diaz #include <bl1/bl1.h>
1309d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
14885e2683SClaus Pedersen #include <common/debug.h>
153b5ea741SLouis Mayencourt #include <lib/fconf/fconf.h>
1682869675SManish V Badarkhe #include <lib/fconf/fconf_dyn_cfg_getter.h>
179c11ed7eSHarrison Mutai #if TRANSFER_LIST
18b5d0740eSHarrison Mutai #include <transfer_list.h>
199c11ed7eSHarrison Mutai #endif
2009d40e0eSAntonio Nino Diaz #include <lib/utils.h>
2109d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_compat.h>
22bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h>
2309d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
2409d40e0eSAntonio Nino Diaz
25b4315306SDan Handley /* Weak definitions may be overridden in specific ARM standard platform */
26b4315306SDan Handley #pragma weak bl1_early_platform_setup
27b4315306SDan Handley #pragma weak bl1_plat_arch_setup
28b4315306SDan Handley #pragma weak bl1_plat_sec_mem_layout
295fb061e7SGary Morrison #pragma weak arm_bl1_early_platform_setup
3007570d59SYatharth Kochar #pragma weak bl1_plat_prepare_exit
314da6f6cdSSathees Balya #pragma weak bl1_plat_get_next_image_id
324da6f6cdSSathees Balya #pragma weak plat_arm_bl1_fwu_needed
335fb061e7SGary Morrison #pragma weak arm_bl1_plat_arch_setup
34e31fb0faSlaurenw-arm #pragma weak arm_bl1_platform_setup
35b4315306SDan Handley
36d323af9eSDaniel Boulby #define MAP_BL1_TOTAL MAP_REGION_FLAT( \
37d323af9eSDaniel Boulby bl1_tzram_layout.total_base, \
38d323af9eSDaniel Boulby bl1_tzram_layout.total_size, \
394bb72c47SZelalem Aweke MT_MEMORY | MT_RW | EL3_PAS)
402ecaafd2SDaniel Boulby /*
412ecaafd2SDaniel Boulby * If SEPARATE_CODE_AND_RODATA=1 we define a region for each section
422ecaafd2SDaniel Boulby * otherwise one region is defined containing both
432ecaafd2SDaniel Boulby */
442ecaafd2SDaniel Boulby #if SEPARATE_CODE_AND_RODATA
452ecaafd2SDaniel Boulby #define MAP_BL1_RO MAP_REGION_FLAT( \
46d323af9eSDaniel Boulby BL_CODE_BASE, \
47d323af9eSDaniel Boulby BL1_CODE_END - BL_CODE_BASE, \
484bb72c47SZelalem Aweke MT_CODE | EL3_PAS), \
492ecaafd2SDaniel Boulby MAP_REGION_FLAT( \
50d323af9eSDaniel Boulby BL1_RO_DATA_BASE, \
51d323af9eSDaniel Boulby BL1_RO_DATA_END \
52d323af9eSDaniel Boulby - BL_RO_DATA_BASE, \
534bb72c47SZelalem Aweke MT_RO_DATA | EL3_PAS)
542ecaafd2SDaniel Boulby #else
552ecaafd2SDaniel Boulby #define MAP_BL1_RO MAP_REGION_FLAT( \
562ecaafd2SDaniel Boulby BL_CODE_BASE, \
572ecaafd2SDaniel Boulby BL1_CODE_END - BL_CODE_BASE, \
584bb72c47SZelalem Aweke MT_CODE | EL3_PAS)
592ecaafd2SDaniel Boulby #endif
60b4315306SDan Handley
61b4315306SDan Handley /* Data structure which holds the extents of the trusted SRAM for BL1*/
62b4315306SDan Handley static meminfo_t bl1_tzram_layout;
63b4315306SDan Handley
64a249a9d9SManish V Badarkhe /* Boolean variable to hold condition whether firmware update needed or not */
65a249a9d9SManish V Badarkhe static bool is_fwu_needed;
66a249a9d9SManish V Badarkhe
67d5705719SHarrison Mutai struct transfer_list_header *secure_tl;
689c11ed7eSHarrison Mutai
bl1_plat_sec_mem_layout(void)696c77e749SSandrine Bailleux struct meminfo *bl1_plat_sec_mem_layout(void)
70b4315306SDan Handley {
71b4315306SDan Handley return &bl1_tzram_layout;
72b4315306SDan Handley }
73b4315306SDan Handley
74b4315306SDan Handley /*******************************************************************************
75b4315306SDan Handley * BL1 specific platform actions shared between ARM standard platforms.
76b4315306SDan Handley ******************************************************************************/
arm_bl1_early_platform_setup(void)77b4315306SDan Handley void arm_bl1_early_platform_setup(void)
78b4315306SDan Handley {
79b4315306SDan Handley
807b4c1405SJuan Castillo #if !ARM_DISABLE_TRUSTED_WDOG
817b4c1405SJuan Castillo /* Enable watchdog */
82b0c97dafSAditya Angadi plat_arm_secure_wdt_start();
837b4c1405SJuan Castillo #endif
847b4c1405SJuan Castillo
85b4315306SDan Handley /* Initialize the console to provide early debug support */
8688a0523eSAntonio Nino Diaz arm_console_boot_init();
87b4315306SDan Handley
88b4315306SDan Handley /* Allow BL1 to see the whole Trusted RAM */
89b4315306SDan Handley bl1_tzram_layout.total_base = ARM_BL_RAM_BASE;
90b4315306SDan Handley bl1_tzram_layout.total_size = ARM_BL_RAM_SIZE;
91d5705719SHarrison Mutai
92d5705719SHarrison Mutai #if TRANSFER_LIST
932948d1f8SHarrison Mutai secure_tl = transfer_list_init((void *)PLAT_ARM_EL3_FW_HANDOFF_BASE,
94d5705719SHarrison Mutai PLAT_ARM_FW_HANDOFF_SIZE);
95d5705719SHarrison Mutai assert(secure_tl != NULL);
96d5705719SHarrison Mutai #endif
97b4315306SDan Handley }
98b4315306SDan Handley
bl1_early_platform_setup(void)99b4315306SDan Handley void bl1_early_platform_setup(void)
100b4315306SDan Handley {
101b4315306SDan Handley arm_bl1_early_platform_setup();
10236fbcf4dSAhmed Azeem #if !HW_ASSISTED_COHERENCY
103b4315306SDan Handley /*
1046355f234SVikram Kanigiri * Initialize Interconnect for this cluster during cold boot.
105b4315306SDan Handley * No need for locks as no other CPU is active.
106b4315306SDan Handley */
1076355f234SVikram Kanigiri plat_arm_interconnect_init();
108b4315306SDan Handley /*
1096355f234SVikram Kanigiri * Enable Interconnect coherency for the primary CPU's cluster.
110b4315306SDan Handley */
1116355f234SVikram Kanigiri plat_arm_interconnect_enter_coherency();
11236fbcf4dSAhmed Azeem #endif
113b4315306SDan Handley }
114b4315306SDan Handley
115b4315306SDan Handley /******************************************************************************
116b4315306SDan Handley * Perform the very early platform specific architecture setup shared between
117b4315306SDan Handley * ARM standard platforms. This only does basic initialization. Later
118b4315306SDan Handley * architectural setup (bl1_arch_setup()) does not do anything platform
119b4315306SDan Handley * specific.
120b4315306SDan Handley *****************************************************************************/
arm_bl1_plat_arch_setup(void)121b4315306SDan Handley void arm_bl1_plat_arch_setup(void)
122b4315306SDan Handley {
123b65dfe40SSandrine Bailleux #if USE_COHERENT_MEM
124b65dfe40SSandrine Bailleux /* Ensure ARM platforms don't use coherent memory in BL1. */
125d323af9eSDaniel Boulby assert((BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE) == 0U);
126b4315306SDan Handley #endif
127d323af9eSDaniel Boulby
128d323af9eSDaniel Boulby const mmap_region_t bl_regions[] = {
129d323af9eSDaniel Boulby MAP_BL1_TOTAL,
1302ecaafd2SDaniel Boulby MAP_BL1_RO,
1311eb735d7SRoberto Vargas #if USE_ROMLIB
1321eb735d7SRoberto Vargas ARM_MAP_ROMLIB_CODE,
1331eb735d7SRoberto Vargas ARM_MAP_ROMLIB_DATA,
1341eb735d7SRoberto Vargas #endif
135d323af9eSDaniel Boulby {0}
136d323af9eSDaniel Boulby };
137d323af9eSDaniel Boulby
1380916c38dSRoberto Vargas setup_page_tables(bl_regions, plat_arm_get_mmap());
139402b3cf8SJulius Werner #ifdef __aarch64__
140b5fa6563SSandrine Bailleux enable_mmu_el3(0);
141402b3cf8SJulius Werner #else
142402b3cf8SJulius Werner enable_mmu_svc_mon(0);
143402b3cf8SJulius Werner #endif /* __aarch64__ */
1441eb735d7SRoberto Vargas
1451eb735d7SRoberto Vargas arm_setup_romlib();
146b4315306SDan Handley }
147b4315306SDan Handley
bl1_plat_arch_setup(void)148b4315306SDan Handley void bl1_plat_arch_setup(void)
149b4315306SDan Handley {
150b4315306SDan Handley arm_bl1_plat_arch_setup();
151b4315306SDan Handley }
152b4315306SDan Handley
153b4315306SDan Handley /*
154b4315306SDan Handley * Perform the platform specific architecture setup shared between
155b4315306SDan Handley * ARM standard platforms.
156b4315306SDan Handley */
arm_bl1_platform_setup(void)157b4315306SDan Handley void arm_bl1_platform_setup(void)
158b4315306SDan Handley {
1599c11ed7eSHarrison Mutai const struct dyn_cfg_dtb_info_t *config_info __unused;
1609c11ed7eSHarrison Mutai uint32_t fw_config_max_size __unused;
1619c11ed7eSHarrison Mutai image_info_t config_image_info __unused;
1629c11ed7eSHarrison Mutai struct transfer_list_entry *te __unused;
1639c11ed7eSHarrison Mutai
16482869675SManish V Badarkhe image_desc_t *desc;
1659c11ed7eSHarrison Mutai
166ada4e59dSHarrison Mutai int err __unused = 1;
16782869675SManish V Badarkhe
168b4315306SDan Handley /* Initialise the IO layer and register platform IO devices */
169b4315306SDan Handley plat_arm_io_setup();
1703b5ea741SLouis Mayencourt
17182869675SManish V Badarkhe /* Check if we need FWU before further processing */
172a249a9d9SManish V Badarkhe is_fwu_needed = plat_arm_bl1_fwu_needed();
173a249a9d9SManish V Badarkhe if (is_fwu_needed) {
17482869675SManish V Badarkhe ERROR("Skip platform setup as FWU detected\n");
17582869675SManish V Badarkhe return;
17682869675SManish V Badarkhe }
17782869675SManish V Badarkhe
1789c11ed7eSHarrison Mutai #if TRANSFER_LIST
179ada4e59dSHarrison Mutai #if CRYPTO_SUPPORT
180ada4e59dSHarrison Mutai te = transfer_list_add(secure_tl, TL_TAG_MBEDTLS_HEAP_INFO,
181ada4e59dSHarrison Mutai sizeof(struct crypto_heap_info), NULL);
1829c11ed7eSHarrison Mutai assert(te != NULL);
1839c11ed7eSHarrison Mutai
184ada4e59dSHarrison Mutai struct crypto_heap_info *heap_info =
185ada4e59dSHarrison Mutai (struct crypto_heap_info *)transfer_list_entry_data(te);
186ada4e59dSHarrison Mutai arm_get_mbedtls_heap(&heap_info->addr, &heap_info->size);
187ada4e59dSHarrison Mutai #endif /* CRYPTO_SUPPORT */
188ada4e59dSHarrison Mutai
189ada4e59dSHarrison Mutai desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
190ada4e59dSHarrison Mutai
1919c11ed7eSHarrison Mutai /*
192ada4e59dSHarrison Mutai * The event log might have been updated prior to this, make sure we have an
193ada4e59dSHarrison Mutai * up to date tl before setting the handoff arguments.
1949c11ed7eSHarrison Mutai */
1959c11ed7eSHarrison Mutai transfer_list_update_checksum(secure_tl);
196ada4e59dSHarrison Mutai transfer_list_set_handoff_args(secure_tl, &desc->ep_info);
1979c11ed7eSHarrison Mutai #else
19882869675SManish V Badarkhe /* Set global DTB info for fixed fw_config information */
19982869675SManish V Badarkhe fw_config_max_size = ARM_FW_CONFIG_LIMIT - ARM_FW_CONFIG_BASE;
200046cb19bSManish V Badarkhe set_config_info(ARM_FW_CONFIG_BASE, ~0UL, fw_config_max_size, FW_CONFIG_ID);
20182869675SManish V Badarkhe
20282869675SManish V Badarkhe /* Fill the device tree information struct with the info from the config dtb */
20382869675SManish V Badarkhe err = fconf_load_config(FW_CONFIG_ID);
20482869675SManish V Badarkhe if (err < 0) {
20582869675SManish V Badarkhe ERROR("Loading of FW_CONFIG failed %d\n", err);
20682869675SManish V Badarkhe plat_error_handler(err);
20782869675SManish V Badarkhe }
20882869675SManish V Badarkhe
20982869675SManish V Badarkhe /*
21082869675SManish V Badarkhe * FW_CONFIG loaded successfully. If FW_CONFIG device tree parsing
21182869675SManish V Badarkhe * is successful then load TB_FW_CONFIG device tree.
21282869675SManish V Badarkhe */
2139c11ed7eSHarrison Mutai config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, FW_CONFIG_ID);
2149c11ed7eSHarrison Mutai if (config_info != NULL) {
2159c11ed7eSHarrison Mutai err = fconf_populate_dtb_registry(config_info->config_addr);
21682869675SManish V Badarkhe if (err < 0) {
21782869675SManish V Badarkhe ERROR("Parsing of FW_CONFIG failed %d\n", err);
21882869675SManish V Badarkhe plat_error_handler(err);
21982869675SManish V Badarkhe }
2209c11ed7eSHarrison Mutai
22182869675SManish V Badarkhe /* load TB_FW_CONFIG */
22282869675SManish V Badarkhe err = fconf_load_config(TB_FW_CONFIG_ID);
22382869675SManish V Badarkhe if (err < 0) {
22482869675SManish V Badarkhe ERROR("Loading of TB_FW_CONFIG failed %d\n", err);
22582869675SManish V Badarkhe plat_error_handler(err);
22682869675SManish V Badarkhe }
22782869675SManish V Badarkhe } else {
22882869675SManish V Badarkhe ERROR("Invalid FW_CONFIG address\n");
22982869675SManish V Badarkhe plat_error_handler(err);
23082869675SManish V Badarkhe }
23182869675SManish V Badarkhe
23282869675SManish V Badarkhe desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
2339c11ed7eSHarrison Mutai
2349c11ed7eSHarrison Mutai /* The BL2 ep_info arg0 is modified to point to FW_CONFIG */
23582869675SManish V Badarkhe assert(desc != NULL);
2369c11ed7eSHarrison Mutai desc->ep_info.args.arg0 = config_info->config_addr;
2373b5ea741SLouis Mayencourt
23888c51c3fSManish V Badarkhe #if CRYPTO_SUPPORT
239ba597da7SJohn Tsichritzis /* Share the Mbed TLS heap info with other images */
240ba597da7SJohn Tsichritzis arm_bl1_set_mbedtls_heap();
24188c51c3fSManish V Badarkhe #endif /* CRYPTO_SUPPORT */
242ada4e59dSHarrison Mutai #endif /* TRANSFER_LIST */
24360e19f57SAntonio Nino Diaz
2443208edcdSSoby Mathew /*
2453208edcdSSoby Mathew * Allow access to the System counter timer module and program
2463208edcdSSoby Mathew * counter frequency for non secure images during FWU
2473208edcdSSoby Mathew */
2486393c787SUsama Arif #ifdef ARM_SYS_TIMCTL_BASE
2493208edcdSSoby Mathew arm_configure_sys_timer();
2506393c787SUsama Arif #endif
2518f73663bSUsama Arif #if (ARM_ARCH_MAJOR > 7) || defined(ARMV7_SUPPORTS_GENERIC_TIMER)
2523208edcdSSoby Mathew write_cntfrq_el0(plat_get_syscnt_freq2());
2538f73663bSUsama Arif #endif
254b4315306SDan Handley }
255b4315306SDan Handley
bl1_plat_prepare_exit(entry_point_info_t * ep_info)2564c117f6cSSandrine Bailleux void bl1_plat_prepare_exit(entry_point_info_t *ep_info)
2574c117f6cSSandrine Bailleux {
2587b4c1405SJuan Castillo #if !ARM_DISABLE_TRUSTED_WDOG
2597b4c1405SJuan Castillo /* Disable watchdog before leaving BL1 */
260b0c97dafSAditya Angadi plat_arm_secure_wdt_stop();
2617b4c1405SJuan Castillo #endif
2627b4c1405SJuan Castillo
2634c117f6cSSandrine Bailleux #ifdef EL3_PAYLOAD_BASE
2644c117f6cSSandrine Bailleux /*
2654c117f6cSSandrine Bailleux * Program the EL3 payload's entry point address into the CPUs mailbox
2664c117f6cSSandrine Bailleux * in order to release secondary CPUs from their holding pen and make
2674c117f6cSSandrine Bailleux * them jump there.
2684c117f6cSSandrine Bailleux */
2692a246d2eSDimitris Papastamos plat_arm_program_trusted_mailbox(ep_info->pc);
2704c117f6cSSandrine Bailleux dsbsy();
2714c117f6cSSandrine Bailleux sev();
2724c117f6cSSandrine Bailleux #endif
2734c117f6cSSandrine Bailleux }
2747b56928aSSoby Mathew
2754da6f6cdSSathees Balya /*
2764da6f6cdSSathees Balya * On Arm platforms, the FWU process is triggered when the FIP image has
2774da6f6cdSSathees Balya * been tampered with.
2784da6f6cdSSathees Balya */
plat_arm_bl1_fwu_needed(void)279d6dcbcadSLouis Mayencourt bool plat_arm_bl1_fwu_needed(void)
2804da6f6cdSSathees Balya {
281d6dcbcadSLouis Mayencourt return !arm_io_is_toc_valid();
2824da6f6cdSSathees Balya }
2834da6f6cdSSathees Balya
2847b56928aSSoby Mathew /*******************************************************************************
2857b56928aSSoby Mathew * The following function checks if Firmware update is needed,
2867b56928aSSoby Mathew * by checking if TOC in FIP image is valid or not.
2877b56928aSSoby Mathew ******************************************************************************/
bl1_plat_get_next_image_id(void)2887b56928aSSoby Mathew unsigned int bl1_plat_get_next_image_id(void)
2897b56928aSSoby Mathew {
290a249a9d9SManish V Badarkhe return is_fwu_needed ? NS_BL1U_IMAGE_ID : BL2_IMAGE_ID;
2917b56928aSSoby Mathew }
2929c11ed7eSHarrison Mutai
2939c11ed7eSHarrison Mutai #if TRANSFER_LIST
bl1_plat_handle_post_image_load(unsigned int image_id)2949c11ed7eSHarrison Mutai int bl1_plat_handle_post_image_load(unsigned int image_id)
2959c11ed7eSHarrison Mutai {
2969c11ed7eSHarrison Mutai struct transfer_list_entry *te;
2979c11ed7eSHarrison Mutai
298abdb953bSHarrison Mutai if (image_id != BL2_IMAGE_ID) {
299abdb953bSHarrison Mutai return 0;
300abdb953bSHarrison Mutai }
301abdb953bSHarrison Mutai
3029c11ed7eSHarrison Mutai /* Convey this information to BL2 via its TL. */
303abdb953bSHarrison Mutai te = transfer_list_add(secure_tl, TL_TAG_SRAM_LAYOUT,
3049c11ed7eSHarrison Mutai sizeof(meminfo_t), NULL);
3059c11ed7eSHarrison Mutai assert(te != NULL);
3069c11ed7eSHarrison Mutai
3079c11ed7eSHarrison Mutai bl1_plat_calc_bl2_layout(&bl1_tzram_layout,
3089c11ed7eSHarrison Mutai (meminfo_t *)transfer_list_entry_data(te));
3099c11ed7eSHarrison Mutai
3109c11ed7eSHarrison Mutai transfer_list_update_checksum(secure_tl);
3119c11ed7eSHarrison Mutai
3129c11ed7eSHarrison Mutai /**
3139c11ed7eSHarrison Mutai * Before exiting make sure the contents of the TL are flushed in case there's no
3149c11ed7eSHarrison Mutai * support for hardware cache coherency.
3159c11ed7eSHarrison Mutai */
3169c11ed7eSHarrison Mutai flush_dcache_range((uintptr_t)secure_tl, secure_tl->size);
3179c11ed7eSHarrison Mutai return 0;
3189c11ed7eSHarrison Mutai }
3199c11ed7eSHarrison Mutai #endif /* TRANSFER_LIST*/
320*75b5be98SXialin Liu
321*75b5be98SXialin Liu /* For ARM platform, the NV ctr is shared among all components */
bl1_plat_is_shared_nv_ctr(void)322*75b5be98SXialin Liu bool bl1_plat_is_shared_nv_ctr(void)
323*75b5be98SXialin Liu {
324*75b5be98SXialin Liu return true;
325*75b5be98SXialin Liu }
326