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