blob: 56cf21d3cbe81c30c8a83155dd435112bbaff825 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Daniel Boulbyce386b12022-03-29 18:36:36 +01002 * Copyright (c) 2018-2022, 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>
Max Shvetsov103e0562021-02-04 16:58:31 +000012#include <cactus_platform_def.h>
J-Alves9f6f0142020-06-17 15:37:59 +010013#include <drivers/arm/pl011.h>
14#include <drivers/console.h>
J-Alves9f6f0142020-06-17 15:37:59 +010015#include <lib/aarch64/arch_helpers.h>
Max Shvetsov2263efb2020-11-12 17:30:11 +000016#include <lib/tftf_lib.h>
J-Alves9f6f0142020-06-17 15:37:59 +010017#include <lib/xlat_tables/xlat_mmu_helpers.h>
18#include <lib/xlat_tables/xlat_tables_v2.h>
Ruari Phippsddc661a2020-09-10 09:06:14 +010019
Daniel Boulbyce386b12022-03-29 18:36:36 +010020#include <ffa_helpers.h>
Max Shvetsov103e0562021-02-04 16:58:31 +000021#include <plat_arm.h>
22#include <plat/common/platform.h>
23#include <platform_def.h>
Ruari Phippsddc661a2020-09-10 09:06:14 +010024#include <sp_debug.h>
J-Alves5aecd982020-06-11 10:25:33 +010025#include <sp_helpers.h>
Olivier Deprez6967c242021-04-09 09:24:08 +020026#include <spm_helpers.h>
J-Alves9f6f0142020-06-17 15:37:59 +010027#include <std_svc.h>
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010028
Max Shvetsov103e0562021-02-04 16:58:31 +000029#include "cactus_def.h"
30#include "cactus_tests.h"
31#include "cactus.h"
J-Alves63cdaa72020-10-08 17:22:45 +010032
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020033/* Host machine information injected by the build system in the ELF file. */
34extern const char build_message[];
35extern const char version_string[];
36
Max Shvetsov2263efb2020-11-12 17:30:11 +000037extern void secondary_cold_entry(void);
38
Manish Pandey87d4c702021-03-02 22:31:57 +000039/* Global ffa_id */
Daniel Boulbye79d2072021-03-03 11:34:53 +000040ffa_id_t g_ffa_id;
Manish Pandey87d4c702021-03-02 22:31:57 +000041
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010042/*
43 *
44 * Message loop function
45 * Notice we cannot use regular print functions because this serves to both
46 * "primary" and "secondary" VMs. Secondary VM cannot access UART directly
47 * but rather through Hafnium print hypercall.
48 *
49 */
J-Alves0e1e7ca2021-01-25 14:11:06 +000050
Daniel Boulbye79d2072021-03-03 11:34:53 +000051static void __dead2 message_loop(ffa_id_t vm_id, struct mailbox_buffers *mb)
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010052{
Daniel Boulbyce386b12022-03-29 18:36:36 +010053 struct ffa_value ffa_ret;
Daniel Boulbye79d2072021-03-03 11:34:53 +000054 ffa_id_t destination;
J-Alves1d203f12020-11-11 11:38:49 +000055
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010056 /*
J-Alves63cdaa72020-10-08 17:22:45 +010057 * This initial wait call is necessary to inform SPMD that
58 * SP initialization has completed. It blocks until receiving
59 * a direct message request.
60 */
61
J-Alves7581c382020-05-07 18:34:20 +010062 ffa_ret = ffa_msg_wait();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010063
64 for (;;) {
J-Alves6cb21d92021-01-07 15:18:12 +000065 VERBOSE("Woke up with func id: %x\n", ffa_func_id(ffa_ret));
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010066
J-Alves6cb21d92021-01-07 15:18:12 +000067 if (ffa_func_id(ffa_ret) == FFA_ERROR) {
68 ERROR("Error: %x\n", ffa_error_code(ffa_ret));
J-Alvesda6ac322020-11-09 15:45:30 +000069 break;
70 }
71
J-Alves6cb21d92021-01-07 15:18:12 +000072 if (ffa_func_id(ffa_ret) != FFA_MSG_SEND_DIRECT_REQ_SMC32 &&
Madhukar Pappireddycd183ef2021-08-05 15:34:07 -050073 ffa_func_id(ffa_ret) != FFA_MSG_SEND_DIRECT_REQ_SMC64 &&
74 ffa_func_id(ffa_ret) != FFA_INTERRUPT) {
J-Alves6cb21d92021-01-07 15:18:12 +000075 ERROR("%s(%u) unknown func id 0x%x\n",
76 __func__, vm_id, ffa_func_id(ffa_ret));
Olivier Deprez73d81cf2020-09-15 16:57:00 +020077 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010078 }
79
Madhukar Pappireddycd183ef2021-08-05 15:34:07 -050080 if (ffa_func_id(ffa_ret) == FFA_INTERRUPT) {
81 /*
82 * Received FFA_INTERRUPT in waiting state.
83 * The interrupt id is passed although this is just
84 * informational as we're running with virtual
85 * interrupts unmasked and the interrupt is processed
86 * by the interrupt handler.
87 */
88 ffa_ret = ffa_msg_wait();
89 continue;
90 }
J-Alvesda6ac322020-11-09 15:45:30 +000091
Madhukar Pappireddycd183ef2021-08-05 15:34:07 -050092 destination = ffa_dir_msg_dest(ffa_ret);
J-Alvesda6ac322020-11-09 15:45:30 +000093 if (destination != vm_id) {
J-Alves6cb21d92021-01-07 15:18:12 +000094 ERROR("%s(%u) invalid vm id 0x%x\n",
95 __func__, vm_id, destination);
Olivier Deprez73d81cf2020-09-15 16:57:00 +020096 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010097 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010098
J-Alves4cb9dee2021-03-03 13:59:52 +000099 if (!cactus_handle_cmd(&ffa_ret, &ffa_ret, mb)) {
J-Alves63cdaa72020-10-08 17:22:45 +0100100 break;
101 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100102 }
Olivier Deprez73d81cf2020-09-15 16:57:00 +0200103
104 panic();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100105}
106
107static const mmap_region_t cactus_mmap[] __attribute__((used)) = {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100108 /* PLAT_ARM_DEVICE0 area includes UART2 necessary to console */
109 MAP_REGION_FLAT(PLAT_ARM_DEVICE0_BASE, PLAT_ARM_DEVICE0_SIZE,
110 MT_DEVICE | MT_RW),
Madhukar Pappireddy172523b2020-12-31 19:25:33 -0600111 /* scratch memory allocated to be used for running SMMU tests */
112 MAP_REGION_FLAT(PLAT_CACTUS_MEMCPY_BASE, PLAT_CACTUS_MEMCPY_RANGE,
113 MT_MEMORY | MT_RW),
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100114 {0}
115};
116
Manish Pandey26c6f812020-04-30 11:43:00 +0100117static void cactus_print_memory_layout(unsigned int vm_id)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200118{
Olivier Deprezaed7f082020-11-04 15:11:59 +0100119 INFO("Secure Partition memory layout:\n");
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200120
Olivier Deprezaed7f082020-11-04 15:11:59 +0100121 INFO(" Text region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100122 (void *)CACTUS_TEXT_START, (void *)CACTUS_TEXT_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100123
Olivier Deprezaed7f082020-11-04 15:11:59 +0100124 INFO(" Read-only data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100125 (void *)CACTUS_RODATA_START, (void *)CACTUS_RODATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100126
Olivier Deprezaed7f082020-11-04 15:11:59 +0100127 INFO(" Data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100128 (void *)CACTUS_DATA_START, (void *)CACTUS_DATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100129
Olivier Deprezaed7f082020-11-04 15:11:59 +0100130 INFO(" BSS region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100131 (void *)CACTUS_BSS_START, (void *)CACTUS_BSS_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100132
Olivier Deprezaed7f082020-11-04 15:11:59 +0100133 INFO(" RX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100134 (void *)get_sp_rx_start(vm_id),
135 (void *)get_sp_rx_end(vm_id));
Manish Pandey26c6f812020-04-30 11:43:00 +0100136
Olivier Deprezaed7f082020-11-04 15:11:59 +0100137 INFO(" TX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100138 (void *)get_sp_tx_start(vm_id),
139 (void *)get_sp_tx_end(vm_id));
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200140}
141
J-Alves31d87952022-04-04 12:34:16 +0100142static void cactus_print_boot_info(struct ffa_boot_info_header *boot_info_header)
143{
144 struct ffa_boot_info_desc *boot_info_desc;
145
146 if (boot_info_header == NULL) {
147 NOTICE("SP doesn't have boot information!\n");
148 return;
149 }
150
151 VERBOSE("SP boot info:\n");
152 VERBOSE(" Signature: %x\n", boot_info_header->signature);
153 VERBOSE(" Version: %x\n", boot_info_header->version);
154 VERBOSE(" Blob Size: %u\n", boot_info_header->info_blob_size);
155 VERBOSE(" Descriptor Size: %u\n", boot_info_header->desc_size);
156 VERBOSE(" Descriptor Count: %u\n", boot_info_header->desc_count);
157
158 boot_info_desc = boot_info_header->boot_info;
159
160 if (boot_info_desc == NULL) {
161 ERROR("Boot data arguments error...\n");
162 return;
163 }
164
165 for (uint32_t i = 0; i < boot_info_header->desc_count; i++) {
166 VERBOSE(" Boot Data:\n");
167 VERBOSE(" Type: %u\n",
168 ffa_boot_info_type(&boot_info_desc[i]));
169 VERBOSE(" Type ID: %u\n",
170 ffa_boot_info_type_id(&boot_info_desc[i]));
171 VERBOSE(" Flags:\n");
172 VERBOSE(" Name Format: %x\n",
173 ffa_boot_info_name_format(&boot_info_desc[i]));
174 VERBOSE(" Content Format: %x\n",
175 ffa_boot_info_content_format(&boot_info_desc[i]));
176 VERBOSE(" Size: %u\n", boot_info_desc[i].size);
177 VERBOSE(" Value: %llx\n", boot_info_desc[i].content);
178 }
179}
180
Manish Pandey26c6f812020-04-30 11:43:00 +0100181static void cactus_plat_configure_mmu(unsigned int vm_id)
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100182{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100183 mmap_add_region(CACTUS_TEXT_START,
184 CACTUS_TEXT_START,
185 CACTUS_TEXT_END - CACTUS_TEXT_START,
186 MT_CODE);
187 mmap_add_region(CACTUS_RODATA_START,
188 CACTUS_RODATA_START,
189 CACTUS_RODATA_END - CACTUS_RODATA_START,
190 MT_RO_DATA);
191 mmap_add_region(CACTUS_DATA_START,
192 CACTUS_DATA_START,
193 CACTUS_DATA_END - CACTUS_DATA_START,
194 MT_RW_DATA);
195 mmap_add_region(CACTUS_BSS_START,
196 CACTUS_BSS_START,
197 CACTUS_BSS_END - CACTUS_BSS_START,
198 MT_RW_DATA);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100199
Max Shvetsovc32f4782020-06-23 09:41:15 +0100200 mmap_add_region(get_sp_rx_start(vm_id),
201 get_sp_rx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100202 (CACTUS_RX_TX_SIZE / 2),
203 MT_RO_DATA);
204
Max Shvetsovc32f4782020-06-23 09:41:15 +0100205 mmap_add_region(get_sp_tx_start(vm_id),
206 get_sp_tx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100207 (CACTUS_RX_TX_SIZE / 2),
208 MT_RW_DATA);
209
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100210 mmap_add(cactus_mmap);
211 init_xlat_tables();
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100212}
213
Max Shvetsov2263efb2020-11-12 17:30:11 +0000214static void register_secondary_entrypoint(void)
215{
216 smc_args args;
217
218 args.fid = FFA_SECONDARY_EP_REGISTER_SMC64;
219 args.arg1 = (u_register_t)&secondary_cold_entry;
220
221 tftf_smc(&args);
222}
223
J-Alves31d87952022-04-04 12:34:16 +0100224void __dead2 cactus_main(bool primary_cold_boot,
225 struct ffa_boot_info_header *boot_info_header)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200226{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100227 assert(IS_IN_EL1() != 0);
228
Max Shvetsovc32f4782020-06-23 09:41:15 +0100229 struct mailbox_buffers mb;
Daniel Boulbyce386b12022-03-29 18:36:36 +0100230 struct ffa_value ret;
J-Alvesda6ac322020-11-09 15:45:30 +0000231
J-Alves7581c382020-05-07 18:34:20 +0100232 /* Get current FFA id */
Daniel Boulbyce386b12022-03-29 18:36:36 +0100233 struct ffa_value ffa_id_ret = ffa_id_get();
Daniel Boulby198deda2021-03-03 11:35:25 +0000234 ffa_id_t ffa_id = ffa_endpoint_id(ffa_id_ret);
J-Alves6cb21d92021-01-07 15:18:12 +0000235 if (ffa_func_id(ffa_id_ret) != FFA_SUCCESS_SMC32) {
J-Alves7581c382020-05-07 18:34:20 +0100236 ERROR("FFA_ID_GET failed.\n");
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100237 panic();
238 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100239
Max Shvetsov2263efb2020-11-12 17:30:11 +0000240 if (primary_cold_boot == true) {
241 /* Clear BSS */
242 memset((void *)CACTUS_BSS_START,
243 0, CACTUS_BSS_END - CACTUS_BSS_START);
244
Max Shvetsov2263efb2020-11-12 17:30:11 +0000245 mb.send = (void *) get_sp_tx_start(ffa_id);
246 mb.recv = (void *) get_sp_rx_start(ffa_id);
247
248 /* Configure and enable Stage-1 MMU, enable D-Cache */
249 cactus_plat_configure_mmu(ffa_id);
Madhukar Pappireddy7caaa4a2022-01-28 17:01:35 -0600250
251 /* Initialize locks for tail end interrupt handler */
252 sp_handler_spin_lock_init();
J-Alves31d87952022-04-04 12:34:16 +0100253
254 if (boot_info_header != NULL) {
255 /*
256 * TODO: Currently just validating that cactus can
257 * access the boot info descriptors. In case we want to
258 * use the boot info contents, we should check the
259 * blob and remap if the size is bigger than one page.
260 * Only then access the contents.
261 */
262 mmap_add_dynamic_region(
263 (unsigned long long)boot_info_header,
264 (uintptr_t)boot_info_header,
265 PAGE_SIZE, MT_RO_DATA);
266 }
Max Shvetsov2263efb2020-11-12 17:30:11 +0000267 }
268
Manish Pandey87d4c702021-03-02 22:31:57 +0000269 /*
270 * The local ffa_id value is held on the stack. The global g_ffa_id
271 * value is set after BSS is cleared.
272 */
273 g_ffa_id = ffa_id;
274
Manish Pandey26c6f812020-04-30 11:43:00 +0100275 enable_mmu_el1(0);
276
Manish Pandeyd27b37d2021-03-02 14:41:58 +0000277 /* Enable IRQ/FIQ */
278 enable_irq();
279 enable_fiq();
280
Max Shvetsov2263efb2020-11-12 17:30:11 +0000281 if (primary_cold_boot == false) {
282 goto msg_loop;
283 }
284
J-Alves7581c382020-05-07 18:34:20 +0100285 if (ffa_id == SPM_VM_ID_FIRST) {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100286 console_init(CACTUS_PL011_UART_BASE,
287 CACTUS_PL011_UART_CLK_IN_HZ,
288 PL011_BAUDRATE);
Manish Pandey29495372020-04-09 15:19:26 +0100289
290 set_putc_impl(PL011_AS_STDOUT);
291
J-Alves31d87952022-04-04 12:34:16 +0100292 cactus_print_boot_info(boot_info_header);
Manish Pandey29495372020-04-09 15:19:26 +0100293 } else {
294 set_putc_impl(HVC_CALL_AS_STDOUT);
Olivier Deprez24bd1702021-10-05 14:35:17 +0200295 }
Manish Pandey29495372020-04-09 15:19:26 +0100296
Olivier Deprez24bd1702021-10-05 14:35:17 +0200297 /* Below string is monitored by CI expect script. */
298 NOTICE("Booting Secure Partition (ID: %x)\n%s\n%s\n",
299 ffa_id, build_message, version_string);
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100300
Olivier Deprez24bd1702021-10-05 14:35:17 +0200301 if (ffa_id == (SPM_VM_ID_FIRST + 2)) {
302 VERBOSE("Mapping RXTX Region\n");
303 CONFIGURE_AND_MAP_MAILBOX(mb, PAGE_SIZE, ret);
304 if (ffa_func_id(ret) != FFA_SUCCESS_SMC32) {
305 ERROR(
306 "Failed to map RXTX buffers. Error: %x\n",
307 ffa_error_code(ret));
308 panic();
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100309 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100310 }
Manish Pandey26c6f812020-04-30 11:43:00 +0100311
Manish Pandey26c6f812020-04-30 11:43:00 +0100312 cactus_print_memory_layout(ffa_id);
313
Max Shvetsov2263efb2020-11-12 17:30:11 +0000314 register_secondary_entrypoint();
315
J-Alves9f6f0142020-06-17 15:37:59 +0100316 /* Invoking Tests */
Max Shvetsovc32f4782020-06-23 09:41:15 +0100317 ffa_tests(&mb);
J-Alves9f6f0142020-06-17 15:37:59 +0100318
Max Shvetsov2263efb2020-11-12 17:30:11 +0000319msg_loop:
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100320 /* End up to message loop */
J-Alves63cdaa72020-10-08 17:22:45 +0100321 message_loop(ffa_id, &mb);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100322
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100323 /* Not reached */
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200324}