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/arm_ffa.h> 26 #include <linux/bitfield.h> 27 #include <linux/device.h> 28 #include <linux/io.h> 29 #include <linux/kernel.h> 30 #include <linux/module.h> 31 #include <linux/mm.h> 32 #include <linux/scatterlist.h> 33 #include <linux/slab.h> 34 #include <linux/uuid.h> 35 36 #include "common.h" 37 38 #define FFA_DRIVER_VERSION FFA_VERSION_1_0 39 40 #define FFA_SMC(calling_convention, func_num) \ 41 ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, (calling_convention), \ 42 ARM_SMCCC_OWNER_STANDARD, (func_num)) 43 44 #define FFA_SMC_32(func_num) FFA_SMC(ARM_SMCCC_SMC_32, (func_num)) 45 #define FFA_SMC_64(func_num) FFA_SMC(ARM_SMCCC_SMC_64, (func_num)) 46 47 #define FFA_ERROR FFA_SMC_32(0x60) 48 #define FFA_SUCCESS FFA_SMC_32(0x61) 49 #define FFA_INTERRUPT FFA_SMC_32(0x62) 50 #define FFA_VERSION FFA_SMC_32(0x63) 51 #define FFA_FEATURES FFA_SMC_32(0x64) 52 #define FFA_RX_RELEASE FFA_SMC_32(0x65) 53 #define FFA_RXTX_MAP FFA_SMC_32(0x66) 54 #define FFA_FN64_RXTX_MAP FFA_SMC_64(0x66) 55 #define FFA_RXTX_UNMAP FFA_SMC_32(0x67) 56 #define FFA_PARTITION_INFO_GET FFA_SMC_32(0x68) 57 #define FFA_ID_GET FFA_SMC_32(0x69) 58 #define FFA_MSG_POLL FFA_SMC_32(0x6A) 59 #define FFA_MSG_WAIT FFA_SMC_32(0x6B) 60 #define FFA_YIELD FFA_SMC_32(0x6C) 61 #define FFA_RUN FFA_SMC_32(0x6D) 62 #define FFA_MSG_SEND FFA_SMC_32(0x6E) 63 #define FFA_MSG_SEND_DIRECT_REQ FFA_SMC_32(0x6F) 64 #define FFA_FN64_MSG_SEND_DIRECT_REQ FFA_SMC_64(0x6F) 65 #define FFA_MSG_SEND_DIRECT_RESP FFA_SMC_32(0x70) 66 #define FFA_FN64_MSG_SEND_DIRECT_RESP FFA_SMC_64(0x70) 67 #define FFA_MEM_DONATE FFA_SMC_32(0x71) 68 #define FFA_FN64_MEM_DONATE FFA_SMC_64(0x71) 69 #define FFA_MEM_LEND FFA_SMC_32(0x72) 70 #define FFA_FN64_MEM_LEND FFA_SMC_64(0x72) 71 #define FFA_MEM_SHARE FFA_SMC_32(0x73) 72 #define FFA_FN64_MEM_SHARE FFA_SMC_64(0x73) 73 #define FFA_MEM_RETRIEVE_REQ FFA_SMC_32(0x74) 74 #define FFA_FN64_MEM_RETRIEVE_REQ FFA_SMC_64(0x74) 75 #define FFA_MEM_RETRIEVE_RESP FFA_SMC_32(0x75) 76 #define FFA_MEM_RELINQUISH FFA_SMC_32(0x76) 77 #define FFA_MEM_RECLAIM FFA_SMC_32(0x77) 78 #define FFA_MEM_OP_PAUSE FFA_SMC_32(0x78) 79 #define FFA_MEM_OP_RESUME FFA_SMC_32(0x79) 80 #define FFA_MEM_FRAG_RX FFA_SMC_32(0x7A) 81 #define FFA_MEM_FRAG_TX FFA_SMC_32(0x7B) 82 #define FFA_NORMAL_WORLD_RESUME FFA_SMC_32(0x7C) 83 84 /* 85 * For some calls it is necessary to use SMC64 to pass or return 64-bit values. 86 * For such calls FFA_FN_NATIVE(name) will choose the appropriate 87 * (native-width) function ID. 88 */ 89 #ifdef CONFIG_64BIT 90 #define FFA_FN_NATIVE(name) FFA_FN64_##name 91 #else 92 #define FFA_FN_NATIVE(name) FFA_##name 93 #endif 94 95 /* FFA error codes. */ 96 #define FFA_RET_SUCCESS (0) 97 #define FFA_RET_NOT_SUPPORTED (-1) 98 #define FFA_RET_INVALID_PARAMETERS (-2) 99 #define FFA_RET_NO_MEMORY (-3) 100 #define FFA_RET_BUSY (-4) 101 #define FFA_RET_INTERRUPTED (-5) 102 #define FFA_RET_DENIED (-6) 103 #define FFA_RET_RETRY (-7) 104 #define FFA_RET_ABORTED (-8) 105 106 #define MAJOR_VERSION_MASK GENMASK(30, 16) 107 #define MINOR_VERSION_MASK GENMASK(15, 0) 108 #define MAJOR_VERSION(x) ((u16)(FIELD_GET(MAJOR_VERSION_MASK, (x)))) 109 #define MINOR_VERSION(x) ((u16)(FIELD_GET(MINOR_VERSION_MASK, (x)))) 110 #define PACK_VERSION_INFO(major, minor) \ 111 (FIELD_PREP(MAJOR_VERSION_MASK, (major)) | \ 112 FIELD_PREP(MINOR_VERSION_MASK, (minor))) 113 #define FFA_VERSION_1_0 PACK_VERSION_INFO(1, 0) 114 #define FFA_MIN_VERSION FFA_VERSION_1_0 115 116 #define SENDER_ID_MASK GENMASK(31, 16) 117 #define RECEIVER_ID_MASK GENMASK(15, 0) 118 #define SENDER_ID(x) ((u16)(FIELD_GET(SENDER_ID_MASK, (x)))) 119 #define RECEIVER_ID(x) ((u16)(FIELD_GET(RECEIVER_ID_MASK, (x)))) 120 #define PACK_TARGET_INFO(s, r) \ 121 (FIELD_PREP(SENDER_ID_MASK, (s)) | FIELD_PREP(RECEIVER_ID_MASK, (r))) 122 123 /* 124 * FF-A specification mentions explicitly about '4K pages'. This should 125 * not be confused with the kernel PAGE_SIZE, which is the translation 126 * granule kernel is configured and may be one among 4K, 16K and 64K. 127 */ 128 #define FFA_PAGE_SIZE SZ_4K 129 /* 130 * Keeping RX TX buffer size as 4K for now 131 * 64K may be preferred to keep it min a page in 64K PAGE_SIZE config 132 */ 133 #define RXTX_BUFFER_SIZE SZ_4K 134 135 static ffa_fn *invoke_ffa_fn; 136 137 static const int ffa_linux_errmap[] = { 138 /* better than switch case as long as return value is continuous */ 139 0, /* FFA_RET_SUCCESS */ 140 -EOPNOTSUPP, /* FFA_RET_NOT_SUPPORTED */ 141 -EINVAL, /* FFA_RET_INVALID_PARAMETERS */ 142 -ENOMEM, /* FFA_RET_NO_MEMORY */ 143 -EBUSY, /* FFA_RET_BUSY */ 144 -EINTR, /* FFA_RET_INTERRUPTED */ 145 -EACCES, /* FFA_RET_DENIED */ 146 -EAGAIN, /* FFA_RET_RETRY */ 147 -ECANCELED, /* FFA_RET_ABORTED */ 148 }; 149 150 static inline int ffa_to_linux_errno(int errno) 151 { 152 int err_idx = -errno; 153 154 if (err_idx >= 0 && err_idx < ARRAY_SIZE(ffa_linux_errmap)) 155 return ffa_linux_errmap[err_idx]; 156 return -EINVAL; 157 } 158 159 struct ffa_drv_info { 160 u32 version; 161 u16 vm_id; 162 struct mutex rx_lock; /* lock to protect Rx buffer */ 163 struct mutex tx_lock; /* lock to protect Tx buffer */ 164 void *rx_buffer; 165 void *tx_buffer; 166 }; 167 168 static struct ffa_drv_info *drv_info; 169 170 static int ffa_version_check(u32 *version) 171 { 172 ffa_value_t ver; 173 174 invoke_ffa_fn((ffa_value_t){ 175 .a0 = FFA_VERSION, .a1 = FFA_DRIVER_VERSION, 176 }, &ver); 177 178 if (ver.a0 == FFA_RET_NOT_SUPPORTED) { 179 pr_info("FFA_VERSION returned not supported\n"); 180 return -EOPNOTSUPP; 181 } 182 183 if (ver.a0 < FFA_MIN_VERSION || ver.a0 > FFA_DRIVER_VERSION) { 184 pr_err("Incompatible version %d.%d found\n", 185 MAJOR_VERSION(ver.a0), MINOR_VERSION(ver.a0)); 186 return -EINVAL; 187 } 188 189 *version = ver.a0; 190 pr_info("Version %d.%d found\n", MAJOR_VERSION(ver.a0), 191 MINOR_VERSION(ver.a0)); 192 return 0; 193 } 194 195 static int ffa_rx_release(void) 196 { 197 ffa_value_t ret; 198 199 invoke_ffa_fn((ffa_value_t){ 200 .a0 = FFA_RX_RELEASE, 201 }, &ret); 202 203 if (ret.a0 == FFA_ERROR) 204 return ffa_to_linux_errno((int)ret.a2); 205 206 /* check for ret.a0 == FFA_RX_RELEASE ? */ 207 208 return 0; 209 } 210 211 static int ffa_rxtx_map(phys_addr_t tx_buf, phys_addr_t rx_buf, u32 pg_cnt) 212 { 213 ffa_value_t ret; 214 215 invoke_ffa_fn((ffa_value_t){ 216 .a0 = FFA_FN_NATIVE(RXTX_MAP), 217 .a1 = tx_buf, .a2 = rx_buf, .a3 = pg_cnt, 218 }, &ret); 219 220 if (ret.a0 == FFA_ERROR) 221 return ffa_to_linux_errno((int)ret.a2); 222 223 return 0; 224 } 225 226 static int ffa_rxtx_unmap(u16 vm_id) 227 { 228 ffa_value_t ret; 229 230 invoke_ffa_fn((ffa_value_t){ 231 .a0 = FFA_RXTX_UNMAP, .a1 = PACK_TARGET_INFO(vm_id, 0), 232 }, &ret); 233 234 if (ret.a0 == FFA_ERROR) 235 return ffa_to_linux_errno((int)ret.a2); 236 237 return 0; 238 } 239 240 /* buffer must be sizeof(struct ffa_partition_info) * num_partitions */ 241 static int 242 __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3, 243 struct ffa_partition_info *buffer, int num_partitions) 244 { 245 int count; 246 ffa_value_t partition_info; 247 248 mutex_lock(&drv_info->rx_lock); 249 invoke_ffa_fn((ffa_value_t){ 250 .a0 = FFA_PARTITION_INFO_GET, 251 .a1 = uuid0, .a2 = uuid1, .a3 = uuid2, .a4 = uuid3, 252 }, &partition_info); 253 254 if (partition_info.a0 == FFA_ERROR) { 255 mutex_unlock(&drv_info->rx_lock); 256 return ffa_to_linux_errno((int)partition_info.a2); 257 } 258 259 count = partition_info.a2; 260 261 if (buffer && count <= num_partitions) 262 memcpy(buffer, drv_info->rx_buffer, sizeof(*buffer) * count); 263 264 ffa_rx_release(); 265 266 mutex_unlock(&drv_info->rx_lock); 267 268 return count; 269 } 270 271 /* buffer is allocated and caller must free the same if returned count > 0 */ 272 static int 273 ffa_partition_probe(const uuid_t *uuid, struct ffa_partition_info **buffer) 274 { 275 int count; 276 u32 uuid0_4[4]; 277 struct ffa_partition_info *pbuf; 278 279 export_uuid((u8 *)uuid0_4, uuid); 280 count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1], uuid0_4[2], 281 uuid0_4[3], NULL, 0); 282 if (count <= 0) 283 return count; 284 285 pbuf = kcalloc(count, sizeof(*pbuf), GFP_KERNEL); 286 if (!pbuf) 287 return -ENOMEM; 288 289 count = __ffa_partition_info_get(uuid0_4[0], uuid0_4[1], uuid0_4[2], 290 uuid0_4[3], pbuf, count); 291 if (count <= 0) 292 kfree(pbuf); 293 else 294 *buffer = pbuf; 295 296 return count; 297 } 298 299 #define VM_ID_MASK GENMASK(15, 0) 300 static int ffa_id_get(u16 *vm_id) 301 { 302 ffa_value_t id; 303 304 invoke_ffa_fn((ffa_value_t){ 305 .a0 = FFA_ID_GET, 306 }, &id); 307 308 if (id.a0 == FFA_ERROR) 309 return ffa_to_linux_errno((int)id.a2); 310 311 *vm_id = FIELD_GET(VM_ID_MASK, (id.a2)); 312 313 return 0; 314 } 315 316 static int ffa_msg_send_direct_req(u16 src_id, u16 dst_id, bool mode_32bit, 317 struct ffa_send_direct_data *data) 318 { 319 u32 req_id, resp_id, src_dst_ids = PACK_TARGET_INFO(src_id, dst_id); 320 ffa_value_t ret; 321 322 if (mode_32bit) { 323 req_id = FFA_MSG_SEND_DIRECT_REQ; 324 resp_id = FFA_MSG_SEND_DIRECT_RESP; 325 } else { 326 req_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_REQ); 327 resp_id = FFA_FN_NATIVE(MSG_SEND_DIRECT_RESP); 328 } 329 330 invoke_ffa_fn((ffa_value_t){ 331 .a0 = req_id, .a1 = src_dst_ids, .a2 = 0, 332 .a3 = data->data0, .a4 = data->data1, .a5 = data->data2, 333 .a6 = data->data3, .a7 = data->data4, 334 }, &ret); 335 336 while (ret.a0 == FFA_INTERRUPT) 337 invoke_ffa_fn((ffa_value_t){ 338 .a0 = FFA_RUN, .a1 = ret.a1, 339 }, &ret); 340 341 if (ret.a0 == FFA_ERROR) 342 return ffa_to_linux_errno((int)ret.a2); 343 344 if (ret.a0 == resp_id) { 345 data->data0 = ret.a3; 346 data->data1 = ret.a4; 347 data->data2 = ret.a5; 348 data->data3 = ret.a6; 349 data->data4 = ret.a7; 350 return 0; 351 } 352 353 return -EINVAL; 354 } 355 356 static int ffa_mem_first_frag(u32 func_id, phys_addr_t buf, u32 buf_sz, 357 u32 frag_len, u32 len, u64 *handle) 358 { 359 ffa_value_t ret; 360 361 invoke_ffa_fn((ffa_value_t){ 362 .a0 = func_id, .a1 = len, .a2 = frag_len, 363 .a3 = buf, .a4 = buf_sz, 364 }, &ret); 365 366 while (ret.a0 == FFA_MEM_OP_PAUSE) 367 invoke_ffa_fn((ffa_value_t){ 368 .a0 = FFA_MEM_OP_RESUME, 369 .a1 = ret.a1, .a2 = ret.a2, 370 }, &ret); 371 372 if (ret.a0 == FFA_ERROR) 373 return ffa_to_linux_errno((int)ret.a2); 374 375 if (ret.a0 != FFA_SUCCESS) 376 return -EOPNOTSUPP; 377 378 if (handle) 379 *handle = PACK_HANDLE(ret.a2, ret.a3); 380 381 return frag_len; 382 } 383 384 static int ffa_mem_next_frag(u64 handle, u32 frag_len) 385 { 386 ffa_value_t ret; 387 388 invoke_ffa_fn((ffa_value_t){ 389 .a0 = FFA_MEM_FRAG_TX, 390 .a1 = HANDLE_LOW(handle), .a2 = HANDLE_HIGH(handle), 391 .a3 = frag_len, 392 }, &ret); 393 394 while (ret.a0 == FFA_MEM_OP_PAUSE) 395 invoke_ffa_fn((ffa_value_t){ 396 .a0 = FFA_MEM_OP_RESUME, 397 .a1 = ret.a1, .a2 = ret.a2, 398 }, &ret); 399 400 if (ret.a0 == FFA_ERROR) 401 return ffa_to_linux_errno((int)ret.a2); 402 403 if (ret.a0 != FFA_MEM_FRAG_RX) 404 return -EOPNOTSUPP; 405 406 return ret.a3; 407 } 408 409 static int 410 ffa_transmit_fragment(u32 func_id, phys_addr_t buf, u32 buf_sz, u32 frag_len, 411 u32 len, u64 *handle, bool first) 412 { 413 if (!first) 414 return ffa_mem_next_frag(*handle, frag_len); 415 416 return ffa_mem_first_frag(func_id, buf, buf_sz, frag_len, len, handle); 417 } 418 419 static u32 ffa_get_num_pages_sg(struct scatterlist *sg) 420 { 421 u32 num_pages = 0; 422 423 do { 424 num_pages += sg->length / FFA_PAGE_SIZE; 425 } while ((sg = sg_next(sg))); 426 427 return num_pages; 428 } 429 430 static int 431 ffa_setup_and_transmit(u32 func_id, void *buffer, u32 max_fragsize, 432 struct ffa_mem_ops_args *args) 433 { 434 int rc = 0; 435 bool first = true; 436 phys_addr_t addr = 0; 437 struct ffa_composite_mem_region *composite; 438 struct ffa_mem_region_addr_range *constituents; 439 struct ffa_mem_region_attributes *ep_mem_access; 440 struct ffa_mem_region *mem_region = buffer; 441 u32 idx, frag_len, length, buf_sz = 0, num_entries = sg_nents(args->sg); 442 443 mem_region->tag = args->tag; 444 mem_region->flags = args->flags; 445 mem_region->sender_id = drv_info->vm_id; 446 mem_region->attributes = FFA_MEM_NORMAL | FFA_MEM_WRITE_BACK | 447 FFA_MEM_INNER_SHAREABLE; 448 ep_mem_access = &mem_region->ep_mem_access[0]; 449 450 for (idx = 0; idx < args->nattrs; idx++, ep_mem_access++) { 451 ep_mem_access->receiver = args->attrs[idx].receiver; 452 ep_mem_access->attrs = args->attrs[idx].attrs; 453 ep_mem_access->composite_off = COMPOSITE_OFFSET(args->nattrs); 454 } 455 mem_region->ep_count = args->nattrs; 456 457 composite = buffer + COMPOSITE_OFFSET(args->nattrs); 458 composite->total_pg_cnt = ffa_get_num_pages_sg(args->sg); 459 composite->addr_range_cnt = num_entries; 460 461 length = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, num_entries); 462 frag_len = COMPOSITE_CONSTITUENTS_OFFSET(args->nattrs, 0); 463 if (frag_len > max_fragsize) 464 return -ENXIO; 465 466 if (!args->use_txbuf) { 467 addr = virt_to_phys(buffer); 468 buf_sz = max_fragsize / FFA_PAGE_SIZE; 469 } 470 471 constituents = buffer + frag_len; 472 idx = 0; 473 do { 474 if (frag_len == max_fragsize) { 475 rc = ffa_transmit_fragment(func_id, addr, buf_sz, 476 frag_len, length, 477 &args->g_handle, first); 478 if (rc < 0) 479 return -ENXIO; 480 481 first = false; 482 idx = 0; 483 frag_len = 0; 484 constituents = buffer; 485 } 486 487 if ((void *)constituents - buffer > max_fragsize) { 488 pr_err("Memory Region Fragment > Tx Buffer size\n"); 489 return -EFAULT; 490 } 491 492 constituents->address = sg_phys(args->sg); 493 constituents->pg_cnt = args->sg->length / FFA_PAGE_SIZE; 494 constituents++; 495 frag_len += sizeof(struct ffa_mem_region_addr_range); 496 } while ((args->sg = sg_next(args->sg))); 497 498 return ffa_transmit_fragment(func_id, addr, buf_sz, frag_len, 499 length, &args->g_handle, first); 500 } 501 502 static int ffa_memory_ops(u32 func_id, struct ffa_mem_ops_args *args) 503 { 504 int ret; 505 void *buffer; 506 507 if (!args->use_txbuf) { 508 buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL); 509 if (!buffer) 510 return -ENOMEM; 511 } else { 512 buffer = drv_info->tx_buffer; 513 mutex_lock(&drv_info->tx_lock); 514 } 515 516 ret = ffa_setup_and_transmit(func_id, buffer, RXTX_BUFFER_SIZE, args); 517 518 if (args->use_txbuf) 519 mutex_unlock(&drv_info->tx_lock); 520 else 521 free_pages_exact(buffer, RXTX_BUFFER_SIZE); 522 523 return ret < 0 ? ret : 0; 524 } 525 526 static int ffa_memory_reclaim(u64 g_handle, u32 flags) 527 { 528 ffa_value_t ret; 529 530 invoke_ffa_fn((ffa_value_t){ 531 .a0 = FFA_MEM_RECLAIM, 532 .a1 = HANDLE_LOW(g_handle), .a2 = HANDLE_HIGH(g_handle), 533 .a3 = flags, 534 }, &ret); 535 536 if (ret.a0 == FFA_ERROR) 537 return ffa_to_linux_errno((int)ret.a2); 538 539 return 0; 540 } 541 542 static u32 ffa_api_version_get(void) 543 { 544 return drv_info->version; 545 } 546 547 static int ffa_partition_info_get(const char *uuid_str, 548 struct ffa_partition_info *buffer) 549 { 550 int count; 551 uuid_t uuid; 552 struct ffa_partition_info *pbuf; 553 554 if (uuid_parse(uuid_str, &uuid)) { 555 pr_err("invalid uuid (%s)\n", uuid_str); 556 return -ENODEV; 557 } 558 559 count = ffa_partition_probe(&uuid_null, &pbuf); 560 if (count <= 0) 561 return -ENOENT; 562 563 memcpy(buffer, pbuf, sizeof(*pbuf) * count); 564 kfree(pbuf); 565 return 0; 566 } 567 568 static void ffa_mode_32bit_set(struct ffa_device *dev) 569 { 570 dev->mode_32bit = true; 571 } 572 573 static int ffa_sync_send_receive(struct ffa_device *dev, 574 struct ffa_send_direct_data *data) 575 { 576 return ffa_msg_send_direct_req(drv_info->vm_id, dev->vm_id, 577 dev->mode_32bit, data); 578 } 579 580 static int 581 ffa_memory_share(struct ffa_device *dev, struct ffa_mem_ops_args *args) 582 { 583 if (dev->mode_32bit) 584 return ffa_memory_ops(FFA_MEM_SHARE, args); 585 586 return ffa_memory_ops(FFA_FN_NATIVE(MEM_SHARE), args); 587 } 588 589 static const struct ffa_dev_ops ffa_ops = { 590 .api_version_get = ffa_api_version_get, 591 .partition_info_get = ffa_partition_info_get, 592 .mode_32bit_set = ffa_mode_32bit_set, 593 .sync_send_receive = ffa_sync_send_receive, 594 .memory_reclaim = ffa_memory_reclaim, 595 .memory_share = ffa_memory_share, 596 }; 597 598 const struct ffa_dev_ops *ffa_dev_ops_get(struct ffa_device *dev) 599 { 600 if (ffa_device_is_valid(dev)) 601 return &ffa_ops; 602 603 return NULL; 604 } 605 EXPORT_SYMBOL_GPL(ffa_dev_ops_get); 606 607 void ffa_device_match_uuid(struct ffa_device *ffa_dev, const uuid_t *uuid) 608 { 609 int count, idx; 610 struct ffa_partition_info *pbuf, *tpbuf; 611 612 count = ffa_partition_probe(uuid, &pbuf); 613 if (count <= 0) 614 return; 615 616 for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++) 617 if (tpbuf->id == ffa_dev->vm_id) 618 uuid_copy(&ffa_dev->uuid, uuid); 619 kfree(pbuf); 620 } 621 622 static void ffa_setup_partitions(void) 623 { 624 int count, idx; 625 struct ffa_device *ffa_dev; 626 struct ffa_partition_info *pbuf, *tpbuf; 627 628 count = ffa_partition_probe(&uuid_null, &pbuf); 629 if (count <= 0) { 630 pr_info("%s: No partitions found, error %d\n", __func__, count); 631 return; 632 } 633 634 for (idx = 0, tpbuf = pbuf; idx < count; idx++, tpbuf++) { 635 /* Note that the &uuid_null parameter will require 636 * ffa_device_match() to find the UUID of this partition id 637 * with help of ffa_device_match_uuid(). Once the FF-A spec 638 * is updated to provide correct UUID here for each partition 639 * as part of the discovery API, we need to pass the 640 * discovered UUID here instead. 641 */ 642 ffa_dev = ffa_device_register(&uuid_null, tpbuf->id); 643 if (!ffa_dev) { 644 pr_err("%s: failed to register partition ID 0x%x\n", 645 __func__, tpbuf->id); 646 continue; 647 } 648 649 ffa_dev_set_drvdata(ffa_dev, drv_info); 650 } 651 kfree(pbuf); 652 } 653 654 static int __init ffa_init(void) 655 { 656 int ret; 657 658 ret = ffa_transport_init(&invoke_ffa_fn); 659 if (ret) 660 return ret; 661 662 ret = arm_ffa_bus_init(); 663 if (ret) 664 return ret; 665 666 drv_info = kzalloc(sizeof(*drv_info), GFP_KERNEL); 667 if (!drv_info) { 668 ret = -ENOMEM; 669 goto ffa_bus_exit; 670 } 671 672 ret = ffa_version_check(&drv_info->version); 673 if (ret) 674 goto free_drv_info; 675 676 if (ffa_id_get(&drv_info->vm_id)) { 677 pr_err("failed to obtain VM id for self\n"); 678 ret = -ENODEV; 679 goto free_drv_info; 680 } 681 682 drv_info->rx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL); 683 if (!drv_info->rx_buffer) { 684 ret = -ENOMEM; 685 goto free_pages; 686 } 687 688 drv_info->tx_buffer = alloc_pages_exact(RXTX_BUFFER_SIZE, GFP_KERNEL); 689 if (!drv_info->tx_buffer) { 690 ret = -ENOMEM; 691 goto free_pages; 692 } 693 694 ret = ffa_rxtx_map(virt_to_phys(drv_info->tx_buffer), 695 virt_to_phys(drv_info->rx_buffer), 696 RXTX_BUFFER_SIZE / FFA_PAGE_SIZE); 697 if (ret) { 698 pr_err("failed to register FFA RxTx buffers\n"); 699 goto free_pages; 700 } 701 702 mutex_init(&drv_info->rx_lock); 703 mutex_init(&drv_info->tx_lock); 704 705 ffa_setup_partitions(); 706 707 return 0; 708 free_pages: 709 if (drv_info->tx_buffer) 710 free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE); 711 free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE); 712 free_drv_info: 713 kfree(drv_info); 714 ffa_bus_exit: 715 arm_ffa_bus_exit(); 716 return ret; 717 } 718 subsys_initcall(ffa_init); 719 720 static void __exit ffa_exit(void) 721 { 722 ffa_rxtx_unmap(drv_info->vm_id); 723 free_pages_exact(drv_info->tx_buffer, RXTX_BUFFER_SIZE); 724 free_pages_exact(drv_info->rx_buffer, RXTX_BUFFER_SIZE); 725 kfree(drv_info); 726 arm_ffa_bus_exit(); 727 } 728 module_exit(ffa_exit); 729 730 MODULE_ALIAS("arm-ffa"); 731 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>"); 732 MODULE_DESCRIPTION("Arm FF-A interface driver"); 733 MODULE_LICENSE("GPL v2"); 734