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