blob: 94ace7d4bc6adf187b7f90bbf9163c5a0109559c [file] [log] [blame]
Amit Nagal055796f2024-06-05 12:32:38 +05301/*
2 * Copyright (c) 2018-2020, Arm Limited and Contributors. All rights reserved.
3 * Copyright (c) 2018-2022, Xilinx, Inc. All rights reserved.
Maheedhar Bollapalli73365f42024-12-04 04:12:53 +00004 * Copyright (c) 2022-2025, Advanced Micro Devices, Inc. All rights reserved.
Amit Nagal055796f2024-06-05 12:32:38 +05305 *
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>
Maheedhar Bollapalli9c411e32024-07-01 07:07:53 +000023#include <plat_console.h>
Amit Nagal055796f2024-06-05 12:32:38 +053024#include <scmi.h>
25
26#include <def.h>
27#include <plat_fdt.h>
28#include <plat_private.h>
29#include <plat_startup.h>
Amit Nagalddb5b062024-08-08 22:15:19 -120030#include <plat_xfer_list.h>
Amit Nagal055796f2024-06-05 12:32:38 +053031#include <pm_api_sys.h>
32#include <pm_client.h>
33
Saivardhan Thatikondac77ba9c2025-03-05 14:18:37 +000034#include <plat_ocm_coherency.h>
35
Amit Nagal055796f2024-06-05 12:32:38 +053036static entry_point_info_t bl32_image_ep_info;
37static entry_point_info_t bl33_image_ep_info;
38
39/*
40 * Return a pointer to the 'entry_point_info' structure of the next image for
41 * the security state specified. BL33 corresponds to the non-secure image type
42 * while BL32 corresponds to the secure image type. A NULL pointer is returned
43 * if the image does not exist.
44 */
45entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
46{
47 assert(sec_state_is_valid(type));
48
49 if (type == NON_SECURE) {
50 return &bl33_image_ep_info;
51 }
52
53 return &bl32_image_ep_info;
54}
55
56/*
57 * Set the build time defaults,if we can't find any config data.
58 */
59static inline void bl31_set_default_config(void)
60{
61 bl32_image_ep_info.pc = BL32_BASE;
62 bl32_image_ep_info.spsr = arm_get_spsr_for_bl32_entry();
63#if defined(SPD_opteed)
Maheedhar Bollapalli91596aa2025-01-27 06:30:41 +000064#if (TRANSFER_LIST == 0)
Amit Nagal055796f2024-06-05 12:32:38 +053065 /* NS dtb addr passed to optee_os */
66 bl32_image_ep_info.args.arg3 = XILINX_OF_BOARD_DTB_ADDR;
67#endif
Maheedhar Bollapalli91596aa2025-01-27 06:30:41 +000068#endif
Amit Nagal055796f2024-06-05 12:32:38 +053069 bl33_image_ep_info.pc = plat_get_ns_image_entrypoint();
Maheedhar Bollapalli9cadba32024-10-24 04:40:04 +000070 bl33_image_ep_info.spsr = (uint32_t)SPSR_64(MODE_EL2, MODE_SP_ELX,
Amit Nagal055796f2024-06-05 12:32:38 +053071 DISABLE_ALL_EXCEPTIONS);
72}
73
74/*
75 * Perform any BL31 specific platform actions. Here is an opportunity to copy
76 * parameters passed by the calling EL (S-EL1 in BL2 & S-EL3 in BL1) before they
77 * are lost (potentially). This needs to be done before the MMU is initialized
78 * so that the memory layout can be used while creating page tables.
79 */
80void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
81 u_register_t arg2, u_register_t arg3)
82{
Maheedhar Bollapalli3e181da2024-10-01 03:55:06 +000083 (void)arg0;
84 (void)arg1;
85 (void)arg2;
86 (void)arg3;
Amit Nagal055796f2024-06-05 12:32:38 +053087 uint32_t uart_clock;
Maheedhar Bollapalli73365f42024-12-04 04:12:53 +000088#if (TRANSFER_LIST == 1)
Amit Nagalddb5b062024-08-08 22:15:19 -120089 int32_t rc;
Maheedhar Bollapalli73365f42024-12-04 04:12:53 +000090 bool tl_status = false;
91#endif
Amit Nagal055796f2024-06-05 12:32:38 +053092
93 board_detection();
94
95 /* FIXME */
96 switch (platform_id) {
97 case SPP:
98 switch (platform_version) {
99 case SPP_PSXC_MMI_V2_0:
100 cpu_clock = 770000;
101 break;
102 case SPP_PSXC_MMI_V3_0:
103 cpu_clock = 908000;
104 break;
105 default:
106 panic();
107 }
108 break;
109 case SPP_MMD:
110 switch (platform_version) {
111 case SPP_PSXC_ISP_AIE_V2_0:
112 case SPP_PSXC_MMD_AIE_FRZ_EA:
113 case SPP_PSXC_MMD_AIE_V3_0:
114 cpu_clock = 760000;
115 break;
116 default:
117 panic();
118 }
119 break;
120 case EMU:
121 case EMU_MMD:
122 cpu_clock = 112203;
123 break;
124 case QEMU:
Amit Nagal055796f2024-06-05 12:32:38 +0530125 case SILICON:
126 cpu_clock = 100000000;
127 break;
128 default:
129 panic();
130 }
Maheedhar Bollapalli73365f42024-12-04 04:12:53 +0000131#if (TRANSFER_LIST == 1)
132 tl_status = populate_data_from_xfer_list();
133 if (tl_status != true) {
134 WARN("Invalid transfer list\n");
135 }
136#endif
Amit Nagal055796f2024-06-05 12:32:38 +0530137
138 uart_clock = get_uart_clk();
139
Saivardhan Thatikondabef30852025-01-05 23:55:02 -1200140 /* Initialize the platform config for future decision making */
141 config_setup();
142
Maheedhar Bollapalli9c411e32024-07-01 07:07:53 +0000143 setup_console();
Amit Nagal055796f2024-06-05 12:32:38 +0530144
Saivardhan Thatikondac77ba9c2025-03-05 14:18:37 +0000145 if (IS_TFA_IN_OCM(BL31_BASE) && (check_ocm_coherency() < 0)) {
146 NOTICE("OCM coherency check not supported\n");
147 }
148
Saivardhan Thatikondaf0e29b42025-01-17 15:07:07 +0530149 NOTICE("TF-A running on %s v%d.%d, RTL v%d.%d, PS v%d.%d, PMC v%d.%d\n",
150 board_name_decode(),
151 (platform_version >> 1), platform_version % 10U,
152 (rtlversion >> 1), rtlversion % 10U,
153 (psversion >> 1), psversion % 10U,
154 (pmcversion >> 1), pmcversion % 10U);
Amit Nagal055796f2024-06-05 12:32:38 +0530155
Amit Nagal055796f2024-06-05 12:32:38 +0530156 /*
157 * Do initial security configuration to allow DRAM/device access. On
158 * Base only DRAM security is programmable (via TrustZone), but
159 * other platforms might have more programmable security devices
160 * present.
161 */
162
163 /* Populate common information for BL32 and BL33 */
164 SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0);
165 SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
166 SET_PARAM_HEAD(&bl33_image_ep_info, PARAM_EP, VERSION_1, 0);
167 SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
Amit Nagalddb5b062024-08-08 22:15:19 -1200168
Maheedhar Bollapalli73365f42024-12-04 04:12:53 +0000169#if (TRANSFER_LIST == 1)
Amit Nagalddb5b062024-08-08 22:15:19 -1200170 rc = transfer_list_populate_ep_info(&bl32_image_ep_info, &bl33_image_ep_info);
171 if (rc == TL_OPS_NON || rc == TL_OPS_CUS) {
172 NOTICE("BL31: TL not found, using default config\n");
173 bl31_set_default_config();
174 }
Maheedhar Bollapalli73365f42024-12-04 04:12:53 +0000175#else
176 bl31_set_default_config();
177#endif
Amit Nagal055796f2024-06-05 12:32:38 +0530178
179 long rev_var = cpu_get_rev_var();
180
181 INFO("CPU Revision = 0x%lx\n", rev_var);
182 INFO("cpu_clock = %dHz, uart_clock = %dHz\n", cpu_clock, uart_clock);
183 NOTICE("BL31: Executing from 0x%x\n", BL31_BASE);
Akshay Belsarec487a902025-03-19 15:09:30 +0530184#if (defined(SPD_tspd) || defined(SPD_opteed))
Amit Nagal055796f2024-06-05 12:32:38 +0530185 NOTICE("BL31: Secure code at 0x%lx\n", bl32_image_ep_info.pc);
Akshay Belsarec487a902025-03-19 15:09:30 +0530186#endif /* SPD_tspd || SPD_opteed */
Amit Nagal055796f2024-06-05 12:32:38 +0530187 NOTICE("BL31: Non secure code at 0x%lx\n", bl33_image_ep_info.pc);
188
189}
190
191static versal_intr_info_type_el3_t type_el3_interrupt_table[MAX_INTR_EL3];
192
193int request_intr_type_el3(uint32_t id, interrupt_type_handler_t handler)
194{
195 static uint32_t index;
196 uint32_t i;
Maheedhar Bollapalli120bdd52024-10-29 03:53:19 +0000197 int32_t ret = 0;
Amit Nagal055796f2024-06-05 12:32:38 +0530198
199 /* Validate 'handler' and 'id' parameters */
Maheedhar Bollapalli095debe2024-10-14 04:22:43 +0000200 if ((handler == NULL) || (index >= MAX_INTR_EL3)) {
Maheedhar Bollapalli120bdd52024-10-29 03:53:19 +0000201 ret = -EINVAL;
202 goto exit_label;
Amit Nagal055796f2024-06-05 12:32:38 +0530203 }
204
205 /* Check if a handler has already been registered */
206 for (i = 0; i < index; i++) {
207 if (id == type_el3_interrupt_table[i].id) {
Maheedhar Bollapalli120bdd52024-10-29 03:53:19 +0000208 ret = -EALREADY;
209 goto exit_label;
Amit Nagal055796f2024-06-05 12:32:38 +0530210 }
211 }
212
213 type_el3_interrupt_table[index].id = id;
214 type_el3_interrupt_table[index].handler = handler;
215
216 index++;
217
Maheedhar Bollapalli120bdd52024-10-29 03:53:19 +0000218exit_label:
219 return ret;
Amit Nagal055796f2024-06-05 12:32:38 +0530220}
221
222static uint64_t rdo_el3_interrupt_handler(uint32_t id, uint32_t flags,
223 void *handle, void *cookie)
224{
Maheedhar Bollapalli3e181da2024-10-01 03:55:06 +0000225 (void)id;
Amit Nagal055796f2024-06-05 12:32:38 +0530226 uint32_t intr_id;
227 uint32_t i;
228 interrupt_type_handler_t handler = NULL;
229
230 intr_id = plat_ic_get_pending_interrupt_id();
231
232 for (i = 0; i < MAX_INTR_EL3; i++) {
233 if (intr_id == type_el3_interrupt_table[i].id) {
234 handler = type_el3_interrupt_table[i].handler;
235 }
236 }
237
238 if (handler != NULL) {
239 (void)handler(intr_id, flags, handle, cookie);
240 }
241
242 return 0;
243}
244
245void bl31_platform_setup(void)
246{
247 prepare_dtb();
248
249 /* Initialize the gic cpu and distributor interfaces */
250 plat_gic_driver_init();
251 plat_gic_init();
252
253 if (platform_id != EMU) {
254 init_scmi_server();
255 }
256}
257
258void bl31_plat_runtime_setup(void)
259{
Maheedhar Bollapalli9cadba32024-10-24 04:40:04 +0000260 uint32_t flags = 0;
Amit Nagal055796f2024-06-05 12:32:38 +0530261 int32_t rc;
262
263 set_interrupt_rm_flag(flags, NON_SECURE);
264 rc = register_interrupt_type_handler(INTR_TYPE_EL3,
265 rdo_el3_interrupt_handler, flags);
266 if (rc != 0) {
267 panic();
268 }
Maheedhar Bollapalli9c411e32024-07-01 07:07:53 +0000269
270 console_switch_state(CONSOLE_FLAG_RUNTIME);
Amit Nagal055796f2024-06-05 12:32:38 +0530271}
272
273/*
274 * Perform the very early platform specific architectural setup here.
275 */
276void bl31_plat_arch_setup(void)
277{
278 const mmap_region_t bl_regions[] = {
Amit Nagal055796f2024-06-05 12:32:38 +0530279 MAP_REGION_FLAT(BL31_BASE, BL31_END - BL31_BASE,
280 MT_MEMORY | MT_RW | MT_SECURE),
281 MAP_REGION_FLAT(BL_CODE_BASE, BL_CODE_END - BL_CODE_BASE,
282 MT_CODE | MT_SECURE),
283 MAP_REGION_FLAT(BL_RO_DATA_BASE, BL_RO_DATA_END - BL_RO_DATA_BASE,
284 MT_RO_DATA | MT_SECURE),
285 MAP_REGION_FLAT(SMT_BUFFER_BASE, 0x1000,
286 MT_DEVICE | MT_RW | MT_NON_CACHEABLE | MT_EXECUTE_NEVER | MT_NS),
287 {0}
288 };
289
290 setup_page_tables(bl_regions, plat_get_mmap());
291 enable_mmu(0);
292}