1b4315306SDan Handley /* 29c11ed7eSHarrison Mutai * 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 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 189c11ed7eSHarrison Mutai #include <lib/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 67*d5705719SHarrison Mutai struct transfer_list_header *secure_tl; 689c11ed7eSHarrison Mutai 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 ******************************************************************************/ 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; 91*d5705719SHarrison Mutai 92*d5705719SHarrison Mutai #if TRANSFER_LIST 93*d5705719SHarrison Mutai secure_tl = transfer_list_ensure((void *)PLAT_ARM_EL3_FW_HANDOFF_BASE, 94*d5705719SHarrison Mutai PLAT_ARM_FW_HANDOFF_SIZE); 95*d5705719SHarrison Mutai assert(secure_tl != NULL); 96*d5705719SHarrison Mutai #endif 97b4315306SDan Handley } 98b4315306SDan Handley 99b4315306SDan Handley void bl1_early_platform_setup(void) 100b4315306SDan Handley { 101b4315306SDan Handley arm_bl1_early_platform_setup(); 102b4315306SDan Handley 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(); 112b4315306SDan Handley } 113b4315306SDan Handley 114b4315306SDan Handley /****************************************************************************** 115b4315306SDan Handley * Perform the very early platform specific architecture setup shared between 116b4315306SDan Handley * ARM standard platforms. This only does basic initialization. Later 117b4315306SDan Handley * architectural setup (bl1_arch_setup()) does not do anything platform 118b4315306SDan Handley * specific. 119b4315306SDan Handley *****************************************************************************/ 120b4315306SDan Handley void arm_bl1_plat_arch_setup(void) 121b4315306SDan Handley { 122b65dfe40SSandrine Bailleux #if USE_COHERENT_MEM 123b65dfe40SSandrine Bailleux /* Ensure ARM platforms don't use coherent memory in BL1. */ 124d323af9eSDaniel Boulby assert((BL_COHERENT_RAM_END - BL_COHERENT_RAM_BASE) == 0U); 125b4315306SDan Handley #endif 126d323af9eSDaniel Boulby 127d323af9eSDaniel Boulby const mmap_region_t bl_regions[] = { 128d323af9eSDaniel Boulby MAP_BL1_TOTAL, 1292ecaafd2SDaniel Boulby MAP_BL1_RO, 1301eb735d7SRoberto Vargas #if USE_ROMLIB 1311eb735d7SRoberto Vargas ARM_MAP_ROMLIB_CODE, 1321eb735d7SRoberto Vargas ARM_MAP_ROMLIB_DATA, 1331eb735d7SRoberto Vargas #endif 134d323af9eSDaniel Boulby {0} 135d323af9eSDaniel Boulby }; 136d323af9eSDaniel Boulby 1370916c38dSRoberto Vargas setup_page_tables(bl_regions, plat_arm_get_mmap()); 138402b3cf8SJulius Werner #ifdef __aarch64__ 139b5fa6563SSandrine Bailleux enable_mmu_el3(0); 140402b3cf8SJulius Werner #else 141402b3cf8SJulius Werner enable_mmu_svc_mon(0); 142402b3cf8SJulius Werner #endif /* __aarch64__ */ 1431eb735d7SRoberto Vargas 1441eb735d7SRoberto Vargas arm_setup_romlib(); 145b4315306SDan Handley } 146b4315306SDan Handley 147b4315306SDan Handley void bl1_plat_arch_setup(void) 148b4315306SDan Handley { 149b4315306SDan Handley arm_bl1_plat_arch_setup(); 150b4315306SDan Handley } 151b4315306SDan Handley 152b4315306SDan Handley /* 153b4315306SDan Handley * Perform the platform specific architecture setup shared between 154b4315306SDan Handley * ARM standard platforms. 155b4315306SDan Handley */ 156b4315306SDan Handley void arm_bl1_platform_setup(void) 157b4315306SDan Handley { 1589c11ed7eSHarrison Mutai const struct dyn_cfg_dtb_info_t *config_info __unused; 1599c11ed7eSHarrison Mutai uint32_t fw_config_max_size __unused; 1609c11ed7eSHarrison Mutai image_info_t config_image_info __unused; 1619c11ed7eSHarrison Mutai struct transfer_list_entry *te __unused; 1629c11ed7eSHarrison Mutai 16382869675SManish V Badarkhe image_desc_t *desc; 1649c11ed7eSHarrison Mutai 16582869675SManish V Badarkhe int err = -1; 16682869675SManish V Badarkhe 167b4315306SDan Handley /* Initialise the IO layer and register platform IO devices */ 168b4315306SDan Handley plat_arm_io_setup(); 1693b5ea741SLouis Mayencourt 17082869675SManish V Badarkhe /* Check if we need FWU before further processing */ 171a249a9d9SManish V Badarkhe is_fwu_needed = plat_arm_bl1_fwu_needed(); 172a249a9d9SManish V Badarkhe if (is_fwu_needed) { 17382869675SManish V Badarkhe ERROR("Skip platform setup as FWU detected\n"); 17482869675SManish V Badarkhe return; 17582869675SManish V Badarkhe } 17682869675SManish V Badarkhe 1779c11ed7eSHarrison Mutai #if TRANSFER_LIST 1789c11ed7eSHarrison Mutai te = transfer_list_add(secure_tl, TL_TAG_TB_FW_CONFIG, 1799c11ed7eSHarrison Mutai ARM_TB_FW_CONFIG_MAX_SIZE, NULL); 1809c11ed7eSHarrison Mutai assert(te != NULL); 1819c11ed7eSHarrison Mutai 1829c11ed7eSHarrison Mutai /* 1839c11ed7eSHarrison Mutai * Set the load address of TB_FW_CONFIG in the data section of the TE just 1849c11ed7eSHarrison Mutai * allocated in the secure transfer list. 1859c11ed7eSHarrison Mutai */ 1869c11ed7eSHarrison Mutai SET_PARAM_HEAD(&config_image_info, PARAM_IMAGE_BINARY, VERSION_2, 0); 1879c11ed7eSHarrison Mutai config_image_info.image_base = (uintptr_t)transfer_list_entry_data(te); 1889c11ed7eSHarrison Mutai config_image_info.image_max_size = te->data_size; 1899c11ed7eSHarrison Mutai 1909c11ed7eSHarrison Mutai VERBOSE("FCONF: Loading config with image ID: %u\n", TB_FW_CONFIG_ID); 1919c11ed7eSHarrison Mutai err = load_auth_image(TB_FW_CONFIG_ID, &config_image_info); 1929c11ed7eSHarrison Mutai if (err != 0) { 1939c11ed7eSHarrison Mutai VERBOSE("Failed to load config %u\n", TB_FW_CONFIG_ID); 1949c11ed7eSHarrison Mutai plat_error_handler(err); 1959c11ed7eSHarrison Mutai } 1969c11ed7eSHarrison Mutai 1979c11ed7eSHarrison Mutai transfer_list_update_checksum(secure_tl); 1989c11ed7eSHarrison Mutai fconf_populate("TB_FW", (uintptr_t)transfer_list_entry_data(te)); 1999c11ed7eSHarrison Mutai #else 20082869675SManish V Badarkhe /* Set global DTB info for fixed fw_config information */ 20182869675SManish V Badarkhe fw_config_max_size = ARM_FW_CONFIG_LIMIT - ARM_FW_CONFIG_BASE; 202046cb19bSManish V Badarkhe set_config_info(ARM_FW_CONFIG_BASE, ~0UL, fw_config_max_size, FW_CONFIG_ID); 20382869675SManish V Badarkhe 20482869675SManish V Badarkhe /* Fill the device tree information struct with the info from the config dtb */ 20582869675SManish V Badarkhe err = fconf_load_config(FW_CONFIG_ID); 20682869675SManish V Badarkhe if (err < 0) { 20782869675SManish V Badarkhe ERROR("Loading of FW_CONFIG failed %d\n", err); 20882869675SManish V Badarkhe plat_error_handler(err); 20982869675SManish V Badarkhe } 21082869675SManish V Badarkhe 21182869675SManish V Badarkhe /* 21282869675SManish V Badarkhe * FW_CONFIG loaded successfully. If FW_CONFIG device tree parsing 21382869675SManish V Badarkhe * is successful then load TB_FW_CONFIG device tree. 21482869675SManish V Badarkhe */ 2159c11ed7eSHarrison Mutai config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, FW_CONFIG_ID); 2169c11ed7eSHarrison Mutai if (config_info != NULL) { 2179c11ed7eSHarrison Mutai err = fconf_populate_dtb_registry(config_info->config_addr); 21882869675SManish V Badarkhe if (err < 0) { 21982869675SManish V Badarkhe ERROR("Parsing of FW_CONFIG failed %d\n", err); 22082869675SManish V Badarkhe plat_error_handler(err); 22182869675SManish V Badarkhe } 2229c11ed7eSHarrison Mutai 22382869675SManish V Badarkhe /* load TB_FW_CONFIG */ 22482869675SManish V Badarkhe err = fconf_load_config(TB_FW_CONFIG_ID); 22582869675SManish V Badarkhe if (err < 0) { 22682869675SManish V Badarkhe ERROR("Loading of TB_FW_CONFIG failed %d\n", err); 22782869675SManish V Badarkhe plat_error_handler(err); 22882869675SManish V Badarkhe } 22982869675SManish V Badarkhe } else { 23082869675SManish V Badarkhe ERROR("Invalid FW_CONFIG address\n"); 23182869675SManish V Badarkhe plat_error_handler(err); 23282869675SManish V Badarkhe } 2339c11ed7eSHarrison Mutai #endif /* TRANSFER_LIST */ 23482869675SManish V Badarkhe 23582869675SManish V Badarkhe desc = bl1_plat_get_image_desc(BL2_IMAGE_ID); 2369c11ed7eSHarrison Mutai 2379c11ed7eSHarrison Mutai #if TRANSFER_LIST 2389c11ed7eSHarrison Mutai transfer_list_set_handoff_args(secure_tl, &desc->ep_info); 2399c11ed7eSHarrison Mutai #else 2409c11ed7eSHarrison Mutai /* The BL2 ep_info arg0 is modified to point to FW_CONFIG */ 24182869675SManish V Badarkhe assert(desc != NULL); 2429c11ed7eSHarrison Mutai desc->ep_info.args.arg0 = config_info->config_addr; 2439c11ed7eSHarrison Mutai #endif /* TRANSFER_LIST */ 2443b5ea741SLouis Mayencourt 24588c51c3fSManish V Badarkhe #if CRYPTO_SUPPORT 246ba597da7SJohn Tsichritzis /* Share the Mbed TLS heap info with other images */ 247ba597da7SJohn Tsichritzis arm_bl1_set_mbedtls_heap(); 24888c51c3fSManish V Badarkhe #endif /* CRYPTO_SUPPORT */ 24960e19f57SAntonio Nino Diaz 2503208edcdSSoby Mathew /* 2513208edcdSSoby Mathew * Allow access to the System counter timer module and program 2523208edcdSSoby Mathew * counter frequency for non secure images during FWU 2533208edcdSSoby Mathew */ 2546393c787SUsama Arif #ifdef ARM_SYS_TIMCTL_BASE 2553208edcdSSoby Mathew arm_configure_sys_timer(); 2566393c787SUsama Arif #endif 2578f73663bSUsama Arif #if (ARM_ARCH_MAJOR > 7) || defined(ARMV7_SUPPORTS_GENERIC_TIMER) 2583208edcdSSoby Mathew write_cntfrq_el0(plat_get_syscnt_freq2()); 2598f73663bSUsama Arif #endif 260b4315306SDan Handley } 261b4315306SDan Handley 2624c117f6cSSandrine Bailleux void bl1_plat_prepare_exit(entry_point_info_t *ep_info) 2634c117f6cSSandrine Bailleux { 2647b4c1405SJuan Castillo #if !ARM_DISABLE_TRUSTED_WDOG 2657b4c1405SJuan Castillo /* Disable watchdog before leaving BL1 */ 266b0c97dafSAditya Angadi plat_arm_secure_wdt_stop(); 2677b4c1405SJuan Castillo #endif 2687b4c1405SJuan Castillo 2694c117f6cSSandrine Bailleux #ifdef EL3_PAYLOAD_BASE 2704c117f6cSSandrine Bailleux /* 2714c117f6cSSandrine Bailleux * Program the EL3 payload's entry point address into the CPUs mailbox 2724c117f6cSSandrine Bailleux * in order to release secondary CPUs from their holding pen and make 2734c117f6cSSandrine Bailleux * them jump there. 2744c117f6cSSandrine Bailleux */ 2752a246d2eSDimitris Papastamos plat_arm_program_trusted_mailbox(ep_info->pc); 2764c117f6cSSandrine Bailleux dsbsy(); 2774c117f6cSSandrine Bailleux sev(); 2784c117f6cSSandrine Bailleux #endif 2794c117f6cSSandrine Bailleux } 2807b56928aSSoby Mathew 2814da6f6cdSSathees Balya /* 2824da6f6cdSSathees Balya * On Arm platforms, the FWU process is triggered when the FIP image has 2834da6f6cdSSathees Balya * been tampered with. 2844da6f6cdSSathees Balya */ 285d6dcbcadSLouis Mayencourt bool plat_arm_bl1_fwu_needed(void) 2864da6f6cdSSathees Balya { 287d6dcbcadSLouis Mayencourt return !arm_io_is_toc_valid(); 2884da6f6cdSSathees Balya } 2894da6f6cdSSathees Balya 2907b56928aSSoby Mathew /******************************************************************************* 2917b56928aSSoby Mathew * The following function checks if Firmware update is needed, 2927b56928aSSoby Mathew * by checking if TOC in FIP image is valid or not. 2937b56928aSSoby Mathew ******************************************************************************/ 2947b56928aSSoby Mathew unsigned int bl1_plat_get_next_image_id(void) 2957b56928aSSoby Mathew { 296a249a9d9SManish V Badarkhe return is_fwu_needed ? NS_BL1U_IMAGE_ID : BL2_IMAGE_ID; 2977b56928aSSoby Mathew } 2989c11ed7eSHarrison Mutai 2999c11ed7eSHarrison Mutai // Use the default implementation of this function when Firmware Handoff is 3009c11ed7eSHarrison Mutai // disabled to avoid duplicating its logic. 3019c11ed7eSHarrison Mutai #if TRANSFER_LIST 3029c11ed7eSHarrison Mutai int bl1_plat_handle_post_image_load(unsigned int image_id) 3039c11ed7eSHarrison Mutai { 3049c11ed7eSHarrison Mutai image_desc_t *image_desc __unused; 3059c11ed7eSHarrison Mutai 3069c11ed7eSHarrison Mutai assert(image_id == BL2_IMAGE_ID); 3079c11ed7eSHarrison Mutai struct transfer_list_entry *te; 3089c11ed7eSHarrison Mutai 3099c11ed7eSHarrison Mutai /* Convey this information to BL2 via its TL. */ 3109c11ed7eSHarrison Mutai te = transfer_list_add(secure_tl, TL_TAG_SRAM_LAYOUT64, 3119c11ed7eSHarrison Mutai sizeof(meminfo_t), NULL); 3129c11ed7eSHarrison Mutai assert(te != NULL); 3139c11ed7eSHarrison Mutai 3149c11ed7eSHarrison Mutai bl1_plat_calc_bl2_layout(&bl1_tzram_layout, 3159c11ed7eSHarrison Mutai (meminfo_t *)transfer_list_entry_data(te)); 3169c11ed7eSHarrison Mutai 3179c11ed7eSHarrison Mutai transfer_list_update_checksum(secure_tl); 3189c11ed7eSHarrison Mutai 3199c11ed7eSHarrison Mutai /** 3209c11ed7eSHarrison Mutai * Before exiting make sure the contents of the TL are flushed in case there's no 3219c11ed7eSHarrison Mutai * support for hardware cache coherency. 3229c11ed7eSHarrison Mutai */ 3239c11ed7eSHarrison Mutai flush_dcache_range((uintptr_t)secure_tl, secure_tl->size); 3249c11ed7eSHarrison Mutai return 0; 3259c11ed7eSHarrison Mutai } 3269c11ed7eSHarrison Mutai #endif /* TRANSFER_LIST*/ 327