2 * Copyright (c) 2014-2016, ARM Limited and Contributors. All rights reserved.
4 * SPDX-License-Identifier: BSD-3-Clause
9 #include <arch_helpers.h>
10 #include <common/debug.h>
13 #include "zynqmp_def.h"
14 #include "zynqmp_private.h"
18 * Parameter bitfield encoding
19 * -----------------------------------------------------------------------------
20 * Exec State 0 0 -> Aarch64, 1-> Aarch32
21 * endianness 1 0 -> LE, 1 -> BE
22 * 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
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
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
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
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
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
56 #define FSBL_MAX_PARTITIONS 8
58 /* Structure corresponding to each partition entry */
59 struct xfsbl_partition
{
64 /* Structure for handoff parameters to ARM Trusted Firmware (ATF) */
65 struct xfsbl_atf_handoff_params
{
68 struct xfsbl_partition partition
[FSBL_MAX_PARTITIONS
];
72 * @partition: Pointer to partition struct
74 * Get the target CPU for @partition.
76 * Return: FSBL_FLAGS_A53_0, FSBL_FLAGS_A53_1, FSBL_FLAGS_A53_2 or FSBL_FLAGS_A53_3
78 static int get_fsbl_cpu(const struct xfsbl_partition
*partition
)
80 uint64_t flags
= partition
->flags
& FSBL_FLAGS_CPU_MASK
;
82 return flags
>> FSBL_FLAGS_CPU_SHIFT
;
86 * @partition: Pointer to partition struct
88 * Get the target exception level for @partition.
90 * Return: FSBL_FLAGS_EL0, FSBL_FLAGS_EL1, FSBL_FLAGS_EL2 or FSBL_FLAGS_EL3
92 static int get_fsbl_el(const struct xfsbl_partition
*partition
)
94 uint64_t flags
= partition
->flags
& FSBL_FLAGS_EL_MASK
;
96 return flags
>> FSBL_FLAGS_EL_SHIFT
;
100 * @partition: Pointer to partition struct
102 * Get the target security state for @partition.
104 * Return: FSBL_FLAGS_NON_SECURE or FSBL_FLAGS_SECURE
106 static int get_fsbl_ss(const struct xfsbl_partition
*partition
)
108 uint64_t flags
= partition
->flags
& FSBL_FLAGS_TZ_MASK
;
110 return flags
>> FSBL_FLAGS_TZ_SHIFT
;
114 * @partition: Pointer to partition struct
116 * Get the target endianness for @partition.
118 * Return: SPSR_E_LITTLE or SPSR_E_BIG
120 static int get_fsbl_endian(const struct xfsbl_partition
*partition
)
122 uint64_t flags
= partition
->flags
& FSBL_FLAGS_ENDIAN_MASK
;
124 flags
>>= FSBL_FLAGS_ENDIAN_SHIFT
;
126 if (flags
== FSBL_FLAGS_ENDIAN_BE
)
129 return SPSR_E_LITTLE
;
133 * @partition: Pointer to partition struct
135 * Get the target execution state for @partition.
137 * Return: FSBL_FLAGS_ESTATE_A32 or FSBL_FLAGS_ESTATE_A64
139 static int get_fsbl_estate(const struct xfsbl_partition
*partition
)
141 uint64_t flags
= partition
->flags
& FSBL_FLAGS_ESTATE_MASK
;
143 return flags
>> FSBL_FLAGS_ESTATE_SHIFT
;
147 * Populates the bl32 and bl33 image info structures
148 * @bl32: BL32 image info structure
149 * @bl33: BL33 image info structure
151 * Process the handoff paramters from the FSBL and populate the BL32 and BL33
152 * image info structures accordingly.
154 * Return: Return the status of the handoff. The value will be from the
157 enum fsbl_handoff
fsbl_atf_handover(entry_point_info_t
*bl32
, entry_point_info_t
*bl33
)
159 uint64_t atf_handoff_addr
;
160 const struct xfsbl_atf_handoff_params
*ATFHandoffParams
;
162 atf_handoff_addr
= mmio_read_32(PMU_GLOBAL_GEN_STORAGE6
);
163 assert((atf_handoff_addr
< BL31_BASE
) ||
164 (atf_handoff_addr
> (uint64_t)&__BL31_END__
));
165 if (!atf_handoff_addr
) {
166 WARN("BL31: No ATF handoff structure passed\n");
167 return FSBL_HANDOFF_NO_STRUCT
;
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')) {
175 ERROR("BL31: invalid ATF handoff structure at %llx\n",
177 return FSBL_HANDOFF_INVAL_STRUCT
;
180 VERBOSE("BL31: ATF handoff params at:0x%llx, entries:%u\n",
181 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
);
185 return FSBL_HANDOFF_TOO_MANY_PARTS
;
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
193 for (size_t i
= 0; i
< ATFHandoffParams
->num_entries
; i
++) {
194 entry_point_info_t
*image
;
195 int target_estate
, target_secure
;
196 int target_cpu
, target_endianness
, target_el
;
198 VERBOSE("BL31: %zd: entry:0x%llx, flags:0x%llx\n", i
,
199 ATFHandoffParams
->partition
[i
].entry_point
,
200 ATFHandoffParams
->partition
[i
].flags
);
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
);
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
);
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
);
223 target_estate
= get_fsbl_estate(&ATFHandoffParams
->partition
[i
]);
224 target_endianness
= get_fsbl_endian(&ATFHandoffParams
->partition
[i
]);
226 if (target_secure
== FSBL_FLAGS_SECURE
) {
229 if (target_estate
== FSBL_FLAGS_ESTATE_A32
)
230 bl32
->spsr
= SPSR_MODE32(MODE32_svc
, SPSR_T_ARM
,
232 DISABLE_ALL_EXCEPTIONS
);
234 bl32
->spsr
= SPSR_64(MODE_EL1
, MODE_SP_ELX
,
235 DISABLE_ALL_EXCEPTIONS
);
239 if (target_estate
== FSBL_FLAGS_ESTATE_A32
) {
240 if (target_el
== FSBL_FLAGS_EL2
)
241 target_el
= MODE32_hyp
;
243 target_el
= MODE32_sys
;
245 bl33
->spsr
= SPSR_MODE32(target_el
, SPSR_T_ARM
,
247 DISABLE_ALL_EXCEPTIONS
);
249 if (target_el
== FSBL_FLAGS_EL2
)
250 target_el
= MODE_EL2
;
252 target_el
= MODE_EL1
;
254 bl33
->spsr
= SPSR_64(target_el
, MODE_SP_ELX
,
255 DISABLE_ALL_EXCEPTIONS
);
259 VERBOSE("Setting up %s entry point to:%llx, el:%x\n",
260 target_secure
== FSBL_FLAGS_SECURE
? "BL32" : "BL33",
261 ATFHandoffParams
->partition
[i
].entry_point
,
263 image
->pc
= ATFHandoffParams
->partition
[i
].entry_point
;
265 if (target_endianness
== SPSR_E_BIG
)
266 EP_SET_EE(image
->h
.attr
, EP_EE_BIG
);
268 EP_SET_EE(image
->h
.attr
, EP_EE_LITTLE
);
271 return FSBL_HANDOFF_SUCCESS
;