1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Arm Firmware Framework for ARMv8-A(FFA) interface driver
4  *
5  * The Arm FFA specification[1] describes a software architecture to
6  * leverages the virtualization extension to isolate software images
7  * provided by an ecosystem of vendors from each other and describes
8  * interfaces that standardize communication between the various software
9  * images including communication between images in the Secure world and
10  * Normal world. Any Hypervisor could use the FFA interfaces to enable
11  * communication between VMs it manages.
12  *
13  * The Hypervisor a.k.a Partition managers in FFA terminology can assign
14  * system resources(Memory regions, Devices, CPU cycles) to the partitions
15  * and manage isolation amongst them.
16  *
17  * [1] https://developer.arm.com/docs/den0077/latest
18  *
19  * Copyright (C) 2021 ARM Ltd.
20  */
21 
22 #define DRIVER_NAME "ARM FF-A"
23 #define pr_fmt(fmt) DRIVER_NAME ": " fmt
24 
25 #include <linux/arm_ffa.h>
26 #include <linux/bitfield.h>
27 #include <linux/device.h>
28 #include <linux/io.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/mm.h>
32 #include <linux/scatterlist.h>
33 #include <linux/slab.h>
34 #include <linux/uuid.h>
35 
36 #include "common.h"
37 
38 #define FFA_DRIVER_VERSION	FFA_VERSION_1_0
39 
40 #define FFA_SMC(calling_convention, func_num)				\
41 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, (calling_convention),	\
42 			   ARM_SMCCC_OWNER_STANDARD, (func_num))
43 
44 #define FFA_SMC_32(func_num)	FFA_SMC(ARM_SMCCC_SMC_32, (func_num))
45 #define FFA_SMC_64(func_num)	FFA_SMC(ARM_SMCCC_SMC_64, (func_num))
46 
47 #define FFA_ERROR			FFA_SMC_32(0x60)
48 #define FFA_SUCCESS			FFA_SMC_32(0x61)
49 #define FFA_INTERRUPT			FFA_SMC_32(0x62)
50 #define FFA_VERSION			FFA_SMC_32(0x63)
51 #define FFA_FEATURES			FFA_SMC_32(0x64)
52 #define FFA_RX_RELEASE			FFA_SMC_32(0x65)
53 #define FFA_RXTX_MAP			FFA_SMC_32(0x66)
54 #define FFA_FN64_RXTX_MAP		FFA_SMC_64(0x66)
55 #define FFA_RXTX_UNMAP			FFA_SMC_32(0x67)
56 #define FFA_PARTITION_INFO_GET		FFA_SMC_32(0x68)
57 #define FFA_ID_GET			FFA_SMC_32(0x69)
58 #define FFA_MSG_POLL			FFA_SMC_32(0x6A)
59 #define FFA_MSG_WAIT			FFA_SMC_32(0x6B)
60 #define FFA_YIELD			FFA_SMC_32(0x6C)
61 #define FFA_RUN				FFA_SMC_32(0x6D)
62 #define FFA_MSG_SEND			FFA_SMC_32(0x6E)
63 #define FFA_MSG_SEND_DIRECT_REQ		FFA_SMC_32(0x6F)
64 #define FFA_FN64_MSG_SEND_DIRECT_REQ	FFA_SMC_64(0x6F)
65 #define FFA_MSG_SEND_DIRECT_RESP	FFA_SMC_32(0x70)
66 #define FFA_FN64_MSG_SEND_DIRECT_RESP	FFA_SMC_64(0x70)
67 #define FFA_MEM_DONATE			FFA_SMC_32(0x71)
68 #define FFA_FN64_MEM_DONATE		FFA_SMC_64(0x71)
69 #define FFA_MEM_LEND			FFA_SMC_32(0x72)
70 #define FFA_FN64_MEM_LEND		FFA_SMC_64(0x72)
71 #define FFA_MEM_SHARE			FFA_SMC_32(0x73)
72 #define FFA_FN64_MEM_SHARE		FFA_SMC_64(0x73)
73 #define FFA_MEM_RETRIEVE_REQ		FFA_SMC_32(0x74)
74 #define FFA_FN64_MEM_RETRIEVE_REQ	FFA_SMC_64(0x74)
75 #define FFA_MEM_RETRIEVE_RESP		FFA_SMC_32(0x75)
76 #define FFA_MEM_RELINQUISH		FFA_SMC_32(0x76)
77 #define FFA_MEM_RECLAIM			FFA_SMC_32(0x77)
78 #define FFA_MEM_OP_PAUSE		FFA_SMC_32(0x78)
79 #define FFA_MEM_OP_RESUME		FFA_SMC_32(0x79)
80 #define FFA_MEM_FRAG_RX			FFA_SMC_32(0x7A)
81 #define FFA_MEM_FRAG_TX			FFA_SMC_32(0x7B)
82 #define FFA_NORMAL_WORLD_RESUME		FFA_SMC_32(0x7C)
83 
84 /*
85  * For some calls it is necessary to use SMC64 to pass or return 64-bit values.
86  * For such calls FFA_FN_NATIVE(name) will choose the appropriate
87  * (native-width) function ID.
88  */
89 #ifdef CONFIG_64BIT
90 #define FFA_FN_NATIVE(name)	FFA_FN64_##name
91 #else
92 #define FFA_FN_NATIVE(name)	FFA_##name
93 #endif
94 
95 /* FFA error codes. */
96 #define FFA_RET_SUCCESS            (0)
97 #define FFA_RET_NOT_SUPPORTED      (-1)
98 #define FFA_RET_INVALID_PARAMETERS (-2)
99 #define FFA_RET_NO_MEMORY          (-3)
100 #define FFA_RET_BUSY               (-4)
101 #define FFA_RET_INTERRUPTED        (-5)
102 #define FFA_RET_DENIED             (-6)
103 #define FFA_RET_RETRY              (-7)
104 #define FFA_RET_ABORTED            (-8)
105 
106 #define MAJOR_VERSION_MASK	GENMASK(30, 16)
107 #define MINOR_VERSION_MASK	GENMASK(15, 0)
108 #define MAJOR_VERSION(x)	((u16)(FIELD_GET(MAJOR_VERSION_MASK, (x))))
109 #define MINOR_VERSION(x)	((u16)(FIELD_GET(MINOR_VERSION_MASK, (x))))
110 #define PACK_VERSION_INFO(major, minor)			\
111 	(FIELD_PREP(MAJOR_VERSION_MASK, (major)) |	\
112 	 FIELD_PREP(MINOR_VERSION_MASK, (minor)))
113 #define FFA_VERSION_1_0		PACK_VERSION_INFO(1, 0)
114 #define FFA_MIN_VERSION		FFA_VERSION_1_0
115 
116 #define SENDER_ID_MASK		GENMASK(31, 16)
117 #define RECEIVER_ID_MASK	GENMASK(15, 0)
118 #define SENDER_ID(x)		((u16)(FIELD_GET(SENDER_ID_MASK, (x))))
119 #define RECEIVER_ID(x)		((u16)(FIELD_GET(RECEIVER_ID_MASK, (x))))
120 #define PACK_TARGET_INFO(s, r)		\
121 	(FIELD_PREP(SENDER_ID_MASK, (s)) | FIELD_PREP(RECEIVER_ID_MASK, (r)))
122 
123 /*
124  * FF-A specification mentions explicitly about '4K pages'. This should
125  * not be confused with the kernel PAGE_SIZE, which is the translation
126  * granule kernel is configured and may be one among 4K, 16K and 64K.
127  */
128 #define FFA_PAGE_SIZE		SZ_4K
129 /*
130  * Keeping RX TX buffer size as 4K for now
131  * 64K may be preferred to keep it min a page in 64K PAGE_SIZE config
132  */
133 #define RXTX_BUFFER_SIZE	SZ_4K
134 
135 static ffa_fn *invoke_ffa_fn;
136 
137 static const int ffa_linux_errmap[] = {
138 	/* better than switch case as long as return value is continuous */
139 	0,		/* FFA_RET_SUCCESS */
140 	-EOPNOTSUPP,	/* FFA_RET_NOT_SUPPORTED */
141 	-EINVAL,	/* FFA_RET_INVALID_PARAMETERS */
142 	-ENOMEM,	/* FFA_RET_NO_MEMORY */
143 	-EBUSY,		/* FFA_RET_BUSY */
144 	-EINTR,		/* FFA_RET_INTERRUPTED */
145 	-EACCES,	/* FFA_RET_DENIED */
146 	-EAGAIN,	/* FFA_RET_RETRY */
147 	-ECANCELED,	/* FFA_RET_ABORTED */
148 };
149 
150 static inline int ffa_to_linux_errno(int errno)
151 {
152 	int err_idx = -errno;
153 
154 	if (err_idx >= 0 && err_idx < ARRAY_SIZE(ffa_linux_errmap))
155 		return ffa_linux_errmap[err_idx];
156 	return -EINVAL;
157 }
158 
159 struct ffa_drv_info {
160 	u32 version;
161 	u16 vm_id;
162 	struct mutex rx_lock; /* lock to protect Rx buffer */
163 	struct mutex tx_lock; /* lock to protect Tx buffer */
164 	void *rx_buffer;
165 	void *tx_buffer;
166 	bool mem_ops_native;
167 };
168 
169 static struct ffa_drv_info *drv_info;
170 
171 /*
172  * The driver must be able to support all the versions from the earliest
173  * supported FFA_MIN_VERSION to the latest supported FFA_DRIVER_VERSION.
174  * The specification states that if firmware supports a FFA implementation
175  * that is incompatible with and at a greater version number than specified
176  * by the caller(FFA_DRIVER_VERSION passed as parameter to FFA_VERSION),
177  * it must return the NOT_SUPPORTED error code.
178  */
179 static u32 ffa_compatible_version_find(u32 version)
180 {
181 	u16 major = MAJOR_VERSION(version), minor = MINOR_VERSION(version);
182 	u16 drv_major = MAJOR_VERSION(FFA_DRIVER_VERSION);
183 	u16 drv_minor = MINOR_VERSION(FFA_DRIVER_VERSION);
184 
185 	if ((major < drv_major) || (major == drv_major && minor <= drv_minor))
186 		return version;
187 
188 	pr_info("Firmware version higher than driver version, downgrading\n");
189 	return FFA_DRIVER_VERSION;
190 }
191 
192 static int ffa_version_check(u32 *version)
193 {
194 	ffa_value_t ver;
195 
196 	invoke_ffa_fn((ffa_value_t){
197 		      .a0 = FFA_VERSION, .a1 = FFA_DRIVER_VERSION,
198 		      }, &ver);
199 
200 	if (ver.a0 == FFA_RET_NOT_SUPPORTED) {
201 		pr_info("FFA_VERSION returned not supported\n");
202 		return -EOPNOTSUPP;
203 	}
204 
205 	if (ver.a0 < FFA_MIN_VERSION) {
206 		pr_err("Incompatible v%d.%d! Earliest supported v%d.%d\n",
207 		       MAJOR_VERSION(ver.a0), MINOR_VERSION(ver.a0),
208 		       MAJOR_VERSION(FFA_MIN_VERSION),
209 		       MINOR_VERSION(FFA_MIN_VERSION));
210 		return -EINVAL;
211 	}
212 
213 	pr_info("Driver version %d.%d\n", MAJOR_VERSION(FFA_DRIVER_VERSION),
214 		MINOR_VERSION(FFA_DRIVER_VERSION));
215 	pr_info("Firmware version %d.%d found\n", MAJOR_VERSION(ver.a0),
216 		MINOR_VERSION(ver.a0));
217 	*version = ffa_compatible_version_find(ver.a0);
218 
219 	return 0;
220 }
221 
222 static int ffa_rx_release(void)
223 {
224 	ffa_value_t ret;
225 
226 	invoke_ffa_fn((ffa_value_t){
227 		      .a0 = FFA_RX_RELEASE,
228 		      }, &ret);
229 
230 	if (ret.a0 == FFA_ERROR)
231 		return ffa_to_linux_errno((int)ret.a2);
232 
233 	/* check for ret.a0 == FFA_RX_RELEASE ? */
234 
235 	return 0;
236 }
237 
238 static int ffa_rxtx_map(phys_addr_t tx_buf, phys_addr_t rx_buf, u32 pg_cnt)
239 {
240 	ffa_value_t ret;
241 
242 	invoke_ffa_fn((ffa_value_t){
243 		      .a0 = FFA_FN_NATIVE(RXTX_MAP),
244 		      .a1 = tx_buf, .a2 = rx_buf, .a3 = pg_cnt,
245 		      }, &ret);
246 
247 	if (ret.a0 == FFA_ERROR)
248 		return ffa_to_linux_errno((int)ret.a2);
249 
250 	return 0;
251 }
252 
253 static int ffa_rxtx_unmap(u16 vm_id)
254 {
255 	ffa_value_t ret;
256 
257 	invoke_ffa_fn((ffa_value_t){
258 		      .a0 = FFA_RXTX_UNMAP, .a1 = PACK_TARGET_INFO(vm_id, 0),
259 		      }, &ret);
260 
261 	if (ret.a0 == FFA_ERROR)
262 		return ffa_to_linux_errno((int)ret.a2);
263 
264 	return 0;
265 }
266 
267 #define PARTITION_INFO_GET_RETURN_COUNT_ONLY	BIT(0)
268 
269 /* buffer must be sizeof(struct ffa_partition_info) * num_partitions */
270 static int
271 __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,
272 			 struct ffa_partition_info *buffer, int num_partitions)
273 {
274 	int idx, count, flags = 0, sz, buf_sz;
275 	ffa_value_t partition_info;
276 
277 	if (!buffer || !num_partitions) /* Just get the count for now */
278 		flags = PARTITION_INFO_GET_RETURN_COUNT_ONLY;
279 
280 	mutex_lock(&drv_info->rx_lock);
281 	invoke_ffa_fn((ffa_value_t){
282 		      .a0 = FFA_PARTITION_INFO_GET,
283 		      .a1 = uuid0, .a2 = uuid1, .a3 = uuid2, .a4 = uuid3,
284 		      .a5 = flags,
285 		      }, &partition_info);
286 
287 	if (partition_info.a0 == FFA_ERROR) {
288 		mutex_unlock(&drv_info->rx_lock);
289 		return ffa_to_linux_errno((int)partition_info.a2);
290 	}
291 
292 	count = partition_info.a2;
293 
294 	if (drv_info->version > FFA_VERSION_1_0) {
295 		buf_sz = sz = partition_info.a3;
296 		if (sz > sizeof(*buffer))
297 			buf_sz = sizeof(*buffer);
298 	} else {
299 		/* FFA_VERSION_1_0 lacks size in the response */
300 		buf_sz = sz = 8;
301 	}
302 
303 	if (buffer && count <= num_partitions)
304 		for (idx = 0; idx < count; idx++)
305 			memcpy(buffer + idx, drv_info->rx_buffer + idx * sz,
306 			       buf_sz);
307 
308 	ffa_rx_release();
309 
310 	mutex_unlock(&drv_info->rx_lock);
311 
312 	return count;
313 }
314 
315 /* buffer is allocated and caller must free the same if returned count > 0 */
316 static int
317 ffa_partition_probe(const uuid_t *uuid, struct ffa_partition_info **buffer)
318 {
319 	int count;
320 	u32 uuid0_4[4];
321 	struct ffa_partition_info *pbuf;
322 
323 	export_uuid((u8 *)uuid0_4, uuid);
324 	count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1], uuid0_4[2],
325 					 uuid0_4[3], NULL, 0);
326 	if (count <= 0)
327 		return count;
328 
329 	pbuf = kcalloc(count, sizeof(*pbuf), GFP_KERNEL);
330 	if (!pbuf)
331 		return -ENOMEM;
332 
333 	count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1], uuid0_4[2],
334 					 uuid0_4[3], pbuf, count);
335 	if (count <= 0)
336 		kfree(pbuf);
337 	else
338 		*buffer = pbuf;
339 
340 	return count;
341 }
342 
343 #define VM_ID_MASK	GENMASK(15, 0)
344 static int ffa_id_get(u16 *vm_id)
345 {
346 	ffa_value_t id;
347 
348 	invoke_ffa_fn((ffa_value_t){
349 		      .a0 = FFA_ID_GET,
350 		      }, &id);
351 
352 	if (id.a0 == FFA_ERROR)
353 		return ffa_to_linux_errno((int)id.a2);
354 
355 	*vm_id = FIELD_GET(VM_ID_MASK, (id.a2));
356 
357 	return 0;
358 }
359 
360 static int ffa_msg_send_direct_req(u16 src_id, u16 dst_id, bool mode_32bit,
361 				   struct ffa_send_direct_data *data)
362 {
363 	u32 req_id, resp_id, src_dst_ids = PACK_TARGET_INFO(src_id, dst_id);
364 	ffa_value_t ret;
365 
366 	if (mode_32bit) {
367 		req_id = FFA_MSG_SEND_DIRECT_REQ;
368 		resp_id = FFA_MSG_SEND_DIRECT_RESP;
369 	} else {
370 		req_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_REQ);
371 		resp_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_RESP);
372 	}
373 
374 	invoke_ffa_fn((ffa_value_t){
375 		      .a0 = req_id, .a1 = src_dst_ids, .a2 = 0,
376 		      .a3 = data->data0, .a4 = data->data1, .a5 = data->data2,
377 		      .a6 = data->data3, .a7 = data->data4,
378 		      }, &ret);
379 
380 	while (ret.a0 == FFA_INTERRUPT)
381 		invoke_ffa_fn((ffa_value_t){
382 			      .a0 = FFA_RUN, .a1 = ret.a1,
383 			      }, &ret);
384 
385 	if (ret.a0 == FFA_ERROR)
386 		return ffa_to_linux_errno((int)ret.a2);
387 
388 	if (ret.a0 == resp_id) {
389 		data->data0 = ret.a3;
390 		data->data1 = ret.a4;
391 		data->data2 = ret.a5;
392 		data->data3 = ret.a6;
393 		data->data4 = ret.a7;
394 		return 0;
395 	}
396 
397 	return -EINVAL;
398 }
399 
400 static int ffa_mem_first_frag(u32 func_id, phys_addr_t buf, u32 buf_sz,
401 			      u32 frag_len, u32 len, u64 *handle)
402 {
403 	ffa_value_t ret;
404 
405 	invoke_ffa_fn((ffa_value_t){
406 		      .a0 = func_id, .a1 = len, .a2 = frag_len,
407 		      .a3 = buf, .a4 = buf_sz,
408 		      }, &ret);
409 
410 	while (ret.a0 == FFA_MEM_OP_PAUSE)
411 		invoke_ffa_fn((ffa_value_t){
412 			      .a0 = FFA_MEM_OP_RESUME,
413 			      .a1 = ret.a1, .a2 = ret.a2,
414 			      }, &ret);
415 
416 	if (ret.a0 == FFA_ERROR)
417 		return ffa_to_linux_errno((int)ret.a2);
418 
419 	if (ret.a0 == FFA_SUCCESS) {
420 		if (handle)
421 			*handle = PACK_HANDLE(ret.a2, ret.a3);
422 	} else if (ret.a0 == FFA_MEM_FRAG_RX) {
423 		if (handle)
424 			*handle = PACK_HANDLE(ret.a1, ret.a2);
425 	} else {
426 		return -EOPNOTSUPP;
427 	}
428 
429 	return frag_len;
430 }
431 
432 static int ffa_mem_next_frag(u64 handle, u32 frag_len)
433 {
434 	ffa_value_t ret;
435 
436 	invoke_ffa_fn((ffa_value_t){
437 		      .a0 = FFA_MEM_FRAG_TX,
438 		      .a1 = HANDLE_LOW(handle), .a2 = HANDLE_HIGH(handle),
439 		      .a3 = frag_len,
440 		      }, &ret);
441 
442 	while (ret.a0 == FFA_MEM_OP_PAUSE)
443 		invoke_ffa_fn((ffa_value_t){
444 			      .a0 = FFA_MEM_OP_RESUME,
445 			      .a1 = ret.a1, .a2 = ret.a2,
446 			      }, &ret);
447 
448 	if (ret.a0 == FFA_ERROR)
449 		return ffa_to_linux_errno((int)ret.a2);
450 
451 	if (ret.a0 == FFA_MEM_FRAG_RX)
452 		return ret.a3;
453 	else if (ret.a0 == FFA_SUCCESS)
454 		return 0;
455 
456 	return -EOPNOTSUPP;
457 }
458 
459 static int
460 ffa_transmit_fragment(u32 func_id, phys_addr_t buf, u32 buf_sz, u32 frag_len,
461 		      u32 len, u64 *handle, bool first)
462 {
463 	if (!first)
464 		return ffa_mem_next_frag(*handle, frag_len);
465 
466 	return ffa_mem_first_frag(func_id, buf, buf_sz, frag_len, len, handle);
467 }
468 
469 static u32 ffa_get_num_pages_sg(struct scatterlist *sg)
470 {
471 	u32 num_pages = 0;
472 
473 	do {
474 		num_pages += sg->length / FFA_PAGE_SIZE;
475 	} while ((sg = sg_next(sg)));
476 
477 	return num_pages;
478 }
479 
480 static int
481 ffa_setup_and_transmit(u32 func_id, void *buffer, u32 max_fragsize,
482 		       struct ffa_mem_ops_args *args)
483 {
484 	int rc = 0;
485 	bool first = true;
486 	phys_addr_t addr = 0;
487 	struct ffa_composite_mem_region *composite;
488 	struct ffa_mem_region_addr_range *constituents;
489 	struct ffa_mem_region_attributes *ep_mem_access;
490 	struct ffa_mem_region *mem_region = buffer;
491 	u32 idx, frag_len, length, buf_sz = 0, num_entries = sg_nents(args->sg);
492 
493 	mem_region->tag = args->tag;
494 	mem_region->flags = args->flags;
495 	mem_region->sender_id = drv_info->vm_id;
496 	mem_region->attributes = FFA_MEM_NORMAL | FFA_MEM_WRITE_BACK |
497 				 FFA_MEM_INNER_SHAREABLE;
498 	ep_mem_access = &mem_region->ep_mem_access[0];
499 
500 	for (idx = 0; idx < args->nattrs; idx++, ep_mem_access++) {
501 		ep_mem_access->receiver = args->attrs[idx].receiver;
502 		ep_mem_access->attrs = args->attrs[idx].attrs;
503 		ep_mem_access->composite_off = COMPOSITE_OFFSET(args->nattrs);
504 	}
505 	mem_region->ep_count = args->nattrs;
506 
507 	composite = buffer + COMPOSITE_OFFSET(args->nattrs);
508 	composite->total_pg_cnt = ffa_get_num_pages_sg(args->sg);
509 	composite->addr_range_cnt = num_entries;
510 
511 	length = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, num_entries);
512 	frag_len = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, 0);
513 	if (frag_len > max_fragsize)
514 		return -ENXIO;
515 
516 	if (!args->use_txbuf) {
517 		addr = virt_to_phys(buffer);
518 		buf_sz = max_fragsize / FFA_PAGE_SIZE;
519 	}
520 
521 	constituents = buffer + frag_len;
522 	idx = 0;
523 	do {
524 		if (frag_len == max_fragsize) {
525 			rc = ffa_transmit_fragment(func_id, addr, buf_sz,
526 						   frag_len, length,
527 						   &args->g_handle, first);
528 			if (rc < 0)
529 				return -ENXIO;
530 
531 			first = false;
532 			idx = 0;
533 			frag_len = 0;
534 			constituents = buffer;
535 		}
536 
537 		if ((void *)constituents - buffer > max_fragsize) {
538 			pr_err("Memory Region Fragment > Tx Buffer size\n");
539 			return -EFAULT;
540 		}
541 
542 		constituents->address = sg_phys(args->sg);
543 		constituents->pg_cnt = args->sg->length / FFA_PAGE_SIZE;
544 		constituents++;
545 		frag_len += sizeof(struct ffa_mem_region_addr_range);
546 	} while ((args->sg = sg_next(args->sg)));
547 
548 	return ffa_transmit_fragment(func_id, addr, buf_sz, frag_len,
549 				     length, &args->g_handle, first);
550 }
551 
552 static int ffa_memory_ops(u32 func_id, struct ffa_mem_ops_args *args)
553 {
554 	int ret;
555 	void *buffer;
556 
557 	if (!args->use_txbuf) {
558 		buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
559 		if (!buffer)
560 			return -ENOMEM;
561 	} else {
562 		buffer = drv_info->tx_buffer;
563 		mutex_lock(&drv_info->tx_lock);
564 	}
565 
566 	ret = ffa_setup_and_transmit(func_id, buffer, RXTX_BUFFER_SIZE, args);
567 
568 	if (args->use_txbuf)
569 		mutex_unlock(&drv_info->tx_lock);
570 	else
571 		free_pages_exact(buffer, RXTX_BUFFER_SIZE);
572 
573 	return ret < 0 ? ret : 0;
574 }
575 
576 static int ffa_memory_reclaim(u64 g_handle, u32 flags)
577 {
578 	ffa_value_t ret;
579 
580 	invoke_ffa_fn((ffa_value_t){
581 		      .a0 = FFA_MEM_RECLAIM,
582 		      .a1 = HANDLE_LOW(g_handle), .a2 = HANDLE_HIGH(g_handle),
583 		      .a3 = flags,
584 		      }, &ret);
585 
586 	if (ret.a0 == FFA_ERROR)
587 		return ffa_to_linux_errno((int)ret.a2);
588 
589 	return 0;
590 }
591 
592 static int ffa_features(u32 func_feat_id, u32 input_props,
593 			u32 *if_props_1, u32 *if_props_2)
594 {
595 	ffa_value_t id;
596 
597 	if (!ARM_SMCCC_IS_FAST_CALL(func_feat_id) && input_props) {
598 		pr_err("%s: Invalid Parameters: %x, %x", __func__,
599 		       func_feat_id, input_props);
600 		return ffa_to_linux_errno(FFA_RET_INVALID_PARAMETERS);
601 	}
602 
603 	invoke_ffa_fn((ffa_value_t){
604 		.a0 = FFA_FEATURES, .a1 = func_feat_id, .a2 = input_props,
605 		}, &id);
606 
607 	if (id.a0 == FFA_ERROR)
608 		return ffa_to_linux_errno((int)id.a2);
609 
610 	if (if_props_1)
611 		*if_props_1 = id.a2;
612 	if (if_props_2)
613 		*if_props_2 = id.a3;
614 
615 	return 0;
616 }
617 
618 static void ffa_set_up_mem_ops_native_flag(void)
619 {
620 	if (!ffa_features(FFA_FN_NATIVE(MEM_LEND), 0, NULL, NULL) ||
621 	    !ffa_features(FFA_FN_NATIVE(MEM_SHARE), 0, NULL, NULL))
622 		drv_info->mem_ops_native = true;
623 }
624 
625 static u32 ffa_api_version_get(void)
626 {
627 	return drv_info->version;
628 }
629 
630 static int ffa_partition_info_get(const char *uuid_str,
631 				  struct ffa_partition_info *buffer)
632 {
633 	int count;
634 	uuid_t uuid;
635 	struct ffa_partition_info *pbuf;
636 
637 	if (uuid_parse(uuid_str, &uuid)) {
638 		pr_err("invalid uuid (%s)\n", uuid_str);
639 		return -ENODEV;
640 	}
641 
642 	count = ffa_partition_probe(&uuid, &pbuf);
643 	if (count <= 0)
644 		return -ENOENT;
645 
646 	memcpy(buffer, pbuf, sizeof(*pbuf) * count);
647 	kfree(pbuf);
648 	return 0;
649 }
650 
651 static void ffa_mode_32bit_set(struct ffa_device *dev)
652 {
653 	dev->mode_32bit = true;
654 }
655 
656 static int ffa_sync_send_receive(struct ffa_device *dev,
657 				 struct ffa_send_direct_data *data)
658 {
659 	return ffa_msg_send_direct_req(drv_info->vm_id, dev->vm_id,
660 				       dev->mode_32bit, data);
661 }
662 
663 static int ffa_memory_share(struct ffa_mem_ops_args *args)
664 {
665 	if (drv_info->mem_ops_native)
666 		return ffa_memory_ops(FFA_FN_NATIVE(MEM_SHARE), args);
667 
668 	return ffa_memory_ops(FFA_MEM_SHARE, args);
669 }
670 
671 static int ffa_memory_lend(struct ffa_mem_ops_args *args)
672 {
673 	/* Note that upon a successful MEM_LEND request the caller
674 	 * must ensure that the memory region specified is not accessed
675 	 * until a successful MEM_RECALIM call has been made.
676 	 * On systems with a hypervisor present this will been enforced,
677 	 * however on systems without a hypervisor the responsibility
678 	 * falls to the calling kernel driver to prevent access.
679 	 */
680 	if (drv_info->mem_ops_native)
681 		return ffa_memory_ops(FFA_FN_NATIVE(MEM_LEND), args);
682 
683 	return ffa_memory_ops(FFA_MEM_LEND, args);
684 }
685 
686 static const struct ffa_ops ffa_ops = {
687 	.api_version_get = ffa_api_version_get,
688 	.partition_info_get = ffa_partition_info_get,
689 	.mode_32bit_set = ffa_mode_32bit_set,
690 	.sync_send_receive = ffa_sync_send_receive,
691 	.memory_reclaim = ffa_memory_reclaim,
692 	.memory_share = ffa_memory_share,
693 	.memory_lend = ffa_memory_lend,
694 };
695 
696 void ffa_device_match_uuid(struct ffa_device *ffa_dev, const uuid_t *uuid)
697 {
698 	int count, idx;
699 	struct ffa_partition_info *pbuf, *tpbuf;
700 
701 	/*
702 	 * FF-A v1.1 provides UUID for each partition as part of the discovery
703 	 * API, the discovered UUID must be populated in the device's UUID and
704 	 * there is no need to copy the same from the driver table.
705 	 */
706 	if (drv_info->version > FFA_VERSION_1_0)
707 		return;
708 
709 	count = ffa_partition_probe(uuid, &pbuf);
710 	if (count <= 0)
711 		return;
712 
713 	for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++)
714 		if (tpbuf->id == ffa_dev->vm_id)
715 			uuid_copy(&ffa_dev->uuid, uuid);
716 	kfree(pbuf);
717 }
718 
719 static void ffa_setup_partitions(void)
720 {
721 	int count, idx;
722 	uuid_t uuid;
723 	struct ffa_device *ffa_dev;
724 	struct ffa_partition_info *pbuf, *tpbuf;
725 
726 	count = ffa_partition_probe(&uuid_null, &pbuf);
727 	if (count <= 0) {
728 		pr_info("%s: No partitions found, error %d\n", __func__, count);
729 		return;
730 	}
731 
732 	for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++) {
733 		import_uuid(&uuid, (u8 *)tpbuf->uuid);
734 
735 		/* Note that if the UUID will be uuid_null, that will require
736 		 * ffa_device_match() to find the UUID of this partition id
737 		 * with help of ffa_device_match_uuid(). FF-A v1.1 and above
738 		 * provides UUID here for each partition as part of the
739 		 * discovery API and the same is passed.
740 		 */
741 		ffa_dev = ffa_device_register(&uuid, tpbuf->id, &ffa_ops);
742 		if (!ffa_dev) {
743 			pr_err("%s: failed to register partition ID 0x%x\n",
744 			       __func__, tpbuf->id);
745 			continue;
746 		}
747 	}
748 	kfree(pbuf);
749 }
750 
751 static int __init ffa_init(void)
752 {
753 	int ret;
754 
755 	ret = ffa_transport_init(&invoke_ffa_fn);
756 	if (ret)
757 		return ret;
758 
759 	ret = arm_ffa_bus_init();
760 	if (ret)
761 		return ret;
762 
763 	drv_info = kzalloc(sizeof(*drv_info), GFP_KERNEL);
764 	if (!drv_info) {
765 		ret = -ENOMEM;
766 		goto ffa_bus_exit;
767 	}
768 
769 	ret = ffa_version_check(&drv_info->version);
770 	if (ret)
771 		goto free_drv_info;
772 
773 	if (ffa_id_get(&drv_info->vm_id)) {
774 		pr_err("failed to obtain VM id for self\n");
775 		ret = -ENODEV;
776 		goto free_drv_info;
777 	}
778 
779 	drv_info->rx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
780 	if (!drv_info->rx_buffer) {
781 		ret = -ENOMEM;
782 		goto free_pages;
783 	}
784 
785 	drv_info->tx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
786 	if (!drv_info->tx_buffer) {
787 		ret = -ENOMEM;
788 		goto free_pages;
789 	}
790 
791 	ret = ffa_rxtx_map(virt_to_phys(drv_info->tx_buffer),
792 			   virt_to_phys(drv_info->rx_buffer),
793 			   RXTX_BUFFER_SIZE / FFA_PAGE_SIZE);
794 	if (ret) {
795 		pr_err("failed to register FFA RxTx buffers\n");
796 		goto free_pages;
797 	}
798 
799 	mutex_init(&drv_info->rx_lock);
800 	mutex_init(&drv_info->tx_lock);
801 
802 	ffa_setup_partitions();
803 
804 	ffa_set_up_mem_ops_native_flag();
805 
806 	return 0;
807 free_pages:
808 	if (drv_info->tx_buffer)
809 		free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE);
810 	free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE);
811 free_drv_info:
812 	kfree(drv_info);
813 ffa_bus_exit:
814 	arm_ffa_bus_exit();
815 	return ret;
816 }
817 subsys_initcall(ffa_init);
818 
819 static void __exit ffa_exit(void)
820 {
821 	ffa_rxtx_unmap(drv_info->vm_id);
822 	free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE);
823 	free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE);
824 	kfree(drv_info);
825 	arm_ffa_bus_exit();
826 }
827 module_exit(ffa_exit);
828 
829 MODULE_ALIAS("arm-ffa");
830 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
831 MODULE_DESCRIPTION("Arm FF-A interface driver");
832 MODULE_LICENSE("GPL v2");
833