blob: ed48fe46cde98aef7530895571c2af910ba78fa3 [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
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010036/*
37 *
38 * Message loop function
39 * Notice we cannot use regular print functions because this serves to both
40 * "primary" and "secondary" VMs. Secondary VM cannot access UART directly
41 * but rather through Hafnium print hypercall.
42 *
43 */
J-Alves0e1e7ca2021-01-25 14:11:06 +000044
J-Alves63cdaa72020-10-08 17:22:45 +010045static void __dead2 message_loop(ffa_vm_id_t vm_id, struct mailbox_buffers *mb)
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010046{
J-Alves7581c382020-05-07 18:34:20 +010047 smc_ret_values ffa_ret;
J-Alvesda6ac322020-11-09 15:45:30 +000048 ffa_vm_id_t destination;
J-Alves1d203f12020-11-11 11:38:49 +000049
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010050 /*
J-Alves63cdaa72020-10-08 17:22:45 +010051 * This initial wait call is necessary to inform SPMD that
52 * SP initialization has completed. It blocks until receiving
53 * a direct message request.
54 */
55
J-Alves7581c382020-05-07 18:34:20 +010056 ffa_ret = ffa_msg_wait();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010057
58 for (;;) {
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-Alves4cb9dee2021-03-03 13:59:52 +000081 if (!cactus_handle_cmd(&ffa_ret, &ffa_ret, mb)) {
J-Alves63cdaa72020-10-08 17:22:45 +010082 break;
83 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010084 }
Olivier Deprez73d81cf2020-09-15 16:57:00 +020085
86 panic();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010087}
88
89static const mmap_region_t cactus_mmap[] __attribute__((used)) = {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +010090 /* PLAT_ARM_DEVICE0 area includes UART2 necessary to console */
91 MAP_REGION_FLAT(PLAT_ARM_DEVICE0_BASE, PLAT_ARM_DEVICE0_SIZE,
92 MT_DEVICE | MT_RW),
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010093 {0}
94};
95
Manish Pandey26c6f812020-04-30 11:43:00 +010096static void cactus_print_memory_layout(unsigned int vm_id)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020097{
Olivier Deprezaed7f082020-11-04 15:11:59 +010098 INFO("Secure Partition memory layout:\n");
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020099
Olivier Deprezaed7f082020-11-04 15:11:59 +0100100 INFO(" Text region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100101 (void *)CACTUS_TEXT_START, (void *)CACTUS_TEXT_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100102
Olivier Deprezaed7f082020-11-04 15:11:59 +0100103 INFO(" Read-only data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100104 (void *)CACTUS_RODATA_START, (void *)CACTUS_RODATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100105
Olivier Deprezaed7f082020-11-04 15:11:59 +0100106 INFO(" Data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100107 (void *)CACTUS_DATA_START, (void *)CACTUS_DATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100108
Olivier Deprezaed7f082020-11-04 15:11:59 +0100109 INFO(" BSS region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100110 (void *)CACTUS_BSS_START, (void *)CACTUS_BSS_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100111
Olivier Deprezaed7f082020-11-04 15:11:59 +0100112 INFO(" RX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100113 (void *)get_sp_rx_start(vm_id),
114 (void *)get_sp_rx_end(vm_id));
Manish Pandey26c6f812020-04-30 11:43:00 +0100115
Olivier Deprezaed7f082020-11-04 15:11:59 +0100116 INFO(" TX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100117 (void *)get_sp_tx_start(vm_id),
118 (void *)get_sp_tx_end(vm_id));
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200119}
120
Manish Pandey26c6f812020-04-30 11:43:00 +0100121static void cactus_plat_configure_mmu(unsigned int vm_id)
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100122{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100123 mmap_add_region(CACTUS_TEXT_START,
124 CACTUS_TEXT_START,
125 CACTUS_TEXT_END - CACTUS_TEXT_START,
126 MT_CODE);
127 mmap_add_region(CACTUS_RODATA_START,
128 CACTUS_RODATA_START,
129 CACTUS_RODATA_END - CACTUS_RODATA_START,
130 MT_RO_DATA);
131 mmap_add_region(CACTUS_DATA_START,
132 CACTUS_DATA_START,
133 CACTUS_DATA_END - CACTUS_DATA_START,
134 MT_RW_DATA);
135 mmap_add_region(CACTUS_BSS_START,
136 CACTUS_BSS_START,
137 CACTUS_BSS_END - CACTUS_BSS_START,
138 MT_RW_DATA);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100139
Max Shvetsovc32f4782020-06-23 09:41:15 +0100140 mmap_add_region(get_sp_rx_start(vm_id),
141 get_sp_rx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100142 (CACTUS_RX_TX_SIZE / 2),
143 MT_RO_DATA);
144
Max Shvetsovc32f4782020-06-23 09:41:15 +0100145 mmap_add_region(get_sp_tx_start(vm_id),
146 get_sp_tx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100147 (CACTUS_RX_TX_SIZE / 2),
148 MT_RW_DATA);
149
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100150 mmap_add(cactus_mmap);
151 init_xlat_tables();
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100152}
153
Max Shvetsov2263efb2020-11-12 17:30:11 +0000154static void register_secondary_entrypoint(void)
155{
156 smc_args args;
157
158 args.fid = FFA_SECONDARY_EP_REGISTER_SMC64;
159 args.arg1 = (u_register_t)&secondary_cold_entry;
160
161 tftf_smc(&args);
162}
163
Olivier Deprez458d5532020-06-09 17:56:20 +0200164int tftf_irq_handler_dispatcher(void)
165{
166 ERROR("%s\n", __func__);
167
168 return 0;
169}
170
Max Shvetsov2263efb2020-11-12 17:30:11 +0000171void __dead2 cactus_main(bool primary_cold_boot)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200172{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100173 assert(IS_IN_EL1() != 0);
174
Max Shvetsovc32f4782020-06-23 09:41:15 +0100175 struct mailbox_buffers mb;
J-Alvesda6ac322020-11-09 15:45:30 +0000176
J-Alves7581c382020-05-07 18:34:20 +0100177 /* Get current FFA id */
178 smc_ret_values ffa_id_ret = ffa_id_get();
J-Alves6cb21d92021-01-07 15:18:12 +0000179 if (ffa_func_id(ffa_id_ret) != FFA_SUCCESS_SMC32) {
J-Alves7581c382020-05-07 18:34:20 +0100180 ERROR("FFA_ID_GET failed.\n");
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100181 panic();
182 }
J-Alves7581c382020-05-07 18:34:20 +0100183 ffa_vm_id_t ffa_id = ffa_id_ret.ret2 & 0xffff;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100184
Max Shvetsov2263efb2020-11-12 17:30:11 +0000185 if (primary_cold_boot == true) {
186 /* Clear BSS */
187 memset((void *)CACTUS_BSS_START,
188 0, CACTUS_BSS_END - CACTUS_BSS_START);
189
190
191 mb.send = (void *) get_sp_tx_start(ffa_id);
192 mb.recv = (void *) get_sp_rx_start(ffa_id);
193
194 /* Configure and enable Stage-1 MMU, enable D-Cache */
195 cactus_plat_configure_mmu(ffa_id);
196 }
197
Manish Pandey26c6f812020-04-30 11:43:00 +0100198 enable_mmu_el1(0);
199
Max Shvetsov2263efb2020-11-12 17:30:11 +0000200 if (primary_cold_boot == false) {
201 goto msg_loop;
202 }
203
J-Alves7581c382020-05-07 18:34:20 +0100204 if (ffa_id == SPM_VM_ID_FIRST) {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100205 console_init(CACTUS_PL011_UART_BASE,
206 CACTUS_PL011_UART_CLK_IN_HZ,
207 PL011_BAUDRATE);
Manish Pandey29495372020-04-09 15:19:26 +0100208
209 set_putc_impl(PL011_AS_STDOUT);
210
211 NOTICE("Booting Primary Cactus Secure Partition\n%s\n%s\n",
212 build_message, version_string);
Manish Pandey29495372020-04-09 15:19:26 +0100213 } else {
J-Alves83ede9b2020-11-02 17:37:19 +0000214 smc_ret_values ret;
Manish Pandey29495372020-04-09 15:19:26 +0100215 set_putc_impl(HVC_CALL_AS_STDOUT);
216
Olivier Deprezaed7f082020-11-04 15:11:59 +0100217 NOTICE("Booting Secondary Cactus Secure Partition (ID: %x)\n%s\n%s\n",
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100218 ffa_id, build_message, version_string);
219
Olivier Deprez0be4abe2020-08-04 11:26:13 +0200220 if (ffa_id == (SPM_VM_ID_FIRST + 2)) {
Olivier Deprezaed7f082020-11-04 15:11:59 +0100221 VERBOSE("Mapping RXTX Region\n");
J-Alves83ede9b2020-11-02 17:37:19 +0000222 CONFIGURE_AND_MAP_MAILBOX(mb, PAGE_SIZE, ret);
J-Alves6cb21d92021-01-07 15:18:12 +0000223 if (ffa_func_id(ret) != FFA_SUCCESS_SMC32) {
J-Alves83ede9b2020-11-02 17:37:19 +0000224 ERROR(
J-Alves6cb21d92021-01-07 15:18:12 +0000225 "Failed to map RXTX buffers. Error: %x\n",
226 ffa_error_code(ret));
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100227 panic();
228 }
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100229 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100230 }
Manish Pandey26c6f812020-04-30 11:43:00 +0100231
Olivier Deprezaed7f082020-11-04 15:11:59 +0100232 INFO("FF-A id: %x\n", ffa_id);
Manish Pandey26c6f812020-04-30 11:43:00 +0100233 cactus_print_memory_layout(ffa_id);
234
Max Shvetsov2263efb2020-11-12 17:30:11 +0000235 register_secondary_entrypoint();
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
Max Shvetsov2263efb2020-11-12 17:30:11 +0000240msg_loop:
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100241 /* End up to message loop */
J-Alves63cdaa72020-10-08 17:22:45 +0100242 message_loop(ffa_id, &mb);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100243
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100244 /* Not reached */
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200245}