blob: ff3f6187170db6cb414fb3777b9a96b75c7c76f2 [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>
Olivier Deprez6967c242021-04-09 09:24:08 +020023#include <spm_helpers.h>
J-Alves9f6f0142020-06-17 15:37:59 +010024#include <std_svc.h>
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010025
Max Shvetsov103e0562021-02-04 16:58:31 +000026#include "cactus_def.h"
27#include "cactus_tests.h"
28#include "cactus.h"
J-Alves63cdaa72020-10-08 17:22:45 +010029
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020030/* Host machine information injected by the build system in the ELF file. */
31extern const char build_message[];
32extern const char version_string[];
33
Max Shvetsov2263efb2020-11-12 17:30:11 +000034extern void secondary_cold_entry(void);
35
Manish Pandey87d4c702021-03-02 22:31:57 +000036/* Global ffa_id */
37ffa_vm_id_t g_ffa_id;
38
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010039/*
40 *
41 * Message loop function
42 * Notice we cannot use regular print functions because this serves to both
43 * "primary" and "secondary" VMs. Secondary VM cannot access UART directly
44 * but rather through Hafnium print hypercall.
45 *
46 */
J-Alves0e1e7ca2021-01-25 14:11:06 +000047
J-Alves63cdaa72020-10-08 17:22:45 +010048static void __dead2 message_loop(ffa_vm_id_t vm_id, struct mailbox_buffers *mb)
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010049{
J-Alves7581c382020-05-07 18:34:20 +010050 smc_ret_values ffa_ret;
J-Alvesda6ac322020-11-09 15:45:30 +000051 ffa_vm_id_t destination;
J-Alves1d203f12020-11-11 11:38:49 +000052
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010053 /*
J-Alves63cdaa72020-10-08 17:22:45 +010054 * This initial wait call is necessary to inform SPMD that
55 * SP initialization has completed. It blocks until receiving
56 * a direct message request.
57 */
58
J-Alves7581c382020-05-07 18:34:20 +010059 ffa_ret = ffa_msg_wait();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010060
61 for (;;) {
J-Alves6cb21d92021-01-07 15:18:12 +000062 VERBOSE("Woke up with func id: %x\n", ffa_func_id(ffa_ret));
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010063
J-Alves6cb21d92021-01-07 15:18:12 +000064 if (ffa_func_id(ffa_ret) == FFA_ERROR) {
65 ERROR("Error: %x\n", ffa_error_code(ffa_ret));
J-Alvesda6ac322020-11-09 15:45:30 +000066 break;
67 }
68
J-Alves6cb21d92021-01-07 15:18:12 +000069 if (ffa_func_id(ffa_ret) != FFA_MSG_SEND_DIRECT_REQ_SMC32 &&
70 ffa_func_id(ffa_ret) != FFA_MSG_SEND_DIRECT_REQ_SMC64) {
71 ERROR("%s(%u) unknown func id 0x%x\n",
72 __func__, vm_id, ffa_func_id(ffa_ret));
Olivier Deprez73d81cf2020-09-15 16:57:00 +020073 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010074 }
75
J-Alves6cb21d92021-01-07 15:18:12 +000076 destination = ffa_dir_msg_dest(ffa_ret);
J-Alvesda6ac322020-11-09 15:45:30 +000077
J-Alvesda6ac322020-11-09 15:45:30 +000078 if (destination != vm_id) {
J-Alves6cb21d92021-01-07 15:18:12 +000079 ERROR("%s(%u) invalid vm id 0x%x\n",
80 __func__, vm_id, destination);
Olivier Deprez73d81cf2020-09-15 16:57:00 +020081 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010082 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010083
J-Alves4cb9dee2021-03-03 13:59:52 +000084 if (!cactus_handle_cmd(&ffa_ret, &ffa_ret, mb)) {
J-Alves63cdaa72020-10-08 17:22:45 +010085 break;
86 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010087 }
Olivier Deprez73d81cf2020-09-15 16:57:00 +020088
89 panic();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010090}
91
92static const mmap_region_t cactus_mmap[] __attribute__((used)) = {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +010093 /* PLAT_ARM_DEVICE0 area includes UART2 necessary to console */
94 MAP_REGION_FLAT(PLAT_ARM_DEVICE0_BASE, PLAT_ARM_DEVICE0_SIZE,
95 MT_DEVICE | MT_RW),
Madhukar Pappireddy172523b2020-12-31 19:25:33 -060096 /* scratch memory allocated to be used for running SMMU tests */
97 MAP_REGION_FLAT(PLAT_CACTUS_MEMCPY_BASE, PLAT_CACTUS_MEMCPY_RANGE,
98 MT_MEMORY | MT_RW),
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010099 {0}
100};
101
Manish Pandey26c6f812020-04-30 11:43:00 +0100102static void cactus_print_memory_layout(unsigned int vm_id)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200103{
Olivier Deprezaed7f082020-11-04 15:11:59 +0100104 INFO("Secure Partition memory layout:\n");
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200105
Olivier Deprezaed7f082020-11-04 15:11:59 +0100106 INFO(" Text region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100107 (void *)CACTUS_TEXT_START, (void *)CACTUS_TEXT_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100108
Olivier Deprezaed7f082020-11-04 15:11:59 +0100109 INFO(" Read-only data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100110 (void *)CACTUS_RODATA_START, (void *)CACTUS_RODATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100111
Olivier Deprezaed7f082020-11-04 15:11:59 +0100112 INFO(" Data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100113 (void *)CACTUS_DATA_START, (void *)CACTUS_DATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100114
Olivier Deprezaed7f082020-11-04 15:11:59 +0100115 INFO(" BSS region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100116 (void *)CACTUS_BSS_START, (void *)CACTUS_BSS_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100117
Olivier Deprezaed7f082020-11-04 15:11:59 +0100118 INFO(" RX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100119 (void *)get_sp_rx_start(vm_id),
120 (void *)get_sp_rx_end(vm_id));
Manish Pandey26c6f812020-04-30 11:43:00 +0100121
Olivier Deprezaed7f082020-11-04 15:11:59 +0100122 INFO(" TX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100123 (void *)get_sp_tx_start(vm_id),
124 (void *)get_sp_tx_end(vm_id));
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200125}
126
Manish Pandey26c6f812020-04-30 11:43:00 +0100127static void cactus_plat_configure_mmu(unsigned int vm_id)
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100128{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100129 mmap_add_region(CACTUS_TEXT_START,
130 CACTUS_TEXT_START,
131 CACTUS_TEXT_END - CACTUS_TEXT_START,
132 MT_CODE);
133 mmap_add_region(CACTUS_RODATA_START,
134 CACTUS_RODATA_START,
135 CACTUS_RODATA_END - CACTUS_RODATA_START,
136 MT_RO_DATA);
137 mmap_add_region(CACTUS_DATA_START,
138 CACTUS_DATA_START,
139 CACTUS_DATA_END - CACTUS_DATA_START,
140 MT_RW_DATA);
141 mmap_add_region(CACTUS_BSS_START,
142 CACTUS_BSS_START,
143 CACTUS_BSS_END - CACTUS_BSS_START,
144 MT_RW_DATA);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100145
Max Shvetsovc32f4782020-06-23 09:41:15 +0100146 mmap_add_region(get_sp_rx_start(vm_id),
147 get_sp_rx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100148 (CACTUS_RX_TX_SIZE / 2),
149 MT_RO_DATA);
150
Max Shvetsovc32f4782020-06-23 09:41:15 +0100151 mmap_add_region(get_sp_tx_start(vm_id),
152 get_sp_tx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100153 (CACTUS_RX_TX_SIZE / 2),
154 MT_RW_DATA);
155
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100156 mmap_add(cactus_mmap);
157 init_xlat_tables();
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100158}
159
Max Shvetsov2263efb2020-11-12 17:30:11 +0000160static void register_secondary_entrypoint(void)
161{
162 smc_args args;
163
164 args.fid = FFA_SECONDARY_EP_REGISTER_SMC64;
165 args.arg1 = (u_register_t)&secondary_cold_entry;
166
167 tftf_smc(&args);
168}
169
Olivier Deprez458d5532020-06-09 17:56:20 +0200170int tftf_irq_handler_dispatcher(void)
171{
172 ERROR("%s\n", __func__);
173
174 return 0;
175}
176
Max Shvetsov2263efb2020-11-12 17:30:11 +0000177void __dead2 cactus_main(bool primary_cold_boot)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200178{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100179 assert(IS_IN_EL1() != 0);
180
Max Shvetsovc32f4782020-06-23 09:41:15 +0100181 struct mailbox_buffers mb;
J-Alvesda6ac322020-11-09 15:45:30 +0000182
J-Alves7581c382020-05-07 18:34:20 +0100183 /* Get current FFA id */
184 smc_ret_values ffa_id_ret = ffa_id_get();
Manish Pandey87d4c702021-03-02 22:31:57 +0000185 ffa_vm_id_t ffa_id = (ffa_vm_id_t)(ffa_id_ret.ret2 & 0xffff);
J-Alves6cb21d92021-01-07 15:18:12 +0000186 if (ffa_func_id(ffa_id_ret) != FFA_SUCCESS_SMC32) {
J-Alves7581c382020-05-07 18:34:20 +0100187 ERROR("FFA_ID_GET failed.\n");
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100188 panic();
189 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100190
Max Shvetsov2263efb2020-11-12 17:30:11 +0000191 if (primary_cold_boot == true) {
192 /* Clear BSS */
193 memset((void *)CACTUS_BSS_START,
194 0, CACTUS_BSS_END - CACTUS_BSS_START);
195
Max Shvetsov2263efb2020-11-12 17:30:11 +0000196 mb.send = (void *) get_sp_tx_start(ffa_id);
197 mb.recv = (void *) get_sp_rx_start(ffa_id);
198
199 /* Configure and enable Stage-1 MMU, enable D-Cache */
200 cactus_plat_configure_mmu(ffa_id);
201 }
202
Manish Pandey87d4c702021-03-02 22:31:57 +0000203 /*
204 * The local ffa_id value is held on the stack. The global g_ffa_id
205 * value is set after BSS is cleared.
206 */
207 g_ffa_id = ffa_id;
208
Manish Pandey26c6f812020-04-30 11:43:00 +0100209 enable_mmu_el1(0);
210
Manish Pandeyd27b37d2021-03-02 14:41:58 +0000211 /* Enable IRQ/FIQ */
212 enable_irq();
213 enable_fiq();
214
Max Shvetsov2263efb2020-11-12 17:30:11 +0000215 if (primary_cold_boot == false) {
216 goto msg_loop;
217 }
218
J-Alves7581c382020-05-07 18:34:20 +0100219 if (ffa_id == SPM_VM_ID_FIRST) {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100220 console_init(CACTUS_PL011_UART_BASE,
221 CACTUS_PL011_UART_CLK_IN_HZ,
222 PL011_BAUDRATE);
Manish Pandey29495372020-04-09 15:19:26 +0100223
224 set_putc_impl(PL011_AS_STDOUT);
225
226 NOTICE("Booting Primary Cactus Secure Partition\n%s\n%s\n",
227 build_message, version_string);
Manish Pandey29495372020-04-09 15:19:26 +0100228 } else {
J-Alves83ede9b2020-11-02 17:37:19 +0000229 smc_ret_values ret;
Manish Pandey29495372020-04-09 15:19:26 +0100230 set_putc_impl(HVC_CALL_AS_STDOUT);
231
Olivier Deprezaed7f082020-11-04 15:11:59 +0100232 NOTICE("Booting Secondary Cactus Secure Partition (ID: %x)\n%s\n%s\n",
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100233 ffa_id, build_message, version_string);
234
Olivier Deprez0be4abe2020-08-04 11:26:13 +0200235 if (ffa_id == (SPM_VM_ID_FIRST + 2)) {
Olivier Deprezaed7f082020-11-04 15:11:59 +0100236 VERBOSE("Mapping RXTX Region\n");
J-Alves83ede9b2020-11-02 17:37:19 +0000237 CONFIGURE_AND_MAP_MAILBOX(mb, PAGE_SIZE, ret);
J-Alves6cb21d92021-01-07 15:18:12 +0000238 if (ffa_func_id(ret) != FFA_SUCCESS_SMC32) {
J-Alves83ede9b2020-11-02 17:37:19 +0000239 ERROR(
J-Alves6cb21d92021-01-07 15:18:12 +0000240 "Failed to map RXTX buffers. Error: %x\n",
241 ffa_error_code(ret));
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100242 panic();
243 }
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100244 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100245 }
Manish Pandey26c6f812020-04-30 11:43:00 +0100246
Olivier Deprezaed7f082020-11-04 15:11:59 +0100247 INFO("FF-A id: %x\n", ffa_id);
Manish Pandey26c6f812020-04-30 11:43:00 +0100248 cactus_print_memory_layout(ffa_id);
249
Max Shvetsov2263efb2020-11-12 17:30:11 +0000250 register_secondary_entrypoint();
251
J-Alves9f6f0142020-06-17 15:37:59 +0100252 /* Invoking Tests */
Max Shvetsovc32f4782020-06-23 09:41:15 +0100253 ffa_tests(&mb);
J-Alves9f6f0142020-06-17 15:37:59 +0100254
Max Shvetsov2263efb2020-11-12 17:30:11 +0000255msg_loop:
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100256 /* End up to message loop */
J-Alves63cdaa72020-10-08 17:22:45 +0100257 message_loop(ffa_id, &mb);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100258
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100259 /* Not reached */
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200260}