tspd_main.c 24.8 KB
Newer Older
1
/*
2
 * Copyright (c) 2013-2021, ARM Limited and Contributors. All rights reserved.
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
7
8
9
10
11
12
13
14
15
16
 */


/*******************************************************************************
 * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a
 * plug-in component to the Secure Monitor, registered as a runtime service. The
 * SPD is expected to be a functional extension of the Secure Payload (SP) that
 * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting
 * the Trusted OS/Applications range to the dispatcher. The SPD will either
 * handle the request locally or delegate it to the Secure Payload. It is also
 * responsible for initialising and maintaining communication with the SP.
 ******************************************************************************/
17
#include <assert.h>
18
#include <errno.h>
19
#include <stddef.h>
20
#include <string.h>
21
22
23
24
25
26
27
28
29
30
31
32

#include <arch_helpers.h>
#include <bl31/bl31.h>
#include <bl31/ehf.h>
#include <bl32/tsp/tsp.h>
#include <common/bl_common.h>
#include <common/debug.h>
#include <common/runtime_svc.h>
#include <lib/el3_runtime/context_mgmt.h>
#include <plat/common/platform.h>
#include <tools_share/uuid.h>

33
#include "tspd_private.h"
34
35

/*******************************************************************************
36
37
 * Address of the entrypoint vector table in the Secure Payload. It is
 * initialised once on the primary core after a cold boot.
38
 ******************************************************************************/
39
tsp_vectors_t *tsp_vectors;
40
41
42
43

/*******************************************************************************
 * Array to keep track of per-cpu Secure Payload state
 ******************************************************************************/
44
tsp_context_t tspd_sp_context[TSPD_CORE_COUNT];
45

46

47
/* TSP UID */
48
49
50
DEFINE_SVC_UUID2(tsp_uuid,
	0xa056305b, 0x9132, 0x7b42, 0x98, 0x11,
	0x71, 0x68, 0xca, 0x50, 0xf3, 0xfa);
51

52
int32_t tspd_init(void);
53

54
55
56
57
58
59
/*
 * This helper function handles Secure EL1 preemption. The preemption could be
 * due Non Secure interrupts or EL3 interrupts. In both the cases we context
 * switch to the normal world and in case of EL3 interrupts, it will again be
 * routed to EL3 which will get handled at the exception vectors.
 */
60
61
62
uint64_t tspd_handle_sp_preemption(void *handle)
{
	cpu_context_t *ns_cpu_context;
63

64
65
66
67
68
69
70
	assert(handle == cm_get_context(SECURE));
	cm_el1_sysregs_context_save(SECURE);
	/* Get a reference to the non-secure context */
	ns_cpu_context = cm_get_context(NON_SECURE);
	assert(ns_cpu_context);

	/*
71
72
73
74
75
76
77
78
	 * To allow Secure EL1 interrupt handler to re-enter TSP while TSP
	 * is preempted, the secure system register context which will get
	 * overwritten must be additionally saved. This is currently done
	 * by the TSPD S-EL1 interrupt handler.
	 */

	/*
	 * Restore non-secure state.
79
80
81
82
	 */
	cm_el1_sysregs_context_restore(NON_SECURE);
	cm_set_next_eret_context(NON_SECURE);

83
	/*
84
	 * The TSP was preempted during execution of a Yielding SMC Call.
85
86
	 * Return back to the normal world with SMC_PREEMPTED as error
	 * code in x0.
87
	 */
88
89
	SMC_RET1(ns_cpu_context, SMC_PREEMPTED);
}
90

