blob: 5440735c94bacb079ee168ff8697809f16514f13 [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;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000286 unsigned long flags;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000287 int i;
288
289 ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
290 cctx = ctx->cctx;
291
292 for (i = 0; i < ctx->nscalars; i++)
293 fastrpc_map_put(ctx->maps[i]);
294
295 if (ctx->buf)
296 fastrpc_buf_free(ctx->buf);
297
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000298 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000299 idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4);
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000300 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000301
302 kfree(ctx->maps);
303 kfree(ctx);
304}
305
306static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx)
307{
308 kref_get(&ctx->refcount);
309}
310
311static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx)
312{
313 kref_put(&ctx->refcount, fastrpc_context_free);
314}
315
Thierry Escande8e7389c2019-03-07 10:12:22 +0000316static void fastrpc_context_put_wq(struct work_struct *work)
317{
318 struct fastrpc_invoke_ctx *ctx =
319 container_of(work, struct fastrpc_invoke_ctx, put_work);
320
321 fastrpc_context_put(ctx);
322}
323
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000324static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
325 struct fastrpc_user *user, u32 kernel, u32 sc,
326 struct fastrpc_invoke_args *args)
327{
328 struct fastrpc_channel_ctx *cctx = user->cctx;
329 struct fastrpc_invoke_ctx *ctx = NULL;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000330 unsigned long flags;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000331 int ret;
332
333 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
334 if (!ctx)
335 return ERR_PTR(-ENOMEM);
336
337 INIT_LIST_HEAD(&ctx->node);
338 ctx->fl = user;
339 ctx->nscalars = REMOTE_SCALARS_LENGTH(sc);
340 ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) +
341 REMOTE_SCALARS_OUTBUFS(sc);
342
343 if (ctx->nscalars) {
344 ctx->maps = kcalloc(ctx->nscalars,
345 sizeof(*ctx->maps), GFP_KERNEL);
346 if (!ctx->maps) {
347 kfree(ctx);
348 return ERR_PTR(-ENOMEM);
349 }
350 ctx->args = args;
351 }
352
353 ctx->sc = sc;
354 ctx->retval = -1;
355 ctx->pid = current->pid;
356 ctx->tgid = user->tgid;
357 ctx->cctx = cctx;
358 init_completion(&ctx->work);
Thierry Escande8e7389c2019-03-07 10:12:22 +0000359 INIT_WORK(&ctx->put_work, fastrpc_context_put_wq);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000360
361 spin_lock(&user->lock);
362 list_add_tail(&ctx->node, &user->pending);
363 spin_unlock(&user->lock);
364
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000365 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000366 ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1,
367 FASTRPC_CTX_MAX, GFP_ATOMIC);
368 if (ret < 0) {
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000369 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000370 goto err_idr;
371 }
372 ctx->ctxid = ret << 4;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000373 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000374
375 kref_init(&ctx->refcount);
376
377 return ctx;
378err_idr:
379 spin_lock(&user->lock);
380 list_del(&ctx->node);
381 spin_unlock(&user->lock);
382 kfree(ctx->maps);
383 kfree(ctx);
384
385 return ERR_PTR(ret);
386}
387
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +0000388static struct sg_table *
389fastrpc_map_dma_buf(struct dma_buf_attachment *attachment,
390 enum dma_data_direction dir)
391{
392 struct fastrpc_dma_buf_attachment *a = attachment->priv;
393 struct sg_table *table;
394
395 table = &a->sgt;
396
397 if (!dma_map_sg(attachment->dev, table->sgl, table->nents, dir))
398 return ERR_PTR(-ENOMEM);
399
400 return table;
401}
402
403static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach,
404 struct sg_table *table,
405 enum dma_data_direction dir)
406{
407 dma_unmap_sg(attach->dev, table->sgl, table->nents, dir);
408}
409
410static void fastrpc_release(struct dma_buf *dmabuf)
411{
412 struct fastrpc_buf *buffer = dmabuf->priv;
413
414 fastrpc_buf_free(buffer);
415}
416
417static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf,
418 struct dma_buf_attachment *attachment)
419{
420 struct fastrpc_dma_buf_attachment *a;
421 struct fastrpc_buf *buffer = dmabuf->priv;
422 int ret;
423
424 a = kzalloc(sizeof(*a), GFP_KERNEL);
425 if (!a)
426 return -ENOMEM;
427
428 ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt,
429 FASTRPC_PHYS(buffer->phys), buffer->size);
430 if (ret < 0) {
431 dev_err(buffer->dev, "failed to get scatterlist from DMA API\n");
432 return -EINVAL;
433 }
434
435 a->dev = attachment->dev;
436 INIT_LIST_HEAD(&a->node);
437 attachment->priv = a;
438
439 mutex_lock(&buffer->lock);
440 list_add(&a->node, &buffer->attachments);
441 mutex_unlock(&buffer->lock);
442
443 return 0;
444}
445
446static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf,
447 struct dma_buf_attachment *attachment)
448{
449 struct fastrpc_dma_buf_attachment *a = attachment->priv;
450 struct fastrpc_buf *buffer = dmabuf->priv;
451
452 mutex_lock(&buffer->lock);
453 list_del(&a->node);
454 mutex_unlock(&buffer->lock);
455 kfree(a);
456}
457
458static void *fastrpc_kmap(struct dma_buf *dmabuf, unsigned long pgnum)
459{
460 struct fastrpc_buf *buf = dmabuf->priv;
461
462 return buf->virt ? buf->virt + pgnum * PAGE_SIZE : NULL;
463}
464
465static void *fastrpc_vmap(struct dma_buf *dmabuf)
466{
467 struct fastrpc_buf *buf = dmabuf->priv;
468
469 return buf->virt;
470}
471
472static int fastrpc_mmap(struct dma_buf *dmabuf,
473 struct vm_area_struct *vma)
474{
475 struct fastrpc_buf *buf = dmabuf->priv;
476 size_t size = vma->vm_end - vma->vm_start;
477
478 return dma_mmap_coherent(buf->dev, vma, buf->virt,
479 FASTRPC_PHYS(buf->phys), size);
480}
481
482static const struct dma_buf_ops fastrpc_dma_buf_ops = {
483 .attach = fastrpc_dma_buf_attach,
484 .detach = fastrpc_dma_buf_detatch,
485 .map_dma_buf = fastrpc_map_dma_buf,
486 .unmap_dma_buf = fastrpc_unmap_dma_buf,
487 .mmap = fastrpc_mmap,
488 .map = fastrpc_kmap,
489 .vmap = fastrpc_vmap,
490 .release = fastrpc_release,
491};
492
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000493static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
494 u64 len, struct fastrpc_map **ppmap)
495{
496 struct fastrpc_session_ctx *sess = fl->sctx;
497 struct fastrpc_map *map = NULL;
498 int err = 0;
499
500 if (!fastrpc_map_find(fl, fd, ppmap))
501 return 0;
502
503 map = kzalloc(sizeof(*map), GFP_KERNEL);
504 if (!map)
505 return -ENOMEM;
506
507 INIT_LIST_HEAD(&map->node);
508 map->fl = fl;
509 map->fd = fd;
510 map->buf = dma_buf_get(fd);
Wei Yongjun682a6042019-02-16 01:35:43 +0000511 if (IS_ERR(map->buf)) {
512 err = PTR_ERR(map->buf);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000513 goto get_err;
514 }
515
516 map->attach = dma_buf_attach(map->buf, sess->dev);
517 if (IS_ERR(map->attach)) {
518 dev_err(sess->dev, "Failed to attach dmabuf\n");
519 err = PTR_ERR(map->attach);
520 goto attach_err;
521 }
522
523 map->table = dma_buf_map_attachment(map->attach, DMA_BIDIRECTIONAL);
524 if (IS_ERR(map->table)) {
525 err = PTR_ERR(map->table);
526 goto map_err;
527 }
528
529 map->phys = sg_dma_address(map->table->sgl);
530 map->phys += ((u64)fl->sctx->sid << 32);
531 map->size = len;
532 map->va = sg_virt(map->table->sgl);
533 map->len = len;
534 kref_init(&map->refcount);
535
536 spin_lock(&fl->lock);
537 list_add_tail(&map->node, &fl->maps);
538 spin_unlock(&fl->lock);
539 *ppmap = map;
540
541 return 0;
542
543map_err:
544 dma_buf_detach(map->buf, map->attach);
545attach_err:
546 dma_buf_put(map->buf);
547get_err:
548 kfree(map);
549
550 return err;
551}
552
553/*
554 * Fastrpc payload buffer with metadata looks like:
555 *
556 * >>>>>> START of METADATA <<<<<<<<<
557 * +---------------------------------+
558 * | Arguments |
559 * | type:(struct fastrpc_remote_arg)|
560 * | (0 - N) |
561 * +---------------------------------+
562 * | Invoke Buffer list |
563 * | type:(struct fastrpc_invoke_buf)|
564 * | (0 - N) |
565 * +---------------------------------+
566 * | Page info list |
567 * | type:(struct fastrpc_phy_page) |
568 * | (0 - N) |
569 * +---------------------------------+
570 * | Optional info |
571 * |(can be specific to SoC/Firmware)|
572 * +---------------------------------+
573 * >>>>>>>> END of METADATA <<<<<<<<<
574 * +---------------------------------+
575 * | Inline ARGS |
576 * | (0-N) |
577 * +---------------------------------+
578 */
579
580static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx)
581{
582 int size = 0;
583
584 size = (sizeof(struct fastrpc_remote_arg) +
585 sizeof(struct fastrpc_invoke_buf) +
586 sizeof(struct fastrpc_phy_page)) * ctx->nscalars +
587 sizeof(u64) * FASTRPC_MAX_FDLIST +
588 sizeof(u32) * FASTRPC_MAX_CRCLIST;
589
590 return size;
591}
592
593static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen)
594{
595 u64 size = 0;
596 int i;
597
598 size = ALIGN(metalen, FASTRPC_ALIGN);
599 for (i = 0; i < ctx->nscalars; i++) {
600 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) {
601 size = ALIGN(size, FASTRPC_ALIGN);
602 size += ctx->args[i].length;
603 }
604 }
605
606 return size;
607}
608
609static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
610{
611 struct device *dev = ctx->fl->sctx->dev;
612 int i, err;
613
614 for (i = 0; i < ctx->nscalars; ++i) {
615 /* Make sure reserved field is set to 0 */
616 if (ctx->args[i].reserved)
617 return -EINVAL;
618
619 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 ||
620 ctx->args[i].length == 0)
621 continue;
622
623 err = fastrpc_map_create(ctx->fl, ctx->args[i].fd,
624 ctx->args[i].length, &ctx->maps[i]);
625 if (err) {
626 dev_err(dev, "Error Creating map %d\n", err);
627 return -EINVAL;
628 }
629
630 }
631 return 0;
632}
633
634static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
635{
636 struct device *dev = ctx->fl->sctx->dev;
637 struct fastrpc_remote_arg *rpra;
638 struct fastrpc_invoke_buf *list;
639 struct fastrpc_phy_page *pages;
640 int inbufs, i, err = 0;
641 u64 rlen, pkt_size;
642 uintptr_t args;
643 int metalen;
644
645
646 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
647 metalen = fastrpc_get_meta_size(ctx);
648 pkt_size = fastrpc_get_payload_size(ctx, metalen);
649
650 err = fastrpc_create_maps(ctx);
651 if (err)
652 return err;
653
654 ctx->msg_sz = pkt_size;
655
656 err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf);
657 if (err)
658 return err;
659
660 rpra = ctx->buf->virt;
661 list = ctx->buf->virt + ctx->nscalars * sizeof(*rpra);
662 pages = ctx->buf->virt + ctx->nscalars * (sizeof(*list) +
663 sizeof(*rpra));
664 args = (uintptr_t)ctx->buf->virt + metalen;
665 rlen = pkt_size - metalen;
666 ctx->rpra = rpra;
667
668 for (i = 0; i < ctx->nbufs; ++i) {
669 u64 len = ctx->args[i].length;
670
671 rpra[i].pv = 0;
672 rpra[i].len = len;
673 list[i].num = len ? 1 : 0;
674 list[i].pgidx = i;
675
676 if (!len)
677 continue;
678
679 pages[i].size = roundup(len, PAGE_SIZE);
680
681 if (ctx->maps[i]) {
Srinivas Kandagatla80f3afd2019-03-07 10:12:26 +0000682 struct vm_area_struct *vma = NULL;
683
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000684 rpra[i].pv = (u64) ctx->args[i].ptr;
685 pages[i].addr = ctx->maps[i]->phys;
Srinivas Kandagatla80f3afd2019-03-07 10:12:26 +0000686
687 vma = find_vma(current->mm, ctx->args[i].ptr);
688 if (vma)
689 pages[i].addr += ctx->args[i].ptr -
690 vma->vm_start;
691
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000692 } else {
693 rlen -= ALIGN(args, FASTRPC_ALIGN) - args;
694 args = ALIGN(args, FASTRPC_ALIGN);
695 if (rlen < len)
696 goto bail;
697
698 rpra[i].pv = args;
699 pages[i].addr = ctx->buf->phys + (pkt_size - rlen);
700 pages[i].addr = pages[i].addr & PAGE_MASK;
701 args = args + len;
702 rlen -= len;
703 }
704
705 if (i < inbufs && !ctx->maps[i]) {
706 void *dst = (void *)(uintptr_t)rpra[i].pv;
707 void *src = (void *)(uintptr_t)ctx->args[i].ptr;
708
709 if (!kernel) {
710 if (copy_from_user(dst, (void __user *)src,
711 len)) {
712 err = -EFAULT;
713 goto bail;
714 }
715 } else {
716 memcpy(dst, src, len);
717 }
718 }
719 }
720
721 for (i = ctx->nbufs; i < ctx->nscalars; ++i) {
722 rpra[i].pv = (u64) ctx->args[i].ptr;
723 rpra[i].len = ctx->args[i].length;
724 list[i].num = ctx->args[i].length ? 1 : 0;
725 list[i].pgidx = i;
726 pages[i].addr = ctx->maps[i]->phys;
727 pages[i].size = ctx->maps[i]->size;
728 }
729
730bail:
731 if (err)
732 dev_err(dev, "Error: get invoke args failed:%d\n", err);
733
734 return err;
735}
736
737static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
738 u32 kernel)
739{
740 struct fastrpc_remote_arg *rpra = ctx->rpra;
741 int i, inbufs;
742
743 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
744
745 for (i = inbufs; i < ctx->nbufs; ++i) {
746 void *src = (void *)(uintptr_t)rpra[i].pv;
747 void *dst = (void *)(uintptr_t)ctx->args[i].ptr;
748 u64 len = rpra[i].len;
749
750 if (!kernel) {
751 if (copy_to_user((void __user *)dst, src, len))
752 return -EFAULT;
753 } else {
754 memcpy(dst, src, len);
755 }
756 }
757
758 return 0;
759}
760
761static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx,
762 struct fastrpc_invoke_ctx *ctx,
763 u32 kernel, uint32_t handle)
764{
765 struct fastrpc_channel_ctx *cctx;
766 struct fastrpc_user *fl = ctx->fl;
767 struct fastrpc_msg *msg = &ctx->msg;
768
769 cctx = fl->cctx;
770 msg->pid = fl->tgid;
771 msg->tid = current->pid;
772
773 if (kernel)
774 msg->pid = 0;
775
776 msg->ctx = ctx->ctxid | fl->pd;
777 msg->handle = handle;
778 msg->sc = ctx->sc;
779 msg->addr = ctx->buf ? ctx->buf->phys : 0;
780 msg->size = roundup(ctx->msg_sz, PAGE_SIZE);
781 fastrpc_context_get(ctx);
782
783 return rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg));
784}
785
786static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
787 u32 handle, u32 sc,
788 struct fastrpc_invoke_args *args)
789{
790 struct fastrpc_invoke_ctx *ctx = NULL;
791 int err = 0;
792
793 if (!fl->sctx)
794 return -EINVAL;
795
796 ctx = fastrpc_context_alloc(fl, kernel, sc, args);
797 if (IS_ERR(ctx))
798 return PTR_ERR(ctx);
799
800 if (ctx->nscalars) {
801 err = fastrpc_get_args(kernel, ctx);
802 if (err)
803 goto bail;
804 }
Srinivas Kandagatla415a0722019-03-07 10:12:24 +0000805
806 /* make sure that all CPU memory writes are seen by DSP */
807 dma_wmb();
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000808 /* Send invoke buffer to remote dsp */
809 err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle);
810 if (err)
811 goto bail;
812
813 /* Wait for remote dsp to respond or time out */
814 err = wait_for_completion_interruptible(&ctx->work);
815 if (err)
816 goto bail;
817
818 /* Check the response from remote dsp */
819 err = ctx->retval;
820 if (err)
821 goto bail;
822
823 if (ctx->nscalars) {
Srinivas Kandagatla415a0722019-03-07 10:12:24 +0000824 /* make sure that all memory writes by DSP are seen by CPU */
825 dma_rmb();
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000826 /* populate all the output buffers with results */
827 err = fastrpc_put_args(ctx, kernel);
828 if (err)
829 goto bail;
830 }
831
832bail:
833 /* We are done with this compute context, remove it from pending list */
834 spin_lock(&fl->lock);
835 list_del(&ctx->node);
836 spin_unlock(&fl->lock);
837 fastrpc_context_put(ctx);
838
839 if (err)
840 dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err);
841
842 return err;
843}
844
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000845static int fastrpc_init_create_process(struct fastrpc_user *fl,
846 char __user *argp)
847{
848 struct fastrpc_init_create init;
849 struct fastrpc_invoke_args *args;
850 struct fastrpc_phy_page pages[1];
851 struct fastrpc_map *map = NULL;
852 struct fastrpc_buf *imem = NULL;
853 int memlen;
854 int err;
855 struct {
856 int pgid;
857 u32 namelen;
858 u32 filelen;
859 u32 pageslen;
860 u32 attrs;
861 u32 siglen;
862 } inbuf;
863 u32 sc;
864
865 args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL);
866 if (!args)
867 return -ENOMEM;
868
869 if (copy_from_user(&init, argp, sizeof(init))) {
870 err = -EFAULT;
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000871 goto err;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000872 }
873
874 if (init.filelen > INIT_FILELEN_MAX) {
875 err = -EINVAL;
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000876 goto err;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000877 }
878
879 inbuf.pgid = fl->tgid;
880 inbuf.namelen = strlen(current->comm) + 1;
881 inbuf.filelen = init.filelen;
882 inbuf.pageslen = 1;
883 inbuf.attrs = init.attrs;
884 inbuf.siglen = init.siglen;
885 fl->pd = 1;
886
887 if (init.filelen && init.filefd) {
888 err = fastrpc_map_create(fl, init.filefd, init.filelen, &map);
889 if (err)
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000890 goto err;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000891 }
892
893 memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4),
894 1024 * 1024);
895 err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen,
896 &imem);
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000897 if (err)
898 goto err_alloc;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000899
900 fl->init_mem = imem;
901 args[0].ptr = (u64)(uintptr_t)&inbuf;
902 args[0].length = sizeof(inbuf);
903 args[0].fd = -1;
904
905 args[1].ptr = (u64)(uintptr_t)current->comm;
906 args[1].length = inbuf.namelen;
907 args[1].fd = -1;
908
909 args[2].ptr = (u64) init.file;
910 args[2].length = inbuf.filelen;
911 args[2].fd = init.filefd;
912
913 pages[0].addr = imem->phys;
914 pages[0].size = imem->size;
915
916 args[3].ptr = (u64)(uintptr_t) pages;
917 args[3].length = 1 * sizeof(*pages);
918 args[3].fd = -1;
919
920 args[4].ptr = (u64)(uintptr_t)&inbuf.attrs;
921 args[4].length = sizeof(inbuf.attrs);
922 args[4].fd = -1;
923
924 args[5].ptr = (u64)(uintptr_t) &inbuf.siglen;
925 args[5].length = sizeof(inbuf.siglen);
926 args[5].fd = -1;
927
928 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0);
929 if (init.attrs)
930 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0);
931
932 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
933 sc, args);
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000934 if (err)
935 goto err_invoke;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000936
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000937 kfree(args);
938
939 return 0;
940
941err_invoke:
942 fl->init_mem = NULL;
943 fastrpc_buf_free(imem);
944err_alloc:
945 if (map) {
946 spin_lock(&fl->lock);
947 list_del(&map->node);
948 spin_unlock(&fl->lock);
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000949 fastrpc_map_put(map);
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000950 }
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000951err:
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000952 kfree(args);
953
954 return err;
955}
956
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +0000957static struct fastrpc_session_ctx *fastrpc_session_alloc(
958 struct fastrpc_channel_ctx *cctx)
959{
960 struct fastrpc_session_ctx *session = NULL;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000961 unsigned long flags;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +0000962 int i;
963
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000964 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +0000965 for (i = 0; i < cctx->sesscount; i++) {
966 if (!cctx->session[i].used && cctx->session[i].valid) {
967 cctx->session[i].used = true;
968 session = &cctx->session[i];
969 break;
970 }
971 }
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000972 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +0000973
974 return session;
975}
976
977static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx,
978 struct fastrpc_session_ctx *session)
979{
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000980 unsigned long flags;
981
982 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +0000983 session->used = false;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000984 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +0000985}
986
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000987static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl)
988{
989 struct fastrpc_invoke_args args[1];
990 int tgid = 0;
991 u32 sc;
992
993 tgid = fl->tgid;
994 args[0].ptr = (u64)(uintptr_t) &tgid;
995 args[0].length = sizeof(tgid);
996 args[0].fd = -1;
997 args[0].reserved = 0;
998 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0);
999
1000 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1001 sc, &args[0]);
1002}
1003
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001004static int fastrpc_device_release(struct inode *inode, struct file *file)
1005{
1006 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1007 struct fastrpc_channel_ctx *cctx = fl->cctx;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001008 struct fastrpc_invoke_ctx *ctx, *n;
1009 struct fastrpc_map *map, *m;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001010 unsigned long flags;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001011
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001012 fastrpc_release_current_dsp_process(fl);
1013
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001014 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001015 list_del(&fl->user);
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001016 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001017
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001018 if (fl->init_mem)
1019 fastrpc_buf_free(fl->init_mem);
1020
1021 list_for_each_entry_safe(ctx, n, &fl->pending, node) {
1022 list_del(&ctx->node);
1023 fastrpc_context_put(ctx);
1024 }
1025
1026 list_for_each_entry_safe(map, m, &fl->maps, node) {
1027 list_del(&map->node);
1028 fastrpc_map_put(map);
1029 }
1030
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001031 fastrpc_session_free(cctx, fl->sctx);
1032
1033 mutex_destroy(&fl->mutex);
1034 kfree(fl);
1035 file->private_data = NULL;
1036
1037 return 0;
1038}
1039
1040static int fastrpc_device_open(struct inode *inode, struct file *filp)
1041{
1042 struct fastrpc_channel_ctx *cctx = miscdev_to_cctx(filp->private_data);
1043 struct fastrpc_user *fl = NULL;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001044 unsigned long flags;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001045
1046 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
1047 if (!fl)
1048 return -ENOMEM;
1049
1050 filp->private_data = fl;
1051 spin_lock_init(&fl->lock);
1052 mutex_init(&fl->mutex);
1053 INIT_LIST_HEAD(&fl->pending);
1054 INIT_LIST_HEAD(&fl->maps);
1055 INIT_LIST_HEAD(&fl->user);
1056 fl->tgid = current->tgid;
1057 fl->cctx = cctx;
Thierry Escande7c11df42019-02-15 10:40:07 +00001058
1059 fl->sctx = fastrpc_session_alloc(cctx);
1060 if (!fl->sctx) {
1061 dev_err(&cctx->rpdev->dev, "No session available\n");
1062 mutex_destroy(&fl->mutex);
1063 kfree(fl);
1064
1065 return -EBUSY;
1066 }
1067
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001068 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001069 list_add_tail(&fl->user, &cctx->users);
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001070 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001071
1072 return 0;
1073}
1074
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +00001075static int fastrpc_dmabuf_free(struct fastrpc_user *fl, char __user *argp)
1076{
1077 struct dma_buf *buf;
1078 int info;
1079
1080 if (copy_from_user(&info, argp, sizeof(info)))
1081 return -EFAULT;
1082
1083 buf = dma_buf_get(info);
1084 if (IS_ERR_OR_NULL(buf))
1085 return -EINVAL;
1086 /*
1087 * one for the last get and other for the ALLOC_DMA_BUFF ioctl
1088 */
1089 dma_buf_put(buf);
1090 dma_buf_put(buf);
1091
1092 return 0;
1093}
1094
1095static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
1096{
1097 struct fastrpc_alloc_dma_buf bp;
1098 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
1099 struct fastrpc_buf *buf = NULL;
1100 int err;
1101
1102 if (copy_from_user(&bp, argp, sizeof(bp)))
1103 return -EFAULT;
1104
1105 err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf);
1106 if (err)
1107 return err;
1108 exp_info.ops = &fastrpc_dma_buf_ops;
1109 exp_info.size = bp.size;
1110 exp_info.flags = O_RDWR;
1111 exp_info.priv = buf;
1112 buf->dmabuf = dma_buf_export(&exp_info);
1113 if (IS_ERR(buf->dmabuf)) {
1114 err = PTR_ERR(buf->dmabuf);
1115 fastrpc_buf_free(buf);
1116 return err;
1117 }
1118
1119 bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE);
1120 if (bp.fd < 0) {
1121 dma_buf_put(buf->dmabuf);
1122 return -EINVAL;
1123 }
1124
1125 if (copy_to_user(argp, &bp, sizeof(bp))) {
1126 dma_buf_put(buf->dmabuf);
1127 return -EFAULT;
1128 }
1129
1130 get_dma_buf(buf->dmabuf);
1131
1132 return 0;
1133}
1134
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001135static int fastrpc_init_attach(struct fastrpc_user *fl)
1136{
1137 struct fastrpc_invoke_args args[1];
1138 int tgid = fl->tgid;
1139 u32 sc;
1140
1141 args[0].ptr = (u64)(uintptr_t) &tgid;
1142 args[0].length = sizeof(tgid);
1143 args[0].fd = -1;
1144 args[0].reserved = 0;
1145 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0);
1146 fl->pd = 0;
1147
1148 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1149 sc, &args[0]);
1150}
1151
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001152static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp)
1153{
1154 struct fastrpc_invoke_args *args = NULL;
1155 struct fastrpc_invoke inv;
1156 u32 nscalars;
1157 int err;
1158
1159 if (copy_from_user(&inv, argp, sizeof(inv)))
1160 return -EFAULT;
1161
1162 /* nscalars is truncated here to max supported value */
1163 nscalars = REMOTE_SCALARS_LENGTH(inv.sc);
1164 if (nscalars) {
1165 args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL);
1166 if (!args)
1167 return -ENOMEM;
1168
1169 if (copy_from_user(args, (void __user *)(uintptr_t)inv.args,
1170 nscalars * sizeof(*args))) {
1171 kfree(args);
1172 return -EFAULT;
1173 }
1174 }
1175
1176 err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args);
1177 kfree(args);
1178
1179 return err;
1180}
1181
1182static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
1183 unsigned long arg)
1184{
1185 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1186 char __user *argp = (char __user *)arg;
1187 int err;
1188
1189 switch (cmd) {
1190 case FASTRPC_IOCTL_INVOKE:
1191 err = fastrpc_invoke(fl, argp);
1192 break;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001193 case FASTRPC_IOCTL_INIT_ATTACH:
1194 err = fastrpc_init_attach(fl);
1195 break;
1196 case FASTRPC_IOCTL_INIT_CREATE:
1197 err = fastrpc_init_create_process(fl, argp);
1198 break;
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +00001199 case FASTRPC_IOCTL_FREE_DMA_BUFF:
1200 err = fastrpc_dmabuf_free(fl, argp);
1201 break;
1202 case FASTRPC_IOCTL_ALLOC_DMA_BUFF:
1203 err = fastrpc_dmabuf_alloc(fl, argp);
1204 break;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001205 default:
1206 err = -ENOTTY;
1207 break;
1208 }
1209
1210 return err;
1211}
1212
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001213static const struct file_operations fastrpc_fops = {
1214 .open = fastrpc_device_open,
1215 .release = fastrpc_device_release,
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001216 .unlocked_ioctl = fastrpc_device_ioctl,
1217 .compat_ioctl = fastrpc_device_ioctl,
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001218};
1219
1220static int fastrpc_cb_probe(struct platform_device *pdev)
1221{
1222 struct fastrpc_channel_ctx *cctx;
1223 struct fastrpc_session_ctx *sess;
1224 struct device *dev = &pdev->dev;
1225 int i, sessions = 0;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001226 unsigned long flags;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001227
1228 cctx = dev_get_drvdata(dev->parent);
1229 if (!cctx)
1230 return -EINVAL;
1231
1232 of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions);
1233
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001234 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001235 sess = &cctx->session[cctx->sesscount];
1236 sess->used = false;
1237 sess->valid = true;
1238 sess->dev = dev;
1239 dev_set_drvdata(dev, sess);
1240
1241 if (of_property_read_u32(dev->of_node, "reg", &sess->sid))
1242 dev_info(dev, "FastRPC Session ID not specified in DT\n");
1243
1244 if (sessions > 0) {
1245 struct fastrpc_session_ctx *dup_sess;
1246
1247 for (i = 1; i < sessions; i++) {
1248 if (cctx->sesscount++ >= FASTRPC_MAX_SESSIONS)
1249 break;
1250 dup_sess = &cctx->session[cctx->sesscount];
1251 memcpy(dup_sess, sess, sizeof(*dup_sess));
1252 }
1253 }
1254 cctx->sesscount++;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001255 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001256 dma_set_mask(dev, DMA_BIT_MASK(32));
1257
1258 return 0;
1259}
1260
1261static int fastrpc_cb_remove(struct platform_device *pdev)
1262{
1263 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent);
1264 struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev);
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001265 unsigned long flags;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001266 int i;
1267
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001268 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001269 for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) {
1270 if (cctx->session[i].sid == sess->sid) {
1271 cctx->session[i].valid = false;
1272 cctx->sesscount--;
1273 }
1274 }
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001275 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001276
1277 return 0;
1278}
1279
1280static const struct of_device_id fastrpc_match_table[] = {
1281 { .compatible = "qcom,fastrpc-compute-cb", },
1282 {}
1283};
1284
1285static struct platform_driver fastrpc_cb_driver = {
1286 .probe = fastrpc_cb_probe,
1287 .remove = fastrpc_cb_remove,
1288 .driver = {
1289 .name = "qcom,fastrpc-cb",
1290 .of_match_table = fastrpc_match_table,
1291 .suppress_bind_attrs = true,
1292 },
1293};
1294
1295static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
1296{
1297 struct device *rdev = &rpdev->dev;
1298 struct fastrpc_channel_ctx *data;
1299 int i, err, domain_id = -1;
1300 const char *domain;
1301
1302 data = devm_kzalloc(rdev, sizeof(*data), GFP_KERNEL);
1303 if (!data)
1304 return -ENOMEM;
1305
1306 err = of_property_read_string(rdev->of_node, "label", &domain);
1307 if (err) {
1308 dev_info(rdev, "FastRPC Domain not specified in DT\n");
1309 return err;
1310 }
1311
1312 for (i = 0; i <= CDSP_DOMAIN_ID; i++) {
1313 if (!strcmp(domains[i], domain)) {
1314 domain_id = i;
1315 break;
1316 }
1317 }
1318
1319 if (domain_id < 0) {
1320 dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id);
1321 return -EINVAL;
1322 }
1323
1324 data->miscdev.minor = MISC_DYNAMIC_MINOR;
1325 data->miscdev.name = kasprintf(GFP_KERNEL, "fastrpc-%s",
1326 domains[domain_id]);
1327 data->miscdev.fops = &fastrpc_fops;
1328 err = misc_register(&data->miscdev);
1329 if (err)
1330 return err;
1331
1332 dev_set_drvdata(&rpdev->dev, data);
1333 dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
1334 INIT_LIST_HEAD(&data->users);
1335 spin_lock_init(&data->lock);
1336 idr_init(&data->ctx_idr);
1337 data->domain_id = domain_id;
1338 data->rpdev = rpdev;
1339
1340 return of_platform_populate(rdev->of_node, NULL, NULL, rdev);
1341}
1342
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001343static void fastrpc_notify_users(struct fastrpc_user *user)
1344{
1345 struct fastrpc_invoke_ctx *ctx;
1346
1347 spin_lock(&user->lock);
1348 list_for_each_entry(ctx, &user->pending, node)
1349 complete(&ctx->work);
1350 spin_unlock(&user->lock);
1351}
1352
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001353static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
1354{
1355 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001356 struct fastrpc_user *user;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001357 unsigned long flags;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001358
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001359 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001360 list_for_each_entry(user, &cctx->users, user)
1361 fastrpc_notify_users(user);
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001362 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001363
1364 misc_deregister(&cctx->miscdev);
1365 of_platform_depopulate(&rpdev->dev);
1366 kfree(cctx);
1367}
1368
1369static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
1370 int len, void *priv, u32 addr)
1371{
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001372 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
1373 struct fastrpc_invoke_rsp *rsp = data;
1374 struct fastrpc_invoke_ctx *ctx;
1375 unsigned long flags;
1376 unsigned long ctxid;
1377
1378 if (len < sizeof(*rsp))
1379 return -EINVAL;
1380
1381 ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4);
1382
1383 spin_lock_irqsave(&cctx->lock, flags);
1384 ctx = idr_find(&cctx->ctx_idr, ctxid);
1385 spin_unlock_irqrestore(&cctx->lock, flags);
1386
1387 if (!ctx) {
1388 dev_err(&rpdev->dev, "No context ID matches response\n");
1389 return -ENOENT;
1390 }
1391
1392 ctx->retval = rsp->retval;
1393 complete(&ctx->work);
Thierry Escande8e7389c2019-03-07 10:12:22 +00001394
1395 /*
1396 * The DMA buffer associated with the context cannot be freed in
1397 * interrupt context so schedule it through a worker thread to
1398 * avoid a kernel BUG.
1399 */
1400 schedule_work(&ctx->put_work);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001401
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001402 return 0;
1403}
1404
1405static const struct of_device_id fastrpc_rpmsg_of_match[] = {
1406 { .compatible = "qcom,fastrpc" },
1407 { },
1408};
1409MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match);
1410
1411static struct rpmsg_driver fastrpc_driver = {
1412 .probe = fastrpc_rpmsg_probe,
1413 .remove = fastrpc_rpmsg_remove,
1414 .callback = fastrpc_rpmsg_callback,
1415 .drv = {
1416 .name = "qcom,fastrpc",
1417 .of_match_table = fastrpc_rpmsg_of_match,
1418 },
1419};
1420
1421static int fastrpc_init(void)
1422{
1423 int ret;
1424
1425 ret = platform_driver_register(&fastrpc_cb_driver);
1426 if (ret < 0) {
1427 pr_err("fastrpc: failed to register cb driver\n");
1428 return ret;
1429 }
1430
1431 ret = register_rpmsg_driver(&fastrpc_driver);
1432 if (ret < 0) {
1433 pr_err("fastrpc: failed to register rpmsg driver\n");
1434 platform_driver_unregister(&fastrpc_cb_driver);
1435 return ret;
1436 }
1437
1438 return 0;
1439}
1440module_init(fastrpc_init);
1441
1442static void fastrpc_exit(void)
1443{
1444 platform_driver_unregister(&fastrpc_cb_driver);
1445 unregister_rpmsg_driver(&fastrpc_driver);
1446}
1447module_exit(fastrpc_exit);
1448
1449MODULE_LICENSE("GPL v2");