misc: fastrpc: Add support for context Invoke method
[linux-2.6-block.git] / drivers / misc / fastrpc.c
CommitLineData
f6f9279f
SK
1// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
3// Copyright (c) 2018, Linaro Limited
4
c68cfb71 5#include <linux/completion.h>
f6f9279f 6#include <linux/device.h>
c68cfb71 7#include <linux/dma-buf.h>
f6f9279f
SK
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>
c68cfb71 19#include <uapi/misc/fastrpc.h>
f6f9279f
SK
20
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*/
c68cfb71
SK
27#define FASTRPC_ALIGN 128
28#define FASTRPC_MAX_FDLIST 16
29#define FASTRPC_MAX_CRCLIST 64
30#define FASTRPC_PHYS(p) ((p) & 0xffffffff)
f6f9279f
SK
31#define FASTRPC_CTX_MAX (256)
32#define FASTRPC_CTXID_MASK (0xFF0)
33#define FASTRPC_DEVICE_NAME "fastrpc"
34
c68cfb71
SK
35/* Retrives number of input buffers from the scalars parameter */
36#define REMOTE_SCALARS_INBUFS(sc) (((sc) >> 16) & 0x0ff)
37
38/* Retrives number of output buffers from the scalars parameter */
39#define REMOTE_SCALARS_OUTBUFS(sc) (((sc) >> 8) & 0x0ff)
40
41/* Retrives number of input handles from the scalars parameter */
42#define REMOTE_SCALARS_INHANDLES(sc) (((sc) >> 4) & 0x0f)
43
44/* Retrives number of output handles from the scalars parameter */
45#define REMOTE_SCALARS_OUTHANDLES(sc) ((sc) & 0x0f)
46
47#define REMOTE_SCALARS_LENGTH(sc) (REMOTE_SCALARS_INBUFS(sc) + \
48 REMOTE_SCALARS_OUTBUFS(sc) + \
49 REMOTE_SCALARS_INHANDLES(sc)+ \
50 REMOTE_SCALARS_OUTHANDLES(sc))
51#define FASTRPC_BUILD_SCALARS(attr, method, in, out, oin, oout) \
52 (((attr & 0x07) << 29) | \
53 ((method & 0x1f) << 24) | \
54 ((in & 0xff) << 16) | \
55 ((out & 0xff) << 8) | \
56 ((oin & 0x0f) << 4) | \
57 (oout & 0x0f))
58
59#define FASTRPC_SCALARS(method, in, out) \
60 FASTRPC_BUILD_SCALARS(0, method, in, out, 0, 0)
61
f6f9279f
SK
62#define miscdev_to_cctx(d) container_of(d, struct fastrpc_channel_ctx, miscdev)
63
64static const char *domains[FASTRPC_DEV_MAX] = { "adsp", "mdsp",
65 "sdsp", "cdsp"};
c68cfb71
SK
66struct fastrpc_phy_page {
67 u64 addr; /* physical address */
68 u64 size; /* size of contiguous region */
69};
70
71struct fastrpc_invoke_buf {
72 u32 num; /* number of contiguous regions */
73 u32 pgidx; /* index to start of contiguous region */
74};
75
76struct fastrpc_remote_arg {
77 u64 pv;
78 u64 len;
79};
80
81struct fastrpc_msg {
82 int pid; /* process group id */
83 int tid; /* thread id */
84 u64 ctx; /* invoke caller context */
85 u32 handle; /* handle to invoke */
86 u32 sc; /* scalars structure describing the data */
87 u64 addr; /* physical address */
88 u64 size; /* size of contiguous region */
89};
90
91struct fastrpc_invoke_rsp {
92 u64 ctx; /* invoke caller context */
93 int retval; /* invoke return value */
94};
95
96struct fastrpc_buf {
97 struct fastrpc_user *fl;
98 struct device *dev;
99 void *virt;
100 u64 phys;
101 u64 size;
102};
103
104struct fastrpc_map {
105 struct list_head node;
106 struct fastrpc_user *fl;
107 int fd;
108 struct dma_buf *buf;
109 struct sg_table *table;
110 struct dma_buf_attachment *attach;
111 u64 phys;
112 u64 size;
113 void *va;
114 u64 len;
115 struct kref refcount;
116};
117
118struct fastrpc_invoke_ctx {
119 int nscalars;
120 int nbufs;
121 int retval;
122 int pid;
123 int tgid;
124 u32 sc;
125 u32 *crc;
126 u64 ctxid;
127 u64 msg_sz;
128 struct kref refcount;
129 struct list_head node; /* list of ctxs */
130 struct completion work;
131 struct fastrpc_msg msg;
132 struct fastrpc_user *fl;
133 struct fastrpc_remote_arg *rpra;
134 struct fastrpc_map **maps;
135 struct fastrpc_buf *buf;
136 struct fastrpc_invoke_args *args;
137 struct fastrpc_channel_ctx *cctx;
138};
f6f9279f
SK
139
140struct fastrpc_session_ctx {
141 struct device *dev;
142 int sid;
143 bool used;
144 bool valid;
145};
146
147struct fastrpc_channel_ctx {
148 int domain_id;
149 int sesscount;
150 struct rpmsg_device *rpdev;
151 struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS];
152 spinlock_t lock;
153 struct idr ctx_idr;
154 struct list_head users;
155 struct miscdevice miscdev;
156};
157
158struct fastrpc_user {
159 struct list_head user;
160 struct list_head maps;
161 struct list_head pending;
162
163 struct fastrpc_channel_ctx *cctx;
164 struct fastrpc_session_ctx *sctx;
c68cfb71 165 struct fastrpc_buf *init_mem;
f6f9279f
SK
166
167 int tgid;
168 int pd;
169 /* Lock for lists */
170 spinlock_t lock;
171 /* lock for allocations */
172 struct mutex mutex;
173};
174
c68cfb71
SK
175static void fastrpc_free_map(struct kref *ref)
176{
177 struct fastrpc_map *map;
178
179 map = container_of(ref, struct fastrpc_map, refcount);
180
181 if (map->table) {
182 dma_buf_unmap_attachment(map->attach, map->table,
183 DMA_BIDIRECTIONAL);
184 dma_buf_detach(map->buf, map->attach);
185 dma_buf_put(map->buf);
186 }
187
188 kfree(map);
189}
190
191static void fastrpc_map_put(struct fastrpc_map *map)
192{
193 if (map)
194 kref_put(&map->refcount, fastrpc_free_map);
195}
196
197static void fastrpc_map_get(struct fastrpc_map *map)
198{
199 if (map)
200 kref_get(&map->refcount);
201}
202
203static int fastrpc_map_find(struct fastrpc_user *fl, int fd,
204 struct fastrpc_map **ppmap)
205{
206 struct fastrpc_map *map = NULL;
207
208 mutex_lock(&fl->mutex);
209 list_for_each_entry(map, &fl->maps, node) {
210 if (map->fd == fd) {
211 fastrpc_map_get(map);
212 *ppmap = map;
213 mutex_unlock(&fl->mutex);
214 return 0;
215 }
216 }
217 mutex_unlock(&fl->mutex);
218
219 return -ENOENT;
220}
221
222static void fastrpc_buf_free(struct fastrpc_buf *buf)
223{
224 dma_free_coherent(buf->dev, buf->size, buf->virt,
225 FASTRPC_PHYS(buf->phys));
226 kfree(buf);
227}
228
229static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
230 u64 size, struct fastrpc_buf **obuf)
231{
232 struct fastrpc_buf *buf;
233
234 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
235 if (!buf)
236 return -ENOMEM;
237
238 buf->fl = fl;
239 buf->virt = NULL;
240 buf->phys = 0;
241 buf->size = size;
242 buf->dev = dev;
243
244 buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys,
245 GFP_KERNEL);
246 if (!buf->virt)
247 return -ENOMEM;
248
249 if (fl->sctx && fl->sctx->sid)
250 buf->phys += ((u64)fl->sctx->sid << 32);
251
252 *obuf = buf;
253
254 return 0;
255}
256
257static void fastrpc_context_free(struct kref *ref)
258{
259 struct fastrpc_invoke_ctx *ctx;
260 struct fastrpc_channel_ctx *cctx;
261 int i;
262
263 ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
264 cctx = ctx->cctx;
265
266 for (i = 0; i < ctx->nscalars; i++)
267 fastrpc_map_put(ctx->maps[i]);
268
269 if (ctx->buf)
270 fastrpc_buf_free(ctx->buf);
271
272 spin_lock(&cctx->lock);
273 idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4);
274 spin_unlock(&cctx->lock);
275
276 kfree(ctx->maps);
277 kfree(ctx);
278}
279
280static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx)
281{
282 kref_get(&ctx->refcount);
283}
284
285static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx)
286{
287 kref_put(&ctx->refcount, fastrpc_context_free);
288}
289
290static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
291 struct fastrpc_user *user, u32 kernel, u32 sc,
292 struct fastrpc_invoke_args *args)
293{
294 struct fastrpc_channel_ctx *cctx = user->cctx;
295 struct fastrpc_invoke_ctx *ctx = NULL;
296 int ret;
297
298 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
299 if (!ctx)
300 return ERR_PTR(-ENOMEM);
301
302 INIT_LIST_HEAD(&ctx->node);
303 ctx->fl = user;
304 ctx->nscalars = REMOTE_SCALARS_LENGTH(sc);
305 ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) +
306 REMOTE_SCALARS_OUTBUFS(sc);
307
308 if (ctx->nscalars) {
309 ctx->maps = kcalloc(ctx->nscalars,
310 sizeof(*ctx->maps), GFP_KERNEL);
311 if (!ctx->maps) {
312 kfree(ctx);
313 return ERR_PTR(-ENOMEM);
314 }
315 ctx->args = args;
316 }
317
318 ctx->sc = sc;
319 ctx->retval = -1;
320 ctx->pid = current->pid;
321 ctx->tgid = user->tgid;
322 ctx->cctx = cctx;
323 init_completion(&ctx->work);
324
325 spin_lock(&user->lock);
326 list_add_tail(&ctx->node, &user->pending);
327 spin_unlock(&user->lock);
328
329 spin_lock(&cctx->lock);
330 ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1,
331 FASTRPC_CTX_MAX, GFP_ATOMIC);
332 if (ret < 0) {
333 spin_unlock(&cctx->lock);
334 goto err_idr;
335 }
336 ctx->ctxid = ret << 4;
337 spin_unlock(&cctx->lock);
338
339 kref_init(&ctx->refcount);
340
341 return ctx;
342err_idr:
343 spin_lock(&user->lock);
344 list_del(&ctx->node);
345 spin_unlock(&user->lock);
346 kfree(ctx->maps);
347 kfree(ctx);
348
349 return ERR_PTR(ret);
350}
351
352static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
353 u64 len, struct fastrpc_map **ppmap)
354{
355 struct fastrpc_session_ctx *sess = fl->sctx;
356 struct fastrpc_map *map = NULL;
357 int err = 0;
358
359 if (!fastrpc_map_find(fl, fd, ppmap))
360 return 0;
361
362 map = kzalloc(sizeof(*map), GFP_KERNEL);
363 if (!map)
364 return -ENOMEM;
365
366 INIT_LIST_HEAD(&map->node);
367 map->fl = fl;
368 map->fd = fd;
369 map->buf = dma_buf_get(fd);
370 if (!map->buf) {
371 err = -EINVAL;
372 goto get_err;
373 }
374
375 map->attach = dma_buf_attach(map->buf, sess->dev);
376 if (IS_ERR(map->attach)) {
377 dev_err(sess->dev, "Failed to attach dmabuf\n");
378 err = PTR_ERR(map->attach);
379 goto attach_err;
380 }
381
382 map->table = dma_buf_map_attachment(map->attach, DMA_BIDIRECTIONAL);
383 if (IS_ERR(map->table)) {
384 err = PTR_ERR(map->table);
385 goto map_err;
386 }
387
388 map->phys = sg_dma_address(map->table->sgl);
389 map->phys += ((u64)fl->sctx->sid << 32);
390 map->size = len;
391 map->va = sg_virt(map->table->sgl);
392 map->len = len;
393 kref_init(&map->refcount);
394
395 spin_lock(&fl->lock);
396 list_add_tail(&map->node, &fl->maps);
397 spin_unlock(&fl->lock);
398 *ppmap = map;
399
400 return 0;
401
402map_err:
403 dma_buf_detach(map->buf, map->attach);
404attach_err:
405 dma_buf_put(map->buf);
406get_err:
407 kfree(map);
408
409 return err;
410}
411
412/*
413 * Fastrpc payload buffer with metadata looks like:
414 *
415 * >>>>>> START of METADATA <<<<<<<<<
416 * +---------------------------------+
417 * | Arguments |
418 * | type:(struct fastrpc_remote_arg)|
419 * | (0 - N) |
420 * +---------------------------------+
421 * | Invoke Buffer list |
422 * | type:(struct fastrpc_invoke_buf)|
423 * | (0 - N) |
424 * +---------------------------------+
425 * | Page info list |
426 * | type:(struct fastrpc_phy_page) |
427 * | (0 - N) |
428 * +---------------------------------+
429 * | Optional info |
430 * |(can be specific to SoC/Firmware)|
431 * +---------------------------------+
432 * >>>>>>>> END of METADATA <<<<<<<<<
433 * +---------------------------------+
434 * | Inline ARGS |
435 * | (0-N) |
436 * +---------------------------------+
437 */
438
439static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx)
440{
441 int size = 0;
442
443 size = (sizeof(struct fastrpc_remote_arg) +
444 sizeof(struct fastrpc_invoke_buf) +
445 sizeof(struct fastrpc_phy_page)) * ctx->nscalars +
446 sizeof(u64) * FASTRPC_MAX_FDLIST +
447 sizeof(u32) * FASTRPC_MAX_CRCLIST;
448
449 return size;
450}
451
452static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen)
453{
454 u64 size = 0;
455 int i;
456
457 size = ALIGN(metalen, FASTRPC_ALIGN);
458 for (i = 0; i < ctx->nscalars; i++) {
459 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) {
460 size = ALIGN(size, FASTRPC_ALIGN);
461 size += ctx->args[i].length;
462 }
463 }
464
465 return size;
466}
467
468static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
469{
470 struct device *dev = ctx->fl->sctx->dev;
471 int i, err;
472
473 for (i = 0; i < ctx->nscalars; ++i) {
474 /* Make sure reserved field is set to 0 */
475 if (ctx->args[i].reserved)
476 return -EINVAL;
477
478 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 ||
479 ctx->args[i].length == 0)
480 continue;
481
482 err = fastrpc_map_create(ctx->fl, ctx->args[i].fd,
483 ctx->args[i].length, &ctx->maps[i]);
484 if (err) {
485 dev_err(dev, "Error Creating map %d\n", err);
486 return -EINVAL;
487 }
488
489 }
490 return 0;
491}
492
493static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
494{
495 struct device *dev = ctx->fl->sctx->dev;
496 struct fastrpc_remote_arg *rpra;
497 struct fastrpc_invoke_buf *list;
498 struct fastrpc_phy_page *pages;
499 int inbufs, i, err = 0;
500 u64 rlen, pkt_size;
501 uintptr_t args;
502 int metalen;
503
504
505 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
506 metalen = fastrpc_get_meta_size(ctx);
507 pkt_size = fastrpc_get_payload_size(ctx, metalen);
508
509 err = fastrpc_create_maps(ctx);
510 if (err)
511 return err;
512
513 ctx->msg_sz = pkt_size;
514
515 err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf);
516 if (err)
517 return err;
518
519 rpra = ctx->buf->virt;
520 list = ctx->buf->virt + ctx->nscalars * sizeof(*rpra);
521 pages = ctx->buf->virt + ctx->nscalars * (sizeof(*list) +
522 sizeof(*rpra));
523 args = (uintptr_t)ctx->buf->virt + metalen;
524 rlen = pkt_size - metalen;
525 ctx->rpra = rpra;
526
527 for (i = 0; i < ctx->nbufs; ++i) {
528 u64 len = ctx->args[i].length;
529
530 rpra[i].pv = 0;
531 rpra[i].len = len;
532 list[i].num = len ? 1 : 0;
533 list[i].pgidx = i;
534
535 if (!len)
536 continue;
537
538 pages[i].size = roundup(len, PAGE_SIZE);
539
540 if (ctx->maps[i]) {
541 rpra[i].pv = (u64) ctx->args[i].ptr;
542 pages[i].addr = ctx->maps[i]->phys;
543 } else {
544 rlen -= ALIGN(args, FASTRPC_ALIGN) - args;
545 args = ALIGN(args, FASTRPC_ALIGN);
546 if (rlen < len)
547 goto bail;
548
549 rpra[i].pv = args;
550 pages[i].addr = ctx->buf->phys + (pkt_size - rlen);
551 pages[i].addr = pages[i].addr & PAGE_MASK;
552 args = args + len;
553 rlen -= len;
554 }
555
556 if (i < inbufs && !ctx->maps[i]) {
557 void *dst = (void *)(uintptr_t)rpra[i].pv;
558 void *src = (void *)(uintptr_t)ctx->args[i].ptr;
559
560 if (!kernel) {
561 if (copy_from_user(dst, (void __user *)src,
562 len)) {
563 err = -EFAULT;
564 goto bail;
565 }
566 } else {
567 memcpy(dst, src, len);
568 }
569 }
570 }
571
572 for (i = ctx->nbufs; i < ctx->nscalars; ++i) {
573 rpra[i].pv = (u64) ctx->args[i].ptr;
574 rpra[i].len = ctx->args[i].length;
575 list[i].num = ctx->args[i].length ? 1 : 0;
576 list[i].pgidx = i;
577 pages[i].addr = ctx->maps[i]->phys;
578 pages[i].size = ctx->maps[i]->size;
579 }
580
581bail:
582 if (err)
583 dev_err(dev, "Error: get invoke args failed:%d\n", err);
584
585 return err;
586}
587
588static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
589 u32 kernel)
590{
591 struct fastrpc_remote_arg *rpra = ctx->rpra;
592 int i, inbufs;
593
594 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
595
596 for (i = inbufs; i < ctx->nbufs; ++i) {
597 void *src = (void *)(uintptr_t)rpra[i].pv;
598 void *dst = (void *)(uintptr_t)ctx->args[i].ptr;
599 u64 len = rpra[i].len;
600
601 if (!kernel) {
602 if (copy_to_user((void __user *)dst, src, len))
603 return -EFAULT;
604 } else {
605 memcpy(dst, src, len);
606 }
607 }
608
609 return 0;
610}
611
612static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx,
613 struct fastrpc_invoke_ctx *ctx,
614 u32 kernel, uint32_t handle)
615{
616 struct fastrpc_channel_ctx *cctx;
617 struct fastrpc_user *fl = ctx->fl;
618 struct fastrpc_msg *msg = &ctx->msg;
619
620 cctx = fl->cctx;
621 msg->pid = fl->tgid;
622 msg->tid = current->pid;
623
624 if (kernel)
625 msg->pid = 0;
626
627 msg->ctx = ctx->ctxid | fl->pd;
628 msg->handle = handle;
629 msg->sc = ctx->sc;
630 msg->addr = ctx->buf ? ctx->buf->phys : 0;
631 msg->size = roundup(ctx->msg_sz, PAGE_SIZE);
632 fastrpc_context_get(ctx);
633
634 return rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg));
635}
636
637static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
638 u32 handle, u32 sc,
639 struct fastrpc_invoke_args *args)
640{
641 struct fastrpc_invoke_ctx *ctx = NULL;
642 int err = 0;
643
644 if (!fl->sctx)
645 return -EINVAL;
646
647 ctx = fastrpc_context_alloc(fl, kernel, sc, args);
648 if (IS_ERR(ctx))
649 return PTR_ERR(ctx);
650
651 if (ctx->nscalars) {
652 err = fastrpc_get_args(kernel, ctx);
653 if (err)
654 goto bail;
655 }
656 /* Send invoke buffer to remote dsp */
657 err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle);
658 if (err)
659 goto bail;
660
661 /* Wait for remote dsp to respond or time out */
662 err = wait_for_completion_interruptible(&ctx->work);
663 if (err)
664 goto bail;
665
666 /* Check the response from remote dsp */
667 err = ctx->retval;
668 if (err)
669 goto bail;
670
671 if (ctx->nscalars) {
672 /* populate all the output buffers with results */
673 err = fastrpc_put_args(ctx, kernel);
674 if (err)
675 goto bail;
676 }
677
678bail:
679 /* We are done with this compute context, remove it from pending list */
680 spin_lock(&fl->lock);
681 list_del(&ctx->node);
682 spin_unlock(&fl->lock);
683 fastrpc_context_put(ctx);
684
685 if (err)
686 dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err);
687
688 return err;
689}
690
f6f9279f
SK
691static struct fastrpc_session_ctx *fastrpc_session_alloc(
692 struct fastrpc_channel_ctx *cctx)
693{
694 struct fastrpc_session_ctx *session = NULL;
695 int i;
696
697 spin_lock(&cctx->lock);
698 for (i = 0; i < cctx->sesscount; i++) {
699 if (!cctx->session[i].used && cctx->session[i].valid) {
700 cctx->session[i].used = true;
701 session = &cctx->session[i];
702 break;
703 }
704 }
705 spin_unlock(&cctx->lock);
706
707 return session;
708}
709
710static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx,
711 struct fastrpc_session_ctx *session)
712{
713 spin_lock(&cctx->lock);
714 session->used = false;
715 spin_unlock(&cctx->lock);
716}
717
718static int fastrpc_device_release(struct inode *inode, struct file *file)
719{
720 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
721 struct fastrpc_channel_ctx *cctx = fl->cctx;
c68cfb71
SK
722 struct fastrpc_invoke_ctx *ctx, *n;
723 struct fastrpc_map *map, *m;
f6f9279f
SK
724
725 spin_lock(&cctx->lock);
726 list_del(&fl->user);
727 spin_unlock(&cctx->lock);
728
c68cfb71
SK
729 if (fl->init_mem)
730 fastrpc_buf_free(fl->init_mem);
731
732 list_for_each_entry_safe(ctx, n, &fl->pending, node) {
733 list_del(&ctx->node);
734 fastrpc_context_put(ctx);
735 }
736
737 list_for_each_entry_safe(map, m, &fl->maps, node) {
738 list_del(&map->node);
739 fastrpc_map_put(map);
740 }
741
f6f9279f
SK
742 fastrpc_session_free(cctx, fl->sctx);
743
744 mutex_destroy(&fl->mutex);
745 kfree(fl);
746 file->private_data = NULL;
747
748 return 0;
749}
750
751static int fastrpc_device_open(struct inode *inode, struct file *filp)
752{
753 struct fastrpc_channel_ctx *cctx = miscdev_to_cctx(filp->private_data);
754 struct fastrpc_user *fl = NULL;
755
756 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
757 if (!fl)
758 return -ENOMEM;
759
760 filp->private_data = fl;
761 spin_lock_init(&fl->lock);
762 mutex_init(&fl->mutex);
763 INIT_LIST_HEAD(&fl->pending);
764 INIT_LIST_HEAD(&fl->maps);
765 INIT_LIST_HEAD(&fl->user);
766 fl->tgid = current->tgid;
767 fl->cctx = cctx;
768 spin_lock(&cctx->lock);
769 list_add_tail(&fl->user, &cctx->users);
770 spin_unlock(&cctx->lock);
771 fl->sctx = fastrpc_session_alloc(cctx);
772
773 return 0;
774}
775
c68cfb71
SK
776static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp)
777{
778 struct fastrpc_invoke_args *args = NULL;
779 struct fastrpc_invoke inv;
780 u32 nscalars;
781 int err;
782
783 if (copy_from_user(&inv, argp, sizeof(inv)))
784 return -EFAULT;
785
786 /* nscalars is truncated here to max supported value */
787 nscalars = REMOTE_SCALARS_LENGTH(inv.sc);
788 if (nscalars) {
789 args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL);
790 if (!args)
791 return -ENOMEM;
792
793 if (copy_from_user(args, (void __user *)(uintptr_t)inv.args,
794 nscalars * sizeof(*args))) {
795 kfree(args);
796 return -EFAULT;
797 }
798 }
799
800 err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args);
801 kfree(args);
802
803 return err;
804}
805
806static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
807 unsigned long arg)
808{
809 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
810 char __user *argp = (char __user *)arg;
811 int err;
812
813 switch (cmd) {
814 case FASTRPC_IOCTL_INVOKE:
815 err = fastrpc_invoke(fl, argp);
816 break;
817 default:
818 err = -ENOTTY;
819 break;
820 }
821
822 return err;
823}
824
f6f9279f
SK
825static const struct file_operations fastrpc_fops = {
826 .open = fastrpc_device_open,
827 .release = fastrpc_device_release,
c68cfb71
SK
828 .unlocked_ioctl = fastrpc_device_ioctl,
829 .compat_ioctl = fastrpc_device_ioctl,
f6f9279f
SK
830};
831
832static int fastrpc_cb_probe(struct platform_device *pdev)
833{
834 struct fastrpc_channel_ctx *cctx;
835 struct fastrpc_session_ctx *sess;
836 struct device *dev = &pdev->dev;
837 int i, sessions = 0;
838
839 cctx = dev_get_drvdata(dev->parent);
840 if (!cctx)
841 return -EINVAL;
842
843 of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions);
844
845 spin_lock(&cctx->lock);
846 sess = &cctx->session[cctx->sesscount];
847 sess->used = false;
848 sess->valid = true;
849 sess->dev = dev;
850 dev_set_drvdata(dev, sess);
851
852 if (of_property_read_u32(dev->of_node, "reg", &sess->sid))
853 dev_info(dev, "FastRPC Session ID not specified in DT\n");
854
855 if (sessions > 0) {
856 struct fastrpc_session_ctx *dup_sess;
857
858 for (i = 1; i < sessions; i++) {
859 if (cctx->sesscount++ >= FASTRPC_MAX_SESSIONS)
860 break;
861 dup_sess = &cctx->session[cctx->sesscount];
862 memcpy(dup_sess, sess, sizeof(*dup_sess));
863 }
864 }
865 cctx->sesscount++;
866 spin_unlock(&cctx->lock);
867 dma_set_mask(dev, DMA_BIT_MASK(32));
868
869 return 0;
870}
871
872static int fastrpc_cb_remove(struct platform_device *pdev)
873{
874 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent);
875 struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev);
876 int i;
877
878 spin_lock(&cctx->lock);
879 for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) {
880 if (cctx->session[i].sid == sess->sid) {
881 cctx->session[i].valid = false;
882 cctx->sesscount--;
883 }
884 }
885 spin_unlock(&cctx->lock);
886
887 return 0;
888}
889
890static const struct of_device_id fastrpc_match_table[] = {
891 { .compatible = "qcom,fastrpc-compute-cb", },
892 {}
893};
894
895static struct platform_driver fastrpc_cb_driver = {
896 .probe = fastrpc_cb_probe,
897 .remove = fastrpc_cb_remove,
898 .driver = {
899 .name = "qcom,fastrpc-cb",
900 .of_match_table = fastrpc_match_table,
901 .suppress_bind_attrs = true,
902 },
903};
904
905static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
906{
907 struct device *rdev = &rpdev->dev;
908 struct fastrpc_channel_ctx *data;
909 int i, err, domain_id = -1;
910 const char *domain;
911
912 data = devm_kzalloc(rdev, sizeof(*data), GFP_KERNEL);
913 if (!data)
914 return -ENOMEM;
915
916 err = of_property_read_string(rdev->of_node, "label", &domain);
917 if (err) {
918 dev_info(rdev, "FastRPC Domain not specified in DT\n");
919 return err;
920 }
921
922 for (i = 0; i <= CDSP_DOMAIN_ID; i++) {
923 if (!strcmp(domains[i], domain)) {
924 domain_id = i;
925 break;
926 }
927 }
928
929 if (domain_id < 0) {
930 dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id);
931 return -EINVAL;
932 }
933
934 data->miscdev.minor = MISC_DYNAMIC_MINOR;
935 data->miscdev.name = kasprintf(GFP_KERNEL, "fastrpc-%s",
936 domains[domain_id]);
937 data->miscdev.fops = &fastrpc_fops;
938 err = misc_register(&data->miscdev);
939 if (err)
940 return err;
941
942 dev_set_drvdata(&rpdev->dev, data);
943 dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
944 INIT_LIST_HEAD(&data->users);
945 spin_lock_init(&data->lock);
946 idr_init(&data->ctx_idr);
947 data->domain_id = domain_id;
948 data->rpdev = rpdev;
949
950 return of_platform_populate(rdev->of_node, NULL, NULL, rdev);
951}
952
c68cfb71
SK
953static void fastrpc_notify_users(struct fastrpc_user *user)
954{
955 struct fastrpc_invoke_ctx *ctx;
956
957 spin_lock(&user->lock);
958 list_for_each_entry(ctx, &user->pending, node)
959 complete(&ctx->work);
960 spin_unlock(&user->lock);
961}
962
f6f9279f
SK
963static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
964{
965 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
c68cfb71
SK
966 struct fastrpc_user *user;
967
968 spin_lock(&cctx->lock);
969 list_for_each_entry(user, &cctx->users, user)
970 fastrpc_notify_users(user);
971 spin_unlock(&cctx->lock);
f6f9279f
SK
972
973 misc_deregister(&cctx->miscdev);
974 of_platform_depopulate(&rpdev->dev);
975 kfree(cctx);
976}
977
978static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
979 int len, void *priv, u32 addr)
980{
c68cfb71
SK
981 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
982 struct fastrpc_invoke_rsp *rsp = data;
983 struct fastrpc_invoke_ctx *ctx;
984 unsigned long flags;
985 unsigned long ctxid;
986
987 if (len < sizeof(*rsp))
988 return -EINVAL;
989
990 ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4);
991
992 spin_lock_irqsave(&cctx->lock, flags);
993 ctx = idr_find(&cctx->ctx_idr, ctxid);
994 spin_unlock_irqrestore(&cctx->lock, flags);
995
996 if (!ctx) {
997 dev_err(&rpdev->dev, "No context ID matches response\n");
998 return -ENOENT;
999 }
1000
1001 ctx->retval = rsp->retval;
1002 complete(&ctx->work);
1003 fastrpc_context_put(ctx);
1004
f6f9279f
SK
1005 return 0;
1006}
1007
1008static const struct of_device_id fastrpc_rpmsg_of_match[] = {
1009 { .compatible = "qcom,fastrpc" },
1010 { },
1011};
1012MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match);
1013
1014static struct rpmsg_driver fastrpc_driver = {
1015 .probe = fastrpc_rpmsg_probe,
1016 .remove = fastrpc_rpmsg_remove,
1017 .callback = fastrpc_rpmsg_callback,
1018 .drv = {
1019 .name = "qcom,fastrpc",
1020 .of_match_table = fastrpc_rpmsg_of_match,
1021 },
1022};
1023
1024static int fastrpc_init(void)
1025{
1026 int ret;
1027
1028 ret = platform_driver_register(&fastrpc_cb_driver);
1029 if (ret < 0) {
1030 pr_err("fastrpc: failed to register cb driver\n");
1031 return ret;
1032 }
1033
1034 ret = register_rpmsg_driver(&fastrpc_driver);
1035 if (ret < 0) {
1036 pr_err("fastrpc: failed to register rpmsg driver\n");
1037 platform_driver_unregister(&fastrpc_cb_driver);
1038 return ret;
1039 }
1040
1041 return 0;
1042}
1043module_init(fastrpc_init);
1044
1045static void fastrpc_exit(void)
1046{
1047 platform_driver_unregister(&fastrpc_cb_driver);
1048 unregister_rpmsg_driver(&fastrpc_driver);
1049}
1050module_exit(fastrpc_exit);
1051
1052MODULE_LICENSE("GPL v2");