File Index
Files and directories
Sign in
FILE

driver.c

File preview with syntax highlighting.

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