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