xref: /rk3399_ARM-atf/services/spd/tspd/tspd_main.c (revision a37810853630098559d93f1b9ecdc222e63ef9cf)
1375f538aSAchin Gupta /*
2375f538aSAchin Gupta  * Copyright (c) 2013-2014, 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>
51375f538aSAchin Gupta #include <tsp.h>
5252538b9bSJeenu Viswambharan #include <uuid.h>
5335e98e55SDan Handley #include "tspd_private.h"
54375f538aSAchin Gupta 
55375f538aSAchin Gupta /*******************************************************************************
56399fb08fSAndrew Thoelke  * Address of the entrypoint vector table in the Secure Payload. It is
57399fb08fSAndrew Thoelke  * initialised once on the primary core after a cold boot.
58375f538aSAchin Gupta  ******************************************************************************/
59399fb08fSAndrew Thoelke tsp_vectors_t *tsp_vectors;
60375f538aSAchin Gupta 
61375f538aSAchin Gupta /*******************************************************************************
62375f538aSAchin Gupta  * Array to keep track of per-cpu Secure Payload state
63375f538aSAchin Gupta  ******************************************************************************/
64fb037bfbSDan Handley tsp_context_t tspd_sp_context[TSPD_CORE_COUNT];
65375f538aSAchin Gupta 
667f366605SJeenu Viswambharan 
6752538b9bSJeenu Viswambharan /* TSP UID */
6852538b9bSJeenu Viswambharan DEFINE_SVC_UUID(tsp_uuid,
6952538b9bSJeenu Viswambharan 		0x5b3056a0, 0x3291, 0x427b, 0x98, 0x11,
7052538b9bSJeenu Viswambharan 		0x71, 0x68, 0xca, 0x50, 0xf3, 0xfa);
7152538b9bSJeenu Viswambharan 
726871c5d3SVikram Kanigiri int32_t tspd_init(void);
737f366605SJeenu Viswambharan 
74b44a4435SAchin Gupta /*******************************************************************************
75b44a4435SAchin Gupta  * This function is the handler registered for S-EL1 interrupts by the TSPD. It
76b44a4435SAchin Gupta  * validates the interrupt and upon success arranges entry into the TSP at
77b44a4435SAchin Gupta  * 'tsp_fiq_entry()' for handling the interrupt.
78b44a4435SAchin Gupta  ******************************************************************************/
79b44a4435SAchin Gupta static uint64_t tspd_sel1_interrupt_handler(uint32_t id,
80b44a4435SAchin Gupta 					    uint32_t flags,
81b44a4435SAchin Gupta 					    void *handle,
82b44a4435SAchin Gupta 					    void *cookie)
83b44a4435SAchin Gupta {
84b44a4435SAchin Gupta 	uint32_t linear_id;
85b44a4435SAchin Gupta 	uint64_t mpidr;
86b44a4435SAchin Gupta 	tsp_context_t *tsp_ctx;
87b44a4435SAchin Gupta 
88b44a4435SAchin Gupta 	/* Check the security state when the exception was generated */
89b44a4435SAchin Gupta 	assert(get_interrupt_src_ss(flags) == NON_SECURE);
90b44a4435SAchin Gupta 
91b44a4435SAchin Gupta #if IMF_READ_INTERRUPT_ID
92b44a4435SAchin Gupta 	/* Check the security status of the interrupt */
93*a3781085SSoby Mathew 	assert(plat_ic_get_interrupt_type(id) == INTR_TYPE_S_EL1);
94b44a4435SAchin Gupta #endif
95b44a4435SAchin Gupta 
96b44a4435SAchin Gupta 	/* Sanity check the pointer to this cpu's context */
97b44a4435SAchin Gupta 	mpidr = read_mpidr();
98b44a4435SAchin Gupta 	assert(handle == cm_get_context(mpidr, NON_SECURE));
99b44a4435SAchin Gupta 
100b44a4435SAchin Gupta 	/* Save the non-secure context before entering the TSP */
101b44a4435SAchin Gupta 	cm_el1_sysregs_context_save(NON_SECURE);
102b44a4435SAchin Gupta 
103b44a4435SAchin Gupta 	/* Get a reference to this cpu's TSP context */
104b44a4435SAchin Gupta 	linear_id = platform_get_core_pos(mpidr);
105b44a4435SAchin Gupta 	tsp_ctx = &tspd_sp_context[linear_id];
106b44a4435SAchin Gupta 	assert(&tsp_ctx->cpu_ctx == cm_get_context(mpidr, SECURE));
107b44a4435SAchin Gupta 
108b44a4435SAchin Gupta 	/*
109b44a4435SAchin Gupta 	 * Determine if the TSP was previously preempted. Its last known
110b44a4435SAchin Gupta 	 * context has to be preserved in this case.
111b44a4435SAchin Gupta 	 * The TSP should return control to the TSPD after handling this
112b44a4435SAchin Gupta 	 * FIQ. Preserve essential EL3 context to allow entry into the
113b44a4435SAchin Gupta 	 * TSP at the FIQ entry point using the 'cpu_context' structure.
114b44a4435SAchin Gupta 	 * There is no need to save the secure system register context
115b44a4435SAchin Gupta 	 * since the TSP is supposed to preserve it during S-EL1 interrupt
116b44a4435SAchin Gupta 	 * handling.
117b44a4435SAchin Gupta 	 */
118b44a4435SAchin Gupta 	if (get_std_smc_active_flag(tsp_ctx->state)) {
119b44a4435SAchin Gupta 		tsp_ctx->saved_spsr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx,
120b44a4435SAchin Gupta 						      CTX_SPSR_EL3);
121b44a4435SAchin Gupta 		tsp_ctx->saved_elr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx,
122b44a4435SAchin Gupta 						     CTX_ELR_EL3);
123b44a4435SAchin Gupta 	}
124b44a4435SAchin Gupta 
125b44a4435SAchin Gupta 	SMC_SET_EL3(&tsp_ctx->cpu_ctx,
126b44a4435SAchin Gupta 		    CTX_SPSR_EL3,
127b44a4435SAchin Gupta 		    SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS));
128b44a4435SAchin Gupta 	SMC_SET_EL3(&tsp_ctx->cpu_ctx,
129b44a4435SAchin Gupta 		    CTX_ELR_EL3,
130399fb08fSAndrew Thoelke 		    (uint64_t) &tsp_vectors->fiq_entry);
131b44a4435SAchin Gupta 	cm_el1_sysregs_context_restore(SECURE);
132b44a4435SAchin Gupta 	cm_set_next_eret_context(SECURE);
133b44a4435SAchin Gupta 
134b44a4435SAchin Gupta 	/*
135b44a4435SAchin Gupta 	 * Tell the TSP that it has to handle an FIQ synchronously. Also the
136b44a4435SAchin Gupta 	 * instruction in normal world where the interrupt was generated is
137b44a4435SAchin Gupta 	 * passed for debugging purposes. It is safe to retrieve this address
138b44a4435SAchin Gupta 	 * from ELR_EL3 as the secure context will not take effect until
139b44a4435SAchin Gupta 	 * el3_exit().
140b44a4435SAchin Gupta 	 */
141b44a4435SAchin Gupta 	SMC_RET2(&tsp_ctx->cpu_ctx, TSP_HANDLE_FIQ_AND_RETURN, read_elr_el3());
142b44a4435SAchin Gupta }
1437f366605SJeenu Viswambharan 
144375f538aSAchin Gupta /*******************************************************************************
145375f538aSAchin Gupta  * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type
146375f538aSAchin Gupta  * (aarch32/aarch64) if not already known and initialises the context for entry
147375f538aSAchin Gupta  * into the SP for its initialisation.
148375f538aSAchin Gupta  ******************************************************************************/
149375f538aSAchin Gupta int32_t tspd_setup(void)
150375f538aSAchin Gupta {
1514112bfa0SVikram Kanigiri 	entry_point_info_t *image_info;
152375f538aSAchin Gupta 	int32_t rc;
153375f538aSAchin Gupta 	uint64_t mpidr = read_mpidr();
154375f538aSAchin Gupta 	uint32_t linear_id;
155375f538aSAchin Gupta 
156375f538aSAchin Gupta 	linear_id = platform_get_core_pos(mpidr);
157375f538aSAchin Gupta 
158375f538aSAchin Gupta 	/*
159375f538aSAchin Gupta 	 * Get information about the Secure Payload (BL32) image. Its
160375f538aSAchin Gupta 	 * absence is a critical failure.  TODO: Add support to
161375f538aSAchin Gupta 	 * conditionally include the SPD service
162375f538aSAchin Gupta 	 */
1639865ac15SDan Handley 	image_info = bl31_plat_get_next_image_ep_info(SECURE);
164375f538aSAchin Gupta 	assert(image_info);
165375f538aSAchin Gupta 
166375f538aSAchin Gupta 	/*
1677f366605SJeenu Viswambharan 	 * If there's no valid entry point for SP, we return a non-zero value
1687f366605SJeenu Viswambharan 	 * signalling failure initializing the service. We bail out without
1697f366605SJeenu Viswambharan 	 * registering any handlers
1707f366605SJeenu Viswambharan 	 */
1714112bfa0SVikram Kanigiri 	if (!image_info->pc)
1727f366605SJeenu Viswambharan 		return 1;
1737f366605SJeenu Viswambharan 
1747f366605SJeenu Viswambharan 	/*
175375f538aSAchin Gupta 	 * We could inspect the SP image and determine it's execution
176375f538aSAchin Gupta 	 * state i.e whether AArch32 or AArch64. Assuming it's AArch64
177375f538aSAchin Gupta 	 * for the time being.
178375f538aSAchin Gupta 	 */
1794112bfa0SVikram Kanigiri 	rc = tspd_init_secure_context(image_info->pc,
180375f538aSAchin Gupta 				     TSP_AARCH64,
181375f538aSAchin Gupta 				     mpidr,
182375f538aSAchin Gupta 				     &tspd_sp_context[linear_id]);
183375f538aSAchin Gupta 	assert(rc == 0);
184375f538aSAchin Gupta 
1857f366605SJeenu Viswambharan 	/*
1867f366605SJeenu Viswambharan 	 * All TSPD initialization done. Now register our init function with
1877f366605SJeenu Viswambharan 	 * BL31 for deferred invocation
1887f366605SJeenu Viswambharan 	 */
1897f366605SJeenu Viswambharan 	bl31_register_bl32_init(&tspd_init);
1907f366605SJeenu Viswambharan 
191375f538aSAchin Gupta 	return rc;
192375f538aSAchin Gupta }
193375f538aSAchin Gupta 
194375f538aSAchin Gupta /*******************************************************************************
195375f538aSAchin Gupta  * This function passes control to the Secure Payload image (BL32) for the first
196375f538aSAchin Gupta  * time on the primary cpu after a cold boot. It assumes that a valid secure
197375f538aSAchin Gupta  * context has already been created by tspd_setup() which can be directly used.
198375f538aSAchin Gupta  * It also assumes that a valid non-secure context has been initialised by PSCI
199375f538aSAchin Gupta  * so it does not need to save and restore any non-secure state. This function
200375f538aSAchin Gupta  * performs a synchronous entry into the Secure payload. The SP passes control
2016871c5d3SVikram Kanigiri  * back to this routine through a SMC.
202375f538aSAchin Gupta  ******************************************************************************/
2036871c5d3SVikram Kanigiri int32_t tspd_init(void)
204375f538aSAchin Gupta {
205375f538aSAchin Gupta 	uint64_t mpidr = read_mpidr();
206b44a4435SAchin Gupta 	uint32_t linear_id = platform_get_core_pos(mpidr), flags;
207375f538aSAchin Gupta 	uint64_t rc;
208fb037bfbSDan Handley 	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
209375f538aSAchin Gupta 
210375f538aSAchin Gupta 	/*
211607084eeSAchin Gupta 	 * Arrange for an entry into the test secure payload. We expect an array
212607084eeSAchin Gupta 	 * of vectors in return
213607084eeSAchin Gupta 	 */
214375f538aSAchin Gupta 	rc = tspd_synchronous_sp_entry(tsp_ctx);
215375f538aSAchin Gupta 	assert(rc != 0);
2167f366605SJeenu Viswambharan 	if (rc) {
2173ee8a164SAchin Gupta 		set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON);
218375f538aSAchin Gupta 
2197f366605SJeenu Viswambharan 		/*
2207f366605SJeenu Viswambharan 		 * TSP has been successfully initialized. Register power
2217f366605SJeenu Viswambharan 		 * managemnt hooks with PSCI
2227f366605SJeenu Viswambharan 		 */
2237f366605SJeenu Viswambharan 		psci_register_spd_pm_hook(&tspd_pm);
2247f366605SJeenu Viswambharan 	}
2257f366605SJeenu Viswambharan 
226b44a4435SAchin Gupta 	/*
227b44a4435SAchin Gupta 	 * Register an interrupt handler for S-EL1 interrupts when generated
228b44a4435SAchin Gupta 	 * during code executing in the non-secure state.
229b44a4435SAchin Gupta 	 */
230b44a4435SAchin Gupta 	flags = 0;
231b44a4435SAchin Gupta 	set_interrupt_rm_flag(flags, NON_SECURE);
232b44a4435SAchin Gupta 	rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
233b44a4435SAchin Gupta 					     tspd_sel1_interrupt_handler,
234b44a4435SAchin Gupta 					     flags);
235b44a4435SAchin Gupta 	if (rc)
236b44a4435SAchin Gupta 		panic();
237b44a4435SAchin Gupta 
238375f538aSAchin Gupta 	return rc;
239375f538aSAchin Gupta }
240375f538aSAchin Gupta 
2417f366605SJeenu Viswambharan 
242375f538aSAchin Gupta /*******************************************************************************
243375f538aSAchin Gupta  * This function is responsible for handling all SMCs in the Trusted OS/App
244375f538aSAchin Gupta  * range from the non-secure state as defined in the SMC Calling Convention
245375f538aSAchin Gupta  * Document. It is also responsible for communicating with the Secure payload
246375f538aSAchin Gupta  * to delegate work and return results back to the non-secure state. Lastly it
247375f538aSAchin Gupta  * will also return any information that the secure payload needs to do the
248375f538aSAchin Gupta  * work assigned to it.
249375f538aSAchin Gupta  ******************************************************************************/
250375f538aSAchin Gupta uint64_t tspd_smc_handler(uint32_t smc_fid,
251375f538aSAchin Gupta 			 uint64_t x1,
252375f538aSAchin Gupta 			 uint64_t x2,
253375f538aSAchin Gupta 			 uint64_t x3,
254375f538aSAchin Gupta 			 uint64_t x4,
255375f538aSAchin Gupta 			 void *cookie,
256375f538aSAchin Gupta 			 void *handle,
257375f538aSAchin Gupta 			 uint64_t flags)
258375f538aSAchin Gupta {
259fb037bfbSDan Handley 	cpu_context_t *ns_cpu_context;
260375f538aSAchin Gupta 	unsigned long mpidr = read_mpidr();
261375f538aSAchin Gupta 	uint32_t linear_id = platform_get_core_pos(mpidr), ns;
262fb037bfbSDan Handley 	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
263375f538aSAchin Gupta 
264375f538aSAchin Gupta 	/* Determine which security state this SMC originated from */
265375f538aSAchin Gupta 	ns = is_caller_non_secure(flags);
266375f538aSAchin Gupta 
267375f538aSAchin Gupta 	switch (smc_fid) {
268375f538aSAchin Gupta 
269375f538aSAchin Gupta 	/*
270239b04faSSoby Mathew 	 * This function ID is used by TSP to indicate that it was
271239b04faSSoby Mathew 	 * preempted by a normal world IRQ.
272239b04faSSoby Mathew 	 *
273239b04faSSoby Mathew 	 */
274239b04faSSoby Mathew 	case TSP_PREEMPTED:
275239b04faSSoby Mathew 		if (ns)
276239b04faSSoby Mathew 			SMC_RET1(handle, SMC_UNK);
277239b04faSSoby Mathew 
278239b04faSSoby Mathew 		assert(handle == cm_get_context(mpidr, SECURE));
279239b04faSSoby Mathew 		cm_el1_sysregs_context_save(SECURE);
280239b04faSSoby Mathew 		/* Get a reference to the non-secure context */
281239b04faSSoby Mathew 		ns_cpu_context = cm_get_context(mpidr, NON_SECURE);
282239b04faSSoby Mathew 		assert(ns_cpu_context);
283239b04faSSoby Mathew 
284239b04faSSoby Mathew 		/*
285239b04faSSoby Mathew 		 * Restore non-secure state. There is no need to save the
286239b04faSSoby Mathew 		 * secure system register context since the TSP was supposed
287239b04faSSoby Mathew 		 * to preserve it during S-EL1 interrupt handling.
288239b04faSSoby Mathew 		 */
289239b04faSSoby Mathew 		cm_el1_sysregs_context_restore(NON_SECURE);
290239b04faSSoby Mathew 		cm_set_next_eret_context(NON_SECURE);
291239b04faSSoby Mathew 
292239b04faSSoby Mathew 		SMC_RET1(ns_cpu_context, SMC_PREEMPTED);
293239b04faSSoby Mathew 
294239b04faSSoby Mathew 	/*
295b44a4435SAchin Gupta 	 * This function ID is used only by the TSP to indicate that it has
296b44a4435SAchin Gupta 	 * finished handling a S-EL1 FIQ interrupt. Execution should resume
297b44a4435SAchin Gupta 	 * in the normal world.
298b44a4435SAchin Gupta 	 */
299b44a4435SAchin Gupta 	case TSP_HANDLED_S_EL1_FIQ:
300b44a4435SAchin Gupta 		if (ns)
301b44a4435SAchin Gupta 			SMC_RET1(handle, SMC_UNK);
302b44a4435SAchin Gupta 
303b44a4435SAchin Gupta 		assert(handle == cm_get_context(mpidr, SECURE));
304b44a4435SAchin Gupta 
305b44a4435SAchin Gupta 		/*
306b44a4435SAchin Gupta 		 * Restore the relevant EL3 state which saved to service
307b44a4435SAchin Gupta 		 * this SMC.
308b44a4435SAchin Gupta 		 */
309b44a4435SAchin Gupta 		if (get_std_smc_active_flag(tsp_ctx->state)) {
310b44a4435SAchin Gupta 			SMC_SET_EL3(&tsp_ctx->cpu_ctx,
311b44a4435SAchin Gupta 				    CTX_SPSR_EL3,
312b44a4435SAchin Gupta 				    tsp_ctx->saved_spsr_el3);
313b44a4435SAchin Gupta 			SMC_SET_EL3(&tsp_ctx->cpu_ctx,
314b44a4435SAchin Gupta 				    CTX_ELR_EL3,
315b44a4435SAchin Gupta 				    tsp_ctx->saved_elr_el3);
316b44a4435SAchin Gupta 		}
317b44a4435SAchin Gupta 
318b44a4435SAchin Gupta 		/* Get a reference to the non-secure context */
319b44a4435SAchin Gupta 		ns_cpu_context = cm_get_context(mpidr, NON_SECURE);
320b44a4435SAchin Gupta 		assert(ns_cpu_context);
321b44a4435SAchin Gupta 
322b44a4435SAchin Gupta 		/*
323b44a4435SAchin Gupta 		 * Restore non-secure state. There is no need to save the
324b44a4435SAchin Gupta 		 * secure system register context since the TSP was supposed
325b44a4435SAchin Gupta 		 * to preserve it during S-EL1 interrupt handling.
326b44a4435SAchin Gupta 		 */
327b44a4435SAchin Gupta 		cm_el1_sysregs_context_restore(NON_SECURE);
328b44a4435SAchin Gupta 		cm_set_next_eret_context(NON_SECURE);
329b44a4435SAchin Gupta 
330b44a4435SAchin Gupta 		SMC_RET0((uint64_t) ns_cpu_context);
331b44a4435SAchin Gupta 
332b44a4435SAchin Gupta 
333b44a4435SAchin Gupta 	/*
334b44a4435SAchin Gupta 	 * This function ID is used only by the TSP to indicate that it was
335b44a4435SAchin Gupta 	 * interrupted due to a EL3 FIQ interrupt. Execution should resume
336b44a4435SAchin Gupta 	 * in the normal world.
337b44a4435SAchin Gupta 	 */
338b44a4435SAchin Gupta 	case TSP_EL3_FIQ:
339b44a4435SAchin Gupta 		if (ns)
340b44a4435SAchin Gupta 			SMC_RET1(handle, SMC_UNK);
341b44a4435SAchin Gupta 
342b44a4435SAchin Gupta 		assert(handle == cm_get_context(mpidr, SECURE));
343b44a4435SAchin Gupta 
344b44a4435SAchin Gupta 		/* Assert that standard SMC execution has been preempted */
345b44a4435SAchin Gupta 		assert(get_std_smc_active_flag(tsp_ctx->state));
346b44a4435SAchin Gupta 
347b44a4435SAchin Gupta 		/* Save the secure system register state */
348b44a4435SAchin Gupta 		cm_el1_sysregs_context_save(SECURE);
349b44a4435SAchin Gupta 
350b44a4435SAchin Gupta 		/* Get a reference to the non-secure context */
351b44a4435SAchin Gupta 		ns_cpu_context = cm_get_context(mpidr, NON_SECURE);
352b44a4435SAchin Gupta 		assert(ns_cpu_context);
353b44a4435SAchin Gupta 
354b44a4435SAchin Gupta 		/* Restore non-secure state */
355b44a4435SAchin Gupta 		cm_el1_sysregs_context_restore(NON_SECURE);
356b44a4435SAchin Gupta 		cm_set_next_eret_context(NON_SECURE);
357b44a4435SAchin Gupta 
358b44a4435SAchin Gupta 		SMC_RET1(ns_cpu_context, TSP_EL3_FIQ);
359b44a4435SAchin Gupta 
360b44a4435SAchin Gupta 
361b44a4435SAchin Gupta 	/*
362375f538aSAchin Gupta 	 * This function ID is used only by the SP to indicate it has
363375f538aSAchin Gupta 	 * finished initialising itself after a cold boot
364375f538aSAchin Gupta 	 */
365375f538aSAchin Gupta 	case TSP_ENTRY_DONE:
366375f538aSAchin Gupta 		if (ns)
367375f538aSAchin Gupta 			SMC_RET1(handle, SMC_UNK);
368375f538aSAchin Gupta 
369375f538aSAchin Gupta 		/*
370375f538aSAchin Gupta 		 * Stash the SP entry points information. This is done
371375f538aSAchin Gupta 		 * only once on the primary cpu
372375f538aSAchin Gupta 		 */
373399fb08fSAndrew Thoelke 		assert(tsp_vectors == NULL);
374399fb08fSAndrew Thoelke 		tsp_vectors = (tsp_vectors_t *) x1;
375375f538aSAchin Gupta 
376375f538aSAchin Gupta 		/*
377375f538aSAchin Gupta 		 * SP reports completion. The SPD must have initiated
378375f538aSAchin Gupta 		 * the original request through a synchronous entry
379375f538aSAchin Gupta 		 * into the SP. Jump back to the original C runtime
380375f538aSAchin Gupta 		 * context.
381375f538aSAchin Gupta 		 */
382916a2c1eSAchin Gupta 		tspd_synchronous_sp_exit(tsp_ctx, x1);
383375f538aSAchin Gupta 
384607084eeSAchin Gupta 	/*
385607084eeSAchin Gupta 	 * These function IDs is used only by the SP to indicate it has
386607084eeSAchin Gupta 	 * finished:
387607084eeSAchin Gupta 	 * 1. turning itself on in response to an earlier psci
388607084eeSAchin Gupta 	 *    cpu_on request
389607084eeSAchin Gupta 	 * 2. resuming itself after an earlier psci cpu_suspend
390607084eeSAchin Gupta 	 *    request.
391607084eeSAchin Gupta 	 */
392607084eeSAchin Gupta 	case TSP_ON_DONE:
393607084eeSAchin Gupta 	case TSP_RESUME_DONE:
394607084eeSAchin Gupta 
395607084eeSAchin Gupta 	/*
396607084eeSAchin Gupta 	 * These function IDs is used only by the SP to indicate it has
397607084eeSAchin Gupta 	 * finished:
398607084eeSAchin Gupta 	 * 1. suspending itself after an earlier psci cpu_suspend
399607084eeSAchin Gupta 	 *    request.
400607084eeSAchin Gupta 	 * 2. turning itself off in response to an earlier psci
401607084eeSAchin Gupta 	 *    cpu_off request.
402607084eeSAchin Gupta 	 */
403607084eeSAchin Gupta 	case TSP_OFF_DONE:
404607084eeSAchin Gupta 	case TSP_SUSPEND_DONE:
405607084eeSAchin Gupta 		if (ns)
406607084eeSAchin Gupta 			SMC_RET1(handle, SMC_UNK);
407607084eeSAchin Gupta 
408607084eeSAchin Gupta 		/*
409607084eeSAchin Gupta 		 * SP reports completion. The SPD must have initiated the
410607084eeSAchin Gupta 		 * original request through a synchronous entry into the SP.
411607084eeSAchin Gupta 		 * Jump back to the original C runtime context, and pass x1 as
412607084eeSAchin Gupta 		 * return value to the caller
413607084eeSAchin Gupta 		 */
414916a2c1eSAchin Gupta 		tspd_synchronous_sp_exit(tsp_ctx, x1);
415607084eeSAchin Gupta 
416916a2c1eSAchin Gupta 		/*
417916a2c1eSAchin Gupta 		 * Request from non-secure client to perform an
418916a2c1eSAchin Gupta 		 * arithmetic operation or response from secure
419916a2c1eSAchin Gupta 		 * payload to an earlier request.
420916a2c1eSAchin Gupta 		 */
421239b04faSSoby Mathew 	case TSP_FAST_FID(TSP_ADD):
422239b04faSSoby Mathew 	case TSP_FAST_FID(TSP_SUB):
423239b04faSSoby Mathew 	case TSP_FAST_FID(TSP_MUL):
424239b04faSSoby Mathew 	case TSP_FAST_FID(TSP_DIV):
425239b04faSSoby Mathew 
426239b04faSSoby Mathew 	case TSP_STD_FID(TSP_ADD):
427239b04faSSoby Mathew 	case TSP_STD_FID(TSP_SUB):
428239b04faSSoby Mathew 	case TSP_STD_FID(TSP_MUL):
429239b04faSSoby Mathew 	case TSP_STD_FID(TSP_DIV):
430916a2c1eSAchin Gupta 		if (ns) {
431916a2c1eSAchin Gupta 			/*
432916a2c1eSAchin Gupta 			 * This is a fresh request from the non-secure client.
433916a2c1eSAchin Gupta 			 * The parameters are in x1 and x2. Figure out which
434916a2c1eSAchin Gupta 			 * registers need to be preserved, save the non-secure
435916a2c1eSAchin Gupta 			 * state and send the request to the secure payload.
436916a2c1eSAchin Gupta 			 */
437916a2c1eSAchin Gupta 			assert(handle == cm_get_context(mpidr, NON_SECURE));
438239b04faSSoby Mathew 
439239b04faSSoby Mathew 			/* Check if we are already preempted */
440239b04faSSoby Mathew 			if (get_std_smc_active_flag(tsp_ctx->state))
441239b04faSSoby Mathew 				SMC_RET1(handle, SMC_UNK);
442239b04faSSoby Mathew 
443916a2c1eSAchin Gupta 			cm_el1_sysregs_context_save(NON_SECURE);
444916a2c1eSAchin Gupta 
445916a2c1eSAchin Gupta 			/* Save x1 and x2 for use by TSP_GET_ARGS call below */
446239b04faSSoby Mathew 			store_tsp_args(tsp_ctx, x1, x2);
447916a2c1eSAchin Gupta 
448916a2c1eSAchin Gupta 			/*
449916a2c1eSAchin Gupta 			 * We are done stashing the non-secure context. Ask the
450916a2c1eSAchin Gupta 			 * secure payload to do the work now.
451916a2c1eSAchin Gupta 			 */
452916a2c1eSAchin Gupta 
453916a2c1eSAchin Gupta 			/*
454916a2c1eSAchin Gupta 			 * Verify if there is a valid context to use, copy the
455916a2c1eSAchin Gupta 			 * operation type and parameters to the secure context
456916a2c1eSAchin Gupta 			 * and jump to the fast smc entry point in the secure
457916a2c1eSAchin Gupta 			 * payload. Entry into S-EL1 will take place upon exit
458916a2c1eSAchin Gupta 			 * from this function.
459916a2c1eSAchin Gupta 			 */
460916a2c1eSAchin Gupta 			assert(&tsp_ctx->cpu_ctx == cm_get_context(mpidr, SECURE));
461239b04faSSoby Mathew 
462239b04faSSoby Mathew 			/* Set appropriate entry for SMC.
463239b04faSSoby Mathew 			 * We expect the TSP to manage the PSTATE.I and PSTATE.F
464239b04faSSoby Mathew 			 * flags as appropriate.
465239b04faSSoby Mathew 			 */
466239b04faSSoby Mathew 			if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_FAST) {
467239b04faSSoby Mathew 				cm_set_elr_el3(SECURE, (uint64_t)
468399fb08fSAndrew Thoelke 						&tsp_vectors->fast_smc_entry);
469239b04faSSoby Mathew 			} else {
470239b04faSSoby Mathew 				set_std_smc_active_flag(tsp_ctx->state);
471239b04faSSoby Mathew 				cm_set_elr_el3(SECURE, (uint64_t)
472399fb08fSAndrew Thoelke 						&tsp_vectors->std_smc_entry);
473239b04faSSoby Mathew 			}
474239b04faSSoby Mathew 
475916a2c1eSAchin Gupta 			cm_el1_sysregs_context_restore(SECURE);
476916a2c1eSAchin Gupta 			cm_set_next_eret_context(SECURE);
477239b04faSSoby Mathew 			SMC_RET3(&tsp_ctx->cpu_ctx, smc_fid, x1, x2);
478916a2c1eSAchin Gupta 		} else {
479916a2c1eSAchin Gupta 			/*
480916a2c1eSAchin Gupta 			 * This is the result from the secure client of an
481239b04faSSoby Mathew 			 * earlier request. The results are in x1-x3. Copy it
482916a2c1eSAchin Gupta 			 * into the non-secure context, save the secure state
483916a2c1eSAchin Gupta 			 * and return to the non-secure state.
484916a2c1eSAchin Gupta 			 */
485916a2c1eSAchin Gupta 			assert(handle == cm_get_context(mpidr, SECURE));
486916a2c1eSAchin Gupta 			cm_el1_sysregs_context_save(SECURE);
487916a2c1eSAchin Gupta 
488916a2c1eSAchin Gupta 			/* Get a reference to the non-secure context */
489916a2c1eSAchin Gupta 			ns_cpu_context = cm_get_context(mpidr, NON_SECURE);
490916a2c1eSAchin Gupta 			assert(ns_cpu_context);
491916a2c1eSAchin Gupta 
492916a2c1eSAchin Gupta 			/* Restore non-secure state */
493916a2c1eSAchin Gupta 			cm_el1_sysregs_context_restore(NON_SECURE);
494916a2c1eSAchin Gupta 			cm_set_next_eret_context(NON_SECURE);
495239b04faSSoby Mathew 			if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_STD)
496239b04faSSoby Mathew 				clr_std_smc_active_flag(tsp_ctx->state);
497239b04faSSoby Mathew 			SMC_RET3(ns_cpu_context, x1, x2, x3);
498916a2c1eSAchin Gupta 		}
499916a2c1eSAchin Gupta 
500916a2c1eSAchin Gupta 		break;
501916a2c1eSAchin Gupta 
502916a2c1eSAchin Gupta 		/*
503239b04faSSoby Mathew 		 * Request from non secure world to resume the preempted
504239b04faSSoby Mathew 		 * Standard SMC call.
505239b04faSSoby Mathew 		 */
506239b04faSSoby Mathew 	case TSP_FID_RESUME:
507239b04faSSoby Mathew 		/* RESUME should be invoked only by normal world */
508239b04faSSoby Mathew 		if (!ns) {
509239b04faSSoby Mathew 			assert(0);
510239b04faSSoby Mathew 			break;
511239b04faSSoby Mathew 		}
512239b04faSSoby Mathew 
513239b04faSSoby Mathew 		/*
514239b04faSSoby Mathew 		 * This is a resume request from the non-secure client.
515239b04faSSoby Mathew 		 * save the non-secure state and send the request to
516239b04faSSoby Mathew 		 * the secure payload.
517239b04faSSoby Mathew 		 */
518239b04faSSoby Mathew 		assert(handle == cm_get_context(mpidr, NON_SECURE));
519239b04faSSoby Mathew 
520239b04faSSoby Mathew 		/* Check if we are already preempted before resume */
521239b04faSSoby Mathew 		if (!get_std_smc_active_flag(tsp_ctx->state))
522239b04faSSoby Mathew 			SMC_RET1(handle, SMC_UNK);
523239b04faSSoby Mathew 
524239b04faSSoby Mathew 		cm_el1_sysregs_context_save(NON_SECURE);
525239b04faSSoby Mathew 
526239b04faSSoby Mathew 		/*
527239b04faSSoby Mathew 		 * We are done stashing the non-secure context. Ask the
528239b04faSSoby Mathew 		 * secure payload to do the work now.
529239b04faSSoby Mathew 		 */
530239b04faSSoby Mathew 
531239b04faSSoby Mathew 		/* We just need to return to the preempted point in
532239b04faSSoby Mathew 		 * TSP and the execution will resume as normal.
533239b04faSSoby Mathew 		 */
534239b04faSSoby Mathew 		cm_el1_sysregs_context_restore(SECURE);
535239b04faSSoby Mathew 		cm_set_next_eret_context(SECURE);
53610b65ecfSSoby Mathew 		SMC_RET0(&tsp_ctx->cpu_ctx);
537239b04faSSoby Mathew 
538239b04faSSoby Mathew 		/*
539916a2c1eSAchin Gupta 		 * This is a request from the secure payload for more arguments
540916a2c1eSAchin Gupta 		 * for an ongoing arithmetic operation requested by the
541916a2c1eSAchin Gupta 		 * non-secure world. Simply return the arguments from the non-
542916a2c1eSAchin Gupta 		 * secure client in the original call.
543916a2c1eSAchin Gupta 		 */
544916a2c1eSAchin Gupta 	case TSP_GET_ARGS:
545916a2c1eSAchin Gupta 		if (ns)
546916a2c1eSAchin Gupta 			SMC_RET1(handle, SMC_UNK);
547916a2c1eSAchin Gupta 
548239b04faSSoby Mathew 		get_tsp_args(tsp_ctx, x1, x2);
549239b04faSSoby Mathew 		SMC_RET2(handle, x1, x2);
550916a2c1eSAchin Gupta 
55152538b9bSJeenu Viswambharan 	case TOS_CALL_COUNT:
55252538b9bSJeenu Viswambharan 		/*
55352538b9bSJeenu Viswambharan 		 * Return the number of service function IDs implemented to
55452538b9bSJeenu Viswambharan 		 * provide service to non-secure
55552538b9bSJeenu Viswambharan 		 */
55652538b9bSJeenu Viswambharan 		SMC_RET1(handle, TSP_NUM_FID);
55752538b9bSJeenu Viswambharan 
55852538b9bSJeenu Viswambharan 	case TOS_UID:
55952538b9bSJeenu Viswambharan 		/* Return TSP UID to the caller */
56052538b9bSJeenu Viswambharan 		SMC_UUID_RET(handle, tsp_uuid);
56152538b9bSJeenu Viswambharan 
56252538b9bSJeenu Viswambharan 	case TOS_CALL_VERSION:
56352538b9bSJeenu Viswambharan 		/* Return the version of current implementation */
56452538b9bSJeenu Viswambharan 		SMC_RET2(handle, TSP_VERSION_MAJOR, TSP_VERSION_MINOR);
56552538b9bSJeenu Viswambharan 
566375f538aSAchin Gupta 	default:
567607084eeSAchin Gupta 		break;
568375f538aSAchin Gupta 	}
569375f538aSAchin Gupta 
570607084eeSAchin Gupta 	SMC_RET1(handle, SMC_UNK);
571375f538aSAchin Gupta }
572375f538aSAchin Gupta 
573239b04faSSoby Mathew /* Define a SPD runtime service descriptor for fast SMC calls */
574375f538aSAchin Gupta DECLARE_RT_SVC(
575239b04faSSoby Mathew 	tspd_fast,
576375f538aSAchin Gupta 
577375f538aSAchin Gupta 	OEN_TOS_START,
578375f538aSAchin Gupta 	OEN_TOS_END,
579375f538aSAchin Gupta 	SMC_TYPE_FAST,
580375f538aSAchin Gupta 	tspd_setup,
581375f538aSAchin Gupta 	tspd_smc_handler
582375f538aSAchin Gupta );
583239b04faSSoby Mathew 
584239b04faSSoby Mathew /* Define a SPD runtime service descriptor for standard SMC calls */
585239b04faSSoby Mathew DECLARE_RT_SVC(
586239b04faSSoby Mathew 	tspd_std,
587239b04faSSoby Mathew 
588239b04faSSoby Mathew 	OEN_TOS_START,
589239b04faSSoby Mathew 	OEN_TOS_END,
590239b04faSSoby Mathew 	SMC_TYPE_STD,
591239b04faSSoby Mathew 	NULL,
592239b04faSSoby Mathew 	tspd_smc_handler
593239b04faSSoby Mathew );
594