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