blob: 07dffb185172043871171ec7e36a27d140ef9c41 [file] [log] [blame]
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +01001/*
Boyan Karatotev1dcba8f2024-11-19 11:27:01 +00002 * Copyright (c) 2015-2025, Arm Limited and Contributors. All rights reserved.
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +01005 */
6
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +00007#ifndef EL3_COMMON_MACROS_S
8#define EL3_COMMON_MACROS_S
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +01009
10#include <arch.h>
11#include <asm_macros.S>
Daniel Boulby928747f2021-05-25 18:09:34 +010012#include <assert_macros.S>
Manish V Badarkhee07e8082020-07-23 12:43:25 +010013#include <context.h>
Varun Wadekar5dc9e9c2020-05-16 20:59:30 -070014#include <lib/xlat_tables/xlat_tables_defs.h>
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +010015
16 /*
17 * Helper macro to initialise EL3 registers we care about.
18 */
Dimitris Papastamos446f7f12017-11-30 14:53:53 +000019 .macro el3_arch_init_common
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +010020 /* ---------------------------------------------------------------------
David Cunadofee86532017-04-13 22:38:29 +010021 * SCTLR_EL3 has already been initialised - read current value before
22 * modifying.
23 *
24 * SCTLR_EL3.I: Enable the instruction cache.
25 *
Qixiang Xu3cc39172018-03-05 09:31:11 +080026 * SCTLR_EL3.SA: Enable Stack Alignment check. A SP alignment fault
David Cunadofee86532017-04-13 22:38:29 +010027 * exception is generated if a load or store instruction executed at
28 * EL3 uses the SP as the base address and the SP is not aligned to a
29 * 16-byte boundary.
30 *
31 * SCTLR_EL3.A: Enable Alignment fault checking. All instructions that
32 * load or store one or more registers have an alignment check that the
33 * address being accessed is aligned to the size of the data element(s)
34 * being accessed.
Boyan Karatotev7a5246e2025-03-26 15:54:55 +000035 *
36 * SCTLR_EL3.BT: PAuth instructions are compatible with bti jc
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +010037 * ---------------------------------------------------------------------
38 */
Boyan Karatotev7a5246e2025-03-26 15:54:55 +000039 mov_imm x1, (SCTLR_I_BIT | SCTLR_A_BIT | SCTLR_SA_BIT)
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +010040 mrs x0, sctlr_el3
Boyan Karatotev7a5246e2025-03-26 15:54:55 +000041#if ENABLE_BTI
42 bic x0, x0, #SCTLR_BT_BIT
43#endif
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +010044 orr x0, x0, x1
45 msr sctlr_el3, x0
46 isb
47
Masahiro Yamada441bfdd2016-12-25 23:36:24 +090048#ifdef IMAGE_BL31
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +010049 /* ---------------------------------------------------------------------
50 * Initialise the per-cpu cache pointer to the CPU.
51 * This is done early to enable crash reporting to have access to crash
52 * stack. Since crash reporting depends on cpu_data to report the
53 * unhandled exception, not doing so can lead to recursive exceptions
54 * due to a NULL TPIDR_EL3.
55 * ---------------------------------------------------------------------
56 */
Boyan Karatotev97476aa2024-11-19 11:27:01 +000057 bl plat_my_core_pos
58 bl _cpu_data_by_index
59 msr tpidr_el3, x0
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +010060#endif /* IMAGE_BL31 */
61
62 /* ---------------------------------------------------------------------
David Cunadofee86532017-04-13 22:38:29 +010063 * Initialise SCR_EL3, setting all fields rather than relying on hw.
64 * All fields are architecturally UNKNOWN on reset. The following fields
65 * do not change during the TF lifetime. The remaining fields are set to
66 * zero here but are updated ahead of transitioning to a lower EL in the
67 * function cm_init_context_common().
68 *
Manish Pandey71af7f12024-01-29 21:17:33 +000069 * SCR_EL3.EEL2: Set to one if S-EL2 is present and enabled.
70 *
71 * NOTE: Modifying EEL2 bit along with EA bit ensures that we mitigate
72 * against ERRATA_V2_3099206.
Gerald Lejeune632d6df2016-03-22 09:29:23 +010073 * ---------------------------------------------------------------------
74 */
Jayanth Dodderi Chidanandb4590652023-08-08 16:10:16 +010075 mov_imm x0, SCR_RESET_VAL
Manish Pandey71af7f12024-01-29 21:17:33 +000076#if IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2
Jayanth Dodderi Chidanandb4590652023-08-08 16:10:16 +010077 mrs x1, id_aa64pfr0_el1
78 and x1, x1, #(ID_AA64PFR0_SEL2_MASK << ID_AA64PFR0_SEL2_SHIFT)
79 cbz x1, 1f
80 orr x0, x0, #SCR_EEL2_BIT
Manish Pandey71af7f12024-01-29 21:17:33 +000081#endif
821:
Gerald Lejeune632d6df2016-03-22 09:29:23 +010083 msr scr_el3, x0
David Cunado5f55e282016-10-31 17:37:34 +000084
85 /* ---------------------------------------------------------------------
David Cunadofee86532017-04-13 22:38:29 +010086 * Initialise MDCR_EL3, setting all fields rather than relying on hw.
87 * Some fields are architecturally UNKNOWN on reset.
David Cunado5f55e282016-10-31 17:37:34 +000088 */
Jayanth Dodderi Chidanandb4590652023-08-08 16:10:16 +010089 mov_imm x0, MDCR_EL3_RESET_VAL
dp-arm595d0d52017-02-08 11:51:50 +000090 msr mdcr_el3, x0
David Cunado5f55e282016-10-31 17:37:34 +000091
Gerald Lejeune632d6df2016-03-22 09:29:23 +010092 /* ---------------------------------------------------------------------
David Cunadofee86532017-04-13 22:38:29 +010093 * Initialise CPTR_EL3, setting all fields rather than relying on hw.
94 * All fields are architecturally UNKNOWN on reset.
Boyan Karatotev8ae58f02023-04-20 11:00:50 +010095 * ---------------------------------------------------------------------
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +010096 */
Boyan Karatotev8ae58f02023-04-20 11:00:50 +010097 mov_imm x0, CPTR_EL3_RESET_VAL
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +010098 msr cptr_el3, x0
Sathees Balya0911df12018-12-06 13:33:24 +000099
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100100 .endm
101
102/* -----------------------------------------------------------------------------
103 * This is the super set of actions that need to be performed during a cold boot
Juan Castillo7d199412015-12-14 09:35:25 +0000104 * or a warm boot in EL3. This code is shared by BL1 and BL31.
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100105 *
106 * This macro will always perform reset handling, architectural initialisations
107 * and stack setup. The rest of the actions are optional because they might not
108 * be needed, depending on the context in which this macro is called. This is
109 * why this macro is parameterised ; each parameter allows to enable/disable
110 * some actions.
111 *
David Cunadofee86532017-04-13 22:38:29 +0100112 * _init_sctlr:
113 * Whether the macro needs to initialise SCTLR_EL3, including configuring
114 * the endianness of data accesses.
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100115 *
116 * _warm_boot_mailbox:
117 * Whether the macro needs to detect the type of boot (cold/warm). The
118 * detection is based on the platform entrypoint address : if it is zero
119 * then it is a cold boot, otherwise it is a warm boot. In the latter case,
120 * this macro jumps on the platform entrypoint address.
121 *
122 * _secondary_cold_boot:
123 * Whether the macro needs to identify the CPU that is calling it: primary
124 * CPU or secondary CPU. The primary CPU will be allowed to carry on with
125 * the platform initialisations, while the secondaries will be put in a
126 * platform-specific state in the meantime.
127 *
128 * If the caller knows this macro will only be called by the primary CPU
129 * then this parameter can be defined to 0 to skip this step.
130 *
131 * _init_memory:
132 * Whether the macro needs to initialise the memory.
133 *
134 * _init_c_runtime:
135 * Whether the macro needs to initialise the C runtime environment.
136 *
137 * _exception_vectors:
138 * Address of the exception vectors to program in the VBAR_EL3 register.
Manish Pandeyc8257682019-11-26 11:34:17 +0000139 *
140 * _pie_fixup_size:
141 * Size of memory region to fixup Global Descriptor Table (GDT).
142 *
143 * A non-zero value is expected when firmware needs GDT to be fixed-up.
144 *
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100145 * -----------------------------------------------------------------------------
146 */
147 .macro el3_entrypoint_common \
David Cunadofee86532017-04-13 22:38:29 +0100148 _init_sctlr, _warm_boot_mailbox, _secondary_cold_boot, \
Manish Pandeyc8257682019-11-26 11:34:17 +0000149 _init_memory, _init_c_runtime, _exception_vectors, \
150 _pie_fixup_size
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100151
David Cunadofee86532017-04-13 22:38:29 +0100152 .if \_init_sctlr
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100153 /* -------------------------------------------------------------
David Cunadofee86532017-04-13 22:38:29 +0100154 * This is the initialisation of SCTLR_EL3 and so must ensure
155 * that all fields are explicitly set rather than relying on hw.
156 * Some fields reset to an IMPLEMENTATION DEFINED value and
157 * others are architecturally UNKNOWN on reset.
158 *
159 * SCTLR.EE: Set the CPU endianness before doing anything that
160 * might involve memory reads or writes. Set to zero to select
161 * Little Endian.
162 *
163 * SCTLR_EL3.WXN: For the EL3 translation regime, this field can
164 * force all memory regions that are writeable to be treated as
165 * XN (Execute-never). Set to zero so that this control has no
166 * effect on memory access permissions.
167 *
Qixiang Xu3cc39172018-03-05 09:31:11 +0800168 * SCTLR_EL3.SA: Set to zero to disable Stack Alignment check.
David Cunadofee86532017-04-13 22:38:29 +0100169 *
170 * SCTLR_EL3.A: Set to zero to disable Alignment fault checking.
Jeenu Viswambharanaa00aff2018-11-15 11:38:03 +0000171 *
172 * SCTLR.DSSBS: Set to zero to disable speculation store bypass
173 * safe behaviour upon exception entry to EL3.
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100174 * -------------------------------------------------------------
175 */
David Cunadofee86532017-04-13 22:38:29 +0100176 mov_imm x0, (SCTLR_RESET_VAL & ~(SCTLR_EE_BIT | SCTLR_WXN_BIT \
Jeenu Viswambharanaa00aff2018-11-15 11:38:03 +0000177 | SCTLR_SA_BIT | SCTLR_A_BIT | SCTLR_DSSBS_BIT))
Manish Pandey514a3012023-10-10 13:53:25 +0100178#if ENABLE_FEAT_RAS
Manish Pandey6b5721f2023-06-26 17:46:14 +0100179 /* If FEAT_RAS is present assume FEAT_IESB is also present */
180 orr x0, x0, #SCTLR_IESB_BIT
181#endif
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100182 msr sctlr_el3, x0
183 isb
David Cunadofee86532017-04-13 22:38:29 +0100184 .endif /* _init_sctlr */
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100185
186 .if \_warm_boot_mailbox
187 /* -------------------------------------------------------------
188 * This code will be executed for both warm and cold resets.
189 * Now is the time to distinguish between the two.
190 * Query the platform entrypoint address and if it is not zero
191 * then it means it is a warm boot so jump to this address.
192 * -------------------------------------------------------------
193 */
Soby Mathew3700a922015-07-13 11:21:11 +0100194 bl plat_get_my_entrypoint
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100195 cbz x0, do_cold_boot
196 br x0
197
198 do_cold_boot:
199 .endif /* _warm_boot_mailbox */
200
Manish Pandeyc8257682019-11-26 11:34:17 +0000201 .if \_pie_fixup_size
202#if ENABLE_PIE
203 /*
204 * ------------------------------------------------------------
205 * If PIE is enabled fixup the Global descriptor Table only
206 * once during primary core cold boot path.
207 *
208 * Compile time base address, required for fixup, is calculated
209 * using "pie_fixup" label present within first page.
210 * ------------------------------------------------------------
211 */
212 pie_fixup:
213 ldr x0, =pie_fixup
Jimmy Brissoned202072020-08-04 16:18:52 -0500214 and x0, x0, #~(PAGE_SIZE_MASK)
Manish Pandeyc8257682019-11-26 11:34:17 +0000215 mov_imm x1, \_pie_fixup_size
216 add x1, x1, x0
217 bl fixup_gdt_reloc
218#endif /* ENABLE_PIE */
219 .endif /* _pie_fixup_size */
220
Antonio Nino Diaz4357b412016-02-23 12:04:58 +0000221 /* ---------------------------------------------------------------------
Dimitris Papastamos446f7f12017-11-30 14:53:53 +0000222 * Set the exception vectors.
223 * ---------------------------------------------------------------------
224 */
225 adr x0, \_exception_vectors
226 msr vbar_el3, x0
227 isb
228
Boyan Karatotev1dcba8f2024-11-19 11:27:01 +0000229 call_reset_handler
Antonio Nino Diaz4357b412016-02-23 12:04:58 +0000230
Dimitris Papastamos446f7f12017-11-30 14:53:53 +0000231 el3_arch_init_common
Antonio Nino Diaz4357b412016-02-23 12:04:58 +0000232
Jayanth Dodderi Chidanandb4590652023-08-08 16:10:16 +0100233 /* ---------------------------------------------------------------------
234 * Set the el3 execution context(i.e. root_context).
235 * ---------------------------------------------------------------------
236 */
237 setup_el3_execution_context
238
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100239 .if \_secondary_cold_boot
240 /* -------------------------------------------------------------
Antonio Nino Diaz4357b412016-02-23 12:04:58 +0000241 * Check if this is a primary or secondary CPU cold boot.
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100242 * The primary CPU will set up the platform while the
243 * secondaries are placed in a platform-specific state until the
244 * primary CPU performs the necessary actions to bring them out
245 * of that state and allows entry into the OS.
246 * -------------------------------------------------------------
247 */
Soby Mathew3700a922015-07-13 11:21:11 +0100248 bl plat_is_my_cpu_primary
Soby Matheweb3bbf12015-06-08 12:32:50 +0100249 cbnz w0, do_primary_cold_boot
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100250
251 /* This is a cold boot on a secondary CPU */
252 bl plat_secondary_cold_boot_setup
253 /* plat_secondary_cold_boot_setup() is not supposed to return */
Antonio Nino Diaz1f21bcf2016-02-01 13:57:25 +0000254 bl el3_panic
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100255
256 do_primary_cold_boot:
257 .endif /* _secondary_cold_boot */
258
259 /* ---------------------------------------------------------------------
Antonio Nino Diaz4357b412016-02-23 12:04:58 +0000260 * Initialize memory now. Secondary CPU initialization won't get to this
261 * point.
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100262 * ---------------------------------------------------------------------
263 */
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100264
265 .if \_init_memory
266 bl platform_mem_init
267 .endif /* _init_memory */
268
269 /* ---------------------------------------------------------------------
270 * Init C runtime environment:
271 * - Zero-initialise the NOBITS sections. There are 2 of them:
272 * - the .bss section;
273 * - the coherent memory section (if any).
274 * - Relocate the data section from ROM to RAM, if required.
275 * ---------------------------------------------------------------------
276 */
277 .if \_init_c_runtime
Zelalem Aweke688fbf72021-07-09 11:37:10 -0500278#if defined(IMAGE_BL31) || (defined(IMAGE_BL2) && \
Arvind Ram Prakash11b9b492022-11-22 14:41:00 -0600279 ((RESET_TO_BL2 && BL2_INV_DCACHE) || ENABLE_RME))
Achin Guptae9c4a642015-09-11 16:03:13 +0100280 /* -------------------------------------------------------------
281 * Invalidate the RW memory used by the BL31 image. This
282 * includes the data and NOBITS sections. This is done to
283 * safeguard against possible corruption of this memory by
284 * dirty cache lines in a system cache as a result of use by
Zelalem Awekeb0d69e82021-10-15 17:25:52 -0500285 * an earlier boot loader stage. If PIE is enabled however,
286 * RO sections including the GOT may be modified during
287 * pie fixup. Therefore, to be on the safe side, invalidate
288 * the entire image region if PIE is enabled.
Achin Guptae9c4a642015-09-11 16:03:13 +0100289 * -------------------------------------------------------------
290 */
Zelalem Awekeb0d69e82021-10-15 17:25:52 -0500291#if ENABLE_PIE
292#if SEPARATE_CODE_AND_RODATA
293 adrp x0, __TEXT_START__
294 add x0, x0, :lo12:__TEXT_START__
295#else
296 adrp x0, __RO_START__
297 add x0, x0, :lo12:__RO_START__
298#endif /* SEPARATE_CODE_AND_RODATA */
299#else
Soby Mathewfcaf1bd2018-10-12 16:40:28 +0100300 adrp x0, __RW_START__
301 add x0, x0, :lo12:__RW_START__
Zelalem Awekeb0d69e82021-10-15 17:25:52 -0500302#endif /* ENABLE_PIE */
Soby Mathewfcaf1bd2018-10-12 16:40:28 +0100303 adrp x1, __RW_END__
304 add x1, x1, :lo12:__RW_END__
Achin Guptae9c4a642015-09-11 16:03:13 +0100305 sub x1, x1, x0
306 bl inv_dcache_range
Samuel Holland31a14e12018-10-17 21:40:18 -0500307#if defined(IMAGE_BL31) && SEPARATE_NOBITS_REGION
308 adrp x0, __NOBITS_START__
309 add x0, x0, :lo12:__NOBITS_START__
310 adrp x1, __NOBITS_END__
311 add x1, x1, :lo12:__NOBITS_END__
312 sub x1, x1, x0
313 bl inv_dcache_range
314#endif
Jiafei Pan0824b452022-02-24 10:47:33 +0800315#if defined(IMAGE_BL2) && SEPARATE_BL2_NOLOAD_REGION
316 adrp x0, __BL2_NOLOAD_START__
317 add x0, x0, :lo12:__BL2_NOLOAD_START__
318 adrp x1, __BL2_NOLOAD_END__
319 add x1, x1, :lo12:__BL2_NOLOAD_END__
320 sub x1, x1, x0
321 bl inv_dcache_range
322#endif
Roberto Vargase0e99462017-10-30 14:43:43 +0000323#endif
Soby Mathewfcaf1bd2018-10-12 16:40:28 +0100324 adrp x0, __BSS_START__
325 add x0, x0, :lo12:__BSS_START__
Achin Guptae9c4a642015-09-11 16:03:13 +0100326
Soby Mathewfcaf1bd2018-10-12 16:40:28 +0100327 adrp x1, __BSS_END__
328 add x1, x1, :lo12:__BSS_END__
329 sub x1, x1, x0
Douglas Raillard21362a92016-12-02 13:51:54 +0000330 bl zeromem
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100331
332#if USE_COHERENT_MEM
Soby Mathewfcaf1bd2018-10-12 16:40:28 +0100333 adrp x0, __COHERENT_RAM_START__
334 add x0, x0, :lo12:__COHERENT_RAM_START__
335 adrp x1, __COHERENT_RAM_END_UNALIGNED__
336 add x1, x1, :lo12: __COHERENT_RAM_END_UNALIGNED__
337 sub x1, x1, x0
Douglas Raillard21362a92016-12-02 13:51:54 +0000338 bl zeromem
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100339#endif
340
Arvind Ram Prakash11b9b492022-11-22 14:41:00 -0600341#if defined(IMAGE_BL1) || \
Ye Li97267752022-08-26 13:48:31 +0800342 (defined(IMAGE_BL2) && RESET_TO_BL2 && BL2_IN_XIP_MEM) || \
343 (defined(IMAGE_BL31) && SEPARATE_RWDATA_REGION)
344
Soby Mathewfcaf1bd2018-10-12 16:40:28 +0100345 adrp x0, __DATA_RAM_START__
346 add x0, x0, :lo12:__DATA_RAM_START__
347 adrp x1, __DATA_ROM_START__
348 add x1, x1, :lo12:__DATA_ROM_START__
349 adrp x2, __DATA_RAM_END__
350 add x2, x2, :lo12:__DATA_RAM_END__
351 sub x2, x2, x0
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100352 bl memcpy16
353#endif
354 .endif /* _init_c_runtime */
355
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100356 /* ---------------------------------------------------------------------
357 * Use SP_EL0 for the C runtime stack.
358 * ---------------------------------------------------------------------
359 */
360 msr spsel, #0
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100361
362 /* ---------------------------------------------------------------------
363 * Allocate a stack whose memory will be marked as Normal-IS-WBWA when
364 * the MMU is enabled. There is no risk of reading stale stack memory
365 * after enabling the MMU as only the primary CPU is running at the
366 * moment.
367 * ---------------------------------------------------------------------
368 */
Soby Mathew3700a922015-07-13 11:21:11 +0100369 bl plat_set_my_stack
Douglas Raillard306593d2017-02-24 18:14:15 +0000370
371#if STACK_PROTECTOR_ENABLED
372 .if \_init_c_runtime
373 bl update_stack_protector_canary
374 .endif /* _init_c_runtime */
375#endif
Sandrine Bailleuxacde8b02015-05-19 11:54:45 +0100376 .endm
377
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100378 .macro apply_at_speculative_wa
379#if ERRATA_SPECULATIVE_AT
380 /*
Manish Pandey66a056e2023-01-11 21:41:07 +0000381 * This function expects x30 has been saved.
382 * Also, save x29 which will be used in the called function.
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100383 */
Manish Pandey66a056e2023-01-11 21:41:07 +0000384 str x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100385 bl save_and_update_ptw_el1_sys_regs
Manish Pandey66a056e2023-01-11 21:41:07 +0000386 ldr x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100387#endif
388 .endm
389
390 .macro restore_ptw_el1_sys_regs
391#if ERRATA_SPECULATIVE_AT
392 /* -----------------------------------------------------------
393 * In case of ERRATA_SPECULATIVE_AT, must follow below order
394 * to ensure that page table walk is not enabled until
395 * restoration of all EL1 system registers. TCR_EL1 register
396 * should be updated at the end which restores previous page
397 * table walk setting of stage1 i.e.(TCR_EL1.EPDx) bits. ISB
398 * ensures that CPU does below steps in order.
399 *
400 * 1. Ensure all other system registers are written before
401 * updating SCTLR_EL1 using ISB.
402 * 2. Restore SCTLR_EL1 register.
403 * 3. Ensure SCTLR_EL1 written successfully using ISB.
404 * 4. Restore TCR_EL1 register.
405 * -----------------------------------------------------------
406 */
407 isb
Jayanth Dodderi Chidanand3a71df62024-06-05 11:13:05 +0100408 ldp x28, x29, [sp, #CTX_ERRATA_SPEC_AT_OFFSET + CTX_ERRATA_SPEC_AT_SCTLR_EL1]
Manish V Badarkhee07e8082020-07-23 12:43:25 +0100409 msr sctlr_el1, x28
410 isb
411 msr tcr_el1, x29
412#endif
413 .endm
414
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100415/* -----------------------------------------------------------------
416 * The below macro reads SCR_EL3 from the context structure to
417 * determine the security state of the context upon ERET.
418 * ------------------------------------------------------------------
419 */
420 .macro get_security_state _ret:req, _scr_reg:req
421 ubfx \_ret, \_scr_reg, #SCR_NSE_SHIFT, #1
422 cmp \_ret, #1
423 beq realm_state
424 bfi \_ret, \_scr_reg, #0, #1
425 b end
426 realm_state:
427 mov \_ret, #2
428 end:
429 .endm
430
Jayanth Dodderi Chidanandb4590652023-08-08 16:10:16 +0100431/*-----------------------------------------------------------------------------
432 * Helper macro to configure EL3 registers we care about, while executing
433 * at EL3/Root world. Root world has its own execution environment and
434 * needs to have its settings configured to be independent of other worlds.
435 * -----------------------------------------------------------------------------
436 */
437 .macro setup_el3_execution_context
438
439 /* ---------------------------------------------------------------------
440 * The following registers need to be part of separate root context
441 * as their values are of importance during EL3 execution.
442 * Hence these registers are overwritten to their intital values,
443 * irrespective of whichever world they return from to ensure EL3 has a
444 * consistent execution context throughout the lifetime of TF-A.
445 *
446 * DAIF.A: Enable External Aborts and SError Interrupts at EL3.
447 *
448 * MDCR_EL3.SDD: Set to one to disable AArch64 Secure self-hosted debug.
449 * Debug exceptions, other than Breakpoint Instruction exceptions, are
450 * disabled from all ELs in Secure state.
451 *
452 * SCR_EL3.EA: Set to one to enable SError interrupts at EL3.
453 *
454 * SCR_EL3.SIF: Set to one to disable instruction fetches from
455 * Non-secure memory.
456 *
457 * PMCR_EL0.DP: Set to one so that the cycle counter,
458 * PMCCNTR_EL0 does not count when event counting is prohibited.
459 * Necessary on PMUv3 <= p7 where MDCR_EL3.{SCCD,MCCD} are not
460 * available.
461 *
Boyan Karatotev90b7b752024-11-15 15:03:02 +0000462 * CPTR_EL3.EZ: Set to one so that accesses to ZCR_EL3 do not trap
463 * CPTR_EL3.TFP: Set to zero so that advanced SIMD operations don't trap
464 * CPTR_EL3.ESM: Set to one so that SME related registers don't trap
465 *
Jayanth Dodderi Chidanandb4590652023-08-08 16:10:16 +0100466 * PSTATE.DIT: Set to one to enable the Data Independent Timing (DIT)
467 * functionality, if implemented in EL3.
468 * ---------------------------------------------------------------------
469 */
470 msr daifclr, #DAIF_ABT_BIT
471
472 mrs x15, mdcr_el3
473 orr x15, x15, #MDCR_SDD_BIT
474 msr mdcr_el3, x15
475
476 mrs x15, scr_el3
477 orr x15, x15, #SCR_EA_BIT
478 orr x15, x15, #SCR_SIF_BIT
479 msr scr_el3, x15
480
481 mrs x15, pmcr_el0
482 orr x15, x15, #PMCR_EL0_DP_BIT
483 msr pmcr_el0, x15
484
Boyan Karatotev90b7b752024-11-15 15:03:02 +0000485 mrs x15, cptr_el3
486 orr x15, x15, #CPTR_EZ_BIT
487 orr x15, x15, #ESM_BIT
488 bic x15, x15, #TFP_BIT
489 msr cptr_el3, x15
490
Jayanth Dodderi Chidanandb4590652023-08-08 16:10:16 +0100491#if ENABLE_FEAT_DIT
492#if ENABLE_FEAT_DIT > 1
493 mrs x15, id_aa64pfr0_el1
494 ubfx x15, x15, #ID_AA64PFR0_DIT_SHIFT, #ID_AA64PFR0_DIT_LENGTH
495 cbz x15, 1f
496#endif
497 mov x15, #DIT_BIT
498 msr DIT, x15
499 1:
500#endif
501
502 isb
503 .endm
504
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +0000505#endif /* EL3_COMMON_MACROS_S */