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