blob: 7c70d67b2ed53bfb9e27213137671cd577be5354 [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;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010046
47 /*
J-Alves63cdaa72020-10-08 17:22:45 +010048 * This initial wait call is necessary to inform SPMD that
49 * SP initialization has completed. It blocks until receiving
50 * a direct message request.
51 */
52
J-Alves7581c382020-05-07 18:34:20 +010053 ffa_ret = ffa_msg_wait();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010054
55 for (;;) {
J-Alves63cdaa72020-10-08 17:22:45 +010056 VERBOSE("Woke up with func id: %lx\n", ffa_ret.ret0);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010057
J-Alves63cdaa72020-10-08 17:22:45 +010058 if (ffa_ret.ret0 != FFA_MSG_SEND_DIRECT_REQ_SMC32 &&
59 ffa_ret.ret0 != FFA_MSG_SEND_DIRECT_REQ_SMC64) {
Olivier Deprez73d81cf2020-09-15 16:57:00 +020060 ERROR("%s(%u) unknown func id 0x%lx\n",
61 __func__, vm_id, ffa_ret.ret0);
62 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010063 }
64
Olivier Deprez0be4abe2020-08-04 11:26:13 +020065 if (ffa_ret.ret1 != vm_id) {
Olivier Deprez73d81cf2020-09-15 16:57:00 +020066 ERROR("%s(%u) invalid vm id 0x%lx\n",
67 __func__, vm_id, ffa_ret.ret1);
68 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010069 }
J-Alves63cdaa72020-10-08 17:22:45 +010070 source = ffa_ret.ret2;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010071
J-Alves63cdaa72020-10-08 17:22:45 +010072 if (source != HYP_ID) {
Olivier Deprez73d81cf2020-09-15 16:57:00 +020073 ERROR("%s(%u) invalid hyp id 0x%lx\n",
74 __func__, vm_id, ffa_ret.ret2);
75 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010076 }
77
J-Alves63cdaa72020-10-08 17:22:45 +010078 PRINT_CMD(ffa_ret);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010079
J-Alves63cdaa72020-10-08 17:22:45 +010080 switch (CACTUS_GET_CMD(ffa_ret)) {
81 case FFA_MEM_SHARE_SMC32:
82 case FFA_MEM_LEND_SMC32:
83 case FFA_MEM_DONATE_SMC32:
84 ffa_memory_management_test(
85 mb, vm_id, source,
86 CACTUS_GET_CMD(ffa_ret),
87 CACTUS_MEM_SEND_GET_HANDLE(ffa_ret));
88
89 /*
90 * If execution gets to this point means all operations
91 * with memory retrieval went well, as such replying
92 */
93 ffa_ret = CACTUS_SUCCESS_RESP(vm_id, source);
94 break;
95 default:
96 /*
97 * Currently direct message test is handled here.
98 * TODO: create a case within the switch case
99 * For the sake of testing, add the vm id to the
100 * received message.
101 */
102 NOTICE("Replying to Direct MSG test\n");
103 sp_response = ffa_ret.ret3 | vm_id;
104 ffa_ret = ffa_msg_send_direct_resp(vm_id,
105 HYP_ID,
106 sp_response);
107
108 break;
109 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100110 }
Olivier Deprez73d81cf2020-09-15 16:57:00 +0200111
112 panic();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100113}
114
115static const mmap_region_t cactus_mmap[] __attribute__((used)) = {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100116 /* PLAT_ARM_DEVICE0 area includes UART2 necessary to console */
117 MAP_REGION_FLAT(PLAT_ARM_DEVICE0_BASE, PLAT_ARM_DEVICE0_SIZE,
118 MT_DEVICE | MT_RW),
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100119 {0}
120};
121
Manish Pandey26c6f812020-04-30 11:43:00 +0100122static void cactus_print_memory_layout(unsigned int vm_id)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200123{
124 NOTICE("Secure Partition memory layout:\n");
125
Manish Pandey26c6f812020-04-30 11:43:00 +0100126 NOTICE(" Text region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100127 (void *)CACTUS_TEXT_START, (void *)CACTUS_TEXT_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100128
129 NOTICE(" Read-only data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100130 (void *)CACTUS_RODATA_START, (void *)CACTUS_RODATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100131
132 NOTICE(" Data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100133 (void *)CACTUS_DATA_START, (void *)CACTUS_DATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100134
135 NOTICE(" BSS region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100136 (void *)CACTUS_BSS_START, (void *)CACTUS_BSS_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100137
138 NOTICE(" RX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100139 (void *)get_sp_rx_start(vm_id),
140 (void *)get_sp_rx_end(vm_id));
Manish Pandey26c6f812020-04-30 11:43:00 +0100141
142 NOTICE(" TX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100143 (void *)get_sp_tx_start(vm_id),
144 (void *)get_sp_tx_end(vm_id));
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200145}
146
Manish Pandey26c6f812020-04-30 11:43:00 +0100147static void cactus_plat_configure_mmu(unsigned int vm_id)
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100148{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100149 mmap_add_region(CACTUS_TEXT_START,
150 CACTUS_TEXT_START,
151 CACTUS_TEXT_END - CACTUS_TEXT_START,
152 MT_CODE);
153 mmap_add_region(CACTUS_RODATA_START,
154 CACTUS_RODATA_START,
155 CACTUS_RODATA_END - CACTUS_RODATA_START,
156 MT_RO_DATA);
157 mmap_add_region(CACTUS_DATA_START,
158 CACTUS_DATA_START,
159 CACTUS_DATA_END - CACTUS_DATA_START,
160 MT_RW_DATA);
161 mmap_add_region(CACTUS_BSS_START,
162 CACTUS_BSS_START,
163 CACTUS_BSS_END - CACTUS_BSS_START,
164 MT_RW_DATA);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100165
Max Shvetsovc32f4782020-06-23 09:41:15 +0100166 mmap_add_region(get_sp_rx_start(vm_id),
167 get_sp_rx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100168 (CACTUS_RX_TX_SIZE / 2),
169 MT_RO_DATA);
170
Max Shvetsovc32f4782020-06-23 09:41:15 +0100171 mmap_add_region(get_sp_tx_start(vm_id),
172 get_sp_tx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100173 (CACTUS_RX_TX_SIZE / 2),
174 MT_RW_DATA);
175
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100176 mmap_add(cactus_mmap);
177 init_xlat_tables();
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100178}
179
Olivier Deprez458d5532020-06-09 17:56:20 +0200180int tftf_irq_handler_dispatcher(void)
181{
182 ERROR("%s\n", __func__);
183
184 return 0;
185}
186
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100187void __dead2 cactus_main(void)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200188{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100189 assert(IS_IN_EL1() != 0);
190
Max Shvetsovc32f4782020-06-23 09:41:15 +0100191 struct mailbox_buffers mb;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100192 /* Clear BSS */
193 memset((void *)CACTUS_BSS_START,
194 0, CACTUS_BSS_END - CACTUS_BSS_START);
195
J-Alves7581c382020-05-07 18:34:20 +0100196 /* Get current FFA id */
197 smc_ret_values ffa_id_ret = ffa_id_get();
198 if (ffa_id_ret.ret0 != FFA_SUCCESS_SMC32) {
199 ERROR("FFA_ID_GET failed.\n");
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100200 panic();
201 }
202
J-Alves7581c382020-05-07 18:34:20 +0100203 ffa_vm_id_t ffa_id = ffa_id_ret.ret2 & 0xffff;
Max Shvetsovc32f4782020-06-23 09:41:15 +0100204 mb.send = (void *) get_sp_tx_start(ffa_id);
205 mb.recv = (void *) get_sp_rx_start(ffa_id);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100206
Manish Pandey26c6f812020-04-30 11:43:00 +0100207 /* Configure and enable Stage-1 MMU, enable D-Cache */
208 cactus_plat_configure_mmu(ffa_id);
209 enable_mmu_el1(0);
210
J-Alves7581c382020-05-07 18:34:20 +0100211 if (ffa_id == SPM_VM_ID_FIRST) {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100212 console_init(CACTUS_PL011_UART_BASE,
213 CACTUS_PL011_UART_CLK_IN_HZ,
214 PL011_BAUDRATE);
Manish Pandey29495372020-04-09 15:19:26 +0100215
216 set_putc_impl(PL011_AS_STDOUT);
217
218 NOTICE("Booting Primary Cactus Secure Partition\n%s\n%s\n",
219 build_message, version_string);
Manish Pandey29495372020-04-09 15:19:26 +0100220 } else {
221 set_putc_impl(HVC_CALL_AS_STDOUT);
222
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100223 NOTICE("Booting Secondary Cactus Secure Partition (ID: %u)\n%s\n%s\n",
224 ffa_id, build_message, version_string);
225
Olivier Deprez0be4abe2020-08-04 11:26:13 +0200226 if (ffa_id == (SPM_VM_ID_FIRST + 2)) {
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100227 NOTICE("Mapping RXTX Region\n");
228
229 /* Declare RX/TX buffers at virtual FF-A instance */
230 static struct {
231 uint8_t rx[PAGE_SIZE];
232 uint8_t tx[PAGE_SIZE];
233 } __aligned(PAGE_SIZE) ffa_buffers;
234
235 /* Map RX/TX buffers */
236 smc_ret_values ret = ffa_rxtx_map((uintptr_t) &ffa_buffers.tx,
237 (uintptr_t) &ffa_buffers.rx,
238 sizeof(ffa_buffers.rx) / PAGE_SIZE);
239
240 if (ret.ret0 != FFA_SUCCESS_SMC32) {
241 ERROR("ffa_rxtx_map error (%lu)\n", ret.ret2);
242 panic();
243 }
244
245 /* Update mailbox with RX/TX buffer */
246 mb.send = (void *) &ffa_buffers.tx;
247 mb.recv = (void *) &ffa_buffers.rx;
248 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100249 }
Manish Pandey26c6f812020-04-30 11:43:00 +0100250
251 NOTICE("FFA id: %u\n", ffa_id);
252 cactus_print_memory_layout(ffa_id);
253
J-Alves9f6f0142020-06-17 15:37:59 +0100254 /* Invoking Tests */
Max Shvetsovc32f4782020-06-23 09:41:15 +0100255 ffa_tests(&mb);
J-Alves9f6f0142020-06-17 15:37:59 +0100256
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100257 /* End up to message loop */
J-Alves63cdaa72020-10-08 17:22:45 +0100258 message_loop(ffa_id, &mb);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100259
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100260 /* Not reached */
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200261}