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