xref: /rk3399_ARM-atf/services/spd/opteed/opteed_main.c (revision 51faada71a219a8b94cd8d8e423f0f22e9da4d8f)
1 /*
2  * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of ARM nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific
16  * prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 
32 /*******************************************************************************
33  * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a
34  * plug-in component to the Secure Monitor, registered as a runtime service. The
35  * SPD is expected to be a functional extension of the Secure Payload (SP) that
36  * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting
37  * the Trusted OS/Applications range to the dispatcher. The SPD will either
38  * handle the request locally or delegate it to the Secure Payload. It is also
39  * responsible for initialising and maintaining communication with the SP.
40  ******************************************************************************/
41 #include <arch_helpers.h>
42 #include <assert.h>
43 #include <bl_common.h>
44 #include <bl31.h>
45 #include <context_mgmt.h>
46 #include <debug.h>
47 #include <errno.h>
48 #include <platform.h>
49 #include <runtime_svc.h>
50 #include <stddef.h>
51 #include <uuid.h>
52 #include "opteed_private.h"
53 #include "teesmc_opteed_macros.h"
54 #include "teesmc_opteed.h"
55 
56 /*******************************************************************************
57  * Address of the entrypoint vector table in OPTEE. It is
58  * initialised once on the primary core after a cold boot.
59  ******************************************************************************/
60 optee_vectors_t *optee_vectors;
61 
62 /*******************************************************************************
63  * Array to keep track of per-cpu OPTEE state
64  ******************************************************************************/
65 optee_context_t opteed_sp_context[OPTEED_CORE_COUNT];
66 uint32_t opteed_rw;
67 
68 
69 
70 static int32_t opteed_init(void);
71 
72 /*******************************************************************************
73  * This function is the handler registered for S-EL1 interrupts by the
74  * OPTEED. It validates the interrupt and upon success arranges entry into
75  * the OPTEE at 'optee_fiq_entry()' for handling the interrupt.
76  ******************************************************************************/
77 static uint64_t opteed_sel1_interrupt_handler(uint32_t id,
78 					    uint32_t flags,
79 					    void *handle,
80 					    void *cookie)
81 {
82 	uint32_t linear_id;
83 	optee_context_t *optee_ctx;
84 
85 	/* Check the security state when the exception was generated */
86 	assert(get_interrupt_src_ss(flags) == NON_SECURE);
87 
88 	/* Sanity check the pointer to this cpu's context */
89 	assert(handle == cm_get_context(NON_SECURE));
90 
91 	/* Save the non-secure context before entering the OPTEE */
92 	cm_el1_sysregs_context_save(NON_SECURE);
93 
94 	/* Get a reference to this cpu's OPTEE context */
95 	linear_id = plat_my_core_pos();
96 	optee_ctx = &opteed_sp_context[linear_id];
97 	assert(&optee_ctx->cpu_ctx == cm_get_context(SECURE));
98 
99 	cm_set_elr_el3(SECURE, (uint64_t)&optee_vectors->fiq_entry);
100 	cm_el1_sysregs_context_restore(SECURE);
101 	cm_set_next_eret_context(SECURE);
102 
103 	/*
104 	 * Tell the OPTEE that it has to handle an FIQ (synchronously).
105 	 * Also the instruction in normal world where the interrupt was
106 	 * generated is passed for debugging purposes. It is safe to
107 	 * retrieve this address from ELR_EL3 as the secure context will
108 	 * not take effect until el3_exit().
109 	 */
110 	SMC_RET1(&optee_ctx->cpu_ctx, read_elr_el3());
111 }
112 
113 /*******************************************************************************
114  * OPTEE Dispatcher setup. The OPTEED finds out the OPTEE entrypoint and type
115  * (aarch32/aarch64) if not already known and initialises the context for entry
116  * into OPTEE for its initialization.
117  ******************************************************************************/
118 int32_t opteed_setup(void)
119 {
120 	entry_point_info_t *optee_ep_info;
121 	uint32_t linear_id;
122 
123 	linear_id = plat_my_core_pos();
124 
125 	/*
126 	 * Get information about the Secure Payload (BL32) image. Its
127 	 * absence is a critical failure.  TODO: Add support to
128 	 * conditionally include the SPD service
129 	 */
130 	optee_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
131 	if (!optee_ep_info) {
132 		WARN("No OPTEE provided by BL2 boot loader, Booting device"
133 			" without OPTEE initialization. SMC`s destined for OPTEE"
134 			" will return SMC_UNK\n");
135 		return 1;
136 	}
137 
138 	/*
139 	 * If there's no valid entry point for SP, we return a non-zero value
140 	 * signalling failure initializing the service. We bail out without
141 	 * registering any handlers
142 	 */
143 	if (!optee_ep_info->pc)
144 		return 1;
145 
146 	/*
147 	 * We could inspect the SP image and determine it's execution
148 	 * state i.e whether AArch32 or AArch64. Assuming it's AArch32
149 	 * for the time being.
150 	 */
151 	opteed_rw = OPTEE_AARCH64;
152 	opteed_init_optee_ep_state(optee_ep_info,
153 				opteed_rw,
154 				optee_ep_info->pc,
155 				&opteed_sp_context[linear_id]);
156 
157 	/*
158 	 * All OPTEED initialization done. Now register our init function with
159 	 * BL31 for deferred invocation
160 	 */
161 	bl31_register_bl32_init(&opteed_init);
162 
163 	return 0;
164 }
165 
166 /*******************************************************************************
167  * This function passes control to the OPTEE image (BL32) for the first time
168  * on the primary cpu after a cold boot. It assumes that a valid secure
169  * context has already been created by opteed_setup() which can be directly
170  * used.  It also assumes that a valid non-secure context has been
171  * initialised by PSCI so it does not need to save and restore any
172  * non-secure state. This function performs a synchronous entry into
173  * OPTEE. OPTEE passes control back to this routine through a SMC.
174  ******************************************************************************/
175 static int32_t opteed_init(void)
176 {
177 	uint32_t linear_id = plat_my_core_pos();
178 	optee_context_t *optee_ctx = &opteed_sp_context[linear_id];
179 	entry_point_info_t *optee_entry_point;
180 	uint64_t rc;
181 
182 	/*
183 	 * Get information about the OPTEE (BL32) image. Its
184 	 * absence is a critical failure.
185 	 */
186 	optee_entry_point = bl31_plat_get_next_image_ep_info(SECURE);
187 	assert(optee_entry_point);
188 
189 	cm_init_my_context(optee_entry_point);
190 
191 	/*
192 	 * Arrange for an entry into OPTEE. It will be returned via
193 	 * OPTEE_ENTRY_DONE case
194 	 */
195 	rc = opteed_synchronous_sp_entry(optee_ctx);
196 	assert(rc != 0);
197 
198 	return rc;
199 }
200 
201 
202 /*******************************************************************************
203  * This function is responsible for handling all SMCs in the Trusted OS/App
204  * range from the non-secure state as defined in the SMC Calling Convention
205  * Document. It is also responsible for communicating with the Secure
206  * payload to delegate work and return results back to the non-secure
207  * state. Lastly it will also return any information that OPTEE needs to do
208  * the work assigned to it.
209  ******************************************************************************/
210 uint64_t opteed_smc_handler(uint32_t smc_fid,
211 			 uint64_t x1,
212 			 uint64_t x2,
213 			 uint64_t x3,
214 			 uint64_t x4,
215 			 void *cookie,
216 			 void *handle,
217 			 uint64_t flags)
218 {
219 	cpu_context_t *ns_cpu_context;
220 	uint32_t linear_id = plat_my_core_pos();
221 	optee_context_t *optee_ctx = &opteed_sp_context[linear_id];
222 	uint64_t rc;
223 
224 	/*
225 	 * Determine which security state this SMC originated from
226 	 */
227 
228 	if (is_caller_non_secure(flags)) {
229 		/*
230 		 * This is a fresh request from the non-secure client.
231 		 * The parameters are in x1 and x2. Figure out which
232 		 * registers need to be preserved, save the non-secure
233 		 * state and send the request to the secure payload.
234 		 */
235 		assert(handle == cm_get_context(NON_SECURE));
236 
237 		cm_el1_sysregs_context_save(NON_SECURE);
238 
239 		/*
240 		 * We are done stashing the non-secure context. Ask the
241 		 * OPTEE to do the work now.
242 		 */
243 
244 		/*
245 		 * Verify if there is a valid context to use, copy the
246 		 * operation type and parameters to the secure context
247 		 * and jump to the fast smc entry point in the secure
248 		 * payload. Entry into S-EL1 will take place upon exit
249 		 * from this function.
250 		 */
251 		assert(&optee_ctx->cpu_ctx == cm_get_context(SECURE));
252 
253 		/* Set appropriate entry for SMC.
254 		 * We expect OPTEE to manage the PSTATE.I and PSTATE.F
255 		 * flags as appropriate.
256 		 */
257 		if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_FAST) {
258 			cm_set_elr_el3(SECURE, (uint64_t)
259 					&optee_vectors->fast_smc_entry);
260 		} else {
261 			cm_set_elr_el3(SECURE, (uint64_t)
262 					&optee_vectors->std_smc_entry);
263 		}
264 
265 		cm_el1_sysregs_context_restore(SECURE);
266 		cm_set_next_eret_context(SECURE);
267 
268 		write_ctx_reg(get_gpregs_ctx(&optee_ctx->cpu_ctx),
269 			      CTX_GPREG_X4,
270 			      read_ctx_reg(get_gpregs_ctx(handle),
271 					   CTX_GPREG_X4));
272 		write_ctx_reg(get_gpregs_ctx(&optee_ctx->cpu_ctx),
273 			      CTX_GPREG_X5,
274 			      read_ctx_reg(get_gpregs_ctx(handle),
275 					   CTX_GPREG_X5));
276 		write_ctx_reg(get_gpregs_ctx(&optee_ctx->cpu_ctx),
277 			      CTX_GPREG_X6,
278 			      read_ctx_reg(get_gpregs_ctx(handle),
279 					   CTX_GPREG_X6));
280 		/* Propagate hypervisor client ID */
281 		write_ctx_reg(get_gpregs_ctx(&optee_ctx->cpu_ctx),
282 			      CTX_GPREG_X7,
283 			      read_ctx_reg(get_gpregs_ctx(handle),
284 					   CTX_GPREG_X7));
285 
286 		SMC_RET4(&optee_ctx->cpu_ctx, smc_fid, x1, x2, x3);
287 	}
288 
289 	/*
290 	 * Returning from OPTEE
291 	 */
292 
293 	switch (smc_fid) {
294 	/*
295 	 * OPTEE has finished initialising itself after a cold boot
296 	 */
297 	case TEESMC_OPTEED_RETURN_ENTRY_DONE:
298 		/*
299 		 * Stash the OPTEE entry points information. This is done
300 		 * only once on the primary cpu
301 		 */
302 		assert(optee_vectors == NULL);
303 		optee_vectors = (optee_vectors_t *) x1;
304 
305 		if (optee_vectors) {
306 			set_optee_pstate(optee_ctx->state, OPTEE_PSTATE_ON);
307 
308 			/*
309 			 * OPTEE has been successfully initialized.
310 			 * Register power management hooks with PSCI
311 			 */
312 			psci_register_spd_pm_hook(&opteed_pm);
313 
314 			/*
315 			 * Register an interrupt handler for S-EL1 interrupts
316 			 * when generated during code executing in the
317 			 * non-secure state.
318 			 */
319 			flags = 0;
320 			set_interrupt_rm_flag(flags, NON_SECURE);
321 			rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
322 						opteed_sel1_interrupt_handler,
323 						flags);
324 			if (rc)
325 				panic();
326 		}
327 
328 		/*
329 		 * OPTEE reports completion. The OPTEED must have initiated
330 		 * the original request through a synchronous entry into
331 		 * OPTEE. Jump back to the original C runtime context.
332 		 */
333 		opteed_synchronous_sp_exit(optee_ctx, x1);
334 
335 
336 	/*
337 	 * These function IDs is used only by OP-TEE to indicate it has
338 	 * finished:
339 	 * 1. turning itself on in response to an earlier psci
340 	 *    cpu_on request
341 	 * 2. resuming itself after an earlier psci cpu_suspend
342 	 *    request.
343 	 */
344 	case TEESMC_OPTEED_RETURN_ON_DONE:
345 	case TEESMC_OPTEED_RETURN_RESUME_DONE:
346 
347 
348 	/*
349 	 * These function IDs is used only by the SP to indicate it has
350 	 * finished:
351 	 * 1. suspending itself after an earlier psci cpu_suspend
352 	 *    request.
353 	 * 2. turning itself off in response to an earlier psci
354 	 *    cpu_off request.
355 	 */
356 	case TEESMC_OPTEED_RETURN_OFF_DONE:
357 	case TEESMC_OPTEED_RETURN_SUSPEND_DONE:
358 	case TEESMC_OPTEED_RETURN_SYSTEM_OFF_DONE:
359 	case TEESMC_OPTEED_RETURN_SYSTEM_RESET_DONE:
360 
361 		/*
362 		 * OPTEE reports completion. The OPTEED must have initiated the
363 		 * original request through a synchronous entry into OPTEE.
364 		 * Jump back to the original C runtime context, and pass x1 as
365 		 * return value to the caller
366 		 */
367 		opteed_synchronous_sp_exit(optee_ctx, x1);
368 
369 	/*
370 	 * OPTEE is returning from a call or being preempted from a call, in
371 	 * either case execution should resume in the normal world.
372 	 */
373 	case TEESMC_OPTEED_RETURN_CALL_DONE:
374 		/*
375 		 * This is the result from the secure client of an
376 		 * earlier request. The results are in x0-x3. Copy it
377 		 * into the non-secure context, save the secure state
378 		 * and return to the non-secure state.
379 		 */
380 		assert(handle == cm_get_context(SECURE));
381 		cm_el1_sysregs_context_save(SECURE);
382 
383 		/* Get a reference to the non-secure context */
384 		ns_cpu_context = cm_get_context(NON_SECURE);
385 		assert(ns_cpu_context);
386 
387 		/* Restore non-secure state */
388 		cm_el1_sysregs_context_restore(NON_SECURE);
389 		cm_set_next_eret_context(NON_SECURE);
390 
391 		SMC_RET4(ns_cpu_context, x1, x2, x3, x4);
392 
393 	/*
394 	 * OPTEE has finished handling a S-EL1 FIQ interrupt. Execution
395 	 * should resume in the normal world.
396 	 */
397 	case TEESMC_OPTEED_RETURN_FIQ_DONE:
398 		/* Get a reference to the non-secure context */
399 		ns_cpu_context = cm_get_context(NON_SECURE);
400 		assert(ns_cpu_context);
401 
402 		/*
403 		 * Restore non-secure state. There is no need to save the
404 		 * secure system register context since OPTEE was supposed
405 		 * to preserve it during S-EL1 interrupt handling.
406 		 */
407 		cm_el1_sysregs_context_restore(NON_SECURE);
408 		cm_set_next_eret_context(NON_SECURE);
409 
410 		SMC_RET0((uint64_t) ns_cpu_context);
411 
412 	default:
413 		panic();
414 	}
415 }
416 
417 /* Define an OPTEED runtime service descriptor for fast SMC calls */
418 DECLARE_RT_SVC(
419 	opteed_fast,
420 
421 	OEN_TOS_START,
422 	OEN_TOS_END,
423 	SMC_TYPE_FAST,
424 	opteed_setup,
425 	opteed_smc_handler
426 );
427 
428 /* Define an OPTEED runtime service descriptor for standard SMC calls */
429 DECLARE_RT_SVC(
430 	opteed_std,
431 
432 	OEN_TOS_START,
433 	OEN_TOS_END,
434 	SMC_TYPE_STD,
435 	NULL,
436 	opteed_smc_handler
437 );
438