summaryrefslogtreecommitdiff
path: root/drivers/firmware/arm-ffa/ffa-emul-uclass.c
blob: 4bf9f6041fecbe46bbdddfefc6ff7828d6afbaef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
 *
 * Authors:
 *   Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
 */
#include <common.h>
#include <dm.h>
#include <mapmem.h>
#include <string.h>
#include <asm/global_data.h>
#include <asm/sandbox_arm_ffa.h>
#include <asm/sandbox_arm_ffa_priv.h>
#include <dm/device-internal.h>
#include <dm/lists.h>
#include <dm/root.h>
#include <linux/errno.h>
#include <linux/sizes.h>

DECLARE_GLOBAL_DATA_PTR;

/* The partitions (SPs) table */
static struct ffa_partition_desc sandbox_partitions[SANDBOX_PARTITIONS_CNT] = {
	{
		.info = { .id = SANDBOX_SP1_ID, .exec_ctxt = 0x5687, .properties = 0x89325621 },
		.sp_uuid = {
			.a1 = SANDBOX_SERVICE1_UUID_A1,
			.a2 = SANDBOX_SERVICE1_UUID_A2,
			.a3 = SANDBOX_SERVICE1_UUID_A3,
			.a4 = SANDBOX_SERVICE1_UUID_A4,
		}
	},
	{
		.info = { .id = SANDBOX_SP3_ID, .exec_ctxt = 0x7687, .properties = 0x23325621 },
		.sp_uuid = {
			.a1 = SANDBOX_SERVICE2_UUID_A1,
			.a2 = SANDBOX_SERVICE2_UUID_A2,
			.a3 = SANDBOX_SERVICE2_UUID_A3,
			.a4 = SANDBOX_SERVICE2_UUID_A4,
		}
	},
	{
		.info = { .id = SANDBOX_SP2_ID, .exec_ctxt = 0x9587, .properties = 0x45325621 },
		.sp_uuid = {
			.a1 = SANDBOX_SERVICE1_UUID_A1,
			.a2 = SANDBOX_SERVICE1_UUID_A2,
			.a3 = SANDBOX_SERVICE1_UUID_A3,
			.a4 = SANDBOX_SERVICE1_UUID_A4,
		}
	},
	{
		.info = { .id = SANDBOX_SP4_ID, .exec_ctxt = 0x1487, .properties = 0x70325621 },
		.sp_uuid = {
			.a1 = SANDBOX_SERVICE2_UUID_A1,
			.a2 = SANDBOX_SERVICE2_UUID_A2,
			.a3 = SANDBOX_SERVICE2_UUID_A3,
			.a4 = SANDBOX_SERVICE2_UUID_A4,
		}
	}

};

/* The emulator functions */

/**
 * sandbox_ffa_version() - Emulated FFA_VERSION handler function
 * @emul: The sandbox FF-A emulator device
 * @pargs: The SMC call input arguments a0-a7
 * @res:  The SMC return data
 *
 * Emulate FFA_VERSION FF-A function.
 *
 * Return:
 *
 * 0 on success. Otherwise, failure
 */

static int sandbox_ffa_version(struct udevice *emul, ffa_value_t *pargs, ffa_value_t *res)
{
	struct sandbox_ffa_emul *priv = dev_get_priv(emul);

	priv->fwk_version = FFA_VERSION_1_0;
	res->a0 = priv->fwk_version;

	/* x1-x7 MBZ */
	memset(FFA_X1X7_MBZ_REG_START, 0, FFA_X1X7_MBZ_CNT * sizeof(ulong));

	return 0;
}

/**
 * sandbox_ffa_id_get() - Emulated FFA_ID_GET handler function
 * @emul: The sandbox FF-A emulator device
 * @pargs: The SMC call input arguments a0-a7
 * @res:  The SMC return data
 *
 * Emulate FFA_ID_GET FF-A function.
 *
 * Return:
 *
 * 0 on success. Otherwise, failure
 */
static int sandbox_ffa_id_get(struct udevice *emul, ffa_value_t *pargs, ffa_value_t *res)
{
	struct sandbox_ffa_emul *priv = dev_get_priv(emul);

	res->a0 = FFA_SMC_32(FFA_SUCCESS);
	res->a1 = 0;

	priv->id = NS_PHYS_ENDPOINT_ID;
	res->a2 = priv->id;

	/* x3-x7 MBZ */
	memset(FFA_X3_MBZ_REG_START, 0, FFA_X3X7_MBZ_CNT * sizeof(ulong));

	return 0;
}

