blob: da7e9139fb82ba61f009daf9b456fe44613e02a5 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Olivier Deprezafcdb7c2019-11-29 14:21:48 +01002 * Copyright (c) 2018-2020, 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>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02009
10#include "cactus.h"
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +010011#include "cactus_def.h"
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +010012#include <cactus_platform_def.h>
J-Alves9f6f0142020-06-17 15:37:59 +010013#include "cactus_tests.h"
14#include <debug.h>
15#include <drivers/arm/pl011.h>
16#include <drivers/console.h>
J-Alves5aecd982020-06-11 10:25:33 +010017#include <ffa_helpers.h>
J-Alves9f6f0142020-06-17 15:37:59 +010018#include <lib/aarch64/arch_helpers.h>
19#include <lib/xlat_tables/xlat_mmu_helpers.h>
20#include <lib/xlat_tables/xlat_tables_v2.h>
J-Alves5aecd982020-06-11 10:25:33 +010021#include <sp_helpers.h>
J-Alves9f6f0142020-06-17 15:37:59 +010022#include <std_svc.h>
23#include <plat/common/platform.h>
24#include <plat_arm.h>
25#include <platform_def.h>
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010026
J-Alves63cdaa72020-10-08 17:22:45 +010027#include <cactus_test_cmds.h>
28
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020029/* Host machine information injected by the build system in the ELF file. */
30extern const char build_message[];
31extern const char version_string[];
32
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010033/*
34 *
35 * Message loop function
36 * Notice we cannot use regular print functions because this serves to both
37 * "primary" and "secondary" VMs. Secondary VM cannot access UART directly
38 * but rather through Hafnium print hypercall.
39 *
40 */
J-Alves63cdaa72020-10-08 17:22:45 +010041static void __dead2 message_loop(ffa_vm_id_t vm_id, struct mailbox_buffers *mb)
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010042{
J-Alves7581c382020-05-07 18:34:20 +010043 smc_ret_values ffa_ret;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010044 uint32_t sp_response;
J-Alves63cdaa72020-10-08 17:22:45 +010045 ffa_vm_id_t source;
J-Alvesda6ac322020-11-09 15:45:30 +000046 ffa_vm_id_t destination;
J-Alves1d203f12020-11-11 11:38:49 +000047 uint64_t cactus_cmd;
48
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010049
50 /*
J-Alves63cdaa72020-10-08 17:22:45 +010051 * This initial wait call is necessary to inform SPMD that
52 * SP initialization has completed. It blocks until receiving
53 * a direct message request.
54 */
55
J-Alves7581c382020-05-07 18:34:20 +010056 ffa_ret = ffa_msg_wait();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010057
58 for (;;) {
J-Alves63cdaa72020-10-08 17:22:45 +010059 VERBOSE("Woke up with func id: %lx\n", ffa_ret.ret0);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010060
J-Alvesda6ac322020-11-09 15:45:30 +000061 if (ffa_ret.ret0 == FFA_ERROR) {
62 ERROR("Error: %lx\n", ffa_ret.ret2);
63 break;
64 }
65
J-Alves63cdaa72020-10-08 17:22:45 +010066 if (ffa_ret.ret0 != FFA_MSG_SEND_DIRECT_REQ_SMC32 &&
67 ffa_ret.ret0 != FFA_MSG_SEND_DIRECT_REQ_SMC64) {
Olivier Deprez73d81cf2020-09-15 16:57:00 +020068 ERROR("%s(%u) unknown func id 0x%lx\n",
69 __func__, vm_id, ffa_ret.ret0);
70 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010071 }
72
J-Alvesda6ac322020-11-09 15:45:30 +000073 destination = ffa_ret.ret1 & U(0xFFFF);
74
75 source = ffa_ret.ret1 >> 16;
76
77 if (destination != vm_id) {
Olivier Deprez73d81cf2020-09-15 16:57:00 +020078 ERROR("%s(%u) invalid vm id 0x%lx\n",
79 __func__, vm_id, ffa_ret.ret1);
80 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010081 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010082
J-Alves63cdaa72020-10-08 17:22:45 +010083 PRINT_CMD(ffa_ret);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010084
J-Alves1d203f12020-11-11 11:38:49 +000085 cactus_cmd = CACTUS_GET_CMD(ffa_ret);
86
87 switch (cactus_cmd) {
J-Alves63cdaa72020-10-08 17:22:45 +010088 case FFA_MEM_SHARE_SMC32:
89 case FFA_MEM_LEND_SMC32:
90 case FFA_MEM_DONATE_SMC32:
91 ffa_memory_management_test(
92 mb, vm_id, source,
93 CACTUS_GET_CMD(ffa_ret),
94 CACTUS_MEM_SEND_GET_HANDLE(ffa_ret));
95
96 /*
97 * If execution gets to this point means all operations
98 * with memory retrieval went well, as such replying
99 */
100 ffa_ret = CACTUS_SUCCESS_RESP(vm_id, source);
101 break;
J-Alvesda6ac322020-11-09 15:45:30 +0000102
103 case CACTUS_ECHO_CMD:
104 {
105 uint64_t echo_val = CACTUS_ECHO_GET_VAL(ffa_ret);
106
107 VERBOSE("Received echo at %x, value %llx.\n",
108 destination, echo_val);
109 ffa_ret = CACTUS_RESPONSE(vm_id, source, echo_val);
110 break;
111 }
112 case CACTUS_REQ_ECHO_CMD:
113 {
114 ffa_vm_id_t echo_dest =
115 CACTUS_REQ_ECHO_GET_ECHO_DEST(ffa_ret);
116 uint64_t echo_val = CACTUS_ECHO_GET_VAL(ffa_ret);
117 bool success = true;
118
119 VERBOSE("%x requested to send echo to %x, value %llx\n",
120 source, echo_dest, echo_val);
121
122 ffa_ret = CACTUS_ECHO_SEND_CMD(vm_id, echo_dest,
123 echo_val);
124
125 if (ffa_ret.ret0 != FFA_MSG_SEND_DIRECT_RESP_SMC32) {
126 ERROR("Failed to send message. error: %lx\n",
127 ffa_ret.ret2);
128 success = false;
129 }
130
131 if (CACTUS_GET_RESPONSE(ffa_ret) != echo_val) {
132 ERROR("Echo Failed!\n");
133 success = false;
134 }
135
136 ffa_ret = success ? CACTUS_SUCCESS_RESP(vm_id, source) :
137 CACTUS_ERROR_RESP(vm_id, source);
138 break;
139 }
J-Alves1d203f12020-11-11 11:38:49 +0000140 case CACTUS_DEADLOCK_CMD:
141 case CACTUS_REQ_DEADLOCK_CMD:
142 {
143 ffa_vm_id_t deadlock_dest =
144 CACTUS_DEADLOCK_GET_NEXT_DEST(ffa_ret);
145 ffa_vm_id_t deadlock_next_dest = source;
146
147 if (cactus_cmd == CACTUS_DEADLOCK_CMD) {
148 VERBOSE("%x is creating deadlock. next: %x\n",
149 source, deadlock_dest);
150 } else if (cactus_cmd == CACTUS_REQ_DEADLOCK_CMD) {
151 VERBOSE(
152 "%x requested deadlock with %x and %x\n",
153 source, deadlock_dest, deadlock_next_dest);
154
155 deadlock_next_dest =
156 CACTUS_DEADLOCK_GET_NEXT_DEST2(ffa_ret);
157 }
158
159 ffa_ret = CACTUS_DEADLOCK_SEND_CMD(vm_id, deadlock_dest,
160 deadlock_next_dest);
161
162 /*
163 * Should be true for the last partition to attempt
164 * an FF-A direct message, to the first partition.
165 */
166 bool is_deadlock_detected =
167 (ffa_ret.ret0 == FFA_ERROR) &&
168 (ffa_ret.ret2 == FFA_ERROR_BUSY);
169
170 /*
171 * Should be true after the deadlock has been detected
172 * and after the first response has been sent down the
173 * request chain.
174 */
175 bool is_returning_from_deadlock =
176 (ffa_ret.ret0 == FFA_MSG_SEND_DIRECT_RESP_SMC32)
177 && (CACTUS_IS_SUCCESS_RESP(ffa_ret));
178
179 if (is_deadlock_detected) {
180 NOTICE("Attempting dealock but got error %lx\n",
181 ffa_ret.ret2);
182 }
183
184 if (is_deadlock_detected ||
185 is_returning_from_deadlock) {
186 /*
187 * This is not the partition, that would have
188 * created the deadlock. As such, reply back
189 * to the partitions.
190 */
191 ffa_ret = CACTUS_SUCCESS_RESP(vm_id, source);
192 break;
193 }
194
195 /* Shouldn't get to this point */
196 ERROR("Deadlock test went wrong!\n");
197 ffa_ret = CACTUS_ERROR_RESP(vm_id, source);
198
199 break;
200 }
J-Alves63cdaa72020-10-08 17:22:45 +0100201 default:
202 /*
203 * Currently direct message test is handled here.
204 * TODO: create a case within the switch case
205 * For the sake of testing, add the vm id to the
206 * received message.
207 */
J-Alves63cdaa72020-10-08 17:22:45 +0100208 sp_response = ffa_ret.ret3 | vm_id;
Olivier Deprezaed7f082020-11-04 15:11:59 +0100209 VERBOSE("Replying with direct message response: %x\n", sp_response);
J-Alves63cdaa72020-10-08 17:22:45 +0100210 ffa_ret = ffa_msg_send_direct_resp(vm_id,
211 HYP_ID,
212 sp_response);
213
214 break;
215 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100216 }
Olivier Deprez73d81cf2020-09-15 16:57:00 +0200217
218 panic();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100219}
220
221static const mmap_region_t cactus_mmap[] __attribute__((used)) = {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100222 /* PLAT_ARM_DEVICE0 area includes UART2 necessary to console */
223 MAP_REGION_FLAT(PLAT_ARM_DEVICE0_BASE, PLAT_ARM_DEVICE0_SIZE,
224 MT_DEVICE | MT_RW),
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100225 {0}
226};
227
Manish Pandey26c6f812020-04-30 11:43:00 +0100228static void cactus_print_memory_layout(unsigned int vm_id)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200229{
Olivier Deprezaed7f082020-11-04 15:11:59 +0100230 INFO("Secure Partition memory layout:\n");
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200231
Olivier Deprezaed7f082020-11-04 15:11:59 +0100232 INFO(" Text region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100233 (void *)CACTUS_TEXT_START, (void *)CACTUS_TEXT_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100234
Olivier Deprezaed7f082020-11-04 15:11:59 +0100235 INFO(" Read-only data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100236 (void *)CACTUS_RODATA_START, (void *)CACTUS_RODATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100237
Olivier Deprezaed7f082020-11-04 15:11:59 +0100238 INFO(" Data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100239 (void *)CACTUS_DATA_START, (void *)CACTUS_DATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100240
Olivier Deprezaed7f082020-11-04 15:11:59 +0100241 INFO(" BSS region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100242 (void *)CACTUS_BSS_START, (void *)CACTUS_BSS_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100243
Olivier Deprezaed7f082020-11-04 15:11:59 +0100244 INFO(" RX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100245 (void *)get_sp_rx_start(vm_id),
246 (void *)get_sp_rx_end(vm_id));
Manish Pandey26c6f812020-04-30 11:43:00 +0100247
Olivier Deprezaed7f082020-11-04 15:11:59 +0100248 INFO(" TX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100249 (void *)get_sp_tx_start(vm_id),
250 (void *)get_sp_tx_end(vm_id));
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200251}
252
Manish Pandey26c6f812020-04-30 11:43:00 +0100253static void cactus_plat_configure_mmu(unsigned int vm_id)
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100254{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100255 mmap_add_region(CACTUS_TEXT_START,
256 CACTUS_TEXT_START,
257 CACTUS_TEXT_END - CACTUS_TEXT_START,
258 MT_CODE);
259 mmap_add_region(CACTUS_RODATA_START,
260 CACTUS_RODATA_START,
261 CACTUS_RODATA_END - CACTUS_RODATA_START,
262 MT_RO_DATA);
263 mmap_add_region(CACTUS_DATA_START,
264 CACTUS_DATA_START,
265 CACTUS_DATA_END - CACTUS_DATA_START,
266 MT_RW_DATA);
267 mmap_add_region(CACTUS_BSS_START,
268 CACTUS_BSS_START,
269 CACTUS_BSS_END - CACTUS_BSS_START,
270 MT_RW_DATA);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100271
Max Shvetsovc32f4782020-06-23 09:41:15 +0100272 mmap_add_region(get_sp_rx_start(vm_id),
273 get_sp_rx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100274 (CACTUS_RX_TX_SIZE / 2),
275 MT_RO_DATA);
276
Max Shvetsovc32f4782020-06-23 09:41:15 +0100277 mmap_add_region(get_sp_tx_start(vm_id),
278 get_sp_tx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100279 (CACTUS_RX_TX_SIZE / 2),
280 MT_RW_DATA);
281
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100282 mmap_add(cactus_mmap);
283 init_xlat_tables();
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100284}
285
Olivier Deprez458d5532020-06-09 17:56:20 +0200286int tftf_irq_handler_dispatcher(void)
287{
288 ERROR("%s\n", __func__);
289
290 return 0;
291}
292
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100293void __dead2 cactus_main(void)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200294{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100295 assert(IS_IN_EL1() != 0);
296
Max Shvetsovc32f4782020-06-23 09:41:15 +0100297 struct mailbox_buffers mb;
J-Alvesda6ac322020-11-09 15:45:30 +0000298
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100299 /* Clear BSS */
300 memset((void *)CACTUS_BSS_START,
301 0, CACTUS_BSS_END - CACTUS_BSS_START);
302
J-Alves7581c382020-05-07 18:34:20 +0100303 /* Get current FFA id */
304 smc_ret_values ffa_id_ret = ffa_id_get();
305 if (ffa_id_ret.ret0 != FFA_SUCCESS_SMC32) {
306 ERROR("FFA_ID_GET failed.\n");
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100307 panic();
308 }
309
J-Alves7581c382020-05-07 18:34:20 +0100310 ffa_vm_id_t ffa_id = ffa_id_ret.ret2 & 0xffff;
Max Shvetsovc32f4782020-06-23 09:41:15 +0100311 mb.send = (void *) get_sp_tx_start(ffa_id);
312 mb.recv = (void *) get_sp_rx_start(ffa_id);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100313
Manish Pandey26c6f812020-04-30 11:43:00 +0100314 /* Configure and enable Stage-1 MMU, enable D-Cache */
315 cactus_plat_configure_mmu(ffa_id);
316 enable_mmu_el1(0);
317
J-Alves7581c382020-05-07 18:34:20 +0100318 if (ffa_id == SPM_VM_ID_FIRST) {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100319 console_init(CACTUS_PL011_UART_BASE,
320 CACTUS_PL011_UART_CLK_IN_HZ,
321 PL011_BAUDRATE);
Manish Pandey29495372020-04-09 15:19:26 +0100322
323 set_putc_impl(PL011_AS_STDOUT);
324
325 NOTICE("Booting Primary Cactus Secure Partition\n%s\n%s\n",
326 build_message, version_string);
Manish Pandey29495372020-04-09 15:19:26 +0100327 } else {
J-Alves83ede9b2020-11-02 17:37:19 +0000328 smc_ret_values ret;
Manish Pandey29495372020-04-09 15:19:26 +0100329 set_putc_impl(HVC_CALL_AS_STDOUT);
330
Olivier Deprezaed7f082020-11-04 15:11:59 +0100331 NOTICE("Booting Secondary Cactus Secure Partition (ID: %x)\n%s\n%s\n",
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100332 ffa_id, build_message, version_string);
333
Olivier Deprez0be4abe2020-08-04 11:26:13 +0200334 if (ffa_id == (SPM_VM_ID_FIRST + 2)) {
Olivier Deprezaed7f082020-11-04 15:11:59 +0100335 VERBOSE("Mapping RXTX Region\n");
J-Alves83ede9b2020-11-02 17:37:19 +0000336 CONFIGURE_AND_MAP_MAILBOX(mb, PAGE_SIZE, ret);
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100337 if (ret.ret0 != FFA_SUCCESS_SMC32) {
J-Alves83ede9b2020-11-02 17:37:19 +0000338 ERROR(
339 "Failed to map RXTX buffers. Error: %lx\n",
340 ret.ret2);
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100341 panic();
342 }
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100343 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100344 }
Manish Pandey26c6f812020-04-30 11:43:00 +0100345
Olivier Deprezaed7f082020-11-04 15:11:59 +0100346 INFO("FF-A id: %x\n", ffa_id);
Manish Pandey26c6f812020-04-30 11:43:00 +0100347 cactus_print_memory_layout(ffa_id);
348
J-Alves9f6f0142020-06-17 15:37:59 +0100349 /* Invoking Tests */
Max Shvetsovc32f4782020-06-23 09:41:15 +0100350 ffa_tests(&mb);
J-Alves9f6f0142020-06-17 15:37:59 +0100351
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100352 /* End up to message loop */
J-Alves63cdaa72020-10-08 17:22:45 +0100353 message_loop(ffa_id, &mb);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100354
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100355 /* Not reached */
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200356}