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 		if (handle)
403 			*handle = PACK_HANDLE(ret.a2, ret.a3);
404 	} else if (ret.a0 == FFA_MEM_FRAG_RX) {
405 		if (handle)
406 			*handle = PACK_HANDLE(ret.a1, ret.a2);
407 	} else {
408 		return -EOPNOTSUPP;
409 	}
410 
411 	return frag_len;
412 }
413 
414 static int ffa_mem_next_frag(u64 handle, u32 frag_len)
415 {
416 	ffa_value_t ret;
417 
418 	invoke_ffa_fn((ffa_value_t){
419 		      .a0 = FFA_MEM_FRAG_TX,
420 		      .a1 = HANDLE_LOW(handle), .a2 = HANDLE_HIGH(handle),
421 		      .a3 = frag_len,
422 		      }, &ret);
423 
424 	while (ret.a0 == FFA_MEM_OP_PAUSE)
425 		invoke_ffa_fn((ffa_value_t){
426 			      .a0 = FFA_MEM_OP_RESUME,
427 			      .a1 = ret.a1, .a2 = ret.a2,
428 			      }, &ret);
429 
430 	if (ret.a0 == FFA_ERROR)
431 		return ffa_to_linux_errno((int)ret.a2);
432 
433 	if (ret.a0 == FFA_MEM_FRAG_RX)
434 		return ret.a3;
435 	else if (ret.a0 == FFA_SUCCESS)
436 		return 0;
437 
438 	return -EOPNOTSUPP;
439 }
440 
441 static int
442 ffa_transmit_fragment(u32 func_id, phys_addr_t buf, u32 buf_sz, u32 frag_len,
443 		      u32 len, u64 *handle, bool first)
444 {
445 	if (!first)
446 		return ffa_mem_next_frag(*handle, frag_len);
447 
448 	return ffa_mem_first_frag(func_id, buf, buf_sz, frag_len, len, handle);
449 }
450 
451 static u32 ffa_get_num_pages_sg(struct scatterlist *sg)
452 {
453 	u32 num_pages = 0;
454 
455 	do {
456 		num_pages += sg->length / FFA_PAGE_SIZE;
457 	} while ((sg = sg_next(sg)));
458 
459 	return num_pages;
460 }
461 
462 static int
463 ffa_setup_and_transmit(u32 func_id, void *buffer, u32 max_fragsize,
464 		       struct ffa_mem_ops_args *args)
465 {
466 	int rc = 0;
467 	bool first = true;
468 	phys_addr_t addr = 0;
469 	struct ffa_composite_mem_region *composite;
470 	struct ffa_mem_region_addr_range *constituents;
471 	struct ffa_mem_region_attributes *ep_mem_access;
472 	struct ffa_mem_region *mem_region = buffer;
473 	u32 idx, frag_len, length, buf_sz = 0, num_entries = sg_nents(args->sg);
474 
475 	mem_region->tag = args->tag;
476 	mem_region->flags = args->flags;
477 	mem_region->sender_id = drv_info->vm_id;
478 	mem_region->attributes = FFA_MEM_NORMAL | FFA_MEM_WRITE_BACK |
479 				 FFA_MEM_INNER_SHAREABLE;
480 	ep_mem_access = &mem_region->ep_mem_access[0];
481 
482 	for (idx = 0; idx < args->nattrs; idx++, ep_mem_access++) {
483 		ep_mem_access->receiver = args->attrs[idx].receiver;
484 		ep_mem_access->attrs = args->attrs[idx].attrs;
485 		ep_mem_access->composite_off = COMPOSITE_OFFSET(args->nattrs);
486 	}
487 	mem_region->ep_count = args->nattrs;
488 
489 	composite = buffer + COMPOSITE_OFFSET(args->nattrs);
490 	composite->total_pg_cnt = ffa_get_num_pages_sg(args->sg);
491 	composite->addr_range_cnt = num_entries;
492 
493 	length = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, num_entries);
494 	frag_len = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, 0);
495 	if (frag_len > max_fragsize)
496 		return -ENXIO;
497 
498 	if (!args->use_txbuf) {
499 		addr = virt_to_phys(buffer);
500 		buf_sz = max_fragsize / FFA_PAGE_SIZE;
501 	}
502 
503 	constituents = buffer + frag_len;
504 	idx = 0;
505 	do {
506 		if (frag_len == max_fragsize) {
507 			rc = ffa_transmit_fragment(func_id, addr, buf_sz,
508 						   frag_len, length,
509 						   &args->g_handle, first);
510 			if (rc < 0)
511 				return -ENXIO;
512 
513 			first = false;
514 			idx = 0;
515 			frag_len = 0;
516 			constituents = buffer;
517 		}
518 
519 		if ((void *)constituents - buffer > max_fragsize) {
520 			pr_err("Memory Region Fragment > Tx Buffer size\n");
521 			return -EFAULT;
522 		}
523 
524 		constituents->address = sg_phys(args->sg);
525 		constituents->pg_cnt = args->sg->length / FFA_PAGE_SIZE;
526 		constituents++;
527 		frag_len += sizeof(struct ffa_mem_region_addr_range);
528 	} while ((args->sg = sg_next(args->sg)));
529 
530 	return ffa_transmit_fragment(func_id, addr, buf_sz, frag_len,
531 				     length, &args->g_handle, first);
532 }
533 
534 static int ffa_memory_ops(u32 func_id, struct ffa_mem_ops_args *args)
535 {
536 	int ret;
537 	void *buffer;
538 
539 	if (!args->use_txbuf) {
540 		buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
541 		if (!buffer)
542 			return -ENOMEM;
543 	} else {
544 		buffer = drv_info->tx_buffer;
545 		mutex_lock(&drv_info->tx_lock);
546 	}
547 
548 	ret = ffa_setup_and_transmit(func_id, buffer, RXTX_BUFFER_SIZE, args);
549 
550 	if (args->use_txbuf)
551 		mutex_unlock(&drv_info->tx_lock);
552 	else
553 		free_pages_exact(buffer, RXTX_BUFFER_SIZE);
554 
555 	return ret < 0 ? ret : 0;
556 }
557 
558 static int ffa_memory_reclaim(u64 g_handle, u32 flags)
559 {
560 	ffa_value_t ret;
561 
562 	invoke_ffa_fn((ffa_value_t){
563 		      .a0 = FFA_MEM_RECLAIM,
564 		      .a1 = HANDLE_LOW(g_handle), .a2 = HANDLE_HIGH(g_handle),
565 		      .a3 = flags,
566 		      }, &ret);
567 
568 	if (ret.a0 == FFA_ERROR)
569 		return ffa_to_linux_errno((int)ret.a2);
570 
571 	return 0;
572 }
573 
574 static u32 ffa_api_version_get(void)
575 {
576 	return drv_info->version;
577 }
578 
579 static int ffa_partition_info_get(const char *uuid_str,
580 				  struct ffa_partition_info *buffer)
581 {
582 	int count;
583 	uuid_t uuid;
584 	struct ffa_partition_info *pbuf;
585 
586 	if (uuid_parse(uuid_str, &uuid)) {
587 		pr_err("invalid uuid (%s)\n", uuid_str);
588 		return -ENODEV;
589 	}
590 
591 	count = ffa_partition_probe(&uuid, &pbuf);
592 	if (count <= 0)
593 		return -ENOENT;
594 
595 	memcpy(buffer, pbuf, sizeof(*pbuf) * count);
596 	kfree(pbuf);
597 	return 0;
598 }
599 
600 static void ffa_mode_32bit_set(struct ffa_device *dev)
601 {
602 	dev->mode_32bit = true;
603 }
604 
605 static int ffa_sync_send_receive(struct ffa_device *dev,
606 				 struct ffa_send_direct_data *data)
607 {
608 	return ffa_msg_send_direct_req(drv_info->vm_id, dev->vm_id,
609 				       dev->mode_32bit, data);
610 }
611 
612 static int
613 ffa_memory_share(struct ffa_device *dev, struct ffa_mem_ops_args *args)
614 {
615 	if (dev->mode_32bit)
616 		return ffa_memory_ops(FFA_MEM_SHARE, args);
617 
618 	return ffa_memory_ops(FFA_FN_NATIVE(MEM_SHARE), args);
619 }
620 
621 static int
622 ffa_memory_lend(struct ffa_device *dev, struct ffa_mem_ops_args *args)
623 {
624 	/* Note that upon a successful MEM_LEND request the caller
625 	 * must ensure that the memory region specified is not accessed
626 	 * until a successful MEM_RECALIM call has been made.
627 	 * On systems with a hypervisor present this will been enforced,
628 	 * however on systems without a hypervisor the responsibility
629 	 * falls to the calling kernel driver to prevent access.
630 	 */
631 	if (dev->mode_32bit)
632 		return ffa_memory_ops(FFA_MEM_LEND, args);
633 
634 	return ffa_memory_ops(FFA_FN_NATIVE(MEM_LEND), args);
635 }
636 
637 static const struct ffa_dev_ops ffa_ops = {
638 	.api_version_get = ffa_api_version_get,
639 	.partition_info_get = ffa_partition_info_get,
640 	.mode_32bit_set = ffa_mode_32bit_set,
641 	.sync_send_receive = ffa_sync_send_receive,
642 	.memory_reclaim = ffa_memory_reclaim,
643 	.memory_share = ffa_memory_share,
644 	.memory_lend = ffa_memory_lend,
645 };
646 
647 const struct ffa_dev_ops *ffa_dev_ops_get(struct ffa_device *dev)
648 {
649 	if (ffa_device_is_valid(dev))
650 		return &ffa_ops;
651 
652 	return NULL;
653 }
654 EXPORT_SYMBOL_GPL(ffa_dev_ops_get);
655 
656 void ffa_device_match_uuid(struct ffa_device *ffa_dev, const uuid_t *uuid)
657 {
658 	int count, idx;
659 	struct ffa_partition_info *pbuf, *tpbuf;
660 
661 	count = ffa_partition_probe(uuid, &pbuf);
662 	if (count <= 0)
663 		return;
664 
665 	for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++)
666 		if (tpbuf->id == ffa_dev->vm_id)
667 			uuid_copy(&ffa_dev->uuid, uuid);
668 	kfree(pbuf);
669 }
670 
671 static void ffa_setup_partitions(void)
672 {
673 	int count, idx;
674 	struct ffa_device *ffa_dev;
675 	struct ffa_partition_info *pbuf, *tpbuf;
676 
677 	count = ffa_partition_probe(&uuid_null, &pbuf);
678 	if (count <= 0) {
679 		pr_info("%s: No partitions found, error %d\n", __func__, count);
680 		return;
681 	}
682 
683 	for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++) {
684 		/* Note that the &uuid_null parameter will require
685 		 * ffa_device_match() to find the UUID of this partition id
686 		 * with help of ffa_device_match_uuid(). Once the FF-A spec
687 		 * is updated to provide correct UUID here for each partition
688 		 * as part of the discovery API, we need to pass the
689 		 * discovered UUID here instead.
690 		 */
691 		ffa_dev = ffa_device_register(&uuid_null, tpbuf->id);
692 		if (!ffa_dev) {
693 			pr_err("%s: failed to register partition ID 0x%x\n",
694 			       __func__, tpbuf->id);
695 			continue;
696 		}
697 	}
698 	kfree(pbuf);
699 }
700 
701 static int __init ffa_init(void)
702 {
703 	int ret;
704 
705 	ret = ffa_transport_init(&invoke_ffa_fn);
706 	if (ret)
707 		return ret;
708 
709 	ret = arm_ffa_bus_init();
710 	if (ret)
711 		return ret;
712 
713 	drv_info = kzalloc(sizeof(*drv_info), GFP_KERNEL);
714 	if (!drv_info) {
715 		ret = -ENOMEM;
716 		goto ffa_bus_exit;
717 	}
718 
719 	ret = ffa_version_check(&drv_info->version);
720 	if (ret)
721 		goto free_drv_info;
722 
723 	if (ffa_id_get(&drv_info->vm_id)) {
724 		pr_err("failed to obtain VM id for self\n");
725 		ret = -ENODEV;
726 		goto free_drv_info;
727 	}
728 
729 	drv_info->rx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
730 	if (!drv_info->rx_buffer) {
731 		ret = -ENOMEM;
732 		goto free_pages;
733 	}
734 
735 	drv_info->tx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
736 	if (!drv_info->tx_buffer) {
737 		ret = -ENOMEM;
738 		goto free_pages;
739 	}
740 
741 	ret = ffa_rxtx_map(virt_to_phys(drv_info->tx_buffer),
742 			   virt_to_phys(drv_info->rx_buffer),
743 			   RXTX_BUFFER_SIZE / FFA_PAGE_SIZE);
744 	if (ret) {
745 		pr_err("failed to register FFA RxTx buffers\n");
746 		goto free_pages;
747 	}
748 
749 	mutex_init(&drv_info->rx_lock);
750 	mutex_init(&drv_info->tx_lock);
751 
752 	ffa_setup_partitions();
753 
754 	return 0;
755 free_pages:
756 	if (drv_info->tx_buffer)
757 		free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE);
758 	free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE);
759 free_drv_info:
760 	kfree(drv_info);
761 ffa_bus_exit:
762 	arm_ffa_bus_exit();
763 	return ret;
764 }
765 subsys_initcall(ffa_init);
766 
767 static void __exit ffa_exit(void)
768 {
769 	ffa_rxtx_unmap(drv_info->vm_id);
770 	free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE);
771 	free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE);
772 	kfree(drv_info);
773 	arm_ffa_bus_exit();
774 }
775 module_exit(ffa_exit);
776 
777 MODULE_ALIAS("arm-ffa");
778 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
779 MODULE_DESCRIPTION("Arm FF-A interface driver");
780 MODULE_LICENSE("GPL v2");
781