xref: /rk3399_ARM-atf/include/arch/aarch64/el3_common_macros.S (revision 0a580b5128d6da0e3885e2a541aae47a7f1f7365)
1f5478dedSAntonio Nino Diaz/*
20d020822SBoyan Karatotev * Copyright (c) 2015-2025, 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	 */
52b07c317fSBoyan Karatotev	bl	plat_my_core_pos
53b07c317fSBoyan Karatotev	bl	_cpu_data_by_index
54b07c317fSBoyan Karatotev	msr	tpidr_el3, x0
55f5478dedSAntonio Nino Diaz#endif /* IMAGE_BL31 */
56f5478dedSAntonio Nino Diaz
57f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
58f5478dedSAntonio Nino Diaz	 * Initialise SCR_EL3, setting all fields rather than relying on hw.
59f5478dedSAntonio Nino Diaz	 * All fields are architecturally UNKNOWN on reset. The following fields
60f5478dedSAntonio Nino Diaz	 * do not change during the TF lifetime. The remaining fields are set to
61f5478dedSAntonio Nino Diaz	 * zero here but are updated ahead of transitioning to a lower EL in the
62f5478dedSAntonio Nino Diaz	 * function cm_init_context_common().
63f5478dedSAntonio Nino Diaz	 *
648815cdafSManish Pandey	 * SCR_EL3.EEL2: Set to one if S-EL2 is present and enabled.
658815cdafSManish Pandey	 *
668815cdafSManish Pandey	 * NOTE: Modifying EEL2 bit along with EA bit ensures that we mitigate
678815cdafSManish Pandey	 * against ERRATA_V2_3099206.
68f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
69f5478dedSAntonio Nino Diaz	 */
7040e5f7a5SJayanth Dodderi Chidanand	mov_imm	x0, SCR_RESET_VAL
718815cdafSManish Pandey#if IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2
728815cdafSManish Pandey	mrs	x1, id_aa64pfr0_el1
738815cdafSManish Pandey	and	x1, x1, #(ID_AA64PFR0_SEL2_MASK << ID_AA64PFR0_SEL2_SHIFT)
748815cdafSManish Pandey	cbz	x1, 1f
758815cdafSManish Pandey	orr	x0, x0, #SCR_EEL2_BIT
768815cdafSManish Pandey#endif
778815cdafSManish Pandey1:
78f5478dedSAntonio Nino Diaz	msr	scr_el3, x0
79f5478dedSAntonio Nino Diaz
80f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
81f5478dedSAntonio Nino Diaz	 * Initialise MDCR_EL3, setting all fields rather than relying on hw.
82f5478dedSAntonio Nino Diaz	 * Some fields are architecturally UNKNOWN on reset.
83f5478dedSAntonio Nino Diaz	 */
8440e5f7a5SJayanth Dodderi Chidanand	mov_imm	x0, MDCR_EL3_RESET_VAL
85f5478dedSAntonio Nino Diaz	msr	mdcr_el3, x0
86f5478dedSAntonio Nino Diaz
87f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
88f5478dedSAntonio Nino Diaz	 * Initialise CPTR_EL3, setting all fields rather than relying on hw.
89f5478dedSAntonio Nino Diaz	 * All fields are architecturally UNKNOWN on reset.
90f0c96a2eSBoyan Karatotev	 * ---------------------------------------------------------------------
91f5478dedSAntonio Nino Diaz	 */
92f0c96a2eSBoyan Karatotev	mov_imm x0, CPTR_EL3_RESET_VAL
93f5478dedSAntonio Nino Diaz	msr	cptr_el3, x0
94f5478dedSAntonio Nino Diaz
95f5478dedSAntonio Nino Diaz	.endm
96f5478dedSAntonio Nino Diaz
97f5478dedSAntonio Nino Diaz/* -----------------------------------------------------------------------------
98f5478dedSAntonio Nino Diaz * This is the super set of actions that need to be performed during a cold boot
99f5478dedSAntonio Nino Diaz * or a warm boot in EL3. This code is shared by BL1 and BL31.
100f5478dedSAntonio Nino Diaz *
101f5478dedSAntonio Nino Diaz * This macro will always perform reset handling, architectural initialisations
102f5478dedSAntonio Nino Diaz * and stack setup. The rest of the actions are optional because they might not
103f5478dedSAntonio Nino Diaz * be needed, depending on the context in which this macro is called. This is
104f5478dedSAntonio Nino Diaz * why this macro is parameterised ; each parameter allows to enable/disable
105f5478dedSAntonio Nino Diaz * some actions.
106f5478dedSAntonio Nino Diaz *
107f5478dedSAntonio Nino Diaz *  _init_sctlr:
108f5478dedSAntonio Nino Diaz *	Whether the macro needs to initialise SCTLR_EL3, including configuring
109f5478dedSAntonio Nino Diaz *      the endianness of data accesses.
110f5478dedSAntonio Nino Diaz *
111f5478dedSAntonio Nino Diaz *  _warm_boot_mailbox:
112f5478dedSAntonio Nino Diaz *	Whether the macro needs to detect the type of boot (cold/warm). The
113f5478dedSAntonio Nino Diaz *	detection is based on the platform entrypoint address : if it is zero
114f5478dedSAntonio Nino Diaz *	then it is a cold boot, otherwise it is a warm boot. In the latter case,
115f5478dedSAntonio Nino Diaz *	this macro jumps on the platform entrypoint address.
116f5478dedSAntonio Nino Diaz *
117f5478dedSAntonio Nino Diaz *  _secondary_cold_boot:
118f5478dedSAntonio Nino Diaz *	Whether the macro needs to identify the CPU that is calling it: primary
119f5478dedSAntonio Nino Diaz *	CPU or secondary CPU. The primary CPU will be allowed to carry on with
120f5478dedSAntonio Nino Diaz *	the platform initialisations, while the secondaries will be put in a
121f5478dedSAntonio Nino Diaz *	platform-specific state in the meantime.
122f5478dedSAntonio Nino Diaz *
123f5478dedSAntonio Nino Diaz *	If the caller knows this macro will only be called by the primary CPU
124f5478dedSAntonio Nino Diaz *	then this parameter can be defined to 0 to skip this step.
125f5478dedSAntonio Nino Diaz *
126f5478dedSAntonio Nino Diaz * _init_memory:
127f5478dedSAntonio Nino Diaz *	Whether the macro needs to initialise the memory.
128f5478dedSAntonio Nino Diaz *
129f5478dedSAntonio Nino Diaz * _init_c_runtime:
130f5478dedSAntonio Nino Diaz *	Whether the macro needs to initialise the C runtime environment.
131f5478dedSAntonio Nino Diaz *
132f5478dedSAntonio Nino Diaz * _exception_vectors:
133f5478dedSAntonio Nino Diaz *	Address of the exception vectors to program in the VBAR_EL3 register.
134da90359bSManish Pandey *
135da90359bSManish Pandey * _pie_fixup_size:
136da90359bSManish Pandey *	Size of memory region to fixup Global Descriptor Table (GDT).
137da90359bSManish Pandey *
138da90359bSManish Pandey *	A non-zero value is expected when firmware needs GDT to be fixed-up.
139da90359bSManish Pandey *
140f5478dedSAntonio Nino Diaz * -----------------------------------------------------------------------------
141f5478dedSAntonio Nino Diaz */
142f5478dedSAntonio Nino Diaz	.macro el3_entrypoint_common					\
143f5478dedSAntonio Nino Diaz		_init_sctlr, _warm_boot_mailbox, _secondary_cold_boot,	\
144da90359bSManish Pandey		_init_memory, _init_c_runtime, _exception_vectors,	\
145da90359bSManish Pandey		_pie_fixup_size
146f5478dedSAntonio Nino Diaz
147f5478dedSAntonio Nino Diaz	.if \_init_sctlr
148f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
149f5478dedSAntonio Nino Diaz		 * This is the initialisation of SCTLR_EL3 and so must ensure
150f5478dedSAntonio Nino Diaz		 * that all fields are explicitly set rather than relying on hw.
151f5478dedSAntonio Nino Diaz		 * Some fields reset to an IMPLEMENTATION DEFINED value and
152f5478dedSAntonio Nino Diaz		 * others are architecturally UNKNOWN on reset.
153f5478dedSAntonio Nino Diaz		 *
154f5478dedSAntonio Nino Diaz		 * SCTLR.EE: Set the CPU endianness before doing anything that
155f5478dedSAntonio Nino Diaz		 *  might involve memory reads or writes. Set to zero to select
156f5478dedSAntonio Nino Diaz		 *  Little Endian.
157f5478dedSAntonio Nino Diaz		 *
158f5478dedSAntonio Nino Diaz		 * SCTLR_EL3.WXN: For the EL3 translation regime, this field can
159f5478dedSAntonio Nino Diaz		 *  force all memory regions that are writeable to be treated as
160f5478dedSAntonio Nino Diaz		 *  XN (Execute-never). Set to zero so that this control has no
161f5478dedSAntonio Nino Diaz		 *  effect on memory access permissions.
162f5478dedSAntonio Nino Diaz		 *
163f5478dedSAntonio Nino Diaz		 * SCTLR_EL3.SA: Set to zero to disable Stack Alignment check.
164f5478dedSAntonio Nino Diaz		 *
165f5478dedSAntonio Nino Diaz		 * SCTLR_EL3.A: Set to zero to disable Alignment fault checking.
166f5478dedSAntonio Nino Diaz		 *
167f5478dedSAntonio Nino Diaz		 * SCTLR.DSSBS: Set to zero to disable speculation store bypass
168f5478dedSAntonio Nino Diaz		 *  safe behaviour upon exception entry to EL3.
169f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
170f5478dedSAntonio Nino Diaz		 */
171f5478dedSAntonio Nino Diaz		mov_imm	x0, (SCTLR_RESET_VAL & ~(SCTLR_EE_BIT | SCTLR_WXN_BIT \
172f5478dedSAntonio Nino Diaz				| SCTLR_SA_BIT | SCTLR_A_BIT | SCTLR_DSSBS_BIT))
173970a4a8dSManish Pandey#if ENABLE_FEAT_RAS
1746597fcf1SManish Pandey		/* If FEAT_RAS is present assume FEAT_IESB is also present */
1756597fcf1SManish Pandey		orr	x0, x0, #SCTLR_IESB_BIT
1766597fcf1SManish Pandey#endif
177f5478dedSAntonio Nino Diaz		msr	sctlr_el3, x0
178f5478dedSAntonio Nino Diaz		isb
179f5478dedSAntonio Nino Diaz	.endif /* _init_sctlr */
180f5478dedSAntonio Nino Diaz
181f5478dedSAntonio Nino Diaz	.if \_warm_boot_mailbox
182f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
183f5478dedSAntonio Nino Diaz		 * This code will be executed for both warm and cold resets.
184f5478dedSAntonio Nino Diaz		 * Now is the time to distinguish between the two.
185f5478dedSAntonio Nino Diaz		 * Query the platform entrypoint address and if it is not zero
186f5478dedSAntonio Nino Diaz		 * then it means it is a warm boot so jump to this address.
187f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
188f5478dedSAntonio Nino Diaz		 */
189f5478dedSAntonio Nino Diaz		bl	plat_get_my_entrypoint
190f5478dedSAntonio Nino Diaz		cbz	x0, do_cold_boot
191f5478dedSAntonio Nino Diaz		br	x0
192f5478dedSAntonio Nino Diaz
193f5478dedSAntonio Nino Diaz	do_cold_boot:
194f5478dedSAntonio Nino Diaz	.endif /* _warm_boot_mailbox */
195f5478dedSAntonio Nino Diaz
196da90359bSManish Pandey	.if \_pie_fixup_size
197da90359bSManish Pandey#if ENABLE_PIE
198da90359bSManish Pandey		/*
199da90359bSManish Pandey		 * ------------------------------------------------------------
200da90359bSManish Pandey		 * If PIE is enabled fixup the Global descriptor Table only
201da90359bSManish Pandey		 * once during primary core cold boot path.
202da90359bSManish Pandey		 *
203da90359bSManish Pandey		 * Compile time base address, required for fixup, is calculated
204da90359bSManish Pandey		 * using "pie_fixup" label present within first page.
205da90359bSManish Pandey		 * ------------------------------------------------------------
206da90359bSManish Pandey		 */
207da90359bSManish Pandey	pie_fixup:
208da90359bSManish Pandey		ldr	x0, =pie_fixup
209d7b5f408SJimmy Brisson		and	x0, x0, #~(PAGE_SIZE_MASK)
210da90359bSManish Pandey		mov_imm	x1, \_pie_fixup_size
211da90359bSManish Pandey		add	x1, x1, x0
212da90359bSManish Pandey		bl	fixup_gdt_reloc
213da90359bSManish Pandey#endif /* ENABLE_PIE */
214da90359bSManish Pandey	.endif /* _pie_fixup_size */
215da90359bSManish Pandey
216f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
217f5478dedSAntonio Nino Diaz	 * Set the exception vectors.
218f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
219f5478dedSAntonio Nino Diaz	 */
220f5478dedSAntonio Nino Diaz	adr	x0, \_exception_vectors
221f5478dedSAntonio Nino Diaz	msr	vbar_el3, x0
222f5478dedSAntonio Nino Diaz	isb
223f5478dedSAntonio Nino Diaz
2240d020822SBoyan Karatotev	call_reset_handler
225f5478dedSAntonio Nino Diaz
226f5478dedSAntonio Nino Diaz	el3_arch_init_common
227f5478dedSAntonio Nino Diaz
22840e5f7a5SJayanth Dodderi Chidanand	/* ---------------------------------------------------------------------
22940e5f7a5SJayanth Dodderi Chidanand	 * Set the el3 execution context(i.e. root_context).
23040e5f7a5SJayanth Dodderi Chidanand	 * ---------------------------------------------------------------------
23140e5f7a5SJayanth Dodderi Chidanand	 */
23240e5f7a5SJayanth Dodderi Chidanand	setup_el3_execution_context
23340e5f7a5SJayanth Dodderi Chidanand
234f5478dedSAntonio Nino Diaz	.if \_secondary_cold_boot
235f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
236f5478dedSAntonio Nino Diaz		 * Check if this is a primary or secondary CPU cold boot.
237f5478dedSAntonio Nino Diaz		 * The primary CPU will set up the platform while the
238f5478dedSAntonio Nino Diaz		 * secondaries are placed in a platform-specific state until the
239f5478dedSAntonio Nino Diaz		 * primary CPU performs the necessary actions to bring them out
240f5478dedSAntonio Nino Diaz		 * of that state and allows entry into the OS.
241f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
242f5478dedSAntonio Nino Diaz		 */
243f5478dedSAntonio Nino Diaz		bl	plat_is_my_cpu_primary
244f5478dedSAntonio Nino Diaz		cbnz	w0, do_primary_cold_boot
245f5478dedSAntonio Nino Diaz
246f5478dedSAntonio Nino Diaz		/* This is a cold boot on a secondary CPU */
247f5478dedSAntonio Nino Diaz		bl	plat_secondary_cold_boot_setup
248f5478dedSAntonio Nino Diaz		/* plat_secondary_cold_boot_setup() is not supposed to return */
249f5478dedSAntonio Nino Diaz		bl	el3_panic
250f5478dedSAntonio Nino Diaz
251f5478dedSAntonio Nino Diaz	do_primary_cold_boot:
252f5478dedSAntonio Nino Diaz	.endif /* _secondary_cold_boot */
253f5478dedSAntonio Nino Diaz
254f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
255f5478dedSAntonio Nino Diaz	 * Initialize memory now. Secondary CPU initialization won't get to this
256f5478dedSAntonio Nino Diaz	 * point.
257f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
258f5478dedSAntonio Nino Diaz	 */
259f5478dedSAntonio Nino Diaz
260f5478dedSAntonio Nino Diaz	.if \_init_memory
261f5478dedSAntonio Nino Diaz		bl	platform_mem_init
262f5478dedSAntonio Nino Diaz	.endif /* _init_memory */
263f5478dedSAntonio Nino Diaz
264f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
265f5478dedSAntonio Nino Diaz	 * Init C runtime environment:
266f5478dedSAntonio Nino Diaz	 *   - Zero-initialise the NOBITS sections. There are 2 of them:
267f5478dedSAntonio Nino Diaz	 *       - the .bss section;
268f5478dedSAntonio Nino Diaz	 *       - the coherent memory section (if any).
269f5478dedSAntonio Nino Diaz	 *   - Relocate the data section from ROM to RAM, if required.
270f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
271f5478dedSAntonio Nino Diaz	 */
272f5478dedSAntonio Nino Diaz	.if \_init_c_runtime
2736c09af9fSZelalem Aweke#if defined(IMAGE_BL31) || (defined(IMAGE_BL2) && \
27442d4d3baSArvind Ram Prakash	((RESET_TO_BL2 && BL2_INV_DCACHE) || ENABLE_RME))
275f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
276f5478dedSAntonio Nino Diaz		 * Invalidate the RW memory used by the BL31 image. This
277f5478dedSAntonio Nino Diaz		 * includes the data and NOBITS sections. This is done to
278f5478dedSAntonio Nino Diaz		 * safeguard against possible corruption of this memory by
279f5478dedSAntonio Nino Diaz		 * dirty cache lines in a system cache as a result of use by
280596d20d9SZelalem Aweke		 * an earlier boot loader stage. If PIE is enabled however,
281596d20d9SZelalem Aweke		 * RO sections including the GOT may be modified during
282596d20d9SZelalem Aweke                 * pie fixup. Therefore, to be on the safe side, invalidate
283596d20d9SZelalem Aweke		 * the entire image region if PIE is enabled.
284f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
285f5478dedSAntonio Nino Diaz		 */
286596d20d9SZelalem Aweke#if ENABLE_PIE
287596d20d9SZelalem Aweke#if SEPARATE_CODE_AND_RODATA
288596d20d9SZelalem Aweke		adrp	x0, __TEXT_START__
289596d20d9SZelalem Aweke		add	x0, x0, :lo12:__TEXT_START__
290596d20d9SZelalem Aweke#else
291596d20d9SZelalem Aweke		adrp	x0, __RO_START__
292596d20d9SZelalem Aweke		add	x0, x0, :lo12:__RO_START__
293596d20d9SZelalem Aweke#endif /* SEPARATE_CODE_AND_RODATA */
294596d20d9SZelalem Aweke#else
295f5478dedSAntonio Nino Diaz		adrp	x0, __RW_START__
296f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__RW_START__
297596d20d9SZelalem Aweke#endif /* ENABLE_PIE */
298f5478dedSAntonio Nino Diaz		adrp	x1, __RW_END__
299f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12:__RW_END__
300f5478dedSAntonio Nino Diaz		sub	x1, x1, x0
301f5478dedSAntonio Nino Diaz		bl	inv_dcache_range
302f8578e64SSamuel Holland#if defined(IMAGE_BL31) && SEPARATE_NOBITS_REGION
303f8578e64SSamuel Holland		adrp	x0, __NOBITS_START__
304f8578e64SSamuel Holland		add	x0, x0, :lo12:__NOBITS_START__
305f8578e64SSamuel Holland		adrp	x1, __NOBITS_END__
306f8578e64SSamuel Holland		add	x1, x1, :lo12:__NOBITS_END__
307f8578e64SSamuel Holland		sub	x1, x1, x0
308f8578e64SSamuel Holland		bl	inv_dcache_range
309f8578e64SSamuel Holland#endif
31096a8ed14SJiafei Pan#if defined(IMAGE_BL2) && SEPARATE_BL2_NOLOAD_REGION
31196a8ed14SJiafei Pan		adrp	x0, __BL2_NOLOAD_START__
31296a8ed14SJiafei Pan		add	x0, x0, :lo12:__BL2_NOLOAD_START__
31396a8ed14SJiafei Pan		adrp	x1, __BL2_NOLOAD_END__
31496a8ed14SJiafei Pan		add	x1, x1, :lo12:__BL2_NOLOAD_END__
31596a8ed14SJiafei Pan		sub	x1, x1, x0
31696a8ed14SJiafei Pan		bl	inv_dcache_range
31796a8ed14SJiafei Pan#endif
318f5478dedSAntonio Nino Diaz#endif
319f5478dedSAntonio Nino Diaz		adrp	x0, __BSS_START__
320f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__BSS_START__
321f5478dedSAntonio Nino Diaz
322f5478dedSAntonio Nino Diaz		adrp	x1, __BSS_END__
323f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12:__BSS_END__
324f5478dedSAntonio Nino Diaz		sub	x1, x1, x0
325f5478dedSAntonio Nino Diaz		bl	zeromem
326f5478dedSAntonio Nino Diaz
327f5478dedSAntonio Nino Diaz#if USE_COHERENT_MEM
328f5478dedSAntonio Nino Diaz		adrp	x0, __COHERENT_RAM_START__
329f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__COHERENT_RAM_START__
330f5478dedSAntonio Nino Diaz		adrp	x1, __COHERENT_RAM_END_UNALIGNED__
331f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12: __COHERENT_RAM_END_UNALIGNED__
332f5478dedSAntonio Nino Diaz		sub	x1, x1, x0
333f5478dedSAntonio Nino Diaz		bl	zeromem
334f5478dedSAntonio Nino Diaz#endif
335f5478dedSAntonio Nino Diaz
33642d4d3baSArvind Ram Prakash#if defined(IMAGE_BL1) ||	\
33786acbbe2SYe Li	(defined(IMAGE_BL2) && RESET_TO_BL2 && BL2_IN_XIP_MEM) || \
33886acbbe2SYe Li	(defined(IMAGE_BL31) && SEPARATE_RWDATA_REGION)
33986acbbe2SYe Li
340f5478dedSAntonio Nino Diaz		adrp	x0, __DATA_RAM_START__
341f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__DATA_RAM_START__
342f5478dedSAntonio Nino Diaz		adrp	x1, __DATA_ROM_START__
343f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12:__DATA_ROM_START__
344f5478dedSAntonio Nino Diaz		adrp	x2, __DATA_RAM_END__
345f5478dedSAntonio Nino Diaz		add	x2, x2, :lo12:__DATA_RAM_END__
346f5478dedSAntonio Nino Diaz		sub	x2, x2, x0
347f5478dedSAntonio Nino Diaz		bl	memcpy16
348f5478dedSAntonio Nino Diaz#endif
349f5478dedSAntonio Nino Diaz	.endif /* _init_c_runtime */
350f5478dedSAntonio Nino Diaz
351f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
352f5478dedSAntonio Nino Diaz	 * Use SP_EL0 for the C runtime stack.
353f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
354f5478dedSAntonio Nino Diaz	 */
355f5478dedSAntonio Nino Diaz	msr	spsel, #0
356f5478dedSAntonio Nino Diaz
357f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
358f5478dedSAntonio Nino Diaz	 * Allocate a stack whose memory will be marked as Normal-IS-WBWA when
359f5478dedSAntonio Nino Diaz	 * the MMU is enabled. There is no risk of reading stale stack memory
360f5478dedSAntonio Nino Diaz	 * after enabling the MMU as only the primary CPU is running at the
361f5478dedSAntonio Nino Diaz	 * moment.
362f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
363f5478dedSAntonio Nino Diaz	 */
364f5478dedSAntonio Nino Diaz	bl	plat_set_my_stack
365f5478dedSAntonio Nino Diaz
366f5478dedSAntonio Nino Diaz#if STACK_PROTECTOR_ENABLED
367f5478dedSAntonio Nino Diaz	.if \_init_c_runtime
368f5478dedSAntonio Nino Diaz	bl	update_stack_protector_canary
369f5478dedSAntonio Nino Diaz	.endif /* _init_c_runtime */
370f5478dedSAntonio Nino Diaz#endif
371f5478dedSAntonio Nino Diaz	.endm
372f5478dedSAntonio Nino Diaz
3733b8456bdSManish V Badarkhe	.macro	apply_at_speculative_wa
3743b8456bdSManish V Badarkhe#if ERRATA_SPECULATIVE_AT
3753b8456bdSManish V Badarkhe	/*
376d87c0e27SManish Pandey	 * This function expects x30 has been saved.
377d87c0e27SManish Pandey	 * Also, save x29 which will be used in the called function.
3783b8456bdSManish V Badarkhe	 */
379d87c0e27SManish Pandey	str	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
3803b8456bdSManish V Badarkhe	bl	save_and_update_ptw_el1_sys_regs
381d87c0e27SManish Pandey	ldr	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
3823b8456bdSManish V Badarkhe#endif
3833b8456bdSManish V Badarkhe	.endm
3843b8456bdSManish V Badarkhe
3853b8456bdSManish V Badarkhe	.macro	restore_ptw_el1_sys_regs
3863b8456bdSManish V Badarkhe#if ERRATA_SPECULATIVE_AT
3873b8456bdSManish V Badarkhe	/* -----------------------------------------------------------
3883b8456bdSManish V Badarkhe	 * In case of ERRATA_SPECULATIVE_AT, must follow below order
3893b8456bdSManish V Badarkhe	 * to ensure that page table walk is not enabled until
3903b8456bdSManish V Badarkhe	 * restoration of all EL1 system registers. TCR_EL1 register
3913b8456bdSManish V Badarkhe	 * should be updated at the end which restores previous page
3923b8456bdSManish V Badarkhe	 * table walk setting of stage1 i.e.(TCR_EL1.EPDx) bits. ISB
3933b8456bdSManish V Badarkhe	 * ensures that CPU does below steps in order.
3943b8456bdSManish V Badarkhe	 *
3953b8456bdSManish V Badarkhe	 * 1. Ensure all other system registers are written before
3963b8456bdSManish V Badarkhe	 *    updating SCTLR_EL1 using ISB.
3973b8456bdSManish V Badarkhe	 * 2. Restore SCTLR_EL1 register.
3983b8456bdSManish V Badarkhe	 * 3. Ensure SCTLR_EL1 written successfully using ISB.
3993b8456bdSManish V Badarkhe	 * 4. Restore TCR_EL1 register.
4003b8456bdSManish V Badarkhe	 * -----------------------------------------------------------
4013b8456bdSManish V Badarkhe	 */
4023b8456bdSManish V Badarkhe	isb
40359b7c0a0SJayanth Dodderi Chidanand	ldp	x28, x29, [sp, #CTX_ERRATA_SPEC_AT_OFFSET + CTX_ERRATA_SPEC_AT_SCTLR_EL1]
4043b8456bdSManish V Badarkhe	msr	sctlr_el1, x28
4053b8456bdSManish V Badarkhe	isb
4063b8456bdSManish V Badarkhe	msr	tcr_el1, x29
4073b8456bdSManish V Badarkhe#endif
4083b8456bdSManish V Badarkhe	.endm
4093b8456bdSManish V Badarkhe
410461c0a5dSElizabeth Ho/* -----------------------------------------------------------------
411461c0a5dSElizabeth Ho * The below macro reads SCR_EL3 from the context structure to
412461c0a5dSElizabeth Ho * determine the security state of the context upon ERET.
413461c0a5dSElizabeth Ho * ------------------------------------------------------------------
414461c0a5dSElizabeth Ho */
415461c0a5dSElizabeth Ho	.macro get_security_state _ret:req, _scr_reg:req
416461c0a5dSElizabeth Ho		ubfx 	\_ret, \_scr_reg, #SCR_NSE_SHIFT, #1
417461c0a5dSElizabeth Ho		cmp 	\_ret, #1
418461c0a5dSElizabeth Ho		beq 	realm_state
419461c0a5dSElizabeth Ho		bfi	\_ret, \_scr_reg, #0, #1
420461c0a5dSElizabeth Ho		b 	end
421461c0a5dSElizabeth Ho	realm_state:
422461c0a5dSElizabeth Ho		mov 	\_ret, #2
423461c0a5dSElizabeth Ho	end:
424461c0a5dSElizabeth Ho	.endm
425461c0a5dSElizabeth Ho
42640e5f7a5SJayanth Dodderi Chidanand/*-----------------------------------------------------------------------------
42740e5f7a5SJayanth Dodderi Chidanand * Helper macro to configure EL3 registers we care about, while executing
42840e5f7a5SJayanth Dodderi Chidanand * at EL3/Root world. Root world has its own execution environment and
42940e5f7a5SJayanth Dodderi Chidanand * needs to have its settings configured to be independent of other worlds.
43040e5f7a5SJayanth Dodderi Chidanand * -----------------------------------------------------------------------------
43140e5f7a5SJayanth Dodderi Chidanand */
43240e5f7a5SJayanth Dodderi Chidanand	.macro setup_el3_execution_context
43340e5f7a5SJayanth Dodderi Chidanand
43440e5f7a5SJayanth Dodderi Chidanand	/* ---------------------------------------------------------------------
43540e5f7a5SJayanth Dodderi Chidanand	 * The following registers need to be part of separate root context
43640e5f7a5SJayanth Dodderi Chidanand	 * as their values are of importance during EL3 execution.
43740e5f7a5SJayanth Dodderi Chidanand	 * Hence these registers are overwritten to their intital values,
43840e5f7a5SJayanth Dodderi Chidanand	 * irrespective of whichever world they return from to ensure EL3 has a
43940e5f7a5SJayanth Dodderi Chidanand	 * consistent execution context throughout the lifetime of TF-A.
44040e5f7a5SJayanth Dodderi Chidanand	 *
44140e5f7a5SJayanth Dodderi Chidanand	 * DAIF.A: Enable External Aborts and SError Interrupts at EL3.
44240e5f7a5SJayanth Dodderi Chidanand	 *
44340e5f7a5SJayanth Dodderi Chidanand	 * MDCR_EL3.SDD: Set to one to disable AArch64 Secure self-hosted debug.
44440e5f7a5SJayanth Dodderi Chidanand	 *  Debug exceptions, other than Breakpoint Instruction exceptions, are
44540e5f7a5SJayanth Dodderi Chidanand	 *  disabled from all ELs in Secure state.
44640e5f7a5SJayanth Dodderi Chidanand	 *
44740e5f7a5SJayanth Dodderi Chidanand	 * SCR_EL3.EA: Set to one to enable SError interrupts at EL3.
44840e5f7a5SJayanth Dodderi Chidanand	 *
44940e5f7a5SJayanth Dodderi Chidanand	 * SCR_EL3.SIF: Set to one to disable instruction fetches from
45040e5f7a5SJayanth Dodderi Chidanand	 *  Non-secure memory.
45140e5f7a5SJayanth Dodderi Chidanand	 *
45240e5f7a5SJayanth Dodderi Chidanand	 * PMCR_EL0.DP: Set to one so that the cycle counter,
45340e5f7a5SJayanth Dodderi Chidanand	 *  PMCCNTR_EL0 does not count when event counting is prohibited.
45440e5f7a5SJayanth Dodderi Chidanand	 *  Necessary on PMUv3 <= p7 where MDCR_EL3.{SCCD,MCCD} are not
45540e5f7a5SJayanth Dodderi Chidanand	 *  available.
45640e5f7a5SJayanth Dodderi Chidanand	 *
457*0a580b51SBoyan Karatotev	 * CPTR_EL3.EZ: Set to one so that accesses to ZCR_EL3 do not trap
458*0a580b51SBoyan Karatotev	 * CPTR_EL3.TFP: Set to zero so that advanced SIMD operations don't trap
459*0a580b51SBoyan Karatotev	 * CPTR_EL3.ESM: Set to one so that SME related registers don't trap
460*0a580b51SBoyan Karatotev	 *
46140e5f7a5SJayanth Dodderi Chidanand	 * PSTATE.DIT: Set to one to enable the Data Independent Timing (DIT)
46240e5f7a5SJayanth Dodderi Chidanand	 *  functionality, if implemented in EL3.
46340e5f7a5SJayanth Dodderi Chidanand	 * ---------------------------------------------------------------------
46440e5f7a5SJayanth Dodderi Chidanand	 */
46540e5f7a5SJayanth Dodderi Chidanand		msr	daifclr, #DAIF_ABT_BIT
46640e5f7a5SJayanth Dodderi Chidanand
46740e5f7a5SJayanth Dodderi Chidanand		mrs 	x15, mdcr_el3
46840e5f7a5SJayanth Dodderi Chidanand		orr	x15, x15, #MDCR_SDD_BIT
46940e5f7a5SJayanth Dodderi Chidanand		msr	mdcr_el3, x15
47040e5f7a5SJayanth Dodderi Chidanand
47140e5f7a5SJayanth Dodderi Chidanand		mrs	x15, scr_el3
47240e5f7a5SJayanth Dodderi Chidanand		orr	x15, x15, #SCR_EA_BIT
47340e5f7a5SJayanth Dodderi Chidanand		orr	x15, x15, #SCR_SIF_BIT
47440e5f7a5SJayanth Dodderi Chidanand		msr	scr_el3, x15
47540e5f7a5SJayanth Dodderi Chidanand
47640e5f7a5SJayanth Dodderi Chidanand		mrs 	x15, pmcr_el0
47740e5f7a5SJayanth Dodderi Chidanand		orr	x15, x15, #PMCR_EL0_DP_BIT
47840e5f7a5SJayanth Dodderi Chidanand		msr	pmcr_el0, x15
47940e5f7a5SJayanth Dodderi Chidanand
480*0a580b51SBoyan Karatotev		mrs	x15, cptr_el3
481*0a580b51SBoyan Karatotev		orr	x15, x15, #CPTR_EZ_BIT
482*0a580b51SBoyan Karatotev		orr	x15, x15, #ESM_BIT
483*0a580b51SBoyan Karatotev		bic	x15, x15, #TFP_BIT
484*0a580b51SBoyan Karatotev		msr	cptr_el3, x15
485*0a580b51SBoyan Karatotev
48640e5f7a5SJayanth Dodderi Chidanand#if ENABLE_FEAT_DIT
48740e5f7a5SJayanth Dodderi Chidanand#if ENABLE_FEAT_DIT > 1
48840e5f7a5SJayanth Dodderi Chidanand		mrs	x15, id_aa64pfr0_el1
48940e5f7a5SJayanth Dodderi Chidanand		ubfx	x15, x15, #ID_AA64PFR0_DIT_SHIFT, #ID_AA64PFR0_DIT_LENGTH
49040e5f7a5SJayanth Dodderi Chidanand		cbz	x15, 1f
49140e5f7a5SJayanth Dodderi Chidanand#endif
49240e5f7a5SJayanth Dodderi Chidanand		mov	x15, #DIT_BIT
49340e5f7a5SJayanth Dodderi Chidanand		msr	DIT, x15
49440e5f7a5SJayanth Dodderi Chidanand	1:
49540e5f7a5SJayanth Dodderi Chidanand#endif
49640e5f7a5SJayanth Dodderi Chidanand
49740e5f7a5SJayanth Dodderi Chidanand		isb
49840e5f7a5SJayanth Dodderi Chidanand	.endm
49940e5f7a5SJayanth Dodderi Chidanand
500f5478dedSAntonio Nino Diaz#endif /* EL3_COMMON_MACROS_S */
501