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