/**
 * sandbox_ffa_features() - Emulated FFA_FEATURES handler function
 * @pargs: The SMC call input arguments a0-a7
 * @res:  The SMC return data
 *
 * Emulate FFA_FEATURES FF-A function.
 *
 * Return:
 *
 * 0 on success. Otherwise, failure
 */
static int sandbox_ffa_features(ffa_value_t *pargs, ffa_value_t *res)
{
	res->a1 = 0;

	if (pargs->a1 == FFA_SMC_64(FFA_RXTX_MAP)) {
		res->a0 = FFA_SMC_32(FFA_SUCCESS);
		res->a2 = RXTX_BUFFERS_MIN_SIZE;
		res->a3 = 0;
		/* x4-x7 MBZ */
		memset(FFA_X4X7_MBZ_REG_START, 0, FFA_X4X7_MBZ_CNT * sizeof(ulong));
		return 0;
	}

	res->a0 = FFA_SMC_32(FFA_ERROR);
	res->a2 = -NOT_SUPPORTED;
	/* x3-x7 MBZ */
	memset(FFA_X3_MBZ_REG_START, 0, FFA_X3X7_MBZ_CNT * sizeof(ulong));
	log_err("FF-A interface %lx not implemented\n", pargs->a1);

	return ffa_to_std_errmap[NOT_SUPPORTED];
}

/**
 * sandbox_ffa_partition_info_get() - Emulated FFA_PARTITION_INFO_GET handler
 * @emul: The sandbox FF-A emulator device
 * @pargs: The SMC call input arguments a0-a7
 * @res:  The SMC return data
 *
 * Emulate FFA_PARTITION_INFO_GET FF-A function.
 *
 * Return:
 *
 * 0 on success. Otherwise, failure
 */
static int sandbox_ffa_partition_info_get(struct udevice *emul, ffa_value_t *pargs,
					  ffa_value_t *res)
{
	struct ffa_partition_info *rxbuf_desc_info = NULL;
	u32 descs_cnt;
	u32 descs_size_bytes;
	int ret;
	struct sandbox_ffa_emul *priv = dev_get_priv(emul);

	res->a0 = FFA_SMC_32(FFA_ERROR);

	if (!priv->pair.rxbuf) {
		res->a2 = -DENIED;
		ret = ffa_to_std_errmap[DENIED];
		goto cleanup;
	}

	if (priv->pair_info.rxbuf_owned) {
		res->a2 = -BUSY;
		ret = ffa_to_std_errmap[BUSY];
		goto cleanup;
	}

	if (!priv->partitions.descs) {
		priv->partitions.descs = sandbox_partitions;
		priv->partitions.count = SANDBOX_PARTITIONS_CNT;
	}

	descs_size_bytes = SANDBOX_PARTITIONS_CNT *
		sizeof(struct ffa_partition_desc);

	/* Abort if the RX buffer size is smaller than the descs buffer size */
	if ((priv->pair_info.rxtx_buf_size * SZ_4K) < descs_size_bytes) {
		res->a2 = -NO_MEMORY;
		ret = ffa_to_std_errmap[NO_MEMORY];
		goto cleanup;
	}

	rxbuf_desc_info = priv->pair.rxbuf;

	/* No UUID specified. Return the information of all partitions */
	if (!pargs->a1 && !pargs->a2 && !pargs->a3 && !pargs->a4) {
		for (descs_cnt = 0; descs_cnt < SANDBOX_PARTITIONS_CNT; descs_cnt++)
			*(rxbuf_desc_info++) = priv->partitions.descs[descs_cnt].info;

		res->a0 = FFA_SMC_32(FFA_SUCCESS);
		res->a2 = SANDBOX_PARTITIONS_CNT;
		/* Transfer ownership to the consumer: the non secure world */
		priv->pair_info.rxbuf_owned = 1;
		ret = 0;

		goto cleanup;
	}

	/* A UUID specified. Return the info of all SPs matching the UUID */

	for (descs_cnt = 0 ; descs_cnt < SANDBOX_PARTITIONS_CNT ; descs_cnt++)
		if (pargs->a1 == priv->partitions.descs[descs_cnt].sp_uuid.a1 &&
		    pargs->a2 == priv->partitions.descs[descs_cnt].sp_uuid.a2 &&
		    pargs->a3 == priv->partitions.descs[descs_cnt].sp_uuid.a3 &&
		    pargs->a4 == priv->partitions.descs[descs_cnt].sp_uuid.a4) {
			*(rxbuf_desc_info++) = priv->partitions.descs[descs_cnt].info;
		}

	if (rxbuf_desc_info != priv->pair.rxbuf) {
		res->a0 = FFA_SMC_32(FFA_SUCCESS);
		/* Store the partitions count */
		res->a2 = (ulong)
			(rxbuf_desc_info - (struct ffa_partition_info *)
			 priv->pair.rxbuf);
		ret = 0;

		/* Transfer ownership to the consumer: the non secure world */
		priv->pair_info.rxbuf_owned = 1;
	} else {
		/* Unrecognized UUID */
		res->a2 = -INVALID_PARAMETERS;
		ret = ffa_to_std_errmap[INVALID_PARAMETERS];
	}

cleanup:

	log_err("FFA_PARTITION_INFO_GET (%ld)\n", res->a2);

	res->a1 = 0;

	/* x3-x7 MBZ */
	memset(FFA_X3_MBZ_REG_START, 0, FFA_X3X7_MBZ_CNT * sizeof(ulong));

	return ret;
}

