xref: /linux/drivers/firmware/arm_ffa/driver.c (revision 9982cabf403fbd06a120a2d5b21830effd32b370)
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 #define NOTIFICATION_LOW_MASK		GENMASK(31, 0)
811 #define NOTIFICATION_HIGH_MASK		GENMASK(63, 32)
812 #define NOTIFICATION_BITMAP_HIGH(x)	\
813 		((u32)(FIELD_GET(NOTIFICATION_HIGH_MASK, (x))))
814 #define NOTIFICATION_BITMAP_LOW(x)	\
815 		((u32)(FIELD_GET(NOTIFICATION_LOW_MASK, (x))))
816 #define PACK_NOTIFICATION_BITMAP(low, high)	\
817 	(FIELD_PREP(NOTIFICATION_LOW_MASK, (low)) | \
818 	 FIELD_PREP(NOTIFICATION_HIGH_MASK, (high)))
819 
820 #define RECEIVER_VCPU_MASK		GENMASK(31, 16)
821 #define PACK_NOTIFICATION_GET_RECEIVER_INFO(vcpu_r, r) \
822 	(FIELD_PREP(RECEIVER_VCPU_MASK, (vcpu_r)) | \
823 	 FIELD_PREP(RECEIVER_ID_MASK, (r)))
824 
825 #define NOTIFICATION_INFO_GET_MORE_PEND_MASK	BIT(0)
826 #define NOTIFICATION_INFO_GET_ID_COUNT		GENMASK(11, 7)
827 #define ID_LIST_MASK_64				GENMASK(51, 12)
828 #define ID_LIST_MASK_32				GENMASK(31, 12)
829 #define MAX_IDS_64				20
830 #define MAX_IDS_32				10
831 
832 #define PER_VCPU_NOTIFICATION_FLAG		BIT(0)
833 #define SECURE_PARTITION_BITMAP			BIT(0)
834 #define NON_SECURE_VM_BITMAP			BIT(1)
835 #define SPM_FRAMEWORK_BITMAP			BIT(2)
836 #define NS_HYP_FRAMEWORK_BITMAP			BIT(3)
837 
838 static int ffa_notification_bind_common(u16 dst_id, u64 bitmap,
839 					u32 flags, bool is_bind)
840 {
841 	ffa_value_t ret;
842 	u32 func, src_dst_ids = PACK_TARGET_INFO(dst_id, drv_info->vm_id);
843 
844 	func = is_bind ? FFA_NOTIFICATION_BIND : FFA_NOTIFICATION_UNBIND;
845 
846 	invoke_ffa_fn((ffa_value_t){
847 		  .a0 = func, .a1 = src_dst_ids, .a2 = flags,
848 		  .a3 = NOTIFICATION_BITMAP_LOW(bitmap),
849 		  .a4 = NOTIFICATION_BITMAP_HIGH(bitmap),
850 		  }, &ret);
851 
852 	if (ret.a0 == FFA_ERROR)
853 		return ffa_to_linux_errno((int)ret.a2);
854 	else if (ret.a0 != FFA_SUCCESS)
855 		return -EINVAL;
856 
857 	return 0;
858 }
859 
860 static
861 int ffa_notification_set(u16 src_id, u16 dst_id, u32 flags, u64 bitmap)
862 {
863 	ffa_value_t ret;
864 	u32 src_dst_ids = PACK_TARGET_INFO(dst_id, src_id);
865 
866 	invoke_ffa_fn((ffa_value_t) {
867 		  .a0 = FFA_NOTIFICATION_SET, .a1 = src_dst_ids, .a2 = flags,
868 		  .a3 = NOTIFICATION_BITMAP_LOW(bitmap),
869 		  .a4 = NOTIFICATION_BITMAP_HIGH(bitmap),
870 		  }, &ret);
871 
872 	if (ret.a0 == FFA_ERROR)
873 		return ffa_to_linux_errno((int)ret.a2);
874 	else if (ret.a0 != FFA_SUCCESS)
875 		return -EINVAL;
876 
877 	return 0;
878 }
879 
880 struct ffa_notify_bitmaps {
881 	u64 sp_map;
882 	u64 vm_map;
883 	u64 arch_map;
884 };
885 
886 static int ffa_notification_get(u32 flags, struct ffa_notify_bitmaps *notify)
887 {
888 	ffa_value_t ret;
889 	u16 src_id = drv_info->vm_id;
890 	u16 cpu_id = smp_processor_id();
891 	u32 rec_vcpu_ids = PACK_NOTIFICATION_GET_RECEIVER_INFO(cpu_id, src_id);
892 
893 	invoke_ffa_fn((ffa_value_t){
894 		  .a0 = FFA_NOTIFICATION_GET, .a1 = rec_vcpu_ids, .a2 = flags,
895 		  }, &ret);
896 
897 	if (ret.a0 == FFA_ERROR)
898 		return ffa_to_linux_errno((int)ret.a2);
899 	else if (ret.a0 != FFA_SUCCESS)
900 		return -EINVAL; /* Something else went wrong. */
901 
902 	notify->sp_map = PACK_NOTIFICATION_BITMAP(ret.a2, ret.a3);
903 	notify->vm_map = PACK_NOTIFICATION_BITMAP(ret.a4, ret.a5);
904 	notify->arch_map = PACK_NOTIFICATION_BITMAP(ret.a6, ret.a7);
905 
906 	return 0;
907 }
908 
909 struct ffa_dev_part_info {
910 	ffa_sched_recv_cb callback;
911 	void *cb_data;
912 	rwlock_t rw_lock;
913 };
914 
915 static void __do_sched_recv_cb(u16 part_id, u16 vcpu, bool is_per_vcpu)
916 {
917 	struct ffa_dev_part_info *partition;
918 	ffa_sched_recv_cb callback;
919 	void *cb_data;
920 
921 	partition = xa_load(&drv_info->partition_info, part_id);
922 	if (!partition) {
923 		pr_err("%s: Invalid partition ID 0x%x\n", __func__, part_id);
924 		return;
925 	}
926 
927 	read_lock(&partition->rw_lock);
928 	callback = partition->callback;
929 	cb_data = partition->cb_data;
930 	read_unlock(&partition->rw_lock);
931 
932 	if (callback)
933 		callback(vcpu, is_per_vcpu, cb_data);
934 }
935 
936 static void ffa_notification_info_get(void)
937 {
938 	int idx, list, max_ids, lists_cnt, ids_processed, ids_count[MAX_IDS_64];
939 	bool is_64b_resp;
940 	ffa_value_t ret;
941 	u64 id_list;
942 
943 	do {
944 		invoke_ffa_fn((ffa_value_t){
945 			  .a0 = FFA_FN_NATIVE(NOTIFICATION_INFO_GET),
946 			  }, &ret);
947 
948 		if (ret.a0 != FFA_FN_NATIVE(SUCCESS) && ret.a0 != FFA_SUCCESS) {
949 			if (ret.a2 != FFA_RET_NO_DATA)
950 				pr_err("Notification Info fetch failed: 0x%lx (0x%lx)",
951 				       ret.a0, ret.a2);
952 			return;
953 		}
954 
955 		is_64b_resp = (ret.a0 == FFA_FN64_SUCCESS);
956 
957 		ids_processed = 0;
958 		lists_cnt = FIELD_GET(NOTIFICATION_INFO_GET_ID_COUNT, ret.a2);
959 		if (is_64b_resp) {
960 			max_ids = MAX_IDS_64;
961 			id_list = FIELD_GET(ID_LIST_MASK_64, ret.a2);
962 		} else {
963 			max_ids = MAX_IDS_32;
964 			id_list = FIELD_GET(ID_LIST_MASK_32, ret.a2);
965 		}
966 
967 		for (idx = 0; idx < lists_cnt; idx++, id_list >>= 2)
968 			ids_count[idx] = (id_list & 0x3) + 1;
969 
970 		/* Process IDs */
971 		for (list = 0; list < lists_cnt; list++) {
972 			u16 vcpu_id, part_id, *packed_id_list = (u16 *)&ret.a3;
973 
974 			if (ids_processed >= max_ids - 1)
975 				break;
976 
977 			part_id = packed_id_list[ids_processed++];
978 
979 			if (ids_count[list] == 1) { /* Global Notification */
980 				__do_sched_recv_cb(part_id, 0, false);
981 				continue;
982 			}
983 
984 			/* Per vCPU Notification */
985 			for (idx = 0; idx < ids_count[list]; idx++) {
986 				if (ids_processed >= max_ids - 1)
987 					break;
988 
989 				vcpu_id = packed_id_list[ids_processed++];
990 
991 				__do_sched_recv_cb(part_id, vcpu_id, true);
992 			}
993 		}
994 	} while (ret.a2 & NOTIFICATION_INFO_GET_MORE_PEND_MASK);
995 }
996 
997 static int ffa_run(struct ffa_device *dev, u16 vcpu)
998 {
999 	ffa_value_t ret;
1000 	u32 target = dev->vm_id << 16 | vcpu;
1001 
1002 	invoke_ffa_fn((ffa_value_t){ .a0 = FFA_RUN, .a1 = target, }, &ret);
1003 
1004 	while (ret.a0 == FFA_INTERRUPT)
1005 		invoke_ffa_fn((ffa_value_t){ .a0 = FFA_RUN, .a1 = ret.a1, },
1006 			      &ret);
1007 
1008 	if (ret.a0 == FFA_ERROR)
1009 		return ffa_to_linux_errno((int)ret.a2);
1010 
1011 	return 0;
1012 }
1013 
1014 static void ffa_drvinfo_flags_init(void)
1015 {
1016 	if (!ffa_features(FFA_FN_NATIVE(MEM_LEND), 0, NULL, NULL) ||
1017 	    !ffa_features(FFA_FN_NATIVE(MEM_SHARE), 0, NULL, NULL))
1018 		drv_info->mem_ops_native = true;
1019 
1020 	if (!ffa_features(FFA_MSG_SEND_DIRECT_REQ2, 0, NULL, NULL) ||
1021 	    !ffa_features(FFA_MSG_SEND_DIRECT_RESP2, 0, NULL, NULL))
1022 		drv_info->msg_direct_req2_supp = true;
1023 }
1024 
1025 static u32 ffa_api_version_get(void)
1026 {
1027 	return drv_info->version;
1028 }
1029 
1030 static int ffa_partition_info_get(const char *uuid_str,
1031 				  struct ffa_partition_info *buffer)
1032 {
1033 	int count;
1034 	uuid_t uuid;
1035 	struct ffa_partition_info *pbuf;
1036 
1037 	if (uuid_parse(uuid_str, &uuid)) {
1038 		pr_err("invalid uuid (%s)\n", uuid_str);
1039 		return -ENODEV;
1040 	}
1041 
1042 	count = ffa_partition_probe(&uuid, &pbuf);
1043 	if (count <= 0)
1044 		return -ENOENT;
1045 
1046 	memcpy(buffer, pbuf, sizeof(*pbuf) * count);
1047 	kfree(pbuf);
1048 	return 0;
1049 }
1050 
1051 static void ffa_mode_32bit_set(struct ffa_device *dev)
1052 {
1053 	dev->mode_32bit = true;
1054 }
1055 
1056 static int ffa_sync_send_receive(struct ffa_device *dev,
1057 				 struct ffa_send_direct_data *data)
1058 {
1059 	return ffa_msg_send_direct_req(drv_info->vm_id, dev->vm_id,
1060 				       dev->mode_32bit, data);
1061 }
1062 
1063 static int ffa_indirect_msg_send(struct ffa_device *dev, void *buf, size_t sz)
1064 {
1065 	return ffa_msg_send2(dev, drv_info->vm_id, buf, sz);
1066 }
1067 
1068 static int ffa_sync_send_receive2(struct ffa_device *dev,
1069 				  struct ffa_send_direct_data2 *data)
1070 {
1071 	if (!drv_info->msg_direct_req2_supp)
1072 		return -EOPNOTSUPP;
1073 
1074 	return ffa_msg_send_direct_req2(drv_info->vm_id, dev->vm_id,
1075 					&dev->uuid, data);
1076 }
1077 
1078 static int ffa_memory_share(struct ffa_mem_ops_args *args)
1079 {
1080 	if (drv_info->mem_ops_native)
1081 		return ffa_memory_ops(FFA_FN_NATIVE(MEM_SHARE), args);
1082 
1083 	return ffa_memory_ops(FFA_MEM_SHARE, args);
1084 }
1085 
1086 static int ffa_memory_lend(struct ffa_mem_ops_args *args)
1087 {
1088 	/* Note that upon a successful MEM_LEND request the caller
1089 	 * must ensure that the memory region specified is not accessed
1090 	 * until a successful MEM_RECALIM call has been made.
1091 	 * On systems with a hypervisor present this will been enforced,
1092 	 * however on systems without a hypervisor the responsibility
1093 	 * falls to the calling kernel driver to prevent access.
1094 	 */
1095 	if (drv_info->mem_ops_native)
1096 		return ffa_memory_ops(FFA_FN_NATIVE(MEM_LEND), args);
1097 
1098 	return ffa_memory_ops(FFA_MEM_LEND, args);
1099 }
1100 
1101 #define FFA_SECURE_PARTITION_ID_FLAG	BIT(15)
1102 
1103 #define ffa_notifications_disabled()	(!drv_info->notif_enabled)
1104 
1105 enum notify_type {
1106 	NON_SECURE_VM,
1107 	SECURE_PARTITION,
1108 	FRAMEWORK,
1109 };
1110 
1111 struct notifier_cb_info {
1112 	struct hlist_node hnode;
1113 	ffa_notifier_cb cb;
1114 	void *cb_data;
1115 	enum notify_type type;
1116 };
1117 
1118 static int ffa_sched_recv_cb_update(u16 part_id, ffa_sched_recv_cb callback,
1119 				    void *cb_data, bool is_registration)
1120 {
1121 	struct ffa_dev_part_info *partition;
1122 	bool cb_valid;
1123 
1124 	if (ffa_notifications_disabled())
1125 		return -EOPNOTSUPP;
1126 
1127 	partition = xa_load(&drv_info->partition_info, part_id);
1128 	if (!partition) {
1129 		pr_err("%s: Invalid partition ID 0x%x\n", __func__, part_id);
1130 		return -EINVAL;
1131 	}
1132 
1133 	write_lock(&partition->rw_lock);
1134 
1135 	cb_valid = !!partition->callback;
1136 	if (!(is_registration ^ cb_valid)) {
1137 		write_unlock(&partition->rw_lock);
1138 		return -EINVAL;
1139 	}
1140 
1141 	partition->callback = callback;
1142 	partition->cb_data = cb_data;
1143 
1144 	write_unlock(&partition->rw_lock);
1145 	return 0;
1146 }
1147 
1148 static int ffa_sched_recv_cb_register(struct ffa_device *dev,
1149 				      ffa_sched_recv_cb cb, void *cb_data)
1150 {
1151 	return ffa_sched_recv_cb_update(dev->vm_id, cb, cb_data, true);
1152 }
1153 
1154 static int ffa_sched_recv_cb_unregister(struct ffa_device *dev)
1155 {
1156 	return ffa_sched_recv_cb_update(dev->vm_id, NULL, NULL, false);
1157 }
1158 
1159 static int ffa_notification_bind(u16 dst_id, u64 bitmap, u32 flags)
1160 {
1161 	return ffa_notification_bind_common(dst_id, bitmap, flags, true);
1162 }
1163 
1164 static int ffa_notification_unbind(u16 dst_id, u64 bitmap)
1165 {
1166 	return ffa_notification_bind_common(dst_id, bitmap, 0, false);
1167 }
1168 
1169 /* Should be called while the notify_lock is taken */
1170 static struct notifier_cb_info *
1171 notifier_hash_node_get(u16 notify_id, enum notify_type type)
1172 {
1173 	struct notifier_cb_info *node;
1174 
1175 	hash_for_each_possible(drv_info->notifier_hash, node, hnode, notify_id)
1176 		if (type == node->type)
1177 			return node;
1178 
1179 	return NULL;
1180 }
1181 
1182 static int
1183 update_notifier_cb(int notify_id, enum notify_type type, ffa_notifier_cb cb,
1184 		   void *cb_data, bool is_registration)
1185 {
1186 	struct notifier_cb_info *cb_info = NULL;
1187 	bool cb_found;
1188 
1189 	cb_info = notifier_hash_node_get(notify_id, type);
1190 	cb_found = !!cb_info;
1191 
1192 	if (!(is_registration ^ cb_found))
1193 		return -EINVAL;
1194 
1195 	if (is_registration) {
1196 		cb_info = kzalloc(sizeof(*cb_info), GFP_KERNEL);
1197 		if (!cb_info)
1198 			return -ENOMEM;
1199 
1200 		cb_info->type = type;
1201 		cb_info->cb = cb;
1202 		cb_info->cb_data = cb_data;
1203 
1204 		hash_add(drv_info->notifier_hash, &cb_info->hnode, notify_id);
1205 	} else {
1206 		hash_del(&cb_info->hnode);
1207 	}
1208 
1209 	return 0;
1210 }
1211 
1212 static enum notify_type ffa_notify_type_get(u16 vm_id)
1213 {
1214 	if (vm_id & FFA_SECURE_PARTITION_ID_FLAG)
1215 		return SECURE_PARTITION;
1216 	else
1217 		return NON_SECURE_VM;
1218 }
1219 
1220 static int ffa_notify_relinquish(struct ffa_device *dev, int notify_id)
1221 {
1222 	int rc;
1223 	enum notify_type type = ffa_notify_type_get(dev->vm_id);
1224 
1225 	if (ffa_notifications_disabled())
1226 		return -EOPNOTSUPP;
1227 
1228 	if (notify_id >= FFA_MAX_NOTIFICATIONS)
1229 		return -EINVAL;
1230 
1231 	mutex_lock(&drv_info->notify_lock);
1232 
1233 	rc = update_notifier_cb(notify_id, type, NULL, NULL, false);
1234 	if (rc) {
1235 		pr_err("Could not unregister notification callback\n");
1236 		mutex_unlock(&drv_info->notify_lock);
1237 		return rc;
1238 	}
1239 
1240 	rc = ffa_notification_unbind(dev->vm_id, BIT(notify_id));
1241 
1242 	mutex_unlock(&drv_info->notify_lock);
1243 
1244 	return rc;
1245 }
1246 
1247 static int ffa_notify_request(struct ffa_device *dev, bool is_per_vcpu,
1248 			      ffa_notifier_cb cb, void *cb_data, int notify_id)
1249 {
1250 	int rc;
1251 	u32 flags = 0;
1252 	enum notify_type type = ffa_notify_type_get(dev->vm_id);
1253 
1254 	if (ffa_notifications_disabled())
1255 		return -EOPNOTSUPP;
1256 
1257 	if (notify_id >= FFA_MAX_NOTIFICATIONS)
1258 		return -EINVAL;
1259 
1260 	mutex_lock(&drv_info->notify_lock);
1261 
1262 	if (is_per_vcpu)
1263 		flags = PER_VCPU_NOTIFICATION_FLAG;
1264 
1265 	rc = ffa_notification_bind(dev->vm_id, BIT(notify_id), flags);
1266 	if (rc) {
1267 		mutex_unlock(&drv_info->notify_lock);
1268 		return rc;
1269 	}
1270 
1271 	rc = update_notifier_cb(notify_id, type, cb, cb_data, true);
1272 	if (rc) {
1273 		pr_err("Failed to register callback for %d - %d\n",
1274 		       notify_id, rc);
1275 		ffa_notification_unbind(dev->vm_id, BIT(notify_id));
1276 	}
1277 	mutex_unlock(&drv_info->notify_lock);
1278 
1279 	return rc;
1280 }
1281 
1282 static int ffa_notify_send(struct ffa_device *dev, int notify_id,
1283 			   bool is_per_vcpu, u16 vcpu)
1284 {
1285 	u32 flags = 0;
1286 
1287 	if (ffa_notifications_disabled())
1288 		return -EOPNOTSUPP;
1289 
1290 	if (is_per_vcpu)
1291 		flags |= (PER_VCPU_NOTIFICATION_FLAG | vcpu << 16);
1292 
1293 	return ffa_notification_set(dev->vm_id, drv_info->vm_id, flags,
1294 				    BIT(notify_id));
1295 }
1296 
1297 static void handle_notif_callbacks(u64 bitmap, enum notify_type type)
1298 {
1299 	int notify_id;
1300 	struct notifier_cb_info *cb_info = NULL;
1301 
1302 	for (notify_id = 0; notify_id <= FFA_MAX_NOTIFICATIONS && bitmap;
1303 	     notify_id++, bitmap >>= 1) {
1304 		if (!(bitmap & 1))
1305 			continue;
1306 
1307 		mutex_lock(&drv_info->notify_lock);
1308 		cb_info = notifier_hash_node_get(notify_id, type);
1309 		mutex_unlock(&drv_info->notify_lock);
1310 
1311 		if (cb_info && cb_info->cb)
1312 			cb_info->cb(notify_id, cb_info->cb_data);
1313 	}
1314 }
1315 
1316 static void notif_get_and_handle(void *unused)
1317 {
1318 	int rc;
1319 	struct ffa_notify_bitmaps bitmaps;
1320 
1321 	rc = ffa_notification_get(SECURE_PARTITION_BITMAP |
1322 				  SPM_FRAMEWORK_BITMAP, &bitmaps);
1323 	if (rc) {
1324 		pr_err("Failed to retrieve notifications with %d!\n", rc);
1325 		return;
1326 	}
1327 
1328 	handle_notif_callbacks(bitmaps.vm_map, NON_SECURE_VM);
1329 	handle_notif_callbacks(bitmaps.sp_map, SECURE_PARTITION);
1330 	handle_notif_callbacks(bitmaps.arch_map, FRAMEWORK);
1331 }
1332 
1333 static void
1334 ffa_self_notif_handle(u16 vcpu, bool is_per_vcpu, void *cb_data)
1335 {
1336 	struct ffa_drv_info *info = cb_data;
1337 
1338 	if (!is_per_vcpu)
1339 		notif_get_and_handle(info);
1340 	else
1341 		smp_call_function_single(vcpu, notif_get_and_handle, info, 0);
1342 }
1343 
1344 static void notif_pcpu_irq_work_fn(struct work_struct *work)
1345 {
1346 	struct ffa_drv_info *info = container_of(work, struct ffa_drv_info,
1347 						 notif_pcpu_work);
1348 
1349 	ffa_self_notif_handle(smp_processor_id(), true, info);
1350 }
1351 
1352 static const struct ffa_info_ops ffa_drv_info_ops = {
1353 	.api_version_get = ffa_api_version_get,
1354 	.partition_info_get = ffa_partition_info_get,
1355 };
1356 
1357 static const struct ffa_msg_ops ffa_drv_msg_ops = {
1358 	.mode_32bit_set = ffa_mode_32bit_set,
1359 	.sync_send_receive = ffa_sync_send_receive,
1360 	.indirect_send = ffa_indirect_msg_send,
1361 	.sync_send_receive2 = ffa_sync_send_receive2,
1362 };
1363 
1364 static const struct ffa_mem_ops ffa_drv_mem_ops = {
1365 	.memory_reclaim = ffa_memory_reclaim,
1366 	.memory_share = ffa_memory_share,
1367 	.memory_lend = ffa_memory_lend,
1368 };
1369 
1370 static const struct ffa_cpu_ops ffa_drv_cpu_ops = {
1371 	.run = ffa_run,
1372 };
1373 
1374 static const struct ffa_notifier_ops ffa_drv_notifier_ops = {
1375 	.sched_recv_cb_register = ffa_sched_recv_cb_register,
1376 	.sched_recv_cb_unregister = ffa_sched_recv_cb_unregister,
1377 	.notify_request = ffa_notify_request,
1378 	.notify_relinquish = ffa_notify_relinquish,
1379 	.notify_send = ffa_notify_send,
1380 };
1381 
1382 static const struct ffa_ops ffa_drv_ops = {
1383 	.info_ops = &ffa_drv_info_ops,
1384 	.msg_ops = &ffa_drv_msg_ops,
1385 	.mem_ops = &ffa_drv_mem_ops,
1386 	.cpu_ops = &ffa_drv_cpu_ops,
1387 	.notifier_ops = &ffa_drv_notifier_ops,
1388 };
1389 
1390 void ffa_device_match_uuid(struct ffa_device *ffa_dev, const uuid_t *uuid)
1391 {
1392 	int count, idx;
1393 	struct ffa_partition_info *pbuf, *tpbuf;
1394 
1395 	count = ffa_partition_probe(uuid, &pbuf);
1396 	if (count <= 0)
1397 		return;
1398 
1399 	for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++)
1400 		if (tpbuf->id == ffa_dev->vm_id)
1401 			uuid_copy(&ffa_dev->uuid, uuid);
1402 	kfree(pbuf);
1403 }
1404 
1405 static int
1406 ffa_bus_notifier(struct notifier_block *nb, unsigned long action, void *data)
1407 {
1408 	struct device *dev = data;
1409 	struct ffa_device *fdev = to_ffa_dev(dev);
1410 
1411 	if (action == BUS_NOTIFY_BIND_DRIVER) {
1412 		struct ffa_driver *ffa_drv = to_ffa_driver(dev->driver);
1413 		const struct ffa_device_id *id_table = ffa_drv->id_table;
1414 
1415 		/*
1416 		 * FF-A v1.1 provides UUID for each partition as part of the
1417 		 * discovery API, the discovered UUID must be populated in the
1418 		 * device's UUID and there is no need to workaround by copying
1419 		 * the same from the driver table.
1420 		 */
1421 		if (uuid_is_null(&fdev->uuid))
1422 			ffa_device_match_uuid(fdev, &id_table->uuid);
1423 
1424 		return NOTIFY_OK;
1425 	}
1426 
1427 	return NOTIFY_DONE;
1428 }
1429 
1430 static struct notifier_block ffa_bus_nb = {
1431 	.notifier_call = ffa_bus_notifier,
1432 };
1433 
1434 static int ffa_xa_add_partition_info(int vm_id)
1435 {
1436 	struct ffa_dev_part_info *info;
1437 	int ret;
1438 
1439 	info = kzalloc(sizeof(*info), GFP_KERNEL);
1440 	if (!info)
1441 		return -ENOMEM;
1442 
1443 	rwlock_init(&info->rw_lock);
1444 	ret = xa_insert(&drv_info->partition_info, vm_id, info, GFP_KERNEL);
1445 	if (ret) {
1446 		pr_err("%s: failed to save partition ID 0x%x - ret:%d. Abort.\n",
1447 		       __func__, vm_id, ret);
1448 		kfree(info);
1449 	}
1450 
1451 	return ret;
1452 }
1453 
1454 static void ffa_partitions_cleanup(void)
1455 {
1456 	struct ffa_dev_part_info *info;
1457 	unsigned long idx;
1458 
1459 	/* Clean up/free all registered devices */
1460 	ffa_devices_unregister();
1461 
1462 	xa_for_each(&drv_info->partition_info, idx, info) {
1463 		xa_erase(&drv_info->partition_info, idx);
1464 		kfree(info);
1465 	}
1466 
1467 	xa_destroy(&drv_info->partition_info);
1468 }
1469 
1470 static int ffa_setup_partitions(void)
1471 {
1472 	int count, idx, ret;
1473 	struct ffa_device *ffa_dev;
1474 	struct ffa_partition_info *pbuf, *tpbuf;
1475 
1476 	if (drv_info->version == FFA_VERSION_1_0) {
1477 		ret = bus_register_notifier(&ffa_bus_type, &ffa_bus_nb);
1478 		if (ret)
1479 			pr_err("Failed to register FF-A bus notifiers\n");
1480 	}
1481 
1482 	count = ffa_partition_probe(&uuid_null, &pbuf);
1483 	if (count <= 0) {
1484 		pr_info("%s: No partitions found, error %d\n", __func__, count);
1485 		return -EINVAL;
1486 	}
1487 
1488 	xa_init(&drv_info->partition_info);
1489 	for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++) {
1490 		/* Note that if the UUID will be uuid_null, that will require
1491 		 * ffa_bus_notifier() to find the UUID of this partition id
1492 		 * with help of ffa_device_match_uuid(). FF-A v1.1 and above
1493 		 * provides UUID here for each partition as part of the
1494 		 * discovery API and the same is passed.
1495 		 */
1496 		ffa_dev = ffa_device_register(tpbuf, &ffa_drv_ops);
1497 		if (!ffa_dev) {
1498 			pr_err("%s: failed to register partition ID 0x%x\n",
1499 			       __func__, tpbuf->id);
1500 			continue;
1501 		}
1502 
1503 		if (drv_info->version > FFA_VERSION_1_0 &&
1504 		    !(tpbuf->properties & FFA_PARTITION_AARCH64_EXEC))
1505 			ffa_mode_32bit_set(ffa_dev);
1506 
1507 		if (ffa_xa_add_partition_info(ffa_dev->vm_id)) {
1508 			ffa_device_unregister(ffa_dev);
1509 			continue;
1510 		}
1511 	}
1512 
1513 	kfree(pbuf);
1514 
1515 	/* Check if the host is already added as part of partition info */
1516 	if (xa_load(&drv_info->partition_info, drv_info->vm_id))
1517 		return 0;
1518 
1519 	/* Allocate for the host */
1520 	ret = ffa_xa_add_partition_info(drv_info->vm_id);
1521 	if (ret)
1522 		ffa_partitions_cleanup();
1523 
1524 	return ret;
1525 }
1526 
1527 /* FFA FEATURE IDs */
1528 #define FFA_FEAT_NOTIFICATION_PENDING_INT	(1)
1529 #define FFA_FEAT_SCHEDULE_RECEIVER_INT		(2)
1530 #define FFA_FEAT_MANAGED_EXIT_INT		(3)
1531 
1532 static irqreturn_t ffa_sched_recv_irq_handler(int irq, void *irq_data)
1533 {
1534 	struct ffa_pcpu_irq *pcpu = irq_data;
1535 	struct ffa_drv_info *info = pcpu->info;
1536 
1537 	queue_work(info->notif_pcpu_wq, &info->sched_recv_irq_work);
1538 
1539 	return IRQ_HANDLED;
1540 }
1541 
1542 static irqreturn_t notif_pend_irq_handler(int irq, void *irq_data)
1543 {
1544 	struct ffa_pcpu_irq *pcpu = irq_data;
1545 	struct ffa_drv_info *info = pcpu->info;
1546 
1547 	queue_work_on(smp_processor_id(), info->notif_pcpu_wq,
1548 		      &info->notif_pcpu_work);
1549 
1550 	return IRQ_HANDLED;
1551 }
1552 
1553 static void ffa_sched_recv_irq_work_fn(struct work_struct *work)
1554 {
1555 	ffa_notification_info_get();
1556 }
1557 
1558 static int ffa_irq_map(u32 id)
1559 {
1560 	char *err_str;
1561 	int ret, irq, intid;
1562 
1563 	if (id == FFA_FEAT_NOTIFICATION_PENDING_INT)
1564 		err_str = "Notification Pending Interrupt";
1565 	else if (id == FFA_FEAT_SCHEDULE_RECEIVER_INT)
1566 		err_str = "Schedule Receiver Interrupt";
1567 	else
1568 		err_str = "Unknown ID";
1569 
1570 	/* The returned intid is assumed to be SGI donated to NS world */
1571 	ret = ffa_features(id, 0, &intid, NULL);
1572 	if (ret < 0) {
1573 		if (ret != -EOPNOTSUPP)
1574 			pr_err("Failed to retrieve FF-A %s %u\n", err_str, id);
1575 		return ret;
1576 	}
1577 
1578 	if (acpi_disabled) {
1579 		struct of_phandle_args oirq = {};
1580 		struct device_node *gic;
1581 
1582 		/* Only GICv3 supported currently with the device tree */
1583 		gic = of_find_compatible_node(NULL, NULL, "arm,gic-v3");
1584 		if (!gic)
1585 			return -ENXIO;
1586 
1587 		oirq.np = gic;
1588 		oirq.args_count = 1;
1589 		oirq.args[0] = intid;
1590 		irq = irq_create_of_mapping(&oirq);
1591 		of_node_put(gic);
1592 #ifdef CONFIG_ACPI
1593 	} else {
1594 		irq = acpi_register_gsi(NULL, intid, ACPI_EDGE_SENSITIVE,
1595 					ACPI_ACTIVE_HIGH);
1596 #endif
1597 	}
1598 
1599 	if (irq <= 0) {
1600 		pr_err("Failed to create IRQ mapping!\n");
1601 		return -ENODATA;
1602 	}
1603 
1604 	return irq;
1605 }
1606 
1607 static void ffa_irq_unmap(unsigned int irq)
1608 {
1609 	if (!irq)
1610 		return;
1611 	irq_dispose_mapping(irq);
1612 }
1613 
1614 static int ffa_cpuhp_pcpu_irq_enable(unsigned int cpu)
1615 {
1616 	if (drv_info->sched_recv_irq)
1617 		enable_percpu_irq(drv_info->sched_recv_irq, IRQ_TYPE_NONE);
1618 	if (drv_info->notif_pend_irq)
1619 		enable_percpu_irq(drv_info->notif_pend_irq, IRQ_TYPE_NONE);
1620 	return 0;
1621 }
1622 
1623 static int ffa_cpuhp_pcpu_irq_disable(unsigned int cpu)
1624 {
1625 	if (drv_info->sched_recv_irq)
1626 		disable_percpu_irq(drv_info->sched_recv_irq);
1627 	if (drv_info->notif_pend_irq)
1628 		disable_percpu_irq(drv_info->notif_pend_irq);
1629 	return 0;
1630 }
1631 
1632 static void ffa_uninit_pcpu_irq(void)
1633 {
1634 	if (drv_info->cpuhp_state) {
1635 		cpuhp_remove_state(drv_info->cpuhp_state);
1636 		drv_info->cpuhp_state = 0;
1637 	}
1638 
1639 	if (drv_info->notif_pcpu_wq) {
1640 		destroy_workqueue(drv_info->notif_pcpu_wq);
1641 		drv_info->notif_pcpu_wq = NULL;
1642 	}
1643 
1644 	if (drv_info->sched_recv_irq)
1645 		free_percpu_irq(drv_info->sched_recv_irq, drv_info->irq_pcpu);
1646 
1647 	if (drv_info->notif_pend_irq)
1648 		free_percpu_irq(drv_info->notif_pend_irq, drv_info->irq_pcpu);
1649 
1650 	if (drv_info->irq_pcpu) {
1651 		free_percpu(drv_info->irq_pcpu);
1652 		drv_info->irq_pcpu = NULL;
1653 	}
1654 }
1655 
1656 static int ffa_init_pcpu_irq(void)
1657 {
1658 	struct ffa_pcpu_irq __percpu *irq_pcpu;
1659 	int ret, cpu;
1660 
1661 	irq_pcpu = alloc_percpu(struct ffa_pcpu_irq);
1662 	if (!irq_pcpu)
1663 		return -ENOMEM;
1664 
1665 	for_each_present_cpu(cpu)
1666 		per_cpu_ptr(irq_pcpu, cpu)->info = drv_info;
1667 
1668 	drv_info->irq_pcpu = irq_pcpu;
1669 
1670 	if (drv_info->sched_recv_irq) {
1671 		ret = request_percpu_irq(drv_info->sched_recv_irq,
1672 					 ffa_sched_recv_irq_handler,
1673 					 "ARM-FFA-SRI", irq_pcpu);
1674 		if (ret) {
1675 			pr_err("Error registering percpu SRI nIRQ %d : %d\n",
1676 			       drv_info->sched_recv_irq, ret);
1677 			drv_info->sched_recv_irq = 0;
1678 			return ret;
1679 		}
1680 	}
1681 
1682 	if (drv_info->notif_pend_irq) {
1683 		ret = request_percpu_irq(drv_info->notif_pend_irq,
1684 					 notif_pend_irq_handler,
1685 					 "ARM-FFA-NPI", irq_pcpu);
1686 		if (ret) {
1687 			pr_err("Error registering percpu NPI nIRQ %d : %d\n",
1688 			       drv_info->notif_pend_irq, ret);
1689 			drv_info->notif_pend_irq = 0;
1690 			return ret;
1691 		}
1692 	}
1693 
1694 	INIT_WORK(&drv_info->sched_recv_irq_work, ffa_sched_recv_irq_work_fn);
1695 	INIT_WORK(&drv_info->notif_pcpu_work, notif_pcpu_irq_work_fn);
1696 	drv_info->notif_pcpu_wq = create_workqueue("ffa_pcpu_irq_notification");
1697 	if (!drv_info->notif_pcpu_wq)
1698 		return -EINVAL;
1699 
1700 	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "ffa/pcpu-irq:starting",
1701 				ffa_cpuhp_pcpu_irq_enable,
1702 				ffa_cpuhp_pcpu_irq_disable);
1703 
1704 	if (ret < 0)
1705 		return ret;
1706 
1707 	drv_info->cpuhp_state = ret;
1708 	return 0;
1709 }
1710 
1711 static void ffa_notifications_cleanup(void)
1712 {
1713 	ffa_uninit_pcpu_irq();
1714 	ffa_irq_unmap(drv_info->sched_recv_irq);
1715 	drv_info->sched_recv_irq = 0;
1716 	ffa_irq_unmap(drv_info->notif_pend_irq);
1717 	drv_info->notif_pend_irq = 0;
1718 
1719 	if (drv_info->bitmap_created) {
1720 		ffa_notification_bitmap_destroy();
1721 		drv_info->bitmap_created = false;
1722 	}
1723 	drv_info->notif_enabled = false;
1724 }
1725 
1726 static void ffa_notifications_setup(void)
1727 {
1728 	int ret;
1729 
1730 	ret = ffa_features(FFA_NOTIFICATION_BITMAP_CREATE, 0, NULL, NULL);
1731 	if (!ret) {
1732 		ret = ffa_notification_bitmap_create();
1733 		if (ret) {
1734 			pr_err("Notification bitmap create error %d\n", ret);
1735 			return;
1736 		}
1737 
1738 		drv_info->bitmap_created = true;
1739 	}
1740 
1741 	ret = ffa_irq_map(FFA_FEAT_SCHEDULE_RECEIVER_INT);
1742 	if (ret > 0)
1743 		drv_info->sched_recv_irq = ret;
1744 
1745 	ret = ffa_irq_map(FFA_FEAT_NOTIFICATION_PENDING_INT);
1746 	if (ret > 0)
1747 		drv_info->notif_pend_irq = ret;
1748 
1749 	if (!drv_info->sched_recv_irq && !drv_info->notif_pend_irq)
1750 		goto cleanup;
1751 
1752 	ret = ffa_init_pcpu_irq();
1753 	if (ret)
1754 		goto cleanup;
1755 
1756 	hash_init(drv_info->notifier_hash);
1757 	mutex_init(&drv_info->notify_lock);
1758 
1759 	drv_info->notif_enabled = true;
1760 	return;
1761 cleanup:
1762 	pr_info("Notification setup failed %d, not enabled\n", ret);
1763 	ffa_notifications_cleanup();
1764 }
1765 
1766 static int __init ffa_init(void)
1767 {
1768 	int ret;
1769 	u32 buf_sz;
1770 	size_t rxtx_bufsz = SZ_4K;
1771 
1772 	ret = ffa_transport_init(&invoke_ffa_fn);
1773 	if (ret)
1774 		return ret;
1775 
1776 	drv_info = kzalloc(sizeof(*drv_info), GFP_KERNEL);
1777 	if (!drv_info)
1778 		return -ENOMEM;
1779 
1780 	ret = ffa_version_check(&drv_info->version);
1781 	if (ret)
1782 		goto free_drv_info;
1783 
1784 	if (ffa_id_get(&drv_info->vm_id)) {
1785 		pr_err("failed to obtain VM id for self\n");
1786 		ret = -ENODEV;
1787 		goto free_drv_info;
1788 	}
1789 
1790 	ret = ffa_features(FFA_FN_NATIVE(RXTX_MAP), 0, &buf_sz, NULL);
1791 	if (!ret) {
1792 		if (RXTX_MAP_MIN_BUFSZ(buf_sz) == 1)
1793 			rxtx_bufsz = SZ_64K;
1794 		else if (RXTX_MAP_MIN_BUFSZ(buf_sz) == 2)
1795 			rxtx_bufsz = SZ_16K;
1796 		else
1797 			rxtx_bufsz = SZ_4K;
1798 	}
1799 
1800 	drv_info->rxtx_bufsz = rxtx_bufsz;
1801 	drv_info->rx_buffer = alloc_pages_exact(rxtx_bufsz, GFP_KERNEL);
1802 	if (!drv_info->rx_buffer) {
1803 		ret = -ENOMEM;
1804 		goto free_pages;
1805 	}
1806 
1807 	drv_info->tx_buffer = alloc_pages_exact(rxtx_bufsz, GFP_KERNEL);
1808 	if (!drv_info->tx_buffer) {
1809 		ret = -ENOMEM;
1810 		goto free_pages;
1811 	}
1812 
1813 	ret = ffa_rxtx_map(virt_to_phys(drv_info->tx_buffer),
1814 			   virt_to_phys(drv_info->rx_buffer),
1815 			   rxtx_bufsz / FFA_PAGE_SIZE);
1816 	if (ret) {
1817 		pr_err("failed to register FFA RxTx buffers\n");
1818 		goto free_pages;
1819 	}
1820 
1821 	mutex_init(&drv_info->rx_lock);
1822 	mutex_init(&drv_info->tx_lock);
1823 
1824 	ffa_drvinfo_flags_init();
1825 
1826 	ffa_notifications_setup();
1827 
1828 	ret = ffa_setup_partitions();
1829 	if (ret) {
1830 		pr_err("failed to setup partitions\n");
1831 		goto cleanup_notifs;
1832 	}
1833 
1834 	ret = ffa_sched_recv_cb_update(drv_info->vm_id, ffa_self_notif_handle,
1835 				       drv_info, true);
1836 	if (ret)
1837 		pr_info("Failed to register driver sched callback %d\n", ret);
1838 
1839 	return 0;
1840 
1841 cleanup_notifs:
1842 	ffa_notifications_cleanup();
1843 free_pages:
1844 	if (drv_info->tx_buffer)
1845 		free_pages_exact(drv_info->tx_buffer, rxtx_bufsz);
1846 	free_pages_exact(drv_info->rx_buffer, rxtx_bufsz);
1847 free_drv_info:
1848 	kfree(drv_info);
1849 	return ret;
1850 }
1851 module_init(ffa_init);
1852 
1853 static void __exit ffa_exit(void)
1854 {
1855 	ffa_notifications_cleanup();
1856 	ffa_partitions_cleanup();
1857 	ffa_rxtx_unmap(drv_info->vm_id);
1858 	free_pages_exact(drv_info->tx_buffer, drv_info->rxtx_bufsz);
1859 	free_pages_exact(drv_info->rx_buffer, drv_info->rxtx_bufsz);
1860 	kfree(drv_info);
1861 }
1862 module_exit(ffa_exit);
1863 
1864 MODULE_ALIAS("arm-ffa");
1865 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
1866 MODULE_DESCRIPTION("Arm FF-A interface driver");
1867 MODULE_LICENSE("GPL v2");
1868