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 213bdf0e5dSYatharth Kochar 223bdf0e5dSYatharth Kocharvector_base sp_min_vector_table 23c11ba852SSoby Mathew b sp_min_entrypoint 24c11ba852SSoby Mathew b plat_panic_handler /* Undef */ 25c11ba852SSoby Mathew b handle_smc /* Syscall */ 26c11ba852SSoby Mathew b plat_panic_handler /* Prefetch abort */ 27c11ba852SSoby Mathew b plat_panic_handler /* Data abort */ 28c11ba852SSoby Mathew b plat_panic_handler /* Reserved */ 29c11ba852SSoby Mathew b plat_panic_handler /* IRQ */ 30c11ba852SSoby Mathew b plat_panic_handler /* FIQ */ 31c11ba852SSoby Mathew 32c11ba852SSoby Mathew 33c11ba852SSoby Mathew/* 34c11ba852SSoby Mathew * The Cold boot/Reset entrypoint for SP_MIN 35c11ba852SSoby Mathew */ 36c11ba852SSoby Mathewfunc sp_min_entrypoint 373bdf0e5dSYatharth Kochar#if !RESET_TO_SP_MIN 383bdf0e5dSYatharth Kochar /* --------------------------------------------------------------- 393bdf0e5dSYatharth Kochar * Preceding bootloader has populated r0 with a pointer to a 403bdf0e5dSYatharth Kochar * 'bl_params_t' structure & r1 with a pointer to platform 413bdf0e5dSYatharth Kochar * specific structure 423bdf0e5dSYatharth Kochar * --------------------------------------------------------------- 43c11ba852SSoby Mathew */ 443bdf0e5dSYatharth Kochar mov r11, r0 453bdf0e5dSYatharth Kochar mov r12, r1 46c11ba852SSoby Mathew 473bdf0e5dSYatharth Kochar /* --------------------------------------------------------------------- 483bdf0e5dSYatharth Kochar * For !RESET_TO_SP_MIN systems, only the primary CPU ever reaches 493bdf0e5dSYatharth Kochar * sp_min_entrypoint() during the cold boot flow, so the cold/warm boot 503bdf0e5dSYatharth Kochar * and primary/secondary CPU logic should not be executed in this case. 513bdf0e5dSYatharth Kochar * 52*18f2efd6SDavid Cunado * Also, assume that the previous bootloader has already initialised the 53*18f2efd6SDavid Cunado * SCTLR, including the CPU endianness, and has initialised the memory. 543bdf0e5dSYatharth Kochar * --------------------------------------------------------------------- 55c11ba852SSoby Mathew */ 563bdf0e5dSYatharth Kochar el3_entrypoint_common \ 57*18f2efd6SDavid Cunado _init_sctlr=0 \ 583bdf0e5dSYatharth Kochar _warm_boot_mailbox=0 \ 593bdf0e5dSYatharth Kochar _secondary_cold_boot=0 \ 603bdf0e5dSYatharth Kochar _init_memory=0 \ 613bdf0e5dSYatharth Kochar _init_c_runtime=1 \ 623bdf0e5dSYatharth Kochar _exception_vectors=sp_min_vector_table 63c11ba852SSoby Mathew 643bdf0e5dSYatharth Kochar /* --------------------------------------------------------------------- 653bdf0e5dSYatharth Kochar * Relay the previous bootloader's arguments to the platform layer 663bdf0e5dSYatharth Kochar * --------------------------------------------------------------------- 67c11ba852SSoby Mathew */ 683bdf0e5dSYatharth Kochar mov r0, r11 693bdf0e5dSYatharth Kochar mov r1, r12 703bdf0e5dSYatharth Kochar#else 713bdf0e5dSYatharth Kochar /* --------------------------------------------------------------------- 723bdf0e5dSYatharth Kochar * For RESET_TO_SP_MIN systems which have a programmable reset address, 733bdf0e5dSYatharth Kochar * sp_min_entrypoint() is executed only on the cold boot path so we can 743bdf0e5dSYatharth Kochar * skip the warm boot mailbox mechanism. 753bdf0e5dSYatharth Kochar * --------------------------------------------------------------------- 76c11ba852SSoby Mathew */ 773bdf0e5dSYatharth Kochar el3_entrypoint_common \ 78*18f2efd6SDavid Cunado _init_sctlr=1 \ 793bdf0e5dSYatharth Kochar _warm_boot_mailbox=!PROGRAMMABLE_RESET_ADDRESS \ 803bdf0e5dSYatharth Kochar _secondary_cold_boot=!COLD_BOOT_SINGLE_CPU \ 813bdf0e5dSYatharth Kochar _init_memory=1 \ 823bdf0e5dSYatharth Kochar _init_c_runtime=1 \ 833bdf0e5dSYatharth Kochar _exception_vectors=sp_min_vector_table 84c11ba852SSoby Mathew 853bdf0e5dSYatharth Kochar /* --------------------------------------------------------------------- 863bdf0e5dSYatharth Kochar * For RESET_TO_SP_MIN systems, BL32 (SP_MIN) is the first bootloader 873bdf0e5dSYatharth Kochar * to run so there's no argument to relay from a previous bootloader. 883bdf0e5dSYatharth Kochar * Zero the arguments passed to the platform layer to reflect that. 893bdf0e5dSYatharth Kochar * --------------------------------------------------------------------- 90c11ba852SSoby Mathew */ 913bdf0e5dSYatharth Kochar mov r0, #0 923bdf0e5dSYatharth Kochar mov r1, #0 933bdf0e5dSYatharth Kochar#endif /* RESET_TO_SP_MIN */ 94c11ba852SSoby Mathew 95c11ba852SSoby Mathew bl sp_min_early_platform_setup 96c11ba852SSoby Mathew bl sp_min_plat_arch_setup 97c11ba852SSoby Mathew 98c11ba852SSoby Mathew /* Jump to the main function */ 99c11ba852SSoby Mathew bl sp_min_main 100c11ba852SSoby Mathew 101c11ba852SSoby Mathew /* ------------------------------------------------------------- 102c11ba852SSoby Mathew * Clean the .data & .bss sections to main memory. This ensures 103c11ba852SSoby Mathew * that any global data which was initialised by the primary CPU 104c11ba852SSoby Mathew * is visible to secondary CPUs before they enable their data 105c11ba852SSoby Mathew * caches and participate in coherency. 106c11ba852SSoby Mathew * ------------------------------------------------------------- 107c11ba852SSoby Mathew */ 108c11ba852SSoby Mathew ldr r0, =__DATA_START__ 109c11ba852SSoby Mathew ldr r1, =__DATA_END__ 110c11ba852SSoby Mathew sub r1, r1, r0 111c11ba852SSoby Mathew bl clean_dcache_range 112c11ba852SSoby Mathew 113c11ba852SSoby Mathew ldr r0, =__BSS_START__ 114c11ba852SSoby Mathew ldr r1, =__BSS_END__ 115c11ba852SSoby Mathew sub r1, r1, r0 116c11ba852SSoby Mathew bl clean_dcache_range 117c11ba852SSoby Mathew 118c11ba852SSoby Mathew bl smc_get_next_ctx 119b6285d64SSoby Mathew 120b6285d64SSoby Mathew /* r0 points to `smc_ctx_t` */ 121b6285d64SSoby Mathew /* The PSCI cpu_context registers have been copied to `smc_ctx_t` */ 122c11ba852SSoby Mathew b sp_min_exit 123c11ba852SSoby Mathewendfunc sp_min_entrypoint 124c11ba852SSoby Mathew 1253bdf0e5dSYatharth Kochar 1263bdf0e5dSYatharth Kochar/* 1273bdf0e5dSYatharth Kochar * SMC handling function for SP_MIN. 1283bdf0e5dSYatharth Kochar */ 1293bdf0e5dSYatharth Kocharfunc handle_smc 130b6285d64SSoby Mathew /* On SMC entry, `sp` points to `smc_ctx_t`. Save `lr`. */ 131b6285d64SSoby Mathew str lr, [sp, #SMC_CTX_LR_MON] 132b6285d64SSoby Mathew 1333bdf0e5dSYatharth Kochar smcc_save_gp_mode_regs 1343bdf0e5dSYatharth Kochar 1359f3ee61cSSoby Mathew /* 136b6285d64SSoby Mathew * `sp` still points to `smc_ctx_t`. Save it to a register 137b6285d64SSoby Mathew * and restore the C runtime stack pointer to `sp`. 1389f3ee61cSSoby Mathew */ 139b6285d64SSoby Mathew mov r2, sp /* handle */ 140b6285d64SSoby Mathew ldr sp, [r2, #SMC_CTX_SP_MON] 141b6285d64SSoby Mathew 142b6285d64SSoby Mathew ldr r0, [r2, #SMC_CTX_SCR] 1433bdf0e5dSYatharth Kochar and r3, r0, #SCR_NS_BIT /* flags */ 1443bdf0e5dSYatharth Kochar 1453bdf0e5dSYatharth Kochar /* Switch to Secure Mode*/ 1463bdf0e5dSYatharth Kochar bic r0, #SCR_NS_BIT 1473bdf0e5dSYatharth Kochar stcopr r0, SCR 1483bdf0e5dSYatharth Kochar isb 149b6285d64SSoby Mathew 1503bdf0e5dSYatharth Kochar ldr r0, [r2, #SMC_CTX_GPREG_R0] /* smc_fid */ 1513bdf0e5dSYatharth Kochar /* Check whether an SMC64 is issued */ 1523bdf0e5dSYatharth Kochar tst r0, #(FUNCID_CC_MASK << FUNCID_CC_SHIFT) 153b6285d64SSoby Mathew beq 1f 154b6285d64SSoby Mathew /* SMC32 is not detected. Return error back to caller */ 1553bdf0e5dSYatharth Kochar mov r0, #SMC_UNK 1563bdf0e5dSYatharth Kochar str r0, [r2, #SMC_CTX_GPREG_R0] 1573bdf0e5dSYatharth Kochar mov r0, r2 158b6285d64SSoby Mathew b sp_min_exit 1593bdf0e5dSYatharth Kochar1: 160b6285d64SSoby Mathew /* SMC32 is detected */ 1613bdf0e5dSYatharth Kochar mov r1, #0 /* cookie */ 1623bdf0e5dSYatharth Kochar bl handle_runtime_svc 1633bdf0e5dSYatharth Kochar 164b6285d64SSoby Mathew /* `r0` points to `smc_ctx_t` */ 1653bdf0e5dSYatharth Kochar b sp_min_exit 1663bdf0e5dSYatharth Kocharendfunc handle_smc 1673bdf0e5dSYatharth Kochar 168c11ba852SSoby Mathew/* 169c11ba852SSoby Mathew * The Warm boot entrypoint for SP_MIN. 170c11ba852SSoby Mathew */ 171c11ba852SSoby Mathewfunc sp_min_warm_entrypoint 1723bdf0e5dSYatharth Kochar /* 1733bdf0e5dSYatharth Kochar * On the warm boot path, most of the EL3 initialisations performed by 1743bdf0e5dSYatharth Kochar * 'el3_entrypoint_common' must be skipped: 1753bdf0e5dSYatharth Kochar * 1763bdf0e5dSYatharth Kochar * - Only when the platform bypasses the BL1/BL32 (SP_MIN) entrypoint by 177*18f2efd6SDavid Cunado * programming the reset address do we need to initialied the SCTLR. 1783bdf0e5dSYatharth Kochar * In other cases, we assume this has been taken care by the 1793bdf0e5dSYatharth Kochar * entrypoint code. 1803bdf0e5dSYatharth Kochar * 1813bdf0e5dSYatharth Kochar * - No need to determine the type of boot, we know it is a warm boot. 1823bdf0e5dSYatharth Kochar * 1833bdf0e5dSYatharth Kochar * - Do not try to distinguish between primary and secondary CPUs, this 1843bdf0e5dSYatharth Kochar * notion only exists for a cold boot. 1853bdf0e5dSYatharth Kochar * 1863bdf0e5dSYatharth Kochar * - No need to initialise the memory or the C runtime environment, 1873bdf0e5dSYatharth Kochar * it has been done once and for all on the cold boot path. 1883bdf0e5dSYatharth Kochar */ 1893bdf0e5dSYatharth Kochar el3_entrypoint_common \ 190*18f2efd6SDavid Cunado _init_sctlr=PROGRAMMABLE_RESET_ADDRESS \ 1913bdf0e5dSYatharth Kochar _warm_boot_mailbox=0 \ 1923bdf0e5dSYatharth Kochar _secondary_cold_boot=0 \ 1933bdf0e5dSYatharth Kochar _init_memory=0 \ 1943bdf0e5dSYatharth Kochar _init_c_runtime=0 \ 1953bdf0e5dSYatharth Kochar _exception_vectors=sp_min_vector_table 196c11ba852SSoby Mathew 19725a93f7cSJeenu Viswambharan /* 19825a93f7cSJeenu Viswambharan * We're about to enable MMU and participate in PSCI state coordination. 19925a93f7cSJeenu Viswambharan * 20025a93f7cSJeenu Viswambharan * The PSCI implementation invokes platform routines that enable CPUs to 20125a93f7cSJeenu Viswambharan * participate in coherency. On a system where CPUs are not 202bcc3c49cSSoby Mathew * cache-coherent without appropriate platform specific programming, 203bcc3c49cSSoby Mathew * having caches enabled until such time might lead to coherency issues 204bcc3c49cSSoby Mathew * (resulting from stale data getting speculatively fetched, among 205bcc3c49cSSoby Mathew * others). Therefore we keep data caches disabled even after enabling 206bcc3c49cSSoby Mathew * the MMU for such platforms. 20725a93f7cSJeenu Viswambharan * 208bcc3c49cSSoby Mathew * On systems with hardware-assisted coherency, or on single cluster 209bcc3c49cSSoby Mathew * platforms, such platform specific programming is not required to 210bcc3c49cSSoby Mathew * enter coherency (as CPUs already are); and there's no reason to have 211bcc3c49cSSoby Mathew * caches disabled either. 212c11ba852SSoby Mathew */ 213c11ba852SSoby Mathew mov r0, #DISABLE_DCACHE 214c11ba852SSoby Mathew bl bl32_plat_enable_mmu 215c11ba852SSoby Mathew 216bcc3c49cSSoby Mathew#if HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY 217bcc3c49cSSoby Mathew ldcopr r0, SCTLR 218bcc3c49cSSoby Mathew orr r0, r0, #SCTLR_C_BIT 219bcc3c49cSSoby Mathew stcopr r0, SCTLR 220bcc3c49cSSoby Mathew isb 221bcc3c49cSSoby Mathew#endif 222bcc3c49cSSoby Mathew 223c11ba852SSoby Mathew bl sp_min_warm_boot 224c11ba852SSoby Mathew bl smc_get_next_ctx 225b6285d64SSoby Mathew /* r0 points to `smc_ctx_t` */ 226b6285d64SSoby Mathew /* The PSCI cpu_context registers have been copied to `smc_ctx_t` */ 227c11ba852SSoby Mathew b sp_min_exit 228c11ba852SSoby Mathewendfunc sp_min_warm_entrypoint 229c11ba852SSoby Mathew 230c11ba852SSoby Mathew/* 231c11ba852SSoby Mathew * The function to restore the registers from SMC context and return 232c11ba852SSoby Mathew * to the mode restored to SPSR. 233c11ba852SSoby Mathew * 234c11ba852SSoby Mathew * Arguments : r0 must point to the SMC context to restore from. 235c11ba852SSoby Mathew */ 236c11ba852SSoby Mathewfunc sp_min_exit 237b6285d64SSoby Mathew monitor_exit 238c11ba852SSoby Mathewendfunc sp_min_exit 239