blob: e54f3b0dafa19b5f492710b5b88c0c0fb4e6cae1 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Max Shvetsov103e0562021-02-04 16:58:31 +00002 * Copyright (c) 2018-2021, 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>
J-Alves9f6f0142020-06-17 15:37:59 +01009#include <debug.h>
Max Shvetsov103e0562021-02-04 16:58:31 +000010
J-Alveseeb25472021-03-11 09:54:21 +000011#include <cactus_message_loop.h>
Max Shvetsov103e0562021-02-04 16:58:31 +000012#include <cactus_platform_def.h>
J-Alves9f6f0142020-06-17 15:37:59 +010013#include <drivers/arm/pl011.h>
14#include <drivers/console.h>
J-Alves9f6f0142020-06-17 15:37:59 +010015#include <lib/aarch64/arch_helpers.h>
Max Shvetsov2263efb2020-11-12 17:30:11 +000016#include <lib/tftf_lib.h>
J-Alves9f6f0142020-06-17 15:37:59 +010017#include <lib/xlat_tables/xlat_mmu_helpers.h>
18#include <lib/xlat_tables/xlat_tables_v2.h>
Max Shvetsov103e0562021-02-04 16:58:31 +000019#include <plat_arm.h>
20#include <plat/common/platform.h>
21#include <platform_def.h>
J-Alves5aecd982020-06-11 10:25:33 +010022#include <sp_helpers.h>
J-Alves9f6f0142020-06-17 15:37:59 +010023#include <std_svc.h>
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010024
Max Shvetsov103e0562021-02-04 16:58:31 +000025#include "cactus_def.h"
26#include "cactus_tests.h"
27#include "cactus.h"
J-Alves63cdaa72020-10-08 17:22:45 +010028
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
Max Shvetsov2263efb2020-11-12 17:30:11 +000033extern void secondary_cold_entry(void);
34
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010035/*
36 *
37 * Message loop function
38 * Notice we cannot use regular print functions because this serves to both
39 * "primary" and "secondary" VMs. Secondary VM cannot access UART directly
40 * but rather through Hafnium print hypercall.
41 *
42 */
J-Alves0e1e7ca2021-01-25 14:11:06 +000043
J-Alves63cdaa72020-10-08 17:22:45 +010044static void __dead2 message_loop(ffa_vm_id_t vm_id, struct mailbox_buffers *mb)
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010045{
J-Alves7581c382020-05-07 18:34:20 +010046 smc_ret_values ffa_ret;
J-Alvesda6ac322020-11-09 15:45:30 +000047 ffa_vm_id_t destination;
J-Alves1d203f12020-11-11 11:38:49 +000048
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010049 /*
J-Alves63cdaa72020-10-08 17:22:45 +010050 * This initial wait call is necessary to inform SPMD that
51 * SP initialization has completed. It blocks until receiving
52 * a direct message request.
53 */
54
J-Alves7581c382020-05-07 18:34:20 +010055 ffa_ret = ffa_msg_wait();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010056
57 for (;;) {
J-Alves6cb21d92021-01-07 15:18:12 +000058 VERBOSE("Woke up with func id: %x\n", ffa_func_id(ffa_ret));
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010059
J-Alves6cb21d92021-01-07 15:18:12 +000060 if (ffa_func_id(ffa_ret) == FFA_ERROR) {
61 ERROR("Error: %x\n", ffa_error_code(ffa_ret));
J-Alvesda6ac322020-11-09 15:45:30 +000062 break;
63 }
64
J-Alves6cb21d92021-01-07 15:18:12 +000065 if (ffa_func_id(ffa_ret) != FFA_MSG_SEND_DIRECT_REQ_SMC32 &&
66 ffa_func_id(ffa_ret) != FFA_MSG_SEND_DIRECT_REQ_SMC64) {
67 ERROR("%s(%u) unknown func id 0x%x\n",
68 __func__, vm_id, ffa_func_id(ffa_ret));
Olivier Deprez73d81cf2020-09-15 16:57:00 +020069 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010070 }
71
J-Alves6cb21d92021-01-07 15:18:12 +000072 destination = ffa_dir_msg_dest(ffa_ret);
J-Alvesda6ac322020-11-09 15:45:30 +000073
J-Alvesda6ac322020-11-09 15:45:30 +000074 if (destination != vm_id) {
J-Alves6cb21d92021-01-07 15:18:12 +000075 ERROR("%s(%u) invalid vm id 0x%x\n",
76 __func__, vm_id, destination);
Olivier Deprez73d81cf2020-09-15 16:57:00 +020077 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010078 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010079
J-Alves4cb9dee2021-03-03 13:59:52 +000080 if (!cactus_handle_cmd(&ffa_ret, &ffa_ret, mb)) {
J-Alves63cdaa72020-10-08 17:22:45 +010081 break;
82 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010083 }
Olivier Deprez73d81cf2020-09-15 16:57:00 +020084
85 panic();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010086}
87
88static const mmap_region_t cactus_mmap[] __attribute__((used)) = {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +010089 /* PLAT_ARM_DEVICE0 area includes UART2 necessary to console */
90 MAP_REGION_FLAT(PLAT_ARM_DEVICE0_BASE, PLAT_ARM_DEVICE0_SIZE,
91 MT_DEVICE | MT_RW),
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010092 {0}
93};
94
Manish Pandey26c6f812020-04-30 11:43:00 +010095static void cactus_print_memory_layout(unsigned int vm_id)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020096{
Olivier Deprezaed7f082020-11-04 15:11:59 +010097 INFO("Secure Partition memory layout:\n");
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020098
Olivier Deprezaed7f082020-11-04 15:11:59 +010099 INFO(" Text region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100100 (void *)CACTUS_TEXT_START, (void *)CACTUS_TEXT_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100101
Olivier Deprezaed7f082020-11-04 15:11:59 +0100102 INFO(" Read-only data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100103 (void *)CACTUS_RODATA_START, (void *)CACTUS_RODATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100104
Olivier Deprezaed7f082020-11-04 15:11:59 +0100105 INFO(" Data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100106 (void *)CACTUS_DATA_START, (void *)CACTUS_DATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100107
Olivier Deprezaed7f082020-11-04 15:11:59 +0100108 INFO(" BSS region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100109 (void *)CACTUS_BSS_START, (void *)CACTUS_BSS_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100110
Olivier Deprezaed7f082020-11-04 15:11:59 +0100111 INFO(" RX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100112 (void *)get_sp_rx_start(vm_id),
113 (void *)get_sp_rx_end(vm_id));
Manish Pandey26c6f812020-04-30 11:43:00 +0100114
Olivier Deprezaed7f082020-11-04 15:11:59 +0100115 INFO(" TX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100116 (void *)get_sp_tx_start(vm_id),
117 (void *)get_sp_tx_end(vm_id));
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200118}
119
Manish Pandey26c6f812020-04-30 11:43:00 +0100120static void cactus_plat_configure_mmu(unsigned int vm_id)
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100121{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100122 mmap_add_region(CACTUS_TEXT_START,
123 CACTUS_TEXT_START,
124 CACTUS_TEXT_END - CACTUS_TEXT_START,
125 MT_CODE);
126 mmap_add_region(CACTUS_RODATA_START,
127 CACTUS_RODATA_START,
128 CACTUS_RODATA_END - CACTUS_RODATA_START,
129 MT_RO_DATA);
130 mmap_add_region(CACTUS_DATA_START,
131 CACTUS_DATA_START,
132 CACTUS_DATA_END - CACTUS_DATA_START,
133 MT_RW_DATA);
134 mmap_add_region(CACTUS_BSS_START,
135 CACTUS_BSS_START,
136 CACTUS_BSS_END - CACTUS_BSS_START,
137 MT_RW_DATA);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100138
Max Shvetsovc32f4782020-06-23 09:41:15 +0100139 mmap_add_region(get_sp_rx_start(vm_id),
140 get_sp_rx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100141 (CACTUS_RX_TX_SIZE / 2),
142 MT_RO_DATA);
143
Max Shvetsovc32f4782020-06-23 09:41:15 +0100144 mmap_add_region(get_sp_tx_start(vm_id),
145 get_sp_tx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100146 (CACTUS_RX_TX_SIZE / 2),
147 MT_RW_DATA);
148
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100149 mmap_add(cactus_mmap);
150 init_xlat_tables();
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100151}
152
Max Shvetsov2263efb2020-11-12 17:30:11 +0000153static void register_secondary_entrypoint(void)
154{
155 smc_args args;
156
157 args.fid = FFA_SECONDARY_EP_REGISTER_SMC64;
158 args.arg1 = (u_register_t)&secondary_cold_entry;
159
160 tftf_smc(&args);
161}
162
Olivier Deprez458d5532020-06-09 17:56:20 +0200163int tftf_irq_handler_dispatcher(void)
164{
165 ERROR("%s\n", __func__);
166
167 return 0;
168}
169
Max Shvetsov2263efb2020-11-12 17:30:11 +0000170void __dead2 cactus_main(bool primary_cold_boot)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200171{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100172 assert(IS_IN_EL1() != 0);
173
Max Shvetsovc32f4782020-06-23 09:41:15 +0100174 struct mailbox_buffers mb;
J-Alvesda6ac322020-11-09 15:45:30 +0000175
J-Alves7581c382020-05-07 18:34:20 +0100176 /* Get current FFA id */
177 smc_ret_values ffa_id_ret = ffa_id_get();
J-Alves6cb21d92021-01-07 15:18:12 +0000178 if (ffa_func_id(ffa_id_ret) != FFA_SUCCESS_SMC32) {
J-Alves7581c382020-05-07 18:34:20 +0100179 ERROR("FFA_ID_GET failed.\n");
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100180 panic();
181 }
J-Alves7581c382020-05-07 18:34:20 +0100182 ffa_vm_id_t ffa_id = ffa_id_ret.ret2 & 0xffff;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100183
Max Shvetsov2263efb2020-11-12 17:30:11 +0000184 if (primary_cold_boot == true) {
185 /* Clear BSS */
186 memset((void *)CACTUS_BSS_START,
187 0, CACTUS_BSS_END - CACTUS_BSS_START);
188
189
190 mb.send = (void *) get_sp_tx_start(ffa_id);
191 mb.recv = (void *) get_sp_rx_start(ffa_id);
192
193 /* Configure and enable Stage-1 MMU, enable D-Cache */
194 cactus_plat_configure_mmu(ffa_id);
195 }
196
Manish Pandey26c6f812020-04-30 11:43:00 +0100197 enable_mmu_el1(0);
198
Max Shvetsov2263efb2020-11-12 17:30:11 +0000199 if (primary_cold_boot == false) {
200 goto msg_loop;
201 }
202
J-Alves7581c382020-05-07 18:34:20 +0100203 if (ffa_id == SPM_VM_ID_FIRST) {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100204 console_init(CACTUS_PL011_UART_BASE,
205 CACTUS_PL011_UART_CLK_IN_HZ,
206 PL011_BAUDRATE);
Manish Pandey29495372020-04-09 15:19:26 +0100207
208 set_putc_impl(PL011_AS_STDOUT);
209
210 NOTICE("Booting Primary Cactus Secure Partition\n%s\n%s\n",
211 build_message, version_string);
Manish Pandey29495372020-04-09 15:19:26 +0100212 } else {
J-Alves83ede9b2020-11-02 17:37:19 +0000213 smc_ret_values ret;
Manish Pandey29495372020-04-09 15:19:26 +0100214 set_putc_impl(HVC_CALL_AS_STDOUT);
215
Olivier Deprezaed7f082020-11-04 15:11:59 +0100216 NOTICE("Booting Secondary Cactus Secure Partition (ID: %x)\n%s\n%s\n",
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100217 ffa_id, build_message, version_string);
218
Olivier Deprez0be4abe2020-08-04 11:26:13 +0200219 if (ffa_id == (SPM_VM_ID_FIRST + 2)) {
Olivier Deprezaed7f082020-11-04 15:11:59 +0100220 VERBOSE("Mapping RXTX Region\n");
J-Alves83ede9b2020-11-02 17:37:19 +0000221 CONFIGURE_AND_MAP_MAILBOX(mb, PAGE_SIZE, ret);
J-Alves6cb21d92021-01-07 15:18:12 +0000222 if (ffa_func_id(ret) != FFA_SUCCESS_SMC32) {
J-Alves83ede9b2020-11-02 17:37:19 +0000223 ERROR(
J-Alves6cb21d92021-01-07 15:18:12 +0000224 "Failed to map RXTX buffers. Error: %x\n",
225 ffa_error_code(ret));
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100226 panic();
227 }
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100228 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100229 }
Manish Pandey26c6f812020-04-30 11:43:00 +0100230
Olivier Deprezaed7f082020-11-04 15:11:59 +0100231 INFO("FF-A id: %x\n", ffa_id);
Manish Pandey26c6f812020-04-30 11:43:00 +0100232 cactus_print_memory_layout(ffa_id);
233
Max Shvetsov2263efb2020-11-12 17:30:11 +0000234 register_secondary_entrypoint();
235
J-Alves9f6f0142020-06-17 15:37:59 +0100236 /* Invoking Tests */
Max Shvetsovc32f4782020-06-23 09:41:15 +0100237 ffa_tests(&mb);
J-Alves9f6f0142020-06-17 15:37:59 +0100238
Max Shvetsov2263efb2020-11-12 17:30:11 +0000239msg_loop:
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100240 /* End up to message loop */
J-Alves63cdaa72020-10-08 17:22:45 +0100241 message_loop(ffa_id, &mb);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100242
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100243 /* Not reached */
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200244}