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