xref: /rk3399_ARM-atf/include/arch/aarch64/el3_common_macros.S (revision 40e5f7a58f906beef74587a06f7fc35efe20537d)
1f5478dedSAntonio Nino Diaz/*
29e51f15eSSona Mathew * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
3f5478dedSAntonio Nino Diaz *
4f5478dedSAntonio Nino Diaz * SPDX-License-Identifier: BSD-3-Clause
5f5478dedSAntonio Nino Diaz */
6f5478dedSAntonio Nino Diaz
7f5478dedSAntonio Nino Diaz#ifndef EL3_COMMON_MACROS_S
8f5478dedSAntonio Nino Diaz#define EL3_COMMON_MACROS_S
9f5478dedSAntonio Nino Diaz
10f5478dedSAntonio Nino Diaz#include <arch.h>
11f5478dedSAntonio Nino Diaz#include <asm_macros.S>
127d33ffe4SDaniel Boulby#include <assert_macros.S>
133b8456bdSManish V Badarkhe#include <context.h>
141a04b2e5SVarun Wadekar#include <lib/xlat_tables/xlat_tables_defs.h>
15f5478dedSAntonio Nino Diaz
16f5478dedSAntonio Nino Diaz	/*
17f5478dedSAntonio Nino Diaz	 * Helper macro to initialise EL3 registers we care about.
18f5478dedSAntonio Nino Diaz	 */
19f5478dedSAntonio Nino Diaz	.macro el3_arch_init_common
20f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
21f5478dedSAntonio Nino Diaz	 * SCTLR_EL3 has already been initialised - read current value before
22f5478dedSAntonio Nino Diaz	 * modifying.
23f5478dedSAntonio Nino Diaz	 *
24f5478dedSAntonio Nino Diaz	 * SCTLR_EL3.I: Enable the instruction cache.
25f5478dedSAntonio Nino Diaz	 *
26f5478dedSAntonio Nino Diaz	 * SCTLR_EL3.SA: Enable Stack Alignment check. A SP alignment fault
27f5478dedSAntonio Nino Diaz	 *  exception is generated if a load or store instruction executed at
28f5478dedSAntonio Nino Diaz	 *  EL3 uses the SP as the base address and the SP is not aligned to a
29f5478dedSAntonio Nino Diaz	 *  16-byte boundary.
30f5478dedSAntonio Nino Diaz	 *
31f5478dedSAntonio Nino Diaz	 * SCTLR_EL3.A: Enable Alignment fault checking. All instructions that
32f5478dedSAntonio Nino Diaz	 *  load or store one or more registers have an alignment check that the
33f5478dedSAntonio Nino Diaz	 *  address being accessed is aligned to the size of the data element(s)
34f5478dedSAntonio Nino Diaz	 *  being accessed.
35f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
36f5478dedSAntonio Nino Diaz	 */
37f5478dedSAntonio Nino Diaz	mov	x1, #(SCTLR_I_BIT | SCTLR_A_BIT | SCTLR_SA_BIT)
38f5478dedSAntonio Nino Diaz	mrs	x0, sctlr_el3
39f5478dedSAntonio Nino Diaz	orr	x0, x0, x1
40f5478dedSAntonio Nino Diaz	msr	sctlr_el3, x0
41f5478dedSAntonio Nino Diaz	isb
42f5478dedSAntonio Nino Diaz
43f5478dedSAntonio Nino Diaz#ifdef IMAGE_BL31
44f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
45f5478dedSAntonio Nino Diaz	 * Initialise the per-cpu cache pointer to the CPU.
46f5478dedSAntonio Nino Diaz	 * This is done early to enable crash reporting to have access to crash
47f5478dedSAntonio Nino Diaz	 * stack. Since crash reporting depends on cpu_data to report the
48f5478dedSAntonio Nino Diaz	 * unhandled exception, not doing so can lead to recursive exceptions
49f5478dedSAntonio Nino Diaz	 * due to a NULL TPIDR_EL3.
50f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
51f5478dedSAntonio Nino Diaz	 */
52f5478dedSAntonio Nino Diaz	bl	init_cpu_data_ptr
53f5478dedSAntonio Nino Diaz#endif /* IMAGE_BL31 */
54f5478dedSAntonio Nino Diaz
55f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
56f5478dedSAntonio Nino Diaz	 * Initialise SCR_EL3, setting all fields rather than relying on hw.
57f5478dedSAntonio Nino Diaz	 * All fields are architecturally UNKNOWN on reset. The following fields
58f5478dedSAntonio Nino Diaz	 * do not change during the TF lifetime. The remaining fields are set to
59f5478dedSAntonio Nino Diaz	 * zero here but are updated ahead of transitioning to a lower EL in the
60f5478dedSAntonio Nino Diaz	 * function cm_init_context_common().
61f5478dedSAntonio Nino Diaz	 *
628815cdafSManish Pandey	 * SCR_EL3.EEL2: Set to one if S-EL2 is present and enabled.
638815cdafSManish Pandey	 *
648815cdafSManish Pandey	 * NOTE: Modifying EEL2 bit along with EA bit ensures that we mitigate
658815cdafSManish Pandey	 * against ERRATA_V2_3099206.
66f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
67f5478dedSAntonio Nino Diaz	 */
68*40e5f7a5SJayanth Dodderi Chidanand	mov_imm	x0, SCR_RESET_VAL
698815cdafSManish Pandey#if IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2
708815cdafSManish Pandey	mrs	x1, id_aa64pfr0_el1
718815cdafSManish Pandey	and	x1, x1, #(ID_AA64PFR0_SEL2_MASK << ID_AA64PFR0_SEL2_SHIFT)
728815cdafSManish Pandey	cbz	x1, 1f
738815cdafSManish Pandey	orr	x0, x0, #SCR_EEL2_BIT
748815cdafSManish Pandey#endif
758815cdafSManish Pandey1:
76f5478dedSAntonio Nino Diaz	msr	scr_el3, x0
77f5478dedSAntonio Nino Diaz
78f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
79f5478dedSAntonio Nino Diaz	 * Initialise MDCR_EL3, setting all fields rather than relying on hw.
80f5478dedSAntonio Nino Diaz	 * Some fields are architecturally UNKNOWN on reset.
81f5478dedSAntonio Nino Diaz	 */
82*40e5f7a5SJayanth Dodderi Chidanand	mov_imm	x0, MDCR_EL3_RESET_VAL
83f5478dedSAntonio Nino Diaz	msr	mdcr_el3, x0
84f5478dedSAntonio Nino Diaz
85f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
86f5478dedSAntonio Nino Diaz	 * Initialise CPTR_EL3, setting all fields rather than relying on hw.
87f5478dedSAntonio Nino Diaz	 * All fields are architecturally UNKNOWN on reset.
88f0c96a2eSBoyan Karatotev	 * ---------------------------------------------------------------------
89f5478dedSAntonio Nino Diaz	 */
90f0c96a2eSBoyan Karatotev	mov_imm x0, CPTR_EL3_RESET_VAL
91f5478dedSAntonio Nino Diaz	msr	cptr_el3, x0
92f5478dedSAntonio Nino Diaz
93f5478dedSAntonio Nino Diaz	.endm
94f5478dedSAntonio Nino Diaz
95f5478dedSAntonio Nino Diaz/* -----------------------------------------------------------------------------
96f5478dedSAntonio Nino Diaz * This is the super set of actions that need to be performed during a cold boot
97f5478dedSAntonio Nino Diaz * or a warm boot in EL3. This code is shared by BL1 and BL31.
98f5478dedSAntonio Nino Diaz *
99f5478dedSAntonio Nino Diaz * This macro will always perform reset handling, architectural initialisations
100f5478dedSAntonio Nino Diaz * and stack setup. The rest of the actions are optional because they might not
101f5478dedSAntonio Nino Diaz * be needed, depending on the context in which this macro is called. This is
102f5478dedSAntonio Nino Diaz * why this macro is parameterised ; each parameter allows to enable/disable
103f5478dedSAntonio Nino Diaz * some actions.
104f5478dedSAntonio Nino Diaz *
105f5478dedSAntonio Nino Diaz *  _init_sctlr:
106f5478dedSAntonio Nino Diaz *	Whether the macro needs to initialise SCTLR_EL3, including configuring
107f5478dedSAntonio Nino Diaz *      the endianness of data accesses.
108f5478dedSAntonio Nino Diaz *
109f5478dedSAntonio Nino Diaz *  _warm_boot_mailbox:
110f5478dedSAntonio Nino Diaz *	Whether the macro needs to detect the type of boot (cold/warm). The
111f5478dedSAntonio Nino Diaz *	detection is based on the platform entrypoint address : if it is zero
112f5478dedSAntonio Nino Diaz *	then it is a cold boot, otherwise it is a warm boot. In the latter case,
113f5478dedSAntonio Nino Diaz *	this macro jumps on the platform entrypoint address.
114f5478dedSAntonio Nino Diaz *
115f5478dedSAntonio Nino Diaz *  _secondary_cold_boot:
116f5478dedSAntonio Nino Diaz *	Whether the macro needs to identify the CPU that is calling it: primary
117f5478dedSAntonio Nino Diaz *	CPU or secondary CPU. The primary CPU will be allowed to carry on with
118f5478dedSAntonio Nino Diaz *	the platform initialisations, while the secondaries will be put in a
119f5478dedSAntonio Nino Diaz *	platform-specific state in the meantime.
120f5478dedSAntonio Nino Diaz *
121f5478dedSAntonio Nino Diaz *	If the caller knows this macro will only be called by the primary CPU
122f5478dedSAntonio Nino Diaz *	then this parameter can be defined to 0 to skip this step.
123f5478dedSAntonio Nino Diaz *
124f5478dedSAntonio Nino Diaz * _init_memory:
125f5478dedSAntonio Nino Diaz *	Whether the macro needs to initialise the memory.
126f5478dedSAntonio Nino Diaz *
127f5478dedSAntonio Nino Diaz * _init_c_runtime:
128f5478dedSAntonio Nino Diaz *	Whether the macro needs to initialise the C runtime environment.
129f5478dedSAntonio Nino Diaz *
130f5478dedSAntonio Nino Diaz * _exception_vectors:
131f5478dedSAntonio Nino Diaz *	Address of the exception vectors to program in the VBAR_EL3 register.
132da90359bSManish Pandey *
133da90359bSManish Pandey * _pie_fixup_size:
134da90359bSManish Pandey *	Size of memory region to fixup Global Descriptor Table (GDT).
135da90359bSManish Pandey *
136da90359bSManish Pandey *	A non-zero value is expected when firmware needs GDT to be fixed-up.
137da90359bSManish Pandey *
138f5478dedSAntonio Nino Diaz * -----------------------------------------------------------------------------
139f5478dedSAntonio Nino Diaz */
140f5478dedSAntonio Nino Diaz	.macro el3_entrypoint_common					\
141f5478dedSAntonio Nino Diaz		_init_sctlr, _warm_boot_mailbox, _secondary_cold_boot,	\
142da90359bSManish Pandey		_init_memory, _init_c_runtime, _exception_vectors,	\
143da90359bSManish Pandey		_pie_fixup_size
144f5478dedSAntonio Nino Diaz
145f5478dedSAntonio Nino Diaz	.if \_init_sctlr
146f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
147f5478dedSAntonio Nino Diaz		 * This is the initialisation of SCTLR_EL3 and so must ensure
148f5478dedSAntonio Nino Diaz		 * that all fields are explicitly set rather than relying on hw.
149f5478dedSAntonio Nino Diaz		 * Some fields reset to an IMPLEMENTATION DEFINED value and
150f5478dedSAntonio Nino Diaz		 * others are architecturally UNKNOWN on reset.
151f5478dedSAntonio Nino Diaz		 *
152f5478dedSAntonio Nino Diaz		 * SCTLR.EE: Set the CPU endianness before doing anything that
153f5478dedSAntonio Nino Diaz		 *  might involve memory reads or writes. Set to zero to select
154f5478dedSAntonio Nino Diaz		 *  Little Endian.
155f5478dedSAntonio Nino Diaz		 *
156f5478dedSAntonio Nino Diaz		 * SCTLR_EL3.WXN: For the EL3 translation regime, this field can
157f5478dedSAntonio Nino Diaz		 *  force all memory regions that are writeable to be treated as
158f5478dedSAntonio Nino Diaz		 *  XN (Execute-never). Set to zero so that this control has no
159f5478dedSAntonio Nino Diaz		 *  effect on memory access permissions.
160f5478dedSAntonio Nino Diaz		 *
161f5478dedSAntonio Nino Diaz		 * SCTLR_EL3.SA: Set to zero to disable Stack Alignment check.
162f5478dedSAntonio Nino Diaz		 *
163f5478dedSAntonio Nino Diaz		 * SCTLR_EL3.A: Set to zero to disable Alignment fault checking.
164f5478dedSAntonio Nino Diaz		 *
165f5478dedSAntonio Nino Diaz		 * SCTLR.DSSBS: Set to zero to disable speculation store bypass
166f5478dedSAntonio Nino Diaz		 *  safe behaviour upon exception entry to EL3.
167f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
168f5478dedSAntonio Nino Diaz		 */
169f5478dedSAntonio Nino Diaz		mov_imm	x0, (SCTLR_RESET_VAL & ~(SCTLR_EE_BIT | SCTLR_WXN_BIT \
170f5478dedSAntonio Nino Diaz				| SCTLR_SA_BIT | SCTLR_A_BIT | SCTLR_DSSBS_BIT))
171970a4a8dSManish Pandey#if ENABLE_FEAT_RAS
1726597fcf1SManish Pandey		/* If FEAT_RAS is present assume FEAT_IESB is also present */
1736597fcf1SManish Pandey		orr	x0, x0, #SCTLR_IESB_BIT
1746597fcf1SManish Pandey#endif
175f5478dedSAntonio Nino Diaz		msr	sctlr_el3, x0
176f5478dedSAntonio Nino Diaz		isb
177f5478dedSAntonio Nino Diaz	.endif /* _init_sctlr */
178f5478dedSAntonio Nino Diaz
179f5478dedSAntonio Nino Diaz	.if \_warm_boot_mailbox
180f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
181f5478dedSAntonio Nino Diaz		 * This code will be executed for both warm and cold resets.
182f5478dedSAntonio Nino Diaz		 * Now is the time to distinguish between the two.
183f5478dedSAntonio Nino Diaz		 * Query the platform entrypoint address and if it is not zero
184f5478dedSAntonio Nino Diaz		 * then it means it is a warm boot so jump to this address.
185f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
186f5478dedSAntonio Nino Diaz		 */
187f5478dedSAntonio Nino Diaz		bl	plat_get_my_entrypoint
188f5478dedSAntonio Nino Diaz		cbz	x0, do_cold_boot
189f5478dedSAntonio Nino Diaz		br	x0
190f5478dedSAntonio Nino Diaz
191f5478dedSAntonio Nino Diaz	do_cold_boot:
192f5478dedSAntonio Nino Diaz	.endif /* _warm_boot_mailbox */
193f5478dedSAntonio Nino Diaz
194da90359bSManish Pandey	.if \_pie_fixup_size
195da90359bSManish Pandey#if ENABLE_PIE
196da90359bSManish Pandey		/*
197da90359bSManish Pandey		 * ------------------------------------------------------------
198da90359bSManish Pandey		 * If PIE is enabled fixup the Global descriptor Table only
199da90359bSManish Pandey		 * once during primary core cold boot path.
200da90359bSManish Pandey		 *
201da90359bSManish Pandey		 * Compile time base address, required for fixup, is calculated
202da90359bSManish Pandey		 * using "pie_fixup" label present within first page.
203da90359bSManish Pandey		 * ------------------------------------------------------------
204da90359bSManish Pandey		 */
205da90359bSManish Pandey	pie_fixup:
206da90359bSManish Pandey		ldr	x0, =pie_fixup
207d7b5f408SJimmy Brisson		and	x0, x0, #~(PAGE_SIZE_MASK)
208da90359bSManish Pandey		mov_imm	x1, \_pie_fixup_size
209da90359bSManish Pandey		add	x1, x1, x0
210da90359bSManish Pandey		bl	fixup_gdt_reloc
211da90359bSManish Pandey#endif /* ENABLE_PIE */
212da90359bSManish Pandey	.endif /* _pie_fixup_size */
213da90359bSManish Pandey
214f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
215f5478dedSAntonio Nino Diaz	 * Set the exception vectors.
216f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
217f5478dedSAntonio Nino Diaz	 */
218f5478dedSAntonio Nino Diaz	adr	x0, \_exception_vectors
219f5478dedSAntonio Nino Diaz	msr	vbar_el3, x0
220f5478dedSAntonio Nino Diaz	isb
221f5478dedSAntonio Nino Diaz
2226c09af9fSZelalem Aweke#if !(defined(IMAGE_BL2) && ENABLE_RME)
223f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
224f5478dedSAntonio Nino Diaz	 * It is a cold boot.
225f5478dedSAntonio Nino Diaz	 * Perform any processor specific actions upon reset e.g. cache, TLB
226f5478dedSAntonio Nino Diaz	 * invalidations etc.
227f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
228f5478dedSAntonio Nino Diaz	 */
229f5478dedSAntonio Nino Diaz	bl	reset_handler
2306c09af9fSZelalem Aweke#endif
231f5478dedSAntonio Nino Diaz
232f5478dedSAntonio Nino Diaz	el3_arch_init_common
233f5478dedSAntonio Nino Diaz
234*40e5f7a5SJayanth Dodderi Chidanand	/* ---------------------------------------------------------------------
235*40e5f7a5SJayanth Dodderi Chidanand	 * Set the el3 execution context(i.e. root_context).
236*40e5f7a5SJayanth Dodderi Chidanand	 * ---------------------------------------------------------------------
237*40e5f7a5SJayanth Dodderi Chidanand	 */
238*40e5f7a5SJayanth Dodderi Chidanand	setup_el3_execution_context
239*40e5f7a5SJayanth Dodderi Chidanand
240f5478dedSAntonio Nino Diaz	.if \_secondary_cold_boot
241f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
242f5478dedSAntonio Nino Diaz		 * Check if this is a primary or secondary CPU cold boot.
243f5478dedSAntonio Nino Diaz		 * The primary CPU will set up the platform while the
244f5478dedSAntonio Nino Diaz		 * secondaries are placed in a platform-specific state until the
245f5478dedSAntonio Nino Diaz		 * primary CPU performs the necessary actions to bring them out
246f5478dedSAntonio Nino Diaz		 * of that state and allows entry into the OS.
247f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
248f5478dedSAntonio Nino Diaz		 */
249f5478dedSAntonio Nino Diaz		bl	plat_is_my_cpu_primary
250f5478dedSAntonio Nino Diaz		cbnz	w0, do_primary_cold_boot
251f5478dedSAntonio Nino Diaz
252f5478dedSAntonio Nino Diaz		/* This is a cold boot on a secondary CPU */
253f5478dedSAntonio Nino Diaz		bl	plat_secondary_cold_boot_setup
254f5478dedSAntonio Nino Diaz		/* plat_secondary_cold_boot_setup() is not supposed to return */
255f5478dedSAntonio Nino Diaz		bl	el3_panic
256f5478dedSAntonio Nino Diaz
257f5478dedSAntonio Nino Diaz	do_primary_cold_boot:
258f5478dedSAntonio Nino Diaz	.endif /* _secondary_cold_boot */
259f5478dedSAntonio Nino Diaz
260f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
261f5478dedSAntonio Nino Diaz	 * Initialize memory now. Secondary CPU initialization won't get to this
262f5478dedSAntonio Nino Diaz	 * point.
263f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
264f5478dedSAntonio Nino Diaz	 */
265f5478dedSAntonio Nino Diaz
266f5478dedSAntonio Nino Diaz	.if \_init_memory
267f5478dedSAntonio Nino Diaz		bl	platform_mem_init
268f5478dedSAntonio Nino Diaz	.endif /* _init_memory */
269f5478dedSAntonio Nino Diaz
270f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
271f5478dedSAntonio Nino Diaz	 * Init C runtime environment:
272f5478dedSAntonio Nino Diaz	 *   - Zero-initialise the NOBITS sections. There are 2 of them:
273f5478dedSAntonio Nino Diaz	 *       - the .bss section;
274f5478dedSAntonio Nino Diaz	 *       - the coherent memory section (if any).
275f5478dedSAntonio Nino Diaz	 *   - Relocate the data section from ROM to RAM, if required.
276f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
277f5478dedSAntonio Nino Diaz	 */
278f5478dedSAntonio Nino Diaz	.if \_init_c_runtime
2796c09af9fSZelalem Aweke#if defined(IMAGE_BL31) || (defined(IMAGE_BL2) && \
28042d4d3baSArvind Ram Prakash	((RESET_TO_BL2 && BL2_INV_DCACHE) || ENABLE_RME))
281f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
282f5478dedSAntonio Nino Diaz		 * Invalidate the RW memory used by the BL31 image. This
283f5478dedSAntonio Nino Diaz		 * includes the data and NOBITS sections. This is done to
284f5478dedSAntonio Nino Diaz		 * safeguard against possible corruption of this memory by
285f5478dedSAntonio Nino Diaz		 * dirty cache lines in a system cache as a result of use by
286596d20d9SZelalem Aweke		 * an earlier boot loader stage. If PIE is enabled however,
287596d20d9SZelalem Aweke		 * RO sections including the GOT may be modified during
288596d20d9SZelalem Aweke                 * pie fixup. Therefore, to be on the safe side, invalidate
289596d20d9SZelalem Aweke		 * the entire image region if PIE is enabled.
290f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
291f5478dedSAntonio Nino Diaz		 */
292596d20d9SZelalem Aweke#if ENABLE_PIE
293596d20d9SZelalem Aweke#if SEPARATE_CODE_AND_RODATA
294596d20d9SZelalem Aweke		adrp	x0, __TEXT_START__
295596d20d9SZelalem Aweke		add	x0, x0, :lo12:__TEXT_START__
296596d20d9SZelalem Aweke#else
297596d20d9SZelalem Aweke		adrp	x0, __RO_START__
298596d20d9SZelalem Aweke		add	x0, x0, :lo12:__RO_START__
299596d20d9SZelalem Aweke#endif /* SEPARATE_CODE_AND_RODATA */
300596d20d9SZelalem Aweke#else
301f5478dedSAntonio Nino Diaz		adrp	x0, __RW_START__
302f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__RW_START__
303596d20d9SZelalem Aweke#endif /* ENABLE_PIE */
304f5478dedSAntonio Nino Diaz		adrp	x1, __RW_END__
305f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12:__RW_END__
306f5478dedSAntonio Nino Diaz		sub	x1, x1, x0
307f5478dedSAntonio Nino Diaz		bl	inv_dcache_range
308f8578e64SSamuel Holland#if defined(IMAGE_BL31) && SEPARATE_NOBITS_REGION
309f8578e64SSamuel Holland		adrp	x0, __NOBITS_START__
310f8578e64SSamuel Holland		add	x0, x0, :lo12:__NOBITS_START__
311f8578e64SSamuel Holland		adrp	x1, __NOBITS_END__
312f8578e64SSamuel Holland		add	x1, x1, :lo12:__NOBITS_END__
313f8578e64SSamuel Holland		sub	x1, x1, x0
314f8578e64SSamuel Holland		bl	inv_dcache_range
315f8578e64SSamuel Holland#endif
31696a8ed14SJiafei Pan#if defined(IMAGE_BL2) && SEPARATE_BL2_NOLOAD_REGION
31796a8ed14SJiafei Pan		adrp	x0, __BL2_NOLOAD_START__
31896a8ed14SJiafei Pan		add	x0, x0, :lo12:__BL2_NOLOAD_START__
31996a8ed14SJiafei Pan		adrp	x1, __BL2_NOLOAD_END__
32096a8ed14SJiafei Pan		add	x1, x1, :lo12:__BL2_NOLOAD_END__
32196a8ed14SJiafei Pan		sub	x1, x1, x0
32296a8ed14SJiafei Pan		bl	inv_dcache_range
32396a8ed14SJiafei Pan#endif
324f5478dedSAntonio Nino Diaz#endif
325f5478dedSAntonio Nino Diaz		adrp	x0, __BSS_START__
326f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__BSS_START__
327f5478dedSAntonio Nino Diaz
328f5478dedSAntonio Nino Diaz		adrp	x1, __BSS_END__
329f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12:__BSS_END__
330f5478dedSAntonio Nino Diaz		sub	x1, x1, x0
331f5478dedSAntonio Nino Diaz		bl	zeromem
332f5478dedSAntonio Nino Diaz
333f5478dedSAntonio Nino Diaz#if USE_COHERENT_MEM
334f5478dedSAntonio Nino Diaz		adrp	x0, __COHERENT_RAM_START__
335f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__COHERENT_RAM_START__
336f5478dedSAntonio Nino Diaz		adrp	x1, __COHERENT_RAM_END_UNALIGNED__
337f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12: __COHERENT_RAM_END_UNALIGNED__
338f5478dedSAntonio Nino Diaz		sub	x1, x1, x0
339f5478dedSAntonio Nino Diaz		bl	zeromem
340f5478dedSAntonio Nino Diaz#endif
341f5478dedSAntonio Nino Diaz
34242d4d3baSArvind Ram Prakash#if defined(IMAGE_BL1) ||	\
34342d4d3baSArvind Ram Prakash	(defined(IMAGE_BL2) && RESET_TO_BL2 && BL2_IN_XIP_MEM)
344f5478dedSAntonio Nino Diaz		adrp	x0, __DATA_RAM_START__
345f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__DATA_RAM_START__
346f5478dedSAntonio Nino Diaz		adrp	x1, __DATA_ROM_START__
347f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12:__DATA_ROM_START__
348f5478dedSAntonio Nino Diaz		adrp	x2, __DATA_RAM_END__
349f5478dedSAntonio Nino Diaz		add	x2, x2, :lo12:__DATA_RAM_END__
350f5478dedSAntonio Nino Diaz		sub	x2, x2, x0
351f5478dedSAntonio Nino Diaz		bl	memcpy16
352f5478dedSAntonio Nino Diaz#endif
353f5478dedSAntonio Nino Diaz	.endif /* _init_c_runtime */
354f5478dedSAntonio Nino Diaz
355f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
356f5478dedSAntonio Nino Diaz	 * Use SP_EL0 for the C runtime stack.
357f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
358f5478dedSAntonio Nino Diaz	 */
359f5478dedSAntonio Nino Diaz	msr	spsel, #0
360f5478dedSAntonio Nino Diaz
361f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
362f5478dedSAntonio Nino Diaz	 * Allocate a stack whose memory will be marked as Normal-IS-WBWA when
363f5478dedSAntonio Nino Diaz	 * the MMU is enabled. There is no risk of reading stale stack memory
364f5478dedSAntonio Nino Diaz	 * after enabling the MMU as only the primary CPU is running at the
365f5478dedSAntonio Nino Diaz	 * moment.
366f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
367f5478dedSAntonio Nino Diaz	 */
368f5478dedSAntonio Nino Diaz	bl	plat_set_my_stack
369f5478dedSAntonio Nino Diaz
370f5478dedSAntonio Nino Diaz#if STACK_PROTECTOR_ENABLED
371f5478dedSAntonio Nino Diaz	.if \_init_c_runtime
372f5478dedSAntonio Nino Diaz	bl	update_stack_protector_canary
373f5478dedSAntonio Nino Diaz	.endif /* _init_c_runtime */
374f5478dedSAntonio Nino Diaz#endif
375f5478dedSAntonio Nino Diaz	.endm
376f5478dedSAntonio Nino Diaz
3773b8456bdSManish V Badarkhe	.macro	apply_at_speculative_wa
3783b8456bdSManish V Badarkhe#if ERRATA_SPECULATIVE_AT
3793b8456bdSManish V Badarkhe	/*
380d87c0e27SManish Pandey	 * This function expects x30 has been saved.
381d87c0e27SManish Pandey	 * Also, save x29 which will be used in the called function.
3823b8456bdSManish V Badarkhe	 */
383d87c0e27SManish Pandey	str	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
3843b8456bdSManish V Badarkhe	bl	save_and_update_ptw_el1_sys_regs
385d87c0e27SManish Pandey	ldr	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
3863b8456bdSManish V Badarkhe#endif
3873b8456bdSManish V Badarkhe	.endm
3883b8456bdSManish V Badarkhe
3893b8456bdSManish V Badarkhe	.macro	restore_ptw_el1_sys_regs
3903b8456bdSManish V Badarkhe#if ERRATA_SPECULATIVE_AT
3913b8456bdSManish V Badarkhe	/* -----------------------------------------------------------
3923b8456bdSManish V Badarkhe	 * In case of ERRATA_SPECULATIVE_AT, must follow below order
3933b8456bdSManish V Badarkhe	 * to ensure that page table walk is not enabled until
3943b8456bdSManish V Badarkhe	 * restoration of all EL1 system registers. TCR_EL1 register
3953b8456bdSManish V Badarkhe	 * should be updated at the end which restores previous page
3963b8456bdSManish V Badarkhe	 * table walk setting of stage1 i.e.(TCR_EL1.EPDx) bits. ISB
3973b8456bdSManish V Badarkhe	 * ensures that CPU does below steps in order.
3983b8456bdSManish V Badarkhe	 *
3993b8456bdSManish V Badarkhe	 * 1. Ensure all other system registers are written before
4003b8456bdSManish V Badarkhe	 *    updating SCTLR_EL1 using ISB.
4013b8456bdSManish V Badarkhe	 * 2. Restore SCTLR_EL1 register.
4023b8456bdSManish V Badarkhe	 * 3. Ensure SCTLR_EL1 written successfully using ISB.
4033b8456bdSManish V Badarkhe	 * 4. Restore TCR_EL1 register.
4043b8456bdSManish V Badarkhe	 * -----------------------------------------------------------
4053b8456bdSManish V Badarkhe	 */
4063b8456bdSManish V Badarkhe	isb
40759b7c0a0SJayanth Dodderi Chidanand	ldp	x28, x29, [sp, #CTX_ERRATA_SPEC_AT_OFFSET + CTX_ERRATA_SPEC_AT_SCTLR_EL1]
4083b8456bdSManish V Badarkhe	msr	sctlr_el1, x28
4093b8456bdSManish V Badarkhe	isb
4103b8456bdSManish V Badarkhe	msr	tcr_el1, x29
4113b8456bdSManish V Badarkhe#endif
4123b8456bdSManish V Badarkhe	.endm
4133b8456bdSManish V Badarkhe
414461c0a5dSElizabeth Ho/* -----------------------------------------------------------------
415461c0a5dSElizabeth Ho * The below macro reads SCR_EL3 from the context structure to
416461c0a5dSElizabeth Ho * determine the security state of the context upon ERET.
417461c0a5dSElizabeth Ho * ------------------------------------------------------------------
418461c0a5dSElizabeth Ho */
419461c0a5dSElizabeth Ho	.macro get_security_state _ret:req, _scr_reg:req
420461c0a5dSElizabeth Ho		ubfx 	\_ret, \_scr_reg, #SCR_NSE_SHIFT, #1
421461c0a5dSElizabeth Ho		cmp 	\_ret, #1
422461c0a5dSElizabeth Ho		beq 	realm_state
423461c0a5dSElizabeth Ho		bfi	\_ret, \_scr_reg, #0, #1
424461c0a5dSElizabeth Ho		b 	end
425461c0a5dSElizabeth Ho	realm_state:
426461c0a5dSElizabeth Ho		mov 	\_ret, #2
427461c0a5dSElizabeth Ho	end:
428461c0a5dSElizabeth Ho	.endm
429461c0a5dSElizabeth Ho
430*40e5f7a5SJayanth Dodderi Chidanand/*-----------------------------------------------------------------------------
431*40e5f7a5SJayanth Dodderi Chidanand * Helper macro to configure EL3 registers we care about, while executing
432*40e5f7a5SJayanth Dodderi Chidanand * at EL3/Root world. Root world has its own execution environment and
433*40e5f7a5SJayanth Dodderi Chidanand * needs to have its settings configured to be independent of other worlds.
434*40e5f7a5SJayanth Dodderi Chidanand * -----------------------------------------------------------------------------
435*40e5f7a5SJayanth Dodderi Chidanand */
436*40e5f7a5SJayanth Dodderi Chidanand	.macro setup_el3_execution_context
437*40e5f7a5SJayanth Dodderi Chidanand
438*40e5f7a5SJayanth Dodderi Chidanand	/* ---------------------------------------------------------------------
439*40e5f7a5SJayanth Dodderi Chidanand	 * The following registers need to be part of separate root context
440*40e5f7a5SJayanth Dodderi Chidanand	 * as their values are of importance during EL3 execution.
441*40e5f7a5SJayanth Dodderi Chidanand	 * Hence these registers are overwritten to their intital values,
442*40e5f7a5SJayanth Dodderi Chidanand	 * irrespective of whichever world they return from to ensure EL3 has a
443*40e5f7a5SJayanth Dodderi Chidanand	 * consistent execution context throughout the lifetime of TF-A.
444*40e5f7a5SJayanth Dodderi Chidanand	 *
445*40e5f7a5SJayanth Dodderi Chidanand	 * DAIF.A: Enable External Aborts and SError Interrupts at EL3.
446*40e5f7a5SJayanth Dodderi Chidanand	 *
447*40e5f7a5SJayanth Dodderi Chidanand	 * MDCR_EL3.SDD: Set to one to disable AArch64 Secure self-hosted debug.
448*40e5f7a5SJayanth Dodderi Chidanand	 *  Debug exceptions, other than Breakpoint Instruction exceptions, are
449*40e5f7a5SJayanth Dodderi Chidanand	 *  disabled from all ELs in Secure state.
450*40e5f7a5SJayanth Dodderi Chidanand	 *
451*40e5f7a5SJayanth Dodderi Chidanand	 * SCR_EL3.EA: Set to one to enable SError interrupts at EL3.
452*40e5f7a5SJayanth Dodderi Chidanand	 *
453*40e5f7a5SJayanth Dodderi Chidanand	 * SCR_EL3.SIF: Set to one to disable instruction fetches from
454*40e5f7a5SJayanth Dodderi Chidanand	 *  Non-secure memory.
455*40e5f7a5SJayanth Dodderi Chidanand	 *
456*40e5f7a5SJayanth Dodderi Chidanand	 * PMCR_EL0.DP: Set to one so that the cycle counter,
457*40e5f7a5SJayanth Dodderi Chidanand	 *  PMCCNTR_EL0 does not count when event counting is prohibited.
458*40e5f7a5SJayanth Dodderi Chidanand	 *  Necessary on PMUv3 <= p7 where MDCR_EL3.{SCCD,MCCD} are not
459*40e5f7a5SJayanth Dodderi Chidanand	 *  available.
460*40e5f7a5SJayanth Dodderi Chidanand	 *
461*40e5f7a5SJayanth Dodderi Chidanand	 * PSTATE.DIT: Set to one to enable the Data Independent Timing (DIT)
462*40e5f7a5SJayanth Dodderi Chidanand	 *  functionality, if implemented in EL3.
463*40e5f7a5SJayanth Dodderi Chidanand	 * ---------------------------------------------------------------------
464*40e5f7a5SJayanth Dodderi Chidanand	 */
465*40e5f7a5SJayanth Dodderi Chidanand		msr	daifclr, #DAIF_ABT_BIT
466*40e5f7a5SJayanth Dodderi Chidanand
467*40e5f7a5SJayanth Dodderi Chidanand		mrs 	x15, mdcr_el3
468*40e5f7a5SJayanth Dodderi Chidanand		orr	x15, x15, #MDCR_SDD_BIT
469*40e5f7a5SJayanth Dodderi Chidanand		msr	mdcr_el3, x15
470*40e5f7a5SJayanth Dodderi Chidanand
471*40e5f7a5SJayanth Dodderi Chidanand		mrs	x15, scr_el3
472*40e5f7a5SJayanth Dodderi Chidanand		orr	x15, x15, #SCR_EA_BIT
473*40e5f7a5SJayanth Dodderi Chidanand		orr	x15, x15, #SCR_SIF_BIT
474*40e5f7a5SJayanth Dodderi Chidanand		msr	scr_el3, x15
475*40e5f7a5SJayanth Dodderi Chidanand
476*40e5f7a5SJayanth Dodderi Chidanand		mrs 	x15, pmcr_el0
477*40e5f7a5SJayanth Dodderi Chidanand		orr	x15, x15, #PMCR_EL0_DP_BIT
478*40e5f7a5SJayanth Dodderi Chidanand		msr	pmcr_el0, x15
479*40e5f7a5SJayanth Dodderi Chidanand
480*40e5f7a5SJayanth Dodderi Chidanand#if ENABLE_FEAT_DIT
481*40e5f7a5SJayanth Dodderi Chidanand#if ENABLE_FEAT_DIT > 1
482*40e5f7a5SJayanth Dodderi Chidanand		mrs	x15, id_aa64pfr0_el1
483*40e5f7a5SJayanth Dodderi Chidanand		ubfx	x15, x15, #ID_AA64PFR0_DIT_SHIFT, #ID_AA64PFR0_DIT_LENGTH
484*40e5f7a5SJayanth Dodderi Chidanand		cbz	x15, 1f
485*40e5f7a5SJayanth Dodderi Chidanand#endif
486*40e5f7a5SJayanth Dodderi Chidanand		mov	x15, #DIT_BIT
487*40e5f7a5SJayanth Dodderi Chidanand		msr	DIT, x15
488*40e5f7a5SJayanth Dodderi Chidanand	1:
489*40e5f7a5SJayanth Dodderi Chidanand#endif
490*40e5f7a5SJayanth Dodderi Chidanand
491*40e5f7a5SJayanth Dodderi Chidanand		isb
492*40e5f7a5SJayanth Dodderi Chidanand	.endm
493*40e5f7a5SJayanth Dodderi Chidanand
494f5478dedSAntonio Nino Diaz#endif /* EL3_COMMON_MACROS_S */
495