1f5478dedSAntonio Nino Diaz/* 20d020822SBoyan Karatotev * Copyright (c) 2015-2025, 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> 1434a22a02SBoyan Karatotev#include <lib/el3_runtime/cpu_data.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. 3510ecd580SBoyan Karatotev * 3610ecd580SBoyan Karatotev * SCTLR_EL3.BT: PAuth instructions are compatible with bti jc 37f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 38f5478dedSAntonio Nino Diaz */ 3910ecd580SBoyan Karatotev mov_imm x1, (SCTLR_I_BIT | SCTLR_A_BIT | SCTLR_SA_BIT) 40f5478dedSAntonio Nino Diaz mrs x0, sctlr_el3 4110ecd580SBoyan Karatotev#if ENABLE_BTI 4210ecd580SBoyan Karatotev bic x0, x0, #SCTLR_BT_BIT 4310ecd580SBoyan Karatotev#endif 44f5478dedSAntonio Nino Diaz orr x0, x0, x1 45f5478dedSAntonio Nino Diaz msr sctlr_el3, x0 46f5478dedSAntonio Nino Diaz isb 47f5478dedSAntonio Nino Diaz 48025b1b81SJohn Powell#if ENABLE_FEAT_SCTLR2 49025b1b81SJohn Powell#if ENABLE_FEAT_SCTLR2 > 1 50025b1b81SJohn Powell is_feat_sctlr2_present_asm x1 51025b1b81SJohn Powell beq feat_sctlr2_not_supported\@ 52025b1b81SJohn Powell#endif 53025b1b81SJohn Powell mov x1, #SCTLR2_RESET_VAL 54025b1b81SJohn Powell msr SCTLR2_EL3, x1 55025b1b81SJohn Powellfeat_sctlr2_not_supported\@: 56025b1b81SJohn Powell#endif 57025b1b81SJohn Powell 58f5478dedSAntonio Nino Diaz#ifdef IMAGE_BL31 59f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 60f5478dedSAntonio Nino Diaz * Initialise the per-cpu cache pointer to the CPU. 61f5478dedSAntonio Nino Diaz * This is done early to enable crash reporting to have access to crash 62f5478dedSAntonio Nino Diaz * stack. Since crash reporting depends on cpu_data to report the 63f5478dedSAntonio Nino Diaz * unhandled exception, not doing so can lead to recursive exceptions 64f5478dedSAntonio Nino Diaz * due to a NULL TPIDR_EL3. 65f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 66f5478dedSAntonio Nino Diaz */ 67b07c317fSBoyan Karatotev bl plat_my_core_pos 68d43b2ea6SBoyan Karatotev /* index into the cpu_data */ 69d43b2ea6SBoyan Karatotev mov_imm x1, CPU_DATA_SIZE 70d43b2ea6SBoyan Karatotev mul x0, x0, x1 71d43b2ea6SBoyan Karatotev adr_l x1, percpu_data 72d43b2ea6SBoyan Karatotev add x0, x0, x1 73b07c317fSBoyan Karatotev msr tpidr_el3, x0 74f5478dedSAntonio Nino Diaz#endif /* IMAGE_BL31 */ 75f5478dedSAntonio Nino Diaz 76f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 77f5478dedSAntonio Nino Diaz * Initialise SCR_EL3, setting all fields rather than relying on hw. 78f5478dedSAntonio Nino Diaz * All fields are architecturally UNKNOWN on reset. The following fields 79f5478dedSAntonio Nino Diaz * do not change during the TF lifetime. The remaining fields are set to 80f5478dedSAntonio Nino Diaz * zero here but are updated ahead of transitioning to a lower EL in the 81f5478dedSAntonio Nino Diaz * function cm_init_context_common(). 82f5478dedSAntonio Nino Diaz * 838815cdafSManish Pandey * SCR_EL3.EEL2: Set to one if S-EL2 is present and enabled. 848815cdafSManish Pandey * 858815cdafSManish Pandey * NOTE: Modifying EEL2 bit along with EA bit ensures that we mitigate 868815cdafSManish Pandey * against ERRATA_V2_3099206. 87f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 88f5478dedSAntonio Nino Diaz */ 8940e5f7a5SJayanth Dodderi Chidanand mov_imm x0, SCR_RESET_VAL 908815cdafSManish Pandey#if IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2 918815cdafSManish Pandey mrs x1, id_aa64pfr0_el1 928815cdafSManish Pandey and x1, x1, #(ID_AA64PFR0_SEL2_MASK << ID_AA64PFR0_SEL2_SHIFT) 938815cdafSManish Pandey cbz x1, 1f 948815cdafSManish Pandey orr x0, x0, #SCR_EEL2_BIT 958815cdafSManish Pandey#endif 968815cdafSManish Pandey1: 97f5478dedSAntonio Nino Diaz msr scr_el3, x0 98f5478dedSAntonio Nino Diaz 99f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 100f5478dedSAntonio Nino Diaz * Initialise MDCR_EL3, setting all fields rather than relying on hw. 101f5478dedSAntonio Nino Diaz * Some fields are architecturally UNKNOWN on reset. 102f5478dedSAntonio Nino Diaz */ 10340e5f7a5SJayanth Dodderi Chidanand mov_imm x0, MDCR_EL3_RESET_VAL 104f5478dedSAntonio Nino Diaz msr mdcr_el3, x0 105f5478dedSAntonio Nino Diaz 106f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 107f5478dedSAntonio Nino Diaz * Initialise CPTR_EL3, setting all fields rather than relying on hw. 108f5478dedSAntonio Nino Diaz * All fields are architecturally UNKNOWN on reset. 109f0c96a2eSBoyan Karatotev * --------------------------------------------------------------------- 110f5478dedSAntonio Nino Diaz */ 111f0c96a2eSBoyan Karatotev mov_imm x0, CPTR_EL3_RESET_VAL 112f5478dedSAntonio Nino Diaz msr cptr_el3, x0 113f5478dedSAntonio Nino Diaz 114f5478dedSAntonio Nino Diaz .endm 115f5478dedSAntonio Nino Diaz 116f5478dedSAntonio Nino Diaz/* ----------------------------------------------------------------------------- 117f5478dedSAntonio Nino Diaz * This is the super set of actions that need to be performed during a cold boot 118f5478dedSAntonio Nino Diaz * or a warm boot in EL3. This code is shared by BL1 and BL31. 119f5478dedSAntonio Nino Diaz * 120f5478dedSAntonio Nino Diaz * This macro will always perform reset handling, architectural initialisations 121f5478dedSAntonio Nino Diaz * and stack setup. The rest of the actions are optional because they might not 122f5478dedSAntonio Nino Diaz * be needed, depending on the context in which this macro is called. This is 123f5478dedSAntonio Nino Diaz * why this macro is parameterised ; each parameter allows to enable/disable 124f5478dedSAntonio Nino Diaz * some actions. 125f5478dedSAntonio Nino Diaz * 126f5478dedSAntonio Nino Diaz * _init_sctlr: 127f5478dedSAntonio Nino Diaz * Whether the macro needs to initialise SCTLR_EL3, including configuring 128f5478dedSAntonio Nino Diaz * the endianness of data accesses. 129f5478dedSAntonio Nino Diaz * 130f5478dedSAntonio Nino Diaz * _warm_boot_mailbox: 131f5478dedSAntonio Nino Diaz * Whether the macro needs to detect the type of boot (cold/warm). The 132f5478dedSAntonio Nino Diaz * detection is based on the platform entrypoint address : if it is zero 133f5478dedSAntonio Nino Diaz * then it is a cold boot, otherwise it is a warm boot. In the latter case, 134f5478dedSAntonio Nino Diaz * this macro jumps on the platform entrypoint address. 135f5478dedSAntonio Nino Diaz * 136f5478dedSAntonio Nino Diaz * _secondary_cold_boot: 137f5478dedSAntonio Nino Diaz * Whether the macro needs to identify the CPU that is calling it: primary 138f5478dedSAntonio Nino Diaz * CPU or secondary CPU. The primary CPU will be allowed to carry on with 139f5478dedSAntonio Nino Diaz * the platform initialisations, while the secondaries will be put in a 140f5478dedSAntonio Nino Diaz * platform-specific state in the meantime. 141f5478dedSAntonio Nino Diaz * 142f5478dedSAntonio Nino Diaz * If the caller knows this macro will only be called by the primary CPU 143f5478dedSAntonio Nino Diaz * then this parameter can be defined to 0 to skip this step. 144f5478dedSAntonio Nino Diaz * 145f5478dedSAntonio Nino Diaz * _init_memory: 146f5478dedSAntonio Nino Diaz * Whether the macro needs to initialise the memory. 147f5478dedSAntonio Nino Diaz * 148f5478dedSAntonio Nino Diaz * _init_c_runtime: 149f5478dedSAntonio Nino Diaz * Whether the macro needs to initialise the C runtime environment. 150f5478dedSAntonio Nino Diaz * 151f5478dedSAntonio Nino Diaz * _exception_vectors: 152f5478dedSAntonio Nino Diaz * Address of the exception vectors to program in the VBAR_EL3 register. 153da90359bSManish Pandey * 154da90359bSManish Pandey * _pie_fixup_size: 155da90359bSManish Pandey * Size of memory region to fixup Global Descriptor Table (GDT). 156da90359bSManish Pandey * 157da90359bSManish Pandey * A non-zero value is expected when firmware needs GDT to be fixed-up. 158da90359bSManish Pandey * 159f5478dedSAntonio Nino Diaz * ----------------------------------------------------------------------------- 160f5478dedSAntonio Nino Diaz */ 161f5478dedSAntonio Nino Diaz .macro el3_entrypoint_common \ 162f5478dedSAntonio Nino Diaz _init_sctlr, _warm_boot_mailbox, _secondary_cold_boot, \ 163da90359bSManish Pandey _init_memory, _init_c_runtime, _exception_vectors, \ 164da90359bSManish Pandey _pie_fixup_size 165f5478dedSAntonio Nino Diaz 166f5478dedSAntonio Nino Diaz .if \_init_sctlr 167f5478dedSAntonio Nino Diaz /* ------------------------------------------------------------- 168f5478dedSAntonio Nino Diaz * This is the initialisation of SCTLR_EL3 and so must ensure 169f5478dedSAntonio Nino Diaz * that all fields are explicitly set rather than relying on hw. 170f5478dedSAntonio Nino Diaz * Some fields reset to an IMPLEMENTATION DEFINED value and 171f5478dedSAntonio Nino Diaz * others are architecturally UNKNOWN on reset. 172f5478dedSAntonio Nino Diaz * 173f5478dedSAntonio Nino Diaz * SCTLR.EE: Set the CPU endianness before doing anything that 174f5478dedSAntonio Nino Diaz * might involve memory reads or writes. Set to zero to select 175f5478dedSAntonio Nino Diaz * Little Endian. 176f5478dedSAntonio Nino Diaz * 177f5478dedSAntonio Nino Diaz * SCTLR_EL3.WXN: For the EL3 translation regime, this field can 178f5478dedSAntonio Nino Diaz * force all memory regions that are writeable to be treated as 179f5478dedSAntonio Nino Diaz * XN (Execute-never). Set to zero so that this control has no 180f5478dedSAntonio Nino Diaz * effect on memory access permissions. 181f5478dedSAntonio Nino Diaz * 182f5478dedSAntonio Nino Diaz * SCTLR_EL3.SA: Set to zero to disable Stack Alignment check. 183f5478dedSAntonio Nino Diaz * 184f5478dedSAntonio Nino Diaz * SCTLR_EL3.A: Set to zero to disable Alignment fault checking. 185f5478dedSAntonio Nino Diaz * 186f5478dedSAntonio Nino Diaz * SCTLR.DSSBS: Set to zero to disable speculation store bypass 187f5478dedSAntonio Nino Diaz * safe behaviour upon exception entry to EL3. 188f5478dedSAntonio Nino Diaz * ------------------------------------------------------------- 189f5478dedSAntonio Nino Diaz */ 190f5478dedSAntonio Nino Diaz mov_imm x0, (SCTLR_RESET_VAL & ~(SCTLR_EE_BIT | SCTLR_WXN_BIT \ 191f5478dedSAntonio Nino Diaz | SCTLR_SA_BIT | SCTLR_A_BIT | SCTLR_DSSBS_BIT)) 192970a4a8dSManish Pandey#if ENABLE_FEAT_RAS 1936597fcf1SManish Pandey /* If FEAT_RAS is present assume FEAT_IESB is also present */ 1946597fcf1SManish Pandey orr x0, x0, #SCTLR_IESB_BIT 1956597fcf1SManish Pandey#endif 196f5478dedSAntonio Nino Diaz msr sctlr_el3, x0 197f5478dedSAntonio Nino Diaz isb 198f5478dedSAntonio Nino Diaz .endif /* _init_sctlr */ 199f5478dedSAntonio Nino Diaz 200f5478dedSAntonio Nino Diaz .if \_warm_boot_mailbox 201f5478dedSAntonio Nino Diaz /* ------------------------------------------------------------- 202f5478dedSAntonio Nino Diaz * This code will be executed for both warm and cold resets. 203f5478dedSAntonio Nino Diaz * Now is the time to distinguish between the two. 204f5478dedSAntonio Nino Diaz * Query the platform entrypoint address and if it is not zero 205f5478dedSAntonio Nino Diaz * then it means it is a warm boot so jump to this address. 206f5478dedSAntonio Nino Diaz * ------------------------------------------------------------- 207f5478dedSAntonio Nino Diaz */ 208f5478dedSAntonio Nino Diaz bl plat_get_my_entrypoint 209f5478dedSAntonio Nino Diaz cbz x0, do_cold_boot 210f5478dedSAntonio Nino Diaz br x0 211f5478dedSAntonio Nino Diaz 212f5478dedSAntonio Nino Diaz do_cold_boot: 213f5478dedSAntonio Nino Diaz .endif /* _warm_boot_mailbox */ 214f5478dedSAntonio Nino Diaz 215da90359bSManish Pandey .if \_pie_fixup_size 216da90359bSManish Pandey#if ENABLE_PIE 217da90359bSManish Pandey /* 218da90359bSManish Pandey * ------------------------------------------------------------ 219da90359bSManish Pandey * If PIE is enabled fixup the Global descriptor Table only 220da90359bSManish Pandey * once during primary core cold boot path. 221da90359bSManish Pandey * 222da90359bSManish Pandey * Compile time base address, required for fixup, is calculated 223da90359bSManish Pandey * using "pie_fixup" label present within first page. 224da90359bSManish Pandey * ------------------------------------------------------------ 225da90359bSManish Pandey */ 226da90359bSManish Pandey pie_fixup: 227da90359bSManish Pandey ldr x0, =pie_fixup 228d7b5f408SJimmy Brisson and x0, x0, #~(PAGE_SIZE_MASK) 229da90359bSManish Pandey mov_imm x1, \_pie_fixup_size 230da90359bSManish Pandey add x1, x1, x0 231da90359bSManish Pandey bl fixup_gdt_reloc 232da90359bSManish Pandey#endif /* ENABLE_PIE */ 233da90359bSManish Pandey .endif /* _pie_fixup_size */ 234da90359bSManish Pandey 235f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 236f5478dedSAntonio Nino Diaz * Set the exception vectors. 237f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 238f5478dedSAntonio Nino Diaz */ 239f5478dedSAntonio Nino Diaz adr x0, \_exception_vectors 240f5478dedSAntonio Nino Diaz msr vbar_el3, x0 241f5478dedSAntonio Nino Diaz isb 242f5478dedSAntonio Nino Diaz 2430d020822SBoyan Karatotev call_reset_handler 244f5478dedSAntonio Nino Diaz 245f5478dedSAntonio Nino Diaz el3_arch_init_common 246f5478dedSAntonio Nino Diaz 24740e5f7a5SJayanth Dodderi Chidanand /* --------------------------------------------------------------------- 24840e5f7a5SJayanth Dodderi Chidanand * Set the el3 execution context(i.e. root_context). 24940e5f7a5SJayanth Dodderi Chidanand * --------------------------------------------------------------------- 25040e5f7a5SJayanth Dodderi Chidanand */ 25140e5f7a5SJayanth Dodderi Chidanand setup_el3_execution_context 25240e5f7a5SJayanth Dodderi Chidanand 253f5478dedSAntonio Nino Diaz .if \_secondary_cold_boot 254f5478dedSAntonio Nino Diaz /* ------------------------------------------------------------- 255f5478dedSAntonio Nino Diaz * Check if this is a primary or secondary CPU cold boot. 256f5478dedSAntonio Nino Diaz * The primary CPU will set up the platform while the 257f5478dedSAntonio Nino Diaz * secondaries are placed in a platform-specific state until the 258f5478dedSAntonio Nino Diaz * primary CPU performs the necessary actions to bring them out 259f5478dedSAntonio Nino Diaz * of that state and allows entry into the OS. 260f5478dedSAntonio Nino Diaz * ------------------------------------------------------------- 261f5478dedSAntonio Nino Diaz */ 262f5478dedSAntonio Nino Diaz bl plat_is_my_cpu_primary 263f5478dedSAntonio Nino Diaz cbnz w0, do_primary_cold_boot 264f5478dedSAntonio Nino Diaz 265f5478dedSAntonio Nino Diaz /* This is a cold boot on a secondary CPU */ 266f5478dedSAntonio Nino Diaz bl plat_secondary_cold_boot_setup 267f5478dedSAntonio Nino Diaz /* plat_secondary_cold_boot_setup() is not supposed to return */ 268f5478dedSAntonio Nino Diaz bl el3_panic 269f5478dedSAntonio Nino Diaz 270f5478dedSAntonio Nino Diaz do_primary_cold_boot: 271f5478dedSAntonio Nino Diaz .endif /* _secondary_cold_boot */ 272f5478dedSAntonio Nino Diaz 273f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 274f5478dedSAntonio Nino Diaz * Initialize memory now. Secondary CPU initialization won't get to this 275f5478dedSAntonio Nino Diaz * point. 276f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 277f5478dedSAntonio Nino Diaz */ 278f5478dedSAntonio Nino Diaz 279f5478dedSAntonio Nino Diaz .if \_init_memory 280f5478dedSAntonio Nino Diaz bl platform_mem_init 281f5478dedSAntonio Nino Diaz .endif /* _init_memory */ 282f5478dedSAntonio Nino Diaz 283f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 284f5478dedSAntonio Nino Diaz * Init C runtime environment: 285f5478dedSAntonio Nino Diaz * - Zero-initialise the NOBITS sections. There are 2 of them: 286f5478dedSAntonio Nino Diaz * - the .bss section; 287f5478dedSAntonio Nino Diaz * - the coherent memory section (if any). 288f5478dedSAntonio Nino Diaz * - Relocate the data section from ROM to RAM, if required. 289f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 290f5478dedSAntonio Nino Diaz */ 291f5478dedSAntonio Nino Diaz .if \_init_c_runtime 2926c09af9fSZelalem Aweke#if defined(IMAGE_BL31) || (defined(IMAGE_BL2) && \ 29342d4d3baSArvind Ram Prakash ((RESET_TO_BL2 && BL2_INV_DCACHE) || ENABLE_RME)) 294f5478dedSAntonio Nino Diaz /* ------------------------------------------------------------- 295f5478dedSAntonio Nino Diaz * Invalidate the RW memory used by the BL31 image. This 296f5478dedSAntonio Nino Diaz * includes the data and NOBITS sections. This is done to 297f5478dedSAntonio Nino Diaz * safeguard against possible corruption of this memory by 298f5478dedSAntonio Nino Diaz * dirty cache lines in a system cache as a result of use by 299596d20d9SZelalem Aweke * an earlier boot loader stage. If PIE is enabled however, 300596d20d9SZelalem Aweke * RO sections including the GOT may be modified during 301596d20d9SZelalem Aweke * pie fixup. Therefore, to be on the safe side, invalidate 302596d20d9SZelalem Aweke * the entire image region if PIE is enabled. 303f5478dedSAntonio Nino Diaz * ------------------------------------------------------------- 304f5478dedSAntonio Nino Diaz */ 305596d20d9SZelalem Aweke#if ENABLE_PIE 306596d20d9SZelalem Aweke#if SEPARATE_CODE_AND_RODATA 307596d20d9SZelalem Aweke adrp x0, __TEXT_START__ 308596d20d9SZelalem Aweke add x0, x0, :lo12:__TEXT_START__ 309596d20d9SZelalem Aweke#else 310596d20d9SZelalem Aweke adrp x0, __RO_START__ 311596d20d9SZelalem Aweke add x0, x0, :lo12:__RO_START__ 312596d20d9SZelalem Aweke#endif /* SEPARATE_CODE_AND_RODATA */ 313596d20d9SZelalem Aweke#else 314f5478dedSAntonio Nino Diaz adrp x0, __RW_START__ 315f5478dedSAntonio Nino Diaz add x0, x0, :lo12:__RW_START__ 316596d20d9SZelalem Aweke#endif /* ENABLE_PIE */ 317f5478dedSAntonio Nino Diaz adrp x1, __RW_END__ 318f5478dedSAntonio Nino Diaz add x1, x1, :lo12:__RW_END__ 319f5478dedSAntonio Nino Diaz sub x1, x1, x0 320f5478dedSAntonio Nino Diaz bl inv_dcache_range 321f8578e64SSamuel Holland#if defined(IMAGE_BL31) && SEPARATE_NOBITS_REGION 322f8578e64SSamuel Holland adrp x0, __NOBITS_START__ 323f8578e64SSamuel Holland add x0, x0, :lo12:__NOBITS_START__ 324f8578e64SSamuel Holland adrp x1, __NOBITS_END__ 325f8578e64SSamuel Holland add x1, x1, :lo12:__NOBITS_END__ 326f8578e64SSamuel Holland sub x1, x1, x0 327f8578e64SSamuel Holland bl inv_dcache_range 328f8578e64SSamuel Holland#endif 32996a8ed14SJiafei Pan#if defined(IMAGE_BL2) && SEPARATE_BL2_NOLOAD_REGION 33096a8ed14SJiafei Pan adrp x0, __BL2_NOLOAD_START__ 33196a8ed14SJiafei Pan add x0, x0, :lo12:__BL2_NOLOAD_START__ 33296a8ed14SJiafei Pan adrp x1, __BL2_NOLOAD_END__ 33396a8ed14SJiafei Pan add x1, x1, :lo12:__BL2_NOLOAD_END__ 33496a8ed14SJiafei Pan sub x1, x1, x0 33596a8ed14SJiafei Pan bl inv_dcache_range 33696a8ed14SJiafei Pan#endif 337f5478dedSAntonio Nino Diaz#endif 338f5478dedSAntonio Nino Diaz adrp x0, __BSS_START__ 339f5478dedSAntonio Nino Diaz add x0, x0, :lo12:__BSS_START__ 340f5478dedSAntonio Nino Diaz 341f5478dedSAntonio Nino Diaz adrp x1, __BSS_END__ 342f5478dedSAntonio Nino Diaz add x1, x1, :lo12:__BSS_END__ 343f5478dedSAntonio Nino Diaz sub x1, x1, x0 344f5478dedSAntonio Nino Diaz bl zeromem 345f5478dedSAntonio Nino Diaz 346f5478dedSAntonio Nino Diaz#if USE_COHERENT_MEM 347f5478dedSAntonio Nino Diaz adrp x0, __COHERENT_RAM_START__ 348f5478dedSAntonio Nino Diaz add x0, x0, :lo12:__COHERENT_RAM_START__ 349f5478dedSAntonio Nino Diaz adrp x1, __COHERENT_RAM_END_UNALIGNED__ 350f5478dedSAntonio Nino Diaz add x1, x1, :lo12: __COHERENT_RAM_END_UNALIGNED__ 351f5478dedSAntonio Nino Diaz sub x1, x1, x0 352f5478dedSAntonio Nino Diaz bl zeromem 353f5478dedSAntonio Nino Diaz#endif 354f5478dedSAntonio Nino Diaz 35542d4d3baSArvind Ram Prakash#if defined(IMAGE_BL1) || \ 35686acbbe2SYe Li (defined(IMAGE_BL2) && RESET_TO_BL2 && BL2_IN_XIP_MEM) || \ 35786acbbe2SYe Li (defined(IMAGE_BL31) && SEPARATE_RWDATA_REGION) 35886acbbe2SYe Li 359f5478dedSAntonio Nino Diaz adrp x0, __DATA_RAM_START__ 360f5478dedSAntonio Nino Diaz add x0, x0, :lo12:__DATA_RAM_START__ 361f5478dedSAntonio Nino Diaz adrp x1, __DATA_ROM_START__ 362f5478dedSAntonio Nino Diaz add x1, x1, :lo12:__DATA_ROM_START__ 363f5478dedSAntonio Nino Diaz adrp x2, __DATA_RAM_END__ 364f5478dedSAntonio Nino Diaz add x2, x2, :lo12:__DATA_RAM_END__ 365f5478dedSAntonio Nino Diaz sub x2, x2, x0 366f5478dedSAntonio Nino Diaz bl memcpy16 367f5478dedSAntonio Nino Diaz#endif 368f5478dedSAntonio Nino Diaz .endif /* _init_c_runtime */ 369f5478dedSAntonio Nino Diaz 370f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 371f5478dedSAntonio Nino Diaz * Use SP_EL0 for the C runtime stack. 372f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 373f5478dedSAntonio Nino Diaz */ 374f5478dedSAntonio Nino Diaz msr spsel, #0 375f5478dedSAntonio Nino Diaz 376f5478dedSAntonio Nino Diaz /* --------------------------------------------------------------------- 377f5478dedSAntonio Nino Diaz * Allocate a stack whose memory will be marked as Normal-IS-WBWA when 378f5478dedSAntonio Nino Diaz * the MMU is enabled. There is no risk of reading stale stack memory 379f5478dedSAntonio Nino Diaz * after enabling the MMU as only the primary CPU is running at the 380f5478dedSAntonio Nino Diaz * moment. 381f5478dedSAntonio Nino Diaz * --------------------------------------------------------------------- 382f5478dedSAntonio Nino Diaz */ 383f5478dedSAntonio Nino Diaz bl plat_set_my_stack 384f5478dedSAntonio Nino Diaz 385f5478dedSAntonio Nino Diaz#if STACK_PROTECTOR_ENABLED 386f5478dedSAntonio Nino Diaz .if \_init_c_runtime 387f5478dedSAntonio Nino Diaz bl update_stack_protector_canary 388f5478dedSAntonio Nino Diaz .endif /* _init_c_runtime */ 389f5478dedSAntonio Nino Diaz#endif 390f5478dedSAntonio Nino Diaz .endm 391f5478dedSAntonio Nino Diaz 3923b8456bdSManish V Badarkhe .macro apply_at_speculative_wa 3933b8456bdSManish V Badarkhe#if ERRATA_SPECULATIVE_AT 3943b8456bdSManish V Badarkhe /* 395d87c0e27SManish Pandey * This function expects x30 has been saved. 396d87c0e27SManish Pandey * Also, save x29 which will be used in the called function. 3973b8456bdSManish V Badarkhe */ 398d87c0e27SManish Pandey str x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 3993b8456bdSManish V Badarkhe bl save_and_update_ptw_el1_sys_regs 400d87c0e27SManish Pandey ldr x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29] 4013b8456bdSManish V Badarkhe#endif 4023b8456bdSManish V Badarkhe .endm 4033b8456bdSManish V Badarkhe 4043b8456bdSManish V Badarkhe .macro restore_ptw_el1_sys_regs 4053b8456bdSManish V Badarkhe#if ERRATA_SPECULATIVE_AT 4063b8456bdSManish V Badarkhe /* ----------------------------------------------------------- 4073b8456bdSManish V Badarkhe * In case of ERRATA_SPECULATIVE_AT, must follow below order 4083b8456bdSManish V Badarkhe * to ensure that page table walk is not enabled until 4093b8456bdSManish V Badarkhe * restoration of all EL1 system registers. TCR_EL1 register 4103b8456bdSManish V Badarkhe * should be updated at the end which restores previous page 4113b8456bdSManish V Badarkhe * table walk setting of stage1 i.e.(TCR_EL1.EPDx) bits. ISB 4123b8456bdSManish V Badarkhe * ensures that CPU does below steps in order. 4133b8456bdSManish V Badarkhe * 4143b8456bdSManish V Badarkhe * 1. Ensure all other system registers are written before 4153b8456bdSManish V Badarkhe * updating SCTLR_EL1 using ISB. 4163b8456bdSManish V Badarkhe * 2. Restore SCTLR_EL1 register. 4173b8456bdSManish V Badarkhe * 3. Ensure SCTLR_EL1 written successfully using ISB. 4183b8456bdSManish V Badarkhe * 4. Restore TCR_EL1 register. 4193b8456bdSManish V Badarkhe * ----------------------------------------------------------- 4203b8456bdSManish V Badarkhe */ 4213b8456bdSManish V Badarkhe isb 42259b7c0a0SJayanth Dodderi Chidanand ldp x28, x29, [sp, #CTX_ERRATA_SPEC_AT_OFFSET + CTX_ERRATA_SPEC_AT_SCTLR_EL1] 4233b8456bdSManish V Badarkhe msr sctlr_el1, x28 4243b8456bdSManish V Badarkhe isb 4253b8456bdSManish V Badarkhe msr tcr_el1, x29 4263b8456bdSManish V Badarkhe#endif 4273b8456bdSManish V Badarkhe .endm 4283b8456bdSManish V Badarkhe 429461c0a5dSElizabeth Ho/* ----------------------------------------------------------------- 430461c0a5dSElizabeth Ho * The below macro reads SCR_EL3 from the context structure to 431461c0a5dSElizabeth Ho * determine the security state of the context upon ERET. 432461c0a5dSElizabeth Ho * ------------------------------------------------------------------ 433461c0a5dSElizabeth Ho */ 434461c0a5dSElizabeth Ho .macro get_security_state _ret:req, _scr_reg:req 435461c0a5dSElizabeth Ho ubfx \_ret, \_scr_reg, #SCR_NSE_SHIFT, #1 436461c0a5dSElizabeth Ho cmp \_ret, #1 437461c0a5dSElizabeth Ho beq realm_state 438461c0a5dSElizabeth Ho bfi \_ret, \_scr_reg, #0, #1 439461c0a5dSElizabeth Ho b end 440461c0a5dSElizabeth Ho realm_state: 441461c0a5dSElizabeth Ho mov \_ret, #2 442461c0a5dSElizabeth Ho end: 443461c0a5dSElizabeth Ho .endm 444461c0a5dSElizabeth Ho 44540e5f7a5SJayanth Dodderi Chidanand/*----------------------------------------------------------------------------- 44640e5f7a5SJayanth Dodderi Chidanand * Helper macro to configure EL3 registers we care about, while executing 44740e5f7a5SJayanth Dodderi Chidanand * at EL3/Root world. Root world has its own execution environment and 44840e5f7a5SJayanth Dodderi Chidanand * needs to have its settings configured to be independent of other worlds. 44940e5f7a5SJayanth Dodderi Chidanand * ----------------------------------------------------------------------------- 45040e5f7a5SJayanth Dodderi Chidanand */ 45140e5f7a5SJayanth Dodderi Chidanand .macro setup_el3_execution_context 45240e5f7a5SJayanth Dodderi Chidanand 45340e5f7a5SJayanth Dodderi Chidanand /* --------------------------------------------------------------------- 45440e5f7a5SJayanth Dodderi Chidanand * The following registers need to be part of separate root context 45540e5f7a5SJayanth Dodderi Chidanand * as their values are of importance during EL3 execution. 45640e5f7a5SJayanth Dodderi Chidanand * Hence these registers are overwritten to their intital values, 45740e5f7a5SJayanth Dodderi Chidanand * irrespective of whichever world they return from to ensure EL3 has a 45840e5f7a5SJayanth Dodderi Chidanand * consistent execution context throughout the lifetime of TF-A. 45940e5f7a5SJayanth Dodderi Chidanand * 46040e5f7a5SJayanth Dodderi Chidanand * DAIF.A: Enable External Aborts and SError Interrupts at EL3. 46140e5f7a5SJayanth Dodderi Chidanand * 46240e5f7a5SJayanth Dodderi Chidanand * MDCR_EL3.SDD: Set to one to disable AArch64 Secure self-hosted debug. 46340e5f7a5SJayanth Dodderi Chidanand * Debug exceptions, other than Breakpoint Instruction exceptions, are 46440e5f7a5SJayanth Dodderi Chidanand * disabled from all ELs in Secure state. 46540e5f7a5SJayanth Dodderi Chidanand * 46640e5f7a5SJayanth Dodderi Chidanand * SCR_EL3.EA: Set to one to enable SError interrupts at EL3. 46740e5f7a5SJayanth Dodderi Chidanand * 46840e5f7a5SJayanth Dodderi Chidanand * SCR_EL3.SIF: Set to one to disable instruction fetches from 46940e5f7a5SJayanth Dodderi Chidanand * Non-secure memory. 47040e5f7a5SJayanth Dodderi Chidanand * 47140e5f7a5SJayanth Dodderi Chidanand * PMCR_EL0.DP: Set to one so that the cycle counter, 47240e5f7a5SJayanth Dodderi Chidanand * PMCCNTR_EL0 does not count when event counting is prohibited. 47340e5f7a5SJayanth Dodderi Chidanand * Necessary on PMUv3 <= p7 where MDCR_EL3.{SCCD,MCCD} are not 47440e5f7a5SJayanth Dodderi Chidanand * available. 47540e5f7a5SJayanth Dodderi Chidanand * 4760a580b51SBoyan Karatotev * CPTR_EL3.EZ: Set to one so that accesses to ZCR_EL3 do not trap 4770a580b51SBoyan Karatotev * CPTR_EL3.ESM: Set to one so that SME related registers don't trap 4780a580b51SBoyan Karatotev * 47940e5f7a5SJayanth Dodderi Chidanand * PSTATE.DIT: Set to one to enable the Data Independent Timing (DIT) 48040e5f7a5SJayanth Dodderi Chidanand * functionality, if implemented in EL3. 48140e5f7a5SJayanth Dodderi Chidanand * --------------------------------------------------------------------- 48240e5f7a5SJayanth Dodderi Chidanand */ 48340e5f7a5SJayanth Dodderi Chidanand msr daifclr, #DAIF_ABT_BIT 48440e5f7a5SJayanth Dodderi Chidanand 48540e5f7a5SJayanth Dodderi Chidanand mrs x15, mdcr_el3 48640e5f7a5SJayanth Dodderi Chidanand orr x15, x15, #MDCR_SDD_BIT 48740e5f7a5SJayanth Dodderi Chidanand msr mdcr_el3, x15 48840e5f7a5SJayanth Dodderi Chidanand 48940e5f7a5SJayanth Dodderi Chidanand mrs x15, scr_el3 49040e5f7a5SJayanth Dodderi Chidanand orr x15, x15, #SCR_EA_BIT 49140e5f7a5SJayanth Dodderi Chidanand orr x15, x15, #SCR_SIF_BIT 492*45218c64SBoyan Karatotev bic x15, x15, #SCR_TRNDR_BIT 49340e5f7a5SJayanth Dodderi Chidanand msr scr_el3, x15 49440e5f7a5SJayanth Dodderi Chidanand 49540e5f7a5SJayanth Dodderi Chidanand mrs x15, pmcr_el0 49640e5f7a5SJayanth Dodderi Chidanand orr x15, x15, #PMCR_EL0_DP_BIT 49740e5f7a5SJayanth Dodderi Chidanand msr pmcr_el0, x15 49840e5f7a5SJayanth Dodderi Chidanand 4990a580b51SBoyan Karatotev mrs x15, cptr_el3 5000a580b51SBoyan Karatotev orr x15, x15, #CPTR_EZ_BIT 5010a580b51SBoyan Karatotev orr x15, x15, #ESM_BIT 5020a580b51SBoyan Karatotev msr cptr_el3, x15 5030a580b51SBoyan Karatotev 50440e5f7a5SJayanth Dodderi Chidanand#if ENABLE_FEAT_DIT 50540e5f7a5SJayanth Dodderi Chidanand#if ENABLE_FEAT_DIT > 1 50640e5f7a5SJayanth Dodderi Chidanand mrs x15, id_aa64pfr0_el1 50740e5f7a5SJayanth Dodderi Chidanand ubfx x15, x15, #ID_AA64PFR0_DIT_SHIFT, #ID_AA64PFR0_DIT_LENGTH 50840e5f7a5SJayanth Dodderi Chidanand cbz x15, 1f 50940e5f7a5SJayanth Dodderi Chidanand#endif 51040e5f7a5SJayanth Dodderi Chidanand mov x15, #DIT_BIT 51140e5f7a5SJayanth Dodderi Chidanand msr DIT, x15 51240e5f7a5SJayanth Dodderi Chidanand 1: 51340e5f7a5SJayanth Dodderi Chidanand#endif 51440e5f7a5SJayanth Dodderi Chidanand 51540e5f7a5SJayanth Dodderi Chidanand isb 51640e5f7a5SJayanth Dodderi Chidanand .endm 51740e5f7a5SJayanth Dodderi Chidanand 518f5478dedSAntonio Nino Diaz#endif /* EL3_COMMON_MACROS_S */ 519