91
92
93
/*******************************************************************************
 * This function is the handler registered for S-EL1 interrupts by the TSPD. It
 * validates the interrupt and upon success arranges entry into the TSP at
94
 * 'tsp_sel1_intr_entry()' for handling the interrupt.
95
96
97
98
99
100
101
102
103
104
105
106
 * Typically, interrupts for a specific security state get handled in the same
 * security execption level if the execution is in the same security state. For
 * example, if a non-secure interrupt gets fired when CPU is executing in NS-EL2
 * it gets handled in the non-secure world.
 * However, interrupts belonging to the opposite security state typically demand
 * a world(context) switch. This is inline with the security principle which
 * states a secure interrupt has to be handled in the secure world.
 * Hence, the TSPD in EL3 expects the context(handle) for a secure interrupt to
 * be non-secure and vice versa.
 * However, a race condition between non-secure and secure interrupts can lead to
 * a scenario where the above assumptions do not hold true. This is demonstrated
 * below through Note 1.
107
108
109
110
111
112
113
114
115
 ******************************************************************************/
static uint64_t tspd_sel1_interrupt_handler(uint32_t id,
					    uint32_t flags,
					    void *handle,
					    void *cookie)
{
	uint32_t linear_id;
	tsp_context_t *tsp_ctx;

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
	/* Get a reference to this cpu's TSP context */
	linear_id = plat_my_core_pos();
	tsp_ctx = &tspd_sp_context[linear_id];

#if TSP_NS_INTR_ASYNC_PREEMPT

	/*
	 * Note 1:
	 * Under the current interrupt routing model, interrupts from other
	 * world are routed to EL3 when TSP_NS_INTR_ASYNC_PREEMPT is enabled.
	 * Consider the following scenario:
	 * 1/ A non-secure payload(like tftf) requests a secure service from
	 *    TSP by invoking a yielding SMC call.
	 * 2/ Later, execution jumps to TSP in S-EL1 with the help of TSP
	 *    Dispatcher in Secure Monitor(EL3).
	 * 3/ While CPU is executing TSP, a Non-secure interrupt gets fired.
	 *    this demands a context switch to the non-secure world through
	 *    secure monitor.
	 * 4/ Consequently, TSP in S-EL1 get asynchronously pre-empted and
	 *    execution switches to secure monitor(EL3).
	 * 5/ EL3 tries to triage the (Non-secure) interrupt based on the
	 *    highest pending interrupt.
	 * 6/ However, while the NS Interrupt was pending, secure timer gets
	 *    fired which makes a S-EL1 interrupt to be pending.
	 * 7/ Hence, execution jumps to this companion handler of S-EL1
	 *    interrupt (i.e., tspd_sel1_interrupt_handler) even though the TSP
	 *    was pre-empted due to non-secure interrupt.
	 * 8/ The above sequence of events explain how TSP was pre-empted by
	 *    S-EL1 interrupt indirectly in an asynchronous way.
	 * 9/ Hence, we track the TSP pre-emption by S-EL1 interrupt using a
	 *    boolean variable per each core.
	 * 10/ This helps us to indicate that SMC call for TSP service was
	 *    pre-empted when execution resumes in non-secure world.
	 */

	/* Check the security state when the exception was generated */
	if (get_interrupt_src_ss(flags) == NON_SECURE) {
		/* Sanity check the pointer to this cpu's context */
		assert(handle == cm_get_context(NON_SECURE));

		/* Save the non-secure context before entering the TSP */
		cm_el1_sysregs_context_save(NON_SECURE);
		tsp_ctx->preempted_by_sel1_intr = false;
	} else {
		/* Sanity check the pointer to this cpu's context */
		assert(handle == cm_get_context(SECURE));

		/* Save the secure context before entering the TSP for S-EL1
		 * interrupt handling
		 */
		cm_el1_sysregs_context_save(SECURE);
		tsp_ctx->preempted_by_sel1_intr = true;
	}
#else
170
171
172
173
	/* Check the security state when the exception was generated */
	assert(get_interrupt_src_ss(flags) == NON_SECURE);

	/* Sanity check the pointer to this cpu's context */
174
	assert(handle == cm_get_context(NON_SECURE));
175
176
177

	/* Save the non-secure context before entering the TSP */
	cm_el1_sysregs_context_save(NON_SECURE);
178
#endif
179

180
	assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE));
