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 <uapi/misc/fastrpc.h>
22 #define ADSP_DOMAIN_ID (0)
23 #define MDSP_DOMAIN_ID (1)
24 #define SDSP_DOMAIN_ID (2)
25 #define CDSP_DOMAIN_ID (3)
26 #define FASTRPC_DEV_MAX 4 /* adsp, mdsp, slpi, cdsp*/
27 #define FASTRPC_MAX_SESSIONS 9 /*8 compute, 1 cpz*/
28 #define FASTRPC_ALIGN 128
29 #define FASTRPC_MAX_FDLIST 16
30 #define FASTRPC_MAX_CRCLIST 64
31 #define FASTRPC_PHYS(p) ((p) & 0xffffffff)
32 #define FASTRPC_CTX_MAX (256)
33 #define FASTRPC_INIT_HANDLE 1
34 #define FASTRPC_CTXID_MASK (0xFF0)
35 #define INIT_FILELEN_MAX (64 * 1024 * 1024)
36 #define INIT_MEMLEN_MAX (8 * 1024 * 1024)
37 #define FASTRPC_DEVICE_NAME "fastrpc"
39 /* Retrives number of input buffers from the scalars parameter */
40 #define REMOTE_SCALARS_INBUFS(sc) (((sc) >> 16) & 0x0ff)
42 /* Retrives number of output buffers from the scalars parameter */
43 #define REMOTE_SCALARS_OUTBUFS(sc) (((sc) >> 8) & 0x0ff)
45 /* Retrives number of input handles from the scalars parameter */
46 #define REMOTE_SCALARS_INHANDLES(sc) (((sc) >> 4) & 0x0f)
48 /* Retrives number of output handles from the scalars parameter */
49 #define REMOTE_SCALARS_OUTHANDLES(sc) ((sc) & 0x0f)
51 #define REMOTE_SCALARS_LENGTH(sc) (REMOTE_SCALARS_INBUFS(sc) + \
52 REMOTE_SCALARS_OUTBUFS(sc) + \
53 REMOTE_SCALARS_INHANDLES(sc)+ \
54 REMOTE_SCALARS_OUTHANDLES(sc))
55 #define FASTRPC_BUILD_SCALARS(attr, method, in, out, oin, oout) \
56 (((attr & 0x07) << 29) | \
57 ((method & 0x1f) << 24) | \
58 ((in & 0xff) << 16) | \
59 ((out & 0xff) << 8) | \
60 ((oin & 0x0f) << 4) | \
63 #define FASTRPC_SCALARS(method, in, out) \
64 FASTRPC_BUILD_SCALARS(0, method, in, out, 0, 0)
66 #define FASTRPC_CREATE_PROCESS_NARGS 6
67 /* Remote Method id table */
68 #define FASTRPC_RMID_INIT_ATTACH 0
69 #define FASTRPC_RMID_INIT_RELEASE 1
70 #define FASTRPC_RMID_INIT_CREATE 6
71 #define FASTRPC_RMID_INIT_CREATE_ATTR 7
72 #define FASTRPC_RMID_INIT_CREATE_STATIC 8
74 #define miscdev_to_cctx(d) container_of(d, struct fastrpc_channel_ctx, miscdev)
76 static const char *domains[FASTRPC_DEV_MAX] = { "adsp", "mdsp",
78 struct fastrpc_phy_page {
79 u64 addr; /* physical address */
80 u64 size; /* size of contiguous region */
83 struct fastrpc_invoke_buf {
84 u32 num; /* number of contiguous regions */
85 u32 pgidx; /* index to start of contiguous region */
88 struct fastrpc_remote_arg {
94 int pid; /* process group id */
95 int tid; /* thread id */
96 u64 ctx; /* invoke caller context */
97 u32 handle; /* handle to invoke */
98 u32 sc; /* scalars structure describing the data */
99 u64 addr; /* physical address */
100 u64 size; /* size of contiguous region */
103 struct fastrpc_invoke_rsp {
104 u64 ctx; /* invoke caller context */
105 int retval; /* invoke return value */
108 struct fastrpc_buf_overlap {
118 struct fastrpc_user *fl;
119 struct dma_buf *dmabuf;
124 /* Lock for dma buf attachments */
126 struct list_head attachments;
129 struct fastrpc_dma_buf_attachment {
132 struct list_head node;
136 struct list_head node;
137 struct fastrpc_user *fl;
140 struct sg_table *table;
141 struct dma_buf_attachment *attach;
146 struct kref refcount;
149 struct fastrpc_invoke_ctx {
159 struct kref refcount;
160 struct list_head node; /* list of ctxs */
161 struct completion work;
162 struct work_struct put_work;
163 struct fastrpc_msg msg;
164 struct fastrpc_user *fl;
165 struct fastrpc_remote_arg *rpra;
166 struct fastrpc_map **maps;
167 struct fastrpc_buf *buf;
168 struct fastrpc_invoke_args *args;
169 struct fastrpc_buf_overlap *olaps;
170 struct fastrpc_channel_ctx *cctx;
173 struct fastrpc_session_ctx {
180 struct fastrpc_channel_ctx {
183 struct rpmsg_device *rpdev;
184 struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS];
187 struct list_head users;
188 struct miscdevice miscdev;
191 struct fastrpc_user {
192 struct list_head user;
193 struct list_head maps;
194 struct list_head pending;
196 struct fastrpc_channel_ctx *cctx;
197 struct fastrpc_session_ctx *sctx;
198 struct fastrpc_buf *init_mem;
204 /* lock for allocations */
208 static void fastrpc_free_map(struct kref *ref)
210 struct fastrpc_map *map;
212 map = container_of(ref, struct fastrpc_map, refcount);
215 dma_buf_unmap_attachment(map->attach, map->table,
217 dma_buf_detach(map->buf, map->attach);
218 dma_buf_put(map->buf);
224 static void fastrpc_map_put(struct fastrpc_map *map)
227 kref_put(&map->refcount, fastrpc_free_map);
230 static void fastrpc_map_get(struct fastrpc_map *map)
233 kref_get(&map->refcount);
236 static int fastrpc_map_find(struct fastrpc_user *fl, int fd,
237 struct fastrpc_map **ppmap)
239 struct fastrpc_map *map = NULL;
241 mutex_lock(&fl->mutex);
242 list_for_each_entry(map, &fl->maps, node) {
244 fastrpc_map_get(map);
246 mutex_unlock(&fl->mutex);
250 mutex_unlock(&fl->mutex);
255 static void fastrpc_buf_free(struct fastrpc_buf *buf)
257 dma_free_coherent(buf->dev, buf->size, buf->virt,
258 FASTRPC_PHYS(buf->phys));
262 static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
263 u64 size, struct fastrpc_buf **obuf)
265 struct fastrpc_buf *buf;
267 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
271 INIT_LIST_HEAD(&buf->attachments);
272 mutex_init(&buf->lock);
280 buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys,
285 if (fl->sctx && fl->sctx->sid)
286 buf->phys += ((u64)fl->sctx->sid << 32);
293 static void fastrpc_context_free(struct kref *ref)
295 struct fastrpc_invoke_ctx *ctx;
296 struct fastrpc_channel_ctx *cctx;
300 ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
303 for (i = 0; i < ctx->nscalars; i++)
304 fastrpc_map_put(ctx->maps[i]);
307 fastrpc_buf_free(ctx->buf);
309 spin_lock_irqsave(&cctx->lock, flags);
310 idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4);
311 spin_unlock_irqrestore(&cctx->lock, flags);
318 static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx)
320 kref_get(&ctx->refcount);
323 static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx)
325 kref_put(&ctx->refcount, fastrpc_context_free);
328 static void fastrpc_context_put_wq(struct work_struct *work)
330 struct fastrpc_invoke_ctx *ctx =
331 container_of(work, struct fastrpc_invoke_ctx, put_work);
333 fastrpc_context_put(ctx);
336 #define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)
337 static int olaps_cmp(const void *a, const void *b)
339 struct fastrpc_buf_overlap *pa = (struct fastrpc_buf_overlap *)a;
340 struct fastrpc_buf_overlap *pb = (struct fastrpc_buf_overlap *)b;
341 /* sort with lowest starting buffer first */
342 int st = CMP(pa->start, pb->start);
343 /* sort with highest ending buffer first */
344 int ed = CMP(pb->end, pa->end);
346 return st == 0 ? ed : st;
349 static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
354 for (i = 0; i < ctx->nbufs; ++i) {
355 ctx->olaps[i].start = ctx->args[i].ptr;
356 ctx->olaps[i].end = ctx->olaps[i].start + ctx->args[i].length;
357 ctx->olaps[i].raix = i;
360 sort(ctx->olaps, ctx->nbufs, sizeof(*ctx->olaps), olaps_cmp, NULL);
362 for (i = 0; i < ctx->nbufs; ++i) {
363 /* Falling inside previous range */
364 if (ctx->olaps[i].start < max_end) {
365 ctx->olaps[i].mstart = max_end;
366 ctx->olaps[i].mend = ctx->olaps[i].end;
367 ctx->olaps[i].offset = max_end - ctx->olaps[i].start;
369 if (ctx->olaps[i].end > max_end) {
370 max_end = ctx->olaps[i].end;
372 ctx->olaps[i].mend = 0;
373 ctx->olaps[i].mstart = 0;
377 ctx->olaps[i].mend = ctx->olaps[i].end;
378 ctx->olaps[i].mstart = ctx->olaps[i].start;
379 ctx->olaps[i].offset = 0;
380 max_end = ctx->olaps[i].end;
385 static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
386 struct fastrpc_user *user, u32 kernel, u32 sc,
387 struct fastrpc_invoke_args *args)
389 struct fastrpc_channel_ctx *cctx = user->cctx;
390 struct fastrpc_invoke_ctx *ctx = NULL;
394 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
396 return ERR_PTR(-ENOMEM);
398 INIT_LIST_HEAD(&ctx->node);
400 ctx->nscalars = REMOTE_SCALARS_LENGTH(sc);
401 ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) +
402 REMOTE_SCALARS_OUTBUFS(sc);
405 ctx->maps = kcalloc(ctx->nscalars,
406 sizeof(*ctx->maps), GFP_KERNEL);
409 return ERR_PTR(-ENOMEM);
411 ctx->olaps = kcalloc(ctx->nscalars,
412 sizeof(*ctx->olaps), GFP_KERNEL);
416 return ERR_PTR(-ENOMEM);
419 fastrpc_get_buff_overlaps(ctx);
424 ctx->pid = current->pid;
425 ctx->tgid = user->tgid;
427 init_completion(&ctx->work);
428 INIT_WORK(&ctx->put_work, fastrpc_context_put_wq);
430 spin_lock(&user->lock);
431 list_add_tail(&ctx->node, &user->pending);
432 spin_unlock(&user->lock);
434 spin_lock_irqsave(&cctx->lock, flags);
435 ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1,
436 FASTRPC_CTX_MAX, GFP_ATOMIC);
438 spin_unlock_irqrestore(&cctx->lock, flags);
441 ctx->ctxid = ret << 4;
442 spin_unlock_irqrestore(&cctx->lock, flags);
444 kref_init(&ctx->refcount);
448 spin_lock(&user->lock);
449 list_del(&ctx->node);
450 spin_unlock(&user->lock);
458 static struct sg_table *
459 fastrpc_map_dma_buf(struct dma_buf_attachment *attachment,
460 enum dma_data_direction dir)
462 struct fastrpc_dma_buf_attachment *a = attachment->priv;
463 struct sg_table *table;
467 if (!dma_map_sg(attachment->dev, table->sgl, table->nents, dir))
468 return ERR_PTR(-ENOMEM);
473 static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach,
474 struct sg_table *table,
475 enum dma_data_direction dir)
477 dma_unmap_sg(attach->dev, table->sgl, table->nents, dir);
480 static void fastrpc_release(struct dma_buf *dmabuf)
482 struct fastrpc_buf *buffer = dmabuf->priv;
484 fastrpc_buf_free(buffer);
487 static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf,
488 struct dma_buf_attachment *attachment)
490 struct fastrpc_dma_buf_attachment *a;
491 struct fastrpc_buf *buffer = dmabuf->priv;
494 a = kzalloc(sizeof(*a), GFP_KERNEL);
498 ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt,
499 FASTRPC_PHYS(buffer->phys), buffer->size);
501 dev_err(buffer->dev, "failed to get scatterlist from DMA API\n");
505 a->dev = attachment->dev;
506 INIT_LIST_HEAD(&a->node);
507 attachment->priv = a;
509 mutex_lock(&buffer->lock);
510 list_add(&a->node, &buffer->attachments);
511 mutex_unlock(&buffer->lock);
516 static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf,
517 struct dma_buf_attachment *attachment)
519 struct fastrpc_dma_buf_attachment *a = attachment->priv;
520 struct fastrpc_buf *buffer = dmabuf->priv;
522 mutex_lock(&buffer->lock);
524 mutex_unlock(&buffer->lock);
528 static void *fastrpc_kmap(struct dma_buf *dmabuf, unsigned long pgnum)
530 struct fastrpc_buf *buf = dmabuf->priv;
532 return buf->virt ? buf->virt + pgnum * PAGE_SIZE : NULL;
535 static void *fastrpc_vmap(struct dma_buf *dmabuf)
537 struct fastrpc_buf *buf = dmabuf->priv;
542 static int fastrpc_mmap(struct dma_buf *dmabuf,
543 struct vm_area_struct *vma)
545 struct fastrpc_buf *buf = dmabuf->priv;
546 size_t size = vma->vm_end - vma->vm_start;
548 return dma_mmap_coherent(buf->dev, vma, buf->virt,
549 FASTRPC_PHYS(buf->phys), size);
552 static const struct dma_buf_ops fastrpc_dma_buf_ops = {
553 .attach = fastrpc_dma_buf_attach,
554 .detach = fastrpc_dma_buf_detatch,
555 .map_dma_buf = fastrpc_map_dma_buf,
556 .unmap_dma_buf = fastrpc_unmap_dma_buf,
557 .mmap = fastrpc_mmap,
559 .vmap = fastrpc_vmap,
560 .release = fastrpc_release,
563 static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
564 u64 len, struct fastrpc_map **ppmap)
566 struct fastrpc_session_ctx *sess = fl->sctx;
567 struct fastrpc_map *map = NULL;
570 if (!fastrpc_map_find(fl, fd, ppmap))
573 map = kzalloc(sizeof(*map), GFP_KERNEL);
577 INIT_LIST_HEAD(&map->node);
580 map->buf = dma_buf_get(fd);
581 if (IS_ERR(map->buf)) {
582 err = PTR_ERR(map->buf);
586 map->attach = dma_buf_attach(map->buf, sess->dev);
587 if (IS_ERR(map->attach)) {
588 dev_err(sess->dev, "Failed to attach dmabuf\n");
589 err = PTR_ERR(map->attach);
593 map->table = dma_buf_map_attachment(map->attach, DMA_BIDIRECTIONAL);
594 if (IS_ERR(map->table)) {
595 err = PTR_ERR(map->table);
599 map->phys = sg_dma_address(map->table->sgl);
600 map->phys += ((u64)fl->sctx->sid << 32);
602 map->va = sg_virt(map->table->sgl);
604 kref_init(&map->refcount);
606 spin_lock(&fl->lock);
607 list_add_tail(&map->node, &fl->maps);
608 spin_unlock(&fl->lock);
614 dma_buf_detach(map->buf, map->attach);
616 dma_buf_put(map->buf);
624 * Fastrpc payload buffer with metadata looks like:
626 * >>>>>> START of METADATA <<<<<<<<<
627 * +---------------------------------+
629 * | type:(struct fastrpc_remote_arg)|
631 * +---------------------------------+
632 * | Invoke Buffer list |
633 * | type:(struct fastrpc_invoke_buf)|
635 * +---------------------------------+
637 * | type:(struct fastrpc_phy_page) |
639 * +---------------------------------+
641 * |(can be specific to SoC/Firmware)|
642 * +---------------------------------+
643 * >>>>>>>> END of METADATA <<<<<<<<<
644 * +---------------------------------+
647 * +---------------------------------+
650 static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx)
654 size = (sizeof(struct fastrpc_remote_arg) +
655 sizeof(struct fastrpc_invoke_buf) +
656 sizeof(struct fastrpc_phy_page)) * ctx->nscalars +
657 sizeof(u64) * FASTRPC_MAX_FDLIST +
658 sizeof(u32) * FASTRPC_MAX_CRCLIST;
663 static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen)
668 size = ALIGN(metalen, FASTRPC_ALIGN);
669 for (i = 0; i < ctx->nscalars; i++) {
670 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) {
672 if (ctx->olaps[i].offset == 0)
673 size = ALIGN(size, FASTRPC_ALIGN);
675 size += (ctx->olaps[i].mend - ctx->olaps[i].mstart);
682 static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
684 struct device *dev = ctx->fl->sctx->dev;
687 for (i = 0; i < ctx->nscalars; ++i) {
688 /* Make sure reserved field is set to 0 */
689 if (ctx->args[i].reserved)
692 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 ||
693 ctx->args[i].length == 0)
696 err = fastrpc_map_create(ctx->fl, ctx->args[i].fd,
697 ctx->args[i].length, &ctx->maps[i]);
699 dev_err(dev, "Error Creating map %d\n", err);
707 static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
709 struct device *dev = ctx->fl->sctx->dev;
710 struct fastrpc_remote_arg *rpra;
711 struct fastrpc_invoke_buf *list;
712 struct fastrpc_phy_page *pages;
713 int inbufs, i, oix, err = 0;
714 u64 len, rlen, pkt_size;
715 u64 pg_start, pg_end;
719 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
720 metalen = fastrpc_get_meta_size(ctx);
721 pkt_size = fastrpc_get_payload_size(ctx, metalen);
723 err = fastrpc_create_maps(ctx);
727 ctx->msg_sz = pkt_size;
729 err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf);
733 rpra = ctx->buf->virt;
734 list = ctx->buf->virt + ctx->nscalars * sizeof(*rpra);
735 pages = ctx->buf->virt + ctx->nscalars * (sizeof(*list) +
737 args = (uintptr_t)ctx->buf->virt + metalen;
738 rlen = pkt_size - metalen;
741 for (oix = 0; oix < ctx->nbufs; ++oix) {
744 i = ctx->olaps[oix].raix;
745 len = ctx->args[i].length;
749 list[i].num = len ? 1 : 0;
756 struct vm_area_struct *vma = NULL;
758 rpra[i].pv = (u64) ctx->args[i].ptr;
759 pages[i].addr = ctx->maps[i]->phys;
761 vma = find_vma(current->mm, ctx->args[i].ptr);
763 pages[i].addr += ctx->args[i].ptr -
766 pg_start = (ctx->args[i].ptr & PAGE_MASK) >> PAGE_SHIFT;
767 pg_end = ((ctx->args[i].ptr + len - 1) & PAGE_MASK) >>
769 pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
773 if (ctx->olaps[oix].offset == 0) {
774 rlen -= ALIGN(args, FASTRPC_ALIGN) - args;
775 args = ALIGN(args, FASTRPC_ALIGN);
778 mlen = ctx->olaps[oix].mend - ctx->olaps[oix].mstart;
783 rpra[i].pv = args - ctx->olaps[oix].offset;
784 pages[i].addr = ctx->buf->phys -
785 ctx->olaps[oix].offset +
787 pages[i].addr = pages[i].addr & PAGE_MASK;
789 pg_start = (args & PAGE_MASK) >> PAGE_SHIFT;
790 pg_end = ((args + len - 1) & PAGE_MASK) >> PAGE_SHIFT;
791 pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
796 if (i < inbufs && !ctx->maps[i]) {
797 void *dst = (void *)(uintptr_t)rpra[i].pv;
798 void *src = (void *)(uintptr_t)ctx->args[i].ptr;
801 if (copy_from_user(dst, (void __user *)src,
807 memcpy(dst, src, len);
812 for (i = ctx->nbufs; i < ctx->nscalars; ++i) {
813 rpra[i].pv = (u64) ctx->args[i].ptr;
814 rpra[i].len = ctx->args[i].length;
815 list[i].num = ctx->args[i].length ? 1 : 0;
817 pages[i].addr = ctx->maps[i]->phys;
818 pages[i].size = ctx->maps[i]->size;
823 dev_err(dev, "Error: get invoke args failed:%d\n", err);
828 static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
831 struct fastrpc_remote_arg *rpra = ctx->rpra;
834 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
836 for (i = inbufs; i < ctx->nbufs; ++i) {
837 void *src = (void *)(uintptr_t)rpra[i].pv;
838 void *dst = (void *)(uintptr_t)ctx->args[i].ptr;
839 u64 len = rpra[i].len;
842 if (copy_to_user((void __user *)dst, src, len))
845 memcpy(dst, src, len);
852 static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx,
853 struct fastrpc_invoke_ctx *ctx,
854 u32 kernel, uint32_t handle)
856 struct fastrpc_channel_ctx *cctx;
857 struct fastrpc_user *fl = ctx->fl;
858 struct fastrpc_msg *msg = &ctx->msg;
862 msg->tid = current->pid;
867 msg->ctx = ctx->ctxid | fl->pd;
868 msg->handle = handle;
870 msg->addr = ctx->buf ? ctx->buf->phys : 0;
871 msg->size = roundup(ctx->msg_sz, PAGE_SIZE);
872 fastrpc_context_get(ctx);
874 return rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg));
877 static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
879 struct fastrpc_invoke_args *args)
881 struct fastrpc_invoke_ctx *ctx = NULL;
887 ctx = fastrpc_context_alloc(fl, kernel, sc, args);
892 err = fastrpc_get_args(kernel, ctx);
897 /* make sure that all CPU memory writes are seen by DSP */
899 /* Send invoke buffer to remote dsp */
900 err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle);
904 /* Wait for remote dsp to respond or time out */
905 err = wait_for_completion_interruptible(&ctx->work);
909 /* Check the response from remote dsp */
915 /* make sure that all memory writes by DSP are seen by CPU */
917 /* populate all the output buffers with results */
918 err = fastrpc_put_args(ctx, kernel);
924 /* We are done with this compute context, remove it from pending list */
925 spin_lock(&fl->lock);
926 list_del(&ctx->node);
927 spin_unlock(&fl->lock);
928 fastrpc_context_put(ctx);
931 dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err);
936 static int fastrpc_init_create_process(struct fastrpc_user *fl,
939 struct fastrpc_init_create init;
940 struct fastrpc_invoke_args *args;
941 struct fastrpc_phy_page pages[1];
942 struct fastrpc_map *map = NULL;
943 struct fastrpc_buf *imem = NULL;
956 args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL);
960 if (copy_from_user(&init, argp, sizeof(init))) {
965 if (init.filelen > INIT_FILELEN_MAX) {
970 inbuf.pgid = fl->tgid;
971 inbuf.namelen = strlen(current->comm) + 1;
972 inbuf.filelen = init.filelen;
974 inbuf.attrs = init.attrs;
975 inbuf.siglen = init.siglen;
978 if (init.filelen && init.filefd) {
979 err = fastrpc_map_create(fl, init.filefd, init.filelen, &map);
984 memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4),
986 err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen,
992 args[0].ptr = (u64)(uintptr_t)&inbuf;
993 args[0].length = sizeof(inbuf);
996 args[1].ptr = (u64)(uintptr_t)current->comm;
997 args[1].length = inbuf.namelen;
1000 args[2].ptr = (u64) init.file;
1001 args[2].length = inbuf.filelen;
1002 args[2].fd = init.filefd;
1004 pages[0].addr = imem->phys;
1005 pages[0].size = imem->size;
1007 args[3].ptr = (u64)(uintptr_t) pages;
1008 args[3].length = 1 * sizeof(*pages);
1011 args[4].ptr = (u64)(uintptr_t)&inbuf.attrs;
1012 args[4].length = sizeof(inbuf.attrs);
1015 args[5].ptr = (u64)(uintptr_t) &inbuf.siglen;
1016 args[5].length = sizeof(inbuf.siglen);
1019 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0);
1021 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0);
1023 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1033 fl->init_mem = NULL;
1034 fastrpc_buf_free(imem);
1037 spin_lock(&fl->lock);
1038 list_del(&map->node);
1039 spin_unlock(&fl->lock);
1040 fastrpc_map_put(map);
1048 static struct fastrpc_session_ctx *fastrpc_session_alloc(
1049 struct fastrpc_channel_ctx *cctx)
1051 struct fastrpc_session_ctx *session = NULL;
1052 unsigned long flags;
1055 spin_lock_irqsave(&cctx->lock, flags);
1056 for (i = 0; i < cctx->sesscount; i++) {
1057 if (!cctx->session[i].used && cctx->session[i].valid) {
1058 cctx->session[i].used = true;
1059 session = &cctx->session[i];
1063 spin_unlock_irqrestore(&cctx->lock, flags);
1068 static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx,
1069 struct fastrpc_session_ctx *session)
1071 unsigned long flags;
1073 spin_lock_irqsave(&cctx->lock, flags);
1074 session->used = false;
1075 spin_unlock_irqrestore(&cctx->lock, flags);
1078 static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl)
1080 struct fastrpc_invoke_args args[1];
1085 args[0].ptr = (u64)(uintptr_t) &tgid;
1086 args[0].length = sizeof(tgid);
1088 args[0].reserved = 0;
1089 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0);
1091 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1095 static int fastrpc_device_release(struct inode *inode, struct file *file)
1097 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1098 struct fastrpc_channel_ctx *cctx = fl->cctx;
1099 struct fastrpc_invoke_ctx *ctx, *n;
1100 struct fastrpc_map *map, *m;
1101 unsigned long flags;
1103 fastrpc_release_current_dsp_process(fl);
1105 spin_lock_irqsave(&cctx->lock, flags);
1106 list_del(&fl->user);
1107 spin_unlock_irqrestore(&cctx->lock, flags);
1110 fastrpc_buf_free(fl->init_mem);
1112 list_for_each_entry_safe(ctx, n, &fl->pending, node) {
1113 list_del(&ctx->node);
1114 fastrpc_context_put(ctx);
1117 list_for_each_entry_safe(map, m, &fl->maps, node) {
1118 list_del(&map->node);
1119 fastrpc_map_put(map);
1122 fastrpc_session_free(cctx, fl->sctx);
1124 mutex_destroy(&fl->mutex);
1126 file->private_data = NULL;
1131 static int fastrpc_device_open(struct inode *inode, struct file *filp)
1133 struct fastrpc_channel_ctx *cctx = miscdev_to_cctx(filp->private_data);
1134 struct fastrpc_user *fl = NULL;
1135 unsigned long flags;
1137 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
1141 filp->private_data = fl;
1142 spin_lock_init(&fl->lock);
1143 mutex_init(&fl->mutex);
1144 INIT_LIST_HEAD(&fl->pending);
1145 INIT_LIST_HEAD(&fl->maps);
1146 INIT_LIST_HEAD(&fl->user);
1147 fl->tgid = current->tgid;
1150 fl->sctx = fastrpc_session_alloc(cctx);
1152 dev_err(&cctx->rpdev->dev, "No session available\n");
1153 mutex_destroy(&fl->mutex);
1159 spin_lock_irqsave(&cctx->lock, flags);
1160 list_add_tail(&fl->user, &cctx->users);
1161 spin_unlock_irqrestore(&cctx->lock, flags);
1166 static int fastrpc_dmabuf_free(struct fastrpc_user *fl, char __user *argp)
1168 struct dma_buf *buf;
1171 if (copy_from_user(&info, argp, sizeof(info)))
1174 buf = dma_buf_get(info);
1175 if (IS_ERR_OR_NULL(buf))
1178 * one for the last get and other for the ALLOC_DMA_BUFF ioctl
1186 static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
1188 struct fastrpc_alloc_dma_buf bp;
1189 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
1190 struct fastrpc_buf *buf = NULL;
1193 if (copy_from_user(&bp, argp, sizeof(bp)))
1196 err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf);
1199 exp_info.ops = &fastrpc_dma_buf_ops;
1200 exp_info.size = bp.size;
1201 exp_info.flags = O_RDWR;
1202 exp_info.priv = buf;
1203 buf->dmabuf = dma_buf_export(&exp_info);
1204 if (IS_ERR(buf->dmabuf)) {
1205 err = PTR_ERR(buf->dmabuf);
1206 fastrpc_buf_free(buf);
1210 bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE);
1212 dma_buf_put(buf->dmabuf);
1216 if (copy_to_user(argp, &bp, sizeof(bp))) {
1217 dma_buf_put(buf->dmabuf);
1221 get_dma_buf(buf->dmabuf);
1226 static int fastrpc_init_attach(struct fastrpc_user *fl)
1228 struct fastrpc_invoke_args args[1];
1229 int tgid = fl->tgid;
1232 args[0].ptr = (u64)(uintptr_t) &tgid;
1233 args[0].length = sizeof(tgid);
1235 args[0].reserved = 0;
1236 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0);
1239 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1243 static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp)
1245 struct fastrpc_invoke_args *args = NULL;
1246 struct fastrpc_invoke inv;
1250 if (copy_from_user(&inv, argp, sizeof(inv)))
1253 /* nscalars is truncated here to max supported value */
1254 nscalars = REMOTE_SCALARS_LENGTH(inv.sc);
1256 args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL);
1260 if (copy_from_user(args, (void __user *)(uintptr_t)inv.args,
1261 nscalars * sizeof(*args))) {
1267 err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args);
1273 static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
1276 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1277 char __user *argp = (char __user *)arg;
1281 case FASTRPC_IOCTL_INVOKE:
1282 err = fastrpc_invoke(fl, argp);
1284 case FASTRPC_IOCTL_INIT_ATTACH:
1285 err = fastrpc_init_attach(fl);
1287 case FASTRPC_IOCTL_INIT_CREATE:
1288 err = fastrpc_init_create_process(fl, argp);
1290 case FASTRPC_IOCTL_FREE_DMA_BUFF:
1291 err = fastrpc_dmabuf_free(fl, argp);
1293 case FASTRPC_IOCTL_ALLOC_DMA_BUFF:
1294 err = fastrpc_dmabuf_alloc(fl, argp);
1304 static const struct file_operations fastrpc_fops = {
1305 .open = fastrpc_device_open,
1306 .release = fastrpc_device_release,
1307 .unlocked_ioctl = fastrpc_device_ioctl,
1308 .compat_ioctl = fastrpc_device_ioctl,
1311 static int fastrpc_cb_probe(struct platform_device *pdev)
1313 struct fastrpc_channel_ctx *cctx;
1314 struct fastrpc_session_ctx *sess;
1315 struct device *dev = &pdev->dev;
1316 int i, sessions = 0;
1317 unsigned long flags;
1320 cctx = dev_get_drvdata(dev->parent);
1324 of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions);
1326 spin_lock_irqsave(&cctx->lock, flags);
1327 sess = &cctx->session[cctx->sesscount];
1331 dev_set_drvdata(dev, sess);
1333 if (of_property_read_u32(dev->of_node, "reg", &sess->sid))
1334 dev_info(dev, "FastRPC Session ID not specified in DT\n");
1337 struct fastrpc_session_ctx *dup_sess;
1339 for (i = 1; i < sessions; i++) {
1340 if (cctx->sesscount++ >= FASTRPC_MAX_SESSIONS)
1342 dup_sess = &cctx->session[cctx->sesscount];
1343 memcpy(dup_sess, sess, sizeof(*dup_sess));
1347 spin_unlock_irqrestore(&cctx->lock, flags);
1348 rc = dma_set_mask(dev, DMA_BIT_MASK(32));
1350 dev_err(dev, "32-bit DMA enable failed\n");
1357 static int fastrpc_cb_remove(struct platform_device *pdev)
1359 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent);
1360 struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev);
1361 unsigned long flags;
1364 spin_lock_irqsave(&cctx->lock, flags);
1365 for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) {
1366 if (cctx->session[i].sid == sess->sid) {
1367 cctx->session[i].valid = false;
1371 spin_unlock_irqrestore(&cctx->lock, flags);
1376 static const struct of_device_id fastrpc_match_table[] = {
1377 { .compatible = "qcom,fastrpc-compute-cb", },
1381 static struct platform_driver fastrpc_cb_driver = {
1382 .probe = fastrpc_cb_probe,
1383 .remove = fastrpc_cb_remove,
1385 .name = "qcom,fastrpc-cb",
1386 .of_match_table = fastrpc_match_table,
1387 .suppress_bind_attrs = true,
1391 static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
1393 struct device *rdev = &rpdev->dev;
1394 struct fastrpc_channel_ctx *data;
1395 int i, err, domain_id = -1;
1398 data = devm_kzalloc(rdev, sizeof(*data), GFP_KERNEL);
1402 err = of_property_read_string(rdev->of_node, "label", &domain);
1404 dev_info(rdev, "FastRPC Domain not specified in DT\n");
1408 for (i = 0; i <= CDSP_DOMAIN_ID; i++) {
1409 if (!strcmp(domains[i], domain)) {
1415 if (domain_id < 0) {
1416 dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id);
1420 data->miscdev.minor = MISC_DYNAMIC_MINOR;
1421 data->miscdev.name = kasprintf(GFP_KERNEL, "fastrpc-%s",
1422 domains[domain_id]);
1423 data->miscdev.fops = &fastrpc_fops;
1424 err = misc_register(&data->miscdev);
1428 dev_set_drvdata(&rpdev->dev, data);
1429 dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
1430 INIT_LIST_HEAD(&data->users);
1431 spin_lock_init(&data->lock);
1432 idr_init(&data->ctx_idr);
1433 data->domain_id = domain_id;
1434 data->rpdev = rpdev;
1436 return of_platform_populate(rdev->of_node, NULL, NULL, rdev);
1439 static void fastrpc_notify_users(struct fastrpc_user *user)
1441 struct fastrpc_invoke_ctx *ctx;
1443 spin_lock(&user->lock);
1444 list_for_each_entry(ctx, &user->pending, node)
1445 complete(&ctx->work);
1446 spin_unlock(&user->lock);
1449 static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
1451 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
1452 struct fastrpc_user *user;
1453 unsigned long flags;
1455 spin_lock_irqsave(&cctx->lock, flags);
1456 list_for_each_entry(user, &cctx->users, user)
1457 fastrpc_notify_users(user);
1458 spin_unlock_irqrestore(&cctx->lock, flags);
1460 misc_deregister(&cctx->miscdev);
1461 of_platform_depopulate(&rpdev->dev);
1465 static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
1466 int len, void *priv, u32 addr)
1468 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
1469 struct fastrpc_invoke_rsp *rsp = data;
1470 struct fastrpc_invoke_ctx *ctx;
1471 unsigned long flags;
1472 unsigned long ctxid;
1474 if (len < sizeof(*rsp))
1477 ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4);
1479 spin_lock_irqsave(&cctx->lock, flags);
1480 ctx = idr_find(&cctx->ctx_idr, ctxid);
1481 spin_unlock_irqrestore(&cctx->lock, flags);
1484 dev_err(&rpdev->dev, "No context ID matches response\n");
1488 ctx->retval = rsp->retval;
1489 complete(&ctx->work);
1492 * The DMA buffer associated with the context cannot be freed in
1493 * interrupt context so schedule it through a worker thread to
1494 * avoid a kernel BUG.
1496 schedule_work(&ctx->put_work);
1501 static const struct of_device_id fastrpc_rpmsg_of_match[] = {
1502 { .compatible = "qcom,fastrpc" },
1505 MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match);
1507 static struct rpmsg_driver fastrpc_driver = {
1508 .probe = fastrpc_rpmsg_probe,
1509 .remove = fastrpc_rpmsg_remove,
1510 .callback = fastrpc_rpmsg_callback,
1512 .name = "qcom,fastrpc",
1513 .of_match_table = fastrpc_rpmsg_of_match,
1517 static int fastrpc_init(void)
1521 ret = platform_driver_register(&fastrpc_cb_driver);
1523 pr_err("fastrpc: failed to register cb driver\n");
1527 ret = register_rpmsg_driver(&fastrpc_driver);
1529 pr_err("fastrpc: failed to register rpmsg driver\n");
1530 platform_driver_unregister(&fastrpc_cb_driver);
1536 module_init(fastrpc_init);
1538 static void fastrpc_exit(void)
1540 platform_driver_unregister(&fastrpc_cb_driver);
1541 unregister_rpmsg_driver(&fastrpc_driver);
1543 module_exit(fastrpc_exit);
1545 MODULE_LICENSE("GPL v2");