xref: /rk3399_ARM-atf/services/spd/tlkd/tlkd_main.c (revision b29c1b0030996977fd20a1dc5fb244eac526b6e8)
122038315SVarun Wadekar /*
28aabea33SPaul Beesley  * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
322038315SVarun Wadekar  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
522038315SVarun Wadekar  */
622038315SVarun Wadekar 
722038315SVarun Wadekar /*******************************************************************************
822038315SVarun Wadekar  * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a
922038315SVarun Wadekar  * plug-in component to the Secure Monitor, registered as a runtime service. The
1022038315SVarun Wadekar  * SPD is expected to be a functional extension of the Secure Payload (SP) that
1122038315SVarun Wadekar  * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting
1222038315SVarun Wadekar  * the Trusted OS/Applications range to the dispatcher. The SPD will either
1322038315SVarun Wadekar  * handle the request locally or delegate it to the Secure Payload. It is also
1422038315SVarun Wadekar  * responsible for initialising and maintaining communication with the SP.
1522038315SVarun Wadekar  ******************************************************************************/
1622038315SVarun Wadekar #include <assert.h>
1722038315SVarun Wadekar #include <errno.h>
1822038315SVarun Wadekar #include <stddef.h>
1909d40e0eSAntonio Nino Diaz 
20*b29c1b00SAntonio Nino Diaz #include <arch_helpers.h>
2109d40e0eSAntonio Nino Diaz #include <bl31/bl31.h>
22*b29c1b00SAntonio Nino Diaz #include <bl32/payloads/tlk.h>
2309d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
2409d40e0eSAntonio Nino Diaz #include <common/debug.h>
2509d40e0eSAntonio Nino Diaz #include <common/runtime_svc.h>
2609d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h>
2709d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
2809d40e0eSAntonio Nino Diaz #include <tools_share/uuid.h>
2909d40e0eSAntonio Nino Diaz 
3022038315SVarun Wadekar #include "tlkd_private.h"
3122038315SVarun Wadekar 
3222038315SVarun Wadekar extern const spd_pm_ops_t tlkd_pm_ops;
3322038315SVarun Wadekar 
3422038315SVarun Wadekar /*******************************************************************************
35cb790c5eSVarun Wadekar  * Per-cpu Secure Payload state
3622038315SVarun Wadekar  ******************************************************************************/
37cb790c5eSVarun Wadekar tlk_context_t tlk_ctx;
3822038315SVarun Wadekar 
3926670c82SVarun Wadekar /*******************************************************************************
4026670c82SVarun Wadekar  * CPU number on which TLK booted up
4126670c82SVarun Wadekar  ******************************************************************************/
426311f63dSVarun Wadekar static uint32_t boot_cpu;
4326670c82SVarun Wadekar 
4422038315SVarun Wadekar /* TLK UID: RFC-4122 compliant UUID (version-5, sha-1) */
4503364865SRoberto Vargas DEFINE_SVC_UUID2(tlk_uuid,
4603364865SRoberto Vargas 	0xc9e911bd, 0xba2b, 0xee52, 0xb1, 0x72,
4722038315SVarun Wadekar 	0x46, 0x1f, 0xba, 0x97, 0x7f, 0x63);
4822038315SVarun Wadekar 
49724fd958SMasahiro Yamada static int32_t tlkd_init(void);
5022038315SVarun Wadekar 
5122038315SVarun Wadekar /*******************************************************************************
5222038315SVarun Wadekar  * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type
5322038315SVarun Wadekar  * (aarch32/aarch64) if not already known and initialises the context for entry
5422038315SVarun Wadekar  * into the SP for its initialisation.
5522038315SVarun Wadekar  ******************************************************************************/
56724fd958SMasahiro Yamada static int32_t tlkd_setup(void)
5722038315SVarun Wadekar {
5822038315SVarun Wadekar 	entry_point_info_t *tlk_ep_info;
5922038315SVarun Wadekar 
6022038315SVarun Wadekar 	/*
6122038315SVarun Wadekar 	 * Get information about the Secure Payload (BL32) image. Its
6222038315SVarun Wadekar 	 * absence is a critical failure.
6322038315SVarun Wadekar 	 */
6422038315SVarun Wadekar 	tlk_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
6522038315SVarun Wadekar 	if (!tlk_ep_info) {
6622038315SVarun Wadekar 		WARN("No SP provided. Booting device without SP"
6722038315SVarun Wadekar 			" initialization. SMC`s destined for SP"
6822038315SVarun Wadekar 			" will return SMC_UNK\n");
6922038315SVarun Wadekar 		return 1;
7022038315SVarun Wadekar 	}
7122038315SVarun Wadekar 
7222038315SVarun Wadekar 	/*
7322038315SVarun Wadekar 	 * If there's no valid entry point for SP, we return a non-zero value
7422038315SVarun Wadekar 	 * signalling failure initializing the service. We bail out without
7522038315SVarun Wadekar 	 * registering any handlers
7622038315SVarun Wadekar 	 */
7722038315SVarun Wadekar 	if (!tlk_ep_info->pc)
7822038315SVarun Wadekar 		return 1;
7922038315SVarun Wadekar 
8022038315SVarun Wadekar 	/*
8122038315SVarun Wadekar 	 * Inspect the SP image's SPSR and determine it's execution state
8222038315SVarun Wadekar 	 * i.e whether AArch32 or AArch64.
8322038315SVarun Wadekar 	 */
8422038315SVarun Wadekar 	tlkd_init_tlk_ep_state(tlk_ep_info,
8522038315SVarun Wadekar 		(tlk_ep_info->spsr >> MODE_RW_SHIFT) & MODE_RW_MASK,
8622038315SVarun Wadekar 		tlk_ep_info->pc,
8722038315SVarun Wadekar 		&tlk_ctx);
8822038315SVarun Wadekar 
8922038315SVarun Wadekar 	/*
9022038315SVarun Wadekar 	 * All TLK SPD initialization done. Now register our init function
9122038315SVarun Wadekar 	 * with BL31 for deferred invocation
9222038315SVarun Wadekar 	 */
9322038315SVarun Wadekar 	bl31_register_bl32_init(&tlkd_init);
9422038315SVarun Wadekar 
9522038315SVarun Wadekar 	return 0;
9622038315SVarun Wadekar }
9722038315SVarun Wadekar 
9822038315SVarun Wadekar /*******************************************************************************
9922038315SVarun Wadekar  * This function passes control to the Secure Payload image (BL32) for the first
10022038315SVarun Wadekar  * time on the primary cpu after a cold boot. It assumes that a valid secure
10122038315SVarun Wadekar  * context has already been created by tlkd_setup() which can be directly
10222038315SVarun Wadekar  * used. This function performs a synchronous entry into the Secure payload.
10322038315SVarun Wadekar  * The SP passes control back to this routine through a SMC.
10422038315SVarun Wadekar  ******************************************************************************/
105724fd958SMasahiro Yamada static int32_t tlkd_init(void)
10622038315SVarun Wadekar {
10722038315SVarun Wadekar 	entry_point_info_t *tlk_entry_point;
10822038315SVarun Wadekar 
10922038315SVarun Wadekar 	/*
11022038315SVarun Wadekar 	 * Get information about the Secure Payload (BL32) image. Its
11122038315SVarun Wadekar 	 * absence is a critical failure.
11222038315SVarun Wadekar 	 */
11322038315SVarun Wadekar 	tlk_entry_point = bl31_plat_get_next_image_ep_info(SECURE);
11422038315SVarun Wadekar 	assert(tlk_entry_point);
11522038315SVarun Wadekar 
116fd650ff6SSoby Mathew 	cm_init_my_context(tlk_entry_point);
11722038315SVarun Wadekar 
11822038315SVarun Wadekar 	/*
11926670c82SVarun Wadekar 	 * TLK runs only on a single CPU. Store the value of the boot
12026670c82SVarun Wadekar 	 * CPU for sanity checking later.
12126670c82SVarun Wadekar 	 */
12226670c82SVarun Wadekar 	boot_cpu = plat_my_core_pos();
12326670c82SVarun Wadekar 
12426670c82SVarun Wadekar 	/*
12522038315SVarun Wadekar 	 * Arrange for an entry into the test secure payload.
12622038315SVarun Wadekar 	 */
12722038315SVarun Wadekar 	return tlkd_synchronous_sp_entry(&tlk_ctx);
12822038315SVarun Wadekar }
12922038315SVarun Wadekar 
13022038315SVarun Wadekar /*******************************************************************************
13122038315SVarun Wadekar  * This function is responsible for handling all SMCs in the Trusted OS/App
13222038315SVarun Wadekar  * range from the non-secure state as defined in the SMC Calling Convention
13322038315SVarun Wadekar  * Document. It is also responsible for communicating with the Secure payload
13422038315SVarun Wadekar  * to delegate work and return results back to the non-secure state. Lastly it
13522038315SVarun Wadekar  * will also return any information that the secure payload needs to do the
13622038315SVarun Wadekar  * work assigned to it.
13722038315SVarun Wadekar  ******************************************************************************/
13857d1e5faSMasahiro Yamada static uintptr_t tlkd_smc_handler(uint32_t smc_fid,
13957d1e5faSMasahiro Yamada 			 u_register_t x1,
14057d1e5faSMasahiro Yamada 			 u_register_t x2,
14157d1e5faSMasahiro Yamada 			 u_register_t x3,
14257d1e5faSMasahiro Yamada 			 u_register_t x4,
14322038315SVarun Wadekar 			 void *cookie,
14422038315SVarun Wadekar 			 void *handle,
14557d1e5faSMasahiro Yamada 			 u_register_t flags)
14622038315SVarun Wadekar {
14777199df7SVarun Wadekar 	cpu_context_t *ns_cpu_context;
148709a3c47SVarun Wadekar 	gp_regs_t *gp_regs;
14922038315SVarun Wadekar 	uint32_t ns;
150709a3c47SVarun Wadekar 	uint64_t par;
15122038315SVarun Wadekar 
15222038315SVarun Wadekar 	/* Passing a NULL context is a critical programming error */
15322038315SVarun Wadekar 	assert(handle);
15422038315SVarun Wadekar 
15526670c82SVarun Wadekar 	/* These SMCs are only supported by a single CPU */
15626670c82SVarun Wadekar 	if (boot_cpu != plat_my_core_pos())
1576693962cSVarun Wadekar 		SMC_RET1(handle, SMC_UNK);
1586693962cSVarun Wadekar 
15922038315SVarun Wadekar 	/* Determine which security state this SMC originated from */
16022038315SVarun Wadekar 	ns = is_caller_non_secure(flags);
16122038315SVarun Wadekar 
16222038315SVarun Wadekar 	switch (smc_fid) {
16322038315SVarun Wadekar 
16422038315SVarun Wadekar 	/*
165f9d25054SVarun Wadekar 	 * This function ID is used by SP to indicate that it was
166f9d25054SVarun Wadekar 	 * preempted by a non-secure world IRQ.
167f9d25054SVarun Wadekar 	 */
168f9d25054SVarun Wadekar 	case TLK_PREEMPTED:
169f9d25054SVarun Wadekar 
170f9d25054SVarun Wadekar 		if (ns)
171f9d25054SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
172f9d25054SVarun Wadekar 
173f9d25054SVarun Wadekar 		assert(handle == cm_get_context(SECURE));
174f9d25054SVarun Wadekar 		cm_el1_sysregs_context_save(SECURE);
175f9d25054SVarun Wadekar 
176f9d25054SVarun Wadekar 		/* Get a reference to the non-secure context */
177f9d25054SVarun Wadekar 		ns_cpu_context = cm_get_context(NON_SECURE);
178f9d25054SVarun Wadekar 		assert(ns_cpu_context);
179f9d25054SVarun Wadekar 
180f9d25054SVarun Wadekar 		/*
181f9d25054SVarun Wadekar 		 * Restore non-secure state. There is no need to save the
182f9d25054SVarun Wadekar 		 * secure system register context since the SP was supposed
183f9d25054SVarun Wadekar 		 * to preserve it during S-EL1 interrupt handling.
184f9d25054SVarun Wadekar 		 */
185f9d25054SVarun Wadekar 		cm_el1_sysregs_context_restore(NON_SECURE);
186f9d25054SVarun Wadekar 		cm_set_next_eret_context(NON_SECURE);
187f9d25054SVarun Wadekar 
188709a3c47SVarun Wadekar 		SMC_RET1(ns_cpu_context, x1);
189f9d25054SVarun Wadekar 
190f9d25054SVarun Wadekar 	/*
19177199df7SVarun Wadekar 	 * This is a request from the non-secure context to:
19277199df7SVarun Wadekar 	 *
19377199df7SVarun Wadekar 	 * a. register shared memory with the SP for storing it's
19477199df7SVarun Wadekar 	 *    activity logs.
19577199df7SVarun Wadekar 	 * b. register shared memory with the SP for passing args
19677199df7SVarun Wadekar 	 *    required for maintaining sessions with the Trusted
19777199df7SVarun Wadekar 	 *    Applications.
1987bc05f52SMihir Joshi 	 * c. register shared persistent buffers for secure storage
1997bc05f52SMihir Joshi 	 * d. register NS DRAM ranges passed by Cboot
2007bc05f52SMihir Joshi 	 * e. register Root of Trust parameters from Cboot for Verified Boot
2017bc05f52SMihir Joshi 	 * f. open/close sessions
2027bc05f52SMihir Joshi 	 * g. issue commands to the Trusted Apps
2037bc05f52SMihir Joshi 	 * h. resume the preempted yielding SMC call.
20477199df7SVarun Wadekar 	 */
20577199df7SVarun Wadekar 	case TLK_REGISTER_LOGBUF:
20677199df7SVarun Wadekar 	case TLK_REGISTER_REQBUF:
2077bc05f52SMihir Joshi 	case TLK_SS_REGISTER_HANDLER:
2087bc05f52SMihir Joshi 	case TLK_REGISTER_NS_DRAM_RANGES:
2097bc05f52SMihir Joshi 	case TLK_SET_ROOT_OF_TRUST:
2106693962cSVarun Wadekar 	case TLK_OPEN_TA_SESSION:
2116693962cSVarun Wadekar 	case TLK_CLOSE_TA_SESSION:
2126693962cSVarun Wadekar 	case TLK_TA_LAUNCH_OP:
2136693962cSVarun Wadekar 	case TLK_TA_SEND_EVENT:
214ca15d9bcSVarun Wadekar 	case TLK_RESUME_FID:
2156693962cSVarun Wadekar 
216709a3c47SVarun Wadekar 		if (!ns)
21777199df7SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
21877199df7SVarun Wadekar 
21977199df7SVarun Wadekar 		/*
22077199df7SVarun Wadekar 		 * This is a fresh request from the non-secure client.
22177199df7SVarun Wadekar 		 * The parameters are in x1 and x2. Figure out which
22277199df7SVarun Wadekar 		 * registers need to be preserved, save the non-secure
22377199df7SVarun Wadekar 		 * state and send the request to the secure payload.
22477199df7SVarun Wadekar 		 */
22577199df7SVarun Wadekar 		assert(handle == cm_get_context(NON_SECURE));
22677199df7SVarun Wadekar 
227ca15d9bcSVarun Wadekar 		/*
228bbbbcdaeSDavid Cunado 		 * Check if we are already processing a yielding SMC
229ca15d9bcSVarun Wadekar 		 * call. Of all the supported fids, only the "resume"
230ca15d9bcSVarun Wadekar 		 * fid expects the flag to be set.
231ca15d9bcSVarun Wadekar 		 */
232ca15d9bcSVarun Wadekar 		if (smc_fid == TLK_RESUME_FID) {
233bbbbcdaeSDavid Cunado 			if (!get_yield_smc_active_flag(tlk_ctx.state))
234ca15d9bcSVarun Wadekar 				SMC_RET1(handle, SMC_UNK);
235ca15d9bcSVarun Wadekar 		} else {
236bbbbcdaeSDavid Cunado 			if (get_yield_smc_active_flag(tlk_ctx.state))
23777199df7SVarun Wadekar 				SMC_RET1(handle, SMC_UNK);
238ca15d9bcSVarun Wadekar 		}
23977199df7SVarun Wadekar 
24077199df7SVarun Wadekar 		cm_el1_sysregs_context_save(NON_SECURE);
24177199df7SVarun Wadekar 
24277199df7SVarun Wadekar 		/*
24377199df7SVarun Wadekar 		 * Verify if there is a valid context to use.
24477199df7SVarun Wadekar 		 */
24577199df7SVarun Wadekar 		assert(&tlk_ctx.cpu_ctx == cm_get_context(SECURE));
24677199df7SVarun Wadekar 
24777199df7SVarun Wadekar 		/*
24877199df7SVarun Wadekar 		 * Mark the SP state as active.
24977199df7SVarun Wadekar 		 */
250bbbbcdaeSDavid Cunado 		set_yield_smc_active_flag(tlk_ctx.state);
25177199df7SVarun Wadekar 
25277199df7SVarun Wadekar 		/*
25377199df7SVarun Wadekar 		 * We are done stashing the non-secure context. Ask the
25477199df7SVarun Wadekar 		 * secure payload to do the work now.
25577199df7SVarun Wadekar 		 */
25677199df7SVarun Wadekar 		cm_el1_sysregs_context_restore(SECURE);
25777199df7SVarun Wadekar 		cm_set_next_eret_context(SECURE);
25877199df7SVarun Wadekar 
25977199df7SVarun Wadekar 		/*
260709a3c47SVarun Wadekar 		 * TLK is a 32-bit Trusted OS and so expects the SMC
261709a3c47SVarun Wadekar 		 * arguments via r0-r7. TLK expects the monitor frame
262709a3c47SVarun Wadekar 		 * registers to be 64-bits long. Hence, we pass x0 in
263709a3c47SVarun Wadekar 		 * r0-r1, x1 in r2-r3, x3 in r4-r5 and x4 in r6-r7.
264709a3c47SVarun Wadekar 		 *
265709a3c47SVarun Wadekar 		 * As smc_fid is a uint32 value, r1 contains 0.
266709a3c47SVarun Wadekar 		 */
267709a3c47SVarun Wadekar 		gp_regs = get_gpregs_ctx(&tlk_ctx.cpu_ctx);
268709a3c47SVarun Wadekar 		write_ctx_reg(gp_regs, CTX_GPREG_X4, (uint32_t)x2);
269709a3c47SVarun Wadekar 		write_ctx_reg(gp_regs, CTX_GPREG_X5, (uint32_t)(x2 >> 32));
270709a3c47SVarun Wadekar 		write_ctx_reg(gp_regs, CTX_GPREG_X6, (uint32_t)x3);
271709a3c47SVarun Wadekar 		write_ctx_reg(gp_regs, CTX_GPREG_X7, (uint32_t)(x3 >> 32));
272709a3c47SVarun Wadekar 		SMC_RET4(&tlk_ctx.cpu_ctx, smc_fid, 0, (uint32_t)x1,
273709a3c47SVarun Wadekar 			(uint32_t)(x1 >> 32));
274709a3c47SVarun Wadekar 
275709a3c47SVarun Wadekar 	/*
276709a3c47SVarun Wadekar 	 * Translate NS/EL1-S virtual addresses.
277709a3c47SVarun Wadekar 	 *
278709a3c47SVarun Wadekar 	 * x1 = virtual address
279709a3c47SVarun Wadekar 	 * x3 = type (NS/S)
280709a3c47SVarun Wadekar 	 *
281709a3c47SVarun Wadekar 	 * Returns PA:lo in r0, PA:hi in r1.
2826e159e7aSVarun Wadekar 	 */
2836e159e7aSVarun Wadekar 	case TLK_VA_TRANSLATE:
284709a3c47SVarun Wadekar 
285709a3c47SVarun Wadekar 		/* Should be invoked only by secure world */
286709a3c47SVarun Wadekar 		if (ns)
2876e159e7aSVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
2886e159e7aSVarun Wadekar 
289709a3c47SVarun Wadekar 		/* NS virtual addresses are 64-bit long */
290709a3c47SVarun Wadekar 		if (x3 & TLK_TRANSLATE_NS_VADDR)
291709a3c47SVarun Wadekar 			x1 = (uint32_t)x1 | (x2 << 32);
292709a3c47SVarun Wadekar 
293709a3c47SVarun Wadekar 		if (!x1)
294709a3c47SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
295709a3c47SVarun Wadekar 
296709a3c47SVarun Wadekar 		/*
297709a3c47SVarun Wadekar 		 * TODO: Sanity check x1. This would require platform
298709a3c47SVarun Wadekar 		 * support.
299709a3c47SVarun Wadekar 		 */
300709a3c47SVarun Wadekar 
3016e159e7aSVarun Wadekar 		/* virtual address and type: ns/s */
302709a3c47SVarun Wadekar 		par = tlkd_va_translate(x1, x3);
3036e159e7aSVarun Wadekar 
304709a3c47SVarun Wadekar 		/* return physical address in r0-r1 */
305709a3c47SVarun Wadekar 		SMC_RET4(handle, (uint32_t)par, (uint32_t)(par >> 32), 0, 0);
3066e159e7aSVarun Wadekar 
3076e159e7aSVarun Wadekar 	/*
30877199df7SVarun Wadekar 	 * This is a request from the SP to mark completion of
309bbbbcdaeSDavid Cunado 	 * a yielding function ID.
31077199df7SVarun Wadekar 	 */
31177199df7SVarun Wadekar 	case TLK_REQUEST_DONE:
312709a3c47SVarun Wadekar 		if (ns)
31377199df7SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
31477199df7SVarun Wadekar 
31577199df7SVarun Wadekar 		/*
31677199df7SVarun Wadekar 		 * Mark the SP state as inactive.
31777199df7SVarun Wadekar 		 */
318bbbbcdaeSDavid Cunado 		clr_yield_smc_active_flag(tlk_ctx.state);
31977199df7SVarun Wadekar 
32077199df7SVarun Wadekar 		/* Get a reference to the non-secure context */
32177199df7SVarun Wadekar 		ns_cpu_context = cm_get_context(NON_SECURE);
32277199df7SVarun Wadekar 		assert(ns_cpu_context);
32377199df7SVarun Wadekar 
32477199df7SVarun Wadekar 		/*
32577199df7SVarun Wadekar 		 * This is a request completion SMC and we must switch to
32677199df7SVarun Wadekar 		 * the non-secure world to pass the result.
32777199df7SVarun Wadekar 		 */
32877199df7SVarun Wadekar 		cm_el1_sysregs_context_save(SECURE);
32977199df7SVarun Wadekar 
33077199df7SVarun Wadekar 		/*
33177199df7SVarun Wadekar 		 * We are done stashing the secure context. Switch to the
33277199df7SVarun Wadekar 		 * non-secure context and return the result.
33377199df7SVarun Wadekar 		 */
33477199df7SVarun Wadekar 		cm_el1_sysregs_context_restore(NON_SECURE);
33577199df7SVarun Wadekar 		cm_set_next_eret_context(NON_SECURE);
336709a3c47SVarun Wadekar 		SMC_RET1(ns_cpu_context, x1);
33777199df7SVarun Wadekar 
33877199df7SVarun Wadekar 	/*
33922038315SVarun Wadekar 	 * This function ID is used only by the SP to indicate it has
34022038315SVarun Wadekar 	 * finished initialising itself after a cold boot
34122038315SVarun Wadekar 	 */
34222038315SVarun Wadekar 	case TLK_ENTRY_DONE:
343709a3c47SVarun Wadekar 		if (ns)
34422038315SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
34522038315SVarun Wadekar 
34622038315SVarun Wadekar 		/*
34722038315SVarun Wadekar 		 * SP has been successfully initialized. Register power
3488aabea33SPaul Beesley 		 * management hooks with PSCI
34922038315SVarun Wadekar 		 */
35022038315SVarun Wadekar 		psci_register_spd_pm_hook(&tlkd_pm_ops);
35122038315SVarun Wadekar 
35222038315SVarun Wadekar 		/*
35322038315SVarun Wadekar 		 * TLK reports completion. The SPD must have initiated
35422038315SVarun Wadekar 		 * the original request through a synchronous entry
35522038315SVarun Wadekar 		 * into the SP. Jump back to the original C runtime
35622038315SVarun Wadekar 		 * context.
35722038315SVarun Wadekar 		 */
358709a3c47SVarun Wadekar 		tlkd_synchronous_sp_exit(&tlk_ctx, x1);
359185a23ffSJonathan Wright 		break;
36022038315SVarun Wadekar 
36122038315SVarun Wadekar 	/*
362cb790c5eSVarun Wadekar 	 * These function IDs are used only by TLK to indicate it has
363cb790c5eSVarun Wadekar 	 * finished:
364cb790c5eSVarun Wadekar 	 * 1. suspending itself after an earlier psci cpu_suspend
365cb790c5eSVarun Wadekar 	 *    request.
366cb790c5eSVarun Wadekar 	 * 2. resuming itself after an earlier psci cpu_suspend
367cb790c5eSVarun Wadekar 	 *    request.
368cb790c5eSVarun Wadekar 	 * 3. powering down after an earlier psci system_off/system_reset
369cb790c5eSVarun Wadekar 	 *    request.
370cb790c5eSVarun Wadekar 	 */
371cb790c5eSVarun Wadekar 	case TLK_SUSPEND_DONE:
372cb790c5eSVarun Wadekar 	case TLK_RESUME_DONE:
373cb790c5eSVarun Wadekar 	case TLK_SYSTEM_OFF_DONE:
374cb790c5eSVarun Wadekar 
375cb790c5eSVarun Wadekar 		if (ns)
376cb790c5eSVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
377cb790c5eSVarun Wadekar 
378cb790c5eSVarun Wadekar 		/*
379cb790c5eSVarun Wadekar 		 * TLK reports completion. TLKD must have initiated the
380cb790c5eSVarun Wadekar 		 * original request through a synchronous entry into the SP.
381cb790c5eSVarun Wadekar 		 * Jump back to the original C runtime context, and pass x1 as
382cb790c5eSVarun Wadekar 		 * return value to the caller
383cb790c5eSVarun Wadekar 		 */
384cb790c5eSVarun Wadekar 		tlkd_synchronous_sp_exit(&tlk_ctx, x1);
385185a23ffSJonathan Wright 		break;
386cb790c5eSVarun Wadekar 
387cb790c5eSVarun Wadekar 	/*
38822038315SVarun Wadekar 	 * Return the number of service function IDs implemented to
38922038315SVarun Wadekar 	 * provide service to non-secure
39022038315SVarun Wadekar 	 */
39122038315SVarun Wadekar 	case TOS_CALL_COUNT:
39222038315SVarun Wadekar 		SMC_RET1(handle, TLK_NUM_FID);
39322038315SVarun Wadekar 
39422038315SVarun Wadekar 	/*
39522038315SVarun Wadekar 	 * Return TLK's UID to the caller
39622038315SVarun Wadekar 	 */
39722038315SVarun Wadekar 	case TOS_UID:
39822038315SVarun Wadekar 		SMC_UUID_RET(handle, tlk_uuid);
39922038315SVarun Wadekar 
40022038315SVarun Wadekar 	/*
40122038315SVarun Wadekar 	 * Return the version of current implementation
40222038315SVarun Wadekar 	 */
40322038315SVarun Wadekar 	case TOS_CALL_VERSION:
40422038315SVarun Wadekar 		SMC_RET2(handle, TLK_VERSION_MAJOR, TLK_VERSION_MINOR);
40522038315SVarun Wadekar 
40622038315SVarun Wadekar 	default:
4077bc05f52SMihir Joshi 		WARN("%s: Unhandled SMC: 0x%x\n", __func__, smc_fid);
40822038315SVarun Wadekar 		break;
40922038315SVarun Wadekar 	}
41022038315SVarun Wadekar 
41122038315SVarun Wadekar 	SMC_RET1(handle, SMC_UNK);
41222038315SVarun Wadekar }
41322038315SVarun Wadekar 
41422038315SVarun Wadekar /* Define a SPD runtime service descriptor for fast SMC calls */
41522038315SVarun Wadekar DECLARE_RT_SVC(
41622038315SVarun Wadekar 	tlkd_tos_fast,
41722038315SVarun Wadekar 
41822038315SVarun Wadekar 	OEN_TOS_START,
41922038315SVarun Wadekar 	OEN_TOS_END,
42022038315SVarun Wadekar 	SMC_TYPE_FAST,
42122038315SVarun Wadekar 	tlkd_setup,
42222038315SVarun Wadekar 	tlkd_smc_handler
42322038315SVarun Wadekar );
42422038315SVarun Wadekar 
425bbbbcdaeSDavid Cunado /* Define a SPD runtime service descriptor for yielding SMC calls */
42622038315SVarun Wadekar DECLARE_RT_SVC(
42722038315SVarun Wadekar 	tlkd_tos_std,
42822038315SVarun Wadekar 
42922038315SVarun Wadekar 	OEN_TOS_START,
43022038315SVarun Wadekar 	OEN_TOS_END,
431bbbbcdaeSDavid Cunado 	SMC_TYPE_YIELD,
43222038315SVarun Wadekar 	NULL,
43322038315SVarun Wadekar 	tlkd_smc_handler
43422038315SVarun Wadekar );
4356693962cSVarun Wadekar 
4366693962cSVarun Wadekar /* Define a SPD runtime service descriptor for fast SMC calls */
4376693962cSVarun Wadekar DECLARE_RT_SVC(
4386693962cSVarun Wadekar 	tlkd_tap_fast,
4396693962cSVarun Wadekar 
4406693962cSVarun Wadekar 	OEN_TAP_START,
4416693962cSVarun Wadekar 	OEN_TAP_END,
4426693962cSVarun Wadekar 	SMC_TYPE_FAST,
4436693962cSVarun Wadekar 	NULL,
4446693962cSVarun Wadekar 	tlkd_smc_handler
4456693962cSVarun Wadekar );
4466693962cSVarun Wadekar 
447bbbbcdaeSDavid Cunado /* Define a SPD runtime service descriptor for yielding SMC calls */
4486693962cSVarun Wadekar DECLARE_RT_SVC(
4496693962cSVarun Wadekar 	tlkd_tap_std,
4506693962cSVarun Wadekar 
4516693962cSVarun Wadekar 	OEN_TAP_START,
4526693962cSVarun Wadekar 	OEN_TAP_END,
453bbbbcdaeSDavid Cunado 	SMC_TYPE_YIELD,
4546693962cSVarun Wadekar 	NULL,
4556693962cSVarun Wadekar 	tlkd_smc_handler
4566693962cSVarun Wadekar );
457