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 FASTRPC_DEVICE_NAME "fastrpc"
38 /* Retrives number of input buffers from the scalars parameter */
39 #define REMOTE_SCALARS_INBUFS(sc) (((sc) >> 16) & 0x0ff)
41 /* Retrives number of output buffers from the scalars parameter */
42 #define REMOTE_SCALARS_OUTBUFS(sc) (((sc) >> 8) & 0x0ff)
44 /* Retrives number of input handles from the scalars parameter */
45 #define REMOTE_SCALARS_INHANDLES(sc) (((sc) >> 4) & 0x0f)
47 /* Retrives number of output handles from the scalars parameter */
48 #define REMOTE_SCALARS_OUTHANDLES(sc) ((sc) & 0x0f)
50 #define REMOTE_SCALARS_LENGTH(sc) (REMOTE_SCALARS_INBUFS(sc) + \
51 REMOTE_SCALARS_OUTBUFS(sc) + \
52 REMOTE_SCALARS_INHANDLES(sc)+ \
53 REMOTE_SCALARS_OUTHANDLES(sc))
54 #define FASTRPC_BUILD_SCALARS(attr, method, in, out, oin, oout) \
55 (((attr & 0x07) << 29) | \
56 ((method & 0x1f) << 24) | \
57 ((in & 0xff) << 16) | \
58 ((out & 0xff) << 8) | \
59 ((oin & 0x0f) << 4) | \
62 #define FASTRPC_SCALARS(method, in, out) \
63 FASTRPC_BUILD_SCALARS(0, method, in, out, 0, 0)
65 #define FASTRPC_CREATE_PROCESS_NARGS 6
66 /* Remote Method id table */
67 #define FASTRPC_RMID_INIT_ATTACH 0
68 #define FASTRPC_RMID_INIT_RELEASE 1
69 #define FASTRPC_RMID_INIT_CREATE 6
70 #define FASTRPC_RMID_INIT_CREATE_ATTR 7
71 #define FASTRPC_RMID_INIT_CREATE_STATIC 8
73 #define miscdev_to_cctx(d) container_of(d, struct fastrpc_channel_ctx, miscdev)
75 static const char *domains[FASTRPC_DEV_MAX] = { "adsp", "mdsp",
77 struct fastrpc_phy_page {
78 u64 addr; /* physical address */
79 u64 size; /* size of contiguous region */
82 struct fastrpc_invoke_buf {
83 u32 num; /* number of contiguous regions */
84 u32 pgidx; /* index to start of contiguous region */
87 struct fastrpc_remote_arg {
93 int pid; /* process group id */
94 int tid; /* thread id */
95 u64 ctx; /* invoke caller context */
96 u32 handle; /* handle to invoke */
97 u32 sc; /* scalars structure describing the data */
98 u64 addr; /* physical address */
99 u64 size; /* size of contiguous region */
102 struct fastrpc_invoke_rsp {
103 u64 ctx; /* invoke caller context */
104 int retval; /* invoke return value */
107 struct fastrpc_buf_overlap {
117 struct fastrpc_user *fl;
118 struct dma_buf *dmabuf;
123 /* Lock for dma buf attachments */
125 struct list_head attachments;
128 struct fastrpc_dma_buf_attachment {
131 struct list_head node;
135 struct list_head node;
136 struct fastrpc_user *fl;
139 struct sg_table *table;
140 struct dma_buf_attachment *attach;
145 struct kref refcount;
148 struct fastrpc_invoke_ctx {
158 struct kref refcount;
159 struct list_head node; /* list of ctxs */
160 struct completion work;
161 struct work_struct put_work;
162 struct fastrpc_msg msg;
163 struct fastrpc_user *fl;
164 struct fastrpc_remote_arg *rpra;
165 struct fastrpc_map **maps;
166 struct fastrpc_buf *buf;
167 struct fastrpc_invoke_args *args;
168 struct fastrpc_buf_overlap *olaps;
169 struct fastrpc_channel_ctx *cctx;
172 struct fastrpc_session_ctx {
179 struct fastrpc_channel_ctx {
182 struct rpmsg_device *rpdev;
183 struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS];
186 struct list_head users;
187 struct miscdevice miscdev;
188 struct kref refcount;
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);
222 spin_lock(&map->fl->lock);
223 list_del(&map->node);
224 spin_unlock(&map->fl->lock);
231 static void fastrpc_map_put(struct fastrpc_map *map)
234 kref_put(&map->refcount, fastrpc_free_map);
237 static int fastrpc_map_get(struct fastrpc_map *map)
242 return kref_get_unless_zero(&map->refcount) ? 0 : -ENOENT;
245 static int fastrpc_map_find(struct fastrpc_user *fl, int fd,
246 struct fastrpc_map **ppmap)
248 struct fastrpc_map *map = NULL;
250 mutex_lock(&fl->mutex);
251 list_for_each_entry(map, &fl->maps, node) {
253 fastrpc_map_get(map);
255 mutex_unlock(&fl->mutex);
259 mutex_unlock(&fl->mutex);
264 static void fastrpc_buf_free(struct fastrpc_buf *buf)
266 dma_free_coherent(buf->dev, buf->size, buf->virt,
267 FASTRPC_PHYS(buf->phys));
271 static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
272 u64 size, struct fastrpc_buf **obuf)
274 struct fastrpc_buf *buf;
276 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
280 INIT_LIST_HEAD(&buf->attachments);
281 mutex_init(&buf->lock);
289 buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys,
292 mutex_destroy(&buf->lock);
297 if (fl->sctx && fl->sctx->sid)
298 buf->phys += ((u64)fl->sctx->sid << 32);
305 static void fastrpc_channel_ctx_free(struct kref *ref)
307 struct fastrpc_channel_ctx *cctx;
309 cctx = container_of(ref, struct fastrpc_channel_ctx, refcount);
314 static void fastrpc_channel_ctx_get(struct fastrpc_channel_ctx *cctx)
316 kref_get(&cctx->refcount);
319 static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx)
321 kref_put(&cctx->refcount, fastrpc_channel_ctx_free);
324 static void fastrpc_context_free(struct kref *ref)
326 struct fastrpc_invoke_ctx *ctx;
327 struct fastrpc_channel_ctx *cctx;
331 ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
334 for (i = 0; i < ctx->nscalars; i++)
335 fastrpc_map_put(ctx->maps[i]);
338 fastrpc_buf_free(ctx->buf);
340 spin_lock_irqsave(&cctx->lock, flags);
341 idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4);
342 spin_unlock_irqrestore(&cctx->lock, flags);
348 fastrpc_channel_ctx_put(cctx);
351 static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx)
353 kref_get(&ctx->refcount);
356 static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx)
358 kref_put(&ctx->refcount, fastrpc_context_free);
361 static void fastrpc_context_put_wq(struct work_struct *work)
363 struct fastrpc_invoke_ctx *ctx =
364 container_of(work, struct fastrpc_invoke_ctx, put_work);
366 fastrpc_context_put(ctx);
369 #define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)
370 static int olaps_cmp(const void *a, const void *b)
372 struct fastrpc_buf_overlap *pa = (struct fastrpc_buf_overlap *)a;
373 struct fastrpc_buf_overlap *pb = (struct fastrpc_buf_overlap *)b;
374 /* sort with lowest starting buffer first */
375 int st = CMP(pa->start, pb->start);
376 /* sort with highest ending buffer first */
377 int ed = CMP(pb->end, pa->end);
379 return st == 0 ? ed : st;
382 static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
387 for (i = 0; i < ctx->nbufs; ++i) {
388 ctx->olaps[i].start = ctx->args[i].ptr;
389 ctx->olaps[i].end = ctx->olaps[i].start + ctx->args[i].length;
390 ctx->olaps[i].raix = i;
393 sort(ctx->olaps, ctx->nbufs, sizeof(*ctx->olaps), olaps_cmp, NULL);
395 for (i = 0; i < ctx->nbufs; ++i) {
396 /* Falling inside previous range */
397 if (ctx->olaps[i].start < max_end) {
398 ctx->olaps[i].mstart = max_end;
399 ctx->olaps[i].mend = ctx->olaps[i].end;
400 ctx->olaps[i].offset = max_end - ctx->olaps[i].start;
402 if (ctx->olaps[i].end > max_end) {
403 max_end = ctx->olaps[i].end;
405 ctx->olaps[i].mend = 0;
406 ctx->olaps[i].mstart = 0;
410 ctx->olaps[i].mend = ctx->olaps[i].end;
411 ctx->olaps[i].mstart = ctx->olaps[i].start;
412 ctx->olaps[i].offset = 0;
413 max_end = ctx->olaps[i].end;
418 static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
419 struct fastrpc_user *user, u32 kernel, u32 sc,
420 struct fastrpc_invoke_args *args)
422 struct fastrpc_channel_ctx *cctx = user->cctx;
423 struct fastrpc_invoke_ctx *ctx = NULL;
427 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
429 return ERR_PTR(-ENOMEM);
431 INIT_LIST_HEAD(&ctx->node);
433 ctx->nscalars = REMOTE_SCALARS_LENGTH(sc);
434 ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) +
435 REMOTE_SCALARS_OUTBUFS(sc);
438 ctx->maps = kcalloc(ctx->nscalars,
439 sizeof(*ctx->maps), GFP_KERNEL);
442 return ERR_PTR(-ENOMEM);
444 ctx->olaps = kcalloc(ctx->nscalars,
445 sizeof(*ctx->olaps), GFP_KERNEL);
449 return ERR_PTR(-ENOMEM);
452 fastrpc_get_buff_overlaps(ctx);
455 /* Released in fastrpc_context_put() */
456 fastrpc_channel_ctx_get(cctx);
460 ctx->pid = current->pid;
461 ctx->tgid = user->tgid;
463 init_completion(&ctx->work);
464 INIT_WORK(&ctx->put_work, fastrpc_context_put_wq);
466 spin_lock(&user->lock);
467 list_add_tail(&ctx->node, &user->pending);
468 spin_unlock(&user->lock);
470 spin_lock_irqsave(&cctx->lock, flags);
471 ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1,
472 FASTRPC_CTX_MAX, GFP_ATOMIC);
474 spin_unlock_irqrestore(&cctx->lock, flags);
477 ctx->ctxid = ret << 4;
478 spin_unlock_irqrestore(&cctx->lock, flags);
480 kref_init(&ctx->refcount);
484 spin_lock(&user->lock);
485 list_del(&ctx->node);
486 spin_unlock(&user->lock);
487 fastrpc_channel_ctx_put(cctx);
495 static struct sg_table *
496 fastrpc_map_dma_buf(struct dma_buf_attachment *attachment,
497 enum dma_data_direction dir)
499 struct fastrpc_dma_buf_attachment *a = attachment->priv;
500 struct sg_table *table;
504 if (!dma_map_sg(attachment->dev, table->sgl, table->nents, dir))
505 return ERR_PTR(-ENOMEM);
510 static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach,
511 struct sg_table *table,
512 enum dma_data_direction dir)
514 dma_unmap_sg(attach->dev, table->sgl, table->nents, dir);
517 static void fastrpc_release(struct dma_buf *dmabuf)
519 struct fastrpc_buf *buffer = dmabuf->priv;
521 fastrpc_buf_free(buffer);
524 static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf,
525 struct dma_buf_attachment *attachment)
527 struct fastrpc_dma_buf_attachment *a;
528 struct fastrpc_buf *buffer = dmabuf->priv;
531 a = kzalloc(sizeof(*a), GFP_KERNEL);
535 ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt,
536 FASTRPC_PHYS(buffer->phys), buffer->size);
538 dev_err(buffer->dev, "failed to get scatterlist from DMA API\n");
543 a->dev = attachment->dev;
544 INIT_LIST_HEAD(&a->node);
545 attachment->priv = a;
547 mutex_lock(&buffer->lock);
548 list_add(&a->node, &buffer->attachments);
549 mutex_unlock(&buffer->lock);
554 static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf,
555 struct dma_buf_attachment *attachment)
557 struct fastrpc_dma_buf_attachment *a = attachment->priv;
558 struct fastrpc_buf *buffer = dmabuf->priv;
560 mutex_lock(&buffer->lock);
562 mutex_unlock(&buffer->lock);
563 sg_free_table(&a->sgt);
567 static void *fastrpc_kmap(struct dma_buf *dmabuf, unsigned long pgnum)
569 struct fastrpc_buf *buf = dmabuf->priv;
571 return buf->virt ? buf->virt + pgnum * PAGE_SIZE : NULL;
574 static void *fastrpc_vmap(struct dma_buf *dmabuf)
576 struct fastrpc_buf *buf = dmabuf->priv;
581 static int fastrpc_mmap(struct dma_buf *dmabuf,
582 struct vm_area_struct *vma)
584 struct fastrpc_buf *buf = dmabuf->priv;
585 size_t size = vma->vm_end - vma->vm_start;
587 return dma_mmap_coherent(buf->dev, vma, buf->virt,
588 FASTRPC_PHYS(buf->phys), size);
591 static const struct dma_buf_ops fastrpc_dma_buf_ops = {
592 .attach = fastrpc_dma_buf_attach,
593 .detach = fastrpc_dma_buf_detatch,
594 .map_dma_buf = fastrpc_map_dma_buf,
595 .unmap_dma_buf = fastrpc_unmap_dma_buf,
596 .mmap = fastrpc_mmap,
598 .vmap = fastrpc_vmap,
599 .release = fastrpc_release,
602 static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
603 u64 len, struct fastrpc_map **ppmap)
605 struct fastrpc_session_ctx *sess = fl->sctx;
606 struct fastrpc_map *map = NULL;
609 if (!fastrpc_map_find(fl, fd, ppmap))
612 map = kzalloc(sizeof(*map), GFP_KERNEL);
616 INIT_LIST_HEAD(&map->node);
619 map->buf = dma_buf_get(fd);
620 if (IS_ERR(map->buf)) {
621 err = PTR_ERR(map->buf);
625 map->attach = dma_buf_attach(map->buf, sess->dev);
626 if (IS_ERR(map->attach)) {
627 dev_err(sess->dev, "Failed to attach dmabuf\n");
628 err = PTR_ERR(map->attach);
632 map->table = dma_buf_map_attachment(map->attach, DMA_BIDIRECTIONAL);
633 if (IS_ERR(map->table)) {
634 err = PTR_ERR(map->table);
638 map->phys = sg_dma_address(map->table->sgl);
639 map->phys += ((u64)fl->sctx->sid << 32);
641 map->va = sg_virt(map->table->sgl);
643 kref_init(&map->refcount);
645 spin_lock(&fl->lock);
646 list_add_tail(&map->node, &fl->maps);
647 spin_unlock(&fl->lock);
653 dma_buf_detach(map->buf, map->attach);
655 dma_buf_put(map->buf);
663 * Fastrpc payload buffer with metadata looks like:
665 * >>>>>> START of METADATA <<<<<<<<<
666 * +---------------------------------+
668 * | type:(struct fastrpc_remote_arg)|
670 * +---------------------------------+
671 * | Invoke Buffer list |
672 * | type:(struct fastrpc_invoke_buf)|
674 * +---------------------------------+
676 * | type:(struct fastrpc_phy_page) |
678 * +---------------------------------+
680 * |(can be specific to SoC/Firmware)|
681 * +---------------------------------+
682 * >>>>>>>> END of METADATA <<<<<<<<<
683 * +---------------------------------+
686 * +---------------------------------+
689 static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx)
693 size = (sizeof(struct fastrpc_remote_arg) +
694 sizeof(struct fastrpc_invoke_buf) +
695 sizeof(struct fastrpc_phy_page)) * ctx->nscalars +
696 sizeof(u64) * FASTRPC_MAX_FDLIST +
697 sizeof(u32) * FASTRPC_MAX_CRCLIST;
702 static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen)
707 size = ALIGN(metalen, FASTRPC_ALIGN);
708 for (oix = 0; oix < ctx->nbufs; oix++) {
709 int i = ctx->olaps[oix].raix;
711 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) {
713 if (ctx->olaps[oix].offset == 0)
714 size = ALIGN(size, FASTRPC_ALIGN);
716 size += (ctx->olaps[oix].mend - ctx->olaps[oix].mstart);
723 static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
725 struct device *dev = ctx->fl->sctx->dev;
728 for (i = 0; i < ctx->nscalars; ++i) {
729 /* Make sure reserved field is set to 0 */
730 if (ctx->args[i].reserved)
733 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 ||
734 ctx->args[i].length == 0)
737 err = fastrpc_map_create(ctx->fl, ctx->args[i].fd,
738 ctx->args[i].length, &ctx->maps[i]);
740 dev_err(dev, "Error Creating map %d\n", err);
748 static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
750 struct device *dev = ctx->fl->sctx->dev;
751 struct fastrpc_remote_arg *rpra;
752 struct fastrpc_invoke_buf *list;
753 struct fastrpc_phy_page *pages;
754 int inbufs, i, oix, err = 0;
755 u64 len, rlen, pkt_size;
756 u64 pg_start, pg_end;
760 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
761 metalen = fastrpc_get_meta_size(ctx);
762 pkt_size = fastrpc_get_payload_size(ctx, metalen);
764 err = fastrpc_create_maps(ctx);
768 ctx->msg_sz = pkt_size;
770 err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf);
774 rpra = ctx->buf->virt;
775 list = ctx->buf->virt + ctx->nscalars * sizeof(*rpra);
776 pages = ctx->buf->virt + ctx->nscalars * (sizeof(*list) +
778 args = (uintptr_t)ctx->buf->virt + metalen;
779 rlen = pkt_size - metalen;
782 for (oix = 0; oix < ctx->nbufs; ++oix) {
785 i = ctx->olaps[oix].raix;
786 len = ctx->args[i].length;
790 list[i].num = len ? 1 : 0;
797 struct vm_area_struct *vma = NULL;
799 rpra[i].pv = (u64) ctx->args[i].ptr;
800 pages[i].addr = ctx->maps[i]->phys;
802 vma = find_vma(current->mm, ctx->args[i].ptr);
804 pages[i].addr += ctx->args[i].ptr -
807 pg_start = (ctx->args[i].ptr & PAGE_MASK) >> PAGE_SHIFT;
808 pg_end = ((ctx->args[i].ptr + len - 1) & PAGE_MASK) >>
810 pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
814 if (ctx->olaps[oix].offset == 0) {
815 rlen -= ALIGN(args, FASTRPC_ALIGN) - args;
816 args = ALIGN(args, FASTRPC_ALIGN);
819 mlen = ctx->olaps[oix].mend - ctx->olaps[oix].mstart;
824 rpra[i].pv = args - ctx->olaps[oix].offset;
825 pages[i].addr = ctx->buf->phys -
826 ctx->olaps[oix].offset +
828 pages[i].addr = pages[i].addr & PAGE_MASK;
830 pg_start = (args & PAGE_MASK) >> PAGE_SHIFT;
831 pg_end = ((args + len - 1) & PAGE_MASK) >> PAGE_SHIFT;
832 pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
837 if (i < inbufs && !ctx->maps[i]) {
838 void *dst = (void *)(uintptr_t)rpra[i].pv;
839 void *src = (void *)(uintptr_t)ctx->args[i].ptr;
842 if (copy_from_user(dst, (void __user *)src,
848 memcpy(dst, src, len);
853 for (i = ctx->nbufs; i < ctx->nscalars; ++i) {
854 rpra[i].pv = (u64) ctx->args[i].ptr;
855 rpra[i].len = ctx->args[i].length;
856 list[i].num = ctx->args[i].length ? 1 : 0;
858 pages[i].addr = ctx->maps[i]->phys;
859 pages[i].size = ctx->maps[i]->size;
864 dev_err(dev, "Error: get invoke args failed:%d\n", err);
869 static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
872 struct fastrpc_remote_arg *rpra = ctx->rpra;
875 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
877 for (i = inbufs; i < ctx->nbufs; ++i) {
878 void *src = (void *)(uintptr_t)rpra[i].pv;
879 void *dst = (void *)(uintptr_t)ctx->args[i].ptr;
880 u64 len = rpra[i].len;
883 if (copy_to_user((void __user *)dst, src, len))
886 memcpy(dst, src, len);
893 static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx,
894 struct fastrpc_invoke_ctx *ctx,
895 u32 kernel, uint32_t handle)
897 struct fastrpc_channel_ctx *cctx;
898 struct fastrpc_user *fl = ctx->fl;
899 struct fastrpc_msg *msg = &ctx->msg;
904 msg->tid = current->pid;
909 msg->ctx = ctx->ctxid | fl->pd;
910 msg->handle = handle;
912 msg->addr = ctx->buf ? ctx->buf->phys : 0;
913 msg->size = roundup(ctx->msg_sz, PAGE_SIZE);
914 fastrpc_context_get(ctx);
916 ret = rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg));
919 fastrpc_context_put(ctx);
925 static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
927 struct fastrpc_invoke_args *args)
929 struct fastrpc_invoke_ctx *ctx = NULL;
935 if (!fl->cctx->rpdev)
938 if (handle == FASTRPC_INIT_HANDLE && !kernel) {
939 dev_warn_ratelimited(fl->sctx->dev, "user app trying to send a kernel RPC message (%d)\n", handle);
943 ctx = fastrpc_context_alloc(fl, kernel, sc, args);
948 err = fastrpc_get_args(kernel, ctx);
953 /* make sure that all CPU memory writes are seen by DSP */
955 /* Send invoke buffer to remote dsp */
956 err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle);
960 /* Wait for remote dsp to respond or time out */
961 err = wait_for_completion_interruptible(&ctx->work);
965 /* Check the response from remote dsp */
971 /* make sure that all memory writes by DSP are seen by CPU */
973 /* populate all the output buffers with results */
974 err = fastrpc_put_args(ctx, kernel);
980 /* We are done with this compute context, remove it from pending list */
981 spin_lock(&fl->lock);
982 list_del(&ctx->node);
983 spin_unlock(&fl->lock);
984 fastrpc_context_put(ctx);
987 dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err);
992 static int fastrpc_init_create_process(struct fastrpc_user *fl,
995 struct fastrpc_init_create init;
996 struct fastrpc_invoke_args *args;
997 struct fastrpc_phy_page pages[1];
998 struct fastrpc_map *map = NULL;
999 struct fastrpc_buf *imem = NULL;
1012 args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL);
1016 if (copy_from_user(&init, argp, sizeof(init))) {
1021 if (init.filelen > INIT_FILELEN_MAX) {
1026 inbuf.pgid = fl->tgid;
1027 inbuf.namelen = strlen(current->comm) + 1;
1028 inbuf.filelen = init.filelen;
1030 inbuf.attrs = init.attrs;
1031 inbuf.siglen = init.siglen;
1034 if (init.filelen && init.filefd) {
1035 err = fastrpc_map_create(fl, init.filefd, init.filelen, &map);
1040 memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4),
1042 err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen,
1047 fl->init_mem = imem;
1048 args[0].ptr = (u64)(uintptr_t)&inbuf;
1049 args[0].length = sizeof(inbuf);
1052 args[1].ptr = (u64)(uintptr_t)current->comm;
1053 args[1].length = inbuf.namelen;
1056 args[2].ptr = (u64) init.file;
1057 args[2].length = inbuf.filelen;
1058 args[2].fd = init.filefd;
1060 pages[0].addr = imem->phys;
1061 pages[0].size = imem->size;
1063 args[3].ptr = (u64)(uintptr_t) pages;
1064 args[3].length = 1 * sizeof(*pages);
1067 args[4].ptr = (u64)(uintptr_t)&inbuf.attrs;
1068 args[4].length = sizeof(inbuf.attrs);
1071 args[5].ptr = (u64)(uintptr_t) &inbuf.siglen;
1072 args[5].length = sizeof(inbuf.siglen);
1075 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0);
1077 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0);
1079 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1089 fl->init_mem = NULL;
1090 fastrpc_buf_free(imem);
1092 fastrpc_map_put(map);
1099 static struct fastrpc_session_ctx *fastrpc_session_alloc(
1100 struct fastrpc_channel_ctx *cctx)
1102 struct fastrpc_session_ctx *session = NULL;
1103 unsigned long flags;
1106 spin_lock_irqsave(&cctx->lock, flags);
1107 for (i = 0; i < cctx->sesscount; i++) {
1108 if (!cctx->session[i].used && cctx->session[i].valid) {
1109 cctx->session[i].used = true;
1110 session = &cctx->session[i];
1114 spin_unlock_irqrestore(&cctx->lock, flags);
1119 static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx,
1120 struct fastrpc_session_ctx *session)
1122 unsigned long flags;
1124 spin_lock_irqsave(&cctx->lock, flags);
1125 session->used = false;
1126 spin_unlock_irqrestore(&cctx->lock, flags);
1129 static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl)
1131 struct fastrpc_invoke_args args[1];
1136 args[0].ptr = (u64)(uintptr_t) &tgid;
1137 args[0].length = sizeof(tgid);
1139 args[0].reserved = 0;
1140 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0);
1142 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1146 static int fastrpc_device_release(struct inode *inode, struct file *file)
1148 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1149 struct fastrpc_channel_ctx *cctx = fl->cctx;
1150 struct fastrpc_invoke_ctx *ctx, *n;
1151 struct fastrpc_map *map, *m;
1152 unsigned long flags;
1154 fastrpc_release_current_dsp_process(fl);
1156 spin_lock_irqsave(&cctx->lock, flags);
1157 list_del(&fl->user);
1158 spin_unlock_irqrestore(&cctx->lock, flags);
1161 fastrpc_buf_free(fl->init_mem);
1163 list_for_each_entry_safe(ctx, n, &fl->pending, node) {
1164 list_del(&ctx->node);
1165 fastrpc_context_put(ctx);
1168 list_for_each_entry_safe(map, m, &fl->maps, node)
1169 fastrpc_map_put(map);
1171 fastrpc_session_free(cctx, fl->sctx);
1172 fastrpc_channel_ctx_put(cctx);
1174 mutex_destroy(&fl->mutex);
1176 file->private_data = NULL;
1181 static int fastrpc_device_open(struct inode *inode, struct file *filp)
1183 struct fastrpc_channel_ctx *cctx = miscdev_to_cctx(filp->private_data);
1184 struct fastrpc_user *fl = NULL;
1185 unsigned long flags;
1187 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
1191 /* Released in fastrpc_device_release() */
1192 fastrpc_channel_ctx_get(cctx);
1194 filp->private_data = fl;
1195 spin_lock_init(&fl->lock);
1196 mutex_init(&fl->mutex);
1197 INIT_LIST_HEAD(&fl->pending);
1198 INIT_LIST_HEAD(&fl->maps);
1199 INIT_LIST_HEAD(&fl->user);
1200 fl->tgid = current->tgid;
1203 fl->sctx = fastrpc_session_alloc(cctx);
1205 dev_err(&cctx->rpdev->dev, "No session available\n");
1206 mutex_destroy(&fl->mutex);
1212 spin_lock_irqsave(&cctx->lock, flags);
1213 list_add_tail(&fl->user, &cctx->users);
1214 spin_unlock_irqrestore(&cctx->lock, flags);
1219 static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
1221 struct fastrpc_alloc_dma_buf bp;
1222 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
1223 struct fastrpc_buf *buf = NULL;
1226 if (copy_from_user(&bp, argp, sizeof(bp)))
1229 err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf);
1232 exp_info.ops = &fastrpc_dma_buf_ops;
1233 exp_info.size = bp.size;
1234 exp_info.flags = O_RDWR;
1235 exp_info.priv = buf;
1236 buf->dmabuf = dma_buf_export(&exp_info);
1237 if (IS_ERR(buf->dmabuf)) {
1238 err = PTR_ERR(buf->dmabuf);
1239 fastrpc_buf_free(buf);
1243 bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE);
1245 dma_buf_put(buf->dmabuf);
1249 if (copy_to_user(argp, &bp, sizeof(bp))) {
1251 * The usercopy failed, but we can't do much about it, as
1252 * dma_buf_fd() already called fd_install() and made the
1253 * file descriptor accessible for the current process. It
1254 * might already be closed and dmabuf no longer valid when
1255 * we reach this point. Therefore "leak" the fd and rely on
1256 * the process exit path to do any required cleanup.
1264 static int fastrpc_init_attach(struct fastrpc_user *fl)
1266 struct fastrpc_invoke_args args[1];
1267 int tgid = fl->tgid;
1270 args[0].ptr = (u64)(uintptr_t) &tgid;
1271 args[0].length = sizeof(tgid);
1273 args[0].reserved = 0;
1274 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0);
1277 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1281 static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp)
1283 struct fastrpc_invoke_args *args = NULL;
1284 struct fastrpc_invoke inv;
1288 if (copy_from_user(&inv, argp, sizeof(inv)))
1291 /* nscalars is truncated here to max supported value */
1292 nscalars = REMOTE_SCALARS_LENGTH(inv.sc);
1294 args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL);
1298 if (copy_from_user(args, (void __user *)(uintptr_t)inv.args,
1299 nscalars * sizeof(*args))) {
1305 err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args);
1311 static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
1314 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1315 char __user *argp = (char __user *)arg;
1319 case FASTRPC_IOCTL_INVOKE:
1320 err = fastrpc_invoke(fl, argp);
1322 case FASTRPC_IOCTL_INIT_ATTACH:
1323 err = fastrpc_init_attach(fl);
1325 case FASTRPC_IOCTL_INIT_CREATE:
1326 err = fastrpc_init_create_process(fl, argp);
1328 case FASTRPC_IOCTL_ALLOC_DMA_BUFF:
1329 err = fastrpc_dmabuf_alloc(fl, argp);
1339 static const struct file_operations fastrpc_fops = {
1340 .open = fastrpc_device_open,
1341 .release = fastrpc_device_release,
1342 .unlocked_ioctl = fastrpc_device_ioctl,
1343 .compat_ioctl = fastrpc_device_ioctl,
1346 static int fastrpc_cb_probe(struct platform_device *pdev)
1348 struct fastrpc_channel_ctx *cctx;
1349 struct fastrpc_session_ctx *sess;
1350 struct device *dev = &pdev->dev;
1351 int i, sessions = 0;
1352 unsigned long flags;
1355 cctx = dev_get_drvdata(dev->parent);
1359 of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions);
1361 spin_lock_irqsave(&cctx->lock, flags);
1362 if (cctx->sesscount >= FASTRPC_MAX_SESSIONS) {
1363 dev_err(&pdev->dev, "too many sessions\n");
1364 spin_unlock_irqrestore(&cctx->lock, flags);
1367 sess = &cctx->session[cctx->sesscount++];
1371 dev_set_drvdata(dev, sess);
1373 if (of_property_read_u32(dev->of_node, "reg", &sess->sid))
1374 dev_info(dev, "FastRPC Session ID not specified in DT\n");
1377 struct fastrpc_session_ctx *dup_sess;
1379 for (i = 1; i < sessions; i++) {
1380 if (cctx->sesscount >= FASTRPC_MAX_SESSIONS)
1382 dup_sess = &cctx->session[cctx->sesscount++];
1383 memcpy(dup_sess, sess, sizeof(*dup_sess));
1386 spin_unlock_irqrestore(&cctx->lock, flags);
1387 rc = dma_set_mask(dev, DMA_BIT_MASK(32));
1389 dev_err(dev, "32-bit DMA enable failed\n");
1396 static int fastrpc_cb_remove(struct platform_device *pdev)
1398 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent);
1399 struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev);
1400 unsigned long flags;
1403 spin_lock_irqsave(&cctx->lock, flags);
1404 for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) {
1405 if (cctx->session[i].sid == sess->sid) {
1406 cctx->session[i].valid = false;
1410 spin_unlock_irqrestore(&cctx->lock, flags);
1415 static const struct of_device_id fastrpc_match_table[] = {
1416 { .compatible = "qcom,fastrpc-compute-cb", },
1420 static struct platform_driver fastrpc_cb_driver = {
1421 .probe = fastrpc_cb_probe,
1422 .remove = fastrpc_cb_remove,
1424 .name = "qcom,fastrpc-cb",
1425 .of_match_table = fastrpc_match_table,
1426 .suppress_bind_attrs = true,
1430 static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
1432 struct device *rdev = &rpdev->dev;
1433 struct fastrpc_channel_ctx *data;
1434 int i, err, domain_id = -1;
1437 err = of_property_read_string(rdev->of_node, "label", &domain);
1439 dev_info(rdev, "FastRPC Domain not specified in DT\n");
1443 for (i = 0; i <= CDSP_DOMAIN_ID; i++) {
1444 if (!strcmp(domains[i], domain)) {
1450 if (domain_id < 0) {
1451 dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id);
1455 data = kzalloc(sizeof(*data), GFP_KERNEL);
1459 data->miscdev.minor = MISC_DYNAMIC_MINOR;
1460 data->miscdev.name = devm_kasprintf(rdev, GFP_KERNEL, "fastrpc-%s",
1461 domains[domain_id]);
1462 data->miscdev.fops = &fastrpc_fops;
1463 err = misc_register(&data->miscdev);
1469 kref_init(&data->refcount);
1471 dev_set_drvdata(&rpdev->dev, data);
1472 dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
1473 INIT_LIST_HEAD(&data->users);
1474 spin_lock_init(&data->lock);
1475 idr_init(&data->ctx_idr);
1476 data->domain_id = domain_id;
1477 data->rpdev = rpdev;
1479 return of_platform_populate(rdev->of_node, NULL, NULL, rdev);
1482 static void fastrpc_notify_users(struct fastrpc_user *user)
1484 struct fastrpc_invoke_ctx *ctx;
1486 spin_lock(&user->lock);
1487 list_for_each_entry(ctx, &user->pending, node)
1488 complete(&ctx->work);
1489 spin_unlock(&user->lock);
1492 static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
1494 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
1495 struct fastrpc_user *user;
1496 unsigned long flags;
1498 spin_lock_irqsave(&cctx->lock, flags);
1499 list_for_each_entry(user, &cctx->users, user)
1500 fastrpc_notify_users(user);
1501 spin_unlock_irqrestore(&cctx->lock, flags);
1503 misc_deregister(&cctx->miscdev);
1504 of_platform_depopulate(&rpdev->dev);
1507 fastrpc_channel_ctx_put(cctx);
1510 static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
1511 int len, void *priv, u32 addr)
1513 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
1514 struct fastrpc_invoke_rsp *rsp = data;
1515 struct fastrpc_invoke_ctx *ctx;
1516 unsigned long flags;
1517 unsigned long ctxid;
1519 if (len < sizeof(*rsp))
1522 ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4);
1524 spin_lock_irqsave(&cctx->lock, flags);
1525 ctx = idr_find(&cctx->ctx_idr, ctxid);
1526 spin_unlock_irqrestore(&cctx->lock, flags);
1529 dev_err(&rpdev->dev, "No context ID matches response\n");
1533 ctx->retval = rsp->retval;
1534 complete(&ctx->work);
1537 * The DMA buffer associated with the context cannot be freed in
1538 * interrupt context so schedule it through a worker thread to
1539 * avoid a kernel BUG.
1541 schedule_work(&ctx->put_work);
1546 static const struct of_device_id fastrpc_rpmsg_of_match[] = {
1547 { .compatible = "qcom,fastrpc" },
1550 MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match);
1552 static struct rpmsg_driver fastrpc_driver = {
1553 .probe = fastrpc_rpmsg_probe,
1554 .remove = fastrpc_rpmsg_remove,
1555 .callback = fastrpc_rpmsg_callback,
1557 .name = "qcom,fastrpc",
1558 .of_match_table = fastrpc_rpmsg_of_match,
1562 static int fastrpc_init(void)
1566 ret = platform_driver_register(&fastrpc_cb_driver);
1568 pr_err("fastrpc: failed to register cb driver\n");
1572 ret = register_rpmsg_driver(&fastrpc_driver);
1574 pr_err("fastrpc: failed to register rpmsg driver\n");
1575 platform_driver_unregister(&fastrpc_cb_driver);
1581 module_init(fastrpc_init);
1583 static void fastrpc_exit(void)
1585 platform_driver_unregister(&fastrpc_cb_driver);
1586 unregister_rpmsg_driver(&fastrpc_driver);
1588 module_exit(fastrpc_exit);
1590 MODULE_LICENSE("GPL v2");