blob: 96c64964bb73b05e094f7ab71a5e68c3195f3edf [file] [log] [blame]
Abdellatif El Khlifi2f403d12023-08-04 14:33:40 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
4 *
5 * Authors:
6 * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
7 */
Abdellatif El Khlifi2f403d12023-08-04 14:33:40 +01008#include <arm_ffa.h>
9#include <arm_ffa_priv.h>
10#include <dm.h>
11#include <log.h>
12#include <malloc.h>
13#include <string.h>
Caleb Connolly29cab7c2024-08-30 13:34:37 +010014#include <u-boot/uuid.h>
Abdellatif El Khlifi2f403d12023-08-04 14:33:40 +010015#include <asm/global_data.h>
16#include <dm/device-internal.h>
17#include <dm/devres.h>
18#include <dm/root.h>
19#include <linux/errno.h>
20#include <linux/sizes.h>
21
22DECLARE_GLOBAL_DATA_PTR;
23
24/* Error mapping declarations */
25
26int ffa_to_std_errmap[MAX_NUMBER_FFA_ERR] = {
27 [NOT_SUPPORTED] = -EOPNOTSUPP,
28 [INVALID_PARAMETERS] = -EINVAL,
29 [NO_MEMORY] = -ENOMEM,
30 [BUSY] = -EBUSY,
31 [INTERRUPTED] = -EINTR,
32 [DENIED] = -EACCES,
33 [RETRY] = -EAGAIN,
34 [ABORTED] = -ECANCELED,
35};
36
37static struct ffa_abi_errmap err_msg_map[FFA_ERRMAP_COUNT] = {
38 [FFA_ID_TO_ERRMAP_ID(FFA_VERSION)] = {
39 {
40 [NOT_SUPPORTED] =
41 "NOT_SUPPORTED: A Firmware Framework implementation does not exist",
42 },
43 },
44 [FFA_ID_TO_ERRMAP_ID(FFA_ID_GET)] = {
45 {
46 [NOT_SUPPORTED] =
47 "NOT_SUPPORTED: This function is not implemented at this FF-A instance",
48 },
49 },
50 [FFA_ID_TO_ERRMAP_ID(FFA_FEATURES)] = {
51 {
52 [NOT_SUPPORTED] =
53 "NOT_SUPPORTED: FFA_RXTX_MAP is not implemented at this FF-A instance",
54 },
55 },
56 [FFA_ID_TO_ERRMAP_ID(FFA_PARTITION_INFO_GET)] = {
57 {
58 [NOT_SUPPORTED] =
59 "NOT_SUPPORTED: This function is not implemented at this FF-A instance",
60 [INVALID_PARAMETERS] =
61 "INVALID_PARAMETERS: Unrecognized UUID",
62 [NO_MEMORY] =
63 "NO_MEMORY: Results cannot fit in RX buffer of the caller",
64 [BUSY] =
65 "BUSY: RX buffer of the caller is not free",
66 [DENIED] =
67 "DENIED: Callee is not in a state to handle this request",
68 },
69 },
70 [FFA_ID_TO_ERRMAP_ID(FFA_RXTX_UNMAP)] = {
71 {
72 [NOT_SUPPORTED] =
73 "NOT_SUPPORTED: FFA_RXTX_UNMAP is not implemented at this FF-A instance",
74 [INVALID_PARAMETERS] =
75 "INVALID_PARAMETERS: No buffer pair registered on behalf of the caller",
76 },
77 },
78 [FFA_ID_TO_ERRMAP_ID(FFA_RX_RELEASE)] = {
79 {
80 [NOT_SUPPORTED] =
81 "NOT_SUPPORTED: FFA_RX_RELEASE is not implemented at this FF-A instance",
82 [DENIED] =
83 "DENIED: Caller did not have ownership of the RX buffer",
84 },
85 },
86 [FFA_ID_TO_ERRMAP_ID(FFA_RXTX_MAP)] = {
87 {
88 [NOT_SUPPORTED] =
89 "NOT_SUPPORTED: This function is not implemented at this FF-A instance",
90 [INVALID_PARAMETERS] =
91 "INVALID_PARAMETERS: Field(s) in input parameters incorrectly encoded",
92 [NO_MEMORY] =
93 "NO_MEMORY: Not enough memory",
94 [DENIED] =
95 "DENIED: Buffer pair already registered",
96 },
97 },
98};
99
100/**
101 * ffa_to_std_errno() - convert FF-A error code to standard error code
102 * @ffa_errno: Error code returned by the FF-A ABI
103 *
104 * Map the given FF-A error code as specified
105 * by the spec to a u-boot standard error code.
106 *
107 * Return:
108 *
109 * The standard error code on success. . Otherwise, failure
110 */
111static int ffa_to_std_errno(int ffa_errno)
112{
113 int err_idx = -ffa_errno;
114
115 /* Map the FF-A error code to the standard u-boot error code */
116 if (err_idx > 0 && err_idx < MAX_NUMBER_FFA_ERR)
117 return ffa_to_std_errmap[err_idx];
118 return -EINVAL;
119}
120
121/**
122 * ffa_print_error_log() - print the error log corresponding to the selected FF-A ABI
123 * @ffa_id: FF-A ABI ID
124 * @ffa_errno: Error code returned by the FF-A ABI
125 *
126 * Map the FF-A error code to the error log relevant to the
127 * selected FF-A ABI. Then the error log is printed.
128 *
129 * Return:
130 *
131 * 0 on success. . Otherwise, failure
132 */
133static int ffa_print_error_log(u32 ffa_id, int ffa_errno)
134{
135 int err_idx = -ffa_errno, abi_idx = 0;
136
137 /* Map the FF-A error code to the corresponding error log */
138
139 if (err_idx <= 0 || err_idx >= MAX_NUMBER_FFA_ERR)
140 return -EINVAL;
141
142 if (ffa_id < FFA_FIRST_ID || ffa_id > FFA_LAST_ID)
143 return -EINVAL;
144
145 abi_idx = FFA_ID_TO_ERRMAP_ID(ffa_id);
Abdellatif El Khlifi2f403d12023-08-04 14:33:40 +0100146
147 if (!err_msg_map[abi_idx].err_str[err_idx])
148 return -EINVAL;
149
150 log_err("%s\n", err_msg_map[abi_idx].err_str[err_idx]);
151
152 return 0;
153}
154
155/* FF-A ABIs implementation (U-Boot side) */
156
157/**
158 * invoke_ffa_fn() - SMC wrapper
159 * @args: FF-A ABI arguments to be copied to Xn registers
160 * @res: FF-A ABI return data to be copied from Xn registers
161 *
162 * Calls low level SMC implementation.
163 * This function should be implemented by the user driver.
164 */
165void __weak invoke_ffa_fn(ffa_value_t args, ffa_value_t *res)
166{
167}
168
169/**
170 * ffa_get_version_hdlr() - FFA_VERSION handler function
171 * @dev: The FF-A bus device
172 *
173 * Implement FFA_VERSION FF-A function
174 * to get from the secure world the FF-A framework version
175 * FFA_VERSION is used to discover the FF-A framework.
176 *
177 * Return:
178 *
179 * 0 on success. Otherwise, failure
180 */
181int ffa_get_version_hdlr(struct udevice *dev)
182{
183 u16 major, minor;
184 ffa_value_t res = {0};
185 int ffa_errno;
186 struct ffa_priv *uc_priv;
187
188 invoke_ffa_fn((ffa_value_t){
189 .a0 = FFA_SMC_32(FFA_VERSION), .a1 = FFA_VERSION_1_0,
190 }, &res);
191
192 ffa_errno = res.a0;
193 if (ffa_errno < 0) {
194 ffa_print_error_log(FFA_VERSION, ffa_errno);
195 return ffa_to_std_errno(ffa_errno);
196 }
197
198 major = GET_FFA_MAJOR_VERSION(res.a0);
199 minor = GET_FFA_MINOR_VERSION(res.a0);
200
Abdellatif El Khlifibf036192023-08-09 12:47:30 +0100201 log_debug("FF-A driver %d.%d\nFF-A framework %d.%d\n",
Abdellatif El Khlifi2f403d12023-08-04 14:33:40 +0100202 FFA_MAJOR_VERSION, FFA_MINOR_VERSION, major, minor);
203
204 if (major == FFA_MAJOR_VERSION && minor >= FFA_MINOR_VERSION) {
Abdellatif El Khlifibf036192023-08-09 12:47:30 +0100205 log_debug("FF-A versions are compatible\n");
Abdellatif El Khlifi2f403d12023-08-04 14:33:40 +0100206
207 if (dev) {
208 uc_priv = dev_get_uclass_priv(dev);
209 if (uc_priv)
210 uc_priv->fwk_version = res.a0;
211 }
212
213 return 0;
214 }
215
216 log_err("versions are incompatible\nExpected: %d.%d , Found: %d.%d\n",
217 FFA_MAJOR_VERSION, FFA_MINOR_VERSION, major, minor);
218
219 return -EPROTONOSUPPORT;
220}
221
222/**
223 * ffa_get_endpoint_id() - FFA_ID_GET handler function
224 * @dev: The FF-A bus device
225 *
226 * Implement FFA_ID_GET FF-A function
227 * to get from the secure world u-boot endpoint ID
228 *
229 * Return:
230 *
231 * 0 on success. Otherwise, failure
232 */
233static int ffa_get_endpoint_id(struct udevice *dev)
234{
235 ffa_value_t res = {0};
236 int ffa_errno;
237 struct ffa_priv *uc_priv = dev_get_uclass_priv(dev);
238
239 invoke_ffa_fn((ffa_value_t){
240 .a0 = FFA_SMC_32(FFA_ID_GET),
241 }, &res);
242
243 if (res.a0 == FFA_SMC_32(FFA_SUCCESS)) {
244 uc_priv->id = GET_SELF_ENDPOINT_ID((u32)res.a2);
245 log_debug("FF-A endpoint ID is %u\n", uc_priv->id);
246
247 return 0;
248 }
249
250 ffa_errno = res.a2;
251
252 ffa_print_error_log(FFA_ID_GET, ffa_errno);
253
254 return ffa_to_std_errno(ffa_errno);
255}
256
257/**
258 * ffa_set_rxtx_buffers_pages_cnt() - set the minimum number of pages in each of the RX/TX buffers
259 * @dev: The FF-A bus device
260 * @prop_field: properties field obtained from FFA_FEATURES ABI
261 *
262 * Set the minimum number of pages in each of the RX/TX buffers in uc_priv
263 *
264 * Return:
265 *
266 * rxtx_min_pages field contains the returned number of pages
267 * 0 on success. Otherwise, failure
268 */
269static int ffa_set_rxtx_buffers_pages_cnt(struct udevice *dev, u32 prop_field)
270{
271 struct ffa_priv *uc_priv = dev_get_uclass_priv(dev);
272
273 switch (prop_field) {
274 case RXTX_4K:
275 uc_priv->pair.rxtx_min_pages = 1;
276 break;
277 case RXTX_16K:
278 uc_priv->pair.rxtx_min_pages = 4;
279 break;
280 case RXTX_64K:
281 uc_priv->pair.rxtx_min_pages = 16;
282 break;
283 default:
284 log_err("RX/TX buffer size not supported\n");
285 return -EINVAL;
286 }
287
288 return 0;
289}
290
291/**
292 * ffa_get_rxtx_map_features_hdlr() - FFA_FEATURES handler function with FFA_RXTX_MAP argument
293 * @dev: The FF-A bus device
294 *
295 * Implement FFA_FEATURES FF-A function to retrieve the FFA_RXTX_MAP features
296 *
297 * Return:
298 *
299 * 0 on success. Otherwise, failure
300 */
301static int ffa_get_rxtx_map_features_hdlr(struct udevice *dev)
302{
303 ffa_value_t res = {0};
304 int ffa_errno;
305
306 invoke_ffa_fn((ffa_value_t){
307 .a0 = FFA_SMC_32(FFA_FEATURES),
308 .a1 = FFA_SMC_64(FFA_RXTX_MAP),
309 }, &res);
310
311 if (res.a0 == FFA_SMC_32(FFA_SUCCESS))
312 return ffa_set_rxtx_buffers_pages_cnt(dev, res.a2);
313
314 ffa_errno = res.a2;
315 ffa_print_error_log(FFA_FEATURES, ffa_errno);
316
317 return ffa_to_std_errno(ffa_errno);
318}
319
320/**
321 * ffa_free_rxtx_buffers() - free the RX/TX buffers
322 * @dev: The FF-A bus device
323 *
324 * Free the RX/TX buffers
325 */
326static void ffa_free_rxtx_buffers(struct udevice *dev)
327{
328 struct ffa_priv *uc_priv = dev_get_uclass_priv(dev);
329
330 log_debug("Freeing FF-A RX/TX buffers\n");
331
332 if (uc_priv->pair.rxbuf) {
333 free(uc_priv->pair.rxbuf);
334 uc_priv->pair.rxbuf = NULL;
335 }
336
337 if (uc_priv->pair.txbuf) {
338 free(uc_priv->pair.txbuf);
339 uc_priv->pair.txbuf = NULL;
340 }
341}
342
343/**
344 * ffa_alloc_rxtx_buffers() - allocate the RX/TX buffers
345 * @dev: The FF-A bus device
346 *
347 * Used by ffa_map_rxtx_buffers to allocate
348 * the RX/TX buffers before mapping them. The allocated memory is physically
349 * contiguous since memalign ends up calling malloc which allocates
350 * contiguous memory in u-boot.
351 * The size of the memory allocated is the minimum allowed.
352 *
353 * Return:
354 *
355 * 0 on success. Otherwise, failure
356 */
357static int ffa_alloc_rxtx_buffers(struct udevice *dev)
358{
359 u64 bytes;
360 struct ffa_priv *uc_priv = dev_get_uclass_priv(dev);
361
362 log_debug("Using %lu 4KB page(s) for FF-A RX/TX buffers size\n",
363 uc_priv->pair.rxtx_min_pages);
364
365 bytes = uc_priv->pair.rxtx_min_pages * SZ_4K;
366
367 /*
368 * The alignment of the RX and TX buffers must be equal
369 * to the larger translation granule size
370 * Assumption: Memory allocated with memalign is always physically contiguous
371 */
372
373 uc_priv->pair.rxbuf = memalign(bytes, bytes);
374 if (!uc_priv->pair.rxbuf) {
375 log_err("failure to allocate RX buffer\n");
376 return -ENOBUFS;
377 }
378
379 log_debug("FF-A RX buffer at virtual address %p\n", uc_priv->pair.rxbuf);
380
381 uc_priv->pair.txbuf = memalign(bytes, bytes);
382 if (!uc_priv->pair.txbuf) {
383 free(uc_priv->pair.rxbuf);
384 uc_priv->pair.rxbuf = NULL;
385 log_err("failure to allocate the TX buffer\n");
386 return -ENOBUFS;
387 }
388
389 log_debug("FF-A TX buffer at virtual address %p\n", uc_priv->pair.txbuf);
390
391 /* Make sure the buffers are cleared before use */
392 memset(uc_priv->pair.rxbuf, 0, bytes);
393 memset(uc_priv->pair.txbuf, 0, bytes);
394
395 return 0;
396}
397
398/**
399 * ffa_map_rxtx_buffers_hdlr() - FFA_RXTX_MAP handler function
400 * @dev: The FF-A bus device
401 *
402 * Implement FFA_RXTX_MAP FF-A function to map the RX/TX buffers
403 *
404 * Return:
405 *
406 * 0 on success. Otherwise, failure
407 */
408static int ffa_map_rxtx_buffers_hdlr(struct udevice *dev)
409{
410 int ret;
411 ffa_value_t res = {0};
412 int ffa_errno;
413 struct ffa_priv *uc_priv = dev_get_uclass_priv(dev);
414
415 ret = ffa_alloc_rxtx_buffers(dev);
416 if (ret)
417 return ret;
418
419 /*
420 * we need to pass the physical addresses of the RX/TX buffers
421 * in u-boot physical/virtual mapping is 1:1
422 * no need to convert from virtual to physical
423 */
424
425 invoke_ffa_fn((ffa_value_t){
426 .a0 = FFA_SMC_64(FFA_RXTX_MAP),
427 .a1 = map_to_sysmem(uc_priv->pair.txbuf),
428 .a2 = map_to_sysmem(uc_priv->pair.rxbuf),
429 .a3 = uc_priv->pair.rxtx_min_pages,
430 }, &res);
431
432 if (res.a0 == FFA_SMC_32(FFA_SUCCESS)) {
433 log_debug("FF-A RX/TX buffers mapped\n");
434 return 0;
435 }
436
437 ffa_errno = res.a2;
438 ffa_print_error_log(FFA_RXTX_MAP, ffa_errno);
439
440 ffa_free_rxtx_buffers(dev);
441
442 return ffa_to_std_errno(ffa_errno);
443}
444
445/**
446 * ffa_unmap_rxtx_buffers_hdlr() - FFA_RXTX_UNMAP handler function
447 * @dev: The FF-A bus device
448 *
449 * Implement FFA_RXTX_UNMAP FF-A function to unmap the RX/TX buffers
450 *
451 * Return:
452 *
453 * 0 on success. Otherwise, failure
454 */
455int ffa_unmap_rxtx_buffers_hdlr(struct udevice *dev)
456{
457 ffa_value_t res = {0};
458 int ffa_errno;
459 struct ffa_priv *uc_priv;
460
461 log_debug("unmapping FF-A RX/TX buffers\n");
462
463 uc_priv = dev_get_uclass_priv(dev);
464
465 invoke_ffa_fn((ffa_value_t){
466 .a0 = FFA_SMC_32(FFA_RXTX_UNMAP),
467 .a1 = PREP_SELF_ENDPOINT_ID(uc_priv->id),
468 }, &res);
469
470 if (res.a0 == FFA_SMC_32(FFA_SUCCESS)) {
471 ffa_free_rxtx_buffers(dev);
472 return 0;
473 }
474
475 ffa_errno = res.a2;
476 ffa_print_error_log(FFA_RXTX_UNMAP, ffa_errno);
477
478 return ffa_to_std_errno(ffa_errno);
479}
480
481/**
482 * ffa_release_rx_buffer_hdlr() - FFA_RX_RELEASE handler function
483 * @dev: The FF-A bus device
484 *
485 * Invoke FFA_RX_RELEASE FF-A function to release the ownership of the RX buffer
486 *
487 * Return:
488 *
489 * 0 on success. Otherwise, failure
490 */
491static int ffa_release_rx_buffer_hdlr(struct udevice *dev)
492{
493 ffa_value_t res = {0};
494 int ffa_errno;
495
496 invoke_ffa_fn((ffa_value_t){
497 .a0 = FFA_SMC_32(FFA_RX_RELEASE),
498 }, &res);
499
500 if (res.a0 == FFA_SMC_32(FFA_SUCCESS))
501 return 0;
502
503 ffa_errno = res.a2;
504 ffa_print_error_log(FFA_RX_RELEASE, ffa_errno);
505
506 return ffa_to_std_errno(ffa_errno);
507}
508
509/**
510 * ffa_uuid_are_identical() - check whether two given UUIDs are identical
511 * @uuid1: first UUID
512 * @uuid2: second UUID
513 *
514 * Used by ffa_read_partitions_info to search for a UUID in the partitions descriptors table
515 *
516 * Return:
517 *
518 * 1 when UUIDs match. Otherwise, 0
519 */
520static bool ffa_uuid_are_identical(const struct ffa_partition_uuid *uuid1,
521 const struct ffa_partition_uuid *uuid2)
522{
523 if (!uuid1 || !uuid2)
524 return 0;
525
526 return !memcmp(uuid1, uuid2, sizeof(struct ffa_partition_uuid));
527}
528
529/**
530 * ffa_read_partitions_info() - read queried partition data
531 * @dev: The FF-A bus device
532 * @count: The number of partitions queried
533 * @part_uuid: Pointer to the partition(s) UUID
534 *
535 * Read the partitions information returned by the FFA_PARTITION_INFO_GET and saves it in uc_priv
536 *
537 * Return:
538 *
539 * uc_priv is updated with the partition(s) information
540 * 0 is returned on success. Otherwise, failure
541 */
542static int ffa_read_partitions_info(struct udevice *dev, u32 count,
543 struct ffa_partition_uuid *part_uuid)
544{
545 struct ffa_priv *uc_priv = dev_get_uclass_priv(dev);
546
547 if (!count) {
548 log_err("no partition detected\n");
549 return -ENODATA;
550 }
551
552 log_debug("Reading FF-A partitions data from the RX buffer\n");
553
554 if (!part_uuid) {
555 /* Querying information of all partitions */
556 u64 buf_bytes;
557 u64 data_bytes;
558 u32 desc_idx;
559 struct ffa_partition_info *parts_info;
560
561 data_bytes = count * sizeof(struct ffa_partition_desc);
562
563 buf_bytes = uc_priv->pair.rxtx_min_pages * SZ_4K;
564
565 if (data_bytes > buf_bytes) {
566 log_err("partitions data size exceeds the RX buffer size:\n");
567 log_err(" sizes in bytes: data %llu , RX buffer %llu\n",
568 data_bytes,
569 buf_bytes);
570
571 return -ENOMEM;
572 }
573
574 uc_priv->partitions.descs = devm_kmalloc(dev, data_bytes, __GFP_ZERO);
575 if (!uc_priv->partitions.descs) {
576 log_err("cannot allocate partitions data buffer\n");
577 return -ENOMEM;
578 }
579
580 parts_info = uc_priv->pair.rxbuf;
581
582 for (desc_idx = 0 ; desc_idx < count ; desc_idx++) {
583 uc_priv->partitions.descs[desc_idx].info =
584 parts_info[desc_idx];
585
586 log_debug("FF-A partition ID %x : info cached\n",
587 uc_priv->partitions.descs[desc_idx].info.id);
588 }
589
590 uc_priv->partitions.count = count;
591
592 log_debug("%d FF-A partition(s) found and cached\n", count);
593
594 } else {
595 u32 rx_desc_idx, cached_desc_idx;
596 struct ffa_partition_info *parts_info;
597 u8 desc_found;
598
599 parts_info = uc_priv->pair.rxbuf;
600
601 /*
602 * Search for the SP IDs read from the RX buffer
603 * in the already cached SPs.
604 * Update the UUID when ID found.
605 */
606 for (rx_desc_idx = 0; rx_desc_idx < count ; rx_desc_idx++) {
607 desc_found = 0;
608
609 /* Search the current ID in the cached partitions */
610 for (cached_desc_idx = 0;
611 cached_desc_idx < uc_priv->partitions.count;
612 cached_desc_idx++) {
613 /* Save the UUID */
614 if (uc_priv->partitions.descs[cached_desc_idx].info.id ==
615 parts_info[rx_desc_idx].id) {
616 uc_priv->partitions.descs[cached_desc_idx].sp_uuid =
617 *part_uuid;
618
619 desc_found = 1;
620 break;
621 }
622 }
623
624 if (!desc_found)
625 return -ENODATA;
626 }
627 }
628
629 return 0;
630}
631
632/**
633 * ffa_query_partitions_info() - invoke FFA_PARTITION_INFO_GET and save partitions data
634 * @dev: The FF-A bus device
635 * @part_uuid: Pointer to the partition(s) UUID
636 * @pcount: Pointer to the number of partitions variable filled when querying
637 *
638 * Execute the FFA_PARTITION_INFO_GET to query the partitions data.
639 * Then, call ffa_read_partitions_info to save the data in uc_priv.
640 *
641 * After reading the data the RX buffer is released using ffa_release_rx_buffer
642 *
643 * Return:
644 *
645 * When part_uuid is NULL, all partitions data are retrieved from secure world
646 * When part_uuid is non NULL, data for partitions matching the given UUID are
647 * retrieved and the number of partitions is returned
648 * 0 is returned on success. Otherwise, failure
649 */
650static int ffa_query_partitions_info(struct udevice *dev, struct ffa_partition_uuid *part_uuid,
651 u32 *pcount)
652{
653 struct ffa_partition_uuid query_uuid = {0};
654 ffa_value_t res = {0};
655 int ffa_errno;
656
657 /*
658 * If a UUID is specified. Information for one or more
659 * partitions in the system is queried. Otherwise, information
660 * for all installed partitions is queried
661 */
662
663 if (part_uuid) {
664 if (!pcount)
665 return -EINVAL;
666
667 query_uuid = *part_uuid;
668 } else if (pcount) {
669 return -EINVAL;
670 }
671
672 invoke_ffa_fn((ffa_value_t){
673 .a0 = FFA_SMC_32(FFA_PARTITION_INFO_GET),
674 .a1 = query_uuid.a1,
675 .a2 = query_uuid.a2,
676 .a3 = query_uuid.a3,
677 .a4 = query_uuid.a4,
678 }, &res);
679
680 if (res.a0 == FFA_SMC_32(FFA_SUCCESS)) {
681 int ret;
682
683 /*
684 * res.a2 contains the count of partition information descriptors
685 * populated in the RX buffer
686 */
687 if (res.a2) {
688 ret = ffa_read_partitions_info(dev, (u32)res.a2, part_uuid);
689 if (ret) {
690 log_err("failed reading SP(s) data , err (%d)\n", ret);
691 ffa_release_rx_buffer_hdlr(dev);
692 return -EINVAL;
693 }
694 }
695
696 /* Return the SP count (when querying using a UUID) */
697 if (pcount)
698 *pcount = (u32)res.a2;
699
700 /*
701 * After calling FFA_PARTITION_INFO_GET the buffer ownership
702 * is assigned to the consumer (u-boot). So, we need to give
703 * the ownership back to the SPM or hypervisor
704 */
705 ret = ffa_release_rx_buffer_hdlr(dev);
706
707 return ret;
708 }
709
710 ffa_errno = res.a2;
711 ffa_print_error_log(FFA_PARTITION_INFO_GET, ffa_errno);
712
713 return ffa_to_std_errno(ffa_errno);
714}
715
716/**
717 * ffa_get_partitions_info_hdlr() - FFA_PARTITION_INFO_GET handler function
718 * @uuid_str: pointer to the UUID string
719 * @sp_count: address of the variable containing the number of partitions matching the UUID
720 * The variable is set by the driver
721 * @sp_descs: address of the descriptors of the partitions matching the UUID
722 * The address is set by the driver
723 *
724 * Return the number of partitions and their descriptors matching the UUID
725 *
726 * Query the secure partition data from uc_priv.
727 * If not found, invoke FFA_PARTITION_INFO_GET FF-A function to query the partition information
728 * from secure world.
729 *
730 * A client of the FF-A driver should know the UUID of the service it wants to
731 * access. It should use the UUID to request the FF-A driver to provide the
732 * partition(s) information of the service. The FF-A driver uses
733 * PARTITION_INFO_GET to obtain this information. This is implemented through
734 * ffa_get_partitions_info_hdlr() function.
735 * If the partition(s) matching the UUID found, the partition(s) information and the
736 * number are returned.
737 * If no partition matching the UUID is found in the cached area, a new FFA_PARTITION_INFO_GET
738 * call is issued.
739 * If not done yet, the UUID is updated in the cached area.
740 * This assumes that partitions data does not change in the secure world.
741 * Otherwise u-boot will have an outdated partition data. The benefit of caching
742 * the information in the FF-A driver is to accommodate discovery after
743 * ExitBootServices().
744 *
745 * Return:
746 *
747 * @sp_count: the number of partitions
748 * @sp_descs: address of the partitions descriptors
749 *
750 * On success 0 is returned. Otherwise, failure
751 */
752int ffa_get_partitions_info_hdlr(struct udevice *dev, const char *uuid_str,
753 u32 *sp_count, struct ffa_partition_desc **sp_descs)
754{
755 u32 i;
756 struct ffa_partition_uuid part_uuid = {0};
757 struct ffa_priv *uc_priv;
758 struct ffa_partition_desc *rx_descs;
759
760 uc_priv = dev_get_uclass_priv(dev);
761
762 if (!uc_priv->partitions.count || !uc_priv->partitions.descs) {
763 log_err("no partition installed\n");
764 return -EINVAL;
765 }
766
767 if (!uuid_str) {
768 log_err("no UUID provided\n");
769 return -EINVAL;
770 }
771
772 if (!sp_count) {
773 log_err("no count argument provided\n");
774 return -EINVAL;
775 }
776
777 if (!sp_descs) {
778 log_err("no info argument provided\n");
779 return -EINVAL;
780 }
781
782 if (uuid_str_to_le_bin(uuid_str, (unsigned char *)&part_uuid)) {
783 log_err("invalid UUID\n");
784 return -EINVAL;
785 }
786
787 log_debug("Searching FF-A partitions using the provided UUID\n");
788
789 *sp_count = 0;
790 *sp_descs = uc_priv->pair.rxbuf;
791 rx_descs = *sp_descs;
792
793 /* Search in the cached partitions */
794 for (i = 0; i < uc_priv->partitions.count; i++)
795 if (ffa_uuid_are_identical(&uc_priv->partitions.descs[i].sp_uuid,
796 &part_uuid)) {
797 log_debug("FF-A partition ID %x matches the provided UUID\n",
798 uc_priv->partitions.descs[i].info.id);
799
800 (*sp_count)++;
801 *rx_descs++ = uc_priv->partitions.descs[i];
802 }
803
804 if (!(*sp_count)) {
805 int ret;
806
807 log_debug("No FF-A partition found. Querying framework ...\n");
808
809 ret = ffa_query_partitions_info(dev, &part_uuid, sp_count);
810
811 if (!ret) {
812 log_debug("Number of FF-A partition(s) matching the UUID: %d\n", *sp_count);
813
814 if (*sp_count)
815 ret = ffa_get_partitions_info_hdlr(dev, uuid_str, sp_count,
816 sp_descs);
817 else
818 ret = -ENODATA;
819 }
820
821 return ret;
822 }
823
824 return 0;
825}
826
827/**
828 * ffa_cache_partitions_info() - Query and saves all secure partitions data
829 * @dev: The FF-A bus device
830 *
831 * Invoke FFA_PARTITION_INFO_GET FF-A function to query from secure world
832 * all partitions information.
833 *
834 * The FFA_PARTITION_INFO_GET call is issued with nil UUID as an argument.
835 * All installed partitions information are returned. We cache them in uc_priv
836 * and we keep the UUID field empty (in FF-A 1.0 UUID is not provided by the partition descriptor)
837 *
838 * Called at the device probing level.
839 * ffa_cache_partitions_info uses ffa_query_partitions_info to get the data
840 *
841 * Return:
842 *
843 * 0 on success. Otherwise, failure
844 */
845static int ffa_cache_partitions_info(struct udevice *dev)
846{
847 return ffa_query_partitions_info(dev, NULL, NULL);
848}
849
850/**
851 * ffa_msg_send_direct_req_hdlr() - FFA_MSG_SEND_DIRECT_{REQ,RESP} handler function
852 * @dev: The FF-A bus device
853 * @dst_part_id: destination partition ID
854 * @msg: pointer to the message data preallocated by the client (in/out)
855 * @is_smc64: select 64-bit or 32-bit FF-A ABI
856 *
857 * Implement FFA_MSG_SEND_DIRECT_{REQ,RESP}
858 * FF-A functions.
859 *
860 * FFA_MSG_SEND_DIRECT_REQ is used to send the data to the secure partition.
861 * The response from the secure partition is handled by reading the
862 * FFA_MSG_SEND_DIRECT_RESP arguments.
863 *
864 * The maximum size of the data that can be exchanged is 40 bytes which is
865 * sizeof(struct ffa_send_direct_data) as defined by the FF-A specification 1.0
866 * in the section relevant to FFA_MSG_SEND_DIRECT_{REQ,RESP}
867 *
868 * Return:
869 *
870 * 0 on success. Otherwise, failure
871 */
872int ffa_msg_send_direct_req_hdlr(struct udevice *dev, u16 dst_part_id,
873 struct ffa_send_direct_data *msg, bool is_smc64)
874{
875 ffa_value_t res = {0};
876 int ffa_errno;
877 u64 req_mode, resp_mode;
878 struct ffa_priv *uc_priv;
879
880 uc_priv = dev_get_uclass_priv(dev);
881
882 /* No partition installed */
883 if (!uc_priv->partitions.count || !uc_priv->partitions.descs)
884 return -ENODEV;
885
886 if (is_smc64) {
887 req_mode = FFA_SMC_64(FFA_MSG_SEND_DIRECT_REQ);
888 resp_mode = FFA_SMC_64(FFA_MSG_SEND_DIRECT_RESP);
889 } else {
890 req_mode = FFA_SMC_32(FFA_MSG_SEND_DIRECT_REQ);
891 resp_mode = FFA_SMC_32(FFA_MSG_SEND_DIRECT_RESP);
892 }
893
894 invoke_ffa_fn((ffa_value_t){
895 .a0 = req_mode,
896 .a1 = PREP_SELF_ENDPOINT_ID(uc_priv->id) |
897 PREP_PART_ENDPOINT_ID(dst_part_id),
898 .a2 = 0,
899 .a3 = msg->data0,
900 .a4 = msg->data1,
901 .a5 = msg->data2,
902 .a6 = msg->data3,
903 .a7 = msg->data4,
904 }, &res);
905
906 while (res.a0 == FFA_SMC_32(FFA_INTERRUPT))
907 invoke_ffa_fn((ffa_value_t){
908 .a0 = FFA_SMC_32(FFA_RUN),
909 .a1 = res.a1,
910 }, &res);
911
912 if (res.a0 == FFA_SMC_32(FFA_SUCCESS)) {
913 /* Message sent with no response */
914 return 0;
915 }
916
917 if (res.a0 == resp_mode) {
918 /* Message sent with response extract the return data */
919 msg->data0 = res.a3;
920 msg->data1 = res.a4;
921 msg->data2 = res.a5;
922 msg->data3 = res.a6;
923 msg->data4 = res.a7;
924
925 return 0;
926 }
927
928 ffa_errno = res.a2;
929 return ffa_to_std_errno(ffa_errno);
930}
931
932/* FF-A driver operations (used by clients for communicating with FF-A)*/
933
934/**
935 * ffa_partition_info_get() - FFA_PARTITION_INFO_GET driver operation
936 * @uuid_str: pointer to the UUID string
937 * @sp_count: address of the variable containing the number of partitions matching the UUID
938 * The variable is set by the driver
939 * @sp_descs: address of the descriptors of the partitions matching the UUID
940 * The address is set by the driver
941 *
942 * Driver operation for FFA_PARTITION_INFO_GET.
943 * Please see ffa_get_partitions_info_hdlr() description for more details.
944 *
945 * Return:
946 *
947 * @sp_count: the number of partitions
948 * @sp_descs: address of the partitions descriptors
949 *
950 * On success 0 is returned. Otherwise, failure
951 */
952int ffa_partition_info_get(struct udevice *dev, const char *uuid_str,
953 u32 *sp_count, struct ffa_partition_desc **sp_descs)
954{
955 struct ffa_bus_ops *ops = ffa_get_ops(dev);
956
957 if (!ops->partition_info_get)
958 return -ENOSYS;
959
960 return ops->partition_info_get(dev, uuid_str, sp_count, sp_descs);
961}
962
963/**
964 * ffa_sync_send_receive() - FFA_MSG_SEND_DIRECT_{REQ,RESP} driver operation
965 * @dev: The FF-A bus device
966 * @dst_part_id: destination partition ID
967 * @msg: pointer to the message data preallocated by the client (in/out)
968 * @is_smc64: select 64-bit or 32-bit FF-A ABI
969 *
970 * Driver operation for FFA_MSG_SEND_DIRECT_{REQ,RESP}.
971 * Please see ffa_msg_send_direct_req_hdlr() description for more details.
972 *
973 * Return:
974 *
975 * 0 on success. Otherwise, failure
976 */
977int ffa_sync_send_receive(struct udevice *dev, u16 dst_part_id,
978 struct ffa_send_direct_data *msg, bool is_smc64)
979{
980 struct ffa_bus_ops *ops = ffa_get_ops(dev);
981
982 if (!ops->sync_send_receive)
983 return -ENOSYS;
984
985 return ops->sync_send_receive(dev, dst_part_id, msg, is_smc64);
986}
987
988/**
989 * ffa_rxtx_unmap() - FFA_RXTX_UNMAP driver operation
990 * @dev: The FF-A bus device
991 *
992 * Driver operation for FFA_RXTX_UNMAP.
993 * Please see ffa_unmap_rxtx_buffers_hdlr() description for more details.
994 *
995 * Return:
996 *
997 * 0 on success. Otherwise, failure
998 */
999int ffa_rxtx_unmap(struct udevice *dev)
1000{
1001 struct ffa_bus_ops *ops = ffa_get_ops(dev);
1002
1003 if (!ops->rxtx_unmap)
1004 return -ENOSYS;
1005
1006 return ops->rxtx_unmap(dev);
1007}
1008
1009/**
1010 * ffa_do_probe() - probing FF-A framework
1011 * @dev: the FF-A bus device (arm_ffa)
1012 *
1013 * Probing is triggered on demand by clients searching for the uclass.
1014 * At probe level the following actions are done:
1015 * - saving the FF-A framework version in uc_priv
1016 * - querying from secure world the u-boot endpoint ID
1017 * - querying from secure world the supported features of FFA_RXTX_MAP
1018 * - mapping the RX/TX buffers
1019 * - querying from secure world all the partitions information
1020 *
1021 * All data queried from secure world is saved in uc_priv.
1022 *
1023 * Return:
1024 *
1025 * 0 on success. Otherwise, failure
1026 */
1027static int ffa_do_probe(struct udevice *dev)
1028{
1029 int ret;
1030
1031 ret = ffa_get_version_hdlr(dev);
1032 if (ret)
1033 return ret;
1034
1035 ret = ffa_get_endpoint_id(dev);
1036 if (ret)
1037 return ret;
1038
1039 ret = ffa_get_rxtx_map_features_hdlr(dev);
1040 if (ret)
1041 return ret;
1042
1043 ret = ffa_map_rxtx_buffers_hdlr(dev);
1044 if (ret)
1045 return ret;
1046
1047 ret = ffa_cache_partitions_info(dev);
1048 if (ret) {
1049 ffa_unmap_rxtx_buffers_hdlr(dev);
1050 return ret;
1051 }
1052
1053 return 0;
1054}
1055
1056UCLASS_DRIVER(ffa) = {
1057 .name = "ffa",
1058 .id = UCLASS_FFA,
1059 .pre_probe = ffa_do_probe,
1060 .pre_remove = ffa_unmap_rxtx_buffers_hdlr,
1061 .per_device_auto = sizeof(struct ffa_priv)
1062};