xref: /rk3399_ARM-atf/plat/xilinx/common/plat_startup.c (revision e5daf0a5122744354d21787365c5b33a1690a7d4)
1 /*
2  * Copyright (c) 2014-2020, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <inttypes.h>
9 #include <stdint.h>
10 
11 #include <arch_helpers.h>
12 #include <common/debug.h>
13 #include <plat_startup.h>
14 
15 
16 /*
17  * ATFHandoffParams
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
25  */
26 
27 #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
31 
32 #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
36 
37 #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
41 
42 #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
48 
49 #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
55 
56 /**
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  */
63 static int32_t get_fsbl_cpu(const struct xfsbl_partition *partition)
64 {
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  */
77 static int32_t get_fsbl_el(const struct xfsbl_partition *partition)
78 {
79 	uint64_t flags = partition->flags & FSBL_FLAGS_EL_MASK;
80 
81 	return flags >> FSBL_FLAGS_EL_SHIFT;
82 }
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  */
91 static int32_t get_fsbl_ss(const struct xfsbl_partition *partition)
92 {
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  *
101  * Get the target endianness for @partition.
102  *
103  * Return: SPSR_E_LITTLE or SPSR_E_BIG
104  */
105 static int32_t get_fsbl_endian(const struct xfsbl_partition *partition)
106 {
107 	uint64_t flags = partition->flags & FSBL_FLAGS_ENDIAN_MASK;
108 
109 	flags >>= FSBL_FLAGS_ENDIAN_SHIFT;
110 
111 	if (flags == FSBL_FLAGS_ENDIAN_BE) {
112 		return SPSR_E_BIG;
113 	} else {
114 		return SPSR_E_LITTLE;
115 	}
116 }
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  */
125 static int32_t get_fsbl_estate(const struct xfsbl_partition *partition)
126 {
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
136  * atf_handoff_addr:  ATF handoff address
137  *
138  * Process the handoff paramters from the FSBL and populate the BL32 and BL33
139  * image info structures accordingly.
140  *
141  * Return: Return the status of the handoff. The value will be from the
142  *         fsbl_handoff enum.
143  */
144 enum fsbl_handoff fsbl_atf_handover(entry_point_info_t *bl32,
145 					entry_point_info_t *bl33,
146 					uint64_t atf_handoff_addr)
147 {
148 	const struct xfsbl_atf_handoff_params *ATFHandoffParams;
149 	assert((atf_handoff_addr < BL31_BASE) ||
150 	       (atf_handoff_addr > (uint64_t)&__BL31_END__));
151 	if (!atf_handoff_addr) {
152 		WARN("BL31: No ATF handoff structure passed\n");
153 		return FSBL_HANDOFF_NO_STRUCT;
154 	}
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')) {
161 		ERROR("BL31: invalid ATF handoff structure at %" PRIx64 "\n",
162 		      atf_handoff_addr);
163 		return FSBL_HANDOFF_INVAL_STRUCT;
164 	}
165 
166 	VERBOSE("BL31: ATF handoff params at:0x%" PRIx64 ", entries:%u\n",
167 		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);
171 		return FSBL_HANDOFF_TOO_MANY_PARTS;
172 	}
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;
181 		int32_t target_estate, target_secure, target_cpu;
182 		uint32_t target_endianness, target_el;
183 
184 		VERBOSE("BL31: %zd: entry:0x%" PRIx64 ", flags:0x%" PRIx64 "\n", i,
185 			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]);
210 		target_endianness = get_fsbl_endian(&ATFHandoffParams->partition[i]);
211 
212 		if (target_secure == FSBL_FLAGS_SECURE) {
213 			image = bl32;
214 
215 			if (target_estate == FSBL_FLAGS_ESTATE_A32) {
216 				bl32->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
217 							 target_endianness,
218 							 DISABLE_ALL_EXCEPTIONS);
219 			} else {
220 				bl32->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
221 						     DISABLE_ALL_EXCEPTIONS);
222 			}
223 		} else {
224 			image = bl33;
225 
226 			if (target_estate == FSBL_FLAGS_ESTATE_A32) {
227 				if (target_el == FSBL_FLAGS_EL2) {
228 					target_el = MODE32_hyp;
229 				} else {
230 					target_el = MODE32_sys;
231 				}
232 
233 				bl33->spsr = SPSR_MODE32(target_el, SPSR_T_ARM,
234 							 target_endianness,
235 							 DISABLE_ALL_EXCEPTIONS);
236 			} else {
237 				if (target_el == FSBL_FLAGS_EL2) {
238 					target_el = MODE_EL2;
239 				} else {
240 					target_el = MODE_EL1;
241 				}
242 
243 				bl33->spsr = SPSR_64(target_el, MODE_SP_ELX,
244 						     DISABLE_ALL_EXCEPTIONS);
245 			}
246 		}
247 
248 		VERBOSE("Setting up %s entry point to:%" PRIx64 ", el:%x\n",
249 			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 
254 		if (target_endianness == SPSR_E_BIG) {
255 			EP_SET_EE(image->h.attr, EP_EE_BIG);
256 		} else {
257 			EP_SET_EE(image->h.attr, EP_EE_LITTLE);
258 		}
259 	}
260 
261 	return FSBL_HANDOFF_SUCCESS;
262 }
263