xref: /rk3399_ARM-atf/services/spd/tlkd/tlkd_main.c (revision 26670c8260a03add519ee26d3da7ac2bce64ee1e)
122038315SVarun Wadekar /*
2*26670c82SVarun Wadekar  * Copyright (c) 2015-2016, ARM Limited and Contributors. All rights reserved.
322038315SVarun Wadekar  *
422038315SVarun Wadekar  * Redistribution and use in source and binary forms, with or without
522038315SVarun Wadekar  * modification, are permitted provided that the following conditions are met:
622038315SVarun Wadekar  *
722038315SVarun Wadekar  * Redistributions of source code must retain the above copyright notice, this
822038315SVarun Wadekar  * list of conditions and the following disclaimer.
922038315SVarun Wadekar  *
1022038315SVarun Wadekar  * Redistributions in binary form must reproduce the above copyright notice,
1122038315SVarun Wadekar  * this list of conditions and the following disclaimer in the documentation
1222038315SVarun Wadekar  * and/or other materials provided with the distribution.
1322038315SVarun Wadekar  *
1422038315SVarun Wadekar  * Neither the name of ARM nor the names of its contributors may be used
1522038315SVarun Wadekar  * to endorse or promote products derived from this software without specific
1622038315SVarun Wadekar  * prior written permission.
1722038315SVarun Wadekar  *
1822038315SVarun Wadekar  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1922038315SVarun Wadekar  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2022038315SVarun Wadekar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2122038315SVarun Wadekar  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
2222038315SVarun Wadekar  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2322038315SVarun Wadekar  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2422038315SVarun Wadekar  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2522038315SVarun Wadekar  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2622038315SVarun Wadekar  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2722038315SVarun Wadekar  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2822038315SVarun Wadekar  * POSSIBILITY OF SUCH DAMAGE.
2922038315SVarun Wadekar  */
3022038315SVarun Wadekar 
3122038315SVarun Wadekar /*******************************************************************************
3222038315SVarun Wadekar  * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a
3322038315SVarun Wadekar  * plug-in component to the Secure Monitor, registered as a runtime service. The
3422038315SVarun Wadekar  * SPD is expected to be a functional extension of the Secure Payload (SP) that
3522038315SVarun Wadekar  * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting
3622038315SVarun Wadekar  * the Trusted OS/Applications range to the dispatcher. The SPD will either
3722038315SVarun Wadekar  * handle the request locally or delegate it to the Secure Payload. It is also
3822038315SVarun Wadekar  * responsible for initialising and maintaining communication with the SP.
3922038315SVarun Wadekar  ******************************************************************************/
4022038315SVarun Wadekar #include <arch_helpers.h>
4122038315SVarun Wadekar #include <assert.h>
4222038315SVarun Wadekar #include <bl_common.h>
4322038315SVarun Wadekar #include <bl31.h>
4422038315SVarun Wadekar #include <context_mgmt.h>
4522038315SVarun Wadekar #include <debug.h>
4622038315SVarun Wadekar #include <errno.h>
4722038315SVarun Wadekar #include <platform.h>
4822038315SVarun Wadekar #include <runtime_svc.h>
4922038315SVarun Wadekar #include <stddef.h>
5022038315SVarun Wadekar #include <tlk.h>
5122038315SVarun Wadekar #include <uuid.h>
5222038315SVarun Wadekar #include "tlkd_private.h"
5322038315SVarun Wadekar 
5422038315SVarun Wadekar extern const spd_pm_ops_t tlkd_pm_ops;
5522038315SVarun Wadekar 
5622038315SVarun Wadekar /*******************************************************************************
57cb790c5eSVarun Wadekar  * Per-cpu Secure Payload state
5822038315SVarun Wadekar  ******************************************************************************/
59cb790c5eSVarun Wadekar tlk_context_t tlk_ctx;
6022038315SVarun Wadekar 
61*26670c82SVarun Wadekar /*******************************************************************************
62*26670c82SVarun Wadekar  * CPU number on which TLK booted up
63*26670c82SVarun Wadekar  ******************************************************************************/
64*26670c82SVarun Wadekar static int boot_cpu;
65*26670c82SVarun Wadekar 
6622038315SVarun Wadekar /* TLK UID: RFC-4122 compliant UUID (version-5, sha-1) */
6722038315SVarun Wadekar DEFINE_SVC_UUID(tlk_uuid,
6822038315SVarun Wadekar 		0xbd11e9c9, 0x2bba, 0x52ee, 0xb1, 0x72,
6922038315SVarun Wadekar 		0x46, 0x1f, 0xba, 0x97, 0x7f, 0x63);
7022038315SVarun Wadekar 
7122038315SVarun Wadekar int32_t tlkd_init(void);
7222038315SVarun Wadekar 
7322038315SVarun Wadekar /*******************************************************************************
7422038315SVarun Wadekar  * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type
7522038315SVarun Wadekar  * (aarch32/aarch64) if not already known and initialises the context for entry
7622038315SVarun Wadekar  * into the SP for its initialisation.
7722038315SVarun Wadekar  ******************************************************************************/
7822038315SVarun Wadekar int32_t tlkd_setup(void)
7922038315SVarun Wadekar {
8022038315SVarun Wadekar 	entry_point_info_t *tlk_ep_info;
8122038315SVarun Wadekar 
8222038315SVarun Wadekar 	/*
8322038315SVarun Wadekar 	 * Get information about the Secure Payload (BL32) image. Its
8422038315SVarun Wadekar 	 * absence is a critical failure.
8522038315SVarun Wadekar 	 */
8622038315SVarun Wadekar 	tlk_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
8722038315SVarun Wadekar 	if (!tlk_ep_info) {
8822038315SVarun Wadekar 		WARN("No SP provided. Booting device without SP"
8922038315SVarun Wadekar 			" initialization. SMC`s destined for SP"
9022038315SVarun Wadekar 			" will return SMC_UNK\n");
9122038315SVarun Wadekar 		return 1;
9222038315SVarun Wadekar 	}
9322038315SVarun Wadekar 
9422038315SVarun Wadekar 	/*
9522038315SVarun Wadekar 	 * If there's no valid entry point for SP, we return a non-zero value
9622038315SVarun Wadekar 	 * signalling failure initializing the service. We bail out without
9722038315SVarun Wadekar 	 * registering any handlers
9822038315SVarun Wadekar 	 */
9922038315SVarun Wadekar 	if (!tlk_ep_info->pc)
10022038315SVarun Wadekar 		return 1;
10122038315SVarun Wadekar 
10222038315SVarun Wadekar 	/*
10322038315SVarun Wadekar 	 * Inspect the SP image's SPSR and determine it's execution state
10422038315SVarun Wadekar 	 * i.e whether AArch32 or AArch64.
10522038315SVarun Wadekar 	 */
10622038315SVarun Wadekar 	tlkd_init_tlk_ep_state(tlk_ep_info,
10722038315SVarun Wadekar 		(tlk_ep_info->spsr >> MODE_RW_SHIFT) & MODE_RW_MASK,
10822038315SVarun Wadekar 		tlk_ep_info->pc,
10922038315SVarun Wadekar 		&tlk_ctx);
11022038315SVarun Wadekar 
11122038315SVarun Wadekar 	/*
11222038315SVarun Wadekar 	 * All TLK SPD initialization done. Now register our init function
11322038315SVarun Wadekar 	 * with BL31 for deferred invocation
11422038315SVarun Wadekar 	 */
11522038315SVarun Wadekar 	bl31_register_bl32_init(&tlkd_init);
11622038315SVarun Wadekar 
11722038315SVarun Wadekar 	return 0;
11822038315SVarun Wadekar }
11922038315SVarun Wadekar 
12022038315SVarun Wadekar /*******************************************************************************
12122038315SVarun Wadekar  * This function passes control to the Secure Payload image (BL32) for the first
12222038315SVarun Wadekar  * time on the primary cpu after a cold boot. It assumes that a valid secure
12322038315SVarun Wadekar  * context has already been created by tlkd_setup() which can be directly
12422038315SVarun Wadekar  * used. This function performs a synchronous entry into the Secure payload.
12522038315SVarun Wadekar  * The SP passes control back to this routine through a SMC.
12622038315SVarun Wadekar  ******************************************************************************/
12722038315SVarun Wadekar int32_t tlkd_init(void)
12822038315SVarun Wadekar {
12922038315SVarun Wadekar 	entry_point_info_t *tlk_entry_point;
13022038315SVarun Wadekar 
13122038315SVarun Wadekar 	/*
13222038315SVarun Wadekar 	 * Get information about the Secure Payload (BL32) image. Its
13322038315SVarun Wadekar 	 * absence is a critical failure.
13422038315SVarun Wadekar 	 */
13522038315SVarun Wadekar 	tlk_entry_point = bl31_plat_get_next_image_ep_info(SECURE);
13622038315SVarun Wadekar 	assert(tlk_entry_point);
13722038315SVarun Wadekar 
138fd650ff6SSoby Mathew 	cm_init_my_context(tlk_entry_point);
13922038315SVarun Wadekar 
14022038315SVarun Wadekar 	/*
141*26670c82SVarun Wadekar 	 * TLK runs only on a single CPU. Store the value of the boot
142*26670c82SVarun Wadekar 	 * CPU for sanity checking later.
143*26670c82SVarun Wadekar 	 */
144*26670c82SVarun Wadekar 	boot_cpu = plat_my_core_pos();
145*26670c82SVarun Wadekar 
146*26670c82SVarun Wadekar 	/*
14722038315SVarun Wadekar 	 * Arrange for an entry into the test secure payload.
14822038315SVarun Wadekar 	 */
14922038315SVarun Wadekar 	return tlkd_synchronous_sp_entry(&tlk_ctx);
15022038315SVarun Wadekar }
15122038315SVarun Wadekar 
15222038315SVarun Wadekar /*******************************************************************************
15322038315SVarun Wadekar  * This function is responsible for handling all SMCs in the Trusted OS/App
15422038315SVarun Wadekar  * range from the non-secure state as defined in the SMC Calling Convention
15522038315SVarun Wadekar  * Document. It is also responsible for communicating with the Secure payload
15622038315SVarun Wadekar  * to delegate work and return results back to the non-secure state. Lastly it
15722038315SVarun Wadekar  * will also return any information that the secure payload needs to do the
15822038315SVarun Wadekar  * work assigned to it.
15922038315SVarun Wadekar  ******************************************************************************/
16022038315SVarun Wadekar uint64_t tlkd_smc_handler(uint32_t smc_fid,
16122038315SVarun Wadekar 			 uint64_t x1,
16222038315SVarun Wadekar 			 uint64_t x2,
16322038315SVarun Wadekar 			 uint64_t x3,
16422038315SVarun Wadekar 			 uint64_t x4,
16522038315SVarun Wadekar 			 void *cookie,
16622038315SVarun Wadekar 			 void *handle,
16722038315SVarun Wadekar 			 uint64_t flags)
16822038315SVarun Wadekar {
16977199df7SVarun Wadekar 	cpu_context_t *ns_cpu_context;
170709a3c47SVarun Wadekar 	gp_regs_t *gp_regs;
17122038315SVarun Wadekar 	uint32_t ns;
172709a3c47SVarun Wadekar 	uint64_t par;
17322038315SVarun Wadekar 
17422038315SVarun Wadekar 	/* Passing a NULL context is a critical programming error */
17522038315SVarun Wadekar 	assert(handle);
17622038315SVarun Wadekar 
177*26670c82SVarun Wadekar 	/* These SMCs are only supported by a single CPU */
178*26670c82SVarun Wadekar 	if (boot_cpu != plat_my_core_pos())
1796693962cSVarun Wadekar 		SMC_RET1(handle, SMC_UNK);
1806693962cSVarun Wadekar 
18122038315SVarun Wadekar 	/* Determine which security state this SMC originated from */
18222038315SVarun Wadekar 	ns = is_caller_non_secure(flags);
18322038315SVarun Wadekar 
18422038315SVarun Wadekar 	switch (smc_fid) {
18522038315SVarun Wadekar 
18622038315SVarun Wadekar 	/*
187f9d25054SVarun Wadekar 	 * This function ID is used by SP to indicate that it was
188f9d25054SVarun Wadekar 	 * preempted by a non-secure world IRQ.
189f9d25054SVarun Wadekar 	 */
190f9d25054SVarun Wadekar 	case TLK_PREEMPTED:
191f9d25054SVarun Wadekar 
192f9d25054SVarun Wadekar 		if (ns)
193f9d25054SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
194f9d25054SVarun Wadekar 
195f9d25054SVarun Wadekar 		assert(handle == cm_get_context(SECURE));
196f9d25054SVarun Wadekar 		cm_el1_sysregs_context_save(SECURE);
197f9d25054SVarun Wadekar 
198f9d25054SVarun Wadekar 		/* Get a reference to the non-secure context */
199f9d25054SVarun Wadekar 		ns_cpu_context = cm_get_context(NON_SECURE);
200f9d25054SVarun Wadekar 		assert(ns_cpu_context);
201f9d25054SVarun Wadekar 
202f9d25054SVarun Wadekar 		/*
203f9d25054SVarun Wadekar 		 * Restore non-secure state. There is no need to save the
204f9d25054SVarun Wadekar 		 * secure system register context since the SP was supposed
205f9d25054SVarun Wadekar 		 * to preserve it during S-EL1 interrupt handling.
206f9d25054SVarun Wadekar 		 */
207f9d25054SVarun Wadekar 		cm_el1_sysregs_context_restore(NON_SECURE);
208f9d25054SVarun Wadekar 		cm_set_next_eret_context(NON_SECURE);
209f9d25054SVarun Wadekar 
210709a3c47SVarun Wadekar 		SMC_RET1(ns_cpu_context, x1);
211f9d25054SVarun Wadekar 
212f9d25054SVarun Wadekar 	/*
21377199df7SVarun Wadekar 	 * This is a request from the non-secure context to:
21477199df7SVarun Wadekar 	 *
21577199df7SVarun Wadekar 	 * a. register shared memory with the SP for storing it's
21677199df7SVarun Wadekar 	 *    activity logs.
21777199df7SVarun Wadekar 	 * b. register shared memory with the SP for passing args
21877199df7SVarun Wadekar 	 *    required for maintaining sessions with the Trusted
21977199df7SVarun Wadekar 	 *    Applications.
2206693962cSVarun Wadekar 	 * c. open/close sessions
2216693962cSVarun Wadekar 	 * d. issue commands to the Trusted Apps
222ca15d9bcSVarun Wadekar 	 * e. resume the preempted standard SMC call.
22377199df7SVarun Wadekar 	 */
22477199df7SVarun Wadekar 	case TLK_REGISTER_LOGBUF:
22577199df7SVarun Wadekar 	case TLK_REGISTER_REQBUF:
2266693962cSVarun Wadekar 	case TLK_OPEN_TA_SESSION:
2276693962cSVarun Wadekar 	case TLK_CLOSE_TA_SESSION:
2286693962cSVarun Wadekar 	case TLK_TA_LAUNCH_OP:
2296693962cSVarun Wadekar 	case TLK_TA_SEND_EVENT:
230ca15d9bcSVarun Wadekar 	case TLK_RESUME_FID:
2316693962cSVarun Wadekar 
232709a3c47SVarun Wadekar 		if (!ns)
23377199df7SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
23477199df7SVarun Wadekar 
23577199df7SVarun Wadekar 		/*
23677199df7SVarun Wadekar 		 * This is a fresh request from the non-secure client.
23777199df7SVarun Wadekar 		 * The parameters are in x1 and x2. Figure out which
23877199df7SVarun Wadekar 		 * registers need to be preserved, save the non-secure
23977199df7SVarun Wadekar 		 * state and send the request to the secure payload.
24077199df7SVarun Wadekar 		 */
24177199df7SVarun Wadekar 		assert(handle == cm_get_context(NON_SECURE));
24277199df7SVarun Wadekar 
243ca15d9bcSVarun Wadekar 		/*
244ca15d9bcSVarun Wadekar 		 * Check if we are already processing a standard SMC
245ca15d9bcSVarun Wadekar 		 * call. Of all the supported fids, only the "resume"
246ca15d9bcSVarun Wadekar 		 * fid expects the flag to be set.
247ca15d9bcSVarun Wadekar 		 */
248ca15d9bcSVarun Wadekar 		if (smc_fid == TLK_RESUME_FID) {
249ca15d9bcSVarun Wadekar 			if (!get_std_smc_active_flag(tlk_ctx.state))
250ca15d9bcSVarun Wadekar 				SMC_RET1(handle, SMC_UNK);
251ca15d9bcSVarun Wadekar 		} else {
25277199df7SVarun Wadekar 			if (get_std_smc_active_flag(tlk_ctx.state))
25377199df7SVarun Wadekar 				SMC_RET1(handle, SMC_UNK);
254ca15d9bcSVarun Wadekar 		}
25577199df7SVarun Wadekar 
25677199df7SVarun Wadekar 		cm_el1_sysregs_context_save(NON_SECURE);
25777199df7SVarun Wadekar 
25877199df7SVarun Wadekar 		/*
25977199df7SVarun Wadekar 		 * Verify if there is a valid context to use.
26077199df7SVarun Wadekar 		 */
26177199df7SVarun Wadekar 		assert(&tlk_ctx.cpu_ctx == cm_get_context(SECURE));
26277199df7SVarun Wadekar 
26377199df7SVarun Wadekar 		/*
26477199df7SVarun Wadekar 		 * Mark the SP state as active.
26577199df7SVarun Wadekar 		 */
26677199df7SVarun Wadekar 		set_std_smc_active_flag(tlk_ctx.state);
26777199df7SVarun Wadekar 
26877199df7SVarun Wadekar 		/*
26977199df7SVarun Wadekar 		 * We are done stashing the non-secure context. Ask the
27077199df7SVarun Wadekar 		 * secure payload to do the work now.
27177199df7SVarun Wadekar 		 */
27277199df7SVarun Wadekar 		cm_el1_sysregs_context_restore(SECURE);
27377199df7SVarun Wadekar 		cm_set_next_eret_context(SECURE);
27477199df7SVarun Wadekar 
27577199df7SVarun Wadekar 		/*
276709a3c47SVarun Wadekar 		 * TLK is a 32-bit Trusted OS and so expects the SMC
277709a3c47SVarun Wadekar 		 * arguments via r0-r7. TLK expects the monitor frame
278709a3c47SVarun Wadekar 		 * registers to be 64-bits long. Hence, we pass x0 in
279709a3c47SVarun Wadekar 		 * r0-r1, x1 in r2-r3, x3 in r4-r5 and x4 in r6-r7.
280709a3c47SVarun Wadekar 		 *
281709a3c47SVarun Wadekar 		 * As smc_fid is a uint32 value, r1 contains 0.
282709a3c47SVarun Wadekar 		 */
283709a3c47SVarun Wadekar 		gp_regs = get_gpregs_ctx(&tlk_ctx.cpu_ctx);
284709a3c47SVarun Wadekar 		write_ctx_reg(gp_regs, CTX_GPREG_X4, (uint32_t)x2);
285709a3c47SVarun Wadekar 		write_ctx_reg(gp_regs, CTX_GPREG_X5, (uint32_t)(x2 >> 32));
286709a3c47SVarun Wadekar 		write_ctx_reg(gp_regs, CTX_GPREG_X6, (uint32_t)x3);
287709a3c47SVarun Wadekar 		write_ctx_reg(gp_regs, CTX_GPREG_X7, (uint32_t)(x3 >> 32));
288709a3c47SVarun Wadekar 		SMC_RET4(&tlk_ctx.cpu_ctx, smc_fid, 0, (uint32_t)x1,
289709a3c47SVarun Wadekar 			(uint32_t)(x1 >> 32));
290709a3c47SVarun Wadekar 
291709a3c47SVarun Wadekar 	/*
292709a3c47SVarun Wadekar 	 * Translate NS/EL1-S virtual addresses.
293709a3c47SVarun Wadekar 	 *
294709a3c47SVarun Wadekar 	 * x1 = virtual address
295709a3c47SVarun Wadekar 	 * x3 = type (NS/S)
296709a3c47SVarun Wadekar 	 *
297709a3c47SVarun Wadekar 	 * Returns PA:lo in r0, PA:hi in r1.
2986e159e7aSVarun Wadekar 	 */
2996e159e7aSVarun Wadekar 	case TLK_VA_TRANSLATE:
300709a3c47SVarun Wadekar 
301709a3c47SVarun Wadekar 		/* Should be invoked only by secure world */
302709a3c47SVarun Wadekar 		if (ns)
3036e159e7aSVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
3046e159e7aSVarun Wadekar 
305709a3c47SVarun Wadekar 		/* NS virtual addresses are 64-bit long */
306709a3c47SVarun Wadekar 		if (x3 & TLK_TRANSLATE_NS_VADDR)
307709a3c47SVarun Wadekar 			x1 = (uint32_t)x1 | (x2 << 32);
308709a3c47SVarun Wadekar 
309709a3c47SVarun Wadekar 		if (!x1)
310709a3c47SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
311709a3c47SVarun Wadekar 
312709a3c47SVarun Wadekar 		/*
313709a3c47SVarun Wadekar 		 * TODO: Sanity check x1. This would require platform
314709a3c47SVarun Wadekar 		 * support.
315709a3c47SVarun Wadekar 		 */
316709a3c47SVarun Wadekar 
3176e159e7aSVarun Wadekar 		/* virtual address and type: ns/s */
318709a3c47SVarun Wadekar 		par = tlkd_va_translate(x1, x3);
3196e159e7aSVarun Wadekar 
320709a3c47SVarun Wadekar 		/* return physical address in r0-r1 */
321709a3c47SVarun Wadekar 		SMC_RET4(handle, (uint32_t)par, (uint32_t)(par >> 32), 0, 0);
3226e159e7aSVarun Wadekar 
3236e159e7aSVarun Wadekar 	/*
32477199df7SVarun Wadekar 	 * This is a request from the SP to mark completion of
32577199df7SVarun Wadekar 	 * a standard function ID.
32677199df7SVarun Wadekar 	 */
32777199df7SVarun Wadekar 	case TLK_REQUEST_DONE:
328709a3c47SVarun Wadekar 		if (ns)
32977199df7SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
33077199df7SVarun Wadekar 
33177199df7SVarun Wadekar 		/*
33277199df7SVarun Wadekar 		 * Mark the SP state as inactive.
33377199df7SVarun Wadekar 		 */
33477199df7SVarun Wadekar 		clr_std_smc_active_flag(tlk_ctx.state);
33577199df7SVarun Wadekar 
33677199df7SVarun Wadekar 		/* Get a reference to the non-secure context */
33777199df7SVarun Wadekar 		ns_cpu_context = cm_get_context(NON_SECURE);
33877199df7SVarun Wadekar 		assert(ns_cpu_context);
33977199df7SVarun Wadekar 
34077199df7SVarun Wadekar 		/*
34177199df7SVarun Wadekar 		 * This is a request completion SMC and we must switch to
34277199df7SVarun Wadekar 		 * the non-secure world to pass the result.
34377199df7SVarun Wadekar 		 */
34477199df7SVarun Wadekar 		cm_el1_sysregs_context_save(SECURE);
34577199df7SVarun Wadekar 
34677199df7SVarun Wadekar 		/*
34777199df7SVarun Wadekar 		 * We are done stashing the secure context. Switch to the
34877199df7SVarun Wadekar 		 * non-secure context and return the result.
34977199df7SVarun Wadekar 		 */
35077199df7SVarun Wadekar 		cm_el1_sysregs_context_restore(NON_SECURE);
35177199df7SVarun Wadekar 		cm_set_next_eret_context(NON_SECURE);
352709a3c47SVarun Wadekar 		SMC_RET1(ns_cpu_context, x1);
35377199df7SVarun Wadekar 
35477199df7SVarun Wadekar 	/*
35522038315SVarun Wadekar 	 * This function ID is used only by the SP to indicate it has
35622038315SVarun Wadekar 	 * finished initialising itself after a cold boot
35722038315SVarun Wadekar 	 */
35822038315SVarun Wadekar 	case TLK_ENTRY_DONE:
359709a3c47SVarun Wadekar 		if (ns)
36022038315SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
36122038315SVarun Wadekar 
36222038315SVarun Wadekar 		/*
36322038315SVarun Wadekar 		 * SP has been successfully initialized. Register power
36422038315SVarun Wadekar 		 * managemnt hooks with PSCI
36522038315SVarun Wadekar 		 */
36622038315SVarun Wadekar 		psci_register_spd_pm_hook(&tlkd_pm_ops);
36722038315SVarun Wadekar 
36822038315SVarun Wadekar 		/*
36922038315SVarun Wadekar 		 * TLK reports completion. The SPD must have initiated
37022038315SVarun Wadekar 		 * the original request through a synchronous entry
37122038315SVarun Wadekar 		 * into the SP. Jump back to the original C runtime
37222038315SVarun Wadekar 		 * context.
37322038315SVarun Wadekar 		 */
374709a3c47SVarun Wadekar 		tlkd_synchronous_sp_exit(&tlk_ctx, x1);
37522038315SVarun Wadekar 
37622038315SVarun Wadekar 	/*
377cb790c5eSVarun Wadekar 	 * These function IDs are used only by TLK to indicate it has
378cb790c5eSVarun Wadekar 	 * finished:
379cb790c5eSVarun Wadekar 	 * 1. suspending itself after an earlier psci cpu_suspend
380cb790c5eSVarun Wadekar 	 *    request.
381cb790c5eSVarun Wadekar 	 * 2. resuming itself after an earlier psci cpu_suspend
382cb790c5eSVarun Wadekar 	 *    request.
383cb790c5eSVarun Wadekar 	 * 3. powering down after an earlier psci system_off/system_reset
384cb790c5eSVarun Wadekar 	 *    request.
385cb790c5eSVarun Wadekar 	 */
386cb790c5eSVarun Wadekar 	case TLK_SUSPEND_DONE:
387cb790c5eSVarun Wadekar 	case TLK_RESUME_DONE:
388cb790c5eSVarun Wadekar 	case TLK_SYSTEM_OFF_DONE:
389cb790c5eSVarun Wadekar 
390cb790c5eSVarun Wadekar 		if (ns)
391cb790c5eSVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
392cb790c5eSVarun Wadekar 
393cb790c5eSVarun Wadekar 		/*
394cb790c5eSVarun Wadekar 		 * TLK reports completion. TLKD must have initiated the
395cb790c5eSVarun Wadekar 		 * original request through a synchronous entry into the SP.
396cb790c5eSVarun Wadekar 		 * Jump back to the original C runtime context, and pass x1 as
397cb790c5eSVarun Wadekar 		 * return value to the caller
398cb790c5eSVarun Wadekar 		 */
399cb790c5eSVarun Wadekar 		tlkd_synchronous_sp_exit(&tlk_ctx, x1);
400cb790c5eSVarun Wadekar 
401cb790c5eSVarun Wadekar 	/*
40222038315SVarun Wadekar 	 * Return the number of service function IDs implemented to
40322038315SVarun Wadekar 	 * provide service to non-secure
40422038315SVarun Wadekar 	 */
40522038315SVarun Wadekar 	case TOS_CALL_COUNT:
40622038315SVarun Wadekar 		SMC_RET1(handle, TLK_NUM_FID);
40722038315SVarun Wadekar 
40822038315SVarun Wadekar 	/*
40922038315SVarun Wadekar 	 * Return TLK's UID to the caller
41022038315SVarun Wadekar 	 */
41122038315SVarun Wadekar 	case TOS_UID:
41222038315SVarun Wadekar 		SMC_UUID_RET(handle, tlk_uuid);
41322038315SVarun Wadekar 
41422038315SVarun Wadekar 	/*
41522038315SVarun Wadekar 	 * Return the version of current implementation
41622038315SVarun Wadekar 	 */
41722038315SVarun Wadekar 	case TOS_CALL_VERSION:
41822038315SVarun Wadekar 		SMC_RET2(handle, TLK_VERSION_MAJOR, TLK_VERSION_MINOR);
41922038315SVarun Wadekar 
42022038315SVarun Wadekar 	default:
42122038315SVarun Wadekar 		break;
42222038315SVarun Wadekar 	}
42322038315SVarun Wadekar 
42422038315SVarun Wadekar 	SMC_RET1(handle, SMC_UNK);
42522038315SVarun Wadekar }
42622038315SVarun Wadekar 
42722038315SVarun Wadekar /* Define a SPD runtime service descriptor for fast SMC calls */
42822038315SVarun Wadekar DECLARE_RT_SVC(
42922038315SVarun Wadekar 	tlkd_tos_fast,
43022038315SVarun Wadekar 
43122038315SVarun Wadekar 	OEN_TOS_START,
43222038315SVarun Wadekar 	OEN_TOS_END,
43322038315SVarun Wadekar 	SMC_TYPE_FAST,
43422038315SVarun Wadekar 	tlkd_setup,
43522038315SVarun Wadekar 	tlkd_smc_handler
43622038315SVarun Wadekar );
43722038315SVarun Wadekar 
43822038315SVarun Wadekar /* Define a SPD runtime service descriptor for standard SMC calls */
43922038315SVarun Wadekar DECLARE_RT_SVC(
44022038315SVarun Wadekar 	tlkd_tos_std,
44122038315SVarun Wadekar 
44222038315SVarun Wadekar 	OEN_TOS_START,
44322038315SVarun Wadekar 	OEN_TOS_END,
44422038315SVarun Wadekar 	SMC_TYPE_STD,
44522038315SVarun Wadekar 	NULL,
44622038315SVarun Wadekar 	tlkd_smc_handler
44722038315SVarun Wadekar );
4486693962cSVarun Wadekar 
4496693962cSVarun Wadekar /* Define a SPD runtime service descriptor for fast SMC calls */
4506693962cSVarun Wadekar DECLARE_RT_SVC(
4516693962cSVarun Wadekar 	tlkd_tap_fast,
4526693962cSVarun Wadekar 
4536693962cSVarun Wadekar 	OEN_TAP_START,
4546693962cSVarun Wadekar 	OEN_TAP_END,
4556693962cSVarun Wadekar 	SMC_TYPE_FAST,
4566693962cSVarun Wadekar 	NULL,
4576693962cSVarun Wadekar 	tlkd_smc_handler
4586693962cSVarun Wadekar );
4596693962cSVarun Wadekar 
4606693962cSVarun Wadekar /* Define a SPD runtime service descriptor for standard SMC calls */
4616693962cSVarun Wadekar DECLARE_RT_SVC(
4626693962cSVarun Wadekar 	tlkd_tap_std,
4636693962cSVarun Wadekar 
4646693962cSVarun Wadekar 	OEN_TAP_START,
4656693962cSVarun Wadekar 	OEN_TAP_END,
4666693962cSVarun Wadekar 	SMC_TYPE_STD,
4676693962cSVarun Wadekar 	NULL,
4686693962cSVarun Wadekar 	tlkd_smc_handler
4696693962cSVarun Wadekar );
470