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