181
182
183
184
185

	/*
	 * Determine if the TSP was previously preempted. Its last known
	 * context has to be preserved in this case.
	 * The TSP should return control to the TSPD after handling this
186
187
188
189
190
	 * S-EL1 interrupt. Preserve essential EL3 context to allow entry into
	 * the TSP at the S-EL1 interrupt entry point using the 'cpu_context'
	 * structure. There is no need to save the secure system register
	 * context since the TSP is supposed to preserve it during S-EL1
	 * interrupt handling.
191
	 */
192
	if (get_yield_smc_active_flag(tsp_ctx->state)) {
Zelalem's avatar
Zelalem committed
193
		tsp_ctx->saved_spsr_el3 = (uint32_t)SMC_GET_EL3(&tsp_ctx->cpu_ctx,
194
195
196
						      CTX_SPSR_EL3);
		tsp_ctx->saved_elr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx,
						     CTX_ELR_EL3);
197
#if TSP_NS_INTR_ASYNC_PREEMPT
198
199
		memcpy(&tsp_ctx->sp_ctx, &tsp_ctx->cpu_ctx, TSPD_SP_CTX_SIZE);
#endif
200
201
202
	}

	cm_el1_sysregs_context_restore(SECURE);
203
	cm_set_elr_spsr_el3(SECURE, (uint64_t) &tsp_vectors->sel1_intr_entry,
204
		    SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS));
205

206
207
208
	cm_set_next_eret_context(SECURE);

	/*
209
210
211
212
213
	 * Tell the TSP that it has to handle a S-EL1 interrupt synchronously.
	 * Also the instruction in normal world where the interrupt was
	 * generated is passed for debugging purposes. It is safe to retrieve
	 * this address from ELR_EL3 as the secure context will not take effect
	 * until el3_exit().
214
	 */
215
	SMC_RET2(&tsp_ctx->cpu_ctx, TSP_HANDLE_SEL1_INTR_AND_RETURN, read_elr_el3());
216
}
217

218
#if TSP_NS_INTR_ASYNC_PREEMPT
219
/*******************************************************************************
220
221
222
 * This function is the handler registered for Non secure interrupts by the
 * TSPD. It validates the interrupt and upon success arranges entry into the
 * normal world for handling the interrupt.
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
 ******************************************************************************/
static uint64_t tspd_ns_interrupt_handler(uint32_t id,
					    uint32_t flags,
					    void *handle,
					    void *cookie)
{
	/* Check the security state when the exception was generated */
	assert(get_interrupt_src_ss(flags) == SECURE);

	/*
	 * Disable the routing of NS interrupts from secure world to EL3 while
	 * interrupted on this core.
	 */
	disable_intr_rm_local(INTR_TYPE_NS, SECURE);

	return tspd_handle_sp_preemption(handle);
}
#endif

242
243
244
245
246
/*******************************************************************************
 * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type
 * (aarch32/aarch64) if not already known and initialises the context for entry
 * into the SP for its initialisation.
 ******************************************************************************/
247
static int32_t tspd_setup(void)
248
{
Vikram Kanigiri's avatar
Vikram Kanigiri committed
249
	entry_point_info_t *tsp_ep_info;
250
251
	uint32_t linear_id;

252
	linear_id = plat_my_core_pos();
253
254
255
256
257
258

	/*
	 * Get information about the Secure Payload (BL32) image. Its
	 * absence is a critical failure.  TODO: Add support to
	 * conditionally include the SPD service
	 */
Vikram Kanigiri's avatar
Vikram Kanigiri committed
259
260
261
262
263
264
265
	tsp_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
	if (!tsp_ep_info) {
		WARN("No TSP provided by BL2 boot loader, Booting device"
			" without TSP initialization. SMC`s destined for TSP"
			" will return SMC_UNK\n");
		return 1;
	}
266

267
268
269
270
271
	/*
	 * If there's no valid entry point for SP, we return a non-zero value
	 * signalling failure initializing the service. We bail out without
	 * registering any handlers
	 */
Vikram Kanigiri's avatar
Vikram Kanigiri committed
272
	if (!tsp_ep_info->pc)
273
274
		return 1;

275
	/*
276
	 * We could inspect the SP image and determine its execution
277
278
279
	 * state i.e whether AArch32 or AArch64. Assuming it's AArch64
	 * for the time being.
	 */
Vikram Kanigiri's avatar
Vikram Kanigiri committed
280
281
282
283
	tspd_init_tsp_ep_state(tsp_ep_info,
				TSP_AARCH64,
				tsp_ep_info->pc,
				&tspd_sp_context[linear_id]);
284

285
286
287
#if TSP_INIT_ASYNC
	bl31_set_next_image_type(SECURE);
#else
288
289
290
291
292
	/*
	 * All TSPD initialization done. Now register our init function with
	 * BL31 for deferred invocation
	 */
	bl31_register_bl32_init(&tspd_init);
293
#endif
Vikram Kanigiri's avatar
Vikram Kanigiri committed
294
	return 0;
295
296
297
298
299
300
301
302
303
}

