blob: 177fb3d6ffe257b8627af88bfd24b237091d0f4d [file] [log] [blame]
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001/*
Tharun Kumar Merugubcd6fbf2018-01-04 17:49:34 +05302 * Copyright (c) 2012-2018, 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>
c_mtharue1a5ce12017-10-13 20:47:09 +053045#include <soc/qcom/scm.h>
Sathish Ambley69e1ab02016-10-18 10:28:15 -070046#include "adsprpc_compat.h"
47#include "adsprpc_shared.h"
c_mtharue1a5ce12017-10-13 20:47:09 +053048#include <soc/qcom/ramdump.h>
Sathish Ambley1ca68232017-01-19 10:32:55 -080049#include <linux/debugfs.h>
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +053050#include <linux/pm_qos.h>
Sathish Ambley69e1ab02016-10-18 10:28:15 -070051#define TZ_PIL_PROTECT_MEM_SUBSYS_ID 0x0C
52#define TZ_PIL_CLEAR_PROTECT_MEM_SUBSYS_ID 0x0D
53#define TZ_PIL_AUTH_QDSP6_PROC 1
c_mtharue1a5ce12017-10-13 20:47:09 +053054#define ADSP_MMAP_HEAP_ADDR 4
55#define ADSP_MMAP_REMOTE_HEAP_ADDR 8
Sathish Ambley69e1ab02016-10-18 10:28:15 -070056#define FASTRPC_ENOSUCH 39
57#define VMID_SSC_Q6 5
58#define VMID_ADSP_Q6 6
Sathish Ambley1ca68232017-01-19 10:32:55 -080059#define DEBUGFS_SIZE 1024
Sathish Ambley69e1ab02016-10-18 10:28:15 -070060
61#define RPC_TIMEOUT (5 * HZ)
62#define BALIGN 128
63#define NUM_CHANNELS 4 /* adsp, mdsp, slpi, cdsp*/
64#define NUM_SESSIONS 9 /*8 compute, 1 cpz*/
Sathish Ambleybae51902017-07-03 15:00:49 -070065#define M_FDLIST (16)
66#define M_CRCLIST (64)
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +053067#define SESSION_ID_INDEX (30)
c_mtharufdac6892017-10-12 13:09:01 +053068#define FASTRPC_CTX_MAGIC (0xbeeddeed)
Sathish Ambley69e1ab02016-10-18 10:28:15 -070069
70#define IS_CACHE_ALIGNED(x) (((x) & ((L1_CACHE_BYTES)-1)) == 0)
71
72#define FASTRPC_LINK_STATE_DOWN (0x0)
73#define FASTRPC_LINK_STATE_UP (0x1)
74#define FASTRPC_LINK_DISCONNECTED (0x0)
75#define FASTRPC_LINK_CONNECTING (0x1)
76#define FASTRPC_LINK_CONNECTED (0x3)
77#define FASTRPC_LINK_DISCONNECTING (0x7)
c_mtharu314a4202017-11-15 22:09:17 +053078#define FASTRPC_LINK_REMOTE_DISCONNECTING (0x8)
79#define FASTRPC_GLINK_INTENT_LEN (64)
Sathish Ambley69e1ab02016-10-18 10:28:15 -070080
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +053081#define PERF_KEYS \
82 "count:flush:map:copy:glink:getargs:putargs:invalidate:invoke:tid:ptr"
Sathish Ambleya21b5b52017-01-11 16:11:01 -080083#define FASTRPC_STATIC_HANDLE_LISTENER (3)
84#define FASTRPC_STATIC_HANDLE_MAX (20)
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +053085#define FASTRPC_LATENCY_CTRL_ENB (1)
Sathish Ambleya21b5b52017-01-11 16:11:01 -080086
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +053087#define INIT_FILELEN_MAX (2*1024*1024)
88#define INIT_MEMLEN_MAX (8*1024*1024)
89
Sathish Ambleya21b5b52017-01-11 16:11:01 -080090#define PERF_END (void)0
91
92#define PERF(enb, cnt, ff) \
93 {\
94 struct timespec startT = {0};\
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +053095 int64_t *counter = cnt;\
96 if (enb && counter) {\
Sathish Ambleya21b5b52017-01-11 16:11:01 -080097 getnstimeofday(&startT);\
98 } \
99 ff ;\
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530100 if (enb && counter) {\
101 *counter += getnstimediff(&startT);\
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800102 } \
103 }
104
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530105#define GET_COUNTER(perf_ptr, offset) \
106 (perf_ptr != NULL ?\
107 (((offset >= 0) && (offset < PERF_KEY_MAX)) ?\
108 (int64_t *)(perf_ptr + offset)\
109 : (int64_t *)NULL) : (int64_t *)NULL)
110
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700111static int fastrpc_glink_open(int cid);
112static void fastrpc_glink_close(void *chan, int cid);
Sathish Ambley1ca68232017-01-19 10:32:55 -0800113static struct dentry *debugfs_root;
114static struct dentry *debugfs_global_file;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700115
116static inline uint64_t buf_page_start(uint64_t buf)
117{
118 uint64_t start = (uint64_t) buf & PAGE_MASK;
119 return start;
120}
121
122static inline uint64_t buf_page_offset(uint64_t buf)
123{
124 uint64_t offset = (uint64_t) buf & (PAGE_SIZE - 1);
125 return offset;
126}
127
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530128static inline uint64_t buf_num_pages(uint64_t buf, size_t len)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700129{
130 uint64_t start = buf_page_start(buf) >> PAGE_SHIFT;
131 uint64_t end = (((uint64_t) buf + len - 1) & PAGE_MASK) >> PAGE_SHIFT;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530132 uint64_t nPages = end - start + 1;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700133 return nPages;
134}
135
136static inline uint64_t buf_page_size(uint32_t size)
137{
138 uint64_t sz = (size + (PAGE_SIZE - 1)) & PAGE_MASK;
139
140 return sz > PAGE_SIZE ? sz : PAGE_SIZE;
141}
142
143static inline void *uint64_to_ptr(uint64_t addr)
144{
145 void *ptr = (void *)((uintptr_t)addr);
146
147 return ptr;
148}
149
150static inline uint64_t ptr_to_uint64(void *ptr)
151{
152 uint64_t addr = (uint64_t)((uintptr_t)ptr);
153
154 return addr;
155}
156
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +0530157struct secure_vm {
158 int *vmid;
159 int *vmperm;
160 int vmcount;
161};
162
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700163struct fastrpc_file;
164
165struct fastrpc_buf {
166 struct hlist_node hn;
167 struct fastrpc_file *fl;
168 void *virt;
169 uint64_t phys;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530170 size_t size;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700171};
172
173struct fastrpc_ctx_lst;
174
175struct overlap {
176 uintptr_t start;
177 uintptr_t end;
178 int raix;
179 uintptr_t mstart;
180 uintptr_t mend;
181 uintptr_t offset;
182};
183
184struct smq_invoke_ctx {
185 struct hlist_node hn;
186 struct completion work;
187 int retval;
188 int pid;
189 int tgid;
190 remote_arg_t *lpra;
191 remote_arg64_t *rpra;
192 int *fds;
193 unsigned int *attrs;
194 struct fastrpc_mmap **maps;
195 struct fastrpc_buf *buf;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530196 size_t used;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700197 struct fastrpc_file *fl;
198 uint32_t sc;
199 struct overlap *overs;
200 struct overlap **overps;
201 struct smq_msg msg;
Sathish Ambleybae51902017-07-03 15:00:49 -0700202 uint32_t *crc;
c_mtharufdac6892017-10-12 13:09:01 +0530203 unsigned int magic;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700204};
205
206struct fastrpc_ctx_lst {
207 struct hlist_head pending;
208 struct hlist_head interrupted;
209};
210
211struct fastrpc_smmu {
c_mtharue1a5ce12017-10-13 20:47:09 +0530212 struct device *dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700213 struct dma_iommu_mapping *mapping;
214 int cb;
215 int enabled;
216 int faults;
217 int secure;
218 int coherent;
219};
220
221struct fastrpc_session_ctx {
222 struct device *dev;
223 struct fastrpc_smmu smmu;
224 int used;
225};
226
227struct fastrpc_glink_info {
228 int link_state;
229 int port_state;
230 struct glink_open_config cfg;
231 struct glink_link_info link_info;
232 void *link_notify_handle;
233};
234
235struct fastrpc_channel_ctx {
236 char *name;
237 char *subsys;
238 void *chan;
239 struct device *dev;
240 struct fastrpc_session_ctx session[NUM_SESSIONS];
241 struct completion work;
Tharun Kumar Merugu53a8ec92017-07-14 15:52:49 +0530242 struct completion workport;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700243 struct notifier_block nb;
244 struct kref kref;
245 int sesscount;
246 int ssrcount;
247 void *handle;
248 int prevssrcount;
c_mtharue1a5ce12017-10-13 20:47:09 +0530249 int issubsystemup;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700250 int vmid;
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +0530251 struct secure_vm rhvm;
c_mtharue1a5ce12017-10-13 20:47:09 +0530252 int ramdumpenabled;
253 void *remoteheap_ramdump_dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700254 struct fastrpc_glink_info link;
255};
256
257struct fastrpc_apps {
258 struct fastrpc_channel_ctx *channel;
259 struct cdev cdev;
260 struct class *class;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +0530261 struct mutex smd_mutex;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700262 struct smq_phy_page range;
263 struct hlist_head maps;
c_mtharue1a5ce12017-10-13 20:47:09 +0530264 uint32_t staticpd_flags;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700265 dev_t dev_no;
266 int compat;
267 struct hlist_head drivers;
268 spinlock_t hlock;
269 struct ion_client *client;
270 struct device *dev;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +0530271 unsigned int latency;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700272};
273
274struct fastrpc_mmap {
275 struct hlist_node hn;
276 struct fastrpc_file *fl;
277 struct fastrpc_apps *apps;
278 int fd;
279 uint32_t flags;
280 struct dma_buf *buf;
281 struct sg_table *table;
282 struct dma_buf_attachment *attach;
283 struct ion_handle *handle;
284 uint64_t phys;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530285 size_t size;
286 uintptr_t va;
287 size_t len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700288 int refs;
289 uintptr_t raddr;
290 int uncached;
291 int secure;
292 uintptr_t attr;
293};
294
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530295enum fastrpc_perfkeys {
296 PERF_COUNT = 0,
297 PERF_FLUSH = 1,
298 PERF_MAP = 2,
299 PERF_COPY = 3,
300 PERF_LINK = 4,
301 PERF_GETARGS = 5,
302 PERF_PUTARGS = 6,
303 PERF_INVARGS = 7,
304 PERF_INVOKE = 8,
305 PERF_KEY_MAX = 9,
306};
307
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800308struct fastrpc_perf {
309 int64_t count;
310 int64_t flush;
311 int64_t map;
312 int64_t copy;
313 int64_t link;
314 int64_t getargs;
315 int64_t putargs;
316 int64_t invargs;
317 int64_t invoke;
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530318 int64_t tid;
319 struct hlist_node hn;
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800320};
321
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700322struct fastrpc_file {
323 struct hlist_node hn;
324 spinlock_t hlock;
325 struct hlist_head maps;
326 struct hlist_head bufs;
327 struct fastrpc_ctx_lst clst;
328 struct fastrpc_session_ctx *sctx;
329 struct fastrpc_session_ctx *secsctx;
330 uint32_t mode;
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800331 uint32_t profile;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +0530332 int sessionid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700333 int tgid;
334 int cid;
335 int ssrcount;
336 int pd;
tharun kumar9f899ea2017-07-03 17:07:03 +0530337 int file_close;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700338 struct fastrpc_apps *apps;
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530339 struct hlist_head perf;
Sathish Ambley1ca68232017-01-19 10:32:55 -0800340 struct dentry *debugfs_file;
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530341 struct mutex perf_mutex;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +0530342 struct pm_qos_request pm_qos_req;
343 int qos_request;
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +0530344 struct mutex map_mutex;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700345};
346
347static struct fastrpc_apps gfa;
348
349static struct fastrpc_channel_ctx gcinfo[NUM_CHANNELS] = {
350 {
351 .name = "adsprpc-smd",
352 .subsys = "adsp",
353 .link.link_info.edge = "lpass",
354 .link.link_info.transport = "smem",
355 },
356 {
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700357 .name = "mdsprpc-smd",
358 .subsys = "modem",
359 .link.link_info.edge = "mpss",
360 .link.link_info.transport = "smem",
361 },
362 {
Sathish Ambley36849af2017-02-02 09:35:55 -0800363 .name = "sdsprpc-smd",
364 .subsys = "slpi",
365 .link.link_info.edge = "dsps",
366 .link.link_info.transport = "smem",
Sathish Ambley36849af2017-02-02 09:35:55 -0800367 },
368 {
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700369 .name = "cdsprpc-smd",
370 .subsys = "cdsp",
371 .link.link_info.edge = "cdsp",
372 .link.link_info.transport = "smem",
373 },
374};
375
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +0530376static int hlosvm[1] = {VMID_HLOS};
377static int hlosvmperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
378
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800379static inline int64_t getnstimediff(struct timespec *start)
380{
381 int64_t ns;
382 struct timespec ts, b;
383
384 getnstimeofday(&ts);
385 b = timespec_sub(ts, *start);
386 ns = timespec_to_ns(&b);
387 return ns;
388}
389
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530390static inline int64_t *getperfcounter(struct fastrpc_file *fl, int key)
391{
392 int err = 0;
393 int64_t *val = NULL;
394 struct fastrpc_perf *perf = NULL, *fperf = NULL;
395 struct hlist_node *n = NULL;
396
397 VERIFY(err, !IS_ERR_OR_NULL(fl));
398 if (err)
399 goto bail;
400
401 mutex_lock(&fl->perf_mutex);
402 hlist_for_each_entry_safe(perf, n, &fl->perf, hn) {
403 if (perf->tid == current->pid) {
404 fperf = perf;
405 break;
406 }
407 }
408
409 if (IS_ERR_OR_NULL(fperf)) {
410 fperf = kzalloc(sizeof(*fperf), GFP_KERNEL);
411
412 VERIFY(err, !IS_ERR_OR_NULL(fperf));
413 if (err) {
414 mutex_unlock(&fl->perf_mutex);
415 kfree(fperf);
416 goto bail;
417 }
418
419 fperf->tid = current->pid;
420 hlist_add_head(&fperf->hn, &fl->perf);
421 }
422
423 val = ((int64_t *)fperf) + key;
424 mutex_unlock(&fl->perf_mutex);
425bail:
426 return val;
427}
428
429
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700430static void fastrpc_buf_free(struct fastrpc_buf *buf, int cache)
431{
c_mtharue1a5ce12017-10-13 20:47:09 +0530432 struct fastrpc_file *fl = buf == NULL ? NULL : buf->fl;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700433 int vmid;
434
435 if (!fl)
436 return;
437 if (cache) {
438 spin_lock(&fl->hlock);
439 hlist_add_head(&buf->hn, &fl->bufs);
440 spin_unlock(&fl->hlock);
441 return;
442 }
443 if (!IS_ERR_OR_NULL(buf->virt)) {
444 int destVM[1] = {VMID_HLOS};
445 int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
446
447 if (fl->sctx->smmu.cb)
448 buf->phys &= ~((uint64_t)fl->sctx->smmu.cb << 32);
449 vmid = fl->apps->channel[fl->cid].vmid;
450 if (vmid) {
451 int srcVM[2] = {VMID_HLOS, vmid};
452
453 hyp_assign_phys(buf->phys, buf_page_size(buf->size),
454 srcVM, 2, destVM, destVMperm, 1);
455 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530456 dma_free_coherent(fl->sctx->smmu.dev, buf->size, buf->virt,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700457 buf->phys);
458 }
459 kfree(buf);
460}
461
462static void fastrpc_buf_list_free(struct fastrpc_file *fl)
463{
464 struct fastrpc_buf *buf, *free;
465
466 do {
467 struct hlist_node *n;
468
c_mtharue1a5ce12017-10-13 20:47:09 +0530469 free = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700470 spin_lock(&fl->hlock);
471 hlist_for_each_entry_safe(buf, n, &fl->bufs, hn) {
472 hlist_del_init(&buf->hn);
473 free = buf;
474 break;
475 }
476 spin_unlock(&fl->hlock);
477 if (free)
478 fastrpc_buf_free(free, 0);
479 } while (free);
480}
481
482static void fastrpc_mmap_add(struct fastrpc_mmap *map)
483{
c_mtharue1a5ce12017-10-13 20:47:09 +0530484 if (map->flags == ADSP_MMAP_HEAP_ADDR ||
485 map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
486 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700487
c_mtharue1a5ce12017-10-13 20:47:09 +0530488 spin_lock(&me->hlock);
489 hlist_add_head(&map->hn, &me->maps);
490 spin_unlock(&me->hlock);
491 } else {
492 struct fastrpc_file *fl = map->fl;
493
494 spin_lock(&fl->hlock);
495 hlist_add_head(&map->hn, &fl->maps);
496 spin_unlock(&fl->hlock);
497 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700498}
499
c_mtharue1a5ce12017-10-13 20:47:09 +0530500static int fastrpc_mmap_find(struct fastrpc_file *fl, int fd,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530501 uintptr_t va, size_t len, int mflags, int refs,
c_mtharue1a5ce12017-10-13 20:47:09 +0530502 struct fastrpc_mmap **ppmap)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700503{
c_mtharue1a5ce12017-10-13 20:47:09 +0530504 struct fastrpc_apps *me = &gfa;
505 struct fastrpc_mmap *match = NULL, *map = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700506 struct hlist_node *n;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530507
508 if ((va + len) < va)
509 return -EOVERFLOW;
c_mtharue1a5ce12017-10-13 20:47:09 +0530510 if (mflags == ADSP_MMAP_HEAP_ADDR ||
511 mflags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
512 spin_lock(&me->hlock);
513 hlist_for_each_entry_safe(map, n, &me->maps, hn) {
514 if (va >= map->va &&
515 va + len <= map->va + map->len &&
516 map->fd == fd) {
517 if (refs)
518 map->refs++;
519 match = map;
520 break;
521 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700522 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530523 spin_unlock(&me->hlock);
524 } else {
525 spin_lock(&fl->hlock);
526 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
527 if (va >= map->va &&
528 va + len <= map->va + map->len &&
529 map->fd == fd) {
530 if (refs)
531 map->refs++;
532 match = map;
533 break;
534 }
535 }
536 spin_unlock(&fl->hlock);
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700537 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700538 if (match) {
539 *ppmap = match;
540 return 0;
541 }
542 return -ENOTTY;
543}
544
c_mtharuf931ff92017-11-30 19:35:30 +0530545static int dma_alloc_memory(dma_addr_t *region_phys, void **vaddr, size_t size)
c_mtharue1a5ce12017-10-13 20:47:09 +0530546{
547 struct fastrpc_apps *me = &gfa;
c_mtharue1a5ce12017-10-13 20:47:09 +0530548
549 if (me->dev == NULL) {
550 pr_err("device adsprpc-mem is not initialized\n");
551 return -ENODEV;
552 }
c_mtharuf931ff92017-11-30 19:35:30 +0530553 *vaddr = dma_alloc_coherent(me->dev, size, region_phys, GFP_KERNEL);
554 if (!*vaddr) {
c_mtharue1a5ce12017-10-13 20:47:09 +0530555 pr_err("ADSPRPC: Failed to allocate %x remote heap memory\n",
556 (unsigned int)size);
557 return -ENOMEM;
558 }
559 return 0;
560}
561
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700562static int fastrpc_mmap_remove(struct fastrpc_file *fl, uintptr_t va,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530563 size_t len, struct fastrpc_mmap **ppmap)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700564{
c_mtharue1a5ce12017-10-13 20:47:09 +0530565 struct fastrpc_mmap *match = NULL, *map;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700566 struct hlist_node *n;
567 struct fastrpc_apps *me = &gfa;
568
569 spin_lock(&me->hlock);
570 hlist_for_each_entry_safe(map, n, &me->maps, hn) {
571 if (map->raddr == va &&
572 map->raddr + map->len == va + len &&
573 map->refs == 1) {
574 match = map;
575 hlist_del_init(&map->hn);
576 break;
577 }
578 }
579 spin_unlock(&me->hlock);
580 if (match) {
581 *ppmap = match;
582 return 0;
583 }
584 spin_lock(&fl->hlock);
585 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
586 if (map->raddr == va &&
587 map->raddr + map->len == va + len &&
588 map->refs == 1) {
589 match = map;
590 hlist_del_init(&map->hn);
591 break;
592 }
593 }
594 spin_unlock(&fl->hlock);
595 if (match) {
596 *ppmap = match;
597 return 0;
598 }
599 return -ENOTTY;
600}
601
c_mtharu7bd6a422017-10-17 18:15:37 +0530602static void fastrpc_mmap_free(struct fastrpc_mmap *map, uint32_t flags)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700603{
c_mtharue1a5ce12017-10-13 20:47:09 +0530604 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700605 struct fastrpc_file *fl;
606 int vmid;
607 struct fastrpc_session_ctx *sess;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700608
609 if (!map)
610 return;
611 fl = map->fl;
c_mtharue1a5ce12017-10-13 20:47:09 +0530612 if (map->flags == ADSP_MMAP_HEAP_ADDR ||
613 map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
614 spin_lock(&me->hlock);
615 map->refs--;
616 if (!map->refs)
617 hlist_del_init(&map->hn);
618 spin_unlock(&me->hlock);
c_mtharu7bd6a422017-10-17 18:15:37 +0530619 if (map->refs > 0)
620 return;
c_mtharue1a5ce12017-10-13 20:47:09 +0530621 } else {
622 spin_lock(&fl->hlock);
623 map->refs--;
624 if (!map->refs)
625 hlist_del_init(&map->hn);
626 spin_unlock(&fl->hlock);
c_mtharu7bd6a422017-10-17 18:15:37 +0530627 if (map->refs > 0 && !flags)
628 return;
c_mtharue1a5ce12017-10-13 20:47:09 +0530629 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530630 if (map->flags == ADSP_MMAP_HEAP_ADDR ||
631 map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700632
c_mtharue1a5ce12017-10-13 20:47:09 +0530633 if (me->dev == NULL) {
634 pr_err("failed to free remote heap allocation\n");
635 return;
636 }
637 if (map->phys) {
638 dma_free_coherent(me->dev, map->size,
c_mtharuf931ff92017-11-30 19:35:30 +0530639 (void *)map->va, (dma_addr_t)map->phys);
c_mtharue1a5ce12017-10-13 20:47:09 +0530640 }
641 } else {
642 int destVM[1] = {VMID_HLOS};
643 int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
644
645 if (map->secure)
646 sess = fl->secsctx;
647 else
648 sess = fl->sctx;
649
650 if (!IS_ERR_OR_NULL(map->handle))
651 ion_free(fl->apps->client, map->handle);
652 if (sess && sess->smmu.enabled) {
653 if (map->size || map->phys)
654 msm_dma_unmap_sg(sess->smmu.dev,
655 map->table->sgl,
656 map->table->nents, DMA_BIDIRECTIONAL,
657 map->buf);
658 }
659 vmid = fl->apps->channel[fl->cid].vmid;
660 if (vmid && map->phys) {
661 int srcVM[2] = {VMID_HLOS, vmid};
662
663 hyp_assign_phys(map->phys, buf_page_size(map->size),
664 srcVM, 2, destVM, destVMperm, 1);
665 }
666
667 if (!IS_ERR_OR_NULL(map->table))
668 dma_buf_unmap_attachment(map->attach, map->table,
669 DMA_BIDIRECTIONAL);
670 if (!IS_ERR_OR_NULL(map->attach))
671 dma_buf_detach(map->buf, map->attach);
672 if (!IS_ERR_OR_NULL(map->buf))
673 dma_buf_put(map->buf);
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700674 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700675 kfree(map);
676}
677
678static int fastrpc_session_alloc(struct fastrpc_channel_ctx *chan, int secure,
679 struct fastrpc_session_ctx **session);
680
681static int fastrpc_mmap_create(struct fastrpc_file *fl, int fd,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530682 unsigned int attr, uintptr_t va, size_t len, int mflags,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700683 struct fastrpc_mmap **ppmap)
684{
c_mtharue1a5ce12017-10-13 20:47:09 +0530685 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700686 struct fastrpc_session_ctx *sess;
687 struct fastrpc_apps *apps = fl->apps;
688 int cid = fl->cid;
689 struct fastrpc_channel_ctx *chan = &apps->channel[cid];
c_mtharue1a5ce12017-10-13 20:47:09 +0530690 struct fastrpc_mmap *map = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700691 unsigned long attrs;
c_mtharuf931ff92017-11-30 19:35:30 +0530692 dma_addr_t region_phys = 0;
693 void *region_vaddr = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700694 unsigned long flags;
695 int err = 0, vmid;
696
Sathish Ambleyae5ee542017-01-16 22:24:23 -0800697 if (!fastrpc_mmap_find(fl, fd, va, len, mflags, 1, ppmap))
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700698 return 0;
699 map = kzalloc(sizeof(*map), GFP_KERNEL);
700 VERIFY(err, !IS_ERR_OR_NULL(map));
701 if (err)
702 goto bail;
703 INIT_HLIST_NODE(&map->hn);
704 map->flags = mflags;
705 map->refs = 1;
706 map->fl = fl;
707 map->fd = fd;
708 map->attr = attr;
c_mtharue1a5ce12017-10-13 20:47:09 +0530709 if (mflags == ADSP_MMAP_HEAP_ADDR ||
710 mflags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
711 map->apps = me;
712 map->fl = NULL;
c_mtharuf931ff92017-11-30 19:35:30 +0530713 VERIFY(err, !dma_alloc_memory(&region_phys, &region_vaddr,
714 len));
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700715 if (err)
716 goto bail;
c_mtharuf931ff92017-11-30 19:35:30 +0530717 map->phys = (uintptr_t)region_phys;
c_mtharue1a5ce12017-10-13 20:47:09 +0530718 map->size = len;
c_mtharuf931ff92017-11-30 19:35:30 +0530719 map->va = (uintptr_t)region_vaddr;
c_mtharue1a5ce12017-10-13 20:47:09 +0530720 } else {
c_mtharu7bd6a422017-10-17 18:15:37 +0530721 if (map->attr && (map->attr & FASTRPC_ATTR_KEEP_MAP)) {
722 pr_info("adsprpc: buffer mapped with persist attr %x\n",
723 (unsigned int)map->attr);
724 map->refs = 2;
725 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530726 VERIFY(err, !IS_ERR_OR_NULL(map->handle =
727 ion_import_dma_buf_fd(fl->apps->client, fd)));
728 if (err)
729 goto bail;
730 VERIFY(err, !ion_handle_get_flags(fl->apps->client, map->handle,
731 &flags));
732 if (err)
733 goto bail;
734
c_mtharue1a5ce12017-10-13 20:47:09 +0530735 map->secure = flags & ION_FLAG_SECURE;
736 if (map->secure) {
737 if (!fl->secsctx)
738 err = fastrpc_session_alloc(chan, 1,
739 &fl->secsctx);
740 if (err)
741 goto bail;
742 }
743 if (map->secure)
744 sess = fl->secsctx;
745 else
746 sess = fl->sctx;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +0530747
c_mtharue1a5ce12017-10-13 20:47:09 +0530748 VERIFY(err, !IS_ERR_OR_NULL(sess));
749 if (err)
750 goto bail;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +0530751
752 map->uncached = !ION_IS_CACHED(flags);
753 if (map->attr & FASTRPC_ATTR_NOVA && !sess->smmu.coherent)
754 map->uncached = 1;
755
c_mtharue1a5ce12017-10-13 20:47:09 +0530756 VERIFY(err, !IS_ERR_OR_NULL(map->buf = dma_buf_get(fd)));
757 if (err)
758 goto bail;
759 VERIFY(err, !IS_ERR_OR_NULL(map->attach =
760 dma_buf_attach(map->buf, sess->smmu.dev)));
761 if (err)
762 goto bail;
763 VERIFY(err, !IS_ERR_OR_NULL(map->table =
764 dma_buf_map_attachment(map->attach,
765 DMA_BIDIRECTIONAL)));
766 if (err)
767 goto bail;
768 if (sess->smmu.enabled) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700769 attrs = DMA_ATTR_EXEC_MAPPING;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +0530770
771 if (map->attr & FASTRPC_ATTR_NON_COHERENT ||
772 (sess->smmu.coherent && map->uncached))
773 attrs |= DMA_ATTR_FORCE_NON_COHERENT;
774 else if (map->attr & FASTRPC_ATTR_COHERENT)
775 attrs |= DMA_ATTR_FORCE_COHERENT;
776
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700777 VERIFY(err, map->table->nents ==
c_mtharue1a5ce12017-10-13 20:47:09 +0530778 msm_dma_map_sg_attrs(sess->smmu.dev,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700779 map->table->sgl, map->table->nents,
780 DMA_BIDIRECTIONAL, map->buf, attrs));
c_mtharue1a5ce12017-10-13 20:47:09 +0530781 if (err)
782 goto bail;
783 } else {
784 VERIFY(err, map->table->nents == 1);
785 if (err)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700786 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +0530787 }
788 map->phys = sg_dma_address(map->table->sgl);
789 if (sess->smmu.cb) {
790 map->phys += ((uint64_t)sess->smmu.cb << 32);
791 map->size = sg_dma_len(map->table->sgl);
792 } else {
793 map->size = buf_page_size(len);
794 }
795 vmid = fl->apps->channel[fl->cid].vmid;
796 if (vmid) {
797 int srcVM[1] = {VMID_HLOS};
798 int destVM[2] = {VMID_HLOS, vmid};
799 int destVMperm[2] = {PERM_READ | PERM_WRITE,
800 PERM_READ | PERM_WRITE | PERM_EXEC};
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700801
c_mtharue1a5ce12017-10-13 20:47:09 +0530802 VERIFY(err, !hyp_assign_phys(map->phys,
803 buf_page_size(map->size),
804 srcVM, 1, destVM, destVMperm, 2));
805 if (err)
806 goto bail;
807 }
808 map->va = va;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700809 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700810 map->len = len;
811
812 fastrpc_mmap_add(map);
813 *ppmap = map;
814
815bail:
816 if (err && map)
c_mtharu7bd6a422017-10-17 18:15:37 +0530817 fastrpc_mmap_free(map, 0);
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700818 return err;
819}
820
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530821static int fastrpc_buf_alloc(struct fastrpc_file *fl, size_t size,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700822 struct fastrpc_buf **obuf)
823{
824 int err = 0, vmid;
c_mtharue1a5ce12017-10-13 20:47:09 +0530825 struct fastrpc_buf *buf = NULL, *fr = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700826 struct hlist_node *n;
827
828 VERIFY(err, size > 0);
829 if (err)
830 goto bail;
831
832 /* find the smallest buffer that fits in the cache */
833 spin_lock(&fl->hlock);
834 hlist_for_each_entry_safe(buf, n, &fl->bufs, hn) {
835 if (buf->size >= size && (!fr || fr->size > buf->size))
836 fr = buf;
837 }
838 if (fr)
839 hlist_del_init(&fr->hn);
840 spin_unlock(&fl->hlock);
841 if (fr) {
842 *obuf = fr;
843 return 0;
844 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530845 buf = NULL;
846 VERIFY(err, NULL != (buf = kzalloc(sizeof(*buf), GFP_KERNEL)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700847 if (err)
848 goto bail;
849 INIT_HLIST_NODE(&buf->hn);
850 buf->fl = fl;
c_mtharue1a5ce12017-10-13 20:47:09 +0530851 buf->virt = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700852 buf->phys = 0;
853 buf->size = size;
c_mtharue1a5ce12017-10-13 20:47:09 +0530854 buf->virt = dma_alloc_coherent(fl->sctx->smmu.dev, buf->size,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700855 (void *)&buf->phys, GFP_KERNEL);
856 if (IS_ERR_OR_NULL(buf->virt)) {
857 /* free cache and retry */
858 fastrpc_buf_list_free(fl);
c_mtharue1a5ce12017-10-13 20:47:09 +0530859 buf->virt = dma_alloc_coherent(fl->sctx->smmu.dev, buf->size,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700860 (void *)&buf->phys, GFP_KERNEL);
861 VERIFY(err, !IS_ERR_OR_NULL(buf->virt));
862 }
863 if (err)
864 goto bail;
865 if (fl->sctx->smmu.cb)
866 buf->phys += ((uint64_t)fl->sctx->smmu.cb << 32);
867 vmid = fl->apps->channel[fl->cid].vmid;
868 if (vmid) {
869 int srcVM[1] = {VMID_HLOS};
870 int destVM[2] = {VMID_HLOS, vmid};
871 int destVMperm[2] = {PERM_READ | PERM_WRITE,
872 PERM_READ | PERM_WRITE | PERM_EXEC};
873
874 VERIFY(err, !hyp_assign_phys(buf->phys, buf_page_size(size),
875 srcVM, 1, destVM, destVMperm, 2));
876 if (err)
877 goto bail;
878 }
879
880 *obuf = buf;
881 bail:
882 if (err && buf)
883 fastrpc_buf_free(buf, 0);
884 return err;
885}
886
887
888static int context_restore_interrupted(struct fastrpc_file *fl,
Sathish Ambleybae51902017-07-03 15:00:49 -0700889 struct fastrpc_ioctl_invoke_crc *inv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700890 struct smq_invoke_ctx **po)
891{
892 int err = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +0530893 struct smq_invoke_ctx *ctx = NULL, *ictx = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700894 struct hlist_node *n;
895 struct fastrpc_ioctl_invoke *invoke = &inv->inv;
896
897 spin_lock(&fl->hlock);
898 hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) {
899 if (ictx->pid == current->pid) {
900 if (invoke->sc != ictx->sc || ictx->fl != fl)
901 err = -1;
902 else {
903 ctx = ictx;
904 hlist_del_init(&ctx->hn);
905 hlist_add_head(&ctx->hn, &fl->clst.pending);
906 }
907 break;
908 }
909 }
910 spin_unlock(&fl->hlock);
911 if (ctx)
912 *po = ctx;
913 return err;
914}
915
916#define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)
917static int overlap_ptr_cmp(const void *a, const void *b)
918{
919 struct overlap *pa = *((struct overlap **)a);
920 struct overlap *pb = *((struct overlap **)b);
921 /* sort with lowest starting buffer first */
922 int st = CMP(pa->start, pb->start);
923 /* sort with highest ending buffer first */
924 int ed = CMP(pb->end, pa->end);
925 return st == 0 ? ed : st;
926}
927
Sathish Ambley9466d672017-01-25 10:51:55 -0800928static int context_build_overlap(struct smq_invoke_ctx *ctx)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700929{
Sathish Ambley9466d672017-01-25 10:51:55 -0800930 int i, err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700931 remote_arg_t *lpra = ctx->lpra;
932 int inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
933 int outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc);
934 int nbufs = inbufs + outbufs;
935 struct overlap max;
936
937 for (i = 0; i < nbufs; ++i) {
938 ctx->overs[i].start = (uintptr_t)lpra[i].buf.pv;
939 ctx->overs[i].end = ctx->overs[i].start + lpra[i].buf.len;
Sathish Ambley9466d672017-01-25 10:51:55 -0800940 if (lpra[i].buf.len) {
941 VERIFY(err, ctx->overs[i].end > ctx->overs[i].start);
942 if (err)
943 goto bail;
944 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700945 ctx->overs[i].raix = i;
946 ctx->overps[i] = &ctx->overs[i];
947 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530948 sort(ctx->overps, nbufs, sizeof(*ctx->overps), overlap_ptr_cmp, NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700949 max.start = 0;
950 max.end = 0;
951 for (i = 0; i < nbufs; ++i) {
952 if (ctx->overps[i]->start < max.end) {
953 ctx->overps[i]->mstart = max.end;
954 ctx->overps[i]->mend = ctx->overps[i]->end;
955 ctx->overps[i]->offset = max.end -
956 ctx->overps[i]->start;
957 if (ctx->overps[i]->end > max.end) {
958 max.end = ctx->overps[i]->end;
959 } else {
960 ctx->overps[i]->mend = 0;
961 ctx->overps[i]->mstart = 0;
962 }
963 } else {
964 ctx->overps[i]->mend = ctx->overps[i]->end;
965 ctx->overps[i]->mstart = ctx->overps[i]->start;
966 ctx->overps[i]->offset = 0;
967 max = *ctx->overps[i];
968 }
969 }
Sathish Ambley9466d672017-01-25 10:51:55 -0800970bail:
971 return err;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700972}
973
974#define K_COPY_FROM_USER(err, kernel, dst, src, size) \
975 do {\
976 if (!(kernel))\
c_mtharue1a5ce12017-10-13 20:47:09 +0530977 VERIFY(err, 0 == copy_from_user((dst),\
978 (void const __user *)(src),\
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700979 (size)));\
980 else\
981 memmove((dst), (src), (size));\
982 } while (0)
983
984#define K_COPY_TO_USER(err, kernel, dst, src, size) \
985 do {\
986 if (!(kernel))\
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530987 VERIFY(err, 0 == copy_to_user((void __user *)(dst),\
c_mtharue1a5ce12017-10-13 20:47:09 +0530988 (src), (size)));\
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700989 else\
990 memmove((dst), (src), (size));\
991 } while (0)
992
993
994static void context_free(struct smq_invoke_ctx *ctx);
995
996static int context_alloc(struct fastrpc_file *fl, uint32_t kernel,
Sathish Ambleybae51902017-07-03 15:00:49 -0700997 struct fastrpc_ioctl_invoke_crc *invokefd,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700998 struct smq_invoke_ctx **po)
999{
1000 int err = 0, bufs, size = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05301001 struct smq_invoke_ctx *ctx = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001002 struct fastrpc_ctx_lst *clst = &fl->clst;
1003 struct fastrpc_ioctl_invoke *invoke = &invokefd->inv;
1004
1005 bufs = REMOTE_SCALARS_LENGTH(invoke->sc);
1006 size = bufs * sizeof(*ctx->lpra) + bufs * sizeof(*ctx->maps) +
1007 sizeof(*ctx->fds) * (bufs) +
1008 sizeof(*ctx->attrs) * (bufs) +
1009 sizeof(*ctx->overs) * (bufs) +
1010 sizeof(*ctx->overps) * (bufs);
1011
c_mtharue1a5ce12017-10-13 20:47:09 +05301012 VERIFY(err, NULL != (ctx = kzalloc(sizeof(*ctx) + size, GFP_KERNEL)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001013 if (err)
1014 goto bail;
1015
1016 INIT_HLIST_NODE(&ctx->hn);
1017 hlist_add_fake(&ctx->hn);
1018 ctx->fl = fl;
1019 ctx->maps = (struct fastrpc_mmap **)(&ctx[1]);
1020 ctx->lpra = (remote_arg_t *)(&ctx->maps[bufs]);
1021 ctx->fds = (int *)(&ctx->lpra[bufs]);
1022 ctx->attrs = (unsigned int *)(&ctx->fds[bufs]);
1023 ctx->overs = (struct overlap *)(&ctx->attrs[bufs]);
1024 ctx->overps = (struct overlap **)(&ctx->overs[bufs]);
1025
c_mtharue1a5ce12017-10-13 20:47:09 +05301026 K_COPY_FROM_USER(err, kernel, (void *)ctx->lpra, invoke->pra,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001027 bufs * sizeof(*ctx->lpra));
1028 if (err)
1029 goto bail;
1030
1031 if (invokefd->fds) {
1032 K_COPY_FROM_USER(err, kernel, ctx->fds, invokefd->fds,
1033 bufs * sizeof(*ctx->fds));
1034 if (err)
1035 goto bail;
1036 }
1037 if (invokefd->attrs) {
1038 K_COPY_FROM_USER(err, kernel, ctx->attrs, invokefd->attrs,
1039 bufs * sizeof(*ctx->attrs));
1040 if (err)
1041 goto bail;
1042 }
Sathish Ambleybae51902017-07-03 15:00:49 -07001043 ctx->crc = (uint32_t *)invokefd->crc;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001044 ctx->sc = invoke->sc;
Sathish Ambley9466d672017-01-25 10:51:55 -08001045 if (bufs) {
1046 VERIFY(err, 0 == context_build_overlap(ctx));
1047 if (err)
1048 goto bail;
1049 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001050 ctx->retval = -1;
1051 ctx->pid = current->pid;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301052 ctx->tgid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001053 init_completion(&ctx->work);
c_mtharufdac6892017-10-12 13:09:01 +05301054 ctx->magic = FASTRPC_CTX_MAGIC;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001055
1056 spin_lock(&fl->hlock);
1057 hlist_add_head(&ctx->hn, &clst->pending);
1058 spin_unlock(&fl->hlock);
1059
1060 *po = ctx;
1061bail:
1062 if (ctx && err)
1063 context_free(ctx);
1064 return err;
1065}
1066
1067static void context_save_interrupted(struct smq_invoke_ctx *ctx)
1068{
1069 struct fastrpc_ctx_lst *clst = &ctx->fl->clst;
1070
1071 spin_lock(&ctx->fl->hlock);
1072 hlist_del_init(&ctx->hn);
1073 hlist_add_head(&ctx->hn, &clst->interrupted);
1074 spin_unlock(&ctx->fl->hlock);
1075 /* free the cache on power collapse */
1076 fastrpc_buf_list_free(ctx->fl);
1077}
1078
1079static void context_free(struct smq_invoke_ctx *ctx)
1080{
1081 int i;
1082 int nbufs = REMOTE_SCALARS_INBUFS(ctx->sc) +
1083 REMOTE_SCALARS_OUTBUFS(ctx->sc);
1084 spin_lock(&ctx->fl->hlock);
1085 hlist_del_init(&ctx->hn);
1086 spin_unlock(&ctx->fl->hlock);
1087 for (i = 0; i < nbufs; ++i)
c_mtharu7bd6a422017-10-17 18:15:37 +05301088 fastrpc_mmap_free(ctx->maps[i], 0);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001089 fastrpc_buf_free(ctx->buf, 1);
c_mtharufdac6892017-10-12 13:09:01 +05301090 ctx->magic = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001091 kfree(ctx);
1092}
1093
1094static void context_notify_user(struct smq_invoke_ctx *ctx, int retval)
1095{
1096 ctx->retval = retval;
1097 complete(&ctx->work);
1098}
1099
1100
1101static void fastrpc_notify_users(struct fastrpc_file *me)
1102{
1103 struct smq_invoke_ctx *ictx;
1104 struct hlist_node *n;
1105
1106 spin_lock(&me->hlock);
1107 hlist_for_each_entry_safe(ictx, n, &me->clst.pending, hn) {
1108 complete(&ictx->work);
1109 }
1110 hlist_for_each_entry_safe(ictx, n, &me->clst.interrupted, hn) {
1111 complete(&ictx->work);
1112 }
1113 spin_unlock(&me->hlock);
1114
1115}
1116
1117static void fastrpc_notify_drivers(struct fastrpc_apps *me, int cid)
1118{
1119 struct fastrpc_file *fl;
1120 struct hlist_node *n;
1121
1122 spin_lock(&me->hlock);
1123 hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
1124 if (fl->cid == cid)
1125 fastrpc_notify_users(fl);
1126 }
1127 spin_unlock(&me->hlock);
1128
1129}
1130static void context_list_ctor(struct fastrpc_ctx_lst *me)
1131{
1132 INIT_HLIST_HEAD(&me->interrupted);
1133 INIT_HLIST_HEAD(&me->pending);
1134}
1135
1136static void fastrpc_context_list_dtor(struct fastrpc_file *fl)
1137{
1138 struct fastrpc_ctx_lst *clst = &fl->clst;
c_mtharue1a5ce12017-10-13 20:47:09 +05301139 struct smq_invoke_ctx *ictx = NULL, *ctxfree;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001140 struct hlist_node *n;
1141
1142 do {
c_mtharue1a5ce12017-10-13 20:47:09 +05301143 ctxfree = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001144 spin_lock(&fl->hlock);
1145 hlist_for_each_entry_safe(ictx, n, &clst->interrupted, hn) {
1146 hlist_del_init(&ictx->hn);
1147 ctxfree = ictx;
1148 break;
1149 }
1150 spin_unlock(&fl->hlock);
1151 if (ctxfree)
1152 context_free(ctxfree);
1153 } while (ctxfree);
1154 do {
c_mtharue1a5ce12017-10-13 20:47:09 +05301155 ctxfree = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001156 spin_lock(&fl->hlock);
1157 hlist_for_each_entry_safe(ictx, n, &clst->pending, hn) {
1158 hlist_del_init(&ictx->hn);
1159 ctxfree = ictx;
1160 break;
1161 }
1162 spin_unlock(&fl->hlock);
1163 if (ctxfree)
1164 context_free(ctxfree);
1165 } while (ctxfree);
1166}
1167
1168static int fastrpc_file_free(struct fastrpc_file *fl);
1169static void fastrpc_file_list_dtor(struct fastrpc_apps *me)
1170{
1171 struct fastrpc_file *fl, *free;
1172 struct hlist_node *n;
1173
1174 do {
c_mtharue1a5ce12017-10-13 20:47:09 +05301175 free = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001176 spin_lock(&me->hlock);
1177 hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
1178 hlist_del_init(&fl->hn);
1179 free = fl;
1180 break;
1181 }
1182 spin_unlock(&me->hlock);
1183 if (free)
1184 fastrpc_file_free(free);
1185 } while (free);
1186}
1187
1188static int get_args(uint32_t kernel, struct smq_invoke_ctx *ctx)
1189{
1190 remote_arg64_t *rpra;
1191 remote_arg_t *lpra = ctx->lpra;
1192 struct smq_invoke_buf *list;
1193 struct smq_phy_page *pages, *ipage;
1194 uint32_t sc = ctx->sc;
1195 int inbufs = REMOTE_SCALARS_INBUFS(sc);
1196 int outbufs = REMOTE_SCALARS_OUTBUFS(sc);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001197 int handles, bufs = inbufs + outbufs;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001198 uintptr_t args;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301199 size_t rlen = 0, copylen = 0, metalen = 0;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001200 int i, oix;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001201 int err = 0;
1202 int mflags = 0;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001203 uint64_t *fdlist;
Sathish Ambleybae51902017-07-03 15:00:49 -07001204 uint32_t *crclist;
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301205 int64_t *perf_counter = getperfcounter(ctx->fl, PERF_COUNT);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001206
1207 /* calculate size of the metadata */
c_mtharue1a5ce12017-10-13 20:47:09 +05301208 rpra = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001209 list = smq_invoke_buf_start(rpra, sc);
1210 pages = smq_phy_page_start(sc, list);
1211 ipage = pages;
1212
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301213 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_MAP),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001214 for (i = 0; i < bufs; ++i) {
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301215 uintptr_t buf = (uintptr_t)lpra[i].buf.pv;
1216 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001217
1218 if (ctx->fds[i] && (ctx->fds[i] != -1))
1219 fastrpc_mmap_create(ctx->fl, ctx->fds[i],
1220 ctx->attrs[i], buf, len,
1221 mflags, &ctx->maps[i]);
1222 ipage += 1;
1223 }
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301224 PERF_END);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001225 handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc);
1226 for (i = bufs; i < bufs + handles; i++) {
1227 VERIFY(err, !fastrpc_mmap_create(ctx->fl, ctx->fds[i],
1228 FASTRPC_ATTR_NOVA, 0, 0, 0, &ctx->maps[i]));
1229 if (err)
1230 goto bail;
1231 ipage += 1;
1232 }
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301233 metalen = copylen = (size_t)&ipage[0] + (sizeof(uint64_t) * M_FDLIST) +
Sathish Ambleybae51902017-07-03 15:00:49 -07001234 (sizeof(uint32_t) * M_CRCLIST);
1235
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001236 /* calculate len requreed for copying */
1237 for (oix = 0; oix < inbufs + outbufs; ++oix) {
1238 int i = ctx->overps[oix]->raix;
Sathish Ambleyd209c1e2016-12-13 15:27:30 -08001239 uintptr_t mstart, mend;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301240 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001241
1242 if (!len)
1243 continue;
1244 if (ctx->maps[i])
1245 continue;
1246 if (ctx->overps[oix]->offset == 0)
1247 copylen = ALIGN(copylen, BALIGN);
Sathish Ambleyd209c1e2016-12-13 15:27:30 -08001248 mstart = ctx->overps[oix]->mstart;
1249 mend = ctx->overps[oix]->mend;
1250 VERIFY(err, (mend - mstart) <= LONG_MAX);
1251 if (err)
1252 goto bail;
1253 copylen += mend - mstart;
1254 VERIFY(err, copylen >= 0);
1255 if (err)
1256 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001257 }
1258 ctx->used = copylen;
1259
1260 /* allocate new buffer */
1261 if (copylen) {
1262 VERIFY(err, !fastrpc_buf_alloc(ctx->fl, copylen, &ctx->buf));
1263 if (err)
1264 goto bail;
1265 }
Tharun Kumar Merugue3361f92017-06-22 10:45:43 +05301266 if (ctx->buf->virt && metalen <= copylen)
1267 memset(ctx->buf->virt, 0, metalen);
1268
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001269 /* copy metadata */
1270 rpra = ctx->buf->virt;
1271 ctx->rpra = rpra;
1272 list = smq_invoke_buf_start(rpra, sc);
1273 pages = smq_phy_page_start(sc, list);
1274 ipage = pages;
1275 args = (uintptr_t)ctx->buf->virt + metalen;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001276 for (i = 0; i < bufs + handles; ++i) {
1277 if (lpra[i].buf.len)
1278 list[i].num = 1;
1279 else
1280 list[i].num = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001281 list[i].pgidx = ipage - pages;
1282 ipage++;
1283 }
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05301284
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001285 /* map ion buffers */
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301286 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_MAP),
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05301287 for (i = 0; rpra && i < inbufs + outbufs; ++i) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001288 struct fastrpc_mmap *map = ctx->maps[i];
1289 uint64_t buf = ptr_to_uint64(lpra[i].buf.pv);
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301290 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001291
1292 rpra[i].buf.pv = 0;
1293 rpra[i].buf.len = len;
1294 if (!len)
1295 continue;
1296 if (map) {
1297 struct vm_area_struct *vma;
1298 uintptr_t offset;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301299 uint64_t num = buf_num_pages(buf, len);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001300 int idx = list[i].pgidx;
1301
1302 if (map->attr & FASTRPC_ATTR_NOVA) {
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001303 offset = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001304 } else {
1305 down_read(&current->mm->mmap_sem);
1306 VERIFY(err, NULL != (vma = find_vma(current->mm,
1307 map->va)));
1308 if (err) {
1309 up_read(&current->mm->mmap_sem);
1310 goto bail;
1311 }
1312 offset = buf_page_start(buf) - vma->vm_start;
1313 up_read(&current->mm->mmap_sem);
1314 VERIFY(err, offset < (uintptr_t)map->size);
1315 if (err)
1316 goto bail;
1317 }
1318 pages[idx].addr = map->phys + offset;
1319 pages[idx].size = num << PAGE_SHIFT;
1320 }
1321 rpra[i].buf.pv = buf;
1322 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001323 PERF_END);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001324 for (i = bufs; i < bufs + handles; ++i) {
1325 struct fastrpc_mmap *map = ctx->maps[i];
1326
1327 pages[i].addr = map->phys;
1328 pages[i].size = map->size;
1329 }
1330 fdlist = (uint64_t *)&pages[bufs + handles];
1331 for (i = 0; i < M_FDLIST; i++)
1332 fdlist[i] = 0;
Sathish Ambleybae51902017-07-03 15:00:49 -07001333 crclist = (uint32_t *)&fdlist[M_FDLIST];
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +05301334 memset(crclist, 0, sizeof(uint32_t)*M_CRCLIST);
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001335
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001336 /* copy non ion buffers */
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301337 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_COPY),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001338 rlen = copylen - metalen;
1339 for (oix = 0; oix < inbufs + outbufs; ++oix) {
1340 int i = ctx->overps[oix]->raix;
1341 struct fastrpc_mmap *map = ctx->maps[i];
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301342 size_t mlen;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001343 uint64_t buf;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301344 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001345
1346 if (!len)
1347 continue;
1348 if (map)
1349 continue;
1350 if (ctx->overps[oix]->offset == 0) {
1351 rlen -= ALIGN(args, BALIGN) - args;
1352 args = ALIGN(args, BALIGN);
1353 }
Sathish Ambleyd209c1e2016-12-13 15:27:30 -08001354 mlen = ctx->overps[oix]->mend - ctx->overps[oix]->mstart;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001355 VERIFY(err, rlen >= mlen);
1356 if (err)
1357 goto bail;
1358 rpra[i].buf.pv = (args - ctx->overps[oix]->offset);
1359 pages[list[i].pgidx].addr = ctx->buf->phys -
1360 ctx->overps[oix]->offset +
1361 (copylen - rlen);
1362 pages[list[i].pgidx].addr =
1363 buf_page_start(pages[list[i].pgidx].addr);
1364 buf = rpra[i].buf.pv;
1365 pages[list[i].pgidx].size = buf_num_pages(buf, len) * PAGE_SIZE;
1366 if (i < inbufs) {
1367 K_COPY_FROM_USER(err, kernel, uint64_to_ptr(buf),
1368 lpra[i].buf.pv, len);
1369 if (err)
1370 goto bail;
1371 }
1372 args = args + mlen;
1373 rlen -= mlen;
1374 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001375 PERF_END);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001376
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301377 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_FLUSH),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001378 for (oix = 0; oix < inbufs + outbufs; ++oix) {
1379 int i = ctx->overps[oix]->raix;
1380 struct fastrpc_mmap *map = ctx->maps[i];
1381
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001382 if (map && map->uncached)
1383 continue;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +05301384 if (ctx->fl->sctx->smmu.coherent &&
1385 !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT)))
1386 continue;
1387 if (map && (map->attr & FASTRPC_ATTR_COHERENT))
1388 continue;
1389
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001390 if (rpra[i].buf.len && ctx->overps[oix]->mstart)
1391 dmac_flush_range(uint64_to_ptr(rpra[i].buf.pv),
1392 uint64_to_ptr(rpra[i].buf.pv + rpra[i].buf.len));
1393 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001394 PERF_END);
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05301395 for (i = bufs; rpra && i < bufs + handles; i++) {
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001396 rpra[i].dma.fd = ctx->fds[i];
1397 rpra[i].dma.len = (uint32_t)lpra[i].buf.len;
1398 rpra[i].dma.offset = (uint32_t)(uintptr_t)lpra[i].buf.pv;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001399 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001400
1401 if (!ctx->fl->sctx->smmu.coherent) {
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301402 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_FLUSH),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001403 dmac_flush_range((char *)rpra, (char *)rpra + ctx->used);
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001404 PERF_END);
1405 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001406 bail:
1407 return err;
1408}
1409
1410static int put_args(uint32_t kernel, struct smq_invoke_ctx *ctx,
1411 remote_arg_t *upra)
1412{
1413 uint32_t sc = ctx->sc;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001414 struct smq_invoke_buf *list;
1415 struct smq_phy_page *pages;
1416 struct fastrpc_mmap *mmap;
1417 uint64_t *fdlist;
Sathish Ambleybae51902017-07-03 15:00:49 -07001418 uint32_t *crclist = NULL;
1419
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001420 remote_arg64_t *rpra = ctx->rpra;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001421 int i, inbufs, outbufs, handles;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001422 int err = 0;
1423
1424 inbufs = REMOTE_SCALARS_INBUFS(sc);
1425 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001426 handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc);
1427 list = smq_invoke_buf_start(ctx->rpra, sc);
1428 pages = smq_phy_page_start(sc, list);
1429 fdlist = (uint64_t *)(pages + inbufs + outbufs + handles);
Sathish Ambleybae51902017-07-03 15:00:49 -07001430 crclist = (uint32_t *)(fdlist + M_FDLIST);
1431
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001432 for (i = inbufs; i < inbufs + outbufs; ++i) {
1433 if (!ctx->maps[i]) {
1434 K_COPY_TO_USER(err, kernel,
1435 ctx->lpra[i].buf.pv,
1436 uint64_to_ptr(rpra[i].buf.pv),
1437 rpra[i].buf.len);
1438 if (err)
1439 goto bail;
1440 } else {
c_mtharu7bd6a422017-10-17 18:15:37 +05301441 fastrpc_mmap_free(ctx->maps[i], 0);
c_mtharue1a5ce12017-10-13 20:47:09 +05301442 ctx->maps[i] = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001443 }
1444 }
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001445 if (inbufs + outbufs + handles) {
1446 for (i = 0; i < M_FDLIST; i++) {
1447 if (!fdlist[i])
1448 break;
1449 if (!fastrpc_mmap_find(ctx->fl, (int)fdlist[i], 0, 0,
Sathish Ambleyae5ee542017-01-16 22:24:23 -08001450 0, 0, &mmap))
c_mtharu7bd6a422017-10-17 18:15:37 +05301451 fastrpc_mmap_free(mmap, 0);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001452 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001453 }
Sathish Ambleybae51902017-07-03 15:00:49 -07001454 if (ctx->crc && crclist && rpra)
c_mtharue1a5ce12017-10-13 20:47:09 +05301455 K_COPY_TO_USER(err, kernel, ctx->crc,
Sathish Ambleybae51902017-07-03 15:00:49 -07001456 crclist, M_CRCLIST*sizeof(uint32_t));
1457
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001458 bail:
1459 return err;
1460}
1461
1462static void inv_args_pre(struct smq_invoke_ctx *ctx)
1463{
1464 int i, inbufs, outbufs;
1465 uint32_t sc = ctx->sc;
1466 remote_arg64_t *rpra = ctx->rpra;
1467 uintptr_t end;
1468
1469 inbufs = REMOTE_SCALARS_INBUFS(sc);
1470 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
1471 for (i = inbufs; i < inbufs + outbufs; ++i) {
1472 struct fastrpc_mmap *map = ctx->maps[i];
1473
1474 if (map && map->uncached)
1475 continue;
1476 if (!rpra[i].buf.len)
1477 continue;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +05301478 if (ctx->fl->sctx->smmu.coherent &&
1479 !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT)))
1480 continue;
1481 if (map && (map->attr & FASTRPC_ATTR_COHERENT))
1482 continue;
1483
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001484 if (buf_page_start(ptr_to_uint64((void *)rpra)) ==
1485 buf_page_start(rpra[i].buf.pv))
1486 continue;
1487 if (!IS_CACHE_ALIGNED((uintptr_t)uint64_to_ptr(rpra[i].buf.pv)))
1488 dmac_flush_range(uint64_to_ptr(rpra[i].buf.pv),
1489 (char *)(uint64_to_ptr(rpra[i].buf.pv + 1)));
1490 end = (uintptr_t)uint64_to_ptr(rpra[i].buf.pv +
1491 rpra[i].buf.len);
1492 if (!IS_CACHE_ALIGNED(end))
1493 dmac_flush_range((char *)end,
1494 (char *)end + 1);
1495 }
1496}
1497
1498static void inv_args(struct smq_invoke_ctx *ctx)
1499{
1500 int i, inbufs, outbufs;
1501 uint32_t sc = ctx->sc;
1502 remote_arg64_t *rpra = ctx->rpra;
1503 int used = ctx->used;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001504
1505 inbufs = REMOTE_SCALARS_INBUFS(sc);
1506 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
1507 for (i = inbufs; i < inbufs + outbufs; ++i) {
1508 struct fastrpc_mmap *map = ctx->maps[i];
1509
1510 if (map && map->uncached)
1511 continue;
1512 if (!rpra[i].buf.len)
1513 continue;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +05301514 if (ctx->fl->sctx->smmu.coherent &&
1515 !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT)))
1516 continue;
1517 if (map && (map->attr & FASTRPC_ATTR_COHERENT))
1518 continue;
1519
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001520 if (buf_page_start(ptr_to_uint64((void *)rpra)) ==
1521 buf_page_start(rpra[i].buf.pv)) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001522 continue;
1523 }
1524 if (map && map->handle)
1525 msm_ion_do_cache_op(ctx->fl->apps->client, map->handle,
1526 (char *)uint64_to_ptr(rpra[i].buf.pv),
1527 rpra[i].buf.len, ION_IOC_INV_CACHES);
1528 else
1529 dmac_inv_range((char *)uint64_to_ptr(rpra[i].buf.pv),
1530 (char *)uint64_to_ptr(rpra[i].buf.pv
1531 + rpra[i].buf.len));
1532 }
1533
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001534 if (rpra)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001535 dmac_inv_range(rpra, (char *)rpra + used);
1536}
1537
1538static int fastrpc_invoke_send(struct smq_invoke_ctx *ctx,
1539 uint32_t kernel, uint32_t handle)
1540{
1541 struct smq_msg *msg = &ctx->msg;
1542 struct fastrpc_file *fl = ctx->fl;
1543 struct fastrpc_channel_ctx *channel_ctx = &fl->apps->channel[fl->cid];
1544 int err = 0;
1545
c_mtharue1a5ce12017-10-13 20:47:09 +05301546 VERIFY(err, NULL != channel_ctx->chan);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001547 if (err)
1548 goto bail;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301549 msg->pid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001550 msg->tid = current->pid;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301551 if (fl->sessionid)
1552 msg->tid |= (1 << SESSION_ID_INDEX);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001553 if (kernel)
1554 msg->pid = 0;
1555 msg->invoke.header.ctx = ptr_to_uint64(ctx) | fl->pd;
1556 msg->invoke.header.handle = handle;
1557 msg->invoke.header.sc = ctx->sc;
1558 msg->invoke.page.addr = ctx->buf ? ctx->buf->phys : 0;
1559 msg->invoke.page.size = buf_page_size(ctx->used);
1560
1561 if (fl->ssrcount != channel_ctx->ssrcount) {
1562 err = -ECONNRESET;
1563 goto bail;
1564 }
1565 VERIFY(err, channel_ctx->link.port_state ==
1566 FASTRPC_LINK_CONNECTED);
1567 if (err)
1568 goto bail;
1569 err = glink_tx(channel_ctx->chan,
1570 (void *)&fl->apps->channel[fl->cid], msg, sizeof(*msg),
1571 GLINK_TX_REQ_INTENT);
1572 bail:
1573 return err;
1574}
1575
1576static void fastrpc_init(struct fastrpc_apps *me)
1577{
1578 int i;
1579
1580 INIT_HLIST_HEAD(&me->drivers);
Tharun Kumar Merugubcd6fbf2018-01-04 17:49:34 +05301581 INIT_HLIST_HEAD(&me->maps);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001582 spin_lock_init(&me->hlock);
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05301583 mutex_init(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001584 me->channel = &gcinfo[0];
1585 for (i = 0; i < NUM_CHANNELS; i++) {
1586 init_completion(&me->channel[i].work);
Tharun Kumar Merugu53a8ec92017-07-14 15:52:49 +05301587 init_completion(&me->channel[i].workport);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001588 me->channel[i].sesscount = 0;
1589 }
1590}
1591
1592static int fastrpc_release_current_dsp_process(struct fastrpc_file *fl);
1593
1594static int fastrpc_internal_invoke(struct fastrpc_file *fl, uint32_t mode,
1595 uint32_t kernel,
Sathish Ambleybae51902017-07-03 15:00:49 -07001596 struct fastrpc_ioctl_invoke_crc *inv)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001597{
c_mtharue1a5ce12017-10-13 20:47:09 +05301598 struct smq_invoke_ctx *ctx = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001599 struct fastrpc_ioctl_invoke *invoke = &inv->inv;
1600 int cid = fl->cid;
1601 int interrupted = 0;
1602 int err = 0;
Maria Yu757199c2017-09-22 16:05:49 +08001603 struct timespec invoket = {0};
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301604 int64_t *perf_counter = getperfcounter(fl, PERF_COUNT);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001605
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001606 if (fl->profile)
1607 getnstimeofday(&invoket);
Tharun Kumar Merugue3edf3e2017-07-27 12:34:07 +05301608
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301609
Tharun Kumar Merugue3edf3e2017-07-27 12:34:07 +05301610 VERIFY(err, fl->sctx != NULL);
1611 if (err)
1612 goto bail;
1613 VERIFY(err, fl->cid >= 0 && fl->cid < NUM_CHANNELS);
1614 if (err)
1615 goto bail;
1616
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001617 if (!kernel) {
1618 VERIFY(err, 0 == context_restore_interrupted(fl, inv,
1619 &ctx));
1620 if (err)
1621 goto bail;
1622 if (fl->sctx->smmu.faults)
1623 err = FASTRPC_ENOSUCH;
1624 if (err)
1625 goto bail;
1626 if (ctx)
1627 goto wait;
1628 }
1629
1630 VERIFY(err, 0 == context_alloc(fl, kernel, inv, &ctx));
1631 if (err)
1632 goto bail;
1633
1634 if (REMOTE_SCALARS_LENGTH(ctx->sc)) {
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301635 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_GETARGS),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001636 VERIFY(err, 0 == get_args(kernel, ctx));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001637 PERF_END);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001638 if (err)
1639 goto bail;
1640 }
1641
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301642 if (!fl->sctx->smmu.coherent) {
1643 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001644 inv_args_pre(ctx);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301645 PERF_END);
1646 }
1647
1648 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_LINK),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001649 VERIFY(err, 0 == fastrpc_invoke_send(ctx, kernel, invoke->handle));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001650 PERF_END);
1651
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001652 if (err)
1653 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001654 wait:
1655 if (kernel)
1656 wait_for_completion(&ctx->work);
1657 else {
1658 interrupted = wait_for_completion_interruptible(&ctx->work);
1659 VERIFY(err, 0 == (err = interrupted));
1660 if (err)
1661 goto bail;
1662 }
Sathish Ambleyc432b502017-06-05 12:03:42 -07001663
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301664 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS),
Sathish Ambleyc432b502017-06-05 12:03:42 -07001665 if (!fl->sctx->smmu.coherent)
1666 inv_args(ctx);
1667 PERF_END);
1668
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001669 VERIFY(err, 0 == (err = ctx->retval));
1670 if (err)
1671 goto bail;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001672
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301673 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_PUTARGS),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001674 VERIFY(err, 0 == put_args(kernel, ctx, invoke->pra));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001675 PERF_END);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001676 if (err)
1677 goto bail;
1678 bail:
1679 if (ctx && interrupted == -ERESTARTSYS)
1680 context_save_interrupted(ctx);
1681 else if (ctx)
1682 context_free(ctx);
1683 if (fl->ssrcount != fl->apps->channel[cid].ssrcount)
1684 err = ECONNRESET;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001685
1686 if (fl->profile && !interrupted) {
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301687 if (invoke->handle != FASTRPC_STATIC_HANDLE_LISTENER) {
1688 int64_t *count = GET_COUNTER(perf_counter, PERF_INVOKE);
1689
1690 if (count)
1691 *count += getnstimediff(&invoket);
1692 }
1693 if (invoke->handle > FASTRPC_STATIC_HANDLE_MAX) {
1694 int64_t *count = GET_COUNTER(perf_counter, PERF_COUNT);
1695
1696 if (count)
1697 *count = *count+1;
1698 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001699 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001700 return err;
1701}
1702
Sathish Ambley36849af2017-02-02 09:35:55 -08001703static int fastrpc_channel_open(struct fastrpc_file *fl);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001704static int fastrpc_init_process(struct fastrpc_file *fl,
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001705 struct fastrpc_ioctl_init_attrs *uproc)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001706{
1707 int err = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05301708 struct fastrpc_apps *me = &gfa;
Sathish Ambleybae51902017-07-03 15:00:49 -07001709 struct fastrpc_ioctl_invoke_crc ioctl;
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001710 struct fastrpc_ioctl_init *init = &uproc->init;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001711 struct smq_phy_page pages[1];
c_mtharue1a5ce12017-10-13 20:47:09 +05301712 struct fastrpc_mmap *file = NULL, *mem = NULL;
1713 char *proc_name = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001714
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05301715 VERIFY(err, 0 == (err = fastrpc_channel_open(fl)));
Sathish Ambley36849af2017-02-02 09:35:55 -08001716 if (err)
1717 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001718 if (init->flags == FASTRPC_INIT_ATTACH) {
1719 remote_arg_t ra[1];
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301720 int tgid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001721
1722 ra[0].buf.pv = (void *)&tgid;
1723 ra[0].buf.len = sizeof(tgid);
1724 ioctl.inv.handle = 1;
1725 ioctl.inv.sc = REMOTE_SCALARS_MAKE(0, 1, 0);
1726 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05301727 ioctl.fds = NULL;
1728 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07001729 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001730 fl->pd = 0;
1731 VERIFY(err, !(err = fastrpc_internal_invoke(fl,
1732 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
1733 if (err)
1734 goto bail;
1735 } else if (init->flags == FASTRPC_INIT_CREATE) {
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001736 remote_arg_t ra[6];
1737 int fds[6];
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001738 int mflags = 0;
1739 struct {
1740 int pgid;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301741 unsigned int namelen;
1742 unsigned int filelen;
1743 unsigned int pageslen;
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001744 int attrs;
1745 int siglen;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001746 } inbuf;
1747
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301748 inbuf.pgid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001749 inbuf.namelen = strlen(current->comm) + 1;
1750 inbuf.filelen = init->filelen;
1751 fl->pd = 1;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301752
Tharun Kumar Merugudf852892017-12-07 16:27:37 +05301753 VERIFY(err, access_ok(0, (void __user *)init->file,
1754 init->filelen));
1755 if (err)
1756 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001757 if (init->filelen) {
1758 VERIFY(err, !fastrpc_mmap_create(fl, init->filefd, 0,
1759 init->file, init->filelen, mflags, &file));
1760 if (err)
1761 goto bail;
1762 }
1763 inbuf.pageslen = 1;
Tharun Kumar Merugudf852892017-12-07 16:27:37 +05301764 VERIFY(err, access_ok(1, (void __user *)init->mem,
1765 init->memlen));
1766 if (err)
1767 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001768 VERIFY(err, !fastrpc_mmap_create(fl, init->memfd, 0,
1769 init->mem, init->memlen, mflags, &mem));
1770 if (err)
1771 goto bail;
1772 inbuf.pageslen = 1;
1773 ra[0].buf.pv = (void *)&inbuf;
1774 ra[0].buf.len = sizeof(inbuf);
1775 fds[0] = 0;
1776
1777 ra[1].buf.pv = (void *)current->comm;
1778 ra[1].buf.len = inbuf.namelen;
1779 fds[1] = 0;
1780
1781 ra[2].buf.pv = (void *)init->file;
1782 ra[2].buf.len = inbuf.filelen;
1783 fds[2] = init->filefd;
1784
1785 pages[0].addr = mem->phys;
1786 pages[0].size = mem->size;
1787 ra[3].buf.pv = (void *)pages;
1788 ra[3].buf.len = 1 * sizeof(*pages);
1789 fds[3] = 0;
1790
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001791 inbuf.attrs = uproc->attrs;
1792 ra[4].buf.pv = (void *)&(inbuf.attrs);
1793 ra[4].buf.len = sizeof(inbuf.attrs);
1794 fds[4] = 0;
1795
1796 inbuf.siglen = uproc->siglen;
1797 ra[5].buf.pv = (void *)&(inbuf.siglen);
1798 ra[5].buf.len = sizeof(inbuf.siglen);
1799 fds[5] = 0;
1800
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001801 ioctl.inv.handle = 1;
1802 ioctl.inv.sc = REMOTE_SCALARS_MAKE(6, 4, 0);
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001803 if (uproc->attrs)
1804 ioctl.inv.sc = REMOTE_SCALARS_MAKE(7, 6, 0);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001805 ioctl.inv.pra = ra;
1806 ioctl.fds = fds;
c_mtharue1a5ce12017-10-13 20:47:09 +05301807 ioctl.attrs = NULL;
1808 ioctl.crc = NULL;
1809 VERIFY(err, !(err = fastrpc_internal_invoke(fl,
1810 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
1811 if (err)
1812 goto bail;
1813 } else if (init->flags == FASTRPC_INIT_CREATE_STATIC) {
1814 remote_arg_t ra[3];
1815 uint64_t phys = 0;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301816 size_t size = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05301817 int fds[3];
1818 struct {
1819 int pgid;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301820 unsigned int namelen;
1821 unsigned int pageslen;
c_mtharue1a5ce12017-10-13 20:47:09 +05301822 } inbuf;
1823
1824 if (!init->filelen)
1825 goto bail;
1826
1827 proc_name = kzalloc(init->filelen, GFP_KERNEL);
1828 VERIFY(err, !IS_ERR_OR_NULL(proc_name));
1829 if (err)
1830 goto bail;
1831 VERIFY(err, 0 == copy_from_user((void *)proc_name,
1832 (void __user *)init->file, init->filelen));
1833 if (err)
1834 goto bail;
1835
1836 inbuf.pgid = current->tgid;
c_mtharu81a0aa72017-11-07 16:13:21 +05301837 inbuf.namelen = init->filelen;
c_mtharue1a5ce12017-10-13 20:47:09 +05301838 inbuf.pageslen = 0;
1839 if (!me->staticpd_flags) {
1840 inbuf.pageslen = 1;
1841 VERIFY(err, !fastrpc_mmap_create(fl, -1, 0, init->mem,
1842 init->memlen, ADSP_MMAP_REMOTE_HEAP_ADDR,
1843 &mem));
1844 if (err)
1845 goto bail;
1846 phys = mem->phys;
1847 size = mem->size;
1848 VERIFY(err, !hyp_assign_phys(phys, (uint64_t)size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05301849 hlosvm, 1, me->channel[fl->cid].rhvm.vmid,
1850 me->channel[fl->cid].rhvm.vmperm,
1851 me->channel[fl->cid].rhvm.vmcount));
c_mtharue1a5ce12017-10-13 20:47:09 +05301852 if (err) {
1853 pr_err("ADSPRPC: hyp_assign_phys fail err %d",
1854 err);
1855 pr_err("map->phys %llx, map->size %d\n",
1856 phys, (int)size);
1857 goto bail;
1858 }
1859 me->staticpd_flags = 1;
1860 }
1861
1862 ra[0].buf.pv = (void *)&inbuf;
1863 ra[0].buf.len = sizeof(inbuf);
1864 fds[0] = 0;
1865
1866 ra[1].buf.pv = (void *)proc_name;
1867 ra[1].buf.len = inbuf.namelen;
1868 fds[1] = 0;
1869
1870 pages[0].addr = phys;
1871 pages[0].size = size;
1872
1873 ra[2].buf.pv = (void *)pages;
1874 ra[2].buf.len = sizeof(*pages);
1875 fds[2] = 0;
1876 ioctl.inv.handle = 1;
1877
1878 ioctl.inv.sc = REMOTE_SCALARS_MAKE(8, 3, 0);
1879 ioctl.inv.pra = ra;
1880 ioctl.fds = NULL;
1881 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07001882 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001883 VERIFY(err, !(err = fastrpc_internal_invoke(fl,
1884 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
1885 if (err)
1886 goto bail;
1887 } else {
1888 err = -ENOTTY;
1889 }
1890bail:
c_mtharud91205a2017-11-07 16:01:06 +05301891 kfree(proc_name);
c_mtharue1a5ce12017-10-13 20:47:09 +05301892 if (err && (init->flags == FASTRPC_INIT_CREATE_STATIC))
1893 me->staticpd_flags = 0;
1894 if (mem && err) {
1895 if (mem->flags == ADSP_MMAP_REMOTE_HEAP_ADDR)
1896 hyp_assign_phys(mem->phys, (uint64_t)mem->size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05301897 me->channel[fl->cid].rhvm.vmid,
1898 me->channel[fl->cid].rhvm.vmcount,
1899 hlosvm, hlosvmperm, 1);
c_mtharu7bd6a422017-10-17 18:15:37 +05301900 fastrpc_mmap_free(mem, 0);
c_mtharue1a5ce12017-10-13 20:47:09 +05301901 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001902 if (file)
c_mtharu7bd6a422017-10-17 18:15:37 +05301903 fastrpc_mmap_free(file, 0);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001904 return err;
1905}
1906
1907static int fastrpc_release_current_dsp_process(struct fastrpc_file *fl)
1908{
1909 int err = 0;
Sathish Ambleybae51902017-07-03 15:00:49 -07001910 struct fastrpc_ioctl_invoke_crc ioctl;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001911 remote_arg_t ra[1];
1912 int tgid = 0;
1913
Sathish Ambley36849af2017-02-02 09:35:55 -08001914 VERIFY(err, fl->cid >= 0 && fl->cid < NUM_CHANNELS);
1915 if (err)
1916 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05301917 VERIFY(err, fl->apps->channel[fl->cid].chan != NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001918 if (err)
1919 goto bail;
1920 tgid = fl->tgid;
1921 ra[0].buf.pv = (void *)&tgid;
1922 ra[0].buf.len = sizeof(tgid);
1923 ioctl.inv.handle = 1;
1924 ioctl.inv.sc = REMOTE_SCALARS_MAKE(1, 1, 0);
1925 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05301926 ioctl.fds = NULL;
1927 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07001928 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001929 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
1930 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
1931bail:
1932 return err;
1933}
1934
1935static int fastrpc_mmap_on_dsp(struct fastrpc_file *fl, uint32_t flags,
1936 struct fastrpc_mmap *map)
1937{
Sathish Ambleybae51902017-07-03 15:00:49 -07001938 struct fastrpc_ioctl_invoke_crc ioctl;
c_mtharu63ffc012017-11-16 15:26:56 +05301939 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001940 struct smq_phy_page page;
1941 int num = 1;
1942 remote_arg_t ra[3];
1943 int err = 0;
1944 struct {
1945 int pid;
1946 uint32_t flags;
1947 uintptr_t vaddrin;
1948 int num;
1949 } inargs;
1950 struct {
1951 uintptr_t vaddrout;
1952 } routargs;
1953
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301954 inargs.pid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001955 inargs.vaddrin = (uintptr_t)map->va;
1956 inargs.flags = flags;
1957 inargs.num = fl->apps->compat ? num * sizeof(page) : num;
1958 ra[0].buf.pv = (void *)&inargs;
1959 ra[0].buf.len = sizeof(inargs);
1960 page.addr = map->phys;
1961 page.size = map->size;
1962 ra[1].buf.pv = (void *)&page;
1963 ra[1].buf.len = num * sizeof(page);
1964
1965 ra[2].buf.pv = (void *)&routargs;
1966 ra[2].buf.len = sizeof(routargs);
1967
1968 ioctl.inv.handle = 1;
1969 if (fl->apps->compat)
1970 ioctl.inv.sc = REMOTE_SCALARS_MAKE(4, 2, 1);
1971 else
1972 ioctl.inv.sc = REMOTE_SCALARS_MAKE(2, 2, 1);
1973 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05301974 ioctl.fds = NULL;
1975 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07001976 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001977 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
1978 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
1979 map->raddr = (uintptr_t)routargs.vaddrout;
c_mtharue1a5ce12017-10-13 20:47:09 +05301980 if (err)
1981 goto bail;
1982 if (flags == ADSP_MMAP_HEAP_ADDR) {
1983 struct scm_desc desc = {0};
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001984
c_mtharue1a5ce12017-10-13 20:47:09 +05301985 desc.args[0] = TZ_PIL_AUTH_QDSP6_PROC;
1986 desc.args[1] = map->phys;
1987 desc.args[2] = map->size;
1988 desc.arginfo = SCM_ARGS(3);
1989 err = scm_call2(SCM_SIP_FNID(SCM_SVC_PIL,
1990 TZ_PIL_PROTECT_MEM_SUBSYS_ID), &desc);
1991 } else if (flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
c_mtharue1a5ce12017-10-13 20:47:09 +05301992 VERIFY(err, !hyp_assign_phys(map->phys, (uint64_t)map->size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05301993 hlosvm, 1, me->channel[fl->cid].rhvm.vmid,
1994 me->channel[fl->cid].rhvm.vmperm,
1995 me->channel[fl->cid].rhvm.vmcount));
c_mtharue1a5ce12017-10-13 20:47:09 +05301996 if (err)
1997 goto bail;
1998 }
1999bail:
2000 return err;
2001}
2002
2003static int fastrpc_munmap_on_dsp_rh(struct fastrpc_file *fl,
2004 struct fastrpc_mmap *map)
2005{
2006 int err = 0;
c_mtharu63ffc012017-11-16 15:26:56 +05302007 struct fastrpc_apps *me = &gfa;
c_mtharue1a5ce12017-10-13 20:47:09 +05302008 int destVM[1] = {VMID_HLOS};
2009 int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
2010
2011 if (map->flags == ADSP_MMAP_HEAP_ADDR) {
2012 struct fastrpc_ioctl_invoke_crc ioctl;
2013 struct scm_desc desc = {0};
2014 remote_arg_t ra[1];
2015 int err = 0;
2016 struct {
2017 uint8_t skey;
2018 } routargs;
2019
2020 ra[0].buf.pv = (void *)&routargs;
2021 ra[0].buf.len = sizeof(routargs);
2022
2023 ioctl.inv.handle = 1;
2024 ioctl.inv.sc = REMOTE_SCALARS_MAKE(7, 0, 1);
2025 ioctl.inv.pra = ra;
2026 ioctl.fds = NULL;
2027 ioctl.attrs = NULL;
2028 ioctl.crc = NULL;
2029 if (fl == NULL)
2030 goto bail;
2031
2032 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
2033 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
2034 if (err)
2035 goto bail;
2036 desc.args[0] = TZ_PIL_AUTH_QDSP6_PROC;
2037 desc.args[1] = map->phys;
2038 desc.args[2] = map->size;
2039 desc.args[3] = routargs.skey;
2040 desc.arginfo = SCM_ARGS(4);
2041 err = scm_call2(SCM_SIP_FNID(SCM_SVC_PIL,
2042 TZ_PIL_CLEAR_PROTECT_MEM_SUBSYS_ID), &desc);
2043 } else if (map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
2044 VERIFY(err, !hyp_assign_phys(map->phys, (uint64_t)map->size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05302045 me->channel[fl->cid].rhvm.vmid,
2046 me->channel[fl->cid].rhvm.vmcount,
2047 destVM, destVMperm, 1));
c_mtharue1a5ce12017-10-13 20:47:09 +05302048 if (err)
2049 goto bail;
2050 }
2051
2052bail:
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002053 return err;
2054}
2055
2056static int fastrpc_munmap_on_dsp(struct fastrpc_file *fl,
2057 struct fastrpc_mmap *map)
2058{
Sathish Ambleybae51902017-07-03 15:00:49 -07002059 struct fastrpc_ioctl_invoke_crc ioctl;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002060 remote_arg_t ra[1];
2061 int err = 0;
2062 struct {
2063 int pid;
2064 uintptr_t vaddrout;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302065 size_t size;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002066 } inargs;
2067
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05302068 inargs.pid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002069 inargs.size = map->size;
2070 inargs.vaddrout = map->raddr;
2071 ra[0].buf.pv = (void *)&inargs;
2072 ra[0].buf.len = sizeof(inargs);
2073
2074 ioctl.inv.handle = 1;
2075 if (fl->apps->compat)
2076 ioctl.inv.sc = REMOTE_SCALARS_MAKE(5, 1, 0);
2077 else
2078 ioctl.inv.sc = REMOTE_SCALARS_MAKE(3, 1, 0);
2079 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05302080 ioctl.fds = NULL;
2081 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07002082 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002083 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
2084 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
c_mtharue1a5ce12017-10-13 20:47:09 +05302085 if (err)
2086 goto bail;
2087 if (map->flags == ADSP_MMAP_HEAP_ADDR ||
2088 map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
2089 VERIFY(err, !fastrpc_munmap_on_dsp_rh(fl, map));
2090 if (err)
2091 goto bail;
2092 }
2093bail:
2094 return err;
2095}
2096
2097static int fastrpc_mmap_remove_ssr(struct fastrpc_file *fl)
2098{
2099 struct fastrpc_mmap *match = NULL, *map = NULL;
2100 struct hlist_node *n = NULL;
2101 int err = 0, ret = 0;
2102 struct fastrpc_apps *me = &gfa;
2103 struct ramdump_segment *ramdump_segments_rh = NULL;
2104
2105 do {
2106 match = NULL;
2107 spin_lock(&me->hlock);
2108 hlist_for_each_entry_safe(map, n, &me->maps, hn) {
2109 match = map;
2110 hlist_del_init(&map->hn);
2111 break;
2112 }
2113 spin_unlock(&me->hlock);
2114
2115 if (match) {
2116 VERIFY(err, !fastrpc_munmap_on_dsp_rh(fl, match));
2117 if (err)
2118 goto bail;
2119 if (me->channel[0].ramdumpenabled) {
2120 ramdump_segments_rh = kcalloc(1,
2121 sizeof(struct ramdump_segment), GFP_KERNEL);
2122 if (ramdump_segments_rh) {
2123 ramdump_segments_rh->address =
2124 match->phys;
2125 ramdump_segments_rh->size = match->size;
2126 ret = do_elf_ramdump(
2127 me->channel[0].remoteheap_ramdump_dev,
2128 ramdump_segments_rh, 1);
2129 if (ret < 0)
2130 pr_err("ADSPRPC: unable to dump heap");
2131 kfree(ramdump_segments_rh);
2132 }
2133 }
c_mtharu7bd6a422017-10-17 18:15:37 +05302134 fastrpc_mmap_free(match, 0);
c_mtharue1a5ce12017-10-13 20:47:09 +05302135 }
2136 } while (match);
2137bail:
2138 if (err && match)
2139 fastrpc_mmap_add(match);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002140 return err;
2141}
2142
2143static int fastrpc_mmap_remove(struct fastrpc_file *fl, uintptr_t va,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302144 size_t len, struct fastrpc_mmap **ppmap);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002145
2146static void fastrpc_mmap_add(struct fastrpc_mmap *map);
2147
2148static int fastrpc_internal_munmap(struct fastrpc_file *fl,
2149 struct fastrpc_ioctl_munmap *ud)
2150{
2151 int err = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05302152 struct fastrpc_mmap *map = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002153
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302154 mutex_lock(&fl->map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002155 VERIFY(err, !fastrpc_mmap_remove(fl, ud->vaddrout, ud->size, &map));
2156 if (err)
2157 goto bail;
2158 VERIFY(err, !fastrpc_munmap_on_dsp(fl, map));
2159 if (err)
2160 goto bail;
c_mtharu7bd6a422017-10-17 18:15:37 +05302161 fastrpc_mmap_free(map, 0);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002162bail:
2163 if (err && map)
2164 fastrpc_mmap_add(map);
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302165 mutex_unlock(&fl->map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002166 return err;
2167}
2168
c_mtharu7bd6a422017-10-17 18:15:37 +05302169static int fastrpc_internal_munmap_fd(struct fastrpc_file *fl,
2170 struct fastrpc_ioctl_munmap_fd *ud) {
2171 int err = 0;
2172 struct fastrpc_mmap *map = NULL;
2173
2174 VERIFY(err, (fl && ud));
2175 if (err)
2176 goto bail;
2177
2178 if (!fastrpc_mmap_find(fl, ud->fd, ud->va, ud->len, 0, 0, &map)) {
2179 pr_err("mapping not found to unamp %x va %llx %x\n",
2180 ud->fd, (unsigned long long)ud->va,
2181 (unsigned int)ud->len);
2182 err = -1;
2183 goto bail;
2184 }
2185 if (map)
2186 fastrpc_mmap_free(map, 0);
2187bail:
2188 return err;
2189}
2190
2191
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002192static int fastrpc_internal_mmap(struct fastrpc_file *fl,
2193 struct fastrpc_ioctl_mmap *ud)
2194{
2195
c_mtharue1a5ce12017-10-13 20:47:09 +05302196 struct fastrpc_mmap *map = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002197 int err = 0;
2198
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302199 mutex_lock(&fl->map_mutex);
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302200 if (!fastrpc_mmap_find(fl, ud->fd, (uintptr_t)ud->vaddrin,
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302201 ud->size, ud->flags, 1, &map)){
2202 mutex_unlock(&fl->map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002203 return 0;
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302204 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002205 VERIFY(err, !fastrpc_mmap_create(fl, ud->fd, 0,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302206 (uintptr_t)ud->vaddrin, ud->size,
c_mtharue1a5ce12017-10-13 20:47:09 +05302207 ud->flags, &map));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002208 if (err)
2209 goto bail;
2210 VERIFY(err, 0 == fastrpc_mmap_on_dsp(fl, ud->flags, map));
2211 if (err)
2212 goto bail;
2213 ud->vaddrout = map->raddr;
2214 bail:
2215 if (err && map)
c_mtharu7bd6a422017-10-17 18:15:37 +05302216 fastrpc_mmap_free(map, 0);
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302217 mutex_unlock(&fl->map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002218 return err;
2219}
2220
2221static void fastrpc_channel_close(struct kref *kref)
2222{
2223 struct fastrpc_apps *me = &gfa;
2224 struct fastrpc_channel_ctx *ctx;
2225 int cid;
2226
2227 ctx = container_of(kref, struct fastrpc_channel_ctx, kref);
2228 cid = ctx - &gcinfo[0];
2229 fastrpc_glink_close(ctx->chan, cid);
c_mtharue1a5ce12017-10-13 20:47:09 +05302230 ctx->chan = NULL;
Tharun Kumar Merugu532767d2017-06-20 19:53:13 +05302231 glink_unregister_link_state_cb(ctx->link.link_notify_handle);
2232 ctx->link.link_notify_handle = NULL;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302233 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002234 pr_info("'closed /dev/%s c %d %d'\n", gcinfo[cid].name,
2235 MAJOR(me->dev_no), cid);
2236}
2237
2238static void fastrpc_context_list_dtor(struct fastrpc_file *fl);
2239
2240static int fastrpc_session_alloc_locked(struct fastrpc_channel_ctx *chan,
2241 int secure, struct fastrpc_session_ctx **session)
2242{
2243 struct fastrpc_apps *me = &gfa;
2244 int idx = 0, err = 0;
2245
2246 if (chan->sesscount) {
2247 for (idx = 0; idx < chan->sesscount; ++idx) {
2248 if (!chan->session[idx].used &&
2249 chan->session[idx].smmu.secure == secure) {
2250 chan->session[idx].used = 1;
2251 break;
2252 }
2253 }
2254 VERIFY(err, idx < chan->sesscount);
2255 if (err)
2256 goto bail;
2257 chan->session[idx].smmu.faults = 0;
2258 } else {
2259 VERIFY(err, me->dev != NULL);
2260 if (err)
2261 goto bail;
2262 chan->session[0].dev = me->dev;
c_mtharue1a5ce12017-10-13 20:47:09 +05302263 chan->session[0].smmu.dev = me->dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002264 }
2265
2266 *session = &chan->session[idx];
2267 bail:
2268 return err;
2269}
2270
c_mtharue1a5ce12017-10-13 20:47:09 +05302271static bool fastrpc_glink_notify_rx_intent_req(void *h, const void *priv,
2272 size_t size)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002273{
2274 if (glink_queue_rx_intent(h, NULL, size))
2275 return false;
2276 return true;
2277}
2278
c_mtharue1a5ce12017-10-13 20:47:09 +05302279static void fastrpc_glink_notify_tx_done(void *handle, const void *priv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002280 const void *pkt_priv, const void *ptr)
2281{
2282}
2283
c_mtharue1a5ce12017-10-13 20:47:09 +05302284static void fastrpc_glink_notify_rx(void *handle, const void *priv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002285 const void *pkt_priv, const void *ptr, size_t size)
2286{
2287 struct smq_invoke_rsp *rsp = (struct smq_invoke_rsp *)ptr;
c_mtharufdac6892017-10-12 13:09:01 +05302288 struct smq_invoke_ctx *ctx;
2289 int err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002290
c_mtharufdac6892017-10-12 13:09:01 +05302291 VERIFY(err, (rsp && size >= sizeof(*rsp)));
2292 if (err)
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302293 goto bail;
2294
c_mtharufdac6892017-10-12 13:09:01 +05302295 ctx = (struct smq_invoke_ctx *)(uint64_to_ptr(rsp->ctx & ~1));
2296 VERIFY(err, (ctx && ctx->magic == FASTRPC_CTX_MAGIC));
2297 if (err)
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302298 goto bail;
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302299
c_mtharufdac6892017-10-12 13:09:01 +05302300 context_notify_user(ctx, rsp->retval);
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302301bail:
c_mtharufdac6892017-10-12 13:09:01 +05302302 if (err)
2303 pr_err("adsprpc: invalid response or context\n");
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002304 glink_rx_done(handle, ptr, true);
2305}
2306
c_mtharue1a5ce12017-10-13 20:47:09 +05302307static void fastrpc_glink_notify_state(void *handle, const void *priv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002308 unsigned int event)
2309{
2310 struct fastrpc_apps *me = &gfa;
2311 int cid = (int)(uintptr_t)priv;
2312 struct fastrpc_glink_info *link;
2313
2314 if (cid < 0 || cid >= NUM_CHANNELS)
2315 return;
2316 link = &me->channel[cid].link;
2317 switch (event) {
2318 case GLINK_CONNECTED:
2319 link->port_state = FASTRPC_LINK_CONNECTED;
Tharun Kumar Merugu53a8ec92017-07-14 15:52:49 +05302320 complete(&me->channel[cid].workport);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002321 break;
2322 case GLINK_LOCAL_DISCONNECTED:
2323 link->port_state = FASTRPC_LINK_DISCONNECTED;
2324 break;
2325 case GLINK_REMOTE_DISCONNECTED:
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002326 break;
2327 default:
2328 break;
2329 }
2330}
2331
2332static int fastrpc_session_alloc(struct fastrpc_channel_ctx *chan, int secure,
2333 struct fastrpc_session_ctx **session)
2334{
2335 int err = 0;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302336 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002337
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302338 mutex_lock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002339 if (!*session)
2340 err = fastrpc_session_alloc_locked(chan, secure, session);
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302341 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002342 return err;
2343}
2344
2345static void fastrpc_session_free(struct fastrpc_channel_ctx *chan,
2346 struct fastrpc_session_ctx *session)
2347{
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302348 struct fastrpc_apps *me = &gfa;
2349
2350 mutex_lock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002351 session->used = 0;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302352 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002353}
2354
2355static int fastrpc_file_free(struct fastrpc_file *fl)
2356{
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302357 struct hlist_node *n = NULL;
c_mtharue1a5ce12017-10-13 20:47:09 +05302358 struct fastrpc_mmap *map = NULL;
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302359 struct fastrpc_perf *perf = NULL, *fperf = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002360 int cid;
2361
2362 if (!fl)
2363 return 0;
2364 cid = fl->cid;
2365
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302366 (void)fastrpc_release_current_dsp_process(fl);
2367
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002368 spin_lock(&fl->apps->hlock);
2369 hlist_del_init(&fl->hn);
2370 spin_unlock(&fl->apps->hlock);
2371
Sathish Ambleyd7fbcbb2017-03-08 10:55:48 -08002372 if (!fl->sctx) {
2373 kfree(fl);
2374 return 0;
2375 }
tharun kumar9f899ea2017-07-03 17:07:03 +05302376 spin_lock(&fl->hlock);
2377 fl->file_close = 1;
2378 spin_unlock(&fl->hlock);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002379 fastrpc_context_list_dtor(fl);
2380 fastrpc_buf_list_free(fl);
2381 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
c_mtharu7bd6a422017-10-17 18:15:37 +05302382 fastrpc_mmap_free(map, 1);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002383 }
2384 if (fl->ssrcount == fl->apps->channel[cid].ssrcount)
2385 kref_put_mutex(&fl->apps->channel[cid].kref,
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302386 fastrpc_channel_close, &fl->apps->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002387 if (fl->sctx)
2388 fastrpc_session_free(&fl->apps->channel[cid], fl->sctx);
2389 if (fl->secsctx)
2390 fastrpc_session_free(&fl->apps->channel[cid], fl->secsctx);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302391
2392 mutex_lock(&fl->perf_mutex);
2393 do {
2394 struct hlist_node *pn = NULL;
2395
2396 fperf = NULL;
2397 hlist_for_each_entry_safe(perf, pn, &fl->perf, hn) {
2398 hlist_del_init(&perf->hn);
2399 fperf = perf;
2400 break;
2401 }
2402 kfree(fperf);
2403 } while (fperf);
2404 mutex_unlock(&fl->perf_mutex);
2405 mutex_destroy(&fl->perf_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002406 kfree(fl);
2407 return 0;
2408}
2409
2410static int fastrpc_device_release(struct inode *inode, struct file *file)
2411{
2412 struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data;
2413
2414 if (fl) {
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05302415 if (fl->qos_request && pm_qos_request_active(&fl->pm_qos_req))
2416 pm_qos_remove_request(&fl->pm_qos_req);
Sathish Ambley1ca68232017-01-19 10:32:55 -08002417 if (fl->debugfs_file != NULL)
2418 debugfs_remove(fl->debugfs_file);
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302419 mutex_destroy(&fl->map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002420 fastrpc_file_free(fl);
c_mtharue1a5ce12017-10-13 20:47:09 +05302421 file->private_data = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002422 }
2423 return 0;
2424}
2425
2426static void fastrpc_link_state_handler(struct glink_link_state_cb_info *cb_info,
2427 void *priv)
2428{
2429 struct fastrpc_apps *me = &gfa;
2430 int cid = (int)((uintptr_t)priv);
2431 struct fastrpc_glink_info *link;
2432
2433 if (cid < 0 || cid >= NUM_CHANNELS)
2434 return;
2435
2436 link = &me->channel[cid].link;
2437 switch (cb_info->link_state) {
2438 case GLINK_LINK_STATE_UP:
2439 link->link_state = FASTRPC_LINK_STATE_UP;
2440 complete(&me->channel[cid].work);
2441 break;
2442 case GLINK_LINK_STATE_DOWN:
2443 link->link_state = FASTRPC_LINK_STATE_DOWN;
2444 break;
2445 default:
2446 pr_err("adsprpc: unknown link state %d\n", cb_info->link_state);
2447 break;
2448 }
2449}
2450
2451static int fastrpc_glink_register(int cid, struct fastrpc_apps *me)
2452{
2453 int err = 0;
2454 struct fastrpc_glink_info *link;
2455
2456 VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS));
2457 if (err)
2458 goto bail;
2459
2460 link = &me->channel[cid].link;
2461 if (link->link_notify_handle != NULL)
2462 goto bail;
2463
2464 link->link_info.glink_link_state_notif_cb = fastrpc_link_state_handler;
2465 link->link_notify_handle = glink_register_link_state_cb(
2466 &link->link_info,
2467 (void *)((uintptr_t)cid));
2468 VERIFY(err, !IS_ERR_OR_NULL(me->channel[cid].link.link_notify_handle));
2469 if (err) {
2470 link->link_notify_handle = NULL;
2471 goto bail;
2472 }
2473 VERIFY(err, wait_for_completion_timeout(&me->channel[cid].work,
2474 RPC_TIMEOUT));
2475bail:
2476 return err;
2477}
2478
2479static void fastrpc_glink_close(void *chan, int cid)
2480{
2481 int err = 0;
2482 struct fastrpc_glink_info *link;
2483
2484 VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS));
2485 if (err)
2486 return;
2487 link = &gfa.channel[cid].link;
2488
c_mtharu314a4202017-11-15 22:09:17 +05302489 if (link->port_state == FASTRPC_LINK_CONNECTED ||
2490 link->port_state == FASTRPC_LINK_REMOTE_DISCONNECTING) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002491 link->port_state = FASTRPC_LINK_DISCONNECTING;
2492 glink_close(chan);
2493 }
2494}
2495
2496static int fastrpc_glink_open(int cid)
2497{
2498 int err = 0;
2499 void *handle = NULL;
2500 struct fastrpc_apps *me = &gfa;
2501 struct glink_open_config *cfg;
2502 struct fastrpc_glink_info *link;
2503
2504 VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS));
2505 if (err)
2506 goto bail;
2507 link = &me->channel[cid].link;
2508 cfg = &me->channel[cid].link.cfg;
2509 VERIFY(err, (link->link_state == FASTRPC_LINK_STATE_UP));
2510 if (err)
2511 goto bail;
2512
Tharun Kumar Meruguca0db5262017-05-10 12:53:12 +05302513 VERIFY(err, (link->port_state == FASTRPC_LINK_DISCONNECTED));
2514 if (err)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002515 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002516
2517 link->port_state = FASTRPC_LINK_CONNECTING;
2518 cfg->priv = (void *)(uintptr_t)cid;
2519 cfg->edge = gcinfo[cid].link.link_info.edge;
2520 cfg->transport = gcinfo[cid].link.link_info.transport;
2521 cfg->name = FASTRPC_GLINK_GUID;
2522 cfg->notify_rx = fastrpc_glink_notify_rx;
2523 cfg->notify_tx_done = fastrpc_glink_notify_tx_done;
2524 cfg->notify_state = fastrpc_glink_notify_state;
2525 cfg->notify_rx_intent_req = fastrpc_glink_notify_rx_intent_req;
2526 handle = glink_open(cfg);
2527 VERIFY(err, !IS_ERR_OR_NULL(handle));
c_mtharu6e1d26b2017-10-09 16:05:24 +05302528 if (err) {
2529 if (link->port_state == FASTRPC_LINK_CONNECTING)
2530 link->port_state = FASTRPC_LINK_DISCONNECTED;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002531 goto bail;
c_mtharu6e1d26b2017-10-09 16:05:24 +05302532 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002533 me->channel[cid].chan = handle;
2534bail:
2535 return err;
2536}
2537
Sathish Ambley1ca68232017-01-19 10:32:55 -08002538static int fastrpc_debugfs_open(struct inode *inode, struct file *filp)
2539{
2540 filp->private_data = inode->i_private;
2541 return 0;
2542}
2543
2544static ssize_t fastrpc_debugfs_read(struct file *filp, char __user *buffer,
2545 size_t count, loff_t *position)
2546{
2547 struct fastrpc_file *fl = filp->private_data;
2548 struct hlist_node *n;
c_mtharue1a5ce12017-10-13 20:47:09 +05302549 struct fastrpc_buf *buf = NULL;
2550 struct fastrpc_mmap *map = NULL;
2551 struct smq_invoke_ctx *ictx = NULL;
Sathish Ambley1ca68232017-01-19 10:32:55 -08002552 struct fastrpc_channel_ctx *chan;
2553 struct fastrpc_session_ctx *sess;
2554 unsigned int len = 0;
2555 int i, j, ret = 0;
2556 char *fileinfo = NULL;
2557
2558 fileinfo = kzalloc(DEBUGFS_SIZE, GFP_KERNEL);
2559 if (!fileinfo)
2560 goto bail;
2561 if (fl == NULL) {
2562 for (i = 0; i < NUM_CHANNELS; i++) {
2563 chan = &gcinfo[i];
2564 len += scnprintf(fileinfo + len,
2565 DEBUGFS_SIZE - len, "%s\n\n",
2566 chan->name);
2567 len += scnprintf(fileinfo + len,
2568 DEBUGFS_SIZE - len, "%s %d\n",
2569 "sesscount:", chan->sesscount);
2570 for (j = 0; j < chan->sesscount; j++) {
2571 sess = &chan->session[j];
2572 len += scnprintf(fileinfo + len,
2573 DEBUGFS_SIZE - len,
2574 "%s%d\n\n", "SESSION", j);
2575 len += scnprintf(fileinfo + len,
2576 DEBUGFS_SIZE - len,
2577 "%s %d\n", "sid:",
2578 sess->smmu.cb);
2579 len += scnprintf(fileinfo + len,
2580 DEBUGFS_SIZE - len,
2581 "%s %d\n", "SECURE:",
2582 sess->smmu.secure);
2583 }
2584 }
2585 } else {
2586 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2587 "%s %d\n\n",
2588 "PROCESS_ID:", fl->tgid);
2589 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2590 "%s %d\n\n",
2591 "CHANNEL_ID:", fl->cid);
2592 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2593 "%s %d\n\n",
2594 "SSRCOUNT:", fl->ssrcount);
2595 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2596 "%s\n",
2597 "LIST OF BUFS:");
2598 spin_lock(&fl->hlock);
2599 hlist_for_each_entry_safe(buf, n, &fl->bufs, hn) {
2600 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Tharun Kumar Meruguce566452017-08-17 15:29:59 +05302601 "%s %pK %s %pK %s %llx\n", "buf:",
2602 buf, "buf->virt:", buf->virt,
2603 "buf->phys:", buf->phys);
Sathish Ambley1ca68232017-01-19 10:32:55 -08002604 }
2605 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2606 "\n%s\n",
2607 "LIST OF MAPS:");
2608 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
2609 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Tharun Kumar Meruguce566452017-08-17 15:29:59 +05302610 "%s %pK %s %lx %s %llx\n",
Sathish Ambley1ca68232017-01-19 10:32:55 -08002611 "map:", map,
2612 "map->va:", map->va,
2613 "map->phys:", map->phys);
2614 }
2615 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2616 "\n%s\n",
2617 "LIST OF PENDING SMQCONTEXTS:");
2618 hlist_for_each_entry_safe(ictx, n, &fl->clst.pending, hn) {
2619 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Tharun Kumar Meruguce566452017-08-17 15:29:59 +05302620 "%s %pK %s %u %s %u %s %u\n",
Sathish Ambley1ca68232017-01-19 10:32:55 -08002621 "smqcontext:", ictx,
2622 "sc:", ictx->sc,
2623 "tid:", ictx->pid,
2624 "handle", ictx->rpra->h);
2625 }
2626 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2627 "\n%s\n",
2628 "LIST OF INTERRUPTED SMQCONTEXTS:");
2629 hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) {
2630 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Tharun Kumar Meruguce566452017-08-17 15:29:59 +05302631 "%s %pK %s %u %s %u %s %u\n",
Sathish Ambley1ca68232017-01-19 10:32:55 -08002632 "smqcontext:", ictx,
2633 "sc:", ictx->sc,
2634 "tid:", ictx->pid,
2635 "handle", ictx->rpra->h);
2636 }
2637 spin_unlock(&fl->hlock);
2638 }
2639 if (len > DEBUGFS_SIZE)
2640 len = DEBUGFS_SIZE;
2641 ret = simple_read_from_buffer(buffer, count, position, fileinfo, len);
2642 kfree(fileinfo);
2643bail:
2644 return ret;
2645}
2646
2647static const struct file_operations debugfs_fops = {
2648 .open = fastrpc_debugfs_open,
2649 .read = fastrpc_debugfs_read,
2650};
Sathish Ambley36849af2017-02-02 09:35:55 -08002651static int fastrpc_channel_open(struct fastrpc_file *fl)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002652{
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002653 struct fastrpc_apps *me = &gfa;
Sathish Ambley36849af2017-02-02 09:35:55 -08002654 int cid, err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002655
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302656 mutex_lock(&me->smd_mutex);
2657
Sathish Ambley36849af2017-02-02 09:35:55 -08002658 VERIFY(err, fl && fl->sctx);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002659 if (err)
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302660 goto bail;
Sathish Ambley36849af2017-02-02 09:35:55 -08002661 cid = fl->cid;
c_mtharu314a4202017-11-15 22:09:17 +05302662 VERIFY(err, cid >= 0 && cid < NUM_CHANNELS);
2663 if (err)
2664 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05302665 if (me->channel[cid].ssrcount !=
2666 me->channel[cid].prevssrcount) {
2667 if (!me->channel[cid].issubsystemup) {
2668 VERIFY(err, 0);
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302669 if (err) {
2670 err = -ENOTCONN;
c_mtharue1a5ce12017-10-13 20:47:09 +05302671 goto bail;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302672 }
c_mtharue1a5ce12017-10-13 20:47:09 +05302673 }
2674 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002675 fl->ssrcount = me->channel[cid].ssrcount;
2676 if ((kref_get_unless_zero(&me->channel[cid].kref) == 0) ||
c_mtharue1a5ce12017-10-13 20:47:09 +05302677 (me->channel[cid].chan == NULL)) {
Tharun Kumar Meruguca0db5262017-05-10 12:53:12 +05302678 VERIFY(err, 0 == fastrpc_glink_register(cid, me));
2679 if (err)
2680 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002681 VERIFY(err, 0 == fastrpc_glink_open(cid));
2682 if (err)
2683 goto bail;
2684
Tharun Kumar Merugu53a8ec92017-07-14 15:52:49 +05302685 VERIFY(err,
2686 wait_for_completion_timeout(&me->channel[cid].workport,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002687 RPC_TIMEOUT));
2688 if (err) {
c_mtharue1a5ce12017-10-13 20:47:09 +05302689 me->channel[cid].chan = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002690 goto bail;
2691 }
2692 kref_init(&me->channel[cid].kref);
2693 pr_info("'opened /dev/%s c %d %d'\n", gcinfo[cid].name,
2694 MAJOR(me->dev_no), cid);
c_mtharu314a4202017-11-15 22:09:17 +05302695 err = glink_queue_rx_intent(me->channel[cid].chan, NULL,
2696 FASTRPC_GLINK_INTENT_LEN);
2697 err |= glink_queue_rx_intent(me->channel[cid].chan, NULL,
2698 FASTRPC_GLINK_INTENT_LEN);
Bruce Levy34c3c1c2017-07-31 17:08:58 -07002699 if (err)
Tharun Kumar Merugu88ba9252017-08-09 12:15:41 +05302700 pr_warn("adsprpc: initial intent fail for %d err %d\n",
2701 cid, err);
Tharun Kumar Merugud86fc8c2018-01-04 16:35:31 +05302702 if (cid == 0 && me->channel[cid].ssrcount !=
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002703 me->channel[cid].prevssrcount) {
c_mtharue1a5ce12017-10-13 20:47:09 +05302704 if (fastrpc_mmap_remove_ssr(fl))
2705 pr_err("ADSPRPC: SSR: Failed to unmap remote heap\n");
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002706 me->channel[cid].prevssrcount =
2707 me->channel[cid].ssrcount;
2708 }
2709 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002710
2711bail:
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302712 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002713 return err;
2714}
2715
Sathish Ambley36849af2017-02-02 09:35:55 -08002716static int fastrpc_device_open(struct inode *inode, struct file *filp)
2717{
2718 int err = 0;
Sathish Ambley567012b2017-03-06 11:55:04 -08002719 struct dentry *debugfs_file;
c_mtharue1a5ce12017-10-13 20:47:09 +05302720 struct fastrpc_file *fl = NULL;
Sathish Ambley36849af2017-02-02 09:35:55 -08002721 struct fastrpc_apps *me = &gfa;
2722
c_mtharue1a5ce12017-10-13 20:47:09 +05302723 VERIFY(err, NULL != (fl = kzalloc(sizeof(*fl), GFP_KERNEL)));
Sathish Ambley36849af2017-02-02 09:35:55 -08002724 if (err)
2725 return err;
Sathish Ambley567012b2017-03-06 11:55:04 -08002726 debugfs_file = debugfs_create_file(current->comm, 0644, debugfs_root,
2727 fl, &debugfs_fops);
Sathish Ambley36849af2017-02-02 09:35:55 -08002728 context_list_ctor(&fl->clst);
2729 spin_lock_init(&fl->hlock);
2730 INIT_HLIST_HEAD(&fl->maps);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302731 INIT_HLIST_HEAD(&fl->perf);
Sathish Ambley36849af2017-02-02 09:35:55 -08002732 INIT_HLIST_HEAD(&fl->bufs);
2733 INIT_HLIST_NODE(&fl->hn);
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05302734 fl->sessionid = 0;
Sathish Ambley36849af2017-02-02 09:35:55 -08002735 fl->tgid = current->tgid;
2736 fl->apps = me;
2737 fl->mode = FASTRPC_MODE_SERIAL;
2738 fl->cid = -1;
Sathish Ambley567012b2017-03-06 11:55:04 -08002739 if (debugfs_file != NULL)
2740 fl->debugfs_file = debugfs_file;
2741 memset(&fl->perf, 0, sizeof(fl->perf));
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05302742 fl->qos_request = 0;
Sathish Ambley36849af2017-02-02 09:35:55 -08002743 filp->private_data = fl;
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302744 mutex_init(&fl->map_mutex);
Sathish Ambley36849af2017-02-02 09:35:55 -08002745 spin_lock(&me->hlock);
2746 hlist_add_head(&fl->hn, &me->drivers);
2747 spin_unlock(&me->hlock);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302748 mutex_init(&fl->perf_mutex);
Sathish Ambley36849af2017-02-02 09:35:55 -08002749 return 0;
2750}
2751
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002752static int fastrpc_get_info(struct fastrpc_file *fl, uint32_t *info)
2753{
2754 int err = 0;
Sathish Ambley36849af2017-02-02 09:35:55 -08002755 uint32_t cid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002756
c_mtharue1a5ce12017-10-13 20:47:09 +05302757 VERIFY(err, fl != NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002758 if (err)
2759 goto bail;
Sathish Ambley36849af2017-02-02 09:35:55 -08002760 if (fl->cid == -1) {
2761 cid = *info;
2762 VERIFY(err, cid < NUM_CHANNELS);
2763 if (err)
2764 goto bail;
2765 fl->cid = cid;
2766 fl->ssrcount = fl->apps->channel[cid].ssrcount;
2767 VERIFY(err, !fastrpc_session_alloc_locked(
2768 &fl->apps->channel[cid], 0, &fl->sctx));
2769 if (err)
2770 goto bail;
2771 }
Tharun Kumar Merugu80be7d62017-08-02 11:03:22 +05302772 VERIFY(err, fl->sctx != NULL);
2773 if (err)
2774 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002775 *info = (fl->sctx->smmu.enabled ? 1 : 0);
2776bail:
2777 return err;
2778}
2779
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05302780static int fastrpc_internal_control(struct fastrpc_file *fl,
2781 struct fastrpc_ioctl_control *cp)
2782{
2783 int err = 0;
2784 int latency;
2785
2786 VERIFY(err, !IS_ERR_OR_NULL(fl) && !IS_ERR_OR_NULL(fl->apps));
2787 if (err)
2788 goto bail;
2789 VERIFY(err, !IS_ERR_OR_NULL(cp));
2790 if (err)
2791 goto bail;
2792
2793 switch (cp->req) {
2794 case FASTRPC_CONTROL_LATENCY:
2795 latency = cp->lp.enable == FASTRPC_LATENCY_CTRL_ENB ?
2796 fl->apps->latency : PM_QOS_DEFAULT_VALUE;
2797 VERIFY(err, latency != 0);
2798 if (err)
2799 goto bail;
2800 if (!fl->qos_request) {
2801 pm_qos_add_request(&fl->pm_qos_req,
2802 PM_QOS_CPU_DMA_LATENCY, latency);
2803 fl->qos_request = 1;
2804 } else
2805 pm_qos_update_request(&fl->pm_qos_req, latency);
2806 break;
2807 default:
2808 err = -ENOTTY;
2809 break;
2810 }
2811bail:
2812 return err;
2813}
2814
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002815static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num,
2816 unsigned long ioctl_param)
2817{
2818 union {
Sathish Ambleybae51902017-07-03 15:00:49 -07002819 struct fastrpc_ioctl_invoke_crc inv;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002820 struct fastrpc_ioctl_mmap mmap;
2821 struct fastrpc_ioctl_munmap munmap;
c_mtharu7bd6a422017-10-17 18:15:37 +05302822 struct fastrpc_ioctl_munmap_fd munmap_fd;
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002823 struct fastrpc_ioctl_init_attrs init;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002824 struct fastrpc_ioctl_perf perf;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05302825 struct fastrpc_ioctl_control cp;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002826 } p;
2827 void *param = (char *)ioctl_param;
2828 struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data;
2829 int size = 0, err = 0;
2830 uint32_t info;
2831
c_mtharue1a5ce12017-10-13 20:47:09 +05302832 p.inv.fds = NULL;
2833 p.inv.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07002834 p.inv.crc = NULL;
tharun kumar9f899ea2017-07-03 17:07:03 +05302835 spin_lock(&fl->hlock);
2836 if (fl->file_close == 1) {
2837 err = EBADF;
2838 pr_warn("ADSPRPC: fastrpc_device_release is happening, So not sending any new requests to DSP");
2839 spin_unlock(&fl->hlock);
2840 goto bail;
2841 }
2842 spin_unlock(&fl->hlock);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002843
2844 switch (ioctl_num) {
2845 case FASTRPC_IOCTL_INVOKE:
2846 size = sizeof(struct fastrpc_ioctl_invoke);
Sathish Ambleybae51902017-07-03 15:00:49 -07002847 /* fall through */
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002848 case FASTRPC_IOCTL_INVOKE_FD:
2849 if (!size)
2850 size = sizeof(struct fastrpc_ioctl_invoke_fd);
2851 /* fall through */
2852 case FASTRPC_IOCTL_INVOKE_ATTRS:
2853 if (!size)
2854 size = sizeof(struct fastrpc_ioctl_invoke_attrs);
Sathish Ambleybae51902017-07-03 15:00:49 -07002855 /* fall through */
2856 case FASTRPC_IOCTL_INVOKE_CRC:
2857 if (!size)
2858 size = sizeof(struct fastrpc_ioctl_invoke_crc);
c_mtharue1a5ce12017-10-13 20:47:09 +05302859 K_COPY_FROM_USER(err, 0, &p.inv, param, size);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002860 if (err)
2861 goto bail;
2862 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, fl->mode,
2863 0, &p.inv)));
2864 if (err)
2865 goto bail;
2866 break;
2867 case FASTRPC_IOCTL_MMAP:
c_mtharue1a5ce12017-10-13 20:47:09 +05302868 K_COPY_FROM_USER(err, 0, &p.mmap, param,
2869 sizeof(p.mmap));
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05302870 if (err)
2871 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002872 VERIFY(err, 0 == (err = fastrpc_internal_mmap(fl, &p.mmap)));
2873 if (err)
2874 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05302875 K_COPY_TO_USER(err, 0, param, &p.mmap, sizeof(p.mmap));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002876 if (err)
2877 goto bail;
2878 break;
2879 case FASTRPC_IOCTL_MUNMAP:
c_mtharue1a5ce12017-10-13 20:47:09 +05302880 K_COPY_FROM_USER(err, 0, &p.munmap, param,
2881 sizeof(p.munmap));
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05302882 if (err)
2883 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002884 VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl,
2885 &p.munmap)));
2886 if (err)
2887 goto bail;
2888 break;
c_mtharu7bd6a422017-10-17 18:15:37 +05302889 case FASTRPC_IOCTL_MUNMAP_FD:
2890 K_COPY_FROM_USER(err, 0, &p.munmap_fd, param,
2891 sizeof(p.munmap_fd));
2892 if (err)
2893 goto bail;
2894 VERIFY(err, 0 == (err = fastrpc_internal_munmap_fd(fl,
2895 &p.munmap_fd)));
2896 if (err)
2897 goto bail;
2898 break;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002899 case FASTRPC_IOCTL_SETMODE:
2900 switch ((uint32_t)ioctl_param) {
2901 case FASTRPC_MODE_PARALLEL:
2902 case FASTRPC_MODE_SERIAL:
2903 fl->mode = (uint32_t)ioctl_param;
2904 break;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002905 case FASTRPC_MODE_PROFILE:
2906 fl->profile = (uint32_t)ioctl_param;
2907 break;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05302908 case FASTRPC_MODE_SESSION:
2909 fl->sessionid = 1;
2910 fl->tgid |= (1 << SESSION_ID_INDEX);
2911 break;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002912 default:
2913 err = -ENOTTY;
2914 break;
2915 }
2916 break;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002917 case FASTRPC_IOCTL_GETPERF:
c_mtharue1a5ce12017-10-13 20:47:09 +05302918 K_COPY_FROM_USER(err, 0, &p.perf,
2919 param, sizeof(p.perf));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002920 if (err)
2921 goto bail;
2922 p.perf.numkeys = sizeof(struct fastrpc_perf)/sizeof(int64_t);
2923 if (p.perf.keys) {
2924 char *keys = PERF_KEYS;
2925
c_mtharue1a5ce12017-10-13 20:47:09 +05302926 K_COPY_TO_USER(err, 0, (void *)p.perf.keys,
2927 keys, strlen(keys)+1);
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002928 if (err)
2929 goto bail;
2930 }
2931 if (p.perf.data) {
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302932 struct fastrpc_perf *perf = NULL, *fperf = NULL;
2933 struct hlist_node *n = NULL;
2934
2935 mutex_lock(&fl->perf_mutex);
2936 hlist_for_each_entry_safe(perf, n, &fl->perf, hn) {
2937 if (perf->tid == current->pid) {
2938 fperf = perf;
2939 break;
2940 }
2941 }
2942
2943 mutex_unlock(&fl->perf_mutex);
2944
2945 if (fperf) {
2946 K_COPY_TO_USER(err, 0, (void *)p.perf.data,
2947 fperf, sizeof(*fperf));
2948 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002949 }
c_mtharue1a5ce12017-10-13 20:47:09 +05302950 K_COPY_TO_USER(err, 0, param, &p.perf, sizeof(p.perf));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002951 if (err)
2952 goto bail;
2953 break;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05302954 case FASTRPC_IOCTL_CONTROL:
c_mtharue1a5ce12017-10-13 20:47:09 +05302955 K_COPY_FROM_USER(err, 0, &p.cp, param,
2956 sizeof(p.cp));
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05302957 if (err)
2958 goto bail;
2959 VERIFY(err, 0 == (err = fastrpc_internal_control(fl, &p.cp)));
2960 if (err)
2961 goto bail;
2962 break;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002963 case FASTRPC_IOCTL_GETINFO:
c_mtharue1a5ce12017-10-13 20:47:09 +05302964 K_COPY_FROM_USER(err, 0, &info, param, sizeof(info));
Sathish Ambley36849af2017-02-02 09:35:55 -08002965 if (err)
2966 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002967 VERIFY(err, 0 == (err = fastrpc_get_info(fl, &info)));
2968 if (err)
2969 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05302970 K_COPY_TO_USER(err, 0, param, &info, sizeof(info));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002971 if (err)
2972 goto bail;
2973 break;
2974 case FASTRPC_IOCTL_INIT:
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002975 p.init.attrs = 0;
2976 p.init.siglen = 0;
2977 size = sizeof(struct fastrpc_ioctl_init);
2978 /* fall through */
2979 case FASTRPC_IOCTL_INIT_ATTRS:
2980 if (!size)
2981 size = sizeof(struct fastrpc_ioctl_init_attrs);
c_mtharue1a5ce12017-10-13 20:47:09 +05302982 K_COPY_FROM_USER(err, 0, &p.init, param, size);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002983 if (err)
2984 goto bail;
Tharun Kumar Merugu4ea0eac2017-08-22 11:42:51 +05302985 VERIFY(err, p.init.init.filelen >= 0 &&
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05302986 p.init.init.filelen < INIT_FILELEN_MAX);
2987 if (err)
2988 goto bail;
2989 VERIFY(err, p.init.init.memlen >= 0 &&
2990 p.init.init.memlen < INIT_MEMLEN_MAX);
Tharun Kumar Merugu4ea0eac2017-08-22 11:42:51 +05302991 if (err)
2992 goto bail;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302993 VERIFY(err, 0 == (err = fastrpc_init_process(fl, &p.init)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002994 if (err)
2995 goto bail;
2996 break;
2997
2998 default:
2999 err = -ENOTTY;
3000 pr_info("bad ioctl: %d\n", ioctl_num);
3001 break;
3002 }
3003 bail:
3004 return err;
3005}
3006
3007static int fastrpc_restart_notifier_cb(struct notifier_block *nb,
3008 unsigned long code,
3009 void *data)
3010{
3011 struct fastrpc_apps *me = &gfa;
3012 struct fastrpc_channel_ctx *ctx;
c_mtharue1a5ce12017-10-13 20:47:09 +05303013 struct notif_data *notifdata = data;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003014 int cid;
3015
3016 ctx = container_of(nb, struct fastrpc_channel_ctx, nb);
3017 cid = ctx - &me->channel[0];
3018 if (code == SUBSYS_BEFORE_SHUTDOWN) {
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303019 mutex_lock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003020 ctx->ssrcount++;
c_mtharue1a5ce12017-10-13 20:47:09 +05303021 ctx->issubsystemup = 0;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303022 if (ctx->chan) {
3023 fastrpc_glink_close(ctx->chan, cid);
3024 ctx->chan = NULL;
3025 pr_info("'restart notifier: closed /dev/%s c %d %d'\n",
3026 gcinfo[cid].name, MAJOR(me->dev_no), cid);
3027 }
3028 mutex_unlock(&me->smd_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05303029 if (cid == 0)
3030 me->staticpd_flags = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003031 fastrpc_notify_drivers(me, cid);
c_mtharue1a5ce12017-10-13 20:47:09 +05303032 } else if (code == SUBSYS_RAMDUMP_NOTIFICATION) {
3033 if (me->channel[0].remoteheap_ramdump_dev &&
3034 notifdata->enable_ramdump) {
3035 me->channel[0].ramdumpenabled = 1;
3036 }
3037 } else if (code == SUBSYS_AFTER_POWERUP) {
3038 ctx->issubsystemup = 1;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003039 }
3040
3041 return NOTIFY_DONE;
3042}
3043
3044static const struct file_operations fops = {
3045 .open = fastrpc_device_open,
3046 .release = fastrpc_device_release,
3047 .unlocked_ioctl = fastrpc_device_ioctl,
3048 .compat_ioctl = compat_fastrpc_device_ioctl,
3049};
3050
3051static const struct of_device_id fastrpc_match_table[] = {
3052 { .compatible = "qcom,msm-fastrpc-adsp", },
3053 { .compatible = "qcom,msm-fastrpc-compute", },
3054 { .compatible = "qcom,msm-fastrpc-compute-cb", },
3055 { .compatible = "qcom,msm-adsprpc-mem-region", },
3056 {}
3057};
3058
3059static int fastrpc_cb_probe(struct device *dev)
3060{
3061 struct fastrpc_channel_ctx *chan;
3062 struct fastrpc_session_ctx *sess;
3063 struct of_phandle_args iommuspec;
3064 const char *name;
3065 unsigned int start = 0x80000000;
3066 int err = 0, i;
3067 int secure_vmid = VMID_CP_PIXEL;
3068
c_mtharue1a5ce12017-10-13 20:47:09 +05303069 VERIFY(err, NULL != (name = of_get_property(dev->of_node,
3070 "label", NULL)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003071 if (err)
3072 goto bail;
3073 for (i = 0; i < NUM_CHANNELS; i++) {
3074 if (!gcinfo[i].name)
3075 continue;
3076 if (!strcmp(name, gcinfo[i].name))
3077 break;
3078 }
3079 VERIFY(err, i < NUM_CHANNELS);
3080 if (err)
3081 goto bail;
3082 chan = &gcinfo[i];
3083 VERIFY(err, chan->sesscount < NUM_SESSIONS);
3084 if (err)
3085 goto bail;
3086
3087 VERIFY(err, !of_parse_phandle_with_args(dev->of_node, "iommus",
3088 "#iommu-cells", 0, &iommuspec));
3089 if (err)
3090 goto bail;
3091 sess = &chan->session[chan->sesscount];
3092 sess->smmu.cb = iommuspec.args[0] & 0xf;
3093 sess->used = 0;
3094 sess->smmu.coherent = of_property_read_bool(dev->of_node,
3095 "dma-coherent");
3096 sess->smmu.secure = of_property_read_bool(dev->of_node,
3097 "qcom,secure-context-bank");
3098 if (sess->smmu.secure)
3099 start = 0x60000000;
3100 VERIFY(err, !IS_ERR_OR_NULL(sess->smmu.mapping =
3101 arm_iommu_create_mapping(&platform_bus_type,
Tharun Kumar Meruguca183f92017-04-27 17:43:27 +05303102 start, 0x78000000)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003103 if (err)
3104 goto bail;
3105
3106 if (sess->smmu.secure)
3107 iommu_domain_set_attr(sess->smmu.mapping->domain,
3108 DOMAIN_ATTR_SECURE_VMID,
3109 &secure_vmid);
3110
3111 VERIFY(err, !arm_iommu_attach_device(dev, sess->smmu.mapping));
3112 if (err)
3113 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05303114 sess->smmu.dev = dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003115 sess->smmu.enabled = 1;
3116 chan->sesscount++;
Sathish Ambley1ca68232017-01-19 10:32:55 -08003117 debugfs_global_file = debugfs_create_file("global", 0644, debugfs_root,
3118 NULL, &debugfs_fops);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003119bail:
3120 return err;
3121}
3122
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05303123static void init_secure_vmid_list(struct device *dev, char *prop_name,
3124 struct secure_vm *destvm)
3125{
3126 int err = 0;
3127 u32 len = 0, i = 0;
3128 u32 *rhvmlist = NULL;
3129 u32 *rhvmpermlist = NULL;
3130
3131 if (!of_find_property(dev->of_node, prop_name, &len))
3132 goto bail;
3133 if (len == 0)
3134 goto bail;
3135 len /= sizeof(u32);
3136 VERIFY(err, NULL != (rhvmlist = kcalloc(len, sizeof(u32), GFP_KERNEL)));
3137 if (err)
3138 goto bail;
3139 VERIFY(err, NULL != (rhvmpermlist = kcalloc(len, sizeof(u32),
3140 GFP_KERNEL)));
3141 if (err)
3142 goto bail;
3143 for (i = 0; i < len; i++) {
3144 err = of_property_read_u32_index(dev->of_node, prop_name, i,
3145 &rhvmlist[i]);
3146 rhvmpermlist[i] = PERM_READ | PERM_WRITE | PERM_EXEC;
3147 pr_info("ADSPRPC: Secure VMID = %d", rhvmlist[i]);
3148 if (err) {
3149 pr_err("ADSPRPC: Failed to read VMID\n");
3150 goto bail;
3151 }
3152 }
3153 destvm->vmid = rhvmlist;
3154 destvm->vmperm = rhvmpermlist;
3155 destvm->vmcount = len;
3156bail:
3157 if (err) {
3158 kfree(rhvmlist);
3159 kfree(rhvmpermlist);
3160 }
3161}
3162
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003163static int fastrpc_probe(struct platform_device *pdev)
3164{
3165 int err = 0;
3166 struct fastrpc_apps *me = &gfa;
3167 struct device *dev = &pdev->dev;
3168 struct smq_phy_page range;
3169 struct device_node *ion_node, *node;
3170 struct platform_device *ion_pdev;
3171 struct cma *cma;
3172 uint32_t val;
3173
c_mtharu63ffc012017-11-16 15:26:56 +05303174
3175 if (of_device_is_compatible(dev->of_node,
3176 "qcom,msm-fastrpc-compute")) {
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05303177 init_secure_vmid_list(dev, "qcom,adsp-remoteheap-vmid",
3178 &gcinfo[0].rhvm);
c_mtharu63ffc012017-11-16 15:26:56 +05303179
c_mtharu63ffc012017-11-16 15:26:56 +05303180
3181 of_property_read_u32(dev->of_node, "qcom,rpc-latency-us",
3182 &me->latency);
3183 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003184 if (of_device_is_compatible(dev->of_node,
3185 "qcom,msm-fastrpc-compute-cb"))
3186 return fastrpc_cb_probe(dev);
3187
3188 if (of_device_is_compatible(dev->of_node,
3189 "qcom,msm-adsprpc-mem-region")) {
3190 me->dev = dev;
3191 range.addr = 0;
3192 ion_node = of_find_compatible_node(NULL, NULL, "qcom,msm-ion");
3193 if (ion_node) {
3194 for_each_available_child_of_node(ion_node, node) {
3195 if (of_property_read_u32(node, "reg", &val))
3196 continue;
3197 if (val != ION_ADSP_HEAP_ID)
3198 continue;
3199 ion_pdev = of_find_device_by_node(node);
3200 if (!ion_pdev)
3201 break;
3202 cma = dev_get_cma_area(&ion_pdev->dev);
3203 if (cma) {
3204 range.addr = cma_get_base(cma);
3205 range.size = (size_t)cma_get_size(cma);
3206 }
3207 break;
3208 }
3209 }
3210 if (range.addr) {
3211 int srcVM[1] = {VMID_HLOS};
3212 int destVM[4] = {VMID_HLOS, VMID_MSS_MSA, VMID_SSC_Q6,
3213 VMID_ADSP_Q6};
Sathish Ambley84d11862017-05-15 14:36:05 -07003214 int destVMperm[4] = {PERM_READ | PERM_WRITE | PERM_EXEC,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003215 PERM_READ | PERM_WRITE | PERM_EXEC,
3216 PERM_READ | PERM_WRITE | PERM_EXEC,
3217 PERM_READ | PERM_WRITE | PERM_EXEC,
3218 };
3219
3220 VERIFY(err, !hyp_assign_phys(range.addr, range.size,
3221 srcVM, 1, destVM, destVMperm, 4));
3222 if (err)
3223 goto bail;
3224 }
3225 return 0;
3226 }
3227
3228 VERIFY(err, !of_platform_populate(pdev->dev.of_node,
3229 fastrpc_match_table,
3230 NULL, &pdev->dev));
3231 if (err)
3232 goto bail;
3233bail:
3234 return err;
3235}
3236
3237static void fastrpc_deinit(void)
3238{
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303239 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003240 struct fastrpc_channel_ctx *chan = gcinfo;
3241 int i, j;
3242
3243 for (i = 0; i < NUM_CHANNELS; i++, chan++) {
3244 if (chan->chan) {
3245 kref_put_mutex(&chan->kref,
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303246 fastrpc_channel_close, &me->smd_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05303247 chan->chan = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003248 }
3249 for (j = 0; j < NUM_SESSIONS; j++) {
3250 struct fastrpc_session_ctx *sess = &chan->session[j];
c_mtharue1a5ce12017-10-13 20:47:09 +05303251 if (sess->smmu.dev) {
3252 arm_iommu_detach_device(sess->smmu.dev);
3253 sess->smmu.dev = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003254 }
3255 if (sess->smmu.mapping) {
3256 arm_iommu_release_mapping(sess->smmu.mapping);
c_mtharue1a5ce12017-10-13 20:47:09 +05303257 sess->smmu.mapping = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003258 }
3259 }
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05303260 kfree(chan->rhvm.vmid);
3261 kfree(chan->rhvm.vmperm);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003262 }
3263}
3264
3265static struct platform_driver fastrpc_driver = {
3266 .probe = fastrpc_probe,
3267 .driver = {
3268 .name = "fastrpc",
3269 .owner = THIS_MODULE,
3270 .of_match_table = fastrpc_match_table,
3271 },
3272};
3273
3274static int __init fastrpc_device_init(void)
3275{
3276 struct fastrpc_apps *me = &gfa;
c_mtharue1a5ce12017-10-13 20:47:09 +05303277 struct device *dev = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003278 int err = 0, i;
3279
3280 memset(me, 0, sizeof(*me));
3281
3282 fastrpc_init(me);
3283 me->dev = NULL;
3284 VERIFY(err, 0 == platform_driver_register(&fastrpc_driver));
3285 if (err)
3286 goto register_bail;
3287 VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, NUM_CHANNELS,
3288 DEVICE_NAME));
3289 if (err)
3290 goto alloc_chrdev_bail;
3291 cdev_init(&me->cdev, &fops);
3292 me->cdev.owner = THIS_MODULE;
3293 VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0),
Sathish Ambley36849af2017-02-02 09:35:55 -08003294 1));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003295 if (err)
3296 goto cdev_init_bail;
3297 me->class = class_create(THIS_MODULE, "fastrpc");
3298 VERIFY(err, !IS_ERR(me->class));
3299 if (err)
3300 goto class_create_bail;
3301 me->compat = (fops.compat_ioctl == NULL) ? 0 : 1;
Sathish Ambley36849af2017-02-02 09:35:55 -08003302 dev = device_create(me->class, NULL,
3303 MKDEV(MAJOR(me->dev_no), 0),
3304 NULL, gcinfo[0].name);
3305 VERIFY(err, !IS_ERR_OR_NULL(dev));
3306 if (err)
3307 goto device_create_bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003308 for (i = 0; i < NUM_CHANNELS; i++) {
Sathish Ambley36849af2017-02-02 09:35:55 -08003309 me->channel[i].dev = dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003310 me->channel[i].ssrcount = 0;
3311 me->channel[i].prevssrcount = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05303312 me->channel[i].issubsystemup = 1;
3313 me->channel[i].ramdumpenabled = 0;
3314 me->channel[i].remoteheap_ramdump_dev = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003315 me->channel[i].nb.notifier_call = fastrpc_restart_notifier_cb;
3316 me->channel[i].handle = subsys_notif_register_notifier(
3317 gcinfo[i].subsys,
3318 &me->channel[i].nb);
3319 }
3320
3321 me->client = msm_ion_client_create(DEVICE_NAME);
3322 VERIFY(err, !IS_ERR_OR_NULL(me->client));
3323 if (err)
3324 goto device_create_bail;
Sathish Ambley1ca68232017-01-19 10:32:55 -08003325 debugfs_root = debugfs_create_dir("adsprpc", NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003326 return 0;
3327device_create_bail:
3328 for (i = 0; i < NUM_CHANNELS; i++) {
Sathish Ambley36849af2017-02-02 09:35:55 -08003329 if (me->channel[i].handle)
3330 subsys_notif_unregister_notifier(me->channel[i].handle,
3331 &me->channel[i].nb);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003332 }
Sathish Ambley36849af2017-02-02 09:35:55 -08003333 if (!IS_ERR_OR_NULL(dev))
3334 device_destroy(me->class, MKDEV(MAJOR(me->dev_no), 0));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003335 class_destroy(me->class);
3336class_create_bail:
3337 cdev_del(&me->cdev);
3338cdev_init_bail:
3339 unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
3340alloc_chrdev_bail:
3341register_bail:
3342 fastrpc_deinit();
3343 return err;
3344}
3345
3346static void __exit fastrpc_device_exit(void)
3347{
3348 struct fastrpc_apps *me = &gfa;
3349 int i;
3350
3351 fastrpc_file_list_dtor(me);
3352 fastrpc_deinit();
3353 for (i = 0; i < NUM_CHANNELS; i++) {
3354 if (!gcinfo[i].name)
3355 continue;
3356 device_destroy(me->class, MKDEV(MAJOR(me->dev_no), i));
3357 subsys_notif_unregister_notifier(me->channel[i].handle,
3358 &me->channel[i].nb);
3359 }
3360 class_destroy(me->class);
3361 cdev_del(&me->cdev);
3362 unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
3363 ion_client_destroy(me->client);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003364 debugfs_remove_recursive(debugfs_root);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003365}
3366
3367late_initcall(fastrpc_device_init);
3368module_exit(fastrpc_device_exit);
3369
3370MODULE_LICENSE("GPL v2");