xref: /rk3399_ARM-atf/services/spd/tspd/tspd_main.c (revision 512173980ff6c913817d261dd55d3cc138c7d14b)
1375f538aSAchin Gupta /*
2*7623e085SJayanth Dodderi Chidanand  * Copyright (c) 2013-2024, ARM Limited and Contributors. All rights reserved.
3375f538aSAchin Gupta  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5375f538aSAchin Gupta  */
6375f538aSAchin Gupta 
7375f538aSAchin Gupta 
8375f538aSAchin Gupta /*******************************************************************************
9375f538aSAchin Gupta  * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a
10375f538aSAchin Gupta  * plug-in component to the Secure Monitor, registered as a runtime service. The
11375f538aSAchin Gupta  * SPD is expected to be a functional extension of the Secure Payload (SP) that
12375f538aSAchin Gupta  * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting
13375f538aSAchin Gupta  * the Trusted OS/Applications range to the dispatcher. The SPD will either
14375f538aSAchin Gupta  * handle the request locally or delegate it to the Secure Payload. It is also
15375f538aSAchin Gupta  * responsible for initialising and maintaining communication with the SP.
16375f538aSAchin Gupta  ******************************************************************************/
1797043ac9SDan Handley #include <assert.h>
18b44a4435SAchin Gupta #include <errno.h>
1997043ac9SDan Handley #include <stddef.h>
20f4f1ae77SSoby Mathew #include <string.h>
2109d40e0eSAntonio Nino Diaz 
2209d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
2309d40e0eSAntonio Nino Diaz #include <bl31/bl31.h>
2409d40e0eSAntonio Nino Diaz #include <bl31/ehf.h>
2509d40e0eSAntonio Nino Diaz #include <bl32/tsp/tsp.h>
2609d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
2709d40e0eSAntonio Nino Diaz #include <common/debug.h>
2809d40e0eSAntonio Nino Diaz #include <common/runtime_svc.h>
2909d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h>
3009d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
3109d40e0eSAntonio Nino Diaz #include <tools_share/uuid.h>
3209d40e0eSAntonio Nino Diaz 
3335e98e55SDan Handley #include "tspd_private.h"
34375f538aSAchin Gupta 
35375f538aSAchin Gupta /*******************************************************************************
36399fb08fSAndrew Thoelke  * Address of the entrypoint vector table in the Secure Payload. It is
37399fb08fSAndrew Thoelke  * initialised once on the primary core after a cold boot.
38375f538aSAchin Gupta  ******************************************************************************/
39399fb08fSAndrew Thoelke tsp_vectors_t *tsp_vectors;
40375f538aSAchin Gupta 
41375f538aSAchin Gupta /*******************************************************************************
42375f538aSAchin Gupta  * Array to keep track of per-cpu Secure Payload state
43375f538aSAchin Gupta  ******************************************************************************/
44fb037bfbSDan Handley tsp_context_t tspd_sp_context[TSPD_CORE_COUNT];
45375f538aSAchin Gupta 
467f366605SJeenu Viswambharan 
4752538b9bSJeenu Viswambharan /* TSP UID */
4803364865SRoberto Vargas DEFINE_SVC_UUID2(tsp_uuid,
4903364865SRoberto Vargas 	0xa056305b, 0x9132, 0x7b42, 0x98, 0x11,
5052538b9bSJeenu Viswambharan 	0x71, 0x68, 0xca, 0x50, 0xf3, 0xfa);
5152538b9bSJeenu Viswambharan 
526871c5d3SVikram Kanigiri int32_t tspd_init(void);
537f366605SJeenu Viswambharan 
54404dba53SSoby Mathew /*
55404dba53SSoby Mathew  * This helper function handles Secure EL1 preemption. The preemption could be
56404dba53SSoby Mathew  * due Non Secure interrupts or EL3 interrupts. In both the cases we context
57404dba53SSoby Mathew  * switch to the normal world and in case of EL3 interrupts, it will again be
58404dba53SSoby Mathew  * routed to EL3 which will get handled at the exception vectors.
59404dba53SSoby Mathew  */
tspd_handle_sp_preemption(void * handle)60f4f1ae77SSoby Mathew uint64_t tspd_handle_sp_preemption(void *handle)
61f4f1ae77SSoby Mathew {
62f4f1ae77SSoby Mathew 	cpu_context_t *ns_cpu_context;
63404dba53SSoby Mathew 
64f4f1ae77SSoby Mathew 	assert(handle == cm_get_context(SECURE));
65f4f1ae77SSoby Mathew 	cm_el1_sysregs_context_save(SECURE);
66f4f1ae77SSoby Mathew 	/* Get a reference to the non-secure context */
67f4f1ae77SSoby Mathew 	ns_cpu_context = cm_get_context(NON_SECURE);
68f4f1ae77SSoby Mathew 	assert(ns_cpu_context);
69f4f1ae77SSoby Mathew 
70f4f1ae77SSoby Mathew 	/*
7163b8440fSSoby Mathew 	 * To allow Secure EL1 interrupt handler to re-enter TSP while TSP
7263b8440fSSoby Mathew 	 * is preempted, the secure system register context which will get
7363b8440fSSoby Mathew 	 * overwritten must be additionally saved. This is currently done
7463b8440fSSoby Mathew 	 * by the TSPD S-EL1 interrupt handler.
7563b8440fSSoby Mathew 	 */
7663b8440fSSoby Mathew 
7763b8440fSSoby Mathew 	/*
7863b8440fSSoby Mathew 	 * Restore non-secure state.
79f4f1ae77SSoby Mathew 	 */
80f4f1ae77SSoby Mathew 	cm_el1_sysregs_context_restore(NON_SECURE);
81f4f1ae77SSoby Mathew 	cm_set_next_eret_context(NON_SECURE);
82f4f1ae77SSoby Mathew 
83404dba53SSoby Mathew 	/*
8416292f54SDavid Cunado 	 * The TSP was preempted during execution of a Yielding SMC Call.
8563b8440fSSoby Mathew 	 * Return back to the normal world with SMC_PREEMPTED as error
8663b8440fSSoby Mathew 	 * code in x0.
87404dba53SSoby Mathew 	 */
88f4f1ae77SSoby Mathew 	SMC_RET1(ns_cpu_context, SMC_PREEMPTED);
89f4f1ae77SSoby Mathew }
90404dba53SSoby Mathew 
91b44a4435SAchin Gupta /*******************************************************************************
92b44a4435SAchin Gupta  * This function is the handler registered for S-EL1 interrupts by the TSPD. It
93b44a4435SAchin Gupta  * validates the interrupt and upon success arranges entry into the TSP at
9402446137SSoby Mathew  * 'tsp_sel1_intr_entry()' for handling the interrupt.
9551bb1d73SMadhukar Pappireddy  * Typically, interrupts for a specific security state get handled in the same
9651bb1d73SMadhukar Pappireddy  * security execption level if the execution is in the same security state. For
9751bb1d73SMadhukar Pappireddy  * example, if a non-secure interrupt gets fired when CPU is executing in NS-EL2
9851bb1d73SMadhukar Pappireddy  * it gets handled in the non-secure world.
9951bb1d73SMadhukar Pappireddy  * However, interrupts belonging to the opposite security state typically demand
10051bb1d73SMadhukar Pappireddy  * a world(context) switch. This is inline with the security principle which
10151bb1d73SMadhukar Pappireddy  * states a secure interrupt has to be handled in the secure world.
10251bb1d73SMadhukar Pappireddy  * Hence, the TSPD in EL3 expects the context(handle) for a secure interrupt to
10351bb1d73SMadhukar Pappireddy  * be non-secure and vice versa.
10451bb1d73SMadhukar Pappireddy  * However, a race condition between non-secure and secure interrupts can lead to
10551bb1d73SMadhukar Pappireddy  * a scenario where the above assumptions do not hold true. This is demonstrated
10651bb1d73SMadhukar Pappireddy  * below through Note 1.
107b44a4435SAchin Gupta  ******************************************************************************/
tspd_sel1_interrupt_handler(uint32_t id,uint32_t flags,void * handle,void * cookie)108b44a4435SAchin Gupta static uint64_t tspd_sel1_interrupt_handler(uint32_t id,
109b44a4435SAchin Gupta 					    uint32_t flags,
110b44a4435SAchin Gupta 					    void *handle,
111b44a4435SAchin Gupta 					    void *cookie)
112b44a4435SAchin Gupta {
113b44a4435SAchin Gupta 	uint32_t linear_id;
114b44a4435SAchin Gupta 	tsp_context_t *tsp_ctx;
115b44a4435SAchin Gupta 
11651bb1d73SMadhukar Pappireddy 	/* Get a reference to this cpu's TSP context */
11751bb1d73SMadhukar Pappireddy 	linear_id = plat_my_core_pos();
11851bb1d73SMadhukar Pappireddy 	tsp_ctx = &tspd_sp_context[linear_id];
11951bb1d73SMadhukar Pappireddy 
12051bb1d73SMadhukar Pappireddy #if TSP_NS_INTR_ASYNC_PREEMPT
12151bb1d73SMadhukar Pappireddy 
12251bb1d73SMadhukar Pappireddy 	/*
12351bb1d73SMadhukar Pappireddy 	 * Note 1:
12451bb1d73SMadhukar Pappireddy 	 * Under the current interrupt routing model, interrupts from other
12551bb1d73SMadhukar Pappireddy 	 * world are routed to EL3 when TSP_NS_INTR_ASYNC_PREEMPT is enabled.
12651bb1d73SMadhukar Pappireddy 	 * Consider the following scenario:
12751bb1d73SMadhukar Pappireddy 	 * 1/ A non-secure payload(like tftf) requests a secure service from
12851bb1d73SMadhukar Pappireddy 	 *    TSP by invoking a yielding SMC call.
12951bb1d73SMadhukar Pappireddy 	 * 2/ Later, execution jumps to TSP in S-EL1 with the help of TSP
13051bb1d73SMadhukar Pappireddy 	 *    Dispatcher in Secure Monitor(EL3).
13151bb1d73SMadhukar Pappireddy 	 * 3/ While CPU is executing TSP, a Non-secure interrupt gets fired.
13251bb1d73SMadhukar Pappireddy 	 *    this demands a context switch to the non-secure world through
13351bb1d73SMadhukar Pappireddy 	 *    secure monitor.
13451bb1d73SMadhukar Pappireddy 	 * 4/ Consequently, TSP in S-EL1 get asynchronously pre-empted and
13551bb1d73SMadhukar Pappireddy 	 *    execution switches to secure monitor(EL3).
13651bb1d73SMadhukar Pappireddy 	 * 5/ EL3 tries to triage the (Non-secure) interrupt based on the
13751bb1d73SMadhukar Pappireddy 	 *    highest pending interrupt.
13851bb1d73SMadhukar Pappireddy 	 * 6/ However, while the NS Interrupt was pending, secure timer gets
13951bb1d73SMadhukar Pappireddy 	 *    fired which makes a S-EL1 interrupt to be pending.
14051bb1d73SMadhukar Pappireddy 	 * 7/ Hence, execution jumps to this companion handler of S-EL1
14151bb1d73SMadhukar Pappireddy 	 *    interrupt (i.e., tspd_sel1_interrupt_handler) even though the TSP
14251bb1d73SMadhukar Pappireddy 	 *    was pre-empted due to non-secure interrupt.
14351bb1d73SMadhukar Pappireddy 	 * 8/ The above sequence of events explain how TSP was pre-empted by
14451bb1d73SMadhukar Pappireddy 	 *    S-EL1 interrupt indirectly in an asynchronous way.
14551bb1d73SMadhukar Pappireddy 	 * 9/ Hence, we track the TSP pre-emption by S-EL1 interrupt using a
14651bb1d73SMadhukar Pappireddy 	 *    boolean variable per each core.
14751bb1d73SMadhukar Pappireddy 	 * 10/ This helps us to indicate that SMC call for TSP service was
14851bb1d73SMadhukar Pappireddy 	 *    pre-empted when execution resumes in non-secure world.
14951bb1d73SMadhukar Pappireddy 	 */
15051bb1d73SMadhukar Pappireddy 
15151bb1d73SMadhukar Pappireddy 	/* Check the security state when the exception was generated */
15251bb1d73SMadhukar Pappireddy 	if (get_interrupt_src_ss(flags) == NON_SECURE) {
15351bb1d73SMadhukar Pappireddy 		/* Sanity check the pointer to this cpu's context */
15451bb1d73SMadhukar Pappireddy 		assert(handle == cm_get_context(NON_SECURE));
15551bb1d73SMadhukar Pappireddy 
15651bb1d73SMadhukar Pappireddy 		/* Save the non-secure context before entering the TSP */
15751bb1d73SMadhukar Pappireddy 		cm_el1_sysregs_context_save(NON_SECURE);
15851bb1d73SMadhukar Pappireddy 		tsp_ctx->preempted_by_sel1_intr = false;
15951bb1d73SMadhukar Pappireddy 	} else {
16051bb1d73SMadhukar Pappireddy 		/* Sanity check the pointer to this cpu's context */
16151bb1d73SMadhukar Pappireddy 		assert(handle == cm_get_context(SECURE));
16251bb1d73SMadhukar Pappireddy 
16351bb1d73SMadhukar Pappireddy 		/* Save the secure context before entering the TSP for S-EL1
16451bb1d73SMadhukar Pappireddy 		 * interrupt handling
16551bb1d73SMadhukar Pappireddy 		 */
16651bb1d73SMadhukar Pappireddy 		cm_el1_sysregs_context_save(SECURE);
16751bb1d73SMadhukar Pappireddy 		tsp_ctx->preempted_by_sel1_intr = true;
16851bb1d73SMadhukar Pappireddy 	}
16951bb1d73SMadhukar Pappireddy #else
170b44a4435SAchin Gupta 	/* Check the security state when the exception was generated */
171b44a4435SAchin Gupta 	assert(get_interrupt_src_ss(flags) == NON_SECURE);
172b44a4435SAchin Gupta 
173b44a4435SAchin Gupta 	/* Sanity check the pointer to this cpu's context */
17408ab89d3SAndrew Thoelke 	assert(handle == cm_get_context(NON_SECURE));
175b44a4435SAchin Gupta 
176b44a4435SAchin Gupta 	/* Save the non-secure context before entering the TSP */
177b44a4435SAchin Gupta 	cm_el1_sysregs_context_save(NON_SECURE);
17851bb1d73SMadhukar Pappireddy #endif
179b44a4435SAchin Gupta 
18008ab89d3SAndrew Thoelke 	assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE));
181b44a4435SAchin Gupta 
182b44a4435SAchin Gupta 	/*
183b44a4435SAchin Gupta 	 * Determine if the TSP was previously preempted. Its last known
184b44a4435SAchin Gupta 	 * context has to be preserved in this case.
185b44a4435SAchin Gupta 	 * The TSP should return control to the TSPD after handling this
18602446137SSoby Mathew 	 * S-EL1 interrupt. Preserve essential EL3 context to allow entry into
18702446137SSoby Mathew 	 * the TSP at the S-EL1 interrupt entry point using the 'cpu_context'
18802446137SSoby Mathew 	 * structure. There is no need to save the secure system register
18902446137SSoby Mathew 	 * context since the TSP is supposed to preserve it during S-EL1
19002446137SSoby Mathew 	 * interrupt handling.
191b44a4435SAchin Gupta 	 */
19216292f54SDavid Cunado 	if (get_yield_smc_active_flag(tsp_ctx->state)) {
1932fe75a2dSZelalem 		tsp_ctx->saved_spsr_el3 = (uint32_t)SMC_GET_EL3(&tsp_ctx->cpu_ctx,
194b44a4435SAchin Gupta 						      CTX_SPSR_EL3);
195b44a4435SAchin Gupta 		tsp_ctx->saved_elr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx,
196b44a4435SAchin Gupta 						     CTX_ELR_EL3);
19702446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT
198f4f1ae77SSoby Mathew 		memcpy(&tsp_ctx->sp_ctx, &tsp_ctx->cpu_ctx, TSPD_SP_CTX_SIZE);
199f4f1ae77SSoby Mathew #endif
200b44a4435SAchin Gupta 	}
201b44a4435SAchin Gupta 
202b44a4435SAchin Gupta 	cm_el1_sysregs_context_restore(SECURE);
20302446137SSoby Mathew 	cm_set_elr_spsr_el3(SECURE, (uint64_t) &tsp_vectors->sel1_intr_entry,
204167a9357SAndrew Thoelke 		    SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS));
205f4f1ae77SSoby Mathew 
206b44a4435SAchin Gupta 	cm_set_next_eret_context(SECURE);
207b44a4435SAchin Gupta 
208b44a4435SAchin Gupta 	/*
20902446137SSoby Mathew 	 * Tell the TSP that it has to handle a S-EL1 interrupt synchronously.
21002446137SSoby Mathew 	 * Also the instruction in normal world where the interrupt was
21102446137SSoby Mathew 	 * generated is passed for debugging purposes. It is safe to retrieve
21202446137SSoby Mathew 	 * this address from ELR_EL3 as the secure context will not take effect
21302446137SSoby Mathew 	 * until el3_exit().
214b44a4435SAchin Gupta 	 */
21502446137SSoby Mathew 	SMC_RET2(&tsp_ctx->cpu_ctx, TSP_HANDLE_SEL1_INTR_AND_RETURN, read_elr_el3());
216b44a4435SAchin Gupta }
2177f366605SJeenu Viswambharan 
21802446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT
219f4f1ae77SSoby Mathew /*******************************************************************************
22002446137SSoby Mathew  * This function is the handler registered for Non secure interrupts by the
22102446137SSoby Mathew  * TSPD. It validates the interrupt and upon success arranges entry into the
22202446137SSoby Mathew  * normal world for handling the interrupt.
223f4f1ae77SSoby Mathew  ******************************************************************************/
tspd_ns_interrupt_handler(uint32_t id,uint32_t flags,void * handle,void * cookie)224f4f1ae77SSoby Mathew static uint64_t tspd_ns_interrupt_handler(uint32_t id,
225f4f1ae77SSoby Mathew 					    uint32_t flags,
226f4f1ae77SSoby Mathew 					    void *handle,
227f4f1ae77SSoby Mathew 					    void *cookie)
228f4f1ae77SSoby Mathew {
229f4f1ae77SSoby Mathew 	/* Check the security state when the exception was generated */
230f4f1ae77SSoby Mathew 	assert(get_interrupt_src_ss(flags) == SECURE);
231f4f1ae77SSoby Mathew 
232f4f1ae77SSoby Mathew 	/*
233f4f1ae77SSoby Mathew 	 * Disable the routing of NS interrupts from secure world to EL3 while
234f4f1ae77SSoby Mathew 	 * interrupted on this core.
235f4f1ae77SSoby Mathew 	 */
236f4f1ae77SSoby Mathew 	disable_intr_rm_local(INTR_TYPE_NS, SECURE);
237f4f1ae77SSoby Mathew 
238f4f1ae77SSoby Mathew 	return tspd_handle_sp_preemption(handle);
239f4f1ae77SSoby Mathew }
240f4f1ae77SSoby Mathew #endif
241f4f1ae77SSoby Mathew 
242375f538aSAchin Gupta /*******************************************************************************
243375f538aSAchin Gupta  * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type
244375f538aSAchin Gupta  * (aarch32/aarch64) if not already known and initialises the context for entry
245375f538aSAchin Gupta  * into the SP for its initialisation.
246375f538aSAchin Gupta  ******************************************************************************/
tspd_setup(void)247724fd958SMasahiro Yamada static int32_t tspd_setup(void)
248375f538aSAchin Gupta {
24950e27dadSVikram Kanigiri 	entry_point_info_t *tsp_ep_info;
250375f538aSAchin Gupta 	uint32_t linear_id;
251375f538aSAchin Gupta 
252fd650ff6SSoby Mathew 	linear_id = plat_my_core_pos();
253375f538aSAchin Gupta 
254375f538aSAchin Gupta 	/*
255375f538aSAchin Gupta 	 * Get information about the Secure Payload (BL32) image. Its
256375f538aSAchin Gupta 	 * absence is a critical failure.  TODO: Add support to
257375f538aSAchin Gupta 	 * conditionally include the SPD service
258375f538aSAchin Gupta 	 */
25950e27dadSVikram Kanigiri 	tsp_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
26050e27dadSVikram Kanigiri 	if (!tsp_ep_info) {
26150e27dadSVikram Kanigiri 		WARN("No TSP provided by BL2 boot loader, Booting device"
26250e27dadSVikram Kanigiri 			" without TSP initialization. SMC`s destined for TSP"
26350e27dadSVikram Kanigiri 			" will return SMC_UNK\n");
26450e27dadSVikram Kanigiri 		return 1;
26550e27dadSVikram Kanigiri 	}
266375f538aSAchin Gupta 
267375f538aSAchin Gupta 	/*
2687f366605SJeenu Viswambharan 	 * If there's no valid entry point for SP, we return a non-zero value
2697f366605SJeenu Viswambharan 	 * signalling failure initializing the service. We bail out without
2707f366605SJeenu Viswambharan 	 * registering any handlers
2717f366605SJeenu Viswambharan 	 */
27250e27dadSVikram Kanigiri 	if (!tsp_ep_info->pc)
2737f366605SJeenu Viswambharan 		return 1;
2747f366605SJeenu Viswambharan 
2757f366605SJeenu Viswambharan 	/*
2761645d3eeSSandrine Bailleux 	 * We could inspect the SP image and determine its execution
277375f538aSAchin Gupta 	 * state i.e whether AArch32 or AArch64. Assuming it's AArch64
278375f538aSAchin Gupta 	 * for the time being.
279375f538aSAchin Gupta 	 */
28050e27dadSVikram Kanigiri 	tspd_init_tsp_ep_state(tsp_ep_info,
281375f538aSAchin Gupta 				TSP_AARCH64,
28250e27dadSVikram Kanigiri 				tsp_ep_info->pc,
283375f538aSAchin Gupta 				&tspd_sp_context[linear_id]);
284375f538aSAchin Gupta 
285faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC
286faaa2e76SVikram Kanigiri 	bl31_set_next_image_type(SECURE);
287faaa2e76SVikram Kanigiri #else
2887f366605SJeenu Viswambharan 	/*
2897f366605SJeenu Viswambharan 	 * All TSPD initialization done. Now register our init function with
2907f366605SJeenu Viswambharan 	 * BL31 for deferred invocation
2917f366605SJeenu Viswambharan 	 */
2927f366605SJeenu Viswambharan 	bl31_register_bl32_init(&tspd_init);
293faaa2e76SVikram Kanigiri #endif
29450e27dadSVikram Kanigiri 	return 0;
295375f538aSAchin Gupta }
296375f538aSAchin Gupta 
297375f538aSAchin Gupta /*******************************************************************************
298375f538aSAchin Gupta  * This function passes control to the Secure Payload image (BL32) for the first
299375f538aSAchin Gupta  * time on the primary cpu after a cold boot. It assumes that a valid secure
300375f538aSAchin Gupta  * context has already been created by tspd_setup() which can be directly used.
301375f538aSAchin Gupta  * It also assumes that a valid non-secure context has been initialised by PSCI
302375f538aSAchin Gupta  * so it does not need to save and restore any non-secure state. This function
303375f538aSAchin Gupta  * performs a synchronous entry into the Secure payload. The SP passes control
3046871c5d3SVikram Kanigiri  * back to this routine through a SMC.
305375f538aSAchin Gupta  ******************************************************************************/
tspd_init(void)3066871c5d3SVikram Kanigiri int32_t tspd_init(void)
307375f538aSAchin Gupta {
308fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
309fb037bfbSDan Handley 	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
31050e27dadSVikram Kanigiri 	entry_point_info_t *tsp_entry_point;
311faaa2e76SVikram Kanigiri 	uint64_t rc;
31250e27dadSVikram Kanigiri 
31350e27dadSVikram Kanigiri 	/*
31450e27dadSVikram Kanigiri 	 * Get information about the Secure Payload (BL32) image. Its
31550e27dadSVikram Kanigiri 	 * absence is a critical failure.
31650e27dadSVikram Kanigiri 	 */
31750e27dadSVikram Kanigiri 	tsp_entry_point = bl31_plat_get_next_image_ep_info(SECURE);
31850e27dadSVikram Kanigiri 	assert(tsp_entry_point);
31950e27dadSVikram Kanigiri 
320fd650ff6SSoby Mathew 	cm_init_my_context(tsp_entry_point);
321375f538aSAchin Gupta 
322375f538aSAchin Gupta 	/*
323faaa2e76SVikram Kanigiri 	 * Arrange for an entry into the test secure payload. It will be
324faaa2e76SVikram Kanigiri 	 * returned via TSP_ENTRY_DONE case
325607084eeSAchin Gupta 	 */
326375f538aSAchin Gupta 	rc = tspd_synchronous_sp_entry(tsp_ctx);
327375f538aSAchin Gupta 	assert(rc != 0);
328b44a4435SAchin Gupta 
329375f538aSAchin Gupta 	return rc;
330375f538aSAchin Gupta }
331375f538aSAchin Gupta 
3327f366605SJeenu Viswambharan 
333375f538aSAchin Gupta /*******************************************************************************
334375f538aSAchin Gupta  * This function is responsible for handling all SMCs in the Trusted OS/App
335375f538aSAchin Gupta  * range from the non-secure state as defined in the SMC Calling Convention
336375f538aSAchin Gupta  * Document. It is also responsible for communicating with the Secure payload
337375f538aSAchin Gupta  * to delegate work and return results back to the non-secure state. Lastly it
338375f538aSAchin Gupta  * will also return any information that the secure payload needs to do the
339375f538aSAchin Gupta  * work assigned to it.
340375f538aSAchin Gupta  ******************************************************************************/
tspd_smc_handler(uint32_t smc_fid,u_register_t x1,u_register_t x2,u_register_t x3,u_register_t x4,void * cookie,void * handle,u_register_t flags)34157d1e5faSMasahiro Yamada static uintptr_t tspd_smc_handler(uint32_t smc_fid,
34257d1e5faSMasahiro Yamada 			 u_register_t x1,
34357d1e5faSMasahiro Yamada 			 u_register_t x2,
34457d1e5faSMasahiro Yamada 			 u_register_t x3,
34557d1e5faSMasahiro Yamada 			 u_register_t x4,
346375f538aSAchin Gupta 			 void *cookie,
347375f538aSAchin Gupta 			 void *handle,
34857d1e5faSMasahiro Yamada 			 u_register_t flags)
349375f538aSAchin Gupta {
350fb037bfbSDan Handley 	cpu_context_t *ns_cpu_context;
351fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos(), ns;
352fb037bfbSDan Handley 	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
353faaa2e76SVikram Kanigiri 	uint64_t rc;
354faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC
355faaa2e76SVikram Kanigiri 	entry_point_info_t *next_image_info;
356faaa2e76SVikram Kanigiri #endif
357375f538aSAchin Gupta 
358375f538aSAchin Gupta 	/* Determine which security state this SMC originated from */
359375f538aSAchin Gupta 	ns = is_caller_non_secure(flags);
360375f538aSAchin Gupta 
361375f538aSAchin Gupta 	switch (smc_fid) {
362375f538aSAchin Gupta 
363375f538aSAchin Gupta 	/*
364239b04faSSoby Mathew 	 * This function ID is used by TSP to indicate that it was
365239b04faSSoby Mathew 	 * preempted by a normal world IRQ.
366239b04faSSoby Mathew 	 *
367239b04faSSoby Mathew 	 */
368239b04faSSoby Mathew 	case TSP_PREEMPTED:
369239b04faSSoby Mathew 		if (ns)
370239b04faSSoby Mathew 			SMC_RET1(handle, SMC_UNK);
371239b04faSSoby Mathew 
372f4f1ae77SSoby Mathew 		return tspd_handle_sp_preemption(handle);
373239b04faSSoby Mathew 
374239b04faSSoby Mathew 	/*
375b44a4435SAchin Gupta 	 * This function ID is used only by the TSP to indicate that it has
37663b8440fSSoby Mathew 	 * finished handling a S-EL1 interrupt or was preempted by a higher
37763b8440fSSoby Mathew 	 * priority pending EL3 interrupt. Execution should resume
378b44a4435SAchin Gupta 	 * in the normal world.
379b44a4435SAchin Gupta 	 */
38002446137SSoby Mathew 	case TSP_HANDLED_S_EL1_INTR:
381b44a4435SAchin Gupta 		if (ns)
382b44a4435SAchin Gupta 			SMC_RET1(handle, SMC_UNK);
383b44a4435SAchin Gupta 
38408ab89d3SAndrew Thoelke 		assert(handle == cm_get_context(SECURE));
385b44a4435SAchin Gupta 
386b44a4435SAchin Gupta 		/*
387b44a4435SAchin Gupta 		 * Restore the relevant EL3 state which saved to service
388b44a4435SAchin Gupta 		 * this SMC.
389b44a4435SAchin Gupta 		 */
39016292f54SDavid Cunado 		if (get_yield_smc_active_flag(tsp_ctx->state)) {
391b44a4435SAchin Gupta 			SMC_SET_EL3(&tsp_ctx->cpu_ctx,
392b44a4435SAchin Gupta 				    CTX_SPSR_EL3,
393b44a4435SAchin Gupta 				    tsp_ctx->saved_spsr_el3);
394b44a4435SAchin Gupta 			SMC_SET_EL3(&tsp_ctx->cpu_ctx,
395b44a4435SAchin Gupta 				    CTX_ELR_EL3,
396b44a4435SAchin Gupta 				    tsp_ctx->saved_elr_el3);
39702446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT
398f4f1ae77SSoby Mathew 			/*
399f4f1ae77SSoby Mathew 			 * Need to restore the previously interrupted
400f4f1ae77SSoby Mathew 			 * secure context.
401f4f1ae77SSoby Mathew 			 */
402f4f1ae77SSoby Mathew 			memcpy(&tsp_ctx->cpu_ctx, &tsp_ctx->sp_ctx,
403f4f1ae77SSoby Mathew 				TSPD_SP_CTX_SIZE);
404f4f1ae77SSoby Mathew #endif
405b44a4435SAchin Gupta 		}
406b44a4435SAchin Gupta 
407b44a4435SAchin Gupta 		/* Get a reference to the non-secure context */
40808ab89d3SAndrew Thoelke 		ns_cpu_context = cm_get_context(NON_SECURE);
409b44a4435SAchin Gupta 		assert(ns_cpu_context);
410b44a4435SAchin Gupta 
411b44a4435SAchin Gupta 		/*
412b44a4435SAchin Gupta 		 * Restore non-secure state. There is no need to save the
413b44a4435SAchin Gupta 		 * secure system register context since the TSP was supposed
414b44a4435SAchin Gupta 		 * to preserve it during S-EL1 interrupt handling.
415b44a4435SAchin Gupta 		 */
416b44a4435SAchin Gupta 		cm_el1_sysregs_context_restore(NON_SECURE);
417b44a4435SAchin Gupta 		cm_set_next_eret_context(NON_SECURE);
418b44a4435SAchin Gupta 
41951bb1d73SMadhukar Pappireddy 		/* Refer to Note 1 in function tspd_sel1_interrupt_handler()*/
42051bb1d73SMadhukar Pappireddy #if TSP_NS_INTR_ASYNC_PREEMPT
42151bb1d73SMadhukar Pappireddy 		if (tsp_ctx->preempted_by_sel1_intr) {
42251bb1d73SMadhukar Pappireddy 			/* Reset the flag */
42351bb1d73SMadhukar Pappireddy 			tsp_ctx->preempted_by_sel1_intr = false;
42451bb1d73SMadhukar Pappireddy 
42551bb1d73SMadhukar Pappireddy 			SMC_RET1(ns_cpu_context, SMC_PREEMPTED);
42651bb1d73SMadhukar Pappireddy 		} else {
427b44a4435SAchin Gupta 			SMC_RET0((uint64_t) ns_cpu_context);
42851bb1d73SMadhukar Pappireddy 		}
42951bb1d73SMadhukar Pappireddy #else
43051bb1d73SMadhukar Pappireddy 		SMC_RET0((uint64_t) ns_cpu_context);
43151bb1d73SMadhukar Pappireddy #endif
43251bb1d73SMadhukar Pappireddy 
433b44a4435SAchin Gupta 
434b44a4435SAchin Gupta 	/*
435375f538aSAchin Gupta 	 * This function ID is used only by the SP to indicate it has
436375f538aSAchin Gupta 	 * finished initialising itself after a cold boot
437375f538aSAchin Gupta 	 */
438375f538aSAchin Gupta 	case TSP_ENTRY_DONE:
439375f538aSAchin Gupta 		if (ns)
440375f538aSAchin Gupta 			SMC_RET1(handle, SMC_UNK);
441375f538aSAchin Gupta 
442375f538aSAchin Gupta 		/*
443375f538aSAchin Gupta 		 * Stash the SP entry points information. This is done
444375f538aSAchin Gupta 		 * only once on the primary cpu
445375f538aSAchin Gupta 		 */
446399fb08fSAndrew Thoelke 		assert(tsp_vectors == NULL);
447399fb08fSAndrew Thoelke 		tsp_vectors = (tsp_vectors_t *) x1;
448375f538aSAchin Gupta 
449faaa2e76SVikram Kanigiri 		if (tsp_vectors) {
450faaa2e76SVikram Kanigiri 			set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON);
451faaa2e76SVikram Kanigiri 
452faaa2e76SVikram Kanigiri 			/*
453faaa2e76SVikram Kanigiri 			 * TSP has been successfully initialized. Register power
4548aabea33SPaul Beesley 			 * management hooks with PSCI
455faaa2e76SVikram Kanigiri 			 */
456faaa2e76SVikram Kanigiri 			psci_register_spd_pm_hook(&tspd_pm);
457faaa2e76SVikram Kanigiri 
458faaa2e76SVikram Kanigiri 			/*
459faaa2e76SVikram Kanigiri 			 * Register an interrupt handler for S-EL1 interrupts
460faaa2e76SVikram Kanigiri 			 * when generated during code executing in the
461faaa2e76SVikram Kanigiri 			 * non-secure state.
462faaa2e76SVikram Kanigiri 			 */
463faaa2e76SVikram Kanigiri 			flags = 0;
464faaa2e76SVikram Kanigiri 			set_interrupt_rm_flag(flags, NON_SECURE);
465faaa2e76SVikram Kanigiri 			rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
466faaa2e76SVikram Kanigiri 						tspd_sel1_interrupt_handler,
467faaa2e76SVikram Kanigiri 						flags);
468faaa2e76SVikram Kanigiri 			if (rc)
469faaa2e76SVikram Kanigiri 				panic();
470f4f1ae77SSoby Mathew 
47102446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT
472f4f1ae77SSoby Mathew 			/*
473f4f1ae77SSoby Mathew 			 * Register an interrupt handler for NS interrupts when
474f4f1ae77SSoby Mathew 			 * generated during code executing in secure state are
475f4f1ae77SSoby Mathew 			 * routed to EL3.
476f4f1ae77SSoby Mathew 			 */
477f4f1ae77SSoby Mathew 			flags = 0;
478f4f1ae77SSoby Mathew 			set_interrupt_rm_flag(flags, SECURE);
479f4f1ae77SSoby Mathew 
480f4f1ae77SSoby Mathew 			rc = register_interrupt_type_handler(INTR_TYPE_NS,
481f4f1ae77SSoby Mathew 						tspd_ns_interrupt_handler,
482f4f1ae77SSoby Mathew 						flags);
483f4f1ae77SSoby Mathew 			if (rc)
484f4f1ae77SSoby Mathew 				panic();
485f4f1ae77SSoby Mathew 
486f4f1ae77SSoby Mathew 			/*
487404dba53SSoby Mathew 			 * Disable the NS interrupt locally.
488f4f1ae77SSoby Mathew 			 */
489f4f1ae77SSoby Mathew 			disable_intr_rm_local(INTR_TYPE_NS, SECURE);
490f4f1ae77SSoby Mathew #endif
491faaa2e76SVikram Kanigiri 		}
492faaa2e76SVikram Kanigiri 
493faaa2e76SVikram Kanigiri 
494faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC
495faaa2e76SVikram Kanigiri 		/* Save the Secure EL1 system register context */
496faaa2e76SVikram Kanigiri 		assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx);
497faaa2e76SVikram Kanigiri 		cm_el1_sysregs_context_save(SECURE);
498faaa2e76SVikram Kanigiri 
499faaa2e76SVikram Kanigiri 		/* Program EL3 registers to enable entry into the next EL */
500faaa2e76SVikram Kanigiri 		next_image_info = bl31_plat_get_next_image_ep_info(NON_SECURE);
501faaa2e76SVikram Kanigiri 		assert(next_image_info);
502faaa2e76SVikram Kanigiri 		assert(NON_SECURE ==
503faaa2e76SVikram Kanigiri 				GET_SECURITY_STATE(next_image_info->h.attr));
504faaa2e76SVikram Kanigiri 
505fd650ff6SSoby Mathew 		cm_init_my_context(next_image_info);
506faaa2e76SVikram Kanigiri 		cm_prepare_el3_exit(NON_SECURE);
507faaa2e76SVikram Kanigiri 		SMC_RET0(cm_get_context(NON_SECURE));
508faaa2e76SVikram Kanigiri #else
509375f538aSAchin Gupta 		/*
510375f538aSAchin Gupta 		 * SP reports completion. The SPD must have initiated
511375f538aSAchin Gupta 		 * the original request through a synchronous entry
512375f538aSAchin Gupta 		 * into the SP. Jump back to the original C runtime
513375f538aSAchin Gupta 		 * context.
514375f538aSAchin Gupta 		 */
515916a2c1eSAchin Gupta 		tspd_synchronous_sp_exit(tsp_ctx, x1);
516185a23ffSJonathan Wright 		break;
517faaa2e76SVikram Kanigiri #endif
5183df6012aSDouglas Raillard 	/*
5193df6012aSDouglas Raillard 	 * This function ID is used only by the SP to indicate it has finished
52016292f54SDavid Cunado 	 * aborting a preempted Yielding SMC Call.
5213df6012aSDouglas Raillard 	 */
5223df6012aSDouglas Raillard 	case TSP_ABORT_DONE:
523375f538aSAchin Gupta 
524607084eeSAchin Gupta 	/*
5251645d3eeSSandrine Bailleux 	 * These function IDs are used only by the SP to indicate it has
526607084eeSAchin Gupta 	 * finished:
527607084eeSAchin Gupta 	 * 1. turning itself on in response to an earlier psci
528607084eeSAchin Gupta 	 *    cpu_on request
529607084eeSAchin Gupta 	 * 2. resuming itself after an earlier psci cpu_suspend
530607084eeSAchin Gupta 	 *    request.
531607084eeSAchin Gupta 	 */
532607084eeSAchin Gupta 	case TSP_ON_DONE:
533607084eeSAchin Gupta 	case TSP_RESUME_DONE:
534607084eeSAchin Gupta 
535607084eeSAchin Gupta 	/*
5361645d3eeSSandrine Bailleux 	 * These function IDs are used only by the SP to indicate it has
537607084eeSAchin Gupta 	 * finished:
538607084eeSAchin Gupta 	 * 1. suspending itself after an earlier psci cpu_suspend
539607084eeSAchin Gupta 	 *    request.
540607084eeSAchin Gupta 	 * 2. turning itself off in response to an earlier psci
541607084eeSAchin Gupta 	 *    cpu_off request.
542607084eeSAchin Gupta 	 */
543607084eeSAchin Gupta 	case TSP_OFF_DONE:
544607084eeSAchin Gupta 	case TSP_SUSPEND_DONE:
545d5f13093SJuan Castillo 	case TSP_SYSTEM_OFF_DONE:
546d5f13093SJuan Castillo 	case TSP_SYSTEM_RESET_DONE:
547607084eeSAchin Gupta 		if (ns)
548607084eeSAchin Gupta 			SMC_RET1(handle, SMC_UNK);
549607084eeSAchin Gupta 
550607084eeSAchin Gupta 		/*
551607084eeSAchin Gupta 		 * SP reports completion. The SPD must have initiated the
552607084eeSAchin Gupta 		 * original request through a synchronous entry into the SP.
553607084eeSAchin Gupta 		 * Jump back to the original C runtime context, and pass x1 as
554607084eeSAchin Gupta 		 * return value to the caller
555607084eeSAchin Gupta 		 */
556916a2c1eSAchin Gupta 		tspd_synchronous_sp_exit(tsp_ctx, x1);
557185a23ffSJonathan Wright 		break;
558607084eeSAchin Gupta 
559916a2c1eSAchin Gupta 		/*
560916a2c1eSAchin Gupta 		 * Request from non-secure client to perform an
561916a2c1eSAchin Gupta 		 * arithmetic operation or response from secure
562916a2c1eSAchin Gupta 		 * payload to an earlier request.
563916a2c1eSAchin Gupta 		 */
564239b04faSSoby Mathew 	case TSP_FAST_FID(TSP_ADD):
565239b04faSSoby Mathew 	case TSP_FAST_FID(TSP_SUB):
566239b04faSSoby Mathew 	case TSP_FAST_FID(TSP_MUL):
567239b04faSSoby Mathew 	case TSP_FAST_FID(TSP_DIV):
568239b04faSSoby Mathew 
56916292f54SDavid Cunado 	case TSP_YIELD_FID(TSP_ADD):
57016292f54SDavid Cunado 	case TSP_YIELD_FID(TSP_SUB):
57116292f54SDavid Cunado 	case TSP_YIELD_FID(TSP_MUL):
57216292f54SDavid Cunado 	case TSP_YIELD_FID(TSP_DIV):
5734d482156SDaniel Boulby 		/*
5744d482156SDaniel Boulby 		 * Request from non-secure client to perform a check
5754d482156SDaniel Boulby 		 * of the DIT PSTATE bit.
5764d482156SDaniel Boulby 		 */
5774d482156SDaniel Boulby 	case TSP_YIELD_FID(TSP_CHECK_DIT):
578*7623e085SJayanth Dodderi Chidanand 		/*
579*7623e085SJayanth Dodderi Chidanand 		 * Request from non-secure client to modify the EL1
580*7623e085SJayanth Dodderi Chidanand 		 * context registers.
581*7623e085SJayanth Dodderi Chidanand 		 */
582*7623e085SJayanth Dodderi Chidanand 	case TSP_YIELD_FID(TSP_MODIFY_EL1_CTX):
583916a2c1eSAchin Gupta 		if (ns) {
584916a2c1eSAchin Gupta 			/*
585916a2c1eSAchin Gupta 			 * This is a fresh request from the non-secure client.
586916a2c1eSAchin Gupta 			 * The parameters are in x1 and x2. Figure out which
587916a2c1eSAchin Gupta 			 * registers need to be preserved, save the non-secure
588916a2c1eSAchin Gupta 			 * state and send the request to the secure payload.
589916a2c1eSAchin Gupta 			 */
59008ab89d3SAndrew Thoelke 			assert(handle == cm_get_context(NON_SECURE));
591239b04faSSoby Mathew 
592239b04faSSoby Mathew 			/* Check if we are already preempted */
59316292f54SDavid Cunado 			if (get_yield_smc_active_flag(tsp_ctx->state))
594239b04faSSoby Mathew 				SMC_RET1(handle, SMC_UNK);
595239b04faSSoby Mathew 
596916a2c1eSAchin Gupta 			cm_el1_sysregs_context_save(NON_SECURE);
597916a2c1eSAchin Gupta 
598916a2c1eSAchin Gupta 			/* Save x1 and x2 for use by TSP_GET_ARGS call below */
599239b04faSSoby Mathew 			store_tsp_args(tsp_ctx, x1, x2);
600916a2c1eSAchin Gupta 
601916a2c1eSAchin Gupta 			/*
602916a2c1eSAchin Gupta 			 * We are done stashing the non-secure context. Ask the
603916a2c1eSAchin Gupta 			 * secure payload to do the work now.
604916a2c1eSAchin Gupta 			 */
605916a2c1eSAchin Gupta 
606916a2c1eSAchin Gupta 			/*
607916a2c1eSAchin Gupta 			 * Verify if there is a valid context to use, copy the
608916a2c1eSAchin Gupta 			 * operation type and parameters to the secure context
609916a2c1eSAchin Gupta 			 * and jump to the fast smc entry point in the secure
610916a2c1eSAchin Gupta 			 * payload. Entry into S-EL1 will take place upon exit
611916a2c1eSAchin Gupta 			 * from this function.
612916a2c1eSAchin Gupta 			 */
61308ab89d3SAndrew Thoelke 			assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE));
614239b04faSSoby Mathew 
615239b04faSSoby Mathew 			/* Set appropriate entry for SMC.
616239b04faSSoby Mathew 			 * We expect the TSP to manage the PSTATE.I and PSTATE.F
617239b04faSSoby Mathew 			 * flags as appropriate.
618239b04faSSoby Mathew 			 */
619239b04faSSoby Mathew 			if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_FAST) {
620239b04faSSoby Mathew 				cm_set_elr_el3(SECURE, (uint64_t)
621399fb08fSAndrew Thoelke 						&tsp_vectors->fast_smc_entry);
622239b04faSSoby Mathew 			} else {
62316292f54SDavid Cunado 				set_yield_smc_active_flag(tsp_ctx->state);
624239b04faSSoby Mathew 				cm_set_elr_el3(SECURE, (uint64_t)
62516292f54SDavid Cunado 						&tsp_vectors->yield_smc_entry);
62602446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT
627f4f1ae77SSoby Mathew 				/*
628f4f1ae77SSoby Mathew 				 * Enable the routing of NS interrupts to EL3
62916292f54SDavid Cunado 				 * during processing of a Yielding SMC Call on
63016292f54SDavid Cunado 				 * this core.
631f4f1ae77SSoby Mathew 				 */
632f4f1ae77SSoby Mathew 				enable_intr_rm_local(INTR_TYPE_NS, SECURE);
633f4f1ae77SSoby Mathew #endif
6341dd022caSJeenu Viswambharan 
6351dd022caSJeenu Viswambharan #if EL3_EXCEPTION_HANDLING
6361dd022caSJeenu Viswambharan 				/*
6371dd022caSJeenu Viswambharan 				 * With EL3 exception handling, while an SMC is
6381dd022caSJeenu Viswambharan 				 * being processed, Non-secure interrupts can't
6391dd022caSJeenu Viswambharan 				 * preempt Secure execution. However, for
6401dd022caSJeenu Viswambharan 				 * yielding SMCs, we want preemption to happen;
6411dd022caSJeenu Viswambharan 				 * so explicitly allow NS preemption in this
642472be0f7SJeenu Viswambharan 				 * case, and supply the preemption return code
643472be0f7SJeenu Viswambharan 				 * for TSP.
6441dd022caSJeenu Viswambharan 				 */
645472be0f7SJeenu Viswambharan 				ehf_allow_ns_preemption(TSP_PREEMPTED);
6461dd022caSJeenu Viswambharan #endif
647239b04faSSoby Mathew 			}
648239b04faSSoby Mathew 
649916a2c1eSAchin Gupta 			cm_el1_sysregs_context_restore(SECURE);
650916a2c1eSAchin Gupta 			cm_set_next_eret_context(SECURE);
651239b04faSSoby Mathew 			SMC_RET3(&tsp_ctx->cpu_ctx, smc_fid, x1, x2);
652916a2c1eSAchin Gupta 		} else {
653916a2c1eSAchin Gupta 			/*
654916a2c1eSAchin Gupta 			 * This is the result from the secure client of an
655239b04faSSoby Mathew 			 * earlier request. The results are in x1-x3. Copy it
656916a2c1eSAchin Gupta 			 * into the non-secure context, save the secure state
657916a2c1eSAchin Gupta 			 * and return to the non-secure state.
658916a2c1eSAchin Gupta 			 */
65908ab89d3SAndrew Thoelke 			assert(handle == cm_get_context(SECURE));
660916a2c1eSAchin Gupta 			cm_el1_sysregs_context_save(SECURE);
661916a2c1eSAchin Gupta 
662916a2c1eSAchin Gupta 			/* Get a reference to the non-secure context */
66308ab89d3SAndrew Thoelke 			ns_cpu_context = cm_get_context(NON_SECURE);
664916a2c1eSAchin Gupta 			assert(ns_cpu_context);
665916a2c1eSAchin Gupta 
666916a2c1eSAchin Gupta 			/* Restore non-secure state */
667916a2c1eSAchin Gupta 			cm_el1_sysregs_context_restore(NON_SECURE);
668916a2c1eSAchin Gupta 			cm_set_next_eret_context(NON_SECURE);
66916292f54SDavid Cunado 			if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_YIELD) {
67016292f54SDavid Cunado 				clr_yield_smc_active_flag(tsp_ctx->state);
67102446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT
672f4f1ae77SSoby Mathew 				/*
673f4f1ae77SSoby Mathew 				 * Disable the routing of NS interrupts to EL3
67416292f54SDavid Cunado 				 * after processing of a Yielding SMC Call on
67516292f54SDavid Cunado 				 * this core is finished.
676f4f1ae77SSoby Mathew 				 */
677f4f1ae77SSoby Mathew 				disable_intr_rm_local(INTR_TYPE_NS, SECURE);
678f4f1ae77SSoby Mathew #endif
679f4f1ae77SSoby Mathew 			}
680f4f1ae77SSoby Mathew 
681239b04faSSoby Mathew 			SMC_RET3(ns_cpu_context, x1, x2, x3);
682916a2c1eSAchin Gupta 		}
683a08a2014SDaniel Boulby 		assert(0); /* Unreachable */
684916a2c1eSAchin Gupta 
6853df6012aSDouglas Raillard 	/*
68616292f54SDavid Cunado 	 * Request from the non-secure world to abort a preempted Yielding SMC
68716292f54SDavid Cunado 	 * Call.
6883df6012aSDouglas Raillard 	 */
6893df6012aSDouglas Raillard 	case TSP_FID_ABORT:
6903df6012aSDouglas Raillard 		/* ABORT should only be invoked by normal world */
6913df6012aSDouglas Raillard 		if (!ns) {
6923df6012aSDouglas Raillard 			assert(0);
6933df6012aSDouglas Raillard 			break;
6943df6012aSDouglas Raillard 		}
6953df6012aSDouglas Raillard 
69657a5a56cSDouglas Raillard 		assert(handle == cm_get_context(NON_SECURE));
69757a5a56cSDouglas Raillard 		cm_el1_sysregs_context_save(NON_SECURE);
69857a5a56cSDouglas Raillard 
6993df6012aSDouglas Raillard 		/* Abort the preempted SMC request */
70057a5a56cSDouglas Raillard 		if (!tspd_abort_preempted_smc(tsp_ctx)) {
7013df6012aSDouglas Raillard 			/*
7023df6012aSDouglas Raillard 			 * If there was no preempted SMC to abort, return
7033df6012aSDouglas Raillard 			 * SMC_UNK.
70457a5a56cSDouglas Raillard 			 *
70557a5a56cSDouglas Raillard 			 * Restoring the NON_SECURE context is not necessary as
70657a5a56cSDouglas Raillard 			 * the synchronous entry did not take place if the
70757a5a56cSDouglas Raillard 			 * return code of tspd_abort_preempted_smc is zero.
7083df6012aSDouglas Raillard 			 */
70957a5a56cSDouglas Raillard 			cm_set_next_eret_context(NON_SECURE);
7103df6012aSDouglas Raillard 			break;
71157a5a56cSDouglas Raillard 		}
71257a5a56cSDouglas Raillard 
71357a5a56cSDouglas Raillard 		cm_el1_sysregs_context_restore(NON_SECURE);
71457a5a56cSDouglas Raillard 		cm_set_next_eret_context(NON_SECURE);
7157a317a70SAntonio Nino Diaz 		SMC_RET1(handle, SMC_OK);
716916a2c1eSAchin Gupta 
717916a2c1eSAchin Gupta 		/*
718239b04faSSoby Mathew 		 * Request from non secure world to resume the preempted
71916292f54SDavid Cunado 		 * Yielding SMC Call.
720239b04faSSoby Mathew 		 */
721239b04faSSoby Mathew 	case TSP_FID_RESUME:
722239b04faSSoby Mathew 		/* RESUME should be invoked only by normal world */
723239b04faSSoby Mathew 		if (!ns) {
724239b04faSSoby Mathew 			assert(0);
725239b04faSSoby Mathew 			break;
726239b04faSSoby Mathew 		}
727239b04faSSoby Mathew 
728239b04faSSoby Mathew 		/*
729239b04faSSoby Mathew 		 * This is a resume request from the non-secure client.
730239b04faSSoby Mathew 		 * save the non-secure state and send the request to
731239b04faSSoby Mathew 		 * the secure payload.
732239b04faSSoby Mathew 		 */
73308ab89d3SAndrew Thoelke 		assert(handle == cm_get_context(NON_SECURE));
734239b04faSSoby Mathew 
735239b04faSSoby Mathew 		/* Check if we are already preempted before resume */
73616292f54SDavid Cunado 		if (!get_yield_smc_active_flag(tsp_ctx->state))
737239b04faSSoby Mathew 			SMC_RET1(handle, SMC_UNK);
738239b04faSSoby Mathew 
739239b04faSSoby Mathew 		cm_el1_sysregs_context_save(NON_SECURE);
740239b04faSSoby Mathew 
741239b04faSSoby Mathew 		/*
742239b04faSSoby Mathew 		 * We are done stashing the non-secure context. Ask the
743239b04faSSoby Mathew 		 * secure payload to do the work now.
744239b04faSSoby Mathew 		 */
74502446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT
746f4f1ae77SSoby Mathew 		/*
747f4f1ae77SSoby Mathew 		 * Enable the routing of NS interrupts to EL3 during resumption
74816292f54SDavid Cunado 		 * of a Yielding SMC Call on this core.
749f4f1ae77SSoby Mathew 		 */
750f4f1ae77SSoby Mathew 		enable_intr_rm_local(INTR_TYPE_NS, SECURE);
751f4f1ae77SSoby Mathew #endif
752f4f1ae77SSoby Mathew 
7531dd022caSJeenu Viswambharan #if EL3_EXCEPTION_HANDLING
7541dd022caSJeenu Viswambharan 		/*
7551dd022caSJeenu Viswambharan 		 * Allow the resumed yielding SMC processing to be preempted by
756472be0f7SJeenu Viswambharan 		 * Non-secure interrupts. Also, supply the preemption return
757472be0f7SJeenu Viswambharan 		 * code for TSP.
7581dd022caSJeenu Viswambharan 		 */
759472be0f7SJeenu Viswambharan 		ehf_allow_ns_preemption(TSP_PREEMPTED);
7601dd022caSJeenu Viswambharan #endif
761239b04faSSoby Mathew 
762239b04faSSoby Mathew 		/* We just need to return to the preempted point in
763239b04faSSoby Mathew 		 * TSP and the execution will resume as normal.
764239b04faSSoby Mathew 		 */
765239b04faSSoby Mathew 		cm_el1_sysregs_context_restore(SECURE);
766239b04faSSoby Mathew 		cm_set_next_eret_context(SECURE);
76710b65ecfSSoby Mathew 		SMC_RET0(&tsp_ctx->cpu_ctx);
768239b04faSSoby Mathew 
769239b04faSSoby Mathew 		/*
770916a2c1eSAchin Gupta 		 * This is a request from the secure payload for more arguments
771916a2c1eSAchin Gupta 		 * for an ongoing arithmetic operation requested by the
772916a2c1eSAchin Gupta 		 * non-secure world. Simply return the arguments from the non-
773916a2c1eSAchin Gupta 		 * secure client in the original call.
774916a2c1eSAchin Gupta 		 */
775916a2c1eSAchin Gupta 	case TSP_GET_ARGS:
776916a2c1eSAchin Gupta 		if (ns)
777916a2c1eSAchin Gupta 			SMC_RET1(handle, SMC_UNK);
778916a2c1eSAchin Gupta 
779239b04faSSoby Mathew 		get_tsp_args(tsp_ctx, x1, x2);
780239b04faSSoby Mathew 		SMC_RET2(handle, x1, x2);
781916a2c1eSAchin Gupta 
78252538b9bSJeenu Viswambharan 	case TOS_CALL_COUNT:
78352538b9bSJeenu Viswambharan 		/*
78452538b9bSJeenu Viswambharan 		 * Return the number of service function IDs implemented to
78552538b9bSJeenu Viswambharan 		 * provide service to non-secure
78652538b9bSJeenu Viswambharan 		 */
78752538b9bSJeenu Viswambharan 		SMC_RET1(handle, TSP_NUM_FID);
78852538b9bSJeenu Viswambharan 
78952538b9bSJeenu Viswambharan 	case TOS_UID:
79052538b9bSJeenu Viswambharan 		/* Return TSP UID to the caller */
79152538b9bSJeenu Viswambharan 		SMC_UUID_RET(handle, tsp_uuid);
79252538b9bSJeenu Viswambharan 
79352538b9bSJeenu Viswambharan 	case TOS_CALL_VERSION:
79452538b9bSJeenu Viswambharan 		/* Return the version of current implementation */
79552538b9bSJeenu Viswambharan 		SMC_RET2(handle, TSP_VERSION_MAJOR, TSP_VERSION_MINOR);
79652538b9bSJeenu Viswambharan 
797375f538aSAchin Gupta 	default:
798607084eeSAchin Gupta 		break;
799375f538aSAchin Gupta 	}
800375f538aSAchin Gupta 
801607084eeSAchin Gupta 	SMC_RET1(handle, SMC_UNK);
802375f538aSAchin Gupta }
803375f538aSAchin Gupta 
804239b04faSSoby Mathew /* Define a SPD runtime service descriptor for fast SMC calls */
805375f538aSAchin Gupta DECLARE_RT_SVC(
806239b04faSSoby Mathew 	tspd_fast,
807375f538aSAchin Gupta 
808375f538aSAchin Gupta 	OEN_TOS_START,
809375f538aSAchin Gupta 	OEN_TOS_END,
810375f538aSAchin Gupta 	SMC_TYPE_FAST,
811375f538aSAchin Gupta 	tspd_setup,
812375f538aSAchin Gupta 	tspd_smc_handler
813375f538aSAchin Gupta );
814239b04faSSoby Mathew 
81516292f54SDavid Cunado /* Define a SPD runtime service descriptor for Yielding SMC Calls */
816239b04faSSoby Mathew DECLARE_RT_SVC(
817239b04faSSoby Mathew 	tspd_std,
818239b04faSSoby Mathew 
819239b04faSSoby Mathew 	OEN_TOS_START,
820239b04faSSoby Mathew 	OEN_TOS_END,
82116292f54SDavid Cunado 	SMC_TYPE_YIELD,
822239b04faSSoby Mathew 	NULL,
823239b04faSSoby Mathew 	tspd_smc_handler
824239b04faSSoby Mathew );
825