/*******************************************************************************
 * This function passes control to the Secure Payload image (BL32) for the first
 * time on the primary cpu after a cold boot. It assumes that a valid secure
 * context has already been created by tspd_setup() which can be directly used.
 * It also assumes that a valid non-secure context has been initialised by PSCI
 * so it does not need to save and restore any non-secure state. This function
 * performs a synchronous entry into the Secure payload. The SP passes control
304
 * back to this routine through a SMC.
305
 ******************************************************************************/
306
int32_t tspd_init(void)
307
{
308
	uint32_t linear_id = plat_my_core_pos();
309
	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
Vikram Kanigiri's avatar
Vikram Kanigiri committed
310
	entry_point_info_t *tsp_entry_point;
311
	uint64_t rc;
Vikram Kanigiri's avatar
Vikram Kanigiri committed
312
313
314
315
316
317
318
319

	/*
	 * Get information about the Secure Payload (BL32) image. Its
	 * absence is a critical failure.
	 */
	tsp_entry_point = bl31_plat_get_next_image_ep_info(SECURE);
	assert(tsp_entry_point);

320
	cm_init_my_context(tsp_entry_point);
321

322
	/*
323
324
	 * Arrange for an entry into the test secure payload. It will be
	 * returned via TSP_ENTRY_DONE case
325
	 */
326
327
	rc = tspd_synchronous_sp_entry(tsp_ctx);
	assert(rc != 0);
328

329
330
331
	return rc;
}

332

333
334
335
336
337
338
339
340
/*******************************************************************************
 * This function is responsible for handling all SMCs in the Trusted OS/App
 * range from the non-secure state as defined in the SMC Calling Convention
 * Document. It is also responsible for communicating with the Secure payload
 * to delegate work and return results back to the non-secure state. Lastly it
 * will also return any information that the secure payload needs to do the
 * work assigned to it.
 ******************************************************************************/
341
342
343
344
345
static uintptr_t tspd_smc_handler(uint32_t smc_fid,
			 u_register_t x1,
			 u_register_t x2,
			 u_register_t x3,
			 u_register_t x4,
346
347
			 void *cookie,
			 void *handle,
348
			 u_register_t flags)
