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