blob: c15ee4e2187f8e9027d3aee0131b66754df479c8 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Olivier Deprez2661ba52024-02-19 18:50:53 +01002 * Copyright (c) 2018-2024, Arm Limited. All rights reserved.
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +01008#include <errno.h>
J-Alves9f6f0142020-06-17 15:37:59 +01009#include <debug.h>
Max Shvetsov103e0562021-02-04 16:58:31 +000010
J-Alveseeb25472021-03-11 09:54:21 +000011#include <cactus_message_loop.h>
J-Alves9f6f0142020-06-17 15:37:59 +010012#include <drivers/arm/pl011.h>
13#include <drivers/console.h>
J-Alves9f6f0142020-06-17 15:37:59 +010014#include <lib/aarch64/arch_helpers.h>
Max Shvetsov2263efb2020-11-12 17:30:11 +000015#include <lib/tftf_lib.h>
J-Alves9f6f0142020-06-17 15:37:59 +010016#include <lib/xlat_tables/xlat_mmu_helpers.h>
17#include <lib/xlat_tables/xlat_tables_v2.h>
Ruari Phippsddc661a2020-09-10 09:06:14 +010018
Daniel Boulbyce386b12022-03-29 18:36:36 +010019#include <ffa_helpers.h>
Max Shvetsov103e0562021-02-04 16:58:31 +000020#include <plat_arm.h>
21#include <plat/common/platform.h>
22#include <platform_def.h>
Ruari Phippsddc661a2020-09-10 09:06:14 +010023#include <sp_debug.h>
J-Alves5aecd982020-06-11 10:25:33 +010024#include <sp_helpers.h>
Olivier Deprez6967c242021-04-09 09:24:08 +020025#include <spm_helpers.h>
J-Alves9f6f0142020-06-17 15:37:59 +010026#include <std_svc.h>
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010027
Daniel Boulbyf3da5912022-04-01 12:31:52 +010028#include "sp_def.h"
29#include "sp_tests.h"
Max Shvetsov103e0562021-02-04 16:58:31 +000030#include "cactus.h"
J-Alves63cdaa72020-10-08 17:22:45 +010031
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020032/* Host machine information injected by the build system in the ELF file. */
33extern const char build_message[];
34extern const char version_string[];
35
Max Shvetsov2263efb2020-11-12 17:30:11 +000036extern void secondary_cold_entry(void);
37
Manish Pandey87d4c702021-03-02 22:31:57 +000038/* Global ffa_id */
Daniel Boulbye79d2072021-03-03 11:34:53 +000039ffa_id_t g_ffa_id;
Manish Pandey87d4c702021-03-02 22:31:57 +000040
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010041/*
42 *
43 * Message loop function
44 * Notice we cannot use regular print functions because this serves to both
45 * "primary" and "secondary" VMs. Secondary VM cannot access UART directly
46 * but rather through Hafnium print hypercall.
47 *
48 */
J-Alves0e1e7ca2021-01-25 14:11:06 +000049
Daniel Boulbye79d2072021-03-03 11:34:53 +000050static void __dead2 message_loop(ffa_id_t vm_id, struct mailbox_buffers *mb)
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010051{
Daniel Boulbyce386b12022-03-29 18:36:36 +010052 struct ffa_value ffa_ret;
Daniel Boulbye79d2072021-03-03 11:34:53 +000053 ffa_id_t destination;
J-Alves1d203f12020-11-11 11:38:49 +000054
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010055 /*
J-Alves63cdaa72020-10-08 17:22:45 +010056 * This initial wait call is necessary to inform SPMD that
57 * SP initialization has completed. It blocks until receiving
58 * a direct message request.
59 */
60
J-Alves7581c382020-05-07 18:34:20 +010061 ffa_ret = ffa_msg_wait();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010062
63 for (;;) {
Madhukar Pappireddy6681b7a2024-11-01 16:27:44 -050064 VERBOSE("Woke up with func:%s id: %x\n",
65 ffa_func_name(ffa_func_id(ffa_ret)),
66 ffa_func_id(ffa_ret));
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010067
J-Alves6cb21d92021-01-07 15:18:12 +000068 if (ffa_func_id(ffa_ret) == FFA_ERROR) {
Karl Meakine3e57062024-01-18 14:28:36 +000069 ERROR("Error: %s\n",
70 ffa_error_name(ffa_error_code(ffa_ret)));
J-Alvesda6ac322020-11-09 15:45:30 +000071 break;
72 }
73
J-Alves6cb21d92021-01-07 15:18:12 +000074 if (ffa_func_id(ffa_ret) != FFA_MSG_SEND_DIRECT_REQ_SMC32 &&
Madhukar Pappireddycd183ef2021-08-05 15:34:07 -050075 ffa_func_id(ffa_ret) != FFA_MSG_SEND_DIRECT_REQ_SMC64 &&
Olivier Deprez57245242022-08-24 16:19:27 +020076 ffa_func_id(ffa_ret) != FFA_INTERRUPT &&
77 ffa_func_id(ffa_ret) != FFA_RUN) {
Madhukar Pappireddy6681b7a2024-11-01 16:27:44 -050078 ERROR("%s(%x) unknown func id %s\n", __func__, vm_id,
Karl Meakine3e57062024-01-18 14:28:36 +000079 ffa_func_name(ffa_func_id(ffa_ret)));
Olivier Deprez73d81cf2020-09-15 16:57:00 +020080 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010081 }
82
Olivier Deprez57245242022-08-24 16:19:27 +020083 if ((ffa_func_id(ffa_ret) == FFA_INTERRUPT) ||
84 (ffa_func_id(ffa_ret) == FFA_RUN)) {
Madhukar Pappireddycd183ef2021-08-05 15:34:07 -050085 /*
86 * Received FFA_INTERRUPT in waiting state.
87 * The interrupt id is passed although this is just
88 * informational as we're running with virtual
89 * interrupts unmasked and the interrupt is processed
90 * by the interrupt handler.
91 */
Madhukar Pappireddy6681b7a2024-11-01 16:27:44 -050092 if (ffa_func_id(ffa_ret) == FFA_RUN) {
93 /*
94 * Received FFA_RUN in waiting state, the
95 * endpoint simply returns by FFA_MSG_WAIT.
96 */
97 VERBOSE("Nothing to do. Exit to NWd\n");
98 }
99
Madhukar Pappireddycd183ef2021-08-05 15:34:07 -0500100 ffa_ret = ffa_msg_wait();
101 continue;
102 }
J-Alvesda6ac322020-11-09 15:45:30 +0000103
Madhukar Pappireddycd183ef2021-08-05 15:34:07 -0500104 destination = ffa_dir_msg_dest(ffa_ret);
J-Alvesda6ac322020-11-09 15:45:30 +0000105 if (destination != vm_id) {
J-Alves6cb21d92021-01-07 15:18:12 +0000106 ERROR("%s(%u) invalid vm id 0x%x\n",
107 __func__, vm_id, destination);
Olivier Deprez73d81cf2020-09-15 16:57:00 +0200108 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100109 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100110
J-Alves4cb9dee2021-03-03 13:59:52 +0000111 if (!cactus_handle_cmd(&ffa_ret, &ffa_ret, mb)) {
J-Alves63cdaa72020-10-08 17:22:45 +0100112 break;
113 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100114 }
Olivier Deprez73d81cf2020-09-15 16:57:00 +0200115
116 panic();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100117}
118
119static const mmap_region_t cactus_mmap[] __attribute__((used)) = {
Daniel Boulby3d8cd682024-07-23 14:28:15 +0100120 MAP_REGION_FLAT(PLAT_CACTUS_DEVICE_BASE, PLAT_CACTUS_DEVICE_SIZE,
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100121 MT_DEVICE | MT_RW),
Daniel Boulby3d8cd682024-07-23 14:28:15 +0100122
Madhukar Pappireddy172523b2020-12-31 19:25:33 -0600123 /* scratch memory allocated to be used for running SMMU tests */
124 MAP_REGION_FLAT(PLAT_CACTUS_MEMCPY_BASE, PLAT_CACTUS_MEMCPY_RANGE,
125 MT_MEMORY | MT_RW),
Madhukar Pappireddy5d4936a2024-02-12 18:49:21 -0600126#if PLAT_fvp
Madhukar Pappireddya08c7192023-10-18 17:49:32 -0500127 MAP_REGION_FLAT(PLAT_CACTUS_NS_MEMCPY_BASE, PLAT_CACTUS_MEMCPY_RANGE,
128 MT_MEMORY | MT_RW | MT_NS),
Madhukar Pappireddy5d4936a2024-02-12 18:49:21 -0600129#endif
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100130 {0}
131};
132
Manish Pandey26c6f812020-04-30 11:43:00 +0100133static void cactus_print_memory_layout(unsigned int vm_id)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200134{
Olivier Deprezaed7f082020-11-04 15:11:59 +0100135 INFO("Secure Partition memory layout:\n");
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200136
Olivier Deprezaed7f082020-11-04 15:11:59 +0100137 INFO(" Text region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100138 (void *)CACTUS_TEXT_START, (void *)CACTUS_TEXT_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100139
Olivier Deprezaed7f082020-11-04 15:11:59 +0100140 INFO(" Read-only data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100141 (void *)CACTUS_RODATA_START, (void *)CACTUS_RODATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100142
Olivier Deprezaed7f082020-11-04 15:11:59 +0100143 INFO(" Data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100144 (void *)CACTUS_DATA_START, (void *)CACTUS_DATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100145
Olivier Deprezaed7f082020-11-04 15:11:59 +0100146 INFO(" BSS region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100147 (void *)CACTUS_BSS_START, (void *)CACTUS_BSS_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100148
Olivier Deprezaed7f082020-11-04 15:11:59 +0100149 INFO(" RX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100150 (void *)get_sp_rx_start(vm_id),
151 (void *)get_sp_rx_end(vm_id));
Manish Pandey26c6f812020-04-30 11:43:00 +0100152
Olivier Deprezaed7f082020-11-04 15:11:59 +0100153 INFO(" TX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100154 (void *)get_sp_tx_start(vm_id),
155 (void *)get_sp_tx_end(vm_id));
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200156}
157
J-Alves31d87952022-04-04 12:34:16 +0100158static void cactus_print_boot_info(struct ffa_boot_info_header *boot_info_header)
159{
160 struct ffa_boot_info_desc *boot_info_desc;
161
162 if (boot_info_header == NULL) {
163 NOTICE("SP doesn't have boot information!\n");
164 return;
165 }
166
167 VERBOSE("SP boot info:\n");
168 VERBOSE(" Signature: %x\n", boot_info_header->signature);
169 VERBOSE(" Version: %x\n", boot_info_header->version);
170 VERBOSE(" Blob Size: %u\n", boot_info_header->info_blob_size);
171 VERBOSE(" Descriptor Size: %u\n", boot_info_header->desc_size);
172 VERBOSE(" Descriptor Count: %u\n", boot_info_header->desc_count);
173
174 boot_info_desc = boot_info_header->boot_info;
175
176 if (boot_info_desc == NULL) {
177 ERROR("Boot data arguments error...\n");
178 return;
179 }
180
181 for (uint32_t i = 0; i < boot_info_header->desc_count; i++) {
182 VERBOSE(" Boot Data:\n");
183 VERBOSE(" Type: %u\n",
184 ffa_boot_info_type(&boot_info_desc[i]));
185 VERBOSE(" Type ID: %u\n",
186 ffa_boot_info_type_id(&boot_info_desc[i]));
187 VERBOSE(" Flags:\n");
188 VERBOSE(" Name Format: %x\n",
189 ffa_boot_info_name_format(&boot_info_desc[i]));
190 VERBOSE(" Content Format: %x\n",
191 ffa_boot_info_content_format(&boot_info_desc[i]));
192 VERBOSE(" Size: %u\n", boot_info_desc[i].size);
193 VERBOSE(" Value: %llx\n", boot_info_desc[i].content);
194 }
195}
196
Manish Pandey26c6f812020-04-30 11:43:00 +0100197static void cactus_plat_configure_mmu(unsigned int vm_id)
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100198{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100199 mmap_add_region(CACTUS_TEXT_START,
200 CACTUS_TEXT_START,
201 CACTUS_TEXT_END - CACTUS_TEXT_START,
202 MT_CODE);
203 mmap_add_region(CACTUS_RODATA_START,
204 CACTUS_RODATA_START,
205 CACTUS_RODATA_END - CACTUS_RODATA_START,
206 MT_RO_DATA);
207 mmap_add_region(CACTUS_DATA_START,
208 CACTUS_DATA_START,
209 CACTUS_DATA_END - CACTUS_DATA_START,
210 MT_RW_DATA);
211 mmap_add_region(CACTUS_BSS_START,
212 CACTUS_BSS_START,
213 CACTUS_BSS_END - CACTUS_BSS_START,
214 MT_RW_DATA);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100215
Max Shvetsovc32f4782020-06-23 09:41:15 +0100216 mmap_add_region(get_sp_rx_start(vm_id),
217 get_sp_rx_start(vm_id),
Daniel Boulbyf3da5912022-04-01 12:31:52 +0100218 (SP_RX_TX_SIZE / 2),
Manish Pandey26c6f812020-04-30 11:43:00 +0100219 MT_RO_DATA);
220
Max Shvetsovc32f4782020-06-23 09:41:15 +0100221 mmap_add_region(get_sp_tx_start(vm_id),
222 get_sp_tx_start(vm_id),
Daniel Boulbyf3da5912022-04-01 12:31:52 +0100223 (SP_RX_TX_SIZE / 2),
Manish Pandey26c6f812020-04-30 11:43:00 +0100224 MT_RW_DATA);
225
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100226 mmap_add(cactus_mmap);
227 init_xlat_tables();
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100228}
229
Madhukar Pappireddyd4de7ed2023-12-11 17:48:48 -0600230static struct ffa_value register_secondary_entrypoint(void)
Max Shvetsov2263efb2020-11-12 17:30:11 +0000231{
Arunachalam Ganapathybbf08c52023-11-07 13:38:54 +0000232 struct ffa_value args;
Max Shvetsov2263efb2020-11-12 17:30:11 +0000233
234 args.fid = FFA_SECONDARY_EP_REGISTER_SMC64;
235 args.arg1 = (u_register_t)&secondary_cold_entry;
236
Madhukar Pappireddyd4de7ed2023-12-11 17:48:48 -0600237 return ffa_service_call(&args);
Max Shvetsov2263efb2020-11-12 17:30:11 +0000238}
239
J-Alves31d87952022-04-04 12:34:16 +0100240void __dead2 cactus_main(bool primary_cold_boot,
241 struct ffa_boot_info_header *boot_info_header)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200242{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100243 assert(IS_IN_EL1() != 0);
244
Max Shvetsovc32f4782020-06-23 09:41:15 +0100245 struct mailbox_buffers mb;
Daniel Boulbyce386b12022-03-29 18:36:36 +0100246 struct ffa_value ret;
J-Alvesda6ac322020-11-09 15:45:30 +0000247
J-Alves7581c382020-05-07 18:34:20 +0100248 /* Get current FFA id */
Daniel Boulbyce386b12022-03-29 18:36:36 +0100249 struct ffa_value ffa_id_ret = ffa_id_get();
Daniel Boulby198deda2021-03-03 11:35:25 +0000250 ffa_id_t ffa_id = ffa_endpoint_id(ffa_id_ret);
J-Alves6cb21d92021-01-07 15:18:12 +0000251 if (ffa_func_id(ffa_id_ret) != FFA_SUCCESS_SMC32) {
J-Alves7581c382020-05-07 18:34:20 +0100252 ERROR("FFA_ID_GET failed.\n");
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100253 panic();
254 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100255
Max Shvetsov2263efb2020-11-12 17:30:11 +0000256 if (primary_cold_boot == true) {
257 /* Clear BSS */
258 memset((void *)CACTUS_BSS_START,
259 0, CACTUS_BSS_END - CACTUS_BSS_START);
260
Max Shvetsov2263efb2020-11-12 17:30:11 +0000261 mb.send = (void *) get_sp_tx_start(ffa_id);
262 mb.recv = (void *) get_sp_rx_start(ffa_id);
263
264 /* Configure and enable Stage-1 MMU, enable D-Cache */
265 cactus_plat_configure_mmu(ffa_id);
Madhukar Pappireddy7caaa4a2022-01-28 17:01:35 -0600266
267 /* Initialize locks for tail end interrupt handler */
268 sp_handler_spin_lock_init();
J-Alves31d87952022-04-04 12:34:16 +0100269
270 if (boot_info_header != NULL) {
271 /*
272 * TODO: Currently just validating that cactus can
273 * access the boot info descriptors. In case we want to
274 * use the boot info contents, we should check the
275 * blob and remap if the size is bigger than one page.
276 * Only then access the contents.
277 */
278 mmap_add_dynamic_region(
279 (unsigned long long)boot_info_header,
280 (uintptr_t)boot_info_header,
281 PAGE_SIZE, MT_RO_DATA);
282 }
Max Shvetsov2263efb2020-11-12 17:30:11 +0000283 }
284
Manish Pandey87d4c702021-03-02 22:31:57 +0000285 /*
286 * The local ffa_id value is held on the stack. The global g_ffa_id
287 * value is set after BSS is cleared.
288 */
289 g_ffa_id = ffa_id;
290
Manish Pandey26c6f812020-04-30 11:43:00 +0100291 enable_mmu_el1(0);
292
Manish Pandeyd27b37d2021-03-02 14:41:58 +0000293 /* Enable IRQ/FIQ */
294 enable_irq();
295 enable_fiq();
296
Max Shvetsov2263efb2020-11-12 17:30:11 +0000297 if (primary_cold_boot == false) {
298 goto msg_loop;
299 }
300
J-Alvesf3ea1a82024-01-17 11:06:55 +0000301 set_putc_impl(FFA_SVC_SMC_CALL_AS_STDOUT);
Manish Pandey29495372020-04-09 15:19:26 +0100302
Olivier Deprez24bd1702021-10-05 14:35:17 +0200303 /* Below string is monitored by CI expect script. */
304 NOTICE("Booting Secure Partition (ID: %x)\n%s\n%s\n",
305 ffa_id, build_message, version_string);
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100306
J-Alvesf3ea1a82024-01-17 11:06:55 +0000307 if (ffa_id == SP_ID(1)) {
308 cactus_print_boot_info(boot_info_header);
309 }
310
Olivier Deprez24bd1702021-10-05 14:35:17 +0200311 if (ffa_id == (SPM_VM_ID_FIRST + 2)) {
312 VERBOSE("Mapping RXTX Region\n");
313 CONFIGURE_AND_MAP_MAILBOX(mb, PAGE_SIZE, ret);
314 if (ffa_func_id(ret) != FFA_SUCCESS_SMC32) {
Karl Meakine3e57062024-01-18 14:28:36 +0000315 ERROR("Failed to map RXTX buffers. Error: %s\n",
316 ffa_error_name(ffa_error_code(ret)));
Olivier Deprez24bd1702021-10-05 14:35:17 +0200317 panic();
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100318 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100319 }
Manish Pandey26c6f812020-04-30 11:43:00 +0100320
Manish Pandey26c6f812020-04-30 11:43:00 +0100321 cactus_print_memory_layout(ffa_id);
322
Madhukar Pappireddyd4de7ed2023-12-11 17:48:48 -0600323 ret = register_secondary_entrypoint();
324
325 /* FFA_SECONDARY_EP_REGISTER interface is not supported for UP SP. */
326 if (ffa_id == (SPM_VM_ID_FIRST + 2)) {
Karl Meakinc884d6b2024-04-16 14:01:10 +0100327 EXPECT(ffa_func_id(ret), FFA_ERROR);
328 EXPECT(ffa_error_code(ret), FFA_ERROR_NOT_SUPPORTED);
Madhukar Pappireddyd4de7ed2023-12-11 17:48:48 -0600329 } else {
Karl Meakinc884d6b2024-04-16 14:01:10 +0100330 EXPECT(ffa_func_id(ret), FFA_SUCCESS_SMC32);
Madhukar Pappireddyd4de7ed2023-12-11 17:48:48 -0600331 }
332
Madhukar Pappireddy6f7344b2024-07-05 16:21:26 -0500333#if !SPMC_AT_EL3
Madhukar Pappireddyca1e2012022-06-22 17:05:09 -0500334 discover_managed_exit_interrupt_id();
Madhukar Pappireddy4d1f1122023-03-16 17:54:24 -0500335 register_maintenance_interrupt_handlers();
Max Shvetsov2263efb2020-11-12 17:30:11 +0000336
Olivier Deprez2661ba52024-02-19 18:50:53 +0100337 /* Invoking self tests */
J-Alves367154e2024-05-13 18:01:02 +0100338 ffa_tests(&mb, true);
Olivier Deprez2661ba52024-02-19 18:50:53 +0100339 cpu_feature_tests();
Madhukar Pappireddy6f7344b2024-07-05 16:21:26 -0500340#endif
J-Alves9f6f0142020-06-17 15:37:59 +0100341
Max Shvetsov2263efb2020-11-12 17:30:11 +0000342msg_loop:
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100343 /* End up to message loop */
J-Alves63cdaa72020-10-08 17:22:45 +0100344 message_loop(ffa_id, &mb);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100345
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100346 /* Not reached */
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200347}