blob: acbe2af65e40d443f3276f13ab000cac77aae25a [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
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020027/* Host machine information injected by the build system in the ELF file. */
28extern const char build_message[];
29extern const char version_string[];
30
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010031/*
32 *
33 * Message loop function
34 * Notice we cannot use regular print functions because this serves to both
35 * "primary" and "secondary" VMs. Secondary VM cannot access UART directly
36 * but rather through Hafnium print hypercall.
37 *
38 */
J-Alves7581c382020-05-07 18:34:20 +010039static void __dead2 message_loop(ffa_vm_id_t vm_id)
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010040{
J-Alves7581c382020-05-07 18:34:20 +010041 smc_ret_values ffa_ret;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010042 uint32_t sp_response;
43
44 /*
45 * This initial wait call is necessary to inform SPMD that
46 * SP initialization has completed. It blocks until receiving
47 * a direct message request.
48 */
J-Alves7581c382020-05-07 18:34:20 +010049 ffa_ret = ffa_msg_wait();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010050
51 for (;;) {
52
J-Alves7581c382020-05-07 18:34:20 +010053 if (ffa_ret.ret0 != FFA_MSG_SEND_DIRECT_REQ_SMC32) {
Olivier Deprez73d81cf2020-09-15 16:57:00 +020054 ERROR("%s(%u) unknown func id 0x%lx\n",
55 __func__, vm_id, ffa_ret.ret0);
56 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010057 }
58
Olivier Deprez0be4abe2020-08-04 11:26:13 +020059 if (ffa_ret.ret1 != vm_id) {
Olivier Deprez73d81cf2020-09-15 16:57:00 +020060 ERROR("%s(%u) invalid vm id 0x%lx\n",
61 __func__, vm_id, ffa_ret.ret1);
62 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010063 }
64
J-Alves7581c382020-05-07 18:34:20 +010065 if (ffa_ret.ret2 != HYP_ID) {
Olivier Deprez73d81cf2020-09-15 16:57:00 +020066 ERROR("%s(%u) invalid hyp id 0x%lx\n",
67 __func__, vm_id, ffa_ret.ret2);
68 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010069 }
70
71 /*
72 * For the sake of testing, add the vm id to the
73 * received message.
74 */
J-Alves7581c382020-05-07 18:34:20 +010075 sp_response = ffa_ret.ret3 | vm_id;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010076
77 /*
78 * Send a response through direct messaging then block
79 * until receiving a new message request.
80 */
Olivier Deprez0be4abe2020-08-04 11:26:13 +020081 ffa_ret = ffa_msg_send_direct_resp(vm_id, HYP_ID, sp_response);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010082 }
Olivier Deprez73d81cf2020-09-15 16:57:00 +020083
84 panic();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010085}
86
87static const mmap_region_t cactus_mmap[] __attribute__((used)) = {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +010088 /* PLAT_ARM_DEVICE0 area includes UART2 necessary to console */
89 MAP_REGION_FLAT(PLAT_ARM_DEVICE0_BASE, PLAT_ARM_DEVICE0_SIZE,
90 MT_DEVICE | MT_RW),
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010091 {0}
92};
93
Manish Pandey26c6f812020-04-30 11:43:00 +010094static void cactus_print_memory_layout(unsigned int vm_id)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020095{
96 NOTICE("Secure Partition memory layout:\n");
97
Manish Pandey26c6f812020-04-30 11:43:00 +010098 NOTICE(" Text region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +010099 (void *)CACTUS_TEXT_START, (void *)CACTUS_TEXT_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100100
101 NOTICE(" Read-only data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100102 (void *)CACTUS_RODATA_START, (void *)CACTUS_RODATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100103
104 NOTICE(" Data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100105 (void *)CACTUS_DATA_START, (void *)CACTUS_DATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100106
107 NOTICE(" BSS region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100108 (void *)CACTUS_BSS_START, (void *)CACTUS_BSS_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100109
110 NOTICE(" RX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100111 (void *)get_sp_rx_start(vm_id),
112 (void *)get_sp_rx_end(vm_id));
Manish Pandey26c6f812020-04-30 11:43:00 +0100113
114 NOTICE(" TX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100115 (void *)get_sp_tx_start(vm_id),
116 (void *)get_sp_tx_end(vm_id));
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200117}
118
Manish Pandey26c6f812020-04-30 11:43:00 +0100119static void cactus_plat_configure_mmu(unsigned int vm_id)
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100120{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100121 mmap_add_region(CACTUS_TEXT_START,
122 CACTUS_TEXT_START,
123 CACTUS_TEXT_END - CACTUS_TEXT_START,
124 MT_CODE);
125 mmap_add_region(CACTUS_RODATA_START,
126 CACTUS_RODATA_START,
127 CACTUS_RODATA_END - CACTUS_RODATA_START,
128 MT_RO_DATA);
129 mmap_add_region(CACTUS_DATA_START,
130 CACTUS_DATA_START,
131 CACTUS_DATA_END - CACTUS_DATA_START,
132 MT_RW_DATA);
133 mmap_add_region(CACTUS_BSS_START,
134 CACTUS_BSS_START,
135 CACTUS_BSS_END - CACTUS_BSS_START,
136 MT_RW_DATA);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100137
Max Shvetsovc32f4782020-06-23 09:41:15 +0100138 mmap_add_region(get_sp_rx_start(vm_id),
139 get_sp_rx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100140 (CACTUS_RX_TX_SIZE / 2),
141 MT_RO_DATA);
142
Max Shvetsovc32f4782020-06-23 09:41:15 +0100143 mmap_add_region(get_sp_tx_start(vm_id),
144 get_sp_tx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100145 (CACTUS_RX_TX_SIZE / 2),
146 MT_RW_DATA);
147
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100148 mmap_add(cactus_mmap);
149 init_xlat_tables();
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100150}
151
Olivier Deprez458d5532020-06-09 17:56:20 +0200152int tftf_irq_handler_dispatcher(void)
153{
154 ERROR("%s\n", __func__);
155
156 return 0;
157}
158
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100159void __dead2 cactus_main(void)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200160{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100161 assert(IS_IN_EL1() != 0);
162
Max Shvetsovc32f4782020-06-23 09:41:15 +0100163 struct mailbox_buffers mb;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100164 /* Clear BSS */
165 memset((void *)CACTUS_BSS_START,
166 0, CACTUS_BSS_END - CACTUS_BSS_START);
167
J-Alves7581c382020-05-07 18:34:20 +0100168 /* Get current FFA id */
169 smc_ret_values ffa_id_ret = ffa_id_get();
170 if (ffa_id_ret.ret0 != FFA_SUCCESS_SMC32) {
171 ERROR("FFA_ID_GET failed.\n");
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100172 panic();
173 }
174
J-Alves7581c382020-05-07 18:34:20 +0100175 ffa_vm_id_t ffa_id = ffa_id_ret.ret2 & 0xffff;
Max Shvetsovc32f4782020-06-23 09:41:15 +0100176 mb.send = (void *) get_sp_tx_start(ffa_id);
177 mb.recv = (void *) get_sp_rx_start(ffa_id);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100178
Manish Pandey26c6f812020-04-30 11:43:00 +0100179 /* Configure and enable Stage-1 MMU, enable D-Cache */
180 cactus_plat_configure_mmu(ffa_id);
181 enable_mmu_el1(0);
182
J-Alves7581c382020-05-07 18:34:20 +0100183 if (ffa_id == SPM_VM_ID_FIRST) {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100184 console_init(CACTUS_PL011_UART_BASE,
185 CACTUS_PL011_UART_CLK_IN_HZ,
186 PL011_BAUDRATE);
Manish Pandey29495372020-04-09 15:19:26 +0100187
188 set_putc_impl(PL011_AS_STDOUT);
189
190 NOTICE("Booting Primary Cactus Secure Partition\n%s\n%s\n",
191 build_message, version_string);
Manish Pandey29495372020-04-09 15:19:26 +0100192 } else {
193 set_putc_impl(HVC_CALL_AS_STDOUT);
194
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100195 NOTICE("Booting Secondary Cactus Secure Partition (ID: %u)\n%s\n%s\n",
196 ffa_id, build_message, version_string);
197
Olivier Deprez0be4abe2020-08-04 11:26:13 +0200198 if (ffa_id == (SPM_VM_ID_FIRST + 2)) {
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100199 NOTICE("Mapping RXTX Region\n");
200
201 /* Declare RX/TX buffers at virtual FF-A instance */
202 static struct {
203 uint8_t rx[PAGE_SIZE];
204 uint8_t tx[PAGE_SIZE];
205 } __aligned(PAGE_SIZE) ffa_buffers;
206
207 /* Map RX/TX buffers */
208 smc_ret_values ret = ffa_rxtx_map((uintptr_t) &ffa_buffers.tx,
209 (uintptr_t) &ffa_buffers.rx,
210 sizeof(ffa_buffers.rx) / PAGE_SIZE);
211
212 if (ret.ret0 != FFA_SUCCESS_SMC32) {
213 ERROR("ffa_rxtx_map error (%lu)\n", ret.ret2);
214 panic();
215 }
216
217 /* Update mailbox with RX/TX buffer */
218 mb.send = (void *) &ffa_buffers.tx;
219 mb.recv = (void *) &ffa_buffers.rx;
220 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100221 }
Manish Pandey26c6f812020-04-30 11:43:00 +0100222
223 NOTICE("FFA id: %u\n", ffa_id);
224 cactus_print_memory_layout(ffa_id);
225
J-Alves9f6f0142020-06-17 15:37:59 +0100226 /* Invoking Tests */
Max Shvetsovc32f4782020-06-23 09:41:15 +0100227 ffa_tests(&mb);
J-Alves9f6f0142020-06-17 15:37:59 +0100228
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100229 /* End up to message loop */
J-Alves7581c382020-05-07 18:34:20 +0100230 message_loop(ffa_id);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100231
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100232 /* Not reached */
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200233}