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