blob: 78bb8fc67cfc5140055da266d6df091724e4342e [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>
Ruari Phippsddc661a2020-09-10 09:06:14 +010019
Max Shvetsov103e0562021-02-04 16:58:31 +000020#include <plat_arm.h>
21#include <plat/common/platform.h>
22#include <platform_def.h>
Ruari Phippsddc661a2020-09-10 09:06:14 +010023#include <sp_debug.h>
J-Alves5aecd982020-06-11 10:25:33 +010024#include <sp_helpers.h>
Olivier Deprez6967c242021-04-09 09:24:08 +020025#include <spm_helpers.h>
J-Alves9f6f0142020-06-17 15:37:59 +010026#include <std_svc.h>
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010027
Max Shvetsov103e0562021-02-04 16:58:31 +000028#include "cactus_def.h"
29#include "cactus_tests.h"
30#include "cactus.h"
J-Alves63cdaa72020-10-08 17:22:45 +010031
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020032/* Host machine information injected by the build system in the ELF file. */
33extern const char build_message[];
34extern const char version_string[];
35
Max Shvetsov2263efb2020-11-12 17:30:11 +000036extern void secondary_cold_entry(void);
37
Manish Pandey87d4c702021-03-02 22:31:57 +000038/* Global ffa_id */
Daniel Boulbye79d2072021-03-03 11:34:53 +000039ffa_id_t g_ffa_id;
Manish Pandey87d4c702021-03-02 22:31:57 +000040
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010041/*
42 *
43 * Message loop function
44 * Notice we cannot use regular print functions because this serves to both
45 * "primary" and "secondary" VMs. Secondary VM cannot access UART directly
46 * but rather through Hafnium print hypercall.
47 *
48 */
J-Alves0e1e7ca2021-01-25 14:11:06 +000049
Daniel Boulbye79d2072021-03-03 11:34:53 +000050static void __dead2 message_loop(ffa_id_t vm_id, struct mailbox_buffers *mb)
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010051{
J-Alves7581c382020-05-07 18:34:20 +010052 smc_ret_values ffa_ret;
Daniel Boulbye79d2072021-03-03 11:34:53 +000053 ffa_id_t destination;
J-Alves1d203f12020-11-11 11:38:49 +000054
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010055 /*
J-Alves63cdaa72020-10-08 17:22:45 +010056 * This initial wait call is necessary to inform SPMD that
57 * SP initialization has completed. It blocks until receiving
58 * a direct message request.
59 */
60
J-Alves7581c382020-05-07 18:34:20 +010061 ffa_ret = ffa_msg_wait();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010062
63 for (;;) {
J-Alves6cb21d92021-01-07 15:18:12 +000064 VERBOSE("Woke up with func id: %x\n", ffa_func_id(ffa_ret));
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010065
J-Alves6cb21d92021-01-07 15:18:12 +000066 if (ffa_func_id(ffa_ret) == FFA_ERROR) {
67 ERROR("Error: %x\n", ffa_error_code(ffa_ret));
J-Alvesda6ac322020-11-09 15:45:30 +000068 break;
69 }
70
J-Alves6cb21d92021-01-07 15:18:12 +000071 if (ffa_func_id(ffa_ret) != FFA_MSG_SEND_DIRECT_REQ_SMC32 &&
72 ffa_func_id(ffa_ret) != FFA_MSG_SEND_DIRECT_REQ_SMC64) {
73 ERROR("%s(%u) unknown func id 0x%x\n",
74 __func__, vm_id, ffa_func_id(ffa_ret));
Olivier Deprez73d81cf2020-09-15 16:57:00 +020075 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010076 }
77
J-Alves6cb21d92021-01-07 15:18:12 +000078 destination = ffa_dir_msg_dest(ffa_ret);
J-Alvesda6ac322020-11-09 15:45:30 +000079
J-Alvesda6ac322020-11-09 15:45:30 +000080 if (destination != vm_id) {
J-Alves6cb21d92021-01-07 15:18:12 +000081 ERROR("%s(%u) invalid vm id 0x%x\n",
82 __func__, vm_id, destination);
Olivier Deprez73d81cf2020-09-15 16:57:00 +020083 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010084 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010085
J-Alves4cb9dee2021-03-03 13:59:52 +000086 if (!cactus_handle_cmd(&ffa_ret, &ffa_ret, mb)) {
J-Alves63cdaa72020-10-08 17:22:45 +010087 break;
88 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010089 }
Olivier Deprez73d81cf2020-09-15 16:57:00 +020090
91 panic();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010092}
93
94static const mmap_region_t cactus_mmap[] __attribute__((used)) = {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +010095 /* PLAT_ARM_DEVICE0 area includes UART2 necessary to console */
96 MAP_REGION_FLAT(PLAT_ARM_DEVICE0_BASE, PLAT_ARM_DEVICE0_SIZE,
97 MT_DEVICE | MT_RW),
Madhukar Pappireddy172523b2020-12-31 19:25:33 -060098 /* scratch memory allocated to be used for running SMMU tests */
99 MAP_REGION_FLAT(PLAT_CACTUS_MEMCPY_BASE, PLAT_CACTUS_MEMCPY_RANGE,
100 MT_MEMORY | MT_RW),
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100101 {0}
102};
103
Manish Pandey26c6f812020-04-30 11:43:00 +0100104static void cactus_print_memory_layout(unsigned int vm_id)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200105{
Olivier Deprezaed7f082020-11-04 15:11:59 +0100106 INFO("Secure Partition memory layout:\n");
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200107
Olivier Deprezaed7f082020-11-04 15:11:59 +0100108 INFO(" Text region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100109 (void *)CACTUS_TEXT_START, (void *)CACTUS_TEXT_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100110
Olivier Deprezaed7f082020-11-04 15:11:59 +0100111 INFO(" Read-only data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100112 (void *)CACTUS_RODATA_START, (void *)CACTUS_RODATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100113
Olivier Deprezaed7f082020-11-04 15:11:59 +0100114 INFO(" Data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100115 (void *)CACTUS_DATA_START, (void *)CACTUS_DATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100116
Olivier Deprezaed7f082020-11-04 15:11:59 +0100117 INFO(" BSS region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100118 (void *)CACTUS_BSS_START, (void *)CACTUS_BSS_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100119
Olivier Deprezaed7f082020-11-04 15:11:59 +0100120 INFO(" RX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100121 (void *)get_sp_rx_start(vm_id),
122 (void *)get_sp_rx_end(vm_id));
Manish Pandey26c6f812020-04-30 11:43:00 +0100123
Olivier Deprezaed7f082020-11-04 15:11:59 +0100124 INFO(" TX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100125 (void *)get_sp_tx_start(vm_id),
126 (void *)get_sp_tx_end(vm_id));
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200127}
128
Manish Pandey26c6f812020-04-30 11:43:00 +0100129static void cactus_plat_configure_mmu(unsigned int vm_id)
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100130{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100131 mmap_add_region(CACTUS_TEXT_START,
132 CACTUS_TEXT_START,
133 CACTUS_TEXT_END - CACTUS_TEXT_START,
134 MT_CODE);
135 mmap_add_region(CACTUS_RODATA_START,
136 CACTUS_RODATA_START,
137 CACTUS_RODATA_END - CACTUS_RODATA_START,
138 MT_RO_DATA);
139 mmap_add_region(CACTUS_DATA_START,
140 CACTUS_DATA_START,
141 CACTUS_DATA_END - CACTUS_DATA_START,
142 MT_RW_DATA);
143 mmap_add_region(CACTUS_BSS_START,
144 CACTUS_BSS_START,
145 CACTUS_BSS_END - CACTUS_BSS_START,
146 MT_RW_DATA);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100147
Max Shvetsovc32f4782020-06-23 09:41:15 +0100148 mmap_add_region(get_sp_rx_start(vm_id),
149 get_sp_rx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100150 (CACTUS_RX_TX_SIZE / 2),
151 MT_RO_DATA);
152
Max Shvetsovc32f4782020-06-23 09:41:15 +0100153 mmap_add_region(get_sp_tx_start(vm_id),
154 get_sp_tx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100155 (CACTUS_RX_TX_SIZE / 2),
156 MT_RW_DATA);
157
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100158 mmap_add(cactus_mmap);
159 init_xlat_tables();
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100160}
161
Max Shvetsov2263efb2020-11-12 17:30:11 +0000162static void register_secondary_entrypoint(void)
163{
164 smc_args args;
165
166 args.fid = FFA_SECONDARY_EP_REGISTER_SMC64;
167 args.arg1 = (u_register_t)&secondary_cold_entry;
168
169 tftf_smc(&args);
170}
171
Olivier Deprez458d5532020-06-09 17:56:20 +0200172int tftf_irq_handler_dispatcher(void)
173{
174 ERROR("%s\n", __func__);
175
176 return 0;
177}
178
Max Shvetsov2263efb2020-11-12 17:30:11 +0000179void __dead2 cactus_main(bool primary_cold_boot)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200180{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100181 assert(IS_IN_EL1() != 0);
182
Max Shvetsovc32f4782020-06-23 09:41:15 +0100183 struct mailbox_buffers mb;
Olivier Deprez24bd1702021-10-05 14:35:17 +0200184 smc_ret_values ret;
J-Alvesda6ac322020-11-09 15:45:30 +0000185
J-Alves7581c382020-05-07 18:34:20 +0100186 /* Get current FFA id */
187 smc_ret_values ffa_id_ret = ffa_id_get();
Daniel Boulby198deda2021-03-03 11:35:25 +0000188 ffa_id_t ffa_id = ffa_endpoint_id(ffa_id_ret);
J-Alves6cb21d92021-01-07 15:18:12 +0000189 if (ffa_func_id(ffa_id_ret) != FFA_SUCCESS_SMC32) {
J-Alves7581c382020-05-07 18:34:20 +0100190 ERROR("FFA_ID_GET failed.\n");
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100191 panic();
192 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100193
Max Shvetsov2263efb2020-11-12 17:30:11 +0000194 if (primary_cold_boot == true) {
195 /* Clear BSS */
196 memset((void *)CACTUS_BSS_START,
197 0, CACTUS_BSS_END - CACTUS_BSS_START);
198
Max Shvetsov2263efb2020-11-12 17:30:11 +0000199 mb.send = (void *) get_sp_tx_start(ffa_id);
200 mb.recv = (void *) get_sp_rx_start(ffa_id);
201
202 /* Configure and enable Stage-1 MMU, enable D-Cache */
203 cactus_plat_configure_mmu(ffa_id);
204 }
205
Manish Pandey87d4c702021-03-02 22:31:57 +0000206 /*
207 * The local ffa_id value is held on the stack. The global g_ffa_id
208 * value is set after BSS is cleared.
209 */
210 g_ffa_id = ffa_id;
211
Manish Pandey26c6f812020-04-30 11:43:00 +0100212 enable_mmu_el1(0);
213
Manish Pandeyd27b37d2021-03-02 14:41:58 +0000214 /* Enable IRQ/FIQ */
215 enable_irq();
216 enable_fiq();
217
Max Shvetsov2263efb2020-11-12 17:30:11 +0000218 if (primary_cold_boot == false) {
219 goto msg_loop;
220 }
221
J-Alves7581c382020-05-07 18:34:20 +0100222 if (ffa_id == SPM_VM_ID_FIRST) {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100223 console_init(CACTUS_PL011_UART_BASE,
224 CACTUS_PL011_UART_CLK_IN_HZ,
225 PL011_BAUDRATE);
Manish Pandey29495372020-04-09 15:19:26 +0100226
227 set_putc_impl(PL011_AS_STDOUT);
228
Manish Pandey29495372020-04-09 15:19:26 +0100229 } else {
230 set_putc_impl(HVC_CALL_AS_STDOUT);
Olivier Deprez24bd1702021-10-05 14:35:17 +0200231 }
Manish Pandey29495372020-04-09 15:19:26 +0100232
Olivier Deprez24bd1702021-10-05 14:35:17 +0200233 /* Below string is monitored by CI expect script. */
234 NOTICE("Booting Secure Partition (ID: %x)\n%s\n%s\n",
235 ffa_id, build_message, version_string);
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100236
Olivier Deprez24bd1702021-10-05 14:35:17 +0200237 if (ffa_id == (SPM_VM_ID_FIRST + 2)) {
238 VERBOSE("Mapping RXTX Region\n");
239 CONFIGURE_AND_MAP_MAILBOX(mb, PAGE_SIZE, ret);
240 if (ffa_func_id(ret) != FFA_SUCCESS_SMC32) {
241 ERROR(
242 "Failed to map RXTX buffers. Error: %x\n",
243 ffa_error_code(ret));
244 panic();
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100245 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100246 }
Manish Pandey26c6f812020-04-30 11:43:00 +0100247
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}