xref: /rk3399_ARM-atf/services/spd/tlkd/tlkd_main.c (revision 0600cf63006688e221e183f1fa480c6ec5975ceb)
122038315SVarun Wadekar /*
2d205cda6SVarun Wadekar  * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved.
3*0600cf63SVarun Wadekar  * Copyright (c) 2020, NVIDIA Corporation. All rights reserved.
422038315SVarun Wadekar  *
582cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
622038315SVarun Wadekar  */
722038315SVarun Wadekar 
822038315SVarun Wadekar /*******************************************************************************
922038315SVarun Wadekar  * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a
1022038315SVarun Wadekar  * plug-in component to the Secure Monitor, registered as a runtime service. The
1122038315SVarun Wadekar  * SPD is expected to be a functional extension of the Secure Payload (SP) that
1222038315SVarun Wadekar  * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting
1322038315SVarun Wadekar  * the Trusted OS/Applications range to the dispatcher. The SPD will either
1422038315SVarun Wadekar  * handle the request locally or delegate it to the Secure Payload. It is also
1522038315SVarun Wadekar  * responsible for initialising and maintaining communication with the SP.
1622038315SVarun Wadekar  ******************************************************************************/
1722038315SVarun Wadekar #include <assert.h>
18d205cda6SVarun Wadekar #include <bl31/interrupt_mgmt.h>
1922038315SVarun Wadekar #include <errno.h>
2022038315SVarun Wadekar #include <stddef.h>
2109d40e0eSAntonio Nino Diaz 
22b29c1b00SAntonio Nino Diaz #include <arch_helpers.h>
2309d40e0eSAntonio Nino Diaz #include <bl31/bl31.h>
24b29c1b00SAntonio Nino Diaz #include <bl32/payloads/tlk.h>
2509d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
2609d40e0eSAntonio Nino Diaz #include <common/debug.h>
2709d40e0eSAntonio Nino Diaz #include <common/runtime_svc.h>
2809d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h>
2909d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
3009d40e0eSAntonio Nino Diaz #include <tools_share/uuid.h>
3109d40e0eSAntonio Nino Diaz 
3222038315SVarun Wadekar #include "tlkd_private.h"
3322038315SVarun Wadekar 
3422038315SVarun Wadekar extern const spd_pm_ops_t tlkd_pm_ops;
3522038315SVarun Wadekar 
3622038315SVarun Wadekar /*******************************************************************************
37cb790c5eSVarun Wadekar  * Per-cpu Secure Payload state
3822038315SVarun Wadekar  ******************************************************************************/
39cb790c5eSVarun Wadekar tlk_context_t tlk_ctx;
4022038315SVarun Wadekar 
4126670c82SVarun Wadekar /*******************************************************************************
4226670c82SVarun Wadekar  * CPU number on which TLK booted up
4326670c82SVarun Wadekar  ******************************************************************************/
446311f63dSVarun Wadekar static uint32_t boot_cpu;
4526670c82SVarun Wadekar 
4622038315SVarun Wadekar /* TLK UID: RFC-4122 compliant UUID (version-5, sha-1) */
4703364865SRoberto Vargas DEFINE_SVC_UUID2(tlk_uuid,
4803364865SRoberto Vargas 	0xc9e911bd, 0xba2b, 0xee52, 0xb1, 0x72,
4922038315SVarun Wadekar 	0x46, 0x1f, 0xba, 0x97, 0x7f, 0x63);
5022038315SVarun Wadekar 
51724fd958SMasahiro Yamada static int32_t tlkd_init(void);
5222038315SVarun Wadekar 
5322038315SVarun Wadekar /*******************************************************************************
54d205cda6SVarun Wadekar  * Secure Payload Dispatcher's timer interrupt handler
55d205cda6SVarun Wadekar  ******************************************************************************/
56d205cda6SVarun Wadekar static uint64_t tlkd_interrupt_handler(uint32_t id,
57d205cda6SVarun Wadekar 					uint32_t flags,
58d205cda6SVarun Wadekar 					void *handle,
59d205cda6SVarun Wadekar 					void *cookie)
60d205cda6SVarun Wadekar {
61d205cda6SVarun Wadekar 	cpu_context_t *s_cpu_context;
62d205cda6SVarun Wadekar 	int irq = plat_ic_get_pending_interrupt_id();
63d205cda6SVarun Wadekar 
64d205cda6SVarun Wadekar 	/* acknowledge the interrupt and mark it complete */
65d205cda6SVarun Wadekar 	(void)plat_ic_acknowledge_interrupt();
66d205cda6SVarun Wadekar 	plat_ic_end_of_interrupt(irq);
67d205cda6SVarun Wadekar 
68d205cda6SVarun Wadekar 	/*
69d205cda6SVarun Wadekar 	 * Disable the routing of NS interrupts from secure world to
70d205cda6SVarun Wadekar 	 * EL3 while interrupted on this core.
71d205cda6SVarun Wadekar 	 */
72d205cda6SVarun Wadekar 	disable_intr_rm_local(INTR_TYPE_S_EL1, SECURE);
73d205cda6SVarun Wadekar 
74d205cda6SVarun Wadekar 	/* Check the security state when the exception was generated */
75d205cda6SVarun Wadekar 	assert(get_interrupt_src_ss(flags) == NON_SECURE);
76d205cda6SVarun Wadekar 	assert(handle == cm_get_context(NON_SECURE));
77d205cda6SVarun Wadekar 
78d205cda6SVarun Wadekar 	/* Save non-secure state */
79d205cda6SVarun Wadekar 	cm_el1_sysregs_context_save(NON_SECURE);
80d205cda6SVarun Wadekar 
81d205cda6SVarun Wadekar 	/* Get a reference to the secure context */
82d205cda6SVarun Wadekar 	s_cpu_context = cm_get_context(SECURE);
83d205cda6SVarun Wadekar 	assert(s_cpu_context);
84d205cda6SVarun Wadekar 
85d205cda6SVarun Wadekar 	/*
86d205cda6SVarun Wadekar 	 * Restore non-secure state. There is no need to save the
87d205cda6SVarun Wadekar 	 * secure system register context since the SP was supposed
88d205cda6SVarun Wadekar 	 * to preserve it during S-EL1 interrupt handling.
89d205cda6SVarun Wadekar 	 */
90d205cda6SVarun Wadekar 	cm_el1_sysregs_context_restore(SECURE);
91d205cda6SVarun Wadekar 	cm_set_next_eret_context(SECURE);
92d205cda6SVarun Wadekar 
93d205cda6SVarun Wadekar 	/* Provide the IRQ number to the SPD */
94d205cda6SVarun Wadekar 	SMC_RET4(s_cpu_context, (uint32_t)TLK_IRQ_FIRED, 0, (uint32_t)irq, 0);
95d205cda6SVarun Wadekar }
96d205cda6SVarun Wadekar 
97d205cda6SVarun Wadekar /*******************************************************************************
9822038315SVarun Wadekar  * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type
9922038315SVarun Wadekar  * (aarch32/aarch64) if not already known and initialises the context for entry
10022038315SVarun Wadekar  * into the SP for its initialisation.
10122038315SVarun Wadekar  ******************************************************************************/
102724fd958SMasahiro Yamada static int32_t tlkd_setup(void)
10322038315SVarun Wadekar {
10422038315SVarun Wadekar 	entry_point_info_t *tlk_ep_info;
105d205cda6SVarun Wadekar 	uint32_t flags;
106d205cda6SVarun Wadekar 	int32_t ret;
10722038315SVarun Wadekar 
10822038315SVarun Wadekar 	/*
10922038315SVarun Wadekar 	 * Get information about the Secure Payload (BL32) image. Its
11022038315SVarun Wadekar 	 * absence is a critical failure.
11122038315SVarun Wadekar 	 */
11222038315SVarun Wadekar 	tlk_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
11322038315SVarun Wadekar 	if (!tlk_ep_info) {
11422038315SVarun Wadekar 		WARN("No SP provided. Booting device without SP"
11522038315SVarun Wadekar 			" initialization. SMC`s destined for SP"
11622038315SVarun Wadekar 			" will return SMC_UNK\n");
11722038315SVarun Wadekar 		return 1;
11822038315SVarun Wadekar 	}
11922038315SVarun Wadekar 
12022038315SVarun Wadekar 	/*
12122038315SVarun Wadekar 	 * If there's no valid entry point for SP, we return a non-zero value
12222038315SVarun Wadekar 	 * signalling failure initializing the service. We bail out without
12322038315SVarun Wadekar 	 * registering any handlers
12422038315SVarun Wadekar 	 */
12522038315SVarun Wadekar 	if (!tlk_ep_info->pc)
12622038315SVarun Wadekar 		return 1;
12722038315SVarun Wadekar 
12822038315SVarun Wadekar 	/*
12922038315SVarun Wadekar 	 * Inspect the SP image's SPSR and determine it's execution state
13022038315SVarun Wadekar 	 * i.e whether AArch32 or AArch64.
13122038315SVarun Wadekar 	 */
13222038315SVarun Wadekar 	tlkd_init_tlk_ep_state(tlk_ep_info,
13322038315SVarun Wadekar 		(tlk_ep_info->spsr >> MODE_RW_SHIFT) & MODE_RW_MASK,
13422038315SVarun Wadekar 		tlk_ep_info->pc,
13522038315SVarun Wadekar 		&tlk_ctx);
13622038315SVarun Wadekar 
137d205cda6SVarun Wadekar 	/* get a list of all S-EL1 IRQs from the platform */
138d205cda6SVarun Wadekar 
139d205cda6SVarun Wadekar 	/* register interrupt handler */
140d205cda6SVarun Wadekar 	flags = 0;
141d205cda6SVarun Wadekar 	set_interrupt_rm_flag(flags, NON_SECURE);
142d205cda6SVarun Wadekar 	ret = register_interrupt_type_handler(INTR_TYPE_S_EL1,
143d205cda6SVarun Wadekar 					      tlkd_interrupt_handler,
144d205cda6SVarun Wadekar 					      flags);
145d205cda6SVarun Wadekar 	if (ret != 0) {
146d205cda6SVarun Wadekar 		ERROR("failed to register tlkd interrupt handler (%d)\n", ret);
147d205cda6SVarun Wadekar 	}
148d205cda6SVarun Wadekar 
14922038315SVarun Wadekar 	/*
15022038315SVarun Wadekar 	 * All TLK SPD initialization done. Now register our init function
15122038315SVarun Wadekar 	 * with BL31 for deferred invocation
15222038315SVarun Wadekar 	 */
15322038315SVarun Wadekar 	bl31_register_bl32_init(&tlkd_init);
15422038315SVarun Wadekar 
15522038315SVarun Wadekar 	return 0;
15622038315SVarun Wadekar }
15722038315SVarun Wadekar 
15822038315SVarun Wadekar /*******************************************************************************
15922038315SVarun Wadekar  * This function passes control to the Secure Payload image (BL32) for the first
16022038315SVarun Wadekar  * time on the primary cpu after a cold boot. It assumes that a valid secure
16122038315SVarun Wadekar  * context has already been created by tlkd_setup() which can be directly
16222038315SVarun Wadekar  * used. This function performs a synchronous entry into the Secure payload.
16322038315SVarun Wadekar  * The SP passes control back to this routine through a SMC.
16422038315SVarun Wadekar  ******************************************************************************/
165724fd958SMasahiro Yamada static int32_t tlkd_init(void)
16622038315SVarun Wadekar {
16722038315SVarun Wadekar 	entry_point_info_t *tlk_entry_point;
16822038315SVarun Wadekar 
16922038315SVarun Wadekar 	/*
17022038315SVarun Wadekar 	 * Get information about the Secure Payload (BL32) image. Its
17122038315SVarun Wadekar 	 * absence is a critical failure.
17222038315SVarun Wadekar 	 */
17322038315SVarun Wadekar 	tlk_entry_point = bl31_plat_get_next_image_ep_info(SECURE);
17422038315SVarun Wadekar 	assert(tlk_entry_point);
17522038315SVarun Wadekar 
176fd650ff6SSoby Mathew 	cm_init_my_context(tlk_entry_point);
17722038315SVarun Wadekar 
17822038315SVarun Wadekar 	/*
17926670c82SVarun Wadekar 	 * TLK runs only on a single CPU. Store the value of the boot
18026670c82SVarun Wadekar 	 * CPU for sanity checking later.
18126670c82SVarun Wadekar 	 */
18226670c82SVarun Wadekar 	boot_cpu = plat_my_core_pos();
18326670c82SVarun Wadekar 
18426670c82SVarun Wadekar 	/*
18522038315SVarun Wadekar 	 * Arrange for an entry into the test secure payload.
18622038315SVarun Wadekar 	 */
18722038315SVarun Wadekar 	return tlkd_synchronous_sp_entry(&tlk_ctx);
18822038315SVarun Wadekar }
18922038315SVarun Wadekar 
19022038315SVarun Wadekar /*******************************************************************************
19122038315SVarun Wadekar  * This function is responsible for handling all SMCs in the Trusted OS/App
19222038315SVarun Wadekar  * range from the non-secure state as defined in the SMC Calling Convention
19322038315SVarun Wadekar  * Document. It is also responsible for communicating with the Secure payload
19422038315SVarun Wadekar  * to delegate work and return results back to the non-secure state. Lastly it
19522038315SVarun Wadekar  * will also return any information that the secure payload needs to do the
19622038315SVarun Wadekar  * work assigned to it.
19722038315SVarun Wadekar  ******************************************************************************/
19857d1e5faSMasahiro Yamada static uintptr_t tlkd_smc_handler(uint32_t smc_fid,
19957d1e5faSMasahiro Yamada 			 u_register_t x1,
20057d1e5faSMasahiro Yamada 			 u_register_t x2,
20157d1e5faSMasahiro Yamada 			 u_register_t x3,
20257d1e5faSMasahiro Yamada 			 u_register_t x4,
20322038315SVarun Wadekar 			 void *cookie,
20422038315SVarun Wadekar 			 void *handle,
20557d1e5faSMasahiro Yamada 			 u_register_t flags)
20622038315SVarun Wadekar {
20777199df7SVarun Wadekar 	cpu_context_t *ns_cpu_context;
208709a3c47SVarun Wadekar 	gp_regs_t *gp_regs;
20922038315SVarun Wadekar 	uint32_t ns;
210709a3c47SVarun Wadekar 	uint64_t par;
21122038315SVarun Wadekar 
21222038315SVarun Wadekar 	/* Passing a NULL context is a critical programming error */
21322038315SVarun Wadekar 	assert(handle);
21422038315SVarun Wadekar 
21526670c82SVarun Wadekar 	/* These SMCs are only supported by a single CPU */
21626670c82SVarun Wadekar 	if (boot_cpu != plat_my_core_pos())
2176693962cSVarun Wadekar 		SMC_RET1(handle, SMC_UNK);
2186693962cSVarun Wadekar 
21922038315SVarun Wadekar 	/* Determine which security state this SMC originated from */
22022038315SVarun Wadekar 	ns = is_caller_non_secure(flags);
22122038315SVarun Wadekar 
22222038315SVarun Wadekar 	switch (smc_fid) {
22322038315SVarun Wadekar 
22422038315SVarun Wadekar 	/*
225f9d25054SVarun Wadekar 	 * This function ID is used by SP to indicate that it was
226f9d25054SVarun Wadekar 	 * preempted by a non-secure world IRQ.
227f9d25054SVarun Wadekar 	 */
228f9d25054SVarun Wadekar 	case TLK_PREEMPTED:
229f9d25054SVarun Wadekar 
230f9d25054SVarun Wadekar 		if (ns)
231f9d25054SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
232f9d25054SVarun Wadekar 
233f9d25054SVarun Wadekar 		assert(handle == cm_get_context(SECURE));
234f9d25054SVarun Wadekar 		cm_el1_sysregs_context_save(SECURE);
235f9d25054SVarun Wadekar 
236f9d25054SVarun Wadekar 		/* Get a reference to the non-secure context */
237f9d25054SVarun Wadekar 		ns_cpu_context = cm_get_context(NON_SECURE);
238f9d25054SVarun Wadekar 		assert(ns_cpu_context);
239f9d25054SVarun Wadekar 
240f9d25054SVarun Wadekar 		/*
241f9d25054SVarun Wadekar 		 * Restore non-secure state. There is no need to save the
242f9d25054SVarun Wadekar 		 * secure system register context since the SP was supposed
243f9d25054SVarun Wadekar 		 * to preserve it during S-EL1 interrupt handling.
244f9d25054SVarun Wadekar 		 */
245f9d25054SVarun Wadekar 		cm_el1_sysregs_context_restore(NON_SECURE);
246f9d25054SVarun Wadekar 		cm_set_next_eret_context(NON_SECURE);
247f9d25054SVarun Wadekar 
248709a3c47SVarun Wadekar 		SMC_RET1(ns_cpu_context, x1);
249f9d25054SVarun Wadekar 
250f9d25054SVarun Wadekar 	/*
25177199df7SVarun Wadekar 	 * This is a request from the non-secure context to:
25277199df7SVarun Wadekar 	 *
25377199df7SVarun Wadekar 	 * a. register shared memory with the SP for storing it's
25477199df7SVarun Wadekar 	 *    activity logs.
25577199df7SVarun Wadekar 	 * b. register shared memory with the SP for passing args
25677199df7SVarun Wadekar 	 *    required for maintaining sessions with the Trusted
25777199df7SVarun Wadekar 	 *    Applications.
2587bc05f52SMihir Joshi 	 * c. register shared persistent buffers for secure storage
2597bc05f52SMihir Joshi 	 * d. register NS DRAM ranges passed by Cboot
2607bc05f52SMihir Joshi 	 * e. register Root of Trust parameters from Cboot for Verified Boot
2617bc05f52SMihir Joshi 	 * f. open/close sessions
2627bc05f52SMihir Joshi 	 * g. issue commands to the Trusted Apps
2637bc05f52SMihir Joshi 	 * h. resume the preempted yielding SMC call.
26477199df7SVarun Wadekar 	 */
26577199df7SVarun Wadekar 	case TLK_REGISTER_LOGBUF:
26677199df7SVarun Wadekar 	case TLK_REGISTER_REQBUF:
2677bc05f52SMihir Joshi 	case TLK_SS_REGISTER_HANDLER:
2687bc05f52SMihir Joshi 	case TLK_REGISTER_NS_DRAM_RANGES:
2697bc05f52SMihir Joshi 	case TLK_SET_ROOT_OF_TRUST:
2706693962cSVarun Wadekar 	case TLK_OPEN_TA_SESSION:
2716693962cSVarun Wadekar 	case TLK_CLOSE_TA_SESSION:
2726693962cSVarun Wadekar 	case TLK_TA_LAUNCH_OP:
2736693962cSVarun Wadekar 	case TLK_TA_SEND_EVENT:
274ca15d9bcSVarun Wadekar 	case TLK_RESUME_FID:
2756693962cSVarun Wadekar 
276709a3c47SVarun Wadekar 		if (!ns)
27777199df7SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
27877199df7SVarun Wadekar 
27977199df7SVarun Wadekar 		/*
28077199df7SVarun Wadekar 		 * This is a fresh request from the non-secure client.
28177199df7SVarun Wadekar 		 * The parameters are in x1 and x2. Figure out which
28277199df7SVarun Wadekar 		 * registers need to be preserved, save the non-secure
28377199df7SVarun Wadekar 		 * state and send the request to the secure payload.
28477199df7SVarun Wadekar 		 */
28577199df7SVarun Wadekar 		assert(handle == cm_get_context(NON_SECURE));
28677199df7SVarun Wadekar 
287ca15d9bcSVarun Wadekar 		/*
288bbbbcdaeSDavid Cunado 		 * Check if we are already processing a yielding SMC
289ca15d9bcSVarun Wadekar 		 * call. Of all the supported fids, only the "resume"
290ca15d9bcSVarun Wadekar 		 * fid expects the flag to be set.
291ca15d9bcSVarun Wadekar 		 */
292ca15d9bcSVarun Wadekar 		if (smc_fid == TLK_RESUME_FID) {
293bbbbcdaeSDavid Cunado 			if (!get_yield_smc_active_flag(tlk_ctx.state))
294ca15d9bcSVarun Wadekar 				SMC_RET1(handle, SMC_UNK);
295ca15d9bcSVarun Wadekar 		} else {
296bbbbcdaeSDavid Cunado 			if (get_yield_smc_active_flag(tlk_ctx.state))
29777199df7SVarun Wadekar 				SMC_RET1(handle, SMC_UNK);
298ca15d9bcSVarun Wadekar 		}
29977199df7SVarun Wadekar 
30077199df7SVarun Wadekar 		cm_el1_sysregs_context_save(NON_SECURE);
30177199df7SVarun Wadekar 
30277199df7SVarun Wadekar 		/*
30377199df7SVarun Wadekar 		 * Verify if there is a valid context to use.
30477199df7SVarun Wadekar 		 */
30577199df7SVarun Wadekar 		assert(&tlk_ctx.cpu_ctx == cm_get_context(SECURE));
30677199df7SVarun Wadekar 
30777199df7SVarun Wadekar 		/*
30877199df7SVarun Wadekar 		 * Mark the SP state as active.
30977199df7SVarun Wadekar 		 */
310bbbbcdaeSDavid Cunado 		set_yield_smc_active_flag(tlk_ctx.state);
31177199df7SVarun Wadekar 
31277199df7SVarun Wadekar 		/*
31377199df7SVarun Wadekar 		 * We are done stashing the non-secure context. Ask the
31477199df7SVarun Wadekar 		 * secure payload to do the work now.
31577199df7SVarun Wadekar 		 */
31677199df7SVarun Wadekar 		cm_el1_sysregs_context_restore(SECURE);
31777199df7SVarun Wadekar 		cm_set_next_eret_context(SECURE);
31877199df7SVarun Wadekar 
31977199df7SVarun Wadekar 		/*
320709a3c47SVarun Wadekar 		 * TLK is a 32-bit Trusted OS and so expects the SMC
321709a3c47SVarun Wadekar 		 * arguments via r0-r7. TLK expects the monitor frame
322709a3c47SVarun Wadekar 		 * registers to be 64-bits long. Hence, we pass x0 in
323709a3c47SVarun Wadekar 		 * r0-r1, x1 in r2-r3, x3 in r4-r5 and x4 in r6-r7.
324709a3c47SVarun Wadekar 		 *
325709a3c47SVarun Wadekar 		 * As smc_fid is a uint32 value, r1 contains 0.
326709a3c47SVarun Wadekar 		 */
327709a3c47SVarun Wadekar 		gp_regs = get_gpregs_ctx(&tlk_ctx.cpu_ctx);
328709a3c47SVarun Wadekar 		write_ctx_reg(gp_regs, CTX_GPREG_X4, (uint32_t)x2);
329709a3c47SVarun Wadekar 		write_ctx_reg(gp_regs, CTX_GPREG_X5, (uint32_t)(x2 >> 32));
330709a3c47SVarun Wadekar 		write_ctx_reg(gp_regs, CTX_GPREG_X6, (uint32_t)x3);
331709a3c47SVarun Wadekar 		write_ctx_reg(gp_regs, CTX_GPREG_X7, (uint32_t)(x3 >> 32));
332709a3c47SVarun Wadekar 		SMC_RET4(&tlk_ctx.cpu_ctx, smc_fid, 0, (uint32_t)x1,
333709a3c47SVarun Wadekar 			(uint32_t)(x1 >> 32));
334709a3c47SVarun Wadekar 
335709a3c47SVarun Wadekar 	/*
336709a3c47SVarun Wadekar 	 * Translate NS/EL1-S virtual addresses.
337709a3c47SVarun Wadekar 	 *
338709a3c47SVarun Wadekar 	 * x1 = virtual address
339709a3c47SVarun Wadekar 	 * x3 = type (NS/S)
340709a3c47SVarun Wadekar 	 *
341709a3c47SVarun Wadekar 	 * Returns PA:lo in r0, PA:hi in r1.
3426e159e7aSVarun Wadekar 	 */
3436e159e7aSVarun Wadekar 	case TLK_VA_TRANSLATE:
344709a3c47SVarun Wadekar 
345709a3c47SVarun Wadekar 		/* Should be invoked only by secure world */
346709a3c47SVarun Wadekar 		if (ns)
3476e159e7aSVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
3486e159e7aSVarun Wadekar 
349709a3c47SVarun Wadekar 		/* NS virtual addresses are 64-bit long */
350709a3c47SVarun Wadekar 		if (x3 & TLK_TRANSLATE_NS_VADDR)
351709a3c47SVarun Wadekar 			x1 = (uint32_t)x1 | (x2 << 32);
352709a3c47SVarun Wadekar 
353709a3c47SVarun Wadekar 		if (!x1)
354709a3c47SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
355709a3c47SVarun Wadekar 
356709a3c47SVarun Wadekar 		/*
357709a3c47SVarun Wadekar 		 * TODO: Sanity check x1. This would require platform
358709a3c47SVarun Wadekar 		 * support.
359709a3c47SVarun Wadekar 		 */
360709a3c47SVarun Wadekar 
3616e159e7aSVarun Wadekar 		/* virtual address and type: ns/s */
362709a3c47SVarun Wadekar 		par = tlkd_va_translate(x1, x3);
3636e159e7aSVarun Wadekar 
364709a3c47SVarun Wadekar 		/* return physical address in r0-r1 */
365709a3c47SVarun Wadekar 		SMC_RET4(handle, (uint32_t)par, (uint32_t)(par >> 32), 0, 0);
3666e159e7aSVarun Wadekar 
3676e159e7aSVarun Wadekar 	/*
36877199df7SVarun Wadekar 	 * This is a request from the SP to mark completion of
369bbbbcdaeSDavid Cunado 	 * a yielding function ID.
37077199df7SVarun Wadekar 	 */
37177199df7SVarun Wadekar 	case TLK_REQUEST_DONE:
372709a3c47SVarun Wadekar 		if (ns)
37377199df7SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
37477199df7SVarun Wadekar 
37577199df7SVarun Wadekar 		/*
37677199df7SVarun Wadekar 		 * Mark the SP state as inactive.
37777199df7SVarun Wadekar 		 */
378bbbbcdaeSDavid Cunado 		clr_yield_smc_active_flag(tlk_ctx.state);
37977199df7SVarun Wadekar 
38077199df7SVarun Wadekar 		/* Get a reference to the non-secure context */
38177199df7SVarun Wadekar 		ns_cpu_context = cm_get_context(NON_SECURE);
38277199df7SVarun Wadekar 		assert(ns_cpu_context);
38377199df7SVarun Wadekar 
38477199df7SVarun Wadekar 		/*
38577199df7SVarun Wadekar 		 * This is a request completion SMC and we must switch to
38677199df7SVarun Wadekar 		 * the non-secure world to pass the result.
38777199df7SVarun Wadekar 		 */
38877199df7SVarun Wadekar 		cm_el1_sysregs_context_save(SECURE);
38977199df7SVarun Wadekar 
39077199df7SVarun Wadekar 		/*
39177199df7SVarun Wadekar 		 * We are done stashing the secure context. Switch to the
39277199df7SVarun Wadekar 		 * non-secure context and return the result.
39377199df7SVarun Wadekar 		 */
39477199df7SVarun Wadekar 		cm_el1_sysregs_context_restore(NON_SECURE);
39577199df7SVarun Wadekar 		cm_set_next_eret_context(NON_SECURE);
396709a3c47SVarun Wadekar 		SMC_RET1(ns_cpu_context, x1);
39777199df7SVarun Wadekar 
39877199df7SVarun Wadekar 	/*
39922038315SVarun Wadekar 	 * This function ID is used only by the SP to indicate it has
40022038315SVarun Wadekar 	 * finished initialising itself after a cold boot
40122038315SVarun Wadekar 	 */
40222038315SVarun Wadekar 	case TLK_ENTRY_DONE:
403709a3c47SVarun Wadekar 		if (ns)
40422038315SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
40522038315SVarun Wadekar 
40622038315SVarun Wadekar 		/*
40722038315SVarun Wadekar 		 * SP has been successfully initialized. Register power
4088aabea33SPaul Beesley 		 * management hooks with PSCI
40922038315SVarun Wadekar 		 */
41022038315SVarun Wadekar 		psci_register_spd_pm_hook(&tlkd_pm_ops);
41122038315SVarun Wadekar 
41222038315SVarun Wadekar 		/*
41322038315SVarun Wadekar 		 * TLK reports completion. The SPD must have initiated
41422038315SVarun Wadekar 		 * the original request through a synchronous entry
41522038315SVarun Wadekar 		 * into the SP. Jump back to the original C runtime
41622038315SVarun Wadekar 		 * context.
41722038315SVarun Wadekar 		 */
418709a3c47SVarun Wadekar 		tlkd_synchronous_sp_exit(&tlk_ctx, x1);
419185a23ffSJonathan Wright 		break;
42022038315SVarun Wadekar 
42122038315SVarun Wadekar 	/*
422cb790c5eSVarun Wadekar 	 * These function IDs are used only by TLK to indicate it has
423cb790c5eSVarun Wadekar 	 * finished:
424cb790c5eSVarun Wadekar 	 * 1. suspending itself after an earlier psci cpu_suspend
425cb790c5eSVarun Wadekar 	 *    request.
426cb790c5eSVarun Wadekar 	 * 2. resuming itself after an earlier psci cpu_suspend
427cb790c5eSVarun Wadekar 	 *    request.
428cb790c5eSVarun Wadekar 	 * 3. powering down after an earlier psci system_off/system_reset
429cb790c5eSVarun Wadekar 	 *    request.
430cb790c5eSVarun Wadekar 	 */
431cb790c5eSVarun Wadekar 	case TLK_SUSPEND_DONE:
432cb790c5eSVarun Wadekar 	case TLK_RESUME_DONE:
433cb790c5eSVarun Wadekar 
434cb790c5eSVarun Wadekar 		if (ns)
435cb790c5eSVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
436cb790c5eSVarun Wadekar 
437cb790c5eSVarun Wadekar 		/*
438cb790c5eSVarun Wadekar 		 * TLK reports completion. TLKD must have initiated the
439cb790c5eSVarun Wadekar 		 * original request through a synchronous entry into the SP.
440cb790c5eSVarun Wadekar 		 * Jump back to the original C runtime context, and pass x1 as
441cb790c5eSVarun Wadekar 		 * return value to the caller
442cb790c5eSVarun Wadekar 		 */
443cb790c5eSVarun Wadekar 		tlkd_synchronous_sp_exit(&tlk_ctx, x1);
444185a23ffSJonathan Wright 		break;
445cb790c5eSVarun Wadekar 
446cb790c5eSVarun Wadekar 	/*
447d205cda6SVarun Wadekar 	 * This function ID is used by SP to indicate that it has completed
448d205cda6SVarun Wadekar 	 * handling the secure interrupt.
449d205cda6SVarun Wadekar 	 */
450d205cda6SVarun Wadekar 	case TLK_IRQ_DONE:
451d205cda6SVarun Wadekar 
452d205cda6SVarun Wadekar 		if (ns)
453d205cda6SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
454d205cda6SVarun Wadekar 
455d205cda6SVarun Wadekar 		assert(handle == cm_get_context(SECURE));
456d205cda6SVarun Wadekar 
457d205cda6SVarun Wadekar 		/* save secure world context */
458d205cda6SVarun Wadekar 		cm_el1_sysregs_context_save(SECURE);
459d205cda6SVarun Wadekar 
460d205cda6SVarun Wadekar 		/* Get a reference to the non-secure context */
461d205cda6SVarun Wadekar 		ns_cpu_context = cm_get_context(NON_SECURE);
462d205cda6SVarun Wadekar 		assert(ns_cpu_context);
463d205cda6SVarun Wadekar 
464d205cda6SVarun Wadekar 		/*
465d205cda6SVarun Wadekar 		 * Restore non-secure state. There is no need to save the
466d205cda6SVarun Wadekar 		 * secure system register context since the SP was supposed
467d205cda6SVarun Wadekar 		 * to preserve it during S-EL1 interrupt handling.
468d205cda6SVarun Wadekar 		 */
469d205cda6SVarun Wadekar 		cm_el1_sysregs_context_restore(NON_SECURE);
470d205cda6SVarun Wadekar 		cm_set_next_eret_context(NON_SECURE);
471d205cda6SVarun Wadekar 
472d205cda6SVarun Wadekar 		SMC_RET0(ns_cpu_context);
473d205cda6SVarun Wadekar 
474d205cda6SVarun Wadekar 	/*
47522038315SVarun Wadekar 	 * Return the number of service function IDs implemented to
47622038315SVarun Wadekar 	 * provide service to non-secure
47722038315SVarun Wadekar 	 */
47822038315SVarun Wadekar 	case TOS_CALL_COUNT:
47922038315SVarun Wadekar 		SMC_RET1(handle, TLK_NUM_FID);
48022038315SVarun Wadekar 
48122038315SVarun Wadekar 	/*
48222038315SVarun Wadekar 	 * Return TLK's UID to the caller
48322038315SVarun Wadekar 	 */
48422038315SVarun Wadekar 	case TOS_UID:
48522038315SVarun Wadekar 		SMC_UUID_RET(handle, tlk_uuid);
48622038315SVarun Wadekar 
48722038315SVarun Wadekar 	/*
48822038315SVarun Wadekar 	 * Return the version of current implementation
48922038315SVarun Wadekar 	 */
49022038315SVarun Wadekar 	case TOS_CALL_VERSION:
49122038315SVarun Wadekar 		SMC_RET2(handle, TLK_VERSION_MAJOR, TLK_VERSION_MINOR);
49222038315SVarun Wadekar 
49322038315SVarun Wadekar 	default:
4947bc05f52SMihir Joshi 		WARN("%s: Unhandled SMC: 0x%x\n", __func__, smc_fid);
49522038315SVarun Wadekar 		break;
49622038315SVarun Wadekar 	}
49722038315SVarun Wadekar 
49822038315SVarun Wadekar 	SMC_RET1(handle, SMC_UNK);
49922038315SVarun Wadekar }
50022038315SVarun Wadekar 
50122038315SVarun Wadekar /* Define a SPD runtime service descriptor for fast SMC calls */
50222038315SVarun Wadekar DECLARE_RT_SVC(
50322038315SVarun Wadekar 	tlkd_tos_fast,
50422038315SVarun Wadekar 
50522038315SVarun Wadekar 	OEN_TOS_START,
50622038315SVarun Wadekar 	OEN_TOS_END,
50722038315SVarun Wadekar 	SMC_TYPE_FAST,
50822038315SVarun Wadekar 	tlkd_setup,
50922038315SVarun Wadekar 	tlkd_smc_handler
51022038315SVarun Wadekar );
51122038315SVarun Wadekar 
512bbbbcdaeSDavid Cunado /* Define a SPD runtime service descriptor for yielding SMC calls */
51322038315SVarun Wadekar DECLARE_RT_SVC(
51422038315SVarun Wadekar 	tlkd_tos_std,
51522038315SVarun Wadekar 
51622038315SVarun Wadekar 	OEN_TOS_START,
51722038315SVarun Wadekar 	OEN_TOS_END,
518bbbbcdaeSDavid Cunado 	SMC_TYPE_YIELD,
51922038315SVarun Wadekar 	NULL,
52022038315SVarun Wadekar 	tlkd_smc_handler
52122038315SVarun Wadekar );
5226693962cSVarun Wadekar 
5236693962cSVarun Wadekar /* Define a SPD runtime service descriptor for fast SMC calls */
5246693962cSVarun Wadekar DECLARE_RT_SVC(
5256693962cSVarun Wadekar 	tlkd_tap_fast,
5266693962cSVarun Wadekar 
5276693962cSVarun Wadekar 	OEN_TAP_START,
5286693962cSVarun Wadekar 	OEN_TAP_END,
5296693962cSVarun Wadekar 	SMC_TYPE_FAST,
5306693962cSVarun Wadekar 	NULL,
5316693962cSVarun Wadekar 	tlkd_smc_handler
5326693962cSVarun Wadekar );
5336693962cSVarun Wadekar 
534bbbbcdaeSDavid Cunado /* Define a SPD runtime service descriptor for yielding SMC calls */
5356693962cSVarun Wadekar DECLARE_RT_SVC(
5366693962cSVarun Wadekar 	tlkd_tap_std,
5376693962cSVarun Wadekar 
5386693962cSVarun Wadekar 	OEN_TAP_START,
5396693962cSVarun Wadekar 	OEN_TAP_END,
540bbbbcdaeSDavid Cunado 	SMC_TYPE_YIELD,
5416693962cSVarun Wadekar 	NULL,
5426693962cSVarun Wadekar 	tlkd_smc_handler
5436693962cSVarun Wadekar );
544