blob: 45d2db03974e4858fe8357978e6defd0f49c7151 [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),
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010096 {0}
97};
98
Manish Pandey26c6f812020-04-30 11:43:00 +010099static void cactus_print_memory_layout(unsigned int vm_id)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200100{
Olivier Deprezaed7f082020-11-04 15:11:59 +0100101 INFO("Secure Partition memory layout:\n");
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200102
Olivier Deprezaed7f082020-11-04 15:11:59 +0100103 INFO(" Text region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100104 (void *)CACTUS_TEXT_START, (void *)CACTUS_TEXT_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100105
Olivier Deprezaed7f082020-11-04 15:11:59 +0100106 INFO(" Read-only data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100107 (void *)CACTUS_RODATA_START, (void *)CACTUS_RODATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100108
Olivier Deprezaed7f082020-11-04 15:11:59 +0100109 INFO(" Data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100110 (void *)CACTUS_DATA_START, (void *)CACTUS_DATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100111
Olivier Deprezaed7f082020-11-04 15:11:59 +0100112 INFO(" BSS region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100113 (void *)CACTUS_BSS_START, (void *)CACTUS_BSS_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100114
Olivier Deprezaed7f082020-11-04 15:11:59 +0100115 INFO(" RX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100116 (void *)get_sp_rx_start(vm_id),
117 (void *)get_sp_rx_end(vm_id));
Manish Pandey26c6f812020-04-30 11:43:00 +0100118
Olivier Deprezaed7f082020-11-04 15:11:59 +0100119 INFO(" TX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100120 (void *)get_sp_tx_start(vm_id),
121 (void *)get_sp_tx_end(vm_id));
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200122}
123
Manish Pandey26c6f812020-04-30 11:43:00 +0100124static void cactus_plat_configure_mmu(unsigned int vm_id)
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100125{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100126 mmap_add_region(CACTUS_TEXT_START,
127 CACTUS_TEXT_START,
128 CACTUS_TEXT_END - CACTUS_TEXT_START,
129 MT_CODE);
130 mmap_add_region(CACTUS_RODATA_START,
131 CACTUS_RODATA_START,
132 CACTUS_RODATA_END - CACTUS_RODATA_START,
133 MT_RO_DATA);
134 mmap_add_region(CACTUS_DATA_START,
135 CACTUS_DATA_START,
136 CACTUS_DATA_END - CACTUS_DATA_START,
137 MT_RW_DATA);
138 mmap_add_region(CACTUS_BSS_START,
139 CACTUS_BSS_START,
140 CACTUS_BSS_END - CACTUS_BSS_START,
141 MT_RW_DATA);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100142
Max Shvetsovc32f4782020-06-23 09:41:15 +0100143 mmap_add_region(get_sp_rx_start(vm_id),
144 get_sp_rx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100145 (CACTUS_RX_TX_SIZE / 2),
146 MT_RO_DATA);
147
Max Shvetsovc32f4782020-06-23 09:41:15 +0100148 mmap_add_region(get_sp_tx_start(vm_id),
149 get_sp_tx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100150 (CACTUS_RX_TX_SIZE / 2),
151 MT_RW_DATA);
152
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100153 mmap_add(cactus_mmap);
154 init_xlat_tables();
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100155}
156
Max Shvetsov2263efb2020-11-12 17:30:11 +0000157static void register_secondary_entrypoint(void)
158{
159 smc_args args;
160
161 args.fid = FFA_SECONDARY_EP_REGISTER_SMC64;
162 args.arg1 = (u_register_t)&secondary_cold_entry;
163
164 tftf_smc(&args);
165}
166
Olivier Deprez458d5532020-06-09 17:56:20 +0200167int tftf_irq_handler_dispatcher(void)
168{
169 ERROR("%s\n", __func__);
170
171 return 0;
172}
173
Max Shvetsov2263efb2020-11-12 17:30:11 +0000174void __dead2 cactus_main(bool primary_cold_boot)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200175{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100176 assert(IS_IN_EL1() != 0);
177
Max Shvetsovc32f4782020-06-23 09:41:15 +0100178 struct mailbox_buffers mb;
J-Alvesda6ac322020-11-09 15:45:30 +0000179
J-Alves7581c382020-05-07 18:34:20 +0100180 /* Get current FFA id */
181 smc_ret_values ffa_id_ret = ffa_id_get();
Manish Pandey87d4c702021-03-02 22:31:57 +0000182 ffa_vm_id_t ffa_id = (ffa_vm_id_t)(ffa_id_ret.ret2 & 0xffff);
J-Alves6cb21d92021-01-07 15:18:12 +0000183 if (ffa_func_id(ffa_id_ret) != FFA_SUCCESS_SMC32) {
J-Alves7581c382020-05-07 18:34:20 +0100184 ERROR("FFA_ID_GET failed.\n");
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100185 panic();
186 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100187
Max Shvetsov2263efb2020-11-12 17:30:11 +0000188 if (primary_cold_boot == true) {
189 /* Clear BSS */
190 memset((void *)CACTUS_BSS_START,
191 0, CACTUS_BSS_END - CACTUS_BSS_START);
192
Max Shvetsov2263efb2020-11-12 17:30:11 +0000193 mb.send = (void *) get_sp_tx_start(ffa_id);
194 mb.recv = (void *) get_sp_rx_start(ffa_id);
195
196 /* Configure and enable Stage-1 MMU, enable D-Cache */
197 cactus_plat_configure_mmu(ffa_id);
198 }
199
Manish Pandey87d4c702021-03-02 22:31:57 +0000200 /*
201 * The local ffa_id value is held on the stack. The global g_ffa_id
202 * value is set after BSS is cleared.
203 */
204 g_ffa_id = ffa_id;
205
Manish Pandey26c6f812020-04-30 11:43:00 +0100206 enable_mmu_el1(0);
207
Manish Pandeyd27b37d2021-03-02 14:41:58 +0000208 /* Enable IRQ/FIQ */
209 enable_irq();
210 enable_fiq();
211
Max Shvetsov2263efb2020-11-12 17:30:11 +0000212 if (primary_cold_boot == false) {
213 goto msg_loop;
214 }
215
J-Alves7581c382020-05-07 18:34:20 +0100216 if (ffa_id == SPM_VM_ID_FIRST) {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100217 console_init(CACTUS_PL011_UART_BASE,
218 CACTUS_PL011_UART_CLK_IN_HZ,
219 PL011_BAUDRATE);
Manish Pandey29495372020-04-09 15:19:26 +0100220
221 set_putc_impl(PL011_AS_STDOUT);
222
223 NOTICE("Booting Primary Cactus Secure Partition\n%s\n%s\n",
224 build_message, version_string);
Manish Pandey29495372020-04-09 15:19:26 +0100225 } else {
J-Alves83ede9b2020-11-02 17:37:19 +0000226 smc_ret_values ret;
Manish Pandey29495372020-04-09 15:19:26 +0100227 set_putc_impl(HVC_CALL_AS_STDOUT);
228
Olivier Deprezaed7f082020-11-04 15:11:59 +0100229 NOTICE("Booting Secondary Cactus Secure Partition (ID: %x)\n%s\n%s\n",
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100230 ffa_id, build_message, version_string);
231
Olivier Deprez0be4abe2020-08-04 11:26:13 +0200232 if (ffa_id == (SPM_VM_ID_FIRST + 2)) {
Olivier Deprezaed7f082020-11-04 15:11:59 +0100233 VERBOSE("Mapping RXTX Region\n");
J-Alves83ede9b2020-11-02 17:37:19 +0000234 CONFIGURE_AND_MAP_MAILBOX(mb, PAGE_SIZE, ret);
J-Alves6cb21d92021-01-07 15:18:12 +0000235 if (ffa_func_id(ret) != FFA_SUCCESS_SMC32) {
J-Alves83ede9b2020-11-02 17:37:19 +0000236 ERROR(
J-Alves6cb21d92021-01-07 15:18:12 +0000237 "Failed to map RXTX buffers. Error: %x\n",
238 ffa_error_code(ret));
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100239 panic();
240 }
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100241 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100242 }
Manish Pandey26c6f812020-04-30 11:43:00 +0100243
Olivier Deprezaed7f082020-11-04 15:11:59 +0100244 INFO("FF-A id: %x\n", ffa_id);
Manish Pandey26c6f812020-04-30 11:43:00 +0100245 cactus_print_memory_layout(ffa_id);
246
Max Shvetsov2263efb2020-11-12 17:30:11 +0000247 register_secondary_entrypoint();
248
J-Alves9f6f0142020-06-17 15:37:59 +0100249 /* Invoking Tests */
Max Shvetsovc32f4782020-06-23 09:41:15 +0100250 ffa_tests(&mb);
J-Alves9f6f0142020-06-17 15:37:59 +0100251
Max Shvetsov2263efb2020-11-12 17:30:11 +0000252msg_loop:
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100253 /* End up to message loop */
J-Alves63cdaa72020-10-08 17:22:45 +0100254 message_loop(ffa_id, &mb);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100255
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100256 /* Not reached */
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200257}