blob: b8b67fadc85636b00b2fb336faddffb1d458ea1d [file] [log] [blame]
Achin Gupta375f5382014-02-18 18:12:48 +00001/*
Soby Mathewda43b662015-07-08 21:45:46 +01002 * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved.
Achin Gupta375f5382014-02-18 18:12:48 +00003 *
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 ******************************************************************************/
Achin Gupta375f5382014-02-18 18:12:48 +000041#include <arch_helpers.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010042#include <assert.h>
43#include <bl_common.h>
44#include <bl31.h>
Achin Gupta375f5382014-02-18 18:12:48 +000045#include <context_mgmt.h>
Achin Guptaaeaab682014-05-09 13:21:31 +010046#include <debug.h>
47#include <errno.h>
48#include <platform.h>
Achin Gupta375f5382014-02-18 18:12:48 +000049#include <runtime_svc.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010050#include <stddef.h>
Soby Mathew47903c02015-01-13 15:48:26 +000051#include <string.h>
Achin Gupta375f5382014-02-18 18:12:48 +000052#include <tsp.h>
Jeenu Viswambharandf1ddb52014-02-28 11:23:35 +000053#include <uuid.h>
Dan Handley714a0d22014-04-09 13:13:04 +010054#include "tspd_private.h"
Achin Gupta375f5382014-02-18 18:12:48 +000055
56/*******************************************************************************
Andrew Thoelke891c4ca2014-05-20 21:43:27 +010057 * Address of the entrypoint vector table in the Secure Payload. It is
58 * initialised once on the primary core after a cold boot.
Achin Gupta375f5382014-02-18 18:12:48 +000059 ******************************************************************************/
Andrew Thoelke891c4ca2014-05-20 21:43:27 +010060tsp_vectors_t *tsp_vectors;
Achin Gupta375f5382014-02-18 18:12:48 +000061
62/*******************************************************************************
63 * Array to keep track of per-cpu Secure Payload state
64 ******************************************************************************/
Dan Handleye2712bc2014-04-10 15:37:22 +010065tsp_context_t tspd_sp_context[TSPD_CORE_COUNT];
Achin Gupta375f5382014-02-18 18:12:48 +000066
Jeenu Viswambharan7f366602014-02-20 17:11:00 +000067
Jeenu Viswambharandf1ddb52014-02-28 11:23:35 +000068/* TSP UID */
69DEFINE_SVC_UUID(tsp_uuid,
70 0x5b3056a0, 0x3291, 0x427b, 0x98, 0x11,
71 0x71, 0x68, 0xca, 0x50, 0xf3, 0xfa);
72
Vikram Kanigirid8c9d262014-05-16 18:48:12 +010073int32_t tspd_init(void);
Jeenu Viswambharan7f366602014-02-20 17:11:00 +000074
Soby Mathew47903c02015-01-13 15:48:26 +000075uint64_t tspd_handle_sp_preemption(void *handle)
76{
77 cpu_context_t *ns_cpu_context;
78 assert(handle == cm_get_context(SECURE));
79 cm_el1_sysregs_context_save(SECURE);
80 /* Get a reference to the non-secure context */
81 ns_cpu_context = cm_get_context(NON_SECURE);
82 assert(ns_cpu_context);
83
84 /*
85 * Restore non-secure state. The secure system
86 * register context will be saved when required.
87 */
88 cm_el1_sysregs_context_restore(NON_SECURE);
89 cm_set_next_eret_context(NON_SECURE);
90
91 SMC_RET1(ns_cpu_context, SMC_PREEMPTED);
92}
Achin Guptaaeaab682014-05-09 13:21:31 +010093/*******************************************************************************
94 * This function is the handler registered for S-EL1 interrupts by the TSPD. It
95 * validates the interrupt and upon success arranges entry into the TSP at
96 * 'tsp_fiq_entry()' for handling the interrupt.
97 ******************************************************************************/
98static uint64_t tspd_sel1_interrupt_handler(uint32_t id,
99 uint32_t flags,
100 void *handle,
101 void *cookie)
102{
103 uint32_t linear_id;
Achin Guptaaeaab682014-05-09 13:21:31 +0100104 tsp_context_t *tsp_ctx;
105
106 /* Check the security state when the exception was generated */
107 assert(get_interrupt_src_ss(flags) == NON_SECURE);
108
109#if IMF_READ_INTERRUPT_ID
110 /* Check the security status of the interrupt */
Soby Mathew93c89ec2014-05-28 17:14:36 +0100111 assert(plat_ic_get_interrupt_type(id) == INTR_TYPE_S_EL1);
Achin Guptaaeaab682014-05-09 13:21:31 +0100112#endif
113
114 /* Sanity check the pointer to this cpu's context */
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100115 assert(handle == cm_get_context(NON_SECURE));
Achin Guptaaeaab682014-05-09 13:21:31 +0100116
117 /* Save the non-secure context before entering the TSP */
118 cm_el1_sysregs_context_save(NON_SECURE);
119
120 /* Get a reference to this cpu's TSP context */
Soby Mathewda43b662015-07-08 21:45:46 +0100121 linear_id = plat_my_core_pos();
Achin Guptaaeaab682014-05-09 13:21:31 +0100122 tsp_ctx = &tspd_sp_context[linear_id];
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100123 assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE));
Achin Guptaaeaab682014-05-09 13:21:31 +0100124
125 /*
126 * Determine if the TSP was previously preempted. Its last known
127 * context has to be preserved in this case.
128 * The TSP should return control to the TSPD after handling this
129 * FIQ. Preserve essential EL3 context to allow entry into the
130 * TSP at the FIQ entry point using the 'cpu_context' structure.
131 * There is no need to save the secure system register context
132 * since the TSP is supposed to preserve it during S-EL1 interrupt
133 * handling.
134 */
135 if (get_std_smc_active_flag(tsp_ctx->state)) {
136 tsp_ctx->saved_spsr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx,
137 CTX_SPSR_EL3);
138 tsp_ctx->saved_elr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx,
139 CTX_ELR_EL3);
Soby Mathew47903c02015-01-13 15:48:26 +0000140#if TSPD_ROUTE_IRQ_TO_EL3
141 /*Need to save the previously interrupted secure context */
142 memcpy(&tsp_ctx->sp_ctx, &tsp_ctx->cpu_ctx, TSPD_SP_CTX_SIZE);
143#endif
Achin Guptaaeaab682014-05-09 13:21:31 +0100144 }
145
Achin Guptaaeaab682014-05-09 13:21:31 +0100146 cm_el1_sysregs_context_restore(SECURE);
Andrew Thoelke4e126072014-06-04 21:10:52 +0100147 cm_set_elr_spsr_el3(SECURE, (uint64_t) &tsp_vectors->fiq_entry,
148 SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS));
Soby Mathew47903c02015-01-13 15:48:26 +0000149
Achin Guptaaeaab682014-05-09 13:21:31 +0100150 cm_set_next_eret_context(SECURE);
151
152 /*
153 * Tell the TSP that it has to handle an FIQ synchronously. Also the
154 * instruction in normal world where the interrupt was generated is
155 * passed for debugging purposes. It is safe to retrieve this address
156 * from ELR_EL3 as the secure context will not take effect until
157 * el3_exit().
158 */
159 SMC_RET2(&tsp_ctx->cpu_ctx, TSP_HANDLE_FIQ_AND_RETURN, read_elr_el3());
160}
Soby Mathew47903c02015-01-13 15:48:26 +0000161
162#if TSPD_ROUTE_IRQ_TO_EL3
163/*******************************************************************************
164 * This function is the handler registered for S-EL1 interrupts by the TSPD. It
165 * validates the interrupt and upon success arranges entry into the TSP at
166 * 'tsp_fiq_entry()' for handling the interrupt.
167 ******************************************************************************/
168static uint64_t tspd_ns_interrupt_handler(uint32_t id,
169 uint32_t flags,
170 void *handle,
171 void *cookie)
172{
173 /* Check the security state when the exception was generated */
174 assert(get_interrupt_src_ss(flags) == SECURE);
175
176#if IMF_READ_INTERRUPT_ID
177 /* Check the security status of the interrupt */
178 assert(plat_ic_get_interrupt_type(id) == INTR_TYPE_NS);
179#endif
180 /*
181 * Disable the routing of NS interrupts from secure world to EL3 while
182 * interrupted on this core.
183 */
184 disable_intr_rm_local(INTR_TYPE_NS, SECURE);
185
186 return tspd_handle_sp_preemption(handle);
187}
188#endif
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000189
Achin Gupta375f5382014-02-18 18:12:48 +0000190/*******************************************************************************
191 * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type
192 * (aarch32/aarch64) if not already known and initialises the context for entry
193 * into the SP for its initialisation.
194 ******************************************************************************/
195int32_t tspd_setup(void)
196{
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +0100197 entry_point_info_t *tsp_ep_info;
Achin Gupta375f5382014-02-18 18:12:48 +0000198 uint32_t linear_id;
199
Soby Mathewda43b662015-07-08 21:45:46 +0100200 linear_id = plat_my_core_pos();
Achin Gupta375f5382014-02-18 18:12:48 +0000201
202 /*
203 * Get information about the Secure Payload (BL32) image. Its
204 * absence is a critical failure. TODO: Add support to
205 * conditionally include the SPD service
206 */
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +0100207 tsp_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
208 if (!tsp_ep_info) {
209 WARN("No TSP provided by BL2 boot loader, Booting device"
210 " without TSP initialization. SMC`s destined for TSP"
211 " will return SMC_UNK\n");
212 return 1;
213 }
Achin Gupta375f5382014-02-18 18:12:48 +0000214
215 /*
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000216 * If there's no valid entry point for SP, we return a non-zero value
217 * signalling failure initializing the service. We bail out without
218 * registering any handlers
219 */
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +0100220 if (!tsp_ep_info->pc)
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000221 return 1;
222
223 /*
Achin Gupta375f5382014-02-18 18:12:48 +0000224 * We could inspect the SP image and determine it's execution
225 * state i.e whether AArch32 or AArch64. Assuming it's AArch64
226 * for the time being.
227 */
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +0100228 tspd_init_tsp_ep_state(tsp_ep_info,
229 TSP_AARCH64,
230 tsp_ep_info->pc,
231 &tspd_sp_context[linear_id]);
Achin Gupta375f5382014-02-18 18:12:48 +0000232
Vikram Kanigiri4e813412014-07-15 16:49:22 +0100233#if TSP_INIT_ASYNC
234 bl31_set_next_image_type(SECURE);
235#else
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000236 /*
237 * All TSPD initialization done. Now register our init function with
238 * BL31 for deferred invocation
239 */
240 bl31_register_bl32_init(&tspd_init);
Vikram Kanigiri4e813412014-07-15 16:49:22 +0100241#endif
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +0100242 return 0;
Achin Gupta375f5382014-02-18 18:12:48 +0000243}
244
245/*******************************************************************************
246 * This function passes control to the Secure Payload image (BL32) for the first
247 * time on the primary cpu after a cold boot. It assumes that a valid secure
248 * context has already been created by tspd_setup() which can be directly used.
249 * It also assumes that a valid non-secure context has been initialised by PSCI
250 * so it does not need to save and restore any non-secure state. This function
251 * performs a synchronous entry into the Secure payload. The SP passes control
Vikram Kanigirid8c9d262014-05-16 18:48:12 +0100252 * back to this routine through a SMC.
Achin Gupta375f5382014-02-18 18:12:48 +0000253 ******************************************************************************/
Vikram Kanigirid8c9d262014-05-16 18:48:12 +0100254int32_t tspd_init(void)
Achin Gupta375f5382014-02-18 18:12:48 +0000255{
Soby Mathewda43b662015-07-08 21:45:46 +0100256 uint32_t linear_id = plat_my_core_pos();
Dan Handleye2712bc2014-04-10 15:37:22 +0100257 tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +0100258 entry_point_info_t *tsp_entry_point;
Vikram Kanigiri4e813412014-07-15 16:49:22 +0100259 uint64_t rc;
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +0100260
261 /*
262 * Get information about the Secure Payload (BL32) image. Its
263 * absence is a critical failure.
264 */
265 tsp_entry_point = bl31_plat_get_next_image_ep_info(SECURE);
266 assert(tsp_entry_point);
267
Soby Mathewda43b662015-07-08 21:45:46 +0100268 cm_init_my_context(tsp_entry_point);
Achin Gupta375f5382014-02-18 18:12:48 +0000269
270 /*
Vikram Kanigiri4e813412014-07-15 16:49:22 +0100271 * Arrange for an entry into the test secure payload. It will be
272 * returned via TSP_ENTRY_DONE case
Achin Gupta607084e2014-02-09 18:24:19 +0000273 */
Achin Gupta375f5382014-02-18 18:12:48 +0000274 rc = tspd_synchronous_sp_entry(tsp_ctx);
275 assert(rc != 0);
Achin Guptaaeaab682014-05-09 13:21:31 +0100276
Achin Gupta375f5382014-02-18 18:12:48 +0000277 return rc;
278}
279
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000280
Achin Gupta375f5382014-02-18 18:12:48 +0000281/*******************************************************************************
282 * This function is responsible for handling all SMCs in the Trusted OS/App
283 * range from the non-secure state as defined in the SMC Calling Convention
284 * Document. It is also responsible for communicating with the Secure payload
285 * to delegate work and return results back to the non-secure state. Lastly it
286 * will also return any information that the secure payload needs to do the
287 * work assigned to it.
288 ******************************************************************************/
289uint64_t tspd_smc_handler(uint32_t smc_fid,
290 uint64_t x1,
291 uint64_t x2,
292 uint64_t x3,
293 uint64_t x4,
294 void *cookie,
295 void *handle,
296 uint64_t flags)
297{
Dan Handleye2712bc2014-04-10 15:37:22 +0100298 cpu_context_t *ns_cpu_context;
Soby Mathewda43b662015-07-08 21:45:46 +0100299 uint32_t linear_id = plat_my_core_pos(), ns;
Dan Handleye2712bc2014-04-10 15:37:22 +0100300 tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
Vikram Kanigiri4e813412014-07-15 16:49:22 +0100301 uint64_t rc;
302#if TSP_INIT_ASYNC
303 entry_point_info_t *next_image_info;
304#endif
Achin Gupta375f5382014-02-18 18:12:48 +0000305
306 /* Determine which security state this SMC originated from */
307 ns = is_caller_non_secure(flags);
308
309 switch (smc_fid) {
310
311 /*
Soby Mathew9f71f702014-05-09 20:49:17 +0100312 * This function ID is used by TSP to indicate that it was
313 * preempted by a normal world IRQ.
314 *
315 */
316 case TSP_PREEMPTED:
317 if (ns)
318 SMC_RET1(handle, SMC_UNK);
319
Soby Mathew47903c02015-01-13 15:48:26 +0000320 return tspd_handle_sp_preemption(handle);
Soby Mathew9f71f702014-05-09 20:49:17 +0100321
322 /*
Achin Guptaaeaab682014-05-09 13:21:31 +0100323 * This function ID is used only by the TSP to indicate that it has
324 * finished handling a S-EL1 FIQ interrupt. Execution should resume
325 * in the normal world.
326 */
327 case TSP_HANDLED_S_EL1_FIQ:
328 if (ns)
329 SMC_RET1(handle, SMC_UNK);
330
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100331 assert(handle == cm_get_context(SECURE));
Achin Guptaaeaab682014-05-09 13:21:31 +0100332
333 /*
334 * Restore the relevant EL3 state which saved to service
335 * this SMC.
336 */
337 if (get_std_smc_active_flag(tsp_ctx->state)) {
338 SMC_SET_EL3(&tsp_ctx->cpu_ctx,
339 CTX_SPSR_EL3,
340 tsp_ctx->saved_spsr_el3);
341 SMC_SET_EL3(&tsp_ctx->cpu_ctx,
342 CTX_ELR_EL3,
343 tsp_ctx->saved_elr_el3);
Soby Mathew47903c02015-01-13 15:48:26 +0000344#if TSPD_ROUTE_IRQ_TO_EL3
345 /*
346 * Need to restore the previously interrupted
347 * secure context.
348 */
349 memcpy(&tsp_ctx->cpu_ctx, &tsp_ctx->sp_ctx,
350 TSPD_SP_CTX_SIZE);
351#endif
Achin Guptaaeaab682014-05-09 13:21:31 +0100352 }
353
354 /* Get a reference to the non-secure context */
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100355 ns_cpu_context = cm_get_context(NON_SECURE);
Achin Guptaaeaab682014-05-09 13:21:31 +0100356 assert(ns_cpu_context);
357
358 /*
359 * Restore non-secure state. There is no need to save the
360 * secure system register context since the TSP was supposed
361 * to preserve it during S-EL1 interrupt handling.
362 */
363 cm_el1_sysregs_context_restore(NON_SECURE);
364 cm_set_next_eret_context(NON_SECURE);
365
366 SMC_RET0((uint64_t) ns_cpu_context);
367
368
369 /*
370 * This function ID is used only by the TSP to indicate that it was
371 * interrupted due to a EL3 FIQ interrupt. Execution should resume
372 * in the normal world.
373 */
374 case TSP_EL3_FIQ:
375 if (ns)
376 SMC_RET1(handle, SMC_UNK);
377
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100378 assert(handle == cm_get_context(SECURE));
Achin Guptaaeaab682014-05-09 13:21:31 +0100379
380 /* Assert that standard SMC execution has been preempted */
381 assert(get_std_smc_active_flag(tsp_ctx->state));
382
383 /* Save the secure system register state */
384 cm_el1_sysregs_context_save(SECURE);
385
386 /* Get a reference to the non-secure context */
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100387 ns_cpu_context = cm_get_context(NON_SECURE);
Achin Guptaaeaab682014-05-09 13:21:31 +0100388 assert(ns_cpu_context);
389
390 /* Restore non-secure state */
391 cm_el1_sysregs_context_restore(NON_SECURE);
392 cm_set_next_eret_context(NON_SECURE);
393
394 SMC_RET1(ns_cpu_context, TSP_EL3_FIQ);
395
396
397 /*
Achin Gupta375f5382014-02-18 18:12:48 +0000398 * This function ID is used only by the SP to indicate it has
399 * finished initialising itself after a cold boot
400 */
401 case TSP_ENTRY_DONE:
402 if (ns)
403 SMC_RET1(handle, SMC_UNK);
404
405 /*
406 * Stash the SP entry points information. This is done
407 * only once on the primary cpu
408 */
Andrew Thoelke891c4ca2014-05-20 21:43:27 +0100409 assert(tsp_vectors == NULL);
410 tsp_vectors = (tsp_vectors_t *) x1;
Achin Gupta375f5382014-02-18 18:12:48 +0000411
Vikram Kanigiri4e813412014-07-15 16:49:22 +0100412 if (tsp_vectors) {
413 set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON);
414
415 /*
416 * TSP has been successfully initialized. Register power
417 * managemnt hooks with PSCI
418 */
419 psci_register_spd_pm_hook(&tspd_pm);
420
421 /*
422 * Register an interrupt handler for S-EL1 interrupts
423 * when generated during code executing in the
424 * non-secure state.
425 */
426 flags = 0;
427 set_interrupt_rm_flag(flags, NON_SECURE);
428 rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
429 tspd_sel1_interrupt_handler,
430 flags);
431 if (rc)
432 panic();
Soby Mathew47903c02015-01-13 15:48:26 +0000433
434#if TSPD_ROUTE_IRQ_TO_EL3
435 /*
436 * Register an interrupt handler for NS interrupts when
437 * generated during code executing in secure state are
438 * routed to EL3.
439 */
440 flags = 0;
441 set_interrupt_rm_flag(flags, SECURE);
442
443 rc = register_interrupt_type_handler(INTR_TYPE_NS,
444 tspd_ns_interrupt_handler,
445 flags);
446 if (rc)
447 panic();
448
449 /*
450 * Disable the interrupt NS locally since it will be enabled globally
Soby Mathewda43b662015-07-08 21:45:46 +0100451 * within cm_init_my_context.
Soby Mathew47903c02015-01-13 15:48:26 +0000452 */
453 disable_intr_rm_local(INTR_TYPE_NS, SECURE);
454#endif
Vikram Kanigiri4e813412014-07-15 16:49:22 +0100455 }
456
457
458#if TSP_INIT_ASYNC
459 /* Save the Secure EL1 system register context */
460 assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx);
461 cm_el1_sysregs_context_save(SECURE);
462
463 /* Program EL3 registers to enable entry into the next EL */
464 next_image_info = bl31_plat_get_next_image_ep_info(NON_SECURE);
465 assert(next_image_info);
466 assert(NON_SECURE ==
467 GET_SECURITY_STATE(next_image_info->h.attr));
468
Soby Mathewda43b662015-07-08 21:45:46 +0100469 cm_init_my_context(next_image_info);
Vikram Kanigiri4e813412014-07-15 16:49:22 +0100470 cm_prepare_el3_exit(NON_SECURE);
471 SMC_RET0(cm_get_context(NON_SECURE));
472#else
Achin Gupta375f5382014-02-18 18:12:48 +0000473 /*
474 * SP reports completion. The SPD must have initiated
475 * the original request through a synchronous entry
476 * into the SP. Jump back to the original C runtime
477 * context.
478 */
Achin Gupta916a2c12014-02-09 23:11:46 +0000479 tspd_synchronous_sp_exit(tsp_ctx, x1);
Vikram Kanigiri4e813412014-07-15 16:49:22 +0100480#endif
Achin Gupta375f5382014-02-18 18:12:48 +0000481
Achin Gupta607084e2014-02-09 18:24:19 +0000482 /*
483 * These function IDs is used only by the SP to indicate it has
484 * finished:
485 * 1. turning itself on in response to an earlier psci
486 * cpu_on request
487 * 2. resuming itself after an earlier psci cpu_suspend
488 * request.
489 */
490 case TSP_ON_DONE:
491 case TSP_RESUME_DONE:
492
493 /*
494 * These function IDs is used only by the SP to indicate it has
495 * finished:
496 * 1. suspending itself after an earlier psci cpu_suspend
497 * request.
498 * 2. turning itself off in response to an earlier psci
499 * cpu_off request.
500 */
501 case TSP_OFF_DONE:
502 case TSP_SUSPEND_DONE:
Juan Castillo4dc4a472014-08-12 11:17:06 +0100503 case TSP_SYSTEM_OFF_DONE:
504 case TSP_SYSTEM_RESET_DONE:
Achin Gupta607084e2014-02-09 18:24:19 +0000505 if (ns)
506 SMC_RET1(handle, SMC_UNK);
507
508 /*
509 * SP reports completion. The SPD must have initiated the
510 * original request through a synchronous entry into the SP.
511 * Jump back to the original C runtime context, and pass x1 as
512 * return value to the caller
513 */
Achin Gupta916a2c12014-02-09 23:11:46 +0000514 tspd_synchronous_sp_exit(tsp_ctx, x1);
Achin Gupta607084e2014-02-09 18:24:19 +0000515
Achin Gupta916a2c12014-02-09 23:11:46 +0000516 /*
517 * Request from non-secure client to perform an
518 * arithmetic operation or response from secure
519 * payload to an earlier request.
520 */
Soby Mathew9f71f702014-05-09 20:49:17 +0100521 case TSP_FAST_FID(TSP_ADD):
522 case TSP_FAST_FID(TSP_SUB):
523 case TSP_FAST_FID(TSP_MUL):
524 case TSP_FAST_FID(TSP_DIV):
525
526 case TSP_STD_FID(TSP_ADD):
527 case TSP_STD_FID(TSP_SUB):
528 case TSP_STD_FID(TSP_MUL):
529 case TSP_STD_FID(TSP_DIV):
Achin Gupta916a2c12014-02-09 23:11:46 +0000530 if (ns) {
531 /*
532 * This is a fresh request from the non-secure client.
533 * The parameters are in x1 and x2. Figure out which
534 * registers need to be preserved, save the non-secure
535 * state and send the request to the secure payload.
536 */
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100537 assert(handle == cm_get_context(NON_SECURE));
Soby Mathew9f71f702014-05-09 20:49:17 +0100538
539 /* Check if we are already preempted */
540 if (get_std_smc_active_flag(tsp_ctx->state))
541 SMC_RET1(handle, SMC_UNK);
542
Achin Gupta916a2c12014-02-09 23:11:46 +0000543 cm_el1_sysregs_context_save(NON_SECURE);
544
545 /* Save x1 and x2 for use by TSP_GET_ARGS call below */
Soby Mathew9f71f702014-05-09 20:49:17 +0100546 store_tsp_args(tsp_ctx, x1, x2);
Achin Gupta916a2c12014-02-09 23:11:46 +0000547
548 /*
549 * We are done stashing the non-secure context. Ask the
550 * secure payload to do the work now.
551 */
552
553 /*
554 * Verify if there is a valid context to use, copy the
555 * operation type and parameters to the secure context
556 * and jump to the fast smc entry point in the secure
557 * payload. Entry into S-EL1 will take place upon exit
558 * from this function.
559 */
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100560 assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE));
Soby Mathew9f71f702014-05-09 20:49:17 +0100561
562 /* Set appropriate entry for SMC.
563 * We expect the TSP to manage the PSTATE.I and PSTATE.F
564 * flags as appropriate.
565 */
566 if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_FAST) {
567 cm_set_elr_el3(SECURE, (uint64_t)
Andrew Thoelke891c4ca2014-05-20 21:43:27 +0100568 &tsp_vectors->fast_smc_entry);
Soby Mathew9f71f702014-05-09 20:49:17 +0100569 } else {
570 set_std_smc_active_flag(tsp_ctx->state);
571 cm_set_elr_el3(SECURE, (uint64_t)
Andrew Thoelke891c4ca2014-05-20 21:43:27 +0100572 &tsp_vectors->std_smc_entry);
Soby Mathew47903c02015-01-13 15:48:26 +0000573#if TSPD_ROUTE_IRQ_TO_EL3
574 /*
575 * Enable the routing of NS interrupts to EL3
576 * during STD SMC processing on this core.
577 */
578 enable_intr_rm_local(INTR_TYPE_NS, SECURE);
579#endif
Soby Mathew9f71f702014-05-09 20:49:17 +0100580 }
581
Achin Gupta916a2c12014-02-09 23:11:46 +0000582 cm_el1_sysregs_context_restore(SECURE);
583 cm_set_next_eret_context(SECURE);
Soby Mathew9f71f702014-05-09 20:49:17 +0100584 SMC_RET3(&tsp_ctx->cpu_ctx, smc_fid, x1, x2);
Achin Gupta916a2c12014-02-09 23:11:46 +0000585 } else {
586 /*
587 * This is the result from the secure client of an
Soby Mathew9f71f702014-05-09 20:49:17 +0100588 * earlier request. The results are in x1-x3. Copy it
Achin Gupta916a2c12014-02-09 23:11:46 +0000589 * into the non-secure context, save the secure state
590 * and return to the non-secure state.
591 */
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100592 assert(handle == cm_get_context(SECURE));
Achin Gupta916a2c12014-02-09 23:11:46 +0000593 cm_el1_sysregs_context_save(SECURE);
594
595 /* Get a reference to the non-secure context */
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100596 ns_cpu_context = cm_get_context(NON_SECURE);
Achin Gupta916a2c12014-02-09 23:11:46 +0000597 assert(ns_cpu_context);
Achin Gupta916a2c12014-02-09 23:11:46 +0000598
599 /* Restore non-secure state */
600 cm_el1_sysregs_context_restore(NON_SECURE);
601 cm_set_next_eret_context(NON_SECURE);
Soby Mathew47903c02015-01-13 15:48:26 +0000602 if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_STD) {
Soby Mathew9f71f702014-05-09 20:49:17 +0100603 clr_std_smc_active_flag(tsp_ctx->state);
Soby Mathew47903c02015-01-13 15:48:26 +0000604#if TSPD_ROUTE_IRQ_TO_EL3
605 /*
606 * Disable the routing of NS interrupts to EL3
607 * after STD SMC processing is finished on this
608 * core.
609 */
610 disable_intr_rm_local(INTR_TYPE_NS, SECURE);
611#endif
612 }
613
Soby Mathew9f71f702014-05-09 20:49:17 +0100614 SMC_RET3(ns_cpu_context, x1, x2, x3);
Achin Gupta916a2c12014-02-09 23:11:46 +0000615 }
616
617 break;
618
619 /*
Soby Mathew9f71f702014-05-09 20:49:17 +0100620 * Request from non secure world to resume the preempted
621 * Standard SMC call.
622 */
623 case TSP_FID_RESUME:
Soby Mathew3d578512014-05-27 10:20:01 +0100624 /* RESUME should be invoked only by normal world */
625 if (!ns) {
626 assert(0);
627 break;
628 }
Soby Mathew9f71f702014-05-09 20:49:17 +0100629
Soby Mathew3d578512014-05-27 10:20:01 +0100630 /*
631 * This is a resume request from the non-secure client.
632 * save the non-secure state and send the request to
633 * the secure payload.
634 */
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100635 assert(handle == cm_get_context(NON_SECURE));
Soby Mathew9f71f702014-05-09 20:49:17 +0100636
Soby Mathew3d578512014-05-27 10:20:01 +0100637 /* Check if we are already preempted before resume */
638 if (!get_std_smc_active_flag(tsp_ctx->state))
639 SMC_RET1(handle, SMC_UNK);
Soby Mathew9f71f702014-05-09 20:49:17 +0100640
Soby Mathew3d578512014-05-27 10:20:01 +0100641 cm_el1_sysregs_context_save(NON_SECURE);
Soby Mathew9f71f702014-05-09 20:49:17 +0100642
Soby Mathew3d578512014-05-27 10:20:01 +0100643 /*
644 * We are done stashing the non-secure context. Ask the
645 * secure payload to do the work now.
646 */
Soby Mathew47903c02015-01-13 15:48:26 +0000647#if TSPD_ROUTE_IRQ_TO_EL3
648 /*
649 * Enable the routing of NS interrupts to EL3 during resumption
650 * of STD SMC call on this core.
651 */
652 enable_intr_rm_local(INTR_TYPE_NS, SECURE);
653#endif
654
655
Soby Mathew9f71f702014-05-09 20:49:17 +0100656
Soby Mathew3d578512014-05-27 10:20:01 +0100657 /* We just need to return to the preempted point in
658 * TSP and the execution will resume as normal.
659 */
660 cm_el1_sysregs_context_restore(SECURE);
661 cm_set_next_eret_context(SECURE);
662 SMC_RET0(&tsp_ctx->cpu_ctx);
Soby Mathew9f71f702014-05-09 20:49:17 +0100663
664 /*
Achin Gupta916a2c12014-02-09 23:11:46 +0000665 * This is a request from the secure payload for more arguments
666 * for an ongoing arithmetic operation requested by the
667 * non-secure world. Simply return the arguments from the non-
668 * secure client in the original call.
669 */
670 case TSP_GET_ARGS:
671 if (ns)
672 SMC_RET1(handle, SMC_UNK);
673
Soby Mathew9f71f702014-05-09 20:49:17 +0100674 get_tsp_args(tsp_ctx, x1, x2);
675 SMC_RET2(handle, x1, x2);
Achin Gupta916a2c12014-02-09 23:11:46 +0000676
Jeenu Viswambharandf1ddb52014-02-28 11:23:35 +0000677 case TOS_CALL_COUNT:
678 /*
679 * Return the number of service function IDs implemented to
680 * provide service to non-secure
681 */
682 SMC_RET1(handle, TSP_NUM_FID);
683
684 case TOS_UID:
685 /* Return TSP UID to the caller */
686 SMC_UUID_RET(handle, tsp_uuid);
687
688 case TOS_CALL_VERSION:
689 /* Return the version of current implementation */
690 SMC_RET2(handle, TSP_VERSION_MAJOR, TSP_VERSION_MINOR);
691
Achin Gupta375f5382014-02-18 18:12:48 +0000692 default:
Achin Gupta607084e2014-02-09 18:24:19 +0000693 break;
Achin Gupta375f5382014-02-18 18:12:48 +0000694 }
695
Achin Gupta607084e2014-02-09 18:24:19 +0000696 SMC_RET1(handle, SMC_UNK);
Achin Gupta375f5382014-02-18 18:12:48 +0000697}
698
Soby Mathew9f71f702014-05-09 20:49:17 +0100699/* Define a SPD runtime service descriptor for fast SMC calls */
Achin Gupta375f5382014-02-18 18:12:48 +0000700DECLARE_RT_SVC(
Soby Mathew9f71f702014-05-09 20:49:17 +0100701 tspd_fast,
Achin Gupta375f5382014-02-18 18:12:48 +0000702
703 OEN_TOS_START,
704 OEN_TOS_END,
705 SMC_TYPE_FAST,
706 tspd_setup,
707 tspd_smc_handler
708);
Soby Mathew9f71f702014-05-09 20:49:17 +0100709
710/* Define a SPD runtime service descriptor for standard SMC calls */
711DECLARE_RT_SVC(
712 tspd_std,
713
714 OEN_TOS_START,
715 OEN_TOS_END,
716 SMC_TYPE_STD,
717 NULL,
718 tspd_smc_handler
719);