]> Git Repo - J-linux.git/blob - drivers/misc/fastrpc.c
Merge tag 'extcon-next-for-6.2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[J-linux.git] / drivers / misc / fastrpc.c
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
3 // Copyright (c) 2018, Linaro Limited
4
5 #include <linux/completion.h>
6 #include <linux/device.h>
7 #include <linux/dma-buf.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/idr.h>
10 #include <linux/list.h>
11 #include <linux/miscdevice.h>
12 #include <linux/module.h>
13 #include <linux/of_address.h>
14 #include <linux/of.h>
15 #include <linux/sort.h>
16 #include <linux/of_platform.h>
17 #include <linux/rpmsg.h>
18 #include <linux/scatterlist.h>
19 #include <linux/slab.h>
20 #include <linux/qcom_scm.h>
21 #include <uapi/misc/fastrpc.h>
22 #include <linux/of_reserved_mem.h>
23
24 #define ADSP_DOMAIN_ID (0)
25 #define MDSP_DOMAIN_ID (1)
26 #define SDSP_DOMAIN_ID (2)
27 #define CDSP_DOMAIN_ID (3)
28 #define FASTRPC_DEV_MAX         4 /* adsp, mdsp, slpi, cdsp*/
29 #define FASTRPC_MAX_SESSIONS    14
30 #define FASTRPC_MAX_VMIDS       16
31 #define FASTRPC_ALIGN           128
32 #define FASTRPC_MAX_FDLIST      16
33 #define FASTRPC_MAX_CRCLIST     64
34 #define FASTRPC_PHYS(p) ((p) & 0xffffffff)
35 #define FASTRPC_CTX_MAX (256)
36 #define FASTRPC_INIT_HANDLE     1
37 #define FASTRPC_DSP_UTILITIES_HANDLE    2
38 #define FASTRPC_CTXID_MASK (0xFF0)
39 #define INIT_FILELEN_MAX (2 * 1024 * 1024)
40 #define INIT_FILE_NAMELEN_MAX (128)
41 #define FASTRPC_DEVICE_NAME     "fastrpc"
42
43 /* Add memory to static PD pool, protection thru XPU */
44 #define ADSP_MMAP_HEAP_ADDR  4
45 /* MAP static DMA buffer on DSP User PD */
46 #define ADSP_MMAP_DMA_BUFFER  6
47 /* Add memory to static PD pool protection thru hypervisor */
48 #define ADSP_MMAP_REMOTE_HEAP_ADDR  8
49 /* Add memory to userPD pool, for user heap */
50 #define ADSP_MMAP_ADD_PAGES 0x1000
51 /* Add memory to userPD pool, for LLC heap */
52 #define ADSP_MMAP_ADD_PAGES_LLC 0x3000,
53
54 #define DSP_UNSUPPORTED_API (0x80000414)
55 /* MAX NUMBER of DSP ATTRIBUTES SUPPORTED */
56 #define FASTRPC_MAX_DSP_ATTRIBUTES (256)
57 #define FASTRPC_MAX_DSP_ATTRIBUTES_LEN (sizeof(u32) * FASTRPC_MAX_DSP_ATTRIBUTES)
58
59 /* Retrives number of input buffers from the scalars parameter */
60 #define REMOTE_SCALARS_INBUFS(sc)       (((sc) >> 16) & 0x0ff)
61
62 /* Retrives number of output buffers from the scalars parameter */
63 #define REMOTE_SCALARS_OUTBUFS(sc)      (((sc) >> 8) & 0x0ff)
64
65 /* Retrives number of input handles from the scalars parameter */
66 #define REMOTE_SCALARS_INHANDLES(sc)    (((sc) >> 4) & 0x0f)
67
68 /* Retrives number of output handles from the scalars parameter */
69 #define REMOTE_SCALARS_OUTHANDLES(sc)   ((sc) & 0x0f)
70
71 #define REMOTE_SCALARS_LENGTH(sc)       (REMOTE_SCALARS_INBUFS(sc) +   \
72                                          REMOTE_SCALARS_OUTBUFS(sc) +  \
73                                          REMOTE_SCALARS_INHANDLES(sc)+ \
74                                          REMOTE_SCALARS_OUTHANDLES(sc))
75 #define FASTRPC_BUILD_SCALARS(attr, method, in, out, oin, oout)  \
76                                 (((attr & 0x07) << 29) |                \
77                                 ((method & 0x1f) << 24) |       \
78                                 ((in & 0xff) << 16) |           \
79                                 ((out & 0xff) <<  8) |          \
80                                 ((oin & 0x0f) <<  4) |          \
81                                 (oout & 0x0f))
82
83 #define FASTRPC_SCALARS(method, in, out) \
84                 FASTRPC_BUILD_SCALARS(0, method, in, out, 0, 0)
85
86 #define FASTRPC_CREATE_PROCESS_NARGS    6
87 #define FASTRPC_CREATE_STATIC_PROCESS_NARGS     3
88 /* Remote Method id table */
89 #define FASTRPC_RMID_INIT_ATTACH        0
90 #define FASTRPC_RMID_INIT_RELEASE       1
91 #define FASTRPC_RMID_INIT_MMAP          4
92 #define FASTRPC_RMID_INIT_MUNMAP        5
93 #define FASTRPC_RMID_INIT_CREATE        6
94 #define FASTRPC_RMID_INIT_CREATE_ATTR   7
95 #define FASTRPC_RMID_INIT_CREATE_STATIC 8
96 #define FASTRPC_RMID_INIT_MEM_MAP      10
97 #define FASTRPC_RMID_INIT_MEM_UNMAP    11
98
99 /* Protection Domain(PD) ids */
100 #define ROOT_PD         (0)
101 #define USER_PD         (1)
102 #define SENSORS_PD      (2)
103
104 #define miscdev_to_fdevice(d) container_of(d, struct fastrpc_device, miscdev)
105
106 static const char *domains[FASTRPC_DEV_MAX] = { "adsp", "mdsp",
107                                                 "sdsp", "cdsp"};
108 struct fastrpc_phy_page {
109         u64 addr;               /* physical address */
110         u64 size;               /* size of contiguous region */
111 };
112
113 struct fastrpc_invoke_buf {
114         u32 num;                /* number of contiguous regions */
115         u32 pgidx;              /* index to start of contiguous region */
116 };
117
118 struct fastrpc_remote_dmahandle {
119         s32 fd;         /* dma handle fd */
120         u32 offset;     /* dma handle offset */
121         u32 len;        /* dma handle length */
122 };
123
124 struct fastrpc_remote_buf {
125         u64 pv;         /* buffer pointer */
126         u64 len;        /* length of buffer */
127 };
128
129 union fastrpc_remote_arg {
130         struct fastrpc_remote_buf buf;
131         struct fastrpc_remote_dmahandle dma;
132 };
133
134 struct fastrpc_mmap_rsp_msg {
135         u64 vaddr;
136 };
137
138 struct fastrpc_mmap_req_msg {
139         s32 pgid;
140         u32 flags;
141         u64 vaddr;
142         s32 num;
143 };
144
145 struct fastrpc_mem_map_req_msg {
146         s32 pgid;
147         s32 fd;
148         s32 offset;
149         u32 flags;
150         u64 vaddrin;
151         s32 num;
152         s32 data_len;
153 };
154
155 struct fastrpc_munmap_req_msg {
156         s32 pgid;
157         u64 vaddr;
158         u64 size;
159 };
160
161 struct fastrpc_mem_unmap_req_msg {
162         s32 pgid;
163         s32 fd;
164         u64 vaddrin;
165         u64 len;
166 };
167
168 struct fastrpc_msg {
169         int pid;                /* process group id */
170         int tid;                /* thread id */
171         u64 ctx;                /* invoke caller context */
172         u32 handle;     /* handle to invoke */
173         u32 sc;         /* scalars structure describing the data */
174         u64 addr;               /* physical address */
175         u64 size;               /* size of contiguous region */
176 };
177
178 struct fastrpc_invoke_rsp {
179         u64 ctx;                /* invoke caller context */
180         int retval;             /* invoke return value */
181 };
182
183 struct fastrpc_buf_overlap {
184         u64 start;
185         u64 end;
186         int raix;
187         u64 mstart;
188         u64 mend;
189         u64 offset;
190 };
191
192 struct fastrpc_buf {
193         struct fastrpc_user *fl;
194         struct dma_buf *dmabuf;
195         struct device *dev;
196         void *virt;
197         u64 phys;
198         u64 size;
199         /* Lock for dma buf attachments */
200         struct mutex lock;
201         struct list_head attachments;
202         /* mmap support */
203         struct list_head node; /* list of user requested mmaps */
204         uintptr_t raddr;
205 };
206
207 struct fastrpc_dma_buf_attachment {
208         struct device *dev;
209         struct sg_table sgt;
210         struct list_head node;
211 };
212
213 struct fastrpc_map {
214         struct list_head node;
215         struct fastrpc_user *fl;
216         int fd;
217         struct dma_buf *buf;
218         struct sg_table *table;
219         struct dma_buf_attachment *attach;
220         u64 phys;
221         u64 size;
222         void *va;
223         u64 len;
224         u64 raddr;
225         u32 attr;
226         struct kref refcount;
227 };
228
229 struct fastrpc_invoke_ctx {
230         int nscalars;
231         int nbufs;
232         int retval;
233         int pid;
234         int tgid;
235         u32 sc;
236         u32 *crc;
237         u64 ctxid;
238         u64 msg_sz;
239         struct kref refcount;
240         struct list_head node; /* list of ctxs */
241         struct completion work;
242         struct work_struct put_work;
243         struct fastrpc_msg msg;
244         struct fastrpc_user *fl;
245         union fastrpc_remote_arg *rpra;
246         struct fastrpc_map **maps;
247         struct fastrpc_buf *buf;
248         struct fastrpc_invoke_args *args;
249         struct fastrpc_buf_overlap *olaps;
250         struct fastrpc_channel_ctx *cctx;
251 };
252
253 struct fastrpc_session_ctx {
254         struct device *dev;
255         int sid;
256         bool used;
257         bool valid;
258 };
259
260 struct fastrpc_channel_ctx {
261         int domain_id;
262         int sesscount;
263         int vmcount;
264         u32 perms;
265         struct qcom_scm_vmperm vmperms[FASTRPC_MAX_VMIDS];
266         struct rpmsg_device *rpdev;
267         struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS];
268         spinlock_t lock;
269         struct idr ctx_idr;
270         struct list_head users;
271         struct kref refcount;
272         /* Flag if dsp attributes are cached */
273         bool valid_attributes;
274         u32 dsp_attributes[FASTRPC_MAX_DSP_ATTRIBUTES];
275         struct fastrpc_device *secure_fdevice;
276         struct fastrpc_device *fdevice;
277         struct fastrpc_buf *remote_heap;
278         struct list_head invoke_interrupted_mmaps;
279         bool secure;
280         bool unsigned_support;
281         u64 dma_mask;
282 };
283
284 struct fastrpc_device {
285         struct fastrpc_channel_ctx *cctx;
286         struct miscdevice miscdev;
287         bool secure;
288 };
289
290 struct fastrpc_user {
291         struct list_head user;
292         struct list_head maps;
293         struct list_head pending;
294         struct list_head mmaps;
295
296         struct fastrpc_channel_ctx *cctx;
297         struct fastrpc_session_ctx *sctx;
298         struct fastrpc_buf *init_mem;
299
300         int tgid;
301         int pd;
302         bool is_secure_dev;
303         /* Lock for lists */
304         spinlock_t lock;
305         /* lock for allocations */
306         struct mutex mutex;
307 };
308
309 static void fastrpc_free_map(struct kref *ref)
310 {
311         struct fastrpc_map *map;
312
313         map = container_of(ref, struct fastrpc_map, refcount);
314
315         if (map->table) {
316                 if (map->attr & FASTRPC_ATTR_SECUREMAP) {
317                         struct qcom_scm_vmperm perm;
318                         int err = 0;
319
320                         perm.vmid = QCOM_SCM_VMID_HLOS;
321                         perm.perm = QCOM_SCM_PERM_RWX;
322                         err = qcom_scm_assign_mem(map->phys, map->size,
323                                 &(map->fl->cctx->vmperms[0].vmid), &perm, 1);
324                         if (err) {
325                                 dev_err(map->fl->sctx->dev, "Failed to assign memory phys 0x%llx size 0x%llx err %d",
326                                                 map->phys, map->size, err);
327                                 return;
328                         }
329                 }
330                 dma_buf_unmap_attachment(map->attach, map->table,
331                                          DMA_BIDIRECTIONAL);
332                 dma_buf_detach(map->buf, map->attach);
333                 dma_buf_put(map->buf);
334         }
335
336         kfree(map);
337 }
338
339 static void fastrpc_map_put(struct fastrpc_map *map)
340 {
341         if (map)
342                 kref_put(&map->refcount, fastrpc_free_map);
343 }
344
345 static void fastrpc_map_get(struct fastrpc_map *map)
346 {
347         if (map)
348                 kref_get(&map->refcount);
349 }
350
351
352 static int fastrpc_map_lookup(struct fastrpc_user *fl, int fd,
353                             struct fastrpc_map **ppmap)
354 {
355         struct fastrpc_map *map = NULL;
356
357         mutex_lock(&fl->mutex);
358         list_for_each_entry(map, &fl->maps, node) {
359                 if (map->fd == fd) {
360                         *ppmap = map;
361                         mutex_unlock(&fl->mutex);
362                         return 0;
363                 }
364         }
365         mutex_unlock(&fl->mutex);
366
367         return -ENOENT;
368 }
369
370 static int fastrpc_map_find(struct fastrpc_user *fl, int fd,
371                             struct fastrpc_map **ppmap)
372 {
373         int ret = fastrpc_map_lookup(fl, fd, ppmap);
374
375         if (!ret)
376                 fastrpc_map_get(*ppmap);
377
378         return ret;
379 }
380
381 static void fastrpc_buf_free(struct fastrpc_buf *buf)
382 {
383         dma_free_coherent(buf->dev, buf->size, buf->virt,
384                           FASTRPC_PHYS(buf->phys));
385         kfree(buf);
386 }
387
388 static int __fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
389                              u64 size, struct fastrpc_buf **obuf)
390 {
391         struct fastrpc_buf *buf;
392
393         buf = kzalloc(sizeof(*buf), GFP_KERNEL);
394         if (!buf)
395                 return -ENOMEM;
396
397         INIT_LIST_HEAD(&buf->attachments);
398         INIT_LIST_HEAD(&buf->node);
399         mutex_init(&buf->lock);
400
401         buf->fl = fl;
402         buf->virt = NULL;
403         buf->phys = 0;
404         buf->size = size;
405         buf->dev = dev;
406         buf->raddr = 0;
407
408         buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys,
409                                        GFP_KERNEL);
410         if (!buf->virt) {
411                 mutex_destroy(&buf->lock);
412                 kfree(buf);
413                 return -ENOMEM;
414         }
415
416         *obuf = buf;
417
418         return 0;
419 }
420
421 static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
422                              u64 size, struct fastrpc_buf **obuf)
423 {
424         int ret;
425         struct fastrpc_buf *buf;
426
427         ret = __fastrpc_buf_alloc(fl, dev, size, obuf);
428         if (ret)
429                 return ret;
430
431         buf = *obuf;
432
433         if (fl->sctx && fl->sctx->sid)
434                 buf->phys += ((u64)fl->sctx->sid << 32);
435
436         return 0;
437 }
438
439 static int fastrpc_remote_heap_alloc(struct fastrpc_user *fl, struct device *dev,
440                                      u64 size, struct fastrpc_buf **obuf)
441 {
442         struct device *rdev = &fl->cctx->rpdev->dev;
443
444         return  __fastrpc_buf_alloc(fl, rdev, size, obuf);
445 }
446
447 static void fastrpc_channel_ctx_free(struct kref *ref)
448 {
449         struct fastrpc_channel_ctx *cctx;
450
451         cctx = container_of(ref, struct fastrpc_channel_ctx, refcount);
452
453         kfree(cctx);
454 }
455
456 static void fastrpc_channel_ctx_get(struct fastrpc_channel_ctx *cctx)
457 {
458         kref_get(&cctx->refcount);
459 }
460
461 static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx)
462 {
463         kref_put(&cctx->refcount, fastrpc_channel_ctx_free);
464 }
465
466 static void fastrpc_context_free(struct kref *ref)
467 {
468         struct fastrpc_invoke_ctx *ctx;
469         struct fastrpc_channel_ctx *cctx;
470         unsigned long flags;
471         int i;
472
473         ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
474         cctx = ctx->cctx;
475
476         for (i = 0; i < ctx->nbufs; i++)
477                 fastrpc_map_put(ctx->maps[i]);
478
479         if (ctx->buf)
480                 fastrpc_buf_free(ctx->buf);
481
482         spin_lock_irqsave(&cctx->lock, flags);
483         idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4);
484         spin_unlock_irqrestore(&cctx->lock, flags);
485
486         kfree(ctx->maps);
487         kfree(ctx->olaps);
488         kfree(ctx);
489
490         fastrpc_channel_ctx_put(cctx);
491 }
492
493 static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx)
494 {
495         kref_get(&ctx->refcount);
496 }
497
498 static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx)
499 {
500         kref_put(&ctx->refcount, fastrpc_context_free);
501 }
502
503 static void fastrpc_context_put_wq(struct work_struct *work)
504 {
505         struct fastrpc_invoke_ctx *ctx =
506                         container_of(work, struct fastrpc_invoke_ctx, put_work);
507
508         fastrpc_context_put(ctx);
509 }
510
511 #define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)
512 static int olaps_cmp(const void *a, const void *b)
513 {
514         struct fastrpc_buf_overlap *pa = (struct fastrpc_buf_overlap *)a;
515         struct fastrpc_buf_overlap *pb = (struct fastrpc_buf_overlap *)b;
516         /* sort with lowest starting buffer first */
517         int st = CMP(pa->start, pb->start);
518         /* sort with highest ending buffer first */
519         int ed = CMP(pb->end, pa->end);
520
521         return st == 0 ? ed : st;
522 }
523
524 static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
525 {
526         u64 max_end = 0;
527         int i;
528
529         for (i = 0; i < ctx->nbufs; ++i) {
530                 ctx->olaps[i].start = ctx->args[i].ptr;
531                 ctx->olaps[i].end = ctx->olaps[i].start + ctx->args[i].length;
532                 ctx->olaps[i].raix = i;
533         }
534
535         sort(ctx->olaps, ctx->nbufs, sizeof(*ctx->olaps), olaps_cmp, NULL);
536
537         for (i = 0; i < ctx->nbufs; ++i) {
538                 /* Falling inside previous range */
539                 if (ctx->olaps[i].start < max_end) {
540                         ctx->olaps[i].mstart = max_end;
541                         ctx->olaps[i].mend = ctx->olaps[i].end;
542                         ctx->olaps[i].offset = max_end - ctx->olaps[i].start;
543
544                         if (ctx->olaps[i].end > max_end) {
545                                 max_end = ctx->olaps[i].end;
546                         } else {
547                                 ctx->olaps[i].mend = 0;
548                                 ctx->olaps[i].mstart = 0;
549                         }
550
551                 } else  {
552                         ctx->olaps[i].mend = ctx->olaps[i].end;
553                         ctx->olaps[i].mstart = ctx->olaps[i].start;
554                         ctx->olaps[i].offset = 0;
555                         max_end = ctx->olaps[i].end;
556                 }
557         }
558 }
559
560 static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
561                         struct fastrpc_user *user, u32 kernel, u32 sc,
562                         struct fastrpc_invoke_args *args)
563 {
564         struct fastrpc_channel_ctx *cctx = user->cctx;
565         struct fastrpc_invoke_ctx *ctx = NULL;
566         unsigned long flags;
567         int ret;
568
569         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
570         if (!ctx)
571                 return ERR_PTR(-ENOMEM);
572
573         INIT_LIST_HEAD(&ctx->node);
574         ctx->fl = user;
575         ctx->nscalars = REMOTE_SCALARS_LENGTH(sc);
576         ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) +
577                      REMOTE_SCALARS_OUTBUFS(sc);
578
579         if (ctx->nscalars) {
580                 ctx->maps = kcalloc(ctx->nscalars,
581                                     sizeof(*ctx->maps), GFP_KERNEL);
582                 if (!ctx->maps) {
583                         kfree(ctx);
584                         return ERR_PTR(-ENOMEM);
585                 }
586                 ctx->olaps = kcalloc(ctx->nscalars,
587                                     sizeof(*ctx->olaps), GFP_KERNEL);
588                 if (!ctx->olaps) {
589                         kfree(ctx->maps);
590                         kfree(ctx);
591                         return ERR_PTR(-ENOMEM);
592                 }
593                 ctx->args = args;
594                 fastrpc_get_buff_overlaps(ctx);
595         }
596
597         /* Released in fastrpc_context_put() */
598         fastrpc_channel_ctx_get(cctx);
599
600         ctx->sc = sc;
601         ctx->retval = -1;
602         ctx->pid = current->pid;
603         ctx->tgid = user->tgid;
604         ctx->cctx = cctx;
605         init_completion(&ctx->work);
606         INIT_WORK(&ctx->put_work, fastrpc_context_put_wq);
607
608         spin_lock(&user->lock);
609         list_add_tail(&ctx->node, &user->pending);
610         spin_unlock(&user->lock);
611
612         spin_lock_irqsave(&cctx->lock, flags);
613         ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1,
614                                FASTRPC_CTX_MAX, GFP_ATOMIC);
615         if (ret < 0) {
616                 spin_unlock_irqrestore(&cctx->lock, flags);
617                 goto err_idr;
618         }
619         ctx->ctxid = ret << 4;
620         spin_unlock_irqrestore(&cctx->lock, flags);
621
622         kref_init(&ctx->refcount);
623
624         return ctx;
625 err_idr:
626         spin_lock(&user->lock);
627         list_del(&ctx->node);
628         spin_unlock(&user->lock);
629         fastrpc_channel_ctx_put(cctx);
630         kfree(ctx->maps);
631         kfree(ctx->olaps);
632         kfree(ctx);
633
634         return ERR_PTR(ret);
635 }
636
637 static struct sg_table *
638 fastrpc_map_dma_buf(struct dma_buf_attachment *attachment,
639                     enum dma_data_direction dir)
640 {
641         struct fastrpc_dma_buf_attachment *a = attachment->priv;
642         struct sg_table *table;
643         int ret;
644
645         table = &a->sgt;
646
647         ret = dma_map_sgtable(attachment->dev, table, dir, 0);
648         if (ret)
649                 table = ERR_PTR(ret);
650         return table;
651 }
652
653 static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach,
654                                   struct sg_table *table,
655                                   enum dma_data_direction dir)
656 {
657         dma_unmap_sgtable(attach->dev, table, dir, 0);
658 }
659
660 static void fastrpc_release(struct dma_buf *dmabuf)
661 {
662         struct fastrpc_buf *buffer = dmabuf->priv;
663
664         fastrpc_buf_free(buffer);
665 }
666
667 static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf,
668                                   struct dma_buf_attachment *attachment)
669 {
670         struct fastrpc_dma_buf_attachment *a;
671         struct fastrpc_buf *buffer = dmabuf->priv;
672         int ret;
673
674         a = kzalloc(sizeof(*a), GFP_KERNEL);
675         if (!a)
676                 return -ENOMEM;
677
678         ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt,
679                               FASTRPC_PHYS(buffer->phys), buffer->size);
680         if (ret < 0) {
681                 dev_err(buffer->dev, "failed to get scatterlist from DMA API\n");
682                 kfree(a);
683                 return -EINVAL;
684         }
685
686         a->dev = attachment->dev;
687         INIT_LIST_HEAD(&a->node);
688         attachment->priv = a;
689
690         mutex_lock(&buffer->lock);
691         list_add(&a->node, &buffer->attachments);
692         mutex_unlock(&buffer->lock);
693
694         return 0;
695 }
696
697 static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf,
698                                     struct dma_buf_attachment *attachment)
699 {
700         struct fastrpc_dma_buf_attachment *a = attachment->priv;
701         struct fastrpc_buf *buffer = dmabuf->priv;
702
703         mutex_lock(&buffer->lock);
704         list_del(&a->node);
705         mutex_unlock(&buffer->lock);
706         sg_free_table(&a->sgt);
707         kfree(a);
708 }
709
710 static int fastrpc_vmap(struct dma_buf *dmabuf, struct iosys_map *map)
711 {
712         struct fastrpc_buf *buf = dmabuf->priv;
713
714         iosys_map_set_vaddr(map, buf->virt);
715
716         return 0;
717 }
718
719 static int fastrpc_mmap(struct dma_buf *dmabuf,
720                         struct vm_area_struct *vma)
721 {
722         struct fastrpc_buf *buf = dmabuf->priv;
723         size_t size = vma->vm_end - vma->vm_start;
724
725         return dma_mmap_coherent(buf->dev, vma, buf->virt,
726                                  FASTRPC_PHYS(buf->phys), size);
727 }
728
729 static const struct dma_buf_ops fastrpc_dma_buf_ops = {
730         .attach = fastrpc_dma_buf_attach,
731         .detach = fastrpc_dma_buf_detatch,
732         .map_dma_buf = fastrpc_map_dma_buf,
733         .unmap_dma_buf = fastrpc_unmap_dma_buf,
734         .mmap = fastrpc_mmap,
735         .vmap = fastrpc_vmap,
736         .release = fastrpc_release,
737 };
738
739 static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
740                               u64 len, u32 attr, struct fastrpc_map **ppmap)
741 {
742         struct fastrpc_session_ctx *sess = fl->sctx;
743         struct fastrpc_map *map = NULL;
744         int err = 0;
745
746         if (!fastrpc_map_find(fl, fd, ppmap))
747                 return 0;
748
749         map = kzalloc(sizeof(*map), GFP_KERNEL);
750         if (!map)
751                 return -ENOMEM;
752
753         INIT_LIST_HEAD(&map->node);
754         kref_init(&map->refcount);
755
756         map->fl = fl;
757         map->fd = fd;
758         map->buf = dma_buf_get(fd);
759         if (IS_ERR(map->buf)) {
760                 err = PTR_ERR(map->buf);
761                 goto get_err;
762         }
763
764         map->attach = dma_buf_attach(map->buf, sess->dev);
765         if (IS_ERR(map->attach)) {
766                 dev_err(sess->dev, "Failed to attach dmabuf\n");
767                 err = PTR_ERR(map->attach);
768                 goto attach_err;
769         }
770
771         map->table = dma_buf_map_attachment(map->attach, DMA_BIDIRECTIONAL);
772         if (IS_ERR(map->table)) {
773                 err = PTR_ERR(map->table);
774                 goto map_err;
775         }
776
777         map->phys = sg_dma_address(map->table->sgl);
778         map->phys += ((u64)fl->sctx->sid << 32);
779         map->size = len;
780         map->va = sg_virt(map->table->sgl);
781         map->len = len;
782
783         if (attr & FASTRPC_ATTR_SECUREMAP) {
784                 /*
785                  * If subsystem VMIDs are defined in DTSI, then do
786                  * hyp_assign from HLOS to those VM(s)
787                  */
788                 unsigned int perms = BIT(QCOM_SCM_VMID_HLOS);
789
790                 map->attr = attr;
791                 err = qcom_scm_assign_mem(map->phys, (u64)map->size, &perms,
792                                 fl->cctx->vmperms, fl->cctx->vmcount);
793                 if (err) {
794                         dev_err(sess->dev, "Failed to assign memory with phys 0x%llx size 0x%llx err %d",
795                                         map->phys, map->size, err);
796                         goto map_err;
797                 }
798         }
799         spin_lock(&fl->lock);
800         list_add_tail(&map->node, &fl->maps);
801         spin_unlock(&fl->lock);
802         *ppmap = map;
803
804         return 0;
805
806 map_err:
807         dma_buf_detach(map->buf, map->attach);
808 attach_err:
809         dma_buf_put(map->buf);
810 get_err:
811         fastrpc_map_put(map);
812
813         return err;
814 }
815
816 /*
817  * Fastrpc payload buffer with metadata looks like:
818  *
819  * >>>>>>  START of METADATA <<<<<<<<<
820  * +---------------------------------+
821  * |           Arguments             |
822  * | type:(union fastrpc_remote_arg)|
823  * |             (0 - N)             |
824  * +---------------------------------+
825  * |         Invoke Buffer list      |
826  * | type:(struct fastrpc_invoke_buf)|
827  * |           (0 - N)               |
828  * +---------------------------------+
829  * |         Page info list          |
830  * | type:(struct fastrpc_phy_page)  |
831  * |             (0 - N)             |
832  * +---------------------------------+
833  * |         Optional info           |
834  * |(can be specific to SoC/Firmware)|
835  * +---------------------------------+
836  * >>>>>>>>  END of METADATA <<<<<<<<<
837  * +---------------------------------+
838  * |         Inline ARGS             |
839  * |            (0-N)                |
840  * +---------------------------------+
841  */
842
843 static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx)
844 {
845         int size = 0;
846
847         size = (sizeof(struct fastrpc_remote_buf) +
848                 sizeof(struct fastrpc_invoke_buf) +
849                 sizeof(struct fastrpc_phy_page)) * ctx->nscalars +
850                 sizeof(u64) * FASTRPC_MAX_FDLIST +
851                 sizeof(u32) * FASTRPC_MAX_CRCLIST;
852
853         return size;
854 }
855
856 static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen)
857 {
858         u64 size = 0;
859         int oix;
860
861         size = ALIGN(metalen, FASTRPC_ALIGN);
862         for (oix = 0; oix < ctx->nbufs; oix++) {
863                 int i = ctx->olaps[oix].raix;
864
865                 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) {
866
867                         if (ctx->olaps[oix].offset == 0)
868                                 size = ALIGN(size, FASTRPC_ALIGN);
869
870                         size += (ctx->olaps[oix].mend - ctx->olaps[oix].mstart);
871                 }
872         }
873
874         return size;
875 }
876
877 static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
878 {
879         struct device *dev = ctx->fl->sctx->dev;
880         int i, err;
881
882         for (i = 0; i < ctx->nscalars; ++i) {
883
884                 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 ||
885                     ctx->args[i].length == 0)
886                         continue;
887
888                 err = fastrpc_map_create(ctx->fl, ctx->args[i].fd,
889                          ctx->args[i].length, ctx->args[i].attr, &ctx->maps[i]);
890                 if (err) {
891                         dev_err(dev, "Error Creating map %d\n", err);
892                         return -EINVAL;
893                 }
894
895         }
896         return 0;
897 }
898
899 static struct fastrpc_invoke_buf *fastrpc_invoke_buf_start(union fastrpc_remote_arg *pra, int len)
900 {
901         return (struct fastrpc_invoke_buf *)(&pra[len]);
902 }
903
904 static struct fastrpc_phy_page *fastrpc_phy_page_start(struct fastrpc_invoke_buf *buf, int len)
905 {
906         return (struct fastrpc_phy_page *)(&buf[len]);
907 }
908
909 static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
910 {
911         struct device *dev = ctx->fl->sctx->dev;
912         union fastrpc_remote_arg *rpra;
913         struct fastrpc_invoke_buf *list;
914         struct fastrpc_phy_page *pages;
915         int inbufs, i, oix, err = 0;
916         u64 len, rlen, pkt_size;
917         u64 pg_start, pg_end;
918         uintptr_t args;
919         int metalen;
920
921         inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
922         metalen = fastrpc_get_meta_size(ctx);
923         pkt_size = fastrpc_get_payload_size(ctx, metalen);
924
925         err = fastrpc_create_maps(ctx);
926         if (err)
927                 return err;
928
929         ctx->msg_sz = pkt_size;
930
931         err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf);
932         if (err)
933                 return err;
934
935         rpra = ctx->buf->virt;
936         list = fastrpc_invoke_buf_start(rpra, ctx->nscalars);
937         pages = fastrpc_phy_page_start(list, ctx->nscalars);
938         args = (uintptr_t)ctx->buf->virt + metalen;
939         rlen = pkt_size - metalen;
940         ctx->rpra = rpra;
941
942         for (oix = 0; oix < ctx->nbufs; ++oix) {
943                 int mlen;
944
945                 i = ctx->olaps[oix].raix;
946                 len = ctx->args[i].length;
947
948                 rpra[i].buf.pv = 0;
949                 rpra[i].buf.len = len;
950                 list[i].num = len ? 1 : 0;
951                 list[i].pgidx = i;
952
953                 if (!len)
954                         continue;
955
956                 if (ctx->maps[i]) {
957                         struct vm_area_struct *vma = NULL;
958
959                         rpra[i].buf.pv = (u64) ctx->args[i].ptr;
960                         pages[i].addr = ctx->maps[i]->phys;
961
962                         mmap_read_lock(current->mm);
963                         vma = find_vma(current->mm, ctx->args[i].ptr);
964                         if (vma)
965                                 pages[i].addr += ctx->args[i].ptr -
966                                                  vma->vm_start;
967                         mmap_read_unlock(current->mm);
968
969                         pg_start = (ctx->args[i].ptr & PAGE_MASK) >> PAGE_SHIFT;
970                         pg_end = ((ctx->args[i].ptr + len - 1) & PAGE_MASK) >>
971                                   PAGE_SHIFT;
972                         pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
973
974                 } else {
975
976                         if (ctx->olaps[oix].offset == 0) {
977                                 rlen -= ALIGN(args, FASTRPC_ALIGN) - args;
978                                 args = ALIGN(args, FASTRPC_ALIGN);
979                         }
980
981                         mlen = ctx->olaps[oix].mend - ctx->olaps[oix].mstart;
982
983                         if (rlen < mlen)
984                                 goto bail;
985
986                         rpra[i].buf.pv = args - ctx->olaps[oix].offset;
987                         pages[i].addr = ctx->buf->phys -
988                                         ctx->olaps[oix].offset +
989                                         (pkt_size - rlen);
990                         pages[i].addr = pages[i].addr & PAGE_MASK;
991
992                         pg_start = (args & PAGE_MASK) >> PAGE_SHIFT;
993                         pg_end = ((args + len - 1) & PAGE_MASK) >> PAGE_SHIFT;
994                         pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
995                         args = args + mlen;
996                         rlen -= mlen;
997                 }
998
999                 if (i < inbufs && !ctx->maps[i]) {
1000                         void *dst = (void *)(uintptr_t)rpra[i].buf.pv;
1001                         void *src = (void *)(uintptr_t)ctx->args[i].ptr;
1002
1003                         if (!kernel) {
1004                                 if (copy_from_user(dst, (void __user *)src,
1005                                                    len)) {
1006                                         err = -EFAULT;
1007                                         goto bail;
1008                                 }
1009                         } else {
1010                                 memcpy(dst, src, len);
1011                         }
1012                 }
1013         }
1014
1015         for (i = ctx->nbufs; i < ctx->nscalars; ++i) {
1016                 list[i].num = ctx->args[i].length ? 1 : 0;
1017                 list[i].pgidx = i;
1018                 if (ctx->maps[i]) {
1019                         pages[i].addr = ctx->maps[i]->phys;
1020                         pages[i].size = ctx->maps[i]->size;
1021                 }
1022                 rpra[i].dma.fd = ctx->args[i].fd;
1023                 rpra[i].dma.len = ctx->args[i].length;
1024                 rpra[i].dma.offset = (u64) ctx->args[i].ptr;
1025         }
1026
1027 bail:
1028         if (err)
1029                 dev_err(dev, "Error: get invoke args failed:%d\n", err);
1030
1031         return err;
1032 }
1033
1034 static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
1035                             u32 kernel)
1036 {
1037         union fastrpc_remote_arg *rpra = ctx->rpra;
1038         struct fastrpc_user *fl = ctx->fl;
1039         struct fastrpc_map *mmap = NULL;
1040         struct fastrpc_invoke_buf *list;
1041         struct fastrpc_phy_page *pages;
1042         u64 *fdlist;
1043         int i, inbufs, outbufs, handles;
1044
1045         inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
1046         outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc);
1047         handles = REMOTE_SCALARS_INHANDLES(ctx->sc) + REMOTE_SCALARS_OUTHANDLES(ctx->sc);
1048         list = fastrpc_invoke_buf_start(rpra, ctx->nscalars);
1049         pages = fastrpc_phy_page_start(list, ctx->nscalars);
1050         fdlist = (uint64_t *)(pages + inbufs + outbufs + handles);
1051
1052         for (i = inbufs; i < ctx->nbufs; ++i) {
1053                 if (!ctx->maps[i]) {
1054                         void *src = (void *)(uintptr_t)rpra[i].buf.pv;
1055                         void *dst = (void *)(uintptr_t)ctx->args[i].ptr;
1056                         u64 len = rpra[i].buf.len;
1057
1058                         if (!kernel) {
1059                                 if (copy_to_user((void __user *)dst, src, len))
1060                                         return -EFAULT;
1061                         } else {
1062                                 memcpy(dst, src, len);
1063                         }
1064                 }
1065         }
1066
1067         for (i = 0; i < FASTRPC_MAX_FDLIST; i++) {
1068                 if (!fdlist[i])
1069                         break;
1070                 if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap))
1071                         fastrpc_map_put(mmap);
1072         }
1073
1074         return 0;
1075 }
1076
1077 static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx,
1078                                struct fastrpc_invoke_ctx *ctx,
1079                                u32 kernel, uint32_t handle)
1080 {
1081         struct fastrpc_channel_ctx *cctx;
1082         struct fastrpc_user *fl = ctx->fl;
1083         struct fastrpc_msg *msg = &ctx->msg;
1084         int ret;
1085
1086         cctx = fl->cctx;
1087         msg->pid = fl->tgid;
1088         msg->tid = current->pid;
1089
1090         if (kernel)
1091                 msg->pid = 0;
1092
1093         msg->ctx = ctx->ctxid | fl->pd;
1094         msg->handle = handle;
1095         msg->sc = ctx->sc;
1096         msg->addr = ctx->buf ? ctx->buf->phys : 0;
1097         msg->size = roundup(ctx->msg_sz, PAGE_SIZE);
1098         fastrpc_context_get(ctx);
1099
1100         ret = rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg));
1101
1102         if (ret)
1103                 fastrpc_context_put(ctx);
1104
1105         return ret;
1106
1107 }
1108
1109 static int fastrpc_internal_invoke(struct fastrpc_user *fl,  u32 kernel,
1110                                    u32 handle, u32 sc,
1111                                    struct fastrpc_invoke_args *args)
1112 {
1113         struct fastrpc_invoke_ctx *ctx = NULL;
1114         struct fastrpc_buf *buf, *b;
1115
1116         int err = 0;
1117
1118         if (!fl->sctx)
1119                 return -EINVAL;
1120
1121         if (!fl->cctx->rpdev)
1122                 return -EPIPE;
1123
1124         if (handle == FASTRPC_INIT_HANDLE && !kernel) {
1125                 dev_warn_ratelimited(fl->sctx->dev, "user app trying to send a kernel RPC message (%d)\n",  handle);
1126                 return -EPERM;
1127         }
1128
1129         ctx = fastrpc_context_alloc(fl, kernel, sc, args);
1130         if (IS_ERR(ctx))
1131                 return PTR_ERR(ctx);
1132
1133         if (ctx->nscalars) {
1134                 err = fastrpc_get_args(kernel, ctx);
1135                 if (err)
1136                         goto bail;
1137         }
1138
1139         /* make sure that all CPU memory writes are seen by DSP */
1140         dma_wmb();
1141         /* Send invoke buffer to remote dsp */
1142         err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle);
1143         if (err)
1144                 goto bail;
1145
1146         if (kernel) {
1147                 if (!wait_for_completion_timeout(&ctx->work, 10 * HZ))
1148                         err = -ETIMEDOUT;
1149         } else {
1150                 err = wait_for_completion_interruptible(&ctx->work);
1151         }
1152
1153         if (err)
1154                 goto bail;
1155
1156         /* Check the response from remote dsp */
1157         err = ctx->retval;
1158         if (err)
1159                 goto bail;
1160
1161         if (ctx->nscalars) {
1162                 /* make sure that all memory writes by DSP are seen by CPU */
1163                 dma_rmb();
1164                 /* populate all the output buffers with results */
1165                 err = fastrpc_put_args(ctx, kernel);
1166                 if (err)
1167                         goto bail;
1168         }
1169
1170 bail:
1171         if (err != -ERESTARTSYS && err != -ETIMEDOUT) {
1172                 /* We are done with this compute context */
1173                 spin_lock(&fl->lock);
1174                 list_del(&ctx->node);
1175                 spin_unlock(&fl->lock);
1176                 fastrpc_context_put(ctx);
1177         }
1178
1179         if (err == -ERESTARTSYS) {
1180                 list_for_each_entry_safe(buf, b, &fl->mmaps, node) {
1181                         list_del(&buf->node);
1182                         list_add_tail(&buf->node, &fl->cctx->invoke_interrupted_mmaps);
1183                 }
1184         }
1185
1186         if (err)
1187                 dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err);
1188
1189         return err;
1190 }
1191
1192 static bool is_session_rejected(struct fastrpc_user *fl, bool unsigned_pd_request)
1193 {
1194         /* Check if the device node is non-secure and channel is secure*/
1195         if (!fl->is_secure_dev && fl->cctx->secure) {
1196                 /*
1197                  * Allow untrusted applications to offload only to Unsigned PD when
1198                  * channel is configured as secure and block untrusted apps on channel
1199                  * that does not support unsigned PD offload
1200                  */
1201                 if (!fl->cctx->unsigned_support || !unsigned_pd_request) {
1202                         dev_err(&fl->cctx->rpdev->dev, "Error: Untrusted application trying to offload to signed PD");
1203                         return true;
1204                 }
1205         }
1206
1207         return false;
1208 }
1209
1210 static int fastrpc_init_create_static_process(struct fastrpc_user *fl,
1211                                               char __user *argp)
1212 {
1213         struct fastrpc_init_create_static init;
1214         struct fastrpc_invoke_args *args;
1215         struct fastrpc_phy_page pages[1];
1216         char *name;
1217         int err;
1218         struct {
1219                 int pgid;
1220                 u32 namelen;
1221                 u32 pageslen;
1222         } inbuf;
1223         u32 sc;
1224
1225         args = kcalloc(FASTRPC_CREATE_STATIC_PROCESS_NARGS, sizeof(*args), GFP_KERNEL);
1226         if (!args)
1227                 return -ENOMEM;
1228
1229         if (copy_from_user(&init, argp, sizeof(init))) {
1230                 err = -EFAULT;
1231                 goto err;
1232         }
1233
1234         if (init.namelen > INIT_FILE_NAMELEN_MAX) {
1235                 err = -EINVAL;
1236                 goto err;
1237         }
1238
1239         name = kzalloc(init.namelen, GFP_KERNEL);
1240         if (!name) {
1241                 err = -ENOMEM;
1242                 goto err;
1243         }
1244
1245         if (copy_from_user(name, (void __user *)(uintptr_t)init.name, init.namelen)) {
1246                 err = -EFAULT;
1247                 goto err_name;
1248         }
1249
1250         if (!fl->cctx->remote_heap) {
1251                 err = fastrpc_remote_heap_alloc(fl, fl->sctx->dev, init.memlen,
1252                                                 &fl->cctx->remote_heap);
1253                 if (err)
1254                         goto err_name;
1255
1256                 /* Map if we have any heap VMIDs associated with this ADSP Static Process. */
1257                 if (fl->cctx->vmcount) {
1258                         unsigned int perms = BIT(QCOM_SCM_VMID_HLOS);
1259
1260                         err = qcom_scm_assign_mem(fl->cctx->remote_heap->phys,
1261                                                         (u64)fl->cctx->remote_heap->size, &perms,
1262                                                         fl->cctx->vmperms, fl->cctx->vmcount);
1263                         if (err) {
1264                                 dev_err(fl->sctx->dev, "Failed to assign memory with phys 0x%llx size 0x%llx err %d",
1265                                         fl->cctx->remote_heap->phys, fl->cctx->remote_heap->size, err);
1266                                 goto err_map;
1267                         }
1268                 }
1269         }
1270
1271         inbuf.pgid = fl->tgid;
1272         inbuf.namelen = init.namelen;
1273         inbuf.pageslen = 0;
1274         fl->pd = USER_PD;
1275
1276         args[0].ptr = (u64)(uintptr_t)&inbuf;
1277         args[0].length = sizeof(inbuf);
1278         args[0].fd = -1;
1279
1280         args[1].ptr = (u64)(uintptr_t)name;
1281         args[1].length = inbuf.namelen;
1282         args[1].fd = -1;
1283
1284         pages[0].addr = fl->cctx->remote_heap->phys;
1285         pages[0].size = fl->cctx->remote_heap->size;
1286
1287         args[2].ptr = (u64)(uintptr_t) pages;
1288         args[2].length = sizeof(*pages);
1289         args[2].fd = -1;
1290
1291         sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_STATIC, 3, 0);
1292
1293         err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1294                                       sc, args);
1295         if (err)
1296                 goto err_invoke;
1297
1298         kfree(args);
1299
1300         return 0;
1301 err_invoke:
1302         if (fl->cctx->vmcount) {
1303                 struct qcom_scm_vmperm perm;
1304
1305                 perm.vmid = QCOM_SCM_VMID_HLOS;
1306                 perm.perm = QCOM_SCM_PERM_RWX;
1307                 err = qcom_scm_assign_mem(fl->cctx->remote_heap->phys,
1308                                                 (u64)fl->cctx->remote_heap->size,
1309                                                 &(fl->cctx->vmperms[0].vmid), &perm, 1);
1310                 if (err)
1311                         dev_err(fl->sctx->dev, "Failed to assign memory phys 0x%llx size 0x%llx err %d",
1312                                 fl->cctx->remote_heap->phys, fl->cctx->remote_heap->size, err);
1313         }
1314 err_map:
1315         fastrpc_buf_free(fl->cctx->remote_heap);
1316 err_name:
1317         kfree(name);
1318 err:
1319         kfree(args);
1320
1321         return err;
1322 }
1323
1324 static int fastrpc_init_create_process(struct fastrpc_user *fl,
1325                                         char __user *argp)
1326 {
1327         struct fastrpc_init_create init;
1328         struct fastrpc_invoke_args *args;
1329         struct fastrpc_phy_page pages[1];
1330         struct fastrpc_map *map = NULL;
1331         struct fastrpc_buf *imem = NULL;
1332         int memlen;
1333         int err;
1334         struct {
1335                 int pgid;
1336                 u32 namelen;
1337                 u32 filelen;
1338                 u32 pageslen;
1339                 u32 attrs;
1340                 u32 siglen;
1341         } inbuf;
1342         u32 sc;
1343         bool unsigned_module = false;
1344
1345         args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL);
1346         if (!args)
1347                 return -ENOMEM;
1348
1349         if (copy_from_user(&init, argp, sizeof(init))) {
1350                 err = -EFAULT;
1351                 goto err;
1352         }
1353
1354         if (init.attrs & FASTRPC_MODE_UNSIGNED_MODULE)
1355                 unsigned_module = true;
1356
1357         if (is_session_rejected(fl, unsigned_module)) {
1358                 err = -ECONNREFUSED;
1359                 goto err;
1360         }
1361
1362         if (init.filelen > INIT_FILELEN_MAX) {
1363                 err = -EINVAL;
1364                 goto err;
1365         }
1366
1367         inbuf.pgid = fl->tgid;
1368         inbuf.namelen = strlen(current->comm) + 1;
1369         inbuf.filelen = init.filelen;
1370         inbuf.pageslen = 1;
1371         inbuf.attrs = init.attrs;
1372         inbuf.siglen = init.siglen;
1373         fl->pd = USER_PD;
1374
1375         if (init.filelen && init.filefd) {
1376                 err = fastrpc_map_create(fl, init.filefd, init.filelen, 0, &map);
1377                 if (err)
1378                         goto err;
1379         }
1380
1381         memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4),
1382                        1024 * 1024);
1383         err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen,
1384                                 &imem);
1385         if (err)
1386                 goto err_alloc;
1387
1388         fl->init_mem = imem;
1389         args[0].ptr = (u64)(uintptr_t)&inbuf;
1390         args[0].length = sizeof(inbuf);
1391         args[0].fd = -1;
1392
1393         args[1].ptr = (u64)(uintptr_t)current->comm;
1394         args[1].length = inbuf.namelen;
1395         args[1].fd = -1;
1396
1397         args[2].ptr = (u64) init.file;
1398         args[2].length = inbuf.filelen;
1399         args[2].fd = init.filefd;
1400
1401         pages[0].addr = imem->phys;
1402         pages[0].size = imem->size;
1403
1404         args[3].ptr = (u64)(uintptr_t) pages;
1405         args[3].length = 1 * sizeof(*pages);
1406         args[3].fd = -1;
1407
1408         args[4].ptr = (u64)(uintptr_t)&inbuf.attrs;
1409         args[4].length = sizeof(inbuf.attrs);
1410         args[4].fd = -1;
1411
1412         args[5].ptr = (u64)(uintptr_t) &inbuf.siglen;
1413         args[5].length = sizeof(inbuf.siglen);
1414         args[5].fd = -1;
1415
1416         sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0);
1417         if (init.attrs)
1418                 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0);
1419
1420         err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1421                                       sc, args);
1422         if (err)
1423                 goto err_invoke;
1424
1425         kfree(args);
1426
1427         return 0;
1428
1429 err_invoke:
1430         fl->init_mem = NULL;
1431         fastrpc_buf_free(imem);
1432 err_alloc:
1433         if (map) {
1434                 spin_lock(&fl->lock);
1435                 list_del(&map->node);
1436                 spin_unlock(&fl->lock);
1437                 fastrpc_map_put(map);
1438         }
1439 err:
1440         kfree(args);
1441
1442         return err;
1443 }
1444
1445 static struct fastrpc_session_ctx *fastrpc_session_alloc(
1446                                         struct fastrpc_channel_ctx *cctx)
1447 {
1448         struct fastrpc_session_ctx *session = NULL;
1449         unsigned long flags;
1450         int i;
1451
1452         spin_lock_irqsave(&cctx->lock, flags);
1453         for (i = 0; i < cctx->sesscount; i++) {
1454                 if (!cctx->session[i].used && cctx->session[i].valid) {
1455                         cctx->session[i].used = true;
1456                         session = &cctx->session[i];
1457                         break;
1458                 }
1459         }
1460         spin_unlock_irqrestore(&cctx->lock, flags);
1461
1462         return session;
1463 }
1464
1465 static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx,
1466                                  struct fastrpc_session_ctx *session)
1467 {
1468         unsigned long flags;
1469
1470         spin_lock_irqsave(&cctx->lock, flags);
1471         session->used = false;
1472         spin_unlock_irqrestore(&cctx->lock, flags);
1473 }
1474
1475 static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl)
1476 {
1477         struct fastrpc_invoke_args args[1];
1478         int tgid = 0;
1479         u32 sc;
1480
1481         tgid = fl->tgid;
1482         args[0].ptr = (u64)(uintptr_t) &tgid;
1483         args[0].length = sizeof(tgid);
1484         args[0].fd = -1;
1485         sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0);
1486
1487         return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1488                                        sc, &args[0]);
1489 }
1490
1491 static int fastrpc_device_release(struct inode *inode, struct file *file)
1492 {
1493         struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1494         struct fastrpc_channel_ctx *cctx = fl->cctx;
1495         struct fastrpc_invoke_ctx *ctx, *n;
1496         struct fastrpc_map *map, *m;
1497         struct fastrpc_buf *buf, *b;
1498         unsigned long flags;
1499
1500         fastrpc_release_current_dsp_process(fl);
1501
1502         spin_lock_irqsave(&cctx->lock, flags);
1503         list_del(&fl->user);
1504         spin_unlock_irqrestore(&cctx->lock, flags);
1505
1506         if (fl->init_mem)
1507                 fastrpc_buf_free(fl->init_mem);
1508
1509         list_for_each_entry_safe(ctx, n, &fl->pending, node) {
1510                 list_del(&ctx->node);
1511                 fastrpc_context_put(ctx);
1512         }
1513
1514         list_for_each_entry_safe(map, m, &fl->maps, node) {
1515                 list_del(&map->node);
1516                 fastrpc_map_put(map);
1517         }
1518
1519         list_for_each_entry_safe(buf, b, &fl->mmaps, node) {
1520                 list_del(&buf->node);
1521                 fastrpc_buf_free(buf);
1522         }
1523
1524         fastrpc_session_free(cctx, fl->sctx);
1525         fastrpc_channel_ctx_put(cctx);
1526
1527         mutex_destroy(&fl->mutex);
1528         kfree(fl);
1529         file->private_data = NULL;
1530
1531         return 0;
1532 }
1533
1534 static int fastrpc_device_open(struct inode *inode, struct file *filp)
1535 {
1536         struct fastrpc_channel_ctx *cctx;
1537         struct fastrpc_device *fdevice;
1538         struct fastrpc_user *fl = NULL;
1539         unsigned long flags;
1540
1541         fdevice = miscdev_to_fdevice(filp->private_data);
1542         cctx = fdevice->cctx;
1543
1544         fl = kzalloc(sizeof(*fl), GFP_KERNEL);
1545         if (!fl)
1546                 return -ENOMEM;
1547
1548         /* Released in fastrpc_device_release() */
1549         fastrpc_channel_ctx_get(cctx);
1550
1551         filp->private_data = fl;
1552         spin_lock_init(&fl->lock);
1553         mutex_init(&fl->mutex);
1554         INIT_LIST_HEAD(&fl->pending);
1555         INIT_LIST_HEAD(&fl->maps);
1556         INIT_LIST_HEAD(&fl->mmaps);
1557         INIT_LIST_HEAD(&fl->user);
1558         fl->tgid = current->tgid;
1559         fl->cctx = cctx;
1560         fl->is_secure_dev = fdevice->secure;
1561
1562         fl->sctx = fastrpc_session_alloc(cctx);
1563         if (!fl->sctx) {
1564                 dev_err(&cctx->rpdev->dev, "No session available\n");
1565                 mutex_destroy(&fl->mutex);
1566                 kfree(fl);
1567
1568                 return -EBUSY;
1569         }
1570
1571         spin_lock_irqsave(&cctx->lock, flags);
1572         list_add_tail(&fl->user, &cctx->users);
1573         spin_unlock_irqrestore(&cctx->lock, flags);
1574
1575         return 0;
1576 }
1577
1578 static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
1579 {
1580         struct fastrpc_alloc_dma_buf bp;
1581         DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
1582         struct fastrpc_buf *buf = NULL;
1583         int err;
1584
1585         if (copy_from_user(&bp, argp, sizeof(bp)))
1586                 return -EFAULT;
1587
1588         err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf);
1589         if (err)
1590                 return err;
1591         exp_info.ops = &fastrpc_dma_buf_ops;
1592         exp_info.size = bp.size;
1593         exp_info.flags = O_RDWR;
1594         exp_info.priv = buf;
1595         buf->dmabuf = dma_buf_export(&exp_info);
1596         if (IS_ERR(buf->dmabuf)) {
1597                 err = PTR_ERR(buf->dmabuf);
1598                 fastrpc_buf_free(buf);
1599                 return err;
1600         }
1601
1602         bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE);
1603         if (bp.fd < 0) {
1604                 dma_buf_put(buf->dmabuf);
1605                 return -EINVAL;
1606         }
1607
1608         if (copy_to_user(argp, &bp, sizeof(bp))) {
1609                 /*
1610                  * The usercopy failed, but we can't do much about it, as
1611                  * dma_buf_fd() already called fd_install() and made the
1612                  * file descriptor accessible for the current process. It
1613                  * might already be closed and dmabuf no longer valid when
1614                  * we reach this point. Therefore "leak" the fd and rely on
1615                  * the process exit path to do any required cleanup.
1616                  */
1617                 return -EFAULT;
1618         }
1619
1620         return 0;
1621 }
1622
1623 static int fastrpc_init_attach(struct fastrpc_user *fl, int pd)
1624 {
1625         struct fastrpc_invoke_args args[1];
1626         int tgid = fl->tgid;
1627         u32 sc;
1628
1629         args[0].ptr = (u64)(uintptr_t) &tgid;
1630         args[0].length = sizeof(tgid);
1631         args[0].fd = -1;
1632         sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0);
1633         fl->pd = pd;
1634
1635         return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1636                                        sc, &args[0]);
1637 }
1638
1639 static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp)
1640 {
1641         struct fastrpc_invoke_args *args = NULL;
1642         struct fastrpc_invoke inv;
1643         u32 nscalars;
1644         int err;
1645
1646         if (copy_from_user(&inv, argp, sizeof(inv)))
1647                 return -EFAULT;
1648
1649         /* nscalars is truncated here to max supported value */
1650         nscalars = REMOTE_SCALARS_LENGTH(inv.sc);
1651         if (nscalars) {
1652                 args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL);
1653                 if (!args)
1654                         return -ENOMEM;
1655
1656                 if (copy_from_user(args, (void __user *)(uintptr_t)inv.args,
1657                                    nscalars * sizeof(*args))) {
1658                         kfree(args);
1659                         return -EFAULT;
1660                 }
1661         }
1662
1663         err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args);
1664         kfree(args);
1665
1666         return err;
1667 }
1668
1669 static int fastrpc_get_info_from_dsp(struct fastrpc_user *fl, uint32_t *dsp_attr_buf,
1670                                      uint32_t dsp_attr_buf_len)
1671 {
1672         struct fastrpc_invoke_args args[2] = { 0 };
1673
1674         /* Capability filled in userspace */
1675         dsp_attr_buf[0] = 0;
1676
1677         args[0].ptr = (u64)(uintptr_t)&dsp_attr_buf_len;
1678         args[0].length = sizeof(dsp_attr_buf_len);
1679         args[0].fd = -1;
1680         args[1].ptr = (u64)(uintptr_t)&dsp_attr_buf[1];
1681         args[1].length = dsp_attr_buf_len;
1682         args[1].fd = -1;
1683         fl->pd = USER_PD;
1684
1685         return fastrpc_internal_invoke(fl, true, FASTRPC_DSP_UTILITIES_HANDLE,
1686                                        FASTRPC_SCALARS(0, 1, 1), args);
1687 }
1688
1689 static int fastrpc_get_info_from_kernel(struct fastrpc_ioctl_capability *cap,
1690                                         struct fastrpc_user *fl)
1691 {
1692         struct fastrpc_channel_ctx *cctx = fl->cctx;
1693         uint32_t attribute_id = cap->attribute_id;
1694         uint32_t *dsp_attributes;
1695         unsigned long flags;
1696         uint32_t domain = cap->domain;
1697         int err;
1698
1699         spin_lock_irqsave(&cctx->lock, flags);
1700         /* check if we already have queried dsp for attributes */
1701         if (cctx->valid_attributes) {
1702                 spin_unlock_irqrestore(&cctx->lock, flags);
1703                 goto done;
1704         }
1705         spin_unlock_irqrestore(&cctx->lock, flags);
1706
1707         dsp_attributes = kzalloc(FASTRPC_MAX_DSP_ATTRIBUTES_LEN, GFP_KERNEL);
1708         if (!dsp_attributes)
1709                 return -ENOMEM;
1710
1711         err = fastrpc_get_info_from_dsp(fl, dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES_LEN);
1712         if (err == DSP_UNSUPPORTED_API) {
1713                 dev_info(&cctx->rpdev->dev,
1714                          "Warning: DSP capabilities not supported on domain: %d\n", domain);
1715                 kfree(dsp_attributes);
1716                 return -EOPNOTSUPP;
1717         } else if (err) {
1718                 dev_err(&cctx->rpdev->dev, "Error: dsp information is incorrect err: %d\n", err);
1719                 kfree(dsp_attributes);
1720                 return err;
1721         }
1722
1723         spin_lock_irqsave(&cctx->lock, flags);
1724         memcpy(cctx->dsp_attributes, dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES_LEN);
1725         cctx->valid_attributes = true;
1726         spin_unlock_irqrestore(&cctx->lock, flags);
1727         kfree(dsp_attributes);
1728 done:
1729         cap->capability = cctx->dsp_attributes[attribute_id];
1730         return 0;
1731 }
1732
1733 static int fastrpc_get_dsp_info(struct fastrpc_user *fl, char __user *argp)
1734 {
1735         struct fastrpc_ioctl_capability cap = {0};
1736         int err = 0;
1737
1738         if (copy_from_user(&cap, argp, sizeof(cap)))
1739                 return  -EFAULT;
1740
1741         cap.capability = 0;
1742         if (cap.domain >= FASTRPC_DEV_MAX) {
1743                 dev_err(&fl->cctx->rpdev->dev, "Error: Invalid domain id:%d, err:%d\n",
1744                         cap.domain, err);
1745                 return -ECHRNG;
1746         }
1747
1748         /* Fastrpc Capablities does not support modem domain */
1749         if (cap.domain == MDSP_DOMAIN_ID) {
1750                 dev_err(&fl->cctx->rpdev->dev, "Error: modem not supported %d\n", err);
1751                 return -ECHRNG;
1752         }
1753
1754         if (cap.attribute_id >= FASTRPC_MAX_DSP_ATTRIBUTES) {
1755                 dev_err(&fl->cctx->rpdev->dev, "Error: invalid attribute: %d, err: %d\n",
1756                         cap.attribute_id, err);
1757                 return -EOVERFLOW;
1758         }
1759
1760         err = fastrpc_get_info_from_kernel(&cap, fl);
1761         if (err)
1762                 return err;
1763
1764         if (copy_to_user(argp, &cap.capability, sizeof(cap.capability)))
1765                 return -EFAULT;
1766
1767         return 0;
1768 }
1769
1770 static int fastrpc_req_munmap_impl(struct fastrpc_user *fl, struct fastrpc_buf *buf)
1771 {
1772         struct fastrpc_invoke_args args[1] = { [0] = { 0 } };
1773         struct fastrpc_munmap_req_msg req_msg;
1774         struct device *dev = fl->sctx->dev;
1775         int err;
1776         u32 sc;
1777
1778         req_msg.pgid = fl->tgid;
1779         req_msg.size = buf->size;
1780         req_msg.vaddr = buf->raddr;
1781
1782         args[0].ptr = (u64) (uintptr_t) &req_msg;
1783         args[0].length = sizeof(req_msg);
1784
1785         sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MUNMAP, 1, 0);
1786         err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc,
1787                                       &args[0]);
1788         if (!err) {
1789                 dev_dbg(dev, "unmmap\tpt 0x%09lx OK\n", buf->raddr);
1790                 spin_lock(&fl->lock);
1791                 list_del(&buf->node);
1792                 spin_unlock(&fl->lock);
1793                 fastrpc_buf_free(buf);
1794         } else {
1795                 dev_err(dev, "unmmap\tpt 0x%09lx ERROR\n", buf->raddr);
1796         }
1797
1798         return err;
1799 }
1800
1801 static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)
1802 {
1803         struct fastrpc_buf *buf = NULL, *iter, *b;
1804         struct fastrpc_req_munmap req;
1805         struct device *dev = fl->sctx->dev;
1806
1807         if (copy_from_user(&req, argp, sizeof(req)))
1808                 return -EFAULT;
1809
1810         spin_lock(&fl->lock);
1811         list_for_each_entry_safe(iter, b, &fl->mmaps, node) {
1812                 if ((iter->raddr == req.vaddrout) && (iter->size == req.size)) {
1813                         buf = iter;
1814                         break;
1815                 }
1816         }
1817         spin_unlock(&fl->lock);
1818
1819         if (!buf) {
1820                 dev_err(dev, "mmap\t\tpt 0x%09llx [len 0x%08llx] not in list\n",
1821                         req.vaddrout, req.size);
1822                 return -EINVAL;
1823         }
1824
1825         return fastrpc_req_munmap_impl(fl, buf);
1826 }
1827
1828 static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp)
1829 {
1830         struct fastrpc_invoke_args args[3] = { [0 ... 2] = { 0 } };
1831         struct fastrpc_buf *buf = NULL;
1832         struct fastrpc_mmap_req_msg req_msg;
1833         struct fastrpc_mmap_rsp_msg rsp_msg;
1834         struct fastrpc_phy_page pages;
1835         struct fastrpc_req_mmap req;
1836         struct device *dev = fl->sctx->dev;
1837         int err;
1838         u32 sc;
1839
1840         if (copy_from_user(&req, argp, sizeof(req)))
1841                 return -EFAULT;
1842
1843         if (req.flags != ADSP_MMAP_ADD_PAGES && req.flags != ADSP_MMAP_REMOTE_HEAP_ADDR) {
1844                 dev_err(dev, "flag not supported 0x%x\n", req.flags);
1845
1846                 return -EINVAL;
1847         }
1848
1849         if (req.vaddrin) {
1850                 dev_err(dev, "adding user allocated pages is not supported\n");
1851                 return -EINVAL;
1852         }
1853
1854         err = fastrpc_buf_alloc(fl, fl->sctx->dev, req.size, &buf);
1855         if (err) {
1856                 dev_err(dev, "failed to allocate buffer\n");
1857                 return err;
1858         }
1859
1860         req_msg.pgid = fl->tgid;
1861         req_msg.flags = req.flags;
1862         req_msg.vaddr = req.vaddrin;
1863         req_msg.num = sizeof(pages);
1864
1865         args[0].ptr = (u64) (uintptr_t) &req_msg;
1866         args[0].length = sizeof(req_msg);
1867
1868         pages.addr = buf->phys;
1869         pages.size = buf->size;
1870
1871         args[1].ptr = (u64) (uintptr_t) &pages;
1872         args[1].length = sizeof(pages);
1873
1874         args[2].ptr = (u64) (uintptr_t) &rsp_msg;
1875         args[2].length = sizeof(rsp_msg);
1876
1877         sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MMAP, 2, 1);
1878         err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc,
1879                                       &args[0]);
1880         if (err) {
1881                 dev_err(dev, "mmap error (len 0x%08llx)\n", buf->size);
1882                 goto err_invoke;
1883         }
1884
1885         /* update the buffer to be able to deallocate the memory on the DSP */
1886         buf->raddr = (uintptr_t) rsp_msg.vaddr;
1887
1888         /* let the client know the address to use */
1889         req.vaddrout = rsp_msg.vaddr;
1890
1891         /* Add memory to static PD pool, protection thru hypervisor */
1892         if (req.flags != ADSP_MMAP_REMOTE_HEAP_ADDR && fl->cctx->vmcount) {
1893                 struct qcom_scm_vmperm perm;
1894                 int err = 0;
1895
1896                 perm.vmid = QCOM_SCM_VMID_HLOS;
1897                 perm.perm = QCOM_SCM_PERM_RWX;
1898                 err = qcom_scm_assign_mem(buf->phys, buf->size,
1899                         &(fl->cctx->vmperms[0].vmid), &perm, 1);
1900                 if (err) {
1901                         dev_err(fl->sctx->dev, "Failed to assign memory phys 0x%llx size 0x%llx err %d",
1902                                         buf->phys, buf->size, err);
1903                         goto err_assign;
1904                 }
1905         }
1906
1907         spin_lock(&fl->lock);
1908         list_add_tail(&buf->node, &fl->mmaps);
1909         spin_unlock(&fl->lock);
1910
1911         if (copy_to_user((void __user *)argp, &req, sizeof(req))) {
1912                 err = -EFAULT;
1913                 goto err_assign;
1914         }
1915
1916         dev_dbg(dev, "mmap\t\tpt 0x%09lx OK [len 0x%08llx]\n",
1917                 buf->raddr, buf->size);
1918
1919         return 0;
1920
1921 err_assign:
1922         fastrpc_req_munmap_impl(fl, buf);
1923 err_invoke:
1924         fastrpc_buf_free(buf);
1925
1926         return err;
1927 }
1928
1929 static int fastrpc_req_mem_unmap_impl(struct fastrpc_user *fl, struct fastrpc_mem_unmap *req)
1930 {
1931         struct fastrpc_invoke_args args[1] = { [0] = { 0 } };
1932         struct fastrpc_map *map = NULL, *iter, *m;
1933         struct fastrpc_mem_unmap_req_msg req_msg = { 0 };
1934         int err = 0;
1935         u32 sc;
1936         struct device *dev = fl->sctx->dev;
1937
1938         spin_lock(&fl->lock);
1939         list_for_each_entry_safe(iter, m, &fl->maps, node) {
1940                 if ((req->fd < 0 || iter->fd == req->fd) && (iter->raddr == req->vaddr)) {
1941                         map = iter;
1942                         break;
1943                 }
1944         }
1945
1946         spin_unlock(&fl->lock);
1947
1948         if (!map) {
1949                 dev_err(dev, "map not in list\n");
1950                 return -EINVAL;
1951         }
1952
1953         req_msg.pgid = fl->tgid;
1954         req_msg.len = map->len;
1955         req_msg.vaddrin = map->raddr;
1956         req_msg.fd = map->fd;
1957
1958         args[0].ptr = (u64) (uintptr_t) &req_msg;
1959         args[0].length = sizeof(req_msg);
1960
1961         sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MEM_UNMAP, 1, 0);
1962         err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc,
1963                                       &args[0]);
1964         fastrpc_map_put(map);
1965         if (err)
1966                 dev_err(dev, "unmmap\tpt fd = %d, 0x%09llx error\n",  map->fd, map->raddr);
1967
1968         return err;
1969 }
1970
1971 static int fastrpc_req_mem_unmap(struct fastrpc_user *fl, char __user *argp)
1972 {
1973         struct fastrpc_mem_unmap req;
1974
1975         if (copy_from_user(&req, argp, sizeof(req)))
1976                 return -EFAULT;
1977
1978         return fastrpc_req_mem_unmap_impl(fl, &req);
1979 }
1980
1981 static int fastrpc_req_mem_map(struct fastrpc_user *fl, char __user *argp)
1982 {
1983         struct fastrpc_invoke_args args[4] = { [0 ... 3] = { 0 } };
1984         struct fastrpc_mem_map_req_msg req_msg = { 0 };
1985         struct fastrpc_mmap_rsp_msg rsp_msg = { 0 };
1986         struct fastrpc_mem_unmap req_unmap = { 0 };
1987         struct fastrpc_phy_page pages = { 0 };
1988         struct fastrpc_mem_map req;
1989         struct device *dev = fl->sctx->dev;
1990         struct fastrpc_map *map = NULL;
1991         int err;
1992         u32 sc;
1993
1994         if (copy_from_user(&req, argp, sizeof(req)))
1995                 return -EFAULT;
1996
1997         /* create SMMU mapping */
1998         err = fastrpc_map_create(fl, req.fd, req.length, 0, &map);
1999         if (err) {
2000                 dev_err(dev, "failed to map buffer, fd = %d\n", req.fd);
2001                 return err;
2002         }
2003
2004         req_msg.pgid = fl->tgid;
2005         req_msg.fd = req.fd;
2006         req_msg.offset = req.offset;
2007         req_msg.vaddrin = req.vaddrin;
2008         map->va = (void *) (uintptr_t) req.vaddrin;
2009         req_msg.flags = req.flags;
2010         req_msg.num = sizeof(pages);
2011         req_msg.data_len = 0;
2012
2013         args[0].ptr = (u64) (uintptr_t) &req_msg;
2014         args[0].length = sizeof(req_msg);
2015
2016         pages.addr = map->phys;
2017         pages.size = map->size;
2018
2019         args[1].ptr = (u64) (uintptr_t) &pages;
2020         args[1].length = sizeof(pages);
2021
2022         args[2].ptr = (u64) (uintptr_t) &pages;
2023         args[2].length = 0;
2024
2025         args[3].ptr = (u64) (uintptr_t) &rsp_msg;
2026         args[3].length = sizeof(rsp_msg);
2027
2028         sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MEM_MAP, 3, 1);
2029         err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, &args[0]);
2030         if (err) {
2031                 dev_err(dev, "mem mmap error, fd %d, vaddr %llx, size %lld\n",
2032                         req.fd, req.vaddrin, map->size);
2033                 goto err_invoke;
2034         }
2035
2036         /* update the buffer to be able to deallocate the memory on the DSP */
2037         map->raddr = rsp_msg.vaddr;
2038
2039         /* let the client know the address to use */
2040         req.vaddrout = rsp_msg.vaddr;
2041
2042         if (copy_to_user((void __user *)argp, &req, sizeof(req))) {
2043                 /* unmap the memory and release the buffer */
2044                 req_unmap.vaddr = (uintptr_t) rsp_msg.vaddr;
2045                 req_unmap.length = map->size;
2046                 fastrpc_req_mem_unmap_impl(fl, &req_unmap);
2047                 return -EFAULT;
2048         }
2049
2050         return 0;
2051
2052 err_invoke:
2053         fastrpc_map_put(map);
2054
2055         return err;
2056 }
2057
2058 static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
2059                                  unsigned long arg)
2060 {
2061         struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
2062         char __user *argp = (char __user *)arg;
2063         int err;
2064
2065         switch (cmd) {
2066         case FASTRPC_IOCTL_INVOKE:
2067                 err = fastrpc_invoke(fl, argp);
2068                 break;
2069         case FASTRPC_IOCTL_INIT_ATTACH:
2070                 err = fastrpc_init_attach(fl, ROOT_PD);
2071                 break;
2072         case FASTRPC_IOCTL_INIT_ATTACH_SNS:
2073                 err = fastrpc_init_attach(fl, SENSORS_PD);
2074                 break;
2075         case FASTRPC_IOCTL_INIT_CREATE_STATIC:
2076                 err = fastrpc_init_create_static_process(fl, argp);
2077                 break;
2078         case FASTRPC_IOCTL_INIT_CREATE:
2079                 err = fastrpc_init_create_process(fl, argp);
2080                 break;
2081         case FASTRPC_IOCTL_ALLOC_DMA_BUFF:
2082                 err = fastrpc_dmabuf_alloc(fl, argp);
2083                 break;
2084         case FASTRPC_IOCTL_MMAP:
2085                 err = fastrpc_req_mmap(fl, argp);
2086                 break;
2087         case FASTRPC_IOCTL_MUNMAP:
2088                 err = fastrpc_req_munmap(fl, argp);
2089                 break;
2090         case FASTRPC_IOCTL_MEM_MAP:
2091                 err = fastrpc_req_mem_map(fl, argp);
2092                 break;
2093         case FASTRPC_IOCTL_MEM_UNMAP:
2094                 err = fastrpc_req_mem_unmap(fl, argp);
2095                 break;
2096         case FASTRPC_IOCTL_GET_DSP_INFO:
2097                 err = fastrpc_get_dsp_info(fl, argp);
2098                 break;
2099         default:
2100                 err = -ENOTTY;
2101                 break;
2102         }
2103
2104         return err;
2105 }
2106
2107 static const struct file_operations fastrpc_fops = {
2108         .open = fastrpc_device_open,
2109         .release = fastrpc_device_release,
2110         .unlocked_ioctl = fastrpc_device_ioctl,
2111         .compat_ioctl = fastrpc_device_ioctl,
2112 };
2113
2114 static int fastrpc_cb_probe(struct platform_device *pdev)
2115 {
2116         struct fastrpc_channel_ctx *cctx;
2117         struct fastrpc_session_ctx *sess;
2118         struct device *dev = &pdev->dev;
2119         int i, sessions = 0;
2120         unsigned long flags;
2121         int rc;
2122
2123         cctx = dev_get_drvdata(dev->parent);
2124         if (!cctx)
2125                 return -EINVAL;
2126
2127         of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions);
2128
2129         spin_lock_irqsave(&cctx->lock, flags);
2130         if (cctx->sesscount >= FASTRPC_MAX_SESSIONS) {
2131                 dev_err(&pdev->dev, "too many sessions\n");
2132                 spin_unlock_irqrestore(&cctx->lock, flags);
2133                 return -ENOSPC;
2134         }
2135         sess = &cctx->session[cctx->sesscount++];
2136         sess->used = false;
2137         sess->valid = true;
2138         sess->dev = dev;
2139         dev_set_drvdata(dev, sess);
2140
2141         if (of_property_read_u32(dev->of_node, "reg", &sess->sid))
2142                 dev_info(dev, "FastRPC Session ID not specified in DT\n");
2143
2144         if (sessions > 0) {
2145                 struct fastrpc_session_ctx *dup_sess;
2146
2147                 for (i = 1; i < sessions; i++) {
2148                         if (cctx->sesscount >= FASTRPC_MAX_SESSIONS)
2149                                 break;
2150                         dup_sess = &cctx->session[cctx->sesscount++];
2151                         memcpy(dup_sess, sess, sizeof(*dup_sess));
2152                 }
2153         }
2154         spin_unlock_irqrestore(&cctx->lock, flags);
2155         rc = dma_set_mask(dev, DMA_BIT_MASK(32));
2156         if (rc) {
2157                 dev_err(dev, "32-bit DMA enable failed\n");
2158                 return rc;
2159         }
2160
2161         return 0;
2162 }
2163
2164 static int fastrpc_cb_remove(struct platform_device *pdev)
2165 {
2166         struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent);
2167         struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev);
2168         unsigned long flags;
2169         int i;
2170
2171         spin_lock_irqsave(&cctx->lock, flags);
2172         for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) {
2173                 if (cctx->session[i].sid == sess->sid) {
2174                         cctx->session[i].valid = false;
2175                         cctx->sesscount--;
2176                 }
2177         }
2178         spin_unlock_irqrestore(&cctx->lock, flags);
2179
2180         return 0;
2181 }
2182
2183 static const struct of_device_id fastrpc_match_table[] = {
2184         { .compatible = "qcom,fastrpc-compute-cb", },
2185         {}
2186 };
2187
2188 static struct platform_driver fastrpc_cb_driver = {
2189         .probe = fastrpc_cb_probe,
2190         .remove = fastrpc_cb_remove,
2191         .driver = {
2192                 .name = "qcom,fastrpc-cb",
2193                 .of_match_table = fastrpc_match_table,
2194                 .suppress_bind_attrs = true,
2195         },
2196 };
2197
2198 static int fastrpc_device_register(struct device *dev, struct fastrpc_channel_ctx *cctx,
2199                                    bool is_secured, const char *domain)
2200 {
2201         struct fastrpc_device *fdev;
2202         int err;
2203
2204         fdev = devm_kzalloc(dev, sizeof(*fdev), GFP_KERNEL);
2205         if (!fdev)
2206                 return -ENOMEM;
2207
2208         fdev->secure = is_secured;
2209         fdev->cctx = cctx;
2210         fdev->miscdev.minor = MISC_DYNAMIC_MINOR;
2211         fdev->miscdev.fops = &fastrpc_fops;
2212         fdev->miscdev.name = devm_kasprintf(dev, GFP_KERNEL, "fastrpc-%s%s",
2213                                             domain, is_secured ? "-secure" : "");
2214         err = misc_register(&fdev->miscdev);
2215         if (!err) {
2216                 if (is_secured)
2217                         cctx->secure_fdevice = fdev;
2218                 else
2219                         cctx->fdevice = fdev;
2220         }
2221
2222         return err;
2223 }
2224
2225 static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
2226 {
2227         struct device *rdev = &rpdev->dev;
2228         struct fastrpc_channel_ctx *data;
2229         int i, err, domain_id = -1, vmcount;
2230         const char *domain;
2231         bool secure_dsp;
2232         unsigned int vmids[FASTRPC_MAX_VMIDS];
2233
2234         err = of_property_read_string(rdev->of_node, "label", &domain);
2235         if (err) {
2236                 dev_info(rdev, "FastRPC Domain not specified in DT\n");
2237                 return err;
2238         }
2239
2240         for (i = 0; i <= CDSP_DOMAIN_ID; i++) {
2241                 if (!strcmp(domains[i], domain)) {
2242                         domain_id = i;
2243                         break;
2244                 }
2245         }
2246
2247         if (domain_id < 0) {
2248                 dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id);
2249                 return -EINVAL;
2250         }
2251
2252         if (of_reserved_mem_device_init_by_idx(rdev, rdev->of_node, 0))
2253                 dev_info(rdev, "no reserved DMA memory for FASTRPC\n");
2254
2255         vmcount = of_property_read_variable_u32_array(rdev->of_node,
2256                                 "qcom,vmids", &vmids[0], 0, FASTRPC_MAX_VMIDS);
2257         if (vmcount < 0)
2258                 vmcount = 0;
2259         else if (!qcom_scm_is_available())
2260                 return -EPROBE_DEFER;
2261
2262         data = kzalloc(sizeof(*data), GFP_KERNEL);
2263         if (!data)
2264                 return -ENOMEM;
2265
2266         if (vmcount) {
2267                 data->vmcount = vmcount;
2268                 data->perms = BIT(QCOM_SCM_VMID_HLOS);
2269                 for (i = 0; i < data->vmcount; i++) {
2270                         data->vmperms[i].vmid = vmids[i];
2271                         data->vmperms[i].perm = QCOM_SCM_PERM_RWX;
2272                 }
2273         }
2274
2275         secure_dsp = !(of_property_read_bool(rdev->of_node, "qcom,non-secure-domain"));
2276         data->secure = secure_dsp;
2277
2278         switch (domain_id) {
2279         case ADSP_DOMAIN_ID:
2280         case MDSP_DOMAIN_ID:
2281         case SDSP_DOMAIN_ID:
2282                 /* Unsigned PD offloading is only supported on CDSP*/
2283                 data->unsigned_support = false;
2284                 err = fastrpc_device_register(rdev, data, secure_dsp, domains[domain_id]);
2285                 if (err)
2286                         goto fdev_error;
2287                 break;
2288         case CDSP_DOMAIN_ID:
2289                 data->unsigned_support = true;
2290                 /* Create both device nodes so that we can allow both Signed and Unsigned PD */
2291                 err = fastrpc_device_register(rdev, data, true, domains[domain_id]);
2292                 if (err)
2293                         goto fdev_error;
2294
2295                 err = fastrpc_device_register(rdev, data, false, domains[domain_id]);
2296                 if (err)
2297                         goto fdev_error;
2298                 break;
2299         default:
2300                 err = -EINVAL;
2301                 goto fdev_error;
2302         }
2303
2304         kref_init(&data->refcount);
2305
2306         dev_set_drvdata(&rpdev->dev, data);
2307         rdev->dma_mask = &data->dma_mask;
2308         dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
2309         INIT_LIST_HEAD(&data->users);
2310         INIT_LIST_HEAD(&data->invoke_interrupted_mmaps);
2311         spin_lock_init(&data->lock);
2312         idr_init(&data->ctx_idr);
2313         data->domain_id = domain_id;
2314         data->rpdev = rpdev;
2315
2316         return of_platform_populate(rdev->of_node, NULL, NULL, rdev);
2317 fdev_error:
2318         kfree(data);
2319         return err;
2320 }
2321
2322 static void fastrpc_notify_users(struct fastrpc_user *user)
2323 {
2324         struct fastrpc_invoke_ctx *ctx;
2325
2326         spin_lock(&user->lock);
2327         list_for_each_entry(ctx, &user->pending, node)
2328                 complete(&ctx->work);
2329         spin_unlock(&user->lock);
2330 }
2331
2332 static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
2333 {
2334         struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
2335         struct fastrpc_buf *buf, *b;
2336         struct fastrpc_user *user;
2337         unsigned long flags;
2338
2339         spin_lock_irqsave(&cctx->lock, flags);
2340         list_for_each_entry(user, &cctx->users, user)
2341                 fastrpc_notify_users(user);
2342         spin_unlock_irqrestore(&cctx->lock, flags);
2343
2344         if (cctx->fdevice)
2345                 misc_deregister(&cctx->fdevice->miscdev);
2346
2347         if (cctx->secure_fdevice)
2348                 misc_deregister(&cctx->secure_fdevice->miscdev);
2349
2350         list_for_each_entry_safe(buf, b, &cctx->invoke_interrupted_mmaps, node)
2351                 list_del(&buf->node);
2352
2353         if (cctx->remote_heap)
2354                 fastrpc_buf_free(cctx->remote_heap);
2355
2356         of_platform_depopulate(&rpdev->dev);
2357
2358         cctx->rpdev = NULL;
2359         fastrpc_channel_ctx_put(cctx);
2360 }
2361
2362 static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
2363                                   int len, void *priv, u32 addr)
2364 {
2365         struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
2366         struct fastrpc_invoke_rsp *rsp = data;
2367         struct fastrpc_invoke_ctx *ctx;
2368         unsigned long flags;
2369         unsigned long ctxid;
2370
2371         if (len < sizeof(*rsp))
2372                 return -EINVAL;
2373
2374         ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4);
2375
2376         spin_lock_irqsave(&cctx->lock, flags);
2377         ctx = idr_find(&cctx->ctx_idr, ctxid);
2378         spin_unlock_irqrestore(&cctx->lock, flags);
2379
2380         if (!ctx) {
2381                 dev_err(&rpdev->dev, "No context ID matches response\n");
2382                 return -ENOENT;
2383         }
2384
2385         ctx->retval = rsp->retval;
2386         complete(&ctx->work);
2387
2388         /*
2389          * The DMA buffer associated with the context cannot be freed in
2390          * interrupt context so schedule it through a worker thread to
2391          * avoid a kernel BUG.
2392          */
2393         schedule_work(&ctx->put_work);
2394
2395         return 0;
2396 }
2397
2398 static const struct of_device_id fastrpc_rpmsg_of_match[] = {
2399         { .compatible = "qcom,fastrpc" },
2400         { },
2401 };
2402 MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match);
2403
2404 static struct rpmsg_driver fastrpc_driver = {
2405         .probe = fastrpc_rpmsg_probe,
2406         .remove = fastrpc_rpmsg_remove,
2407         .callback = fastrpc_rpmsg_callback,
2408         .drv = {
2409                 .name = "qcom,fastrpc",
2410                 .of_match_table = fastrpc_rpmsg_of_match,
2411         },
2412 };
2413
2414 static int fastrpc_init(void)
2415 {
2416         int ret;
2417
2418         ret = platform_driver_register(&fastrpc_cb_driver);
2419         if (ret < 0) {
2420                 pr_err("fastrpc: failed to register cb driver\n");
2421                 return ret;
2422         }
2423
2424         ret = register_rpmsg_driver(&fastrpc_driver);
2425         if (ret < 0) {
2426                 pr_err("fastrpc: failed to register rpmsg driver\n");
2427                 platform_driver_unregister(&fastrpc_cb_driver);
2428                 return ret;
2429         }
2430
2431         return 0;
2432 }
2433 module_init(fastrpc_init);
2434
2435 static void fastrpc_exit(void)
2436 {
2437         platform_driver_unregister(&fastrpc_cb_driver);
2438         unregister_rpmsg_driver(&fastrpc_driver);
2439 }
2440 module_exit(fastrpc_exit);
2441
2442 MODULE_LICENSE("GPL v2");
2443 MODULE_IMPORT_NS(DMA_BUF);
This page took 0.198263 seconds and 4 git commands to generate.