blob: 64e6e2fa1e5de6022abfe3550aae58bf618f80ae [file] [log] [blame]
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001/*
Mohammed Nayeem Ur Rahmanfc548a52020-01-07 17:07:55 +05302 * Copyright (c) 2012-2020, 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;
Mohammed Nayeem Ur Rahmancd836462020-04-01 14:30:33 +0530723 int vmid, cid = -1, err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700724 struct fastrpc_session_ctx *sess;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700725
726 if (!map)
727 return;
728 fl = map->fl;
Jeya R85f57d32020-05-26 18:17:26 +0530729 if (fl && !(map->flags == ADSP_MMAP_HEAP_ADDR ||
730 map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR)) {
731 cid = fl->cid;
732 VERIFY(err, cid >= ADSP_DOMAIN_ID && cid < NUM_CHANNELS);
733 if (err) {
734 err = -ECHRNG;
735 pr_err("adsprpc: ERROR:%s, Invalid channel id: %d, err:%d",
736 __func__, cid, err);
737 return;
738 }
Mohammed Nayeem Ur Rahmancd836462020-04-01 14:30:33 +0530739 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530740 if (map->flags == ADSP_MMAP_HEAP_ADDR ||
741 map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
742 spin_lock(&me->hlock);
743 map->refs--;
744 if (!map->refs)
745 hlist_del_init(&map->hn);
746 spin_unlock(&me->hlock);
c_mtharu7bd6a422017-10-17 18:15:37 +0530747 if (map->refs > 0)
748 return;
c_mtharue1a5ce12017-10-13 20:47:09 +0530749 } else {
c_mtharue1a5ce12017-10-13 20:47:09 +0530750 map->refs--;
751 if (!map->refs)
752 hlist_del_init(&map->hn);
c_mtharu7bd6a422017-10-17 18:15:37 +0530753 if (map->refs > 0 && !flags)
754 return;
c_mtharue1a5ce12017-10-13 20:47:09 +0530755 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530756 if (map->flags == ADSP_MMAP_HEAP_ADDR ||
757 map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700758
c_mtharue1a5ce12017-10-13 20:47:09 +0530759 if (me->dev == NULL) {
760 pr_err("failed to free remote heap allocation\n");
761 return;
762 }
763 if (map->phys) {
Tharun Kumar Merugu72c90252019-08-29 18:36:08 +0530764 unsigned long dma_attrs = DMA_ATTR_SKIP_ZEROING |
765 DMA_ATTR_NO_KERNEL_MAPPING;
Tharun Kumar Merugu48d5ff32018-04-16 19:24:16 +0530766 dma_free_attrs(me->dev, map->size, (void *)map->va,
767 (dma_addr_t)map->phys, dma_attrs);
c_mtharue1a5ce12017-10-13 20:47:09 +0530768 }
Tharun Kumar Merugu35a94a52018-02-01 21:09:04 +0530769 } else if (map->flags == FASTRPC_DMAHANDLE_NOMAP) {
770 if (!IS_ERR_OR_NULL(map->handle))
771 ion_free(fl->apps->client, map->handle);
c_mtharue1a5ce12017-10-13 20:47:09 +0530772 } else {
773 int destVM[1] = {VMID_HLOS};
774 int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
775
776 if (map->secure)
777 sess = fl->secsctx;
778 else
779 sess = fl->sctx;
780
781 if (!IS_ERR_OR_NULL(map->handle))
782 ion_free(fl->apps->client, map->handle);
783 if (sess && sess->smmu.enabled) {
784 if (map->size || map->phys)
785 msm_dma_unmap_sg(sess->smmu.dev,
786 map->table->sgl,
787 map->table->nents, DMA_BIDIRECTIONAL,
788 map->buf);
789 }
790 vmid = fl->apps->channel[fl->cid].vmid;
791 if (vmid && map->phys) {
792 int srcVM[2] = {VMID_HLOS, vmid};
793
794 hyp_assign_phys(map->phys, buf_page_size(map->size),
795 srcVM, 2, destVM, destVMperm, 1);
796 }
797
798 if (!IS_ERR_OR_NULL(map->table))
799 dma_buf_unmap_attachment(map->attach, map->table,
800 DMA_BIDIRECTIONAL);
801 if (!IS_ERR_OR_NULL(map->attach))
802 dma_buf_detach(map->buf, map->attach);
803 if (!IS_ERR_OR_NULL(map->buf))
804 dma_buf_put(map->buf);
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700805 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700806 kfree(map);
807}
808
809static int fastrpc_session_alloc(struct fastrpc_channel_ctx *chan, int secure,
810 struct fastrpc_session_ctx **session);
811
812static int fastrpc_mmap_create(struct fastrpc_file *fl, int fd,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530813 unsigned int attr, uintptr_t va, size_t len, int mflags,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700814 struct fastrpc_mmap **ppmap)
815{
c_mtharue1a5ce12017-10-13 20:47:09 +0530816 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700817 struct fastrpc_session_ctx *sess;
818 struct fastrpc_apps *apps = fl->apps;
c_mtharue1a5ce12017-10-13 20:47:09 +0530819 struct fastrpc_mmap *map = NULL;
Mohammed Nayeem Ur Rahmancd836462020-04-01 14:30:33 +0530820 struct fastrpc_channel_ctx *chan = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700821 unsigned long attrs;
c_mtharuf931ff92017-11-30 19:35:30 +0530822 dma_addr_t region_phys = 0;
Tharun Kumar Merugu0d0b69e2018-09-14 22:30:58 +0530823 void *region_vaddr = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700824 unsigned long flags;
Mohammed Nayeem Ur Rahmancd836462020-04-01 14:30:33 +0530825 int err = 0, vmid, cid = -1;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700826
Mohammed Nayeem Ur Rahmancd836462020-04-01 14:30:33 +0530827 cid = fl->cid;
828 VERIFY(err, cid >= ADSP_DOMAIN_ID && cid < NUM_CHANNELS);
829 if (err) {
830 err = -ECHRNG;
831 goto bail;
832 }
833 chan = &apps->channel[cid];
Sathish Ambleyae5ee542017-01-16 22:24:23 -0800834 if (!fastrpc_mmap_find(fl, fd, va, len, mflags, 1, ppmap))
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700835 return 0;
836 map = kzalloc(sizeof(*map), GFP_KERNEL);
837 VERIFY(err, !IS_ERR_OR_NULL(map));
838 if (err)
839 goto bail;
840 INIT_HLIST_NODE(&map->hn);
841 map->flags = mflags;
842 map->refs = 1;
843 map->fl = fl;
844 map->fd = fd;
845 map->attr = attr;
c_mtharue1a5ce12017-10-13 20:47:09 +0530846 if (mflags == ADSP_MMAP_HEAP_ADDR ||
847 mflags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
Tharun Kumar Merugue073de72018-07-30 23:57:47 +0530848 unsigned long dma_attrs = DMA_ATTR_SKIP_ZEROING |
849 DMA_ATTR_NO_KERNEL_MAPPING;
850
c_mtharue1a5ce12017-10-13 20:47:09 +0530851 map->apps = me;
852 map->fl = NULL;
Tharun Kumar Merugu0d0b69e2018-09-14 22:30:58 +0530853 VERIFY(err, !dma_alloc_memory(&region_phys, &region_vaddr,
854 len, dma_attrs));
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700855 if (err)
856 goto bail;
c_mtharuf931ff92017-11-30 19:35:30 +0530857 map->phys = (uintptr_t)region_phys;
c_mtharue1a5ce12017-10-13 20:47:09 +0530858 map->size = len;
Tharun Kumar Merugu0d0b69e2018-09-14 22:30:58 +0530859 map->va = (uintptr_t)region_vaddr;
Tharun Kumar Merugu35a94a52018-02-01 21:09:04 +0530860 } else if (mflags == FASTRPC_DMAHANDLE_NOMAP) {
861 ion_phys_addr_t iphys;
862
863 VERIFY(err, !IS_ERR_OR_NULL(map->handle =
864 ion_import_dma_buf_fd(fl->apps->client, fd)));
865 if (err)
866 goto bail;
867
868 map->uncached = 1;
869 map->buf = NULL;
870 map->attach = NULL;
871 map->table = NULL;
872 map->va = 0;
873 map->phys = 0;
874
875 err = ion_phys(fl->apps->client, map->handle,
876 &iphys, &map->size);
877 if (err)
878 goto bail;
879 map->phys = (uint64_t)iphys;
c_mtharue1a5ce12017-10-13 20:47:09 +0530880 } else {
c_mtharu7bd6a422017-10-17 18:15:37 +0530881 if (map->attr && (map->attr & FASTRPC_ATTR_KEEP_MAP)) {
882 pr_info("adsprpc: buffer mapped with persist attr %x\n",
883 (unsigned int)map->attr);
884 map->refs = 2;
885 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530886 VERIFY(err, !IS_ERR_OR_NULL(map->handle =
887 ion_import_dma_buf_fd(fl->apps->client, fd)));
888 if (err)
889 goto bail;
890 VERIFY(err, !ion_handle_get_flags(fl->apps->client, map->handle,
891 &flags));
892 if (err)
893 goto bail;
894
c_mtharue1a5ce12017-10-13 20:47:09 +0530895 map->secure = flags & ION_FLAG_SECURE;
896 if (map->secure) {
897 if (!fl->secsctx)
898 err = fastrpc_session_alloc(chan, 1,
899 &fl->secsctx);
900 if (err)
901 goto bail;
902 }
903 if (map->secure)
904 sess = fl->secsctx;
905 else
906 sess = fl->sctx;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +0530907
c_mtharue1a5ce12017-10-13 20:47:09 +0530908 VERIFY(err, !IS_ERR_OR_NULL(sess));
909 if (err)
910 goto bail;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +0530911
912 map->uncached = !ION_IS_CACHED(flags);
913 if (map->attr & FASTRPC_ATTR_NOVA && !sess->smmu.coherent)
914 map->uncached = 1;
915
c_mtharue1a5ce12017-10-13 20:47:09 +0530916 VERIFY(err, !IS_ERR_OR_NULL(map->buf = dma_buf_get(fd)));
917 if (err)
918 goto bail;
919 VERIFY(err, !IS_ERR_OR_NULL(map->attach =
920 dma_buf_attach(map->buf, sess->smmu.dev)));
921 if (err)
922 goto bail;
923 VERIFY(err, !IS_ERR_OR_NULL(map->table =
924 dma_buf_map_attachment(map->attach,
925 DMA_BIDIRECTIONAL)));
926 if (err)
927 goto bail;
928 if (sess->smmu.enabled) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700929 attrs = DMA_ATTR_EXEC_MAPPING;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +0530930
931 if (map->attr & FASTRPC_ATTR_NON_COHERENT ||
932 (sess->smmu.coherent && map->uncached))
933 attrs |= DMA_ATTR_FORCE_NON_COHERENT;
934 else if (map->attr & FASTRPC_ATTR_COHERENT)
935 attrs |= DMA_ATTR_FORCE_COHERENT;
936
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700937 VERIFY(err, map->table->nents ==
c_mtharue1a5ce12017-10-13 20:47:09 +0530938 msm_dma_map_sg_attrs(sess->smmu.dev,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700939 map->table->sgl, map->table->nents,
940 DMA_BIDIRECTIONAL, map->buf, attrs));
c_mtharue1a5ce12017-10-13 20:47:09 +0530941 if (err)
942 goto bail;
943 } else {
944 VERIFY(err, map->table->nents == 1);
945 if (err)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700946 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +0530947 }
948 map->phys = sg_dma_address(map->table->sgl);
Tharun Kumar Merugu93f319a2018-02-01 17:35:42 +0530949
c_mtharue1a5ce12017-10-13 20:47:09 +0530950 if (sess->smmu.cb) {
951 map->phys += ((uint64_t)sess->smmu.cb << 32);
952 map->size = sg_dma_len(map->table->sgl);
953 } else {
954 map->size = buf_page_size(len);
955 }
Tharun Kumar Merugu93f319a2018-02-01 17:35:42 +0530956
c_mtharue1a5ce12017-10-13 20:47:09 +0530957 vmid = fl->apps->channel[fl->cid].vmid;
Tharun Kumar Merugu93f319a2018-02-01 17:35:42 +0530958 if (!sess->smmu.enabled && !vmid) {
959 VERIFY(err, map->phys >= me->range.addr &&
960 map->phys + map->size <=
961 me->range.addr + me->range.size);
962 if (err) {
963 pr_err("adsprpc: mmap fail out of range\n");
964 goto bail;
965 }
966 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530967 if (vmid) {
968 int srcVM[1] = {VMID_HLOS};
969 int destVM[2] = {VMID_HLOS, vmid};
970 int destVMperm[2] = {PERM_READ | PERM_WRITE,
971 PERM_READ | PERM_WRITE | PERM_EXEC};
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700972
c_mtharue1a5ce12017-10-13 20:47:09 +0530973 VERIFY(err, !hyp_assign_phys(map->phys,
974 buf_page_size(map->size),
975 srcVM, 1, destVM, destVMperm, 2));
976 if (err)
977 goto bail;
978 }
979 map->va = va;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700980 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700981 map->len = len;
982
983 fastrpc_mmap_add(map);
984 *ppmap = map;
985
986bail:
987 if (err && map)
c_mtharu7bd6a422017-10-17 18:15:37 +0530988 fastrpc_mmap_free(map, 0);
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700989 return err;
990}
991
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530992static int fastrpc_buf_alloc(struct fastrpc_file *fl, size_t size,
Tharun Kumar Merugue073de72018-07-30 23:57:47 +0530993 unsigned long dma_attr, uint32_t rflags,
994 int remote, struct fastrpc_buf **obuf)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700995{
996 int err = 0, vmid;
c_mtharue1a5ce12017-10-13 20:47:09 +0530997 struct fastrpc_buf *buf = NULL, *fr = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700998 struct hlist_node *n;
999
1000 VERIFY(err, size > 0);
1001 if (err)
1002 goto bail;
1003
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05301004 if (!remote) {
1005 /* find the smallest buffer that fits in the cache */
1006 spin_lock(&fl->hlock);
1007 hlist_for_each_entry_safe(buf, n, &fl->cached_bufs, hn) {
1008 if (buf->size >= size && (!fr || fr->size > buf->size))
1009 fr = buf;
1010 }
1011 if (fr)
1012 hlist_del_init(&fr->hn);
1013 spin_unlock(&fl->hlock);
1014 if (fr) {
1015 *obuf = fr;
1016 return 0;
1017 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001018 }
c_mtharue1a5ce12017-10-13 20:47:09 +05301019 buf = NULL;
1020 VERIFY(err, NULL != (buf = kzalloc(sizeof(*buf), GFP_KERNEL)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001021 if (err)
1022 goto bail;
1023 INIT_HLIST_NODE(&buf->hn);
1024 buf->fl = fl;
c_mtharue1a5ce12017-10-13 20:47:09 +05301025 buf->virt = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001026 buf->phys = 0;
1027 buf->size = size;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05301028 buf->dma_attr = dma_attr;
1029 buf->flags = rflags;
1030 buf->raddr = 0;
1031 buf->remote = 0;
1032 buf->virt = dma_alloc_attrs(fl->sctx->smmu.dev, buf->size,
1033 (dma_addr_t *)&buf->phys,
1034 GFP_KERNEL, buf->dma_attr);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001035 if (IS_ERR_OR_NULL(buf->virt)) {
1036 /* free cache and retry */
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05301037 fastrpc_cached_buf_list_free(fl);
1038 buf->virt = dma_alloc_attrs(fl->sctx->smmu.dev, buf->size,
1039 (dma_addr_t *)&buf->phys,
1040 GFP_KERNEL, buf->dma_attr);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001041 VERIFY(err, !IS_ERR_OR_NULL(buf->virt));
1042 }
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05301043 if (err) {
1044 err = -ENOMEM;
1045 pr_err("adsprpc: %s: %s: dma_alloc_attrs failed for size 0x%zx\n",
1046 current->comm, __func__, size);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001047 goto bail;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05301048 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001049 if (fl->sctx->smmu.cb)
1050 buf->phys += ((uint64_t)fl->sctx->smmu.cb << 32);
1051 vmid = fl->apps->channel[fl->cid].vmid;
1052 if (vmid) {
1053 int srcVM[1] = {VMID_HLOS};
1054 int destVM[2] = {VMID_HLOS, vmid};
1055 int destVMperm[2] = {PERM_READ | PERM_WRITE,
1056 PERM_READ | PERM_WRITE | PERM_EXEC};
1057
1058 VERIFY(err, !hyp_assign_phys(buf->phys, buf_page_size(size),
1059 srcVM, 1, destVM, destVMperm, 2));
1060 if (err)
1061 goto bail;
1062 }
1063
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05301064 if (remote) {
1065 INIT_HLIST_NODE(&buf->hn_rem);
1066 spin_lock(&fl->hlock);
1067 hlist_add_head(&buf->hn_rem, &fl->remote_bufs);
1068 spin_unlock(&fl->hlock);
1069 buf->remote = remote;
1070 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001071 *obuf = buf;
1072 bail:
1073 if (err && buf)
1074 fastrpc_buf_free(buf, 0);
1075 return err;
1076}
1077
1078
1079static int context_restore_interrupted(struct fastrpc_file *fl,
Sathish Ambleybae51902017-07-03 15:00:49 -07001080 struct fastrpc_ioctl_invoke_crc *inv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001081 struct smq_invoke_ctx **po)
1082{
1083 int err = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05301084 struct smq_invoke_ctx *ctx = NULL, *ictx = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001085 struct hlist_node *n;
1086 struct fastrpc_ioctl_invoke *invoke = &inv->inv;
1087
1088 spin_lock(&fl->hlock);
1089 hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) {
1090 if (ictx->pid == current->pid) {
1091 if (invoke->sc != ictx->sc || ictx->fl != fl)
1092 err = -1;
1093 else {
1094 ctx = ictx;
1095 hlist_del_init(&ctx->hn);
1096 hlist_add_head(&ctx->hn, &fl->clst.pending);
1097 }
1098 break;
1099 }
1100 }
1101 spin_unlock(&fl->hlock);
1102 if (ctx)
1103 *po = ctx;
1104 return err;
1105}
1106
1107#define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)
1108static int overlap_ptr_cmp(const void *a, const void *b)
1109{
1110 struct overlap *pa = *((struct overlap **)a);
1111 struct overlap *pb = *((struct overlap **)b);
1112 /* sort with lowest starting buffer first */
1113 int st = CMP(pa->start, pb->start);
1114 /* sort with highest ending buffer first */
1115 int ed = CMP(pb->end, pa->end);
1116 return st == 0 ? ed : st;
1117}
1118
Sathish Ambley9466d672017-01-25 10:51:55 -08001119static int context_build_overlap(struct smq_invoke_ctx *ctx)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001120{
Sathish Ambley9466d672017-01-25 10:51:55 -08001121 int i, err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001122 remote_arg_t *lpra = ctx->lpra;
1123 int inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
1124 int outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc);
1125 int nbufs = inbufs + outbufs;
1126 struct overlap max;
1127
1128 for (i = 0; i < nbufs; ++i) {
1129 ctx->overs[i].start = (uintptr_t)lpra[i].buf.pv;
1130 ctx->overs[i].end = ctx->overs[i].start + lpra[i].buf.len;
Sathish Ambley9466d672017-01-25 10:51:55 -08001131 if (lpra[i].buf.len) {
1132 VERIFY(err, ctx->overs[i].end > ctx->overs[i].start);
1133 if (err)
1134 goto bail;
1135 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001136 ctx->overs[i].raix = i;
1137 ctx->overps[i] = &ctx->overs[i];
1138 }
c_mtharue1a5ce12017-10-13 20:47:09 +05301139 sort(ctx->overps, nbufs, sizeof(*ctx->overps), overlap_ptr_cmp, NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001140 max.start = 0;
1141 max.end = 0;
1142 for (i = 0; i < nbufs; ++i) {
1143 if (ctx->overps[i]->start < max.end) {
1144 ctx->overps[i]->mstart = max.end;
1145 ctx->overps[i]->mend = ctx->overps[i]->end;
1146 ctx->overps[i]->offset = max.end -
1147 ctx->overps[i]->start;
1148 if (ctx->overps[i]->end > max.end) {
1149 max.end = ctx->overps[i]->end;
1150 } else {
1151 ctx->overps[i]->mend = 0;
1152 ctx->overps[i]->mstart = 0;
1153 }
1154 } else {
1155 ctx->overps[i]->mend = ctx->overps[i]->end;
1156 ctx->overps[i]->mstart = ctx->overps[i]->start;
1157 ctx->overps[i]->offset = 0;
1158 max = *ctx->overps[i];
1159 }
1160 }
Sathish Ambley9466d672017-01-25 10:51:55 -08001161bail:
1162 return err;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001163}
1164
1165#define K_COPY_FROM_USER(err, kernel, dst, src, size) \
1166 do {\
1167 if (!(kernel))\
c_mtharue1a5ce12017-10-13 20:47:09 +05301168 VERIFY(err, 0 == copy_from_user((dst),\
1169 (void const __user *)(src),\
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001170 (size)));\
1171 else\
1172 memmove((dst), (src), (size));\
1173 } while (0)
1174
1175#define K_COPY_TO_USER(err, kernel, dst, src, size) \
1176 do {\
1177 if (!(kernel))\
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301178 VERIFY(err, 0 == copy_to_user((void __user *)(dst),\
c_mtharue1a5ce12017-10-13 20:47:09 +05301179 (src), (size)));\
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001180 else\
1181 memmove((dst), (src), (size));\
1182 } while (0)
1183
1184
1185static void context_free(struct smq_invoke_ctx *ctx);
1186
1187static int context_alloc(struct fastrpc_file *fl, uint32_t kernel,
Sathish Ambleybae51902017-07-03 15:00:49 -07001188 struct fastrpc_ioctl_invoke_crc *invokefd,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001189 struct smq_invoke_ctx **po)
1190{
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301191 struct fastrpc_apps *me = &gfa;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301192 int err = 0, bufs, ii, size = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05301193 struct smq_invoke_ctx *ctx = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001194 struct fastrpc_ctx_lst *clst = &fl->clst;
1195 struct fastrpc_ioctl_invoke *invoke = &invokefd->inv;
1196
1197 bufs = REMOTE_SCALARS_LENGTH(invoke->sc);
1198 size = bufs * sizeof(*ctx->lpra) + bufs * sizeof(*ctx->maps) +
1199 sizeof(*ctx->fds) * (bufs) +
1200 sizeof(*ctx->attrs) * (bufs) +
1201 sizeof(*ctx->overs) * (bufs) +
1202 sizeof(*ctx->overps) * (bufs);
1203
c_mtharue1a5ce12017-10-13 20:47:09 +05301204 VERIFY(err, NULL != (ctx = kzalloc(sizeof(*ctx) + size, GFP_KERNEL)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001205 if (err)
1206 goto bail;
1207
1208 INIT_HLIST_NODE(&ctx->hn);
1209 hlist_add_fake(&ctx->hn);
1210 ctx->fl = fl;
1211 ctx->maps = (struct fastrpc_mmap **)(&ctx[1]);
1212 ctx->lpra = (remote_arg_t *)(&ctx->maps[bufs]);
1213 ctx->fds = (int *)(&ctx->lpra[bufs]);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301214 if (me->legacy) {
1215 ctx->overs = (struct overlap *)(&ctx->fds[bufs]);
1216 ctx->overps = (struct overlap **)(&ctx->overs[bufs]);
1217 } else {
1218 ctx->attrs = (unsigned int *)(&ctx->fds[bufs]);
1219 ctx->overs = (struct overlap *)(&ctx->attrs[bufs]);
1220 ctx->overps = (struct overlap **)(&ctx->overs[bufs]);
1221 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001222
c_mtharue1a5ce12017-10-13 20:47:09 +05301223 K_COPY_FROM_USER(err, kernel, (void *)ctx->lpra, invoke->pra,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001224 bufs * sizeof(*ctx->lpra));
1225 if (err)
1226 goto bail;
1227
1228 if (invokefd->fds) {
1229 K_COPY_FROM_USER(err, kernel, ctx->fds, invokefd->fds,
1230 bufs * sizeof(*ctx->fds));
1231 if (err)
1232 goto bail;
1233 }
1234 if (invokefd->attrs) {
1235 K_COPY_FROM_USER(err, kernel, ctx->attrs, invokefd->attrs,
1236 bufs * sizeof(*ctx->attrs));
1237 if (err)
1238 goto bail;
1239 }
Sathish Ambleybae51902017-07-03 15:00:49 -07001240 ctx->crc = (uint32_t *)invokefd->crc;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001241 ctx->sc = invoke->sc;
Sathish Ambley9466d672017-01-25 10:51:55 -08001242 if (bufs) {
1243 VERIFY(err, 0 == context_build_overlap(ctx));
1244 if (err)
1245 goto bail;
1246 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001247 ctx->retval = -1;
1248 ctx->pid = current->pid;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301249 ctx->tgid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001250 init_completion(&ctx->work);
c_mtharufdac6892017-10-12 13:09:01 +05301251 ctx->magic = FASTRPC_CTX_MAGIC;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001252
1253 spin_lock(&fl->hlock);
1254 hlist_add_head(&ctx->hn, &clst->pending);
1255 spin_unlock(&fl->hlock);
1256
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301257 spin_lock(&me->ctxlock);
1258 for (ii = 0; ii < FASTRPC_CTX_MAX; ii++) {
1259 if (!me->ctxtable[ii]) {
1260 me->ctxtable[ii] = ctx;
1261 ctx->ctxid = (ptr_to_uint64(ctx) & ~0xFFF)|(ii << 4);
1262 break;
1263 }
1264 }
1265 spin_unlock(&me->ctxlock);
1266 VERIFY(err, ii < FASTRPC_CTX_MAX);
1267 if (err) {
1268 pr_err("adsprpc: out of context memory\n");
1269 goto bail;
1270 }
1271
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001272 *po = ctx;
1273bail:
1274 if (ctx && err)
1275 context_free(ctx);
1276 return err;
1277}
1278
1279static void context_save_interrupted(struct smq_invoke_ctx *ctx)
1280{
1281 struct fastrpc_ctx_lst *clst = &ctx->fl->clst;
1282
1283 spin_lock(&ctx->fl->hlock);
1284 hlist_del_init(&ctx->hn);
1285 hlist_add_head(&ctx->hn, &clst->interrupted);
1286 spin_unlock(&ctx->fl->hlock);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001287}
1288
1289static void context_free(struct smq_invoke_ctx *ctx)
1290{
1291 int i;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301292 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001293 int nbufs = REMOTE_SCALARS_INBUFS(ctx->sc) +
1294 REMOTE_SCALARS_OUTBUFS(ctx->sc);
1295 spin_lock(&ctx->fl->hlock);
1296 hlist_del_init(&ctx->hn);
1297 spin_unlock(&ctx->fl->hlock);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301298 mutex_lock(&ctx->fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001299 for (i = 0; i < nbufs; ++i)
c_mtharu7bd6a422017-10-17 18:15:37 +05301300 fastrpc_mmap_free(ctx->maps[i], 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301301
1302 mutex_unlock(&ctx->fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001303 fastrpc_buf_free(ctx->buf, 1);
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301304 fastrpc_buf_free(ctx->lbuf, 1);
c_mtharufdac6892017-10-12 13:09:01 +05301305 ctx->magic = 0;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301306 ctx->ctxid = 0;
1307
1308 spin_lock(&me->ctxlock);
1309 for (i = 0; i < FASTRPC_CTX_MAX; i++) {
1310 if (me->ctxtable[i] == ctx) {
1311 me->ctxtable[i] = NULL;
1312 break;
1313 }
1314 }
1315 spin_unlock(&me->ctxlock);
1316
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001317 kfree(ctx);
1318}
1319
1320static void context_notify_user(struct smq_invoke_ctx *ctx, int retval)
1321{
1322 ctx->retval = retval;
1323 complete(&ctx->work);
1324}
1325
1326
1327static void fastrpc_notify_users(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 complete(&ictx->work);
1335 }
1336 hlist_for_each_entry_safe(ictx, n, &me->clst.interrupted, hn) {
1337 complete(&ictx->work);
1338 }
1339 spin_unlock(&me->hlock);
1340
1341}
1342
Tharun Kumar Merugu77dd5872018-04-02 12:48:17 +05301343
1344static void fastrpc_notify_users_staticpd_pdr(struct fastrpc_file *me)
1345{
1346 struct smq_invoke_ctx *ictx;
1347 struct hlist_node *n;
1348
1349 spin_lock(&me->hlock);
1350 hlist_for_each_entry_safe(ictx, n, &me->clst.pending, hn) {
1351 if (ictx->msg.pid)
1352 complete(&ictx->work);
1353 }
1354 hlist_for_each_entry_safe(ictx, n, &me->clst.interrupted, hn) {
1355 if (ictx->msg.pid)
1356 complete(&ictx->work);
1357 }
1358 spin_unlock(&me->hlock);
1359}
1360
1361
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001362static void fastrpc_notify_drivers(struct fastrpc_apps *me, int cid)
1363{
1364 struct fastrpc_file *fl;
1365 struct hlist_node *n;
1366
1367 spin_lock(&me->hlock);
1368 hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
1369 if (fl->cid == cid)
1370 fastrpc_notify_users(fl);
1371 }
1372 spin_unlock(&me->hlock);
1373
1374}
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05301375
1376static void fastrpc_notify_pdr_drivers(struct fastrpc_apps *me, char *spdname)
1377{
1378 struct fastrpc_file *fl;
1379 struct hlist_node *n;
1380
1381 spin_lock(&me->hlock);
1382 hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
1383 if (fl->spdname && !strcmp(spdname, fl->spdname))
Tharun Kumar Merugu77dd5872018-04-02 12:48:17 +05301384 fastrpc_notify_users_staticpd_pdr(fl);
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05301385 }
1386 spin_unlock(&me->hlock);
1387
1388}
1389
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001390static void context_list_ctor(struct fastrpc_ctx_lst *me)
1391{
1392 INIT_HLIST_HEAD(&me->interrupted);
1393 INIT_HLIST_HEAD(&me->pending);
1394}
1395
1396static void fastrpc_context_list_dtor(struct fastrpc_file *fl)
1397{
1398 struct fastrpc_ctx_lst *clst = &fl->clst;
c_mtharue1a5ce12017-10-13 20:47:09 +05301399 struct smq_invoke_ctx *ictx = NULL, *ctxfree;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001400 struct hlist_node *n;
1401
1402 do {
c_mtharue1a5ce12017-10-13 20:47:09 +05301403 ctxfree = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001404 spin_lock(&fl->hlock);
1405 hlist_for_each_entry_safe(ictx, n, &clst->interrupted, hn) {
1406 hlist_del_init(&ictx->hn);
1407 ctxfree = ictx;
1408 break;
1409 }
1410 spin_unlock(&fl->hlock);
1411 if (ctxfree)
1412 context_free(ctxfree);
1413 } while (ctxfree);
1414 do {
c_mtharue1a5ce12017-10-13 20:47:09 +05301415 ctxfree = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001416 spin_lock(&fl->hlock);
1417 hlist_for_each_entry_safe(ictx, n, &clst->pending, hn) {
1418 hlist_del_init(&ictx->hn);
1419 ctxfree = ictx;
1420 break;
1421 }
1422 spin_unlock(&fl->hlock);
1423 if (ctxfree)
1424 context_free(ctxfree);
1425 } while (ctxfree);
1426}
1427
1428static int fastrpc_file_free(struct fastrpc_file *fl);
1429static void fastrpc_file_list_dtor(struct fastrpc_apps *me)
1430{
1431 struct fastrpc_file *fl, *free;
1432 struct hlist_node *n;
1433
1434 do {
c_mtharue1a5ce12017-10-13 20:47:09 +05301435 free = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001436 spin_lock(&me->hlock);
1437 hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
1438 hlist_del_init(&fl->hn);
1439 free = fl;
1440 break;
1441 }
1442 spin_unlock(&me->hlock);
1443 if (free)
1444 fastrpc_file_free(free);
1445 } while (free);
1446}
1447
1448static int get_args(uint32_t kernel, struct smq_invoke_ctx *ctx)
1449{
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301450 struct fastrpc_apps *me = &gfa;
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301451 remote_arg64_t *rpra, *lrpra;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001452 remote_arg_t *lpra = ctx->lpra;
1453 struct smq_invoke_buf *list;
1454 struct smq_phy_page *pages, *ipage;
1455 uint32_t sc = ctx->sc;
1456 int inbufs = REMOTE_SCALARS_INBUFS(sc);
1457 int outbufs = REMOTE_SCALARS_OUTBUFS(sc);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001458 int handles, bufs = inbufs + outbufs;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001459 uintptr_t args;
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301460 size_t rlen = 0, copylen = 0, metalen = 0, lrpralen = 0;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001461 int i, oix;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001462 int err = 0;
1463 int mflags = 0;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001464 uint64_t *fdlist;
Sathish Ambleybae51902017-07-03 15:00:49 -07001465 uint32_t *crclist;
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301466 int64_t *perf_counter = getperfcounter(ctx->fl, PERF_COUNT);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001467
1468 /* calculate size of the metadata */
c_mtharue1a5ce12017-10-13 20:47:09 +05301469 rpra = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001470 list = smq_invoke_buf_start(rpra, sc);
1471 pages = smq_phy_page_start(sc, list);
1472 ipage = pages;
1473
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301474 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_MAP),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001475 for (i = 0; i < bufs; ++i) {
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301476 uintptr_t buf = (uintptr_t)lpra[i].buf.pv;
1477 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001478
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301479 mutex_lock(&ctx->fl->fl_map_mutex);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301480 if (ctx->fds[i] && (ctx->fds[i] != -1)) {
1481 unsigned int attrs = 0;
1482
1483 if (ctx->attrs)
1484 attrs = ctx->attrs[i];
1485
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001486 fastrpc_mmap_create(ctx->fl, ctx->fds[i],
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301487 attrs, buf, len,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001488 mflags, &ctx->maps[i]);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301489 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301490 mutex_unlock(&ctx->fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001491 ipage += 1;
1492 }
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301493 PERF_END);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001494 handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301495 mutex_lock(&ctx->fl->fl_map_mutex);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001496 for (i = bufs; i < bufs + handles; i++) {
Tharun Kumar Merugu35a94a52018-02-01 21:09:04 +05301497 int dmaflags = 0;
1498
1499 if (ctx->attrs && (ctx->attrs[i] & FASTRPC_ATTR_NOMAP))
1500 dmaflags = FASTRPC_DMAHANDLE_NOMAP;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001501 VERIFY(err, !fastrpc_mmap_create(ctx->fl, ctx->fds[i],
Tharun Kumar Merugu35a94a52018-02-01 21:09:04 +05301502 FASTRPC_ATTR_NOVA, 0, 0, dmaflags, &ctx->maps[i]));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301503 if (err) {
1504 mutex_unlock(&ctx->fl->fl_map_mutex);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001505 goto bail;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301506 }
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001507 ipage += 1;
1508 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301509 mutex_unlock(&ctx->fl->fl_map_mutex);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301510 if (!me->legacy) {
1511 metalen = copylen = (size_t)&ipage[0] +
1512 (sizeof(uint64_t) * M_FDLIST) +
1513 (sizeof(uint32_t) * M_CRCLIST);
1514 } else {
1515 metalen = copylen = (size_t)&ipage[0];
1516 }
Sathish Ambleybae51902017-07-03 15:00:49 -07001517
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301518 /* allocate new local rpra buffer */
1519 lrpralen = (size_t)&list[0];
1520 if (lrpralen) {
1521 err = fastrpc_buf_alloc(ctx->fl, lrpralen, 0, 0, 0, &ctx->lbuf);
1522 if (err)
1523 goto bail;
1524 }
1525 if (ctx->lbuf->virt)
1526 memset(ctx->lbuf->virt, 0, lrpralen);
1527
1528 lrpra = ctx->lbuf->virt;
1529 ctx->lrpra = lrpra;
1530
1531 /* calculate len required for copying */
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001532 for (oix = 0; oix < inbufs + outbufs; ++oix) {
1533 int i = ctx->overps[oix]->raix;
Sathish Ambleyd209c1e2016-12-13 15:27:30 -08001534 uintptr_t mstart, mend;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301535 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001536
1537 if (!len)
1538 continue;
1539 if (ctx->maps[i])
1540 continue;
1541 if (ctx->overps[oix]->offset == 0)
1542 copylen = ALIGN(copylen, BALIGN);
Sathish Ambleyd209c1e2016-12-13 15:27:30 -08001543 mstart = ctx->overps[oix]->mstart;
1544 mend = ctx->overps[oix]->mend;
1545 VERIFY(err, (mend - mstart) <= LONG_MAX);
1546 if (err)
1547 goto bail;
1548 copylen += mend - mstart;
1549 VERIFY(err, copylen >= 0);
1550 if (err)
1551 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001552 }
1553 ctx->used = copylen;
1554
1555 /* allocate new buffer */
1556 if (copylen) {
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05301557 err = fastrpc_buf_alloc(ctx->fl, copylen, 0, 0, 0, &ctx->buf);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001558 if (err)
1559 goto bail;
1560 }
Tharun Kumar Merugue3361f92017-06-22 10:45:43 +05301561 if (ctx->buf->virt && metalen <= copylen)
1562 memset(ctx->buf->virt, 0, metalen);
1563
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001564 /* copy metadata */
1565 rpra = ctx->buf->virt;
1566 ctx->rpra = rpra;
1567 list = smq_invoke_buf_start(rpra, sc);
1568 pages = smq_phy_page_start(sc, list);
1569 ipage = pages;
1570 args = (uintptr_t)ctx->buf->virt + metalen;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001571 for (i = 0; i < bufs + handles; ++i) {
1572 if (lpra[i].buf.len)
1573 list[i].num = 1;
1574 else
1575 list[i].num = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001576 list[i].pgidx = ipage - pages;
1577 ipage++;
1578 }
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05301579
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001580 /* map ion buffers */
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301581 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_MAP),
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301582 for (i = 0; rpra && lrpra && i < inbufs + outbufs; ++i) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001583 struct fastrpc_mmap *map = ctx->maps[i];
1584 uint64_t buf = ptr_to_uint64(lpra[i].buf.pv);
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301585 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001586
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301587 rpra[i].buf.pv = lrpra[i].buf.pv = 0;
1588 rpra[i].buf.len = lrpra[i].buf.len = len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001589 if (!len)
1590 continue;
1591 if (map) {
1592 struct vm_area_struct *vma;
1593 uintptr_t offset;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301594 uint64_t num = buf_num_pages(buf, len);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001595 int idx = list[i].pgidx;
1596
1597 if (map->attr & FASTRPC_ATTR_NOVA) {
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001598 offset = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001599 } else {
1600 down_read(&current->mm->mmap_sem);
1601 VERIFY(err, NULL != (vma = find_vma(current->mm,
1602 map->va)));
1603 if (err) {
1604 up_read(&current->mm->mmap_sem);
1605 goto bail;
1606 }
1607 offset = buf_page_start(buf) - vma->vm_start;
1608 up_read(&current->mm->mmap_sem);
1609 VERIFY(err, offset < (uintptr_t)map->size);
1610 if (err)
1611 goto bail;
1612 }
1613 pages[idx].addr = map->phys + offset;
1614 pages[idx].size = num << PAGE_SHIFT;
1615 }
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301616 rpra[i].buf.pv = lrpra[i].buf.pv = buf;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001617 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001618 PERF_END);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001619 for (i = bufs; i < bufs + handles; ++i) {
1620 struct fastrpc_mmap *map = ctx->maps[i];
1621
1622 pages[i].addr = map->phys;
1623 pages[i].size = map->size;
1624 }
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301625 if (!me->legacy) {
1626 fdlist = (uint64_t *)&pages[bufs + handles];
1627 for (i = 0; i < M_FDLIST; i++)
1628 fdlist[i] = 0;
1629 crclist = (uint32_t *)&fdlist[M_FDLIST];
1630 memset(crclist, 0, sizeof(uint32_t)*M_CRCLIST);
1631 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001632
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001633 /* copy non ion buffers */
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301634 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_COPY),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001635 rlen = copylen - metalen;
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301636 for (oix = 0; rpra && lrpra && oix < inbufs + outbufs; ++oix) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001637 int i = ctx->overps[oix]->raix;
1638 struct fastrpc_mmap *map = ctx->maps[i];
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301639 size_t mlen;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001640 uint64_t buf;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301641 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001642
1643 if (!len)
1644 continue;
1645 if (map)
1646 continue;
1647 if (ctx->overps[oix]->offset == 0) {
1648 rlen -= ALIGN(args, BALIGN) - args;
1649 args = ALIGN(args, BALIGN);
1650 }
Sathish Ambleyd209c1e2016-12-13 15:27:30 -08001651 mlen = ctx->overps[oix]->mend - ctx->overps[oix]->mstart;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001652 VERIFY(err, rlen >= mlen);
1653 if (err)
1654 goto bail;
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301655 rpra[i].buf.pv = lrpra[i].buf.pv =
1656 (args - ctx->overps[oix]->offset);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001657 pages[list[i].pgidx].addr = ctx->buf->phys -
1658 ctx->overps[oix]->offset +
1659 (copylen - rlen);
1660 pages[list[i].pgidx].addr =
1661 buf_page_start(pages[list[i].pgidx].addr);
1662 buf = rpra[i].buf.pv;
1663 pages[list[i].pgidx].size = buf_num_pages(buf, len) * PAGE_SIZE;
1664 if (i < inbufs) {
1665 K_COPY_FROM_USER(err, kernel, uint64_to_ptr(buf),
1666 lpra[i].buf.pv, len);
1667 if (err)
1668 goto bail;
1669 }
1670 args = args + mlen;
1671 rlen -= mlen;
1672 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001673 PERF_END);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001674
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301675 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_FLUSH),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001676 for (oix = 0; oix < inbufs + outbufs; ++oix) {
1677 int i = ctx->overps[oix]->raix;
1678 struct fastrpc_mmap *map = ctx->maps[i];
1679
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001680 if (map && map->uncached)
1681 continue;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +05301682 if (ctx->fl->sctx->smmu.coherent &&
1683 !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT)))
1684 continue;
1685 if (map && (map->attr & FASTRPC_ATTR_COHERENT))
1686 continue;
1687
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301688 if (rpra && lrpra && rpra[i].buf.len &&
1689 ctx->overps[oix]->mstart) {
Tharun Kumar Merugub67336e2017-08-08 18:56:03 +05301690 if (map && map->handle)
1691 msm_ion_do_cache_op(ctx->fl->apps->client,
1692 map->handle,
1693 uint64_to_ptr(rpra[i].buf.pv),
1694 rpra[i].buf.len,
1695 ION_IOC_CLEAN_INV_CACHES);
1696 else
1697 dmac_flush_range(uint64_to_ptr(rpra[i].buf.pv),
1698 uint64_to_ptr(rpra[i].buf.pv
1699 + rpra[i].buf.len));
1700 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001701 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001702 PERF_END);
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301703 for (i = bufs; rpra && lrpra && i < bufs + handles; i++) {
1704 rpra[i].dma.fd = lrpra[i].dma.fd = ctx->fds[i];
1705 rpra[i].dma.len = lrpra[i].dma.len = (uint32_t)lpra[i].buf.len;
1706 rpra[i].dma.offset = lrpra[i].dma.offset =
1707 (uint32_t)(uintptr_t)lpra[i].buf.pv;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001708 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001709
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001710 bail:
1711 return err;
1712}
1713
1714static int put_args(uint32_t kernel, struct smq_invoke_ctx *ctx,
1715 remote_arg_t *upra)
1716{
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301717 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001718 uint32_t sc = ctx->sc;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001719 struct smq_invoke_buf *list;
1720 struct smq_phy_page *pages;
1721 struct fastrpc_mmap *mmap;
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301722 uint64_t *fdlist = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07001723 uint32_t *crclist = NULL;
1724
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301725 remote_arg64_t *rpra = ctx->lrpra;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001726 int i, inbufs, outbufs, handles;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001727 int err = 0;
1728
1729 inbufs = REMOTE_SCALARS_INBUFS(sc);
1730 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001731 handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc);
1732 list = smq_invoke_buf_start(ctx->rpra, sc);
1733 pages = smq_phy_page_start(sc, list);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301734 if (!me->legacy) {
1735 fdlist = (uint64_t *)(pages + inbufs + outbufs + handles);
1736 crclist = (uint32_t *)(fdlist + M_FDLIST);
1737 }
Sathish Ambleybae51902017-07-03 15:00:49 -07001738
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001739 for (i = inbufs; i < inbufs + outbufs; ++i) {
1740 if (!ctx->maps[i]) {
1741 K_COPY_TO_USER(err, kernel,
1742 ctx->lpra[i].buf.pv,
1743 uint64_to_ptr(rpra[i].buf.pv),
1744 rpra[i].buf.len);
1745 if (err)
1746 goto bail;
1747 } else {
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301748 mutex_lock(&ctx->fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05301749 fastrpc_mmap_free(ctx->maps[i], 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301750 mutex_unlock(&ctx->fl->fl_map_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05301751 ctx->maps[i] = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001752 }
1753 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301754 mutex_lock(&ctx->fl->fl_map_mutex);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301755 if (fdlist && (inbufs + outbufs + handles)) {
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001756 for (i = 0; i < M_FDLIST; i++) {
1757 if (!fdlist[i])
1758 break;
1759 if (!fastrpc_mmap_find(ctx->fl, (int)fdlist[i], 0, 0,
Sathish Ambleyae5ee542017-01-16 22:24:23 -08001760 0, 0, &mmap))
c_mtharu7bd6a422017-10-17 18:15:37 +05301761 fastrpc_mmap_free(mmap, 0);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001762 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001763 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301764 mutex_unlock(&ctx->fl->fl_map_mutex);
Sathish Ambleybae51902017-07-03 15:00:49 -07001765 if (ctx->crc && crclist && rpra)
c_mtharue1a5ce12017-10-13 20:47:09 +05301766 K_COPY_TO_USER(err, kernel, ctx->crc,
Sathish Ambleybae51902017-07-03 15:00:49 -07001767 crclist, M_CRCLIST*sizeof(uint32_t));
1768
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001769 bail:
1770 return err;
1771}
1772
1773static void inv_args_pre(struct smq_invoke_ctx *ctx)
1774{
1775 int i, inbufs, outbufs;
1776 uint32_t sc = ctx->sc;
1777 remote_arg64_t *rpra = ctx->rpra;
1778 uintptr_t end;
1779
1780 inbufs = REMOTE_SCALARS_INBUFS(sc);
1781 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
1782 for (i = inbufs; i < inbufs + outbufs; ++i) {
1783 struct fastrpc_mmap *map = ctx->maps[i];
1784
1785 if (map && map->uncached)
1786 continue;
1787 if (!rpra[i].buf.len)
1788 continue;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +05301789 if (ctx->fl->sctx->smmu.coherent &&
1790 !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT)))
1791 continue;
1792 if (map && (map->attr & FASTRPC_ATTR_COHERENT))
1793 continue;
1794
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001795 if (buf_page_start(ptr_to_uint64((void *)rpra)) ==
1796 buf_page_start(rpra[i].buf.pv))
1797 continue;
Tharun Kumar Merugub67336e2017-08-08 18:56:03 +05301798 if (!IS_CACHE_ALIGNED((uintptr_t)
1799 uint64_to_ptr(rpra[i].buf.pv))) {
1800 if (map && map->handle)
1801 msm_ion_do_cache_op(ctx->fl->apps->client,
1802 map->handle,
1803 uint64_to_ptr(rpra[i].buf.pv),
1804 sizeof(uintptr_t),
1805 ION_IOC_CLEAN_INV_CACHES);
1806 else
1807 dmac_flush_range(
1808 uint64_to_ptr(rpra[i].buf.pv), (char *)
1809 uint64_to_ptr(rpra[i].buf.pv + 1));
1810 }
1811
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001812 end = (uintptr_t)uint64_to_ptr(rpra[i].buf.pv +
1813 rpra[i].buf.len);
Tharun Kumar Merugub67336e2017-08-08 18:56:03 +05301814 if (!IS_CACHE_ALIGNED(end)) {
1815 if (map && map->handle)
1816 msm_ion_do_cache_op(ctx->fl->apps->client,
1817 map->handle,
1818 uint64_to_ptr(end),
1819 sizeof(uintptr_t),
1820 ION_IOC_CLEAN_INV_CACHES);
1821 else
1822 dmac_flush_range((char *)end,
1823 (char *)end + 1);
1824 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001825 }
1826}
1827
1828static void inv_args(struct smq_invoke_ctx *ctx)
1829{
1830 int i, inbufs, outbufs;
1831 uint32_t sc = ctx->sc;
Tharun Kumar Merugub31cc732019-05-07 00:39:43 +05301832 remote_arg64_t *rpra = ctx->lrpra;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001833
1834 inbufs = REMOTE_SCALARS_INBUFS(sc);
1835 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
1836 for (i = inbufs; i < inbufs + outbufs; ++i) {
1837 struct fastrpc_mmap *map = ctx->maps[i];
1838
1839 if (map && map->uncached)
1840 continue;
1841 if (!rpra[i].buf.len)
1842 continue;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +05301843 if (ctx->fl->sctx->smmu.coherent &&
1844 !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT)))
1845 continue;
1846 if (map && (map->attr & FASTRPC_ATTR_COHERENT))
1847 continue;
1848
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001849 if (buf_page_start(ptr_to_uint64((void *)rpra)) ==
1850 buf_page_start(rpra[i].buf.pv)) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001851 continue;
1852 }
1853 if (map && map->handle)
1854 msm_ion_do_cache_op(ctx->fl->apps->client, map->handle,
1855 (char *)uint64_to_ptr(rpra[i].buf.pv),
1856 rpra[i].buf.len, ION_IOC_INV_CACHES);
1857 else
1858 dmac_inv_range((char *)uint64_to_ptr(rpra[i].buf.pv),
1859 (char *)uint64_to_ptr(rpra[i].buf.pv
1860 + rpra[i].buf.len));
1861 }
1862
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001863}
1864
1865static int fastrpc_invoke_send(struct smq_invoke_ctx *ctx,
1866 uint32_t kernel, uint32_t handle)
1867{
1868 struct smq_msg *msg = &ctx->msg;
1869 struct fastrpc_file *fl = ctx->fl;
Mohammed Nayeem Ur Rahmancd836462020-04-01 14:30:33 +05301870 int err = 0, len, cid = -1;
1871 struct fastrpc_channel_ctx *channel_ctx = NULL;
1872
1873 cid = fl->cid;
1874 VERIFY(err, cid >= ADSP_DOMAIN_ID && cid < NUM_CHANNELS);
1875 if (err) {
1876 err = -ECHRNG;
1877 goto bail;
1878 }
1879 channel_ctx = &fl->apps->channel[fl->cid];
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001880
c_mtharue1a5ce12017-10-13 20:47:09 +05301881 VERIFY(err, NULL != channel_ctx->chan);
Mohammed Nayeem Ur Rahmancd836462020-04-01 14:30:33 +05301882 if (err) {
1883 err = -ECHRNG;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001884 goto bail;
Mohammed Nayeem Ur Rahmancd836462020-04-01 14:30:33 +05301885 }
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301886 msg->pid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001887 msg->tid = current->pid;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301888 if (fl->sessionid)
1889 msg->tid |= (1 << SESSION_ID_INDEX);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001890 if (kernel)
1891 msg->pid = 0;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301892 msg->invoke.header.ctx = ctx->ctxid | fl->pd;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001893 msg->invoke.header.handle = handle;
1894 msg->invoke.header.sc = ctx->sc;
1895 msg->invoke.page.addr = ctx->buf ? ctx->buf->phys : 0;
1896 msg->invoke.page.size = buf_page_size(ctx->used);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301897 if (fl->apps->glink) {
1898 if (fl->ssrcount != channel_ctx->ssrcount) {
1899 err = -ECONNRESET;
1900 goto bail;
1901 }
1902 VERIFY(err, channel_ctx->link.port_state ==
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001903 FASTRPC_LINK_CONNECTED);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301904 if (err)
1905 goto bail;
1906 err = glink_tx(channel_ctx->chan,
1907 (void *)&fl->apps->channel[fl->cid], msg, sizeof(*msg),
1908 GLINK_TX_REQ_INTENT);
1909 } else {
1910 spin_lock(&fl->apps->hlock);
1911 len = smd_write((smd_channel_t *)
1912 channel_ctx->chan,
1913 msg, sizeof(*msg));
1914 spin_unlock(&fl->apps->hlock);
1915 VERIFY(err, len == sizeof(*msg));
1916 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001917 bail:
1918 return err;
1919}
1920
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301921static void fastrpc_smd_read_handler(int cid)
1922{
1923 struct fastrpc_apps *me = &gfa;
1924 struct smq_invoke_rsp rsp = {0};
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301925 int ret = 0, err = 0;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301926 uint32_t index;
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301927
1928 do {
1929 ret = smd_read_from_cb(me->channel[cid].chan, &rsp,
1930 sizeof(rsp));
1931 if (ret != sizeof(rsp))
1932 break;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301933
1934 index = (uint32_t)((rsp.ctx & FASTRPC_CTXID_MASK) >> 4);
1935 VERIFY(err, index < FASTRPC_CTX_MAX);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301936 if (err)
1937 goto bail;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301938
1939 VERIFY(err, !IS_ERR_OR_NULL(me->ctxtable[index]));
1940 if (err)
1941 goto bail;
1942
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05301943 VERIFY(err, ((me->ctxtable[index]->ctxid == (rsp.ctx & ~3)) &&
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301944 me->ctxtable[index]->magic == FASTRPC_CTX_MAGIC));
1945 if (err)
1946 goto bail;
1947
1948 context_notify_user(me->ctxtable[index], rsp.retval);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301949 } while (ret == sizeof(rsp));
1950bail:
1951 if (err)
1952 pr_err("adsprpc: invalid response or context\n");
1953
1954}
1955
1956static void smd_event_handler(void *priv, unsigned int event)
1957{
1958 struct fastrpc_apps *me = &gfa;
1959 int cid = (int)(uintptr_t)priv;
1960
1961 switch (event) {
1962 case SMD_EVENT_OPEN:
1963 complete(&me->channel[cid].workport);
1964 break;
1965 case SMD_EVENT_CLOSE:
1966 fastrpc_notify_drivers(me, cid);
1967 break;
1968 case SMD_EVENT_DATA:
1969 fastrpc_smd_read_handler(cid);
1970 break;
1971 }
1972}
1973
1974
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001975static void fastrpc_init(struct fastrpc_apps *me)
1976{
1977 int i;
1978
1979 INIT_HLIST_HEAD(&me->drivers);
Tharun Kumar Merugubcd6fbf2018-01-04 17:49:34 +05301980 INIT_HLIST_HEAD(&me->maps);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001981 spin_lock_init(&me->hlock);
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301982 spin_lock_init(&me->ctxlock);
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05301983 mutex_init(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001984 me->channel = &gcinfo[0];
1985 for (i = 0; i < NUM_CHANNELS; i++) {
1986 init_completion(&me->channel[i].work);
Tharun Kumar Merugu53a8ec92017-07-14 15:52:49 +05301987 init_completion(&me->channel[i].workport);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001988 me->channel[i].sesscount = 0;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05301989 /* All channels are secure by default except CDSP */
1990 me->channel[i].secure = SECURE_CHANNEL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001991 }
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05301992 /* Set CDSP channel to non secure */
1993 me->channel[CDSP_DOMAIN_ID].secure = NON_SECURE_CHANNEL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001994}
1995
1996static int fastrpc_release_current_dsp_process(struct fastrpc_file *fl);
1997
1998static int fastrpc_internal_invoke(struct fastrpc_file *fl, uint32_t mode,
1999 uint32_t kernel,
Sathish Ambleybae51902017-07-03 15:00:49 -07002000 struct fastrpc_ioctl_invoke_crc *inv)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002001{
c_mtharue1a5ce12017-10-13 20:47:09 +05302002 struct smq_invoke_ctx *ctx = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002003 struct fastrpc_ioctl_invoke *invoke = &inv->inv;
Mohammed Nayeem Ur Rahmancd836462020-04-01 14:30:33 +05302004 int err = 0, cid = -1, interrupted = 0;
Maria Yu757199c2017-09-22 16:05:49 +08002005 struct timespec invoket = {0};
Mohammed Nayeem Ur Rahmancd836462020-04-01 14:30:33 +05302006 int64_t *perf_counter = NULL;
2007
2008 cid = fl->cid;
2009 VERIFY(err, cid >= ADSP_DOMAIN_ID && cid < NUM_CHANNELS);
2010 if (err) {
2011 err = -ECHRNG;
2012 goto bail;
2013 }
2014 VERIFY(err, fl->sctx != NULL);
2015 if (err) {
2016 err = -EBADR;
2017 goto bail;
2018 }
2019 perf_counter = getperfcounter(fl, PERF_COUNT);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002020
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002021 if (fl->profile)
2022 getnstimeofday(&invoket);
Tharun Kumar Merugue3edf3e2017-07-27 12:34:07 +05302023
Tharun Kumar Merugucc2e11e2019-02-02 01:22:47 +05302024 if (!kernel) {
2025 VERIFY(err, invoke->handle != FASTRPC_STATIC_HANDLE_KERNEL);
2026 if (err) {
2027 pr_err("adsprpc: ERROR: %s: user application %s trying to send a kernel RPC message to channel %d",
2028 __func__, current->comm, cid);
2029 goto bail;
2030 }
2031 }
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05302032
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002033 if (!kernel) {
2034 VERIFY(err, 0 == context_restore_interrupted(fl, inv,
2035 &ctx));
2036 if (err)
2037 goto bail;
2038 if (fl->sctx->smmu.faults)
2039 err = FASTRPC_ENOSUCH;
2040 if (err)
2041 goto bail;
2042 if (ctx)
2043 goto wait;
2044 }
2045
2046 VERIFY(err, 0 == context_alloc(fl, kernel, inv, &ctx));
2047 if (err)
2048 goto bail;
2049
2050 if (REMOTE_SCALARS_LENGTH(ctx->sc)) {
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302051 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_GETARGS),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002052 VERIFY(err, 0 == get_args(kernel, ctx));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002053 PERF_END);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002054 if (err)
2055 goto bail;
2056 }
2057
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302058 if (!fl->sctx->smmu.coherent) {
2059 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002060 inv_args_pre(ctx);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302061 PERF_END);
2062 }
2063
2064 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_LINK),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002065 VERIFY(err, 0 == fastrpc_invoke_send(ctx, kernel, invoke->handle));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002066 PERF_END);
2067
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002068 if (err)
2069 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002070 wait:
2071 if (kernel)
2072 wait_for_completion(&ctx->work);
2073 else {
2074 interrupted = wait_for_completion_interruptible(&ctx->work);
2075 VERIFY(err, 0 == (err = interrupted));
2076 if (err)
2077 goto bail;
2078 }
Mohammed Nayeem Ur Rahman32ba95d2019-07-26 17:31:37 +05302079 if (ctx->handle)
2080 glink_rx_done(ctx->handle, ctx->ptr, true);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302081 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS),
Sathish Ambleyc432b502017-06-05 12:03:42 -07002082 if (!fl->sctx->smmu.coherent)
2083 inv_args(ctx);
2084 PERF_END);
2085
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002086 VERIFY(err, 0 == (err = ctx->retval));
2087 if (err)
2088 goto bail;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002089
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302090 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_PUTARGS),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002091 VERIFY(err, 0 == put_args(kernel, ctx, invoke->pra));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002092 PERF_END);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002093 if (err)
2094 goto bail;
2095 bail:
2096 if (ctx && interrupted == -ERESTARTSYS)
2097 context_save_interrupted(ctx);
2098 else if (ctx)
2099 context_free(ctx);
2100 if (fl->ssrcount != fl->apps->channel[cid].ssrcount)
2101 err = ECONNRESET;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002102
2103 if (fl->profile && !interrupted) {
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302104 if (invoke->handle != FASTRPC_STATIC_HANDLE_LISTENER) {
2105 int64_t *count = GET_COUNTER(perf_counter, PERF_INVOKE);
2106
2107 if (count)
2108 *count += getnstimediff(&invoket);
2109 }
2110 if (invoke->handle > FASTRPC_STATIC_HANDLE_MAX) {
2111 int64_t *count = GET_COUNTER(perf_counter, PERF_COUNT);
2112
2113 if (count)
2114 *count = *count+1;
2115 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08002116 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002117 return err;
2118}
2119
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05302120static int fastrpc_get_adsp_session(char *name, int *session)
2121{
2122 struct fastrpc_apps *me = &gfa;
2123 int err = 0, i;
2124
2125 for (i = 0; i < NUM_SESSIONS; i++) {
2126 if (!me->channel[0].spd[i].spdname)
2127 continue;
2128 if (!strcmp(name, me->channel[0].spd[i].spdname))
2129 break;
2130 }
2131 VERIFY(err, i < NUM_SESSIONS);
2132 if (err)
2133 goto bail;
2134 *session = i;
2135bail:
2136 return err;
2137}
2138
2139static int fastrpc_mmap_remove_pdr(struct fastrpc_file *fl);
Sathish Ambley36849af2017-02-02 09:35:55 -08002140static int fastrpc_channel_open(struct fastrpc_file *fl);
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05302141static int fastrpc_mmap_remove_ssr(struct fastrpc_file *fl);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002142static int fastrpc_init_process(struct fastrpc_file *fl,
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002143 struct fastrpc_ioctl_init_attrs *uproc)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002144{
2145 int err = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05302146 struct fastrpc_apps *me = &gfa;
Sathish Ambleybae51902017-07-03 15:00:49 -07002147 struct fastrpc_ioctl_invoke_crc ioctl;
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002148 struct fastrpc_ioctl_init *init = &uproc->init;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002149 struct smq_phy_page pages[1];
c_mtharue1a5ce12017-10-13 20:47:09 +05302150 struct fastrpc_mmap *file = NULL, *mem = NULL;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302151 struct fastrpc_buf *imem = NULL;
2152 unsigned long imem_dma_attr = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05302153 char *proc_name = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002154
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302155 VERIFY(err, 0 == (err = fastrpc_channel_open(fl)));
Sathish Ambley36849af2017-02-02 09:35:55 -08002156 if (err)
2157 goto bail;
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302158 if (init->flags == FASTRPC_INIT_ATTACH ||
2159 init->flags == FASTRPC_INIT_ATTACH_SENSORS) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002160 remote_arg_t ra[1];
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05302161 int tgid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002162
2163 ra[0].buf.pv = (void *)&tgid;
2164 ra[0].buf.len = sizeof(tgid);
Tharun Kumar Merugucc2e11e2019-02-02 01:22:47 +05302165 ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002166 ioctl.inv.sc = REMOTE_SCALARS_MAKE(0, 1, 0);
2167 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05302168 ioctl.fds = NULL;
2169 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07002170 ioctl.crc = NULL;
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302171 if (init->flags == FASTRPC_INIT_ATTACH)
2172 fl->pd = 0;
2173 else if (init->flags == FASTRPC_INIT_ATTACH_SENSORS) {
2174 fl->spdname = SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME;
2175 fl->pd = 2;
2176 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002177 VERIFY(err, !(err = fastrpc_internal_invoke(fl,
2178 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
2179 if (err)
2180 goto bail;
2181 } else if (init->flags == FASTRPC_INIT_CREATE) {
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002182 remote_arg_t ra[6];
2183 int fds[6];
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002184 int mflags = 0;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302185 int memlen;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002186 struct {
2187 int pgid;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302188 unsigned int namelen;
2189 unsigned int filelen;
2190 unsigned int pageslen;
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002191 int attrs;
2192 int siglen;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002193 } inbuf;
2194
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05302195 inbuf.pgid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002196 inbuf.namelen = strlen(current->comm) + 1;
2197 inbuf.filelen = init->filelen;
2198 fl->pd = 1;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302199
Tharun Kumar Merugudf852892017-12-07 16:27:37 +05302200 VERIFY(err, access_ok(0, (void __user *)init->file,
2201 init->filelen));
2202 if (err)
2203 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002204 if (init->filelen) {
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302205 mutex_lock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002206 VERIFY(err, !fastrpc_mmap_create(fl, init->filefd, 0,
2207 init->file, init->filelen, mflags, &file));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302208 mutex_unlock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002209 if (err)
2210 goto bail;
2211 }
2212 inbuf.pageslen = 1;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302213
2214 VERIFY(err, !init->mem);
2215 if (err) {
2216 err = -EINVAL;
2217 pr_err("adsprpc: %s: %s: ERROR: donated memory allocated in userspace\n",
2218 current->comm, __func__);
2219 goto bail;
2220 }
2221 memlen = ALIGN(max(1024*1024*3, (int)init->filelen * 4),
2222 1024*1024);
2223 imem_dma_attr = DMA_ATTR_EXEC_MAPPING |
2224 DMA_ATTR_NO_KERNEL_MAPPING |
2225 DMA_ATTR_FORCE_NON_COHERENT;
2226 err = fastrpc_buf_alloc(fl, memlen, imem_dma_attr, 0, 0, &imem);
Tharun Kumar Merugudf852892017-12-07 16:27:37 +05302227 if (err)
2228 goto bail;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302229 fl->init_mem = imem;
2230
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002231 inbuf.pageslen = 1;
2232 ra[0].buf.pv = (void *)&inbuf;
2233 ra[0].buf.len = sizeof(inbuf);
2234 fds[0] = 0;
2235
2236 ra[1].buf.pv = (void *)current->comm;
2237 ra[1].buf.len = inbuf.namelen;
2238 fds[1] = 0;
2239
2240 ra[2].buf.pv = (void *)init->file;
2241 ra[2].buf.len = inbuf.filelen;
2242 fds[2] = init->filefd;
2243
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302244 pages[0].addr = imem->phys;
2245 pages[0].size = imem->size;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002246 ra[3].buf.pv = (void *)pages;
2247 ra[3].buf.len = 1 * sizeof(*pages);
2248 fds[3] = 0;
2249
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002250 inbuf.attrs = uproc->attrs;
2251 ra[4].buf.pv = (void *)&(inbuf.attrs);
2252 ra[4].buf.len = sizeof(inbuf.attrs);
2253 fds[4] = 0;
2254
2255 inbuf.siglen = uproc->siglen;
2256 ra[5].buf.pv = (void *)&(inbuf.siglen);
2257 ra[5].buf.len = sizeof(inbuf.siglen);
2258 fds[5] = 0;
2259
Tharun Kumar Merugucc2e11e2019-02-02 01:22:47 +05302260 ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002261 ioctl.inv.sc = REMOTE_SCALARS_MAKE(6, 4, 0);
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002262 if (uproc->attrs)
2263 ioctl.inv.sc = REMOTE_SCALARS_MAKE(7, 6, 0);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002264 ioctl.inv.pra = ra;
2265 ioctl.fds = fds;
c_mtharue1a5ce12017-10-13 20:47:09 +05302266 ioctl.attrs = NULL;
2267 ioctl.crc = NULL;
2268 VERIFY(err, !(err = fastrpc_internal_invoke(fl,
2269 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
2270 if (err)
2271 goto bail;
2272 } else if (init->flags == FASTRPC_INIT_CREATE_STATIC) {
2273 remote_arg_t ra[3];
2274 uint64_t phys = 0;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302275 size_t size = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05302276 int fds[3];
2277 struct {
2278 int pgid;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302279 unsigned int namelen;
2280 unsigned int pageslen;
c_mtharue1a5ce12017-10-13 20:47:09 +05302281 } inbuf;
2282
2283 if (!init->filelen)
2284 goto bail;
2285
Jeya R26177c92021-04-09 13:22:31 +05302286 proc_name = kzalloc(init->filelen + 1, GFP_KERNEL);
c_mtharue1a5ce12017-10-13 20:47:09 +05302287 VERIFY(err, !IS_ERR_OR_NULL(proc_name));
2288 if (err)
2289 goto bail;
2290 VERIFY(err, 0 == copy_from_user((void *)proc_name,
2291 (void __user *)init->file, init->filelen));
2292 if (err)
2293 goto bail;
2294
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302295 fl->pd = 1;
c_mtharue1a5ce12017-10-13 20:47:09 +05302296 inbuf.pgid = current->tgid;
c_mtharu81a0aa72017-11-07 16:13:21 +05302297 inbuf.namelen = init->filelen;
c_mtharue1a5ce12017-10-13 20:47:09 +05302298 inbuf.pageslen = 0;
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05302299
2300 if (!strcmp(proc_name, "audiopd")) {
2301 fl->spdname = AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME;
2302 VERIFY(err, !fastrpc_mmap_remove_pdr(fl));
Tharun Kumar Merugu35173342018-02-08 16:13:17 +05302303 if (err)
2304 goto bail;
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05302305 }
2306
c_mtharue1a5ce12017-10-13 20:47:09 +05302307 if (!me->staticpd_flags) {
2308 inbuf.pageslen = 1;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302309 mutex_lock(&fl->fl_map_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05302310 VERIFY(err, !fastrpc_mmap_create(fl, -1, 0, init->mem,
2311 init->memlen, ADSP_MMAP_REMOTE_HEAP_ADDR,
2312 &mem));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302313 mutex_unlock(&fl->fl_map_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05302314 if (err)
2315 goto bail;
2316 phys = mem->phys;
2317 size = mem->size;
2318 VERIFY(err, !hyp_assign_phys(phys, (uint64_t)size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05302319 hlosvm, 1, me->channel[fl->cid].rhvm.vmid,
2320 me->channel[fl->cid].rhvm.vmperm,
2321 me->channel[fl->cid].rhvm.vmcount));
c_mtharue1a5ce12017-10-13 20:47:09 +05302322 if (err) {
2323 pr_err("ADSPRPC: hyp_assign_phys fail err %d",
2324 err);
2325 pr_err("map->phys %llx, map->size %d\n",
2326 phys, (int)size);
2327 goto bail;
2328 }
2329 me->staticpd_flags = 1;
2330 }
2331
2332 ra[0].buf.pv = (void *)&inbuf;
2333 ra[0].buf.len = sizeof(inbuf);
2334 fds[0] = 0;
2335
2336 ra[1].buf.pv = (void *)proc_name;
2337 ra[1].buf.len = inbuf.namelen;
2338 fds[1] = 0;
2339
2340 pages[0].addr = phys;
2341 pages[0].size = size;
2342
2343 ra[2].buf.pv = (void *)pages;
2344 ra[2].buf.len = sizeof(*pages);
2345 fds[2] = 0;
Tharun Kumar Merugucc2e11e2019-02-02 01:22:47 +05302346 ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL;
c_mtharue1a5ce12017-10-13 20:47:09 +05302347
2348 ioctl.inv.sc = REMOTE_SCALARS_MAKE(8, 3, 0);
2349 ioctl.inv.pra = ra;
2350 ioctl.fds = NULL;
2351 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07002352 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002353 VERIFY(err, !(err = fastrpc_internal_invoke(fl,
2354 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
2355 if (err)
2356 goto bail;
2357 } else {
2358 err = -ENOTTY;
2359 }
2360bail:
c_mtharud91205a2017-11-07 16:01:06 +05302361 kfree(proc_name);
c_mtharue1a5ce12017-10-13 20:47:09 +05302362 if (err && (init->flags == FASTRPC_INIT_CREATE_STATIC))
2363 me->staticpd_flags = 0;
2364 if (mem && err) {
2365 if (mem->flags == ADSP_MMAP_REMOTE_HEAP_ADDR)
2366 hyp_assign_phys(mem->phys, (uint64_t)mem->size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05302367 me->channel[fl->cid].rhvm.vmid,
2368 me->channel[fl->cid].rhvm.vmcount,
2369 hlosvm, hlosvmperm, 1);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302370 mutex_lock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302371 fastrpc_mmap_free(mem, 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302372 mutex_unlock(&fl->fl_map_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05302373 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302374 if (file) {
2375 mutex_lock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302376 fastrpc_mmap_free(file, 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302377 mutex_unlock(&fl->fl_map_mutex);
2378 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002379 return err;
2380}
2381
2382static int fastrpc_release_current_dsp_process(struct fastrpc_file *fl)
2383{
2384 int err = 0;
Sathish Ambleybae51902017-07-03 15:00:49 -07002385 struct fastrpc_ioctl_invoke_crc ioctl;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002386 remote_arg_t ra[1];
2387 int tgid = 0;
2388
Sathish Ambley36849af2017-02-02 09:35:55 -08002389 VERIFY(err, fl->cid >= 0 && fl->cid < NUM_CHANNELS);
2390 if (err)
2391 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05302392 VERIFY(err, fl->apps->channel[fl->cid].chan != NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002393 if (err)
2394 goto bail;
2395 tgid = fl->tgid;
2396 ra[0].buf.pv = (void *)&tgid;
2397 ra[0].buf.len = sizeof(tgid);
Tharun Kumar Merugucc2e11e2019-02-02 01:22:47 +05302398 ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002399 ioctl.inv.sc = REMOTE_SCALARS_MAKE(1, 1, 0);
2400 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05302401 ioctl.fds = NULL;
2402 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07002403 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002404 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
2405 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
2406bail:
2407 return err;
2408}
2409
2410static int fastrpc_mmap_on_dsp(struct fastrpc_file *fl, uint32_t flags,
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302411 uintptr_t va, uint64_t phys,
2412 size_t size, uintptr_t *raddr)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002413{
Sathish Ambleybae51902017-07-03 15:00:49 -07002414 struct fastrpc_ioctl_invoke_crc ioctl;
c_mtharu63ffc012017-11-16 15:26:56 +05302415 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002416 struct smq_phy_page page;
2417 int num = 1;
2418 remote_arg_t ra[3];
2419 int err = 0;
2420 struct {
2421 int pid;
2422 uint32_t flags;
2423 uintptr_t vaddrin;
2424 int num;
2425 } inargs;
2426 struct {
2427 uintptr_t vaddrout;
2428 } routargs;
2429
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05302430 inargs.pid = fl->tgid;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302431 inargs.vaddrin = (uintptr_t)va;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002432 inargs.flags = flags;
2433 inargs.num = fl->apps->compat ? num * sizeof(page) : num;
2434 ra[0].buf.pv = (void *)&inargs;
2435 ra[0].buf.len = sizeof(inargs);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302436 page.addr = phys;
2437 page.size = size;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002438 ra[1].buf.pv = (void *)&page;
2439 ra[1].buf.len = num * sizeof(page);
2440
2441 ra[2].buf.pv = (void *)&routargs;
2442 ra[2].buf.len = sizeof(routargs);
2443
Tharun Kumar Merugucc2e11e2019-02-02 01:22:47 +05302444 ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002445 if (fl->apps->compat)
2446 ioctl.inv.sc = REMOTE_SCALARS_MAKE(4, 2, 1);
2447 else
2448 ioctl.inv.sc = REMOTE_SCALARS_MAKE(2, 2, 1);
2449 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05302450 ioctl.fds = NULL;
2451 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07002452 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002453 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
2454 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302455 *raddr = (uintptr_t)routargs.vaddrout;
c_mtharue1a5ce12017-10-13 20:47:09 +05302456 if (err)
2457 goto bail;
2458 if (flags == ADSP_MMAP_HEAP_ADDR) {
2459 struct scm_desc desc = {0};
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002460
c_mtharue1a5ce12017-10-13 20:47:09 +05302461 desc.args[0] = TZ_PIL_AUTH_QDSP6_PROC;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302462 desc.args[1] = phys;
2463 desc.args[2] = size;
c_mtharue1a5ce12017-10-13 20:47:09 +05302464 desc.arginfo = SCM_ARGS(3);
2465 err = scm_call2(SCM_SIP_FNID(SCM_SVC_PIL,
2466 TZ_PIL_PROTECT_MEM_SUBSYS_ID), &desc);
2467 } else if (flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302468 VERIFY(err, !hyp_assign_phys(phys, (uint64_t)size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05302469 hlosvm, 1, me->channel[fl->cid].rhvm.vmid,
2470 me->channel[fl->cid].rhvm.vmperm,
2471 me->channel[fl->cid].rhvm.vmcount));
c_mtharue1a5ce12017-10-13 20:47:09 +05302472 if (err)
2473 goto bail;
2474 }
2475bail:
2476 return err;
2477}
2478
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302479static int fastrpc_munmap_on_dsp_rh(struct fastrpc_file *fl, uint64_t phys,
2480 size_t size, uint32_t flags)
c_mtharue1a5ce12017-10-13 20:47:09 +05302481{
2482 int err = 0;
c_mtharu63ffc012017-11-16 15:26:56 +05302483 struct fastrpc_apps *me = &gfa;
Tharun Kumar Merugu72c90252019-08-29 18:36:08 +05302484 int tgid = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05302485 int destVM[1] = {VMID_HLOS};
2486 int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
2487
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302488 if (flags == ADSP_MMAP_HEAP_ADDR) {
c_mtharue1a5ce12017-10-13 20:47:09 +05302489 struct fastrpc_ioctl_invoke_crc ioctl;
2490 struct scm_desc desc = {0};
Tharun Kumar Merugu72c90252019-08-29 18:36:08 +05302491 remote_arg_t ra[2];
2492
c_mtharue1a5ce12017-10-13 20:47:09 +05302493 struct {
2494 uint8_t skey;
2495 } routargs;
2496
Tharun Kumar Merugu72c90252019-08-29 18:36:08 +05302497 if (fl == NULL)
2498 goto bail;
2499 tgid = fl->tgid;
2500 ra[0].buf.pv = (void *)&tgid;
2501 ra[0].buf.len = sizeof(tgid);
2502 ra[1].buf.pv = (void *)&routargs;
2503 ra[1].buf.len = sizeof(routargs);
c_mtharue1a5ce12017-10-13 20:47:09 +05302504
Tharun Kumar Merugucc2e11e2019-02-02 01:22:47 +05302505 ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL;
Tharun Kumar Merugu72c90252019-08-29 18:36:08 +05302506 ioctl.inv.sc = REMOTE_SCALARS_MAKE(9, 1, 1);
c_mtharue1a5ce12017-10-13 20:47:09 +05302507 ioctl.inv.pra = ra;
2508 ioctl.fds = NULL;
2509 ioctl.attrs = NULL;
2510 ioctl.crc = NULL;
Tharun Kumar Merugu72c90252019-08-29 18:36:08 +05302511
c_mtharue1a5ce12017-10-13 20:47:09 +05302512
2513 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
2514 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
Mohammed Nayeem Ur Rahman80f45dc2019-09-23 19:35:19 +05302515 if (err == AEE_EUNSUPPORTED) {
2516 remote_arg_t ra[1];
2517
2518 pr_warn("ADSPRPC:Failed to get security key with updated remote call, falling back to older method");
2519 ra[0].buf.pv = (void *)&routargs;
2520 ra[0].buf.len = sizeof(routargs);
2521 ioctl.inv.sc = REMOTE_SCALARS_MAKE(7, 0, 1);
2522 ioctl.inv.pra = ra;
2523 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
2524 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
2525 }
c_mtharue1a5ce12017-10-13 20:47:09 +05302526 if (err)
2527 goto bail;
Mohammed Nayeem Ur Rahman80f45dc2019-09-23 19:35:19 +05302528
c_mtharue1a5ce12017-10-13 20:47:09 +05302529 desc.args[0] = TZ_PIL_AUTH_QDSP6_PROC;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302530 desc.args[1] = phys;
2531 desc.args[2] = size;
c_mtharue1a5ce12017-10-13 20:47:09 +05302532 desc.args[3] = routargs.skey;
2533 desc.arginfo = SCM_ARGS(4);
2534 err = scm_call2(SCM_SIP_FNID(SCM_SVC_PIL,
2535 TZ_PIL_CLEAR_PROTECT_MEM_SUBSYS_ID), &desc);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302536 } else if (flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
2537 VERIFY(err, !hyp_assign_phys(phys, (uint64_t)size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05302538 me->channel[fl->cid].rhvm.vmid,
2539 me->channel[fl->cid].rhvm.vmcount,
2540 destVM, destVMperm, 1));
c_mtharue1a5ce12017-10-13 20:47:09 +05302541 if (err)
2542 goto bail;
2543 }
2544
2545bail:
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002546 return err;
2547}
2548
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302549static int fastrpc_munmap_on_dsp(struct fastrpc_file *fl, uintptr_t raddr,
2550 uint64_t phys, size_t size, uint32_t flags)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002551{
Sathish Ambleybae51902017-07-03 15:00:49 -07002552 struct fastrpc_ioctl_invoke_crc ioctl;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002553 remote_arg_t ra[1];
2554 int err = 0;
2555 struct {
2556 int pid;
2557 uintptr_t vaddrout;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302558 size_t size;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002559 } inargs;
2560
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05302561 inargs.pid = fl->tgid;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302562 inargs.size = size;
2563 inargs.vaddrout = raddr;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002564 ra[0].buf.pv = (void *)&inargs;
2565 ra[0].buf.len = sizeof(inargs);
2566
Tharun Kumar Merugucc2e11e2019-02-02 01:22:47 +05302567 ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002568 if (fl->apps->compat)
2569 ioctl.inv.sc = REMOTE_SCALARS_MAKE(5, 1, 0);
2570 else
2571 ioctl.inv.sc = REMOTE_SCALARS_MAKE(3, 1, 0);
2572 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05302573 ioctl.fds = NULL;
2574 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07002575 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002576 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
2577 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
c_mtharue1a5ce12017-10-13 20:47:09 +05302578 if (err)
2579 goto bail;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302580 if (flags == ADSP_MMAP_HEAP_ADDR ||
2581 flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
2582 VERIFY(err, !fastrpc_munmap_on_dsp_rh(fl, phys, size, flags));
c_mtharue1a5ce12017-10-13 20:47:09 +05302583 if (err)
2584 goto bail;
2585 }
2586bail:
2587 return err;
2588}
2589
2590static int fastrpc_mmap_remove_ssr(struct fastrpc_file *fl)
2591{
2592 struct fastrpc_mmap *match = NULL, *map = NULL;
2593 struct hlist_node *n = NULL;
2594 int err = 0, ret = 0;
2595 struct fastrpc_apps *me = &gfa;
2596 struct ramdump_segment *ramdump_segments_rh = NULL;
2597
2598 do {
2599 match = NULL;
2600 spin_lock(&me->hlock);
2601 hlist_for_each_entry_safe(map, n, &me->maps, hn) {
2602 match = map;
2603 hlist_del_init(&map->hn);
2604 break;
2605 }
2606 spin_unlock(&me->hlock);
2607
2608 if (match) {
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302609 VERIFY(err, !fastrpc_munmap_on_dsp_rh(fl, match->phys,
2610 match->size, match->flags));
c_mtharue1a5ce12017-10-13 20:47:09 +05302611 if (err)
2612 goto bail;
2613 if (me->channel[0].ramdumpenabled) {
2614 ramdump_segments_rh = kcalloc(1,
2615 sizeof(struct ramdump_segment), GFP_KERNEL);
2616 if (ramdump_segments_rh) {
2617 ramdump_segments_rh->address =
2618 match->phys;
2619 ramdump_segments_rh->size = match->size;
2620 ret = do_elf_ramdump(
2621 me->channel[0].remoteheap_ramdump_dev,
2622 ramdump_segments_rh, 1);
2623 if (ret < 0)
2624 pr_err("ADSPRPC: unable to dump heap");
2625 kfree(ramdump_segments_rh);
2626 }
2627 }
c_mtharu7bd6a422017-10-17 18:15:37 +05302628 fastrpc_mmap_free(match, 0);
c_mtharue1a5ce12017-10-13 20:47:09 +05302629 }
2630 } while (match);
2631bail:
2632 if (err && match)
2633 fastrpc_mmap_add(match);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002634 return err;
2635}
2636
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05302637static int fastrpc_mmap_remove_pdr(struct fastrpc_file *fl)
2638{
2639 struct fastrpc_apps *me = &gfa;
2640 int session = 0, err = 0;
2641
2642 VERIFY(err, !fastrpc_get_adsp_session(
2643 AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME, &session));
2644 if (err)
2645 goto bail;
2646 if (me->channel[fl->cid].spd[session].pdrcount !=
2647 me->channel[fl->cid].spd[session].prevpdrcount) {
2648 if (fastrpc_mmap_remove_ssr(fl))
2649 pr_err("ADSPRPC: SSR: Failed to unmap remote heap\n");
2650 me->channel[fl->cid].spd[session].prevpdrcount =
2651 me->channel[fl->cid].spd[session].pdrcount;
2652 }
2653 if (!me->channel[fl->cid].spd[session].ispdup) {
2654 VERIFY(err, 0);
2655 if (err) {
2656 err = -ENOTCONN;
2657 goto bail;
2658 }
2659 }
2660bail:
2661 return err;
2662}
2663
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002664static int fastrpc_mmap_remove(struct fastrpc_file *fl, uintptr_t va,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302665 size_t len, struct fastrpc_mmap **ppmap);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002666
2667static void fastrpc_mmap_add(struct fastrpc_mmap *map);
2668
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05302669static inline void get_fastrpc_ioctl_mmap_64(
2670 struct fastrpc_ioctl_mmap_64 *mmap64,
2671 struct fastrpc_ioctl_mmap *immap)
2672{
2673 immap->fd = mmap64->fd;
2674 immap->flags = mmap64->flags;
2675 immap->vaddrin = (uintptr_t)mmap64->vaddrin;
2676 immap->size = mmap64->size;
2677}
2678
2679static inline void put_fastrpc_ioctl_mmap_64(
2680 struct fastrpc_ioctl_mmap_64 *mmap64,
2681 struct fastrpc_ioctl_mmap *immap)
2682{
2683 mmap64->vaddrout = (uint64_t)immap->vaddrout;
2684}
2685
2686static inline void get_fastrpc_ioctl_munmap_64(
2687 struct fastrpc_ioctl_munmap_64 *munmap64,
2688 struct fastrpc_ioctl_munmap *imunmap)
2689{
2690 imunmap->vaddrout = (uintptr_t)munmap64->vaddrout;
2691 imunmap->size = munmap64->size;
2692}
2693
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002694static int fastrpc_internal_munmap(struct fastrpc_file *fl,
2695 struct fastrpc_ioctl_munmap *ud)
2696{
2697 int err = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05302698 struct fastrpc_mmap *map = NULL;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302699 struct fastrpc_buf *rbuf = NULL, *free = NULL;
2700 struct hlist_node *n;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002701
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302702 mutex_lock(&fl->map_mutex);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302703
2704 spin_lock(&fl->hlock);
2705 hlist_for_each_entry_safe(rbuf, n, &fl->remote_bufs, hn_rem) {
2706 if (rbuf->raddr && (rbuf->flags == ADSP_MMAP_ADD_PAGES)) {
2707 if ((rbuf->raddr == ud->vaddrout) &&
2708 (rbuf->size == ud->size)) {
2709 free = rbuf;
2710 break;
2711 }
2712 }
2713 }
2714 spin_unlock(&fl->hlock);
2715
2716 if (free) {
2717 VERIFY(err, !fastrpc_munmap_on_dsp(fl, free->raddr,
2718 free->phys, free->size, free->flags));
2719 if (err)
2720 goto bail;
2721 fastrpc_buf_free(rbuf, 0);
2722 mutex_unlock(&fl->map_mutex);
2723 return err;
2724 }
2725
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302726 mutex_lock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002727 VERIFY(err, !fastrpc_mmap_remove(fl, ud->vaddrout, ud->size, &map));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302728 mutex_unlock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002729 if (err)
2730 goto bail;
Vamsi krishna Gattupalli3fed6e32020-12-02 12:59:03 +05302731 if (map) {
2732 VERIFY(err, !fastrpc_munmap_on_dsp(fl, map->raddr,
2733 map->phys, map->size, map->flags));
2734 if (err)
2735 goto bail;
2736 mutex_lock(&fl->fl_map_mutex);
2737 fastrpc_mmap_free(map, 0);
2738 mutex_unlock(&fl->fl_map_mutex);
2739 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002740bail:
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302741 if (err && map) {
2742 mutex_lock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002743 fastrpc_mmap_add(map);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302744 mutex_unlock(&fl->fl_map_mutex);
2745 }
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302746 mutex_unlock(&fl->map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002747 return err;
2748}
2749
c_mtharu7bd6a422017-10-17 18:15:37 +05302750static int fastrpc_internal_munmap_fd(struct fastrpc_file *fl,
2751 struct fastrpc_ioctl_munmap_fd *ud) {
2752 int err = 0;
2753 struct fastrpc_mmap *map = NULL;
2754
2755 VERIFY(err, (fl && ud));
2756 if (err)
2757 goto bail;
Mohammed Nayeem Ur Rahmanfc548a52020-01-07 17:07:55 +05302758 mutex_lock(&fl->map_mutex);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302759 mutex_lock(&fl->fl_map_mutex);
Tharun Kumar Merugu09fc6152018-02-16 13:13:12 +05302760 if (fastrpc_mmap_find(fl, ud->fd, ud->va, ud->len, 0, 0, &map)) {
2761 pr_err("adsprpc: mapping not found to unmap %d va %llx %x\n",
c_mtharu7bd6a422017-10-17 18:15:37 +05302762 ud->fd, (unsigned long long)ud->va,
2763 (unsigned int)ud->len);
2764 err = -1;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302765 mutex_unlock(&fl->fl_map_mutex);
Mohammed Nayeem Ur Rahmanfc548a52020-01-07 17:07:55 +05302766 mutex_unlock(&fl->map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302767 goto bail;
2768 }
2769 if (map)
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302770 fastrpc_mmap_free(map, 0);
2771 mutex_unlock(&fl->fl_map_mutex);
Mohammed Nayeem Ur Rahmanfc548a52020-01-07 17:07:55 +05302772 mutex_unlock(&fl->map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302773bail:
2774 return err;
2775}
2776
2777
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002778static int fastrpc_internal_mmap(struct fastrpc_file *fl,
2779 struct fastrpc_ioctl_mmap *ud)
2780{
2781
c_mtharue1a5ce12017-10-13 20:47:09 +05302782 struct fastrpc_mmap *map = NULL;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302783 struct fastrpc_buf *rbuf = NULL;
2784 unsigned long dma_attr = 0;
2785 uintptr_t raddr = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002786 int err = 0;
2787
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302788 mutex_lock(&fl->map_mutex);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302789 if (ud->flags == ADSP_MMAP_ADD_PAGES) {
2790 if (ud->vaddrin) {
2791 err = -EINVAL;
2792 pr_err("adsprpc: %s: %s: ERROR: adding user allocated pages is not supported\n",
2793 current->comm, __func__);
2794 goto bail;
2795 }
2796 dma_attr = DMA_ATTR_EXEC_MAPPING |
2797 DMA_ATTR_NO_KERNEL_MAPPING |
2798 DMA_ATTR_FORCE_NON_COHERENT;
2799 err = fastrpc_buf_alloc(fl, ud->size, dma_attr, ud->flags,
2800 1, &rbuf);
2801 if (err)
2802 goto bail;
Tharun Kumar Merugu0d0b69e2018-09-14 22:30:58 +05302803 err = fastrpc_mmap_on_dsp(fl, ud->flags, 0,
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302804 rbuf->phys, rbuf->size, &raddr);
2805 if (err)
2806 goto bail;
2807 rbuf->raddr = raddr;
2808 } else {
Tharun Kumar Merugu0d0b69e2018-09-14 22:30:58 +05302809
2810 uintptr_t va_to_dsp;
2811
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302812 mutex_lock(&fl->fl_map_mutex);
2813 if (!fastrpc_mmap_find(fl, ud->fd, (uintptr_t)ud->vaddrin,
2814 ud->size, ud->flags, 1, &map)) {
Mohammed Nayeem Ur Rahmanaf5f6102019-10-09 13:36:52 +05302815 ud->vaddrout = map->raddr;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302816 mutex_unlock(&fl->fl_map_mutex);
2817 mutex_unlock(&fl->map_mutex);
2818 return 0;
2819 }
Tharun Kumar Merugu0d0b69e2018-09-14 22:30:58 +05302820
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302821 VERIFY(err, !fastrpc_mmap_create(fl, ud->fd, 0,
2822 (uintptr_t)ud->vaddrin, ud->size,
2823 ud->flags, &map));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302824 mutex_unlock(&fl->fl_map_mutex);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302825 if (err)
2826 goto bail;
Tharun Kumar Merugu0d0b69e2018-09-14 22:30:58 +05302827
2828 if (ud->flags == ADSP_MMAP_HEAP_ADDR ||
2829 ud->flags == ADSP_MMAP_REMOTE_HEAP_ADDR)
2830 va_to_dsp = 0;
2831 else
2832 va_to_dsp = (uintptr_t)map->va;
2833 VERIFY(err, 0 == fastrpc_mmap_on_dsp(fl, ud->flags, va_to_dsp,
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05302834 map->phys, map->size, &raddr));
2835 if (err)
2836 goto bail;
2837 map->raddr = raddr;
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302838 }
Mohammed Nayeem Ur Rahman3ac5d322018-09-24 13:54:08 +05302839 ud->vaddrout = raddr;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002840 bail:
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302841 if (err && map) {
2842 mutex_lock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302843 fastrpc_mmap_free(map, 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302844 mutex_unlock(&fl->fl_map_mutex);
2845 }
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302846 mutex_unlock(&fl->map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002847 return err;
2848}
2849
2850static void fastrpc_channel_close(struct kref *kref)
2851{
2852 struct fastrpc_apps *me = &gfa;
2853 struct fastrpc_channel_ctx *ctx;
2854 int cid;
2855
2856 ctx = container_of(kref, struct fastrpc_channel_ctx, kref);
2857 cid = ctx - &gcinfo[0];
Mohammed Nayeem Ur Rahmana967be62019-09-23 20:56:15 +05302858 if (me->glink) {
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05302859 fastrpc_glink_close(ctx->chan, cid);
Mohammed Nayeem Ur Rahmana967be62019-09-23 20:56:15 +05302860 ctx->chan = NULL;
2861 }
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302862 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002863 pr_info("'closed /dev/%s c %d %d'\n", gcinfo[cid].name,
2864 MAJOR(me->dev_no), cid);
2865}
2866
2867static void fastrpc_context_list_dtor(struct fastrpc_file *fl);
2868
2869static int fastrpc_session_alloc_locked(struct fastrpc_channel_ctx *chan,
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302870 int secure, int sharedcb, struct fastrpc_session_ctx **session)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002871{
2872 struct fastrpc_apps *me = &gfa;
2873 int idx = 0, err = 0;
2874
2875 if (chan->sesscount) {
2876 for (idx = 0; idx < chan->sesscount; ++idx) {
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302877 if ((sharedcb && chan->session[idx].smmu.sharedcb) ||
2878 (!chan->session[idx].used &&
2879 chan->session[idx].smmu.secure
2880 == secure && !sharedcb)) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002881 chan->session[idx].used = 1;
2882 break;
2883 }
2884 }
2885 VERIFY(err, idx < chan->sesscount);
2886 if (err)
2887 goto bail;
2888 chan->session[idx].smmu.faults = 0;
2889 } else {
2890 VERIFY(err, me->dev != NULL);
2891 if (err)
2892 goto bail;
2893 chan->session[0].dev = me->dev;
c_mtharue1a5ce12017-10-13 20:47:09 +05302894 chan->session[0].smmu.dev = me->dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002895 }
2896
2897 *session = &chan->session[idx];
2898 bail:
2899 return err;
2900}
2901
c_mtharue1a5ce12017-10-13 20:47:09 +05302902static bool fastrpc_glink_notify_rx_intent_req(void *h, const void *priv,
2903 size_t size)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002904{
2905 if (glink_queue_rx_intent(h, NULL, size))
2906 return false;
2907 return true;
2908}
2909
c_mtharue1a5ce12017-10-13 20:47:09 +05302910static void fastrpc_glink_notify_tx_done(void *handle, const void *priv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002911 const void *pkt_priv, const void *ptr)
2912{
2913}
2914
c_mtharue1a5ce12017-10-13 20:47:09 +05302915static void fastrpc_glink_notify_rx(void *handle, const void *priv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002916 const void *pkt_priv, const void *ptr, size_t size)
2917{
2918 struct smq_invoke_rsp *rsp = (struct smq_invoke_rsp *)ptr;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05302919 struct fastrpc_apps *me = &gfa;
2920 uint32_t index;
c_mtharufdac6892017-10-12 13:09:01 +05302921 int err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002922
c_mtharufdac6892017-10-12 13:09:01 +05302923 VERIFY(err, (rsp && size >= sizeof(*rsp)));
2924 if (err)
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302925 goto bail;
2926
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05302927 index = (uint32_t)((rsp->ctx & FASTRPC_CTXID_MASK) >> 4);
2928 VERIFY(err, index < FASTRPC_CTX_MAX);
c_mtharufdac6892017-10-12 13:09:01 +05302929 if (err)
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302930 goto bail;
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302931
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05302932 VERIFY(err, !IS_ERR_OR_NULL(me->ctxtable[index]));
2933 if (err)
2934 goto bail;
2935
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302936 VERIFY(err, ((me->ctxtable[index]->ctxid == (rsp->ctx & ~3)) &&
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05302937 me->ctxtable[index]->magic == FASTRPC_CTX_MAGIC));
2938 if (err)
2939 goto bail;
2940
Mohammed Nayeem Ur Rahman32ba95d2019-07-26 17:31:37 +05302941 me->ctxtable[index]->handle = handle;
2942 me->ctxtable[index]->ptr = ptr;
2943
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05302944 context_notify_user(me->ctxtable[index], rsp->retval);
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302945bail:
c_mtharufdac6892017-10-12 13:09:01 +05302946 if (err)
2947 pr_err("adsprpc: invalid response or context\n");
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002948}
2949
c_mtharue1a5ce12017-10-13 20:47:09 +05302950static void fastrpc_glink_notify_state(void *handle, const void *priv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002951 unsigned int event)
2952{
2953 struct fastrpc_apps *me = &gfa;
2954 int cid = (int)(uintptr_t)priv;
2955 struct fastrpc_glink_info *link;
2956
2957 if (cid < 0 || cid >= NUM_CHANNELS)
2958 return;
2959 link = &me->channel[cid].link;
2960 switch (event) {
2961 case GLINK_CONNECTED:
2962 link->port_state = FASTRPC_LINK_CONNECTED;
Tharun Kumar Merugu53a8ec92017-07-14 15:52:49 +05302963 complete(&me->channel[cid].workport);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002964 break;
2965 case GLINK_LOCAL_DISCONNECTED:
2966 link->port_state = FASTRPC_LINK_DISCONNECTED;
2967 break;
2968 case GLINK_REMOTE_DISCONNECTED:
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002969 break;
2970 default:
2971 break;
2972 }
2973}
2974
2975static int fastrpc_session_alloc(struct fastrpc_channel_ctx *chan, int secure,
2976 struct fastrpc_session_ctx **session)
2977{
2978 int err = 0;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302979 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002980
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302981 mutex_lock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002982 if (!*session)
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302983 err = fastrpc_session_alloc_locked(chan, secure, 0, session);
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302984 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002985 return err;
2986}
2987
2988static void fastrpc_session_free(struct fastrpc_channel_ctx *chan,
2989 struct fastrpc_session_ctx *session)
2990{
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302991 struct fastrpc_apps *me = &gfa;
2992
2993 mutex_lock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002994 session->used = 0;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302995 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002996}
2997
2998static int fastrpc_file_free(struct fastrpc_file *fl)
2999{
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05303000 struct hlist_node *n = NULL;
Tharun Kumar Merugu3e966762018-04-04 10:56:44 +05303001 struct fastrpc_mmap *map = NULL, *lmap = NULL;
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05303002 struct fastrpc_perf *perf = NULL, *fperf = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003003 int cid;
3004
3005 if (!fl)
3006 return 0;
3007 cid = fl->cid;
3008
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05303009 (void)fastrpc_release_current_dsp_process(fl);
3010
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003011 spin_lock(&fl->apps->hlock);
3012 hlist_del_init(&fl->hn);
3013 spin_unlock(&fl->apps->hlock);
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303014 kfree(fl->debug_buf);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003015
Sathish Ambleyd7fbcbb2017-03-08 10:55:48 -08003016 if (!fl->sctx) {
3017 kfree(fl);
3018 return 0;
3019 }
tharun kumar9f899ea2017-07-03 17:07:03 +05303020 spin_lock(&fl->hlock);
3021 fl->file_close = 1;
3022 spin_unlock(&fl->hlock);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303023 if (!IS_ERR_OR_NULL(fl->init_mem))
3024 fastrpc_buf_free(fl->init_mem, 0);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003025 fastrpc_context_list_dtor(fl);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303026 fastrpc_cached_buf_list_free(fl);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05303027 mutex_lock(&fl->fl_map_mutex);
Tharun Kumar Merugu3e966762018-04-04 10:56:44 +05303028 do {
3029 lmap = NULL;
3030 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
3031 hlist_del_init(&map->hn);
3032 lmap = map;
3033 break;
3034 }
3035 fastrpc_mmap_free(lmap, 1);
3036 } while (lmap);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05303037 mutex_unlock(&fl->fl_map_mutex);
Tharun Kumar Merugu35173342018-02-08 16:13:17 +05303038 if (fl->refcount && (fl->ssrcount == fl->apps->channel[cid].ssrcount))
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003039 kref_put_mutex(&fl->apps->channel[cid].kref,
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303040 fastrpc_channel_close, &fl->apps->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003041 if (fl->sctx)
3042 fastrpc_session_free(&fl->apps->channel[cid], fl->sctx);
3043 if (fl->secsctx)
3044 fastrpc_session_free(&fl->apps->channel[cid], fl->secsctx);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05303045
3046 mutex_lock(&fl->perf_mutex);
3047 do {
3048 struct hlist_node *pn = NULL;
3049
3050 fperf = NULL;
3051 hlist_for_each_entry_safe(perf, pn, &fl->perf, hn) {
3052 hlist_del_init(&perf->hn);
3053 fperf = perf;
3054 break;
3055 }
3056 kfree(fperf);
3057 } while (fperf);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303058 fastrpc_remote_buf_list_free(fl);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05303059 mutex_unlock(&fl->perf_mutex);
3060 mutex_destroy(&fl->perf_mutex);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05303061 mutex_destroy(&fl->fl_map_mutex);
Tharun Kumar Merugu8714e642018-05-17 15:21:08 +05303062 mutex_destroy(&fl->map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003063 kfree(fl);
3064 return 0;
3065}
3066
3067static int fastrpc_device_release(struct inode *inode, struct file *file)
3068{
3069 struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data;
3070
3071 if (fl) {
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303072 if (fl->qos_request && pm_qos_request_active(&fl->pm_qos_req))
3073 pm_qos_remove_request(&fl->pm_qos_req);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003074 if (fl->debugfs_file != NULL)
3075 debugfs_remove(fl->debugfs_file);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003076 fastrpc_file_free(fl);
c_mtharue1a5ce12017-10-13 20:47:09 +05303077 file->private_data = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003078 }
3079 return 0;
3080}
3081
3082static void fastrpc_link_state_handler(struct glink_link_state_cb_info *cb_info,
3083 void *priv)
3084{
3085 struct fastrpc_apps *me = &gfa;
3086 int cid = (int)((uintptr_t)priv);
3087 struct fastrpc_glink_info *link;
3088
3089 if (cid < 0 || cid >= NUM_CHANNELS)
3090 return;
3091
3092 link = &me->channel[cid].link;
3093 switch (cb_info->link_state) {
3094 case GLINK_LINK_STATE_UP:
3095 link->link_state = FASTRPC_LINK_STATE_UP;
3096 complete(&me->channel[cid].work);
3097 break;
3098 case GLINK_LINK_STATE_DOWN:
3099 link->link_state = FASTRPC_LINK_STATE_DOWN;
3100 break;
3101 default:
3102 pr_err("adsprpc: unknown link state %d\n", cb_info->link_state);
3103 break;
3104 }
3105}
3106
3107static int fastrpc_glink_register(int cid, struct fastrpc_apps *me)
3108{
3109 int err = 0;
3110 struct fastrpc_glink_info *link;
3111
3112 VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS));
3113 if (err)
3114 goto bail;
3115
3116 link = &me->channel[cid].link;
3117 if (link->link_notify_handle != NULL)
3118 goto bail;
3119
3120 link->link_info.glink_link_state_notif_cb = fastrpc_link_state_handler;
3121 link->link_notify_handle = glink_register_link_state_cb(
3122 &link->link_info,
3123 (void *)((uintptr_t)cid));
3124 VERIFY(err, !IS_ERR_OR_NULL(me->channel[cid].link.link_notify_handle));
3125 if (err) {
3126 link->link_notify_handle = NULL;
3127 goto bail;
3128 }
3129 VERIFY(err, wait_for_completion_timeout(&me->channel[cid].work,
3130 RPC_TIMEOUT));
3131bail:
3132 return err;
3133}
3134
3135static void fastrpc_glink_close(void *chan, int cid)
3136{
3137 int err = 0;
3138 struct fastrpc_glink_info *link;
3139
3140 VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS));
3141 if (err)
3142 return;
3143 link = &gfa.channel[cid].link;
3144
c_mtharu314a4202017-11-15 22:09:17 +05303145 if (link->port_state == FASTRPC_LINK_CONNECTED ||
3146 link->port_state == FASTRPC_LINK_REMOTE_DISCONNECTING) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003147 link->port_state = FASTRPC_LINK_DISCONNECTING;
3148 glink_close(chan);
3149 }
3150}
3151
3152static int fastrpc_glink_open(int cid)
3153{
3154 int err = 0;
3155 void *handle = NULL;
3156 struct fastrpc_apps *me = &gfa;
3157 struct glink_open_config *cfg;
3158 struct fastrpc_glink_info *link;
3159
3160 VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS));
3161 if (err)
3162 goto bail;
3163 link = &me->channel[cid].link;
3164 cfg = &me->channel[cid].link.cfg;
3165 VERIFY(err, (link->link_state == FASTRPC_LINK_STATE_UP));
3166 if (err)
3167 goto bail;
3168
Tharun Kumar Meruguca0db5262017-05-10 12:53:12 +05303169 VERIFY(err, (link->port_state == FASTRPC_LINK_DISCONNECTED));
3170 if (err)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003171 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003172
3173 link->port_state = FASTRPC_LINK_CONNECTING;
3174 cfg->priv = (void *)(uintptr_t)cid;
3175 cfg->edge = gcinfo[cid].link.link_info.edge;
3176 cfg->transport = gcinfo[cid].link.link_info.transport;
3177 cfg->name = FASTRPC_GLINK_GUID;
3178 cfg->notify_rx = fastrpc_glink_notify_rx;
3179 cfg->notify_tx_done = fastrpc_glink_notify_tx_done;
3180 cfg->notify_state = fastrpc_glink_notify_state;
3181 cfg->notify_rx_intent_req = fastrpc_glink_notify_rx_intent_req;
3182 handle = glink_open(cfg);
3183 VERIFY(err, !IS_ERR_OR_NULL(handle));
c_mtharu6e1d26b2017-10-09 16:05:24 +05303184 if (err) {
3185 if (link->port_state == FASTRPC_LINK_CONNECTING)
3186 link->port_state = FASTRPC_LINK_DISCONNECTED;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003187 goto bail;
c_mtharu6e1d26b2017-10-09 16:05:24 +05303188 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003189 me->channel[cid].chan = handle;
3190bail:
3191 return err;
3192}
3193
Sathish Ambley1ca68232017-01-19 10:32:55 -08003194static int fastrpc_debugfs_open(struct inode *inode, struct file *filp)
3195{
3196 filp->private_data = inode->i_private;
3197 return 0;
3198}
3199
3200static ssize_t fastrpc_debugfs_read(struct file *filp, char __user *buffer,
3201 size_t count, loff_t *position)
3202{
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303203 struct fastrpc_apps *me = &gfa;
Sathish Ambley1ca68232017-01-19 10:32:55 -08003204 struct fastrpc_file *fl = filp->private_data;
3205 struct hlist_node *n;
c_mtharue1a5ce12017-10-13 20:47:09 +05303206 struct fastrpc_buf *buf = NULL;
3207 struct fastrpc_mmap *map = NULL;
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303208 struct fastrpc_mmap *gmaps = NULL;
c_mtharue1a5ce12017-10-13 20:47:09 +05303209 struct smq_invoke_ctx *ictx = NULL;
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303210 struct fastrpc_channel_ctx *chan = NULL;
Sathish Ambley1ca68232017-01-19 10:32:55 -08003211 unsigned int len = 0;
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303212 int i, j, sess_used = 0, ret = 0;
Sathish Ambley1ca68232017-01-19 10:32:55 -08003213 char *fileinfo = NULL;
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303214 char single_line[UL_SIZE] = "----------------";
3215 char title[UL_SIZE] = "=========================";
Sathish Ambley1ca68232017-01-19 10:32:55 -08003216
3217 fileinfo = kzalloc(DEBUGFS_SIZE, GFP_KERNEL);
3218 if (!fileinfo)
3219 goto bail;
3220 if (fl == NULL) {
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303221 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3222 "\n%s %s %s\n", title, " CHANNEL INFO ", title);
3223 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3224 "%-8s|%-9s|%-9s|%-14s|%-9s|%-13s\n",
3225 "susbsys", "refcount", "sesscount", "issubsystemup",
3226 "ssrcount", "session_used");
3227 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3228 "-%s%s%s%s-\n", single_line, single_line,
3229 single_line, single_line);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003230 for (i = 0; i < NUM_CHANNELS; i++) {
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303231 sess_used = 0;
Sathish Ambley1ca68232017-01-19 10:32:55 -08003232 chan = &gcinfo[i];
3233 len += scnprintf(fileinfo + len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303234 DEBUGFS_SIZE - len, "%-8s", chan->subsys);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003235 len += scnprintf(fileinfo + len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303236 DEBUGFS_SIZE - len, "|%-9d",
3237 chan->kref.refcount.counter);
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05303238 len += scnprintf(fileinfo + len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303239 DEBUGFS_SIZE - len, "|%-9d",
3240 chan->sesscount);
3241 len += scnprintf(fileinfo + len,
3242 DEBUGFS_SIZE - len, "|%-14d",
3243 chan->issubsystemup);
3244 len += scnprintf(fileinfo + len,
3245 DEBUGFS_SIZE - len, "|%-9d",
3246 chan->ssrcount);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003247 for (j = 0; j < chan->sesscount; j++) {
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303248 sess_used += chan->session[j].used;
3249 }
3250 len += scnprintf(fileinfo + len,
3251 DEBUGFS_SIZE - len, "|%-13d\n", sess_used);
3252
3253 }
3254 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3255 "\n%s%s%s\n", "=============",
3256 " CMA HEAP ", "==============");
3257 len += scnprintf(fileinfo + len,
3258 DEBUGFS_SIZE - len, "%-20s|%-20s\n", "addr", "size");
3259 len += scnprintf(fileinfo + len,
3260 DEBUGFS_SIZE - len, "--%s%s---\n",
3261 single_line, single_line);
3262 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3263 "0x%-18llX", me->range.addr);
3264 len += scnprintf(fileinfo + len,
3265 DEBUGFS_SIZE - len, "|0x%-18llX\n", me->range.size);
3266 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3267 "\n==========%s %s %s===========\n",
3268 title, " GMAPS ", title);
3269 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3270 "%-20s|%-20s|%-20s|%-20s\n",
3271 "fd", "phys", "size", "va");
3272 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3273 "%s%s%s%s%s\n", single_line, single_line,
3274 single_line, single_line, single_line);
3275 hlist_for_each_entry_safe(gmaps, n, &me->maps, hn) {
3276 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3277 "%-20d|0x%-18llX|0x%-18X|0x%-20lX\n\n",
3278 gmaps->fd, gmaps->phys,
3279 (uint32_t)gmaps->size,
3280 gmaps->va);
3281 }
3282 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3283 "%-20s|%-20s|%-20s|%-20s\n",
3284 "len", "refs", "raddr", "flags");
3285 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3286 "%s%s%s%s%s\n", single_line, single_line,
3287 single_line, single_line, single_line);
3288 hlist_for_each_entry_safe(gmaps, n, &me->maps, hn) {
3289 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3290 "0x%-18X|%-20d|%-20lu|%-20u\n",
3291 (uint32_t)gmaps->len, gmaps->refs,
3292 gmaps->raddr, gmaps->flags);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003293 }
3294 } else {
3295 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303296 "\n%s %13s %d\n", "cid", ":", fl->cid);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003297 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303298 "%s %12s %d\n", "tgid", ":", fl->tgid);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003299 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303300 "%s %7s %d\n", "sessionid", ":", fl->sessionid);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003301 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303302 "%s %8s %d\n", "ssrcount", ":", fl->ssrcount);
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05303303 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303304 "%s %8s %d\n", "refcount", ":", fl->refcount);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003305 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303306 "%s %14s %d\n", "pd", ":", fl->pd);
3307 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3308 "%s %9s %s\n", "spdname", ":", fl->spdname);
3309 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3310 "%s %6s %d\n", "file_close", ":", fl->file_close);
3311 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3312 "%s %8s %d\n", "sharedcb", ":", fl->sharedcb);
3313 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3314 "%s %9s %d\n", "profile", ":", fl->profile);
3315 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3316 "%s %3s %d\n", "smmu.coherent", ":",
3317 fl->sctx->smmu.coherent);
3318 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3319 "%s %4s %d\n", "smmu.enabled", ":",
3320 fl->sctx->smmu.enabled);
3321 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3322 "%s %9s %d\n", "smmu.cb", ":", fl->sctx->smmu.cb);
3323 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3324 "%s %5s %d\n", "smmu.secure", ":",
3325 fl->sctx->smmu.secure);
3326 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3327 "%s %5s %d\n", "smmu.faults", ":",
3328 fl->sctx->smmu.faults);
3329 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3330 "%s %s %d\n", "link.link_state",
3331 ":", *&me->channel[fl->cid].link.link_state);
3332
3333 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3334 "\n=======%s %s %s======\n", title,
3335 " LIST OF MAPS ", title);
3336
3337 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3338 "%-20s|%-20s|%-20s\n", "va", "phys", "size");
3339 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3340 "%s%s%s%s%s\n",
3341 single_line, single_line, single_line,
3342 single_line, single_line);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003343 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303344 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3345 "0x%-20lX|0x%-20llX|0x%-20zu\n\n",
3346 map->va, map->phys,
3347 map->size);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003348 }
3349 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303350 "%-20s|%-20s|%-20s|%-20s\n",
3351 "len", "refs",
3352 "raddr", "uncached");
3353 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3354 "%s%s%s%s%s\n",
3355 single_line, single_line, single_line,
3356 single_line, single_line);
3357 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
3358 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3359 "%-20zu|%-20d|0x%-20lX|%-20d\n\n",
3360 map->len, map->refs, map->raddr,
3361 map->uncached);
3362 }
3363 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3364 "%-20s|%-20s\n", "secure", "attr");
3365 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3366 "%s%s%s%s%s\n",
3367 single_line, single_line, single_line,
3368 single_line, single_line);
3369 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
3370 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3371 "%-20d|0x%-20lX\n\n",
3372 map->secure, map->attr);
3373 }
3374 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303375 "%s %d\n\n",
3376 "KERNEL MEMORY ALLOCATION:", 1);
3377 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303378 "\n======%s %s %s======\n", title,
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303379 " LIST OF CACHED BUFS ", title);
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303380 spin_lock(&fl->hlock);
3381 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303382 "%-19s|%-19s|%-19s|%-19s\n",
3383 "virt", "phys", "size", "dma_attr");
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303384 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3385 "%s%s%s%s%s\n", single_line, single_line,
3386 single_line, single_line, single_line);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303387 hlist_for_each_entry_safe(buf, n, &fl->cached_bufs, hn) {
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303388 len += scnprintf(fileinfo + len,
3389 DEBUGFS_SIZE - len,
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303390 "0x%-17p|0x%-17llX|%-19zu|0x%-17lX\n",
3391 buf->virt, (uint64_t)buf->phys, buf->size,
3392 buf->dma_attr);
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303393 }
3394 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3395 "\n%s %s %s\n", title,
3396 " LIST OF PENDING SMQCONTEXTS ", title);
3397
3398 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3399 "%-20s|%-10s|%-10s|%-10s|%-20s\n",
3400 "sc", "pid", "tgid", "used", "ctxid");
3401 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3402 "%s%s%s%s%s\n", single_line, single_line,
3403 single_line, single_line, single_line);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003404 hlist_for_each_entry_safe(ictx, n, &fl->clst.pending, hn) {
3405 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303406 "0x%-18X|%-10d|%-10d|%-10zu|0x%-20llX\n\n",
3407 ictx->sc, ictx->pid, ictx->tgid,
3408 ictx->used, ictx->ctxid);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003409 }
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303410
Sathish Ambley1ca68232017-01-19 10:32:55 -08003411 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303412 "\n%s %s %s\n", title,
3413 " LIST OF INTERRUPTED SMQCONTEXTS ", title);
3414
3415 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3416 "%-20s|%-10s|%-10s|%-10s|%-20s\n",
3417 "sc", "pid", "tgid", "used", "ctxid");
3418 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3419 "%s%s%s%s%s\n", single_line, single_line,
3420 single_line, single_line, single_line);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003421 hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) {
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303422 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
3423 "%-20u|%-20d|%-20d|%-20zu|0x%-20llX\n\n",
3424 ictx->sc, ictx->pid, ictx->tgid,
3425 ictx->used, ictx->ctxid);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003426 }
3427 spin_unlock(&fl->hlock);
3428 }
3429 if (len > DEBUGFS_SIZE)
3430 len = DEBUGFS_SIZE;
3431 ret = simple_read_from_buffer(buffer, count, position, fileinfo, len);
3432 kfree(fileinfo);
3433bail:
3434 return ret;
3435}
3436
3437static const struct file_operations debugfs_fops = {
3438 .open = fastrpc_debugfs_open,
3439 .read = fastrpc_debugfs_read,
3440};
Sathish Ambley36849af2017-02-02 09:35:55 -08003441static int fastrpc_channel_open(struct fastrpc_file *fl)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003442{
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003443 struct fastrpc_apps *me = &gfa;
Mohammed Nayeem Ur Rahmancd836462020-04-01 14:30:33 +05303444 int cid = -1, ii, err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003445
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303446 mutex_lock(&me->smd_mutex);
3447
Sathish Ambley36849af2017-02-02 09:35:55 -08003448 VERIFY(err, fl && fl->sctx);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003449 if (err)
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303450 goto bail;
Sathish Ambley36849af2017-02-02 09:35:55 -08003451 cid = fl->cid;
Mohammed Nayeem Ur Rahmancd836462020-04-01 14:30:33 +05303452 VERIFY(err, cid >= ADSP_DOMAIN_ID && cid < NUM_CHANNELS);
3453 if (err) {
3454 err = -ECHRNG;
c_mtharu314a4202017-11-15 22:09:17 +05303455 goto bail;
Mohammed Nayeem Ur Rahmancd836462020-04-01 14:30:33 +05303456 }
c_mtharue1a5ce12017-10-13 20:47:09 +05303457 if (me->channel[cid].ssrcount !=
3458 me->channel[cid].prevssrcount) {
3459 if (!me->channel[cid].issubsystemup) {
3460 VERIFY(err, 0);
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303461 if (err) {
3462 err = -ENOTCONN;
c_mtharue1a5ce12017-10-13 20:47:09 +05303463 goto bail;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303464 }
c_mtharue1a5ce12017-10-13 20:47:09 +05303465 }
3466 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003467 fl->ssrcount = me->channel[cid].ssrcount;
Tharun Kumar Merugu35173342018-02-08 16:13:17 +05303468 fl->refcount = 1;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003469 if ((kref_get_unless_zero(&me->channel[cid].kref) == 0) ||
c_mtharue1a5ce12017-10-13 20:47:09 +05303470 (me->channel[cid].chan == NULL)) {
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05303471 if (me->glink) {
3472 VERIFY(err, 0 == fastrpc_glink_register(cid, me));
3473 if (err)
3474 goto bail;
3475 VERIFY(err, 0 == fastrpc_glink_open(cid));
Mohammed Nayeem Ur Rahmana967be62019-09-23 20:56:15 +05303476 VERIFY(err,
3477 wait_for_completion_timeout(&me->channel[cid].workport,
3478 RPC_TIMEOUT));
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05303479 } else {
Mohammed Nayeem Ur Rahmana967be62019-09-23 20:56:15 +05303480 if (me->channel[cid].chan == NULL) {
3481 VERIFY(err, !smd_named_open_on_edge(
3482 FASTRPC_SMD_GUID,
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05303483 gcinfo[cid].channel,
3484 (smd_channel_t **)&me->channel[cid].chan,
3485 (void *)(uintptr_t)cid,
3486 smd_event_handler));
Tharun Kumar Merugu53a8ec92017-07-14 15:52:49 +05303487 VERIFY(err,
3488 wait_for_completion_timeout(&me->channel[cid].workport,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003489 RPC_TIMEOUT));
Mohammed Nayeem Ur Rahmana967be62019-09-23 20:56:15 +05303490
3491 }
3492 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003493 if (err) {
c_mtharue1a5ce12017-10-13 20:47:09 +05303494 me->channel[cid].chan = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003495 goto bail;
3496 }
3497 kref_init(&me->channel[cid].kref);
3498 pr_info("'opened /dev/%s c %d %d'\n", gcinfo[cid].name,
3499 MAJOR(me->dev_no), cid);
Tharun Kumar Meruguc42c6e22018-05-29 15:50:46 +05303500
3501 for (ii = 0; ii < FASTRPC_GLINK_INTENT_NUM && me->glink; ii++)
3502 glink_queue_rx_intent(me->channel[cid].chan, NULL,
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05303503 FASTRPC_GLINK_INTENT_LEN);
Tharun Kumar Meruguc42c6e22018-05-29 15:50:46 +05303504
Tharun Kumar Merugud86fc8c2018-01-04 16:35:31 +05303505 if (cid == 0 && me->channel[cid].ssrcount !=
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003506 me->channel[cid].prevssrcount) {
c_mtharue1a5ce12017-10-13 20:47:09 +05303507 if (fastrpc_mmap_remove_ssr(fl))
3508 pr_err("ADSPRPC: SSR: Failed to unmap remote heap\n");
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003509 me->channel[cid].prevssrcount =
3510 me->channel[cid].ssrcount;
3511 }
3512 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003513
3514bail:
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303515 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003516 return err;
3517}
3518
Sathish Ambley36849af2017-02-02 09:35:55 -08003519static int fastrpc_device_open(struct inode *inode, struct file *filp)
3520{
3521 int err = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05303522 struct fastrpc_file *fl = NULL;
Sathish Ambley36849af2017-02-02 09:35:55 -08003523 struct fastrpc_apps *me = &gfa;
3524
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05303525 /*
3526 * Indicates the device node opened
3527 * MINOR_NUM_DEV or MINOR_NUM_SECURE_DEV
3528 */
3529 int dev_minor = MINOR(inode->i_rdev);
3530
3531 VERIFY(err, ((dev_minor == MINOR_NUM_DEV) ||
3532 (dev_minor == MINOR_NUM_SECURE_DEV)));
3533 if (err) {
3534 pr_err("adsprpc: Invalid dev minor num %d\n", dev_minor);
3535 return err;
3536 }
3537
c_mtharue1a5ce12017-10-13 20:47:09 +05303538 VERIFY(err, NULL != (fl = kzalloc(sizeof(*fl), GFP_KERNEL)));
Sathish Ambley36849af2017-02-02 09:35:55 -08003539 if (err)
3540 return err;
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05303541
Sathish Ambley36849af2017-02-02 09:35:55 -08003542 context_list_ctor(&fl->clst);
3543 spin_lock_init(&fl->hlock);
3544 INIT_HLIST_HEAD(&fl->maps);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05303545 INIT_HLIST_HEAD(&fl->perf);
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303546 INIT_HLIST_HEAD(&fl->cached_bufs);
3547 INIT_HLIST_HEAD(&fl->remote_bufs);
Sathish Ambley36849af2017-02-02 09:35:55 -08003548 INIT_HLIST_NODE(&fl->hn);
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05303549 fl->sessionid = 0;
Sathish Ambley36849af2017-02-02 09:35:55 -08003550 fl->apps = me;
3551 fl->mode = FASTRPC_MODE_SERIAL;
3552 fl->cid = -1;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05303553 fl->dev_minor = dev_minor;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303554 fl->init_mem = NULL;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303555 fl->qos_request = 0;
Tharun Kumar Merugu35173342018-02-08 16:13:17 +05303556 fl->refcount = 0;
Sathish Ambley36849af2017-02-02 09:35:55 -08003557 filp->private_data = fl;
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05303558 mutex_init(&fl->map_mutex);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05303559 mutex_init(&fl->fl_map_mutex);
Sathish Ambley36849af2017-02-02 09:35:55 -08003560 spin_lock(&me->hlock);
3561 hlist_add_head(&fl->hn, &me->drivers);
3562 spin_unlock(&me->hlock);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05303563 mutex_init(&fl->perf_mutex);
Sathish Ambley36849af2017-02-02 09:35:55 -08003564 return 0;
3565}
3566
Edgar Flores1a772fa2020-02-07 14:59:29 -08003567static int fastrpc_set_process_info(struct fastrpc_file *fl)
3568{
3569 int err = 0, buf_size = 0;
3570 char strpid[PID_SIZE];
3571
3572 fl->tgid = current->tgid;
3573 snprintf(strpid, PID_SIZE, "%d", current->pid);
3574 buf_size = strlen(current->comm) + strlen("_") + strlen(strpid) + 1;
3575 fl->debug_buf = kzalloc(buf_size, GFP_KERNEL);
3576 if (!fl->debug_buf) {
3577 err = -ENOMEM;
3578 return err;
3579 }
3580 snprintf(fl->debug_buf, UL_SIZE, "%.10s%s%d",
3581 current->comm, "_", current->pid);
3582 fl->debugfs_file = debugfs_create_file(fl->debug_buf, 0644,
3583 debugfs_root, fl, &debugfs_fops);
3584 if (!fl->debugfs_file)
3585 pr_warn("Error: %s: %s: failed to create debugfs file %s\n",
3586 current->comm, __func__, fl->debug_buf);
3587 return err;
3588}
3589
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003590static int fastrpc_get_info(struct fastrpc_file *fl, uint32_t *info)
3591{
3592 int err = 0;
Sathish Ambley36849af2017-02-02 09:35:55 -08003593 uint32_t cid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003594
c_mtharue1a5ce12017-10-13 20:47:09 +05303595 VERIFY(err, fl != NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003596 if (err)
3597 goto bail;
Edgar Flores1a772fa2020-02-07 14:59:29 -08003598 err = fastrpc_set_process_info(fl);
3599 if (err)
3600 goto bail;
Sathish Ambley36849af2017-02-02 09:35:55 -08003601 if (fl->cid == -1) {
3602 cid = *info;
3603 VERIFY(err, cid < NUM_CHANNELS);
3604 if (err)
3605 goto bail;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05303606 /* Check to see if the device node is non-secure */
zhaochenfc798572018-08-17 15:32:37 +08003607 if (fl->dev_minor == MINOR_NUM_DEV &&
3608 fl->apps->secure_flag == true) {
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05303609 /*
3610 * For non secure device node check and make sure that
3611 * the channel allows non-secure access
3612 * If not, bail. Session will not start.
3613 * cid will remain -1 and client will not be able to
3614 * invoke any other methods without failure
3615 */
3616 if (fl->apps->channel[cid].secure == SECURE_CHANNEL) {
3617 err = -EPERM;
3618 pr_err("adsprpc: GetInfo failed dev %d, cid %d, secure %d\n",
3619 fl->dev_minor, cid,
3620 fl->apps->channel[cid].secure);
3621 goto bail;
3622 }
3623 }
Sathish Ambley36849af2017-02-02 09:35:55 -08003624 fl->cid = cid;
3625 fl->ssrcount = fl->apps->channel[cid].ssrcount;
3626 VERIFY(err, !fastrpc_session_alloc_locked(
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303627 &fl->apps->channel[cid], 0, fl->sharedcb, &fl->sctx));
Sathish Ambley36849af2017-02-02 09:35:55 -08003628 if (err)
3629 goto bail;
3630 }
Tharun Kumar Merugu80be7d62017-08-02 11:03:22 +05303631 VERIFY(err, fl->sctx != NULL);
3632 if (err)
3633 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003634 *info = (fl->sctx->smmu.enabled ? 1 : 0);
3635bail:
3636 return err;
3637}
3638
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303639static int fastrpc_internal_control(struct fastrpc_file *fl,
3640 struct fastrpc_ioctl_control *cp)
3641{
Mohammed Nayeem Ur Rahman18d633f2019-05-28 15:11:40 +05303642 struct fastrpc_apps *me = &gfa;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303643 int err = 0;
3644 int latency;
3645
3646 VERIFY(err, !IS_ERR_OR_NULL(fl) && !IS_ERR_OR_NULL(fl->apps));
3647 if (err)
3648 goto bail;
3649 VERIFY(err, !IS_ERR_OR_NULL(cp));
3650 if (err)
3651 goto bail;
3652
3653 switch (cp->req) {
3654 case FASTRPC_CONTROL_LATENCY:
3655 latency = cp->lp.enable == FASTRPC_LATENCY_CTRL_ENB ?
3656 fl->apps->latency : PM_QOS_DEFAULT_VALUE;
3657 VERIFY(err, latency != 0);
3658 if (err)
3659 goto bail;
3660 if (!fl->qos_request) {
3661 pm_qos_add_request(&fl->pm_qos_req,
3662 PM_QOS_CPU_DMA_LATENCY, latency);
3663 fl->qos_request = 1;
3664 } else
3665 pm_qos_update_request(&fl->pm_qos_req, latency);
3666 break;
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303667 case FASTRPC_CONTROL_SMMU:
Mohammed Nayeem Ur Rahman18d633f2019-05-28 15:11:40 +05303668 if (!me->legacy)
3669 fl->sharedcb = cp->smmu.sharedcb;
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303670 break;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303671 case FASTRPC_CONTROL_KALLOC:
3672 cp->kalloc.kalloc_support = 1;
3673 break;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303674 default:
3675 err = -ENOTTY;
3676 break;
3677 }
3678bail:
3679 return err;
3680}
3681
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003682static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num,
3683 unsigned long ioctl_param)
3684{
3685 union {
Sathish Ambleybae51902017-07-03 15:00:49 -07003686 struct fastrpc_ioctl_invoke_crc inv;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003687 struct fastrpc_ioctl_mmap mmap;
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05303688 struct fastrpc_ioctl_mmap_64 mmap64;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003689 struct fastrpc_ioctl_munmap munmap;
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05303690 struct fastrpc_ioctl_munmap_64 munmap64;
c_mtharu7bd6a422017-10-17 18:15:37 +05303691 struct fastrpc_ioctl_munmap_fd munmap_fd;
Sathish Ambleyd6300c32017-01-18 09:50:43 -08003692 struct fastrpc_ioctl_init_attrs init;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003693 struct fastrpc_ioctl_perf perf;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303694 struct fastrpc_ioctl_control cp;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003695 } p;
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05303696 union {
3697 struct fastrpc_ioctl_mmap mmap;
3698 struct fastrpc_ioctl_munmap munmap;
3699 } i;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003700 void *param = (char *)ioctl_param;
3701 struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data;
3702 int size = 0, err = 0;
3703 uint32_t info;
3704
c_mtharue1a5ce12017-10-13 20:47:09 +05303705 p.inv.fds = NULL;
3706 p.inv.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07003707 p.inv.crc = NULL;
tharun kumar9f899ea2017-07-03 17:07:03 +05303708 spin_lock(&fl->hlock);
3709 if (fl->file_close == 1) {
3710 err = EBADF;
3711 pr_warn("ADSPRPC: fastrpc_device_release is happening, So not sending any new requests to DSP");
3712 spin_unlock(&fl->hlock);
3713 goto bail;
3714 }
3715 spin_unlock(&fl->hlock);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003716
3717 switch (ioctl_num) {
3718 case FASTRPC_IOCTL_INVOKE:
3719 size = sizeof(struct fastrpc_ioctl_invoke);
Sathish Ambleybae51902017-07-03 15:00:49 -07003720 /* fall through */
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003721 case FASTRPC_IOCTL_INVOKE_FD:
3722 if (!size)
3723 size = sizeof(struct fastrpc_ioctl_invoke_fd);
3724 /* fall through */
3725 case FASTRPC_IOCTL_INVOKE_ATTRS:
3726 if (!size)
3727 size = sizeof(struct fastrpc_ioctl_invoke_attrs);
Sathish Ambleybae51902017-07-03 15:00:49 -07003728 /* fall through */
3729 case FASTRPC_IOCTL_INVOKE_CRC:
3730 if (!size)
3731 size = sizeof(struct fastrpc_ioctl_invoke_crc);
c_mtharue1a5ce12017-10-13 20:47:09 +05303732 K_COPY_FROM_USER(err, 0, &p.inv, param, size);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003733 if (err)
3734 goto bail;
3735 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, fl->mode,
3736 0, &p.inv)));
3737 if (err)
3738 goto bail;
3739 break;
3740 case FASTRPC_IOCTL_MMAP:
c_mtharue1a5ce12017-10-13 20:47:09 +05303741 K_COPY_FROM_USER(err, 0, &p.mmap, param,
3742 sizeof(p.mmap));
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05303743 if (err)
3744 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003745 VERIFY(err, 0 == (err = fastrpc_internal_mmap(fl, &p.mmap)));
3746 if (err)
3747 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05303748 K_COPY_TO_USER(err, 0, param, &p.mmap, sizeof(p.mmap));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003749 if (err)
3750 goto bail;
3751 break;
3752 case FASTRPC_IOCTL_MUNMAP:
c_mtharue1a5ce12017-10-13 20:47:09 +05303753 K_COPY_FROM_USER(err, 0, &p.munmap, param,
3754 sizeof(p.munmap));
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05303755 if (err)
3756 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003757 VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl,
3758 &p.munmap)));
3759 if (err)
3760 goto bail;
3761 break;
Tharun Kumar Merugu55be90d2018-05-31 11:41:03 +05303762 case FASTRPC_IOCTL_MMAP_64:
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05303763 K_COPY_FROM_USER(err, 0, &p.mmap64, param,
3764 sizeof(p.mmap64));
Tharun Kumar Merugu55be90d2018-05-31 11:41:03 +05303765 if (err)
3766 goto bail;
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05303767 get_fastrpc_ioctl_mmap_64(&p.mmap64, &i.mmap);
3768 VERIFY(err, 0 == (err = fastrpc_internal_mmap(fl, &i.mmap)));
Tharun Kumar Merugu55be90d2018-05-31 11:41:03 +05303769 if (err)
3770 goto bail;
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05303771 put_fastrpc_ioctl_mmap_64(&p.mmap64, &i.mmap);
3772 K_COPY_TO_USER(err, 0, param, &p.mmap64, sizeof(p.mmap64));
Tharun Kumar Merugu55be90d2018-05-31 11:41:03 +05303773 if (err)
3774 goto bail;
3775 break;
3776 case FASTRPC_IOCTL_MUNMAP_64:
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05303777 K_COPY_FROM_USER(err, 0, &p.munmap64, param,
3778 sizeof(p.munmap64));
Tharun Kumar Merugu55be90d2018-05-31 11:41:03 +05303779 if (err)
3780 goto bail;
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05303781 get_fastrpc_ioctl_munmap_64(&p.munmap64, &i.munmap);
Tharun Kumar Merugu55be90d2018-05-31 11:41:03 +05303782 VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl,
Tharun Kumar Merugu92b5e132018-07-18 15:03:35 +05303783 &i.munmap)));
Tharun Kumar Merugu55be90d2018-05-31 11:41:03 +05303784 if (err)
3785 goto bail;
3786 break;
c_mtharu7bd6a422017-10-17 18:15:37 +05303787 case FASTRPC_IOCTL_MUNMAP_FD:
3788 K_COPY_FROM_USER(err, 0, &p.munmap_fd, param,
3789 sizeof(p.munmap_fd));
3790 if (err)
3791 goto bail;
3792 VERIFY(err, 0 == (err = fastrpc_internal_munmap_fd(fl,
3793 &p.munmap_fd)));
3794 if (err)
3795 goto bail;
3796 break;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003797 case FASTRPC_IOCTL_SETMODE:
3798 switch ((uint32_t)ioctl_param) {
3799 case FASTRPC_MODE_PARALLEL:
3800 case FASTRPC_MODE_SERIAL:
3801 fl->mode = (uint32_t)ioctl_param;
3802 break;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003803 case FASTRPC_MODE_PROFILE:
3804 fl->profile = (uint32_t)ioctl_param;
3805 break;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05303806 case FASTRPC_MODE_SESSION:
3807 fl->sessionid = 1;
3808 fl->tgid |= (1 << SESSION_ID_INDEX);
3809 break;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003810 default:
3811 err = -ENOTTY;
3812 break;
3813 }
3814 break;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003815 case FASTRPC_IOCTL_GETPERF:
c_mtharue1a5ce12017-10-13 20:47:09 +05303816 K_COPY_FROM_USER(err, 0, &p.perf,
3817 param, sizeof(p.perf));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003818 if (err)
3819 goto bail;
3820 p.perf.numkeys = sizeof(struct fastrpc_perf)/sizeof(int64_t);
3821 if (p.perf.keys) {
3822 char *keys = PERF_KEYS;
3823
c_mtharue1a5ce12017-10-13 20:47:09 +05303824 K_COPY_TO_USER(err, 0, (void *)p.perf.keys,
3825 keys, strlen(keys)+1);
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003826 if (err)
3827 goto bail;
3828 }
3829 if (p.perf.data) {
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05303830 struct fastrpc_perf *perf = NULL, *fperf = NULL;
3831 struct hlist_node *n = NULL;
3832
3833 mutex_lock(&fl->perf_mutex);
3834 hlist_for_each_entry_safe(perf, n, &fl->perf, hn) {
3835 if (perf->tid == current->pid) {
3836 fperf = perf;
3837 break;
3838 }
3839 }
3840
3841 mutex_unlock(&fl->perf_mutex);
3842
3843 if (fperf) {
3844 K_COPY_TO_USER(err, 0, (void *)p.perf.data,
3845 fperf, sizeof(*fperf));
3846 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003847 }
c_mtharue1a5ce12017-10-13 20:47:09 +05303848 K_COPY_TO_USER(err, 0, param, &p.perf, sizeof(p.perf));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003849 if (err)
3850 goto bail;
3851 break;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303852 case FASTRPC_IOCTL_CONTROL:
c_mtharue1a5ce12017-10-13 20:47:09 +05303853 K_COPY_FROM_USER(err, 0, &p.cp, param,
3854 sizeof(p.cp));
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303855 if (err)
3856 goto bail;
3857 VERIFY(err, 0 == (err = fastrpc_internal_control(fl, &p.cp)));
3858 if (err)
3859 goto bail;
Tharun Kumar Merugue073de72018-07-30 23:57:47 +05303860 if (p.cp.req == FASTRPC_CONTROL_KALLOC) {
3861 K_COPY_TO_USER(err, 0, param, &p.cp, sizeof(p.cp));
3862 if (err)
3863 goto bail;
3864 }
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303865 break;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003866 case FASTRPC_IOCTL_GETINFO:
c_mtharue1a5ce12017-10-13 20:47:09 +05303867 K_COPY_FROM_USER(err, 0, &info, param, sizeof(info));
Sathish Ambley36849af2017-02-02 09:35:55 -08003868 if (err)
3869 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003870 VERIFY(err, 0 == (err = fastrpc_get_info(fl, &info)));
3871 if (err)
3872 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05303873 K_COPY_TO_USER(err, 0, param, &info, sizeof(info));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003874 if (err)
3875 goto bail;
3876 break;
3877 case FASTRPC_IOCTL_INIT:
Sathish Ambleyd6300c32017-01-18 09:50:43 -08003878 p.init.attrs = 0;
3879 p.init.siglen = 0;
3880 size = sizeof(struct fastrpc_ioctl_init);
3881 /* fall through */
3882 case FASTRPC_IOCTL_INIT_ATTRS:
3883 if (!size)
3884 size = sizeof(struct fastrpc_ioctl_init_attrs);
c_mtharue1a5ce12017-10-13 20:47:09 +05303885 K_COPY_FROM_USER(err, 0, &p.init, param, size);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003886 if (err)
3887 goto bail;
Tharun Kumar Merugu4ea0eac2017-08-22 11:42:51 +05303888 VERIFY(err, p.init.init.filelen >= 0 &&
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05303889 p.init.init.filelen < INIT_FILELEN_MAX);
3890 if (err)
3891 goto bail;
3892 VERIFY(err, p.init.init.memlen >= 0 &&
3893 p.init.init.memlen < INIT_MEMLEN_MAX);
Tharun Kumar Merugu4ea0eac2017-08-22 11:42:51 +05303894 if (err)
3895 goto bail;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303896 VERIFY(err, 0 == (err = fastrpc_init_process(fl, &p.init)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003897 if (err)
3898 goto bail;
3899 break;
3900
3901 default:
3902 err = -ENOTTY;
3903 pr_info("bad ioctl: %d\n", ioctl_num);
3904 break;
3905 }
3906 bail:
3907 return err;
3908}
3909
3910static int fastrpc_restart_notifier_cb(struct notifier_block *nb,
3911 unsigned long code,
3912 void *data)
3913{
3914 struct fastrpc_apps *me = &gfa;
3915 struct fastrpc_channel_ctx *ctx;
c_mtharue1a5ce12017-10-13 20:47:09 +05303916 struct notif_data *notifdata = data;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003917 int cid;
3918
3919 ctx = container_of(nb, struct fastrpc_channel_ctx, nb);
3920 cid = ctx - &me->channel[0];
3921 if (code == SUBSYS_BEFORE_SHUTDOWN) {
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303922 mutex_lock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003923 ctx->ssrcount++;
c_mtharue1a5ce12017-10-13 20:47:09 +05303924 ctx->issubsystemup = 0;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303925 if (ctx->chan) {
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05303926 if (me->glink)
3927 fastrpc_glink_close(ctx->chan, cid);
3928 else
3929 smd_close(ctx->chan);
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303930 ctx->chan = NULL;
3931 pr_info("'restart notifier: closed /dev/%s c %d %d'\n",
3932 gcinfo[cid].name, MAJOR(me->dev_no), cid);
3933 }
3934 mutex_unlock(&me->smd_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05303935 if (cid == 0)
3936 me->staticpd_flags = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003937 fastrpc_notify_drivers(me, cid);
c_mtharue1a5ce12017-10-13 20:47:09 +05303938 } else if (code == SUBSYS_RAMDUMP_NOTIFICATION) {
3939 if (me->channel[0].remoteheap_ramdump_dev &&
3940 notifdata->enable_ramdump) {
3941 me->channel[0].ramdumpenabled = 1;
3942 }
3943 } else if (code == SUBSYS_AFTER_POWERUP) {
3944 ctx->issubsystemup = 1;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003945 }
3946
3947 return NOTIFY_DONE;
3948}
3949
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303950static int fastrpc_pdr_notifier_cb(struct notifier_block *pdrnb,
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05303951 unsigned long code,
3952 void *data)
3953{
3954 struct fastrpc_apps *me = &gfa;
3955 struct fastrpc_static_pd *spd;
3956 struct notif_data *notifdata = data;
3957
3958 spd = container_of(pdrnb, struct fastrpc_static_pd, pdrnb);
3959 if (code == SERVREG_NOTIF_SERVICE_STATE_DOWN_V01) {
3960 mutex_lock(&me->smd_mutex);
3961 spd->pdrcount++;
3962 spd->ispdup = 0;
3963 pr_info("ADSPRPC: Audio PDR notifier %d %s\n",
3964 MAJOR(me->dev_no), spd->spdname);
3965 mutex_unlock(&me->smd_mutex);
3966 if (!strcmp(spd->spdname,
3967 AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME))
3968 me->staticpd_flags = 0;
3969 fastrpc_notify_pdr_drivers(me, spd->spdname);
3970 } else if (code == SUBSYS_RAMDUMP_NOTIFICATION) {
3971 if (me->channel[0].remoteheap_ramdump_dev &&
3972 notifdata->enable_ramdump) {
3973 me->channel[0].ramdumpenabled = 1;
3974 }
3975 } else if (code == SERVREG_NOTIF_SERVICE_STATE_UP_V01) {
3976 spd->ispdup = 1;
3977 }
3978
3979 return NOTIFY_DONE;
3980}
3981
3982static int fastrpc_get_service_location_notify(struct notifier_block *nb,
3983 unsigned long opcode, void *data)
3984{
3985 struct fastrpc_static_pd *spd;
3986 struct pd_qmi_client_data *pdr = data;
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303987 int curr_state = 0, i = 0;
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05303988
3989 spd = container_of(nb, struct fastrpc_static_pd, get_service_nb);
3990 if (opcode == LOCATOR_DOWN) {
3991 pr_err("ADSPRPC: Audio PD restart notifier locator down\n");
3992 return NOTIFY_DONE;
3993 }
3994
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303995 for (i = 0; i < pdr->total_domains; i++) {
3996 if ((!strcmp(pdr->domain_list[i].name,
3997 "msm/adsp/audio_pd")) ||
3998 (!strcmp(pdr->domain_list[i].name,
3999 "msm/adsp/sensor_pd"))) {
4000 spd->pdrhandle =
4001 service_notif_register_notifier(
4002 pdr->domain_list[i].name,
4003 pdr->domain_list[i].instance_id,
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05304004 &spd->pdrnb, &curr_state);
Tharun Kumar Meruguad4beb82018-05-10 19:51:48 +05304005 if (IS_ERR(spd->pdrhandle)) {
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05304006 pr_err("ADSPRPC: Unable to register notifier\n");
Tharun Kumar Meruguad4beb82018-05-10 19:51:48 +05304007 } else if (curr_state ==
4008 SERVREG_NOTIF_SERVICE_STATE_UP_V01) {
4009 pr_info("ADSPRPC: STATE_UP_V01 received\n");
4010 spd->ispdup = 1;
4011 } else if (curr_state ==
4012 SERVREG_NOTIF_SERVICE_STATE_UNINIT_V01) {
4013 pr_info("ADSPRPC: STATE_UNINIT_V01 received\n");
4014 }
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05304015 break;
4016 }
4017 }
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05304018
4019 return NOTIFY_DONE;
4020}
4021
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004022static const struct file_operations fops = {
4023 .open = fastrpc_device_open,
4024 .release = fastrpc_device_release,
4025 .unlocked_ioctl = fastrpc_device_ioctl,
4026 .compat_ioctl = compat_fastrpc_device_ioctl,
4027};
4028
4029static const struct of_device_id fastrpc_match_table[] = {
4030 { .compatible = "qcom,msm-fastrpc-adsp", },
4031 { .compatible = "qcom,msm-fastrpc-compute", },
4032 { .compatible = "qcom,msm-fastrpc-compute-cb", },
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05304033 { .compatible = "qcom,msm-fastrpc-legacy-compute", },
4034 { .compatible = "qcom,msm-fastrpc-legacy-compute-cb", },
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004035 { .compatible = "qcom,msm-adsprpc-mem-region", },
4036 {}
4037};
4038
4039static int fastrpc_cb_probe(struct device *dev)
4040{
4041 struct fastrpc_channel_ctx *chan;
4042 struct fastrpc_session_ctx *sess;
4043 struct of_phandle_args iommuspec;
4044 const char *name;
4045 unsigned int start = 0x80000000;
4046 int err = 0, i;
4047 int secure_vmid = VMID_CP_PIXEL;
4048
c_mtharue1a5ce12017-10-13 20:47:09 +05304049 VERIFY(err, NULL != (name = of_get_property(dev->of_node,
4050 "label", NULL)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004051 if (err)
4052 goto bail;
4053 for (i = 0; i < NUM_CHANNELS; i++) {
4054 if (!gcinfo[i].name)
4055 continue;
4056 if (!strcmp(name, gcinfo[i].name))
4057 break;
4058 }
4059 VERIFY(err, i < NUM_CHANNELS);
4060 if (err)
4061 goto bail;
4062 chan = &gcinfo[i];
4063 VERIFY(err, chan->sesscount < NUM_SESSIONS);
4064 if (err)
4065 goto bail;
4066
4067 VERIFY(err, !of_parse_phandle_with_args(dev->of_node, "iommus",
4068 "#iommu-cells", 0, &iommuspec));
4069 if (err)
4070 goto bail;
4071 sess = &chan->session[chan->sesscount];
4072 sess->smmu.cb = iommuspec.args[0] & 0xf;
4073 sess->used = 0;
4074 sess->smmu.coherent = of_property_read_bool(dev->of_node,
4075 "dma-coherent");
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05304076 sess->smmu.sharedcb = of_property_read_bool(dev->of_node,
4077 "shared-cb");
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004078 sess->smmu.secure = of_property_read_bool(dev->of_node,
4079 "qcom,secure-context-bank");
4080 if (sess->smmu.secure)
4081 start = 0x60000000;
4082 VERIFY(err, !IS_ERR_OR_NULL(sess->smmu.mapping =
4083 arm_iommu_create_mapping(&platform_bus_type,
Tharun Kumar Meruguca183f92017-04-27 17:43:27 +05304084 start, 0x78000000)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004085 if (err)
4086 goto bail;
4087
4088 if (sess->smmu.secure)
4089 iommu_domain_set_attr(sess->smmu.mapping->domain,
4090 DOMAIN_ATTR_SECURE_VMID,
4091 &secure_vmid);
4092
4093 VERIFY(err, !arm_iommu_attach_device(dev, sess->smmu.mapping));
4094 if (err)
4095 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05304096 sess->smmu.dev = dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004097 sess->smmu.enabled = 1;
4098 chan->sesscount++;
Sathish Ambley1ca68232017-01-19 10:32:55 -08004099 debugfs_global_file = debugfs_create_file("global", 0644, debugfs_root,
4100 NULL, &debugfs_fops);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004101bail:
4102 return err;
4103}
4104
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05304105static int fastrpc_cb_legacy_probe(struct device *dev)
4106{
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05304107 struct fastrpc_channel_ctx *chan;
4108 struct fastrpc_session_ctx *first_sess = NULL, *sess = NULL;
4109 const char *name;
4110 unsigned int *sids = NULL, sids_size = 0;
4111 int err = 0, ret = 0, i;
4112
4113 unsigned int start = 0x80000000;
4114
4115 VERIFY(err, NULL != (name = of_get_property(dev->of_node,
4116 "label", NULL)));
4117 if (err)
4118 goto bail;
4119
4120 for (i = 0; i < NUM_CHANNELS; i++) {
4121 if (!gcinfo[i].name)
4122 continue;
4123 if (!strcmp(name, gcinfo[i].name))
4124 break;
4125 }
4126 VERIFY(err, i < NUM_CHANNELS);
4127 if (err)
4128 goto bail;
4129
4130 chan = &gcinfo[i];
4131 VERIFY(err, chan->sesscount < NUM_SESSIONS);
4132 if (err)
4133 goto bail;
4134
4135 first_sess = &chan->session[chan->sesscount];
4136
4137 VERIFY(err, NULL != of_get_property(dev->of_node,
4138 "sids", &sids_size));
4139 if (err)
4140 goto bail;
4141
4142 VERIFY(err, NULL != (sids = kzalloc(sids_size, GFP_KERNEL)));
4143 if (err)
4144 goto bail;
4145 ret = of_property_read_u32_array(dev->of_node, "sids", sids,
4146 sids_size/sizeof(unsigned int));
4147 if (ret)
4148 goto bail;
4149
4150 VERIFY(err, !IS_ERR_OR_NULL(first_sess->smmu.mapping =
4151 arm_iommu_create_mapping(&platform_bus_type,
4152 start, 0x78000000)));
4153 if (err)
4154 goto bail;
4155
4156 VERIFY(err, !arm_iommu_attach_device(dev, first_sess->smmu.mapping));
4157 if (err)
4158 goto bail;
4159
4160
4161 for (i = 0; i < sids_size/sizeof(unsigned int); i++) {
4162 VERIFY(err, chan->sesscount < NUM_SESSIONS);
4163 if (err)
4164 goto bail;
4165 sess = &chan->session[chan->sesscount];
4166 sess->smmu.cb = sids[i];
4167 sess->smmu.dev = dev;
4168 sess->smmu.mapping = first_sess->smmu.mapping;
4169 sess->smmu.enabled = 1;
4170 sess->used = 0;
4171 sess->smmu.coherent = false;
4172 sess->smmu.secure = false;
4173 chan->sesscount++;
4174 }
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05304175bail:
4176 kfree(sids);
4177 return err;
4178}
4179
4180
4181
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05304182static void init_secure_vmid_list(struct device *dev, char *prop_name,
4183 struct secure_vm *destvm)
4184{
4185 int err = 0;
4186 u32 len = 0, i = 0;
4187 u32 *rhvmlist = NULL;
4188 u32 *rhvmpermlist = NULL;
4189
4190 if (!of_find_property(dev->of_node, prop_name, &len))
4191 goto bail;
4192 if (len == 0)
4193 goto bail;
4194 len /= sizeof(u32);
4195 VERIFY(err, NULL != (rhvmlist = kcalloc(len, sizeof(u32), GFP_KERNEL)));
4196 if (err)
4197 goto bail;
4198 VERIFY(err, NULL != (rhvmpermlist = kcalloc(len, sizeof(u32),
4199 GFP_KERNEL)));
4200 if (err)
4201 goto bail;
4202 for (i = 0; i < len; i++) {
4203 err = of_property_read_u32_index(dev->of_node, prop_name, i,
4204 &rhvmlist[i]);
4205 rhvmpermlist[i] = PERM_READ | PERM_WRITE | PERM_EXEC;
4206 pr_info("ADSPRPC: Secure VMID = %d", rhvmlist[i]);
4207 if (err) {
4208 pr_err("ADSPRPC: Failed to read VMID\n");
4209 goto bail;
4210 }
4211 }
4212 destvm->vmid = rhvmlist;
4213 destvm->vmperm = rhvmpermlist;
4214 destvm->vmcount = len;
4215bail:
4216 if (err) {
4217 kfree(rhvmlist);
4218 kfree(rhvmpermlist);
4219 }
4220}
4221
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304222static void configure_secure_channels(uint32_t secure_domains)
4223{
4224 struct fastrpc_apps *me = &gfa;
4225 int ii = 0;
4226 /*
4227 * secure_domains contains the bitmask of the secure channels
4228 * Bit 0 - ADSP
4229 * Bit 1 - MDSP
4230 * Bit 2 - SLPI
4231 * Bit 3 - CDSP
4232 */
4233 for (ii = ADSP_DOMAIN_ID; ii <= CDSP_DOMAIN_ID; ++ii) {
4234 int secure = (secure_domains >> ii) & 0x01;
4235
4236 me->channel[ii].secure = secure;
4237 }
4238}
4239
4240
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004241static int fastrpc_probe(struct platform_device *pdev)
4242{
4243 int err = 0;
4244 struct fastrpc_apps *me = &gfa;
4245 struct device *dev = &pdev->dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004246 struct device_node *ion_node, *node;
4247 struct platform_device *ion_pdev;
4248 struct cma *cma;
4249 uint32_t val;
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05304250 int ret = 0;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304251 uint32_t secure_domains;
c_mtharu63ffc012017-11-16 15:26:56 +05304252
4253 if (of_device_is_compatible(dev->of_node,
4254 "qcom,msm-fastrpc-compute")) {
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05304255 init_secure_vmid_list(dev, "qcom,adsp-remoteheap-vmid",
4256 &gcinfo[0].rhvm);
c_mtharu63ffc012017-11-16 15:26:56 +05304257
c_mtharu63ffc012017-11-16 15:26:56 +05304258
4259 of_property_read_u32(dev->of_node, "qcom,rpc-latency-us",
4260 &me->latency);
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304261 if (of_get_property(dev->of_node,
4262 "qcom,secure-domains", NULL) != NULL) {
4263 VERIFY(err, !of_property_read_u32(dev->of_node,
4264 "qcom,secure-domains",
4265 &secure_domains));
zhaochenfc798572018-08-17 15:32:37 +08004266 if (!err) {
4267 me->secure_flag = true;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304268 configure_secure_channels(secure_domains);
zhaochenfc798572018-08-17 15:32:37 +08004269 } else {
4270 me->secure_flag = false;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304271 pr_info("adsprpc: unable to read the domain configuration from dts\n");
zhaochenfc798572018-08-17 15:32:37 +08004272 }
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304273 }
c_mtharu63ffc012017-11-16 15:26:56 +05304274 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004275 if (of_device_is_compatible(dev->of_node,
4276 "qcom,msm-fastrpc-compute-cb"))
4277 return fastrpc_cb_probe(dev);
4278
4279 if (of_device_is_compatible(dev->of_node,
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05304280 "qcom,msm-fastrpc-legacy-compute")) {
4281 me->glink = false;
Tharun Kumar Merugu4f2dcc82018-03-29 00:35:49 +05304282 me->legacy = 1;
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05304283 }
4284
4285 if (of_device_is_compatible(dev->of_node,
4286 "qcom,msm-fastrpc-legacy-compute-cb")){
4287 return fastrpc_cb_legacy_probe(dev);
4288 }
4289
4290 if (of_device_is_compatible(dev->of_node,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004291 "qcom,msm-adsprpc-mem-region")) {
4292 me->dev = dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004293 ion_node = of_find_compatible_node(NULL, NULL, "qcom,msm-ion");
4294 if (ion_node) {
4295 for_each_available_child_of_node(ion_node, node) {
4296 if (of_property_read_u32(node, "reg", &val))
4297 continue;
4298 if (val != ION_ADSP_HEAP_ID)
4299 continue;
4300 ion_pdev = of_find_device_by_node(node);
4301 if (!ion_pdev)
4302 break;
4303 cma = dev_get_cma_area(&ion_pdev->dev);
4304 if (cma) {
Tharun Kumar Merugu4f2dcc82018-03-29 00:35:49 +05304305 me->range.addr = cma_get_base(cma);
4306 me->range.size =
4307 (size_t)cma_get_size(cma);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004308 }
4309 break;
4310 }
4311 }
Tharun Kumar Merugu4f2dcc82018-03-29 00:35:49 +05304312 if (me->range.addr && !of_property_read_bool(dev->of_node,
Tharun Kumar Merugu6b7a4a22018-01-17 16:08:07 +05304313 "restrict-access")) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004314 int srcVM[1] = {VMID_HLOS};
4315 int destVM[4] = {VMID_HLOS, VMID_MSS_MSA, VMID_SSC_Q6,
4316 VMID_ADSP_Q6};
Sathish Ambley84d11862017-05-15 14:36:05 -07004317 int destVMperm[4] = {PERM_READ | PERM_WRITE | PERM_EXEC,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004318 PERM_READ | PERM_WRITE | PERM_EXEC,
4319 PERM_READ | PERM_WRITE | PERM_EXEC,
4320 PERM_READ | PERM_WRITE | PERM_EXEC,
4321 };
4322
Tharun Kumar Merugu4f2dcc82018-03-29 00:35:49 +05304323 VERIFY(err, !hyp_assign_phys(me->range.addr,
4324 me->range.size, srcVM, 1,
4325 destVM, destVMperm, 4));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004326 if (err)
4327 goto bail;
4328 }
4329 return 0;
4330 }
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05304331 if (of_property_read_bool(dev->of_node,
4332 "qcom,fastrpc-adsp-audio-pdr")) {
4333 int session;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004334
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05304335 VERIFY(err, !fastrpc_get_adsp_session(
4336 AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME, &session));
4337 if (err)
4338 goto spdbail;
4339 me->channel[0].spd[session].get_service_nb.notifier_call =
4340 fastrpc_get_service_location_notify;
4341 ret = get_service_location(
4342 AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME,
4343 AUDIO_PDR_ADSP_SERVICE_NAME,
4344 &me->channel[0].spd[session].get_service_nb);
4345 if (ret)
4346 pr_err("ADSPRPC: Get service location failed: %d\n",
4347 ret);
4348 }
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05304349 if (of_property_read_bool(dev->of_node,
4350 "qcom,fastrpc-adsp-sensors-pdr")) {
4351 int session;
4352
4353 VERIFY(err, !fastrpc_get_adsp_session(
4354 SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME, &session));
4355 if (err)
4356 goto spdbail;
4357 me->channel[0].spd[session].get_service_nb.notifier_call =
4358 fastrpc_get_service_location_notify;
4359 ret = get_service_location(
4360 SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME,
4361 SENSORS_PDR_ADSP_SERVICE_NAME,
4362 &me->channel[0].spd[session].get_service_nb);
4363 if (ret)
4364 pr_err("ADSPRPC: Get service location failed: %d\n",
4365 ret);
4366 }
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05304367spdbail:
4368 err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004369 VERIFY(err, !of_platform_populate(pdev->dev.of_node,
4370 fastrpc_match_table,
4371 NULL, &pdev->dev));
4372 if (err)
4373 goto bail;
4374bail:
4375 return err;
4376}
4377
4378static void fastrpc_deinit(void)
4379{
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05304380 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004381 struct fastrpc_channel_ctx *chan = gcinfo;
4382 int i, j;
4383
4384 for (i = 0; i < NUM_CHANNELS; i++, chan++) {
4385 if (chan->chan) {
4386 kref_put_mutex(&chan->kref,
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05304387 fastrpc_channel_close, &me->smd_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05304388 chan->chan = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004389 }
4390 for (j = 0; j < NUM_SESSIONS; j++) {
4391 struct fastrpc_session_ctx *sess = &chan->session[j];
c_mtharue1a5ce12017-10-13 20:47:09 +05304392 if (sess->smmu.dev) {
4393 arm_iommu_detach_device(sess->smmu.dev);
4394 sess->smmu.dev = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004395 }
4396 if (sess->smmu.mapping) {
4397 arm_iommu_release_mapping(sess->smmu.mapping);
c_mtharue1a5ce12017-10-13 20:47:09 +05304398 sess->smmu.mapping = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004399 }
4400 }
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05304401 kfree(chan->rhvm.vmid);
4402 kfree(chan->rhvm.vmperm);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004403 }
4404}
4405
4406static struct platform_driver fastrpc_driver = {
4407 .probe = fastrpc_probe,
4408 .driver = {
4409 .name = "fastrpc",
4410 .owner = THIS_MODULE,
4411 .of_match_table = fastrpc_match_table,
4412 },
4413};
4414
4415static int __init fastrpc_device_init(void)
4416{
4417 struct fastrpc_apps *me = &gfa;
c_mtharue1a5ce12017-10-13 20:47:09 +05304418 struct device *dev = NULL;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304419 struct device *secure_dev = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004420 int err = 0, i;
4421
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05304422 debugfs_root = debugfs_create_dir("adsprpc", NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004423 memset(me, 0, sizeof(*me));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004424 fastrpc_init(me);
4425 me->dev = NULL;
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05304426 me->glink = true;
zhaochenfc798572018-08-17 15:32:37 +08004427 me->secure_flag = false;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004428 VERIFY(err, 0 == platform_driver_register(&fastrpc_driver));
4429 if (err)
4430 goto register_bail;
4431 VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, NUM_CHANNELS,
4432 DEVICE_NAME));
4433 if (err)
4434 goto alloc_chrdev_bail;
4435 cdev_init(&me->cdev, &fops);
4436 me->cdev.owner = THIS_MODULE;
4437 VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0),
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304438 NUM_DEVICES));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004439 if (err)
4440 goto cdev_init_bail;
4441 me->class = class_create(THIS_MODULE, "fastrpc");
4442 VERIFY(err, !IS_ERR(me->class));
4443 if (err)
4444 goto class_create_bail;
4445 me->compat = (fops.compat_ioctl == NULL) ? 0 : 1;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304446
4447 /*
4448 * Create devices and register with sysfs
4449 * Create first device with minor number 0
4450 */
Sathish Ambley36849af2017-02-02 09:35:55 -08004451 dev = device_create(me->class, NULL,
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304452 MKDEV(MAJOR(me->dev_no), MINOR_NUM_DEV),
4453 NULL, DEVICE_NAME);
Sathish Ambley36849af2017-02-02 09:35:55 -08004454 VERIFY(err, !IS_ERR_OR_NULL(dev));
4455 if (err)
4456 goto device_create_bail;
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304457
4458 /* Create secure device with minor number for secure device */
4459 secure_dev = device_create(me->class, NULL,
4460 MKDEV(MAJOR(me->dev_no), MINOR_NUM_SECURE_DEV),
4461 NULL, DEVICE_NAME_SECURE);
4462 VERIFY(err, !IS_ERR_OR_NULL(secure_dev));
4463 if (err)
4464 goto device_create_bail;
4465
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004466 for (i = 0; i < NUM_CHANNELS; i++) {
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304467 me->channel[i].dev = secure_dev;
4468 if (i == CDSP_DOMAIN_ID)
4469 me->channel[i].dev = dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004470 me->channel[i].ssrcount = 0;
4471 me->channel[i].prevssrcount = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05304472 me->channel[i].issubsystemup = 1;
4473 me->channel[i].ramdumpenabled = 0;
4474 me->channel[i].remoteheap_ramdump_dev = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004475 me->channel[i].nb.notifier_call = fastrpc_restart_notifier_cb;
4476 me->channel[i].handle = subsys_notif_register_notifier(
4477 gcinfo[i].subsys,
4478 &me->channel[i].nb);
4479 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004480 me->client = msm_ion_client_create(DEVICE_NAME);
4481 VERIFY(err, !IS_ERR_OR_NULL(me->client));
4482 if (err)
4483 goto device_create_bail;
Mohammed Nayeem Ur Rahman492e3862018-08-17 12:53:43 +05304484
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004485 return 0;
4486device_create_bail:
4487 for (i = 0; i < NUM_CHANNELS; i++) {
Sathish Ambley36849af2017-02-02 09:35:55 -08004488 if (me->channel[i].handle)
4489 subsys_notif_unregister_notifier(me->channel[i].handle,
4490 &me->channel[i].nb);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004491 }
Sathish Ambley36849af2017-02-02 09:35:55 -08004492 if (!IS_ERR_OR_NULL(dev))
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304493 device_destroy(me->class, MKDEV(MAJOR(me->dev_no),
4494 MINOR_NUM_DEV));
4495 if (!IS_ERR_OR_NULL(secure_dev))
4496 device_destroy(me->class, MKDEV(MAJOR(me->dev_no),
4497 MINOR_NUM_SECURE_DEV));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004498 class_destroy(me->class);
4499class_create_bail:
4500 cdev_del(&me->cdev);
4501cdev_init_bail:
4502 unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
4503alloc_chrdev_bail:
4504register_bail:
4505 fastrpc_deinit();
4506 return err;
4507}
4508
4509static void __exit fastrpc_device_exit(void)
4510{
4511 struct fastrpc_apps *me = &gfa;
4512 int i;
4513
4514 fastrpc_file_list_dtor(me);
4515 fastrpc_deinit();
4516 for (i = 0; i < NUM_CHANNELS; i++) {
4517 if (!gcinfo[i].name)
4518 continue;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004519 subsys_notif_unregister_notifier(me->channel[i].handle,
4520 &me->channel[i].nb);
4521 }
Tharun Kumar Merugud996b262018-07-18 22:28:53 +05304522
4523 /* Destroy the secure and non secure devices */
4524 device_destroy(me->class, MKDEV(MAJOR(me->dev_no), MINOR_NUM_DEV));
4525 device_destroy(me->class, MKDEV(MAJOR(me->dev_no),
4526 MINOR_NUM_SECURE_DEV));
4527
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004528 class_destroy(me->class);
4529 cdev_del(&me->cdev);
4530 unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
4531 ion_client_destroy(me->client);
Sathish Ambley1ca68232017-01-19 10:32:55 -08004532 debugfs_remove_recursive(debugfs_root);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07004533}
4534
4535late_initcall(fastrpc_device_init);
4536module_exit(fastrpc_device_exit);
4537
4538MODULE_LICENSE("GPL v2");