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