349
{
350
	cpu_context_t *ns_cpu_context;
351
	uint32_t linear_id = plat_my_core_pos(), ns;
352
	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
353
354
355
356
	uint64_t rc;
#if TSP_INIT_ASYNC
	entry_point_info_t *next_image_info;
#endif
357
358
359
360
361
362

	/* Determine which security state this SMC originated from */
	ns = is_caller_non_secure(flags);

	switch (smc_fid) {

363
364
365
366
367
368
369
370
371
	/*
	 * This function ID is used by TSP to indicate that it was
	 * preempted by a normal world IRQ.
	 *
	 */
	case TSP_PREEMPTED:
		if (ns)
			SMC_RET1(handle, SMC_UNK);

372
		return tspd_handle_sp_preemption(handle);
373

374
375
	/*
	 * This function ID is used only by the TSP to indicate that it has
376
377
	 * finished handling a S-EL1 interrupt or was preempted by a higher
	 * priority pending EL3 interrupt. Execution should resume
378
379
	 * in the normal world.
	 */
380
	case TSP_HANDLED_S_EL1_INTR:
381
382
383
		if (ns)
			SMC_RET1(handle, SMC_UNK);

384
		assert(handle == cm_get_context(SECURE));
385
386
387
388
389

		/*
		 * Restore the relevant EL3 state which saved to service
		 * this SMC.
		 */
390
		if (get_yield_smc_active_flag(tsp_ctx->state)) {
391
392
393
394
395
396
			SMC_SET_EL3(&tsp_ctx->cpu_ctx,
				    CTX_SPSR_EL3,
				    tsp_ctx->saved_spsr_el3);
			SMC_SET_EL3(&tsp_ctx->cpu_ctx,
				    CTX_ELR_EL3,
				    tsp_ctx->saved_elr_el3);
397
#if TSP_NS_INTR_ASYNC_PREEMPT
398
399
400
401
402
403
404
			/*
			 * Need to restore the previously interrupted
			 * secure context.
			 */
			memcpy(&tsp_ctx->cpu_ctx, &tsp_ctx->sp_ctx,
				TSPD_SP_CTX_SIZE);
#endif
405
406
407
		}

		/* Get a reference to the non-secure context */
408
		ns_cpu_context = cm_get_context(NON_SECURE);
409
410
411
412
413
414
415
416
417
418
		assert(ns_cpu_context);

		/*
		 * Restore non-secure state. There is no need to save the
		 * secure system register context since the TSP was supposed
		 * to preserve it during S-EL1 interrupt handling.
		 */
		cm_el1_sysregs_context_restore(NON_SECURE);
		cm_set_next_eret_context(NON_SECURE);

419
420
421
422
423
424
425
426
427
428
429
		/* Refer to Note 1 in function tspd_sel1_interrupt_handler()*/
#if TSP_NS_INTR_ASYNC_PREEMPT
		if (tsp_ctx->preempted_by_sel1_intr) {
			/* Reset the flag */
			tsp_ctx->preempted_by_sel1_intr = false;

			SMC_RET1(ns_cpu_context, SMC_PREEMPTED);
		} else {
			SMC_RET0((uint64_t) ns_cpu_context);
		}
#else
430
		SMC_RET0((uint64_t) ns_cpu_context);
431
432
#endif

433

434
435
436
437
438
439
440
441
442
443
444
445
	/*
	 * This function ID is used only by the SP to indicate it has
	 * finished initialising itself after a cold boot
	 */
	case TSP_ENTRY_DONE:
		if (ns)
			SMC_RET1(handle, SMC_UNK);

		/*
		 * Stash the SP entry points information. This is done
		 * only once on the primary cpu
		 */
446
447
		assert(tsp_vectors == NULL);
		tsp_vectors = (tsp_vectors_t *) x1;
448

449
450
451
452
453
		if (tsp_vectors) {
			set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON);

			/*
			 * TSP has been successfully initialized. Register power
Paul Beesley's avatar
Paul Beesley committed
454
			 * management hooks with PSCI
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
			 */
			psci_register_spd_pm_hook(&tspd_pm);

			/*
			 * Register an interrupt handler for S-EL1 interrupts
			 * when generated during code executing in the
			 * non-secure state.
			 */
			flags = 0;
			set_interrupt_rm_flag(flags, NON_SECURE);
			rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
						tspd_sel1_interrupt_handler,
						flags);
			if (rc)
				panic();
470

471
#if TSP_NS_INTR_ASYNC_PREEMPT
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
			/*
			 * Register an interrupt handler for NS interrupts when
			 * generated during code executing in secure state are
			 * routed to EL3.
			 */
			flags = 0;
			set_interrupt_rm_flag(flags, SECURE);

			rc = register_interrupt_type_handler(INTR_TYPE_NS,
						tspd_ns_interrupt_handler,
						flags);
			if (rc)
				panic();

			/*
487
			 * Disable the NS interrupt locally.
488
489
490
			 */
			disable_intr_rm_local(INTR_TYPE_NS, SECURE);
#endif
491
492
493
494
495
496
497
498
499
500
501
502
503
504
		}


