1*91f16700Schasinglulu /* 2*91f16700Schasinglulu * Copyright (c) 2018-2020, Arm Limited and Contributors. All rights reserved. 3*91f16700Schasinglulu * Copyright (c) 2018-2022, Xilinx, Inc. All rights reserved. 4*91f16700Schasinglulu * Copyright (c) 2022-2023, Advanced Micro Devices, Inc. All rights reserved. 5*91f16700Schasinglulu * 6*91f16700Schasinglulu * SPDX-License-Identifier: BSD-3-Clause 7*91f16700Schasinglulu */ 8*91f16700Schasinglulu 9*91f16700Schasinglulu #include <assert.h> 10*91f16700Schasinglulu #include <errno.h> 11*91f16700Schasinglulu 12*91f16700Schasinglulu #include <bl31/bl31.h> 13*91f16700Schasinglulu #include <common/bl_common.h> 14*91f16700Schasinglulu #include <common/debug.h> 15*91f16700Schasinglulu #include <lib/mmio.h> 16*91f16700Schasinglulu #include <lib/xlat_tables/xlat_tables_v2.h> 17*91f16700Schasinglulu #include <plat/common/platform.h> 18*91f16700Schasinglulu #include <plat_arm.h> 19*91f16700Schasinglulu #include <plat_console.h> 20*91f16700Schasinglulu 21*91f16700Schasinglulu #include <plat_fdt.h> 22*91f16700Schasinglulu #include <plat_private.h> 23*91f16700Schasinglulu #include <plat_startup.h> 24*91f16700Schasinglulu #include <pm_api_sys.h> 25*91f16700Schasinglulu #include <pm_client.h> 26*91f16700Schasinglulu #include <pm_ipi.h> 27*91f16700Schasinglulu #include <versal_net_def.h> 28*91f16700Schasinglulu 29*91f16700Schasinglulu static entry_point_info_t bl32_image_ep_info; 30*91f16700Schasinglulu static entry_point_info_t bl33_image_ep_info; 31*91f16700Schasinglulu 32*91f16700Schasinglulu /* 33*91f16700Schasinglulu * Return a pointer to the 'entry_point_info' structure of the next image for 34*91f16700Schasinglulu * the security state specified. BL33 corresponds to the non-secure image type 35*91f16700Schasinglulu * while BL32 corresponds to the secure image type. A NULL pointer is returned 36*91f16700Schasinglulu * if the image does not exist. 37*91f16700Schasinglulu */ 38*91f16700Schasinglulu entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type) 39*91f16700Schasinglulu { 40*91f16700Schasinglulu assert(sec_state_is_valid(type)); 41*91f16700Schasinglulu 42*91f16700Schasinglulu if (type == NON_SECURE) { 43*91f16700Schasinglulu return &bl33_image_ep_info; 44*91f16700Schasinglulu } 45*91f16700Schasinglulu 46*91f16700Schasinglulu return &bl32_image_ep_info; 47*91f16700Schasinglulu } 48*91f16700Schasinglulu 49*91f16700Schasinglulu /* 50*91f16700Schasinglulu * Set the build time defaults,if we can't find any config data. 51*91f16700Schasinglulu */ 52*91f16700Schasinglulu static inline void bl31_set_default_config(void) 53*91f16700Schasinglulu { 54*91f16700Schasinglulu bl32_image_ep_info.pc = BL32_BASE; 55*91f16700Schasinglulu bl32_image_ep_info.spsr = arm_get_spsr_for_bl32_entry(); 56*91f16700Schasinglulu bl33_image_ep_info.pc = plat_get_ns_image_entrypoint(); 57*91f16700Schasinglulu bl33_image_ep_info.spsr = SPSR_64(MODE_EL2, MODE_SP_ELX, 58*91f16700Schasinglulu DISABLE_ALL_EXCEPTIONS); 59*91f16700Schasinglulu } 60*91f16700Schasinglulu 61*91f16700Schasinglulu /* 62*91f16700Schasinglulu * Perform any BL31 specific platform actions. Here is an opportunity to copy 63*91f16700Schasinglulu * parameters passed by the calling EL (S-EL1 in BL2 & S-EL3 in BL1) before they 64*91f16700Schasinglulu * are lost (potentially). This needs to be done before the MMU is initialized 65*91f16700Schasinglulu * so that the memory layout can be used while creating page tables. 66*91f16700Schasinglulu */ 67*91f16700Schasinglulu void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1, 68*91f16700Schasinglulu u_register_t arg2, u_register_t arg3) 69*91f16700Schasinglulu { 70*91f16700Schasinglulu #if !(TFA_NO_PM) 71*91f16700Schasinglulu uint64_t tfa_handoff_addr, buff[HANDOFF_PARAMS_MAX_SIZE] = {0}; 72*91f16700Schasinglulu uint32_t payload[PAYLOAD_ARG_CNT], max_size = HANDOFF_PARAMS_MAX_SIZE; 73*91f16700Schasinglulu enum pm_ret_status ret_status; 74*91f16700Schasinglulu #endif /* !(TFA_NO_PM) */ 75*91f16700Schasinglulu 76*91f16700Schasinglulu board_detection(); 77*91f16700Schasinglulu 78*91f16700Schasinglulu switch (platform_id) { 79*91f16700Schasinglulu case VERSAL_NET_SPP: 80*91f16700Schasinglulu cpu_clock = 1000000; 81*91f16700Schasinglulu break; 82*91f16700Schasinglulu case VERSAL_NET_EMU: 83*91f16700Schasinglulu cpu_clock = 3660000; 84*91f16700Schasinglulu break; 85*91f16700Schasinglulu case VERSAL_NET_QEMU: 86*91f16700Schasinglulu /* Random values now */ 87*91f16700Schasinglulu cpu_clock = 100000000; 88*91f16700Schasinglulu break; 89*91f16700Schasinglulu case VERSAL_NET_SILICON: 90*91f16700Schasinglulu cpu_clock = 100000000; 91*91f16700Schasinglulu break; 92*91f16700Schasinglulu default: 93*91f16700Schasinglulu panic(); 94*91f16700Schasinglulu } 95*91f16700Schasinglulu 96*91f16700Schasinglulu setup_console(); 97*91f16700Schasinglulu 98*91f16700Schasinglulu NOTICE("TF-A running on %s %d.%d\n", board_name_decode(), 99*91f16700Schasinglulu platform_version / 10U, platform_version % 10U); 100*91f16700Schasinglulu 101*91f16700Schasinglulu /* Initialize the platform config for future decision making */ 102*91f16700Schasinglulu versal_net_config_setup(); 103*91f16700Schasinglulu 104*91f16700Schasinglulu /* 105*91f16700Schasinglulu * Do initial security configuration to allow DRAM/device access. On 106*91f16700Schasinglulu * Base VERSAL_NET only DRAM security is programmable (via TrustZone), but 107*91f16700Schasinglulu * other platforms might have more programmable security devices 108*91f16700Schasinglulu * present. 109*91f16700Schasinglulu */ 110*91f16700Schasinglulu 111*91f16700Schasinglulu /* Populate common information for BL32 and BL33 */ 112*91f16700Schasinglulu SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0); 113*91f16700Schasinglulu SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE); 114*91f16700Schasinglulu SET_PARAM_HEAD(&bl33_image_ep_info, PARAM_EP, VERSION_1, 0); 115*91f16700Schasinglulu SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE); 116*91f16700Schasinglulu #if !(TFA_NO_PM) 117*91f16700Schasinglulu PM_PACK_PAYLOAD4(payload, LOADER_MODULE_ID, 1, PM_LOAD_GET_HANDOFF_PARAMS, 118*91f16700Schasinglulu (uintptr_t)buff >> 32U, (uintptr_t)buff, max_size); 119*91f16700Schasinglulu 120*91f16700Schasinglulu ret_status = pm_ipi_send_sync(primary_proc, payload, NULL, 0); 121*91f16700Schasinglulu if (ret_status == PM_RET_SUCCESS) { 122*91f16700Schasinglulu enum xbl_handoff xbl_ret; 123*91f16700Schasinglulu 124*91f16700Schasinglulu tfa_handoff_addr = (uintptr_t)&buff; 125*91f16700Schasinglulu 126*91f16700Schasinglulu xbl_ret = xbl_handover(&bl32_image_ep_info, &bl33_image_ep_info, 127*91f16700Schasinglulu tfa_handoff_addr); 128*91f16700Schasinglulu if (xbl_ret != XBL_HANDOFF_SUCCESS) { 129*91f16700Schasinglulu ERROR("BL31: PLM to TF-A handover failed %u\n", xbl_ret); 130*91f16700Schasinglulu panic(); 131*91f16700Schasinglulu } 132*91f16700Schasinglulu 133*91f16700Schasinglulu INFO("BL31: PLM to TF-A handover success\n"); 134*91f16700Schasinglulu 135*91f16700Schasinglulu /* 136*91f16700Schasinglulu * The BL32 load address is indicated as 0x0 in the handoff 137*91f16700Schasinglulu * parameters, which is different from the default/user-provided 138*91f16700Schasinglulu * load address of 0x60000000 but the flags are correctly 139*91f16700Schasinglulu * configured. Consequently, in this scenario, set the PC 140*91f16700Schasinglulu * to the requested BL32_BASE address. 141*91f16700Schasinglulu */ 142*91f16700Schasinglulu 143*91f16700Schasinglulu /* TODO: Remove the following check once this is fixed from PLM */ 144*91f16700Schasinglulu if (bl32_image_ep_info.pc == 0 && bl32_image_ep_info.spsr != 0) { 145*91f16700Schasinglulu bl32_image_ep_info.pc = (uintptr_t)BL32_BASE; 146*91f16700Schasinglulu } 147*91f16700Schasinglulu } else { 148*91f16700Schasinglulu INFO("BL31: setting up default configs\n"); 149*91f16700Schasinglulu 150*91f16700Schasinglulu bl31_set_default_config(); 151*91f16700Schasinglulu } 152*91f16700Schasinglulu #else 153*91f16700Schasinglulu bl31_set_default_config(); 154*91f16700Schasinglulu #endif /* !(TFA_NO_PM) */ 155*91f16700Schasinglulu 156*91f16700Schasinglulu NOTICE("BL31: Secure code at 0x%lx\n", bl32_image_ep_info.pc); 157*91f16700Schasinglulu NOTICE("BL31: Non secure code at 0x%lx\n", bl33_image_ep_info.pc); 158*91f16700Schasinglulu } 159*91f16700Schasinglulu 160*91f16700Schasinglulu static versal_intr_info_type_el3_t type_el3_interrupt_table[MAX_INTR_EL3]; 161*91f16700Schasinglulu 162*91f16700Schasinglulu int request_intr_type_el3(uint32_t id, interrupt_type_handler_t handler) 163*91f16700Schasinglulu { 164*91f16700Schasinglulu static uint32_t index; 165*91f16700Schasinglulu uint32_t i; 166*91f16700Schasinglulu 167*91f16700Schasinglulu /* Validate 'handler' and 'id' parameters */ 168*91f16700Schasinglulu if (handler == NULL || index >= MAX_INTR_EL3) { 169*91f16700Schasinglulu return -EINVAL; 170*91f16700Schasinglulu } 171*91f16700Schasinglulu 172*91f16700Schasinglulu /* Check if a handler has already been registered */ 173*91f16700Schasinglulu for (i = 0; i < index; i++) { 174*91f16700Schasinglulu if (id == type_el3_interrupt_table[i].id) { 175*91f16700Schasinglulu return -EALREADY; 176*91f16700Schasinglulu } 177*91f16700Schasinglulu } 178*91f16700Schasinglulu 179*91f16700Schasinglulu type_el3_interrupt_table[index].id = id; 180*91f16700Schasinglulu type_el3_interrupt_table[index].handler = handler; 181*91f16700Schasinglulu 182*91f16700Schasinglulu index++; 183*91f16700Schasinglulu 184*91f16700Schasinglulu return 0; 185*91f16700Schasinglulu } 186*91f16700Schasinglulu 187*91f16700Schasinglulu static uint64_t rdo_el3_interrupt_handler(uint32_t id, uint32_t flags, 188*91f16700Schasinglulu void *handle, void *cookie) 189*91f16700Schasinglulu { 190*91f16700Schasinglulu uint32_t intr_id; 191*91f16700Schasinglulu uint32_t i; 192*91f16700Schasinglulu interrupt_type_handler_t handler = NULL; 193*91f16700Schasinglulu 194*91f16700Schasinglulu intr_id = plat_ic_get_pending_interrupt_id(); 195*91f16700Schasinglulu 196*91f16700Schasinglulu for (i = 0; i < MAX_INTR_EL3; i++) { 197*91f16700Schasinglulu if (intr_id == type_el3_interrupt_table[i].id) { 198*91f16700Schasinglulu handler = type_el3_interrupt_table[i].handler; 199*91f16700Schasinglulu } 200*91f16700Schasinglulu } 201*91f16700Schasinglulu 202*91f16700Schasinglulu if (handler != NULL) { 203*91f16700Schasinglulu handler(intr_id, flags, handle, cookie); 204*91f16700Schasinglulu } 205*91f16700Schasinglulu 206*91f16700Schasinglulu return 0; 207*91f16700Schasinglulu } 208*91f16700Schasinglulu 209*91f16700Schasinglulu void bl31_platform_setup(void) 210*91f16700Schasinglulu { 211*91f16700Schasinglulu prepare_dtb(); 212*91f16700Schasinglulu 213*91f16700Schasinglulu /* Initialize the gic cpu and distributor interfaces */ 214*91f16700Schasinglulu plat_versal_net_gic_driver_init(); 215*91f16700Schasinglulu plat_versal_net_gic_init(); 216*91f16700Schasinglulu } 217*91f16700Schasinglulu 218*91f16700Schasinglulu void bl31_plat_runtime_setup(void) 219*91f16700Schasinglulu { 220*91f16700Schasinglulu uint64_t flags = 0; 221*91f16700Schasinglulu int32_t rc; 222*91f16700Schasinglulu 223*91f16700Schasinglulu set_interrupt_rm_flag(flags, NON_SECURE); 224*91f16700Schasinglulu rc = register_interrupt_type_handler(INTR_TYPE_EL3, 225*91f16700Schasinglulu rdo_el3_interrupt_handler, flags); 226*91f16700Schasinglulu if (rc != 0) { 227*91f16700Schasinglulu panic(); 228*91f16700Schasinglulu } 229*91f16700Schasinglulu 230*91f16700Schasinglulu console_switch_state(CONSOLE_FLAG_RUNTIME); 231*91f16700Schasinglulu } 232*91f16700Schasinglulu 233*91f16700Schasinglulu /* 234*91f16700Schasinglulu * Perform the very early platform specific architectural setup here. 235*91f16700Schasinglulu */ 236*91f16700Schasinglulu void bl31_plat_arch_setup(void) 237*91f16700Schasinglulu { 238*91f16700Schasinglulu const mmap_region_t bl_regions[] = { 239*91f16700Schasinglulu #if (defined(XILINX_OF_BOARD_DTB_ADDR) && !IS_TFA_IN_OCM(BL31_BASE)) 240*91f16700Schasinglulu MAP_REGION_FLAT(XILINX_OF_BOARD_DTB_ADDR, XILINX_OF_BOARD_DTB_MAX_SIZE, 241*91f16700Schasinglulu MT_MEMORY | MT_RW | MT_NS), 242*91f16700Schasinglulu #endif 243*91f16700Schasinglulu MAP_REGION_FLAT(BL31_BASE, BL31_END - BL31_BASE, 244*91f16700Schasinglulu MT_MEMORY | MT_RW | MT_SECURE), 245*91f16700Schasinglulu MAP_REGION_FLAT(BL_CODE_BASE, BL_CODE_END - BL_CODE_BASE, 246*91f16700Schasinglulu MT_CODE | MT_SECURE), 247*91f16700Schasinglulu MAP_REGION_FLAT(BL_RO_DATA_BASE, BL_RO_DATA_END - BL_RO_DATA_BASE, 248*91f16700Schasinglulu MT_RO_DATA | MT_SECURE), 249*91f16700Schasinglulu {0} 250*91f16700Schasinglulu }; 251*91f16700Schasinglulu 252*91f16700Schasinglulu setup_page_tables(bl_regions, plat_get_mmap()); 253*91f16700Schasinglulu enable_mmu(0); 254*91f16700Schasinglulu } 255