xref: /rk3399_ARM-atf/services/spd/tspd/tspd_main.c (revision 7a317a70d4829feaeaf841584aff58162d13eb07)
1375f538aSAchin Gupta /*
23df6012aSDouglas Raillard  * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved.
3375f538aSAchin Gupta  *
4375f538aSAchin Gupta  * Redistribution and use in source and binary forms, with or without
5375f538aSAchin Gupta  * modification, are permitted provided that the following conditions are met:
6375f538aSAchin Gupta  *
7375f538aSAchin Gupta  * Redistributions of source code must retain the above copyright notice, this
8375f538aSAchin Gupta  * list of conditions and the following disclaimer.
9375f538aSAchin Gupta  *
10375f538aSAchin Gupta  * Redistributions in binary form must reproduce the above copyright notice,
11375f538aSAchin Gupta  * this list of conditions and the following disclaimer in the documentation
12375f538aSAchin Gupta  * and/or other materials provided with the distribution.
13375f538aSAchin Gupta  *
14375f538aSAchin Gupta  * Neither the name of ARM nor the names of its contributors may be used
15375f538aSAchin Gupta  * to endorse or promote products derived from this software without specific
16375f538aSAchin Gupta  * prior written permission.
17375f538aSAchin Gupta  *
18375f538aSAchin Gupta  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19375f538aSAchin Gupta  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20375f538aSAchin Gupta  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21375f538aSAchin Gupta  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22375f538aSAchin Gupta  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23375f538aSAchin Gupta  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24375f538aSAchin Gupta  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25375f538aSAchin Gupta  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26375f538aSAchin Gupta  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27375f538aSAchin Gupta  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28375f538aSAchin Gupta  * POSSIBILITY OF SUCH DAMAGE.
29375f538aSAchin Gupta  */
30375f538aSAchin Gupta 
31375f538aSAchin Gupta 
32375f538aSAchin Gupta /*******************************************************************************
33375f538aSAchin Gupta  * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a
34375f538aSAchin Gupta  * plug-in component to the Secure Monitor, registered as a runtime service. The
35375f538aSAchin Gupta  * SPD is expected to be a functional extension of the Secure Payload (SP) that
36375f538aSAchin Gupta  * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting
37375f538aSAchin Gupta  * the Trusted OS/Applications range to the dispatcher. The SPD will either
38375f538aSAchin Gupta  * handle the request locally or delegate it to the Secure Payload. It is also
39375f538aSAchin Gupta  * responsible for initialising and maintaining communication with the SP.
40375f538aSAchin Gupta  ******************************************************************************/
41375f538aSAchin Gupta #include <arch_helpers.h>
4297043ac9SDan Handley #include <assert.h>
4397043ac9SDan Handley #include <bl_common.h>
4497043ac9SDan Handley #include <bl31.h>
45375f538aSAchin Gupta #include <context_mgmt.h>
46b44a4435SAchin Gupta #include <debug.h>
47b44a4435SAchin Gupta #include <errno.h>
48b44a4435SAchin Gupta #include <platform.h>
49375f538aSAchin Gupta #include <runtime_svc.h>
5097043ac9SDan Handley #include <stddef.h>
51f4f1ae77SSoby Mathew #include <string.h>
52375f538aSAchin Gupta #include <tsp.h>
5352538b9bSJeenu Viswambharan #include <uuid.h>
5435e98e55SDan Handley #include "tspd_private.h"
55375f538aSAchin Gupta 
56375f538aSAchin Gupta /*******************************************************************************
57399fb08fSAndrew Thoelke  * Address of the entrypoint vector table in the Secure Payload. It is
58399fb08fSAndrew Thoelke  * initialised once on the primary core after a cold boot.
59375f538aSAchin Gupta  ******************************************************************************/
60399fb08fSAndrew Thoelke tsp_vectors_t *tsp_vectors;
61375f538aSAchin Gupta 
62375f538aSAchin Gupta /*******************************************************************************
63375f538aSAchin Gupta  * Array to keep track of per-cpu Secure Payload state
64375f538aSAchin Gupta  ******************************************************************************/
65fb037bfbSDan Handley tsp_context_t tspd_sp_context[TSPD_CORE_COUNT];
66375f538aSAchin Gupta 
677f366605SJeenu Viswambharan 
6852538b9bSJeenu Viswambharan /* TSP UID */
6952538b9bSJeenu Viswambharan DEFINE_SVC_UUID(tsp_uuid,
7052538b9bSJeenu Viswambharan 		0x5b3056a0, 0x3291, 0x427b, 0x98, 0x11,
7152538b9bSJeenu Viswambharan 		0x71, 0x68, 0xca, 0x50, 0xf3, 0xfa);
7252538b9bSJeenu Viswambharan 
736871c5d3SVikram Kanigiri int32_t tspd_init(void);
747f366605SJeenu Viswambharan 
75404dba53SSoby Mathew /*
76404dba53SSoby Mathew  * This helper function handles Secure EL1 preemption. The preemption could be
77404dba53SSoby Mathew  * due Non Secure interrupts or EL3 interrupts. In both the cases we context
78404dba53SSoby Mathew  * switch to the normal world and in case of EL3 interrupts, it will again be
79404dba53SSoby Mathew  * routed to EL3 which will get handled at the exception vectors.
80404dba53SSoby Mathew  */
81f4f1ae77SSoby Mathew uint64_t tspd_handle_sp_preemption(void *handle)
82f4f1ae77SSoby Mathew {
83f4f1ae77SSoby Mathew 	cpu_context_t *ns_cpu_context;
84404dba53SSoby Mathew 
85f4f1ae77SSoby Mathew 	assert(handle == cm_get_context(SECURE));
86f4f1ae77SSoby Mathew 	cm_el1_sysregs_context_save(SECURE);
87f4f1ae77SSoby Mathew 	/* Get a reference to the non-secure context */
88f4f1ae77SSoby Mathew 	ns_cpu_context = cm_get_context(NON_SECURE);
89f4f1ae77SSoby Mathew 	assert(ns_cpu_context);
90f4f1ae77SSoby Mathew 
91f4f1ae77SSoby Mathew 	/*
9263b8440fSSoby Mathew 	 * To allow Secure EL1 interrupt handler to re-enter TSP while TSP
9363b8440fSSoby Mathew 	 * is preempted, the secure system register context which will get
9463b8440fSSoby Mathew 	 * overwritten must be additionally saved. This is currently done
9563b8440fSSoby Mathew 	 * by the TSPD S-EL1 interrupt handler.
9663b8440fSSoby Mathew 	 */
9763b8440fSSoby Mathew 
9863b8440fSSoby Mathew 	/*
9963b8440fSSoby Mathew 	 * Restore non-secure state.
100f4f1ae77SSoby Mathew 	 */
101f4f1ae77SSoby Mathew 	cm_el1_sysregs_context_restore(NON_SECURE);
102f4f1ae77SSoby Mathew 	cm_set_next_eret_context(NON_SECURE);
103f4f1ae77SSoby Mathew 
104404dba53SSoby Mathew 	/*
10563b8440fSSoby Mathew 	 * The TSP was preempted during STD SMC execution.
10663b8440fSSoby Mathew 	 * Return back to the normal world with SMC_PREEMPTED as error
10763b8440fSSoby Mathew 	 * code in x0.
108404dba53SSoby Mathew 	 */
109f4f1ae77SSoby Mathew 	SMC_RET1(ns_cpu_context, SMC_PREEMPTED);
110f4f1ae77SSoby Mathew }
111404dba53SSoby Mathew 
112b44a4435SAchin Gupta /*******************************************************************************
113b44a4435SAchin Gupta  * This function is the handler registered for S-EL1 interrupts by the TSPD. It
114b44a4435SAchin Gupta  * validates the interrupt and upon success arranges entry into the TSP at
11502446137SSoby Mathew  * 'tsp_sel1_intr_entry()' for handling the interrupt.
116b44a4435SAchin Gupta  ******************************************************************************/
117b44a4435SAchin Gupta static uint64_t tspd_sel1_interrupt_handler(uint32_t id,
118b44a4435SAchin Gupta 					    uint32_t flags,
119b44a4435SAchin Gupta 					    void *handle,
120b44a4435SAchin Gupta 					    void *cookie)
121b44a4435SAchin Gupta {
122b44a4435SAchin Gupta 	uint32_t linear_id;
123b44a4435SAchin Gupta 	tsp_context_t *tsp_ctx;
124b44a4435SAchin Gupta 
125b44a4435SAchin Gupta 	/* Check the security state when the exception was generated */
126b44a4435SAchin Gupta 	assert(get_interrupt_src_ss(flags) == NON_SECURE);
127b44a4435SAchin Gupta 
128b44a4435SAchin Gupta 	/* Sanity check the pointer to this cpu's context */
12908ab89d3SAndrew Thoelke 	assert(handle == cm_get_context(NON_SECURE));
130b44a4435SAchin Gupta 
131b44a4435SAchin Gupta 	/* Save the non-secure context before entering the TSP */
132b44a4435SAchin Gupta 	cm_el1_sysregs_context_save(NON_SECURE);
133b44a4435SAchin Gupta 
134b44a4435SAchin Gupta 	/* Get a reference to this cpu's TSP context */
135fd650ff6SSoby Mathew 	linear_id = plat_my_core_pos();
136b44a4435SAchin Gupta 	tsp_ctx = &tspd_sp_context[linear_id];
13708ab89d3SAndrew Thoelke 	assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE));
138b44a4435SAchin Gupta 
139b44a4435SAchin Gupta 	/*
140b44a4435SAchin Gupta 	 * Determine if the TSP was previously preempted. Its last known
141b44a4435SAchin Gupta 	 * context has to be preserved in this case.
142b44a4435SAchin Gupta 	 * The TSP should return control to the TSPD after handling this
14302446137SSoby Mathew 	 * S-EL1 interrupt. Preserve essential EL3 context to allow entry into
14402446137SSoby Mathew 	 * the TSP at the S-EL1 interrupt entry point using the 'cpu_context'
14502446137SSoby Mathew 	 * structure. There is no need to save the secure system register
14602446137SSoby Mathew 	 * context since the TSP is supposed to preserve it during S-EL1
14702446137SSoby Mathew 	 * interrupt handling.
148b44a4435SAchin Gupta 	 */
149b44a4435SAchin Gupta 	if (get_std_smc_active_flag(tsp_ctx->state)) {
150b44a4435SAchin Gupta 		tsp_ctx->saved_spsr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx,
151b44a4435SAchin Gupta 						      CTX_SPSR_EL3);
152b44a4435SAchin Gupta 		tsp_ctx->saved_elr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx,
153b44a4435SAchin Gupta 						     CTX_ELR_EL3);
15402446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT
155f4f1ae77SSoby Mathew 		/*Need to save the previously interrupted secure context */
156f4f1ae77SSoby Mathew 		memcpy(&tsp_ctx->sp_ctx, &tsp_ctx->cpu_ctx, TSPD_SP_CTX_SIZE);
157f4f1ae77SSoby Mathew #endif
158b44a4435SAchin Gupta 	}
159b44a4435SAchin Gupta 
160b44a4435SAchin Gupta 	cm_el1_sysregs_context_restore(SECURE);
16102446137SSoby Mathew 	cm_set_elr_spsr_el3(SECURE, (uint64_t) &tsp_vectors->sel1_intr_entry,
162167a9357SAndrew Thoelke 		    SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS));
163f4f1ae77SSoby Mathew 
164b44a4435SAchin Gupta 	cm_set_next_eret_context(SECURE);
165b44a4435SAchin Gupta 
166b44a4435SAchin Gupta 	/*
16702446137SSoby Mathew 	 * Tell the TSP that it has to handle a S-EL1 interrupt synchronously.
16802446137SSoby Mathew 	 * Also the instruction in normal world where the interrupt was
16902446137SSoby Mathew 	 * generated is passed for debugging purposes. It is safe to retrieve
17002446137SSoby Mathew 	 * this address from ELR_EL3 as the secure context will not take effect
17102446137SSoby Mathew 	 * until el3_exit().
172b44a4435SAchin Gupta 	 */
17302446137SSoby Mathew 	SMC_RET2(&tsp_ctx->cpu_ctx, TSP_HANDLE_SEL1_INTR_AND_RETURN, read_elr_el3());
174b44a4435SAchin Gupta }
1757f366605SJeenu Viswambharan 
17602446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT
177f4f1ae77SSoby Mathew /*******************************************************************************
17802446137SSoby Mathew  * This function is the handler registered for Non secure interrupts by the
17902446137SSoby Mathew  * TSPD. It validates the interrupt and upon success arranges entry into the
18002446137SSoby Mathew  * normal world for handling the interrupt.
181f4f1ae77SSoby Mathew  ******************************************************************************/
182f4f1ae77SSoby Mathew static uint64_t tspd_ns_interrupt_handler(uint32_t id,
183f4f1ae77SSoby Mathew 					    uint32_t flags,
184f4f1ae77SSoby Mathew 					    void *handle,
185f4f1ae77SSoby Mathew 					    void *cookie)
186f4f1ae77SSoby Mathew {
187f4f1ae77SSoby Mathew 	/* Check the security state when the exception was generated */
188f4f1ae77SSoby Mathew 	assert(get_interrupt_src_ss(flags) == SECURE);
189f4f1ae77SSoby Mathew 
190f4f1ae77SSoby Mathew 	/*
191f4f1ae77SSoby Mathew 	 * Disable the routing of NS interrupts from secure world to EL3 while
192f4f1ae77SSoby Mathew 	 * interrupted on this core.
193f4f1ae77SSoby Mathew 	 */
194f4f1ae77SSoby Mathew 	disable_intr_rm_local(INTR_TYPE_NS, SECURE);
195f4f1ae77SSoby Mathew 
196f4f1ae77SSoby Mathew 	return tspd_handle_sp_preemption(handle);
197f4f1ae77SSoby Mathew }
198f4f1ae77SSoby Mathew #endif
199f4f1ae77SSoby Mathew 
200375f538aSAchin Gupta /*******************************************************************************
201375f538aSAchin Gupta  * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type
202375f538aSAchin Gupta  * (aarch32/aarch64) if not already known and initialises the context for entry
203375f538aSAchin Gupta  * into the SP for its initialisation.
204375f538aSAchin Gupta  ******************************************************************************/
205375f538aSAchin Gupta int32_t tspd_setup(void)
206375f538aSAchin Gupta {
20750e27dadSVikram Kanigiri 	entry_point_info_t *tsp_ep_info;
208375f538aSAchin Gupta 	uint32_t linear_id;
209375f538aSAchin Gupta 
210fd650ff6SSoby Mathew 	linear_id = plat_my_core_pos();
211375f538aSAchin Gupta 
212375f538aSAchin Gupta 	/*
213375f538aSAchin Gupta 	 * Get information about the Secure Payload (BL32) image. Its
214375f538aSAchin Gupta 	 * absence is a critical failure.  TODO: Add support to
215375f538aSAchin Gupta 	 * conditionally include the SPD service
216375f538aSAchin Gupta 	 */
21750e27dadSVikram Kanigiri 	tsp_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
21850e27dadSVikram Kanigiri 	if (!tsp_ep_info) {
21950e27dadSVikram Kanigiri 		WARN("No TSP provided by BL2 boot loader, Booting device"
22050e27dadSVikram Kanigiri 			" without TSP initialization. SMC`s destined for TSP"
22150e27dadSVikram Kanigiri 			" will return SMC_UNK\n");
22250e27dadSVikram Kanigiri 		return 1;
22350e27dadSVikram Kanigiri 	}
224375f538aSAchin Gupta 
225375f538aSAchin Gupta 	/*
2267f366605SJeenu Viswambharan 	 * If there's no valid entry point for SP, we return a non-zero value
2277f366605SJeenu Viswambharan 	 * signalling failure initializing the service. We bail out without
2287f366605SJeenu Viswambharan 	 * registering any handlers
2297f366605SJeenu Viswambharan 	 */
23050e27dadSVikram Kanigiri 	if (!tsp_ep_info->pc)
2317f366605SJeenu Viswambharan 		return 1;
2327f366605SJeenu Viswambharan 
2337f366605SJeenu Viswambharan 	/*
2341645d3eeSSandrine Bailleux 	 * We could inspect the SP image and determine its execution
235375f538aSAchin Gupta 	 * state i.e whether AArch32 or AArch64. Assuming it's AArch64
236375f538aSAchin Gupta 	 * for the time being.
237375f538aSAchin Gupta 	 */
23850e27dadSVikram Kanigiri 	tspd_init_tsp_ep_state(tsp_ep_info,
239375f538aSAchin Gupta 				TSP_AARCH64,
24050e27dadSVikram Kanigiri 				tsp_ep_info->pc,
241375f538aSAchin Gupta 				&tspd_sp_context[linear_id]);
242375f538aSAchin Gupta 
243faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC
244faaa2e76SVikram Kanigiri 	bl31_set_next_image_type(SECURE);
245faaa2e76SVikram Kanigiri #else
2467f366605SJeenu Viswambharan 	/*
2477f366605SJeenu Viswambharan 	 * All TSPD initialization done. Now register our init function with
2487f366605SJeenu Viswambharan 	 * BL31 for deferred invocation
2497f366605SJeenu Viswambharan 	 */
2507f366605SJeenu Viswambharan 	bl31_register_bl32_init(&tspd_init);
251faaa2e76SVikram Kanigiri #endif
25250e27dadSVikram Kanigiri 	return 0;
253375f538aSAchin Gupta }
254375f538aSAchin Gupta 
255375f538aSAchin Gupta /*******************************************************************************
256375f538aSAchin Gupta  * This function passes control to the Secure Payload image (BL32) for the first
257375f538aSAchin Gupta  * time on the primary cpu after a cold boot. It assumes that a valid secure
258375f538aSAchin Gupta  * context has already been created by tspd_setup() which can be directly used.
259375f538aSAchin Gupta  * It also assumes that a valid non-secure context has been initialised by PSCI
260375f538aSAchin Gupta  * so it does not need to save and restore any non-secure state. This function
261375f538aSAchin Gupta  * performs a synchronous entry into the Secure payload. The SP passes control
2626871c5d3SVikram Kanigiri  * back to this routine through a SMC.
263375f538aSAchin Gupta  ******************************************************************************/
2646871c5d3SVikram Kanigiri int32_t tspd_init(void)
265375f538aSAchin Gupta {
266fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
267fb037bfbSDan Handley 	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
26850e27dadSVikram Kanigiri 	entry_point_info_t *tsp_entry_point;
269faaa2e76SVikram Kanigiri 	uint64_t rc;
27050e27dadSVikram Kanigiri 
27150e27dadSVikram Kanigiri 	/*
27250e27dadSVikram Kanigiri 	 * Get information about the Secure Payload (BL32) image. Its
27350e27dadSVikram Kanigiri 	 * absence is a critical failure.
27450e27dadSVikram Kanigiri 	 */
27550e27dadSVikram Kanigiri 	tsp_entry_point = bl31_plat_get_next_image_ep_info(SECURE);
27650e27dadSVikram Kanigiri 	assert(tsp_entry_point);
27750e27dadSVikram Kanigiri 
278fd650ff6SSoby Mathew 	cm_init_my_context(tsp_entry_point);
279375f538aSAchin Gupta 
280375f538aSAchin Gupta 	/*
281faaa2e76SVikram Kanigiri 	 * Arrange for an entry into the test secure payload. It will be
282faaa2e76SVikram Kanigiri 	 * returned via TSP_ENTRY_DONE case
283607084eeSAchin Gupta 	 */
284375f538aSAchin Gupta 	rc = tspd_synchronous_sp_entry(tsp_ctx);
285375f538aSAchin Gupta 	assert(rc != 0);
286b44a4435SAchin Gupta 
287375f538aSAchin Gupta 	return rc;
288375f538aSAchin Gupta }
289375f538aSAchin Gupta 
2907f366605SJeenu Viswambharan 
291375f538aSAchin Gupta /*******************************************************************************
292375f538aSAchin Gupta  * This function is responsible for handling all SMCs in the Trusted OS/App
293375f538aSAchin Gupta  * range from the non-secure state as defined in the SMC Calling Convention
294375f538aSAchin Gupta  * Document. It is also responsible for communicating with the Secure payload
295375f538aSAchin Gupta  * to delegate work and return results back to the non-secure state. Lastly it
296375f538aSAchin Gupta  * will also return any information that the secure payload needs to do the
297375f538aSAchin Gupta  * work assigned to it.
298375f538aSAchin Gupta  ******************************************************************************/
299375f538aSAchin Gupta uint64_t tspd_smc_handler(uint32_t smc_fid,
300375f538aSAchin Gupta 			 uint64_t x1,
301375f538aSAchin Gupta 			 uint64_t x2,
302375f538aSAchin Gupta 			 uint64_t x3,
303375f538aSAchin Gupta 			 uint64_t x4,
304375f538aSAchin Gupta 			 void *cookie,
305375f538aSAchin Gupta 			 void *handle,
306375f538aSAchin Gupta 			 uint64_t flags)
307375f538aSAchin Gupta {
308fb037bfbSDan Handley 	cpu_context_t *ns_cpu_context;
309fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos(), ns;
310fb037bfbSDan Handley 	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
311faaa2e76SVikram Kanigiri 	uint64_t rc;
312faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC
313faaa2e76SVikram Kanigiri 	entry_point_info_t *next_image_info;
314faaa2e76SVikram Kanigiri #endif
315375f538aSAchin Gupta 
316375f538aSAchin Gupta 	/* Determine which security state this SMC originated from */
317375f538aSAchin Gupta 	ns = is_caller_non_secure(flags);
318375f538aSAchin Gupta 
319375f538aSAchin Gupta 	switch (smc_fid) {
320375f538aSAchin Gupta 
321375f538aSAchin Gupta 	/*
322239b04faSSoby Mathew 	 * This function ID is used by TSP to indicate that it was
323239b04faSSoby Mathew 	 * preempted by a normal world IRQ.
324239b04faSSoby Mathew 	 *
325239b04faSSoby Mathew 	 */
326239b04faSSoby Mathew 	case TSP_PREEMPTED:
327239b04faSSoby Mathew 		if (ns)
328239b04faSSoby Mathew 			SMC_RET1(handle, SMC_UNK);
329239b04faSSoby Mathew 
330f4f1ae77SSoby Mathew 		return tspd_handle_sp_preemption(handle);
331239b04faSSoby Mathew 
332239b04faSSoby Mathew 	/*
333b44a4435SAchin Gupta 	 * This function ID is used only by the TSP to indicate that it has
33463b8440fSSoby Mathew 	 * finished handling a S-EL1 interrupt or was preempted by a higher
33563b8440fSSoby Mathew 	 * priority pending EL3 interrupt. Execution should resume
336b44a4435SAchin Gupta 	 * in the normal world.
337b44a4435SAchin Gupta 	 */
33802446137SSoby Mathew 	case TSP_HANDLED_S_EL1_INTR:
339b44a4435SAchin Gupta 		if (ns)
340b44a4435SAchin Gupta 			SMC_RET1(handle, SMC_UNK);
341b44a4435SAchin Gupta 
34208ab89d3SAndrew Thoelke 		assert(handle == cm_get_context(SECURE));
343b44a4435SAchin Gupta 
344b44a4435SAchin Gupta 		/*
345b44a4435SAchin Gupta 		 * Restore the relevant EL3 state which saved to service
346b44a4435SAchin Gupta 		 * this SMC.
347b44a4435SAchin Gupta 		 */
348b44a4435SAchin Gupta 		if (get_std_smc_active_flag(tsp_ctx->state)) {
349b44a4435SAchin Gupta 			SMC_SET_EL3(&tsp_ctx->cpu_ctx,
350b44a4435SAchin Gupta 				    CTX_SPSR_EL3,
351b44a4435SAchin Gupta 				    tsp_ctx->saved_spsr_el3);
352b44a4435SAchin Gupta 			SMC_SET_EL3(&tsp_ctx->cpu_ctx,
353b44a4435SAchin Gupta 				    CTX_ELR_EL3,
354b44a4435SAchin Gupta 				    tsp_ctx->saved_elr_el3);
35502446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT
356f4f1ae77SSoby Mathew 			/*
357f4f1ae77SSoby Mathew 			 * Need to restore the previously interrupted
358f4f1ae77SSoby Mathew 			 * secure context.
359f4f1ae77SSoby Mathew 			 */
360f4f1ae77SSoby Mathew 			memcpy(&tsp_ctx->cpu_ctx, &tsp_ctx->sp_ctx,
361f4f1ae77SSoby Mathew 				TSPD_SP_CTX_SIZE);
362f4f1ae77SSoby Mathew #endif
363b44a4435SAchin Gupta 		}
364b44a4435SAchin Gupta 
365b44a4435SAchin Gupta 		/* Get a reference to the non-secure context */
36608ab89d3SAndrew Thoelke 		ns_cpu_context = cm_get_context(NON_SECURE);
367b44a4435SAchin Gupta 		assert(ns_cpu_context);
368b44a4435SAchin Gupta 
369b44a4435SAchin Gupta 		/*
370b44a4435SAchin Gupta 		 * Restore non-secure state. There is no need to save the
371b44a4435SAchin Gupta 		 * secure system register context since the TSP was supposed
372b44a4435SAchin Gupta 		 * to preserve it during S-EL1 interrupt handling.
373b44a4435SAchin Gupta 		 */
374b44a4435SAchin Gupta 		cm_el1_sysregs_context_restore(NON_SECURE);
375b44a4435SAchin Gupta 		cm_set_next_eret_context(NON_SECURE);
376b44a4435SAchin Gupta 
377b44a4435SAchin Gupta 		SMC_RET0((uint64_t) ns_cpu_context);
378b44a4435SAchin Gupta 
379b44a4435SAchin Gupta 	/*
380375f538aSAchin Gupta 	 * This function ID is used only by the SP to indicate it has
381375f538aSAchin Gupta 	 * finished initialising itself after a cold boot
382375f538aSAchin Gupta 	 */
383375f538aSAchin Gupta 	case TSP_ENTRY_DONE:
384375f538aSAchin Gupta 		if (ns)
385375f538aSAchin Gupta 			SMC_RET1(handle, SMC_UNK);
386375f538aSAchin Gupta 
387375f538aSAchin Gupta 		/*
388375f538aSAchin Gupta 		 * Stash the SP entry points information. This is done
389375f538aSAchin Gupta 		 * only once on the primary cpu
390375f538aSAchin Gupta 		 */
391399fb08fSAndrew Thoelke 		assert(tsp_vectors == NULL);
392399fb08fSAndrew Thoelke 		tsp_vectors = (tsp_vectors_t *) x1;
393375f538aSAchin Gupta 
394faaa2e76SVikram Kanigiri 		if (tsp_vectors) {
395faaa2e76SVikram Kanigiri 			set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON);
396faaa2e76SVikram Kanigiri 
397faaa2e76SVikram Kanigiri 			/*
398faaa2e76SVikram Kanigiri 			 * TSP has been successfully initialized. Register power
399faaa2e76SVikram Kanigiri 			 * managemnt hooks with PSCI
400faaa2e76SVikram Kanigiri 			 */
401faaa2e76SVikram Kanigiri 			psci_register_spd_pm_hook(&tspd_pm);
402faaa2e76SVikram Kanigiri 
403faaa2e76SVikram Kanigiri 			/*
404faaa2e76SVikram Kanigiri 			 * Register an interrupt handler for S-EL1 interrupts
405faaa2e76SVikram Kanigiri 			 * when generated during code executing in the
406faaa2e76SVikram Kanigiri 			 * non-secure state.
407faaa2e76SVikram Kanigiri 			 */
408faaa2e76SVikram Kanigiri 			flags = 0;
409faaa2e76SVikram Kanigiri 			set_interrupt_rm_flag(flags, NON_SECURE);
410faaa2e76SVikram Kanigiri 			rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
411faaa2e76SVikram Kanigiri 						tspd_sel1_interrupt_handler,
412faaa2e76SVikram Kanigiri 						flags);
413faaa2e76SVikram Kanigiri 			if (rc)
414faaa2e76SVikram Kanigiri 				panic();
415f4f1ae77SSoby Mathew 
41602446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT
417f4f1ae77SSoby Mathew 			/*
418f4f1ae77SSoby Mathew 			 * Register an interrupt handler for NS interrupts when
419f4f1ae77SSoby Mathew 			 * generated during code executing in secure state are
420f4f1ae77SSoby Mathew 			 * routed to EL3.
421f4f1ae77SSoby Mathew 			 */
422f4f1ae77SSoby Mathew 			flags = 0;
423f4f1ae77SSoby Mathew 			set_interrupt_rm_flag(flags, SECURE);
424f4f1ae77SSoby Mathew 
425f4f1ae77SSoby Mathew 			rc = register_interrupt_type_handler(INTR_TYPE_NS,
426f4f1ae77SSoby Mathew 						tspd_ns_interrupt_handler,
427f4f1ae77SSoby Mathew 						flags);
428f4f1ae77SSoby Mathew 			if (rc)
429f4f1ae77SSoby Mathew 				panic();
430f4f1ae77SSoby Mathew 
431f4f1ae77SSoby Mathew 			/*
432404dba53SSoby Mathew 			 * Disable the NS interrupt locally.
433f4f1ae77SSoby Mathew 			 */
434f4f1ae77SSoby Mathew 			disable_intr_rm_local(INTR_TYPE_NS, SECURE);
435f4f1ae77SSoby Mathew #endif
436faaa2e76SVikram Kanigiri 		}
437faaa2e76SVikram Kanigiri 
438faaa2e76SVikram Kanigiri 
439faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC
440faaa2e76SVikram Kanigiri 		/* Save the Secure EL1 system register context */
441faaa2e76SVikram Kanigiri 		assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx);
442faaa2e76SVikram Kanigiri 		cm_el1_sysregs_context_save(SECURE);
443faaa2e76SVikram Kanigiri 
444faaa2e76SVikram Kanigiri 		/* Program EL3 registers to enable entry into the next EL */
445faaa2e76SVikram Kanigiri 		next_image_info = bl31_plat_get_next_image_ep_info(NON_SECURE);
446faaa2e76SVikram Kanigiri 		assert(next_image_info);
447faaa2e76SVikram Kanigiri 		assert(NON_SECURE ==
448faaa2e76SVikram Kanigiri 				GET_SECURITY_STATE(next_image_info->h.attr));
449faaa2e76SVikram Kanigiri 
450fd650ff6SSoby Mathew 		cm_init_my_context(next_image_info);
451faaa2e76SVikram Kanigiri 		cm_prepare_el3_exit(NON_SECURE);
452faaa2e76SVikram Kanigiri 		SMC_RET0(cm_get_context(NON_SECURE));
453faaa2e76SVikram Kanigiri #else
454375f538aSAchin Gupta 		/*
455375f538aSAchin Gupta 		 * SP reports completion. The SPD must have initiated
456375f538aSAchin Gupta 		 * the original request through a synchronous entry
457375f538aSAchin Gupta 		 * into the SP. Jump back to the original C runtime
458375f538aSAchin Gupta 		 * context.
459375f538aSAchin Gupta 		 */
460916a2c1eSAchin Gupta 		tspd_synchronous_sp_exit(tsp_ctx, x1);
461faaa2e76SVikram Kanigiri #endif
4623df6012aSDouglas Raillard 	/*
4633df6012aSDouglas Raillard 	 * This function ID is used only by the SP to indicate it has finished
4643df6012aSDouglas Raillard 	 * aborting a preempted Standard SMC request.
4653df6012aSDouglas Raillard 	 */
4663df6012aSDouglas Raillard 	case TSP_ABORT_DONE:
467375f538aSAchin Gupta 
468607084eeSAchin Gupta 	/*
4691645d3eeSSandrine Bailleux 	 * These function IDs are used only by the SP to indicate it has
470607084eeSAchin Gupta 	 * finished:
471607084eeSAchin Gupta 	 * 1. turning itself on in response to an earlier psci
472607084eeSAchin Gupta 	 *    cpu_on request
473607084eeSAchin Gupta 	 * 2. resuming itself after an earlier psci cpu_suspend
474607084eeSAchin Gupta 	 *    request.
475607084eeSAchin Gupta 	 */
476607084eeSAchin Gupta 	case TSP_ON_DONE:
477607084eeSAchin Gupta 	case TSP_RESUME_DONE:
478607084eeSAchin Gupta 
479607084eeSAchin Gupta 	/*
4801645d3eeSSandrine Bailleux 	 * These function IDs are used only by the SP to indicate it has
481607084eeSAchin Gupta 	 * finished:
482607084eeSAchin Gupta 	 * 1. suspending itself after an earlier psci cpu_suspend
483607084eeSAchin Gupta 	 *    request.
484607084eeSAchin Gupta 	 * 2. turning itself off in response to an earlier psci
485607084eeSAchin Gupta 	 *    cpu_off request.
486607084eeSAchin Gupta 	 */
487607084eeSAchin Gupta 	case TSP_OFF_DONE:
488607084eeSAchin Gupta 	case TSP_SUSPEND_DONE:
489d5f13093SJuan Castillo 	case TSP_SYSTEM_OFF_DONE:
490d5f13093SJuan Castillo 	case TSP_SYSTEM_RESET_DONE:
491607084eeSAchin Gupta 		if (ns)
492607084eeSAchin Gupta 			SMC_RET1(handle, SMC_UNK);
493607084eeSAchin Gupta 
494607084eeSAchin Gupta 		/*
495607084eeSAchin Gupta 		 * SP reports completion. The SPD must have initiated the
496607084eeSAchin Gupta 		 * original request through a synchronous entry into the SP.
497607084eeSAchin Gupta 		 * Jump back to the original C runtime context, and pass x1 as
498607084eeSAchin Gupta 		 * return value to the caller
499607084eeSAchin Gupta 		 */
500916a2c1eSAchin Gupta 		tspd_synchronous_sp_exit(tsp_ctx, x1);
501607084eeSAchin Gupta 
502916a2c1eSAchin Gupta 		/*
503916a2c1eSAchin Gupta 		 * Request from non-secure client to perform an
504916a2c1eSAchin Gupta 		 * arithmetic operation or response from secure
505916a2c1eSAchin Gupta 		 * payload to an earlier request.
506916a2c1eSAchin Gupta 		 */
507239b04faSSoby Mathew 	case TSP_FAST_FID(TSP_ADD):
508239b04faSSoby Mathew 	case TSP_FAST_FID(TSP_SUB):
509239b04faSSoby Mathew 	case TSP_FAST_FID(TSP_MUL):
510239b04faSSoby Mathew 	case TSP_FAST_FID(TSP_DIV):
511239b04faSSoby Mathew 
512239b04faSSoby Mathew 	case TSP_STD_FID(TSP_ADD):
513239b04faSSoby Mathew 	case TSP_STD_FID(TSP_SUB):
514239b04faSSoby Mathew 	case TSP_STD_FID(TSP_MUL):
515239b04faSSoby Mathew 	case TSP_STD_FID(TSP_DIV):
516916a2c1eSAchin Gupta 		if (ns) {
517916a2c1eSAchin Gupta 			/*
518916a2c1eSAchin Gupta 			 * This is a fresh request from the non-secure client.
519916a2c1eSAchin Gupta 			 * The parameters are in x1 and x2. Figure out which
520916a2c1eSAchin Gupta 			 * registers need to be preserved, save the non-secure
521916a2c1eSAchin Gupta 			 * state and send the request to the secure payload.
522916a2c1eSAchin Gupta 			 */
52308ab89d3SAndrew Thoelke 			assert(handle == cm_get_context(NON_SECURE));
524239b04faSSoby Mathew 
525239b04faSSoby Mathew 			/* Check if we are already preempted */
526239b04faSSoby Mathew 			if (get_std_smc_active_flag(tsp_ctx->state))
527239b04faSSoby Mathew 				SMC_RET1(handle, SMC_UNK);
528239b04faSSoby Mathew 
529916a2c1eSAchin Gupta 			cm_el1_sysregs_context_save(NON_SECURE);
530916a2c1eSAchin Gupta 
531916a2c1eSAchin Gupta 			/* Save x1 and x2 for use by TSP_GET_ARGS call below */
532239b04faSSoby Mathew 			store_tsp_args(tsp_ctx, x1, x2);
533916a2c1eSAchin Gupta 
534916a2c1eSAchin Gupta 			/*
535916a2c1eSAchin Gupta 			 * We are done stashing the non-secure context. Ask the
536916a2c1eSAchin Gupta 			 * secure payload to do the work now.
537916a2c1eSAchin Gupta 			 */
538916a2c1eSAchin Gupta 
539916a2c1eSAchin Gupta 			/*
540916a2c1eSAchin Gupta 			 * Verify if there is a valid context to use, copy the
541916a2c1eSAchin Gupta 			 * operation type and parameters to the secure context
542916a2c1eSAchin Gupta 			 * and jump to the fast smc entry point in the secure
543916a2c1eSAchin Gupta 			 * payload. Entry into S-EL1 will take place upon exit
544916a2c1eSAchin Gupta 			 * from this function.
545916a2c1eSAchin Gupta 			 */
54608ab89d3SAndrew Thoelke 			assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE));
547239b04faSSoby Mathew 
548239b04faSSoby Mathew 			/* Set appropriate entry for SMC.
549239b04faSSoby Mathew 			 * We expect the TSP to manage the PSTATE.I and PSTATE.F
550239b04faSSoby Mathew 			 * flags as appropriate.
551239b04faSSoby Mathew 			 */
552239b04faSSoby Mathew 			if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_FAST) {
553239b04faSSoby Mathew 				cm_set_elr_el3(SECURE, (uint64_t)
554399fb08fSAndrew Thoelke 						&tsp_vectors->fast_smc_entry);
555239b04faSSoby Mathew 			} else {
556239b04faSSoby Mathew 				set_std_smc_active_flag(tsp_ctx->state);
557239b04faSSoby Mathew 				cm_set_elr_el3(SECURE, (uint64_t)
558399fb08fSAndrew Thoelke 						&tsp_vectors->std_smc_entry);
55902446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT
560f4f1ae77SSoby Mathew 				/*
561f4f1ae77SSoby Mathew 				 * Enable the routing of NS interrupts to EL3
562f4f1ae77SSoby Mathew 				 * during STD SMC processing on this core.
563f4f1ae77SSoby Mathew 				 */
564f4f1ae77SSoby Mathew 				enable_intr_rm_local(INTR_TYPE_NS, SECURE);
565f4f1ae77SSoby Mathew #endif
566239b04faSSoby Mathew 			}
567239b04faSSoby Mathew 
568916a2c1eSAchin Gupta 			cm_el1_sysregs_context_restore(SECURE);
569916a2c1eSAchin Gupta 			cm_set_next_eret_context(SECURE);
570239b04faSSoby Mathew 			SMC_RET3(&tsp_ctx->cpu_ctx, smc_fid, x1, x2);
571916a2c1eSAchin Gupta 		} else {
572916a2c1eSAchin Gupta 			/*
573916a2c1eSAchin Gupta 			 * This is the result from the secure client of an
574239b04faSSoby Mathew 			 * earlier request. The results are in x1-x3. Copy it
575916a2c1eSAchin Gupta 			 * into the non-secure context, save the secure state
576916a2c1eSAchin Gupta 			 * and return to the non-secure state.
577916a2c1eSAchin Gupta 			 */
57808ab89d3SAndrew Thoelke 			assert(handle == cm_get_context(SECURE));
579916a2c1eSAchin Gupta 			cm_el1_sysregs_context_save(SECURE);
580916a2c1eSAchin Gupta 
581916a2c1eSAchin Gupta 			/* Get a reference to the non-secure context */
58208ab89d3SAndrew Thoelke 			ns_cpu_context = cm_get_context(NON_SECURE);
583916a2c1eSAchin Gupta 			assert(ns_cpu_context);
584916a2c1eSAchin Gupta 
585916a2c1eSAchin Gupta 			/* Restore non-secure state */
586916a2c1eSAchin Gupta 			cm_el1_sysregs_context_restore(NON_SECURE);
587916a2c1eSAchin Gupta 			cm_set_next_eret_context(NON_SECURE);
588f4f1ae77SSoby Mathew 			if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_STD) {
589239b04faSSoby Mathew 				clr_std_smc_active_flag(tsp_ctx->state);
59002446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT
591f4f1ae77SSoby Mathew 				/*
592f4f1ae77SSoby Mathew 				 * Disable the routing of NS interrupts to EL3
593f4f1ae77SSoby Mathew 				 * after STD SMC processing is finished on this
594f4f1ae77SSoby Mathew 				 * core.
595f4f1ae77SSoby Mathew 				 */
596f4f1ae77SSoby Mathew 				disable_intr_rm_local(INTR_TYPE_NS, SECURE);
597f4f1ae77SSoby Mathew #endif
598f4f1ae77SSoby Mathew 			}
599f4f1ae77SSoby Mathew 
600239b04faSSoby Mathew 			SMC_RET3(ns_cpu_context, x1, x2, x3);
601916a2c1eSAchin Gupta 		}
602916a2c1eSAchin Gupta 
603916a2c1eSAchin Gupta 		break;
6043df6012aSDouglas Raillard 	/*
6053df6012aSDouglas Raillard 	 * Request from the non-secure world to abort a preempted Standard SMC
6063df6012aSDouglas Raillard 	 * call.
6073df6012aSDouglas Raillard 	 */
6083df6012aSDouglas Raillard 	case TSP_FID_ABORT:
6093df6012aSDouglas Raillard 		/* ABORT should only be invoked by normal world */
6103df6012aSDouglas Raillard 		if (!ns) {
6113df6012aSDouglas Raillard 			assert(0);
6123df6012aSDouglas Raillard 			break;
6133df6012aSDouglas Raillard 		}
6143df6012aSDouglas Raillard 
61557a5a56cSDouglas Raillard 		assert(handle == cm_get_context(NON_SECURE));
61657a5a56cSDouglas Raillard 		cm_el1_sysregs_context_save(NON_SECURE);
61757a5a56cSDouglas Raillard 
6183df6012aSDouglas Raillard 		/* Abort the preempted SMC request */
61957a5a56cSDouglas Raillard 		if (!tspd_abort_preempted_smc(tsp_ctx)) {
6203df6012aSDouglas Raillard 			/*
6213df6012aSDouglas Raillard 			 * If there was no preempted SMC to abort, return
6223df6012aSDouglas Raillard 			 * SMC_UNK.
62357a5a56cSDouglas Raillard 			 *
62457a5a56cSDouglas Raillard 			 * Restoring the NON_SECURE context is not necessary as
62557a5a56cSDouglas Raillard 			 * the synchronous entry did not take place if the
62657a5a56cSDouglas Raillard 			 * return code of tspd_abort_preempted_smc is zero.
6273df6012aSDouglas Raillard 			 */
62857a5a56cSDouglas Raillard 			cm_set_next_eret_context(NON_SECURE);
6293df6012aSDouglas Raillard 			break;
63057a5a56cSDouglas Raillard 		}
63157a5a56cSDouglas Raillard 
63257a5a56cSDouglas Raillard 		cm_el1_sysregs_context_restore(NON_SECURE);
63357a5a56cSDouglas Raillard 		cm_set_next_eret_context(NON_SECURE);
634*7a317a70SAntonio Nino Diaz 		SMC_RET1(handle, SMC_OK);
635916a2c1eSAchin Gupta 
636916a2c1eSAchin Gupta 		/*
637239b04faSSoby Mathew 		 * Request from non secure world to resume the preempted
638239b04faSSoby Mathew 		 * Standard SMC call.
639239b04faSSoby Mathew 		 */
640239b04faSSoby Mathew 	case TSP_FID_RESUME:
641239b04faSSoby Mathew 		/* RESUME should be invoked only by normal world */
642239b04faSSoby Mathew 		if (!ns) {
643239b04faSSoby Mathew 			assert(0);
644239b04faSSoby Mathew 			break;
645239b04faSSoby Mathew 		}
646239b04faSSoby Mathew 
647239b04faSSoby Mathew 		/*
648239b04faSSoby Mathew 		 * This is a resume request from the non-secure client.
649239b04faSSoby Mathew 		 * save the non-secure state and send the request to
650239b04faSSoby Mathew 		 * the secure payload.
651239b04faSSoby Mathew 		 */
65208ab89d3SAndrew Thoelke 		assert(handle == cm_get_context(NON_SECURE));
653239b04faSSoby Mathew 
654239b04faSSoby Mathew 		/* Check if we are already preempted before resume */
655239b04faSSoby Mathew 		if (!get_std_smc_active_flag(tsp_ctx->state))
656239b04faSSoby Mathew 			SMC_RET1(handle, SMC_UNK);
657239b04faSSoby Mathew 
658239b04faSSoby Mathew 		cm_el1_sysregs_context_save(NON_SECURE);
659239b04faSSoby Mathew 
660239b04faSSoby Mathew 		/*
661239b04faSSoby Mathew 		 * We are done stashing the non-secure context. Ask the
662239b04faSSoby Mathew 		 * secure payload to do the work now.
663239b04faSSoby Mathew 		 */
66402446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT
665f4f1ae77SSoby Mathew 		/*
666f4f1ae77SSoby Mathew 		 * Enable the routing of NS interrupts to EL3 during resumption
667f4f1ae77SSoby Mathew 		 * of STD SMC call on this core.
668f4f1ae77SSoby Mathew 		 */
669f4f1ae77SSoby Mathew 		enable_intr_rm_local(INTR_TYPE_NS, SECURE);
670f4f1ae77SSoby Mathew #endif
671f4f1ae77SSoby Mathew 
672f4f1ae77SSoby Mathew 
673239b04faSSoby Mathew 
674239b04faSSoby Mathew 		/* We just need to return to the preempted point in
675239b04faSSoby Mathew 		 * TSP and the execution will resume as normal.
676239b04faSSoby Mathew 		 */
677239b04faSSoby Mathew 		cm_el1_sysregs_context_restore(SECURE);
678239b04faSSoby Mathew 		cm_set_next_eret_context(SECURE);
67910b65ecfSSoby Mathew 		SMC_RET0(&tsp_ctx->cpu_ctx);
680239b04faSSoby Mathew 
681239b04faSSoby Mathew 		/*
682916a2c1eSAchin Gupta 		 * This is a request from the secure payload for more arguments
683916a2c1eSAchin Gupta 		 * for an ongoing arithmetic operation requested by the
684916a2c1eSAchin Gupta 		 * non-secure world. Simply return the arguments from the non-
685916a2c1eSAchin Gupta 		 * secure client in the original call.
686916a2c1eSAchin Gupta 		 */
687916a2c1eSAchin Gupta 	case TSP_GET_ARGS:
688916a2c1eSAchin Gupta 		if (ns)
689916a2c1eSAchin Gupta 			SMC_RET1(handle, SMC_UNK);
690916a2c1eSAchin Gupta 
691239b04faSSoby Mathew 		get_tsp_args(tsp_ctx, x1, x2);
692239b04faSSoby Mathew 		SMC_RET2(handle, x1, x2);
693916a2c1eSAchin Gupta 
69452538b9bSJeenu Viswambharan 	case TOS_CALL_COUNT:
69552538b9bSJeenu Viswambharan 		/*
69652538b9bSJeenu Viswambharan 		 * Return the number of service function IDs implemented to
69752538b9bSJeenu Viswambharan 		 * provide service to non-secure
69852538b9bSJeenu Viswambharan 		 */
69952538b9bSJeenu Viswambharan 		SMC_RET1(handle, TSP_NUM_FID);
70052538b9bSJeenu Viswambharan 
70152538b9bSJeenu Viswambharan 	case TOS_UID:
70252538b9bSJeenu Viswambharan 		/* Return TSP UID to the caller */
70352538b9bSJeenu Viswambharan 		SMC_UUID_RET(handle, tsp_uuid);
70452538b9bSJeenu Viswambharan 
70552538b9bSJeenu Viswambharan 	case TOS_CALL_VERSION:
70652538b9bSJeenu Viswambharan 		/* Return the version of current implementation */
70752538b9bSJeenu Viswambharan 		SMC_RET2(handle, TSP_VERSION_MAJOR, TSP_VERSION_MINOR);
70852538b9bSJeenu Viswambharan 
709375f538aSAchin Gupta 	default:
710607084eeSAchin Gupta 		break;
711375f538aSAchin Gupta 	}
712375f538aSAchin Gupta 
713607084eeSAchin Gupta 	SMC_RET1(handle, SMC_UNK);
714375f538aSAchin Gupta }
715375f538aSAchin Gupta 
716239b04faSSoby Mathew /* Define a SPD runtime service descriptor for fast SMC calls */
717375f538aSAchin Gupta DECLARE_RT_SVC(
718239b04faSSoby Mathew 	tspd_fast,
719375f538aSAchin Gupta 
720375f538aSAchin Gupta 	OEN_TOS_START,
721375f538aSAchin Gupta 	OEN_TOS_END,
722375f538aSAchin Gupta 	SMC_TYPE_FAST,
723375f538aSAchin Gupta 	tspd_setup,
724375f538aSAchin Gupta 	tspd_smc_handler
725375f538aSAchin Gupta );
726239b04faSSoby Mathew 
727239b04faSSoby Mathew /* Define a SPD runtime service descriptor for standard SMC calls */
728239b04faSSoby Mathew DECLARE_RT_SVC(
729239b04faSSoby Mathew 	tspd_std,
730239b04faSSoby Mathew 
731239b04faSSoby Mathew 	OEN_TOS_START,
732239b04faSSoby Mathew 	OEN_TOS_END,
733239b04faSSoby Mathew 	SMC_TYPE_STD,
734239b04faSSoby Mathew 	NULL,
735239b04faSSoby Mathew 	tspd_smc_handler
736239b04faSSoby Mathew );
737