blob: 1508d98392d13047f103315ddcf1ddc5b24b678b [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-Alves7581c382020-05-07 18:34:20 +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>
20#include <std_svc.h>
21#include <plat/common/platform.h>
22#include <plat_arm.h>
23#include <platform_def.h>
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010024
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020025/* Host machine information injected by the build system in the ELF file. */
26extern const char build_message[];
27extern const char version_string[];
28
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010029/*
30 *
31 * Message loop function
32 * Notice we cannot use regular print functions because this serves to both
33 * "primary" and "secondary" VMs. Secondary VM cannot access UART directly
34 * but rather through Hafnium print hypercall.
35 *
36 */
J-Alves7581c382020-05-07 18:34:20 +010037static void __dead2 message_loop(ffa_vm_id_t vm_id)
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010038{
J-Alves7581c382020-05-07 18:34:20 +010039 smc_ret_values ffa_ret;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010040 uint32_t sp_response;
41
42 /*
43 * This initial wait call is necessary to inform SPMD that
44 * SP initialization has completed. It blocks until receiving
45 * a direct message request.
46 */
J-Alves7581c382020-05-07 18:34:20 +010047 ffa_ret = ffa_msg_wait();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010048
49 for (;;) {
50
J-Alves7581c382020-05-07 18:34:20 +010051 if (ffa_ret.ret0 != FFA_MSG_SEND_DIRECT_REQ_SMC32) {
52 ffa_ret = ffa_error(-1);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010053 continue;
54 }
55
J-Alves7581c382020-05-07 18:34:20 +010056 if (ffa_ret.ret1 != SP_ID(vm_id)) {
57 ffa_ret = ffa_error(-2);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010058 continue;
59 }
60
J-Alves7581c382020-05-07 18:34:20 +010061 if (ffa_ret.ret2 != HYP_ID) {
62 ffa_ret = ffa_error(-3);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010063 continue;
64 }
65
66 /*
67 * For the sake of testing, add the vm id to the
68 * received message.
69 */
J-Alves7581c382020-05-07 18:34:20 +010070 sp_response = ffa_ret.ret3 | vm_id;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010071
72 /*
73 * Send a response through direct messaging then block
74 * until receiving a new message request.
75 */
J-Alves7581c382020-05-07 18:34:20 +010076 ffa_ret = ffa_msg_send_direct_resp(SP_ID(vm_id),
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010077 HYP_ID, sp_response);
78 }
79}
80
81static const mmap_region_t cactus_mmap[] __attribute__((used)) = {
82 /* DEVICE0 area includes UART2 necessary to console */
83 MAP_REGION_FLAT(DEVICE0_BASE, DEVICE0_SIZE, MT_DEVICE | MT_RW),
84 {0}
85};
86
Manish Pandey26c6f812020-04-30 11:43:00 +010087static void cactus_print_memory_layout(unsigned int vm_id)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020088{
89 NOTICE("Secure Partition memory layout:\n");
90
Manish Pandey26c6f812020-04-30 11:43:00 +010091 NOTICE(" Text region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +010092 (void *)CACTUS_TEXT_START, (void *)CACTUS_TEXT_END);
Manish Pandey26c6f812020-04-30 11:43:00 +010093
94 NOTICE(" Read-only data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +010095 (void *)CACTUS_RODATA_START, (void *)CACTUS_RODATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +010096
97 NOTICE(" Data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +010098 (void *)CACTUS_DATA_START, (void *)CACTUS_DATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +010099
100 NOTICE(" BSS region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100101 (void *)CACTUS_BSS_START, (void *)CACTUS_BSS_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100102
103 NOTICE(" RX : %p - %p\n",
104 (void *)(CACTUS_RX_BASE + ((vm_id - 1) * CACTUS_RX_TX_SIZE)),
105 (void *)(CACTUS_TX_BASE + ((vm_id - 1) * CACTUS_RX_TX_SIZE)));
106
107 NOTICE(" TX : %p - %p\n",
108 (void *)(CACTUS_TX_BASE + ((vm_id - 1) * CACTUS_RX_TX_SIZE)),
109 (void *)(CACTUS_RX_BASE + (vm_id * CACTUS_RX_TX_SIZE)));
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200110}
111
Manish Pandey26c6f812020-04-30 11:43:00 +0100112static void cactus_plat_configure_mmu(unsigned int vm_id)
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100113{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100114 mmap_add_region(CACTUS_TEXT_START,
115 CACTUS_TEXT_START,
116 CACTUS_TEXT_END - CACTUS_TEXT_START,
117 MT_CODE);
118 mmap_add_region(CACTUS_RODATA_START,
119 CACTUS_RODATA_START,
120 CACTUS_RODATA_END - CACTUS_RODATA_START,
121 MT_RO_DATA);
122 mmap_add_region(CACTUS_DATA_START,
123 CACTUS_DATA_START,
124 CACTUS_DATA_END - CACTUS_DATA_START,
125 MT_RW_DATA);
126 mmap_add_region(CACTUS_BSS_START,
127 CACTUS_BSS_START,
128 CACTUS_BSS_END - CACTUS_BSS_START,
129 MT_RW_DATA);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100130
Manish Pandey26c6f812020-04-30 11:43:00 +0100131 mmap_add_region((CACTUS_RX_BASE + ((vm_id - 1) * CACTUS_RX_TX_SIZE)),
132 (CACTUS_RX_BASE + ((vm_id - 1) * CACTUS_RX_TX_SIZE)),
133 (CACTUS_RX_TX_SIZE / 2),
134 MT_RO_DATA);
135
136 mmap_add_region((CACTUS_TX_BASE + ((vm_id - 1) * CACTUS_RX_TX_SIZE)),
137 (CACTUS_TX_BASE + ((vm_id - 1) * CACTUS_RX_TX_SIZE)),
138 (CACTUS_RX_TX_SIZE / 2),
139 MT_RW_DATA);
140
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100141 mmap_add(cactus_mmap);
142 init_xlat_tables();
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100143}
144
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100145void __dead2 cactus_main(void)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200146{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100147 assert(IS_IN_EL1() != 0);
148
149 /* Clear BSS */
150 memset((void *)CACTUS_BSS_START,
151 0, CACTUS_BSS_END - CACTUS_BSS_START);
152
J-Alves7581c382020-05-07 18:34:20 +0100153 /* Get current FFA id */
154 smc_ret_values ffa_id_ret = ffa_id_get();
155 if (ffa_id_ret.ret0 != FFA_SUCCESS_SMC32) {
156 ERROR("FFA_ID_GET failed.\n");
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100157 panic();
158 }
159
J-Alves7581c382020-05-07 18:34:20 +0100160 ffa_vm_id_t ffa_id = ffa_id_ret.ret2 & 0xffff;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100161
Manish Pandey26c6f812020-04-30 11:43:00 +0100162 /* Configure and enable Stage-1 MMU, enable D-Cache */
163 cactus_plat_configure_mmu(ffa_id);
164 enable_mmu_el1(0);
165
J-Alves7581c382020-05-07 18:34:20 +0100166 if (ffa_id == SPM_VM_ID_FIRST) {
Manish Pandey29495372020-04-09 15:19:26 +0100167 console_init(PL011_UART2_BASE,
168 PL011_UART2_CLK_IN_HZ,
169 PL011_BAUDRATE);
170
171 set_putc_impl(PL011_AS_STDOUT);
172
173 NOTICE("Booting Primary Cactus Secure Partition\n%s\n%s\n",
174 build_message, version_string);
175
Manish Pandey29495372020-04-09 15:19:26 +0100176 /* Get number of VMs */
177 NOTICE("VM count: %u\n", spm_vm_get_count());
178
179 /* Get virtual CPU count for current VM */
J-Alves7581c382020-05-07 18:34:20 +0100180 NOTICE("vCPU count: %u\n", spm_vcpu_get_count(ffa_id));
Manish Pandey29495372020-04-09 15:19:26 +0100181 } else {
182 set_putc_impl(HVC_CALL_AS_STDOUT);
183
184 NOTICE("Booting Secondary Cactus Secure Partition\n%s\n%s\n",
185 build_message, version_string);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100186 }
Manish Pandey26c6f812020-04-30 11:43:00 +0100187
188 NOTICE("FFA id: %u\n", ffa_id);
189 cactus_print_memory_layout(ffa_id);
190
J-Alves9f6f0142020-06-17 15:37:59 +0100191 /* Invoking Tests */
192 ffa_tests();
193
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100194 /* End up to message loop */
J-Alves7581c382020-05-07 18:34:20 +0100195 message_loop(ffa_id);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100196
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100197 /* Not reached */
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200198}