1c11ba852SSoby Mathew/* 2d50ece03SAntonio Nino Diaz * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved. 3c11ba852SSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5c11ba852SSoby Mathew */ 6c11ba852SSoby Mathew 7c11ba852SSoby Mathew#include <arch.h> 8c11ba852SSoby Mathew#include <asm_macros.S> 9c11ba852SSoby Mathew#include <bl_common.h> 10c11ba852SSoby Mathew#include <context.h> 113bdf0e5dSYatharth Kochar#include <el3_common_macros.S> 12c11ba852SSoby Mathew#include <runtime_svc.h> 13c11ba852SSoby Mathew#include <smcc_helpers.h> 14c11ba852SSoby Mathew#include <smcc_macros.S> 15d50ece03SAntonio Nino Diaz#include <xlat_tables_defs.h> 16c11ba852SSoby Mathew 17c11ba852SSoby Mathew .globl sp_min_vector_table 18c11ba852SSoby Mathew .globl sp_min_entrypoint 19c11ba852SSoby Mathew .globl sp_min_warm_entrypoint 20c11ba852SSoby Mathew 2171816096SEtienne Carriere .macro route_fiq_to_sp_min reg 2271816096SEtienne Carriere /* ----------------------------------------------------- 2371816096SEtienne Carriere * FIQs are secure interrupts trapped by Monitor and non 2471816096SEtienne Carriere * secure is not allowed to mask the FIQs. 2571816096SEtienne Carriere * ----------------------------------------------------- 2671816096SEtienne Carriere */ 2771816096SEtienne Carriere ldcopr \reg, SCR 2871816096SEtienne Carriere orr \reg, \reg, #SCR_FIQ_BIT 2971816096SEtienne Carriere bic \reg, \reg, #SCR_FW_BIT 3071816096SEtienne Carriere stcopr \reg, SCR 3171816096SEtienne Carriere .endm 323bdf0e5dSYatharth Kochar 33*70896274SEtienne Carriere .macro clrex_on_monitor_entry 34*70896274SEtienne Carriere#if (ARM_ARCH_MAJOR == 7) 35*70896274SEtienne Carriere /* 36*70896274SEtienne Carriere * ARMv7 architectures need to clear the exclusive access when 37*70896274SEtienne Carriere * entering Monitor mode. 38*70896274SEtienne Carriere */ 39*70896274SEtienne Carriere clrex 40*70896274SEtienne Carriere#endif 41*70896274SEtienne Carriere .endm 42*70896274SEtienne Carriere 433bdf0e5dSYatharth Kocharvector_base sp_min_vector_table 44c11ba852SSoby Mathew b sp_min_entrypoint 45c11ba852SSoby Mathew b plat_panic_handler /* Undef */ 46c11ba852SSoby Mathew b handle_smc /* Syscall */ 47c11ba852SSoby Mathew b plat_panic_handler /* Prefetch abort */ 48c11ba852SSoby Mathew b plat_panic_handler /* Data abort */ 49c11ba852SSoby Mathew b plat_panic_handler /* Reserved */ 50c11ba852SSoby Mathew b plat_panic_handler /* IRQ */ 5171816096SEtienne Carriere b handle_fiq /* FIQ */ 52c11ba852SSoby Mathew 53c11ba852SSoby Mathew 54c11ba852SSoby Mathew/* 55c11ba852SSoby Mathew * The Cold boot/Reset entrypoint for SP_MIN 56c11ba852SSoby Mathew */ 57c11ba852SSoby Mathewfunc sp_min_entrypoint 583bdf0e5dSYatharth Kochar#if !RESET_TO_SP_MIN 593bdf0e5dSYatharth Kochar /* --------------------------------------------------------------- 603bdf0e5dSYatharth Kochar * Preceding bootloader has populated r0 with a pointer to a 613bdf0e5dSYatharth Kochar * 'bl_params_t' structure & r1 with a pointer to platform 623bdf0e5dSYatharth Kochar * specific structure 633bdf0e5dSYatharth Kochar * --------------------------------------------------------------- 64c11ba852SSoby Mathew */ 653bdf0e5dSYatharth Kochar mov r11, r0 663bdf0e5dSYatharth Kochar mov r12, r1 67c11ba852SSoby Mathew 683bdf0e5dSYatharth Kochar /* --------------------------------------------------------------------- 693bdf0e5dSYatharth Kochar * For !RESET_TO_SP_MIN systems, only the primary CPU ever reaches 703bdf0e5dSYatharth Kochar * sp_min_entrypoint() during the cold boot flow, so the cold/warm boot 713bdf0e5dSYatharth Kochar * and primary/secondary CPU logic should not be executed in this case. 723bdf0e5dSYatharth Kochar * 7318f2efd6SDavid Cunado * Also, assume that the previous bootloader has already initialised the 7418f2efd6SDavid Cunado * SCTLR, including the CPU endianness, and has initialised the memory. 753bdf0e5dSYatharth Kochar * --------------------------------------------------------------------- 76c11ba852SSoby Mathew */ 773bdf0e5dSYatharth Kochar el3_entrypoint_common \ 7818f2efd6SDavid Cunado _init_sctlr=0 \ 793bdf0e5dSYatharth Kochar _warm_boot_mailbox=0 \ 803bdf0e5dSYatharth Kochar _secondary_cold_boot=0 \ 813bdf0e5dSYatharth Kochar _init_memory=0 \ 823bdf0e5dSYatharth Kochar _init_c_runtime=1 \ 833bdf0e5dSYatharth Kochar _exception_vectors=sp_min_vector_table 84c11ba852SSoby Mathew 853bdf0e5dSYatharth Kochar /* --------------------------------------------------------------------- 863bdf0e5dSYatharth Kochar * Relay the previous bootloader's arguments to the platform layer 873bdf0e5dSYatharth Kochar * --------------------------------------------------------------------- 88c11ba852SSoby Mathew */ 893bdf0e5dSYatharth Kochar mov r0, r11 903bdf0e5dSYatharth Kochar mov r1, r12 913bdf0e5dSYatharth Kochar#else 923bdf0e5dSYatharth Kochar /* --------------------------------------------------------------------- 933bdf0e5dSYatharth Kochar * For RESET_TO_SP_MIN systems which have a programmable reset address, 943bdf0e5dSYatharth Kochar * sp_min_entrypoint() is executed only on the cold boot path so we can 953bdf0e5dSYatharth Kochar * skip the warm boot mailbox mechanism. 963bdf0e5dSYatharth Kochar * --------------------------------------------------------------------- 97c11ba852SSoby Mathew */ 983bdf0e5dSYatharth Kochar el3_entrypoint_common \ 9918f2efd6SDavid Cunado _init_sctlr=1 \ 1003bdf0e5dSYatharth Kochar _warm_boot_mailbox=!PROGRAMMABLE_RESET_ADDRESS \ 1013bdf0e5dSYatharth Kochar _secondary_cold_boot=!COLD_BOOT_SINGLE_CPU \ 1023bdf0e5dSYatharth Kochar _init_memory=1 \ 1033bdf0e5dSYatharth Kochar _init_c_runtime=1 \ 1043bdf0e5dSYatharth Kochar _exception_vectors=sp_min_vector_table 105c11ba852SSoby Mathew 1063bdf0e5dSYatharth Kochar /* --------------------------------------------------------------------- 1073bdf0e5dSYatharth Kochar * For RESET_TO_SP_MIN systems, BL32 (SP_MIN) is the first bootloader 1083bdf0e5dSYatharth Kochar * to run so there's no argument to relay from a previous bootloader. 1093bdf0e5dSYatharth Kochar * Zero the arguments passed to the platform layer to reflect that. 1103bdf0e5dSYatharth Kochar * --------------------------------------------------------------------- 111c11ba852SSoby Mathew */ 1123bdf0e5dSYatharth Kochar mov r0, #0 1133bdf0e5dSYatharth Kochar mov r1, #0 1143bdf0e5dSYatharth Kochar#endif /* RESET_TO_SP_MIN */ 115c11ba852SSoby Mathew 11671816096SEtienne Carriere#if SP_MIN_WITH_SECURE_FIQ 11771816096SEtienne Carriere route_fiq_to_sp_min r4 11871816096SEtienne Carriere#endif 11971816096SEtienne Carriere 120c11ba852SSoby Mathew bl sp_min_early_platform_setup 121c11ba852SSoby Mathew bl sp_min_plat_arch_setup 122c11ba852SSoby Mathew 123c11ba852SSoby Mathew /* Jump to the main function */ 124c11ba852SSoby Mathew bl sp_min_main 125c11ba852SSoby Mathew 126c11ba852SSoby Mathew /* ------------------------------------------------------------- 127c11ba852SSoby Mathew * Clean the .data & .bss sections to main memory. This ensures 128c11ba852SSoby Mathew * that any global data which was initialised by the primary CPU 129c11ba852SSoby Mathew * is visible to secondary CPUs before they enable their data 130c11ba852SSoby Mathew * caches and participate in coherency. 131c11ba852SSoby Mathew * ------------------------------------------------------------- 132c11ba852SSoby Mathew */ 133c11ba852SSoby Mathew ldr r0, =__DATA_START__ 134c11ba852SSoby Mathew ldr r1, =__DATA_END__ 135c11ba852SSoby Mathew sub r1, r1, r0 136c11ba852SSoby Mathew bl clean_dcache_range 137c11ba852SSoby Mathew 138c11ba852SSoby Mathew ldr r0, =__BSS_START__ 139c11ba852SSoby Mathew ldr r1, =__BSS_END__ 140c11ba852SSoby Mathew sub r1, r1, r0 141c11ba852SSoby Mathew bl clean_dcache_range 142c11ba852SSoby Mathew 143c11ba852SSoby Mathew bl smc_get_next_ctx 144b6285d64SSoby Mathew 145b6285d64SSoby Mathew /* r0 points to `smc_ctx_t` */ 146b6285d64SSoby Mathew /* The PSCI cpu_context registers have been copied to `smc_ctx_t` */ 147c11ba852SSoby Mathew b sp_min_exit 148c11ba852SSoby Mathewendfunc sp_min_entrypoint 149c11ba852SSoby Mathew 1503bdf0e5dSYatharth Kochar 1513bdf0e5dSYatharth Kochar/* 1523bdf0e5dSYatharth Kochar * SMC handling function for SP_MIN. 1533bdf0e5dSYatharth Kochar */ 1543bdf0e5dSYatharth Kocharfunc handle_smc 155b6285d64SSoby Mathew /* On SMC entry, `sp` points to `smc_ctx_t`. Save `lr`. */ 156b6285d64SSoby Mathew str lr, [sp, #SMC_CTX_LR_MON] 157b6285d64SSoby Mathew 1583bdf0e5dSYatharth Kochar smcc_save_gp_mode_regs 1593bdf0e5dSYatharth Kochar 160*70896274SEtienne Carriere clrex_on_monitor_entry 161*70896274SEtienne Carriere 1629f3ee61cSSoby Mathew /* 163b6285d64SSoby Mathew * `sp` still points to `smc_ctx_t`. Save it to a register 164b6285d64SSoby Mathew * and restore the C runtime stack pointer to `sp`. 1659f3ee61cSSoby Mathew */ 166b6285d64SSoby Mathew mov r2, sp /* handle */ 167b6285d64SSoby Mathew ldr sp, [r2, #SMC_CTX_SP_MON] 168b6285d64SSoby Mathew 169b6285d64SSoby Mathew ldr r0, [r2, #SMC_CTX_SCR] 1703bdf0e5dSYatharth Kochar and r3, r0, #SCR_NS_BIT /* flags */ 1713bdf0e5dSYatharth Kochar 1723bdf0e5dSYatharth Kochar /* Switch to Secure Mode*/ 1733bdf0e5dSYatharth Kochar bic r0, #SCR_NS_BIT 1743bdf0e5dSYatharth Kochar stcopr r0, SCR 1753bdf0e5dSYatharth Kochar isb 176b6285d64SSoby Mathew 1773e61b2b5SDavid Cunado /* 1783e61b2b5SDavid Cunado * Set PMCR.DP to 1 to prohibit cycle counting whilst in Secure Mode. 1793e61b2b5SDavid Cunado * Also, the PMCR.LC field has an architecturally UNKNOWN value on reset 1803e61b2b5SDavid Cunado * and so set to 1 as ARM has deprecated use of PMCR.LC=0. 1813e61b2b5SDavid Cunado */ 1823e61b2b5SDavid Cunado ldcopr r0, PMCR 1833e61b2b5SDavid Cunado orr r0, r0, #(PMCR_LC_BIT | PMCR_DP_BIT) 1843e61b2b5SDavid Cunado stcopr r0, PMCR 1853e61b2b5SDavid Cunado 1863bdf0e5dSYatharth Kochar ldr r0, [r2, #SMC_CTX_GPREG_R0] /* smc_fid */ 1873bdf0e5dSYatharth Kochar /* Check whether an SMC64 is issued */ 1883bdf0e5dSYatharth Kochar tst r0, #(FUNCID_CC_MASK << FUNCID_CC_SHIFT) 189b6285d64SSoby Mathew beq 1f 190b6285d64SSoby Mathew /* SMC32 is not detected. Return error back to caller */ 1913bdf0e5dSYatharth Kochar mov r0, #SMC_UNK 1923bdf0e5dSYatharth Kochar str r0, [r2, #SMC_CTX_GPREG_R0] 1933bdf0e5dSYatharth Kochar mov r0, r2 194b6285d64SSoby Mathew b sp_min_exit 1953bdf0e5dSYatharth Kochar1: 196b6285d64SSoby Mathew /* SMC32 is detected */ 1973bdf0e5dSYatharth Kochar mov r1, #0 /* cookie */ 1983bdf0e5dSYatharth Kochar bl handle_runtime_svc 1993bdf0e5dSYatharth Kochar 200b6285d64SSoby Mathew /* `r0` points to `smc_ctx_t` */ 2013bdf0e5dSYatharth Kochar b sp_min_exit 2023bdf0e5dSYatharth Kocharendfunc handle_smc 2033bdf0e5dSYatharth Kochar 204c11ba852SSoby Mathew/* 20571816096SEtienne Carriere * Secure Interrupts handling function for SP_MIN. 20671816096SEtienne Carriere */ 20771816096SEtienne Carrierefunc handle_fiq 20871816096SEtienne Carriere#if !SP_MIN_WITH_SECURE_FIQ 20971816096SEtienne Carriere b plat_panic_handler 21071816096SEtienne Carriere#else 21171816096SEtienne Carriere /* FIQ has a +4 offset for lr compared to preferred return address */ 21271816096SEtienne Carriere sub lr, lr, #4 21371816096SEtienne Carriere /* On SMC entry, `sp` points to `smc_ctx_t`. Save `lr`. */ 21471816096SEtienne Carriere str lr, [sp, #SMC_CTX_LR_MON] 21571816096SEtienne Carriere 21671816096SEtienne Carriere smcc_save_gp_mode_regs 21771816096SEtienne Carriere 218*70896274SEtienne Carriere clrex_on_monitor_entry 21971816096SEtienne Carriere 22071816096SEtienne Carriere /* load run-time stack */ 22171816096SEtienne Carriere mov r2, sp 22271816096SEtienne Carriere ldr sp, [r2, #SMC_CTX_SP_MON] 22371816096SEtienne Carriere 22471816096SEtienne Carriere /* Switch to Secure Mode */ 22571816096SEtienne Carriere ldr r0, [r2, #SMC_CTX_SCR] 22671816096SEtienne Carriere bic r0, #SCR_NS_BIT 22771816096SEtienne Carriere stcopr r0, SCR 22871816096SEtienne Carriere isb 22971816096SEtienne Carriere 2303e61b2b5SDavid Cunado /* 2313e61b2b5SDavid Cunado * Set PMCR.DP to 1 to prohibit cycle counting whilst in Secure Mode. 2323e61b2b5SDavid Cunado * Also, the PMCR.LC field has an architecturally UNKNOWN value on reset 2333e61b2b5SDavid Cunado * and so set to 1 as ARM has deprecated use of PMCR.LC=0. 2343e61b2b5SDavid Cunado */ 2353e61b2b5SDavid Cunado ldcopr r0, PMCR 2363e61b2b5SDavid Cunado orr r0, r0, #(PMCR_LC_BIT | PMCR_DP_BIT) 2373e61b2b5SDavid Cunado stcopr r0, PMCR 2383e61b2b5SDavid Cunado 23971816096SEtienne Carriere push {r2, r3} 24071816096SEtienne Carriere bl sp_min_fiq 24171816096SEtienne Carriere pop {r0, r3} 24271816096SEtienne Carriere 24371816096SEtienne Carriere b sp_min_exit 24471816096SEtienne Carriere#endif 24571816096SEtienne Carriereendfunc handle_fiq 24671816096SEtienne Carriere 24771816096SEtienne Carriere/* 248c11ba852SSoby Mathew * The Warm boot entrypoint for SP_MIN. 249c11ba852SSoby Mathew */ 250c11ba852SSoby Mathewfunc sp_min_warm_entrypoint 2513bdf0e5dSYatharth Kochar /* 2523bdf0e5dSYatharth Kochar * On the warm boot path, most of the EL3 initialisations performed by 2533bdf0e5dSYatharth Kochar * 'el3_entrypoint_common' must be skipped: 2543bdf0e5dSYatharth Kochar * 2553bdf0e5dSYatharth Kochar * - Only when the platform bypasses the BL1/BL32 (SP_MIN) entrypoint by 25618f2efd6SDavid Cunado * programming the reset address do we need to initialied the SCTLR. 2573bdf0e5dSYatharth Kochar * In other cases, we assume this has been taken care by the 2583bdf0e5dSYatharth Kochar * entrypoint code. 2593bdf0e5dSYatharth Kochar * 2603bdf0e5dSYatharth Kochar * - No need to determine the type of boot, we know it is a warm boot. 2613bdf0e5dSYatharth Kochar * 2623bdf0e5dSYatharth Kochar * - Do not try to distinguish between primary and secondary CPUs, this 2633bdf0e5dSYatharth Kochar * notion only exists for a cold boot. 2643bdf0e5dSYatharth Kochar * 2653bdf0e5dSYatharth Kochar * - No need to initialise the memory or the C runtime environment, 2663bdf0e5dSYatharth Kochar * it has been done once and for all on the cold boot path. 2673bdf0e5dSYatharth Kochar */ 2683bdf0e5dSYatharth Kochar el3_entrypoint_common \ 26918f2efd6SDavid Cunado _init_sctlr=PROGRAMMABLE_RESET_ADDRESS \ 2703bdf0e5dSYatharth Kochar _warm_boot_mailbox=0 \ 2713bdf0e5dSYatharth Kochar _secondary_cold_boot=0 \ 2723bdf0e5dSYatharth Kochar _init_memory=0 \ 2733bdf0e5dSYatharth Kochar _init_c_runtime=0 \ 2743bdf0e5dSYatharth Kochar _exception_vectors=sp_min_vector_table 275c11ba852SSoby Mathew 27625a93f7cSJeenu Viswambharan /* 27725a93f7cSJeenu Viswambharan * We're about to enable MMU and participate in PSCI state coordination. 27825a93f7cSJeenu Viswambharan * 27925a93f7cSJeenu Viswambharan * The PSCI implementation invokes platform routines that enable CPUs to 28025a93f7cSJeenu Viswambharan * participate in coherency. On a system where CPUs are not 281bcc3c49cSSoby Mathew * cache-coherent without appropriate platform specific programming, 282bcc3c49cSSoby Mathew * having caches enabled until such time might lead to coherency issues 283bcc3c49cSSoby Mathew * (resulting from stale data getting speculatively fetched, among 284bcc3c49cSSoby Mathew * others). Therefore we keep data caches disabled even after enabling 285bcc3c49cSSoby Mathew * the MMU for such platforms. 28625a93f7cSJeenu Viswambharan * 287bcc3c49cSSoby Mathew * On systems with hardware-assisted coherency, or on single cluster 288bcc3c49cSSoby Mathew * platforms, such platform specific programming is not required to 289bcc3c49cSSoby Mathew * enter coherency (as CPUs already are); and there's no reason to have 290bcc3c49cSSoby Mathew * caches disabled either. 291c11ba852SSoby Mathew */ 292c11ba852SSoby Mathew mov r0, #DISABLE_DCACHE 293c11ba852SSoby Mathew bl bl32_plat_enable_mmu 294c11ba852SSoby Mathew 29571816096SEtienne Carriere#if SP_MIN_WITH_SECURE_FIQ 29671816096SEtienne Carriere route_fiq_to_sp_min r0 29771816096SEtienne Carriere#endif 29871816096SEtienne Carriere 299bcc3c49cSSoby Mathew#if HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY 300bcc3c49cSSoby Mathew ldcopr r0, SCTLR 301bcc3c49cSSoby Mathew orr r0, r0, #SCTLR_C_BIT 302bcc3c49cSSoby Mathew stcopr r0, SCTLR 303bcc3c49cSSoby Mathew isb 304bcc3c49cSSoby Mathew#endif 305bcc3c49cSSoby Mathew 306c11ba852SSoby Mathew bl sp_min_warm_boot 307c11ba852SSoby Mathew bl smc_get_next_ctx 308b6285d64SSoby Mathew /* r0 points to `smc_ctx_t` */ 309b6285d64SSoby Mathew /* The PSCI cpu_context registers have been copied to `smc_ctx_t` */ 310c11ba852SSoby Mathew b sp_min_exit 311c11ba852SSoby Mathewendfunc sp_min_warm_entrypoint 312c11ba852SSoby Mathew 313c11ba852SSoby Mathew/* 314c11ba852SSoby Mathew * The function to restore the registers from SMC context and return 315c11ba852SSoby Mathew * to the mode restored to SPSR. 316c11ba852SSoby Mathew * 317c11ba852SSoby Mathew * Arguments : r0 must point to the SMC context to restore from. 318c11ba852SSoby Mathew */ 319c11ba852SSoby Mathewfunc sp_min_exit 320b6285d64SSoby Mathew monitor_exit 321c11ba852SSoby Mathewendfunc sp_min_exit 322