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