xref: /rk3399_ARM-atf/include/arch/aarch64/el3_common_macros.S (revision 40ff90747098ed9d2a09894d1a886c10ca76cee6)
1f5478dedSAntonio Nino Diaz/*
212f6c064SAlexei Fedorov * Copyright (c) 2015-2021, 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>
123b8456bdSManish V Badarkhe#include <context.h>
131a04b2e5SVarun Wadekar#include <lib/xlat_tables/xlat_tables_defs.h>
14f5478dedSAntonio Nino Diaz
15f5478dedSAntonio Nino Diaz	/*
16f5478dedSAntonio Nino Diaz	 * Helper macro to initialise EL3 registers we care about.
17f5478dedSAntonio Nino Diaz	 */
18f5478dedSAntonio Nino Diaz	.macro el3_arch_init_common
19f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
20f5478dedSAntonio Nino Diaz	 * SCTLR_EL3 has already been initialised - read current value before
21f5478dedSAntonio Nino Diaz	 * modifying.
22f5478dedSAntonio Nino Diaz	 *
23f5478dedSAntonio Nino Diaz	 * SCTLR_EL3.I: Enable the instruction cache.
24f5478dedSAntonio Nino Diaz	 *
25f5478dedSAntonio Nino Diaz	 * SCTLR_EL3.SA: Enable Stack Alignment check. A SP alignment fault
26f5478dedSAntonio Nino Diaz	 *  exception is generated if a load or store instruction executed at
27f5478dedSAntonio Nino Diaz	 *  EL3 uses the SP as the base address and the SP is not aligned to a
28f5478dedSAntonio Nino Diaz	 *  16-byte boundary.
29f5478dedSAntonio Nino Diaz	 *
30f5478dedSAntonio Nino Diaz	 * SCTLR_EL3.A: Enable Alignment fault checking. All instructions that
31f5478dedSAntonio Nino Diaz	 *  load or store one or more registers have an alignment check that the
32f5478dedSAntonio Nino Diaz	 *  address being accessed is aligned to the size of the data element(s)
33f5478dedSAntonio Nino Diaz	 *  being accessed.
34f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
35f5478dedSAntonio Nino Diaz	 */
36f5478dedSAntonio Nino Diaz	mov	x1, #(SCTLR_I_BIT | SCTLR_A_BIT | SCTLR_SA_BIT)
37f5478dedSAntonio Nino Diaz	mrs	x0, sctlr_el3
38f5478dedSAntonio Nino Diaz	orr	x0, x0, x1
39f5478dedSAntonio Nino Diaz	msr	sctlr_el3, x0
40f5478dedSAntonio Nino Diaz	isb
41f5478dedSAntonio Nino Diaz
42f5478dedSAntonio Nino Diaz#ifdef IMAGE_BL31
43f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
44f5478dedSAntonio Nino Diaz	 * Initialise the per-cpu cache pointer to the CPU.
45f5478dedSAntonio Nino Diaz	 * This is done early to enable crash reporting to have access to crash
46f5478dedSAntonio Nino Diaz	 * stack. Since crash reporting depends on cpu_data to report the
47f5478dedSAntonio Nino Diaz	 * unhandled exception, not doing so can lead to recursive exceptions
48f5478dedSAntonio Nino Diaz	 * due to a NULL TPIDR_EL3.
49f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
50f5478dedSAntonio Nino Diaz	 */
51f5478dedSAntonio Nino Diaz	bl	init_cpu_data_ptr
52f5478dedSAntonio Nino Diaz#endif /* IMAGE_BL31 */
53f5478dedSAntonio Nino Diaz
54f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
55f5478dedSAntonio Nino Diaz	 * Initialise SCR_EL3, setting all fields rather than relying on hw.
56f5478dedSAntonio Nino Diaz	 * All fields are architecturally UNKNOWN on reset. The following fields
57f5478dedSAntonio Nino Diaz	 * do not change during the TF lifetime. The remaining fields are set to
58f5478dedSAntonio Nino Diaz	 * zero here but are updated ahead of transitioning to a lower EL in the
59f5478dedSAntonio Nino Diaz	 * function cm_init_context_common().
60f5478dedSAntonio Nino Diaz	 *
61f5478dedSAntonio Nino Diaz	 * SCR_EL3.TWE: Set to zero so that execution of WFE instructions at
62f5478dedSAntonio Nino Diaz	 *  EL2, EL1 and EL0 are not trapped to EL3.
63f5478dedSAntonio Nino Diaz	 *
64f5478dedSAntonio Nino Diaz	 * SCR_EL3.TWI: Set to zero so that execution of WFI instructions at
65f5478dedSAntonio Nino Diaz	 *  EL2, EL1 and EL0 are not trapped to EL3.
66f5478dedSAntonio Nino Diaz	 *
67f5478dedSAntonio Nino Diaz	 * SCR_EL3.SIF: Set to one to disable instruction fetches from
68f5478dedSAntonio Nino Diaz	 *  Non-secure memory.
69f5478dedSAntonio Nino Diaz	 *
70f5478dedSAntonio Nino Diaz	 * SCR_EL3.SMD: Set to zero to enable SMC calls at EL1 and above, from
71f5478dedSAntonio Nino Diaz	 *  both Security states and both Execution states.
72f5478dedSAntonio Nino Diaz	 *
73f5478dedSAntonio Nino Diaz	 * SCR_EL3.EA: Set to one to route External Aborts and SError Interrupts
74f5478dedSAntonio Nino Diaz	 *  to EL3 when executing at any EL.
75f5478dedSAntonio Nino Diaz	 *
76f5478dedSAntonio Nino Diaz	 * SCR_EL3.{API,APK}: For Armv8.3 pointer authentication feature,
77f5478dedSAntonio Nino Diaz	 * disable traps to EL3 when accessing key registers or using pointer
78f5478dedSAntonio Nino Diaz	 * authentication instructions from lower ELs.
79f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
80f5478dedSAntonio Nino Diaz	 */
815283962eSAntonio Nino Diaz	mov_imm	x0, ((SCR_RESET_VAL | SCR_EA_BIT | SCR_SIF_BIT) \
82f5478dedSAntonio Nino Diaz			& ~(SCR_TWE_BIT | SCR_TWI_BIT | SCR_SMD_BIT))
835283962eSAntonio Nino Diaz#if CTX_INCLUDE_PAUTH_REGS
845283962eSAntonio Nino Diaz	/*
855283962eSAntonio Nino Diaz	 * If the pointer authentication registers are saved during world
865283962eSAntonio Nino Diaz	 * switches, enable pointer authentication everywhere, as it is safe to
875283962eSAntonio Nino Diaz	 * do so.
885283962eSAntonio Nino Diaz	 */
895283962eSAntonio Nino Diaz	orr	x0, x0, #(SCR_API_BIT | SCR_APK_BIT)
905283962eSAntonio Nino Diaz#endif
91f5478dedSAntonio Nino Diaz	msr	scr_el3, x0
92f5478dedSAntonio Nino Diaz
93f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
94f5478dedSAntonio Nino Diaz	 * Initialise MDCR_EL3, setting all fields rather than relying on hw.
95f5478dedSAntonio Nino Diaz	 * Some fields are architecturally UNKNOWN on reset.
96f5478dedSAntonio Nino Diaz	 *
97f5478dedSAntonio Nino Diaz	 * MDCR_EL3.SDD: Set to one to disable AArch64 Secure self-hosted debug.
98f5478dedSAntonio Nino Diaz	 *  Debug exceptions, other than Breakpoint Instruction exceptions, are
99f5478dedSAntonio Nino Diaz	 *  disabled from all ELs in Secure state.
100f5478dedSAntonio Nino Diaz	 *
101f5478dedSAntonio Nino Diaz	 * MDCR_EL3.SPD32: Set to 0b10 to disable AArch32 Secure self-hosted
102f5478dedSAntonio Nino Diaz	 *  privileged debug from S-EL1.
103f5478dedSAntonio Nino Diaz	 *
104f5478dedSAntonio Nino Diaz	 * MDCR_EL3.TDOSA: Set to zero so that EL2 and EL2 System register
105f5478dedSAntonio Nino Diaz	 *  access to the powerdown debug registers do not trap to EL3.
106f5478dedSAntonio Nino Diaz	 *
107f5478dedSAntonio Nino Diaz	 * MDCR_EL3.TDA: Set to zero to allow EL0, EL1 and EL2 access to the
108f5478dedSAntonio Nino Diaz	 *  debug registers, other than those registers that are controlled by
109f5478dedSAntonio Nino Diaz	 *  MDCR_EL3.TDOSA.
110f5478dedSAntonio Nino Diaz	 *
111f5478dedSAntonio Nino Diaz	 * MDCR_EL3.TPM: Set to zero so that EL0, EL1, and EL2 System register
112f5478dedSAntonio Nino Diaz	 *  accesses to all Performance Monitors registers do not trap to EL3.
113ed4fc6f0SAntonio Nino Diaz	 *
114ed4fc6f0SAntonio Nino Diaz	 * MDCR_EL3.SCCD: Set to one so that cycle counting by PMCCNTR_EL0 is
115ed4fc6f0SAntonio Nino Diaz	 *  prohibited in Secure state. This bit is RES0 in versions of the
11612f6c064SAlexei Fedorov	 *  architecture with FEAT_PMUv3p5 not implemented, setting it to 1
11712f6c064SAlexei Fedorov	 *  doesn't have any effect on them.
11812f6c064SAlexei Fedorov	 *
11912f6c064SAlexei Fedorov	 * MDCR_EL3.MCCD: Set to one so that cycle counting by PMCCNTR_EL0 is
12012f6c064SAlexei Fedorov	 *  prohibited in EL3. This bit is RES0 in versions of the
12112f6c064SAlexei Fedorov	 *  architecture with FEAT_PMUv3p7 not implemented, setting it to 1
12212f6c064SAlexei Fedorov	 *  doesn't have any effect on them.
1232a7adf25SPetre-Ionut Tudor	 *
1242a7adf25SPetre-Ionut Tudor	 * MDCR_EL3.SPME: Set to zero so that event counting by the programmable
1252a7adf25SPetre-Ionut Tudor	 *  counters PMEVCNTR<n>_EL0 is prohibited in Secure state. If ARMv8.2
1262a7adf25SPetre-Ionut Tudor	 *  Debug is not implemented this bit does not have any effect on the
1272a7adf25SPetre-Ionut Tudor	 *  counters unless there is support for the implementation defined
1282a7adf25SPetre-Ionut Tudor	 *  authentication interface ExternalSecureNoninvasiveDebugEnabled().
129*40ff9074SManish V Badarkhe	 *
130*40ff9074SManish V Badarkhe	 * MDCR_EL3.NSTB, MDCR_EL3.NSTBE: Set to zero so that Trace Buffer
131*40ff9074SManish V Badarkhe	 *  owning security state is Secure state. If FEAT_TRBE is implemented,
132*40ff9074SManish V Badarkhe	 *  accesses to Trace Buffer control registers at EL2 and EL1 in any
133*40ff9074SManish V Badarkhe	 *  security state generates trap exceptions to EL3.
134*40ff9074SManish V Badarkhe	 *  If FEAT_TRBE is not implemented, these bits are RES0.
135f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
136f5478dedSAntonio Nino Diaz	 */
137ed4fc6f0SAntonio Nino Diaz	mov_imm	x0, ((MDCR_EL3_RESET_VAL | MDCR_SDD_BIT | \
13812f6c064SAlexei Fedorov		      MDCR_SPD32(MDCR_SPD32_DISABLE) | MDCR_SCCD_BIT | \
13912f6c064SAlexei Fedorov		      MDCR_MCCD_BIT) & ~(MDCR_SPME_BIT | MDCR_TDOSA_BIT | \
140*40ff9074SManish V Badarkhe		      MDCR_TDA_BIT | MDCR_TPM_BIT | MDCR_NSTB(MDCR_NSTB_EL1) | \
141*40ff9074SManish V Badarkhe		      MDCR_NSTBE))
142ed4fc6f0SAntonio Nino Diaz
143f5478dedSAntonio Nino Diaz	msr	mdcr_el3, x0
144f5478dedSAntonio Nino Diaz
145f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
146e290a8fcSAlexei Fedorov	 * Initialise PMCR_EL0 setting all fields rather than relying
147e290a8fcSAlexei Fedorov	 * on hw. Some fields are architecturally UNKNOWN on reset.
148e290a8fcSAlexei Fedorov	 *
149e290a8fcSAlexei Fedorov	 * PMCR_EL0.LP: Set to one so that event counter overflow, that
150e290a8fcSAlexei Fedorov	 *  is recorded in PMOVSCLR_EL0[0-30], occurs on the increment
151e290a8fcSAlexei Fedorov	 *  that changes PMEVCNTR<n>_EL0[63] from 1 to 0, when ARMv8.5-PMU
152e290a8fcSAlexei Fedorov	 *  is implemented. This bit is RES0 in versions of the architecture
153e290a8fcSAlexei Fedorov	 *  earlier than ARMv8.5, setting it to 1 doesn't have any effect
154e290a8fcSAlexei Fedorov	 *  on them.
155e290a8fcSAlexei Fedorov	 *
156e290a8fcSAlexei Fedorov	 * PMCR_EL0.LC: Set to one so that cycle counter overflow, that
157e290a8fcSAlexei Fedorov	 *  is recorded in PMOVSCLR_EL0[31], occurs on the increment
158e290a8fcSAlexei Fedorov	 *  that changes PMCCNTR_EL0[63] from 1 to 0.
159e290a8fcSAlexei Fedorov	 *
160e290a8fcSAlexei Fedorov	 * PMCR_EL0.DP: Set to one so that the cycle counter,
161e290a8fcSAlexei Fedorov	 *  PMCCNTR_EL0 does not count when event counting is prohibited.
162e290a8fcSAlexei Fedorov	 *
163e290a8fcSAlexei Fedorov	 * PMCR_EL0.X: Set to zero to disable export of events.
164e290a8fcSAlexei Fedorov	 *
165e290a8fcSAlexei Fedorov	 * PMCR_EL0.D: Set to zero so that, when enabled, PMCCNTR_EL0
166e290a8fcSAlexei Fedorov	 *  counts on every clock cycle.
167e290a8fcSAlexei Fedorov	 * ---------------------------------------------------------------------
168e290a8fcSAlexei Fedorov	 */
169e290a8fcSAlexei Fedorov	mov_imm	x0, ((PMCR_EL0_RESET_VAL | PMCR_EL0_LP_BIT | \
170e290a8fcSAlexei Fedorov		      PMCR_EL0_LC_BIT | PMCR_EL0_DP_BIT) & \
171e290a8fcSAlexei Fedorov		    ~(PMCR_EL0_X_BIT | PMCR_EL0_D_BIT))
172e290a8fcSAlexei Fedorov
173e290a8fcSAlexei Fedorov	msr	pmcr_el0, x0
174e290a8fcSAlexei Fedorov
175e290a8fcSAlexei Fedorov	/* ---------------------------------------------------------------------
176f5478dedSAntonio Nino Diaz	 * Enable External Aborts and SError Interrupts now that the exception
177f5478dedSAntonio Nino Diaz	 * vectors have been setup.
178f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
179f5478dedSAntonio Nino Diaz	 */
180f5478dedSAntonio Nino Diaz	msr	daifclr, #DAIF_ABT_BIT
181f5478dedSAntonio Nino Diaz
182f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
183f5478dedSAntonio Nino Diaz	 * Initialise CPTR_EL3, setting all fields rather than relying on hw.
184f5478dedSAntonio Nino Diaz	 * All fields are architecturally UNKNOWN on reset.
185f5478dedSAntonio Nino Diaz	 *
186f5478dedSAntonio Nino Diaz	 * CPTR_EL3.TCPAC: Set to zero so that any accesses to CPACR_EL1,
187f5478dedSAntonio Nino Diaz	 *  CPTR_EL2, CPACR, or HCPTR do not trap to EL3.
188f5478dedSAntonio Nino Diaz	 *
189f5478dedSAntonio Nino Diaz	 * CPTR_EL3.TTA: Set to zero so that System register accesses to the
190f5478dedSAntonio Nino Diaz	 *  trace registers do not trap to EL3.
191f5478dedSAntonio Nino Diaz	 *
192f5478dedSAntonio Nino Diaz	 * CPTR_EL3.TFP: Set to zero so that accesses to the V- or Z- registers
193f5478dedSAntonio Nino Diaz	 *  by Advanced SIMD, floating-point or SVE instructions (if implemented)
194f5478dedSAntonio Nino Diaz	 *  do not trap to EL3.
1950c5e7d1cSMax Shvetsov	 *
1960c5e7d1cSMax Shvetsov	 * CPTR_EL3.TAM: Set to one so that Activity Monitor access is
1970c5e7d1cSMax Shvetsov	 *  trapped to EL3 by default.
1980c5e7d1cSMax Shvetsov	 *
1990c5e7d1cSMax Shvetsov	 * CPTR_EL3.EZ: Set to zero so that all SVE functionality is trapped
2000c5e7d1cSMax Shvetsov	 *  to EL3 by default.
201f5478dedSAntonio Nino Diaz	 */
2020c5e7d1cSMax Shvetsov
203f5478dedSAntonio Nino Diaz	mov_imm x0, (CPTR_EL3_RESET_VAL & ~(TCPAC_BIT | TTA_BIT | TFP_BIT))
204f5478dedSAntonio Nino Diaz	msr	cptr_el3, x0
205f5478dedSAntonio Nino Diaz
206f5478dedSAntonio Nino Diaz	/*
207f5478dedSAntonio Nino Diaz	 * If Data Independent Timing (DIT) functionality is implemented,
208f5478dedSAntonio Nino Diaz	 * always enable DIT in EL3
209f5478dedSAntonio Nino Diaz	 */
210f5478dedSAntonio Nino Diaz	mrs	x0, id_aa64pfr0_el1
211f5478dedSAntonio Nino Diaz	ubfx	x0, x0, #ID_AA64PFR0_DIT_SHIFT, #ID_AA64PFR0_DIT_LENGTH
212f5478dedSAntonio Nino Diaz	cmp	x0, #ID_AA64PFR0_DIT_SUPPORTED
213f5478dedSAntonio Nino Diaz	bne	1f
214f5478dedSAntonio Nino Diaz	mov	x0, #DIT_BIT
215f5478dedSAntonio Nino Diaz	msr	DIT, x0
216f5478dedSAntonio Nino Diaz1:
217f5478dedSAntonio Nino Diaz	.endm
218f5478dedSAntonio Nino Diaz
219f5478dedSAntonio Nino Diaz/* -----------------------------------------------------------------------------
220f5478dedSAntonio Nino Diaz * This is the super set of actions that need to be performed during a cold boot
221f5478dedSAntonio Nino Diaz * or a warm boot in EL3. This code is shared by BL1 and BL31.
222f5478dedSAntonio Nino Diaz *
223f5478dedSAntonio Nino Diaz * This macro will always perform reset handling, architectural initialisations
224f5478dedSAntonio Nino Diaz * and stack setup. The rest of the actions are optional because they might not
225f5478dedSAntonio Nino Diaz * be needed, depending on the context in which this macro is called. This is
226f5478dedSAntonio Nino Diaz * why this macro is parameterised ; each parameter allows to enable/disable
227f5478dedSAntonio Nino Diaz * some actions.
228f5478dedSAntonio Nino Diaz *
229f5478dedSAntonio Nino Diaz *  _init_sctlr:
230f5478dedSAntonio Nino Diaz *	Whether the macro needs to initialise SCTLR_EL3, including configuring
231f5478dedSAntonio Nino Diaz *      the endianness of data accesses.
232f5478dedSAntonio Nino Diaz *
233f5478dedSAntonio Nino Diaz *  _warm_boot_mailbox:
234f5478dedSAntonio Nino Diaz *	Whether the macro needs to detect the type of boot (cold/warm). The
235f5478dedSAntonio Nino Diaz *	detection is based on the platform entrypoint address : if it is zero
236f5478dedSAntonio Nino Diaz *	then it is a cold boot, otherwise it is a warm boot. In the latter case,
237f5478dedSAntonio Nino Diaz *	this macro jumps on the platform entrypoint address.
238f5478dedSAntonio Nino Diaz *
239f5478dedSAntonio Nino Diaz *  _secondary_cold_boot:
240f5478dedSAntonio Nino Diaz *	Whether the macro needs to identify the CPU that is calling it: primary
241f5478dedSAntonio Nino Diaz *	CPU or secondary CPU. The primary CPU will be allowed to carry on with
242f5478dedSAntonio Nino Diaz *	the platform initialisations, while the secondaries will be put in a
243f5478dedSAntonio Nino Diaz *	platform-specific state in the meantime.
244f5478dedSAntonio Nino Diaz *
245f5478dedSAntonio Nino Diaz *	If the caller knows this macro will only be called by the primary CPU
246f5478dedSAntonio Nino Diaz *	then this parameter can be defined to 0 to skip this step.
247f5478dedSAntonio Nino Diaz *
248f5478dedSAntonio Nino Diaz * _init_memory:
249f5478dedSAntonio Nino Diaz *	Whether the macro needs to initialise the memory.
250f5478dedSAntonio Nino Diaz *
251f5478dedSAntonio Nino Diaz * _init_c_runtime:
252f5478dedSAntonio Nino Diaz *	Whether the macro needs to initialise the C runtime environment.
253f5478dedSAntonio Nino Diaz *
254f5478dedSAntonio Nino Diaz * _exception_vectors:
255f5478dedSAntonio Nino Diaz *	Address of the exception vectors to program in the VBAR_EL3 register.
256da90359bSManish Pandey *
257da90359bSManish Pandey * _pie_fixup_size:
258da90359bSManish Pandey *	Size of memory region to fixup Global Descriptor Table (GDT).
259da90359bSManish Pandey *
260da90359bSManish Pandey *	A non-zero value is expected when firmware needs GDT to be fixed-up.
261da90359bSManish Pandey *
262f5478dedSAntonio Nino Diaz * -----------------------------------------------------------------------------
263f5478dedSAntonio Nino Diaz */
264f5478dedSAntonio Nino Diaz	.macro el3_entrypoint_common					\
265f5478dedSAntonio Nino Diaz		_init_sctlr, _warm_boot_mailbox, _secondary_cold_boot,	\
266da90359bSManish Pandey		_init_memory, _init_c_runtime, _exception_vectors,	\
267da90359bSManish Pandey		_pie_fixup_size
268f5478dedSAntonio Nino Diaz
269f5478dedSAntonio Nino Diaz	.if \_init_sctlr
270f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
271f5478dedSAntonio Nino Diaz		 * This is the initialisation of SCTLR_EL3 and so must ensure
272f5478dedSAntonio Nino Diaz		 * that all fields are explicitly set rather than relying on hw.
273f5478dedSAntonio Nino Diaz		 * Some fields reset to an IMPLEMENTATION DEFINED value and
274f5478dedSAntonio Nino Diaz		 * others are architecturally UNKNOWN on reset.
275f5478dedSAntonio Nino Diaz		 *
276f5478dedSAntonio Nino Diaz		 * SCTLR.EE: Set the CPU endianness before doing anything that
277f5478dedSAntonio Nino Diaz		 *  might involve memory reads or writes. Set to zero to select
278f5478dedSAntonio Nino Diaz		 *  Little Endian.
279f5478dedSAntonio Nino Diaz		 *
280f5478dedSAntonio Nino Diaz		 * SCTLR_EL3.WXN: For the EL3 translation regime, this field can
281f5478dedSAntonio Nino Diaz		 *  force all memory regions that are writeable to be treated as
282f5478dedSAntonio Nino Diaz		 *  XN (Execute-never). Set to zero so that this control has no
283f5478dedSAntonio Nino Diaz		 *  effect on memory access permissions.
284f5478dedSAntonio Nino Diaz		 *
285f5478dedSAntonio Nino Diaz		 * SCTLR_EL3.SA: Set to zero to disable Stack Alignment check.
286f5478dedSAntonio Nino Diaz		 *
287f5478dedSAntonio Nino Diaz		 * SCTLR_EL3.A: Set to zero to disable Alignment fault checking.
288f5478dedSAntonio Nino Diaz		 *
289f5478dedSAntonio Nino Diaz		 * SCTLR.DSSBS: Set to zero to disable speculation store bypass
290f5478dedSAntonio Nino Diaz		 *  safe behaviour upon exception entry to EL3.
291f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
292f5478dedSAntonio Nino Diaz		 */
293f5478dedSAntonio Nino Diaz		mov_imm	x0, (SCTLR_RESET_VAL & ~(SCTLR_EE_BIT | SCTLR_WXN_BIT \
294f5478dedSAntonio Nino Diaz				| SCTLR_SA_BIT | SCTLR_A_BIT | SCTLR_DSSBS_BIT))
295f5478dedSAntonio Nino Diaz		msr	sctlr_el3, x0
296f5478dedSAntonio Nino Diaz		isb
297f5478dedSAntonio Nino Diaz	.endif /* _init_sctlr */
298f5478dedSAntonio Nino Diaz
2990063dd17SJavier Almansa Sobrino#if DISABLE_MTPMU
3000063dd17SJavier Almansa Sobrino		bl	mtpmu_disable
3010063dd17SJavier Almansa Sobrino#endif
3020063dd17SJavier Almansa Sobrino
303f5478dedSAntonio Nino Diaz	.if \_warm_boot_mailbox
304f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
305f5478dedSAntonio Nino Diaz		 * This code will be executed for both warm and cold resets.
306f5478dedSAntonio Nino Diaz		 * Now is the time to distinguish between the two.
307f5478dedSAntonio Nino Diaz		 * Query the platform entrypoint address and if it is not zero
308f5478dedSAntonio Nino Diaz		 * then it means it is a warm boot so jump to this address.
309f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
310f5478dedSAntonio Nino Diaz		 */
311f5478dedSAntonio Nino Diaz		bl	plat_get_my_entrypoint
312f5478dedSAntonio Nino Diaz		cbz	x0, do_cold_boot
313f5478dedSAntonio Nino Diaz		br	x0
314f5478dedSAntonio Nino Diaz
315f5478dedSAntonio Nino Diaz	do_cold_boot:
316f5478dedSAntonio Nino Diaz	.endif /* _warm_boot_mailbox */
317f5478dedSAntonio Nino Diaz
318da90359bSManish Pandey	.if \_pie_fixup_size
319da90359bSManish Pandey#if ENABLE_PIE
320da90359bSManish Pandey		/*
321da90359bSManish Pandey		 * ------------------------------------------------------------
322da90359bSManish Pandey		 * If PIE is enabled fixup the Global descriptor Table only
323da90359bSManish Pandey		 * once during primary core cold boot path.
324da90359bSManish Pandey		 *
325da90359bSManish Pandey		 * Compile time base address, required for fixup, is calculated
326da90359bSManish Pandey		 * using "pie_fixup" label present within first page.
327da90359bSManish Pandey		 * ------------------------------------------------------------
328da90359bSManish Pandey		 */
329da90359bSManish Pandey	pie_fixup:
330da90359bSManish Pandey		ldr	x0, =pie_fixup
331d7b5f408SJimmy Brisson		and	x0, x0, #~(PAGE_SIZE_MASK)
332da90359bSManish Pandey		mov_imm	x1, \_pie_fixup_size
333da90359bSManish Pandey		add	x1, x1, x0
334da90359bSManish Pandey		bl	fixup_gdt_reloc
335da90359bSManish Pandey#endif /* ENABLE_PIE */
336da90359bSManish Pandey	.endif /* _pie_fixup_size */
337da90359bSManish Pandey
338f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
339f5478dedSAntonio Nino Diaz	 * Set the exception vectors.
340f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
341f5478dedSAntonio Nino Diaz	 */
342f5478dedSAntonio Nino Diaz	adr	x0, \_exception_vectors
343f5478dedSAntonio Nino Diaz	msr	vbar_el3, x0
344f5478dedSAntonio Nino Diaz	isb
345f5478dedSAntonio Nino Diaz
346f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
347f5478dedSAntonio Nino Diaz	 * It is a cold boot.
348f5478dedSAntonio Nino Diaz	 * Perform any processor specific actions upon reset e.g. cache, TLB
349f5478dedSAntonio Nino Diaz	 * invalidations etc.
350f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
351f5478dedSAntonio Nino Diaz	 */
352f5478dedSAntonio Nino Diaz	bl	reset_handler
353f5478dedSAntonio Nino Diaz
354f5478dedSAntonio Nino Diaz	el3_arch_init_common
355f5478dedSAntonio Nino Diaz
356f5478dedSAntonio Nino Diaz	.if \_secondary_cold_boot
357f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
358f5478dedSAntonio Nino Diaz		 * Check if this is a primary or secondary CPU cold boot.
359f5478dedSAntonio Nino Diaz		 * The primary CPU will set up the platform while the
360f5478dedSAntonio Nino Diaz		 * secondaries are placed in a platform-specific state until the
361f5478dedSAntonio Nino Diaz		 * primary CPU performs the necessary actions to bring them out
362f5478dedSAntonio Nino Diaz		 * of that state and allows entry into the OS.
363f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
364f5478dedSAntonio Nino Diaz		 */
365f5478dedSAntonio Nino Diaz		bl	plat_is_my_cpu_primary
366f5478dedSAntonio Nino Diaz		cbnz	w0, do_primary_cold_boot
367f5478dedSAntonio Nino Diaz
368f5478dedSAntonio Nino Diaz		/* This is a cold boot on a secondary CPU */
369f5478dedSAntonio Nino Diaz		bl	plat_secondary_cold_boot_setup
370f5478dedSAntonio Nino Diaz		/* plat_secondary_cold_boot_setup() is not supposed to return */
371f5478dedSAntonio Nino Diaz		bl	el3_panic
372f5478dedSAntonio Nino Diaz
373f5478dedSAntonio Nino Diaz	do_primary_cold_boot:
374f5478dedSAntonio Nino Diaz	.endif /* _secondary_cold_boot */
375f5478dedSAntonio Nino Diaz
376f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
377f5478dedSAntonio Nino Diaz	 * Initialize memory now. Secondary CPU initialization won't get to this
378f5478dedSAntonio Nino Diaz	 * point.
379f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
380f5478dedSAntonio Nino Diaz	 */
381f5478dedSAntonio Nino Diaz
382f5478dedSAntonio Nino Diaz	.if \_init_memory
383f5478dedSAntonio Nino Diaz		bl	platform_mem_init
384f5478dedSAntonio Nino Diaz	.endif /* _init_memory */
385f5478dedSAntonio Nino Diaz
386f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
387f5478dedSAntonio Nino Diaz	 * Init C runtime environment:
388f5478dedSAntonio Nino Diaz	 *   - Zero-initialise the NOBITS sections. There are 2 of them:
389f5478dedSAntonio Nino Diaz	 *       - the .bss section;
390f5478dedSAntonio Nino Diaz	 *       - the coherent memory section (if any).
391f5478dedSAntonio Nino Diaz	 *   - Relocate the data section from ROM to RAM, if required.
392f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
393f5478dedSAntonio Nino Diaz	 */
394f5478dedSAntonio Nino Diaz	.if \_init_c_runtime
395b90f207aSHadi Asyrafi#if defined(IMAGE_BL31) || (defined(IMAGE_BL2) && BL2_AT_EL3 && BL2_INV_DCACHE)
396f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
397f5478dedSAntonio Nino Diaz		 * Invalidate the RW memory used by the BL31 image. This
398f5478dedSAntonio Nino Diaz		 * includes the data and NOBITS sections. This is done to
399f5478dedSAntonio Nino Diaz		 * safeguard against possible corruption of this memory by
400f5478dedSAntonio Nino Diaz		 * dirty cache lines in a system cache as a result of use by
401f5478dedSAntonio Nino Diaz		 * an earlier boot loader stage.
402f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
403f5478dedSAntonio Nino Diaz		 */
404f5478dedSAntonio Nino Diaz		adrp	x0, __RW_START__
405f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__RW_START__
406f5478dedSAntonio Nino Diaz		adrp	x1, __RW_END__
407f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12:__RW_END__
408f5478dedSAntonio Nino Diaz		sub	x1, x1, x0
409f5478dedSAntonio Nino Diaz		bl	inv_dcache_range
410f8578e64SSamuel Holland#if defined(IMAGE_BL31) && SEPARATE_NOBITS_REGION
411f8578e64SSamuel Holland		adrp	x0, __NOBITS_START__
412f8578e64SSamuel Holland		add	x0, x0, :lo12:__NOBITS_START__
413f8578e64SSamuel Holland		adrp	x1, __NOBITS_END__
414f8578e64SSamuel Holland		add	x1, x1, :lo12:__NOBITS_END__
415f8578e64SSamuel Holland		sub	x1, x1, x0
416f8578e64SSamuel Holland		bl	inv_dcache_range
417f8578e64SSamuel Holland#endif
418f5478dedSAntonio Nino Diaz#endif
419f5478dedSAntonio Nino Diaz		adrp	x0, __BSS_START__
420f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__BSS_START__
421f5478dedSAntonio Nino Diaz
422f5478dedSAntonio Nino Diaz		adrp	x1, __BSS_END__
423f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12:__BSS_END__
424f5478dedSAntonio Nino Diaz		sub	x1, x1, x0
425f5478dedSAntonio Nino Diaz		bl	zeromem
426f5478dedSAntonio Nino Diaz
427f5478dedSAntonio Nino Diaz#if USE_COHERENT_MEM
428f5478dedSAntonio Nino Diaz		adrp	x0, __COHERENT_RAM_START__
429f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__COHERENT_RAM_START__
430f5478dedSAntonio Nino Diaz		adrp	x1, __COHERENT_RAM_END_UNALIGNED__
431f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12: __COHERENT_RAM_END_UNALIGNED__
432f5478dedSAntonio Nino Diaz		sub	x1, x1, x0
433f5478dedSAntonio Nino Diaz		bl	zeromem
434f5478dedSAntonio Nino Diaz#endif
435f5478dedSAntonio Nino Diaz
4360a12302cSLionel Debieve#if defined(IMAGE_BL1) || (defined(IMAGE_BL2) && BL2_AT_EL3 && BL2_IN_XIP_MEM)
437f5478dedSAntonio Nino Diaz		adrp	x0, __DATA_RAM_START__
438f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__DATA_RAM_START__
439f5478dedSAntonio Nino Diaz		adrp	x1, __DATA_ROM_START__
440f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12:__DATA_ROM_START__
441f5478dedSAntonio Nino Diaz		adrp	x2, __DATA_RAM_END__
442f5478dedSAntonio Nino Diaz		add	x2, x2, :lo12:__DATA_RAM_END__
443f5478dedSAntonio Nino Diaz		sub	x2, x2, x0
444f5478dedSAntonio Nino Diaz		bl	memcpy16
445f5478dedSAntonio Nino Diaz#endif
446f5478dedSAntonio Nino Diaz	.endif /* _init_c_runtime */
447f5478dedSAntonio Nino Diaz
448f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
449f5478dedSAntonio Nino Diaz	 * Use SP_EL0 for the C runtime stack.
450f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
451f5478dedSAntonio Nino Diaz	 */
452f5478dedSAntonio Nino Diaz	msr	spsel, #0
453f5478dedSAntonio Nino Diaz
454f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
455f5478dedSAntonio Nino Diaz	 * Allocate a stack whose memory will be marked as Normal-IS-WBWA when
456f5478dedSAntonio Nino Diaz	 * the MMU is enabled. There is no risk of reading stale stack memory
457f5478dedSAntonio Nino Diaz	 * after enabling the MMU as only the primary CPU is running at the
458f5478dedSAntonio Nino Diaz	 * moment.
459f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
460f5478dedSAntonio Nino Diaz	 */
461f5478dedSAntonio Nino Diaz	bl	plat_set_my_stack
462f5478dedSAntonio Nino Diaz
463f5478dedSAntonio Nino Diaz#if STACK_PROTECTOR_ENABLED
464f5478dedSAntonio Nino Diaz	.if \_init_c_runtime
465f5478dedSAntonio Nino Diaz	bl	update_stack_protector_canary
466f5478dedSAntonio Nino Diaz	.endif /* _init_c_runtime */
467f5478dedSAntonio Nino Diaz#endif
468f5478dedSAntonio Nino Diaz	.endm
469f5478dedSAntonio Nino Diaz
4703b8456bdSManish V Badarkhe	.macro	apply_at_speculative_wa
4713b8456bdSManish V Badarkhe#if ERRATA_SPECULATIVE_AT
4723b8456bdSManish V Badarkhe	/*
4733b8456bdSManish V Badarkhe	 * Explicitly save x30 so as to free up a register and to enable
4743b8456bdSManish V Badarkhe	 * branching and also, save x29 which will be used in the called
4753b8456bdSManish V Badarkhe	 * function
4763b8456bdSManish V Badarkhe	 */
4773b8456bdSManish V Badarkhe	stp	x29, x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
4783b8456bdSManish V Badarkhe	bl	save_and_update_ptw_el1_sys_regs
4793b8456bdSManish V Badarkhe	ldp	x29, x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
4803b8456bdSManish V Badarkhe#endif
4813b8456bdSManish V Badarkhe	.endm
4823b8456bdSManish V Badarkhe
4833b8456bdSManish V Badarkhe	.macro	restore_ptw_el1_sys_regs
4843b8456bdSManish V Badarkhe#if ERRATA_SPECULATIVE_AT
4853b8456bdSManish V Badarkhe	/* -----------------------------------------------------------
4863b8456bdSManish V Badarkhe	 * In case of ERRATA_SPECULATIVE_AT, must follow below order
4873b8456bdSManish V Badarkhe	 * to ensure that page table walk is not enabled until
4883b8456bdSManish V Badarkhe	 * restoration of all EL1 system registers. TCR_EL1 register
4893b8456bdSManish V Badarkhe	 * should be updated at the end which restores previous page
4903b8456bdSManish V Badarkhe	 * table walk setting of stage1 i.e.(TCR_EL1.EPDx) bits. ISB
4913b8456bdSManish V Badarkhe	 * ensures that CPU does below steps in order.
4923b8456bdSManish V Badarkhe	 *
4933b8456bdSManish V Badarkhe	 * 1. Ensure all other system registers are written before
4943b8456bdSManish V Badarkhe	 *    updating SCTLR_EL1 using ISB.
4953b8456bdSManish V Badarkhe	 * 2. Restore SCTLR_EL1 register.
4963b8456bdSManish V Badarkhe	 * 3. Ensure SCTLR_EL1 written successfully using ISB.
4973b8456bdSManish V Badarkhe	 * 4. Restore TCR_EL1 register.
4983b8456bdSManish V Badarkhe	 * -----------------------------------------------------------
4993b8456bdSManish V Badarkhe	 */
5003b8456bdSManish V Badarkhe	isb
5013b8456bdSManish V Badarkhe	ldp	x28, x29, [sp, #CTX_EL1_SYSREGS_OFFSET + CTX_SCTLR_EL1]
5023b8456bdSManish V Badarkhe	msr	sctlr_el1, x28
5033b8456bdSManish V Badarkhe	isb
5043b8456bdSManish V Badarkhe	msr	tcr_el1, x29
5053b8456bdSManish V Badarkhe#endif
5063b8456bdSManish V Badarkhe	.endm
5073b8456bdSManish V Badarkhe
508f5478dedSAntonio Nino Diaz#endif /* EL3_COMMON_MACROS_S */
509