blob: 7d8605bfeb84a7606a9c5214929a6fad9ff5a016 [file] [log] [blame]
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001/*
Tharun Kumar Merugubcd6fbf2018-01-04 17:49:34 +05302 * Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14#include <linux/dma-buf.h>
15#include <linux/dma-mapping.h>
16#include <linux/slab.h>
17#include <linux/completion.h>
18#include <linux/pagemap.h>
19#include <linux/mm.h>
20#include <linux/fs.h>
21#include <linux/sched.h>
22#include <linux/module.h>
23#include <linux/cdev.h>
24#include <linux/list.h>
25#include <linux/hash.h>
26#include <linux/msm_ion.h>
27#include <soc/qcom/secure_buffer.h>
28#include <soc/qcom/glink.h>
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 Merugu35a94a52018-02-01 21:09:04 +053059#define FASTRPC_DMAHANDLE_NOMAP (16)
60
Sathish Ambley69e1ab02016-10-18 10:28:15 -070061#define FASTRPC_ENOSUCH 39
62#define VMID_SSC_Q6 5
63#define VMID_ADSP_Q6 6
Sathish Ambley1ca68232017-01-19 10:32:55 -080064#define DEBUGFS_SIZE 1024
Sathish Ambley69e1ab02016-10-18 10:28:15 -070065
Tharun Kumar Merugudf860662018-01-17 19:59:50 +053066#define AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME "audio_pdr_adsprpc"
67#define AUDIO_PDR_ADSP_SERVICE_NAME "avs/audio"
68
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +053069#define SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME "sensors_pdr_adsprpc"
70#define SENSORS_PDR_ADSP_SERVICE_NAME "tms/servreg"
71
Sathish Ambley69e1ab02016-10-18 10:28:15 -070072#define RPC_TIMEOUT (5 * HZ)
73#define BALIGN 128
74#define NUM_CHANNELS 4 /* adsp, mdsp, slpi, cdsp*/
75#define NUM_SESSIONS 9 /*8 compute, 1 cpz*/
Sathish Ambleybae51902017-07-03 15:00:49 -070076#define M_FDLIST (16)
77#define M_CRCLIST (64)
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +053078#define SESSION_ID_INDEX (30)
c_mtharufdac6892017-10-12 13:09:01 +053079#define FASTRPC_CTX_MAGIC (0xbeeddeed)
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +053080#define FASTRPC_CTX_MAX (256)
81#define FASTRPC_CTXID_MASK (0xFF0)
Sathish Ambley69e1ab02016-10-18 10:28:15 -070082
83#define IS_CACHE_ALIGNED(x) (((x) & ((L1_CACHE_BYTES)-1)) == 0)
84
85#define FASTRPC_LINK_STATE_DOWN (0x0)
86#define FASTRPC_LINK_STATE_UP (0x1)
87#define FASTRPC_LINK_DISCONNECTED (0x0)
88#define FASTRPC_LINK_CONNECTING (0x1)
89#define FASTRPC_LINK_CONNECTED (0x3)
90#define FASTRPC_LINK_DISCONNECTING (0x7)
c_mtharu314a4202017-11-15 22:09:17 +053091#define FASTRPC_LINK_REMOTE_DISCONNECTING (0x8)
92#define FASTRPC_GLINK_INTENT_LEN (64)
Sathish Ambley69e1ab02016-10-18 10:28:15 -070093
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +053094#define PERF_KEYS \
95 "count:flush:map:copy:glink:getargs:putargs:invalidate:invoke:tid:ptr"
Sathish Ambleya21b5b52017-01-11 16:11:01 -080096#define FASTRPC_STATIC_HANDLE_LISTENER (3)
97#define FASTRPC_STATIC_HANDLE_MAX (20)
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +053098#define FASTRPC_LATENCY_CTRL_ENB (1)
Sathish Ambleya21b5b52017-01-11 16:11:01 -080099
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +0530100#define INIT_FILELEN_MAX (2*1024*1024)
101#define INIT_MEMLEN_MAX (8*1024*1024)
102
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800103#define PERF_END (void)0
104
105#define PERF(enb, cnt, ff) \
106 {\
107 struct timespec startT = {0};\
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530108 int64_t *counter = cnt;\
109 if (enb && counter) {\
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800110 getnstimeofday(&startT);\
111 } \
112 ff ;\
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530113 if (enb && counter) {\
114 *counter += getnstimediff(&startT);\
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800115 } \
116 }
117
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530118#define GET_COUNTER(perf_ptr, offset) \
119 (perf_ptr != NULL ?\
120 (((offset >= 0) && (offset < PERF_KEY_MAX)) ?\
121 (int64_t *)(perf_ptr + offset)\
122 : (int64_t *)NULL) : (int64_t *)NULL)
123
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700124static int fastrpc_glink_open(int cid);
125static void fastrpc_glink_close(void *chan, int cid);
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +0530126static int fastrpc_pdr_notifier_cb(struct notifier_block *nb,
Tharun Kumar Merugudf860662018-01-17 19:59:50 +0530127 unsigned long code,
128 void *data);
Sathish Ambley1ca68232017-01-19 10:32:55 -0800129static struct dentry *debugfs_root;
130static struct dentry *debugfs_global_file;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700131
132static inline uint64_t buf_page_start(uint64_t buf)
133{
134 uint64_t start = (uint64_t) buf & PAGE_MASK;
135 return start;
136}
137
138static inline uint64_t buf_page_offset(uint64_t buf)
139{
140 uint64_t offset = (uint64_t) buf & (PAGE_SIZE - 1);
141 return offset;
142}
143
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530144static inline uint64_t buf_num_pages(uint64_t buf, size_t len)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700145{
146 uint64_t start = buf_page_start(buf) >> PAGE_SHIFT;
147 uint64_t end = (((uint64_t) buf + len - 1) & PAGE_MASK) >> PAGE_SHIFT;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530148 uint64_t nPages = end - start + 1;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700149 return nPages;
150}
151
152static inline uint64_t buf_page_size(uint32_t size)
153{
154 uint64_t sz = (size + (PAGE_SIZE - 1)) & PAGE_MASK;
155
156 return sz > PAGE_SIZE ? sz : PAGE_SIZE;
157}
158
159static inline void *uint64_to_ptr(uint64_t addr)
160{
161 void *ptr = (void *)((uintptr_t)addr);
162
163 return ptr;
164}
165
166static inline uint64_t ptr_to_uint64(void *ptr)
167{
168 uint64_t addr = (uint64_t)((uintptr_t)ptr);
169
170 return addr;
171}
172
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +0530173struct secure_vm {
174 int *vmid;
175 int *vmperm;
176 int vmcount;
177};
178
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700179struct fastrpc_file;
180
181struct fastrpc_buf {
182 struct hlist_node hn;
183 struct fastrpc_file *fl;
184 void *virt;
185 uint64_t phys;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530186 size_t size;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700187};
188
189struct fastrpc_ctx_lst;
190
191struct overlap {
192 uintptr_t start;
193 uintptr_t end;
194 int raix;
195 uintptr_t mstart;
196 uintptr_t mend;
197 uintptr_t offset;
198};
199
200struct smq_invoke_ctx {
201 struct hlist_node hn;
202 struct completion work;
203 int retval;
204 int pid;
205 int tgid;
206 remote_arg_t *lpra;
207 remote_arg64_t *rpra;
208 int *fds;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700209 struct fastrpc_mmap **maps;
210 struct fastrpc_buf *buf;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530211 size_t used;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700212 struct fastrpc_file *fl;
213 uint32_t sc;
214 struct overlap *overs;
215 struct overlap **overps;
216 struct smq_msg msg;
c_mtharufdac6892017-10-12 13:09:01 +0530217 unsigned int magic;
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +0530218 unsigned int *attrs;
219 uint32_t *crc;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +0530220 uint64_t ctxid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700221};
222
223struct fastrpc_ctx_lst {
224 struct hlist_head pending;
225 struct hlist_head interrupted;
226};
227
228struct fastrpc_smmu {
c_mtharue1a5ce12017-10-13 20:47:09 +0530229 struct device *dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700230 struct dma_iommu_mapping *mapping;
231 int cb;
232 int enabled;
233 int faults;
234 int secure;
235 int coherent;
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +0530236 int sharedcb;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700237};
238
239struct fastrpc_session_ctx {
240 struct device *dev;
241 struct fastrpc_smmu smmu;
242 int used;
243};
244
Tharun Kumar Merugudf860662018-01-17 19:59:50 +0530245struct fastrpc_static_pd {
246 char *spdname;
247 struct notifier_block pdrnb;
248 struct notifier_block get_service_nb;
249 void *pdrhandle;
250 int pdrcount;
251 int prevpdrcount;
252 int ispdup;
253};
254
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700255struct fastrpc_glink_info {
256 int link_state;
257 int port_state;
258 struct glink_open_config cfg;
259 struct glink_link_info link_info;
260 void *link_notify_handle;
261};
262
263struct fastrpc_channel_ctx {
264 char *name;
265 char *subsys;
266 void *chan;
267 struct device *dev;
268 struct fastrpc_session_ctx session[NUM_SESSIONS];
Tharun Kumar Merugudf860662018-01-17 19:59:50 +0530269 struct fastrpc_static_pd spd[NUM_SESSIONS];
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700270 struct completion work;
Tharun Kumar Merugu53a8ec92017-07-14 15:52:49 +0530271 struct completion workport;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700272 struct notifier_block nb;
273 struct kref kref;
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +0530274 int channel;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700275 int sesscount;
276 int ssrcount;
277 void *handle;
278 int prevssrcount;
c_mtharue1a5ce12017-10-13 20:47:09 +0530279 int issubsystemup;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700280 int vmid;
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +0530281 struct secure_vm rhvm;
c_mtharue1a5ce12017-10-13 20:47:09 +0530282 int ramdumpenabled;
283 void *remoteheap_ramdump_dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700284 struct fastrpc_glink_info link;
285};
286
287struct fastrpc_apps {
288 struct fastrpc_channel_ctx *channel;
289 struct cdev cdev;
290 struct class *class;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +0530291 struct mutex smd_mutex;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700292 struct smq_phy_page range;
293 struct hlist_head maps;
c_mtharue1a5ce12017-10-13 20:47:09 +0530294 uint32_t staticpd_flags;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700295 dev_t dev_no;
296 int compat;
297 struct hlist_head drivers;
298 spinlock_t hlock;
299 struct ion_client *client;
300 struct device *dev;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +0530301 unsigned int latency;
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +0530302 bool glink;
303 bool legacy;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +0530304 spinlock_t ctxlock;
305 struct smq_invoke_ctx *ctxtable[FASTRPC_CTX_MAX];
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700306};
307
308struct fastrpc_mmap {
309 struct hlist_node hn;
310 struct fastrpc_file *fl;
311 struct fastrpc_apps *apps;
312 int fd;
313 uint32_t flags;
314 struct dma_buf *buf;
315 struct sg_table *table;
316 struct dma_buf_attachment *attach;
317 struct ion_handle *handle;
318 uint64_t phys;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530319 size_t size;
320 uintptr_t va;
321 size_t len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700322 int refs;
323 uintptr_t raddr;
324 int uncached;
325 int secure;
326 uintptr_t attr;
327};
328
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530329enum fastrpc_perfkeys {
330 PERF_COUNT = 0,
331 PERF_FLUSH = 1,
332 PERF_MAP = 2,
333 PERF_COPY = 3,
334 PERF_LINK = 4,
335 PERF_GETARGS = 5,
336 PERF_PUTARGS = 6,
337 PERF_INVARGS = 7,
338 PERF_INVOKE = 8,
339 PERF_KEY_MAX = 9,
340};
341
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800342struct fastrpc_perf {
343 int64_t count;
344 int64_t flush;
345 int64_t map;
346 int64_t copy;
347 int64_t link;
348 int64_t getargs;
349 int64_t putargs;
350 int64_t invargs;
351 int64_t invoke;
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530352 int64_t tid;
353 struct hlist_node hn;
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800354};
355
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700356struct fastrpc_file {
357 struct hlist_node hn;
358 spinlock_t hlock;
359 struct hlist_head maps;
360 struct hlist_head bufs;
361 struct fastrpc_ctx_lst clst;
362 struct fastrpc_session_ctx *sctx;
363 struct fastrpc_session_ctx *secsctx;
364 uint32_t mode;
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800365 uint32_t profile;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +0530366 int sessionid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700367 int tgid;
368 int cid;
369 int ssrcount;
370 int pd;
Tharun Kumar Merugudf860662018-01-17 19:59:50 +0530371 char *spdname;
tharun kumar9f899ea2017-07-03 17:07:03 +0530372 int file_close;
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +0530373 int sharedcb;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700374 struct fastrpc_apps *apps;
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530375 struct hlist_head perf;
Sathish Ambley1ca68232017-01-19 10:32:55 -0800376 struct dentry *debugfs_file;
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530377 struct mutex perf_mutex;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +0530378 struct pm_qos_request pm_qos_req;
379 int qos_request;
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +0530380 struct mutex map_mutex;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +0530381 struct mutex fl_map_mutex;
Tharun Kumar Merugu35173342018-02-08 16:13:17 +0530382 int refcount;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700383};
384
385static struct fastrpc_apps gfa;
386
387static struct fastrpc_channel_ctx gcinfo[NUM_CHANNELS] = {
388 {
389 .name = "adsprpc-smd",
390 .subsys = "adsp",
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +0530391 .channel = SMD_APPS_QDSP,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700392 .link.link_info.edge = "lpass",
393 .link.link_info.transport = "smem",
Tharun Kumar Merugudf860662018-01-17 19:59:50 +0530394 .spd = {
395 {
396 .spdname =
397 AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME,
398 .pdrnb.notifier_call =
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +0530399 fastrpc_pdr_notifier_cb,
400 },
401 {
402 .spdname =
403 SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME,
404 .pdrnb.notifier_call =
405 fastrpc_pdr_notifier_cb,
Tharun Kumar Merugudf860662018-01-17 19:59:50 +0530406 }
407 },
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700408 },
409 {
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700410 .name = "mdsprpc-smd",
411 .subsys = "modem",
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +0530412 .channel = SMD_APPS_MODEM,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700413 .link.link_info.edge = "mpss",
414 .link.link_info.transport = "smem",
415 },
416 {
Sathish Ambley36849af2017-02-02 09:35:55 -0800417 .name = "sdsprpc-smd",
418 .subsys = "slpi",
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +0530419 .channel = SMD_APPS_DSPS,
Sathish Ambley36849af2017-02-02 09:35:55 -0800420 .link.link_info.edge = "dsps",
421 .link.link_info.transport = "smem",
Sathish Ambley36849af2017-02-02 09:35:55 -0800422 },
423 {
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700424 .name = "cdsprpc-smd",
425 .subsys = "cdsp",
426 .link.link_info.edge = "cdsp",
427 .link.link_info.transport = "smem",
428 },
429};
430
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +0530431static int hlosvm[1] = {VMID_HLOS};
432static int hlosvmperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
433
Sathish Ambleya21b5b52017-01-11 16:11:01 -0800434static inline int64_t getnstimediff(struct timespec *start)
435{
436 int64_t ns;
437 struct timespec ts, b;
438
439 getnstimeofday(&ts);
440 b = timespec_sub(ts, *start);
441 ns = timespec_to_ns(&b);
442 return ns;
443}
444
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +0530445static inline int64_t *getperfcounter(struct fastrpc_file *fl, int key)
446{
447 int err = 0;
448 int64_t *val = NULL;
449 struct fastrpc_perf *perf = NULL, *fperf = NULL;
450 struct hlist_node *n = NULL;
451
452 VERIFY(err, !IS_ERR_OR_NULL(fl));
453 if (err)
454 goto bail;
455
456 mutex_lock(&fl->perf_mutex);
457 hlist_for_each_entry_safe(perf, n, &fl->perf, hn) {
458 if (perf->tid == current->pid) {
459 fperf = perf;
460 break;
461 }
462 }
463
464 if (IS_ERR_OR_NULL(fperf)) {
465 fperf = kzalloc(sizeof(*fperf), GFP_KERNEL);
466
467 VERIFY(err, !IS_ERR_OR_NULL(fperf));
468 if (err) {
469 mutex_unlock(&fl->perf_mutex);
470 kfree(fperf);
471 goto bail;
472 }
473
474 fperf->tid = current->pid;
475 hlist_add_head(&fperf->hn, &fl->perf);
476 }
477
478 val = ((int64_t *)fperf) + key;
479 mutex_unlock(&fl->perf_mutex);
480bail:
481 return val;
482}
483
484
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700485static void fastrpc_buf_free(struct fastrpc_buf *buf, int cache)
486{
c_mtharue1a5ce12017-10-13 20:47:09 +0530487 struct fastrpc_file *fl = buf == NULL ? NULL : buf->fl;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700488 int vmid;
489
490 if (!fl)
491 return;
492 if (cache) {
493 spin_lock(&fl->hlock);
494 hlist_add_head(&buf->hn, &fl->bufs);
495 spin_unlock(&fl->hlock);
496 return;
497 }
498 if (!IS_ERR_OR_NULL(buf->virt)) {
499 int destVM[1] = {VMID_HLOS};
500 int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
501
502 if (fl->sctx->smmu.cb)
503 buf->phys &= ~((uint64_t)fl->sctx->smmu.cb << 32);
504 vmid = fl->apps->channel[fl->cid].vmid;
505 if (vmid) {
506 int srcVM[2] = {VMID_HLOS, vmid};
507
508 hyp_assign_phys(buf->phys, buf_page_size(buf->size),
509 srcVM, 2, destVM, destVMperm, 1);
510 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530511 dma_free_coherent(fl->sctx->smmu.dev, buf->size, buf->virt,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700512 buf->phys);
513 }
514 kfree(buf);
515}
516
517static void fastrpc_buf_list_free(struct fastrpc_file *fl)
518{
519 struct fastrpc_buf *buf, *free;
520
521 do {
522 struct hlist_node *n;
523
c_mtharue1a5ce12017-10-13 20:47:09 +0530524 free = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700525 spin_lock(&fl->hlock);
526 hlist_for_each_entry_safe(buf, n, &fl->bufs, hn) {
527 hlist_del_init(&buf->hn);
528 free = buf;
529 break;
530 }
531 spin_unlock(&fl->hlock);
532 if (free)
533 fastrpc_buf_free(free, 0);
534 } while (free);
535}
536
537static void fastrpc_mmap_add(struct fastrpc_mmap *map)
538{
c_mtharue1a5ce12017-10-13 20:47:09 +0530539 if (map->flags == ADSP_MMAP_HEAP_ADDR ||
540 map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
541 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700542
c_mtharue1a5ce12017-10-13 20:47:09 +0530543 spin_lock(&me->hlock);
544 hlist_add_head(&map->hn, &me->maps);
545 spin_unlock(&me->hlock);
546 } else {
547 struct fastrpc_file *fl = map->fl;
548
c_mtharue1a5ce12017-10-13 20:47:09 +0530549 hlist_add_head(&map->hn, &fl->maps);
c_mtharue1a5ce12017-10-13 20:47:09 +0530550 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700551}
552
c_mtharue1a5ce12017-10-13 20:47:09 +0530553static int fastrpc_mmap_find(struct fastrpc_file *fl, int fd,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530554 uintptr_t va, size_t len, int mflags, int refs,
c_mtharue1a5ce12017-10-13 20:47:09 +0530555 struct fastrpc_mmap **ppmap)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700556{
c_mtharue1a5ce12017-10-13 20:47:09 +0530557 struct fastrpc_apps *me = &gfa;
558 struct fastrpc_mmap *match = NULL, *map = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700559 struct hlist_node *n;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530560
561 if ((va + len) < va)
562 return -EOVERFLOW;
c_mtharue1a5ce12017-10-13 20:47:09 +0530563 if (mflags == ADSP_MMAP_HEAP_ADDR ||
564 mflags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
565 spin_lock(&me->hlock);
566 hlist_for_each_entry_safe(map, n, &me->maps, hn) {
567 if (va >= map->va &&
568 va + len <= map->va + map->len &&
569 map->fd == fd) {
570 if (refs)
571 map->refs++;
572 match = map;
573 break;
574 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700575 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530576 spin_unlock(&me->hlock);
577 } else {
c_mtharue1a5ce12017-10-13 20:47:09 +0530578 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
579 if (va >= map->va &&
580 va + len <= map->va + map->len &&
581 map->fd == fd) {
582 if (refs)
583 map->refs++;
584 match = map;
585 break;
586 }
587 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700588 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700589 if (match) {
590 *ppmap = match;
591 return 0;
592 }
593 return -ENOTTY;
594}
595
Tharun Kumar Merugu48d5ff32018-04-16 19:24:16 +0530596static int dma_alloc_memory(dma_addr_t *region_phys, size_t size)
c_mtharue1a5ce12017-10-13 20:47:09 +0530597{
598 struct fastrpc_apps *me = &gfa;
Tharun Kumar Merugu48d5ff32018-04-16 19:24:16 +0530599 void *vaddr = NULL;
600 unsigned long dma_attrs = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +0530601
602 if (me->dev == NULL) {
603 pr_err("device adsprpc-mem is not initialized\n");
604 return -ENODEV;
605 }
Tharun Kumar Merugu48d5ff32018-04-16 19:24:16 +0530606 dma_attrs |= DMA_ATTR_SKIP_ZEROING | DMA_ATTR_NO_KERNEL_MAPPING;
607 vaddr = dma_alloc_attrs(me->dev, size, region_phys, GFP_KERNEL,
608 dma_attrs);
609 if (!vaddr) {
c_mtharue1a5ce12017-10-13 20:47:09 +0530610 pr_err("ADSPRPC: Failed to allocate %x remote heap memory\n",
611 (unsigned int)size);
612 return -ENOMEM;
613 }
614 return 0;
615}
616
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700617static int fastrpc_mmap_remove(struct fastrpc_file *fl, uintptr_t va,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530618 size_t len, struct fastrpc_mmap **ppmap)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700619{
c_mtharue1a5ce12017-10-13 20:47:09 +0530620 struct fastrpc_mmap *match = NULL, *map;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700621 struct hlist_node *n;
622 struct fastrpc_apps *me = &gfa;
623
624 spin_lock(&me->hlock);
625 hlist_for_each_entry_safe(map, n, &me->maps, hn) {
626 if (map->raddr == va &&
627 map->raddr + map->len == va + len &&
628 map->refs == 1) {
629 match = map;
630 hlist_del_init(&map->hn);
631 break;
632 }
633 }
634 spin_unlock(&me->hlock);
635 if (match) {
636 *ppmap = match;
637 return 0;
638 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700639 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
640 if (map->raddr == va &&
641 map->raddr + map->len == va + len &&
642 map->refs == 1) {
643 match = map;
644 hlist_del_init(&map->hn);
645 break;
646 }
647 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700648 if (match) {
649 *ppmap = match;
650 return 0;
651 }
652 return -ENOTTY;
653}
654
c_mtharu7bd6a422017-10-17 18:15:37 +0530655static void fastrpc_mmap_free(struct fastrpc_mmap *map, uint32_t flags)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700656{
c_mtharue1a5ce12017-10-13 20:47:09 +0530657 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700658 struct fastrpc_file *fl;
659 int vmid;
660 struct fastrpc_session_ctx *sess;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700661
662 if (!map)
663 return;
664 fl = map->fl;
c_mtharue1a5ce12017-10-13 20:47:09 +0530665 if (map->flags == ADSP_MMAP_HEAP_ADDR ||
666 map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
667 spin_lock(&me->hlock);
668 map->refs--;
669 if (!map->refs)
670 hlist_del_init(&map->hn);
671 spin_unlock(&me->hlock);
c_mtharu7bd6a422017-10-17 18:15:37 +0530672 if (map->refs > 0)
673 return;
c_mtharue1a5ce12017-10-13 20:47:09 +0530674 } else {
c_mtharue1a5ce12017-10-13 20:47:09 +0530675 map->refs--;
676 if (!map->refs)
677 hlist_del_init(&map->hn);
c_mtharu7bd6a422017-10-17 18:15:37 +0530678 if (map->refs > 0 && !flags)
679 return;
c_mtharue1a5ce12017-10-13 20:47:09 +0530680 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530681 if (map->flags == ADSP_MMAP_HEAP_ADDR ||
682 map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
Tharun Kumar Merugu48d5ff32018-04-16 19:24:16 +0530683 unsigned long dma_attrs = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700684
c_mtharue1a5ce12017-10-13 20:47:09 +0530685 if (me->dev == NULL) {
686 pr_err("failed to free remote heap allocation\n");
687 return;
688 }
689 if (map->phys) {
Tharun Kumar Merugu48d5ff32018-04-16 19:24:16 +0530690 dma_attrs |=
691 DMA_ATTR_SKIP_ZEROING | DMA_ATTR_NO_KERNEL_MAPPING;
692 dma_free_attrs(me->dev, map->size, (void *)map->va,
693 (dma_addr_t)map->phys, dma_attrs);
c_mtharue1a5ce12017-10-13 20:47:09 +0530694 }
Tharun Kumar Merugu35a94a52018-02-01 21:09:04 +0530695 } else if (map->flags == FASTRPC_DMAHANDLE_NOMAP) {
696 if (!IS_ERR_OR_NULL(map->handle))
697 ion_free(fl->apps->client, map->handle);
c_mtharue1a5ce12017-10-13 20:47:09 +0530698 } else {
699 int destVM[1] = {VMID_HLOS};
700 int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
701
702 if (map->secure)
703 sess = fl->secsctx;
704 else
705 sess = fl->sctx;
706
707 if (!IS_ERR_OR_NULL(map->handle))
708 ion_free(fl->apps->client, map->handle);
709 if (sess && sess->smmu.enabled) {
710 if (map->size || map->phys)
711 msm_dma_unmap_sg(sess->smmu.dev,
712 map->table->sgl,
713 map->table->nents, DMA_BIDIRECTIONAL,
714 map->buf);
715 }
716 vmid = fl->apps->channel[fl->cid].vmid;
717 if (vmid && map->phys) {
718 int srcVM[2] = {VMID_HLOS, vmid};
719
720 hyp_assign_phys(map->phys, buf_page_size(map->size),
721 srcVM, 2, destVM, destVMperm, 1);
722 }
723
724 if (!IS_ERR_OR_NULL(map->table))
725 dma_buf_unmap_attachment(map->attach, map->table,
726 DMA_BIDIRECTIONAL);
727 if (!IS_ERR_OR_NULL(map->attach))
728 dma_buf_detach(map->buf, map->attach);
729 if (!IS_ERR_OR_NULL(map->buf))
730 dma_buf_put(map->buf);
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700731 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700732 kfree(map);
733}
734
735static int fastrpc_session_alloc(struct fastrpc_channel_ctx *chan, int secure,
736 struct fastrpc_session_ctx **session);
737
738static int fastrpc_mmap_create(struct fastrpc_file *fl, int fd,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530739 unsigned int attr, uintptr_t va, size_t len, int mflags,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700740 struct fastrpc_mmap **ppmap)
741{
c_mtharue1a5ce12017-10-13 20:47:09 +0530742 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700743 struct fastrpc_session_ctx *sess;
744 struct fastrpc_apps *apps = fl->apps;
745 int cid = fl->cid;
746 struct fastrpc_channel_ctx *chan = &apps->channel[cid];
c_mtharue1a5ce12017-10-13 20:47:09 +0530747 struct fastrpc_mmap *map = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700748 unsigned long attrs;
c_mtharuf931ff92017-11-30 19:35:30 +0530749 dma_addr_t region_phys = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700750 unsigned long flags;
751 int err = 0, vmid;
752
Sathish Ambleyae5ee542017-01-16 22:24:23 -0800753 if (!fastrpc_mmap_find(fl, fd, va, len, mflags, 1, ppmap))
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700754 return 0;
755 map = kzalloc(sizeof(*map), GFP_KERNEL);
756 VERIFY(err, !IS_ERR_OR_NULL(map));
757 if (err)
758 goto bail;
759 INIT_HLIST_NODE(&map->hn);
760 map->flags = mflags;
761 map->refs = 1;
762 map->fl = fl;
763 map->fd = fd;
764 map->attr = attr;
c_mtharue1a5ce12017-10-13 20:47:09 +0530765 if (mflags == ADSP_MMAP_HEAP_ADDR ||
766 mflags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
767 map->apps = me;
768 map->fl = NULL;
Tharun Kumar Merugu48d5ff32018-04-16 19:24:16 +0530769 VERIFY(err, !dma_alloc_memory(&region_phys, len));
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700770 if (err)
771 goto bail;
c_mtharuf931ff92017-11-30 19:35:30 +0530772 map->phys = (uintptr_t)region_phys;
c_mtharue1a5ce12017-10-13 20:47:09 +0530773 map->size = len;
Tharun Kumar Merugu48d5ff32018-04-16 19:24:16 +0530774 map->va = (uintptr_t)map->phys;
Tharun Kumar Merugu35a94a52018-02-01 21:09:04 +0530775 } else if (mflags == FASTRPC_DMAHANDLE_NOMAP) {
776 ion_phys_addr_t iphys;
777
778 VERIFY(err, !IS_ERR_OR_NULL(map->handle =
779 ion_import_dma_buf_fd(fl->apps->client, fd)));
780 if (err)
781 goto bail;
782
783 map->uncached = 1;
784 map->buf = NULL;
785 map->attach = NULL;
786 map->table = NULL;
787 map->va = 0;
788 map->phys = 0;
789
790 err = ion_phys(fl->apps->client, map->handle,
791 &iphys, &map->size);
792 if (err)
793 goto bail;
794 map->phys = (uint64_t)iphys;
c_mtharue1a5ce12017-10-13 20:47:09 +0530795 } else {
c_mtharu7bd6a422017-10-17 18:15:37 +0530796 if (map->attr && (map->attr & FASTRPC_ATTR_KEEP_MAP)) {
797 pr_info("adsprpc: buffer mapped with persist attr %x\n",
798 (unsigned int)map->attr);
799 map->refs = 2;
800 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530801 VERIFY(err, !IS_ERR_OR_NULL(map->handle =
802 ion_import_dma_buf_fd(fl->apps->client, fd)));
803 if (err)
804 goto bail;
805 VERIFY(err, !ion_handle_get_flags(fl->apps->client, map->handle,
806 &flags));
807 if (err)
808 goto bail;
809
c_mtharue1a5ce12017-10-13 20:47:09 +0530810 map->secure = flags & ION_FLAG_SECURE;
811 if (map->secure) {
812 if (!fl->secsctx)
813 err = fastrpc_session_alloc(chan, 1,
814 &fl->secsctx);
815 if (err)
816 goto bail;
817 }
818 if (map->secure)
819 sess = fl->secsctx;
820 else
821 sess = fl->sctx;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +0530822
c_mtharue1a5ce12017-10-13 20:47:09 +0530823 VERIFY(err, !IS_ERR_OR_NULL(sess));
824 if (err)
825 goto bail;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +0530826
827 map->uncached = !ION_IS_CACHED(flags);
828 if (map->attr & FASTRPC_ATTR_NOVA && !sess->smmu.coherent)
829 map->uncached = 1;
830
c_mtharue1a5ce12017-10-13 20:47:09 +0530831 VERIFY(err, !IS_ERR_OR_NULL(map->buf = dma_buf_get(fd)));
832 if (err)
833 goto bail;
834 VERIFY(err, !IS_ERR_OR_NULL(map->attach =
835 dma_buf_attach(map->buf, sess->smmu.dev)));
836 if (err)
837 goto bail;
838 VERIFY(err, !IS_ERR_OR_NULL(map->table =
839 dma_buf_map_attachment(map->attach,
840 DMA_BIDIRECTIONAL)));
841 if (err)
842 goto bail;
843 if (sess->smmu.enabled) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700844 attrs = DMA_ATTR_EXEC_MAPPING;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +0530845
846 if (map->attr & FASTRPC_ATTR_NON_COHERENT ||
847 (sess->smmu.coherent && map->uncached))
848 attrs |= DMA_ATTR_FORCE_NON_COHERENT;
849 else if (map->attr & FASTRPC_ATTR_COHERENT)
850 attrs |= DMA_ATTR_FORCE_COHERENT;
851
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700852 VERIFY(err, map->table->nents ==
c_mtharue1a5ce12017-10-13 20:47:09 +0530853 msm_dma_map_sg_attrs(sess->smmu.dev,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700854 map->table->sgl, map->table->nents,
855 DMA_BIDIRECTIONAL, map->buf, attrs));
c_mtharue1a5ce12017-10-13 20:47:09 +0530856 if (err)
857 goto bail;
858 } else {
859 VERIFY(err, map->table->nents == 1);
860 if (err)
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700861 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +0530862 }
863 map->phys = sg_dma_address(map->table->sgl);
Tharun Kumar Merugu93f319a2018-02-01 17:35:42 +0530864
c_mtharue1a5ce12017-10-13 20:47:09 +0530865 if (sess->smmu.cb) {
866 map->phys += ((uint64_t)sess->smmu.cb << 32);
867 map->size = sg_dma_len(map->table->sgl);
868 } else {
869 map->size = buf_page_size(len);
870 }
Tharun Kumar Merugu93f319a2018-02-01 17:35:42 +0530871
c_mtharue1a5ce12017-10-13 20:47:09 +0530872 vmid = fl->apps->channel[fl->cid].vmid;
Tharun Kumar Merugu93f319a2018-02-01 17:35:42 +0530873 if (!sess->smmu.enabled && !vmid) {
874 VERIFY(err, map->phys >= me->range.addr &&
875 map->phys + map->size <=
876 me->range.addr + me->range.size);
877 if (err) {
878 pr_err("adsprpc: mmap fail out of range\n");
879 goto bail;
880 }
881 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530882 if (vmid) {
883 int srcVM[1] = {VMID_HLOS};
884 int destVM[2] = {VMID_HLOS, vmid};
885 int destVMperm[2] = {PERM_READ | PERM_WRITE,
886 PERM_READ | PERM_WRITE | PERM_EXEC};
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700887
c_mtharue1a5ce12017-10-13 20:47:09 +0530888 VERIFY(err, !hyp_assign_phys(map->phys,
889 buf_page_size(map->size),
890 srcVM, 1, destVM, destVMperm, 2));
891 if (err)
892 goto bail;
893 }
894 map->va = va;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700895 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700896 map->len = len;
897
898 fastrpc_mmap_add(map);
899 *ppmap = map;
900
901bail:
902 if (err && map)
c_mtharu7bd6a422017-10-17 18:15:37 +0530903 fastrpc_mmap_free(map, 0);
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700904 return err;
905}
906
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +0530907static int fastrpc_buf_alloc(struct fastrpc_file *fl, size_t size,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700908 struct fastrpc_buf **obuf)
909{
910 int err = 0, vmid;
c_mtharue1a5ce12017-10-13 20:47:09 +0530911 struct fastrpc_buf *buf = NULL, *fr = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700912 struct hlist_node *n;
913
914 VERIFY(err, size > 0);
915 if (err)
916 goto bail;
917
918 /* find the smallest buffer that fits in the cache */
919 spin_lock(&fl->hlock);
920 hlist_for_each_entry_safe(buf, n, &fl->bufs, hn) {
921 if (buf->size >= size && (!fr || fr->size > buf->size))
922 fr = buf;
923 }
924 if (fr)
925 hlist_del_init(&fr->hn);
926 spin_unlock(&fl->hlock);
927 if (fr) {
928 *obuf = fr;
929 return 0;
930 }
c_mtharue1a5ce12017-10-13 20:47:09 +0530931 buf = NULL;
932 VERIFY(err, NULL != (buf = kzalloc(sizeof(*buf), GFP_KERNEL)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700933 if (err)
934 goto bail;
935 INIT_HLIST_NODE(&buf->hn);
936 buf->fl = fl;
c_mtharue1a5ce12017-10-13 20:47:09 +0530937 buf->virt = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700938 buf->phys = 0;
939 buf->size = size;
c_mtharue1a5ce12017-10-13 20:47:09 +0530940 buf->virt = dma_alloc_coherent(fl->sctx->smmu.dev, buf->size,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700941 (void *)&buf->phys, GFP_KERNEL);
942 if (IS_ERR_OR_NULL(buf->virt)) {
943 /* free cache and retry */
944 fastrpc_buf_list_free(fl);
c_mtharue1a5ce12017-10-13 20:47:09 +0530945 buf->virt = dma_alloc_coherent(fl->sctx->smmu.dev, buf->size,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700946 (void *)&buf->phys, GFP_KERNEL);
947 VERIFY(err, !IS_ERR_OR_NULL(buf->virt));
948 }
949 if (err)
950 goto bail;
951 if (fl->sctx->smmu.cb)
952 buf->phys += ((uint64_t)fl->sctx->smmu.cb << 32);
953 vmid = fl->apps->channel[fl->cid].vmid;
954 if (vmid) {
955 int srcVM[1] = {VMID_HLOS};
956 int destVM[2] = {VMID_HLOS, vmid};
957 int destVMperm[2] = {PERM_READ | PERM_WRITE,
958 PERM_READ | PERM_WRITE | PERM_EXEC};
959
960 VERIFY(err, !hyp_assign_phys(buf->phys, buf_page_size(size),
961 srcVM, 1, destVM, destVMperm, 2));
962 if (err)
963 goto bail;
964 }
965
966 *obuf = buf;
967 bail:
968 if (err && buf)
969 fastrpc_buf_free(buf, 0);
970 return err;
971}
972
973
974static int context_restore_interrupted(struct fastrpc_file *fl,
Sathish Ambleybae51902017-07-03 15:00:49 -0700975 struct fastrpc_ioctl_invoke_crc *inv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700976 struct smq_invoke_ctx **po)
977{
978 int err = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +0530979 struct smq_invoke_ctx *ctx = NULL, *ictx = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -0700980 struct hlist_node *n;
981 struct fastrpc_ioctl_invoke *invoke = &inv->inv;
982
983 spin_lock(&fl->hlock);
984 hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) {
985 if (ictx->pid == current->pid) {
986 if (invoke->sc != ictx->sc || ictx->fl != fl)
987 err = -1;
988 else {
989 ctx = ictx;
990 hlist_del_init(&ctx->hn);
991 hlist_add_head(&ctx->hn, &fl->clst.pending);
992 }
993 break;
994 }
995 }
996 spin_unlock(&fl->hlock);
997 if (ctx)
998 *po = ctx;
999 return err;
1000}
1001
1002#define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)
1003static int overlap_ptr_cmp(const void *a, const void *b)
1004{
1005 struct overlap *pa = *((struct overlap **)a);
1006 struct overlap *pb = *((struct overlap **)b);
1007 /* sort with lowest starting buffer first */
1008 int st = CMP(pa->start, pb->start);
1009 /* sort with highest ending buffer first */
1010 int ed = CMP(pb->end, pa->end);
1011 return st == 0 ? ed : st;
1012}
1013
Sathish Ambley9466d672017-01-25 10:51:55 -08001014static int context_build_overlap(struct smq_invoke_ctx *ctx)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001015{
Sathish Ambley9466d672017-01-25 10:51:55 -08001016 int i, err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001017 remote_arg_t *lpra = ctx->lpra;
1018 int inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
1019 int outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc);
1020 int nbufs = inbufs + outbufs;
1021 struct overlap max;
1022
1023 for (i = 0; i < nbufs; ++i) {
1024 ctx->overs[i].start = (uintptr_t)lpra[i].buf.pv;
1025 ctx->overs[i].end = ctx->overs[i].start + lpra[i].buf.len;
Sathish Ambley9466d672017-01-25 10:51:55 -08001026 if (lpra[i].buf.len) {
1027 VERIFY(err, ctx->overs[i].end > ctx->overs[i].start);
1028 if (err)
1029 goto bail;
1030 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001031 ctx->overs[i].raix = i;
1032 ctx->overps[i] = &ctx->overs[i];
1033 }
c_mtharue1a5ce12017-10-13 20:47:09 +05301034 sort(ctx->overps, nbufs, sizeof(*ctx->overps), overlap_ptr_cmp, NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001035 max.start = 0;
1036 max.end = 0;
1037 for (i = 0; i < nbufs; ++i) {
1038 if (ctx->overps[i]->start < max.end) {
1039 ctx->overps[i]->mstart = max.end;
1040 ctx->overps[i]->mend = ctx->overps[i]->end;
1041 ctx->overps[i]->offset = max.end -
1042 ctx->overps[i]->start;
1043 if (ctx->overps[i]->end > max.end) {
1044 max.end = ctx->overps[i]->end;
1045 } else {
1046 ctx->overps[i]->mend = 0;
1047 ctx->overps[i]->mstart = 0;
1048 }
1049 } else {
1050 ctx->overps[i]->mend = ctx->overps[i]->end;
1051 ctx->overps[i]->mstart = ctx->overps[i]->start;
1052 ctx->overps[i]->offset = 0;
1053 max = *ctx->overps[i];
1054 }
1055 }
Sathish Ambley9466d672017-01-25 10:51:55 -08001056bail:
1057 return err;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001058}
1059
1060#define K_COPY_FROM_USER(err, kernel, dst, src, size) \
1061 do {\
1062 if (!(kernel))\
c_mtharue1a5ce12017-10-13 20:47:09 +05301063 VERIFY(err, 0 == copy_from_user((dst),\
1064 (void const __user *)(src),\
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001065 (size)));\
1066 else\
1067 memmove((dst), (src), (size));\
1068 } while (0)
1069
1070#define K_COPY_TO_USER(err, kernel, dst, src, size) \
1071 do {\
1072 if (!(kernel))\
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301073 VERIFY(err, 0 == copy_to_user((void __user *)(dst),\
c_mtharue1a5ce12017-10-13 20:47:09 +05301074 (src), (size)));\
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001075 else\
1076 memmove((dst), (src), (size));\
1077 } while (0)
1078
1079
1080static void context_free(struct smq_invoke_ctx *ctx);
1081
1082static int context_alloc(struct fastrpc_file *fl, uint32_t kernel,
Sathish Ambleybae51902017-07-03 15:00:49 -07001083 struct fastrpc_ioctl_invoke_crc *invokefd,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001084 struct smq_invoke_ctx **po)
1085{
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301086 struct fastrpc_apps *me = &gfa;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301087 int err = 0, bufs, ii, size = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05301088 struct smq_invoke_ctx *ctx = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001089 struct fastrpc_ctx_lst *clst = &fl->clst;
1090 struct fastrpc_ioctl_invoke *invoke = &invokefd->inv;
1091
1092 bufs = REMOTE_SCALARS_LENGTH(invoke->sc);
1093 size = bufs * sizeof(*ctx->lpra) + bufs * sizeof(*ctx->maps) +
1094 sizeof(*ctx->fds) * (bufs) +
1095 sizeof(*ctx->attrs) * (bufs) +
1096 sizeof(*ctx->overs) * (bufs) +
1097 sizeof(*ctx->overps) * (bufs);
1098
c_mtharue1a5ce12017-10-13 20:47:09 +05301099 VERIFY(err, NULL != (ctx = kzalloc(sizeof(*ctx) + size, GFP_KERNEL)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001100 if (err)
1101 goto bail;
1102
1103 INIT_HLIST_NODE(&ctx->hn);
1104 hlist_add_fake(&ctx->hn);
1105 ctx->fl = fl;
1106 ctx->maps = (struct fastrpc_mmap **)(&ctx[1]);
1107 ctx->lpra = (remote_arg_t *)(&ctx->maps[bufs]);
1108 ctx->fds = (int *)(&ctx->lpra[bufs]);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301109 if (me->legacy) {
1110 ctx->overs = (struct overlap *)(&ctx->fds[bufs]);
1111 ctx->overps = (struct overlap **)(&ctx->overs[bufs]);
1112 } else {
1113 ctx->attrs = (unsigned int *)(&ctx->fds[bufs]);
1114 ctx->overs = (struct overlap *)(&ctx->attrs[bufs]);
1115 ctx->overps = (struct overlap **)(&ctx->overs[bufs]);
1116 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001117
c_mtharue1a5ce12017-10-13 20:47:09 +05301118 K_COPY_FROM_USER(err, kernel, (void *)ctx->lpra, invoke->pra,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001119 bufs * sizeof(*ctx->lpra));
1120 if (err)
1121 goto bail;
1122
1123 if (invokefd->fds) {
1124 K_COPY_FROM_USER(err, kernel, ctx->fds, invokefd->fds,
1125 bufs * sizeof(*ctx->fds));
1126 if (err)
1127 goto bail;
1128 }
1129 if (invokefd->attrs) {
1130 K_COPY_FROM_USER(err, kernel, ctx->attrs, invokefd->attrs,
1131 bufs * sizeof(*ctx->attrs));
1132 if (err)
1133 goto bail;
1134 }
Sathish Ambleybae51902017-07-03 15:00:49 -07001135 ctx->crc = (uint32_t *)invokefd->crc;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001136 ctx->sc = invoke->sc;
Sathish Ambley9466d672017-01-25 10:51:55 -08001137 if (bufs) {
1138 VERIFY(err, 0 == context_build_overlap(ctx));
1139 if (err)
1140 goto bail;
1141 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001142 ctx->retval = -1;
1143 ctx->pid = current->pid;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301144 ctx->tgid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001145 init_completion(&ctx->work);
c_mtharufdac6892017-10-12 13:09:01 +05301146 ctx->magic = FASTRPC_CTX_MAGIC;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001147
1148 spin_lock(&fl->hlock);
1149 hlist_add_head(&ctx->hn, &clst->pending);
1150 spin_unlock(&fl->hlock);
1151
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301152 spin_lock(&me->ctxlock);
1153 for (ii = 0; ii < FASTRPC_CTX_MAX; ii++) {
1154 if (!me->ctxtable[ii]) {
1155 me->ctxtable[ii] = ctx;
1156 ctx->ctxid = (ptr_to_uint64(ctx) & ~0xFFF)|(ii << 4);
1157 break;
1158 }
1159 }
1160 spin_unlock(&me->ctxlock);
1161 VERIFY(err, ii < FASTRPC_CTX_MAX);
1162 if (err) {
1163 pr_err("adsprpc: out of context memory\n");
1164 goto bail;
1165 }
1166
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001167 *po = ctx;
1168bail:
1169 if (ctx && err)
1170 context_free(ctx);
1171 return err;
1172}
1173
1174static void context_save_interrupted(struct smq_invoke_ctx *ctx)
1175{
1176 struct fastrpc_ctx_lst *clst = &ctx->fl->clst;
1177
1178 spin_lock(&ctx->fl->hlock);
1179 hlist_del_init(&ctx->hn);
1180 hlist_add_head(&ctx->hn, &clst->interrupted);
1181 spin_unlock(&ctx->fl->hlock);
1182 /* free the cache on power collapse */
1183 fastrpc_buf_list_free(ctx->fl);
1184}
1185
1186static void context_free(struct smq_invoke_ctx *ctx)
1187{
1188 int i;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301189 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001190 int nbufs = REMOTE_SCALARS_INBUFS(ctx->sc) +
1191 REMOTE_SCALARS_OUTBUFS(ctx->sc);
1192 spin_lock(&ctx->fl->hlock);
1193 hlist_del_init(&ctx->hn);
1194 spin_unlock(&ctx->fl->hlock);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301195 mutex_lock(&ctx->fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001196 for (i = 0; i < nbufs; ++i)
c_mtharu7bd6a422017-10-17 18:15:37 +05301197 fastrpc_mmap_free(ctx->maps[i], 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301198
1199 mutex_unlock(&ctx->fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001200 fastrpc_buf_free(ctx->buf, 1);
c_mtharufdac6892017-10-12 13:09:01 +05301201 ctx->magic = 0;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301202 ctx->ctxid = 0;
1203
1204 spin_lock(&me->ctxlock);
1205 for (i = 0; i < FASTRPC_CTX_MAX; i++) {
1206 if (me->ctxtable[i] == ctx) {
1207 me->ctxtable[i] = NULL;
1208 break;
1209 }
1210 }
1211 spin_unlock(&me->ctxlock);
1212
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001213 kfree(ctx);
1214}
1215
1216static void context_notify_user(struct smq_invoke_ctx *ctx, int retval)
1217{
1218 ctx->retval = retval;
1219 complete(&ctx->work);
1220}
1221
1222
1223static void fastrpc_notify_users(struct fastrpc_file *me)
1224{
1225 struct smq_invoke_ctx *ictx;
1226 struct hlist_node *n;
1227
1228 spin_lock(&me->hlock);
1229 hlist_for_each_entry_safe(ictx, n, &me->clst.pending, hn) {
1230 complete(&ictx->work);
1231 }
1232 hlist_for_each_entry_safe(ictx, n, &me->clst.interrupted, hn) {
1233 complete(&ictx->work);
1234 }
1235 spin_unlock(&me->hlock);
1236
1237}
1238
Tharun Kumar Merugu77dd5872018-04-02 12:48:17 +05301239
1240static void fastrpc_notify_users_staticpd_pdr(struct fastrpc_file *me)
1241{
1242 struct smq_invoke_ctx *ictx;
1243 struct hlist_node *n;
1244
1245 spin_lock(&me->hlock);
1246 hlist_for_each_entry_safe(ictx, n, &me->clst.pending, hn) {
1247 if (ictx->msg.pid)
1248 complete(&ictx->work);
1249 }
1250 hlist_for_each_entry_safe(ictx, n, &me->clst.interrupted, hn) {
1251 if (ictx->msg.pid)
1252 complete(&ictx->work);
1253 }
1254 spin_unlock(&me->hlock);
1255}
1256
1257
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001258static void fastrpc_notify_drivers(struct fastrpc_apps *me, int cid)
1259{
1260 struct fastrpc_file *fl;
1261 struct hlist_node *n;
1262
1263 spin_lock(&me->hlock);
1264 hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
1265 if (fl->cid == cid)
1266 fastrpc_notify_users(fl);
1267 }
1268 spin_unlock(&me->hlock);
1269
1270}
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05301271
1272static void fastrpc_notify_pdr_drivers(struct fastrpc_apps *me, char *spdname)
1273{
1274 struct fastrpc_file *fl;
1275 struct hlist_node *n;
1276
1277 spin_lock(&me->hlock);
1278 hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
1279 if (fl->spdname && !strcmp(spdname, fl->spdname))
Tharun Kumar Merugu77dd5872018-04-02 12:48:17 +05301280 fastrpc_notify_users_staticpd_pdr(fl);
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05301281 }
1282 spin_unlock(&me->hlock);
1283
1284}
1285
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001286static void context_list_ctor(struct fastrpc_ctx_lst *me)
1287{
1288 INIT_HLIST_HEAD(&me->interrupted);
1289 INIT_HLIST_HEAD(&me->pending);
1290}
1291
1292static void fastrpc_context_list_dtor(struct fastrpc_file *fl)
1293{
1294 struct fastrpc_ctx_lst *clst = &fl->clst;
c_mtharue1a5ce12017-10-13 20:47:09 +05301295 struct smq_invoke_ctx *ictx = NULL, *ctxfree;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001296 struct hlist_node *n;
1297
1298 do {
c_mtharue1a5ce12017-10-13 20:47:09 +05301299 ctxfree = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001300 spin_lock(&fl->hlock);
1301 hlist_for_each_entry_safe(ictx, n, &clst->interrupted, hn) {
1302 hlist_del_init(&ictx->hn);
1303 ctxfree = ictx;
1304 break;
1305 }
1306 spin_unlock(&fl->hlock);
1307 if (ctxfree)
1308 context_free(ctxfree);
1309 } while (ctxfree);
1310 do {
c_mtharue1a5ce12017-10-13 20:47:09 +05301311 ctxfree = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001312 spin_lock(&fl->hlock);
1313 hlist_for_each_entry_safe(ictx, n, &clst->pending, hn) {
1314 hlist_del_init(&ictx->hn);
1315 ctxfree = ictx;
1316 break;
1317 }
1318 spin_unlock(&fl->hlock);
1319 if (ctxfree)
1320 context_free(ctxfree);
1321 } while (ctxfree);
1322}
1323
1324static int fastrpc_file_free(struct fastrpc_file *fl);
1325static void fastrpc_file_list_dtor(struct fastrpc_apps *me)
1326{
1327 struct fastrpc_file *fl, *free;
1328 struct hlist_node *n;
1329
1330 do {
c_mtharue1a5ce12017-10-13 20:47:09 +05301331 free = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001332 spin_lock(&me->hlock);
1333 hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
1334 hlist_del_init(&fl->hn);
1335 free = fl;
1336 break;
1337 }
1338 spin_unlock(&me->hlock);
1339 if (free)
1340 fastrpc_file_free(free);
1341 } while (free);
1342}
1343
1344static int get_args(uint32_t kernel, struct smq_invoke_ctx *ctx)
1345{
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301346 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001347 remote_arg64_t *rpra;
1348 remote_arg_t *lpra = ctx->lpra;
1349 struct smq_invoke_buf *list;
1350 struct smq_phy_page *pages, *ipage;
1351 uint32_t sc = ctx->sc;
1352 int inbufs = REMOTE_SCALARS_INBUFS(sc);
1353 int outbufs = REMOTE_SCALARS_OUTBUFS(sc);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001354 int handles, bufs = inbufs + outbufs;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001355 uintptr_t args;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301356 size_t rlen = 0, copylen = 0, metalen = 0;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001357 int i, oix;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001358 int err = 0;
1359 int mflags = 0;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001360 uint64_t *fdlist;
Sathish Ambleybae51902017-07-03 15:00:49 -07001361 uint32_t *crclist;
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301362 int64_t *perf_counter = getperfcounter(ctx->fl, PERF_COUNT);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001363
1364 /* calculate size of the metadata */
c_mtharue1a5ce12017-10-13 20:47:09 +05301365 rpra = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001366 list = smq_invoke_buf_start(rpra, sc);
1367 pages = smq_phy_page_start(sc, list);
1368 ipage = pages;
1369
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301370 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_MAP),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001371 for (i = 0; i < bufs; ++i) {
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301372 uintptr_t buf = (uintptr_t)lpra[i].buf.pv;
1373 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001374
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301375 mutex_lock(&ctx->fl->fl_map_mutex);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301376 if (ctx->fds[i] && (ctx->fds[i] != -1)) {
1377 unsigned int attrs = 0;
1378
1379 if (ctx->attrs)
1380 attrs = ctx->attrs[i];
1381
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001382 fastrpc_mmap_create(ctx->fl, ctx->fds[i],
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301383 attrs, buf, len,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001384 mflags, &ctx->maps[i]);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301385 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301386 mutex_unlock(&ctx->fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001387 ipage += 1;
1388 }
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301389 PERF_END);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001390 handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301391 mutex_lock(&ctx->fl->fl_map_mutex);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001392 for (i = bufs; i < bufs + handles; i++) {
Tharun Kumar Merugu35a94a52018-02-01 21:09:04 +05301393 int dmaflags = 0;
1394
1395 if (ctx->attrs && (ctx->attrs[i] & FASTRPC_ATTR_NOMAP))
1396 dmaflags = FASTRPC_DMAHANDLE_NOMAP;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001397 VERIFY(err, !fastrpc_mmap_create(ctx->fl, ctx->fds[i],
Tharun Kumar Merugu35a94a52018-02-01 21:09:04 +05301398 FASTRPC_ATTR_NOVA, 0, 0, dmaflags, &ctx->maps[i]));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301399 if (err) {
1400 mutex_unlock(&ctx->fl->fl_map_mutex);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001401 goto bail;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301402 }
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001403 ipage += 1;
1404 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301405 mutex_unlock(&ctx->fl->fl_map_mutex);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301406 if (!me->legacy) {
1407 metalen = copylen = (size_t)&ipage[0] +
1408 (sizeof(uint64_t) * M_FDLIST) +
1409 (sizeof(uint32_t) * M_CRCLIST);
1410 } else {
1411 metalen = copylen = (size_t)&ipage[0];
1412 }
Sathish Ambleybae51902017-07-03 15:00:49 -07001413
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001414 /* calculate len requreed for copying */
1415 for (oix = 0; oix < inbufs + outbufs; ++oix) {
1416 int i = ctx->overps[oix]->raix;
Sathish Ambleyd209c1e2016-12-13 15:27:30 -08001417 uintptr_t mstart, mend;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301418 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001419
1420 if (!len)
1421 continue;
1422 if (ctx->maps[i])
1423 continue;
1424 if (ctx->overps[oix]->offset == 0)
1425 copylen = ALIGN(copylen, BALIGN);
Sathish Ambleyd209c1e2016-12-13 15:27:30 -08001426 mstart = ctx->overps[oix]->mstart;
1427 mend = ctx->overps[oix]->mend;
1428 VERIFY(err, (mend - mstart) <= LONG_MAX);
1429 if (err)
1430 goto bail;
1431 copylen += mend - mstart;
1432 VERIFY(err, copylen >= 0);
1433 if (err)
1434 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001435 }
1436 ctx->used = copylen;
1437
1438 /* allocate new buffer */
1439 if (copylen) {
1440 VERIFY(err, !fastrpc_buf_alloc(ctx->fl, copylen, &ctx->buf));
1441 if (err)
1442 goto bail;
1443 }
Tharun Kumar Merugue3361f92017-06-22 10:45:43 +05301444 if (ctx->buf->virt && metalen <= copylen)
1445 memset(ctx->buf->virt, 0, metalen);
1446
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001447 /* copy metadata */
1448 rpra = ctx->buf->virt;
1449 ctx->rpra = rpra;
1450 list = smq_invoke_buf_start(rpra, sc);
1451 pages = smq_phy_page_start(sc, list);
1452 ipage = pages;
1453 args = (uintptr_t)ctx->buf->virt + metalen;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001454 for (i = 0; i < bufs + handles; ++i) {
1455 if (lpra[i].buf.len)
1456 list[i].num = 1;
1457 else
1458 list[i].num = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001459 list[i].pgidx = ipage - pages;
1460 ipage++;
1461 }
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05301462
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001463 /* map ion buffers */
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301464 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_MAP),
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05301465 for (i = 0; rpra && i < inbufs + outbufs; ++i) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001466 struct fastrpc_mmap *map = ctx->maps[i];
1467 uint64_t buf = ptr_to_uint64(lpra[i].buf.pv);
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301468 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001469
1470 rpra[i].buf.pv = 0;
1471 rpra[i].buf.len = len;
1472 if (!len)
1473 continue;
1474 if (map) {
1475 struct vm_area_struct *vma;
1476 uintptr_t offset;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301477 uint64_t num = buf_num_pages(buf, len);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001478 int idx = list[i].pgidx;
1479
1480 if (map->attr & FASTRPC_ATTR_NOVA) {
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001481 offset = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001482 } else {
1483 down_read(&current->mm->mmap_sem);
1484 VERIFY(err, NULL != (vma = find_vma(current->mm,
1485 map->va)));
1486 if (err) {
1487 up_read(&current->mm->mmap_sem);
1488 goto bail;
1489 }
1490 offset = buf_page_start(buf) - vma->vm_start;
1491 up_read(&current->mm->mmap_sem);
1492 VERIFY(err, offset < (uintptr_t)map->size);
1493 if (err)
1494 goto bail;
1495 }
1496 pages[idx].addr = map->phys + offset;
1497 pages[idx].size = num << PAGE_SHIFT;
1498 }
1499 rpra[i].buf.pv = buf;
1500 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001501 PERF_END);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001502 for (i = bufs; i < bufs + handles; ++i) {
1503 struct fastrpc_mmap *map = ctx->maps[i];
1504
1505 pages[i].addr = map->phys;
1506 pages[i].size = map->size;
1507 }
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301508 if (!me->legacy) {
1509 fdlist = (uint64_t *)&pages[bufs + handles];
1510 for (i = 0; i < M_FDLIST; i++)
1511 fdlist[i] = 0;
1512 crclist = (uint32_t *)&fdlist[M_FDLIST];
1513 memset(crclist, 0, sizeof(uint32_t)*M_CRCLIST);
1514 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001515
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001516 /* copy non ion buffers */
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301517 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_COPY),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001518 rlen = copylen - metalen;
Tharun Kumar Meruguc230bd72018-01-29 18:02:42 +05301519 for (oix = 0; rpra && oix < inbufs + outbufs; ++oix) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001520 int i = ctx->overps[oix]->raix;
1521 struct fastrpc_mmap *map = ctx->maps[i];
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301522 size_t mlen;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001523 uint64_t buf;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05301524 size_t len = lpra[i].buf.len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001525
1526 if (!len)
1527 continue;
1528 if (map)
1529 continue;
1530 if (ctx->overps[oix]->offset == 0) {
1531 rlen -= ALIGN(args, BALIGN) - args;
1532 args = ALIGN(args, BALIGN);
1533 }
Sathish Ambleyd209c1e2016-12-13 15:27:30 -08001534 mlen = ctx->overps[oix]->mend - ctx->overps[oix]->mstart;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001535 VERIFY(err, rlen >= mlen);
1536 if (err)
1537 goto bail;
1538 rpra[i].buf.pv = (args - ctx->overps[oix]->offset);
1539 pages[list[i].pgidx].addr = ctx->buf->phys -
1540 ctx->overps[oix]->offset +
1541 (copylen - rlen);
1542 pages[list[i].pgidx].addr =
1543 buf_page_start(pages[list[i].pgidx].addr);
1544 buf = rpra[i].buf.pv;
1545 pages[list[i].pgidx].size = buf_num_pages(buf, len) * PAGE_SIZE;
1546 if (i < inbufs) {
1547 K_COPY_FROM_USER(err, kernel, uint64_to_ptr(buf),
1548 lpra[i].buf.pv, len);
1549 if (err)
1550 goto bail;
1551 }
1552 args = args + mlen;
1553 rlen -= mlen;
1554 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001555 PERF_END);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001556
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301557 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_FLUSH),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001558 for (oix = 0; oix < inbufs + outbufs; ++oix) {
1559 int i = ctx->overps[oix]->raix;
1560 struct fastrpc_mmap *map = ctx->maps[i];
1561
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001562 if (map && map->uncached)
1563 continue;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +05301564 if (ctx->fl->sctx->smmu.coherent &&
1565 !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT)))
1566 continue;
1567 if (map && (map->attr & FASTRPC_ATTR_COHERENT))
1568 continue;
1569
Tharun Kumar Meruguc230bd72018-01-29 18:02:42 +05301570 if (rpra && rpra[i].buf.len && ctx->overps[oix]->mstart)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001571 dmac_flush_range(uint64_to_ptr(rpra[i].buf.pv),
1572 uint64_to_ptr(rpra[i].buf.pv + rpra[i].buf.len));
1573 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001574 PERF_END);
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05301575 for (i = bufs; rpra && i < bufs + handles; i++) {
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001576 rpra[i].dma.fd = ctx->fds[i];
1577 rpra[i].dma.len = (uint32_t)lpra[i].buf.len;
1578 rpra[i].dma.offset = (uint32_t)(uintptr_t)lpra[i].buf.pv;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001579 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001580
1581 if (!ctx->fl->sctx->smmu.coherent) {
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301582 PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_FLUSH),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001583 dmac_flush_range((char *)rpra, (char *)rpra + ctx->used);
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001584 PERF_END);
1585 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001586 bail:
1587 return err;
1588}
1589
1590static int put_args(uint32_t kernel, struct smq_invoke_ctx *ctx,
1591 remote_arg_t *upra)
1592{
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301593 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001594 uint32_t sc = ctx->sc;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001595 struct smq_invoke_buf *list;
1596 struct smq_phy_page *pages;
1597 struct fastrpc_mmap *mmap;
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301598 uint64_t *fdlist = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07001599 uint32_t *crclist = NULL;
1600
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001601 remote_arg64_t *rpra = ctx->rpra;
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001602 int i, inbufs, outbufs, handles;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001603 int err = 0;
1604
1605 inbufs = REMOTE_SCALARS_INBUFS(sc);
1606 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001607 handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc);
1608 list = smq_invoke_buf_start(ctx->rpra, sc);
1609 pages = smq_phy_page_start(sc, list);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301610 if (!me->legacy) {
1611 fdlist = (uint64_t *)(pages + inbufs + outbufs + handles);
1612 crclist = (uint32_t *)(fdlist + M_FDLIST);
1613 }
Sathish Ambleybae51902017-07-03 15:00:49 -07001614
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001615 for (i = inbufs; i < inbufs + outbufs; ++i) {
1616 if (!ctx->maps[i]) {
1617 K_COPY_TO_USER(err, kernel,
1618 ctx->lpra[i].buf.pv,
1619 uint64_to_ptr(rpra[i].buf.pv),
1620 rpra[i].buf.len);
1621 if (err)
1622 goto bail;
1623 } else {
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301624 mutex_lock(&ctx->fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05301625 fastrpc_mmap_free(ctx->maps[i], 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301626 mutex_unlock(&ctx->fl->fl_map_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05301627 ctx->maps[i] = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001628 }
1629 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301630 mutex_lock(&ctx->fl->fl_map_mutex);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301631 if (fdlist && (inbufs + outbufs + handles)) {
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001632 for (i = 0; i < M_FDLIST; i++) {
1633 if (!fdlist[i])
1634 break;
1635 if (!fastrpc_mmap_find(ctx->fl, (int)fdlist[i], 0, 0,
Sathish Ambleyae5ee542017-01-16 22:24:23 -08001636 0, 0, &mmap))
c_mtharu7bd6a422017-10-17 18:15:37 +05301637 fastrpc_mmap_free(mmap, 0);
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001638 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001639 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05301640 mutex_unlock(&ctx->fl->fl_map_mutex);
Sathish Ambleybae51902017-07-03 15:00:49 -07001641 if (ctx->crc && crclist && rpra)
c_mtharue1a5ce12017-10-13 20:47:09 +05301642 K_COPY_TO_USER(err, kernel, ctx->crc,
Sathish Ambleybae51902017-07-03 15:00:49 -07001643 crclist, M_CRCLIST*sizeof(uint32_t));
1644
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001645 bail:
1646 return err;
1647}
1648
1649static void inv_args_pre(struct smq_invoke_ctx *ctx)
1650{
1651 int i, inbufs, outbufs;
1652 uint32_t sc = ctx->sc;
1653 remote_arg64_t *rpra = ctx->rpra;
1654 uintptr_t end;
1655
1656 inbufs = REMOTE_SCALARS_INBUFS(sc);
1657 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
1658 for (i = inbufs; i < inbufs + outbufs; ++i) {
1659 struct fastrpc_mmap *map = ctx->maps[i];
1660
1661 if (map && map->uncached)
1662 continue;
1663 if (!rpra[i].buf.len)
1664 continue;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +05301665 if (ctx->fl->sctx->smmu.coherent &&
1666 !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT)))
1667 continue;
1668 if (map && (map->attr & FASTRPC_ATTR_COHERENT))
1669 continue;
1670
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001671 if (buf_page_start(ptr_to_uint64((void *)rpra)) ==
1672 buf_page_start(rpra[i].buf.pv))
1673 continue;
1674 if (!IS_CACHE_ALIGNED((uintptr_t)uint64_to_ptr(rpra[i].buf.pv)))
1675 dmac_flush_range(uint64_to_ptr(rpra[i].buf.pv),
1676 (char *)(uint64_to_ptr(rpra[i].buf.pv + 1)));
1677 end = (uintptr_t)uint64_to_ptr(rpra[i].buf.pv +
1678 rpra[i].buf.len);
1679 if (!IS_CACHE_ALIGNED(end))
1680 dmac_flush_range((char *)end,
1681 (char *)end + 1);
1682 }
1683}
1684
1685static void inv_args(struct smq_invoke_ctx *ctx)
1686{
1687 int i, inbufs, outbufs;
1688 uint32_t sc = ctx->sc;
1689 remote_arg64_t *rpra = ctx->rpra;
1690 int used = ctx->used;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001691
1692 inbufs = REMOTE_SCALARS_INBUFS(sc);
1693 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
1694 for (i = inbufs; i < inbufs + outbufs; ++i) {
1695 struct fastrpc_mmap *map = ctx->maps[i];
1696
1697 if (map && map->uncached)
1698 continue;
1699 if (!rpra[i].buf.len)
1700 continue;
Tharun Kumar Merugu2e5f12e2017-07-06 12:04:40 +05301701 if (ctx->fl->sctx->smmu.coherent &&
1702 !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT)))
1703 continue;
1704 if (map && (map->attr & FASTRPC_ATTR_COHERENT))
1705 continue;
1706
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001707 if (buf_page_start(ptr_to_uint64((void *)rpra)) ==
1708 buf_page_start(rpra[i].buf.pv)) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001709 continue;
1710 }
1711 if (map && map->handle)
1712 msm_ion_do_cache_op(ctx->fl->apps->client, map->handle,
1713 (char *)uint64_to_ptr(rpra[i].buf.pv),
1714 rpra[i].buf.len, ION_IOC_INV_CACHES);
1715 else
1716 dmac_inv_range((char *)uint64_to_ptr(rpra[i].buf.pv),
1717 (char *)uint64_to_ptr(rpra[i].buf.pv
1718 + rpra[i].buf.len));
1719 }
1720
Sathish Ambley58dc64d2016-11-29 17:11:53 -08001721 if (rpra)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001722 dmac_inv_range(rpra, (char *)rpra + used);
1723}
1724
1725static int fastrpc_invoke_send(struct smq_invoke_ctx *ctx,
1726 uint32_t kernel, uint32_t handle)
1727{
1728 struct smq_msg *msg = &ctx->msg;
1729 struct fastrpc_file *fl = ctx->fl;
1730 struct fastrpc_channel_ctx *channel_ctx = &fl->apps->channel[fl->cid];
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301731 int err = 0, len;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001732
c_mtharue1a5ce12017-10-13 20:47:09 +05301733 VERIFY(err, NULL != channel_ctx->chan);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001734 if (err)
1735 goto bail;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301736 msg->pid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001737 msg->tid = current->pid;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301738 if (fl->sessionid)
1739 msg->tid |= (1 << SESSION_ID_INDEX);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001740 if (kernel)
1741 msg->pid = 0;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301742 msg->invoke.header.ctx = ctx->ctxid | fl->pd;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001743 msg->invoke.header.handle = handle;
1744 msg->invoke.header.sc = ctx->sc;
1745 msg->invoke.page.addr = ctx->buf ? ctx->buf->phys : 0;
1746 msg->invoke.page.size = buf_page_size(ctx->used);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301747 if (fl->apps->glink) {
1748 if (fl->ssrcount != channel_ctx->ssrcount) {
1749 err = -ECONNRESET;
1750 goto bail;
1751 }
1752 VERIFY(err, channel_ctx->link.port_state ==
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001753 FASTRPC_LINK_CONNECTED);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301754 if (err)
1755 goto bail;
1756 err = glink_tx(channel_ctx->chan,
1757 (void *)&fl->apps->channel[fl->cid], msg, sizeof(*msg),
1758 GLINK_TX_REQ_INTENT);
1759 } else {
1760 spin_lock(&fl->apps->hlock);
1761 len = smd_write((smd_channel_t *)
1762 channel_ctx->chan,
1763 msg, sizeof(*msg));
1764 spin_unlock(&fl->apps->hlock);
1765 VERIFY(err, len == sizeof(*msg));
1766 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001767 bail:
1768 return err;
1769}
1770
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301771static void fastrpc_smd_read_handler(int cid)
1772{
1773 struct fastrpc_apps *me = &gfa;
1774 struct smq_invoke_rsp rsp = {0};
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301775 int ret = 0, err = 0;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301776 uint32_t index;
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301777
1778 do {
1779 ret = smd_read_from_cb(me->channel[cid].chan, &rsp,
1780 sizeof(rsp));
1781 if (ret != sizeof(rsp))
1782 break;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301783
1784 index = (uint32_t)((rsp.ctx & FASTRPC_CTXID_MASK) >> 4);
1785 VERIFY(err, index < FASTRPC_CTX_MAX);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301786 if (err)
1787 goto bail;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301788
1789 VERIFY(err, !IS_ERR_OR_NULL(me->ctxtable[index]));
1790 if (err)
1791 goto bail;
1792
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05301793 VERIFY(err, ((me->ctxtable[index]->ctxid == (rsp.ctx & ~3)) &&
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301794 me->ctxtable[index]->magic == FASTRPC_CTX_MAGIC));
1795 if (err)
1796 goto bail;
1797
1798 context_notify_user(me->ctxtable[index], rsp.retval);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05301799 } while (ret == sizeof(rsp));
1800bail:
1801 if (err)
1802 pr_err("adsprpc: invalid response or context\n");
1803
1804}
1805
1806static void smd_event_handler(void *priv, unsigned int event)
1807{
1808 struct fastrpc_apps *me = &gfa;
1809 int cid = (int)(uintptr_t)priv;
1810
1811 switch (event) {
1812 case SMD_EVENT_OPEN:
1813 complete(&me->channel[cid].workport);
1814 break;
1815 case SMD_EVENT_CLOSE:
1816 fastrpc_notify_drivers(me, cid);
1817 break;
1818 case SMD_EVENT_DATA:
1819 fastrpc_smd_read_handler(cid);
1820 break;
1821 }
1822}
1823
1824
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001825static void fastrpc_init(struct fastrpc_apps *me)
1826{
1827 int i;
1828
1829 INIT_HLIST_HEAD(&me->drivers);
Tharun Kumar Merugubcd6fbf2018-01-04 17:49:34 +05301830 INIT_HLIST_HEAD(&me->maps);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001831 spin_lock_init(&me->hlock);
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05301832 spin_lock_init(&me->ctxlock);
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05301833 mutex_init(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001834 me->channel = &gcinfo[0];
1835 for (i = 0; i < NUM_CHANNELS; i++) {
1836 init_completion(&me->channel[i].work);
Tharun Kumar Merugu53a8ec92017-07-14 15:52:49 +05301837 init_completion(&me->channel[i].workport);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001838 me->channel[i].sesscount = 0;
1839 }
1840}
1841
1842static int fastrpc_release_current_dsp_process(struct fastrpc_file *fl);
1843
1844static int fastrpc_internal_invoke(struct fastrpc_file *fl, uint32_t mode,
1845 uint32_t kernel,
Sathish Ambleybae51902017-07-03 15:00:49 -07001846 struct fastrpc_ioctl_invoke_crc *inv)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001847{
c_mtharue1a5ce12017-10-13 20:47:09 +05301848 struct smq_invoke_ctx *ctx = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001849 struct fastrpc_ioctl_invoke *invoke = &inv->inv;
1850 int cid = fl->cid;
1851 int interrupted = 0;
1852 int err = 0;
Maria Yu757199c2017-09-22 16:05:49 +08001853 struct timespec invoket = {0};
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301854 int64_t *perf_counter = getperfcounter(fl, PERF_COUNT);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001855
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001856 if (fl->profile)
1857 getnstimeofday(&invoket);
Tharun Kumar Merugue3edf3e2017-07-27 12:34:07 +05301858
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301859
Tharun Kumar Merugue3edf3e2017-07-27 12:34:07 +05301860 VERIFY(err, fl->sctx != NULL);
1861 if (err)
1862 goto bail;
1863 VERIFY(err, fl->cid >= 0 && fl->cid < NUM_CHANNELS);
1864 if (err)
1865 goto bail;
1866
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001867 if (!kernel) {
1868 VERIFY(err, 0 == context_restore_interrupted(fl, inv,
1869 &ctx));
1870 if (err)
1871 goto bail;
1872 if (fl->sctx->smmu.faults)
1873 err = FASTRPC_ENOSUCH;
1874 if (err)
1875 goto bail;
1876 if (ctx)
1877 goto wait;
1878 }
1879
1880 VERIFY(err, 0 == context_alloc(fl, kernel, inv, &ctx));
1881 if (err)
1882 goto bail;
1883
1884 if (REMOTE_SCALARS_LENGTH(ctx->sc)) {
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301885 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_GETARGS),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001886 VERIFY(err, 0 == get_args(kernel, ctx));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001887 PERF_END);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001888 if (err)
1889 goto bail;
1890 }
1891
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301892 if (!fl->sctx->smmu.coherent) {
1893 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001894 inv_args_pre(ctx);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301895 PERF_END);
1896 }
1897
1898 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_LINK),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001899 VERIFY(err, 0 == fastrpc_invoke_send(ctx, kernel, invoke->handle));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001900 PERF_END);
1901
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001902 if (err)
1903 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001904 wait:
1905 if (kernel)
1906 wait_for_completion(&ctx->work);
1907 else {
1908 interrupted = wait_for_completion_interruptible(&ctx->work);
1909 VERIFY(err, 0 == (err = interrupted));
1910 if (err)
1911 goto bail;
1912 }
Sathish Ambleyc432b502017-06-05 12:03:42 -07001913
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301914 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS),
Sathish Ambleyc432b502017-06-05 12:03:42 -07001915 if (!fl->sctx->smmu.coherent)
1916 inv_args(ctx);
1917 PERF_END);
1918
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001919 VERIFY(err, 0 == (err = ctx->retval));
1920 if (err)
1921 goto bail;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001922
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301923 PERF(fl->profile, GET_COUNTER(perf_counter, PERF_PUTARGS),
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001924 VERIFY(err, 0 == put_args(kernel, ctx, invoke->pra));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001925 PERF_END);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001926 if (err)
1927 goto bail;
1928 bail:
1929 if (ctx && interrupted == -ERESTARTSYS)
1930 context_save_interrupted(ctx);
1931 else if (ctx)
1932 context_free(ctx);
1933 if (fl->ssrcount != fl->apps->channel[cid].ssrcount)
1934 err = ECONNRESET;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001935
1936 if (fl->profile && !interrupted) {
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05301937 if (invoke->handle != FASTRPC_STATIC_HANDLE_LISTENER) {
1938 int64_t *count = GET_COUNTER(perf_counter, PERF_INVOKE);
1939
1940 if (count)
1941 *count += getnstimediff(&invoket);
1942 }
1943 if (invoke->handle > FASTRPC_STATIC_HANDLE_MAX) {
1944 int64_t *count = GET_COUNTER(perf_counter, PERF_COUNT);
1945
1946 if (count)
1947 *count = *count+1;
1948 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08001949 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001950 return err;
1951}
1952
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05301953static int fastrpc_get_adsp_session(char *name, int *session)
1954{
1955 struct fastrpc_apps *me = &gfa;
1956 int err = 0, i;
1957
1958 for (i = 0; i < NUM_SESSIONS; i++) {
1959 if (!me->channel[0].spd[i].spdname)
1960 continue;
1961 if (!strcmp(name, me->channel[0].spd[i].spdname))
1962 break;
1963 }
1964 VERIFY(err, i < NUM_SESSIONS);
1965 if (err)
1966 goto bail;
1967 *session = i;
1968bail:
1969 return err;
1970}
1971
1972static int fastrpc_mmap_remove_pdr(struct fastrpc_file *fl);
Sathish Ambley36849af2017-02-02 09:35:55 -08001973static int fastrpc_channel_open(struct fastrpc_file *fl);
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05301974static int fastrpc_mmap_remove_ssr(struct fastrpc_file *fl);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001975static int fastrpc_init_process(struct fastrpc_file *fl,
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001976 struct fastrpc_ioctl_init_attrs *uproc)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001977{
1978 int err = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05301979 struct fastrpc_apps *me = &gfa;
Sathish Ambleybae51902017-07-03 15:00:49 -07001980 struct fastrpc_ioctl_invoke_crc ioctl;
Sathish Ambleyd6300c32017-01-18 09:50:43 -08001981 struct fastrpc_ioctl_init *init = &uproc->init;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001982 struct smq_phy_page pages[1];
c_mtharue1a5ce12017-10-13 20:47:09 +05301983 struct fastrpc_mmap *file = NULL, *mem = NULL;
1984 char *proc_name = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001985
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05301986 VERIFY(err, 0 == (err = fastrpc_channel_open(fl)));
Sathish Ambley36849af2017-02-02 09:35:55 -08001987 if (err)
1988 goto bail;
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05301989 if (init->flags == FASTRPC_INIT_ATTACH ||
1990 init->flags == FASTRPC_INIT_ATTACH_SENSORS) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001991 remote_arg_t ra[1];
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05301992 int tgid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07001993
1994 ra[0].buf.pv = (void *)&tgid;
1995 ra[0].buf.len = sizeof(tgid);
1996 ioctl.inv.handle = 1;
1997 ioctl.inv.sc = REMOTE_SCALARS_MAKE(0, 1, 0);
1998 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05301999 ioctl.fds = NULL;
2000 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07002001 ioctl.crc = NULL;
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302002 if (init->flags == FASTRPC_INIT_ATTACH)
2003 fl->pd = 0;
2004 else if (init->flags == FASTRPC_INIT_ATTACH_SENSORS) {
2005 fl->spdname = SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME;
2006 fl->pd = 2;
2007 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002008 VERIFY(err, !(err = fastrpc_internal_invoke(fl,
2009 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
2010 if (err)
2011 goto bail;
2012 } else if (init->flags == FASTRPC_INIT_CREATE) {
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002013 remote_arg_t ra[6];
2014 int fds[6];
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002015 int mflags = 0;
2016 struct {
2017 int pgid;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302018 unsigned int namelen;
2019 unsigned int filelen;
2020 unsigned int pageslen;
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002021 int attrs;
2022 int siglen;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002023 } inbuf;
2024
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05302025 inbuf.pgid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002026 inbuf.namelen = strlen(current->comm) + 1;
2027 inbuf.filelen = init->filelen;
2028 fl->pd = 1;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302029
Tharun Kumar Merugudf852892017-12-07 16:27:37 +05302030 VERIFY(err, access_ok(0, (void __user *)init->file,
2031 init->filelen));
2032 if (err)
2033 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002034 if (init->filelen) {
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302035 mutex_lock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002036 VERIFY(err, !fastrpc_mmap_create(fl, init->filefd, 0,
2037 init->file, init->filelen, mflags, &file));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302038 mutex_unlock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002039 if (err)
2040 goto bail;
2041 }
2042 inbuf.pageslen = 1;
Tharun Kumar Merugudf852892017-12-07 16:27:37 +05302043 VERIFY(err, access_ok(1, (void __user *)init->mem,
2044 init->memlen));
2045 if (err)
2046 goto bail;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302047 mutex_lock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002048 VERIFY(err, !fastrpc_mmap_create(fl, init->memfd, 0,
2049 init->mem, init->memlen, mflags, &mem));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302050 mutex_unlock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002051 if (err)
2052 goto bail;
2053 inbuf.pageslen = 1;
2054 ra[0].buf.pv = (void *)&inbuf;
2055 ra[0].buf.len = sizeof(inbuf);
2056 fds[0] = 0;
2057
2058 ra[1].buf.pv = (void *)current->comm;
2059 ra[1].buf.len = inbuf.namelen;
2060 fds[1] = 0;
2061
2062 ra[2].buf.pv = (void *)init->file;
2063 ra[2].buf.len = inbuf.filelen;
2064 fds[2] = init->filefd;
2065
2066 pages[0].addr = mem->phys;
2067 pages[0].size = mem->size;
2068 ra[3].buf.pv = (void *)pages;
2069 ra[3].buf.len = 1 * sizeof(*pages);
2070 fds[3] = 0;
2071
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002072 inbuf.attrs = uproc->attrs;
2073 ra[4].buf.pv = (void *)&(inbuf.attrs);
2074 ra[4].buf.len = sizeof(inbuf.attrs);
2075 fds[4] = 0;
2076
2077 inbuf.siglen = uproc->siglen;
2078 ra[5].buf.pv = (void *)&(inbuf.siglen);
2079 ra[5].buf.len = sizeof(inbuf.siglen);
2080 fds[5] = 0;
2081
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002082 ioctl.inv.handle = 1;
2083 ioctl.inv.sc = REMOTE_SCALARS_MAKE(6, 4, 0);
Sathish Ambleyd6300c32017-01-18 09:50:43 -08002084 if (uproc->attrs)
2085 ioctl.inv.sc = REMOTE_SCALARS_MAKE(7, 6, 0);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002086 ioctl.inv.pra = ra;
2087 ioctl.fds = fds;
c_mtharue1a5ce12017-10-13 20:47:09 +05302088 ioctl.attrs = NULL;
2089 ioctl.crc = NULL;
2090 VERIFY(err, !(err = fastrpc_internal_invoke(fl,
2091 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
2092 if (err)
2093 goto bail;
2094 } else if (init->flags == FASTRPC_INIT_CREATE_STATIC) {
2095 remote_arg_t ra[3];
2096 uint64_t phys = 0;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302097 size_t size = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05302098 int fds[3];
2099 struct {
2100 int pgid;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302101 unsigned int namelen;
2102 unsigned int pageslen;
c_mtharue1a5ce12017-10-13 20:47:09 +05302103 } inbuf;
2104
2105 if (!init->filelen)
2106 goto bail;
2107
2108 proc_name = kzalloc(init->filelen, GFP_KERNEL);
2109 VERIFY(err, !IS_ERR_OR_NULL(proc_name));
2110 if (err)
2111 goto bail;
2112 VERIFY(err, 0 == copy_from_user((void *)proc_name,
2113 (void __user *)init->file, init->filelen));
2114 if (err)
2115 goto bail;
2116
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302117 fl->pd = 1;
c_mtharue1a5ce12017-10-13 20:47:09 +05302118 inbuf.pgid = current->tgid;
c_mtharu81a0aa72017-11-07 16:13:21 +05302119 inbuf.namelen = init->filelen;
c_mtharue1a5ce12017-10-13 20:47:09 +05302120 inbuf.pageslen = 0;
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05302121
2122 if (!strcmp(proc_name, "audiopd")) {
2123 fl->spdname = AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME;
2124 VERIFY(err, !fastrpc_mmap_remove_pdr(fl));
Tharun Kumar Merugu35173342018-02-08 16:13:17 +05302125 if (err)
2126 goto bail;
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05302127 }
2128
c_mtharue1a5ce12017-10-13 20:47:09 +05302129 if (!me->staticpd_flags) {
2130 inbuf.pageslen = 1;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302131 mutex_lock(&fl->fl_map_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05302132 VERIFY(err, !fastrpc_mmap_create(fl, -1, 0, init->mem,
2133 init->memlen, ADSP_MMAP_REMOTE_HEAP_ADDR,
2134 &mem));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302135 mutex_unlock(&fl->fl_map_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05302136 if (err)
2137 goto bail;
2138 phys = mem->phys;
2139 size = mem->size;
2140 VERIFY(err, !hyp_assign_phys(phys, (uint64_t)size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05302141 hlosvm, 1, me->channel[fl->cid].rhvm.vmid,
2142 me->channel[fl->cid].rhvm.vmperm,
2143 me->channel[fl->cid].rhvm.vmcount));
c_mtharue1a5ce12017-10-13 20:47:09 +05302144 if (err) {
2145 pr_err("ADSPRPC: hyp_assign_phys fail err %d",
2146 err);
2147 pr_err("map->phys %llx, map->size %d\n",
2148 phys, (int)size);
2149 goto bail;
2150 }
2151 me->staticpd_flags = 1;
2152 }
2153
2154 ra[0].buf.pv = (void *)&inbuf;
2155 ra[0].buf.len = sizeof(inbuf);
2156 fds[0] = 0;
2157
2158 ra[1].buf.pv = (void *)proc_name;
2159 ra[1].buf.len = inbuf.namelen;
2160 fds[1] = 0;
2161
2162 pages[0].addr = phys;
2163 pages[0].size = size;
2164
2165 ra[2].buf.pv = (void *)pages;
2166 ra[2].buf.len = sizeof(*pages);
2167 fds[2] = 0;
2168 ioctl.inv.handle = 1;
2169
2170 ioctl.inv.sc = REMOTE_SCALARS_MAKE(8, 3, 0);
2171 ioctl.inv.pra = ra;
2172 ioctl.fds = NULL;
2173 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07002174 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002175 VERIFY(err, !(err = fastrpc_internal_invoke(fl,
2176 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
2177 if (err)
2178 goto bail;
2179 } else {
2180 err = -ENOTTY;
2181 }
2182bail:
c_mtharud91205a2017-11-07 16:01:06 +05302183 kfree(proc_name);
c_mtharue1a5ce12017-10-13 20:47:09 +05302184 if (err && (init->flags == FASTRPC_INIT_CREATE_STATIC))
2185 me->staticpd_flags = 0;
2186 if (mem && err) {
2187 if (mem->flags == ADSP_MMAP_REMOTE_HEAP_ADDR)
2188 hyp_assign_phys(mem->phys, (uint64_t)mem->size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05302189 me->channel[fl->cid].rhvm.vmid,
2190 me->channel[fl->cid].rhvm.vmcount,
2191 hlosvm, hlosvmperm, 1);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302192 mutex_lock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302193 fastrpc_mmap_free(mem, 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302194 mutex_unlock(&fl->fl_map_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05302195 }
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302196 if (file) {
2197 mutex_lock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302198 fastrpc_mmap_free(file, 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302199 mutex_unlock(&fl->fl_map_mutex);
2200 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002201 return err;
2202}
2203
2204static int fastrpc_release_current_dsp_process(struct fastrpc_file *fl)
2205{
2206 int err = 0;
Sathish Ambleybae51902017-07-03 15:00:49 -07002207 struct fastrpc_ioctl_invoke_crc ioctl;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002208 remote_arg_t ra[1];
2209 int tgid = 0;
2210
Sathish Ambley36849af2017-02-02 09:35:55 -08002211 VERIFY(err, fl->cid >= 0 && fl->cid < NUM_CHANNELS);
2212 if (err)
2213 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05302214 VERIFY(err, fl->apps->channel[fl->cid].chan != NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002215 if (err)
2216 goto bail;
2217 tgid = fl->tgid;
2218 ra[0].buf.pv = (void *)&tgid;
2219 ra[0].buf.len = sizeof(tgid);
2220 ioctl.inv.handle = 1;
2221 ioctl.inv.sc = REMOTE_SCALARS_MAKE(1, 1, 0);
2222 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05302223 ioctl.fds = NULL;
2224 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07002225 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002226 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
2227 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
2228bail:
2229 return err;
2230}
2231
2232static int fastrpc_mmap_on_dsp(struct fastrpc_file *fl, uint32_t flags,
2233 struct fastrpc_mmap *map)
2234{
Sathish Ambleybae51902017-07-03 15:00:49 -07002235 struct fastrpc_ioctl_invoke_crc ioctl;
c_mtharu63ffc012017-11-16 15:26:56 +05302236 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002237 struct smq_phy_page page;
2238 int num = 1;
2239 remote_arg_t ra[3];
2240 int err = 0;
2241 struct {
2242 int pid;
2243 uint32_t flags;
2244 uintptr_t vaddrin;
2245 int num;
2246 } inargs;
2247 struct {
2248 uintptr_t vaddrout;
2249 } routargs;
2250
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05302251 inargs.pid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002252 inargs.vaddrin = (uintptr_t)map->va;
2253 inargs.flags = flags;
2254 inargs.num = fl->apps->compat ? num * sizeof(page) : num;
2255 ra[0].buf.pv = (void *)&inargs;
2256 ra[0].buf.len = sizeof(inargs);
2257 page.addr = map->phys;
2258 page.size = map->size;
2259 ra[1].buf.pv = (void *)&page;
2260 ra[1].buf.len = num * sizeof(page);
2261
2262 ra[2].buf.pv = (void *)&routargs;
2263 ra[2].buf.len = sizeof(routargs);
2264
2265 ioctl.inv.handle = 1;
2266 if (fl->apps->compat)
2267 ioctl.inv.sc = REMOTE_SCALARS_MAKE(4, 2, 1);
2268 else
2269 ioctl.inv.sc = REMOTE_SCALARS_MAKE(2, 2, 1);
2270 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05302271 ioctl.fds = NULL;
2272 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07002273 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002274 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
2275 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
2276 map->raddr = (uintptr_t)routargs.vaddrout;
c_mtharue1a5ce12017-10-13 20:47:09 +05302277 if (err)
2278 goto bail;
2279 if (flags == ADSP_MMAP_HEAP_ADDR) {
2280 struct scm_desc desc = {0};
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002281
c_mtharue1a5ce12017-10-13 20:47:09 +05302282 desc.args[0] = TZ_PIL_AUTH_QDSP6_PROC;
2283 desc.args[1] = map->phys;
2284 desc.args[2] = map->size;
2285 desc.arginfo = SCM_ARGS(3);
2286 err = scm_call2(SCM_SIP_FNID(SCM_SVC_PIL,
2287 TZ_PIL_PROTECT_MEM_SUBSYS_ID), &desc);
2288 } else if (flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
c_mtharue1a5ce12017-10-13 20:47:09 +05302289 VERIFY(err, !hyp_assign_phys(map->phys, (uint64_t)map->size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05302290 hlosvm, 1, me->channel[fl->cid].rhvm.vmid,
2291 me->channel[fl->cid].rhvm.vmperm,
2292 me->channel[fl->cid].rhvm.vmcount));
c_mtharue1a5ce12017-10-13 20:47:09 +05302293 if (err)
2294 goto bail;
2295 }
2296bail:
2297 return err;
2298}
2299
2300static int fastrpc_munmap_on_dsp_rh(struct fastrpc_file *fl,
2301 struct fastrpc_mmap *map)
2302{
2303 int err = 0;
c_mtharu63ffc012017-11-16 15:26:56 +05302304 struct fastrpc_apps *me = &gfa;
c_mtharue1a5ce12017-10-13 20:47:09 +05302305 int destVM[1] = {VMID_HLOS};
2306 int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
2307
2308 if (map->flags == ADSP_MMAP_HEAP_ADDR) {
2309 struct fastrpc_ioctl_invoke_crc ioctl;
2310 struct scm_desc desc = {0};
2311 remote_arg_t ra[1];
2312 int err = 0;
2313 struct {
2314 uint8_t skey;
2315 } routargs;
2316
2317 ra[0].buf.pv = (void *)&routargs;
2318 ra[0].buf.len = sizeof(routargs);
2319
2320 ioctl.inv.handle = 1;
2321 ioctl.inv.sc = REMOTE_SCALARS_MAKE(7, 0, 1);
2322 ioctl.inv.pra = ra;
2323 ioctl.fds = NULL;
2324 ioctl.attrs = NULL;
2325 ioctl.crc = NULL;
2326 if (fl == NULL)
2327 goto bail;
2328
2329 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
2330 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
2331 if (err)
2332 goto bail;
2333 desc.args[0] = TZ_PIL_AUTH_QDSP6_PROC;
2334 desc.args[1] = map->phys;
2335 desc.args[2] = map->size;
2336 desc.args[3] = routargs.skey;
2337 desc.arginfo = SCM_ARGS(4);
2338 err = scm_call2(SCM_SIP_FNID(SCM_SVC_PIL,
2339 TZ_PIL_CLEAR_PROTECT_MEM_SUBSYS_ID), &desc);
2340 } else if (map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
2341 VERIFY(err, !hyp_assign_phys(map->phys, (uint64_t)map->size,
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05302342 me->channel[fl->cid].rhvm.vmid,
2343 me->channel[fl->cid].rhvm.vmcount,
2344 destVM, destVMperm, 1));
c_mtharue1a5ce12017-10-13 20:47:09 +05302345 if (err)
2346 goto bail;
2347 }
2348
2349bail:
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002350 return err;
2351}
2352
2353static int fastrpc_munmap_on_dsp(struct fastrpc_file *fl,
2354 struct fastrpc_mmap *map)
2355{
Sathish Ambleybae51902017-07-03 15:00:49 -07002356 struct fastrpc_ioctl_invoke_crc ioctl;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002357 remote_arg_t ra[1];
2358 int err = 0;
2359 struct {
2360 int pid;
2361 uintptr_t vaddrout;
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302362 size_t size;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002363 } inargs;
2364
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05302365 inargs.pid = fl->tgid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002366 inargs.size = map->size;
2367 inargs.vaddrout = map->raddr;
2368 ra[0].buf.pv = (void *)&inargs;
2369 ra[0].buf.len = sizeof(inargs);
2370
2371 ioctl.inv.handle = 1;
2372 if (fl->apps->compat)
2373 ioctl.inv.sc = REMOTE_SCALARS_MAKE(5, 1, 0);
2374 else
2375 ioctl.inv.sc = REMOTE_SCALARS_MAKE(3, 1, 0);
2376 ioctl.inv.pra = ra;
c_mtharue1a5ce12017-10-13 20:47:09 +05302377 ioctl.fds = NULL;
2378 ioctl.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07002379 ioctl.crc = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002380 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
2381 FASTRPC_MODE_PARALLEL, 1, &ioctl)));
c_mtharue1a5ce12017-10-13 20:47:09 +05302382 if (err)
2383 goto bail;
2384 if (map->flags == ADSP_MMAP_HEAP_ADDR ||
2385 map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
2386 VERIFY(err, !fastrpc_munmap_on_dsp_rh(fl, map));
2387 if (err)
2388 goto bail;
2389 }
2390bail:
2391 return err;
2392}
2393
2394static int fastrpc_mmap_remove_ssr(struct fastrpc_file *fl)
2395{
2396 struct fastrpc_mmap *match = NULL, *map = NULL;
2397 struct hlist_node *n = NULL;
2398 int err = 0, ret = 0;
2399 struct fastrpc_apps *me = &gfa;
2400 struct ramdump_segment *ramdump_segments_rh = NULL;
2401
2402 do {
2403 match = NULL;
2404 spin_lock(&me->hlock);
2405 hlist_for_each_entry_safe(map, n, &me->maps, hn) {
2406 match = map;
2407 hlist_del_init(&map->hn);
2408 break;
2409 }
2410 spin_unlock(&me->hlock);
2411
2412 if (match) {
2413 VERIFY(err, !fastrpc_munmap_on_dsp_rh(fl, match));
2414 if (err)
2415 goto bail;
2416 if (me->channel[0].ramdumpenabled) {
2417 ramdump_segments_rh = kcalloc(1,
2418 sizeof(struct ramdump_segment), GFP_KERNEL);
2419 if (ramdump_segments_rh) {
2420 ramdump_segments_rh->address =
2421 match->phys;
2422 ramdump_segments_rh->size = match->size;
2423 ret = do_elf_ramdump(
2424 me->channel[0].remoteheap_ramdump_dev,
2425 ramdump_segments_rh, 1);
2426 if (ret < 0)
2427 pr_err("ADSPRPC: unable to dump heap");
2428 kfree(ramdump_segments_rh);
2429 }
2430 }
c_mtharu7bd6a422017-10-17 18:15:37 +05302431 fastrpc_mmap_free(match, 0);
c_mtharue1a5ce12017-10-13 20:47:09 +05302432 }
2433 } while (match);
2434bail:
2435 if (err && match)
2436 fastrpc_mmap_add(match);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002437 return err;
2438}
2439
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05302440static int fastrpc_mmap_remove_pdr(struct fastrpc_file *fl)
2441{
2442 struct fastrpc_apps *me = &gfa;
2443 int session = 0, err = 0;
2444
2445 VERIFY(err, !fastrpc_get_adsp_session(
2446 AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME, &session));
2447 if (err)
2448 goto bail;
2449 if (me->channel[fl->cid].spd[session].pdrcount !=
2450 me->channel[fl->cid].spd[session].prevpdrcount) {
2451 if (fastrpc_mmap_remove_ssr(fl))
2452 pr_err("ADSPRPC: SSR: Failed to unmap remote heap\n");
2453 me->channel[fl->cid].spd[session].prevpdrcount =
2454 me->channel[fl->cid].spd[session].pdrcount;
2455 }
2456 if (!me->channel[fl->cid].spd[session].ispdup) {
2457 VERIFY(err, 0);
2458 if (err) {
2459 err = -ENOTCONN;
2460 goto bail;
2461 }
2462 }
2463bail:
2464 return err;
2465}
2466
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002467static int fastrpc_mmap_remove(struct fastrpc_file *fl, uintptr_t va,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302468 size_t len, struct fastrpc_mmap **ppmap);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002469
2470static void fastrpc_mmap_add(struct fastrpc_mmap *map);
2471
2472static int fastrpc_internal_munmap(struct fastrpc_file *fl,
2473 struct fastrpc_ioctl_munmap *ud)
2474{
2475 int err = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05302476 struct fastrpc_mmap *map = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002477
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302478 mutex_lock(&fl->map_mutex);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302479 mutex_lock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002480 VERIFY(err, !fastrpc_mmap_remove(fl, ud->vaddrout, ud->size, &map));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302481 mutex_unlock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002482 if (err)
2483 goto bail;
2484 VERIFY(err, !fastrpc_munmap_on_dsp(fl, map));
2485 if (err)
2486 goto bail;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302487 mutex_lock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302488 fastrpc_mmap_free(map, 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302489 mutex_unlock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002490bail:
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302491 if (err && map) {
2492 mutex_lock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002493 fastrpc_mmap_add(map);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302494 mutex_unlock(&fl->fl_map_mutex);
2495 }
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302496 mutex_unlock(&fl->map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002497 return err;
2498}
2499
c_mtharu7bd6a422017-10-17 18:15:37 +05302500static int fastrpc_internal_munmap_fd(struct fastrpc_file *fl,
2501 struct fastrpc_ioctl_munmap_fd *ud) {
2502 int err = 0;
2503 struct fastrpc_mmap *map = NULL;
2504
2505 VERIFY(err, (fl && ud));
2506 if (err)
2507 goto bail;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302508 mutex_lock(&fl->fl_map_mutex);
Tharun Kumar Merugu09fc6152018-02-16 13:13:12 +05302509 if (fastrpc_mmap_find(fl, ud->fd, ud->va, ud->len, 0, 0, &map)) {
2510 pr_err("adsprpc: mapping not found to unmap %d va %llx %x\n",
c_mtharu7bd6a422017-10-17 18:15:37 +05302511 ud->fd, (unsigned long long)ud->va,
2512 (unsigned int)ud->len);
2513 err = -1;
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302514 mutex_unlock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302515 goto bail;
2516 }
2517 if (map)
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302518 fastrpc_mmap_free(map, 0);
2519 mutex_unlock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302520bail:
2521 return err;
2522}
2523
2524
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002525static int fastrpc_internal_mmap(struct fastrpc_file *fl,
2526 struct fastrpc_ioctl_mmap *ud)
2527{
2528
c_mtharue1a5ce12017-10-13 20:47:09 +05302529 struct fastrpc_mmap *map = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002530 int err = 0;
2531
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302532 mutex_lock(&fl->map_mutex);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302533 mutex_lock(&fl->fl_map_mutex);
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302534 if (!fastrpc_mmap_find(fl, ud->fd, (uintptr_t)ud->vaddrin,
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302535 ud->size, ud->flags, 1, &map)) {
2536 mutex_unlock(&fl->fl_map_mutex);
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302537 mutex_unlock(&fl->map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002538 return 0;
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302539 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002540 VERIFY(err, !fastrpc_mmap_create(fl, ud->fd, 0,
Tharun Kumar Merugufb51f942017-12-07 10:32:15 +05302541 (uintptr_t)ud->vaddrin, ud->size,
c_mtharue1a5ce12017-10-13 20:47:09 +05302542 ud->flags, &map));
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302543 mutex_unlock(&fl->fl_map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002544 if (err)
2545 goto bail;
2546 VERIFY(err, 0 == fastrpc_mmap_on_dsp(fl, ud->flags, map));
2547 if (err)
2548 goto bail;
2549 ud->vaddrout = map->raddr;
2550 bail:
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302551 if (err && map) {
2552 mutex_lock(&fl->fl_map_mutex);
c_mtharu7bd6a422017-10-17 18:15:37 +05302553 fastrpc_mmap_free(map, 0);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302554 mutex_unlock(&fl->fl_map_mutex);
2555 }
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05302556 mutex_unlock(&fl->map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002557 return err;
2558}
2559
2560static void fastrpc_channel_close(struct kref *kref)
2561{
2562 struct fastrpc_apps *me = &gfa;
2563 struct fastrpc_channel_ctx *ctx;
2564 int cid;
2565
2566 ctx = container_of(kref, struct fastrpc_channel_ctx, kref);
2567 cid = ctx - &gcinfo[0];
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05302568 if (!me->glink)
2569 smd_close(ctx->chan);
2570 else
2571 fastrpc_glink_close(ctx->chan, cid);
c_mtharue1a5ce12017-10-13 20:47:09 +05302572 ctx->chan = NULL;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302573 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002574 pr_info("'closed /dev/%s c %d %d'\n", gcinfo[cid].name,
2575 MAJOR(me->dev_no), cid);
2576}
2577
2578static void fastrpc_context_list_dtor(struct fastrpc_file *fl);
2579
2580static int fastrpc_session_alloc_locked(struct fastrpc_channel_ctx *chan,
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302581 int secure, int sharedcb, struct fastrpc_session_ctx **session)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002582{
2583 struct fastrpc_apps *me = &gfa;
2584 int idx = 0, err = 0;
2585
2586 if (chan->sesscount) {
2587 for (idx = 0; idx < chan->sesscount; ++idx) {
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302588 if ((sharedcb && chan->session[idx].smmu.sharedcb) ||
2589 (!chan->session[idx].used &&
2590 chan->session[idx].smmu.secure
2591 == secure && !sharedcb)) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002592 chan->session[idx].used = 1;
2593 break;
2594 }
2595 }
2596 VERIFY(err, idx < chan->sesscount);
2597 if (err)
2598 goto bail;
2599 chan->session[idx].smmu.faults = 0;
2600 } else {
2601 VERIFY(err, me->dev != NULL);
2602 if (err)
2603 goto bail;
2604 chan->session[0].dev = me->dev;
c_mtharue1a5ce12017-10-13 20:47:09 +05302605 chan->session[0].smmu.dev = me->dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002606 }
2607
2608 *session = &chan->session[idx];
2609 bail:
2610 return err;
2611}
2612
c_mtharue1a5ce12017-10-13 20:47:09 +05302613static bool fastrpc_glink_notify_rx_intent_req(void *h, const void *priv,
2614 size_t size)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002615{
2616 if (glink_queue_rx_intent(h, NULL, size))
2617 return false;
2618 return true;
2619}
2620
c_mtharue1a5ce12017-10-13 20:47:09 +05302621static void fastrpc_glink_notify_tx_done(void *handle, const void *priv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002622 const void *pkt_priv, const void *ptr)
2623{
2624}
2625
c_mtharue1a5ce12017-10-13 20:47:09 +05302626static void fastrpc_glink_notify_rx(void *handle, const void *priv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002627 const void *pkt_priv, const void *ptr, size_t size)
2628{
2629 struct smq_invoke_rsp *rsp = (struct smq_invoke_rsp *)ptr;
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05302630 struct fastrpc_apps *me = &gfa;
2631 uint32_t index;
c_mtharufdac6892017-10-12 13:09:01 +05302632 int err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002633
c_mtharufdac6892017-10-12 13:09:01 +05302634 VERIFY(err, (rsp && size >= sizeof(*rsp)));
2635 if (err)
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302636 goto bail;
2637
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05302638 index = (uint32_t)((rsp->ctx & FASTRPC_CTXID_MASK) >> 4);
2639 VERIFY(err, index < FASTRPC_CTX_MAX);
c_mtharufdac6892017-10-12 13:09:01 +05302640 if (err)
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302641 goto bail;
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302642
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05302643 VERIFY(err, !IS_ERR_OR_NULL(me->ctxtable[index]));
2644 if (err)
2645 goto bail;
2646
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302647 VERIFY(err, ((me->ctxtable[index]->ctxid == (rsp->ctx & ~3)) &&
Tharun Kumar Merugu9c908aa2018-02-06 12:03:48 +05302648 me->ctxtable[index]->magic == FASTRPC_CTX_MAGIC));
2649 if (err)
2650 goto bail;
2651
2652 context_notify_user(me->ctxtable[index], rsp->retval);
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302653bail:
c_mtharufdac6892017-10-12 13:09:01 +05302654 if (err)
2655 pr_err("adsprpc: invalid response or context\n");
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002656 glink_rx_done(handle, ptr, true);
2657}
2658
c_mtharue1a5ce12017-10-13 20:47:09 +05302659static void fastrpc_glink_notify_state(void *handle, const void *priv,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002660 unsigned int event)
2661{
2662 struct fastrpc_apps *me = &gfa;
2663 int cid = (int)(uintptr_t)priv;
2664 struct fastrpc_glink_info *link;
2665
2666 if (cid < 0 || cid >= NUM_CHANNELS)
2667 return;
2668 link = &me->channel[cid].link;
2669 switch (event) {
2670 case GLINK_CONNECTED:
2671 link->port_state = FASTRPC_LINK_CONNECTED;
Tharun Kumar Merugu53a8ec92017-07-14 15:52:49 +05302672 complete(&me->channel[cid].workport);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002673 break;
2674 case GLINK_LOCAL_DISCONNECTED:
2675 link->port_state = FASTRPC_LINK_DISCONNECTED;
2676 break;
2677 case GLINK_REMOTE_DISCONNECTED:
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002678 break;
2679 default:
2680 break;
2681 }
2682}
2683
2684static int fastrpc_session_alloc(struct fastrpc_channel_ctx *chan, int secure,
2685 struct fastrpc_session_ctx **session)
2686{
2687 int err = 0;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302688 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002689
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302690 mutex_lock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002691 if (!*session)
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05302692 err = fastrpc_session_alloc_locked(chan, secure, 0, session);
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302693 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002694 return err;
2695}
2696
2697static void fastrpc_session_free(struct fastrpc_channel_ctx *chan,
2698 struct fastrpc_session_ctx *session)
2699{
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302700 struct fastrpc_apps *me = &gfa;
2701
2702 mutex_lock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002703 session->used = 0;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302704 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002705}
2706
2707static int fastrpc_file_free(struct fastrpc_file *fl)
2708{
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302709 struct hlist_node *n = NULL;
Tharun Kumar Merugu3e966762018-04-04 10:56:44 +05302710 struct fastrpc_mmap *map = NULL, *lmap = NULL;
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302711 struct fastrpc_perf *perf = NULL, *fperf = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002712 int cid;
2713
2714 if (!fl)
2715 return 0;
2716 cid = fl->cid;
2717
Tharun Kumar Merugu622d8712017-09-15 15:30:06 +05302718 (void)fastrpc_release_current_dsp_process(fl);
2719
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002720 spin_lock(&fl->apps->hlock);
2721 hlist_del_init(&fl->hn);
2722 spin_unlock(&fl->apps->hlock);
2723
Sathish Ambleyd7fbcbb2017-03-08 10:55:48 -08002724 if (!fl->sctx) {
2725 kfree(fl);
2726 return 0;
2727 }
tharun kumar9f899ea2017-07-03 17:07:03 +05302728 spin_lock(&fl->hlock);
2729 fl->file_close = 1;
2730 spin_unlock(&fl->hlock);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002731 fastrpc_context_list_dtor(fl);
2732 fastrpc_buf_list_free(fl);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302733 mutex_lock(&fl->fl_map_mutex);
Tharun Kumar Merugu3e966762018-04-04 10:56:44 +05302734 do {
2735 lmap = NULL;
2736 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
2737 hlist_del_init(&map->hn);
2738 lmap = map;
2739 break;
2740 }
2741 fastrpc_mmap_free(lmap, 1);
2742 } while (lmap);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302743 mutex_unlock(&fl->fl_map_mutex);
Tharun Kumar Merugu35173342018-02-08 16:13:17 +05302744 if (fl->refcount && (fl->ssrcount == fl->apps->channel[cid].ssrcount))
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002745 kref_put_mutex(&fl->apps->channel[cid].kref,
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05302746 fastrpc_channel_close, &fl->apps->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002747 if (fl->sctx)
2748 fastrpc_session_free(&fl->apps->channel[cid], fl->sctx);
2749 if (fl->secsctx)
2750 fastrpc_session_free(&fl->apps->channel[cid], fl->secsctx);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05302751
2752 mutex_lock(&fl->perf_mutex);
2753 do {
2754 struct hlist_node *pn = NULL;
2755
2756 fperf = NULL;
2757 hlist_for_each_entry_safe(perf, pn, &fl->perf, hn) {
2758 hlist_del_init(&perf->hn);
2759 fperf = perf;
2760 break;
2761 }
2762 kfree(fperf);
2763 } while (fperf);
2764 mutex_unlock(&fl->perf_mutex);
2765 mutex_destroy(&fl->perf_mutex);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05302766 mutex_destroy(&fl->fl_map_mutex);
Tharun Kumar Merugu8714e642018-05-17 15:21:08 +05302767 mutex_destroy(&fl->map_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002768 kfree(fl);
2769 return 0;
2770}
2771
2772static int fastrpc_device_release(struct inode *inode, struct file *file)
2773{
2774 struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data;
2775
2776 if (fl) {
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05302777 if (fl->qos_request && pm_qos_request_active(&fl->pm_qos_req))
2778 pm_qos_remove_request(&fl->pm_qos_req);
Sathish Ambley1ca68232017-01-19 10:32:55 -08002779 if (fl->debugfs_file != NULL)
2780 debugfs_remove(fl->debugfs_file);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002781 fastrpc_file_free(fl);
c_mtharue1a5ce12017-10-13 20:47:09 +05302782 file->private_data = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002783 }
2784 return 0;
2785}
2786
2787static void fastrpc_link_state_handler(struct glink_link_state_cb_info *cb_info,
2788 void *priv)
2789{
2790 struct fastrpc_apps *me = &gfa;
2791 int cid = (int)((uintptr_t)priv);
2792 struct fastrpc_glink_info *link;
2793
2794 if (cid < 0 || cid >= NUM_CHANNELS)
2795 return;
2796
2797 link = &me->channel[cid].link;
2798 switch (cb_info->link_state) {
2799 case GLINK_LINK_STATE_UP:
2800 link->link_state = FASTRPC_LINK_STATE_UP;
2801 complete(&me->channel[cid].work);
2802 break;
2803 case GLINK_LINK_STATE_DOWN:
2804 link->link_state = FASTRPC_LINK_STATE_DOWN;
2805 break;
2806 default:
2807 pr_err("adsprpc: unknown link state %d\n", cb_info->link_state);
2808 break;
2809 }
2810}
2811
2812static int fastrpc_glink_register(int cid, struct fastrpc_apps *me)
2813{
2814 int err = 0;
2815 struct fastrpc_glink_info *link;
2816
2817 VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS));
2818 if (err)
2819 goto bail;
2820
2821 link = &me->channel[cid].link;
2822 if (link->link_notify_handle != NULL)
2823 goto bail;
2824
2825 link->link_info.glink_link_state_notif_cb = fastrpc_link_state_handler;
2826 link->link_notify_handle = glink_register_link_state_cb(
2827 &link->link_info,
2828 (void *)((uintptr_t)cid));
2829 VERIFY(err, !IS_ERR_OR_NULL(me->channel[cid].link.link_notify_handle));
2830 if (err) {
2831 link->link_notify_handle = NULL;
2832 goto bail;
2833 }
2834 VERIFY(err, wait_for_completion_timeout(&me->channel[cid].work,
2835 RPC_TIMEOUT));
2836bail:
2837 return err;
2838}
2839
2840static void fastrpc_glink_close(void *chan, int cid)
2841{
2842 int err = 0;
2843 struct fastrpc_glink_info *link;
2844
2845 VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS));
2846 if (err)
2847 return;
2848 link = &gfa.channel[cid].link;
2849
c_mtharu314a4202017-11-15 22:09:17 +05302850 if (link->port_state == FASTRPC_LINK_CONNECTED ||
2851 link->port_state == FASTRPC_LINK_REMOTE_DISCONNECTING) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002852 link->port_state = FASTRPC_LINK_DISCONNECTING;
2853 glink_close(chan);
2854 }
2855}
2856
2857static int fastrpc_glink_open(int cid)
2858{
2859 int err = 0;
2860 void *handle = NULL;
2861 struct fastrpc_apps *me = &gfa;
2862 struct glink_open_config *cfg;
2863 struct fastrpc_glink_info *link;
2864
2865 VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS));
2866 if (err)
2867 goto bail;
2868 link = &me->channel[cid].link;
2869 cfg = &me->channel[cid].link.cfg;
2870 VERIFY(err, (link->link_state == FASTRPC_LINK_STATE_UP));
2871 if (err)
2872 goto bail;
2873
Tharun Kumar Meruguca0db5262017-05-10 12:53:12 +05302874 VERIFY(err, (link->port_state == FASTRPC_LINK_DISCONNECTED));
2875 if (err)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002876 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002877
2878 link->port_state = FASTRPC_LINK_CONNECTING;
2879 cfg->priv = (void *)(uintptr_t)cid;
2880 cfg->edge = gcinfo[cid].link.link_info.edge;
2881 cfg->transport = gcinfo[cid].link.link_info.transport;
2882 cfg->name = FASTRPC_GLINK_GUID;
2883 cfg->notify_rx = fastrpc_glink_notify_rx;
2884 cfg->notify_tx_done = fastrpc_glink_notify_tx_done;
2885 cfg->notify_state = fastrpc_glink_notify_state;
2886 cfg->notify_rx_intent_req = fastrpc_glink_notify_rx_intent_req;
2887 handle = glink_open(cfg);
2888 VERIFY(err, !IS_ERR_OR_NULL(handle));
c_mtharu6e1d26b2017-10-09 16:05:24 +05302889 if (err) {
2890 if (link->port_state == FASTRPC_LINK_CONNECTING)
2891 link->port_state = FASTRPC_LINK_DISCONNECTED;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002892 goto bail;
c_mtharu6e1d26b2017-10-09 16:05:24 +05302893 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07002894 me->channel[cid].chan = handle;
2895bail:
2896 return err;
2897}
2898
Sathish Ambley1ca68232017-01-19 10:32:55 -08002899static int fastrpc_debugfs_open(struct inode *inode, struct file *filp)
2900{
2901 filp->private_data = inode->i_private;
2902 return 0;
2903}
2904
2905static ssize_t fastrpc_debugfs_read(struct file *filp, char __user *buffer,
2906 size_t count, loff_t *position)
2907{
2908 struct fastrpc_file *fl = filp->private_data;
2909 struct hlist_node *n;
c_mtharue1a5ce12017-10-13 20:47:09 +05302910 struct fastrpc_buf *buf = NULL;
2911 struct fastrpc_mmap *map = NULL;
2912 struct smq_invoke_ctx *ictx = NULL;
Sathish Ambley1ca68232017-01-19 10:32:55 -08002913 struct fastrpc_channel_ctx *chan;
2914 struct fastrpc_session_ctx *sess;
2915 unsigned int len = 0;
2916 int i, j, ret = 0;
2917 char *fileinfo = NULL;
2918
2919 fileinfo = kzalloc(DEBUGFS_SIZE, GFP_KERNEL);
2920 if (!fileinfo)
2921 goto bail;
2922 if (fl == NULL) {
2923 for (i = 0; i < NUM_CHANNELS; i++) {
2924 chan = &gcinfo[i];
2925 len += scnprintf(fileinfo + len,
2926 DEBUGFS_SIZE - len, "%s\n\n",
2927 chan->name);
2928 len += scnprintf(fileinfo + len,
2929 DEBUGFS_SIZE - len, "%s %d\n",
2930 "sesscount:", chan->sesscount);
2931 for (j = 0; j < chan->sesscount; j++) {
2932 sess = &chan->session[j];
2933 len += scnprintf(fileinfo + len,
2934 DEBUGFS_SIZE - len,
2935 "%s%d\n\n", "SESSION", j);
2936 len += scnprintf(fileinfo + len,
2937 DEBUGFS_SIZE - len,
2938 "%s %d\n", "sid:",
2939 sess->smmu.cb);
2940 len += scnprintf(fileinfo + len,
2941 DEBUGFS_SIZE - len,
2942 "%s %d\n", "SECURE:",
2943 sess->smmu.secure);
2944 }
2945 }
2946 } else {
2947 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2948 "%s %d\n\n",
2949 "PROCESS_ID:", fl->tgid);
2950 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2951 "%s %d\n\n",
2952 "CHANNEL_ID:", fl->cid);
2953 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2954 "%s %d\n\n",
2955 "SSRCOUNT:", fl->ssrcount);
2956 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2957 "%s\n",
2958 "LIST OF BUFS:");
2959 spin_lock(&fl->hlock);
2960 hlist_for_each_entry_safe(buf, n, &fl->bufs, hn) {
2961 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Tharun Kumar Meruguce566452017-08-17 15:29:59 +05302962 "%s %pK %s %pK %s %llx\n", "buf:",
2963 buf, "buf->virt:", buf->virt,
2964 "buf->phys:", buf->phys);
Sathish Ambley1ca68232017-01-19 10:32:55 -08002965 }
2966 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2967 "\n%s\n",
2968 "LIST OF MAPS:");
2969 hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
2970 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Tharun Kumar Meruguce566452017-08-17 15:29:59 +05302971 "%s %pK %s %lx %s %llx\n",
Sathish Ambley1ca68232017-01-19 10:32:55 -08002972 "map:", map,
2973 "map->va:", map->va,
2974 "map->phys:", map->phys);
2975 }
2976 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2977 "\n%s\n",
2978 "LIST OF PENDING SMQCONTEXTS:");
2979 hlist_for_each_entry_safe(ictx, n, &fl->clst.pending, hn) {
2980 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Tharun Kumar Meruguce566452017-08-17 15:29:59 +05302981 "%s %pK %s %u %s %u %s %u\n",
Sathish Ambley1ca68232017-01-19 10:32:55 -08002982 "smqcontext:", ictx,
2983 "sc:", ictx->sc,
2984 "tid:", ictx->pid,
2985 "handle", ictx->rpra->h);
2986 }
2987 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
2988 "\n%s\n",
2989 "LIST OF INTERRUPTED SMQCONTEXTS:");
2990 hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) {
2991 len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
Tharun Kumar Meruguce566452017-08-17 15:29:59 +05302992 "%s %pK %s %u %s %u %s %u\n",
Sathish Ambley1ca68232017-01-19 10:32:55 -08002993 "smqcontext:", ictx,
2994 "sc:", ictx->sc,
2995 "tid:", ictx->pid,
2996 "handle", ictx->rpra->h);
2997 }
2998 spin_unlock(&fl->hlock);
2999 }
3000 if (len > DEBUGFS_SIZE)
3001 len = DEBUGFS_SIZE;
3002 ret = simple_read_from_buffer(buffer, count, position, fileinfo, len);
3003 kfree(fileinfo);
3004bail:
3005 return ret;
3006}
3007
3008static const struct file_operations debugfs_fops = {
3009 .open = fastrpc_debugfs_open,
3010 .read = fastrpc_debugfs_read,
3011};
Sathish Ambley36849af2017-02-02 09:35:55 -08003012static int fastrpc_channel_open(struct fastrpc_file *fl)
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003013{
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003014 struct fastrpc_apps *me = &gfa;
Sathish Ambley36849af2017-02-02 09:35:55 -08003015 int cid, err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003016
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303017 mutex_lock(&me->smd_mutex);
3018
Sathish Ambley36849af2017-02-02 09:35:55 -08003019 VERIFY(err, fl && fl->sctx);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003020 if (err)
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303021 goto bail;
Sathish Ambley36849af2017-02-02 09:35:55 -08003022 cid = fl->cid;
c_mtharu314a4202017-11-15 22:09:17 +05303023 VERIFY(err, cid >= 0 && cid < NUM_CHANNELS);
3024 if (err)
3025 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05303026 if (me->channel[cid].ssrcount !=
3027 me->channel[cid].prevssrcount) {
3028 if (!me->channel[cid].issubsystemup) {
3029 VERIFY(err, 0);
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303030 if (err) {
3031 err = -ENOTCONN;
c_mtharue1a5ce12017-10-13 20:47:09 +05303032 goto bail;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303033 }
c_mtharue1a5ce12017-10-13 20:47:09 +05303034 }
3035 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003036 fl->ssrcount = me->channel[cid].ssrcount;
Tharun Kumar Merugu35173342018-02-08 16:13:17 +05303037 fl->refcount = 1;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003038 if ((kref_get_unless_zero(&me->channel[cid].kref) == 0) ||
c_mtharue1a5ce12017-10-13 20:47:09 +05303039 (me->channel[cid].chan == NULL)) {
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05303040 if (me->glink) {
3041 VERIFY(err, 0 == fastrpc_glink_register(cid, me));
3042 if (err)
3043 goto bail;
3044 VERIFY(err, 0 == fastrpc_glink_open(cid));
3045 } else {
3046 VERIFY(err, !smd_named_open_on_edge(FASTRPC_SMD_GUID,
3047 gcinfo[cid].channel,
3048 (smd_channel_t **)&me->channel[cid].chan,
3049 (void *)(uintptr_t)cid,
3050 smd_event_handler));
3051 }
Tharun Kumar Merugu53a8ec92017-07-14 15:52:49 +05303052 VERIFY(err,
3053 wait_for_completion_timeout(&me->channel[cid].workport,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003054 RPC_TIMEOUT));
3055 if (err) {
c_mtharue1a5ce12017-10-13 20:47:09 +05303056 me->channel[cid].chan = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003057 goto bail;
3058 }
3059 kref_init(&me->channel[cid].kref);
3060 pr_info("'opened /dev/%s c %d %d'\n", gcinfo[cid].name,
3061 MAJOR(me->dev_no), cid);
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05303062 if (me->glink) {
3063 err = glink_queue_rx_intent(me->channel[cid].chan, NULL,
3064 FASTRPC_GLINK_INTENT_LEN);
3065 err |= glink_queue_rx_intent(me->channel[cid].chan,
3066 NULL, FASTRPC_GLINK_INTENT_LEN);
3067 if (err)
3068 pr_warn("adsprpc: initial intent fail for %d err %d\n",
3069 cid, err);
3070 }
Tharun Kumar Merugud86fc8c2018-01-04 16:35:31 +05303071 if (cid == 0 && me->channel[cid].ssrcount !=
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003072 me->channel[cid].prevssrcount) {
c_mtharue1a5ce12017-10-13 20:47:09 +05303073 if (fastrpc_mmap_remove_ssr(fl))
3074 pr_err("ADSPRPC: SSR: Failed to unmap remote heap\n");
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003075 me->channel[cid].prevssrcount =
3076 me->channel[cid].ssrcount;
3077 }
3078 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003079
3080bail:
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303081 mutex_unlock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003082 return err;
3083}
3084
Sathish Ambley36849af2017-02-02 09:35:55 -08003085static int fastrpc_device_open(struct inode *inode, struct file *filp)
3086{
3087 int err = 0;
Sathish Ambley567012b2017-03-06 11:55:04 -08003088 struct dentry *debugfs_file;
c_mtharue1a5ce12017-10-13 20:47:09 +05303089 struct fastrpc_file *fl = NULL;
Sathish Ambley36849af2017-02-02 09:35:55 -08003090 struct fastrpc_apps *me = &gfa;
3091
c_mtharue1a5ce12017-10-13 20:47:09 +05303092 VERIFY(err, NULL != (fl = kzalloc(sizeof(*fl), GFP_KERNEL)));
Sathish Ambley36849af2017-02-02 09:35:55 -08003093 if (err)
3094 return err;
Sathish Ambley567012b2017-03-06 11:55:04 -08003095 debugfs_file = debugfs_create_file(current->comm, 0644, debugfs_root,
3096 fl, &debugfs_fops);
Sathish Ambley36849af2017-02-02 09:35:55 -08003097 context_list_ctor(&fl->clst);
3098 spin_lock_init(&fl->hlock);
3099 INIT_HLIST_HEAD(&fl->maps);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05303100 INIT_HLIST_HEAD(&fl->perf);
Sathish Ambley36849af2017-02-02 09:35:55 -08003101 INIT_HLIST_HEAD(&fl->bufs);
3102 INIT_HLIST_NODE(&fl->hn);
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05303103 fl->sessionid = 0;
Sathish Ambley36849af2017-02-02 09:35:55 -08003104 fl->tgid = current->tgid;
3105 fl->apps = me;
3106 fl->mode = FASTRPC_MODE_SERIAL;
3107 fl->cid = -1;
Sathish Ambley567012b2017-03-06 11:55:04 -08003108 if (debugfs_file != NULL)
3109 fl->debugfs_file = debugfs_file;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303110 fl->qos_request = 0;
Tharun Kumar Merugu35173342018-02-08 16:13:17 +05303111 fl->refcount = 0;
Sathish Ambley36849af2017-02-02 09:35:55 -08003112 filp->private_data = fl;
Tharun Kumar Meruguc31eac52018-01-02 11:42:45 +05303113 mutex_init(&fl->map_mutex);
Tharun Kumar Merugued7a8472018-01-25 12:10:15 +05303114 mutex_init(&fl->fl_map_mutex);
Sathish Ambley36849af2017-02-02 09:35:55 -08003115 spin_lock(&me->hlock);
3116 hlist_add_head(&fl->hn, &me->drivers);
3117 spin_unlock(&me->hlock);
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05303118 mutex_init(&fl->perf_mutex);
Sathish Ambley36849af2017-02-02 09:35:55 -08003119 return 0;
3120}
3121
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003122static int fastrpc_get_info(struct fastrpc_file *fl, uint32_t *info)
3123{
3124 int err = 0;
Sathish Ambley36849af2017-02-02 09:35:55 -08003125 uint32_t cid;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003126
c_mtharue1a5ce12017-10-13 20:47:09 +05303127 VERIFY(err, fl != NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003128 if (err)
3129 goto bail;
Sathish Ambley36849af2017-02-02 09:35:55 -08003130 if (fl->cid == -1) {
3131 cid = *info;
3132 VERIFY(err, cid < NUM_CHANNELS);
3133 if (err)
3134 goto bail;
3135 fl->cid = cid;
3136 fl->ssrcount = fl->apps->channel[cid].ssrcount;
3137 VERIFY(err, !fastrpc_session_alloc_locked(
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303138 &fl->apps->channel[cid], 0, fl->sharedcb, &fl->sctx));
Sathish Ambley36849af2017-02-02 09:35:55 -08003139 if (err)
3140 goto bail;
3141 }
Tharun Kumar Merugu80be7d62017-08-02 11:03:22 +05303142 VERIFY(err, fl->sctx != NULL);
3143 if (err)
3144 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003145 *info = (fl->sctx->smmu.enabled ? 1 : 0);
3146bail:
3147 return err;
3148}
3149
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303150static int fastrpc_internal_control(struct fastrpc_file *fl,
3151 struct fastrpc_ioctl_control *cp)
3152{
3153 int err = 0;
3154 int latency;
3155
3156 VERIFY(err, !IS_ERR_OR_NULL(fl) && !IS_ERR_OR_NULL(fl->apps));
3157 if (err)
3158 goto bail;
3159 VERIFY(err, !IS_ERR_OR_NULL(cp));
3160 if (err)
3161 goto bail;
3162
3163 switch (cp->req) {
3164 case FASTRPC_CONTROL_LATENCY:
3165 latency = cp->lp.enable == FASTRPC_LATENCY_CTRL_ENB ?
3166 fl->apps->latency : PM_QOS_DEFAULT_VALUE;
3167 VERIFY(err, latency != 0);
3168 if (err)
3169 goto bail;
3170 if (!fl->qos_request) {
3171 pm_qos_add_request(&fl->pm_qos_req,
3172 PM_QOS_CPU_DMA_LATENCY, latency);
3173 fl->qos_request = 1;
3174 } else
3175 pm_qos_update_request(&fl->pm_qos_req, latency);
3176 break;
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303177 case FASTRPC_CONTROL_SMMU:
3178 fl->sharedcb = cp->smmu.sharedcb;
3179 break;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303180 default:
3181 err = -ENOTTY;
3182 break;
3183 }
3184bail:
3185 return err;
3186}
3187
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003188static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num,
3189 unsigned long ioctl_param)
3190{
3191 union {
Sathish Ambleybae51902017-07-03 15:00:49 -07003192 struct fastrpc_ioctl_invoke_crc inv;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003193 struct fastrpc_ioctl_mmap mmap;
3194 struct fastrpc_ioctl_munmap munmap;
c_mtharu7bd6a422017-10-17 18:15:37 +05303195 struct fastrpc_ioctl_munmap_fd munmap_fd;
Sathish Ambleyd6300c32017-01-18 09:50:43 -08003196 struct fastrpc_ioctl_init_attrs init;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003197 struct fastrpc_ioctl_perf perf;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303198 struct fastrpc_ioctl_control cp;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003199 } p;
3200 void *param = (char *)ioctl_param;
3201 struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data;
3202 int size = 0, err = 0;
3203 uint32_t info;
3204
c_mtharue1a5ce12017-10-13 20:47:09 +05303205 p.inv.fds = NULL;
3206 p.inv.attrs = NULL;
Sathish Ambleybae51902017-07-03 15:00:49 -07003207 p.inv.crc = NULL;
tharun kumar9f899ea2017-07-03 17:07:03 +05303208 spin_lock(&fl->hlock);
3209 if (fl->file_close == 1) {
3210 err = EBADF;
3211 pr_warn("ADSPRPC: fastrpc_device_release is happening, So not sending any new requests to DSP");
3212 spin_unlock(&fl->hlock);
3213 goto bail;
3214 }
3215 spin_unlock(&fl->hlock);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003216
3217 switch (ioctl_num) {
3218 case FASTRPC_IOCTL_INVOKE:
3219 size = sizeof(struct fastrpc_ioctl_invoke);
Sathish Ambleybae51902017-07-03 15:00:49 -07003220 /* fall through */
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003221 case FASTRPC_IOCTL_INVOKE_FD:
3222 if (!size)
3223 size = sizeof(struct fastrpc_ioctl_invoke_fd);
3224 /* fall through */
3225 case FASTRPC_IOCTL_INVOKE_ATTRS:
3226 if (!size)
3227 size = sizeof(struct fastrpc_ioctl_invoke_attrs);
Sathish Ambleybae51902017-07-03 15:00:49 -07003228 /* fall through */
3229 case FASTRPC_IOCTL_INVOKE_CRC:
3230 if (!size)
3231 size = sizeof(struct fastrpc_ioctl_invoke_crc);
c_mtharue1a5ce12017-10-13 20:47:09 +05303232 K_COPY_FROM_USER(err, 0, &p.inv, param, size);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003233 if (err)
3234 goto bail;
3235 VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, fl->mode,
3236 0, &p.inv)));
3237 if (err)
3238 goto bail;
3239 break;
3240 case FASTRPC_IOCTL_MMAP:
c_mtharue1a5ce12017-10-13 20:47:09 +05303241 K_COPY_FROM_USER(err, 0, &p.mmap, param,
3242 sizeof(p.mmap));
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05303243 if (err)
3244 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003245 VERIFY(err, 0 == (err = fastrpc_internal_mmap(fl, &p.mmap)));
3246 if (err)
3247 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05303248 K_COPY_TO_USER(err, 0, param, &p.mmap, sizeof(p.mmap));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003249 if (err)
3250 goto bail;
3251 break;
3252 case FASTRPC_IOCTL_MUNMAP:
c_mtharue1a5ce12017-10-13 20:47:09 +05303253 K_COPY_FROM_USER(err, 0, &p.munmap, param,
3254 sizeof(p.munmap));
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05303255 if (err)
3256 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003257 VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl,
3258 &p.munmap)));
3259 if (err)
3260 goto bail;
3261 break;
Tharun Kumar Merugu55be90d2018-05-31 11:41:03 +05303262 case FASTRPC_IOCTL_MMAP_64:
3263 K_COPY_FROM_USER(err, 0, &p.mmap, param,
3264 sizeof(p.mmap));
3265 if (err)
3266 goto bail;
3267 VERIFY(err, 0 == (err = fastrpc_internal_mmap(fl, &p.mmap)));
3268 if (err)
3269 goto bail;
3270 K_COPY_TO_USER(err, 0, param, &p.mmap, sizeof(p.mmap));
3271 if (err)
3272 goto bail;
3273 break;
3274 case FASTRPC_IOCTL_MUNMAP_64:
3275 K_COPY_FROM_USER(err, 0, &p.munmap, param,
3276 sizeof(p.munmap));
3277 if (err)
3278 goto bail;
3279 VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl,
3280 &p.munmap)));
3281 if (err)
3282 goto bail;
3283 break;
c_mtharu7bd6a422017-10-17 18:15:37 +05303284 case FASTRPC_IOCTL_MUNMAP_FD:
3285 K_COPY_FROM_USER(err, 0, &p.munmap_fd, param,
3286 sizeof(p.munmap_fd));
3287 if (err)
3288 goto bail;
3289 VERIFY(err, 0 == (err = fastrpc_internal_munmap_fd(fl,
3290 &p.munmap_fd)));
3291 if (err)
3292 goto bail;
3293 break;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003294 case FASTRPC_IOCTL_SETMODE:
3295 switch ((uint32_t)ioctl_param) {
3296 case FASTRPC_MODE_PARALLEL:
3297 case FASTRPC_MODE_SERIAL:
3298 fl->mode = (uint32_t)ioctl_param;
3299 break;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003300 case FASTRPC_MODE_PROFILE:
3301 fl->profile = (uint32_t)ioctl_param;
3302 break;
Tharun Kumar Merugud4d079482017-09-06 11:22:19 +05303303 case FASTRPC_MODE_SESSION:
3304 fl->sessionid = 1;
3305 fl->tgid |= (1 << SESSION_ID_INDEX);
3306 break;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003307 default:
3308 err = -ENOTTY;
3309 break;
3310 }
3311 break;
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003312 case FASTRPC_IOCTL_GETPERF:
c_mtharue1a5ce12017-10-13 20:47:09 +05303313 K_COPY_FROM_USER(err, 0, &p.perf,
3314 param, sizeof(p.perf));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003315 if (err)
3316 goto bail;
3317 p.perf.numkeys = sizeof(struct fastrpc_perf)/sizeof(int64_t);
3318 if (p.perf.keys) {
3319 char *keys = PERF_KEYS;
3320
c_mtharue1a5ce12017-10-13 20:47:09 +05303321 K_COPY_TO_USER(err, 0, (void *)p.perf.keys,
3322 keys, strlen(keys)+1);
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003323 if (err)
3324 goto bail;
3325 }
3326 if (p.perf.data) {
Tharun Kumar Merugu7c966dd2018-01-04 18:07:03 +05303327 struct fastrpc_perf *perf = NULL, *fperf = NULL;
3328 struct hlist_node *n = NULL;
3329
3330 mutex_lock(&fl->perf_mutex);
3331 hlist_for_each_entry_safe(perf, n, &fl->perf, hn) {
3332 if (perf->tid == current->pid) {
3333 fperf = perf;
3334 break;
3335 }
3336 }
3337
3338 mutex_unlock(&fl->perf_mutex);
3339
3340 if (fperf) {
3341 K_COPY_TO_USER(err, 0, (void *)p.perf.data,
3342 fperf, sizeof(*fperf));
3343 }
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003344 }
c_mtharue1a5ce12017-10-13 20:47:09 +05303345 K_COPY_TO_USER(err, 0, param, &p.perf, sizeof(p.perf));
Sathish Ambleya21b5b52017-01-11 16:11:01 -08003346 if (err)
3347 goto bail;
3348 break;
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303349 case FASTRPC_IOCTL_CONTROL:
c_mtharue1a5ce12017-10-13 20:47:09 +05303350 K_COPY_FROM_USER(err, 0, &p.cp, param,
3351 sizeof(p.cp));
Tharun Kumar Merugu5f6ca61c2017-08-11 11:43:11 +05303352 if (err)
3353 goto bail;
3354 VERIFY(err, 0 == (err = fastrpc_internal_control(fl, &p.cp)));
3355 if (err)
3356 goto bail;
3357 break;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003358 case FASTRPC_IOCTL_GETINFO:
c_mtharue1a5ce12017-10-13 20:47:09 +05303359 K_COPY_FROM_USER(err, 0, &info, param, sizeof(info));
Sathish Ambley36849af2017-02-02 09:35:55 -08003360 if (err)
3361 goto bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003362 VERIFY(err, 0 == (err = fastrpc_get_info(fl, &info)));
3363 if (err)
3364 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05303365 K_COPY_TO_USER(err, 0, param, &info, sizeof(info));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003366 if (err)
3367 goto bail;
3368 break;
3369 case FASTRPC_IOCTL_INIT:
Sathish Ambleyd6300c32017-01-18 09:50:43 -08003370 p.init.attrs = 0;
3371 p.init.siglen = 0;
3372 size = sizeof(struct fastrpc_ioctl_init);
3373 /* fall through */
3374 case FASTRPC_IOCTL_INIT_ATTRS:
3375 if (!size)
3376 size = sizeof(struct fastrpc_ioctl_init_attrs);
c_mtharue1a5ce12017-10-13 20:47:09 +05303377 K_COPY_FROM_USER(err, 0, &p.init, param, size);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003378 if (err)
3379 goto bail;
Tharun Kumar Merugu4ea0eac2017-08-22 11:42:51 +05303380 VERIFY(err, p.init.init.filelen >= 0 &&
Tharun Kumar Merugud1f388a2017-10-01 10:51:11 +05303381 p.init.init.filelen < INIT_FILELEN_MAX);
3382 if (err)
3383 goto bail;
3384 VERIFY(err, p.init.init.memlen >= 0 &&
3385 p.init.init.memlen < INIT_MEMLEN_MAX);
Tharun Kumar Merugu4ea0eac2017-08-22 11:42:51 +05303386 if (err)
3387 goto bail;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303388 VERIFY(err, 0 == (err = fastrpc_init_process(fl, &p.init)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003389 if (err)
3390 goto bail;
3391 break;
3392
3393 default:
3394 err = -ENOTTY;
3395 pr_info("bad ioctl: %d\n", ioctl_num);
3396 break;
3397 }
3398 bail:
3399 return err;
3400}
3401
3402static int fastrpc_restart_notifier_cb(struct notifier_block *nb,
3403 unsigned long code,
3404 void *data)
3405{
3406 struct fastrpc_apps *me = &gfa;
3407 struct fastrpc_channel_ctx *ctx;
c_mtharue1a5ce12017-10-13 20:47:09 +05303408 struct notif_data *notifdata = data;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003409 int cid;
3410
3411 ctx = container_of(nb, struct fastrpc_channel_ctx, nb);
3412 cid = ctx - &me->channel[0];
3413 if (code == SUBSYS_BEFORE_SHUTDOWN) {
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303414 mutex_lock(&me->smd_mutex);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003415 ctx->ssrcount++;
c_mtharue1a5ce12017-10-13 20:47:09 +05303416 ctx->issubsystemup = 0;
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303417 if (ctx->chan) {
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05303418 if (me->glink)
3419 fastrpc_glink_close(ctx->chan, cid);
3420 else
3421 smd_close(ctx->chan);
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303422 ctx->chan = NULL;
3423 pr_info("'restart notifier: closed /dev/%s c %d %d'\n",
3424 gcinfo[cid].name, MAJOR(me->dev_no), cid);
3425 }
3426 mutex_unlock(&me->smd_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05303427 if (cid == 0)
3428 me->staticpd_flags = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003429 fastrpc_notify_drivers(me, cid);
c_mtharue1a5ce12017-10-13 20:47:09 +05303430 } else if (code == SUBSYS_RAMDUMP_NOTIFICATION) {
3431 if (me->channel[0].remoteheap_ramdump_dev &&
3432 notifdata->enable_ramdump) {
3433 me->channel[0].ramdumpenabled = 1;
3434 }
3435 } else if (code == SUBSYS_AFTER_POWERUP) {
3436 ctx->issubsystemup = 1;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003437 }
3438
3439 return NOTIFY_DONE;
3440}
3441
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303442static int fastrpc_pdr_notifier_cb(struct notifier_block *pdrnb,
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05303443 unsigned long code,
3444 void *data)
3445{
3446 struct fastrpc_apps *me = &gfa;
3447 struct fastrpc_static_pd *spd;
3448 struct notif_data *notifdata = data;
3449
3450 spd = container_of(pdrnb, struct fastrpc_static_pd, pdrnb);
3451 if (code == SERVREG_NOTIF_SERVICE_STATE_DOWN_V01) {
3452 mutex_lock(&me->smd_mutex);
3453 spd->pdrcount++;
3454 spd->ispdup = 0;
3455 pr_info("ADSPRPC: Audio PDR notifier %d %s\n",
3456 MAJOR(me->dev_no), spd->spdname);
3457 mutex_unlock(&me->smd_mutex);
3458 if (!strcmp(spd->spdname,
3459 AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME))
3460 me->staticpd_flags = 0;
3461 fastrpc_notify_pdr_drivers(me, spd->spdname);
3462 } else if (code == SUBSYS_RAMDUMP_NOTIFICATION) {
3463 if (me->channel[0].remoteheap_ramdump_dev &&
3464 notifdata->enable_ramdump) {
3465 me->channel[0].ramdumpenabled = 1;
3466 }
3467 } else if (code == SERVREG_NOTIF_SERVICE_STATE_UP_V01) {
3468 spd->ispdup = 1;
3469 }
3470
3471 return NOTIFY_DONE;
3472}
3473
3474static int fastrpc_get_service_location_notify(struct notifier_block *nb,
3475 unsigned long opcode, void *data)
3476{
3477 struct fastrpc_static_pd *spd;
3478 struct pd_qmi_client_data *pdr = data;
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303479 int curr_state = 0, i = 0;
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05303480
3481 spd = container_of(nb, struct fastrpc_static_pd, get_service_nb);
3482 if (opcode == LOCATOR_DOWN) {
3483 pr_err("ADSPRPC: Audio PD restart notifier locator down\n");
3484 return NOTIFY_DONE;
3485 }
3486
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303487 for (i = 0; i < pdr->total_domains; i++) {
3488 if ((!strcmp(pdr->domain_list[i].name,
3489 "msm/adsp/audio_pd")) ||
3490 (!strcmp(pdr->domain_list[i].name,
3491 "msm/adsp/sensor_pd"))) {
3492 spd->pdrhandle =
3493 service_notif_register_notifier(
3494 pdr->domain_list[i].name,
3495 pdr->domain_list[i].instance_id,
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05303496 &spd->pdrnb, &curr_state);
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303497 if (IS_ERR(spd->pdrhandle))
3498 pr_err("ADSPRPC: Unable to register notifier\n");
3499 break;
3500 }
3501 }
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05303502
3503 return NOTIFY_DONE;
3504}
3505
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003506static const struct file_operations fops = {
3507 .open = fastrpc_device_open,
3508 .release = fastrpc_device_release,
3509 .unlocked_ioctl = fastrpc_device_ioctl,
3510 .compat_ioctl = compat_fastrpc_device_ioctl,
3511};
3512
3513static const struct of_device_id fastrpc_match_table[] = {
3514 { .compatible = "qcom,msm-fastrpc-adsp", },
3515 { .compatible = "qcom,msm-fastrpc-compute", },
3516 { .compatible = "qcom,msm-fastrpc-compute-cb", },
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05303517 { .compatible = "qcom,msm-fastrpc-legacy-compute", },
3518 { .compatible = "qcom,msm-fastrpc-legacy-compute-cb", },
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003519 { .compatible = "qcom,msm-adsprpc-mem-region", },
3520 {}
3521};
3522
3523static int fastrpc_cb_probe(struct device *dev)
3524{
3525 struct fastrpc_channel_ctx *chan;
3526 struct fastrpc_session_ctx *sess;
3527 struct of_phandle_args iommuspec;
3528 const char *name;
3529 unsigned int start = 0x80000000;
3530 int err = 0, i;
3531 int secure_vmid = VMID_CP_PIXEL;
3532
c_mtharue1a5ce12017-10-13 20:47:09 +05303533 VERIFY(err, NULL != (name = of_get_property(dev->of_node,
3534 "label", NULL)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003535 if (err)
3536 goto bail;
3537 for (i = 0; i < NUM_CHANNELS; i++) {
3538 if (!gcinfo[i].name)
3539 continue;
3540 if (!strcmp(name, gcinfo[i].name))
3541 break;
3542 }
3543 VERIFY(err, i < NUM_CHANNELS);
3544 if (err)
3545 goto bail;
3546 chan = &gcinfo[i];
3547 VERIFY(err, chan->sesscount < NUM_SESSIONS);
3548 if (err)
3549 goto bail;
3550
3551 VERIFY(err, !of_parse_phandle_with_args(dev->of_node, "iommus",
3552 "#iommu-cells", 0, &iommuspec));
3553 if (err)
3554 goto bail;
3555 sess = &chan->session[chan->sesscount];
3556 sess->smmu.cb = iommuspec.args[0] & 0xf;
3557 sess->used = 0;
3558 sess->smmu.coherent = of_property_read_bool(dev->of_node,
3559 "dma-coherent");
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303560 sess->smmu.sharedcb = of_property_read_bool(dev->of_node,
3561 "shared-cb");
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003562 sess->smmu.secure = of_property_read_bool(dev->of_node,
3563 "qcom,secure-context-bank");
3564 if (sess->smmu.secure)
3565 start = 0x60000000;
3566 VERIFY(err, !IS_ERR_OR_NULL(sess->smmu.mapping =
3567 arm_iommu_create_mapping(&platform_bus_type,
Tharun Kumar Meruguca183f92017-04-27 17:43:27 +05303568 start, 0x78000000)));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003569 if (err)
3570 goto bail;
3571
3572 if (sess->smmu.secure)
3573 iommu_domain_set_attr(sess->smmu.mapping->domain,
3574 DOMAIN_ATTR_SECURE_VMID,
3575 &secure_vmid);
3576
3577 VERIFY(err, !arm_iommu_attach_device(dev, sess->smmu.mapping));
3578 if (err)
3579 goto bail;
c_mtharue1a5ce12017-10-13 20:47:09 +05303580 sess->smmu.dev = dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003581 sess->smmu.enabled = 1;
3582 chan->sesscount++;
Sathish Ambley1ca68232017-01-19 10:32:55 -08003583 debugfs_global_file = debugfs_create_file("global", 0644, debugfs_root,
3584 NULL, &debugfs_fops);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003585bail:
3586 return err;
3587}
3588
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05303589static int fastrpc_cb_legacy_probe(struct device *dev)
3590{
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05303591 struct fastrpc_channel_ctx *chan;
3592 struct fastrpc_session_ctx *first_sess = NULL, *sess = NULL;
3593 const char *name;
3594 unsigned int *sids = NULL, sids_size = 0;
3595 int err = 0, ret = 0, i;
3596
3597 unsigned int start = 0x80000000;
3598
3599 VERIFY(err, NULL != (name = of_get_property(dev->of_node,
3600 "label", NULL)));
3601 if (err)
3602 goto bail;
3603
3604 for (i = 0; i < NUM_CHANNELS; i++) {
3605 if (!gcinfo[i].name)
3606 continue;
3607 if (!strcmp(name, gcinfo[i].name))
3608 break;
3609 }
3610 VERIFY(err, i < NUM_CHANNELS);
3611 if (err)
3612 goto bail;
3613
3614 chan = &gcinfo[i];
3615 VERIFY(err, chan->sesscount < NUM_SESSIONS);
3616 if (err)
3617 goto bail;
3618
3619 first_sess = &chan->session[chan->sesscount];
3620
3621 VERIFY(err, NULL != of_get_property(dev->of_node,
3622 "sids", &sids_size));
3623 if (err)
3624 goto bail;
3625
3626 VERIFY(err, NULL != (sids = kzalloc(sids_size, GFP_KERNEL)));
3627 if (err)
3628 goto bail;
3629 ret = of_property_read_u32_array(dev->of_node, "sids", sids,
3630 sids_size/sizeof(unsigned int));
3631 if (ret)
3632 goto bail;
3633
3634 VERIFY(err, !IS_ERR_OR_NULL(first_sess->smmu.mapping =
3635 arm_iommu_create_mapping(&platform_bus_type,
3636 start, 0x78000000)));
3637 if (err)
3638 goto bail;
3639
3640 VERIFY(err, !arm_iommu_attach_device(dev, first_sess->smmu.mapping));
3641 if (err)
3642 goto bail;
3643
3644
3645 for (i = 0; i < sids_size/sizeof(unsigned int); i++) {
3646 VERIFY(err, chan->sesscount < NUM_SESSIONS);
3647 if (err)
3648 goto bail;
3649 sess = &chan->session[chan->sesscount];
3650 sess->smmu.cb = sids[i];
3651 sess->smmu.dev = dev;
3652 sess->smmu.mapping = first_sess->smmu.mapping;
3653 sess->smmu.enabled = 1;
3654 sess->used = 0;
3655 sess->smmu.coherent = false;
3656 sess->smmu.secure = false;
3657 chan->sesscount++;
3658 }
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05303659bail:
3660 kfree(sids);
3661 return err;
3662}
3663
3664
3665
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05303666static void init_secure_vmid_list(struct device *dev, char *prop_name,
3667 struct secure_vm *destvm)
3668{
3669 int err = 0;
3670 u32 len = 0, i = 0;
3671 u32 *rhvmlist = NULL;
3672 u32 *rhvmpermlist = NULL;
3673
3674 if (!of_find_property(dev->of_node, prop_name, &len))
3675 goto bail;
3676 if (len == 0)
3677 goto bail;
3678 len /= sizeof(u32);
3679 VERIFY(err, NULL != (rhvmlist = kcalloc(len, sizeof(u32), GFP_KERNEL)));
3680 if (err)
3681 goto bail;
3682 VERIFY(err, NULL != (rhvmpermlist = kcalloc(len, sizeof(u32),
3683 GFP_KERNEL)));
3684 if (err)
3685 goto bail;
3686 for (i = 0; i < len; i++) {
3687 err = of_property_read_u32_index(dev->of_node, prop_name, i,
3688 &rhvmlist[i]);
3689 rhvmpermlist[i] = PERM_READ | PERM_WRITE | PERM_EXEC;
3690 pr_info("ADSPRPC: Secure VMID = %d", rhvmlist[i]);
3691 if (err) {
3692 pr_err("ADSPRPC: Failed to read VMID\n");
3693 goto bail;
3694 }
3695 }
3696 destvm->vmid = rhvmlist;
3697 destvm->vmperm = rhvmpermlist;
3698 destvm->vmcount = len;
3699bail:
3700 if (err) {
3701 kfree(rhvmlist);
3702 kfree(rhvmpermlist);
3703 }
3704}
3705
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003706static int fastrpc_probe(struct platform_device *pdev)
3707{
3708 int err = 0;
3709 struct fastrpc_apps *me = &gfa;
3710 struct device *dev = &pdev->dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003711 struct device_node *ion_node, *node;
3712 struct platform_device *ion_pdev;
3713 struct cma *cma;
3714 uint32_t val;
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05303715 int ret = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003716
c_mtharu63ffc012017-11-16 15:26:56 +05303717
3718 if (of_device_is_compatible(dev->of_node,
3719 "qcom,msm-fastrpc-compute")) {
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05303720 init_secure_vmid_list(dev, "qcom,adsp-remoteheap-vmid",
3721 &gcinfo[0].rhvm);
c_mtharu63ffc012017-11-16 15:26:56 +05303722
c_mtharu63ffc012017-11-16 15:26:56 +05303723
3724 of_property_read_u32(dev->of_node, "qcom,rpc-latency-us",
3725 &me->latency);
3726 }
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003727 if (of_device_is_compatible(dev->of_node,
3728 "qcom,msm-fastrpc-compute-cb"))
3729 return fastrpc_cb_probe(dev);
3730
3731 if (of_device_is_compatible(dev->of_node,
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05303732 "qcom,msm-fastrpc-legacy-compute")) {
3733 me->glink = false;
Tharun Kumar Merugu4f2dcc82018-03-29 00:35:49 +05303734 me->legacy = 1;
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05303735 }
3736
3737 if (of_device_is_compatible(dev->of_node,
3738 "qcom,msm-fastrpc-legacy-compute-cb")){
3739 return fastrpc_cb_legacy_probe(dev);
3740 }
3741
3742 if (of_device_is_compatible(dev->of_node,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003743 "qcom,msm-adsprpc-mem-region")) {
3744 me->dev = dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003745 ion_node = of_find_compatible_node(NULL, NULL, "qcom,msm-ion");
3746 if (ion_node) {
3747 for_each_available_child_of_node(ion_node, node) {
3748 if (of_property_read_u32(node, "reg", &val))
3749 continue;
3750 if (val != ION_ADSP_HEAP_ID)
3751 continue;
3752 ion_pdev = of_find_device_by_node(node);
3753 if (!ion_pdev)
3754 break;
3755 cma = dev_get_cma_area(&ion_pdev->dev);
3756 if (cma) {
Tharun Kumar Merugu4f2dcc82018-03-29 00:35:49 +05303757 me->range.addr = cma_get_base(cma);
3758 me->range.size =
3759 (size_t)cma_get_size(cma);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003760 }
3761 break;
3762 }
3763 }
Tharun Kumar Merugu4f2dcc82018-03-29 00:35:49 +05303764 if (me->range.addr && !of_property_read_bool(dev->of_node,
Tharun Kumar Merugu6b7a4a22018-01-17 16:08:07 +05303765 "restrict-access")) {
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003766 int srcVM[1] = {VMID_HLOS};
3767 int destVM[4] = {VMID_HLOS, VMID_MSS_MSA, VMID_SSC_Q6,
3768 VMID_ADSP_Q6};
Sathish Ambley84d11862017-05-15 14:36:05 -07003769 int destVMperm[4] = {PERM_READ | PERM_WRITE | PERM_EXEC,
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003770 PERM_READ | PERM_WRITE | PERM_EXEC,
3771 PERM_READ | PERM_WRITE | PERM_EXEC,
3772 PERM_READ | PERM_WRITE | PERM_EXEC,
3773 };
3774
Tharun Kumar Merugu4f2dcc82018-03-29 00:35:49 +05303775 VERIFY(err, !hyp_assign_phys(me->range.addr,
3776 me->range.size, srcVM, 1,
3777 destVM, destVMperm, 4));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003778 if (err)
3779 goto bail;
3780 }
3781 return 0;
3782 }
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05303783 if (of_property_read_bool(dev->of_node,
3784 "qcom,fastrpc-adsp-audio-pdr")) {
3785 int session;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003786
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05303787 VERIFY(err, !fastrpc_get_adsp_session(
3788 AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME, &session));
3789 if (err)
3790 goto spdbail;
3791 me->channel[0].spd[session].get_service_nb.notifier_call =
3792 fastrpc_get_service_location_notify;
3793 ret = get_service_location(
3794 AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME,
3795 AUDIO_PDR_ADSP_SERVICE_NAME,
3796 &me->channel[0].spd[session].get_service_nb);
3797 if (ret)
3798 pr_err("ADSPRPC: Get service location failed: %d\n",
3799 ret);
3800 }
Tharun Kumar Merugu848c0952018-02-07 21:37:19 +05303801 if (of_property_read_bool(dev->of_node,
3802 "qcom,fastrpc-adsp-sensors-pdr")) {
3803 int session;
3804
3805 VERIFY(err, !fastrpc_get_adsp_session(
3806 SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME, &session));
3807 if (err)
3808 goto spdbail;
3809 me->channel[0].spd[session].get_service_nb.notifier_call =
3810 fastrpc_get_service_location_notify;
3811 ret = get_service_location(
3812 SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME,
3813 SENSORS_PDR_ADSP_SERVICE_NAME,
3814 &me->channel[0].spd[session].get_service_nb);
3815 if (ret)
3816 pr_err("ADSPRPC: Get service location failed: %d\n",
3817 ret);
3818 }
Tharun Kumar Merugudf860662018-01-17 19:59:50 +05303819spdbail:
3820 err = 0;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003821 VERIFY(err, !of_platform_populate(pdev->dev.of_node,
3822 fastrpc_match_table,
3823 NULL, &pdev->dev));
3824 if (err)
3825 goto bail;
3826bail:
3827 return err;
3828}
3829
3830static void fastrpc_deinit(void)
3831{
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303832 struct fastrpc_apps *me = &gfa;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003833 struct fastrpc_channel_ctx *chan = gcinfo;
3834 int i, j;
3835
3836 for (i = 0; i < NUM_CHANNELS; i++, chan++) {
3837 if (chan->chan) {
3838 kref_put_mutex(&chan->kref,
Tharun Kumar Merugu642fcce2017-12-07 19:22:10 +05303839 fastrpc_channel_close, &me->smd_mutex);
c_mtharue1a5ce12017-10-13 20:47:09 +05303840 chan->chan = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003841 }
3842 for (j = 0; j < NUM_SESSIONS; j++) {
3843 struct fastrpc_session_ctx *sess = &chan->session[j];
c_mtharue1a5ce12017-10-13 20:47:09 +05303844 if (sess->smmu.dev) {
3845 arm_iommu_detach_device(sess->smmu.dev);
3846 sess->smmu.dev = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003847 }
3848 if (sess->smmu.mapping) {
3849 arm_iommu_release_mapping(sess->smmu.mapping);
c_mtharue1a5ce12017-10-13 20:47:09 +05303850 sess->smmu.mapping = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003851 }
3852 }
Tharun Kumar Merugu3937e912017-12-21 16:24:37 +05303853 kfree(chan->rhvm.vmid);
3854 kfree(chan->rhvm.vmperm);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003855 }
3856}
3857
3858static struct platform_driver fastrpc_driver = {
3859 .probe = fastrpc_probe,
3860 .driver = {
3861 .name = "fastrpc",
3862 .owner = THIS_MODULE,
3863 .of_match_table = fastrpc_match_table,
3864 },
3865};
3866
3867static int __init fastrpc_device_init(void)
3868{
3869 struct fastrpc_apps *me = &gfa;
c_mtharue1a5ce12017-10-13 20:47:09 +05303870 struct device *dev = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003871 int err = 0, i;
3872
3873 memset(me, 0, sizeof(*me));
3874
3875 fastrpc_init(me);
3876 me->dev = NULL;
Tharun Kumar Merugubbc78f22018-01-22 19:26:44 +05303877 me->glink = true;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003878 VERIFY(err, 0 == platform_driver_register(&fastrpc_driver));
3879 if (err)
3880 goto register_bail;
3881 VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, NUM_CHANNELS,
3882 DEVICE_NAME));
3883 if (err)
3884 goto alloc_chrdev_bail;
3885 cdev_init(&me->cdev, &fops);
3886 me->cdev.owner = THIS_MODULE;
3887 VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0),
Sathish Ambley36849af2017-02-02 09:35:55 -08003888 1));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003889 if (err)
3890 goto cdev_init_bail;
3891 me->class = class_create(THIS_MODULE, "fastrpc");
3892 VERIFY(err, !IS_ERR(me->class));
3893 if (err)
3894 goto class_create_bail;
3895 me->compat = (fops.compat_ioctl == NULL) ? 0 : 1;
Sathish Ambley36849af2017-02-02 09:35:55 -08003896 dev = device_create(me->class, NULL,
3897 MKDEV(MAJOR(me->dev_no), 0),
3898 NULL, gcinfo[0].name);
3899 VERIFY(err, !IS_ERR_OR_NULL(dev));
3900 if (err)
3901 goto device_create_bail;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003902 for (i = 0; i < NUM_CHANNELS; i++) {
Sathish Ambley36849af2017-02-02 09:35:55 -08003903 me->channel[i].dev = dev;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003904 me->channel[i].ssrcount = 0;
3905 me->channel[i].prevssrcount = 0;
c_mtharue1a5ce12017-10-13 20:47:09 +05303906 me->channel[i].issubsystemup = 1;
3907 me->channel[i].ramdumpenabled = 0;
3908 me->channel[i].remoteheap_ramdump_dev = NULL;
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003909 me->channel[i].nb.notifier_call = fastrpc_restart_notifier_cb;
3910 me->channel[i].handle = subsys_notif_register_notifier(
3911 gcinfo[i].subsys,
3912 &me->channel[i].nb);
3913 }
3914
3915 me->client = msm_ion_client_create(DEVICE_NAME);
3916 VERIFY(err, !IS_ERR_OR_NULL(me->client));
3917 if (err)
3918 goto device_create_bail;
Sathish Ambley1ca68232017-01-19 10:32:55 -08003919 debugfs_root = debugfs_create_dir("adsprpc", NULL);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003920 return 0;
3921device_create_bail:
3922 for (i = 0; i < NUM_CHANNELS; i++) {
Sathish Ambley36849af2017-02-02 09:35:55 -08003923 if (me->channel[i].handle)
3924 subsys_notif_unregister_notifier(me->channel[i].handle,
3925 &me->channel[i].nb);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003926 }
Sathish Ambley36849af2017-02-02 09:35:55 -08003927 if (!IS_ERR_OR_NULL(dev))
3928 device_destroy(me->class, MKDEV(MAJOR(me->dev_no), 0));
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003929 class_destroy(me->class);
3930class_create_bail:
3931 cdev_del(&me->cdev);
3932cdev_init_bail:
3933 unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
3934alloc_chrdev_bail:
3935register_bail:
3936 fastrpc_deinit();
3937 return err;
3938}
3939
3940static void __exit fastrpc_device_exit(void)
3941{
3942 struct fastrpc_apps *me = &gfa;
3943 int i;
3944
3945 fastrpc_file_list_dtor(me);
3946 fastrpc_deinit();
3947 for (i = 0; i < NUM_CHANNELS; i++) {
3948 if (!gcinfo[i].name)
3949 continue;
3950 device_destroy(me->class, MKDEV(MAJOR(me->dev_no), i));
3951 subsys_notif_unregister_notifier(me->channel[i].handle,
3952 &me->channel[i].nb);
3953 }
3954 class_destroy(me->class);
3955 cdev_del(&me->cdev);
3956 unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
3957 ion_client_destroy(me->client);
Sathish Ambley1ca68232017-01-19 10:32:55 -08003958 debugfs_remove_recursive(debugfs_root);
Sathish Ambley69e1ab02016-10-18 10:28:15 -07003959}
3960
3961late_initcall(fastrpc_device_init);
3962module_exit(fastrpc_device_exit);
3963
3964MODULE_LICENSE("GPL v2");