xref: /rk3399_ARM-atf/include/arch/aarch64/el3_common_macros.S (revision d87c0e277fcc6b07112c0ee6d1aecbc2200055a5)
1f5478dedSAntonio Nino Diaz/*
27d33ffe4SDaniel Boulby * Copyright (c) 2015-2022, Arm Limited and Contributors. All rights reserved.
3f5478dedSAntonio Nino Diaz *
4f5478dedSAntonio Nino Diaz * SPDX-License-Identifier: BSD-3-Clause
5f5478dedSAntonio Nino Diaz */
6f5478dedSAntonio Nino Diaz
7f5478dedSAntonio Nino Diaz#ifndef EL3_COMMON_MACROS_S
8f5478dedSAntonio Nino Diaz#define EL3_COMMON_MACROS_S
9f5478dedSAntonio Nino Diaz
10f5478dedSAntonio Nino Diaz#include <arch.h>
11f5478dedSAntonio Nino Diaz#include <asm_macros.S>
127d33ffe4SDaniel Boulby#include <assert_macros.S>
133b8456bdSManish V Badarkhe#include <context.h>
141a04b2e5SVarun Wadekar#include <lib/xlat_tables/xlat_tables_defs.h>
15f5478dedSAntonio Nino Diaz
16f5478dedSAntonio Nino Diaz	/*
17f5478dedSAntonio Nino Diaz	 * Helper macro to initialise EL3 registers we care about.
18f5478dedSAntonio Nino Diaz	 */
19f5478dedSAntonio Nino Diaz	.macro el3_arch_init_common
20f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
21f5478dedSAntonio Nino Diaz	 * SCTLR_EL3 has already been initialised - read current value before
22f5478dedSAntonio Nino Diaz	 * modifying.
23f5478dedSAntonio Nino Diaz	 *
24f5478dedSAntonio Nino Diaz	 * SCTLR_EL3.I: Enable the instruction cache.
25f5478dedSAntonio Nino Diaz	 *
26f5478dedSAntonio Nino Diaz	 * SCTLR_EL3.SA: Enable Stack Alignment check. A SP alignment fault
27f5478dedSAntonio Nino Diaz	 *  exception is generated if a load or store instruction executed at
28f5478dedSAntonio Nino Diaz	 *  EL3 uses the SP as the base address and the SP is not aligned to a
29f5478dedSAntonio Nino Diaz	 *  16-byte boundary.
30f5478dedSAntonio Nino Diaz	 *
31f5478dedSAntonio Nino Diaz	 * SCTLR_EL3.A: Enable Alignment fault checking. All instructions that
32f5478dedSAntonio Nino Diaz	 *  load or store one or more registers have an alignment check that the
33f5478dedSAntonio Nino Diaz	 *  address being accessed is aligned to the size of the data element(s)
34f5478dedSAntonio Nino Diaz	 *  being accessed.
35f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
36f5478dedSAntonio Nino Diaz	 */
37f5478dedSAntonio Nino Diaz	mov	x1, #(SCTLR_I_BIT | SCTLR_A_BIT | SCTLR_SA_BIT)
38f5478dedSAntonio Nino Diaz	mrs	x0, sctlr_el3
39f5478dedSAntonio Nino Diaz	orr	x0, x0, x1
40f5478dedSAntonio Nino Diaz	msr	sctlr_el3, x0
41f5478dedSAntonio Nino Diaz	isb
42f5478dedSAntonio Nino Diaz
43f5478dedSAntonio Nino Diaz#ifdef IMAGE_BL31
44f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
45f5478dedSAntonio Nino Diaz	 * Initialise the per-cpu cache pointer to the CPU.
46f5478dedSAntonio Nino Diaz	 * This is done early to enable crash reporting to have access to crash
47f5478dedSAntonio Nino Diaz	 * stack. Since crash reporting depends on cpu_data to report the
48f5478dedSAntonio Nino Diaz	 * unhandled exception, not doing so can lead to recursive exceptions
49f5478dedSAntonio Nino Diaz	 * due to a NULL TPIDR_EL3.
50f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
51f5478dedSAntonio Nino Diaz	 */
52f5478dedSAntonio Nino Diaz	bl	init_cpu_data_ptr
53f5478dedSAntonio Nino Diaz#endif /* IMAGE_BL31 */
54f5478dedSAntonio Nino Diaz
55f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
56f5478dedSAntonio Nino Diaz	 * Initialise SCR_EL3, setting all fields rather than relying on hw.
57f5478dedSAntonio Nino Diaz	 * All fields are architecturally UNKNOWN on reset. The following fields
58f5478dedSAntonio Nino Diaz	 * do not change during the TF lifetime. The remaining fields are set to
59f5478dedSAntonio Nino Diaz	 * zero here but are updated ahead of transitioning to a lower EL in the
60f5478dedSAntonio Nino Diaz	 * function cm_init_context_common().
61f5478dedSAntonio Nino Diaz	 *
62f5478dedSAntonio Nino Diaz	 * SCR_EL3.TWE: Set to zero so that execution of WFE instructions at
63f5478dedSAntonio Nino Diaz	 *  EL2, EL1 and EL0 are not trapped to EL3.
64f5478dedSAntonio Nino Diaz	 *
65f5478dedSAntonio Nino Diaz	 * SCR_EL3.TWI: Set to zero so that execution of WFI instructions at
66f5478dedSAntonio Nino Diaz	 *  EL2, EL1 and EL0 are not trapped to EL3.
67f5478dedSAntonio Nino Diaz	 *
68f5478dedSAntonio Nino Diaz	 * SCR_EL3.SIF: Set to one to disable instruction fetches from
69f5478dedSAntonio Nino Diaz	 *  Non-secure memory.
70f5478dedSAntonio Nino Diaz	 *
71f5478dedSAntonio Nino Diaz	 * SCR_EL3.SMD: Set to zero to enable SMC calls at EL1 and above, from
72f5478dedSAntonio Nino Diaz	 *  both Security states and both Execution states.
73f5478dedSAntonio Nino Diaz	 *
74f5478dedSAntonio Nino Diaz	 * SCR_EL3.EA: Set to one to route External Aborts and SError Interrupts
75f5478dedSAntonio Nino Diaz	 *  to EL3 when executing at any EL.
76f5478dedSAntonio Nino Diaz	 *
77f5478dedSAntonio Nino Diaz	 * SCR_EL3.{API,APK}: For Armv8.3 pointer authentication feature,
78f5478dedSAntonio Nino Diaz	 * disable traps to EL3 when accessing key registers or using pointer
79f5478dedSAntonio Nino Diaz	 * authentication instructions from lower ELs.
80f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
81f5478dedSAntonio Nino Diaz	 */
825283962eSAntonio Nino Diaz	mov_imm	x0, ((SCR_RESET_VAL | SCR_EA_BIT | SCR_SIF_BIT) \
83f5478dedSAntonio Nino Diaz			& ~(SCR_TWE_BIT | SCR_TWI_BIT | SCR_SMD_BIT))
845283962eSAntonio Nino Diaz#if CTX_INCLUDE_PAUTH_REGS
855283962eSAntonio Nino Diaz	/*
865283962eSAntonio Nino Diaz	 * If the pointer authentication registers are saved during world
875283962eSAntonio Nino Diaz	 * switches, enable pointer authentication everywhere, as it is safe to
885283962eSAntonio Nino Diaz	 * do so.
895283962eSAntonio Nino Diaz	 */
905283962eSAntonio Nino Diaz	orr	x0, x0, #(SCR_API_BIT | SCR_APK_BIT)
915283962eSAntonio Nino Diaz#endif
926c09af9fSZelalem Aweke#if ENABLE_RME
936c09af9fSZelalem Aweke	/*
946c09af9fSZelalem Aweke	 * TODO: Settting the EEL2 bit to allow EL3 access to secure only registers
956c09af9fSZelalem Aweke	 * in context management. This will need to be refactored.
966c09af9fSZelalem Aweke	 */
976c09af9fSZelalem Aweke	orr	x0, x0, #SCR_EEL2_BIT
986c09af9fSZelalem Aweke#endif
99f5478dedSAntonio Nino Diaz	msr	scr_el3, x0
100f5478dedSAntonio Nino Diaz
101f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
102f5478dedSAntonio Nino Diaz	 * Initialise MDCR_EL3, setting all fields rather than relying on hw.
103f5478dedSAntonio Nino Diaz	 * Some fields are architecturally UNKNOWN on reset.
104f5478dedSAntonio Nino Diaz	 *
105f5478dedSAntonio Nino Diaz	 * MDCR_EL3.SDD: Set to one to disable AArch64 Secure self-hosted debug.
106f5478dedSAntonio Nino Diaz	 *  Debug exceptions, other than Breakpoint Instruction exceptions, are
107f5478dedSAntonio Nino Diaz	 *  disabled from all ELs in Secure state.
108f5478dedSAntonio Nino Diaz	 *
109f5478dedSAntonio Nino Diaz	 * MDCR_EL3.SPD32: Set to 0b10 to disable AArch32 Secure self-hosted
110f5478dedSAntonio Nino Diaz	 *  privileged debug from S-EL1.
111f5478dedSAntonio Nino Diaz	 *
112f5478dedSAntonio Nino Diaz	 * MDCR_EL3.TDOSA: Set to zero so that EL2 and EL2 System register
113f5478dedSAntonio Nino Diaz	 *  access to the powerdown debug registers do not trap to EL3.
114f5478dedSAntonio Nino Diaz	 *
115f5478dedSAntonio Nino Diaz	 * MDCR_EL3.TDA: Set to zero to allow EL0, EL1 and EL2 access to the
116f5478dedSAntonio Nino Diaz	 *  debug registers, other than those registers that are controlled by
117f5478dedSAntonio Nino Diaz	 *  MDCR_EL3.TDOSA.
118f5478dedSAntonio Nino Diaz	 *
119f5478dedSAntonio Nino Diaz	 * MDCR_EL3.TPM: Set to zero so that EL0, EL1, and EL2 System register
120f5478dedSAntonio Nino Diaz	 *  accesses to all Performance Monitors registers do not trap to EL3.
121ed4fc6f0SAntonio Nino Diaz	 *
122ed4fc6f0SAntonio Nino Diaz	 * MDCR_EL3.SCCD: Set to one so that cycle counting by PMCCNTR_EL0 is
123ed4fc6f0SAntonio Nino Diaz	 *  prohibited in Secure state. This bit is RES0 in versions of the
12412f6c064SAlexei Fedorov	 *  architecture with FEAT_PMUv3p5 not implemented, setting it to 1
12512f6c064SAlexei Fedorov	 *  doesn't have any effect on them.
12612f6c064SAlexei Fedorov	 *
12712f6c064SAlexei Fedorov	 * MDCR_EL3.MCCD: Set to one so that cycle counting by PMCCNTR_EL0 is
12812f6c064SAlexei Fedorov	 *  prohibited in EL3. This bit is RES0 in versions of the
12912f6c064SAlexei Fedorov	 *  architecture with FEAT_PMUv3p7 not implemented, setting it to 1
13012f6c064SAlexei Fedorov	 *  doesn't have any effect on them.
1312a7adf25SPetre-Ionut Tudor	 *
1322a7adf25SPetre-Ionut Tudor	 * MDCR_EL3.SPME: Set to zero so that event counting by the programmable
1332a7adf25SPetre-Ionut Tudor	 *  counters PMEVCNTR<n>_EL0 is prohibited in Secure state. If ARMv8.2
1342a7adf25SPetre-Ionut Tudor	 *  Debug is not implemented this bit does not have any effect on the
1352a7adf25SPetre-Ionut Tudor	 *  counters unless there is support for the implementation defined
1362a7adf25SPetre-Ionut Tudor	 *  authentication interface ExternalSecureNoninvasiveDebugEnabled().
13740ff9074SManish V Badarkhe	 *
13840ff9074SManish V Badarkhe	 * MDCR_EL3.NSTB, MDCR_EL3.NSTBE: Set to zero so that Trace Buffer
13940ff9074SManish V Badarkhe	 *  owning security state is Secure state. If FEAT_TRBE is implemented,
14040ff9074SManish V Badarkhe	 *  accesses to Trace Buffer control registers at EL2 and EL1 in any
14140ff9074SManish V Badarkhe	 *  security state generates trap exceptions to EL3.
14240ff9074SManish V Badarkhe	 *  If FEAT_TRBE is not implemented, these bits are RES0.
1435de20eceSManish V Badarkhe	 *
1445de20eceSManish V Badarkhe	 * MDCR_EL3.TTRF: Set to one so that access to trace filter control
1455de20eceSManish V Badarkhe	 *  registers in non-monitor mode generate EL3 trap exception,
1465de20eceSManish V Badarkhe	 *  unless the access generates a higher priority exception when trace
1475de20eceSManish V Badarkhe	 *  filter control(FEAT_TRF) is implemented.
1485de20eceSManish V Badarkhe	 *  When FEAT_TRF is not implemented, this bit is RES0.
149f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
150f5478dedSAntonio Nino Diaz	 */
151ed4fc6f0SAntonio Nino Diaz	mov_imm	x0, ((MDCR_EL3_RESET_VAL | MDCR_SDD_BIT | \
15212f6c064SAlexei Fedorov		      MDCR_SPD32(MDCR_SPD32_DISABLE) | MDCR_SCCD_BIT | \
15312f6c064SAlexei Fedorov		      MDCR_MCCD_BIT) & ~(MDCR_SPME_BIT | MDCR_TDOSA_BIT | \
15440ff9074SManish V Badarkhe		      MDCR_TDA_BIT | MDCR_TPM_BIT | MDCR_NSTB(MDCR_NSTB_EL1) | \
1555de20eceSManish V Badarkhe		      MDCR_NSTBE | MDCR_TTRF_BIT))
156ed4fc6f0SAntonio Nino Diaz
1575de20eceSManish V Badarkhe	mrs	x1, id_aa64dfr0_el1
1585de20eceSManish V Badarkhe	ubfx	x1, x1, #ID_AA64DFR0_TRACEFILT_SHIFT, #ID_AA64DFR0_TRACEFILT_LENGTH
1595de20eceSManish V Badarkhe	cbz	x1, 1f
1605de20eceSManish V Badarkhe	orr	x0, x0, #MDCR_TTRF_BIT
1615de20eceSManish V Badarkhe1:
162f5478dedSAntonio Nino Diaz	msr	mdcr_el3, x0
163f5478dedSAntonio Nino Diaz
164f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
165e290a8fcSAlexei Fedorov	 * Initialise PMCR_EL0 setting all fields rather than relying
166e290a8fcSAlexei Fedorov	 * on hw. Some fields are architecturally UNKNOWN on reset.
167e290a8fcSAlexei Fedorov	 *
168e290a8fcSAlexei Fedorov	 * PMCR_EL0.LP: Set to one so that event counter overflow, that
169e290a8fcSAlexei Fedorov	 *  is recorded in PMOVSCLR_EL0[0-30], occurs on the increment
170e290a8fcSAlexei Fedorov	 *  that changes PMEVCNTR<n>_EL0[63] from 1 to 0, when ARMv8.5-PMU
171e290a8fcSAlexei Fedorov	 *  is implemented. This bit is RES0 in versions of the architecture
172e290a8fcSAlexei Fedorov	 *  earlier than ARMv8.5, setting it to 1 doesn't have any effect
173e290a8fcSAlexei Fedorov	 *  on them.
174e290a8fcSAlexei Fedorov	 *
175e290a8fcSAlexei Fedorov	 * PMCR_EL0.LC: Set to one so that cycle counter overflow, that
176e290a8fcSAlexei Fedorov	 *  is recorded in PMOVSCLR_EL0[31], occurs on the increment
177e290a8fcSAlexei Fedorov	 *  that changes PMCCNTR_EL0[63] from 1 to 0.
178e290a8fcSAlexei Fedorov	 *
179e290a8fcSAlexei Fedorov	 * PMCR_EL0.DP: Set to one so that the cycle counter,
180e290a8fcSAlexei Fedorov	 *  PMCCNTR_EL0 does not count when event counting is prohibited.
181e290a8fcSAlexei Fedorov	 *
182e290a8fcSAlexei Fedorov	 * PMCR_EL0.X: Set to zero to disable export of events.
183e290a8fcSAlexei Fedorov	 *
184e290a8fcSAlexei Fedorov	 * PMCR_EL0.D: Set to zero so that, when enabled, PMCCNTR_EL0
185e290a8fcSAlexei Fedorov	 *  counts on every clock cycle.
186e290a8fcSAlexei Fedorov	 * ---------------------------------------------------------------------
187e290a8fcSAlexei Fedorov	 */
188e290a8fcSAlexei Fedorov	mov_imm	x0, ((PMCR_EL0_RESET_VAL | PMCR_EL0_LP_BIT | \
189e290a8fcSAlexei Fedorov		      PMCR_EL0_LC_BIT | PMCR_EL0_DP_BIT) & \
190e290a8fcSAlexei Fedorov		    ~(PMCR_EL0_X_BIT | PMCR_EL0_D_BIT))
191e290a8fcSAlexei Fedorov
192e290a8fcSAlexei Fedorov	msr	pmcr_el0, x0
193e290a8fcSAlexei Fedorov
194e290a8fcSAlexei Fedorov	/* ---------------------------------------------------------------------
195f5478dedSAntonio Nino Diaz	 * Enable External Aborts and SError Interrupts now that the exception
196f5478dedSAntonio Nino Diaz	 * vectors have been setup.
197f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
198f5478dedSAntonio Nino Diaz	 */
199f5478dedSAntonio Nino Diaz	msr	daifclr, #DAIF_ABT_BIT
200f5478dedSAntonio Nino Diaz
201f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
202f5478dedSAntonio Nino Diaz	 * Initialise CPTR_EL3, setting all fields rather than relying on hw.
203f5478dedSAntonio Nino Diaz	 * All fields are architecturally UNKNOWN on reset.
204f5478dedSAntonio Nino Diaz	 *
205f5478dedSAntonio Nino Diaz	 * CPTR_EL3.TCPAC: Set to zero so that any accesses to CPACR_EL1,
206f5478dedSAntonio Nino Diaz	 *  CPTR_EL2, CPACR, or HCPTR do not trap to EL3.
207f5478dedSAntonio Nino Diaz	 *
2082031d616SManish V Badarkhe	 * CPTR_EL3.TTA: Set to one so that accesses to the trace system
2092031d616SManish V Badarkhe	 *  registers trap to EL3 from all exception levels and security
2102031d616SManish V Badarkhe	 *  states when system register trace is implemented.
2112031d616SManish V Badarkhe	 *  When system register trace is not implemented, this bit is RES0 and
2122031d616SManish V Badarkhe	 *  hence set to zero.
2132031d616SManish V Badarkhe	 *
214f5478dedSAntonio Nino Diaz	 * CPTR_EL3.TTA: Set to zero so that System register accesses to the
215f5478dedSAntonio Nino Diaz	 *  trace registers do not trap to EL3.
216f5478dedSAntonio Nino Diaz	 *
217f5478dedSAntonio Nino Diaz	 * CPTR_EL3.TFP: Set to zero so that accesses to the V- or Z- registers
218f5478dedSAntonio Nino Diaz	 *  by Advanced SIMD, floating-point or SVE instructions (if implemented)
219f5478dedSAntonio Nino Diaz	 *  do not trap to EL3.
2200c5e7d1cSMax Shvetsov	 *
2210c5e7d1cSMax Shvetsov	 * CPTR_EL3.TAM: Set to one so that Activity Monitor access is
2220c5e7d1cSMax Shvetsov	 *  trapped to EL3 by default.
2230c5e7d1cSMax Shvetsov	 *
2240c5e7d1cSMax Shvetsov	 * CPTR_EL3.EZ: Set to zero so that all SVE functionality is trapped
2250c5e7d1cSMax Shvetsov	 *  to EL3 by default.
226dc78e62dSjohpow01	 *
227dc78e62dSjohpow01	 * CPTR_EL3.ESM: Set to zero so that all SME functionality is trapped
228dc78e62dSjohpow01	 *  to EL3 by default.
229f5478dedSAntonio Nino Diaz	 */
2300c5e7d1cSMax Shvetsov
231f5478dedSAntonio Nino Diaz	mov_imm x0, (CPTR_EL3_RESET_VAL & ~(TCPAC_BIT | TTA_BIT | TFP_BIT))
2322031d616SManish V Badarkhe	mrs	x1, id_aa64dfr0_el1
2332031d616SManish V Badarkhe	ubfx	x1, x1, #ID_AA64DFR0_TRACEVER_SHIFT, #ID_AA64DFR0_TRACEVER_LENGTH
2342031d616SManish V Badarkhe	cbz	x1, 1f
2352031d616SManish V Badarkhe	orr	x0, x0, #TTA_BIT
2362031d616SManish V Badarkhe1:
237f5478dedSAntonio Nino Diaz	msr	cptr_el3, x0
238f5478dedSAntonio Nino Diaz
239f5478dedSAntonio Nino Diaz	/*
240f5478dedSAntonio Nino Diaz	 * If Data Independent Timing (DIT) functionality is implemented,
2417d33ffe4SDaniel Boulby	 * always enable DIT in EL3.
2427d33ffe4SDaniel Boulby	 * First assert that the FEAT_DIT build flag matches the feature id
2437d33ffe4SDaniel Boulby	 * register value for DIT.
244f5478dedSAntonio Nino Diaz	 */
2457d33ffe4SDaniel Boulby#if ENABLE_FEAT_DIT
2467d33ffe4SDaniel Boulby#if ENABLE_ASSERTIONS
247f5478dedSAntonio Nino Diaz	mrs	x0, id_aa64pfr0_el1
248f5478dedSAntonio Nino Diaz	ubfx	x0, x0, #ID_AA64PFR0_DIT_SHIFT, #ID_AA64PFR0_DIT_LENGTH
249f5478dedSAntonio Nino Diaz	cmp	x0, #ID_AA64PFR0_DIT_SUPPORTED
2507d33ffe4SDaniel Boulby	ASM_ASSERT(eq)
2517d33ffe4SDaniel Boulby#endif /* ENABLE_ASSERTIONS */
252f5478dedSAntonio Nino Diaz	mov	x0, #DIT_BIT
253f5478dedSAntonio Nino Diaz	msr	DIT, x0
2547d33ffe4SDaniel Boulby#endif
255f5478dedSAntonio Nino Diaz	.endm
256f5478dedSAntonio Nino Diaz
257f5478dedSAntonio Nino Diaz/* -----------------------------------------------------------------------------
258f5478dedSAntonio Nino Diaz * This is the super set of actions that need to be performed during a cold boot
259f5478dedSAntonio Nino Diaz * or a warm boot in EL3. This code is shared by BL1 and BL31.
260f5478dedSAntonio Nino Diaz *
261f5478dedSAntonio Nino Diaz * This macro will always perform reset handling, architectural initialisations
262f5478dedSAntonio Nino Diaz * and stack setup. The rest of the actions are optional because they might not
263f5478dedSAntonio Nino Diaz * be needed, depending on the context in which this macro is called. This is
264f5478dedSAntonio Nino Diaz * why this macro is parameterised ; each parameter allows to enable/disable
265f5478dedSAntonio Nino Diaz * some actions.
266f5478dedSAntonio Nino Diaz *
267f5478dedSAntonio Nino Diaz *  _init_sctlr:
268f5478dedSAntonio Nino Diaz *	Whether the macro needs to initialise SCTLR_EL3, including configuring
269f5478dedSAntonio Nino Diaz *      the endianness of data accesses.
270f5478dedSAntonio Nino Diaz *
271f5478dedSAntonio Nino Diaz *  _warm_boot_mailbox:
272f5478dedSAntonio Nino Diaz *	Whether the macro needs to detect the type of boot (cold/warm). The
273f5478dedSAntonio Nino Diaz *	detection is based on the platform entrypoint address : if it is zero
274f5478dedSAntonio Nino Diaz *	then it is a cold boot, otherwise it is a warm boot. In the latter case,
275f5478dedSAntonio Nino Diaz *	this macro jumps on the platform entrypoint address.
276f5478dedSAntonio Nino Diaz *
277f5478dedSAntonio Nino Diaz *  _secondary_cold_boot:
278f5478dedSAntonio Nino Diaz *	Whether the macro needs to identify the CPU that is calling it: primary
279f5478dedSAntonio Nino Diaz *	CPU or secondary CPU. The primary CPU will be allowed to carry on with
280f5478dedSAntonio Nino Diaz *	the platform initialisations, while the secondaries will be put in a
281f5478dedSAntonio Nino Diaz *	platform-specific state in the meantime.
282f5478dedSAntonio Nino Diaz *
283f5478dedSAntonio Nino Diaz *	If the caller knows this macro will only be called by the primary CPU
284f5478dedSAntonio Nino Diaz *	then this parameter can be defined to 0 to skip this step.
285f5478dedSAntonio Nino Diaz *
286f5478dedSAntonio Nino Diaz * _init_memory:
287f5478dedSAntonio Nino Diaz *	Whether the macro needs to initialise the memory.
288f5478dedSAntonio Nino Diaz *
289f5478dedSAntonio Nino Diaz * _init_c_runtime:
290f5478dedSAntonio Nino Diaz *	Whether the macro needs to initialise the C runtime environment.
291f5478dedSAntonio Nino Diaz *
292f5478dedSAntonio Nino Diaz * _exception_vectors:
293f5478dedSAntonio Nino Diaz *	Address of the exception vectors to program in the VBAR_EL3 register.
294da90359bSManish Pandey *
295da90359bSManish Pandey * _pie_fixup_size:
296da90359bSManish Pandey *	Size of memory region to fixup Global Descriptor Table (GDT).
297da90359bSManish Pandey *
298da90359bSManish Pandey *	A non-zero value is expected when firmware needs GDT to be fixed-up.
299da90359bSManish Pandey *
300f5478dedSAntonio Nino Diaz * -----------------------------------------------------------------------------
301f5478dedSAntonio Nino Diaz */
302f5478dedSAntonio Nino Diaz	.macro el3_entrypoint_common					\
303f5478dedSAntonio Nino Diaz		_init_sctlr, _warm_boot_mailbox, _secondary_cold_boot,	\
304da90359bSManish Pandey		_init_memory, _init_c_runtime, _exception_vectors,	\
305da90359bSManish Pandey		_pie_fixup_size
306f5478dedSAntonio Nino Diaz
307f5478dedSAntonio Nino Diaz	.if \_init_sctlr
308f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
309f5478dedSAntonio Nino Diaz		 * This is the initialisation of SCTLR_EL3 and so must ensure
310f5478dedSAntonio Nino Diaz		 * that all fields are explicitly set rather than relying on hw.
311f5478dedSAntonio Nino Diaz		 * Some fields reset to an IMPLEMENTATION DEFINED value and
312f5478dedSAntonio Nino Diaz		 * others are architecturally UNKNOWN on reset.
313f5478dedSAntonio Nino Diaz		 *
314f5478dedSAntonio Nino Diaz		 * SCTLR.EE: Set the CPU endianness before doing anything that
315f5478dedSAntonio Nino Diaz		 *  might involve memory reads or writes. Set to zero to select
316f5478dedSAntonio Nino Diaz		 *  Little Endian.
317f5478dedSAntonio Nino Diaz		 *
318f5478dedSAntonio Nino Diaz		 * SCTLR_EL3.WXN: For the EL3 translation regime, this field can
319f5478dedSAntonio Nino Diaz		 *  force all memory regions that are writeable to be treated as
320f5478dedSAntonio Nino Diaz		 *  XN (Execute-never). Set to zero so that this control has no
321f5478dedSAntonio Nino Diaz		 *  effect on memory access permissions.
322f5478dedSAntonio Nino Diaz		 *
323f5478dedSAntonio Nino Diaz		 * SCTLR_EL3.SA: Set to zero to disable Stack Alignment check.
324f5478dedSAntonio Nino Diaz		 *
325f5478dedSAntonio Nino Diaz		 * SCTLR_EL3.A: Set to zero to disable Alignment fault checking.
326f5478dedSAntonio Nino Diaz		 *
327f5478dedSAntonio Nino Diaz		 * SCTLR.DSSBS: Set to zero to disable speculation store bypass
328f5478dedSAntonio Nino Diaz		 *  safe behaviour upon exception entry to EL3.
329f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
330f5478dedSAntonio Nino Diaz		 */
331f5478dedSAntonio Nino Diaz		mov_imm	x0, (SCTLR_RESET_VAL & ~(SCTLR_EE_BIT | SCTLR_WXN_BIT \
332f5478dedSAntonio Nino Diaz				| SCTLR_SA_BIT | SCTLR_A_BIT | SCTLR_DSSBS_BIT))
333f5478dedSAntonio Nino Diaz		msr	sctlr_el3, x0
334f5478dedSAntonio Nino Diaz		isb
335f5478dedSAntonio Nino Diaz	.endif /* _init_sctlr */
336f5478dedSAntonio Nino Diaz
3370063dd17SJavier Almansa Sobrino#if DISABLE_MTPMU
3380063dd17SJavier Almansa Sobrino		bl	mtpmu_disable
3390063dd17SJavier Almansa Sobrino#endif
3400063dd17SJavier Almansa Sobrino
341f5478dedSAntonio Nino Diaz	.if \_warm_boot_mailbox
342f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
343f5478dedSAntonio Nino Diaz		 * This code will be executed for both warm and cold resets.
344f5478dedSAntonio Nino Diaz		 * Now is the time to distinguish between the two.
345f5478dedSAntonio Nino Diaz		 * Query the platform entrypoint address and if it is not zero
346f5478dedSAntonio Nino Diaz		 * then it means it is a warm boot so jump to this address.
347f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
348f5478dedSAntonio Nino Diaz		 */
349f5478dedSAntonio Nino Diaz		bl	plat_get_my_entrypoint
350f5478dedSAntonio Nino Diaz		cbz	x0, do_cold_boot
351f5478dedSAntonio Nino Diaz		br	x0
352f5478dedSAntonio Nino Diaz
353f5478dedSAntonio Nino Diaz	do_cold_boot:
354f5478dedSAntonio Nino Diaz	.endif /* _warm_boot_mailbox */
355f5478dedSAntonio Nino Diaz
356da90359bSManish Pandey	.if \_pie_fixup_size
357da90359bSManish Pandey#if ENABLE_PIE
358da90359bSManish Pandey		/*
359da90359bSManish Pandey		 * ------------------------------------------------------------
360da90359bSManish Pandey		 * If PIE is enabled fixup the Global descriptor Table only
361da90359bSManish Pandey		 * once during primary core cold boot path.
362da90359bSManish Pandey		 *
363da90359bSManish Pandey		 * Compile time base address, required for fixup, is calculated
364da90359bSManish Pandey		 * using "pie_fixup" label present within first page.
365da90359bSManish Pandey		 * ------------------------------------------------------------
366da90359bSManish Pandey		 */
367da90359bSManish Pandey	pie_fixup:
368da90359bSManish Pandey		ldr	x0, =pie_fixup
369d7b5f408SJimmy Brisson		and	x0, x0, #~(PAGE_SIZE_MASK)
370da90359bSManish Pandey		mov_imm	x1, \_pie_fixup_size
371da90359bSManish Pandey		add	x1, x1, x0
372da90359bSManish Pandey		bl	fixup_gdt_reloc
373da90359bSManish Pandey#endif /* ENABLE_PIE */
374da90359bSManish Pandey	.endif /* _pie_fixup_size */
375da90359bSManish Pandey
376f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
377f5478dedSAntonio Nino Diaz	 * Set the exception vectors.
378f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
379f5478dedSAntonio Nino Diaz	 */
380f5478dedSAntonio Nino Diaz	adr	x0, \_exception_vectors
381f5478dedSAntonio Nino Diaz	msr	vbar_el3, x0
382f5478dedSAntonio Nino Diaz	isb
383f5478dedSAntonio Nino Diaz
3846c09af9fSZelalem Aweke#if !(defined(IMAGE_BL2) && ENABLE_RME)
385f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
386f5478dedSAntonio Nino Diaz	 * It is a cold boot.
387f5478dedSAntonio Nino Diaz	 * Perform any processor specific actions upon reset e.g. cache, TLB
388f5478dedSAntonio Nino Diaz	 * invalidations etc.
389f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
390f5478dedSAntonio Nino Diaz	 */
391f5478dedSAntonio Nino Diaz	bl	reset_handler
3926c09af9fSZelalem Aweke#endif
393f5478dedSAntonio Nino Diaz
394f5478dedSAntonio Nino Diaz	el3_arch_init_common
395f5478dedSAntonio Nino Diaz
396f5478dedSAntonio Nino Diaz	.if \_secondary_cold_boot
397f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
398f5478dedSAntonio Nino Diaz		 * Check if this is a primary or secondary CPU cold boot.
399f5478dedSAntonio Nino Diaz		 * The primary CPU will set up the platform while the
400f5478dedSAntonio Nino Diaz		 * secondaries are placed in a platform-specific state until the
401f5478dedSAntonio Nino Diaz		 * primary CPU performs the necessary actions to bring them out
402f5478dedSAntonio Nino Diaz		 * of that state and allows entry into the OS.
403f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
404f5478dedSAntonio Nino Diaz		 */
405f5478dedSAntonio Nino Diaz		bl	plat_is_my_cpu_primary
406f5478dedSAntonio Nino Diaz		cbnz	w0, do_primary_cold_boot
407f5478dedSAntonio Nino Diaz
408f5478dedSAntonio Nino Diaz		/* This is a cold boot on a secondary CPU */
409f5478dedSAntonio Nino Diaz		bl	plat_secondary_cold_boot_setup
410f5478dedSAntonio Nino Diaz		/* plat_secondary_cold_boot_setup() is not supposed to return */
411f5478dedSAntonio Nino Diaz		bl	el3_panic
412f5478dedSAntonio Nino Diaz
413f5478dedSAntonio Nino Diaz	do_primary_cold_boot:
414f5478dedSAntonio Nino Diaz	.endif /* _secondary_cold_boot */
415f5478dedSAntonio Nino Diaz
416f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
417f5478dedSAntonio Nino Diaz	 * Initialize memory now. Secondary CPU initialization won't get to this
418f5478dedSAntonio Nino Diaz	 * point.
419f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
420f5478dedSAntonio Nino Diaz	 */
421f5478dedSAntonio Nino Diaz
422f5478dedSAntonio Nino Diaz	.if \_init_memory
423f5478dedSAntonio Nino Diaz		bl	platform_mem_init
424f5478dedSAntonio Nino Diaz	.endif /* _init_memory */
425f5478dedSAntonio Nino Diaz
426f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
427f5478dedSAntonio Nino Diaz	 * Init C runtime environment:
428f5478dedSAntonio Nino Diaz	 *   - Zero-initialise the NOBITS sections. There are 2 of them:
429f5478dedSAntonio Nino Diaz	 *       - the .bss section;
430f5478dedSAntonio Nino Diaz	 *       - the coherent memory section (if any).
431f5478dedSAntonio Nino Diaz	 *   - Relocate the data section from ROM to RAM, if required.
432f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
433f5478dedSAntonio Nino Diaz	 */
434f5478dedSAntonio Nino Diaz	.if \_init_c_runtime
4356c09af9fSZelalem Aweke#if defined(IMAGE_BL31) || (defined(IMAGE_BL2) && \
4366c09af9fSZelalem Aweke	((BL2_AT_EL3 && BL2_INV_DCACHE) || ENABLE_RME))
437f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
438f5478dedSAntonio Nino Diaz		 * Invalidate the RW memory used by the BL31 image. This
439f5478dedSAntonio Nino Diaz		 * includes the data and NOBITS sections. This is done to
440f5478dedSAntonio Nino Diaz		 * safeguard against possible corruption of this memory by
441f5478dedSAntonio Nino Diaz		 * dirty cache lines in a system cache as a result of use by
442596d20d9SZelalem Aweke		 * an earlier boot loader stage. If PIE is enabled however,
443596d20d9SZelalem Aweke		 * RO sections including the GOT may be modified during
444596d20d9SZelalem Aweke                 * pie fixup. Therefore, to be on the safe side, invalidate
445596d20d9SZelalem Aweke		 * the entire image region if PIE is enabled.
446f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
447f5478dedSAntonio Nino Diaz		 */
448596d20d9SZelalem Aweke#if ENABLE_PIE
449596d20d9SZelalem Aweke#if SEPARATE_CODE_AND_RODATA
450596d20d9SZelalem Aweke		adrp	x0, __TEXT_START__
451596d20d9SZelalem Aweke		add	x0, x0, :lo12:__TEXT_START__
452596d20d9SZelalem Aweke#else
453596d20d9SZelalem Aweke		adrp	x0, __RO_START__
454596d20d9SZelalem Aweke		add	x0, x0, :lo12:__RO_START__
455596d20d9SZelalem Aweke#endif /* SEPARATE_CODE_AND_RODATA */
456596d20d9SZelalem Aweke#else
457f5478dedSAntonio Nino Diaz		adrp	x0, __RW_START__
458f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__RW_START__
459596d20d9SZelalem Aweke#endif /* ENABLE_PIE */
460f5478dedSAntonio Nino Diaz		adrp	x1, __RW_END__
461f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12:__RW_END__
462f5478dedSAntonio Nino Diaz		sub	x1, x1, x0
463f5478dedSAntonio Nino Diaz		bl	inv_dcache_range
464f8578e64SSamuel Holland#if defined(IMAGE_BL31) && SEPARATE_NOBITS_REGION
465f8578e64SSamuel Holland		adrp	x0, __NOBITS_START__
466f8578e64SSamuel Holland		add	x0, x0, :lo12:__NOBITS_START__
467f8578e64SSamuel Holland		adrp	x1, __NOBITS_END__
468f8578e64SSamuel Holland		add	x1, x1, :lo12:__NOBITS_END__
469f8578e64SSamuel Holland		sub	x1, x1, x0
470f8578e64SSamuel Holland		bl	inv_dcache_range
471f8578e64SSamuel Holland#endif
47296a8ed14SJiafei Pan#if defined(IMAGE_BL2) && SEPARATE_BL2_NOLOAD_REGION
47396a8ed14SJiafei Pan		adrp	x0, __BL2_NOLOAD_START__
47496a8ed14SJiafei Pan		add	x0, x0, :lo12:__BL2_NOLOAD_START__
47596a8ed14SJiafei Pan		adrp	x1, __BL2_NOLOAD_END__
47696a8ed14SJiafei Pan		add	x1, x1, :lo12:__BL2_NOLOAD_END__
47796a8ed14SJiafei Pan		sub	x1, x1, x0
47896a8ed14SJiafei Pan		bl	inv_dcache_range
47996a8ed14SJiafei Pan#endif
480f5478dedSAntonio Nino Diaz#endif
481f5478dedSAntonio Nino Diaz		adrp	x0, __BSS_START__
482f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__BSS_START__
483f5478dedSAntonio Nino Diaz
484f5478dedSAntonio Nino Diaz		adrp	x1, __BSS_END__
485f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12:__BSS_END__
486f5478dedSAntonio Nino Diaz		sub	x1, x1, x0
487f5478dedSAntonio Nino Diaz		bl	zeromem
488f5478dedSAntonio Nino Diaz
489f5478dedSAntonio Nino Diaz#if USE_COHERENT_MEM
490f5478dedSAntonio Nino Diaz		adrp	x0, __COHERENT_RAM_START__
491f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__COHERENT_RAM_START__
492f5478dedSAntonio Nino Diaz		adrp	x1, __COHERENT_RAM_END_UNALIGNED__
493f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12: __COHERENT_RAM_END_UNALIGNED__
494f5478dedSAntonio Nino Diaz		sub	x1, x1, x0
495f5478dedSAntonio Nino Diaz		bl	zeromem
496f5478dedSAntonio Nino Diaz#endif
497f5478dedSAntonio Nino Diaz
4980a12302cSLionel Debieve#if defined(IMAGE_BL1) || (defined(IMAGE_BL2) && BL2_AT_EL3 && BL2_IN_XIP_MEM)
499f5478dedSAntonio Nino Diaz		adrp	x0, __DATA_RAM_START__
500f5478dedSAntonio Nino Diaz		add	x0, x0, :lo12:__DATA_RAM_START__
501f5478dedSAntonio Nino Diaz		adrp	x1, __DATA_ROM_START__
502f5478dedSAntonio Nino Diaz		add	x1, x1, :lo12:__DATA_ROM_START__
503f5478dedSAntonio Nino Diaz		adrp	x2, __DATA_RAM_END__
504f5478dedSAntonio Nino Diaz		add	x2, x2, :lo12:__DATA_RAM_END__
505f5478dedSAntonio Nino Diaz		sub	x2, x2, x0
506f5478dedSAntonio Nino Diaz		bl	memcpy16
507f5478dedSAntonio Nino Diaz#endif
508f5478dedSAntonio Nino Diaz	.endif /* _init_c_runtime */
509f5478dedSAntonio Nino Diaz
510f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
511f5478dedSAntonio Nino Diaz	 * Use SP_EL0 for the C runtime stack.
512f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
513f5478dedSAntonio Nino Diaz	 */
514f5478dedSAntonio Nino Diaz	msr	spsel, #0
515f5478dedSAntonio Nino Diaz
516f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
517f5478dedSAntonio Nino Diaz	 * Allocate a stack whose memory will be marked as Normal-IS-WBWA when
518f5478dedSAntonio Nino Diaz	 * the MMU is enabled. There is no risk of reading stale stack memory
519f5478dedSAntonio Nino Diaz	 * after enabling the MMU as only the primary CPU is running at the
520f5478dedSAntonio Nino Diaz	 * moment.
521f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
522f5478dedSAntonio Nino Diaz	 */
523f5478dedSAntonio Nino Diaz	bl	plat_set_my_stack
524f5478dedSAntonio Nino Diaz
525f5478dedSAntonio Nino Diaz#if STACK_PROTECTOR_ENABLED
526f5478dedSAntonio Nino Diaz	.if \_init_c_runtime
527f5478dedSAntonio Nino Diaz	bl	update_stack_protector_canary
528f5478dedSAntonio Nino Diaz	.endif /* _init_c_runtime */
529f5478dedSAntonio Nino Diaz#endif
530f5478dedSAntonio Nino Diaz	.endm
531f5478dedSAntonio Nino Diaz
5323b8456bdSManish V Badarkhe	.macro	apply_at_speculative_wa
5333b8456bdSManish V Badarkhe#if ERRATA_SPECULATIVE_AT
5343b8456bdSManish V Badarkhe	/*
535*d87c0e27SManish Pandey	 * This function expects x30 has been saved.
536*d87c0e27SManish Pandey	 * Also, save x29 which will be used in the called function.
5373b8456bdSManish V Badarkhe	 */
538*d87c0e27SManish Pandey	str	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
5393b8456bdSManish V Badarkhe	bl	save_and_update_ptw_el1_sys_regs
540*d87c0e27SManish Pandey	ldr	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
5413b8456bdSManish V Badarkhe#endif
5423b8456bdSManish V Badarkhe	.endm
5433b8456bdSManish V Badarkhe
5443b8456bdSManish V Badarkhe	.macro	restore_ptw_el1_sys_regs
5453b8456bdSManish V Badarkhe#if ERRATA_SPECULATIVE_AT
5463b8456bdSManish V Badarkhe	/* -----------------------------------------------------------
5473b8456bdSManish V Badarkhe	 * In case of ERRATA_SPECULATIVE_AT, must follow below order
5483b8456bdSManish V Badarkhe	 * to ensure that page table walk is not enabled until
5493b8456bdSManish V Badarkhe	 * restoration of all EL1 system registers. TCR_EL1 register
5503b8456bdSManish V Badarkhe	 * should be updated at the end which restores previous page
5513b8456bdSManish V Badarkhe	 * table walk setting of stage1 i.e.(TCR_EL1.EPDx) bits. ISB
5523b8456bdSManish V Badarkhe	 * ensures that CPU does below steps in order.
5533b8456bdSManish V Badarkhe	 *
5543b8456bdSManish V Badarkhe	 * 1. Ensure all other system registers are written before
5553b8456bdSManish V Badarkhe	 *    updating SCTLR_EL1 using ISB.
5563b8456bdSManish V Badarkhe	 * 2. Restore SCTLR_EL1 register.
5573b8456bdSManish V Badarkhe	 * 3. Ensure SCTLR_EL1 written successfully using ISB.
5583b8456bdSManish V Badarkhe	 * 4. Restore TCR_EL1 register.
5593b8456bdSManish V Badarkhe	 * -----------------------------------------------------------
5603b8456bdSManish V Badarkhe	 */
5613b8456bdSManish V Badarkhe	isb
5623b8456bdSManish V Badarkhe	ldp	x28, x29, [sp, #CTX_EL1_SYSREGS_OFFSET + CTX_SCTLR_EL1]
5633b8456bdSManish V Badarkhe	msr	sctlr_el1, x28
5643b8456bdSManish V Badarkhe	isb
5653b8456bdSManish V Badarkhe	msr	tcr_el1, x29
5663b8456bdSManish V Badarkhe#endif
5673b8456bdSManish V Badarkhe	.endm
5683b8456bdSManish V Badarkhe
569f5478dedSAntonio Nino Diaz#endif /* EL3_COMMON_MACROS_S */
570