#if TSP_INIT_ASYNC
		/* Save the Secure EL1 system register context */
		assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx);
		cm_el1_sysregs_context_save(SECURE);

		/* Program EL3 registers to enable entry into the next EL */
		next_image_info = bl31_plat_get_next_image_ep_info(NON_SECURE);
		assert(next_image_info);
		assert(NON_SECURE ==
				GET_SECURITY_STATE(next_image_info->h.attr));

505
		cm_init_my_context(next_image_info);
506
507
508
		cm_prepare_el3_exit(NON_SECURE);
		SMC_RET0(cm_get_context(NON_SECURE));
#else
509
510
511
512
513
514
		/*
		 * SP reports completion. The SPD must have initiated
		 * the original request through a synchronous entry
		 * into the SP. Jump back to the original C runtime
		 * context.
		 */
515
		tspd_synchronous_sp_exit(tsp_ctx, x1);
516
		break;
517
#endif
518
519
	/*
	 * This function ID is used only by the SP to indicate it has finished
520
	 * aborting a preempted Yielding SMC Call.
521
522
	 */
	case TSP_ABORT_DONE:
523

524
	/*
525
	 * These function IDs are used only by the SP to indicate it has
526
527
528
529
530
531
532
533
534
535
	 * finished:
	 * 1. turning itself on in response to an earlier psci
	 *    cpu_on request
	 * 2. resuming itself after an earlier psci cpu_suspend
	 *    request.
	 */
	case TSP_ON_DONE:
	case TSP_RESUME_DONE:

	/*
536
	 * These function IDs are used only by the SP to indicate it has
537
538
539
540
541
542
543
544
	 * finished:
	 * 1. suspending itself after an earlier psci cpu_suspend
	 *    request.
	 * 2. turning itself off in response to an earlier psci
	 *    cpu_off request.
	 */
	case TSP_OFF_DONE:
	case TSP_SUSPEND_DONE:
545
546
	case TSP_SYSTEM_OFF_DONE:
	case TSP_SYSTEM_RESET_DONE:
547
548
549
550
551
552
553
554
555
		if (ns)
			SMC_RET1(handle, SMC_UNK);

		/*
		 * SP reports completion. The SPD must have initiated the
		 * original request through a synchronous entry into the SP.
		 * Jump back to the original C runtime context, and pass x1 as
		 * return value to the caller
		 */
556
		tspd_synchronous_sp_exit(tsp_ctx, x1);
557
		break;
558

559
560
561
562
563
		/*
		 * Request from non-secure client to perform an
		 * arithmetic operation or response from secure
		 * payload to an earlier request.
		 */
564
565
566
567
568
	case TSP_FAST_FID(TSP_ADD):
	case TSP_FAST_FID(TSP_SUB):
	case TSP_FAST_FID(TSP_MUL):
	case TSP_FAST_FID(TSP_DIV):

569
570
571
572
	case TSP_YIELD_FID(TSP_ADD):
	case TSP_YIELD_FID(TSP_SUB):
	case TSP_YIELD_FID(TSP_MUL):
	case TSP_YIELD_FID(TSP_DIV):
573
574
575
576
577
578
579
		if (ns) {
			/*
			 * This is a fresh request from the non-secure client.
			 * The parameters are in x1 and x2. Figure out which
			 * registers need to be preserved, save the non-secure
			 * state and send the request to the secure payload.
			 */
580
			assert(handle == cm_get_context(NON_SECURE));
581
582

			/* Check if we are already preempted */
583
			if (get_yield_smc_active_flag(tsp_ctx->state))
584
585
				SMC_RET1(handle, SMC_UNK);

586
587
588
			cm_el1_sysregs_context_save(NON_SECURE);

			/* Save x1 and x2 for use by TSP_GET_ARGS call below */
589
			store_tsp_args(tsp_ctx, x1, x2);
590
591
592
593
594
595
596
597
598
599
600
601
602

			/*
			 * We are done stashing the non-secure context. Ask the
			 * secure payload to do the work now.
			 */

			/*
			 * Verify if there is a valid context to use, copy the
			 * operation type and parameters to the secure context
			 * and jump to the fast smc entry point in the secure
			 * payload. Entry into S-EL1 will take place upon exit
			 * from this function.
			 */
603
			assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE));
