blob: 80fec7b619cc8d333cef706efe5566d990810dbc [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>
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +000015#include <linux/sort.h>
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +000016#include <linux/of_platform.h>
17#include <linux/rpmsg.h>
18#include <linux/scatterlist.h>
19#include <linux/slab.h>
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +000020#include <uapi/misc/fastrpc.h>
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +000021
22#define ADSP_DOMAIN_ID (0)
23#define MDSP_DOMAIN_ID (1)
24#define SDSP_DOMAIN_ID (2)
25#define CDSP_DOMAIN_ID (3)
26#define FASTRPC_DEV_MAX 4 /* adsp, mdsp, slpi, cdsp*/
27#define FASTRPC_MAX_SESSIONS 9 /*8 compute, 1 cpz*/
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +000028#define FASTRPC_ALIGN 128
29#define FASTRPC_MAX_FDLIST 16
30#define FASTRPC_MAX_CRCLIST 64
31#define FASTRPC_PHYS(p) ((p) & 0xffffffff)
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +000032#define FASTRPC_CTX_MAX (256)
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +000033#define FASTRPC_INIT_HANDLE 1
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +000034#define FASTRPC_CTXID_MASK (0xFF0)
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +000035#define INIT_FILELEN_MAX (2 * 1024 * 1024)
36#define INIT_MEMLEN_MAX (8 * 1024 * 1024)
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +000037#define FASTRPC_DEVICE_NAME "fastrpc"
38
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +000039/* Retrives number of input buffers from the scalars parameter */
40#define REMOTE_SCALARS_INBUFS(sc) (((sc) >> 16) & 0x0ff)
41
42/* Retrives number of output buffers from the scalars parameter */
43#define REMOTE_SCALARS_OUTBUFS(sc) (((sc) >> 8) & 0x0ff)
44
45/* Retrives number of input handles from the scalars parameter */
46#define REMOTE_SCALARS_INHANDLES(sc) (((sc) >> 4) & 0x0f)
47
48/* Retrives number of output handles from the scalars parameter */
49#define REMOTE_SCALARS_OUTHANDLES(sc) ((sc) & 0x0f)
50
51#define REMOTE_SCALARS_LENGTH(sc) (REMOTE_SCALARS_INBUFS(sc) + \
52 REMOTE_SCALARS_OUTBUFS(sc) + \
53 REMOTE_SCALARS_INHANDLES(sc)+ \
54 REMOTE_SCALARS_OUTHANDLES(sc))
55#define FASTRPC_BUILD_SCALARS(attr, method, in, out, oin, oout) \
56 (((attr & 0x07) << 29) | \
57 ((method & 0x1f) << 24) | \
58 ((in & 0xff) << 16) | \
59 ((out & 0xff) << 8) | \
60 ((oin & 0x0f) << 4) | \
61 (oout & 0x0f))
62
63#define FASTRPC_SCALARS(method, in, out) \
64 FASTRPC_BUILD_SCALARS(0, method, in, out, 0, 0)
65
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +000066#define FASTRPC_CREATE_PROCESS_NARGS 6
67/* Remote Method id table */
68#define FASTRPC_RMID_INIT_ATTACH 0
69#define FASTRPC_RMID_INIT_RELEASE 1
70#define FASTRPC_RMID_INIT_CREATE 6
71#define FASTRPC_RMID_INIT_CREATE_ATTR 7
72#define FASTRPC_RMID_INIT_CREATE_STATIC 8
73
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +000074#define miscdev_to_cctx(d) container_of(d, struct fastrpc_channel_ctx, miscdev)
75
76static const char *domains[FASTRPC_DEV_MAX] = { "adsp", "mdsp",
77 "sdsp", "cdsp"};
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +000078struct fastrpc_phy_page {
79 u64 addr; /* physical address */
80 u64 size; /* size of contiguous region */
81};
82
83struct fastrpc_invoke_buf {
84 u32 num; /* number of contiguous regions */
85 u32 pgidx; /* index to start of contiguous region */
86};
87
88struct fastrpc_remote_arg {
89 u64 pv;
90 u64 len;
91};
92
93struct fastrpc_msg {
94 int pid; /* process group id */
95 int tid; /* thread id */
96 u64 ctx; /* invoke caller context */
97 u32 handle; /* handle to invoke */
98 u32 sc; /* scalars structure describing the data */
99 u64 addr; /* physical address */
100 u64 size; /* size of contiguous region */
101};
102
103struct fastrpc_invoke_rsp {
104 u64 ctx; /* invoke caller context */
105 int retval; /* invoke return value */
106};
107
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000108struct fastrpc_buf_overlap {
109 u64 start;
110 u64 end;
111 int raix;
112 u64 mstart;
113 u64 mend;
114 u64 offset;
115};
116
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000117struct fastrpc_buf {
118 struct fastrpc_user *fl;
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +0000119 struct dma_buf *dmabuf;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000120 struct device *dev;
121 void *virt;
122 u64 phys;
123 u64 size;
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +0000124 /* Lock for dma buf attachments */
125 struct mutex lock;
126 struct list_head attachments;
127};
128
129struct fastrpc_dma_buf_attachment {
130 struct device *dev;
131 struct sg_table sgt;
132 struct list_head node;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000133};
134
135struct fastrpc_map {
136 struct list_head node;
137 struct fastrpc_user *fl;
138 int fd;
139 struct dma_buf *buf;
140 struct sg_table *table;
141 struct dma_buf_attachment *attach;
142 u64 phys;
143 u64 size;
144 void *va;
145 u64 len;
146 struct kref refcount;
147};
148
149struct fastrpc_invoke_ctx {
150 int nscalars;
151 int nbufs;
152 int retval;
153 int pid;
154 int tgid;
155 u32 sc;
156 u32 *crc;
157 u64 ctxid;
158 u64 msg_sz;
159 struct kref refcount;
160 struct list_head node; /* list of ctxs */
161 struct completion work;
Thierry Escande8e7389c2019-03-07 10:12:22 +0000162 struct work_struct put_work;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000163 struct fastrpc_msg msg;
164 struct fastrpc_user *fl;
165 struct fastrpc_remote_arg *rpra;
166 struct fastrpc_map **maps;
167 struct fastrpc_buf *buf;
168 struct fastrpc_invoke_args *args;
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000169 struct fastrpc_buf_overlap *olaps;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000170 struct fastrpc_channel_ctx *cctx;
171};
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +0000172
173struct fastrpc_session_ctx {
174 struct device *dev;
175 int sid;
176 bool used;
177 bool valid;
178};
179
180struct fastrpc_channel_ctx {
181 int domain_id;
182 int sesscount;
183 struct rpmsg_device *rpdev;
184 struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS];
185 spinlock_t lock;
186 struct idr ctx_idr;
187 struct list_head users;
188 struct miscdevice miscdev;
189};
190
191struct fastrpc_user {
192 struct list_head user;
193 struct list_head maps;
194 struct list_head pending;
195
196 struct fastrpc_channel_ctx *cctx;
197 struct fastrpc_session_ctx *sctx;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000198 struct fastrpc_buf *init_mem;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +0000199
200 int tgid;
201 int pd;
202 /* Lock for lists */
203 spinlock_t lock;
204 /* lock for allocations */
205 struct mutex mutex;
206};
207
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000208static void fastrpc_free_map(struct kref *ref)
209{
210 struct fastrpc_map *map;
211
212 map = container_of(ref, struct fastrpc_map, refcount);
213
214 if (map->table) {
215 dma_buf_unmap_attachment(map->attach, map->table,
216 DMA_BIDIRECTIONAL);
217 dma_buf_detach(map->buf, map->attach);
218 dma_buf_put(map->buf);
219 }
220
221 kfree(map);
222}
223
224static void fastrpc_map_put(struct fastrpc_map *map)
225{
226 if (map)
227 kref_put(&map->refcount, fastrpc_free_map);
228}
229
230static void fastrpc_map_get(struct fastrpc_map *map)
231{
232 if (map)
233 kref_get(&map->refcount);
234}
235
236static int fastrpc_map_find(struct fastrpc_user *fl, int fd,
237 struct fastrpc_map **ppmap)
238{
239 struct fastrpc_map *map = NULL;
240
241 mutex_lock(&fl->mutex);
242 list_for_each_entry(map, &fl->maps, node) {
243 if (map->fd == fd) {
244 fastrpc_map_get(map);
245 *ppmap = map;
246 mutex_unlock(&fl->mutex);
247 return 0;
248 }
249 }
250 mutex_unlock(&fl->mutex);
251
252 return -ENOENT;
253}
254
255static void fastrpc_buf_free(struct fastrpc_buf *buf)
256{
257 dma_free_coherent(buf->dev, buf->size, buf->virt,
258 FASTRPC_PHYS(buf->phys));
259 kfree(buf);
260}
261
262static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
263 u64 size, struct fastrpc_buf **obuf)
264{
265 struct fastrpc_buf *buf;
266
267 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
268 if (!buf)
269 return -ENOMEM;
270
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +0000271 INIT_LIST_HEAD(&buf->attachments);
272 mutex_init(&buf->lock);
273
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000274 buf->fl = fl;
275 buf->virt = NULL;
276 buf->phys = 0;
277 buf->size = size;
278 buf->dev = dev;
279
280 buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys,
281 GFP_KERNEL);
282 if (!buf->virt)
283 return -ENOMEM;
284
285 if (fl->sctx && fl->sctx->sid)
286 buf->phys += ((u64)fl->sctx->sid << 32);
287
288 *obuf = buf;
289
290 return 0;
291}
292
293static void fastrpc_context_free(struct kref *ref)
294{
295 struct fastrpc_invoke_ctx *ctx;
296 struct fastrpc_channel_ctx *cctx;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000297 unsigned long flags;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000298 int i;
299
300 ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
301 cctx = ctx->cctx;
302
303 for (i = 0; i < ctx->nscalars; i++)
304 fastrpc_map_put(ctx->maps[i]);
305
306 if (ctx->buf)
307 fastrpc_buf_free(ctx->buf);
308
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000309 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000310 idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4);
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000311 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000312
313 kfree(ctx->maps);
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000314 kfree(ctx->olaps);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000315 kfree(ctx);
316}
317
318static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx)
319{
320 kref_get(&ctx->refcount);
321}
322
323static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx)
324{
325 kref_put(&ctx->refcount, fastrpc_context_free);
326}
327
Thierry Escande8e7389c2019-03-07 10:12:22 +0000328static void fastrpc_context_put_wq(struct work_struct *work)
329{
330 struct fastrpc_invoke_ctx *ctx =
331 container_of(work, struct fastrpc_invoke_ctx, put_work);
332
333 fastrpc_context_put(ctx);
334}
335
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000336#define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)
337static int olaps_cmp(const void *a, const void *b)
338{
339 struct fastrpc_buf_overlap *pa = (struct fastrpc_buf_overlap *)a;
340 struct fastrpc_buf_overlap *pb = (struct fastrpc_buf_overlap *)b;
341 /* sort with lowest starting buffer first */
342 int st = CMP(pa->start, pb->start);
343 /* sort with highest ending buffer first */
344 int ed = CMP(pb->end, pa->end);
345
346 return st == 0 ? ed : st;
347}
348
349static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
350{
351 u64 max_end = 0;
352 int i;
353
354 for (i = 0; i < ctx->nbufs; ++i) {
355 ctx->olaps[i].start = ctx->args[i].ptr;
356 ctx->olaps[i].end = ctx->olaps[i].start + ctx->args[i].length;
357 ctx->olaps[i].raix = i;
358 }
359
360 sort(ctx->olaps, ctx->nbufs, sizeof(*ctx->olaps), olaps_cmp, NULL);
361
362 for (i = 0; i < ctx->nbufs; ++i) {
363 /* Falling inside previous range */
364 if (ctx->olaps[i].start < max_end) {
365 ctx->olaps[i].mstart = max_end;
366 ctx->olaps[i].mend = ctx->olaps[i].end;
367 ctx->olaps[i].offset = max_end - ctx->olaps[i].start;
368
369 if (ctx->olaps[i].end > max_end) {
370 max_end = ctx->olaps[i].end;
371 } else {
372 ctx->olaps[i].mend = 0;
373 ctx->olaps[i].mstart = 0;
374 }
375
376 } else {
377 ctx->olaps[i].mend = ctx->olaps[i].end;
378 ctx->olaps[i].mstart = ctx->olaps[i].start;
379 ctx->olaps[i].offset = 0;
380 max_end = ctx->olaps[i].end;
381 }
382 }
383}
384
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000385static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
386 struct fastrpc_user *user, u32 kernel, u32 sc,
387 struct fastrpc_invoke_args *args)
388{
389 struct fastrpc_channel_ctx *cctx = user->cctx;
390 struct fastrpc_invoke_ctx *ctx = NULL;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000391 unsigned long flags;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000392 int ret;
393
394 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
395 if (!ctx)
396 return ERR_PTR(-ENOMEM);
397
398 INIT_LIST_HEAD(&ctx->node);
399 ctx->fl = user;
400 ctx->nscalars = REMOTE_SCALARS_LENGTH(sc);
401 ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) +
402 REMOTE_SCALARS_OUTBUFS(sc);
403
404 if (ctx->nscalars) {
405 ctx->maps = kcalloc(ctx->nscalars,
406 sizeof(*ctx->maps), GFP_KERNEL);
407 if (!ctx->maps) {
408 kfree(ctx);
409 return ERR_PTR(-ENOMEM);
410 }
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000411 ctx->olaps = kcalloc(ctx->nscalars,
412 sizeof(*ctx->olaps), GFP_KERNEL);
413 if (!ctx->olaps) {
414 kfree(ctx->maps);
415 kfree(ctx);
416 return ERR_PTR(-ENOMEM);
417 }
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000418 ctx->args = args;
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000419 fastrpc_get_buff_overlaps(ctx);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000420 }
421
422 ctx->sc = sc;
423 ctx->retval = -1;
424 ctx->pid = current->pid;
425 ctx->tgid = user->tgid;
426 ctx->cctx = cctx;
427 init_completion(&ctx->work);
Thierry Escande8e7389c2019-03-07 10:12:22 +0000428 INIT_WORK(&ctx->put_work, fastrpc_context_put_wq);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000429
430 spin_lock(&user->lock);
431 list_add_tail(&ctx->node, &user->pending);
432 spin_unlock(&user->lock);
433
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000434 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000435 ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1,
436 FASTRPC_CTX_MAX, GFP_ATOMIC);
437 if (ret < 0) {
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000438 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000439 goto err_idr;
440 }
441 ctx->ctxid = ret << 4;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000442 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000443
444 kref_init(&ctx->refcount);
445
446 return ctx;
447err_idr:
448 spin_lock(&user->lock);
449 list_del(&ctx->node);
450 spin_unlock(&user->lock);
451 kfree(ctx->maps);
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000452 kfree(ctx->olaps);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000453 kfree(ctx);
454
455 return ERR_PTR(ret);
456}
457
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +0000458static struct sg_table *
459fastrpc_map_dma_buf(struct dma_buf_attachment *attachment,
460 enum dma_data_direction dir)
461{
462 struct fastrpc_dma_buf_attachment *a = attachment->priv;
463 struct sg_table *table;
464
465 table = &a->sgt;
466
467 if (!dma_map_sg(attachment->dev, table->sgl, table->nents, dir))
468 return ERR_PTR(-ENOMEM);
469
470 return table;
471}
472
473static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach,
474 struct sg_table *table,
475 enum dma_data_direction dir)
476{
477 dma_unmap_sg(attach->dev, table->sgl, table->nents, dir);
478}
479
480static void fastrpc_release(struct dma_buf *dmabuf)
481{
482 struct fastrpc_buf *buffer = dmabuf->priv;
483
484 fastrpc_buf_free(buffer);
485}
486
487static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf,
488 struct dma_buf_attachment *attachment)
489{
490 struct fastrpc_dma_buf_attachment *a;
491 struct fastrpc_buf *buffer = dmabuf->priv;
492 int ret;
493
494 a = kzalloc(sizeof(*a), GFP_KERNEL);
495 if (!a)
496 return -ENOMEM;
497
498 ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt,
499 FASTRPC_PHYS(buffer->phys), buffer->size);
500 if (ret < 0) {
501 dev_err(buffer->dev, "failed to get scatterlist from DMA API\n");
502 return -EINVAL;
503 }
504
505 a->dev = attachment->dev;
506 INIT_LIST_HEAD(&a->node);
507 attachment->priv = a;
508
509 mutex_lock(&buffer->lock);
510 list_add(&a->node, &buffer->attachments);
511 mutex_unlock(&buffer->lock);
512
513 return 0;
514}
515
516static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf,
517 struct dma_buf_attachment *attachment)
518{
519 struct fastrpc_dma_buf_attachment *a = attachment->priv;
520 struct fastrpc_buf *buffer = dmabuf->priv;
521
522 mutex_lock(&buffer->lock);
523 list_del(&a->node);
524 mutex_unlock(&buffer->lock);
525 kfree(a);
526}
527
528static void *fastrpc_kmap(struct dma_buf *dmabuf, unsigned long pgnum)
529{
530 struct fastrpc_buf *buf = dmabuf->priv;
531
532 return buf->virt ? buf->virt + pgnum * PAGE_SIZE : NULL;
533}
534
535static void *fastrpc_vmap(struct dma_buf *dmabuf)
536{
537 struct fastrpc_buf *buf = dmabuf->priv;
538
539 return buf->virt;
540}
541
542static int fastrpc_mmap(struct dma_buf *dmabuf,
543 struct vm_area_struct *vma)
544{
545 struct fastrpc_buf *buf = dmabuf->priv;
546 size_t size = vma->vm_end - vma->vm_start;
547
548 return dma_mmap_coherent(buf->dev, vma, buf->virt,
549 FASTRPC_PHYS(buf->phys), size);
550}
551
552static const struct dma_buf_ops fastrpc_dma_buf_ops = {
553 .attach = fastrpc_dma_buf_attach,
554 .detach = fastrpc_dma_buf_detatch,
555 .map_dma_buf = fastrpc_map_dma_buf,
556 .unmap_dma_buf = fastrpc_unmap_dma_buf,
557 .mmap = fastrpc_mmap,
558 .map = fastrpc_kmap,
559 .vmap = fastrpc_vmap,
560 .release = fastrpc_release,
561};
562
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000563static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
564 u64 len, struct fastrpc_map **ppmap)
565{
566 struct fastrpc_session_ctx *sess = fl->sctx;
567 struct fastrpc_map *map = NULL;
568 int err = 0;
569
570 if (!fastrpc_map_find(fl, fd, ppmap))
571 return 0;
572
573 map = kzalloc(sizeof(*map), GFP_KERNEL);
574 if (!map)
575 return -ENOMEM;
576
577 INIT_LIST_HEAD(&map->node);
578 map->fl = fl;
579 map->fd = fd;
580 map->buf = dma_buf_get(fd);
Wei Yongjun682a6042019-02-16 01:35:43 +0000581 if (IS_ERR(map->buf)) {
582 err = PTR_ERR(map->buf);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000583 goto get_err;
584 }
585
586 map->attach = dma_buf_attach(map->buf, sess->dev);
587 if (IS_ERR(map->attach)) {
588 dev_err(sess->dev, "Failed to attach dmabuf\n");
589 err = PTR_ERR(map->attach);
590 goto attach_err;
591 }
592
593 map->table = dma_buf_map_attachment(map->attach, DMA_BIDIRECTIONAL);
594 if (IS_ERR(map->table)) {
595 err = PTR_ERR(map->table);
596 goto map_err;
597 }
598
599 map->phys = sg_dma_address(map->table->sgl);
600 map->phys += ((u64)fl->sctx->sid << 32);
601 map->size = len;
602 map->va = sg_virt(map->table->sgl);
603 map->len = len;
604 kref_init(&map->refcount);
605
606 spin_lock(&fl->lock);
607 list_add_tail(&map->node, &fl->maps);
608 spin_unlock(&fl->lock);
609 *ppmap = map;
610
611 return 0;
612
613map_err:
614 dma_buf_detach(map->buf, map->attach);
615attach_err:
616 dma_buf_put(map->buf);
617get_err:
618 kfree(map);
619
620 return err;
621}
622
623/*
624 * Fastrpc payload buffer with metadata looks like:
625 *
626 * >>>>>> START of METADATA <<<<<<<<<
627 * +---------------------------------+
628 * | Arguments |
629 * | type:(struct fastrpc_remote_arg)|
630 * | (0 - N) |
631 * +---------------------------------+
632 * | Invoke Buffer list |
633 * | type:(struct fastrpc_invoke_buf)|
634 * | (0 - N) |
635 * +---------------------------------+
636 * | Page info list |
637 * | type:(struct fastrpc_phy_page) |
638 * | (0 - N) |
639 * +---------------------------------+
640 * | Optional info |
641 * |(can be specific to SoC/Firmware)|
642 * +---------------------------------+
643 * >>>>>>>> END of METADATA <<<<<<<<<
644 * +---------------------------------+
645 * | Inline ARGS |
646 * | (0-N) |
647 * +---------------------------------+
648 */
649
650static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx)
651{
652 int size = 0;
653
654 size = (sizeof(struct fastrpc_remote_arg) +
655 sizeof(struct fastrpc_invoke_buf) +
656 sizeof(struct fastrpc_phy_page)) * ctx->nscalars +
657 sizeof(u64) * FASTRPC_MAX_FDLIST +
658 sizeof(u32) * FASTRPC_MAX_CRCLIST;
659
660 return size;
661}
662
663static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen)
664{
665 u64 size = 0;
666 int i;
667
668 size = ALIGN(metalen, FASTRPC_ALIGN);
669 for (i = 0; i < ctx->nscalars; i++) {
670 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) {
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000671
672 if (ctx->olaps[i].offset == 0)
673 size = ALIGN(size, FASTRPC_ALIGN);
674
675 size += (ctx->olaps[i].mend - ctx->olaps[i].mstart);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000676 }
677 }
678
679 return size;
680}
681
682static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
683{
684 struct device *dev = ctx->fl->sctx->dev;
685 int i, err;
686
687 for (i = 0; i < ctx->nscalars; ++i) {
688 /* Make sure reserved field is set to 0 */
689 if (ctx->args[i].reserved)
690 return -EINVAL;
691
692 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 ||
693 ctx->args[i].length == 0)
694 continue;
695
696 err = fastrpc_map_create(ctx->fl, ctx->args[i].fd,
697 ctx->args[i].length, &ctx->maps[i]);
698 if (err) {
699 dev_err(dev, "Error Creating map %d\n", err);
700 return -EINVAL;
701 }
702
703 }
704 return 0;
705}
706
707static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
708{
709 struct device *dev = ctx->fl->sctx->dev;
710 struct fastrpc_remote_arg *rpra;
711 struct fastrpc_invoke_buf *list;
712 struct fastrpc_phy_page *pages;
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000713 int inbufs, i, oix, err = 0;
714 u64 len, rlen, pkt_size;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000715 uintptr_t args;
716 int metalen;
717
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000718 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
719 metalen = fastrpc_get_meta_size(ctx);
720 pkt_size = fastrpc_get_payload_size(ctx, metalen);
721
722 err = fastrpc_create_maps(ctx);
723 if (err)
724 return err;
725
726 ctx->msg_sz = pkt_size;
727
728 err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf);
729 if (err)
730 return err;
731
732 rpra = ctx->buf->virt;
733 list = ctx->buf->virt + ctx->nscalars * sizeof(*rpra);
734 pages = ctx->buf->virt + ctx->nscalars * (sizeof(*list) +
735 sizeof(*rpra));
736 args = (uintptr_t)ctx->buf->virt + metalen;
737 rlen = pkt_size - metalen;
738 ctx->rpra = rpra;
739
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000740 for (oix = 0; oix < ctx->nbufs; ++oix) {
741 int mlen;
742
743 i = ctx->olaps[oix].raix;
744 len = ctx->args[i].length;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000745
746 rpra[i].pv = 0;
747 rpra[i].len = len;
748 list[i].num = len ? 1 : 0;
749 list[i].pgidx = i;
750
751 if (!len)
752 continue;
753
754 pages[i].size = roundup(len, PAGE_SIZE);
755
756 if (ctx->maps[i]) {
Srinivas Kandagatla80f3afd2019-03-07 10:12:26 +0000757 struct vm_area_struct *vma = NULL;
758
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000759 rpra[i].pv = (u64) ctx->args[i].ptr;
760 pages[i].addr = ctx->maps[i]->phys;
Srinivas Kandagatla80f3afd2019-03-07 10:12:26 +0000761
762 vma = find_vma(current->mm, ctx->args[i].ptr);
763 if (vma)
764 pages[i].addr += ctx->args[i].ptr -
765 vma->vm_start;
766
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000767 } else {
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000768
769 if (ctx->olaps[oix].offset == 0) {
770 rlen -= ALIGN(args, FASTRPC_ALIGN) - args;
771 args = ALIGN(args, FASTRPC_ALIGN);
772 }
773
774 mlen = ctx->olaps[oix].mend - ctx->olaps[oix].mstart;
775
776 if (rlen < mlen)
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000777 goto bail;
778
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000779 rpra[i].pv = args - ctx->olaps[oix].offset;
780 pages[i].addr = ctx->buf->phys -
781 ctx->olaps[oix].offset +
782 (pkt_size - rlen);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000783 pages[i].addr = pages[i].addr & PAGE_MASK;
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000784
785 args = args + mlen;
786 rlen -= mlen;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000787 }
788
789 if (i < inbufs && !ctx->maps[i]) {
790 void *dst = (void *)(uintptr_t)rpra[i].pv;
791 void *src = (void *)(uintptr_t)ctx->args[i].ptr;
792
793 if (!kernel) {
794 if (copy_from_user(dst, (void __user *)src,
795 len)) {
796 err = -EFAULT;
797 goto bail;
798 }
799 } else {
800 memcpy(dst, src, len);
801 }
802 }
803 }
804
805 for (i = ctx->nbufs; i < ctx->nscalars; ++i) {
806 rpra[i].pv = (u64) ctx->args[i].ptr;
807 rpra[i].len = ctx->args[i].length;
808 list[i].num = ctx->args[i].length ? 1 : 0;
809 list[i].pgidx = i;
810 pages[i].addr = ctx->maps[i]->phys;
811 pages[i].size = ctx->maps[i]->size;
812 }
813
814bail:
815 if (err)
816 dev_err(dev, "Error: get invoke args failed:%d\n", err);
817
818 return err;
819}
820
821static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
822 u32 kernel)
823{
824 struct fastrpc_remote_arg *rpra = ctx->rpra;
825 int i, inbufs;
826
827 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
828
829 for (i = inbufs; i < ctx->nbufs; ++i) {
830 void *src = (void *)(uintptr_t)rpra[i].pv;
831 void *dst = (void *)(uintptr_t)ctx->args[i].ptr;
832 u64 len = rpra[i].len;
833
834 if (!kernel) {
835 if (copy_to_user((void __user *)dst, src, len))
836 return -EFAULT;
837 } else {
838 memcpy(dst, src, len);
839 }
840 }
841
842 return 0;
843}
844
845static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx,
846 struct fastrpc_invoke_ctx *ctx,
847 u32 kernel, uint32_t handle)
848{
849 struct fastrpc_channel_ctx *cctx;
850 struct fastrpc_user *fl = ctx->fl;
851 struct fastrpc_msg *msg = &ctx->msg;
852
853 cctx = fl->cctx;
854 msg->pid = fl->tgid;
855 msg->tid = current->pid;
856
857 if (kernel)
858 msg->pid = 0;
859
860 msg->ctx = ctx->ctxid | fl->pd;
861 msg->handle = handle;
862 msg->sc = ctx->sc;
863 msg->addr = ctx->buf ? ctx->buf->phys : 0;
864 msg->size = roundup(ctx->msg_sz, PAGE_SIZE);
865 fastrpc_context_get(ctx);
866
867 return rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg));
868}
869
870static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
871 u32 handle, u32 sc,
872 struct fastrpc_invoke_args *args)
873{
874 struct fastrpc_invoke_ctx *ctx = NULL;
875 int err = 0;
876
877 if (!fl->sctx)
878 return -EINVAL;
879
880 ctx = fastrpc_context_alloc(fl, kernel, sc, args);
881 if (IS_ERR(ctx))
882 return PTR_ERR(ctx);
883
884 if (ctx->nscalars) {
885 err = fastrpc_get_args(kernel, ctx);
886 if (err)
887 goto bail;
888 }
Srinivas Kandagatla415a0722019-03-07 10:12:24 +0000889
890 /* make sure that all CPU memory writes are seen by DSP */
891 dma_wmb();
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000892 /* Send invoke buffer to remote dsp */
893 err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle);
894 if (err)
895 goto bail;
896
897 /* Wait for remote dsp to respond or time out */
898 err = wait_for_completion_interruptible(&ctx->work);
899 if (err)
900 goto bail;
901
902 /* Check the response from remote dsp */
903 err = ctx->retval;
904 if (err)
905 goto bail;
906
907 if (ctx->nscalars) {
Srinivas Kandagatla415a0722019-03-07 10:12:24 +0000908 /* make sure that all memory writes by DSP are seen by CPU */
909 dma_rmb();
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000910 /* populate all the output buffers with results */
911 err = fastrpc_put_args(ctx, kernel);
912 if (err)
913 goto bail;
914 }
915
916bail:
917 /* We are done with this compute context, remove it from pending list */
918 spin_lock(&fl->lock);
919 list_del(&ctx->node);
920 spin_unlock(&fl->lock);
921 fastrpc_context_put(ctx);
922
923 if (err)
924 dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err);
925
926 return err;
927}
928
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000929static int fastrpc_init_create_process(struct fastrpc_user *fl,
930 char __user *argp)
931{
932 struct fastrpc_init_create init;
933 struct fastrpc_invoke_args *args;
934 struct fastrpc_phy_page pages[1];
935 struct fastrpc_map *map = NULL;
936 struct fastrpc_buf *imem = NULL;
937 int memlen;
938 int err;
939 struct {
940 int pgid;
941 u32 namelen;
942 u32 filelen;
943 u32 pageslen;
944 u32 attrs;
945 u32 siglen;
946 } inbuf;
947 u32 sc;
948
949 args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL);
950 if (!args)
951 return -ENOMEM;
952
953 if (copy_from_user(&init, argp, sizeof(init))) {
954 err = -EFAULT;
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000955 goto err;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000956 }
957
958 if (init.filelen > INIT_FILELEN_MAX) {
959 err = -EINVAL;
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000960 goto err;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000961 }
962
963 inbuf.pgid = fl->tgid;
964 inbuf.namelen = strlen(current->comm) + 1;
965 inbuf.filelen = init.filelen;
966 inbuf.pageslen = 1;
967 inbuf.attrs = init.attrs;
968 inbuf.siglen = init.siglen;
969 fl->pd = 1;
970
971 if (init.filelen && init.filefd) {
972 err = fastrpc_map_create(fl, init.filefd, init.filelen, &map);
973 if (err)
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000974 goto err;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000975 }
976
977 memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4),
978 1024 * 1024);
979 err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen,
980 &imem);
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000981 if (err)
982 goto err_alloc;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000983
984 fl->init_mem = imem;
985 args[0].ptr = (u64)(uintptr_t)&inbuf;
986 args[0].length = sizeof(inbuf);
987 args[0].fd = -1;
988
989 args[1].ptr = (u64)(uintptr_t)current->comm;
990 args[1].length = inbuf.namelen;
991 args[1].fd = -1;
992
993 args[2].ptr = (u64) init.file;
994 args[2].length = inbuf.filelen;
995 args[2].fd = init.filefd;
996
997 pages[0].addr = imem->phys;
998 pages[0].size = imem->size;
999
1000 args[3].ptr = (u64)(uintptr_t) pages;
1001 args[3].length = 1 * sizeof(*pages);
1002 args[3].fd = -1;
1003
1004 args[4].ptr = (u64)(uintptr_t)&inbuf.attrs;
1005 args[4].length = sizeof(inbuf.attrs);
1006 args[4].fd = -1;
1007
1008 args[5].ptr = (u64)(uintptr_t) &inbuf.siglen;
1009 args[5].length = sizeof(inbuf.siglen);
1010 args[5].fd = -1;
1011
1012 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0);
1013 if (init.attrs)
1014 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0);
1015
1016 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1017 sc, args);
Thierry Escandeb49f6d82019-03-07 10:12:23 +00001018 if (err)
1019 goto err_invoke;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001020
Thierry Escandeb49f6d82019-03-07 10:12:23 +00001021 kfree(args);
1022
1023 return 0;
1024
1025err_invoke:
1026 fl->init_mem = NULL;
1027 fastrpc_buf_free(imem);
1028err_alloc:
1029 if (map) {
1030 spin_lock(&fl->lock);
1031 list_del(&map->node);
1032 spin_unlock(&fl->lock);
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001033 fastrpc_map_put(map);
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001034 }
Thierry Escandeb49f6d82019-03-07 10:12:23 +00001035err:
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001036 kfree(args);
1037
1038 return err;
1039}
1040
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001041static struct fastrpc_session_ctx *fastrpc_session_alloc(
1042 struct fastrpc_channel_ctx *cctx)
1043{
1044 struct fastrpc_session_ctx *session = NULL;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001045 unsigned long flags;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001046 int i;
1047
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001048 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001049 for (i = 0; i < cctx->sesscount; i++) {
1050 if (!cctx->session[i].used && cctx->session[i].valid) {
1051 cctx->session[i].used = true;
1052 session = &cctx->session[i];
1053 break;
1054 }
1055 }
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001056 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001057
1058 return session;
1059}
1060
1061static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx,
1062 struct fastrpc_session_ctx *session)
1063{
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001064 unsigned long flags;
1065
1066 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001067 session->used = false;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001068 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001069}
1070
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001071static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl)
1072{
1073 struct fastrpc_invoke_args args[1];
1074 int tgid = 0;
1075 u32 sc;
1076
1077 tgid = fl->tgid;
1078 args[0].ptr = (u64)(uintptr_t) &tgid;
1079 args[0].length = sizeof(tgid);
1080 args[0].fd = -1;
1081 args[0].reserved = 0;
1082 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0);
1083
1084 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1085 sc, &args[0]);
1086}
1087
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001088static int fastrpc_device_release(struct inode *inode, struct file *file)
1089{
1090 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1091 struct fastrpc_channel_ctx *cctx = fl->cctx;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001092 struct fastrpc_invoke_ctx *ctx, *n;
1093 struct fastrpc_map *map, *m;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001094 unsigned long flags;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001095
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001096 fastrpc_release_current_dsp_process(fl);
1097
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001098 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001099 list_del(&fl->user);
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001100 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001101
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001102 if (fl->init_mem)
1103 fastrpc_buf_free(fl->init_mem);
1104
1105 list_for_each_entry_safe(ctx, n, &fl->pending, node) {
1106 list_del(&ctx->node);
1107 fastrpc_context_put(ctx);
1108 }
1109
1110 list_for_each_entry_safe(map, m, &fl->maps, node) {
1111 list_del(&map->node);
1112 fastrpc_map_put(map);
1113 }
1114
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001115 fastrpc_session_free(cctx, fl->sctx);
1116
1117 mutex_destroy(&fl->mutex);
1118 kfree(fl);
1119 file->private_data = NULL;
1120
1121 return 0;
1122}
1123
1124static int fastrpc_device_open(struct inode *inode, struct file *filp)
1125{
1126 struct fastrpc_channel_ctx *cctx = miscdev_to_cctx(filp->private_data);
1127 struct fastrpc_user *fl = NULL;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001128 unsigned long flags;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001129
1130 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
1131 if (!fl)
1132 return -ENOMEM;
1133
1134 filp->private_data = fl;
1135 spin_lock_init(&fl->lock);
1136 mutex_init(&fl->mutex);
1137 INIT_LIST_HEAD(&fl->pending);
1138 INIT_LIST_HEAD(&fl->maps);
1139 INIT_LIST_HEAD(&fl->user);
1140 fl->tgid = current->tgid;
1141 fl->cctx = cctx;
Thierry Escande7c11df42019-02-15 10:40:07 +00001142
1143 fl->sctx = fastrpc_session_alloc(cctx);
1144 if (!fl->sctx) {
1145 dev_err(&cctx->rpdev->dev, "No session available\n");
1146 mutex_destroy(&fl->mutex);
1147 kfree(fl);
1148
1149 return -EBUSY;
1150 }
1151
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001152 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001153 list_add_tail(&fl->user, &cctx->users);
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001154 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001155
1156 return 0;
1157}
1158
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +00001159static int fastrpc_dmabuf_free(struct fastrpc_user *fl, char __user *argp)
1160{
1161 struct dma_buf *buf;
1162 int info;
1163
1164 if (copy_from_user(&info, argp, sizeof(info)))
1165 return -EFAULT;
1166
1167 buf = dma_buf_get(info);
1168 if (IS_ERR_OR_NULL(buf))
1169 return -EINVAL;
1170 /*
1171 * one for the last get and other for the ALLOC_DMA_BUFF ioctl
1172 */
1173 dma_buf_put(buf);
1174 dma_buf_put(buf);
1175
1176 return 0;
1177}
1178
1179static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
1180{
1181 struct fastrpc_alloc_dma_buf bp;
1182 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
1183 struct fastrpc_buf *buf = NULL;
1184 int err;
1185
1186 if (copy_from_user(&bp, argp, sizeof(bp)))
1187 return -EFAULT;
1188
1189 err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf);
1190 if (err)
1191 return err;
1192 exp_info.ops = &fastrpc_dma_buf_ops;
1193 exp_info.size = bp.size;
1194 exp_info.flags = O_RDWR;
1195 exp_info.priv = buf;
1196 buf->dmabuf = dma_buf_export(&exp_info);
1197 if (IS_ERR(buf->dmabuf)) {
1198 err = PTR_ERR(buf->dmabuf);
1199 fastrpc_buf_free(buf);
1200 return err;
1201 }
1202
1203 bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE);
1204 if (bp.fd < 0) {
1205 dma_buf_put(buf->dmabuf);
1206 return -EINVAL;
1207 }
1208
1209 if (copy_to_user(argp, &bp, sizeof(bp))) {
1210 dma_buf_put(buf->dmabuf);
1211 return -EFAULT;
1212 }
1213
1214 get_dma_buf(buf->dmabuf);
1215
1216 return 0;
1217}
1218
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001219static int fastrpc_init_attach(struct fastrpc_user *fl)
1220{
1221 struct fastrpc_invoke_args args[1];
1222 int tgid = fl->tgid;
1223 u32 sc;
1224
1225 args[0].ptr = (u64)(uintptr_t) &tgid;
1226 args[0].length = sizeof(tgid);
1227 args[0].fd = -1;
1228 args[0].reserved = 0;
1229 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0);
1230 fl->pd = 0;
1231
1232 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1233 sc, &args[0]);
1234}
1235
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001236static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp)
1237{
1238 struct fastrpc_invoke_args *args = NULL;
1239 struct fastrpc_invoke inv;
1240 u32 nscalars;
1241 int err;
1242
1243 if (copy_from_user(&inv, argp, sizeof(inv)))
1244 return -EFAULT;
1245
1246 /* nscalars is truncated here to max supported value */
1247 nscalars = REMOTE_SCALARS_LENGTH(inv.sc);
1248 if (nscalars) {
1249 args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL);
1250 if (!args)
1251 return -ENOMEM;
1252
1253 if (copy_from_user(args, (void __user *)(uintptr_t)inv.args,
1254 nscalars * sizeof(*args))) {
1255 kfree(args);
1256 return -EFAULT;
1257 }
1258 }
1259
1260 err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args);
1261 kfree(args);
1262
1263 return err;
1264}
1265
1266static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
1267 unsigned long arg)
1268{
1269 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1270 char __user *argp = (char __user *)arg;
1271 int err;
1272
1273 switch (cmd) {
1274 case FASTRPC_IOCTL_INVOKE:
1275 err = fastrpc_invoke(fl, argp);
1276 break;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001277 case FASTRPC_IOCTL_INIT_ATTACH:
1278 err = fastrpc_init_attach(fl);
1279 break;
1280 case FASTRPC_IOCTL_INIT_CREATE:
1281 err = fastrpc_init_create_process(fl, argp);
1282 break;
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +00001283 case FASTRPC_IOCTL_FREE_DMA_BUFF:
1284 err = fastrpc_dmabuf_free(fl, argp);
1285 break;
1286 case FASTRPC_IOCTL_ALLOC_DMA_BUFF:
1287 err = fastrpc_dmabuf_alloc(fl, argp);
1288 break;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001289 default:
1290 err = -ENOTTY;
1291 break;
1292 }
1293
1294 return err;
1295}
1296
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001297static const struct file_operations fastrpc_fops = {
1298 .open = fastrpc_device_open,
1299 .release = fastrpc_device_release,
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001300 .unlocked_ioctl = fastrpc_device_ioctl,
1301 .compat_ioctl = fastrpc_device_ioctl,
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001302};
1303
1304static int fastrpc_cb_probe(struct platform_device *pdev)
1305{
1306 struct fastrpc_channel_ctx *cctx;
1307 struct fastrpc_session_ctx *sess;
1308 struct device *dev = &pdev->dev;
1309 int i, sessions = 0;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001310 unsigned long flags;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001311
1312 cctx = dev_get_drvdata(dev->parent);
1313 if (!cctx)
1314 return -EINVAL;
1315
1316 of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions);
1317
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001318 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001319 sess = &cctx->session[cctx->sesscount];
1320 sess->used = false;
1321 sess->valid = true;
1322 sess->dev = dev;
1323 dev_set_drvdata(dev, sess);
1324
1325 if (of_property_read_u32(dev->of_node, "reg", &sess->sid))
1326 dev_info(dev, "FastRPC Session ID not specified in DT\n");
1327
1328 if (sessions > 0) {
1329 struct fastrpc_session_ctx *dup_sess;
1330
1331 for (i = 1; i < sessions; i++) {
1332 if (cctx->sesscount++ >= FASTRPC_MAX_SESSIONS)
1333 break;
1334 dup_sess = &cctx->session[cctx->sesscount];
1335 memcpy(dup_sess, sess, sizeof(*dup_sess));
1336 }
1337 }
1338 cctx->sesscount++;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001339 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001340 dma_set_mask(dev, DMA_BIT_MASK(32));
1341
1342 return 0;
1343}
1344
1345static int fastrpc_cb_remove(struct platform_device *pdev)
1346{
1347 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent);
1348 struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev);
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001349 unsigned long flags;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001350 int i;
1351
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001352 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001353 for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) {
1354 if (cctx->session[i].sid == sess->sid) {
1355 cctx->session[i].valid = false;
1356 cctx->sesscount--;
1357 }
1358 }
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001359 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001360
1361 return 0;
1362}
1363
1364static const struct of_device_id fastrpc_match_table[] = {
1365 { .compatible = "qcom,fastrpc-compute-cb", },
1366 {}
1367};
1368
1369static struct platform_driver fastrpc_cb_driver = {
1370 .probe = fastrpc_cb_probe,
1371 .remove = fastrpc_cb_remove,
1372 .driver = {
1373 .name = "qcom,fastrpc-cb",
1374 .of_match_table = fastrpc_match_table,
1375 .suppress_bind_attrs = true,
1376 },
1377};
1378
1379static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
1380{
1381 struct device *rdev = &rpdev->dev;
1382 struct fastrpc_channel_ctx *data;
1383 int i, err, domain_id = -1;
1384 const char *domain;
1385
1386 data = devm_kzalloc(rdev, sizeof(*data), GFP_KERNEL);
1387 if (!data)
1388 return -ENOMEM;
1389
1390 err = of_property_read_string(rdev->of_node, "label", &domain);
1391 if (err) {
1392 dev_info(rdev, "FastRPC Domain not specified in DT\n");
1393 return err;
1394 }
1395
1396 for (i = 0; i <= CDSP_DOMAIN_ID; i++) {
1397 if (!strcmp(domains[i], domain)) {
1398 domain_id = i;
1399 break;
1400 }
1401 }
1402
1403 if (domain_id < 0) {
1404 dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id);
1405 return -EINVAL;
1406 }
1407
1408 data->miscdev.minor = MISC_DYNAMIC_MINOR;
1409 data->miscdev.name = kasprintf(GFP_KERNEL, "fastrpc-%s",
1410 domains[domain_id]);
1411 data->miscdev.fops = &fastrpc_fops;
1412 err = misc_register(&data->miscdev);
1413 if (err)
1414 return err;
1415
1416 dev_set_drvdata(&rpdev->dev, data);
1417 dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
1418 INIT_LIST_HEAD(&data->users);
1419 spin_lock_init(&data->lock);
1420 idr_init(&data->ctx_idr);
1421 data->domain_id = domain_id;
1422 data->rpdev = rpdev;
1423
1424 return of_platform_populate(rdev->of_node, NULL, NULL, rdev);
1425}
1426
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001427static void fastrpc_notify_users(struct fastrpc_user *user)
1428{
1429 struct fastrpc_invoke_ctx *ctx;
1430
1431 spin_lock(&user->lock);
1432 list_for_each_entry(ctx, &user->pending, node)
1433 complete(&ctx->work);
1434 spin_unlock(&user->lock);
1435}
1436
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001437static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
1438{
1439 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001440 struct fastrpc_user *user;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001441 unsigned long flags;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001442
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001443 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001444 list_for_each_entry(user, &cctx->users, user)
1445 fastrpc_notify_users(user);
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001446 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001447
1448 misc_deregister(&cctx->miscdev);
1449 of_platform_depopulate(&rpdev->dev);
1450 kfree(cctx);
1451}
1452
1453static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
1454 int len, void *priv, u32 addr)
1455{
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001456 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
1457 struct fastrpc_invoke_rsp *rsp = data;
1458 struct fastrpc_invoke_ctx *ctx;
1459 unsigned long flags;
1460 unsigned long ctxid;
1461
1462 if (len < sizeof(*rsp))
1463 return -EINVAL;
1464
1465 ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4);
1466
1467 spin_lock_irqsave(&cctx->lock, flags);
1468 ctx = idr_find(&cctx->ctx_idr, ctxid);
1469 spin_unlock_irqrestore(&cctx->lock, flags);
1470
1471 if (!ctx) {
1472 dev_err(&rpdev->dev, "No context ID matches response\n");
1473 return -ENOENT;
1474 }
1475
1476 ctx->retval = rsp->retval;
1477 complete(&ctx->work);
Thierry Escande8e7389c2019-03-07 10:12:22 +00001478
1479 /*
1480 * The DMA buffer associated with the context cannot be freed in
1481 * interrupt context so schedule it through a worker thread to
1482 * avoid a kernel BUG.
1483 */
1484 schedule_work(&ctx->put_work);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001485
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001486 return 0;
1487}
1488
1489static const struct of_device_id fastrpc_rpmsg_of_match[] = {
1490 { .compatible = "qcom,fastrpc" },
1491 { },
1492};
1493MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match);
1494
1495static struct rpmsg_driver fastrpc_driver = {
1496 .probe = fastrpc_rpmsg_probe,
1497 .remove = fastrpc_rpmsg_remove,
1498 .callback = fastrpc_rpmsg_callback,
1499 .drv = {
1500 .name = "qcom,fastrpc",
1501 .of_match_table = fastrpc_rpmsg_of_match,
1502 },
1503};
1504
1505static int fastrpc_init(void)
1506{
1507 int ret;
1508
1509 ret = platform_driver_register(&fastrpc_cb_driver);
1510 if (ret < 0) {
1511 pr_err("fastrpc: failed to register cb driver\n");
1512 return ret;
1513 }
1514
1515 ret = register_rpmsg_driver(&fastrpc_driver);
1516 if (ret < 0) {
1517 pr_err("fastrpc: failed to register rpmsg driver\n");
1518 platform_driver_unregister(&fastrpc_cb_driver);
1519 return ret;
1520 }
1521
1522 return 0;
1523}
1524module_init(fastrpc_init);
1525
1526static void fastrpc_exit(void)
1527{
1528 platform_driver_unregister(&fastrpc_cb_driver);
1529 unregister_rpmsg_driver(&fastrpc_driver);
1530}
1531module_exit(fastrpc_exit);
1532
1533MODULE_LICENSE("GPL v2");