blob: 6b5f03ccbe1c1720e764e013b488f90c1ee045c7 [file] [log] [blame]
Amit Nagalc97857d2024-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 Bollapalliea453872024-12-04 04:12:53 +00004 * Copyright (c) 2022-2025, Advanced Micro Devices, Inc. All rights reserved.
Amit Nagalc97857d2024-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 Bollapalli11964742024-07-01 07:07:53 +000023#include <plat_console.h>
Amit Nagalc97857d2024-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 Nagal1fbe81f2024-08-08 22:15:19 -120030#include <plat_xfer_list.h>
Amit Nagalc97857d2024-06-05 12:32:38 +053031#include <pm_api_sys.h>
32#include <pm_client.h>
33
34static entry_point_info_t bl32_image_ep_info;
35static entry_point_info_t bl33_image_ep_info;
36
37/*
38 * Return a pointer to the 'entry_point_info' structure of the next image for
39 * the security state specified. BL33 corresponds to the non-secure image type
40 * while BL32 corresponds to the secure image type. A NULL pointer is returned
41 * if the image does not exist.
42 */
43entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
44{
45 assert(sec_state_is_valid(type));
46
47 if (type == NON_SECURE) {
48 return &bl33_image_ep_info;
49 }
50
51 return &bl32_image_ep_info;
52}
53
54/*
55 * Set the build time defaults,if we can't find any config data.
56 */
57static inline void bl31_set_default_config(void)
58{
59 bl32_image_ep_info.pc = BL32_BASE;
60 bl32_image_ep_info.spsr = arm_get_spsr_for_bl32_entry();
61#if defined(SPD_opteed)
Maheedhar Bollapalli1fb34462025-01-27 06:30:41 +000062#if (TRANSFER_LIST == 0)
Amit Nagalc97857d2024-06-05 12:32:38 +053063 /* NS dtb addr passed to optee_os */
64 bl32_image_ep_info.args.arg3 = XILINX_OF_BOARD_DTB_ADDR;
65#endif
Maheedhar Bollapalli1fb34462025-01-27 06:30:41 +000066#endif
Amit Nagalc97857d2024-06-05 12:32:38 +053067 bl33_image_ep_info.pc = plat_get_ns_image_entrypoint();
Maheedhar Bollapalli07be78d2024-10-24 04:40:04 +000068 bl33_image_ep_info.spsr = (uint32_t)SPSR_64(MODE_EL2, MODE_SP_ELX,
Amit Nagalc97857d2024-06-05 12:32:38 +053069 DISABLE_ALL_EXCEPTIONS);
70}
71
72/*
73 * Perform any BL31 specific platform actions. Here is an opportunity to copy
74 * parameters passed by the calling EL (S-EL1 in BL2 & S-EL3 in BL1) before they
75 * are lost (potentially). This needs to be done before the MMU is initialized
76 * so that the memory layout can be used while creating page tables.
77 */
78void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
79 u_register_t arg2, u_register_t arg3)
80{
Maheedhar Bollapalli851df3c2024-10-01 03:55:06 +000081 (void)arg0;
82 (void)arg1;
83 (void)arg2;
84 (void)arg3;
Amit Nagalc97857d2024-06-05 12:32:38 +053085 uint32_t uart_clock;
Maheedhar Bollapalliea453872024-12-04 04:12:53 +000086#if (TRANSFER_LIST == 1)
Amit Nagal1fbe81f2024-08-08 22:15:19 -120087 int32_t rc;
Maheedhar Bollapalliea453872024-12-04 04:12:53 +000088 bool tl_status = false;
89#endif
Amit Nagalc97857d2024-06-05 12:32:38 +053090
91 board_detection();
92
93 /* FIXME */
94 switch (platform_id) {
95 case SPP:
96 switch (platform_version) {
97 case SPP_PSXC_MMI_V2_0:
98 cpu_clock = 770000;
99 break;
100 case SPP_PSXC_MMI_V3_0:
101 cpu_clock = 908000;
102 break;
103 default:
104 panic();
105 }
106 break;
107 case SPP_MMD:
108 switch (platform_version) {
109 case SPP_PSXC_ISP_AIE_V2_0:
110 case SPP_PSXC_MMD_AIE_FRZ_EA:
111 case SPP_PSXC_MMD_AIE_V3_0:
112 cpu_clock = 760000;
113 break;
114 default:
115 panic();
116 }
117 break;
118 case EMU:
119 case EMU_MMD:
120 cpu_clock = 112203;
121 break;
122 case QEMU:
Amit Nagalc97857d2024-06-05 12:32:38 +0530123 case SILICON:
124 cpu_clock = 100000000;
125 break;
126 default:
127 panic();
128 }
Maheedhar Bollapalliea453872024-12-04 04:12:53 +0000129#if (TRANSFER_LIST == 1)
130 tl_status = populate_data_from_xfer_list();
131 if (tl_status != true) {
132 WARN("Invalid transfer list\n");
133 }
134#endif
Amit Nagalc97857d2024-06-05 12:32:38 +0530135
136 uart_clock = get_uart_clk();
137
Saivardhan Thatikondae14ae4b2025-01-05 23:55:02 -1200138 /* Initialize the platform config for future decision making */
139 config_setup();
140
Maheedhar Bollapalli11964742024-07-01 07:07:53 +0000141 setup_console();
Amit Nagalc97857d2024-06-05 12:32:38 +0530142
Saivardhan Thatikonda4003ac02025-01-17 15:07:07 +0530143 NOTICE("TF-A running on %s v%d.%d, RTL v%d.%d, PS v%d.%d, PMC v%d.%d\n",
144 board_name_decode(),
145 (platform_version >> 1), platform_version % 10U,
146 (rtlversion >> 1), rtlversion % 10U,
147 (psversion >> 1), psversion % 10U,
148 (pmcversion >> 1), pmcversion % 10U);
Amit Nagalc97857d2024-06-05 12:32:38 +0530149
Amit Nagalc97857d2024-06-05 12:32:38 +0530150 /*
151 * Do initial security configuration to allow DRAM/device access. On
152 * Base only DRAM security is programmable (via TrustZone), but
153 * other platforms might have more programmable security devices
154 * present.
155 */
156
157 /* Populate common information for BL32 and BL33 */
158 SET_PARAM_HEAD(&bl32_image_ep_info, PARAM_EP, VERSION_1, 0);
159 SET_SECURITY_STATE(bl32_image_ep_info.h.attr, SECURE);
160 SET_PARAM_HEAD(&bl33_image_ep_info, PARAM_EP, VERSION_1, 0);
161 SET_SECURITY_STATE(bl33_image_ep_info.h.attr, NON_SECURE);
Amit Nagal1fbe81f2024-08-08 22:15:19 -1200162
Maheedhar Bollapalliea453872024-12-04 04:12:53 +0000163#if (TRANSFER_LIST == 1)
Amit Nagal1fbe81f2024-08-08 22:15:19 -1200164 rc = transfer_list_populate_ep_info(&bl32_image_ep_info, &bl33_image_ep_info);
165 if (rc == TL_OPS_NON || rc == TL_OPS_CUS) {
166 NOTICE("BL31: TL not found, using default config\n");
167 bl31_set_default_config();
168 }
Maheedhar Bollapalliea453872024-12-04 04:12:53 +0000169#else
170 bl31_set_default_config();
171#endif
Amit Nagalc97857d2024-06-05 12:32:38 +0530172
173 long rev_var = cpu_get_rev_var();
174
175 INFO("CPU Revision = 0x%lx\n", rev_var);
176 INFO("cpu_clock = %dHz, uart_clock = %dHz\n", cpu_clock, uart_clock);
177 NOTICE("BL31: Executing from 0x%x\n", BL31_BASE);
178 NOTICE("BL31: Secure code at 0x%lx\n", bl32_image_ep_info.pc);
179 NOTICE("BL31: Non secure code at 0x%lx\n", bl33_image_ep_info.pc);
180
181}
182
183static versal_intr_info_type_el3_t type_el3_interrupt_table[MAX_INTR_EL3];
184
185int request_intr_type_el3(uint32_t id, interrupt_type_handler_t handler)
186{
187 static uint32_t index;
188 uint32_t i;
Maheedhar Bollapallifb2fdcd2024-10-29 03:53:19 +0000189 int32_t ret = 0;
Amit Nagalc97857d2024-06-05 12:32:38 +0530190
191 /* Validate 'handler' and 'id' parameters */
Maheedhar Bollapalli15a9e382024-10-14 04:22:43 +0000192 if ((handler == NULL) || (index >= MAX_INTR_EL3)) {
Maheedhar Bollapallifb2fdcd2024-10-29 03:53:19 +0000193 ret = -EINVAL;
194 goto exit_label;
Amit Nagalc97857d2024-06-05 12:32:38 +0530195 }
196
197 /* Check if a handler has already been registered */
198 for (i = 0; i < index; i++) {
199 if (id == type_el3_interrupt_table[i].id) {
Maheedhar Bollapallifb2fdcd2024-10-29 03:53:19 +0000200 ret = -EALREADY;
201 goto exit_label;
Amit Nagalc97857d2024-06-05 12:32:38 +0530202 }
203 }
204
205 type_el3_interrupt_table[index].id = id;
206 type_el3_interrupt_table[index].handler = handler;
207
208 index++;
209
Maheedhar Bollapallifb2fdcd2024-10-29 03:53:19 +0000210exit_label:
211 return ret;
Amit Nagalc97857d2024-06-05 12:32:38 +0530212}
213
214static uint64_t rdo_el3_interrupt_handler(uint32_t id, uint32_t flags,
215 void *handle, void *cookie)
216{
Maheedhar Bollapalli851df3c2024-10-01 03:55:06 +0000217 (void)id;
Amit Nagalc97857d2024-06-05 12:32:38 +0530218 uint32_t intr_id;
219 uint32_t i;
220 interrupt_type_handler_t handler = NULL;
221
222 intr_id = plat_ic_get_pending_interrupt_id();
223
224 for (i = 0; i < MAX_INTR_EL3; i++) {
225 if (intr_id == type_el3_interrupt_table[i].id) {
226 handler = type_el3_interrupt_table[i].handler;
227 }
228 }
229
230 if (handler != NULL) {
231 (void)handler(intr_id, flags, handle, cookie);
232 }
233
234 return 0;
235}
236
237void bl31_platform_setup(void)
238{
239 prepare_dtb();
240
241 /* Initialize the gic cpu and distributor interfaces */
242 plat_gic_driver_init();
243 plat_gic_init();
244
245 if (platform_id != EMU) {
246 init_scmi_server();
247 }
248}
249
250void bl31_plat_runtime_setup(void)
251{
Maheedhar Bollapalli07be78d2024-10-24 04:40:04 +0000252 uint32_t flags = 0;
Amit Nagalc97857d2024-06-05 12:32:38 +0530253 int32_t rc;
254
255 set_interrupt_rm_flag(flags, NON_SECURE);
256 rc = register_interrupt_type_handler(INTR_TYPE_EL3,
257 rdo_el3_interrupt_handler, flags);
258 if (rc != 0) {
259 panic();
260 }
Maheedhar Bollapalli11964742024-07-01 07:07:53 +0000261
262 console_switch_state(CONSOLE_FLAG_RUNTIME);
Amit Nagalc97857d2024-06-05 12:32:38 +0530263}
264
265/*
266 * Perform the very early platform specific architectural setup here.
267 */
268void bl31_plat_arch_setup(void)
269{
270 const mmap_region_t bl_regions[] = {
Amit Nagalc97857d2024-06-05 12:32:38 +0530271 MAP_REGION_FLAT(BL31_BASE, BL31_END - BL31_BASE,
272 MT_MEMORY | MT_RW | MT_SECURE),
273 MAP_REGION_FLAT(BL_CODE_BASE, BL_CODE_END - BL_CODE_BASE,
274 MT_CODE | MT_SECURE),
275 MAP_REGION_FLAT(BL_RO_DATA_BASE, BL_RO_DATA_END - BL_RO_DATA_BASE,
276 MT_RO_DATA | MT_SECURE),
277 MAP_REGION_FLAT(SMT_BUFFER_BASE, 0x1000,
278 MT_DEVICE | MT_RW | MT_NON_CACHEABLE | MT_EXECUTE_NEVER | MT_NS),
279 {0}
280 };
281
282 setup_page_tables(bl_regions, plat_get_mmap());
283 enable_mmu(0);
284}