xref: /optee_os/core/arch/arm/tee/entry_fast.c (revision b05cd886e06dce9516b6434d3f1e0bd0029be1ac)
1 /*
2  * Copyright (c) 2015, Linaro Limited
3  * All rights reserved.
4  * Copyright (c) 2014, STMicroelectronics International N.V.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <tee/entry_fast.h>
31 #include <optee_msg.h>
32 #include <sm/optee_smc.h>
33 #include <kernel/generic_boot.h>
34 #include <kernel/tee_l2cc_mutex.h>
35 #include <kernel/misc.h>
36 #include <mm/core_mmu.h>
37 
38 static void tee_entry_get_shm_config(struct thread_smc_args *args)
39 {
40 	args->a0 = OPTEE_SMC_RETURN_OK;
41 	args->a1 = default_nsec_shm_paddr;
42 	args->a2 = default_nsec_shm_size;
43 	/* Should this be TEESMC cache attributes instead? */
44 	args->a3 = core_mmu_is_shm_cached();
45 }
46 
47 static void tee_entry_fastcall_l2cc_mutex(struct thread_smc_args *args)
48 {
49 	TEE_Result ret;
50 #ifdef ARM32
51 	paddr_t pa = 0;
52 
53 	switch (args->a1) {
54 	case OPTEE_SMC_L2CC_MUTEX_GET_ADDR:
55 		ret = tee_get_l2cc_mutex(&pa);
56 		reg_pair_from_64(pa, &args->a2, &args->a3);
57 		break;
58 	case OPTEE_SMC_L2CC_MUTEX_SET_ADDR:
59 		pa = reg_pair_to_64(args->a2, args->a3);
60 		ret = tee_set_l2cc_mutex(&pa);
61 		break;
62 	case OPTEE_SMC_L2CC_MUTEX_ENABLE:
63 		ret = tee_enable_l2cc_mutex();
64 		break;
65 	case OPTEE_SMC_L2CC_MUTEX_DISABLE:
66 		ret = tee_disable_l2cc_mutex();
67 		break;
68 	default:
69 		args->a0 = OPTEE_SMC_RETURN_EBADCMD;
70 		return;
71 	}
72 #else
73 	ret = TEE_ERROR_NOT_SUPPORTED;
74 #endif
75 	if (ret == TEE_ERROR_NOT_SUPPORTED)
76 		args->a0 = OPTEE_SMC_RETURN_UNKNOWN_FUNCTION;
77 	else if (ret)
78 		args->a0 = OPTEE_SMC_RETURN_EBADADDR;
79 	else
80 		args->a0 = OPTEE_SMC_RETURN_OK;
81 }
82 
83 static void tee_entry_exchange_capabilities(struct thread_smc_args *args)
84 {
85 	bool dyn_shm_en = false;
86 
87 	/*
88 	 * Currently we ignore OPTEE_SMC_NSEC_CAP_UNIPROCESSOR.
89 	 *
90 	 * The memory mapping of shared memory is defined as normal
91 	 * shared memory for SMP systems and normal memory for UP
92 	 * systems. Currently we map all memory as shared in secure
93 	 * world.
94 	 *
95 	 * When translation tables are created with shared bit cleared for
96 	 * uniprocessor systems we'll need to check
97 	 * OPTEE_SMC_NSEC_CAP_UNIPROCESSOR.
98 	 */
99 
100 	if (args->a1 & ~OPTEE_SMC_NSEC_CAP_UNIPROCESSOR) {
101 		/* Unknown capability. */
102 		args->a0 = OPTEE_SMC_RETURN_ENOTAVAIL;
103 		return;
104 	}
105 
106 	args->a0 = OPTEE_SMC_RETURN_OK;
107 	args->a1 = OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM;
108 
109 	dyn_shm_en = core_mmu_nsec_ddr_is_defined();
110 	if (dyn_shm_en)
111 		args->a1 |= OPTEE_SMC_SEC_CAP_DYNAMIC_SHM;
112 
113 	IMSG("Dynamic shared memory is %sabled", dyn_shm_en ? "en" : "dis");
114 }
115 
116 static void tee_entry_disable_shm_cache(struct thread_smc_args *args)
117 {
118 	uint64_t cookie;
119 
120 	if (!thread_disable_prealloc_rpc_cache(&cookie)) {
121 		args->a0 = OPTEE_SMC_RETURN_EBUSY;
122 		return;
123 	}
124 
125 	if (!cookie) {
126 		args->a0 = OPTEE_SMC_RETURN_ENOTAVAIL;
127 		return;
128 	}
129 
130 	args->a0 = OPTEE_SMC_RETURN_OK;
131 	args->a1 = cookie >> 32;
132 	args->a2 = cookie;
133 }
134 
135 static void tee_entry_enable_shm_cache(struct thread_smc_args *args)
136 {
137 	if (thread_enable_prealloc_rpc_cache())
138 		args->a0 = OPTEE_SMC_RETURN_OK;
139 	else
140 		args->a0 = OPTEE_SMC_RETURN_EBUSY;
141 }
142 
143 static void tee_entry_boot_secondary(struct thread_smc_args *args)
144 {
145 #if defined(CFG_BOOT_SECONDARY_REQUEST)
146 	if (!generic_boot_core_release(args->a1, (paddr_t)(args->a3)))
147 		args->a0 = OPTEE_SMC_RETURN_OK;
148 	else
149 		args->a0 = OPTEE_SMC_RETURN_EBADCMD;
150 #else
151 	args->a0 = OPTEE_SMC_RETURN_ENOTAVAIL;
152 #endif
153 }
154 
155 void tee_entry_fast(struct thread_smc_args *args)
156 {
157 	switch (args->a0) {
158 
159 	/* Generic functions */
160 	case OPTEE_SMC_CALLS_COUNT:
161 		tee_entry_get_api_call_count(args);
162 		break;
163 	case OPTEE_SMC_CALLS_UID:
164 		tee_entry_get_api_uuid(args);
165 		break;
166 	case OPTEE_SMC_CALLS_REVISION:
167 		tee_entry_get_api_revision(args);
168 		break;
169 	case OPTEE_SMC_CALL_GET_OS_UUID:
170 		tee_entry_get_os_uuid(args);
171 		break;
172 	case OPTEE_SMC_CALL_GET_OS_REVISION:
173 		tee_entry_get_os_revision(args);
174 		break;
175 
176 	/* OP-TEE specific SMC functions */
177 	case OPTEE_SMC_GET_SHM_CONFIG:
178 		tee_entry_get_shm_config(args);
179 		break;
180 	case OPTEE_SMC_L2CC_MUTEX:
181 		tee_entry_fastcall_l2cc_mutex(args);
182 		break;
183 	case OPTEE_SMC_EXCHANGE_CAPABILITIES:
184 		tee_entry_exchange_capabilities(args);
185 		break;
186 	case OPTEE_SMC_DISABLE_SHM_CACHE:
187 		tee_entry_disable_shm_cache(args);
188 		break;
189 	case OPTEE_SMC_ENABLE_SHM_CACHE:
190 		tee_entry_enable_shm_cache(args);
191 		break;
192 	case OPTEE_SMC_BOOT_SECONDARY:
193 		tee_entry_boot_secondary(args);
194 		break;
195 
196 	default:
197 		args->a0 = OPTEE_SMC_RETURN_UNKNOWN_FUNCTION;
198 		break;
199 	}
200 }
201 
202 size_t tee_entry_generic_get_api_call_count(void)
203 {
204 	/*
205 	 * All the different calls handled in this file. If the specific
206 	 * target has additional calls it will call this function and
207 	 * add the number of calls the target has added.
208 	 */
209 	return 9;
210 }
211 
212 void __weak tee_entry_get_api_call_count(struct thread_smc_args *args)
213 {
214 	args->a0 = tee_entry_generic_get_api_call_count();
215 }
216 
217 void __weak tee_entry_get_api_uuid(struct thread_smc_args *args)
218 {
219 	args->a0 = OPTEE_MSG_UID_0;
220 	args->a1 = OPTEE_MSG_UID_1;
221 	args->a2 = OPTEE_MSG_UID_2;
222 	args->a3 = OPTEE_MSG_UID_3;
223 }
224 
225 void __weak tee_entry_get_api_revision(struct thread_smc_args *args)
226 {
227 	args->a0 = OPTEE_MSG_REVISION_MAJOR;
228 	args->a1 = OPTEE_MSG_REVISION_MINOR;
229 }
230 
231 void __weak tee_entry_get_os_uuid(struct thread_smc_args *args)
232 {
233 	args->a0 = OPTEE_MSG_OS_OPTEE_UUID_0;
234 	args->a1 = OPTEE_MSG_OS_OPTEE_UUID_1;
235 	args->a2 = OPTEE_MSG_OS_OPTEE_UUID_2;
236 	args->a3 = OPTEE_MSG_OS_OPTEE_UUID_3;
237 }
238 
239 void __weak tee_entry_get_os_revision(struct thread_smc_args *args)
240 {
241 	args->a0 = CFG_OPTEE_REVISION_MAJOR;
242 	args->a1 = CFG_OPTEE_REVISION_MINOR;
243 }
244