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>
10 #include <linux/list.h>
11 #include <linux/miscdevice.h>
12 #include <linux/module.h>
13 #include <linux/of_address.h>
15 #include <linux/sort.h>
16 #include <linux/of_platform.h>
17 #include <linux/rpmsg.h>
18 #include <linux/scatterlist.h>
19 #include <linux/slab.h>
20 #include <linux/qcom_scm.h>
21 #include <uapi/misc/fastrpc.h>
23 #define ADSP_DOMAIN_ID (0)
24 #define MDSP_DOMAIN_ID (1)
25 #define SDSP_DOMAIN_ID (2)
26 #define CDSP_DOMAIN_ID (3)
27 #define FASTRPC_DEV_MAX 4 /* adsp, mdsp, slpi, cdsp*/
28 #define FASTRPC_MAX_SESSIONS 14
29 #define FASTRPC_MAX_VMIDS 16
30 #define FASTRPC_ALIGN 128
31 #define FASTRPC_MAX_FDLIST 16
32 #define FASTRPC_MAX_CRCLIST 64
33 #define FASTRPC_PHYS(p) ((p) & 0xffffffff)
34 #define FASTRPC_CTX_MAX (256)
35 #define FASTRPC_INIT_HANDLE 1
36 #define FASTRPC_DSP_UTILITIES_HANDLE 2
37 #define FASTRPC_CTXID_MASK (0xFF0)
38 #define INIT_FILELEN_MAX (2 * 1024 * 1024)
39 #define FASTRPC_DEVICE_NAME "fastrpc"
40 #define ADSP_MMAP_ADD_PAGES 0x1000
41 #define DSP_UNSUPPORTED_API (0x80000414)
42 /* MAX NUMBER of DSP ATTRIBUTES SUPPORTED */
43 #define FASTRPC_MAX_DSP_ATTRIBUTES (256)
44 #define FASTRPC_MAX_DSP_ATTRIBUTES_LEN (sizeof(u32) * FASTRPC_MAX_DSP_ATTRIBUTES)
46 /* Retrives number of input buffers from the scalars parameter */
47 #define REMOTE_SCALARS_INBUFS(sc) (((sc) >> 16) & 0x0ff)
49 /* Retrives number of output buffers from the scalars parameter */
50 #define REMOTE_SCALARS_OUTBUFS(sc) (((sc) >> 8) & 0x0ff)
52 /* Retrives number of input handles from the scalars parameter */
53 #define REMOTE_SCALARS_INHANDLES(sc) (((sc) >> 4) & 0x0f)
55 /* Retrives number of output handles from the scalars parameter */
56 #define REMOTE_SCALARS_OUTHANDLES(sc) ((sc) & 0x0f)
58 #define REMOTE_SCALARS_LENGTH(sc) (REMOTE_SCALARS_INBUFS(sc) + \
59 REMOTE_SCALARS_OUTBUFS(sc) + \
60 REMOTE_SCALARS_INHANDLES(sc)+ \
61 REMOTE_SCALARS_OUTHANDLES(sc))
62 #define FASTRPC_BUILD_SCALARS(attr, method, in, out, oin, oout) \
63 (((attr & 0x07) << 29) | \
64 ((method & 0x1f) << 24) | \
65 ((in & 0xff) << 16) | \
66 ((out & 0xff) << 8) | \
67 ((oin & 0x0f) << 4) | \
70 #define FASTRPC_SCALARS(method, in, out) \
71 FASTRPC_BUILD_SCALARS(0, method, in, out, 0, 0)
73 #define FASTRPC_CREATE_PROCESS_NARGS 6
74 /* Remote Method id table */
75 #define FASTRPC_RMID_INIT_ATTACH 0
76 #define FASTRPC_RMID_INIT_RELEASE 1
77 #define FASTRPC_RMID_INIT_MMAP 4
78 #define FASTRPC_RMID_INIT_MUNMAP 5
79 #define FASTRPC_RMID_INIT_CREATE 6
80 #define FASTRPC_RMID_INIT_CREATE_ATTR 7
81 #define FASTRPC_RMID_INIT_CREATE_STATIC 8
82 #define FASTRPC_RMID_INIT_MEM_MAP 10
83 #define FASTRPC_RMID_INIT_MEM_UNMAP 11
85 /* Protection Domain(PD) ids */
86 #define AUDIO_PD (0) /* also GUEST_OS PD? */
88 #define SENSORS_PD (2)
90 #define miscdev_to_fdevice(d) container_of(d, struct fastrpc_device, miscdev)
92 static const char *domains[FASTRPC_DEV_MAX] = { "adsp", "mdsp",
94 struct fastrpc_phy_page {
95 u64 addr; /* physical address */
96 u64 size; /* size of contiguous region */
99 struct fastrpc_invoke_buf {
100 u32 num; /* number of contiguous regions */
101 u32 pgidx; /* index to start of contiguous region */
104 struct fastrpc_remote_dmahandle {
105 s32 fd; /* dma handle fd */
106 u32 offset; /* dma handle offset */
107 u32 len; /* dma handle length */
110 struct fastrpc_remote_buf {
111 u64 pv; /* buffer pointer */
112 u64 len; /* length of buffer */
115 union fastrpc_remote_arg {
116 struct fastrpc_remote_buf buf;
117 struct fastrpc_remote_dmahandle dma;
120 struct fastrpc_mmap_rsp_msg {
124 struct fastrpc_mmap_req_msg {
131 struct fastrpc_mem_map_req_msg {
141 struct fastrpc_munmap_req_msg {
147 struct fastrpc_mem_unmap_req_msg {
155 int pid; /* process group id */
156 int tid; /* thread id */
157 u64 ctx; /* invoke caller context */
158 u32 handle; /* handle to invoke */
159 u32 sc; /* scalars structure describing the data */
160 u64 addr; /* physical address */
161 u64 size; /* size of contiguous region */
164 struct fastrpc_invoke_rsp {
165 u64 ctx; /* invoke caller context */
166 int retval; /* invoke return value */
169 struct fastrpc_buf_overlap {
179 struct fastrpc_user *fl;
180 struct dma_buf *dmabuf;
185 /* Lock for dma buf attachments */
187 struct list_head attachments;
189 struct list_head node; /* list of user requested mmaps */
193 struct fastrpc_dma_buf_attachment {
196 struct list_head node;
200 struct list_head node;
201 struct fastrpc_user *fl;
204 struct sg_table *table;
205 struct dma_buf_attachment *attach;
212 struct kref refcount;
215 struct fastrpc_invoke_ctx {
225 struct kref refcount;
226 struct list_head node; /* list of ctxs */
227 struct completion work;
228 struct work_struct put_work;
229 struct fastrpc_msg msg;
230 struct fastrpc_user *fl;
231 union fastrpc_remote_arg *rpra;
232 struct fastrpc_map **maps;
233 struct fastrpc_buf *buf;
234 struct fastrpc_invoke_args *args;
235 struct fastrpc_buf_overlap *olaps;
236 struct fastrpc_channel_ctx *cctx;
239 struct fastrpc_session_ctx {
246 struct fastrpc_channel_ctx {
251 struct qcom_scm_vmperm vmperms[FASTRPC_MAX_VMIDS];
252 struct rpmsg_device *rpdev;
253 struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS];
256 struct list_head users;
257 struct kref refcount;
258 /* Flag if dsp attributes are cached */
259 bool valid_attributes;
260 u32 dsp_attributes[FASTRPC_MAX_DSP_ATTRIBUTES];
261 struct fastrpc_device *secure_fdevice;
262 struct fastrpc_device *fdevice;
264 bool unsigned_support;
267 struct fastrpc_device {
268 struct fastrpc_channel_ctx *cctx;
269 struct miscdevice miscdev;
273 struct fastrpc_user {
274 struct list_head user;
275 struct list_head maps;
276 struct list_head pending;
277 struct list_head mmaps;
279 struct fastrpc_channel_ctx *cctx;
280 struct fastrpc_session_ctx *sctx;
281 struct fastrpc_buf *init_mem;
288 /* lock for allocations */
292 static void fastrpc_free_map(struct kref *ref)
294 struct fastrpc_map *map;
296 map = container_of(ref, struct fastrpc_map, refcount);
299 if (map->attr & FASTRPC_ATTR_SECUREMAP) {
300 struct qcom_scm_vmperm perm;
303 perm.vmid = QCOM_SCM_VMID_HLOS;
304 perm.perm = QCOM_SCM_PERM_RWX;
305 err = qcom_scm_assign_mem(map->phys, map->size,
306 &(map->fl->cctx->vmperms[0].vmid), &perm, 1);
308 dev_err(map->fl->sctx->dev, "Failed to assign memory phys 0x%llx size 0x%llx err %d",
309 map->phys, map->size, err);
313 dma_buf_unmap_attachment(map->attach, map->table,
315 dma_buf_detach(map->buf, map->attach);
316 dma_buf_put(map->buf);
320 spin_lock(&map->fl->lock);
321 list_del(&map->node);
322 spin_unlock(&map->fl->lock);
329 static void fastrpc_map_put(struct fastrpc_map *map)
332 kref_put(&map->refcount, fastrpc_free_map);
335 static int fastrpc_map_get(struct fastrpc_map *map)
340 return kref_get_unless_zero(&map->refcount) ? 0 : -ENOENT;
344 static int fastrpc_map_lookup(struct fastrpc_user *fl, int fd,
345 struct fastrpc_map **ppmap, bool take_ref)
347 struct fastrpc_session_ctx *sess = fl->sctx;
348 struct fastrpc_map *map = NULL;
351 spin_lock(&fl->lock);
352 list_for_each_entry(map, &fl->maps, node) {
357 ret = fastrpc_map_get(map);
359 dev_dbg(sess->dev, "%s: Failed to get map fd=%d ret=%d\n",
369 spin_unlock(&fl->lock);
374 static void fastrpc_buf_free(struct fastrpc_buf *buf)
376 dma_free_coherent(buf->dev, buf->size, buf->virt,
377 FASTRPC_PHYS(buf->phys));
381 static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
382 u64 size, struct fastrpc_buf **obuf)
384 struct fastrpc_buf *buf;
386 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
390 INIT_LIST_HEAD(&buf->attachments);
391 INIT_LIST_HEAD(&buf->node);
392 mutex_init(&buf->lock);
401 buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys,
404 mutex_destroy(&buf->lock);
409 if (fl->sctx && fl->sctx->sid)
410 buf->phys += ((u64)fl->sctx->sid << 32);
417 static void fastrpc_channel_ctx_free(struct kref *ref)
419 struct fastrpc_channel_ctx *cctx;
421 cctx = container_of(ref, struct fastrpc_channel_ctx, refcount);
426 static void fastrpc_channel_ctx_get(struct fastrpc_channel_ctx *cctx)
428 kref_get(&cctx->refcount);
431 static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx)
433 kref_put(&cctx->refcount, fastrpc_channel_ctx_free);
436 static void fastrpc_context_free(struct kref *ref)
438 struct fastrpc_invoke_ctx *ctx;
439 struct fastrpc_channel_ctx *cctx;
443 ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
446 for (i = 0; i < ctx->nbufs; i++)
447 fastrpc_map_put(ctx->maps[i]);
450 fastrpc_buf_free(ctx->buf);
452 spin_lock_irqsave(&cctx->lock, flags);
453 idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4);
454 spin_unlock_irqrestore(&cctx->lock, flags);
460 fastrpc_channel_ctx_put(cctx);
463 static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx)
465 kref_get(&ctx->refcount);
468 static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx)
470 kref_put(&ctx->refcount, fastrpc_context_free);
473 static void fastrpc_context_put_wq(struct work_struct *work)
475 struct fastrpc_invoke_ctx *ctx =
476 container_of(work, struct fastrpc_invoke_ctx, put_work);
478 fastrpc_context_put(ctx);
481 #define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)
482 static int olaps_cmp(const void *a, const void *b)
484 struct fastrpc_buf_overlap *pa = (struct fastrpc_buf_overlap *)a;
485 struct fastrpc_buf_overlap *pb = (struct fastrpc_buf_overlap *)b;
486 /* sort with lowest starting buffer first */
487 int st = CMP(pa->start, pb->start);
488 /* sort with highest ending buffer first */
489 int ed = CMP(pb->end, pa->end);
491 return st == 0 ? ed : st;
494 static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
499 for (i = 0; i < ctx->nbufs; ++i) {
500 ctx->olaps[i].start = ctx->args[i].ptr;
501 ctx->olaps[i].end = ctx->olaps[i].start + ctx->args[i].length;
502 ctx->olaps[i].raix = i;
505 sort(ctx->olaps, ctx->nbufs, sizeof(*ctx->olaps), olaps_cmp, NULL);
507 for (i = 0; i < ctx->nbufs; ++i) {
508 /* Falling inside previous range */
509 if (ctx->olaps[i].start < max_end) {
510 ctx->olaps[i].mstart = max_end;
511 ctx->olaps[i].mend = ctx->olaps[i].end;
512 ctx->olaps[i].offset = max_end - ctx->olaps[i].start;
514 if (ctx->olaps[i].end > max_end) {
515 max_end = ctx->olaps[i].end;
517 ctx->olaps[i].mend = 0;
518 ctx->olaps[i].mstart = 0;
522 ctx->olaps[i].mend = ctx->olaps[i].end;
523 ctx->olaps[i].mstart = ctx->olaps[i].start;
524 ctx->olaps[i].offset = 0;
525 max_end = ctx->olaps[i].end;
530 static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
531 struct fastrpc_user *user, u32 kernel, u32 sc,
532 struct fastrpc_invoke_args *args)
534 struct fastrpc_channel_ctx *cctx = user->cctx;
535 struct fastrpc_invoke_ctx *ctx = NULL;
539 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
541 return ERR_PTR(-ENOMEM);
543 INIT_LIST_HEAD(&ctx->node);
545 ctx->nscalars = REMOTE_SCALARS_LENGTH(sc);
546 ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) +
547 REMOTE_SCALARS_OUTBUFS(sc);
550 ctx->maps = kcalloc(ctx->nscalars,
551 sizeof(*ctx->maps), GFP_KERNEL);
554 return ERR_PTR(-ENOMEM);
556 ctx->olaps = kcalloc(ctx->nscalars,
557 sizeof(*ctx->olaps), GFP_KERNEL);
561 return ERR_PTR(-ENOMEM);
564 fastrpc_get_buff_overlaps(ctx);
567 /* Released in fastrpc_context_put() */
568 fastrpc_channel_ctx_get(cctx);
572 ctx->pid = current->pid;
573 ctx->tgid = user->tgid;
575 init_completion(&ctx->work);
576 INIT_WORK(&ctx->put_work, fastrpc_context_put_wq);
578 spin_lock(&user->lock);
579 list_add_tail(&ctx->node, &user->pending);
580 spin_unlock(&user->lock);
582 spin_lock_irqsave(&cctx->lock, flags);
583 ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1,
584 FASTRPC_CTX_MAX, GFP_ATOMIC);
586 spin_unlock_irqrestore(&cctx->lock, flags);
589 ctx->ctxid = ret << 4;
590 spin_unlock_irqrestore(&cctx->lock, flags);
592 kref_init(&ctx->refcount);
596 spin_lock(&user->lock);
597 list_del(&ctx->node);
598 spin_unlock(&user->lock);
599 fastrpc_channel_ctx_put(cctx);
607 static struct sg_table *
608 fastrpc_map_dma_buf(struct dma_buf_attachment *attachment,
609 enum dma_data_direction dir)
611 struct fastrpc_dma_buf_attachment *a = attachment->priv;
612 struct sg_table *table;
617 ret = dma_map_sgtable(attachment->dev, table, dir, 0);
619 table = ERR_PTR(ret);
623 static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach,
624 struct sg_table *table,
625 enum dma_data_direction dir)
627 dma_unmap_sgtable(attach->dev, table, dir, 0);
630 static void fastrpc_release(struct dma_buf *dmabuf)
632 struct fastrpc_buf *buffer = dmabuf->priv;
634 fastrpc_buf_free(buffer);
637 static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf,
638 struct dma_buf_attachment *attachment)
640 struct fastrpc_dma_buf_attachment *a;
641 struct fastrpc_buf *buffer = dmabuf->priv;
644 a = kzalloc(sizeof(*a), GFP_KERNEL);
648 ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt,
649 FASTRPC_PHYS(buffer->phys), buffer->size);
651 dev_err(buffer->dev, "failed to get scatterlist from DMA API\n");
656 a->dev = attachment->dev;
657 INIT_LIST_HEAD(&a->node);
658 attachment->priv = a;
660 mutex_lock(&buffer->lock);
661 list_add(&a->node, &buffer->attachments);
662 mutex_unlock(&buffer->lock);
667 static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf,
668 struct dma_buf_attachment *attachment)
670 struct fastrpc_dma_buf_attachment *a = attachment->priv;
671 struct fastrpc_buf *buffer = dmabuf->priv;
673 mutex_lock(&buffer->lock);
675 mutex_unlock(&buffer->lock);
676 sg_free_table(&a->sgt);
680 static int fastrpc_vmap(struct dma_buf *dmabuf, struct iosys_map *map)
682 struct fastrpc_buf *buf = dmabuf->priv;
684 iosys_map_set_vaddr(map, buf->virt);
689 static int fastrpc_mmap(struct dma_buf *dmabuf,
690 struct vm_area_struct *vma)
692 struct fastrpc_buf *buf = dmabuf->priv;
693 size_t size = vma->vm_end - vma->vm_start;
695 return dma_mmap_coherent(buf->dev, vma, buf->virt,
696 FASTRPC_PHYS(buf->phys), size);
699 static const struct dma_buf_ops fastrpc_dma_buf_ops = {
700 .attach = fastrpc_dma_buf_attach,
701 .detach = fastrpc_dma_buf_detatch,
702 .map_dma_buf = fastrpc_map_dma_buf,
703 .unmap_dma_buf = fastrpc_unmap_dma_buf,
704 .mmap = fastrpc_mmap,
705 .vmap = fastrpc_vmap,
706 .release = fastrpc_release,
709 static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
710 u64 len, u32 attr, struct fastrpc_map **ppmap)
712 struct fastrpc_session_ctx *sess = fl->sctx;
713 struct fastrpc_map *map = NULL;
716 if (!fastrpc_map_lookup(fl, fd, ppmap, true))
719 map = kzalloc(sizeof(*map), GFP_KERNEL);
723 INIT_LIST_HEAD(&map->node);
726 map->buf = dma_buf_get(fd);
727 if (IS_ERR(map->buf)) {
728 err = PTR_ERR(map->buf);
732 map->attach = dma_buf_attach(map->buf, sess->dev);
733 if (IS_ERR(map->attach)) {
734 dev_err(sess->dev, "Failed to attach dmabuf\n");
735 err = PTR_ERR(map->attach);
739 map->table = dma_buf_map_attachment(map->attach, DMA_BIDIRECTIONAL);
740 if (IS_ERR(map->table)) {
741 err = PTR_ERR(map->table);
745 map->phys = sg_dma_address(map->table->sgl);
746 map->phys += ((u64)fl->sctx->sid << 32);
748 map->va = sg_virt(map->table->sgl);
750 kref_init(&map->refcount);
752 if (attr & FASTRPC_ATTR_SECUREMAP) {
754 * If subsystem VMIDs are defined in DTSI, then do
755 * hyp_assign from HLOS to those VM(s)
757 unsigned int perms = BIT(QCOM_SCM_VMID_HLOS);
760 err = qcom_scm_assign_mem(map->phys, (u64)map->size, &perms,
761 fl->cctx->vmperms, fl->cctx->vmcount);
763 dev_err(sess->dev, "Failed to assign memory with phys 0x%llx size 0x%llx err %d",
764 map->phys, map->size, err);
768 spin_lock(&fl->lock);
769 list_add_tail(&map->node, &fl->maps);
770 spin_unlock(&fl->lock);
776 dma_buf_detach(map->buf, map->attach);
778 dma_buf_put(map->buf);
786 * Fastrpc payload buffer with metadata looks like:
788 * >>>>>> START of METADATA <<<<<<<<<
789 * +---------------------------------+
791 * | type:(union fastrpc_remote_arg)|
793 * +---------------------------------+
794 * | Invoke Buffer list |
795 * | type:(struct fastrpc_invoke_buf)|
797 * +---------------------------------+
799 * | type:(struct fastrpc_phy_page) |
801 * +---------------------------------+
803 * |(can be specific to SoC/Firmware)|
804 * +---------------------------------+
805 * >>>>>>>> END of METADATA <<<<<<<<<
806 * +---------------------------------+
809 * +---------------------------------+
812 static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx)
816 size = (sizeof(struct fastrpc_remote_buf) +
817 sizeof(struct fastrpc_invoke_buf) +
818 sizeof(struct fastrpc_phy_page)) * ctx->nscalars +
819 sizeof(u64) * FASTRPC_MAX_FDLIST +
820 sizeof(u32) * FASTRPC_MAX_CRCLIST;
825 static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen)
830 size = ALIGN(metalen, FASTRPC_ALIGN);
831 for (oix = 0; oix < ctx->nbufs; oix++) {
832 int i = ctx->olaps[oix].raix;
834 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) {
836 if (ctx->olaps[oix].offset == 0)
837 size = ALIGN(size, FASTRPC_ALIGN);
839 size += (ctx->olaps[oix].mend - ctx->olaps[oix].mstart);
846 static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
848 struct device *dev = ctx->fl->sctx->dev;
851 for (i = 0; i < ctx->nscalars; ++i) {
853 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 ||
854 ctx->args[i].length == 0)
857 err = fastrpc_map_create(ctx->fl, ctx->args[i].fd,
858 ctx->args[i].length, ctx->args[i].attr, &ctx->maps[i]);
860 dev_err(dev, "Error Creating map %d\n", err);
868 static struct fastrpc_invoke_buf *fastrpc_invoke_buf_start(union fastrpc_remote_arg *pra, int len)
870 return (struct fastrpc_invoke_buf *)(&pra[len]);
873 static struct fastrpc_phy_page *fastrpc_phy_page_start(struct fastrpc_invoke_buf *buf, int len)
875 return (struct fastrpc_phy_page *)(&buf[len]);
878 static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
880 struct device *dev = ctx->fl->sctx->dev;
881 union fastrpc_remote_arg *rpra;
882 struct fastrpc_invoke_buf *list;
883 struct fastrpc_phy_page *pages;
884 int inbufs, i, oix, err = 0;
885 u64 len, rlen, pkt_size;
886 u64 pg_start, pg_end;
890 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
891 metalen = fastrpc_get_meta_size(ctx);
892 pkt_size = fastrpc_get_payload_size(ctx, metalen);
894 err = fastrpc_create_maps(ctx);
898 ctx->msg_sz = pkt_size;
900 err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf);
904 rpra = ctx->buf->virt;
905 list = fastrpc_invoke_buf_start(rpra, ctx->nscalars);
906 pages = fastrpc_phy_page_start(list, ctx->nscalars);
907 args = (uintptr_t)ctx->buf->virt + metalen;
908 rlen = pkt_size - metalen;
911 for (oix = 0; oix < ctx->nbufs; ++oix) {
914 i = ctx->olaps[oix].raix;
915 len = ctx->args[i].length;
918 rpra[i].buf.len = len;
919 list[i].num = len ? 1 : 0;
926 struct vm_area_struct *vma = NULL;
928 rpra[i].buf.pv = (u64) ctx->args[i].ptr;
929 pages[i].addr = ctx->maps[i]->phys;
931 mmap_read_lock(current->mm);
932 vma = find_vma(current->mm, ctx->args[i].ptr);
934 pages[i].addr += ctx->args[i].ptr -
936 mmap_read_unlock(current->mm);
938 pg_start = (ctx->args[i].ptr & PAGE_MASK) >> PAGE_SHIFT;
939 pg_end = ((ctx->args[i].ptr + len - 1) & PAGE_MASK) >>
941 pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
945 if (ctx->olaps[oix].offset == 0) {
946 rlen -= ALIGN(args, FASTRPC_ALIGN) - args;
947 args = ALIGN(args, FASTRPC_ALIGN);
950 mlen = ctx->olaps[oix].mend - ctx->olaps[oix].mstart;
955 rpra[i].buf.pv = args - ctx->olaps[oix].offset;
956 pages[i].addr = ctx->buf->phys -
957 ctx->olaps[oix].offset +
959 pages[i].addr = pages[i].addr & PAGE_MASK;
961 pg_start = (args & PAGE_MASK) >> PAGE_SHIFT;
962 pg_end = ((args + len - 1) & PAGE_MASK) >> PAGE_SHIFT;
963 pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
968 if (i < inbufs && !ctx->maps[i]) {
969 void *dst = (void *)(uintptr_t)rpra[i].buf.pv;
970 void *src = (void *)(uintptr_t)ctx->args[i].ptr;
973 if (copy_from_user(dst, (void __user *)src,
979 memcpy(dst, src, len);
984 for (i = ctx->nbufs; i < ctx->nscalars; ++i) {
985 list[i].num = ctx->args[i].length ? 1 : 0;
988 pages[i].addr = ctx->maps[i]->phys;
989 pages[i].size = ctx->maps[i]->size;
991 rpra[i].dma.fd = ctx->args[i].fd;
992 rpra[i].dma.len = ctx->args[i].length;
993 rpra[i].dma.offset = (u64) ctx->args[i].ptr;
998 dev_err(dev, "Error: get invoke args failed:%d\n", err);
1003 static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
1006 union fastrpc_remote_arg *rpra = ctx->rpra;
1007 struct fastrpc_user *fl = ctx->fl;
1008 struct fastrpc_map *mmap = NULL;
1009 struct fastrpc_invoke_buf *list;
1010 struct fastrpc_phy_page *pages;
1012 int i, inbufs, outbufs, handles;
1014 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
1015 outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc);
1016 handles = REMOTE_SCALARS_INHANDLES(ctx->sc) + REMOTE_SCALARS_OUTHANDLES(ctx->sc);
1017 list = fastrpc_invoke_buf_start(rpra, ctx->nscalars);
1018 pages = fastrpc_phy_page_start(list, ctx->nscalars);
1019 fdlist = (uint64_t *)(pages + inbufs + outbufs + handles);
1021 for (i = inbufs; i < ctx->nbufs; ++i) {
1022 if (!ctx->maps[i]) {
1023 void *src = (void *)(uintptr_t)rpra[i].buf.pv;
1024 void *dst = (void *)(uintptr_t)ctx->args[i].ptr;
1025 u64 len = rpra[i].buf.len;
1028 if (copy_to_user((void __user *)dst, src, len))
1031 memcpy(dst, src, len);
1036 for (i = 0; i < FASTRPC_MAX_FDLIST; i++) {
1039 if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap, false))
1040 fastrpc_map_put(mmap);
1046 static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx,
1047 struct fastrpc_invoke_ctx *ctx,
1048 u32 kernel, uint32_t handle)
1050 struct fastrpc_channel_ctx *cctx;
1051 struct fastrpc_user *fl = ctx->fl;
1052 struct fastrpc_msg *msg = &ctx->msg;
1056 msg->pid = fl->tgid;
1057 msg->tid = current->pid;
1062 msg->ctx = ctx->ctxid | fl->pd;
1063 msg->handle = handle;
1065 msg->addr = ctx->buf ? ctx->buf->phys : 0;
1066 msg->size = roundup(ctx->msg_sz, PAGE_SIZE);
1067 fastrpc_context_get(ctx);
1069 ret = rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg));
1072 fastrpc_context_put(ctx);
1078 static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
1080 struct fastrpc_invoke_args *args)
1082 struct fastrpc_invoke_ctx *ctx = NULL;
1088 if (!fl->cctx->rpdev)
1091 if (handle == FASTRPC_INIT_HANDLE && !kernel) {
1092 dev_warn_ratelimited(fl->sctx->dev, "user app trying to send a kernel RPC message (%d)\n", handle);
1096 ctx = fastrpc_context_alloc(fl, kernel, sc, args);
1098 return PTR_ERR(ctx);
1100 if (ctx->nscalars) {
1101 err = fastrpc_get_args(kernel, ctx);
1106 /* make sure that all CPU memory writes are seen by DSP */
1108 /* Send invoke buffer to remote dsp */
1109 err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle);
1114 if (!wait_for_completion_timeout(&ctx->work, 10 * HZ))
1117 err = wait_for_completion_interruptible(&ctx->work);
1123 /* Check the response from remote dsp */
1128 if (ctx->nscalars) {
1129 /* make sure that all memory writes by DSP are seen by CPU */
1131 /* populate all the output buffers with results */
1132 err = fastrpc_put_args(ctx, kernel);
1138 if (err != -ERESTARTSYS && err != -ETIMEDOUT) {
1139 /* We are done with this compute context */
1140 spin_lock(&fl->lock);
1141 list_del(&ctx->node);
1142 spin_unlock(&fl->lock);
1143 fastrpc_context_put(ctx);
1146 dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err);
1151 static bool is_session_rejected(struct fastrpc_user *fl, bool unsigned_pd_request)
1153 /* Check if the device node is non-secure and channel is secure*/
1154 if (!fl->is_secure_dev && fl->cctx->secure) {
1156 * Allow untrusted applications to offload only to Unsigned PD when
1157 * channel is configured as secure and block untrusted apps on channel
1158 * that does not support unsigned PD offload
1160 if (!fl->cctx->unsigned_support || !unsigned_pd_request) {
1161 dev_err(&fl->cctx->rpdev->dev, "Error: Untrusted application trying to offload to signed PD");
1169 static int fastrpc_init_create_process(struct fastrpc_user *fl,
1172 struct fastrpc_init_create init;
1173 struct fastrpc_invoke_args *args;
1174 struct fastrpc_phy_page pages[1];
1175 struct fastrpc_map *map = NULL;
1176 struct fastrpc_buf *imem = NULL;
1188 bool unsigned_module = false;
1190 args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL);
1194 if (copy_from_user(&init, argp, sizeof(init))) {
1199 if (init.attrs & FASTRPC_MODE_UNSIGNED_MODULE)
1200 unsigned_module = true;
1202 if (is_session_rejected(fl, unsigned_module)) {
1203 err = -ECONNREFUSED;
1207 if (init.filelen > INIT_FILELEN_MAX) {
1212 inbuf.pgid = fl->tgid;
1213 inbuf.namelen = strlen(current->comm) + 1;
1214 inbuf.filelen = init.filelen;
1216 inbuf.attrs = init.attrs;
1217 inbuf.siglen = init.siglen;
1220 if (init.filelen && init.filefd) {
1221 err = fastrpc_map_create(fl, init.filefd, init.filelen, 0, &map);
1226 memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4),
1228 err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen,
1233 fl->init_mem = imem;
1234 args[0].ptr = (u64)(uintptr_t)&inbuf;
1235 args[0].length = sizeof(inbuf);
1238 args[1].ptr = (u64)(uintptr_t)current->comm;
1239 args[1].length = inbuf.namelen;
1242 args[2].ptr = (u64) init.file;
1243 args[2].length = inbuf.filelen;
1244 args[2].fd = init.filefd;
1246 pages[0].addr = imem->phys;
1247 pages[0].size = imem->size;
1249 args[3].ptr = (u64)(uintptr_t) pages;
1250 args[3].length = 1 * sizeof(*pages);
1253 args[4].ptr = (u64)(uintptr_t)&inbuf.attrs;
1254 args[4].length = sizeof(inbuf.attrs);
1257 args[5].ptr = (u64)(uintptr_t) &inbuf.siglen;
1258 args[5].length = sizeof(inbuf.siglen);
1261 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0);
1263 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0);
1265 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1275 fl->init_mem = NULL;
1276 fastrpc_buf_free(imem);
1278 fastrpc_map_put(map);
1285 static struct fastrpc_session_ctx *fastrpc_session_alloc(
1286 struct fastrpc_channel_ctx *cctx)
1288 struct fastrpc_session_ctx *session = NULL;
1289 unsigned long flags;
1292 spin_lock_irqsave(&cctx->lock, flags);
1293 for (i = 0; i < cctx->sesscount; i++) {
1294 if (!cctx->session[i].used && cctx->session[i].valid) {
1295 cctx->session[i].used = true;
1296 session = &cctx->session[i];
1300 spin_unlock_irqrestore(&cctx->lock, flags);
1305 static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx,
1306 struct fastrpc_session_ctx *session)
1308 unsigned long flags;
1310 spin_lock_irqsave(&cctx->lock, flags);
1311 session->used = false;
1312 spin_unlock_irqrestore(&cctx->lock, flags);
1315 static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl)
1317 struct fastrpc_invoke_args args[1];
1322 args[0].ptr = (u64)(uintptr_t) &tgid;
1323 args[0].length = sizeof(tgid);
1325 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0);
1327 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1331 static int fastrpc_device_release(struct inode *inode, struct file *file)
1333 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1334 struct fastrpc_channel_ctx *cctx = fl->cctx;
1335 struct fastrpc_invoke_ctx *ctx, *n;
1336 struct fastrpc_map *map, *m;
1337 struct fastrpc_buf *buf, *b;
1338 unsigned long flags;
1340 fastrpc_release_current_dsp_process(fl);
1342 spin_lock_irqsave(&cctx->lock, flags);
1343 list_del(&fl->user);
1344 spin_unlock_irqrestore(&cctx->lock, flags);
1347 fastrpc_buf_free(fl->init_mem);
1349 list_for_each_entry_safe(ctx, n, &fl->pending, node) {
1350 list_del(&ctx->node);
1351 fastrpc_context_put(ctx);
1354 list_for_each_entry_safe(map, m, &fl->maps, node)
1355 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);