blob: 650f06d2b59c30e7a6fe8d34f58655c94ed0cf38 [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;
J-Alvesda6ac322020-11-09 15:45:30 +000044 ffa_vm_id_t destination;
J-Alves1d203f12020-11-11 11:38:49 +000045
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010046 /*
J-Alves63cdaa72020-10-08 17:22:45 +010047 * This initial wait call is necessary to inform SPMD that
48 * SP initialization has completed. It blocks until receiving
49 * a direct message request.
50 */
51
J-Alves7581c382020-05-07 18:34:20 +010052 ffa_ret = ffa_msg_wait();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010053
54 for (;;) {
J-Alves6cb21d92021-01-07 15:18:12 +000055 VERBOSE("Woke up with func id: %x\n", ffa_func_id(ffa_ret));
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010056
J-Alves6cb21d92021-01-07 15:18:12 +000057 if (ffa_func_id(ffa_ret) == FFA_ERROR) {
58 ERROR("Error: %x\n", ffa_error_code(ffa_ret));
J-Alvesda6ac322020-11-09 15:45:30 +000059 break;
60 }
61
J-Alves6cb21d92021-01-07 15:18:12 +000062 if (ffa_func_id(ffa_ret) != FFA_MSG_SEND_DIRECT_REQ_SMC32 &&
63 ffa_func_id(ffa_ret) != FFA_MSG_SEND_DIRECT_REQ_SMC64) {
64 ERROR("%s(%u) unknown func id 0x%x\n",
65 __func__, vm_id, ffa_func_id(ffa_ret));
Olivier Deprez73d81cf2020-09-15 16:57:00 +020066 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010067 }
68
J-Alves6cb21d92021-01-07 15:18:12 +000069 destination = ffa_dir_msg_dest(ffa_ret);
J-Alvesda6ac322020-11-09 15:45:30 +000070
J-Alvesda6ac322020-11-09 15:45:30 +000071 if (destination != vm_id) {
J-Alves6cb21d92021-01-07 15:18:12 +000072 ERROR("%s(%u) invalid vm id 0x%x\n",
73 __func__, vm_id, destination);
Olivier Deprez73d81cf2020-09-15 16:57:00 +020074 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010075 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010076
J-Alves63cdaa72020-10-08 17:22:45 +010077 PRINT_CMD(ffa_ret);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010078
J-Alves4cb9dee2021-03-03 13:59:52 +000079 if (!cactus_handle_cmd(&ffa_ret, &ffa_ret, mb)) {
J-Alves63cdaa72020-10-08 17:22:45 +010080 break;
81 }
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{
Olivier Deprezaed7f082020-11-04 15:11:59 +010096 INFO("Secure Partition memory layout:\n");
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020097
Olivier Deprezaed7f082020-11-04 15:11:59 +010098 INFO(" 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
Olivier Deprezaed7f082020-11-04 15:11:59 +0100101 INFO(" 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
Olivier Deprezaed7f082020-11-04 15:11:59 +0100104 INFO(" 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
Olivier Deprezaed7f082020-11-04 15:11:59 +0100107 INFO(" 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
Olivier Deprezaed7f082020-11-04 15:11:59 +0100110 INFO(" 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
Olivier Deprezaed7f082020-11-04 15:11:59 +0100114 INFO(" 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;
J-Alvesda6ac322020-11-09 15:45:30 +0000164
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100165 /* Clear BSS */
166 memset((void *)CACTUS_BSS_START,
167 0, CACTUS_BSS_END - CACTUS_BSS_START);
168
J-Alves7581c382020-05-07 18:34:20 +0100169 /* Get current FFA id */
170 smc_ret_values ffa_id_ret = ffa_id_get();
J-Alves6cb21d92021-01-07 15:18:12 +0000171 if (ffa_func_id(ffa_id_ret) != FFA_SUCCESS_SMC32) {
J-Alves7581c382020-05-07 18:34:20 +0100172 ERROR("FFA_ID_GET failed.\n");
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100173 panic();
174 }
175
J-Alves7581c382020-05-07 18:34:20 +0100176 ffa_vm_id_t ffa_id = ffa_id_ret.ret2 & 0xffff;
Max Shvetsovc32f4782020-06-23 09:41:15 +0100177 mb.send = (void *) get_sp_tx_start(ffa_id);
178 mb.recv = (void *) get_sp_rx_start(ffa_id);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100179
Manish Pandey26c6f812020-04-30 11:43:00 +0100180 /* Configure and enable Stage-1 MMU, enable D-Cache */
181 cactus_plat_configure_mmu(ffa_id);
182 enable_mmu_el1(0);
183
J-Alves7581c382020-05-07 18:34:20 +0100184 if (ffa_id == SPM_VM_ID_FIRST) {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100185 console_init(CACTUS_PL011_UART_BASE,
186 CACTUS_PL011_UART_CLK_IN_HZ,
187 PL011_BAUDRATE);
Manish Pandey29495372020-04-09 15:19:26 +0100188
189 set_putc_impl(PL011_AS_STDOUT);
190
191 NOTICE("Booting Primary Cactus Secure Partition\n%s\n%s\n",
192 build_message, version_string);
Manish Pandey29495372020-04-09 15:19:26 +0100193 } else {
J-Alves83ede9b2020-11-02 17:37:19 +0000194 smc_ret_values ret;
Manish Pandey29495372020-04-09 15:19:26 +0100195 set_putc_impl(HVC_CALL_AS_STDOUT);
196
Olivier Deprezaed7f082020-11-04 15:11:59 +0100197 NOTICE("Booting Secondary Cactus Secure Partition (ID: %x)\n%s\n%s\n",
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100198 ffa_id, build_message, version_string);
199
Olivier Deprez0be4abe2020-08-04 11:26:13 +0200200 if (ffa_id == (SPM_VM_ID_FIRST + 2)) {
Olivier Deprezaed7f082020-11-04 15:11:59 +0100201 VERBOSE("Mapping RXTX Region\n");
J-Alves83ede9b2020-11-02 17:37:19 +0000202 CONFIGURE_AND_MAP_MAILBOX(mb, PAGE_SIZE, ret);
J-Alves6cb21d92021-01-07 15:18:12 +0000203 if (ffa_func_id(ret) != FFA_SUCCESS_SMC32) {
J-Alves83ede9b2020-11-02 17:37:19 +0000204 ERROR(
J-Alves6cb21d92021-01-07 15:18:12 +0000205 "Failed to map RXTX buffers. Error: %x\n",
206 ffa_error_code(ret));
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100207 panic();
208 }
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100209 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100210 }
Manish Pandey26c6f812020-04-30 11:43:00 +0100211
Olivier Deprezaed7f082020-11-04 15:11:59 +0100212 INFO("FF-A id: %x\n", ffa_id);
Manish Pandey26c6f812020-04-30 11:43:00 +0100213 cactus_print_memory_layout(ffa_id);
214
J-Alves9f6f0142020-06-17 15:37:59 +0100215 /* Invoking Tests */
Max Shvetsovc32f4782020-06-23 09:41:15 +0100216 ffa_tests(&mb);
J-Alves9f6f0142020-06-17 15:37:59 +0100217
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100218 /* End up to message loop */
J-Alves63cdaa72020-10-08 17:22:45 +0100219 message_loop(ffa_id, &mb);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100220
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100221 /* Not reached */
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200222}