xref: /rk3399_ARM-atf/bl31/aarch64/runtime_exceptions.S (revision eaeaa4d048076cf48abb42a288ec9ef7a57dd0e6)
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
74f6ad66aSAchin Gupta#include <arch.h>
835e98e55SDan Handley#include <asm_macros.S>
997043ac9SDan Handley#include <context.h>
10872be88aSdp-arm#include <cpu_data.h>
1176454abfSJeenu Viswambharan#include <ea_handle.h>
12dce74b89SAchin Gupta#include <interrupt_mgmt.h>
135f0cdb05SDan Handley#include <platform_def.h>
1497043ac9SDan Handley#include <runtime_svc.h>
152f370465SAntonio Nino Diaz#include <smccc.h>
164f6ad66aSAchin Gupta
174f6ad66aSAchin Gupta	.globl	runtime_exceptions
184f6ad66aSAchin Gupta
19f62ad322SDimitris Papastamos	.globl	sync_exception_sp_el0
20f62ad322SDimitris Papastamos	.globl	irq_sp_el0
21f62ad322SDimitris Papastamos	.globl	fiq_sp_el0
22f62ad322SDimitris Papastamos	.globl	serror_sp_el0
23f62ad322SDimitris Papastamos
24f62ad322SDimitris Papastamos	.globl	sync_exception_sp_elx
25f62ad322SDimitris Papastamos	.globl	irq_sp_elx
26f62ad322SDimitris Papastamos	.globl	fiq_sp_elx
27f62ad322SDimitris Papastamos	.globl	serror_sp_elx
28f62ad322SDimitris Papastamos
29f62ad322SDimitris Papastamos	.globl	sync_exception_aarch64
30f62ad322SDimitris Papastamos	.globl	irq_aarch64
31f62ad322SDimitris Papastamos	.globl	fiq_aarch64
32f62ad322SDimitris Papastamos	.globl	serror_aarch64
33f62ad322SDimitris Papastamos
34f62ad322SDimitris Papastamos	.globl	sync_exception_aarch32
35f62ad322SDimitris Papastamos	.globl	irq_aarch32
36f62ad322SDimitris Papastamos	.globl	fiq_aarch32
37f62ad322SDimitris Papastamos	.globl	serror_aarch32
38f62ad322SDimitris Papastamos
3976454abfSJeenu Viswambharan	/*
4014c6016aSJeenu Viswambharan	 * Macro that prepares entry to EL3 upon taking an exception.
4114c6016aSJeenu Viswambharan	 *
4214c6016aSJeenu Viswambharan	 * With RAS_EXTENSION, this macro synchronizes pending errors with an ESB
4314c6016aSJeenu Viswambharan	 * instruction. When an error is thus synchronized, the handling is
4414c6016aSJeenu Viswambharan	 * delegated to platform EA handler.
4514c6016aSJeenu Viswambharan	 *
4614c6016aSJeenu Viswambharan	 * Without RAS_EXTENSION, this macro just saves x30, and unmasks
4714c6016aSJeenu Viswambharan	 * Asynchronous External Aborts.
4814c6016aSJeenu Viswambharan	 */
4914c6016aSJeenu Viswambharan	.macro check_and_unmask_ea
5014c6016aSJeenu Viswambharan#if RAS_EXTENSION
5114c6016aSJeenu Viswambharan	/* Synchronize pending External Aborts */
5214c6016aSJeenu Viswambharan	esb
5314c6016aSJeenu Viswambharan
5414c6016aSJeenu Viswambharan	/* Unmask the SError interrupt */
5514c6016aSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
5614c6016aSJeenu Viswambharan
5714c6016aSJeenu Viswambharan	/*
5814c6016aSJeenu Viswambharan	 * Explicitly save x30 so as to free up a register and to enable
5914c6016aSJeenu Viswambharan	 * branching
6014c6016aSJeenu Viswambharan	 */
6114c6016aSJeenu Viswambharan	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
6214c6016aSJeenu Viswambharan
6314c6016aSJeenu Viswambharan	/* Check for SErrors synchronized by the ESB instruction */
6414c6016aSJeenu Viswambharan	mrs	x30, DISR_EL1
6514c6016aSJeenu Viswambharan	tbz	x30, #DISR_A_BIT, 1f
6614c6016aSJeenu Viswambharan
6714c6016aSJeenu Viswambharan	/* Save GP registers and restore them afterwards */
6814c6016aSJeenu Viswambharan	bl	save_gp_registers
69df8f3188SJeenu Viswambharan	bl	handle_lower_el_ea_esb
7014c6016aSJeenu Viswambharan	bl	restore_gp_registers
7114c6016aSJeenu Viswambharan
7214c6016aSJeenu Viswambharan1:
7314c6016aSJeenu Viswambharan#else
7414c6016aSJeenu Viswambharan	/* Unmask the SError interrupt */
7514c6016aSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
7614c6016aSJeenu Viswambharan
7714c6016aSJeenu Viswambharan	str	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
7814c6016aSJeenu Viswambharan#endif
7914c6016aSJeenu Viswambharan	.endm
8014c6016aSJeenu Viswambharan
81a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
82a6ef4393SDouglas Raillard	 * This macro handles Synchronous exceptions.
83a6ef4393SDouglas Raillard	 * Only SMC exceptions are supported.
84a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
85dce74b89SAchin Gupta	 */
86dce74b89SAchin Gupta	.macro	handle_sync_exception
87872be88aSdp-arm#if ENABLE_RUNTIME_INSTRUMENTATION
88872be88aSdp-arm	/*
89a6ef4393SDouglas Raillard	 * Read the timestamp value and store it in per-cpu data. The value
90a6ef4393SDouglas Raillard	 * will be extracted from per-cpu data by the C level SMC handler and
91a6ef4393SDouglas Raillard	 * saved to the PMF timestamp region.
92872be88aSdp-arm	 */
93872be88aSdp-arm	mrs	x30, cntpct_el0
94872be88aSdp-arm	str	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
95872be88aSdp-arm	mrs	x29, tpidr_el3
96872be88aSdp-arm	str	x30, [x29, #CPU_DATA_PMF_TS0_OFFSET]
97872be88aSdp-arm	ldr	x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X29]
98872be88aSdp-arm#endif
99872be88aSdp-arm
100dce74b89SAchin Gupta	mrs	x30, esr_el3
101dce74b89SAchin Gupta	ubfx	x30, x30, #ESR_EC_SHIFT, #ESR_EC_LENGTH
102dce74b89SAchin Gupta
103a6ef4393SDouglas Raillard	/* Handle SMC exceptions separately from other synchronous exceptions */
104dce74b89SAchin Gupta	cmp	x30, #EC_AARCH32_SMC
105dce74b89SAchin Gupta	b.eq	smc_handler32
106dce74b89SAchin Gupta
107dce74b89SAchin Gupta	cmp	x30, #EC_AARCH64_SMC
108dce74b89SAchin Gupta	b.eq	smc_handler64
109dce74b89SAchin Gupta
110df8f3188SJeenu Viswambharan	/* Synchronous exceptions other than the above are assumed to be EA */
1114d91838bSJulius Werner	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
112df8f3188SJeenu Viswambharan	b	enter_lower_el_sync_ea
113dce74b89SAchin Gupta	.endm
114dce74b89SAchin Gupta
115dce74b89SAchin Gupta
116a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
117a6ef4393SDouglas Raillard	 * This macro handles FIQ or IRQ interrupts i.e. EL3, S-EL1 and NS
118a6ef4393SDouglas Raillard	 * interrupts.
119a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
120dce74b89SAchin Gupta	 */
121dce74b89SAchin Gupta	.macro	handle_interrupt_exception label
122dce74b89SAchin Gupta	bl	save_gp_registers
123a6ef4393SDouglas Raillard	/* Save the EL3 system registers needed to return from this exception */
1245717aae1SAchin Gupta	mrs	x0, spsr_el3
1255717aae1SAchin Gupta	mrs	x1, elr_el3
1265717aae1SAchin Gupta	stp	x0, x1, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
1275717aae1SAchin Gupta
128dce74b89SAchin Gupta	/* Switch to the runtime stack i.e. SP_EL0 */
129dce74b89SAchin Gupta	ldr	x2, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
130dce74b89SAchin Gupta	mov	x20, sp
131dce74b89SAchin Gupta	msr	spsel, #0
132dce74b89SAchin Gupta	mov	sp, x2
133dce74b89SAchin Gupta
134dce74b89SAchin Gupta	/*
135a6ef4393SDouglas Raillard	 * Find out whether this is a valid interrupt type.
136a6ef4393SDouglas Raillard	 * If the interrupt controller reports a spurious interrupt then return
137a6ef4393SDouglas Raillard	 * to where we came from.
138dce74b89SAchin Gupta	 */
1399865ac15SDan Handley	bl	plat_ic_get_pending_interrupt_type
140dce74b89SAchin Gupta	cmp	x0, #INTR_TYPE_INVAL
141dce74b89SAchin Gupta	b.eq	interrupt_exit_\label
142dce74b89SAchin Gupta
143dce74b89SAchin Gupta	/*
144a6ef4393SDouglas Raillard	 * Get the registered handler for this interrupt type.
145a6ef4393SDouglas Raillard	 * A NULL return value could be 'cause of the following conditions:
1465717aae1SAchin Gupta	 *
147a6ef4393SDouglas Raillard	 * a. An interrupt of a type was routed correctly but a handler for its
148a6ef4393SDouglas Raillard	 *    type was not registered.
1495717aae1SAchin Gupta	 *
150a6ef4393SDouglas Raillard	 * b. An interrupt of a type was not routed correctly so a handler for
151a6ef4393SDouglas Raillard	 *    its type was not registered.
1525717aae1SAchin Gupta	 *
153a6ef4393SDouglas Raillard	 * c. An interrupt of a type was routed correctly to EL3, but was
154a6ef4393SDouglas Raillard	 *    deasserted before its pending state could be read. Another
155a6ef4393SDouglas Raillard	 *    interrupt of a different type pended at the same time and its
156a6ef4393SDouglas Raillard	 *    type was reported as pending instead. However, a handler for this
157a6ef4393SDouglas Raillard	 *    type was not registered.
1585717aae1SAchin Gupta	 *
159a6ef4393SDouglas Raillard	 * a. and b. can only happen due to a programming error. The
160a6ef4393SDouglas Raillard	 * occurrence of c. could be beyond the control of Trusted Firmware.
161a6ef4393SDouglas Raillard	 * It makes sense to return from this exception instead of reporting an
162a6ef4393SDouglas Raillard	 * error.
163dce74b89SAchin Gupta	 */
164dce74b89SAchin Gupta	bl	get_interrupt_type_handler
1655717aae1SAchin Gupta	cbz	x0, interrupt_exit_\label
166dce74b89SAchin Gupta	mov	x21, x0
167dce74b89SAchin Gupta
168dce74b89SAchin Gupta	mov	x0, #INTR_ID_UNAVAILABLE
169dce74b89SAchin Gupta
170dce74b89SAchin Gupta	/* Set the current security state in the 'flags' parameter */
171dce74b89SAchin Gupta	mrs	x2, scr_el3
172dce74b89SAchin Gupta	ubfx	x1, x2, #0, #1
173dce74b89SAchin Gupta
174dce74b89SAchin Gupta	/* Restore the reference to the 'handle' i.e. SP_EL3 */
175dce74b89SAchin Gupta	mov	x2, x20
176dce74b89SAchin Gupta
177b460b8bfSSoby Mathew	/* x3 will point to a cookie (not used now) */
178b460b8bfSSoby Mathew	mov	x3, xzr
179b460b8bfSSoby Mathew
180dce74b89SAchin Gupta	/* Call the interrupt type handler */
181dce74b89SAchin Gupta	blr	x21
182dce74b89SAchin Gupta
183dce74b89SAchin Guptainterrupt_exit_\label:
184dce74b89SAchin Gupta	/* Return from exception, possibly in a different security state */
185dce74b89SAchin Gupta	b	el3_exit
186dce74b89SAchin Gupta
187dce74b89SAchin Gupta	.endm
188dce74b89SAchin Gupta
189dce74b89SAchin Gupta
190e0ae9fabSSandrine Bailleuxvector_base runtime_exceptions
191e0ae9fabSSandrine Bailleux
192a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
193a6ef4393SDouglas Raillard	 * Current EL with SP_EL0 : 0x0 - 0x200
194a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
1954f6ad66aSAchin Gupta	 */
196e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_el0
197a6ef4393SDouglas Raillard	/* We don't expect any synchronous exceptions from EL3 */
1984d91838bSJulius Werner	b	report_unhandled_exception
199a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_el0
2004f6ad66aSAchin Gupta
201e0ae9fabSSandrine Bailleuxvector_entry irq_sp_el0
202a6ef4393SDouglas Raillard	/*
203a6ef4393SDouglas Raillard	 * EL3 code is non-reentrant. Any asynchronous exception is a serious
204a6ef4393SDouglas Raillard	 * error. Loop infinitely.
205a6ef4393SDouglas Raillard	 */
2064d91838bSJulius Werner	b	report_unhandled_interrupt
207a9203edaSRoberto Vargasend_vector_entry irq_sp_el0
2084f6ad66aSAchin Gupta
209e0ae9fabSSandrine Bailleux
210e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_el0
2114d91838bSJulius Werner	b	report_unhandled_interrupt
212a9203edaSRoberto Vargasend_vector_entry fiq_sp_el0
2134f6ad66aSAchin Gupta
214e0ae9fabSSandrine Bailleux
215e0ae9fabSSandrine Bailleuxvector_entry serror_sp_el0
216*eaeaa4d0SJeenu Viswambharan	no_ret	plat_handle_el3_ea
217a9203edaSRoberto Vargasend_vector_entry serror_sp_el0
2184f6ad66aSAchin Gupta
219a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
220a6ef4393SDouglas Raillard	 * Current EL with SP_ELx: 0x200 - 0x400
221a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2224f6ad66aSAchin Gupta	 */
223e0ae9fabSSandrine Bailleuxvector_entry sync_exception_sp_elx
224a6ef4393SDouglas Raillard	/*
225a6ef4393SDouglas Raillard	 * This exception will trigger if anything went wrong during a previous
226a6ef4393SDouglas Raillard	 * exception entry or exit or while handling an earlier unexpected
227a6ef4393SDouglas Raillard	 * synchronous exception. There is a high probability that SP_EL3 is
228a6ef4393SDouglas Raillard	 * corrupted.
229caa84939SJeenu Viswambharan	 */
2304d91838bSJulius Werner	b	report_unhandled_exception
231a9203edaSRoberto Vargasend_vector_entry sync_exception_sp_elx
2324f6ad66aSAchin Gupta
233e0ae9fabSSandrine Bailleuxvector_entry irq_sp_elx
2344d91838bSJulius Werner	b	report_unhandled_interrupt
235a9203edaSRoberto Vargasend_vector_entry irq_sp_elx
236a7934d69SJeenu Viswambharan
237e0ae9fabSSandrine Bailleuxvector_entry fiq_sp_elx
2384d91838bSJulius Werner	b	report_unhandled_interrupt
239a9203edaSRoberto Vargasend_vector_entry fiq_sp_elx
240a7934d69SJeenu Viswambharan
241e0ae9fabSSandrine Bailleuxvector_entry serror_sp_elx
242*eaeaa4d0SJeenu Viswambharan	no_ret	plat_handle_el3_ea
243a9203edaSRoberto Vargasend_vector_entry serror_sp_elx
2444f6ad66aSAchin Gupta
245a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
24644804252SSandrine Bailleux	 * Lower EL using AArch64 : 0x400 - 0x600
247a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2484f6ad66aSAchin Gupta	 */
249e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch64
250a6ef4393SDouglas Raillard	/*
251a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
252a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
253a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
254a6ef4393SDouglas Raillard	 * state can be saved.
255caa84939SJeenu Viswambharan	 */
25614c6016aSJeenu Viswambharan	check_and_unmask_ea
257caa84939SJeenu Viswambharan	handle_sync_exception
258a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch64
2594f6ad66aSAchin Gupta
260e0ae9fabSSandrine Bailleuxvector_entry irq_aarch64
26114c6016aSJeenu Viswambharan	check_and_unmask_ea
262dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch64
263a9203edaSRoberto Vargasend_vector_entry irq_aarch64
2644f6ad66aSAchin Gupta
265e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch64
26614c6016aSJeenu Viswambharan	check_and_unmask_ea
267dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch64
268a9203edaSRoberto Vargasend_vector_entry fiq_aarch64
2694f6ad66aSAchin Gupta
270e0ae9fabSSandrine Bailleuxvector_entry serror_aarch64
27176454abfSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
272df8f3188SJeenu Viswambharan	b	enter_lower_el_async_ea
273a9203edaSRoberto Vargasend_vector_entry serror_aarch64
2744f6ad66aSAchin Gupta
275a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
27644804252SSandrine Bailleux	 * Lower EL using AArch32 : 0x600 - 0x800
277a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
2784f6ad66aSAchin Gupta	 */
279e0ae9fabSSandrine Bailleuxvector_entry sync_exception_aarch32
280a6ef4393SDouglas Raillard	/*
281a6ef4393SDouglas Raillard	 * This exception vector will be the entry point for SMCs and traps
282a6ef4393SDouglas Raillard	 * that are unhandled at lower ELs most commonly. SP_EL3 should point
283a6ef4393SDouglas Raillard	 * to a valid cpu context where the general purpose and system register
284a6ef4393SDouglas Raillard	 * state can be saved.
285caa84939SJeenu Viswambharan	 */
28614c6016aSJeenu Viswambharan	check_and_unmask_ea
287caa84939SJeenu Viswambharan	handle_sync_exception
288a9203edaSRoberto Vargasend_vector_entry sync_exception_aarch32
2894f6ad66aSAchin Gupta
290e0ae9fabSSandrine Bailleuxvector_entry irq_aarch32
29114c6016aSJeenu Viswambharan	check_and_unmask_ea
292dce74b89SAchin Gupta	handle_interrupt_exception irq_aarch32
293a9203edaSRoberto Vargasend_vector_entry irq_aarch32
2944f6ad66aSAchin Gupta
295e0ae9fabSSandrine Bailleuxvector_entry fiq_aarch32
29614c6016aSJeenu Viswambharan	check_and_unmask_ea
297dce74b89SAchin Gupta	handle_interrupt_exception fiq_aarch32
298a9203edaSRoberto Vargasend_vector_entry fiq_aarch32
2994f6ad66aSAchin Gupta
300e0ae9fabSSandrine Bailleuxvector_entry serror_aarch32
30176454abfSJeenu Viswambharan	msr	daifclr, #DAIF_ABT_BIT
302df8f3188SJeenu Viswambharan	b	enter_lower_el_async_ea
303a9203edaSRoberto Vargasend_vector_entry serror_aarch32
304a7934d69SJeenu Viswambharan
305caa84939SJeenu Viswambharan
306a6ef4393SDouglas Raillard	/* ---------------------------------------------------------------------
3072f370465SAntonio Nino Diaz	 * This macro takes an argument in x16 that is the index in the
3082f370465SAntonio Nino Diaz	 * 'rt_svc_descs_indices' array, checks that the value in the array is
3092f370465SAntonio Nino Diaz	 * valid, and loads in x15 the pointer to the handler of that service.
3102f370465SAntonio Nino Diaz	 * ---------------------------------------------------------------------
3112f370465SAntonio Nino Diaz	 */
3122f370465SAntonio Nino Diaz	.macro	load_rt_svc_desc_pointer
3132f370465SAntonio Nino Diaz	/* Load descriptor index from array of indices */
3142f370465SAntonio Nino Diaz	adr	x14, rt_svc_descs_indices
3152f370465SAntonio Nino Diaz	ldrb	w15, [x14, x16]
3162f370465SAntonio Nino Diaz
3172f370465SAntonio Nino Diaz#if SMCCC_MAJOR_VERSION == 1
3182f370465SAntonio Nino Diaz	/* Any index greater than 127 is invalid. Check bit 7. */
3192f370465SAntonio Nino Diaz	tbnz	w15, 7, smc_unknown
3202f370465SAntonio Nino Diaz#elif SMCCC_MAJOR_VERSION == 2
3212f370465SAntonio Nino Diaz	/* Verify that the top 3 bits of the loaded index are 0 (w15 <= 31) */
3222f370465SAntonio Nino Diaz	cmp	w15, #31
3232f370465SAntonio Nino Diaz	b.hi	smc_unknown
3242f370465SAntonio Nino Diaz#endif /* SMCCC_MAJOR_VERSION */
3252f370465SAntonio Nino Diaz
3262f370465SAntonio Nino Diaz	/*
3272f370465SAntonio Nino Diaz	 * Get the descriptor using the index
3282f370465SAntonio Nino Diaz	 * x11 = (base + off), w15 = index
3292f370465SAntonio Nino Diaz	 *
3302f370465SAntonio Nino Diaz	 * handler = (base + off) + (index << log2(size))
3312f370465SAntonio Nino Diaz	 */
3322f370465SAntonio Nino Diaz	adr	x11, (__RT_SVC_DESCS_START__ + RT_SVC_DESC_HANDLE)
3332f370465SAntonio Nino Diaz	lsl	w10, w15, #RT_SVC_SIZE_LOG2
3342f370465SAntonio Nino Diaz	ldr	x15, [x11, w10, uxtw]
3352f370465SAntonio Nino Diaz	.endm
3362f370465SAntonio Nino Diaz
3372f370465SAntonio Nino Diaz	/* ---------------------------------------------------------------------
338caa84939SJeenu Viswambharan	 * The following code handles secure monitor calls.
339a6ef4393SDouglas Raillard	 * Depending upon the execution state from where the SMC has been
340a6ef4393SDouglas Raillard	 * invoked, it frees some general purpose registers to perform the
341a6ef4393SDouglas Raillard	 * remaining tasks. They involve finding the runtime service handler
342a6ef4393SDouglas Raillard	 * that is the target of the SMC & switching to runtime stacks (SP_EL0)
343a6ef4393SDouglas Raillard	 * before calling the handler.
344caa84939SJeenu Viswambharan	 *
345a6ef4393SDouglas Raillard	 * Note that x30 has been explicitly saved and can be used here
346a6ef4393SDouglas Raillard	 * ---------------------------------------------------------------------
347caa84939SJeenu Viswambharan	 */
3480a30cf54SAndrew Thoelkefunc smc_handler
349caa84939SJeenu Viswambharansmc_handler32:
350caa84939SJeenu Viswambharan	/* Check whether aarch32 issued an SMC64 */
351caa84939SJeenu Viswambharan	tbnz	x0, #FUNCID_CC_SHIFT, smc_prohibited
352caa84939SJeenu Viswambharan
353caa84939SJeenu Viswambharansmc_handler64:
354a6ef4393SDouglas Raillard	/*
355a6ef4393SDouglas Raillard	 * Populate the parameters for the SMC handler.
356a6ef4393SDouglas Raillard	 * We already have x0-x4 in place. x5 will point to a cookie (not used
357a6ef4393SDouglas Raillard	 * now). x6 will point to the context structure (SP_EL3) and x7 will
358201ca5b6SDimitris Papastamos	 * contain flags we need to pass to the handler.
359a6ef4393SDouglas Raillard	 *
3602f370465SAntonio Nino Diaz	 * Save x4-x29 and sp_el0.
361caa84939SJeenu Viswambharan	 */
362ef653d93SJeenu Viswambharan	stp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
363ef653d93SJeenu Viswambharan	stp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
364ef653d93SJeenu Viswambharan	stp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
365ef653d93SJeenu Viswambharan	stp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
366ef653d93SJeenu Viswambharan	stp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
367ef653d93SJeenu Viswambharan	stp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
368ef653d93SJeenu Viswambharan	stp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
369ef653d93SJeenu Viswambharan	stp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
370ef653d93SJeenu Viswambharan	stp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
371ef653d93SJeenu Viswambharan	stp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
372ef653d93SJeenu Viswambharan	stp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
373ef653d93SJeenu Viswambharan	stp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
374ef653d93SJeenu Viswambharan	stp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
375ef653d93SJeenu Viswambharan	mrs	x18, sp_el0
376ef653d93SJeenu Viswambharan	str	x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
377c3260f9bSSoby Mathew
378caa84939SJeenu Viswambharan	mov	x5, xzr
379caa84939SJeenu Viswambharan	mov	x6, sp
380caa84939SJeenu Viswambharan
3812f370465SAntonio Nino Diaz#if SMCCC_MAJOR_VERSION == 1
3822f370465SAntonio Nino Diaz
383caa84939SJeenu Viswambharan	/* Get the unique owning entity number */
384caa84939SJeenu Viswambharan	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #FUNCID_OEN_WIDTH
385caa84939SJeenu Viswambharan	ubfx	x15, x0, #FUNCID_TYPE_SHIFT, #FUNCID_TYPE_WIDTH
386caa84939SJeenu Viswambharan	orr	x16, x16, x15, lsl #FUNCID_OEN_WIDTH
387caa84939SJeenu Viswambharan
3882f370465SAntonio Nino Diaz	load_rt_svc_desc_pointer
389caa84939SJeenu Viswambharan
3902f370465SAntonio Nino Diaz#elif SMCCC_MAJOR_VERSION == 2
3912f370465SAntonio Nino Diaz
3922f370465SAntonio Nino Diaz	/* Bit 31 must be set */
3932f370465SAntonio Nino Diaz	tbz	x0, #FUNCID_TYPE_SHIFT, smc_unknown
3942f370465SAntonio Nino Diaz
3952f370465SAntonio Nino Diaz	/*
3962f370465SAntonio Nino Diaz	 * Check MSB of namespace to decide between compatibility/vendor and
3972f370465SAntonio Nino Diaz	 * SPCI/SPRT
3982f370465SAntonio Nino Diaz	 */
3992f370465SAntonio Nino Diaz	tbz	x0, #(FUNCID_NAMESPACE_SHIFT + 1), compat_or_vendor
4002f370465SAntonio Nino Diaz
4012f370465SAntonio Nino Diaz	/* Namespaces SPRT and SPCI currently unimplemented */
4022f370465SAntonio Nino Diaz	b	smc_unknown
4032f370465SAntonio Nino Diaz
4042f370465SAntonio Nino Diazcompat_or_vendor:
4052f370465SAntonio Nino Diaz
4062f370465SAntonio Nino Diaz	/* Namespace is b'00 (compatibility) or b'01 (vendor) */
4072f370465SAntonio Nino Diaz
4082f370465SAntonio Nino Diaz	/*
4092f370465SAntonio Nino Diaz	 * Add the LSB of the namespace (bit [28]) to the OEN [27:24] to create
4102f370465SAntonio Nino Diaz	 * a 5-bit index into the rt_svc_descs_indices array.
4112f370465SAntonio Nino Diaz	 *
4122f370465SAntonio Nino Diaz	 * The low 16 entries of the rt_svc_descs_indices array correspond to
4132f370465SAntonio Nino Diaz	 * OENs of the compatibility namespace and the top 16 entries of the
4142f370465SAntonio Nino Diaz	 * array are assigned to the vendor namespace descriptor.
4152f370465SAntonio Nino Diaz	 */
4162f370465SAntonio Nino Diaz	ubfx	x16, x0, #FUNCID_OEN_SHIFT, #(FUNCID_OEN_WIDTH + 1)
4172f370465SAntonio Nino Diaz
4182f370465SAntonio Nino Diaz	load_rt_svc_desc_pointer
4192f370465SAntonio Nino Diaz
4202f370465SAntonio Nino Diaz#endif /* SMCCC_MAJOR_VERSION */
421caa84939SJeenu Viswambharan
422a6ef4393SDouglas Raillard	/*
423a6ef4393SDouglas Raillard	 * Restore the saved C runtime stack value which will become the new
424a6ef4393SDouglas Raillard	 * SP_EL0 i.e. EL3 runtime stack. It was saved in the 'cpu_context'
425a6ef4393SDouglas Raillard	 * structure prior to the last ERET from EL3.
426caa84939SJeenu Viswambharan	 */
427caa84939SJeenu Viswambharan	ldr	x12, [x6, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
428caa84939SJeenu Viswambharan
429caa84939SJeenu Viswambharan	/* Switch to SP_EL0 */
430caa84939SJeenu Viswambharan	msr	spsel, #0
431caa84939SJeenu Viswambharan
432a6ef4393SDouglas Raillard	/*
433a6ef4393SDouglas Raillard	 * Save the SPSR_EL3, ELR_EL3, & SCR_EL3 in case there is a world
434a6ef4393SDouglas Raillard	 * switch during SMC handling.
435a6ef4393SDouglas Raillard	 * TODO: Revisit if all system registers can be saved later.
436caa84939SJeenu Viswambharan	 */
437caa84939SJeenu Viswambharan	mrs	x16, spsr_el3
438caa84939SJeenu Viswambharan	mrs	x17, elr_el3
439caa84939SJeenu Viswambharan	mrs	x18, scr_el3
440caa84939SJeenu Viswambharan	stp	x16, x17, [x6, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
441b51da821SAchin Gupta	str	x18, [x6, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
442caa84939SJeenu Viswambharan
443caa84939SJeenu Viswambharan	/* Copy SCR_EL3.NS bit to the flag to indicate caller's security */
444caa84939SJeenu Viswambharan	bfi	x7, x18, #0, #1
445caa84939SJeenu Viswambharan
446caa84939SJeenu Viswambharan	mov	sp, x12
447caa84939SJeenu Viswambharan
448a6ef4393SDouglas Raillard	/*
449a6ef4393SDouglas Raillard	 * Call the Secure Monitor Call handler and then drop directly into
450a6ef4393SDouglas Raillard	 * el3_exit() which will program any remaining architectural state
451a6ef4393SDouglas Raillard	 * prior to issuing the ERET to the desired lower EL.
452caa84939SJeenu Viswambharan	 */
453caa84939SJeenu Viswambharan#if DEBUG
454caa84939SJeenu Viswambharan	cbz	x15, rt_svc_fw_critical_error
455caa84939SJeenu Viswambharan#endif
456caa84939SJeenu Viswambharan	blr	x15
457caa84939SJeenu Viswambharan
458bbf8f6f9SYatharth Kochar	b	el3_exit
4594f6ad66aSAchin Gupta
460caa84939SJeenu Viswambharansmc_unknown:
461caa84939SJeenu Viswambharan	/*
462ef653d93SJeenu Viswambharan	 * Unknown SMC call. Populate return value with SMC_UNK, restore
463ef653d93SJeenu Viswambharan	 * GP registers, and return to caller.
464caa84939SJeenu Viswambharan	 */
4654abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
466ef653d93SJeenu Viswambharan	str	x0, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
467ef653d93SJeenu Viswambharan	b	restore_gp_registers_eret
468caa84939SJeenu Viswambharan
469caa84939SJeenu Viswambharansmc_prohibited:
470c3260f9bSSoby Mathew	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
4714abd7fa7SAntonio Nino Diaz	mov	x0, #SMC_UNK
472caa84939SJeenu Viswambharan	eret
473caa84939SJeenu Viswambharan
474caa84939SJeenu Viswambharanrt_svc_fw_critical_error:
475a6ef4393SDouglas Raillard	/* Switch to SP_ELx */
476a6ef4393SDouglas Raillard	msr	spsel, #1
477a806dad5SJeenu Viswambharan	no_ret	report_unhandled_exception
4788b779620SKévin Petitendfunc smc_handler
479