Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Arm Firmware Framework for ARMv8-A(FFA) interface driver
0004  *
0005  * The Arm FFA specification[1] describes a software architecture to
0006  * leverages the virtualization extension to isolate software images
0007  * provided by an ecosystem of vendors from each other and describes
0008  * interfaces that standardize communication between the various software
0009  * images including communication between images in the Secure world and
0010  * Normal world. Any Hypervisor could use the FFA interfaces to enable
0011  * communication between VMs it manages.
0012  *
0013  * The Hypervisor a.k.a Partition managers in FFA terminology can assign
0014  * system resources(Memory regions, Devices, CPU cycles) to the partitions
0015  * and manage isolation amongst them.
0016  *
0017  * [1] https://developer.arm.com/docs/den0077/latest
0018  *
0019  * Copyright (C) 2021 ARM Ltd.
0020  */
0021 
0022 #define DRIVER_NAME "ARM FF-A"
0023 #define pr_fmt(fmt) DRIVER_NAME ": " fmt
0024 
0025 #include <linux/arm_ffa.h>
0026 #include <linux/bitfield.h>
0027 #include <linux/device.h>
0028 #include <linux/io.h>
0029 #include <linux/kernel.h>
0030 #include <linux/module.h>
0031 #include <linux/mm.h>
0032 #include <linux/scatterlist.h>
0033 #include <linux/slab.h>
0034 #include <linux/uuid.h>
0035 
0036 #include "common.h"
0037 
0038 #define FFA_DRIVER_VERSION  FFA_VERSION_1_0
0039 
0040 #define FFA_SMC(calling_convention, func_num)               \
0041     ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, (calling_convention),   \
0042                ARM_SMCCC_OWNER_STANDARD, (func_num))
0043 
0044 #define FFA_SMC_32(func_num)    FFA_SMC(ARM_SMCCC_SMC_32, (func_num))
0045 #define FFA_SMC_64(func_num)    FFA_SMC(ARM_SMCCC_SMC_64, (func_num))
0046 
0047 #define FFA_ERROR           FFA_SMC_32(0x60)
0048 #define FFA_SUCCESS         FFA_SMC_32(0x61)
0049 #define FFA_INTERRUPT           FFA_SMC_32(0x62)
0050 #define FFA_VERSION         FFA_SMC_32(0x63)
0051 #define FFA_FEATURES            FFA_SMC_32(0x64)
0052 #define FFA_RX_RELEASE          FFA_SMC_32(0x65)
0053 #define FFA_RXTX_MAP            FFA_SMC_32(0x66)
0054 #define FFA_FN64_RXTX_MAP       FFA_SMC_64(0x66)
0055 #define FFA_RXTX_UNMAP          FFA_SMC_32(0x67)
0056 #define FFA_PARTITION_INFO_GET      FFA_SMC_32(0x68)
0057 #define FFA_ID_GET          FFA_SMC_32(0x69)
0058 #define FFA_MSG_POLL            FFA_SMC_32(0x6A)
0059 #define FFA_MSG_WAIT            FFA_SMC_32(0x6B)
0060 #define FFA_YIELD           FFA_SMC_32(0x6C)
0061 #define FFA_RUN             FFA_SMC_32(0x6D)
0062 #define FFA_MSG_SEND            FFA_SMC_32(0x6E)
0063 #define FFA_MSG_SEND_DIRECT_REQ     FFA_SMC_32(0x6F)
0064 #define FFA_FN64_MSG_SEND_DIRECT_REQ    FFA_SMC_64(0x6F)
0065 #define FFA_MSG_SEND_DIRECT_RESP    FFA_SMC_32(0x70)
0066 #define FFA_FN64_MSG_SEND_DIRECT_RESP   FFA_SMC_64(0x70)
0067 #define FFA_MEM_DONATE          FFA_SMC_32(0x71)
0068 #define FFA_FN64_MEM_DONATE     FFA_SMC_64(0x71)
0069 #define FFA_MEM_LEND            FFA_SMC_32(0x72)
0070 #define FFA_FN64_MEM_LEND       FFA_SMC_64(0x72)
0071 #define FFA_MEM_SHARE           FFA_SMC_32(0x73)
0072 #define FFA_FN64_MEM_SHARE      FFA_SMC_64(0x73)
0073 #define FFA_MEM_RETRIEVE_REQ        FFA_SMC_32(0x74)
0074 #define FFA_FN64_MEM_RETRIEVE_REQ   FFA_SMC_64(0x74)
0075 #define FFA_MEM_RETRIEVE_RESP       FFA_SMC_32(0x75)
0076 #define FFA_MEM_RELINQUISH      FFA_SMC_32(0x76)
0077 #define FFA_MEM_RECLAIM         FFA_SMC_32(0x77)
0078 #define FFA_MEM_OP_PAUSE        FFA_SMC_32(0x78)
0079 #define FFA_MEM_OP_RESUME       FFA_SMC_32(0x79)
0080 #define FFA_MEM_FRAG_RX         FFA_SMC_32(0x7A)
0081 #define FFA_MEM_FRAG_TX         FFA_SMC_32(0x7B)
0082 #define FFA_NORMAL_WORLD_RESUME     FFA_SMC_32(0x7C)
0083 
0084 /*
0085  * For some calls it is necessary to use SMC64 to pass or return 64-bit values.
0086  * For such calls FFA_FN_NATIVE(name) will choose the appropriate
0087  * (native-width) function ID.
0088  */
0089 #ifdef CONFIG_64BIT
0090 #define FFA_FN_NATIVE(name) FFA_FN64_##name
0091 #else
0092 #define FFA_FN_NATIVE(name) FFA_##name
0093 #endif
0094 
0095 /* FFA error codes. */
0096 #define FFA_RET_SUCCESS            (0)
0097 #define FFA_RET_NOT_SUPPORTED      (-1)
0098 #define FFA_RET_INVALID_PARAMETERS (-2)
0099 #define FFA_RET_NO_MEMORY          (-3)
0100 #define FFA_RET_BUSY               (-4)
0101 #define FFA_RET_INTERRUPTED        (-5)
0102 #define FFA_RET_DENIED             (-6)
0103 #define FFA_RET_RETRY              (-7)
0104 #define FFA_RET_ABORTED            (-8)
0105 
0106 #define MAJOR_VERSION_MASK  GENMASK(30, 16)
0107 #define MINOR_VERSION_MASK  GENMASK(15, 0)
0108 #define MAJOR_VERSION(x)    ((u16)(FIELD_GET(MAJOR_VERSION_MASK, (x))))
0109 #define MINOR_VERSION(x)    ((u16)(FIELD_GET(MINOR_VERSION_MASK, (x))))
0110 #define PACK_VERSION_INFO(major, minor)         \
0111     (FIELD_PREP(MAJOR_VERSION_MASK, (major)) |  \
0112      FIELD_PREP(MINOR_VERSION_MASK, (minor)))
0113 #define FFA_VERSION_1_0     PACK_VERSION_INFO(1, 0)
0114 #define FFA_MIN_VERSION     FFA_VERSION_1_0
0115 
0116 #define SENDER_ID_MASK      GENMASK(31, 16)
0117 #define RECEIVER_ID_MASK    GENMASK(15, 0)
0118 #define SENDER_ID(x)        ((u16)(FIELD_GET(SENDER_ID_MASK, (x))))
0119 #define RECEIVER_ID(x)      ((u16)(FIELD_GET(RECEIVER_ID_MASK, (x))))
0120 #define PACK_TARGET_INFO(s, r)      \
0121     (FIELD_PREP(SENDER_ID_MASK, (s)) | FIELD_PREP(RECEIVER_ID_MASK, (r)))
0122 
0123 /*
0124  * FF-A specification mentions explicitly about '4K pages'. This should
0125  * not be confused with the kernel PAGE_SIZE, which is the translation
0126  * granule kernel is configured and may be one among 4K, 16K and 64K.
0127  */
0128 #define FFA_PAGE_SIZE       SZ_4K
0129 /*
0130  * Keeping RX TX buffer size as 4K for now
0131  * 64K may be preferred to keep it min a page in 64K PAGE_SIZE config
0132  */
0133 #define RXTX_BUFFER_SIZE    SZ_4K
0134 
0135 static ffa_fn *invoke_ffa_fn;
0136 
0137 static const int ffa_linux_errmap[] = {
0138     /* better than switch case as long as return value is continuous */
0139     0,      /* FFA_RET_SUCCESS */
0140     -EOPNOTSUPP,    /* FFA_RET_NOT_SUPPORTED */
0141     -EINVAL,    /* FFA_RET_INVALID_PARAMETERS */
0142     -ENOMEM,    /* FFA_RET_NO_MEMORY */
0143     -EBUSY,     /* FFA_RET_BUSY */
0144     -EINTR,     /* FFA_RET_INTERRUPTED */
0145     -EACCES,    /* FFA_RET_DENIED */
0146     -EAGAIN,    /* FFA_RET_RETRY */
0147     -ECANCELED, /* FFA_RET_ABORTED */
0148 };
0149 
0150 static inline int ffa_to_linux_errno(int errno)
0151 {
0152     int err_idx = -errno;
0153 
0154     if (err_idx >= 0 && err_idx < ARRAY_SIZE(ffa_linux_errmap))
0155         return ffa_linux_errmap[err_idx];
0156     return -EINVAL;
0157 }
0158 
0159 struct ffa_drv_info {
0160     u32 version;
0161     u16 vm_id;
0162     struct mutex rx_lock; /* lock to protect Rx buffer */
0163     struct mutex tx_lock; /* lock to protect Tx buffer */
0164     void *rx_buffer;
0165     void *tx_buffer;
0166 };
0167 
0168 static struct ffa_drv_info *drv_info;
0169 
0170 /*
0171  * The driver must be able to support all the versions from the earliest
0172  * supported FFA_MIN_VERSION to the latest supported FFA_DRIVER_VERSION.
0173  * The specification states that if firmware supports a FFA implementation
0174  * that is incompatible with and at a greater version number than specified
0175  * by the caller(FFA_DRIVER_VERSION passed as parameter to FFA_VERSION),
0176  * it must return the NOT_SUPPORTED error code.
0177  */
0178 static u32 ffa_compatible_version_find(u32 version)
0179 {
0180     u16 major = MAJOR_VERSION(version), minor = MINOR_VERSION(version);
0181     u16 drv_major = MAJOR_VERSION(FFA_DRIVER_VERSION);
0182     u16 drv_minor = MINOR_VERSION(FFA_DRIVER_VERSION);
0183 
0184     if ((major < drv_major) || (major == drv_major && minor <= drv_minor))
0185         return version;
0186 
0187     pr_info("Firmware version higher than driver version, downgrading\n");
0188     return FFA_DRIVER_VERSION;
0189 }
0190 
0191 static int ffa_version_check(u32 *version)
0192 {
0193     ffa_value_t ver;
0194 
0195     invoke_ffa_fn((ffa_value_t){
0196               .a0 = FFA_VERSION, .a1 = FFA_DRIVER_VERSION,
0197               }, &ver);
0198 
0199     if (ver.a0 == FFA_RET_NOT_SUPPORTED) {
0200         pr_info("FFA_VERSION returned not supported\n");
0201         return -EOPNOTSUPP;
0202     }
0203 
0204     if (ver.a0 < FFA_MIN_VERSION) {
0205         pr_err("Incompatible v%d.%d! Earliest supported v%d.%d\n",
0206                MAJOR_VERSION(ver.a0), MINOR_VERSION(ver.a0),
0207                MAJOR_VERSION(FFA_MIN_VERSION),
0208                MINOR_VERSION(FFA_MIN_VERSION));
0209         return -EINVAL;
0210     }
0211 
0212     pr_info("Driver version %d.%d\n", MAJOR_VERSION(FFA_DRIVER_VERSION),
0213         MINOR_VERSION(FFA_DRIVER_VERSION));
0214     pr_info("Firmware version %d.%d found\n", MAJOR_VERSION(ver.a0),
0215         MINOR_VERSION(ver.a0));
0216     *version = ffa_compatible_version_find(ver.a0);
0217 
0218     return 0;
0219 }
0220 
0221 static int ffa_rx_release(void)
0222 {
0223     ffa_value_t ret;
0224 
0225     invoke_ffa_fn((ffa_value_t){
0226               .a0 = FFA_RX_RELEASE,
0227               }, &ret);
0228 
0229     if (ret.a0 == FFA_ERROR)
0230         return ffa_to_linux_errno((int)ret.a2);
0231 
0232     /* check for ret.a0 == FFA_RX_RELEASE ? */
0233 
0234     return 0;
0235 }
0236 
0237 static int ffa_rxtx_map(phys_addr_t tx_buf, phys_addr_t rx_buf, u32 pg_cnt)
0238 {
0239     ffa_value_t ret;
0240 
0241     invoke_ffa_fn((ffa_value_t){
0242               .a0 = FFA_FN_NATIVE(RXTX_MAP),
0243               .a1 = tx_buf, .a2 = rx_buf, .a3 = pg_cnt,
0244               }, &ret);
0245 
0246     if (ret.a0 == FFA_ERROR)
0247         return ffa_to_linux_errno((int)ret.a2);
0248 
0249     return 0;
0250 }
0251 
0252 static int ffa_rxtx_unmap(u16 vm_id)
0253 {
0254     ffa_value_t ret;
0255 
0256     invoke_ffa_fn((ffa_value_t){
0257               .a0 = FFA_RXTX_UNMAP, .a1 = PACK_TARGET_INFO(vm_id, 0),
0258               }, &ret);
0259 
0260     if (ret.a0 == FFA_ERROR)
0261         return ffa_to_linux_errno((int)ret.a2);
0262 
0263     return 0;
0264 }
0265 
0266 /* buffer must be sizeof(struct ffa_partition_info) * num_partitions */
0267 static int
0268 __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,
0269              struct ffa_partition_info *buffer, int num_partitions)
0270 {
0271     int count;
0272     ffa_value_t partition_info;
0273 
0274     mutex_lock(&drv_info->rx_lock);
0275     invoke_ffa_fn((ffa_value_t){
0276               .a0 = FFA_PARTITION_INFO_GET,
0277               .a1 = uuid0, .a2 = uuid1, .a3 = uuid2, .a4 = uuid3,
0278               }, &partition_info);
0279 
0280     if (partition_info.a0 == FFA_ERROR) {
0281         mutex_unlock(&drv_info->rx_lock);
0282         return ffa_to_linux_errno((int)partition_info.a2);
0283     }
0284 
0285     count = partition_info.a2;
0286 
0287     if (buffer && count <= num_partitions)
0288         memcpy(buffer, drv_info->rx_buffer, sizeof(*buffer) * count);
0289 
0290     ffa_rx_release();
0291 
0292     mutex_unlock(&drv_info->rx_lock);
0293 
0294     return count;
0295 }
0296 
0297 /* buffer is allocated and caller must free the same if returned count > 0 */
0298 static int
0299 ffa_partition_probe(const uuid_t *uuid, struct ffa_partition_info **buffer)
0300 {
0301     int count;
0302     u32 uuid0_4[4];
0303     struct ffa_partition_info *pbuf;
0304 
0305     export_uuid((u8 *)uuid0_4, uuid);
0306     count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1], uuid0_4[2],
0307                      uuid0_4[3], NULL, 0);
0308     if (count <= 0)
0309         return count;
0310 
0311     pbuf = kcalloc(count, sizeof(*pbuf), GFP_KERNEL);
0312     if (!pbuf)
0313         return -ENOMEM;
0314 
0315     count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1], uuid0_4[2],
0316                      uuid0_4[3], pbuf, count);
0317     if (count <= 0)
0318         kfree(pbuf);
0319     else
0320         *buffer = pbuf;
0321 
0322     return count;
0323 }
0324 
0325 #define VM_ID_MASK  GENMASK(15, 0)
0326 static int ffa_id_get(u16 *vm_id)
0327 {
0328     ffa_value_t id;
0329 
0330     invoke_ffa_fn((ffa_value_t){
0331               .a0 = FFA_ID_GET,
0332               }, &id);
0333 
0334     if (id.a0 == FFA_ERROR)
0335         return ffa_to_linux_errno((int)id.a2);
0336 
0337     *vm_id = FIELD_GET(VM_ID_MASK, (id.a2));
0338 
0339     return 0;
0340 }
0341 
0342 static int ffa_msg_send_direct_req(u16 src_id, u16 dst_id, bool mode_32bit,
0343                    struct ffa_send_direct_data *data)
0344 {
0345     u32 req_id, resp_id, src_dst_ids = PACK_TARGET_INFO(src_id, dst_id);
0346     ffa_value_t ret;
0347 
0348     if (mode_32bit) {
0349         req_id = FFA_MSG_SEND_DIRECT_REQ;
0350         resp_id = FFA_MSG_SEND_DIRECT_RESP;
0351     } else {
0352         req_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_REQ);
0353         resp_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_RESP);
0354     }
0355 
0356     invoke_ffa_fn((ffa_value_t){
0357               .a0 = req_id, .a1 = src_dst_ids, .a2 = 0,
0358               .a3 = data->data0, .a4 = data->data1, .a5 = data->data2,
0359               .a6 = data->data3, .a7 = data->data4,
0360               }, &ret);
0361 
0362     while (ret.a0 == FFA_INTERRUPT)
0363         invoke_ffa_fn((ffa_value_t){
0364                   .a0 = FFA_RUN, .a1 = ret.a1,
0365                   }, &ret);
0366 
0367     if (ret.a0 == FFA_ERROR)
0368         return ffa_to_linux_errno((int)ret.a2);
0369 
0370     if (ret.a0 == resp_id) {
0371         data->data0 = ret.a3;
0372         data->data1 = ret.a4;
0373         data->data2 = ret.a5;
0374         data->data3 = ret.a6;
0375         data->data4 = ret.a7;
0376         return 0;
0377     }
0378 
0379     return -EINVAL;
0380 }
0381 
0382 static int ffa_mem_first_frag(u32 func_id, phys_addr_t buf, u32 buf_sz,
0383                   u32 frag_len, u32 len, u64 *handle)
0384 {
0385     ffa_value_t ret;
0386 
0387     invoke_ffa_fn((ffa_value_t){
0388               .a0 = func_id, .a1 = len, .a2 = frag_len,
0389               .a3 = buf, .a4 = buf_sz,
0390               }, &ret);
0391 
0392     while (ret.a0 == FFA_MEM_OP_PAUSE)
0393         invoke_ffa_fn((ffa_value_t){
0394                   .a0 = FFA_MEM_OP_RESUME,
0395                   .a1 = ret.a1, .a2 = ret.a2,
0396                   }, &ret);
0397 
0398     if (ret.a0 == FFA_ERROR)
0399         return ffa_to_linux_errno((int)ret.a2);
0400 
0401     if (ret.a0 == FFA_SUCCESS) {
0402         if (handle)
0403             *handle = PACK_HANDLE(ret.a2, ret.a3);
0404     } else if (ret.a0 == FFA_MEM_FRAG_RX) {
0405         if (handle)
0406             *handle = PACK_HANDLE(ret.a1, ret.a2);
0407     } else {
0408         return -EOPNOTSUPP;
0409     }
0410 
0411     return frag_len;
0412 }
0413 
0414 static int ffa_mem_next_frag(u64 handle, u32 frag_len)
0415 {
0416     ffa_value_t ret;
0417 
0418     invoke_ffa_fn((ffa_value_t){
0419               .a0 = FFA_MEM_FRAG_TX,
0420               .a1 = HANDLE_LOW(handle), .a2 = HANDLE_HIGH(handle),
0421               .a3 = frag_len,
0422               }, &ret);
0423 
0424     while (ret.a0 == FFA_MEM_OP_PAUSE)
0425         invoke_ffa_fn((ffa_value_t){
0426                   .a0 = FFA_MEM_OP_RESUME,
0427                   .a1 = ret.a1, .a2 = ret.a2,
0428                   }, &ret);
0429 
0430     if (ret.a0 == FFA_ERROR)
0431         return ffa_to_linux_errno((int)ret.a2);
0432 
0433     if (ret.a0 == FFA_MEM_FRAG_RX)
0434         return ret.a3;
0435     else if (ret.a0 == FFA_SUCCESS)
0436         return 0;
0437 
0438     return -EOPNOTSUPP;
0439 }
0440 
0441 static int
0442 ffa_transmit_fragment(u32 func_id, phys_addr_t buf, u32 buf_sz, u32 frag_len,
0443               u32 len, u64 *handle, bool first)
0444 {
0445     if (!first)
0446         return ffa_mem_next_frag(*handle, frag_len);
0447 
0448     return ffa_mem_first_frag(func_id, buf, buf_sz, frag_len, len, handle);
0449 }
0450 
0451 static u32 ffa_get_num_pages_sg(struct scatterlist *sg)
0452 {
0453     u32 num_pages = 0;
0454 
0455     do {
0456         num_pages += sg->length / FFA_PAGE_SIZE;
0457     } while ((sg = sg_next(sg)));
0458 
0459     return num_pages;
0460 }
0461 
0462 static int
0463 ffa_setup_and_transmit(u32 func_id, void *buffer, u32 max_fragsize,
0464                struct ffa_mem_ops_args *args)
0465 {
0466     int rc = 0;
0467     bool first = true;
0468     phys_addr_t addr = 0;
0469     struct ffa_composite_mem_region *composite;
0470     struct ffa_mem_region_addr_range *constituents;
0471     struct ffa_mem_region_attributes *ep_mem_access;
0472     struct ffa_mem_region *mem_region = buffer;
0473     u32 idx, frag_len, length, buf_sz = 0, num_entries = sg_nents(args->sg);
0474 
0475     mem_region->tag = args->tag;
0476     mem_region->flags = args->flags;
0477     mem_region->sender_id = drv_info->vm_id;
0478     mem_region->attributes = FFA_MEM_NORMAL | FFA_MEM_WRITE_BACK |
0479                  FFA_MEM_INNER_SHAREABLE;
0480     ep_mem_access = &mem_region->ep_mem_access[0];
0481 
0482     for (idx = 0; idx < args->nattrs; idx++, ep_mem_access++) {
0483         ep_mem_access->receiver = args->attrs[idx].receiver;
0484         ep_mem_access->attrs = args->attrs[idx].attrs;
0485         ep_mem_access->composite_off = COMPOSITE_OFFSET(args->nattrs);
0486     }
0487     mem_region->ep_count = args->nattrs;
0488 
0489     composite = buffer + COMPOSITE_OFFSET(args->nattrs);
0490     composite->total_pg_cnt = ffa_get_num_pages_sg(args->sg);
0491     composite->addr_range_cnt = num_entries;
0492 
0493     length = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, num_entries);
0494     frag_len = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, 0);
0495     if (frag_len > max_fragsize)
0496         return -ENXIO;
0497 
0498     if (!args->use_txbuf) {
0499         addr = virt_to_phys(buffer);
0500         buf_sz = max_fragsize / FFA_PAGE_SIZE;
0501     }
0502 
0503     constituents = buffer + frag_len;
0504     idx = 0;
0505     do {
0506         if (frag_len == max_fragsize) {
0507             rc = ffa_transmit_fragment(func_id, addr, buf_sz,
0508                            frag_len, length,
0509                            &args->g_handle, first);
0510             if (rc < 0)
0511                 return -ENXIO;
0512 
0513             first = false;
0514             idx = 0;
0515             frag_len = 0;
0516             constituents = buffer;
0517         }
0518 
0519         if ((void *)constituents - buffer > max_fragsize) {
0520             pr_err("Memory Region Fragment > Tx Buffer size\n");
0521             return -EFAULT;
0522         }
0523 
0524         constituents->address = sg_phys(args->sg);
0525         constituents->pg_cnt = args->sg->length / FFA_PAGE_SIZE;
0526         constituents++;
0527         frag_len += sizeof(struct ffa_mem_region_addr_range);
0528     } while ((args->sg = sg_next(args->sg)));
0529 
0530     return ffa_transmit_fragment(func_id, addr, buf_sz, frag_len,
0531                      length, &args->g_handle, first);
0532 }
0533 
0534 static int ffa_memory_ops(u32 func_id, struct ffa_mem_ops_args *args)
0535 {
0536     int ret;
0537     void *buffer;
0538 
0539     if (!args->use_txbuf) {
0540         buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
0541         if (!buffer)
0542             return -ENOMEM;
0543     } else {
0544         buffer = drv_info->tx_buffer;
0545         mutex_lock(&drv_info->tx_lock);
0546     }
0547 
0548     ret = ffa_setup_and_transmit(func_id, buffer, RXTX_BUFFER_SIZE, args);
0549 
0550     if (args->use_txbuf)
0551         mutex_unlock(&drv_info->tx_lock);
0552     else
0553         free_pages_exact(buffer, RXTX_BUFFER_SIZE);
0554 
0555     return ret < 0 ? ret : 0;
0556 }
0557 
0558 static int ffa_memory_reclaim(u64 g_handle, u32 flags)
0559 {
0560     ffa_value_t ret;
0561 
0562     invoke_ffa_fn((ffa_value_t){
0563               .a0 = FFA_MEM_RECLAIM,
0564               .a1 = HANDLE_LOW(g_handle), .a2 = HANDLE_HIGH(g_handle),
0565               .a3 = flags,
0566               }, &ret);
0567 
0568     if (ret.a0 == FFA_ERROR)
0569         return ffa_to_linux_errno((int)ret.a2);
0570 
0571     return 0;
0572 }
0573 
0574 static u32 ffa_api_version_get(void)
0575 {
0576     return drv_info->version;
0577 }
0578 
0579 static int ffa_partition_info_get(const char *uuid_str,
0580                   struct ffa_partition_info *buffer)
0581 {
0582     int count;
0583     uuid_t uuid;
0584     struct ffa_partition_info *pbuf;
0585 
0586     if (uuid_parse(uuid_str, &uuid)) {
0587         pr_err("invalid uuid (%s)\n", uuid_str);
0588         return -ENODEV;
0589     }
0590 
0591     count = ffa_partition_probe(&uuid, &pbuf);
0592     if (count <= 0)
0593         return -ENOENT;
0594 
0595     memcpy(buffer, pbuf, sizeof(*pbuf) * count);
0596     kfree(pbuf);
0597     return 0;
0598 }
0599 
0600 static void ffa_mode_32bit_set(struct ffa_device *dev)
0601 {
0602     dev->mode_32bit = true;
0603 }
0604 
0605 static int ffa_sync_send_receive(struct ffa_device *dev,
0606                  struct ffa_send_direct_data *data)
0607 {
0608     return ffa_msg_send_direct_req(drv_info->vm_id, dev->vm_id,
0609                        dev->mode_32bit, data);
0610 }
0611 
0612 static int
0613 ffa_memory_share(struct ffa_device *dev, struct ffa_mem_ops_args *args)
0614 {
0615     if (dev->mode_32bit)
0616         return ffa_memory_ops(FFA_MEM_SHARE, args);
0617 
0618     return ffa_memory_ops(FFA_FN_NATIVE(MEM_SHARE), args);
0619 }
0620 
0621 static int
0622 ffa_memory_lend(struct ffa_device *dev, struct ffa_mem_ops_args *args)
0623 {
0624     /* Note that upon a successful MEM_LEND request the caller
0625      * must ensure that the memory region specified is not accessed
0626      * until a successful MEM_RECALIM call has been made.
0627      * On systems with a hypervisor present this will been enforced,
0628      * however on systems without a hypervisor the responsibility
0629      * falls to the calling kernel driver to prevent access.
0630      */
0631     if (dev->mode_32bit)
0632         return ffa_memory_ops(FFA_MEM_LEND, args);
0633 
0634     return ffa_memory_ops(FFA_FN_NATIVE(MEM_LEND), args);
0635 }
0636 
0637 static const struct ffa_dev_ops ffa_ops = {
0638     .api_version_get = ffa_api_version_get,
0639     .partition_info_get = ffa_partition_info_get,
0640     .mode_32bit_set = ffa_mode_32bit_set,
0641     .sync_send_receive = ffa_sync_send_receive,
0642     .memory_reclaim = ffa_memory_reclaim,
0643     .memory_share = ffa_memory_share,
0644     .memory_lend = ffa_memory_lend,
0645 };
0646 
0647 const struct ffa_dev_ops *ffa_dev_ops_get(struct ffa_device *dev)
0648 {
0649     if (ffa_device_is_valid(dev))
0650         return &ffa_ops;
0651 
0652     return NULL;
0653 }
0654 EXPORT_SYMBOL_GPL(ffa_dev_ops_get);
0655 
0656 void ffa_device_match_uuid(struct ffa_device *ffa_dev, const uuid_t *uuid)
0657 {
0658     int count, idx;
0659     struct ffa_partition_info *pbuf, *tpbuf;
0660 
0661     count = ffa_partition_probe(uuid, &pbuf);
0662     if (count <= 0)
0663         return;
0664 
0665     for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++)
0666         if (tpbuf->id == ffa_dev->vm_id)
0667             uuid_copy(&ffa_dev->uuid, uuid);
0668     kfree(pbuf);
0669 }
0670 
0671 static void ffa_setup_partitions(void)
0672 {
0673     int count, idx;
0674     struct ffa_device *ffa_dev;
0675     struct ffa_partition_info *pbuf, *tpbuf;
0676 
0677     count = ffa_partition_probe(&uuid_null, &pbuf);
0678     if (count <= 0) {
0679         pr_info("%s: No partitions found, error %d\n", __func__, count);
0680         return;
0681     }
0682 
0683     for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++) {
0684         /* Note that the &uuid_null parameter will require
0685          * ffa_device_match() to find the UUID of this partition id
0686          * with help of ffa_device_match_uuid(). Once the FF-A spec
0687          * is updated to provide correct UUID here for each partition
0688          * as part of the discovery API, we need to pass the
0689          * discovered UUID here instead.
0690          */
0691         ffa_dev = ffa_device_register(&uuid_null, tpbuf->id);
0692         if (!ffa_dev) {
0693             pr_err("%s: failed to register partition ID 0x%x\n",
0694                    __func__, tpbuf->id);
0695             continue;
0696         }
0697     }
0698     kfree(pbuf);
0699 }
0700 
0701 static int __init ffa_init(void)
0702 {
0703     int ret;
0704 
0705     ret = ffa_transport_init(&invoke_ffa_fn);
0706     if (ret)
0707         return ret;
0708 
0709     ret = arm_ffa_bus_init();
0710     if (ret)
0711         return ret;
0712 
0713     drv_info = kzalloc(sizeof(*drv_info), GFP_KERNEL);
0714     if (!drv_info) {
0715         ret = -ENOMEM;
0716         goto ffa_bus_exit;
0717     }
0718 
0719     ret = ffa_version_check(&drv_info->version);
0720     if (ret)
0721         goto free_drv_info;
0722 
0723     if (ffa_id_get(&drv_info->vm_id)) {
0724         pr_err("failed to obtain VM id for self\n");
0725         ret = -ENODEV;
0726         goto free_drv_info;
0727     }
0728 
0729     drv_info->rx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
0730     if (!drv_info->rx_buffer) {
0731         ret = -ENOMEM;
0732         goto free_pages;
0733     }
0734 
0735     drv_info->tx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
0736     if (!drv_info->tx_buffer) {
0737         ret = -ENOMEM;
0738         goto free_pages;
0739     }
0740 
0741     ret = ffa_rxtx_map(virt_to_phys(drv_info->tx_buffer),
0742                virt_to_phys(drv_info->rx_buffer),
0743                RXTX_BUFFER_SIZE / FFA_PAGE_SIZE);
0744     if (ret) {
0745         pr_err("failed to register FFA RxTx buffers\n");
0746         goto free_pages;
0747     }
0748 
0749     mutex_init(&drv_info->rx_lock);
0750     mutex_init(&drv_info->tx_lock);
0751 
0752     ffa_setup_partitions();
0753 
0754     return 0;
0755 free_pages:
0756     if (drv_info->tx_buffer)
0757         free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE);
0758     free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE);
0759 free_drv_info:
0760     kfree(drv_info);
0761 ffa_bus_exit:
0762     arm_ffa_bus_exit();
0763     return ret;
0764 }
0765 subsys_initcall(ffa_init);
0766 
0767 static void __exit ffa_exit(void)
0768 {
0769     ffa_rxtx_unmap(drv_info->vm_id);
0770     free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE);
0771     free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE);
0772     kfree(drv_info);
0773     arm_ffa_bus_exit();
0774 }
0775 module_exit(ffa_exit);
0776 
0777 MODULE_ALIAS("arm-ffa");
0778 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
0779 MODULE_DESCRIPTION("Arm FF-A interface driver");
0780 MODULE_LICENSE("GPL v2");