xref: /rk3399_ARM-atf/include/arch/aarch64/el3_common_macros.S (revision 7303319b3823e9e33748d963e9173f3678aba4da)
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>
1434a22a02SBoyan Karatotev#include <lib/el3_runtime/cpu_data.h>
15*98859b99SSammit Joshi#include <lib/per_cpu/per_cpu_macros.S>
16f5478dedSAntonio Nino Diaz
17f5478dedSAntonio Nino Diaz	/*
18f5478dedSAntonio Nino Diaz	 * Helper macro to initialise EL3 registers we care about.
19f5478dedSAntonio Nino Diaz	 */
20f5478dedSAntonio Nino Diaz	.macro el3_arch_init_common
21f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
22f5478dedSAntonio Nino Diaz	 * SCTLR_EL3 has already been initialised - read current value before
23f5478dedSAntonio Nino Diaz	 * modifying.
24f5478dedSAntonio Nino Diaz	 *
25f5478dedSAntonio Nino Diaz	 * SCTLR_EL3.I: Enable the instruction cache.
26f5478dedSAntonio Nino Diaz	 *
27f5478dedSAntonio Nino Diaz	 * SCTLR_EL3.SA: Enable Stack Alignment check. A SP alignment fault
28f5478dedSAntonio Nino Diaz	 *  exception is generated if a load or store instruction executed at
29f5478dedSAntonio Nino Diaz	 *  EL3 uses the SP as the base address and the SP is not aligned to a
30f5478dedSAntonio Nino Diaz	 *  16-byte boundary.
31f5478dedSAntonio Nino Diaz	 *
32f5478dedSAntonio Nino Diaz	 * SCTLR_EL3.A: Enable Alignment fault checking. All instructions that
33f5478dedSAntonio Nino Diaz	 *  load or store one or more registers have an alignment check that the
34f5478dedSAntonio Nino Diaz	 *  address being accessed is aligned to the size of the data element(s)
35f5478dedSAntonio Nino Diaz	 *  being accessed.
3610ecd580SBoyan Karatotev	 *
3710ecd580SBoyan Karatotev	 * SCTLR_EL3.BT: PAuth instructions are compatible with bti jc
38f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
39f5478dedSAntonio Nino Diaz	 */
4010ecd580SBoyan Karatotev	mov_imm	x1, (SCTLR_I_BIT | SCTLR_A_BIT | SCTLR_SA_BIT)
41f5478dedSAntonio Nino Diaz	mrs	x0, sctlr_el3
4210ecd580SBoyan Karatotev#if ENABLE_BTI
4310ecd580SBoyan Karatotev	bic	x0, x0, #SCTLR_BT_BIT
4410ecd580SBoyan Karatotev#endif
45f5478dedSAntonio Nino Diaz	orr	x0, x0, x1
46f5478dedSAntonio Nino Diaz	msr	sctlr_el3, x0
47f5478dedSAntonio Nino Diaz	isb
48f5478dedSAntonio Nino Diaz
49025b1b81SJohn Powell#if ENABLE_FEAT_SCTLR2
50025b1b81SJohn Powell#if ENABLE_FEAT_SCTLR2 > 1
51025b1b81SJohn Powell	is_feat_sctlr2_present_asm x1
52025b1b81SJohn Powell	beq	feat_sctlr2_not_supported\@
53025b1b81SJohn Powell#endif
54025b1b81SJohn Powell	mov	x1, #SCTLR2_RESET_VAL
55025b1b81SJohn Powell	msr	SCTLR2_EL3, x1
56025b1b81SJohn Powellfeat_sctlr2_not_supported\@:
57025b1b81SJohn Powell#endif
58025b1b81SJohn Powell
59f5478dedSAntonio Nino Diaz#ifdef IMAGE_BL31
60f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
61*98859b99SSammit Joshi	 * Initialise the per-cpu framework to utilize tpidr_el3.
62*98859b99SSammit Joshi	 *
63f5478dedSAntonio Nino Diaz	 * This is done early to enable crash reporting to have access to crash
64f5478dedSAntonio Nino Diaz	 * stack. Since crash reporting depends on cpu_data to report the
65f5478dedSAntonio Nino Diaz	 * unhandled exception, not doing so can lead to recursive exceptions
66f5478dedSAntonio Nino Diaz	 * due to a NULL TPIDR_EL3.
67f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
68f5478dedSAntonio Nino Diaz	 */
69*98859b99SSammit Joshi	per_cpu_init
70f5478dedSAntonio Nino Diaz#endif /* IMAGE_BL31 */
71f5478dedSAntonio Nino Diaz
72f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
73f5478dedSAntonio Nino Diaz	 * Initialise SCR_EL3, setting all fields rather than relying on hw.
74f5478dedSAntonio Nino Diaz	 * All fields are architecturally UNKNOWN on reset. The following fields
75f5478dedSAntonio Nino Diaz	 * do not change during the TF lifetime. The remaining fields are set to
76f5478dedSAntonio Nino Diaz	 * zero here but are updated ahead of transitioning to a lower EL in the
77f5478dedSAntonio Nino Diaz	 * function cm_init_context_common().
78f5478dedSAntonio Nino Diaz	 *
798815cdafSManish Pandey	 * SCR_EL3.EEL2: Set to one if S-EL2 is present and enabled.
808815cdafSManish Pandey	 *
818815cdafSManish Pandey	 * NOTE: Modifying EEL2 bit along with EA bit ensures that we mitigate
828815cdafSManish Pandey	 * against ERRATA_V2_3099206.
83f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
84f5478dedSAntonio Nino Diaz	 */
8540e5f7a5SJayanth Dodderi Chidanand	mov_imm	x0, SCR_RESET_VAL
868815cdafSManish Pandey#if IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2
878815cdafSManish Pandey	mrs	x1, id_aa64pfr0_el1
888815cdafSManish Pandey	and	x1, x1, #(ID_AA64PFR0_SEL2_MASK << ID_AA64PFR0_SEL2_SHIFT)
898815cdafSManish Pandey	cbz	x1, 1f
908815cdafSManish Pandey	orr	x0, x0, #SCR_EEL2_BIT
918815cdafSManish Pandey#endif
928815cdafSManish Pandey1:
93f5478dedSAntonio Nino Diaz	msr	scr_el3, x0
94f5478dedSAntonio Nino Diaz
95f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
96f5478dedSAntonio Nino Diaz	 * Initialise MDCR_EL3, setting all fields rather than relying on hw.
97f5478dedSAntonio Nino Diaz	 * Some fields are architecturally UNKNOWN on reset.
98f5478dedSAntonio Nino Diaz	 */
9940e5f7a5SJayanth Dodderi Chidanand	mov_imm	x0, MDCR_EL3_RESET_VAL
100f5478dedSAntonio Nino Diaz	msr	mdcr_el3, x0
101f5478dedSAntonio Nino Diaz
102f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
103f5478dedSAntonio Nino Diaz	 * Initialise CPTR_EL3, setting all fields rather than relying on hw.
104f5478dedSAntonio Nino Diaz	 * All fields are architecturally UNKNOWN on reset.
105f0c96a2eSBoyan Karatotev	 * ---------------------------------------------------------------------
106f5478dedSAntonio Nino Diaz	 */
107f0c96a2eSBoyan Karatotev	mov_imm x0, CPTR_EL3_RESET_VAL
108f5478dedSAntonio Nino Diaz	msr	cptr_el3, x0
109f5478dedSAntonio Nino Diaz
110f5478dedSAntonio Nino Diaz	.endm
111f5478dedSAntonio Nino Diaz
112f5478dedSAntonio Nino Diaz/* -----------------------------------------------------------------------------
113f5478dedSAntonio Nino Diaz * This is the super set of actions that need to be performed during a cold boot
114f5478dedSAntonio Nino Diaz * or a warm boot in EL3. This code is shared by BL1 and BL31.
115f5478dedSAntonio Nino Diaz *
116f5478dedSAntonio Nino Diaz * This macro will always perform reset handling, architectural initialisations
117f5478dedSAntonio Nino Diaz * and stack setup. The rest of the actions are optional because they might not
118f5478dedSAntonio Nino Diaz * be needed, depending on the context in which this macro is called. This is
119f5478dedSAntonio Nino Diaz * why this macro is parameterised ; each parameter allows to enable/disable
120f5478dedSAntonio Nino Diaz * some actions.
121f5478dedSAntonio Nino Diaz *
122f5478dedSAntonio Nino Diaz *  _init_sctlr:
123f5478dedSAntonio Nino Diaz *	Whether the macro needs to initialise SCTLR_EL3, including configuring
124f5478dedSAntonio Nino Diaz *      the endianness of data accesses.
125f5478dedSAntonio Nino Diaz *
126f5478dedSAntonio Nino Diaz *  _warm_boot_mailbox:
127f5478dedSAntonio Nino Diaz *	Whether the macro needs to detect the type of boot (cold/warm). The
128f5478dedSAntonio Nino Diaz *	detection is based on the platform entrypoint address : if it is zero
129f5478dedSAntonio Nino Diaz *	then it is a cold boot, otherwise it is a warm boot. In the latter case,
130f5478dedSAntonio Nino Diaz *	this macro jumps on the platform entrypoint address.
131f5478dedSAntonio Nino Diaz *
132f5478dedSAntonio Nino Diaz *  _secondary_cold_boot:
133f5478dedSAntonio Nino Diaz *	Whether the macro needs to identify the CPU that is calling it: primary
134f5478dedSAntonio Nino Diaz *	CPU or secondary CPU. The primary CPU will be allowed to carry on with
135f5478dedSAntonio Nino Diaz *	the platform initialisations, while the secondaries will be put in a
136f5478dedSAntonio Nino Diaz *	platform-specific state in the meantime.
137f5478dedSAntonio Nino Diaz *
138f5478dedSAntonio Nino Diaz *	If the caller knows this macro will only be called by the primary CPU
139f5478dedSAntonio Nino Diaz *	then this parameter can be defined to 0 to skip this step.
140f5478dedSAntonio Nino Diaz *
141f5478dedSAntonio Nino Diaz * _init_memory:
142f5478dedSAntonio Nino Diaz *	Whether the macro needs to initialise the memory.
143f5478dedSAntonio Nino Diaz *
144f5478dedSAntonio Nino Diaz * _init_c_runtime:
145f5478dedSAntonio Nino Diaz *	Whether the macro needs to initialise the C runtime environment.
146f5478dedSAntonio Nino Diaz *
147f5478dedSAntonio Nino Diaz * _exception_vectors:
148f5478dedSAntonio Nino Diaz *	Address of the exception vectors to program in the VBAR_EL3 register.
149da90359bSManish Pandey *
150da90359bSManish Pandey * _pie_fixup_size:
151da90359bSManish Pandey *	Size of memory region to fixup Global Descriptor Table (GDT).
152da90359bSManish Pandey *
153da90359bSManish Pandey *	A non-zero value is expected when firmware needs GDT to be fixed-up.
154da90359bSManish Pandey *
155f5478dedSAntonio Nino Diaz * -----------------------------------------------------------------------------
156f5478dedSAntonio Nino Diaz */
157f5478dedSAntonio Nino Diaz	.macro el3_entrypoint_common					\
158f5478dedSAntonio Nino Diaz		_init_sctlr, _warm_boot_mailbox, _secondary_cold_boot,	\
159da90359bSManish Pandey		_init_memory, _init_c_runtime, _exception_vectors,	\
160da90359bSManish Pandey		_pie_fixup_size
161f5478dedSAntonio Nino Diaz
162f5478dedSAntonio Nino Diaz	.if \_init_sctlr
163f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
164f5478dedSAntonio Nino Diaz		 * This is the initialisation of SCTLR_EL3 and so must ensure
165f5478dedSAntonio Nino Diaz		 * that all fields are explicitly set rather than relying on hw.
166f5478dedSAntonio Nino Diaz		 * Some fields reset to an IMPLEMENTATION DEFINED value and
167f5478dedSAntonio Nino Diaz		 * others are architecturally UNKNOWN on reset.
168f5478dedSAntonio Nino Diaz		 *
169f5478dedSAntonio Nino Diaz		 * SCTLR.EE: Set the CPU endianness before doing anything that
170f5478dedSAntonio Nino Diaz		 *  might involve memory reads or writes. Set to zero to select
171f5478dedSAntonio Nino Diaz		 *  Little Endian.
172f5478dedSAntonio Nino Diaz		 *
173f5478dedSAntonio Nino Diaz		 * SCTLR_EL3.WXN: For the EL3 translation regime, this field can
174f5478dedSAntonio Nino Diaz		 *  force all memory regions that are writeable to be treated as
175f5478dedSAntonio Nino Diaz		 *  XN (Execute-never). Set to zero so that this control has no
176f5478dedSAntonio Nino Diaz		 *  effect on memory access permissions.
177f5478dedSAntonio Nino Diaz		 *
178f5478dedSAntonio Nino Diaz		 * SCTLR_EL3.SA: Set to zero to disable Stack Alignment check.
179f5478dedSAntonio Nino Diaz		 *
180f5478dedSAntonio Nino Diaz		 * SCTLR_EL3.A: Set to zero to disable Alignment fault checking.
181f5478dedSAntonio Nino Diaz		 *
182f5478dedSAntonio Nino Diaz		 * SCTLR.DSSBS: Set to zero to disable speculation store bypass
183f5478dedSAntonio Nino Diaz		 *  safe behaviour upon exception entry to EL3.
184f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
185f5478dedSAntonio Nino Diaz		 */
186f5478dedSAntonio Nino Diaz		mov_imm	x0, (SCTLR_RESET_VAL & ~(SCTLR_EE_BIT | SCTLR_WXN_BIT \
187f5478dedSAntonio Nino Diaz				| SCTLR_SA_BIT | SCTLR_A_BIT | SCTLR_DSSBS_BIT))
188970a4a8dSManish Pandey#if ENABLE_FEAT_RAS
1896597fcf1SManish Pandey		/* If FEAT_RAS is present assume FEAT_IESB is also present */
1906597fcf1SManish Pandey		orr	x0, x0, #SCTLR_IESB_BIT
1916597fcf1SManish Pandey#endif
192f5478dedSAntonio Nino Diaz		msr	sctlr_el3, x0
193f5478dedSAntonio Nino Diaz		isb
194f5478dedSAntonio Nino Diaz	.endif /* _init_sctlr */
195f5478dedSAntonio Nino Diaz
196f5478dedSAntonio Nino Diaz	.if \_warm_boot_mailbox
197f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
198f5478dedSAntonio Nino Diaz		 * This code will be executed for both warm and cold resets.
199f5478dedSAntonio Nino Diaz		 * Now is the time to distinguish between the two.
200f5478dedSAntonio Nino Diaz		 * Query the platform entrypoint address and if it is not zero
201f5478dedSAntonio Nino Diaz		 * then it means it is a warm boot so jump to this address.
202f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
203f5478dedSAntonio Nino Diaz		 */
204f5478dedSAntonio Nino Diaz		bl	plat_get_my_entrypoint
205f5478dedSAntonio Nino Diaz		cbz	x0, do_cold_boot
206f5478dedSAntonio Nino Diaz		br	x0
207f5478dedSAntonio Nino Diaz
208f5478dedSAntonio Nino Diaz	do_cold_boot:
209f5478dedSAntonio Nino Diaz	.endif /* _warm_boot_mailbox */
210f5478dedSAntonio Nino Diaz
211da90359bSManish Pandey	.if \_pie_fixup_size
212da90359bSManish Pandey#if ENABLE_PIE
213da90359bSManish Pandey		/*
214da90359bSManish Pandey		 * ------------------------------------------------------------
215da90359bSManish Pandey		 * If PIE is enabled fixup the Global descriptor Table only
216da90359bSManish Pandey		 * once during primary core cold boot path.
217da90359bSManish Pandey		 *
218da90359bSManish Pandey		 * Compile time base address, required for fixup, is calculated
219da90359bSManish Pandey		 * using "pie_fixup" label present within first page.
220da90359bSManish Pandey		 * ------------------------------------------------------------
221da90359bSManish Pandey		 */
222da90359bSManish Pandey	pie_fixup:
223da90359bSManish Pandey		ldr	x0, =pie_fixup
224d7b5f408SJimmy Brisson		and	x0, x0, #~(PAGE_SIZE_MASK)
225da90359bSManish Pandey		mov_imm	x1, \_pie_fixup_size
226da90359bSManish Pandey		add	x1, x1, x0
227da90359bSManish Pandey		bl	fixup_gdt_reloc
228da90359bSManish Pandey#endif /* ENABLE_PIE */
229da90359bSManish Pandey	.endif /* _pie_fixup_size */
230da90359bSManish Pandey
231f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
232f5478dedSAntonio Nino Diaz	 * Set the exception vectors.
233f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
234f5478dedSAntonio Nino Diaz	 */
235f5478dedSAntonio Nino Diaz	adr	x0, \_exception_vectors
236f5478dedSAntonio Nino Diaz	msr	vbar_el3, x0
237f5478dedSAntonio Nino Diaz	isb
238f5478dedSAntonio Nino Diaz
2390d020822SBoyan Karatotev	call_reset_handler
240f5478dedSAntonio Nino Diaz
241f5478dedSAntonio Nino Diaz	el3_arch_init_common
242f5478dedSAntonio Nino Diaz
24340e5f7a5SJayanth Dodderi Chidanand	/* ---------------------------------------------------------------------
24440e5f7a5SJayanth Dodderi Chidanand	 * Set the el3 execution context(i.e. root_context).
24540e5f7a5SJayanth Dodderi Chidanand	 * ---------------------------------------------------------------------
24640e5f7a5SJayanth Dodderi Chidanand	 */
24740e5f7a5SJayanth Dodderi Chidanand	setup_el3_execution_context
24840e5f7a5SJayanth Dodderi Chidanand
249f5478dedSAntonio Nino Diaz	.if \_secondary_cold_boot
250f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
251f5478dedSAntonio Nino Diaz		 * Check if this is a primary or secondary CPU cold boot.
252f5478dedSAntonio Nino Diaz		 * The primary CPU will set up the platform while the
253f5478dedSAntonio Nino Diaz		 * secondaries are placed in a platform-specific state until the
254f5478dedSAntonio Nino Diaz		 * primary CPU performs the necessary actions to bring them out
255f5478dedSAntonio Nino Diaz		 * of that state and allows entry into the OS.
256f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
257f5478dedSAntonio Nino Diaz		 */
258f5478dedSAntonio Nino Diaz		bl	plat_is_my_cpu_primary
259f5478dedSAntonio Nino Diaz		cbnz	w0, do_primary_cold_boot
260f5478dedSAntonio Nino Diaz
261f5478dedSAntonio Nino Diaz		/* This is a cold boot on a secondary CPU */
262f5478dedSAntonio Nino Diaz		bl	plat_secondary_cold_boot_setup
263f5478dedSAntonio Nino Diaz		/* plat_secondary_cold_boot_setup() is not supposed to return */
264f5478dedSAntonio Nino Diaz		bl	el3_panic
265f5478dedSAntonio Nino Diaz
266f5478dedSAntonio Nino Diaz	do_primary_cold_boot:
267f5478dedSAntonio Nino Diaz	.endif /* _secondary_cold_boot */
268f5478dedSAntonio Nino Diaz
269f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
270f5478dedSAntonio Nino Diaz	 * Initialize memory now. Secondary CPU initialization won't get to this
271f5478dedSAntonio Nino Diaz	 * point.
272f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
273f5478dedSAntonio Nino Diaz	 */
274f5478dedSAntonio Nino Diaz
275f5478dedSAntonio Nino Diaz	.if \_init_memory
276f5478dedSAntonio Nino Diaz		bl	platform_mem_init
277f5478dedSAntonio Nino Diaz	.endif /* _init_memory */
278f5478dedSAntonio Nino Diaz
279f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
280f5478dedSAntonio Nino Diaz	 * Init C runtime environment:
281f5478dedSAntonio Nino Diaz	 *   - Zero-initialise the NOBITS sections. There are 2 of them:
282f5478dedSAntonio Nino Diaz	 *       - the .bss section;
283f5478dedSAntonio Nino Diaz	 *       - the coherent memory section (if any).
284f5478dedSAntonio Nino Diaz	 *   - Relocate the data section from ROM to RAM, if required.
285f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
286f5478dedSAntonio Nino Diaz	 */
287f5478dedSAntonio Nino Diaz	.if \_init_c_runtime
2886c09af9fSZelalem Aweke#if defined(IMAGE_BL31) || (defined(IMAGE_BL2) && \
28942d4d3baSArvind Ram Prakash	((RESET_TO_BL2 && BL2_INV_DCACHE) || ENABLE_RME))
290f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
291f5478dedSAntonio Nino Diaz		 * Invalidate the RW memory used by the BL31 image. This
292f5478dedSAntonio Nino Diaz		 * includes the data and NOBITS sections. This is done to
293f5478dedSAntonio Nino Diaz		 * safeguard against possible corruption of this memory by
294f5478dedSAntonio Nino Diaz		 * dirty cache lines in a system cache as a result of use by
295596d20d9SZelalem Aweke		 * an earlier boot loader stage. If PIE is enabled however,
296596d20d9SZelalem Aweke		 * RO sections including the GOT may be modified during
297596d20d9SZelalem Aweke                 * pie fixup. Therefore, to be on the safe side, invalidate
298596d20d9SZelalem Aweke		 * the entire image region if PIE is enabled.
299f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
300f5478dedSAntonio Nino Diaz		 */
301596d20d9SZelalem Aweke#if ENABLE_PIE
302596d20d9SZelalem Aweke#if SEPARATE_CODE_AND_RODATA
303596d20d9SZelalem Aweke		adrp	x0, __TEXT_START__
304596d20d9SZelalem Aweke		add	x0, x0, :lo12:__TEXT_START__
305596d20d9SZelalem Aweke#else
306596d20d9SZelalem Aweke		adrp	x0, __RO_START__
307596d20d9SZelalem Aweke		add	x0, x0, :lo12:__RO_START__
308596d20d9SZelalem Aweke#endif /* SEPARATE_CODE_AND_RODATA */
309596d20d9SZelalem Aweke#else
310f5478dedSAntonio Nino Diaz		adrp	x0, __RW_START__
311f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__RW_START__
312596d20d9SZelalem Aweke#endif /* ENABLE_PIE */
313f5478dedSAntonio Nino Diaz		adrp	x1, __RW_END__
314f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12:__RW_END__
315f5478dedSAntonio Nino Diaz		sub	x1, x1, x0
316f5478dedSAntonio Nino Diaz		bl	inv_dcache_range
317f8578e64SSamuel Holland#if defined(IMAGE_BL31) && SEPARATE_NOBITS_REGION
318f8578e64SSamuel Holland		adrp	x0, __NOBITS_START__
319f8578e64SSamuel Holland		add	x0, x0, :lo12:__NOBITS_START__
320f8578e64SSamuel Holland		adrp	x1, __NOBITS_END__
321f8578e64SSamuel Holland		add	x1, x1, :lo12:__NOBITS_END__
322f8578e64SSamuel Holland		sub	x1, x1, x0
323f8578e64SSamuel Holland		bl	inv_dcache_range
324f8578e64SSamuel Holland#endif
32596a8ed14SJiafei Pan#if defined(IMAGE_BL2) && SEPARATE_BL2_NOLOAD_REGION
32696a8ed14SJiafei Pan		adrp	x0, __BL2_NOLOAD_START__
32796a8ed14SJiafei Pan		add	x0, x0, :lo12:__BL2_NOLOAD_START__
32896a8ed14SJiafei Pan		adrp	x1, __BL2_NOLOAD_END__
32996a8ed14SJiafei Pan		add	x1, x1, :lo12:__BL2_NOLOAD_END__
33096a8ed14SJiafei Pan		sub	x1, x1, x0
33196a8ed14SJiafei Pan		bl	inv_dcache_range
33296a8ed14SJiafei Pan#endif
333f5478dedSAntonio Nino Diaz#endif
334*98859b99SSammit Joshi#if defined(IMAGE_BL31)
335*98859b99SSammit Joshi		adrp	x0, __PER_CPU_START__
336*98859b99SSammit Joshi		add	x0, x0, :lo12:__PER_CPU_START__
337*98859b99SSammit Joshi		adrp	x1, __PER_CPU_END__
338*98859b99SSammit Joshi		add	x1, x1, :lo12:__PER_CPU_END__
339*98859b99SSammit Joshi		sub	x1, x1, x0
340*98859b99SSammit Joshi#if (PLATFORM_NODE_COUNT > 1)
341*98859b99SSammit Joshi		mov	x9, x1
342*98859b99SSammit Joshi#endif /* (PLATFORM_NODE_COUNT > 1) */
343*98859b99SSammit Joshi		bl	zeromem
344*98859b99SSammit Joshi#if (PLATFORM_NODE_COUNT > 1)
345*98859b99SSammit Joshi		/*
346*98859b99SSammit Joshi		 * Zero-initialize per-cpu sections defined by the platform.
347*98859b99SSammit Joshi		 * Care must be taken to preserve and retain the clobbered
348*98859b99SSammit Joshi		 * registers. A standard around the container for per-cpu nodes
349*98859b99SSammit Joshi		 * is not yet defined.
350*98859b99SSammit Joshi		 */
351*98859b99SSammit Joshi		mov	x10, #1
352*98859b99SSammit Joshi		mov	x11, #PLATFORM_NODE_COUNT
353*98859b99SSammit Joshi
354*98859b99SSammit Joshi		1:
355*98859b99SSammit Joshi			cmp	x10, x11
356*98859b99SSammit Joshi			b.hs	2f
357*98859b99SSammit Joshi
358*98859b99SSammit Joshi			mov	x0, x10
359*98859b99SSammit Joshi			bl	plat_per_cpu_node_base
360*98859b99SSammit Joshi			cmn	x0, #1
361*98859b99SSammit Joshi			b.eq	3f
362*98859b99SSammit Joshi
363*98859b99SSammit Joshi			/* x1 contains size param */
364*98859b99SSammit Joshi			mov	x1, x9
365*98859b99SSammit Joshi			bl	zeromem
366*98859b99SSammit Joshi
367*98859b99SSammit Joshi		3:
368*98859b99SSammit Joshi			add	x10, x10, #1
369*98859b99SSammit Joshi			b	1b
370*98859b99SSammit Joshi
371*98859b99SSammit Joshi		2:
372*98859b99SSammit Joshi#endif /* (PLATFORM_NODE_COUNT > 1) */
373*98859b99SSammit Joshi#endif /* defined(IMAGE_BL31) */
374*98859b99SSammit Joshi
375f5478dedSAntonio Nino Diaz		adrp	x0, __BSS_START__
376f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__BSS_START__
377f5478dedSAntonio Nino Diaz
378f5478dedSAntonio Nino Diaz		adrp	x1, __BSS_END__
379f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12:__BSS_END__
380f5478dedSAntonio Nino Diaz		sub	x1, x1, x0
381f5478dedSAntonio Nino Diaz		bl	zeromem
382f5478dedSAntonio Nino Diaz
383f5478dedSAntonio Nino Diaz#if USE_COHERENT_MEM
384f5478dedSAntonio Nino Diaz		adrp	x0, __COHERENT_RAM_START__
385f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__COHERENT_RAM_START__
386f5478dedSAntonio Nino Diaz		adrp	x1, __COHERENT_RAM_END_UNALIGNED__
387f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12: __COHERENT_RAM_END_UNALIGNED__
388f5478dedSAntonio Nino Diaz		sub	x1, x1, x0
389f5478dedSAntonio Nino Diaz		bl	zeromem
390f5478dedSAntonio Nino Diaz#endif
391f5478dedSAntonio Nino Diaz
39242d4d3baSArvind Ram Prakash#if defined(IMAGE_BL1) ||	\
39386acbbe2SYe Li	(defined(IMAGE_BL2) && RESET_TO_BL2 && BL2_IN_XIP_MEM) || \
39486acbbe2SYe Li	(defined(IMAGE_BL31) && SEPARATE_RWDATA_REGION)
39586acbbe2SYe Li
396f5478dedSAntonio Nino Diaz		adrp	x0, __DATA_RAM_START__
397f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__DATA_RAM_START__
398f5478dedSAntonio Nino Diaz		adrp	x1, __DATA_ROM_START__
399f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12:__DATA_ROM_START__
400f5478dedSAntonio Nino Diaz		adrp	x2, __DATA_RAM_END__
401f5478dedSAntonio Nino Diaz		add	x2, x2, :lo12:__DATA_RAM_END__
402f5478dedSAntonio Nino Diaz		sub	x2, x2, x0
403f5478dedSAntonio Nino Diaz		bl	memcpy16
404f5478dedSAntonio Nino Diaz#endif
405f5478dedSAntonio Nino Diaz	.endif /* _init_c_runtime */
406f5478dedSAntonio Nino Diaz
407f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
408f5478dedSAntonio Nino Diaz	 * Use SP_EL0 for the C runtime stack.
409f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
410f5478dedSAntonio Nino Diaz	 */
411f5478dedSAntonio Nino Diaz	msr	spsel, #0
412f5478dedSAntonio Nino Diaz
413f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
414f5478dedSAntonio Nino Diaz	 * Allocate a stack whose memory will be marked as Normal-IS-WBWA when
415f5478dedSAntonio Nino Diaz	 * the MMU is enabled. There is no risk of reading stale stack memory
416f5478dedSAntonio Nino Diaz	 * after enabling the MMU as only the primary CPU is running at the
417f5478dedSAntonio Nino Diaz	 * moment.
418f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
419f5478dedSAntonio Nino Diaz	 */
420f5478dedSAntonio Nino Diaz	bl	plat_set_my_stack
421f5478dedSAntonio Nino Diaz
422f5478dedSAntonio Nino Diaz#if STACK_PROTECTOR_ENABLED
423f5478dedSAntonio Nino Diaz	.if \_init_c_runtime
424f5478dedSAntonio Nino Diaz	bl	update_stack_protector_canary
425f5478dedSAntonio Nino Diaz	.endif /* _init_c_runtime */
426f5478dedSAntonio Nino Diaz#endif
427f5478dedSAntonio Nino Diaz	.endm
428f5478dedSAntonio Nino Diaz
4293b8456bdSManish V Badarkhe	.macro	apply_at_speculative_wa
4303b8456bdSManish V Badarkhe#if ERRATA_SPECULATIVE_AT
4313b8456bdSManish V Badarkhe	/*
432d87c0e27SManish Pandey	 * This function expects x30 has been saved.
433d87c0e27SManish Pandey	 * Also, save x29 which will be used in the called function.
4343b8456bdSManish V Badarkhe	 */
435d87c0e27SManish Pandey	str	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
4363b8456bdSManish V Badarkhe	bl	save_and_update_ptw_el1_sys_regs
437d87c0e27SManish Pandey	ldr	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
4383b8456bdSManish V Badarkhe#endif
4393b8456bdSManish V Badarkhe	.endm
4403b8456bdSManish V Badarkhe
4413b8456bdSManish V Badarkhe	.macro	restore_ptw_el1_sys_regs
4423b8456bdSManish V Badarkhe#if ERRATA_SPECULATIVE_AT
4433b8456bdSManish V Badarkhe	/* -----------------------------------------------------------
4443b8456bdSManish V Badarkhe	 * In case of ERRATA_SPECULATIVE_AT, must follow below order
4453b8456bdSManish V Badarkhe	 * to ensure that page table walk is not enabled until
4463b8456bdSManish V Badarkhe	 * restoration of all EL1 system registers. TCR_EL1 register
4473b8456bdSManish V Badarkhe	 * should be updated at the end which restores previous page
4483b8456bdSManish V Badarkhe	 * table walk setting of stage1 i.e.(TCR_EL1.EPDx) bits. ISB
4493b8456bdSManish V Badarkhe	 * ensures that CPU does below steps in order.
4503b8456bdSManish V Badarkhe	 *
4513b8456bdSManish V Badarkhe	 * 1. Ensure all other system registers are written before
4523b8456bdSManish V Badarkhe	 *    updating SCTLR_EL1 using ISB.
4533b8456bdSManish V Badarkhe	 * 2. Restore SCTLR_EL1 register.
4543b8456bdSManish V Badarkhe	 * 3. Ensure SCTLR_EL1 written successfully using ISB.
4553b8456bdSManish V Badarkhe	 * 4. Restore TCR_EL1 register.
4563b8456bdSManish V Badarkhe	 * -----------------------------------------------------------
4573b8456bdSManish V Badarkhe	 */
4583b8456bdSManish V Badarkhe	isb
45959b7c0a0SJayanth Dodderi Chidanand	ldp	x28, x29, [sp, #CTX_ERRATA_SPEC_AT_OFFSET + CTX_ERRATA_SPEC_AT_SCTLR_EL1]
4603b8456bdSManish V Badarkhe	msr	sctlr_el1, x28
4613b8456bdSManish V Badarkhe	isb
4623b8456bdSManish V Badarkhe	msr	tcr_el1, x29
4633b8456bdSManish V Badarkhe#endif
4643b8456bdSManish V Badarkhe	.endm
4653b8456bdSManish V Badarkhe
466461c0a5dSElizabeth Ho/* -----------------------------------------------------------------
467461c0a5dSElizabeth Ho * The below macro reads SCR_EL3 from the context structure to
468461c0a5dSElizabeth Ho * determine the security state of the context upon ERET.
469461c0a5dSElizabeth Ho * ------------------------------------------------------------------
470461c0a5dSElizabeth Ho */
471461c0a5dSElizabeth Ho	.macro get_security_state _ret:req, _scr_reg:req
472461c0a5dSElizabeth Ho		ubfx 	\_ret, \_scr_reg, #SCR_NSE_SHIFT, #1
473461c0a5dSElizabeth Ho		cmp 	\_ret, #1
474461c0a5dSElizabeth Ho		beq 	realm_state
475461c0a5dSElizabeth Ho		bfi	\_ret, \_scr_reg, #0, #1
476461c0a5dSElizabeth Ho		b 	end
477461c0a5dSElizabeth Ho	realm_state:
478461c0a5dSElizabeth Ho		mov 	\_ret, #2
479461c0a5dSElizabeth Ho	end:
480461c0a5dSElizabeth Ho	.endm
481461c0a5dSElizabeth Ho
48240e5f7a5SJayanth Dodderi Chidanand/*-----------------------------------------------------------------------------
48340e5f7a5SJayanth Dodderi Chidanand * Helper macro to configure EL3 registers we care about, while executing
48440e5f7a5SJayanth Dodderi Chidanand * at EL3/Root world. Root world has its own execution environment and
48540e5f7a5SJayanth Dodderi Chidanand * needs to have its settings configured to be independent of other worlds.
48640e5f7a5SJayanth Dodderi Chidanand * -----------------------------------------------------------------------------
48740e5f7a5SJayanth Dodderi Chidanand */
48840e5f7a5SJayanth Dodderi Chidanand	.macro setup_el3_execution_context
48940e5f7a5SJayanth Dodderi Chidanand
49040e5f7a5SJayanth Dodderi Chidanand	/* ---------------------------------------------------------------------
49140e5f7a5SJayanth Dodderi Chidanand	 * The following registers need to be part of separate root context
49240e5f7a5SJayanth Dodderi Chidanand	 * as their values are of importance during EL3 execution.
49340e5f7a5SJayanth Dodderi Chidanand	 * Hence these registers are overwritten to their intital values,
49440e5f7a5SJayanth Dodderi Chidanand	 * irrespective of whichever world they return from to ensure EL3 has a
49540e5f7a5SJayanth Dodderi Chidanand	 * consistent execution context throughout the lifetime of TF-A.
49640e5f7a5SJayanth Dodderi Chidanand	 *
49740e5f7a5SJayanth Dodderi Chidanand	 * DAIF.A: Enable External Aborts and SError Interrupts at EL3.
49840e5f7a5SJayanth Dodderi Chidanand	 *
49940e5f7a5SJayanth Dodderi Chidanand	 * MDCR_EL3.SDD: Set to one to disable AArch64 Secure self-hosted debug.
50040e5f7a5SJayanth Dodderi Chidanand	 *  Debug exceptions, other than Breakpoint Instruction exceptions, are
50140e5f7a5SJayanth Dodderi Chidanand	 *  disabled from all ELs in Secure state.
50240e5f7a5SJayanth Dodderi Chidanand	 *
50340e5f7a5SJayanth Dodderi Chidanand	 * SCR_EL3.EA: Set to one to enable SError interrupts at EL3.
50440e5f7a5SJayanth Dodderi Chidanand	 *
50540e5f7a5SJayanth Dodderi Chidanand	 * SCR_EL3.SIF: Set to one to disable instruction fetches from
50640e5f7a5SJayanth Dodderi Chidanand	 *  Non-secure memory.
50740e5f7a5SJayanth Dodderi Chidanand	 *
50840e5f7a5SJayanth Dodderi Chidanand	 * PMCR_EL0.DP: Set to one so that the cycle counter,
50940e5f7a5SJayanth Dodderi Chidanand	 *  PMCCNTR_EL0 does not count when event counting is prohibited.
51040e5f7a5SJayanth Dodderi Chidanand	 *  Necessary on PMUv3 <= p7 where MDCR_EL3.{SCCD,MCCD} are not
51140e5f7a5SJayanth Dodderi Chidanand	 *  available.
51240e5f7a5SJayanth Dodderi Chidanand	 *
5130a580b51SBoyan Karatotev	 * CPTR_EL3.EZ: Set to one so that accesses to ZCR_EL3 do not trap
5140a580b51SBoyan Karatotev	 * CPTR_EL3.ESM: Set to one so that SME related registers don't trap
5150a580b51SBoyan Karatotev	 *
51640e5f7a5SJayanth Dodderi Chidanand	 * PSTATE.DIT: Set to one to enable the Data Independent Timing (DIT)
51740e5f7a5SJayanth Dodderi Chidanand	 *  functionality, if implemented in EL3.
51840e5f7a5SJayanth Dodderi Chidanand	 * ---------------------------------------------------------------------
51940e5f7a5SJayanth Dodderi Chidanand	 */
52040e5f7a5SJayanth Dodderi Chidanand		msr	daifclr, #DAIF_ABT_BIT
52140e5f7a5SJayanth Dodderi Chidanand
52240e5f7a5SJayanth Dodderi Chidanand		mrs 	x15, mdcr_el3
52340e5f7a5SJayanth Dodderi Chidanand		orr	x15, x15, #MDCR_SDD_BIT
52440e5f7a5SJayanth Dodderi Chidanand		msr	mdcr_el3, x15
52540e5f7a5SJayanth Dodderi Chidanand
52640e5f7a5SJayanth Dodderi Chidanand		mrs	x15, scr_el3
52740e5f7a5SJayanth Dodderi Chidanand		orr	x15, x15, #SCR_EA_BIT
52840e5f7a5SJayanth Dodderi Chidanand		orr	x15, x15, #SCR_SIF_BIT
52945218c64SBoyan Karatotev		bic	x15, x15, #SCR_TRNDR_BIT
53040e5f7a5SJayanth Dodderi Chidanand		msr	scr_el3, x15
53140e5f7a5SJayanth Dodderi Chidanand
53240e5f7a5SJayanth Dodderi Chidanand		mrs 	x15, pmcr_el0
53340e5f7a5SJayanth Dodderi Chidanand		orr	x15, x15, #PMCR_EL0_DP_BIT
53440e5f7a5SJayanth Dodderi Chidanand		msr	pmcr_el0, x15
53540e5f7a5SJayanth Dodderi Chidanand
5360a580b51SBoyan Karatotev		mrs	x15, cptr_el3
5370a580b51SBoyan Karatotev		orr	x15, x15, #CPTR_EZ_BIT
5380a580b51SBoyan Karatotev		orr	x15, x15, #ESM_BIT
5390a580b51SBoyan Karatotev		msr	cptr_el3, x15
5400a580b51SBoyan Karatotev
54140e5f7a5SJayanth Dodderi Chidanand#if ENABLE_FEAT_DIT
54240e5f7a5SJayanth Dodderi Chidanand#if ENABLE_FEAT_DIT > 1
54340e5f7a5SJayanth Dodderi Chidanand		mrs	x15, id_aa64pfr0_el1
54440e5f7a5SJayanth Dodderi Chidanand		ubfx	x15, x15, #ID_AA64PFR0_DIT_SHIFT, #ID_AA64PFR0_DIT_LENGTH
54540e5f7a5SJayanth Dodderi Chidanand		cbz	x15, 1f
54640e5f7a5SJayanth Dodderi Chidanand#endif
54740e5f7a5SJayanth Dodderi Chidanand		mov	x15, #DIT_BIT
54840e5f7a5SJayanth Dodderi Chidanand		msr	DIT, x15
54940e5f7a5SJayanth Dodderi Chidanand	1:
55040e5f7a5SJayanth Dodderi Chidanand#endif
55140e5f7a5SJayanth Dodderi Chidanand
55240e5f7a5SJayanth Dodderi Chidanand		isb
55340e5f7a5SJayanth Dodderi Chidanand	.endm
55440e5f7a5SJayanth Dodderi Chidanand
555f5478dedSAntonio Nino Diaz#endif /* EL3_COMMON_MACROS_S */
556