Linux 5.1.15
[linux/fpc-iii.git] / drivers / misc / fastrpc.c
blob35be1cc11dd85361bb13148820afd6da06ba12a0
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
3 // Copyright (c) 2018, Linaro Limited
5 #include <linux/completion.h>
6 #include <linux/device.h>
7 #include <linux/dma-buf.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/idr.h>
10 #include <linux/list.h>
11 #include <linux/miscdevice.h>
12 #include <linux/module.h>
13 #include <linux/of_address.h>
14 #include <linux/of.h>
15 #include <linux/of_platform.h>
16 #include <linux/rpmsg.h>
17 #include <linux/scatterlist.h>
18 #include <linux/slab.h>
19 #include <uapi/misc/fastrpc.h>
21 #define ADSP_DOMAIN_ID (0)
22 #define MDSP_DOMAIN_ID (1)
23 #define SDSP_DOMAIN_ID (2)
24 #define CDSP_DOMAIN_ID (3)
25 #define FASTRPC_DEV_MAX 4 /* adsp, mdsp, slpi, cdsp*/
26 #define FASTRPC_MAX_SESSIONS 9 /*8 compute, 1 cpz*/
27 #define FASTRPC_ALIGN 128
28 #define FASTRPC_MAX_FDLIST 16
29 #define FASTRPC_MAX_CRCLIST 64
30 #define FASTRPC_PHYS(p) ((p) & 0xffffffff)
31 #define FASTRPC_CTX_MAX (256)
32 #define FASTRPC_INIT_HANDLE 1
33 #define FASTRPC_CTXID_MASK (0xFF0)
34 #define INIT_FILELEN_MAX (2 * 1024 * 1024)
35 #define INIT_MEMLEN_MAX (8 * 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) | \
60 (oout & 0x0f))
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",
76 "sdsp", "cdsp"};
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 {
88 u64 pv;
89 u64 len;
92 struct fastrpc_msg {
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 {
108 struct fastrpc_user *fl;
109 struct dma_buf *dmabuf;
110 struct device *dev;
111 void *virt;
112 u64 phys;
113 u64 size;
114 /* Lock for dma buf attachments */
115 struct mutex lock;
116 struct list_head attachments;
119 struct fastrpc_dma_buf_attachment {
120 struct device *dev;
121 struct sg_table sgt;
122 struct list_head node;
125 struct fastrpc_map {
126 struct list_head node;
127 struct fastrpc_user *fl;
128 int fd;
129 struct dma_buf *buf;
130 struct sg_table *table;
131 struct dma_buf_attachment *attach;
132 u64 phys;
133 u64 size;
134 void *va;
135 u64 len;
136 struct kref refcount;
139 struct fastrpc_invoke_ctx {
140 int nscalars;
141 int nbufs;
142 int retval;
143 int pid;
144 int tgid;
145 u32 sc;
146 u32 *crc;
147 u64 ctxid;
148 u64 msg_sz;
149 struct kref refcount;
150 struct list_head node; /* list of ctxs */
151 struct completion work;
152 struct fastrpc_msg msg;
153 struct fastrpc_user *fl;
154 struct fastrpc_remote_arg *rpra;
155 struct fastrpc_map **maps;
156 struct fastrpc_buf *buf;
157 struct fastrpc_invoke_args *args;
158 struct fastrpc_channel_ctx *cctx;
161 struct fastrpc_session_ctx {
162 struct device *dev;
163 int sid;
164 bool used;
165 bool valid;
168 struct fastrpc_channel_ctx {
169 int domain_id;
170 int sesscount;
171 struct rpmsg_device *rpdev;
172 struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS];
173 spinlock_t lock;
174 struct idr ctx_idr;
175 struct list_head users;
176 struct miscdevice miscdev;
179 struct fastrpc_user {
180 struct list_head user;
181 struct list_head maps;
182 struct list_head pending;
184 struct fastrpc_channel_ctx *cctx;
185 struct fastrpc_session_ctx *sctx;
186 struct fastrpc_buf *init_mem;
188 int tgid;
189 int pd;
190 /* Lock for lists */
191 spinlock_t lock;
192 /* lock for allocations */
193 struct mutex mutex;
196 static void fastrpc_free_map(struct kref *ref)
198 struct fastrpc_map *map;
200 map = container_of(ref, struct fastrpc_map, refcount);
202 if (map->table) {
203 dma_buf_unmap_attachment(map->attach, map->table,
204 DMA_BIDIRECTIONAL);
205 dma_buf_detach(map->buf, map->attach);
206 dma_buf_put(map->buf);
209 kfree(map);
212 static void fastrpc_map_put(struct fastrpc_map *map)
214 if (map)
215 kref_put(&map->refcount, fastrpc_free_map);
218 static void fastrpc_map_get(struct fastrpc_map *map)
220 if (map)
221 kref_get(&map->refcount);
224 static int fastrpc_map_find(struct fastrpc_user *fl, int fd,
225 struct fastrpc_map **ppmap)
227 struct fastrpc_map *map = NULL;
229 mutex_lock(&fl->mutex);
230 list_for_each_entry(map, &fl->maps, node) {
231 if (map->fd == fd) {
232 fastrpc_map_get(map);
233 *ppmap = map;
234 mutex_unlock(&fl->mutex);
235 return 0;
238 mutex_unlock(&fl->mutex);
240 return -ENOENT;
243 static void fastrpc_buf_free(struct fastrpc_buf *buf)
245 dma_free_coherent(buf->dev, buf->size, buf->virt,
246 FASTRPC_PHYS(buf->phys));
247 kfree(buf);
250 static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
251 u64 size, struct fastrpc_buf **obuf)
253 struct fastrpc_buf *buf;
255 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
256 if (!buf)
257 return -ENOMEM;
259 INIT_LIST_HEAD(&buf->attachments);
260 mutex_init(&buf->lock);
262 buf->fl = fl;
263 buf->virt = NULL;
264 buf->phys = 0;
265 buf->size = size;
266 buf->dev = dev;
268 buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys,
269 GFP_KERNEL);
270 if (!buf->virt)
271 return -ENOMEM;
273 if (fl->sctx && fl->sctx->sid)
274 buf->phys += ((u64)fl->sctx->sid << 32);
276 *obuf = buf;
278 return 0;
281 static void fastrpc_context_free(struct kref *ref)
283 struct fastrpc_invoke_ctx *ctx;
284 struct fastrpc_channel_ctx *cctx;
285 int i;
287 ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
288 cctx = ctx->cctx;
290 for (i = 0; i < ctx->nscalars; i++)
291 fastrpc_map_put(ctx->maps[i]);
293 if (ctx->buf)
294 fastrpc_buf_free(ctx->buf);
296 spin_lock(&cctx->lock);
297 idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4);
298 spin_unlock(&cctx->lock);
300 kfree(ctx->maps);
301 kfree(ctx);
304 static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx)
306 kref_get(&ctx->refcount);
309 static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx)
311 kref_put(&ctx->refcount, fastrpc_context_free);
314 static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
315 struct fastrpc_user *user, u32 kernel, u32 sc,
316 struct fastrpc_invoke_args *args)
318 struct fastrpc_channel_ctx *cctx = user->cctx;
319 struct fastrpc_invoke_ctx *ctx = NULL;
320 int ret;
322 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
323 if (!ctx)
324 return ERR_PTR(-ENOMEM);
326 INIT_LIST_HEAD(&ctx->node);
327 ctx->fl = user;
328 ctx->nscalars = REMOTE_SCALARS_LENGTH(sc);
329 ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) +
330 REMOTE_SCALARS_OUTBUFS(sc);
332 if (ctx->nscalars) {
333 ctx->maps = kcalloc(ctx->nscalars,
334 sizeof(*ctx->maps), GFP_KERNEL);
335 if (!ctx->maps) {
336 kfree(ctx);
337 return ERR_PTR(-ENOMEM);
339 ctx->args = args;
342 ctx->sc = sc;
343 ctx->retval = -1;
344 ctx->pid = current->pid;
345 ctx->tgid = user->tgid;
346 ctx->cctx = cctx;
347 init_completion(&ctx->work);
349 spin_lock(&user->lock);
350 list_add_tail(&ctx->node, &user->pending);
351 spin_unlock(&user->lock);
353 spin_lock(&cctx->lock);
354 ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1,
355 FASTRPC_CTX_MAX, GFP_ATOMIC);
356 if (ret < 0) {
357 spin_unlock(&cctx->lock);
358 goto err_idr;
360 ctx->ctxid = ret << 4;
361 spin_unlock(&cctx->lock);
363 kref_init(&ctx->refcount);
365 return ctx;
366 err_idr:
367 spin_lock(&user->lock);
368 list_del(&ctx->node);
369 spin_unlock(&user->lock);
370 kfree(ctx->maps);
371 kfree(ctx);
373 return ERR_PTR(ret);
376 static struct sg_table *
377 fastrpc_map_dma_buf(struct dma_buf_attachment *attachment,
378 enum dma_data_direction dir)
380 struct fastrpc_dma_buf_attachment *a = attachment->priv;
381 struct sg_table *table;
383 table = &a->sgt;
385 if (!dma_map_sg(attachment->dev, table->sgl, table->nents, dir))
386 return ERR_PTR(-ENOMEM);
388 return table;
391 static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach,
392 struct sg_table *table,
393 enum dma_data_direction dir)
395 dma_unmap_sg(attach->dev, table->sgl, table->nents, dir);
398 static void fastrpc_release(struct dma_buf *dmabuf)
400 struct fastrpc_buf *buffer = dmabuf->priv;
402 fastrpc_buf_free(buffer);
405 static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf,
406 struct dma_buf_attachment *attachment)
408 struct fastrpc_dma_buf_attachment *a;
409 struct fastrpc_buf *buffer = dmabuf->priv;
410 int ret;
412 a = kzalloc(sizeof(*a), GFP_KERNEL);
413 if (!a)
414 return -ENOMEM;
416 ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt,
417 FASTRPC_PHYS(buffer->phys), buffer->size);
418 if (ret < 0) {
419 dev_err(buffer->dev, "failed to get scatterlist from DMA API\n");
420 return -EINVAL;
423 a->dev = attachment->dev;
424 INIT_LIST_HEAD(&a->node);
425 attachment->priv = a;
427 mutex_lock(&buffer->lock);
428 list_add(&a->node, &buffer->attachments);
429 mutex_unlock(&buffer->lock);
431 return 0;
434 static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf,
435 struct dma_buf_attachment *attachment)
437 struct fastrpc_dma_buf_attachment *a = attachment->priv;
438 struct fastrpc_buf *buffer = dmabuf->priv;
440 mutex_lock(&buffer->lock);
441 list_del(&a->node);
442 mutex_unlock(&buffer->lock);
443 kfree(a);
446 static void *fastrpc_kmap(struct dma_buf *dmabuf, unsigned long pgnum)
448 struct fastrpc_buf *buf = dmabuf->priv;
450 return buf->virt ? buf->virt + pgnum * PAGE_SIZE : NULL;
453 static void *fastrpc_vmap(struct dma_buf *dmabuf)
455 struct fastrpc_buf *buf = dmabuf->priv;
457 return buf->virt;
460 static int fastrpc_mmap(struct dma_buf *dmabuf,
461 struct vm_area_struct *vma)
463 struct fastrpc_buf *buf = dmabuf->priv;
464 size_t size = vma->vm_end - vma->vm_start;
466 return dma_mmap_coherent(buf->dev, vma, buf->virt,
467 FASTRPC_PHYS(buf->phys), size);
470 static const struct dma_buf_ops fastrpc_dma_buf_ops = {
471 .attach = fastrpc_dma_buf_attach,
472 .detach = fastrpc_dma_buf_detatch,
473 .map_dma_buf = fastrpc_map_dma_buf,
474 .unmap_dma_buf = fastrpc_unmap_dma_buf,
475 .mmap = fastrpc_mmap,
476 .map = fastrpc_kmap,
477 .vmap = fastrpc_vmap,
478 .release = fastrpc_release,
481 static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
482 u64 len, struct fastrpc_map **ppmap)
484 struct fastrpc_session_ctx *sess = fl->sctx;
485 struct fastrpc_map *map = NULL;
486 int err = 0;
488 if (!fastrpc_map_find(fl, fd, ppmap))
489 return 0;
491 map = kzalloc(sizeof(*map), GFP_KERNEL);
492 if (!map)
493 return -ENOMEM;
495 INIT_LIST_HEAD(&map->node);
496 map->fl = fl;
497 map->fd = fd;
498 map->buf = dma_buf_get(fd);
499 if (IS_ERR(map->buf)) {
500 err = PTR_ERR(map->buf);
501 goto get_err;
504 map->attach = dma_buf_attach(map->buf, sess->dev);
505 if (IS_ERR(map->attach)) {
506 dev_err(sess->dev, "Failed to attach dmabuf\n");
507 err = PTR_ERR(map->attach);
508 goto attach_err;
511 map->table = dma_buf_map_attachment(map->attach, DMA_BIDIRECTIONAL);
512 if (IS_ERR(map->table)) {
513 err = PTR_ERR(map->table);
514 goto map_err;
517 map->phys = sg_dma_address(map->table->sgl);
518 map->phys += ((u64)fl->sctx->sid << 32);
519 map->size = len;
520 map->va = sg_virt(map->table->sgl);
521 map->len = len;
522 kref_init(&map->refcount);
524 spin_lock(&fl->lock);
525 list_add_tail(&map->node, &fl->maps);
526 spin_unlock(&fl->lock);
527 *ppmap = map;
529 return 0;
531 map_err:
532 dma_buf_detach(map->buf, map->attach);
533 attach_err:
534 dma_buf_put(map->buf);
535 get_err:
536 kfree(map);
538 return err;
542 * Fastrpc payload buffer with metadata looks like:
544 * >>>>>> START of METADATA <<<<<<<<<
545 * +---------------------------------+
546 * | Arguments |
547 * | type:(struct fastrpc_remote_arg)|
548 * | (0 - N) |
549 * +---------------------------------+
550 * | Invoke Buffer list |
551 * | type:(struct fastrpc_invoke_buf)|
552 * | (0 - N) |
553 * +---------------------------------+
554 * | Page info list |
555 * | type:(struct fastrpc_phy_page) |
556 * | (0 - N) |
557 * +---------------------------------+
558 * | Optional info |
559 * |(can be specific to SoC/Firmware)|
560 * +---------------------------------+
561 * >>>>>>>> END of METADATA <<<<<<<<<
562 * +---------------------------------+
563 * | Inline ARGS |
564 * | (0-N) |
565 * +---------------------------------+
568 static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx)
570 int size = 0;
572 size = (sizeof(struct fastrpc_remote_arg) +
573 sizeof(struct fastrpc_invoke_buf) +
574 sizeof(struct fastrpc_phy_page)) * ctx->nscalars +
575 sizeof(u64) * FASTRPC_MAX_FDLIST +
576 sizeof(u32) * FASTRPC_MAX_CRCLIST;
578 return size;
581 static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen)
583 u64 size = 0;
584 int i;
586 size = ALIGN(metalen, FASTRPC_ALIGN);
587 for (i = 0; i < ctx->nscalars; i++) {
588 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) {
589 size = ALIGN(size, FASTRPC_ALIGN);
590 size += ctx->args[i].length;
594 return size;
597 static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
599 struct device *dev = ctx->fl->sctx->dev;
600 int i, err;
602 for (i = 0; i < ctx->nscalars; ++i) {
603 /* Make sure reserved field is set to 0 */
604 if (ctx->args[i].reserved)
605 return -EINVAL;
607 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 ||
608 ctx->args[i].length == 0)
609 continue;
611 err = fastrpc_map_create(ctx->fl, ctx->args[i].fd,
612 ctx->args[i].length, &ctx->maps[i]);
613 if (err) {
614 dev_err(dev, "Error Creating map %d\n", err);
615 return -EINVAL;
619 return 0;
622 static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
624 struct device *dev = ctx->fl->sctx->dev;
625 struct fastrpc_remote_arg *rpra;
626 struct fastrpc_invoke_buf *list;
627 struct fastrpc_phy_page *pages;
628 int inbufs, i, err = 0;
629 u64 rlen, pkt_size;
630 uintptr_t args;
631 int metalen;
634 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
635 metalen = fastrpc_get_meta_size(ctx);
636 pkt_size = fastrpc_get_payload_size(ctx, metalen);
638 err = fastrpc_create_maps(ctx);
639 if (err)
640 return err;
642 ctx->msg_sz = pkt_size;
644 err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf);
645 if (err)
646 return err;
648 rpra = ctx->buf->virt;
649 list = ctx->buf->virt + ctx->nscalars * sizeof(*rpra);
650 pages = ctx->buf->virt + ctx->nscalars * (sizeof(*list) +
651 sizeof(*rpra));
652 args = (uintptr_t)ctx->buf->virt + metalen;
653 rlen = pkt_size - metalen;
654 ctx->rpra = rpra;
656 for (i = 0; i < ctx->nbufs; ++i) {
657 u64 len = ctx->args[i].length;
659 rpra[i].pv = 0;
660 rpra[i].len = len;
661 list[i].num = len ? 1 : 0;
662 list[i].pgidx = i;
664 if (!len)
665 continue;
667 pages[i].size = roundup(len, PAGE_SIZE);
669 if (ctx->maps[i]) {
670 struct vm_area_struct *vma = NULL;
672 rpra[i].pv = (u64) ctx->args[i].ptr;
673 pages[i].addr = ctx->maps[i]->phys;
675 vma = find_vma(current->mm, ctx->args[i].ptr);
676 if (vma)
677 pages[i].addr += ctx->args[i].ptr -
678 vma->vm_start;
680 } else {
681 rlen -= ALIGN(args, FASTRPC_ALIGN) - args;
682 args = ALIGN(args, FASTRPC_ALIGN);
683 if (rlen < len)
684 goto bail;
686 rpra[i].pv = args;
687 pages[i].addr = ctx->buf->phys + (pkt_size - rlen);
688 pages[i].addr = pages[i].addr & PAGE_MASK;
689 args = args + len;
690 rlen -= len;
693 if (i < inbufs && !ctx->maps[i]) {
694 void *dst = (void *)(uintptr_t)rpra[i].pv;
695 void *src = (void *)(uintptr_t)ctx->args[i].ptr;
697 if (!kernel) {
698 if (copy_from_user(dst, (void __user *)src,
699 len)) {
700 err = -EFAULT;
701 goto bail;
703 } else {
704 memcpy(dst, src, len);
709 for (i = ctx->nbufs; i < ctx->nscalars; ++i) {
710 rpra[i].pv = (u64) ctx->args[i].ptr;
711 rpra[i].len = ctx->args[i].length;
712 list[i].num = ctx->args[i].length ? 1 : 0;
713 list[i].pgidx = i;
714 pages[i].addr = ctx->maps[i]->phys;
715 pages[i].size = ctx->maps[i]->size;
718 bail:
719 if (err)
720 dev_err(dev, "Error: get invoke args failed:%d\n", err);
722 return err;
725 static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
726 u32 kernel)
728 struct fastrpc_remote_arg *rpra = ctx->rpra;
729 int i, inbufs;
731 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
733 for (i = inbufs; i < ctx->nbufs; ++i) {
734 void *src = (void *)(uintptr_t)rpra[i].pv;
735 void *dst = (void *)(uintptr_t)ctx->args[i].ptr;
736 u64 len = rpra[i].len;
738 if (!kernel) {
739 if (copy_to_user((void __user *)dst, src, len))
740 return -EFAULT;
741 } else {
742 memcpy(dst, src, len);
746 return 0;
749 static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx,
750 struct fastrpc_invoke_ctx *ctx,
751 u32 kernel, uint32_t handle)
753 struct fastrpc_channel_ctx *cctx;
754 struct fastrpc_user *fl = ctx->fl;
755 struct fastrpc_msg *msg = &ctx->msg;
757 cctx = fl->cctx;
758 msg->pid = fl->tgid;
759 msg->tid = current->pid;
761 if (kernel)
762 msg->pid = 0;
764 msg->ctx = ctx->ctxid | fl->pd;
765 msg->handle = handle;
766 msg->sc = ctx->sc;
767 msg->addr = ctx->buf ? ctx->buf->phys : 0;
768 msg->size = roundup(ctx->msg_sz, PAGE_SIZE);
769 fastrpc_context_get(ctx);
771 return rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg));
774 static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
775 u32 handle, u32 sc,
776 struct fastrpc_invoke_args *args)
778 struct fastrpc_invoke_ctx *ctx = NULL;
779 int err = 0;
781 if (!fl->sctx)
782 return -EINVAL;
784 ctx = fastrpc_context_alloc(fl, kernel, sc, args);
785 if (IS_ERR(ctx))
786 return PTR_ERR(ctx);
788 if (ctx->nscalars) {
789 err = fastrpc_get_args(kernel, ctx);
790 if (err)
791 goto bail;
794 /* make sure that all CPU memory writes are seen by DSP */
795 dma_wmb();
796 /* Send invoke buffer to remote dsp */
797 err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle);
798 if (err)
799 goto bail;
801 /* Wait for remote dsp to respond or time out */
802 err = wait_for_completion_interruptible(&ctx->work);
803 if (err)
804 goto bail;
806 /* Check the response from remote dsp */
807 err = ctx->retval;
808 if (err)
809 goto bail;
811 if (ctx->nscalars) {
812 /* make sure that all memory writes by DSP are seen by CPU */
813 dma_rmb();
814 /* populate all the output buffers with results */
815 err = fastrpc_put_args(ctx, kernel);
816 if (err)
817 goto bail;
820 bail:
821 /* We are done with this compute context, remove it from pending list */
822 spin_lock(&fl->lock);
823 list_del(&ctx->node);
824 spin_unlock(&fl->lock);
825 fastrpc_context_put(ctx);
827 if (err)
828 dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err);
830 return err;
833 static int fastrpc_init_create_process(struct fastrpc_user *fl,
834 char __user *argp)
836 struct fastrpc_init_create init;
837 struct fastrpc_invoke_args *args;
838 struct fastrpc_phy_page pages[1];
839 struct fastrpc_map *map = NULL;
840 struct fastrpc_buf *imem = NULL;
841 int memlen;
842 int err;
843 struct {
844 int pgid;
845 u32 namelen;
846 u32 filelen;
847 u32 pageslen;
848 u32 attrs;
849 u32 siglen;
850 } inbuf;
851 u32 sc;
853 args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL);
854 if (!args)
855 return -ENOMEM;
857 if (copy_from_user(&init, argp, sizeof(init))) {
858 err = -EFAULT;
859 goto err;
862 if (init.filelen > INIT_FILELEN_MAX) {
863 err = -EINVAL;
864 goto err;
867 inbuf.pgid = fl->tgid;
868 inbuf.namelen = strlen(current->comm) + 1;
869 inbuf.filelen = init.filelen;
870 inbuf.pageslen = 1;
871 inbuf.attrs = init.attrs;
872 inbuf.siglen = init.siglen;
873 fl->pd = 1;
875 if (init.filelen && init.filefd) {
876 err = fastrpc_map_create(fl, init.filefd, init.filelen, &map);
877 if (err)
878 goto err;
881 memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4),
882 1024 * 1024);
883 err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen,
884 &imem);
885 if (err)
886 goto err_alloc;
888 fl->init_mem = imem;
889 args[0].ptr = (u64)(uintptr_t)&inbuf;
890 args[0].length = sizeof(inbuf);
891 args[0].fd = -1;
893 args[1].ptr = (u64)(uintptr_t)current->comm;
894 args[1].length = inbuf.namelen;
895 args[1].fd = -1;
897 args[2].ptr = (u64) init.file;
898 args[2].length = inbuf.filelen;
899 args[2].fd = init.filefd;
901 pages[0].addr = imem->phys;
902 pages[0].size = imem->size;
904 args[3].ptr = (u64)(uintptr_t) pages;
905 args[3].length = 1 * sizeof(*pages);
906 args[3].fd = -1;
908 args[4].ptr = (u64)(uintptr_t)&inbuf.attrs;
909 args[4].length = sizeof(inbuf.attrs);
910 args[4].fd = -1;
912 args[5].ptr = (u64)(uintptr_t) &inbuf.siglen;
913 args[5].length = sizeof(inbuf.siglen);
914 args[5].fd = -1;
916 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0);
917 if (init.attrs)
918 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0);
920 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
921 sc, args);
922 if (err)
923 goto err_invoke;
925 kfree(args);
927 return 0;
929 err_invoke:
930 fl->init_mem = NULL;
931 fastrpc_buf_free(imem);
932 err_alloc:
933 if (map) {
934 spin_lock(&fl->lock);
935 list_del(&map->node);
936 spin_unlock(&fl->lock);
937 fastrpc_map_put(map);
939 err:
940 kfree(args);
942 return err;
945 static struct fastrpc_session_ctx *fastrpc_session_alloc(
946 struct fastrpc_channel_ctx *cctx)
948 struct fastrpc_session_ctx *session = NULL;
949 int i;
951 spin_lock(&cctx->lock);
952 for (i = 0; i < cctx->sesscount; i++) {
953 if (!cctx->session[i].used && cctx->session[i].valid) {
954 cctx->session[i].used = true;
955 session = &cctx->session[i];
956 break;
959 spin_unlock(&cctx->lock);
961 return session;
964 static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx,
965 struct fastrpc_session_ctx *session)
967 spin_lock(&cctx->lock);
968 session->used = false;
969 spin_unlock(&cctx->lock);
972 static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl)
974 struct fastrpc_invoke_args args[1];
975 int tgid = 0;
976 u32 sc;
978 tgid = fl->tgid;
979 args[0].ptr = (u64)(uintptr_t) &tgid;
980 args[0].length = sizeof(tgid);
981 args[0].fd = -1;
982 args[0].reserved = 0;
983 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0);
985 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
986 sc, &args[0]);
989 static int fastrpc_device_release(struct inode *inode, struct file *file)
991 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
992 struct fastrpc_channel_ctx *cctx = fl->cctx;
993 struct fastrpc_invoke_ctx *ctx, *n;
994 struct fastrpc_map *map, *m;
996 fastrpc_release_current_dsp_process(fl);
998 spin_lock(&cctx->lock);
999 list_del(&fl->user);
1000 spin_unlock(&cctx->lock);
1002 if (fl->init_mem)
1003 fastrpc_buf_free(fl->init_mem);
1005 list_for_each_entry_safe(ctx, n, &fl->pending, node) {
1006 list_del(&ctx->node);
1007 fastrpc_context_put(ctx);
1010 list_for_each_entry_safe(map, m, &fl->maps, node) {
1011 list_del(&map->node);
1012 fastrpc_map_put(map);
1015 fastrpc_session_free(cctx, fl->sctx);
1017 mutex_destroy(&fl->mutex);
1018 kfree(fl);
1019 file->private_data = NULL;
1021 return 0;
1024 static int fastrpc_device_open(struct inode *inode, struct file *filp)
1026 struct fastrpc_channel_ctx *cctx = miscdev_to_cctx(filp->private_data);
1027 struct fastrpc_user *fl = NULL;
1029 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
1030 if (!fl)
1031 return -ENOMEM;
1033 filp->private_data = fl;
1034 spin_lock_init(&fl->lock);
1035 mutex_init(&fl->mutex);
1036 INIT_LIST_HEAD(&fl->pending);
1037 INIT_LIST_HEAD(&fl->maps);
1038 INIT_LIST_HEAD(&fl->user);
1039 fl->tgid = current->tgid;
1040 fl->cctx = cctx;
1042 fl->sctx = fastrpc_session_alloc(cctx);
1043 if (!fl->sctx) {
1044 dev_err(&cctx->rpdev->dev, "No session available\n");
1045 mutex_destroy(&fl->mutex);
1046 kfree(fl);
1048 return -EBUSY;
1051 spin_lock(&cctx->lock);
1052 list_add_tail(&fl->user, &cctx->users);
1053 spin_unlock(&cctx->lock);
1055 return 0;
1058 static int fastrpc_dmabuf_free(struct fastrpc_user *fl, char __user *argp)
1060 struct dma_buf *buf;
1061 int info;
1063 if (copy_from_user(&info, argp, sizeof(info)))
1064 return -EFAULT;
1066 buf = dma_buf_get(info);
1067 if (IS_ERR_OR_NULL(buf))
1068 return -EINVAL;
1070 * one for the last get and other for the ALLOC_DMA_BUFF ioctl
1072 dma_buf_put(buf);
1073 dma_buf_put(buf);
1075 return 0;
1078 static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
1080 struct fastrpc_alloc_dma_buf bp;
1081 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
1082 struct fastrpc_buf *buf = NULL;
1083 int err;
1085 if (copy_from_user(&bp, argp, sizeof(bp)))
1086 return -EFAULT;
1088 err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf);
1089 if (err)
1090 return err;
1091 exp_info.ops = &fastrpc_dma_buf_ops;
1092 exp_info.size = bp.size;
1093 exp_info.flags = O_RDWR;
1094 exp_info.priv = buf;
1095 buf->dmabuf = dma_buf_export(&exp_info);
1096 if (IS_ERR(buf->dmabuf)) {
1097 err = PTR_ERR(buf->dmabuf);
1098 fastrpc_buf_free(buf);
1099 return err;
1102 bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE);
1103 if (bp.fd < 0) {
1104 dma_buf_put(buf->dmabuf);
1105 return -EINVAL;
1108 if (copy_to_user(argp, &bp, sizeof(bp))) {
1109 dma_buf_put(buf->dmabuf);
1110 return -EFAULT;
1113 get_dma_buf(buf->dmabuf);
1115 return 0;
1118 static int fastrpc_init_attach(struct fastrpc_user *fl)
1120 struct fastrpc_invoke_args args[1];
1121 int tgid = fl->tgid;
1122 u32 sc;
1124 args[0].ptr = (u64)(uintptr_t) &tgid;
1125 args[0].length = sizeof(tgid);
1126 args[0].fd = -1;
1127 args[0].reserved = 0;
1128 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0);
1129 fl->pd = 0;
1131 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1132 sc, &args[0]);
1135 static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp)
1137 struct fastrpc_invoke_args *args = NULL;
1138 struct fastrpc_invoke inv;
1139 u32 nscalars;
1140 int err;
1142 if (copy_from_user(&inv, argp, sizeof(inv)))
1143 return -EFAULT;
1145 /* nscalars is truncated here to max supported value */
1146 nscalars = REMOTE_SCALARS_LENGTH(inv.sc);
1147 if (nscalars) {
1148 args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL);
1149 if (!args)
1150 return -ENOMEM;
1152 if (copy_from_user(args, (void __user *)(uintptr_t)inv.args,
1153 nscalars * sizeof(*args))) {
1154 kfree(args);
1155 return -EFAULT;
1159 err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args);
1160 kfree(args);
1162 return err;
1165 static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
1166 unsigned long arg)
1168 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1169 char __user *argp = (char __user *)arg;
1170 int err;
1172 switch (cmd) {
1173 case FASTRPC_IOCTL_INVOKE:
1174 err = fastrpc_invoke(fl, argp);
1175 break;
1176 case FASTRPC_IOCTL_INIT_ATTACH:
1177 err = fastrpc_init_attach(fl);
1178 break;
1179 case FASTRPC_IOCTL_INIT_CREATE:
1180 err = fastrpc_init_create_process(fl, argp);
1181 break;
1182 case FASTRPC_IOCTL_FREE_DMA_BUFF:
1183 err = fastrpc_dmabuf_free(fl, argp);
1184 break;
1185 case FASTRPC_IOCTL_ALLOC_DMA_BUFF:
1186 err = fastrpc_dmabuf_alloc(fl, argp);
1187 break;
1188 default:
1189 err = -ENOTTY;
1190 break;
1193 return err;
1196 static const struct file_operations fastrpc_fops = {
1197 .open = fastrpc_device_open,
1198 .release = fastrpc_device_release,
1199 .unlocked_ioctl = fastrpc_device_ioctl,
1200 .compat_ioctl = fastrpc_device_ioctl,
1203 static int fastrpc_cb_probe(struct platform_device *pdev)
1205 struct fastrpc_channel_ctx *cctx;
1206 struct fastrpc_session_ctx *sess;
1207 struct device *dev = &pdev->dev;
1208 int i, sessions = 0;
1209 int rc;
1211 cctx = dev_get_drvdata(dev->parent);
1212 if (!cctx)
1213 return -EINVAL;
1215 of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions);
1217 spin_lock(&cctx->lock);
1218 sess = &cctx->session[cctx->sesscount];
1219 sess->used = false;
1220 sess->valid = true;
1221 sess->dev = dev;
1222 dev_set_drvdata(dev, sess);
1224 if (of_property_read_u32(dev->of_node, "reg", &sess->sid))
1225 dev_info(dev, "FastRPC Session ID not specified in DT\n");
1227 if (sessions > 0) {
1228 struct fastrpc_session_ctx *dup_sess;
1230 for (i = 1; i < sessions; i++) {
1231 if (cctx->sesscount++ >= FASTRPC_MAX_SESSIONS)
1232 break;
1233 dup_sess = &cctx->session[cctx->sesscount];
1234 memcpy(dup_sess, sess, sizeof(*dup_sess));
1237 cctx->sesscount++;
1238 spin_unlock(&cctx->lock);
1239 rc = dma_set_mask(dev, DMA_BIT_MASK(32));
1240 if (rc) {
1241 dev_err(dev, "32-bit DMA enable failed\n");
1242 return rc;
1245 return 0;
1248 static int fastrpc_cb_remove(struct platform_device *pdev)
1250 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent);
1251 struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev);
1252 int i;
1254 spin_lock(&cctx->lock);
1255 for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) {
1256 if (cctx->session[i].sid == sess->sid) {
1257 cctx->session[i].valid = false;
1258 cctx->sesscount--;
1261 spin_unlock(&cctx->lock);
1263 return 0;
1266 static const struct of_device_id fastrpc_match_table[] = {
1267 { .compatible = "qcom,fastrpc-compute-cb", },
1271 static struct platform_driver fastrpc_cb_driver = {
1272 .probe = fastrpc_cb_probe,
1273 .remove = fastrpc_cb_remove,
1274 .driver = {
1275 .name = "qcom,fastrpc-cb",
1276 .of_match_table = fastrpc_match_table,
1277 .suppress_bind_attrs = true,
1281 static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
1283 struct device *rdev = &rpdev->dev;
1284 struct fastrpc_channel_ctx *data;
1285 int i, err, domain_id = -1;
1286 const char *domain;
1288 data = devm_kzalloc(rdev, sizeof(*data), GFP_KERNEL);
1289 if (!data)
1290 return -ENOMEM;
1292 err = of_property_read_string(rdev->of_node, "label", &domain);
1293 if (err) {
1294 dev_info(rdev, "FastRPC Domain not specified in DT\n");
1295 return err;
1298 for (i = 0; i <= CDSP_DOMAIN_ID; i++) {
1299 if (!strcmp(domains[i], domain)) {
1300 domain_id = i;
1301 break;
1305 if (domain_id < 0) {
1306 dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id);
1307 return -EINVAL;
1310 data->miscdev.minor = MISC_DYNAMIC_MINOR;
1311 data->miscdev.name = kasprintf(GFP_KERNEL, "fastrpc-%s",
1312 domains[domain_id]);
1313 data->miscdev.fops = &fastrpc_fops;
1314 err = misc_register(&data->miscdev);
1315 if (err)
1316 return err;
1318 dev_set_drvdata(&rpdev->dev, data);
1319 dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
1320 INIT_LIST_HEAD(&data->users);
1321 spin_lock_init(&data->lock);
1322 idr_init(&data->ctx_idr);
1323 data->domain_id = domain_id;
1324 data->rpdev = rpdev;
1326 return of_platform_populate(rdev->of_node, NULL, NULL, rdev);
1329 static void fastrpc_notify_users(struct fastrpc_user *user)
1331 struct fastrpc_invoke_ctx *ctx;
1333 spin_lock(&user->lock);
1334 list_for_each_entry(ctx, &user->pending, node)
1335 complete(&ctx->work);
1336 spin_unlock(&user->lock);
1339 static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
1341 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
1342 struct fastrpc_user *user;
1344 spin_lock(&cctx->lock);
1345 list_for_each_entry(user, &cctx->users, user)
1346 fastrpc_notify_users(user);
1347 spin_unlock(&cctx->lock);
1349 misc_deregister(&cctx->miscdev);
1350 of_platform_depopulate(&rpdev->dev);
1351 kfree(cctx);
1354 static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
1355 int len, void *priv, u32 addr)
1357 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
1358 struct fastrpc_invoke_rsp *rsp = data;
1359 struct fastrpc_invoke_ctx *ctx;
1360 unsigned long flags;
1361 unsigned long ctxid;
1363 if (len < sizeof(*rsp))
1364 return -EINVAL;
1366 ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4);
1368 spin_lock_irqsave(&cctx->lock, flags);
1369 ctx = idr_find(&cctx->ctx_idr, ctxid);
1370 spin_unlock_irqrestore(&cctx->lock, flags);
1372 if (!ctx) {
1373 dev_err(&rpdev->dev, "No context ID matches response\n");
1374 return -ENOENT;
1377 ctx->retval = rsp->retval;
1378 complete(&ctx->work);
1379 fastrpc_context_put(ctx);
1381 return 0;
1384 static const struct of_device_id fastrpc_rpmsg_of_match[] = {
1385 { .compatible = "qcom,fastrpc" },
1386 { },
1388 MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match);
1390 static struct rpmsg_driver fastrpc_driver = {
1391 .probe = fastrpc_rpmsg_probe,
1392 .remove = fastrpc_rpmsg_remove,
1393 .callback = fastrpc_rpmsg_callback,
1394 .drv = {
1395 .name = "qcom,fastrpc",
1396 .of_match_table = fastrpc_rpmsg_of_match,
1400 static int fastrpc_init(void)
1402 int ret;
1404 ret = platform_driver_register(&fastrpc_cb_driver);
1405 if (ret < 0) {
1406 pr_err("fastrpc: failed to register cb driver\n");
1407 return ret;
1410 ret = register_rpmsg_driver(&fastrpc_driver);
1411 if (ret < 0) {
1412 pr_err("fastrpc: failed to register rpmsg driver\n");
1413 platform_driver_unregister(&fastrpc_cb_driver);
1414 return ret;
1417 return 0;
1419 module_init(fastrpc_init);
1421 static void fastrpc_exit(void)
1423 platform_driver_unregister(&fastrpc_cb_driver);
1424 unregister_rpmsg_driver(&fastrpc_driver);
1426 module_exit(fastrpc_exit);
1428 MODULE_LICENSE("GPL v2");