/**
 * sandbox_ffa_rxtx_map() - Emulated FFA_RXTX_MAP handler
 * @emul: The sandbox FF-A emulator device
 * @pargs: The SMC call input arguments a0-a7
 * @res:  The SMC return data
 *
 * Emulate FFA_RXTX_MAP FF-A function.
 *
 * Return:
 *
 * 0 on success. Otherwise, failure
 */
static int sandbox_ffa_rxtx_map(struct udevice *emul, ffa_value_t *pargs, ffa_value_t *res)
{
	int ret;
	struct sandbox_ffa_emul *priv = dev_get_priv(emul);

	res->a0 = FFA_SMC_32(FFA_ERROR);

	if (priv->pair.txbuf && priv->pair.rxbuf) {
		res->a2 = -DENIED;
		ret = ffa_to_std_errmap[DENIED];
		goto feedback;
	}

	if (pargs->a3 >= RXTX_BUFFERS_MIN_PAGES && pargs->a1 && pargs->a2) {
		priv->pair.txbuf = map_sysmem(pargs->a1, 0);
		priv->pair.rxbuf = map_sysmem(pargs->a2, 0);
		priv->pair_info.rxtx_buf_size = pargs->a3;
		priv->pair_info.rxbuf_mapped = 1;
		res->a0 = FFA_SMC_32(FFA_SUCCESS);
		res->a2 = 0;
		ret = 0;
		goto feedback;
	}

	if (!pargs->a1 || !pargs->a2) {
		res->a2 = -INVALID_PARAMETERS;
		ret = ffa_to_std_errmap[INVALID_PARAMETERS];
	} else {
		res->a2 = -NO_MEMORY;
		ret = ffa_to_std_errmap[NO_MEMORY];
	}

	log_err("Error in FFA_RXTX_MAP arguments (%d)\n",
		(int)res->a2);

feedback:

	res->a1 = 0;

	/* x3-x7 MBZ */
	memset(FFA_X3_MBZ_REG_START, 0, FFA_X3X7_MBZ_CNT * sizeof(ulong));

	return ret;
}

/**
 * sandbox_ffa_rxtx_unmap() - Emulated FFA_RXTX_UNMAP handler
 * @emul: The sandbox FF-A emulator device
 * @pargs: The SMC call input arguments a0-a7
 * @res:  The SMC return data
 *
 * Emulate FFA_RXTX_UNMAP FF-A function.
 *
 * Return:
 *
 * 0 on success. Otherwise, failure
 */
static int sandbox_ffa_rxtx_unmap(struct udevice *emul, ffa_value_t *pargs, ffa_value_t *res)
{
	int ret;
	struct sandbox_ffa_emul *priv = dev_get_priv(emul);

	res->a0 = FFA_SMC_32(FFA_ERROR);
	res->a2 = -INVALID_PARAMETERS;
	ret = ffa_to_std_errmap[INVALID_PARAMETERS];

	if (GET_NS_PHYS_ENDPOINT_ID(pargs->a1) != priv->id)
		goto feedback;

	if (priv->pair.txbuf && priv->pair.rxbuf) {
		priv->pair.txbuf = 0;
		priv->pair.rxbuf = 0;
		priv->pair_info.rxtx_buf_size = 0;
		priv->pair_info.rxbuf_mapped = 0;
		res->a0 = FFA_SMC_32(FFA_SUCCESS);
		res->a2 = 0;
		ret = 0;
		goto feedback;
	}

	log_err("No buffer pair registered on behalf of the caller\n");

feedback:

	res->a1 = 0;

	/* x3-x7 MBZ */
	memset(FFA_X3_MBZ_REG_START, 0, FFA_X3X7_MBZ_CNT * sizeof(ulong));

	return ret;
}

