1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (c) 2011-2018, The Linux Foundation. All rights reserved. 3 // Copyright (c) 2018, Linaro Limited 4 5 #include <linux/completion.h> 6 #include <linux/device.h> 7 #include <linux/dma-buf.h> 8 #include <linux/dma-mapping.h> 9 #include <linux/dma-resv.h> 10 #include <linux/idr.h> 11 #include <linux/list.h> 12 #include <linux/miscdevice.h> 13 #include <linux/module.h> 14 #include <linux/of_address.h> 15 #include <linux/of.h> 16 #include <linux/platform_device.h> 17 #include <linux/sort.h> 18 #include <linux/of_platform.h> 19 #include <linux/rpmsg.h> 20 #include <linux/scatterlist.h> 21 #include <linux/slab.h> 22 #include <linux/firmware/qcom/qcom_scm.h> 23 #include <uapi/misc/fastrpc.h> 24 #include <linux/of_reserved_mem.h> 25 #include <linux/bits.h> 26 27 #define ADSP_DOMAIN_ID (0) 28 #define MDSP_DOMAIN_ID (1) 29 #define SDSP_DOMAIN_ID (2) 30 #define CDSP_DOMAIN_ID (3) 31 #define GDSP_DOMAIN_ID (4) 32 #define FASTRPC_MAX_SESSIONS 14 33 #define FASTRPC_MAX_VMIDS 16 34 #define FASTRPC_ALIGN 128 35 #define FASTRPC_MAX_FDLIST 16 36 #define FASTRPC_MAX_CRCLIST 64 37 #define FASTRPC_CTX_MAX (256) 38 #define FASTRPC_INIT_HANDLE 1 39 #define FASTRPC_DSP_UTILITIES_HANDLE 2 40 #define FASTRPC_CTXID_MASK (0xFF0) 41 #define INIT_FILELEN_MAX (2 * 1024 * 1024) 42 #define INIT_FILE_NAMELEN_MAX (128) 43 #define FASTRPC_DEVICE_NAME "fastrpc" 44 45 /* Add memory to static PD pool, protection thru XPU */ 46 #define ADSP_MMAP_HEAP_ADDR 4 47 /* MAP static DMA buffer on DSP User PD */ 48 #define ADSP_MMAP_DMA_BUFFER 6 49 /* Add memory to static PD pool protection thru hypervisor */ 50 #define ADSP_MMAP_REMOTE_HEAP_ADDR 8 51 /* Add memory to userPD pool, for user heap */ 52 #define ADSP_MMAP_ADD_PAGES 0x1000 53 /* Add memory to userPD pool, for LLC heap */ 54 #define ADSP_MMAP_ADD_PAGES_LLC 0x3000, 55 56 #define DSP_UNSUPPORTED_API (0x80000414) 57 /* MAX NUMBER of DSP ATTRIBUTES SUPPORTED */ 58 #define FASTRPC_MAX_DSP_ATTRIBUTES (256) 59 #define FASTRPC_MAX_DSP_ATTRIBUTES_LEN (sizeof(u32) * FASTRPC_MAX_DSP_ATTRIBUTES) 60 61 /* Retrives number of input buffers from the scalars parameter */ 62 #define REMOTE_SCALARS_INBUFS(sc) (((sc) >> 16) & 0x0ff) 63 64 /* Retrives number of output buffers from the scalars parameter */ 65 #define REMOTE_SCALARS_OUTBUFS(sc) (((sc) >> 8) & 0x0ff) 66 67 /* Retrives number of input handles from the scalars parameter */ 68 #define REMOTE_SCALARS_INHANDLES(sc) (((sc) >> 4) & 0x0f) 69 70 /* Retrives number of output handles from the scalars parameter */ 71 #define REMOTE_SCALARS_OUTHANDLES(sc) ((sc) & 0x0f) 72 73 #define REMOTE_SCALARS_LENGTH(sc) (REMOTE_SCALARS_INBUFS(sc) + \ 74 REMOTE_SCALARS_OUTBUFS(sc) + \ 75 REMOTE_SCALARS_INHANDLES(sc)+ \ 76 REMOTE_SCALARS_OUTHANDLES(sc)) 77 #define FASTRPC_BUILD_SCALARS(attr, method, in, out, oin, oout) \ 78 (((attr & 0x07) << 29) | \ 79 ((method & 0x1f) << 24) | \ 80 ((in & 0xff) << 16) | \ 81 ((out & 0xff) << 8) | \ 82 ((oin & 0x0f) << 4) | \ 83 (oout & 0x0f)) 84 85 #define FASTRPC_SCALARS(method, in, out) \ 86 FASTRPC_BUILD_SCALARS(0, method, in, out, 0, 0) 87 88 #define FASTRPC_CREATE_PROCESS_NARGS 6 89 #define FASTRPC_CREATE_STATIC_PROCESS_NARGS 3 90 /* Remote Method id table */ 91 #define FASTRPC_RMID_INIT_ATTACH 0 92 #define FASTRPC_RMID_INIT_RELEASE 1 93 #define FASTRPC_RMID_INIT_MMAP 4 94 #define FASTRPC_RMID_INIT_MUNMAP 5 95 #define FASTRPC_RMID_INIT_CREATE 6 96 #define FASTRPC_RMID_INIT_CREATE_ATTR 7 97 #define FASTRPC_RMID_INIT_CREATE_STATIC 8 98 #define FASTRPC_RMID_INIT_MEM_MAP 10 99 #define FASTRPC_RMID_INIT_MEM_UNMAP 11 100 101 /* Protection Domain(PD) ids */ 102 #define ROOT_PD (0) 103 #define USER_PD (1) 104 #define SENSORS_PD (2) 105 106 #define miscdev_to_fdevice(d) container_of(d, struct fastrpc_device, miscdev) 107 108 struct fastrpc_phy_page { 109 dma_addr_t addr; /* dma address */ 110 u64 size; /* size of contiguous region */ 111 }; 112 113 struct fastrpc_invoke_buf { 114 u32 num; /* number of contiguous regions */ 115 u32 pgidx; /* index to start of contiguous region */ 116 }; 117 118 struct fastrpc_remote_dmahandle { 119 s32 fd; /* dma handle fd */ 120 u32 offset; /* dma handle offset */ 121 u32 len; /* dma handle length */ 122 }; 123 124 struct fastrpc_remote_buf { 125 u64 pv; /* buffer pointer */ 126 u64 len; /* length of buffer */ 127 }; 128 129 union fastrpc_remote_arg { 130 struct fastrpc_remote_buf buf; 131 struct fastrpc_remote_dmahandle dma; 132 }; 133 134 struct fastrpc_mmap_rsp_msg { 135 u64 vaddr; 136 }; 137 138 struct fastrpc_mmap_req_msg { 139 s32 client_id; 140 u32 flags; 141 u64 vaddr; 142 s32 num; 143 }; 144 145 struct fastrpc_mem_map_req_msg { 146 s32 client_id; 147 s32 fd; 148 s32 offset; 149 u32 flags; 150 u64 vaddrin; 151 s32 num; 152 s32 data_len; 153 }; 154 155 struct fastrpc_munmap_req_msg { 156 s32 client_id; 157 u64 vaddr; 158 u64 size; 159 }; 160 161 struct fastrpc_mem_unmap_req_msg { 162 s32 client_id; 163 s32 fd; 164 u64 vaddrin; 165 u64 len; 166 }; 167 168 struct fastrpc_msg { 169 int client_id; /* process client id */ 170 int tid; /* thread id */ 171 u64 ctx; /* invoke caller context */ 172 u32 handle; /* handle to invoke */ 173 u32 sc; /* scalars structure describing the data */ 174 dma_addr_t addr; /* dma address */ 175 u64 size; /* size of contiguous region */ 176 }; 177 178 struct fastrpc_invoke_rsp { 179 u64 ctx; /* invoke caller context */ 180 int retval; /* invoke return value */ 181 }; 182 183 struct fastrpc_buf_overlap { 184 u64 start; 185 u64 end; 186 int raix; 187 u64 mstart; 188 u64 mend; 189 u64 offset; 190 }; 191 192 struct fastrpc_buf { 193 struct fastrpc_user *fl; 194 struct dma_buf *dmabuf; 195 struct device *dev; 196 void *virt; 197 dma_addr_t dma_addr; 198 u64 size; 199 /* Lock for dma buf attachments */ 200 struct mutex lock; 201 struct list_head attachments; 202 /* mmap support */ 203 struct list_head node; /* list of user requested mmaps */ 204 uintptr_t raddr; 205 }; 206 207 struct fastrpc_dma_buf_attachment { 208 struct device *dev; 209 struct sg_table sgt; 210 struct list_head node; 211 }; 212 213 struct fastrpc_map { 214 struct list_head node; 215 struct fastrpc_user *fl; 216 int fd; 217 struct dma_buf *buf; 218 struct sg_table *table; 219 struct dma_buf_attachment *attach; 220 dma_addr_t dma_addr; 221 u64 size; 222 void *va; 223 u64 len; 224 u64 raddr; 225 u32 attr; 226 struct kref refcount; 227 }; 228 229 struct fastrpc_invoke_ctx { 230 int nscalars; 231 int nbufs; 232 int retval; 233 int pid; 234 int client_id; 235 u32 sc; 236 u32 *crc; 237 u64 ctxid; 238 u64 msg_sz; 239 struct kref refcount; 240 struct list_head node; /* list of ctxs */ 241 struct completion work; 242 struct work_struct put_work; 243 struct fastrpc_msg msg; 244 struct fastrpc_user *fl; 245 union fastrpc_remote_arg *rpra; 246 struct fastrpc_map **maps; 247 struct fastrpc_buf *buf; 248 struct fastrpc_invoke_args *args; 249 struct fastrpc_buf_overlap *olaps; 250 struct fastrpc_channel_ctx *cctx; 251 }; 252 253 struct fastrpc_session_ctx { 254 struct device *dev; 255 int sid; 256 bool used; 257 bool valid; 258 }; 259 260 struct fastrpc_soc_data { 261 u32 sid_pos; 262 u32 dma_addr_bits_cdsp; 263 u32 dma_addr_bits_default; 264 }; 265 266 struct fastrpc_channel_ctx { 267 int domain_id; 268 int sesscount; 269 int vmcount; 270 struct qcom_scm_vmperm vmperms[FASTRPC_MAX_VMIDS]; 271 struct rpmsg_device *rpdev; 272 struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS]; 273 spinlock_t lock; 274 struct idr ctx_idr; 275 struct list_head users; 276 struct kref refcount; 277 /* Flag if dsp attributes are cached */ 278 bool valid_attributes; 279 u32 dsp_attributes[FASTRPC_MAX_DSP_ATTRIBUTES]; 280 struct fastrpc_device *secure_fdevice; 281 struct fastrpc_device *fdevice; 282 struct fastrpc_buf *remote_heap; 283 struct list_head invoke_interrupted_mmaps; 284 bool secure; 285 bool unsigned_support; 286 u64 dma_mask; 287 const struct fastrpc_soc_data *soc_data; 288 }; 289 290 struct fastrpc_device { 291 struct fastrpc_channel_ctx *cctx; 292 struct miscdevice miscdev; 293 bool secure; 294 }; 295 296 struct fastrpc_user { 297 struct list_head user; 298 struct list_head maps; 299 struct list_head pending; 300 struct list_head mmaps; 301 302 struct fastrpc_channel_ctx *cctx; 303 struct fastrpc_session_ctx *sctx; 304 struct fastrpc_buf *init_mem; 305 306 int client_id; 307 int pd; 308 bool is_secure_dev; 309 /* Lock for lists */ 310 spinlock_t lock; 311 /* lock for allocations */ 312 struct mutex mutex; 313 /* Reference count */ 314 struct kref refcount; 315 }; 316 317 /* Extract SMMU PA from consolidated IOVA */ 318 static inline dma_addr_t fastrpc_ipa_to_dma_addr(struct fastrpc_channel_ctx *cctx, dma_addr_t iova) 319 { 320 if (!cctx->soc_data->sid_pos) 321 return 0; 322 return iova & GENMASK_ULL(cctx->soc_data->sid_pos - 1, 0); 323 } 324 325 /* 326 * Prepare the consolidated iova to send to DSP by prepending the SID 327 * to smmu PA at the appropriate position 328 */ 329 static inline u64 fastrpc_sid_offset(struct fastrpc_channel_ctx *cctx, 330 struct fastrpc_session_ctx *sctx) 331 { 332 return (u64)sctx->sid << cctx->soc_data->sid_pos; 333 } 334 335 static void fastrpc_free_map(struct kref *ref) 336 { 337 struct fastrpc_map *map; 338 339 map = container_of(ref, struct fastrpc_map, refcount); 340 341 if (map->table) { 342 if (map->attr & FASTRPC_ATTR_SECUREMAP) { 343 struct qcom_scm_vmperm perm; 344 int vmid = map->fl->cctx->vmperms[0].vmid; 345 u64 src_perms = BIT(QCOM_SCM_VMID_HLOS) | BIT(vmid); 346 int err = 0; 347 348 perm.vmid = QCOM_SCM_VMID_HLOS; 349 perm.perm = QCOM_SCM_PERM_RWX; 350 err = qcom_scm_assign_mem(map->dma_addr, map->len, 351 &src_perms, &perm, 1); 352 if (err) { 353 dev_err(map->fl->sctx->dev, 354 "Failed to assign memory dma_addr %pad size 0x%llx err %d\n", 355 &map->dma_addr, map->len, err); 356 return; 357 } 358 } 359 dma_buf_unmap_attachment_unlocked(map->attach, map->table, 360 DMA_BIDIRECTIONAL); 361 dma_buf_detach(map->buf, map->attach); 362 dma_buf_put(map->buf); 363 } 364 365 if (map->fl) { 366 spin_lock(&map->fl->lock); 367 list_del(&map->node); 368 spin_unlock(&map->fl->lock); 369 map->fl = NULL; 370 } 371 372 kfree(map); 373 } 374 375 static void fastrpc_map_put(struct fastrpc_map *map) 376 { 377 if (map) 378 kref_put(&map->refcount, fastrpc_free_map); 379 } 380 381 static int fastrpc_map_get(struct fastrpc_map *map) 382 { 383 if (!map) 384 return -ENOENT; 385 386 return kref_get_unless_zero(&map->refcount) ? 0 : -ENOENT; 387 } 388 389 390 static int fastrpc_map_lookup(struct fastrpc_user *fl, int fd, 391 struct fastrpc_map **ppmap, bool take_ref) 392 { 393 struct fastrpc_map *map = NULL; 394 struct dma_buf *buf; 395 int ret = -ENOENT; 396 397 buf = dma_buf_get(fd); 398 if (IS_ERR(buf)) 399 return PTR_ERR(buf); 400 401 spin_lock(&fl->lock); 402 list_for_each_entry(map, &fl->maps, node) { 403 if (map->fd != fd || map->buf != buf) 404 continue; 405 406 if (take_ref) { 407 ret = fastrpc_map_get(map); 408 if (ret) 409 break; 410 } 411 412 *ppmap = map; 413 ret = 0; 414 break; 415 } 416 spin_unlock(&fl->lock); 417 418 dma_buf_put(buf); 419 420 return ret; 421 } 422 423 static void fastrpc_buf_free(struct fastrpc_buf *buf) 424 { 425 dma_free_coherent(buf->dev, buf->size, buf->virt, 426 fastrpc_ipa_to_dma_addr(buf->fl->cctx, buf->dma_addr)); 427 kfree(buf); 428 } 429 430 static int __fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev, 431 u64 size, struct fastrpc_buf **obuf) 432 { 433 struct fastrpc_buf *buf; 434 435 buf = kzalloc_obj(*buf); 436 if (!buf) 437 return -ENOMEM; 438 439 INIT_LIST_HEAD(&buf->attachments); 440 INIT_LIST_HEAD(&buf->node); 441 mutex_init(&buf->lock); 442 443 buf->fl = fl; 444 buf->virt = NULL; 445 buf->dma_addr = 0; 446 buf->size = size; 447 buf->dev = dev; 448 buf->raddr = 0; 449 450 buf->virt = dma_alloc_coherent(dev, buf->size, &buf->dma_addr, 451 GFP_KERNEL); 452 if (!buf->virt) { 453 mutex_destroy(&buf->lock); 454 kfree(buf); 455 return -ENOMEM; 456 } 457 458 *obuf = buf; 459 460 return 0; 461 } 462 463 static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev, 464 u64 size, struct fastrpc_buf **obuf) 465 { 466 int ret; 467 struct fastrpc_buf *buf; 468 469 ret = __fastrpc_buf_alloc(fl, dev, size, obuf); 470 if (ret) 471 return ret; 472 473 buf = *obuf; 474 475 if (fl->sctx && fl->sctx->sid) 476 buf->dma_addr += fastrpc_sid_offset(fl->cctx, fl->sctx); 477 478 return 0; 479 } 480 481 static int fastrpc_remote_heap_alloc(struct fastrpc_user *fl, struct device *dev, 482 u64 size, struct fastrpc_buf **obuf) 483 { 484 struct device *rdev = &fl->cctx->rpdev->dev; 485 486 return __fastrpc_buf_alloc(fl, rdev, size, obuf); 487 } 488 489 static void fastrpc_channel_ctx_free(struct kref *ref) 490 { 491 struct fastrpc_channel_ctx *cctx; 492 493 cctx = container_of(ref, struct fastrpc_channel_ctx, refcount); 494 495 kfree(cctx); 496 } 497 498 static void fastrpc_channel_ctx_get(struct fastrpc_channel_ctx *cctx) 499 { 500 kref_get(&cctx->refcount); 501 } 502 503 static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx) 504 { 505 kref_put(&cctx->refcount, fastrpc_channel_ctx_free); 506 } 507 508 static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx); 509 510 static void fastrpc_user_free(struct kref *ref) 511 { 512 struct fastrpc_user *fl = container_of(ref, struct fastrpc_user, refcount); 513 struct fastrpc_invoke_ctx *ctx, *n; 514 struct fastrpc_map *map, *m; 515 struct fastrpc_buf *buf, *b; 516 517 if (fl->init_mem) 518 fastrpc_buf_free(fl->init_mem); 519 520 list_for_each_entry_safe(ctx, n, &fl->pending, node) { 521 list_del(&ctx->node); 522 fastrpc_context_put(ctx); 523 } 524 525 list_for_each_entry_safe(map, m, &fl->maps, node) 526 fastrpc_map_put(map); 527 528 list_for_each_entry_safe(buf, b, &fl->mmaps, node) { 529 list_del(&buf->node); 530 fastrpc_buf_free(buf); 531 } 532 533 fastrpc_channel_ctx_put(fl->cctx); 534 mutex_destroy(&fl->mutex); 535 kfree(fl); 536 } 537 538 static void fastrpc_user_get(struct fastrpc_user *fl) 539 { 540 kref_get(&fl->refcount); 541 } 542 543 static void fastrpc_user_put(struct fastrpc_user *fl) 544 { 545 kref_put(&fl->refcount, fastrpc_user_free); 546 } 547 548 static void fastrpc_context_free(struct kref *ref) 549 { 550 struct fastrpc_invoke_ctx *ctx; 551 struct fastrpc_channel_ctx *cctx; 552 struct fastrpc_user *fl; 553 unsigned long flags; 554 int i; 555 556 ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount); 557 cctx = ctx->cctx; 558 fl = ctx->fl; 559 560 for (i = 0; i < ctx->nbufs; i++) 561 fastrpc_map_put(ctx->maps[i]); 562 563 if (ctx->buf) 564 fastrpc_buf_free(ctx->buf); 565 566 spin_lock_irqsave(&cctx->lock, flags); 567 idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4); 568 spin_unlock_irqrestore(&cctx->lock, flags); 569 570 kfree(ctx->maps); 571 kfree(ctx->olaps); 572 kfree(ctx); 573 574 /* Release the reference taken in fastrpc_context_alloc() */ 575 fastrpc_user_put(fl); 576 fastrpc_channel_ctx_put(cctx); 577 } 578 579 static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx) 580 { 581 kref_get(&ctx->refcount); 582 } 583 584 static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx) 585 { 586 kref_put(&ctx->refcount, fastrpc_context_free); 587 } 588 589 static void fastrpc_context_put_wq(struct work_struct *work) 590 { 591 struct fastrpc_invoke_ctx *ctx = 592 container_of(work, struct fastrpc_invoke_ctx, put_work); 593 594 fastrpc_context_put(ctx); 595 } 596 597 #define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1) 598 static int olaps_cmp(const void *a, const void *b) 599 { 600 struct fastrpc_buf_overlap *pa = (struct fastrpc_buf_overlap *)a; 601 struct fastrpc_buf_overlap *pb = (struct fastrpc_buf_overlap *)b; 602 /* sort with lowest starting buffer first */ 603 int st = CMP(pa->start, pb->start); 604 /* sort with highest ending buffer first */ 605 int ed = CMP(pb->end, pa->end); 606 607 return st == 0 ? ed : st; 608 } 609 610 static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx) 611 { 612 u64 max_end = 0; 613 int i; 614 615 for (i = 0; i < ctx->nbufs; ++i) { 616 ctx->olaps[i].start = ctx->args[i].ptr; 617 ctx->olaps[i].end = ctx->olaps[i].start + ctx->args[i].length; 618 ctx->olaps[i].raix = i; 619 } 620 621 sort(ctx->olaps, ctx->nbufs, sizeof(*ctx->olaps), olaps_cmp, NULL); 622 623 for (i = 0; i < ctx->nbufs; ++i) { 624 /* Falling inside previous range */ 625 if (ctx->olaps[i].start < max_end) { 626 ctx->olaps[i].mstart = max_end; 627 ctx->olaps[i].mend = ctx->olaps[i].end; 628 ctx->olaps[i].offset = max_end - ctx->olaps[i].start; 629 630 if (ctx->olaps[i].end > max_end) { 631 max_end = ctx->olaps[i].end; 632 } else { 633 ctx->olaps[i].mend = 0; 634 ctx->olaps[i].mstart = 0; 635 } 636 637 } else { 638 ctx->olaps[i].mend = ctx->olaps[i].end; 639 ctx->olaps[i].mstart = ctx->olaps[i].start; 640 ctx->olaps[i].offset = 0; 641 max_end = ctx->olaps[i].end; 642 } 643 } 644 } 645 646 static struct fastrpc_invoke_ctx *fastrpc_context_alloc( 647 struct fastrpc_user *user, u32 kernel, u32 sc, 648 struct fastrpc_invoke_args *args) 649 { 650 struct fastrpc_channel_ctx *cctx = user->cctx; 651 struct fastrpc_invoke_ctx *ctx = NULL; 652 unsigned long flags; 653 int ret; 654 655 ctx = kzalloc_obj(*ctx); 656 if (!ctx) 657 return ERR_PTR(-ENOMEM); 658 659 INIT_LIST_HEAD(&ctx->node); 660 ctx->fl = user; 661 ctx->nscalars = REMOTE_SCALARS_LENGTH(sc); 662 ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) + 663 REMOTE_SCALARS_OUTBUFS(sc); 664 665 if (ctx->nscalars) { 666 ctx->maps = kzalloc_objs(*ctx->maps, ctx->nscalars); 667 if (!ctx->maps) { 668 kfree(ctx); 669 return ERR_PTR(-ENOMEM); 670 } 671 ctx->olaps = kzalloc_objs(*ctx->olaps, ctx->nscalars); 672 if (!ctx->olaps) { 673 kfree(ctx->maps); 674 kfree(ctx); 675 return ERR_PTR(-ENOMEM); 676 } 677 ctx->args = args; 678 fastrpc_get_buff_overlaps(ctx); 679 } 680 681 /* Released in fastrpc_context_put() */ 682 fastrpc_channel_ctx_get(cctx); 683 /* Take a reference to user, released in fastrpc_context_free() */ 684 fastrpc_user_get(user); 685 686 ctx->sc = sc; 687 ctx->retval = -1; 688 ctx->pid = current->pid; 689 ctx->client_id = user->client_id; 690 ctx->cctx = cctx; 691 init_completion(&ctx->work); 692 INIT_WORK(&ctx->put_work, fastrpc_context_put_wq); 693 694 spin_lock(&user->lock); 695 list_add_tail(&ctx->node, &user->pending); 696 spin_unlock(&user->lock); 697 698 spin_lock_irqsave(&cctx->lock, flags); 699 ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1, 700 FASTRPC_CTX_MAX, GFP_ATOMIC); 701 if (ret < 0) { 702 spin_unlock_irqrestore(&cctx->lock, flags); 703 goto err_idr; 704 } 705 ctx->ctxid = ret << 4; 706 spin_unlock_irqrestore(&cctx->lock, flags); 707 708 kref_init(&ctx->refcount); 709 710 return ctx; 711 err_idr: 712 spin_lock(&user->lock); 713 list_del(&ctx->node); 714 spin_unlock(&user->lock); 715 fastrpc_user_put(user); 716 fastrpc_channel_ctx_put(cctx); 717 kfree(ctx->maps); 718 kfree(ctx->olaps); 719 kfree(ctx); 720 721 return ERR_PTR(ret); 722 } 723 724 static struct sg_table * 725 fastrpc_map_dma_buf(struct dma_buf_attachment *attachment, 726 enum dma_data_direction dir) 727 { 728 struct fastrpc_dma_buf_attachment *a = attachment->priv; 729 struct sg_table *table; 730 int ret; 731 732 table = &a->sgt; 733 734 ret = dma_map_sgtable(attachment->dev, table, dir, 0); 735 if (ret) 736 table = ERR_PTR(ret); 737 return table; 738 } 739 740 static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach, 741 struct sg_table *table, 742 enum dma_data_direction dir) 743 { 744 dma_unmap_sgtable(attach->dev, table, dir, 0); 745 } 746 747 static void fastrpc_release(struct dma_buf *dmabuf) 748 { 749 struct fastrpc_buf *buffer = dmabuf->priv; 750 751 fastrpc_buf_free(buffer); 752 } 753 754 static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf, 755 struct dma_buf_attachment *attachment) 756 { 757 struct fastrpc_dma_buf_attachment *a; 758 struct fastrpc_buf *buffer = dmabuf->priv; 759 int ret; 760 761 a = kzalloc_obj(*a); 762 if (!a) 763 return -ENOMEM; 764 765 ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt, 766 fastrpc_ipa_to_dma_addr(buffer->fl->cctx, buffer->dma_addr), 767 buffer->size); 768 if (ret < 0) { 769 dev_err(buffer->dev, "failed to get scatterlist from DMA API\n"); 770 kfree(a); 771 return -EINVAL; 772 } 773 774 a->dev = attachment->dev; 775 INIT_LIST_HEAD(&a->node); 776 attachment->priv = a; 777 778 mutex_lock(&buffer->lock); 779 list_add(&a->node, &buffer->attachments); 780 mutex_unlock(&buffer->lock); 781 782 return 0; 783 } 784 785 static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf, 786 struct dma_buf_attachment *attachment) 787 { 788 struct fastrpc_dma_buf_attachment *a = attachment->priv; 789 struct fastrpc_buf *buffer = dmabuf->priv; 790 791 mutex_lock(&buffer->lock); 792 list_del(&a->node); 793 mutex_unlock(&buffer->lock); 794 sg_free_table(&a->sgt); 795 kfree(a); 796 } 797 798 static int fastrpc_vmap(struct dma_buf *dmabuf, struct iosys_map *map) 799 { 800 struct fastrpc_buf *buf = dmabuf->priv; 801 802 iosys_map_set_vaddr(map, buf->virt); 803 804 return 0; 805 } 806 807 static int fastrpc_mmap(struct dma_buf *dmabuf, 808 struct vm_area_struct *vma) 809 { 810 struct fastrpc_buf *buf = dmabuf->priv; 811 size_t size = vma->vm_end - vma->vm_start; 812 813 dma_resv_assert_held(dmabuf->resv); 814 815 return dma_mmap_coherent(buf->dev, vma, buf->virt, 816 fastrpc_ipa_to_dma_addr(buf->fl->cctx, buf->dma_addr), size); 817 } 818 819 static const struct dma_buf_ops fastrpc_dma_buf_ops = { 820 .attach = fastrpc_dma_buf_attach, 821 .detach = fastrpc_dma_buf_detatch, 822 .map_dma_buf = fastrpc_map_dma_buf, 823 .unmap_dma_buf = fastrpc_unmap_dma_buf, 824 .mmap = fastrpc_mmap, 825 .vmap = fastrpc_vmap, 826 .release = fastrpc_release, 827 }; 828 829 static dma_addr_t fastrpc_compute_dma_addr(struct fastrpc_user *fl, dma_addr_t sg_dma_addr) 830 { 831 return sg_dma_addr + fastrpc_sid_offset(fl->cctx, fl->sctx); 832 } 833 834 static int fastrpc_map_attach(struct fastrpc_user *fl, int fd, 835 u64 len, u32 attr, struct fastrpc_map **ppmap) 836 { 837 struct fastrpc_session_ctx *sess = fl->sctx; 838 struct fastrpc_map *map = NULL; 839 struct sg_table *table; 840 struct scatterlist *sgl = NULL; 841 int err = 0, sgl_index = 0; 842 843 map = kzalloc_obj(*map); 844 if (!map) 845 return -ENOMEM; 846 847 INIT_LIST_HEAD(&map->node); 848 kref_init(&map->refcount); 849 850 map->fl = fl; 851 map->fd = fd; 852 map->buf = dma_buf_get(fd); 853 if (IS_ERR(map->buf)) { 854 err = PTR_ERR(map->buf); 855 goto get_err; 856 } 857 858 map->attach = dma_buf_attach(map->buf, sess->dev); 859 if (IS_ERR(map->attach)) { 860 dev_err(sess->dev, "Failed to attach dmabuf\n"); 861 err = PTR_ERR(map->attach); 862 goto attach_err; 863 } 864 865 table = dma_buf_map_attachment_unlocked(map->attach, DMA_BIDIRECTIONAL); 866 if (IS_ERR(table)) { 867 err = PTR_ERR(table); 868 goto map_err; 869 } 870 map->table = table; 871 872 if (attr & FASTRPC_ATTR_SECUREMAP) 873 map->dma_addr = sg_phys(map->table->sgl); 874 else 875 map->dma_addr = fastrpc_compute_dma_addr(fl, sg_dma_address(map->table->sgl)); 876 for_each_sg(map->table->sgl, sgl, map->table->nents, 877 sgl_index) 878 map->size += sg_dma_len(sgl); 879 if (len > map->size) { 880 dev_dbg(sess->dev, "Bad size passed len 0x%llx map size 0x%llx\n", 881 len, map->size); 882 err = -EINVAL; 883 goto map_err; 884 } 885 map->va = sg_virt(map->table->sgl); 886 map->len = len; 887 888 if (attr & FASTRPC_ATTR_SECUREMAP) { 889 /* 890 * If subsystem VMIDs are defined in DTSI, then do 891 * hyp_assign from HLOS to those VM(s) 892 */ 893 u64 src_perms = BIT(QCOM_SCM_VMID_HLOS); 894 struct qcom_scm_vmperm dst_perms[2] = {0}; 895 896 dst_perms[0].vmid = QCOM_SCM_VMID_HLOS; 897 dst_perms[0].perm = QCOM_SCM_PERM_RW; 898 dst_perms[1].vmid = fl->cctx->vmperms[0].vmid; 899 dst_perms[1].perm = QCOM_SCM_PERM_RWX; 900 map->attr = attr; 901 err = qcom_scm_assign_mem(map->dma_addr, (u64)map->len, &src_perms, dst_perms, 2); 902 if (err) { 903 dev_err(sess->dev, 904 "Failed to assign memory with dma_addr %pad size 0x%llx err %d\n", 905 &map->dma_addr, map->len, err); 906 goto map_err; 907 } 908 } 909 spin_lock(&fl->lock); 910 list_add_tail(&map->node, &fl->maps); 911 spin_unlock(&fl->lock); 912 *ppmap = map; 913 914 return 0; 915 916 map_err: 917 dma_buf_detach(map->buf, map->attach); 918 attach_err: 919 dma_buf_put(map->buf); 920 get_err: 921 fastrpc_map_put(map); 922 923 return err; 924 } 925 926 static int fastrpc_map_create(struct fastrpc_user *fl, int fd, 927 u64 len, u32 attr, struct fastrpc_map **ppmap) 928 { 929 if (!fastrpc_map_lookup(fl, fd, ppmap, true)) 930 return 0; 931 932 return fastrpc_map_attach(fl, fd, len, attr, ppmap); 933 } 934 935 /* 936 * Fastrpc payload buffer with metadata looks like: 937 * 938 * >>>>>> START of METADATA <<<<<<<<< 939 * +---------------------------------+ 940 * | Arguments | 941 * | type:(union fastrpc_remote_arg)| 942 * | (0 - N) | 943 * +---------------------------------+ 944 * | Invoke Buffer list | 945 * | type:(struct fastrpc_invoke_buf)| 946 * | (0 - N) | 947 * +---------------------------------+ 948 * | Page info list | 949 * | type:(struct fastrpc_phy_page) | 950 * | (0 - N) | 951 * +---------------------------------+ 952 * | Optional info | 953 * |(can be specific to SoC/Firmware)| 954 * +---------------------------------+ 955 * >>>>>>>> END of METADATA <<<<<<<<< 956 * +---------------------------------+ 957 * | Inline ARGS | 958 * | (0-N) | 959 * +---------------------------------+ 960 */ 961 962 static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx) 963 { 964 int size = 0; 965 966 size = (sizeof(struct fastrpc_remote_buf) + 967 sizeof(struct fastrpc_invoke_buf) + 968 sizeof(struct fastrpc_phy_page)) * ctx->nscalars + 969 sizeof(u64) * FASTRPC_MAX_FDLIST + 970 sizeof(u32) * FASTRPC_MAX_CRCLIST; 971 972 return size; 973 } 974 975 static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen) 976 { 977 u64 size = 0; 978 int oix; 979 980 size = ALIGN(metalen, FASTRPC_ALIGN); 981 for (oix = 0; oix < ctx->nbufs; oix++) { 982 int i = ctx->olaps[oix].raix; 983 984 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) { 985 986 if (ctx->olaps[oix].offset == 0) 987 size = ALIGN(size, FASTRPC_ALIGN); 988 989 size += (ctx->olaps[oix].mend - ctx->olaps[oix].mstart); 990 } 991 } 992 993 return size; 994 } 995 996 static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx) 997 { 998 struct device *dev = ctx->fl->sctx->dev; 999 int i, err; 1000 1001 for (i = 0; i < ctx->nscalars; ++i) { 1002 1003 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 || 1004 ctx->args[i].length == 0) 1005 continue; 1006 1007 if (i < ctx->nbufs) 1008 err = fastrpc_map_create(ctx->fl, ctx->args[i].fd, 1009 ctx->args[i].length, ctx->args[i].attr, &ctx->maps[i]); 1010 else 1011 err = fastrpc_map_attach(ctx->fl, ctx->args[i].fd, 1012 ctx->args[i].length, ctx->args[i].attr, &ctx->maps[i]); 1013 if (err) { 1014 dev_err(dev, "Error Creating map %d\n", err); 1015 return -EINVAL; 1016 } 1017 1018 } 1019 return 0; 1020 } 1021 1022 static struct fastrpc_invoke_buf *fastrpc_invoke_buf_start(union fastrpc_remote_arg *pra, int len) 1023 { 1024 return (struct fastrpc_invoke_buf *)(&pra[len]); 1025 } 1026 1027 static struct fastrpc_phy_page *fastrpc_phy_page_start(struct fastrpc_invoke_buf *buf, int len) 1028 { 1029 return (struct fastrpc_phy_page *)(&buf[len]); 1030 } 1031 1032 static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx) 1033 { 1034 struct device *dev = ctx->fl->sctx->dev; 1035 union fastrpc_remote_arg *rpra; 1036 struct fastrpc_invoke_buf *list; 1037 struct fastrpc_phy_page *pages; 1038 int inbufs, i, oix, err = 0; 1039 u64 len, rlen, pkt_size; 1040 u64 pg_start, pg_end; 1041 uintptr_t args; 1042 int metalen; 1043 1044 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc); 1045 metalen = fastrpc_get_meta_size(ctx); 1046 pkt_size = fastrpc_get_payload_size(ctx, metalen); 1047 1048 err = fastrpc_create_maps(ctx); 1049 if (err) 1050 return err; 1051 1052 ctx->msg_sz = pkt_size; 1053 1054 if (ctx->fl->sctx->sid) 1055 err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf); 1056 else 1057 err = fastrpc_remote_heap_alloc(ctx->fl, dev, pkt_size, &ctx->buf); 1058 if (err) 1059 return err; 1060 1061 memset(ctx->buf->virt, 0, pkt_size); 1062 rpra = ctx->buf->virt; 1063 list = fastrpc_invoke_buf_start(rpra, ctx->nscalars); 1064 pages = fastrpc_phy_page_start(list, ctx->nscalars); 1065 args = (uintptr_t)ctx->buf->virt + metalen; 1066 rlen = pkt_size - metalen; 1067 ctx->rpra = rpra; 1068 1069 for (oix = 0; oix < ctx->nbufs; ++oix) { 1070 int mlen; 1071 1072 i = ctx->olaps[oix].raix; 1073 len = ctx->args[i].length; 1074 1075 rpra[i].buf.pv = 0; 1076 rpra[i].buf.len = len; 1077 list[i].num = len ? 1 : 0; 1078 list[i].pgidx = i; 1079 1080 if (!len) 1081 continue; 1082 1083 if (ctx->maps[i]) { 1084 struct vm_area_struct *vma = NULL; 1085 1086 rpra[i].buf.pv = (u64) ctx->args[i].ptr; 1087 pages[i].addr = ctx->maps[i]->dma_addr; 1088 1089 mmap_read_lock(current->mm); 1090 vma = vma_lookup(current->mm, ctx->args[i].ptr); 1091 if (vma) 1092 pages[i].addr += (ctx->args[i].ptr & PAGE_MASK) - 1093 vma->vm_start; 1094 mmap_read_unlock(current->mm); 1095 1096 pg_start = (ctx->args[i].ptr & PAGE_MASK) >> PAGE_SHIFT; 1097 pg_end = ((ctx->args[i].ptr + len - 1) & PAGE_MASK) >> 1098 PAGE_SHIFT; 1099 pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE; 1100 1101 } else { 1102 1103 if (ctx->olaps[oix].offset == 0) { 1104 rlen -= ALIGN(args, FASTRPC_ALIGN) - args; 1105 args = ALIGN(args, FASTRPC_ALIGN); 1106 } 1107 1108 mlen = ctx->olaps[oix].mend - ctx->olaps[oix].mstart; 1109 1110 if (rlen < mlen) 1111 goto bail; 1112 1113 rpra[i].buf.pv = args - ctx->olaps[oix].offset; 1114 pages[i].addr = ctx->buf->dma_addr - 1115 ctx->olaps[oix].offset + 1116 (pkt_size - rlen); 1117 pages[i].addr = pages[i].addr & PAGE_MASK; 1118 1119 pg_start = (rpra[i].buf.pv & PAGE_MASK) >> PAGE_SHIFT; 1120 pg_end = ((rpra[i].buf.pv + len - 1) & PAGE_MASK) >> PAGE_SHIFT; 1121 pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE; 1122 args = args + mlen; 1123 rlen -= mlen; 1124 } 1125 1126 if (i < inbufs && !ctx->maps[i]) { 1127 void *dst = (void *)(uintptr_t)rpra[i].buf.pv; 1128 void *src = (void *)(uintptr_t)ctx->args[i].ptr; 1129 1130 if (!kernel) { 1131 if (copy_from_user(dst, (void __user *)src, 1132 len)) { 1133 err = -EFAULT; 1134 goto bail; 1135 } 1136 } else { 1137 memcpy(dst, src, len); 1138 } 1139 } 1140 } 1141 1142 for (i = ctx->nbufs; i < ctx->nscalars; ++i) { 1143 list[i].num = ctx->args[i].length ? 1 : 0; 1144 list[i].pgidx = i; 1145 if (ctx->maps[i]) { 1146 pages[i].addr = ctx->maps[i]->dma_addr; 1147 pages[i].size = ctx->maps[i]->size; 1148 } 1149 rpra[i].dma.fd = ctx->args[i].fd; 1150 rpra[i].dma.len = ctx->args[i].length; 1151 rpra[i].dma.offset = (u64) ctx->args[i].ptr; 1152 } 1153 1154 bail: 1155 if (err) 1156 dev_err(dev, "Error: get invoke args failed:%d\n", err); 1157 1158 return err; 1159 } 1160 1161 static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx, 1162 u32 kernel) 1163 { 1164 union fastrpc_remote_arg *rpra = ctx->rpra; 1165 struct fastrpc_user *fl = ctx->fl; 1166 struct fastrpc_map *mmap = NULL; 1167 struct fastrpc_invoke_buf *list; 1168 struct fastrpc_phy_page *pages; 1169 u64 *fdlist; 1170 int i, inbufs, outbufs, handles; 1171 int ret = 0; 1172 1173 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc); 1174 outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc); 1175 handles = REMOTE_SCALARS_INHANDLES(ctx->sc) + REMOTE_SCALARS_OUTHANDLES(ctx->sc); 1176 list = fastrpc_invoke_buf_start(rpra, ctx->nscalars); 1177 pages = fastrpc_phy_page_start(list, ctx->nscalars); 1178 fdlist = (uint64_t *)(pages + inbufs + outbufs + handles); 1179 1180 for (i = inbufs; i < ctx->nbufs; ++i) { 1181 if (!ctx->maps[i]) { 1182 void *src = (void *)(uintptr_t)rpra[i].buf.pv; 1183 void *dst = (void *)(uintptr_t)ctx->args[i].ptr; 1184 u64 len = rpra[i].buf.len; 1185 1186 if (!kernel) { 1187 if (copy_to_user((void __user *)dst, src, len)) { 1188 ret = -EFAULT; 1189 goto cleanup_fdlist; 1190 } 1191 } else { 1192 memcpy(dst, src, len); 1193 } 1194 } 1195 } 1196 1197 cleanup_fdlist: 1198 /* Clean up fdlist which is updated by DSP */ 1199 for (i = 0; i < FASTRPC_MAX_FDLIST; i++) { 1200 if (!fdlist[i]) 1201 break; 1202 if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap, false)) 1203 fastrpc_map_put(mmap); 1204 } 1205 1206 return ret; 1207 } 1208 1209 static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx, 1210 struct fastrpc_invoke_ctx *ctx, 1211 u32 kernel, uint32_t handle) 1212 { 1213 struct fastrpc_channel_ctx *cctx; 1214 struct fastrpc_user *fl = ctx->fl; 1215 struct fastrpc_msg *msg = &ctx->msg; 1216 int ret; 1217 1218 cctx = fl->cctx; 1219 msg->client_id = fl->client_id; 1220 msg->tid = current->pid; 1221 1222 if (kernel) 1223 msg->client_id = 0; 1224 1225 msg->ctx = ctx->ctxid | fl->pd; 1226 msg->handle = handle; 1227 msg->sc = ctx->sc; 1228 msg->addr = ctx->buf ? ctx->buf->dma_addr : 0; 1229 msg->size = roundup(ctx->msg_sz, PAGE_SIZE); 1230 fastrpc_context_get(ctx); 1231 1232 ret = rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg)); 1233 1234 if (ret) 1235 fastrpc_context_put(ctx); 1236 1237 return ret; 1238 1239 } 1240 1241 static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, 1242 u32 handle, u32 sc, 1243 struct fastrpc_invoke_args *args) 1244 { 1245 struct fastrpc_invoke_ctx *ctx = NULL; 1246 struct fastrpc_buf *buf, *b; 1247 1248 int err = 0; 1249 1250 if (!fl->sctx) 1251 return -EINVAL; 1252 1253 if (!fl->cctx->rpdev) 1254 return -EPIPE; 1255 1256 if (handle == FASTRPC_INIT_HANDLE && !kernel) { 1257 dev_warn_ratelimited(fl->sctx->dev, "user app trying to send a kernel RPC message (%d)\n", handle); 1258 return -EPERM; 1259 } 1260 1261 ctx = fastrpc_context_alloc(fl, kernel, sc, args); 1262 if (IS_ERR(ctx)) 1263 return PTR_ERR(ctx); 1264 1265 err = fastrpc_get_args(kernel, ctx); 1266 if (err) 1267 goto bail; 1268 1269 /* make sure that all CPU memory writes are seen by DSP */ 1270 dma_wmb(); 1271 /* Send invoke buffer to remote dsp */ 1272 err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle); 1273 if (err) 1274 goto bail; 1275 1276 if (kernel) { 1277 if (!wait_for_completion_timeout(&ctx->work, 10 * HZ)) 1278 err = -ETIMEDOUT; 1279 } else { 1280 err = wait_for_completion_interruptible(&ctx->work); 1281 } 1282 1283 if (err) 1284 goto bail; 1285 1286 /* make sure that all memory writes by DSP are seen by CPU */ 1287 dma_rmb(); 1288 /* populate all the output buffers with results */ 1289 err = fastrpc_put_args(ctx, kernel); 1290 if (err) 1291 goto bail; 1292 1293 /* Check the response from remote dsp */ 1294 err = ctx->retval; 1295 if (err) 1296 goto bail; 1297 1298 bail: 1299 if (err != -ERESTARTSYS && err != -ETIMEDOUT) { 1300 /* We are done with this compute context */ 1301 spin_lock(&fl->lock); 1302 list_del(&ctx->node); 1303 spin_unlock(&fl->lock); 1304 fastrpc_context_put(ctx); 1305 } 1306 1307 if (err == -ERESTARTSYS) { 1308 list_for_each_entry_safe(buf, b, &fl->mmaps, node) { 1309 list_del(&buf->node); 1310 list_add_tail(&buf->node, &fl->cctx->invoke_interrupted_mmaps); 1311 } 1312 } 1313 1314 if (err) 1315 dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err); 1316 1317 return err; 1318 } 1319 1320 static bool is_session_rejected(struct fastrpc_user *fl, bool unsigned_pd_request) 1321 { 1322 /* Check if the device node is non-secure and channel is secure*/ 1323 if (!fl->is_secure_dev && fl->cctx->secure) { 1324 /* 1325 * Allow untrusted applications to offload only to Unsigned PD when 1326 * channel is configured as secure and block untrusted apps on channel 1327 * that does not support unsigned PD offload 1328 */ 1329 if (!fl->cctx->unsigned_support || !unsigned_pd_request) { 1330 dev_err(&fl->cctx->rpdev->dev, "Error: Untrusted application trying to offload to signed PD\n"); 1331 return true; 1332 } 1333 } 1334 1335 return false; 1336 } 1337 1338 static int fastrpc_init_create_static_process(struct fastrpc_user *fl, 1339 char __user *argp) 1340 { 1341 struct fastrpc_init_create_static init; 1342 struct fastrpc_invoke_args *args; 1343 struct fastrpc_phy_page pages[1]; 1344 char *name; 1345 int err; 1346 bool scm_done = false; 1347 struct { 1348 int client_id; 1349 u32 namelen; 1350 u32 pageslen; 1351 } inbuf; 1352 u32 sc; 1353 1354 args = kzalloc_objs(*args, FASTRPC_CREATE_STATIC_PROCESS_NARGS); 1355 if (!args) 1356 return -ENOMEM; 1357 1358 if (copy_from_user(&init, argp, sizeof(init))) { 1359 err = -EFAULT; 1360 goto err; 1361 } 1362 1363 if (init.namelen > INIT_FILE_NAMELEN_MAX) { 1364 err = -EINVAL; 1365 goto err; 1366 } 1367 1368 name = memdup_user(u64_to_user_ptr(init.name), init.namelen); 1369 if (IS_ERR(name)) { 1370 err = PTR_ERR(name); 1371 goto err; 1372 } 1373 1374 if (!fl->cctx->remote_heap) { 1375 err = fastrpc_remote_heap_alloc(fl, fl->sctx->dev, init.memlen, 1376 &fl->cctx->remote_heap); 1377 if (err) 1378 goto err_name; 1379 1380 /* Map if we have any heap VMIDs associated with this ADSP Static Process. */ 1381 if (fl->cctx->vmcount) { 1382 u64 src_perms = BIT(QCOM_SCM_VMID_HLOS); 1383 1384 err = qcom_scm_assign_mem(fl->cctx->remote_heap->dma_addr, 1385 (u64)fl->cctx->remote_heap->size, 1386 &src_perms, 1387 fl->cctx->vmperms, fl->cctx->vmcount); 1388 if (err) { 1389 dev_err(fl->sctx->dev, 1390 "Failed to assign memory with dma_addr %pad size 0x%llx err %d\n", 1391 &fl->cctx->remote_heap->dma_addr, 1392 fl->cctx->remote_heap->size, err); 1393 goto err_map; 1394 } 1395 scm_done = true; 1396 } 1397 } 1398 1399 inbuf.client_id = fl->client_id; 1400 inbuf.namelen = init.namelen; 1401 inbuf.pageslen = 0; 1402 fl->pd = USER_PD; 1403 1404 args[0].ptr = (u64)(uintptr_t)&inbuf; 1405 args[0].length = sizeof(inbuf); 1406 args[0].fd = -1; 1407 1408 args[1].ptr = (u64)(uintptr_t)name; 1409 args[1].length = inbuf.namelen; 1410 args[1].fd = -1; 1411 1412 pages[0].addr = fl->cctx->remote_heap->dma_addr; 1413 pages[0].size = fl->cctx->remote_heap->size; 1414 1415 args[2].ptr = (u64)(uintptr_t) pages; 1416 args[2].length = sizeof(*pages); 1417 args[2].fd = -1; 1418 1419 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_STATIC, 3, 0); 1420 1421 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, 1422 sc, args); 1423 if (err) 1424 goto err_invoke; 1425 1426 kfree(args); 1427 kfree(name); 1428 1429 return 0; 1430 err_invoke: 1431 if (fl->cctx->vmcount && scm_done) { 1432 u64 src_perms = 0; 1433 struct qcom_scm_vmperm dst_perms; 1434 u32 i; 1435 1436 for (i = 0; i < fl->cctx->vmcount; i++) 1437 src_perms |= BIT(fl->cctx->vmperms[i].vmid); 1438 1439 dst_perms.vmid = QCOM_SCM_VMID_HLOS; 1440 dst_perms.perm = QCOM_SCM_PERM_RWX; 1441 err = qcom_scm_assign_mem(fl->cctx->remote_heap->dma_addr, 1442 (u64)fl->cctx->remote_heap->size, 1443 &src_perms, &dst_perms, 1); 1444 if (err) 1445 dev_err(fl->sctx->dev, "Failed to assign memory dma_addr %pad size 0x%llx err %d\n", 1446 &fl->cctx->remote_heap->dma_addr, fl->cctx->remote_heap->size, err); 1447 } 1448 err_map: 1449 fastrpc_buf_free(fl->cctx->remote_heap); 1450 fl->cctx->remote_heap = NULL; 1451 err_name: 1452 kfree(name); 1453 err: 1454 kfree(args); 1455 1456 return err; 1457 } 1458 1459 static int fastrpc_init_create_process(struct fastrpc_user *fl, 1460 char __user *argp) 1461 { 1462 struct fastrpc_init_create init; 1463 struct fastrpc_invoke_args *args; 1464 struct fastrpc_phy_page pages[1]; 1465 struct fastrpc_map *map = NULL; 1466 struct fastrpc_buf *imem = NULL; 1467 int memlen; 1468 int err; 1469 struct { 1470 int client_id; 1471 u32 namelen; 1472 u32 filelen; 1473 u32 pageslen; 1474 u32 attrs; 1475 u32 siglen; 1476 } inbuf; 1477 u32 sc; 1478 bool unsigned_module = false; 1479 1480 args = kzalloc_objs(*args, FASTRPC_CREATE_PROCESS_NARGS); 1481 if (!args) 1482 return -ENOMEM; 1483 1484 if (copy_from_user(&init, argp, sizeof(init))) { 1485 err = -EFAULT; 1486 goto err; 1487 } 1488 1489 if (init.attrs & FASTRPC_MODE_UNSIGNED_MODULE) 1490 unsigned_module = true; 1491 1492 if (is_session_rejected(fl, unsigned_module)) { 1493 err = -ECONNREFUSED; 1494 goto err; 1495 } 1496 1497 if (init.filelen > INIT_FILELEN_MAX) { 1498 err = -EINVAL; 1499 goto err; 1500 } 1501 1502 inbuf.client_id = fl->client_id; 1503 inbuf.namelen = strlen(current->comm) + 1; 1504 inbuf.filelen = init.filelen; 1505 inbuf.pageslen = 1; 1506 inbuf.attrs = init.attrs; 1507 inbuf.siglen = init.siglen; 1508 fl->pd = USER_PD; 1509 1510 if (init.filelen && init.filefd) { 1511 err = fastrpc_map_create(fl, init.filefd, init.filelen, 0, &map); 1512 if (err) 1513 goto err; 1514 } 1515 1516 memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4), 1517 1024 * 1024); 1518 err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen, 1519 &imem); 1520 if (err) 1521 goto err_alloc; 1522 1523 fl->init_mem = imem; 1524 args[0].ptr = (u64)(uintptr_t)&inbuf; 1525 args[0].length = sizeof(inbuf); 1526 args[0].fd = -1; 1527 1528 args[1].ptr = (u64)(uintptr_t)current->comm; 1529 args[1].length = inbuf.namelen; 1530 args[1].fd = -1; 1531 1532 args[2].ptr = (u64) init.file; 1533 args[2].length = inbuf.filelen; 1534 args[2].fd = init.filefd; 1535 1536 pages[0].addr = imem->dma_addr; 1537 pages[0].size = imem->size; 1538 1539 args[3].ptr = (u64)(uintptr_t) pages; 1540 args[3].length = 1 * sizeof(*pages); 1541 args[3].fd = -1; 1542 1543 args[4].ptr = (u64)(uintptr_t)&inbuf.attrs; 1544 args[4].length = sizeof(inbuf.attrs); 1545 args[4].fd = -1; 1546 1547 args[5].ptr = (u64)(uintptr_t) &inbuf.siglen; 1548 args[5].length = sizeof(inbuf.siglen); 1549 args[5].fd = -1; 1550 1551 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0); 1552 if (init.attrs) 1553 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 4, 0); 1554 1555 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, 1556 sc, args); 1557 if (err) 1558 goto err_invoke; 1559 1560 kfree(args); 1561 1562 return 0; 1563 1564 err_invoke: 1565 fl->init_mem = NULL; 1566 fastrpc_buf_free(imem); 1567 err_alloc: 1568 fastrpc_map_put(map); 1569 err: 1570 kfree(args); 1571 1572 return err; 1573 } 1574 1575 static struct fastrpc_session_ctx *fastrpc_session_alloc( 1576 struct fastrpc_user *fl) 1577 { 1578 struct fastrpc_channel_ctx *cctx = fl->cctx; 1579 struct fastrpc_session_ctx *session = NULL; 1580 unsigned long flags; 1581 int i; 1582 1583 spin_lock_irqsave(&cctx->lock, flags); 1584 for (i = 0; i < cctx->sesscount; i++) { 1585 if (!cctx->session[i].used && cctx->session[i].valid) { 1586 cctx->session[i].used = true; 1587 session = &cctx->session[i]; 1588 /* any non-zero ID will work, session_idx + 1 is the simplest one */ 1589 fl->client_id = i + 1; 1590 break; 1591 } 1592 } 1593 spin_unlock_irqrestore(&cctx->lock, flags); 1594 1595 return session; 1596 } 1597 1598 static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx, 1599 struct fastrpc_session_ctx *session) 1600 { 1601 unsigned long flags; 1602 1603 spin_lock_irqsave(&cctx->lock, flags); 1604 session->used = false; 1605 spin_unlock_irqrestore(&cctx->lock, flags); 1606 } 1607 1608 static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl) 1609 { 1610 struct fastrpc_invoke_args args[1]; 1611 int client_id = 0; 1612 u32 sc; 1613 1614 client_id = fl->client_id; 1615 args[0].ptr = (u64)(uintptr_t) &client_id; 1616 args[0].length = sizeof(client_id); 1617 args[0].fd = -1; 1618 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0); 1619 1620 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, 1621 sc, &args[0]); 1622 } 1623 1624 static int fastrpc_device_release(struct inode *inode, struct file *file) 1625 { 1626 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data; 1627 struct fastrpc_channel_ctx *cctx = fl->cctx; 1628 unsigned long flags; 1629 1630 fastrpc_release_current_dsp_process(fl); 1631 1632 spin_lock_irqsave(&cctx->lock, flags); 1633 list_del(&fl->user); 1634 spin_unlock_irqrestore(&cctx->lock, flags); 1635 1636 fastrpc_session_free(cctx, fl->sctx); 1637 file->private_data = NULL; 1638 /* Release the reference taken in fastrpc_device_open */ 1639 fastrpc_user_put(fl); 1640 1641 return 0; 1642 } 1643 1644 static int fastrpc_device_open(struct inode *inode, struct file *filp) 1645 { 1646 struct fastrpc_channel_ctx *cctx; 1647 struct fastrpc_device *fdevice; 1648 struct fastrpc_user *fl = NULL; 1649 unsigned long flags; 1650 1651 fdevice = miscdev_to_fdevice(filp->private_data); 1652 cctx = fdevice->cctx; 1653 1654 fl = kzalloc_obj(*fl); 1655 if (!fl) 1656 return -ENOMEM; 1657 1658 /* Released in fastrpc_device_release() */ 1659 fastrpc_channel_ctx_get(cctx); 1660 1661 filp->private_data = fl; 1662 spin_lock_init(&fl->lock); 1663 mutex_init(&fl->mutex); 1664 INIT_LIST_HEAD(&fl->pending); 1665 INIT_LIST_HEAD(&fl->maps); 1666 INIT_LIST_HEAD(&fl->mmaps); 1667 INIT_LIST_HEAD(&fl->user); 1668 fl->cctx = cctx; 1669 fl->is_secure_dev = fdevice->secure; 1670 1671 fl->sctx = fastrpc_session_alloc(fl); 1672 if (!fl->sctx) { 1673 dev_err(&cctx->rpdev->dev, "No session available\n"); 1674 mutex_destroy(&fl->mutex); 1675 kfree(fl); 1676 1677 return -EBUSY; 1678 } 1679 1680 spin_lock_irqsave(&cctx->lock, flags); 1681 list_add_tail(&fl->user, &cctx->users); 1682 spin_unlock_irqrestore(&cctx->lock, flags); 1683 kref_init(&fl->refcount); 1684 1685 return 0; 1686 } 1687 1688 static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp) 1689 { 1690 struct fastrpc_alloc_dma_buf bp; 1691 DEFINE_DMA_BUF_EXPORT_INFO(exp_info); 1692 struct fastrpc_buf *buf = NULL; 1693 int err; 1694 1695 if (copy_from_user(&bp, argp, sizeof(bp))) 1696 return -EFAULT; 1697 1698 err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf); 1699 if (err) 1700 return err; 1701 exp_info.ops = &fastrpc_dma_buf_ops; 1702 exp_info.size = bp.size; 1703 exp_info.flags = O_RDWR; 1704 exp_info.priv = buf; 1705 buf->dmabuf = dma_buf_export(&exp_info); 1706 if (IS_ERR(buf->dmabuf)) { 1707 err = PTR_ERR(buf->dmabuf); 1708 fastrpc_buf_free(buf); 1709 return err; 1710 } 1711 1712 bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE); 1713 if (bp.fd < 0) { 1714 dma_buf_put(buf->dmabuf); 1715 return -EINVAL; 1716 } 1717 1718 if (copy_to_user(argp, &bp, sizeof(bp))) { 1719 /* 1720 * The usercopy failed, but we can't do much about it, as 1721 * dma_buf_fd() already called fd_install() and made the 1722 * file descriptor accessible for the current process. It 1723 * might already be closed and dmabuf no longer valid when 1724 * we reach this point. Therefore "leak" the fd and rely on 1725 * the process exit path to do any required cleanup. 1726 */ 1727 return -EFAULT; 1728 } 1729 1730 return 0; 1731 } 1732 1733 static int fastrpc_init_attach(struct fastrpc_user *fl, int pd) 1734 { 1735 struct fastrpc_invoke_args args[1]; 1736 int client_id = fl->client_id; 1737 u32 sc; 1738 1739 args[0].ptr = (u64)(uintptr_t) &client_id; 1740 args[0].length = sizeof(client_id); 1741 args[0].fd = -1; 1742 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0); 1743 fl->pd = pd; 1744 1745 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, 1746 sc, &args[0]); 1747 } 1748 1749 static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp) 1750 { 1751 struct fastrpc_invoke_args *args = NULL; 1752 struct fastrpc_invoke inv; 1753 u32 nscalars; 1754 int err; 1755 1756 if (copy_from_user(&inv, argp, sizeof(inv))) 1757 return -EFAULT; 1758 1759 /* nscalars is truncated here to max supported value */ 1760 nscalars = REMOTE_SCALARS_LENGTH(inv.sc); 1761 if (nscalars) { 1762 args = kzalloc_objs(*args, nscalars); 1763 if (!args) 1764 return -ENOMEM; 1765 1766 if (copy_from_user(args, (void __user *)(uintptr_t)inv.args, 1767 nscalars * sizeof(*args))) { 1768 kfree(args); 1769 return -EFAULT; 1770 } 1771 } 1772 1773 err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args); 1774 kfree(args); 1775 1776 return err; 1777 } 1778 1779 static int fastrpc_get_info_from_dsp(struct fastrpc_user *fl, uint32_t *dsp_attr_buf, 1780 uint32_t dsp_attr_buf_len) 1781 { 1782 struct fastrpc_invoke_args args[2] = { 0 }; 1783 1784 /* 1785 * Capability filled in userspace. This carries the information 1786 * about the remoteproc support which is fetched from the remoteproc 1787 * sysfs node by userspace. 1788 */ 1789 dsp_attr_buf[0] = 0; 1790 dsp_attr_buf_len -= 1; 1791 1792 args[0].ptr = (u64)(uintptr_t)&dsp_attr_buf_len; 1793 args[0].length = sizeof(dsp_attr_buf_len); 1794 args[0].fd = -1; 1795 args[1].ptr = (u64)(uintptr_t)&dsp_attr_buf[1]; 1796 args[1].length = dsp_attr_buf_len * sizeof(u32); 1797 args[1].fd = -1; 1798 1799 return fastrpc_internal_invoke(fl, true, FASTRPC_DSP_UTILITIES_HANDLE, 1800 FASTRPC_SCALARS(0, 1, 1), args); 1801 } 1802 1803 static int fastrpc_get_info_from_kernel(struct fastrpc_ioctl_capability *cap, 1804 struct fastrpc_user *fl) 1805 { 1806 struct fastrpc_channel_ctx *cctx = fl->cctx; 1807 uint32_t attribute_id = cap->attribute_id; 1808 uint32_t *dsp_attributes; 1809 unsigned long flags; 1810 int err; 1811 1812 spin_lock_irqsave(&cctx->lock, flags); 1813 /* check if we already have queried dsp for attributes */ 1814 if (cctx->valid_attributes) { 1815 spin_unlock_irqrestore(&cctx->lock, flags); 1816 goto done; 1817 } 1818 spin_unlock_irqrestore(&cctx->lock, flags); 1819 1820 dsp_attributes = kzalloc(FASTRPC_MAX_DSP_ATTRIBUTES_LEN, GFP_KERNEL); 1821 if (!dsp_attributes) 1822 return -ENOMEM; 1823 1824 err = fastrpc_get_info_from_dsp(fl, dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES); 1825 if (err == DSP_UNSUPPORTED_API) { 1826 dev_info(&cctx->rpdev->dev, 1827 "Warning: DSP capabilities not supported\n"); 1828 kfree(dsp_attributes); 1829 return -EOPNOTSUPP; 1830 } else if (err) { 1831 dev_err(&cctx->rpdev->dev, "Error: dsp information is incorrect err: %d\n", err); 1832 kfree(dsp_attributes); 1833 return err; 1834 } 1835 1836 spin_lock_irqsave(&cctx->lock, flags); 1837 memcpy(cctx->dsp_attributes, dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES_LEN); 1838 cctx->valid_attributes = true; 1839 spin_unlock_irqrestore(&cctx->lock, flags); 1840 kfree(dsp_attributes); 1841 done: 1842 cap->capability = cctx->dsp_attributes[attribute_id]; 1843 return 0; 1844 } 1845 1846 static int fastrpc_get_dsp_info(struct fastrpc_user *fl, char __user *argp) 1847 { 1848 struct fastrpc_ioctl_capability cap = {0}; 1849 int err = 0; 1850 1851 if (copy_from_user(&cap, argp, sizeof(cap))) 1852 return -EFAULT; 1853 1854 cap.capability = 0; 1855 1856 if (cap.attribute_id >= FASTRPC_MAX_DSP_ATTRIBUTES) { 1857 dev_err(&fl->cctx->rpdev->dev, "Error: invalid attribute: %d, err: %d\n", 1858 cap.attribute_id, err); 1859 return -EOVERFLOW; 1860 } 1861 1862 err = fastrpc_get_info_from_kernel(&cap, fl); 1863 if (err) 1864 return err; 1865 1866 if (copy_to_user(argp, &cap, sizeof(cap))) 1867 return -EFAULT; 1868 1869 return 0; 1870 } 1871 1872 static int fastrpc_req_munmap_impl(struct fastrpc_user *fl, struct fastrpc_buf *buf) 1873 { 1874 struct fastrpc_invoke_args args[1] = { [0] = { 0 } }; 1875 struct fastrpc_munmap_req_msg req_msg; 1876 struct device *dev = fl->sctx->dev; 1877 int err; 1878 u32 sc; 1879 1880 req_msg.client_id = fl->client_id; 1881 req_msg.size = buf->size; 1882 req_msg.vaddr = buf->raddr; 1883 1884 args[0].ptr = (u64) (uintptr_t) &req_msg; 1885 args[0].length = sizeof(req_msg); 1886 1887 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MUNMAP, 1, 0); 1888 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, 1889 &args[0]); 1890 if (!err) { 1891 dev_dbg(dev, "unmmap\tpt 0x%09lx OK\n", buf->raddr); 1892 spin_lock(&fl->lock); 1893 list_del(&buf->node); 1894 spin_unlock(&fl->lock); 1895 fastrpc_buf_free(buf); 1896 } else { 1897 dev_err(dev, "unmmap\tpt 0x%09lx ERROR\n", buf->raddr); 1898 } 1899 1900 return err; 1901 } 1902 1903 static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp) 1904 { 1905 struct fastrpc_buf *buf = NULL, *iter, *b; 1906 struct fastrpc_req_munmap req; 1907 struct device *dev = fl->sctx->dev; 1908 1909 if (copy_from_user(&req, argp, sizeof(req))) 1910 return -EFAULT; 1911 1912 spin_lock(&fl->lock); 1913 list_for_each_entry_safe(iter, b, &fl->mmaps, node) { 1914 if ((iter->raddr == req.vaddrout) && (iter->size == req.size)) { 1915 buf = iter; 1916 break; 1917 } 1918 } 1919 spin_unlock(&fl->lock); 1920 1921 if (!buf) { 1922 dev_err(dev, "mmap\t\tpt 0x%09llx [len 0x%08llx] not in list\n", 1923 req.vaddrout, req.size); 1924 return -EINVAL; 1925 } 1926 1927 return fastrpc_req_munmap_impl(fl, buf); 1928 } 1929 1930 static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp) 1931 { 1932 struct fastrpc_invoke_args args[3] = { [0 ... 2] = { 0 } }; 1933 struct fastrpc_buf *buf = NULL; 1934 struct fastrpc_mmap_req_msg req_msg; 1935 struct fastrpc_mmap_rsp_msg rsp_msg; 1936 struct fastrpc_phy_page pages; 1937 struct fastrpc_req_mmap req; 1938 struct device *dev = fl->sctx->dev; 1939 int err; 1940 u32 sc; 1941 1942 if (copy_from_user(&req, argp, sizeof(req))) 1943 return -EFAULT; 1944 1945 if (req.flags != ADSP_MMAP_ADD_PAGES && req.flags != ADSP_MMAP_REMOTE_HEAP_ADDR) { 1946 dev_err(dev, "flag not supported 0x%x\n", req.flags); 1947 1948 return -EINVAL; 1949 } 1950 1951 if (req.vaddrin) { 1952 dev_err(dev, "adding user allocated pages is not supported\n"); 1953 return -EINVAL; 1954 } 1955 1956 if (req.flags == ADSP_MMAP_REMOTE_HEAP_ADDR) 1957 err = fastrpc_remote_heap_alloc(fl, dev, req.size, &buf); 1958 else 1959 err = fastrpc_buf_alloc(fl, dev, req.size, &buf); 1960 1961 if (err) { 1962 dev_err(dev, "failed to allocate buffer\n"); 1963 return err; 1964 } 1965 1966 req_msg.client_id = fl->client_id; 1967 req_msg.flags = req.flags; 1968 req_msg.vaddr = req.vaddrin; 1969 req_msg.num = sizeof(pages); 1970 1971 args[0].ptr = (u64) (uintptr_t) &req_msg; 1972 args[0].length = sizeof(req_msg); 1973 1974 pages.addr = buf->dma_addr; 1975 pages.size = buf->size; 1976 1977 args[1].ptr = (u64) (uintptr_t) &pages; 1978 args[1].length = sizeof(pages); 1979 1980 args[2].ptr = (u64) (uintptr_t) &rsp_msg; 1981 args[2].length = sizeof(rsp_msg); 1982 1983 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MMAP, 2, 1); 1984 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, 1985 &args[0]); 1986 if (err) { 1987 dev_err(dev, "mmap error (len 0x%08llx)\n", buf->size); 1988 fastrpc_buf_free(buf); 1989 return err; 1990 } 1991 1992 /* update the buffer to be able to deallocate the memory on the DSP */ 1993 buf->raddr = (uintptr_t) rsp_msg.vaddr; 1994 1995 /* let the client know the address to use */ 1996 req.vaddrout = rsp_msg.vaddr; 1997 1998 /* Add memory to static PD pool, protection thru hypervisor */ 1999 if (req.flags == ADSP_MMAP_REMOTE_HEAP_ADDR && fl->cctx->vmcount) { 2000 u64 src_perms = BIT(QCOM_SCM_VMID_HLOS); 2001 2002 err = qcom_scm_assign_mem(buf->dma_addr, (u64)buf->size, 2003 &src_perms, fl->cctx->vmperms, fl->cctx->vmcount); 2004 if (err) { 2005 dev_err(fl->sctx->dev, 2006 "Failed to assign memory dma_addr %pad size 0x%llx err %d", 2007 &buf->dma_addr, buf->size, err); 2008 goto err_assign; 2009 } 2010 } 2011 2012 spin_lock(&fl->lock); 2013 list_add_tail(&buf->node, &fl->mmaps); 2014 spin_unlock(&fl->lock); 2015 2016 if (copy_to_user((void __user *)argp, &req, sizeof(req))) { 2017 err = -EFAULT; 2018 goto err_assign; 2019 } 2020 2021 dev_dbg(dev, "mmap\t\tpt 0x%09lx OK [len 0x%08llx]\n", 2022 buf->raddr, buf->size); 2023 2024 return 0; 2025 2026 err_assign: 2027 fastrpc_req_munmap_impl(fl, buf); 2028 2029 return err; 2030 } 2031 2032 static int fastrpc_req_mem_unmap_impl(struct fastrpc_user *fl, struct fastrpc_mem_unmap *req) 2033 { 2034 struct fastrpc_invoke_args args[1] = { [0] = { 0 } }; 2035 struct fastrpc_map *map = NULL, *iter, *m; 2036 struct fastrpc_mem_unmap_req_msg req_msg = { 0 }; 2037 int err = 0; 2038 u32 sc; 2039 struct device *dev = fl->sctx->dev; 2040 2041 spin_lock(&fl->lock); 2042 list_for_each_entry_safe(iter, m, &fl->maps, node) { 2043 if ((req->fd < 0 || iter->fd == req->fd) && (iter->raddr == req->vaddr)) { 2044 map = iter; 2045 break; 2046 } 2047 } 2048 2049 spin_unlock(&fl->lock); 2050 2051 if (!map) { 2052 dev_err(dev, "map not in list\n"); 2053 return -EINVAL; 2054 } 2055 2056 req_msg.client_id = fl->client_id; 2057 req_msg.len = map->len; 2058 req_msg.vaddrin = map->raddr; 2059 req_msg.fd = map->fd; 2060 2061 args[0].ptr = (u64) (uintptr_t) &req_msg; 2062 args[0].length = sizeof(req_msg); 2063 2064 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MEM_UNMAP, 1, 0); 2065 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, 2066 &args[0]); 2067 if (err) { 2068 dev_err(dev, "unmmap\tpt fd = %d, 0x%09llx error\n", map->fd, map->raddr); 2069 return err; 2070 } 2071 fastrpc_map_put(map); 2072 2073 return 0; 2074 } 2075 2076 static int fastrpc_req_mem_unmap(struct fastrpc_user *fl, char __user *argp) 2077 { 2078 struct fastrpc_mem_unmap req; 2079 2080 if (copy_from_user(&req, argp, sizeof(req))) 2081 return -EFAULT; 2082 2083 return fastrpc_req_mem_unmap_impl(fl, &req); 2084 } 2085 2086 static int fastrpc_req_mem_map(struct fastrpc_user *fl, char __user *argp) 2087 { 2088 struct fastrpc_invoke_args args[4] = { [0 ... 3] = { 0 } }; 2089 struct fastrpc_mem_map_req_msg req_msg = { 0 }; 2090 struct fastrpc_mmap_rsp_msg rsp_msg = { 0 }; 2091 struct fastrpc_mem_unmap req_unmap = { 0 }; 2092 struct fastrpc_phy_page pages = { 0 }; 2093 struct fastrpc_mem_map req; 2094 struct device *dev = fl->sctx->dev; 2095 struct fastrpc_map *map = NULL; 2096 int err; 2097 u32 sc; 2098 2099 if (copy_from_user(&req, argp, sizeof(req))) 2100 return -EFAULT; 2101 2102 /* create SMMU mapping */ 2103 err = fastrpc_map_create(fl, req.fd, req.length, 0, &map); 2104 if (err) { 2105 dev_err(dev, "failed to map buffer, fd = %d\n", req.fd); 2106 return err; 2107 } 2108 2109 req_msg.client_id = fl->client_id; 2110 req_msg.fd = req.fd; 2111 req_msg.offset = req.offset; 2112 req_msg.vaddrin = req.vaddrin; 2113 map->va = (void *) (uintptr_t) req.vaddrin; 2114 req_msg.flags = req.flags; 2115 req_msg.num = sizeof(pages); 2116 req_msg.data_len = 0; 2117 2118 args[0].ptr = (u64) (uintptr_t) &req_msg; 2119 args[0].length = sizeof(req_msg); 2120 2121 pages.addr = map->dma_addr; 2122 pages.size = map->len; 2123 2124 args[1].ptr = (u64) (uintptr_t) &pages; 2125 args[1].length = sizeof(pages); 2126 2127 args[2].ptr = (u64) (uintptr_t) &pages; 2128 args[2].length = 0; 2129 2130 args[3].ptr = (u64) (uintptr_t) &rsp_msg; 2131 args[3].length = sizeof(rsp_msg); 2132 2133 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MEM_MAP, 3, 1); 2134 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, &args[0]); 2135 if (err) { 2136 dev_err(dev, "mem mmap error, fd %d, vaddr %llx, size %lld\n", 2137 req.fd, req.vaddrin, map->len); 2138 goto err_invoke; 2139 } 2140 2141 /* update the buffer to be able to deallocate the memory on the DSP */ 2142 map->raddr = rsp_msg.vaddr; 2143 2144 /* let the client know the address to use */ 2145 req.vaddrout = rsp_msg.vaddr; 2146 2147 if (copy_to_user((void __user *)argp, &req, sizeof(req))) { 2148 /* unmap the memory and release the buffer */ 2149 req_unmap.vaddr = (uintptr_t) rsp_msg.vaddr; 2150 req_unmap.length = map->len; 2151 fastrpc_req_mem_unmap_impl(fl, &req_unmap); 2152 return -EFAULT; 2153 } 2154 2155 return 0; 2156 2157 err_invoke: 2158 fastrpc_map_put(map); 2159 2160 return err; 2161 } 2162 2163 static long fastrpc_device_ioctl(struct file *file, unsigned int cmd, 2164 unsigned long arg) 2165 { 2166 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data; 2167 char __user *argp = (char __user *)arg; 2168 int err; 2169 2170 switch (cmd) { 2171 case FASTRPC_IOCTL_INVOKE: 2172 err = fastrpc_invoke(fl, argp); 2173 break; 2174 case FASTRPC_IOCTL_INIT_ATTACH: 2175 err = fastrpc_init_attach(fl, ROOT_PD); 2176 break; 2177 case FASTRPC_IOCTL_INIT_ATTACH_SNS: 2178 err = fastrpc_init_attach(fl, SENSORS_PD); 2179 break; 2180 case FASTRPC_IOCTL_INIT_CREATE_STATIC: 2181 err = fastrpc_init_create_static_process(fl, argp); 2182 break; 2183 case FASTRPC_IOCTL_INIT_CREATE: 2184 err = fastrpc_init_create_process(fl, argp); 2185 break; 2186 case FASTRPC_IOCTL_ALLOC_DMA_BUFF: 2187 err = fastrpc_dmabuf_alloc(fl, argp); 2188 break; 2189 case FASTRPC_IOCTL_MMAP: 2190 err = fastrpc_req_mmap(fl, argp); 2191 break; 2192 case FASTRPC_IOCTL_MUNMAP: 2193 err = fastrpc_req_munmap(fl, argp); 2194 break; 2195 case FASTRPC_IOCTL_MEM_MAP: 2196 err = fastrpc_req_mem_map(fl, argp); 2197 break; 2198 case FASTRPC_IOCTL_MEM_UNMAP: 2199 err = fastrpc_req_mem_unmap(fl, argp); 2200 break; 2201 case FASTRPC_IOCTL_GET_DSP_INFO: 2202 err = fastrpc_get_dsp_info(fl, argp); 2203 break; 2204 default: 2205 err = -ENOTTY; 2206 break; 2207 } 2208 2209 return err; 2210 } 2211 2212 static const struct file_operations fastrpc_fops = { 2213 .open = fastrpc_device_open, 2214 .release = fastrpc_device_release, 2215 .unlocked_ioctl = fastrpc_device_ioctl, 2216 .compat_ioctl = fastrpc_device_ioctl, 2217 }; 2218 2219 static int fastrpc_cb_probe(struct platform_device *pdev) 2220 { 2221 struct fastrpc_channel_ctx *cctx; 2222 struct fastrpc_session_ctx *sess; 2223 struct device *dev = &pdev->dev; 2224 int i, sessions = 0; 2225 unsigned long flags; 2226 int rc; 2227 u32 dma_bits; 2228 2229 cctx = dev_get_drvdata(dev->parent); 2230 if (!cctx) 2231 return -EINVAL; 2232 2233 of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions); 2234 2235 spin_lock_irqsave(&cctx->lock, flags); 2236 if (cctx->sesscount >= FASTRPC_MAX_SESSIONS) { 2237 dev_err(&pdev->dev, "too many sessions\n"); 2238 spin_unlock_irqrestore(&cctx->lock, flags); 2239 return -ENOSPC; 2240 } 2241 dma_bits = cctx->soc_data->dma_addr_bits_default; 2242 sess = &cctx->session[cctx->sesscount++]; 2243 sess->used = false; 2244 sess->valid = true; 2245 sess->dev = dev; 2246 dev_set_drvdata(dev, sess); 2247 2248 if (cctx->domain_id == CDSP_DOMAIN_ID) 2249 dma_bits = cctx->soc_data->dma_addr_bits_cdsp; 2250 2251 if (of_property_read_u32(dev->of_node, "reg", &sess->sid)) 2252 dev_info(dev, "FastRPC Session ID not specified in DT\n"); 2253 2254 if (sessions > 0) { 2255 struct fastrpc_session_ctx *dup_sess; 2256 2257 for (i = 1; i < sessions; i++) { 2258 if (cctx->sesscount >= FASTRPC_MAX_SESSIONS) 2259 break; 2260 dup_sess = &cctx->session[cctx->sesscount++]; 2261 memcpy(dup_sess, sess, sizeof(*dup_sess)); 2262 } 2263 } 2264 spin_unlock_irqrestore(&cctx->lock, flags); 2265 rc = dma_set_mask(dev, DMA_BIT_MASK(dma_bits)); 2266 if (rc) { 2267 dev_err(dev, "%u-bit DMA enable failed\n", dma_bits); 2268 return rc; 2269 } 2270 2271 return 0; 2272 } 2273 2274 static void fastrpc_cb_remove(struct platform_device *pdev) 2275 { 2276 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent); 2277 struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev); 2278 unsigned long flags; 2279 int i; 2280 2281 spin_lock_irqsave(&cctx->lock, flags); 2282 for (i = 0; i < FASTRPC_MAX_SESSIONS; i++) { 2283 if (cctx->session[i].sid == sess->sid) { 2284 cctx->session[i].valid = false; 2285 cctx->sesscount--; 2286 } 2287 } 2288 spin_unlock_irqrestore(&cctx->lock, flags); 2289 } 2290 2291 static const struct of_device_id fastrpc_match_table[] = { 2292 { .compatible = "qcom,fastrpc-compute-cb", }, 2293 {} 2294 }; 2295 2296 static struct platform_driver fastrpc_cb_driver = { 2297 .probe = fastrpc_cb_probe, 2298 .remove = fastrpc_cb_remove, 2299 .driver = { 2300 .name = "qcom,fastrpc-cb", 2301 .of_match_table = fastrpc_match_table, 2302 .suppress_bind_attrs = true, 2303 }, 2304 }; 2305 2306 static int fastrpc_device_register(struct device *dev, struct fastrpc_channel_ctx *cctx, 2307 bool is_secured, const char *domain) 2308 { 2309 struct fastrpc_device *fdev; 2310 int err; 2311 2312 fdev = devm_kzalloc(dev, sizeof(*fdev), GFP_KERNEL); 2313 if (!fdev) 2314 return -ENOMEM; 2315 2316 fdev->secure = is_secured; 2317 fdev->cctx = cctx; 2318 fdev->miscdev.minor = MISC_DYNAMIC_MINOR; 2319 fdev->miscdev.fops = &fastrpc_fops; 2320 fdev->miscdev.name = devm_kasprintf(dev, GFP_KERNEL, "fastrpc-%s%s", 2321 domain, is_secured ? "-secure" : ""); 2322 if (!fdev->miscdev.name) 2323 return -ENOMEM; 2324 2325 err = misc_register(&fdev->miscdev); 2326 if (!err) { 2327 if (is_secured) 2328 cctx->secure_fdevice = fdev; 2329 else 2330 cctx->fdevice = fdev; 2331 } 2332 2333 return err; 2334 } 2335 2336 static int fastrpc_get_domain_id(const char *domain) 2337 { 2338 if (!strncmp(domain, "adsp", 4)) 2339 return ADSP_DOMAIN_ID; 2340 else if (!strncmp(domain, "cdsp", 4)) 2341 return CDSP_DOMAIN_ID; 2342 else if (!strncmp(domain, "mdsp", 4)) 2343 return MDSP_DOMAIN_ID; 2344 else if (!strncmp(domain, "sdsp", 4)) 2345 return SDSP_DOMAIN_ID; 2346 else if (!strncmp(domain, "gdsp", 4)) 2347 return GDSP_DOMAIN_ID; 2348 2349 return -EINVAL; 2350 } 2351 2352 static const struct fastrpc_soc_data kaanapali_soc_data = { 2353 .sid_pos = 56, 2354 .dma_addr_bits_cdsp = 34, 2355 .dma_addr_bits_default = 32, 2356 }; 2357 2358 static const struct fastrpc_soc_data default_soc_data = { 2359 .sid_pos = 32, 2360 .dma_addr_bits_cdsp = 32, 2361 .dma_addr_bits_default = 32, 2362 }; 2363 2364 static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) 2365 { 2366 struct device *rdev = &rpdev->dev; 2367 struct fastrpc_channel_ctx *data; 2368 int i, err, domain_id = -1, vmcount; 2369 const char *domain; 2370 bool secure_dsp; 2371 unsigned int vmids[FASTRPC_MAX_VMIDS]; 2372 const struct fastrpc_soc_data *soc_data; 2373 2374 soc_data = device_get_match_data(rdev); 2375 2376 err = of_property_read_string(rdev->of_node, "label", &domain); 2377 if (err) { 2378 dev_info(rdev, "FastRPC Domain not specified in DT\n"); 2379 return err; 2380 } 2381 2382 domain_id = fastrpc_get_domain_id(domain); 2383 2384 if (domain_id < 0) { 2385 dev_info(rdev, "FastRPC Domain %s not supported\n", domain); 2386 return -EINVAL; 2387 } 2388 2389 if (of_reserved_mem_device_init_by_idx(rdev, rdev->of_node, 0)) 2390 dev_info(rdev, "no reserved DMA memory for FASTRPC\n"); 2391 2392 vmcount = of_property_read_variable_u32_array(rdev->of_node, 2393 "qcom,vmids", &vmids[0], 0, FASTRPC_MAX_VMIDS); 2394 if (vmcount < 0) 2395 vmcount = 0; 2396 else if (!qcom_scm_is_available()) 2397 return -EPROBE_DEFER; 2398 2399 data = kzalloc_obj(*data); 2400 if (!data) 2401 return -ENOMEM; 2402 2403 if (vmcount) { 2404 data->vmcount = vmcount; 2405 for (i = 0; i < data->vmcount; i++) { 2406 data->vmperms[i].vmid = vmids[i]; 2407 data->vmperms[i].perm = QCOM_SCM_PERM_RWX; 2408 } 2409 } 2410 2411 if (domain_id == SDSP_DOMAIN_ID) { 2412 struct resource res; 2413 u64 src_perms; 2414 2415 err = of_reserved_mem_region_to_resource(rdev->of_node, 0, &res); 2416 if (!err) { 2417 src_perms = BIT(QCOM_SCM_VMID_HLOS); 2418 2419 err = qcom_scm_assign_mem(res.start, resource_size(&res), &src_perms, 2420 data->vmperms, data->vmcount); 2421 if (err) 2422 goto err_free_data; 2423 } 2424 2425 } 2426 2427 secure_dsp = !(of_property_read_bool(rdev->of_node, "qcom,non-secure-domain")); 2428 data->secure = secure_dsp; 2429 data->soc_data = soc_data; 2430 2431 switch (domain_id) { 2432 case ADSP_DOMAIN_ID: 2433 case MDSP_DOMAIN_ID: 2434 case SDSP_DOMAIN_ID: 2435 /* Unsigned PD offloading is only supported on CDSP and GDSP */ 2436 data->unsigned_support = false; 2437 err = fastrpc_device_register(rdev, data, secure_dsp, domain); 2438 if (err) 2439 goto err_free_data; 2440 break; 2441 case CDSP_DOMAIN_ID: 2442 case GDSP_DOMAIN_ID: 2443 data->unsigned_support = true; 2444 /* Create both device nodes so that we can allow both Signed and Unsigned PD */ 2445 err = fastrpc_device_register(rdev, data, true, domain); 2446 if (err) 2447 goto err_free_data; 2448 2449 err = fastrpc_device_register(rdev, data, false, domain); 2450 if (err) 2451 goto err_deregister_fdev; 2452 break; 2453 default: 2454 err = -EINVAL; 2455 goto err_free_data; 2456 } 2457 2458 kref_init(&data->refcount); 2459 2460 rdev->dma_mask = &data->dma_mask; 2461 dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32)); 2462 INIT_LIST_HEAD(&data->users); 2463 INIT_LIST_HEAD(&data->invoke_interrupted_mmaps); 2464 spin_lock_init(&data->lock); 2465 idr_init(&data->ctx_idr); 2466 data->domain_id = domain_id; 2467 data->rpdev = rpdev; 2468 dev_set_drvdata(&rpdev->dev, data); 2469 2470 err = of_platform_populate(rdev->of_node, NULL, NULL, rdev); 2471 if (err) 2472 goto err_deregister_fdev; 2473 2474 return 0; 2475 2476 err_deregister_fdev: 2477 if (data->fdevice) 2478 misc_deregister(&data->fdevice->miscdev); 2479 if (data->secure_fdevice) 2480 misc_deregister(&data->secure_fdevice->miscdev); 2481 2482 err_free_data: 2483 kfree(data); 2484 return err; 2485 } 2486 2487 static void fastrpc_notify_users(struct fastrpc_user *user) 2488 { 2489 struct fastrpc_invoke_ctx *ctx; 2490 2491 spin_lock(&user->lock); 2492 list_for_each_entry(ctx, &user->pending, node) { 2493 ctx->retval = -EPIPE; 2494 complete(&ctx->work); 2495 } 2496 spin_unlock(&user->lock); 2497 } 2498 2499 static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev) 2500 { 2501 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev); 2502 struct fastrpc_buf *buf, *b; 2503 struct fastrpc_user *user; 2504 unsigned long flags; 2505 2506 /* No invocations past this point */ 2507 spin_lock_irqsave(&cctx->lock, flags); 2508 cctx->rpdev = NULL; 2509 list_for_each_entry(user, &cctx->users, user) 2510 fastrpc_notify_users(user); 2511 spin_unlock_irqrestore(&cctx->lock, flags); 2512 2513 if (cctx->fdevice) 2514 misc_deregister(&cctx->fdevice->miscdev); 2515 2516 if (cctx->secure_fdevice) 2517 misc_deregister(&cctx->secure_fdevice->miscdev); 2518 2519 list_for_each_entry_safe(buf, b, &cctx->invoke_interrupted_mmaps, node) 2520 list_del(&buf->node); 2521 2522 if (cctx->remote_heap) 2523 fastrpc_buf_free(cctx->remote_heap); 2524 2525 of_platform_depopulate(&rpdev->dev); 2526 2527 fastrpc_channel_ctx_put(cctx); 2528 } 2529 2530 static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data, 2531 int len, void *priv, u32 addr) 2532 { 2533 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev); 2534 struct fastrpc_invoke_rsp *rsp = data; 2535 struct fastrpc_invoke_ctx *ctx; 2536 unsigned long flags; 2537 unsigned long ctxid; 2538 2539 if (len < sizeof(*rsp)) 2540 return -EINVAL; 2541 2542 if (!cctx) 2543 return -ENODEV; 2544 2545 ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4); 2546 2547 spin_lock_irqsave(&cctx->lock, flags); 2548 ctx = idr_find(&cctx->ctx_idr, ctxid); 2549 spin_unlock_irqrestore(&cctx->lock, flags); 2550 2551 if (!ctx) { 2552 dev_err(&rpdev->dev, "No context ID matches response\n"); 2553 return -ENOENT; 2554 } 2555 2556 ctx->retval = rsp->retval; 2557 complete(&ctx->work); 2558 2559 /* 2560 * The DMA buffer associated with the context cannot be freed in 2561 * interrupt context so schedule it through a worker thread to 2562 * avoid a kernel BUG. 2563 */ 2564 schedule_work(&ctx->put_work); 2565 2566 return 0; 2567 } 2568 2569 static const struct of_device_id fastrpc_rpmsg_of_match[] = { 2570 { .compatible = "qcom,kaanapali-fastrpc", .data = &kaanapali_soc_data }, 2571 { .compatible = "qcom,fastrpc", .data = &default_soc_data }, 2572 { }, 2573 }; 2574 MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match); 2575 2576 static struct rpmsg_driver fastrpc_driver = { 2577 .probe = fastrpc_rpmsg_probe, 2578 .remove = fastrpc_rpmsg_remove, 2579 .callback = fastrpc_rpmsg_callback, 2580 .drv = { 2581 .name = "qcom,fastrpc", 2582 .of_match_table = fastrpc_rpmsg_of_match, 2583 }, 2584 }; 2585 2586 static int fastrpc_init(void) 2587 { 2588 int ret; 2589 2590 ret = platform_driver_register(&fastrpc_cb_driver); 2591 if (ret < 0) { 2592 pr_err("fastrpc: failed to register cb driver\n"); 2593 return ret; 2594 } 2595 2596 ret = register_rpmsg_driver(&fastrpc_driver); 2597 if (ret < 0) { 2598 pr_err("fastrpc: failed to register rpmsg driver\n"); 2599 platform_driver_unregister(&fastrpc_cb_driver); 2600 return ret; 2601 } 2602 2603 return 0; 2604 } 2605 module_init(fastrpc_init); 2606 2607 static void fastrpc_exit(void) 2608 { 2609 platform_driver_unregister(&fastrpc_cb_driver); 2610 unregister_rpmsg_driver(&fastrpc_driver); 2611 } 2612 module_exit(fastrpc_exit); 2613 2614 MODULE_DESCRIPTION("Qualcomm FastRPC"); 2615 MODULE_LICENSE("GPL v2"); 2616 MODULE_IMPORT_NS("DMA_BUF"); 2617