blob: be137adcd528f940459b598b1620033a6cde2451 [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>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02008#include <debug.h>
Antonio Nino Diaz09a00ef2019-01-11 13:12:58 +00009#include <drivers/arm/pl011.h>
10#include <drivers/console.h>
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +010011#include <errno.h>
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010012#include <lib/aarch64/arch_helpers.h>
13#include <lib/xlat_tables/xlat_tables_v2.h>
14#include <lib/xlat_tables/xlat_mmu_helpers.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020015#include <plat_arm.h>
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010016#include <plat/common/platform.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020017#include <platform_def.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020018#include <std_svc.h>
19
20#include "cactus.h"
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +010021#include "cactus_def.h"
Manish Pandey29495372020-04-09 15:19:26 +010022#include "spci_helpers.h"
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010023
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020024/* Host machine information injected by the build system in the ELF file. */
25extern const char build_message[];
26extern const char version_string[];
27
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010028/*
29 *
30 * Message loop function
31 * Notice we cannot use regular print functions because this serves to both
32 * "primary" and "secondary" VMs. Secondary VM cannot access UART directly
33 * but rather through Hafnium print hypercall.
34 *
35 */
36static void __dead2 message_loop(spci_vm_id_t vm_id)
37{
38 smc_ret_values spci_ret;
39 uint32_t sp_response;
40
41 /*
42 * This initial wait call is necessary to inform SPMD that
43 * SP initialization has completed. It blocks until receiving
44 * a direct message request.
45 */
46 spci_ret = spci_msg_wait();
47
48 for (;;) {
49
50 if (spci_ret.ret0 != SPCI_MSG_SEND_DIRECT_REQ_SMC32) {
51 spci_ret = spci_error(-1);
52 continue;
53 }
54
55 if (spci_ret.ret1 != SP_ID(vm_id)) {
56 spci_ret = spci_error(-2);
57 continue;
58 }
59
60 if (spci_ret.ret2 != HYP_ID) {
61 spci_ret = spci_error(-3);
62 continue;
63 }
64
65 /*
66 * For the sake of testing, add the vm id to the
67 * received message.
68 */
69 sp_response = spci_ret.ret3 | vm_id;
70
71 /*
72 * Send a response through direct messaging then block
73 * until receiving a new message request.
74 */
75 spci_ret = spci_msg_send_direct_resp(SP_ID(vm_id),
76 HYP_ID, sp_response);
77 }
78}
79
80static const mmap_region_t cactus_mmap[] __attribute__((used)) = {
81 /* DEVICE0 area includes UART2 necessary to console */
82 MAP_REGION_FLAT(DEVICE0_BASE, DEVICE0_SIZE, MT_DEVICE | MT_RW),
83 {0}
84};
85
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +010086static void cactus_print_memory_layout(void)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020087{
88 NOTICE("Secure Partition memory layout:\n");
89
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +010090 NOTICE(" Image regions\n");
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020091 NOTICE(" Text region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +010092 (void *)CACTUS_TEXT_START, (void *)CACTUS_TEXT_END);
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020093 NOTICE(" Read-only data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +010094 (void *)CACTUS_RODATA_START, (void *)CACTUS_RODATA_END);
95 NOTICE(" Data region : %p - %p\n",
96 (void *)CACTUS_DATA_START, (void *)CACTUS_DATA_END);
97 NOTICE(" BSS region : %p - %p\n",
98 (void *)CACTUS_BSS_START, (void *)CACTUS_BSS_END);
99 NOTICE(" Total image memory : %p - %p\n",
100 (void *)CACTUS_IMAGE_BASE,
101 (void *)(CACTUS_IMAGE_BASE + CACTUS_IMAGE_SIZE));
102 NOTICE(" SPM regions\n");
103 NOTICE(" SPM <-> SP buffer : %p - %p\n",
104 (void *)CACTUS_SPM_BUF_BASE,
105 (void *)(CACTUS_SPM_BUF_BASE + CACTUS_SPM_BUF_SIZE));
106 NOTICE(" NS <-> SP buffer : %p - %p\n",
107 (void *)CACTUS_NS_BUF_BASE,
108 (void *)(CACTUS_NS_BUF_BASE + CACTUS_NS_BUF_SIZE));
109 NOTICE(" Test regions\n");
110 NOTICE(" Test region : %p - %p\n",
111 (void *)CACTUS_TEST_MEM_BASE,
112 (void *)(CACTUS_TEST_MEM_BASE + CACTUS_TEST_MEM_SIZE));
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200113}
114
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100115static void cactus_plat_configure_mmu(void)
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100116{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100117 mmap_add_region(CACTUS_TEXT_START,
118 CACTUS_TEXT_START,
119 CACTUS_TEXT_END - CACTUS_TEXT_START,
120 MT_CODE);
121 mmap_add_region(CACTUS_RODATA_START,
122 CACTUS_RODATA_START,
123 CACTUS_RODATA_END - CACTUS_RODATA_START,
124 MT_RO_DATA);
125 mmap_add_region(CACTUS_DATA_START,
126 CACTUS_DATA_START,
127 CACTUS_DATA_END - CACTUS_DATA_START,
128 MT_RW_DATA);
129 mmap_add_region(CACTUS_BSS_START,
130 CACTUS_BSS_START,
131 CACTUS_BSS_END - CACTUS_BSS_START,
132 MT_RW_DATA);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100133
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100134 mmap_add(cactus_mmap);
135 init_xlat_tables();
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100136}
137
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100138void __dead2 cactus_main(void)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200139{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100140 assert(IS_IN_EL1() != 0);
141
142 /* Clear BSS */
143 memset((void *)CACTUS_BSS_START,
144 0, CACTUS_BSS_END - CACTUS_BSS_START);
145
146 /* Configure and enable Stage-1 MMU, enable D-Cache */
147 cactus_plat_configure_mmu();
148 enable_mmu_el1(0);
149
150 /* Get current SPCI id */
151 smc_ret_values spci_id_ret = spci_id_get();
152 if (spci_id_ret.ret0 != SPCI_SUCCESS_SMC32) {
153 ERROR("SPCI_ID_GET failed.\n");
154 panic();
155 }
156
157 spci_vm_id_t spci_id = spci_id_ret.ret2 & 0xffff;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100158
Manish Pandey29495372020-04-09 15:19:26 +0100159 if (spci_id == SPM_VM_ID_FIRST) {
160 console_init(PL011_UART2_BASE,
161 PL011_UART2_CLK_IN_HZ,
162 PL011_BAUDRATE);
163
164 set_putc_impl(PL011_AS_STDOUT);
165
166 NOTICE("Booting Primary Cactus Secure Partition\n%s\n%s\n",
167 build_message, version_string);
168
169 cactus_print_memory_layout();
170
171 NOTICE("SPCI id: %u\n", spci_id); /* Expect VM id 1 */
172
173 /* Get number of VMs */
174 NOTICE("VM count: %u\n", spm_vm_get_count());
175
176 /* Get virtual CPU count for current VM */
177 NOTICE("vCPU count: %u\n", spm_vcpu_get_count(spci_id));
178 } else {
179 set_putc_impl(HVC_CALL_AS_STDOUT);
180
181 NOTICE("Booting Secondary Cactus Secure Partition\n%s\n%s\n",
182 build_message, version_string);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100183 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100184 /* End up to message loop */
185 message_loop(spci_id);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100186
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100187 /* Not reached */
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200188}