xref: /rk3399_ARM-atf/services/spd/tlkd/tlkd_common.c (revision 185a23ffa3b6fc007021ac9bbab2213c63911c0a)
122038315SVarun Wadekar /*
2bbbbcdaeSDavid Cunado  * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
322038315SVarun Wadekar  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
522038315SVarun Wadekar  */
622038315SVarun Wadekar 
722038315SVarun Wadekar #include <arch_helpers.h>
822038315SVarun Wadekar #include <assert.h>
922038315SVarun Wadekar #include <bl_common.h>
1022038315SVarun Wadekar #include <context_mgmt.h>
1122038315SVarun Wadekar #include <string.h>
1222038315SVarun Wadekar #include "tlkd_private.h"
1322038315SVarun Wadekar 
146e159e7aSVarun Wadekar #define AT_MASK		3
156e159e7aSVarun Wadekar 
166e159e7aSVarun Wadekar /*******************************************************************************
176e159e7aSVarun Wadekar  * This function helps the SP to translate NS/S virtual addresses.
186e159e7aSVarun Wadekar  ******************************************************************************/
196e159e7aSVarun Wadekar uint64_t tlkd_va_translate(uintptr_t va, int type)
206e159e7aSVarun Wadekar {
216e159e7aSVarun Wadekar 	uint64_t pa;
226e159e7aSVarun Wadekar 
236e159e7aSVarun Wadekar 	if (type & TLK_TRANSLATE_NS_VADDR) {
246e159e7aSVarun Wadekar 
256e159e7aSVarun Wadekar 		/* save secure context */
266e159e7aSVarun Wadekar 		cm_el1_sysregs_context_save(SECURE);
276e159e7aSVarun Wadekar 
286e159e7aSVarun Wadekar 		/* restore non-secure context */
296e159e7aSVarun Wadekar 		cm_el1_sysregs_context_restore(NON_SECURE);
306e159e7aSVarun Wadekar 
316e159e7aSVarun Wadekar 		/* switch NS bit to start using 64-bit, non-secure mappings */
326e159e7aSVarun Wadekar 		write_scr(cm_get_scr_el3(NON_SECURE));
336e159e7aSVarun Wadekar 		isb();
346e159e7aSVarun Wadekar 	}
356e159e7aSVarun Wadekar 
366e159e7aSVarun Wadekar 	int at = type & AT_MASK;
376e159e7aSVarun Wadekar 	switch (at) {
386e159e7aSVarun Wadekar 	case 0:
396e159e7aSVarun Wadekar 		ats12e1r(va);
406e159e7aSVarun Wadekar 		break;
416e159e7aSVarun Wadekar 	case 1:
426e159e7aSVarun Wadekar 		ats12e1w(va);
436e159e7aSVarun Wadekar 		break;
446e159e7aSVarun Wadekar 	case 2:
456e159e7aSVarun Wadekar 		ats12e0r(va);
466e159e7aSVarun Wadekar 		break;
476e159e7aSVarun Wadekar 	case 3:
486e159e7aSVarun Wadekar 		ats12e0w(va);
496e159e7aSVarun Wadekar 		break;
506e159e7aSVarun Wadekar 	default:
516e159e7aSVarun Wadekar 		assert(0);
52*185a23ffSJonathan Wright 		break;
536e159e7aSVarun Wadekar 	}
546e159e7aSVarun Wadekar 
556e159e7aSVarun Wadekar 	/* get the (NS/S) physical address */
566e159e7aSVarun Wadekar 	isb();
576e159e7aSVarun Wadekar 	pa = read_par_el1();
586e159e7aSVarun Wadekar 
596e159e7aSVarun Wadekar 	/* Restore secure state */
606e159e7aSVarun Wadekar 	if (type & TLK_TRANSLATE_NS_VADDR) {
616e159e7aSVarun Wadekar 
626e159e7aSVarun Wadekar 		/* restore secure context */
636e159e7aSVarun Wadekar 		cm_el1_sysregs_context_restore(SECURE);
646e159e7aSVarun Wadekar 
656e159e7aSVarun Wadekar 		/* switch NS bit to start using 32-bit, secure mappings */
666e159e7aSVarun Wadekar 		write_scr(cm_get_scr_el3(SECURE));
676e159e7aSVarun Wadekar 		isb();
686e159e7aSVarun Wadekar 	}
696e159e7aSVarun Wadekar 
706e159e7aSVarun Wadekar 	return pa;
716e159e7aSVarun Wadekar }
726e159e7aSVarun Wadekar 
7322038315SVarun Wadekar /*******************************************************************************
7422038315SVarun Wadekar  * Given a secure payload entrypoint, register width, cpu id & pointer to a
7522038315SVarun Wadekar  * context data structure, this function will create a secure context ready for
7622038315SVarun Wadekar  * programming an entry into the secure payload.
7722038315SVarun Wadekar  ******************************************************************************/
7822038315SVarun Wadekar void tlkd_init_tlk_ep_state(struct entry_point_info *tlk_entry_point,
7922038315SVarun Wadekar 			    uint32_t rw,
8022038315SVarun Wadekar 			    uint64_t pc,
8122038315SVarun Wadekar 			    tlk_context_t *tlk_ctx)
8222038315SVarun Wadekar {
8322038315SVarun Wadekar 	uint32_t ep_attr, spsr;
8422038315SVarun Wadekar 
8522038315SVarun Wadekar 	/* Passing a NULL context is a critical programming error */
8622038315SVarun Wadekar 	assert(tlk_ctx);
8722038315SVarun Wadekar 	assert(tlk_entry_point);
8822038315SVarun Wadekar 	assert(pc);
8922038315SVarun Wadekar 
9022038315SVarun Wadekar 	/* Associate this context with the cpu specified */
9122038315SVarun Wadekar 	tlk_ctx->mpidr = read_mpidr_el1();
92bbbbcdaeSDavid Cunado 	clr_yield_smc_active_flag(tlk_ctx->state);
9322038315SVarun Wadekar 	cm_set_context(&tlk_ctx->cpu_ctx, SECURE);
9422038315SVarun Wadekar 
9522038315SVarun Wadekar 	if (rw == SP_AARCH64)
9622038315SVarun Wadekar 		spsr = SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
9722038315SVarun Wadekar 	else
9822038315SVarun Wadekar 		spsr = SPSR_MODE32(MODE32_svc,
9922038315SVarun Wadekar 				   SPSR_T_ARM,
10022038315SVarun Wadekar 				   read_sctlr_el3() & SCTLR_EE_BIT,
10122038315SVarun Wadekar 				   DISABLE_ALL_EXCEPTIONS);
10222038315SVarun Wadekar 
10322038315SVarun Wadekar 	/* initialise an entrypoint to set up the CPU context */
10422038315SVarun Wadekar 	ep_attr = SECURE | EP_ST_ENABLE;
10522038315SVarun Wadekar 	if (read_sctlr_el3() & SCTLR_EE_BIT)
10622038315SVarun Wadekar 		ep_attr |= EP_EE_BIG;
10722038315SVarun Wadekar 	SET_PARAM_HEAD(tlk_entry_point, PARAM_EP, VERSION_1, ep_attr);
10822038315SVarun Wadekar 
10922038315SVarun Wadekar 	tlk_entry_point->pc = pc;
11022038315SVarun Wadekar 	tlk_entry_point->spsr = spsr;
11122038315SVarun Wadekar }
11222038315SVarun Wadekar 
11322038315SVarun Wadekar /*******************************************************************************
11422038315SVarun Wadekar  * This function takes a TLK context pointer and:
11522038315SVarun Wadekar  * 1. Applies the S-EL1 system register context from tlk_ctx->cpu_ctx.
11622038315SVarun Wadekar  * 2. Saves the current C runtime state (callee saved registers) on the stack
11722038315SVarun Wadekar  *    frame and saves a reference to this state.
11822038315SVarun Wadekar  * 3. Calls el3_exit() so that the EL3 system and general purpose registers
11922038315SVarun Wadekar  *    from the tlk_ctx->cpu_ctx are used to enter the secure payload image.
12022038315SVarun Wadekar  ******************************************************************************/
12122038315SVarun Wadekar uint64_t tlkd_synchronous_sp_entry(tlk_context_t *tlk_ctx)
12222038315SVarun Wadekar {
12322038315SVarun Wadekar 	uint64_t rc;
12422038315SVarun Wadekar 
12522038315SVarun Wadekar 	/* Passing a NULL context is a critical programming error */
12622038315SVarun Wadekar 	assert(tlk_ctx);
12722038315SVarun Wadekar 	assert(tlk_ctx->c_rt_ctx == 0);
12822038315SVarun Wadekar 
12922038315SVarun Wadekar 	/* Apply the Secure EL1 system register context and switch to it */
13022038315SVarun Wadekar 	assert(cm_get_context(SECURE) == &tlk_ctx->cpu_ctx);
13122038315SVarun Wadekar 	cm_el1_sysregs_context_restore(SECURE);
13222038315SVarun Wadekar 	cm_set_next_eret_context(SECURE);
13322038315SVarun Wadekar 
13422038315SVarun Wadekar 	rc = tlkd_enter_sp(&tlk_ctx->c_rt_ctx);
13592cad5faSAntonio Nino Diaz #if ENABLE_ASSERTIONS
13622038315SVarun Wadekar 	tlk_ctx->c_rt_ctx = 0;
13722038315SVarun Wadekar #endif
13822038315SVarun Wadekar 
13922038315SVarun Wadekar 	return rc;
14022038315SVarun Wadekar }
14122038315SVarun Wadekar 
14222038315SVarun Wadekar /*******************************************************************************
14322038315SVarun Wadekar  * This function takes a TLK context pointer and:
14422038315SVarun Wadekar  * 1. Saves the S-EL1 system register context to tlk_ctx->cpu_ctx.
14522038315SVarun Wadekar  * 2. Restores the current C runtime state (callee saved registers) from the
14622038315SVarun Wadekar  *    stack frame using reference to this state saved in tlkd_enter_sp().
14722038315SVarun Wadekar  * 3. It does not need to save any general purpose or EL3 system register state
14822038315SVarun Wadekar  *    as the generic smc entry routine should have saved those.
14922038315SVarun Wadekar  ******************************************************************************/
15022038315SVarun Wadekar void tlkd_synchronous_sp_exit(tlk_context_t *tlk_ctx, uint64_t ret)
15122038315SVarun Wadekar {
15222038315SVarun Wadekar 	/* Passing a NULL context is a critical programming error */
15322038315SVarun Wadekar 	assert(tlk_ctx);
15422038315SVarun Wadekar 
15522038315SVarun Wadekar 	/* Save the Secure EL1 system register context */
15622038315SVarun Wadekar 	assert(cm_get_context(SECURE) == &tlk_ctx->cpu_ctx);
15722038315SVarun Wadekar 	cm_el1_sysregs_context_save(SECURE);
15822038315SVarun Wadekar 
15922038315SVarun Wadekar 	assert(tlk_ctx->c_rt_ctx != 0);
16022038315SVarun Wadekar 	tlkd_exit_sp(tlk_ctx->c_rt_ctx, ret);
16122038315SVarun Wadekar 
16222038315SVarun Wadekar 	/* Should never reach here */
16322038315SVarun Wadekar 	assert(0);
16422038315SVarun Wadekar }
165