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