/**
 * sandbox_ffa_rx_release() - Emulated FFA_RX_RELEASE handler
 * @emul: The sandbox FF-A emulator device
 * @pargs: The SMC call input arguments a0-a7
 * @res:  The SMC return data
 *
 * Emulate FFA_RX_RELEASE FF-A function.
 *
 * Return:
 *
 * 0 on success. Otherwise, failure
 */
static int sandbox_ffa_rx_release(struct udevice *emul, ffa_value_t *pargs, ffa_value_t *res)
{
	int ret;
	struct sandbox_ffa_emul *priv = dev_get_priv(emul);

	if (!priv->pair_info.rxbuf_owned) {
		res->a0 = FFA_SMC_32(FFA_ERROR);
		res->a2 = -DENIED;
		ret = ffa_to_std_errmap[DENIED];
	} else {
		priv->pair_info.rxbuf_owned = 0;
		res->a0 = FFA_SMC_32(FFA_SUCCESS);
		res->a2 = 0;
		ret = 0;
	}

	res->a1 = 0;

	/* x3-x7 MBZ */
	memset(FFA_X3_MBZ_REG_START, 0, FFA_X3X7_MBZ_CNT * sizeof(ulong));

	return ret;
}

/**
 * sandbox_ffa_sp_valid() - Check SP validity
 * @emul: The sandbox FF-A emulator device
 * @part_id:	partition ID to check
 *
 * Search the input ID in the descriptors table.
 *
 * Return:
 *
 * 1 on success (Partition found). Otherwise, failure
 */
static int sandbox_ffa_sp_valid(struct udevice *emul, u16 part_id)
{
	u32 descs_cnt;
	struct sandbox_ffa_emul *priv = dev_get_priv(emul);

	for (descs_cnt = 0 ; descs_cnt < SANDBOX_PARTITIONS_CNT ; descs_cnt++)
		if (priv->partitions.descs[descs_cnt].info.id == part_id)
			return 1;

	return 0;
}

/**
 * sandbox_ffa_msg_send_direct_req() - Emulated FFA_MSG_SEND_DIRECT_{REQ,RESP} handler
 * @emul: The sandbox FF-A emulator device
 * @pargs: The SMC call input arguments a0-a7
 * @res:  The SMC return data
 *
 * Emulate FFA_MSG_SEND_DIRECT_{REQ,RESP} FF-A ABIs.
 * Only SMC 64-bit is supported in Sandbox.
 *
 * Emulating interrupts is not supported. So, FFA_RUN and FFA_INTERRUPT are not
 * supported. In case of success FFA_MSG_SEND_DIRECT_RESP is returned with
 * default pattern data (0xff).
 *
 * Return:
 *
 * 0 on success. Otherwise, failure
 */
static int sandbox_ffa_msg_send_direct_req(struct udevice *emul,
					   ffa_value_t *pargs, ffa_value_t *res)
{
	u16 part_id;
	struct sandbox_ffa_emul *priv = dev_get_priv(emul);

	part_id = GET_DST_SP_ID(pargs->a1);

	if (GET_NS_PHYS_ENDPOINT_ID(pargs->a1) != priv->id ||
	    !sandbox_ffa_sp_valid(emul, part_id) || pargs->a2) {
		res->a0 = FFA_SMC_32(FFA_ERROR);
		res->a1 = 0;
		res->a2 = -INVALID_PARAMETERS;

		/* x3-x7 MBZ */
		memset(FFA_X3_MBZ_REG_START, 0, FFA_X3X7_MBZ_CNT * sizeof(ulong));

		return ffa_to_std_errmap[INVALID_PARAMETERS];
	}

	res->a0 = FFA_SMC_64(FFA_MSG_SEND_DIRECT_RESP);

	res->a1 = PREP_SRC_SP_ID(part_id) |
		PREP_NS_PHYS_ENDPOINT_ID(priv->id);

	res->a2 = 0;

	/* Return 0xff bytes as a response */
	res->a3 = -1UL;
	res->a4 = -1UL;
	res->a5 = -1UL;
	res->a6 = -1UL;
	res->a7 = -1UL;

	return 0;
}

