xref: /rk3399_ARM-atf/include/arch/aarch32/el3_common_macros.S (revision 96a8ed14b74cca33a8caf567d0f0a2d3b2483a3b)
1f5478dedSAntonio Nino Diaz/*
2*96a8ed14SJiafei Pan * Copyright (c) 2016-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>
12f5478dedSAntonio Nino Diaz#include <assert_macros.S>
134324a14bSYann Gautier#include <lib/xlat_tables/xlat_tables_defs.h>
144324a14bSYann Gautier
154324a14bSYann Gautier#define PAGE_START_MASK		~(PAGE_SIZE_MASK)
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 has already been initialised - read current value before
23f5478dedSAntonio Nino Diaz	 * modifying.
24f5478dedSAntonio Nino Diaz	 *
25f5478dedSAntonio Nino Diaz	 * SCTLR.I: Enable the instruction cache.
26f5478dedSAntonio Nino Diaz	 *
27f5478dedSAntonio Nino Diaz	 * SCTLR.A: Enable Alignment fault checking. All instructions that load
28f5478dedSAntonio Nino Diaz	 *  or store one or more registers have an alignment check that the
29f5478dedSAntonio Nino Diaz	 *  address being accessed is aligned to the size of the data element(s)
30f5478dedSAntonio Nino Diaz	 *  being accessed.
31f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
32f5478dedSAntonio Nino Diaz	 */
33f5478dedSAntonio Nino Diaz	ldr	r1, =(SCTLR_I_BIT | SCTLR_A_BIT)
34f5478dedSAntonio Nino Diaz	ldcopr	r0, SCTLR
35f5478dedSAntonio Nino Diaz	orr	r0, r0, r1
36f5478dedSAntonio Nino Diaz	stcopr	r0, SCTLR
37f5478dedSAntonio Nino Diaz	isb
38f5478dedSAntonio Nino Diaz
39f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
40f5478dedSAntonio Nino Diaz	 * Initialise SCR, setting all fields rather than relying on the hw.
41f5478dedSAntonio Nino Diaz	 *
42f5478dedSAntonio Nino Diaz	 * SCR.SIF: Enabled so that Secure state instruction fetches from
43f5478dedSAntonio Nino Diaz	 *  Non-secure memory are not permitted.
44f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
45f5478dedSAntonio Nino Diaz	 */
46f5478dedSAntonio Nino Diaz	ldr	r0, =(SCR_RESET_VAL | SCR_SIF_BIT)
47f5478dedSAntonio Nino Diaz	stcopr	r0, SCR
48f5478dedSAntonio Nino Diaz
49f5478dedSAntonio Nino Diaz	/* -----------------------------------------------------
50f5478dedSAntonio Nino Diaz	 * Enable the Asynchronous data abort now that the
51f5478dedSAntonio Nino Diaz	 * exception vectors have been setup.
52f5478dedSAntonio Nino Diaz	 * -----------------------------------------------------
53f5478dedSAntonio Nino Diaz	 */
54f5478dedSAntonio Nino Diaz	cpsie   a
55f5478dedSAntonio Nino Diaz	isb
56f5478dedSAntonio Nino Diaz
57f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
58f5478dedSAntonio Nino Diaz	 * Initialise NSACR, setting all the fields, except for the
59f5478dedSAntonio Nino Diaz	 * IMPLEMENTATION DEFINED field, rather than relying on the hw. Some
60f5478dedSAntonio Nino Diaz	 * fields are architecturally UNKNOWN on reset.
61f5478dedSAntonio Nino Diaz	 *
62f5478dedSAntonio Nino Diaz	 * NSACR_ENABLE_FP_ACCESS: Represents NSACR.cp11 and NSACR.cp10. The
63f5478dedSAntonio Nino Diaz	 *  cp11 field is ignored, but is set to same value as cp10. The cp10
64f5478dedSAntonio Nino Diaz	 *  field is set to allow access to Advanced SIMD and floating point
65f5478dedSAntonio Nino Diaz	 *  features from both Security states.
662031d616SManish V Badarkhe	 *
672031d616SManish V Badarkhe	 * NSACR.NSTRCDIS: When system register trace implemented, Set to one
682031d616SManish V Badarkhe	 *  so that NS System register accesses to all implemented trace
692031d616SManish V Badarkhe	 *  registers are disabled.
702031d616SManish V Badarkhe	 *  When system register trace is not implemented, this bit is RES0 and
712031d616SManish V Badarkhe	 *  hence set to zero.
72f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
73f5478dedSAntonio Nino Diaz	 */
74f5478dedSAntonio Nino Diaz	ldcopr	r0, NSACR
75f5478dedSAntonio Nino Diaz	and	r0, r0, #NSACR_IMP_DEF_MASK
76f5478dedSAntonio Nino Diaz	orr	r0, r0, #(NSACR_RESET_VAL | NSACR_ENABLE_FP_ACCESS)
772031d616SManish V Badarkhe	ldcopr	r1, ID_DFR0
782031d616SManish V Badarkhe	ubfx	r1, r1, #ID_DFR0_COPTRC_SHIFT, #ID_DFR0_COPTRC_LENGTH
792031d616SManish V Badarkhe	cmp	r1, #ID_DFR0_COPTRC_SUPPORTED
802031d616SManish V Badarkhe	bne	1f
812031d616SManish V Badarkhe	orr	r0, r0, #NSTRCDIS_BIT
822031d616SManish V Badarkhe1:
83f5478dedSAntonio Nino Diaz	stcopr	r0, NSACR
84f5478dedSAntonio Nino Diaz	isb
85f5478dedSAntonio Nino Diaz
86f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
87f5478dedSAntonio Nino Diaz	 * Initialise CPACR, setting all fields rather than relying on hw. Some
88f5478dedSAntonio Nino Diaz	 * fields are architecturally UNKNOWN on reset.
89f5478dedSAntonio Nino Diaz	 *
90f5478dedSAntonio Nino Diaz	 * CPACR.TRCDIS: Trap control for PL0 and PL1 System register accesses
91f5478dedSAntonio Nino Diaz	 *  to trace registers. Set to zero to allow access.
92f5478dedSAntonio Nino Diaz	 *
93f5478dedSAntonio Nino Diaz	 * CPACR_ENABLE_FP_ACCESS: Represents CPACR.cp11 and CPACR.cp10. The
94f5478dedSAntonio Nino Diaz	 *  cp11 field is ignored, but is set to same value as cp10. The cp10
95f5478dedSAntonio Nino Diaz	 *  field is set to allow full access from PL0 and PL1 to floating-point
96f5478dedSAntonio Nino Diaz	 *  and Advanced SIMD features.
97f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
98f5478dedSAntonio Nino Diaz	 */
99f5478dedSAntonio Nino Diaz	ldr	r0, =((CPACR_RESET_VAL | CPACR_ENABLE_FP_ACCESS) & ~(TRCDIS_BIT))
100f5478dedSAntonio Nino Diaz	stcopr	r0, CPACR
101f5478dedSAntonio Nino Diaz	isb
102f5478dedSAntonio Nino Diaz
103f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
104f5478dedSAntonio Nino Diaz	 * Initialise FPEXC, setting all fields rather than relying on hw. Some
105f5478dedSAntonio Nino Diaz	 * fields are architecturally UNKNOWN on reset and are set to zero
106f5478dedSAntonio Nino Diaz	 * except for field(s) listed below.
107f5478dedSAntonio Nino Diaz	 *
108f5478dedSAntonio Nino Diaz	 * FPEXC.EN: Enable access to Advanced SIMD and floating point features
109f5478dedSAntonio Nino Diaz	 *  from all exception levels.
110fbd8f6c8SManish Pandey         *
111fbd8f6c8SManish Pandey         * __SOFTFP__: Predefined macro exposed by soft-float toolchain.
112fbd8f6c8SManish Pandey         *  ARMv7 and Cortex-A32(ARMv8/aarch32) has both soft-float and
113fbd8f6c8SManish Pandey         *  hard-float variants of toolchain, avoid compiling below code with
114fbd8f6c8SManish Pandey         *  soft-float toolchain as "vmsr" instruction will not be recognized.
115f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
116f5478dedSAntonio Nino Diaz	 */
117fbd8f6c8SManish Pandey#if ((ARM_ARCH_MAJOR > 7) || defined(ARMV7_SUPPORTS_VFP)) && !(__SOFTFP__)
118f5478dedSAntonio Nino Diaz	ldr	r0, =(FPEXC_RESET_VAL | FPEXC_EN_BIT)
119f5478dedSAntonio Nino Diaz	vmsr	FPEXC, r0
120f5478dedSAntonio Nino Diaz	isb
1218f73663bSUsama Arif#endif
122f5478dedSAntonio Nino Diaz
123f5478dedSAntonio Nino Diaz#if (ARM_ARCH_MAJOR > 7)
124f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
125f5478dedSAntonio Nino Diaz	 * Initialise SDCR, setting all the fields rather than relying on hw.
126f5478dedSAntonio Nino Diaz	 *
127f5478dedSAntonio Nino Diaz	 * SDCR.SPD: Disable AArch32 privileged debug. Debug exceptions from
128f5478dedSAntonio Nino Diaz	 *  Secure EL1 are disabled.
129ed4fc6f0SAntonio Nino Diaz	 *
130c3e8b0beSAlexei Fedorov	 * SDCR.SCCD: Set to one so that cycle counting by PMCCNTR is prohibited
131c3e8b0beSAlexei Fedorov	 *  in Secure state. This bit is RES0 in versions of the architecture
132ed4fc6f0SAntonio Nino Diaz	 *  earlier than ARMv8.5, setting it to 1 doesn't have any effect on
133ed4fc6f0SAntonio Nino Diaz	 *  them.
1345de20eceSManish V Badarkhe	 *
1355de20eceSManish V Badarkhe	 * SDCR.TTRF: Set to one so that access to trace filter control
1365de20eceSManish V Badarkhe	 *  registers in non-monitor mode generate Monitor trap exception,
1375de20eceSManish V Badarkhe	 *  unless the access generates a higher priority exception when
1385de20eceSManish V Badarkhe	 *  trace filter control(FEAT_TRF) is implemented.
1395de20eceSManish V Badarkhe	 *  When FEAT_TRF is not implemented, this bit is RES0.
140f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
141f5478dedSAntonio Nino Diaz	 */
1425de20eceSManish V Badarkhe	ldr	r0, =((SDCR_RESET_VAL | SDCR_SPD(SDCR_SPD_DISABLE) | \
1435de20eceSManish V Badarkhe		      SDCR_SCCD_BIT) & ~SDCR_TTRF_BIT)
1445de20eceSManish V Badarkhe	ldcopr	r1, ID_DFR0
1455de20eceSManish V Badarkhe	ubfx	r1, r1, #ID_DFR0_TRACEFILT_SHIFT, #ID_DFR0_TRACEFILT_LENGTH
1465de20eceSManish V Badarkhe	cmp	r1, #ID_DFR0_TRACEFILT_SUPPORTED
1475de20eceSManish V Badarkhe	bne	1f
1485de20eceSManish V Badarkhe	orr	r0, r0, #SDCR_TTRF_BIT
1495de20eceSManish V Badarkhe1:
150f5478dedSAntonio Nino Diaz	stcopr	r0, SDCR
151c3e8b0beSAlexei Fedorov
152c3e8b0beSAlexei Fedorov	/* ---------------------------------------------------------------------
153c3e8b0beSAlexei Fedorov	 * Initialise PMCR, setting all fields rather than relying
154c3e8b0beSAlexei Fedorov	 * on hw. Some fields are architecturally UNKNOWN on reset.
155c3e8b0beSAlexei Fedorov	 *
156c3e8b0beSAlexei Fedorov	 * PMCR.LP: Set to one so that event counter overflow, that
157c3e8b0beSAlexei Fedorov	 *  is recorded in PMOVSCLR[0-30], occurs on the increment
158c3e8b0beSAlexei Fedorov	 *  that changes PMEVCNTR<n>[63] from 1 to 0, when ARMv8.5-PMU
159c3e8b0beSAlexei Fedorov	 *  is implemented. This bit is RES0 in versions of the architecture
160c3e8b0beSAlexei Fedorov	 *  earlier than ARMv8.5, setting it to 1 doesn't have any effect
161c3e8b0beSAlexei Fedorov	 *  on them.
162c3e8b0beSAlexei Fedorov	 *  This bit is Reserved, UNK/SBZP in ARMv7.
163c3e8b0beSAlexei Fedorov	 *
164c3e8b0beSAlexei Fedorov	 * PMCR.LC: Set to one so that cycle counter overflow, that
165c3e8b0beSAlexei Fedorov	 *  is recorded in PMOVSCLR[31], occurs on the increment
166c3e8b0beSAlexei Fedorov	 *  that changes PMCCNTR[63] from 1 to 0.
167c3e8b0beSAlexei Fedorov	 *  This bit is Reserved, UNK/SBZP in ARMv7.
168c3e8b0beSAlexei Fedorov	 *
169c3e8b0beSAlexei Fedorov	 * PMCR.DP: Set to one to prohibit cycle counting whilst in Secure mode.
170c3e8b0beSAlexei Fedorov	 * ---------------------------------------------------------------------
171c3e8b0beSAlexei Fedorov	 */
172c3e8b0beSAlexei Fedorov	ldr	r0, =(PMCR_RESET_VAL | PMCR_DP_BIT | PMCR_LC_BIT | \
173c3e8b0beSAlexei Fedorov		      PMCR_LP_BIT)
174c3e8b0beSAlexei Fedorov#else
175c3e8b0beSAlexei Fedorov	ldr	r0, =(PMCR_RESET_VAL | PMCR_DP_BIT)
176f5478dedSAntonio Nino Diaz#endif
177c3e8b0beSAlexei Fedorov	stcopr	r0, PMCR
178f5478dedSAntonio Nino Diaz
179f5478dedSAntonio Nino Diaz	/*
180f5478dedSAntonio Nino Diaz	 * If Data Independent Timing (DIT) functionality is implemented,
181f5478dedSAntonio Nino Diaz	 * always enable DIT in EL3
182f5478dedSAntonio Nino Diaz	 */
183f5478dedSAntonio Nino Diaz	ldcopr	r0, ID_PFR0
184f5478dedSAntonio Nino Diaz	and	r0, r0, #(ID_PFR0_DIT_MASK << ID_PFR0_DIT_SHIFT)
185f5478dedSAntonio Nino Diaz	cmp	r0, #ID_PFR0_DIT_SUPPORTED
186f5478dedSAntonio Nino Diaz	bne	1f
187f5478dedSAntonio Nino Diaz	mrs	r0, cpsr
188f5478dedSAntonio Nino Diaz	orr	r0, r0, #CPSR_DIT_BIT
189f5478dedSAntonio Nino Diaz	msr	cpsr_cxsf, r0
190f5478dedSAntonio Nino Diaz1:
191f5478dedSAntonio Nino Diaz	.endm
192f5478dedSAntonio Nino Diaz
193f5478dedSAntonio Nino Diaz/* -----------------------------------------------------------------------------
194f5478dedSAntonio Nino Diaz * This is the super set of actions that need to be performed during a cold boot
195f5478dedSAntonio Nino Diaz * or a warm boot in EL3. This code is shared by BL1 and BL32 (SP_MIN).
196f5478dedSAntonio Nino Diaz *
197f5478dedSAntonio Nino Diaz * This macro will always perform reset handling, architectural initialisations
198f5478dedSAntonio Nino Diaz * and stack setup. The rest of the actions are optional because they might not
199f5478dedSAntonio Nino Diaz * be needed, depending on the context in which this macro is called. This is
200f5478dedSAntonio Nino Diaz * why this macro is parameterised ; each parameter allows to enable/disable
201f5478dedSAntonio Nino Diaz * some actions.
202f5478dedSAntonio Nino Diaz *
203f5478dedSAntonio Nino Diaz *  _init_sctlr:
204f5478dedSAntonio Nino Diaz *	Whether the macro needs to initialise the SCTLR register including
205f5478dedSAntonio Nino Diaz *	configuring the endianness of data accesses.
206f5478dedSAntonio Nino Diaz *
207f5478dedSAntonio Nino Diaz *  _warm_boot_mailbox:
208f5478dedSAntonio Nino Diaz *	Whether the macro needs to detect the type of boot (cold/warm). The
209f5478dedSAntonio Nino Diaz *	detection is based on the platform entrypoint address : if it is zero
210f5478dedSAntonio Nino Diaz *	then it is a cold boot, otherwise it is a warm boot. In the latter case,
211f5478dedSAntonio Nino Diaz *	this macro jumps on the platform entrypoint address.
212f5478dedSAntonio Nino Diaz *
213f5478dedSAntonio Nino Diaz *  _secondary_cold_boot:
214f5478dedSAntonio Nino Diaz *	Whether the macro needs to identify the CPU that is calling it: primary
215f5478dedSAntonio Nino Diaz *	CPU or secondary CPU. The primary CPU will be allowed to carry on with
216f5478dedSAntonio Nino Diaz *	the platform initialisations, while the secondaries will be put in a
217f5478dedSAntonio Nino Diaz *	platform-specific state in the meantime.
218f5478dedSAntonio Nino Diaz *
219f5478dedSAntonio Nino Diaz *	If the caller knows this macro will only be called by the primary CPU
220f5478dedSAntonio Nino Diaz *	then this parameter can be defined to 0 to skip this step.
221f5478dedSAntonio Nino Diaz *
222f5478dedSAntonio Nino Diaz * _init_memory:
223f5478dedSAntonio Nino Diaz *	Whether the macro needs to initialise the memory.
224f5478dedSAntonio Nino Diaz *
225f5478dedSAntonio Nino Diaz * _init_c_runtime:
226f5478dedSAntonio Nino Diaz *	Whether the macro needs to initialise the C runtime environment.
227f5478dedSAntonio Nino Diaz *
228f5478dedSAntonio Nino Diaz * _exception_vectors:
229f5478dedSAntonio Nino Diaz *	Address of the exception vectors to program in the VBAR_EL3 register.
2304324a14bSYann Gautier *
2314324a14bSYann Gautier * _pie_fixup_size:
2324324a14bSYann Gautier *	Size of memory region to fixup Global Descriptor Table (GDT).
2334324a14bSYann Gautier *
2344324a14bSYann Gautier *	A non-zero value is expected when firmware needs GDT to be fixed-up.
2354324a14bSYann Gautier *
236f5478dedSAntonio Nino Diaz * -----------------------------------------------------------------------------
237f5478dedSAntonio Nino Diaz */
238f5478dedSAntonio Nino Diaz	.macro el3_entrypoint_common					\
239f5478dedSAntonio Nino Diaz		_init_sctlr, _warm_boot_mailbox, _secondary_cold_boot,	\
2404324a14bSYann Gautier		_init_memory, _init_c_runtime, _exception_vectors,	\
2414324a14bSYann Gautier		_pie_fixup_size
242f5478dedSAntonio Nino Diaz
243f5478dedSAntonio Nino Diaz	/* Make sure we are in Secure Mode */
244f5478dedSAntonio Nino Diaz#if ENABLE_ASSERTIONS
245f5478dedSAntonio Nino Diaz	ldcopr	r0, SCR
246f5478dedSAntonio Nino Diaz	tst	r0, #SCR_NS_BIT
247f5478dedSAntonio Nino Diaz	ASM_ASSERT(eq)
248f5478dedSAntonio Nino Diaz#endif
249f5478dedSAntonio Nino Diaz
250f5478dedSAntonio Nino Diaz	.if \_init_sctlr
251f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
252f5478dedSAntonio Nino Diaz		 * This is the initialisation of SCTLR and so must ensure that
253f5478dedSAntonio Nino Diaz		 * all fields are explicitly set rather than relying on hw. Some
254f5478dedSAntonio Nino Diaz		 * fields reset to an IMPLEMENTATION DEFINED value.
255f5478dedSAntonio Nino Diaz		 *
256f5478dedSAntonio Nino Diaz		 * SCTLR.TE: Set to zero so that exceptions to an Exception
257f5478dedSAntonio Nino Diaz		 *  Level executing at PL1 are taken to A32 state.
258f5478dedSAntonio Nino Diaz		 *
259f5478dedSAntonio Nino Diaz		 * SCTLR.EE: Set the CPU endianness before doing anything that
260f5478dedSAntonio Nino Diaz		 *  might involve memory reads or writes. Set to zero to select
261f5478dedSAntonio Nino Diaz		 *  Little Endian.
262f5478dedSAntonio Nino Diaz		 *
263f5478dedSAntonio Nino Diaz		 * SCTLR.V: Set to zero to select the normal exception vectors
264f5478dedSAntonio Nino Diaz		 *  with base address held in VBAR.
265f5478dedSAntonio Nino Diaz		 *
266f5478dedSAntonio Nino Diaz		 * SCTLR.DSSBS: Set to zero to disable speculation store bypass
267f5478dedSAntonio Nino Diaz		 *  safe behaviour upon exception entry to EL3.
268f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
269f5478dedSAntonio Nino Diaz		 */
270f5478dedSAntonio Nino Diaz		ldr     r0, =(SCTLR_RESET_VAL & ~(SCTLR_TE_BIT | SCTLR_EE_BIT | \
271f5478dedSAntonio Nino Diaz				SCTLR_V_BIT | SCTLR_DSSBS_BIT))
272f5478dedSAntonio Nino Diaz		stcopr	r0, SCTLR
273f5478dedSAntonio Nino Diaz		isb
274f5478dedSAntonio Nino Diaz	.endif /* _init_sctlr */
275f5478dedSAntonio Nino Diaz
276f5478dedSAntonio Nino Diaz	/* Switch to monitor mode */
277f5478dedSAntonio Nino Diaz	cps	#MODE32_mon
278f5478dedSAntonio Nino Diaz	isb
279f5478dedSAntonio Nino Diaz
2800063dd17SJavier Almansa Sobrino#if DISABLE_MTPMU
2810063dd17SJavier Almansa Sobrino	bl	mtpmu_disable
2820063dd17SJavier Almansa Sobrino#endif
2830063dd17SJavier Almansa Sobrino
284f5478dedSAntonio Nino Diaz	.if \_warm_boot_mailbox
285f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
286f5478dedSAntonio Nino Diaz		 * This code will be executed for both warm and cold resets.
287f5478dedSAntonio Nino Diaz		 * Now is the time to distinguish between the two.
288f5478dedSAntonio Nino Diaz		 * Query the platform entrypoint address and if it is not zero
289f5478dedSAntonio Nino Diaz		 * then it means it is a warm boot so jump to this address.
290f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
291f5478dedSAntonio Nino Diaz		 */
292f5478dedSAntonio Nino Diaz		bl	plat_get_my_entrypoint
293f5478dedSAntonio Nino Diaz		cmp	r0, #0
294f5478dedSAntonio Nino Diaz		bxne	r0
295f5478dedSAntonio Nino Diaz	.endif /* _warm_boot_mailbox */
296f5478dedSAntonio Nino Diaz
2974324a14bSYann Gautier	.if \_pie_fixup_size
2984324a14bSYann Gautier#if ENABLE_PIE
2994324a14bSYann Gautier		/*
3004324a14bSYann Gautier		 * ------------------------------------------------------------
3014324a14bSYann Gautier		 * If PIE is enabled fixup the Global descriptor Table only
3024324a14bSYann Gautier		 * once during primary core cold boot path.
3034324a14bSYann Gautier		 *
3044324a14bSYann Gautier		 * Compile time base address, required for fixup, is calculated
3054324a14bSYann Gautier		 * using "pie_fixup" label present within first page.
3064324a14bSYann Gautier		 * ------------------------------------------------------------
3074324a14bSYann Gautier		 */
3084324a14bSYann Gautier	pie_fixup:
3094324a14bSYann Gautier		ldr	r0, =pie_fixup
3104324a14bSYann Gautier		ldr	r1, =PAGE_START_MASK
3114324a14bSYann Gautier		and	r0, r0, r1
3124324a14bSYann Gautier		mov_imm	r1, \_pie_fixup_size
3134324a14bSYann Gautier		add	r1, r1, r0
3144324a14bSYann Gautier		bl	fixup_gdt_reloc
3154324a14bSYann Gautier#endif /* ENABLE_PIE */
3164324a14bSYann Gautier	.endif /* _pie_fixup_size */
3174324a14bSYann Gautier
318f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
319f5478dedSAntonio Nino Diaz	 * Set the exception vectors (VBAR/MVBAR).
320f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
321f5478dedSAntonio Nino Diaz	 */
322f5478dedSAntonio Nino Diaz	ldr	r0, =\_exception_vectors
323f5478dedSAntonio Nino Diaz	stcopr	r0, VBAR
324f5478dedSAntonio Nino Diaz	stcopr	r0, MVBAR
325f5478dedSAntonio Nino Diaz	isb
326f5478dedSAntonio Nino Diaz
327f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
328f5478dedSAntonio Nino Diaz	 * It is a cold boot.
329f5478dedSAntonio Nino Diaz	 * Perform any processor specific actions upon reset e.g. cache, TLB
330f5478dedSAntonio Nino Diaz	 * invalidations etc.
331f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
332f5478dedSAntonio Nino Diaz	 */
333f5478dedSAntonio Nino Diaz	bl	reset_handler
334f5478dedSAntonio Nino Diaz
335f5478dedSAntonio Nino Diaz	el3_arch_init_common
336f5478dedSAntonio Nino Diaz
337f5478dedSAntonio Nino Diaz	.if \_secondary_cold_boot
338f5478dedSAntonio Nino Diaz		/* -------------------------------------------------------------
339f5478dedSAntonio Nino Diaz		 * Check if this is a primary or secondary CPU cold boot.
340f5478dedSAntonio Nino Diaz		 * The primary CPU will set up the platform while the
341f5478dedSAntonio Nino Diaz		 * secondaries are placed in a platform-specific state until the
342f5478dedSAntonio Nino Diaz		 * primary CPU performs the necessary actions to bring them out
343f5478dedSAntonio Nino Diaz		 * of that state and allows entry into the OS.
344f5478dedSAntonio Nino Diaz		 * -------------------------------------------------------------
345f5478dedSAntonio Nino Diaz		 */
346f5478dedSAntonio Nino Diaz		bl	plat_is_my_cpu_primary
347f5478dedSAntonio Nino Diaz		cmp	r0, #0
348f5478dedSAntonio Nino Diaz		bne	do_primary_cold_boot
349f5478dedSAntonio Nino Diaz
350f5478dedSAntonio Nino Diaz		/* This is a cold boot on a secondary CPU */
351f5478dedSAntonio Nino Diaz		bl	plat_secondary_cold_boot_setup
352f5478dedSAntonio Nino Diaz		/* plat_secondary_cold_boot_setup() is not supposed to return */
353f5478dedSAntonio Nino Diaz		no_ret	plat_panic_handler
354f5478dedSAntonio Nino Diaz
355f5478dedSAntonio Nino Diaz	do_primary_cold_boot:
356f5478dedSAntonio Nino Diaz	.endif /* _secondary_cold_boot */
357f5478dedSAntonio Nino Diaz
358f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
359f5478dedSAntonio Nino Diaz	 * Initialize memory now. Secondary CPU initialization won't get to this
360f5478dedSAntonio Nino Diaz	 * point.
361f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
362f5478dedSAntonio Nino Diaz	 */
363f5478dedSAntonio Nino Diaz
364f5478dedSAntonio Nino Diaz	.if \_init_memory
365f5478dedSAntonio Nino Diaz		bl	platform_mem_init
366f5478dedSAntonio Nino Diaz	.endif /* _init_memory */
367f5478dedSAntonio Nino Diaz
368f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
369f5478dedSAntonio Nino Diaz	 * Init C runtime environment:
370f5478dedSAntonio Nino Diaz	 *   - Zero-initialise the NOBITS sections. There are 2 of them:
371f5478dedSAntonio Nino Diaz	 *       - the .bss section;
372f5478dedSAntonio Nino Diaz	 *       - the coherent memory section (if any).
373f5478dedSAntonio Nino Diaz	 *   - Relocate the data section from ROM to RAM, if required.
374f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
375f5478dedSAntonio Nino Diaz	 */
376f5478dedSAntonio Nino Diaz	.if \_init_c_runtime
377f5478dedSAntonio Nino Diaz#if defined(IMAGE_BL32) || (defined(IMAGE_BL2) && BL2_AT_EL3)
378f5478dedSAntonio Nino Diaz		/* -----------------------------------------------------------------
379f5478dedSAntonio Nino Diaz		 * Invalidate the RW memory used by the image. This
380f5478dedSAntonio Nino Diaz		 * includes the data and NOBITS sections. This is done to
381f5478dedSAntonio Nino Diaz		 * safeguard against possible corruption of this memory by
382f5478dedSAntonio Nino Diaz		 * dirty cache lines in a system cache as a result of use by
383596d20d9SZelalem Aweke		 * an earlier boot loader stage. If PIE is enabled however,
384596d20d9SZelalem Aweke		 * RO sections including the GOT may be modified during
385596d20d9SZelalem Aweke		 * pie fixup. Therefore, to be on the safe side, invalidate
386596d20d9SZelalem Aweke		 * the entire image region if PIE is enabled.
387f5478dedSAntonio Nino Diaz		 * -----------------------------------------------------------------
388f5478dedSAntonio Nino Diaz		 */
389596d20d9SZelalem Aweke#if ENABLE_PIE
390596d20d9SZelalem Aweke#if SEPARATE_CODE_AND_RODATA
391596d20d9SZelalem Aweke		ldr	r0, =__TEXT_START__
392596d20d9SZelalem Aweke#else
393596d20d9SZelalem Aweke		ldr	r0, =__RO_START__
394596d20d9SZelalem Aweke#endif /* SEPARATE_CODE_AND_RODATA */
395596d20d9SZelalem Aweke#else
396f5478dedSAntonio Nino Diaz		ldr	r0, =__RW_START__
397596d20d9SZelalem Aweke#endif /* ENABLE_PIE */
398f5478dedSAntonio Nino Diaz		ldr	r1, =__RW_END__
399f5478dedSAntonio Nino Diaz		sub	r1, r1, r0
400f5478dedSAntonio Nino Diaz		bl	inv_dcache_range
401*96a8ed14SJiafei Pan#if defined(IMAGE_BL2) && SEPARATE_BL2_NOLOAD_REGION
402*96a8ed14SJiafei Pan		ldr	r0, =__BL2_NOLOAD_START__
403*96a8ed14SJiafei Pan		ldr	r1, =__BL2_NOLOAD_END__
404*96a8ed14SJiafei Pan		sub	r1, r1, r0
405*96a8ed14SJiafei Pan		bl	inv_dcache_range
406*96a8ed14SJiafei Pan#endif
407f5478dedSAntonio Nino Diaz#endif
408f5478dedSAntonio Nino Diaz
40930f31005SYann Gautier		/*
41030f31005SYann Gautier		 * zeromem uses r12 whereas it is used to save previous BL arg3,
41130f31005SYann Gautier		 * save it in r7
41230f31005SYann Gautier		 */
41330f31005SYann Gautier		mov	r7, r12
414f5478dedSAntonio Nino Diaz		ldr	r0, =__BSS_START__
415fb4f511fSYann Gautier		ldr	r1, =__BSS_END__
416fb4f511fSYann Gautier		sub 	r1, r1, r0
417f5478dedSAntonio Nino Diaz		bl	zeromem
418f5478dedSAntonio Nino Diaz
419f5478dedSAntonio Nino Diaz#if USE_COHERENT_MEM
420f5478dedSAntonio Nino Diaz		ldr	r0, =__COHERENT_RAM_START__
421fb4f511fSYann Gautier		ldr	r1, =__COHERENT_RAM_END_UNALIGNED__
422fb4f511fSYann Gautier		sub 	r1, r1, r0
423f5478dedSAntonio Nino Diaz		bl	zeromem
424f5478dedSAntonio Nino Diaz#endif
425f5478dedSAntonio Nino Diaz
42630f31005SYann Gautier		/* Restore r12 */
42730f31005SYann Gautier		mov	r12, r7
42830f31005SYann Gautier
4290a12302cSLionel Debieve#if defined(IMAGE_BL1) || (defined(IMAGE_BL2) && BL2_AT_EL3 && BL2_IN_XIP_MEM)
430f5478dedSAntonio Nino Diaz		/* -----------------------------------------------------
431f5478dedSAntonio Nino Diaz		 * Copy data from ROM to RAM.
432f5478dedSAntonio Nino Diaz		 * -----------------------------------------------------
433f5478dedSAntonio Nino Diaz		 */
434f5478dedSAntonio Nino Diaz		ldr	r0, =__DATA_RAM_START__
435f5478dedSAntonio Nino Diaz		ldr	r1, =__DATA_ROM_START__
436fb4f511fSYann Gautier		ldr	r2, =__DATA_RAM_END__
437fb4f511fSYann Gautier		sub 	r2, r2, r0
438f5478dedSAntonio Nino Diaz		bl	memcpy4
439f5478dedSAntonio Nino Diaz#endif
440f5478dedSAntonio Nino Diaz	.endif /* _init_c_runtime */
441f5478dedSAntonio Nino Diaz
442f5478dedSAntonio Nino Diaz	/* ---------------------------------------------------------------------
443f5478dedSAntonio Nino Diaz	 * Allocate a stack whose memory will be marked as Normal-IS-WBWA when
444f5478dedSAntonio Nino Diaz	 * the MMU is enabled. There is no risk of reading stale stack memory
445f5478dedSAntonio Nino Diaz	 * after enabling the MMU as only the primary CPU is running at the
446f5478dedSAntonio Nino Diaz	 * moment.
447f5478dedSAntonio Nino Diaz	 * ---------------------------------------------------------------------
448f5478dedSAntonio Nino Diaz	 */
449f5478dedSAntonio Nino Diaz	bl	plat_set_my_stack
450f5478dedSAntonio Nino Diaz
451f5478dedSAntonio Nino Diaz#if STACK_PROTECTOR_ENABLED
452f5478dedSAntonio Nino Diaz	.if \_init_c_runtime
453f5478dedSAntonio Nino Diaz	bl	update_stack_protector_canary
454f5478dedSAntonio Nino Diaz	.endif /* _init_c_runtime */
455f5478dedSAntonio Nino Diaz#endif
456f5478dedSAntonio Nino Diaz	.endm
457f5478dedSAntonio Nino Diaz
458f5478dedSAntonio Nino Diaz#endif /* EL3_COMMON_MACROS_S */
459