misc: fastrpc: Fix a possible double free
[linux-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 31#define FASTRPC_CTX_MAX (256)
d73f71c7 32#define FASTRPC_INIT_HANDLE 1
f6f9279f 33#define FASTRPC_CTXID_MASK (0xFF0)
d73f71c7
SK
34#define INIT_FILELEN_MAX (2 * 1024 * 1024)
35#define INIT_MEMLEN_MAX (8 * 1024 * 1024)
f6f9279f
SK
36#define FASTRPC_DEVICE_NAME "fastrpc"
37
c68cfb71
SK
38/* Retrives number of input buffers from the scalars parameter */
39#define REMOTE_SCALARS_INBUFS(sc) (((sc) >> 16) & 0x0ff)
40
41/* Retrives number of output buffers from the scalars parameter */
42#define REMOTE_SCALARS_OUTBUFS(sc) (((sc) >> 8) & 0x0ff)
43
44/* Retrives number of input handles from the scalars parameter */
45#define REMOTE_SCALARS_INHANDLES(sc) (((sc) >> 4) & 0x0f)
46
47/* Retrives number of output handles from the scalars parameter */
48#define REMOTE_SCALARS_OUTHANDLES(sc) ((sc) & 0x0f)
49
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))
61
62#define FASTRPC_SCALARS(method, in, out) \
63 FASTRPC_BUILD_SCALARS(0, method, in, out, 0, 0)
64
d73f71c7
SK
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
72
f6f9279f
SK
73#define miscdev_to_cctx(d) container_of(d, struct fastrpc_channel_ctx, miscdev)
74
75static const char *domains[FASTRPC_DEV_MAX] = { "adsp", "mdsp",
76 "sdsp", "cdsp"};
c68cfb71
SK
77struct fastrpc_phy_page {
78 u64 addr; /* physical address */
79 u64 size; /* size of contiguous region */
80};
81
82struct fastrpc_invoke_buf {
83 u32 num; /* number of contiguous regions */
84 u32 pgidx; /* index to start of contiguous region */
85};
86
87struct fastrpc_remote_arg {
88 u64 pv;
89 u64 len;
90};
91
92struct 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 */
100};
101
102struct fastrpc_invoke_rsp {
103 u64 ctx; /* invoke caller context */
104 int retval; /* invoke return value */
105};
106
107struct fastrpc_buf {
108 struct fastrpc_user *fl;
6cffd795 109 struct dma_buf *dmabuf;
c68cfb71
SK
110 struct device *dev;
111 void *virt;
112 u64 phys;
113 u64 size;
6cffd795
SK
114 /* Lock for dma buf attachments */
115 struct mutex lock;
116 struct list_head attachments;
117};
118
119struct fastrpc_dma_buf_attachment {
120 struct device *dev;
121 struct sg_table sgt;
122 struct list_head node;
c68cfb71
SK
123};
124
125struct 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;
137};
138
139struct 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;
8e7389c7 152 struct work_struct put_work;
c68cfb71
SK
153 struct fastrpc_msg msg;
154 struct fastrpc_user *fl;
155 struct fastrpc_remote_arg *rpra;
156 struct fastrpc_map **maps;
157 struct fastrpc_buf *buf;
158 struct fastrpc_invoke_args *args;
159 struct fastrpc_channel_ctx *cctx;
160};
f6f9279f
SK
161
162struct fastrpc_session_ctx {
163 struct device *dev;
164 int sid;
165 bool used;
166 bool valid;
167};
168
169struct fastrpc_channel_ctx {
170 int domain_id;
171 int sesscount;
172 struct rpmsg_device *rpdev;
173 struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS];
174 spinlock_t lock;
175 struct idr ctx_idr;
176 struct list_head users;
177 struct miscdevice miscdev;
178};
179
180struct fastrpc_user {
181 struct list_head user;
182 struct list_head maps;
183 struct list_head pending;
184
185 struct fastrpc_channel_ctx *cctx;
186 struct fastrpc_session_ctx *sctx;
c68cfb71 187 struct fastrpc_buf *init_mem;
f6f9279f
SK
188
189 int tgid;
190 int pd;
191 /* Lock for lists */
192 spinlock_t lock;
193 /* lock for allocations */
194 struct mutex mutex;
195};
196
c68cfb71
SK
197static void fastrpc_free_map(struct kref *ref)
198{
199 struct fastrpc_map *map;
200
201 map = container_of(ref, struct fastrpc_map, refcount);
202
203 if (map->table) {
204 dma_buf_unmap_attachment(map->attach, map->table,
205 DMA_BIDIRECTIONAL);
206 dma_buf_detach(map->buf, map->attach);
207 dma_buf_put(map->buf);
208 }
209
210 kfree(map);
211}
212
213static void fastrpc_map_put(struct fastrpc_map *map)
214{
215 if (map)
216 kref_put(&map->refcount, fastrpc_free_map);
217}
218
219static void fastrpc_map_get(struct fastrpc_map *map)
220{
221 if (map)
222 kref_get(&map->refcount);
223}
224
225static int fastrpc_map_find(struct fastrpc_user *fl, int fd,
226 struct fastrpc_map **ppmap)
227{
228 struct fastrpc_map *map = NULL;
229
230 mutex_lock(&fl->mutex);
231 list_for_each_entry(map, &fl->maps, node) {
232 if (map->fd == fd) {
233 fastrpc_map_get(map);
234 *ppmap = map;
235 mutex_unlock(&fl->mutex);
236 return 0;
237 }
238 }
239 mutex_unlock(&fl->mutex);
240
241 return -ENOENT;
242}
243
244static void fastrpc_buf_free(struct fastrpc_buf *buf)
245{
246 dma_free_coherent(buf->dev, buf->size, buf->virt,
247 FASTRPC_PHYS(buf->phys));
248 kfree(buf);
249}
250
251static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
252 u64 size, struct fastrpc_buf **obuf)
253{
254 struct fastrpc_buf *buf;
255
256 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
257 if (!buf)
258 return -ENOMEM;
259
6cffd795
SK
260 INIT_LIST_HEAD(&buf->attachments);
261 mutex_init(&buf->lock);
262
c68cfb71
SK
263 buf->fl = fl;
264 buf->virt = NULL;
265 buf->phys = 0;
266 buf->size = size;
267 buf->dev = dev;
268
269 buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys,
270 GFP_KERNEL);
271 if (!buf->virt)
272 return -ENOMEM;
273
274 if (fl->sctx && fl->sctx->sid)
275 buf->phys += ((u64)fl->sctx->sid << 32);
276
277 *obuf = buf;
278
279 return 0;
280}
281
282static void fastrpc_context_free(struct kref *ref)
283{
284 struct fastrpc_invoke_ctx *ctx;
285 struct fastrpc_channel_ctx *cctx;
286 int i;
287
288 ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
289 cctx = ctx->cctx;
290
291 for (i = 0; i < ctx->nscalars; i++)
292 fastrpc_map_put(ctx->maps[i]);
293
294 if (ctx->buf)
295 fastrpc_buf_free(ctx->buf);
296
297 spin_lock(&cctx->lock);
298 idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4);
299 spin_unlock(&cctx->lock);
300
301 kfree(ctx->maps);
302 kfree(ctx);
303}
304
305static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx)
306{
307 kref_get(&ctx->refcount);
308}
309
310static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx)
311{
312 kref_put(&ctx->refcount, fastrpc_context_free);
313}
314
8e7389c7
TE
315static void fastrpc_context_put_wq(struct work_struct *work)
316{
317 struct fastrpc_invoke_ctx *ctx =
318 container_of(work, struct fastrpc_invoke_ctx, put_work);
319
320 fastrpc_context_put(ctx);
321}
322
c68cfb71
SK
323static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
324 struct fastrpc_user *user, u32 kernel, u32 sc,
325 struct fastrpc_invoke_args *args)
326{
327 struct fastrpc_channel_ctx *cctx = user->cctx;
328 struct fastrpc_invoke_ctx *ctx = NULL;
329 int ret;
330
331 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
332 if (!ctx)
333 return ERR_PTR(-ENOMEM);
334
335 INIT_LIST_HEAD(&ctx->node);
336 ctx->fl = user;
337 ctx->nscalars = REMOTE_SCALARS_LENGTH(sc);
338 ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) +
339 REMOTE_SCALARS_OUTBUFS(sc);
340
341 if (ctx->nscalars) {
342 ctx->maps = kcalloc(ctx->nscalars,
343 sizeof(*ctx->maps), GFP_KERNEL);
344 if (!ctx->maps) {
345 kfree(ctx);
346 return ERR_PTR(-ENOMEM);
347 }
348 ctx->args = args;
349 }
350
351 ctx->sc = sc;
352 ctx->retval = -1;
353 ctx->pid = current->pid;
354 ctx->tgid = user->tgid;
355 ctx->cctx = cctx;
356 init_completion(&ctx->work);
8e7389c7 357 INIT_WORK(&ctx->put_work, fastrpc_context_put_wq);
c68cfb71
SK
358
359 spin_lock(&user->lock);
360 list_add_tail(&ctx->node, &user->pending);
361 spin_unlock(&user->lock);
362
363 spin_lock(&cctx->lock);
364 ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1,
365 FASTRPC_CTX_MAX, GFP_ATOMIC);
366 if (ret < 0) {
367 spin_unlock(&cctx->lock);
368 goto err_idr;
369 }
370 ctx->ctxid = ret << 4;
371 spin_unlock(&cctx->lock);
372
373 kref_init(&ctx->refcount);
374
375 return ctx;
376err_idr:
377 spin_lock(&user->lock);
378 list_del(&ctx->node);
379 spin_unlock(&user->lock);
380 kfree(ctx->maps);
381 kfree(ctx);
382
383 return ERR_PTR(ret);
384}
385
6cffd795
SK
386static struct sg_table *
387fastrpc_map_dma_buf(struct dma_buf_attachment *attachment,
388 enum dma_data_direction dir)
389{
390 struct fastrpc_dma_buf_attachment *a = attachment->priv;
391 struct sg_table *table;
392
393 table = &a->sgt;
394
395 if (!dma_map_sg(attachment->dev, table->sgl, table->nents, dir))
396 return ERR_PTR(-ENOMEM);
397
398 return table;
399}
400
401static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach,
402 struct sg_table *table,
403 enum dma_data_direction dir)
404{
405 dma_unmap_sg(attach->dev, table->sgl, table->nents, dir);
406}
407
408static void fastrpc_release(struct dma_buf *dmabuf)
409{
410 struct fastrpc_buf *buffer = dmabuf->priv;
411
412 fastrpc_buf_free(buffer);
413}
414
415static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf,
416 struct dma_buf_attachment *attachment)
417{
418 struct fastrpc_dma_buf_attachment *a;
419 struct fastrpc_buf *buffer = dmabuf->priv;
420 int ret;
421
422 a = kzalloc(sizeof(*a), GFP_KERNEL);
423 if (!a)
424 return -ENOMEM;
425
426 ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt,
427 FASTRPC_PHYS(buffer->phys), buffer->size);
428 if (ret < 0) {
429 dev_err(buffer->dev, "failed to get scatterlist from DMA API\n");
430 return -EINVAL;
431 }
432
433 a->dev = attachment->dev;
434 INIT_LIST_HEAD(&a->node);
435 attachment->priv = a;
436
437 mutex_lock(&buffer->lock);
438 list_add(&a->node, &buffer->attachments);
439 mutex_unlock(&buffer->lock);
440
441 return 0;
442}
443
444static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf,
445 struct dma_buf_attachment *attachment)
446{
447 struct fastrpc_dma_buf_attachment *a = attachment->priv;
448 struct fastrpc_buf *buffer = dmabuf->priv;
449
450 mutex_lock(&buffer->lock);
451 list_del(&a->node);
452 mutex_unlock(&buffer->lock);
453 kfree(a);
454}
455
456static void *fastrpc_kmap(struct dma_buf *dmabuf, unsigned long pgnum)
457{
458 struct fastrpc_buf *buf = dmabuf->priv;
459
460 return buf->virt ? buf->virt + pgnum * PAGE_SIZE : NULL;
461}
462
463static void *fastrpc_vmap(struct dma_buf *dmabuf)
464{
465 struct fastrpc_buf *buf = dmabuf->priv;
466
467 return buf->virt;
468}
469
470static int fastrpc_mmap(struct dma_buf *dmabuf,
471 struct vm_area_struct *vma)
472{
473 struct fastrpc_buf *buf = dmabuf->priv;
474 size_t size = vma->vm_end - vma->vm_start;
475
476 return dma_mmap_coherent(buf->dev, vma, buf->virt,
477 FASTRPC_PHYS(buf->phys), size);
478}
479
480static const struct dma_buf_ops fastrpc_dma_buf_ops = {
481 .attach = fastrpc_dma_buf_attach,
482 .detach = fastrpc_dma_buf_detatch,
483 .map_dma_buf = fastrpc_map_dma_buf,
484 .unmap_dma_buf = fastrpc_unmap_dma_buf,
485 .mmap = fastrpc_mmap,
486 .map = fastrpc_kmap,
487 .vmap = fastrpc_vmap,
488 .release = fastrpc_release,
489};
490
c68cfb71
SK
491static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
492 u64 len, struct fastrpc_map **ppmap)
493{
494 struct fastrpc_session_ctx *sess = fl->sctx;
495 struct fastrpc_map *map = NULL;
496 int err = 0;
497
498 if (!fastrpc_map_find(fl, fd, ppmap))
499 return 0;
500
501 map = kzalloc(sizeof(*map), GFP_KERNEL);
502 if (!map)
503 return -ENOMEM;
504
505 INIT_LIST_HEAD(&map->node);
506 map->fl = fl;
507 map->fd = fd;
508 map->buf = dma_buf_get(fd);
682a6044
WY
509 if (IS_ERR(map->buf)) {
510 err = PTR_ERR(map->buf);
c68cfb71
SK
511 goto get_err;
512 }
513
514 map->attach = dma_buf_attach(map->buf, sess->dev);
515 if (IS_ERR(map->attach)) {
516 dev_err(sess->dev, "Failed to attach dmabuf\n");
517 err = PTR_ERR(map->attach);
518 goto attach_err;
519 }
520
521 map->table = dma_buf_map_attachment(map->attach, DMA_BIDIRECTIONAL);
522 if (IS_ERR(map->table)) {
523 err = PTR_ERR(map->table);
524 goto map_err;
525 }
526
527 map->phys = sg_dma_address(map->table->sgl);
528 map->phys += ((u64)fl->sctx->sid << 32);
529 map->size = len;
530 map->va = sg_virt(map->table->sgl);
531 map->len = len;
532 kref_init(&map->refcount);
533
534 spin_lock(&fl->lock);
535 list_add_tail(&map->node, &fl->maps);
536 spin_unlock(&fl->lock);
537 *ppmap = map;
538
539 return 0;
540
541map_err:
542 dma_buf_detach(map->buf, map->attach);
543attach_err:
544 dma_buf_put(map->buf);
545get_err:
546 kfree(map);
547
548 return err;
549}
550
551/*
552 * Fastrpc payload buffer with metadata looks like:
553 *
554 * >>>>>> START of METADATA <<<<<<<<<
555 * +---------------------------------+
556 * | Arguments |
557 * | type:(struct fastrpc_remote_arg)|
558 * | (0 - N) |
559 * +---------------------------------+
560 * | Invoke Buffer list |
561 * | type:(struct fastrpc_invoke_buf)|
562 * | (0 - N) |
563 * +---------------------------------+
564 * | Page info list |
565 * | type:(struct fastrpc_phy_page) |
566 * | (0 - N) |
567 * +---------------------------------+
568 * | Optional info |
569 * |(can be specific to SoC/Firmware)|
570 * +---------------------------------+
571 * >>>>>>>> END of METADATA <<<<<<<<<
572 * +---------------------------------+
573 * | Inline ARGS |
574 * | (0-N) |
575 * +---------------------------------+
576 */
577
578static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx)
579{
580 int size = 0;
581
582 size = (sizeof(struct fastrpc_remote_arg) +
583 sizeof(struct fastrpc_invoke_buf) +
584 sizeof(struct fastrpc_phy_page)) * ctx->nscalars +
585 sizeof(u64) * FASTRPC_MAX_FDLIST +
586 sizeof(u32) * FASTRPC_MAX_CRCLIST;
587
588 return size;
589}
590
591static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen)
592{
593 u64 size = 0;
594 int i;
595
596 size = ALIGN(metalen, FASTRPC_ALIGN);
597 for (i = 0; i < ctx->nscalars; i++) {
598 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) {
599 size = ALIGN(size, FASTRPC_ALIGN);
600 size += ctx->args[i].length;
601 }
602 }
603
604 return size;
605}
606
607static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
608{
609 struct device *dev = ctx->fl->sctx->dev;
610 int i, err;
611
612 for (i = 0; i < ctx->nscalars; ++i) {
613 /* Make sure reserved field is set to 0 */
614 if (ctx->args[i].reserved)
615 return -EINVAL;
616
617 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 ||
618 ctx->args[i].length == 0)
619 continue;
620
621 err = fastrpc_map_create(ctx->fl, ctx->args[i].fd,
622 ctx->args[i].length, &ctx->maps[i]);
623 if (err) {
624 dev_err(dev, "Error Creating map %d\n", err);
625 return -EINVAL;
626 }
627
628 }
629 return 0;
630}
631
632static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
633{
634 struct device *dev = ctx->fl->sctx->dev;
635 struct fastrpc_remote_arg *rpra;
636 struct fastrpc_invoke_buf *list;
637 struct fastrpc_phy_page *pages;
638 int inbufs, i, err = 0;
639 u64 rlen, pkt_size;
640 uintptr_t args;
641 int metalen;
642
643
644 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
645 metalen = fastrpc_get_meta_size(ctx);
646 pkt_size = fastrpc_get_payload_size(ctx, metalen);
647
648 err = fastrpc_create_maps(ctx);
649 if (err)
650 return err;
651
652 ctx->msg_sz = pkt_size;
653
654 err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf);
655 if (err)
656 return err;
657
658 rpra = ctx->buf->virt;
659 list = ctx->buf->virt + ctx->nscalars * sizeof(*rpra);
660 pages = ctx->buf->virt + ctx->nscalars * (sizeof(*list) +
661 sizeof(*rpra));
662 args = (uintptr_t)ctx->buf->virt + metalen;
663 rlen = pkt_size - metalen;
664 ctx->rpra = rpra;
665
666 for (i = 0; i < ctx->nbufs; ++i) {
667 u64 len = ctx->args[i].length;
668
669 rpra[i].pv = 0;
670 rpra[i].len = len;
671 list[i].num = len ? 1 : 0;
672 list[i].pgidx = i;
673
674 if (!len)
675 continue;
676
677 pages[i].size = roundup(len, PAGE_SIZE);
678
679 if (ctx->maps[i]) {
680 rpra[i].pv = (u64) ctx->args[i].ptr;
681 pages[i].addr = ctx->maps[i]->phys;
682 } else {
683 rlen -= ALIGN(args, FASTRPC_ALIGN) - args;
684 args = ALIGN(args, FASTRPC_ALIGN);
685 if (rlen < len)
686 goto bail;
687
688 rpra[i].pv = args;
689 pages[i].addr = ctx->buf->phys + (pkt_size - rlen);
690 pages[i].addr = pages[i].addr & PAGE_MASK;
691 args = args + len;
692 rlen -= len;
693 }
694
695 if (i < inbufs && !ctx->maps[i]) {
696 void *dst = (void *)(uintptr_t)rpra[i].pv;
697 void *src = (void *)(uintptr_t)ctx->args[i].ptr;
698
699 if (!kernel) {
700 if (copy_from_user(dst, (void __user *)src,
701 len)) {
702 err = -EFAULT;
703 goto bail;
704 }
705 } else {
706 memcpy(dst, src, len);
707 }
708 }
709 }
710
711 for (i = ctx->nbufs; i < ctx->nscalars; ++i) {
712 rpra[i].pv = (u64) ctx->args[i].ptr;
713 rpra[i].len = ctx->args[i].length;
714 list[i].num = ctx->args[i].length ? 1 : 0;
715 list[i].pgidx = i;
716 pages[i].addr = ctx->maps[i]->phys;
717 pages[i].size = ctx->maps[i]->size;
718 }
719
720bail:
721 if (err)
722 dev_err(dev, "Error: get invoke args failed:%d\n", err);
723
724 return err;
725}
726
727static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
728 u32 kernel)
729{
730 struct fastrpc_remote_arg *rpra = ctx->rpra;
731 int i, inbufs;
732
733 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
734
735 for (i = inbufs; i < ctx->nbufs; ++i) {
736 void *src = (void *)(uintptr_t)rpra[i].pv;
737 void *dst = (void *)(uintptr_t)ctx->args[i].ptr;
738 u64 len = rpra[i].len;
739
740 if (!kernel) {
741 if (copy_to_user((void __user *)dst, src, len))
742 return -EFAULT;
743 } else {
744 memcpy(dst, src, len);
745 }
746 }
747
748 return 0;
749}
750
751static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx,
752 struct fastrpc_invoke_ctx *ctx,
753 u32 kernel, uint32_t handle)
754{
755 struct fastrpc_channel_ctx *cctx;
756 struct fastrpc_user *fl = ctx->fl;
757 struct fastrpc_msg *msg = &ctx->msg;
758
759 cctx = fl->cctx;
760 msg->pid = fl->tgid;
761 msg->tid = current->pid;
762
763 if (kernel)
764 msg->pid = 0;
765
766 msg->ctx = ctx->ctxid | fl->pd;
767 msg->handle = handle;
768 msg->sc = ctx->sc;
769 msg->addr = ctx->buf ? ctx->buf->phys : 0;
770 msg->size = roundup(ctx->msg_sz, PAGE_SIZE);
771 fastrpc_context_get(ctx);
772
773 return rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg));
774}
775
776static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
777 u32 handle, u32 sc,
778 struct fastrpc_invoke_args *args)
779{
780 struct fastrpc_invoke_ctx *ctx = NULL;
781 int err = 0;
782
783 if (!fl->sctx)
784 return -EINVAL;
785
786 ctx = fastrpc_context_alloc(fl, kernel, sc, args);
787 if (IS_ERR(ctx))
788 return PTR_ERR(ctx);
789
790 if (ctx->nscalars) {
791 err = fastrpc_get_args(kernel, ctx);
792 if (err)
793 goto bail;
794 }
795 /* Send invoke buffer to remote dsp */
796 err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle);
797 if (err)
798 goto bail;
799
800 /* Wait for remote dsp to respond or time out */
801 err = wait_for_completion_interruptible(&ctx->work);
802 if (err)
803 goto bail;
804
805 /* Check the response from remote dsp */
806 err = ctx->retval;
807 if (err)
808 goto bail;
809
810 if (ctx->nscalars) {
811 /* populate all the output buffers with results */
812 err = fastrpc_put_args(ctx, kernel);
813 if (err)
814 goto bail;
815 }
816
817bail:
818 /* We are done with this compute context, remove it from pending list */
819 spin_lock(&fl->lock);
820 list_del(&ctx->node);
821 spin_unlock(&fl->lock);
822 fastrpc_context_put(ctx);
823
824 if (err)
825 dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err);
826
827 return err;
828}
829
d73f71c7
SK
830static int fastrpc_init_create_process(struct fastrpc_user *fl,
831 char __user *argp)
832{
833 struct fastrpc_init_create init;
834 struct fastrpc_invoke_args *args;
835 struct fastrpc_phy_page pages[1];
836 struct fastrpc_map *map = NULL;
837 struct fastrpc_buf *imem = NULL;
838 int memlen;
839 int err;
840 struct {
841 int pgid;
842 u32 namelen;
843 u32 filelen;
844 u32 pageslen;
845 u32 attrs;
846 u32 siglen;
847 } inbuf;
848 u32 sc;
849
850 args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL);
851 if (!args)
852 return -ENOMEM;
853
854 if (copy_from_user(&init, argp, sizeof(init))) {
855 err = -EFAULT;
b49f6d83 856 goto err;
d73f71c7
SK
857 }
858
859 if (init.filelen > INIT_FILELEN_MAX) {
860 err = -EINVAL;
b49f6d83 861 goto err;
d73f71c7
SK
862 }
863
864 inbuf.pgid = fl->tgid;
865 inbuf.namelen = strlen(current->comm) + 1;
866 inbuf.filelen = init.filelen;
867 inbuf.pageslen = 1;
868 inbuf.attrs = init.attrs;
869 inbuf.siglen = init.siglen;
870 fl->pd = 1;
871
872 if (init.filelen && init.filefd) {
873 err = fastrpc_map_create(fl, init.filefd, init.filelen, &map);
874 if (err)
b49f6d83 875 goto err;
d73f71c7
SK
876 }
877
878 memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4),
879 1024 * 1024);
880 err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen,
881 &imem);
b49f6d83
TE
882 if (err)
883 goto err_alloc;
d73f71c7
SK
884
885 fl->init_mem = imem;
886 args[0].ptr = (u64)(uintptr_t)&inbuf;
887 args[0].length = sizeof(inbuf);
888 args[0].fd = -1;
889
890 args[1].ptr = (u64)(uintptr_t)current->comm;
891 args[1].length = inbuf.namelen;
892 args[1].fd = -1;
893
894 args[2].ptr = (u64) init.file;
895 args[2].length = inbuf.filelen;
896 args[2].fd = init.filefd;
897
898 pages[0].addr = imem->phys;
899 pages[0].size = imem->size;
900
901 args[3].ptr = (u64)(uintptr_t) pages;
902 args[3].length = 1 * sizeof(*pages);
903 args[3].fd = -1;
904
905 args[4].ptr = (u64)(uintptr_t)&inbuf.attrs;
906 args[4].length = sizeof(inbuf.attrs);
907 args[4].fd = -1;
908
909 args[5].ptr = (u64)(uintptr_t) &inbuf.siglen;
910 args[5].length = sizeof(inbuf.siglen);
911 args[5].fd = -1;
912
913 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0);
914 if (init.attrs)
915 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0);
916
917 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
918 sc, args);
b49f6d83
TE
919 if (err)
920 goto err_invoke;
921
922 kfree(args);
d73f71c7 923
b49f6d83
TE
924 return 0;
925
926err_invoke:
927 fl->init_mem = NULL;
928 fastrpc_buf_free(imem);
929err_alloc:
930 if (map) {
931 spin_lock(&fl->lock);
932 list_del(&map->node);
933 spin_unlock(&fl->lock);
d73f71c7 934 fastrpc_map_put(map);
d73f71c7 935 }
b49f6d83 936err:
d73f71c7
SK
937 kfree(args);
938
939 return err;
940}
941
f6f9279f
SK
942static struct fastrpc_session_ctx *fastrpc_session_alloc(
943 struct fastrpc_channel_ctx *cctx)
944{
945 struct fastrpc_session_ctx *session = NULL;
946 int i;
947
948 spin_lock(&cctx->lock);
949 for (i = 0; i < cctx->sesscount; i++) {
950 if (!cctx->session[i].used && cctx->session[i].valid) {
951 cctx->session[i].used = true;
952 session = &cctx->session[i];
953 break;
954 }
955 }
956 spin_unlock(&cctx->lock);
957
958 return session;
959}
960
961static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx,
962 struct fastrpc_session_ctx *session)
963{
964 spin_lock(&cctx->lock);
965 session->used = false;
966 spin_unlock(&cctx->lock);
967}
968
d73f71c7
SK
969static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl)
970{
971 struct fastrpc_invoke_args args[1];
972 int tgid = 0;
973 u32 sc;
974
975 tgid = fl->tgid;
976 args[0].ptr = (u64)(uintptr_t) &tgid;
977 args[0].length = sizeof(tgid);
978 args[0].fd = -1;
979 args[0].reserved = 0;
980 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0);
981
982 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
983 sc, &args[0]);
984}
985
f6f9279f
SK
986static int fastrpc_device_release(struct inode *inode, struct file *file)
987{
988 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
989 struct fastrpc_channel_ctx *cctx = fl->cctx;
c68cfb71
SK
990 struct fastrpc_invoke_ctx *ctx, *n;
991 struct fastrpc_map *map, *m;
f6f9279f 992
d73f71c7
SK
993 fastrpc_release_current_dsp_process(fl);
994
f6f9279f
SK
995 spin_lock(&cctx->lock);
996 list_del(&fl->user);
997 spin_unlock(&cctx->lock);
998
c68cfb71
SK
999 if (fl->init_mem)
1000 fastrpc_buf_free(fl->init_mem);
1001
1002 list_for_each_entry_safe(ctx, n, &fl->pending, node) {
1003 list_del(&ctx->node);
1004 fastrpc_context_put(ctx);
1005 }
1006
1007 list_for_each_entry_safe(map, m, &fl->maps, node) {
1008 list_del(&map->node);
1009 fastrpc_map_put(map);
1010 }
1011
f6f9279f
SK
1012 fastrpc_session_free(cctx, fl->sctx);
1013
1014 mutex_destroy(&fl->mutex);
1015 kfree(fl);
1016 file->private_data = NULL;
1017
1018 return 0;
1019}
1020
1021static int fastrpc_device_open(struct inode *inode, struct file *filp)
1022{
1023 struct fastrpc_channel_ctx *cctx = miscdev_to_cctx(filp->private_data);
1024 struct fastrpc_user *fl = NULL;
1025
1026 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
1027 if (!fl)
1028 return -ENOMEM;
1029
1030 filp->private_data = fl;
1031 spin_lock_init(&fl->lock);
1032 mutex_init(&fl->mutex);
1033 INIT_LIST_HEAD(&fl->pending);
1034 INIT_LIST_HEAD(&fl->maps);
1035 INIT_LIST_HEAD(&fl->user);
1036 fl->tgid = current->tgid;
1037 fl->cctx = cctx;
7c11df42
TE
1038
1039 fl->sctx = fastrpc_session_alloc(cctx);
1040 if (!fl->sctx) {
1041 dev_err(&cctx->rpdev->dev, "No session available\n");
1042 mutex_destroy(&fl->mutex);
1043 kfree(fl);
1044
1045 return -EBUSY;
1046 }
1047
f6f9279f
SK
1048 spin_lock(&cctx->lock);
1049 list_add_tail(&fl->user, &cctx->users);
1050 spin_unlock(&cctx->lock);
f6f9279f
SK
1051
1052 return 0;
1053}
1054
6cffd795
SK
1055static int fastrpc_dmabuf_free(struct fastrpc_user *fl, char __user *argp)
1056{
1057 struct dma_buf *buf;
1058 int info;
1059
1060 if (copy_from_user(&info, argp, sizeof(info)))
1061 return -EFAULT;
1062
1063 buf = dma_buf_get(info);
1064 if (IS_ERR_OR_NULL(buf))
1065 return -EINVAL;
1066 /*
1067 * one for the last get and other for the ALLOC_DMA_BUFF ioctl
1068 */
1069 dma_buf_put(buf);
1070 dma_buf_put(buf);
1071
1072 return 0;
1073}
1074
1075static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
1076{
1077 struct fastrpc_alloc_dma_buf bp;
1078 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
1079 struct fastrpc_buf *buf = NULL;
1080 int err;
1081
1082 if (copy_from_user(&bp, argp, sizeof(bp)))
1083 return -EFAULT;
1084
1085 err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf);
1086 if (err)
1087 return err;
1088 exp_info.ops = &fastrpc_dma_buf_ops;
1089 exp_info.size = bp.size;
1090 exp_info.flags = O_RDWR;
1091 exp_info.priv = buf;
1092 buf->dmabuf = dma_buf_export(&exp_info);
1093 if (IS_ERR(buf->dmabuf)) {
1094 err = PTR_ERR(buf->dmabuf);
1095 fastrpc_buf_free(buf);
1096 return err;
1097 }
1098
1099 bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE);
1100 if (bp.fd < 0) {
1101 dma_buf_put(buf->dmabuf);
1102 return -EINVAL;
1103 }
1104
1105 if (copy_to_user(argp, &bp, sizeof(bp))) {
1106 dma_buf_put(buf->dmabuf);
1107 return -EFAULT;
1108 }
1109
1110 get_dma_buf(buf->dmabuf);
1111
1112 return 0;
1113}
1114
d73f71c7
SK
1115static int fastrpc_init_attach(struct fastrpc_user *fl)
1116{
1117 struct fastrpc_invoke_args args[1];
1118 int tgid = fl->tgid;
1119 u32 sc;
1120
1121 args[0].ptr = (u64)(uintptr_t) &tgid;
1122 args[0].length = sizeof(tgid);
1123 args[0].fd = -1;
1124 args[0].reserved = 0;
1125 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0);
1126 fl->pd = 0;
1127
1128 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1129 sc, &args[0]);
1130}
1131
c68cfb71
SK
1132static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp)
1133{
1134 struct fastrpc_invoke_args *args = NULL;
1135 struct fastrpc_invoke inv;
1136 u32 nscalars;
1137 int err;
1138
1139 if (copy_from_user(&inv, argp, sizeof(inv)))
1140 return -EFAULT;
1141
1142 /* nscalars is truncated here to max supported value */
1143 nscalars = REMOTE_SCALARS_LENGTH(inv.sc);
1144 if (nscalars) {
1145 args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL);
1146 if (!args)
1147 return -ENOMEM;
1148
1149 if (copy_from_user(args, (void __user *)(uintptr_t)inv.args,
1150 nscalars * sizeof(*args))) {
1151 kfree(args);
1152 return -EFAULT;
1153 }
1154 }
1155
1156 err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args);
1157 kfree(args);
1158
1159 return err;
1160}
1161
1162static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
1163 unsigned long arg)
1164{
1165 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1166 char __user *argp = (char __user *)arg;
1167 int err;
1168
1169 switch (cmd) {
1170 case FASTRPC_IOCTL_INVOKE:
1171 err = fastrpc_invoke(fl, argp);
1172 break;
d73f71c7
SK
1173 case FASTRPC_IOCTL_INIT_ATTACH:
1174 err = fastrpc_init_attach(fl);
1175 break;
1176 case FASTRPC_IOCTL_INIT_CREATE:
1177 err = fastrpc_init_create_process(fl, argp);
1178 break;
6cffd795
SK
1179 case FASTRPC_IOCTL_FREE_DMA_BUFF:
1180 err = fastrpc_dmabuf_free(fl, argp);
1181 break;
1182 case FASTRPC_IOCTL_ALLOC_DMA_BUFF:
1183 err = fastrpc_dmabuf_alloc(fl, argp);
1184 break;
c68cfb71
SK
1185 default:
1186 err = -ENOTTY;
1187 break;
1188 }
1189
1190 return err;
1191}
1192
f6f9279f
SK
1193static const struct file_operations fastrpc_fops = {
1194 .open = fastrpc_device_open,
1195 .release = fastrpc_device_release,
c68cfb71
SK
1196 .unlocked_ioctl = fastrpc_device_ioctl,
1197 .compat_ioctl = fastrpc_device_ioctl,
f6f9279f
SK
1198};
1199
1200static int fastrpc_cb_probe(struct platform_device *pdev)
1201{
1202 struct fastrpc_channel_ctx *cctx;
1203 struct fastrpc_session_ctx *sess;
1204 struct device *dev = &pdev->dev;
1205 int i, sessions = 0;
1206
1207 cctx = dev_get_drvdata(dev->parent);
1208 if (!cctx)
1209 return -EINVAL;
1210
1211 of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions);
1212
1213 spin_lock(&cctx->lock);
1214 sess = &cctx->session[cctx->sesscount];
1215 sess->used = false;
1216 sess->valid = true;
1217 sess->dev = dev;
1218 dev_set_drvdata(dev, sess);
1219
1220 if (of_property_read_u32(dev->of_node, "reg", &sess->sid))
1221 dev_info(dev, "FastRPC Session ID not specified in DT\n");
1222
1223 if (sessions > 0) {
1224 struct fastrpc_session_ctx *dup_sess;
1225
1226 for (i = 1; i < sessions; i++) {
1227 if (cctx->sesscount++ >= FASTRPC_MAX_SESSIONS)
1228 break;
1229 dup_sess = &cctx->session[cctx->sesscount];
1230 memcpy(dup_sess, sess, sizeof(*dup_sess));
1231 }
1232 }
1233 cctx->sesscount++;
1234 spin_unlock(&cctx->lock);
1235 dma_set_mask(dev, DMA_BIT_MASK(32));
1236
1237 return 0;
1238}
1239
1240static int fastrpc_cb_remove(struct platform_device *pdev)
1241{
1242 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent);
1243 struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev);
1244 int i;
1245
1246 spin_lock(&cctx->lock);
1247 for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) {
1248 if (cctx->session[i].sid == sess->sid) {
1249 cctx->session[i].valid = false;
1250 cctx->sesscount--;
1251 }
1252 }
1253 spin_unlock(&cctx->lock);
1254
1255 return 0;
1256}
1257
1258static const struct of_device_id fastrpc_match_table[] = {
1259 { .compatible = "qcom,fastrpc-compute-cb", },
1260 {}
1261};
1262
1263static struct platform_driver fastrpc_cb_driver = {
1264 .probe = fastrpc_cb_probe,
1265 .remove = fastrpc_cb_remove,
1266 .driver = {
1267 .name = "qcom,fastrpc-cb",
1268 .of_match_table = fastrpc_match_table,
1269 .suppress_bind_attrs = true,
1270 },
1271};
1272
1273static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
1274{
1275 struct device *rdev = &rpdev->dev;
1276 struct fastrpc_channel_ctx *data;
1277 int i, err, domain_id = -1;
1278 const char *domain;
1279
1280 data = devm_kzalloc(rdev, sizeof(*data), GFP_KERNEL);
1281 if (!data)
1282 return -ENOMEM;
1283
1284 err = of_property_read_string(rdev->of_node, "label", &domain);
1285 if (err) {
1286 dev_info(rdev, "FastRPC Domain not specified in DT\n");
1287 return err;
1288 }
1289
1290 for (i = 0; i <= CDSP_DOMAIN_ID; i++) {
1291 if (!strcmp(domains[i], domain)) {
1292 domain_id = i;
1293 break;
1294 }
1295 }
1296
1297 if (domain_id < 0) {
1298 dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id);
1299 return -EINVAL;
1300 }
1301
1302 data->miscdev.minor = MISC_DYNAMIC_MINOR;
1303 data->miscdev.name = kasprintf(GFP_KERNEL, "fastrpc-%s",
1304 domains[domain_id]);
1305 data->miscdev.fops = &fastrpc_fops;
1306 err = misc_register(&data->miscdev);
1307 if (err)
1308 return err;
1309
1310 dev_set_drvdata(&rpdev->dev, data);
1311 dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
1312 INIT_LIST_HEAD(&data->users);
1313 spin_lock_init(&data->lock);
1314 idr_init(&data->ctx_idr);
1315 data->domain_id = domain_id;
1316 data->rpdev = rpdev;
1317
1318 return of_platform_populate(rdev->of_node, NULL, NULL, rdev);
1319}
1320
c68cfb71
SK
1321static void fastrpc_notify_users(struct fastrpc_user *user)
1322{
1323 struct fastrpc_invoke_ctx *ctx;
1324
1325 spin_lock(&user->lock);
1326 list_for_each_entry(ctx, &user->pending, node)
1327 complete(&ctx->work);
1328 spin_unlock(&user->lock);
1329}
1330
f6f9279f
SK
1331static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
1332{
1333 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
c68cfb71
SK
1334 struct fastrpc_user *user;
1335
1336 spin_lock(&cctx->lock);
1337 list_for_each_entry(user, &cctx->users, user)
1338 fastrpc_notify_users(user);
1339 spin_unlock(&cctx->lock);
f6f9279f
SK
1340
1341 misc_deregister(&cctx->miscdev);
1342 of_platform_depopulate(&rpdev->dev);
1343 kfree(cctx);
1344}
1345
1346static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
1347 int len, void *priv, u32 addr)
1348{
c68cfb71
SK
1349 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
1350 struct fastrpc_invoke_rsp *rsp = data;
1351 struct fastrpc_invoke_ctx *ctx;
1352 unsigned long flags;
1353 unsigned long ctxid;
1354
1355 if (len < sizeof(*rsp))
1356 return -EINVAL;
1357
1358 ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4);
1359
1360 spin_lock_irqsave(&cctx->lock, flags);
1361 ctx = idr_find(&cctx->ctx_idr, ctxid);
1362 spin_unlock_irqrestore(&cctx->lock, flags);
1363
1364 if (!ctx) {
1365 dev_err(&rpdev->dev, "No context ID matches response\n");
1366 return -ENOENT;
1367 }
1368
1369 ctx->retval = rsp->retval;
1370 complete(&ctx->work);
8e7389c7
TE
1371
1372 /*
1373 * The DMA buffer associated with the context cannot be freed in
1374 * interrupt context so schedule it through a worker thread to
1375 * avoid a kernel BUG.
1376 */
1377 schedule_work(&ctx->put_work);
c68cfb71 1378
f6f9279f
SK
1379 return 0;
1380}
1381
1382static const struct of_device_id fastrpc_rpmsg_of_match[] = {
1383 { .compatible = "qcom,fastrpc" },
1384 { },
1385};
1386MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match);
1387
1388static struct rpmsg_driver fastrpc_driver = {
1389 .probe = fastrpc_rpmsg_probe,
1390 .remove = fastrpc_rpmsg_remove,
1391 .callback = fastrpc_rpmsg_callback,
1392 .drv = {
1393 .name = "qcom,fastrpc",
1394 .of_match_table = fastrpc_rpmsg_of_match,
1395 },
1396};
1397
1398static int fastrpc_init(void)
1399{
1400 int ret;
1401
1402 ret = platform_driver_register(&fastrpc_cb_driver);
1403 if (ret < 0) {
1404 pr_err("fastrpc: failed to register cb driver\n");
1405 return ret;
1406 }
1407
1408 ret = register_rpmsg_driver(&fastrpc_driver);
1409 if (ret < 0) {
1410 pr_err("fastrpc: failed to register rpmsg driver\n");
1411 platform_driver_unregister(&fastrpc_cb_driver);
1412 return ret;
1413 }
1414
1415 return 0;
1416}
1417module_init(fastrpc_init);
1418
1419static void fastrpc_exit(void)
1420{
1421 platform_driver_unregister(&fastrpc_cb_driver);
1422 unregister_rpmsg_driver(&fastrpc_driver);
1423}
1424module_exit(fastrpc_exit);
1425
1426MODULE_LICENSE("GPL v2");