blob: 512126ef26f276af7b04383c50366e2637b13ea1 [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
Venkatesh Yadav Abbarapua2ca35d2022-07-04 11:40:27 +053027#define FSBL_FLAGS_ESTATE_SHIFT 0U
28#define FSBL_FLAGS_ESTATE_MASK (1U << FSBL_FLAGS_ESTATE_SHIFT)
29#define FSBL_FLAGS_ESTATE_A64 0U
30#define FSBL_FLAGS_ESTATE_A32 1U
Michal Simekef8f5592015-06-15 14:22:50 +020031
Venkatesh Yadav Abbarapua2ca35d2022-07-04 11:40:27 +053032#define FSBL_FLAGS_ENDIAN_SHIFT 1U
33#define FSBL_FLAGS_ENDIAN_MASK (1U << FSBL_FLAGS_ENDIAN_SHIFT)
34#define FSBL_FLAGS_ENDIAN_LE 0U
35#define FSBL_FLAGS_ENDIAN_BE 1U
Michal Simekef8f5592015-06-15 14:22:50 +020036
Venkatesh Yadav Abbarapua2ca35d2022-07-04 11:40:27 +053037#define FSBL_FLAGS_TZ_SHIFT 2U
38#define FSBL_FLAGS_TZ_MASK (1U << FSBL_FLAGS_TZ_SHIFT)
39#define FSBL_FLAGS_NON_SECURE 0U
40#define FSBL_FLAGS_SECURE 1U
Michal Simekef8f5592015-06-15 14:22:50 +020041
Venkatesh Yadav Abbarapua2ca35d2022-07-04 11:40:27 +053042#define FSBL_FLAGS_EL_SHIFT 3U
43#define FSBL_FLAGS_EL_MASK (3U << FSBL_FLAGS_EL_SHIFT)
44#define FSBL_FLAGS_EL0 0U
45#define FSBL_FLAGS_EL1 1U
46#define FSBL_FLAGS_EL2 2U
47#define FSBL_FLAGS_EL3 3U
Michal Simekef8f5592015-06-15 14:22:50 +020048
Venkatesh Yadav Abbarapua2ca35d2022-07-04 11:40:27 +053049#define FSBL_FLAGS_CPU_SHIFT 5U
50#define FSBL_FLAGS_CPU_MASK (3U << FSBL_FLAGS_CPU_SHIFT)
51#define FSBL_FLAGS_A53_0 0U
52#define FSBL_FLAGS_A53_1 1U
53#define FSBL_FLAGS_A53_2 2U
54#define FSBL_FLAGS_A53_3 3U
Michal Simekef8f5592015-06-15 14:22:50 +020055
Michal Simekef8f5592015-06-15 14:22:50 +020056/**
57 * @partition: Pointer to partition struct
58 *
59 * Get the target CPU for @partition.
60 *
61 * Return: FSBL_FLAGS_A53_0, FSBL_FLAGS_A53_1, FSBL_FLAGS_A53_2 or FSBL_FLAGS_A53_3
62 */
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +053063static int32_t get_fsbl_cpu(const struct xfsbl_partition *partition)
Michal Simekef8f5592015-06-15 14:22:50 +020064{
65 uint64_t flags = partition->flags & FSBL_FLAGS_CPU_MASK;
66
67 return flags >> FSBL_FLAGS_CPU_SHIFT;
68}
69
70/**
71 * @partition: Pointer to partition struct
72 *
73 * Get the target exception level for @partition.
74 *
75 * Return: FSBL_FLAGS_EL0, FSBL_FLAGS_EL1, FSBL_FLAGS_EL2 or FSBL_FLAGS_EL3
76 */
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +053077static int32_t get_fsbl_el(const struct xfsbl_partition *partition)
Michal Simekef8f5592015-06-15 14:22:50 +020078{
79 uint64_t flags = partition->flags & FSBL_FLAGS_EL_MASK;
80
Soren Brinkmanndeba2af2016-05-29 09:48:44 -070081 return flags >> FSBL_FLAGS_EL_SHIFT;
Michal Simekef8f5592015-06-15 14:22:50 +020082}
83
84/**
85 * @partition: Pointer to partition struct
86 *
87 * Get the target security state for @partition.
88 *
89 * Return: FSBL_FLAGS_NON_SECURE or FSBL_FLAGS_SECURE
90 */
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +053091static int32_t get_fsbl_ss(const struct xfsbl_partition *partition)
Michal Simekef8f5592015-06-15 14:22:50 +020092{
93 uint64_t flags = partition->flags & FSBL_FLAGS_TZ_MASK;
94
95 return flags >> FSBL_FLAGS_TZ_SHIFT;
96}
97
98/**
99 * @partition: Pointer to partition struct
100 *
Soren Brinkmann8bcd3052016-05-29 09:48:26 -0700101 * Get the target endianness for @partition.
Michal Simekef8f5592015-06-15 14:22:50 +0200102 *
103 * Return: SPSR_E_LITTLE or SPSR_E_BIG
104 */
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530105static int32_t get_fsbl_endian(const struct xfsbl_partition *partition)
Michal Simekef8f5592015-06-15 14:22:50 +0200106{
107 uint64_t flags = partition->flags & FSBL_FLAGS_ENDIAN_MASK;
108
109 flags >>= FSBL_FLAGS_ENDIAN_SHIFT;
110
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530111 if (flags == FSBL_FLAGS_ENDIAN_BE) {
Michal Simekef8f5592015-06-15 14:22:50 +0200112 return SPSR_E_BIG;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530113 } else {
Michal Simekef8f5592015-06-15 14:22:50 +0200114 return SPSR_E_LITTLE;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530115 }
Michal Simekef8f5592015-06-15 14:22:50 +0200116}
117
118/**
119 * @partition: Pointer to partition struct
120 *
121 * Get the target execution state for @partition.
122 *
123 * Return: FSBL_FLAGS_ESTATE_A32 or FSBL_FLAGS_ESTATE_A64
124 */
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530125static int32_t get_fsbl_estate(const struct xfsbl_partition *partition)
Michal Simekef8f5592015-06-15 14:22:50 +0200126{
127 uint64_t flags = partition->flags & FSBL_FLAGS_ESTATE_MASK;
128
129 return flags >> FSBL_FLAGS_ESTATE_SHIFT;
130}
131
132/**
133 * Populates the bl32 and bl33 image info structures
134 * @bl32: BL32 image info structure
135 * @bl33: BL33 image info structure
Venkatesh Yadav Abbarapu1463dd52020-01-07 03:25:16 -0700136 * atf_handoff_addr: ATF handoff address
Michal Simekef8f5592015-06-15 14:22:50 +0200137 *
138 * Process the handoff paramters from the FSBL and populate the BL32 and BL33
139 * image info structures accordingly.
Siva Durga Prasad Paladugu8f499722018-05-17 15:17:46 +0530140 *
141 * Return: Return the status of the handoff. The value will be from the
142 * fsbl_handoff enum.
Michal Simekef8f5592015-06-15 14:22:50 +0200143 */
Venkatesh Yadav Abbarapu1463dd52020-01-07 03:25:16 -0700144enum fsbl_handoff fsbl_atf_handover(entry_point_info_t *bl32,
145 entry_point_info_t *bl33,
146 uint64_t atf_handoff_addr)
Michal Simekef8f5592015-06-15 14:22:50 +0200147{
Michal Simekef8f5592015-06-15 14:22:50 +0200148 const struct xfsbl_atf_handoff_params *ATFHandoffParams;
Michal Simekef8f5592015-06-15 14:22:50 +0200149 assert((atf_handoff_addr < BL31_BASE) ||
150 (atf_handoff_addr > (uint64_t)&__BL31_END__));
151 if (!atf_handoff_addr) {
Alistair Francisb8d474f2017-11-30 16:21:21 -0800152 WARN("BL31: No ATF handoff structure passed\n");
Siva Durga Prasad Paladugu8f499722018-05-17 15:17:46 +0530153 return FSBL_HANDOFF_NO_STRUCT;
Michal Simekef8f5592015-06-15 14:22:50 +0200154 }
155
156 ATFHandoffParams = (struct xfsbl_atf_handoff_params *)atf_handoff_addr;
157 if ((ATFHandoffParams->magic[0] != 'X') ||
158 (ATFHandoffParams->magic[1] != 'L') ||
159 (ATFHandoffParams->magic[2] != 'N') ||
160 (ATFHandoffParams->magic[3] != 'X')) {
Scott Brandene5dcf982020-08-25 13:49:32 -0700161 ERROR("BL31: invalid ATF handoff structure at %" PRIx64 "\n",
Michal Simekef8f5592015-06-15 14:22:50 +0200162 atf_handoff_addr);
Siva Durga Prasad Paladugu8f499722018-05-17 15:17:46 +0530163 return FSBL_HANDOFF_INVAL_STRUCT;
Michal Simekef8f5592015-06-15 14:22:50 +0200164 }
165
Scott Brandene5dcf982020-08-25 13:49:32 -0700166 VERBOSE("BL31: ATF handoff params at:0x%" PRIx64 ", entries:%u\n",
Michal Simekef8f5592015-06-15 14:22:50 +0200167 atf_handoff_addr, ATFHandoffParams->num_entries);
168 if (ATFHandoffParams->num_entries > FSBL_MAX_PARTITIONS) {
169 ERROR("BL31: ATF handoff params: too many partitions (%u/%u)\n",
170 ATFHandoffParams->num_entries, FSBL_MAX_PARTITIONS);
Siva Durga Prasad Paladugu8f499722018-05-17 15:17:46 +0530171 return FSBL_HANDOFF_TOO_MANY_PARTS;
Michal Simekef8f5592015-06-15 14:22:50 +0200172 }
173
174 /*
175 * we loop over all passed entries but only populate two image structs
176 * (bl32, bl33). I.e. the last applicable images in the handoff
177 * structure will be used for the hand off
178 */
179 for (size_t i = 0; i < ATFHandoffParams->num_entries; i++) {
180 entry_point_info_t *image;
Venkatesh Yadav Abbarapua2ca35d2022-07-04 11:40:27 +0530181 int32_t target_estate, target_secure, target_cpu;
182 uint32_t target_endianness, target_el;
Michal Simekef8f5592015-06-15 14:22:50 +0200183
Scott Brandene5dcf982020-08-25 13:49:32 -0700184 VERBOSE("BL31: %zd: entry:0x%" PRIx64 ", flags:0x%" PRIx64 "\n", i,
Michal Simekef8f5592015-06-15 14:22:50 +0200185 ATFHandoffParams->partition[i].entry_point,
186 ATFHandoffParams->partition[i].flags);
187
188 target_cpu = get_fsbl_cpu(&ATFHandoffParams->partition[i]);
189 if (target_cpu != FSBL_FLAGS_A53_0) {
190 WARN("BL31: invalid target CPU (%i)\n", target_cpu);
191 continue;
192 }
193
194 target_el = get_fsbl_el(&ATFHandoffParams->partition[i]);
195 if ((target_el == FSBL_FLAGS_EL3) ||
196 (target_el == FSBL_FLAGS_EL0)) {
197 WARN("BL31: invalid exception level (%i)\n", target_el);
198 continue;
199 }
200
201 target_secure = get_fsbl_ss(&ATFHandoffParams->partition[i]);
202 if (target_secure == FSBL_FLAGS_SECURE &&
203 target_el == FSBL_FLAGS_EL2) {
204 WARN("BL31: invalid security state (%i) for exception level (%i)\n",
205 target_secure, target_el);
206 continue;
207 }
208
209 target_estate = get_fsbl_estate(&ATFHandoffParams->partition[i]);
Soren Brinkmann8bcd3052016-05-29 09:48:26 -0700210 target_endianness = get_fsbl_endian(&ATFHandoffParams->partition[i]);
Michal Simekef8f5592015-06-15 14:22:50 +0200211
212 if (target_secure == FSBL_FLAGS_SECURE) {
213 image = bl32;
214
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530215 if (target_estate == FSBL_FLAGS_ESTATE_A32) {
Michal Simekef8f5592015-06-15 14:22:50 +0200216 bl32->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
Soren Brinkmann8bcd3052016-05-29 09:48:26 -0700217 target_endianness,
Michal Simekef8f5592015-06-15 14:22:50 +0200218 DISABLE_ALL_EXCEPTIONS);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530219 } else {
Michal Simekef8f5592015-06-15 14:22:50 +0200220 bl32->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
221 DISABLE_ALL_EXCEPTIONS);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530222 }
Michal Simekef8f5592015-06-15 14:22:50 +0200223 } else {
224 image = bl33;
225
226 if (target_estate == FSBL_FLAGS_ESTATE_A32) {
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530227 if (target_el == FSBL_FLAGS_EL2) {
Michal Simekef8f5592015-06-15 14:22:50 +0200228 target_el = MODE32_hyp;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530229 } else {
Michal Simekef8f5592015-06-15 14:22:50 +0200230 target_el = MODE32_sys;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530231 }
Michal Simekef8f5592015-06-15 14:22:50 +0200232
233 bl33->spsr = SPSR_MODE32(target_el, SPSR_T_ARM,
Soren Brinkmann8bcd3052016-05-29 09:48:26 -0700234 target_endianness,
Michal Simekef8f5592015-06-15 14:22:50 +0200235 DISABLE_ALL_EXCEPTIONS);
236 } else {
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530237 if (target_el == FSBL_FLAGS_EL2) {
Michal Simekef8f5592015-06-15 14:22:50 +0200238 target_el = MODE_EL2;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530239 } else {
Michal Simekef8f5592015-06-15 14:22:50 +0200240 target_el = MODE_EL1;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530241 }
Michal Simekef8f5592015-06-15 14:22:50 +0200242
243 bl33->spsr = SPSR_64(target_el, MODE_SP_ELX,
244 DISABLE_ALL_EXCEPTIONS);
245 }
246 }
247
Scott Brandene5dcf982020-08-25 13:49:32 -0700248 VERBOSE("Setting up %s entry point to:%" PRIx64 ", el:%x\n",
Michal Simekef8f5592015-06-15 14:22:50 +0200249 target_secure == FSBL_FLAGS_SECURE ? "BL32" : "BL33",
250 ATFHandoffParams->partition[i].entry_point,
251 target_el);
252 image->pc = ATFHandoffParams->partition[i].entry_point;
253
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530254 if (target_endianness == SPSR_E_BIG) {
Michal Simekef8f5592015-06-15 14:22:50 +0200255 EP_SET_EE(image->h.attr, EP_EE_BIG);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530256 } else {
Michal Simekef8f5592015-06-15 14:22:50 +0200257 EP_SET_EE(image->h.attr, EP_EE_LITTLE);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530258 }
Michal Simekef8f5592015-06-15 14:22:50 +0200259 }
Siva Durga Prasad Paladugu8f499722018-05-17 15:17:46 +0530260
261 return FSBL_HANDOFF_SUCCESS;
Michal Simekef8f5592015-06-15 14:22:50 +0200262}