xref: /rk3399_ARM-atf/services/spd/tlkd/tlkd_main.c (revision 907c58b2e12cbc7336b3f9d5977cc9ae8aeefae4)
122038315SVarun Wadekar /*
2d205cda6SVarun Wadekar  * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved.
30600cf63SVarun 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  ******************************************************************************/
tlkd_interrupt_handler(uint32_t id,uint32_t flags,void * handle,void * cookie)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  ******************************************************************************/
tlkd_setup(void)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  ******************************************************************************/
tlkd_init(void)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  ******************************************************************************/
tlkd_smc_handler(uint32_t smc_fid,u_register_t x1,u_register_t x2,u_register_t x3,u_register_t x4,void * cookie,void * handle,u_register_t flags)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:
275*bd0c2f8dSMustafa Yigit Bilgen 	case TLK_SET_BL_VERSION:
276*bd0c2f8dSMustafa Yigit Bilgen 	case TLK_LOCK_BL_INTERFACE:
277*bd0c2f8dSMustafa Yigit Bilgen 	case TLK_BL_RPMB_SERVICE:
2786693962cSVarun Wadekar 
279709a3c47SVarun Wadekar 		if (!ns)
28077199df7SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
28177199df7SVarun Wadekar 
28277199df7SVarun Wadekar 		/*
28377199df7SVarun Wadekar 		 * This is a fresh request from the non-secure client.
28477199df7SVarun Wadekar 		 * The parameters are in x1 and x2. Figure out which
28577199df7SVarun Wadekar 		 * registers need to be preserved, save the non-secure
28677199df7SVarun Wadekar 		 * state and send the request to the secure payload.
28777199df7SVarun Wadekar 		 */
28877199df7SVarun Wadekar 		assert(handle == cm_get_context(NON_SECURE));
28977199df7SVarun Wadekar 
290ca15d9bcSVarun Wadekar 		/*
291bbbbcdaeSDavid Cunado 		 * Check if we are already processing a yielding SMC
292ca15d9bcSVarun Wadekar 		 * call. Of all the supported fids, only the "resume"
293ca15d9bcSVarun Wadekar 		 * fid expects the flag to be set.
294ca15d9bcSVarun Wadekar 		 */
295ca15d9bcSVarun Wadekar 		if (smc_fid == TLK_RESUME_FID) {
296bbbbcdaeSDavid Cunado 			if (!get_yield_smc_active_flag(tlk_ctx.state))
297ca15d9bcSVarun Wadekar 				SMC_RET1(handle, SMC_UNK);
298ca15d9bcSVarun Wadekar 		} else {
299bbbbcdaeSDavid Cunado 			if (get_yield_smc_active_flag(tlk_ctx.state))
30077199df7SVarun Wadekar 				SMC_RET1(handle, SMC_UNK);
301ca15d9bcSVarun Wadekar 		}
30277199df7SVarun Wadekar 
30377199df7SVarun Wadekar 		cm_el1_sysregs_context_save(NON_SECURE);
30477199df7SVarun Wadekar 
30577199df7SVarun Wadekar 		/*
30677199df7SVarun Wadekar 		 * Verify if there is a valid context to use.
30777199df7SVarun Wadekar 		 */
30877199df7SVarun Wadekar 		assert(&tlk_ctx.cpu_ctx == cm_get_context(SECURE));
30977199df7SVarun Wadekar 
31077199df7SVarun Wadekar 		/*
31177199df7SVarun Wadekar 		 * Mark the SP state as active.
31277199df7SVarun Wadekar 		 */
313bbbbcdaeSDavid Cunado 		set_yield_smc_active_flag(tlk_ctx.state);
31477199df7SVarun Wadekar 
31577199df7SVarun Wadekar 		/*
31677199df7SVarun Wadekar 		 * We are done stashing the non-secure context. Ask the
31777199df7SVarun Wadekar 		 * secure payload to do the work now.
31877199df7SVarun Wadekar 		 */
31977199df7SVarun Wadekar 		cm_el1_sysregs_context_restore(SECURE);
32077199df7SVarun Wadekar 		cm_set_next_eret_context(SECURE);
32177199df7SVarun Wadekar 
32277199df7SVarun Wadekar 		/*
323709a3c47SVarun Wadekar 		 * TLK is a 32-bit Trusted OS and so expects the SMC
324709a3c47SVarun Wadekar 		 * arguments via r0-r7. TLK expects the monitor frame
325709a3c47SVarun Wadekar 		 * registers to be 64-bits long. Hence, we pass x0 in
326709a3c47SVarun Wadekar 		 * r0-r1, x1 in r2-r3, x3 in r4-r5 and x4 in r6-r7.
327709a3c47SVarun Wadekar 		 *
328709a3c47SVarun Wadekar 		 * As smc_fid is a uint32 value, r1 contains 0.
329709a3c47SVarun Wadekar 		 */
330709a3c47SVarun Wadekar 		gp_regs = get_gpregs_ctx(&tlk_ctx.cpu_ctx);
331709a3c47SVarun Wadekar 		write_ctx_reg(gp_regs, CTX_GPREG_X4, (uint32_t)x2);
332709a3c47SVarun Wadekar 		write_ctx_reg(gp_regs, CTX_GPREG_X5, (uint32_t)(x2 >> 32));
333709a3c47SVarun Wadekar 		write_ctx_reg(gp_regs, CTX_GPREG_X6, (uint32_t)x3);
334709a3c47SVarun Wadekar 		write_ctx_reg(gp_regs, CTX_GPREG_X7, (uint32_t)(x3 >> 32));
335709a3c47SVarun Wadekar 		SMC_RET4(&tlk_ctx.cpu_ctx, smc_fid, 0, (uint32_t)x1,
336709a3c47SVarun Wadekar 			(uint32_t)(x1 >> 32));
337709a3c47SVarun Wadekar 
338709a3c47SVarun Wadekar 	/*
339709a3c47SVarun Wadekar 	 * Translate NS/EL1-S virtual addresses.
340709a3c47SVarun Wadekar 	 *
341709a3c47SVarun Wadekar 	 * x1 = virtual address
342709a3c47SVarun Wadekar 	 * x3 = type (NS/S)
343709a3c47SVarun Wadekar 	 *
344709a3c47SVarun Wadekar 	 * Returns PA:lo in r0, PA:hi in r1.
3456e159e7aSVarun Wadekar 	 */
3466e159e7aSVarun Wadekar 	case TLK_VA_TRANSLATE:
347709a3c47SVarun Wadekar 
348709a3c47SVarun Wadekar 		/* Should be invoked only by secure world */
349709a3c47SVarun Wadekar 		if (ns)
3506e159e7aSVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
3516e159e7aSVarun Wadekar 
352709a3c47SVarun Wadekar 		/* NS virtual addresses are 64-bit long */
353709a3c47SVarun Wadekar 		if (x3 & TLK_TRANSLATE_NS_VADDR)
354709a3c47SVarun Wadekar 			x1 = (uint32_t)x1 | (x2 << 32);
355709a3c47SVarun Wadekar 
356709a3c47SVarun Wadekar 		if (!x1)
357709a3c47SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
358709a3c47SVarun Wadekar 
359709a3c47SVarun Wadekar 		/*
360709a3c47SVarun Wadekar 		 * TODO: Sanity check x1. This would require platform
361709a3c47SVarun Wadekar 		 * support.
362709a3c47SVarun Wadekar 		 */
363709a3c47SVarun Wadekar 
3646e159e7aSVarun Wadekar 		/* virtual address and type: ns/s */
365709a3c47SVarun Wadekar 		par = tlkd_va_translate(x1, x3);
3666e159e7aSVarun Wadekar 
367709a3c47SVarun Wadekar 		/* return physical address in r0-r1 */
368709a3c47SVarun Wadekar 		SMC_RET4(handle, (uint32_t)par, (uint32_t)(par >> 32), 0, 0);
3696e159e7aSVarun Wadekar 
3706e159e7aSVarun Wadekar 	/*
37177199df7SVarun Wadekar 	 * This is a request from the SP to mark completion of
372bbbbcdaeSDavid Cunado 	 * a yielding function ID.
37377199df7SVarun Wadekar 	 */
37477199df7SVarun Wadekar 	case TLK_REQUEST_DONE:
375709a3c47SVarun Wadekar 		if (ns)
37677199df7SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
37777199df7SVarun Wadekar 
37877199df7SVarun Wadekar 		/*
37977199df7SVarun Wadekar 		 * Mark the SP state as inactive.
38077199df7SVarun Wadekar 		 */
381bbbbcdaeSDavid Cunado 		clr_yield_smc_active_flag(tlk_ctx.state);
38277199df7SVarun Wadekar 
38377199df7SVarun Wadekar 		/* Get a reference to the non-secure context */
38477199df7SVarun Wadekar 		ns_cpu_context = cm_get_context(NON_SECURE);
38577199df7SVarun Wadekar 		assert(ns_cpu_context);
38677199df7SVarun Wadekar 
38777199df7SVarun Wadekar 		/*
38877199df7SVarun Wadekar 		 * This is a request completion SMC and we must switch to
38977199df7SVarun Wadekar 		 * the non-secure world to pass the result.
39077199df7SVarun Wadekar 		 */
39177199df7SVarun Wadekar 		cm_el1_sysregs_context_save(SECURE);
39277199df7SVarun Wadekar 
39377199df7SVarun Wadekar 		/*
39477199df7SVarun Wadekar 		 * We are done stashing the secure context. Switch to the
39577199df7SVarun Wadekar 		 * non-secure context and return the result.
39677199df7SVarun Wadekar 		 */
39777199df7SVarun Wadekar 		cm_el1_sysregs_context_restore(NON_SECURE);
39877199df7SVarun Wadekar 		cm_set_next_eret_context(NON_SECURE);
399709a3c47SVarun Wadekar 		SMC_RET1(ns_cpu_context, x1);
40077199df7SVarun Wadekar 
40177199df7SVarun Wadekar 	/*
40222038315SVarun Wadekar 	 * This function ID is used only by the SP to indicate it has
40322038315SVarun Wadekar 	 * finished initialising itself after a cold boot
40422038315SVarun Wadekar 	 */
40522038315SVarun Wadekar 	case TLK_ENTRY_DONE:
406709a3c47SVarun Wadekar 		if (ns)
40722038315SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
40822038315SVarun Wadekar 
40922038315SVarun Wadekar 		/*
41022038315SVarun Wadekar 		 * SP has been successfully initialized. Register power
4118aabea33SPaul Beesley 		 * management hooks with PSCI
41222038315SVarun Wadekar 		 */
41322038315SVarun Wadekar 		psci_register_spd_pm_hook(&tlkd_pm_ops);
41422038315SVarun Wadekar 
41522038315SVarun Wadekar 		/*
41622038315SVarun Wadekar 		 * TLK reports completion. The SPD must have initiated
41722038315SVarun Wadekar 		 * the original request through a synchronous entry
41822038315SVarun Wadekar 		 * into the SP. Jump back to the original C runtime
41922038315SVarun Wadekar 		 * context.
42022038315SVarun Wadekar 		 */
421709a3c47SVarun Wadekar 		tlkd_synchronous_sp_exit(&tlk_ctx, x1);
422185a23ffSJonathan Wright 		break;
42322038315SVarun Wadekar 
42422038315SVarun Wadekar 	/*
425cb790c5eSVarun Wadekar 	 * These function IDs are used only by TLK to indicate it has
426cb790c5eSVarun Wadekar 	 * finished:
427cb790c5eSVarun Wadekar 	 * 1. suspending itself after an earlier psci cpu_suspend
428cb790c5eSVarun Wadekar 	 *    request.
429cb790c5eSVarun Wadekar 	 * 2. resuming itself after an earlier psci cpu_suspend
430cb790c5eSVarun Wadekar 	 *    request.
431cb790c5eSVarun Wadekar 	 * 3. powering down after an earlier psci system_off/system_reset
432cb790c5eSVarun Wadekar 	 *    request.
433cb790c5eSVarun Wadekar 	 */
434cb790c5eSVarun Wadekar 	case TLK_SUSPEND_DONE:
435cb790c5eSVarun Wadekar 	case TLK_RESUME_DONE:
436cb790c5eSVarun Wadekar 
437cb790c5eSVarun Wadekar 		if (ns)
438cb790c5eSVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
439cb790c5eSVarun Wadekar 
440cb790c5eSVarun Wadekar 		/*
441cb790c5eSVarun Wadekar 		 * TLK reports completion. TLKD must have initiated the
442cb790c5eSVarun Wadekar 		 * original request through a synchronous entry into the SP.
443cb790c5eSVarun Wadekar 		 * Jump back to the original C runtime context, and pass x1 as
444cb790c5eSVarun Wadekar 		 * return value to the caller
445cb790c5eSVarun Wadekar 		 */
446cb790c5eSVarun Wadekar 		tlkd_synchronous_sp_exit(&tlk_ctx, x1);
447185a23ffSJonathan Wright 		break;
448cb790c5eSVarun Wadekar 
449cb790c5eSVarun Wadekar 	/*
450d205cda6SVarun Wadekar 	 * This function ID is used by SP to indicate that it has completed
451d205cda6SVarun Wadekar 	 * handling the secure interrupt.
452d205cda6SVarun Wadekar 	 */
453d205cda6SVarun Wadekar 	case TLK_IRQ_DONE:
454d205cda6SVarun Wadekar 
455d205cda6SVarun Wadekar 		if (ns)
456d205cda6SVarun Wadekar 			SMC_RET1(handle, SMC_UNK);
457d205cda6SVarun Wadekar 
458d205cda6SVarun Wadekar 		assert(handle == cm_get_context(SECURE));
459d205cda6SVarun Wadekar 
460d205cda6SVarun Wadekar 		/* save secure world context */
461d205cda6SVarun Wadekar 		cm_el1_sysregs_context_save(SECURE);
462d205cda6SVarun Wadekar 
463d205cda6SVarun Wadekar 		/* Get a reference to the non-secure context */
464d205cda6SVarun Wadekar 		ns_cpu_context = cm_get_context(NON_SECURE);
465d205cda6SVarun Wadekar 		assert(ns_cpu_context);
466d205cda6SVarun Wadekar 
467d205cda6SVarun Wadekar 		/*
468d205cda6SVarun Wadekar 		 * Restore non-secure state. There is no need to save the
469d205cda6SVarun Wadekar 		 * secure system register context since the SP was supposed
470d205cda6SVarun Wadekar 		 * to preserve it during S-EL1 interrupt handling.
471d205cda6SVarun Wadekar 		 */
472d205cda6SVarun Wadekar 		cm_el1_sysregs_context_restore(NON_SECURE);
473d205cda6SVarun Wadekar 		cm_set_next_eret_context(NON_SECURE);
474d205cda6SVarun Wadekar 
475d205cda6SVarun Wadekar 		SMC_RET0(ns_cpu_context);
476d205cda6SVarun Wadekar 
477d205cda6SVarun Wadekar 	/*
47822038315SVarun Wadekar 	 * Return the number of service function IDs implemented to
47922038315SVarun Wadekar 	 * provide service to non-secure
48022038315SVarun Wadekar 	 */
48122038315SVarun Wadekar 	case TOS_CALL_COUNT:
48222038315SVarun Wadekar 		SMC_RET1(handle, TLK_NUM_FID);
48322038315SVarun Wadekar 
48422038315SVarun Wadekar 	/*
48522038315SVarun Wadekar 	 * Return TLK's UID to the caller
48622038315SVarun Wadekar 	 */
48722038315SVarun Wadekar 	case TOS_UID:
48822038315SVarun Wadekar 		SMC_UUID_RET(handle, tlk_uuid);
48922038315SVarun Wadekar 
49022038315SVarun Wadekar 	/*
49122038315SVarun Wadekar 	 * Return the version of current implementation
49222038315SVarun Wadekar 	 */
49322038315SVarun Wadekar 	case TOS_CALL_VERSION:
49422038315SVarun Wadekar 		SMC_RET2(handle, TLK_VERSION_MAJOR, TLK_VERSION_MINOR);
49522038315SVarun Wadekar 
49622038315SVarun Wadekar 	default:
4977bc05f52SMihir Joshi 		WARN("%s: Unhandled SMC: 0x%x\n", __func__, smc_fid);
49822038315SVarun Wadekar 		break;
49922038315SVarun Wadekar 	}
50022038315SVarun Wadekar 
50122038315SVarun Wadekar 	SMC_RET1(handle, SMC_UNK);
50222038315SVarun Wadekar }
50322038315SVarun Wadekar 
50422038315SVarun Wadekar /* Define a SPD runtime service descriptor for fast SMC calls */
50522038315SVarun Wadekar DECLARE_RT_SVC(
50622038315SVarun Wadekar 	tlkd_tos_fast,
50722038315SVarun Wadekar 
50822038315SVarun Wadekar 	OEN_TOS_START,
50922038315SVarun Wadekar 	OEN_TOS_END,
51022038315SVarun Wadekar 	SMC_TYPE_FAST,
51122038315SVarun Wadekar 	tlkd_setup,
51222038315SVarun Wadekar 	tlkd_smc_handler
51322038315SVarun Wadekar );
51422038315SVarun Wadekar 
515bbbbcdaeSDavid Cunado /* Define a SPD runtime service descriptor for yielding SMC calls */
51622038315SVarun Wadekar DECLARE_RT_SVC(
51722038315SVarun Wadekar 	tlkd_tos_std,
51822038315SVarun Wadekar 
51922038315SVarun Wadekar 	OEN_TOS_START,
52022038315SVarun Wadekar 	OEN_TOS_END,
521bbbbcdaeSDavid Cunado 	SMC_TYPE_YIELD,
52222038315SVarun Wadekar 	NULL,
52322038315SVarun Wadekar 	tlkd_smc_handler
52422038315SVarun Wadekar );
5256693962cSVarun Wadekar 
5266693962cSVarun Wadekar /* Define a SPD runtime service descriptor for fast SMC calls */
5276693962cSVarun Wadekar DECLARE_RT_SVC(
5286693962cSVarun Wadekar 	tlkd_tap_fast,
5296693962cSVarun Wadekar 
5306693962cSVarun Wadekar 	OEN_TAP_START,
5316693962cSVarun Wadekar 	OEN_TAP_END,
5326693962cSVarun Wadekar 	SMC_TYPE_FAST,
5336693962cSVarun Wadekar 	NULL,
5346693962cSVarun Wadekar 	tlkd_smc_handler
5356693962cSVarun Wadekar );
5366693962cSVarun Wadekar 
537bbbbcdaeSDavid Cunado /* Define a SPD runtime service descriptor for yielding SMC calls */
5386693962cSVarun Wadekar DECLARE_RT_SVC(
5396693962cSVarun Wadekar 	tlkd_tap_std,
5406693962cSVarun Wadekar 
5416693962cSVarun Wadekar 	OEN_TAP_START,
5426693962cSVarun Wadekar 	OEN_TAP_END,
543bbbbcdaeSDavid Cunado 	SMC_TYPE_YIELD,
5446693962cSVarun Wadekar 	NULL,
5456693962cSVarun Wadekar 	tlkd_smc_handler
5466693962cSVarun Wadekar );
547