blob: 31906d597eebf400137f806d1a816d25072f10c9 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Olivier Deprezafcdb7c2019-11-29 14:21:48 +01002 * Copyright (c) 2018-2020, 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>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02009
10#include "cactus.h"
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +010011#include "cactus_def.h"
J-Alves9f6f0142020-06-17 15:37:59 +010012#include "cactus_tests.h"
13#include <debug.h>
14#include <drivers/arm/pl011.h>
15#include <drivers/console.h>
J-Alves5aecd982020-06-11 10:25:33 +010016#include <ffa_helpers.h>
J-Alves9f6f0142020-06-17 15:37:59 +010017#include <lib/aarch64/arch_helpers.h>
18#include <lib/xlat_tables/xlat_mmu_helpers.h>
19#include <lib/xlat_tables/xlat_tables_v2.h>
J-Alves5aecd982020-06-11 10:25:33 +010020#include <sp_helpers.h>
J-Alves9f6f0142020-06-17 15:37:59 +010021#include <std_svc.h>
22#include <plat/common/platform.h>
23#include <plat_arm.h>
24#include <platform_def.h>
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010025
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020026/* Host machine information injected by the build system in the ELF file. */
27extern const char build_message[];
28extern const char version_string[];
29
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010030/*
31 *
32 * Message loop function
33 * Notice we cannot use regular print functions because this serves to both
34 * "primary" and "secondary" VMs. Secondary VM cannot access UART directly
35 * but rather through Hafnium print hypercall.
36 *
37 */
J-Alves7581c382020-05-07 18:34:20 +010038static void __dead2 message_loop(ffa_vm_id_t vm_id)
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010039{
J-Alves7581c382020-05-07 18:34:20 +010040 smc_ret_values ffa_ret;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010041 uint32_t sp_response;
42
43 /*
44 * This initial wait call is necessary to inform SPMD that
45 * SP initialization has completed. It blocks until receiving
46 * a direct message request.
47 */
J-Alves7581c382020-05-07 18:34:20 +010048 ffa_ret = ffa_msg_wait();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010049
50 for (;;) {
51
J-Alves7581c382020-05-07 18:34:20 +010052 if (ffa_ret.ret0 != FFA_MSG_SEND_DIRECT_REQ_SMC32) {
53 ffa_ret = ffa_error(-1);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010054 continue;
55 }
56
J-Alves7581c382020-05-07 18:34:20 +010057 if (ffa_ret.ret1 != SP_ID(vm_id)) {
58 ffa_ret = ffa_error(-2);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010059 continue;
60 }
61
J-Alves7581c382020-05-07 18:34:20 +010062 if (ffa_ret.ret2 != HYP_ID) {
63 ffa_ret = ffa_error(-3);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010064 continue;
65 }
66
67 /*
68 * For the sake of testing, add the vm id to the
69 * received message.
70 */
J-Alves7581c382020-05-07 18:34:20 +010071 sp_response = ffa_ret.ret3 | vm_id;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010072
73 /*
74 * Send a response through direct messaging then block
75 * until receiving a new message request.
76 */
J-Alves7581c382020-05-07 18:34:20 +010077 ffa_ret = ffa_msg_send_direct_resp(SP_ID(vm_id),
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010078 HYP_ID, sp_response);
79 }
80}
81
82static const mmap_region_t cactus_mmap[] __attribute__((used)) = {
83 /* DEVICE0 area includes UART2 necessary to console */
84 MAP_REGION_FLAT(DEVICE0_BASE, DEVICE0_SIZE, MT_DEVICE | MT_RW),
85 {0}
86};
87
Manish Pandey26c6f812020-04-30 11:43:00 +010088static void cactus_print_memory_layout(unsigned int vm_id)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020089{
90 NOTICE("Secure Partition memory layout:\n");
91
Manish Pandey26c6f812020-04-30 11:43:00 +010092 NOTICE(" Text region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +010093 (void *)CACTUS_TEXT_START, (void *)CACTUS_TEXT_END);
Manish Pandey26c6f812020-04-30 11:43:00 +010094
95 NOTICE(" Read-only data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +010096 (void *)CACTUS_RODATA_START, (void *)CACTUS_RODATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +010097
98 NOTICE(" Data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +010099 (void *)CACTUS_DATA_START, (void *)CACTUS_DATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100100
101 NOTICE(" BSS region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100102 (void *)CACTUS_BSS_START, (void *)CACTUS_BSS_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100103
104 NOTICE(" RX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100105 (void *)get_sp_rx_start(vm_id),
106 (void *)get_sp_rx_end(vm_id));
Manish Pandey26c6f812020-04-30 11:43:00 +0100107
108 NOTICE(" TX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100109 (void *)get_sp_tx_start(vm_id),
110 (void *)get_sp_tx_end(vm_id));
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200111}
112
Manish Pandey26c6f812020-04-30 11:43:00 +0100113static void cactus_plat_configure_mmu(unsigned int vm_id)
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100114{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100115 mmap_add_region(CACTUS_TEXT_START,
116 CACTUS_TEXT_START,
117 CACTUS_TEXT_END - CACTUS_TEXT_START,
118 MT_CODE);
119 mmap_add_region(CACTUS_RODATA_START,
120 CACTUS_RODATA_START,
121 CACTUS_RODATA_END - CACTUS_RODATA_START,
122 MT_RO_DATA);
123 mmap_add_region(CACTUS_DATA_START,
124 CACTUS_DATA_START,
125 CACTUS_DATA_END - CACTUS_DATA_START,
126 MT_RW_DATA);
127 mmap_add_region(CACTUS_BSS_START,
128 CACTUS_BSS_START,
129 CACTUS_BSS_END - CACTUS_BSS_START,
130 MT_RW_DATA);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100131
Max Shvetsovc32f4782020-06-23 09:41:15 +0100132 mmap_add_region(get_sp_rx_start(vm_id),
133 get_sp_rx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100134 (CACTUS_RX_TX_SIZE / 2),
135 MT_RO_DATA);
136
Max Shvetsovc32f4782020-06-23 09:41:15 +0100137 mmap_add_region(get_sp_tx_start(vm_id),
138 get_sp_tx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100139 (CACTUS_RX_TX_SIZE / 2),
140 MT_RW_DATA);
141
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100142 mmap_add(cactus_mmap);
143 init_xlat_tables();
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100144}
145
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100146void __dead2 cactus_main(void)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200147{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100148 assert(IS_IN_EL1() != 0);
149
Max Shvetsovc32f4782020-06-23 09:41:15 +0100150 struct mailbox_buffers mb;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100151 /* Clear BSS */
152 memset((void *)CACTUS_BSS_START,
153 0, CACTUS_BSS_END - CACTUS_BSS_START);
154
J-Alves7581c382020-05-07 18:34:20 +0100155 /* Get current FFA id */
156 smc_ret_values ffa_id_ret = ffa_id_get();
157 if (ffa_id_ret.ret0 != FFA_SUCCESS_SMC32) {
158 ERROR("FFA_ID_GET failed.\n");
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100159 panic();
160 }
161
J-Alves7581c382020-05-07 18:34:20 +0100162 ffa_vm_id_t ffa_id = ffa_id_ret.ret2 & 0xffff;
Max Shvetsovc32f4782020-06-23 09:41:15 +0100163 mb.send = (void *) get_sp_tx_start(ffa_id);
164 mb.recv = (void *) get_sp_rx_start(ffa_id);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100165
Manish Pandey26c6f812020-04-30 11:43:00 +0100166 /* Configure and enable Stage-1 MMU, enable D-Cache */
167 cactus_plat_configure_mmu(ffa_id);
168 enable_mmu_el1(0);
169
J-Alves7581c382020-05-07 18:34:20 +0100170 if (ffa_id == SPM_VM_ID_FIRST) {
Manish Pandey29495372020-04-09 15:19:26 +0100171 console_init(PL011_UART2_BASE,
172 PL011_UART2_CLK_IN_HZ,
173 PL011_BAUDRATE);
174
175 set_putc_impl(PL011_AS_STDOUT);
176
177 NOTICE("Booting Primary Cactus Secure Partition\n%s\n%s\n",
178 build_message, version_string);
179
Manish Pandey29495372020-04-09 15:19:26 +0100180 /* Get number of VMs */
181 NOTICE("VM count: %u\n", spm_vm_get_count());
182
183 /* Get virtual CPU count for current VM */
J-Alves7581c382020-05-07 18:34:20 +0100184 NOTICE("vCPU count: %u\n", spm_vcpu_get_count(ffa_id));
Manish Pandey29495372020-04-09 15:19:26 +0100185 } else {
186 set_putc_impl(HVC_CALL_AS_STDOUT);
187
188 NOTICE("Booting Secondary Cactus Secure Partition\n%s\n%s\n",
189 build_message, version_string);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100190 }
Manish Pandey26c6f812020-04-30 11:43:00 +0100191
192 NOTICE("FFA id: %u\n", ffa_id);
193 cactus_print_memory_layout(ffa_id);
194
J-Alves9f6f0142020-06-17 15:37:59 +0100195 /* Invoking Tests */
Max Shvetsovc32f4782020-06-23 09:41:15 +0100196 ffa_tests(&mb);
J-Alves9f6f0142020-06-17 15:37:59 +0100197
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100198 /* End up to message loop */
J-Alves7581c382020-05-07 18:34:20 +0100199 message_loop(ffa_id);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100200
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100201 /* Not reached */
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200202}