• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define FFA_MIN_VERSION		FFA_VERSION_1_0
40 
41 #define SENDER_ID_MASK		GENMASK(31, 16)
42 #define RECEIVER_ID_MASK	GENMASK(15, 0)
43 #define SENDER_ID(x)		((u16)(FIELD_GET(SENDER_ID_MASK, (x))))
44 #define RECEIVER_ID(x)		((u16)(FIELD_GET(RECEIVER_ID_MASK, (x))))
45 #define PACK_TARGET_INFO(s, r)		\
46 	(FIELD_PREP(SENDER_ID_MASK, (s)) | FIELD_PREP(RECEIVER_ID_MASK, (r)))
47 
48 /*
49  * Keeping RX TX buffer size as 4K for now
50  * 64K may be preferred to keep it min a page in 64K PAGE_SIZE config
51  */
52 #define RXTX_BUFFER_SIZE	SZ_4K
53 
54 static ffa_fn *invoke_ffa_fn;
55 
56 static const int ffa_linux_errmap[] = {
57 	/* better than switch case as long as return value is continuous */
58 	0,		/* FFA_RET_SUCCESS */
59 	-EOPNOTSUPP,	/* FFA_RET_NOT_SUPPORTED */
60 	-EINVAL,	/* FFA_RET_INVALID_PARAMETERS */
61 	-ENOMEM,	/* FFA_RET_NO_MEMORY */
62 	-EBUSY,		/* FFA_RET_BUSY */
63 	-EINTR,		/* FFA_RET_INTERRUPTED */
64 	-EACCES,	/* FFA_RET_DENIED */
65 	-EAGAIN,	/* FFA_RET_RETRY */
66 	-ECANCELED,	/* FFA_RET_ABORTED */
67 };
68 
ffa_to_linux_errno(int errno)69 static inline int ffa_to_linux_errno(int errno)
70 {
71 	int err_idx = -errno;
72 
73 	if (err_idx >= 0 && err_idx < ARRAY_SIZE(ffa_linux_errmap))
74 		return ffa_linux_errmap[err_idx];
75 	return -EINVAL;
76 }
77 
78 struct ffa_drv_info {
79 	u32 version;
80 	u16 vm_id;
81 	struct mutex rx_lock; /* lock to protect Rx buffer */
82 	struct mutex tx_lock; /* lock to protect Tx buffer */
83 	void *rx_buffer;
84 	void *tx_buffer;
85 };
86 
87 static struct ffa_drv_info *drv_info;
88 
89 /*
90  * The driver must be able to support all the versions from the earliest
91  * supported FFA_MIN_VERSION to the latest supported FFA_DRIVER_VERSION.
92  * The specification states that if firmware supports a FFA implementation
93  * that is incompatible with and at a greater version number than specified
94  * by the caller(FFA_DRIVER_VERSION passed as parameter to FFA_VERSION),
95  * it must return the NOT_SUPPORTED error code.
96  */
ffa_compatible_version_find(u32 version)97 static u32 ffa_compatible_version_find(u32 version)
98 {
99 	u16 major = FFA_MAJOR_VERSION(version), minor = FFA_MINOR_VERSION(version);
100 	u16 drv_major = FFA_MAJOR_VERSION(FFA_DRIVER_VERSION);
101 	u16 drv_minor = FFA_MINOR_VERSION(FFA_DRIVER_VERSION);
102 
103 	if ((major < drv_major) || (major == drv_major && minor <= drv_minor))
104 		return version;
105 
106 	pr_info("Firmware version higher than driver version, downgrading\n");
107 	return FFA_DRIVER_VERSION;
108 }
109 
ffa_version_check(u32 * version)110 static int ffa_version_check(u32 *version)
111 {
112 	ffa_value_t ver;
113 
114 	invoke_ffa_fn((ffa_value_t){
115 		      .a0 = FFA_VERSION, .a1 = FFA_DRIVER_VERSION,
116 		      }, &ver);
117 
118 	if (ver.a0 == FFA_RET_NOT_SUPPORTED) {
119 		pr_info("FFA_VERSION returned not supported\n");
120 		return -EOPNOTSUPP;
121 	}
122 
123 	if (ver.a0 < FFA_MIN_VERSION) {
124 		pr_err("Incompatible v%d.%d! Earliest supported v%d.%d\n",
125 		       FFA_MAJOR_VERSION(ver.a0), FFA_MINOR_VERSION(ver.a0),
126 		       FFA_MAJOR_VERSION(FFA_MIN_VERSION),
127 		       FFA_MINOR_VERSION(FFA_MIN_VERSION));
128 		return -EINVAL;
129 	}
130 
131 	pr_info("Driver version %d.%d\n", FFA_MAJOR_VERSION(FFA_DRIVER_VERSION),
132 		FFA_MINOR_VERSION(FFA_DRIVER_VERSION));
133 	pr_info("Firmware version %d.%d found\n", FFA_MAJOR_VERSION(ver.a0),
134 		FFA_MINOR_VERSION(ver.a0));
135 	*version = ffa_compatible_version_find(ver.a0);
136 
137 	return 0;
138 }
139 
ffa_rx_release(void)140 static int ffa_rx_release(void)
141 {
142 	ffa_value_t ret;
143 
144 	invoke_ffa_fn((ffa_value_t){
145 		      .a0 = FFA_RX_RELEASE,
146 		      }, &ret);
147 
148 	if (ret.a0 == FFA_ERROR)
149 		return ffa_to_linux_errno((int)ret.a2);
150 
151 	/* check for ret.a0 == FFA_RX_RELEASE ? */
152 
153 	return 0;
154 }
155 
ffa_rxtx_map(phys_addr_t tx_buf,phys_addr_t rx_buf,u32 pg_cnt)156 static int ffa_rxtx_map(phys_addr_t tx_buf, phys_addr_t rx_buf, u32 pg_cnt)
157 {
158 	ffa_value_t ret;
159 
160 	invoke_ffa_fn((ffa_value_t){
161 		      .a0 = FFA_FN_NATIVE(RXTX_MAP),
162 		      .a1 = tx_buf, .a2 = rx_buf, .a3 = pg_cnt,
163 		      }, &ret);
164 
165 	if (ret.a0 == FFA_ERROR)
166 		return ffa_to_linux_errno((int)ret.a2);
167 
168 	return 0;
169 }
170 
ffa_rxtx_unmap(u16 vm_id)171 static int ffa_rxtx_unmap(u16 vm_id)
172 {
173 	ffa_value_t ret;
174 
175 	invoke_ffa_fn((ffa_value_t){
176 		      .a0 = FFA_RXTX_UNMAP, .a1 = PACK_TARGET_INFO(vm_id, 0),
177 		      }, &ret);
178 
179 	if (ret.a0 == FFA_ERROR)
180 		return ffa_to_linux_errno((int)ret.a2);
181 
182 	return 0;
183 }
184 
185 /* buffer must be sizeof(struct ffa_partition_info) * num_partitions */
186 static int
__ffa_partition_info_get(u32 uuid0,u32 uuid1,u32 uuid2,u32 uuid3,struct ffa_partition_info * buffer,int num_partitions)187 __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,
188 			 struct ffa_partition_info *buffer, int num_partitions)
189 {
190 	int count;
191 	ffa_value_t partition_info;
192 
193 	mutex_lock(&drv_info->rx_lock);
194 	invoke_ffa_fn((ffa_value_t){
195 		      .a0 = FFA_PARTITION_INFO_GET,
196 		      .a1 = uuid0, .a2 = uuid1, .a3 = uuid2, .a4 = uuid3,
197 		      }, &partition_info);
198 
199 	if (partition_info.a0 == FFA_ERROR) {
200 		mutex_unlock(&drv_info->rx_lock);
201 		return ffa_to_linux_errno((int)partition_info.a2);
202 	}
203 
204 	count = partition_info.a2;
205 
206 	if (buffer && count <= num_partitions)
207 		memcpy(buffer, drv_info->rx_buffer, sizeof(*buffer) * count);
208 
209 	ffa_rx_release();
210 
211 	mutex_unlock(&drv_info->rx_lock);
212 
213 	return count;
214 }
215 
216 /* buffer is allocated and caller must free the same if returned count > 0 */
217 static int
ffa_partition_probe(const uuid_t * uuid,struct ffa_partition_info ** buffer)218 ffa_partition_probe(const uuid_t *uuid, struct ffa_partition_info **buffer)
219 {
220 	int count;
221 	u32 uuid0_4[4];
222 	struct ffa_partition_info *pbuf;
223 
224 	export_uuid((u8 *)uuid0_4, uuid);
225 	count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1], uuid0_4[2],
226 					 uuid0_4[3], NULL, 0);
227 	if (count <= 0)
228 		return count;
229 
230 	pbuf = kcalloc(count, sizeof(*pbuf), GFP_KERNEL);
231 	if (!pbuf)
232 		return -ENOMEM;
233 
234 	count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1], uuid0_4[2],
235 					 uuid0_4[3], pbuf, count);
236 	if (count <= 0)
237 		kfree(pbuf);
238 	else
239 		*buffer = pbuf;
240 
241 	return count;
242 }
243 
244 #define VM_ID_MASK	GENMASK(15, 0)
ffa_id_get(u16 * vm_id)245 static int ffa_id_get(u16 *vm_id)
246 {
247 	ffa_value_t id;
248 
249 	invoke_ffa_fn((ffa_value_t){
250 		      .a0 = FFA_ID_GET,
251 		      }, &id);
252 
253 	if (id.a0 == FFA_ERROR)
254 		return ffa_to_linux_errno((int)id.a2);
255 
256 	*vm_id = FIELD_GET(VM_ID_MASK, (id.a2));
257 
258 	return 0;
259 }
260 
ffa_msg_send_direct_req(u16 src_id,u16 dst_id,bool mode_32bit,struct ffa_send_direct_data * data)261 static int ffa_msg_send_direct_req(u16 src_id, u16 dst_id, bool mode_32bit,
262 				   struct ffa_send_direct_data *data)
263 {
264 	u32 req_id, resp_id, src_dst_ids = PACK_TARGET_INFO(src_id, dst_id);
265 	ffa_value_t ret;
266 
267 	if (mode_32bit) {
268 		req_id = FFA_MSG_SEND_DIRECT_REQ;
269 		resp_id = FFA_MSG_SEND_DIRECT_RESP;
270 	} else {
271 		req_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_REQ);
272 		resp_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_RESP);
273 	}
274 
275 	invoke_ffa_fn((ffa_value_t){
276 		      .a0 = req_id, .a1 = src_dst_ids, .a2 = 0,
277 		      .a3 = data->data0, .a4 = data->data1, .a5 = data->data2,
278 		      .a6 = data->data3, .a7 = data->data4,
279 		      }, &ret);
280 
281 	while (ret.a0 == FFA_INTERRUPT)
282 		invoke_ffa_fn((ffa_value_t){
283 			      .a0 = FFA_RUN, .a1 = ret.a1,
284 			      }, &ret);
285 
286 	if (ret.a0 == FFA_ERROR)
287 		return ffa_to_linux_errno((int)ret.a2);
288 
289 	if (ret.a0 == resp_id) {
290 		data->data0 = ret.a3;
291 		data->data1 = ret.a4;
292 		data->data2 = ret.a5;
293 		data->data3 = ret.a6;
294 		data->data4 = ret.a7;
295 		return 0;
296 	}
297 
298 	return -EINVAL;
299 }
300 
ffa_mem_first_frag(u32 func_id,phys_addr_t buf,u32 buf_sz,u32 frag_len,u32 len,u64 * handle)301 static int ffa_mem_first_frag(u32 func_id, phys_addr_t buf, u32 buf_sz,
302 			      u32 frag_len, u32 len, u64 *handle)
303 {
304 	ffa_value_t ret;
305 
306 	invoke_ffa_fn((ffa_value_t){
307 		      .a0 = func_id, .a1 = len, .a2 = frag_len,
308 		      .a3 = buf, .a4 = buf_sz,
309 		      }, &ret);
310 
311 	while (ret.a0 == FFA_MEM_OP_PAUSE)
312 		invoke_ffa_fn((ffa_value_t){
313 			      .a0 = FFA_MEM_OP_RESUME,
314 			      .a1 = ret.a1, .a2 = ret.a2,
315 			      }, &ret);
316 
317 	if (ret.a0 == FFA_ERROR)
318 		return ffa_to_linux_errno((int)ret.a2);
319 
320 	if (ret.a0 != FFA_SUCCESS)
321 		return -EOPNOTSUPP;
322 
323 	if (handle)
324 		*handle = PACK_HANDLE(ret.a2, ret.a3);
325 
326 	return frag_len;
327 }
328 
ffa_mem_next_frag(u64 handle,u32 frag_len)329 static int ffa_mem_next_frag(u64 handle, u32 frag_len)
330 {
331 	ffa_value_t ret;
332 
333 	invoke_ffa_fn((ffa_value_t){
334 		      .a0 = FFA_MEM_FRAG_TX,
335 		      .a1 = HANDLE_LOW(handle), .a2 = HANDLE_HIGH(handle),
336 		      .a3 = frag_len,
337 		      }, &ret);
338 
339 	while (ret.a0 == FFA_MEM_OP_PAUSE)
340 		invoke_ffa_fn((ffa_value_t){
341 			      .a0 = FFA_MEM_OP_RESUME,
342 			      .a1 = ret.a1, .a2 = ret.a2,
343 			      }, &ret);
344 
345 	if (ret.a0 == FFA_ERROR)
346 		return ffa_to_linux_errno((int)ret.a2);
347 
348 	if (ret.a0 != FFA_MEM_FRAG_RX)
349 		return -EOPNOTSUPP;
350 
351 	return ret.a3;
352 }
353 
354 static int
ffa_transmit_fragment(u32 func_id,phys_addr_t buf,u32 buf_sz,u32 frag_len,u32 len,u64 * handle,bool first)355 ffa_transmit_fragment(u32 func_id, phys_addr_t buf, u32 buf_sz, u32 frag_len,
356 		      u32 len, u64 *handle, bool first)
357 {
358 	if (!first)
359 		return ffa_mem_next_frag(*handle, frag_len);
360 
361 	return ffa_mem_first_frag(func_id, buf, buf_sz, frag_len, len, handle);
362 }
363 
ffa_get_num_pages_sg(struct scatterlist * sg)364 static u32 ffa_get_num_pages_sg(struct scatterlist *sg)
365 {
366 	u32 num_pages = 0;
367 
368 	do {
369 		num_pages += sg->length / FFA_PAGE_SIZE;
370 	} while ((sg = sg_next(sg)));
371 
372 	return num_pages;
373 }
374 
375 static int
ffa_setup_and_transmit(u32 func_id,void * buffer,u32 max_fragsize,struct ffa_mem_ops_args * args)376 ffa_setup_and_transmit(u32 func_id, void *buffer, u32 max_fragsize,
377 		       struct ffa_mem_ops_args *args)
378 {
379 	int rc = 0;
380 	bool first = true;
381 	phys_addr_t addr = 0;
382 	struct ffa_composite_mem_region *composite;
383 	struct ffa_mem_region_addr_range *constituents;
384 	struct ffa_mem_region_attributes *ep_mem_access;
385 	struct ffa_mem_region *mem_region = buffer;
386 	u32 idx, frag_len, length, buf_sz = 0, num_entries = sg_nents(args->sg);
387 
388 	mem_region->tag = args->tag;
389 	mem_region->flags = args->flags;
390 	mem_region->sender_id = drv_info->vm_id;
391 	mem_region->attributes = FFA_MEM_NORMAL | FFA_MEM_WRITE_BACK |
392 				 FFA_MEM_INNER_SHAREABLE;
393 	ep_mem_access = &mem_region->ep_mem_access[0];
394 
395 	for (idx = 0; idx < args->nattrs; idx++, ep_mem_access++) {
396 		ep_mem_access->receiver = args->attrs[idx].receiver;
397 		ep_mem_access->attrs = args->attrs[idx].attrs;
398 		ep_mem_access->composite_off = COMPOSITE_OFFSET(args->nattrs);
399 		ep_mem_access->flag = 0;
400 		ep_mem_access->reserved = 0;
401 	}
402 	mem_region->handle = 0;
403 	mem_region->reserved_0 = 0;
404 	mem_region->reserved_1 = 0;
405 	mem_region->ep_count = args->nattrs;
406 
407 	composite = buffer + COMPOSITE_OFFSET(args->nattrs);
408 	composite->total_pg_cnt = ffa_get_num_pages_sg(args->sg);
409 	composite->addr_range_cnt = num_entries;
410 	composite->reserved = 0;
411 
412 	length = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, num_entries);
413 	frag_len = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, 0);
414 	if (frag_len > max_fragsize)
415 		return -ENXIO;
416 
417 	if (!args->use_txbuf) {
418 		addr = virt_to_phys(buffer);
419 		buf_sz = max_fragsize / FFA_PAGE_SIZE;
420 	}
421 
422 	constituents = buffer + frag_len;
423 	idx = 0;
424 	do {
425 		if (frag_len == max_fragsize) {
426 			rc = ffa_transmit_fragment(func_id, addr, buf_sz,
427 						   frag_len, length,
428 						   &args->g_handle, first);
429 			if (rc < 0)
430 				return -ENXIO;
431 
432 			first = false;
433 			idx = 0;
434 			frag_len = 0;
435 			constituents = buffer;
436 		}
437 
438 		if ((void *)constituents - buffer > max_fragsize) {
439 			pr_err("Memory Region Fragment > Tx Buffer size\n");
440 			return -EFAULT;
441 		}
442 
443 		constituents->address = sg_phys(args->sg);
444 		constituents->pg_cnt = args->sg->length / FFA_PAGE_SIZE;
445 		constituents->reserved = 0;
446 		constituents++;
447 		frag_len += sizeof(struct ffa_mem_region_addr_range);
448 	} while ((args->sg = sg_next(args->sg)));
449 
450 	return ffa_transmit_fragment(func_id, addr, buf_sz, frag_len,
451 				     length, &args->g_handle, first);
452 }
453 
ffa_memory_ops(u32 func_id,struct ffa_mem_ops_args * args)454 static int ffa_memory_ops(u32 func_id, struct ffa_mem_ops_args *args)
455 {
456 	int ret;
457 	void *buffer;
458 
459 	if (!args->use_txbuf) {
460 		buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
461 		if (!buffer)
462 			return -ENOMEM;
463 	} else {
464 		buffer = drv_info->tx_buffer;
465 		mutex_lock(&drv_info->tx_lock);
466 	}
467 
468 	ret = ffa_setup_and_transmit(func_id, buffer, RXTX_BUFFER_SIZE, args);
469 
470 	if (args->use_txbuf)
471 		mutex_unlock(&drv_info->tx_lock);
472 	else
473 		free_pages_exact(buffer, RXTX_BUFFER_SIZE);
474 
475 	return ret < 0 ? ret : 0;
476 }
477 
ffa_memory_reclaim(u64 g_handle,u32 flags)478 static int ffa_memory_reclaim(u64 g_handle, u32 flags)
479 {
480 	ffa_value_t ret;
481 
482 	invoke_ffa_fn((ffa_value_t){
483 		      .a0 = FFA_MEM_RECLAIM,
484 		      .a1 = HANDLE_LOW(g_handle), .a2 = HANDLE_HIGH(g_handle),
485 		      .a3 = flags,
486 		      }, &ret);
487 
488 	if (ret.a0 == FFA_ERROR)
489 		return ffa_to_linux_errno((int)ret.a2);
490 
491 	return 0;
492 }
493 
ffa_api_version_get(void)494 static u32 ffa_api_version_get(void)
495 {
496 	return drv_info->version;
497 }
498 
ffa_partition_info_get(const char * uuid_str,struct ffa_partition_info * buffer)499 static int ffa_partition_info_get(const char *uuid_str,
500 				  struct ffa_partition_info *buffer)
501 {
502 	int count;
503 	uuid_t uuid;
504 	struct ffa_partition_info *pbuf;
505 
506 	if (uuid_parse(uuid_str, &uuid)) {
507 		pr_err("invalid uuid (%s)\n", uuid_str);
508 		return -ENODEV;
509 	}
510 
511 	count = ffa_partition_probe(&uuid, &pbuf);
512 	if (count <= 0)
513 		return -ENOENT;
514 
515 	memcpy(buffer, pbuf, sizeof(*pbuf) * count);
516 	kfree(pbuf);
517 	return 0;
518 }
519 
ffa_mode_32bit_set(struct ffa_device * dev)520 static void ffa_mode_32bit_set(struct ffa_device *dev)
521 {
522 	dev->mode_32bit = true;
523 }
524 
ffa_sync_send_receive(struct ffa_device * dev,struct ffa_send_direct_data * data)525 static int ffa_sync_send_receive(struct ffa_device *dev,
526 				 struct ffa_send_direct_data *data)
527 {
528 	return ffa_msg_send_direct_req(drv_info->vm_id, dev->vm_id,
529 				       dev->mode_32bit, data);
530 }
531 
532 static int
ffa_memory_share(struct ffa_device * dev,struct ffa_mem_ops_args * args)533 ffa_memory_share(struct ffa_device *dev, struct ffa_mem_ops_args *args)
534 {
535 	if (dev->mode_32bit)
536 		return ffa_memory_ops(FFA_MEM_SHARE, args);
537 
538 	return ffa_memory_ops(FFA_FN_NATIVE(MEM_SHARE), args);
539 }
540 
541 static int
ffa_memory_lend(struct ffa_device * dev,struct ffa_mem_ops_args * args)542 ffa_memory_lend(struct ffa_device *dev, struct ffa_mem_ops_args *args)
543 {
544 	/* Note that upon a successful MEM_LEND request the caller
545 	 * must ensure that the memory region specified is not accessed
546 	 * until a successful MEM_RECALIM call has been made.
547 	 * On systems with a hypervisor present this will been enforced,
548 	 * however on systems without a hypervisor the responsibility
549 	 * falls to the calling kernel driver to prevent access.
550 	 */
551 	if (dev->mode_32bit)
552 		return ffa_memory_ops(FFA_MEM_LEND, args);
553 
554 	return ffa_memory_ops(FFA_FN_NATIVE(MEM_LEND), args);
555 }
556 
557 static const struct ffa_dev_ops ffa_ops = {
558 	.api_version_get = ffa_api_version_get,
559 	.partition_info_get = ffa_partition_info_get,
560 	.mode_32bit_set = ffa_mode_32bit_set,
561 	.sync_send_receive = ffa_sync_send_receive,
562 	.memory_reclaim = ffa_memory_reclaim,
563 	.memory_share = ffa_memory_share,
564 	.memory_lend = ffa_memory_lend,
565 };
566 
ffa_dev_ops_get(struct ffa_device * dev)567 const struct ffa_dev_ops *ffa_dev_ops_get(struct ffa_device *dev)
568 {
569 	if (ffa_device_is_valid(dev))
570 		return &ffa_ops;
571 
572 	return NULL;
573 }
574 EXPORT_SYMBOL_GPL(ffa_dev_ops_get);
575 
ffa_device_match_uuid(struct ffa_device * ffa_dev,const uuid_t * uuid)576 void ffa_device_match_uuid(struct ffa_device *ffa_dev, const uuid_t *uuid)
577 {
578 	int count, idx;
579 	struct ffa_partition_info *pbuf, *tpbuf;
580 
581 	count = ffa_partition_probe(uuid, &pbuf);
582 	if (count <= 0)
583 		return;
584 
585 	for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++)
586 		if (tpbuf->id == ffa_dev->vm_id)
587 			uuid_copy(&ffa_dev->uuid, uuid);
588 	kfree(pbuf);
589 }
590 
ffa_setup_partitions(void)591 static void ffa_setup_partitions(void)
592 {
593 	int count, idx;
594 	struct ffa_device *ffa_dev;
595 	struct ffa_partition_info *pbuf, *tpbuf;
596 
597 	count = ffa_partition_probe(&uuid_null, &pbuf);
598 	if (count <= 0) {
599 		pr_info("%s: No partitions found, error %d\n", __func__, count);
600 		return;
601 	}
602 
603 	for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++) {
604 		/* Note that the &uuid_null parameter will require
605 		 * ffa_device_match() to find the UUID of this partition id
606 		 * with help of ffa_device_match_uuid(). Once the FF-A spec
607 		 * is updated to provide correct UUID here for each partition
608 		 * as part of the discovery API, we need to pass the
609 		 * discovered UUID here instead.
610 		 */
611 		ffa_dev = ffa_device_register(&uuid_null, tpbuf->id);
612 		if (!ffa_dev) {
613 			pr_err("%s: failed to register partition ID 0x%x\n",
614 			       __func__, tpbuf->id);
615 			continue;
616 		}
617 	}
618 	kfree(pbuf);
619 }
620 
ffa_init(void)621 static int __init ffa_init(void)
622 {
623 	int ret;
624 
625 	ret = ffa_transport_init(&invoke_ffa_fn);
626 	if (ret)
627 		return ret;
628 
629 	ret = arm_ffa_bus_init();
630 	if (ret)
631 		return ret;
632 
633 	drv_info = kzalloc(sizeof(*drv_info), GFP_KERNEL);
634 	if (!drv_info) {
635 		ret = -ENOMEM;
636 		goto ffa_bus_exit;
637 	}
638 
639 	ret = ffa_version_check(&drv_info->version);
640 	if (ret)
641 		goto free_drv_info;
642 
643 	if (ffa_id_get(&drv_info->vm_id)) {
644 		pr_err("failed to obtain VM id for self\n");
645 		ret = -ENODEV;
646 		goto free_drv_info;
647 	}
648 
649 	drv_info->rx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
650 	if (!drv_info->rx_buffer) {
651 		ret = -ENOMEM;
652 		goto free_pages;
653 	}
654 
655 	drv_info->tx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL);
656 	if (!drv_info->tx_buffer) {
657 		ret = -ENOMEM;
658 		goto free_pages;
659 	}
660 
661 	ret = ffa_rxtx_map(virt_to_phys(drv_info->tx_buffer),
662 			   virt_to_phys(drv_info->rx_buffer),
663 			   RXTX_BUFFER_SIZE / FFA_PAGE_SIZE);
664 	if (ret) {
665 		pr_err("failed to register FFA RxTx buffers\n");
666 		goto free_pages;
667 	}
668 
669 	mutex_init(&drv_info->rx_lock);
670 	mutex_init(&drv_info->tx_lock);
671 
672 	ffa_setup_partitions();
673 
674 	return 0;
675 free_pages:
676 	if (drv_info->tx_buffer)
677 		free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE);
678 	free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE);
679 free_drv_info:
680 	kfree(drv_info);
681 ffa_bus_exit:
682 	arm_ffa_bus_exit();
683 	return ret;
684 }
685 subsys_initcall(ffa_init);
686 
ffa_exit(void)687 static void __exit ffa_exit(void)
688 {
689 	ffa_rxtx_unmap(drv_info->vm_id);
690 	free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE);
691 	free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE);
692 	kfree(drv_info);
693 	arm_ffa_bus_exit();
694 }
695 module_exit(ffa_exit);
696 
697 MODULE_ALIAS("arm-ffa");
698 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
699 MODULE_DESCRIPTION("Arm FF-A interface driver");
700 MODULE_LICENSE("GPL v2");
701