blob: 986c9aee539aa00b8e16ac227df65493836f4e88 [file] [log] [blame]
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
3// Copyright (c) 2018, Linaro Limited
4
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00005#include <linux/completion.h>
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00006#include <linux/device.h>
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00007#include <linux/dma-buf.h>
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00008#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>
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +000019#include <uapi/misc/fastrpc.h>
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +000020
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*/
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +000027#define FASTRPC_ALIGN 128
28#define FASTRPC_MAX_FDLIST 16
29#define FASTRPC_MAX_CRCLIST 64
30#define FASTRPC_PHYS(p) ((p) & 0xffffffff)
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +000031#define FASTRPC_CTX_MAX (256)
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +000032#define FASTRPC_INIT_HANDLE 1
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +000033#define FASTRPC_CTXID_MASK (0xFF0)
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +000034#define INIT_FILELEN_MAX (2 * 1024 * 1024)
35#define INIT_MEMLEN_MAX (8 * 1024 * 1024)
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +000036#define FASTRPC_DEVICE_NAME "fastrpc"
37
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +000038/* 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
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +000065#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
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +000073#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"};
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +000077struct 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;
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +0000109 struct dma_buf *dmabuf;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000110 struct device *dev;
111 void *virt;
112 u64 phys;
113 u64 size;
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +0000114 /* 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;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000123};
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;
Thierry Escande8e7389c2019-03-07 10:12:22 +0000152 struct work_struct put_work;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000153 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};
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +0000161
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;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000187 struct fastrpc_buf *init_mem;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +0000188
189 int tgid;
190 int pd;
191 /* Lock for lists */
192 spinlock_t lock;
193 /* lock for allocations */
194 struct mutex mutex;
195};
196
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000197static 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
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +0000260 INIT_LIST_HEAD(&buf->attachments);
261 mutex_init(&buf->lock);
262
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000263 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
Thierry Escande8e7389c2019-03-07 10:12:22 +0000315static 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
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000323static 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);
Thierry Escande8e7389c2019-03-07 10:12:22 +0000357 INIT_WORK(&ctx->put_work, fastrpc_context_put_wq);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000358
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
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +0000386static 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
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000491static 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);
Wei Yongjun682a6042019-02-16 01:35:43 +0000509 if (IS_ERR(map->buf)) {
510 err = PTR_ERR(map->buf);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000511 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 }
Srinivas Kandagatla415a0722019-03-07 10:12:24 +0000795
796 /* make sure that all CPU memory writes are seen by DSP */
797 dma_wmb();
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000798 /* Send invoke buffer to remote dsp */
799 err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle);
800 if (err)
801 goto bail;
802
803 /* Wait for remote dsp to respond or time out */
804 err = wait_for_completion_interruptible(&ctx->work);
805 if (err)
806 goto bail;
807
808 /* Check the response from remote dsp */
809 err = ctx->retval;
810 if (err)
811 goto bail;
812
813 if (ctx->nscalars) {
Srinivas Kandagatla415a0722019-03-07 10:12:24 +0000814 /* make sure that all memory writes by DSP are seen by CPU */
815 dma_rmb();
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000816 /* populate all the output buffers with results */
817 err = fastrpc_put_args(ctx, kernel);
818 if (err)
819 goto bail;
820 }
821
822bail:
823 /* We are done with this compute context, remove it from pending list */
824 spin_lock(&fl->lock);
825 list_del(&ctx->node);
826 spin_unlock(&fl->lock);
827 fastrpc_context_put(ctx);
828
829 if (err)
830 dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err);
831
832 return err;
833}
834
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000835static int fastrpc_init_create_process(struct fastrpc_user *fl,
836 char __user *argp)
837{
838 struct fastrpc_init_create init;
839 struct fastrpc_invoke_args *args;
840 struct fastrpc_phy_page pages[1];
841 struct fastrpc_map *map = NULL;
842 struct fastrpc_buf *imem = NULL;
843 int memlen;
844 int err;
845 struct {
846 int pgid;
847 u32 namelen;
848 u32 filelen;
849 u32 pageslen;
850 u32 attrs;
851 u32 siglen;
852 } inbuf;
853 u32 sc;
854
855 args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL);
856 if (!args)
857 return -ENOMEM;
858
859 if (copy_from_user(&init, argp, sizeof(init))) {
860 err = -EFAULT;
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000861 goto err;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000862 }
863
864 if (init.filelen > INIT_FILELEN_MAX) {
865 err = -EINVAL;
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000866 goto err;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000867 }
868
869 inbuf.pgid = fl->tgid;
870 inbuf.namelen = strlen(current->comm) + 1;
871 inbuf.filelen = init.filelen;
872 inbuf.pageslen = 1;
873 inbuf.attrs = init.attrs;
874 inbuf.siglen = init.siglen;
875 fl->pd = 1;
876
877 if (init.filelen && init.filefd) {
878 err = fastrpc_map_create(fl, init.filefd, init.filelen, &map);
879 if (err)
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000880 goto err;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000881 }
882
883 memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4),
884 1024 * 1024);
885 err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen,
886 &imem);
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000887 if (err)
888 goto err_alloc;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000889
890 fl->init_mem = imem;
891 args[0].ptr = (u64)(uintptr_t)&inbuf;
892 args[0].length = sizeof(inbuf);
893 args[0].fd = -1;
894
895 args[1].ptr = (u64)(uintptr_t)current->comm;
896 args[1].length = inbuf.namelen;
897 args[1].fd = -1;
898
899 args[2].ptr = (u64) init.file;
900 args[2].length = inbuf.filelen;
901 args[2].fd = init.filefd;
902
903 pages[0].addr = imem->phys;
904 pages[0].size = imem->size;
905
906 args[3].ptr = (u64)(uintptr_t) pages;
907 args[3].length = 1 * sizeof(*pages);
908 args[3].fd = -1;
909
910 args[4].ptr = (u64)(uintptr_t)&inbuf.attrs;
911 args[4].length = sizeof(inbuf.attrs);
912 args[4].fd = -1;
913
914 args[5].ptr = (u64)(uintptr_t) &inbuf.siglen;
915 args[5].length = sizeof(inbuf.siglen);
916 args[5].fd = -1;
917
918 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0);
919 if (init.attrs)
920 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0);
921
922 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
923 sc, args);
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000924 if (err)
925 goto err_invoke;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000926
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000927 kfree(args);
928
929 return 0;
930
931err_invoke:
932 fl->init_mem = NULL;
933 fastrpc_buf_free(imem);
934err_alloc:
935 if (map) {
936 spin_lock(&fl->lock);
937 list_del(&map->node);
938 spin_unlock(&fl->lock);
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000939 fastrpc_map_put(map);
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000940 }
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000941err:
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000942 kfree(args);
943
944 return err;
945}
946
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +0000947static struct fastrpc_session_ctx *fastrpc_session_alloc(
948 struct fastrpc_channel_ctx *cctx)
949{
950 struct fastrpc_session_ctx *session = NULL;
951 int i;
952
953 spin_lock(&cctx->lock);
954 for (i = 0; i < cctx->sesscount; i++) {
955 if (!cctx->session[i].used && cctx->session[i].valid) {
956 cctx->session[i].used = true;
957 session = &cctx->session[i];
958 break;
959 }
960 }
961 spin_unlock(&cctx->lock);
962
963 return session;
964}
965
966static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx,
967 struct fastrpc_session_ctx *session)
968{
969 spin_lock(&cctx->lock);
970 session->used = false;
971 spin_unlock(&cctx->lock);
972}
973
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000974static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl)
975{
976 struct fastrpc_invoke_args args[1];
977 int tgid = 0;
978 u32 sc;
979
980 tgid = fl->tgid;
981 args[0].ptr = (u64)(uintptr_t) &tgid;
982 args[0].length = sizeof(tgid);
983 args[0].fd = -1;
984 args[0].reserved = 0;
985 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0);
986
987 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
988 sc, &args[0]);
989}
990
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +0000991static int fastrpc_device_release(struct inode *inode, struct file *file)
992{
993 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
994 struct fastrpc_channel_ctx *cctx = fl->cctx;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000995 struct fastrpc_invoke_ctx *ctx, *n;
996 struct fastrpc_map *map, *m;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +0000997
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000998 fastrpc_release_current_dsp_process(fl);
999
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001000 spin_lock(&cctx->lock);
1001 list_del(&fl->user);
1002 spin_unlock(&cctx->lock);
1003
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001004 if (fl->init_mem)
1005 fastrpc_buf_free(fl->init_mem);
1006
1007 list_for_each_entry_safe(ctx, n, &fl->pending, node) {
1008 list_del(&ctx->node);
1009 fastrpc_context_put(ctx);
1010 }
1011
1012 list_for_each_entry_safe(map, m, &fl->maps, node) {
1013 list_del(&map->node);
1014 fastrpc_map_put(map);
1015 }
1016
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001017 fastrpc_session_free(cctx, fl->sctx);
1018
1019 mutex_destroy(&fl->mutex);
1020 kfree(fl);
1021 file->private_data = NULL;
1022
1023 return 0;
1024}
1025
1026static int fastrpc_device_open(struct inode *inode, struct file *filp)
1027{
1028 struct fastrpc_channel_ctx *cctx = miscdev_to_cctx(filp->private_data);
1029 struct fastrpc_user *fl = NULL;
1030
1031 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
1032 if (!fl)
1033 return -ENOMEM;
1034
1035 filp->private_data = fl;
1036 spin_lock_init(&fl->lock);
1037 mutex_init(&fl->mutex);
1038 INIT_LIST_HEAD(&fl->pending);
1039 INIT_LIST_HEAD(&fl->maps);
1040 INIT_LIST_HEAD(&fl->user);
1041 fl->tgid = current->tgid;
1042 fl->cctx = cctx;
Thierry Escande7c11df42019-02-15 10:40:07 +00001043
1044 fl->sctx = fastrpc_session_alloc(cctx);
1045 if (!fl->sctx) {
1046 dev_err(&cctx->rpdev->dev, "No session available\n");
1047 mutex_destroy(&fl->mutex);
1048 kfree(fl);
1049
1050 return -EBUSY;
1051 }
1052
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001053 spin_lock(&cctx->lock);
1054 list_add_tail(&fl->user, &cctx->users);
1055 spin_unlock(&cctx->lock);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001056
1057 return 0;
1058}
1059
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +00001060static int fastrpc_dmabuf_free(struct fastrpc_user *fl, char __user *argp)
1061{
1062 struct dma_buf *buf;
1063 int info;
1064
1065 if (copy_from_user(&info, argp, sizeof(info)))
1066 return -EFAULT;
1067
1068 buf = dma_buf_get(info);
1069 if (IS_ERR_OR_NULL(buf))
1070 return -EINVAL;
1071 /*
1072 * one for the last get and other for the ALLOC_DMA_BUFF ioctl
1073 */
1074 dma_buf_put(buf);
1075 dma_buf_put(buf);
1076
1077 return 0;
1078}
1079
1080static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
1081{
1082 struct fastrpc_alloc_dma_buf bp;
1083 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
1084 struct fastrpc_buf *buf = NULL;
1085 int err;
1086
1087 if (copy_from_user(&bp, argp, sizeof(bp)))
1088 return -EFAULT;
1089
1090 err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf);
1091 if (err)
1092 return err;
1093 exp_info.ops = &fastrpc_dma_buf_ops;
1094 exp_info.size = bp.size;
1095 exp_info.flags = O_RDWR;
1096 exp_info.priv = buf;
1097 buf->dmabuf = dma_buf_export(&exp_info);
1098 if (IS_ERR(buf->dmabuf)) {
1099 err = PTR_ERR(buf->dmabuf);
1100 fastrpc_buf_free(buf);
1101 return err;
1102 }
1103
1104 bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE);
1105 if (bp.fd < 0) {
1106 dma_buf_put(buf->dmabuf);
1107 return -EINVAL;
1108 }
1109
1110 if (copy_to_user(argp, &bp, sizeof(bp))) {
1111 dma_buf_put(buf->dmabuf);
1112 return -EFAULT;
1113 }
1114
1115 get_dma_buf(buf->dmabuf);
1116
1117 return 0;
1118}
1119
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001120static int fastrpc_init_attach(struct fastrpc_user *fl)
1121{
1122 struct fastrpc_invoke_args args[1];
1123 int tgid = fl->tgid;
1124 u32 sc;
1125
1126 args[0].ptr = (u64)(uintptr_t) &tgid;
1127 args[0].length = sizeof(tgid);
1128 args[0].fd = -1;
1129 args[0].reserved = 0;
1130 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0);
1131 fl->pd = 0;
1132
1133 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1134 sc, &args[0]);
1135}
1136
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001137static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp)
1138{
1139 struct fastrpc_invoke_args *args = NULL;
1140 struct fastrpc_invoke inv;
1141 u32 nscalars;
1142 int err;
1143
1144 if (copy_from_user(&inv, argp, sizeof(inv)))
1145 return -EFAULT;
1146
1147 /* nscalars is truncated here to max supported value */
1148 nscalars = REMOTE_SCALARS_LENGTH(inv.sc);
1149 if (nscalars) {
1150 args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL);
1151 if (!args)
1152 return -ENOMEM;
1153
1154 if (copy_from_user(args, (void __user *)(uintptr_t)inv.args,
1155 nscalars * sizeof(*args))) {
1156 kfree(args);
1157 return -EFAULT;
1158 }
1159 }
1160
1161 err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args);
1162 kfree(args);
1163
1164 return err;
1165}
1166
1167static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
1168 unsigned long arg)
1169{
1170 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1171 char __user *argp = (char __user *)arg;
1172 int err;
1173
1174 switch (cmd) {
1175 case FASTRPC_IOCTL_INVOKE:
1176 err = fastrpc_invoke(fl, argp);
1177 break;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001178 case FASTRPC_IOCTL_INIT_ATTACH:
1179 err = fastrpc_init_attach(fl);
1180 break;
1181 case FASTRPC_IOCTL_INIT_CREATE:
1182 err = fastrpc_init_create_process(fl, argp);
1183 break;
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +00001184 case FASTRPC_IOCTL_FREE_DMA_BUFF:
1185 err = fastrpc_dmabuf_free(fl, argp);
1186 break;
1187 case FASTRPC_IOCTL_ALLOC_DMA_BUFF:
1188 err = fastrpc_dmabuf_alloc(fl, argp);
1189 break;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001190 default:
1191 err = -ENOTTY;
1192 break;
1193 }
1194
1195 return err;
1196}
1197
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001198static const struct file_operations fastrpc_fops = {
1199 .open = fastrpc_device_open,
1200 .release = fastrpc_device_release,
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001201 .unlocked_ioctl = fastrpc_device_ioctl,
1202 .compat_ioctl = fastrpc_device_ioctl,
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001203};
1204
1205static int fastrpc_cb_probe(struct platform_device *pdev)
1206{
1207 struct fastrpc_channel_ctx *cctx;
1208 struct fastrpc_session_ctx *sess;
1209 struct device *dev = &pdev->dev;
1210 int i, sessions = 0;
1211
1212 cctx = dev_get_drvdata(dev->parent);
1213 if (!cctx)
1214 return -EINVAL;
1215
1216 of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions);
1217
1218 spin_lock(&cctx->lock);
1219 sess = &cctx->session[cctx->sesscount];
1220 sess->used = false;
1221 sess->valid = true;
1222 sess->dev = dev;
1223 dev_set_drvdata(dev, sess);
1224
1225 if (of_property_read_u32(dev->of_node, "reg", &sess->sid))
1226 dev_info(dev, "FastRPC Session ID not specified in DT\n");
1227
1228 if (sessions > 0) {
1229 struct fastrpc_session_ctx *dup_sess;
1230
1231 for (i = 1; i < sessions; i++) {
1232 if (cctx->sesscount++ >= FASTRPC_MAX_SESSIONS)
1233 break;
1234 dup_sess = &cctx->session[cctx->sesscount];
1235 memcpy(dup_sess, sess, sizeof(*dup_sess));
1236 }
1237 }
1238 cctx->sesscount++;
1239 spin_unlock(&cctx->lock);
1240 dma_set_mask(dev, DMA_BIT_MASK(32));
1241
1242 return 0;
1243}
1244
1245static int fastrpc_cb_remove(struct platform_device *pdev)
1246{
1247 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent);
1248 struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev);
1249 int i;
1250
1251 spin_lock(&cctx->lock);
1252 for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) {
1253 if (cctx->session[i].sid == sess->sid) {
1254 cctx->session[i].valid = false;
1255 cctx->sesscount--;
1256 }
1257 }
1258 spin_unlock(&cctx->lock);
1259
1260 return 0;
1261}
1262
1263static const struct of_device_id fastrpc_match_table[] = {
1264 { .compatible = "qcom,fastrpc-compute-cb", },
1265 {}
1266};
1267
1268static struct platform_driver fastrpc_cb_driver = {
1269 .probe = fastrpc_cb_probe,
1270 .remove = fastrpc_cb_remove,
1271 .driver = {
1272 .name = "qcom,fastrpc-cb",
1273 .of_match_table = fastrpc_match_table,
1274 .suppress_bind_attrs = true,
1275 },
1276};
1277
1278static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
1279{
1280 struct device *rdev = &rpdev->dev;
1281 struct fastrpc_channel_ctx *data;
1282 int i, err, domain_id = -1;
1283 const char *domain;
1284
1285 data = devm_kzalloc(rdev, sizeof(*data), GFP_KERNEL);
1286 if (!data)
1287 return -ENOMEM;
1288
1289 err = of_property_read_string(rdev->of_node, "label", &domain);
1290 if (err) {
1291 dev_info(rdev, "FastRPC Domain not specified in DT\n");
1292 return err;
1293 }
1294
1295 for (i = 0; i <= CDSP_DOMAIN_ID; i++) {
1296 if (!strcmp(domains[i], domain)) {
1297 domain_id = i;
1298 break;
1299 }
1300 }
1301
1302 if (domain_id < 0) {
1303 dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id);
1304 return -EINVAL;
1305 }
1306
1307 data->miscdev.minor = MISC_DYNAMIC_MINOR;
1308 data->miscdev.name = kasprintf(GFP_KERNEL, "fastrpc-%s",
1309 domains[domain_id]);
1310 data->miscdev.fops = &fastrpc_fops;
1311 err = misc_register(&data->miscdev);
1312 if (err)
1313 return err;
1314
1315 dev_set_drvdata(&rpdev->dev, data);
1316 dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
1317 INIT_LIST_HEAD(&data->users);
1318 spin_lock_init(&data->lock);
1319 idr_init(&data->ctx_idr);
1320 data->domain_id = domain_id;
1321 data->rpdev = rpdev;
1322
1323 return of_platform_populate(rdev->of_node, NULL, NULL, rdev);
1324}
1325
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001326static void fastrpc_notify_users(struct fastrpc_user *user)
1327{
1328 struct fastrpc_invoke_ctx *ctx;
1329
1330 spin_lock(&user->lock);
1331 list_for_each_entry(ctx, &user->pending, node)
1332 complete(&ctx->work);
1333 spin_unlock(&user->lock);
1334}
1335
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001336static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
1337{
1338 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001339 struct fastrpc_user *user;
1340
1341 spin_lock(&cctx->lock);
1342 list_for_each_entry(user, &cctx->users, user)
1343 fastrpc_notify_users(user);
1344 spin_unlock(&cctx->lock);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001345
1346 misc_deregister(&cctx->miscdev);
1347 of_platform_depopulate(&rpdev->dev);
1348 kfree(cctx);
1349}
1350
1351static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
1352 int len, void *priv, u32 addr)
1353{
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001354 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
1355 struct fastrpc_invoke_rsp *rsp = data;
1356 struct fastrpc_invoke_ctx *ctx;
1357 unsigned long flags;
1358 unsigned long ctxid;
1359
1360 if (len < sizeof(*rsp))
1361 return -EINVAL;
1362
1363 ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4);
1364
1365 spin_lock_irqsave(&cctx->lock, flags);
1366 ctx = idr_find(&cctx->ctx_idr, ctxid);
1367 spin_unlock_irqrestore(&cctx->lock, flags);
1368
1369 if (!ctx) {
1370 dev_err(&rpdev->dev, "No context ID matches response\n");
1371 return -ENOENT;
1372 }
1373
1374 ctx->retval = rsp->retval;
1375 complete(&ctx->work);
Thierry Escande8e7389c2019-03-07 10:12:22 +00001376
1377 /*
1378 * The DMA buffer associated with the context cannot be freed in
1379 * interrupt context so schedule it through a worker thread to
1380 * avoid a kernel BUG.
1381 */
1382 schedule_work(&ctx->put_work);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001383
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001384 return 0;
1385}
1386
1387static const struct of_device_id fastrpc_rpmsg_of_match[] = {
1388 { .compatible = "qcom,fastrpc" },
1389 { },
1390};
1391MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match);
1392
1393static struct rpmsg_driver fastrpc_driver = {
1394 .probe = fastrpc_rpmsg_probe,
1395 .remove = fastrpc_rpmsg_remove,
1396 .callback = fastrpc_rpmsg_callback,
1397 .drv = {
1398 .name = "qcom,fastrpc",
1399 .of_match_table = fastrpc_rpmsg_of_match,
1400 },
1401};
1402
1403static int fastrpc_init(void)
1404{
1405 int ret;
1406
1407 ret = platform_driver_register(&fastrpc_cb_driver);
1408 if (ret < 0) {
1409 pr_err("fastrpc: failed to register cb driver\n");
1410 return ret;
1411 }
1412
1413 ret = register_rpmsg_driver(&fastrpc_driver);
1414 if (ret < 0) {
1415 pr_err("fastrpc: failed to register rpmsg driver\n");
1416 platform_driver_unregister(&fastrpc_cb_driver);
1417 return ret;
1418 }
1419
1420 return 0;
1421}
1422module_init(fastrpc_init);
1423
1424static void fastrpc_exit(void)
1425{
1426 platform_driver_unregister(&fastrpc_cb_driver);
1427 unregister_rpmsg_driver(&fastrpc_driver);
1428}
1429module_exit(fastrpc_exit);
1430
1431MODULE_LICENSE("GPL v2");