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