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