GNU Linux-libre 5.15.131-gnu
[releases.git] / include / linux / arm_ffa.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2021 ARM Ltd.
4  */
5
6 #ifndef _LINUX_ARM_FFA_H
7 #define _LINUX_ARM_FFA_H
8
9 #include <linux/device.h>
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/uuid.h>
13
14 /* FFA Bus/Device/Driver related */
15 struct ffa_device {
16         u32 id;
17         int vm_id;
18         bool mode_32bit;
19         uuid_t uuid;
20         struct device dev;
21 };
22
23 #define to_ffa_dev(d) container_of(d, struct ffa_device, dev)
24
25 struct ffa_device_id {
26         uuid_t uuid;
27 };
28
29 struct ffa_driver {
30         const char *name;
31         int (*probe)(struct ffa_device *sdev);
32         void (*remove)(struct ffa_device *sdev);
33         const struct ffa_device_id *id_table;
34
35         struct device_driver driver;
36 };
37
38 #define to_ffa_driver(d) container_of(d, struct ffa_driver, driver)
39
40 static inline void ffa_dev_set_drvdata(struct ffa_device *fdev, void *data)
41 {
42         fdev->dev.driver_data = data;
43 }
44
45 #if IS_REACHABLE(CONFIG_ARM_FFA_TRANSPORT)
46 struct ffa_device *ffa_device_register(const uuid_t *uuid, int vm_id);
47 void ffa_device_unregister(struct ffa_device *ffa_dev);
48 int ffa_driver_register(struct ffa_driver *driver, struct module *owner,
49                         const char *mod_name);
50 void ffa_driver_unregister(struct ffa_driver *driver);
51 bool ffa_device_is_valid(struct ffa_device *ffa_dev);
52 const struct ffa_dev_ops *ffa_dev_ops_get(struct ffa_device *dev);
53
54 #else
55 static inline
56 struct ffa_device *ffa_device_register(const uuid_t *uuid, int vm_id)
57 {
58         return NULL;
59 }
60
61 static inline void ffa_device_unregister(struct ffa_device *dev) {}
62
63 static inline int
64 ffa_driver_register(struct ffa_driver *driver, struct module *owner,
65                     const char *mod_name)
66 {
67         return -EINVAL;
68 }
69
70 static inline void ffa_driver_unregister(struct ffa_driver *driver) {}
71
72 static inline
73 bool ffa_device_is_valid(struct ffa_device *ffa_dev) { return false; }
74
75 static inline
76 const struct ffa_dev_ops *ffa_dev_ops_get(struct ffa_device *dev)
77 {
78         return NULL;
79 }
80 #endif /* CONFIG_ARM_FFA_TRANSPORT */
81
82 #define ffa_register(driver) \
83         ffa_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
84 #define ffa_unregister(driver) \
85         ffa_driver_unregister(driver)
86
87 /**
88  * module_ffa_driver() - Helper macro for registering a psa_ffa driver
89  * @__ffa_driver: ffa_driver structure
90  *
91  * Helper macro for psa_ffa drivers to set up proper module init / exit
92  * functions.  Replaces module_init() and module_exit() and keeps people from
93  * printing pointless things to the kernel log when their driver is loaded.
94  */
95 #define module_ffa_driver(__ffa_driver) \
96         module_driver(__ffa_driver, ffa_register, ffa_unregister)
97
98 /* FFA transport related */
99 struct ffa_partition_info {
100         u16 id;
101         u16 exec_ctxt;
102 /* partition supports receipt of direct requests */
103 #define FFA_PARTITION_DIRECT_RECV       BIT(0)
104 /* partition can send direct requests. */
105 #define FFA_PARTITION_DIRECT_SEND       BIT(1)
106 /* partition can send and receive indirect messages. */
107 #define FFA_PARTITION_INDIRECT_MSG      BIT(2)
108         u32 properties;
109 };
110
111 /* For use with FFA_MSG_SEND_DIRECT_{REQ,RESP} which pass data via registers */
112 struct ffa_send_direct_data {
113         unsigned long data0; /* w3/x3 */
114         unsigned long data1; /* w4/x4 */
115         unsigned long data2; /* w5/x5 */
116         unsigned long data3; /* w6/x6 */
117         unsigned long data4; /* w7/x7 */
118 };
119
120 struct ffa_mem_region_addr_range {
121         /* The base IPA of the constituent memory region, aligned to 4 kiB */
122         u64 address;
123         /* The number of 4 kiB pages in the constituent memory region. */
124         u32 pg_cnt;
125         u32 reserved;
126 };
127
128 struct ffa_composite_mem_region {
129         /*
130          * The total number of 4 kiB pages included in this memory region. This
131          * must be equal to the sum of page counts specified in each
132          * `struct ffa_mem_region_addr_range`.
133          */
134         u32 total_pg_cnt;
135         /* The number of constituents included in this memory region range */
136         u32 addr_range_cnt;
137         u64 reserved;
138         /** An array of `addr_range_cnt` memory region constituents. */
139         struct ffa_mem_region_addr_range constituents[];
140 };
141
142 struct ffa_mem_region_attributes {
143         /* The ID of the VM to which the memory is being given or shared. */
144         u16 receiver;
145         /*
146          * The permissions with which the memory region should be mapped in the
147          * receiver's page table.
148          */
149 #define FFA_MEM_EXEC            BIT(3)
150 #define FFA_MEM_NO_EXEC         BIT(2)
151 #define FFA_MEM_RW              BIT(1)
152 #define FFA_MEM_RO              BIT(0)
153         u8 attrs;
154         /*
155          * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
156          * for memory regions with multiple borrowers.
157          */
158 #define FFA_MEM_RETRIEVE_SELF_BORROWER  BIT(0)
159         u8 flag;
160         u32 composite_off;
161         /*
162          * Offset in bytes from the start of the outer `ffa_memory_region` to
163          * an `struct ffa_mem_region_addr_range`.
164          */
165         u64 reserved;
166 };
167
168 struct ffa_mem_region {
169         /* The ID of the VM/owner which originally sent the memory region */
170         u16 sender_id;
171 #define FFA_MEM_NORMAL          BIT(5)
172 #define FFA_MEM_DEVICE          BIT(4)
173
174 #define FFA_MEM_WRITE_BACK      (3 << 2)
175 #define FFA_MEM_NON_CACHEABLE   (1 << 2)
176
177 #define FFA_DEV_nGnRnE          (0 << 2)
178 #define FFA_DEV_nGnRE           (1 << 2)
179 #define FFA_DEV_nGRE            (2 << 2)
180 #define FFA_DEV_GRE             (3 << 2)
181
182 #define FFA_MEM_NON_SHAREABLE   (0)
183 #define FFA_MEM_OUTER_SHAREABLE (2)
184 #define FFA_MEM_INNER_SHAREABLE (3)
185         u8 attributes;
186         u8 reserved_0;
187 /*
188  * Clear memory region contents after unmapping it from the sender and
189  * before mapping it for any receiver.
190  */
191 #define FFA_MEM_CLEAR                   BIT(0)
192 /*
193  * Whether the hypervisor may time slice the memory sharing or retrieval
194  * operation.
195  */
196 #define FFA_TIME_SLICE_ENABLE           BIT(1)
197
198 #define FFA_MEM_RETRIEVE_TYPE_IN_RESP   (0 << 3)
199 #define FFA_MEM_RETRIEVE_TYPE_SHARE     (1 << 3)
200 #define FFA_MEM_RETRIEVE_TYPE_LEND      (2 << 3)
201 #define FFA_MEM_RETRIEVE_TYPE_DONATE    (3 << 3)
202
203 #define FFA_MEM_RETRIEVE_ADDR_ALIGN_HINT        BIT(9)
204 #define FFA_MEM_RETRIEVE_ADDR_ALIGN(x)          ((x) << 5)
205         /* Flags to control behaviour of the transaction. */
206         u32 flags;
207 #define HANDLE_LOW_MASK         GENMASK_ULL(31, 0)
208 #define HANDLE_HIGH_MASK        GENMASK_ULL(63, 32)
209 #define HANDLE_LOW(x)           ((u32)(FIELD_GET(HANDLE_LOW_MASK, (x))))
210 #define HANDLE_HIGH(x)          ((u32)(FIELD_GET(HANDLE_HIGH_MASK, (x))))
211
212 #define PACK_HANDLE(l, h)               \
213         (FIELD_PREP(HANDLE_LOW_MASK, (l)) | FIELD_PREP(HANDLE_HIGH_MASK, (h)))
214         /*
215          * A globally-unique ID assigned by the hypervisor for a region
216          * of memory being sent between VMs.
217          */
218         u64 handle;
219         /*
220          * An implementation defined value associated with the receiver and the
221          * memory region.
222          */
223         u64 tag;
224         u32 reserved_1;
225         /*
226          * The number of `ffa_mem_region_attributes` entries included in this
227          * transaction.
228          */
229         u32 ep_count;
230         /*
231          * An array of endpoint memory access descriptors.
232          * Each one specifies a memory region offset, an endpoint and the
233          * attributes with which this memory region should be mapped in that
234          * endpoint's page table.
235          */
236         struct ffa_mem_region_attributes ep_mem_access[];
237 };
238
239 #define COMPOSITE_OFFSET(x)     \
240         (offsetof(struct ffa_mem_region, ep_mem_access[x]))
241 #define CONSTITUENTS_OFFSET(x)  \
242         (offsetof(struct ffa_composite_mem_region, constituents[x]))
243 #define COMPOSITE_CONSTITUENTS_OFFSET(x, y)     \
244         (COMPOSITE_OFFSET(x) + CONSTITUENTS_OFFSET(y))
245
246 struct ffa_mem_ops_args {
247         bool use_txbuf;
248         u32 nattrs;
249         u32 flags;
250         u64 tag;
251         u64 g_handle;
252         struct scatterlist *sg;
253         struct ffa_mem_region_attributes *attrs;
254 };
255
256 struct ffa_dev_ops {
257         u32 (*api_version_get)(void);
258         int (*partition_info_get)(const char *uuid_str,
259                                   struct ffa_partition_info *buffer);
260         void (*mode_32bit_set)(struct ffa_device *dev);
261         int (*sync_send_receive)(struct ffa_device *dev,
262                                  struct ffa_send_direct_data *data);
263         int (*memory_reclaim)(u64 g_handle, u32 flags);
264         int (*memory_share)(struct ffa_device *dev,
265                             struct ffa_mem_ops_args *args);
266 };
267
268 #endif /* _LINUX_ARM_FFA_H */