blob: 5cf41d5247adc7ceff7c619cafa88e2bf1a71987 [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;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +0530345 struct mutex fl_map_mutex;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700346};
347
348static struct fastrpc_apps gfa;
349
350static struct fastrpc_channel_ctx gcinfo[NUM_CHANNELS] = {
351 {
352 .name = "adsprpc-smd",
353 .subsys = "adsp",
354 .link.link_info.edge = "lpass",
355 .link.link_info.transport = "smem",
356 },
357 {
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700358 .name = "mdsprpc-smd",
359 .subsys = "modem",
360 .link.link_info.edge = "mpss",
361 .link.link_info.transport = "smem",
362 },
363 {
Sathish Ambley36849af2017-02-02 09:35:55 -0800364 .name = "sdsprpc-smd",
365 .subsys = "slpi",
366 .link.link_info.edge = "dsps",
367 .link.link_info.transport = "smem",
Sathish Ambley36849af2017-02-02 09:35:55 -0800368 },
369 {
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700370 .name = "cdsprpc-smd",
371 .subsys = "cdsp",
372 .link.link_info.edge = "cdsp",
373 .link.link_info.transport = "smem",
374 },
375};
376
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +0530377static int hlosvm[1] = {VMID_HLOS};
378static int hlosvmperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
379
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800380static inline int64_t getnstimediff(struct timespec *start)
381{
382 int64_t ns;
383 struct timespec ts, b;
384
385 getnstimeofday(&ts);
386 b = timespec_sub(ts, *start);
387 ns = timespec_to_ns(&b);
388 return ns;
389}
390
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530391static inline int64_t *getperfcounter(struct fastrpc_file *fl, int key)
392{
393 int err = 0;
394 int64_t *val = NULL;
395 struct fastrpc_perf *perf = NULL, *fperf = NULL;
396 struct hlist_node *n = NULL;
397
398 VERIFY(err, !IS_ERR_OR_NULL(fl));
399 if (err)
400 goto bail;
401
402 mutex_lock(&fl->perf_mutex);
403 hlist_for_each_entry_safe(perf, n, &fl->perf, hn) {
404 if (perf->tid == current->pid) {
405 fperf = perf;
406 break;
407 }
408 }
409
410 if (IS_ERR_OR_NULL(fperf)) {
411 fperf = kzalloc(sizeof(*fperf), GFP_KERNEL);
412
413 VERIFY(err, !IS_ERR_OR_NULL(fperf));
414 if (err) {
415 mutex_unlock(&fl->perf_mutex);
416 kfree(fperf);
417 goto bail;
418 }
419
420 fperf->tid = current->pid;
421 hlist_add_head(&fperf->hn, &fl->perf);
422 }
423
424 val = ((int64_t *)fperf) + key;
425 mutex_unlock(&fl->perf_mutex);
426bail:
427 return val;
428}
429
430
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700431static void fastrpc_buf_free(struct fastrpc_buf *buf, int cache)
432{
c_mtharue1a5ce12017-10-13 20:47:09 +0530433 struct fastrpc_file *fl = buf == NULL ? NULL : buf->fl;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700434 int vmid;
435
436 if (!fl)
437 return;
438 if (cache) {
439 spin_lock(&fl->hlock);
440 hlist_add_head(&buf->hn, &fl->bufs);
441 spin_unlock(&fl->hlock);
442 return;
443 }
444 if (!IS_ERR_OR_NULL(buf->virt)) {
445 int destVM[1] = {VMID_HLOS};
446 int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
447
448 if (fl->sctx->smmu.cb)
449 buf->phys &= ~((uint64_t)fl->sctx->smmu.cb << 32);
450 vmid = fl->apps->channel[fl->cid].vmid;
451 if (vmid) {
452 int srcVM[2] = {VMID_HLOS, vmid};
453
454 hyp_assign_phys(buf->phys, buf_page_size(buf->size),
455 srcVM, 2, destVM, destVMperm, 1);
456 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530457 dma_free_coherent(fl->sctx->smmu.dev, buf->size, buf->virt,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700458 buf->phys);
459 }
460 kfree(buf);
461}
462
463static void fastrpc_buf_list_free(struct fastrpc_file *fl)
464{
465 struct fastrpc_buf *buf, *free;
466
467 do {
468 struct hlist_node *n;
469
c_mtharue1a5ce12017-10-13 20:47:09 +0530470 free = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700471 spin_lock(&fl->hlock);
472 hlist_for_each_entry_safe(buf, n, &fl->bufs, hn) {
473 hlist_del_init(&buf->hn);
474 free = buf;
475 break;
476 }
477 spin_unlock(&fl->hlock);
478 if (free)
479 fastrpc_buf_free(free, 0);
480 } while (free);
481}
482
483static void fastrpc_mmap_add(struct fastrpc_mmap *map)
484{
c_mtharue1a5ce12017-10-13 20:47:09 +0530485 if (map->flags == ADSP_MMAP_HEAP_ADDR ||
486 map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
487 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700488
c_mtharue1a5ce12017-10-13 20:47:09 +0530489 spin_lock(&me->hlock);
490 hlist_add_head(&map->hn, &me->maps);
491 spin_unlock(&me->hlock);
492 } else {
493 struct fastrpc_file *fl = map->fl;
494
c_mtharue1a5ce12017-10-13 20:47:09 +0530495 hlist_add_head(&map->hn, &fl->maps);
c_mtharue1a5ce12017-10-13 20:47:09 +0530496 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700497}
498
c_mtharue1a5ce12017-10-13 20:47:09 +0530499static int fastrpc_mmap_find(struct fastrpc_file *fl, int fd,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530500 uintptr_t va, size_t len, int mflags, int refs,
c_mtharue1a5ce12017-10-13 20:47:09 +0530501 struct fastrpc_mmap **ppmap)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700502{
c_mtharue1a5ce12017-10-13 20:47:09 +0530503 struct fastrpc_apps *me = &gfa;
504 struct fastrpc_mmap *match = NULL, *map = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700505 struct hlist_node *n;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530506
507 if ((va + len) < va)
508 return -EOVERFLOW;
c_mtharue1a5ce12017-10-13 20:47:09 +0530509 if (mflags == ADSP_MMAP_HEAP_ADDR ||
510 mflags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
511 spin_lock(&me->hlock);
512 hlist_for_each_entry_safe(map, n, &me->maps, hn) {
513 if (va >= map->va &&
514 va + len <= map->va + map->len &&
515 map->fd == fd) {
516 if (refs)
517 map->refs++;
518 match = map;
519 break;
520 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700521 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530522 spin_unlock(&me->hlock);
523 } else {
c_mtharue1a5ce12017-10-13 20:47:09 +0530524 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
525 if (va >= map->va &&
526 va + len <= map->va + map->len &&
527 map->fd == fd) {
528 if (refs)
529 map->refs++;
530 match = map;
531 break;
532 }
533 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700534 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700535 if (match) {
536 *ppmap = match;
537 return 0;
538 }
539 return -ENOTTY;
540}
541
c_mtharuf931ff92017-11-30 19:35:30 +0530542static int dma_alloc_memory(dma_addr_t *region_phys, void **vaddr, size_t size)
c_mtharue1a5ce12017-10-13 20:47:09 +0530543{
544 struct fastrpc_apps *me = &gfa;
c_mtharue1a5ce12017-10-13 20:47:09 +0530545
546 if (me->dev == NULL) {
547 pr_err("device adsprpc-mem is not initialized\n");
548 return -ENODEV;
549 }
c_mtharuf931ff92017-11-30 19:35:30 +0530550 *vaddr = dma_alloc_coherent(me->dev, size, region_phys, GFP_KERNEL);
551 if (!*vaddr) {
c_mtharue1a5ce12017-10-13 20:47:09 +0530552 pr_err("ADSPRPC: Failed to allocate %x remote heap memory\n",
553 (unsigned int)size);
554 return -ENOMEM;
555 }
556 return 0;
557}
558
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700559static int fastrpc_mmap_remove(struct fastrpc_file *fl, uintptr_t va,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530560 size_t len, struct fastrpc_mmap **ppmap)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700561{
c_mtharue1a5ce12017-10-13 20:47:09 +0530562 struct fastrpc_mmap *match = NULL, *map;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700563 struct hlist_node *n;
564 struct fastrpc_apps *me = &gfa;
565
566 spin_lock(&me->hlock);
567 hlist_for_each_entry_safe(map, n, &me->maps, hn) {
568 if (map->raddr == va &&
569 map->raddr + map->len == va + len &&
570 map->refs == 1) {
571 match = map;
572 hlist_del_init(&map->hn);
573 break;
574 }
575 }
576 spin_unlock(&me->hlock);
577 if (match) {
578 *ppmap = match;
579 return 0;
580 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700581 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
582 if (map->raddr == va &&
583 map->raddr + map->len == va + len &&
584 map->refs == 1) {
585 match = map;
586 hlist_del_init(&map->hn);
587 break;
588 }
589 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700590 if (match) {
591 *ppmap = match;
592 return 0;
593 }
594 return -ENOTTY;
595}
596
c_mtharu7bd6a422017-10-17 18:15:37 +0530597static void fastrpc_mmap_free(struct fastrpc_mmap *map, uint32_t flags)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700598{
c_mtharue1a5ce12017-10-13 20:47:09 +0530599 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700600 struct fastrpc_file *fl;
601 int vmid;
602 struct fastrpc_session_ctx *sess;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700603
604 if (!map)
605 return;
606 fl = map->fl;
c_mtharue1a5ce12017-10-13 20:47:09 +0530607 if (map->flags == ADSP_MMAP_HEAP_ADDR ||
608 map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
609 spin_lock(&me->hlock);
610 map->refs--;
611 if (!map->refs)
612 hlist_del_init(&map->hn);
613 spin_unlock(&me->hlock);
c_mtharu7bd6a422017-10-17 18:15:37 +0530614 if (map->refs > 0)
615 return;
c_mtharue1a5ce12017-10-13 20:47:09 +0530616 } else {
c_mtharue1a5ce12017-10-13 20:47:09 +0530617 map->refs--;
618 if (!map->refs)
619 hlist_del_init(&map->hn);
c_mtharu7bd6a422017-10-17 18:15:37 +0530620 if (map->refs > 0 && !flags)
621 return;
c_mtharue1a5ce12017-10-13 20:47:09 +0530622 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530623 if (map->flags == ADSP_MMAP_HEAP_ADDR ||
624 map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700625
c_mtharue1a5ce12017-10-13 20:47:09 +0530626 if (me->dev == NULL) {
627 pr_err("failed to free remote heap allocation\n");
628 return;
629 }
630 if (map->phys) {
631 dma_free_coherent(me->dev, map->size,
c_mtharuf931ff92017-11-30 19:35:30 +0530632 (void *)map->va, (dma_addr_t)map->phys);
c_mtharue1a5ce12017-10-13 20:47:09 +0530633 }
634 } else {
635 int destVM[1] = {VMID_HLOS};
636 int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
637
638 if (map->secure)
639 sess = fl->secsctx;
640 else
641 sess = fl->sctx;
642
643 if (!IS_ERR_OR_NULL(map->handle))
644 ion_free(fl->apps->client, map->handle);
645 if (sess && sess->smmu.enabled) {
646 if (map->size || map->phys)
647 msm_dma_unmap_sg(sess->smmu.dev,
648 map->table->sgl,
649 map->table->nents, DMA_BIDIRECTIONAL,
650 map->buf);
651 }
652 vmid = fl->apps->channel[fl->cid].vmid;
653 if (vmid && map->phys) {
654 int srcVM[2] = {VMID_HLOS, vmid};
655
656 hyp_assign_phys(map->phys, buf_page_size(map->size),
657 srcVM, 2, destVM, destVMperm, 1);
658 }
659
660 if (!IS_ERR_OR_NULL(map->table))
661 dma_buf_unmap_attachment(map->attach, map->table,
662 DMA_BIDIRECTIONAL);
663 if (!IS_ERR_OR_NULL(map->attach))
664 dma_buf_detach(map->buf, map->attach);
665 if (!IS_ERR_OR_NULL(map->buf))
666 dma_buf_put(map->buf);
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700667 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700668 kfree(map);
669}
670
671static int fastrpc_session_alloc(struct fastrpc_channel_ctx *chan, int secure,
672 struct fastrpc_session_ctx **session);
673
674static int fastrpc_mmap_create(struct fastrpc_file *fl, int fd,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530675 unsigned int attr, uintptr_t va, size_t len, int mflags,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700676 struct fastrpc_mmap **ppmap)
677{
c_mtharue1a5ce12017-10-13 20:47:09 +0530678 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700679 struct fastrpc_session_ctx *sess;
680 struct fastrpc_apps *apps = fl->apps;
681 int cid = fl->cid;
682 struct fastrpc_channel_ctx *chan = &apps->channel[cid];
c_mtharue1a5ce12017-10-13 20:47:09 +0530683 struct fastrpc_mmap *map = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700684 unsigned long attrs;
c_mtharuf931ff92017-11-30 19:35:30 +0530685 dma_addr_t region_phys = 0;
686 void *region_vaddr = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700687 unsigned long flags;
688 int err = 0, vmid;
689
Sathish Ambleyae5ee542017-01-16 22:24:23 -0800690 if (!fastrpc_mmap_find(fl, fd, va, len, mflags, 1, ppmap))
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700691 return 0;
692 map = kzalloc(sizeof(*map), GFP_KERNEL);
693 VERIFY(err, !IS_ERR_OR_NULL(map));
694 if (err)
695 goto bail;
696 INIT_HLIST_NODE(&map->hn);
697 map->flags = mflags;
698 map->refs = 1;
699 map->fl = fl;
700 map->fd = fd;
701 map->attr = attr;
c_mtharue1a5ce12017-10-13 20:47:09 +0530702 if (mflags == ADSP_MMAP_HEAP_ADDR ||
703 mflags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
704 map->apps = me;
705 map->fl = NULL;
c_mtharuf931ff92017-11-30 19:35:30 +0530706 VERIFY(err, !dma_alloc_memory(&region_phys, &region_vaddr,
707 len));
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700708 if (err)
709 goto bail;
c_mtharuf931ff92017-11-30 19:35:30 +0530710 map->phys = (uintptr_t)region_phys;
c_mtharue1a5ce12017-10-13 20:47:09 +0530711 map->size = len;
c_mtharuf931ff92017-11-30 19:35:30 +0530712 map->va = (uintptr_t)region_vaddr;
c_mtharue1a5ce12017-10-13 20:47:09 +0530713 } else {
c_mtharu7bd6a422017-10-17 18:15:37 +0530714 if (map->attr && (map->attr & FASTRPC_ATTR_KEEP_MAP)) {
715 pr_info("adsprpc: buffer mapped with persist attr %x\n",
716 (unsigned int)map->attr);
717 map->refs = 2;
718 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530719 VERIFY(err, !IS_ERR_OR_NULL(map->handle =
720 ion_import_dma_buf_fd(fl->apps->client, fd)));
721 if (err)
722 goto bail;
723 VERIFY(err, !ion_handle_get_flags(fl->apps->client, map->handle,
724 &flags));
725 if (err)
726 goto bail;
727
c_mtharue1a5ce12017-10-13 20:47:09 +0530728 map->secure = flags & ION_FLAG_SECURE;
729 if (map->secure) {
730 if (!fl->secsctx)
731 err = fastrpc_session_alloc(chan, 1,
732 &fl->secsctx);
733 if (err)
734 goto bail;
735 }
736 if (map->secure)
737 sess = fl->secsctx;
738 else
739 sess = fl->sctx;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +0530740
c_mtharue1a5ce12017-10-13 20:47:09 +0530741 VERIFY(err, !IS_ERR_OR_NULL(sess));
742 if (err)
743 goto bail;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +0530744
745 map->uncached = !ION_IS_CACHED(flags);
746 if (map->attr & FASTRPC_ATTR_NOVA && !sess->smmu.coherent)
747 map->uncached = 1;
748
c_mtharue1a5ce12017-10-13 20:47:09 +0530749 VERIFY(err, !IS_ERR_OR_NULL(map->buf = dma_buf_get(fd)));
750 if (err)
751 goto bail;
752 VERIFY(err, !IS_ERR_OR_NULL(map->attach =
753 dma_buf_attach(map->buf, sess->smmu.dev)));
754 if (err)
755 goto bail;
756 VERIFY(err, !IS_ERR_OR_NULL(map->table =
757 dma_buf_map_attachment(map->attach,
758 DMA_BIDIRECTIONAL)));
759 if (err)
760 goto bail;
761 if (sess->smmu.enabled) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700762 attrs = DMA_ATTR_EXEC_MAPPING;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +0530763
764 if (map->attr & FASTRPC_ATTR_NON_COHERENT ||
765 (sess->smmu.coherent && map->uncached))
766 attrs |= DMA_ATTR_FORCE_NON_COHERENT;
767 else if (map->attr & FASTRPC_ATTR_COHERENT)
768 attrs |= DMA_ATTR_FORCE_COHERENT;
769
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700770 VERIFY(err, map->table->nents ==
c_mtharue1a5ce12017-10-13 20:47:09 +0530771 msm_dma_map_sg_attrs(sess->smmu.dev,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700772 map->table->sgl, map->table->nents,
773 DMA_BIDIRECTIONAL, map->buf, attrs));
c_mtharue1a5ce12017-10-13 20:47:09 +0530774 if (err)
775 goto bail;
776 } else {
777 VERIFY(err, map->table->nents == 1);
778 if (err)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700779 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +0530780 }
781 map->phys = sg_dma_address(map->table->sgl);
782 if (sess->smmu.cb) {
783 map->phys += ((uint64_t)sess->smmu.cb << 32);
784 map->size = sg_dma_len(map->table->sgl);
785 } else {
786 map->size = buf_page_size(len);
787 }
788 vmid = fl->apps->channel[fl->cid].vmid;
789 if (vmid) {
790 int srcVM[1] = {VMID_HLOS};
791 int destVM[2] = {VMID_HLOS, vmid};
792 int destVMperm[2] = {PERM_READ | PERM_WRITE,
793 PERM_READ | PERM_WRITE | PERM_EXEC};
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700794
c_mtharue1a5ce12017-10-13 20:47:09 +0530795 VERIFY(err, !hyp_assign_phys(map->phys,
796 buf_page_size(map->size),
797 srcVM, 1, destVM, destVMperm, 2));
798 if (err)
799 goto bail;
800 }
801 map->va = va;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700802 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700803 map->len = len;
804
805 fastrpc_mmap_add(map);
806 *ppmap = map;
807
808bail:
809 if (err && map)
c_mtharu7bd6a422017-10-17 18:15:37 +0530810 fastrpc_mmap_free(map, 0);
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700811 return err;
812}
813
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530814static int fastrpc_buf_alloc(struct fastrpc_file *fl, size_t size,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700815 struct fastrpc_buf **obuf)
816{
817 int err = 0, vmid;
c_mtharue1a5ce12017-10-13 20:47:09 +0530818 struct fastrpc_buf *buf = NULL, *fr = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700819 struct hlist_node *n;
820
821 VERIFY(err, size > 0);
822 if (err)
823 goto bail;
824
825 /* find the smallest buffer that fits in the cache */
826 spin_lock(&fl->hlock);
827 hlist_for_each_entry_safe(buf, n, &fl->bufs, hn) {
828 if (buf->size >= size && (!fr || fr->size > buf->size))
829 fr = buf;
830 }
831 if (fr)
832 hlist_del_init(&fr->hn);
833 spin_unlock(&fl->hlock);
834 if (fr) {
835 *obuf = fr;
836 return 0;
837 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530838 buf = NULL;
839 VERIFY(err, NULL != (buf = kzalloc(sizeof(*buf), GFP_KERNEL)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700840 if (err)
841 goto bail;
842 INIT_HLIST_NODE(&buf->hn);
843 buf->fl = fl;
c_mtharue1a5ce12017-10-13 20:47:09 +0530844 buf->virt = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700845 buf->phys = 0;
846 buf->size = size;
c_mtharue1a5ce12017-10-13 20:47:09 +0530847 buf->virt = dma_alloc_coherent(fl->sctx->smmu.dev, buf->size,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700848 (void *)&buf->phys, GFP_KERNEL);
849 if (IS_ERR_OR_NULL(buf->virt)) {
850 /* free cache and retry */
851 fastrpc_buf_list_free(fl);
c_mtharue1a5ce12017-10-13 20:47:09 +0530852 buf->virt = dma_alloc_coherent(fl->sctx->smmu.dev, buf->size,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700853 (void *)&buf->phys, GFP_KERNEL);
854 VERIFY(err, !IS_ERR_OR_NULL(buf->virt));
855 }
856 if (err)
857 goto bail;
858 if (fl->sctx->smmu.cb)
859 buf->phys += ((uint64_t)fl->sctx->smmu.cb << 32);
860 vmid = fl->apps->channel[fl->cid].vmid;
861 if (vmid) {
862 int srcVM[1] = {VMID_HLOS};
863 int destVM[2] = {VMID_HLOS, vmid};
864 int destVMperm[2] = {PERM_READ | PERM_WRITE,
865 PERM_READ | PERM_WRITE | PERM_EXEC};
866
867 VERIFY(err, !hyp_assign_phys(buf->phys, buf_page_size(size),
868 srcVM, 1, destVM, destVMperm, 2));
869 if (err)
870 goto bail;
871 }
872
873 *obuf = buf;
874 bail:
875 if (err && buf)
876 fastrpc_buf_free(buf, 0);
877 return err;
878}
879
880
881static int context_restore_interrupted(struct fastrpc_file *fl,
Sathish Ambleybae51902017-07-03 15:00:49 -0700882 struct fastrpc_ioctl_invoke_crc *inv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700883 struct smq_invoke_ctx **po)
884{
885 int err = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +0530886 struct smq_invoke_ctx *ctx = NULL, *ictx = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700887 struct hlist_node *n;
888 struct fastrpc_ioctl_invoke *invoke = &inv->inv;
889
890 spin_lock(&fl->hlock);
891 hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) {
892 if (ictx->pid == current->pid) {
893 if (invoke->sc != ictx->sc || ictx->fl != fl)
894 err = -1;
895 else {
896 ctx = ictx;
897 hlist_del_init(&ctx->hn);
898 hlist_add_head(&ctx->hn, &fl->clst.pending);
899 }
900 break;
901 }
902 }
903 spin_unlock(&fl->hlock);
904 if (ctx)
905 *po = ctx;
906 return err;
907}
908
909#define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)
910static int overlap_ptr_cmp(const void *a, const void *b)
911{
912 struct overlap *pa = *((struct overlap **)a);
913 struct overlap *pb = *((struct overlap **)b);
914 /* sort with lowest starting buffer first */
915 int st = CMP(pa->start, pb->start);
916 /* sort with highest ending buffer first */
917 int ed = CMP(pb->end, pa->end);
918 return st == 0 ? ed : st;
919}
920
Sathish Ambley9466d672017-01-25 10:51:55 -0800921static int context_build_overlap(struct smq_invoke_ctx *ctx)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700922{
Sathish Ambley9466d672017-01-25 10:51:55 -0800923 int i, err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700924 remote_arg_t *lpra = ctx->lpra;
925 int inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
926 int outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc);
927 int nbufs = inbufs + outbufs;
928 struct overlap max;
929
930 for (i = 0; i < nbufs; ++i) {
931 ctx->overs[i].start = (uintptr_t)lpra[i].buf.pv;
932 ctx->overs[i].end = ctx->overs[i].start + lpra[i].buf.len;
Sathish Ambley9466d672017-01-25 10:51:55 -0800933 if (lpra[i].buf.len) {
934 VERIFY(err, ctx->overs[i].end > ctx->overs[i].start);
935 if (err)
936 goto bail;
937 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700938 ctx->overs[i].raix = i;
939 ctx->overps[i] = &ctx->overs[i];
940 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530941 sort(ctx->overps, nbufs, sizeof(*ctx->overps), overlap_ptr_cmp, NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700942 max.start = 0;
943 max.end = 0;
944 for (i = 0; i < nbufs; ++i) {
945 if (ctx->overps[i]->start < max.end) {
946 ctx->overps[i]->mstart = max.end;
947 ctx->overps[i]->mend = ctx->overps[i]->end;
948 ctx->overps[i]->offset = max.end -
949 ctx->overps[i]->start;
950 if (ctx->overps[i]->end > max.end) {
951 max.end = ctx->overps[i]->end;
952 } else {
953 ctx->overps[i]->mend = 0;
954 ctx->overps[i]->mstart = 0;
955 }
956 } else {
957 ctx->overps[i]->mend = ctx->overps[i]->end;
958 ctx->overps[i]->mstart = ctx->overps[i]->start;
959 ctx->overps[i]->offset = 0;
960 max = *ctx->overps[i];
961 }
962 }
Sathish Ambley9466d672017-01-25 10:51:55 -0800963bail:
964 return err;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700965}
966
967#define K_COPY_FROM_USER(err, kernel, dst, src, size) \
968 do {\
969 if (!(kernel))\
c_mtharue1a5ce12017-10-13 20:47:09 +0530970 VERIFY(err, 0 == copy_from_user((dst),\
971 (void const __user *)(src),\
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700972 (size)));\
973 else\
974 memmove((dst), (src), (size));\
975 } while (0)
976
977#define K_COPY_TO_USER(err, kernel, dst, src, size) \
978 do {\
979 if (!(kernel))\
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530980 VERIFY(err, 0 == copy_to_user((void __user *)(dst),\
c_mtharue1a5ce12017-10-13 20:47:09 +0530981 (src), (size)));\
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700982 else\
983 memmove((dst), (src), (size));\
984 } while (0)
985
986
987static void context_free(struct smq_invoke_ctx *ctx);
988
989static int context_alloc(struct fastrpc_file *fl, uint32_t kernel,
Sathish Ambleybae51902017-07-03 15:00:49 -0700990 struct fastrpc_ioctl_invoke_crc *invokefd,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700991 struct smq_invoke_ctx **po)
992{
993 int err = 0, bufs, size = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +0530994 struct smq_invoke_ctx *ctx = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700995 struct fastrpc_ctx_lst *clst = &fl->clst;
996 struct fastrpc_ioctl_invoke *invoke = &invokefd->inv;
997
998 bufs = REMOTE_SCALARS_LENGTH(invoke->sc);
999 size = bufs * sizeof(*ctx->lpra) + bufs * sizeof(*ctx->maps) +
1000 sizeof(*ctx->fds) * (bufs) +
1001 sizeof(*ctx->attrs) * (bufs) +
1002 sizeof(*ctx->overs) * (bufs) +
1003 sizeof(*ctx->overps) * (bufs);
1004
c_mtharue1a5ce12017-10-13 20:47:09 +05301005 VERIFY(err, NULL != (ctx = kzalloc(sizeof(*ctx) + size, GFP_KERNEL)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001006 if (err)
1007 goto bail;
1008
1009 INIT_HLIST_NODE(&ctx->hn);
1010 hlist_add_fake(&ctx->hn);
1011 ctx->fl = fl;
1012 ctx->maps = (struct fastrpc_mmap **)(&ctx[1]);
1013 ctx->lpra = (remote_arg_t *)(&ctx->maps[bufs]);
1014 ctx->fds = (int *)(&ctx->lpra[bufs]);
1015 ctx->attrs = (unsigned int *)(&ctx->fds[bufs]);
1016 ctx->overs = (struct overlap *)(&ctx->attrs[bufs]);
1017 ctx->overps = (struct overlap **)(&ctx->overs[bufs]);
1018
c_mtharue1a5ce12017-10-13 20:47:09 +05301019 K_COPY_FROM_USER(err, kernel, (void *)ctx->lpra, invoke->pra,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001020 bufs * sizeof(*ctx->lpra));
1021 if (err)
1022 goto bail;
1023
1024 if (invokefd->fds) {
1025 K_COPY_FROM_USER(err, kernel, ctx->fds, invokefd->fds,
1026 bufs * sizeof(*ctx->fds));
1027 if (err)
1028 goto bail;
1029 }
1030 if (invokefd->attrs) {
1031 K_COPY_FROM_USER(err, kernel, ctx->attrs, invokefd->attrs,
1032 bufs * sizeof(*ctx->attrs));
1033 if (err)
1034 goto bail;
1035 }
Sathish Ambleybae51902017-07-03 15:00:49 -07001036 ctx->crc = (uint32_t *)invokefd->crc;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001037 ctx->sc = invoke->sc;
Sathish Ambley9466d672017-01-25 10:51:55 -08001038 if (bufs) {
1039 VERIFY(err, 0 == context_build_overlap(ctx));
1040 if (err)
1041 goto bail;
1042 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001043 ctx->retval = -1;
1044 ctx->pid = current->pid;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301045 ctx->tgid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001046 init_completion(&ctx->work);
c_mtharufdac6892017-10-12 13:09:01 +05301047 ctx->magic = FASTRPC_CTX_MAGIC;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001048
1049 spin_lock(&fl->hlock);
1050 hlist_add_head(&ctx->hn, &clst->pending);
1051 spin_unlock(&fl->hlock);
1052
1053 *po = ctx;
1054bail:
1055 if (ctx && err)
1056 context_free(ctx);
1057 return err;
1058}
1059
1060static void context_save_interrupted(struct smq_invoke_ctx *ctx)
1061{
1062 struct fastrpc_ctx_lst *clst = &ctx->fl->clst;
1063
1064 spin_lock(&ctx->fl->hlock);
1065 hlist_del_init(&ctx->hn);
1066 hlist_add_head(&ctx->hn, &clst->interrupted);
1067 spin_unlock(&ctx->fl->hlock);
1068 /* free the cache on power collapse */
1069 fastrpc_buf_list_free(ctx->fl);
1070}
1071
1072static void context_free(struct smq_invoke_ctx *ctx)
1073{
1074 int i;
1075 int nbufs = REMOTE_SCALARS_INBUFS(ctx->sc) +
1076 REMOTE_SCALARS_OUTBUFS(ctx->sc);
1077 spin_lock(&ctx->fl->hlock);
1078 hlist_del_init(&ctx->hn);
1079 spin_unlock(&ctx->fl->hlock);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301080 mutex_lock(&ctx->fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001081 for (i = 0; i < nbufs; ++i)
c_mtharu7bd6a422017-10-17 18:15:37 +05301082 fastrpc_mmap_free(ctx->maps[i], 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301083
1084 mutex_unlock(&ctx->fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001085 fastrpc_buf_free(ctx->buf, 1);
c_mtharufdac6892017-10-12 13:09:01 +05301086 ctx->magic = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001087 kfree(ctx);
1088}
1089
1090static void context_notify_user(struct smq_invoke_ctx *ctx, int retval)
1091{
1092 ctx->retval = retval;
1093 complete(&ctx->work);
1094}
1095
1096
1097static void fastrpc_notify_users(struct fastrpc_file *me)
1098{
1099 struct smq_invoke_ctx *ictx;
1100 struct hlist_node *n;
1101
1102 spin_lock(&me->hlock);
1103 hlist_for_each_entry_safe(ictx, n, &me->clst.pending, hn) {
1104 complete(&ictx->work);
1105 }
1106 hlist_for_each_entry_safe(ictx, n, &me->clst.interrupted, hn) {
1107 complete(&ictx->work);
1108 }
1109 spin_unlock(&me->hlock);
1110
1111}
1112
1113static void fastrpc_notify_drivers(struct fastrpc_apps *me, int cid)
1114{
1115 struct fastrpc_file *fl;
1116 struct hlist_node *n;
1117
1118 spin_lock(&me->hlock);
1119 hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
1120 if (fl->cid == cid)
1121 fastrpc_notify_users(fl);
1122 }
1123 spin_unlock(&me->hlock);
1124
1125}
1126static void context_list_ctor(struct fastrpc_ctx_lst *me)
1127{
1128 INIT_HLIST_HEAD(&me->interrupted);
1129 INIT_HLIST_HEAD(&me->pending);
1130}
1131
1132static void fastrpc_context_list_dtor(struct fastrpc_file *fl)
1133{
1134 struct fastrpc_ctx_lst *clst = &fl->clst;
c_mtharue1a5ce12017-10-13 20:47:09 +05301135 struct smq_invoke_ctx *ictx = NULL, *ctxfree;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001136 struct hlist_node *n;
1137
1138 do {
c_mtharue1a5ce12017-10-13 20:47:09 +05301139 ctxfree = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001140 spin_lock(&fl->hlock);
1141 hlist_for_each_entry_safe(ictx, n, &clst->interrupted, hn) {
1142 hlist_del_init(&ictx->hn);
1143 ctxfree = ictx;
1144 break;
1145 }
1146 spin_unlock(&fl->hlock);
1147 if (ctxfree)
1148 context_free(ctxfree);
1149 } while (ctxfree);
1150 do {
c_mtharue1a5ce12017-10-13 20:47:09 +05301151 ctxfree = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001152 spin_lock(&fl->hlock);
1153 hlist_for_each_entry_safe(ictx, n, &clst->pending, hn) {
1154 hlist_del_init(&ictx->hn);
1155 ctxfree = ictx;
1156 break;
1157 }
1158 spin_unlock(&fl->hlock);
1159 if (ctxfree)
1160 context_free(ctxfree);
1161 } while (ctxfree);
1162}
1163
1164static int fastrpc_file_free(struct fastrpc_file *fl);
1165static void fastrpc_file_list_dtor(struct fastrpc_apps *me)
1166{
1167 struct fastrpc_file *fl, *free;
1168 struct hlist_node *n;
1169
1170 do {
c_mtharue1a5ce12017-10-13 20:47:09 +05301171 free = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001172 spin_lock(&me->hlock);
1173 hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
1174 hlist_del_init(&fl->hn);
1175 free = fl;
1176 break;
1177 }
1178 spin_unlock(&me->hlock);
1179 if (free)
1180 fastrpc_file_free(free);
1181 } while (free);
1182}
1183
1184static int get_args(uint32_t kernel, struct smq_invoke_ctx *ctx)
1185{
1186 remote_arg64_t *rpra;
1187 remote_arg_t *lpra = ctx->lpra;
1188 struct smq_invoke_buf *list;
1189 struct smq_phy_page *pages, *ipage;
1190 uint32_t sc = ctx->sc;
1191 int inbufs = REMOTE_SCALARS_INBUFS(sc);
1192 int outbufs = REMOTE_SCALARS_OUTBUFS(sc);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001193 int handles, bufs = inbufs + outbufs;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001194 uintptr_t args;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301195 size_t rlen = 0, copylen = 0, metalen = 0;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001196 int i, oix;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001197 int err = 0;
1198 int mflags = 0;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001199 uint64_t *fdlist;
Sathish Ambleybae51902017-07-03 15:00:49 -07001200 uint32_t *crclist;
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301201 int64_t *perf_counter = getperfcounter(ctx->fl, PERF_COUNT);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001202
1203 /* calculate size of the metadata */
c_mtharue1a5ce12017-10-13 20:47:09 +05301204 rpra = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001205 list = smq_invoke_buf_start(rpra, sc);
1206 pages = smq_phy_page_start(sc, list);
1207 ipage = pages;
1208
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301209 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_MAP),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001210 for (i = 0; i < bufs; ++i) {
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301211 uintptr_t buf = (uintptr_t)lpra[i].buf.pv;
1212 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001213
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301214 mutex_lock(&ctx->fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001215 if (ctx->fds[i] && (ctx->fds[i] != -1))
1216 fastrpc_mmap_create(ctx->fl, ctx->fds[i],
1217 ctx->attrs[i], buf, len,
1218 mflags, &ctx->maps[i]);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301219 mutex_unlock(&ctx->fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001220 ipage += 1;
1221 }
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301222 PERF_END);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001223 handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301224 mutex_lock(&ctx->fl->fl_map_mutex);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001225 for (i = bufs; i < bufs + handles; i++) {
1226 VERIFY(err, !fastrpc_mmap_create(ctx->fl, ctx->fds[i],
1227 FASTRPC_ATTR_NOVA, 0, 0, 0, &ctx->maps[i]));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301228 if (err) {
1229 mutex_unlock(&ctx->fl->fl_map_mutex);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001230 goto bail;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301231 }
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001232 ipage += 1;
1233 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301234 mutex_unlock(&ctx->fl->fl_map_mutex);
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301235 metalen = copylen = (size_t)&ipage[0] + (sizeof(uint64_t) * M_FDLIST) +
Sathish Ambleybae51902017-07-03 15:00:49 -07001236 (sizeof(uint32_t) * M_CRCLIST);
1237
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001238 /* calculate len requreed for copying */
1239 for (oix = 0; oix < inbufs + outbufs; ++oix) {
1240 int i = ctx->overps[oix]->raix;
Sathish Ambleyd209c1e2016-12-13 15:27:30 -08001241 uintptr_t mstart, mend;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301242 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001243
1244 if (!len)
1245 continue;
1246 if (ctx->maps[i])
1247 continue;
1248 if (ctx->overps[oix]->offset == 0)
1249 copylen = ALIGN(copylen, BALIGN);
Sathish Ambleyd209c1e2016-12-13 15:27:30 -08001250 mstart = ctx->overps[oix]->mstart;
1251 mend = ctx->overps[oix]->mend;
1252 VERIFY(err, (mend - mstart) <= LONG_MAX);
1253 if (err)
1254 goto bail;
1255 copylen += mend - mstart;
1256 VERIFY(err, copylen >= 0);
1257 if (err)
1258 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001259 }
1260 ctx->used = copylen;
1261
1262 /* allocate new buffer */
1263 if (copylen) {
1264 VERIFY(err, !fastrpc_buf_alloc(ctx->fl, copylen, &ctx->buf));
1265 if (err)
1266 goto bail;
1267 }
Tharun Kumar Merugue3361f92017-06-22 10:45:43 +05301268 if (ctx->buf->virt && metalen <= copylen)
1269 memset(ctx->buf->virt, 0, metalen);
1270
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001271 /* copy metadata */
1272 rpra = ctx->buf->virt;
1273 ctx->rpra = rpra;
1274 list = smq_invoke_buf_start(rpra, sc);
1275 pages = smq_phy_page_start(sc, list);
1276 ipage = pages;
1277 args = (uintptr_t)ctx->buf->virt + metalen;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001278 for (i = 0; i < bufs + handles; ++i) {
1279 if (lpra[i].buf.len)
1280 list[i].num = 1;
1281 else
1282 list[i].num = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001283 list[i].pgidx = ipage - pages;
1284 ipage++;
1285 }
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05301286
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001287 /* map ion buffers */
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301288 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_MAP),
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05301289 for (i = 0; rpra && i < inbufs + outbufs; ++i) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001290 struct fastrpc_mmap *map = ctx->maps[i];
1291 uint64_t buf = ptr_to_uint64(lpra[i].buf.pv);
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301292 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001293
1294 rpra[i].buf.pv = 0;
1295 rpra[i].buf.len = len;
1296 if (!len)
1297 continue;
1298 if (map) {
1299 struct vm_area_struct *vma;
1300 uintptr_t offset;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301301 uint64_t num = buf_num_pages(buf, len);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001302 int idx = list[i].pgidx;
1303
1304 if (map->attr & FASTRPC_ATTR_NOVA) {
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001305 offset = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001306 } else {
1307 down_read(&current->mm->mmap_sem);
1308 VERIFY(err, NULL != (vma = find_vma(current->mm,
1309 map->va)));
1310 if (err) {
1311 up_read(&current->mm->mmap_sem);
1312 goto bail;
1313 }
1314 offset = buf_page_start(buf) - vma->vm_start;
1315 up_read(&current->mm->mmap_sem);
1316 VERIFY(err, offset < (uintptr_t)map->size);
1317 if (err)
1318 goto bail;
1319 }
1320 pages[idx].addr = map->phys + offset;
1321 pages[idx].size = num << PAGE_SHIFT;
1322 }
1323 rpra[i].buf.pv = buf;
1324 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001325 PERF_END);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001326 for (i = bufs; i < bufs + handles; ++i) {
1327 struct fastrpc_mmap *map = ctx->maps[i];
1328
1329 pages[i].addr = map->phys;
1330 pages[i].size = map->size;
1331 }
1332 fdlist = (uint64_t *)&pages[bufs + handles];
1333 for (i = 0; i < M_FDLIST; i++)
1334 fdlist[i] = 0;
Sathish Ambleybae51902017-07-03 15:00:49 -07001335 crclist = (uint32_t *)&fdlist[M_FDLIST];
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +05301336 memset(crclist, 0, sizeof(uint32_t)*M_CRCLIST);
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001337
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001338 /* copy non ion buffers */
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301339 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_COPY),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001340 rlen = copylen - metalen;
1341 for (oix = 0; oix < inbufs + outbufs; ++oix) {
1342 int i = ctx->overps[oix]->raix;
1343 struct fastrpc_mmap *map = ctx->maps[i];
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301344 size_t mlen;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001345 uint64_t buf;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301346 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001347
1348 if (!len)
1349 continue;
1350 if (map)
1351 continue;
1352 if (ctx->overps[oix]->offset == 0) {
1353 rlen -= ALIGN(args, BALIGN) - args;
1354 args = ALIGN(args, BALIGN);
1355 }
Sathish Ambleyd209c1e2016-12-13 15:27:30 -08001356 mlen = ctx->overps[oix]->mend - ctx->overps[oix]->mstart;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001357 VERIFY(err, rlen >= mlen);
1358 if (err)
1359 goto bail;
1360 rpra[i].buf.pv = (args - ctx->overps[oix]->offset);
1361 pages[list[i].pgidx].addr = ctx->buf->phys -
1362 ctx->overps[oix]->offset +
1363 (copylen - rlen);
1364 pages[list[i].pgidx].addr =
1365 buf_page_start(pages[list[i].pgidx].addr);
1366 buf = rpra[i].buf.pv;
1367 pages[list[i].pgidx].size = buf_num_pages(buf, len) * PAGE_SIZE;
1368 if (i < inbufs) {
1369 K_COPY_FROM_USER(err, kernel, uint64_to_ptr(buf),
1370 lpra[i].buf.pv, len);
1371 if (err)
1372 goto bail;
1373 }
1374 args = args + mlen;
1375 rlen -= mlen;
1376 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001377 PERF_END);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001378
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301379 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_FLUSH),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001380 for (oix = 0; oix < inbufs + outbufs; ++oix) {
1381 int i = ctx->overps[oix]->raix;
1382 struct fastrpc_mmap *map = ctx->maps[i];
1383
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001384 if (map && map->uncached)
1385 continue;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +05301386 if (ctx->fl->sctx->smmu.coherent &&
1387 !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT)))
1388 continue;
1389 if (map && (map->attr & FASTRPC_ATTR_COHERENT))
1390 continue;
1391
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001392 if (rpra[i].buf.len && ctx->overps[oix]->mstart)
1393 dmac_flush_range(uint64_to_ptr(rpra[i].buf.pv),
1394 uint64_to_ptr(rpra[i].buf.pv + rpra[i].buf.len));
1395 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001396 PERF_END);
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05301397 for (i = bufs; rpra && i < bufs + handles; i++) {
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001398 rpra[i].dma.fd = ctx->fds[i];
1399 rpra[i].dma.len = (uint32_t)lpra[i].buf.len;
1400 rpra[i].dma.offset = (uint32_t)(uintptr_t)lpra[i].buf.pv;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001401 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001402
1403 if (!ctx->fl->sctx->smmu.coherent) {
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301404 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_FLUSH),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001405 dmac_flush_range((char *)rpra, (char *)rpra + ctx->used);
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001406 PERF_END);
1407 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001408 bail:
1409 return err;
1410}
1411
1412static int put_args(uint32_t kernel, struct smq_invoke_ctx *ctx,
1413 remote_arg_t *upra)
1414{
1415 uint32_t sc = ctx->sc;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001416 struct smq_invoke_buf *list;
1417 struct smq_phy_page *pages;
1418 struct fastrpc_mmap *mmap;
1419 uint64_t *fdlist;
Sathish Ambleybae51902017-07-03 15:00:49 -07001420 uint32_t *crclist = NULL;
1421
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001422 remote_arg64_t *rpra = ctx->rpra;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001423 int i, inbufs, outbufs, handles;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001424 int err = 0;
1425
1426 inbufs = REMOTE_SCALARS_INBUFS(sc);
1427 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001428 handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc);
1429 list = smq_invoke_buf_start(ctx->rpra, sc);
1430 pages = smq_phy_page_start(sc, list);
1431 fdlist = (uint64_t *)(pages + inbufs + outbufs + handles);
Sathish Ambleybae51902017-07-03 15:00:49 -07001432 crclist = (uint32_t *)(fdlist + M_FDLIST);
1433
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001434 for (i = inbufs; i < inbufs + outbufs; ++i) {
1435 if (!ctx->maps[i]) {
1436 K_COPY_TO_USER(err, kernel,
1437 ctx->lpra[i].buf.pv,
1438 uint64_to_ptr(rpra[i].buf.pv),
1439 rpra[i].buf.len);
1440 if (err)
1441 goto bail;
1442 } else {
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301443 mutex_lock(&ctx->fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05301444 fastrpc_mmap_free(ctx->maps[i], 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301445 mutex_unlock(&ctx->fl->fl_map_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05301446 ctx->maps[i] = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001447 }
1448 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301449 mutex_lock(&ctx->fl->fl_map_mutex);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001450 if (inbufs + outbufs + handles) {
1451 for (i = 0; i < M_FDLIST; i++) {
1452 if (!fdlist[i])
1453 break;
1454 if (!fastrpc_mmap_find(ctx->fl, (int)fdlist[i], 0, 0,
Sathish Ambleyae5ee542017-01-16 22:24:23 -08001455 0, 0, &mmap))
c_mtharu7bd6a422017-10-17 18:15:37 +05301456 fastrpc_mmap_free(mmap, 0);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001457 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001458 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301459 mutex_unlock(&ctx->fl->fl_map_mutex);
Sathish Ambleybae51902017-07-03 15:00:49 -07001460 if (ctx->crc && crclist && rpra)
c_mtharue1a5ce12017-10-13 20:47:09 +05301461 K_COPY_TO_USER(err, kernel, ctx->crc,
Sathish Ambleybae51902017-07-03 15:00:49 -07001462 crclist, M_CRCLIST*sizeof(uint32_t));
1463
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001464 bail:
1465 return err;
1466}
1467
1468static void inv_args_pre(struct smq_invoke_ctx *ctx)
1469{
1470 int i, inbufs, outbufs;
1471 uint32_t sc = ctx->sc;
1472 remote_arg64_t *rpra = ctx->rpra;
1473 uintptr_t end;
1474
1475 inbufs = REMOTE_SCALARS_INBUFS(sc);
1476 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
1477 for (i = inbufs; i < inbufs + outbufs; ++i) {
1478 struct fastrpc_mmap *map = ctx->maps[i];
1479
1480 if (map && map->uncached)
1481 continue;
1482 if (!rpra[i].buf.len)
1483 continue;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +05301484 if (ctx->fl->sctx->smmu.coherent &&
1485 !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT)))
1486 continue;
1487 if (map && (map->attr & FASTRPC_ATTR_COHERENT))
1488 continue;
1489
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001490 if (buf_page_start(ptr_to_uint64((void *)rpra)) ==
1491 buf_page_start(rpra[i].buf.pv))
1492 continue;
1493 if (!IS_CACHE_ALIGNED((uintptr_t)uint64_to_ptr(rpra[i].buf.pv)))
1494 dmac_flush_range(uint64_to_ptr(rpra[i].buf.pv),
1495 (char *)(uint64_to_ptr(rpra[i].buf.pv + 1)));
1496 end = (uintptr_t)uint64_to_ptr(rpra[i].buf.pv +
1497 rpra[i].buf.len);
1498 if (!IS_CACHE_ALIGNED(end))
1499 dmac_flush_range((char *)end,
1500 (char *)end + 1);
1501 }
1502}
1503
1504static void inv_args(struct smq_invoke_ctx *ctx)
1505{
1506 int i, inbufs, outbufs;
1507 uint32_t sc = ctx->sc;
1508 remote_arg64_t *rpra = ctx->rpra;
1509 int used = ctx->used;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001510
1511 inbufs = REMOTE_SCALARS_INBUFS(sc);
1512 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
1513 for (i = inbufs; i < inbufs + outbufs; ++i) {
1514 struct fastrpc_mmap *map = ctx->maps[i];
1515
1516 if (map && map->uncached)
1517 continue;
1518 if (!rpra[i].buf.len)
1519 continue;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +05301520 if (ctx->fl->sctx->smmu.coherent &&
1521 !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT)))
1522 continue;
1523 if (map && (map->attr & FASTRPC_ATTR_COHERENT))
1524 continue;
1525
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001526 if (buf_page_start(ptr_to_uint64((void *)rpra)) ==
1527 buf_page_start(rpra[i].buf.pv)) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001528 continue;
1529 }
1530 if (map && map->handle)
1531 msm_ion_do_cache_op(ctx->fl->apps->client, map->handle,
1532 (char *)uint64_to_ptr(rpra[i].buf.pv),
1533 rpra[i].buf.len, ION_IOC_INV_CACHES);
1534 else
1535 dmac_inv_range((char *)uint64_to_ptr(rpra[i].buf.pv),
1536 (char *)uint64_to_ptr(rpra[i].buf.pv
1537 + rpra[i].buf.len));
1538 }
1539
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001540 if (rpra)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001541 dmac_inv_range(rpra, (char *)rpra + used);
1542}
1543
1544static int fastrpc_invoke_send(struct smq_invoke_ctx *ctx,
1545 uint32_t kernel, uint32_t handle)
1546{
1547 struct smq_msg *msg = &ctx->msg;
1548 struct fastrpc_file *fl = ctx->fl;
1549 struct fastrpc_channel_ctx *channel_ctx = &fl->apps->channel[fl->cid];
1550 int err = 0;
1551
c_mtharue1a5ce12017-10-13 20:47:09 +05301552 VERIFY(err, NULL != channel_ctx->chan);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001553 if (err)
1554 goto bail;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301555 msg->pid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001556 msg->tid = current->pid;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301557 if (fl->sessionid)
1558 msg->tid |= (1 << SESSION_ID_INDEX);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001559 if (kernel)
1560 msg->pid = 0;
1561 msg->invoke.header.ctx = ptr_to_uint64(ctx) | fl->pd;
1562 msg->invoke.header.handle = handle;
1563 msg->invoke.header.sc = ctx->sc;
1564 msg->invoke.page.addr = ctx->buf ? ctx->buf->phys : 0;
1565 msg->invoke.page.size = buf_page_size(ctx->used);
1566
1567 if (fl->ssrcount != channel_ctx->ssrcount) {
1568 err = -ECONNRESET;
1569 goto bail;
1570 }
1571 VERIFY(err, channel_ctx->link.port_state ==
1572 FASTRPC_LINK_CONNECTED);
1573 if (err)
1574 goto bail;
1575 err = glink_tx(channel_ctx->chan,
1576 (void *)&fl->apps->channel[fl->cid], msg, sizeof(*msg),
1577 GLINK_TX_REQ_INTENT);
1578 bail:
1579 return err;
1580}
1581
1582static void fastrpc_init(struct fastrpc_apps *me)
1583{
1584 int i;
1585
1586 INIT_HLIST_HEAD(&me->drivers);
Tharun Kumar Merugubcd6fbf2018-01-04 17:49:34 +05301587 INIT_HLIST_HEAD(&me->maps);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001588 spin_lock_init(&me->hlock);
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05301589 mutex_init(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001590 me->channel = &gcinfo[0];
1591 for (i = 0; i < NUM_CHANNELS; i++) {
1592 init_completion(&me->channel[i].work);
Tharun Kumar Merugu53a8ec92017-07-14 15:52:49 +05301593 init_completion(&me->channel[i].workport);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001594 me->channel[i].sesscount = 0;
1595 }
1596}
1597
1598static int fastrpc_release_current_dsp_process(struct fastrpc_file *fl);
1599
1600static int fastrpc_internal_invoke(struct fastrpc_file *fl, uint32_t mode,
1601 uint32_t kernel,
Sathish Ambleybae51902017-07-03 15:00:49 -07001602 struct fastrpc_ioctl_invoke_crc *inv)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001603{
c_mtharue1a5ce12017-10-13 20:47:09 +05301604 struct smq_invoke_ctx *ctx = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001605 struct fastrpc_ioctl_invoke *invoke = &inv->inv;
1606 int cid = fl->cid;
1607 int interrupted = 0;
1608 int err = 0;
Maria Yu757199c2017-09-22 16:05:49 +08001609 struct timespec invoket = {0};
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301610 int64_t *perf_counter = getperfcounter(fl, PERF_COUNT);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001611
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001612 if (fl->profile)
1613 getnstimeofday(&invoket);
Tharun Kumar Merugue3edf3e2017-07-27 12:34:07 +05301614
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301615
Tharun Kumar Merugue3edf3e2017-07-27 12:34:07 +05301616 VERIFY(err, fl->sctx != NULL);
1617 if (err)
1618 goto bail;
1619 VERIFY(err, fl->cid >= 0 && fl->cid < NUM_CHANNELS);
1620 if (err)
1621 goto bail;
1622
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001623 if (!kernel) {
1624 VERIFY(err, 0 == context_restore_interrupted(fl, inv,
1625 &ctx));
1626 if (err)
1627 goto bail;
1628 if (fl->sctx->smmu.faults)
1629 err = FASTRPC_ENOSUCH;
1630 if (err)
1631 goto bail;
1632 if (ctx)
1633 goto wait;
1634 }
1635
1636 VERIFY(err, 0 == context_alloc(fl, kernel, inv, &ctx));
1637 if (err)
1638 goto bail;
1639
1640 if (REMOTE_SCALARS_LENGTH(ctx->sc)) {
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301641 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_GETARGS),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001642 VERIFY(err, 0 == get_args(kernel, ctx));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001643 PERF_END);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001644 if (err)
1645 goto bail;
1646 }
1647
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301648 if (!fl->sctx->smmu.coherent) {
1649 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001650 inv_args_pre(ctx);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301651 PERF_END);
1652 }
1653
1654 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_LINK),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001655 VERIFY(err, 0 == fastrpc_invoke_send(ctx, kernel, invoke->handle));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001656 PERF_END);
1657
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001658 if (err)
1659 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001660 wait:
1661 if (kernel)
1662 wait_for_completion(&ctx->work);
1663 else {
1664 interrupted = wait_for_completion_interruptible(&ctx->work);
1665 VERIFY(err, 0 == (err = interrupted));
1666 if (err)
1667 goto bail;
1668 }
Sathish Ambleyc432b502017-06-05 12:03:42 -07001669
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301670 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS),
Sathish Ambleyc432b502017-06-05 12:03:42 -07001671 if (!fl->sctx->smmu.coherent)
1672 inv_args(ctx);
1673 PERF_END);
1674
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001675 VERIFY(err, 0 == (err = ctx->retval));
1676 if (err)
1677 goto bail;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001678
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301679 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_PUTARGS),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001680 VERIFY(err, 0 == put_args(kernel, ctx, invoke->pra));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001681 PERF_END);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001682 if (err)
1683 goto bail;
1684 bail:
1685 if (ctx && interrupted == -ERESTARTSYS)
1686 context_save_interrupted(ctx);
1687 else if (ctx)
1688 context_free(ctx);
1689 if (fl->ssrcount != fl->apps->channel[cid].ssrcount)
1690 err = ECONNRESET;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001691
1692 if (fl->profile && !interrupted) {
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301693 if (invoke->handle != FASTRPC_STATIC_HANDLE_LISTENER) {
1694 int64_t *count = GET_COUNTER(perf_counter, PERF_INVOKE);
1695
1696 if (count)
1697 *count += getnstimediff(&invoket);
1698 }
1699 if (invoke->handle > FASTRPC_STATIC_HANDLE_MAX) {
1700 int64_t *count = GET_COUNTER(perf_counter, PERF_COUNT);
1701
1702 if (count)
1703 *count = *count+1;
1704 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001705 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001706 return err;
1707}
1708
Sathish Ambley36849af2017-02-02 09:35:55 -08001709static int fastrpc_channel_open(struct fastrpc_file *fl);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001710static int fastrpc_init_process(struct fastrpc_file *fl,
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001711 struct fastrpc_ioctl_init_attrs *uproc)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001712{
1713 int err = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05301714 struct fastrpc_apps *me = &gfa;
Sathish Ambleybae51902017-07-03 15:00:49 -07001715 struct fastrpc_ioctl_invoke_crc ioctl;
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001716 struct fastrpc_ioctl_init *init = &uproc->init;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001717 struct smq_phy_page pages[1];
c_mtharue1a5ce12017-10-13 20:47:09 +05301718 struct fastrpc_mmap *file = NULL, *mem = NULL;
1719 char *proc_name = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001720
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05301721 VERIFY(err, 0 == (err = fastrpc_channel_open(fl)));
Sathish Ambley36849af2017-02-02 09:35:55 -08001722 if (err)
1723 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001724 if (init->flags == FASTRPC_INIT_ATTACH) {
1725 remote_arg_t ra[1];
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301726 int tgid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001727
1728 ra[0].buf.pv = (void *)&tgid;
1729 ra[0].buf.len = sizeof(tgid);
1730 ioctl.inv.handle = 1;
1731 ioctl.inv.sc = REMOTE_SCALARS_MAKE(0, 1, 0);
1732 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05301733 ioctl.fds = NULL;
1734 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07001735 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001736 fl->pd = 0;
1737 VERIFY(err, !(err = fastrpc_internal_invoke(fl,
1738 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
1739 if (err)
1740 goto bail;
1741 } else if (init->flags == FASTRPC_INIT_CREATE) {
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001742 remote_arg_t ra[6];
1743 int fds[6];
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001744 int mflags = 0;
1745 struct {
1746 int pgid;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301747 unsigned int namelen;
1748 unsigned int filelen;
1749 unsigned int pageslen;
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001750 int attrs;
1751 int siglen;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001752 } inbuf;
1753
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301754 inbuf.pgid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001755 inbuf.namelen = strlen(current->comm) + 1;
1756 inbuf.filelen = init->filelen;
1757 fl->pd = 1;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301758
Tharun Kumar Merugudf852892017-12-07 16:27:37 +05301759 VERIFY(err, access_ok(0, (void __user *)init->file,
1760 init->filelen));
1761 if (err)
1762 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001763 if (init->filelen) {
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301764 mutex_lock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001765 VERIFY(err, !fastrpc_mmap_create(fl, init->filefd, 0,
1766 init->file, init->filelen, mflags, &file));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301767 mutex_unlock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001768 if (err)
1769 goto bail;
1770 }
1771 inbuf.pageslen = 1;
Tharun Kumar Merugudf852892017-12-07 16:27:37 +05301772 VERIFY(err, access_ok(1, (void __user *)init->mem,
1773 init->memlen));
1774 if (err)
1775 goto bail;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301776 mutex_lock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001777 VERIFY(err, !fastrpc_mmap_create(fl, init->memfd, 0,
1778 init->mem, init->memlen, mflags, &mem));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301779 mutex_unlock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001780 if (err)
1781 goto bail;
1782 inbuf.pageslen = 1;
1783 ra[0].buf.pv = (void *)&inbuf;
1784 ra[0].buf.len = sizeof(inbuf);
1785 fds[0] = 0;
1786
1787 ra[1].buf.pv = (void *)current->comm;
1788 ra[1].buf.len = inbuf.namelen;
1789 fds[1] = 0;
1790
1791 ra[2].buf.pv = (void *)init->file;
1792 ra[2].buf.len = inbuf.filelen;
1793 fds[2] = init->filefd;
1794
1795 pages[0].addr = mem->phys;
1796 pages[0].size = mem->size;
1797 ra[3].buf.pv = (void *)pages;
1798 ra[3].buf.len = 1 * sizeof(*pages);
1799 fds[3] = 0;
1800
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001801 inbuf.attrs = uproc->attrs;
1802 ra[4].buf.pv = (void *)&(inbuf.attrs);
1803 ra[4].buf.len = sizeof(inbuf.attrs);
1804 fds[4] = 0;
1805
1806 inbuf.siglen = uproc->siglen;
1807 ra[5].buf.pv = (void *)&(inbuf.siglen);
1808 ra[5].buf.len = sizeof(inbuf.siglen);
1809 fds[5] = 0;
1810
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001811 ioctl.inv.handle = 1;
1812 ioctl.inv.sc = REMOTE_SCALARS_MAKE(6, 4, 0);
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001813 if (uproc->attrs)
1814 ioctl.inv.sc = REMOTE_SCALARS_MAKE(7, 6, 0);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001815 ioctl.inv.pra = ra;
1816 ioctl.fds = fds;
c_mtharue1a5ce12017-10-13 20:47:09 +05301817 ioctl.attrs = NULL;
1818 ioctl.crc = NULL;
1819 VERIFY(err, !(err = fastrpc_internal_invoke(fl,
1820 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
1821 if (err)
1822 goto bail;
1823 } else if (init->flags == FASTRPC_INIT_CREATE_STATIC) {
1824 remote_arg_t ra[3];
1825 uint64_t phys = 0;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301826 size_t size = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05301827 int fds[3];
1828 struct {
1829 int pgid;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301830 unsigned int namelen;
1831 unsigned int pageslen;
c_mtharue1a5ce12017-10-13 20:47:09 +05301832 } inbuf;
1833
1834 if (!init->filelen)
1835 goto bail;
1836
1837 proc_name = kzalloc(init->filelen, GFP_KERNEL);
1838 VERIFY(err, !IS_ERR_OR_NULL(proc_name));
1839 if (err)
1840 goto bail;
1841 VERIFY(err, 0 == copy_from_user((void *)proc_name,
1842 (void __user *)init->file, init->filelen));
1843 if (err)
1844 goto bail;
1845
1846 inbuf.pgid = current->tgid;
c_mtharu81a0aa72017-11-07 16:13:21 +05301847 inbuf.namelen = init->filelen;
c_mtharue1a5ce12017-10-13 20:47:09 +05301848 inbuf.pageslen = 0;
1849 if (!me->staticpd_flags) {
1850 inbuf.pageslen = 1;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301851 mutex_lock(&fl->fl_map_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05301852 VERIFY(err, !fastrpc_mmap_create(fl, -1, 0, init->mem,
1853 init->memlen, ADSP_MMAP_REMOTE_HEAP_ADDR,
1854 &mem));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301855 mutex_unlock(&fl->fl_map_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05301856 if (err)
1857 goto bail;
1858 phys = mem->phys;
1859 size = mem->size;
1860 VERIFY(err, !hyp_assign_phys(phys, (uint64_t)size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05301861 hlosvm, 1, me->channel[fl->cid].rhvm.vmid,
1862 me->channel[fl->cid].rhvm.vmperm,
1863 me->channel[fl->cid].rhvm.vmcount));
c_mtharue1a5ce12017-10-13 20:47:09 +05301864 if (err) {
1865 pr_err("ADSPRPC: hyp_assign_phys fail err %d",
1866 err);
1867 pr_err("map->phys %llx, map->size %d\n",
1868 phys, (int)size);
1869 goto bail;
1870 }
1871 me->staticpd_flags = 1;
1872 }
1873
1874 ra[0].buf.pv = (void *)&inbuf;
1875 ra[0].buf.len = sizeof(inbuf);
1876 fds[0] = 0;
1877
1878 ra[1].buf.pv = (void *)proc_name;
1879 ra[1].buf.len = inbuf.namelen;
1880 fds[1] = 0;
1881
1882 pages[0].addr = phys;
1883 pages[0].size = size;
1884
1885 ra[2].buf.pv = (void *)pages;
1886 ra[2].buf.len = sizeof(*pages);
1887 fds[2] = 0;
1888 ioctl.inv.handle = 1;
1889
1890 ioctl.inv.sc = REMOTE_SCALARS_MAKE(8, 3, 0);
1891 ioctl.inv.pra = ra;
1892 ioctl.fds = NULL;
1893 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07001894 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001895 VERIFY(err, !(err = fastrpc_internal_invoke(fl,
1896 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
1897 if (err)
1898 goto bail;
1899 } else {
1900 err = -ENOTTY;
1901 }
1902bail:
c_mtharud91205a2017-11-07 16:01:06 +05301903 kfree(proc_name);
c_mtharue1a5ce12017-10-13 20:47:09 +05301904 if (err && (init->flags == FASTRPC_INIT_CREATE_STATIC))
1905 me->staticpd_flags = 0;
1906 if (mem && err) {
1907 if (mem->flags == ADSP_MMAP_REMOTE_HEAP_ADDR)
1908 hyp_assign_phys(mem->phys, (uint64_t)mem->size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05301909 me->channel[fl->cid].rhvm.vmid,
1910 me->channel[fl->cid].rhvm.vmcount,
1911 hlosvm, hlosvmperm, 1);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301912 mutex_lock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05301913 fastrpc_mmap_free(mem, 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301914 mutex_unlock(&fl->fl_map_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05301915 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301916 if (file) {
1917 mutex_lock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05301918 fastrpc_mmap_free(file, 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301919 mutex_unlock(&fl->fl_map_mutex);
1920 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001921 return err;
1922}
1923
1924static int fastrpc_release_current_dsp_process(struct fastrpc_file *fl)
1925{
1926 int err = 0;
Sathish Ambleybae51902017-07-03 15:00:49 -07001927 struct fastrpc_ioctl_invoke_crc ioctl;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001928 remote_arg_t ra[1];
1929 int tgid = 0;
1930
Sathish Ambley36849af2017-02-02 09:35:55 -08001931 VERIFY(err, fl->cid >= 0 && fl->cid < NUM_CHANNELS);
1932 if (err)
1933 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05301934 VERIFY(err, fl->apps->channel[fl->cid].chan != NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001935 if (err)
1936 goto bail;
1937 tgid = fl->tgid;
1938 ra[0].buf.pv = (void *)&tgid;
1939 ra[0].buf.len = sizeof(tgid);
1940 ioctl.inv.handle = 1;
1941 ioctl.inv.sc = REMOTE_SCALARS_MAKE(1, 1, 0);
1942 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05301943 ioctl.fds = NULL;
1944 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07001945 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001946 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
1947 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
1948bail:
1949 return err;
1950}
1951
1952static int fastrpc_mmap_on_dsp(struct fastrpc_file *fl, uint32_t flags,
1953 struct fastrpc_mmap *map)
1954{
Sathish Ambleybae51902017-07-03 15:00:49 -07001955 struct fastrpc_ioctl_invoke_crc ioctl;
c_mtharu63ffc012017-11-16 15:26:56 +05301956 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001957 struct smq_phy_page page;
1958 int num = 1;
1959 remote_arg_t ra[3];
1960 int err = 0;
1961 struct {
1962 int pid;
1963 uint32_t flags;
1964 uintptr_t vaddrin;
1965 int num;
1966 } inargs;
1967 struct {
1968 uintptr_t vaddrout;
1969 } routargs;
1970
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301971 inargs.pid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001972 inargs.vaddrin = (uintptr_t)map->va;
1973 inargs.flags = flags;
1974 inargs.num = fl->apps->compat ? num * sizeof(page) : num;
1975 ra[0].buf.pv = (void *)&inargs;
1976 ra[0].buf.len = sizeof(inargs);
1977 page.addr = map->phys;
1978 page.size = map->size;
1979 ra[1].buf.pv = (void *)&page;
1980 ra[1].buf.len = num * sizeof(page);
1981
1982 ra[2].buf.pv = (void *)&routargs;
1983 ra[2].buf.len = sizeof(routargs);
1984
1985 ioctl.inv.handle = 1;
1986 if (fl->apps->compat)
1987 ioctl.inv.sc = REMOTE_SCALARS_MAKE(4, 2, 1);
1988 else
1989 ioctl.inv.sc = REMOTE_SCALARS_MAKE(2, 2, 1);
1990 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05301991 ioctl.fds = NULL;
1992 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07001993 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001994 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
1995 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
1996 map->raddr = (uintptr_t)routargs.vaddrout;
c_mtharue1a5ce12017-10-13 20:47:09 +05301997 if (err)
1998 goto bail;
1999 if (flags == ADSP_MMAP_HEAP_ADDR) {
2000 struct scm_desc desc = {0};
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002001
c_mtharue1a5ce12017-10-13 20:47:09 +05302002 desc.args[0] = TZ_PIL_AUTH_QDSP6_PROC;
2003 desc.args[1] = map->phys;
2004 desc.args[2] = map->size;
2005 desc.arginfo = SCM_ARGS(3);
2006 err = scm_call2(SCM_SIP_FNID(SCM_SVC_PIL,
2007 TZ_PIL_PROTECT_MEM_SUBSYS_ID), &desc);
2008 } else if (flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
c_mtharue1a5ce12017-10-13 20:47:09 +05302009 VERIFY(err, !hyp_assign_phys(map->phys, (uint64_t)map->size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05302010 hlosvm, 1, me->channel[fl->cid].rhvm.vmid,
2011 me->channel[fl->cid].rhvm.vmperm,
2012 me->channel[fl->cid].rhvm.vmcount));
c_mtharue1a5ce12017-10-13 20:47:09 +05302013 if (err)
2014 goto bail;
2015 }
2016bail:
2017 return err;
2018}
2019
2020static int fastrpc_munmap_on_dsp_rh(struct fastrpc_file *fl,
2021 struct fastrpc_mmap *map)
2022{
2023 int err = 0;
c_mtharu63ffc012017-11-16 15:26:56 +05302024 struct fastrpc_apps *me = &gfa;
c_mtharue1a5ce12017-10-13 20:47:09 +05302025 int destVM[1] = {VMID_HLOS};
2026 int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
2027
2028 if (map->flags == ADSP_MMAP_HEAP_ADDR) {
2029 struct fastrpc_ioctl_invoke_crc ioctl;
2030 struct scm_desc desc = {0};
2031 remote_arg_t ra[1];
2032 int err = 0;
2033 struct {
2034 uint8_t skey;
2035 } routargs;
2036
2037 ra[0].buf.pv = (void *)&routargs;
2038 ra[0].buf.len = sizeof(routargs);
2039
2040 ioctl.inv.handle = 1;
2041 ioctl.inv.sc = REMOTE_SCALARS_MAKE(7, 0, 1);
2042 ioctl.inv.pra = ra;
2043 ioctl.fds = NULL;
2044 ioctl.attrs = NULL;
2045 ioctl.crc = NULL;
2046 if (fl == NULL)
2047 goto bail;
2048
2049 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
2050 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
2051 if (err)
2052 goto bail;
2053 desc.args[0] = TZ_PIL_AUTH_QDSP6_PROC;
2054 desc.args[1] = map->phys;
2055 desc.args[2] = map->size;
2056 desc.args[3] = routargs.skey;
2057 desc.arginfo = SCM_ARGS(4);
2058 err = scm_call2(SCM_SIP_FNID(SCM_SVC_PIL,
2059 TZ_PIL_CLEAR_PROTECT_MEM_SUBSYS_ID), &desc);
2060 } else if (map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
2061 VERIFY(err, !hyp_assign_phys(map->phys, (uint64_t)map->size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05302062 me->channel[fl->cid].rhvm.vmid,
2063 me->channel[fl->cid].rhvm.vmcount,
2064 destVM, destVMperm, 1));
c_mtharue1a5ce12017-10-13 20:47:09 +05302065 if (err)
2066 goto bail;
2067 }
2068
2069bail:
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002070 return err;
2071}
2072
2073static int fastrpc_munmap_on_dsp(struct fastrpc_file *fl,
2074 struct fastrpc_mmap *map)
2075{
Sathish Ambleybae51902017-07-03 15:00:49 -07002076 struct fastrpc_ioctl_invoke_crc ioctl;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002077 remote_arg_t ra[1];
2078 int err = 0;
2079 struct {
2080 int pid;
2081 uintptr_t vaddrout;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302082 size_t size;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002083 } inargs;
2084
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05302085 inargs.pid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002086 inargs.size = map->size;
2087 inargs.vaddrout = map->raddr;
2088 ra[0].buf.pv = (void *)&inargs;
2089 ra[0].buf.len = sizeof(inargs);
2090
2091 ioctl.inv.handle = 1;
2092 if (fl->apps->compat)
2093 ioctl.inv.sc = REMOTE_SCALARS_MAKE(5, 1, 0);
2094 else
2095 ioctl.inv.sc = REMOTE_SCALARS_MAKE(3, 1, 0);
2096 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05302097 ioctl.fds = NULL;
2098 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07002099 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002100 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
2101 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
c_mtharue1a5ce12017-10-13 20:47:09 +05302102 if (err)
2103 goto bail;
2104 if (map->flags == ADSP_MMAP_HEAP_ADDR ||
2105 map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
2106 VERIFY(err, !fastrpc_munmap_on_dsp_rh(fl, map));
2107 if (err)
2108 goto bail;
2109 }
2110bail:
2111 return err;
2112}
2113
2114static int fastrpc_mmap_remove_ssr(struct fastrpc_file *fl)
2115{
2116 struct fastrpc_mmap *match = NULL, *map = NULL;
2117 struct hlist_node *n = NULL;
2118 int err = 0, ret = 0;
2119 struct fastrpc_apps *me = &gfa;
2120 struct ramdump_segment *ramdump_segments_rh = NULL;
2121
2122 do {
2123 match = NULL;
2124 spin_lock(&me->hlock);
2125 hlist_for_each_entry_safe(map, n, &me->maps, hn) {
2126 match = map;
2127 hlist_del_init(&map->hn);
2128 break;
2129 }
2130 spin_unlock(&me->hlock);
2131
2132 if (match) {
2133 VERIFY(err, !fastrpc_munmap_on_dsp_rh(fl, match));
2134 if (err)
2135 goto bail;
2136 if (me->channel[0].ramdumpenabled) {
2137 ramdump_segments_rh = kcalloc(1,
2138 sizeof(struct ramdump_segment), GFP_KERNEL);
2139 if (ramdump_segments_rh) {
2140 ramdump_segments_rh->address =
2141 match->phys;
2142 ramdump_segments_rh->size = match->size;
2143 ret = do_elf_ramdump(
2144 me->channel[0].remoteheap_ramdump_dev,
2145 ramdump_segments_rh, 1);
2146 if (ret < 0)
2147 pr_err("ADSPRPC: unable to dump heap");
2148 kfree(ramdump_segments_rh);
2149 }
2150 }
c_mtharu7bd6a422017-10-17 18:15:37 +05302151 fastrpc_mmap_free(match, 0);
c_mtharue1a5ce12017-10-13 20:47:09 +05302152 }
2153 } while (match);
2154bail:
2155 if (err && match)
2156 fastrpc_mmap_add(match);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002157 return err;
2158}
2159
2160static int fastrpc_mmap_remove(struct fastrpc_file *fl, uintptr_t va,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302161 size_t len, struct fastrpc_mmap **ppmap);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002162
2163static void fastrpc_mmap_add(struct fastrpc_mmap *map);
2164
2165static int fastrpc_internal_munmap(struct fastrpc_file *fl,
2166 struct fastrpc_ioctl_munmap *ud)
2167{
2168 int err = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05302169 struct fastrpc_mmap *map = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002170
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302171 mutex_lock(&fl->map_mutex);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302172 mutex_lock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002173 VERIFY(err, !fastrpc_mmap_remove(fl, ud->vaddrout, ud->size, &map));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302174 mutex_unlock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002175 if (err)
2176 goto bail;
2177 VERIFY(err, !fastrpc_munmap_on_dsp(fl, map));
2178 if (err)
2179 goto bail;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302180 mutex_lock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302181 fastrpc_mmap_free(map, 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302182 mutex_unlock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002183bail:
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302184 if (err && map) {
2185 mutex_lock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002186 fastrpc_mmap_add(map);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302187 mutex_unlock(&fl->fl_map_mutex);
2188 }
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302189 mutex_unlock(&fl->map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002190 return err;
2191}
2192
c_mtharu7bd6a422017-10-17 18:15:37 +05302193static int fastrpc_internal_munmap_fd(struct fastrpc_file *fl,
2194 struct fastrpc_ioctl_munmap_fd *ud) {
2195 int err = 0;
2196 struct fastrpc_mmap *map = NULL;
2197
2198 VERIFY(err, (fl && ud));
2199 if (err)
2200 goto bail;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302201 mutex_lock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302202 if (!fastrpc_mmap_find(fl, ud->fd, ud->va, ud->len, 0, 0, &map)) {
2203 pr_err("mapping not found to unamp %x va %llx %x\n",
2204 ud->fd, (unsigned long long)ud->va,
2205 (unsigned int)ud->len);
2206 err = -1;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302207 mutex_unlock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302208 goto bail;
2209 }
2210 if (map)
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302211 fastrpc_mmap_free(map, 0);
2212 mutex_unlock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302213bail:
2214 return err;
2215}
2216
2217
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002218static int fastrpc_internal_mmap(struct fastrpc_file *fl,
2219 struct fastrpc_ioctl_mmap *ud)
2220{
2221
c_mtharue1a5ce12017-10-13 20:47:09 +05302222 struct fastrpc_mmap *map = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002223 int err = 0;
2224
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302225 mutex_lock(&fl->map_mutex);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302226 mutex_lock(&fl->fl_map_mutex);
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302227 if (!fastrpc_mmap_find(fl, ud->fd, (uintptr_t)ud->vaddrin,
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302228 ud->size, ud->flags, 1, &map)) {
2229 mutex_unlock(&fl->fl_map_mutex);
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302230 mutex_unlock(&fl->map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002231 return 0;
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302232 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002233 VERIFY(err, !fastrpc_mmap_create(fl, ud->fd, 0,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302234 (uintptr_t)ud->vaddrin, ud->size,
c_mtharue1a5ce12017-10-13 20:47:09 +05302235 ud->flags, &map));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302236 mutex_unlock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002237 if (err)
2238 goto bail;
2239 VERIFY(err, 0 == fastrpc_mmap_on_dsp(fl, ud->flags, map));
2240 if (err)
2241 goto bail;
2242 ud->vaddrout = map->raddr;
2243 bail:
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302244 if (err && map) {
2245 mutex_lock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302246 fastrpc_mmap_free(map, 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302247 mutex_unlock(&fl->fl_map_mutex);
2248 }
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302249 mutex_unlock(&fl->map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002250 return err;
2251}
2252
2253static void fastrpc_channel_close(struct kref *kref)
2254{
2255 struct fastrpc_apps *me = &gfa;
2256 struct fastrpc_channel_ctx *ctx;
2257 int cid;
2258
2259 ctx = container_of(kref, struct fastrpc_channel_ctx, kref);
2260 cid = ctx - &gcinfo[0];
2261 fastrpc_glink_close(ctx->chan, cid);
c_mtharue1a5ce12017-10-13 20:47:09 +05302262 ctx->chan = NULL;
Tharun Kumar Merugu532767d2017-06-20 19:53:13 +05302263 glink_unregister_link_state_cb(ctx->link.link_notify_handle);
2264 ctx->link.link_notify_handle = NULL;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302265 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002266 pr_info("'closed /dev/%s c %d %d'\n", gcinfo[cid].name,
2267 MAJOR(me->dev_no), cid);
2268}
2269
2270static void fastrpc_context_list_dtor(struct fastrpc_file *fl);
2271
2272static int fastrpc_session_alloc_locked(struct fastrpc_channel_ctx *chan,
2273 int secure, struct fastrpc_session_ctx **session)
2274{
2275 struct fastrpc_apps *me = &gfa;
2276 int idx = 0, err = 0;
2277
2278 if (chan->sesscount) {
2279 for (idx = 0; idx < chan->sesscount; ++idx) {
2280 if (!chan->session[idx].used &&
2281 chan->session[idx].smmu.secure == secure) {
2282 chan->session[idx].used = 1;
2283 break;
2284 }
2285 }
2286 VERIFY(err, idx < chan->sesscount);
2287 if (err)
2288 goto bail;
2289 chan->session[idx].smmu.faults = 0;
2290 } else {
2291 VERIFY(err, me->dev != NULL);
2292 if (err)
2293 goto bail;
2294 chan->session[0].dev = me->dev;
c_mtharue1a5ce12017-10-13 20:47:09 +05302295 chan->session[0].smmu.dev = me->dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002296 }
2297
2298 *session = &chan->session[idx];
2299 bail:
2300 return err;
2301}
2302
c_mtharue1a5ce12017-10-13 20:47:09 +05302303static bool fastrpc_glink_notify_rx_intent_req(void *h, const void *priv,
2304 size_t size)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002305{
2306 if (glink_queue_rx_intent(h, NULL, size))
2307 return false;
2308 return true;
2309}
2310
c_mtharue1a5ce12017-10-13 20:47:09 +05302311static void fastrpc_glink_notify_tx_done(void *handle, const void *priv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002312 const void *pkt_priv, const void *ptr)
2313{
2314}
2315
c_mtharue1a5ce12017-10-13 20:47:09 +05302316static void fastrpc_glink_notify_rx(void *handle, const void *priv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002317 const void *pkt_priv, const void *ptr, size_t size)
2318{
2319 struct smq_invoke_rsp *rsp = (struct smq_invoke_rsp *)ptr;
c_mtharufdac6892017-10-12 13:09:01 +05302320 struct smq_invoke_ctx *ctx;
2321 int err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002322
c_mtharufdac6892017-10-12 13:09:01 +05302323 VERIFY(err, (rsp && size >= sizeof(*rsp)));
2324 if (err)
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302325 goto bail;
2326
c_mtharufdac6892017-10-12 13:09:01 +05302327 ctx = (struct smq_invoke_ctx *)(uint64_to_ptr(rsp->ctx & ~1));
2328 VERIFY(err, (ctx && ctx->magic == FASTRPC_CTX_MAGIC));
2329 if (err)
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302330 goto bail;
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302331
c_mtharufdac6892017-10-12 13:09:01 +05302332 context_notify_user(ctx, rsp->retval);
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302333bail:
c_mtharufdac6892017-10-12 13:09:01 +05302334 if (err)
2335 pr_err("adsprpc: invalid response or context\n");
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002336 glink_rx_done(handle, ptr, true);
2337}
2338
c_mtharue1a5ce12017-10-13 20:47:09 +05302339static void fastrpc_glink_notify_state(void *handle, const void *priv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002340 unsigned int event)
2341{
2342 struct fastrpc_apps *me = &gfa;
2343 int cid = (int)(uintptr_t)priv;
2344 struct fastrpc_glink_info *link;
2345
2346 if (cid < 0 || cid >= NUM_CHANNELS)
2347 return;
2348 link = &me->channel[cid].link;
2349 switch (event) {
2350 case GLINK_CONNECTED:
2351 link->port_state = FASTRPC_LINK_CONNECTED;
Tharun Kumar Merugu53a8ec92017-07-14 15:52:49 +05302352 complete(&me->channel[cid].workport);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002353 break;
2354 case GLINK_LOCAL_DISCONNECTED:
2355 link->port_state = FASTRPC_LINK_DISCONNECTED;
2356 break;
2357 case GLINK_REMOTE_DISCONNECTED:
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002358 break;
2359 default:
2360 break;
2361 }
2362}
2363
2364static int fastrpc_session_alloc(struct fastrpc_channel_ctx *chan, int secure,
2365 struct fastrpc_session_ctx **session)
2366{
2367 int err = 0;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302368 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002369
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302370 mutex_lock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002371 if (!*session)
2372 err = fastrpc_session_alloc_locked(chan, secure, session);
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302373 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002374 return err;
2375}
2376
2377static void fastrpc_session_free(struct fastrpc_channel_ctx *chan,
2378 struct fastrpc_session_ctx *session)
2379{
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302380 struct fastrpc_apps *me = &gfa;
2381
2382 mutex_lock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002383 session->used = 0;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302384 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002385}
2386
2387static int fastrpc_file_free(struct fastrpc_file *fl)
2388{
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302389 struct hlist_node *n = NULL;
c_mtharue1a5ce12017-10-13 20:47:09 +05302390 struct fastrpc_mmap *map = NULL;
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302391 struct fastrpc_perf *perf = NULL, *fperf = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002392 int cid;
2393
2394 if (!fl)
2395 return 0;
2396 cid = fl->cid;
2397
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302398 (void)fastrpc_release_current_dsp_process(fl);
2399
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002400 spin_lock(&fl->apps->hlock);
2401 hlist_del_init(&fl->hn);
2402 spin_unlock(&fl->apps->hlock);
2403
Sathish Ambleyd7fbcbb2017-03-08 10:55:48 -08002404 if (!fl->sctx) {
2405 kfree(fl);
2406 return 0;
2407 }
tharun kumar9f899ea2017-07-03 17:07:03 +05302408 spin_lock(&fl->hlock);
2409 fl->file_close = 1;
2410 spin_unlock(&fl->hlock);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002411 fastrpc_context_list_dtor(fl);
2412 fastrpc_buf_list_free(fl);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302413 mutex_lock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002414 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
c_mtharu7bd6a422017-10-17 18:15:37 +05302415 fastrpc_mmap_free(map, 1);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002416 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302417 mutex_unlock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002418 if (fl->ssrcount == fl->apps->channel[cid].ssrcount)
2419 kref_put_mutex(&fl->apps->channel[cid].kref,
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302420 fastrpc_channel_close, &fl->apps->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002421 if (fl->sctx)
2422 fastrpc_session_free(&fl->apps->channel[cid], fl->sctx);
2423 if (fl->secsctx)
2424 fastrpc_session_free(&fl->apps->channel[cid], fl->secsctx);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302425
2426 mutex_lock(&fl->perf_mutex);
2427 do {
2428 struct hlist_node *pn = NULL;
2429
2430 fperf = NULL;
2431 hlist_for_each_entry_safe(perf, pn, &fl->perf, hn) {
2432 hlist_del_init(&perf->hn);
2433 fperf = perf;
2434 break;
2435 }
2436 kfree(fperf);
2437 } while (fperf);
2438 mutex_unlock(&fl->perf_mutex);
2439 mutex_destroy(&fl->perf_mutex);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302440 mutex_destroy(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002441 kfree(fl);
2442 return 0;
2443}
2444
2445static int fastrpc_device_release(struct inode *inode, struct file *file)
2446{
2447 struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data;
2448
2449 if (fl) {
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05302450 if (fl->qos_request && pm_qos_request_active(&fl->pm_qos_req))
2451 pm_qos_remove_request(&fl->pm_qos_req);
Sathish Ambley1ca68232017-01-19 10:32:55 -08002452 if (fl->debugfs_file != NULL)
2453 debugfs_remove(fl->debugfs_file);
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302454 mutex_destroy(&fl->map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002455 fastrpc_file_free(fl);
c_mtharue1a5ce12017-10-13 20:47:09 +05302456 file->private_data = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002457 }
2458 return 0;
2459}
2460
2461static void fastrpc_link_state_handler(struct glink_link_state_cb_info *cb_info,
2462 void *priv)
2463{
2464 struct fastrpc_apps *me = &gfa;
2465 int cid = (int)((uintptr_t)priv);
2466 struct fastrpc_glink_info *link;
2467
2468 if (cid < 0 || cid >= NUM_CHANNELS)
2469 return;
2470
2471 link = &me->channel[cid].link;
2472 switch (cb_info->link_state) {
2473 case GLINK_LINK_STATE_UP:
2474 link->link_state = FASTRPC_LINK_STATE_UP;
2475 complete(&me->channel[cid].work);
2476 break;
2477 case GLINK_LINK_STATE_DOWN:
2478 link->link_state = FASTRPC_LINK_STATE_DOWN;
2479 break;
2480 default:
2481 pr_err("adsprpc: unknown link state %d\n", cb_info->link_state);
2482 break;
2483 }
2484}
2485
2486static int fastrpc_glink_register(int cid, struct fastrpc_apps *me)
2487{
2488 int err = 0;
2489 struct fastrpc_glink_info *link;
2490
2491 VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS));
2492 if (err)
2493 goto bail;
2494
2495 link = &me->channel[cid].link;
2496 if (link->link_notify_handle != NULL)
2497 goto bail;
2498
2499 link->link_info.glink_link_state_notif_cb = fastrpc_link_state_handler;
2500 link->link_notify_handle = glink_register_link_state_cb(
2501 &link->link_info,
2502 (void *)((uintptr_t)cid));
2503 VERIFY(err, !IS_ERR_OR_NULL(me->channel[cid].link.link_notify_handle));
2504 if (err) {
2505 link->link_notify_handle = NULL;
2506 goto bail;
2507 }
2508 VERIFY(err, wait_for_completion_timeout(&me->channel[cid].work,
2509 RPC_TIMEOUT));
2510bail:
2511 return err;
2512}
2513
2514static void fastrpc_glink_close(void *chan, int cid)
2515{
2516 int err = 0;
2517 struct fastrpc_glink_info *link;
2518
2519 VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS));
2520 if (err)
2521 return;
2522 link = &gfa.channel[cid].link;
2523
c_mtharu314a4202017-11-15 22:09:17 +05302524 if (link->port_state == FASTRPC_LINK_CONNECTED ||
2525 link->port_state == FASTRPC_LINK_REMOTE_DISCONNECTING) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002526 link->port_state = FASTRPC_LINK_DISCONNECTING;
2527 glink_close(chan);
2528 }
2529}
2530
2531static int fastrpc_glink_open(int cid)
2532{
2533 int err = 0;
2534 void *handle = NULL;
2535 struct fastrpc_apps *me = &gfa;
2536 struct glink_open_config *cfg;
2537 struct fastrpc_glink_info *link;
2538
2539 VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS));
2540 if (err)
2541 goto bail;
2542 link = &me->channel[cid].link;
2543 cfg = &me->channel[cid].link.cfg;
2544 VERIFY(err, (link->link_state == FASTRPC_LINK_STATE_UP));
2545 if (err)
2546 goto bail;
2547
Tharun Kumar Meruguca0db5262017-05-10 12:53:12 +05302548 VERIFY(err, (link->port_state == FASTRPC_LINK_DISCONNECTED));
2549 if (err)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002550 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002551
2552 link->port_state = FASTRPC_LINK_CONNECTING;
2553 cfg->priv = (void *)(uintptr_t)cid;
2554 cfg->edge = gcinfo[cid].link.link_info.edge;
2555 cfg->transport = gcinfo[cid].link.link_info.transport;
2556 cfg->name = FASTRPC_GLINK_GUID;
2557 cfg->notify_rx = fastrpc_glink_notify_rx;
2558 cfg->notify_tx_done = fastrpc_glink_notify_tx_done;
2559 cfg->notify_state = fastrpc_glink_notify_state;
2560 cfg->notify_rx_intent_req = fastrpc_glink_notify_rx_intent_req;
2561 handle = glink_open(cfg);
2562 VERIFY(err, !IS_ERR_OR_NULL(handle));
c_mtharu6e1d26b2017-10-09 16:05:24 +05302563 if (err) {
2564 if (link->port_state == FASTRPC_LINK_CONNECTING)
2565 link->port_state = FASTRPC_LINK_DISCONNECTED;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002566 goto bail;
c_mtharu6e1d26b2017-10-09 16:05:24 +05302567 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002568 me->channel[cid].chan = handle;
2569bail:
2570 return err;
2571}
2572
Sathish Ambley1ca68232017-01-19 10:32:55 -08002573static int fastrpc_debugfs_open(struct inode *inode, struct file *filp)
2574{
2575 filp->private_data = inode->i_private;
2576 return 0;
2577}
2578
2579static ssize_t fastrpc_debugfs_read(struct file *filp, char __user *buffer,
2580 size_t count, loff_t *position)
2581{
2582 struct fastrpc_file *fl = filp->private_data;
2583 struct hlist_node *n;
c_mtharue1a5ce12017-10-13 20:47:09 +05302584 struct fastrpc_buf *buf = NULL;
2585 struct fastrpc_mmap *map = NULL;
2586 struct smq_invoke_ctx *ictx = NULL;
Sathish Ambley1ca68232017-01-19 10:32:55 -08002587 struct fastrpc_channel_ctx *chan;
2588 struct fastrpc_session_ctx *sess;
2589 unsigned int len = 0;
2590 int i, j, ret = 0;
2591 char *fileinfo = NULL;
2592
2593 fileinfo = kzalloc(DEBUGFS_SIZE, GFP_KERNEL);
2594 if (!fileinfo)
2595 goto bail;
2596 if (fl == NULL) {
2597 for (i = 0; i < NUM_CHANNELS; i++) {
2598 chan = &gcinfo[i];
2599 len += scnprintf(fileinfo + len,
2600 DEBUGFS_SIZE - len, "%s\n\n",
2601 chan->name);
2602 len += scnprintf(fileinfo + len,
2603 DEBUGFS_SIZE - len, "%s %d\n",
2604 "sesscount:", chan->sesscount);
2605 for (j = 0; j < chan->sesscount; j++) {
2606 sess = &chan->session[j];
2607 len += scnprintf(fileinfo + len,
2608 DEBUGFS_SIZE - len,
2609 "%s%d\n\n", "SESSION", j);
2610 len += scnprintf(fileinfo + len,
2611 DEBUGFS_SIZE - len,
2612 "%s %d\n", "sid:",
2613 sess->smmu.cb);
2614 len += scnprintf(fileinfo + len,
2615 DEBUGFS_SIZE - len,
2616 "%s %d\n", "SECURE:",
2617 sess->smmu.secure);
2618 }
2619 }
2620 } else {
2621 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2622 "%s %d\n\n",
2623 "PROCESS_ID:", fl->tgid);
2624 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2625 "%s %d\n\n",
2626 "CHANNEL_ID:", fl->cid);
2627 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2628 "%s %d\n\n",
2629 "SSRCOUNT:", fl->ssrcount);
2630 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2631 "%s\n",
2632 "LIST OF BUFS:");
2633 spin_lock(&fl->hlock);
2634 hlist_for_each_entry_safe(buf, n, &fl->bufs, hn) {
2635 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Tharun Kumar Meruguce566452017-08-17 15:29:59 +05302636 "%s %pK %s %pK %s %llx\n", "buf:",
2637 buf, "buf->virt:", buf->virt,
2638 "buf->phys:", buf->phys);
Sathish Ambley1ca68232017-01-19 10:32:55 -08002639 }
2640 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2641 "\n%s\n",
2642 "LIST OF MAPS:");
2643 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
2644 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Tharun Kumar Meruguce566452017-08-17 15:29:59 +05302645 "%s %pK %s %lx %s %llx\n",
Sathish Ambley1ca68232017-01-19 10:32:55 -08002646 "map:", map,
2647 "map->va:", map->va,
2648 "map->phys:", map->phys);
2649 }
2650 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2651 "\n%s\n",
2652 "LIST OF PENDING SMQCONTEXTS:");
2653 hlist_for_each_entry_safe(ictx, n, &fl->clst.pending, hn) {
2654 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Tharun Kumar Meruguce566452017-08-17 15:29:59 +05302655 "%s %pK %s %u %s %u %s %u\n",
Sathish Ambley1ca68232017-01-19 10:32:55 -08002656 "smqcontext:", ictx,
2657 "sc:", ictx->sc,
2658 "tid:", ictx->pid,
2659 "handle", ictx->rpra->h);
2660 }
2661 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2662 "\n%s\n",
2663 "LIST OF INTERRUPTED SMQCONTEXTS:");
2664 hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) {
2665 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Tharun Kumar Meruguce566452017-08-17 15:29:59 +05302666 "%s %pK %s %u %s %u %s %u\n",
Sathish Ambley1ca68232017-01-19 10:32:55 -08002667 "smqcontext:", ictx,
2668 "sc:", ictx->sc,
2669 "tid:", ictx->pid,
2670 "handle", ictx->rpra->h);
2671 }
2672 spin_unlock(&fl->hlock);
2673 }
2674 if (len > DEBUGFS_SIZE)
2675 len = DEBUGFS_SIZE;
2676 ret = simple_read_from_buffer(buffer, count, position, fileinfo, len);
2677 kfree(fileinfo);
2678bail:
2679 return ret;
2680}
2681
2682static const struct file_operations debugfs_fops = {
2683 .open = fastrpc_debugfs_open,
2684 .read = fastrpc_debugfs_read,
2685};
Sathish Ambley36849af2017-02-02 09:35:55 -08002686static int fastrpc_channel_open(struct fastrpc_file *fl)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002687{
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002688 struct fastrpc_apps *me = &gfa;
Sathish Ambley36849af2017-02-02 09:35:55 -08002689 int cid, err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002690
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302691 mutex_lock(&me->smd_mutex);
2692
Sathish Ambley36849af2017-02-02 09:35:55 -08002693 VERIFY(err, fl && fl->sctx);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002694 if (err)
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302695 goto bail;
Sathish Ambley36849af2017-02-02 09:35:55 -08002696 cid = fl->cid;
c_mtharu314a4202017-11-15 22:09:17 +05302697 VERIFY(err, cid >= 0 && cid < NUM_CHANNELS);
2698 if (err)
2699 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05302700 if (me->channel[cid].ssrcount !=
2701 me->channel[cid].prevssrcount) {
2702 if (!me->channel[cid].issubsystemup) {
2703 VERIFY(err, 0);
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302704 if (err) {
2705 err = -ENOTCONN;
c_mtharue1a5ce12017-10-13 20:47:09 +05302706 goto bail;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302707 }
c_mtharue1a5ce12017-10-13 20:47:09 +05302708 }
2709 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002710 fl->ssrcount = me->channel[cid].ssrcount;
2711 if ((kref_get_unless_zero(&me->channel[cid].kref) == 0) ||
c_mtharue1a5ce12017-10-13 20:47:09 +05302712 (me->channel[cid].chan == NULL)) {
Tharun Kumar Meruguca0db5262017-05-10 12:53:12 +05302713 VERIFY(err, 0 == fastrpc_glink_register(cid, me));
2714 if (err)
2715 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002716 VERIFY(err, 0 == fastrpc_glink_open(cid));
2717 if (err)
2718 goto bail;
2719
Tharun Kumar Merugu53a8ec92017-07-14 15:52:49 +05302720 VERIFY(err,
2721 wait_for_completion_timeout(&me->channel[cid].workport,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002722 RPC_TIMEOUT));
2723 if (err) {
c_mtharue1a5ce12017-10-13 20:47:09 +05302724 me->channel[cid].chan = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002725 goto bail;
2726 }
2727 kref_init(&me->channel[cid].kref);
2728 pr_info("'opened /dev/%s c %d %d'\n", gcinfo[cid].name,
2729 MAJOR(me->dev_no), cid);
c_mtharu314a4202017-11-15 22:09:17 +05302730 err = glink_queue_rx_intent(me->channel[cid].chan, NULL,
2731 FASTRPC_GLINK_INTENT_LEN);
2732 err |= glink_queue_rx_intent(me->channel[cid].chan, NULL,
2733 FASTRPC_GLINK_INTENT_LEN);
Bruce Levy34c3c1c2017-07-31 17:08:58 -07002734 if (err)
Tharun Kumar Merugu88ba9252017-08-09 12:15:41 +05302735 pr_warn("adsprpc: initial intent fail for %d err %d\n",
2736 cid, err);
Tharun Kumar Merugud86fc8c2018-01-04 16:35:31 +05302737 if (cid == 0 && me->channel[cid].ssrcount !=
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002738 me->channel[cid].prevssrcount) {
c_mtharue1a5ce12017-10-13 20:47:09 +05302739 if (fastrpc_mmap_remove_ssr(fl))
2740 pr_err("ADSPRPC: SSR: Failed to unmap remote heap\n");
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002741 me->channel[cid].prevssrcount =
2742 me->channel[cid].ssrcount;
2743 }
2744 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002745
2746bail:
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302747 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002748 return err;
2749}
2750
Sathish Ambley36849af2017-02-02 09:35:55 -08002751static int fastrpc_device_open(struct inode *inode, struct file *filp)
2752{
2753 int err = 0;
Sathish Ambley567012b2017-03-06 11:55:04 -08002754 struct dentry *debugfs_file;
c_mtharue1a5ce12017-10-13 20:47:09 +05302755 struct fastrpc_file *fl = NULL;
Sathish Ambley36849af2017-02-02 09:35:55 -08002756 struct fastrpc_apps *me = &gfa;
2757
c_mtharue1a5ce12017-10-13 20:47:09 +05302758 VERIFY(err, NULL != (fl = kzalloc(sizeof(*fl), GFP_KERNEL)));
Sathish Ambley36849af2017-02-02 09:35:55 -08002759 if (err)
2760 return err;
Sathish Ambley567012b2017-03-06 11:55:04 -08002761 debugfs_file = debugfs_create_file(current->comm, 0644, debugfs_root,
2762 fl, &debugfs_fops);
Sathish Ambley36849af2017-02-02 09:35:55 -08002763 context_list_ctor(&fl->clst);
2764 spin_lock_init(&fl->hlock);
2765 INIT_HLIST_HEAD(&fl->maps);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302766 INIT_HLIST_HEAD(&fl->perf);
Sathish Ambley36849af2017-02-02 09:35:55 -08002767 INIT_HLIST_HEAD(&fl->bufs);
2768 INIT_HLIST_NODE(&fl->hn);
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05302769 fl->sessionid = 0;
Sathish Ambley36849af2017-02-02 09:35:55 -08002770 fl->tgid = current->tgid;
2771 fl->apps = me;
2772 fl->mode = FASTRPC_MODE_SERIAL;
2773 fl->cid = -1;
Sathish Ambley567012b2017-03-06 11:55:04 -08002774 if (debugfs_file != NULL)
2775 fl->debugfs_file = debugfs_file;
2776 memset(&fl->perf, 0, sizeof(fl->perf));
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05302777 fl->qos_request = 0;
Sathish Ambley36849af2017-02-02 09:35:55 -08002778 filp->private_data = fl;
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302779 mutex_init(&fl->map_mutex);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302780 mutex_init(&fl->fl_map_mutex);
Sathish Ambley36849af2017-02-02 09:35:55 -08002781 spin_lock(&me->hlock);
2782 hlist_add_head(&fl->hn, &me->drivers);
2783 spin_unlock(&me->hlock);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302784 mutex_init(&fl->perf_mutex);
Sathish Ambley36849af2017-02-02 09:35:55 -08002785 return 0;
2786}
2787
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002788static int fastrpc_get_info(struct fastrpc_file *fl, uint32_t *info)
2789{
2790 int err = 0;
Sathish Ambley36849af2017-02-02 09:35:55 -08002791 uint32_t cid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002792
c_mtharue1a5ce12017-10-13 20:47:09 +05302793 VERIFY(err, fl != NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002794 if (err)
2795 goto bail;
Sathish Ambley36849af2017-02-02 09:35:55 -08002796 if (fl->cid == -1) {
2797 cid = *info;
2798 VERIFY(err, cid < NUM_CHANNELS);
2799 if (err)
2800 goto bail;
2801 fl->cid = cid;
2802 fl->ssrcount = fl->apps->channel[cid].ssrcount;
2803 VERIFY(err, !fastrpc_session_alloc_locked(
2804 &fl->apps->channel[cid], 0, &fl->sctx));
2805 if (err)
2806 goto bail;
2807 }
Tharun Kumar Merugu80be7d62017-08-02 11:03:22 +05302808 VERIFY(err, fl->sctx != NULL);
2809 if (err)
2810 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002811 *info = (fl->sctx->smmu.enabled ? 1 : 0);
2812bail:
2813 return err;
2814}
2815
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05302816static int fastrpc_internal_control(struct fastrpc_file *fl,
2817 struct fastrpc_ioctl_control *cp)
2818{
2819 int err = 0;
2820 int latency;
2821
2822 VERIFY(err, !IS_ERR_OR_NULL(fl) && !IS_ERR_OR_NULL(fl->apps));
2823 if (err)
2824 goto bail;
2825 VERIFY(err, !IS_ERR_OR_NULL(cp));
2826 if (err)
2827 goto bail;
2828
2829 switch (cp->req) {
2830 case FASTRPC_CONTROL_LATENCY:
2831 latency = cp->lp.enable == FASTRPC_LATENCY_CTRL_ENB ?
2832 fl->apps->latency : PM_QOS_DEFAULT_VALUE;
2833 VERIFY(err, latency != 0);
2834 if (err)
2835 goto bail;
2836 if (!fl->qos_request) {
2837 pm_qos_add_request(&fl->pm_qos_req,
2838 PM_QOS_CPU_DMA_LATENCY, latency);
2839 fl->qos_request = 1;
2840 } else
2841 pm_qos_update_request(&fl->pm_qos_req, latency);
2842 break;
2843 default:
2844 err = -ENOTTY;
2845 break;
2846 }
2847bail:
2848 return err;
2849}
2850
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002851static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num,
2852 unsigned long ioctl_param)
2853{
2854 union {
Sathish Ambleybae51902017-07-03 15:00:49 -07002855 struct fastrpc_ioctl_invoke_crc inv;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002856 struct fastrpc_ioctl_mmap mmap;
2857 struct fastrpc_ioctl_munmap munmap;
c_mtharu7bd6a422017-10-17 18:15:37 +05302858 struct fastrpc_ioctl_munmap_fd munmap_fd;
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002859 struct fastrpc_ioctl_init_attrs init;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002860 struct fastrpc_ioctl_perf perf;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05302861 struct fastrpc_ioctl_control cp;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002862 } p;
2863 void *param = (char *)ioctl_param;
2864 struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data;
2865 int size = 0, err = 0;
2866 uint32_t info;
2867
c_mtharue1a5ce12017-10-13 20:47:09 +05302868 p.inv.fds = NULL;
2869 p.inv.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07002870 p.inv.crc = NULL;
tharun kumar9f899ea2017-07-03 17:07:03 +05302871 spin_lock(&fl->hlock);
2872 if (fl->file_close == 1) {
2873 err = EBADF;
2874 pr_warn("ADSPRPC: fastrpc_device_release is happening, So not sending any new requests to DSP");
2875 spin_unlock(&fl->hlock);
2876 goto bail;
2877 }
2878 spin_unlock(&fl->hlock);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002879
2880 switch (ioctl_num) {
2881 case FASTRPC_IOCTL_INVOKE:
2882 size = sizeof(struct fastrpc_ioctl_invoke);
Sathish Ambleybae51902017-07-03 15:00:49 -07002883 /* fall through */
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002884 case FASTRPC_IOCTL_INVOKE_FD:
2885 if (!size)
2886 size = sizeof(struct fastrpc_ioctl_invoke_fd);
2887 /* fall through */
2888 case FASTRPC_IOCTL_INVOKE_ATTRS:
2889 if (!size)
2890 size = sizeof(struct fastrpc_ioctl_invoke_attrs);
Sathish Ambleybae51902017-07-03 15:00:49 -07002891 /* fall through */
2892 case FASTRPC_IOCTL_INVOKE_CRC:
2893 if (!size)
2894 size = sizeof(struct fastrpc_ioctl_invoke_crc);
c_mtharue1a5ce12017-10-13 20:47:09 +05302895 K_COPY_FROM_USER(err, 0, &p.inv, param, size);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002896 if (err)
2897 goto bail;
2898 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, fl->mode,
2899 0, &p.inv)));
2900 if (err)
2901 goto bail;
2902 break;
2903 case FASTRPC_IOCTL_MMAP:
c_mtharue1a5ce12017-10-13 20:47:09 +05302904 K_COPY_FROM_USER(err, 0, &p.mmap, param,
2905 sizeof(p.mmap));
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05302906 if (err)
2907 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002908 VERIFY(err, 0 == (err = fastrpc_internal_mmap(fl, &p.mmap)));
2909 if (err)
2910 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05302911 K_COPY_TO_USER(err, 0, param, &p.mmap, sizeof(p.mmap));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002912 if (err)
2913 goto bail;
2914 break;
2915 case FASTRPC_IOCTL_MUNMAP:
c_mtharue1a5ce12017-10-13 20:47:09 +05302916 K_COPY_FROM_USER(err, 0, &p.munmap, param,
2917 sizeof(p.munmap));
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05302918 if (err)
2919 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002920 VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl,
2921 &p.munmap)));
2922 if (err)
2923 goto bail;
2924 break;
c_mtharu7bd6a422017-10-17 18:15:37 +05302925 case FASTRPC_IOCTL_MUNMAP_FD:
2926 K_COPY_FROM_USER(err, 0, &p.munmap_fd, param,
2927 sizeof(p.munmap_fd));
2928 if (err)
2929 goto bail;
2930 VERIFY(err, 0 == (err = fastrpc_internal_munmap_fd(fl,
2931 &p.munmap_fd)));
2932 if (err)
2933 goto bail;
2934 break;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002935 case FASTRPC_IOCTL_SETMODE:
2936 switch ((uint32_t)ioctl_param) {
2937 case FASTRPC_MODE_PARALLEL:
2938 case FASTRPC_MODE_SERIAL:
2939 fl->mode = (uint32_t)ioctl_param;
2940 break;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002941 case FASTRPC_MODE_PROFILE:
2942 fl->profile = (uint32_t)ioctl_param;
2943 break;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05302944 case FASTRPC_MODE_SESSION:
2945 fl->sessionid = 1;
2946 fl->tgid |= (1 << SESSION_ID_INDEX);
2947 break;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002948 default:
2949 err = -ENOTTY;
2950 break;
2951 }
2952 break;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002953 case FASTRPC_IOCTL_GETPERF:
c_mtharue1a5ce12017-10-13 20:47:09 +05302954 K_COPY_FROM_USER(err, 0, &p.perf,
2955 param, sizeof(p.perf));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002956 if (err)
2957 goto bail;
2958 p.perf.numkeys = sizeof(struct fastrpc_perf)/sizeof(int64_t);
2959 if (p.perf.keys) {
2960 char *keys = PERF_KEYS;
2961
c_mtharue1a5ce12017-10-13 20:47:09 +05302962 K_COPY_TO_USER(err, 0, (void *)p.perf.keys,
2963 keys, strlen(keys)+1);
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002964 if (err)
2965 goto bail;
2966 }
2967 if (p.perf.data) {
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302968 struct fastrpc_perf *perf = NULL, *fperf = NULL;
2969 struct hlist_node *n = NULL;
2970
2971 mutex_lock(&fl->perf_mutex);
2972 hlist_for_each_entry_safe(perf, n, &fl->perf, hn) {
2973 if (perf->tid == current->pid) {
2974 fperf = perf;
2975 break;
2976 }
2977 }
2978
2979 mutex_unlock(&fl->perf_mutex);
2980
2981 if (fperf) {
2982 K_COPY_TO_USER(err, 0, (void *)p.perf.data,
2983 fperf, sizeof(*fperf));
2984 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002985 }
c_mtharue1a5ce12017-10-13 20:47:09 +05302986 K_COPY_TO_USER(err, 0, param, &p.perf, sizeof(p.perf));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002987 if (err)
2988 goto bail;
2989 break;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05302990 case FASTRPC_IOCTL_CONTROL:
c_mtharue1a5ce12017-10-13 20:47:09 +05302991 K_COPY_FROM_USER(err, 0, &p.cp, param,
2992 sizeof(p.cp));
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05302993 if (err)
2994 goto bail;
2995 VERIFY(err, 0 == (err = fastrpc_internal_control(fl, &p.cp)));
2996 if (err)
2997 goto bail;
2998 break;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002999 case FASTRPC_IOCTL_GETINFO:
c_mtharue1a5ce12017-10-13 20:47:09 +05303000 K_COPY_FROM_USER(err, 0, &info, param, sizeof(info));
Sathish Ambley36849af2017-02-02 09:35:55 -08003001 if (err)
3002 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003003 VERIFY(err, 0 == (err = fastrpc_get_info(fl, &info)));
3004 if (err)
3005 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05303006 K_COPY_TO_USER(err, 0, param, &info, sizeof(info));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003007 if (err)
3008 goto bail;
3009 break;
3010 case FASTRPC_IOCTL_INIT:
Sathish Ambleyd6300c32017-01-18 09:50:43 -08003011 p.init.attrs = 0;
3012 p.init.siglen = 0;
3013 size = sizeof(struct fastrpc_ioctl_init);
3014 /* fall through */
3015 case FASTRPC_IOCTL_INIT_ATTRS:
3016 if (!size)
3017 size = sizeof(struct fastrpc_ioctl_init_attrs);
c_mtharue1a5ce12017-10-13 20:47:09 +05303018 K_COPY_FROM_USER(err, 0, &p.init, param, size);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003019 if (err)
3020 goto bail;
Tharun Kumar Merugu4ea0eac2017-08-22 11:42:51 +05303021 VERIFY(err, p.init.init.filelen >= 0 &&
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05303022 p.init.init.filelen < INIT_FILELEN_MAX);
3023 if (err)
3024 goto bail;
3025 VERIFY(err, p.init.init.memlen >= 0 &&
3026 p.init.init.memlen < INIT_MEMLEN_MAX);
Tharun Kumar Merugu4ea0eac2017-08-22 11:42:51 +05303027 if (err)
3028 goto bail;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303029 VERIFY(err, 0 == (err = fastrpc_init_process(fl, &p.init)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003030 if (err)
3031 goto bail;
3032 break;
3033
3034 default:
3035 err = -ENOTTY;
3036 pr_info("bad ioctl: %d\n", ioctl_num);
3037 break;
3038 }
3039 bail:
3040 return err;
3041}
3042
3043static int fastrpc_restart_notifier_cb(struct notifier_block *nb,
3044 unsigned long code,
3045 void *data)
3046{
3047 struct fastrpc_apps *me = &gfa;
3048 struct fastrpc_channel_ctx *ctx;
c_mtharue1a5ce12017-10-13 20:47:09 +05303049 struct notif_data *notifdata = data;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003050 int cid;
3051
3052 ctx = container_of(nb, struct fastrpc_channel_ctx, nb);
3053 cid = ctx - &me->channel[0];
3054 if (code == SUBSYS_BEFORE_SHUTDOWN) {
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303055 mutex_lock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003056 ctx->ssrcount++;
c_mtharue1a5ce12017-10-13 20:47:09 +05303057 ctx->issubsystemup = 0;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303058 if (ctx->chan) {
3059 fastrpc_glink_close(ctx->chan, cid);
3060 ctx->chan = NULL;
3061 pr_info("'restart notifier: closed /dev/%s c %d %d'\n",
3062 gcinfo[cid].name, MAJOR(me->dev_no), cid);
3063 }
3064 mutex_unlock(&me->smd_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05303065 if (cid == 0)
3066 me->staticpd_flags = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003067 fastrpc_notify_drivers(me, cid);
c_mtharue1a5ce12017-10-13 20:47:09 +05303068 } else if (code == SUBSYS_RAMDUMP_NOTIFICATION) {
3069 if (me->channel[0].remoteheap_ramdump_dev &&
3070 notifdata->enable_ramdump) {
3071 me->channel[0].ramdumpenabled = 1;
3072 }
3073 } else if (code == SUBSYS_AFTER_POWERUP) {
3074 ctx->issubsystemup = 1;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003075 }
3076
3077 return NOTIFY_DONE;
3078}
3079
3080static const struct file_operations fops = {
3081 .open = fastrpc_device_open,
3082 .release = fastrpc_device_release,
3083 .unlocked_ioctl = fastrpc_device_ioctl,
3084 .compat_ioctl = compat_fastrpc_device_ioctl,
3085};
3086
3087static const struct of_device_id fastrpc_match_table[] = {
3088 { .compatible = "qcom,msm-fastrpc-adsp", },
3089 { .compatible = "qcom,msm-fastrpc-compute", },
3090 { .compatible = "qcom,msm-fastrpc-compute-cb", },
3091 { .compatible = "qcom,msm-adsprpc-mem-region", },
3092 {}
3093};
3094
3095static int fastrpc_cb_probe(struct device *dev)
3096{
3097 struct fastrpc_channel_ctx *chan;
3098 struct fastrpc_session_ctx *sess;
3099 struct of_phandle_args iommuspec;
3100 const char *name;
3101 unsigned int start = 0x80000000;
3102 int err = 0, i;
3103 int secure_vmid = VMID_CP_PIXEL;
3104
c_mtharue1a5ce12017-10-13 20:47:09 +05303105 VERIFY(err, NULL != (name = of_get_property(dev->of_node,
3106 "label", NULL)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003107 if (err)
3108 goto bail;
3109 for (i = 0; i < NUM_CHANNELS; i++) {
3110 if (!gcinfo[i].name)
3111 continue;
3112 if (!strcmp(name, gcinfo[i].name))
3113 break;
3114 }
3115 VERIFY(err, i < NUM_CHANNELS);
3116 if (err)
3117 goto bail;
3118 chan = &gcinfo[i];
3119 VERIFY(err, chan->sesscount < NUM_SESSIONS);
3120 if (err)
3121 goto bail;
3122
3123 VERIFY(err, !of_parse_phandle_with_args(dev->of_node, "iommus",
3124 "#iommu-cells", 0, &iommuspec));
3125 if (err)
3126 goto bail;
3127 sess = &chan->session[chan->sesscount];
3128 sess->smmu.cb = iommuspec.args[0] & 0xf;
3129 sess->used = 0;
3130 sess->smmu.coherent = of_property_read_bool(dev->of_node,
3131 "dma-coherent");
3132 sess->smmu.secure = of_property_read_bool(dev->of_node,
3133 "qcom,secure-context-bank");
3134 if (sess->smmu.secure)
3135 start = 0x60000000;
3136 VERIFY(err, !IS_ERR_OR_NULL(sess->smmu.mapping =
3137 arm_iommu_create_mapping(&platform_bus_type,
Tharun Kumar Meruguca183f92017-04-27 17:43:27 +05303138 start, 0x78000000)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003139 if (err)
3140 goto bail;
3141
3142 if (sess->smmu.secure)
3143 iommu_domain_set_attr(sess->smmu.mapping->domain,
3144 DOMAIN_ATTR_SECURE_VMID,
3145 &secure_vmid);
3146
3147 VERIFY(err, !arm_iommu_attach_device(dev, sess->smmu.mapping));
3148 if (err)
3149 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05303150 sess->smmu.dev = dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003151 sess->smmu.enabled = 1;
3152 chan->sesscount++;
Sathish Ambley1ca68232017-01-19 10:32:55 -08003153 debugfs_global_file = debugfs_create_file("global", 0644, debugfs_root,
3154 NULL, &debugfs_fops);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003155bail:
3156 return err;
3157}
3158
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05303159static void init_secure_vmid_list(struct device *dev, char *prop_name,
3160 struct secure_vm *destvm)
3161{
3162 int err = 0;
3163 u32 len = 0, i = 0;
3164 u32 *rhvmlist = NULL;
3165 u32 *rhvmpermlist = NULL;
3166
3167 if (!of_find_property(dev->of_node, prop_name, &len))
3168 goto bail;
3169 if (len == 0)
3170 goto bail;
3171 len /= sizeof(u32);
3172 VERIFY(err, NULL != (rhvmlist = kcalloc(len, sizeof(u32), GFP_KERNEL)));
3173 if (err)
3174 goto bail;
3175 VERIFY(err, NULL != (rhvmpermlist = kcalloc(len, sizeof(u32),
3176 GFP_KERNEL)));
3177 if (err)
3178 goto bail;
3179 for (i = 0; i < len; i++) {
3180 err = of_property_read_u32_index(dev->of_node, prop_name, i,
3181 &rhvmlist[i]);
3182 rhvmpermlist[i] = PERM_READ | PERM_WRITE | PERM_EXEC;
3183 pr_info("ADSPRPC: Secure VMID = %d", rhvmlist[i]);
3184 if (err) {
3185 pr_err("ADSPRPC: Failed to read VMID\n");
3186 goto bail;
3187 }
3188 }
3189 destvm->vmid = rhvmlist;
3190 destvm->vmperm = rhvmpermlist;
3191 destvm->vmcount = len;
3192bail:
3193 if (err) {
3194 kfree(rhvmlist);
3195 kfree(rhvmpermlist);
3196 }
3197}
3198
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003199static int fastrpc_probe(struct platform_device *pdev)
3200{
3201 int err = 0;
3202 struct fastrpc_apps *me = &gfa;
3203 struct device *dev = &pdev->dev;
3204 struct smq_phy_page range;
3205 struct device_node *ion_node, *node;
3206 struct platform_device *ion_pdev;
3207 struct cma *cma;
3208 uint32_t val;
3209
c_mtharu63ffc012017-11-16 15:26:56 +05303210
3211 if (of_device_is_compatible(dev->of_node,
3212 "qcom,msm-fastrpc-compute")) {
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05303213 init_secure_vmid_list(dev, "qcom,adsp-remoteheap-vmid",
3214 &gcinfo[0].rhvm);
c_mtharu63ffc012017-11-16 15:26:56 +05303215
c_mtharu63ffc012017-11-16 15:26:56 +05303216
3217 of_property_read_u32(dev->of_node, "qcom,rpc-latency-us",
3218 &me->latency);
3219 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003220 if (of_device_is_compatible(dev->of_node,
3221 "qcom,msm-fastrpc-compute-cb"))
3222 return fastrpc_cb_probe(dev);
3223
3224 if (of_device_is_compatible(dev->of_node,
3225 "qcom,msm-adsprpc-mem-region")) {
3226 me->dev = dev;
3227 range.addr = 0;
3228 ion_node = of_find_compatible_node(NULL, NULL, "qcom,msm-ion");
3229 if (ion_node) {
3230 for_each_available_child_of_node(ion_node, node) {
3231 if (of_property_read_u32(node, "reg", &val))
3232 continue;
3233 if (val != ION_ADSP_HEAP_ID)
3234 continue;
3235 ion_pdev = of_find_device_by_node(node);
3236 if (!ion_pdev)
3237 break;
3238 cma = dev_get_cma_area(&ion_pdev->dev);
3239 if (cma) {
3240 range.addr = cma_get_base(cma);
3241 range.size = (size_t)cma_get_size(cma);
3242 }
3243 break;
3244 }
3245 }
3246 if (range.addr) {
3247 int srcVM[1] = {VMID_HLOS};
3248 int destVM[4] = {VMID_HLOS, VMID_MSS_MSA, VMID_SSC_Q6,
3249 VMID_ADSP_Q6};
Sathish Ambley84d11862017-05-15 14:36:05 -07003250 int destVMperm[4] = {PERM_READ | PERM_WRITE | PERM_EXEC,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003251 PERM_READ | PERM_WRITE | PERM_EXEC,
3252 PERM_READ | PERM_WRITE | PERM_EXEC,
3253 PERM_READ | PERM_WRITE | PERM_EXEC,
3254 };
3255
3256 VERIFY(err, !hyp_assign_phys(range.addr, range.size,
3257 srcVM, 1, destVM, destVMperm, 4));
3258 if (err)
3259 goto bail;
3260 }
3261 return 0;
3262 }
3263
3264 VERIFY(err, !of_platform_populate(pdev->dev.of_node,
3265 fastrpc_match_table,
3266 NULL, &pdev->dev));
3267 if (err)
3268 goto bail;
3269bail:
3270 return err;
3271}
3272
3273static void fastrpc_deinit(void)
3274{
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303275 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003276 struct fastrpc_channel_ctx *chan = gcinfo;
3277 int i, j;
3278
3279 for (i = 0; i < NUM_CHANNELS; i++, chan++) {
3280 if (chan->chan) {
3281 kref_put_mutex(&chan->kref,
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303282 fastrpc_channel_close, &me->smd_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05303283 chan->chan = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003284 }
3285 for (j = 0; j < NUM_SESSIONS; j++) {
3286 struct fastrpc_session_ctx *sess = &chan->session[j];
c_mtharue1a5ce12017-10-13 20:47:09 +05303287 if (sess->smmu.dev) {
3288 arm_iommu_detach_device(sess->smmu.dev);
3289 sess->smmu.dev = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003290 }
3291 if (sess->smmu.mapping) {
3292 arm_iommu_release_mapping(sess->smmu.mapping);
c_mtharue1a5ce12017-10-13 20:47:09 +05303293 sess->smmu.mapping = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003294 }
3295 }
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05303296 kfree(chan->rhvm.vmid);
3297 kfree(chan->rhvm.vmperm);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003298 }
3299}
3300
3301static struct platform_driver fastrpc_driver = {
3302 .probe = fastrpc_probe,
3303 .driver = {
3304 .name = "fastrpc",
3305 .owner = THIS_MODULE,
3306 .of_match_table = fastrpc_match_table,
3307 },
3308};
3309
3310static int __init fastrpc_device_init(void)
3311{
3312 struct fastrpc_apps *me = &gfa;
c_mtharue1a5ce12017-10-13 20:47:09 +05303313 struct device *dev = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003314 int err = 0, i;
3315
3316 memset(me, 0, sizeof(*me));
3317
3318 fastrpc_init(me);
3319 me->dev = NULL;
3320 VERIFY(err, 0 == platform_driver_register(&fastrpc_driver));
3321 if (err)
3322 goto register_bail;
3323 VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, NUM_CHANNELS,
3324 DEVICE_NAME));
3325 if (err)
3326 goto alloc_chrdev_bail;
3327 cdev_init(&me->cdev, &fops);
3328 me->cdev.owner = THIS_MODULE;
3329 VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0),
Sathish Ambley36849af2017-02-02 09:35:55 -08003330 1));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003331 if (err)
3332 goto cdev_init_bail;
3333 me->class = class_create(THIS_MODULE, "fastrpc");
3334 VERIFY(err, !IS_ERR(me->class));
3335 if (err)
3336 goto class_create_bail;
3337 me->compat = (fops.compat_ioctl == NULL) ? 0 : 1;
Sathish Ambley36849af2017-02-02 09:35:55 -08003338 dev = device_create(me->class, NULL,
3339 MKDEV(MAJOR(me->dev_no), 0),
3340 NULL, gcinfo[0].name);
3341 VERIFY(err, !IS_ERR_OR_NULL(dev));
3342 if (err)
3343 goto device_create_bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003344 for (i = 0; i < NUM_CHANNELS; i++) {
Sathish Ambley36849af2017-02-02 09:35:55 -08003345 me->channel[i].dev = dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003346 me->channel[i].ssrcount = 0;
3347 me->channel[i].prevssrcount = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05303348 me->channel[i].issubsystemup = 1;
3349 me->channel[i].ramdumpenabled = 0;
3350 me->channel[i].remoteheap_ramdump_dev = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003351 me->channel[i].nb.notifier_call = fastrpc_restart_notifier_cb;
3352 me->channel[i].handle = subsys_notif_register_notifier(
3353 gcinfo[i].subsys,
3354 &me->channel[i].nb);
3355 }
3356
3357 me->client = msm_ion_client_create(DEVICE_NAME);
3358 VERIFY(err, !IS_ERR_OR_NULL(me->client));
3359 if (err)
3360 goto device_create_bail;
Sathish Ambley1ca68232017-01-19 10:32:55 -08003361 debugfs_root = debugfs_create_dir("adsprpc", NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003362 return 0;
3363device_create_bail:
3364 for (i = 0; i < NUM_CHANNELS; i++) {
Sathish Ambley36849af2017-02-02 09:35:55 -08003365 if (me->channel[i].handle)
3366 subsys_notif_unregister_notifier(me->channel[i].handle,
3367 &me->channel[i].nb);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003368 }
Sathish Ambley36849af2017-02-02 09:35:55 -08003369 if (!IS_ERR_OR_NULL(dev))
3370 device_destroy(me->class, MKDEV(MAJOR(me->dev_no), 0));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003371 class_destroy(me->class);
3372class_create_bail:
3373 cdev_del(&me->cdev);
3374cdev_init_bail:
3375 unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
3376alloc_chrdev_bail:
3377register_bail:
3378 fastrpc_deinit();
3379 return err;
3380}
3381
3382static void __exit fastrpc_device_exit(void)
3383{
3384 struct fastrpc_apps *me = &gfa;
3385 int i;
3386
3387 fastrpc_file_list_dtor(me);
3388 fastrpc_deinit();
3389 for (i = 0; i < NUM_CHANNELS; i++) {
3390 if (!gcinfo[i].name)
3391 continue;
3392 device_destroy(me->class, MKDEV(MAJOR(me->dev_no), i));
3393 subsys_notif_unregister_notifier(me->channel[i].handle,
3394 &me->channel[i].nb);
3395 }
3396 class_destroy(me->class);
3397 cdev_del(&me->cdev);
3398 unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
3399 ion_client_destroy(me->client);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003400 debugfs_remove_recursive(debugfs_root);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003401}
3402
3403late_initcall(fastrpc_device_init);
3404module_exit(fastrpc_device_exit);
3405
3406MODULE_LICENSE("GPL v2");