blob: 7900e7f39c99584e911ebd1eba7eb8b20e93027e [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;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010047
48 /*
J-Alves63cdaa72020-10-08 17:22:45 +010049 * This initial wait call is necessary to inform SPMD that
50 * SP initialization has completed. It blocks until receiving
51 * a direct message request.
52 */
53
J-Alves7581c382020-05-07 18:34:20 +010054 ffa_ret = ffa_msg_wait();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010055
56 for (;;) {
J-Alves63cdaa72020-10-08 17:22:45 +010057 VERBOSE("Woke up with func id: %lx\n", ffa_ret.ret0);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010058
J-Alvesda6ac322020-11-09 15:45:30 +000059 if (ffa_ret.ret0 == FFA_ERROR) {
60 ERROR("Error: %lx\n", ffa_ret.ret2);
61 break;
62 }
63
J-Alves63cdaa72020-10-08 17:22:45 +010064 if (ffa_ret.ret0 != FFA_MSG_SEND_DIRECT_REQ_SMC32 &&
65 ffa_ret.ret0 != FFA_MSG_SEND_DIRECT_REQ_SMC64) {
Olivier Deprez73d81cf2020-09-15 16:57:00 +020066 ERROR("%s(%u) unknown func id 0x%lx\n",
67 __func__, vm_id, ffa_ret.ret0);
68 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010069 }
70
J-Alvesda6ac322020-11-09 15:45:30 +000071 destination = ffa_ret.ret1 & U(0xFFFF);
72
73 source = ffa_ret.ret1 >> 16;
74
75 if (destination != vm_id) {
Olivier Deprez73d81cf2020-09-15 16:57:00 +020076 ERROR("%s(%u) invalid vm id 0x%lx\n",
77 __func__, vm_id, ffa_ret.ret1);
78 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010079 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010080
J-Alves63cdaa72020-10-08 17:22:45 +010081 PRINT_CMD(ffa_ret);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010082
J-Alves63cdaa72020-10-08 17:22:45 +010083 switch (CACTUS_GET_CMD(ffa_ret)) {
84 case FFA_MEM_SHARE_SMC32:
85 case FFA_MEM_LEND_SMC32:
86 case FFA_MEM_DONATE_SMC32:
87 ffa_memory_management_test(
88 mb, vm_id, source,
89 CACTUS_GET_CMD(ffa_ret),
90 CACTUS_MEM_SEND_GET_HANDLE(ffa_ret));
91
92 /*
93 * If execution gets to this point means all operations
94 * with memory retrieval went well, as such replying
95 */
96 ffa_ret = CACTUS_SUCCESS_RESP(vm_id, source);
97 break;
J-Alvesda6ac322020-11-09 15:45:30 +000098
99 case CACTUS_ECHO_CMD:
100 {
101 uint64_t echo_val = CACTUS_ECHO_GET_VAL(ffa_ret);
102
103 VERBOSE("Received echo at %x, value %llx.\n",
104 destination, echo_val);
105 ffa_ret = CACTUS_RESPONSE(vm_id, source, echo_val);
106 break;
107 }
108 case CACTUS_REQ_ECHO_CMD:
109 {
110 ffa_vm_id_t echo_dest =
111 CACTUS_REQ_ECHO_GET_ECHO_DEST(ffa_ret);
112 uint64_t echo_val = CACTUS_ECHO_GET_VAL(ffa_ret);
113 bool success = true;
114
115 VERBOSE("%x requested to send echo to %x, value %llx\n",
116 source, echo_dest, echo_val);
117
118 ffa_ret = CACTUS_ECHO_SEND_CMD(vm_id, echo_dest,
119 echo_val);
120
121 if (ffa_ret.ret0 != FFA_MSG_SEND_DIRECT_RESP_SMC32) {
122 ERROR("Failed to send message. error: %lx\n",
123 ffa_ret.ret2);
124 success = false;
125 }
126
127 if (CACTUS_GET_RESPONSE(ffa_ret) != echo_val) {
128 ERROR("Echo Failed!\n");
129 success = false;
130 }
131
132 ffa_ret = success ? CACTUS_SUCCESS_RESP(vm_id, source) :
133 CACTUS_ERROR_RESP(vm_id, source);
134 break;
135 }
J-Alves63cdaa72020-10-08 17:22:45 +0100136 default:
137 /*
138 * Currently direct message test is handled here.
139 * TODO: create a case within the switch case
140 * For the sake of testing, add the vm id to the
141 * received message.
142 */
J-Alves63cdaa72020-10-08 17:22:45 +0100143 sp_response = ffa_ret.ret3 | vm_id;
Olivier Deprezaed7f082020-11-04 15:11:59 +0100144 VERBOSE("Replying with direct message response: %x\n", sp_response);
J-Alves63cdaa72020-10-08 17:22:45 +0100145 ffa_ret = ffa_msg_send_direct_resp(vm_id,
146 HYP_ID,
147 sp_response);
148
149 break;
150 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100151 }
Olivier Deprez73d81cf2020-09-15 16:57:00 +0200152
153 panic();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100154}
155
156static const mmap_region_t cactus_mmap[] __attribute__((used)) = {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100157 /* PLAT_ARM_DEVICE0 area includes UART2 necessary to console */
158 MAP_REGION_FLAT(PLAT_ARM_DEVICE0_BASE, PLAT_ARM_DEVICE0_SIZE,
159 MT_DEVICE | MT_RW),
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100160 {0}
161};
162
Manish Pandey26c6f812020-04-30 11:43:00 +0100163static void cactus_print_memory_layout(unsigned int vm_id)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200164{
Olivier Deprezaed7f082020-11-04 15:11:59 +0100165 INFO("Secure Partition memory layout:\n");
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200166
Olivier Deprezaed7f082020-11-04 15:11:59 +0100167 INFO(" Text region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100168 (void *)CACTUS_TEXT_START, (void *)CACTUS_TEXT_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100169
Olivier Deprezaed7f082020-11-04 15:11:59 +0100170 INFO(" Read-only data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100171 (void *)CACTUS_RODATA_START, (void *)CACTUS_RODATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100172
Olivier Deprezaed7f082020-11-04 15:11:59 +0100173 INFO(" Data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100174 (void *)CACTUS_DATA_START, (void *)CACTUS_DATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100175
Olivier Deprezaed7f082020-11-04 15:11:59 +0100176 INFO(" BSS region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100177 (void *)CACTUS_BSS_START, (void *)CACTUS_BSS_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100178
Olivier Deprezaed7f082020-11-04 15:11:59 +0100179 INFO(" RX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100180 (void *)get_sp_rx_start(vm_id),
181 (void *)get_sp_rx_end(vm_id));
Manish Pandey26c6f812020-04-30 11:43:00 +0100182
Olivier Deprezaed7f082020-11-04 15:11:59 +0100183 INFO(" TX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100184 (void *)get_sp_tx_start(vm_id),
185 (void *)get_sp_tx_end(vm_id));
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200186}
187
Manish Pandey26c6f812020-04-30 11:43:00 +0100188static void cactus_plat_configure_mmu(unsigned int vm_id)
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100189{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100190 mmap_add_region(CACTUS_TEXT_START,
191 CACTUS_TEXT_START,
192 CACTUS_TEXT_END - CACTUS_TEXT_START,
193 MT_CODE);
194 mmap_add_region(CACTUS_RODATA_START,
195 CACTUS_RODATA_START,
196 CACTUS_RODATA_END - CACTUS_RODATA_START,
197 MT_RO_DATA);
198 mmap_add_region(CACTUS_DATA_START,
199 CACTUS_DATA_START,
200 CACTUS_DATA_END - CACTUS_DATA_START,
201 MT_RW_DATA);
202 mmap_add_region(CACTUS_BSS_START,
203 CACTUS_BSS_START,
204 CACTUS_BSS_END - CACTUS_BSS_START,
205 MT_RW_DATA);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100206
Max Shvetsovc32f4782020-06-23 09:41:15 +0100207 mmap_add_region(get_sp_rx_start(vm_id),
208 get_sp_rx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100209 (CACTUS_RX_TX_SIZE / 2),
210 MT_RO_DATA);
211
Max Shvetsovc32f4782020-06-23 09:41:15 +0100212 mmap_add_region(get_sp_tx_start(vm_id),
213 get_sp_tx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100214 (CACTUS_RX_TX_SIZE / 2),
215 MT_RW_DATA);
216
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100217 mmap_add(cactus_mmap);
218 init_xlat_tables();
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100219}
220
Olivier Deprez458d5532020-06-09 17:56:20 +0200221int tftf_irq_handler_dispatcher(void)
222{
223 ERROR("%s\n", __func__);
224
225 return 0;
226}
227
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100228void __dead2 cactus_main(void)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200229{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100230 assert(IS_IN_EL1() != 0);
231
Max Shvetsovc32f4782020-06-23 09:41:15 +0100232 struct mailbox_buffers mb;
J-Alvesda6ac322020-11-09 15:45:30 +0000233
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100234 /* Clear BSS */
235 memset((void *)CACTUS_BSS_START,
236 0, CACTUS_BSS_END - CACTUS_BSS_START);
237
J-Alves7581c382020-05-07 18:34:20 +0100238 /* Get current FFA id */
239 smc_ret_values ffa_id_ret = ffa_id_get();
240 if (ffa_id_ret.ret0 != FFA_SUCCESS_SMC32) {
241 ERROR("FFA_ID_GET failed.\n");
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100242 panic();
243 }
244
J-Alves7581c382020-05-07 18:34:20 +0100245 ffa_vm_id_t ffa_id = ffa_id_ret.ret2 & 0xffff;
Max Shvetsovc32f4782020-06-23 09:41:15 +0100246 mb.send = (void *) get_sp_tx_start(ffa_id);
247 mb.recv = (void *) get_sp_rx_start(ffa_id);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100248
Manish Pandey26c6f812020-04-30 11:43:00 +0100249 /* Configure and enable Stage-1 MMU, enable D-Cache */
250 cactus_plat_configure_mmu(ffa_id);
251 enable_mmu_el1(0);
252
J-Alves7581c382020-05-07 18:34:20 +0100253 if (ffa_id == SPM_VM_ID_FIRST) {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100254 console_init(CACTUS_PL011_UART_BASE,
255 CACTUS_PL011_UART_CLK_IN_HZ,
256 PL011_BAUDRATE);
Manish Pandey29495372020-04-09 15:19:26 +0100257
258 set_putc_impl(PL011_AS_STDOUT);
259
260 NOTICE("Booting Primary Cactus Secure Partition\n%s\n%s\n",
261 build_message, version_string);
Manish Pandey29495372020-04-09 15:19:26 +0100262 } else {
J-Alves83ede9b2020-11-02 17:37:19 +0000263 smc_ret_values ret;
Manish Pandey29495372020-04-09 15:19:26 +0100264 set_putc_impl(HVC_CALL_AS_STDOUT);
265
Olivier Deprezaed7f082020-11-04 15:11:59 +0100266 NOTICE("Booting Secondary Cactus Secure Partition (ID: %x)\n%s\n%s\n",
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100267 ffa_id, build_message, version_string);
268
Olivier Deprez0be4abe2020-08-04 11:26:13 +0200269 if (ffa_id == (SPM_VM_ID_FIRST + 2)) {
Olivier Deprezaed7f082020-11-04 15:11:59 +0100270 VERBOSE("Mapping RXTX Region\n");
J-Alves83ede9b2020-11-02 17:37:19 +0000271 CONFIGURE_AND_MAP_MAILBOX(mb, PAGE_SIZE, ret);
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100272 if (ret.ret0 != FFA_SUCCESS_SMC32) {
J-Alves83ede9b2020-11-02 17:37:19 +0000273 ERROR(
274 "Failed to map RXTX buffers. Error: %lx\n",
275 ret.ret2);
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100276 panic();
277 }
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100278 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100279 }
Manish Pandey26c6f812020-04-30 11:43:00 +0100280
Olivier Deprezaed7f082020-11-04 15:11:59 +0100281 INFO("FF-A id: %x\n", ffa_id);
Manish Pandey26c6f812020-04-30 11:43:00 +0100282 cactus_print_memory_layout(ffa_id);
283
J-Alves9f6f0142020-06-17 15:37:59 +0100284 /* Invoking Tests */
Max Shvetsovc32f4782020-06-23 09:41:15 +0100285 ffa_tests(&mb);
J-Alves9f6f0142020-06-17 15:37:59 +0100286
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100287 /* End up to message loop */
J-Alves63cdaa72020-10-08 17:22:45 +0100288 message_loop(ffa_id, &mb);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100289
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100290 /* Not reached */
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200291}