blob: 080971630905f739ebe11e4ad2072ec413235101 [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"
J-Alves9f6f0142020-06-17 15:37:59 +010012#include "cactus_tests.h"
13#include <debug.h>
14#include <drivers/arm/pl011.h>
15#include <drivers/console.h>
J-Alves5aecd982020-06-11 10:25:33 +010016#include <ffa_helpers.h>
J-Alves9f6f0142020-06-17 15:37:59 +010017#include <lib/aarch64/arch_helpers.h>
18#include <lib/xlat_tables/xlat_mmu_helpers.h>
19#include <lib/xlat_tables/xlat_tables_v2.h>
J-Alves5aecd982020-06-11 10:25:33 +010020#include <sp_helpers.h>
J-Alves9f6f0142020-06-17 15:37:59 +010021#include <std_svc.h>
22#include <plat/common/platform.h>
23#include <plat_arm.h>
24#include <platform_def.h>
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010025
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020026/* Host machine information injected by the build system in the ELF file. */
27extern const char build_message[];
28extern const char version_string[];
29
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010030/*
31 *
32 * Message loop function
33 * Notice we cannot use regular print functions because this serves to both
34 * "primary" and "secondary" VMs. Secondary VM cannot access UART directly
35 * but rather through Hafnium print hypercall.
36 *
37 */
J-Alves7581c382020-05-07 18:34:20 +010038static void __dead2 message_loop(ffa_vm_id_t vm_id)
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010039{
J-Alves7581c382020-05-07 18:34:20 +010040 smc_ret_values ffa_ret;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010041 uint32_t sp_response;
42
43 /*
44 * This initial wait call is necessary to inform SPMD that
45 * SP initialization has completed. It blocks until receiving
46 * a direct message request.
47 */
J-Alves7581c382020-05-07 18:34:20 +010048 ffa_ret = ffa_msg_wait();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010049
50 for (;;) {
51
J-Alves7581c382020-05-07 18:34:20 +010052 if (ffa_ret.ret0 != FFA_MSG_SEND_DIRECT_REQ_SMC32) {
Olivier Deprez73d81cf2020-09-15 16:57:00 +020053 ERROR("%s(%u) unknown func id 0x%lx\n",
54 __func__, vm_id, ffa_ret.ret0);
55 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010056 }
57
Olivier Deprez0be4abe2020-08-04 11:26:13 +020058 if (ffa_ret.ret1 != vm_id) {
Olivier Deprez73d81cf2020-09-15 16:57:00 +020059 ERROR("%s(%u) invalid vm id 0x%lx\n",
60 __func__, vm_id, ffa_ret.ret1);
61 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010062 }
63
J-Alves7581c382020-05-07 18:34:20 +010064 if (ffa_ret.ret2 != HYP_ID) {
Olivier Deprez73d81cf2020-09-15 16:57:00 +020065 ERROR("%s(%u) invalid hyp id 0x%lx\n",
66 __func__, vm_id, ffa_ret.ret2);
67 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010068 }
69
70 /*
71 * For the sake of testing, add the vm id to the
72 * received message.
73 */
J-Alves7581c382020-05-07 18:34:20 +010074 sp_response = ffa_ret.ret3 | vm_id;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010075
76 /*
77 * Send a response through direct messaging then block
78 * until receiving a new message request.
79 */
Olivier Deprez0be4abe2020-08-04 11:26:13 +020080 ffa_ret = ffa_msg_send_direct_resp(vm_id, HYP_ID, sp_response);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010081 }
Olivier Deprez73d81cf2020-09-15 16:57:00 +020082
83 panic();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010084}
85
86static const mmap_region_t cactus_mmap[] __attribute__((used)) = {
87 /* DEVICE0 area includes UART2 necessary to console */
88 MAP_REGION_FLAT(DEVICE0_BASE, DEVICE0_SIZE, MT_DEVICE | MT_RW),
89 {0}
90};
91
Manish Pandey26c6f812020-04-30 11:43:00 +010092static void cactus_print_memory_layout(unsigned int vm_id)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020093{
94 NOTICE("Secure Partition memory layout:\n");
95
Manish Pandey26c6f812020-04-30 11:43:00 +010096 NOTICE(" Text region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +010097 (void *)CACTUS_TEXT_START, (void *)CACTUS_TEXT_END);
Manish Pandey26c6f812020-04-30 11:43:00 +010098
99 NOTICE(" Read-only data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100100 (void *)CACTUS_RODATA_START, (void *)CACTUS_RODATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100101
102 NOTICE(" Data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100103 (void *)CACTUS_DATA_START, (void *)CACTUS_DATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100104
105 NOTICE(" BSS region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100106 (void *)CACTUS_BSS_START, (void *)CACTUS_BSS_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100107
108 NOTICE(" RX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100109 (void *)get_sp_rx_start(vm_id),
110 (void *)get_sp_rx_end(vm_id));
Manish Pandey26c6f812020-04-30 11:43:00 +0100111
112 NOTICE(" TX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100113 (void *)get_sp_tx_start(vm_id),
114 (void *)get_sp_tx_end(vm_id));
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200115}
116
Manish Pandey26c6f812020-04-30 11:43:00 +0100117static void cactus_plat_configure_mmu(unsigned int vm_id)
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100118{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100119 mmap_add_region(CACTUS_TEXT_START,
120 CACTUS_TEXT_START,
121 CACTUS_TEXT_END - CACTUS_TEXT_START,
122 MT_CODE);
123 mmap_add_region(CACTUS_RODATA_START,
124 CACTUS_RODATA_START,
125 CACTUS_RODATA_END - CACTUS_RODATA_START,
126 MT_RO_DATA);
127 mmap_add_region(CACTUS_DATA_START,
128 CACTUS_DATA_START,
129 CACTUS_DATA_END - CACTUS_DATA_START,
130 MT_RW_DATA);
131 mmap_add_region(CACTUS_BSS_START,
132 CACTUS_BSS_START,
133 CACTUS_BSS_END - CACTUS_BSS_START,
134 MT_RW_DATA);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100135
Max Shvetsovc32f4782020-06-23 09:41:15 +0100136 mmap_add_region(get_sp_rx_start(vm_id),
137 get_sp_rx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100138 (CACTUS_RX_TX_SIZE / 2),
139 MT_RO_DATA);
140
Max Shvetsovc32f4782020-06-23 09:41:15 +0100141 mmap_add_region(get_sp_tx_start(vm_id),
142 get_sp_tx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100143 (CACTUS_RX_TX_SIZE / 2),
144 MT_RW_DATA);
145
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100146 mmap_add(cactus_mmap);
147 init_xlat_tables();
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100148}
149
Olivier Deprez458d5532020-06-09 17:56:20 +0200150int tftf_irq_handler_dispatcher(void)
151{
152 ERROR("%s\n", __func__);
153
154 return 0;
155}
156
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100157void __dead2 cactus_main(void)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200158{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100159 assert(IS_IN_EL1() != 0);
160
Max Shvetsovc32f4782020-06-23 09:41:15 +0100161 struct mailbox_buffers mb;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100162 /* Clear BSS */
163 memset((void *)CACTUS_BSS_START,
164 0, CACTUS_BSS_END - CACTUS_BSS_START);
165
J-Alves7581c382020-05-07 18:34:20 +0100166 /* Get current FFA id */
167 smc_ret_values ffa_id_ret = ffa_id_get();
168 if (ffa_id_ret.ret0 != FFA_SUCCESS_SMC32) {
169 ERROR("FFA_ID_GET failed.\n");
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100170 panic();
171 }
172
J-Alves7581c382020-05-07 18:34:20 +0100173 ffa_vm_id_t ffa_id = ffa_id_ret.ret2 & 0xffff;
Max Shvetsovc32f4782020-06-23 09:41:15 +0100174 mb.send = (void *) get_sp_tx_start(ffa_id);
175 mb.recv = (void *) get_sp_rx_start(ffa_id);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100176
Manish Pandey26c6f812020-04-30 11:43:00 +0100177 /* Configure and enable Stage-1 MMU, enable D-Cache */
178 cactus_plat_configure_mmu(ffa_id);
179 enable_mmu_el1(0);
180
J-Alves7581c382020-05-07 18:34:20 +0100181 if (ffa_id == SPM_VM_ID_FIRST) {
Manish Pandey29495372020-04-09 15:19:26 +0100182 console_init(PL011_UART2_BASE,
183 PL011_UART2_CLK_IN_HZ,
184 PL011_BAUDRATE);
185
186 set_putc_impl(PL011_AS_STDOUT);
187
188 NOTICE("Booting Primary Cactus Secure Partition\n%s\n%s\n",
189 build_message, version_string);
Manish Pandey29495372020-04-09 15:19:26 +0100190 } else {
191 set_putc_impl(HVC_CALL_AS_STDOUT);
192
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100193 NOTICE("Booting Secondary Cactus Secure Partition (ID: %u)\n%s\n%s\n",
194 ffa_id, build_message, version_string);
195
Olivier Deprez0be4abe2020-08-04 11:26:13 +0200196 if (ffa_id == (SPM_VM_ID_FIRST + 2)) {
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100197 NOTICE("Mapping RXTX Region\n");
198
199 /* Declare RX/TX buffers at virtual FF-A instance */
200 static struct {
201 uint8_t rx[PAGE_SIZE];
202 uint8_t tx[PAGE_SIZE];
203 } __aligned(PAGE_SIZE) ffa_buffers;
204
205 /* Map RX/TX buffers */
206 smc_ret_values ret = ffa_rxtx_map((uintptr_t) &ffa_buffers.tx,
207 (uintptr_t) &ffa_buffers.rx,
208 sizeof(ffa_buffers.rx) / PAGE_SIZE);
209
210 if (ret.ret0 != FFA_SUCCESS_SMC32) {
211 ERROR("ffa_rxtx_map error (%lu)\n", ret.ret2);
212 panic();
213 }
214
215 /* Update mailbox with RX/TX buffer */
216 mb.send = (void *) &ffa_buffers.tx;
217 mb.recv = (void *) &ffa_buffers.rx;
218 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100219 }
Manish Pandey26c6f812020-04-30 11:43:00 +0100220
221 NOTICE("FFA id: %u\n", ffa_id);
222 cactus_print_memory_layout(ffa_id);
223
J-Alves9f6f0142020-06-17 15:37:59 +0100224 /* Invoking Tests */
Max Shvetsovc32f4782020-06-23 09:41:15 +0100225 ffa_tests(&mb);
J-Alves9f6f0142020-06-17 15:37:59 +0100226
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100227 /* End up to message loop */
J-Alves7581c382020-05-07 18:34:20 +0100228 message_loop(ffa_id);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100229
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100230 /* Not reached */
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200231}