/**
 * sandbox_ffa_get_rxbuf_flags() - Read the mapping/ownership flags
 * @emul: The sandbox FF-A emulator device
 * @queried_func_id:	The FF-A function to be queried
 * @func_data:  Pointer to the FF-A function arguments container structure
 *
 * Query the status flags of the following emulated
 * ABIs: FFA_RXTX_MAP, FFA_RXTX_UNMAP, FFA_RX_RELEASE.
 *
 * Return:
 *
 * 0 on success. Otherwise, failure
 */
static int sandbox_ffa_get_rxbuf_flags(struct udevice *emul, u32 queried_func_id,
				       struct ffa_sandbox_data *func_data)
{
	struct sandbox_ffa_emul *priv = dev_get_priv(emul);

	if (!func_data)
		return -EINVAL;

	if (!func_data->data0 || func_data->data0_size != sizeof(u8))
		return -EINVAL;

	switch (queried_func_id) {
	case FFA_RXTX_MAP:
	case FFA_RXTX_UNMAP:
		*((u8 *)func_data->data0) = priv->pair_info.rxbuf_mapped;
		return 0;
	case FFA_RX_RELEASE:
		*((u8 *)func_data->data0) = priv->pair_info.rxbuf_owned;
		return 0;
	default:
		log_err("The querried FF-A interface flag (%d) undefined\n",
			queried_func_id);
		return -EINVAL;
	}
}

/**
 * sandbox_ffa_get_fwk_version() - Return the FFA framework version
 * @emul: The sandbox FF-A emulator device
 * @func_data:  Pointer to the FF-A function arguments container structure
 *
 * Return the FFA framework version read from the FF-A emulator data.
 *
 * Return:
 *
 * 0 on success. Otherwise, failure
 */
static int sandbox_ffa_get_fwk_version(struct udevice *emul, struct ffa_sandbox_data *func_data)
{
	struct sandbox_ffa_emul *priv = dev_get_priv(emul);

	if (!func_data)
		return -EINVAL;

	if (!func_data->data0 ||
	    func_data->data0_size != sizeof(priv->fwk_version))
		return -EINVAL;

	*((u32 *)func_data->data0) = priv->fwk_version;

	return 0;
}

/**
 * sandbox_ffa_get_parts() - Return the address of partitions data
 * @emul: The sandbox FF-A emulator device
 * @func_data:  Pointer to the FF-A function arguments container structure
 *
 * Return the address of partitions data read from the FF-A emulator data.
 *
 * Return:
 *
 * 0 on success. Otherwise, failure
 */
static int sandbox_ffa_get_parts(struct udevice *emul, struct ffa_sandbox_data *func_data)
{
	struct sandbox_ffa_emul *priv = dev_get_priv(emul);

	if (!func_data)
		return -EINVAL;

	if (!func_data->data0 ||
	    func_data->data0_size != sizeof(struct ffa_partitions *))
		return -EINVAL;

	*((struct ffa_partitions **)func_data->data0) = &priv->partitions;

	return 0;
}

/**
 * sandbox_query_ffa_emul_state() - Inspect the FF-A ABIs
 * @queried_func_id:	The FF-A function to be queried
 * @func_data:  Pointer to the FF-A function arguments container structure
 *
 * Query the status of FF-A ABI specified in the input argument.
 *
 * Return:
 *
 * 0 on success. Otherwise, failure
 */
int sandbox_query_ffa_emul_state(u32 queried_func_id,
				 struct ffa_sandbox_data *func_data)
{
	struct udevice *emul;
	int ret;

	ret = uclass_first_device_err(UCLASS_FFA_EMUL, &emul);
	if (ret) {
		log_err("Cannot find FF-A emulator during querying state\n");
		return ret;
	}