604
605
606
607
608
609
610

			/* Set appropriate entry for SMC.
			 * We expect the TSP to manage the PSTATE.I and PSTATE.F
			 * flags as appropriate.
			 */
			if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_FAST) {
				cm_set_elr_el3(SECURE, (uint64_t)
611
						&tsp_vectors->fast_smc_entry);
612
			} else {
613
				set_yield_smc_active_flag(tsp_ctx->state);
614
				cm_set_elr_el3(SECURE, (uint64_t)
615
						&tsp_vectors->yield_smc_entry);
616
#if TSP_NS_INTR_ASYNC_PREEMPT
617
618
				/*
				 * Enable the routing of NS interrupts to EL3
619
620
				 * during processing of a Yielding SMC Call on
				 * this core.
621
622
623
				 */
				enable_intr_rm_local(INTR_TYPE_NS, SECURE);
#endif
624
625
626
627
628
629
630
631

#if EL3_EXCEPTION_HANDLING
				/*
				 * With EL3 exception handling, while an SMC is
				 * being processed, Non-secure interrupts can't
				 * preempt Secure execution. However, for
				 * yielding SMCs, we want preemption to happen;
				 * so explicitly allow NS preemption in this
632
633
				 * case, and supply the preemption return code
				 * for TSP.
634
				 */
635
				ehf_allow_ns_preemption(TSP_PREEMPTED);
636
#endif
637
638
			}

639
640
			cm_el1_sysregs_context_restore(SECURE);
			cm_set_next_eret_context(SECURE);
641
			SMC_RET3(&tsp_ctx->cpu_ctx, smc_fid, x1, x2);
642
643
644
		} else {
			/*
			 * This is the result from the secure client of an
645
			 * earlier request. The results are in x1-x3. Copy it
646
647
648
			 * into the non-secure context, save the secure state
			 * and return to the non-secure state.
			 */
649
			assert(handle == cm_get_context(SECURE));
650
651
652
			cm_el1_sysregs_context_save(SECURE);

			/* Get a reference to the non-secure context */
653
			ns_cpu_context = cm_get_context(NON_SECURE);
654
655
656
657
658
			assert(ns_cpu_context);

			/* Restore non-secure state */
			cm_el1_sysregs_context_restore(NON_SECURE);
			cm_set_next_eret_context(NON_SECURE);
659
660
			if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_YIELD) {
				clr_yield_smc_active_flag(tsp_ctx->state);
661
#if TSP_NS_INTR_ASYNC_PREEMPT
662
663
				/*
				 * Disable the routing of NS interrupts to EL3
664
665
				 * after processing of a Yielding SMC Call on
				 * this core is finished.
666
667
668
669
670
				 */
				disable_intr_rm_local(INTR_TYPE_NS, SECURE);
#endif
			}

671
			SMC_RET3(ns_cpu_context, x1, x2, x3);
672
		}
673
		assert(0); /* Unreachable */
674

675
	/*
676
677
	 * Request from the non-secure world to abort a preempted Yielding SMC
	 * Call.
678
679
680
681
682
683
684
685
	 */
	case TSP_FID_ABORT:
		/* ABORT should only be invoked by normal world */
		if (!ns) {
			assert(0);
			break;
		}

686
687
688
		assert(handle == cm_get_context(NON_SECURE));
		cm_el1_sysregs_context_save(NON_SECURE);

689
		/* Abort the preempted SMC request */
690
		if (!tspd_abort_preempted_smc(tsp_ctx)) {
691
692
693
			/*
			 * If there was no preempted SMC to abort, return
			 * SMC_UNK.
694
695
696
697
			 *
			 * Restoring the NON_SECURE context is not necessary as
			 * the synchronous entry did not take place if the
			 * return code of tspd_abort_preempted_smc is zero.
698
			 */
699
700
701
			cm_set_next_eret_context(NON_SECURE);
			break;
		}
