xref: /linux-6.15/include/linux/arm_ffa.h (revision fe2ddb6b)
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 #define FFA_SMC(calling_convention, func_num)				\
15 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, (calling_convention),	\
16 			   ARM_SMCCC_OWNER_STANDARD, (func_num))
17 
18 #define FFA_SMC_32(func_num)	FFA_SMC(ARM_SMCCC_SMC_32, (func_num))
19 #define FFA_SMC_64(func_num)	FFA_SMC(ARM_SMCCC_SMC_64, (func_num))
20 
21 #define FFA_ERROR			FFA_SMC_32(0x60)
22 #define FFA_SUCCESS			FFA_SMC_32(0x61)
23 #define FFA_FN64_SUCCESS		FFA_SMC_64(0x61)
24 #define FFA_INTERRUPT			FFA_SMC_32(0x62)
25 #define FFA_VERSION			FFA_SMC_32(0x63)
26 #define FFA_FEATURES			FFA_SMC_32(0x64)
27 #define FFA_RX_RELEASE			FFA_SMC_32(0x65)
28 #define FFA_RXTX_MAP			FFA_SMC_32(0x66)
29 #define FFA_FN64_RXTX_MAP		FFA_SMC_64(0x66)
30 #define FFA_RXTX_UNMAP			FFA_SMC_32(0x67)
31 #define FFA_PARTITION_INFO_GET		FFA_SMC_32(0x68)
32 #define FFA_ID_GET			FFA_SMC_32(0x69)
33 #define FFA_MSG_POLL			FFA_SMC_32(0x6A)
34 #define FFA_MSG_WAIT			FFA_SMC_32(0x6B)
35 #define FFA_YIELD			FFA_SMC_32(0x6C)
36 #define FFA_RUN				FFA_SMC_32(0x6D)
37 #define FFA_MSG_SEND			FFA_SMC_32(0x6E)
38 #define FFA_MSG_SEND_DIRECT_REQ		FFA_SMC_32(0x6F)
39 #define FFA_FN64_MSG_SEND_DIRECT_REQ	FFA_SMC_64(0x6F)
40 #define FFA_MSG_SEND_DIRECT_RESP	FFA_SMC_32(0x70)
41 #define FFA_FN64_MSG_SEND_DIRECT_RESP	FFA_SMC_64(0x70)
42 #define FFA_MEM_DONATE			FFA_SMC_32(0x71)
43 #define FFA_FN64_MEM_DONATE		FFA_SMC_64(0x71)
44 #define FFA_MEM_LEND			FFA_SMC_32(0x72)
45 #define FFA_FN64_MEM_LEND		FFA_SMC_64(0x72)
46 #define FFA_MEM_SHARE			FFA_SMC_32(0x73)
47 #define FFA_FN64_MEM_SHARE		FFA_SMC_64(0x73)
48 #define FFA_MEM_RETRIEVE_REQ		FFA_SMC_32(0x74)
49 #define FFA_FN64_MEM_RETRIEVE_REQ	FFA_SMC_64(0x74)
50 #define FFA_MEM_RETRIEVE_RESP		FFA_SMC_32(0x75)
51 #define FFA_MEM_RELINQUISH		FFA_SMC_32(0x76)
52 #define FFA_MEM_RECLAIM			FFA_SMC_32(0x77)
53 #define FFA_MEM_OP_PAUSE		FFA_SMC_32(0x78)
54 #define FFA_MEM_OP_RESUME		FFA_SMC_32(0x79)
55 #define FFA_MEM_FRAG_RX			FFA_SMC_32(0x7A)
56 #define FFA_MEM_FRAG_TX			FFA_SMC_32(0x7B)
57 #define FFA_NORMAL_WORLD_RESUME		FFA_SMC_32(0x7C)
58 #define FFA_NOTIFICATION_BITMAP_CREATE	FFA_SMC_32(0x7D)
59 #define FFA_NOTIFICATION_BITMAP_DESTROY FFA_SMC_32(0x7E)
60 #define FFA_NOTIFICATION_BIND		FFA_SMC_32(0x7F)
61 #define FFA_NOTIFICATION_UNBIND		FFA_SMC_32(0x80)
62 #define FFA_NOTIFICATION_SET		FFA_SMC_32(0x81)
63 #define FFA_NOTIFICATION_GET		FFA_SMC_32(0x82)
64 #define FFA_NOTIFICATION_INFO_GET	FFA_SMC_32(0x83)
65 #define FFA_FN64_NOTIFICATION_INFO_GET	FFA_SMC_64(0x83)
66 #define FFA_RX_ACQUIRE			FFA_SMC_32(0x84)
67 #define FFA_SPM_ID_GET			FFA_SMC_32(0x85)
68 #define FFA_MSG_SEND2			FFA_SMC_32(0x86)
69 #define FFA_SECONDARY_EP_REGISTER	FFA_SMC_32(0x87)
70 #define FFA_FN64_SECONDARY_EP_REGISTER	FFA_SMC_64(0x87)
71 #define FFA_MEM_PERM_GET		FFA_SMC_32(0x88)
72 #define FFA_FN64_MEM_PERM_GET		FFA_SMC_64(0x88)
73 #define FFA_MEM_PERM_SET		FFA_SMC_32(0x89)
74 #define FFA_FN64_MEM_PERM_SET		FFA_SMC_64(0x89)
75 
76 /*
77  * For some calls it is necessary to use SMC64 to pass or return 64-bit values.
78  * For such calls FFA_FN_NATIVE(name) will choose the appropriate
79  * (native-width) function ID.
80  */
81 #ifdef CONFIG_64BIT
82 #define FFA_FN_NATIVE(name)	FFA_FN64_##name
83 #else
84 #define FFA_FN_NATIVE(name)	FFA_##name
85 #endif
86 
87 /* FFA error codes. */
88 #define FFA_RET_SUCCESS            (0)
89 #define FFA_RET_NOT_SUPPORTED      (-1)
90 #define FFA_RET_INVALID_PARAMETERS (-2)
91 #define FFA_RET_NO_MEMORY          (-3)
92 #define FFA_RET_BUSY               (-4)
93 #define FFA_RET_INTERRUPTED        (-5)
94 #define FFA_RET_DENIED             (-6)
95 #define FFA_RET_RETRY              (-7)
96 #define FFA_RET_ABORTED            (-8)
97 #define FFA_RET_NO_DATA            (-9)
98 
99 /* FFA version encoding */
100 #define FFA_MAJOR_VERSION_MASK	GENMASK(30, 16)
101 #define FFA_MINOR_VERSION_MASK	GENMASK(15, 0)
102 #define FFA_MAJOR_VERSION(x)	((u16)(FIELD_GET(FFA_MAJOR_VERSION_MASK, (x))))
103 #define FFA_MINOR_VERSION(x)	((u16)(FIELD_GET(FFA_MINOR_VERSION_MASK, (x))))
104 #define FFA_PACK_VERSION_INFO(major, minor)			\
105 	(FIELD_PREP(FFA_MAJOR_VERSION_MASK, (major)) |		\
106 	 FIELD_PREP(FFA_MINOR_VERSION_MASK, (minor)))
107 #define FFA_VERSION_1_0		FFA_PACK_VERSION_INFO(1, 0)
108 #define FFA_VERSION_1_1		FFA_PACK_VERSION_INFO(1, 1)
109 
110 /**
111  * FF-A specification mentions explicitly about '4K pages'. This should
112  * not be confused with the kernel PAGE_SIZE, which is the translation
113  * granule kernel is configured and may be one among 4K, 16K and 64K.
114  */
115 #define FFA_PAGE_SIZE		SZ_4K
116 
117 /*
118  * Minimum buffer size/alignment encodings returned by an FFA_FEATURES
119  * query for FFA_RXTX_MAP.
120  */
121 #define FFA_FEAT_RXTX_MIN_SZ_4K		0
122 #define FFA_FEAT_RXTX_MIN_SZ_64K	1
123 #define FFA_FEAT_RXTX_MIN_SZ_16K	2
124 
125 /* FFA Bus/Device/Driver related */
126 struct ffa_device {
127 	u32 id;
128 	int vm_id;
129 	bool mode_32bit;
130 	uuid_t uuid;
131 	struct device dev;
132 	const struct ffa_ops *ops;
133 };
134 
135 #define to_ffa_dev(d) container_of(d, struct ffa_device, dev)
136 
137 struct ffa_device_id {
138 	uuid_t uuid;
139 };
140 
141 struct ffa_driver {
142 	const char *name;
143 	int (*probe)(struct ffa_device *sdev);
144 	void (*remove)(struct ffa_device *sdev);
145 	const struct ffa_device_id *id_table;
146 
147 	struct device_driver driver;
148 };
149 
150 #define to_ffa_driver(d) container_of(d, struct ffa_driver, driver)
151 
152 static inline void ffa_dev_set_drvdata(struct ffa_device *fdev, void *data)
153 {
154 	dev_set_drvdata(&fdev->dev, data);
155 }
156 
157 static inline void *ffa_dev_get_drvdata(struct ffa_device *fdev)
158 {
159 	return dev_get_drvdata(&fdev->dev);
160 }
161 
162 #if IS_REACHABLE(CONFIG_ARM_FFA_TRANSPORT)
163 struct ffa_device *ffa_device_register(const uuid_t *uuid, int vm_id,
164 				       const struct ffa_ops *ops);
165 void ffa_device_unregister(struct ffa_device *ffa_dev);
166 int ffa_driver_register(struct ffa_driver *driver, struct module *owner,
167 			const char *mod_name);
168 void ffa_driver_unregister(struct ffa_driver *driver);
169 bool ffa_device_is_valid(struct ffa_device *ffa_dev);
170 
171 #else
172 static inline
173 struct ffa_device *ffa_device_register(const uuid_t *uuid, int vm_id,
174 				       const struct ffa_ops *ops)
175 {
176 	return NULL;
177 }
178 
179 static inline void ffa_device_unregister(struct ffa_device *dev) {}
180 
181 static inline int
182 ffa_driver_register(struct ffa_driver *driver, struct module *owner,
183 		    const char *mod_name)
184 {
185 	return -EINVAL;
186 }
187 
188 static inline void ffa_driver_unregister(struct ffa_driver *driver) {}
189 
190 static inline
191 bool ffa_device_is_valid(struct ffa_device *ffa_dev) { return false; }
192 
193 #endif /* CONFIG_ARM_FFA_TRANSPORT */
194 
195 #define ffa_register(driver) \
196 	ffa_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
197 #define ffa_unregister(driver) \
198 	ffa_driver_unregister(driver)
199 
200 /**
201  * module_ffa_driver() - Helper macro for registering a psa_ffa driver
202  * @__ffa_driver: ffa_driver structure
203  *
204  * Helper macro for psa_ffa drivers to set up proper module init / exit
205  * functions.  Replaces module_init() and module_exit() and keeps people from
206  * printing pointless things to the kernel log when their driver is loaded.
207  */
208 #define module_ffa_driver(__ffa_driver)	\
209 	module_driver(__ffa_driver, ffa_register, ffa_unregister)
210 
211 /* FFA transport related */
212 struct ffa_partition_info {
213 	u16 id;
214 	u16 exec_ctxt;
215 /* partition supports receipt of direct requests */
216 #define FFA_PARTITION_DIRECT_RECV	BIT(0)
217 /* partition can send direct requests. */
218 #define FFA_PARTITION_DIRECT_SEND	BIT(1)
219 /* partition can send and receive indirect messages. */
220 #define FFA_PARTITION_INDIRECT_MSG	BIT(2)
221 /* partition runs in the AArch64 execution state. */
222 #define FFA_PARTITION_AARCH64_EXEC	BIT(8)
223 	u32 properties;
224 	u32 uuid[4];
225 };
226 
227 /* For use with FFA_MSG_SEND_DIRECT_{REQ,RESP} which pass data via registers */
228 struct ffa_send_direct_data {
229 	unsigned long data0; /* w3/x3 */
230 	unsigned long data1; /* w4/x4 */
231 	unsigned long data2; /* w5/x5 */
232 	unsigned long data3; /* w6/x6 */
233 	unsigned long data4; /* w7/x7 */
234 };
235 
236 struct ffa_mem_region_addr_range {
237 	/* The base IPA of the constituent memory region, aligned to 4 kiB */
238 	u64 address;
239 	/* The number of 4 kiB pages in the constituent memory region. */
240 	u32 pg_cnt;
241 	u32 reserved;
242 };
243 
244 struct ffa_composite_mem_region {
245 	/*
246 	 * The total number of 4 kiB pages included in this memory region. This
247 	 * must be equal to the sum of page counts specified in each
248 	 * `struct ffa_mem_region_addr_range`.
249 	 */
250 	u32 total_pg_cnt;
251 	/* The number of constituents included in this memory region range */
252 	u32 addr_range_cnt;
253 	u64 reserved;
254 	/** An array of `addr_range_cnt` memory region constituents. */
255 	struct ffa_mem_region_addr_range constituents[];
256 };
257 
258 struct ffa_mem_region_attributes {
259 	/* The ID of the VM to which the memory is being given or shared. */
260 	u16 receiver;
261 	/*
262 	 * The permissions with which the memory region should be mapped in the
263 	 * receiver's page table.
264 	 */
265 #define FFA_MEM_EXEC		BIT(3)
266 #define FFA_MEM_NO_EXEC		BIT(2)
267 #define FFA_MEM_RW		BIT(1)
268 #define FFA_MEM_RO		BIT(0)
269 	u8 attrs;
270 	/*
271 	 * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
272 	 * for memory regions with multiple borrowers.
273 	 */
274 #define FFA_MEM_RETRIEVE_SELF_BORROWER	BIT(0)
275 	u8 flag;
276 	/*
277 	 * Offset in bytes from the start of the outer `ffa_memory_region` to
278 	 * an `struct ffa_mem_region_addr_range`.
279 	 */
280 	u32 composite_off;
281 	u64 reserved;
282 };
283 
284 struct ffa_mem_region {
285 	/* The ID of the VM/owner which originally sent the memory region */
286 	u16 sender_id;
287 #define FFA_MEM_NORMAL		BIT(5)
288 #define FFA_MEM_DEVICE		BIT(4)
289 
290 #define FFA_MEM_WRITE_BACK	(3 << 2)
291 #define FFA_MEM_NON_CACHEABLE	(1 << 2)
292 
293 #define FFA_DEV_nGnRnE		(0 << 2)
294 #define FFA_DEV_nGnRE		(1 << 2)
295 #define FFA_DEV_nGRE		(2 << 2)
296 #define FFA_DEV_GRE		(3 << 2)
297 
298 #define FFA_MEM_NON_SHAREABLE	(0)
299 #define FFA_MEM_OUTER_SHAREABLE	(2)
300 #define FFA_MEM_INNER_SHAREABLE	(3)
301 	u8 attributes;
302 	u8 reserved_0;
303 /*
304  * Clear memory region contents after unmapping it from the sender and
305  * before mapping it for any receiver.
306  */
307 #define FFA_MEM_CLEAR			BIT(0)
308 /*
309  * Whether the hypervisor may time slice the memory sharing or retrieval
310  * operation.
311  */
312 #define FFA_TIME_SLICE_ENABLE		BIT(1)
313 
314 #define FFA_MEM_RETRIEVE_TYPE_IN_RESP	(0 << 3)
315 #define FFA_MEM_RETRIEVE_TYPE_SHARE	(1 << 3)
316 #define FFA_MEM_RETRIEVE_TYPE_LEND	(2 << 3)
317 #define FFA_MEM_RETRIEVE_TYPE_DONATE	(3 << 3)
318 
319 #define FFA_MEM_RETRIEVE_ADDR_ALIGN_HINT	BIT(9)
320 #define FFA_MEM_RETRIEVE_ADDR_ALIGN(x)		((x) << 5)
321 	/* Flags to control behaviour of the transaction. */
322 	u32 flags;
323 #define HANDLE_LOW_MASK		GENMASK_ULL(31, 0)
324 #define HANDLE_HIGH_MASK	GENMASK_ULL(63, 32)
325 #define HANDLE_LOW(x)		((u32)(FIELD_GET(HANDLE_LOW_MASK, (x))))
326 #define	HANDLE_HIGH(x)		((u32)(FIELD_GET(HANDLE_HIGH_MASK, (x))))
327 
328 #define PACK_HANDLE(l, h)		\
329 	(FIELD_PREP(HANDLE_LOW_MASK, (l)) | FIELD_PREP(HANDLE_HIGH_MASK, (h)))
330 	/*
331 	 * A globally-unique ID assigned by the hypervisor for a region
332 	 * of memory being sent between VMs.
333 	 */
334 	u64 handle;
335 	/*
336 	 * An implementation defined value associated with the receiver and the
337 	 * memory region.
338 	 */
339 	u64 tag;
340 	u32 reserved_1;
341 	/*
342 	 * The number of `ffa_mem_region_attributes` entries included in this
343 	 * transaction.
344 	 */
345 	u32 ep_count;
346 	/*
347 	 * An array of endpoint memory access descriptors.
348 	 * Each one specifies a memory region offset, an endpoint and the
349 	 * attributes with which this memory region should be mapped in that
350 	 * endpoint's page table.
351 	 */
352 	struct ffa_mem_region_attributes ep_mem_access[];
353 };
354 
355 #define	COMPOSITE_OFFSET(x)	\
356 	(offsetof(struct ffa_mem_region, ep_mem_access[x]))
357 #define CONSTITUENTS_OFFSET(x)	\
358 	(offsetof(struct ffa_composite_mem_region, constituents[x]))
359 #define COMPOSITE_CONSTITUENTS_OFFSET(x, y)	\
360 	(COMPOSITE_OFFSET(x) + CONSTITUENTS_OFFSET(y))
361 
362 struct ffa_mem_ops_args {
363 	bool use_txbuf;
364 	u32 nattrs;
365 	u32 flags;
366 	u64 tag;
367 	u64 g_handle;
368 	struct scatterlist *sg;
369 	struct ffa_mem_region_attributes *attrs;
370 };
371 
372 struct ffa_info_ops {
373 	u32 (*api_version_get)(void);
374 	int (*partition_info_get)(const char *uuid_str,
375 				  struct ffa_partition_info *buffer);
376 };
377 
378 struct ffa_msg_ops {
379 	void (*mode_32bit_set)(struct ffa_device *dev);
380 	int (*sync_send_receive)(struct ffa_device *dev,
381 				 struct ffa_send_direct_data *data);
382 };
383 
384 struct ffa_mem_ops {
385 	int (*memory_reclaim)(u64 g_handle, u32 flags);
386 	int (*memory_share)(struct ffa_mem_ops_args *args);
387 	int (*memory_lend)(struct ffa_mem_ops_args *args);
388 };
389 
390 struct ffa_cpu_ops {
391 	int (*run)(struct ffa_device *dev, u16 vcpu);
392 };
393 
394 struct ffa_ops {
395 	const struct ffa_info_ops *info_ops;
396 	const struct ffa_msg_ops *msg_ops;
397 	const struct ffa_mem_ops *mem_ops;
398 	const struct ffa_cpu_ops *cpu_ops;
399 };
400 
401 #endif /* _LINUX_ARM_FFA_H */
402