blob: 37b8bd50e838bc905e2a40790662bbd5f03d9261 [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
11#include <cactus_platform_def.h>
12#include <cactus_test_cmds.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>
16#include <lib/xlat_tables/xlat_mmu_helpers.h>
17#include <lib/xlat_tables/xlat_tables_v2.h>
Max Shvetsov103e0562021-02-04 16:58:31 +000018#include <plat_arm.h>
19#include <plat/common/platform.h>
20#include <platform_def.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>
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010023
Max Shvetsov103e0562021-02-04 16:58:31 +000024#include "cactus_def.h"
25#include "cactus_tests.h"
26#include "cactus.h"
J-Alves63cdaa72020-10-08 17:22:45 +010027
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020028/* Host machine information injected by the build system in the ELF file. */
29extern const char build_message[];
30extern const char version_string[];
31
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010032/*
33 *
34 * Message loop function
35 * Notice we cannot use regular print functions because this serves to both
36 * "primary" and "secondary" VMs. Secondary VM cannot access UART directly
37 * but rather through Hafnium print hypercall.
38 *
39 */
J-Alves0e1e7ca2021-01-25 14:11:06 +000040
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-Alvesda6ac322020-11-09 15:45:30 +000045 ffa_vm_id_t destination;
J-Alves1d203f12020-11-11 11:38:49 +000046 uint64_t cactus_cmd;
47
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010048 /*
J-Alves63cdaa72020-10-08 17:22:45 +010049 * This initial wait call is necessary to inform SPMD that
50 * SP initialization has completed. It blocks until receiving
51 * a direct message request.
52 */
53
J-Alves7581c382020-05-07 18:34:20 +010054 ffa_ret = ffa_msg_wait();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010055
56 for (;;) {
J-Alves0e1e7ca2021-01-25 14:11:06 +000057 /* temporary 'skip_switch' label. Deleted in following commit */
58 skip_switch:
J-Alves6cb21d92021-01-07 15:18:12 +000059 VERBOSE("Woke up with func id: %x\n", ffa_func_id(ffa_ret));
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010060
J-Alves6cb21d92021-01-07 15:18:12 +000061 if (ffa_func_id(ffa_ret) == FFA_ERROR) {
62 ERROR("Error: %x\n", ffa_error_code(ffa_ret));
J-Alvesda6ac322020-11-09 15:45:30 +000063 break;
64 }
65
J-Alves6cb21d92021-01-07 15:18:12 +000066 if (ffa_func_id(ffa_ret) != FFA_MSG_SEND_DIRECT_REQ_SMC32 &&
67 ffa_func_id(ffa_ret) != FFA_MSG_SEND_DIRECT_REQ_SMC64) {
68 ERROR("%s(%u) unknown func id 0x%x\n",
69 __func__, vm_id, ffa_func_id(ffa_ret));
Olivier Deprez73d81cf2020-09-15 16:57:00 +020070 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010071 }
72
J-Alves6cb21d92021-01-07 15:18:12 +000073 destination = ffa_dir_msg_dest(ffa_ret);
J-Alvesda6ac322020-11-09 15:45:30 +000074
J-Alvesda6ac322020-11-09 15:45:30 +000075 if (destination != vm_id) {
J-Alves6cb21d92021-01-07 15:18:12 +000076 ERROR("%s(%u) invalid vm id 0x%x\n",
77 __func__, vm_id, destination);
Olivier Deprez73d81cf2020-09-15 16:57:00 +020078 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010079 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010080
J-Alves63cdaa72020-10-08 17:22:45 +010081 PRINT_CMD(ffa_ret);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010082
J-Alves53392012020-11-18 14:51:57 +000083 cactus_cmd = cactus_get_cmd(ffa_ret);
J-Alves1d203f12020-11-11 11:38:49 +000084
J-Alves0e1e7ca2021-01-25 14:11:06 +000085 if (cactus_handle_cmd(&ffa_ret, &ffa_ret, mb)) {
86 goto skip_switch;
87 }
J-Alves1d203f12020-11-11 11:38:49 +000088 switch (cactus_cmd) {
J-Alves63cdaa72020-10-08 17:22:45 +010089 default:
90 /*
91 * Currently direct message test is handled here.
92 * TODO: create a case within the switch case
93 * For the sake of testing, add the vm id to the
94 * received message.
95 */
J-Alves63cdaa72020-10-08 17:22:45 +010096 sp_response = ffa_ret.ret3 | vm_id;
Olivier Deprezaed7f082020-11-04 15:11:59 +010097 VERBOSE("Replying with direct message response: %x\n", sp_response);
J-Alves63cdaa72020-10-08 17:22:45 +010098 ffa_ret = ffa_msg_send_direct_resp(vm_id,
99 HYP_ID,
100 sp_response);
101
102 break;
103 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100104 }
Olivier Deprez73d81cf2020-09-15 16:57:00 +0200105
106 panic();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100107}
108
109static const mmap_region_t cactus_mmap[] __attribute__((used)) = {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100110 /* PLAT_ARM_DEVICE0 area includes UART2 necessary to console */
111 MAP_REGION_FLAT(PLAT_ARM_DEVICE0_BASE, PLAT_ARM_DEVICE0_SIZE,
112 MT_DEVICE | MT_RW),
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100113 {0}
114};
115
Manish Pandey26c6f812020-04-30 11:43:00 +0100116static void cactus_print_memory_layout(unsigned int vm_id)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200117{
Olivier Deprezaed7f082020-11-04 15:11:59 +0100118 INFO("Secure Partition memory layout:\n");
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200119
Olivier Deprezaed7f082020-11-04 15:11:59 +0100120 INFO(" Text region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100121 (void *)CACTUS_TEXT_START, (void *)CACTUS_TEXT_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100122
Olivier Deprezaed7f082020-11-04 15:11:59 +0100123 INFO(" Read-only data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100124 (void *)CACTUS_RODATA_START, (void *)CACTUS_RODATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100125
Olivier Deprezaed7f082020-11-04 15:11:59 +0100126 INFO(" Data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100127 (void *)CACTUS_DATA_START, (void *)CACTUS_DATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100128
Olivier Deprezaed7f082020-11-04 15:11:59 +0100129 INFO(" BSS region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100130 (void *)CACTUS_BSS_START, (void *)CACTUS_BSS_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100131
Olivier Deprezaed7f082020-11-04 15:11:59 +0100132 INFO(" RX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100133 (void *)get_sp_rx_start(vm_id),
134 (void *)get_sp_rx_end(vm_id));
Manish Pandey26c6f812020-04-30 11:43:00 +0100135
Olivier Deprezaed7f082020-11-04 15:11:59 +0100136 INFO(" TX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100137 (void *)get_sp_tx_start(vm_id),
138 (void *)get_sp_tx_end(vm_id));
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200139}
140
Manish Pandey26c6f812020-04-30 11:43:00 +0100141static void cactus_plat_configure_mmu(unsigned int vm_id)
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100142{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100143 mmap_add_region(CACTUS_TEXT_START,
144 CACTUS_TEXT_START,
145 CACTUS_TEXT_END - CACTUS_TEXT_START,
146 MT_CODE);
147 mmap_add_region(CACTUS_RODATA_START,
148 CACTUS_RODATA_START,
149 CACTUS_RODATA_END - CACTUS_RODATA_START,
150 MT_RO_DATA);
151 mmap_add_region(CACTUS_DATA_START,
152 CACTUS_DATA_START,
153 CACTUS_DATA_END - CACTUS_DATA_START,
154 MT_RW_DATA);
155 mmap_add_region(CACTUS_BSS_START,
156 CACTUS_BSS_START,
157 CACTUS_BSS_END - CACTUS_BSS_START,
158 MT_RW_DATA);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100159
Max Shvetsovc32f4782020-06-23 09:41:15 +0100160 mmap_add_region(get_sp_rx_start(vm_id),
161 get_sp_rx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100162 (CACTUS_RX_TX_SIZE / 2),
163 MT_RO_DATA);
164
Max Shvetsovc32f4782020-06-23 09:41:15 +0100165 mmap_add_region(get_sp_tx_start(vm_id),
166 get_sp_tx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100167 (CACTUS_RX_TX_SIZE / 2),
168 MT_RW_DATA);
169
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100170 mmap_add(cactus_mmap);
171 init_xlat_tables();
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100172}
173
Olivier Deprez458d5532020-06-09 17:56:20 +0200174int tftf_irq_handler_dispatcher(void)
175{
176 ERROR("%s\n", __func__);
177
178 return 0;
179}
180
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100181void __dead2 cactus_main(void)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200182{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100183 assert(IS_IN_EL1() != 0);
184
Max Shvetsovc32f4782020-06-23 09:41:15 +0100185 struct mailbox_buffers mb;
J-Alvesda6ac322020-11-09 15:45:30 +0000186
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100187 /* Clear BSS */
188 memset((void *)CACTUS_BSS_START,
189 0, CACTUS_BSS_END - CACTUS_BSS_START);
190
J-Alves7581c382020-05-07 18:34:20 +0100191 /* Get current FFA id */
192 smc_ret_values ffa_id_ret = ffa_id_get();
J-Alves6cb21d92021-01-07 15:18:12 +0000193 if (ffa_func_id(ffa_id_ret) != FFA_SUCCESS_SMC32) {
J-Alves7581c382020-05-07 18:34:20 +0100194 ERROR("FFA_ID_GET failed.\n");
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100195 panic();
196 }
197
J-Alves7581c382020-05-07 18:34:20 +0100198 ffa_vm_id_t ffa_id = ffa_id_ret.ret2 & 0xffff;
Max Shvetsovc32f4782020-06-23 09:41:15 +0100199 mb.send = (void *) get_sp_tx_start(ffa_id);
200 mb.recv = (void *) get_sp_rx_start(ffa_id);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100201
Manish Pandey26c6f812020-04-30 11:43:00 +0100202 /* Configure and enable Stage-1 MMU, enable D-Cache */
203 cactus_plat_configure_mmu(ffa_id);
204 enable_mmu_el1(0);
205
J-Alves7581c382020-05-07 18:34:20 +0100206 if (ffa_id == SPM_VM_ID_FIRST) {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100207 console_init(CACTUS_PL011_UART_BASE,
208 CACTUS_PL011_UART_CLK_IN_HZ,
209 PL011_BAUDRATE);
Manish Pandey29495372020-04-09 15:19:26 +0100210
211 set_putc_impl(PL011_AS_STDOUT);
212
213 NOTICE("Booting Primary Cactus Secure Partition\n%s\n%s\n",
214 build_message, version_string);
Manish Pandey29495372020-04-09 15:19:26 +0100215 } else {
J-Alves83ede9b2020-11-02 17:37:19 +0000216 smc_ret_values ret;
Manish Pandey29495372020-04-09 15:19:26 +0100217 set_putc_impl(HVC_CALL_AS_STDOUT);
218
Olivier Deprezaed7f082020-11-04 15:11:59 +0100219 NOTICE("Booting Secondary Cactus Secure Partition (ID: %x)\n%s\n%s\n",
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100220 ffa_id, build_message, version_string);
221
Olivier Deprez0be4abe2020-08-04 11:26:13 +0200222 if (ffa_id == (SPM_VM_ID_FIRST + 2)) {
Olivier Deprezaed7f082020-11-04 15:11:59 +0100223 VERBOSE("Mapping RXTX Region\n");
J-Alves83ede9b2020-11-02 17:37:19 +0000224 CONFIGURE_AND_MAP_MAILBOX(mb, PAGE_SIZE, ret);
J-Alves6cb21d92021-01-07 15:18:12 +0000225 if (ffa_func_id(ret) != FFA_SUCCESS_SMC32) {
J-Alves83ede9b2020-11-02 17:37:19 +0000226 ERROR(
J-Alves6cb21d92021-01-07 15:18:12 +0000227 "Failed to map RXTX buffers. Error: %x\n",
228 ffa_error_code(ret));
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100229 panic();
230 }
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100231 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100232 }
Manish Pandey26c6f812020-04-30 11:43:00 +0100233
Olivier Deprezaed7f082020-11-04 15:11:59 +0100234 INFO("FF-A id: %x\n", ffa_id);
Manish Pandey26c6f812020-04-30 11:43:00 +0100235 cactus_print_memory_layout(ffa_id);
236
J-Alves9f6f0142020-06-17 15:37:59 +0100237 /* Invoking Tests */
Max Shvetsovc32f4782020-06-23 09:41:15 +0100238 ffa_tests(&mb);
J-Alves9f6f0142020-06-17 15:37:59 +0100239
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}