xref: /linux/drivers/firmware/arm_ffa/driver.c (revision 07b760e713255a2224cfaad62eeaae85de913bac)
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/acpi.h>
26 #include <linux/arm_ffa.h>
27 #include <linux/bitfield.h>
28 #include <linux/cpuhotplug.h>
29 #include <linux/delay.h>
30 #include <linux/device.h>
31 #include <linux/hashtable.h>
32 #include <linux/interrupt.h>
33 #include <linux/io.h>
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/mm.h>
37 #include <linux/mutex.h>
38 #include <linux/of_irq.h>
39 #include <linux/scatterlist.h>
40 #include <linux/slab.h>
41 #include <linux/smp.h>
42 #include <linux/uuid.h>
43 #include <linux/xarray.h>
44 
45 #include "common.h"
46 
47 #define FFA_DRIVER_VERSION	FFA_VERSION_1_2
48 #define FFA_MIN_VERSION		FFA_VERSION_1_0
49 
50 #define SENDER_ID_MASK		GENMASK(31, 16)
51 #define RECEIVER_ID_MASK	GENMASK(15, 0)
52 #define SENDER_ID(x)		((u16)(FIELD_GET(SENDER_ID_MASK, (x))))
53 #define RECEIVER_ID(x)		((u16)(FIELD_GET(RECEIVER_ID_MASK, (x))))
54 #define PACK_TARGET_INFO(s, r)		\
55 	(FIELD_PREP(SENDER_ID_MASK, (s)) | FIELD_PREP(RECEIVER_ID_MASK, (r)))
56 
57 #define RXTX_MAP_MIN_BUFSZ_MASK	GENMASK(1, 0)
58 #define RXTX_MAP_MIN_BUFSZ(x)	((x) & RXTX_MAP_MIN_BUFSZ_MASK)
59 
60 #define FFA_MAX_NOTIFICATIONS		64
61 
62 static ffa_fn *invoke_ffa_fn;
63 
64 static const int ffa_linux_errmap[] = {
65 	/* better than switch case as long as return value is continuous */
66 	0,		/* FFA_RET_SUCCESS */
67 	-EOPNOTSUPP,	/* FFA_RET_NOT_SUPPORTED */
68 	-EINVAL,	/* FFA_RET_INVALID_PARAMETERS */
69 	-ENOMEM,	/* FFA_RET_NO_MEMORY */
70 	-EBUSY,		/* FFA_RET_BUSY */
71 	-EINTR,		/* FFA_RET_INTERRUPTED */
72 	-EACCES,	/* FFA_RET_DENIED */
73 	-EAGAIN,	/* FFA_RET_RETRY */
74 	-ECANCELED,	/* FFA_RET_ABORTED */
75 	-ENODATA,	/* FFA_RET_NO_DATA */
76 	-EAGAIN,	/* FFA_RET_NOT_READY */
77 };
78 
79 static inline int ffa_to_linux_errno(int errno)
80 {
81 	int err_idx = -errno;
82 
83 	if (err_idx >= 0 && err_idx < ARRAY_SIZE(ffa_linux_errmap))
84 		return ffa_linux_errmap[err_idx];
85 	return -EINVAL;
86 }
87 
88 struct ffa_pcpu_irq {
89 	struct ffa_drv_info *info;
90 };
91 
92 struct ffa_drv_info {
93 	u32 version;
94 	u16 vm_id;
95 	struct mutex rx_lock; /* lock to protect Rx buffer */
96 	struct mutex tx_lock; /* lock to protect Tx buffer */
97 	void *rx_buffer;
98 	void *tx_buffer;
99 	size_t rxtx_bufsz;
100 	bool mem_ops_native;
101 	bool msg_direct_req2_supp;
102 	bool bitmap_created;
103 	bool notif_enabled;
104 	unsigned int sched_recv_irq;
105 	unsigned int notif_pend_irq;
106 	unsigned int cpuhp_state;
107 	struct ffa_pcpu_irq __percpu *irq_pcpu;
108 	struct workqueue_struct *notif_pcpu_wq;
109 	struct work_struct notif_pcpu_work;
110 	struct work_struct sched_recv_irq_work;
111 	struct xarray partition_info;
112 	DECLARE_HASHTABLE(notifier_hash, ilog2(FFA_MAX_NOTIFICATIONS));
113 	struct mutex notify_lock; /* lock to protect notifier hashtable  */
114 };
115 
116 static struct ffa_drv_info *drv_info;
117 
118 /*
119  * The driver must be able to support all the versions from the earliest
120  * supported FFA_MIN_VERSION to the latest supported FFA_DRIVER_VERSION.
121  * The specification states that if firmware supports a FFA implementation
122  * that is incompatible with and at a greater version number than specified
123  * by the caller(FFA_DRIVER_VERSION passed as parameter to FFA_VERSION),
124  * it must return the NOT_SUPPORTED error code.
125  */
126 static u32 ffa_compatible_version_find(u32 version)
127 {
128 	u16 major = FFA_MAJOR_VERSION(version), minor = FFA_MINOR_VERSION(version);
129 	u16 drv_major = FFA_MAJOR_VERSION(FFA_DRIVER_VERSION);
130 	u16 drv_minor = FFA_MINOR_VERSION(FFA_DRIVER_VERSION);
131 
132 	if ((major < drv_major) || (major == drv_major && minor <= drv_minor))
133 		return version;
134 
135 	pr_info("Firmware version higher than driver version, downgrading\n");
136 	return FFA_DRIVER_VERSION;
137 }
138 
139 static int ffa_version_check(u32 *version)
140 {
141 	ffa_value_t ver;
142 
143 	invoke_ffa_fn((ffa_value_t){
144 		      .a0 = FFA_VERSION, .a1 = FFA_DRIVER_VERSION,
145 		      }, &ver);
146 
147 	if (ver.a0 == FFA_RET_NOT_SUPPORTED) {
148 		pr_info("FFA_VERSION returned not supported\n");
149 		return -EOPNOTSUPP;
150 	}
151 
152 	if (FFA_MAJOR_VERSION(ver.a0) > FFA_MAJOR_VERSION(FFA_DRIVER_VERSION)) {
153 		pr_err("Incompatible v%d.%d! Latest supported v%d.%d\n",
154 		       FFA_MAJOR_VERSION(ver.a0), FFA_MINOR_VERSION(ver.a0),
155 		       FFA_MAJOR_VERSION(FFA_DRIVER_VERSION),
156 		       FFA_MINOR_VERSION(FFA_DRIVER_VERSION));
157 		return -EINVAL;
158 	}
159 
160 	if (ver.a0 < FFA_MIN_VERSION) {
161 		pr_err("Incompatible v%d.%d! Earliest supported v%d.%d\n",
162 		       FFA_MAJOR_VERSION(ver.a0), FFA_MINOR_VERSION(ver.a0),
163 		       FFA_MAJOR_VERSION(FFA_MIN_VERSION),
164 		       FFA_MINOR_VERSION(FFA_MIN_VERSION));
165 		return -EINVAL;
166 	}
167 
168 	pr_info("Driver version %d.%d\n", FFA_MAJOR_VERSION(FFA_DRIVER_VERSION),
169 		FFA_MINOR_VERSION(FFA_DRIVER_VERSION));
170 	pr_info("Firmware version %d.%d found\n", FFA_MAJOR_VERSION(ver.a0),
171 		FFA_MINOR_VERSION(ver.a0));
172 	*version = ffa_compatible_version_find(ver.a0);
173 
174 	return 0;
175 }
176 
177 static int ffa_rx_release(void)
178 {
179 	ffa_value_t ret;
180 
181 	invoke_ffa_fn((ffa_value_t){
182 		      .a0 = FFA_RX_RELEASE,
183 		      }, &ret);
184 
185 	if (ret.a0 == FFA_ERROR)
186 		return ffa_to_linux_errno((int)ret.a2);
187 
188 	/* check for ret.a0 == FFA_RX_RELEASE ? */
189 
190 	return 0;
191 }
192 
193 static int ffa_rxtx_map(phys_addr_t tx_buf, phys_addr_t rx_buf, u32 pg_cnt)
194 {
195 	ffa_value_t ret;
196 
197 	invoke_ffa_fn((ffa_value_t){
198 		      .a0 = FFA_FN_NATIVE(RXTX_MAP),
199 		      .a1 = tx_buf, .a2 = rx_buf, .a3 = pg_cnt,
200 		      }, &ret);
201 
202 	if (ret.a0 == FFA_ERROR)
203 		return ffa_to_linux_errno((int)ret.a2);
204 
205 	return 0;
206 }
207 
208 static int ffa_rxtx_unmap(u16 vm_id)
209 {
210 	ffa_value_t ret;
211 
212 	invoke_ffa_fn((ffa_value_t){
213 		      .a0 = FFA_RXTX_UNMAP, .a1 = PACK_TARGET_INFO(vm_id, 0),
214 		      }, &ret);
215 
216 	if (ret.a0 == FFA_ERROR)
217 		return ffa_to_linux_errno((int)ret.a2);
218 
219 	return 0;
220 }
221 
222 static int ffa_features(u32 func_feat_id, u32 input_props,
223 			u32 *if_props_1, u32 *if_props_2)
224 {
225 	ffa_value_t id;
226 
227 	if (!ARM_SMCCC_IS_FAST_CALL(func_feat_id) && input_props) {
228 		pr_err("%s: Invalid Parameters: %x, %x", __func__,
229 		       func_feat_id, input_props);
230 		return ffa_to_linux_errno(FFA_RET_INVALID_PARAMETERS);
231 	}
232 
233 	invoke_ffa_fn((ffa_value_t){
234 		.a0 = FFA_FEATURES, .a1 = func_feat_id, .a2 = input_props,
235 		}, &id);
236 
237 	if (id.a0 == FFA_ERROR)
238 		return ffa_to_linux_errno((int)id.a2);
239 
240 	if (if_props_1)
241 		*if_props_1 = id.a2;
242 	if (if_props_2)
243 		*if_props_2 = id.a3;
244 
245 	return 0;
246 }
247 
248 #define PARTITION_INFO_GET_RETURN_COUNT_ONLY	BIT(0)
249 
250 /* buffer must be sizeof(struct ffa_partition_info) * num_partitions */
251 static int
252 __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,
253 			 struct ffa_partition_info *buffer, int num_partitions)
254 {
255 	int idx, count, flags = 0, sz, buf_sz;
256 	ffa_value_t partition_info;
257 
258 	if (drv_info->version > FFA_VERSION_1_0 &&
259 	    (!buffer || !num_partitions)) /* Just get the count for now */
260 		flags = PARTITION_INFO_GET_RETURN_COUNT_ONLY;
261 
262 	mutex_lock(&drv_info->rx_lock);
263 	invoke_ffa_fn((ffa_value_t){
264 		      .a0 = FFA_PARTITION_INFO_GET,
265 		      .a1 = uuid0, .a2 = uuid1, .a3 = uuid2, .a4 = uuid3,
266 		      .a5 = flags,
267 		      }, &partition_info);
268 
269 	if (partition_info.a0 == FFA_ERROR) {
270 		mutex_unlock(&drv_info->rx_lock);
271 		return ffa_to_linux_errno((int)partition_info.a2);
272 	}
273 
274 	count = partition_info.a2;
275 
276 	if (drv_info->version > FFA_VERSION_1_0) {
277 		buf_sz = sz = partition_info.a3;
278 		if (sz > sizeof(*buffer))
279 			buf_sz = sizeof(*buffer);
280 	} else {
281 		/* FFA_VERSION_1_0 lacks size in the response */
282 		buf_sz = sz = 8;
283 	}
284 
285 	if (buffer && count <= num_partitions)
286 		for (idx = 0; idx < count; idx++) {
287 			struct ffa_partition_info_le {
288 				__le16 id;
289 				__le16 exec_ctxt;
290 				__le32 properties;
291 				uuid_t uuid;
292 			} *rx_buf = drv_info->rx_buffer + idx * sz;
293 			struct ffa_partition_info *buf = buffer + idx;
294 
295 			buf->id = le16_to_cpu(rx_buf->id);
296 			buf->exec_ctxt = le16_to_cpu(rx_buf->exec_ctxt);
297 			buf->properties = le32_to_cpu(rx_buf->properties);
298 			if (buf_sz > 8)
299 				import_uuid(&buf->uuid, (u8 *)&rx_buf->uuid);
300 		}
301 
302 	ffa_rx_release();
303 
304 	mutex_unlock(&drv_info->rx_lock);
305 
306 	return count;
307 }
308 
309 #define LAST_INDEX_MASK		GENMASK(15, 0)
310 #define CURRENT_INDEX_MASK	GENMASK(31, 16)
311 #define UUID_INFO_TAG_MASK	GENMASK(47, 32)
312 #define PARTITION_INFO_SZ_MASK	GENMASK(63, 48)
313 #define PARTITION_COUNT(x)	((u16)(FIELD_GET(LAST_INDEX_MASK, (x))) + 1)
314 #define CURRENT_INDEX(x)	((u16)(FIELD_GET(CURRENT_INDEX_MASK, (x))))
315 #define UUID_INFO_TAG(x)	((u16)(FIELD_GET(UUID_INFO_TAG_MASK, (x))))
316 #define PARTITION_INFO_SZ(x)	((u16)(FIELD_GET(PARTITION_INFO_SZ_MASK, (x))))
317 #define PART_INFO_ID_MASK	GENMASK(15, 0)
318 #define PART_INFO_EXEC_CXT_MASK	GENMASK(31, 16)
319 #define PART_INFO_PROPS_MASK	GENMASK(63, 32)
320 #define PART_INFO_ID(x)		((u16)(FIELD_GET(PART_INFO_ID_MASK, (x))))
321 #define PART_INFO_EXEC_CXT(x)	((u16)(FIELD_GET(PART_INFO_EXEC_CXT_MASK, (x))))
322 #define PART_INFO_PROPERTIES(x)	((u32)(FIELD_GET(PART_INFO_PROPS_MASK, (x))))
323 static int
324 __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,
325 			      struct ffa_partition_info *buffer, int num_parts)
326 {
327 	u16 buf_sz, start_idx, cur_idx, count = 0, prev_idx = 0, tag = 0;
328 	struct ffa_partition_info *buf = buffer;
329 	ffa_value_t partition_info;
330 
331 	do {
332 		__le64 *regs;
333 		int idx;
334 
335 		start_idx = prev_idx ? prev_idx + 1 : 0;
336 
337 		invoke_ffa_fn((ffa_value_t){
338 			      .a0 = FFA_PARTITION_INFO_GET_REGS,
339 			      .a1 = (u64)uuid1 << 32 | uuid0,
340 			      .a2 = (u64)uuid3 << 32 | uuid2,
341 			      .a3 = start_idx | tag << 16,
342 			      }, &partition_info);
343 
344 		if (partition_info.a0 == FFA_ERROR)
345 			return ffa_to_linux_errno((int)partition_info.a2);
346 
347 		if (!count)
348 			count = PARTITION_COUNT(partition_info.a2);
349 		if (!buffer || !num_parts) /* count only */
350 			return count;
351 
352 		cur_idx = CURRENT_INDEX(partition_info.a2);
353 		tag = UUID_INFO_TAG(partition_info.a2);
354 		buf_sz = PARTITION_INFO_SZ(partition_info.a2);
355 		if (buf_sz > sizeof(*buffer))
356 			buf_sz = sizeof(*buffer);
357 
358 		regs = (void *)&partition_info.a3;
359 		for (idx = 0; idx < cur_idx - start_idx + 1; idx++, buf++) {
360 			union {
361 				uuid_t uuid;
362 				u64 regs[2];
363 			} uuid_regs = {
364 				.regs = {
365 					le64_to_cpu(*(regs + 1)),
366 					le64_to_cpu(*(regs + 2)),
367 					}
368 			};
369 			u64 val = *(u64 *)regs;
370 
371 			buf->id = PART_INFO_ID(val);
372 			buf->exec_ctxt = PART_INFO_EXEC_CXT(val);
373 			buf->properties = PART_INFO_PROPERTIES(val);
374 			uuid_copy(&buf->uuid, &uuid_regs.uuid);
375 			regs += 3;
376 		}
377 		prev_idx = cur_idx;
378 
379 	} while (cur_idx < (count - 1));
380 
381 	return count;
382 }
383 
384 /* buffer is allocated and caller must free the same if returned count > 0 */
385 static int
386 ffa_partition_probe(const uuid_t *uuid, struct ffa_partition_info **buffer)
387 {
388 	int count;
389 	u32 uuid0_4[4];
390 	bool reg_mode = false;
391 	struct ffa_partition_info *pbuf;
392 
393 	if (!ffa_features(FFA_PARTITION_INFO_GET_REGS, 0, NULL, NULL))
394 		reg_mode = true;
395 
396 	export_uuid((u8 *)uuid0_4, uuid);
397 	if (reg_mode)
398 		count = __ffa_partition_info_get_regs(uuid0_4[0], uuid0_4[1],
399 						      uuid0_4[2], uuid0_4[3],
400 						      NULL, 0);
401 	else
402 		count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1],
403 						 uuid0_4[2], uuid0_4[3],
404 						 NULL, 0);
405 	if (count <= 0)
406 		return count;
407 
408 	pbuf = kcalloc(count, sizeof(*pbuf), GFP_KERNEL);
409 	if (!pbuf)
410 		return -ENOMEM;
411 
412 	if (reg_mode)
413 		count = __ffa_partition_info_get_regs(uuid0_4[0], uuid0_4[1],
414 						      uuid0_4[2], uuid0_4[3],
415 						      pbuf, count);
416 	else
417 		count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1],
418 						 uuid0_4[2], uuid0_4[3],
419 						 pbuf, count);
420 	if (count <= 0)
421 		kfree(pbuf);
422 	else
423 		*buffer = pbuf;
424 
425 	return count;
426 }
427 
428 #define VM_ID_MASK	GENMASK(15, 0)
429 static int ffa_id_get(u16 *vm_id)
430 {
431 	ffa_value_t id;
432 
433 	invoke_ffa_fn((ffa_value_t){
434 		      .a0 = FFA_ID_GET,
435 		      }, &id);
436 
437 	if (id.a0 == FFA_ERROR)
438 		return ffa_to_linux_errno((int)id.a2);
439 
440 	*vm_id = FIELD_GET(VM_ID_MASK, (id.a2));
441 
442 	return 0;
443 }
444 
445 static inline void ffa_msg_send_wait_for_completion(ffa_value_t *ret)
446 {
447 	while (ret->a0 == FFA_INTERRUPT || ret->a0 == FFA_YIELD) {
448 		if (ret->a0 == FFA_YIELD)
449 			fsleep(1000);
450 
451 		invoke_ffa_fn((ffa_value_t){
452 			      .a0 = FFA_RUN, .a1 = ret->a1,
453 			      }, ret);
454 	}
455 }
456 
457 static int ffa_msg_send_direct_req(u16 src_id, u16 dst_id, bool mode_32bit,
458 				   struct ffa_send_direct_data *data)
459 {
460 	u32 req_id, resp_id, src_dst_ids = PACK_TARGET_INFO(src_id, dst_id);
461 	ffa_value_t ret;
462 
463 	if (mode_32bit) {
464 		req_id = FFA_MSG_SEND_DIRECT_REQ;
465 		resp_id = FFA_MSG_SEND_DIRECT_RESP;
466 	} else {
467 		req_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_REQ);
468 		resp_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_RESP);
469 	}
470 
471 	invoke_ffa_fn((ffa_value_t){
472 		      .a0 = req_id, .a1 = src_dst_ids, .a2 = 0,
473 		      .a3 = data->data0, .a4 = data->data1, .a5 = data->data2,
474 		      .a6 = data->data3, .a7 = data->data4,
475 		      }, &ret);
476 
477 	ffa_msg_send_wait_for_completion(&ret);
478 
479 	if (ret.a0 == FFA_ERROR)
480 		return ffa_to_linux_errno((int)ret.a2);
481 
482 	if (ret.a0 == resp_id) {
483 		data->data0 = ret.a3;
484 		data->data1 = ret.a4;
485 		data->data2 = ret.a5;
486 		data->data3 = ret.a6;
487 		data->data4 = ret.a7;
488 		return 0;
489 	}
490 
491 	return -EINVAL;
492 }
493 
494 static int ffa_msg_send2(struct ffa_device *dev, u16 src_id, void *buf, size_t sz)
495 {
496 	u32 src_dst_ids = PACK_TARGET_INFO(src_id, dev->vm_id);
497 	struct ffa_indirect_msg_hdr *msg;
498 	ffa_value_t ret;
499 	int retval = 0;
500 
501 	if (sz > (drv_info->rxtx_bufsz - sizeof(*msg)))
502 		return -ERANGE;
503 
504 	mutex_lock(&drv_info->tx_lock);
505 
506 	msg = drv_info->tx_buffer;
507 	msg->flags = 0;
508 	msg->res0 = 0;
509 	msg->offset = sizeof(*msg);
510 	msg->send_recv_id = src_dst_ids;
511 	msg->size = sz;
512 	uuid_copy(&msg->uuid, &dev->uuid);
513 	memcpy((u8 *)msg + msg->offset, buf, sz);
514 
515 	/* flags = 0, sender VMID = 0 works for both physical/virtual NS */
516 	invoke_ffa_fn((ffa_value_t){
517 		      .a0 = FFA_MSG_SEND2, .a1 = 0, .a2 = 0
518 		      }, &ret);
519 
520 	if (ret.a0 == FFA_ERROR)
521 		retval = ffa_to_linux_errno((int)ret.a2);
522 
523 	mutex_unlock(&drv_info->tx_lock);
524 	return retval;
525 }
526 
527 static int ffa_msg_send_direct_req2(u16 src_id, u16 dst_id, const uuid_t *uuid,
528 				    struct ffa_send_direct_data2 *data)
529 {
530 	u32 src_dst_ids = PACK_TARGET_INFO(src_id, dst_id);
531 	union {
532 		uuid_t uuid;
533 		__le64 regs[2];
534 	} uuid_regs = { .uuid = *uuid };
535 	ffa_value_t ret, args = {
536 		.a0 = FFA_MSG_SEND_DIRECT_REQ2,
537 		.a1 = src_dst_ids,
538 		.a2 = le64_to_cpu(uuid_regs.regs[0]),
539 		.a3 = le64_to_cpu(uuid_regs.regs[1]),
540 	};
541 	memcpy((void *)&args + offsetof(ffa_value_t, a4), data, sizeof(*data));
542 
543 	invoke_ffa_fn(args, &ret);
544 
545 	ffa_msg_send_wait_for_completion(&ret);
546 
547 	if (ret.a0 == FFA_ERROR)
548 		return ffa_to_linux_errno((int)ret.a2);
549 
550 	if (ret.a0 == FFA_MSG_SEND_DIRECT_RESP2) {
551 		memcpy(data, (void *)&ret + offsetof(ffa_value_t, a4), sizeof(*data));
552 		return 0;
553 	}
554 
555 	return -EINVAL;
556 }
557 
558 static int ffa_mem_first_frag(u32 func_id, phys_addr_t buf, u32 buf_sz,
559 			      u32 frag_len, u32 len, u64 *handle)
560 {
561 	ffa_value_t ret;
562 
563 	invoke_ffa_fn((ffa_value_t){
564 		      .a0 = func_id, .a1 = len, .a2 = frag_len,
565 		      .a3 = buf, .a4 = buf_sz,
566 		      }, &ret);
567 
568 	while (ret.a0 == FFA_MEM_OP_PAUSE)
569 		invoke_ffa_fn((ffa_value_t){
570 			      .a0 = FFA_MEM_OP_RESUME,
571 			      .a1 = ret.a1, .a2 = ret.a2,
572 			      }, &ret);
573 
574 	if (ret.a0 == FFA_ERROR)
575 		return ffa_to_linux_errno((int)ret.a2);
576 
577 	if (ret.a0 == FFA_SUCCESS) {
578 		if (handle)
579 			*handle = PACK_HANDLE(ret.a2, ret.a3);
580 	} else if (ret.a0 == FFA_MEM_FRAG_RX) {
581 		if (handle)
582 			*handle = PACK_HANDLE(ret.a1, ret.a2);
583 	} else {
584 		return -EOPNOTSUPP;
585 	}
586 
587 	return frag_len;
588 }
589 
590 static int ffa_mem_next_frag(u64 handle, u32 frag_len)
591 {
592 	ffa_value_t ret;
593 
594 	invoke_ffa_fn((ffa_value_t){
595 		      .a0 = FFA_MEM_FRAG_TX,
596 		      .a1 = HANDLE_LOW(handle), .a2 = HANDLE_HIGH(handle),
597 		      .a3 = frag_len,
598 		      }, &ret);
599 
600 	while (ret.a0 == FFA_MEM_OP_PAUSE)
601 		invoke_ffa_fn((ffa_value_t){
602 			      .a0 = FFA_MEM_OP_RESUME,
603 			      .a1 = ret.a1, .a2 = ret.a2,
604 			      }, &ret);
605 
606 	if (ret.a0 == FFA_ERROR)
607 		return ffa_to_linux_errno((int)ret.a2);
608 
609 	if (ret.a0 == FFA_MEM_FRAG_RX)
610 		return ret.a3;
611 	else if (ret.a0 == FFA_SUCCESS)
612 		return 0;
613 
614 	return -EOPNOTSUPP;
615 }
616 
617 static int
618 ffa_transmit_fragment(u32 func_id, phys_addr_t buf, u32 buf_sz, u32 frag_len,
619 		      u32 len, u64 *handle, bool first)
620 {
621 	if (!first)
622 		return ffa_mem_next_frag(*handle, frag_len);
623 
624 	return ffa_mem_first_frag(func_id, buf, buf_sz, frag_len, len, handle);
625 }
626 
627 static u32 ffa_get_num_pages_sg(struct scatterlist *sg)
628 {
629 	u32 num_pages = 0;
630 
631 	do {
632 		num_pages += sg->length / FFA_PAGE_SIZE;
633 	} while ((sg = sg_next(sg)));
634 
635 	return num_pages;
636 }
637 
638 static u16 ffa_memory_attributes_get(u32 func_id)
639 {
640 	/*
641 	 * For the memory lend or donate operation, if the receiver is a PE or
642 	 * a proxy endpoint, the owner/sender must not specify the attributes
643 	 */
644 	if (func_id == FFA_FN_NATIVE(MEM_LEND) ||
645 	    func_id == FFA_MEM_LEND)
646 		return 0;
647 
648 	return FFA_MEM_NORMAL | FFA_MEM_WRITE_BACK | FFA_MEM_INNER_SHAREABLE;
649 }
650 
651 static int
652 ffa_setup_and_transmit(u32 func_id, void *buffer, u32 max_fragsize,
653 		       struct ffa_mem_ops_args *args)
654 {
655 	int rc = 0;
656 	bool first = true;
657 	u32 composite_offset;
658 	phys_addr_t addr = 0;
659 	struct ffa_mem_region *mem_region = buffer;
660 	struct ffa_composite_mem_region *composite;
661 	struct ffa_mem_region_addr_range *constituents;
662 	struct ffa_mem_region_attributes *ep_mem_access;
663 	u32 idx, frag_len, length, buf_sz = 0, num_entries = sg_nents(args->sg);
664 
665 	mem_region->tag = args->tag;
666 	mem_region->flags = args->flags;
667 	mem_region->sender_id = drv_info->vm_id;
668 	mem_region->attributes = ffa_memory_attributes_get(func_id);
669 	ep_mem_access = buffer +
670 			ffa_mem_desc_offset(buffer, 0, drv_info->version);
671 	composite_offset = ffa_mem_desc_offset(buffer, args->nattrs,
672 					       drv_info->version);
673 
674 	for (idx = 0; idx < args->nattrs; idx++, ep_mem_access++) {
675 		ep_mem_access->receiver = args->attrs[idx].receiver;
676 		ep_mem_access->attrs = args->attrs[idx].attrs;
677 		ep_mem_access->composite_off = composite_offset;
678 		ep_mem_access->flag = 0;
679 		ep_mem_access->reserved = 0;
680 	}
681 	mem_region->handle = 0;
682 	mem_region->ep_count = args->nattrs;
683 	if (drv_info->version <= FFA_VERSION_1_0) {
684 		mem_region->ep_mem_size = 0;
685 	} else {
686 		mem_region->ep_mem_size = sizeof(*ep_mem_access);
687 		mem_region->ep_mem_offset = sizeof(*mem_region);
688 		memset(mem_region->reserved, 0, 12);
689 	}
690 
691 	composite = buffer + composite_offset;
692 	composite->total_pg_cnt = ffa_get_num_pages_sg(args->sg);
693 	composite->addr_range_cnt = num_entries;
694 	composite->reserved = 0;
695 
696 	length = composite_offset + CONSTITUENTS_OFFSET(num_entries);
697 	frag_len = composite_offset + CONSTITUENTS_OFFSET(0);
698 	if (frag_len > max_fragsize)
699 		return -ENXIO;
700 
701 	if (!args->use_txbuf) {
702 		addr = virt_to_phys(buffer);
703 		buf_sz = max_fragsize / FFA_PAGE_SIZE;
704 	}
705 
706 	constituents = buffer + frag_len;
707 	idx = 0;
708 	do {
709 		if (frag_len == max_fragsize) {
710 			rc = ffa_transmit_fragment(func_id, addr, buf_sz,
711 						   frag_len, length,
712 						   &args->g_handle, first);
713 			if (rc < 0)
714 				return -ENXIO;
715 
716 			first = false;
717 			idx = 0;
718 			frag_len = 0;
719 			constituents = buffer;
720 		}
721 
722 		if ((void *)constituents - buffer > max_fragsize) {
723 			pr_err("Memory Region Fragment > Tx Buffer size\n");
724 			return -EFAULT;
725 		}
726 
727 		constituents->address = sg_phys(args->sg);
728 		constituents->pg_cnt = args->sg->length / FFA_PAGE_SIZE;
729 		constituents->reserved = 0;
730 		constituents++;
731 		frag_len += sizeof(struct ffa_mem_region_addr_range);
732 	} while ((args->sg = sg_next(args->sg)));
733 
734 	return ffa_transmit_fragment(func_id, addr, buf_sz, frag_len,
735 				     length, &args->g_handle, first);
736 }
737 
738 static int ffa_memory_ops(u32 func_id, struct ffa_mem_ops_args *args)
739 {
740 	int ret;
741 	void *buffer;
742 	size_t rxtx_bufsz = drv_info->rxtx_bufsz;
743 
744 	if (!args->use_txbuf) {
745 		buffer = alloc_pages_exact(rxtx_bufsz, GFP_KERNEL);
746 		if (!buffer)
747 			return -ENOMEM;
748 	} else {
749 		buffer = drv_info->tx_buffer;
750 		mutex_lock(&drv_info->tx_lock);
751 	}
752 
753 	ret = ffa_setup_and_transmit(func_id, buffer, rxtx_bufsz, args);
754 
755 	if (args->use_txbuf)
756 		mutex_unlock(&drv_info->tx_lock);
757 	else
758 		free_pages_exact(buffer, rxtx_bufsz);
759 
760 	return ret < 0 ? ret : 0;
761 }
762 
763 static int ffa_memory_reclaim(u64 g_handle, u32 flags)
764 {
765 	ffa_value_t ret;
766 
767 	invoke_ffa_fn((ffa_value_t){
768 		      .a0 = FFA_MEM_RECLAIM,
769 		      .a1 = HANDLE_LOW(g_handle), .a2 = HANDLE_HIGH(g_handle),
770 		      .a3 = flags,
771 		      }, &ret);
772 
773 	if (ret.a0 == FFA_ERROR)
774 		return ffa_to_linux_errno((int)ret.a2);
775 
776 	return 0;
777 }
778 
779 static int ffa_notification_bitmap_create(void)
780 {
781 	ffa_value_t ret;
782 	u16 vcpu_count = nr_cpu_ids;
783 
784 	invoke_ffa_fn((ffa_value_t){
785 		      .a0 = FFA_NOTIFICATION_BITMAP_CREATE,
786 		      .a1 = drv_info->vm_id, .a2 = vcpu_count,
787 		      }, &ret);
788 
789 	if (ret.a0 == FFA_ERROR)
790 		return ffa_to_linux_errno((int)ret.a2);
791 
792 	return 0;
793 }
794 
795 static int ffa_notification_bitmap_destroy(void)
796 {
797 	ffa_value_t ret;
798 
799 	invoke_ffa_fn((ffa_value_t){
800 		      .a0 = FFA_NOTIFICATION_BITMAP_DESTROY,
801 		      .a1 = drv_info->vm_id,
802 		      }, &ret);
803 
804 	if (ret.a0 == FFA_ERROR)
805 		return ffa_to_linux_errno((int)ret.a2);
806 
807 	return 0;
808 }
809 
810 enum notify_type {
811 	SECURE_PARTITION,
812 	NON_SECURE_VM,
813 	SPM_FRAMEWORK,
814 	NS_HYP_FRAMEWORK,
815 };
816 
817 #define NOTIFICATION_LOW_MASK		GENMASK(31, 0)
818 #define NOTIFICATION_HIGH_MASK		GENMASK(63, 32)
819 #define NOTIFICATION_BITMAP_HIGH(x)	\
820 		((u32)(FIELD_GET(NOTIFICATION_HIGH_MASK, (x))))
821 #define NOTIFICATION_BITMAP_LOW(x)	\
822 		((u32)(FIELD_GET(NOTIFICATION_LOW_MASK, (x))))
823 #define PACK_NOTIFICATION_BITMAP(low, high)	\
824 	(FIELD_PREP(NOTIFICATION_LOW_MASK, (low)) | \
825 	 FIELD_PREP(NOTIFICATION_HIGH_MASK, (high)))
826 
827 #define RECEIVER_VCPU_MASK		GENMASK(31, 16)
828 #define PACK_NOTIFICATION_GET_RECEIVER_INFO(vcpu_r, r) \
829 	(FIELD_PREP(RECEIVER_VCPU_MASK, (vcpu_r)) | \
830 	 FIELD_PREP(RECEIVER_ID_MASK, (r)))
831 
832 #define NOTIFICATION_INFO_GET_MORE_PEND_MASK	BIT(0)
833 #define NOTIFICATION_INFO_GET_ID_COUNT		GENMASK(11, 7)
834 #define ID_LIST_MASK_64				GENMASK(51, 12)
835 #define ID_LIST_MASK_32				GENMASK(31, 12)
836 #define MAX_IDS_64				20
837 #define MAX_IDS_32				10
838 
839 #define PER_VCPU_NOTIFICATION_FLAG		BIT(0)
840 #define SECURE_PARTITION_BITMAP_ENABLE		BIT(SECURE_PARTITION)
841 #define NON_SECURE_VM_BITMAP_ENABLE		BIT(NON_SECURE_VM)
842 #define SPM_FRAMEWORK_BITMAP_ENABLE		BIT(SPM_FRAMEWORK)
843 #define NS_HYP_FRAMEWORK_BITMAP_ENABLE		BIT(NS_HYP_FRAMEWORK)
844 #define FFA_BITMAP_ENABLE_MASK			\
845 	(SECURE_PARTITION_BITMAP_ENABLE | SPM_FRAMEWORK_BITMAP_ENABLE)
846 
847 #define FFA_SECURE_PARTITION_ID_FLAG		BIT(15)
848 
849 #define SPM_FRAMEWORK_BITMAP(x)			NOTIFICATION_BITMAP_LOW(x)
850 #define NS_HYP_FRAMEWORK_BITMAP(x)		NOTIFICATION_BITMAP_HIGH(x)
851 
852 static int ffa_notification_bind_common(u16 dst_id, u64 bitmap,
853 					u32 flags, bool is_bind)
854 {
855 	ffa_value_t ret;
856 	u32 func, src_dst_ids = PACK_TARGET_INFO(dst_id, drv_info->vm_id);
857 
858 	func = is_bind ? FFA_NOTIFICATION_BIND : FFA_NOTIFICATION_UNBIND;
859 
860 	invoke_ffa_fn((ffa_value_t){
861 		  .a0 = func, .a1 = src_dst_ids, .a2 = flags,
862 		  .a3 = NOTIFICATION_BITMAP_LOW(bitmap),
863 		  .a4 = NOTIFICATION_BITMAP_HIGH(bitmap),
864 		  }, &ret);
865 
866 	if (ret.a0 == FFA_ERROR)
867 		return ffa_to_linux_errno((int)ret.a2);
868 	else if (ret.a0 != FFA_SUCCESS)
869 		return -EINVAL;
870 
871 	return 0;
872 }
873 
874 static
875 int ffa_notification_set(u16 src_id, u16 dst_id, u32 flags, u64 bitmap)
876 {
877 	ffa_value_t ret;
878 	u32 src_dst_ids = PACK_TARGET_INFO(dst_id, src_id);
879 
880 	invoke_ffa_fn((ffa_value_t) {
881 		  .a0 = FFA_NOTIFICATION_SET, .a1 = src_dst_ids, .a2 = flags,
882 		  .a3 = NOTIFICATION_BITMAP_LOW(bitmap),
883 		  .a4 = NOTIFICATION_BITMAP_HIGH(bitmap),
884 		  }, &ret);
885 
886 	if (ret.a0 == FFA_ERROR)
887 		return ffa_to_linux_errno((int)ret.a2);
888 	else if (ret.a0 != FFA_SUCCESS)
889 		return -EINVAL;
890 
891 	return 0;
892 }
893 
894 struct ffa_notify_bitmaps {
895 	u64 sp_map;
896 	u64 vm_map;
897 	u64 arch_map;
898 };
899 
900 static int ffa_notification_get(u32 flags, struct ffa_notify_bitmaps *notify)
901 {
902 	ffa_value_t ret;
903 	u16 src_id = drv_info->vm_id;
904 	u16 cpu_id = smp_processor_id();
905 	u32 rec_vcpu_ids = PACK_NOTIFICATION_GET_RECEIVER_INFO(cpu_id, src_id);
906 
907 	invoke_ffa_fn((ffa_value_t){
908 		  .a0 = FFA_NOTIFICATION_GET, .a1 = rec_vcpu_ids, .a2 = flags,
909 		  }, &ret);
910 
911 	if (ret.a0 == FFA_ERROR)
912 		return ffa_to_linux_errno((int)ret.a2);
913 	else if (ret.a0 != FFA_SUCCESS)
914 		return -EINVAL; /* Something else went wrong. */
915 
916 	notify->sp_map = PACK_NOTIFICATION_BITMAP(ret.a2, ret.a3);
917 	notify->vm_map = PACK_NOTIFICATION_BITMAP(ret.a4, ret.a5);
918 	notify->arch_map = PACK_NOTIFICATION_BITMAP(ret.a6, ret.a7);
919 
920 	return 0;
921 }
922 
923 struct ffa_dev_part_info {
924 	ffa_sched_recv_cb callback;
925 	void *cb_data;
926 	rwlock_t rw_lock;
927 };
928 
929 static void __do_sched_recv_cb(u16 part_id, u16 vcpu, bool is_per_vcpu)
930 {
931 	struct ffa_dev_part_info *partition;
932 	ffa_sched_recv_cb callback;
933 	void *cb_data;
934 
935 	partition = xa_load(&drv_info->partition_info, part_id);
936 	if (!partition) {
937 		pr_err("%s: Invalid partition ID 0x%x\n", __func__, part_id);
938 		return;
939 	}
940 
941 	read_lock(&partition->rw_lock);
942 	callback = partition->callback;
943 	cb_data = partition->cb_data;
944 	read_unlock(&partition->rw_lock);
945 
946 	if (callback)
947 		callback(vcpu, is_per_vcpu, cb_data);
948 }
949 
950 static void ffa_notification_info_get(void)
951 {
952 	int idx, list, max_ids, lists_cnt, ids_processed, ids_count[MAX_IDS_64];
953 	bool is_64b_resp;
954 	ffa_value_t ret;
955 	u64 id_list;
956 
957 	do {
958 		invoke_ffa_fn((ffa_value_t){
959 			  .a0 = FFA_FN_NATIVE(NOTIFICATION_INFO_GET),
960 			  }, &ret);
961 
962 		if (ret.a0 != FFA_FN_NATIVE(SUCCESS) && ret.a0 != FFA_SUCCESS) {
963 			if (ret.a2 != FFA_RET_NO_DATA)
964 				pr_err("Notification Info fetch failed: 0x%lx (0x%lx)",
965 				       ret.a0, ret.a2);
966 			return;
967 		}
968 
969 		is_64b_resp = (ret.a0 == FFA_FN64_SUCCESS);
970 
971 		ids_processed = 0;
972 		lists_cnt = FIELD_GET(NOTIFICATION_INFO_GET_ID_COUNT, ret.a2);
973 		if (is_64b_resp) {
974 			max_ids = MAX_IDS_64;
975 			id_list = FIELD_GET(ID_LIST_MASK_64, ret.a2);
976 		} else {
977 			max_ids = MAX_IDS_32;
978 			id_list = FIELD_GET(ID_LIST_MASK_32, ret.a2);
979 		}
980 
981 		for (idx = 0; idx < lists_cnt; idx++, id_list >>= 2)
982 			ids_count[idx] = (id_list & 0x3) + 1;
983 
984 		/* Process IDs */
985 		for (list = 0; list < lists_cnt; list++) {
986 			u16 vcpu_id, part_id, *packed_id_list = (u16 *)&ret.a3;
987 
988 			if (ids_processed >= max_ids - 1)
989 				break;
990 
991 			part_id = packed_id_list[ids_processed++];
992 
993 			if (ids_count[list] == 1) { /* Global Notification */
994 				__do_sched_recv_cb(part_id, 0, false);
995 				continue;
996 			}
997 
998 			/* Per vCPU Notification */
999 			for (idx = 0; idx < ids_count[list]; idx++) {
1000 				if (ids_processed >= max_ids - 1)
1001 					break;
1002 
1003 				vcpu_id = packed_id_list[ids_processed++];
1004 
1005 				__do_sched_recv_cb(part_id, vcpu_id, true);
1006 			}
1007 		}
1008 	} while (ret.a2 & NOTIFICATION_INFO_GET_MORE_PEND_MASK);
1009 }
1010 
1011 static int ffa_run(struct ffa_device *dev, u16 vcpu)
1012 {
1013 	ffa_value_t ret;
1014 	u32 target = dev->vm_id << 16 | vcpu;
1015 
1016 	invoke_ffa_fn((ffa_value_t){ .a0 = FFA_RUN, .a1 = target, }, &ret);
1017 
1018 	while (ret.a0 == FFA_INTERRUPT)
1019 		invoke_ffa_fn((ffa_value_t){ .a0 = FFA_RUN, .a1 = ret.a1, },
1020 			      &ret);
1021 
1022 	if (ret.a0 == FFA_ERROR)
1023 		return ffa_to_linux_errno((int)ret.a2);
1024 
1025 	return 0;
1026 }
1027 
1028 static void ffa_drvinfo_flags_init(void)
1029 {
1030 	if (!ffa_features(FFA_FN_NATIVE(MEM_LEND), 0, NULL, NULL) ||
1031 	    !ffa_features(FFA_FN_NATIVE(MEM_SHARE), 0, NULL, NULL))
1032 		drv_info->mem_ops_native = true;
1033 
1034 	if (!ffa_features(FFA_MSG_SEND_DIRECT_REQ2, 0, NULL, NULL) ||
1035 	    !ffa_features(FFA_MSG_SEND_DIRECT_RESP2, 0, NULL, NULL))
1036 		drv_info->msg_direct_req2_supp = true;
1037 }
1038 
1039 static u32 ffa_api_version_get(void)
1040 {
1041 	return drv_info->version;
1042 }
1043 
1044 static int ffa_partition_info_get(const char *uuid_str,
1045 				  struct ffa_partition_info *buffer)
1046 {
1047 	int count;
1048 	uuid_t uuid;
1049 	struct ffa_partition_info *pbuf;
1050 
1051 	if (uuid_parse(uuid_str, &uuid)) {
1052 		pr_err("invalid uuid (%s)\n", uuid_str);
1053 		return -ENODEV;
1054 	}
1055 
1056 	count = ffa_partition_probe(&uuid, &pbuf);
1057 	if (count <= 0)
1058 		return -ENOENT;
1059 
1060 	memcpy(buffer, pbuf, sizeof(*pbuf) * count);
1061 	kfree(pbuf);
1062 	return 0;
1063 }
1064 
1065 static void ffa_mode_32bit_set(struct ffa_device *dev)
1066 {
1067 	dev->mode_32bit = true;
1068 }
1069 
1070 static int ffa_sync_send_receive(struct ffa_device *dev,
1071 				 struct ffa_send_direct_data *data)
1072 {
1073 	return ffa_msg_send_direct_req(drv_info->vm_id, dev->vm_id,
1074 				       dev->mode_32bit, data);
1075 }
1076 
1077 static int ffa_indirect_msg_send(struct ffa_device *dev, void *buf, size_t sz)
1078 {
1079 	return ffa_msg_send2(dev, drv_info->vm_id, buf, sz);
1080 }
1081 
1082 static int ffa_sync_send_receive2(struct ffa_device *dev,
1083 				  struct ffa_send_direct_data2 *data)
1084 {
1085 	if (!drv_info->msg_direct_req2_supp)
1086 		return -EOPNOTSUPP;
1087 
1088 	return ffa_msg_send_direct_req2(drv_info->vm_id, dev->vm_id,
1089 					&dev->uuid, data);
1090 }
1091 
1092 static int ffa_memory_share(struct ffa_mem_ops_args *args)
1093 {
1094 	if (drv_info->mem_ops_native)
1095 		return ffa_memory_ops(FFA_FN_NATIVE(MEM_SHARE), args);
1096 
1097 	return ffa_memory_ops(FFA_MEM_SHARE, args);
1098 }
1099 
1100 static int ffa_memory_lend(struct ffa_mem_ops_args *args)
1101 {
1102 	/* Note that upon a successful MEM_LEND request the caller
1103 	 * must ensure that the memory region specified is not accessed
1104 	 * until a successful MEM_RECALIM call has been made.
1105 	 * On systems with a hypervisor present this will been enforced,
1106 	 * however on systems without a hypervisor the responsibility
1107 	 * falls to the calling kernel driver to prevent access.
1108 	 */
1109 	if (drv_info->mem_ops_native)
1110 		return ffa_memory_ops(FFA_FN_NATIVE(MEM_LEND), args);
1111 
1112 	return ffa_memory_ops(FFA_MEM_LEND, args);
1113 }
1114 
1115 #define ffa_notifications_disabled()	(!drv_info->notif_enabled)
1116 
1117 struct notifier_cb_info {
1118 	struct hlist_node hnode;
1119 	ffa_notifier_cb cb;
1120 	void *cb_data;
1121 	enum notify_type type;
1122 };
1123 
1124 static int ffa_sched_recv_cb_update(u16 part_id, ffa_sched_recv_cb callback,
1125 				    void *cb_data, bool is_registration)
1126 {
1127 	struct ffa_dev_part_info *partition;
1128 	bool cb_valid;
1129 
1130 	if (ffa_notifications_disabled())
1131 		return -EOPNOTSUPP;
1132 
1133 	partition = xa_load(&drv_info->partition_info, part_id);
1134 	if (!partition) {
1135 		pr_err("%s: Invalid partition ID 0x%x\n", __func__, part_id);
1136 		return -EINVAL;
1137 	}
1138 
1139 	write_lock(&partition->rw_lock);
1140 
1141 	cb_valid = !!partition->callback;
1142 	if (!(is_registration ^ cb_valid)) {
1143 		write_unlock(&partition->rw_lock);
1144 		return -EINVAL;
1145 	}
1146 
1147 	partition->callback = callback;
1148 	partition->cb_data = cb_data;
1149 
1150 	write_unlock(&partition->rw_lock);
1151 	return 0;
1152 }
1153 
1154 static int ffa_sched_recv_cb_register(struct ffa_device *dev,
1155 				      ffa_sched_recv_cb cb, void *cb_data)
1156 {
1157 	return ffa_sched_recv_cb_update(dev->vm_id, cb, cb_data, true);
1158 }
1159 
1160 static int ffa_sched_recv_cb_unregister(struct ffa_device *dev)
1161 {
1162 	return ffa_sched_recv_cb_update(dev->vm_id, NULL, NULL, false);
1163 }
1164 
1165 static int ffa_notification_bind(u16 dst_id, u64 bitmap, u32 flags)
1166 {
1167 	return ffa_notification_bind_common(dst_id, bitmap, flags, true);
1168 }
1169 
1170 static int ffa_notification_unbind(u16 dst_id, u64 bitmap)
1171 {
1172 	return ffa_notification_bind_common(dst_id, bitmap, 0, false);
1173 }
1174 
1175 static enum notify_type ffa_notify_type_get(u16 vm_id)
1176 {
1177 	if (vm_id & FFA_SECURE_PARTITION_ID_FLAG)
1178 		return SECURE_PARTITION;
1179 	else
1180 		return NON_SECURE_VM;
1181 }
1182 
1183 /* Should be called while the notify_lock is taken */
1184 static struct notifier_cb_info *
1185 notifier_hash_node_get(u16 notify_id, enum notify_type type)
1186 {
1187 	struct notifier_cb_info *node;
1188 
1189 	hash_for_each_possible(drv_info->notifier_hash, node, hnode, notify_id)
1190 		if (type == node->type)
1191 			return node;
1192 
1193 	return NULL;
1194 }
1195 
1196 static int
1197 update_notifier_cb(int notify_id, enum notify_type type, ffa_notifier_cb cb,
1198 		   void *cb_data, bool is_registration)
1199 {
1200 	struct notifier_cb_info *cb_info = NULL;
1201 	bool cb_found;
1202 
1203 	cb_info = notifier_hash_node_get(notify_id, type);
1204 	cb_found = !!cb_info;
1205 
1206 	if (!(is_registration ^ cb_found))
1207 		return -EINVAL;
1208 
1209 	if (is_registration) {
1210 		cb_info = kzalloc(sizeof(*cb_info), GFP_KERNEL);
1211 		if (!cb_info)
1212 			return -ENOMEM;
1213 
1214 		cb_info->type = type;
1215 		cb_info->cb = cb;
1216 		cb_info->cb_data = cb_data;
1217 
1218 		hash_add(drv_info->notifier_hash, &cb_info->hnode, notify_id);
1219 	} else {
1220 		hash_del(&cb_info->hnode);
1221 	}
1222 
1223 	return 0;
1224 }
1225 
1226 static int ffa_notify_relinquish(struct ffa_device *dev, int notify_id)
1227 {
1228 	int rc;
1229 	enum notify_type type = ffa_notify_type_get(dev->vm_id);
1230 
1231 	if (ffa_notifications_disabled())
1232 		return -EOPNOTSUPP;
1233 
1234 	if (notify_id >= FFA_MAX_NOTIFICATIONS)
1235 		return -EINVAL;
1236 
1237 	mutex_lock(&drv_info->notify_lock);
1238 
1239 	rc = update_notifier_cb(notify_id, type, NULL, NULL, false);
1240 	if (rc) {
1241 		pr_err("Could not unregister notification callback\n");
1242 		mutex_unlock(&drv_info->notify_lock);
1243 		return rc;
1244 	}
1245 
1246 	rc = ffa_notification_unbind(dev->vm_id, BIT(notify_id));
1247 
1248 	mutex_unlock(&drv_info->notify_lock);
1249 
1250 	return rc;
1251 }
1252 
1253 static int ffa_notify_request(struct ffa_device *dev, bool is_per_vcpu,
1254 			      ffa_notifier_cb cb, void *cb_data, int notify_id)
1255 {
1256 	int rc;
1257 	u32 flags = 0;
1258 	enum notify_type type = ffa_notify_type_get(dev->vm_id);
1259 
1260 	if (ffa_notifications_disabled())
1261 		return -EOPNOTSUPP;
1262 
1263 	if (notify_id >= FFA_MAX_NOTIFICATIONS)
1264 		return -EINVAL;
1265 
1266 	mutex_lock(&drv_info->notify_lock);
1267 
1268 	if (is_per_vcpu)
1269 		flags = PER_VCPU_NOTIFICATION_FLAG;
1270 
1271 	rc = ffa_notification_bind(dev->vm_id, BIT(notify_id), flags);
1272 	if (rc) {
1273 		mutex_unlock(&drv_info->notify_lock);
1274 		return rc;
1275 	}
1276 
1277 	rc = update_notifier_cb(notify_id, type, cb, cb_data, true);
1278 	if (rc) {
1279 		pr_err("Failed to register callback for %d - %d\n",
1280 		       notify_id, rc);
1281 		ffa_notification_unbind(dev->vm_id, BIT(notify_id));
1282 	}
1283 	mutex_unlock(&drv_info->notify_lock);
1284 
1285 	return rc;
1286 }
1287 
1288 static int ffa_notify_send(struct ffa_device *dev, int notify_id,
1289 			   bool is_per_vcpu, u16 vcpu)
1290 {
1291 	u32 flags = 0;
1292 
1293 	if (ffa_notifications_disabled())
1294 		return -EOPNOTSUPP;
1295 
1296 	if (is_per_vcpu)
1297 		flags |= (PER_VCPU_NOTIFICATION_FLAG | vcpu << 16);
1298 
1299 	return ffa_notification_set(dev->vm_id, drv_info->vm_id, flags,
1300 				    BIT(notify_id));
1301 }
1302 
1303 static void handle_notif_callbacks(u64 bitmap, enum notify_type type)
1304 {
1305 	int notify_id;
1306 	struct notifier_cb_info *cb_info = NULL;
1307 
1308 	if (type == SPM_FRAMEWORK || type == NS_HYP_FRAMEWORK)
1309 		return;
1310 
1311 	for (notify_id = 0; notify_id <= FFA_MAX_NOTIFICATIONS && bitmap;
1312 	     notify_id++, bitmap >>= 1) {
1313 		if (!(bitmap & 1))
1314 			continue;
1315 
1316 		mutex_lock(&drv_info->notify_lock);
1317 		cb_info = notifier_hash_node_get(notify_id, type);
1318 		mutex_unlock(&drv_info->notify_lock);
1319 
1320 		if (cb_info && cb_info->cb)
1321 			cb_info->cb(notify_id, cb_info->cb_data);
1322 	}
1323 }
1324 
1325 static void notif_get_and_handle(void *unused)
1326 {
1327 	int rc;
1328 	struct ffa_notify_bitmaps bitmaps;
1329 
1330 	rc = ffa_notification_get(FFA_BITMAP_ENABLE_MASK, &bitmaps);
1331 	if (rc) {
1332 		pr_err("Failed to retrieve notifications with %d!\n", rc);
1333 		return;
1334 	}
1335 
1336 	handle_notif_callbacks(SPM_FRAMEWORK_BITMAP(bitmaps.arch_map),
1337 			       SPM_FRAMEWORK);
1338 	handle_notif_callbacks(NS_HYP_FRAMEWORK_BITMAP(bitmaps.arch_map),
1339 			       NS_HYP_FRAMEWORK);
1340 	handle_notif_callbacks(bitmaps.vm_map, NON_SECURE_VM);
1341 	handle_notif_callbacks(bitmaps.sp_map, SECURE_PARTITION);
1342 }
1343 
1344 static void
1345 ffa_self_notif_handle(u16 vcpu, bool is_per_vcpu, void *cb_data)
1346 {
1347 	struct ffa_drv_info *info = cb_data;
1348 
1349 	if (!is_per_vcpu)
1350 		notif_get_and_handle(info);
1351 	else
1352 		smp_call_function_single(vcpu, notif_get_and_handle, info, 0);
1353 }
1354 
1355 static void notif_pcpu_irq_work_fn(struct work_struct *work)
1356 {
1357 	struct ffa_drv_info *info = container_of(work, struct ffa_drv_info,
1358 						 notif_pcpu_work);
1359 
1360 	ffa_self_notif_handle(smp_processor_id(), true, info);
1361 }
1362 
1363 static const struct ffa_info_ops ffa_drv_info_ops = {
1364 	.api_version_get = ffa_api_version_get,
1365 	.partition_info_get = ffa_partition_info_get,
1366 };
1367 
1368 static const struct ffa_msg_ops ffa_drv_msg_ops = {
1369 	.mode_32bit_set = ffa_mode_32bit_set,
1370 	.sync_send_receive = ffa_sync_send_receive,
1371 	.indirect_send = ffa_indirect_msg_send,
1372 	.sync_send_receive2 = ffa_sync_send_receive2,
1373 };
1374 
1375 static const struct ffa_mem_ops ffa_drv_mem_ops = {
1376 	.memory_reclaim = ffa_memory_reclaim,
1377 	.memory_share = ffa_memory_share,
1378 	.memory_lend = ffa_memory_lend,
1379 };
1380 
1381 static const struct ffa_cpu_ops ffa_drv_cpu_ops = {
1382 	.run = ffa_run,
1383 };
1384 
1385 static const struct ffa_notifier_ops ffa_drv_notifier_ops = {
1386 	.sched_recv_cb_register = ffa_sched_recv_cb_register,
1387 	.sched_recv_cb_unregister = ffa_sched_recv_cb_unregister,
1388 	.notify_request = ffa_notify_request,
1389 	.notify_relinquish = ffa_notify_relinquish,
1390 	.notify_send = ffa_notify_send,
1391 };
1392 
1393 static const struct ffa_ops ffa_drv_ops = {
1394 	.info_ops = &ffa_drv_info_ops,
1395 	.msg_ops = &ffa_drv_msg_ops,
1396 	.mem_ops = &ffa_drv_mem_ops,
1397 	.cpu_ops = &ffa_drv_cpu_ops,
1398 	.notifier_ops = &ffa_drv_notifier_ops,
1399 };
1400 
1401 void ffa_device_match_uuid(struct ffa_device *ffa_dev, const uuid_t *uuid)
1402 {
1403 	int count, idx;
1404 	struct ffa_partition_info *pbuf, *tpbuf;
1405 
1406 	count = ffa_partition_probe(uuid, &pbuf);
1407 	if (count <= 0)
1408 		return;
1409 
1410 	for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++)
1411 		if (tpbuf->id == ffa_dev->vm_id)
1412 			uuid_copy(&ffa_dev->uuid, uuid);
1413 	kfree(pbuf);
1414 }
1415 
1416 static int
1417 ffa_bus_notifier(struct notifier_block *nb, unsigned long action, void *data)
1418 {
1419 	struct device *dev = data;
1420 	struct ffa_device *fdev = to_ffa_dev(dev);
1421 
1422 	if (action == BUS_NOTIFY_BIND_DRIVER) {
1423 		struct ffa_driver *ffa_drv = to_ffa_driver(dev->driver);
1424 		const struct ffa_device_id *id_table = ffa_drv->id_table;
1425 
1426 		/*
1427 		 * FF-A v1.1 provides UUID for each partition as part of the
1428 		 * discovery API, the discovered UUID must be populated in the
1429 		 * device's UUID and there is no need to workaround by copying
1430 		 * the same from the driver table.
1431 		 */
1432 		if (uuid_is_null(&fdev->uuid))
1433 			ffa_device_match_uuid(fdev, &id_table->uuid);
1434 
1435 		return NOTIFY_OK;
1436 	}
1437 
1438 	return NOTIFY_DONE;
1439 }
1440 
1441 static struct notifier_block ffa_bus_nb = {
1442 	.notifier_call = ffa_bus_notifier,
1443 };
1444 
1445 static int ffa_xa_add_partition_info(int vm_id)
1446 {
1447 	struct ffa_dev_part_info *info;
1448 	int ret;
1449 
1450 	info = kzalloc(sizeof(*info), GFP_KERNEL);
1451 	if (!info)
1452 		return -ENOMEM;
1453 
1454 	rwlock_init(&info->rw_lock);
1455 	ret = xa_insert(&drv_info->partition_info, vm_id, info, GFP_KERNEL);
1456 	if (ret) {
1457 		pr_err("%s: failed to save partition ID 0x%x - ret:%d. Abort.\n",
1458 		       __func__, vm_id, ret);
1459 		kfree(info);
1460 	}
1461 
1462 	return ret;
1463 }
1464 
1465 static void ffa_partitions_cleanup(void)
1466 {
1467 	struct ffa_dev_part_info *info;
1468 	unsigned long idx;
1469 
1470 	/* Clean up/free all registered devices */
1471 	ffa_devices_unregister();
1472 
1473 	xa_for_each(&drv_info->partition_info, idx, info) {
1474 		xa_erase(&drv_info->partition_info, idx);
1475 		kfree(info);
1476 	}
1477 
1478 	xa_destroy(&drv_info->partition_info);
1479 }
1480 
1481 static int ffa_setup_partitions(void)
1482 {
1483 	int count, idx, ret;
1484 	struct ffa_device *ffa_dev;
1485 	struct ffa_partition_info *pbuf, *tpbuf;
1486 
1487 	if (drv_info->version == FFA_VERSION_1_0) {
1488 		ret = bus_register_notifier(&ffa_bus_type, &ffa_bus_nb);
1489 		if (ret)
1490 			pr_err("Failed to register FF-A bus notifiers\n");
1491 	}
1492 
1493 	count = ffa_partition_probe(&uuid_null, &pbuf);
1494 	if (count <= 0) {
1495 		pr_info("%s: No partitions found, error %d\n", __func__, count);
1496 		return -EINVAL;
1497 	}
1498 
1499 	xa_init(&drv_info->partition_info);
1500 	for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++) {
1501 		/* Note that if the UUID will be uuid_null, that will require
1502 		 * ffa_bus_notifier() to find the UUID of this partition id
1503 		 * with help of ffa_device_match_uuid(). FF-A v1.1 and above
1504 		 * provides UUID here for each partition as part of the
1505 		 * discovery API and the same is passed.
1506 		 */
1507 		ffa_dev = ffa_device_register(tpbuf, &ffa_drv_ops);
1508 		if (!ffa_dev) {
1509 			pr_err("%s: failed to register partition ID 0x%x\n",
1510 			       __func__, tpbuf->id);
1511 			continue;
1512 		}
1513 
1514 		if (drv_info->version > FFA_VERSION_1_0 &&
1515 		    !(tpbuf->properties & FFA_PARTITION_AARCH64_EXEC))
1516 			ffa_mode_32bit_set(ffa_dev);
1517 
1518 		if (ffa_xa_add_partition_info(ffa_dev->vm_id)) {
1519 			ffa_device_unregister(ffa_dev);
1520 			continue;
1521 		}
1522 	}
1523 
1524 	kfree(pbuf);
1525 
1526 	/* Check if the host is already added as part of partition info */
1527 	if (xa_load(&drv_info->partition_info, drv_info->vm_id))
1528 		return 0;
1529 
1530 	/* Allocate for the host */
1531 	ret = ffa_xa_add_partition_info(drv_info->vm_id);
1532 	if (ret)
1533 		ffa_partitions_cleanup();
1534 
1535 	return ret;
1536 }
1537 
1538 /* FFA FEATURE IDs */
1539 #define FFA_FEAT_NOTIFICATION_PENDING_INT	(1)
1540 #define FFA_FEAT_SCHEDULE_RECEIVER_INT		(2)
1541 #define FFA_FEAT_MANAGED_EXIT_INT		(3)
1542 
1543 static irqreturn_t ffa_sched_recv_irq_handler(int irq, void *irq_data)
1544 {
1545 	struct ffa_pcpu_irq *pcpu = irq_data;
1546 	struct ffa_drv_info *info = pcpu->info;
1547 
1548 	queue_work(info->notif_pcpu_wq, &info->sched_recv_irq_work);
1549 
1550 	return IRQ_HANDLED;
1551 }
1552 
1553 static irqreturn_t notif_pend_irq_handler(int irq, void *irq_data)
1554 {
1555 	struct ffa_pcpu_irq *pcpu = irq_data;
1556 	struct ffa_drv_info *info = pcpu->info;
1557 
1558 	queue_work_on(smp_processor_id(), info->notif_pcpu_wq,
1559 		      &info->notif_pcpu_work);
1560 
1561 	return IRQ_HANDLED;
1562 }
1563 
1564 static void ffa_sched_recv_irq_work_fn(struct work_struct *work)
1565 {
1566 	ffa_notification_info_get();
1567 }
1568 
1569 static int ffa_irq_map(u32 id)
1570 {
1571 	char *err_str;
1572 	int ret, irq, intid;
1573 
1574 	if (id == FFA_FEAT_NOTIFICATION_PENDING_INT)
1575 		err_str = "Notification Pending Interrupt";
1576 	else if (id == FFA_FEAT_SCHEDULE_RECEIVER_INT)
1577 		err_str = "Schedule Receiver Interrupt";
1578 	else
1579 		err_str = "Unknown ID";
1580 
1581 	/* The returned intid is assumed to be SGI donated to NS world */
1582 	ret = ffa_features(id, 0, &intid, NULL);
1583 	if (ret < 0) {
1584 		if (ret != -EOPNOTSUPP)
1585 			pr_err("Failed to retrieve FF-A %s %u\n", err_str, id);
1586 		return ret;
1587 	}
1588 
1589 	if (acpi_disabled) {
1590 		struct of_phandle_args oirq = {};
1591 		struct device_node *gic;
1592 
1593 		/* Only GICv3 supported currently with the device tree */
1594 		gic = of_find_compatible_node(NULL, NULL, "arm,gic-v3");
1595 		if (!gic)
1596 			return -ENXIO;
1597 
1598 		oirq.np = gic;
1599 		oirq.args_count = 1;
1600 		oirq.args[0] = intid;
1601 		irq = irq_create_of_mapping(&oirq);
1602 		of_node_put(gic);
1603 #ifdef CONFIG_ACPI
1604 	} else {
1605 		irq = acpi_register_gsi(NULL, intid, ACPI_EDGE_SENSITIVE,
1606 					ACPI_ACTIVE_HIGH);
1607 #endif
1608 	}
1609 
1610 	if (irq <= 0) {
1611 		pr_err("Failed to create IRQ mapping!\n");
1612 		return -ENODATA;
1613 	}
1614 
1615 	return irq;
1616 }
1617 
1618 static void ffa_irq_unmap(unsigned int irq)
1619 {
1620 	if (!irq)
1621 		return;
1622 	irq_dispose_mapping(irq);
1623 }
1624 
1625 static int ffa_cpuhp_pcpu_irq_enable(unsigned int cpu)
1626 {
1627 	if (drv_info->sched_recv_irq)
1628 		enable_percpu_irq(drv_info->sched_recv_irq, IRQ_TYPE_NONE);
1629 	if (drv_info->notif_pend_irq)
1630 		enable_percpu_irq(drv_info->notif_pend_irq, IRQ_TYPE_NONE);
1631 	return 0;
1632 }
1633 
1634 static int ffa_cpuhp_pcpu_irq_disable(unsigned int cpu)
1635 {
1636 	if (drv_info->sched_recv_irq)
1637 		disable_percpu_irq(drv_info->sched_recv_irq);
1638 	if (drv_info->notif_pend_irq)
1639 		disable_percpu_irq(drv_info->notif_pend_irq);
1640 	return 0;
1641 }
1642 
1643 static void ffa_uninit_pcpu_irq(void)
1644 {
1645 	if (drv_info->cpuhp_state) {
1646 		cpuhp_remove_state(drv_info->cpuhp_state);
1647 		drv_info->cpuhp_state = 0;
1648 	}
1649 
1650 	if (drv_info->notif_pcpu_wq) {
1651 		destroy_workqueue(drv_info->notif_pcpu_wq);
1652 		drv_info->notif_pcpu_wq = NULL;
1653 	}
1654 
1655 	if (drv_info->sched_recv_irq)
1656 		free_percpu_irq(drv_info->sched_recv_irq, drv_info->irq_pcpu);
1657 
1658 	if (drv_info->notif_pend_irq)
1659 		free_percpu_irq(drv_info->notif_pend_irq, drv_info->irq_pcpu);
1660 
1661 	if (drv_info->irq_pcpu) {
1662 		free_percpu(drv_info->irq_pcpu);
1663 		drv_info->irq_pcpu = NULL;
1664 	}
1665 }
1666 
1667 static int ffa_init_pcpu_irq(void)
1668 {
1669 	struct ffa_pcpu_irq __percpu *irq_pcpu;
1670 	int ret, cpu;
1671 
1672 	irq_pcpu = alloc_percpu(struct ffa_pcpu_irq);
1673 	if (!irq_pcpu)
1674 		return -ENOMEM;
1675 
1676 	for_each_present_cpu(cpu)
1677 		per_cpu_ptr(irq_pcpu, cpu)->info = drv_info;
1678 
1679 	drv_info->irq_pcpu = irq_pcpu;
1680 
1681 	if (drv_info->sched_recv_irq) {
1682 		ret = request_percpu_irq(drv_info->sched_recv_irq,
1683 					 ffa_sched_recv_irq_handler,
1684 					 "ARM-FFA-SRI", irq_pcpu);
1685 		if (ret) {
1686 			pr_err("Error registering percpu SRI nIRQ %d : %d\n",
1687 			       drv_info->sched_recv_irq, ret);
1688 			drv_info->sched_recv_irq = 0;
1689 			return ret;
1690 		}
1691 	}
1692 
1693 	if (drv_info->notif_pend_irq) {
1694 		ret = request_percpu_irq(drv_info->notif_pend_irq,
1695 					 notif_pend_irq_handler,
1696 					 "ARM-FFA-NPI", irq_pcpu);
1697 		if (ret) {
1698 			pr_err("Error registering percpu NPI nIRQ %d : %d\n",
1699 			       drv_info->notif_pend_irq, ret);
1700 			drv_info->notif_pend_irq = 0;
1701 			return ret;
1702 		}
1703 	}
1704 
1705 	INIT_WORK(&drv_info->sched_recv_irq_work, ffa_sched_recv_irq_work_fn);
1706 	INIT_WORK(&drv_info->notif_pcpu_work, notif_pcpu_irq_work_fn);
1707 	drv_info->notif_pcpu_wq = create_workqueue("ffa_pcpu_irq_notification");
1708 	if (!drv_info->notif_pcpu_wq)
1709 		return -EINVAL;
1710 
1711 	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "ffa/pcpu-irq:starting",
1712 				ffa_cpuhp_pcpu_irq_enable,
1713 				ffa_cpuhp_pcpu_irq_disable);
1714 
1715 	if (ret < 0)
1716 		return ret;
1717 
1718 	drv_info->cpuhp_state = ret;
1719 	return 0;
1720 }
1721 
1722 static void ffa_notifications_cleanup(void)
1723 {
1724 	ffa_uninit_pcpu_irq();
1725 	ffa_irq_unmap(drv_info->sched_recv_irq);
1726 	drv_info->sched_recv_irq = 0;
1727 	ffa_irq_unmap(drv_info->notif_pend_irq);
1728 	drv_info->notif_pend_irq = 0;
1729 
1730 	if (drv_info->bitmap_created) {
1731 		ffa_notification_bitmap_destroy();
1732 		drv_info->bitmap_created = false;
1733 	}
1734 	drv_info->notif_enabled = false;
1735 }
1736 
1737 static void ffa_notifications_setup(void)
1738 {
1739 	int ret;
1740 
1741 	ret = ffa_features(FFA_NOTIFICATION_BITMAP_CREATE, 0, NULL, NULL);
1742 	if (!ret) {
1743 		ret = ffa_notification_bitmap_create();
1744 		if (ret) {
1745 			pr_err("Notification bitmap create error %d\n", ret);
1746 			return;
1747 		}
1748 
1749 		drv_info->bitmap_created = true;
1750 	}
1751 
1752 	ret = ffa_irq_map(FFA_FEAT_SCHEDULE_RECEIVER_INT);
1753 	if (ret > 0)
1754 		drv_info->sched_recv_irq = ret;
1755 
1756 	ret = ffa_irq_map(FFA_FEAT_NOTIFICATION_PENDING_INT);
1757 	if (ret > 0)
1758 		drv_info->notif_pend_irq = ret;
1759 
1760 	if (!drv_info->sched_recv_irq && !drv_info->notif_pend_irq)
1761 		goto cleanup;
1762 
1763 	ret = ffa_init_pcpu_irq();
1764 	if (ret)
1765 		goto cleanup;
1766 
1767 	hash_init(drv_info->notifier_hash);
1768 	mutex_init(&drv_info->notify_lock);
1769 
1770 	drv_info->notif_enabled = true;
1771 	return;
1772 cleanup:
1773 	pr_info("Notification setup failed %d, not enabled\n", ret);
1774 	ffa_notifications_cleanup();
1775 }
1776 
1777 static int __init ffa_init(void)
1778 {
1779 	int ret;
1780 	u32 buf_sz;
1781 	size_t rxtx_bufsz = SZ_4K;
1782 
1783 	ret = ffa_transport_init(&invoke_ffa_fn);
1784 	if (ret)
1785 		return ret;
1786 
1787 	drv_info = kzalloc(sizeof(*drv_info), GFP_KERNEL);
1788 	if (!drv_info)
1789 		return -ENOMEM;
1790 
1791 	ret = ffa_version_check(&drv_info->version);
1792 	if (ret)
1793 		goto free_drv_info;
1794 
1795 	if (ffa_id_get(&drv_info->vm_id)) {
1796 		pr_err("failed to obtain VM id for self\n");
1797 		ret = -ENODEV;
1798 		goto free_drv_info;
1799 	}
1800 
1801 	ret = ffa_features(FFA_FN_NATIVE(RXTX_MAP), 0, &buf_sz, NULL);
1802 	if (!ret) {
1803 		if (RXTX_MAP_MIN_BUFSZ(buf_sz) == 1)
1804 			rxtx_bufsz = SZ_64K;
1805 		else if (RXTX_MAP_MIN_BUFSZ(buf_sz) == 2)
1806 			rxtx_bufsz = SZ_16K;
1807 		else
1808 			rxtx_bufsz = SZ_4K;
1809 	}
1810 
1811 	drv_info->rxtx_bufsz = rxtx_bufsz;
1812 	drv_info->rx_buffer = alloc_pages_exact(rxtx_bufsz, GFP_KERNEL);
1813 	if (!drv_info->rx_buffer) {
1814 		ret = -ENOMEM;
1815 		goto free_pages;
1816 	}
1817 
1818 	drv_info->tx_buffer = alloc_pages_exact(rxtx_bufsz, GFP_KERNEL);
1819 	if (!drv_info->tx_buffer) {
1820 		ret = -ENOMEM;
1821 		goto free_pages;
1822 	}
1823 
1824 	ret = ffa_rxtx_map(virt_to_phys(drv_info->tx_buffer),
1825 			   virt_to_phys(drv_info->rx_buffer),
1826 			   rxtx_bufsz / FFA_PAGE_SIZE);
1827 	if (ret) {
1828 		pr_err("failed to register FFA RxTx buffers\n");
1829 		goto free_pages;
1830 	}
1831 
1832 	mutex_init(&drv_info->rx_lock);
1833 	mutex_init(&drv_info->tx_lock);
1834 
1835 	ffa_drvinfo_flags_init();
1836 
1837 	ffa_notifications_setup();
1838 
1839 	ret = ffa_setup_partitions();
1840 	if (ret) {
1841 		pr_err("failed to setup partitions\n");
1842 		goto cleanup_notifs;
1843 	}
1844 
1845 	ret = ffa_sched_recv_cb_update(drv_info->vm_id, ffa_self_notif_handle,
1846 				       drv_info, true);
1847 	if (ret)
1848 		pr_info("Failed to register driver sched callback %d\n", ret);
1849 
1850 	return 0;
1851 
1852 cleanup_notifs:
1853 	ffa_notifications_cleanup();
1854 free_pages:
1855 	if (drv_info->tx_buffer)
1856 		free_pages_exact(drv_info->tx_buffer, rxtx_bufsz);
1857 	free_pages_exact(drv_info->rx_buffer, rxtx_bufsz);
1858 free_drv_info:
1859 	kfree(drv_info);
1860 	return ret;
1861 }
1862 module_init(ffa_init);
1863 
1864 static void __exit ffa_exit(void)
1865 {
1866 	ffa_notifications_cleanup();
1867 	ffa_partitions_cleanup();
1868 	ffa_rxtx_unmap(drv_info->vm_id);
1869 	free_pages_exact(drv_info->tx_buffer, drv_info->rxtx_bufsz);
1870 	free_pages_exact(drv_info->rx_buffer, drv_info->rxtx_bufsz);
1871 	kfree(drv_info);
1872 }
1873 module_exit(ffa_exit);
1874 
1875 MODULE_ALIAS("arm-ffa");
1876 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
1877 MODULE_DESCRIPTION("Arm FF-A interface driver");
1878 MODULE_LICENSE("GPL v2");
1879