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