xref: /rk3399_ARM-atf/bl32/sp_min/aarch32/entrypoint.S (revision 7343505d9661ab6481e99fa96d60f2a8447a4565)
1c11ba852SSoby Mathew/*
2*7343505dSDimitris Papastamos * Copyright (c) 2016-2018, 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
20*7343505dSDimitris Papastamos	.globl	sp_min_handle_smc
21*7343505dSDimitris Papastamos	.globl	sp_min_handle_fiq
22c11ba852SSoby Mathew
2371816096SEtienne Carriere	.macro route_fiq_to_sp_min reg
2471816096SEtienne Carriere		/* -----------------------------------------------------
2571816096SEtienne Carriere		 * FIQs are secure interrupts trapped by Monitor and non
2671816096SEtienne Carriere		 * secure is not allowed to mask the FIQs.
2771816096SEtienne Carriere		 * -----------------------------------------------------
2871816096SEtienne Carriere		 */
2971816096SEtienne Carriere		ldcopr	\reg, SCR
3071816096SEtienne Carriere		orr	\reg, \reg, #SCR_FIQ_BIT
3171816096SEtienne Carriere		bic	\reg, \reg, #SCR_FW_BIT
3271816096SEtienne Carriere		stcopr	\reg, SCR
3371816096SEtienne Carriere	.endm
343bdf0e5dSYatharth Kochar
3570896274SEtienne Carriere	.macro clrex_on_monitor_entry
3670896274SEtienne Carriere#if (ARM_ARCH_MAJOR == 7)
3770896274SEtienne Carriere	/*
3870896274SEtienne Carriere	 * ARMv7 architectures need to clear the exclusive access when
3970896274SEtienne Carriere	 * entering Monitor mode.
4070896274SEtienne Carriere	 */
4170896274SEtienne Carriere	clrex
4270896274SEtienne Carriere#endif
4370896274SEtienne Carriere	.endm
4470896274SEtienne Carriere
453bdf0e5dSYatharth Kocharvector_base sp_min_vector_table
46c11ba852SSoby Mathew	b	sp_min_entrypoint
47c11ba852SSoby Mathew	b	plat_panic_handler	/* Undef */
48*7343505dSDimitris Papastamos	b	sp_min_handle_smc	/* Syscall */
49c11ba852SSoby Mathew	b	plat_panic_handler	/* Prefetch abort */
50c11ba852SSoby Mathew	b	plat_panic_handler	/* Data abort */
51c11ba852SSoby Mathew	b	plat_panic_handler	/* Reserved */
52c11ba852SSoby Mathew	b	plat_panic_handler	/* IRQ */
53*7343505dSDimitris Papastamos	b	sp_min_handle_fiq	/* FIQ */
54c11ba852SSoby Mathew
55c11ba852SSoby Mathew
56c11ba852SSoby Mathew/*
57c11ba852SSoby Mathew * The Cold boot/Reset entrypoint for SP_MIN
58c11ba852SSoby Mathew */
59c11ba852SSoby Mathewfunc sp_min_entrypoint
603bdf0e5dSYatharth Kochar#if !RESET_TO_SP_MIN
613bdf0e5dSYatharth Kochar	/* ---------------------------------------------------------------
623bdf0e5dSYatharth Kochar	 * Preceding bootloader has populated r0 with a pointer to a
633bdf0e5dSYatharth Kochar	 * 'bl_params_t' structure & r1 with a pointer to platform
643bdf0e5dSYatharth Kochar	 * specific structure
653bdf0e5dSYatharth Kochar	 * ---------------------------------------------------------------
66c11ba852SSoby Mathew	 */
673bdf0e5dSYatharth Kochar	mov	r11, r0
683bdf0e5dSYatharth Kochar	mov	r12, r1
69c11ba852SSoby Mathew
703bdf0e5dSYatharth Kochar	/* ---------------------------------------------------------------------
713bdf0e5dSYatharth Kochar	 * For !RESET_TO_SP_MIN systems, only the primary CPU ever reaches
723bdf0e5dSYatharth Kochar	 * sp_min_entrypoint() during the cold boot flow, so the cold/warm boot
733bdf0e5dSYatharth Kochar	 * and primary/secondary CPU logic should not be executed in this case.
743bdf0e5dSYatharth Kochar	 *
7518f2efd6SDavid Cunado	 * Also, assume that the previous bootloader has already initialised the
7618f2efd6SDavid Cunado	 * SCTLR, including the CPU endianness, and has initialised the memory.
773bdf0e5dSYatharth Kochar	 * ---------------------------------------------------------------------
78c11ba852SSoby Mathew	 */
793bdf0e5dSYatharth Kochar	el3_entrypoint_common					\
8018f2efd6SDavid Cunado		_init_sctlr=0					\
813bdf0e5dSYatharth Kochar		_warm_boot_mailbox=0				\
823bdf0e5dSYatharth Kochar		_secondary_cold_boot=0				\
833bdf0e5dSYatharth Kochar		_init_memory=0					\
843bdf0e5dSYatharth Kochar		_init_c_runtime=1				\
853bdf0e5dSYatharth Kochar		_exception_vectors=sp_min_vector_table
86c11ba852SSoby Mathew
873bdf0e5dSYatharth Kochar	/* ---------------------------------------------------------------------
883bdf0e5dSYatharth Kochar	 * Relay the previous bootloader's arguments to the platform layer
893bdf0e5dSYatharth Kochar	 * ---------------------------------------------------------------------
90c11ba852SSoby Mathew	 */
913bdf0e5dSYatharth Kochar	mov	r0, r11
923bdf0e5dSYatharth Kochar	mov	r1, r12
933bdf0e5dSYatharth Kochar#else
943bdf0e5dSYatharth Kochar	/* ---------------------------------------------------------------------
953bdf0e5dSYatharth Kochar	 * For RESET_TO_SP_MIN systems which have a programmable reset address,
963bdf0e5dSYatharth Kochar	 * sp_min_entrypoint() is executed only on the cold boot path so we can
973bdf0e5dSYatharth Kochar	 * skip the warm boot mailbox mechanism.
983bdf0e5dSYatharth Kochar	 * ---------------------------------------------------------------------
99c11ba852SSoby Mathew	 */
1003bdf0e5dSYatharth Kochar	el3_entrypoint_common					\
10118f2efd6SDavid Cunado		_init_sctlr=1					\
1023bdf0e5dSYatharth Kochar		_warm_boot_mailbox=!PROGRAMMABLE_RESET_ADDRESS	\
1033bdf0e5dSYatharth Kochar		_secondary_cold_boot=!COLD_BOOT_SINGLE_CPU	\
1043bdf0e5dSYatharth Kochar		_init_memory=1					\
1053bdf0e5dSYatharth Kochar		_init_c_runtime=1				\
1063bdf0e5dSYatharth Kochar		_exception_vectors=sp_min_vector_table
107c11ba852SSoby Mathew
1083bdf0e5dSYatharth Kochar	/* ---------------------------------------------------------------------
1093bdf0e5dSYatharth Kochar	 * For RESET_TO_SP_MIN systems, BL32 (SP_MIN) is the first bootloader
1103bdf0e5dSYatharth Kochar	 * to run so there's no argument to relay from a previous bootloader.
1113bdf0e5dSYatharth Kochar	 * Zero the arguments passed to the platform layer to reflect that.
1123bdf0e5dSYatharth Kochar	 * ---------------------------------------------------------------------
113c11ba852SSoby Mathew	 */
1143bdf0e5dSYatharth Kochar	mov	r0, #0
1153bdf0e5dSYatharth Kochar	mov	r1, #0
1163bdf0e5dSYatharth Kochar#endif /* RESET_TO_SP_MIN */
117c11ba852SSoby Mathew
11871816096SEtienne Carriere#if SP_MIN_WITH_SECURE_FIQ
11971816096SEtienne Carriere	route_fiq_to_sp_min r4
12071816096SEtienne Carriere#endif
12171816096SEtienne Carriere
122c11ba852SSoby Mathew	bl	sp_min_early_platform_setup
123c11ba852SSoby Mathew	bl	sp_min_plat_arch_setup
124c11ba852SSoby Mathew
125c11ba852SSoby Mathew	/* Jump to the main function */
126c11ba852SSoby Mathew	bl	sp_min_main
127c11ba852SSoby Mathew
128c11ba852SSoby Mathew	/* -------------------------------------------------------------
129c11ba852SSoby Mathew	 * Clean the .data & .bss sections to main memory. This ensures
130c11ba852SSoby Mathew	 * that any global data which was initialised by the primary CPU
131c11ba852SSoby Mathew	 * is visible to secondary CPUs before they enable their data
132c11ba852SSoby Mathew	 * caches and participate in coherency.
133c11ba852SSoby Mathew	 * -------------------------------------------------------------
134c11ba852SSoby Mathew	 */
135c11ba852SSoby Mathew	ldr	r0, =__DATA_START__
136c11ba852SSoby Mathew	ldr	r1, =__DATA_END__
137c11ba852SSoby Mathew	sub	r1, r1, r0
138c11ba852SSoby Mathew	bl	clean_dcache_range
139c11ba852SSoby Mathew
140c11ba852SSoby Mathew	ldr	r0, =__BSS_START__
141c11ba852SSoby Mathew	ldr	r1, =__BSS_END__
142c11ba852SSoby Mathew	sub	r1, r1, r0
143c11ba852SSoby Mathew	bl	clean_dcache_range
144c11ba852SSoby Mathew
145c11ba852SSoby Mathew	bl	smc_get_next_ctx
146b6285d64SSoby Mathew
147b6285d64SSoby Mathew	/* r0 points to `smc_ctx_t` */
148b6285d64SSoby Mathew	/* The PSCI cpu_context registers have been copied to `smc_ctx_t` */
149c11ba852SSoby Mathew	b	sp_min_exit
150c11ba852SSoby Mathewendfunc sp_min_entrypoint
151c11ba852SSoby Mathew
1523bdf0e5dSYatharth Kochar
1533bdf0e5dSYatharth Kochar/*
1543bdf0e5dSYatharth Kochar * SMC handling function for SP_MIN.
1553bdf0e5dSYatharth Kochar */
156*7343505dSDimitris Papastamosfunc sp_min_handle_smc
157b6285d64SSoby Mathew	/* On SMC entry, `sp` points to `smc_ctx_t`. Save `lr`. */
158b6285d64SSoby Mathew	str	lr, [sp, #SMC_CTX_LR_MON]
159b6285d64SSoby Mathew
1603bdf0e5dSYatharth Kochar	smcc_save_gp_mode_regs
1613bdf0e5dSYatharth Kochar
16270896274SEtienne Carriere	clrex_on_monitor_entry
16370896274SEtienne Carriere
1649f3ee61cSSoby Mathew	/*
165b6285d64SSoby Mathew	 * `sp` still points to `smc_ctx_t`. Save it to a register
166b6285d64SSoby Mathew	 * and restore the C runtime stack pointer to `sp`.
1679f3ee61cSSoby Mathew	 */
168b6285d64SSoby Mathew	mov	r2, sp				/* handle */
169b6285d64SSoby Mathew	ldr	sp, [r2, #SMC_CTX_SP_MON]
170b6285d64SSoby Mathew
171b6285d64SSoby Mathew	ldr	r0, [r2, #SMC_CTX_SCR]
1723bdf0e5dSYatharth Kochar	and	r3, r0, #SCR_NS_BIT		/* flags */
1733bdf0e5dSYatharth Kochar
1743bdf0e5dSYatharth Kochar	/* Switch to Secure Mode*/
1753bdf0e5dSYatharth Kochar	bic	r0, #SCR_NS_BIT
1763bdf0e5dSYatharth Kochar	stcopr	r0, SCR
1773bdf0e5dSYatharth Kochar	isb
178b6285d64SSoby Mathew
1793e61b2b5SDavid Cunado	/*
1803e61b2b5SDavid Cunado	 * Set PMCR.DP to 1 to prohibit cycle counting whilst in Secure Mode.
1813e61b2b5SDavid Cunado	 * Also, the PMCR.LC field has an architecturally UNKNOWN value on reset
1823e61b2b5SDavid Cunado	 * and so set to 1 as ARM has deprecated use of PMCR.LC=0.
1833e61b2b5SDavid Cunado	 */
1843e61b2b5SDavid Cunado	ldcopr	r0, PMCR
1853e61b2b5SDavid Cunado	orr	r0, r0, #(PMCR_LC_BIT | PMCR_DP_BIT)
1863e61b2b5SDavid Cunado	stcopr	r0, PMCR
1873e61b2b5SDavid Cunado
1883bdf0e5dSYatharth Kochar	ldr	r0, [r2, #SMC_CTX_GPREG_R0]	/* smc_fid */
1893bdf0e5dSYatharth Kochar	/* Check whether an SMC64 is issued */
1903bdf0e5dSYatharth Kochar	tst	r0, #(FUNCID_CC_MASK << FUNCID_CC_SHIFT)
191b6285d64SSoby Mathew	beq	1f
192b6285d64SSoby Mathew	/* SMC32 is not detected. Return error back to caller */
1933bdf0e5dSYatharth Kochar	mov	r0, #SMC_UNK
1943bdf0e5dSYatharth Kochar	str	r0, [r2, #SMC_CTX_GPREG_R0]
1953bdf0e5dSYatharth Kochar	mov	r0, r2
196b6285d64SSoby Mathew	b	sp_min_exit
1973bdf0e5dSYatharth Kochar1:
198b6285d64SSoby Mathew	/* SMC32 is detected */
1993bdf0e5dSYatharth Kochar	mov	r1, #0				/* cookie */
2003bdf0e5dSYatharth Kochar	bl	handle_runtime_svc
2013bdf0e5dSYatharth Kochar
202b6285d64SSoby Mathew	/* `r0` points to `smc_ctx_t` */
2033bdf0e5dSYatharth Kochar	b	sp_min_exit
204*7343505dSDimitris Papastamosendfunc sp_min_handle_smc
2053bdf0e5dSYatharth Kochar
206c11ba852SSoby Mathew/*
20771816096SEtienne Carriere * Secure Interrupts handling function for SP_MIN.
20871816096SEtienne Carriere */
209*7343505dSDimitris Papastamosfunc sp_min_handle_fiq
21071816096SEtienne Carriere#if !SP_MIN_WITH_SECURE_FIQ
21171816096SEtienne Carriere	b plat_panic_handler
21271816096SEtienne Carriere#else
21371816096SEtienne Carriere	/* FIQ has a +4 offset for lr compared to preferred return address */
21471816096SEtienne Carriere	sub	lr, lr, #4
21571816096SEtienne Carriere	/* On SMC entry, `sp` points to `smc_ctx_t`. Save `lr`. */
21671816096SEtienne Carriere	str	lr, [sp, #SMC_CTX_LR_MON]
21771816096SEtienne Carriere
21871816096SEtienne Carriere	smcc_save_gp_mode_regs
21971816096SEtienne Carriere
22070896274SEtienne Carriere	clrex_on_monitor_entry
22171816096SEtienne Carriere
22271816096SEtienne Carriere	/* load run-time stack */
22371816096SEtienne Carriere	mov	r2, sp
22471816096SEtienne Carriere	ldr	sp, [r2, #SMC_CTX_SP_MON]
22571816096SEtienne Carriere
22671816096SEtienne Carriere	/* Switch to Secure Mode */
22771816096SEtienne Carriere	ldr	r0, [r2, #SMC_CTX_SCR]
22871816096SEtienne Carriere	bic	r0, #SCR_NS_BIT
22971816096SEtienne Carriere	stcopr	r0, SCR
23071816096SEtienne Carriere	isb
23171816096SEtienne Carriere
2323e61b2b5SDavid Cunado	/*
2333e61b2b5SDavid Cunado	 * Set PMCR.DP to 1 to prohibit cycle counting whilst in Secure Mode.
2343e61b2b5SDavid Cunado	 * Also, the PMCR.LC field has an architecturally UNKNOWN value on reset
2353e61b2b5SDavid Cunado	 * and so set to 1 as ARM has deprecated use of PMCR.LC=0.
2363e61b2b5SDavid Cunado	 */
2373e61b2b5SDavid Cunado	ldcopr	r0, PMCR
2383e61b2b5SDavid Cunado	orr	r0, r0, #(PMCR_LC_BIT | PMCR_DP_BIT)
2393e61b2b5SDavid Cunado	stcopr	r0, PMCR
2403e61b2b5SDavid Cunado
24171816096SEtienne Carriere	push	{r2, r3}
24271816096SEtienne Carriere	bl	sp_min_fiq
24371816096SEtienne Carriere	pop	{r0, r3}
24471816096SEtienne Carriere
24571816096SEtienne Carriere	b	sp_min_exit
24671816096SEtienne Carriere#endif
247*7343505dSDimitris Papastamosendfunc sp_min_handle_fiq
24871816096SEtienne Carriere
24971816096SEtienne Carriere/*
250c11ba852SSoby Mathew * The Warm boot entrypoint for SP_MIN.
251c11ba852SSoby Mathew */
252c11ba852SSoby Mathewfunc sp_min_warm_entrypoint
2533bdf0e5dSYatharth Kochar	/*
2543bdf0e5dSYatharth Kochar	 * On the warm boot path, most of the EL3 initialisations performed by
2553bdf0e5dSYatharth Kochar	 * 'el3_entrypoint_common' must be skipped:
2563bdf0e5dSYatharth Kochar	 *
2573bdf0e5dSYatharth Kochar	 *  - Only when the platform bypasses the BL1/BL32 (SP_MIN) entrypoint by
25818f2efd6SDavid Cunado	 *    programming the reset address do we need to initialied the SCTLR.
2593bdf0e5dSYatharth Kochar	 *    In other cases, we assume this has been taken care by the
2603bdf0e5dSYatharth Kochar	 *    entrypoint code.
2613bdf0e5dSYatharth Kochar	 *
2623bdf0e5dSYatharth Kochar	 *  - No need to determine the type of boot, we know it is a warm boot.
2633bdf0e5dSYatharth Kochar	 *
2643bdf0e5dSYatharth Kochar	 *  - Do not try to distinguish between primary and secondary CPUs, this
2653bdf0e5dSYatharth Kochar	 *    notion only exists for a cold boot.
2663bdf0e5dSYatharth Kochar	 *
2673bdf0e5dSYatharth Kochar	 *  - No need to initialise the memory or the C runtime environment,
2683bdf0e5dSYatharth Kochar	 *    it has been done once and for all on the cold boot path.
2693bdf0e5dSYatharth Kochar	 */
2703bdf0e5dSYatharth Kochar	el3_entrypoint_common					\
27118f2efd6SDavid Cunado		_init_sctlr=PROGRAMMABLE_RESET_ADDRESS		\
2723bdf0e5dSYatharth Kochar		_warm_boot_mailbox=0				\
2733bdf0e5dSYatharth Kochar		_secondary_cold_boot=0				\
2743bdf0e5dSYatharth Kochar		_init_memory=0					\
2753bdf0e5dSYatharth Kochar		_init_c_runtime=0				\
2763bdf0e5dSYatharth Kochar		_exception_vectors=sp_min_vector_table
277c11ba852SSoby Mathew
27825a93f7cSJeenu Viswambharan	/*
27925a93f7cSJeenu Viswambharan	 * We're about to enable MMU and participate in PSCI state coordination.
28025a93f7cSJeenu Viswambharan	 *
28125a93f7cSJeenu Viswambharan	 * The PSCI implementation invokes platform routines that enable CPUs to
28225a93f7cSJeenu Viswambharan	 * participate in coherency. On a system where CPUs are not
283bcc3c49cSSoby Mathew	 * cache-coherent without appropriate platform specific programming,
284bcc3c49cSSoby Mathew	 * having caches enabled until such time might lead to coherency issues
285bcc3c49cSSoby Mathew	 * (resulting from stale data getting speculatively fetched, among
286bcc3c49cSSoby Mathew	 * others). Therefore we keep data caches disabled even after enabling
287bcc3c49cSSoby Mathew	 * the MMU for such platforms.
28825a93f7cSJeenu Viswambharan	 *
289bcc3c49cSSoby Mathew	 * On systems with hardware-assisted coherency, or on single cluster
290bcc3c49cSSoby Mathew	 * platforms, such platform specific programming is not required to
291bcc3c49cSSoby Mathew	 * enter coherency (as CPUs already are); and there's no reason to have
292bcc3c49cSSoby Mathew	 * caches disabled either.
293c11ba852SSoby Mathew	 */
294c11ba852SSoby Mathew	mov	r0, #DISABLE_DCACHE
295c11ba852SSoby Mathew	bl	bl32_plat_enable_mmu
296c11ba852SSoby Mathew
29771816096SEtienne Carriere#if SP_MIN_WITH_SECURE_FIQ
29871816096SEtienne Carriere	route_fiq_to_sp_min r0
29971816096SEtienne Carriere#endif
30071816096SEtienne Carriere
301bcc3c49cSSoby Mathew#if HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY
302bcc3c49cSSoby Mathew	ldcopr	r0, SCTLR
303bcc3c49cSSoby Mathew	orr	r0, r0, #SCTLR_C_BIT
304bcc3c49cSSoby Mathew	stcopr	r0, SCTLR
305bcc3c49cSSoby Mathew	isb
306bcc3c49cSSoby Mathew#endif
307bcc3c49cSSoby Mathew
308c11ba852SSoby Mathew	bl	sp_min_warm_boot
309c11ba852SSoby Mathew	bl	smc_get_next_ctx
310b6285d64SSoby Mathew	/* r0 points to `smc_ctx_t` */
311b6285d64SSoby Mathew	/* The PSCI cpu_context registers have been copied to `smc_ctx_t` */
312c11ba852SSoby Mathew	b	sp_min_exit
313c11ba852SSoby Mathewendfunc sp_min_warm_entrypoint
314c11ba852SSoby Mathew
315c11ba852SSoby Mathew/*
316c11ba852SSoby Mathew * The function to restore the registers from SMC context and return
317c11ba852SSoby Mathew * to the mode restored to SPSR.
318c11ba852SSoby Mathew *
319c11ba852SSoby Mathew * Arguments : r0 must point to the SMC context to restore from.
320c11ba852SSoby Mathew */
321c11ba852SSoby Mathewfunc sp_min_exit
322b6285d64SSoby Mathew	monitor_exit
323c11ba852SSoby Mathewendfunc sp_min_exit
324