xref: /rk3399_ARM-atf/services/spd/tspd/tspd_main.c (revision d5f130930624ceb95cde40de999a880aa2b00493)
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 */
93a3781085SSoby 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();
9808ab89d3SAndrew Thoelke 	assert(handle == cm_get_context(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];
10608ab89d3SAndrew Thoelke 	assert(&tsp_ctx->cpu_ctx == cm_get_context(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 	cm_el1_sysregs_context_restore(SECURE);
126167a9357SAndrew Thoelke 	cm_set_elr_spsr_el3(SECURE, (uint64_t) &tsp_vectors->fiq_entry,
127167a9357SAndrew Thoelke 		    SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS));
128b44a4435SAchin Gupta 	cm_set_next_eret_context(SECURE);
129b44a4435SAchin Gupta 
130b44a4435SAchin Gupta 	/*
131b44a4435SAchin Gupta 	 * Tell the TSP that it has to handle an FIQ synchronously. Also the
132b44a4435SAchin Gupta 	 * instruction in normal world where the interrupt was generated is
133b44a4435SAchin Gupta 	 * passed for debugging purposes. It is safe to retrieve this address
134b44a4435SAchin Gupta 	 * from ELR_EL3 as the secure context will not take effect until
135b44a4435SAchin Gupta 	 * el3_exit().
136b44a4435SAchin Gupta 	 */
137b44a4435SAchin Gupta 	SMC_RET2(&tsp_ctx->cpu_ctx, TSP_HANDLE_FIQ_AND_RETURN, read_elr_el3());
138b44a4435SAchin Gupta }
1397f366605SJeenu Viswambharan 
140375f538aSAchin Gupta /*******************************************************************************
141375f538aSAchin Gupta  * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type
142375f538aSAchin Gupta  * (aarch32/aarch64) if not already known and initialises the context for entry
143375f538aSAchin Gupta  * into the SP for its initialisation.
144375f538aSAchin Gupta  ******************************************************************************/
145375f538aSAchin Gupta int32_t tspd_setup(void)
146375f538aSAchin Gupta {
14750e27dadSVikram Kanigiri 	entry_point_info_t *tsp_ep_info;
148375f538aSAchin Gupta 	uint64_t mpidr = read_mpidr();
149375f538aSAchin Gupta 	uint32_t linear_id;
150375f538aSAchin Gupta 
151375f538aSAchin Gupta 	linear_id = platform_get_core_pos(mpidr);
152375f538aSAchin Gupta 
153375f538aSAchin Gupta 	/*
154375f538aSAchin Gupta 	 * Get information about the Secure Payload (BL32) image. Its
155375f538aSAchin Gupta 	 * absence is a critical failure.  TODO: Add support to
156375f538aSAchin Gupta 	 * conditionally include the SPD service
157375f538aSAchin Gupta 	 */
15850e27dadSVikram Kanigiri 	tsp_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
15950e27dadSVikram Kanigiri 	if (!tsp_ep_info) {
16050e27dadSVikram Kanigiri 		WARN("No TSP provided by BL2 boot loader, Booting device"
16150e27dadSVikram Kanigiri 			" without TSP initialization. SMC`s destined for TSP"
16250e27dadSVikram Kanigiri 			" will return SMC_UNK\n");
16350e27dadSVikram Kanigiri 		return 1;
16450e27dadSVikram Kanigiri 	}
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 	 */
17150e27dadSVikram Kanigiri 	if (!tsp_ep_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 	 */
17950e27dadSVikram Kanigiri 	tspd_init_tsp_ep_state(tsp_ep_info,
180375f538aSAchin Gupta 				TSP_AARCH64,
18150e27dadSVikram Kanigiri 				tsp_ep_info->pc,
182375f538aSAchin Gupta 				&tspd_sp_context[linear_id]);
183375f538aSAchin Gupta 
184faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC
185faaa2e76SVikram Kanigiri 	bl31_set_next_image_type(SECURE);
186faaa2e76SVikram Kanigiri #else
1877f366605SJeenu Viswambharan 	/*
1887f366605SJeenu Viswambharan 	 * All TSPD initialization done. Now register our init function with
1897f366605SJeenu Viswambharan 	 * BL31 for deferred invocation
1907f366605SJeenu Viswambharan 	 */
1917f366605SJeenu Viswambharan 	bl31_register_bl32_init(&tspd_init);
192faaa2e76SVikram Kanigiri #endif
19350e27dadSVikram Kanigiri 	return 0;
194375f538aSAchin Gupta }
195375f538aSAchin Gupta 
196375f538aSAchin Gupta /*******************************************************************************
197375f538aSAchin Gupta  * This function passes control to the Secure Payload image (BL32) for the first
198375f538aSAchin Gupta  * time on the primary cpu after a cold boot. It assumes that a valid secure
199375f538aSAchin Gupta  * context has already been created by tspd_setup() which can be directly used.
200375f538aSAchin Gupta  * It also assumes that a valid non-secure context has been initialised by PSCI
201375f538aSAchin Gupta  * so it does not need to save and restore any non-secure state. This function
202375f538aSAchin Gupta  * performs a synchronous entry into the Secure payload. The SP passes control
2036871c5d3SVikram Kanigiri  * back to this routine through a SMC.
204375f538aSAchin Gupta  ******************************************************************************/
2056871c5d3SVikram Kanigiri int32_t tspd_init(void)
206375f538aSAchin Gupta {
207375f538aSAchin Gupta 	uint64_t mpidr = read_mpidr();
208faaa2e76SVikram Kanigiri 	uint32_t linear_id = platform_get_core_pos(mpidr);
209fb037bfbSDan Handley 	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
21050e27dadSVikram Kanigiri 	entry_point_info_t *tsp_entry_point;
211faaa2e76SVikram Kanigiri 	uint64_t rc;
21250e27dadSVikram Kanigiri 
21350e27dadSVikram Kanigiri 	/*
21450e27dadSVikram Kanigiri 	 * Get information about the Secure Payload (BL32) image. Its
21550e27dadSVikram Kanigiri 	 * absence is a critical failure.
21650e27dadSVikram Kanigiri 	 */
21750e27dadSVikram Kanigiri 	tsp_entry_point = bl31_plat_get_next_image_ep_info(SECURE);
21850e27dadSVikram Kanigiri 	assert(tsp_entry_point);
21950e27dadSVikram Kanigiri 
22050e27dadSVikram Kanigiri 	cm_init_context(mpidr, tsp_entry_point);
221375f538aSAchin Gupta 
222375f538aSAchin Gupta 	/*
223faaa2e76SVikram Kanigiri 	 * Arrange for an entry into the test secure payload. It will be
224faaa2e76SVikram Kanigiri 	 * returned via TSP_ENTRY_DONE case
225607084eeSAchin Gupta 	 */
226375f538aSAchin Gupta 	rc = tspd_synchronous_sp_entry(tsp_ctx);
227375f538aSAchin Gupta 	assert(rc != 0);
228b44a4435SAchin Gupta 
229375f538aSAchin Gupta 	return rc;
230375f538aSAchin Gupta }
231375f538aSAchin Gupta 
2327f366605SJeenu Viswambharan 
233375f538aSAchin Gupta /*******************************************************************************
234375f538aSAchin Gupta  * This function is responsible for handling all SMCs in the Trusted OS/App
235375f538aSAchin Gupta  * range from the non-secure state as defined in the SMC Calling Convention
236375f538aSAchin Gupta  * Document. It is also responsible for communicating with the Secure payload
237375f538aSAchin Gupta  * to delegate work and return results back to the non-secure state. Lastly it
238375f538aSAchin Gupta  * will also return any information that the secure payload needs to do the
239375f538aSAchin Gupta  * work assigned to it.
240375f538aSAchin Gupta  ******************************************************************************/
241375f538aSAchin Gupta uint64_t tspd_smc_handler(uint32_t smc_fid,
242375f538aSAchin Gupta 			 uint64_t x1,
243375f538aSAchin Gupta 			 uint64_t x2,
244375f538aSAchin Gupta 			 uint64_t x3,
245375f538aSAchin Gupta 			 uint64_t x4,
246375f538aSAchin Gupta 			 void *cookie,
247375f538aSAchin Gupta 			 void *handle,
248375f538aSAchin Gupta 			 uint64_t flags)
249375f538aSAchin Gupta {
250fb037bfbSDan Handley 	cpu_context_t *ns_cpu_context;
251375f538aSAchin Gupta 	unsigned long mpidr = read_mpidr();
252375f538aSAchin Gupta 	uint32_t linear_id = platform_get_core_pos(mpidr), ns;
253fb037bfbSDan Handley 	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
254faaa2e76SVikram Kanigiri 	uint64_t rc;
255faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC
256faaa2e76SVikram Kanigiri 	entry_point_info_t *next_image_info;
257faaa2e76SVikram Kanigiri #endif
258375f538aSAchin Gupta 
259375f538aSAchin Gupta 	/* Determine which security state this SMC originated from */
260375f538aSAchin Gupta 	ns = is_caller_non_secure(flags);
261375f538aSAchin Gupta 
262375f538aSAchin Gupta 	switch (smc_fid) {
263375f538aSAchin Gupta 
264375f538aSAchin Gupta 	/*
265239b04faSSoby Mathew 	 * This function ID is used by TSP to indicate that it was
266239b04faSSoby Mathew 	 * preempted by a normal world IRQ.
267239b04faSSoby Mathew 	 *
268239b04faSSoby Mathew 	 */
269239b04faSSoby Mathew 	case TSP_PREEMPTED:
270239b04faSSoby Mathew 		if (ns)
271239b04faSSoby Mathew 			SMC_RET1(handle, SMC_UNK);
272239b04faSSoby Mathew 
27308ab89d3SAndrew Thoelke 		assert(handle == cm_get_context(SECURE));
274239b04faSSoby Mathew 		cm_el1_sysregs_context_save(SECURE);
275239b04faSSoby Mathew 		/* Get a reference to the non-secure context */
27608ab89d3SAndrew Thoelke 		ns_cpu_context = cm_get_context(NON_SECURE);
277239b04faSSoby Mathew 		assert(ns_cpu_context);
278239b04faSSoby Mathew 
279239b04faSSoby Mathew 		/*
280239b04faSSoby Mathew 		 * Restore non-secure state. There is no need to save the
281239b04faSSoby Mathew 		 * secure system register context since the TSP was supposed
282239b04faSSoby Mathew 		 * to preserve it during S-EL1 interrupt handling.
283239b04faSSoby Mathew 		 */
284239b04faSSoby Mathew 		cm_el1_sysregs_context_restore(NON_SECURE);
285239b04faSSoby Mathew 		cm_set_next_eret_context(NON_SECURE);
286239b04faSSoby Mathew 
287239b04faSSoby Mathew 		SMC_RET1(ns_cpu_context, SMC_PREEMPTED);
288239b04faSSoby Mathew 
289239b04faSSoby Mathew 	/*
290b44a4435SAchin Gupta 	 * This function ID is used only by the TSP to indicate that it has
291b44a4435SAchin Gupta 	 * finished handling a S-EL1 FIQ interrupt. Execution should resume
292b44a4435SAchin Gupta 	 * in the normal world.
293b44a4435SAchin Gupta 	 */
294b44a4435SAchin Gupta 	case TSP_HANDLED_S_EL1_FIQ:
295b44a4435SAchin Gupta 		if (ns)
296b44a4435SAchin Gupta 			SMC_RET1(handle, SMC_UNK);
297b44a4435SAchin Gupta 
29808ab89d3SAndrew Thoelke 		assert(handle == cm_get_context(SECURE));
299b44a4435SAchin Gupta 
300b44a4435SAchin Gupta 		/*
301b44a4435SAchin Gupta 		 * Restore the relevant EL3 state which saved to service
302b44a4435SAchin Gupta 		 * this SMC.
303b44a4435SAchin Gupta 		 */
304b44a4435SAchin Gupta 		if (get_std_smc_active_flag(tsp_ctx->state)) {
305b44a4435SAchin Gupta 			SMC_SET_EL3(&tsp_ctx->cpu_ctx,
306b44a4435SAchin Gupta 				    CTX_SPSR_EL3,
307b44a4435SAchin Gupta 				    tsp_ctx->saved_spsr_el3);
308b44a4435SAchin Gupta 			SMC_SET_EL3(&tsp_ctx->cpu_ctx,
309b44a4435SAchin Gupta 				    CTX_ELR_EL3,
310b44a4435SAchin Gupta 				    tsp_ctx->saved_elr_el3);
311b44a4435SAchin Gupta 		}
312b44a4435SAchin Gupta 
313b44a4435SAchin Gupta 		/* Get a reference to the non-secure context */
31408ab89d3SAndrew Thoelke 		ns_cpu_context = cm_get_context(NON_SECURE);
315b44a4435SAchin Gupta 		assert(ns_cpu_context);
316b44a4435SAchin Gupta 
317b44a4435SAchin Gupta 		/*
318b44a4435SAchin Gupta 		 * Restore non-secure state. There is no need to save the
319b44a4435SAchin Gupta 		 * secure system register context since the TSP was supposed
320b44a4435SAchin Gupta 		 * to preserve it during S-EL1 interrupt handling.
321b44a4435SAchin Gupta 		 */
322b44a4435SAchin Gupta 		cm_el1_sysregs_context_restore(NON_SECURE);
323b44a4435SAchin Gupta 		cm_set_next_eret_context(NON_SECURE);
324b44a4435SAchin Gupta 
325b44a4435SAchin Gupta 		SMC_RET0((uint64_t) ns_cpu_context);
326b44a4435SAchin Gupta 
327b44a4435SAchin Gupta 
328b44a4435SAchin Gupta 	/*
329b44a4435SAchin Gupta 	 * This function ID is used only by the TSP to indicate that it was
330b44a4435SAchin Gupta 	 * interrupted due to a EL3 FIQ interrupt. Execution should resume
331b44a4435SAchin Gupta 	 * in the normal world.
332b44a4435SAchin Gupta 	 */
333b44a4435SAchin Gupta 	case TSP_EL3_FIQ:
334b44a4435SAchin Gupta 		if (ns)
335b44a4435SAchin Gupta 			SMC_RET1(handle, SMC_UNK);
336b44a4435SAchin Gupta 
33708ab89d3SAndrew Thoelke 		assert(handle == cm_get_context(SECURE));
338b44a4435SAchin Gupta 
339b44a4435SAchin Gupta 		/* Assert that standard SMC execution has been preempted */
340b44a4435SAchin Gupta 		assert(get_std_smc_active_flag(tsp_ctx->state));
341b44a4435SAchin Gupta 
342b44a4435SAchin Gupta 		/* Save the secure system register state */
343b44a4435SAchin Gupta 		cm_el1_sysregs_context_save(SECURE);
344b44a4435SAchin Gupta 
345b44a4435SAchin Gupta 		/* Get a reference to the non-secure context */
34608ab89d3SAndrew Thoelke 		ns_cpu_context = cm_get_context(NON_SECURE);
347b44a4435SAchin Gupta 		assert(ns_cpu_context);
348b44a4435SAchin Gupta 
349b44a4435SAchin Gupta 		/* Restore non-secure state */
350b44a4435SAchin Gupta 		cm_el1_sysregs_context_restore(NON_SECURE);
351b44a4435SAchin Gupta 		cm_set_next_eret_context(NON_SECURE);
352b44a4435SAchin Gupta 
353b44a4435SAchin Gupta 		SMC_RET1(ns_cpu_context, TSP_EL3_FIQ);
354b44a4435SAchin Gupta 
355b44a4435SAchin Gupta 
356b44a4435SAchin Gupta 	/*
357375f538aSAchin Gupta 	 * This function ID is used only by the SP to indicate it has
358375f538aSAchin Gupta 	 * finished initialising itself after a cold boot
359375f538aSAchin Gupta 	 */
360375f538aSAchin Gupta 	case TSP_ENTRY_DONE:
361375f538aSAchin Gupta 		if (ns)
362375f538aSAchin Gupta 			SMC_RET1(handle, SMC_UNK);
363375f538aSAchin Gupta 
364375f538aSAchin Gupta 		/*
365375f538aSAchin Gupta 		 * Stash the SP entry points information. This is done
366375f538aSAchin Gupta 		 * only once on the primary cpu
367375f538aSAchin Gupta 		 */
368399fb08fSAndrew Thoelke 		assert(tsp_vectors == NULL);
369399fb08fSAndrew Thoelke 		tsp_vectors = (tsp_vectors_t *) x1;
370375f538aSAchin Gupta 
371faaa2e76SVikram Kanigiri 		if (tsp_vectors) {
372faaa2e76SVikram Kanigiri 			set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON);
373faaa2e76SVikram Kanigiri 
374faaa2e76SVikram Kanigiri 			/*
375faaa2e76SVikram Kanigiri 			 * TSP has been successfully initialized. Register power
376faaa2e76SVikram Kanigiri 			 * managemnt hooks with PSCI
377faaa2e76SVikram Kanigiri 			 */
378faaa2e76SVikram Kanigiri 			psci_register_spd_pm_hook(&tspd_pm);
379faaa2e76SVikram Kanigiri 
380faaa2e76SVikram Kanigiri 			/*
381faaa2e76SVikram Kanigiri 			 * Register an interrupt handler for S-EL1 interrupts
382faaa2e76SVikram Kanigiri 			 * when generated during code executing in the
383faaa2e76SVikram Kanigiri 			 * non-secure state.
384faaa2e76SVikram Kanigiri 			 */
385faaa2e76SVikram Kanigiri 			flags = 0;
386faaa2e76SVikram Kanigiri 			set_interrupt_rm_flag(flags, NON_SECURE);
387faaa2e76SVikram Kanigiri 			rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
388faaa2e76SVikram Kanigiri 						tspd_sel1_interrupt_handler,
389faaa2e76SVikram Kanigiri 						flags);
390faaa2e76SVikram Kanigiri 			if (rc)
391faaa2e76SVikram Kanigiri 				panic();
392faaa2e76SVikram Kanigiri 		}
393faaa2e76SVikram Kanigiri 
394faaa2e76SVikram Kanigiri 
395faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC
396faaa2e76SVikram Kanigiri 		/* Save the Secure EL1 system register context */
397faaa2e76SVikram Kanigiri 		assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx);
398faaa2e76SVikram Kanigiri 		cm_el1_sysregs_context_save(SECURE);
399faaa2e76SVikram Kanigiri 
400faaa2e76SVikram Kanigiri 		/* Program EL3 registers to enable entry into the next EL */
401faaa2e76SVikram Kanigiri 		next_image_info = bl31_plat_get_next_image_ep_info(NON_SECURE);
402faaa2e76SVikram Kanigiri 		assert(next_image_info);
403faaa2e76SVikram Kanigiri 		assert(NON_SECURE ==
404faaa2e76SVikram Kanigiri 				GET_SECURITY_STATE(next_image_info->h.attr));
405faaa2e76SVikram Kanigiri 
406faaa2e76SVikram Kanigiri 		cm_init_context(read_mpidr_el1(), next_image_info);
407faaa2e76SVikram Kanigiri 		cm_prepare_el3_exit(NON_SECURE);
408faaa2e76SVikram Kanigiri 		SMC_RET0(cm_get_context(NON_SECURE));
409faaa2e76SVikram Kanigiri #else
410375f538aSAchin Gupta 		/*
411375f538aSAchin Gupta 		 * SP reports completion. The SPD must have initiated
412375f538aSAchin Gupta 		 * the original request through a synchronous entry
413375f538aSAchin Gupta 		 * into the SP. Jump back to the original C runtime
414375f538aSAchin Gupta 		 * context.
415375f538aSAchin Gupta 		 */
416916a2c1eSAchin Gupta 		tspd_synchronous_sp_exit(tsp_ctx, x1);
417faaa2e76SVikram Kanigiri #endif
418375f538aSAchin Gupta 
419607084eeSAchin Gupta 	/*
420607084eeSAchin Gupta 	 * These function IDs is used only by the SP to indicate it has
421607084eeSAchin Gupta 	 * finished:
422607084eeSAchin Gupta 	 * 1. turning itself on in response to an earlier psci
423607084eeSAchin Gupta 	 *    cpu_on request
424607084eeSAchin Gupta 	 * 2. resuming itself after an earlier psci cpu_suspend
425607084eeSAchin Gupta 	 *    request.
426607084eeSAchin Gupta 	 */
427607084eeSAchin Gupta 	case TSP_ON_DONE:
428607084eeSAchin Gupta 	case TSP_RESUME_DONE:
429607084eeSAchin Gupta 
430607084eeSAchin Gupta 	/*
431607084eeSAchin Gupta 	 * These function IDs is used only by the SP to indicate it has
432607084eeSAchin Gupta 	 * finished:
433607084eeSAchin Gupta 	 * 1. suspending itself after an earlier psci cpu_suspend
434607084eeSAchin Gupta 	 *    request.
435607084eeSAchin Gupta 	 * 2. turning itself off in response to an earlier psci
436607084eeSAchin Gupta 	 *    cpu_off request.
437607084eeSAchin Gupta 	 */
438607084eeSAchin Gupta 	case TSP_OFF_DONE:
439607084eeSAchin Gupta 	case TSP_SUSPEND_DONE:
440*d5f13093SJuan Castillo 	case TSP_SYSTEM_OFF_DONE:
441*d5f13093SJuan Castillo 	case TSP_SYSTEM_RESET_DONE:
442607084eeSAchin Gupta 		if (ns)
443607084eeSAchin Gupta 			SMC_RET1(handle, SMC_UNK);
444607084eeSAchin Gupta 
445607084eeSAchin Gupta 		/*
446607084eeSAchin Gupta 		 * SP reports completion. The SPD must have initiated the
447607084eeSAchin Gupta 		 * original request through a synchronous entry into the SP.
448607084eeSAchin Gupta 		 * Jump back to the original C runtime context, and pass x1 as
449607084eeSAchin Gupta 		 * return value to the caller
450607084eeSAchin Gupta 		 */
451916a2c1eSAchin Gupta 		tspd_synchronous_sp_exit(tsp_ctx, x1);
452607084eeSAchin Gupta 
453916a2c1eSAchin Gupta 		/*
454916a2c1eSAchin Gupta 		 * Request from non-secure client to perform an
455916a2c1eSAchin Gupta 		 * arithmetic operation or response from secure
456916a2c1eSAchin Gupta 		 * payload to an earlier request.
457916a2c1eSAchin Gupta 		 */
458239b04faSSoby Mathew 	case TSP_FAST_FID(TSP_ADD):
459239b04faSSoby Mathew 	case TSP_FAST_FID(TSP_SUB):
460239b04faSSoby Mathew 	case TSP_FAST_FID(TSP_MUL):
461239b04faSSoby Mathew 	case TSP_FAST_FID(TSP_DIV):
462239b04faSSoby Mathew 
463239b04faSSoby Mathew 	case TSP_STD_FID(TSP_ADD):
464239b04faSSoby Mathew 	case TSP_STD_FID(TSP_SUB):
465239b04faSSoby Mathew 	case TSP_STD_FID(TSP_MUL):
466239b04faSSoby Mathew 	case TSP_STD_FID(TSP_DIV):
467916a2c1eSAchin Gupta 		if (ns) {
468916a2c1eSAchin Gupta 			/*
469916a2c1eSAchin Gupta 			 * This is a fresh request from the non-secure client.
470916a2c1eSAchin Gupta 			 * The parameters are in x1 and x2. Figure out which
471916a2c1eSAchin Gupta 			 * registers need to be preserved, save the non-secure
472916a2c1eSAchin Gupta 			 * state and send the request to the secure payload.
473916a2c1eSAchin Gupta 			 */
47408ab89d3SAndrew Thoelke 			assert(handle == cm_get_context(NON_SECURE));
475239b04faSSoby Mathew 
476239b04faSSoby Mathew 			/* Check if we are already preempted */
477239b04faSSoby Mathew 			if (get_std_smc_active_flag(tsp_ctx->state))
478239b04faSSoby Mathew 				SMC_RET1(handle, SMC_UNK);
479239b04faSSoby Mathew 
480916a2c1eSAchin Gupta 			cm_el1_sysregs_context_save(NON_SECURE);
481916a2c1eSAchin Gupta 
482916a2c1eSAchin Gupta 			/* Save x1 and x2 for use by TSP_GET_ARGS call below */
483239b04faSSoby Mathew 			store_tsp_args(tsp_ctx, x1, x2);
484916a2c1eSAchin Gupta 
485916a2c1eSAchin Gupta 			/*
486916a2c1eSAchin Gupta 			 * We are done stashing the non-secure context. Ask the
487916a2c1eSAchin Gupta 			 * secure payload to do the work now.
488916a2c1eSAchin Gupta 			 */
489916a2c1eSAchin Gupta 
490916a2c1eSAchin Gupta 			/*
491916a2c1eSAchin Gupta 			 * Verify if there is a valid context to use, copy the
492916a2c1eSAchin Gupta 			 * operation type and parameters to the secure context
493916a2c1eSAchin Gupta 			 * and jump to the fast smc entry point in the secure
494916a2c1eSAchin Gupta 			 * payload. Entry into S-EL1 will take place upon exit
495916a2c1eSAchin Gupta 			 * from this function.
496916a2c1eSAchin Gupta 			 */
49708ab89d3SAndrew Thoelke 			assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE));
498239b04faSSoby Mathew 
499239b04faSSoby Mathew 			/* Set appropriate entry for SMC.
500239b04faSSoby Mathew 			 * We expect the TSP to manage the PSTATE.I and PSTATE.F
501239b04faSSoby Mathew 			 * flags as appropriate.
502239b04faSSoby Mathew 			 */
503239b04faSSoby Mathew 			if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_FAST) {
504239b04faSSoby Mathew 				cm_set_elr_el3(SECURE, (uint64_t)
505399fb08fSAndrew Thoelke 						&tsp_vectors->fast_smc_entry);
506239b04faSSoby Mathew 			} else {
507239b04faSSoby Mathew 				set_std_smc_active_flag(tsp_ctx->state);
508239b04faSSoby Mathew 				cm_set_elr_el3(SECURE, (uint64_t)
509399fb08fSAndrew Thoelke 						&tsp_vectors->std_smc_entry);
510239b04faSSoby Mathew 			}
511239b04faSSoby Mathew 
512916a2c1eSAchin Gupta 			cm_el1_sysregs_context_restore(SECURE);
513916a2c1eSAchin Gupta 			cm_set_next_eret_context(SECURE);
514239b04faSSoby Mathew 			SMC_RET3(&tsp_ctx->cpu_ctx, smc_fid, x1, x2);
515916a2c1eSAchin Gupta 		} else {
516916a2c1eSAchin Gupta 			/*
517916a2c1eSAchin Gupta 			 * This is the result from the secure client of an
518239b04faSSoby Mathew 			 * earlier request. The results are in x1-x3. Copy it
519916a2c1eSAchin Gupta 			 * into the non-secure context, save the secure state
520916a2c1eSAchin Gupta 			 * and return to the non-secure state.
521916a2c1eSAchin Gupta 			 */
52208ab89d3SAndrew Thoelke 			assert(handle == cm_get_context(SECURE));
523916a2c1eSAchin Gupta 			cm_el1_sysregs_context_save(SECURE);
524916a2c1eSAchin Gupta 
525916a2c1eSAchin Gupta 			/* Get a reference to the non-secure context */
52608ab89d3SAndrew Thoelke 			ns_cpu_context = cm_get_context(NON_SECURE);
527916a2c1eSAchin Gupta 			assert(ns_cpu_context);
528916a2c1eSAchin Gupta 
529916a2c1eSAchin Gupta 			/* Restore non-secure state */
530916a2c1eSAchin Gupta 			cm_el1_sysregs_context_restore(NON_SECURE);
531916a2c1eSAchin Gupta 			cm_set_next_eret_context(NON_SECURE);
532239b04faSSoby Mathew 			if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_STD)
533239b04faSSoby Mathew 				clr_std_smc_active_flag(tsp_ctx->state);
534239b04faSSoby Mathew 			SMC_RET3(ns_cpu_context, x1, x2, x3);
535916a2c1eSAchin Gupta 		}
536916a2c1eSAchin Gupta 
537916a2c1eSAchin Gupta 		break;
538916a2c1eSAchin Gupta 
539916a2c1eSAchin Gupta 		/*
540239b04faSSoby Mathew 		 * Request from non secure world to resume the preempted
541239b04faSSoby Mathew 		 * Standard SMC call.
542239b04faSSoby Mathew 		 */
543239b04faSSoby Mathew 	case TSP_FID_RESUME:
544239b04faSSoby Mathew 		/* RESUME should be invoked only by normal world */
545239b04faSSoby Mathew 		if (!ns) {
546239b04faSSoby Mathew 			assert(0);
547239b04faSSoby Mathew 			break;
548239b04faSSoby Mathew 		}
549239b04faSSoby Mathew 
550239b04faSSoby Mathew 		/*
551239b04faSSoby Mathew 		 * This is a resume request from the non-secure client.
552239b04faSSoby Mathew 		 * save the non-secure state and send the request to
553239b04faSSoby Mathew 		 * the secure payload.
554239b04faSSoby Mathew 		 */
55508ab89d3SAndrew Thoelke 		assert(handle == cm_get_context(NON_SECURE));
556239b04faSSoby Mathew 
557239b04faSSoby Mathew 		/* Check if we are already preempted before resume */
558239b04faSSoby Mathew 		if (!get_std_smc_active_flag(tsp_ctx->state))
559239b04faSSoby Mathew 			SMC_RET1(handle, SMC_UNK);
560239b04faSSoby Mathew 
561239b04faSSoby Mathew 		cm_el1_sysregs_context_save(NON_SECURE);
562239b04faSSoby Mathew 
563239b04faSSoby Mathew 		/*
564239b04faSSoby Mathew 		 * We are done stashing the non-secure context. Ask the
565239b04faSSoby Mathew 		 * secure payload to do the work now.
566239b04faSSoby Mathew 		 */
567239b04faSSoby Mathew 
568239b04faSSoby Mathew 		/* We just need to return to the preempted point in
569239b04faSSoby Mathew 		 * TSP and the execution will resume as normal.
570239b04faSSoby Mathew 		 */
571239b04faSSoby Mathew 		cm_el1_sysregs_context_restore(SECURE);
572239b04faSSoby Mathew 		cm_set_next_eret_context(SECURE);
57310b65ecfSSoby Mathew 		SMC_RET0(&tsp_ctx->cpu_ctx);
574239b04faSSoby Mathew 
575239b04faSSoby Mathew 		/*
576916a2c1eSAchin Gupta 		 * This is a request from the secure payload for more arguments
577916a2c1eSAchin Gupta 		 * for an ongoing arithmetic operation requested by the
578916a2c1eSAchin Gupta 		 * non-secure world. Simply return the arguments from the non-
579916a2c1eSAchin Gupta 		 * secure client in the original call.
580916a2c1eSAchin Gupta 		 */
581916a2c1eSAchin Gupta 	case TSP_GET_ARGS:
582916a2c1eSAchin Gupta 		if (ns)
583916a2c1eSAchin Gupta 			SMC_RET1(handle, SMC_UNK);
584916a2c1eSAchin Gupta 
585239b04faSSoby Mathew 		get_tsp_args(tsp_ctx, x1, x2);
586239b04faSSoby Mathew 		SMC_RET2(handle, x1, x2);
587916a2c1eSAchin Gupta 
58852538b9bSJeenu Viswambharan 	case TOS_CALL_COUNT:
58952538b9bSJeenu Viswambharan 		/*
59052538b9bSJeenu Viswambharan 		 * Return the number of service function IDs implemented to
59152538b9bSJeenu Viswambharan 		 * provide service to non-secure
59252538b9bSJeenu Viswambharan 		 */
59352538b9bSJeenu Viswambharan 		SMC_RET1(handle, TSP_NUM_FID);
59452538b9bSJeenu Viswambharan 
59552538b9bSJeenu Viswambharan 	case TOS_UID:
59652538b9bSJeenu Viswambharan 		/* Return TSP UID to the caller */
59752538b9bSJeenu Viswambharan 		SMC_UUID_RET(handle, tsp_uuid);
59852538b9bSJeenu Viswambharan 
59952538b9bSJeenu Viswambharan 	case TOS_CALL_VERSION:
60052538b9bSJeenu Viswambharan 		/* Return the version of current implementation */
60152538b9bSJeenu Viswambharan 		SMC_RET2(handle, TSP_VERSION_MAJOR, TSP_VERSION_MINOR);
60252538b9bSJeenu Viswambharan 
603375f538aSAchin Gupta 	default:
604607084eeSAchin Gupta 		break;
605375f538aSAchin Gupta 	}
606375f538aSAchin Gupta 
607607084eeSAchin Gupta 	SMC_RET1(handle, SMC_UNK);
608375f538aSAchin Gupta }
609375f538aSAchin Gupta 
610239b04faSSoby Mathew /* Define a SPD runtime service descriptor for fast SMC calls */
611375f538aSAchin Gupta DECLARE_RT_SVC(
612239b04faSSoby Mathew 	tspd_fast,
613375f538aSAchin Gupta 
614375f538aSAchin Gupta 	OEN_TOS_START,
615375f538aSAchin Gupta 	OEN_TOS_END,
616375f538aSAchin Gupta 	SMC_TYPE_FAST,
617375f538aSAchin Gupta 	tspd_setup,
618375f538aSAchin Gupta 	tspd_smc_handler
619375f538aSAchin Gupta );
620239b04faSSoby Mathew 
621239b04faSSoby Mathew /* Define a SPD runtime service descriptor for standard SMC calls */
622239b04faSSoby Mathew DECLARE_RT_SVC(
623239b04faSSoby Mathew 	tspd_std,
624239b04faSSoby Mathew 
625239b04faSSoby Mathew 	OEN_TOS_START,
626239b04faSSoby Mathew 	OEN_TOS_END,
627239b04faSSoby Mathew 	SMC_TYPE_STD,
628239b04faSSoby Mathew 	NULL,
629239b04faSSoby Mathew 	tspd_smc_handler
630239b04faSSoby Mathew );
631