1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
3 // Copyright (c) 2018, Linaro Limited
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>
16 #include <linux/sort.h>
17 #include <linux/of_platform.h>
18 #include <linux/rpmsg.h>
19 #include <linux/scatterlist.h>
20 #include <linux/slab.h>
21 #include <linux/qcom_scm.h>
22 #include <uapi/misc/fastrpc.h>
24 #define ADSP_DOMAIN_ID (0)
25 #define MDSP_DOMAIN_ID (1)
26 #define SDSP_DOMAIN_ID (2)
27 #define CDSP_DOMAIN_ID (3)
28 #define FASTRPC_DEV_MAX 4 /* adsp, mdsp, slpi, cdsp*/
29 #define FASTRPC_MAX_SESSIONS 14
30 #define FASTRPC_MAX_VMIDS 16
31 #define FASTRPC_ALIGN 128
32 #define FASTRPC_MAX_FDLIST 16
33 #define FASTRPC_MAX_CRCLIST 64
34 #define FASTRPC_PHYS(p) ((p) & 0xffffffff)
35 #define FASTRPC_CTX_MAX (256)
36 #define FASTRPC_INIT_HANDLE 1
37 #define FASTRPC_DSP_UTILITIES_HANDLE 2
38 #define FASTRPC_CTXID_MASK (0xFF0)
39 #define INIT_FILELEN_MAX (2 * 1024 * 1024)
40 #define FASTRPC_DEVICE_NAME "fastrpc"
41 #define ADSP_MMAP_ADD_PAGES 0x1000
42 #define DSP_UNSUPPORTED_API (0x80000414)
43 /* MAX NUMBER of DSP ATTRIBUTES SUPPORTED */
44 #define FASTRPC_MAX_DSP_ATTRIBUTES (256)
45 #define FASTRPC_MAX_DSP_ATTRIBUTES_LEN (sizeof(u32) * FASTRPC_MAX_DSP_ATTRIBUTES)
47 /* Retrives number of input buffers from the scalars parameter */
48 #define REMOTE_SCALARS_INBUFS(sc) (((sc) >> 16) & 0x0ff)
50 /* Retrives number of output buffers from the scalars parameter */
51 #define REMOTE_SCALARS_OUTBUFS(sc) (((sc) >> 8) & 0x0ff)
53 /* Retrives number of input handles from the scalars parameter */
54 #define REMOTE_SCALARS_INHANDLES(sc) (((sc) >> 4) & 0x0f)
56 /* Retrives number of output handles from the scalars parameter */
57 #define REMOTE_SCALARS_OUTHANDLES(sc) ((sc) & 0x0f)
59 #define REMOTE_SCALARS_LENGTH(sc) (REMOTE_SCALARS_INBUFS(sc) + \
60 REMOTE_SCALARS_OUTBUFS(sc) + \
61 REMOTE_SCALARS_INHANDLES(sc)+ \
62 REMOTE_SCALARS_OUTHANDLES(sc))
63 #define FASTRPC_BUILD_SCALARS(attr, method, in, out, oin, oout) \
64 (((attr & 0x07) << 29) | \
65 ((method & 0x1f) << 24) | \
66 ((in & 0xff) << 16) | \
67 ((out & 0xff) << 8) | \
68 ((oin & 0x0f) << 4) | \
71 #define FASTRPC_SCALARS(method, in, out) \
72 FASTRPC_BUILD_SCALARS(0, method, in, out, 0, 0)
74 #define FASTRPC_CREATE_PROCESS_NARGS 6
75 /* Remote Method id table */
76 #define FASTRPC_RMID_INIT_ATTACH 0
77 #define FASTRPC_RMID_INIT_RELEASE 1
78 #define FASTRPC_RMID_INIT_MMAP 4
79 #define FASTRPC_RMID_INIT_MUNMAP 5
80 #define FASTRPC_RMID_INIT_CREATE 6
81 #define FASTRPC_RMID_INIT_CREATE_ATTR 7
82 #define FASTRPC_RMID_INIT_CREATE_STATIC 8
83 #define FASTRPC_RMID_INIT_MEM_MAP 10
84 #define FASTRPC_RMID_INIT_MEM_UNMAP 11
86 /* Protection Domain(PD) ids */
87 #define AUDIO_PD (0) /* also GUEST_OS PD? */
89 #define SENSORS_PD (2)
91 #define miscdev_to_fdevice(d) container_of(d, struct fastrpc_device, miscdev)
93 static const char *domains[FASTRPC_DEV_MAX] = { "adsp", "mdsp",
95 struct fastrpc_phy_page {
96 u64 addr; /* physical address */
97 u64 size; /* size of contiguous region */
100 struct fastrpc_invoke_buf {
101 u32 num; /* number of contiguous regions */
102 u32 pgidx; /* index to start of contiguous region */
105 struct fastrpc_remote_dmahandle {
106 s32 fd; /* dma handle fd */
107 u32 offset; /* dma handle offset */
108 u32 len; /* dma handle length */
111 struct fastrpc_remote_buf {
112 u64 pv; /* buffer pointer */
113 u64 len; /* length of buffer */
116 union fastrpc_remote_arg {
117 struct fastrpc_remote_buf buf;
118 struct fastrpc_remote_dmahandle dma;
121 struct fastrpc_mmap_rsp_msg {
125 struct fastrpc_mmap_req_msg {
132 struct fastrpc_mem_map_req_msg {
142 struct fastrpc_munmap_req_msg {
148 struct fastrpc_mem_unmap_req_msg {
156 int pid; /* process group id */
157 int tid; /* thread id */
158 u64 ctx; /* invoke caller context */
159 u32 handle; /* handle to invoke */
160 u32 sc; /* scalars structure describing the data */
161 u64 addr; /* physical address */
162 u64 size; /* size of contiguous region */
165 struct fastrpc_invoke_rsp {
166 u64 ctx; /* invoke caller context */
167 int retval; /* invoke return value */
170 struct fastrpc_buf_overlap {
180 struct fastrpc_user *fl;
181 struct dma_buf *dmabuf;
186 /* Lock for dma buf attachments */
188 struct list_head attachments;
190 struct list_head node; /* list of user requested mmaps */
194 struct fastrpc_dma_buf_attachment {
197 struct list_head node;
201 struct list_head node;
202 struct fastrpc_user *fl;
205 struct sg_table *table;
206 struct dma_buf_attachment *attach;
213 struct kref refcount;
216 struct fastrpc_invoke_ctx {
226 struct kref refcount;
227 struct list_head node; /* list of ctxs */
228 struct completion work;
229 struct work_struct put_work;
230 struct fastrpc_msg msg;
231 struct fastrpc_user *fl;
232 union fastrpc_remote_arg *rpra;
233 struct fastrpc_map **maps;
234 struct fastrpc_buf *buf;
235 struct fastrpc_invoke_args *args;
236 struct fastrpc_buf_overlap *olaps;
237 struct fastrpc_channel_ctx *cctx;
240 struct fastrpc_session_ctx {
247 struct fastrpc_channel_ctx {
252 struct qcom_scm_vmperm vmperms[FASTRPC_MAX_VMIDS];
253 struct rpmsg_device *rpdev;
254 struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS];
257 struct list_head users;
258 struct kref refcount;
259 /* Flag if dsp attributes are cached */
260 bool valid_attributes;
261 u32 dsp_attributes[FASTRPC_MAX_DSP_ATTRIBUTES];
262 struct fastrpc_device *secure_fdevice;
263 struct fastrpc_device *fdevice;
265 bool unsigned_support;
268 struct fastrpc_device {
269 struct fastrpc_channel_ctx *cctx;
270 struct miscdevice miscdev;
274 struct fastrpc_user {
275 struct list_head user;
276 struct list_head maps;
277 struct list_head pending;
278 struct list_head mmaps;
280 struct fastrpc_channel_ctx *cctx;
281 struct fastrpc_session_ctx *sctx;
282 struct fastrpc_buf *init_mem;
289 /* lock for allocations */
293 static void fastrpc_free_map(struct kref *ref)
295 struct fastrpc_map *map;
297 map = container_of(ref, struct fastrpc_map, refcount);
300 if (map->attr & FASTRPC_ATTR_SECUREMAP) {
301 struct qcom_scm_vmperm perm;
304 perm.vmid = QCOM_SCM_VMID_HLOS;
305 perm.perm = QCOM_SCM_PERM_RWX;
306 err = qcom_scm_assign_mem(map->phys, map->size,
307 &(map->fl->cctx->vmperms[0].vmid), &perm, 1);
309 dev_err(map->fl->sctx->dev, "Failed to assign memory phys 0x%llx size 0x%llx err %d",
310 map->phys, map->size, err);
314 dma_buf_unmap_attachment_unlocked(map->attach, map->table,
316 dma_buf_detach(map->buf, map->attach);
317 dma_buf_put(map->buf);
323 static void fastrpc_map_put(struct fastrpc_map *map)
326 kref_put(&map->refcount, fastrpc_free_map);
329 static void fastrpc_map_get(struct fastrpc_map *map)
332 kref_get(&map->refcount);
336 static int fastrpc_map_lookup(struct fastrpc_user *fl, int fd,
337 struct fastrpc_map **ppmap)
339 struct fastrpc_map *map = NULL;
341 mutex_lock(&fl->mutex);
342 list_for_each_entry(map, &fl->maps, node) {
345 mutex_unlock(&fl->mutex);
349 mutex_unlock(&fl->mutex);
354 static int fastrpc_map_find(struct fastrpc_user *fl, int fd,
355 struct fastrpc_map **ppmap)
357 int ret = fastrpc_map_lookup(fl, fd, ppmap);
360 fastrpc_map_get(*ppmap);
365 static void fastrpc_buf_free(struct fastrpc_buf *buf)
367 dma_free_coherent(buf->dev, buf->size, buf->virt,
368 FASTRPC_PHYS(buf->phys));
372 static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
373 u64 size, struct fastrpc_buf **obuf)
375 struct fastrpc_buf *buf;
377 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
381 INIT_LIST_HEAD(&buf->attachments);
382 INIT_LIST_HEAD(&buf->node);
383 mutex_init(&buf->lock);
392 buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys,
395 mutex_destroy(&buf->lock);
400 if (fl->sctx && fl->sctx->sid)
401 buf->phys += ((u64)fl->sctx->sid << 32);
408 static void fastrpc_channel_ctx_free(struct kref *ref)
410 struct fastrpc_channel_ctx *cctx;
412 cctx = container_of(ref, struct fastrpc_channel_ctx, refcount);
417 static void fastrpc_channel_ctx_get(struct fastrpc_channel_ctx *cctx)
419 kref_get(&cctx->refcount);
422 static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx)
424 kref_put(&cctx->refcount, fastrpc_channel_ctx_free);
427 static void fastrpc_context_free(struct kref *ref)
429 struct fastrpc_invoke_ctx *ctx;
430 struct fastrpc_channel_ctx *cctx;
434 ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
437 for (i = 0; i < ctx->nbufs; i++)
438 fastrpc_map_put(ctx->maps[i]);
441 fastrpc_buf_free(ctx->buf);
443 spin_lock_irqsave(&cctx->lock, flags);
444 idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4);
445 spin_unlock_irqrestore(&cctx->lock, flags);
451 fastrpc_channel_ctx_put(cctx);
454 static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx)
456 kref_get(&ctx->refcount);
459 static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx)
461 kref_put(&ctx->refcount, fastrpc_context_free);
464 static void fastrpc_context_put_wq(struct work_struct *work)
466 struct fastrpc_invoke_ctx *ctx =
467 container_of(work, struct fastrpc_invoke_ctx, put_work);
469 fastrpc_context_put(ctx);
472 #define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)
473 static int olaps_cmp(const void *a, const void *b)
475 struct fastrpc_buf_overlap *pa = (struct fastrpc_buf_overlap *)a;
476 struct fastrpc_buf_overlap *pb = (struct fastrpc_buf_overlap *)b;
477 /* sort with lowest starting buffer first */
478 int st = CMP(pa->start, pb->start);
479 /* sort with highest ending buffer first */
480 int ed = CMP(pb->end, pa->end);
482 return st == 0 ? ed : st;
485 static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
490 for (i = 0; i < ctx->nbufs; ++i) {
491 ctx->olaps[i].start = ctx->args[i].ptr;
492 ctx->olaps[i].end = ctx->olaps[i].start + ctx->args[i].length;
493 ctx->olaps[i].raix = i;
496 sort(ctx->olaps, ctx->nbufs, sizeof(*ctx->olaps), olaps_cmp, NULL);
498 for (i = 0; i < ctx->nbufs; ++i) {
499 /* Falling inside previous range */
500 if (ctx->olaps[i].start < max_end) {
501 ctx->olaps[i].mstart = max_end;
502 ctx->olaps[i].mend = ctx->olaps[i].end;
503 ctx->olaps[i].offset = max_end - ctx->olaps[i].start;
505 if (ctx->olaps[i].end > max_end) {
506 max_end = ctx->olaps[i].end;
508 ctx->olaps[i].mend = 0;
509 ctx->olaps[i].mstart = 0;
513 ctx->olaps[i].mend = ctx->olaps[i].end;
514 ctx->olaps[i].mstart = ctx->olaps[i].start;
515 ctx->olaps[i].offset = 0;
516 max_end = ctx->olaps[i].end;
521 static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
522 struct fastrpc_user *user, u32 kernel, u32 sc,
523 struct fastrpc_invoke_args *args)
525 struct fastrpc_channel_ctx *cctx = user->cctx;
526 struct fastrpc_invoke_ctx *ctx = NULL;
530 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
532 return ERR_PTR(-ENOMEM);
534 INIT_LIST_HEAD(&ctx->node);
536 ctx->nscalars = REMOTE_SCALARS_LENGTH(sc);
537 ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) +
538 REMOTE_SCALARS_OUTBUFS(sc);
541 ctx->maps = kcalloc(ctx->nscalars,
542 sizeof(*ctx->maps), GFP_KERNEL);
545 return ERR_PTR(-ENOMEM);
547 ctx->olaps = kcalloc(ctx->nscalars,
548 sizeof(*ctx->olaps), GFP_KERNEL);
552 return ERR_PTR(-ENOMEM);
555 fastrpc_get_buff_overlaps(ctx);
558 /* Released in fastrpc_context_put() */
559 fastrpc_channel_ctx_get(cctx);
563 ctx->pid = current->pid;
564 ctx->tgid = user->tgid;
566 init_completion(&ctx->work);
567 INIT_WORK(&ctx->put_work, fastrpc_context_put_wq);
569 spin_lock(&user->lock);
570 list_add_tail(&ctx->node, &user->pending);
571 spin_unlock(&user->lock);
573 spin_lock_irqsave(&cctx->lock, flags);
574 ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1,
575 FASTRPC_CTX_MAX, GFP_ATOMIC);
577 spin_unlock_irqrestore(&cctx->lock, flags);
580 ctx->ctxid = ret << 4;
581 spin_unlock_irqrestore(&cctx->lock, flags);
583 kref_init(&ctx->refcount);
587 spin_lock(&user->lock);
588 list_del(&ctx->node);
589 spin_unlock(&user->lock);
590 fastrpc_channel_ctx_put(cctx);
598 static struct sg_table *
599 fastrpc_map_dma_buf(struct dma_buf_attachment *attachment,
600 enum dma_data_direction dir)
602 struct fastrpc_dma_buf_attachment *a = attachment->priv;
603 struct sg_table *table;
608 ret = dma_map_sgtable(attachment->dev, table, dir, 0);
610 table = ERR_PTR(ret);
614 static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach,
615 struct sg_table *table,
616 enum dma_data_direction dir)
618 dma_unmap_sgtable(attach->dev, table, dir, 0);
621 static void fastrpc_release(struct dma_buf *dmabuf)
623 struct fastrpc_buf *buffer = dmabuf->priv;
625 fastrpc_buf_free(buffer);
628 static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf,
629 struct dma_buf_attachment *attachment)
631 struct fastrpc_dma_buf_attachment *a;
632 struct fastrpc_buf *buffer = dmabuf->priv;
635 a = kzalloc(sizeof(*a), GFP_KERNEL);
639 ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt,
640 FASTRPC_PHYS(buffer->phys), buffer->size);
642 dev_err(buffer->dev, "failed to get scatterlist from DMA API\n");
647 a->dev = attachment->dev;
648 INIT_LIST_HEAD(&a->node);
649 attachment->priv = a;
651 mutex_lock(&buffer->lock);
652 list_add(&a->node, &buffer->attachments);
653 mutex_unlock(&buffer->lock);
658 static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf,
659 struct dma_buf_attachment *attachment)
661 struct fastrpc_dma_buf_attachment *a = attachment->priv;
662 struct fastrpc_buf *buffer = dmabuf->priv;
664 mutex_lock(&buffer->lock);
666 mutex_unlock(&buffer->lock);
667 sg_free_table(&a->sgt);
671 static int fastrpc_vmap(struct dma_buf *dmabuf, struct iosys_map *map)
673 struct fastrpc_buf *buf = dmabuf->priv;
675 iosys_map_set_vaddr(map, buf->virt);
680 static int fastrpc_mmap(struct dma_buf *dmabuf,
681 struct vm_area_struct *vma)
683 struct fastrpc_buf *buf = dmabuf->priv;
684 size_t size = vma->vm_end - vma->vm_start;
686 dma_resv_assert_held(dmabuf->resv);
688 return dma_mmap_coherent(buf->dev, vma, buf->virt,
689 FASTRPC_PHYS(buf->phys), size);
692 static const struct dma_buf_ops fastrpc_dma_buf_ops = {
693 .attach = fastrpc_dma_buf_attach,
694 .detach = fastrpc_dma_buf_detatch,
695 .map_dma_buf = fastrpc_map_dma_buf,
696 .unmap_dma_buf = fastrpc_unmap_dma_buf,
697 .mmap = fastrpc_mmap,
698 .vmap = fastrpc_vmap,
699 .release = fastrpc_release,
702 static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
703 u64 len, u32 attr, struct fastrpc_map **ppmap)
705 struct fastrpc_session_ctx *sess = fl->sctx;
706 struct fastrpc_map *map = NULL;
709 if (!fastrpc_map_find(fl, fd, ppmap))
712 map = kzalloc(sizeof(*map), GFP_KERNEL);
716 INIT_LIST_HEAD(&map->node);
719 map->buf = dma_buf_get(fd);
720 if (IS_ERR(map->buf)) {
721 err = PTR_ERR(map->buf);
725 map->attach = dma_buf_attach(map->buf, sess->dev);
726 if (IS_ERR(map->attach)) {
727 dev_err(sess->dev, "Failed to attach dmabuf\n");
728 err = PTR_ERR(map->attach);
732 map->table = dma_buf_map_attachment_unlocked(map->attach, DMA_BIDIRECTIONAL);
733 if (IS_ERR(map->table)) {
734 err = PTR_ERR(map->table);
738 map->phys = sg_dma_address(map->table->sgl);
739 map->phys += ((u64)fl->sctx->sid << 32);
741 map->va = sg_virt(map->table->sgl);
743 kref_init(&map->refcount);
745 if (attr & FASTRPC_ATTR_SECUREMAP) {
747 * If subsystem VMIDs are defined in DTSI, then do
748 * hyp_assign from HLOS to those VM(s)
750 unsigned int perms = BIT(QCOM_SCM_VMID_HLOS);
753 err = qcom_scm_assign_mem(map->phys, (u64)map->size, &perms,
754 fl->cctx->vmperms, fl->cctx->vmcount);
756 dev_err(sess->dev, "Failed to assign memory with phys 0x%llx size 0x%llx err %d",
757 map->phys, map->size, err);
761 spin_lock(&fl->lock);
762 list_add_tail(&map->node, &fl->maps);
763 spin_unlock(&fl->lock);
769 dma_buf_detach(map->buf, map->attach);
771 dma_buf_put(map->buf);
779 * Fastrpc payload buffer with metadata looks like:
781 * >>>>>> START of METADATA <<<<<<<<<
782 * +---------------------------------+
784 * | type:(union fastrpc_remote_arg)|
786 * +---------------------------------+
787 * | Invoke Buffer list |
788 * | type:(struct fastrpc_invoke_buf)|
790 * +---------------------------------+
792 * | type:(struct fastrpc_phy_page) |
794 * +---------------------------------+
796 * |(can be specific to SoC/Firmware)|
797 * +---------------------------------+
798 * >>>>>>>> END of METADATA <<<<<<<<<
799 * +---------------------------------+
802 * +---------------------------------+
805 static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx)
809 size = (sizeof(struct fastrpc_remote_buf) +
810 sizeof(struct fastrpc_invoke_buf) +
811 sizeof(struct fastrpc_phy_page)) * ctx->nscalars +
812 sizeof(u64) * FASTRPC_MAX_FDLIST +
813 sizeof(u32) * FASTRPC_MAX_CRCLIST;
818 static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen)
823 size = ALIGN(metalen, FASTRPC_ALIGN);
824 for (oix = 0; oix < ctx->nbufs; oix++) {
825 int i = ctx->olaps[oix].raix;
827 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) {
829 if (ctx->olaps[oix].offset == 0)
830 size = ALIGN(size, FASTRPC_ALIGN);
832 size += (ctx->olaps[oix].mend - ctx->olaps[oix].mstart);
839 static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
841 struct device *dev = ctx->fl->sctx->dev;
844 for (i = 0; i < ctx->nscalars; ++i) {
846 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 ||
847 ctx->args[i].length == 0)
850 err = fastrpc_map_create(ctx->fl, ctx->args[i].fd,
851 ctx->args[i].length, ctx->args[i].attr, &ctx->maps[i]);
853 dev_err(dev, "Error Creating map %d\n", err);
861 static struct fastrpc_invoke_buf *fastrpc_invoke_buf_start(union fastrpc_remote_arg *pra, int len)
863 return (struct fastrpc_invoke_buf *)(&pra[len]);
866 static struct fastrpc_phy_page *fastrpc_phy_page_start(struct fastrpc_invoke_buf *buf, int len)
868 return (struct fastrpc_phy_page *)(&buf[len]);
871 static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
873 struct device *dev = ctx->fl->sctx->dev;
874 union fastrpc_remote_arg *rpra;
875 struct fastrpc_invoke_buf *list;
876 struct fastrpc_phy_page *pages;
877 int inbufs, i, oix, err = 0;
878 u64 len, rlen, pkt_size;
879 u64 pg_start, pg_end;
883 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
884 metalen = fastrpc_get_meta_size(ctx);
885 pkt_size = fastrpc_get_payload_size(ctx, metalen);
887 err = fastrpc_create_maps(ctx);
891 ctx->msg_sz = pkt_size;
893 err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf);
897 rpra = ctx->buf->virt;
898 list = fastrpc_invoke_buf_start(rpra, ctx->nscalars);
899 pages = fastrpc_phy_page_start(list, ctx->nscalars);
900 args = (uintptr_t)ctx->buf->virt + metalen;
901 rlen = pkt_size - metalen;
904 for (oix = 0; oix < ctx->nbufs; ++oix) {
907 i = ctx->olaps[oix].raix;
908 len = ctx->args[i].length;
911 rpra[i].buf.len = len;
912 list[i].num = len ? 1 : 0;
919 struct vm_area_struct *vma = NULL;
921 rpra[i].buf.pv = (u64) ctx->args[i].ptr;
922 pages[i].addr = ctx->maps[i]->phys;
924 mmap_read_lock(current->mm);
925 vma = find_vma(current->mm, ctx->args[i].ptr);
927 pages[i].addr += ctx->args[i].ptr -
929 mmap_read_unlock(current->mm);
931 pg_start = (ctx->args[i].ptr & PAGE_MASK) >> PAGE_SHIFT;
932 pg_end = ((ctx->args[i].ptr + len - 1) & PAGE_MASK) >>
934 pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
938 if (ctx->olaps[oix].offset == 0) {
939 rlen -= ALIGN(args, FASTRPC_ALIGN) - args;
940 args = ALIGN(args, FASTRPC_ALIGN);
943 mlen = ctx->olaps[oix].mend - ctx->olaps[oix].mstart;
948 rpra[i].buf.pv = args - ctx->olaps[oix].offset;
949 pages[i].addr = ctx->buf->phys -
950 ctx->olaps[oix].offset +
952 pages[i].addr = pages[i].addr & PAGE_MASK;
954 pg_start = (args & PAGE_MASK) >> PAGE_SHIFT;
955 pg_end = ((args + len - 1) & PAGE_MASK) >> PAGE_SHIFT;
956 pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
961 if (i < inbufs && !ctx->maps[i]) {
962 void *dst = (void *)(uintptr_t)rpra[i].buf.pv;
963 void *src = (void *)(uintptr_t)ctx->args[i].ptr;
966 if (copy_from_user(dst, (void __user *)src,
972 memcpy(dst, src, len);
977 for (i = ctx->nbufs; i < ctx->nscalars; ++i) {
978 list[i].num = ctx->args[i].length ? 1 : 0;
981 pages[i].addr = ctx->maps[i]->phys;
982 pages[i].size = ctx->maps[i]->size;
984 rpra[i].dma.fd = ctx->args[i].fd;
985 rpra[i].dma.len = ctx->args[i].length;
986 rpra[i].dma.offset = (u64) ctx->args[i].ptr;
991 dev_err(dev, "Error: get invoke args failed:%d\n", err);
996 static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
999 union fastrpc_remote_arg *rpra = ctx->rpra;
1000 struct fastrpc_user *fl = ctx->fl;
1001 struct fastrpc_map *mmap = NULL;
1002 struct fastrpc_invoke_buf *list;
1003 struct fastrpc_phy_page *pages;
1005 int i, inbufs, outbufs, handles;
1007 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
1008 outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc);
1009 handles = REMOTE_SCALARS_INHANDLES(ctx->sc) + REMOTE_SCALARS_OUTHANDLES(ctx->sc);
1010 list = fastrpc_invoke_buf_start(rpra, ctx->nscalars);
1011 pages = fastrpc_phy_page_start(list, ctx->nscalars);
1012 fdlist = (uint64_t *)(pages + inbufs + outbufs + handles);
1014 for (i = inbufs; i < ctx->nbufs; ++i) {
1015 if (!ctx->maps[i]) {
1016 void *src = (void *)(uintptr_t)rpra[i].buf.pv;
1017 void *dst = (void *)(uintptr_t)ctx->args[i].ptr;
1018 u64 len = rpra[i].buf.len;
1021 if (copy_to_user((void __user *)dst, src, len))
1024 memcpy(dst, src, len);
1029 for (i = 0; i < FASTRPC_MAX_FDLIST; i++) {
1032 if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap))
1033 fastrpc_map_put(mmap);
1039 static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx,
1040 struct fastrpc_invoke_ctx *ctx,
1041 u32 kernel, uint32_t handle)
1043 struct fastrpc_channel_ctx *cctx;
1044 struct fastrpc_user *fl = ctx->fl;
1045 struct fastrpc_msg *msg = &ctx->msg;
1049 msg->pid = fl->tgid;
1050 msg->tid = current->pid;
1055 msg->ctx = ctx->ctxid | fl->pd;
1056 msg->handle = handle;
1058 msg->addr = ctx->buf ? ctx->buf->phys : 0;
1059 msg->size = roundup(ctx->msg_sz, PAGE_SIZE);
1060 fastrpc_context_get(ctx);
1062 ret = rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg));
1065 fastrpc_context_put(ctx);
1071 static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
1073 struct fastrpc_invoke_args *args)
1075 struct fastrpc_invoke_ctx *ctx = NULL;
1081 if (!fl->cctx->rpdev)
1084 if (handle == FASTRPC_INIT_HANDLE && !kernel) {
1085 dev_warn_ratelimited(fl->sctx->dev, "user app trying to send a kernel RPC message (%d)\n", handle);
1089 ctx = fastrpc_context_alloc(fl, kernel, sc, args);
1091 return PTR_ERR(ctx);
1093 if (ctx->nscalars) {
1094 err = fastrpc_get_args(kernel, ctx);
1099 /* make sure that all CPU memory writes are seen by DSP */
1101 /* Send invoke buffer to remote dsp */
1102 err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle);
1107 if (!wait_for_completion_timeout(&ctx->work, 10 * HZ))
1110 err = wait_for_completion_interruptible(&ctx->work);
1116 /* Check the response from remote dsp */
1121 if (ctx->nscalars) {
1122 /* make sure that all memory writes by DSP are seen by CPU */
1124 /* populate all the output buffers with results */
1125 err = fastrpc_put_args(ctx, kernel);
1131 if (err != -ERESTARTSYS && err != -ETIMEDOUT) {
1132 /* We are done with this compute context */
1133 spin_lock(&fl->lock);
1134 list_del(&ctx->node);
1135 spin_unlock(&fl->lock);
1136 fastrpc_context_put(ctx);
1139 dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err);
1144 static bool is_session_rejected(struct fastrpc_user *fl, bool unsigned_pd_request)
1146 /* Check if the device node is non-secure and channel is secure*/
1147 if (!fl->is_secure_dev && fl->cctx->secure) {
1149 * Allow untrusted applications to offload only to Unsigned PD when
1150 * channel is configured as secure and block untrusted apps on channel
1151 * that does not support unsigned PD offload
1153 if (!fl->cctx->unsigned_support || !unsigned_pd_request) {
1154 dev_err(&fl->cctx->rpdev->dev, "Error: Untrusted application trying to offload to signed PD");
1162 static int fastrpc_init_create_process(struct fastrpc_user *fl,
1165 struct fastrpc_init_create init;
1166 struct fastrpc_invoke_args *args;
1167 struct fastrpc_phy_page pages[1];
1168 struct fastrpc_map *map = NULL;
1169 struct fastrpc_buf *imem = NULL;
1181 bool unsigned_module = false;
1183 args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL);
1187 if (copy_from_user(&init, argp, sizeof(init))) {
1192 if (init.attrs & FASTRPC_MODE_UNSIGNED_MODULE)
1193 unsigned_module = true;
1195 if (is_session_rejected(fl, unsigned_module)) {
1196 err = -ECONNREFUSED;
1200 if (init.filelen > INIT_FILELEN_MAX) {
1205 inbuf.pgid = fl->tgid;
1206 inbuf.namelen = strlen(current->comm) + 1;
1207 inbuf.filelen = init.filelen;
1209 inbuf.attrs = init.attrs;
1210 inbuf.siglen = init.siglen;
1213 if (init.filelen && init.filefd) {
1214 err = fastrpc_map_create(fl, init.filefd, init.filelen, 0, &map);
1219 memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4),
1221 err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen,
1226 fl->init_mem = imem;
1227 args[0].ptr = (u64)(uintptr_t)&inbuf;
1228 args[0].length = sizeof(inbuf);
1231 args[1].ptr = (u64)(uintptr_t)current->comm;
1232 args[1].length = inbuf.namelen;
1235 args[2].ptr = (u64) init.file;
1236 args[2].length = inbuf.filelen;
1237 args[2].fd = init.filefd;
1239 pages[0].addr = imem->phys;
1240 pages[0].size = imem->size;
1242 args[3].ptr = (u64)(uintptr_t) pages;
1243 args[3].length = 1 * sizeof(*pages);
1246 args[4].ptr = (u64)(uintptr_t)&inbuf.attrs;
1247 args[4].length = sizeof(inbuf.attrs);
1250 args[5].ptr = (u64)(uintptr_t) &inbuf.siglen;
1251 args[5].length = sizeof(inbuf.siglen);
1254 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0);
1256 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0);
1258 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1268 fl->init_mem = NULL;
1269 fastrpc_buf_free(imem);
1272 spin_lock(&fl->lock);
1273 list_del(&map->node);
1274 spin_unlock(&fl->lock);
1275 fastrpc_map_put(map);
1283 static struct fastrpc_session_ctx *fastrpc_session_alloc(
1284 struct fastrpc_channel_ctx *cctx)
1286 struct fastrpc_session_ctx *session = NULL;
1287 unsigned long flags;
1290 spin_lock_irqsave(&cctx->lock, flags);
1291 for (i = 0; i < cctx->sesscount; i++) {
1292 if (!cctx->session[i].used && cctx->session[i].valid) {
1293 cctx->session[i].used = true;
1294 session = &cctx->session[i];
1298 spin_unlock_irqrestore(&cctx->lock, flags);
1303 static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx,
1304 struct fastrpc_session_ctx *session)
1306 unsigned long flags;
1308 spin_lock_irqsave(&cctx->lock, flags);
1309 session->used = false;
1310 spin_unlock_irqrestore(&cctx->lock, flags);
1313 static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl)
1315 struct fastrpc_invoke_args args[1];
1320 args[0].ptr = (u64)(uintptr_t) &tgid;
1321 args[0].length = sizeof(tgid);
1323 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0);
1325 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1329 static int fastrpc_device_release(struct inode *inode, struct file *file)
1331 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1332 struct fastrpc_channel_ctx *cctx = fl->cctx;
1333 struct fastrpc_invoke_ctx *ctx, *n;
1334 struct fastrpc_map *map, *m;
1335 struct fastrpc_buf *buf, *b;
1336 unsigned long flags;
1338 fastrpc_release_current_dsp_process(fl);
1340 spin_lock_irqsave(&cctx->lock, flags);
1341 list_del(&fl->user);
1342 spin_unlock_irqrestore(&cctx->lock, flags);
1345 fastrpc_buf_free(fl->init_mem);
1347 list_for_each_entry_safe(ctx, n, &fl->pending, node) {
1348 list_del(&ctx->node);
1349 fastrpc_context_put(ctx);
1352 list_for_each_entry_safe(map, m, &fl->maps, node) {
1353 list_del(&map->node);
1354 fastrpc_map_put(map);
1357 list_for_each_entry_safe(buf, b, &fl->mmaps, node) {
1358 list_del(&buf->node);
1359 fastrpc_buf_free(buf);
1362 fastrpc_session_free(cctx, fl->sctx);
1363 fastrpc_channel_ctx_put(cctx);
1365 mutex_destroy(&fl->mutex);
1367 file->private_data = NULL;
1372 static int fastrpc_device_open(struct inode *inode, struct file *filp)
1374 struct fastrpc_channel_ctx *cctx;
1375 struct fastrpc_device *fdevice;
1376 struct fastrpc_user *fl = NULL;
1377 unsigned long flags;
1379 fdevice = miscdev_to_fdevice(filp->private_data);
1380 cctx = fdevice->cctx;
1382 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
1386 /* Released in fastrpc_device_release() */
1387 fastrpc_channel_ctx_get(cctx);
1389 filp->private_data = fl;
1390 spin_lock_init(&fl->lock);
1391 mutex_init(&fl->mutex);
1392 INIT_LIST_HEAD(&fl->pending);
1393 INIT_LIST_HEAD(&fl->maps);
1394 INIT_LIST_HEAD(&fl->mmaps);
1395 INIT_LIST_HEAD(&fl->user);
1396 fl->tgid = current->tgid;
1398 fl->is_secure_dev = fdevice->secure;
1400 fl->sctx = fastrpc_session_alloc(cctx);
1402 dev_err(&cctx->rpdev->dev, "No session available\n");
1403 mutex_destroy(&fl->mutex);
1409 spin_lock_irqsave(&cctx->lock, flags);
1410 list_add_tail(&fl->user, &cctx->users);
1411 spin_unlock_irqrestore(&cctx->lock, flags);
1416 static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
1418 struct fastrpc_alloc_dma_buf bp;
1419 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
1420 struct fastrpc_buf *buf = NULL;
1423 if (copy_from_user(&bp, argp, sizeof(bp)))
1426 err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf);
1429 exp_info.ops = &fastrpc_dma_buf_ops;
1430 exp_info.size = bp.size;
1431 exp_info.flags = O_RDWR;
1432 exp_info.priv = buf;
1433 buf->dmabuf = dma_buf_export(&exp_info);
1434 if (IS_ERR(buf->dmabuf)) {
1435 err = PTR_ERR(buf->dmabuf);
1436 fastrpc_buf_free(buf);
1440 bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE);
1442 dma_buf_put(buf->dmabuf);
1446 if (copy_to_user(argp, &bp, sizeof(bp))) {
1448 * The usercopy failed, but we can't do much about it, as
1449 * dma_buf_fd() already called fd_install() and made the
1450 * file descriptor accessible for the current process. It
1451 * might already be closed and dmabuf no longer valid when
1452 * we reach this point. Therefore "leak" the fd and rely on
1453 * the process exit path to do any required cleanup.
1461 static int fastrpc_init_attach(struct fastrpc_user *fl, int pd)
1463 struct fastrpc_invoke_args args[1];
1464 int tgid = fl->tgid;
1467 args[0].ptr = (u64)(uintptr_t) &tgid;
1468 args[0].length = sizeof(tgid);
1470 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0);
1473 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1477 static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp)
1479 struct fastrpc_invoke_args *args = NULL;
1480 struct fastrpc_invoke inv;
1484 if (copy_from_user(&inv, argp, sizeof(inv)))
1487 /* nscalars is truncated here to max supported value */
1488 nscalars = REMOTE_SCALARS_LENGTH(inv.sc);
1490 args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL);
1494 if (copy_from_user(args, (void __user *)(uintptr_t)inv.args,
1495 nscalars * sizeof(*args))) {
1501 err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args);
1507 static int fastrpc_get_info_from_dsp(struct fastrpc_user *fl, uint32_t *dsp_attr_buf,
1508 uint32_t dsp_attr_buf_len)
1510 struct fastrpc_invoke_args args[2] = { 0 };
1512 /* Capability filled in userspace */
1513 dsp_attr_buf[0] = 0;
1515 args[0].ptr = (u64)(uintptr_t)&dsp_attr_buf_len;
1516 args[0].length = sizeof(dsp_attr_buf_len);
1518 args[1].ptr = (u64)(uintptr_t)&dsp_attr_buf[1];
1519 args[1].length = dsp_attr_buf_len;
1523 return fastrpc_internal_invoke(fl, true, FASTRPC_DSP_UTILITIES_HANDLE,
1524 FASTRPC_SCALARS(0, 1, 1), args);
1527 static int fastrpc_get_info_from_kernel(struct fastrpc_ioctl_capability *cap,
1528 struct fastrpc_user *fl)
1530 struct fastrpc_channel_ctx *cctx = fl->cctx;
1531 uint32_t attribute_id = cap->attribute_id;
1532 uint32_t *dsp_attributes;
1533 unsigned long flags;
1534 uint32_t domain = cap->domain;
1537 spin_lock_irqsave(&cctx->lock, flags);
1538 /* check if we already have queried dsp for attributes */
1539 if (cctx->valid_attributes) {
1540 spin_unlock_irqrestore(&cctx->lock, flags);
1543 spin_unlock_irqrestore(&cctx->lock, flags);
1545 dsp_attributes = kzalloc(FASTRPC_MAX_DSP_ATTRIBUTES_LEN, GFP_KERNEL);
1546 if (!dsp_attributes)
1549 err = fastrpc_get_info_from_dsp(fl, dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES_LEN);
1550 if (err == DSP_UNSUPPORTED_API) {
1551 dev_info(&cctx->rpdev->dev,
1552 "Warning: DSP capabilities not supported on domain: %d\n", domain);
1553 kfree(dsp_attributes);
1556 dev_err(&cctx->rpdev->dev, "Error: dsp information is incorrect err: %d\n", err);
1557 kfree(dsp_attributes);
1561 spin_lock_irqsave(&cctx->lock, flags);
1562 memcpy(cctx->dsp_attributes, dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES_LEN);
1563 cctx->valid_attributes = true;
1564 spin_unlock_irqrestore(&cctx->lock, flags);
1565 kfree(dsp_attributes);
1567 cap->capability = cctx->dsp_attributes[attribute_id];
1571 static int fastrpc_get_dsp_info(struct fastrpc_user *fl, char __user *argp)
1573 struct fastrpc_ioctl_capability cap = {0};
1576 if (copy_from_user(&cap, argp, sizeof(cap)))
1580 if (cap.domain >= FASTRPC_DEV_MAX) {
1581 dev_err(&fl->cctx->rpdev->dev, "Error: Invalid domain id:%d, err:%d\n",
1586 /* Fastrpc Capablities does not support modem domain */
1587 if (cap.domain == MDSP_DOMAIN_ID) {
1588 dev_err(&fl->cctx->rpdev->dev, "Error: modem not supported %d\n", err);
1592 if (cap.attribute_id >= FASTRPC_MAX_DSP_ATTRIBUTES) {
1593 dev_err(&fl->cctx->rpdev->dev, "Error: invalid attribute: %d, err: %d\n",
1594 cap.attribute_id, err);
1598 err = fastrpc_get_info_from_kernel(&cap, fl);
1602 if (copy_to_user(argp, &cap.capability, sizeof(cap.capability)))
1608 static int fastrpc_req_munmap_impl(struct fastrpc_user *fl,
1609 struct fastrpc_req_munmap *req)
1611 struct fastrpc_invoke_args args[1] = { [0] = { 0 } };
1612 struct fastrpc_buf *buf = NULL, *iter, *b;
1613 struct fastrpc_munmap_req_msg req_msg;
1614 struct device *dev = fl->sctx->dev;
1618 spin_lock(&fl->lock);
1619 list_for_each_entry_safe(iter, b, &fl->mmaps, node) {
1620 if ((iter->raddr == req->vaddrout) && (iter->size == req->size)) {
1625 spin_unlock(&fl->lock);
1628 dev_err(dev, "mmap not in list\n");
1632 req_msg.pgid = fl->tgid;
1633 req_msg.size = buf->size;
1634 req_msg.vaddr = buf->raddr;
1636 args[0].ptr = (u64) (uintptr_t) &req_msg;
1637 args[0].length = sizeof(req_msg);
1639 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MUNMAP, 1, 0);
1640 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc,
1643 dev_dbg(dev, "unmmap\tpt 0x%09lx OK\n", buf->raddr);
1644 spin_lock(&fl->lock);
1645 list_del(&buf->node);
1646 spin_unlock(&fl->lock);
1647 fastrpc_buf_free(buf);
1649 dev_err(dev, "unmmap\tpt 0x%09lx ERROR\n", buf->raddr);
1655 static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)
1657 struct fastrpc_req_munmap req;
1659 if (copy_from_user(&req, argp, sizeof(req)))
1662 return fastrpc_req_munmap_impl(fl, &req);
1665 static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp)
1667 struct fastrpc_invoke_args args[3] = { [0 ... 2] = { 0 } };
1668 struct fastrpc_buf *buf = NULL;
1669 struct fastrpc_mmap_req_msg req_msg;
1670 struct fastrpc_mmap_rsp_msg rsp_msg;
1671 struct fastrpc_req_munmap req_unmap;
1672 struct fastrpc_phy_page pages;
1673 struct fastrpc_req_mmap req;
1674 struct device *dev = fl->sctx->dev;
1678 if (copy_from_user(&req, argp, sizeof(req)))
1681 if (req.flags != ADSP_MMAP_ADD_PAGES) {
1682 dev_err(dev, "flag not supported 0x%x\n", req.flags);
1687 dev_err(dev, "adding user allocated pages is not supported\n");
1691 err = fastrpc_buf_alloc(fl, fl->sctx->dev, req.size, &buf);
1693 dev_err(dev, "failed to allocate buffer\n");
1697 req_msg.pgid = fl->tgid;
1698 req_msg.flags = req.flags;
1699 req_msg.vaddr = req.vaddrin;
1700 req_msg.num = sizeof(pages);
1702 args[0].ptr = (u64) (uintptr_t) &req_msg;
1703 args[0].length = sizeof(req_msg);
1705 pages.addr = buf->phys;
1706 pages.size = buf->size;
1708 args[1].ptr = (u64) (uintptr_t) &pages;
1709 args[1].length = sizeof(pages);
1711 args[2].ptr = (u64) (uintptr_t) &rsp_msg;
1712 args[2].length = sizeof(rsp_msg);
1714 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MMAP, 2, 1);
1715 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc,
1718 dev_err(dev, "mmap error (len 0x%08llx)\n", buf->size);
1722 /* update the buffer to be able to deallocate the memory on the DSP */
1723 buf->raddr = (uintptr_t) rsp_msg.vaddr;
1725 /* let the client know the address to use */
1726 req.vaddrout = rsp_msg.vaddr;
1728 spin_lock(&fl->lock);
1729 list_add_tail(&buf->node, &fl->mmaps);
1730 spin_unlock(&fl->lock);
1732 if (copy_to_user((void __user *)argp, &req, sizeof(req))) {
1733 /* unmap the memory and release the buffer */
1734 req_unmap.vaddrout = buf->raddr;
1735 req_unmap.size = buf->size;
1736 fastrpc_req_munmap_impl(fl, &req_unmap);
1740 dev_dbg(dev, "mmap\t\tpt 0x%09lx OK [len 0x%08llx]\n",
1741 buf->raddr, buf->size);
1746 fastrpc_buf_free(buf);
1751 static int fastrpc_req_mem_unmap_impl(struct fastrpc_user *fl, struct fastrpc_mem_unmap *req)
1753 struct fastrpc_invoke_args args[1] = { [0] = { 0 } };
1754 struct fastrpc_map *map = NULL, *iter, *m;
1755 struct fastrpc_mem_unmap_req_msg req_msg = { 0 };
1758 struct device *dev = fl->sctx->dev;
1760 spin_lock(&fl->lock);
1761 list_for_each_entry_safe(iter, m, &fl->maps, node) {
1762 if ((req->fd < 0 || iter->fd == req->fd) && (iter->raddr == req->vaddr)) {
1768 spin_unlock(&fl->lock);
1771 dev_err(dev, "map not in list\n");
1775 req_msg.pgid = fl->tgid;
1776 req_msg.len = map->len;
1777 req_msg.vaddrin = map->raddr;
1778 req_msg.fd = map->fd;
1780 args[0].ptr = (u64) (uintptr_t) &req_msg;
1781 args[0].length = sizeof(req_msg);
1783 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MEM_UNMAP, 1, 0);
1784 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc,
1786 fastrpc_map_put(map);
1788 dev_err(dev, "unmmap\tpt fd = %d, 0x%09llx error\n", map->fd, map->raddr);
1793 static int fastrpc_req_mem_unmap(struct fastrpc_user *fl, char __user *argp)
1795 struct fastrpc_mem_unmap req;
1797 if (copy_from_user(&req, argp, sizeof(req)))
1800 return fastrpc_req_mem_unmap_impl(fl, &req);
1803 static int fastrpc_req_mem_map(struct fastrpc_user *fl, char __user *argp)
1805 struct fastrpc_invoke_args args[4] = { [0 ... 3] = { 0 } };
1806 struct fastrpc_mem_map_req_msg req_msg = { 0 };
1807 struct fastrpc_mmap_rsp_msg rsp_msg = { 0 };
1808 struct fastrpc_mem_unmap req_unmap = { 0 };
1809 struct fastrpc_phy_page pages = { 0 };
1810 struct fastrpc_mem_map req;
1811 struct device *dev = fl->sctx->dev;
1812 struct fastrpc_map *map = NULL;
1816 if (copy_from_user(&req, argp, sizeof(req)))
1819 /* create SMMU mapping */
1820 err = fastrpc_map_create(fl, req.fd, req.length, 0, &map);
1822 dev_err(dev, "failed to map buffer, fd = %d\n", req.fd);
1826 req_msg.pgid = fl->tgid;
1827 req_msg.fd = req.fd;
1828 req_msg.offset = req.offset;
1829 req_msg.vaddrin = req.vaddrin;
1830 map->va = (void *) (uintptr_t) req.vaddrin;
1831 req_msg.flags = req.flags;
1832 req_msg.num = sizeof(pages);
1833 req_msg.data_len = 0;
1835 args[0].ptr = (u64) (uintptr_t) &req_msg;
1836 args[0].length = sizeof(req_msg);
1838 pages.addr = map->phys;
1839 pages.size = map->size;
1841 args[1].ptr = (u64) (uintptr_t) &pages;
1842 args[1].length = sizeof(pages);
1844 args[2].ptr = (u64) (uintptr_t) &pages;
1847 args[3].ptr = (u64) (uintptr_t) &rsp_msg;
1848 args[3].length = sizeof(rsp_msg);
1850 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MEM_MAP, 3, 1);
1851 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, &args[0]);
1853 dev_err(dev, "mem mmap error, fd %d, vaddr %llx, size %lld\n",
1854 req.fd, req.vaddrin, map->size);
1858 /* update the buffer to be able to deallocate the memory on the DSP */
1859 map->raddr = rsp_msg.vaddr;
1861 /* let the client know the address to use */
1862 req.vaddrout = rsp_msg.vaddr;
1864 if (copy_to_user((void __user *)argp, &req, sizeof(req))) {
1865 /* unmap the memory and release the buffer */
1866 req_unmap.vaddr = (uintptr_t) rsp_msg.vaddr;
1867 req_unmap.length = map->size;
1868 fastrpc_req_mem_unmap_impl(fl, &req_unmap);
1875 fastrpc_map_put(map);
1880 static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
1883 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1884 char __user *argp = (char __user *)arg;
1888 case FASTRPC_IOCTL_INVOKE:
1889 err = fastrpc_invoke(fl, argp);
1891 case FASTRPC_IOCTL_INIT_ATTACH:
1892 err = fastrpc_init_attach(fl, AUDIO_PD);
1894 case FASTRPC_IOCTL_INIT_ATTACH_SNS:
1895 err = fastrpc_init_attach(fl, SENSORS_PD);
1897 case FASTRPC_IOCTL_INIT_CREATE:
1898 err = fastrpc_init_create_process(fl, argp);
1900 case FASTRPC_IOCTL_ALLOC_DMA_BUFF:
1901 err = fastrpc_dmabuf_alloc(fl, argp);
1903 case FASTRPC_IOCTL_MMAP:
1904 err = fastrpc_req_mmap(fl, argp);
1906 case FASTRPC_IOCTL_MUNMAP:
1907 err = fastrpc_req_munmap(fl, argp);
1909 case FASTRPC_IOCTL_MEM_MAP:
1910 err = fastrpc_req_mem_map(fl, argp);
1912 case FASTRPC_IOCTL_MEM_UNMAP:
1913 err = fastrpc_req_mem_unmap(fl, argp);
1915 case FASTRPC_IOCTL_GET_DSP_INFO:
1916 err = fastrpc_get_dsp_info(fl, argp);
1926 static const struct file_operations fastrpc_fops = {
1927 .open = fastrpc_device_open,
1928 .release = fastrpc_device_release,
1929 .unlocked_ioctl = fastrpc_device_ioctl,
1930 .compat_ioctl = fastrpc_device_ioctl,
1933 static int fastrpc_cb_probe(struct platform_device *pdev)
1935 struct fastrpc_channel_ctx *cctx;
1936 struct fastrpc_session_ctx *sess;
1937 struct device *dev = &pdev->dev;
1938 int i, sessions = 0;
1939 unsigned long flags;
1942 cctx = dev_get_drvdata(dev->parent);
1946 of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions);
1948 spin_lock_irqsave(&cctx->lock, flags);
1949 if (cctx->sesscount >= FASTRPC_MAX_SESSIONS) {
1950 dev_err(&pdev->dev, "too many sessions\n");
1951 spin_unlock_irqrestore(&cctx->lock, flags);
1954 sess = &cctx->session[cctx->sesscount++];
1958 dev_set_drvdata(dev, sess);
1960 if (of_property_read_u32(dev->of_node, "reg", &sess->sid))
1961 dev_info(dev, "FastRPC Session ID not specified in DT\n");
1964 struct fastrpc_session_ctx *dup_sess;
1966 for (i = 1; i < sessions; i++) {
1967 if (cctx->sesscount >= FASTRPC_MAX_SESSIONS)
1969 dup_sess = &cctx->session[cctx->sesscount++];
1970 memcpy(dup_sess, sess, sizeof(*dup_sess));
1973 spin_unlock_irqrestore(&cctx->lock, flags);
1974 rc = dma_set_mask(dev, DMA_BIT_MASK(32));
1976 dev_err(dev, "32-bit DMA enable failed\n");
1983 static int fastrpc_cb_remove(struct platform_device *pdev)
1985 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent);
1986 struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev);
1987 unsigned long flags;
1990 spin_lock_irqsave(&cctx->lock, flags);
1991 for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) {
1992 if (cctx->session[i].sid == sess->sid) {
1993 cctx->session[i].valid = false;
1997 spin_unlock_irqrestore(&cctx->lock, flags);
2002 static const struct of_device_id fastrpc_match_table[] = {
2003 { .compatible = "qcom,fastrpc-compute-cb", },
2007 static struct platform_driver fastrpc_cb_driver = {
2008 .probe = fastrpc_cb_probe,
2009 .remove = fastrpc_cb_remove,
2011 .name = "qcom,fastrpc-cb",
2012 .of_match_table = fastrpc_match_table,
2013 .suppress_bind_attrs = true,
2017 static int fastrpc_device_register(struct device *dev, struct fastrpc_channel_ctx *cctx,
2018 bool is_secured, const char *domain)
2020 struct fastrpc_device *fdev;
2023 fdev = devm_kzalloc(dev, sizeof(*fdev), GFP_KERNEL);
2027 fdev->secure = is_secured;
2029 fdev->miscdev.minor = MISC_DYNAMIC_MINOR;
2030 fdev->miscdev.fops = &fastrpc_fops;
2031 fdev->miscdev.name = devm_kasprintf(dev, GFP_KERNEL, "fastrpc-%s%s",
2032 domain, is_secured ? "-secure" : "");
2033 err = misc_register(&fdev->miscdev);
2036 cctx->secure_fdevice = fdev;
2038 cctx->fdevice = fdev;
2044 static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
2046 struct device *rdev = &rpdev->dev;
2047 struct fastrpc_channel_ctx *data;
2048 int i, err, domain_id = -1, vmcount;
2051 unsigned int vmids[FASTRPC_MAX_VMIDS];
2053 err = of_property_read_string(rdev->of_node, "label", &domain);
2055 dev_info(rdev, "FastRPC Domain not specified in DT\n");
2059 for (i = 0; i <= CDSP_DOMAIN_ID; i++) {
2060 if (!strcmp(domains[i], domain)) {
2066 if (domain_id < 0) {
2067 dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id);
2071 vmcount = of_property_read_variable_u32_array(rdev->of_node,
2072 "qcom,vmids", &vmids[0], 0, FASTRPC_MAX_VMIDS);
2075 else if (!qcom_scm_is_available())
2076 return -EPROBE_DEFER;
2078 data = kzalloc(sizeof(*data), GFP_KERNEL);
2083 data->vmcount = vmcount;
2084 data->perms = BIT(QCOM_SCM_VMID_HLOS);
2085 for (i = 0; i < data->vmcount; i++) {
2086 data->vmperms[i].vmid = vmids[i];
2087 data->vmperms[i].perm = QCOM_SCM_PERM_RWX;
2091 secure_dsp = !(of_property_read_bool(rdev->of_node, "qcom,non-secure-domain"));
2092 data->secure = secure_dsp;
2094 switch (domain_id) {
2095 case ADSP_DOMAIN_ID:
2096 case MDSP_DOMAIN_ID:
2097 case SDSP_DOMAIN_ID:
2098 /* Unsigned PD offloading is only supported on CDSP*/
2099 data->unsigned_support = false;
2100 err = fastrpc_device_register(rdev, data, secure_dsp, domains[domain_id]);
2104 case CDSP_DOMAIN_ID:
2105 data->unsigned_support = true;
2106 /* Create both device nodes so that we can allow both Signed and Unsigned PD */
2107 err = fastrpc_device_register(rdev, data, true, domains[domain_id]);
2111 err = fastrpc_device_register(rdev, data, false, domains[domain_id]);
2120 kref_init(&data->refcount);
2122 dev_set_drvdata(&rpdev->dev, data);
2123 dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
2124 INIT_LIST_HEAD(&data->users);
2125 spin_lock_init(&data->lock);
2126 idr_init(&data->ctx_idr);
2127 data->domain_id = domain_id;
2128 data->rpdev = rpdev;
2130 return of_platform_populate(rdev->of_node, NULL, NULL, rdev);
2136 static void fastrpc_notify_users(struct fastrpc_user *user)
2138 struct fastrpc_invoke_ctx *ctx;
2140 spin_lock(&user->lock);
2141 list_for_each_entry(ctx, &user->pending, node)
2142 complete(&ctx->work);
2143 spin_unlock(&user->lock);
2146 static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
2148 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
2149 struct fastrpc_user *user;
2150 unsigned long flags;
2152 spin_lock_irqsave(&cctx->lock, flags);
2153 list_for_each_entry(user, &cctx->users, user)
2154 fastrpc_notify_users(user);
2155 spin_unlock_irqrestore(&cctx->lock, flags);
2158 misc_deregister(&cctx->fdevice->miscdev);
2160 if (cctx->secure_fdevice)
2161 misc_deregister(&cctx->secure_fdevice->miscdev);
2163 of_platform_depopulate(&rpdev->dev);
2166 fastrpc_channel_ctx_put(cctx);
2169 static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
2170 int len, void *priv, u32 addr)
2172 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
2173 struct fastrpc_invoke_rsp *rsp = data;
2174 struct fastrpc_invoke_ctx *ctx;
2175 unsigned long flags;
2176 unsigned long ctxid;
2178 if (len < sizeof(*rsp))
2181 ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4);
2183 spin_lock_irqsave(&cctx->lock, flags);
2184 ctx = idr_find(&cctx->ctx_idr, ctxid);
2185 spin_unlock_irqrestore(&cctx->lock, flags);
2188 dev_err(&rpdev->dev, "No context ID matches response\n");
2192 ctx->retval = rsp->retval;
2193 complete(&ctx->work);
2196 * The DMA buffer associated with the context cannot be freed in
2197 * interrupt context so schedule it through a worker thread to
2198 * avoid a kernel BUG.
2200 schedule_work(&ctx->put_work);
2205 static const struct of_device_id fastrpc_rpmsg_of_match[] = {
2206 { .compatible = "qcom,fastrpc" },
2209 MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match);
2211 static struct rpmsg_driver fastrpc_driver = {
2212 .probe = fastrpc_rpmsg_probe,
2213 .remove = fastrpc_rpmsg_remove,
2214 .callback = fastrpc_rpmsg_callback,
2216 .name = "qcom,fastrpc",
2217 .of_match_table = fastrpc_rpmsg_of_match,
2221 static int fastrpc_init(void)
2225 ret = platform_driver_register(&fastrpc_cb_driver);
2227 pr_err("fastrpc: failed to register cb driver\n");
2231 ret = register_rpmsg_driver(&fastrpc_driver);
2233 pr_err("fastrpc: failed to register rpmsg driver\n");
2234 platform_driver_unregister(&fastrpc_cb_driver);
2240 module_init(fastrpc_init);
2242 static void fastrpc_exit(void)
2244 platform_driver_unregister(&fastrpc_cb_driver);
2245 unregister_rpmsg_driver(&fastrpc_driver);
2247 module_exit(fastrpc_exit);
2249 MODULE_LICENSE("GPL v2");
2250 MODULE_IMPORT_NS(DMA_BUF);