xref: /rk3399_ARM-atf/bl31/aarch64/runtime_exceptions.S (revision 09d40e0e08283a249e7dce0e106c07c5141f9b7e)
14f6ad66aSAchin Gupta/*
2201ca5b6SDimitris Papastamos * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
34f6ad66aSAchin Gupta *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
54f6ad66aSAchin Gupta */
64f6ad66aSAchin Gupta
7*09d40e0eSAntonio Nino Diaz#include <platform_def.h>
8*09d40e0eSAntonio Nino Diaz
94f6ad66aSAchin Gupta#include <arch.h>
1035e98e55SDan Handley#include <asm_macros.S>
11*09d40e0eSAntonio Nino Diaz#include <bl31/ea_handle.h>
12*09d40e0eSAntonio Nino Diaz#include <bl31/interrupt_mgmt.h>
13*09d40e0eSAntonio Nino Diaz#include <common/runtime_svc.h>
1497043ac9SDan Handley#include <context.h>
15*09d40e0eSAntonio Nino Diaz#include <lib/el3_runtime/cpu_data.h>
16*09d40e0eSAntonio Nino Diaz#include <lib/smccc.h>
174f6ad66aSAchin Gupta
184f6ad66aSAchin Gupta	.globl	runtime_exceptions
194f6ad66aSAchin Gupta
20f62ad322SDimitris Papastamos	.globl	sync_exception_sp_el0
21f62ad322SDimitris Papastamos	.globl	irq_sp_el0
22f62ad322SDimitris Papastamos	.globl	fiq_sp_el0
23f62ad322SDimitris Papastamos	.globl	serror_sp_el0
24f62ad322SDimitris Papastamos
25f62ad322SDimitris Papastamos	.globl	sync_exception_sp_elx
26f62ad322SDimitris Papastamos	.globl	irq_sp_elx
27f62ad322SDimitris Papastamos	.globl	fiq_sp_elx
28f62ad322SDimitris Papastamos	.globl	serror_sp_elx
29f62ad322SDimitris Papastamos
30f62ad322SDimitris Papastamos	.globl	sync_exception_aarch64
31f62ad322SDimitris Papastamos	.globl	irq_aarch64
32f62ad322SDimitris Papastamos	.globl	fiq_aarch64
33f62ad322SDimitris Papastamos	.globl	serror_aarch64
34f62ad322SDimitris Papastamos
35f62ad322SDimitris Papastamos	.globl	sync_exception_aarch32
36f62ad322SDimitris Papastamos	.globl	irq_aarch32
37f62ad322SDimitris Papastamos	.globl	fiq_aarch32
38f62ad322SDimitris Papastamos	.globl	serror_aarch32
39f62ad322SDimitris Papastamos
4076454abfSJeenu Viswambharan	/*
4114c6016aSJeenu Viswambharan	 * Macro that prepares entry to EL3 upon taking an exception.
4214c6016aSJeenu Viswambharan	 *
4314c6016aSJeenu Viswambharan	 * With RAS_EXTENSION, this macro synchronizes pending errors with an ESB
4414c6016aSJeenu Viswambharan	 * instruction. When an error is thus synchronized, the handling is
4514c6016aSJeenu Viswambharan	 * delegated to platform EA handler.
4614c6016aSJeenu Viswambharan	 *
4714c6016aSJeenu Viswambharan	 * Without RAS_EXTENSION, this macro just saves x30, and unmasks
4814c6016aSJeenu Viswambharan	 * Asynchronous External Aborts.
4914c6016aSJeenu Viswambharan	 */
5014c6016aSJeenu Viswambharan	.macro check_and_unmask_ea
5114c6016aSJeenu Viswambharan#if RAS_EXTENSION
5214c6016aSJeenu Viswambharan	/* Synchronize pending External Aborts */
5314c6016aSJeenu Viswambharan	esb
5414c6016aSJeenu Viswambharan
5514c6016aSJeenu Viswambharan	/* Unmask the SError interrupt */
5614c6016aSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
5714c6016aSJeenu Viswambharan
5814c6016aSJeenu Viswambharan	/*
5914c6016aSJeenu Viswambharan	 * Explicitly save x30 so as to free up a register and to enable
6014c6016aSJeenu Viswambharan	 * branching
6114c6016aSJeenu Viswambharan	 */
6214c6016aSJeenu Viswambharan	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
6314c6016aSJeenu Viswambharan
6414c6016aSJeenu Viswambharan	/* Check for SErrors synchronized by the ESB instruction */
6514c6016aSJeenu Viswambharan	mrs	x30, DISR_EL1
6614c6016aSJeenu Viswambharan	tbz	x30, #DISR_A_BIT, 1f
6714c6016aSJeenu Viswambharan
6814c6016aSJeenu Viswambharan	/* Save GP registers and restore them afterwards */
6914c6016aSJeenu Viswambharan	bl	save_gp_registers
70df8f3188SJeenu Viswambharan	bl	handle_lower_el_ea_esb
7114c6016aSJeenu Viswambharan	bl	restore_gp_registers
7214c6016aSJeenu Viswambharan
7314c6016aSJeenu Viswambharan1:
7414c6016aSJeenu Viswambharan#else
7514c6016aSJeenu Viswambharan	/* Unmask the SError interrupt */
7614c6016aSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
7714c6016aSJeenu Viswambharan
7814c6016aSJeenu Viswambharan	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
7914c6016aSJeenu Viswambharan#endif
8014c6016aSJeenu Viswambharan	.endm
8114c6016aSJeenu Viswambharan
82a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
83a6ef4393SDouglas Raillard	 * This macro handles Synchronous exceptions.
84a6ef4393SDouglas Raillard	 * Only SMC exceptions are supported.
85a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
86dce74b89SAchin Gupta	 */
87dce74b89SAchin Gupta	.macro	handle_sync_exception
88872be88aSdp-arm#if ENABLE_RUNTIME_INSTRUMENTATION
89872be88aSdp-arm	/*
90a6ef4393SDouglas Raillard	 * Read the timestamp value and store it in per-cpu data. The value
91a6ef4393SDouglas Raillard	 * will be extracted from per-cpu data by the C level SMC handler and
92a6ef4393SDouglas Raillard	 * saved to the PMF timestamp region.
93872be88aSdp-arm	 */
94872be88aSdp-arm	mrs	x30, cntpct_el0
95872be88aSdp-arm	str	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
96872be88aSdp-arm	mrs	x29, tpidr_el3
97872be88aSdp-arm	str	x30, [x29, #CPU_DATA_PMF_TS0_OFFSET]
98872be88aSdp-arm	ldr	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
99872be88aSdp-arm#endif
100872be88aSdp-arm
101dce74b89SAchin Gupta	mrs	x30, esr_el3
102dce74b89SAchin Gupta	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
103dce74b89SAchin Gupta
104a6ef4393SDouglas Raillard	/* Handle SMC exceptions separately from other synchronous exceptions */
105dce74b89SAchin Gupta	cmp	x30, #EC_AARCH32_SMC
106dce74b89SAchin Gupta	b.eq	smc_handler32
107dce74b89SAchin Gupta
108dce74b89SAchin Gupta	cmp	x30, #EC_AARCH64_SMC
109dce74b89SAchin Gupta	b.eq	smc_handler64
110dce74b89SAchin Gupta
111df8f3188SJeenu Viswambharan	/* Synchronous exceptions other than the above are assumed to be EA */
1124d91838bSJulius Werner	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
113df8f3188SJeenu Viswambharan	b	enter_lower_el_sync_ea
114dce74b89SAchin Gupta	.endm
115dce74b89SAchin Gupta
116dce74b89SAchin Gupta
117a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
118a6ef4393SDouglas Raillard	 * This macro handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS
119a6ef4393SDouglas Raillard	 * interrupts.
120a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
121dce74b89SAchin Gupta	 */
122dce74b89SAchin Gupta	.macro	handle_interrupt_exception label
123dce74b89SAchin Gupta	bl	save_gp_registers
124a6ef4393SDouglas Raillard	/* Save the EL3 system registers needed to return from this exception */
1255717aae1SAchin Gupta	mrs	x0, spsr_el3
1265717aae1SAchin Gupta	mrs	x1, elr_el3
1275717aae1SAchin Gupta	stp	x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
1285717aae1SAchin Gupta
129dce74b89SAchin Gupta	/* Switch to the runtime stack i.e. SP_EL0 */
130dce74b89SAchin Gupta	ldr	x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
131dce74b89SAchin Gupta	mov	x20, sp
132dce74b89SAchin Gupta	msr	spsel, #0
133dce74b89SAchin Gupta	mov	sp, x2
134dce74b89SAchin Gupta
135dce74b89SAchin Gupta	/*
136a6ef4393SDouglas Raillard	 * Find out whether this is a valid interrupt type.
137a6ef4393SDouglas Raillard	 * If the interrupt controller reports a spurious interrupt then return
138a6ef4393SDouglas Raillard	 * to where we came from.
139dce74b89SAchin Gupta	 */
1409865ac15SDan Handley	bl	plat_ic_get_pending_interrupt_type
141dce74b89SAchin Gupta	cmp	x0, #INTR_TYPE_INVAL
142dce74b89SAchin Gupta	b.eq	interrupt_exit_\label
143dce74b89SAchin Gupta
144dce74b89SAchin Gupta	/*
145a6ef4393SDouglas Raillard	 * Get the registered handler for this interrupt type.
146a6ef4393SDouglas Raillard	 * A NULL return value could be 'cause of the following conditions:
1475717aae1SAchin Gupta	 *
148a6ef4393SDouglas Raillard	 * a. An interrupt of a type was routed correctly but a handler for its
149a6ef4393SDouglas Raillard	 *    type was not registered.
1505717aae1SAchin Gupta	 *
151a6ef4393SDouglas Raillard	 * b. An interrupt of a type was not routed correctly so a handler for
152a6ef4393SDouglas Raillard	 *    its type was not registered.
1535717aae1SAchin Gupta	 *
154a6ef4393SDouglas Raillard	 * c. An interrupt of a type was routed correctly to EL3, but was
155a6ef4393SDouglas Raillard	 *    deasserted before its pending state could be read. Another
156a6ef4393SDouglas Raillard	 *    interrupt of a different type pended at the same time and its
157a6ef4393SDouglas Raillard	 *    type was reported as pending instead. However, a handler for this
158a6ef4393SDouglas Raillard	 *    type was not registered.
1595717aae1SAchin Gupta	 *
160a6ef4393SDouglas Raillard	 * a. and b. can only happen due to a programming error. The
161a6ef4393SDouglas Raillard	 * occurrence of c. could be beyond the control of Trusted Firmware.
162a6ef4393SDouglas Raillard	 * It makes sense to return from this exception instead of reporting an
163a6ef4393SDouglas Raillard	 * error.
164dce74b89SAchin Gupta	 */
165dce74b89SAchin Gupta	bl	get_interrupt_type_handler
1665717aae1SAchin Gupta	cbz	x0, interrupt_exit_\label
167dce74b89SAchin Gupta	mov	x21, x0
168dce74b89SAchin Gupta
169dce74b89SAchin Gupta	mov	x0, #INTR_ID_UNAVAILABLE
170dce74b89SAchin Gupta
171dce74b89SAchin Gupta	/* Set the current security state in the 'flags' parameter */
172dce74b89SAchin Gupta	mrs	x2, scr_el3
173dce74b89SAchin Gupta	ubfx	x1, x2, #0, #1
174dce74b89SAchin Gupta
175dce74b89SAchin Gupta	/* Restore the reference to the 'handle' i.e. SP_EL3 */
176dce74b89SAchin Gupta	mov	x2, x20
177dce74b89SAchin Gupta
178b460b8bfSSoby Mathew	/* x3 will point to a cookie (not used now) */
179b460b8bfSSoby Mathew	mov	x3, xzr
180b460b8bfSSoby Mathew
181dce74b89SAchin Gupta	/* Call the interrupt type handler */
182dce74b89SAchin Gupta	blr	x21
183dce74b89SAchin Gupta
184dce74b89SAchin Guptainterrupt_exit_\label:
185dce74b89SAchin Gupta	/* Return from exception, possibly in a different security state */
186dce74b89SAchin Gupta	b	el3_exit
187dce74b89SAchin Gupta
188dce74b89SAchin Gupta	.endm
189dce74b89SAchin Gupta
190dce74b89SAchin Gupta
191e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions
192e0ae9fabSSandrine Bailleux
193a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
194a6ef4393SDouglas Raillard	 * Current EL with SP_EL0 : 0x0 - 0x200
195a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
1964f6ad66aSAchin Gupta	 */
197e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0
198a6ef4393SDouglas Raillard	/* We don't expect any synchronous exceptions from EL3 */
1994d91838bSJulius Werner	b	report_unhandled_exception
200a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_el0
2014f6ad66aSAchin Gupta
202e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0
203a6ef4393SDouglas Raillard	/*
204a6ef4393SDouglas Raillard	 * EL3 code is non-reentrant. Any asynchronous exception is a serious
205a6ef4393SDouglas Raillard	 * error. Loop infinitely.
206a6ef4393SDouglas Raillard	 */
2074d91838bSJulius Werner	b	report_unhandled_interrupt
208a9203edaSRoberto Vargasend_vector_entry irq_sp_el0
2094f6ad66aSAchin Gupta
210e0ae9fabSSandrine Bailleux
211e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0
2124d91838bSJulius Werner	b	report_unhandled_interrupt
213a9203edaSRoberto Vargasend_vector_entry fiq_sp_el0
2144f6ad66aSAchin Gupta
215e0ae9fabSSandrine Bailleux
216e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0
217eaeaa4d0SJeenu Viswambharan	no_ret	plat_handle_el3_ea
218a9203edaSRoberto Vargasend_vector_entry serror_sp_el0
2194f6ad66aSAchin Gupta
220a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
221a6ef4393SDouglas Raillard	 * Current EL with SP_ELx: 0x200 - 0x400
222a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2234f6ad66aSAchin Gupta	 */
224e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx
225a6ef4393SDouglas Raillard	/*
226a6ef4393SDouglas Raillard	 * This exception will trigger if anything went wrong during a previous
227a6ef4393SDouglas Raillard	 * exception entry or exit or while handling an earlier unexpected
228a6ef4393SDouglas Raillard	 * synchronous exception. There is a high probability that SP_EL3 is
229a6ef4393SDouglas Raillard	 * corrupted.
230caa84939SJeenu Viswambharan	 */
2314d91838bSJulius Werner	b	report_unhandled_exception
232a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_elx
2334f6ad66aSAchin Gupta
234e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx
2354d91838bSJulius Werner	b	report_unhandled_interrupt
236a9203edaSRoberto Vargasend_vector_entry irq_sp_elx
237a7934d69SJeenu Viswambharan
238e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx
2394d91838bSJulius Werner	b	report_unhandled_interrupt
240a9203edaSRoberto Vargasend_vector_entry fiq_sp_elx
241a7934d69SJeenu Viswambharan
242e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx
243eaeaa4d0SJeenu Viswambharan	no_ret	plat_handle_el3_ea
244a9203edaSRoberto Vargasend_vector_entry serror_sp_elx
2454f6ad66aSAchin Gupta
246a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
24744804252SSandrine Bailleux	 * Lower EL using AArch64 : 0x400 - 0x600
248a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2494f6ad66aSAchin Gupta	 */
250e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64
251a6ef4393SDouglas Raillard	/*
252a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
253a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
254a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
255a6ef4393SDouglas Raillard	 * state can be saved.
256caa84939SJeenu Viswambharan	 */
25714c6016aSJeenu Viswambharan	check_and_unmask_ea
258caa84939SJeenu Viswambharan	handle_sync_exception
259a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch64
2604f6ad66aSAchin Gupta
261e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64
26214c6016aSJeenu Viswambharan	check_and_unmask_ea
263dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch64
264a9203edaSRoberto Vargasend_vector_entry irq_aarch64
2654f6ad66aSAchin Gupta
266e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch64
26714c6016aSJeenu Viswambharan	check_and_unmask_ea
268dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch64
269a9203edaSRoberto Vargasend_vector_entry fiq_aarch64
2704f6ad66aSAchin Gupta
271e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64
27276454abfSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
273df8f3188SJeenu Viswambharan	b	enter_lower_el_async_ea
274a9203edaSRoberto Vargasend_vector_entry serror_aarch64
2754f6ad66aSAchin Gupta
276a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
27744804252SSandrine Bailleux	 * Lower EL using AArch32 : 0x600 - 0x800
278a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2794f6ad66aSAchin Gupta	 */
280e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32
281a6ef4393SDouglas Raillard	/*
282a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
283a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
284a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
285a6ef4393SDouglas Raillard	 * state can be saved.
286caa84939SJeenu Viswambharan	 */
28714c6016aSJeenu Viswambharan	check_and_unmask_ea
288caa84939SJeenu Viswambharan	handle_sync_exception
289a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch32
2904f6ad66aSAchin Gupta
291e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32
29214c6016aSJeenu Viswambharan	check_and_unmask_ea
293dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch32
294a9203edaSRoberto Vargasend_vector_entry irq_aarch32
2954f6ad66aSAchin Gupta
296e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch32
29714c6016aSJeenu Viswambharan	check_and_unmask_ea
298dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch32
299a9203edaSRoberto Vargasend_vector_entry fiq_aarch32
3004f6ad66aSAchin Gupta
301e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32
30276454abfSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
303df8f3188SJeenu Viswambharan	b	enter_lower_el_async_ea
304a9203edaSRoberto Vargasend_vector_entry serror_aarch32
305a7934d69SJeenu Viswambharan
306caa84939SJeenu Viswambharan
307a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
3082f370465SAntonio Nino Diaz	 * This macro takes an argument in x16 that is the index in the
3092f370465SAntonio Nino Diaz	 * 'rt_svc_descs_indices' array, checks that the value in the array is
3102f370465SAntonio Nino Diaz	 * valid, and loads in x15 the pointer to the handler of that service.
3112f370465SAntonio Nino Diaz	 * ---------------------------------------------------------------------
3122f370465SAntonio Nino Diaz	 */
3132f370465SAntonio Nino Diaz	.macro	load_rt_svc_desc_pointer
3142f370465SAntonio Nino Diaz	/* Load descriptor index from array of indices */
3152f370465SAntonio Nino Diaz	adr	x14, rt_svc_descs_indices
3162f370465SAntonio Nino Diaz	ldrb	w15, [x14, x16]
3172f370465SAntonio Nino Diaz
3182f370465SAntonio Nino Diaz#if SMCCC_MAJOR_VERSION == 1
3192f370465SAntonio Nino Diaz	/* Any index greater than 127 is invalid. Check bit 7. */
3202f370465SAntonio Nino Diaz	tbnz	w15, 7, smc_unknown
3212f370465SAntonio Nino Diaz#elif SMCCC_MAJOR_VERSION == 2
3222f370465SAntonio Nino Diaz	/* Verify that the top 3 bits of the loaded index are 0 (w15 <= 31) */
3232f370465SAntonio Nino Diaz	cmp	w15, #31
3242f370465SAntonio Nino Diaz	b.hi	smc_unknown
3252f370465SAntonio Nino Diaz#endif /* SMCCC_MAJOR_VERSION */
3262f370465SAntonio Nino Diaz
3272f370465SAntonio Nino Diaz	/*
3282f370465SAntonio Nino Diaz	 * Get the descriptor using the index
3292f370465SAntonio Nino Diaz	 * x11 = (base + off), w15 = index
3302f370465SAntonio Nino Diaz	 *
3312f370465SAntonio Nino Diaz	 * handler = (base + off) + (index << log2(size))
3322f370465SAntonio Nino Diaz	 */
3332f370465SAntonio Nino Diaz	adr	x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE)
3342f370465SAntonio Nino Diaz	lsl	w10, w15, #RT_SVC_SIZE_LOG2
3352f370465SAntonio Nino Diaz	ldr	x15, [x11, w10, uxtw]
3362f370465SAntonio Nino Diaz	.endm
3372f370465SAntonio Nino Diaz
3382f370465SAntonio Nino Diaz	/* ---------------------------------------------------------------------
339caa84939SJeenu Viswambharan	 * The following code handles secure monitor calls.
340a6ef4393SDouglas Raillard	 * Depending upon the execution state from where the SMC has been
341a6ef4393SDouglas Raillard	 * invoked, it frees some general purpose registers to perform the
342a6ef4393SDouglas Raillard	 * remaining tasks. They involve finding the runtime service handler
343a6ef4393SDouglas Raillard	 * that is the target of the SMC & switching to runtime stacks (SP_EL0)
344a6ef4393SDouglas Raillard	 * before calling the handler.
345caa84939SJeenu Viswambharan	 *
346a6ef4393SDouglas Raillard	 * Note that x30 has been explicitly saved and can be used here
347a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
348caa84939SJeenu Viswambharan	 */
3490a30cf54SAndrew Thoelkefunc smc_handler
350caa84939SJeenu Viswambharansmc_handler32:
351caa84939SJeenu Viswambharan	/* Check whether aarch32 issued an SMC64 */
352caa84939SJeenu Viswambharan	tbnz	x0, #FUNCID_CC_SHIFT, smc_prohibited
353caa84939SJeenu Viswambharan
354caa84939SJeenu Viswambharansmc_handler64:
355a6ef4393SDouglas Raillard	/*
356a6ef4393SDouglas Raillard	 * Populate the parameters for the SMC handler.
357a6ef4393SDouglas Raillard	 * We already have x0-x4 in place. x5 will point to a cookie (not used
358a6ef4393SDouglas Raillard	 * now). x6 will point to the context structure (SP_EL3) and x7 will
359201ca5b6SDimitris Papastamos	 * contain flags we need to pass to the handler.
360caa84939SJeenu Viswambharan	 */
36101fc1c24SSoby Mathew	bl	save_gp_registers
362c3260f9bSSoby Mathew
363caa84939SJeenu Viswambharan	mov	x5, xzr
364caa84939SJeenu Viswambharan	mov	x6, sp
365caa84939SJeenu Viswambharan
3662f370465SAntonio Nino Diaz#if SMCCC_MAJOR_VERSION == 1
3672f370465SAntonio Nino Diaz
368caa84939SJeenu Viswambharan	/* Get the unique owning entity number */
369caa84939SJeenu Viswambharan	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH
370caa84939SJeenu Viswambharan	ubfx	x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH
371caa84939SJeenu Viswambharan	orr	x16, x16, x15, lsl #FUNCID_OEN_WIDTH
372caa84939SJeenu Viswambharan
3732f370465SAntonio Nino Diaz	load_rt_svc_desc_pointer
374caa84939SJeenu Viswambharan
3752f370465SAntonio Nino Diaz#elif SMCCC_MAJOR_VERSION == 2
3762f370465SAntonio Nino Diaz
3772f370465SAntonio Nino Diaz	/* Bit 31 must be set */
3782f370465SAntonio Nino Diaz	tbz	x0, #FUNCID_TYPE_SHIFT, smc_unknown
3792f370465SAntonio Nino Diaz
3802f370465SAntonio Nino Diaz	/*
3812f370465SAntonio Nino Diaz	 * Check MSB of namespace to decide between compatibility/vendor and
3822f370465SAntonio Nino Diaz	 * SPCI/SPRT
3832f370465SAntonio Nino Diaz	 */
3842f370465SAntonio Nino Diaz	tbz	x0, #(FUNCID_NAMESPACE_SHIFT + 1), compat_or_vendor
3852f370465SAntonio Nino Diaz
386e8ce60aeSAntonio Nino Diaz	/* Namespace is b'10 (SPRT) or b'11 (SPCI) */
387e8ce60aeSAntonio Nino Diaz#if ENABLE_SPM
388e8ce60aeSAntonio Nino Diaz	tst	x0, #(1 << FUNCID_NAMESPACE_SHIFT)
389e8ce60aeSAntonio Nino Diaz	adr	x15, spci_smc_handler
390e8ce60aeSAntonio Nino Diaz	adr	x16, sprt_smc_handler
391e8ce60aeSAntonio Nino Diaz	csel	x15, x15, x16, ne
392e8ce60aeSAntonio Nino Diaz	b	prepare_enter_handler
393e8ce60aeSAntonio Nino Diaz#else
3942f370465SAntonio Nino Diaz	b	smc_unknown
395e8ce60aeSAntonio Nino Diaz#endif
3962f370465SAntonio Nino Diaz
3972f370465SAntonio Nino Diazcompat_or_vendor:
3982f370465SAntonio Nino Diaz
3992f370465SAntonio Nino Diaz	/* Namespace is b'00 (compatibility) or b'01 (vendor) */
4002f370465SAntonio Nino Diaz
4012f370465SAntonio Nino Diaz	/*
4022f370465SAntonio Nino Diaz	 * Add the LSB of the namespace (bit [28]) to the OEN [27:24] to create
4032f370465SAntonio Nino Diaz	 * a 5-bit index into the rt_svc_descs_indices array.
4042f370465SAntonio Nino Diaz	 *
4052f370465SAntonio Nino Diaz	 * The low 16 entries of the rt_svc_descs_indices array correspond to
4062f370465SAntonio Nino Diaz	 * OENs of the compatibility namespace and the top 16 entries of the
4072f370465SAntonio Nino Diaz	 * array are assigned to the vendor namespace descriptor.
4082f370465SAntonio Nino Diaz	 */
4092f370465SAntonio Nino Diaz	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #(FUNCID_OEN_WIDTH + 1)
4102f370465SAntonio Nino Diaz
4112f370465SAntonio Nino Diaz	load_rt_svc_desc_pointer
4122f370465SAntonio Nino Diaz
413e8ce60aeSAntonio Nino Diazprepare_enter_handler:
414e8ce60aeSAntonio Nino Diaz
4152f370465SAntonio Nino Diaz#endif /* SMCCC_MAJOR_VERSION */
416caa84939SJeenu Viswambharan
417a6ef4393SDouglas Raillard	/*
418a6ef4393SDouglas Raillard	 * Restore the saved C runtime stack value which will become the new
419a6ef4393SDouglas Raillard	 * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context'
420a6ef4393SDouglas Raillard	 * structure prior to the last ERET from EL3.
421caa84939SJeenu Viswambharan	 */
422caa84939SJeenu Viswambharan	ldr	x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
423caa84939SJeenu Viswambharan
424caa84939SJeenu Viswambharan	/* Switch to SP_EL0 */
425caa84939SJeenu Viswambharan	msr	spsel, #0
426caa84939SJeenu Viswambharan
427a6ef4393SDouglas Raillard	/*
428a6ef4393SDouglas Raillard	 * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there is a world
429a6ef4393SDouglas Raillard	 * switch during SMC handling.
430a6ef4393SDouglas Raillard	 * TODO: Revisit if all system registers can be saved later.
431caa84939SJeenu Viswambharan	 */
432caa84939SJeenu Viswambharan	mrs	x16, spsr_el3
433caa84939SJeenu Viswambharan	mrs	x17, elr_el3
434caa84939SJeenu Viswambharan	mrs	x18, scr_el3
435caa84939SJeenu Viswambharan	stp	x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
436b51da821SAchin Gupta	str	x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
437caa84939SJeenu Viswambharan
438caa84939SJeenu Viswambharan	/* Copy SCR_EL3.NS bit to the flag to indicate caller's security */
439caa84939SJeenu Viswambharan	bfi	x7, x18, #0, #1
440caa84939SJeenu Viswambharan
441caa84939SJeenu Viswambharan	mov	sp, x12
442caa84939SJeenu Viswambharan
443a6ef4393SDouglas Raillard	/*
444a6ef4393SDouglas Raillard	 * Call the Secure Monitor Call handler and then drop directly into
445a6ef4393SDouglas Raillard	 * el3_exit() which will program any remaining architectural state
446a6ef4393SDouglas Raillard	 * prior to issuing the ERET to the desired lower EL.
447caa84939SJeenu Viswambharan	 */
448caa84939SJeenu Viswambharan#if DEBUG
449caa84939SJeenu Viswambharan	cbz	x15, rt_svc_fw_critical_error
450caa84939SJeenu Viswambharan#endif
451caa84939SJeenu Viswambharan	blr	x15
452caa84939SJeenu Viswambharan
453bbf8f6f9SYatharth Kochar	b	el3_exit
4544f6ad66aSAchin Gupta
455caa84939SJeenu Viswambharansmc_unknown:
456caa84939SJeenu Viswambharan	/*
457ef653d93SJeenu Viswambharan	 * Unknown SMC call. Populate return value with SMC_UNK, restore
458ef653d93SJeenu Viswambharan	 * GP registers, and return to caller.
459caa84939SJeenu Viswambharan	 */
4604abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
461ef653d93SJeenu Viswambharan	str	x0, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
462ef653d93SJeenu Viswambharan	b	restore_gp_registers_eret
463caa84939SJeenu Viswambharan
464caa84939SJeenu Viswambharansmc_prohibited:
465c3260f9bSSoby Mathew	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
4664abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
467caa84939SJeenu Viswambharan	eret
468caa84939SJeenu Viswambharan
469caa84939SJeenu Viswambharanrt_svc_fw_critical_error:
470a6ef4393SDouglas Raillard	/* Switch to SP_ELx */
471a6ef4393SDouglas Raillard	msr	spsel, #1
472a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
4738b779620SKévin Petitendfunc smc_handler
474