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