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