blob: f62d96dcf6360a8d57ec791da3de04fdf5cd3c4e [file] [log] [blame]
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001/*
Tharun Kumar Merugucc2e11e2019-02-02 01:22:47 +05302 * Copyright (c) 2012-2019, 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>
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +053029#include <soc/qcom/smd.h>
Sathish Ambley69e1ab02016-10-18 10:28:15 -070030#include <soc/qcom/subsystem_notif.h>
31#include <soc/qcom/subsystem_restart.h>
Tharun Kumar Merugudf860662018-01-17 19:59:50 +053032#include <soc/qcom/service-notifier.h>
33#include <soc/qcom/service-locator.h>
Sathish Ambley69e1ab02016-10-18 10:28:15 -070034#include <linux/scatterlist.h>
35#include <linux/fs.h>
36#include <linux/uaccess.h>
37#include <linux/device.h>
38#include <linux/of.h>
39#include <linux/of_address.h>
40#include <linux/of_platform.h>
41#include <linux/dma-contiguous.h>
42#include <linux/cma.h>
43#include <linux/iommu.h>
44#include <linux/kref.h>
45#include <linux/sort.h>
46#include <linux/msm_dma_iommu_mapping.h>
47#include <asm/dma-iommu.h>
c_mtharue1a5ce12017-10-13 20:47:09 +053048#include <soc/qcom/scm.h>
Sathish Ambley69e1ab02016-10-18 10:28:15 -070049#include "adsprpc_compat.h"
50#include "adsprpc_shared.h"
c_mtharue1a5ce12017-10-13 20:47:09 +053051#include <soc/qcom/ramdump.h>
Sathish Ambley1ca68232017-01-19 10:32:55 -080052#include <linux/debugfs.h>
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +053053#include <linux/pm_qos.h>
Sathish Ambley69e1ab02016-10-18 10:28:15 -070054#define TZ_PIL_PROTECT_MEM_SUBSYS_ID 0x0C
55#define TZ_PIL_CLEAR_PROTECT_MEM_SUBSYS_ID 0x0D
56#define TZ_PIL_AUTH_QDSP6_PROC 1
c_mtharue1a5ce12017-10-13 20:47:09 +053057#define ADSP_MMAP_HEAP_ADDR 4
58#define ADSP_MMAP_REMOTE_HEAP_ADDR 8
Tharun Kumar Merugue073de72018-07-30 23:57:47 +053059#define ADSP_MMAP_ADD_PAGES 0x1000
Tharun Kumar Merugu35a94a52018-02-01 21:09:04 +053060#define FASTRPC_DMAHANDLE_NOMAP (16)
61
Sathish Ambley69e1ab02016-10-18 10:28:15 -070062#define FASTRPC_ENOSUCH 39
63#define VMID_SSC_Q6 5
64#define VMID_ADSP_Q6 6
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +053065#define DEBUGFS_SIZE 3072
66#define UL_SIZE 25
67#define PID_SIZE 10
Sathish Ambley69e1ab02016-10-18 10:28:15 -070068
Tharun Kumar Merugudf860662018-01-17 19:59:50 +053069#define AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME "audio_pdr_adsprpc"
70#define AUDIO_PDR_ADSP_SERVICE_NAME "avs/audio"
71
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +053072#define SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME "sensors_pdr_adsprpc"
73#define SENSORS_PDR_ADSP_SERVICE_NAME "tms/servreg"
74
Sathish Ambley69e1ab02016-10-18 10:28:15 -070075#define RPC_TIMEOUT (5 * HZ)
76#define BALIGN 128
77#define NUM_CHANNELS 4 /* adsp, mdsp, slpi, cdsp*/
78#define NUM_SESSIONS 9 /*8 compute, 1 cpz*/
Sathish Ambleybae51902017-07-03 15:00:49 -070079#define M_FDLIST (16)
80#define M_CRCLIST (64)
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +053081#define SESSION_ID_INDEX (30)
c_mtharufdac6892017-10-12 13:09:01 +053082#define FASTRPC_CTX_MAGIC (0xbeeddeed)
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +053083#define FASTRPC_CTX_MAX (256)
84#define FASTRPC_CTXID_MASK (0xFF0)
Tharun Kumar Merugud996b262018-07-18 22:28:53 +053085#define NUM_DEVICES 2 /* adsprpc-smd, adsprpc-smd-secure */
86#define MINOR_NUM_DEV 0
87#define MINOR_NUM_SECURE_DEV 1
88#define NON_SECURE_CHANNEL 0
89#define SECURE_CHANNEL 1
90
91#define ADSP_DOMAIN_ID (0)
92#define MDSP_DOMAIN_ID (1)
93#define SDSP_DOMAIN_ID (2)
94#define CDSP_DOMAIN_ID (3)
Sathish Ambley69e1ab02016-10-18 10:28:15 -070095
96#define IS_CACHE_ALIGNED(x) (((x) & ((L1_CACHE_BYTES)-1)) == 0)
97
98#define FASTRPC_LINK_STATE_DOWN (0x0)
99#define FASTRPC_LINK_STATE_UP (0x1)
100#define FASTRPC_LINK_DISCONNECTED (0x0)
101#define FASTRPC_LINK_CONNECTING (0x1)
102#define FASTRPC_LINK_CONNECTED (0x3)
103#define FASTRPC_LINK_DISCONNECTING (0x7)
c_mtharu314a4202017-11-15 22:09:17 +0530104#define FASTRPC_LINK_REMOTE_DISCONNECTING (0x8)
105#define FASTRPC_GLINK_INTENT_LEN (64)
Tharun Kumar Meruguc42c6e22018-05-29 15:50:46 +0530106#define FASTRPC_GLINK_INTENT_NUM (16)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700107
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530108#define PERF_KEYS \
109 "count:flush:map:copy:glink:getargs:putargs:invalidate:invoke:tid:ptr"
Tharun Kumar Merugucc2e11e2019-02-02 01:22:47 +0530110#define FASTRPC_STATIC_HANDLE_KERNEL (1)
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800111#define FASTRPC_STATIC_HANDLE_LISTENER (3)
112#define FASTRPC_STATIC_HANDLE_MAX (20)
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +0530113#define FASTRPC_LATENCY_CTRL_ENB (1)
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800114
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +0530115#define INIT_FILELEN_MAX (2*1024*1024)
116#define INIT_MEMLEN_MAX (8*1024*1024)
117
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800118#define PERF_END (void)0
119
120#define PERF(enb, cnt, ff) \
121 {\
122 struct timespec startT = {0};\
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530123 int64_t *counter = cnt;\
124 if (enb && counter) {\
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800125 getnstimeofday(&startT);\
126 } \
127 ff ;\
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530128 if (enb && counter) {\
129 *counter += getnstimediff(&startT);\
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800130 } \
131 }
132
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530133#define GET_COUNTER(perf_ptr, offset) \
134 (perf_ptr != NULL ?\
135 (((offset >= 0) && (offset < PERF_KEY_MAX)) ?\
136 (int64_t *)(perf_ptr + offset)\
137 : (int64_t *)NULL) : (int64_t *)NULL)
138
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700139static int fastrpc_glink_open(int cid);
140static void fastrpc_glink_close(void *chan, int cid);
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +0530141static int fastrpc_pdr_notifier_cb(struct notifier_block *nb,
Tharun Kumar Merugudf860662018-01-17 19:59:50 +0530142 unsigned long code,
143 void *data);
Sathish Ambley1ca68232017-01-19 10:32:55 -0800144static struct dentry *debugfs_root;
145static struct dentry *debugfs_global_file;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700146
147static inline uint64_t buf_page_start(uint64_t buf)
148{
149 uint64_t start = (uint64_t) buf & PAGE_MASK;
150 return start;
151}
152
153static inline uint64_t buf_page_offset(uint64_t buf)
154{
155 uint64_t offset = (uint64_t) buf & (PAGE_SIZE - 1);
156 return offset;
157}
158
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530159static inline uint64_t buf_num_pages(uint64_t buf, size_t len)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700160{
161 uint64_t start = buf_page_start(buf) >> PAGE_SHIFT;
162 uint64_t end = (((uint64_t) buf + len - 1) & PAGE_MASK) >> PAGE_SHIFT;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530163 uint64_t nPages = end - start + 1;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700164 return nPages;
165}
166
167static inline uint64_t buf_page_size(uint32_t size)
168{
169 uint64_t sz = (size + (PAGE_SIZE - 1)) & PAGE_MASK;
170
171 return sz > PAGE_SIZE ? sz : PAGE_SIZE;
172}
173
174static inline void *uint64_to_ptr(uint64_t addr)
175{
176 void *ptr = (void *)((uintptr_t)addr);
177
178 return ptr;
179}
180
181static inline uint64_t ptr_to_uint64(void *ptr)
182{
183 uint64_t addr = (uint64_t)((uintptr_t)ptr);
184
185 return addr;
186}
187
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +0530188struct secure_vm {
189 int *vmid;
190 int *vmperm;
191 int vmcount;
192};
193
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700194struct fastrpc_file;
195
196struct fastrpc_buf {
197 struct hlist_node hn;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +0530198 struct hlist_node hn_rem;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700199 struct fastrpc_file *fl;
200 void *virt;
201 uint64_t phys;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530202 size_t size;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +0530203 unsigned long dma_attr;
204 uintptr_t raddr;
205 uint32_t flags;
206 int remote;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700207};
208
209struct fastrpc_ctx_lst;
210
211struct overlap {
212 uintptr_t start;
213 uintptr_t end;
214 int raix;
215 uintptr_t mstart;
216 uintptr_t mend;
217 uintptr_t offset;
218};
219
220struct smq_invoke_ctx {
221 struct hlist_node hn;
222 struct completion work;
223 int retval;
224 int pid;
225 int tgid;
226 remote_arg_t *lpra;
227 remote_arg64_t *rpra;
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +0530228 remote_arg64_t *lrpra; /* Local copy of rpra for put_args */
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700229 int *fds;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700230 struct fastrpc_mmap **maps;
231 struct fastrpc_buf *buf;
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +0530232 struct fastrpc_buf *lbuf;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530233 size_t used;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700234 struct fastrpc_file *fl;
235 uint32_t sc;
236 struct overlap *overs;
237 struct overlap **overps;
238 struct smq_msg msg;
c_mtharufdac6892017-10-12 13:09:01 +0530239 unsigned int magic;
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +0530240 unsigned int *attrs;
241 uint32_t *crc;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +0530242 uint64_t ctxid;
Mohammed Nayeem Ur Rahman32ba95d2019-07-26 17:31:37 +0530243 void *handle;
244 const void *ptr;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700245};
246
247struct fastrpc_ctx_lst {
248 struct hlist_head pending;
249 struct hlist_head interrupted;
250};
251
252struct fastrpc_smmu {
c_mtharue1a5ce12017-10-13 20:47:09 +0530253 struct device *dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700254 struct dma_iommu_mapping *mapping;
255 int cb;
256 int enabled;
257 int faults;
258 int secure;
259 int coherent;
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +0530260 int sharedcb;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700261};
262
263struct fastrpc_session_ctx {
264 struct device *dev;
265 struct fastrpc_smmu smmu;
266 int used;
267};
268
Tharun Kumar Merugudf860662018-01-17 19:59:50 +0530269struct fastrpc_static_pd {
270 char *spdname;
271 struct notifier_block pdrnb;
272 struct notifier_block get_service_nb;
273 void *pdrhandle;
274 int pdrcount;
275 int prevpdrcount;
276 int ispdup;
277};
278
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700279struct fastrpc_glink_info {
280 int link_state;
281 int port_state;
282 struct glink_open_config cfg;
283 struct glink_link_info link_info;
284 void *link_notify_handle;
285};
286
287struct fastrpc_channel_ctx {
288 char *name;
289 char *subsys;
290 void *chan;
291 struct device *dev;
292 struct fastrpc_session_ctx session[NUM_SESSIONS];
Tharun Kumar Merugudf860662018-01-17 19:59:50 +0530293 struct fastrpc_static_pd spd[NUM_SESSIONS];
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700294 struct completion work;
Tharun Kumar Merugu53a8ec92017-07-14 15:52:49 +0530295 struct completion workport;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700296 struct notifier_block nb;
297 struct kref kref;
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +0530298 int channel;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700299 int sesscount;
300 int ssrcount;
301 void *handle;
302 int prevssrcount;
c_mtharue1a5ce12017-10-13 20:47:09 +0530303 int issubsystemup;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700304 int vmid;
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +0530305 struct secure_vm rhvm;
c_mtharue1a5ce12017-10-13 20:47:09 +0530306 int ramdumpenabled;
307 void *remoteheap_ramdump_dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700308 struct fastrpc_glink_info link;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +0530309 /* Indicates, if channel is restricted to secure node only */
310 int secure;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700311};
312
313struct fastrpc_apps {
314 struct fastrpc_channel_ctx *channel;
315 struct cdev cdev;
316 struct class *class;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +0530317 struct mutex smd_mutex;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700318 struct smq_phy_page range;
319 struct hlist_head maps;
c_mtharue1a5ce12017-10-13 20:47:09 +0530320 uint32_t staticpd_flags;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700321 dev_t dev_no;
322 int compat;
323 struct hlist_head drivers;
324 spinlock_t hlock;
325 struct ion_client *client;
326 struct device *dev;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +0530327 unsigned int latency;
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +0530328 bool glink;
329 bool legacy;
zhaochenfc798572018-08-17 15:32:37 +0800330 bool secure_flag;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +0530331 spinlock_t ctxlock;
332 struct smq_invoke_ctx *ctxtable[FASTRPC_CTX_MAX];
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700333};
334
335struct fastrpc_mmap {
336 struct hlist_node hn;
337 struct fastrpc_file *fl;
338 struct fastrpc_apps *apps;
339 int fd;
340 uint32_t flags;
341 struct dma_buf *buf;
342 struct sg_table *table;
343 struct dma_buf_attachment *attach;
344 struct ion_handle *handle;
345 uint64_t phys;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530346 size_t size;
347 uintptr_t va;
348 size_t len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700349 int refs;
350 uintptr_t raddr;
351 int uncached;
352 int secure;
353 uintptr_t attr;
354};
355
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530356enum fastrpc_perfkeys {
357 PERF_COUNT = 0,
358 PERF_FLUSH = 1,
359 PERF_MAP = 2,
360 PERF_COPY = 3,
361 PERF_LINK = 4,
362 PERF_GETARGS = 5,
363 PERF_PUTARGS = 6,
364 PERF_INVARGS = 7,
365 PERF_INVOKE = 8,
366 PERF_KEY_MAX = 9,
367};
368
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800369struct fastrpc_perf {
370 int64_t count;
371 int64_t flush;
372 int64_t map;
373 int64_t copy;
374 int64_t link;
375 int64_t getargs;
376 int64_t putargs;
377 int64_t invargs;
378 int64_t invoke;
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530379 int64_t tid;
380 struct hlist_node hn;
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800381};
382
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700383struct fastrpc_file {
384 struct hlist_node hn;
385 spinlock_t hlock;
386 struct hlist_head maps;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +0530387 struct hlist_head cached_bufs;
388 struct hlist_head remote_bufs;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700389 struct fastrpc_ctx_lst clst;
390 struct fastrpc_session_ctx *sctx;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +0530391 struct fastrpc_buf *init_mem;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700392 struct fastrpc_session_ctx *secsctx;
393 uint32_t mode;
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800394 uint32_t profile;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +0530395 int sessionid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700396 int tgid;
397 int cid;
398 int ssrcount;
399 int pd;
Tharun Kumar Merugudf860662018-01-17 19:59:50 +0530400 char *spdname;
tharun kumar9f899ea2017-07-03 17:07:03 +0530401 int file_close;
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +0530402 int sharedcb;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700403 struct fastrpc_apps *apps;
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530404 struct hlist_head perf;
Sathish Ambley1ca68232017-01-19 10:32:55 -0800405 struct dentry *debugfs_file;
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530406 struct mutex perf_mutex;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +0530407 struct pm_qos_request pm_qos_req;
408 int qos_request;
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +0530409 struct mutex map_mutex;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +0530410 struct mutex fl_map_mutex;
Tharun Kumar Merugu35173342018-02-08 16:13:17 +0530411 int refcount;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +0530412 /* Identifies the device (MINOR_NUM_DEV / MINOR_NUM_SECURE_DEV) */
413 int dev_minor;
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +0530414 char *debug_buf;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700415};
416
417static struct fastrpc_apps gfa;
418
419static struct fastrpc_channel_ctx gcinfo[NUM_CHANNELS] = {
420 {
421 .name = "adsprpc-smd",
422 .subsys = "adsp",
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +0530423 .channel = SMD_APPS_QDSP,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700424 .link.link_info.edge = "lpass",
425 .link.link_info.transport = "smem",
Tharun Kumar Merugudf860662018-01-17 19:59:50 +0530426 .spd = {
427 {
428 .spdname =
429 AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME,
430 .pdrnb.notifier_call =
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +0530431 fastrpc_pdr_notifier_cb,
432 },
433 {
434 .spdname =
435 SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME,
436 .pdrnb.notifier_call =
437 fastrpc_pdr_notifier_cb,
Tharun Kumar Merugudf860662018-01-17 19:59:50 +0530438 }
439 },
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700440 },
441 {
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700442 .name = "mdsprpc-smd",
443 .subsys = "modem",
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +0530444 .channel = SMD_APPS_MODEM,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700445 .link.link_info.edge = "mpss",
446 .link.link_info.transport = "smem",
447 },
448 {
Sathish Ambley36849af2017-02-02 09:35:55 -0800449 .name = "sdsprpc-smd",
450 .subsys = "slpi",
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +0530451 .channel = SMD_APPS_DSPS,
Sathish Ambley36849af2017-02-02 09:35:55 -0800452 .link.link_info.edge = "dsps",
453 .link.link_info.transport = "smem",
Sathish Ambley36849af2017-02-02 09:35:55 -0800454 },
455 {
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700456 .name = "cdsprpc-smd",
457 .subsys = "cdsp",
458 .link.link_info.edge = "cdsp",
459 .link.link_info.transport = "smem",
460 },
461};
462
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +0530463static int hlosvm[1] = {VMID_HLOS};
464static int hlosvmperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
465
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800466static inline int64_t getnstimediff(struct timespec *start)
467{
468 int64_t ns;
469 struct timespec ts, b;
470
471 getnstimeofday(&ts);
472 b = timespec_sub(ts, *start);
473 ns = timespec_to_ns(&b);
474 return ns;
475}
476
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530477static inline int64_t *getperfcounter(struct fastrpc_file *fl, int key)
478{
479 int err = 0;
480 int64_t *val = NULL;
481 struct fastrpc_perf *perf = NULL, *fperf = NULL;
482 struct hlist_node *n = NULL;
483
484 VERIFY(err, !IS_ERR_OR_NULL(fl));
485 if (err)
486 goto bail;
487
488 mutex_lock(&fl->perf_mutex);
489 hlist_for_each_entry_safe(perf, n, &fl->perf, hn) {
490 if (perf->tid == current->pid) {
491 fperf = perf;
492 break;
493 }
494 }
495
496 if (IS_ERR_OR_NULL(fperf)) {
497 fperf = kzalloc(sizeof(*fperf), GFP_KERNEL);
498
499 VERIFY(err, !IS_ERR_OR_NULL(fperf));
500 if (err) {
501 mutex_unlock(&fl->perf_mutex);
502 kfree(fperf);
503 goto bail;
504 }
505
506 fperf->tid = current->pid;
507 hlist_add_head(&fperf->hn, &fl->perf);
508 }
509
510 val = ((int64_t *)fperf) + key;
511 mutex_unlock(&fl->perf_mutex);
512bail:
513 return val;
514}
515
516
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700517static void fastrpc_buf_free(struct fastrpc_buf *buf, int cache)
518{
c_mtharue1a5ce12017-10-13 20:47:09 +0530519 struct fastrpc_file *fl = buf == NULL ? NULL : buf->fl;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700520 int vmid;
521
522 if (!fl)
523 return;
524 if (cache) {
525 spin_lock(&fl->hlock);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +0530526 hlist_add_head(&buf->hn, &fl->cached_bufs);
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700527 spin_unlock(&fl->hlock);
528 return;
529 }
Tharun Kumar Merugue073de72018-07-30 23:57:47 +0530530 if (buf->remote) {
531 spin_lock(&fl->hlock);
532 hlist_del_init(&buf->hn_rem);
533 spin_unlock(&fl->hlock);
534 buf->remote = 0;
535 buf->raddr = 0;
536 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700537 if (!IS_ERR_OR_NULL(buf->virt)) {
538 int destVM[1] = {VMID_HLOS};
539 int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
540
541 if (fl->sctx->smmu.cb)
542 buf->phys &= ~((uint64_t)fl->sctx->smmu.cb << 32);
543 vmid = fl->apps->channel[fl->cid].vmid;
544 if (vmid) {
545 int srcVM[2] = {VMID_HLOS, vmid};
546
547 hyp_assign_phys(buf->phys, buf_page_size(buf->size),
548 srcVM, 2, destVM, destVMperm, 1);
549 }
Tharun Kumar Merugue073de72018-07-30 23:57:47 +0530550 dma_free_attrs(fl->sctx->smmu.dev, buf->size, buf->virt,
551 buf->phys, buf->dma_attr);
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700552 }
553 kfree(buf);
554}
555
Tharun Kumar Merugue073de72018-07-30 23:57:47 +0530556static void fastrpc_cached_buf_list_free(struct fastrpc_file *fl)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700557{
558 struct fastrpc_buf *buf, *free;
559
560 do {
561 struct hlist_node *n;
562
c_mtharue1a5ce12017-10-13 20:47:09 +0530563 free = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700564 spin_lock(&fl->hlock);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +0530565 hlist_for_each_entry_safe(buf, n, &fl->cached_bufs, hn) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700566 hlist_del_init(&buf->hn);
567 free = buf;
568 break;
569 }
570 spin_unlock(&fl->hlock);
571 if (free)
572 fastrpc_buf_free(free, 0);
573 } while (free);
574}
575
Tharun Kumar Merugue073de72018-07-30 23:57:47 +0530576static void fastrpc_remote_buf_list_free(struct fastrpc_file *fl)
577{
578 struct fastrpc_buf *buf, *free;
579
580 do {
581 struct hlist_node *n;
582
583 free = NULL;
584 spin_lock(&fl->hlock);
585 hlist_for_each_entry_safe(buf, n, &fl->remote_bufs, hn_rem) {
586 free = buf;
587 break;
588 }
589 spin_unlock(&fl->hlock);
590 if (free)
591 fastrpc_buf_free(free, 0);
592 } while (free);
593}
594
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700595static void fastrpc_mmap_add(struct fastrpc_mmap *map)
596{
c_mtharue1a5ce12017-10-13 20:47:09 +0530597 if (map->flags == ADSP_MMAP_HEAP_ADDR ||
598 map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
599 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700600
c_mtharue1a5ce12017-10-13 20:47:09 +0530601 spin_lock(&me->hlock);
602 hlist_add_head(&map->hn, &me->maps);
603 spin_unlock(&me->hlock);
604 } else {
605 struct fastrpc_file *fl = map->fl;
606
c_mtharue1a5ce12017-10-13 20:47:09 +0530607 hlist_add_head(&map->hn, &fl->maps);
c_mtharue1a5ce12017-10-13 20:47:09 +0530608 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700609}
610
c_mtharue1a5ce12017-10-13 20:47:09 +0530611static int fastrpc_mmap_find(struct fastrpc_file *fl, int fd,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530612 uintptr_t va, size_t len, int mflags, int refs,
c_mtharue1a5ce12017-10-13 20:47:09 +0530613 struct fastrpc_mmap **ppmap)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700614{
c_mtharue1a5ce12017-10-13 20:47:09 +0530615 struct fastrpc_apps *me = &gfa;
616 struct fastrpc_mmap *match = NULL, *map = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700617 struct hlist_node *n;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530618
619 if ((va + len) < va)
620 return -EOVERFLOW;
c_mtharue1a5ce12017-10-13 20:47:09 +0530621 if (mflags == ADSP_MMAP_HEAP_ADDR ||
622 mflags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
623 spin_lock(&me->hlock);
624 hlist_for_each_entry_safe(map, n, &me->maps, hn) {
625 if (va >= map->va &&
626 va + len <= map->va + map->len &&
627 map->fd == fd) {
Tharun Kumar Merugu496ad342019-06-06 15:01:42 +0530628 if (refs) {
629 if (map->refs + 1 == INT_MAX) {
630 spin_unlock(&me->hlock);
631 return -ETOOMANYREFS;
632 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530633 map->refs++;
Tharun Kumar Merugu496ad342019-06-06 15:01:42 +0530634 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530635 match = map;
636 break;
637 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700638 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530639 spin_unlock(&me->hlock);
640 } else {
c_mtharue1a5ce12017-10-13 20:47:09 +0530641 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
642 if (va >= map->va &&
643 va + len <= map->va + map->len &&
644 map->fd == fd) {
Tharun Kumar Merugu496ad342019-06-06 15:01:42 +0530645 if (refs) {
646 if (map->refs + 1 == INT_MAX)
647 return -ETOOMANYREFS;
c_mtharue1a5ce12017-10-13 20:47:09 +0530648 map->refs++;
Tharun Kumar Merugu496ad342019-06-06 15:01:42 +0530649 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530650 match = map;
651 break;
652 }
653 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700654 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700655 if (match) {
656 *ppmap = match;
657 return 0;
658 }
659 return -ENOTTY;
660}
661
Tharun Kumar Merugu0d0b69e2018-09-14 22:30:58 +0530662static int dma_alloc_memory(dma_addr_t *region_phys, void **vaddr, size_t size,
Tharun Kumar Merugue073de72018-07-30 23:57:47 +0530663 unsigned long dma_attrs)
c_mtharue1a5ce12017-10-13 20:47:09 +0530664{
665 struct fastrpc_apps *me = &gfa;
c_mtharue1a5ce12017-10-13 20:47:09 +0530666
667 if (me->dev == NULL) {
668 pr_err("device adsprpc-mem is not initialized\n");
669 return -ENODEV;
670 }
Tharun Kumar Merugu0d0b69e2018-09-14 22:30:58 +0530671 *vaddr = dma_alloc_attrs(me->dev, size, region_phys, GFP_KERNEL,
Tharun Kumar Merugu48d5ff32018-04-16 19:24:16 +0530672 dma_attrs);
Tharun Kumar Merugu0d0b69e2018-09-14 22:30:58 +0530673 if (IS_ERR_OR_NULL(*vaddr)) {
674 pr_err("adsprpc: %s: %s: dma_alloc_attrs failed for size 0x%zx, returned %pK\n",
675 current->comm, __func__, size, (*vaddr));
c_mtharue1a5ce12017-10-13 20:47:09 +0530676 return -ENOMEM;
677 }
678 return 0;
679}
680
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700681static int fastrpc_mmap_remove(struct fastrpc_file *fl, uintptr_t va,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530682 size_t len, struct fastrpc_mmap **ppmap)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700683{
c_mtharue1a5ce12017-10-13 20:47:09 +0530684 struct fastrpc_mmap *match = NULL, *map;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700685 struct hlist_node *n;
686 struct fastrpc_apps *me = &gfa;
687
688 spin_lock(&me->hlock);
689 hlist_for_each_entry_safe(map, n, &me->maps, hn) {
690 if (map->raddr == va &&
691 map->raddr + map->len == va + len &&
692 map->refs == 1) {
693 match = map;
694 hlist_del_init(&map->hn);
695 break;
696 }
697 }
698 spin_unlock(&me->hlock);
699 if (match) {
700 *ppmap = match;
701 return 0;
702 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700703 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
704 if (map->raddr == va &&
705 map->raddr + map->len == va + len &&
706 map->refs == 1) {
707 match = map;
708 hlist_del_init(&map->hn);
709 break;
710 }
711 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700712 if (match) {
713 *ppmap = match;
714 return 0;
715 }
716 return -ENOTTY;
717}
718
c_mtharu7bd6a422017-10-17 18:15:37 +0530719static void fastrpc_mmap_free(struct fastrpc_mmap *map, uint32_t flags)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700720{
c_mtharue1a5ce12017-10-13 20:47:09 +0530721 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700722 struct fastrpc_file *fl;
723 int vmid;
724 struct fastrpc_session_ctx *sess;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700725
726 if (!map)
727 return;
728 fl = map->fl;
c_mtharue1a5ce12017-10-13 20:47:09 +0530729 if (map->flags == ADSP_MMAP_HEAP_ADDR ||
730 map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
731 spin_lock(&me->hlock);
732 map->refs--;
733 if (!map->refs)
734 hlist_del_init(&map->hn);
735 spin_unlock(&me->hlock);
c_mtharu7bd6a422017-10-17 18:15:37 +0530736 if (map->refs > 0)
737 return;
c_mtharue1a5ce12017-10-13 20:47:09 +0530738 } else {
c_mtharue1a5ce12017-10-13 20:47:09 +0530739 map->refs--;
740 if (!map->refs)
741 hlist_del_init(&map->hn);
c_mtharu7bd6a422017-10-17 18:15:37 +0530742 if (map->refs > 0 && !flags)
743 return;
c_mtharue1a5ce12017-10-13 20:47:09 +0530744 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530745 if (map->flags == ADSP_MMAP_HEAP_ADDR ||
746 map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700747
c_mtharue1a5ce12017-10-13 20:47:09 +0530748 if (me->dev == NULL) {
749 pr_err("failed to free remote heap allocation\n");
750 return;
751 }
752 if (map->phys) {
Tharun Kumar Merugu72c90252019-08-29 18:36:08 +0530753 unsigned long dma_attrs = DMA_ATTR_SKIP_ZEROING |
754 DMA_ATTR_NO_KERNEL_MAPPING;
Tharun Kumar Merugu48d5ff32018-04-16 19:24:16 +0530755 dma_free_attrs(me->dev, map->size, (void *)map->va,
756 (dma_addr_t)map->phys, dma_attrs);
c_mtharue1a5ce12017-10-13 20:47:09 +0530757 }
Tharun Kumar Merugu35a94a52018-02-01 21:09:04 +0530758 } else if (map->flags == FASTRPC_DMAHANDLE_NOMAP) {
759 if (!IS_ERR_OR_NULL(map->handle))
760 ion_free(fl->apps->client, map->handle);
c_mtharue1a5ce12017-10-13 20:47:09 +0530761 } else {
762 int destVM[1] = {VMID_HLOS};
763 int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
764
765 if (map->secure)
766 sess = fl->secsctx;
767 else
768 sess = fl->sctx;
769
770 if (!IS_ERR_OR_NULL(map->handle))
771 ion_free(fl->apps->client, map->handle);
772 if (sess && sess->smmu.enabled) {
773 if (map->size || map->phys)
774 msm_dma_unmap_sg(sess->smmu.dev,
775 map->table->sgl,
776 map->table->nents, DMA_BIDIRECTIONAL,
777 map->buf);
778 }
779 vmid = fl->apps->channel[fl->cid].vmid;
780 if (vmid && map->phys) {
781 int srcVM[2] = {VMID_HLOS, vmid};
782
783 hyp_assign_phys(map->phys, buf_page_size(map->size),
784 srcVM, 2, destVM, destVMperm, 1);
785 }
786
787 if (!IS_ERR_OR_NULL(map->table))
788 dma_buf_unmap_attachment(map->attach, map->table,
789 DMA_BIDIRECTIONAL);
790 if (!IS_ERR_OR_NULL(map->attach))
791 dma_buf_detach(map->buf, map->attach);
792 if (!IS_ERR_OR_NULL(map->buf))
793 dma_buf_put(map->buf);
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700794 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700795 kfree(map);
796}
797
798static int fastrpc_session_alloc(struct fastrpc_channel_ctx *chan, int secure,
799 struct fastrpc_session_ctx **session);
800
801static int fastrpc_mmap_create(struct fastrpc_file *fl, int fd,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530802 unsigned int attr, uintptr_t va, size_t len, int mflags,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700803 struct fastrpc_mmap **ppmap)
804{
c_mtharue1a5ce12017-10-13 20:47:09 +0530805 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700806 struct fastrpc_session_ctx *sess;
807 struct fastrpc_apps *apps = fl->apps;
808 int cid = fl->cid;
809 struct fastrpc_channel_ctx *chan = &apps->channel[cid];
c_mtharue1a5ce12017-10-13 20:47:09 +0530810 struct fastrpc_mmap *map = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700811 unsigned long attrs;
c_mtharuf931ff92017-11-30 19:35:30 +0530812 dma_addr_t region_phys = 0;
Tharun Kumar Merugu0d0b69e2018-09-14 22:30:58 +0530813 void *region_vaddr = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700814 unsigned long flags;
815 int err = 0, vmid;
816
Sathish Ambleyae5ee542017-01-16 22:24:23 -0800817 if (!fastrpc_mmap_find(fl, fd, va, len, mflags, 1, ppmap))
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700818 return 0;
819 map = kzalloc(sizeof(*map), GFP_KERNEL);
820 VERIFY(err, !IS_ERR_OR_NULL(map));
821 if (err)
822 goto bail;
823 INIT_HLIST_NODE(&map->hn);
824 map->flags = mflags;
825 map->refs = 1;
826 map->fl = fl;
827 map->fd = fd;
828 map->attr = attr;
c_mtharue1a5ce12017-10-13 20:47:09 +0530829 if (mflags == ADSP_MMAP_HEAP_ADDR ||
830 mflags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
Tharun Kumar Merugue073de72018-07-30 23:57:47 +0530831 unsigned long dma_attrs = DMA_ATTR_SKIP_ZEROING |
832 DMA_ATTR_NO_KERNEL_MAPPING;
833
c_mtharue1a5ce12017-10-13 20:47:09 +0530834 map->apps = me;
835 map->fl = NULL;
Tharun Kumar Merugu0d0b69e2018-09-14 22:30:58 +0530836 VERIFY(err, !dma_alloc_memory(&region_phys, &region_vaddr,
837 len, dma_attrs));
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700838 if (err)
839 goto bail;
c_mtharuf931ff92017-11-30 19:35:30 +0530840 map->phys = (uintptr_t)region_phys;
c_mtharue1a5ce12017-10-13 20:47:09 +0530841 map->size = len;
Tharun Kumar Merugu0d0b69e2018-09-14 22:30:58 +0530842 map->va = (uintptr_t)region_vaddr;
Tharun Kumar Merugu35a94a52018-02-01 21:09:04 +0530843 } else if (mflags == FASTRPC_DMAHANDLE_NOMAP) {
844 ion_phys_addr_t iphys;
845
846 VERIFY(err, !IS_ERR_OR_NULL(map->handle =
847 ion_import_dma_buf_fd(fl->apps->client, fd)));
848 if (err)
849 goto bail;
850
851 map->uncached = 1;
852 map->buf = NULL;
853 map->attach = NULL;
854 map->table = NULL;
855 map->va = 0;
856 map->phys = 0;
857
858 err = ion_phys(fl->apps->client, map->handle,
859 &iphys, &map->size);
860 if (err)
861 goto bail;
862 map->phys = (uint64_t)iphys;
c_mtharue1a5ce12017-10-13 20:47:09 +0530863 } else {
c_mtharu7bd6a422017-10-17 18:15:37 +0530864 if (map->attr && (map->attr & FASTRPC_ATTR_KEEP_MAP)) {
865 pr_info("adsprpc: buffer mapped with persist attr %x\n",
866 (unsigned int)map->attr);
867 map->refs = 2;
868 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530869 VERIFY(err, !IS_ERR_OR_NULL(map->handle =
870 ion_import_dma_buf_fd(fl->apps->client, fd)));
871 if (err)
872 goto bail;
873 VERIFY(err, !ion_handle_get_flags(fl->apps->client, map->handle,
874 &flags));
875 if (err)
876 goto bail;
877
c_mtharue1a5ce12017-10-13 20:47:09 +0530878 map->secure = flags & ION_FLAG_SECURE;
879 if (map->secure) {
880 if (!fl->secsctx)
881 err = fastrpc_session_alloc(chan, 1,
882 &fl->secsctx);
883 if (err)
884 goto bail;
885 }
886 if (map->secure)
887 sess = fl->secsctx;
888 else
889 sess = fl->sctx;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +0530890
c_mtharue1a5ce12017-10-13 20:47:09 +0530891 VERIFY(err, !IS_ERR_OR_NULL(sess));
892 if (err)
893 goto bail;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +0530894
895 map->uncached = !ION_IS_CACHED(flags);
896 if (map->attr & FASTRPC_ATTR_NOVA && !sess->smmu.coherent)
897 map->uncached = 1;
898
c_mtharue1a5ce12017-10-13 20:47:09 +0530899 VERIFY(err, !IS_ERR_OR_NULL(map->buf = dma_buf_get(fd)));
900 if (err)
901 goto bail;
902 VERIFY(err, !IS_ERR_OR_NULL(map->attach =
903 dma_buf_attach(map->buf, sess->smmu.dev)));
904 if (err)
905 goto bail;
906 VERIFY(err, !IS_ERR_OR_NULL(map->table =
907 dma_buf_map_attachment(map->attach,
908 DMA_BIDIRECTIONAL)));
909 if (err)
910 goto bail;
911 if (sess->smmu.enabled) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700912 attrs = DMA_ATTR_EXEC_MAPPING;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +0530913
914 if (map->attr & FASTRPC_ATTR_NON_COHERENT ||
915 (sess->smmu.coherent && map->uncached))
916 attrs |= DMA_ATTR_FORCE_NON_COHERENT;
917 else if (map->attr & FASTRPC_ATTR_COHERENT)
918 attrs |= DMA_ATTR_FORCE_COHERENT;
919
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700920 VERIFY(err, map->table->nents ==
c_mtharue1a5ce12017-10-13 20:47:09 +0530921 msm_dma_map_sg_attrs(sess->smmu.dev,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700922 map->table->sgl, map->table->nents,
923 DMA_BIDIRECTIONAL, map->buf, attrs));
c_mtharue1a5ce12017-10-13 20:47:09 +0530924 if (err)
925 goto bail;
926 } else {
927 VERIFY(err, map->table->nents == 1);
928 if (err)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700929 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +0530930 }
931 map->phys = sg_dma_address(map->table->sgl);
Tharun Kumar Merugu93f319a2018-02-01 17:35:42 +0530932
c_mtharue1a5ce12017-10-13 20:47:09 +0530933 if (sess->smmu.cb) {
934 map->phys += ((uint64_t)sess->smmu.cb << 32);
935 map->size = sg_dma_len(map->table->sgl);
936 } else {
937 map->size = buf_page_size(len);
938 }
Tharun Kumar Merugu93f319a2018-02-01 17:35:42 +0530939
c_mtharue1a5ce12017-10-13 20:47:09 +0530940 vmid = fl->apps->channel[fl->cid].vmid;
Tharun Kumar Merugu93f319a2018-02-01 17:35:42 +0530941 if (!sess->smmu.enabled && !vmid) {
942 VERIFY(err, map->phys >= me->range.addr &&
943 map->phys + map->size <=
944 me->range.addr + me->range.size);
945 if (err) {
946 pr_err("adsprpc: mmap fail out of range\n");
947 goto bail;
948 }
949 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530950 if (vmid) {
951 int srcVM[1] = {VMID_HLOS};
952 int destVM[2] = {VMID_HLOS, vmid};
953 int destVMperm[2] = {PERM_READ | PERM_WRITE,
954 PERM_READ | PERM_WRITE | PERM_EXEC};
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700955
c_mtharue1a5ce12017-10-13 20:47:09 +0530956 VERIFY(err, !hyp_assign_phys(map->phys,
957 buf_page_size(map->size),
958 srcVM, 1, destVM, destVMperm, 2));
959 if (err)
960 goto bail;
961 }
962 map->va = va;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700963 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700964 map->len = len;
965
966 fastrpc_mmap_add(map);
967 *ppmap = map;
968
969bail:
970 if (err && map)
c_mtharu7bd6a422017-10-17 18:15:37 +0530971 fastrpc_mmap_free(map, 0);
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700972 return err;
973}
974
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530975static int fastrpc_buf_alloc(struct fastrpc_file *fl, size_t size,
Tharun Kumar Merugue073de72018-07-30 23:57:47 +0530976 unsigned long dma_attr, uint32_t rflags,
977 int remote, struct fastrpc_buf **obuf)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700978{
979 int err = 0, vmid;
c_mtharue1a5ce12017-10-13 20:47:09 +0530980 struct fastrpc_buf *buf = NULL, *fr = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700981 struct hlist_node *n;
982
983 VERIFY(err, size > 0);
984 if (err)
985 goto bail;
986
Tharun Kumar Merugue073de72018-07-30 23:57:47 +0530987 if (!remote) {
988 /* find the smallest buffer that fits in the cache */
989 spin_lock(&fl->hlock);
990 hlist_for_each_entry_safe(buf, n, &fl->cached_bufs, hn) {
991 if (buf->size >= size && (!fr || fr->size > buf->size))
992 fr = buf;
993 }
994 if (fr)
995 hlist_del_init(&fr->hn);
996 spin_unlock(&fl->hlock);
997 if (fr) {
998 *obuf = fr;
999 return 0;
1000 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001001 }
c_mtharue1a5ce12017-10-13 20:47:09 +05301002 buf = NULL;
1003 VERIFY(err, NULL != (buf = kzalloc(sizeof(*buf), GFP_KERNEL)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001004 if (err)
1005 goto bail;
1006 INIT_HLIST_NODE(&buf->hn);
1007 buf->fl = fl;
c_mtharue1a5ce12017-10-13 20:47:09 +05301008 buf->virt = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001009 buf->phys = 0;
1010 buf->size = size;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05301011 buf->dma_attr = dma_attr;
1012 buf->flags = rflags;
1013 buf->raddr = 0;
1014 buf->remote = 0;
1015 buf->virt = dma_alloc_attrs(fl->sctx->smmu.dev, buf->size,
1016 (dma_addr_t *)&buf->phys,
1017 GFP_KERNEL, buf->dma_attr);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001018 if (IS_ERR_OR_NULL(buf->virt)) {
1019 /* free cache and retry */
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05301020 fastrpc_cached_buf_list_free(fl);
1021 buf->virt = dma_alloc_attrs(fl->sctx->smmu.dev, buf->size,
1022 (dma_addr_t *)&buf->phys,
1023 GFP_KERNEL, buf->dma_attr);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001024 VERIFY(err, !IS_ERR_OR_NULL(buf->virt));
1025 }
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05301026 if (err) {
1027 err = -ENOMEM;
1028 pr_err("adsprpc: %s: %s: dma_alloc_attrs failed for size 0x%zx\n",
1029 current->comm, __func__, size);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001030 goto bail;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05301031 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001032 if (fl->sctx->smmu.cb)
1033 buf->phys += ((uint64_t)fl->sctx->smmu.cb << 32);
1034 vmid = fl->apps->channel[fl->cid].vmid;
1035 if (vmid) {
1036 int srcVM[1] = {VMID_HLOS};
1037 int destVM[2] = {VMID_HLOS, vmid};
1038 int destVMperm[2] = {PERM_READ | PERM_WRITE,
1039 PERM_READ | PERM_WRITE | PERM_EXEC};
1040
1041 VERIFY(err, !hyp_assign_phys(buf->phys, buf_page_size(size),
1042 srcVM, 1, destVM, destVMperm, 2));
1043 if (err)
1044 goto bail;
1045 }
1046
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05301047 if (remote) {
1048 INIT_HLIST_NODE(&buf->hn_rem);
1049 spin_lock(&fl->hlock);
1050 hlist_add_head(&buf->hn_rem, &fl->remote_bufs);
1051 spin_unlock(&fl->hlock);
1052 buf->remote = remote;
1053 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001054 *obuf = buf;
1055 bail:
1056 if (err && buf)
1057 fastrpc_buf_free(buf, 0);
1058 return err;
1059}
1060
1061
1062static int context_restore_interrupted(struct fastrpc_file *fl,
Sathish Ambleybae51902017-07-03 15:00:49 -07001063 struct fastrpc_ioctl_invoke_crc *inv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001064 struct smq_invoke_ctx **po)
1065{
1066 int err = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05301067 struct smq_invoke_ctx *ctx = NULL, *ictx = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001068 struct hlist_node *n;
1069 struct fastrpc_ioctl_invoke *invoke = &inv->inv;
1070
1071 spin_lock(&fl->hlock);
1072 hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) {
1073 if (ictx->pid == current->pid) {
1074 if (invoke->sc != ictx->sc || ictx->fl != fl)
1075 err = -1;
1076 else {
1077 ctx = ictx;
1078 hlist_del_init(&ctx->hn);
1079 hlist_add_head(&ctx->hn, &fl->clst.pending);
1080 }
1081 break;
1082 }
1083 }
1084 spin_unlock(&fl->hlock);
1085 if (ctx)
1086 *po = ctx;
1087 return err;
1088}
1089
1090#define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)
1091static int overlap_ptr_cmp(const void *a, const void *b)
1092{
1093 struct overlap *pa = *((struct overlap **)a);
1094 struct overlap *pb = *((struct overlap **)b);
1095 /* sort with lowest starting buffer first */
1096 int st = CMP(pa->start, pb->start);
1097 /* sort with highest ending buffer first */
1098 int ed = CMP(pb->end, pa->end);
1099 return st == 0 ? ed : st;
1100}
1101
Sathish Ambley9466d672017-01-25 10:51:55 -08001102static int context_build_overlap(struct smq_invoke_ctx *ctx)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001103{
Sathish Ambley9466d672017-01-25 10:51:55 -08001104 int i, err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001105 remote_arg_t *lpra = ctx->lpra;
1106 int inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
1107 int outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc);
1108 int nbufs = inbufs + outbufs;
1109 struct overlap max;
1110
1111 for (i = 0; i < nbufs; ++i) {
1112 ctx->overs[i].start = (uintptr_t)lpra[i].buf.pv;
1113 ctx->overs[i].end = ctx->overs[i].start + lpra[i].buf.len;
Sathish Ambley9466d672017-01-25 10:51:55 -08001114 if (lpra[i].buf.len) {
1115 VERIFY(err, ctx->overs[i].end > ctx->overs[i].start);
1116 if (err)
1117 goto bail;
1118 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001119 ctx->overs[i].raix = i;
1120 ctx->overps[i] = &ctx->overs[i];
1121 }
c_mtharue1a5ce12017-10-13 20:47:09 +05301122 sort(ctx->overps, nbufs, sizeof(*ctx->overps), overlap_ptr_cmp, NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001123 max.start = 0;
1124 max.end = 0;
1125 for (i = 0; i < nbufs; ++i) {
1126 if (ctx->overps[i]->start < max.end) {
1127 ctx->overps[i]->mstart = max.end;
1128 ctx->overps[i]->mend = ctx->overps[i]->end;
1129 ctx->overps[i]->offset = max.end -
1130 ctx->overps[i]->start;
1131 if (ctx->overps[i]->end > max.end) {
1132 max.end = ctx->overps[i]->end;
1133 } else {
1134 ctx->overps[i]->mend = 0;
1135 ctx->overps[i]->mstart = 0;
1136 }
1137 } else {
1138 ctx->overps[i]->mend = ctx->overps[i]->end;
1139 ctx->overps[i]->mstart = ctx->overps[i]->start;
1140 ctx->overps[i]->offset = 0;
1141 max = *ctx->overps[i];
1142 }
1143 }
Sathish Ambley9466d672017-01-25 10:51:55 -08001144bail:
1145 return err;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001146}
1147
1148#define K_COPY_FROM_USER(err, kernel, dst, src, size) \
1149 do {\
1150 if (!(kernel))\
c_mtharue1a5ce12017-10-13 20:47:09 +05301151 VERIFY(err, 0 == copy_from_user((dst),\
1152 (void const __user *)(src),\
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001153 (size)));\
1154 else\
1155 memmove((dst), (src), (size));\
1156 } while (0)
1157
1158#define K_COPY_TO_USER(err, kernel, dst, src, size) \
1159 do {\
1160 if (!(kernel))\
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301161 VERIFY(err, 0 == copy_to_user((void __user *)(dst),\
c_mtharue1a5ce12017-10-13 20:47:09 +05301162 (src), (size)));\
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001163 else\
1164 memmove((dst), (src), (size));\
1165 } while (0)
1166
1167
1168static void context_free(struct smq_invoke_ctx *ctx);
1169
1170static int context_alloc(struct fastrpc_file *fl, uint32_t kernel,
Sathish Ambleybae51902017-07-03 15:00:49 -07001171 struct fastrpc_ioctl_invoke_crc *invokefd,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001172 struct smq_invoke_ctx **po)
1173{
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301174 struct fastrpc_apps *me = &gfa;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301175 int err = 0, bufs, ii, size = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05301176 struct smq_invoke_ctx *ctx = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001177 struct fastrpc_ctx_lst *clst = &fl->clst;
1178 struct fastrpc_ioctl_invoke *invoke = &invokefd->inv;
1179
1180 bufs = REMOTE_SCALARS_LENGTH(invoke->sc);
1181 size = bufs * sizeof(*ctx->lpra) + bufs * sizeof(*ctx->maps) +
1182 sizeof(*ctx->fds) * (bufs) +
1183 sizeof(*ctx->attrs) * (bufs) +
1184 sizeof(*ctx->overs) * (bufs) +
1185 sizeof(*ctx->overps) * (bufs);
1186
c_mtharue1a5ce12017-10-13 20:47:09 +05301187 VERIFY(err, NULL != (ctx = kzalloc(sizeof(*ctx) + size, GFP_KERNEL)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001188 if (err)
1189 goto bail;
1190
1191 INIT_HLIST_NODE(&ctx->hn);
1192 hlist_add_fake(&ctx->hn);
1193 ctx->fl = fl;
1194 ctx->maps = (struct fastrpc_mmap **)(&ctx[1]);
1195 ctx->lpra = (remote_arg_t *)(&ctx->maps[bufs]);
1196 ctx->fds = (int *)(&ctx->lpra[bufs]);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301197 if (me->legacy) {
1198 ctx->overs = (struct overlap *)(&ctx->fds[bufs]);
1199 ctx->overps = (struct overlap **)(&ctx->overs[bufs]);
1200 } else {
1201 ctx->attrs = (unsigned int *)(&ctx->fds[bufs]);
1202 ctx->overs = (struct overlap *)(&ctx->attrs[bufs]);
1203 ctx->overps = (struct overlap **)(&ctx->overs[bufs]);
1204 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001205
c_mtharue1a5ce12017-10-13 20:47:09 +05301206 K_COPY_FROM_USER(err, kernel, (void *)ctx->lpra, invoke->pra,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001207 bufs * sizeof(*ctx->lpra));
1208 if (err)
1209 goto bail;
1210
1211 if (invokefd->fds) {
1212 K_COPY_FROM_USER(err, kernel, ctx->fds, invokefd->fds,
1213 bufs * sizeof(*ctx->fds));
1214 if (err)
1215 goto bail;
1216 }
1217 if (invokefd->attrs) {
1218 K_COPY_FROM_USER(err, kernel, ctx->attrs, invokefd->attrs,
1219 bufs * sizeof(*ctx->attrs));
1220 if (err)
1221 goto bail;
1222 }
Sathish Ambleybae51902017-07-03 15:00:49 -07001223 ctx->crc = (uint32_t *)invokefd->crc;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001224 ctx->sc = invoke->sc;
Sathish Ambley9466d672017-01-25 10:51:55 -08001225 if (bufs) {
1226 VERIFY(err, 0 == context_build_overlap(ctx));
1227 if (err)
1228 goto bail;
1229 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001230 ctx->retval = -1;
1231 ctx->pid = current->pid;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301232 ctx->tgid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001233 init_completion(&ctx->work);
c_mtharufdac6892017-10-12 13:09:01 +05301234 ctx->magic = FASTRPC_CTX_MAGIC;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001235
1236 spin_lock(&fl->hlock);
1237 hlist_add_head(&ctx->hn, &clst->pending);
1238 spin_unlock(&fl->hlock);
1239
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301240 spin_lock(&me->ctxlock);
1241 for (ii = 0; ii < FASTRPC_CTX_MAX; ii++) {
1242 if (!me->ctxtable[ii]) {
1243 me->ctxtable[ii] = ctx;
1244 ctx->ctxid = (ptr_to_uint64(ctx) & ~0xFFF)|(ii << 4);
1245 break;
1246 }
1247 }
1248 spin_unlock(&me->ctxlock);
1249 VERIFY(err, ii < FASTRPC_CTX_MAX);
1250 if (err) {
1251 pr_err("adsprpc: out of context memory\n");
1252 goto bail;
1253 }
1254
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001255 *po = ctx;
1256bail:
1257 if (ctx && err)
1258 context_free(ctx);
1259 return err;
1260}
1261
1262static void context_save_interrupted(struct smq_invoke_ctx *ctx)
1263{
1264 struct fastrpc_ctx_lst *clst = &ctx->fl->clst;
1265
1266 spin_lock(&ctx->fl->hlock);
1267 hlist_del_init(&ctx->hn);
1268 hlist_add_head(&ctx->hn, &clst->interrupted);
1269 spin_unlock(&ctx->fl->hlock);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001270}
1271
1272static void context_free(struct smq_invoke_ctx *ctx)
1273{
1274 int i;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301275 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001276 int nbufs = REMOTE_SCALARS_INBUFS(ctx->sc) +
1277 REMOTE_SCALARS_OUTBUFS(ctx->sc);
1278 spin_lock(&ctx->fl->hlock);
1279 hlist_del_init(&ctx->hn);
1280 spin_unlock(&ctx->fl->hlock);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301281 mutex_lock(&ctx->fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001282 for (i = 0; i < nbufs; ++i)
c_mtharu7bd6a422017-10-17 18:15:37 +05301283 fastrpc_mmap_free(ctx->maps[i], 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301284
1285 mutex_unlock(&ctx->fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001286 fastrpc_buf_free(ctx->buf, 1);
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301287 fastrpc_buf_free(ctx->lbuf, 1);
c_mtharufdac6892017-10-12 13:09:01 +05301288 ctx->magic = 0;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301289 ctx->ctxid = 0;
1290
1291 spin_lock(&me->ctxlock);
1292 for (i = 0; i < FASTRPC_CTX_MAX; i++) {
1293 if (me->ctxtable[i] == ctx) {
1294 me->ctxtable[i] = NULL;
1295 break;
1296 }
1297 }
1298 spin_unlock(&me->ctxlock);
1299
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001300 kfree(ctx);
1301}
1302
1303static void context_notify_user(struct smq_invoke_ctx *ctx, int retval)
1304{
1305 ctx->retval = retval;
1306 complete(&ctx->work);
1307}
1308
1309
1310static void fastrpc_notify_users(struct fastrpc_file *me)
1311{
1312 struct smq_invoke_ctx *ictx;
1313 struct hlist_node *n;
1314
1315 spin_lock(&me->hlock);
1316 hlist_for_each_entry_safe(ictx, n, &me->clst.pending, hn) {
1317 complete(&ictx->work);
1318 }
1319 hlist_for_each_entry_safe(ictx, n, &me->clst.interrupted, hn) {
1320 complete(&ictx->work);
1321 }
1322 spin_unlock(&me->hlock);
1323
1324}
1325
Tharun Kumar Merugu77dd5872018-04-02 12:48:17 +05301326
1327static void fastrpc_notify_users_staticpd_pdr(struct fastrpc_file *me)
1328{
1329 struct smq_invoke_ctx *ictx;
1330 struct hlist_node *n;
1331
1332 spin_lock(&me->hlock);
1333 hlist_for_each_entry_safe(ictx, n, &me->clst.pending, hn) {
1334 if (ictx->msg.pid)
1335 complete(&ictx->work);
1336 }
1337 hlist_for_each_entry_safe(ictx, n, &me->clst.interrupted, hn) {
1338 if (ictx->msg.pid)
1339 complete(&ictx->work);
1340 }
1341 spin_unlock(&me->hlock);
1342}
1343
1344
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001345static void fastrpc_notify_drivers(struct fastrpc_apps *me, int cid)
1346{
1347 struct fastrpc_file *fl;
1348 struct hlist_node *n;
1349
1350 spin_lock(&me->hlock);
1351 hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
1352 if (fl->cid == cid)
1353 fastrpc_notify_users(fl);
1354 }
1355 spin_unlock(&me->hlock);
1356
1357}
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05301358
1359static void fastrpc_notify_pdr_drivers(struct fastrpc_apps *me, char *spdname)
1360{
1361 struct fastrpc_file *fl;
1362 struct hlist_node *n;
1363
1364 spin_lock(&me->hlock);
1365 hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
1366 if (fl->spdname && !strcmp(spdname, fl->spdname))
Tharun Kumar Merugu77dd5872018-04-02 12:48:17 +05301367 fastrpc_notify_users_staticpd_pdr(fl);
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05301368 }
1369 spin_unlock(&me->hlock);
1370
1371}
1372
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001373static void context_list_ctor(struct fastrpc_ctx_lst *me)
1374{
1375 INIT_HLIST_HEAD(&me->interrupted);
1376 INIT_HLIST_HEAD(&me->pending);
1377}
1378
1379static void fastrpc_context_list_dtor(struct fastrpc_file *fl)
1380{
1381 struct fastrpc_ctx_lst *clst = &fl->clst;
c_mtharue1a5ce12017-10-13 20:47:09 +05301382 struct smq_invoke_ctx *ictx = NULL, *ctxfree;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001383 struct hlist_node *n;
1384
1385 do {
c_mtharue1a5ce12017-10-13 20:47:09 +05301386 ctxfree = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001387 spin_lock(&fl->hlock);
1388 hlist_for_each_entry_safe(ictx, n, &clst->interrupted, hn) {
1389 hlist_del_init(&ictx->hn);
1390 ctxfree = ictx;
1391 break;
1392 }
1393 spin_unlock(&fl->hlock);
1394 if (ctxfree)
1395 context_free(ctxfree);
1396 } while (ctxfree);
1397 do {
c_mtharue1a5ce12017-10-13 20:47:09 +05301398 ctxfree = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001399 spin_lock(&fl->hlock);
1400 hlist_for_each_entry_safe(ictx, n, &clst->pending, hn) {
1401 hlist_del_init(&ictx->hn);
1402 ctxfree = ictx;
1403 break;
1404 }
1405 spin_unlock(&fl->hlock);
1406 if (ctxfree)
1407 context_free(ctxfree);
1408 } while (ctxfree);
1409}
1410
1411static int fastrpc_file_free(struct fastrpc_file *fl);
1412static void fastrpc_file_list_dtor(struct fastrpc_apps *me)
1413{
1414 struct fastrpc_file *fl, *free;
1415 struct hlist_node *n;
1416
1417 do {
c_mtharue1a5ce12017-10-13 20:47:09 +05301418 free = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001419 spin_lock(&me->hlock);
1420 hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
1421 hlist_del_init(&fl->hn);
1422 free = fl;
1423 break;
1424 }
1425 spin_unlock(&me->hlock);
1426 if (free)
1427 fastrpc_file_free(free);
1428 } while (free);
1429}
1430
1431static int get_args(uint32_t kernel, struct smq_invoke_ctx *ctx)
1432{
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301433 struct fastrpc_apps *me = &gfa;
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301434 remote_arg64_t *rpra, *lrpra;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001435 remote_arg_t *lpra = ctx->lpra;
1436 struct smq_invoke_buf *list;
1437 struct smq_phy_page *pages, *ipage;
1438 uint32_t sc = ctx->sc;
1439 int inbufs = REMOTE_SCALARS_INBUFS(sc);
1440 int outbufs = REMOTE_SCALARS_OUTBUFS(sc);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001441 int handles, bufs = inbufs + outbufs;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001442 uintptr_t args;
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301443 size_t rlen = 0, copylen = 0, metalen = 0, lrpralen = 0;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001444 int i, oix;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001445 int err = 0;
1446 int mflags = 0;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001447 uint64_t *fdlist;
Sathish Ambleybae51902017-07-03 15:00:49 -07001448 uint32_t *crclist;
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301449 int64_t *perf_counter = getperfcounter(ctx->fl, PERF_COUNT);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001450
1451 /* calculate size of the metadata */
c_mtharue1a5ce12017-10-13 20:47:09 +05301452 rpra = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001453 list = smq_invoke_buf_start(rpra, sc);
1454 pages = smq_phy_page_start(sc, list);
1455 ipage = pages;
1456
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301457 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_MAP),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001458 for (i = 0; i < bufs; ++i) {
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301459 uintptr_t buf = (uintptr_t)lpra[i].buf.pv;
1460 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001461
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301462 mutex_lock(&ctx->fl->fl_map_mutex);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301463 if (ctx->fds[i] && (ctx->fds[i] != -1)) {
1464 unsigned int attrs = 0;
1465
1466 if (ctx->attrs)
1467 attrs = ctx->attrs[i];
1468
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001469 fastrpc_mmap_create(ctx->fl, ctx->fds[i],
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301470 attrs, buf, len,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001471 mflags, &ctx->maps[i]);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301472 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301473 mutex_unlock(&ctx->fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001474 ipage += 1;
1475 }
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301476 PERF_END);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001477 handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301478 mutex_lock(&ctx->fl->fl_map_mutex);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001479 for (i = bufs; i < bufs + handles; i++) {
Tharun Kumar Merugu35a94a52018-02-01 21:09:04 +05301480 int dmaflags = 0;
1481
1482 if (ctx->attrs && (ctx->attrs[i] & FASTRPC_ATTR_NOMAP))
1483 dmaflags = FASTRPC_DMAHANDLE_NOMAP;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001484 VERIFY(err, !fastrpc_mmap_create(ctx->fl, ctx->fds[i],
Tharun Kumar Merugu35a94a52018-02-01 21:09:04 +05301485 FASTRPC_ATTR_NOVA, 0, 0, dmaflags, &ctx->maps[i]));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301486 if (err) {
1487 mutex_unlock(&ctx->fl->fl_map_mutex);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001488 goto bail;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301489 }
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001490 ipage += 1;
1491 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301492 mutex_unlock(&ctx->fl->fl_map_mutex);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301493 if (!me->legacy) {
1494 metalen = copylen = (size_t)&ipage[0] +
1495 (sizeof(uint64_t) * M_FDLIST) +
1496 (sizeof(uint32_t) * M_CRCLIST);
1497 } else {
1498 metalen = copylen = (size_t)&ipage[0];
1499 }
Sathish Ambleybae51902017-07-03 15:00:49 -07001500
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301501 /* allocate new local rpra buffer */
1502 lrpralen = (size_t)&list[0];
1503 if (lrpralen) {
1504 err = fastrpc_buf_alloc(ctx->fl, lrpralen, 0, 0, 0, &ctx->lbuf);
1505 if (err)
1506 goto bail;
1507 }
1508 if (ctx->lbuf->virt)
1509 memset(ctx->lbuf->virt, 0, lrpralen);
1510
1511 lrpra = ctx->lbuf->virt;
1512 ctx->lrpra = lrpra;
1513
1514 /* calculate len required for copying */
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001515 for (oix = 0; oix < inbufs + outbufs; ++oix) {
1516 int i = ctx->overps[oix]->raix;
Sathish Ambleyd209c1e2016-12-13 15:27:30 -08001517 uintptr_t mstart, mend;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301518 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001519
1520 if (!len)
1521 continue;
1522 if (ctx->maps[i])
1523 continue;
1524 if (ctx->overps[oix]->offset == 0)
1525 copylen = ALIGN(copylen, BALIGN);
Sathish Ambleyd209c1e2016-12-13 15:27:30 -08001526 mstart = ctx->overps[oix]->mstart;
1527 mend = ctx->overps[oix]->mend;
1528 VERIFY(err, (mend - mstart) <= LONG_MAX);
1529 if (err)
1530 goto bail;
1531 copylen += mend - mstart;
1532 VERIFY(err, copylen >= 0);
1533 if (err)
1534 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001535 }
1536 ctx->used = copylen;
1537
1538 /* allocate new buffer */
1539 if (copylen) {
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05301540 err = fastrpc_buf_alloc(ctx->fl, copylen, 0, 0, 0, &ctx->buf);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001541 if (err)
1542 goto bail;
1543 }
Tharun Kumar Merugue3361f92017-06-22 10:45:43 +05301544 if (ctx->buf->virt && metalen <= copylen)
1545 memset(ctx->buf->virt, 0, metalen);
1546
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001547 /* copy metadata */
1548 rpra = ctx->buf->virt;
1549 ctx->rpra = rpra;
1550 list = smq_invoke_buf_start(rpra, sc);
1551 pages = smq_phy_page_start(sc, list);
1552 ipage = pages;
1553 args = (uintptr_t)ctx->buf->virt + metalen;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001554 for (i = 0; i < bufs + handles; ++i) {
1555 if (lpra[i].buf.len)
1556 list[i].num = 1;
1557 else
1558 list[i].num = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001559 list[i].pgidx = ipage - pages;
1560 ipage++;
1561 }
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05301562
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001563 /* map ion buffers */
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301564 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_MAP),
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301565 for (i = 0; rpra && lrpra && i < inbufs + outbufs; ++i) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001566 struct fastrpc_mmap *map = ctx->maps[i];
1567 uint64_t buf = ptr_to_uint64(lpra[i].buf.pv);
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301568 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001569
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301570 rpra[i].buf.pv = lrpra[i].buf.pv = 0;
1571 rpra[i].buf.len = lrpra[i].buf.len = len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001572 if (!len)
1573 continue;
1574 if (map) {
1575 struct vm_area_struct *vma;
1576 uintptr_t offset;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301577 uint64_t num = buf_num_pages(buf, len);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001578 int idx = list[i].pgidx;
1579
1580 if (map->attr & FASTRPC_ATTR_NOVA) {
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001581 offset = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001582 } else {
1583 down_read(&current->mm->mmap_sem);
1584 VERIFY(err, NULL != (vma = find_vma(current->mm,
1585 map->va)));
1586 if (err) {
1587 up_read(&current->mm->mmap_sem);
1588 goto bail;
1589 }
1590 offset = buf_page_start(buf) - vma->vm_start;
1591 up_read(&current->mm->mmap_sem);
1592 VERIFY(err, offset < (uintptr_t)map->size);
1593 if (err)
1594 goto bail;
1595 }
1596 pages[idx].addr = map->phys + offset;
1597 pages[idx].size = num << PAGE_SHIFT;
1598 }
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301599 rpra[i].buf.pv = lrpra[i].buf.pv = buf;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001600 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001601 PERF_END);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001602 for (i = bufs; i < bufs + handles; ++i) {
1603 struct fastrpc_mmap *map = ctx->maps[i];
1604
1605 pages[i].addr = map->phys;
1606 pages[i].size = map->size;
1607 }
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301608 if (!me->legacy) {
1609 fdlist = (uint64_t *)&pages[bufs + handles];
1610 for (i = 0; i < M_FDLIST; i++)
1611 fdlist[i] = 0;
1612 crclist = (uint32_t *)&fdlist[M_FDLIST];
1613 memset(crclist, 0, sizeof(uint32_t)*M_CRCLIST);
1614 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001615
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001616 /* copy non ion buffers */
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301617 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_COPY),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001618 rlen = copylen - metalen;
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301619 for (oix = 0; rpra && lrpra && oix < inbufs + outbufs; ++oix) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001620 int i = ctx->overps[oix]->raix;
1621 struct fastrpc_mmap *map = ctx->maps[i];
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301622 size_t mlen;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001623 uint64_t buf;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301624 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001625
1626 if (!len)
1627 continue;
1628 if (map)
1629 continue;
1630 if (ctx->overps[oix]->offset == 0) {
1631 rlen -= ALIGN(args, BALIGN) - args;
1632 args = ALIGN(args, BALIGN);
1633 }
Sathish Ambleyd209c1e2016-12-13 15:27:30 -08001634 mlen = ctx->overps[oix]->mend - ctx->overps[oix]->mstart;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001635 VERIFY(err, rlen >= mlen);
1636 if (err)
1637 goto bail;
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301638 rpra[i].buf.pv = lrpra[i].buf.pv =
1639 (args - ctx->overps[oix]->offset);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001640 pages[list[i].pgidx].addr = ctx->buf->phys -
1641 ctx->overps[oix]->offset +
1642 (copylen - rlen);
1643 pages[list[i].pgidx].addr =
1644 buf_page_start(pages[list[i].pgidx].addr);
1645 buf = rpra[i].buf.pv;
1646 pages[list[i].pgidx].size = buf_num_pages(buf, len) * PAGE_SIZE;
1647 if (i < inbufs) {
1648 K_COPY_FROM_USER(err, kernel, uint64_to_ptr(buf),
1649 lpra[i].buf.pv, len);
1650 if (err)
1651 goto bail;
1652 }
1653 args = args + mlen;
1654 rlen -= mlen;
1655 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001656 PERF_END);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001657
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301658 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_FLUSH),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001659 for (oix = 0; oix < inbufs + outbufs; ++oix) {
1660 int i = ctx->overps[oix]->raix;
1661 struct fastrpc_mmap *map = ctx->maps[i];
1662
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001663 if (map && map->uncached)
1664 continue;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +05301665 if (ctx->fl->sctx->smmu.coherent &&
1666 !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT)))
1667 continue;
1668 if (map && (map->attr & FASTRPC_ATTR_COHERENT))
1669 continue;
1670
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301671 if (rpra && lrpra && rpra[i].buf.len &&
1672 ctx->overps[oix]->mstart) {
Tharun Kumar Merugub67336e2017-08-08 18:56:03 +05301673 if (map && map->handle)
1674 msm_ion_do_cache_op(ctx->fl->apps->client,
1675 map->handle,
1676 uint64_to_ptr(rpra[i].buf.pv),
1677 rpra[i].buf.len,
1678 ION_IOC_CLEAN_INV_CACHES);
1679 else
1680 dmac_flush_range(uint64_to_ptr(rpra[i].buf.pv),
1681 uint64_to_ptr(rpra[i].buf.pv
1682 + rpra[i].buf.len));
1683 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001684 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001685 PERF_END);
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301686 for (i = bufs; rpra && lrpra && i < bufs + handles; i++) {
1687 rpra[i].dma.fd = lrpra[i].dma.fd = ctx->fds[i];
1688 rpra[i].dma.len = lrpra[i].dma.len = (uint32_t)lpra[i].buf.len;
1689 rpra[i].dma.offset = lrpra[i].dma.offset =
1690 (uint32_t)(uintptr_t)lpra[i].buf.pv;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001691 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001692
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001693 bail:
1694 return err;
1695}
1696
1697static int put_args(uint32_t kernel, struct smq_invoke_ctx *ctx,
1698 remote_arg_t *upra)
1699{
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301700 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001701 uint32_t sc = ctx->sc;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001702 struct smq_invoke_buf *list;
1703 struct smq_phy_page *pages;
1704 struct fastrpc_mmap *mmap;
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301705 uint64_t *fdlist = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07001706 uint32_t *crclist = NULL;
1707
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301708 remote_arg64_t *rpra = ctx->lrpra;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001709 int i, inbufs, outbufs, handles;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001710 int err = 0;
1711
1712 inbufs = REMOTE_SCALARS_INBUFS(sc);
1713 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001714 handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc);
1715 list = smq_invoke_buf_start(ctx->rpra, sc);
1716 pages = smq_phy_page_start(sc, list);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301717 if (!me->legacy) {
1718 fdlist = (uint64_t *)(pages + inbufs + outbufs + handles);
1719 crclist = (uint32_t *)(fdlist + M_FDLIST);
1720 }
Sathish Ambleybae51902017-07-03 15:00:49 -07001721
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001722 for (i = inbufs; i < inbufs + outbufs; ++i) {
1723 if (!ctx->maps[i]) {
1724 K_COPY_TO_USER(err, kernel,
1725 ctx->lpra[i].buf.pv,
1726 uint64_to_ptr(rpra[i].buf.pv),
1727 rpra[i].buf.len);
1728 if (err)
1729 goto bail;
1730 } else {
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301731 mutex_lock(&ctx->fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05301732 fastrpc_mmap_free(ctx->maps[i], 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301733 mutex_unlock(&ctx->fl->fl_map_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05301734 ctx->maps[i] = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001735 }
1736 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301737 mutex_lock(&ctx->fl->fl_map_mutex);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301738 if (fdlist && (inbufs + outbufs + handles)) {
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001739 for (i = 0; i < M_FDLIST; i++) {
1740 if (!fdlist[i])
1741 break;
1742 if (!fastrpc_mmap_find(ctx->fl, (int)fdlist[i], 0, 0,
Sathish Ambleyae5ee542017-01-16 22:24:23 -08001743 0, 0, &mmap))
c_mtharu7bd6a422017-10-17 18:15:37 +05301744 fastrpc_mmap_free(mmap, 0);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001745 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001746 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301747 mutex_unlock(&ctx->fl->fl_map_mutex);
Sathish Ambleybae51902017-07-03 15:00:49 -07001748 if (ctx->crc && crclist && rpra)
c_mtharue1a5ce12017-10-13 20:47:09 +05301749 K_COPY_TO_USER(err, kernel, ctx->crc,
Sathish Ambleybae51902017-07-03 15:00:49 -07001750 crclist, M_CRCLIST*sizeof(uint32_t));
1751
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001752 bail:
1753 return err;
1754}
1755
1756static void inv_args_pre(struct smq_invoke_ctx *ctx)
1757{
1758 int i, inbufs, outbufs;
1759 uint32_t sc = ctx->sc;
1760 remote_arg64_t *rpra = ctx->rpra;
1761 uintptr_t end;
1762
1763 inbufs = REMOTE_SCALARS_INBUFS(sc);
1764 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
1765 for (i = inbufs; i < inbufs + outbufs; ++i) {
1766 struct fastrpc_mmap *map = ctx->maps[i];
1767
1768 if (map && map->uncached)
1769 continue;
1770 if (!rpra[i].buf.len)
1771 continue;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +05301772 if (ctx->fl->sctx->smmu.coherent &&
1773 !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT)))
1774 continue;
1775 if (map && (map->attr & FASTRPC_ATTR_COHERENT))
1776 continue;
1777
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001778 if (buf_page_start(ptr_to_uint64((void *)rpra)) ==
1779 buf_page_start(rpra[i].buf.pv))
1780 continue;
Tharun Kumar Merugub67336e2017-08-08 18:56:03 +05301781 if (!IS_CACHE_ALIGNED((uintptr_t)
1782 uint64_to_ptr(rpra[i].buf.pv))) {
1783 if (map && map->handle)
1784 msm_ion_do_cache_op(ctx->fl->apps->client,
1785 map->handle,
1786 uint64_to_ptr(rpra[i].buf.pv),
1787 sizeof(uintptr_t),
1788 ION_IOC_CLEAN_INV_CACHES);
1789 else
1790 dmac_flush_range(
1791 uint64_to_ptr(rpra[i].buf.pv), (char *)
1792 uint64_to_ptr(rpra[i].buf.pv + 1));
1793 }
1794
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001795 end = (uintptr_t)uint64_to_ptr(rpra[i].buf.pv +
1796 rpra[i].buf.len);
Tharun Kumar Merugub67336e2017-08-08 18:56:03 +05301797 if (!IS_CACHE_ALIGNED(end)) {
1798 if (map && map->handle)
1799 msm_ion_do_cache_op(ctx->fl->apps->client,
1800 map->handle,
1801 uint64_to_ptr(end),
1802 sizeof(uintptr_t),
1803 ION_IOC_CLEAN_INV_CACHES);
1804 else
1805 dmac_flush_range((char *)end,
1806 (char *)end + 1);
1807 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001808 }
1809}
1810
1811static void inv_args(struct smq_invoke_ctx *ctx)
1812{
1813 int i, inbufs, outbufs;
1814 uint32_t sc = ctx->sc;
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301815 remote_arg64_t *rpra = ctx->lrpra;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001816
1817 inbufs = REMOTE_SCALARS_INBUFS(sc);
1818 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
1819 for (i = inbufs; i < inbufs + outbufs; ++i) {
1820 struct fastrpc_mmap *map = ctx->maps[i];
1821
1822 if (map && map->uncached)
1823 continue;
1824 if (!rpra[i].buf.len)
1825 continue;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +05301826 if (ctx->fl->sctx->smmu.coherent &&
1827 !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT)))
1828 continue;
1829 if (map && (map->attr & FASTRPC_ATTR_COHERENT))
1830 continue;
1831
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001832 if (buf_page_start(ptr_to_uint64((void *)rpra)) ==
1833 buf_page_start(rpra[i].buf.pv)) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001834 continue;
1835 }
1836 if (map && map->handle)
1837 msm_ion_do_cache_op(ctx->fl->apps->client, map->handle,
1838 (char *)uint64_to_ptr(rpra[i].buf.pv),
1839 rpra[i].buf.len, ION_IOC_INV_CACHES);
1840 else
1841 dmac_inv_range((char *)uint64_to_ptr(rpra[i].buf.pv),
1842 (char *)uint64_to_ptr(rpra[i].buf.pv
1843 + rpra[i].buf.len));
1844 }
1845
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001846}
1847
1848static int fastrpc_invoke_send(struct smq_invoke_ctx *ctx,
1849 uint32_t kernel, uint32_t handle)
1850{
1851 struct smq_msg *msg = &ctx->msg;
1852 struct fastrpc_file *fl = ctx->fl;
1853 struct fastrpc_channel_ctx *channel_ctx = &fl->apps->channel[fl->cid];
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301854 int err = 0, len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001855
c_mtharue1a5ce12017-10-13 20:47:09 +05301856 VERIFY(err, NULL != channel_ctx->chan);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001857 if (err)
1858 goto bail;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301859 msg->pid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001860 msg->tid = current->pid;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301861 if (fl->sessionid)
1862 msg->tid |= (1 << SESSION_ID_INDEX);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001863 if (kernel)
1864 msg->pid = 0;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301865 msg->invoke.header.ctx = ctx->ctxid | fl->pd;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001866 msg->invoke.header.handle = handle;
1867 msg->invoke.header.sc = ctx->sc;
1868 msg->invoke.page.addr = ctx->buf ? ctx->buf->phys : 0;
1869 msg->invoke.page.size = buf_page_size(ctx->used);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301870 if (fl->apps->glink) {
1871 if (fl->ssrcount != channel_ctx->ssrcount) {
1872 err = -ECONNRESET;
1873 goto bail;
1874 }
1875 VERIFY(err, channel_ctx->link.port_state ==
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001876 FASTRPC_LINK_CONNECTED);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301877 if (err)
1878 goto bail;
1879 err = glink_tx(channel_ctx->chan,
1880 (void *)&fl->apps->channel[fl->cid], msg, sizeof(*msg),
1881 GLINK_TX_REQ_INTENT);
1882 } else {
1883 spin_lock(&fl->apps->hlock);
1884 len = smd_write((smd_channel_t *)
1885 channel_ctx->chan,
1886 msg, sizeof(*msg));
1887 spin_unlock(&fl->apps->hlock);
1888 VERIFY(err, len == sizeof(*msg));
1889 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001890 bail:
1891 return err;
1892}
1893
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301894static void fastrpc_smd_read_handler(int cid)
1895{
1896 struct fastrpc_apps *me = &gfa;
1897 struct smq_invoke_rsp rsp = {0};
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301898 int ret = 0, err = 0;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301899 uint32_t index;
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301900
1901 do {
1902 ret = smd_read_from_cb(me->channel[cid].chan, &rsp,
1903 sizeof(rsp));
1904 if (ret != sizeof(rsp))
1905 break;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301906
1907 index = (uint32_t)((rsp.ctx & FASTRPC_CTXID_MASK) >> 4);
1908 VERIFY(err, index < FASTRPC_CTX_MAX);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301909 if (err)
1910 goto bail;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301911
1912 VERIFY(err, !IS_ERR_OR_NULL(me->ctxtable[index]));
1913 if (err)
1914 goto bail;
1915
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05301916 VERIFY(err, ((me->ctxtable[index]->ctxid == (rsp.ctx & ~3)) &&
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301917 me->ctxtable[index]->magic == FASTRPC_CTX_MAGIC));
1918 if (err)
1919 goto bail;
1920
1921 context_notify_user(me->ctxtable[index], rsp.retval);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301922 } while (ret == sizeof(rsp));
1923bail:
1924 if (err)
1925 pr_err("adsprpc: invalid response or context\n");
1926
1927}
1928
1929static void smd_event_handler(void *priv, unsigned int event)
1930{
1931 struct fastrpc_apps *me = &gfa;
1932 int cid = (int)(uintptr_t)priv;
1933
1934 switch (event) {
1935 case SMD_EVENT_OPEN:
1936 complete(&me->channel[cid].workport);
1937 break;
1938 case SMD_EVENT_CLOSE:
1939 fastrpc_notify_drivers(me, cid);
1940 break;
1941 case SMD_EVENT_DATA:
1942 fastrpc_smd_read_handler(cid);
1943 break;
1944 }
1945}
1946
1947
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001948static void fastrpc_init(struct fastrpc_apps *me)
1949{
1950 int i;
1951
1952 INIT_HLIST_HEAD(&me->drivers);
Tharun Kumar Merugubcd6fbf2018-01-04 17:49:34 +05301953 INIT_HLIST_HEAD(&me->maps);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001954 spin_lock_init(&me->hlock);
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301955 spin_lock_init(&me->ctxlock);
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05301956 mutex_init(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001957 me->channel = &gcinfo[0];
1958 for (i = 0; i < NUM_CHANNELS; i++) {
1959 init_completion(&me->channel[i].work);
Tharun Kumar Merugu53a8ec92017-07-14 15:52:49 +05301960 init_completion(&me->channel[i].workport);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001961 me->channel[i].sesscount = 0;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05301962 /* All channels are secure by default except CDSP */
1963 me->channel[i].secure = SECURE_CHANNEL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001964 }
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05301965 /* Set CDSP channel to non secure */
1966 me->channel[CDSP_DOMAIN_ID].secure = NON_SECURE_CHANNEL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001967}
1968
1969static int fastrpc_release_current_dsp_process(struct fastrpc_file *fl);
1970
1971static int fastrpc_internal_invoke(struct fastrpc_file *fl, uint32_t mode,
1972 uint32_t kernel,
Sathish Ambleybae51902017-07-03 15:00:49 -07001973 struct fastrpc_ioctl_invoke_crc *inv)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001974{
c_mtharue1a5ce12017-10-13 20:47:09 +05301975 struct smq_invoke_ctx *ctx = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001976 struct fastrpc_ioctl_invoke *invoke = &inv->inv;
1977 int cid = fl->cid;
1978 int interrupted = 0;
1979 int err = 0;
Maria Yu757199c2017-09-22 16:05:49 +08001980 struct timespec invoket = {0};
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301981 int64_t *perf_counter = getperfcounter(fl, PERF_COUNT);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001982
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001983 if (fl->profile)
1984 getnstimeofday(&invoket);
Tharun Kumar Merugue3edf3e2017-07-27 12:34:07 +05301985
Tharun Kumar Merugucc2e11e2019-02-02 01:22:47 +05301986 if (!kernel) {
1987 VERIFY(err, invoke->handle != FASTRPC_STATIC_HANDLE_KERNEL);
1988 if (err) {
1989 pr_err("adsprpc: ERROR: %s: user application %s trying to send a kernel RPC message to channel %d",
1990 __func__, current->comm, cid);
1991 goto bail;
1992 }
1993 }
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301994
Tharun Kumar Merugue3edf3e2017-07-27 12:34:07 +05301995 VERIFY(err, fl->sctx != NULL);
1996 if (err)
1997 goto bail;
1998 VERIFY(err, fl->cid >= 0 && fl->cid < NUM_CHANNELS);
1999 if (err)
2000 goto bail;
2001
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002002 if (!kernel) {
2003 VERIFY(err, 0 == context_restore_interrupted(fl, inv,
2004 &ctx));
2005 if (err)
2006 goto bail;
2007 if (fl->sctx->smmu.faults)
2008 err = FASTRPC_ENOSUCH;
2009 if (err)
2010 goto bail;
2011 if (ctx)
2012 goto wait;
2013 }
2014
2015 VERIFY(err, 0 == context_alloc(fl, kernel, inv, &ctx));
2016 if (err)
2017 goto bail;
2018
2019 if (REMOTE_SCALARS_LENGTH(ctx->sc)) {
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302020 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_GETARGS),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002021 VERIFY(err, 0 == get_args(kernel, ctx));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002022 PERF_END);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002023 if (err)
2024 goto bail;
2025 }
2026
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302027 if (!fl->sctx->smmu.coherent) {
2028 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002029 inv_args_pre(ctx);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302030 PERF_END);
2031 }
2032
2033 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_LINK),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002034 VERIFY(err, 0 == fastrpc_invoke_send(ctx, kernel, invoke->handle));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002035 PERF_END);
2036
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002037 if (err)
2038 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002039 wait:
2040 if (kernel)
2041 wait_for_completion(&ctx->work);
2042 else {
2043 interrupted = wait_for_completion_interruptible(&ctx->work);
2044 VERIFY(err, 0 == (err = interrupted));
2045 if (err)
2046 goto bail;
2047 }
Mohammed Nayeem Ur Rahman32ba95d2019-07-26 17:31:37 +05302048 if (ctx->handle)
2049 glink_rx_done(ctx->handle, ctx->ptr, true);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302050 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS),
Sathish Ambleyc432b502017-06-05 12:03:42 -07002051 if (!fl->sctx->smmu.coherent)
2052 inv_args(ctx);
2053 PERF_END);
2054
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002055 VERIFY(err, 0 == (err = ctx->retval));
2056 if (err)
2057 goto bail;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002058
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302059 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_PUTARGS),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002060 VERIFY(err, 0 == put_args(kernel, ctx, invoke->pra));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002061 PERF_END);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002062 if (err)
2063 goto bail;
2064 bail:
2065 if (ctx && interrupted == -ERESTARTSYS)
2066 context_save_interrupted(ctx);
2067 else if (ctx)
2068 context_free(ctx);
2069 if (fl->ssrcount != fl->apps->channel[cid].ssrcount)
2070 err = ECONNRESET;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002071
2072 if (fl->profile && !interrupted) {
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302073 if (invoke->handle != FASTRPC_STATIC_HANDLE_LISTENER) {
2074 int64_t *count = GET_COUNTER(perf_counter, PERF_INVOKE);
2075
2076 if (count)
2077 *count += getnstimediff(&invoket);
2078 }
2079 if (invoke->handle > FASTRPC_STATIC_HANDLE_MAX) {
2080 int64_t *count = GET_COUNTER(perf_counter, PERF_COUNT);
2081
2082 if (count)
2083 *count = *count+1;
2084 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002085 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002086 return err;
2087}
2088
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05302089static int fastrpc_get_adsp_session(char *name, int *session)
2090{
2091 struct fastrpc_apps *me = &gfa;
2092 int err = 0, i;
2093
2094 for (i = 0; i < NUM_SESSIONS; i++) {
2095 if (!me->channel[0].spd[i].spdname)
2096 continue;
2097 if (!strcmp(name, me->channel[0].spd[i].spdname))
2098 break;
2099 }
2100 VERIFY(err, i < NUM_SESSIONS);
2101 if (err)
2102 goto bail;
2103 *session = i;
2104bail:
2105 return err;
2106}
2107
2108static int fastrpc_mmap_remove_pdr(struct fastrpc_file *fl);
Sathish Ambley36849af2017-02-02 09:35:55 -08002109static int fastrpc_channel_open(struct fastrpc_file *fl);
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05302110static int fastrpc_mmap_remove_ssr(struct fastrpc_file *fl);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002111static int fastrpc_init_process(struct fastrpc_file *fl,
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002112 struct fastrpc_ioctl_init_attrs *uproc)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002113{
2114 int err = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05302115 struct fastrpc_apps *me = &gfa;
Sathish Ambleybae51902017-07-03 15:00:49 -07002116 struct fastrpc_ioctl_invoke_crc ioctl;
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002117 struct fastrpc_ioctl_init *init = &uproc->init;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002118 struct smq_phy_page pages[1];
c_mtharue1a5ce12017-10-13 20:47:09 +05302119 struct fastrpc_mmap *file = NULL, *mem = NULL;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302120 struct fastrpc_buf *imem = NULL;
2121 unsigned long imem_dma_attr = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05302122 char *proc_name = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002123
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302124 VERIFY(err, 0 == (err = fastrpc_channel_open(fl)));
Sathish Ambley36849af2017-02-02 09:35:55 -08002125 if (err)
2126 goto bail;
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302127 if (init->flags == FASTRPC_INIT_ATTACH ||
2128 init->flags == FASTRPC_INIT_ATTACH_SENSORS) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002129 remote_arg_t ra[1];
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05302130 int tgid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002131
2132 ra[0].buf.pv = (void *)&tgid;
2133 ra[0].buf.len = sizeof(tgid);
Tharun Kumar Merugucc2e11e2019-02-02 01:22:47 +05302134 ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002135 ioctl.inv.sc = REMOTE_SCALARS_MAKE(0, 1, 0);
2136 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05302137 ioctl.fds = NULL;
2138 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07002139 ioctl.crc = NULL;
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302140 if (init->flags == FASTRPC_INIT_ATTACH)
2141 fl->pd = 0;
2142 else if (init->flags == FASTRPC_INIT_ATTACH_SENSORS) {
2143 fl->spdname = SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME;
2144 fl->pd = 2;
2145 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002146 VERIFY(err, !(err = fastrpc_internal_invoke(fl,
2147 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
2148 if (err)
2149 goto bail;
2150 } else if (init->flags == FASTRPC_INIT_CREATE) {
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002151 remote_arg_t ra[6];
2152 int fds[6];
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002153 int mflags = 0;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302154 int memlen;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002155 struct {
2156 int pgid;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302157 unsigned int namelen;
2158 unsigned int filelen;
2159 unsigned int pageslen;
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002160 int attrs;
2161 int siglen;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002162 } inbuf;
2163
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05302164 inbuf.pgid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002165 inbuf.namelen = strlen(current->comm) + 1;
2166 inbuf.filelen = init->filelen;
2167 fl->pd = 1;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302168
Tharun Kumar Merugudf852892017-12-07 16:27:37 +05302169 VERIFY(err, access_ok(0, (void __user *)init->file,
2170 init->filelen));
2171 if (err)
2172 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002173 if (init->filelen) {
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302174 mutex_lock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002175 VERIFY(err, !fastrpc_mmap_create(fl, init->filefd, 0,
2176 init->file, init->filelen, mflags, &file));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302177 mutex_unlock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002178 if (err)
2179 goto bail;
2180 }
2181 inbuf.pageslen = 1;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302182
2183 VERIFY(err, !init->mem);
2184 if (err) {
2185 err = -EINVAL;
2186 pr_err("adsprpc: %s: %s: ERROR: donated memory allocated in userspace\n",
2187 current->comm, __func__);
2188 goto bail;
2189 }
2190 memlen = ALIGN(max(1024*1024*3, (int)init->filelen * 4),
2191 1024*1024);
2192 imem_dma_attr = DMA_ATTR_EXEC_MAPPING |
2193 DMA_ATTR_NO_KERNEL_MAPPING |
2194 DMA_ATTR_FORCE_NON_COHERENT;
2195 err = fastrpc_buf_alloc(fl, memlen, imem_dma_attr, 0, 0, &imem);
Tharun Kumar Merugudf852892017-12-07 16:27:37 +05302196 if (err)
2197 goto bail;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302198 fl->init_mem = imem;
2199
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002200 inbuf.pageslen = 1;
2201 ra[0].buf.pv = (void *)&inbuf;
2202 ra[0].buf.len = sizeof(inbuf);
2203 fds[0] = 0;
2204
2205 ra[1].buf.pv = (void *)current->comm;
2206 ra[1].buf.len = inbuf.namelen;
2207 fds[1] = 0;
2208
2209 ra[2].buf.pv = (void *)init->file;
2210 ra[2].buf.len = inbuf.filelen;
2211 fds[2] = init->filefd;
2212
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302213 pages[0].addr = imem->phys;
2214 pages[0].size = imem->size;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002215 ra[3].buf.pv = (void *)pages;
2216 ra[3].buf.len = 1 * sizeof(*pages);
2217 fds[3] = 0;
2218
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002219 inbuf.attrs = uproc->attrs;
2220 ra[4].buf.pv = (void *)&(inbuf.attrs);
2221 ra[4].buf.len = sizeof(inbuf.attrs);
2222 fds[4] = 0;
2223
2224 inbuf.siglen = uproc->siglen;
2225 ra[5].buf.pv = (void *)&(inbuf.siglen);
2226 ra[5].buf.len = sizeof(inbuf.siglen);
2227 fds[5] = 0;
2228
Tharun Kumar Merugucc2e11e2019-02-02 01:22:47 +05302229 ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002230 ioctl.inv.sc = REMOTE_SCALARS_MAKE(6, 4, 0);
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002231 if (uproc->attrs)
2232 ioctl.inv.sc = REMOTE_SCALARS_MAKE(7, 6, 0);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002233 ioctl.inv.pra = ra;
2234 ioctl.fds = fds;
c_mtharue1a5ce12017-10-13 20:47:09 +05302235 ioctl.attrs = NULL;
2236 ioctl.crc = NULL;
2237 VERIFY(err, !(err = fastrpc_internal_invoke(fl,
2238 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
2239 if (err)
2240 goto bail;
2241 } else if (init->flags == FASTRPC_INIT_CREATE_STATIC) {
2242 remote_arg_t ra[3];
2243 uint64_t phys = 0;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302244 size_t size = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05302245 int fds[3];
2246 struct {
2247 int pgid;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302248 unsigned int namelen;
2249 unsigned int pageslen;
c_mtharue1a5ce12017-10-13 20:47:09 +05302250 } inbuf;
2251
2252 if (!init->filelen)
2253 goto bail;
2254
2255 proc_name = kzalloc(init->filelen, GFP_KERNEL);
2256 VERIFY(err, !IS_ERR_OR_NULL(proc_name));
2257 if (err)
2258 goto bail;
2259 VERIFY(err, 0 == copy_from_user((void *)proc_name,
2260 (void __user *)init->file, init->filelen));
2261 if (err)
2262 goto bail;
2263
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302264 fl->pd = 1;
c_mtharue1a5ce12017-10-13 20:47:09 +05302265 inbuf.pgid = current->tgid;
c_mtharu81a0aa72017-11-07 16:13:21 +05302266 inbuf.namelen = init->filelen;
c_mtharue1a5ce12017-10-13 20:47:09 +05302267 inbuf.pageslen = 0;
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05302268
2269 if (!strcmp(proc_name, "audiopd")) {
2270 fl->spdname = AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME;
2271 VERIFY(err, !fastrpc_mmap_remove_pdr(fl));
Tharun Kumar Merugu35173342018-02-08 16:13:17 +05302272 if (err)
2273 goto bail;
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05302274 }
2275
c_mtharue1a5ce12017-10-13 20:47:09 +05302276 if (!me->staticpd_flags) {
2277 inbuf.pageslen = 1;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302278 mutex_lock(&fl->fl_map_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05302279 VERIFY(err, !fastrpc_mmap_create(fl, -1, 0, init->mem,
2280 init->memlen, ADSP_MMAP_REMOTE_HEAP_ADDR,
2281 &mem));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302282 mutex_unlock(&fl->fl_map_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05302283 if (err)
2284 goto bail;
2285 phys = mem->phys;
2286 size = mem->size;
2287 VERIFY(err, !hyp_assign_phys(phys, (uint64_t)size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05302288 hlosvm, 1, me->channel[fl->cid].rhvm.vmid,
2289 me->channel[fl->cid].rhvm.vmperm,
2290 me->channel[fl->cid].rhvm.vmcount));
c_mtharue1a5ce12017-10-13 20:47:09 +05302291 if (err) {
2292 pr_err("ADSPRPC: hyp_assign_phys fail err %d",
2293 err);
2294 pr_err("map->phys %llx, map->size %d\n",
2295 phys, (int)size);
2296 goto bail;
2297 }
2298 me->staticpd_flags = 1;
2299 }
2300
2301 ra[0].buf.pv = (void *)&inbuf;
2302 ra[0].buf.len = sizeof(inbuf);
2303 fds[0] = 0;
2304
2305 ra[1].buf.pv = (void *)proc_name;
2306 ra[1].buf.len = inbuf.namelen;
2307 fds[1] = 0;
2308
2309 pages[0].addr = phys;
2310 pages[0].size = size;
2311
2312 ra[2].buf.pv = (void *)pages;
2313 ra[2].buf.len = sizeof(*pages);
2314 fds[2] = 0;
Tharun Kumar Merugucc2e11e2019-02-02 01:22:47 +05302315 ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL;
c_mtharue1a5ce12017-10-13 20:47:09 +05302316
2317 ioctl.inv.sc = REMOTE_SCALARS_MAKE(8, 3, 0);
2318 ioctl.inv.pra = ra;
2319 ioctl.fds = NULL;
2320 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07002321 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002322 VERIFY(err, !(err = fastrpc_internal_invoke(fl,
2323 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
2324 if (err)
2325 goto bail;
2326 } else {
2327 err = -ENOTTY;
2328 }
2329bail:
c_mtharud91205a2017-11-07 16:01:06 +05302330 kfree(proc_name);
c_mtharue1a5ce12017-10-13 20:47:09 +05302331 if (err && (init->flags == FASTRPC_INIT_CREATE_STATIC))
2332 me->staticpd_flags = 0;
2333 if (mem && err) {
2334 if (mem->flags == ADSP_MMAP_REMOTE_HEAP_ADDR)
2335 hyp_assign_phys(mem->phys, (uint64_t)mem->size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05302336 me->channel[fl->cid].rhvm.vmid,
2337 me->channel[fl->cid].rhvm.vmcount,
2338 hlosvm, hlosvmperm, 1);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302339 mutex_lock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302340 fastrpc_mmap_free(mem, 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302341 mutex_unlock(&fl->fl_map_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05302342 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302343 if (file) {
2344 mutex_lock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302345 fastrpc_mmap_free(file, 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302346 mutex_unlock(&fl->fl_map_mutex);
2347 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002348 return err;
2349}
2350
2351static int fastrpc_release_current_dsp_process(struct fastrpc_file *fl)
2352{
2353 int err = 0;
Sathish Ambleybae51902017-07-03 15:00:49 -07002354 struct fastrpc_ioctl_invoke_crc ioctl;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002355 remote_arg_t ra[1];
2356 int tgid = 0;
2357
Sathish Ambley36849af2017-02-02 09:35:55 -08002358 VERIFY(err, fl->cid >= 0 && fl->cid < NUM_CHANNELS);
2359 if (err)
2360 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05302361 VERIFY(err, fl->apps->channel[fl->cid].chan != NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002362 if (err)
2363 goto bail;
2364 tgid = fl->tgid;
2365 ra[0].buf.pv = (void *)&tgid;
2366 ra[0].buf.len = sizeof(tgid);
Tharun Kumar Merugucc2e11e2019-02-02 01:22:47 +05302367 ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002368 ioctl.inv.sc = REMOTE_SCALARS_MAKE(1, 1, 0);
2369 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05302370 ioctl.fds = NULL;
2371 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07002372 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002373 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
2374 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
2375bail:
2376 return err;
2377}
2378
2379static int fastrpc_mmap_on_dsp(struct fastrpc_file *fl, uint32_t flags,
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302380 uintptr_t va, uint64_t phys,
2381 size_t size, uintptr_t *raddr)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002382{
Sathish Ambleybae51902017-07-03 15:00:49 -07002383 struct fastrpc_ioctl_invoke_crc ioctl;
c_mtharu63ffc012017-11-16 15:26:56 +05302384 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002385 struct smq_phy_page page;
2386 int num = 1;
2387 remote_arg_t ra[3];
2388 int err = 0;
2389 struct {
2390 int pid;
2391 uint32_t flags;
2392 uintptr_t vaddrin;
2393 int num;
2394 } inargs;
2395 struct {
2396 uintptr_t vaddrout;
2397 } routargs;
2398
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05302399 inargs.pid = fl->tgid;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302400 inargs.vaddrin = (uintptr_t)va;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002401 inargs.flags = flags;
2402 inargs.num = fl->apps->compat ? num * sizeof(page) : num;
2403 ra[0].buf.pv = (void *)&inargs;
2404 ra[0].buf.len = sizeof(inargs);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302405 page.addr = phys;
2406 page.size = size;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002407 ra[1].buf.pv = (void *)&page;
2408 ra[1].buf.len = num * sizeof(page);
2409
2410 ra[2].buf.pv = (void *)&routargs;
2411 ra[2].buf.len = sizeof(routargs);
2412
Tharun Kumar Merugucc2e11e2019-02-02 01:22:47 +05302413 ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002414 if (fl->apps->compat)
2415 ioctl.inv.sc = REMOTE_SCALARS_MAKE(4, 2, 1);
2416 else
2417 ioctl.inv.sc = REMOTE_SCALARS_MAKE(2, 2, 1);
2418 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05302419 ioctl.fds = NULL;
2420 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07002421 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002422 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
2423 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302424 *raddr = (uintptr_t)routargs.vaddrout;
c_mtharue1a5ce12017-10-13 20:47:09 +05302425 if (err)
2426 goto bail;
2427 if (flags == ADSP_MMAP_HEAP_ADDR) {
2428 struct scm_desc desc = {0};
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002429
c_mtharue1a5ce12017-10-13 20:47:09 +05302430 desc.args[0] = TZ_PIL_AUTH_QDSP6_PROC;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302431 desc.args[1] = phys;
2432 desc.args[2] = size;
c_mtharue1a5ce12017-10-13 20:47:09 +05302433 desc.arginfo = SCM_ARGS(3);
2434 err = scm_call2(SCM_SIP_FNID(SCM_SVC_PIL,
2435 TZ_PIL_PROTECT_MEM_SUBSYS_ID), &desc);
2436 } else if (flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302437 VERIFY(err, !hyp_assign_phys(phys, (uint64_t)size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05302438 hlosvm, 1, me->channel[fl->cid].rhvm.vmid,
2439 me->channel[fl->cid].rhvm.vmperm,
2440 me->channel[fl->cid].rhvm.vmcount));
c_mtharue1a5ce12017-10-13 20:47:09 +05302441 if (err)
2442 goto bail;
2443 }
2444bail:
2445 return err;
2446}
2447
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302448static int fastrpc_munmap_on_dsp_rh(struct fastrpc_file *fl, uint64_t phys,
2449 size_t size, uint32_t flags)
c_mtharue1a5ce12017-10-13 20:47:09 +05302450{
2451 int err = 0;
c_mtharu63ffc012017-11-16 15:26:56 +05302452 struct fastrpc_apps *me = &gfa;
Tharun Kumar Merugu72c90252019-08-29 18:36:08 +05302453 int tgid = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05302454 int destVM[1] = {VMID_HLOS};
2455 int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
2456
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302457 if (flags == ADSP_MMAP_HEAP_ADDR) {
c_mtharue1a5ce12017-10-13 20:47:09 +05302458 struct fastrpc_ioctl_invoke_crc ioctl;
2459 struct scm_desc desc = {0};
Tharun Kumar Merugu72c90252019-08-29 18:36:08 +05302460 remote_arg_t ra[2];
2461
c_mtharue1a5ce12017-10-13 20:47:09 +05302462 struct {
2463 uint8_t skey;
2464 } routargs;
2465
Tharun Kumar Merugu72c90252019-08-29 18:36:08 +05302466 if (fl == NULL)
2467 goto bail;
2468 tgid = fl->tgid;
2469 ra[0].buf.pv = (void *)&tgid;
2470 ra[0].buf.len = sizeof(tgid);
2471 ra[1].buf.pv = (void *)&routargs;
2472 ra[1].buf.len = sizeof(routargs);
c_mtharue1a5ce12017-10-13 20:47:09 +05302473
Tharun Kumar Merugucc2e11e2019-02-02 01:22:47 +05302474 ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL;
Tharun Kumar Merugu72c90252019-08-29 18:36:08 +05302475 ioctl.inv.sc = REMOTE_SCALARS_MAKE(9, 1, 1);
c_mtharue1a5ce12017-10-13 20:47:09 +05302476 ioctl.inv.pra = ra;
2477 ioctl.fds = NULL;
2478 ioctl.attrs = NULL;
2479 ioctl.crc = NULL;
Tharun Kumar Merugu72c90252019-08-29 18:36:08 +05302480
c_mtharue1a5ce12017-10-13 20:47:09 +05302481
2482 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
2483 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
2484 if (err)
2485 goto bail;
2486 desc.args[0] = TZ_PIL_AUTH_QDSP6_PROC;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302487 desc.args[1] = phys;
2488 desc.args[2] = size;
c_mtharue1a5ce12017-10-13 20:47:09 +05302489 desc.args[3] = routargs.skey;
2490 desc.arginfo = SCM_ARGS(4);
2491 err = scm_call2(SCM_SIP_FNID(SCM_SVC_PIL,
2492 TZ_PIL_CLEAR_PROTECT_MEM_SUBSYS_ID), &desc);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302493 } else if (flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
2494 VERIFY(err, !hyp_assign_phys(phys, (uint64_t)size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05302495 me->channel[fl->cid].rhvm.vmid,
2496 me->channel[fl->cid].rhvm.vmcount,
2497 destVM, destVMperm, 1));
c_mtharue1a5ce12017-10-13 20:47:09 +05302498 if (err)
2499 goto bail;
2500 }
2501
2502bail:
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002503 return err;
2504}
2505
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302506static int fastrpc_munmap_on_dsp(struct fastrpc_file *fl, uintptr_t raddr,
2507 uint64_t phys, size_t size, uint32_t flags)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002508{
Sathish Ambleybae51902017-07-03 15:00:49 -07002509 struct fastrpc_ioctl_invoke_crc ioctl;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002510 remote_arg_t ra[1];
2511 int err = 0;
2512 struct {
2513 int pid;
2514 uintptr_t vaddrout;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302515 size_t size;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002516 } inargs;
2517
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05302518 inargs.pid = fl->tgid;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302519 inargs.size = size;
2520 inargs.vaddrout = raddr;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002521 ra[0].buf.pv = (void *)&inargs;
2522 ra[0].buf.len = sizeof(inargs);
2523
Tharun Kumar Merugucc2e11e2019-02-02 01:22:47 +05302524 ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002525 if (fl->apps->compat)
2526 ioctl.inv.sc = REMOTE_SCALARS_MAKE(5, 1, 0);
2527 else
2528 ioctl.inv.sc = REMOTE_SCALARS_MAKE(3, 1, 0);
2529 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05302530 ioctl.fds = NULL;
2531 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07002532 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002533 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
2534 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
c_mtharue1a5ce12017-10-13 20:47:09 +05302535 if (err)
2536 goto bail;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302537 if (flags == ADSP_MMAP_HEAP_ADDR ||
2538 flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
2539 VERIFY(err, !fastrpc_munmap_on_dsp_rh(fl, phys, size, flags));
c_mtharue1a5ce12017-10-13 20:47:09 +05302540 if (err)
2541 goto bail;
2542 }
2543bail:
2544 return err;
2545}
2546
2547static int fastrpc_mmap_remove_ssr(struct fastrpc_file *fl)
2548{
2549 struct fastrpc_mmap *match = NULL, *map = NULL;
2550 struct hlist_node *n = NULL;
2551 int err = 0, ret = 0;
2552 struct fastrpc_apps *me = &gfa;
2553 struct ramdump_segment *ramdump_segments_rh = NULL;
2554
2555 do {
2556 match = NULL;
2557 spin_lock(&me->hlock);
2558 hlist_for_each_entry_safe(map, n, &me->maps, hn) {
2559 match = map;
2560 hlist_del_init(&map->hn);
2561 break;
2562 }
2563 spin_unlock(&me->hlock);
2564
2565 if (match) {
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302566 VERIFY(err, !fastrpc_munmap_on_dsp_rh(fl, match->phys,
2567 match->size, match->flags));
c_mtharue1a5ce12017-10-13 20:47:09 +05302568 if (err)
2569 goto bail;
2570 if (me->channel[0].ramdumpenabled) {
2571 ramdump_segments_rh = kcalloc(1,
2572 sizeof(struct ramdump_segment), GFP_KERNEL);
2573 if (ramdump_segments_rh) {
2574 ramdump_segments_rh->address =
2575 match->phys;
2576 ramdump_segments_rh->size = match->size;
2577 ret = do_elf_ramdump(
2578 me->channel[0].remoteheap_ramdump_dev,
2579 ramdump_segments_rh, 1);
2580 if (ret < 0)
2581 pr_err("ADSPRPC: unable to dump heap");
2582 kfree(ramdump_segments_rh);
2583 }
2584 }
c_mtharu7bd6a422017-10-17 18:15:37 +05302585 fastrpc_mmap_free(match, 0);
c_mtharue1a5ce12017-10-13 20:47:09 +05302586 }
2587 } while (match);
2588bail:
2589 if (err && match)
2590 fastrpc_mmap_add(match);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002591 return err;
2592}
2593
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05302594static int fastrpc_mmap_remove_pdr(struct fastrpc_file *fl)
2595{
2596 struct fastrpc_apps *me = &gfa;
2597 int session = 0, err = 0;
2598
2599 VERIFY(err, !fastrpc_get_adsp_session(
2600 AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME, &session));
2601 if (err)
2602 goto bail;
2603 if (me->channel[fl->cid].spd[session].pdrcount !=
2604 me->channel[fl->cid].spd[session].prevpdrcount) {
2605 if (fastrpc_mmap_remove_ssr(fl))
2606 pr_err("ADSPRPC: SSR: Failed to unmap remote heap\n");
2607 me->channel[fl->cid].spd[session].prevpdrcount =
2608 me->channel[fl->cid].spd[session].pdrcount;
2609 }
2610 if (!me->channel[fl->cid].spd[session].ispdup) {
2611 VERIFY(err, 0);
2612 if (err) {
2613 err = -ENOTCONN;
2614 goto bail;
2615 }
2616 }
2617bail:
2618 return err;
2619}
2620
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002621static int fastrpc_mmap_remove(struct fastrpc_file *fl, uintptr_t va,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302622 size_t len, struct fastrpc_mmap **ppmap);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002623
2624static void fastrpc_mmap_add(struct fastrpc_mmap *map);
2625
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05302626static inline void get_fastrpc_ioctl_mmap_64(
2627 struct fastrpc_ioctl_mmap_64 *mmap64,
2628 struct fastrpc_ioctl_mmap *immap)
2629{
2630 immap->fd = mmap64->fd;
2631 immap->flags = mmap64->flags;
2632 immap->vaddrin = (uintptr_t)mmap64->vaddrin;
2633 immap->size = mmap64->size;
2634}
2635
2636static inline void put_fastrpc_ioctl_mmap_64(
2637 struct fastrpc_ioctl_mmap_64 *mmap64,
2638 struct fastrpc_ioctl_mmap *immap)
2639{
2640 mmap64->vaddrout = (uint64_t)immap->vaddrout;
2641}
2642
2643static inline void get_fastrpc_ioctl_munmap_64(
2644 struct fastrpc_ioctl_munmap_64 *munmap64,
2645 struct fastrpc_ioctl_munmap *imunmap)
2646{
2647 imunmap->vaddrout = (uintptr_t)munmap64->vaddrout;
2648 imunmap->size = munmap64->size;
2649}
2650
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002651static int fastrpc_internal_munmap(struct fastrpc_file *fl,
2652 struct fastrpc_ioctl_munmap *ud)
2653{
2654 int err = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05302655 struct fastrpc_mmap *map = NULL;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302656 struct fastrpc_buf *rbuf = NULL, *free = NULL;
2657 struct hlist_node *n;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002658
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302659 mutex_lock(&fl->map_mutex);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302660
2661 spin_lock(&fl->hlock);
2662 hlist_for_each_entry_safe(rbuf, n, &fl->remote_bufs, hn_rem) {
2663 if (rbuf->raddr && (rbuf->flags == ADSP_MMAP_ADD_PAGES)) {
2664 if ((rbuf->raddr == ud->vaddrout) &&
2665 (rbuf->size == ud->size)) {
2666 free = rbuf;
2667 break;
2668 }
2669 }
2670 }
2671 spin_unlock(&fl->hlock);
2672
2673 if (free) {
2674 VERIFY(err, !fastrpc_munmap_on_dsp(fl, free->raddr,
2675 free->phys, free->size, free->flags));
2676 if (err)
2677 goto bail;
2678 fastrpc_buf_free(rbuf, 0);
2679 mutex_unlock(&fl->map_mutex);
2680 return err;
2681 }
2682
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302683 mutex_lock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002684 VERIFY(err, !fastrpc_mmap_remove(fl, ud->vaddrout, ud->size, &map));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302685 mutex_unlock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002686 if (err)
2687 goto bail;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302688 VERIFY(err, !fastrpc_munmap_on_dsp(fl, map->raddr,
2689 map->phys, map->size, map->flags));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002690 if (err)
2691 goto bail;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302692 mutex_lock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302693 fastrpc_mmap_free(map, 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302694 mutex_unlock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002695bail:
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302696 if (err && map) {
2697 mutex_lock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002698 fastrpc_mmap_add(map);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302699 mutex_unlock(&fl->fl_map_mutex);
2700 }
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302701 mutex_unlock(&fl->map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002702 return err;
2703}
2704
c_mtharu7bd6a422017-10-17 18:15:37 +05302705static int fastrpc_internal_munmap_fd(struct fastrpc_file *fl,
2706 struct fastrpc_ioctl_munmap_fd *ud) {
2707 int err = 0;
2708 struct fastrpc_mmap *map = NULL;
2709
2710 VERIFY(err, (fl && ud));
2711 if (err)
2712 goto bail;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302713 mutex_lock(&fl->fl_map_mutex);
Tharun Kumar Merugu09fc6152018-02-16 13:13:12 +05302714 if (fastrpc_mmap_find(fl, ud->fd, ud->va, ud->len, 0, 0, &map)) {
2715 pr_err("adsprpc: mapping not found to unmap %d va %llx %x\n",
c_mtharu7bd6a422017-10-17 18:15:37 +05302716 ud->fd, (unsigned long long)ud->va,
2717 (unsigned int)ud->len);
2718 err = -1;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302719 mutex_unlock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302720 goto bail;
2721 }
2722 if (map)
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302723 fastrpc_mmap_free(map, 0);
2724 mutex_unlock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302725bail:
2726 return err;
2727}
2728
2729
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002730static int fastrpc_internal_mmap(struct fastrpc_file *fl,
2731 struct fastrpc_ioctl_mmap *ud)
2732{
2733
c_mtharue1a5ce12017-10-13 20:47:09 +05302734 struct fastrpc_mmap *map = NULL;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302735 struct fastrpc_buf *rbuf = NULL;
2736 unsigned long dma_attr = 0;
2737 uintptr_t raddr = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002738 int err = 0;
2739
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302740 mutex_lock(&fl->map_mutex);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302741 if (ud->flags == ADSP_MMAP_ADD_PAGES) {
2742 if (ud->vaddrin) {
2743 err = -EINVAL;
2744 pr_err("adsprpc: %s: %s: ERROR: adding user allocated pages is not supported\n",
2745 current->comm, __func__);
2746 goto bail;
2747 }
2748 dma_attr = DMA_ATTR_EXEC_MAPPING |
2749 DMA_ATTR_NO_KERNEL_MAPPING |
2750 DMA_ATTR_FORCE_NON_COHERENT;
2751 err = fastrpc_buf_alloc(fl, ud->size, dma_attr, ud->flags,
2752 1, &rbuf);
2753 if (err)
2754 goto bail;
Tharun Kumar Merugu0d0b69e2018-09-14 22:30:58 +05302755 err = fastrpc_mmap_on_dsp(fl, ud->flags, 0,
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302756 rbuf->phys, rbuf->size, &raddr);
2757 if (err)
2758 goto bail;
2759 rbuf->raddr = raddr;
2760 } else {
Tharun Kumar Merugu0d0b69e2018-09-14 22:30:58 +05302761
2762 uintptr_t va_to_dsp;
2763
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302764 mutex_lock(&fl->fl_map_mutex);
2765 if (!fastrpc_mmap_find(fl, ud->fd, (uintptr_t)ud->vaddrin,
2766 ud->size, ud->flags, 1, &map)) {
2767 mutex_unlock(&fl->fl_map_mutex);
2768 mutex_unlock(&fl->map_mutex);
2769 return 0;
2770 }
Tharun Kumar Merugu0d0b69e2018-09-14 22:30:58 +05302771
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302772 VERIFY(err, !fastrpc_mmap_create(fl, ud->fd, 0,
2773 (uintptr_t)ud->vaddrin, ud->size,
2774 ud->flags, &map));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302775 mutex_unlock(&fl->fl_map_mutex);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302776 if (err)
2777 goto bail;
Tharun Kumar Merugu0d0b69e2018-09-14 22:30:58 +05302778
2779 if (ud->flags == ADSP_MMAP_HEAP_ADDR ||
2780 ud->flags == ADSP_MMAP_REMOTE_HEAP_ADDR)
2781 va_to_dsp = 0;
2782 else
2783 va_to_dsp = (uintptr_t)map->va;
2784 VERIFY(err, 0 == fastrpc_mmap_on_dsp(fl, ud->flags, va_to_dsp,
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302785 map->phys, map->size, &raddr));
2786 if (err)
2787 goto bail;
2788 map->raddr = raddr;
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302789 }
Mohammed Nayeem Ur Rahman3ac5d322018-09-24 13:54:08 +05302790 ud->vaddrout = raddr;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002791 bail:
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302792 if (err && map) {
2793 mutex_lock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302794 fastrpc_mmap_free(map, 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302795 mutex_unlock(&fl->fl_map_mutex);
2796 }
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302797 mutex_unlock(&fl->map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002798 return err;
2799}
2800
2801static void fastrpc_channel_close(struct kref *kref)
2802{
2803 struct fastrpc_apps *me = &gfa;
2804 struct fastrpc_channel_ctx *ctx;
2805 int cid;
2806
2807 ctx = container_of(kref, struct fastrpc_channel_ctx, kref);
2808 cid = ctx - &gcinfo[0];
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05302809 if (!me->glink)
2810 smd_close(ctx->chan);
2811 else
2812 fastrpc_glink_close(ctx->chan, cid);
c_mtharue1a5ce12017-10-13 20:47:09 +05302813 ctx->chan = NULL;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302814 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002815 pr_info("'closed /dev/%s c %d %d'\n", gcinfo[cid].name,
2816 MAJOR(me->dev_no), cid);
2817}
2818
2819static void fastrpc_context_list_dtor(struct fastrpc_file *fl);
2820
2821static int fastrpc_session_alloc_locked(struct fastrpc_channel_ctx *chan,
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302822 int secure, int sharedcb, struct fastrpc_session_ctx **session)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002823{
2824 struct fastrpc_apps *me = &gfa;
2825 int idx = 0, err = 0;
2826
2827 if (chan->sesscount) {
2828 for (idx = 0; idx < chan->sesscount; ++idx) {
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302829 if ((sharedcb && chan->session[idx].smmu.sharedcb) ||
2830 (!chan->session[idx].used &&
2831 chan->session[idx].smmu.secure
2832 == secure && !sharedcb)) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002833 chan->session[idx].used = 1;
2834 break;
2835 }
2836 }
2837 VERIFY(err, idx < chan->sesscount);
2838 if (err)
2839 goto bail;
2840 chan->session[idx].smmu.faults = 0;
2841 } else {
2842 VERIFY(err, me->dev != NULL);
2843 if (err)
2844 goto bail;
2845 chan->session[0].dev = me->dev;
c_mtharue1a5ce12017-10-13 20:47:09 +05302846 chan->session[0].smmu.dev = me->dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002847 }
2848
2849 *session = &chan->session[idx];
2850 bail:
2851 return err;
2852}
2853
c_mtharue1a5ce12017-10-13 20:47:09 +05302854static bool fastrpc_glink_notify_rx_intent_req(void *h, const void *priv,
2855 size_t size)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002856{
2857 if (glink_queue_rx_intent(h, NULL, size))
2858 return false;
2859 return true;
2860}
2861
c_mtharue1a5ce12017-10-13 20:47:09 +05302862static void fastrpc_glink_notify_tx_done(void *handle, const void *priv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002863 const void *pkt_priv, const void *ptr)
2864{
2865}
2866
c_mtharue1a5ce12017-10-13 20:47:09 +05302867static void fastrpc_glink_notify_rx(void *handle, const void *priv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002868 const void *pkt_priv, const void *ptr, size_t size)
2869{
2870 struct smq_invoke_rsp *rsp = (struct smq_invoke_rsp *)ptr;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05302871 struct fastrpc_apps *me = &gfa;
2872 uint32_t index;
c_mtharufdac6892017-10-12 13:09:01 +05302873 int err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002874
c_mtharufdac6892017-10-12 13:09:01 +05302875 VERIFY(err, (rsp && size >= sizeof(*rsp)));
2876 if (err)
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302877 goto bail;
2878
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05302879 index = (uint32_t)((rsp->ctx & FASTRPC_CTXID_MASK) >> 4);
2880 VERIFY(err, index < FASTRPC_CTX_MAX);
c_mtharufdac6892017-10-12 13:09:01 +05302881 if (err)
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302882 goto bail;
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302883
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05302884 VERIFY(err, !IS_ERR_OR_NULL(me->ctxtable[index]));
2885 if (err)
2886 goto bail;
2887
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302888 VERIFY(err, ((me->ctxtable[index]->ctxid == (rsp->ctx & ~3)) &&
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05302889 me->ctxtable[index]->magic == FASTRPC_CTX_MAGIC));
2890 if (err)
2891 goto bail;
2892
Mohammed Nayeem Ur Rahman32ba95d2019-07-26 17:31:37 +05302893 me->ctxtable[index]->handle = handle;
2894 me->ctxtable[index]->ptr = ptr;
2895
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05302896 context_notify_user(me->ctxtable[index], rsp->retval);
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302897bail:
c_mtharufdac6892017-10-12 13:09:01 +05302898 if (err)
2899 pr_err("adsprpc: invalid response or context\n");
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002900}
2901
c_mtharue1a5ce12017-10-13 20:47:09 +05302902static void fastrpc_glink_notify_state(void *handle, const void *priv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002903 unsigned int event)
2904{
2905 struct fastrpc_apps *me = &gfa;
2906 int cid = (int)(uintptr_t)priv;
2907 struct fastrpc_glink_info *link;
2908
2909 if (cid < 0 || cid >= NUM_CHANNELS)
2910 return;
2911 link = &me->channel[cid].link;
2912 switch (event) {
2913 case GLINK_CONNECTED:
2914 link->port_state = FASTRPC_LINK_CONNECTED;
Tharun Kumar Merugu53a8ec92017-07-14 15:52:49 +05302915 complete(&me->channel[cid].workport);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002916 break;
2917 case GLINK_LOCAL_DISCONNECTED:
2918 link->port_state = FASTRPC_LINK_DISCONNECTED;
2919 break;
2920 case GLINK_REMOTE_DISCONNECTED:
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002921 break;
2922 default:
2923 break;
2924 }
2925}
2926
2927static int fastrpc_session_alloc(struct fastrpc_channel_ctx *chan, int secure,
2928 struct fastrpc_session_ctx **session)
2929{
2930 int err = 0;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302931 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002932
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302933 mutex_lock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002934 if (!*session)
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302935 err = fastrpc_session_alloc_locked(chan, secure, 0, session);
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302936 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002937 return err;
2938}
2939
2940static void fastrpc_session_free(struct fastrpc_channel_ctx *chan,
2941 struct fastrpc_session_ctx *session)
2942{
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302943 struct fastrpc_apps *me = &gfa;
2944
2945 mutex_lock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002946 session->used = 0;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302947 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002948}
2949
2950static int fastrpc_file_free(struct fastrpc_file *fl)
2951{
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302952 struct hlist_node *n = NULL;
Tharun Kumar Merugu3e966762018-04-04 10:56:44 +05302953 struct fastrpc_mmap *map = NULL, *lmap = NULL;
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302954 struct fastrpc_perf *perf = NULL, *fperf = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002955 int cid;
2956
2957 if (!fl)
2958 return 0;
2959 cid = fl->cid;
2960
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302961 (void)fastrpc_release_current_dsp_process(fl);
2962
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002963 spin_lock(&fl->apps->hlock);
2964 hlist_del_init(&fl->hn);
2965 spin_unlock(&fl->apps->hlock);
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05302966 kfree(fl->debug_buf);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002967
Sathish Ambleyd7fbcbb2017-03-08 10:55:48 -08002968 if (!fl->sctx) {
2969 kfree(fl);
2970 return 0;
2971 }
tharun kumar9f899ea2017-07-03 17:07:03 +05302972 spin_lock(&fl->hlock);
2973 fl->file_close = 1;
2974 spin_unlock(&fl->hlock);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302975 if (!IS_ERR_OR_NULL(fl->init_mem))
2976 fastrpc_buf_free(fl->init_mem, 0);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002977 fastrpc_context_list_dtor(fl);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302978 fastrpc_cached_buf_list_free(fl);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302979 mutex_lock(&fl->fl_map_mutex);
Tharun Kumar Merugu3e966762018-04-04 10:56:44 +05302980 do {
2981 lmap = NULL;
2982 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
2983 hlist_del_init(&map->hn);
2984 lmap = map;
2985 break;
2986 }
2987 fastrpc_mmap_free(lmap, 1);
2988 } while (lmap);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302989 mutex_unlock(&fl->fl_map_mutex);
Tharun Kumar Merugu35173342018-02-08 16:13:17 +05302990 if (fl->refcount && (fl->ssrcount == fl->apps->channel[cid].ssrcount))
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002991 kref_put_mutex(&fl->apps->channel[cid].kref,
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302992 fastrpc_channel_close, &fl->apps->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002993 if (fl->sctx)
2994 fastrpc_session_free(&fl->apps->channel[cid], fl->sctx);
2995 if (fl->secsctx)
2996 fastrpc_session_free(&fl->apps->channel[cid], fl->secsctx);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302997
2998 mutex_lock(&fl->perf_mutex);
2999 do {
3000 struct hlist_node *pn = NULL;
3001
3002 fperf = NULL;
3003 hlist_for_each_entry_safe(perf, pn, &fl->perf, hn) {
3004 hlist_del_init(&perf->hn);
3005 fperf = perf;
3006 break;
3007 }
3008 kfree(fperf);
3009 } while (fperf);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303010 fastrpc_remote_buf_list_free(fl);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05303011 mutex_unlock(&fl->perf_mutex);
3012 mutex_destroy(&fl->perf_mutex);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05303013 mutex_destroy(&fl->fl_map_mutex);
Tharun Kumar Merugu8714e642018-05-17 15:21:08 +05303014 mutex_destroy(&fl->map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003015 kfree(fl);
3016 return 0;
3017}
3018
3019static int fastrpc_device_release(struct inode *inode, struct file *file)
3020{
3021 struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data;
3022
3023 if (fl) {
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303024 if (fl->qos_request && pm_qos_request_active(&fl->pm_qos_req))
3025 pm_qos_remove_request(&fl->pm_qos_req);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003026 if (fl->debugfs_file != NULL)
3027 debugfs_remove(fl->debugfs_file);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003028 fastrpc_file_free(fl);
c_mtharue1a5ce12017-10-13 20:47:09 +05303029 file->private_data = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003030 }
3031 return 0;
3032}
3033
3034static void fastrpc_link_state_handler(struct glink_link_state_cb_info *cb_info,
3035 void *priv)
3036{
3037 struct fastrpc_apps *me = &gfa;
3038 int cid = (int)((uintptr_t)priv);
3039 struct fastrpc_glink_info *link;
3040
3041 if (cid < 0 || cid >= NUM_CHANNELS)
3042 return;
3043
3044 link = &me->channel[cid].link;
3045 switch (cb_info->link_state) {
3046 case GLINK_LINK_STATE_UP:
3047 link->link_state = FASTRPC_LINK_STATE_UP;
3048 complete(&me->channel[cid].work);
3049 break;
3050 case GLINK_LINK_STATE_DOWN:
3051 link->link_state = FASTRPC_LINK_STATE_DOWN;
3052 break;
3053 default:
3054 pr_err("adsprpc: unknown link state %d\n", cb_info->link_state);
3055 break;
3056 }
3057}
3058
3059static int fastrpc_glink_register(int cid, struct fastrpc_apps *me)
3060{
3061 int err = 0;
3062 struct fastrpc_glink_info *link;
3063
3064 VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS));
3065 if (err)
3066 goto bail;
3067
3068 link = &me->channel[cid].link;
3069 if (link->link_notify_handle != NULL)
3070 goto bail;
3071
3072 link->link_info.glink_link_state_notif_cb = fastrpc_link_state_handler;
3073 link->link_notify_handle = glink_register_link_state_cb(
3074 &link->link_info,
3075 (void *)((uintptr_t)cid));
3076 VERIFY(err, !IS_ERR_OR_NULL(me->channel[cid].link.link_notify_handle));
3077 if (err) {
3078 link->link_notify_handle = NULL;
3079 goto bail;
3080 }
3081 VERIFY(err, wait_for_completion_timeout(&me->channel[cid].work,
3082 RPC_TIMEOUT));
3083bail:
3084 return err;
3085}
3086
3087static void fastrpc_glink_close(void *chan, int cid)
3088{
3089 int err = 0;
3090 struct fastrpc_glink_info *link;
3091
3092 VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS));
3093 if (err)
3094 return;
3095 link = &gfa.channel[cid].link;
3096
c_mtharu314a4202017-11-15 22:09:17 +05303097 if (link->port_state == FASTRPC_LINK_CONNECTED ||
3098 link->port_state == FASTRPC_LINK_REMOTE_DISCONNECTING) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003099 link->port_state = FASTRPC_LINK_DISCONNECTING;
3100 glink_close(chan);
3101 }
3102}
3103
3104static int fastrpc_glink_open(int cid)
3105{
3106 int err = 0;
3107 void *handle = NULL;
3108 struct fastrpc_apps *me = &gfa;
3109 struct glink_open_config *cfg;
3110 struct fastrpc_glink_info *link;
3111
3112 VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS));
3113 if (err)
3114 goto bail;
3115 link = &me->channel[cid].link;
3116 cfg = &me->channel[cid].link.cfg;
3117 VERIFY(err, (link->link_state == FASTRPC_LINK_STATE_UP));
3118 if (err)
3119 goto bail;
3120
Tharun Kumar Meruguca0db5262017-05-10 12:53:12 +05303121 VERIFY(err, (link->port_state == FASTRPC_LINK_DISCONNECTED));
3122 if (err)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003123 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003124
3125 link->port_state = FASTRPC_LINK_CONNECTING;
3126 cfg->priv = (void *)(uintptr_t)cid;
3127 cfg->edge = gcinfo[cid].link.link_info.edge;
3128 cfg->transport = gcinfo[cid].link.link_info.transport;
3129 cfg->name = FASTRPC_GLINK_GUID;
3130 cfg->notify_rx = fastrpc_glink_notify_rx;
3131 cfg->notify_tx_done = fastrpc_glink_notify_tx_done;
3132 cfg->notify_state = fastrpc_glink_notify_state;
3133 cfg->notify_rx_intent_req = fastrpc_glink_notify_rx_intent_req;
3134 handle = glink_open(cfg);
3135 VERIFY(err, !IS_ERR_OR_NULL(handle));
c_mtharu6e1d26b2017-10-09 16:05:24 +05303136 if (err) {
3137 if (link->port_state == FASTRPC_LINK_CONNECTING)
3138 link->port_state = FASTRPC_LINK_DISCONNECTED;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003139 goto bail;
c_mtharu6e1d26b2017-10-09 16:05:24 +05303140 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003141 me->channel[cid].chan = handle;
3142bail:
3143 return err;
3144}
3145
Sathish Ambley1ca68232017-01-19 10:32:55 -08003146static int fastrpc_debugfs_open(struct inode *inode, struct file *filp)
3147{
3148 filp->private_data = inode->i_private;
3149 return 0;
3150}
3151
3152static ssize_t fastrpc_debugfs_read(struct file *filp, char __user *buffer,
3153 size_t count, loff_t *position)
3154{
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303155 struct fastrpc_apps *me = &gfa;
Sathish Ambley1ca68232017-01-19 10:32:55 -08003156 struct fastrpc_file *fl = filp->private_data;
3157 struct hlist_node *n;
c_mtharue1a5ce12017-10-13 20:47:09 +05303158 struct fastrpc_buf *buf = NULL;
3159 struct fastrpc_mmap *map = NULL;
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303160 struct fastrpc_mmap *gmaps = NULL;
c_mtharue1a5ce12017-10-13 20:47:09 +05303161 struct smq_invoke_ctx *ictx = NULL;
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303162 struct fastrpc_channel_ctx *chan = NULL;
Sathish Ambley1ca68232017-01-19 10:32:55 -08003163 unsigned int len = 0;
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303164 int i, j, sess_used = 0, ret = 0;
Sathish Ambley1ca68232017-01-19 10:32:55 -08003165 char *fileinfo = NULL;
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303166 char single_line[UL_SIZE] = "----------------";
3167 char title[UL_SIZE] = "=========================";
Sathish Ambley1ca68232017-01-19 10:32:55 -08003168
3169 fileinfo = kzalloc(DEBUGFS_SIZE, GFP_KERNEL);
3170 if (!fileinfo)
3171 goto bail;
3172 if (fl == NULL) {
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303173 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3174 "\n%s %s %s\n", title, " CHANNEL INFO ", title);
3175 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3176 "%-8s|%-9s|%-9s|%-14s|%-9s|%-13s\n",
3177 "susbsys", "refcount", "sesscount", "issubsystemup",
3178 "ssrcount", "session_used");
3179 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3180 "-%s%s%s%s-\n", single_line, single_line,
3181 single_line, single_line);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003182 for (i = 0; i < NUM_CHANNELS; i++) {
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303183 sess_used = 0;
Sathish Ambley1ca68232017-01-19 10:32:55 -08003184 chan = &gcinfo[i];
3185 len += scnprintf(fileinfo + len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303186 DEBUGFS_SIZE - len, "%-8s", chan->subsys);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003187 len += scnprintf(fileinfo + len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303188 DEBUGFS_SIZE - len, "|%-9d",
3189 chan->kref.refcount.counter);
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05303190 len += scnprintf(fileinfo + len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303191 DEBUGFS_SIZE - len, "|%-9d",
3192 chan->sesscount);
3193 len += scnprintf(fileinfo + len,
3194 DEBUGFS_SIZE - len, "|%-14d",
3195 chan->issubsystemup);
3196 len += scnprintf(fileinfo + len,
3197 DEBUGFS_SIZE - len, "|%-9d",
3198 chan->ssrcount);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003199 for (j = 0; j < chan->sesscount; j++) {
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303200 sess_used += chan->session[j].used;
3201 }
3202 len += scnprintf(fileinfo + len,
3203 DEBUGFS_SIZE - len, "|%-13d\n", sess_used);
3204
3205 }
3206 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3207 "\n%s%s%s\n", "=============",
3208 " CMA HEAP ", "==============");
3209 len += scnprintf(fileinfo + len,
3210 DEBUGFS_SIZE - len, "%-20s|%-20s\n", "addr", "size");
3211 len += scnprintf(fileinfo + len,
3212 DEBUGFS_SIZE - len, "--%s%s---\n",
3213 single_line, single_line);
3214 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3215 "0x%-18llX", me->range.addr);
3216 len += scnprintf(fileinfo + len,
3217 DEBUGFS_SIZE - len, "|0x%-18llX\n", me->range.size);
3218 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3219 "\n==========%s %s %s===========\n",
3220 title, " GMAPS ", title);
3221 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3222 "%-20s|%-20s|%-20s|%-20s\n",
3223 "fd", "phys", "size", "va");
3224 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3225 "%s%s%s%s%s\n", single_line, single_line,
3226 single_line, single_line, single_line);
3227 hlist_for_each_entry_safe(gmaps, n, &me->maps, hn) {
3228 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3229 "%-20d|0x%-18llX|0x%-18X|0x%-20lX\n\n",
3230 gmaps->fd, gmaps->phys,
3231 (uint32_t)gmaps->size,
3232 gmaps->va);
3233 }
3234 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3235 "%-20s|%-20s|%-20s|%-20s\n",
3236 "len", "refs", "raddr", "flags");
3237 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3238 "%s%s%s%s%s\n", single_line, single_line,
3239 single_line, single_line, single_line);
3240 hlist_for_each_entry_safe(gmaps, n, &me->maps, hn) {
3241 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3242 "0x%-18X|%-20d|%-20lu|%-20u\n",
3243 (uint32_t)gmaps->len, gmaps->refs,
3244 gmaps->raddr, gmaps->flags);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003245 }
3246 } else {
3247 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303248 "\n%s %13s %d\n", "cid", ":", fl->cid);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003249 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303250 "%s %12s %d\n", "tgid", ":", fl->tgid);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003251 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303252 "%s %7s %d\n", "sessionid", ":", fl->sessionid);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003253 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303254 "%s %8s %d\n", "ssrcount", ":", fl->ssrcount);
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05303255 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303256 "%s %8s %d\n", "refcount", ":", fl->refcount);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003257 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303258 "%s %14s %d\n", "pd", ":", fl->pd);
3259 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3260 "%s %9s %s\n", "spdname", ":", fl->spdname);
3261 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3262 "%s %6s %d\n", "file_close", ":", fl->file_close);
3263 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3264 "%s %8s %d\n", "sharedcb", ":", fl->sharedcb);
3265 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3266 "%s %9s %d\n", "profile", ":", fl->profile);
3267 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3268 "%s %3s %d\n", "smmu.coherent", ":",
3269 fl->sctx->smmu.coherent);
3270 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3271 "%s %4s %d\n", "smmu.enabled", ":",
3272 fl->sctx->smmu.enabled);
3273 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3274 "%s %9s %d\n", "smmu.cb", ":", fl->sctx->smmu.cb);
3275 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3276 "%s %5s %d\n", "smmu.secure", ":",
3277 fl->sctx->smmu.secure);
3278 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3279 "%s %5s %d\n", "smmu.faults", ":",
3280 fl->sctx->smmu.faults);
3281 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3282 "%s %s %d\n", "link.link_state",
3283 ":", *&me->channel[fl->cid].link.link_state);
3284
3285 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3286 "\n=======%s %s %s======\n", title,
3287 " LIST OF MAPS ", title);
3288
3289 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3290 "%-20s|%-20s|%-20s\n", "va", "phys", "size");
3291 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3292 "%s%s%s%s%s\n",
3293 single_line, single_line, single_line,
3294 single_line, single_line);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003295 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303296 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3297 "0x%-20lX|0x%-20llX|0x%-20zu\n\n",
3298 map->va, map->phys,
3299 map->size);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003300 }
3301 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303302 "%-20s|%-20s|%-20s|%-20s\n",
3303 "len", "refs",
3304 "raddr", "uncached");
3305 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3306 "%s%s%s%s%s\n",
3307 single_line, single_line, single_line,
3308 single_line, single_line);
3309 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
3310 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3311 "%-20zu|%-20d|0x%-20lX|%-20d\n\n",
3312 map->len, map->refs, map->raddr,
3313 map->uncached);
3314 }
3315 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3316 "%-20s|%-20s\n", "secure", "attr");
3317 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3318 "%s%s%s%s%s\n",
3319 single_line, single_line, single_line,
3320 single_line, single_line);
3321 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
3322 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3323 "%-20d|0x%-20lX\n\n",
3324 map->secure, map->attr);
3325 }
3326 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303327 "%s %d\n\n",
3328 "KERNEL MEMORY ALLOCATION:", 1);
3329 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303330 "\n======%s %s %s======\n", title,
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303331 " LIST OF CACHED BUFS ", title);
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303332 spin_lock(&fl->hlock);
3333 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303334 "%-19s|%-19s|%-19s|%-19s\n",
3335 "virt", "phys", "size", "dma_attr");
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303336 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3337 "%s%s%s%s%s\n", single_line, single_line,
3338 single_line, single_line, single_line);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303339 hlist_for_each_entry_safe(buf, n, &fl->cached_bufs, hn) {
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303340 len += scnprintf(fileinfo + len,
3341 DEBUGFS_SIZE - len,
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303342 "0x%-17p|0x%-17llX|%-19zu|0x%-17lX\n",
3343 buf->virt, (uint64_t)buf->phys, buf->size,
3344 buf->dma_attr);
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303345 }
3346 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3347 "\n%s %s %s\n", title,
3348 " LIST OF PENDING SMQCONTEXTS ", title);
3349
3350 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3351 "%-20s|%-10s|%-10s|%-10s|%-20s\n",
3352 "sc", "pid", "tgid", "used", "ctxid");
3353 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3354 "%s%s%s%s%s\n", single_line, single_line,
3355 single_line, single_line, single_line);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003356 hlist_for_each_entry_safe(ictx, n, &fl->clst.pending, hn) {
3357 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303358 "0x%-18X|%-10d|%-10d|%-10zu|0x%-20llX\n\n",
3359 ictx->sc, ictx->pid, ictx->tgid,
3360 ictx->used, ictx->ctxid);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003361 }
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303362
Sathish Ambley1ca68232017-01-19 10:32:55 -08003363 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303364 "\n%s %s %s\n", title,
3365 " LIST OF INTERRUPTED SMQCONTEXTS ", title);
3366
3367 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3368 "%-20s|%-10s|%-10s|%-10s|%-20s\n",
3369 "sc", "pid", "tgid", "used", "ctxid");
3370 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3371 "%s%s%s%s%s\n", single_line, single_line,
3372 single_line, single_line, single_line);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003373 hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) {
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303374 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3375 "%-20u|%-20d|%-20d|%-20zu|0x%-20llX\n\n",
3376 ictx->sc, ictx->pid, ictx->tgid,
3377 ictx->used, ictx->ctxid);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003378 }
3379 spin_unlock(&fl->hlock);
3380 }
3381 if (len > DEBUGFS_SIZE)
3382 len = DEBUGFS_SIZE;
3383 ret = simple_read_from_buffer(buffer, count, position, fileinfo, len);
3384 kfree(fileinfo);
3385bail:
3386 return ret;
3387}
3388
3389static const struct file_operations debugfs_fops = {
3390 .open = fastrpc_debugfs_open,
3391 .read = fastrpc_debugfs_read,
3392};
Sathish Ambley36849af2017-02-02 09:35:55 -08003393static int fastrpc_channel_open(struct fastrpc_file *fl)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003394{
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003395 struct fastrpc_apps *me = &gfa;
Tharun Kumar Meruguc42c6e22018-05-29 15:50:46 +05303396 int cid, ii, err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003397
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303398 mutex_lock(&me->smd_mutex);
3399
Sathish Ambley36849af2017-02-02 09:35:55 -08003400 VERIFY(err, fl && fl->sctx);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003401 if (err)
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303402 goto bail;
Sathish Ambley36849af2017-02-02 09:35:55 -08003403 cid = fl->cid;
c_mtharu314a4202017-11-15 22:09:17 +05303404 VERIFY(err, cid >= 0 && cid < NUM_CHANNELS);
3405 if (err)
3406 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05303407 if (me->channel[cid].ssrcount !=
3408 me->channel[cid].prevssrcount) {
3409 if (!me->channel[cid].issubsystemup) {
3410 VERIFY(err, 0);
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303411 if (err) {
3412 err = -ENOTCONN;
c_mtharue1a5ce12017-10-13 20:47:09 +05303413 goto bail;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303414 }
c_mtharue1a5ce12017-10-13 20:47:09 +05303415 }
3416 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003417 fl->ssrcount = me->channel[cid].ssrcount;
Tharun Kumar Merugu35173342018-02-08 16:13:17 +05303418 fl->refcount = 1;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003419 if ((kref_get_unless_zero(&me->channel[cid].kref) == 0) ||
c_mtharue1a5ce12017-10-13 20:47:09 +05303420 (me->channel[cid].chan == NULL)) {
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05303421 if (me->glink) {
3422 VERIFY(err, 0 == fastrpc_glink_register(cid, me));
3423 if (err)
3424 goto bail;
3425 VERIFY(err, 0 == fastrpc_glink_open(cid));
3426 } else {
3427 VERIFY(err, !smd_named_open_on_edge(FASTRPC_SMD_GUID,
3428 gcinfo[cid].channel,
3429 (smd_channel_t **)&me->channel[cid].chan,
3430 (void *)(uintptr_t)cid,
3431 smd_event_handler));
3432 }
Tharun Kumar Merugu53a8ec92017-07-14 15:52:49 +05303433 VERIFY(err,
3434 wait_for_completion_timeout(&me->channel[cid].workport,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003435 RPC_TIMEOUT));
3436 if (err) {
c_mtharue1a5ce12017-10-13 20:47:09 +05303437 me->channel[cid].chan = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003438 goto bail;
3439 }
3440 kref_init(&me->channel[cid].kref);
3441 pr_info("'opened /dev/%s c %d %d'\n", gcinfo[cid].name,
3442 MAJOR(me->dev_no), cid);
Tharun Kumar Meruguc42c6e22018-05-29 15:50:46 +05303443
3444 for (ii = 0; ii < FASTRPC_GLINK_INTENT_NUM && me->glink; ii++)
3445 glink_queue_rx_intent(me->channel[cid].chan, NULL,
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05303446 FASTRPC_GLINK_INTENT_LEN);
Tharun Kumar Meruguc42c6e22018-05-29 15:50:46 +05303447
Tharun Kumar Merugud86fc8c2018-01-04 16:35:31 +05303448 if (cid == 0 && me->channel[cid].ssrcount !=
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003449 me->channel[cid].prevssrcount) {
c_mtharue1a5ce12017-10-13 20:47:09 +05303450 if (fastrpc_mmap_remove_ssr(fl))
3451 pr_err("ADSPRPC: SSR: Failed to unmap remote heap\n");
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003452 me->channel[cid].prevssrcount =
3453 me->channel[cid].ssrcount;
3454 }
3455 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003456
3457bail:
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303458 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003459 return err;
3460}
3461
Sathish Ambley36849af2017-02-02 09:35:55 -08003462static int fastrpc_device_open(struct inode *inode, struct file *filp)
3463{
3464 int err = 0;
Sathish Ambley567012b2017-03-06 11:55:04 -08003465 struct dentry *debugfs_file;
c_mtharue1a5ce12017-10-13 20:47:09 +05303466 struct fastrpc_file *fl = NULL;
Sathish Ambley36849af2017-02-02 09:35:55 -08003467 struct fastrpc_apps *me = &gfa;
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303468 char strpid[PID_SIZE];
3469 int buf_size = 0;
Sathish Ambley36849af2017-02-02 09:35:55 -08003470
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05303471 /*
3472 * Indicates the device node opened
3473 * MINOR_NUM_DEV or MINOR_NUM_SECURE_DEV
3474 */
3475 int dev_minor = MINOR(inode->i_rdev);
3476
3477 VERIFY(err, ((dev_minor == MINOR_NUM_DEV) ||
3478 (dev_minor == MINOR_NUM_SECURE_DEV)));
3479 if (err) {
3480 pr_err("adsprpc: Invalid dev minor num %d\n", dev_minor);
3481 return err;
3482 }
3483
c_mtharue1a5ce12017-10-13 20:47:09 +05303484 VERIFY(err, NULL != (fl = kzalloc(sizeof(*fl), GFP_KERNEL)));
Sathish Ambley36849af2017-02-02 09:35:55 -08003485 if (err)
3486 return err;
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303487 snprintf(strpid, PID_SIZE, "%d", current->pid);
Mohammed Nayeem Ur Rahman2d65b4a2018-10-10 16:34:37 +05303488 buf_size = strlen(current->comm) + strlen("_") + strlen(strpid) + 1;
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303489 fl->debug_buf = kzalloc(buf_size, GFP_KERNEL);
3490 snprintf(fl->debug_buf, UL_SIZE, "%.10s%s%d",
3491 current->comm, "_", current->pid);
3492 debugfs_file = debugfs_create_file(fl->debug_buf, 0644,
3493 debugfs_root, fl, &debugfs_fops);
3494
Sathish Ambley36849af2017-02-02 09:35:55 -08003495 context_list_ctor(&fl->clst);
3496 spin_lock_init(&fl->hlock);
3497 INIT_HLIST_HEAD(&fl->maps);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05303498 INIT_HLIST_HEAD(&fl->perf);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303499 INIT_HLIST_HEAD(&fl->cached_bufs);
3500 INIT_HLIST_HEAD(&fl->remote_bufs);
Sathish Ambley36849af2017-02-02 09:35:55 -08003501 INIT_HLIST_NODE(&fl->hn);
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05303502 fl->sessionid = 0;
Sathish Ambley36849af2017-02-02 09:35:55 -08003503 fl->tgid = current->tgid;
3504 fl->apps = me;
3505 fl->mode = FASTRPC_MODE_SERIAL;
3506 fl->cid = -1;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05303507 fl->dev_minor = dev_minor;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303508 fl->init_mem = NULL;
Sathish Ambley567012b2017-03-06 11:55:04 -08003509 if (debugfs_file != NULL)
3510 fl->debugfs_file = debugfs_file;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303511 fl->qos_request = 0;
Tharun Kumar Merugu35173342018-02-08 16:13:17 +05303512 fl->refcount = 0;
Sathish Ambley36849af2017-02-02 09:35:55 -08003513 filp->private_data = fl;
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05303514 mutex_init(&fl->map_mutex);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05303515 mutex_init(&fl->fl_map_mutex);
Sathish Ambley36849af2017-02-02 09:35:55 -08003516 spin_lock(&me->hlock);
3517 hlist_add_head(&fl->hn, &me->drivers);
3518 spin_unlock(&me->hlock);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05303519 mutex_init(&fl->perf_mutex);
Sathish Ambley36849af2017-02-02 09:35:55 -08003520 return 0;
3521}
3522
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003523static int fastrpc_get_info(struct fastrpc_file *fl, uint32_t *info)
3524{
3525 int err = 0;
Sathish Ambley36849af2017-02-02 09:35:55 -08003526 uint32_t cid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003527
c_mtharue1a5ce12017-10-13 20:47:09 +05303528 VERIFY(err, fl != NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003529 if (err)
3530 goto bail;
Sathish Ambley36849af2017-02-02 09:35:55 -08003531 if (fl->cid == -1) {
3532 cid = *info;
3533 VERIFY(err, cid < NUM_CHANNELS);
3534 if (err)
3535 goto bail;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05303536 /* Check to see if the device node is non-secure */
zhaochenfc798572018-08-17 15:32:37 +08003537 if (fl->dev_minor == MINOR_NUM_DEV &&
3538 fl->apps->secure_flag == true) {
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05303539 /*
3540 * For non secure device node check and make sure that
3541 * the channel allows non-secure access
3542 * If not, bail. Session will not start.
3543 * cid will remain -1 and client will not be able to
3544 * invoke any other methods without failure
3545 */
3546 if (fl->apps->channel[cid].secure == SECURE_CHANNEL) {
3547 err = -EPERM;
3548 pr_err("adsprpc: GetInfo failed dev %d, cid %d, secure %d\n",
3549 fl->dev_minor, cid,
3550 fl->apps->channel[cid].secure);
3551 goto bail;
3552 }
3553 }
Sathish Ambley36849af2017-02-02 09:35:55 -08003554 fl->cid = cid;
3555 fl->ssrcount = fl->apps->channel[cid].ssrcount;
3556 VERIFY(err, !fastrpc_session_alloc_locked(
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303557 &fl->apps->channel[cid], 0, fl->sharedcb, &fl->sctx));
Sathish Ambley36849af2017-02-02 09:35:55 -08003558 if (err)
3559 goto bail;
3560 }
Tharun Kumar Merugu80be7d62017-08-02 11:03:22 +05303561 VERIFY(err, fl->sctx != NULL);
3562 if (err)
3563 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003564 *info = (fl->sctx->smmu.enabled ? 1 : 0);
3565bail:
3566 return err;
3567}
3568
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303569static int fastrpc_internal_control(struct fastrpc_file *fl,
3570 struct fastrpc_ioctl_control *cp)
3571{
Mohammed Nayeem Ur Rahman18d633f2019-05-28 15:11:40 +05303572 struct fastrpc_apps *me = &gfa;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303573 int err = 0;
3574 int latency;
3575
3576 VERIFY(err, !IS_ERR_OR_NULL(fl) && !IS_ERR_OR_NULL(fl->apps));
3577 if (err)
3578 goto bail;
3579 VERIFY(err, !IS_ERR_OR_NULL(cp));
3580 if (err)
3581 goto bail;
3582
3583 switch (cp->req) {
3584 case FASTRPC_CONTROL_LATENCY:
3585 latency = cp->lp.enable == FASTRPC_LATENCY_CTRL_ENB ?
3586 fl->apps->latency : PM_QOS_DEFAULT_VALUE;
3587 VERIFY(err, latency != 0);
3588 if (err)
3589 goto bail;
3590 if (!fl->qos_request) {
3591 pm_qos_add_request(&fl->pm_qos_req,
3592 PM_QOS_CPU_DMA_LATENCY, latency);
3593 fl->qos_request = 1;
3594 } else
3595 pm_qos_update_request(&fl->pm_qos_req, latency);
3596 break;
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303597 case FASTRPC_CONTROL_SMMU:
Mohammed Nayeem Ur Rahman18d633f2019-05-28 15:11:40 +05303598 if (!me->legacy)
3599 fl->sharedcb = cp->smmu.sharedcb;
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303600 break;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303601 case FASTRPC_CONTROL_KALLOC:
3602 cp->kalloc.kalloc_support = 1;
3603 break;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303604 default:
3605 err = -ENOTTY;
3606 break;
3607 }
3608bail:
3609 return err;
3610}
3611
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003612static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num,
3613 unsigned long ioctl_param)
3614{
3615 union {
Sathish Ambleybae51902017-07-03 15:00:49 -07003616 struct fastrpc_ioctl_invoke_crc inv;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003617 struct fastrpc_ioctl_mmap mmap;
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05303618 struct fastrpc_ioctl_mmap_64 mmap64;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003619 struct fastrpc_ioctl_munmap munmap;
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05303620 struct fastrpc_ioctl_munmap_64 munmap64;
c_mtharu7bd6a422017-10-17 18:15:37 +05303621 struct fastrpc_ioctl_munmap_fd munmap_fd;
Sathish Ambleyd6300c32017-01-18 09:50:43 -08003622 struct fastrpc_ioctl_init_attrs init;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003623 struct fastrpc_ioctl_perf perf;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303624 struct fastrpc_ioctl_control cp;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003625 } p;
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05303626 union {
3627 struct fastrpc_ioctl_mmap mmap;
3628 struct fastrpc_ioctl_munmap munmap;
3629 } i;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003630 void *param = (char *)ioctl_param;
3631 struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data;
3632 int size = 0, err = 0;
3633 uint32_t info;
3634
c_mtharue1a5ce12017-10-13 20:47:09 +05303635 p.inv.fds = NULL;
3636 p.inv.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07003637 p.inv.crc = NULL;
tharun kumar9f899ea2017-07-03 17:07:03 +05303638 spin_lock(&fl->hlock);
3639 if (fl->file_close == 1) {
3640 err = EBADF;
3641 pr_warn("ADSPRPC: fastrpc_device_release is happening, So not sending any new requests to DSP");
3642 spin_unlock(&fl->hlock);
3643 goto bail;
3644 }
3645 spin_unlock(&fl->hlock);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003646
3647 switch (ioctl_num) {
3648 case FASTRPC_IOCTL_INVOKE:
3649 size = sizeof(struct fastrpc_ioctl_invoke);
Sathish Ambleybae51902017-07-03 15:00:49 -07003650 /* fall through */
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003651 case FASTRPC_IOCTL_INVOKE_FD:
3652 if (!size)
3653 size = sizeof(struct fastrpc_ioctl_invoke_fd);
3654 /* fall through */
3655 case FASTRPC_IOCTL_INVOKE_ATTRS:
3656 if (!size)
3657 size = sizeof(struct fastrpc_ioctl_invoke_attrs);
Sathish Ambleybae51902017-07-03 15:00:49 -07003658 /* fall through */
3659 case FASTRPC_IOCTL_INVOKE_CRC:
3660 if (!size)
3661 size = sizeof(struct fastrpc_ioctl_invoke_crc);
c_mtharue1a5ce12017-10-13 20:47:09 +05303662 K_COPY_FROM_USER(err, 0, &p.inv, param, size);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003663 if (err)
3664 goto bail;
3665 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, fl->mode,
3666 0, &p.inv)));
3667 if (err)
3668 goto bail;
3669 break;
3670 case FASTRPC_IOCTL_MMAP:
c_mtharue1a5ce12017-10-13 20:47:09 +05303671 K_COPY_FROM_USER(err, 0, &p.mmap, param,
3672 sizeof(p.mmap));
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05303673 if (err)
3674 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003675 VERIFY(err, 0 == (err = fastrpc_internal_mmap(fl, &p.mmap)));
3676 if (err)
3677 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05303678 K_COPY_TO_USER(err, 0, param, &p.mmap, sizeof(p.mmap));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003679 if (err)
3680 goto bail;
3681 break;
3682 case FASTRPC_IOCTL_MUNMAP:
c_mtharue1a5ce12017-10-13 20:47:09 +05303683 K_COPY_FROM_USER(err, 0, &p.munmap, param,
3684 sizeof(p.munmap));
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05303685 if (err)
3686 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003687 VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl,
3688 &p.munmap)));
3689 if (err)
3690 goto bail;
3691 break;
Tharun Kumar Merugu55be90d2018-05-31 11:41:03 +05303692 case FASTRPC_IOCTL_MMAP_64:
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05303693 K_COPY_FROM_USER(err, 0, &p.mmap64, param,
3694 sizeof(p.mmap64));
Tharun Kumar Merugu55be90d2018-05-31 11:41:03 +05303695 if (err)
3696 goto bail;
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05303697 get_fastrpc_ioctl_mmap_64(&p.mmap64, &i.mmap);
3698 VERIFY(err, 0 == (err = fastrpc_internal_mmap(fl, &i.mmap)));
Tharun Kumar Merugu55be90d2018-05-31 11:41:03 +05303699 if (err)
3700 goto bail;
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05303701 put_fastrpc_ioctl_mmap_64(&p.mmap64, &i.mmap);
3702 K_COPY_TO_USER(err, 0, param, &p.mmap64, sizeof(p.mmap64));
Tharun Kumar Merugu55be90d2018-05-31 11:41:03 +05303703 if (err)
3704 goto bail;
3705 break;
3706 case FASTRPC_IOCTL_MUNMAP_64:
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05303707 K_COPY_FROM_USER(err, 0, &p.munmap64, param,
3708 sizeof(p.munmap64));
Tharun Kumar Merugu55be90d2018-05-31 11:41:03 +05303709 if (err)
3710 goto bail;
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05303711 get_fastrpc_ioctl_munmap_64(&p.munmap64, &i.munmap);
Tharun Kumar Merugu55be90d2018-05-31 11:41:03 +05303712 VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl,
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05303713 &i.munmap)));
Tharun Kumar Merugu55be90d2018-05-31 11:41:03 +05303714 if (err)
3715 goto bail;
3716 break;
c_mtharu7bd6a422017-10-17 18:15:37 +05303717 case FASTRPC_IOCTL_MUNMAP_FD:
3718 K_COPY_FROM_USER(err, 0, &p.munmap_fd, param,
3719 sizeof(p.munmap_fd));
3720 if (err)
3721 goto bail;
3722 VERIFY(err, 0 == (err = fastrpc_internal_munmap_fd(fl,
3723 &p.munmap_fd)));
3724 if (err)
3725 goto bail;
3726 break;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003727 case FASTRPC_IOCTL_SETMODE:
3728 switch ((uint32_t)ioctl_param) {
3729 case FASTRPC_MODE_PARALLEL:
3730 case FASTRPC_MODE_SERIAL:
3731 fl->mode = (uint32_t)ioctl_param;
3732 break;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003733 case FASTRPC_MODE_PROFILE:
3734 fl->profile = (uint32_t)ioctl_param;
3735 break;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05303736 case FASTRPC_MODE_SESSION:
3737 fl->sessionid = 1;
3738 fl->tgid |= (1 << SESSION_ID_INDEX);
3739 break;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003740 default:
3741 err = -ENOTTY;
3742 break;
3743 }
3744 break;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003745 case FASTRPC_IOCTL_GETPERF:
c_mtharue1a5ce12017-10-13 20:47:09 +05303746 K_COPY_FROM_USER(err, 0, &p.perf,
3747 param, sizeof(p.perf));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003748 if (err)
3749 goto bail;
3750 p.perf.numkeys = sizeof(struct fastrpc_perf)/sizeof(int64_t);
3751 if (p.perf.keys) {
3752 char *keys = PERF_KEYS;
3753
c_mtharue1a5ce12017-10-13 20:47:09 +05303754 K_COPY_TO_USER(err, 0, (void *)p.perf.keys,
3755 keys, strlen(keys)+1);
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003756 if (err)
3757 goto bail;
3758 }
3759 if (p.perf.data) {
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05303760 struct fastrpc_perf *perf = NULL, *fperf = NULL;
3761 struct hlist_node *n = NULL;
3762
3763 mutex_lock(&fl->perf_mutex);
3764 hlist_for_each_entry_safe(perf, n, &fl->perf, hn) {
3765 if (perf->tid == current->pid) {
3766 fperf = perf;
3767 break;
3768 }
3769 }
3770
3771 mutex_unlock(&fl->perf_mutex);
3772
3773 if (fperf) {
3774 K_COPY_TO_USER(err, 0, (void *)p.perf.data,
3775 fperf, sizeof(*fperf));
3776 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003777 }
c_mtharue1a5ce12017-10-13 20:47:09 +05303778 K_COPY_TO_USER(err, 0, param, &p.perf, sizeof(p.perf));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003779 if (err)
3780 goto bail;
3781 break;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303782 case FASTRPC_IOCTL_CONTROL:
c_mtharue1a5ce12017-10-13 20:47:09 +05303783 K_COPY_FROM_USER(err, 0, &p.cp, param,
3784 sizeof(p.cp));
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303785 if (err)
3786 goto bail;
3787 VERIFY(err, 0 == (err = fastrpc_internal_control(fl, &p.cp)));
3788 if (err)
3789 goto bail;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303790 if (p.cp.req == FASTRPC_CONTROL_KALLOC) {
3791 K_COPY_TO_USER(err, 0, param, &p.cp, sizeof(p.cp));
3792 if (err)
3793 goto bail;
3794 }
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303795 break;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003796 case FASTRPC_IOCTL_GETINFO:
c_mtharue1a5ce12017-10-13 20:47:09 +05303797 K_COPY_FROM_USER(err, 0, &info, param, sizeof(info));
Sathish Ambley36849af2017-02-02 09:35:55 -08003798 if (err)
3799 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003800 VERIFY(err, 0 == (err = fastrpc_get_info(fl, &info)));
3801 if (err)
3802 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05303803 K_COPY_TO_USER(err, 0, param, &info, sizeof(info));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003804 if (err)
3805 goto bail;
3806 break;
3807 case FASTRPC_IOCTL_INIT:
Sathish Ambleyd6300c32017-01-18 09:50:43 -08003808 p.init.attrs = 0;
3809 p.init.siglen = 0;
3810 size = sizeof(struct fastrpc_ioctl_init);
3811 /* fall through */
3812 case FASTRPC_IOCTL_INIT_ATTRS:
3813 if (!size)
3814 size = sizeof(struct fastrpc_ioctl_init_attrs);
c_mtharue1a5ce12017-10-13 20:47:09 +05303815 K_COPY_FROM_USER(err, 0, &p.init, param, size);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003816 if (err)
3817 goto bail;
Tharun Kumar Merugu4ea0eac2017-08-22 11:42:51 +05303818 VERIFY(err, p.init.init.filelen >= 0 &&
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05303819 p.init.init.filelen < INIT_FILELEN_MAX);
3820 if (err)
3821 goto bail;
3822 VERIFY(err, p.init.init.memlen >= 0 &&
3823 p.init.init.memlen < INIT_MEMLEN_MAX);
Tharun Kumar Merugu4ea0eac2017-08-22 11:42:51 +05303824 if (err)
3825 goto bail;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303826 VERIFY(err, 0 == (err = fastrpc_init_process(fl, &p.init)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003827 if (err)
3828 goto bail;
3829 break;
3830
3831 default:
3832 err = -ENOTTY;
3833 pr_info("bad ioctl: %d\n", ioctl_num);
3834 break;
3835 }
3836 bail:
3837 return err;
3838}
3839
3840static int fastrpc_restart_notifier_cb(struct notifier_block *nb,
3841 unsigned long code,
3842 void *data)
3843{
3844 struct fastrpc_apps *me = &gfa;
3845 struct fastrpc_channel_ctx *ctx;
c_mtharue1a5ce12017-10-13 20:47:09 +05303846 struct notif_data *notifdata = data;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003847 int cid;
3848
3849 ctx = container_of(nb, struct fastrpc_channel_ctx, nb);
3850 cid = ctx - &me->channel[0];
3851 if (code == SUBSYS_BEFORE_SHUTDOWN) {
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303852 mutex_lock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003853 ctx->ssrcount++;
c_mtharue1a5ce12017-10-13 20:47:09 +05303854 ctx->issubsystemup = 0;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303855 if (ctx->chan) {
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05303856 if (me->glink)
3857 fastrpc_glink_close(ctx->chan, cid);
3858 else
3859 smd_close(ctx->chan);
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303860 ctx->chan = NULL;
3861 pr_info("'restart notifier: closed /dev/%s c %d %d'\n",
3862 gcinfo[cid].name, MAJOR(me->dev_no), cid);
3863 }
3864 mutex_unlock(&me->smd_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05303865 if (cid == 0)
3866 me->staticpd_flags = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003867 fastrpc_notify_drivers(me, cid);
c_mtharue1a5ce12017-10-13 20:47:09 +05303868 } else if (code == SUBSYS_RAMDUMP_NOTIFICATION) {
3869 if (me->channel[0].remoteheap_ramdump_dev &&
3870 notifdata->enable_ramdump) {
3871 me->channel[0].ramdumpenabled = 1;
3872 }
3873 } else if (code == SUBSYS_AFTER_POWERUP) {
3874 ctx->issubsystemup = 1;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003875 }
3876
3877 return NOTIFY_DONE;
3878}
3879
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303880static int fastrpc_pdr_notifier_cb(struct notifier_block *pdrnb,
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05303881 unsigned long code,
3882 void *data)
3883{
3884 struct fastrpc_apps *me = &gfa;
3885 struct fastrpc_static_pd *spd;
3886 struct notif_data *notifdata = data;
3887
3888 spd = container_of(pdrnb, struct fastrpc_static_pd, pdrnb);
3889 if (code == SERVREG_NOTIF_SERVICE_STATE_DOWN_V01) {
3890 mutex_lock(&me->smd_mutex);
3891 spd->pdrcount++;
3892 spd->ispdup = 0;
3893 pr_info("ADSPRPC: Audio PDR notifier %d %s\n",
3894 MAJOR(me->dev_no), spd->spdname);
3895 mutex_unlock(&me->smd_mutex);
3896 if (!strcmp(spd->spdname,
3897 AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME))
3898 me->staticpd_flags = 0;
3899 fastrpc_notify_pdr_drivers(me, spd->spdname);
3900 } else if (code == SUBSYS_RAMDUMP_NOTIFICATION) {
3901 if (me->channel[0].remoteheap_ramdump_dev &&
3902 notifdata->enable_ramdump) {
3903 me->channel[0].ramdumpenabled = 1;
3904 }
3905 } else if (code == SERVREG_NOTIF_SERVICE_STATE_UP_V01) {
3906 spd->ispdup = 1;
3907 }
3908
3909 return NOTIFY_DONE;
3910}
3911
3912static int fastrpc_get_service_location_notify(struct notifier_block *nb,
3913 unsigned long opcode, void *data)
3914{
3915 struct fastrpc_static_pd *spd;
3916 struct pd_qmi_client_data *pdr = data;
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303917 int curr_state = 0, i = 0;
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05303918
3919 spd = container_of(nb, struct fastrpc_static_pd, get_service_nb);
3920 if (opcode == LOCATOR_DOWN) {
3921 pr_err("ADSPRPC: Audio PD restart notifier locator down\n");
3922 return NOTIFY_DONE;
3923 }
3924
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303925 for (i = 0; i < pdr->total_domains; i++) {
3926 if ((!strcmp(pdr->domain_list[i].name,
3927 "msm/adsp/audio_pd")) ||
3928 (!strcmp(pdr->domain_list[i].name,
3929 "msm/adsp/sensor_pd"))) {
3930 spd->pdrhandle =
3931 service_notif_register_notifier(
3932 pdr->domain_list[i].name,
3933 pdr->domain_list[i].instance_id,
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05303934 &spd->pdrnb, &curr_state);
Tharun Kumar Meruguad4beb82018-05-10 19:51:48 +05303935 if (IS_ERR(spd->pdrhandle)) {
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303936 pr_err("ADSPRPC: Unable to register notifier\n");
Tharun Kumar Meruguad4beb82018-05-10 19:51:48 +05303937 } else if (curr_state ==
3938 SERVREG_NOTIF_SERVICE_STATE_UP_V01) {
3939 pr_info("ADSPRPC: STATE_UP_V01 received\n");
3940 spd->ispdup = 1;
3941 } else if (curr_state ==
3942 SERVREG_NOTIF_SERVICE_STATE_UNINIT_V01) {
3943 pr_info("ADSPRPC: STATE_UNINIT_V01 received\n");
3944 }
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303945 break;
3946 }
3947 }
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05303948
3949 return NOTIFY_DONE;
3950}
3951
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003952static const struct file_operations fops = {
3953 .open = fastrpc_device_open,
3954 .release = fastrpc_device_release,
3955 .unlocked_ioctl = fastrpc_device_ioctl,
3956 .compat_ioctl = compat_fastrpc_device_ioctl,
3957};
3958
3959static const struct of_device_id fastrpc_match_table[] = {
3960 { .compatible = "qcom,msm-fastrpc-adsp", },
3961 { .compatible = "qcom,msm-fastrpc-compute", },
3962 { .compatible = "qcom,msm-fastrpc-compute-cb", },
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05303963 { .compatible = "qcom,msm-fastrpc-legacy-compute", },
3964 { .compatible = "qcom,msm-fastrpc-legacy-compute-cb", },
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003965 { .compatible = "qcom,msm-adsprpc-mem-region", },
3966 {}
3967};
3968
3969static int fastrpc_cb_probe(struct device *dev)
3970{
3971 struct fastrpc_channel_ctx *chan;
3972 struct fastrpc_session_ctx *sess;
3973 struct of_phandle_args iommuspec;
3974 const char *name;
3975 unsigned int start = 0x80000000;
3976 int err = 0, i;
3977 int secure_vmid = VMID_CP_PIXEL;
3978
c_mtharue1a5ce12017-10-13 20:47:09 +05303979 VERIFY(err, NULL != (name = of_get_property(dev->of_node,
3980 "label", NULL)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003981 if (err)
3982 goto bail;
3983 for (i = 0; i < NUM_CHANNELS; i++) {
3984 if (!gcinfo[i].name)
3985 continue;
3986 if (!strcmp(name, gcinfo[i].name))
3987 break;
3988 }
3989 VERIFY(err, i < NUM_CHANNELS);
3990 if (err)
3991 goto bail;
3992 chan = &gcinfo[i];
3993 VERIFY(err, chan->sesscount < NUM_SESSIONS);
3994 if (err)
3995 goto bail;
3996
3997 VERIFY(err, !of_parse_phandle_with_args(dev->of_node, "iommus",
3998 "#iommu-cells", 0, &iommuspec));
3999 if (err)
4000 goto bail;
4001 sess = &chan->session[chan->sesscount];
4002 sess->smmu.cb = iommuspec.args[0] & 0xf;
4003 sess->used = 0;
4004 sess->smmu.coherent = of_property_read_bool(dev->of_node,
4005 "dma-coherent");
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05304006 sess->smmu.sharedcb = of_property_read_bool(dev->of_node,
4007 "shared-cb");
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004008 sess->smmu.secure = of_property_read_bool(dev->of_node,
4009 "qcom,secure-context-bank");
4010 if (sess->smmu.secure)
4011 start = 0x60000000;
4012 VERIFY(err, !IS_ERR_OR_NULL(sess->smmu.mapping =
4013 arm_iommu_create_mapping(&platform_bus_type,
Tharun Kumar Meruguca183f92017-04-27 17:43:27 +05304014 start, 0x78000000)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004015 if (err)
4016 goto bail;
4017
4018 if (sess->smmu.secure)
4019 iommu_domain_set_attr(sess->smmu.mapping->domain,
4020 DOMAIN_ATTR_SECURE_VMID,
4021 &secure_vmid);
4022
4023 VERIFY(err, !arm_iommu_attach_device(dev, sess->smmu.mapping));
4024 if (err)
4025 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05304026 sess->smmu.dev = dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004027 sess->smmu.enabled = 1;
4028 chan->sesscount++;
Sathish Ambley1ca68232017-01-19 10:32:55 -08004029 debugfs_global_file = debugfs_create_file("global", 0644, debugfs_root,
4030 NULL, &debugfs_fops);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004031bail:
4032 return err;
4033}
4034
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05304035static int fastrpc_cb_legacy_probe(struct device *dev)
4036{
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05304037 struct fastrpc_channel_ctx *chan;
4038 struct fastrpc_session_ctx *first_sess = NULL, *sess = NULL;
4039 const char *name;
4040 unsigned int *sids = NULL, sids_size = 0;
4041 int err = 0, ret = 0, i;
4042
4043 unsigned int start = 0x80000000;
4044
4045 VERIFY(err, NULL != (name = of_get_property(dev->of_node,
4046 "label", NULL)));
4047 if (err)
4048 goto bail;
4049
4050 for (i = 0; i < NUM_CHANNELS; i++) {
4051 if (!gcinfo[i].name)
4052 continue;
4053 if (!strcmp(name, gcinfo[i].name))
4054 break;
4055 }
4056 VERIFY(err, i < NUM_CHANNELS);
4057 if (err)
4058 goto bail;
4059
4060 chan = &gcinfo[i];
4061 VERIFY(err, chan->sesscount < NUM_SESSIONS);
4062 if (err)
4063 goto bail;
4064
4065 first_sess = &chan->session[chan->sesscount];
4066
4067 VERIFY(err, NULL != of_get_property(dev->of_node,
4068 "sids", &sids_size));
4069 if (err)
4070 goto bail;
4071
4072 VERIFY(err, NULL != (sids = kzalloc(sids_size, GFP_KERNEL)));
4073 if (err)
4074 goto bail;
4075 ret = of_property_read_u32_array(dev->of_node, "sids", sids,
4076 sids_size/sizeof(unsigned int));
4077 if (ret)
4078 goto bail;
4079
4080 VERIFY(err, !IS_ERR_OR_NULL(first_sess->smmu.mapping =
4081 arm_iommu_create_mapping(&platform_bus_type,
4082 start, 0x78000000)));
4083 if (err)
4084 goto bail;
4085
4086 VERIFY(err, !arm_iommu_attach_device(dev, first_sess->smmu.mapping));
4087 if (err)
4088 goto bail;
4089
4090
4091 for (i = 0; i < sids_size/sizeof(unsigned int); i++) {
4092 VERIFY(err, chan->sesscount < NUM_SESSIONS);
4093 if (err)
4094 goto bail;
4095 sess = &chan->session[chan->sesscount];
4096 sess->smmu.cb = sids[i];
4097 sess->smmu.dev = dev;
4098 sess->smmu.mapping = first_sess->smmu.mapping;
4099 sess->smmu.enabled = 1;
4100 sess->used = 0;
4101 sess->smmu.coherent = false;
4102 sess->smmu.secure = false;
4103 chan->sesscount++;
4104 }
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05304105bail:
4106 kfree(sids);
4107 return err;
4108}
4109
4110
4111
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05304112static void init_secure_vmid_list(struct device *dev, char *prop_name,
4113 struct secure_vm *destvm)
4114{
4115 int err = 0;
4116 u32 len = 0, i = 0;
4117 u32 *rhvmlist = NULL;
4118 u32 *rhvmpermlist = NULL;
4119
4120 if (!of_find_property(dev->of_node, prop_name, &len))
4121 goto bail;
4122 if (len == 0)
4123 goto bail;
4124 len /= sizeof(u32);
4125 VERIFY(err, NULL != (rhvmlist = kcalloc(len, sizeof(u32), GFP_KERNEL)));
4126 if (err)
4127 goto bail;
4128 VERIFY(err, NULL != (rhvmpermlist = kcalloc(len, sizeof(u32),
4129 GFP_KERNEL)));
4130 if (err)
4131 goto bail;
4132 for (i = 0; i < len; i++) {
4133 err = of_property_read_u32_index(dev->of_node, prop_name, i,
4134 &rhvmlist[i]);
4135 rhvmpermlist[i] = PERM_READ | PERM_WRITE | PERM_EXEC;
4136 pr_info("ADSPRPC: Secure VMID = %d", rhvmlist[i]);
4137 if (err) {
4138 pr_err("ADSPRPC: Failed to read VMID\n");
4139 goto bail;
4140 }
4141 }
4142 destvm->vmid = rhvmlist;
4143 destvm->vmperm = rhvmpermlist;
4144 destvm->vmcount = len;
4145bail:
4146 if (err) {
4147 kfree(rhvmlist);
4148 kfree(rhvmpermlist);
4149 }
4150}
4151
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304152static void configure_secure_channels(uint32_t secure_domains)
4153{
4154 struct fastrpc_apps *me = &gfa;
4155 int ii = 0;
4156 /*
4157 * secure_domains contains the bitmask of the secure channels
4158 * Bit 0 - ADSP
4159 * Bit 1 - MDSP
4160 * Bit 2 - SLPI
4161 * Bit 3 - CDSP
4162 */
4163 for (ii = ADSP_DOMAIN_ID; ii <= CDSP_DOMAIN_ID; ++ii) {
4164 int secure = (secure_domains >> ii) & 0x01;
4165
4166 me->channel[ii].secure = secure;
4167 }
4168}
4169
4170
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004171static int fastrpc_probe(struct platform_device *pdev)
4172{
4173 int err = 0;
4174 struct fastrpc_apps *me = &gfa;
4175 struct device *dev = &pdev->dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004176 struct device_node *ion_node, *node;
4177 struct platform_device *ion_pdev;
4178 struct cma *cma;
4179 uint32_t val;
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05304180 int ret = 0;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304181 uint32_t secure_domains;
c_mtharu63ffc012017-11-16 15:26:56 +05304182
4183 if (of_device_is_compatible(dev->of_node,
4184 "qcom,msm-fastrpc-compute")) {
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05304185 init_secure_vmid_list(dev, "qcom,adsp-remoteheap-vmid",
4186 &gcinfo[0].rhvm);
c_mtharu63ffc012017-11-16 15:26:56 +05304187
c_mtharu63ffc012017-11-16 15:26:56 +05304188
4189 of_property_read_u32(dev->of_node, "qcom,rpc-latency-us",
4190 &me->latency);
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304191 if (of_get_property(dev->of_node,
4192 "qcom,secure-domains", NULL) != NULL) {
4193 VERIFY(err, !of_property_read_u32(dev->of_node,
4194 "qcom,secure-domains",
4195 &secure_domains));
zhaochenfc798572018-08-17 15:32:37 +08004196 if (!err) {
4197 me->secure_flag = true;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304198 configure_secure_channels(secure_domains);
zhaochenfc798572018-08-17 15:32:37 +08004199 } else {
4200 me->secure_flag = false;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304201 pr_info("adsprpc: unable to read the domain configuration from dts\n");
zhaochenfc798572018-08-17 15:32:37 +08004202 }
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304203 }
c_mtharu63ffc012017-11-16 15:26:56 +05304204 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004205 if (of_device_is_compatible(dev->of_node,
4206 "qcom,msm-fastrpc-compute-cb"))
4207 return fastrpc_cb_probe(dev);
4208
4209 if (of_device_is_compatible(dev->of_node,
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05304210 "qcom,msm-fastrpc-legacy-compute")) {
4211 me->glink = false;
Tharun Kumar Merugu4f2dcc82018-03-29 00:35:49 +05304212 me->legacy = 1;
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05304213 }
4214
4215 if (of_device_is_compatible(dev->of_node,
4216 "qcom,msm-fastrpc-legacy-compute-cb")){
4217 return fastrpc_cb_legacy_probe(dev);
4218 }
4219
4220 if (of_device_is_compatible(dev->of_node,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004221 "qcom,msm-adsprpc-mem-region")) {
4222 me->dev = dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004223 ion_node = of_find_compatible_node(NULL, NULL, "qcom,msm-ion");
4224 if (ion_node) {
4225 for_each_available_child_of_node(ion_node, node) {
4226 if (of_property_read_u32(node, "reg", &val))
4227 continue;
4228 if (val != ION_ADSP_HEAP_ID)
4229 continue;
4230 ion_pdev = of_find_device_by_node(node);
4231 if (!ion_pdev)
4232 break;
4233 cma = dev_get_cma_area(&ion_pdev->dev);
4234 if (cma) {
Tharun Kumar Merugu4f2dcc82018-03-29 00:35:49 +05304235 me->range.addr = cma_get_base(cma);
4236 me->range.size =
4237 (size_t)cma_get_size(cma);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004238 }
4239 break;
4240 }
4241 }
Tharun Kumar Merugu4f2dcc82018-03-29 00:35:49 +05304242 if (me->range.addr && !of_property_read_bool(dev->of_node,
Tharun Kumar Merugu6b7a4a22018-01-17 16:08:07 +05304243 "restrict-access")) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004244 int srcVM[1] = {VMID_HLOS};
4245 int destVM[4] = {VMID_HLOS, VMID_MSS_MSA, VMID_SSC_Q6,
4246 VMID_ADSP_Q6};
Sathish Ambley84d11862017-05-15 14:36:05 -07004247 int destVMperm[4] = {PERM_READ | PERM_WRITE | PERM_EXEC,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004248 PERM_READ | PERM_WRITE | PERM_EXEC,
4249 PERM_READ | PERM_WRITE | PERM_EXEC,
4250 PERM_READ | PERM_WRITE | PERM_EXEC,
4251 };
4252
Tharun Kumar Merugu4f2dcc82018-03-29 00:35:49 +05304253 VERIFY(err, !hyp_assign_phys(me->range.addr,
4254 me->range.size, srcVM, 1,
4255 destVM, destVMperm, 4));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004256 if (err)
4257 goto bail;
4258 }
4259 return 0;
4260 }
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05304261 if (of_property_read_bool(dev->of_node,
4262 "qcom,fastrpc-adsp-audio-pdr")) {
4263 int session;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004264
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05304265 VERIFY(err, !fastrpc_get_adsp_session(
4266 AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME, &session));
4267 if (err)
4268 goto spdbail;
4269 me->channel[0].spd[session].get_service_nb.notifier_call =
4270 fastrpc_get_service_location_notify;
4271 ret = get_service_location(
4272 AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME,
4273 AUDIO_PDR_ADSP_SERVICE_NAME,
4274 &me->channel[0].spd[session].get_service_nb);
4275 if (ret)
4276 pr_err("ADSPRPC: Get service location failed: %d\n",
4277 ret);
4278 }
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05304279 if (of_property_read_bool(dev->of_node,
4280 "qcom,fastrpc-adsp-sensors-pdr")) {
4281 int session;
4282
4283 VERIFY(err, !fastrpc_get_adsp_session(
4284 SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME, &session));
4285 if (err)
4286 goto spdbail;
4287 me->channel[0].spd[session].get_service_nb.notifier_call =
4288 fastrpc_get_service_location_notify;
4289 ret = get_service_location(
4290 SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME,
4291 SENSORS_PDR_ADSP_SERVICE_NAME,
4292 &me->channel[0].spd[session].get_service_nb);
4293 if (ret)
4294 pr_err("ADSPRPC: Get service location failed: %d\n",
4295 ret);
4296 }
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05304297spdbail:
4298 err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004299 VERIFY(err, !of_platform_populate(pdev->dev.of_node,
4300 fastrpc_match_table,
4301 NULL, &pdev->dev));
4302 if (err)
4303 goto bail;
4304bail:
4305 return err;
4306}
4307
4308static void fastrpc_deinit(void)
4309{
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05304310 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004311 struct fastrpc_channel_ctx *chan = gcinfo;
4312 int i, j;
4313
4314 for (i = 0; i < NUM_CHANNELS; i++, chan++) {
4315 if (chan->chan) {
4316 kref_put_mutex(&chan->kref,
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05304317 fastrpc_channel_close, &me->smd_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05304318 chan->chan = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004319 }
4320 for (j = 0; j < NUM_SESSIONS; j++) {
4321 struct fastrpc_session_ctx *sess = &chan->session[j];
c_mtharue1a5ce12017-10-13 20:47:09 +05304322 if (sess->smmu.dev) {
4323 arm_iommu_detach_device(sess->smmu.dev);
4324 sess->smmu.dev = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004325 }
4326 if (sess->smmu.mapping) {
4327 arm_iommu_release_mapping(sess->smmu.mapping);
c_mtharue1a5ce12017-10-13 20:47:09 +05304328 sess->smmu.mapping = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004329 }
4330 }
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05304331 kfree(chan->rhvm.vmid);
4332 kfree(chan->rhvm.vmperm);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004333 }
4334}
4335
4336static struct platform_driver fastrpc_driver = {
4337 .probe = fastrpc_probe,
4338 .driver = {
4339 .name = "fastrpc",
4340 .owner = THIS_MODULE,
4341 .of_match_table = fastrpc_match_table,
4342 },
4343};
4344
4345static int __init fastrpc_device_init(void)
4346{
4347 struct fastrpc_apps *me = &gfa;
c_mtharue1a5ce12017-10-13 20:47:09 +05304348 struct device *dev = NULL;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304349 struct device *secure_dev = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004350 int err = 0, i;
4351
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05304352 debugfs_root = debugfs_create_dir("adsprpc", NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004353 memset(me, 0, sizeof(*me));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004354 fastrpc_init(me);
4355 me->dev = NULL;
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05304356 me->glink = true;
zhaochenfc798572018-08-17 15:32:37 +08004357 me->secure_flag = false;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004358 VERIFY(err, 0 == platform_driver_register(&fastrpc_driver));
4359 if (err)
4360 goto register_bail;
4361 VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, NUM_CHANNELS,
4362 DEVICE_NAME));
4363 if (err)
4364 goto alloc_chrdev_bail;
4365 cdev_init(&me->cdev, &fops);
4366 me->cdev.owner = THIS_MODULE;
4367 VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0),
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304368 NUM_DEVICES));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004369 if (err)
4370 goto cdev_init_bail;
4371 me->class = class_create(THIS_MODULE, "fastrpc");
4372 VERIFY(err, !IS_ERR(me->class));
4373 if (err)
4374 goto class_create_bail;
4375 me->compat = (fops.compat_ioctl == NULL) ? 0 : 1;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304376
4377 /*
4378 * Create devices and register with sysfs
4379 * Create first device with minor number 0
4380 */
Sathish Ambley36849af2017-02-02 09:35:55 -08004381 dev = device_create(me->class, NULL,
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304382 MKDEV(MAJOR(me->dev_no), MINOR_NUM_DEV),
4383 NULL, DEVICE_NAME);
Sathish Ambley36849af2017-02-02 09:35:55 -08004384 VERIFY(err, !IS_ERR_OR_NULL(dev));
4385 if (err)
4386 goto device_create_bail;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304387
4388 /* Create secure device with minor number for secure device */
4389 secure_dev = device_create(me->class, NULL,
4390 MKDEV(MAJOR(me->dev_no), MINOR_NUM_SECURE_DEV),
4391 NULL, DEVICE_NAME_SECURE);
4392 VERIFY(err, !IS_ERR_OR_NULL(secure_dev));
4393 if (err)
4394 goto device_create_bail;
4395
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004396 for (i = 0; i < NUM_CHANNELS; i++) {
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304397 me->channel[i].dev = secure_dev;
4398 if (i == CDSP_DOMAIN_ID)
4399 me->channel[i].dev = dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004400 me->channel[i].ssrcount = 0;
4401 me->channel[i].prevssrcount = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05304402 me->channel[i].issubsystemup = 1;
4403 me->channel[i].ramdumpenabled = 0;
4404 me->channel[i].remoteheap_ramdump_dev = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004405 me->channel[i].nb.notifier_call = fastrpc_restart_notifier_cb;
4406 me->channel[i].handle = subsys_notif_register_notifier(
4407 gcinfo[i].subsys,
4408 &me->channel[i].nb);
4409 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004410 me->client = msm_ion_client_create(DEVICE_NAME);
4411 VERIFY(err, !IS_ERR_OR_NULL(me->client));
4412 if (err)
4413 goto device_create_bail;
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05304414
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004415 return 0;
4416device_create_bail:
4417 for (i = 0; i < NUM_CHANNELS; i++) {
Sathish Ambley36849af2017-02-02 09:35:55 -08004418 if (me->channel[i].handle)
4419 subsys_notif_unregister_notifier(me->channel[i].handle,
4420 &me->channel[i].nb);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004421 }
Sathish Ambley36849af2017-02-02 09:35:55 -08004422 if (!IS_ERR_OR_NULL(dev))
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304423 device_destroy(me->class, MKDEV(MAJOR(me->dev_no),
4424 MINOR_NUM_DEV));
4425 if (!IS_ERR_OR_NULL(secure_dev))
4426 device_destroy(me->class, MKDEV(MAJOR(me->dev_no),
4427 MINOR_NUM_SECURE_DEV));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004428 class_destroy(me->class);
4429class_create_bail:
4430 cdev_del(&me->cdev);
4431cdev_init_bail:
4432 unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
4433alloc_chrdev_bail:
4434register_bail:
4435 fastrpc_deinit();
4436 return err;
4437}
4438
4439static void __exit fastrpc_device_exit(void)
4440{
4441 struct fastrpc_apps *me = &gfa;
4442 int i;
4443
4444 fastrpc_file_list_dtor(me);
4445 fastrpc_deinit();
4446 for (i = 0; i < NUM_CHANNELS; i++) {
4447 if (!gcinfo[i].name)
4448 continue;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004449 subsys_notif_unregister_notifier(me->channel[i].handle,
4450 &me->channel[i].nb);
4451 }
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304452
4453 /* Destroy the secure and non secure devices */
4454 device_destroy(me->class, MKDEV(MAJOR(me->dev_no), MINOR_NUM_DEV));
4455 device_destroy(me->class, MKDEV(MAJOR(me->dev_no),
4456 MINOR_NUM_SECURE_DEV));
4457
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004458 class_destroy(me->class);
4459 cdev_del(&me->cdev);
4460 unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
4461 ion_client_destroy(me->client);
Sathish Ambley1ca68232017-01-19 10:32:55 -08004462 debugfs_remove_recursive(debugfs_root);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004463}
4464
4465late_initcall(fastrpc_device_init);
4466module_exit(fastrpc_device_exit);
4467
4468MODULE_LICENSE("GPL v2");