blob: 07b45847d5cbac9c7ab1df74e618ed50e9724dc2 [file] [log] [blame]
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001/*
Sathish Ambleyae5ee542017-01-16 22:24:23 -08002 * Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14#include <linux/dma-buf.h>
15#include <linux/dma-mapping.h>
16#include <linux/slab.h>
17#include <linux/completion.h>
18#include <linux/pagemap.h>
19#include <linux/mm.h>
20#include <linux/fs.h>
21#include <linux/sched.h>
22#include <linux/module.h>
23#include <linux/cdev.h>
24#include <linux/list.h>
25#include <linux/hash.h>
26#include <linux/msm_ion.h>
27#include <soc/qcom/secure_buffer.h>
28#include <soc/qcom/glink.h>
29#include <soc/qcom/subsystem_notif.h>
30#include <soc/qcom/subsystem_restart.h>
31#include <linux/scatterlist.h>
32#include <linux/fs.h>
33#include <linux/uaccess.h>
34#include <linux/device.h>
35#include <linux/of.h>
36#include <linux/of_address.h>
37#include <linux/of_platform.h>
38#include <linux/dma-contiguous.h>
39#include <linux/cma.h>
40#include <linux/iommu.h>
41#include <linux/kref.h>
42#include <linux/sort.h>
43#include <linux/msm_dma_iommu_mapping.h>
44#include <asm/dma-iommu.h>
45#include "adsprpc_compat.h"
46#include "adsprpc_shared.h"
Sathish Ambley1ca68232017-01-19 10:32:55 -080047#include <linux/debugfs.h>
Sathish Ambley69e1ab02016-10-18 10:28:15 -070048
49#define TZ_PIL_PROTECT_MEM_SUBSYS_ID 0x0C
50#define TZ_PIL_CLEAR_PROTECT_MEM_SUBSYS_ID 0x0D
51#define TZ_PIL_AUTH_QDSP6_PROC 1
52#define FASTRPC_ENOSUCH 39
53#define VMID_SSC_Q6 5
54#define VMID_ADSP_Q6 6
Sathish Ambley1ca68232017-01-19 10:32:55 -080055#define DEBUGFS_SIZE 1024
Sathish Ambley69e1ab02016-10-18 10:28:15 -070056
57#define RPC_TIMEOUT (5 * HZ)
58#define BALIGN 128
59#define NUM_CHANNELS 4 /* adsp, mdsp, slpi, cdsp*/
60#define NUM_SESSIONS 9 /*8 compute, 1 cpz*/
Sathish Ambley58dc64d2016-11-29 17:11:53 -080061#define M_FDLIST 16
Sathish Ambley69e1ab02016-10-18 10:28:15 -070062
63#define IS_CACHE_ALIGNED(x) (((x) & ((L1_CACHE_BYTES)-1)) == 0)
64
65#define FASTRPC_LINK_STATE_DOWN (0x0)
66#define FASTRPC_LINK_STATE_UP (0x1)
67#define FASTRPC_LINK_DISCONNECTED (0x0)
68#define FASTRPC_LINK_CONNECTING (0x1)
69#define FASTRPC_LINK_CONNECTED (0x3)
70#define FASTRPC_LINK_DISCONNECTING (0x7)
71
Sathish Ambleya21b5b52017-01-11 16:11:01 -080072#define PERF_KEYS "count:flush:map:copy:glink:getargs:putargs:invalidate:invoke"
73#define FASTRPC_STATIC_HANDLE_LISTENER (3)
74#define FASTRPC_STATIC_HANDLE_MAX (20)
75
76#define PERF_END (void)0
77
78#define PERF(enb, cnt, ff) \
79 {\
80 struct timespec startT = {0};\
81 if (enb) {\
82 getnstimeofday(&startT);\
83 } \
84 ff ;\
85 if (enb) {\
86 cnt += getnstimediff(&startT);\
87 } \
88 }
89
Sathish Ambley69e1ab02016-10-18 10:28:15 -070090static int fastrpc_glink_open(int cid);
91static void fastrpc_glink_close(void *chan, int cid);
Sathish Ambley1ca68232017-01-19 10:32:55 -080092static struct dentry *debugfs_root;
93static struct dentry *debugfs_global_file;
Sathish Ambley69e1ab02016-10-18 10:28:15 -070094
95static inline uint64_t buf_page_start(uint64_t buf)
96{
97 uint64_t start = (uint64_t) buf & PAGE_MASK;
98 return start;
99}
100
101static inline uint64_t buf_page_offset(uint64_t buf)
102{
103 uint64_t offset = (uint64_t) buf & (PAGE_SIZE - 1);
104 return offset;
105}
106
107static inline int buf_num_pages(uint64_t buf, ssize_t len)
108{
109 uint64_t start = buf_page_start(buf) >> PAGE_SHIFT;
110 uint64_t end = (((uint64_t) buf + len - 1) & PAGE_MASK) >> PAGE_SHIFT;
111 int nPages = end - start + 1;
112 return nPages;
113}
114
115static inline uint64_t buf_page_size(uint32_t size)
116{
117 uint64_t sz = (size + (PAGE_SIZE - 1)) & PAGE_MASK;
118
119 return sz > PAGE_SIZE ? sz : PAGE_SIZE;
120}
121
122static inline void *uint64_to_ptr(uint64_t addr)
123{
124 void *ptr = (void *)((uintptr_t)addr);
125
126 return ptr;
127}
128
129static inline uint64_t ptr_to_uint64(void *ptr)
130{
131 uint64_t addr = (uint64_t)((uintptr_t)ptr);
132
133 return addr;
134}
135
136struct fastrpc_file;
137
138struct fastrpc_buf {
139 struct hlist_node hn;
140 struct fastrpc_file *fl;
141 void *virt;
142 uint64_t phys;
143 ssize_t size;
144};
145
146struct fastrpc_ctx_lst;
147
148struct overlap {
149 uintptr_t start;
150 uintptr_t end;
151 int raix;
152 uintptr_t mstart;
153 uintptr_t mend;
154 uintptr_t offset;
155};
156
157struct smq_invoke_ctx {
158 struct hlist_node hn;
159 struct completion work;
160 int retval;
161 int pid;
162 int tgid;
163 remote_arg_t *lpra;
164 remote_arg64_t *rpra;
165 int *fds;
166 unsigned int *attrs;
167 struct fastrpc_mmap **maps;
168 struct fastrpc_buf *buf;
169 ssize_t used;
170 struct fastrpc_file *fl;
171 uint32_t sc;
172 struct overlap *overs;
173 struct overlap **overps;
174 struct smq_msg msg;
175};
176
177struct fastrpc_ctx_lst {
178 struct hlist_head pending;
179 struct hlist_head interrupted;
180};
181
182struct fastrpc_smmu {
183 struct dma_iommu_mapping *mapping;
184 int cb;
185 int enabled;
186 int faults;
187 int secure;
188 int coherent;
189};
190
191struct fastrpc_session_ctx {
192 struct device *dev;
193 struct fastrpc_smmu smmu;
194 int used;
195};
196
197struct fastrpc_glink_info {
198 int link_state;
199 int port_state;
200 struct glink_open_config cfg;
201 struct glink_link_info link_info;
202 void *link_notify_handle;
203};
204
205struct fastrpc_channel_ctx {
206 char *name;
207 char *subsys;
208 void *chan;
209 struct device *dev;
210 struct fastrpc_session_ctx session[NUM_SESSIONS];
211 struct completion work;
212 struct notifier_block nb;
213 struct kref kref;
214 int sesscount;
215 int ssrcount;
216 void *handle;
217 int prevssrcount;
218 int vmid;
219 struct fastrpc_glink_info link;
220};
221
222struct fastrpc_apps {
223 struct fastrpc_channel_ctx *channel;
224 struct cdev cdev;
225 struct class *class;
226 struct mutex smd_mutex;
227 struct smq_phy_page range;
228 struct hlist_head maps;
229 dev_t dev_no;
230 int compat;
231 struct hlist_head drivers;
232 spinlock_t hlock;
233 struct ion_client *client;
234 struct device *dev;
235};
236
237struct fastrpc_mmap {
238 struct hlist_node hn;
239 struct fastrpc_file *fl;
240 struct fastrpc_apps *apps;
241 int fd;
242 uint32_t flags;
243 struct dma_buf *buf;
244 struct sg_table *table;
245 struct dma_buf_attachment *attach;
246 struct ion_handle *handle;
247 uint64_t phys;
248 ssize_t size;
249 uintptr_t va;
250 ssize_t len;
251 int refs;
252 uintptr_t raddr;
253 int uncached;
254 int secure;
255 uintptr_t attr;
256};
257
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800258struct fastrpc_perf {
259 int64_t count;
260 int64_t flush;
261 int64_t map;
262 int64_t copy;
263 int64_t link;
264 int64_t getargs;
265 int64_t putargs;
266 int64_t invargs;
267 int64_t invoke;
268};
269
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700270struct fastrpc_file {
271 struct hlist_node hn;
272 spinlock_t hlock;
273 struct hlist_head maps;
274 struct hlist_head bufs;
275 struct fastrpc_ctx_lst clst;
276 struct fastrpc_session_ctx *sctx;
277 struct fastrpc_session_ctx *secsctx;
278 uint32_t mode;
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800279 uint32_t profile;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700280 int tgid;
281 int cid;
282 int ssrcount;
283 int pd;
284 struct fastrpc_apps *apps;
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800285 struct fastrpc_perf perf;
Sathish Ambley1ca68232017-01-19 10:32:55 -0800286 struct dentry *debugfs_file;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700287};
288
289static struct fastrpc_apps gfa;
290
291static struct fastrpc_channel_ctx gcinfo[NUM_CHANNELS] = {
292 {
293 .name = "adsprpc-smd",
294 .subsys = "adsp",
295 .link.link_info.edge = "lpass",
296 .link.link_info.transport = "smem",
297 },
298 {
299 .name = "sdsprpc-smd",
Sathish Ambleya32a6392017-01-18 13:00:28 -0800300 .subsys = "slpi",
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700301 .link.link_info.edge = "dsps",
302 .link.link_info.transport = "smem",
303 },
304 {
305 .name = "mdsprpc-smd",
306 .subsys = "modem",
307 .link.link_info.edge = "mpss",
308 .link.link_info.transport = "smem",
309 },
310 {
311 .name = "cdsprpc-smd",
312 .subsys = "cdsp",
313 .link.link_info.edge = "cdsp",
314 .link.link_info.transport = "smem",
315 },
316};
317
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800318static inline int64_t getnstimediff(struct timespec *start)
319{
320 int64_t ns;
321 struct timespec ts, b;
322
323 getnstimeofday(&ts);
324 b = timespec_sub(ts, *start);
325 ns = timespec_to_ns(&b);
326 return ns;
327}
328
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700329static void fastrpc_buf_free(struct fastrpc_buf *buf, int cache)
330{
331 struct fastrpc_file *fl = buf == 0 ? 0 : buf->fl;
332 int vmid;
333
334 if (!fl)
335 return;
336 if (cache) {
337 spin_lock(&fl->hlock);
338 hlist_add_head(&buf->hn, &fl->bufs);
339 spin_unlock(&fl->hlock);
340 return;
341 }
342 if (!IS_ERR_OR_NULL(buf->virt)) {
343 int destVM[1] = {VMID_HLOS};
344 int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
345
346 if (fl->sctx->smmu.cb)
347 buf->phys &= ~((uint64_t)fl->sctx->smmu.cb << 32);
348 vmid = fl->apps->channel[fl->cid].vmid;
349 if (vmid) {
350 int srcVM[2] = {VMID_HLOS, vmid};
351
352 hyp_assign_phys(buf->phys, buf_page_size(buf->size),
353 srcVM, 2, destVM, destVMperm, 1);
354 }
355 dma_free_coherent(fl->sctx->dev, buf->size, buf->virt,
356 buf->phys);
357 }
358 kfree(buf);
359}
360
361static void fastrpc_buf_list_free(struct fastrpc_file *fl)
362{
363 struct fastrpc_buf *buf, *free;
364
365 do {
366 struct hlist_node *n;
367
368 free = 0;
369 spin_lock(&fl->hlock);
370 hlist_for_each_entry_safe(buf, n, &fl->bufs, hn) {
371 hlist_del_init(&buf->hn);
372 free = buf;
373 break;
374 }
375 spin_unlock(&fl->hlock);
376 if (free)
377 fastrpc_buf_free(free, 0);
378 } while (free);
379}
380
381static void fastrpc_mmap_add(struct fastrpc_mmap *map)
382{
383 struct fastrpc_file *fl = map->fl;
384
385 spin_lock(&fl->hlock);
386 hlist_add_head(&map->hn, &fl->maps);
387 spin_unlock(&fl->hlock);
388}
389
390static int fastrpc_mmap_find(struct fastrpc_file *fl, int fd, uintptr_t va,
Sathish Ambleyae5ee542017-01-16 22:24:23 -0800391 ssize_t len, int mflags, int refs, struct fastrpc_mmap **ppmap)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700392{
393 struct fastrpc_mmap *match = 0, *map;
394 struct hlist_node *n;
395
396 spin_lock(&fl->hlock);
397 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
398 if (va >= map->va &&
399 va + len <= map->va + map->len &&
400 map->fd == fd) {
Sathish Ambleyae5ee542017-01-16 22:24:23 -0800401 if (refs)
402 map->refs++;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700403 match = map;
404 break;
405 }
406 }
407 spin_unlock(&fl->hlock);
408 if (match) {
409 *ppmap = match;
410 return 0;
411 }
412 return -ENOTTY;
413}
414
415static int fastrpc_mmap_remove(struct fastrpc_file *fl, uintptr_t va,
416 ssize_t len, struct fastrpc_mmap **ppmap)
417{
418 struct fastrpc_mmap *match = 0, *map;
419 struct hlist_node *n;
420 struct fastrpc_apps *me = &gfa;
421
422 spin_lock(&me->hlock);
423 hlist_for_each_entry_safe(map, n, &me->maps, hn) {
424 if (map->raddr == va &&
425 map->raddr + map->len == va + len &&
426 map->refs == 1) {
427 match = map;
428 hlist_del_init(&map->hn);
429 break;
430 }
431 }
432 spin_unlock(&me->hlock);
433 if (match) {
434 *ppmap = match;
435 return 0;
436 }
437 spin_lock(&fl->hlock);
438 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
439 if (map->raddr == va &&
440 map->raddr + map->len == va + len &&
441 map->refs == 1) {
442 match = map;
443 hlist_del_init(&map->hn);
444 break;
445 }
446 }
447 spin_unlock(&fl->hlock);
448 if (match) {
449 *ppmap = match;
450 return 0;
451 }
452 return -ENOTTY;
453}
454
455static void fastrpc_mmap_free(struct fastrpc_mmap *map)
456{
457 struct fastrpc_file *fl;
458 int vmid;
459 struct fastrpc_session_ctx *sess;
460 int destVM[1] = {VMID_HLOS};
461 int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
462
463 if (!map)
464 return;
465 fl = map->fl;
466 spin_lock(&fl->hlock);
467 map->refs--;
468 if (!map->refs)
469 hlist_del_init(&map->hn);
470 spin_unlock(&fl->hlock);
471 if (map->refs > 0)
472 return;
473 if (map->secure)
474 sess = fl->secsctx;
475 else
476 sess = fl->sctx;
477
478 if (!IS_ERR_OR_NULL(map->handle))
479 ion_free(fl->apps->client, map->handle);
480 if (sess->smmu.enabled) {
481 if (map->size || map->phys)
Sathish Ambley58dc64d2016-11-29 17:11:53 -0800482 msm_dma_unmap_sg(sess->dev,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700483 map->table->sgl,
484 map->table->nents, DMA_BIDIRECTIONAL,
485 map->buf);
486 }
487 vmid = fl->apps->channel[fl->cid].vmid;
488 if (vmid && map->phys) {
489 int srcVM[2] = {VMID_HLOS, vmid};
490
491 hyp_assign_phys(map->phys, buf_page_size(map->size),
492 srcVM, 2, destVM, destVMperm, 1);
493 }
494
495 if (!IS_ERR_OR_NULL(map->table))
496 dma_buf_unmap_attachment(map->attach, map->table,
497 DMA_BIDIRECTIONAL);
498 if (!IS_ERR_OR_NULL(map->attach))
499 dma_buf_detach(map->buf, map->attach);
500 if (!IS_ERR_OR_NULL(map->buf))
501 dma_buf_put(map->buf);
502 kfree(map);
503}
504
505static int fastrpc_session_alloc(struct fastrpc_channel_ctx *chan, int secure,
506 struct fastrpc_session_ctx **session);
507
508static int fastrpc_mmap_create(struct fastrpc_file *fl, int fd,
509 unsigned int attr, uintptr_t va, ssize_t len, int mflags,
510 struct fastrpc_mmap **ppmap)
511{
512 struct fastrpc_session_ctx *sess;
513 struct fastrpc_apps *apps = fl->apps;
514 int cid = fl->cid;
515 struct fastrpc_channel_ctx *chan = &apps->channel[cid];
516 struct fastrpc_mmap *map = 0;
517 unsigned long attrs;
518 unsigned long flags;
519 int err = 0, vmid;
520
Sathish Ambleyae5ee542017-01-16 22:24:23 -0800521 if (!fastrpc_mmap_find(fl, fd, va, len, mflags, 1, ppmap))
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700522 return 0;
523 map = kzalloc(sizeof(*map), GFP_KERNEL);
524 VERIFY(err, !IS_ERR_OR_NULL(map));
525 if (err)
526 goto bail;
527 INIT_HLIST_NODE(&map->hn);
528 map->flags = mflags;
529 map->refs = 1;
530 map->fl = fl;
531 map->fd = fd;
532 map->attr = attr;
533 VERIFY(err, !IS_ERR_OR_NULL(map->handle =
534 ion_import_dma_buf_fd(fl->apps->client, fd)));
535 if (err)
536 goto bail;
537 VERIFY(err, !ion_handle_get_flags(fl->apps->client, map->handle,
538 &flags));
539 if (err)
540 goto bail;
541
542 map->uncached = !ION_IS_CACHED(flags);
543 if (map->attr & FASTRPC_ATTR_NOVA)
544 map->uncached = 1;
545
546 map->secure = flags & ION_FLAG_SECURE;
547 if (map->secure) {
548 if (!fl->secsctx)
549 err = fastrpc_session_alloc(chan, 1,
550 &fl->secsctx);
551 if (err)
552 goto bail;
553 }
554 if (map->secure)
555 sess = fl->secsctx;
556 else
557 sess = fl->sctx;
558
559 VERIFY(err, !IS_ERR_OR_NULL(map->buf = dma_buf_get(fd)));
560 if (err)
561 goto bail;
562 VERIFY(err, !IS_ERR_OR_NULL(map->attach =
563 dma_buf_attach(map->buf, sess->dev)));
564 if (err)
565 goto bail;
566 VERIFY(err, !IS_ERR_OR_NULL(map->table =
567 dma_buf_map_attachment(map->attach,
568 DMA_BIDIRECTIONAL)));
569 if (err)
570 goto bail;
571 if (sess->smmu.enabled) {
572 attrs = DMA_ATTR_EXEC_MAPPING;
573 VERIFY(err, map->table->nents ==
574 msm_dma_map_sg_attrs(sess->dev,
575 map->table->sgl, map->table->nents,
576 DMA_BIDIRECTIONAL, map->buf, attrs));
577 if (err)
578 goto bail;
579 } else {
580 VERIFY(err, map->table->nents == 1);
581 if (err)
582 goto bail;
583 }
584 map->phys = sg_dma_address(map->table->sgl);
585 if (sess->smmu.cb) {
586 map->phys += ((uint64_t)sess->smmu.cb << 32);
587 map->size = sg_dma_len(map->table->sgl);
588 } else {
589 map->size = buf_page_size(len);
590 }
591 vmid = fl->apps->channel[fl->cid].vmid;
592 if (vmid) {
593 int srcVM[1] = {VMID_HLOS};
594 int destVM[2] = {VMID_HLOS, vmid};
595 int destVMperm[2] = {PERM_READ | PERM_WRITE,
596 PERM_READ | PERM_WRITE | PERM_EXEC};
597
598 VERIFY(err, !hyp_assign_phys(map->phys,
599 buf_page_size(map->size),
600 srcVM, 1, destVM, destVMperm, 2));
601 if (err)
602 goto bail;
603 }
604 map->va = va;
605 map->len = len;
606
607 fastrpc_mmap_add(map);
608 *ppmap = map;
609
610bail:
611 if (err && map)
612 fastrpc_mmap_free(map);
613 return err;
614}
615
616static int fastrpc_buf_alloc(struct fastrpc_file *fl, ssize_t size,
617 struct fastrpc_buf **obuf)
618{
619 int err = 0, vmid;
620 struct fastrpc_buf *buf = 0, *fr = 0;
621 struct hlist_node *n;
622
623 VERIFY(err, size > 0);
624 if (err)
625 goto bail;
626
627 /* find the smallest buffer that fits in the cache */
628 spin_lock(&fl->hlock);
629 hlist_for_each_entry_safe(buf, n, &fl->bufs, hn) {
630 if (buf->size >= size && (!fr || fr->size > buf->size))
631 fr = buf;
632 }
633 if (fr)
634 hlist_del_init(&fr->hn);
635 spin_unlock(&fl->hlock);
636 if (fr) {
637 *obuf = fr;
638 return 0;
639 }
640 buf = 0;
641 VERIFY(err, buf = kzalloc(sizeof(*buf), GFP_KERNEL));
642 if (err)
643 goto bail;
644 INIT_HLIST_NODE(&buf->hn);
645 buf->fl = fl;
646 buf->virt = 0;
647 buf->phys = 0;
648 buf->size = size;
649 buf->virt = dma_alloc_coherent(fl->sctx->dev, buf->size,
650 (void *)&buf->phys, GFP_KERNEL);
651 if (IS_ERR_OR_NULL(buf->virt)) {
652 /* free cache and retry */
653 fastrpc_buf_list_free(fl);
654 buf->virt = dma_alloc_coherent(fl->sctx->dev, buf->size,
655 (void *)&buf->phys, GFP_KERNEL);
656 VERIFY(err, !IS_ERR_OR_NULL(buf->virt));
657 }
658 if (err)
659 goto bail;
660 if (fl->sctx->smmu.cb)
661 buf->phys += ((uint64_t)fl->sctx->smmu.cb << 32);
662 vmid = fl->apps->channel[fl->cid].vmid;
663 if (vmid) {
664 int srcVM[1] = {VMID_HLOS};
665 int destVM[2] = {VMID_HLOS, vmid};
666 int destVMperm[2] = {PERM_READ | PERM_WRITE,
667 PERM_READ | PERM_WRITE | PERM_EXEC};
668
669 VERIFY(err, !hyp_assign_phys(buf->phys, buf_page_size(size),
670 srcVM, 1, destVM, destVMperm, 2));
671 if (err)
672 goto bail;
673 }
674
675 *obuf = buf;
676 bail:
677 if (err && buf)
678 fastrpc_buf_free(buf, 0);
679 return err;
680}
681
682
683static int context_restore_interrupted(struct fastrpc_file *fl,
684 struct fastrpc_ioctl_invoke_attrs *inv,
685 struct smq_invoke_ctx **po)
686{
687 int err = 0;
688 struct smq_invoke_ctx *ctx = 0, *ictx = 0;
689 struct hlist_node *n;
690 struct fastrpc_ioctl_invoke *invoke = &inv->inv;
691
692 spin_lock(&fl->hlock);
693 hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) {
694 if (ictx->pid == current->pid) {
695 if (invoke->sc != ictx->sc || ictx->fl != fl)
696 err = -1;
697 else {
698 ctx = ictx;
699 hlist_del_init(&ctx->hn);
700 hlist_add_head(&ctx->hn, &fl->clst.pending);
701 }
702 break;
703 }
704 }
705 spin_unlock(&fl->hlock);
706 if (ctx)
707 *po = ctx;
708 return err;
709}
710
711#define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)
712static int overlap_ptr_cmp(const void *a, const void *b)
713{
714 struct overlap *pa = *((struct overlap **)a);
715 struct overlap *pb = *((struct overlap **)b);
716 /* sort with lowest starting buffer first */
717 int st = CMP(pa->start, pb->start);
718 /* sort with highest ending buffer first */
719 int ed = CMP(pb->end, pa->end);
720 return st == 0 ? ed : st;
721}
722
Sathish Ambley9466d672017-01-25 10:51:55 -0800723static int context_build_overlap(struct smq_invoke_ctx *ctx)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700724{
Sathish Ambley9466d672017-01-25 10:51:55 -0800725 int i, err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700726 remote_arg_t *lpra = ctx->lpra;
727 int inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
728 int outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc);
729 int nbufs = inbufs + outbufs;
730 struct overlap max;
731
732 for (i = 0; i < nbufs; ++i) {
733 ctx->overs[i].start = (uintptr_t)lpra[i].buf.pv;
734 ctx->overs[i].end = ctx->overs[i].start + lpra[i].buf.len;
Sathish Ambley9466d672017-01-25 10:51:55 -0800735 if (lpra[i].buf.len) {
736 VERIFY(err, ctx->overs[i].end > ctx->overs[i].start);
737 if (err)
738 goto bail;
739 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700740 ctx->overs[i].raix = i;
741 ctx->overps[i] = &ctx->overs[i];
742 }
743 sort(ctx->overps, nbufs, sizeof(*ctx->overps), overlap_ptr_cmp, 0);
744 max.start = 0;
745 max.end = 0;
746 for (i = 0; i < nbufs; ++i) {
747 if (ctx->overps[i]->start < max.end) {
748 ctx->overps[i]->mstart = max.end;
749 ctx->overps[i]->mend = ctx->overps[i]->end;
750 ctx->overps[i]->offset = max.end -
751 ctx->overps[i]->start;
752 if (ctx->overps[i]->end > max.end) {
753 max.end = ctx->overps[i]->end;
754 } else {
755 ctx->overps[i]->mend = 0;
756 ctx->overps[i]->mstart = 0;
757 }
758 } else {
759 ctx->overps[i]->mend = ctx->overps[i]->end;
760 ctx->overps[i]->mstart = ctx->overps[i]->start;
761 ctx->overps[i]->offset = 0;
762 max = *ctx->overps[i];
763 }
764 }
Sathish Ambley9466d672017-01-25 10:51:55 -0800765bail:
766 return err;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700767}
768
769#define K_COPY_FROM_USER(err, kernel, dst, src, size) \
770 do {\
771 if (!(kernel))\
772 VERIFY(err, 0 == copy_from_user((dst), (src),\
773 (size)));\
774 else\
775 memmove((dst), (src), (size));\
776 } while (0)
777
778#define K_COPY_TO_USER(err, kernel, dst, src, size) \
779 do {\
780 if (!(kernel))\
781 VERIFY(err, 0 == copy_to_user((dst), (src),\
782 (size)));\
783 else\
784 memmove((dst), (src), (size));\
785 } while (0)
786
787
788static void context_free(struct smq_invoke_ctx *ctx);
789
790static int context_alloc(struct fastrpc_file *fl, uint32_t kernel,
791 struct fastrpc_ioctl_invoke_attrs *invokefd,
792 struct smq_invoke_ctx **po)
793{
794 int err = 0, bufs, size = 0;
795 struct smq_invoke_ctx *ctx = 0;
796 struct fastrpc_ctx_lst *clst = &fl->clst;
797 struct fastrpc_ioctl_invoke *invoke = &invokefd->inv;
798
799 bufs = REMOTE_SCALARS_LENGTH(invoke->sc);
800 size = bufs * sizeof(*ctx->lpra) + bufs * sizeof(*ctx->maps) +
801 sizeof(*ctx->fds) * (bufs) +
802 sizeof(*ctx->attrs) * (bufs) +
803 sizeof(*ctx->overs) * (bufs) +
804 sizeof(*ctx->overps) * (bufs);
805
806 VERIFY(err, ctx = kzalloc(sizeof(*ctx) + size, GFP_KERNEL));
807 if (err)
808 goto bail;
809
810 INIT_HLIST_NODE(&ctx->hn);
811 hlist_add_fake(&ctx->hn);
812 ctx->fl = fl;
813 ctx->maps = (struct fastrpc_mmap **)(&ctx[1]);
814 ctx->lpra = (remote_arg_t *)(&ctx->maps[bufs]);
815 ctx->fds = (int *)(&ctx->lpra[bufs]);
816 ctx->attrs = (unsigned int *)(&ctx->fds[bufs]);
817 ctx->overs = (struct overlap *)(&ctx->attrs[bufs]);
818 ctx->overps = (struct overlap **)(&ctx->overs[bufs]);
819
820 K_COPY_FROM_USER(err, kernel, ctx->lpra, invoke->pra,
821 bufs * sizeof(*ctx->lpra));
822 if (err)
823 goto bail;
824
825 if (invokefd->fds) {
826 K_COPY_FROM_USER(err, kernel, ctx->fds, invokefd->fds,
827 bufs * sizeof(*ctx->fds));
828 if (err)
829 goto bail;
830 }
831 if (invokefd->attrs) {
832 K_COPY_FROM_USER(err, kernel, ctx->attrs, invokefd->attrs,
833 bufs * sizeof(*ctx->attrs));
834 if (err)
835 goto bail;
836 }
837
838 ctx->sc = invoke->sc;
Sathish Ambley9466d672017-01-25 10:51:55 -0800839 if (bufs) {
840 VERIFY(err, 0 == context_build_overlap(ctx));
841 if (err)
842 goto bail;
843 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700844 ctx->retval = -1;
845 ctx->pid = current->pid;
846 ctx->tgid = current->tgid;
847 init_completion(&ctx->work);
848
849 spin_lock(&fl->hlock);
850 hlist_add_head(&ctx->hn, &clst->pending);
851 spin_unlock(&fl->hlock);
852
853 *po = ctx;
854bail:
855 if (ctx && err)
856 context_free(ctx);
857 return err;
858}
859
860static void context_save_interrupted(struct smq_invoke_ctx *ctx)
861{
862 struct fastrpc_ctx_lst *clst = &ctx->fl->clst;
863
864 spin_lock(&ctx->fl->hlock);
865 hlist_del_init(&ctx->hn);
866 hlist_add_head(&ctx->hn, &clst->interrupted);
867 spin_unlock(&ctx->fl->hlock);
868 /* free the cache on power collapse */
869 fastrpc_buf_list_free(ctx->fl);
870}
871
872static void context_free(struct smq_invoke_ctx *ctx)
873{
874 int i;
875 int nbufs = REMOTE_SCALARS_INBUFS(ctx->sc) +
876 REMOTE_SCALARS_OUTBUFS(ctx->sc);
877 spin_lock(&ctx->fl->hlock);
878 hlist_del_init(&ctx->hn);
879 spin_unlock(&ctx->fl->hlock);
880 for (i = 0; i < nbufs; ++i)
881 fastrpc_mmap_free(ctx->maps[i]);
882 fastrpc_buf_free(ctx->buf, 1);
883 kfree(ctx);
884}
885
886static void context_notify_user(struct smq_invoke_ctx *ctx, int retval)
887{
888 ctx->retval = retval;
889 complete(&ctx->work);
890}
891
892
893static void fastrpc_notify_users(struct fastrpc_file *me)
894{
895 struct smq_invoke_ctx *ictx;
896 struct hlist_node *n;
897
898 spin_lock(&me->hlock);
899 hlist_for_each_entry_safe(ictx, n, &me->clst.pending, hn) {
900 complete(&ictx->work);
901 }
902 hlist_for_each_entry_safe(ictx, n, &me->clst.interrupted, hn) {
903 complete(&ictx->work);
904 }
905 spin_unlock(&me->hlock);
906
907}
908
909static void fastrpc_notify_drivers(struct fastrpc_apps *me, int cid)
910{
911 struct fastrpc_file *fl;
912 struct hlist_node *n;
913
914 spin_lock(&me->hlock);
915 hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
916 if (fl->cid == cid)
917 fastrpc_notify_users(fl);
918 }
919 spin_unlock(&me->hlock);
920
921}
922static void context_list_ctor(struct fastrpc_ctx_lst *me)
923{
924 INIT_HLIST_HEAD(&me->interrupted);
925 INIT_HLIST_HEAD(&me->pending);
926}
927
928static void fastrpc_context_list_dtor(struct fastrpc_file *fl)
929{
930 struct fastrpc_ctx_lst *clst = &fl->clst;
931 struct smq_invoke_ctx *ictx = 0, *ctxfree;
932 struct hlist_node *n;
933
934 do {
935 ctxfree = 0;
936 spin_lock(&fl->hlock);
937 hlist_for_each_entry_safe(ictx, n, &clst->interrupted, hn) {
938 hlist_del_init(&ictx->hn);
939 ctxfree = ictx;
940 break;
941 }
942 spin_unlock(&fl->hlock);
943 if (ctxfree)
944 context_free(ctxfree);
945 } while (ctxfree);
946 do {
947 ctxfree = 0;
948 spin_lock(&fl->hlock);
949 hlist_for_each_entry_safe(ictx, n, &clst->pending, hn) {
950 hlist_del_init(&ictx->hn);
951 ctxfree = ictx;
952 break;
953 }
954 spin_unlock(&fl->hlock);
955 if (ctxfree)
956 context_free(ctxfree);
957 } while (ctxfree);
958}
959
960static int fastrpc_file_free(struct fastrpc_file *fl);
961static void fastrpc_file_list_dtor(struct fastrpc_apps *me)
962{
963 struct fastrpc_file *fl, *free;
964 struct hlist_node *n;
965
966 do {
967 free = 0;
968 spin_lock(&me->hlock);
969 hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
970 hlist_del_init(&fl->hn);
971 free = fl;
972 break;
973 }
974 spin_unlock(&me->hlock);
975 if (free)
976 fastrpc_file_free(free);
977 } while (free);
978}
979
980static int get_args(uint32_t kernel, struct smq_invoke_ctx *ctx)
981{
982 remote_arg64_t *rpra;
983 remote_arg_t *lpra = ctx->lpra;
984 struct smq_invoke_buf *list;
985 struct smq_phy_page *pages, *ipage;
986 uint32_t sc = ctx->sc;
987 int inbufs = REMOTE_SCALARS_INBUFS(sc);
988 int outbufs = REMOTE_SCALARS_OUTBUFS(sc);
Sathish Ambley58dc64d2016-11-29 17:11:53 -0800989 int handles, bufs = inbufs + outbufs;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700990 uintptr_t args;
991 ssize_t rlen = 0, copylen = 0, metalen = 0;
Sathish Ambley58dc64d2016-11-29 17:11:53 -0800992 int i, oix;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700993 int err = 0;
994 int mflags = 0;
Sathish Ambley58dc64d2016-11-29 17:11:53 -0800995 uint64_t *fdlist;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700996
997 /* calculate size of the metadata */
998 rpra = 0;
999 list = smq_invoke_buf_start(rpra, sc);
1000 pages = smq_phy_page_start(sc, list);
1001 ipage = pages;
1002
1003 for (i = 0; i < bufs; ++i) {
1004 uintptr_t buf = (uintptr_t)lpra[i].buf.pv;
1005 ssize_t len = lpra[i].buf.len;
1006
1007 if (ctx->fds[i] && (ctx->fds[i] != -1))
1008 fastrpc_mmap_create(ctx->fl, ctx->fds[i],
1009 ctx->attrs[i], buf, len,
1010 mflags, &ctx->maps[i]);
1011 ipage += 1;
1012 }
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001013 handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc);
1014 for (i = bufs; i < bufs + handles; i++) {
1015 VERIFY(err, !fastrpc_mmap_create(ctx->fl, ctx->fds[i],
1016 FASTRPC_ATTR_NOVA, 0, 0, 0, &ctx->maps[i]));
1017 if (err)
1018 goto bail;
1019 ipage += 1;
1020 }
1021 metalen = copylen = (ssize_t)&ipage[0] + (sizeof(uint64_t) * M_FDLIST);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001022 /* calculate len requreed for copying */
1023 for (oix = 0; oix < inbufs + outbufs; ++oix) {
1024 int i = ctx->overps[oix]->raix;
1025 ssize_t len = lpra[i].buf.len;
1026
1027 if (!len)
1028 continue;
1029 if (ctx->maps[i])
1030 continue;
1031 if (ctx->overps[oix]->offset == 0)
1032 copylen = ALIGN(copylen, BALIGN);
1033 copylen += ctx->overps[oix]->mend - ctx->overps[oix]->mstart;
1034 }
1035 ctx->used = copylen;
1036
1037 /* allocate new buffer */
1038 if (copylen) {
1039 VERIFY(err, !fastrpc_buf_alloc(ctx->fl, copylen, &ctx->buf));
1040 if (err)
1041 goto bail;
1042 }
1043 /* copy metadata */
1044 rpra = ctx->buf->virt;
1045 ctx->rpra = rpra;
1046 list = smq_invoke_buf_start(rpra, sc);
1047 pages = smq_phy_page_start(sc, list);
1048 ipage = pages;
1049 args = (uintptr_t)ctx->buf->virt + metalen;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001050 for (i = 0; i < bufs + handles; ++i) {
1051 if (lpra[i].buf.len)
1052 list[i].num = 1;
1053 else
1054 list[i].num = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001055 list[i].pgidx = ipage - pages;
1056 ipage++;
1057 }
1058 /* map ion buffers */
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001059 PERF(ctx->fl->profile, ctx->fl->perf.map,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001060 for (i = 0; i < inbufs + outbufs; ++i) {
1061 struct fastrpc_mmap *map = ctx->maps[i];
1062 uint64_t buf = ptr_to_uint64(lpra[i].buf.pv);
1063 ssize_t len = lpra[i].buf.len;
1064
1065 rpra[i].buf.pv = 0;
1066 rpra[i].buf.len = len;
1067 if (!len)
1068 continue;
1069 if (map) {
1070 struct vm_area_struct *vma;
1071 uintptr_t offset;
1072 int num = buf_num_pages(buf, len);
1073 int idx = list[i].pgidx;
1074
1075 if (map->attr & FASTRPC_ATTR_NOVA) {
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001076 offset = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001077 } else {
1078 down_read(&current->mm->mmap_sem);
1079 VERIFY(err, NULL != (vma = find_vma(current->mm,
1080 map->va)));
1081 if (err) {
1082 up_read(&current->mm->mmap_sem);
1083 goto bail;
1084 }
1085 offset = buf_page_start(buf) - vma->vm_start;
1086 up_read(&current->mm->mmap_sem);
1087 VERIFY(err, offset < (uintptr_t)map->size);
1088 if (err)
1089 goto bail;
1090 }
1091 pages[idx].addr = map->phys + offset;
1092 pages[idx].size = num << PAGE_SHIFT;
1093 }
1094 rpra[i].buf.pv = buf;
1095 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001096 PERF_END);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001097 for (i = bufs; i < bufs + handles; ++i) {
1098 struct fastrpc_mmap *map = ctx->maps[i];
1099
1100 pages[i].addr = map->phys;
1101 pages[i].size = map->size;
1102 }
1103 fdlist = (uint64_t *)&pages[bufs + handles];
1104 for (i = 0; i < M_FDLIST; i++)
1105 fdlist[i] = 0;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001106
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001107 /* copy non ion buffers */
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001108 PERF(ctx->fl->profile, ctx->fl->perf.copy,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001109 rlen = copylen - metalen;
1110 for (oix = 0; oix < inbufs + outbufs; ++oix) {
1111 int i = ctx->overps[oix]->raix;
1112 struct fastrpc_mmap *map = ctx->maps[i];
1113 int mlen = ctx->overps[oix]->mend - ctx->overps[oix]->mstart;
1114 uint64_t buf;
1115 ssize_t len = lpra[i].buf.len;
1116
1117 if (!len)
1118 continue;
1119 if (map)
1120 continue;
1121 if (ctx->overps[oix]->offset == 0) {
1122 rlen -= ALIGN(args, BALIGN) - args;
1123 args = ALIGN(args, BALIGN);
1124 }
1125 VERIFY(err, rlen >= mlen);
1126 if (err)
1127 goto bail;
1128 rpra[i].buf.pv = (args - ctx->overps[oix]->offset);
1129 pages[list[i].pgidx].addr = ctx->buf->phys -
1130 ctx->overps[oix]->offset +
1131 (copylen - rlen);
1132 pages[list[i].pgidx].addr =
1133 buf_page_start(pages[list[i].pgidx].addr);
1134 buf = rpra[i].buf.pv;
1135 pages[list[i].pgidx].size = buf_num_pages(buf, len) * PAGE_SIZE;
1136 if (i < inbufs) {
1137 K_COPY_FROM_USER(err, kernel, uint64_to_ptr(buf),
1138 lpra[i].buf.pv, len);
1139 if (err)
1140 goto bail;
1141 }
1142 args = args + mlen;
1143 rlen -= mlen;
1144 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001145 PERF_END);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001146
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001147 PERF(ctx->fl->profile, ctx->fl->perf.flush,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001148 for (oix = 0; oix < inbufs + outbufs; ++oix) {
1149 int i = ctx->overps[oix]->raix;
1150 struct fastrpc_mmap *map = ctx->maps[i];
1151
1152 if (ctx->fl->sctx->smmu.coherent)
1153 continue;
1154 if (map && map->uncached)
1155 continue;
1156 if (rpra[i].buf.len && ctx->overps[oix]->mstart)
1157 dmac_flush_range(uint64_to_ptr(rpra[i].buf.pv),
1158 uint64_to_ptr(rpra[i].buf.pv + rpra[i].buf.len));
1159 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001160 PERF_END);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001161 for (i = bufs; i < bufs + handles; i++) {
1162 rpra[i].dma.fd = ctx->fds[i];
1163 rpra[i].dma.len = (uint32_t)lpra[i].buf.len;
1164 rpra[i].dma.offset = (uint32_t)(uintptr_t)lpra[i].buf.pv;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001165 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001166
1167 if (!ctx->fl->sctx->smmu.coherent) {
1168 PERF(ctx->fl->profile, ctx->fl->perf.flush,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001169 dmac_flush_range((char *)rpra, (char *)rpra + ctx->used);
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001170 PERF_END);
1171 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001172 bail:
1173 return err;
1174}
1175
1176static int put_args(uint32_t kernel, struct smq_invoke_ctx *ctx,
1177 remote_arg_t *upra)
1178{
1179 uint32_t sc = ctx->sc;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001180 struct smq_invoke_buf *list;
1181 struct smq_phy_page *pages;
1182 struct fastrpc_mmap *mmap;
1183 uint64_t *fdlist;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001184 remote_arg64_t *rpra = ctx->rpra;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001185 int i, inbufs, outbufs, handles;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001186 int err = 0;
1187
1188 inbufs = REMOTE_SCALARS_INBUFS(sc);
1189 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001190 handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc);
1191 list = smq_invoke_buf_start(ctx->rpra, sc);
1192 pages = smq_phy_page_start(sc, list);
1193 fdlist = (uint64_t *)(pages + inbufs + outbufs + handles);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001194 for (i = inbufs; i < inbufs + outbufs; ++i) {
1195 if (!ctx->maps[i]) {
1196 K_COPY_TO_USER(err, kernel,
1197 ctx->lpra[i].buf.pv,
1198 uint64_to_ptr(rpra[i].buf.pv),
1199 rpra[i].buf.len);
1200 if (err)
1201 goto bail;
1202 } else {
1203 fastrpc_mmap_free(ctx->maps[i]);
1204 ctx->maps[i] = 0;
1205 }
1206 }
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001207 if (inbufs + outbufs + handles) {
1208 for (i = 0; i < M_FDLIST; i++) {
1209 if (!fdlist[i])
1210 break;
1211 if (!fastrpc_mmap_find(ctx->fl, (int)fdlist[i], 0, 0,
Sathish Ambleyae5ee542017-01-16 22:24:23 -08001212 0, 0, &mmap))
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001213 fastrpc_mmap_free(mmap);
1214 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001215 }
1216 bail:
1217 return err;
1218}
1219
1220static void inv_args_pre(struct smq_invoke_ctx *ctx)
1221{
1222 int i, inbufs, outbufs;
1223 uint32_t sc = ctx->sc;
1224 remote_arg64_t *rpra = ctx->rpra;
1225 uintptr_t end;
1226
1227 inbufs = REMOTE_SCALARS_INBUFS(sc);
1228 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
1229 for (i = inbufs; i < inbufs + outbufs; ++i) {
1230 struct fastrpc_mmap *map = ctx->maps[i];
1231
1232 if (map && map->uncached)
1233 continue;
1234 if (!rpra[i].buf.len)
1235 continue;
1236 if (buf_page_start(ptr_to_uint64((void *)rpra)) ==
1237 buf_page_start(rpra[i].buf.pv))
1238 continue;
1239 if (!IS_CACHE_ALIGNED((uintptr_t)uint64_to_ptr(rpra[i].buf.pv)))
1240 dmac_flush_range(uint64_to_ptr(rpra[i].buf.pv),
1241 (char *)(uint64_to_ptr(rpra[i].buf.pv + 1)));
1242 end = (uintptr_t)uint64_to_ptr(rpra[i].buf.pv +
1243 rpra[i].buf.len);
1244 if (!IS_CACHE_ALIGNED(end))
1245 dmac_flush_range((char *)end,
1246 (char *)end + 1);
1247 }
1248}
1249
1250static void inv_args(struct smq_invoke_ctx *ctx)
1251{
1252 int i, inbufs, outbufs;
1253 uint32_t sc = ctx->sc;
1254 remote_arg64_t *rpra = ctx->rpra;
1255 int used = ctx->used;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001256
1257 inbufs = REMOTE_SCALARS_INBUFS(sc);
1258 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
1259 for (i = inbufs; i < inbufs + outbufs; ++i) {
1260 struct fastrpc_mmap *map = ctx->maps[i];
1261
1262 if (map && map->uncached)
1263 continue;
1264 if (!rpra[i].buf.len)
1265 continue;
1266 if (buf_page_start(ptr_to_uint64((void *)rpra)) ==
1267 buf_page_start(rpra[i].buf.pv)) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001268 continue;
1269 }
1270 if (map && map->handle)
1271 msm_ion_do_cache_op(ctx->fl->apps->client, map->handle,
1272 (char *)uint64_to_ptr(rpra[i].buf.pv),
1273 rpra[i].buf.len, ION_IOC_INV_CACHES);
1274 else
1275 dmac_inv_range((char *)uint64_to_ptr(rpra[i].buf.pv),
1276 (char *)uint64_to_ptr(rpra[i].buf.pv
1277 + rpra[i].buf.len));
1278 }
1279
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001280 if (rpra)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001281 dmac_inv_range(rpra, (char *)rpra + used);
1282}
1283
1284static int fastrpc_invoke_send(struct smq_invoke_ctx *ctx,
1285 uint32_t kernel, uint32_t handle)
1286{
1287 struct smq_msg *msg = &ctx->msg;
1288 struct fastrpc_file *fl = ctx->fl;
1289 struct fastrpc_channel_ctx *channel_ctx = &fl->apps->channel[fl->cid];
1290 int err = 0;
1291
1292 VERIFY(err, 0 != channel_ctx->chan);
1293 if (err)
1294 goto bail;
1295 msg->pid = current->tgid;
1296 msg->tid = current->pid;
1297 if (kernel)
1298 msg->pid = 0;
1299 msg->invoke.header.ctx = ptr_to_uint64(ctx) | fl->pd;
1300 msg->invoke.header.handle = handle;
1301 msg->invoke.header.sc = ctx->sc;
1302 msg->invoke.page.addr = ctx->buf ? ctx->buf->phys : 0;
1303 msg->invoke.page.size = buf_page_size(ctx->used);
1304
1305 if (fl->ssrcount != channel_ctx->ssrcount) {
1306 err = -ECONNRESET;
1307 goto bail;
1308 }
1309 VERIFY(err, channel_ctx->link.port_state ==
1310 FASTRPC_LINK_CONNECTED);
1311 if (err)
1312 goto bail;
1313 err = glink_tx(channel_ctx->chan,
1314 (void *)&fl->apps->channel[fl->cid], msg, sizeof(*msg),
1315 GLINK_TX_REQ_INTENT);
1316 bail:
1317 return err;
1318}
1319
1320static void fastrpc_init(struct fastrpc_apps *me)
1321{
1322 int i;
1323
1324 INIT_HLIST_HEAD(&me->drivers);
1325 spin_lock_init(&me->hlock);
1326 mutex_init(&me->smd_mutex);
1327 me->channel = &gcinfo[0];
1328 for (i = 0; i < NUM_CHANNELS; i++) {
1329 init_completion(&me->channel[i].work);
1330 me->channel[i].sesscount = 0;
1331 }
1332}
1333
1334static int fastrpc_release_current_dsp_process(struct fastrpc_file *fl);
1335
1336static int fastrpc_internal_invoke(struct fastrpc_file *fl, uint32_t mode,
1337 uint32_t kernel,
1338 struct fastrpc_ioctl_invoke_attrs *inv)
1339{
1340 struct smq_invoke_ctx *ctx = 0;
1341 struct fastrpc_ioctl_invoke *invoke = &inv->inv;
1342 int cid = fl->cid;
1343 int interrupted = 0;
1344 int err = 0;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001345 struct timespec invoket;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001346
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001347 if (fl->profile)
1348 getnstimeofday(&invoket);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001349 if (!kernel) {
1350 VERIFY(err, 0 == context_restore_interrupted(fl, inv,
1351 &ctx));
1352 if (err)
1353 goto bail;
1354 if (fl->sctx->smmu.faults)
1355 err = FASTRPC_ENOSUCH;
1356 if (err)
1357 goto bail;
1358 if (ctx)
1359 goto wait;
1360 }
1361
1362 VERIFY(err, 0 == context_alloc(fl, kernel, inv, &ctx));
1363 if (err)
1364 goto bail;
1365
1366 if (REMOTE_SCALARS_LENGTH(ctx->sc)) {
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001367 PERF(fl->profile, fl->perf.getargs,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001368 VERIFY(err, 0 == get_args(kernel, ctx));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001369 PERF_END);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001370 if (err)
1371 goto bail;
1372 }
1373
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001374 PERF(fl->profile, fl->perf.invargs,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001375 if (!fl->sctx->smmu.coherent) {
1376 inv_args_pre(ctx);
1377 if (mode == FASTRPC_MODE_SERIAL)
1378 inv_args(ctx);
1379 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001380 PERF_END);
1381
1382 PERF(fl->profile, fl->perf.link,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001383 VERIFY(err, 0 == fastrpc_invoke_send(ctx, kernel, invoke->handle));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001384 PERF_END);
1385
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001386 if (err)
1387 goto bail;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001388 PERF(fl->profile, fl->perf.invargs,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001389 if (mode == FASTRPC_MODE_PARALLEL && !fl->sctx->smmu.coherent)
1390 inv_args(ctx);
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001391 PERF_END);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001392 wait:
1393 if (kernel)
1394 wait_for_completion(&ctx->work);
1395 else {
1396 interrupted = wait_for_completion_interruptible(&ctx->work);
1397 VERIFY(err, 0 == (err = interrupted));
1398 if (err)
1399 goto bail;
1400 }
1401 VERIFY(err, 0 == (err = ctx->retval));
1402 if (err)
1403 goto bail;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001404
1405 PERF(fl->profile, fl->perf.putargs,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001406 VERIFY(err, 0 == put_args(kernel, ctx, invoke->pra));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001407 PERF_END);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001408 if (err)
1409 goto bail;
1410 bail:
1411 if (ctx && interrupted == -ERESTARTSYS)
1412 context_save_interrupted(ctx);
1413 else if (ctx)
1414 context_free(ctx);
1415 if (fl->ssrcount != fl->apps->channel[cid].ssrcount)
1416 err = ECONNRESET;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001417
1418 if (fl->profile && !interrupted) {
1419 if (invoke->handle != FASTRPC_STATIC_HANDLE_LISTENER)
1420 fl->perf.invoke += getnstimediff(&invoket);
1421 if (!(invoke->handle >= 0 &&
1422 invoke->handle <= FASTRPC_STATIC_HANDLE_MAX))
1423 fl->perf.count++;
1424 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001425 return err;
1426}
1427
1428static int fastrpc_init_process(struct fastrpc_file *fl,
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001429 struct fastrpc_ioctl_init_attrs *uproc)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001430{
1431 int err = 0;
1432 struct fastrpc_ioctl_invoke_attrs ioctl;
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001433 struct fastrpc_ioctl_init *init = &uproc->init;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001434 struct smq_phy_page pages[1];
1435 struct fastrpc_mmap *file = 0, *mem = 0;
1436
1437 if (init->flags == FASTRPC_INIT_ATTACH) {
1438 remote_arg_t ra[1];
1439 int tgid = current->tgid;
1440
1441 ra[0].buf.pv = (void *)&tgid;
1442 ra[0].buf.len = sizeof(tgid);
1443 ioctl.inv.handle = 1;
1444 ioctl.inv.sc = REMOTE_SCALARS_MAKE(0, 1, 0);
1445 ioctl.inv.pra = ra;
1446 ioctl.fds = 0;
1447 ioctl.attrs = 0;
1448 fl->pd = 0;
1449 VERIFY(err, !(err = fastrpc_internal_invoke(fl,
1450 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
1451 if (err)
1452 goto bail;
1453 } else if (init->flags == FASTRPC_INIT_CREATE) {
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001454 remote_arg_t ra[6];
1455 int fds[6];
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001456 int mflags = 0;
1457 struct {
1458 int pgid;
1459 int namelen;
1460 int filelen;
1461 int pageslen;
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001462 int attrs;
1463 int siglen;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001464 } inbuf;
1465
1466 inbuf.pgid = current->tgid;
1467 inbuf.namelen = strlen(current->comm) + 1;
1468 inbuf.filelen = init->filelen;
1469 fl->pd = 1;
1470 if (init->filelen) {
1471 VERIFY(err, !fastrpc_mmap_create(fl, init->filefd, 0,
1472 init->file, init->filelen, mflags, &file));
1473 if (err)
1474 goto bail;
1475 }
1476 inbuf.pageslen = 1;
1477 VERIFY(err, !fastrpc_mmap_create(fl, init->memfd, 0,
1478 init->mem, init->memlen, mflags, &mem));
1479 if (err)
1480 goto bail;
1481 inbuf.pageslen = 1;
1482 ra[0].buf.pv = (void *)&inbuf;
1483 ra[0].buf.len = sizeof(inbuf);
1484 fds[0] = 0;
1485
1486 ra[1].buf.pv = (void *)current->comm;
1487 ra[1].buf.len = inbuf.namelen;
1488 fds[1] = 0;
1489
1490 ra[2].buf.pv = (void *)init->file;
1491 ra[2].buf.len = inbuf.filelen;
1492 fds[2] = init->filefd;
1493
1494 pages[0].addr = mem->phys;
1495 pages[0].size = mem->size;
1496 ra[3].buf.pv = (void *)pages;
1497 ra[3].buf.len = 1 * sizeof(*pages);
1498 fds[3] = 0;
1499
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001500 inbuf.attrs = uproc->attrs;
1501 ra[4].buf.pv = (void *)&(inbuf.attrs);
1502 ra[4].buf.len = sizeof(inbuf.attrs);
1503 fds[4] = 0;
1504
1505 inbuf.siglen = uproc->siglen;
1506 ra[5].buf.pv = (void *)&(inbuf.siglen);
1507 ra[5].buf.len = sizeof(inbuf.siglen);
1508 fds[5] = 0;
1509
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001510 ioctl.inv.handle = 1;
1511 ioctl.inv.sc = REMOTE_SCALARS_MAKE(6, 4, 0);
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001512 if (uproc->attrs)
1513 ioctl.inv.sc = REMOTE_SCALARS_MAKE(7, 6, 0);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001514 ioctl.inv.pra = ra;
1515 ioctl.fds = fds;
1516 ioctl.attrs = 0;
1517 VERIFY(err, !(err = fastrpc_internal_invoke(fl,
1518 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
1519 if (err)
1520 goto bail;
1521 } else {
1522 err = -ENOTTY;
1523 }
1524bail:
1525 if (mem && err)
1526 fastrpc_mmap_free(mem);
1527 if (file)
1528 fastrpc_mmap_free(file);
1529 return err;
1530}
1531
1532static int fastrpc_release_current_dsp_process(struct fastrpc_file *fl)
1533{
1534 int err = 0;
1535 struct fastrpc_ioctl_invoke_attrs ioctl;
1536 remote_arg_t ra[1];
1537 int tgid = 0;
1538
1539 VERIFY(err, fl->apps->channel[fl->cid].chan != 0);
1540 if (err)
1541 goto bail;
1542 tgid = fl->tgid;
1543 ra[0].buf.pv = (void *)&tgid;
1544 ra[0].buf.len = sizeof(tgid);
1545 ioctl.inv.handle = 1;
1546 ioctl.inv.sc = REMOTE_SCALARS_MAKE(1, 1, 0);
1547 ioctl.inv.pra = ra;
1548 ioctl.fds = 0;
1549 ioctl.attrs = 0;
1550 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
1551 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
1552bail:
1553 return err;
1554}
1555
1556static int fastrpc_mmap_on_dsp(struct fastrpc_file *fl, uint32_t flags,
1557 struct fastrpc_mmap *map)
1558{
1559 struct fastrpc_ioctl_invoke_attrs ioctl;
1560 struct smq_phy_page page;
1561 int num = 1;
1562 remote_arg_t ra[3];
1563 int err = 0;
1564 struct {
1565 int pid;
1566 uint32_t flags;
1567 uintptr_t vaddrin;
1568 int num;
1569 } inargs;
1570 struct {
1571 uintptr_t vaddrout;
1572 } routargs;
1573
1574 inargs.pid = current->tgid;
1575 inargs.vaddrin = (uintptr_t)map->va;
1576 inargs.flags = flags;
1577 inargs.num = fl->apps->compat ? num * sizeof(page) : num;
1578 ra[0].buf.pv = (void *)&inargs;
1579 ra[0].buf.len = sizeof(inargs);
1580 page.addr = map->phys;
1581 page.size = map->size;
1582 ra[1].buf.pv = (void *)&page;
1583 ra[1].buf.len = num * sizeof(page);
1584
1585 ra[2].buf.pv = (void *)&routargs;
1586 ra[2].buf.len = sizeof(routargs);
1587
1588 ioctl.inv.handle = 1;
1589 if (fl->apps->compat)
1590 ioctl.inv.sc = REMOTE_SCALARS_MAKE(4, 2, 1);
1591 else
1592 ioctl.inv.sc = REMOTE_SCALARS_MAKE(2, 2, 1);
1593 ioctl.inv.pra = ra;
1594 ioctl.fds = 0;
1595 ioctl.attrs = 0;
1596 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
1597 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
1598 map->raddr = (uintptr_t)routargs.vaddrout;
1599
1600 return err;
1601}
1602
1603static int fastrpc_munmap_on_dsp(struct fastrpc_file *fl,
1604 struct fastrpc_mmap *map)
1605{
1606 struct fastrpc_ioctl_invoke_attrs ioctl;
1607 remote_arg_t ra[1];
1608 int err = 0;
1609 struct {
1610 int pid;
1611 uintptr_t vaddrout;
1612 ssize_t size;
1613 } inargs;
1614
1615 inargs.pid = current->tgid;
1616 inargs.size = map->size;
1617 inargs.vaddrout = map->raddr;
1618 ra[0].buf.pv = (void *)&inargs;
1619 ra[0].buf.len = sizeof(inargs);
1620
1621 ioctl.inv.handle = 1;
1622 if (fl->apps->compat)
1623 ioctl.inv.sc = REMOTE_SCALARS_MAKE(5, 1, 0);
1624 else
1625 ioctl.inv.sc = REMOTE_SCALARS_MAKE(3, 1, 0);
1626 ioctl.inv.pra = ra;
1627 ioctl.fds = 0;
1628 ioctl.attrs = 0;
1629 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
1630 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
1631 return err;
1632}
1633
1634static int fastrpc_mmap_remove(struct fastrpc_file *fl, uintptr_t va,
1635 ssize_t len, struct fastrpc_mmap **ppmap);
1636
1637static void fastrpc_mmap_add(struct fastrpc_mmap *map);
1638
1639static int fastrpc_internal_munmap(struct fastrpc_file *fl,
1640 struct fastrpc_ioctl_munmap *ud)
1641{
1642 int err = 0;
1643 struct fastrpc_mmap *map = 0;
1644
1645 VERIFY(err, !fastrpc_mmap_remove(fl, ud->vaddrout, ud->size, &map));
1646 if (err)
1647 goto bail;
1648 VERIFY(err, !fastrpc_munmap_on_dsp(fl, map));
1649 if (err)
1650 goto bail;
1651 fastrpc_mmap_free(map);
1652bail:
1653 if (err && map)
1654 fastrpc_mmap_add(map);
1655 return err;
1656}
1657
1658static int fastrpc_internal_mmap(struct fastrpc_file *fl,
1659 struct fastrpc_ioctl_mmap *ud)
1660{
1661
1662 struct fastrpc_mmap *map = 0;
1663 int err = 0;
1664
1665 if (!fastrpc_mmap_find(fl, ud->fd, (uintptr_t)ud->vaddrin, ud->size,
Sathish Ambleyae5ee542017-01-16 22:24:23 -08001666 ud->flags, 1, &map))
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001667 return 0;
1668
1669 VERIFY(err, !fastrpc_mmap_create(fl, ud->fd, 0,
1670 (uintptr_t)ud->vaddrin, ud->size, ud->flags, &map));
1671 if (err)
1672 goto bail;
1673 VERIFY(err, 0 == fastrpc_mmap_on_dsp(fl, ud->flags, map));
1674 if (err)
1675 goto bail;
1676 ud->vaddrout = map->raddr;
1677 bail:
1678 if (err && map)
1679 fastrpc_mmap_free(map);
1680 return err;
1681}
1682
1683static void fastrpc_channel_close(struct kref *kref)
1684{
1685 struct fastrpc_apps *me = &gfa;
1686 struct fastrpc_channel_ctx *ctx;
1687 int cid;
1688
1689 ctx = container_of(kref, struct fastrpc_channel_ctx, kref);
1690 cid = ctx - &gcinfo[0];
1691 fastrpc_glink_close(ctx->chan, cid);
1692 ctx->chan = 0;
1693 mutex_unlock(&me->smd_mutex);
1694 pr_info("'closed /dev/%s c %d %d'\n", gcinfo[cid].name,
1695 MAJOR(me->dev_no), cid);
1696}
1697
1698static void fastrpc_context_list_dtor(struct fastrpc_file *fl);
1699
1700static int fastrpc_session_alloc_locked(struct fastrpc_channel_ctx *chan,
1701 int secure, struct fastrpc_session_ctx **session)
1702{
1703 struct fastrpc_apps *me = &gfa;
1704 int idx = 0, err = 0;
1705
1706 if (chan->sesscount) {
1707 for (idx = 0; idx < chan->sesscount; ++idx) {
1708 if (!chan->session[idx].used &&
1709 chan->session[idx].smmu.secure == secure) {
1710 chan->session[idx].used = 1;
1711 break;
1712 }
1713 }
1714 VERIFY(err, idx < chan->sesscount);
1715 if (err)
1716 goto bail;
1717 chan->session[idx].smmu.faults = 0;
1718 } else {
1719 VERIFY(err, me->dev != NULL);
1720 if (err)
1721 goto bail;
1722 chan->session[0].dev = me->dev;
1723 }
1724
1725 *session = &chan->session[idx];
1726 bail:
1727 return err;
1728}
1729
1730bool fastrpc_glink_notify_rx_intent_req(void *h, const void *priv, size_t size)
1731{
1732 if (glink_queue_rx_intent(h, NULL, size))
1733 return false;
1734 return true;
1735}
1736
1737void fastrpc_glink_notify_tx_done(void *handle, const void *priv,
1738 const void *pkt_priv, const void *ptr)
1739{
1740}
1741
1742void fastrpc_glink_notify_rx(void *handle, const void *priv,
1743 const void *pkt_priv, const void *ptr, size_t size)
1744{
1745 struct smq_invoke_rsp *rsp = (struct smq_invoke_rsp *)ptr;
1746 int len = size;
1747
1748 while (len >= sizeof(*rsp) && rsp) {
1749 rsp->ctx = rsp->ctx & ~1;
1750 context_notify_user(uint64_to_ptr(rsp->ctx), rsp->retval);
1751 rsp++;
1752 len = len - sizeof(*rsp);
1753 }
1754 glink_rx_done(handle, ptr, true);
1755}
1756
1757void fastrpc_glink_notify_state(void *handle, const void *priv,
1758 unsigned int event)
1759{
1760 struct fastrpc_apps *me = &gfa;
1761 int cid = (int)(uintptr_t)priv;
1762 struct fastrpc_glink_info *link;
1763
1764 if (cid < 0 || cid >= NUM_CHANNELS)
1765 return;
1766 link = &me->channel[cid].link;
1767 switch (event) {
1768 case GLINK_CONNECTED:
1769 link->port_state = FASTRPC_LINK_CONNECTED;
1770 complete(&me->channel[cid].work);
1771 break;
1772 case GLINK_LOCAL_DISCONNECTED:
1773 link->port_state = FASTRPC_LINK_DISCONNECTED;
1774 break;
1775 case GLINK_REMOTE_DISCONNECTED:
1776 if (me->channel[cid].chan &&
1777 link->link_state == FASTRPC_LINK_STATE_UP) {
1778 fastrpc_glink_close(me->channel[cid].chan, cid);
1779 me->channel[cid].chan = 0;
1780 link->port_state = FASTRPC_LINK_DISCONNECTED;
1781 }
1782 break;
1783 default:
1784 break;
1785 }
1786}
1787
1788static int fastrpc_session_alloc(struct fastrpc_channel_ctx *chan, int secure,
1789 struct fastrpc_session_ctx **session)
1790{
1791 int err = 0;
1792 struct fastrpc_apps *me = &gfa;
1793
1794 mutex_lock(&me->smd_mutex);
1795 if (!*session)
1796 err = fastrpc_session_alloc_locked(chan, secure, session);
1797 mutex_unlock(&me->smd_mutex);
1798 return err;
1799}
1800
1801static void fastrpc_session_free(struct fastrpc_channel_ctx *chan,
1802 struct fastrpc_session_ctx *session)
1803{
1804 struct fastrpc_apps *me = &gfa;
1805
1806 mutex_lock(&me->smd_mutex);
1807 session->used = 0;
1808 mutex_unlock(&me->smd_mutex);
1809}
1810
1811static int fastrpc_file_free(struct fastrpc_file *fl)
1812{
1813 struct hlist_node *n;
1814 struct fastrpc_mmap *map = 0;
1815 int cid;
1816
1817 if (!fl)
1818 return 0;
1819 cid = fl->cid;
1820
1821 spin_lock(&fl->apps->hlock);
1822 hlist_del_init(&fl->hn);
1823 spin_unlock(&fl->apps->hlock);
1824
1825 (void)fastrpc_release_current_dsp_process(fl);
1826 fastrpc_context_list_dtor(fl);
1827 fastrpc_buf_list_free(fl);
1828 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
1829 fastrpc_mmap_free(map);
1830 }
1831 if (fl->ssrcount == fl->apps->channel[cid].ssrcount)
1832 kref_put_mutex(&fl->apps->channel[cid].kref,
1833 fastrpc_channel_close, &fl->apps->smd_mutex);
1834 if (fl->sctx)
1835 fastrpc_session_free(&fl->apps->channel[cid], fl->sctx);
1836 if (fl->secsctx)
1837 fastrpc_session_free(&fl->apps->channel[cid], fl->secsctx);
1838 kfree(fl);
1839 return 0;
1840}
1841
1842static int fastrpc_device_release(struct inode *inode, struct file *file)
1843{
1844 struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data;
1845
1846 if (fl) {
Sathish Ambley1ca68232017-01-19 10:32:55 -08001847 if (fl->debugfs_file != NULL)
1848 debugfs_remove(fl->debugfs_file);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001849 fastrpc_file_free(fl);
1850 file->private_data = 0;
1851 }
1852 return 0;
1853}
1854
1855static void fastrpc_link_state_handler(struct glink_link_state_cb_info *cb_info,
1856 void *priv)
1857{
1858 struct fastrpc_apps *me = &gfa;
1859 int cid = (int)((uintptr_t)priv);
1860 struct fastrpc_glink_info *link;
1861
1862 if (cid < 0 || cid >= NUM_CHANNELS)
1863 return;
1864
1865 link = &me->channel[cid].link;
1866 switch (cb_info->link_state) {
1867 case GLINK_LINK_STATE_UP:
1868 link->link_state = FASTRPC_LINK_STATE_UP;
1869 complete(&me->channel[cid].work);
1870 break;
1871 case GLINK_LINK_STATE_DOWN:
1872 link->link_state = FASTRPC_LINK_STATE_DOWN;
1873 break;
1874 default:
1875 pr_err("adsprpc: unknown link state %d\n", cb_info->link_state);
1876 break;
1877 }
1878}
1879
1880static int fastrpc_glink_register(int cid, struct fastrpc_apps *me)
1881{
1882 int err = 0;
1883 struct fastrpc_glink_info *link;
1884
1885 VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS));
1886 if (err)
1887 goto bail;
1888
1889 link = &me->channel[cid].link;
1890 if (link->link_notify_handle != NULL)
1891 goto bail;
1892
1893 link->link_info.glink_link_state_notif_cb = fastrpc_link_state_handler;
1894 link->link_notify_handle = glink_register_link_state_cb(
1895 &link->link_info,
1896 (void *)((uintptr_t)cid));
1897 VERIFY(err, !IS_ERR_OR_NULL(me->channel[cid].link.link_notify_handle));
1898 if (err) {
1899 link->link_notify_handle = NULL;
1900 goto bail;
1901 }
1902 VERIFY(err, wait_for_completion_timeout(&me->channel[cid].work,
1903 RPC_TIMEOUT));
1904bail:
1905 return err;
1906}
1907
1908static void fastrpc_glink_close(void *chan, int cid)
1909{
1910 int err = 0;
1911 struct fastrpc_glink_info *link;
1912
1913 VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS));
1914 if (err)
1915 return;
1916 link = &gfa.channel[cid].link;
1917
1918 if (link->port_state == FASTRPC_LINK_CONNECTED ||
1919 link->port_state == FASTRPC_LINK_CONNECTING) {
1920 link->port_state = FASTRPC_LINK_DISCONNECTING;
1921 glink_close(chan);
1922 }
1923}
1924
1925static int fastrpc_glink_open(int cid)
1926{
1927 int err = 0;
1928 void *handle = NULL;
1929 struct fastrpc_apps *me = &gfa;
1930 struct glink_open_config *cfg;
1931 struct fastrpc_glink_info *link;
1932
1933 VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS));
1934 if (err)
1935 goto bail;
1936 link = &me->channel[cid].link;
1937 cfg = &me->channel[cid].link.cfg;
1938 VERIFY(err, (link->link_state == FASTRPC_LINK_STATE_UP));
1939 if (err)
1940 goto bail;
1941
1942 if (link->port_state == FASTRPC_LINK_CONNECTED ||
1943 link->port_state == FASTRPC_LINK_CONNECTING) {
1944 goto bail;
1945 }
1946
1947 link->port_state = FASTRPC_LINK_CONNECTING;
1948 cfg->priv = (void *)(uintptr_t)cid;
1949 cfg->edge = gcinfo[cid].link.link_info.edge;
1950 cfg->transport = gcinfo[cid].link.link_info.transport;
1951 cfg->name = FASTRPC_GLINK_GUID;
1952 cfg->notify_rx = fastrpc_glink_notify_rx;
1953 cfg->notify_tx_done = fastrpc_glink_notify_tx_done;
1954 cfg->notify_state = fastrpc_glink_notify_state;
1955 cfg->notify_rx_intent_req = fastrpc_glink_notify_rx_intent_req;
1956 handle = glink_open(cfg);
1957 VERIFY(err, !IS_ERR_OR_NULL(handle));
1958 if (err)
1959 goto bail;
1960 me->channel[cid].chan = handle;
1961bail:
1962 return err;
1963}
1964
Sathish Ambley1ca68232017-01-19 10:32:55 -08001965static int fastrpc_debugfs_open(struct inode *inode, struct file *filp)
1966{
1967 filp->private_data = inode->i_private;
1968 return 0;
1969}
1970
1971static ssize_t fastrpc_debugfs_read(struct file *filp, char __user *buffer,
1972 size_t count, loff_t *position)
1973{
1974 struct fastrpc_file *fl = filp->private_data;
1975 struct hlist_node *n;
1976 struct fastrpc_buf *buf = 0;
1977 struct fastrpc_mmap *map = 0;
1978 struct smq_invoke_ctx *ictx = 0;
1979 struct fastrpc_channel_ctx *chan;
1980 struct fastrpc_session_ctx *sess;
1981 unsigned int len = 0;
1982 int i, j, ret = 0;
1983 char *fileinfo = NULL;
1984
1985 fileinfo = kzalloc(DEBUGFS_SIZE, GFP_KERNEL);
1986 if (!fileinfo)
1987 goto bail;
1988 if (fl == NULL) {
1989 for (i = 0; i < NUM_CHANNELS; i++) {
1990 chan = &gcinfo[i];
1991 len += scnprintf(fileinfo + len,
1992 DEBUGFS_SIZE - len, "%s\n\n",
1993 chan->name);
1994 len += scnprintf(fileinfo + len,
1995 DEBUGFS_SIZE - len, "%s %d\n",
1996 "sesscount:", chan->sesscount);
1997 for (j = 0; j < chan->sesscount; j++) {
1998 sess = &chan->session[j];
1999 len += scnprintf(fileinfo + len,
2000 DEBUGFS_SIZE - len,
2001 "%s%d\n\n", "SESSION", j);
2002 len += scnprintf(fileinfo + len,
2003 DEBUGFS_SIZE - len,
2004 "%s %d\n", "sid:",
2005 sess->smmu.cb);
2006 len += scnprintf(fileinfo + len,
2007 DEBUGFS_SIZE - len,
2008 "%s %d\n", "SECURE:",
2009 sess->smmu.secure);
2010 }
2011 }
2012 } else {
2013 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2014 "%s %d\n\n",
2015 "PROCESS_ID:", fl->tgid);
2016 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2017 "%s %d\n\n",
2018 "CHANNEL_ID:", fl->cid);
2019 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2020 "%s %d\n\n",
2021 "SSRCOUNT:", fl->ssrcount);
2022 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2023 "%s\n",
2024 "LIST OF BUFS:");
2025 spin_lock(&fl->hlock);
2026 hlist_for_each_entry_safe(buf, n, &fl->bufs, hn) {
2027 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2028 "%s %p %s %p %s %llx\n", "buf:",
2029 buf, "buf->virt:", buf->virt,
2030 "buf->phys:", buf->phys);
2031 }
2032 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2033 "\n%s\n",
2034 "LIST OF MAPS:");
2035 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
2036 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2037 "%s %p %s %lx %s %llx\n",
2038 "map:", map,
2039 "map->va:", map->va,
2040 "map->phys:", map->phys);
2041 }
2042 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2043 "\n%s\n",
2044 "LIST OF PENDING SMQCONTEXTS:");
2045 hlist_for_each_entry_safe(ictx, n, &fl->clst.pending, hn) {
2046 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2047 "%s %p %s %u %s %u %s %u\n",
2048 "smqcontext:", ictx,
2049 "sc:", ictx->sc,
2050 "tid:", ictx->pid,
2051 "handle", ictx->rpra->h);
2052 }
2053 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2054 "\n%s\n",
2055 "LIST OF INTERRUPTED SMQCONTEXTS:");
2056 hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) {
2057 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2058 "%s %p %s %u %s %u %s %u\n",
2059 "smqcontext:", ictx,
2060 "sc:", ictx->sc,
2061 "tid:", ictx->pid,
2062 "handle", ictx->rpra->h);
2063 }
2064 spin_unlock(&fl->hlock);
2065 }
2066 if (len > DEBUGFS_SIZE)
2067 len = DEBUGFS_SIZE;
2068 ret = simple_read_from_buffer(buffer, count, position, fileinfo, len);
2069 kfree(fileinfo);
2070bail:
2071 return ret;
2072}
2073
2074static const struct file_operations debugfs_fops = {
2075 .open = fastrpc_debugfs_open,
2076 .read = fastrpc_debugfs_read,
2077};
2078
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002079static int fastrpc_device_open(struct inode *inode, struct file *filp)
2080{
2081 int cid = MINOR(inode->i_rdev);
Sathish Ambley1ca68232017-01-19 10:32:55 -08002082 struct dentry *debugfs_file;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002083 int err = 0;
2084 struct fastrpc_apps *me = &gfa;
2085 struct fastrpc_file *fl = 0;
2086
2087 VERIFY(err, fl = kzalloc(sizeof(*fl), GFP_KERNEL));
2088 if (err)
2089 return err;
2090
2091 filp->private_data = fl;
2092
2093 mutex_lock(&me->smd_mutex);
2094
Sathish Ambley1ca68232017-01-19 10:32:55 -08002095 debugfs_file = debugfs_create_file(current->comm, 0644, debugfs_root,
2096 fl, &debugfs_fops);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002097 context_list_ctor(&fl->clst);
2098 spin_lock_init(&fl->hlock);
2099 INIT_HLIST_HEAD(&fl->maps);
2100 INIT_HLIST_HEAD(&fl->bufs);
2101 INIT_HLIST_NODE(&fl->hn);
2102 fl->tgid = current->tgid;
2103 fl->apps = me;
2104 fl->cid = cid;
Sathish Ambley1ca68232017-01-19 10:32:55 -08002105 if (debugfs_file != NULL)
2106 fl->debugfs_file = debugfs_file;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002107 memset(&fl->perf, 0, sizeof(fl->perf));
2108
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002109 VERIFY(err, !fastrpc_session_alloc_locked(&me->channel[cid], 0,
2110 &fl->sctx));
2111 if (err)
2112 goto bail;
2113 fl->cid = cid;
2114 fl->ssrcount = me->channel[cid].ssrcount;
2115 if ((kref_get_unless_zero(&me->channel[cid].kref) == 0) ||
2116 (me->channel[cid].chan == 0)) {
2117 fastrpc_glink_register(cid, me);
2118 VERIFY(err, 0 == fastrpc_glink_open(cid));
2119 if (err)
2120 goto bail;
2121
2122 VERIFY(err, wait_for_completion_timeout(&me->channel[cid].work,
2123 RPC_TIMEOUT));
2124 if (err) {
2125 me->channel[cid].chan = 0;
2126 goto bail;
2127 }
2128 kref_init(&me->channel[cid].kref);
2129 pr_info("'opened /dev/%s c %d %d'\n", gcinfo[cid].name,
2130 MAJOR(me->dev_no), cid);
2131 if (me->channel[cid].ssrcount !=
2132 me->channel[cid].prevssrcount) {
2133 me->channel[cid].prevssrcount =
2134 me->channel[cid].ssrcount;
2135 }
2136 }
2137 spin_lock(&me->hlock);
2138 hlist_add_head(&fl->hn, &me->drivers);
2139 spin_unlock(&me->hlock);
2140
2141bail:
2142 mutex_unlock(&me->smd_mutex);
2143
2144 if (err)
2145 fastrpc_device_release(inode, filp);
2146 return err;
2147}
2148
2149static int fastrpc_get_info(struct fastrpc_file *fl, uint32_t *info)
2150{
2151 int err = 0;
2152
2153 VERIFY(err, fl && fl->sctx);
2154 if (err)
2155 goto bail;
2156 *info = (fl->sctx->smmu.enabled ? 1 : 0);
2157bail:
2158 return err;
2159}
2160
2161static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num,
2162 unsigned long ioctl_param)
2163{
2164 union {
2165 struct fastrpc_ioctl_invoke_attrs inv;
2166 struct fastrpc_ioctl_mmap mmap;
2167 struct fastrpc_ioctl_munmap munmap;
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002168 struct fastrpc_ioctl_init_attrs init;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002169 struct fastrpc_ioctl_perf perf;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002170 } p;
2171 void *param = (char *)ioctl_param;
2172 struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data;
2173 int size = 0, err = 0;
2174 uint32_t info;
2175
2176 p.inv.fds = 0;
2177 p.inv.attrs = 0;
2178
2179 switch (ioctl_num) {
2180 case FASTRPC_IOCTL_INVOKE:
2181 size = sizeof(struct fastrpc_ioctl_invoke);
2182 case FASTRPC_IOCTL_INVOKE_FD:
2183 if (!size)
2184 size = sizeof(struct fastrpc_ioctl_invoke_fd);
2185 /* fall through */
2186 case FASTRPC_IOCTL_INVOKE_ATTRS:
2187 if (!size)
2188 size = sizeof(struct fastrpc_ioctl_invoke_attrs);
2189 VERIFY(err, 0 == copy_from_user(&p.inv, param, size));
2190 if (err)
2191 goto bail;
2192 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, fl->mode,
2193 0, &p.inv)));
2194 if (err)
2195 goto bail;
2196 break;
2197 case FASTRPC_IOCTL_MMAP:
2198 VERIFY(err, 0 == copy_from_user(&p.mmap, param,
2199 sizeof(p.mmap)));
2200 if (err)
2201 goto bail;
2202 VERIFY(err, 0 == (err = fastrpc_internal_mmap(fl, &p.mmap)));
2203 if (err)
2204 goto bail;
2205 VERIFY(err, 0 == copy_to_user(param, &p.mmap, sizeof(p.mmap)));
2206 if (err)
2207 goto bail;
2208 break;
2209 case FASTRPC_IOCTL_MUNMAP:
2210 VERIFY(err, 0 == copy_from_user(&p.munmap, param,
2211 sizeof(p.munmap)));
2212 if (err)
2213 goto bail;
2214 VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl,
2215 &p.munmap)));
2216 if (err)
2217 goto bail;
2218 break;
2219 case FASTRPC_IOCTL_SETMODE:
2220 switch ((uint32_t)ioctl_param) {
2221 case FASTRPC_MODE_PARALLEL:
2222 case FASTRPC_MODE_SERIAL:
2223 fl->mode = (uint32_t)ioctl_param;
2224 break;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002225 case FASTRPC_MODE_PROFILE:
2226 fl->profile = (uint32_t)ioctl_param;
2227 break;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002228 default:
2229 err = -ENOTTY;
2230 break;
2231 }
2232 break;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002233 case FASTRPC_IOCTL_GETPERF:
2234 VERIFY(err, 0 == copy_from_user(&p.perf,
2235 param, sizeof(p.perf)));
2236 if (err)
2237 goto bail;
2238 p.perf.numkeys = sizeof(struct fastrpc_perf)/sizeof(int64_t);
2239 if (p.perf.keys) {
2240 char *keys = PERF_KEYS;
2241
2242 VERIFY(err, 0 == copy_to_user((char *)p.perf.keys,
2243 keys, strlen(keys)+1));
2244 if (err)
2245 goto bail;
2246 }
2247 if (p.perf.data) {
2248 VERIFY(err, 0 == copy_to_user((int64_t *)p.perf.data,
2249 &fl->perf, sizeof(fl->perf)));
2250 }
2251 VERIFY(err, 0 == copy_to_user(param, &p.perf, sizeof(p.perf)));
2252 if (err)
2253 goto bail;
2254 break;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002255 case FASTRPC_IOCTL_GETINFO:
2256 VERIFY(err, 0 == (err = fastrpc_get_info(fl, &info)));
2257 if (err)
2258 goto bail;
2259 VERIFY(err, 0 == copy_to_user(param, &info, sizeof(info)));
2260 if (err)
2261 goto bail;
2262 break;
2263 case FASTRPC_IOCTL_INIT:
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002264 p.init.attrs = 0;
2265 p.init.siglen = 0;
2266 size = sizeof(struct fastrpc_ioctl_init);
2267 /* fall through */
2268 case FASTRPC_IOCTL_INIT_ATTRS:
2269 if (!size)
2270 size = sizeof(struct fastrpc_ioctl_init_attrs);
2271 VERIFY(err, 0 == copy_from_user(&p.init, param, size));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002272 if (err)
2273 goto bail;
2274 VERIFY(err, 0 == fastrpc_init_process(fl, &p.init));
2275 if (err)
2276 goto bail;
2277 break;
2278
2279 default:
2280 err = -ENOTTY;
2281 pr_info("bad ioctl: %d\n", ioctl_num);
2282 break;
2283 }
2284 bail:
2285 return err;
2286}
2287
2288static int fastrpc_restart_notifier_cb(struct notifier_block *nb,
2289 unsigned long code,
2290 void *data)
2291{
2292 struct fastrpc_apps *me = &gfa;
2293 struct fastrpc_channel_ctx *ctx;
2294 int cid;
2295
2296 ctx = container_of(nb, struct fastrpc_channel_ctx, nb);
2297 cid = ctx - &me->channel[0];
2298 if (code == SUBSYS_BEFORE_SHUTDOWN) {
2299 mutex_lock(&me->smd_mutex);
2300 ctx->ssrcount++;
2301 if (ctx->chan) {
2302 fastrpc_glink_close(ctx->chan, cid);
2303 ctx->chan = 0;
2304 pr_info("'restart notifier: closed /dev/%s c %d %d'\n",
2305 gcinfo[cid].name, MAJOR(me->dev_no), cid);
2306 }
2307 mutex_unlock(&me->smd_mutex);
2308 fastrpc_notify_drivers(me, cid);
2309 }
2310
2311 return NOTIFY_DONE;
2312}
2313
2314static const struct file_operations fops = {
2315 .open = fastrpc_device_open,
2316 .release = fastrpc_device_release,
2317 .unlocked_ioctl = fastrpc_device_ioctl,
2318 .compat_ioctl = compat_fastrpc_device_ioctl,
2319};
2320
2321static const struct of_device_id fastrpc_match_table[] = {
2322 { .compatible = "qcom,msm-fastrpc-adsp", },
2323 { .compatible = "qcom,msm-fastrpc-compute", },
2324 { .compatible = "qcom,msm-fastrpc-compute-cb", },
2325 { .compatible = "qcom,msm-adsprpc-mem-region", },
2326 {}
2327};
2328
2329static int fastrpc_cb_probe(struct device *dev)
2330{
2331 struct fastrpc_channel_ctx *chan;
2332 struct fastrpc_session_ctx *sess;
2333 struct of_phandle_args iommuspec;
2334 const char *name;
2335 unsigned int start = 0x80000000;
2336 int err = 0, i;
2337 int secure_vmid = VMID_CP_PIXEL;
2338
2339 VERIFY(err, 0 != (name = of_get_property(dev->of_node, "label", NULL)));
2340 if (err)
2341 goto bail;
2342 for (i = 0; i < NUM_CHANNELS; i++) {
2343 if (!gcinfo[i].name)
2344 continue;
2345 if (!strcmp(name, gcinfo[i].name))
2346 break;
2347 }
2348 VERIFY(err, i < NUM_CHANNELS);
2349 if (err)
2350 goto bail;
2351 chan = &gcinfo[i];
2352 VERIFY(err, chan->sesscount < NUM_SESSIONS);
2353 if (err)
2354 goto bail;
2355
2356 VERIFY(err, !of_parse_phandle_with_args(dev->of_node, "iommus",
2357 "#iommu-cells", 0, &iommuspec));
2358 if (err)
2359 goto bail;
2360 sess = &chan->session[chan->sesscount];
2361 sess->smmu.cb = iommuspec.args[0] & 0xf;
2362 sess->used = 0;
2363 sess->smmu.coherent = of_property_read_bool(dev->of_node,
2364 "dma-coherent");
2365 sess->smmu.secure = of_property_read_bool(dev->of_node,
2366 "qcom,secure-context-bank");
2367 if (sess->smmu.secure)
2368 start = 0x60000000;
2369 VERIFY(err, !IS_ERR_OR_NULL(sess->smmu.mapping =
2370 arm_iommu_create_mapping(&platform_bus_type,
2371 start, 0x7fffffff)));
2372 if (err)
2373 goto bail;
2374
2375 if (sess->smmu.secure)
2376 iommu_domain_set_attr(sess->smmu.mapping->domain,
2377 DOMAIN_ATTR_SECURE_VMID,
2378 &secure_vmid);
2379
2380 VERIFY(err, !arm_iommu_attach_device(dev, sess->smmu.mapping));
2381 if (err)
2382 goto bail;
2383 sess->dev = dev;
2384 sess->smmu.enabled = 1;
2385 chan->sesscount++;
Sathish Ambley1ca68232017-01-19 10:32:55 -08002386 debugfs_global_file = debugfs_create_file("global", 0644, debugfs_root,
2387 NULL, &debugfs_fops);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002388bail:
2389 return err;
2390}
2391
2392static int fastrpc_probe(struct platform_device *pdev)
2393{
2394 int err = 0;
2395 struct fastrpc_apps *me = &gfa;
2396 struct device *dev = &pdev->dev;
2397 struct smq_phy_page range;
2398 struct device_node *ion_node, *node;
2399 struct platform_device *ion_pdev;
2400 struct cma *cma;
2401 uint32_t val;
2402
2403 if (of_device_is_compatible(dev->of_node,
2404 "qcom,msm-fastrpc-compute-cb"))
2405 return fastrpc_cb_probe(dev);
2406
2407 if (of_device_is_compatible(dev->of_node,
2408 "qcom,msm-adsprpc-mem-region")) {
2409 me->dev = dev;
2410 range.addr = 0;
2411 ion_node = of_find_compatible_node(NULL, NULL, "qcom,msm-ion");
2412 if (ion_node) {
2413 for_each_available_child_of_node(ion_node, node) {
2414 if (of_property_read_u32(node, "reg", &val))
2415 continue;
2416 if (val != ION_ADSP_HEAP_ID)
2417 continue;
2418 ion_pdev = of_find_device_by_node(node);
2419 if (!ion_pdev)
2420 break;
2421 cma = dev_get_cma_area(&ion_pdev->dev);
2422 if (cma) {
2423 range.addr = cma_get_base(cma);
2424 range.size = (size_t)cma_get_size(cma);
2425 }
2426 break;
2427 }
2428 }
2429 if (range.addr) {
2430 int srcVM[1] = {VMID_HLOS};
2431 int destVM[4] = {VMID_HLOS, VMID_MSS_MSA, VMID_SSC_Q6,
2432 VMID_ADSP_Q6};
2433 int destVMperm[4] = {PERM_READ | PERM_WRITE,
2434 PERM_READ | PERM_WRITE | PERM_EXEC,
2435 PERM_READ | PERM_WRITE | PERM_EXEC,
2436 PERM_READ | PERM_WRITE | PERM_EXEC,
2437 };
2438
2439 VERIFY(err, !hyp_assign_phys(range.addr, range.size,
2440 srcVM, 1, destVM, destVMperm, 4));
2441 if (err)
2442 goto bail;
2443 }
2444 return 0;
2445 }
2446
2447 VERIFY(err, !of_platform_populate(pdev->dev.of_node,
2448 fastrpc_match_table,
2449 NULL, &pdev->dev));
2450 if (err)
2451 goto bail;
2452bail:
2453 return err;
2454}
2455
2456static void fastrpc_deinit(void)
2457{
2458 struct fastrpc_apps *me = &gfa;
2459 struct fastrpc_channel_ctx *chan = gcinfo;
2460 int i, j;
2461
2462 for (i = 0; i < NUM_CHANNELS; i++, chan++) {
2463 if (chan->chan) {
2464 kref_put_mutex(&chan->kref,
2465 fastrpc_channel_close, &me->smd_mutex);
2466 chan->chan = 0;
2467 }
2468 for (j = 0; j < NUM_SESSIONS; j++) {
2469 struct fastrpc_session_ctx *sess = &chan->session[j];
2470
2471 if (sess->smmu.enabled) {
2472 arm_iommu_detach_device(sess->dev);
2473 sess->dev = 0;
2474 }
2475 if (sess->smmu.mapping) {
2476 arm_iommu_release_mapping(sess->smmu.mapping);
2477 sess->smmu.mapping = 0;
2478 }
2479 }
2480 }
2481}
2482
2483static struct platform_driver fastrpc_driver = {
2484 .probe = fastrpc_probe,
2485 .driver = {
2486 .name = "fastrpc",
2487 .owner = THIS_MODULE,
2488 .of_match_table = fastrpc_match_table,
2489 },
2490};
2491
2492static int __init fastrpc_device_init(void)
2493{
2494 struct fastrpc_apps *me = &gfa;
2495 int err = 0, i;
2496
2497 memset(me, 0, sizeof(*me));
2498
2499 fastrpc_init(me);
2500 me->dev = NULL;
2501 VERIFY(err, 0 == platform_driver_register(&fastrpc_driver));
2502 if (err)
2503 goto register_bail;
2504 VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, NUM_CHANNELS,
2505 DEVICE_NAME));
2506 if (err)
2507 goto alloc_chrdev_bail;
2508 cdev_init(&me->cdev, &fops);
2509 me->cdev.owner = THIS_MODULE;
2510 VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0),
2511 NUM_CHANNELS));
2512 if (err)
2513 goto cdev_init_bail;
2514 me->class = class_create(THIS_MODULE, "fastrpc");
2515 VERIFY(err, !IS_ERR(me->class));
2516 if (err)
2517 goto class_create_bail;
2518 me->compat = (fops.compat_ioctl == NULL) ? 0 : 1;
2519 for (i = 0; i < NUM_CHANNELS; i++) {
2520 if (!gcinfo[i].name)
2521 continue;
2522 me->channel[i].dev = device_create(me->class, NULL,
2523 MKDEV(MAJOR(me->dev_no), i),
2524 NULL, gcinfo[i].name);
2525 VERIFY(err, !IS_ERR(me->channel[i].dev));
2526 if (err)
2527 goto device_create_bail;
2528 me->channel[i].ssrcount = 0;
2529 me->channel[i].prevssrcount = 0;
2530 me->channel[i].nb.notifier_call = fastrpc_restart_notifier_cb;
2531 me->channel[i].handle = subsys_notif_register_notifier(
2532 gcinfo[i].subsys,
2533 &me->channel[i].nb);
2534 }
2535
2536 me->client = msm_ion_client_create(DEVICE_NAME);
2537 VERIFY(err, !IS_ERR_OR_NULL(me->client));
2538 if (err)
2539 goto device_create_bail;
Sathish Ambley1ca68232017-01-19 10:32:55 -08002540 debugfs_root = debugfs_create_dir("adsprpc", NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002541 return 0;
2542device_create_bail:
2543 for (i = 0; i < NUM_CHANNELS; i++) {
2544 if (IS_ERR_OR_NULL(me->channel[i].dev))
2545 continue;
2546 device_destroy(me->class, MKDEV(MAJOR(me->dev_no), i));
2547 subsys_notif_unregister_notifier(me->channel[i].handle,
2548 &me->channel[i].nb);
2549 }
2550 class_destroy(me->class);
2551class_create_bail:
2552 cdev_del(&me->cdev);
2553cdev_init_bail:
2554 unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
2555alloc_chrdev_bail:
2556register_bail:
2557 fastrpc_deinit();
2558 return err;
2559}
2560
2561static void __exit fastrpc_device_exit(void)
2562{
2563 struct fastrpc_apps *me = &gfa;
2564 int i;
2565
2566 fastrpc_file_list_dtor(me);
2567 fastrpc_deinit();
2568 for (i = 0; i < NUM_CHANNELS; i++) {
2569 if (!gcinfo[i].name)
2570 continue;
2571 device_destroy(me->class, MKDEV(MAJOR(me->dev_no), i));
2572 subsys_notif_unregister_notifier(me->channel[i].handle,
2573 &me->channel[i].nb);
2574 }
2575 class_destroy(me->class);
2576 cdev_del(&me->cdev);
2577 unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
2578 ion_client_destroy(me->client);
Sathish Ambley1ca68232017-01-19 10:32:55 -08002579 debugfs_remove_recursive(debugfs_root);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002580}
2581
2582late_initcall(fastrpc_device_init);
2583module_exit(fastrpc_device_exit);
2584
2585MODULE_LICENSE("GPL v2");