	switch (queried_func_id) {
	case FFA_RXTX_MAP:
	case FFA_RXTX_UNMAP:
	case FFA_RX_RELEASE:
		return sandbox_ffa_get_rxbuf_flags(emul, queried_func_id, func_data);
	case FFA_VERSION:
		return sandbox_ffa_get_fwk_version(emul, func_data);
	case FFA_PARTITION_INFO_GET:
		return sandbox_ffa_get_parts(emul, func_data);
	default:
		log_err("Undefined FF-A interface (%d)\n",
			queried_func_id);
		return -EINVAL;
	}
}

/**
 * sandbox_arm_ffa_smccc_smc() - FF-A SMC call emulation
 * @args:	the SMC call arguments
 * @res:	the SMC call returned data
 *
 * Emulate the FF-A ABIs SMC call.
 * The emulated FF-A ABI is identified and invoked.
 * FF-A emulation is based on the FF-A specification 1.0
 *
 * Return:
 *
 * 0 on success. Otherwise, failure.
 * FF-A protocol error codes are returned using the registers arguments as
 * described by the specification
 */
void sandbox_arm_ffa_smccc_smc(ffa_value_t *args, ffa_value_t *res)
{
	int ret = 0;
	struct udevice *emul;

	ret = uclass_first_device_err(UCLASS_FFA_EMUL, &emul);
	if (ret) {
		log_err("Cannot find FF-A emulator during SMC emulation\n");
		return;
	}

	switch (args->a0) {
	case FFA_SMC_32(FFA_VERSION):
		ret = sandbox_ffa_version(emul, args, res);
		break;
	case FFA_SMC_32(FFA_PARTITION_INFO_GET):
		ret = sandbox_ffa_partition_info_get(emul, args, res);
		break;
	case FFA_SMC_32(FFA_RXTX_UNMAP):
		ret = sandbox_ffa_rxtx_unmap(emul, args, res);
		break;
	case FFA_SMC_64(FFA_MSG_SEND_DIRECT_REQ):
		ret = sandbox_ffa_msg_send_direct_req(emul, args, res);
		break;
	case FFA_SMC_32(FFA_ID_GET):
		ret = sandbox_ffa_id_get(emul, args, res);
		break;
	case FFA_SMC_32(FFA_FEATURES):
		ret = sandbox_ffa_features(args, res);
		break;
	case FFA_SMC_64(FFA_RXTX_MAP):
		ret = sandbox_ffa_rxtx_map(emul, args, res);
		break;
	case FFA_SMC_32(FFA_RX_RELEASE):
		ret = sandbox_ffa_rx_release(emul, args, res);
		break;
	default:
		log_err("Undefined FF-A interface (%lx)\n",
			args->a0);
	}

	if (ret != 0)
		log_err("FF-A ABI internal failure (%d)\n", ret);
}

/**
 * invoke_ffa_fn() - SMC wrapper
 * @args: FF-A ABI arguments to be copied to Xn registers
 * @res: FF-A ABI return data to be copied from Xn registers
 *
 * Calls the emulated SMC call.
 */
void invoke_ffa_fn(ffa_value_t args, ffa_value_t *res)
{
	sandbox_arm_ffa_smccc_smc(&args, res);
}

/**
 * ffa_emul_find() - Find the FF-A emulator
 * @dev:	the sandbox FF-A device (sandbox-arm-ffa)
 * @emulp:	the FF-A emulator device (sandbox-ffa-emul)
 *
 * Search for the FF-A emulator and returns its device pointer.
 *
 * Return:
 * 0 on success. Otherwise, failure
 */
int ffa_emul_find(struct udevice *dev, struct udevice **emulp)
{
	int ret;

	ret = uclass_first_device_err(UCLASS_FFA_EMUL, emulp);
	if (ret) {
		log_err("Cannot find FF-A emulator\n");
		return ret;
	}

	log_debug("FF-A emulator ready to use\n");

	return 0;
}

UCLASS_DRIVER(ffa_emul) = {
	.name		= "ffa_emul",
	.id		= UCLASS_FFA_EMUL,
	.post_bind = dm_scan_fdt_dev,
};

static const struct udevice_id sandbox_ffa_emul_ids[] = {
	{ .compatible = "sandbox,arm-ffa-emul" },
	{ }
};

/* Declaring the sandbox FF-A emulator under UCLASS_FFA_EMUL */
U_BOOT_DRIVER(sandbox_ffa_emul) = {
	.name		= "sandbox_ffa_emul",
	.id		= UCLASS_FFA_EMUL,
	.of_match	= sandbox_ffa_emul_ids,
	.priv_auto	= sizeof(struct sandbox_ffa_emul),
};