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