xref: /rk3399_ARM-atf/services/spd/tlkd/tlkd_common.c (revision 1a29aba3673b753664e97fcfed1e3d38f138b3b7)
1 /*
2  * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <arch_helpers.h>
8 #include <assert.h>
9 #include <bl_common.h>
10 #include <context_mgmt.h>
11 #include <string.h>
12 #include "tlkd_private.h"
13 
14 #define AT_MASK		3
15 
16 /*******************************************************************************
17  * This function helps the SP to translate NS/S virtual addresses.
18  ******************************************************************************/
19 uint64_t tlkd_va_translate(uintptr_t va, int type)
20 {
21 	uint64_t pa;
22 
23 	if (type & TLK_TRANSLATE_NS_VADDR) {
24 
25 		/* save secure context */
26 		cm_el1_sysregs_context_save(SECURE);
27 
28 		/* restore non-secure context */
29 		cm_el1_sysregs_context_restore(NON_SECURE);
30 
31 		/* switch NS bit to start using 64-bit, non-secure mappings */
32 		write_scr(cm_get_scr_el3(NON_SECURE));
33 		isb();
34 	}
35 
36 	int at = type & AT_MASK;
37 	switch (at) {
38 	case 0:
39 		ats12e1r(va);
40 		break;
41 	case 1:
42 		ats12e1w(va);
43 		break;
44 	case 2:
45 		ats12e0r(va);
46 		break;
47 	case 3:
48 		ats12e0w(va);
49 		break;
50 	default:
51 		assert(0); /* Unreachable */
52 		break;
53 	}
54 
55 	/* get the (NS/S) physical address */
56 	isb();
57 	pa = read_par_el1();
58 
59 	/* Restore secure state */
60 	if (type & TLK_TRANSLATE_NS_VADDR) {
61 
62 		/* restore secure context */
63 		cm_el1_sysregs_context_restore(SECURE);
64 
65 		/* switch NS bit to start using 32-bit, secure mappings */
66 		write_scr(cm_get_scr_el3(SECURE));
67 		isb();
68 	}
69 
70 	return pa;
71 }
72 
73 /*******************************************************************************
74  * Given a secure payload entrypoint, register width, cpu id & pointer to a
75  * context data structure, this function will create a secure context ready for
76  * programming an entry into the secure payload.
77  ******************************************************************************/
78 void tlkd_init_tlk_ep_state(struct entry_point_info *tlk_entry_point,
79 			    uint32_t rw,
80 			    uint64_t pc,
81 			    tlk_context_t *tlk_ctx)
82 {
83 	uint32_t ep_attr, spsr;
84 
85 	/* Passing a NULL context is a critical programming error */
86 	assert(tlk_ctx);
87 	assert(tlk_entry_point);
88 	assert(pc);
89 
90 	/* Associate this context with the cpu specified */
91 	tlk_ctx->mpidr = read_mpidr_el1();
92 	clr_yield_smc_active_flag(tlk_ctx->state);
93 	cm_set_context(&tlk_ctx->cpu_ctx, SECURE);
94 
95 	if (rw == SP_AARCH64)
96 		spsr = SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
97 	else
98 		spsr = SPSR_MODE32(MODE32_svc,
99 				   SPSR_T_ARM,
100 				   read_sctlr_el3() & SCTLR_EE_BIT,
101 				   DISABLE_ALL_EXCEPTIONS);
102 
103 	/* initialise an entrypoint to set up the CPU context */
104 	ep_attr = SECURE | EP_ST_ENABLE;
105 	if (read_sctlr_el3() & SCTLR_EE_BIT)
106 		ep_attr |= EP_EE_BIG;
107 	SET_PARAM_HEAD(tlk_entry_point, PARAM_EP, VERSION_1, ep_attr);
108 
109 	tlk_entry_point->pc = pc;
110 	tlk_entry_point->spsr = spsr;
111 }
112 
113 /*******************************************************************************
114  * This function takes a TLK context pointer and:
115  * 1. Applies the S-EL1 system register context from tlk_ctx->cpu_ctx.
116  * 2. Saves the current C runtime state (callee saved registers) on the stack
117  *    frame and saves a reference to this state.
118  * 3. Calls el3_exit() so that the EL3 system and general purpose registers
119  *    from the tlk_ctx->cpu_ctx are used to enter the secure payload image.
120  ******************************************************************************/
121 uint64_t tlkd_synchronous_sp_entry(tlk_context_t *tlk_ctx)
122 {
123 	uint64_t rc;
124 
125 	/* Passing a NULL context is a critical programming error */
126 	assert(tlk_ctx);
127 	assert(tlk_ctx->c_rt_ctx == 0);
128 
129 	/* Apply the Secure EL1 system register context and switch to it */
130 	assert(cm_get_context(SECURE) == &tlk_ctx->cpu_ctx);
131 	cm_el1_sysregs_context_restore(SECURE);
132 	cm_set_next_eret_context(SECURE);
133 
134 	rc = tlkd_enter_sp(&tlk_ctx->c_rt_ctx);
135 #if ENABLE_ASSERTIONS
136 	tlk_ctx->c_rt_ctx = 0;
137 #endif
138 
139 	return rc;
140 }
141 
142 /*******************************************************************************
143  * This function takes a TLK context pointer and:
144  * 1. Saves the S-EL1 system register context to tlk_ctx->cpu_ctx.
145  * 2. Restores the current C runtime state (callee saved registers) from the
146  *    stack frame using reference to this state saved in tlkd_enter_sp().
147  * 3. It does not need to save any general purpose or EL3 system register state
148  *    as the generic smc entry routine should have saved those.
149  ******************************************************************************/
150 void tlkd_synchronous_sp_exit(tlk_context_t *tlk_ctx, uint64_t ret)
151 {
152 	/* Passing a NULL context is a critical programming error */
153 	assert(tlk_ctx);
154 
155 	/* Save the Secure EL1 system register context */
156 	assert(cm_get_context(SECURE) == &tlk_ctx->cpu_ctx);
157 	cm_el1_sysregs_context_save(SECURE);
158 
159 	assert(tlk_ctx->c_rt_ctx != 0);
160 	tlkd_exit_sp(tlk_ctx->c_rt_ctx, ret);
161 
162 	/* Should never reach here */
163 	assert(0);
164 }
165