blob: c57a1ec3c524b930e54f08fc81180345125c2f74 [file] [log] [blame]
Gary Morrison3d7f6542021-01-27 13:08:47 -06001/*
2 * Copyright (c) 2015-2021, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef EL2_COMMON_MACROS_S
8#define EL2_COMMON_MACROS_S
9
10#include <arch.h>
11#include <asm_macros.S>
12#include <context.h>
13#include <lib/xlat_tables/xlat_tables_defs.h>
14
15#include <platform_def.h>
16
17 /*
18 * Helper macro to initialise system registers at EL2.
19 */
20 .macro el2_arch_init_common
21
22 /* ---------------------------------------------------------------------
23 * SCTLR_EL2 has already been initialised - read current value before
24 * modifying.
25 *
26 * SCTLR_EL2.I: Enable the instruction cache.
27 *
28 * SCTLR_EL2.SA: Enable Stack Alignment check. A SP alignment fault
29 * exception is generated if a load or store instruction executed at
30 * EL2 uses the SP as the base address and the SP is not aligned to a
31 * 16-byte boundary.
32 *
33 * SCTLR_EL2.A: Enable Alignment fault checking. All instructions that
34 * load or store one or more registers have an alignment check that the
35 * address being accessed is aligned to the size of the data element(s)
36 * being accessed.
37 * ---------------------------------------------------------------------
38 */
39 mov x1, #(SCTLR_I_BIT | SCTLR_A_BIT | SCTLR_SA_BIT)
40 mrs x0, sctlr_el2
41 orr x0, x0, x1
42 msr sctlr_el2, x0
43 isb
44
45 /* ---------------------------------------------------------------------
46 * Initialise HCR_EL2, setting all fields rather than relying on HW.
47 * All fields are architecturally UNKNOWN on reset. The following fields
48 * do not change during the TF lifetime. The remaining fields are set to
49 * zero here but are updated ahead of transitioning to a lower EL in the
50 * function cm_init_context_common().
51 *
52 * HCR_EL2.TWE: Set to zero so that execution of WFE instructions at
53 * EL2, EL1 and EL0 are not trapped to EL2.
54 *
55 * HCR_EL2.TWI: Set to zero so that execution of WFI instructions at
56 * EL2, EL1 and EL0 are not trapped to EL2.
57 *
58 * HCR_EL2.HCD: Set to zero to enable HVC calls at EL1 and above,
59 * from both Security states and both Execution states.
60 *
61 * HCR_EL2.TEA: Set to one to route External Aborts and SError
62 * Interrupts to EL2 when executing at any EL.
63 *
64 * HCR_EL2.{API,APK}: For Armv8.3 pointer authentication feature,
65 * disable traps to EL2 when accessing key registers or using
66 * pointer authentication instructions from lower ELs.
67 * ---------------------------------------------------------------------
68 */
69 mov_imm x0, ((HCR_RESET_VAL | HCR_TEA_BIT) \
70 & ~(HCR_TWE_BIT | HCR_TWI_BIT | HCR_HCD_BIT))
71#if CTX_INCLUDE_PAUTH_REGS
72 /*
73 * If the pointer authentication registers are saved during world
74 * switches, enable pointer authentication everywhere, as it is safe to
75 * do so.
76 */
77 orr x0, x0, #(HCR_API_BIT | HCR_APK_BIT)
78#endif /* CTX_INCLUDE_PAUTH_REGS */
79 msr hcr_el2, x0
80
81 /* ---------------------------------------------------------------------
82 * Initialise MDCR_EL2, setting all fields rather than relying on
83 * hw. Some fields are architecturally UNKNOWN on reset.
84 *
85 * MDCR_EL2.SDD: Set to one to disable AArch64 Secure self-hosted
86 * debug. Debug exceptions, other than Breakpoint Instruction
87 * exceptions, are disabled from all ELs in Secure state.
88 *
89 * MDCR_EL2.TDOSA: Set to zero so that EL2 and EL2 System register
90 * access to the powerdown debug registers do not trap to EL2.
91 *
92 * MDCR_EL2.TDA: Set to zero to allow EL0, EL1 and EL2 access to the
93 * debug registers, other than those registers that are controlled by
94 * MDCR_EL2.TDOSA.
95 *
96 * MDCR_EL2.TPM: Set to zero so that EL0, EL1, and EL2 System
97 * register accesses to all Performance Monitors registers do not trap
98 * to EL2.
99 *
100 * MDCR_EL2.SCCD: Set to one so that cycle counting by PMCCNTR_EL0
101 * is prohibited in Secure state. This bit is RES0 in versions of the
102 * architecture with FEAT_PMUv3p5 not implemented, setting it to 1
103 * doesn't have any effect on them.
104 *
105 * MDCR_EL2.MCCD: Set to one so that cycle counting by PMCCNTR_EL0
106 * is prohibited in EL2. This bit is RES0 in versions of the
107 * architecture with FEAT_PMUv3p7 not implemented, setting it to 1
108 * doesn't have any effect on them.
109 *
110 * MDCR_EL2.SPME: Set to zero so that event counting by the program-
111 * mable counters PMEVCNTR<n>_EL0 is prohibited in Secure state. If
112 * ARMv8.2 Debug is not implemented this bit does not have any effect
113 * on the counters unless there is support for the implementation
114 * defined authentication interface
115 * ExternalSecureNoninvasiveDebugEnabled().
116 * ---------------------------------------------------------------------
117 */
118 mov_imm x0, ((MDCR_EL2_RESET_VAL | MDCR_SDD_BIT | \
119 MDCR_SPD32(MDCR_SPD32_DISABLE) | MDCR_SCCD_BIT | \
120 MDCR_MCCD_BIT) & ~(MDCR_SPME_BIT | MDCR_TDOSA_BIT | \
121 MDCR_TDA_BIT | MDCR_TPM_BIT))
122
123 msr mdcr_el2, x0
124
125 /* ---------------------------------------------------------------------
126 * Initialise PMCR_EL0 setting all fields rather than relying
127 * on hw. Some fields are architecturally UNKNOWN on reset.
128 *
129 * PMCR_EL0.LP: Set to one so that event counter overflow, that
130 * is recorded in PMOVSCLR_EL0[0-30], occurs on the increment
131 * that changes PMEVCNTR<n>_EL0[63] from 1 to 0, when ARMv8.5-PMU
132 * is implemented. This bit is RES0 in versions of the architecture
133 * earlier than ARMv8.5, setting it to 1 doesn't have any effect
134 * on them.
135 *
136 * PMCR_EL0.LC: Set to one so that cycle counter overflow, that
137 * is recorded in PMOVSCLR_EL0[31], occurs on the increment
138 * that changes PMCCNTR_EL0[63] from 1 to 0.
139 *
140 * PMCR_EL0.DP: Set to one so that the cycle counter,
141 * PMCCNTR_EL0 does not count when event counting is prohibited.
142 *
143 * PMCR_EL0.X: Set to zero to disable export of events.
144 *
145 * PMCR_EL0.D: Set to zero so that, when enabled, PMCCNTR_EL0
146 * counts on every clock cycle.
147 * ---------------------------------------------------------------------
148 */
149 mov_imm x0, ((PMCR_EL0_RESET_VAL | PMCR_EL0_LP_BIT | \
150 PMCR_EL0_LC_BIT | PMCR_EL0_DP_BIT) & \
151 ~(PMCR_EL0_X_BIT | PMCR_EL0_D_BIT))
152
153 msr pmcr_el0, x0
154
155 /* ---------------------------------------------------------------------
156 * Enable External Aborts and SError Interrupts now that the exception
157 * vectors have been setup.
158 * ---------------------------------------------------------------------
159 */
160 msr daifclr, #DAIF_ABT_BIT
161
162 /* ---------------------------------------------------------------------
163 * Initialise CPTR_EL2, setting all fields rather than relying on hw.
164 * All fields are architecturally UNKNOWN on reset.
165 *
166 * CPTR_EL2.TCPAC: Set to zero so that any accesses to CPACR_EL1 do
167 * not trap to EL2.
168 *
169 * CPTR_EL2.TTA: Set to zero so that System register accesses to the
170 * trace registers do not trap to EL2.
171 *
172 * CPTR_EL2.TFP: Set to zero so that accesses to the V- or Z- registers
173 * by Advanced SIMD, floating-point or SVE instructions (if implemented)
174 * do not trap to EL2.
175 */
176
177 mov_imm x0, (CPTR_EL2_RESET_VAL & ~(TCPAC_BIT | TTA_BIT | TFP_BIT))
178 msr cptr_el2, x0
179
180 /*
181 * If Data Independent Timing (DIT) functionality is implemented,
182 * always enable DIT in EL2
183 */
184 mrs x0, id_aa64pfr0_el1
185 ubfx x0, x0, #ID_AA64PFR0_DIT_SHIFT, #ID_AA64PFR0_DIT_LENGTH
186 cmp x0, #ID_AA64PFR0_DIT_SUPPORTED
187 bne 1f
188 mov x0, #DIT_BIT
189 msr DIT, x0
1901:
191 .endm
192
193/* -----------------------------------------------------------------------------
194 * This is the super set of actions that need to be performed during a cold boot
195 * or a warm boot in EL2. This code is shared by BL1 and BL31.
196 *
197 * This macro will always perform reset handling, architectural initialisations
198 * and stack setup. The rest of the actions are optional because they might not
199 * be needed, depending on the context in which this macro is called. This is
200 * why this macro is parameterised ; each parameter allows to enable/disable
201 * some actions.
202 *
203 * _init_sctlr:
204 * Whether the macro needs to initialise SCTLR_EL2, including configuring
205 * the endianness of data accesses.
206 *
207 * _warm_boot_mailbox:
208 * Whether the macro needs to detect the type of boot (cold/warm). The
209 * detection is based on the platform entrypoint address : if it is zero
210 * then it is a cold boot, otherwise it is a warm boot. In the latter case,
211 * this macro jumps on the platform entrypoint address.
212 *
213 * _secondary_cold_boot:
214 * Whether the macro needs to identify the CPU that is calling it: primary
215 * CPU or secondary CPU. The primary CPU will be allowed to carry on with
216 * the platform initialisations, while the secondaries will be put in a
217 * platform-specific state in the meantime.
218 *
219 * If the caller knows this macro will only be called by the primary CPU
220 * then this parameter can be defined to 0 to skip this step.
221 *
222 * _init_memory:
223 * Whether the macro needs to initialise the memory.
224 *
225 * _init_c_runtime:
226 * Whether the macro needs to initialise the C runtime environment.
227 *
228 * _exception_vectors:
229 * Address of the exception vectors to program in the VBAR_EL2 register.
230 *
231 * _pie_fixup_size:
232 * Size of memory region to fixup Global Descriptor Table (GDT).
233 *
234 * A non-zero value is expected when firmware needs GDT to be fixed-up.
235 *
236 * -----------------------------------------------------------------------------
237 */
238 .macro el2_entrypoint_common \
239 _init_sctlr, _warm_boot_mailbox, _secondary_cold_boot, \
240 _init_memory, _init_c_runtime, _exception_vectors, \
241 _pie_fixup_size
242
243 .if \_init_sctlr
244 /* -------------------------------------------------------------
245 * This is the initialisation of SCTLR_EL2 and so must ensure
246 * that all fields are explicitly set rather than relying on hw.
247 * Some fields reset to an IMPLEMENTATION DEFINED value and
248 * others are architecturally UNKNOWN on reset.
249 *
250 * SCTLR.EE: Set the CPU endianness before doing anything that
251 * might involve memory reads or writes. Set to zero to select
252 * Little Endian.
253 *
254 * SCTLR_EL2.WXN: For the EL2 translation regime, this field can
255 * force all memory regions that are writeable to be treated as
256 * XN (Execute-never). Set to zero so that this control has no
257 * effect on memory access permissions.
258 *
259 * SCTLR_EL2.SA: Set to zero to disable Stack Alignment check.
260 *
261 * SCTLR_EL2.A: Set to zero to disable Alignment fault checking.
262 *
263 * SCTLR.DSSBS: Set to zero to disable speculation store bypass
264 * safe behaviour upon exception entry to EL2.
265 * -------------------------------------------------------------
266 */
267 mov_imm x0, (SCTLR_RESET_VAL & ~(SCTLR_EE_BIT | SCTLR_WXN_BIT \
268 | SCTLR_SA_BIT | SCTLR_A_BIT | SCTLR_DSSBS_BIT))
269 msr sctlr_el2, x0
270 isb
271 .endif /* _init_sctlr */
272
273#if DISABLE_MTPMU
274 bl mtpmu_disable
275#endif
276
277 .if \_warm_boot_mailbox
278 /* -------------------------------------------------------------
279 * This code will be executed for both warm and cold resets.
280 * Now is the time to distinguish between the two.
281 * Query the platform entrypoint address and if it is not zero
282 * then it means it is a warm boot so jump to this address.
283 * -------------------------------------------------------------
284 */
285 bl plat_get_my_entrypoint
286 cbz x0, do_cold_boot
287 br x0
288
289 do_cold_boot:
290 .endif /* _warm_boot_mailbox */
291
292 .if \_pie_fixup_size
293#if ENABLE_PIE
294 /*
295 * ------------------------------------------------------------
296 * If PIE is enabled fixup the Global descriptor Table only
297 * once during primary core cold boot path.
298 *
299 * Compile time base address, required for fixup, is calculated
300 * using "pie_fixup" label present within first page.
301 * ------------------------------------------------------------
302 */
303 pie_fixup:
304 ldr x0, =pie_fixup
305 and x0, x0, #~(PAGE_SIZE_MASK)
306 mov_imm x1, \_pie_fixup_size
307 add x1, x1, x0
308 bl fixup_gdt_reloc
309#endif /* ENABLE_PIE */
310 .endif /* _pie_fixup_size */
311
312 /* ---------------------------------------------------------------------
313 * Set the exception vectors.
314 * ---------------------------------------------------------------------
315 */
316 adr x0, \_exception_vectors
317 msr vbar_el2, x0
318 isb
319
320 /* ---------------------------------------------------------------------
321 * It is a cold boot.
322 * Perform any processor specific actions upon reset e.g. cache, TLB
323 * invalidations etc.
324 * ---------------------------------------------------------------------
325 */
326 bl reset_handler
327
328 el2_arch_init_common
329
330 .if \_secondary_cold_boot
331 /* -------------------------------------------------------------
332 * Check if this is a primary or secondary CPU cold boot.
333 * The primary CPU will set up the platform while the
334 * secondaries are placed in a platform-specific state until the
335 * primary CPU performs the necessary actions to bring them out
336 * of that state and allows entry into the OS.
337 * -------------------------------------------------------------
338 */
339 bl plat_is_my_cpu_primary
340 cbnz w0, do_primary_cold_boot
341
342 /* This is a cold boot on a secondary CPU */
343 bl plat_secondary_cold_boot_setup
344 /* plat_secondary_cold_boot_setup() is not supposed to return */
345 bl el2_panic
346 do_primary_cold_boot:
347 .endif /* _secondary_cold_boot */
348
349 /* ---------------------------------------------------------------------
350 * Initialize memory now. Secondary CPU initialization won't get to this
351 * point.
352 * ---------------------------------------------------------------------
353 */
354
355 .if \_init_memory
356 bl platform_mem_init
357 .endif /* _init_memory */
358
359 /* ---------------------------------------------------------------------
360 * Init C runtime environment:
361 * - Zero-initialise the NOBITS sections. There are 2 of them:
362 * - the .bss section;
363 * - the coherent memory section (if any).
364 * - Relocate the data section from ROM to RAM, if required.
365 * ---------------------------------------------------------------------
366 */
367 .if \_init_c_runtime
368 adrp x0, __BSS_START__
369 add x0, x0, :lo12:__BSS_START__
370
371 adrp x1, __BSS_END__
372 add x1, x1, :lo12:__BSS_END__
373 sub x1, x1, x0
374 bl zeromem
375
376#if defined(IMAGE_BL1) || (defined(IMAGE_BL2) && BL2_AT_EL3 && BL2_IN_XIP_MEM)
377 adrp x0, __DATA_RAM_START__
378 add x0, x0, :lo12:__DATA_RAM_START__
379 adrp x1, __DATA_ROM_START__
380 add x1, x1, :lo12:__DATA_ROM_START__
381 adrp x2, __DATA_RAM_END__
382 add x2, x2, :lo12:__DATA_RAM_END__
383 sub x2, x2, x0
384 bl memcpy16
385#endif
386 .endif /* _init_c_runtime */
387
388 /* ---------------------------------------------------------------------
389 * Use SP_EL0 for the C runtime stack.
390 * ---------------------------------------------------------------------
391 */
392 msr spsel, #0
393
394 /* ---------------------------------------------------------------------
395 * Allocate a stack whose memory will be marked as Normal-IS-WBWA when
396 * the MMU is enabled. There is no risk of reading stale stack memory
397 * after enabling the MMU as only the primary CPU is running at the
398 * moment.
399 * ---------------------------------------------------------------------
400 */
401 bl plat_set_my_stack
402
403#if STACK_PROTECTOR_ENABLED
404 .if \_init_c_runtime
405 bl update_stack_protector_canary
406 .endif /* _init_c_runtime */
407#endif
408 .endm
409
410 .macro apply_at_speculative_wa
411#if ERRATA_SPECULATIVE_AT
412 /*
413 * Explicitly save x30 so as to free up a register and to enable
414 * branching and also, save x29 which will be used in the called
415 * function
416 */
417 stp x29, x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
418 bl save_and_update_ptw_el1_sys_regs
419 ldp x29, x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
420#endif
421 .endm
422
423 .macro restore_ptw_el1_sys_regs
424#if ERRATA_SPECULATIVE_AT
425 /* -----------------------------------------------------------
426 * In case of ERRATA_SPECULATIVE_AT, must follow below order
427 * to ensure that page table walk is not enabled until
428 * restoration of all EL1 system registers. TCR_EL1 register
429 * should be updated at the end which restores previous page
430 * table walk setting of stage1 i.e.(TCR_EL1.EPDx) bits. ISB
431 * ensures that CPU does below steps in order.
432 *
433 * 1. Ensure all other system registers are written before
434 * updating SCTLR_EL1 using ISB.
435 * 2. Restore SCTLR_EL1 register.
436 * 3. Ensure SCTLR_EL1 written successfully using ISB.
437 * 4. Restore TCR_EL1 register.
438 * -----------------------------------------------------------
439 */
440 isb
441 ldp x28, x29, [sp, #CTX_EL1_SYSREGS_OFFSET + CTX_SCTLR_EL1]
442 msr sctlr_el1, x28
443 isb
444 msr tcr_el1, x29
445#endif
446 .endm
447
448#endif /* EL2_COMMON_MACROS_S */