blob: 6fc00dd228eca9d52907e804f9faf5c4c9c38dc6 [file] [log] [blame]
Yatharth Kocharf528faf2016-06-28 16:58:26 +01001/*
Douglas Raillard306593d2017-02-24 18:14:15 +00002 * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
Yatharth Kocharf528faf2016-06-28 16:58:26 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Yatharth Kocharf528faf2016-06-28 16:58:26 +01005 */
6
7#ifndef __EL3_COMMON_MACROS_S__
8#define __EL3_COMMON_MACROS_S__
9
10#include <arch.h>
11#include <asm_macros.S>
12#include <assert_macros.S>
13
14 /*
15 * Helper macro to initialise EL3 registers we care about.
16 */
17 .macro el3_arch_init_common _exception_vectors
18 /* ---------------------------------------------------------------------
David Cunadofee86532017-04-13 22:38:29 +010019 * SCTLR has already been initialised - read current value before
20 * modifying.
21 *
22 * SCTLR.I: Enable the instruction cache.
23 *
24 * SCTLR.A: Enable Alignment fault checking. All instructions that load
25 * or store one or more registers have an alignment check that the
26 * address being accessed is aligned to the size of the data element(s)
27 * being accessed.
Yatharth Kocharf528faf2016-06-28 16:58:26 +010028 * ---------------------------------------------------------------------
29 */
David Cunadofee86532017-04-13 22:38:29 +010030 ldr r1, =(SCTLR_I_BIT | SCTLR_A_BIT)
Yatharth Kocharf528faf2016-06-28 16:58:26 +010031 ldcopr r0, SCTLR
32 orr r0, r0, r1
33 stcopr r0, SCTLR
34 isb
35
36 /* ---------------------------------------------------------------------
37 * Set the exception vectors (VBAR/MVBAR).
38 * ---------------------------------------------------------------------
39 */
40 ldr r0, =\_exception_vectors
41 stcopr r0, VBAR
42 stcopr r0, MVBAR
43 isb
44
David Cunadofee86532017-04-13 22:38:29 +010045 /* ---------------------------------------------------------------------
46 * Initialise SCR, setting all fields rather than relying on the hw.
47 *
48 * SCR.SIF: Enabled so that Secure state instruction fetches from
49 * Non-secure memory are not permitted.
50 * ---------------------------------------------------------------------
Yatharth Kocharf528faf2016-06-28 16:58:26 +010051 */
David Cunadofee86532017-04-13 22:38:29 +010052 ldr r0, =(SCR_RESET_VAL | SCR_SIF_BIT)
Yatharth Kocharf528faf2016-06-28 16:58:26 +010053 stcopr r0, SCR
54
55 /* -----------------------------------------------------
56 * Enable the Asynchronous data abort now that the
57 * exception vectors have been setup.
58 * -----------------------------------------------------
59 */
60 cpsie a
61 isb
62
David Cunadofee86532017-04-13 22:38:29 +010063 /* ---------------------------------------------------------------------
64 * Initialise NSACR, setting all the fields, except for the
65 * IMPLEMENTATION DEFINED field, rather than relying on the hw. Some
66 * fields are architecturally UNKNOWN on reset.
67 *
68 * NSACR_ENABLE_FP_ACCESS: Represents NSACR.cp11 and NSACR.cp10. The
69 * cp11 field is ignored, but is set to same value as cp10. The cp10
70 * field is set to allow access to Advanced SIMD and floating point
71 * features from both Security states.
72 * ---------------------------------------------------------------------
73 */
Yatharth Kocharf528faf2016-06-28 16:58:26 +010074 ldcopr r0, NSACR
David Cunadofee86532017-04-13 22:38:29 +010075 and r0, r0, #NSACR_IMP_DEF_MASK
76 orr r0, r0, #(NSACR_RESET_VAL | NSACR_ENABLE_FP_ACCESS)
Yatharth Kocharf528faf2016-06-28 16:58:26 +010077 stcopr r0, NSACR
78 isb
79
David Cunadofee86532017-04-13 22:38:29 +010080 /* ---------------------------------------------------------------------
81 * Initialise CPACR, setting all fields rather than relying on hw. Some
82 * fields are architecturally UNKNOWN on reset.
83 *
84 * CPACR.TRCDIS: Trap control for PL0 and PL1 System register accesses
85 * to trace registers. Set to zero to allow access.
86 *
87 * CPACR_ENABLE_FP_ACCESS: Represents CPACR.cp11 and CPACR.cp10. The
88 * cp11 field is ignored, but is set to same value as cp10. The cp10
89 * field is set to allow full access from PL0 and PL1 to floating-point
90 * and Advanced SIMD features.
91 * ---------------------------------------------------------------------
Yatharth Kocharf528faf2016-06-28 16:58:26 +010092 */
David Cunadofee86532017-04-13 22:38:29 +010093 ldr r0, =((CPACR_RESET_VAL | CPACR_ENABLE_FP_ACCESS) & ~(TRCDIS_BIT))
Yatharth Kocharf528faf2016-06-28 16:58:26 +010094 stcopr r0, CPACR
95 isb
96
David Cunadofee86532017-04-13 22:38:29 +010097 /* ---------------------------------------------------------------------
98 * Initialise FPEXC, setting all fields rather than relying on hw. Some
99 * fields are architecturally UNKNOWN on reset and are set to zero
100 * except for field(s) listed below.
101 *
102 * FPEXC.EN: Enable access to Advanced SIMD and floating point features
103 * from all exception levels.
104 * ---------------------------------------------------------------------
105 */
106 ldr r0, =(FPEXC_RESET_VAL | FPEXC_EN_BIT)
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100107 vmsr FPEXC, r0
108 isb
dp-arm595d0d52017-02-08 11:51:50 +0000109
David Cunadofee86532017-04-13 22:38:29 +0100110 /* ---------------------------------------------------------------------
111 * Initialise SDCR, setting all the fields rather than relying on hw.
112 *
113 * SDCR.SPD: Disable AArch32 privileged debug. Debug exceptions from
114 * Secure EL1 are disabled.
115 * ---------------------------------------------------------------------
116 */
117 ldr r0, =(SDCR_RESET_VAL | SDCR_SPD(SDCR_SPD_DISABLE))
dp-arm595d0d52017-02-08 11:51:50 +0000118 stcopr r0, SDCR
119
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100120 .endm
121
122/* -----------------------------------------------------------------------------
123 * This is the super set of actions that need to be performed during a cold boot
124 * or a warm boot in EL3. This code is shared by BL1 and BL32 (SP_MIN).
125 *
126 * This macro will always perform reset handling, architectural initialisations
127 * and stack setup. The rest of the actions are optional because they might not
128 * be needed, depending on the context in which this macro is called. This is
129 * why this macro is parameterised ; each parameter allows to enable/disable
130 * some actions.
131 *
David Cunadofee86532017-04-13 22:38:29 +0100132 * _init_sctlr:
133 * Whether the macro needs to initialise the SCTLR register including
134 * configuring the endianness of data accesses.
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100135 *
136 * _warm_boot_mailbox:
137 * Whether the macro needs to detect the type of boot (cold/warm). The
138 * detection is based on the platform entrypoint address : if it is zero
139 * then it is a cold boot, otherwise it is a warm boot. In the latter case,
140 * this macro jumps on the platform entrypoint address.
141 *
142 * _secondary_cold_boot:
143 * Whether the macro needs to identify the CPU that is calling it: primary
144 * CPU or secondary CPU. The primary CPU will be allowed to carry on with
145 * the platform initialisations, while the secondaries will be put in a
146 * platform-specific state in the meantime.
147 *
148 * If the caller knows this macro will only be called by the primary CPU
149 * then this parameter can be defined to 0 to skip this step.
150 *
151 * _init_memory:
152 * Whether the macro needs to initialise the memory.
153 *
154 * _init_c_runtime:
155 * Whether the macro needs to initialise the C runtime environment.
156 *
157 * _exception_vectors:
158 * Address of the exception vectors to program in the VBAR_EL3 register.
159 * -----------------------------------------------------------------------------
160 */
161 .macro el3_entrypoint_common \
David Cunadofee86532017-04-13 22:38:29 +0100162 _init_sctlr, _warm_boot_mailbox, _secondary_cold_boot, \
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100163 _init_memory, _init_c_runtime, _exception_vectors
164
165 /* Make sure we are in Secure Mode */
Antonio Nino Diaz7c65c1e2017-04-20 09:58:28 +0100166#if ENABLE_ASSERTIONS
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100167 ldcopr r0, SCR
168 tst r0, #SCR_NS_BIT
169 ASM_ASSERT(eq)
170#endif
171
David Cunadofee86532017-04-13 22:38:29 +0100172 .if \_init_sctlr
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100173 /* -------------------------------------------------------------
David Cunadofee86532017-04-13 22:38:29 +0100174 * This is the initialisation of SCTLR and so must ensure that
175 * all fields are explicitly set rather than relying on hw. Some
176 * fields reset to an IMPLEMENTATION DEFINED value.
177 *
178 * SCTLR.TE: Set to zero so that exceptions to an Exception
179 * Level executing at PL1 are taken to A32 state.
180 *
181 * SCTLR.EE: Set the CPU endianness before doing anything that
182 * might involve memory reads or writes. Set to zero to select
183 * Little Endian.
184 *
185 * SCTLR.V: Set to zero to select the normal exception vectors
186 * with base address held in VBAR.
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100187 * -------------------------------------------------------------
188 */
David Cunadofee86532017-04-13 22:38:29 +0100189 ldr r0, =(SCTLR_RESET_VAL & ~(SCTLR_TE_BIT | SCTLR_EE_BIT | SCTLR_V_BIT))
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100190 stcopr r0, SCTLR
191 isb
David Cunadofee86532017-04-13 22:38:29 +0100192 .endif /* _init_sctlr */
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100193
194 /* Switch to monitor mode */
195 cps #MODE32_mon
196 isb
197
198 .if \_warm_boot_mailbox
199 /* -------------------------------------------------------------
200 * This code will be executed for both warm and cold resets.
201 * Now is the time to distinguish between the two.
202 * Query the platform entrypoint address and if it is not zero
203 * then it means it is a warm boot so jump to this address.
204 * -------------------------------------------------------------
205 */
206 bl plat_get_my_entrypoint
207 cmp r0, #0
208 bxne r0
209 .endif /* _warm_boot_mailbox */
210
211 /* ---------------------------------------------------------------------
212 * It is a cold boot.
213 * Perform any processor specific actions upon reset e.g. cache, TLB
214 * invalidations etc.
215 * ---------------------------------------------------------------------
216 */
217 bl reset_handler
218
219 el3_arch_init_common \_exception_vectors
220
221 .if \_secondary_cold_boot
222 /* -------------------------------------------------------------
223 * Check if this is a primary or secondary CPU cold boot.
224 * The primary CPU will set up the platform while the
225 * secondaries are placed in a platform-specific state until the
226 * primary CPU performs the necessary actions to bring them out
227 * of that state and allows entry into the OS.
228 * -------------------------------------------------------------
229 */
230 bl plat_is_my_cpu_primary
231 cmp r0, #0
232 bne do_primary_cold_boot
233
234 /* This is a cold boot on a secondary CPU */
235 bl plat_secondary_cold_boot_setup
236 /* plat_secondary_cold_boot_setup() is not supposed to return */
Jeenu Viswambharan68aef102016-11-30 15:21:11 +0000237 no_ret plat_panic_handler
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100238
239 do_primary_cold_boot:
240 .endif /* _secondary_cold_boot */
241
242 /* ---------------------------------------------------------------------
243 * Initialize memory now. Secondary CPU initialization won't get to this
244 * point.
245 * ---------------------------------------------------------------------
246 */
247
248 .if \_init_memory
249 bl platform_mem_init
250 .endif /* _init_memory */
251
252 /* ---------------------------------------------------------------------
253 * Init C runtime environment:
254 * - Zero-initialise the NOBITS sections. There are 2 of them:
255 * - the .bss section;
256 * - the coherent memory section (if any).
257 * - Relocate the data section from ROM to RAM, if required.
258 * ---------------------------------------------------------------------
259 */
260 .if \_init_c_runtime
Masahiro Yamada441bfdd2016-12-25 23:36:24 +0900261#ifdef IMAGE_BL32
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100262 /* -----------------------------------------------------------------
263 * Invalidate the RW memory used by the BL32 (SP_MIN) image. This
264 * includes the data and NOBITS sections. This is done to
265 * safeguard against possible corruption of this memory by
266 * dirty cache lines in a system cache as a result of use by
267 * an earlier boot loader stage.
268 * -----------------------------------------------------------------
269 */
270 ldr r0, =__RW_START__
271 ldr r1, =__RW_END__
272 sub r1, r1, r0
273 bl inv_dcache_range
274#endif /* IMAGE_BL32 */
275
276 ldr r0, =__BSS_START__
277 ldr r1, =__BSS_SIZE__
278 bl zeromem
279
280#if USE_COHERENT_MEM
281 ldr r0, =__COHERENT_RAM_START__
282 ldr r1, =__COHERENT_RAM_UNALIGNED_SIZE__
283 bl zeromem
284#endif
285
Masahiro Yamada441bfdd2016-12-25 23:36:24 +0900286#ifdef IMAGE_BL1
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100287 /* -----------------------------------------------------
288 * Copy data from ROM to RAM.
289 * -----------------------------------------------------
290 */
291 ldr r0, =__DATA_RAM_START__
292 ldr r1, =__DATA_ROM_START__
293 ldr r2, =__DATA_SIZE__
Yatharth Kocharc44c5af2016-09-28 11:00:05 +0100294 bl memcpy4
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100295#endif
296 .endif /* _init_c_runtime */
297
298 /* ---------------------------------------------------------------------
299 * Allocate a stack whose memory will be marked as Normal-IS-WBWA when
300 * the MMU is enabled. There is no risk of reading stale stack memory
301 * after enabling the MMU as only the primary CPU is running at the
302 * moment.
303 * ---------------------------------------------------------------------
304 */
305 bl plat_set_my_stack
Douglas Raillard306593d2017-02-24 18:14:15 +0000306
307#if STACK_PROTECTOR_ENABLED
308 .if \_init_c_runtime
309 bl update_stack_protector_canary
310 .endif /* _init_c_runtime */
311#endif
Yatharth Kocharf528faf2016-06-28 16:58:26 +0100312 .endm
313
314#endif /* __EL3_COMMON_MACROS_S__ */