blob: 737fd119f6565f6aa88d6027fe4057d74a20e410 [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) {
53 ffa_ret = ffa_error(-1);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010054 continue;
55 }
56
Olivier Deprez0be4abe2020-08-04 11:26:13 +020057 if (ffa_ret.ret1 != vm_id) {
J-Alves7581c382020-05-07 18:34:20 +010058 ffa_ret = ffa_error(-2);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010059 continue;
60 }
61
J-Alves7581c382020-05-07 18:34:20 +010062 if (ffa_ret.ret2 != HYP_ID) {
63 ffa_ret = ffa_error(-3);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010064 continue;
65 }
66
67 /*
68 * For the sake of testing, add the vm id to the
69 * received message.
70 */
J-Alves7581c382020-05-07 18:34:20 +010071 sp_response = ffa_ret.ret3 | vm_id;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010072
73 /*
74 * Send a response through direct messaging then block
75 * until receiving a new message request.
76 */
Olivier Deprez0be4abe2020-08-04 11:26:13 +020077 ffa_ret = ffa_msg_send_direct_resp(vm_id, HYP_ID, sp_response);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010078 }
79}
80
81static const mmap_region_t cactus_mmap[] __attribute__((used)) = {
82 /* DEVICE0 area includes UART2 necessary to console */
83 MAP_REGION_FLAT(DEVICE0_BASE, DEVICE0_SIZE, MT_DEVICE | MT_RW),
84 {0}
85};
86
Manish Pandey26c6f812020-04-30 11:43:00 +010087static void cactus_print_memory_layout(unsigned int vm_id)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020088{
89 NOTICE("Secure Partition memory layout:\n");
90
Manish Pandey26c6f812020-04-30 11:43:00 +010091 NOTICE(" Text region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +010092 (void *)CACTUS_TEXT_START, (void *)CACTUS_TEXT_END);
Manish Pandey26c6f812020-04-30 11:43:00 +010093
94 NOTICE(" Read-only data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +010095 (void *)CACTUS_RODATA_START, (void *)CACTUS_RODATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +010096
97 NOTICE(" Data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +010098 (void *)CACTUS_DATA_START, (void *)CACTUS_DATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +010099
100 NOTICE(" BSS region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100101 (void *)CACTUS_BSS_START, (void *)CACTUS_BSS_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100102
103 NOTICE(" RX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100104 (void *)get_sp_rx_start(vm_id),
105 (void *)get_sp_rx_end(vm_id));
Manish Pandey26c6f812020-04-30 11:43:00 +0100106
107 NOTICE(" TX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100108 (void *)get_sp_tx_start(vm_id),
109 (void *)get_sp_tx_end(vm_id));
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200110}
111
Manish Pandey26c6f812020-04-30 11:43:00 +0100112static void cactus_plat_configure_mmu(unsigned int vm_id)
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100113{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100114 mmap_add_region(CACTUS_TEXT_START,
115 CACTUS_TEXT_START,
116 CACTUS_TEXT_END - CACTUS_TEXT_START,
117 MT_CODE);
118 mmap_add_region(CACTUS_RODATA_START,
119 CACTUS_RODATA_START,
120 CACTUS_RODATA_END - CACTUS_RODATA_START,
121 MT_RO_DATA);
122 mmap_add_region(CACTUS_DATA_START,
123 CACTUS_DATA_START,
124 CACTUS_DATA_END - CACTUS_DATA_START,
125 MT_RW_DATA);
126 mmap_add_region(CACTUS_BSS_START,
127 CACTUS_BSS_START,
128 CACTUS_BSS_END - CACTUS_BSS_START,
129 MT_RW_DATA);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100130
Max Shvetsovc32f4782020-06-23 09:41:15 +0100131 mmap_add_region(get_sp_rx_start(vm_id),
132 get_sp_rx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100133 (CACTUS_RX_TX_SIZE / 2),
134 MT_RO_DATA);
135
Max Shvetsovc32f4782020-06-23 09:41:15 +0100136 mmap_add_region(get_sp_tx_start(vm_id),
137 get_sp_tx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100138 (CACTUS_RX_TX_SIZE / 2),
139 MT_RW_DATA);
140
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100141 mmap_add(cactus_mmap);
142 init_xlat_tables();
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100143}
144
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100145void __dead2 cactus_main(void)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200146{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100147 assert(IS_IN_EL1() != 0);
148
Max Shvetsovc32f4782020-06-23 09:41:15 +0100149 struct mailbox_buffers mb;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100150 /* Clear BSS */
151 memset((void *)CACTUS_BSS_START,
152 0, CACTUS_BSS_END - CACTUS_BSS_START);
153
J-Alves7581c382020-05-07 18:34:20 +0100154 /* Get current FFA id */
155 smc_ret_values ffa_id_ret = ffa_id_get();
156 if (ffa_id_ret.ret0 != FFA_SUCCESS_SMC32) {
157 ERROR("FFA_ID_GET failed.\n");
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100158 panic();
159 }
160
J-Alves7581c382020-05-07 18:34:20 +0100161 ffa_vm_id_t ffa_id = ffa_id_ret.ret2 & 0xffff;
Max Shvetsovc32f4782020-06-23 09:41:15 +0100162 mb.send = (void *) get_sp_tx_start(ffa_id);
163 mb.recv = (void *) get_sp_rx_start(ffa_id);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100164
Manish Pandey26c6f812020-04-30 11:43:00 +0100165 /* Configure and enable Stage-1 MMU, enable D-Cache */
166 cactus_plat_configure_mmu(ffa_id);
167 enable_mmu_el1(0);
168
J-Alves7581c382020-05-07 18:34:20 +0100169 if (ffa_id == SPM_VM_ID_FIRST) {
Manish Pandey29495372020-04-09 15:19:26 +0100170 console_init(PL011_UART2_BASE,
171 PL011_UART2_CLK_IN_HZ,
172 PL011_BAUDRATE);
173
174 set_putc_impl(PL011_AS_STDOUT);
175
176 NOTICE("Booting Primary Cactus Secure Partition\n%s\n%s\n",
177 build_message, version_string);
178
Manish Pandey29495372020-04-09 15:19:26 +0100179 /* Get number of VMs */
180 NOTICE("VM count: %u\n", spm_vm_get_count());
181
182 /* Get virtual CPU count for current VM */
J-Alves7581c382020-05-07 18:34:20 +0100183 NOTICE("vCPU count: %u\n", spm_vcpu_get_count(ffa_id));
Manish Pandey29495372020-04-09 15:19:26 +0100184 } else {
185 set_putc_impl(HVC_CALL_AS_STDOUT);
186
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100187 NOTICE("Booting Secondary Cactus Secure Partition (ID: %u)\n%s\n%s\n",
188 ffa_id, build_message, version_string);
189
Olivier Deprez0be4abe2020-08-04 11:26:13 +0200190 if (ffa_id == (SPM_VM_ID_FIRST + 2)) {
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100191 NOTICE("Mapping RXTX Region\n");
192
193 /* Declare RX/TX buffers at virtual FF-A instance */
194 static struct {
195 uint8_t rx[PAGE_SIZE];
196 uint8_t tx[PAGE_SIZE];
197 } __aligned(PAGE_SIZE) ffa_buffers;
198
199 /* Map RX/TX buffers */
200 smc_ret_values ret = ffa_rxtx_map((uintptr_t) &ffa_buffers.tx,
201 (uintptr_t) &ffa_buffers.rx,
202 sizeof(ffa_buffers.rx) / PAGE_SIZE);
203
204 if (ret.ret0 != FFA_SUCCESS_SMC32) {
205 ERROR("ffa_rxtx_map error (%lu)\n", ret.ret2);
206 panic();
207 }
208
209 /* Update mailbox with RX/TX buffer */
210 mb.send = (void *) &ffa_buffers.tx;
211 mb.recv = (void *) &ffa_buffers.rx;
212 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100213 }
Manish Pandey26c6f812020-04-30 11:43:00 +0100214
215 NOTICE("FFA id: %u\n", ffa_id);
216 cactus_print_memory_layout(ffa_id);
217
J-Alves9f6f0142020-06-17 15:37:59 +0100218 /* Invoking Tests */
Max Shvetsovc32f4782020-06-23 09:41:15 +0100219 ffa_tests(&mb);
J-Alves9f6f0142020-06-17 15:37:59 +0100220
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100221 /* End up to message loop */
J-Alves7581c382020-05-07 18:34:20 +0100222 message_loop(ffa_id);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100223
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100224 /* Not reached */
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200225}