blob: f02f41e914c62c42108ec4153412c686b0323d18 [file] [log] [blame]
Michal Simekef8f5592015-06-15 14:22:50 +02001/*
Venkatesh Yadav Abbarapu1463dd52020-01-07 03:25:16 -07002 * Copyright (c) 2014-2020, ARM Limited and Contributors. All rights reserved.
Michal Simekef8f5592015-06-15 14:22:50 +02003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Michal Simekef8f5592015-06-15 14:22:50 +02005 */
6
Michal Simekef8f5592015-06-15 14:22:50 +02007#include <assert.h>
Scott Brandene5dcf982020-08-25 13:49:32 -07008#include <inttypes.h>
9#include <stdint.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010
11#include <arch_helpers.h>
12#include <common/debug.h>
Venkatesh Yadav Abbarapu1463dd52020-01-07 03:25:16 -070013#include <plat_startup.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014
Michal Simekef8f5592015-06-15 14:22:50 +020015
16/*
17 * ATFHandoffParams
18 * Parameter bitfield encoding
19 * -----------------------------------------------------------------------------
20 * Exec State 0 0 -> Aarch64, 1-> Aarch32
Soren Brinkmann8bcd3052016-05-29 09:48:26 -070021 * endianness 1 0 -> LE, 1 -> BE
Michal Simekef8f5592015-06-15 14:22:50 +020022 * secure (TZ) 2 0 -> Non secure, 1 -> secure
23 * EL 3:4 00 -> EL0, 01 -> EL1, 10 -> EL2, 11 -> EL3
24 * CPU# 5:6 00 -> A53_0, 01 -> A53_1, 10 -> A53_2, 11 -> A53_3
25 */
26
27#define FSBL_FLAGS_ESTATE_SHIFT 0
28#define FSBL_FLAGS_ESTATE_MASK (1 << FSBL_FLAGS_ESTATE_SHIFT)
29#define FSBL_FLAGS_ESTATE_A64 0
30#define FSBL_FLAGS_ESTATE_A32 1
31
32#define FSBL_FLAGS_ENDIAN_SHIFT 1
33#define FSBL_FLAGS_ENDIAN_MASK (1 << FSBL_FLAGS_ENDIAN_SHIFT)
34#define FSBL_FLAGS_ENDIAN_LE 0
35#define FSBL_FLAGS_ENDIAN_BE 1
36
37#define FSBL_FLAGS_TZ_SHIFT 2
38#define FSBL_FLAGS_TZ_MASK (1 << FSBL_FLAGS_TZ_SHIFT)
39#define FSBL_FLAGS_NON_SECURE 0
40#define FSBL_FLAGS_SECURE 1
41
42#define FSBL_FLAGS_EL_SHIFT 3
43#define FSBL_FLAGS_EL_MASK (3 << FSBL_FLAGS_EL_SHIFT)
44#define FSBL_FLAGS_EL0 0
45#define FSBL_FLAGS_EL1 1
46#define FSBL_FLAGS_EL2 2
47#define FSBL_FLAGS_EL3 3
48
49#define FSBL_FLAGS_CPU_SHIFT 5
50#define FSBL_FLAGS_CPU_MASK (3 << FSBL_FLAGS_CPU_SHIFT)
51#define FSBL_FLAGS_A53_0 0
52#define FSBL_FLAGS_A53_1 1
53#define FSBL_FLAGS_A53_2 2
54#define FSBL_FLAGS_A53_3 3
55
56#define FSBL_MAX_PARTITIONS 8
57
58/* Structure corresponding to each partition entry */
59struct xfsbl_partition {
60 uint64_t entry_point;
61 uint64_t flags;
62};
63
64/* Structure for handoff parameters to ARM Trusted Firmware (ATF) */
65struct xfsbl_atf_handoff_params {
66 uint8_t magic[4];
67 uint32_t num_entries;
68 struct xfsbl_partition partition[FSBL_MAX_PARTITIONS];
69};
70
71/**
72 * @partition: Pointer to partition struct
73 *
74 * Get the target CPU for @partition.
75 *
76 * Return: FSBL_FLAGS_A53_0, FSBL_FLAGS_A53_1, FSBL_FLAGS_A53_2 or FSBL_FLAGS_A53_3
77 */
78static int get_fsbl_cpu(const struct xfsbl_partition *partition)
79{
80 uint64_t flags = partition->flags & FSBL_FLAGS_CPU_MASK;
81
82 return flags >> FSBL_FLAGS_CPU_SHIFT;
83}
84
85/**
86 * @partition: Pointer to partition struct
87 *
88 * Get the target exception level for @partition.
89 *
90 * Return: FSBL_FLAGS_EL0, FSBL_FLAGS_EL1, FSBL_FLAGS_EL2 or FSBL_FLAGS_EL3
91 */
92static int get_fsbl_el(const struct xfsbl_partition *partition)
93{
94 uint64_t flags = partition->flags & FSBL_FLAGS_EL_MASK;
95
Soren Brinkmanndeba2af2016-05-29 09:48:44 -070096 return flags >> FSBL_FLAGS_EL_SHIFT;
Michal Simekef8f5592015-06-15 14:22:50 +020097}
98
99/**
100 * @partition: Pointer to partition struct
101 *
102 * Get the target security state for @partition.
103 *
104 * Return: FSBL_FLAGS_NON_SECURE or FSBL_FLAGS_SECURE
105 */
106static int get_fsbl_ss(const struct xfsbl_partition *partition)
107{
108 uint64_t flags = partition->flags & FSBL_FLAGS_TZ_MASK;
109
110 return flags >> FSBL_FLAGS_TZ_SHIFT;
111}
112
113/**
114 * @partition: Pointer to partition struct
115 *
Soren Brinkmann8bcd3052016-05-29 09:48:26 -0700116 * Get the target endianness for @partition.
Michal Simekef8f5592015-06-15 14:22:50 +0200117 *
118 * Return: SPSR_E_LITTLE or SPSR_E_BIG
119 */
120static int get_fsbl_endian(const struct xfsbl_partition *partition)
121{
122 uint64_t flags = partition->flags & FSBL_FLAGS_ENDIAN_MASK;
123
124 flags >>= FSBL_FLAGS_ENDIAN_SHIFT;
125
126 if (flags == FSBL_FLAGS_ENDIAN_BE)
127 return SPSR_E_BIG;
128 else
129 return SPSR_E_LITTLE;
130}
131
132/**
133 * @partition: Pointer to partition struct
134 *
135 * Get the target execution state for @partition.
136 *
137 * Return: FSBL_FLAGS_ESTATE_A32 or FSBL_FLAGS_ESTATE_A64
138 */
139static int get_fsbl_estate(const struct xfsbl_partition *partition)
140{
141 uint64_t flags = partition->flags & FSBL_FLAGS_ESTATE_MASK;
142
143 return flags >> FSBL_FLAGS_ESTATE_SHIFT;
144}
145
146/**
147 * Populates the bl32 and bl33 image info structures
148 * @bl32: BL32 image info structure
149 * @bl33: BL33 image info structure
Venkatesh Yadav Abbarapu1463dd52020-01-07 03:25:16 -0700150 * atf_handoff_addr: ATF handoff address
Michal Simekef8f5592015-06-15 14:22:50 +0200151 *
152 * Process the handoff paramters from the FSBL and populate the BL32 and BL33
153 * image info structures accordingly.
Siva Durga Prasad Paladugu8f499722018-05-17 15:17:46 +0530154 *
155 * Return: Return the status of the handoff. The value will be from the
156 * fsbl_handoff enum.
Michal Simekef8f5592015-06-15 14:22:50 +0200157 */
Venkatesh Yadav Abbarapu1463dd52020-01-07 03:25:16 -0700158enum fsbl_handoff fsbl_atf_handover(entry_point_info_t *bl32,
159 entry_point_info_t *bl33,
160 uint64_t atf_handoff_addr)
Michal Simekef8f5592015-06-15 14:22:50 +0200161{
Michal Simekef8f5592015-06-15 14:22:50 +0200162 const struct xfsbl_atf_handoff_params *ATFHandoffParams;
Michal Simekef8f5592015-06-15 14:22:50 +0200163 assert((atf_handoff_addr < BL31_BASE) ||
164 (atf_handoff_addr > (uint64_t)&__BL31_END__));
165 if (!atf_handoff_addr) {
Alistair Francisb8d474f2017-11-30 16:21:21 -0800166 WARN("BL31: No ATF handoff structure passed\n");
Siva Durga Prasad Paladugu8f499722018-05-17 15:17:46 +0530167 return FSBL_HANDOFF_NO_STRUCT;
Michal Simekef8f5592015-06-15 14:22:50 +0200168 }
169
170 ATFHandoffParams = (struct xfsbl_atf_handoff_params *)atf_handoff_addr;
171 if ((ATFHandoffParams->magic[0] != 'X') ||
172 (ATFHandoffParams->magic[1] != 'L') ||
173 (ATFHandoffParams->magic[2] != 'N') ||
174 (ATFHandoffParams->magic[3] != 'X')) {
Scott Brandene5dcf982020-08-25 13:49:32 -0700175 ERROR("BL31: invalid ATF handoff structure at %" PRIx64 "\n",
Michal Simekef8f5592015-06-15 14:22:50 +0200176 atf_handoff_addr);
Siva Durga Prasad Paladugu8f499722018-05-17 15:17:46 +0530177 return FSBL_HANDOFF_INVAL_STRUCT;
Michal Simekef8f5592015-06-15 14:22:50 +0200178 }
179
Scott Brandene5dcf982020-08-25 13:49:32 -0700180 VERBOSE("BL31: ATF handoff params at:0x%" PRIx64 ", entries:%u\n",
Michal Simekef8f5592015-06-15 14:22:50 +0200181 atf_handoff_addr, ATFHandoffParams->num_entries);
182 if (ATFHandoffParams->num_entries > FSBL_MAX_PARTITIONS) {
183 ERROR("BL31: ATF handoff params: too many partitions (%u/%u)\n",
184 ATFHandoffParams->num_entries, FSBL_MAX_PARTITIONS);
Siva Durga Prasad Paladugu8f499722018-05-17 15:17:46 +0530185 return FSBL_HANDOFF_TOO_MANY_PARTS;
Michal Simekef8f5592015-06-15 14:22:50 +0200186 }
187
188 /*
189 * we loop over all passed entries but only populate two image structs
190 * (bl32, bl33). I.e. the last applicable images in the handoff
191 * structure will be used for the hand off
192 */
193 for (size_t i = 0; i < ATFHandoffParams->num_entries; i++) {
194 entry_point_info_t *image;
195 int target_estate, target_secure;
Soren Brinkmann8bcd3052016-05-29 09:48:26 -0700196 int target_cpu, target_endianness, target_el;
Michal Simekef8f5592015-06-15 14:22:50 +0200197
Scott Brandene5dcf982020-08-25 13:49:32 -0700198 VERBOSE("BL31: %zd: entry:0x%" PRIx64 ", flags:0x%" PRIx64 "\n", i,
Michal Simekef8f5592015-06-15 14:22:50 +0200199 ATFHandoffParams->partition[i].entry_point,
200 ATFHandoffParams->partition[i].flags);
201
202 target_cpu = get_fsbl_cpu(&ATFHandoffParams->partition[i]);
203 if (target_cpu != FSBL_FLAGS_A53_0) {
204 WARN("BL31: invalid target CPU (%i)\n", target_cpu);
205 continue;
206 }
207
208 target_el = get_fsbl_el(&ATFHandoffParams->partition[i]);
209 if ((target_el == FSBL_FLAGS_EL3) ||
210 (target_el == FSBL_FLAGS_EL0)) {
211 WARN("BL31: invalid exception level (%i)\n", target_el);
212 continue;
213 }
214
215 target_secure = get_fsbl_ss(&ATFHandoffParams->partition[i]);
216 if (target_secure == FSBL_FLAGS_SECURE &&
217 target_el == FSBL_FLAGS_EL2) {
218 WARN("BL31: invalid security state (%i) for exception level (%i)\n",
219 target_secure, target_el);
220 continue;
221 }
222
223 target_estate = get_fsbl_estate(&ATFHandoffParams->partition[i]);
Soren Brinkmann8bcd3052016-05-29 09:48:26 -0700224 target_endianness = get_fsbl_endian(&ATFHandoffParams->partition[i]);
Michal Simekef8f5592015-06-15 14:22:50 +0200225
226 if (target_secure == FSBL_FLAGS_SECURE) {
227 image = bl32;
228
229 if (target_estate == FSBL_FLAGS_ESTATE_A32)
230 bl32->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
Soren Brinkmann8bcd3052016-05-29 09:48:26 -0700231 target_endianness,
Michal Simekef8f5592015-06-15 14:22:50 +0200232 DISABLE_ALL_EXCEPTIONS);
233 else
234 bl32->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
235 DISABLE_ALL_EXCEPTIONS);
236 } else {
237 image = bl33;
238
239 if (target_estate == FSBL_FLAGS_ESTATE_A32) {
240 if (target_el == FSBL_FLAGS_EL2)
241 target_el = MODE32_hyp;
242 else
243 target_el = MODE32_sys;
244
245 bl33->spsr = SPSR_MODE32(target_el, SPSR_T_ARM,
Soren Brinkmann8bcd3052016-05-29 09:48:26 -0700246 target_endianness,
Michal Simekef8f5592015-06-15 14:22:50 +0200247 DISABLE_ALL_EXCEPTIONS);
248 } else {
249 if (target_el == FSBL_FLAGS_EL2)
250 target_el = MODE_EL2;
251 else
252 target_el = MODE_EL1;
253
254 bl33->spsr = SPSR_64(target_el, MODE_SP_ELX,
255 DISABLE_ALL_EXCEPTIONS);
256 }
257 }
258
Scott Brandene5dcf982020-08-25 13:49:32 -0700259 VERBOSE("Setting up %s entry point to:%" PRIx64 ", el:%x\n",
Michal Simekef8f5592015-06-15 14:22:50 +0200260 target_secure == FSBL_FLAGS_SECURE ? "BL32" : "BL33",
261 ATFHandoffParams->partition[i].entry_point,
262 target_el);
263 image->pc = ATFHandoffParams->partition[i].entry_point;
264
Soren Brinkmann8bcd3052016-05-29 09:48:26 -0700265 if (target_endianness == SPSR_E_BIG)
Michal Simekef8f5592015-06-15 14:22:50 +0200266 EP_SET_EE(image->h.attr, EP_EE_BIG);
267 else
268 EP_SET_EE(image->h.attr, EP_EE_LITTLE);
269 }
Siva Durga Prasad Paladugu8f499722018-05-17 15:17:46 +0530270
271 return FSBL_HANDOFF_SUCCESS;
Michal Simekef8f5592015-06-15 14:22:50 +0200272}