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>
22 #include <linux/of_reserved_mem.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 INIT_FILE_NAMELEN_MAX (128)
41 #define FASTRPC_DEVICE_NAME "fastrpc"
43 /* Add memory to static PD pool, protection thru XPU */
44 #define ADSP_MMAP_HEAP_ADDR 4
45 /* MAP static DMA buffer on DSP User PD */
46 #define ADSP_MMAP_DMA_BUFFER 6
47 /* Add memory to static PD pool protection thru hypervisor */
48 #define ADSP_MMAP_REMOTE_HEAP_ADDR 8
49 /* Add memory to userPD pool, for user heap */
50 #define ADSP_MMAP_ADD_PAGES 0x1000
51 /* Add memory to userPD pool, for LLC heap */
52 #define ADSP_MMAP_ADD_PAGES_LLC 0x3000,
54 #define DSP_UNSUPPORTED_API (0x80000414)
55 /* MAX NUMBER of DSP ATTRIBUTES SUPPORTED */
56 #define FASTRPC_MAX_DSP_ATTRIBUTES (256)
57 #define FASTRPC_MAX_DSP_ATTRIBUTES_LEN (sizeof(u32) * FASTRPC_MAX_DSP_ATTRIBUTES)
59 /* Retrives number of input buffers from the scalars parameter */
60 #define REMOTE_SCALARS_INBUFS(sc) (((sc) >> 16) & 0x0ff)
62 /* Retrives number of output buffers from the scalars parameter */
63 #define REMOTE_SCALARS_OUTBUFS(sc) (((sc) >> 8) & 0x0ff)
65 /* Retrives number of input handles from the scalars parameter */
66 #define REMOTE_SCALARS_INHANDLES(sc) (((sc) >> 4) & 0x0f)
68 /* Retrives number of output handles from the scalars parameter */
69 #define REMOTE_SCALARS_OUTHANDLES(sc) ((sc) & 0x0f)
71 #define REMOTE_SCALARS_LENGTH(sc) (REMOTE_SCALARS_INBUFS(sc) + \
72 REMOTE_SCALARS_OUTBUFS(sc) + \
73 REMOTE_SCALARS_INHANDLES(sc)+ \
74 REMOTE_SCALARS_OUTHANDLES(sc))
75 #define FASTRPC_BUILD_SCALARS(attr, method, in, out, oin, oout) \
76 (((attr & 0x07) << 29) | \
77 ((method & 0x1f) << 24) | \
78 ((in & 0xff) << 16) | \
79 ((out & 0xff) << 8) | \
80 ((oin & 0x0f) << 4) | \
83 #define FASTRPC_SCALARS(method, in, out) \
84 FASTRPC_BUILD_SCALARS(0, method, in, out, 0, 0)
86 #define FASTRPC_CREATE_PROCESS_NARGS 6
87 #define FASTRPC_CREATE_STATIC_PROCESS_NARGS 3
88 /* Remote Method id table */
89 #define FASTRPC_RMID_INIT_ATTACH 0
90 #define FASTRPC_RMID_INIT_RELEASE 1
91 #define FASTRPC_RMID_INIT_MMAP 4
92 #define FASTRPC_RMID_INIT_MUNMAP 5
93 #define FASTRPC_RMID_INIT_CREATE 6
94 #define FASTRPC_RMID_INIT_CREATE_ATTR 7
95 #define FASTRPC_RMID_INIT_CREATE_STATIC 8
96 #define FASTRPC_RMID_INIT_MEM_MAP 10
97 #define FASTRPC_RMID_INIT_MEM_UNMAP 11
99 /* Protection Domain(PD) ids */
102 #define SENSORS_PD (2)
104 #define miscdev_to_fdevice(d) container_of(d, struct fastrpc_device, miscdev)
106 static const char *domains[FASTRPC_DEV_MAX] = { "adsp", "mdsp",
108 struct fastrpc_phy_page {
109 u64 addr; /* physical address */
110 u64 size; /* size of contiguous region */
113 struct fastrpc_invoke_buf {
114 u32 num; /* number of contiguous regions */
115 u32 pgidx; /* index to start of contiguous region */
118 struct fastrpc_remote_dmahandle {
119 s32 fd; /* dma handle fd */
120 u32 offset; /* dma handle offset */
121 u32 len; /* dma handle length */
124 struct fastrpc_remote_buf {
125 u64 pv; /* buffer pointer */
126 u64 len; /* length of buffer */
129 union fastrpc_remote_arg {
130 struct fastrpc_remote_buf buf;
131 struct fastrpc_remote_dmahandle dma;
134 struct fastrpc_mmap_rsp_msg {
138 struct fastrpc_mmap_req_msg {
145 struct fastrpc_mem_map_req_msg {
155 struct fastrpc_munmap_req_msg {
161 struct fastrpc_mem_unmap_req_msg {
169 int pid; /* process group id */
170 int tid; /* thread id */
171 u64 ctx; /* invoke caller context */
172 u32 handle; /* handle to invoke */
173 u32 sc; /* scalars structure describing the data */
174 u64 addr; /* physical address */
175 u64 size; /* size of contiguous region */
178 struct fastrpc_invoke_rsp {
179 u64 ctx; /* invoke caller context */
180 int retval; /* invoke return value */
183 struct fastrpc_buf_overlap {
193 struct fastrpc_user *fl;
194 struct dma_buf *dmabuf;
199 /* Lock for dma buf attachments */
201 struct list_head attachments;
203 struct list_head node; /* list of user requested mmaps */
207 struct fastrpc_dma_buf_attachment {
210 struct list_head node;
214 struct list_head node;
215 struct fastrpc_user *fl;
218 struct sg_table *table;
219 struct dma_buf_attachment *attach;
226 struct kref refcount;
229 struct fastrpc_invoke_ctx {
239 struct kref refcount;
240 struct list_head node; /* list of ctxs */
241 struct completion work;
242 struct work_struct put_work;
243 struct fastrpc_msg msg;
244 struct fastrpc_user *fl;
245 union fastrpc_remote_arg *rpra;
246 struct fastrpc_map **maps;
247 struct fastrpc_buf *buf;
248 struct fastrpc_invoke_args *args;
249 struct fastrpc_buf_overlap *olaps;
250 struct fastrpc_channel_ctx *cctx;
253 struct fastrpc_session_ctx {
260 struct fastrpc_channel_ctx {
265 struct qcom_scm_vmperm vmperms[FASTRPC_MAX_VMIDS];
266 struct rpmsg_device *rpdev;
267 struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS];
270 struct list_head users;
271 struct kref refcount;
272 /* Flag if dsp attributes are cached */
273 bool valid_attributes;
274 u32 dsp_attributes[FASTRPC_MAX_DSP_ATTRIBUTES];
275 struct fastrpc_device *secure_fdevice;
276 struct fastrpc_device *fdevice;
277 struct fastrpc_buf *remote_heap;
278 struct list_head invoke_interrupted_mmaps;
280 bool unsigned_support;
284 struct fastrpc_device {
285 struct fastrpc_channel_ctx *cctx;
286 struct miscdevice miscdev;
290 struct fastrpc_user {
291 struct list_head user;
292 struct list_head maps;
293 struct list_head pending;
294 struct list_head mmaps;
296 struct fastrpc_channel_ctx *cctx;
297 struct fastrpc_session_ctx *sctx;
298 struct fastrpc_buf *init_mem;
305 /* lock for allocations */
309 static void fastrpc_free_map(struct kref *ref)
311 struct fastrpc_map *map;
313 map = container_of(ref, struct fastrpc_map, refcount);
316 if (map->attr & FASTRPC_ATTR_SECUREMAP) {
317 struct qcom_scm_vmperm perm;
320 perm.vmid = QCOM_SCM_VMID_HLOS;
321 perm.perm = QCOM_SCM_PERM_RWX;
322 err = qcom_scm_assign_mem(map->phys, map->size,
323 &(map->fl->cctx->vmperms[0].vmid), &perm, 1);
325 dev_err(map->fl->sctx->dev, "Failed to assign memory phys 0x%llx size 0x%llx err %d",
326 map->phys, map->size, err);
330 dma_buf_unmap_attachment(map->attach, map->table,
332 dma_buf_detach(map->buf, map->attach);
333 dma_buf_put(map->buf);
339 static void fastrpc_map_put(struct fastrpc_map *map)
342 kref_put(&map->refcount, fastrpc_free_map);
345 static void fastrpc_map_get(struct fastrpc_map *map)
348 kref_get(&map->refcount);
352 static int fastrpc_map_lookup(struct fastrpc_user *fl, int fd,
353 struct fastrpc_map **ppmap)
355 struct fastrpc_map *map = NULL;
357 mutex_lock(&fl->mutex);
358 list_for_each_entry(map, &fl->maps, node) {
361 mutex_unlock(&fl->mutex);
365 mutex_unlock(&fl->mutex);
370 static int fastrpc_map_find(struct fastrpc_user *fl, int fd,
371 struct fastrpc_map **ppmap)
373 int ret = fastrpc_map_lookup(fl, fd, ppmap);
376 fastrpc_map_get(*ppmap);
381 static void fastrpc_buf_free(struct fastrpc_buf *buf)
383 dma_free_coherent(buf->dev, buf->size, buf->virt,
384 FASTRPC_PHYS(buf->phys));
388 static int __fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
389 u64 size, struct fastrpc_buf **obuf)
391 struct fastrpc_buf *buf;
393 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
397 INIT_LIST_HEAD(&buf->attachments);
398 INIT_LIST_HEAD(&buf->node);
399 mutex_init(&buf->lock);
408 buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys,
411 mutex_destroy(&buf->lock);
421 static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
422 u64 size, struct fastrpc_buf **obuf)
425 struct fastrpc_buf *buf;
427 ret = __fastrpc_buf_alloc(fl, dev, size, obuf);
433 if (fl->sctx && fl->sctx->sid)
434 buf->phys += ((u64)fl->sctx->sid << 32);
439 static int fastrpc_remote_heap_alloc(struct fastrpc_user *fl, struct device *dev,
440 u64 size, struct fastrpc_buf **obuf)
442 struct device *rdev = &fl->cctx->rpdev->dev;
444 return __fastrpc_buf_alloc(fl, rdev, size, obuf);
447 static void fastrpc_channel_ctx_free(struct kref *ref)
449 struct fastrpc_channel_ctx *cctx;
451 cctx = container_of(ref, struct fastrpc_channel_ctx, refcount);
456 static void fastrpc_channel_ctx_get(struct fastrpc_channel_ctx *cctx)
458 kref_get(&cctx->refcount);
461 static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx)
463 kref_put(&cctx->refcount, fastrpc_channel_ctx_free);
466 static void fastrpc_context_free(struct kref *ref)
468 struct fastrpc_invoke_ctx *ctx;
469 struct fastrpc_channel_ctx *cctx;
473 ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
476 for (i = 0; i < ctx->nbufs; i++)
477 fastrpc_map_put(ctx->maps[i]);
480 fastrpc_buf_free(ctx->buf);
482 spin_lock_irqsave(&cctx->lock, flags);
483 idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4);
484 spin_unlock_irqrestore(&cctx->lock, flags);
490 fastrpc_channel_ctx_put(cctx);
493 static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx)
495 kref_get(&ctx->refcount);
498 static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx)
500 kref_put(&ctx->refcount, fastrpc_context_free);
503 static void fastrpc_context_put_wq(struct work_struct *work)
505 struct fastrpc_invoke_ctx *ctx =
506 container_of(work, struct fastrpc_invoke_ctx, put_work);
508 fastrpc_context_put(ctx);
511 #define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)
512 static int olaps_cmp(const void *a, const void *b)
514 struct fastrpc_buf_overlap *pa = (struct fastrpc_buf_overlap *)a;
515 struct fastrpc_buf_overlap *pb = (struct fastrpc_buf_overlap *)b;
516 /* sort with lowest starting buffer first */
517 int st = CMP(pa->start, pb->start);
518 /* sort with highest ending buffer first */
519 int ed = CMP(pb->end, pa->end);
521 return st == 0 ? ed : st;
524 static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
529 for (i = 0; i < ctx->nbufs; ++i) {
530 ctx->olaps[i].start = ctx->args[i].ptr;
531 ctx->olaps[i].end = ctx->olaps[i].start + ctx->args[i].length;
532 ctx->olaps[i].raix = i;
535 sort(ctx->olaps, ctx->nbufs, sizeof(*ctx->olaps), olaps_cmp, NULL);
537 for (i = 0; i < ctx->nbufs; ++i) {
538 /* Falling inside previous range */
539 if (ctx->olaps[i].start < max_end) {
540 ctx->olaps[i].mstart = max_end;
541 ctx->olaps[i].mend = ctx->olaps[i].end;
542 ctx->olaps[i].offset = max_end - ctx->olaps[i].start;
544 if (ctx->olaps[i].end > max_end) {
545 max_end = ctx->olaps[i].end;
547 ctx->olaps[i].mend = 0;
548 ctx->olaps[i].mstart = 0;
552 ctx->olaps[i].mend = ctx->olaps[i].end;
553 ctx->olaps[i].mstart = ctx->olaps[i].start;
554 ctx->olaps[i].offset = 0;
555 max_end = ctx->olaps[i].end;
560 static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
561 struct fastrpc_user *user, u32 kernel, u32 sc,
562 struct fastrpc_invoke_args *args)
564 struct fastrpc_channel_ctx *cctx = user->cctx;
565 struct fastrpc_invoke_ctx *ctx = NULL;
569 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
571 return ERR_PTR(-ENOMEM);
573 INIT_LIST_HEAD(&ctx->node);
575 ctx->nscalars = REMOTE_SCALARS_LENGTH(sc);
576 ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) +
577 REMOTE_SCALARS_OUTBUFS(sc);
580 ctx->maps = kcalloc(ctx->nscalars,
581 sizeof(*ctx->maps), GFP_KERNEL);
584 return ERR_PTR(-ENOMEM);
586 ctx->olaps = kcalloc(ctx->nscalars,
587 sizeof(*ctx->olaps), GFP_KERNEL);
591 return ERR_PTR(-ENOMEM);
594 fastrpc_get_buff_overlaps(ctx);
597 /* Released in fastrpc_context_put() */
598 fastrpc_channel_ctx_get(cctx);
602 ctx->pid = current->pid;
603 ctx->tgid = user->tgid;
605 init_completion(&ctx->work);
606 INIT_WORK(&ctx->put_work, fastrpc_context_put_wq);
608 spin_lock(&user->lock);
609 list_add_tail(&ctx->node, &user->pending);
610 spin_unlock(&user->lock);
612 spin_lock_irqsave(&cctx->lock, flags);
613 ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1,
614 FASTRPC_CTX_MAX, GFP_ATOMIC);
616 spin_unlock_irqrestore(&cctx->lock, flags);
619 ctx->ctxid = ret << 4;
620 spin_unlock_irqrestore(&cctx->lock, flags);
622 kref_init(&ctx->refcount);
626 spin_lock(&user->lock);
627 list_del(&ctx->node);
628 spin_unlock(&user->lock);
629 fastrpc_channel_ctx_put(cctx);
637 static struct sg_table *
638 fastrpc_map_dma_buf(struct dma_buf_attachment *attachment,
639 enum dma_data_direction dir)
641 struct fastrpc_dma_buf_attachment *a = attachment->priv;
642 struct sg_table *table;
647 ret = dma_map_sgtable(attachment->dev, table, dir, 0);
649 table = ERR_PTR(ret);
653 static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach,
654 struct sg_table *table,
655 enum dma_data_direction dir)
657 dma_unmap_sgtable(attach->dev, table, dir, 0);
660 static void fastrpc_release(struct dma_buf *dmabuf)
662 struct fastrpc_buf *buffer = dmabuf->priv;
664 fastrpc_buf_free(buffer);
667 static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf,
668 struct dma_buf_attachment *attachment)
670 struct fastrpc_dma_buf_attachment *a;
671 struct fastrpc_buf *buffer = dmabuf->priv;
674 a = kzalloc(sizeof(*a), GFP_KERNEL);
678 ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt,
679 FASTRPC_PHYS(buffer->phys), buffer->size);
681 dev_err(buffer->dev, "failed to get scatterlist from DMA API\n");
686 a->dev = attachment->dev;
687 INIT_LIST_HEAD(&a->node);
688 attachment->priv = a;
690 mutex_lock(&buffer->lock);
691 list_add(&a->node, &buffer->attachments);
692 mutex_unlock(&buffer->lock);
697 static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf,
698 struct dma_buf_attachment *attachment)
700 struct fastrpc_dma_buf_attachment *a = attachment->priv;
701 struct fastrpc_buf *buffer = dmabuf->priv;
703 mutex_lock(&buffer->lock);
705 mutex_unlock(&buffer->lock);
706 sg_free_table(&a->sgt);
710 static int fastrpc_vmap(struct dma_buf *dmabuf, struct iosys_map *map)
712 struct fastrpc_buf *buf = dmabuf->priv;
714 iosys_map_set_vaddr(map, buf->virt);
719 static int fastrpc_mmap(struct dma_buf *dmabuf,
720 struct vm_area_struct *vma)
722 struct fastrpc_buf *buf = dmabuf->priv;
723 size_t size = vma->vm_end - vma->vm_start;
725 return dma_mmap_coherent(buf->dev, vma, buf->virt,
726 FASTRPC_PHYS(buf->phys), size);
729 static const struct dma_buf_ops fastrpc_dma_buf_ops = {
730 .attach = fastrpc_dma_buf_attach,
731 .detach = fastrpc_dma_buf_detatch,
732 .map_dma_buf = fastrpc_map_dma_buf,
733 .unmap_dma_buf = fastrpc_unmap_dma_buf,
734 .mmap = fastrpc_mmap,
735 .vmap = fastrpc_vmap,
736 .release = fastrpc_release,
739 static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
740 u64 len, u32 attr, struct fastrpc_map **ppmap)
742 struct fastrpc_session_ctx *sess = fl->sctx;
743 struct fastrpc_map *map = NULL;
746 if (!fastrpc_map_find(fl, fd, ppmap))
749 map = kzalloc(sizeof(*map), GFP_KERNEL);
753 INIT_LIST_HEAD(&map->node);
754 kref_init(&map->refcount);
758 map->buf = dma_buf_get(fd);
759 if (IS_ERR(map->buf)) {
760 err = PTR_ERR(map->buf);
764 map->attach = dma_buf_attach(map->buf, sess->dev);
765 if (IS_ERR(map->attach)) {
766 dev_err(sess->dev, "Failed to attach dmabuf\n");
767 err = PTR_ERR(map->attach);
771 map->table = dma_buf_map_attachment(map->attach, DMA_BIDIRECTIONAL);
772 if (IS_ERR(map->table)) {
773 err = PTR_ERR(map->table);
777 map->phys = sg_dma_address(map->table->sgl);
778 map->phys += ((u64)fl->sctx->sid << 32);
780 map->va = sg_virt(map->table->sgl);
783 if (attr & FASTRPC_ATTR_SECUREMAP) {
785 * If subsystem VMIDs are defined in DTSI, then do
786 * hyp_assign from HLOS to those VM(s)
788 unsigned int perms = BIT(QCOM_SCM_VMID_HLOS);
791 err = qcom_scm_assign_mem(map->phys, (u64)map->size, &perms,
792 fl->cctx->vmperms, fl->cctx->vmcount);
794 dev_err(sess->dev, "Failed to assign memory with phys 0x%llx size 0x%llx err %d",
795 map->phys, map->size, err);
799 spin_lock(&fl->lock);
800 list_add_tail(&map->node, &fl->maps);
801 spin_unlock(&fl->lock);
807 dma_buf_detach(map->buf, map->attach);
809 dma_buf_put(map->buf);
811 fastrpc_map_put(map);
817 * Fastrpc payload buffer with metadata looks like:
819 * >>>>>> START of METADATA <<<<<<<<<
820 * +---------------------------------+
822 * | type:(union fastrpc_remote_arg)|
824 * +---------------------------------+
825 * | Invoke Buffer list |
826 * | type:(struct fastrpc_invoke_buf)|
828 * +---------------------------------+
830 * | type:(struct fastrpc_phy_page) |
832 * +---------------------------------+
834 * |(can be specific to SoC/Firmware)|
835 * +---------------------------------+
836 * >>>>>>>> END of METADATA <<<<<<<<<
837 * +---------------------------------+
840 * +---------------------------------+
843 static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx)
847 size = (sizeof(struct fastrpc_remote_buf) +
848 sizeof(struct fastrpc_invoke_buf) +
849 sizeof(struct fastrpc_phy_page)) * ctx->nscalars +
850 sizeof(u64) * FASTRPC_MAX_FDLIST +
851 sizeof(u32) * FASTRPC_MAX_CRCLIST;
856 static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen)
861 size = ALIGN(metalen, FASTRPC_ALIGN);
862 for (oix = 0; oix < ctx->nbufs; oix++) {
863 int i = ctx->olaps[oix].raix;
865 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) {
867 if (ctx->olaps[oix].offset == 0)
868 size = ALIGN(size, FASTRPC_ALIGN);
870 size += (ctx->olaps[oix].mend - ctx->olaps[oix].mstart);
877 static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
879 struct device *dev = ctx->fl->sctx->dev;
882 for (i = 0; i < ctx->nscalars; ++i) {
884 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 ||
885 ctx->args[i].length == 0)
888 err = fastrpc_map_create(ctx->fl, ctx->args[i].fd,
889 ctx->args[i].length, ctx->args[i].attr, &ctx->maps[i]);
891 dev_err(dev, "Error Creating map %d\n", err);
899 static struct fastrpc_invoke_buf *fastrpc_invoke_buf_start(union fastrpc_remote_arg *pra, int len)
901 return (struct fastrpc_invoke_buf *)(&pra[len]);
904 static struct fastrpc_phy_page *fastrpc_phy_page_start(struct fastrpc_invoke_buf *buf, int len)
906 return (struct fastrpc_phy_page *)(&buf[len]);
909 static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
911 struct device *dev = ctx->fl->sctx->dev;
912 union fastrpc_remote_arg *rpra;
913 struct fastrpc_invoke_buf *list;
914 struct fastrpc_phy_page *pages;
915 int inbufs, i, oix, err = 0;
916 u64 len, rlen, pkt_size;
917 u64 pg_start, pg_end;
921 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
922 metalen = fastrpc_get_meta_size(ctx);
923 pkt_size = fastrpc_get_payload_size(ctx, metalen);
925 err = fastrpc_create_maps(ctx);
929 ctx->msg_sz = pkt_size;
931 err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf);
935 rpra = ctx->buf->virt;
936 list = fastrpc_invoke_buf_start(rpra, ctx->nscalars);
937 pages = fastrpc_phy_page_start(list, ctx->nscalars);
938 args = (uintptr_t)ctx->buf->virt + metalen;
939 rlen = pkt_size - metalen;
942 for (oix = 0; oix < ctx->nbufs; ++oix) {
945 i = ctx->olaps[oix].raix;
946 len = ctx->args[i].length;
949 rpra[i].buf.len = len;
950 list[i].num = len ? 1 : 0;
957 struct vm_area_struct *vma = NULL;
959 rpra[i].buf.pv = (u64) ctx->args[i].ptr;
960 pages[i].addr = ctx->maps[i]->phys;
962 mmap_read_lock(current->mm);
963 vma = find_vma(current->mm, ctx->args[i].ptr);
965 pages[i].addr += ctx->args[i].ptr -
967 mmap_read_unlock(current->mm);
969 pg_start = (ctx->args[i].ptr & PAGE_MASK) >> PAGE_SHIFT;
970 pg_end = ((ctx->args[i].ptr + len - 1) & PAGE_MASK) >>
972 pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
976 if (ctx->olaps[oix].offset == 0) {
977 rlen -= ALIGN(args, FASTRPC_ALIGN) - args;
978 args = ALIGN(args, FASTRPC_ALIGN);
981 mlen = ctx->olaps[oix].mend - ctx->olaps[oix].mstart;
986 rpra[i].buf.pv = args - ctx->olaps[oix].offset;
987 pages[i].addr = ctx->buf->phys -
988 ctx->olaps[oix].offset +
990 pages[i].addr = pages[i].addr & PAGE_MASK;
992 pg_start = (args & PAGE_MASK) >> PAGE_SHIFT;
993 pg_end = ((args + len - 1) & PAGE_MASK) >> PAGE_SHIFT;
994 pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
999 if (i < inbufs && !ctx->maps[i]) {
1000 void *dst = (void *)(uintptr_t)rpra[i].buf.pv;
1001 void *src = (void *)(uintptr_t)ctx->args[i].ptr;
1004 if (copy_from_user(dst, (void __user *)src,
1010 memcpy(dst, src, len);
1015 for (i = ctx->nbufs; i < ctx->nscalars; ++i) {
1016 list[i].num = ctx->args[i].length ? 1 : 0;
1019 pages[i].addr = ctx->maps[i]->phys;
1020 pages[i].size = ctx->maps[i]->size;
1022 rpra[i].dma.fd = ctx->args[i].fd;
1023 rpra[i].dma.len = ctx->args[i].length;
1024 rpra[i].dma.offset = (u64) ctx->args[i].ptr;
1029 dev_err(dev, "Error: get invoke args failed:%d\n", err);
1034 static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
1037 union fastrpc_remote_arg *rpra = ctx->rpra;
1038 struct fastrpc_user *fl = ctx->fl;
1039 struct fastrpc_map *mmap = NULL;
1040 struct fastrpc_invoke_buf *list;
1041 struct fastrpc_phy_page *pages;
1043 int i, inbufs, outbufs, handles;
1045 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
1046 outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc);
1047 handles = REMOTE_SCALARS_INHANDLES(ctx->sc) + REMOTE_SCALARS_OUTHANDLES(ctx->sc);
1048 list = fastrpc_invoke_buf_start(rpra, ctx->nscalars);
1049 pages = fastrpc_phy_page_start(list, ctx->nscalars);
1050 fdlist = (uint64_t *)(pages + inbufs + outbufs + handles);
1052 for (i = inbufs; i < ctx->nbufs; ++i) {
1053 if (!ctx->maps[i]) {
1054 void *src = (void *)(uintptr_t)rpra[i].buf.pv;
1055 void *dst = (void *)(uintptr_t)ctx->args[i].ptr;
1056 u64 len = rpra[i].buf.len;
1059 if (copy_to_user((void __user *)dst, src, len))
1062 memcpy(dst, src, len);
1067 for (i = 0; i < FASTRPC_MAX_FDLIST; i++) {
1070 if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap))
1071 fastrpc_map_put(mmap);
1077 static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx,
1078 struct fastrpc_invoke_ctx *ctx,
1079 u32 kernel, uint32_t handle)
1081 struct fastrpc_channel_ctx *cctx;
1082 struct fastrpc_user *fl = ctx->fl;
1083 struct fastrpc_msg *msg = &ctx->msg;
1087 msg->pid = fl->tgid;
1088 msg->tid = current->pid;
1093 msg->ctx = ctx->ctxid | fl->pd;
1094 msg->handle = handle;
1096 msg->addr = ctx->buf ? ctx->buf->phys : 0;
1097 msg->size = roundup(ctx->msg_sz, PAGE_SIZE);
1098 fastrpc_context_get(ctx);
1100 ret = rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg));
1103 fastrpc_context_put(ctx);
1109 static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
1111 struct fastrpc_invoke_args *args)
1113 struct fastrpc_invoke_ctx *ctx = NULL;
1114 struct fastrpc_buf *buf, *b;
1121 if (!fl->cctx->rpdev)
1124 if (handle == FASTRPC_INIT_HANDLE && !kernel) {
1125 dev_warn_ratelimited(fl->sctx->dev, "user app trying to send a kernel RPC message (%d)\n", handle);
1129 ctx = fastrpc_context_alloc(fl, kernel, sc, args);
1131 return PTR_ERR(ctx);
1133 if (ctx->nscalars) {
1134 err = fastrpc_get_args(kernel, ctx);
1139 /* make sure that all CPU memory writes are seen by DSP */
1141 /* Send invoke buffer to remote dsp */
1142 err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle);
1147 if (!wait_for_completion_timeout(&ctx->work, 10 * HZ))
1150 err = wait_for_completion_interruptible(&ctx->work);
1156 /* Check the response from remote dsp */
1161 if (ctx->nscalars) {
1162 /* make sure that all memory writes by DSP are seen by CPU */
1164 /* populate all the output buffers with results */
1165 err = fastrpc_put_args(ctx, kernel);
1171 if (err != -ERESTARTSYS && err != -ETIMEDOUT) {
1172 /* We are done with this compute context */
1173 spin_lock(&fl->lock);
1174 list_del(&ctx->node);
1175 spin_unlock(&fl->lock);
1176 fastrpc_context_put(ctx);
1179 if (err == -ERESTARTSYS) {
1180 list_for_each_entry_safe(buf, b, &fl->mmaps, node) {
1181 list_del(&buf->node);
1182 list_add_tail(&buf->node, &fl->cctx->invoke_interrupted_mmaps);
1187 dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err);
1192 static bool is_session_rejected(struct fastrpc_user *fl, bool unsigned_pd_request)
1194 /* Check if the device node is non-secure and channel is secure*/
1195 if (!fl->is_secure_dev && fl->cctx->secure) {
1197 * Allow untrusted applications to offload only to Unsigned PD when
1198 * channel is configured as secure and block untrusted apps on channel
1199 * that does not support unsigned PD offload
1201 if (!fl->cctx->unsigned_support || !unsigned_pd_request) {
1202 dev_err(&fl->cctx->rpdev->dev, "Error: Untrusted application trying to offload to signed PD");
1210 static int fastrpc_init_create_static_process(struct fastrpc_user *fl,
1213 struct fastrpc_init_create_static init;
1214 struct fastrpc_invoke_args *args;
1215 struct fastrpc_phy_page pages[1];
1225 args = kcalloc(FASTRPC_CREATE_STATIC_PROCESS_NARGS, sizeof(*args), GFP_KERNEL);
1229 if (copy_from_user(&init, argp, sizeof(init))) {
1234 if (init.namelen > INIT_FILE_NAMELEN_MAX) {
1239 name = kzalloc(init.namelen, GFP_KERNEL);
1245 if (copy_from_user(name, (void __user *)(uintptr_t)init.name, init.namelen)) {
1250 if (!fl->cctx->remote_heap) {
1251 err = fastrpc_remote_heap_alloc(fl, fl->sctx->dev, init.memlen,
1252 &fl->cctx->remote_heap);
1256 /* Map if we have any heap VMIDs associated with this ADSP Static Process. */
1257 if (fl->cctx->vmcount) {
1258 unsigned int perms = BIT(QCOM_SCM_VMID_HLOS);
1260 err = qcom_scm_assign_mem(fl->cctx->remote_heap->phys,
1261 (u64)fl->cctx->remote_heap->size, &perms,
1262 fl->cctx->vmperms, fl->cctx->vmcount);
1264 dev_err(fl->sctx->dev, "Failed to assign memory with phys 0x%llx size 0x%llx err %d",
1265 fl->cctx->remote_heap->phys, fl->cctx->remote_heap->size, err);
1271 inbuf.pgid = fl->tgid;
1272 inbuf.namelen = init.namelen;
1276 args[0].ptr = (u64)(uintptr_t)&inbuf;
1277 args[0].length = sizeof(inbuf);
1280 args[1].ptr = (u64)(uintptr_t)name;
1281 args[1].length = inbuf.namelen;
1284 pages[0].addr = fl->cctx->remote_heap->phys;
1285 pages[0].size = fl->cctx->remote_heap->size;
1287 args[2].ptr = (u64)(uintptr_t) pages;
1288 args[2].length = sizeof(*pages);
1291 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_STATIC, 3, 0);
1293 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1302 if (fl->cctx->vmcount) {
1303 struct qcom_scm_vmperm perm;
1305 perm.vmid = QCOM_SCM_VMID_HLOS;
1306 perm.perm = QCOM_SCM_PERM_RWX;
1307 err = qcom_scm_assign_mem(fl->cctx->remote_heap->phys,
1308 (u64)fl->cctx->remote_heap->size,
1309 &(fl->cctx->vmperms[0].vmid), &perm, 1);
1311 dev_err(fl->sctx->dev, "Failed to assign memory phys 0x%llx size 0x%llx err %d",
1312 fl->cctx->remote_heap->phys, fl->cctx->remote_heap->size, err);
1315 fastrpc_buf_free(fl->cctx->remote_heap);
1324 static int fastrpc_init_create_process(struct fastrpc_user *fl,
1327 struct fastrpc_init_create init;
1328 struct fastrpc_invoke_args *args;
1329 struct fastrpc_phy_page pages[1];
1330 struct fastrpc_map *map = NULL;
1331 struct fastrpc_buf *imem = NULL;
1343 bool unsigned_module = false;
1345 args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL);
1349 if (copy_from_user(&init, argp, sizeof(init))) {
1354 if (init.attrs & FASTRPC_MODE_UNSIGNED_MODULE)
1355 unsigned_module = true;
1357 if (is_session_rejected(fl, unsigned_module)) {
1358 err = -ECONNREFUSED;
1362 if (init.filelen > INIT_FILELEN_MAX) {
1367 inbuf.pgid = fl->tgid;
1368 inbuf.namelen = strlen(current->comm) + 1;
1369 inbuf.filelen = init.filelen;
1371 inbuf.attrs = init.attrs;
1372 inbuf.siglen = init.siglen;
1375 if (init.filelen && init.filefd) {
1376 err = fastrpc_map_create(fl, init.filefd, init.filelen, 0, &map);
1381 memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4),
1383 err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen,
1388 fl->init_mem = imem;
1389 args[0].ptr = (u64)(uintptr_t)&inbuf;
1390 args[0].length = sizeof(inbuf);
1393 args[1].ptr = (u64)(uintptr_t)current->comm;
1394 args[1].length = inbuf.namelen;
1397 args[2].ptr = (u64) init.file;
1398 args[2].length = inbuf.filelen;
1399 args[2].fd = init.filefd;
1401 pages[0].addr = imem->phys;
1402 pages[0].size = imem->size;
1404 args[3].ptr = (u64)(uintptr_t) pages;
1405 args[3].length = 1 * sizeof(*pages);
1408 args[4].ptr = (u64)(uintptr_t)&inbuf.attrs;
1409 args[4].length = sizeof(inbuf.attrs);
1412 args[5].ptr = (u64)(uintptr_t) &inbuf.siglen;
1413 args[5].length = sizeof(inbuf.siglen);
1416 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0);
1418 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0);
1420 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1430 fl->init_mem = NULL;
1431 fastrpc_buf_free(imem);
1434 spin_lock(&fl->lock);
1435 list_del(&map->node);
1436 spin_unlock(&fl->lock);
1437 fastrpc_map_put(map);
1445 static struct fastrpc_session_ctx *fastrpc_session_alloc(
1446 struct fastrpc_channel_ctx *cctx)
1448 struct fastrpc_session_ctx *session = NULL;
1449 unsigned long flags;
1452 spin_lock_irqsave(&cctx->lock, flags);
1453 for (i = 0; i < cctx->sesscount; i++) {
1454 if (!cctx->session[i].used && cctx->session[i].valid) {
1455 cctx->session[i].used = true;
1456 session = &cctx->session[i];
1460 spin_unlock_irqrestore(&cctx->lock, flags);
1465 static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx,
1466 struct fastrpc_session_ctx *session)
1468 unsigned long flags;
1470 spin_lock_irqsave(&cctx->lock, flags);
1471 session->used = false;
1472 spin_unlock_irqrestore(&cctx->lock, flags);
1475 static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl)
1477 struct fastrpc_invoke_args args[1];
1482 args[0].ptr = (u64)(uintptr_t) &tgid;
1483 args[0].length = sizeof(tgid);
1485 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0);
1487 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1491 static int fastrpc_device_release(struct inode *inode, struct file *file)
1493 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1494 struct fastrpc_channel_ctx *cctx = fl->cctx;
1495 struct fastrpc_invoke_ctx *ctx, *n;
1496 struct fastrpc_map *map, *m;
1497 struct fastrpc_buf *buf, *b;
1498 unsigned long flags;
1500 fastrpc_release_current_dsp_process(fl);
1502 spin_lock_irqsave(&cctx->lock, flags);
1503 list_del(&fl->user);
1504 spin_unlock_irqrestore(&cctx->lock, flags);
1507 fastrpc_buf_free(fl->init_mem);
1509 list_for_each_entry_safe(ctx, n, &fl->pending, node) {
1510 list_del(&ctx->node);
1511 fastrpc_context_put(ctx);
1514 list_for_each_entry_safe(map, m, &fl->maps, node) {
1515 list_del(&map->node);
1516 fastrpc_map_put(map);
1519 list_for_each_entry_safe(buf, b, &fl->mmaps, node) {
1520 list_del(&buf->node);
1521 fastrpc_buf_free(buf);
1524 fastrpc_session_free(cctx, fl->sctx);
1525 fastrpc_channel_ctx_put(cctx);
1527 mutex_destroy(&fl->mutex);
1529 file->private_data = NULL;
1534 static int fastrpc_device_open(struct inode *inode, struct file *filp)
1536 struct fastrpc_channel_ctx *cctx;
1537 struct fastrpc_device *fdevice;
1538 struct fastrpc_user *fl = NULL;
1539 unsigned long flags;
1541 fdevice = miscdev_to_fdevice(filp->private_data);
1542 cctx = fdevice->cctx;
1544 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
1548 /* Released in fastrpc_device_release() */
1549 fastrpc_channel_ctx_get(cctx);
1551 filp->private_data = fl;
1552 spin_lock_init(&fl->lock);
1553 mutex_init(&fl->mutex);
1554 INIT_LIST_HEAD(&fl->pending);
1555 INIT_LIST_HEAD(&fl->maps);
1556 INIT_LIST_HEAD(&fl->mmaps);
1557 INIT_LIST_HEAD(&fl->user);
1558 fl->tgid = current->tgid;
1560 fl->is_secure_dev = fdevice->secure;
1562 fl->sctx = fastrpc_session_alloc(cctx);
1564 dev_err(&cctx->rpdev->dev, "No session available\n");
1565 mutex_destroy(&fl->mutex);
1571 spin_lock_irqsave(&cctx->lock, flags);
1572 list_add_tail(&fl->user, &cctx->users);
1573 spin_unlock_irqrestore(&cctx->lock, flags);
1578 static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
1580 struct fastrpc_alloc_dma_buf bp;
1581 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
1582 struct fastrpc_buf *buf = NULL;
1585 if (copy_from_user(&bp, argp, sizeof(bp)))
1588 err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf);
1591 exp_info.ops = &fastrpc_dma_buf_ops;
1592 exp_info.size = bp.size;
1593 exp_info.flags = O_RDWR;
1594 exp_info.priv = buf;
1595 buf->dmabuf = dma_buf_export(&exp_info);
1596 if (IS_ERR(buf->dmabuf)) {
1597 err = PTR_ERR(buf->dmabuf);
1598 fastrpc_buf_free(buf);
1602 bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE);
1604 dma_buf_put(buf->dmabuf);
1608 if (copy_to_user(argp, &bp, sizeof(bp))) {
1610 * The usercopy failed, but we can't do much about it, as
1611 * dma_buf_fd() already called fd_install() and made the
1612 * file descriptor accessible for the current process. It
1613 * might already be closed and dmabuf no longer valid when
1614 * we reach this point. Therefore "leak" the fd and rely on
1615 * the process exit path to do any required cleanup.
1623 static int fastrpc_init_attach(struct fastrpc_user *fl, int pd)
1625 struct fastrpc_invoke_args args[1];
1626 int tgid = fl->tgid;
1629 args[0].ptr = (u64)(uintptr_t) &tgid;
1630 args[0].length = sizeof(tgid);
1632 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0);
1635 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1639 static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp)
1641 struct fastrpc_invoke_args *args = NULL;
1642 struct fastrpc_invoke inv;
1646 if (copy_from_user(&inv, argp, sizeof(inv)))
1649 /* nscalars is truncated here to max supported value */
1650 nscalars = REMOTE_SCALARS_LENGTH(inv.sc);
1652 args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL);
1656 if (copy_from_user(args, (void __user *)(uintptr_t)inv.args,
1657 nscalars * sizeof(*args))) {
1663 err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args);
1669 static int fastrpc_get_info_from_dsp(struct fastrpc_user *fl, uint32_t *dsp_attr_buf,
1670 uint32_t dsp_attr_buf_len)
1672 struct fastrpc_invoke_args args[2] = { 0 };
1674 /* Capability filled in userspace */
1675 dsp_attr_buf[0] = 0;
1677 args[0].ptr = (u64)(uintptr_t)&dsp_attr_buf_len;
1678 args[0].length = sizeof(dsp_attr_buf_len);
1680 args[1].ptr = (u64)(uintptr_t)&dsp_attr_buf[1];
1681 args[1].length = dsp_attr_buf_len;
1685 return fastrpc_internal_invoke(fl, true, FASTRPC_DSP_UTILITIES_HANDLE,
1686 FASTRPC_SCALARS(0, 1, 1), args);
1689 static int fastrpc_get_info_from_kernel(struct fastrpc_ioctl_capability *cap,
1690 struct fastrpc_user *fl)
1692 struct fastrpc_channel_ctx *cctx = fl->cctx;
1693 uint32_t attribute_id = cap->attribute_id;
1694 uint32_t *dsp_attributes;
1695 unsigned long flags;
1696 uint32_t domain = cap->domain;
1699 spin_lock_irqsave(&cctx->lock, flags);
1700 /* check if we already have queried dsp for attributes */
1701 if (cctx->valid_attributes) {
1702 spin_unlock_irqrestore(&cctx->lock, flags);
1705 spin_unlock_irqrestore(&cctx->lock, flags);
1707 dsp_attributes = kzalloc(FASTRPC_MAX_DSP_ATTRIBUTES_LEN, GFP_KERNEL);
1708 if (!dsp_attributes)
1711 err = fastrpc_get_info_from_dsp(fl, dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES_LEN);
1712 if (err == DSP_UNSUPPORTED_API) {
1713 dev_info(&cctx->rpdev->dev,
1714 "Warning: DSP capabilities not supported on domain: %d\n", domain);
1715 kfree(dsp_attributes);
1718 dev_err(&cctx->rpdev->dev, "Error: dsp information is incorrect err: %d\n", err);
1719 kfree(dsp_attributes);
1723 spin_lock_irqsave(&cctx->lock, flags);
1724 memcpy(cctx->dsp_attributes, dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES_LEN);
1725 cctx->valid_attributes = true;
1726 spin_unlock_irqrestore(&cctx->lock, flags);
1727 kfree(dsp_attributes);
1729 cap->capability = cctx->dsp_attributes[attribute_id];
1733 static int fastrpc_get_dsp_info(struct fastrpc_user *fl, char __user *argp)
1735 struct fastrpc_ioctl_capability cap = {0};
1738 if (copy_from_user(&cap, argp, sizeof(cap)))
1742 if (cap.domain >= FASTRPC_DEV_MAX) {
1743 dev_err(&fl->cctx->rpdev->dev, "Error: Invalid domain id:%d, err:%d\n",
1748 /* Fastrpc Capablities does not support modem domain */
1749 if (cap.domain == MDSP_DOMAIN_ID) {
1750 dev_err(&fl->cctx->rpdev->dev, "Error: modem not supported %d\n", err);
1754 if (cap.attribute_id >= FASTRPC_MAX_DSP_ATTRIBUTES) {
1755 dev_err(&fl->cctx->rpdev->dev, "Error: invalid attribute: %d, err: %d\n",
1756 cap.attribute_id, err);
1760 err = fastrpc_get_info_from_kernel(&cap, fl);
1764 if (copy_to_user(argp, &cap.capability, sizeof(cap.capability)))
1770 static int fastrpc_req_munmap_impl(struct fastrpc_user *fl, struct fastrpc_buf *buf)
1772 struct fastrpc_invoke_args args[1] = { [0] = { 0 } };
1773 struct fastrpc_munmap_req_msg req_msg;
1774 struct device *dev = fl->sctx->dev;
1778 req_msg.pgid = fl->tgid;
1779 req_msg.size = buf->size;
1780 req_msg.vaddr = buf->raddr;
1782 args[0].ptr = (u64) (uintptr_t) &req_msg;
1783 args[0].length = sizeof(req_msg);
1785 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MUNMAP, 1, 0);
1786 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc,
1789 dev_dbg(dev, "unmmap\tpt 0x%09lx OK\n", buf->raddr);
1790 spin_lock(&fl->lock);
1791 list_del(&buf->node);
1792 spin_unlock(&fl->lock);
1793 fastrpc_buf_free(buf);
1795 dev_err(dev, "unmmap\tpt 0x%09lx ERROR\n", buf->raddr);
1801 static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)
1803 struct fastrpc_buf *buf = NULL, *iter, *b;
1804 struct fastrpc_req_munmap req;
1805 struct device *dev = fl->sctx->dev;
1807 if (copy_from_user(&req, argp, sizeof(req)))
1810 spin_lock(&fl->lock);
1811 list_for_each_entry_safe(iter, b, &fl->mmaps, node) {
1812 if ((iter->raddr == req.vaddrout) && (iter->size == req.size)) {
1817 spin_unlock(&fl->lock);
1820 dev_err(dev, "mmap\t\tpt 0x%09llx [len 0x%08llx] not in list\n",
1821 req.vaddrout, req.size);
1825 return fastrpc_req_munmap_impl(fl, buf);
1828 static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp)
1830 struct fastrpc_invoke_args args[3] = { [0 ... 2] = { 0 } };
1831 struct fastrpc_buf *buf = NULL;
1832 struct fastrpc_mmap_req_msg req_msg;
1833 struct fastrpc_mmap_rsp_msg rsp_msg;
1834 struct fastrpc_phy_page pages;
1835 struct fastrpc_req_mmap req;
1836 struct device *dev = fl->sctx->dev;
1840 if (copy_from_user(&req, argp, sizeof(req)))
1843 if (req.flags != ADSP_MMAP_ADD_PAGES && req.flags != ADSP_MMAP_REMOTE_HEAP_ADDR) {
1844 dev_err(dev, "flag not supported 0x%x\n", req.flags);
1850 dev_err(dev, "adding user allocated pages is not supported\n");
1854 err = fastrpc_buf_alloc(fl, fl->sctx->dev, req.size, &buf);
1856 dev_err(dev, "failed to allocate buffer\n");
1860 req_msg.pgid = fl->tgid;
1861 req_msg.flags = req.flags;
1862 req_msg.vaddr = req.vaddrin;
1863 req_msg.num = sizeof(pages);
1865 args[0].ptr = (u64) (uintptr_t) &req_msg;
1866 args[0].length = sizeof(req_msg);
1868 pages.addr = buf->phys;
1869 pages.size = buf->size;
1871 args[1].ptr = (u64) (uintptr_t) &pages;
1872 args[1].length = sizeof(pages);
1874 args[2].ptr = (u64) (uintptr_t) &rsp_msg;
1875 args[2].length = sizeof(rsp_msg);
1877 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MMAP, 2, 1);
1878 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc,
1881 dev_err(dev, "mmap error (len 0x%08llx)\n", buf->size);
1885 /* update the buffer to be able to deallocate the memory on the DSP */
1886 buf->raddr = (uintptr_t) rsp_msg.vaddr;
1888 /* let the client know the address to use */
1889 req.vaddrout = rsp_msg.vaddr;
1891 /* Add memory to static PD pool, protection thru hypervisor */
1892 if (req.flags != ADSP_MMAP_REMOTE_HEAP_ADDR && fl->cctx->vmcount) {
1893 struct qcom_scm_vmperm perm;
1896 perm.vmid = QCOM_SCM_VMID_HLOS;
1897 perm.perm = QCOM_SCM_PERM_RWX;
1898 err = qcom_scm_assign_mem(buf->phys, buf->size,
1899 &(fl->cctx->vmperms[0].vmid), &perm, 1);
1901 dev_err(fl->sctx->dev, "Failed to assign memory phys 0x%llx size 0x%llx err %d",
1902 buf->phys, buf->size, err);
1907 spin_lock(&fl->lock);
1908 list_add_tail(&buf->node, &fl->mmaps);
1909 spin_unlock(&fl->lock);
1911 if (copy_to_user((void __user *)argp, &req, sizeof(req))) {
1916 dev_dbg(dev, "mmap\t\tpt 0x%09lx OK [len 0x%08llx]\n",
1917 buf->raddr, buf->size);
1922 fastrpc_req_munmap_impl(fl, buf);
1924 fastrpc_buf_free(buf);
1929 static int fastrpc_req_mem_unmap_impl(struct fastrpc_user *fl, struct fastrpc_mem_unmap *req)
1931 struct fastrpc_invoke_args args[1] = { [0] = { 0 } };
1932 struct fastrpc_map *map = NULL, *iter, *m;
1933 struct fastrpc_mem_unmap_req_msg req_msg = { 0 };
1936 struct device *dev = fl->sctx->dev;
1938 spin_lock(&fl->lock);
1939 list_for_each_entry_safe(iter, m, &fl->maps, node) {
1940 if ((req->fd < 0 || iter->fd == req->fd) && (iter->raddr == req->vaddr)) {
1946 spin_unlock(&fl->lock);
1949 dev_err(dev, "map not in list\n");
1953 req_msg.pgid = fl->tgid;
1954 req_msg.len = map->len;
1955 req_msg.vaddrin = map->raddr;
1956 req_msg.fd = map->fd;
1958 args[0].ptr = (u64) (uintptr_t) &req_msg;
1959 args[0].length = sizeof(req_msg);
1961 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MEM_UNMAP, 1, 0);
1962 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc,
1964 fastrpc_map_put(map);
1966 dev_err(dev, "unmmap\tpt fd = %d, 0x%09llx error\n", map->fd, map->raddr);
1971 static int fastrpc_req_mem_unmap(struct fastrpc_user *fl, char __user *argp)
1973 struct fastrpc_mem_unmap req;
1975 if (copy_from_user(&req, argp, sizeof(req)))
1978 return fastrpc_req_mem_unmap_impl(fl, &req);
1981 static int fastrpc_req_mem_map(struct fastrpc_user *fl, char __user *argp)
1983 struct fastrpc_invoke_args args[4] = { [0 ... 3] = { 0 } };
1984 struct fastrpc_mem_map_req_msg req_msg = { 0 };
1985 struct fastrpc_mmap_rsp_msg rsp_msg = { 0 };
1986 struct fastrpc_mem_unmap req_unmap = { 0 };
1987 struct fastrpc_phy_page pages = { 0 };
1988 struct fastrpc_mem_map req;
1989 struct device *dev = fl->sctx->dev;
1990 struct fastrpc_map *map = NULL;
1994 if (copy_from_user(&req, argp, sizeof(req)))
1997 /* create SMMU mapping */
1998 err = fastrpc_map_create(fl, req.fd, req.length, 0, &map);
2000 dev_err(dev, "failed to map buffer, fd = %d\n", req.fd);
2004 req_msg.pgid = fl->tgid;
2005 req_msg.fd = req.fd;
2006 req_msg.offset = req.offset;
2007 req_msg.vaddrin = req.vaddrin;
2008 map->va = (void *) (uintptr_t) req.vaddrin;
2009 req_msg.flags = req.flags;
2010 req_msg.num = sizeof(pages);
2011 req_msg.data_len = 0;
2013 args[0].ptr = (u64) (uintptr_t) &req_msg;
2014 args[0].length = sizeof(req_msg);
2016 pages.addr = map->phys;
2017 pages.size = map->size;
2019 args[1].ptr = (u64) (uintptr_t) &pages;
2020 args[1].length = sizeof(pages);
2022 args[2].ptr = (u64) (uintptr_t) &pages;
2025 args[3].ptr = (u64) (uintptr_t) &rsp_msg;
2026 args[3].length = sizeof(rsp_msg);
2028 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MEM_MAP, 3, 1);
2029 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, &args[0]);
2031 dev_err(dev, "mem mmap error, fd %d, vaddr %llx, size %lld\n",
2032 req.fd, req.vaddrin, map->size);
2036 /* update the buffer to be able to deallocate the memory on the DSP */
2037 map->raddr = rsp_msg.vaddr;
2039 /* let the client know the address to use */
2040 req.vaddrout = rsp_msg.vaddr;
2042 if (copy_to_user((void __user *)argp, &req, sizeof(req))) {
2043 /* unmap the memory and release the buffer */
2044 req_unmap.vaddr = (uintptr_t) rsp_msg.vaddr;
2045 req_unmap.length = map->size;
2046 fastrpc_req_mem_unmap_impl(fl, &req_unmap);
2053 fastrpc_map_put(map);
2058 static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
2061 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
2062 char __user *argp = (char __user *)arg;
2066 case FASTRPC_IOCTL_INVOKE:
2067 err = fastrpc_invoke(fl, argp);
2069 case FASTRPC_IOCTL_INIT_ATTACH:
2070 err = fastrpc_init_attach(fl, ROOT_PD);
2072 case FASTRPC_IOCTL_INIT_ATTACH_SNS:
2073 err = fastrpc_init_attach(fl, SENSORS_PD);
2075 case FASTRPC_IOCTL_INIT_CREATE_STATIC:
2076 err = fastrpc_init_create_static_process(fl, argp);
2078 case FASTRPC_IOCTL_INIT_CREATE:
2079 err = fastrpc_init_create_process(fl, argp);
2081 case FASTRPC_IOCTL_ALLOC_DMA_BUFF:
2082 err = fastrpc_dmabuf_alloc(fl, argp);
2084 case FASTRPC_IOCTL_MMAP:
2085 err = fastrpc_req_mmap(fl, argp);
2087 case FASTRPC_IOCTL_MUNMAP:
2088 err = fastrpc_req_munmap(fl, argp);
2090 case FASTRPC_IOCTL_MEM_MAP:
2091 err = fastrpc_req_mem_map(fl, argp);
2093 case FASTRPC_IOCTL_MEM_UNMAP:
2094 err = fastrpc_req_mem_unmap(fl, argp);
2096 case FASTRPC_IOCTL_GET_DSP_INFO:
2097 err = fastrpc_get_dsp_info(fl, argp);
2107 static const struct file_operations fastrpc_fops = {
2108 .open = fastrpc_device_open,
2109 .release = fastrpc_device_release,
2110 .unlocked_ioctl = fastrpc_device_ioctl,
2111 .compat_ioctl = fastrpc_device_ioctl,
2114 static int fastrpc_cb_probe(struct platform_device *pdev)
2116 struct fastrpc_channel_ctx *cctx;
2117 struct fastrpc_session_ctx *sess;
2118 struct device *dev = &pdev->dev;
2119 int i, sessions = 0;
2120 unsigned long flags;
2123 cctx = dev_get_drvdata(dev->parent);
2127 of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions);
2129 spin_lock_irqsave(&cctx->lock, flags);
2130 if (cctx->sesscount >= FASTRPC_MAX_SESSIONS) {
2131 dev_err(&pdev->dev, "too many sessions\n");
2132 spin_unlock_irqrestore(&cctx->lock, flags);
2135 sess = &cctx->session[cctx->sesscount++];
2139 dev_set_drvdata(dev, sess);
2141 if (of_property_read_u32(dev->of_node, "reg", &sess->sid))
2142 dev_info(dev, "FastRPC Session ID not specified in DT\n");
2145 struct fastrpc_session_ctx *dup_sess;
2147 for (i = 1; i < sessions; i++) {
2148 if (cctx->sesscount >= FASTRPC_MAX_SESSIONS)
2150 dup_sess = &cctx->session[cctx->sesscount++];
2151 memcpy(dup_sess, sess, sizeof(*dup_sess));
2154 spin_unlock_irqrestore(&cctx->lock, flags);
2155 rc = dma_set_mask(dev, DMA_BIT_MASK(32));
2157 dev_err(dev, "32-bit DMA enable failed\n");
2164 static int fastrpc_cb_remove(struct platform_device *pdev)
2166 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent);
2167 struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev);
2168 unsigned long flags;
2171 spin_lock_irqsave(&cctx->lock, flags);
2172 for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) {
2173 if (cctx->session[i].sid == sess->sid) {
2174 cctx->session[i].valid = false;
2178 spin_unlock_irqrestore(&cctx->lock, flags);
2183 static const struct of_device_id fastrpc_match_table[] = {
2184 { .compatible = "qcom,fastrpc-compute-cb", },
2188 static struct platform_driver fastrpc_cb_driver = {
2189 .probe = fastrpc_cb_probe,
2190 .remove = fastrpc_cb_remove,
2192 .name = "qcom,fastrpc-cb",
2193 .of_match_table = fastrpc_match_table,
2194 .suppress_bind_attrs = true,
2198 static int fastrpc_device_register(struct device *dev, struct fastrpc_channel_ctx *cctx,
2199 bool is_secured, const char *domain)
2201 struct fastrpc_device *fdev;
2204 fdev = devm_kzalloc(dev, sizeof(*fdev), GFP_KERNEL);
2208 fdev->secure = is_secured;
2210 fdev->miscdev.minor = MISC_DYNAMIC_MINOR;
2211 fdev->miscdev.fops = &fastrpc_fops;
2212 fdev->miscdev.name = devm_kasprintf(dev, GFP_KERNEL, "fastrpc-%s%s",
2213 domain, is_secured ? "-secure" : "");
2214 err = misc_register(&fdev->miscdev);
2217 cctx->secure_fdevice = fdev;
2219 cctx->fdevice = fdev;
2225 static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
2227 struct device *rdev = &rpdev->dev;
2228 struct fastrpc_channel_ctx *data;
2229 int i, err, domain_id = -1, vmcount;
2232 unsigned int vmids[FASTRPC_MAX_VMIDS];
2234 err = of_property_read_string(rdev->of_node, "label", &domain);
2236 dev_info(rdev, "FastRPC Domain not specified in DT\n");
2240 for (i = 0; i <= CDSP_DOMAIN_ID; i++) {
2241 if (!strcmp(domains[i], domain)) {
2247 if (domain_id < 0) {
2248 dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id);
2252 if (of_reserved_mem_device_init_by_idx(rdev, rdev->of_node, 0))
2253 dev_info(rdev, "no reserved DMA memory for FASTRPC\n");
2255 vmcount = of_property_read_variable_u32_array(rdev->of_node,
2256 "qcom,vmids", &vmids[0], 0, FASTRPC_MAX_VMIDS);
2259 else if (!qcom_scm_is_available())
2260 return -EPROBE_DEFER;
2262 data = kzalloc(sizeof(*data), GFP_KERNEL);
2267 data->vmcount = vmcount;
2268 data->perms = BIT(QCOM_SCM_VMID_HLOS);
2269 for (i = 0; i < data->vmcount; i++) {
2270 data->vmperms[i].vmid = vmids[i];
2271 data->vmperms[i].perm = QCOM_SCM_PERM_RWX;
2275 secure_dsp = !(of_property_read_bool(rdev->of_node, "qcom,non-secure-domain"));
2276 data->secure = secure_dsp;
2278 switch (domain_id) {
2279 case ADSP_DOMAIN_ID:
2280 case MDSP_DOMAIN_ID:
2281 case SDSP_DOMAIN_ID:
2282 /* Unsigned PD offloading is only supported on CDSP*/
2283 data->unsigned_support = false;
2284 err = fastrpc_device_register(rdev, data, secure_dsp, domains[domain_id]);
2288 case CDSP_DOMAIN_ID:
2289 data->unsigned_support = true;
2290 /* Create both device nodes so that we can allow both Signed and Unsigned PD */
2291 err = fastrpc_device_register(rdev, data, true, domains[domain_id]);
2295 err = fastrpc_device_register(rdev, data, false, domains[domain_id]);
2304 kref_init(&data->refcount);
2306 dev_set_drvdata(&rpdev->dev, data);
2307 rdev->dma_mask = &data->dma_mask;
2308 dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
2309 INIT_LIST_HEAD(&data->users);
2310 INIT_LIST_HEAD(&data->invoke_interrupted_mmaps);
2311 spin_lock_init(&data->lock);
2312 idr_init(&data->ctx_idr);
2313 data->domain_id = domain_id;
2314 data->rpdev = rpdev;
2316 return of_platform_populate(rdev->of_node, NULL, NULL, rdev);
2322 static void fastrpc_notify_users(struct fastrpc_user *user)
2324 struct fastrpc_invoke_ctx *ctx;
2326 spin_lock(&user->lock);
2327 list_for_each_entry(ctx, &user->pending, node)
2328 complete(&ctx->work);
2329 spin_unlock(&user->lock);
2332 static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
2334 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
2335 struct fastrpc_buf *buf, *b;
2336 struct fastrpc_user *user;
2337 unsigned long flags;
2339 spin_lock_irqsave(&cctx->lock, flags);
2340 list_for_each_entry(user, &cctx->users, user)
2341 fastrpc_notify_users(user);
2342 spin_unlock_irqrestore(&cctx->lock, flags);
2345 misc_deregister(&cctx->fdevice->miscdev);
2347 if (cctx->secure_fdevice)
2348 misc_deregister(&cctx->secure_fdevice->miscdev);
2350 list_for_each_entry_safe(buf, b, &cctx->invoke_interrupted_mmaps, node)
2351 list_del(&buf->node);
2353 if (cctx->remote_heap)
2354 fastrpc_buf_free(cctx->remote_heap);
2356 of_platform_depopulate(&rpdev->dev);
2359 fastrpc_channel_ctx_put(cctx);
2362 static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
2363 int len, void *priv, u32 addr)
2365 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
2366 struct fastrpc_invoke_rsp *rsp = data;
2367 struct fastrpc_invoke_ctx *ctx;
2368 unsigned long flags;
2369 unsigned long ctxid;
2371 if (len < sizeof(*rsp))
2374 ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4);
2376 spin_lock_irqsave(&cctx->lock, flags);
2377 ctx = idr_find(&cctx->ctx_idr, ctxid);
2378 spin_unlock_irqrestore(&cctx->lock, flags);
2381 dev_err(&rpdev->dev, "No context ID matches response\n");
2385 ctx->retval = rsp->retval;
2386 complete(&ctx->work);
2389 * The DMA buffer associated with the context cannot be freed in
2390 * interrupt context so schedule it through a worker thread to
2391 * avoid a kernel BUG.
2393 schedule_work(&ctx->put_work);
2398 static const struct of_device_id fastrpc_rpmsg_of_match[] = {
2399 { .compatible = "qcom,fastrpc" },
2402 MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match);
2404 static struct rpmsg_driver fastrpc_driver = {
2405 .probe = fastrpc_rpmsg_probe,
2406 .remove = fastrpc_rpmsg_remove,
2407 .callback = fastrpc_rpmsg_callback,
2409 .name = "qcom,fastrpc",
2410 .of_match_table = fastrpc_rpmsg_of_match,
2414 static int fastrpc_init(void)
2418 ret = platform_driver_register(&fastrpc_cb_driver);
2420 pr_err("fastrpc: failed to register cb driver\n");
2424 ret = register_rpmsg_driver(&fastrpc_driver);
2426 pr_err("fastrpc: failed to register rpmsg driver\n");
2427 platform_driver_unregister(&fastrpc_cb_driver);
2433 module_init(fastrpc_init);
2435 static void fastrpc_exit(void)
2437 platform_driver_unregister(&fastrpc_cb_driver);
2438 unregister_rpmsg_driver(&fastrpc_driver);
2440 module_exit(fastrpc_exit);
2442 MODULE_LICENSE("GPL v2");
2443 MODULE_IMPORT_NS(DMA_BUF);