blob: 007c045916748e783e1e056138615c58e8ae0c43 [file] [log] [blame]
Michal Simekef8f5592015-06-15 14:22:50 +02001/*
Michal Simek2a47faa2023-04-14 08:43:51 +02002 * Copyright (c) 2014-2020, Arm Limited and Contributors. All rights reserved.
Prasad Kummarie0783112023-04-26 11:02:07 +05303 * Copyright (c) 2023, Advanced Micro Devices, Inc. All rights reserved.
Michal Simekef8f5592015-06-15 14:22:50 +02004 *
dp-armfa3cf0b2017-05-03 09:38:09 +01005 * SPDX-License-Identifier: BSD-3-Clause
Michal Simekef8f5592015-06-15 14:22:50 +02006 */
7
Michal Simekef8f5592015-06-15 14:22:50 +02008#include <assert.h>
Scott Brandene5dcf982020-08-25 13:49:32 -07009#include <inttypes.h>
10#include <stdint.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011
12#include <arch_helpers.h>
13#include <common/debug.h>
Venkatesh Yadav Abbarapu1463dd52020-01-07 03:25:16 -070014#include <plat_startup.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000015
Michal Simekef8f5592015-06-15 14:22:50 +020016
17/*
Prasad Kummarie0783112023-04-26 11:02:07 +053018 * TFAHandoffParams
Michal Simekef8f5592015-06-15 14:22:50 +020019 * Parameter bitfield encoding
20 * -----------------------------------------------------------------------------
21 * Exec State 0 0 -> Aarch64, 1-> Aarch32
Soren Brinkmann8bcd3052016-05-29 09:48:26 -070022 * endianness 1 0 -> LE, 1 -> BE
Michal Simekef8f5592015-06-15 14:22:50 +020023 * secure (TZ) 2 0 -> Non secure, 1 -> secure
24 * EL 3:4 00 -> EL0, 01 -> EL1, 10 -> EL2, 11 -> EL3
25 * CPU# 5:6 00 -> A53_0, 01 -> A53_1, 10 -> A53_2, 11 -> A53_3
26 */
27
Venkatesh Yadav Abbarapua2ca35d2022-07-04 11:40:27 +053028#define FSBL_FLAGS_ESTATE_SHIFT 0U
29#define FSBL_FLAGS_ESTATE_MASK (1U << FSBL_FLAGS_ESTATE_SHIFT)
30#define FSBL_FLAGS_ESTATE_A64 0U
31#define FSBL_FLAGS_ESTATE_A32 1U
Michal Simekef8f5592015-06-15 14:22:50 +020032
Venkatesh Yadav Abbarapua2ca35d2022-07-04 11:40:27 +053033#define FSBL_FLAGS_ENDIAN_SHIFT 1U
34#define FSBL_FLAGS_ENDIAN_MASK (1U << FSBL_FLAGS_ENDIAN_SHIFT)
35#define FSBL_FLAGS_ENDIAN_LE 0U
36#define FSBL_FLAGS_ENDIAN_BE 1U
Michal Simekef8f5592015-06-15 14:22:50 +020037
Venkatesh Yadav Abbarapua2ca35d2022-07-04 11:40:27 +053038#define FSBL_FLAGS_TZ_SHIFT 2U
39#define FSBL_FLAGS_TZ_MASK (1U << FSBL_FLAGS_TZ_SHIFT)
40#define FSBL_FLAGS_NON_SECURE 0U
41#define FSBL_FLAGS_SECURE 1U
Michal Simekef8f5592015-06-15 14:22:50 +020042
Venkatesh Yadav Abbarapua2ca35d2022-07-04 11:40:27 +053043#define FSBL_FLAGS_EL_SHIFT 3U
44#define FSBL_FLAGS_EL_MASK (3U << FSBL_FLAGS_EL_SHIFT)
45#define FSBL_FLAGS_EL0 0U
46#define FSBL_FLAGS_EL1 1U
47#define FSBL_FLAGS_EL2 2U
48#define FSBL_FLAGS_EL3 3U
Michal Simekef8f5592015-06-15 14:22:50 +020049
Venkatesh Yadav Abbarapua2ca35d2022-07-04 11:40:27 +053050#define FSBL_FLAGS_CPU_SHIFT 5U
51#define FSBL_FLAGS_CPU_MASK (3U << FSBL_FLAGS_CPU_SHIFT)
52#define FSBL_FLAGS_A53_0 0U
53#define FSBL_FLAGS_A53_1 1U
54#define FSBL_FLAGS_A53_2 2U
55#define FSBL_FLAGS_A53_3 3U
Michal Simekef8f5592015-06-15 14:22:50 +020056
Michal Simekef8f5592015-06-15 14:22:50 +020057/**
58 * @partition: Pointer to partition struct
59 *
60 * Get the target CPU for @partition.
61 *
62 * Return: FSBL_FLAGS_A53_0, FSBL_FLAGS_A53_1, FSBL_FLAGS_A53_2 or FSBL_FLAGS_A53_3
63 */
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +053064static int32_t get_fsbl_cpu(const struct xfsbl_partition *partition)
Michal Simekef8f5592015-06-15 14:22:50 +020065{
66 uint64_t flags = partition->flags & FSBL_FLAGS_CPU_MASK;
67
68 return flags >> FSBL_FLAGS_CPU_SHIFT;
69}
70
71/**
72 * @partition: Pointer to partition struct
73 *
74 * Get the target exception level for @partition.
75 *
76 * Return: FSBL_FLAGS_EL0, FSBL_FLAGS_EL1, FSBL_FLAGS_EL2 or FSBL_FLAGS_EL3
77 */
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +053078static int32_t get_fsbl_el(const struct xfsbl_partition *partition)
Michal Simekef8f5592015-06-15 14:22:50 +020079{
80 uint64_t flags = partition->flags & FSBL_FLAGS_EL_MASK;
81
Soren Brinkmanndeba2af2016-05-29 09:48:44 -070082 return flags >> FSBL_FLAGS_EL_SHIFT;
Michal Simekef8f5592015-06-15 14:22:50 +020083}
84
85/**
86 * @partition: Pointer to partition struct
87 *
88 * Get the target security state for @partition.
89 *
90 * Return: FSBL_FLAGS_NON_SECURE or FSBL_FLAGS_SECURE
91 */
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +053092static int32_t get_fsbl_ss(const struct xfsbl_partition *partition)
Michal Simekef8f5592015-06-15 14:22:50 +020093{
94 uint64_t flags = partition->flags & FSBL_FLAGS_TZ_MASK;
95
96 return flags >> FSBL_FLAGS_TZ_SHIFT;
97}
98
99/**
100 * @partition: Pointer to partition struct
101 *
Soren Brinkmann8bcd3052016-05-29 09:48:26 -0700102 * Get the target endianness for @partition.
Michal Simekef8f5592015-06-15 14:22:50 +0200103 *
104 * Return: SPSR_E_LITTLE or SPSR_E_BIG
105 */
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530106static int32_t get_fsbl_endian(const struct xfsbl_partition *partition)
Michal Simekef8f5592015-06-15 14:22:50 +0200107{
108 uint64_t flags = partition->flags & FSBL_FLAGS_ENDIAN_MASK;
109
110 flags >>= FSBL_FLAGS_ENDIAN_SHIFT;
111
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530112 if (flags == FSBL_FLAGS_ENDIAN_BE) {
Michal Simekef8f5592015-06-15 14:22:50 +0200113 return SPSR_E_BIG;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530114 } else {
Michal Simekef8f5592015-06-15 14:22:50 +0200115 return SPSR_E_LITTLE;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530116 }
Michal Simekef8f5592015-06-15 14:22:50 +0200117}
118
119/**
120 * @partition: Pointer to partition struct
121 *
122 * Get the target execution state for @partition.
123 *
124 * Return: FSBL_FLAGS_ESTATE_A32 or FSBL_FLAGS_ESTATE_A64
125 */
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530126static int32_t get_fsbl_estate(const struct xfsbl_partition *partition)
Michal Simekef8f5592015-06-15 14:22:50 +0200127{
128 uint64_t flags = partition->flags & FSBL_FLAGS_ESTATE_MASK;
129
130 return flags >> FSBL_FLAGS_ESTATE_SHIFT;
131}
132
133/**
134 * Populates the bl32 and bl33 image info structures
135 * @bl32: BL32 image info structure
136 * @bl33: BL33 image info structure
Prasad Kummarie0783112023-04-26 11:02:07 +0530137 * tfa_handoff_addr: TF-A handoff address
Michal Simekef8f5592015-06-15 14:22:50 +0200138 *
Elyes Haouas2be03c02023-02-13 09:14:48 +0100139 * Process the handoff parameters from the FSBL and populate the BL32 and BL33
Michal Simekef8f5592015-06-15 14:22:50 +0200140 * image info structures accordingly.
Siva Durga Prasad Paladugu8f499722018-05-17 15:17:46 +0530141 *
142 * Return: Return the status of the handoff. The value will be from the
143 * fsbl_handoff enum.
Michal Simekef8f5592015-06-15 14:22:50 +0200144 */
Prasad Kummarie0783112023-04-26 11:02:07 +0530145enum fsbl_handoff fsbl_tfa_handover(entry_point_info_t *bl32,
Venkatesh Yadav Abbarapu1463dd52020-01-07 03:25:16 -0700146 entry_point_info_t *bl33,
Prasad Kummarie0783112023-04-26 11:02:07 +0530147 uint64_t tfa_handoff_addr)
Michal Simekef8f5592015-06-15 14:22:50 +0200148{
Prasad Kummarie0783112023-04-26 11:02:07 +0530149 const struct xfsbl_tfa_handoff_params *TFAHandoffParams;
150 if (!tfa_handoff_addr) {
151 WARN("BL31: No TFA handoff structure passed\n");
Siva Durga Prasad Paladugu8f499722018-05-17 15:17:46 +0530152 return FSBL_HANDOFF_NO_STRUCT;
Michal Simekef8f5592015-06-15 14:22:50 +0200153 }
154
Prasad Kummarie0783112023-04-26 11:02:07 +0530155 TFAHandoffParams = (struct xfsbl_tfa_handoff_params *)tfa_handoff_addr;
156 if ((TFAHandoffParams->magic[0] != 'X') ||
157 (TFAHandoffParams->magic[1] != 'L') ||
158 (TFAHandoffParams->magic[2] != 'N') ||
159 (TFAHandoffParams->magic[3] != 'X')) {
160 ERROR("BL31: invalid TF-A handoff structure at %" PRIx64 "\n",
161 tfa_handoff_addr);
Siva Durga Prasad Paladugu8f499722018-05-17 15:17:46 +0530162 return FSBL_HANDOFF_INVAL_STRUCT;
Michal Simekef8f5592015-06-15 14:22:50 +0200163 }
164
Prasad Kummarie0783112023-04-26 11:02:07 +0530165 VERBOSE("BL31: TF-A handoff params at:0x%" PRIx64 ", entries:%u\n",
166 tfa_handoff_addr, TFAHandoffParams->num_entries);
167 if (TFAHandoffParams->num_entries > FSBL_MAX_PARTITIONS) {
168 ERROR("BL31: TF-A handoff params: too many partitions (%u/%u)\n",
169 TFAHandoffParams->num_entries, FSBL_MAX_PARTITIONS);
Siva Durga Prasad Paladugu8f499722018-05-17 15:17:46 +0530170 return FSBL_HANDOFF_TOO_MANY_PARTS;
Michal Simekef8f5592015-06-15 14:22:50 +0200171 }
172
173 /*
174 * we loop over all passed entries but only populate two image structs
175 * (bl32, bl33). I.e. the last applicable images in the handoff
176 * structure will be used for the hand off
177 */
Prasad Kummarie0783112023-04-26 11:02:07 +0530178 for (size_t i = 0; i < TFAHandoffParams->num_entries; i++) {
Michal Simekef8f5592015-06-15 14:22:50 +0200179 entry_point_info_t *image;
Venkatesh Yadav Abbarapua2ca35d2022-07-04 11:40:27 +0530180 int32_t target_estate, target_secure, target_cpu;
181 uint32_t target_endianness, target_el;
Michal Simekef8f5592015-06-15 14:22:50 +0200182
Scott Brandene5dcf982020-08-25 13:49:32 -0700183 VERBOSE("BL31: %zd: entry:0x%" PRIx64 ", flags:0x%" PRIx64 "\n", i,
Prasad Kummarie0783112023-04-26 11:02:07 +0530184 TFAHandoffParams->partition[i].entry_point,
185 TFAHandoffParams->partition[i].flags);
Michal Simekef8f5592015-06-15 14:22:50 +0200186
Prasad Kummarie0783112023-04-26 11:02:07 +0530187 target_cpu = get_fsbl_cpu(&TFAHandoffParams->partition[i]);
Michal Simekef8f5592015-06-15 14:22:50 +0200188 if (target_cpu != FSBL_FLAGS_A53_0) {
189 WARN("BL31: invalid target CPU (%i)\n", target_cpu);
190 continue;
191 }
192
Prasad Kummarie0783112023-04-26 11:02:07 +0530193 target_el = get_fsbl_el(&TFAHandoffParams->partition[i]);
Michal Simekef8f5592015-06-15 14:22:50 +0200194 if ((target_el == FSBL_FLAGS_EL3) ||
195 (target_el == FSBL_FLAGS_EL0)) {
196 WARN("BL31: invalid exception level (%i)\n", target_el);
197 continue;
198 }
199
Prasad Kummarie0783112023-04-26 11:02:07 +0530200 target_secure = get_fsbl_ss(&TFAHandoffParams->partition[i]);
Michal Simekef8f5592015-06-15 14:22:50 +0200201 if (target_secure == FSBL_FLAGS_SECURE &&
202 target_el == FSBL_FLAGS_EL2) {
203 WARN("BL31: invalid security state (%i) for exception level (%i)\n",
204 target_secure, target_el);
205 continue;
206 }
207
Prasad Kummarie0783112023-04-26 11:02:07 +0530208 target_estate = get_fsbl_estate(&TFAHandoffParams->partition[i]);
209 target_endianness = get_fsbl_endian(&TFAHandoffParams->partition[i]);
Michal Simekef8f5592015-06-15 14:22:50 +0200210
211 if (target_secure == FSBL_FLAGS_SECURE) {
212 image = bl32;
213
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530214 if (target_estate == FSBL_FLAGS_ESTATE_A32) {
Michal Simekef8f5592015-06-15 14:22:50 +0200215 bl32->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
Soren Brinkmann8bcd3052016-05-29 09:48:26 -0700216 target_endianness,
Michal Simekef8f5592015-06-15 14:22:50 +0200217 DISABLE_ALL_EXCEPTIONS);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530218 } else {
Michal Simekef8f5592015-06-15 14:22:50 +0200219 bl32->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
220 DISABLE_ALL_EXCEPTIONS);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530221 }
Michal Simekef8f5592015-06-15 14:22:50 +0200222 } else {
223 image = bl33;
224
225 if (target_estate == FSBL_FLAGS_ESTATE_A32) {
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530226 if (target_el == FSBL_FLAGS_EL2) {
Michal Simekef8f5592015-06-15 14:22:50 +0200227 target_el = MODE32_hyp;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530228 } else {
Michal Simekef8f5592015-06-15 14:22:50 +0200229 target_el = MODE32_sys;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530230 }
Michal Simekef8f5592015-06-15 14:22:50 +0200231
232 bl33->spsr = SPSR_MODE32(target_el, SPSR_T_ARM,
Soren Brinkmann8bcd3052016-05-29 09:48:26 -0700233 target_endianness,
Michal Simekef8f5592015-06-15 14:22:50 +0200234 DISABLE_ALL_EXCEPTIONS);
235 } else {
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530236 if (target_el == FSBL_FLAGS_EL2) {
Michal Simekef8f5592015-06-15 14:22:50 +0200237 target_el = MODE_EL2;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530238 } else {
Michal Simekef8f5592015-06-15 14:22:50 +0200239 target_el = MODE_EL1;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530240 }
Michal Simekef8f5592015-06-15 14:22:50 +0200241
242 bl33->spsr = SPSR_64(target_el, MODE_SP_ELX,
243 DISABLE_ALL_EXCEPTIONS);
244 }
245 }
246
Scott Brandene5dcf982020-08-25 13:49:32 -0700247 VERBOSE("Setting up %s entry point to:%" PRIx64 ", el:%x\n",
Michal Simekef8f5592015-06-15 14:22:50 +0200248 target_secure == FSBL_FLAGS_SECURE ? "BL32" : "BL33",
Prasad Kummarie0783112023-04-26 11:02:07 +0530249 TFAHandoffParams->partition[i].entry_point,
Michal Simekef8f5592015-06-15 14:22:50 +0200250 target_el);
Prasad Kummarie0783112023-04-26 11:02:07 +0530251 image->pc = TFAHandoffParams->partition[i].entry_point;
Michal Simekef8f5592015-06-15 14:22:50 +0200252
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530253 if (target_endianness == SPSR_E_BIG) {
Michal Simekef8f5592015-06-15 14:22:50 +0200254 EP_SET_EE(image->h.attr, EP_EE_BIG);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530255 } else {
Michal Simekef8f5592015-06-15 14:22:50 +0200256 EP_SET_EE(image->h.attr, EP_EE_LITTLE);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530257 }
Michal Simekef8f5592015-06-15 14:22:50 +0200258 }
Siva Durga Prasad Paladugu8f499722018-05-17 15:17:46 +0530259
260 return FSBL_HANDOFF_SUCCESS;
Michal Simekef8f5592015-06-15 14:22:50 +0200261}