Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright (C) 2021 ARM Ltd.
0004  */
0005 
0006 #ifndef _LINUX_ARM_FFA_H
0007 #define _LINUX_ARM_FFA_H
0008 
0009 #include <linux/device.h>
0010 #include <linux/module.h>
0011 #include <linux/types.h>
0012 #include <linux/uuid.h>
0013 
0014 /* FFA Bus/Device/Driver related */
0015 struct ffa_device {
0016     int vm_id;
0017     bool mode_32bit;
0018     uuid_t uuid;
0019     struct device dev;
0020 };
0021 
0022 #define to_ffa_dev(d) container_of(d, struct ffa_device, dev)
0023 
0024 struct ffa_device_id {
0025     uuid_t uuid;
0026 };
0027 
0028 struct ffa_driver {
0029     const char *name;
0030     int (*probe)(struct ffa_device *sdev);
0031     void (*remove)(struct ffa_device *sdev);
0032     const struct ffa_device_id *id_table;
0033 
0034     struct device_driver driver;
0035 };
0036 
0037 #define to_ffa_driver(d) container_of(d, struct ffa_driver, driver)
0038 
0039 static inline void ffa_dev_set_drvdata(struct ffa_device *fdev, void *data)
0040 {
0041     dev_set_drvdata(&fdev->dev, data);
0042 }
0043 
0044 static inline void *ffa_dev_get_drvdata(struct ffa_device *fdev)
0045 {
0046     return dev_get_drvdata(&fdev->dev);
0047 }
0048 
0049 #if IS_REACHABLE(CONFIG_ARM_FFA_TRANSPORT)
0050 struct ffa_device *ffa_device_register(const uuid_t *uuid, int vm_id);
0051 void ffa_device_unregister(struct ffa_device *ffa_dev);
0052 int ffa_driver_register(struct ffa_driver *driver, struct module *owner,
0053             const char *mod_name);
0054 void ffa_driver_unregister(struct ffa_driver *driver);
0055 bool ffa_device_is_valid(struct ffa_device *ffa_dev);
0056 const struct ffa_dev_ops *ffa_dev_ops_get(struct ffa_device *dev);
0057 
0058 #else
0059 static inline
0060 struct ffa_device *ffa_device_register(const uuid_t *uuid, int vm_id)
0061 {
0062     return NULL;
0063 }
0064 
0065 static inline void ffa_device_unregister(struct ffa_device *dev) {}
0066 
0067 static inline int
0068 ffa_driver_register(struct ffa_driver *driver, struct module *owner,
0069             const char *mod_name)
0070 {
0071     return -EINVAL;
0072 }
0073 
0074 static inline void ffa_driver_unregister(struct ffa_driver *driver) {}
0075 
0076 static inline
0077 bool ffa_device_is_valid(struct ffa_device *ffa_dev) { return false; }
0078 
0079 static inline
0080 const struct ffa_dev_ops *ffa_dev_ops_get(struct ffa_device *dev)
0081 {
0082     return NULL;
0083 }
0084 #endif /* CONFIG_ARM_FFA_TRANSPORT */
0085 
0086 #define ffa_register(driver) \
0087     ffa_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
0088 #define ffa_unregister(driver) \
0089     ffa_driver_unregister(driver)
0090 
0091 /**
0092  * module_ffa_driver() - Helper macro for registering a psa_ffa driver
0093  * @__ffa_driver: ffa_driver structure
0094  *
0095  * Helper macro for psa_ffa drivers to set up proper module init / exit
0096  * functions.  Replaces module_init() and module_exit() and keeps people from
0097  * printing pointless things to the kernel log when their driver is loaded.
0098  */
0099 #define module_ffa_driver(__ffa_driver) \
0100     module_driver(__ffa_driver, ffa_register, ffa_unregister)
0101 
0102 /* FFA transport related */
0103 struct ffa_partition_info {
0104     u16 id;
0105     u16 exec_ctxt;
0106 /* partition supports receipt of direct requests */
0107 #define FFA_PARTITION_DIRECT_RECV   BIT(0)
0108 /* partition can send direct requests. */
0109 #define FFA_PARTITION_DIRECT_SEND   BIT(1)
0110 /* partition can send and receive indirect messages. */
0111 #define FFA_PARTITION_INDIRECT_MSG  BIT(2)
0112     u32 properties;
0113 };
0114 
0115 /* For use with FFA_MSG_SEND_DIRECT_{REQ,RESP} which pass data via registers */
0116 struct ffa_send_direct_data {
0117     unsigned long data0; /* w3/x3 */
0118     unsigned long data1; /* w4/x4 */
0119     unsigned long data2; /* w5/x5 */
0120     unsigned long data3; /* w6/x6 */
0121     unsigned long data4; /* w7/x7 */
0122 };
0123 
0124 struct ffa_mem_region_addr_range {
0125     /* The base IPA of the constituent memory region, aligned to 4 kiB */
0126     u64 address;
0127     /* The number of 4 kiB pages in the constituent memory region. */
0128     u32 pg_cnt;
0129     u32 reserved;
0130 };
0131 
0132 struct ffa_composite_mem_region {
0133     /*
0134      * The total number of 4 kiB pages included in this memory region. This
0135      * must be equal to the sum of page counts specified in each
0136      * `struct ffa_mem_region_addr_range`.
0137      */
0138     u32 total_pg_cnt;
0139     /* The number of constituents included in this memory region range */
0140     u32 addr_range_cnt;
0141     u64 reserved;
0142     /** An array of `addr_range_cnt` memory region constituents. */
0143     struct ffa_mem_region_addr_range constituents[];
0144 };
0145 
0146 struct ffa_mem_region_attributes {
0147     /* The ID of the VM to which the memory is being given or shared. */
0148     u16 receiver;
0149     /*
0150      * The permissions with which the memory region should be mapped in the
0151      * receiver's page table.
0152      */
0153 #define FFA_MEM_EXEC        BIT(3)
0154 #define FFA_MEM_NO_EXEC     BIT(2)
0155 #define FFA_MEM_RW      BIT(1)
0156 #define FFA_MEM_RO      BIT(0)
0157     u8 attrs;
0158     /*
0159      * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
0160      * for memory regions with multiple borrowers.
0161      */
0162 #define FFA_MEM_RETRIEVE_SELF_BORROWER  BIT(0)
0163     u8 flag;
0164     u32 composite_off;
0165     /*
0166      * Offset in bytes from the start of the outer `ffa_memory_region` to
0167      * an `struct ffa_mem_region_addr_range`.
0168      */
0169     u64 reserved;
0170 };
0171 
0172 struct ffa_mem_region {
0173     /* The ID of the VM/owner which originally sent the memory region */
0174     u16 sender_id;
0175 #define FFA_MEM_NORMAL      BIT(5)
0176 #define FFA_MEM_DEVICE      BIT(4)
0177 
0178 #define FFA_MEM_WRITE_BACK  (3 << 2)
0179 #define FFA_MEM_NON_CACHEABLE   (1 << 2)
0180 
0181 #define FFA_DEV_nGnRnE      (0 << 2)
0182 #define FFA_DEV_nGnRE       (1 << 2)
0183 #define FFA_DEV_nGRE        (2 << 2)
0184 #define FFA_DEV_GRE     (3 << 2)
0185 
0186 #define FFA_MEM_NON_SHAREABLE   (0)
0187 #define FFA_MEM_OUTER_SHAREABLE (2)
0188 #define FFA_MEM_INNER_SHAREABLE (3)
0189     u8 attributes;
0190     u8 reserved_0;
0191 /*
0192  * Clear memory region contents after unmapping it from the sender and
0193  * before mapping it for any receiver.
0194  */
0195 #define FFA_MEM_CLEAR           BIT(0)
0196 /*
0197  * Whether the hypervisor may time slice the memory sharing or retrieval
0198  * operation.
0199  */
0200 #define FFA_TIME_SLICE_ENABLE       BIT(1)
0201 
0202 #define FFA_MEM_RETRIEVE_TYPE_IN_RESP   (0 << 3)
0203 #define FFA_MEM_RETRIEVE_TYPE_SHARE (1 << 3)
0204 #define FFA_MEM_RETRIEVE_TYPE_LEND  (2 << 3)
0205 #define FFA_MEM_RETRIEVE_TYPE_DONATE    (3 << 3)
0206 
0207 #define FFA_MEM_RETRIEVE_ADDR_ALIGN_HINT    BIT(9)
0208 #define FFA_MEM_RETRIEVE_ADDR_ALIGN(x)      ((x) << 5)
0209     /* Flags to control behaviour of the transaction. */
0210     u32 flags;
0211 #define HANDLE_LOW_MASK     GENMASK_ULL(31, 0)
0212 #define HANDLE_HIGH_MASK    GENMASK_ULL(63, 32)
0213 #define HANDLE_LOW(x)       ((u32)(FIELD_GET(HANDLE_LOW_MASK, (x))))
0214 #define HANDLE_HIGH(x)      ((u32)(FIELD_GET(HANDLE_HIGH_MASK, (x))))
0215 
0216 #define PACK_HANDLE(l, h)       \
0217     (FIELD_PREP(HANDLE_LOW_MASK, (l)) | FIELD_PREP(HANDLE_HIGH_MASK, (h)))
0218     /*
0219      * A globally-unique ID assigned by the hypervisor for a region
0220      * of memory being sent between VMs.
0221      */
0222     u64 handle;
0223     /*
0224      * An implementation defined value associated with the receiver and the
0225      * memory region.
0226      */
0227     u64 tag;
0228     u32 reserved_1;
0229     /*
0230      * The number of `ffa_mem_region_attributes` entries included in this
0231      * transaction.
0232      */
0233     u32 ep_count;
0234     /*
0235      * An array of endpoint memory access descriptors.
0236      * Each one specifies a memory region offset, an endpoint and the
0237      * attributes with which this memory region should be mapped in that
0238      * endpoint's page table.
0239      */
0240     struct ffa_mem_region_attributes ep_mem_access[];
0241 };
0242 
0243 #define COMPOSITE_OFFSET(x) \
0244     (offsetof(struct ffa_mem_region, ep_mem_access[x]))
0245 #define CONSTITUENTS_OFFSET(x)  \
0246     (offsetof(struct ffa_composite_mem_region, constituents[x]))
0247 #define COMPOSITE_CONSTITUENTS_OFFSET(x, y) \
0248     (COMPOSITE_OFFSET(x) + CONSTITUENTS_OFFSET(y))
0249 
0250 struct ffa_mem_ops_args {
0251     bool use_txbuf;
0252     u32 nattrs;
0253     u32 flags;
0254     u64 tag;
0255     u64 g_handle;
0256     struct scatterlist *sg;
0257     struct ffa_mem_region_attributes *attrs;
0258 };
0259 
0260 struct ffa_dev_ops {
0261     u32 (*api_version_get)(void);
0262     int (*partition_info_get)(const char *uuid_str,
0263                   struct ffa_partition_info *buffer);
0264     void (*mode_32bit_set)(struct ffa_device *dev);
0265     int (*sync_send_receive)(struct ffa_device *dev,
0266                  struct ffa_send_direct_data *data);
0267     int (*memory_reclaim)(u64 g_handle, u32 flags);
0268     int (*memory_share)(struct ffa_device *dev,
0269                 struct ffa_mem_ops_args *args);
0270     int (*memory_lend)(struct ffa_device *dev,
0271                struct ffa_mem_ops_args *args);
0272 };
0273 
0274 #endif /* _LINUX_ARM_FFA_H */