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