xref: /linux/drivers/firmware/arm_ffa/driver.c (revision 34f7c6e7d4396090692a09789db231e12cb4762b)
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 };
167 
168 static struct ffa_drv_info *drv_info;
169 
170 /*
171  * The driver must be able to support all the versions from the earliest
172  * supported FFA_MIN_VERSION to the latest supported FFA_DRIVER_VERSION.
173  * The specification states that if firmware supports a FFA implementation
174  * that is incompatible with and at a greater version number than specified
175  * by the caller(FFA_DRIVER_VERSION passed as parameter to FFA_VERSION),
176  * it must return the NOT_SUPPORTED error code.
177  */
178 static u32 ffa_compatible_version_find(u32 version)
179 {
180 	u16 major = MAJOR_VERSION(version), minor = MINOR_VERSION(version);
181 	u16 drv_major = MAJOR_VERSION(FFA_DRIVER_VERSION);
182 	u16 drv_minor = MINOR_VERSION(FFA_DRIVER_VERSION);
183 
184 	if ((major < drv_major) || (major == drv_major && minor <= drv_minor))
185 		return version;
186 
187 	pr_info("Firmware version higher than driver version, downgrading\n");
188 	return FFA_DRIVER_VERSION;
189 }
190 
191 static int ffa_version_check(u32 *version)
192 {
193 	ffa_value_t ver;
194 
195 	invoke_ffa_fn((ffa_value_t){
196 		      .a0 = FFA_VERSION, .a1 = FFA_DRIVER_VERSION,
197 		      }, &ver);
198 
199 	if (ver.a0 == FFA_RET_NOT_SUPPORTED) {
200 		pr_info("FFA_VERSION returned not supported\n");
201 		return -EOPNOTSUPP;
202 	}
203 
204 	if (ver.a0 < FFA_MIN_VERSION) {
205 		pr_err("Incompatible v%d.%d! Earliest supported v%d.%d\n",
206 		       MAJOR_VERSION(ver.a0), MINOR_VERSION(ver.a0),
207 		       MAJOR_VERSION(FFA_MIN_VERSION),
208 		       MINOR_VERSION(FFA_MIN_VERSION));
209 		return -EINVAL;
210 	}
211 
212 	pr_info("Driver version %d.%d\n", MAJOR_VERSION(FFA_DRIVER_VERSION),
213 		MINOR_VERSION(FFA_DRIVER_VERSION));
214 	pr_info("Firmware version %d.%d found\n", MAJOR_VERSION(ver.a0),
215 		MINOR_VERSION(ver.a0));
216 	*version = ffa_compatible_version_find(ver.a0);
217 
218 	return 0;
219 }
220 
221 static int ffa_rx_release(void)
222 {
223 	ffa_value_t ret;
224 
225 	invoke_ffa_fn((ffa_value_t){
226 		      .a0 = FFA_RX_RELEASE,
227 		      }, &ret);
228 
229 	if (ret.a0 == FFA_ERROR)
230 		return ffa_to_linux_errno((int)ret.a2);
231 
232 	/* check for ret.a0 == FFA_RX_RELEASE ? */
233 
234 	return 0;
235 }
236 
237 static int ffa_rxtx_map(phys_addr_t tx_buf, phys_addr_t rx_buf, u32 pg_cnt)
238 {
239 	ffa_value_t ret;
240 
241 	invoke_ffa_fn((ffa_value_t){
242 		      .a0 = FFA_FN_NATIVE(RXTX_MAP),
243 		      .a1 = tx_buf, .a2 = rx_buf, .a3 = pg_cnt,
244 		      }, &ret);
245 
246 	if (ret.a0 == FFA_ERROR)
247 		return ffa_to_linux_errno((int)ret.a2);
248 
249 	return 0;
250 }
251 
252 static int ffa_rxtx_unmap(u16 vm_id)
253 {
254 	ffa_value_t ret;
255 
256 	invoke_ffa_fn((ffa_value_t){
257 		      .a0 = FFA_RXTX_UNMAP, .a1 = PACK_TARGET_INFO(vm_id, 0),
258 		      }, &ret);
259 
260 	if (ret.a0 == FFA_ERROR)
261 		return ffa_to_linux_errno((int)ret.a2);
262 
263 	return 0;
264 }
265 
266 /* buffer must be sizeof(struct ffa_partition_info) * num_partitions */
267 static int
268 __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,
269 			 struct ffa_partition_info *buffer, int num_partitions)
270 {
271 	int count;
272 	ffa_value_t partition_info;
273 
274 	mutex_lock(&drv_info->rx_lock);
275 	invoke_ffa_fn((ffa_value_t){
276 		      .a0 = FFA_PARTITION_INFO_GET,
277 		      .a1 = uuid0, .a2 = uuid1, .a3 = uuid2, .a4 = uuid3,
278 		      }, &partition_info);
279 
280 	if (partition_info.a0 == FFA_ERROR) {
281 		mutex_unlock(&drv_info->rx_lock);
282 		return ffa_to_linux_errno((int)partition_info.a2);
283 	}
284 
285 	count = partition_info.a2;
286 
287 	if (buffer && count <= num_partitions)
288 		memcpy(buffer, drv_info->rx_buffer, sizeof(*buffer) * count);
289 
290 	ffa_rx_release();
291 
292 	mutex_unlock(&drv_info->rx_lock);
293 
294 	return count;
295 }
296 
297 /* buffer is allocated and caller must free the same if returned count > 0 */
298 static int
299 ffa_partition_probe(const uuid_t *uuid, struct ffa_partition_info **buffer)
300 {
301 	int count;
302 	u32 uuid0_4[4];
303 	struct ffa_partition_info *pbuf;
304 
305 	export_uuid((u8 *)uuid0_4, uuid);
306 	count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1], uuid0_4[2],
307 					 uuid0_4[3], NULL, 0);
308 	if (count <= 0)
309 		return count;
310 
311 	pbuf = kcalloc(count, sizeof(*pbuf), GFP_KERNEL);
312 	if (!pbuf)
313 		return -ENOMEM;
314 
315 	count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1], uuid0_4[2],
316 					 uuid0_4[3], pbuf, count);
317 	if (count <= 0)
318 		kfree(pbuf);
319 	else
320 		*buffer = pbuf;
321 
322 	return count;
323 }
324 
325 #define VM_ID_MASK	GENMASK(15, 0)
326 static int ffa_id_get(u16 *vm_id)
327 {
328 	ffa_value_t id;
329 
330 	invoke_ffa_fn((ffa_value_t){
331 		      .a0 = FFA_ID_GET,
332 		      }, &id);
333 
334 	if (id.a0 == FFA_ERROR)
335 		return ffa_to_linux_errno((int)id.a2);
336 
337 	*vm_id = FIELD_GET(VM_ID_MASK, (id.a2));
338 
339 	return 0;
340 }
341 
342 static int ffa_msg_send_direct_req(u16 src_id, u16 dst_id, bool mode_32bit,
343 				   struct ffa_send_direct_data *data)
344 {
345 	u32 req_id, resp_id, src_dst_ids = PACK_TARGET_INFO(src_id, dst_id);
346 	ffa_value_t ret;
347 
348 	if (mode_32bit) {
349 		req_id = FFA_MSG_SEND_DIRECT_REQ;
350 		resp_id = FFA_MSG_SEND_DIRECT_RESP;
351 	} else {
352 		req_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_REQ);
353 		resp_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_RESP);
354 	}
355 
356 	invoke_ffa_fn((ffa_value_t){
357 		      .a0 = req_id, .a1 = src_dst_ids, .a2 = 0,
358 		      .a3 = data->data0, .a4 = data->data1, .a5 = data->data2,
359 		      .a6 = data->data3, .a7 = data->data4,
360 		      }, &ret);
361 
362 	while (ret.a0 == FFA_INTERRUPT)
363 		invoke_ffa_fn((ffa_value_t){
364 			      .a0 = FFA_RUN, .a1 = ret.a1,
365 			      }, &ret);
366 
367 	if (ret.a0 == FFA_ERROR)
368 		return ffa_to_linux_errno((int)ret.a2);
369 
370 	if (ret.a0 == resp_id) {
371 		data->data0 = ret.a3;
372 		data->data1 = ret.a4;
373 		data->data2 = ret.a5;
374 		data->data3 = ret.a6;
375 		data->data4 = ret.a7;
376 		return 0;
377 	}
378 
379 	return -EINVAL;
380 }
381 
382 static int ffa_mem_first_frag(u32 func_id, phys_addr_t buf, u32 buf_sz,
383 			      u32 frag_len, u32 len, u64 *handle)
384 {
385 	ffa_value_t ret;
386 
387 	invoke_ffa_fn((ffa_value_t){
388 		      .a0 = func_id, .a1 = len, .a2 = frag_len,
389 		      .a3 = buf, .a4 = buf_sz,
390 		      }, &ret);
391 
392 	while (ret.a0 == FFA_MEM_OP_PAUSE)
393 		invoke_ffa_fn((ffa_value_t){
394 			      .a0 = FFA_MEM_OP_RESUME,
395 			      .a1 = ret.a1, .a2 = ret.a2,
396 			      }, &ret);
397 
398 	if (ret.a0 == FFA_ERROR)
399 		return ffa_to_linux_errno((int)ret.a2);
400 
401 	if (ret.a0 != FFA_SUCCESS)
402 		return -EOPNOTSUPP;
403 
404 	if (handle)
405 		*handle = PACK_HANDLE(ret.a2, ret.a3);
406 
407 	return frag_len;
408 }
409 
410 static int ffa_mem_next_frag(u64 handle, u32 frag_len)
411 {
412 	ffa_value_t ret;
413 
414 	invoke_ffa_fn((ffa_value_t){
415 		      .a0 = FFA_MEM_FRAG_TX,
416 		      .a1 = HANDLE_LOW(handle), .a2 = HANDLE_HIGH(handle),
417 		      .a3 = frag_len,
418 		      }, &ret);
419 
420 	while (ret.a0 == FFA_MEM_OP_PAUSE)
421 		invoke_ffa_fn((ffa_value_t){
422 			      .a0 = FFA_MEM_OP_RESUME,
423 			      .a1 = ret.a1, .a2 = ret.a2,
424 			      }, &ret);
425 
426 	if (ret.a0 == FFA_ERROR)
427 		return ffa_to_linux_errno((int)ret.a2);
428 
429 	if (ret.a0 != FFA_MEM_FRAG_RX)
430 		return -EOPNOTSUPP;
431 
432 	return ret.a3;
433 }
434 
435 static int
436 ffa_transmit_fragment(u32 func_id, phys_addr_t buf, u32 buf_sz, u32 frag_len,
437 		      u32 len, u64 *handle, bool first)
438 {
439 	if (!first)
440 		return ffa_mem_next_frag(*handle, frag_len);
441 
442 	return ffa_mem_first_frag(func_id, buf, buf_sz, frag_len, len, handle);
443 }
444 
445 static u32 ffa_get_num_pages_sg(struct scatterlist *sg)
446 {
447 	u32 num_pages = 0;
448 
449 	do {
450 		num_pages += sg->length / FFA_PAGE_SIZE;
451 	} while ((sg = sg_next(sg)));
452 
453 	return num_pages;
454 }
455 
456 static int
457 ffa_setup_and_transmit(u32 func_id, void *buffer, u32 max_fragsize,
458 		       struct ffa_mem_ops_args *args)
459 {
460 	int rc = 0;
461 	bool first = true;
462 	phys_addr_t addr = 0;
463 	struct ffa_composite_mem_region *composite;
464 	struct ffa_mem_region_addr_range *constituents;
465 	struct ffa_mem_region_attributes *ep_mem_access;
466 	struct ffa_mem_region *mem_region = buffer;
467 	u32 idx, frag_len, length, buf_sz = 0, num_entries = sg_nents(args->sg);
468 
469 	mem_region->tag = args->tag;
470 	mem_region->flags = args->flags;
471 	mem_region->sender_id = drv_info->vm_id;
472 	mem_region->attributes = FFA_MEM_NORMAL | FFA_MEM_WRITE_BACK |
473 				 FFA_MEM_INNER_SHAREABLE;
474 	ep_mem_access = &mem_region->ep_mem_access[0];
475 
476 	for (idx = 0; idx < args->nattrs; idx++, ep_mem_access++) {
477 		ep_mem_access->receiver = args->attrs[idx].receiver;
478 		ep_mem_access->attrs = args->attrs[idx].attrs;
479 		ep_mem_access->composite_off = COMPOSITE_OFFSET(args->nattrs);
480 	}
481 	mem_region->ep_count = args->nattrs;
482 
483 	composite = buffer + COMPOSITE_OFFSET(args->nattrs);
484 	composite->total_pg_cnt = ffa_get_num_pages_sg(args->sg);
485 	composite->addr_range_cnt = num_entries;
486 
487 	length = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, num_entries);
488 	frag_len = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, 0);
489 	if (frag_len > max_fragsize)
490 		return -ENXIO;
491 
492 	if (!args->use_txbuf) {
493 		addr = virt_to_phys(buffer);
494 		buf_sz = max_fragsize / FFA_PAGE_SIZE;
495 	}
496 
497 	constituents = buffer + frag_len;
498 	idx = 0;
499 	do {
500 		if (frag_len == max_fragsize) {
501 			rc = ffa_transmit_fragment(func_id, addr, buf_sz,
502 						   frag_len, length,
503 						   &args->g_handle, first);
504 			if (rc < 0)
505 				return -ENXIO;
506 
507 			first = false;
508 			idx = 0;
509 			frag_len = 0;
510 			constituents = buffer;
511 		}
512 
513 		if ((void *)constituents - buffer > max_fragsize) {
514 			pr_err("Memory Region Fragment > Tx Buffer size\n");
515 			return -EFAULT;
516 		}
517 
518 		constituents->address = sg_phys(args->sg);
519 		constituents->pg_cnt = args->sg->length / FFA_PAGE_SIZE;
520 		constituents++;
521 		frag_len += sizeof(struct ffa_mem_region_addr_range);
522 	} while ((args->sg = sg_next(args->sg)));
523 
524 	return ffa_transmit_fragment(func_id, addr, buf_sz, frag_len,
525 				     length, &args->g_handle, first);
526 }
527 
528 static int ffa_memory_ops(u32 func_id, struct ffa_mem_ops_args *args)
529 {
530 	int ret;
531 	void *buffer;
532 
533 	if (!args->use_txbuf) {
534 		buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
535 		if (!buffer)
536 			return -ENOMEM;
537 	} else {
538 		buffer = drv_info->tx_buffer;
539 		mutex_lock(&drv_info->tx_lock);
540 	}
541 
542 	ret = ffa_setup_and_transmit(func_id, buffer, RXTX_BUFFER_SIZE, args);
543 
544 	if (args->use_txbuf)
545 		mutex_unlock(&drv_info->tx_lock);
546 	else
547 		free_pages_exact(buffer, RXTX_BUFFER_SIZE);
548 
549 	return ret < 0 ? ret : 0;
550 }
551 
552 static int ffa_memory_reclaim(u64 g_handle, u32 flags)
553 {
554 	ffa_value_t ret;
555 
556 	invoke_ffa_fn((ffa_value_t){
557 		      .a0 = FFA_MEM_RECLAIM,
558 		      .a1 = HANDLE_LOW(g_handle), .a2 = HANDLE_HIGH(g_handle),
559 		      .a3 = flags,
560 		      }, &ret);
561 
562 	if (ret.a0 == FFA_ERROR)
563 		return ffa_to_linux_errno((int)ret.a2);
564 
565 	return 0;
566 }
567 
568 static u32 ffa_api_version_get(void)
569 {
570 	return drv_info->version;
571 }
572 
573 static int ffa_partition_info_get(const char *uuid_str,
574 				  struct ffa_partition_info *buffer)
575 {
576 	int count;
577 	uuid_t uuid;
578 	struct ffa_partition_info *pbuf;
579 
580 	if (uuid_parse(uuid_str, &uuid)) {
581 		pr_err("invalid uuid (%s)\n", uuid_str);
582 		return -ENODEV;
583 	}
584 
585 	count = ffa_partition_probe(&uuid_null, &pbuf);
586 	if (count <= 0)
587 		return -ENOENT;
588 
589 	memcpy(buffer, pbuf, sizeof(*pbuf) * count);
590 	kfree(pbuf);
591 	return 0;
592 }
593 
594 static void ffa_mode_32bit_set(struct ffa_device *dev)
595 {
596 	dev->mode_32bit = true;
597 }
598 
599 static int ffa_sync_send_receive(struct ffa_device *dev,
600 				 struct ffa_send_direct_data *data)
601 {
602 	return ffa_msg_send_direct_req(drv_info->vm_id, dev->vm_id,
603 				       dev->mode_32bit, data);
604 }
605 
606 static int
607 ffa_memory_share(struct ffa_device *dev, struct ffa_mem_ops_args *args)
608 {
609 	if (dev->mode_32bit)
610 		return ffa_memory_ops(FFA_MEM_SHARE, args);
611 
612 	return ffa_memory_ops(FFA_FN_NATIVE(MEM_SHARE), args);
613 }
614 
615 static int
616 ffa_memory_lend(struct ffa_device *dev, struct ffa_mem_ops_args *args)
617 {
618 	/* Note that upon a successful MEM_LEND request the caller
619 	 * must ensure that the memory region specified is not accessed
620 	 * until a successful MEM_RECALIM call has been made.
621 	 * On systems with a hypervisor present this will been enforced,
622 	 * however on systems without a hypervisor the responsibility
623 	 * falls to the calling kernel driver to prevent access.
624 	 */
625 	if (dev->mode_32bit)
626 		return ffa_memory_ops(FFA_MEM_LEND, args);
627 
628 	return ffa_memory_ops(FFA_FN_NATIVE(MEM_LEND), args);
629 }
630 
631 static const struct ffa_dev_ops ffa_ops = {
632 	.api_version_get = ffa_api_version_get,
633 	.partition_info_get = ffa_partition_info_get,
634 	.mode_32bit_set = ffa_mode_32bit_set,
635 	.sync_send_receive = ffa_sync_send_receive,
636 	.memory_reclaim = ffa_memory_reclaim,
637 	.memory_share = ffa_memory_share,
638 	.memory_lend = ffa_memory_lend,
639 };
640 
641 const struct ffa_dev_ops *ffa_dev_ops_get(struct ffa_device *dev)
642 {
643 	if (ffa_device_is_valid(dev))
644 		return &ffa_ops;
645 
646 	return NULL;
647 }
648 EXPORT_SYMBOL_GPL(ffa_dev_ops_get);
649 
650 void ffa_device_match_uuid(struct ffa_device *ffa_dev, const uuid_t *uuid)
651 {
652 	int count, idx;
653 	struct ffa_partition_info *pbuf, *tpbuf;
654 
655 	count = ffa_partition_probe(uuid, &pbuf);
656 	if (count <= 0)
657 		return;
658 
659 	for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++)
660 		if (tpbuf->id == ffa_dev->vm_id)
661 			uuid_copy(&ffa_dev->uuid, uuid);
662 	kfree(pbuf);
663 }
664 
665 static void ffa_setup_partitions(void)
666 {
667 	int count, idx;
668 	struct ffa_device *ffa_dev;
669 	struct ffa_partition_info *pbuf, *tpbuf;
670 
671 	count = ffa_partition_probe(&uuid_null, &pbuf);
672 	if (count <= 0) {
673 		pr_info("%s: No partitions found, error %d\n", __func__, count);
674 		return;
675 	}
676 
677 	for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++) {
678 		/* Note that the &uuid_null parameter will require
679 		 * ffa_device_match() to find the UUID of this partition id
680 		 * with help of ffa_device_match_uuid(). Once the FF-A spec
681 		 * is updated to provide correct UUID here for each partition
682 		 * as part of the discovery API, we need to pass the
683 		 * discovered UUID here instead.
684 		 */
685 		ffa_dev = ffa_device_register(&uuid_null, tpbuf->id);
686 		if (!ffa_dev) {
687 			pr_err("%s: failed to register partition ID 0x%x\n",
688 			       __func__, tpbuf->id);
689 			continue;
690 		}
691 
692 		ffa_dev_set_drvdata(ffa_dev, drv_info);
693 	}
694 	kfree(pbuf);
695 }
696 
697 static int __init ffa_init(void)
698 {
699 	int ret;
700 
701 	ret = ffa_transport_init(&invoke_ffa_fn);
702 	if (ret)
703 		return ret;
704 
705 	ret = arm_ffa_bus_init();
706 	if (ret)
707 		return ret;
708 
709 	drv_info = kzalloc(sizeof(*drv_info), GFP_KERNEL);
710 	if (!drv_info) {
711 		ret = -ENOMEM;
712 		goto ffa_bus_exit;
713 	}
714 
715 	ret = ffa_version_check(&drv_info->version);
716 	if (ret)
717 		goto free_drv_info;
718 
719 	if (ffa_id_get(&drv_info->vm_id)) {
720 		pr_err("failed to obtain VM id for self\n");
721 		ret = -ENODEV;
722 		goto free_drv_info;
723 	}
724 
725 	drv_info->rx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
726 	if (!drv_info->rx_buffer) {
727 		ret = -ENOMEM;
728 		goto free_pages;
729 	}
730 
731 	drv_info->tx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
732 	if (!drv_info->tx_buffer) {
733 		ret = -ENOMEM;
734 		goto free_pages;
735 	}
736 
737 	ret = ffa_rxtx_map(virt_to_phys(drv_info->tx_buffer),
738 			   virt_to_phys(drv_info->rx_buffer),
739 			   RXTX_BUFFER_SIZE / FFA_PAGE_SIZE);
740 	if (ret) {
741 		pr_err("failed to register FFA RxTx buffers\n");
742 		goto free_pages;
743 	}
744 
745 	mutex_init(&drv_info->rx_lock);
746 	mutex_init(&drv_info->tx_lock);
747 
748 	ffa_setup_partitions();
749 
750 	return 0;
751 free_pages:
752 	if (drv_info->tx_buffer)
753 		free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE);
754 	free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE);
755 free_drv_info:
756 	kfree(drv_info);
757 ffa_bus_exit:
758 	arm_ffa_bus_exit();
759 	return ret;
760 }
761 subsys_initcall(ffa_init);
762 
763 static void __exit ffa_exit(void)
764 {
765 	ffa_rxtx_unmap(drv_info->vm_id);
766 	free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE);
767 	free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE);
768 	kfree(drv_info);
769 	arm_ffa_bus_exit();
770 }
771 module_exit(ffa_exit);
772 
773 MODULE_ALIAS("arm-ffa");
774 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
775 MODULE_DESCRIPTION("Arm FF-A interface driver");
776 MODULE_LICENSE("GPL v2");
777