1 // SPDX-License-Identifier: GPL-2.0-only
3 * Arm Firmware Framework for ARMv8-A(FFA) interface driver
5 * The Arm FFA specification[1] describes a software architecture to
6 * leverages the virtualization extension to isolate software images
7 * provided by an ecosystem of vendors from each other and describes
8 * interfaces that standardize communication between the various software
9 * images including communication between images in the Secure world and
10 * Normal world. Any Hypervisor could use the FFA interfaces to enable
11 * communication between VMs it manages.
13 * The Hypervisor a.k.a Partition managers in FFA terminology can assign
14 * system resources(Memory regions, Devices, CPU cycles) to the partitions
15 * and manage isolation amongst them.
17 * [1] https://developer.arm.com/docs/den0077/latest
19 * Copyright (C) 2021 ARM Ltd.
22 #define DRIVER_NAME "ARM FF-A"
23 #define pr_fmt(fmt) DRIVER_NAME ": " fmt
25 #include <linux/arm_ffa.h>
26 #include <linux/bitfield.h>
27 #include <linux/device.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
32 #include <linux/scatterlist.h>
33 #include <linux/slab.h>
34 #include <linux/uuid.h>
38 #define FFA_DRIVER_VERSION FFA_VERSION_1_0
39 #define FFA_MIN_VERSION FFA_VERSION_1_0
41 #define SENDER_ID_MASK GENMASK(31, 16)
42 #define RECEIVER_ID_MASK GENMASK(15, 0)
43 #define SENDER_ID(x) ((u16)(FIELD_GET(SENDER_ID_MASK, (x))))
44 #define RECEIVER_ID(x) ((u16)(FIELD_GET(RECEIVER_ID_MASK, (x))))
45 #define PACK_TARGET_INFO(s, r) \
46 (FIELD_PREP(SENDER_ID_MASK, (s)) | FIELD_PREP(RECEIVER_ID_MASK, (r)))
49 * Keeping RX TX buffer size as 4K for now
50 * 64K may be preferred to keep it min a page in 64K PAGE_SIZE config
52 #define RXTX_BUFFER_SIZE SZ_4K
54 static ffa_fn *invoke_ffa_fn;
56 static const int ffa_linux_errmap[] = {
57 /* better than switch case as long as return value is continuous */
58 0, /* FFA_RET_SUCCESS */
59 -EOPNOTSUPP, /* FFA_RET_NOT_SUPPORTED */
60 -EINVAL, /* FFA_RET_INVALID_PARAMETERS */
61 -ENOMEM, /* FFA_RET_NO_MEMORY */
62 -EBUSY, /* FFA_RET_BUSY */
63 -EINTR, /* FFA_RET_INTERRUPTED */
64 -EACCES, /* FFA_RET_DENIED */
65 -EAGAIN, /* FFA_RET_RETRY */
66 -ECANCELED, /* FFA_RET_ABORTED */
69 static inline int ffa_to_linux_errno(int errno)
73 if (err_idx >= 0 && err_idx < ARRAY_SIZE(ffa_linux_errmap))
74 return ffa_linux_errmap[err_idx];
81 struct mutex rx_lock; /* lock to protect Rx buffer */
82 struct mutex tx_lock; /* lock to protect Tx buffer */
88 static struct ffa_drv_info *drv_info;
91 * The driver must be able to support all the versions from the earliest
92 * supported FFA_MIN_VERSION to the latest supported FFA_DRIVER_VERSION.
93 * The specification states that if firmware supports a FFA implementation
94 * that is incompatible with and at a greater version number than specified
95 * by the caller(FFA_DRIVER_VERSION passed as parameter to FFA_VERSION),
96 * it must return the NOT_SUPPORTED error code.
98 static u32 ffa_compatible_version_find(u32 version)
100 u16 major = FFA_MAJOR_VERSION(version), minor = FFA_MINOR_VERSION(version);
101 u16 drv_major = FFA_MAJOR_VERSION(FFA_DRIVER_VERSION);
102 u16 drv_minor = FFA_MINOR_VERSION(FFA_DRIVER_VERSION);
104 if ((major < drv_major) || (major == drv_major && minor <= drv_minor))
107 pr_info("Firmware version higher than driver version, downgrading\n");
108 return FFA_DRIVER_VERSION;
111 static int ffa_version_check(u32 *version)
115 invoke_ffa_fn((ffa_value_t){
116 .a0 = FFA_VERSION, .a1 = FFA_DRIVER_VERSION,
119 if (ver.a0 == FFA_RET_NOT_SUPPORTED) {
120 pr_info("FFA_VERSION returned not supported\n");
124 if (ver.a0 < FFA_MIN_VERSION) {
125 pr_err("Incompatible v%d.%d! Earliest supported v%d.%d\n",
126 FFA_MAJOR_VERSION(ver.a0), FFA_MINOR_VERSION(ver.a0),
127 FFA_MAJOR_VERSION(FFA_MIN_VERSION),
128 FFA_MINOR_VERSION(FFA_MIN_VERSION));
132 pr_info("Driver version %d.%d\n", FFA_MAJOR_VERSION(FFA_DRIVER_VERSION),
133 FFA_MINOR_VERSION(FFA_DRIVER_VERSION));
134 pr_info("Firmware version %d.%d found\n", FFA_MAJOR_VERSION(ver.a0),
135 FFA_MINOR_VERSION(ver.a0));
136 *version = ffa_compatible_version_find(ver.a0);
141 static int ffa_rx_release(void)
145 invoke_ffa_fn((ffa_value_t){
146 .a0 = FFA_RX_RELEASE,
149 if (ret.a0 == FFA_ERROR)
150 return ffa_to_linux_errno((int)ret.a2);
152 /* check for ret.a0 == FFA_RX_RELEASE ? */
157 static int ffa_rxtx_map(phys_addr_t tx_buf, phys_addr_t rx_buf, u32 pg_cnt)
161 invoke_ffa_fn((ffa_value_t){
162 .a0 = FFA_FN_NATIVE(RXTX_MAP),
163 .a1 = tx_buf, .a2 = rx_buf, .a3 = pg_cnt,
166 if (ret.a0 == FFA_ERROR)
167 return ffa_to_linux_errno((int)ret.a2);
172 static int ffa_rxtx_unmap(u16 vm_id)
176 invoke_ffa_fn((ffa_value_t){
177 .a0 = FFA_RXTX_UNMAP, .a1 = PACK_TARGET_INFO(vm_id, 0),
180 if (ret.a0 == FFA_ERROR)
181 return ffa_to_linux_errno((int)ret.a2);
186 #define PARTITION_INFO_GET_RETURN_COUNT_ONLY BIT(0)
188 /* buffer must be sizeof(struct ffa_partition_info) * num_partitions */
190 __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,
191 struct ffa_partition_info *buffer, int num_partitions)
193 int idx, count, flags = 0, sz, buf_sz;
194 ffa_value_t partition_info;
196 if (drv_info->version > FFA_VERSION_1_0 &&
197 (!buffer || !num_partitions)) /* Just get the count for now */
198 flags = PARTITION_INFO_GET_RETURN_COUNT_ONLY;
200 mutex_lock(&drv_info->rx_lock);
201 invoke_ffa_fn((ffa_value_t){
202 .a0 = FFA_PARTITION_INFO_GET,
203 .a1 = uuid0, .a2 = uuid1, .a3 = uuid2, .a4 = uuid3,
207 if (partition_info.a0 == FFA_ERROR) {
208 mutex_unlock(&drv_info->rx_lock);
209 return ffa_to_linux_errno((int)partition_info.a2);
212 count = partition_info.a2;
214 if (drv_info->version > FFA_VERSION_1_0) {
215 buf_sz = sz = partition_info.a3;
216 if (sz > sizeof(*buffer))
217 buf_sz = sizeof(*buffer);
219 /* FFA_VERSION_1_0 lacks size in the response */
223 if (buffer && count <= num_partitions)
224 for (idx = 0; idx < count; idx++)
225 memcpy(buffer + idx, drv_info->rx_buffer + idx * sz,
230 mutex_unlock(&drv_info->rx_lock);
235 /* buffer is allocated and caller must free the same if returned count > 0 */
237 ffa_partition_probe(const uuid_t *uuid, struct ffa_partition_info **buffer)
241 struct ffa_partition_info *pbuf;
243 export_uuid((u8 *)uuid0_4, uuid);
244 count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1], uuid0_4[2],
245 uuid0_4[3], NULL, 0);
249 pbuf = kcalloc(count, sizeof(*pbuf), GFP_KERNEL);
253 count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1], uuid0_4[2],
254 uuid0_4[3], pbuf, count);
263 #define VM_ID_MASK GENMASK(15, 0)
264 static int ffa_id_get(u16 *vm_id)
268 invoke_ffa_fn((ffa_value_t){
272 if (id.a0 == FFA_ERROR)
273 return ffa_to_linux_errno((int)id.a2);
275 *vm_id = FIELD_GET(VM_ID_MASK, (id.a2));
280 static int ffa_msg_send_direct_req(u16 src_id, u16 dst_id, bool mode_32bit,
281 struct ffa_send_direct_data *data)
283 u32 req_id, resp_id, src_dst_ids = PACK_TARGET_INFO(src_id, dst_id);
287 req_id = FFA_MSG_SEND_DIRECT_REQ;
288 resp_id = FFA_MSG_SEND_DIRECT_RESP;
290 req_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_REQ);
291 resp_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_RESP);
294 invoke_ffa_fn((ffa_value_t){
295 .a0 = req_id, .a1 = src_dst_ids, .a2 = 0,
296 .a3 = data->data0, .a4 = data->data1, .a5 = data->data2,
297 .a6 = data->data3, .a7 = data->data4,
300 while (ret.a0 == FFA_INTERRUPT)
301 invoke_ffa_fn((ffa_value_t){
302 .a0 = FFA_RUN, .a1 = ret.a1,
305 if (ret.a0 == FFA_ERROR)
306 return ffa_to_linux_errno((int)ret.a2);
308 if (ret.a0 == resp_id) {
309 data->data0 = ret.a3;
310 data->data1 = ret.a4;
311 data->data2 = ret.a5;
312 data->data3 = ret.a6;
313 data->data4 = ret.a7;
320 static int ffa_mem_first_frag(u32 func_id, phys_addr_t buf, u32 buf_sz,
321 u32 frag_len, u32 len, u64 *handle)
325 invoke_ffa_fn((ffa_value_t){
326 .a0 = func_id, .a1 = len, .a2 = frag_len,
327 .a3 = buf, .a4 = buf_sz,
330 while (ret.a0 == FFA_MEM_OP_PAUSE)
331 invoke_ffa_fn((ffa_value_t){
332 .a0 = FFA_MEM_OP_RESUME,
333 .a1 = ret.a1, .a2 = ret.a2,
336 if (ret.a0 == FFA_ERROR)
337 return ffa_to_linux_errno((int)ret.a2);
339 if (ret.a0 == FFA_SUCCESS) {
341 *handle = PACK_HANDLE(ret.a2, ret.a3);
342 } else if (ret.a0 == FFA_MEM_FRAG_RX) {
344 *handle = PACK_HANDLE(ret.a1, ret.a2);
352 static int ffa_mem_next_frag(u64 handle, u32 frag_len)
356 invoke_ffa_fn((ffa_value_t){
357 .a0 = FFA_MEM_FRAG_TX,
358 .a1 = HANDLE_LOW(handle), .a2 = HANDLE_HIGH(handle),
362 while (ret.a0 == FFA_MEM_OP_PAUSE)
363 invoke_ffa_fn((ffa_value_t){
364 .a0 = FFA_MEM_OP_RESUME,
365 .a1 = ret.a1, .a2 = ret.a2,
368 if (ret.a0 == FFA_ERROR)
369 return ffa_to_linux_errno((int)ret.a2);
371 if (ret.a0 == FFA_MEM_FRAG_RX)
373 else if (ret.a0 == FFA_SUCCESS)
380 ffa_transmit_fragment(u32 func_id, phys_addr_t buf, u32 buf_sz, u32 frag_len,
381 u32 len, u64 *handle, bool first)
384 return ffa_mem_next_frag(*handle, frag_len);
386 return ffa_mem_first_frag(func_id, buf, buf_sz, frag_len, len, handle);
389 static u32 ffa_get_num_pages_sg(struct scatterlist *sg)
394 num_pages += sg->length / FFA_PAGE_SIZE;
395 } while ((sg = sg_next(sg)));
401 ffa_setup_and_transmit(u32 func_id, void *buffer, u32 max_fragsize,
402 struct ffa_mem_ops_args *args)
406 phys_addr_t addr = 0;
407 struct ffa_composite_mem_region *composite;
408 struct ffa_mem_region_addr_range *constituents;
409 struct ffa_mem_region_attributes *ep_mem_access;
410 struct ffa_mem_region *mem_region = buffer;
411 u32 idx, frag_len, length, buf_sz = 0, num_entries = sg_nents(args->sg);
413 mem_region->tag = args->tag;
414 mem_region->flags = args->flags;
415 mem_region->sender_id = drv_info->vm_id;
416 mem_region->attributes = FFA_MEM_NORMAL | FFA_MEM_WRITE_BACK |
417 FFA_MEM_INNER_SHAREABLE;
418 ep_mem_access = &mem_region->ep_mem_access[0];
420 for (idx = 0; idx < args->nattrs; idx++, ep_mem_access++) {
421 ep_mem_access->receiver = args->attrs[idx].receiver;
422 ep_mem_access->attrs = args->attrs[idx].attrs;
423 ep_mem_access->composite_off = COMPOSITE_OFFSET(args->nattrs);
424 ep_mem_access->flag = 0;
425 ep_mem_access->reserved = 0;
427 mem_region->handle = 0;
428 mem_region->reserved_0 = 0;
429 mem_region->reserved_1 = 0;
430 mem_region->ep_count = args->nattrs;
432 composite = buffer + COMPOSITE_OFFSET(args->nattrs);
433 composite->total_pg_cnt = ffa_get_num_pages_sg(args->sg);
434 composite->addr_range_cnt = num_entries;
435 composite->reserved = 0;
437 length = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, num_entries);
438 frag_len = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, 0);
439 if (frag_len > max_fragsize)
442 if (!args->use_txbuf) {
443 addr = virt_to_phys(buffer);
444 buf_sz = max_fragsize / FFA_PAGE_SIZE;
447 constituents = buffer + frag_len;
450 if (frag_len == max_fragsize) {
451 rc = ffa_transmit_fragment(func_id, addr, buf_sz,
453 &args->g_handle, first);
460 constituents = buffer;
463 if ((void *)constituents - buffer > max_fragsize) {
464 pr_err("Memory Region Fragment > Tx Buffer size\n");
468 constituents->address = sg_phys(args->sg);
469 constituents->pg_cnt = args->sg->length / FFA_PAGE_SIZE;
470 constituents->reserved = 0;
472 frag_len += sizeof(struct ffa_mem_region_addr_range);
473 } while ((args->sg = sg_next(args->sg)));
475 return ffa_transmit_fragment(func_id, addr, buf_sz, frag_len,
476 length, &args->g_handle, first);
479 static int ffa_memory_ops(u32 func_id, struct ffa_mem_ops_args *args)
484 if (!args->use_txbuf) {
485 buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
489 buffer = drv_info->tx_buffer;
490 mutex_lock(&drv_info->tx_lock);
493 ret = ffa_setup_and_transmit(func_id, buffer, RXTX_BUFFER_SIZE, args);
496 mutex_unlock(&drv_info->tx_lock);
498 free_pages_exact(buffer, RXTX_BUFFER_SIZE);
500 return ret < 0 ? ret : 0;
503 static int ffa_memory_reclaim(u64 g_handle, u32 flags)
507 invoke_ffa_fn((ffa_value_t){
508 .a0 = FFA_MEM_RECLAIM,
509 .a1 = HANDLE_LOW(g_handle), .a2 = HANDLE_HIGH(g_handle),
513 if (ret.a0 == FFA_ERROR)
514 return ffa_to_linux_errno((int)ret.a2);
519 static int ffa_features(u32 func_feat_id, u32 input_props,
520 u32 *if_props_1, u32 *if_props_2)
524 if (!ARM_SMCCC_IS_FAST_CALL(func_feat_id) && input_props) {
525 pr_err("%s: Invalid Parameters: %x, %x", __func__,
526 func_feat_id, input_props);
527 return ffa_to_linux_errno(FFA_RET_INVALID_PARAMETERS);
530 invoke_ffa_fn((ffa_value_t){
531 .a0 = FFA_FEATURES, .a1 = func_feat_id, .a2 = input_props,
534 if (id.a0 == FFA_ERROR)
535 return ffa_to_linux_errno((int)id.a2);
545 static void ffa_set_up_mem_ops_native_flag(void)
547 if (!ffa_features(FFA_FN_NATIVE(MEM_LEND), 0, NULL, NULL) ||
548 !ffa_features(FFA_FN_NATIVE(MEM_SHARE), 0, NULL, NULL))
549 drv_info->mem_ops_native = true;
552 static u32 ffa_api_version_get(void)
554 return drv_info->version;
557 static int ffa_partition_info_get(const char *uuid_str,
558 struct ffa_partition_info *buffer)
562 struct ffa_partition_info *pbuf;
564 if (uuid_parse(uuid_str, &uuid)) {
565 pr_err("invalid uuid (%s)\n", uuid_str);
569 count = ffa_partition_probe(&uuid, &pbuf);
573 memcpy(buffer, pbuf, sizeof(*pbuf) * count);
578 static void _ffa_mode_32bit_set(struct ffa_device *dev)
580 dev->mode_32bit = true;
583 static void ffa_mode_32bit_set(struct ffa_device *dev)
585 if (drv_info->version > FFA_VERSION_1_0)
588 _ffa_mode_32bit_set(dev);
591 static int ffa_sync_send_receive(struct ffa_device *dev,
592 struct ffa_send_direct_data *data)
594 return ffa_msg_send_direct_req(drv_info->vm_id, dev->vm_id,
595 dev->mode_32bit, data);
598 static int ffa_memory_share(struct ffa_mem_ops_args *args)
600 if (drv_info->mem_ops_native)
601 return ffa_memory_ops(FFA_FN_NATIVE(MEM_SHARE), args);
603 return ffa_memory_ops(FFA_MEM_SHARE, args);
606 static int ffa_memory_lend(struct ffa_mem_ops_args *args)
608 /* Note that upon a successful MEM_LEND request the caller
609 * must ensure that the memory region specified is not accessed
610 * until a successful MEM_RECALIM call has been made.
611 * On systems with a hypervisor present this will been enforced,
612 * however on systems without a hypervisor the responsibility
613 * falls to the calling kernel driver to prevent access.
615 if (drv_info->mem_ops_native)
616 return ffa_memory_ops(FFA_FN_NATIVE(MEM_LEND), args);
618 return ffa_memory_ops(FFA_MEM_LEND, args);
621 static const struct ffa_info_ops ffa_drv_info_ops = {
622 .api_version_get = ffa_api_version_get,
623 .partition_info_get = ffa_partition_info_get,
626 static const struct ffa_msg_ops ffa_drv_msg_ops = {
627 .mode_32bit_set = ffa_mode_32bit_set,
628 .sync_send_receive = ffa_sync_send_receive,
631 static const struct ffa_mem_ops ffa_drv_mem_ops = {
632 .memory_reclaim = ffa_memory_reclaim,
633 .memory_share = ffa_memory_share,
634 .memory_lend = ffa_memory_lend,
637 static const struct ffa_ops ffa_drv_ops = {
638 .info_ops = &ffa_drv_info_ops,
639 .msg_ops = &ffa_drv_msg_ops,
640 .mem_ops = &ffa_drv_mem_ops,
643 void ffa_device_match_uuid(struct ffa_device *ffa_dev, const uuid_t *uuid)
646 struct ffa_partition_info *pbuf, *tpbuf;
649 * FF-A v1.1 provides UUID for each partition as part of the discovery
650 * API, the discovered UUID must be populated in the device's UUID and
651 * there is no need to copy the same from the driver table.
653 if (drv_info->version > FFA_VERSION_1_0)
656 count = ffa_partition_probe(uuid, &pbuf);
660 for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++)
661 if (tpbuf->id == ffa_dev->vm_id)
662 uuid_copy(&ffa_dev->uuid, uuid);
666 static void ffa_setup_partitions(void)
670 struct ffa_device *ffa_dev;
671 struct ffa_partition_info *pbuf, *tpbuf;
673 count = ffa_partition_probe(&uuid_null, &pbuf);
675 pr_info("%s: No partitions found, error %d\n", __func__, count);
679 for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++) {
680 import_uuid(&uuid, (u8 *)tpbuf->uuid);
682 /* Note that if the UUID will be uuid_null, that will require
683 * ffa_device_match() to find the UUID of this partition id
684 * with help of ffa_device_match_uuid(). FF-A v1.1 and above
685 * provides UUID here for each partition as part of the
686 * discovery API and the same is passed.
688 ffa_dev = ffa_device_register(&uuid, tpbuf->id, &ffa_drv_ops);
690 pr_err("%s: failed to register partition ID 0x%x\n",
691 __func__, tpbuf->id);
695 if (drv_info->version > FFA_VERSION_1_0 &&
696 !(tpbuf->properties & FFA_PARTITION_AARCH64_EXEC))
697 _ffa_mode_32bit_set(ffa_dev);
702 static int __init ffa_init(void)
706 ret = ffa_transport_init(&invoke_ffa_fn);
710 ret = arm_ffa_bus_init();
714 drv_info = kzalloc(sizeof(*drv_info), GFP_KERNEL);
720 ret = ffa_version_check(&drv_info->version);
724 if (ffa_id_get(&drv_info->vm_id)) {
725 pr_err("failed to obtain VM id for self\n");
730 drv_info->rx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
731 if (!drv_info->rx_buffer) {
736 drv_info->tx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
737 if (!drv_info->tx_buffer) {
742 ret = ffa_rxtx_map(virt_to_phys(drv_info->tx_buffer),
743 virt_to_phys(drv_info->rx_buffer),
744 RXTX_BUFFER_SIZE / FFA_PAGE_SIZE);
746 pr_err("failed to register FFA RxTx buffers\n");
750 mutex_init(&drv_info->rx_lock);
751 mutex_init(&drv_info->tx_lock);
753 ffa_setup_partitions();
755 ffa_set_up_mem_ops_native_flag();
759 if (drv_info->tx_buffer)
760 free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE);
761 free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE);
768 subsys_initcall(ffa_init);
770 static void __exit ffa_exit(void)
772 ffa_rxtx_unmap(drv_info->vm_id);
773 free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE);
774 free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE);
778 module_exit(ffa_exit);
780 MODULE_ALIAS("arm-ffa");
782 MODULE_DESCRIPTION("Arm FF-A interface driver");
783 MODULE_LICENSE("GPL v2");