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