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