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