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