702

703
704
		cm_el1_sysregs_context_restore(NON_SECURE);
		cm_set_next_eret_context(NON_SECURE);
705
		SMC_RET1(handle, SMC_OK);
706

707
708
		/*
		 * Request from non secure world to resume the preempted
709
		 * Yielding SMC Call.
710
711
		 */
	case TSP_FID_RESUME:
712
713
714
715
716
		/* RESUME should be invoked only by normal world */
		if (!ns) {
			assert(0);
			break;
		}
717

718
719
720
721
722
		/*
		 * This is a resume request from the non-secure client.
		 * save the non-secure state and send the request to
		 * the secure payload.
		 */
723
		assert(handle == cm_get_context(NON_SECURE));
724

725
		/* Check if we are already preempted before resume */
726
		if (!get_yield_smc_active_flag(tsp_ctx->state))
727
			SMC_RET1(handle, SMC_UNK);
728

729
		cm_el1_sysregs_context_save(NON_SECURE);
730

731
732
733
734
		/*
		 * We are done stashing the non-secure context. Ask the
		 * secure payload to do the work now.
		 */
735
#if TSP_NS_INTR_ASYNC_PREEMPT
736
737
		/*
		 * Enable the routing of NS interrupts to EL3 during resumption
738
		 * of a Yielding SMC Call on this core.
739
740
741
742
		 */
		enable_intr_rm_local(INTR_TYPE_NS, SECURE);
#endif

743
744
745
#if EL3_EXCEPTION_HANDLING
		/*
		 * Allow the resumed yielding SMC processing to be preempted by
746
747
		 * Non-secure interrupts. Also, supply the preemption return
		 * code for TSP.
748
		 */
749
		ehf_allow_ns_preemption(TSP_PREEMPTED);
750
#endif
751

752
753
754
755
756
757
		/* We just need to return to the preempted point in
		 * TSP and the execution will resume as normal.
		 */
		cm_el1_sysregs_context_restore(SECURE);
		cm_set_next_eret_context(SECURE);
		SMC_RET0(&tsp_ctx->cpu_ctx);
758

759
760
761
762
763
764
765
766
767
768
		/*
		 * This is a request from the secure payload for more arguments
		 * for an ongoing arithmetic operation requested by the
		 * non-secure world. Simply return the arguments from the non-
		 * secure client in the original call.
		 */
	case TSP_GET_ARGS:
		if (ns)
			SMC_RET1(handle, SMC_UNK);

769
770
		get_tsp_args(tsp_ctx, x1, x2);
		SMC_RET2(handle, x1, x2);
771

772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
	case TOS_CALL_COUNT:
		/*
		 * Return the number of service function IDs implemented to
		 * provide service to non-secure
		 */
		SMC_RET1(handle, TSP_NUM_FID);

	case TOS_UID:
		/* Return TSP UID to the caller */
		SMC_UUID_RET(handle, tsp_uuid);

	case TOS_CALL_VERSION:
		/* Return the version of current implementation */
		SMC_RET2(handle, TSP_VERSION_MAJOR, TSP_VERSION_MINOR);

787
	default:
788
		break;
789
790
	}

791
	SMC_RET1(handle, SMC_UNK);
792
793
}

794
/* Define a SPD runtime service descriptor for fast SMC calls */
795
DECLARE_RT_SVC(
796
	tspd_fast,
797
798
799
800
801
802
803

	OEN_TOS_START,
	OEN_TOS_END,
	SMC_TYPE_FAST,
	tspd_setup,
	tspd_smc_handler
);
804

805
/* Define a SPD runtime service descriptor for Yielding SMC Calls */
806
807
808
809
810
DECLARE_RT_SVC(
	tspd_std,

	OEN_TOS_START,
	OEN_TOS_END,
811
	SMC_TYPE_YIELD,
812
813
814
	NULL,
	tspd_smc_handler
);