1*91f16700Schasinglulu /* 2*91f16700Schasinglulu * Copyright (c) 2021, ARM Limited and Contributors. All rights reserved. 3*91f16700Schasinglulu * 4*91f16700Schasinglulu * SPDX-License-Identifier: BSD-3-Clause 5*91f16700Schasinglulu */ 6*91f16700Schasinglulu 7*91f16700Schasinglulu /* Use the xlat_tables_v2 data structures: */ 8*91f16700Schasinglulu #define XLAT_TABLES_LIB_V2 1 9*91f16700Schasinglulu 10*91f16700Schasinglulu #include <assert.h> 11*91f16700Schasinglulu 12*91f16700Schasinglulu #include <bl1/bl1.h> 13*91f16700Schasinglulu #include <common/tbbr/tbbr_img_def.h> 14*91f16700Schasinglulu #include <drivers/arm/sp805.h> 15*91f16700Schasinglulu #include <lib/fconf/fconf.h> 16*91f16700Schasinglulu #include <lib/fconf/fconf_dyn_cfg_getter.h> 17*91f16700Schasinglulu #include <lib/xlat_mpu/xlat_mpu.h> 18*91f16700Schasinglulu 19*91f16700Schasinglulu #include "fvp_r_private.h" 20*91f16700Schasinglulu #include <plat/arm/common/arm_config.h> 21*91f16700Schasinglulu #include <plat/arm/common/arm_def.h> 22*91f16700Schasinglulu #include <plat/arm/common/plat_arm.h> 23*91f16700Schasinglulu #include <plat/common/platform.h> 24*91f16700Schasinglulu #include <platform_def.h> 25*91f16700Schasinglulu 26*91f16700Schasinglulu #define MAP_BL1_TOTAL MAP_REGION_FLAT( \ 27*91f16700Schasinglulu bl1_tzram_layout.total_base, \ 28*91f16700Schasinglulu bl1_tzram_layout.total_size, \ 29*91f16700Schasinglulu MT_MEMORY | MT_RW | MT_SECURE) 30*91f16700Schasinglulu /* 31*91f16700Schasinglulu * If SEPARATE_CODE_AND_RODATA=1 we define a region for each section 32*91f16700Schasinglulu * otherwise one region is defined containing both 33*91f16700Schasinglulu */ 34*91f16700Schasinglulu #if SEPARATE_CODE_AND_RODATA 35*91f16700Schasinglulu #define MAP_BL1_RO MAP_REGION_FLAT( \ 36*91f16700Schasinglulu BL_CODE_BASE, \ 37*91f16700Schasinglulu BL1_CODE_END - BL_CODE_BASE, \ 38*91f16700Schasinglulu MT_CODE | MT_SECURE), \ 39*91f16700Schasinglulu MAP_REGION_FLAT( \ 40*91f16700Schasinglulu BL1_RO_DATA_BASE, \ 41*91f16700Schasinglulu BL1_RO_DATA_END \ 42*91f16700Schasinglulu - BL_RO_DATA_BASE, \ 43*91f16700Schasinglulu MT_RO_DATA | MT_SECURE) 44*91f16700Schasinglulu #else 45*91f16700Schasinglulu #define MAP_BL1_RO MAP_REGION_FLAT( \ 46*91f16700Schasinglulu BL_CODE_BASE, \ 47*91f16700Schasinglulu BL1_CODE_END - BL_CODE_BASE, \ 48*91f16700Schasinglulu MT_CODE | MT_SECURE) 49*91f16700Schasinglulu #endif 50*91f16700Schasinglulu 51*91f16700Schasinglulu /* Data structure which holds the extents of the trusted SRAM for BL1*/ 52*91f16700Schasinglulu static meminfo_t bl1_tzram_layout; 53*91f16700Schasinglulu 54*91f16700Schasinglulu struct meminfo *bl1_plat_sec_mem_layout(void) 55*91f16700Schasinglulu { 56*91f16700Schasinglulu return &bl1_tzram_layout; 57*91f16700Schasinglulu } 58*91f16700Schasinglulu 59*91f16700Schasinglulu void arm_bl1_early_platform_setup(void) 60*91f16700Schasinglulu { 61*91f16700Schasinglulu 62*91f16700Schasinglulu #if !ARM_DISABLE_TRUSTED_WDOG 63*91f16700Schasinglulu /* Enable watchdog */ 64*91f16700Schasinglulu plat_arm_secure_wdt_start(); 65*91f16700Schasinglulu #endif 66*91f16700Schasinglulu 67*91f16700Schasinglulu /* Initialize the console to provide early debug support */ 68*91f16700Schasinglulu arm_console_boot_init(); 69*91f16700Schasinglulu 70*91f16700Schasinglulu /* Allow BL1 to see the whole Trusted RAM */ 71*91f16700Schasinglulu bl1_tzram_layout.total_base = ARM_BL_RAM_BASE; 72*91f16700Schasinglulu bl1_tzram_layout.total_size = ARM_BL_RAM_SIZE; 73*91f16700Schasinglulu } 74*91f16700Schasinglulu 75*91f16700Schasinglulu /* Boolean variable to hold condition whether firmware update needed or not */ 76*91f16700Schasinglulu static bool is_fwu_needed; 77*91f16700Schasinglulu 78*91f16700Schasinglulu /******************************************************************************* 79*91f16700Schasinglulu * Perform any BL1 specific platform actions. 80*91f16700Schasinglulu ******************************************************************************/ 81*91f16700Schasinglulu void bl1_early_platform_setup(void) 82*91f16700Schasinglulu { 83*91f16700Schasinglulu arm_bl1_early_platform_setup(); 84*91f16700Schasinglulu 85*91f16700Schasinglulu /* Initialize the platform config for future decision making */ 86*91f16700Schasinglulu fvp_config_setup(); 87*91f16700Schasinglulu 88*91f16700Schasinglulu /* 89*91f16700Schasinglulu * Initialize Interconnect for this cluster during cold boot. 90*91f16700Schasinglulu * No need for locks as no other CPU is active. 91*91f16700Schasinglulu */ 92*91f16700Schasinglulu fvp_interconnect_init(); 93*91f16700Schasinglulu /* 94*91f16700Schasinglulu * Enable coherency in Interconnect for the primary CPU's cluster. 95*91f16700Schasinglulu */ 96*91f16700Schasinglulu fvp_interconnect_enable(); 97*91f16700Schasinglulu } 98*91f16700Schasinglulu 99*91f16700Schasinglulu void arm_bl1_plat_arch_setup(void) 100*91f16700Schasinglulu { 101*91f16700Schasinglulu const mmap_region_t bl_regions[] = { 102*91f16700Schasinglulu MAP_BL1_TOTAL, 103*91f16700Schasinglulu MAP_BL1_RO, 104*91f16700Schasinglulu #if USE_ROMLIB 105*91f16700Schasinglulu ARM_MAP_ROMLIB_CODE, 106*91f16700Schasinglulu ARM_MAP_ROMLIB_DATA, 107*91f16700Schasinglulu #endif 108*91f16700Schasinglulu /* DRAM1_region: */ 109*91f16700Schasinglulu MAP_REGION_FLAT( 110*91f16700Schasinglulu PLAT_ARM_DRAM1_BASE, 111*91f16700Schasinglulu PLAT_ARM_DRAM1_SIZE, 112*91f16700Schasinglulu MT_MEMORY | MT_SECURE | MT_EXECUTE 113*91f16700Schasinglulu | MT_RW | MT_NON_CACHEABLE), 114*91f16700Schasinglulu /* NULL terminator: */ 115*91f16700Schasinglulu {0} 116*91f16700Schasinglulu }; 117*91f16700Schasinglulu 118*91f16700Schasinglulu setup_page_tables(bl_regions, plat_arm_get_mmap()); 119*91f16700Schasinglulu enable_mpu_el2(0); 120*91f16700Schasinglulu 121*91f16700Schasinglulu arm_setup_romlib(); 122*91f16700Schasinglulu } 123*91f16700Schasinglulu 124*91f16700Schasinglulu void plat_arm_secure_wdt_start(void) 125*91f16700Schasinglulu { 126*91f16700Schasinglulu sp805_start(ARM_SP805_TWDG_BASE, ARM_TWDG_LOAD_VAL); 127*91f16700Schasinglulu } 128*91f16700Schasinglulu 129*91f16700Schasinglulu void plat_arm_secure_wdt_stop(void) 130*91f16700Schasinglulu { 131*91f16700Schasinglulu sp805_stop(ARM_SP805_TWDG_BASE); 132*91f16700Schasinglulu } 133*91f16700Schasinglulu 134*91f16700Schasinglulu /* 135*91f16700Schasinglulu * Perform the platform specific architecture setup shared between 136*91f16700Schasinglulu * ARM standard platforms. 137*91f16700Schasinglulu */ 138*91f16700Schasinglulu void arm_bl1_platform_setup(void) 139*91f16700Schasinglulu { 140*91f16700Schasinglulu uint32_t fw_config_max_size; 141*91f16700Schasinglulu 142*91f16700Schasinglulu /* Initialise the IO layer and register platform IO devices */ 143*91f16700Schasinglulu plat_arm_io_setup(); 144*91f16700Schasinglulu 145*91f16700Schasinglulu /* Check if we need FWU before further processing */ 146*91f16700Schasinglulu is_fwu_needed = plat_arm_bl1_fwu_needed(); 147*91f16700Schasinglulu if (is_fwu_needed) { 148*91f16700Schasinglulu ERROR("Skip platform setup as FWU detected\n"); 149*91f16700Schasinglulu return; 150*91f16700Schasinglulu } 151*91f16700Schasinglulu 152*91f16700Schasinglulu /* Set global DTB info for fixed fw_config information */ 153*91f16700Schasinglulu fw_config_max_size = ARM_FW_CONFIG_LIMIT - ARM_FW_CONFIG_BASE; 154*91f16700Schasinglulu set_config_info(ARM_FW_CONFIG_BASE, ~0UL, fw_config_max_size, 155*91f16700Schasinglulu FW_CONFIG_ID); 156*91f16700Schasinglulu 157*91f16700Schasinglulu assert(bl1_plat_get_image_desc(BL33_IMAGE_ID) != NULL); 158*91f16700Schasinglulu 159*91f16700Schasinglulu /* 160*91f16700Schasinglulu * Allow access to the System counter timer module and program 161*91f16700Schasinglulu * counter frequency for non secure images during FWU 162*91f16700Schasinglulu */ 163*91f16700Schasinglulu #ifdef ARM_SYS_TIMCTL_BASE 164*91f16700Schasinglulu arm_configure_sys_timer(); 165*91f16700Schasinglulu #endif 166*91f16700Schasinglulu #if (ARM_ARCH_MAJOR > 7) || defined(ARMV7_SUPPORTS_GENERIC_TIMER) 167*91f16700Schasinglulu write_cntfrq_el0(plat_get_syscnt_freq2()); 168*91f16700Schasinglulu #endif 169*91f16700Schasinglulu } 170*91f16700Schasinglulu 171*91f16700Schasinglulu void bl1_platform_setup(void) 172*91f16700Schasinglulu { 173*91f16700Schasinglulu arm_bl1_platform_setup(); 174*91f16700Schasinglulu 175*91f16700Schasinglulu /* Initialize System level generic or SP804 timer */ 176*91f16700Schasinglulu fvp_timer_init(); 177*91f16700Schasinglulu } 178*91f16700Schasinglulu 179*91f16700Schasinglulu __dead2 void bl1_plat_fwu_done(void *client_cookie, void *reserved) 180*91f16700Schasinglulu { 181*91f16700Schasinglulu /* Setup the watchdog to reset the system as soon as possible */ 182*91f16700Schasinglulu sp805_refresh(ARM_SP805_TWDG_BASE, 1U); 183*91f16700Schasinglulu 184*91f16700Schasinglulu while (true) { 185*91f16700Schasinglulu wfi(); 186*91f16700Schasinglulu } 187*91f16700Schasinglulu } 188*91f16700Schasinglulu 189*91f16700Schasinglulu unsigned int bl1_plat_get_next_image_id(void) 190*91f16700Schasinglulu { 191*91f16700Schasinglulu return is_fwu_needed ? NS_BL1U_IMAGE_ID : BL33_IMAGE_ID; 192*91f16700Schasinglulu } 193*91f16700Schasinglulu 194*91f16700Schasinglulu /* 195*91f16700Schasinglulu * Returns BL33 image details. 196*91f16700Schasinglulu */ 197*91f16700Schasinglulu struct image_desc *bl1_plat_get_image_desc(unsigned int image_id) 198*91f16700Schasinglulu { 199*91f16700Schasinglulu static image_desc_t bl33_img_desc = BL33_IMAGE_DESC; 200*91f16700Schasinglulu 201*91f16700Schasinglulu return &bl33_img_desc; 202*91f16700Schasinglulu } 203*91f16700Schasinglulu 204*91f16700Schasinglulu /* 205*91f16700Schasinglulu * This function populates the default arguments to BL33. 206*91f16700Schasinglulu * The BL33 memory layout structure is allocated and the 207*91f16700Schasinglulu * calculated layout is populated in arg1 to BL33. 208*91f16700Schasinglulu */ 209*91f16700Schasinglulu int bl1_plat_handle_post_image_load(unsigned int image_id) 210*91f16700Schasinglulu { 211*91f16700Schasinglulu meminfo_t *bl33_secram_layout; 212*91f16700Schasinglulu meminfo_t *bl1_secram_layout; 213*91f16700Schasinglulu image_desc_t *image_desc; 214*91f16700Schasinglulu entry_point_info_t *ep_info; 215*91f16700Schasinglulu 216*91f16700Schasinglulu if (image_id != BL33_IMAGE_ID) { 217*91f16700Schasinglulu return 0; 218*91f16700Schasinglulu } 219*91f16700Schasinglulu /* Get the image descriptor */ 220*91f16700Schasinglulu image_desc = bl1_plat_get_image_desc(BL33_IMAGE_ID); 221*91f16700Schasinglulu assert(image_desc != NULL); 222*91f16700Schasinglulu 223*91f16700Schasinglulu /* Get the entry point info */ 224*91f16700Schasinglulu ep_info = &image_desc->ep_info; 225*91f16700Schasinglulu 226*91f16700Schasinglulu /* Find out how much free trusted ram remains after BL1 load */ 227*91f16700Schasinglulu bl1_secram_layout = bl1_plat_sec_mem_layout(); 228*91f16700Schasinglulu 229*91f16700Schasinglulu /* 230*91f16700Schasinglulu * Create a new layout of memory for BL33 as seen by BL1 i.e. 231*91f16700Schasinglulu * tell it the amount of total and free memory available. 232*91f16700Schasinglulu * This layout is created at the first free address visible 233*91f16700Schasinglulu * to BL33. BL33 will read the memory layout before using its 234*91f16700Schasinglulu * memory for other purposes. 235*91f16700Schasinglulu */ 236*91f16700Schasinglulu bl33_secram_layout = (meminfo_t *) bl1_secram_layout->total_base; 237*91f16700Schasinglulu 238*91f16700Schasinglulu bl1_calc_bl2_mem_layout(bl1_secram_layout, bl33_secram_layout); 239*91f16700Schasinglulu 240*91f16700Schasinglulu ep_info->args.arg1 = (uintptr_t)bl33_secram_layout; 241*91f16700Schasinglulu 242*91f16700Schasinglulu VERBOSE("BL1: BL3 memory layout address = %p\n", 243*91f16700Schasinglulu (void *) bl33_secram_layout); 244*91f16700Schasinglulu return 0; 245*91f16700Schasinglulu } 246