Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1 | /* |
Tharun Kumar Merugu | bcd6fbf | 2018-01-04 17:49:34 +0530 | [diff] [blame] | 2 | * Copyright (c) 2012-2018, The Linux Foundation. All rights reserved. |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | */ |
| 14 | #include <linux/dma-buf.h> |
| 15 | #include <linux/dma-mapping.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/completion.h> |
| 18 | #include <linux/pagemap.h> |
| 19 | #include <linux/mm.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/cdev.h> |
| 24 | #include <linux/list.h> |
| 25 | #include <linux/hash.h> |
| 26 | #include <linux/msm_ion.h> |
| 27 | #include <soc/qcom/secure_buffer.h> |
| 28 | #include <soc/qcom/glink.h> |
| 29 | #include <soc/qcom/subsystem_notif.h> |
| 30 | #include <soc/qcom/subsystem_restart.h> |
| 31 | #include <linux/scatterlist.h> |
| 32 | #include <linux/fs.h> |
| 33 | #include <linux/uaccess.h> |
| 34 | #include <linux/device.h> |
| 35 | #include <linux/of.h> |
| 36 | #include <linux/of_address.h> |
| 37 | #include <linux/of_platform.h> |
| 38 | #include <linux/dma-contiguous.h> |
| 39 | #include <linux/cma.h> |
| 40 | #include <linux/iommu.h> |
| 41 | #include <linux/kref.h> |
| 42 | #include <linux/sort.h> |
| 43 | #include <linux/msm_dma_iommu_mapping.h> |
| 44 | #include <asm/dma-iommu.h> |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 45 | #include <soc/qcom/scm.h> |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 46 | #include "adsprpc_compat.h" |
| 47 | #include "adsprpc_shared.h" |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 48 | #include <soc/qcom/ramdump.h> |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 49 | #include <linux/debugfs.h> |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 50 | #include <linux/pm_qos.h> |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 51 | #define TZ_PIL_PROTECT_MEM_SUBSYS_ID 0x0C |
| 52 | #define TZ_PIL_CLEAR_PROTECT_MEM_SUBSYS_ID 0x0D |
| 53 | #define TZ_PIL_AUTH_QDSP6_PROC 1 |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 54 | #define ADSP_MMAP_HEAP_ADDR 4 |
| 55 | #define ADSP_MMAP_REMOTE_HEAP_ADDR 8 |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 56 | #define FASTRPC_ENOSUCH 39 |
| 57 | #define VMID_SSC_Q6 5 |
| 58 | #define VMID_ADSP_Q6 6 |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 59 | #define DEBUGFS_SIZE 1024 |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 60 | |
| 61 | #define RPC_TIMEOUT (5 * HZ) |
| 62 | #define BALIGN 128 |
| 63 | #define NUM_CHANNELS 4 /* adsp, mdsp, slpi, cdsp*/ |
| 64 | #define NUM_SESSIONS 9 /*8 compute, 1 cpz*/ |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 65 | #define M_FDLIST (16) |
| 66 | #define M_CRCLIST (64) |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 67 | #define SESSION_ID_INDEX (30) |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 68 | #define FASTRPC_CTX_MAGIC (0xbeeddeed) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 69 | |
| 70 | #define IS_CACHE_ALIGNED(x) (((x) & ((L1_CACHE_BYTES)-1)) == 0) |
| 71 | |
| 72 | #define FASTRPC_LINK_STATE_DOWN (0x0) |
| 73 | #define FASTRPC_LINK_STATE_UP (0x1) |
| 74 | #define FASTRPC_LINK_DISCONNECTED (0x0) |
| 75 | #define FASTRPC_LINK_CONNECTING (0x1) |
| 76 | #define FASTRPC_LINK_CONNECTED (0x3) |
| 77 | #define FASTRPC_LINK_DISCONNECTING (0x7) |
c_mtharu | 314a420 | 2017-11-15 22:09:17 +0530 | [diff] [blame] | 78 | #define FASTRPC_LINK_REMOTE_DISCONNECTING (0x8) |
| 79 | #define FASTRPC_GLINK_INTENT_LEN (64) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 80 | |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 81 | #define PERF_KEYS \ |
| 82 | "count:flush:map:copy:glink:getargs:putargs:invalidate:invoke:tid:ptr" |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 83 | #define FASTRPC_STATIC_HANDLE_LISTENER (3) |
| 84 | #define FASTRPC_STATIC_HANDLE_MAX (20) |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 85 | #define FASTRPC_LATENCY_CTRL_ENB (1) |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 86 | |
Tharun Kumar Merugu | d1f388a | 2017-10-01 10:51:11 +0530 | [diff] [blame] | 87 | #define INIT_FILELEN_MAX (2*1024*1024) |
| 88 | #define INIT_MEMLEN_MAX (8*1024*1024) |
| 89 | |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 90 | #define PERF_END (void)0 |
| 91 | |
| 92 | #define PERF(enb, cnt, ff) \ |
| 93 | {\ |
| 94 | struct timespec startT = {0};\ |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 95 | int64_t *counter = cnt;\ |
| 96 | if (enb && counter) {\ |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 97 | getnstimeofday(&startT);\ |
| 98 | } \ |
| 99 | ff ;\ |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 100 | if (enb && counter) {\ |
| 101 | *counter += getnstimediff(&startT);\ |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 102 | } \ |
| 103 | } |
| 104 | |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 105 | #define GET_COUNTER(perf_ptr, offset) \ |
| 106 | (perf_ptr != NULL ?\ |
| 107 | (((offset >= 0) && (offset < PERF_KEY_MAX)) ?\ |
| 108 | (int64_t *)(perf_ptr + offset)\ |
| 109 | : (int64_t *)NULL) : (int64_t *)NULL) |
| 110 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 111 | static int fastrpc_glink_open(int cid); |
| 112 | static void fastrpc_glink_close(void *chan, int cid); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 113 | static struct dentry *debugfs_root; |
| 114 | static struct dentry *debugfs_global_file; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 115 | |
| 116 | static inline uint64_t buf_page_start(uint64_t buf) |
| 117 | { |
| 118 | uint64_t start = (uint64_t) buf & PAGE_MASK; |
| 119 | return start; |
| 120 | } |
| 121 | |
| 122 | static inline uint64_t buf_page_offset(uint64_t buf) |
| 123 | { |
| 124 | uint64_t offset = (uint64_t) buf & (PAGE_SIZE - 1); |
| 125 | return offset; |
| 126 | } |
| 127 | |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 128 | static inline uint64_t buf_num_pages(uint64_t buf, size_t len) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 129 | { |
| 130 | uint64_t start = buf_page_start(buf) >> PAGE_SHIFT; |
| 131 | uint64_t end = (((uint64_t) buf + len - 1) & PAGE_MASK) >> PAGE_SHIFT; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 132 | uint64_t nPages = end - start + 1; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 133 | return nPages; |
| 134 | } |
| 135 | |
| 136 | static inline uint64_t buf_page_size(uint32_t size) |
| 137 | { |
| 138 | uint64_t sz = (size + (PAGE_SIZE - 1)) & PAGE_MASK; |
| 139 | |
| 140 | return sz > PAGE_SIZE ? sz : PAGE_SIZE; |
| 141 | } |
| 142 | |
| 143 | static inline void *uint64_to_ptr(uint64_t addr) |
| 144 | { |
| 145 | void *ptr = (void *)((uintptr_t)addr); |
| 146 | |
| 147 | return ptr; |
| 148 | } |
| 149 | |
| 150 | static inline uint64_t ptr_to_uint64(void *ptr) |
| 151 | { |
| 152 | uint64_t addr = (uint64_t)((uintptr_t)ptr); |
| 153 | |
| 154 | return addr; |
| 155 | } |
| 156 | |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 157 | struct secure_vm { |
| 158 | int *vmid; |
| 159 | int *vmperm; |
| 160 | int vmcount; |
| 161 | }; |
| 162 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 163 | struct fastrpc_file; |
| 164 | |
| 165 | struct fastrpc_buf { |
| 166 | struct hlist_node hn; |
| 167 | struct fastrpc_file *fl; |
| 168 | void *virt; |
| 169 | uint64_t phys; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 170 | size_t size; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | struct fastrpc_ctx_lst; |
| 174 | |
| 175 | struct overlap { |
| 176 | uintptr_t start; |
| 177 | uintptr_t end; |
| 178 | int raix; |
| 179 | uintptr_t mstart; |
| 180 | uintptr_t mend; |
| 181 | uintptr_t offset; |
| 182 | }; |
| 183 | |
| 184 | struct smq_invoke_ctx { |
| 185 | struct hlist_node hn; |
| 186 | struct completion work; |
| 187 | int retval; |
| 188 | int pid; |
| 189 | int tgid; |
| 190 | remote_arg_t *lpra; |
| 191 | remote_arg64_t *rpra; |
| 192 | int *fds; |
| 193 | unsigned int *attrs; |
| 194 | struct fastrpc_mmap **maps; |
| 195 | struct fastrpc_buf *buf; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 196 | size_t used; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 197 | struct fastrpc_file *fl; |
| 198 | uint32_t sc; |
| 199 | struct overlap *overs; |
| 200 | struct overlap **overps; |
| 201 | struct smq_msg msg; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 202 | uint32_t *crc; |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 203 | unsigned int magic; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | struct fastrpc_ctx_lst { |
| 207 | struct hlist_head pending; |
| 208 | struct hlist_head interrupted; |
| 209 | }; |
| 210 | |
| 211 | struct fastrpc_smmu { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 212 | struct device *dev; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 213 | struct dma_iommu_mapping *mapping; |
| 214 | int cb; |
| 215 | int enabled; |
| 216 | int faults; |
| 217 | int secure; |
| 218 | int coherent; |
| 219 | }; |
| 220 | |
| 221 | struct fastrpc_session_ctx { |
| 222 | struct device *dev; |
| 223 | struct fastrpc_smmu smmu; |
| 224 | int used; |
| 225 | }; |
| 226 | |
| 227 | struct fastrpc_glink_info { |
| 228 | int link_state; |
| 229 | int port_state; |
| 230 | struct glink_open_config cfg; |
| 231 | struct glink_link_info link_info; |
| 232 | void *link_notify_handle; |
| 233 | }; |
| 234 | |
| 235 | struct fastrpc_channel_ctx { |
| 236 | char *name; |
| 237 | char *subsys; |
| 238 | void *chan; |
| 239 | struct device *dev; |
| 240 | struct fastrpc_session_ctx session[NUM_SESSIONS]; |
| 241 | struct completion work; |
Tharun Kumar Merugu | 53a8ec9 | 2017-07-14 15:52:49 +0530 | [diff] [blame] | 242 | struct completion workport; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 243 | struct notifier_block nb; |
| 244 | struct kref kref; |
| 245 | int sesscount; |
| 246 | int ssrcount; |
| 247 | void *handle; |
| 248 | int prevssrcount; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 249 | int issubsystemup; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 250 | int vmid; |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 251 | struct secure_vm rhvm; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 252 | int ramdumpenabled; |
| 253 | void *remoteheap_ramdump_dev; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 254 | struct fastrpc_glink_info link; |
| 255 | }; |
| 256 | |
| 257 | struct fastrpc_apps { |
| 258 | struct fastrpc_channel_ctx *channel; |
| 259 | struct cdev cdev; |
| 260 | struct class *class; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 261 | struct mutex smd_mutex; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 262 | struct smq_phy_page range; |
| 263 | struct hlist_head maps; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 264 | uint32_t staticpd_flags; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 265 | dev_t dev_no; |
| 266 | int compat; |
| 267 | struct hlist_head drivers; |
| 268 | spinlock_t hlock; |
| 269 | struct ion_client *client; |
| 270 | struct device *dev; |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 271 | unsigned int latency; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | struct fastrpc_mmap { |
| 275 | struct hlist_node hn; |
| 276 | struct fastrpc_file *fl; |
| 277 | struct fastrpc_apps *apps; |
| 278 | int fd; |
| 279 | uint32_t flags; |
| 280 | struct dma_buf *buf; |
| 281 | struct sg_table *table; |
| 282 | struct dma_buf_attachment *attach; |
| 283 | struct ion_handle *handle; |
| 284 | uint64_t phys; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 285 | size_t size; |
| 286 | uintptr_t va; |
| 287 | size_t len; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 288 | int refs; |
| 289 | uintptr_t raddr; |
| 290 | int uncached; |
| 291 | int secure; |
| 292 | uintptr_t attr; |
| 293 | }; |
| 294 | |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 295 | enum fastrpc_perfkeys { |
| 296 | PERF_COUNT = 0, |
| 297 | PERF_FLUSH = 1, |
| 298 | PERF_MAP = 2, |
| 299 | PERF_COPY = 3, |
| 300 | PERF_LINK = 4, |
| 301 | PERF_GETARGS = 5, |
| 302 | PERF_PUTARGS = 6, |
| 303 | PERF_INVARGS = 7, |
| 304 | PERF_INVOKE = 8, |
| 305 | PERF_KEY_MAX = 9, |
| 306 | }; |
| 307 | |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 308 | struct fastrpc_perf { |
| 309 | int64_t count; |
| 310 | int64_t flush; |
| 311 | int64_t map; |
| 312 | int64_t copy; |
| 313 | int64_t link; |
| 314 | int64_t getargs; |
| 315 | int64_t putargs; |
| 316 | int64_t invargs; |
| 317 | int64_t invoke; |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 318 | int64_t tid; |
| 319 | struct hlist_node hn; |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 320 | }; |
| 321 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 322 | struct fastrpc_file { |
| 323 | struct hlist_node hn; |
| 324 | spinlock_t hlock; |
| 325 | struct hlist_head maps; |
| 326 | struct hlist_head bufs; |
| 327 | struct fastrpc_ctx_lst clst; |
| 328 | struct fastrpc_session_ctx *sctx; |
| 329 | struct fastrpc_session_ctx *secsctx; |
| 330 | uint32_t mode; |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 331 | uint32_t profile; |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 332 | int sessionid; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 333 | int tgid; |
| 334 | int cid; |
| 335 | int ssrcount; |
| 336 | int pd; |
tharun kumar | 9f899ea | 2017-07-03 17:07:03 +0530 | [diff] [blame] | 337 | int file_close; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 338 | struct fastrpc_apps *apps; |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 339 | struct hlist_head perf; |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 340 | struct dentry *debugfs_file; |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 341 | struct mutex perf_mutex; |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 342 | struct pm_qos_request pm_qos_req; |
| 343 | int qos_request; |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 344 | struct mutex map_mutex; |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 345 | struct mutex fl_map_mutex; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 346 | }; |
| 347 | |
| 348 | static struct fastrpc_apps gfa; |
| 349 | |
| 350 | static struct fastrpc_channel_ctx gcinfo[NUM_CHANNELS] = { |
| 351 | { |
| 352 | .name = "adsprpc-smd", |
| 353 | .subsys = "adsp", |
| 354 | .link.link_info.edge = "lpass", |
| 355 | .link.link_info.transport = "smem", |
| 356 | }, |
| 357 | { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 358 | .name = "mdsprpc-smd", |
| 359 | .subsys = "modem", |
| 360 | .link.link_info.edge = "mpss", |
| 361 | .link.link_info.transport = "smem", |
| 362 | }, |
| 363 | { |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 364 | .name = "sdsprpc-smd", |
| 365 | .subsys = "slpi", |
| 366 | .link.link_info.edge = "dsps", |
| 367 | .link.link_info.transport = "smem", |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 368 | }, |
| 369 | { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 370 | .name = "cdsprpc-smd", |
| 371 | .subsys = "cdsp", |
| 372 | .link.link_info.edge = "cdsp", |
| 373 | .link.link_info.transport = "smem", |
| 374 | }, |
| 375 | }; |
| 376 | |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 377 | static int hlosvm[1] = {VMID_HLOS}; |
| 378 | static int hlosvmperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC}; |
| 379 | |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 380 | static inline int64_t getnstimediff(struct timespec *start) |
| 381 | { |
| 382 | int64_t ns; |
| 383 | struct timespec ts, b; |
| 384 | |
| 385 | getnstimeofday(&ts); |
| 386 | b = timespec_sub(ts, *start); |
| 387 | ns = timespec_to_ns(&b); |
| 388 | return ns; |
| 389 | } |
| 390 | |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 391 | static inline int64_t *getperfcounter(struct fastrpc_file *fl, int key) |
| 392 | { |
| 393 | int err = 0; |
| 394 | int64_t *val = NULL; |
| 395 | struct fastrpc_perf *perf = NULL, *fperf = NULL; |
| 396 | struct hlist_node *n = NULL; |
| 397 | |
| 398 | VERIFY(err, !IS_ERR_OR_NULL(fl)); |
| 399 | if (err) |
| 400 | goto bail; |
| 401 | |
| 402 | mutex_lock(&fl->perf_mutex); |
| 403 | hlist_for_each_entry_safe(perf, n, &fl->perf, hn) { |
| 404 | if (perf->tid == current->pid) { |
| 405 | fperf = perf; |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | if (IS_ERR_OR_NULL(fperf)) { |
| 411 | fperf = kzalloc(sizeof(*fperf), GFP_KERNEL); |
| 412 | |
| 413 | VERIFY(err, !IS_ERR_OR_NULL(fperf)); |
| 414 | if (err) { |
| 415 | mutex_unlock(&fl->perf_mutex); |
| 416 | kfree(fperf); |
| 417 | goto bail; |
| 418 | } |
| 419 | |
| 420 | fperf->tid = current->pid; |
| 421 | hlist_add_head(&fperf->hn, &fl->perf); |
| 422 | } |
| 423 | |
| 424 | val = ((int64_t *)fperf) + key; |
| 425 | mutex_unlock(&fl->perf_mutex); |
| 426 | bail: |
| 427 | return val; |
| 428 | } |
| 429 | |
| 430 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 431 | static void fastrpc_buf_free(struct fastrpc_buf *buf, int cache) |
| 432 | { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 433 | struct fastrpc_file *fl = buf == NULL ? NULL : buf->fl; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 434 | int vmid; |
| 435 | |
| 436 | if (!fl) |
| 437 | return; |
| 438 | if (cache) { |
| 439 | spin_lock(&fl->hlock); |
| 440 | hlist_add_head(&buf->hn, &fl->bufs); |
| 441 | spin_unlock(&fl->hlock); |
| 442 | return; |
| 443 | } |
| 444 | if (!IS_ERR_OR_NULL(buf->virt)) { |
| 445 | int destVM[1] = {VMID_HLOS}; |
| 446 | int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC}; |
| 447 | |
| 448 | if (fl->sctx->smmu.cb) |
| 449 | buf->phys &= ~((uint64_t)fl->sctx->smmu.cb << 32); |
| 450 | vmid = fl->apps->channel[fl->cid].vmid; |
| 451 | if (vmid) { |
| 452 | int srcVM[2] = {VMID_HLOS, vmid}; |
| 453 | |
| 454 | hyp_assign_phys(buf->phys, buf_page_size(buf->size), |
| 455 | srcVM, 2, destVM, destVMperm, 1); |
| 456 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 457 | dma_free_coherent(fl->sctx->smmu.dev, buf->size, buf->virt, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 458 | buf->phys); |
| 459 | } |
| 460 | kfree(buf); |
| 461 | } |
| 462 | |
| 463 | static void fastrpc_buf_list_free(struct fastrpc_file *fl) |
| 464 | { |
| 465 | struct fastrpc_buf *buf, *free; |
| 466 | |
| 467 | do { |
| 468 | struct hlist_node *n; |
| 469 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 470 | free = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 471 | spin_lock(&fl->hlock); |
| 472 | hlist_for_each_entry_safe(buf, n, &fl->bufs, hn) { |
| 473 | hlist_del_init(&buf->hn); |
| 474 | free = buf; |
| 475 | break; |
| 476 | } |
| 477 | spin_unlock(&fl->hlock); |
| 478 | if (free) |
| 479 | fastrpc_buf_free(free, 0); |
| 480 | } while (free); |
| 481 | } |
| 482 | |
| 483 | static void fastrpc_mmap_add(struct fastrpc_mmap *map) |
| 484 | { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 485 | if (map->flags == ADSP_MMAP_HEAP_ADDR || |
| 486 | map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) { |
| 487 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 488 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 489 | spin_lock(&me->hlock); |
| 490 | hlist_add_head(&map->hn, &me->maps); |
| 491 | spin_unlock(&me->hlock); |
| 492 | } else { |
| 493 | struct fastrpc_file *fl = map->fl; |
| 494 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 495 | hlist_add_head(&map->hn, &fl->maps); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 496 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 497 | } |
| 498 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 499 | static int fastrpc_mmap_find(struct fastrpc_file *fl, int fd, |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 500 | uintptr_t va, size_t len, int mflags, int refs, |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 501 | struct fastrpc_mmap **ppmap) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 502 | { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 503 | struct fastrpc_apps *me = &gfa; |
| 504 | struct fastrpc_mmap *match = NULL, *map = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 505 | struct hlist_node *n; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 506 | |
| 507 | if ((va + len) < va) |
| 508 | return -EOVERFLOW; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 509 | if (mflags == ADSP_MMAP_HEAP_ADDR || |
| 510 | mflags == ADSP_MMAP_REMOTE_HEAP_ADDR) { |
| 511 | spin_lock(&me->hlock); |
| 512 | hlist_for_each_entry_safe(map, n, &me->maps, hn) { |
| 513 | if (va >= map->va && |
| 514 | va + len <= map->va + map->len && |
| 515 | map->fd == fd) { |
| 516 | if (refs) |
| 517 | map->refs++; |
| 518 | match = map; |
| 519 | break; |
| 520 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 521 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 522 | spin_unlock(&me->hlock); |
| 523 | } else { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 524 | hlist_for_each_entry_safe(map, n, &fl->maps, hn) { |
| 525 | if (va >= map->va && |
| 526 | va + len <= map->va + map->len && |
| 527 | map->fd == fd) { |
| 528 | if (refs) |
| 529 | map->refs++; |
| 530 | match = map; |
| 531 | break; |
| 532 | } |
| 533 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 534 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 535 | if (match) { |
| 536 | *ppmap = match; |
| 537 | return 0; |
| 538 | } |
| 539 | return -ENOTTY; |
| 540 | } |
| 541 | |
c_mtharu | f931ff9 | 2017-11-30 19:35:30 +0530 | [diff] [blame] | 542 | static int dma_alloc_memory(dma_addr_t *region_phys, void **vaddr, size_t size) |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 543 | { |
| 544 | struct fastrpc_apps *me = &gfa; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 545 | |
| 546 | if (me->dev == NULL) { |
| 547 | pr_err("device adsprpc-mem is not initialized\n"); |
| 548 | return -ENODEV; |
| 549 | } |
c_mtharu | f931ff9 | 2017-11-30 19:35:30 +0530 | [diff] [blame] | 550 | *vaddr = dma_alloc_coherent(me->dev, size, region_phys, GFP_KERNEL); |
| 551 | if (!*vaddr) { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 552 | pr_err("ADSPRPC: Failed to allocate %x remote heap memory\n", |
| 553 | (unsigned int)size); |
| 554 | return -ENOMEM; |
| 555 | } |
| 556 | return 0; |
| 557 | } |
| 558 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 559 | static int fastrpc_mmap_remove(struct fastrpc_file *fl, uintptr_t va, |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 560 | size_t len, struct fastrpc_mmap **ppmap) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 561 | { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 562 | struct fastrpc_mmap *match = NULL, *map; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 563 | struct hlist_node *n; |
| 564 | struct fastrpc_apps *me = &gfa; |
| 565 | |
| 566 | spin_lock(&me->hlock); |
| 567 | hlist_for_each_entry_safe(map, n, &me->maps, hn) { |
| 568 | if (map->raddr == va && |
| 569 | map->raddr + map->len == va + len && |
| 570 | map->refs == 1) { |
| 571 | match = map; |
| 572 | hlist_del_init(&map->hn); |
| 573 | break; |
| 574 | } |
| 575 | } |
| 576 | spin_unlock(&me->hlock); |
| 577 | if (match) { |
| 578 | *ppmap = match; |
| 579 | return 0; |
| 580 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 581 | hlist_for_each_entry_safe(map, n, &fl->maps, hn) { |
| 582 | if (map->raddr == va && |
| 583 | map->raddr + map->len == va + len && |
| 584 | map->refs == 1) { |
| 585 | match = map; |
| 586 | hlist_del_init(&map->hn); |
| 587 | break; |
| 588 | } |
| 589 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 590 | if (match) { |
| 591 | *ppmap = match; |
| 592 | return 0; |
| 593 | } |
| 594 | return -ENOTTY; |
| 595 | } |
| 596 | |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 597 | static void fastrpc_mmap_free(struct fastrpc_mmap *map, uint32_t flags) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 598 | { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 599 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 600 | struct fastrpc_file *fl; |
| 601 | int vmid; |
| 602 | struct fastrpc_session_ctx *sess; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 603 | |
| 604 | if (!map) |
| 605 | return; |
| 606 | fl = map->fl; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 607 | if (map->flags == ADSP_MMAP_HEAP_ADDR || |
| 608 | map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) { |
| 609 | spin_lock(&me->hlock); |
| 610 | map->refs--; |
| 611 | if (!map->refs) |
| 612 | hlist_del_init(&map->hn); |
| 613 | spin_unlock(&me->hlock); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 614 | if (map->refs > 0) |
| 615 | return; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 616 | } else { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 617 | map->refs--; |
| 618 | if (!map->refs) |
| 619 | hlist_del_init(&map->hn); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 620 | if (map->refs > 0 && !flags) |
| 621 | return; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 622 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 623 | if (map->flags == ADSP_MMAP_HEAP_ADDR || |
| 624 | map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 625 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 626 | if (me->dev == NULL) { |
| 627 | pr_err("failed to free remote heap allocation\n"); |
| 628 | return; |
| 629 | } |
| 630 | if (map->phys) { |
| 631 | dma_free_coherent(me->dev, map->size, |
c_mtharu | f931ff9 | 2017-11-30 19:35:30 +0530 | [diff] [blame] | 632 | (void *)map->va, (dma_addr_t)map->phys); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 633 | } |
| 634 | } else { |
| 635 | int destVM[1] = {VMID_HLOS}; |
| 636 | int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC}; |
| 637 | |
| 638 | if (map->secure) |
| 639 | sess = fl->secsctx; |
| 640 | else |
| 641 | sess = fl->sctx; |
| 642 | |
| 643 | if (!IS_ERR_OR_NULL(map->handle)) |
| 644 | ion_free(fl->apps->client, map->handle); |
| 645 | if (sess && sess->smmu.enabled) { |
| 646 | if (map->size || map->phys) |
| 647 | msm_dma_unmap_sg(sess->smmu.dev, |
| 648 | map->table->sgl, |
| 649 | map->table->nents, DMA_BIDIRECTIONAL, |
| 650 | map->buf); |
| 651 | } |
| 652 | vmid = fl->apps->channel[fl->cid].vmid; |
| 653 | if (vmid && map->phys) { |
| 654 | int srcVM[2] = {VMID_HLOS, vmid}; |
| 655 | |
| 656 | hyp_assign_phys(map->phys, buf_page_size(map->size), |
| 657 | srcVM, 2, destVM, destVMperm, 1); |
| 658 | } |
| 659 | |
| 660 | if (!IS_ERR_OR_NULL(map->table)) |
| 661 | dma_buf_unmap_attachment(map->attach, map->table, |
| 662 | DMA_BIDIRECTIONAL); |
| 663 | if (!IS_ERR_OR_NULL(map->attach)) |
| 664 | dma_buf_detach(map->buf, map->attach); |
| 665 | if (!IS_ERR_OR_NULL(map->buf)) |
| 666 | dma_buf_put(map->buf); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 667 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 668 | kfree(map); |
| 669 | } |
| 670 | |
| 671 | static int fastrpc_session_alloc(struct fastrpc_channel_ctx *chan, int secure, |
| 672 | struct fastrpc_session_ctx **session); |
| 673 | |
| 674 | static int fastrpc_mmap_create(struct fastrpc_file *fl, int fd, |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 675 | unsigned int attr, uintptr_t va, size_t len, int mflags, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 676 | struct fastrpc_mmap **ppmap) |
| 677 | { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 678 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 679 | struct fastrpc_session_ctx *sess; |
| 680 | struct fastrpc_apps *apps = fl->apps; |
| 681 | int cid = fl->cid; |
| 682 | struct fastrpc_channel_ctx *chan = &apps->channel[cid]; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 683 | struct fastrpc_mmap *map = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 684 | unsigned long attrs; |
c_mtharu | f931ff9 | 2017-11-30 19:35:30 +0530 | [diff] [blame] | 685 | dma_addr_t region_phys = 0; |
| 686 | void *region_vaddr = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 687 | unsigned long flags; |
| 688 | int err = 0, vmid; |
| 689 | |
Sathish Ambley | ae5ee54 | 2017-01-16 22:24:23 -0800 | [diff] [blame] | 690 | if (!fastrpc_mmap_find(fl, fd, va, len, mflags, 1, ppmap)) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 691 | return 0; |
| 692 | map = kzalloc(sizeof(*map), GFP_KERNEL); |
| 693 | VERIFY(err, !IS_ERR_OR_NULL(map)); |
| 694 | if (err) |
| 695 | goto bail; |
| 696 | INIT_HLIST_NODE(&map->hn); |
| 697 | map->flags = mflags; |
| 698 | map->refs = 1; |
| 699 | map->fl = fl; |
| 700 | map->fd = fd; |
| 701 | map->attr = attr; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 702 | if (mflags == ADSP_MMAP_HEAP_ADDR || |
| 703 | mflags == ADSP_MMAP_REMOTE_HEAP_ADDR) { |
| 704 | map->apps = me; |
| 705 | map->fl = NULL; |
c_mtharu | f931ff9 | 2017-11-30 19:35:30 +0530 | [diff] [blame] | 706 | VERIFY(err, !dma_alloc_memory(®ion_phys, ®ion_vaddr, |
| 707 | len)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 708 | if (err) |
| 709 | goto bail; |
c_mtharu | f931ff9 | 2017-11-30 19:35:30 +0530 | [diff] [blame] | 710 | map->phys = (uintptr_t)region_phys; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 711 | map->size = len; |
c_mtharu | f931ff9 | 2017-11-30 19:35:30 +0530 | [diff] [blame] | 712 | map->va = (uintptr_t)region_vaddr; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 713 | } else { |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 714 | if (map->attr && (map->attr & FASTRPC_ATTR_KEEP_MAP)) { |
| 715 | pr_info("adsprpc: buffer mapped with persist attr %x\n", |
| 716 | (unsigned int)map->attr); |
| 717 | map->refs = 2; |
| 718 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 719 | VERIFY(err, !IS_ERR_OR_NULL(map->handle = |
| 720 | ion_import_dma_buf_fd(fl->apps->client, fd))); |
| 721 | if (err) |
| 722 | goto bail; |
| 723 | VERIFY(err, !ion_handle_get_flags(fl->apps->client, map->handle, |
| 724 | &flags)); |
| 725 | if (err) |
| 726 | goto bail; |
| 727 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 728 | map->secure = flags & ION_FLAG_SECURE; |
| 729 | if (map->secure) { |
| 730 | if (!fl->secsctx) |
| 731 | err = fastrpc_session_alloc(chan, 1, |
| 732 | &fl->secsctx); |
| 733 | if (err) |
| 734 | goto bail; |
| 735 | } |
| 736 | if (map->secure) |
| 737 | sess = fl->secsctx; |
| 738 | else |
| 739 | sess = fl->sctx; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 740 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 741 | VERIFY(err, !IS_ERR_OR_NULL(sess)); |
| 742 | if (err) |
| 743 | goto bail; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 744 | |
| 745 | map->uncached = !ION_IS_CACHED(flags); |
| 746 | if (map->attr & FASTRPC_ATTR_NOVA && !sess->smmu.coherent) |
| 747 | map->uncached = 1; |
| 748 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 749 | VERIFY(err, !IS_ERR_OR_NULL(map->buf = dma_buf_get(fd))); |
| 750 | if (err) |
| 751 | goto bail; |
| 752 | VERIFY(err, !IS_ERR_OR_NULL(map->attach = |
| 753 | dma_buf_attach(map->buf, sess->smmu.dev))); |
| 754 | if (err) |
| 755 | goto bail; |
| 756 | VERIFY(err, !IS_ERR_OR_NULL(map->table = |
| 757 | dma_buf_map_attachment(map->attach, |
| 758 | DMA_BIDIRECTIONAL))); |
| 759 | if (err) |
| 760 | goto bail; |
| 761 | if (sess->smmu.enabled) { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 762 | attrs = DMA_ATTR_EXEC_MAPPING; |
Tharun Kumar Merugu | 2e5f12e | 2017-07-06 12:04:40 +0530 | [diff] [blame] | 763 | |
| 764 | if (map->attr & FASTRPC_ATTR_NON_COHERENT || |
| 765 | (sess->smmu.coherent && map->uncached)) |
| 766 | attrs |= DMA_ATTR_FORCE_NON_COHERENT; |
| 767 | else if (map->attr & FASTRPC_ATTR_COHERENT) |
| 768 | attrs |= DMA_ATTR_FORCE_COHERENT; |
| 769 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 770 | VERIFY(err, map->table->nents == |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 771 | msm_dma_map_sg_attrs(sess->smmu.dev, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 772 | map->table->sgl, map->table->nents, |
| 773 | DMA_BIDIRECTIONAL, map->buf, attrs)); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 774 | if (err) |
| 775 | goto bail; |
| 776 | } else { |
| 777 | VERIFY(err, map->table->nents == 1); |
| 778 | if (err) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 779 | goto bail; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 780 | } |
| 781 | map->phys = sg_dma_address(map->table->sgl); |
| 782 | if (sess->smmu.cb) { |
| 783 | map->phys += ((uint64_t)sess->smmu.cb << 32); |
| 784 | map->size = sg_dma_len(map->table->sgl); |
| 785 | } else { |
| 786 | map->size = buf_page_size(len); |
| 787 | } |
| 788 | vmid = fl->apps->channel[fl->cid].vmid; |
| 789 | if (vmid) { |
| 790 | int srcVM[1] = {VMID_HLOS}; |
| 791 | int destVM[2] = {VMID_HLOS, vmid}; |
| 792 | int destVMperm[2] = {PERM_READ | PERM_WRITE, |
| 793 | PERM_READ | PERM_WRITE | PERM_EXEC}; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 794 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 795 | VERIFY(err, !hyp_assign_phys(map->phys, |
| 796 | buf_page_size(map->size), |
| 797 | srcVM, 1, destVM, destVMperm, 2)); |
| 798 | if (err) |
| 799 | goto bail; |
| 800 | } |
| 801 | map->va = va; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 802 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 803 | map->len = len; |
| 804 | |
| 805 | fastrpc_mmap_add(map); |
| 806 | *ppmap = map; |
| 807 | |
| 808 | bail: |
| 809 | if (err && map) |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 810 | fastrpc_mmap_free(map, 0); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 811 | return err; |
| 812 | } |
| 813 | |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 814 | static int fastrpc_buf_alloc(struct fastrpc_file *fl, size_t size, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 815 | struct fastrpc_buf **obuf) |
| 816 | { |
| 817 | int err = 0, vmid; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 818 | struct fastrpc_buf *buf = NULL, *fr = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 819 | struct hlist_node *n; |
| 820 | |
| 821 | VERIFY(err, size > 0); |
| 822 | if (err) |
| 823 | goto bail; |
| 824 | |
| 825 | /* find the smallest buffer that fits in the cache */ |
| 826 | spin_lock(&fl->hlock); |
| 827 | hlist_for_each_entry_safe(buf, n, &fl->bufs, hn) { |
| 828 | if (buf->size >= size && (!fr || fr->size > buf->size)) |
| 829 | fr = buf; |
| 830 | } |
| 831 | if (fr) |
| 832 | hlist_del_init(&fr->hn); |
| 833 | spin_unlock(&fl->hlock); |
| 834 | if (fr) { |
| 835 | *obuf = fr; |
| 836 | return 0; |
| 837 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 838 | buf = NULL; |
| 839 | VERIFY(err, NULL != (buf = kzalloc(sizeof(*buf), GFP_KERNEL))); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 840 | if (err) |
| 841 | goto bail; |
| 842 | INIT_HLIST_NODE(&buf->hn); |
| 843 | buf->fl = fl; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 844 | buf->virt = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 845 | buf->phys = 0; |
| 846 | buf->size = size; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 847 | buf->virt = dma_alloc_coherent(fl->sctx->smmu.dev, buf->size, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 848 | (void *)&buf->phys, GFP_KERNEL); |
| 849 | if (IS_ERR_OR_NULL(buf->virt)) { |
| 850 | /* free cache and retry */ |
| 851 | fastrpc_buf_list_free(fl); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 852 | buf->virt = dma_alloc_coherent(fl->sctx->smmu.dev, buf->size, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 853 | (void *)&buf->phys, GFP_KERNEL); |
| 854 | VERIFY(err, !IS_ERR_OR_NULL(buf->virt)); |
| 855 | } |
| 856 | if (err) |
| 857 | goto bail; |
| 858 | if (fl->sctx->smmu.cb) |
| 859 | buf->phys += ((uint64_t)fl->sctx->smmu.cb << 32); |
| 860 | vmid = fl->apps->channel[fl->cid].vmid; |
| 861 | if (vmid) { |
| 862 | int srcVM[1] = {VMID_HLOS}; |
| 863 | int destVM[2] = {VMID_HLOS, vmid}; |
| 864 | int destVMperm[2] = {PERM_READ | PERM_WRITE, |
| 865 | PERM_READ | PERM_WRITE | PERM_EXEC}; |
| 866 | |
| 867 | VERIFY(err, !hyp_assign_phys(buf->phys, buf_page_size(size), |
| 868 | srcVM, 1, destVM, destVMperm, 2)); |
| 869 | if (err) |
| 870 | goto bail; |
| 871 | } |
| 872 | |
| 873 | *obuf = buf; |
| 874 | bail: |
| 875 | if (err && buf) |
| 876 | fastrpc_buf_free(buf, 0); |
| 877 | return err; |
| 878 | } |
| 879 | |
| 880 | |
| 881 | static int context_restore_interrupted(struct fastrpc_file *fl, |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 882 | struct fastrpc_ioctl_invoke_crc *inv, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 883 | struct smq_invoke_ctx **po) |
| 884 | { |
| 885 | int err = 0; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 886 | struct smq_invoke_ctx *ctx = NULL, *ictx = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 887 | struct hlist_node *n; |
| 888 | struct fastrpc_ioctl_invoke *invoke = &inv->inv; |
| 889 | |
| 890 | spin_lock(&fl->hlock); |
| 891 | hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) { |
| 892 | if (ictx->pid == current->pid) { |
| 893 | if (invoke->sc != ictx->sc || ictx->fl != fl) |
| 894 | err = -1; |
| 895 | else { |
| 896 | ctx = ictx; |
| 897 | hlist_del_init(&ctx->hn); |
| 898 | hlist_add_head(&ctx->hn, &fl->clst.pending); |
| 899 | } |
| 900 | break; |
| 901 | } |
| 902 | } |
| 903 | spin_unlock(&fl->hlock); |
| 904 | if (ctx) |
| 905 | *po = ctx; |
| 906 | return err; |
| 907 | } |
| 908 | |
| 909 | #define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1) |
| 910 | static int overlap_ptr_cmp(const void *a, const void *b) |
| 911 | { |
| 912 | struct overlap *pa = *((struct overlap **)a); |
| 913 | struct overlap *pb = *((struct overlap **)b); |
| 914 | /* sort with lowest starting buffer first */ |
| 915 | int st = CMP(pa->start, pb->start); |
| 916 | /* sort with highest ending buffer first */ |
| 917 | int ed = CMP(pb->end, pa->end); |
| 918 | return st == 0 ? ed : st; |
| 919 | } |
| 920 | |
Sathish Ambley | 9466d67 | 2017-01-25 10:51:55 -0800 | [diff] [blame] | 921 | static int context_build_overlap(struct smq_invoke_ctx *ctx) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 922 | { |
Sathish Ambley | 9466d67 | 2017-01-25 10:51:55 -0800 | [diff] [blame] | 923 | int i, err = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 924 | remote_arg_t *lpra = ctx->lpra; |
| 925 | int inbufs = REMOTE_SCALARS_INBUFS(ctx->sc); |
| 926 | int outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc); |
| 927 | int nbufs = inbufs + outbufs; |
| 928 | struct overlap max; |
| 929 | |
| 930 | for (i = 0; i < nbufs; ++i) { |
| 931 | ctx->overs[i].start = (uintptr_t)lpra[i].buf.pv; |
| 932 | ctx->overs[i].end = ctx->overs[i].start + lpra[i].buf.len; |
Sathish Ambley | 9466d67 | 2017-01-25 10:51:55 -0800 | [diff] [blame] | 933 | if (lpra[i].buf.len) { |
| 934 | VERIFY(err, ctx->overs[i].end > ctx->overs[i].start); |
| 935 | if (err) |
| 936 | goto bail; |
| 937 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 938 | ctx->overs[i].raix = i; |
| 939 | ctx->overps[i] = &ctx->overs[i]; |
| 940 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 941 | sort(ctx->overps, nbufs, sizeof(*ctx->overps), overlap_ptr_cmp, NULL); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 942 | max.start = 0; |
| 943 | max.end = 0; |
| 944 | for (i = 0; i < nbufs; ++i) { |
| 945 | if (ctx->overps[i]->start < max.end) { |
| 946 | ctx->overps[i]->mstart = max.end; |
| 947 | ctx->overps[i]->mend = ctx->overps[i]->end; |
| 948 | ctx->overps[i]->offset = max.end - |
| 949 | ctx->overps[i]->start; |
| 950 | if (ctx->overps[i]->end > max.end) { |
| 951 | max.end = ctx->overps[i]->end; |
| 952 | } else { |
| 953 | ctx->overps[i]->mend = 0; |
| 954 | ctx->overps[i]->mstart = 0; |
| 955 | } |
| 956 | } else { |
| 957 | ctx->overps[i]->mend = ctx->overps[i]->end; |
| 958 | ctx->overps[i]->mstart = ctx->overps[i]->start; |
| 959 | ctx->overps[i]->offset = 0; |
| 960 | max = *ctx->overps[i]; |
| 961 | } |
| 962 | } |
Sathish Ambley | 9466d67 | 2017-01-25 10:51:55 -0800 | [diff] [blame] | 963 | bail: |
| 964 | return err; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | #define K_COPY_FROM_USER(err, kernel, dst, src, size) \ |
| 968 | do {\ |
| 969 | if (!(kernel))\ |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 970 | VERIFY(err, 0 == copy_from_user((dst),\ |
| 971 | (void const __user *)(src),\ |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 972 | (size)));\ |
| 973 | else\ |
| 974 | memmove((dst), (src), (size));\ |
| 975 | } while (0) |
| 976 | |
| 977 | #define K_COPY_TO_USER(err, kernel, dst, src, size) \ |
| 978 | do {\ |
| 979 | if (!(kernel))\ |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 980 | VERIFY(err, 0 == copy_to_user((void __user *)(dst),\ |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 981 | (src), (size)));\ |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 982 | else\ |
| 983 | memmove((dst), (src), (size));\ |
| 984 | } while (0) |
| 985 | |
| 986 | |
| 987 | static void context_free(struct smq_invoke_ctx *ctx); |
| 988 | |
| 989 | static int context_alloc(struct fastrpc_file *fl, uint32_t kernel, |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 990 | struct fastrpc_ioctl_invoke_crc *invokefd, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 991 | struct smq_invoke_ctx **po) |
| 992 | { |
| 993 | int err = 0, bufs, size = 0; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 994 | struct smq_invoke_ctx *ctx = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 995 | struct fastrpc_ctx_lst *clst = &fl->clst; |
| 996 | struct fastrpc_ioctl_invoke *invoke = &invokefd->inv; |
| 997 | |
| 998 | bufs = REMOTE_SCALARS_LENGTH(invoke->sc); |
| 999 | size = bufs * sizeof(*ctx->lpra) + bufs * sizeof(*ctx->maps) + |
| 1000 | sizeof(*ctx->fds) * (bufs) + |
| 1001 | sizeof(*ctx->attrs) * (bufs) + |
| 1002 | sizeof(*ctx->overs) * (bufs) + |
| 1003 | sizeof(*ctx->overps) * (bufs); |
| 1004 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1005 | VERIFY(err, NULL != (ctx = kzalloc(sizeof(*ctx) + size, GFP_KERNEL))); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1006 | if (err) |
| 1007 | goto bail; |
| 1008 | |
| 1009 | INIT_HLIST_NODE(&ctx->hn); |
| 1010 | hlist_add_fake(&ctx->hn); |
| 1011 | ctx->fl = fl; |
| 1012 | ctx->maps = (struct fastrpc_mmap **)(&ctx[1]); |
| 1013 | ctx->lpra = (remote_arg_t *)(&ctx->maps[bufs]); |
| 1014 | ctx->fds = (int *)(&ctx->lpra[bufs]); |
| 1015 | ctx->attrs = (unsigned int *)(&ctx->fds[bufs]); |
| 1016 | ctx->overs = (struct overlap *)(&ctx->attrs[bufs]); |
| 1017 | ctx->overps = (struct overlap **)(&ctx->overs[bufs]); |
| 1018 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1019 | K_COPY_FROM_USER(err, kernel, (void *)ctx->lpra, invoke->pra, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1020 | bufs * sizeof(*ctx->lpra)); |
| 1021 | if (err) |
| 1022 | goto bail; |
| 1023 | |
| 1024 | if (invokefd->fds) { |
| 1025 | K_COPY_FROM_USER(err, kernel, ctx->fds, invokefd->fds, |
| 1026 | bufs * sizeof(*ctx->fds)); |
| 1027 | if (err) |
| 1028 | goto bail; |
| 1029 | } |
| 1030 | if (invokefd->attrs) { |
| 1031 | K_COPY_FROM_USER(err, kernel, ctx->attrs, invokefd->attrs, |
| 1032 | bufs * sizeof(*ctx->attrs)); |
| 1033 | if (err) |
| 1034 | goto bail; |
| 1035 | } |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1036 | ctx->crc = (uint32_t *)invokefd->crc; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1037 | ctx->sc = invoke->sc; |
Sathish Ambley | 9466d67 | 2017-01-25 10:51:55 -0800 | [diff] [blame] | 1038 | if (bufs) { |
| 1039 | VERIFY(err, 0 == context_build_overlap(ctx)); |
| 1040 | if (err) |
| 1041 | goto bail; |
| 1042 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1043 | ctx->retval = -1; |
| 1044 | ctx->pid = current->pid; |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 1045 | ctx->tgid = fl->tgid; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1046 | init_completion(&ctx->work); |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 1047 | ctx->magic = FASTRPC_CTX_MAGIC; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1048 | |
| 1049 | spin_lock(&fl->hlock); |
| 1050 | hlist_add_head(&ctx->hn, &clst->pending); |
| 1051 | spin_unlock(&fl->hlock); |
| 1052 | |
| 1053 | *po = ctx; |
| 1054 | bail: |
| 1055 | if (ctx && err) |
| 1056 | context_free(ctx); |
| 1057 | return err; |
| 1058 | } |
| 1059 | |
| 1060 | static void context_save_interrupted(struct smq_invoke_ctx *ctx) |
| 1061 | { |
| 1062 | struct fastrpc_ctx_lst *clst = &ctx->fl->clst; |
| 1063 | |
| 1064 | spin_lock(&ctx->fl->hlock); |
| 1065 | hlist_del_init(&ctx->hn); |
| 1066 | hlist_add_head(&ctx->hn, &clst->interrupted); |
| 1067 | spin_unlock(&ctx->fl->hlock); |
| 1068 | /* free the cache on power collapse */ |
| 1069 | fastrpc_buf_list_free(ctx->fl); |
| 1070 | } |
| 1071 | |
| 1072 | static void context_free(struct smq_invoke_ctx *ctx) |
| 1073 | { |
| 1074 | int i; |
| 1075 | int nbufs = REMOTE_SCALARS_INBUFS(ctx->sc) + |
| 1076 | REMOTE_SCALARS_OUTBUFS(ctx->sc); |
| 1077 | spin_lock(&ctx->fl->hlock); |
| 1078 | hlist_del_init(&ctx->hn); |
| 1079 | spin_unlock(&ctx->fl->hlock); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1080 | mutex_lock(&ctx->fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1081 | for (i = 0; i < nbufs; ++i) |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 1082 | fastrpc_mmap_free(ctx->maps[i], 0); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1083 | |
| 1084 | mutex_unlock(&ctx->fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1085 | fastrpc_buf_free(ctx->buf, 1); |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 1086 | ctx->magic = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1087 | kfree(ctx); |
| 1088 | } |
| 1089 | |
| 1090 | static void context_notify_user(struct smq_invoke_ctx *ctx, int retval) |
| 1091 | { |
| 1092 | ctx->retval = retval; |
| 1093 | complete(&ctx->work); |
| 1094 | } |
| 1095 | |
| 1096 | |
| 1097 | static void fastrpc_notify_users(struct fastrpc_file *me) |
| 1098 | { |
| 1099 | struct smq_invoke_ctx *ictx; |
| 1100 | struct hlist_node *n; |
| 1101 | |
| 1102 | spin_lock(&me->hlock); |
| 1103 | hlist_for_each_entry_safe(ictx, n, &me->clst.pending, hn) { |
| 1104 | complete(&ictx->work); |
| 1105 | } |
| 1106 | hlist_for_each_entry_safe(ictx, n, &me->clst.interrupted, hn) { |
| 1107 | complete(&ictx->work); |
| 1108 | } |
| 1109 | spin_unlock(&me->hlock); |
| 1110 | |
| 1111 | } |
| 1112 | |
| 1113 | static void fastrpc_notify_drivers(struct fastrpc_apps *me, int cid) |
| 1114 | { |
| 1115 | struct fastrpc_file *fl; |
| 1116 | struct hlist_node *n; |
| 1117 | |
| 1118 | spin_lock(&me->hlock); |
| 1119 | hlist_for_each_entry_safe(fl, n, &me->drivers, hn) { |
| 1120 | if (fl->cid == cid) |
| 1121 | fastrpc_notify_users(fl); |
| 1122 | } |
| 1123 | spin_unlock(&me->hlock); |
| 1124 | |
| 1125 | } |
| 1126 | static void context_list_ctor(struct fastrpc_ctx_lst *me) |
| 1127 | { |
| 1128 | INIT_HLIST_HEAD(&me->interrupted); |
| 1129 | INIT_HLIST_HEAD(&me->pending); |
| 1130 | } |
| 1131 | |
| 1132 | static void fastrpc_context_list_dtor(struct fastrpc_file *fl) |
| 1133 | { |
| 1134 | struct fastrpc_ctx_lst *clst = &fl->clst; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1135 | struct smq_invoke_ctx *ictx = NULL, *ctxfree; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1136 | struct hlist_node *n; |
| 1137 | |
| 1138 | do { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1139 | ctxfree = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1140 | spin_lock(&fl->hlock); |
| 1141 | hlist_for_each_entry_safe(ictx, n, &clst->interrupted, hn) { |
| 1142 | hlist_del_init(&ictx->hn); |
| 1143 | ctxfree = ictx; |
| 1144 | break; |
| 1145 | } |
| 1146 | spin_unlock(&fl->hlock); |
| 1147 | if (ctxfree) |
| 1148 | context_free(ctxfree); |
| 1149 | } while (ctxfree); |
| 1150 | do { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1151 | ctxfree = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1152 | spin_lock(&fl->hlock); |
| 1153 | hlist_for_each_entry_safe(ictx, n, &clst->pending, hn) { |
| 1154 | hlist_del_init(&ictx->hn); |
| 1155 | ctxfree = ictx; |
| 1156 | break; |
| 1157 | } |
| 1158 | spin_unlock(&fl->hlock); |
| 1159 | if (ctxfree) |
| 1160 | context_free(ctxfree); |
| 1161 | } while (ctxfree); |
| 1162 | } |
| 1163 | |
| 1164 | static int fastrpc_file_free(struct fastrpc_file *fl); |
| 1165 | static void fastrpc_file_list_dtor(struct fastrpc_apps *me) |
| 1166 | { |
| 1167 | struct fastrpc_file *fl, *free; |
| 1168 | struct hlist_node *n; |
| 1169 | |
| 1170 | do { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1171 | free = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1172 | spin_lock(&me->hlock); |
| 1173 | hlist_for_each_entry_safe(fl, n, &me->drivers, hn) { |
| 1174 | hlist_del_init(&fl->hn); |
| 1175 | free = fl; |
| 1176 | break; |
| 1177 | } |
| 1178 | spin_unlock(&me->hlock); |
| 1179 | if (free) |
| 1180 | fastrpc_file_free(free); |
| 1181 | } while (free); |
| 1182 | } |
| 1183 | |
| 1184 | static int get_args(uint32_t kernel, struct smq_invoke_ctx *ctx) |
| 1185 | { |
| 1186 | remote_arg64_t *rpra; |
| 1187 | remote_arg_t *lpra = ctx->lpra; |
| 1188 | struct smq_invoke_buf *list; |
| 1189 | struct smq_phy_page *pages, *ipage; |
| 1190 | uint32_t sc = ctx->sc; |
| 1191 | int inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 1192 | int outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1193 | int handles, bufs = inbufs + outbufs; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1194 | uintptr_t args; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 1195 | size_t rlen = 0, copylen = 0, metalen = 0; |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1196 | int i, oix; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1197 | int err = 0; |
| 1198 | int mflags = 0; |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1199 | uint64_t *fdlist; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1200 | uint32_t *crclist; |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1201 | int64_t *perf_counter = getperfcounter(ctx->fl, PERF_COUNT); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1202 | |
| 1203 | /* calculate size of the metadata */ |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1204 | rpra = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1205 | list = smq_invoke_buf_start(rpra, sc); |
| 1206 | pages = smq_phy_page_start(sc, list); |
| 1207 | ipage = pages; |
| 1208 | |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1209 | PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_MAP), |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1210 | for (i = 0; i < bufs; ++i) { |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 1211 | uintptr_t buf = (uintptr_t)lpra[i].buf.pv; |
| 1212 | size_t len = lpra[i].buf.len; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1213 | |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1214 | mutex_lock(&ctx->fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1215 | if (ctx->fds[i] && (ctx->fds[i] != -1)) |
| 1216 | fastrpc_mmap_create(ctx->fl, ctx->fds[i], |
| 1217 | ctx->attrs[i], buf, len, |
| 1218 | mflags, &ctx->maps[i]); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1219 | mutex_unlock(&ctx->fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1220 | ipage += 1; |
| 1221 | } |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1222 | PERF_END); |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1223 | handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1224 | mutex_lock(&ctx->fl->fl_map_mutex); |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1225 | for (i = bufs; i < bufs + handles; i++) { |
| 1226 | VERIFY(err, !fastrpc_mmap_create(ctx->fl, ctx->fds[i], |
| 1227 | FASTRPC_ATTR_NOVA, 0, 0, 0, &ctx->maps[i])); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1228 | if (err) { |
| 1229 | mutex_unlock(&ctx->fl->fl_map_mutex); |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1230 | goto bail; |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1231 | } |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1232 | ipage += 1; |
| 1233 | } |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1234 | mutex_unlock(&ctx->fl->fl_map_mutex); |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 1235 | metalen = copylen = (size_t)&ipage[0] + (sizeof(uint64_t) * M_FDLIST) + |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1236 | (sizeof(uint32_t) * M_CRCLIST); |
| 1237 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1238 | /* calculate len requreed for copying */ |
| 1239 | for (oix = 0; oix < inbufs + outbufs; ++oix) { |
| 1240 | int i = ctx->overps[oix]->raix; |
Sathish Ambley | d209c1e | 2016-12-13 15:27:30 -0800 | [diff] [blame] | 1241 | uintptr_t mstart, mend; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 1242 | size_t len = lpra[i].buf.len; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1243 | |
| 1244 | if (!len) |
| 1245 | continue; |
| 1246 | if (ctx->maps[i]) |
| 1247 | continue; |
| 1248 | if (ctx->overps[oix]->offset == 0) |
| 1249 | copylen = ALIGN(copylen, BALIGN); |
Sathish Ambley | d209c1e | 2016-12-13 15:27:30 -0800 | [diff] [blame] | 1250 | mstart = ctx->overps[oix]->mstart; |
| 1251 | mend = ctx->overps[oix]->mend; |
| 1252 | VERIFY(err, (mend - mstart) <= LONG_MAX); |
| 1253 | if (err) |
| 1254 | goto bail; |
| 1255 | copylen += mend - mstart; |
| 1256 | VERIFY(err, copylen >= 0); |
| 1257 | if (err) |
| 1258 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1259 | } |
| 1260 | ctx->used = copylen; |
| 1261 | |
| 1262 | /* allocate new buffer */ |
| 1263 | if (copylen) { |
| 1264 | VERIFY(err, !fastrpc_buf_alloc(ctx->fl, copylen, &ctx->buf)); |
| 1265 | if (err) |
| 1266 | goto bail; |
| 1267 | } |
Tharun Kumar Merugu | e3361f9 | 2017-06-22 10:45:43 +0530 | [diff] [blame] | 1268 | if (ctx->buf->virt && metalen <= copylen) |
| 1269 | memset(ctx->buf->virt, 0, metalen); |
| 1270 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1271 | /* copy metadata */ |
| 1272 | rpra = ctx->buf->virt; |
| 1273 | ctx->rpra = rpra; |
| 1274 | list = smq_invoke_buf_start(rpra, sc); |
| 1275 | pages = smq_phy_page_start(sc, list); |
| 1276 | ipage = pages; |
| 1277 | args = (uintptr_t)ctx->buf->virt + metalen; |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1278 | for (i = 0; i < bufs + handles; ++i) { |
| 1279 | if (lpra[i].buf.len) |
| 1280 | list[i].num = 1; |
| 1281 | else |
| 1282 | list[i].num = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1283 | list[i].pgidx = ipage - pages; |
| 1284 | ipage++; |
| 1285 | } |
Tharun Kumar Merugu | d1f388a | 2017-10-01 10:51:11 +0530 | [diff] [blame] | 1286 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1287 | /* map ion buffers */ |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1288 | PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_MAP), |
Tharun Kumar Merugu | d1f388a | 2017-10-01 10:51:11 +0530 | [diff] [blame] | 1289 | for (i = 0; rpra && i < inbufs + outbufs; ++i) { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1290 | struct fastrpc_mmap *map = ctx->maps[i]; |
| 1291 | uint64_t buf = ptr_to_uint64(lpra[i].buf.pv); |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 1292 | size_t len = lpra[i].buf.len; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1293 | |
| 1294 | rpra[i].buf.pv = 0; |
| 1295 | rpra[i].buf.len = len; |
| 1296 | if (!len) |
| 1297 | continue; |
| 1298 | if (map) { |
| 1299 | struct vm_area_struct *vma; |
| 1300 | uintptr_t offset; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 1301 | uint64_t num = buf_num_pages(buf, len); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1302 | int idx = list[i].pgidx; |
| 1303 | |
| 1304 | if (map->attr & FASTRPC_ATTR_NOVA) { |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1305 | offset = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1306 | } else { |
| 1307 | down_read(¤t->mm->mmap_sem); |
| 1308 | VERIFY(err, NULL != (vma = find_vma(current->mm, |
| 1309 | map->va))); |
| 1310 | if (err) { |
| 1311 | up_read(¤t->mm->mmap_sem); |
| 1312 | goto bail; |
| 1313 | } |
| 1314 | offset = buf_page_start(buf) - vma->vm_start; |
| 1315 | up_read(¤t->mm->mmap_sem); |
| 1316 | VERIFY(err, offset < (uintptr_t)map->size); |
| 1317 | if (err) |
| 1318 | goto bail; |
| 1319 | } |
| 1320 | pages[idx].addr = map->phys + offset; |
| 1321 | pages[idx].size = num << PAGE_SHIFT; |
| 1322 | } |
| 1323 | rpra[i].buf.pv = buf; |
| 1324 | } |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 1325 | PERF_END); |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1326 | for (i = bufs; i < bufs + handles; ++i) { |
| 1327 | struct fastrpc_mmap *map = ctx->maps[i]; |
| 1328 | |
| 1329 | pages[i].addr = map->phys; |
| 1330 | pages[i].size = map->size; |
| 1331 | } |
| 1332 | fdlist = (uint64_t *)&pages[bufs + handles]; |
| 1333 | for (i = 0; i < M_FDLIST; i++) |
| 1334 | fdlist[i] = 0; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1335 | crclist = (uint32_t *)&fdlist[M_FDLIST]; |
Tharun Kumar Merugu | 2e5f12e | 2017-07-06 12:04:40 +0530 | [diff] [blame] | 1336 | memset(crclist, 0, sizeof(uint32_t)*M_CRCLIST); |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 1337 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1338 | /* copy non ion buffers */ |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1339 | PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_COPY), |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1340 | rlen = copylen - metalen; |
| 1341 | for (oix = 0; oix < inbufs + outbufs; ++oix) { |
| 1342 | int i = ctx->overps[oix]->raix; |
| 1343 | struct fastrpc_mmap *map = ctx->maps[i]; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 1344 | size_t mlen; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1345 | uint64_t buf; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 1346 | size_t len = lpra[i].buf.len; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1347 | |
| 1348 | if (!len) |
| 1349 | continue; |
| 1350 | if (map) |
| 1351 | continue; |
| 1352 | if (ctx->overps[oix]->offset == 0) { |
| 1353 | rlen -= ALIGN(args, BALIGN) - args; |
| 1354 | args = ALIGN(args, BALIGN); |
| 1355 | } |
Sathish Ambley | d209c1e | 2016-12-13 15:27:30 -0800 | [diff] [blame] | 1356 | mlen = ctx->overps[oix]->mend - ctx->overps[oix]->mstart; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1357 | VERIFY(err, rlen >= mlen); |
| 1358 | if (err) |
| 1359 | goto bail; |
| 1360 | rpra[i].buf.pv = (args - ctx->overps[oix]->offset); |
| 1361 | pages[list[i].pgidx].addr = ctx->buf->phys - |
| 1362 | ctx->overps[oix]->offset + |
| 1363 | (copylen - rlen); |
| 1364 | pages[list[i].pgidx].addr = |
| 1365 | buf_page_start(pages[list[i].pgidx].addr); |
| 1366 | buf = rpra[i].buf.pv; |
| 1367 | pages[list[i].pgidx].size = buf_num_pages(buf, len) * PAGE_SIZE; |
| 1368 | if (i < inbufs) { |
| 1369 | K_COPY_FROM_USER(err, kernel, uint64_to_ptr(buf), |
| 1370 | lpra[i].buf.pv, len); |
| 1371 | if (err) |
| 1372 | goto bail; |
| 1373 | } |
| 1374 | args = args + mlen; |
| 1375 | rlen -= mlen; |
| 1376 | } |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 1377 | PERF_END); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1378 | |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1379 | PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_FLUSH), |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1380 | for (oix = 0; oix < inbufs + outbufs; ++oix) { |
| 1381 | int i = ctx->overps[oix]->raix; |
| 1382 | struct fastrpc_mmap *map = ctx->maps[i]; |
| 1383 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1384 | if (map && map->uncached) |
| 1385 | continue; |
Tharun Kumar Merugu | 2e5f12e | 2017-07-06 12:04:40 +0530 | [diff] [blame] | 1386 | if (ctx->fl->sctx->smmu.coherent && |
| 1387 | !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT))) |
| 1388 | continue; |
| 1389 | if (map && (map->attr & FASTRPC_ATTR_COHERENT)) |
| 1390 | continue; |
| 1391 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1392 | if (rpra[i].buf.len && ctx->overps[oix]->mstart) |
| 1393 | dmac_flush_range(uint64_to_ptr(rpra[i].buf.pv), |
| 1394 | uint64_to_ptr(rpra[i].buf.pv + rpra[i].buf.len)); |
| 1395 | } |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 1396 | PERF_END); |
Tharun Kumar Merugu | d1f388a | 2017-10-01 10:51:11 +0530 | [diff] [blame] | 1397 | for (i = bufs; rpra && i < bufs + handles; i++) { |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1398 | rpra[i].dma.fd = ctx->fds[i]; |
| 1399 | rpra[i].dma.len = (uint32_t)lpra[i].buf.len; |
| 1400 | rpra[i].dma.offset = (uint32_t)(uintptr_t)lpra[i].buf.pv; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1401 | } |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 1402 | |
| 1403 | if (!ctx->fl->sctx->smmu.coherent) { |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1404 | PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_FLUSH), |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1405 | dmac_flush_range((char *)rpra, (char *)rpra + ctx->used); |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 1406 | PERF_END); |
| 1407 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1408 | bail: |
| 1409 | return err; |
| 1410 | } |
| 1411 | |
| 1412 | static int put_args(uint32_t kernel, struct smq_invoke_ctx *ctx, |
| 1413 | remote_arg_t *upra) |
| 1414 | { |
| 1415 | uint32_t sc = ctx->sc; |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1416 | struct smq_invoke_buf *list; |
| 1417 | struct smq_phy_page *pages; |
| 1418 | struct fastrpc_mmap *mmap; |
| 1419 | uint64_t *fdlist; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1420 | uint32_t *crclist = NULL; |
| 1421 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1422 | remote_arg64_t *rpra = ctx->rpra; |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1423 | int i, inbufs, outbufs, handles; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1424 | int err = 0; |
| 1425 | |
| 1426 | inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 1427 | outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1428 | handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc); |
| 1429 | list = smq_invoke_buf_start(ctx->rpra, sc); |
| 1430 | pages = smq_phy_page_start(sc, list); |
| 1431 | fdlist = (uint64_t *)(pages + inbufs + outbufs + handles); |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1432 | crclist = (uint32_t *)(fdlist + M_FDLIST); |
| 1433 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1434 | for (i = inbufs; i < inbufs + outbufs; ++i) { |
| 1435 | if (!ctx->maps[i]) { |
| 1436 | K_COPY_TO_USER(err, kernel, |
| 1437 | ctx->lpra[i].buf.pv, |
| 1438 | uint64_to_ptr(rpra[i].buf.pv), |
| 1439 | rpra[i].buf.len); |
| 1440 | if (err) |
| 1441 | goto bail; |
| 1442 | } else { |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1443 | mutex_lock(&ctx->fl->fl_map_mutex); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 1444 | fastrpc_mmap_free(ctx->maps[i], 0); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1445 | mutex_unlock(&ctx->fl->fl_map_mutex); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1446 | ctx->maps[i] = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1447 | } |
| 1448 | } |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1449 | mutex_lock(&ctx->fl->fl_map_mutex); |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1450 | if (inbufs + outbufs + handles) { |
| 1451 | for (i = 0; i < M_FDLIST; i++) { |
| 1452 | if (!fdlist[i]) |
| 1453 | break; |
| 1454 | if (!fastrpc_mmap_find(ctx->fl, (int)fdlist[i], 0, 0, |
Sathish Ambley | ae5ee54 | 2017-01-16 22:24:23 -0800 | [diff] [blame] | 1455 | 0, 0, &mmap)) |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 1456 | fastrpc_mmap_free(mmap, 0); |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1457 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1458 | } |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1459 | mutex_unlock(&ctx->fl->fl_map_mutex); |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1460 | if (ctx->crc && crclist && rpra) |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1461 | K_COPY_TO_USER(err, kernel, ctx->crc, |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1462 | crclist, M_CRCLIST*sizeof(uint32_t)); |
| 1463 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1464 | bail: |
| 1465 | return err; |
| 1466 | } |
| 1467 | |
| 1468 | static void inv_args_pre(struct smq_invoke_ctx *ctx) |
| 1469 | { |
| 1470 | int i, inbufs, outbufs; |
| 1471 | uint32_t sc = ctx->sc; |
| 1472 | remote_arg64_t *rpra = ctx->rpra; |
| 1473 | uintptr_t end; |
| 1474 | |
| 1475 | inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 1476 | outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
| 1477 | for (i = inbufs; i < inbufs + outbufs; ++i) { |
| 1478 | struct fastrpc_mmap *map = ctx->maps[i]; |
| 1479 | |
| 1480 | if (map && map->uncached) |
| 1481 | continue; |
| 1482 | if (!rpra[i].buf.len) |
| 1483 | continue; |
Tharun Kumar Merugu | 2e5f12e | 2017-07-06 12:04:40 +0530 | [diff] [blame] | 1484 | if (ctx->fl->sctx->smmu.coherent && |
| 1485 | !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT))) |
| 1486 | continue; |
| 1487 | if (map && (map->attr & FASTRPC_ATTR_COHERENT)) |
| 1488 | continue; |
| 1489 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1490 | if (buf_page_start(ptr_to_uint64((void *)rpra)) == |
| 1491 | buf_page_start(rpra[i].buf.pv)) |
| 1492 | continue; |
| 1493 | if (!IS_CACHE_ALIGNED((uintptr_t)uint64_to_ptr(rpra[i].buf.pv))) |
| 1494 | dmac_flush_range(uint64_to_ptr(rpra[i].buf.pv), |
| 1495 | (char *)(uint64_to_ptr(rpra[i].buf.pv + 1))); |
| 1496 | end = (uintptr_t)uint64_to_ptr(rpra[i].buf.pv + |
| 1497 | rpra[i].buf.len); |
| 1498 | if (!IS_CACHE_ALIGNED(end)) |
| 1499 | dmac_flush_range((char *)end, |
| 1500 | (char *)end + 1); |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | static void inv_args(struct smq_invoke_ctx *ctx) |
| 1505 | { |
| 1506 | int i, inbufs, outbufs; |
| 1507 | uint32_t sc = ctx->sc; |
| 1508 | remote_arg64_t *rpra = ctx->rpra; |
| 1509 | int used = ctx->used; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1510 | |
| 1511 | inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 1512 | outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
| 1513 | for (i = inbufs; i < inbufs + outbufs; ++i) { |
| 1514 | struct fastrpc_mmap *map = ctx->maps[i]; |
| 1515 | |
| 1516 | if (map && map->uncached) |
| 1517 | continue; |
| 1518 | if (!rpra[i].buf.len) |
| 1519 | continue; |
Tharun Kumar Merugu | 2e5f12e | 2017-07-06 12:04:40 +0530 | [diff] [blame] | 1520 | if (ctx->fl->sctx->smmu.coherent && |
| 1521 | !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT))) |
| 1522 | continue; |
| 1523 | if (map && (map->attr & FASTRPC_ATTR_COHERENT)) |
| 1524 | continue; |
| 1525 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1526 | if (buf_page_start(ptr_to_uint64((void *)rpra)) == |
| 1527 | buf_page_start(rpra[i].buf.pv)) { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1528 | continue; |
| 1529 | } |
| 1530 | if (map && map->handle) |
| 1531 | msm_ion_do_cache_op(ctx->fl->apps->client, map->handle, |
| 1532 | (char *)uint64_to_ptr(rpra[i].buf.pv), |
| 1533 | rpra[i].buf.len, ION_IOC_INV_CACHES); |
| 1534 | else |
| 1535 | dmac_inv_range((char *)uint64_to_ptr(rpra[i].buf.pv), |
| 1536 | (char *)uint64_to_ptr(rpra[i].buf.pv |
| 1537 | + rpra[i].buf.len)); |
| 1538 | } |
| 1539 | |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1540 | if (rpra) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1541 | dmac_inv_range(rpra, (char *)rpra + used); |
| 1542 | } |
| 1543 | |
| 1544 | static int fastrpc_invoke_send(struct smq_invoke_ctx *ctx, |
| 1545 | uint32_t kernel, uint32_t handle) |
| 1546 | { |
| 1547 | struct smq_msg *msg = &ctx->msg; |
| 1548 | struct fastrpc_file *fl = ctx->fl; |
| 1549 | struct fastrpc_channel_ctx *channel_ctx = &fl->apps->channel[fl->cid]; |
| 1550 | int err = 0; |
| 1551 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1552 | VERIFY(err, NULL != channel_ctx->chan); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1553 | if (err) |
| 1554 | goto bail; |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 1555 | msg->pid = fl->tgid; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1556 | msg->tid = current->pid; |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 1557 | if (fl->sessionid) |
| 1558 | msg->tid |= (1 << SESSION_ID_INDEX); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1559 | if (kernel) |
| 1560 | msg->pid = 0; |
| 1561 | msg->invoke.header.ctx = ptr_to_uint64(ctx) | fl->pd; |
| 1562 | msg->invoke.header.handle = handle; |
| 1563 | msg->invoke.header.sc = ctx->sc; |
| 1564 | msg->invoke.page.addr = ctx->buf ? ctx->buf->phys : 0; |
| 1565 | msg->invoke.page.size = buf_page_size(ctx->used); |
| 1566 | |
| 1567 | if (fl->ssrcount != channel_ctx->ssrcount) { |
| 1568 | err = -ECONNRESET; |
| 1569 | goto bail; |
| 1570 | } |
| 1571 | VERIFY(err, channel_ctx->link.port_state == |
| 1572 | FASTRPC_LINK_CONNECTED); |
| 1573 | if (err) |
| 1574 | goto bail; |
| 1575 | err = glink_tx(channel_ctx->chan, |
| 1576 | (void *)&fl->apps->channel[fl->cid], msg, sizeof(*msg), |
| 1577 | GLINK_TX_REQ_INTENT); |
| 1578 | bail: |
| 1579 | return err; |
| 1580 | } |
| 1581 | |
| 1582 | static void fastrpc_init(struct fastrpc_apps *me) |
| 1583 | { |
| 1584 | int i; |
| 1585 | |
| 1586 | INIT_HLIST_HEAD(&me->drivers); |
Tharun Kumar Merugu | bcd6fbf | 2018-01-04 17:49:34 +0530 | [diff] [blame] | 1587 | INIT_HLIST_HEAD(&me->maps); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1588 | spin_lock_init(&me->hlock); |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 1589 | mutex_init(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1590 | me->channel = &gcinfo[0]; |
| 1591 | for (i = 0; i < NUM_CHANNELS; i++) { |
| 1592 | init_completion(&me->channel[i].work); |
Tharun Kumar Merugu | 53a8ec9 | 2017-07-14 15:52:49 +0530 | [diff] [blame] | 1593 | init_completion(&me->channel[i].workport); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1594 | me->channel[i].sesscount = 0; |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | static int fastrpc_release_current_dsp_process(struct fastrpc_file *fl); |
| 1599 | |
| 1600 | static int fastrpc_internal_invoke(struct fastrpc_file *fl, uint32_t mode, |
| 1601 | uint32_t kernel, |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1602 | struct fastrpc_ioctl_invoke_crc *inv) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1603 | { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1604 | struct smq_invoke_ctx *ctx = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1605 | struct fastrpc_ioctl_invoke *invoke = &inv->inv; |
| 1606 | int cid = fl->cid; |
| 1607 | int interrupted = 0; |
| 1608 | int err = 0; |
Maria Yu | 757199c | 2017-09-22 16:05:49 +0800 | [diff] [blame] | 1609 | struct timespec invoket = {0}; |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1610 | int64_t *perf_counter = getperfcounter(fl, PERF_COUNT); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1611 | |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 1612 | if (fl->profile) |
| 1613 | getnstimeofday(&invoket); |
Tharun Kumar Merugu | e3edf3e | 2017-07-27 12:34:07 +0530 | [diff] [blame] | 1614 | |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 1615 | |
Tharun Kumar Merugu | e3edf3e | 2017-07-27 12:34:07 +0530 | [diff] [blame] | 1616 | VERIFY(err, fl->sctx != NULL); |
| 1617 | if (err) |
| 1618 | goto bail; |
| 1619 | VERIFY(err, fl->cid >= 0 && fl->cid < NUM_CHANNELS); |
| 1620 | if (err) |
| 1621 | goto bail; |
| 1622 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1623 | if (!kernel) { |
| 1624 | VERIFY(err, 0 == context_restore_interrupted(fl, inv, |
| 1625 | &ctx)); |
| 1626 | if (err) |
| 1627 | goto bail; |
| 1628 | if (fl->sctx->smmu.faults) |
| 1629 | err = FASTRPC_ENOSUCH; |
| 1630 | if (err) |
| 1631 | goto bail; |
| 1632 | if (ctx) |
| 1633 | goto wait; |
| 1634 | } |
| 1635 | |
| 1636 | VERIFY(err, 0 == context_alloc(fl, kernel, inv, &ctx)); |
| 1637 | if (err) |
| 1638 | goto bail; |
| 1639 | |
| 1640 | if (REMOTE_SCALARS_LENGTH(ctx->sc)) { |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1641 | PERF(fl->profile, GET_COUNTER(perf_counter, PERF_GETARGS), |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1642 | VERIFY(err, 0 == get_args(kernel, ctx)); |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 1643 | PERF_END); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1644 | if (err) |
| 1645 | goto bail; |
| 1646 | } |
| 1647 | |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1648 | if (!fl->sctx->smmu.coherent) { |
| 1649 | PERF(fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS), |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1650 | inv_args_pre(ctx); |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1651 | PERF_END); |
| 1652 | } |
| 1653 | |
| 1654 | PERF(fl->profile, GET_COUNTER(perf_counter, PERF_LINK), |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1655 | VERIFY(err, 0 == fastrpc_invoke_send(ctx, kernel, invoke->handle)); |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 1656 | PERF_END); |
| 1657 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1658 | if (err) |
| 1659 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1660 | wait: |
| 1661 | if (kernel) |
| 1662 | wait_for_completion(&ctx->work); |
| 1663 | else { |
| 1664 | interrupted = wait_for_completion_interruptible(&ctx->work); |
| 1665 | VERIFY(err, 0 == (err = interrupted)); |
| 1666 | if (err) |
| 1667 | goto bail; |
| 1668 | } |
Sathish Ambley | c432b50 | 2017-06-05 12:03:42 -0700 | [diff] [blame] | 1669 | |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1670 | PERF(fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS), |
Sathish Ambley | c432b50 | 2017-06-05 12:03:42 -0700 | [diff] [blame] | 1671 | if (!fl->sctx->smmu.coherent) |
| 1672 | inv_args(ctx); |
| 1673 | PERF_END); |
| 1674 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1675 | VERIFY(err, 0 == (err = ctx->retval)); |
| 1676 | if (err) |
| 1677 | goto bail; |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 1678 | |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1679 | PERF(fl->profile, GET_COUNTER(perf_counter, PERF_PUTARGS), |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1680 | VERIFY(err, 0 == put_args(kernel, ctx, invoke->pra)); |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 1681 | PERF_END); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1682 | if (err) |
| 1683 | goto bail; |
| 1684 | bail: |
| 1685 | if (ctx && interrupted == -ERESTARTSYS) |
| 1686 | context_save_interrupted(ctx); |
| 1687 | else if (ctx) |
| 1688 | context_free(ctx); |
| 1689 | if (fl->ssrcount != fl->apps->channel[cid].ssrcount) |
| 1690 | err = ECONNRESET; |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 1691 | |
| 1692 | if (fl->profile && !interrupted) { |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1693 | if (invoke->handle != FASTRPC_STATIC_HANDLE_LISTENER) { |
| 1694 | int64_t *count = GET_COUNTER(perf_counter, PERF_INVOKE); |
| 1695 | |
| 1696 | if (count) |
| 1697 | *count += getnstimediff(&invoket); |
| 1698 | } |
| 1699 | if (invoke->handle > FASTRPC_STATIC_HANDLE_MAX) { |
| 1700 | int64_t *count = GET_COUNTER(perf_counter, PERF_COUNT); |
| 1701 | |
| 1702 | if (count) |
| 1703 | *count = *count+1; |
| 1704 | } |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 1705 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1706 | return err; |
| 1707 | } |
| 1708 | |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 1709 | static int fastrpc_channel_open(struct fastrpc_file *fl); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1710 | static int fastrpc_init_process(struct fastrpc_file *fl, |
Sathish Ambley | d6300c3 | 2017-01-18 09:50:43 -0800 | [diff] [blame] | 1711 | struct fastrpc_ioctl_init_attrs *uproc) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1712 | { |
| 1713 | int err = 0; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1714 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1715 | struct fastrpc_ioctl_invoke_crc ioctl; |
Sathish Ambley | d6300c3 | 2017-01-18 09:50:43 -0800 | [diff] [blame] | 1716 | struct fastrpc_ioctl_init *init = &uproc->init; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1717 | struct smq_phy_page pages[1]; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1718 | struct fastrpc_mmap *file = NULL, *mem = NULL; |
| 1719 | char *proc_name = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1720 | |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 1721 | VERIFY(err, 0 == (err = fastrpc_channel_open(fl))); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 1722 | if (err) |
| 1723 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1724 | if (init->flags == FASTRPC_INIT_ATTACH) { |
| 1725 | remote_arg_t ra[1]; |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 1726 | int tgid = fl->tgid; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1727 | |
| 1728 | ra[0].buf.pv = (void *)&tgid; |
| 1729 | ra[0].buf.len = sizeof(tgid); |
| 1730 | ioctl.inv.handle = 1; |
| 1731 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(0, 1, 0); |
| 1732 | ioctl.inv.pra = ra; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1733 | ioctl.fds = NULL; |
| 1734 | ioctl.attrs = NULL; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1735 | ioctl.crc = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1736 | fl->pd = 0; |
| 1737 | VERIFY(err, !(err = fastrpc_internal_invoke(fl, |
| 1738 | FASTRPC_MODE_PARALLEL, 1, &ioctl))); |
| 1739 | if (err) |
| 1740 | goto bail; |
| 1741 | } else if (init->flags == FASTRPC_INIT_CREATE) { |
Sathish Ambley | d6300c3 | 2017-01-18 09:50:43 -0800 | [diff] [blame] | 1742 | remote_arg_t ra[6]; |
| 1743 | int fds[6]; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1744 | int mflags = 0; |
| 1745 | struct { |
| 1746 | int pgid; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 1747 | unsigned int namelen; |
| 1748 | unsigned int filelen; |
| 1749 | unsigned int pageslen; |
Sathish Ambley | d6300c3 | 2017-01-18 09:50:43 -0800 | [diff] [blame] | 1750 | int attrs; |
| 1751 | int siglen; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1752 | } inbuf; |
| 1753 | |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 1754 | inbuf.pgid = fl->tgid; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1755 | inbuf.namelen = strlen(current->comm) + 1; |
| 1756 | inbuf.filelen = init->filelen; |
| 1757 | fl->pd = 1; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 1758 | |
Tharun Kumar Merugu | df85289 | 2017-12-07 16:27:37 +0530 | [diff] [blame] | 1759 | VERIFY(err, access_ok(0, (void __user *)init->file, |
| 1760 | init->filelen)); |
| 1761 | if (err) |
| 1762 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1763 | if (init->filelen) { |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1764 | mutex_lock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1765 | VERIFY(err, !fastrpc_mmap_create(fl, init->filefd, 0, |
| 1766 | init->file, init->filelen, mflags, &file)); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1767 | mutex_unlock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1768 | if (err) |
| 1769 | goto bail; |
| 1770 | } |
| 1771 | inbuf.pageslen = 1; |
Tharun Kumar Merugu | df85289 | 2017-12-07 16:27:37 +0530 | [diff] [blame] | 1772 | VERIFY(err, access_ok(1, (void __user *)init->mem, |
| 1773 | init->memlen)); |
| 1774 | if (err) |
| 1775 | goto bail; |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1776 | mutex_lock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1777 | VERIFY(err, !fastrpc_mmap_create(fl, init->memfd, 0, |
| 1778 | init->mem, init->memlen, mflags, &mem)); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1779 | mutex_unlock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1780 | if (err) |
| 1781 | goto bail; |
| 1782 | inbuf.pageslen = 1; |
| 1783 | ra[0].buf.pv = (void *)&inbuf; |
| 1784 | ra[0].buf.len = sizeof(inbuf); |
| 1785 | fds[0] = 0; |
| 1786 | |
| 1787 | ra[1].buf.pv = (void *)current->comm; |
| 1788 | ra[1].buf.len = inbuf.namelen; |
| 1789 | fds[1] = 0; |
| 1790 | |
| 1791 | ra[2].buf.pv = (void *)init->file; |
| 1792 | ra[2].buf.len = inbuf.filelen; |
| 1793 | fds[2] = init->filefd; |
| 1794 | |
| 1795 | pages[0].addr = mem->phys; |
| 1796 | pages[0].size = mem->size; |
| 1797 | ra[3].buf.pv = (void *)pages; |
| 1798 | ra[3].buf.len = 1 * sizeof(*pages); |
| 1799 | fds[3] = 0; |
| 1800 | |
Sathish Ambley | d6300c3 | 2017-01-18 09:50:43 -0800 | [diff] [blame] | 1801 | inbuf.attrs = uproc->attrs; |
| 1802 | ra[4].buf.pv = (void *)&(inbuf.attrs); |
| 1803 | ra[4].buf.len = sizeof(inbuf.attrs); |
| 1804 | fds[4] = 0; |
| 1805 | |
| 1806 | inbuf.siglen = uproc->siglen; |
| 1807 | ra[5].buf.pv = (void *)&(inbuf.siglen); |
| 1808 | ra[5].buf.len = sizeof(inbuf.siglen); |
| 1809 | fds[5] = 0; |
| 1810 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1811 | ioctl.inv.handle = 1; |
| 1812 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(6, 4, 0); |
Sathish Ambley | d6300c3 | 2017-01-18 09:50:43 -0800 | [diff] [blame] | 1813 | if (uproc->attrs) |
| 1814 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(7, 6, 0); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1815 | ioctl.inv.pra = ra; |
| 1816 | ioctl.fds = fds; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1817 | ioctl.attrs = NULL; |
| 1818 | ioctl.crc = NULL; |
| 1819 | VERIFY(err, !(err = fastrpc_internal_invoke(fl, |
| 1820 | FASTRPC_MODE_PARALLEL, 1, &ioctl))); |
| 1821 | if (err) |
| 1822 | goto bail; |
| 1823 | } else if (init->flags == FASTRPC_INIT_CREATE_STATIC) { |
| 1824 | remote_arg_t ra[3]; |
| 1825 | uint64_t phys = 0; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 1826 | size_t size = 0; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1827 | int fds[3]; |
| 1828 | struct { |
| 1829 | int pgid; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 1830 | unsigned int namelen; |
| 1831 | unsigned int pageslen; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1832 | } inbuf; |
| 1833 | |
| 1834 | if (!init->filelen) |
| 1835 | goto bail; |
| 1836 | |
| 1837 | proc_name = kzalloc(init->filelen, GFP_KERNEL); |
| 1838 | VERIFY(err, !IS_ERR_OR_NULL(proc_name)); |
| 1839 | if (err) |
| 1840 | goto bail; |
| 1841 | VERIFY(err, 0 == copy_from_user((void *)proc_name, |
| 1842 | (void __user *)init->file, init->filelen)); |
| 1843 | if (err) |
| 1844 | goto bail; |
| 1845 | |
| 1846 | inbuf.pgid = current->tgid; |
c_mtharu | 81a0aa7 | 2017-11-07 16:13:21 +0530 | [diff] [blame] | 1847 | inbuf.namelen = init->filelen; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1848 | inbuf.pageslen = 0; |
| 1849 | if (!me->staticpd_flags) { |
| 1850 | inbuf.pageslen = 1; |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1851 | mutex_lock(&fl->fl_map_mutex); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1852 | VERIFY(err, !fastrpc_mmap_create(fl, -1, 0, init->mem, |
| 1853 | init->memlen, ADSP_MMAP_REMOTE_HEAP_ADDR, |
| 1854 | &mem)); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1855 | mutex_unlock(&fl->fl_map_mutex); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1856 | if (err) |
| 1857 | goto bail; |
| 1858 | phys = mem->phys; |
| 1859 | size = mem->size; |
| 1860 | VERIFY(err, !hyp_assign_phys(phys, (uint64_t)size, |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 1861 | hlosvm, 1, me->channel[fl->cid].rhvm.vmid, |
| 1862 | me->channel[fl->cid].rhvm.vmperm, |
| 1863 | me->channel[fl->cid].rhvm.vmcount)); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1864 | if (err) { |
| 1865 | pr_err("ADSPRPC: hyp_assign_phys fail err %d", |
| 1866 | err); |
| 1867 | pr_err("map->phys %llx, map->size %d\n", |
| 1868 | phys, (int)size); |
| 1869 | goto bail; |
| 1870 | } |
| 1871 | me->staticpd_flags = 1; |
| 1872 | } |
| 1873 | |
| 1874 | ra[0].buf.pv = (void *)&inbuf; |
| 1875 | ra[0].buf.len = sizeof(inbuf); |
| 1876 | fds[0] = 0; |
| 1877 | |
| 1878 | ra[1].buf.pv = (void *)proc_name; |
| 1879 | ra[1].buf.len = inbuf.namelen; |
| 1880 | fds[1] = 0; |
| 1881 | |
| 1882 | pages[0].addr = phys; |
| 1883 | pages[0].size = size; |
| 1884 | |
| 1885 | ra[2].buf.pv = (void *)pages; |
| 1886 | ra[2].buf.len = sizeof(*pages); |
| 1887 | fds[2] = 0; |
| 1888 | ioctl.inv.handle = 1; |
| 1889 | |
| 1890 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(8, 3, 0); |
| 1891 | ioctl.inv.pra = ra; |
| 1892 | ioctl.fds = NULL; |
| 1893 | ioctl.attrs = NULL; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1894 | ioctl.crc = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1895 | VERIFY(err, !(err = fastrpc_internal_invoke(fl, |
| 1896 | FASTRPC_MODE_PARALLEL, 1, &ioctl))); |
| 1897 | if (err) |
| 1898 | goto bail; |
| 1899 | } else { |
| 1900 | err = -ENOTTY; |
| 1901 | } |
| 1902 | bail: |
c_mtharu | d91205a | 2017-11-07 16:01:06 +0530 | [diff] [blame] | 1903 | kfree(proc_name); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1904 | if (err && (init->flags == FASTRPC_INIT_CREATE_STATIC)) |
| 1905 | me->staticpd_flags = 0; |
| 1906 | if (mem && err) { |
| 1907 | if (mem->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) |
| 1908 | hyp_assign_phys(mem->phys, (uint64_t)mem->size, |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 1909 | me->channel[fl->cid].rhvm.vmid, |
| 1910 | me->channel[fl->cid].rhvm.vmcount, |
| 1911 | hlosvm, hlosvmperm, 1); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1912 | mutex_lock(&fl->fl_map_mutex); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 1913 | fastrpc_mmap_free(mem, 0); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1914 | mutex_unlock(&fl->fl_map_mutex); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1915 | } |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1916 | if (file) { |
| 1917 | mutex_lock(&fl->fl_map_mutex); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 1918 | fastrpc_mmap_free(file, 0); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 1919 | mutex_unlock(&fl->fl_map_mutex); |
| 1920 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1921 | return err; |
| 1922 | } |
| 1923 | |
| 1924 | static int fastrpc_release_current_dsp_process(struct fastrpc_file *fl) |
| 1925 | { |
| 1926 | int err = 0; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1927 | struct fastrpc_ioctl_invoke_crc ioctl; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1928 | remote_arg_t ra[1]; |
| 1929 | int tgid = 0; |
| 1930 | |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 1931 | VERIFY(err, fl->cid >= 0 && fl->cid < NUM_CHANNELS); |
| 1932 | if (err) |
| 1933 | goto bail; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1934 | VERIFY(err, fl->apps->channel[fl->cid].chan != NULL); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1935 | if (err) |
| 1936 | goto bail; |
| 1937 | tgid = fl->tgid; |
| 1938 | ra[0].buf.pv = (void *)&tgid; |
| 1939 | ra[0].buf.len = sizeof(tgid); |
| 1940 | ioctl.inv.handle = 1; |
| 1941 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(1, 1, 0); |
| 1942 | ioctl.inv.pra = ra; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1943 | ioctl.fds = NULL; |
| 1944 | ioctl.attrs = NULL; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1945 | ioctl.crc = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1946 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, |
| 1947 | FASTRPC_MODE_PARALLEL, 1, &ioctl))); |
| 1948 | bail: |
| 1949 | return err; |
| 1950 | } |
| 1951 | |
| 1952 | static int fastrpc_mmap_on_dsp(struct fastrpc_file *fl, uint32_t flags, |
| 1953 | struct fastrpc_mmap *map) |
| 1954 | { |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1955 | struct fastrpc_ioctl_invoke_crc ioctl; |
c_mtharu | 63ffc01 | 2017-11-16 15:26:56 +0530 | [diff] [blame] | 1956 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1957 | struct smq_phy_page page; |
| 1958 | int num = 1; |
| 1959 | remote_arg_t ra[3]; |
| 1960 | int err = 0; |
| 1961 | struct { |
| 1962 | int pid; |
| 1963 | uint32_t flags; |
| 1964 | uintptr_t vaddrin; |
| 1965 | int num; |
| 1966 | } inargs; |
| 1967 | struct { |
| 1968 | uintptr_t vaddrout; |
| 1969 | } routargs; |
| 1970 | |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 1971 | inargs.pid = fl->tgid; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1972 | inargs.vaddrin = (uintptr_t)map->va; |
| 1973 | inargs.flags = flags; |
| 1974 | inargs.num = fl->apps->compat ? num * sizeof(page) : num; |
| 1975 | ra[0].buf.pv = (void *)&inargs; |
| 1976 | ra[0].buf.len = sizeof(inargs); |
| 1977 | page.addr = map->phys; |
| 1978 | page.size = map->size; |
| 1979 | ra[1].buf.pv = (void *)&page; |
| 1980 | ra[1].buf.len = num * sizeof(page); |
| 1981 | |
| 1982 | ra[2].buf.pv = (void *)&routargs; |
| 1983 | ra[2].buf.len = sizeof(routargs); |
| 1984 | |
| 1985 | ioctl.inv.handle = 1; |
| 1986 | if (fl->apps->compat) |
| 1987 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(4, 2, 1); |
| 1988 | else |
| 1989 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(2, 2, 1); |
| 1990 | ioctl.inv.pra = ra; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1991 | ioctl.fds = NULL; |
| 1992 | ioctl.attrs = NULL; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1993 | ioctl.crc = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1994 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, |
| 1995 | FASTRPC_MODE_PARALLEL, 1, &ioctl))); |
| 1996 | map->raddr = (uintptr_t)routargs.vaddrout; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1997 | if (err) |
| 1998 | goto bail; |
| 1999 | if (flags == ADSP_MMAP_HEAP_ADDR) { |
| 2000 | struct scm_desc desc = {0}; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2001 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2002 | desc.args[0] = TZ_PIL_AUTH_QDSP6_PROC; |
| 2003 | desc.args[1] = map->phys; |
| 2004 | desc.args[2] = map->size; |
| 2005 | desc.arginfo = SCM_ARGS(3); |
| 2006 | err = scm_call2(SCM_SIP_FNID(SCM_SVC_PIL, |
| 2007 | TZ_PIL_PROTECT_MEM_SUBSYS_ID), &desc); |
| 2008 | } else if (flags == ADSP_MMAP_REMOTE_HEAP_ADDR) { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2009 | VERIFY(err, !hyp_assign_phys(map->phys, (uint64_t)map->size, |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 2010 | hlosvm, 1, me->channel[fl->cid].rhvm.vmid, |
| 2011 | me->channel[fl->cid].rhvm.vmperm, |
| 2012 | me->channel[fl->cid].rhvm.vmcount)); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2013 | if (err) |
| 2014 | goto bail; |
| 2015 | } |
| 2016 | bail: |
| 2017 | return err; |
| 2018 | } |
| 2019 | |
| 2020 | static int fastrpc_munmap_on_dsp_rh(struct fastrpc_file *fl, |
| 2021 | struct fastrpc_mmap *map) |
| 2022 | { |
| 2023 | int err = 0; |
c_mtharu | 63ffc01 | 2017-11-16 15:26:56 +0530 | [diff] [blame] | 2024 | struct fastrpc_apps *me = &gfa; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2025 | int destVM[1] = {VMID_HLOS}; |
| 2026 | int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC}; |
| 2027 | |
| 2028 | if (map->flags == ADSP_MMAP_HEAP_ADDR) { |
| 2029 | struct fastrpc_ioctl_invoke_crc ioctl; |
| 2030 | struct scm_desc desc = {0}; |
| 2031 | remote_arg_t ra[1]; |
| 2032 | int err = 0; |
| 2033 | struct { |
| 2034 | uint8_t skey; |
| 2035 | } routargs; |
| 2036 | |
| 2037 | ra[0].buf.pv = (void *)&routargs; |
| 2038 | ra[0].buf.len = sizeof(routargs); |
| 2039 | |
| 2040 | ioctl.inv.handle = 1; |
| 2041 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(7, 0, 1); |
| 2042 | ioctl.inv.pra = ra; |
| 2043 | ioctl.fds = NULL; |
| 2044 | ioctl.attrs = NULL; |
| 2045 | ioctl.crc = NULL; |
| 2046 | if (fl == NULL) |
| 2047 | goto bail; |
| 2048 | |
| 2049 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, |
| 2050 | FASTRPC_MODE_PARALLEL, 1, &ioctl))); |
| 2051 | if (err) |
| 2052 | goto bail; |
| 2053 | desc.args[0] = TZ_PIL_AUTH_QDSP6_PROC; |
| 2054 | desc.args[1] = map->phys; |
| 2055 | desc.args[2] = map->size; |
| 2056 | desc.args[3] = routargs.skey; |
| 2057 | desc.arginfo = SCM_ARGS(4); |
| 2058 | err = scm_call2(SCM_SIP_FNID(SCM_SVC_PIL, |
| 2059 | TZ_PIL_CLEAR_PROTECT_MEM_SUBSYS_ID), &desc); |
| 2060 | } else if (map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) { |
| 2061 | VERIFY(err, !hyp_assign_phys(map->phys, (uint64_t)map->size, |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 2062 | me->channel[fl->cid].rhvm.vmid, |
| 2063 | me->channel[fl->cid].rhvm.vmcount, |
| 2064 | destVM, destVMperm, 1)); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2065 | if (err) |
| 2066 | goto bail; |
| 2067 | } |
| 2068 | |
| 2069 | bail: |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2070 | return err; |
| 2071 | } |
| 2072 | |
| 2073 | static int fastrpc_munmap_on_dsp(struct fastrpc_file *fl, |
| 2074 | struct fastrpc_mmap *map) |
| 2075 | { |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 2076 | struct fastrpc_ioctl_invoke_crc ioctl; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2077 | remote_arg_t ra[1]; |
| 2078 | int err = 0; |
| 2079 | struct { |
| 2080 | int pid; |
| 2081 | uintptr_t vaddrout; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 2082 | size_t size; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2083 | } inargs; |
| 2084 | |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 2085 | inargs.pid = fl->tgid; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2086 | inargs.size = map->size; |
| 2087 | inargs.vaddrout = map->raddr; |
| 2088 | ra[0].buf.pv = (void *)&inargs; |
| 2089 | ra[0].buf.len = sizeof(inargs); |
| 2090 | |
| 2091 | ioctl.inv.handle = 1; |
| 2092 | if (fl->apps->compat) |
| 2093 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(5, 1, 0); |
| 2094 | else |
| 2095 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(3, 1, 0); |
| 2096 | ioctl.inv.pra = ra; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2097 | ioctl.fds = NULL; |
| 2098 | ioctl.attrs = NULL; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 2099 | ioctl.crc = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2100 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, |
| 2101 | FASTRPC_MODE_PARALLEL, 1, &ioctl))); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2102 | if (err) |
| 2103 | goto bail; |
| 2104 | if (map->flags == ADSP_MMAP_HEAP_ADDR || |
| 2105 | map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) { |
| 2106 | VERIFY(err, !fastrpc_munmap_on_dsp_rh(fl, map)); |
| 2107 | if (err) |
| 2108 | goto bail; |
| 2109 | } |
| 2110 | bail: |
| 2111 | return err; |
| 2112 | } |
| 2113 | |
| 2114 | static int fastrpc_mmap_remove_ssr(struct fastrpc_file *fl) |
| 2115 | { |
| 2116 | struct fastrpc_mmap *match = NULL, *map = NULL; |
| 2117 | struct hlist_node *n = NULL; |
| 2118 | int err = 0, ret = 0; |
| 2119 | struct fastrpc_apps *me = &gfa; |
| 2120 | struct ramdump_segment *ramdump_segments_rh = NULL; |
| 2121 | |
| 2122 | do { |
| 2123 | match = NULL; |
| 2124 | spin_lock(&me->hlock); |
| 2125 | hlist_for_each_entry_safe(map, n, &me->maps, hn) { |
| 2126 | match = map; |
| 2127 | hlist_del_init(&map->hn); |
| 2128 | break; |
| 2129 | } |
| 2130 | spin_unlock(&me->hlock); |
| 2131 | |
| 2132 | if (match) { |
| 2133 | VERIFY(err, !fastrpc_munmap_on_dsp_rh(fl, match)); |
| 2134 | if (err) |
| 2135 | goto bail; |
| 2136 | if (me->channel[0].ramdumpenabled) { |
| 2137 | ramdump_segments_rh = kcalloc(1, |
| 2138 | sizeof(struct ramdump_segment), GFP_KERNEL); |
| 2139 | if (ramdump_segments_rh) { |
| 2140 | ramdump_segments_rh->address = |
| 2141 | match->phys; |
| 2142 | ramdump_segments_rh->size = match->size; |
| 2143 | ret = do_elf_ramdump( |
| 2144 | me->channel[0].remoteheap_ramdump_dev, |
| 2145 | ramdump_segments_rh, 1); |
| 2146 | if (ret < 0) |
| 2147 | pr_err("ADSPRPC: unable to dump heap"); |
| 2148 | kfree(ramdump_segments_rh); |
| 2149 | } |
| 2150 | } |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2151 | fastrpc_mmap_free(match, 0); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2152 | } |
| 2153 | } while (match); |
| 2154 | bail: |
| 2155 | if (err && match) |
| 2156 | fastrpc_mmap_add(match); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2157 | return err; |
| 2158 | } |
| 2159 | |
| 2160 | static int fastrpc_mmap_remove(struct fastrpc_file *fl, uintptr_t va, |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 2161 | size_t len, struct fastrpc_mmap **ppmap); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2162 | |
| 2163 | static void fastrpc_mmap_add(struct fastrpc_mmap *map); |
| 2164 | |
| 2165 | static int fastrpc_internal_munmap(struct fastrpc_file *fl, |
| 2166 | struct fastrpc_ioctl_munmap *ud) |
| 2167 | { |
| 2168 | int err = 0; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2169 | struct fastrpc_mmap *map = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2170 | |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 2171 | mutex_lock(&fl->map_mutex); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 2172 | mutex_lock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2173 | VERIFY(err, !fastrpc_mmap_remove(fl, ud->vaddrout, ud->size, &map)); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 2174 | mutex_unlock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2175 | if (err) |
| 2176 | goto bail; |
| 2177 | VERIFY(err, !fastrpc_munmap_on_dsp(fl, map)); |
| 2178 | if (err) |
| 2179 | goto bail; |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 2180 | mutex_lock(&fl->fl_map_mutex); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2181 | fastrpc_mmap_free(map, 0); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 2182 | mutex_unlock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2183 | bail: |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 2184 | if (err && map) { |
| 2185 | mutex_lock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2186 | fastrpc_mmap_add(map); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 2187 | mutex_unlock(&fl->fl_map_mutex); |
| 2188 | } |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 2189 | mutex_unlock(&fl->map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2190 | return err; |
| 2191 | } |
| 2192 | |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2193 | static int fastrpc_internal_munmap_fd(struct fastrpc_file *fl, |
| 2194 | struct fastrpc_ioctl_munmap_fd *ud) { |
| 2195 | int err = 0; |
| 2196 | struct fastrpc_mmap *map = NULL; |
| 2197 | |
| 2198 | VERIFY(err, (fl && ud)); |
| 2199 | if (err) |
| 2200 | goto bail; |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 2201 | mutex_lock(&fl->fl_map_mutex); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2202 | if (!fastrpc_mmap_find(fl, ud->fd, ud->va, ud->len, 0, 0, &map)) { |
| 2203 | pr_err("mapping not found to unamp %x va %llx %x\n", |
| 2204 | ud->fd, (unsigned long long)ud->va, |
| 2205 | (unsigned int)ud->len); |
| 2206 | err = -1; |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 2207 | mutex_unlock(&fl->fl_map_mutex); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2208 | goto bail; |
| 2209 | } |
| 2210 | if (map) |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 2211 | fastrpc_mmap_free(map, 0); |
| 2212 | mutex_unlock(&fl->fl_map_mutex); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2213 | bail: |
| 2214 | return err; |
| 2215 | } |
| 2216 | |
| 2217 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2218 | static int fastrpc_internal_mmap(struct fastrpc_file *fl, |
| 2219 | struct fastrpc_ioctl_mmap *ud) |
| 2220 | { |
| 2221 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2222 | struct fastrpc_mmap *map = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2223 | int err = 0; |
| 2224 | |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 2225 | mutex_lock(&fl->map_mutex); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 2226 | mutex_lock(&fl->fl_map_mutex); |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 2227 | if (!fastrpc_mmap_find(fl, ud->fd, (uintptr_t)ud->vaddrin, |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 2228 | ud->size, ud->flags, 1, &map)) { |
| 2229 | mutex_unlock(&fl->fl_map_mutex); |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 2230 | mutex_unlock(&fl->map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2231 | return 0; |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 2232 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2233 | VERIFY(err, !fastrpc_mmap_create(fl, ud->fd, 0, |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 2234 | (uintptr_t)ud->vaddrin, ud->size, |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2235 | ud->flags, &map)); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 2236 | mutex_unlock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2237 | if (err) |
| 2238 | goto bail; |
| 2239 | VERIFY(err, 0 == fastrpc_mmap_on_dsp(fl, ud->flags, map)); |
| 2240 | if (err) |
| 2241 | goto bail; |
| 2242 | ud->vaddrout = map->raddr; |
| 2243 | bail: |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 2244 | if (err && map) { |
| 2245 | mutex_lock(&fl->fl_map_mutex); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2246 | fastrpc_mmap_free(map, 0); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 2247 | mutex_unlock(&fl->fl_map_mutex); |
| 2248 | } |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 2249 | mutex_unlock(&fl->map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2250 | return err; |
| 2251 | } |
| 2252 | |
| 2253 | static void fastrpc_channel_close(struct kref *kref) |
| 2254 | { |
| 2255 | struct fastrpc_apps *me = &gfa; |
| 2256 | struct fastrpc_channel_ctx *ctx; |
| 2257 | int cid; |
| 2258 | |
| 2259 | ctx = container_of(kref, struct fastrpc_channel_ctx, kref); |
| 2260 | cid = ctx - &gcinfo[0]; |
| 2261 | fastrpc_glink_close(ctx->chan, cid); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2262 | ctx->chan = NULL; |
Tharun Kumar Merugu | 532767d | 2017-06-20 19:53:13 +0530 | [diff] [blame] | 2263 | glink_unregister_link_state_cb(ctx->link.link_notify_handle); |
| 2264 | ctx->link.link_notify_handle = NULL; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2265 | mutex_unlock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2266 | pr_info("'closed /dev/%s c %d %d'\n", gcinfo[cid].name, |
| 2267 | MAJOR(me->dev_no), cid); |
| 2268 | } |
| 2269 | |
| 2270 | static void fastrpc_context_list_dtor(struct fastrpc_file *fl); |
| 2271 | |
| 2272 | static int fastrpc_session_alloc_locked(struct fastrpc_channel_ctx *chan, |
| 2273 | int secure, struct fastrpc_session_ctx **session) |
| 2274 | { |
| 2275 | struct fastrpc_apps *me = &gfa; |
| 2276 | int idx = 0, err = 0; |
| 2277 | |
| 2278 | if (chan->sesscount) { |
| 2279 | for (idx = 0; idx < chan->sesscount; ++idx) { |
| 2280 | if (!chan->session[idx].used && |
| 2281 | chan->session[idx].smmu.secure == secure) { |
| 2282 | chan->session[idx].used = 1; |
| 2283 | break; |
| 2284 | } |
| 2285 | } |
| 2286 | VERIFY(err, idx < chan->sesscount); |
| 2287 | if (err) |
| 2288 | goto bail; |
| 2289 | chan->session[idx].smmu.faults = 0; |
| 2290 | } else { |
| 2291 | VERIFY(err, me->dev != NULL); |
| 2292 | if (err) |
| 2293 | goto bail; |
| 2294 | chan->session[0].dev = me->dev; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2295 | chan->session[0].smmu.dev = me->dev; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2296 | } |
| 2297 | |
| 2298 | *session = &chan->session[idx]; |
| 2299 | bail: |
| 2300 | return err; |
| 2301 | } |
| 2302 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2303 | static bool fastrpc_glink_notify_rx_intent_req(void *h, const void *priv, |
| 2304 | size_t size) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2305 | { |
| 2306 | if (glink_queue_rx_intent(h, NULL, size)) |
| 2307 | return false; |
| 2308 | return true; |
| 2309 | } |
| 2310 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2311 | static void fastrpc_glink_notify_tx_done(void *handle, const void *priv, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2312 | const void *pkt_priv, const void *ptr) |
| 2313 | { |
| 2314 | } |
| 2315 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2316 | static void fastrpc_glink_notify_rx(void *handle, const void *priv, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2317 | const void *pkt_priv, const void *ptr, size_t size) |
| 2318 | { |
| 2319 | struct smq_invoke_rsp *rsp = (struct smq_invoke_rsp *)ptr; |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 2320 | struct smq_invoke_ctx *ctx; |
| 2321 | int err = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2322 | |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 2323 | VERIFY(err, (rsp && size >= sizeof(*rsp))); |
| 2324 | if (err) |
Tharun Kumar Merugu | 622d871 | 2017-09-15 15:30:06 +0530 | [diff] [blame] | 2325 | goto bail; |
| 2326 | |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 2327 | ctx = (struct smq_invoke_ctx *)(uint64_to_ptr(rsp->ctx & ~1)); |
| 2328 | VERIFY(err, (ctx && ctx->magic == FASTRPC_CTX_MAGIC)); |
| 2329 | if (err) |
Tharun Kumar Merugu | 622d871 | 2017-09-15 15:30:06 +0530 | [diff] [blame] | 2330 | goto bail; |
Tharun Kumar Merugu | 622d871 | 2017-09-15 15:30:06 +0530 | [diff] [blame] | 2331 | |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 2332 | context_notify_user(ctx, rsp->retval); |
Tharun Kumar Merugu | 622d871 | 2017-09-15 15:30:06 +0530 | [diff] [blame] | 2333 | bail: |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 2334 | if (err) |
| 2335 | pr_err("adsprpc: invalid response or context\n"); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2336 | glink_rx_done(handle, ptr, true); |
| 2337 | } |
| 2338 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2339 | static void fastrpc_glink_notify_state(void *handle, const void *priv, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2340 | unsigned int event) |
| 2341 | { |
| 2342 | struct fastrpc_apps *me = &gfa; |
| 2343 | int cid = (int)(uintptr_t)priv; |
| 2344 | struct fastrpc_glink_info *link; |
| 2345 | |
| 2346 | if (cid < 0 || cid >= NUM_CHANNELS) |
| 2347 | return; |
| 2348 | link = &me->channel[cid].link; |
| 2349 | switch (event) { |
| 2350 | case GLINK_CONNECTED: |
| 2351 | link->port_state = FASTRPC_LINK_CONNECTED; |
Tharun Kumar Merugu | 53a8ec9 | 2017-07-14 15:52:49 +0530 | [diff] [blame] | 2352 | complete(&me->channel[cid].workport); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2353 | break; |
| 2354 | case GLINK_LOCAL_DISCONNECTED: |
| 2355 | link->port_state = FASTRPC_LINK_DISCONNECTED; |
| 2356 | break; |
| 2357 | case GLINK_REMOTE_DISCONNECTED: |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2358 | break; |
| 2359 | default: |
| 2360 | break; |
| 2361 | } |
| 2362 | } |
| 2363 | |
| 2364 | static int fastrpc_session_alloc(struct fastrpc_channel_ctx *chan, int secure, |
| 2365 | struct fastrpc_session_ctx **session) |
| 2366 | { |
| 2367 | int err = 0; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2368 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2369 | |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2370 | mutex_lock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2371 | if (!*session) |
| 2372 | err = fastrpc_session_alloc_locked(chan, secure, session); |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2373 | mutex_unlock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2374 | return err; |
| 2375 | } |
| 2376 | |
| 2377 | static void fastrpc_session_free(struct fastrpc_channel_ctx *chan, |
| 2378 | struct fastrpc_session_ctx *session) |
| 2379 | { |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2380 | struct fastrpc_apps *me = &gfa; |
| 2381 | |
| 2382 | mutex_lock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2383 | session->used = 0; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2384 | mutex_unlock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2385 | } |
| 2386 | |
| 2387 | static int fastrpc_file_free(struct fastrpc_file *fl) |
| 2388 | { |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 2389 | struct hlist_node *n = NULL; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2390 | struct fastrpc_mmap *map = NULL; |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 2391 | struct fastrpc_perf *perf = NULL, *fperf = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2392 | int cid; |
| 2393 | |
| 2394 | if (!fl) |
| 2395 | return 0; |
| 2396 | cid = fl->cid; |
| 2397 | |
Tharun Kumar Merugu | 622d871 | 2017-09-15 15:30:06 +0530 | [diff] [blame] | 2398 | (void)fastrpc_release_current_dsp_process(fl); |
| 2399 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2400 | spin_lock(&fl->apps->hlock); |
| 2401 | hlist_del_init(&fl->hn); |
| 2402 | spin_unlock(&fl->apps->hlock); |
| 2403 | |
Sathish Ambley | d7fbcbb | 2017-03-08 10:55:48 -0800 | [diff] [blame] | 2404 | if (!fl->sctx) { |
| 2405 | kfree(fl); |
| 2406 | return 0; |
| 2407 | } |
tharun kumar | 9f899ea | 2017-07-03 17:07:03 +0530 | [diff] [blame] | 2408 | spin_lock(&fl->hlock); |
| 2409 | fl->file_close = 1; |
| 2410 | spin_unlock(&fl->hlock); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2411 | fastrpc_context_list_dtor(fl); |
| 2412 | fastrpc_buf_list_free(fl); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 2413 | mutex_lock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2414 | hlist_for_each_entry_safe(map, n, &fl->maps, hn) { |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2415 | fastrpc_mmap_free(map, 1); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2416 | } |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 2417 | mutex_unlock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2418 | if (fl->ssrcount == fl->apps->channel[cid].ssrcount) |
| 2419 | kref_put_mutex(&fl->apps->channel[cid].kref, |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2420 | fastrpc_channel_close, &fl->apps->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2421 | if (fl->sctx) |
| 2422 | fastrpc_session_free(&fl->apps->channel[cid], fl->sctx); |
| 2423 | if (fl->secsctx) |
| 2424 | fastrpc_session_free(&fl->apps->channel[cid], fl->secsctx); |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 2425 | |
| 2426 | mutex_lock(&fl->perf_mutex); |
| 2427 | do { |
| 2428 | struct hlist_node *pn = NULL; |
| 2429 | |
| 2430 | fperf = NULL; |
| 2431 | hlist_for_each_entry_safe(perf, pn, &fl->perf, hn) { |
| 2432 | hlist_del_init(&perf->hn); |
| 2433 | fperf = perf; |
| 2434 | break; |
| 2435 | } |
| 2436 | kfree(fperf); |
| 2437 | } while (fperf); |
| 2438 | mutex_unlock(&fl->perf_mutex); |
| 2439 | mutex_destroy(&fl->perf_mutex); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 2440 | mutex_destroy(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2441 | kfree(fl); |
| 2442 | return 0; |
| 2443 | } |
| 2444 | |
| 2445 | static int fastrpc_device_release(struct inode *inode, struct file *file) |
| 2446 | { |
| 2447 | struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data; |
| 2448 | |
| 2449 | if (fl) { |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 2450 | if (fl->qos_request && pm_qos_request_active(&fl->pm_qos_req)) |
| 2451 | pm_qos_remove_request(&fl->pm_qos_req); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 2452 | if (fl->debugfs_file != NULL) |
| 2453 | debugfs_remove(fl->debugfs_file); |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 2454 | mutex_destroy(&fl->map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2455 | fastrpc_file_free(fl); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2456 | file->private_data = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2457 | } |
| 2458 | return 0; |
| 2459 | } |
| 2460 | |
| 2461 | static void fastrpc_link_state_handler(struct glink_link_state_cb_info *cb_info, |
| 2462 | void *priv) |
| 2463 | { |
| 2464 | struct fastrpc_apps *me = &gfa; |
| 2465 | int cid = (int)((uintptr_t)priv); |
| 2466 | struct fastrpc_glink_info *link; |
| 2467 | |
| 2468 | if (cid < 0 || cid >= NUM_CHANNELS) |
| 2469 | return; |
| 2470 | |
| 2471 | link = &me->channel[cid].link; |
| 2472 | switch (cb_info->link_state) { |
| 2473 | case GLINK_LINK_STATE_UP: |
| 2474 | link->link_state = FASTRPC_LINK_STATE_UP; |
| 2475 | complete(&me->channel[cid].work); |
| 2476 | break; |
| 2477 | case GLINK_LINK_STATE_DOWN: |
| 2478 | link->link_state = FASTRPC_LINK_STATE_DOWN; |
| 2479 | break; |
| 2480 | default: |
| 2481 | pr_err("adsprpc: unknown link state %d\n", cb_info->link_state); |
| 2482 | break; |
| 2483 | } |
| 2484 | } |
| 2485 | |
| 2486 | static int fastrpc_glink_register(int cid, struct fastrpc_apps *me) |
| 2487 | { |
| 2488 | int err = 0; |
| 2489 | struct fastrpc_glink_info *link; |
| 2490 | |
| 2491 | VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS)); |
| 2492 | if (err) |
| 2493 | goto bail; |
| 2494 | |
| 2495 | link = &me->channel[cid].link; |
| 2496 | if (link->link_notify_handle != NULL) |
| 2497 | goto bail; |
| 2498 | |
| 2499 | link->link_info.glink_link_state_notif_cb = fastrpc_link_state_handler; |
| 2500 | link->link_notify_handle = glink_register_link_state_cb( |
| 2501 | &link->link_info, |
| 2502 | (void *)((uintptr_t)cid)); |
| 2503 | VERIFY(err, !IS_ERR_OR_NULL(me->channel[cid].link.link_notify_handle)); |
| 2504 | if (err) { |
| 2505 | link->link_notify_handle = NULL; |
| 2506 | goto bail; |
| 2507 | } |
| 2508 | VERIFY(err, wait_for_completion_timeout(&me->channel[cid].work, |
| 2509 | RPC_TIMEOUT)); |
| 2510 | bail: |
| 2511 | return err; |
| 2512 | } |
| 2513 | |
| 2514 | static void fastrpc_glink_close(void *chan, int cid) |
| 2515 | { |
| 2516 | int err = 0; |
| 2517 | struct fastrpc_glink_info *link; |
| 2518 | |
| 2519 | VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS)); |
| 2520 | if (err) |
| 2521 | return; |
| 2522 | link = &gfa.channel[cid].link; |
| 2523 | |
c_mtharu | 314a420 | 2017-11-15 22:09:17 +0530 | [diff] [blame] | 2524 | if (link->port_state == FASTRPC_LINK_CONNECTED || |
| 2525 | link->port_state == FASTRPC_LINK_REMOTE_DISCONNECTING) { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2526 | link->port_state = FASTRPC_LINK_DISCONNECTING; |
| 2527 | glink_close(chan); |
| 2528 | } |
| 2529 | } |
| 2530 | |
| 2531 | static int fastrpc_glink_open(int cid) |
| 2532 | { |
| 2533 | int err = 0; |
| 2534 | void *handle = NULL; |
| 2535 | struct fastrpc_apps *me = &gfa; |
| 2536 | struct glink_open_config *cfg; |
| 2537 | struct fastrpc_glink_info *link; |
| 2538 | |
| 2539 | VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS)); |
| 2540 | if (err) |
| 2541 | goto bail; |
| 2542 | link = &me->channel[cid].link; |
| 2543 | cfg = &me->channel[cid].link.cfg; |
| 2544 | VERIFY(err, (link->link_state == FASTRPC_LINK_STATE_UP)); |
| 2545 | if (err) |
| 2546 | goto bail; |
| 2547 | |
Tharun Kumar Merugu | ca0db526 | 2017-05-10 12:53:12 +0530 | [diff] [blame] | 2548 | VERIFY(err, (link->port_state == FASTRPC_LINK_DISCONNECTED)); |
| 2549 | if (err) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2550 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2551 | |
| 2552 | link->port_state = FASTRPC_LINK_CONNECTING; |
| 2553 | cfg->priv = (void *)(uintptr_t)cid; |
| 2554 | cfg->edge = gcinfo[cid].link.link_info.edge; |
| 2555 | cfg->transport = gcinfo[cid].link.link_info.transport; |
| 2556 | cfg->name = FASTRPC_GLINK_GUID; |
| 2557 | cfg->notify_rx = fastrpc_glink_notify_rx; |
| 2558 | cfg->notify_tx_done = fastrpc_glink_notify_tx_done; |
| 2559 | cfg->notify_state = fastrpc_glink_notify_state; |
| 2560 | cfg->notify_rx_intent_req = fastrpc_glink_notify_rx_intent_req; |
| 2561 | handle = glink_open(cfg); |
| 2562 | VERIFY(err, !IS_ERR_OR_NULL(handle)); |
c_mtharu | 6e1d26b | 2017-10-09 16:05:24 +0530 | [diff] [blame] | 2563 | if (err) { |
| 2564 | if (link->port_state == FASTRPC_LINK_CONNECTING) |
| 2565 | link->port_state = FASTRPC_LINK_DISCONNECTED; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2566 | goto bail; |
c_mtharu | 6e1d26b | 2017-10-09 16:05:24 +0530 | [diff] [blame] | 2567 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2568 | me->channel[cid].chan = handle; |
| 2569 | bail: |
| 2570 | return err; |
| 2571 | } |
| 2572 | |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 2573 | static int fastrpc_debugfs_open(struct inode *inode, struct file *filp) |
| 2574 | { |
| 2575 | filp->private_data = inode->i_private; |
| 2576 | return 0; |
| 2577 | } |
| 2578 | |
| 2579 | static ssize_t fastrpc_debugfs_read(struct file *filp, char __user *buffer, |
| 2580 | size_t count, loff_t *position) |
| 2581 | { |
| 2582 | struct fastrpc_file *fl = filp->private_data; |
| 2583 | struct hlist_node *n; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2584 | struct fastrpc_buf *buf = NULL; |
| 2585 | struct fastrpc_mmap *map = NULL; |
| 2586 | struct smq_invoke_ctx *ictx = NULL; |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 2587 | struct fastrpc_channel_ctx *chan; |
| 2588 | struct fastrpc_session_ctx *sess; |
| 2589 | unsigned int len = 0; |
| 2590 | int i, j, ret = 0; |
| 2591 | char *fileinfo = NULL; |
| 2592 | |
| 2593 | fileinfo = kzalloc(DEBUGFS_SIZE, GFP_KERNEL); |
| 2594 | if (!fileinfo) |
| 2595 | goto bail; |
| 2596 | if (fl == NULL) { |
| 2597 | for (i = 0; i < NUM_CHANNELS; i++) { |
| 2598 | chan = &gcinfo[i]; |
| 2599 | len += scnprintf(fileinfo + len, |
| 2600 | DEBUGFS_SIZE - len, "%s\n\n", |
| 2601 | chan->name); |
| 2602 | len += scnprintf(fileinfo + len, |
| 2603 | DEBUGFS_SIZE - len, "%s %d\n", |
| 2604 | "sesscount:", chan->sesscount); |
| 2605 | for (j = 0; j < chan->sesscount; j++) { |
| 2606 | sess = &chan->session[j]; |
| 2607 | len += scnprintf(fileinfo + len, |
| 2608 | DEBUGFS_SIZE - len, |
| 2609 | "%s%d\n\n", "SESSION", j); |
| 2610 | len += scnprintf(fileinfo + len, |
| 2611 | DEBUGFS_SIZE - len, |
| 2612 | "%s %d\n", "sid:", |
| 2613 | sess->smmu.cb); |
| 2614 | len += scnprintf(fileinfo + len, |
| 2615 | DEBUGFS_SIZE - len, |
| 2616 | "%s %d\n", "SECURE:", |
| 2617 | sess->smmu.secure); |
| 2618 | } |
| 2619 | } |
| 2620 | } else { |
| 2621 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 2622 | "%s %d\n\n", |
| 2623 | "PROCESS_ID:", fl->tgid); |
| 2624 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 2625 | "%s %d\n\n", |
| 2626 | "CHANNEL_ID:", fl->cid); |
| 2627 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 2628 | "%s %d\n\n", |
| 2629 | "SSRCOUNT:", fl->ssrcount); |
| 2630 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 2631 | "%s\n", |
| 2632 | "LIST OF BUFS:"); |
| 2633 | spin_lock(&fl->hlock); |
| 2634 | hlist_for_each_entry_safe(buf, n, &fl->bufs, hn) { |
| 2635 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Tharun Kumar Merugu | ce56645 | 2017-08-17 15:29:59 +0530 | [diff] [blame] | 2636 | "%s %pK %s %pK %s %llx\n", "buf:", |
| 2637 | buf, "buf->virt:", buf->virt, |
| 2638 | "buf->phys:", buf->phys); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 2639 | } |
| 2640 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 2641 | "\n%s\n", |
| 2642 | "LIST OF MAPS:"); |
| 2643 | hlist_for_each_entry_safe(map, n, &fl->maps, hn) { |
| 2644 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Tharun Kumar Merugu | ce56645 | 2017-08-17 15:29:59 +0530 | [diff] [blame] | 2645 | "%s %pK %s %lx %s %llx\n", |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 2646 | "map:", map, |
| 2647 | "map->va:", map->va, |
| 2648 | "map->phys:", map->phys); |
| 2649 | } |
| 2650 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 2651 | "\n%s\n", |
| 2652 | "LIST OF PENDING SMQCONTEXTS:"); |
| 2653 | hlist_for_each_entry_safe(ictx, n, &fl->clst.pending, hn) { |
| 2654 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Tharun Kumar Merugu | ce56645 | 2017-08-17 15:29:59 +0530 | [diff] [blame] | 2655 | "%s %pK %s %u %s %u %s %u\n", |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 2656 | "smqcontext:", ictx, |
| 2657 | "sc:", ictx->sc, |
| 2658 | "tid:", ictx->pid, |
| 2659 | "handle", ictx->rpra->h); |
| 2660 | } |
| 2661 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 2662 | "\n%s\n", |
| 2663 | "LIST OF INTERRUPTED SMQCONTEXTS:"); |
| 2664 | hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) { |
| 2665 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Tharun Kumar Merugu | ce56645 | 2017-08-17 15:29:59 +0530 | [diff] [blame] | 2666 | "%s %pK %s %u %s %u %s %u\n", |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 2667 | "smqcontext:", ictx, |
| 2668 | "sc:", ictx->sc, |
| 2669 | "tid:", ictx->pid, |
| 2670 | "handle", ictx->rpra->h); |
| 2671 | } |
| 2672 | spin_unlock(&fl->hlock); |
| 2673 | } |
| 2674 | if (len > DEBUGFS_SIZE) |
| 2675 | len = DEBUGFS_SIZE; |
| 2676 | ret = simple_read_from_buffer(buffer, count, position, fileinfo, len); |
| 2677 | kfree(fileinfo); |
| 2678 | bail: |
| 2679 | return ret; |
| 2680 | } |
| 2681 | |
| 2682 | static const struct file_operations debugfs_fops = { |
| 2683 | .open = fastrpc_debugfs_open, |
| 2684 | .read = fastrpc_debugfs_read, |
| 2685 | }; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 2686 | static int fastrpc_channel_open(struct fastrpc_file *fl) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2687 | { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2688 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 2689 | int cid, err = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2690 | |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2691 | mutex_lock(&me->smd_mutex); |
| 2692 | |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 2693 | VERIFY(err, fl && fl->sctx); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2694 | if (err) |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2695 | goto bail; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 2696 | cid = fl->cid; |
c_mtharu | 314a420 | 2017-11-15 22:09:17 +0530 | [diff] [blame] | 2697 | VERIFY(err, cid >= 0 && cid < NUM_CHANNELS); |
| 2698 | if (err) |
| 2699 | goto bail; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2700 | if (me->channel[cid].ssrcount != |
| 2701 | me->channel[cid].prevssrcount) { |
| 2702 | if (!me->channel[cid].issubsystemup) { |
| 2703 | VERIFY(err, 0); |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2704 | if (err) { |
| 2705 | err = -ENOTCONN; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2706 | goto bail; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2707 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2708 | } |
| 2709 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2710 | fl->ssrcount = me->channel[cid].ssrcount; |
| 2711 | if ((kref_get_unless_zero(&me->channel[cid].kref) == 0) || |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2712 | (me->channel[cid].chan == NULL)) { |
Tharun Kumar Merugu | ca0db526 | 2017-05-10 12:53:12 +0530 | [diff] [blame] | 2713 | VERIFY(err, 0 == fastrpc_glink_register(cid, me)); |
| 2714 | if (err) |
| 2715 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2716 | VERIFY(err, 0 == fastrpc_glink_open(cid)); |
| 2717 | if (err) |
| 2718 | goto bail; |
| 2719 | |
Tharun Kumar Merugu | 53a8ec9 | 2017-07-14 15:52:49 +0530 | [diff] [blame] | 2720 | VERIFY(err, |
| 2721 | wait_for_completion_timeout(&me->channel[cid].workport, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2722 | RPC_TIMEOUT)); |
| 2723 | if (err) { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2724 | me->channel[cid].chan = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2725 | goto bail; |
| 2726 | } |
| 2727 | kref_init(&me->channel[cid].kref); |
| 2728 | pr_info("'opened /dev/%s c %d %d'\n", gcinfo[cid].name, |
| 2729 | MAJOR(me->dev_no), cid); |
c_mtharu | 314a420 | 2017-11-15 22:09:17 +0530 | [diff] [blame] | 2730 | err = glink_queue_rx_intent(me->channel[cid].chan, NULL, |
| 2731 | FASTRPC_GLINK_INTENT_LEN); |
| 2732 | err |= glink_queue_rx_intent(me->channel[cid].chan, NULL, |
| 2733 | FASTRPC_GLINK_INTENT_LEN); |
Bruce Levy | 34c3c1c | 2017-07-31 17:08:58 -0700 | [diff] [blame] | 2734 | if (err) |
Tharun Kumar Merugu | 88ba925 | 2017-08-09 12:15:41 +0530 | [diff] [blame] | 2735 | pr_warn("adsprpc: initial intent fail for %d err %d\n", |
| 2736 | cid, err); |
Tharun Kumar Merugu | d86fc8c | 2018-01-04 16:35:31 +0530 | [diff] [blame] | 2737 | if (cid == 0 && me->channel[cid].ssrcount != |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2738 | me->channel[cid].prevssrcount) { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2739 | if (fastrpc_mmap_remove_ssr(fl)) |
| 2740 | pr_err("ADSPRPC: SSR: Failed to unmap remote heap\n"); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2741 | me->channel[cid].prevssrcount = |
| 2742 | me->channel[cid].ssrcount; |
| 2743 | } |
| 2744 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2745 | |
| 2746 | bail: |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2747 | mutex_unlock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2748 | return err; |
| 2749 | } |
| 2750 | |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 2751 | static int fastrpc_device_open(struct inode *inode, struct file *filp) |
| 2752 | { |
| 2753 | int err = 0; |
Sathish Ambley | 567012b | 2017-03-06 11:55:04 -0800 | [diff] [blame] | 2754 | struct dentry *debugfs_file; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2755 | struct fastrpc_file *fl = NULL; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 2756 | struct fastrpc_apps *me = &gfa; |
| 2757 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2758 | VERIFY(err, NULL != (fl = kzalloc(sizeof(*fl), GFP_KERNEL))); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 2759 | if (err) |
| 2760 | return err; |
Sathish Ambley | 567012b | 2017-03-06 11:55:04 -0800 | [diff] [blame] | 2761 | debugfs_file = debugfs_create_file(current->comm, 0644, debugfs_root, |
| 2762 | fl, &debugfs_fops); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 2763 | context_list_ctor(&fl->clst); |
| 2764 | spin_lock_init(&fl->hlock); |
| 2765 | INIT_HLIST_HEAD(&fl->maps); |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 2766 | INIT_HLIST_HEAD(&fl->perf); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 2767 | INIT_HLIST_HEAD(&fl->bufs); |
| 2768 | INIT_HLIST_NODE(&fl->hn); |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 2769 | fl->sessionid = 0; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 2770 | fl->tgid = current->tgid; |
| 2771 | fl->apps = me; |
| 2772 | fl->mode = FASTRPC_MODE_SERIAL; |
| 2773 | fl->cid = -1; |
Sathish Ambley | 567012b | 2017-03-06 11:55:04 -0800 | [diff] [blame] | 2774 | if (debugfs_file != NULL) |
| 2775 | fl->debugfs_file = debugfs_file; |
| 2776 | memset(&fl->perf, 0, sizeof(fl->perf)); |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 2777 | fl->qos_request = 0; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 2778 | filp->private_data = fl; |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 2779 | mutex_init(&fl->map_mutex); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame^] | 2780 | mutex_init(&fl->fl_map_mutex); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 2781 | spin_lock(&me->hlock); |
| 2782 | hlist_add_head(&fl->hn, &me->drivers); |
| 2783 | spin_unlock(&me->hlock); |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 2784 | mutex_init(&fl->perf_mutex); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 2785 | return 0; |
| 2786 | } |
| 2787 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2788 | static int fastrpc_get_info(struct fastrpc_file *fl, uint32_t *info) |
| 2789 | { |
| 2790 | int err = 0; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 2791 | uint32_t cid; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2792 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2793 | VERIFY(err, fl != NULL); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2794 | if (err) |
| 2795 | goto bail; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 2796 | if (fl->cid == -1) { |
| 2797 | cid = *info; |
| 2798 | VERIFY(err, cid < NUM_CHANNELS); |
| 2799 | if (err) |
| 2800 | goto bail; |
| 2801 | fl->cid = cid; |
| 2802 | fl->ssrcount = fl->apps->channel[cid].ssrcount; |
| 2803 | VERIFY(err, !fastrpc_session_alloc_locked( |
| 2804 | &fl->apps->channel[cid], 0, &fl->sctx)); |
| 2805 | if (err) |
| 2806 | goto bail; |
| 2807 | } |
Tharun Kumar Merugu | 80be7d6 | 2017-08-02 11:03:22 +0530 | [diff] [blame] | 2808 | VERIFY(err, fl->sctx != NULL); |
| 2809 | if (err) |
| 2810 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2811 | *info = (fl->sctx->smmu.enabled ? 1 : 0); |
| 2812 | bail: |
| 2813 | return err; |
| 2814 | } |
| 2815 | |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 2816 | static int fastrpc_internal_control(struct fastrpc_file *fl, |
| 2817 | struct fastrpc_ioctl_control *cp) |
| 2818 | { |
| 2819 | int err = 0; |
| 2820 | int latency; |
| 2821 | |
| 2822 | VERIFY(err, !IS_ERR_OR_NULL(fl) && !IS_ERR_OR_NULL(fl->apps)); |
| 2823 | if (err) |
| 2824 | goto bail; |
| 2825 | VERIFY(err, !IS_ERR_OR_NULL(cp)); |
| 2826 | if (err) |
| 2827 | goto bail; |
| 2828 | |
| 2829 | switch (cp->req) { |
| 2830 | case FASTRPC_CONTROL_LATENCY: |
| 2831 | latency = cp->lp.enable == FASTRPC_LATENCY_CTRL_ENB ? |
| 2832 | fl->apps->latency : PM_QOS_DEFAULT_VALUE; |
| 2833 | VERIFY(err, latency != 0); |
| 2834 | if (err) |
| 2835 | goto bail; |
| 2836 | if (!fl->qos_request) { |
| 2837 | pm_qos_add_request(&fl->pm_qos_req, |
| 2838 | PM_QOS_CPU_DMA_LATENCY, latency); |
| 2839 | fl->qos_request = 1; |
| 2840 | } else |
| 2841 | pm_qos_update_request(&fl->pm_qos_req, latency); |
| 2842 | break; |
| 2843 | default: |
| 2844 | err = -ENOTTY; |
| 2845 | break; |
| 2846 | } |
| 2847 | bail: |
| 2848 | return err; |
| 2849 | } |
| 2850 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2851 | static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num, |
| 2852 | unsigned long ioctl_param) |
| 2853 | { |
| 2854 | union { |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 2855 | struct fastrpc_ioctl_invoke_crc inv; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2856 | struct fastrpc_ioctl_mmap mmap; |
| 2857 | struct fastrpc_ioctl_munmap munmap; |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2858 | struct fastrpc_ioctl_munmap_fd munmap_fd; |
Sathish Ambley | d6300c3 | 2017-01-18 09:50:43 -0800 | [diff] [blame] | 2859 | struct fastrpc_ioctl_init_attrs init; |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 2860 | struct fastrpc_ioctl_perf perf; |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 2861 | struct fastrpc_ioctl_control cp; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2862 | } p; |
| 2863 | void *param = (char *)ioctl_param; |
| 2864 | struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data; |
| 2865 | int size = 0, err = 0; |
| 2866 | uint32_t info; |
| 2867 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2868 | p.inv.fds = NULL; |
| 2869 | p.inv.attrs = NULL; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 2870 | p.inv.crc = NULL; |
tharun kumar | 9f899ea | 2017-07-03 17:07:03 +0530 | [diff] [blame] | 2871 | spin_lock(&fl->hlock); |
| 2872 | if (fl->file_close == 1) { |
| 2873 | err = EBADF; |
| 2874 | pr_warn("ADSPRPC: fastrpc_device_release is happening, So not sending any new requests to DSP"); |
| 2875 | spin_unlock(&fl->hlock); |
| 2876 | goto bail; |
| 2877 | } |
| 2878 | spin_unlock(&fl->hlock); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2879 | |
| 2880 | switch (ioctl_num) { |
| 2881 | case FASTRPC_IOCTL_INVOKE: |
| 2882 | size = sizeof(struct fastrpc_ioctl_invoke); |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 2883 | /* fall through */ |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2884 | case FASTRPC_IOCTL_INVOKE_FD: |
| 2885 | if (!size) |
| 2886 | size = sizeof(struct fastrpc_ioctl_invoke_fd); |
| 2887 | /* fall through */ |
| 2888 | case FASTRPC_IOCTL_INVOKE_ATTRS: |
| 2889 | if (!size) |
| 2890 | size = sizeof(struct fastrpc_ioctl_invoke_attrs); |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 2891 | /* fall through */ |
| 2892 | case FASTRPC_IOCTL_INVOKE_CRC: |
| 2893 | if (!size) |
| 2894 | size = sizeof(struct fastrpc_ioctl_invoke_crc); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2895 | K_COPY_FROM_USER(err, 0, &p.inv, param, size); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2896 | if (err) |
| 2897 | goto bail; |
| 2898 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, fl->mode, |
| 2899 | 0, &p.inv))); |
| 2900 | if (err) |
| 2901 | goto bail; |
| 2902 | break; |
| 2903 | case FASTRPC_IOCTL_MMAP: |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2904 | K_COPY_FROM_USER(err, 0, &p.mmap, param, |
| 2905 | sizeof(p.mmap)); |
Tharun Kumar Merugu | d1f388a | 2017-10-01 10:51:11 +0530 | [diff] [blame] | 2906 | if (err) |
| 2907 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2908 | VERIFY(err, 0 == (err = fastrpc_internal_mmap(fl, &p.mmap))); |
| 2909 | if (err) |
| 2910 | goto bail; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2911 | K_COPY_TO_USER(err, 0, param, &p.mmap, sizeof(p.mmap)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2912 | if (err) |
| 2913 | goto bail; |
| 2914 | break; |
| 2915 | case FASTRPC_IOCTL_MUNMAP: |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2916 | K_COPY_FROM_USER(err, 0, &p.munmap, param, |
| 2917 | sizeof(p.munmap)); |
Tharun Kumar Merugu | d1f388a | 2017-10-01 10:51:11 +0530 | [diff] [blame] | 2918 | if (err) |
| 2919 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2920 | VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl, |
| 2921 | &p.munmap))); |
| 2922 | if (err) |
| 2923 | goto bail; |
| 2924 | break; |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2925 | case FASTRPC_IOCTL_MUNMAP_FD: |
| 2926 | K_COPY_FROM_USER(err, 0, &p.munmap_fd, param, |
| 2927 | sizeof(p.munmap_fd)); |
| 2928 | if (err) |
| 2929 | goto bail; |
| 2930 | VERIFY(err, 0 == (err = fastrpc_internal_munmap_fd(fl, |
| 2931 | &p.munmap_fd))); |
| 2932 | if (err) |
| 2933 | goto bail; |
| 2934 | break; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2935 | case FASTRPC_IOCTL_SETMODE: |
| 2936 | switch ((uint32_t)ioctl_param) { |
| 2937 | case FASTRPC_MODE_PARALLEL: |
| 2938 | case FASTRPC_MODE_SERIAL: |
| 2939 | fl->mode = (uint32_t)ioctl_param; |
| 2940 | break; |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 2941 | case FASTRPC_MODE_PROFILE: |
| 2942 | fl->profile = (uint32_t)ioctl_param; |
| 2943 | break; |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 2944 | case FASTRPC_MODE_SESSION: |
| 2945 | fl->sessionid = 1; |
| 2946 | fl->tgid |= (1 << SESSION_ID_INDEX); |
| 2947 | break; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2948 | default: |
| 2949 | err = -ENOTTY; |
| 2950 | break; |
| 2951 | } |
| 2952 | break; |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 2953 | case FASTRPC_IOCTL_GETPERF: |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2954 | K_COPY_FROM_USER(err, 0, &p.perf, |
| 2955 | param, sizeof(p.perf)); |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 2956 | if (err) |
| 2957 | goto bail; |
| 2958 | p.perf.numkeys = sizeof(struct fastrpc_perf)/sizeof(int64_t); |
| 2959 | if (p.perf.keys) { |
| 2960 | char *keys = PERF_KEYS; |
| 2961 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2962 | K_COPY_TO_USER(err, 0, (void *)p.perf.keys, |
| 2963 | keys, strlen(keys)+1); |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 2964 | if (err) |
| 2965 | goto bail; |
| 2966 | } |
| 2967 | if (p.perf.data) { |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 2968 | struct fastrpc_perf *perf = NULL, *fperf = NULL; |
| 2969 | struct hlist_node *n = NULL; |
| 2970 | |
| 2971 | mutex_lock(&fl->perf_mutex); |
| 2972 | hlist_for_each_entry_safe(perf, n, &fl->perf, hn) { |
| 2973 | if (perf->tid == current->pid) { |
| 2974 | fperf = perf; |
| 2975 | break; |
| 2976 | } |
| 2977 | } |
| 2978 | |
| 2979 | mutex_unlock(&fl->perf_mutex); |
| 2980 | |
| 2981 | if (fperf) { |
| 2982 | K_COPY_TO_USER(err, 0, (void *)p.perf.data, |
| 2983 | fperf, sizeof(*fperf)); |
| 2984 | } |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 2985 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2986 | K_COPY_TO_USER(err, 0, param, &p.perf, sizeof(p.perf)); |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 2987 | if (err) |
| 2988 | goto bail; |
| 2989 | break; |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 2990 | case FASTRPC_IOCTL_CONTROL: |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2991 | K_COPY_FROM_USER(err, 0, &p.cp, param, |
| 2992 | sizeof(p.cp)); |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 2993 | if (err) |
| 2994 | goto bail; |
| 2995 | VERIFY(err, 0 == (err = fastrpc_internal_control(fl, &p.cp))); |
| 2996 | if (err) |
| 2997 | goto bail; |
| 2998 | break; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2999 | case FASTRPC_IOCTL_GETINFO: |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3000 | K_COPY_FROM_USER(err, 0, &info, param, sizeof(info)); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3001 | if (err) |
| 3002 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3003 | VERIFY(err, 0 == (err = fastrpc_get_info(fl, &info))); |
| 3004 | if (err) |
| 3005 | goto bail; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3006 | K_COPY_TO_USER(err, 0, param, &info, sizeof(info)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3007 | if (err) |
| 3008 | goto bail; |
| 3009 | break; |
| 3010 | case FASTRPC_IOCTL_INIT: |
Sathish Ambley | d6300c3 | 2017-01-18 09:50:43 -0800 | [diff] [blame] | 3011 | p.init.attrs = 0; |
| 3012 | p.init.siglen = 0; |
| 3013 | size = sizeof(struct fastrpc_ioctl_init); |
| 3014 | /* fall through */ |
| 3015 | case FASTRPC_IOCTL_INIT_ATTRS: |
| 3016 | if (!size) |
| 3017 | size = sizeof(struct fastrpc_ioctl_init_attrs); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3018 | K_COPY_FROM_USER(err, 0, &p.init, param, size); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3019 | if (err) |
| 3020 | goto bail; |
Tharun Kumar Merugu | 4ea0eac | 2017-08-22 11:42:51 +0530 | [diff] [blame] | 3021 | VERIFY(err, p.init.init.filelen >= 0 && |
Tharun Kumar Merugu | d1f388a | 2017-10-01 10:51:11 +0530 | [diff] [blame] | 3022 | p.init.init.filelen < INIT_FILELEN_MAX); |
| 3023 | if (err) |
| 3024 | goto bail; |
| 3025 | VERIFY(err, p.init.init.memlen >= 0 && |
| 3026 | p.init.init.memlen < INIT_MEMLEN_MAX); |
Tharun Kumar Merugu | 4ea0eac | 2017-08-22 11:42:51 +0530 | [diff] [blame] | 3027 | if (err) |
| 3028 | goto bail; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3029 | VERIFY(err, 0 == (err = fastrpc_init_process(fl, &p.init))); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3030 | if (err) |
| 3031 | goto bail; |
| 3032 | break; |
| 3033 | |
| 3034 | default: |
| 3035 | err = -ENOTTY; |
| 3036 | pr_info("bad ioctl: %d\n", ioctl_num); |
| 3037 | break; |
| 3038 | } |
| 3039 | bail: |
| 3040 | return err; |
| 3041 | } |
| 3042 | |
| 3043 | static int fastrpc_restart_notifier_cb(struct notifier_block *nb, |
| 3044 | unsigned long code, |
| 3045 | void *data) |
| 3046 | { |
| 3047 | struct fastrpc_apps *me = &gfa; |
| 3048 | struct fastrpc_channel_ctx *ctx; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3049 | struct notif_data *notifdata = data; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3050 | int cid; |
| 3051 | |
| 3052 | ctx = container_of(nb, struct fastrpc_channel_ctx, nb); |
| 3053 | cid = ctx - &me->channel[0]; |
| 3054 | if (code == SUBSYS_BEFORE_SHUTDOWN) { |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3055 | mutex_lock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3056 | ctx->ssrcount++; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3057 | ctx->issubsystemup = 0; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3058 | if (ctx->chan) { |
| 3059 | fastrpc_glink_close(ctx->chan, cid); |
| 3060 | ctx->chan = NULL; |
| 3061 | pr_info("'restart notifier: closed /dev/%s c %d %d'\n", |
| 3062 | gcinfo[cid].name, MAJOR(me->dev_no), cid); |
| 3063 | } |
| 3064 | mutex_unlock(&me->smd_mutex); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3065 | if (cid == 0) |
| 3066 | me->staticpd_flags = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3067 | fastrpc_notify_drivers(me, cid); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3068 | } else if (code == SUBSYS_RAMDUMP_NOTIFICATION) { |
| 3069 | if (me->channel[0].remoteheap_ramdump_dev && |
| 3070 | notifdata->enable_ramdump) { |
| 3071 | me->channel[0].ramdumpenabled = 1; |
| 3072 | } |
| 3073 | } else if (code == SUBSYS_AFTER_POWERUP) { |
| 3074 | ctx->issubsystemup = 1; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3075 | } |
| 3076 | |
| 3077 | return NOTIFY_DONE; |
| 3078 | } |
| 3079 | |
| 3080 | static const struct file_operations fops = { |
| 3081 | .open = fastrpc_device_open, |
| 3082 | .release = fastrpc_device_release, |
| 3083 | .unlocked_ioctl = fastrpc_device_ioctl, |
| 3084 | .compat_ioctl = compat_fastrpc_device_ioctl, |
| 3085 | }; |
| 3086 | |
| 3087 | static const struct of_device_id fastrpc_match_table[] = { |
| 3088 | { .compatible = "qcom,msm-fastrpc-adsp", }, |
| 3089 | { .compatible = "qcom,msm-fastrpc-compute", }, |
| 3090 | { .compatible = "qcom,msm-fastrpc-compute-cb", }, |
| 3091 | { .compatible = "qcom,msm-adsprpc-mem-region", }, |
| 3092 | {} |
| 3093 | }; |
| 3094 | |
| 3095 | static int fastrpc_cb_probe(struct device *dev) |
| 3096 | { |
| 3097 | struct fastrpc_channel_ctx *chan; |
| 3098 | struct fastrpc_session_ctx *sess; |
| 3099 | struct of_phandle_args iommuspec; |
| 3100 | const char *name; |
| 3101 | unsigned int start = 0x80000000; |
| 3102 | int err = 0, i; |
| 3103 | int secure_vmid = VMID_CP_PIXEL; |
| 3104 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3105 | VERIFY(err, NULL != (name = of_get_property(dev->of_node, |
| 3106 | "label", NULL))); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3107 | if (err) |
| 3108 | goto bail; |
| 3109 | for (i = 0; i < NUM_CHANNELS; i++) { |
| 3110 | if (!gcinfo[i].name) |
| 3111 | continue; |
| 3112 | if (!strcmp(name, gcinfo[i].name)) |
| 3113 | break; |
| 3114 | } |
| 3115 | VERIFY(err, i < NUM_CHANNELS); |
| 3116 | if (err) |
| 3117 | goto bail; |
| 3118 | chan = &gcinfo[i]; |
| 3119 | VERIFY(err, chan->sesscount < NUM_SESSIONS); |
| 3120 | if (err) |
| 3121 | goto bail; |
| 3122 | |
| 3123 | VERIFY(err, !of_parse_phandle_with_args(dev->of_node, "iommus", |
| 3124 | "#iommu-cells", 0, &iommuspec)); |
| 3125 | if (err) |
| 3126 | goto bail; |
| 3127 | sess = &chan->session[chan->sesscount]; |
| 3128 | sess->smmu.cb = iommuspec.args[0] & 0xf; |
| 3129 | sess->used = 0; |
| 3130 | sess->smmu.coherent = of_property_read_bool(dev->of_node, |
| 3131 | "dma-coherent"); |
| 3132 | sess->smmu.secure = of_property_read_bool(dev->of_node, |
| 3133 | "qcom,secure-context-bank"); |
| 3134 | if (sess->smmu.secure) |
| 3135 | start = 0x60000000; |
| 3136 | VERIFY(err, !IS_ERR_OR_NULL(sess->smmu.mapping = |
| 3137 | arm_iommu_create_mapping(&platform_bus_type, |
Tharun Kumar Merugu | ca183f9 | 2017-04-27 17:43:27 +0530 | [diff] [blame] | 3138 | start, 0x78000000))); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3139 | if (err) |
| 3140 | goto bail; |
| 3141 | |
| 3142 | if (sess->smmu.secure) |
| 3143 | iommu_domain_set_attr(sess->smmu.mapping->domain, |
| 3144 | DOMAIN_ATTR_SECURE_VMID, |
| 3145 | &secure_vmid); |
| 3146 | |
| 3147 | VERIFY(err, !arm_iommu_attach_device(dev, sess->smmu.mapping)); |
| 3148 | if (err) |
| 3149 | goto bail; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3150 | sess->smmu.dev = dev; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3151 | sess->smmu.enabled = 1; |
| 3152 | chan->sesscount++; |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3153 | debugfs_global_file = debugfs_create_file("global", 0644, debugfs_root, |
| 3154 | NULL, &debugfs_fops); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3155 | bail: |
| 3156 | return err; |
| 3157 | } |
| 3158 | |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 3159 | static void init_secure_vmid_list(struct device *dev, char *prop_name, |
| 3160 | struct secure_vm *destvm) |
| 3161 | { |
| 3162 | int err = 0; |
| 3163 | u32 len = 0, i = 0; |
| 3164 | u32 *rhvmlist = NULL; |
| 3165 | u32 *rhvmpermlist = NULL; |
| 3166 | |
| 3167 | if (!of_find_property(dev->of_node, prop_name, &len)) |
| 3168 | goto bail; |
| 3169 | if (len == 0) |
| 3170 | goto bail; |
| 3171 | len /= sizeof(u32); |
| 3172 | VERIFY(err, NULL != (rhvmlist = kcalloc(len, sizeof(u32), GFP_KERNEL))); |
| 3173 | if (err) |
| 3174 | goto bail; |
| 3175 | VERIFY(err, NULL != (rhvmpermlist = kcalloc(len, sizeof(u32), |
| 3176 | GFP_KERNEL))); |
| 3177 | if (err) |
| 3178 | goto bail; |
| 3179 | for (i = 0; i < len; i++) { |
| 3180 | err = of_property_read_u32_index(dev->of_node, prop_name, i, |
| 3181 | &rhvmlist[i]); |
| 3182 | rhvmpermlist[i] = PERM_READ | PERM_WRITE | PERM_EXEC; |
| 3183 | pr_info("ADSPRPC: Secure VMID = %d", rhvmlist[i]); |
| 3184 | if (err) { |
| 3185 | pr_err("ADSPRPC: Failed to read VMID\n"); |
| 3186 | goto bail; |
| 3187 | } |
| 3188 | } |
| 3189 | destvm->vmid = rhvmlist; |
| 3190 | destvm->vmperm = rhvmpermlist; |
| 3191 | destvm->vmcount = len; |
| 3192 | bail: |
| 3193 | if (err) { |
| 3194 | kfree(rhvmlist); |
| 3195 | kfree(rhvmpermlist); |
| 3196 | } |
| 3197 | } |
| 3198 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3199 | static int fastrpc_probe(struct platform_device *pdev) |
| 3200 | { |
| 3201 | int err = 0; |
| 3202 | struct fastrpc_apps *me = &gfa; |
| 3203 | struct device *dev = &pdev->dev; |
| 3204 | struct smq_phy_page range; |
| 3205 | struct device_node *ion_node, *node; |
| 3206 | struct platform_device *ion_pdev; |
| 3207 | struct cma *cma; |
| 3208 | uint32_t val; |
| 3209 | |
c_mtharu | 63ffc01 | 2017-11-16 15:26:56 +0530 | [diff] [blame] | 3210 | |
| 3211 | if (of_device_is_compatible(dev->of_node, |
| 3212 | "qcom,msm-fastrpc-compute")) { |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 3213 | init_secure_vmid_list(dev, "qcom,adsp-remoteheap-vmid", |
| 3214 | &gcinfo[0].rhvm); |
c_mtharu | 63ffc01 | 2017-11-16 15:26:56 +0530 | [diff] [blame] | 3215 | |
c_mtharu | 63ffc01 | 2017-11-16 15:26:56 +0530 | [diff] [blame] | 3216 | |
| 3217 | of_property_read_u32(dev->of_node, "qcom,rpc-latency-us", |
| 3218 | &me->latency); |
| 3219 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3220 | if (of_device_is_compatible(dev->of_node, |
| 3221 | "qcom,msm-fastrpc-compute-cb")) |
| 3222 | return fastrpc_cb_probe(dev); |
| 3223 | |
| 3224 | if (of_device_is_compatible(dev->of_node, |
| 3225 | "qcom,msm-adsprpc-mem-region")) { |
| 3226 | me->dev = dev; |
| 3227 | range.addr = 0; |
| 3228 | ion_node = of_find_compatible_node(NULL, NULL, "qcom,msm-ion"); |
| 3229 | if (ion_node) { |
| 3230 | for_each_available_child_of_node(ion_node, node) { |
| 3231 | if (of_property_read_u32(node, "reg", &val)) |
| 3232 | continue; |
| 3233 | if (val != ION_ADSP_HEAP_ID) |
| 3234 | continue; |
| 3235 | ion_pdev = of_find_device_by_node(node); |
| 3236 | if (!ion_pdev) |
| 3237 | break; |
| 3238 | cma = dev_get_cma_area(&ion_pdev->dev); |
| 3239 | if (cma) { |
| 3240 | range.addr = cma_get_base(cma); |
| 3241 | range.size = (size_t)cma_get_size(cma); |
| 3242 | } |
| 3243 | break; |
| 3244 | } |
| 3245 | } |
| 3246 | if (range.addr) { |
| 3247 | int srcVM[1] = {VMID_HLOS}; |
| 3248 | int destVM[4] = {VMID_HLOS, VMID_MSS_MSA, VMID_SSC_Q6, |
| 3249 | VMID_ADSP_Q6}; |
Sathish Ambley | 84d1186 | 2017-05-15 14:36:05 -0700 | [diff] [blame] | 3250 | int destVMperm[4] = {PERM_READ | PERM_WRITE | PERM_EXEC, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3251 | PERM_READ | PERM_WRITE | PERM_EXEC, |
| 3252 | PERM_READ | PERM_WRITE | PERM_EXEC, |
| 3253 | PERM_READ | PERM_WRITE | PERM_EXEC, |
| 3254 | }; |
| 3255 | |
| 3256 | VERIFY(err, !hyp_assign_phys(range.addr, range.size, |
| 3257 | srcVM, 1, destVM, destVMperm, 4)); |
| 3258 | if (err) |
| 3259 | goto bail; |
| 3260 | } |
| 3261 | return 0; |
| 3262 | } |
| 3263 | |
| 3264 | VERIFY(err, !of_platform_populate(pdev->dev.of_node, |
| 3265 | fastrpc_match_table, |
| 3266 | NULL, &pdev->dev)); |
| 3267 | if (err) |
| 3268 | goto bail; |
| 3269 | bail: |
| 3270 | return err; |
| 3271 | } |
| 3272 | |
| 3273 | static void fastrpc_deinit(void) |
| 3274 | { |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3275 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3276 | struct fastrpc_channel_ctx *chan = gcinfo; |
| 3277 | int i, j; |
| 3278 | |
| 3279 | for (i = 0; i < NUM_CHANNELS; i++, chan++) { |
| 3280 | if (chan->chan) { |
| 3281 | kref_put_mutex(&chan->kref, |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3282 | fastrpc_channel_close, &me->smd_mutex); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3283 | chan->chan = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3284 | } |
| 3285 | for (j = 0; j < NUM_SESSIONS; j++) { |
| 3286 | struct fastrpc_session_ctx *sess = &chan->session[j]; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3287 | if (sess->smmu.dev) { |
| 3288 | arm_iommu_detach_device(sess->smmu.dev); |
| 3289 | sess->smmu.dev = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3290 | } |
| 3291 | if (sess->smmu.mapping) { |
| 3292 | arm_iommu_release_mapping(sess->smmu.mapping); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3293 | sess->smmu.mapping = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3294 | } |
| 3295 | } |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 3296 | kfree(chan->rhvm.vmid); |
| 3297 | kfree(chan->rhvm.vmperm); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3298 | } |
| 3299 | } |
| 3300 | |
| 3301 | static struct platform_driver fastrpc_driver = { |
| 3302 | .probe = fastrpc_probe, |
| 3303 | .driver = { |
| 3304 | .name = "fastrpc", |
| 3305 | .owner = THIS_MODULE, |
| 3306 | .of_match_table = fastrpc_match_table, |
| 3307 | }, |
| 3308 | }; |
| 3309 | |
| 3310 | static int __init fastrpc_device_init(void) |
| 3311 | { |
| 3312 | struct fastrpc_apps *me = &gfa; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3313 | struct device *dev = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3314 | int err = 0, i; |
| 3315 | |
| 3316 | memset(me, 0, sizeof(*me)); |
| 3317 | |
| 3318 | fastrpc_init(me); |
| 3319 | me->dev = NULL; |
| 3320 | VERIFY(err, 0 == platform_driver_register(&fastrpc_driver)); |
| 3321 | if (err) |
| 3322 | goto register_bail; |
| 3323 | VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, NUM_CHANNELS, |
| 3324 | DEVICE_NAME)); |
| 3325 | if (err) |
| 3326 | goto alloc_chrdev_bail; |
| 3327 | cdev_init(&me->cdev, &fops); |
| 3328 | me->cdev.owner = THIS_MODULE; |
| 3329 | VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0), |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3330 | 1)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3331 | if (err) |
| 3332 | goto cdev_init_bail; |
| 3333 | me->class = class_create(THIS_MODULE, "fastrpc"); |
| 3334 | VERIFY(err, !IS_ERR(me->class)); |
| 3335 | if (err) |
| 3336 | goto class_create_bail; |
| 3337 | me->compat = (fops.compat_ioctl == NULL) ? 0 : 1; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3338 | dev = device_create(me->class, NULL, |
| 3339 | MKDEV(MAJOR(me->dev_no), 0), |
| 3340 | NULL, gcinfo[0].name); |
| 3341 | VERIFY(err, !IS_ERR_OR_NULL(dev)); |
| 3342 | if (err) |
| 3343 | goto device_create_bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3344 | for (i = 0; i < NUM_CHANNELS; i++) { |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3345 | me->channel[i].dev = dev; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3346 | me->channel[i].ssrcount = 0; |
| 3347 | me->channel[i].prevssrcount = 0; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3348 | me->channel[i].issubsystemup = 1; |
| 3349 | me->channel[i].ramdumpenabled = 0; |
| 3350 | me->channel[i].remoteheap_ramdump_dev = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3351 | me->channel[i].nb.notifier_call = fastrpc_restart_notifier_cb; |
| 3352 | me->channel[i].handle = subsys_notif_register_notifier( |
| 3353 | gcinfo[i].subsys, |
| 3354 | &me->channel[i].nb); |
| 3355 | } |
| 3356 | |
| 3357 | me->client = msm_ion_client_create(DEVICE_NAME); |
| 3358 | VERIFY(err, !IS_ERR_OR_NULL(me->client)); |
| 3359 | if (err) |
| 3360 | goto device_create_bail; |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3361 | debugfs_root = debugfs_create_dir("adsprpc", NULL); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3362 | return 0; |
| 3363 | device_create_bail: |
| 3364 | for (i = 0; i < NUM_CHANNELS; i++) { |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3365 | if (me->channel[i].handle) |
| 3366 | subsys_notif_unregister_notifier(me->channel[i].handle, |
| 3367 | &me->channel[i].nb); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3368 | } |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3369 | if (!IS_ERR_OR_NULL(dev)) |
| 3370 | device_destroy(me->class, MKDEV(MAJOR(me->dev_no), 0)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3371 | class_destroy(me->class); |
| 3372 | class_create_bail: |
| 3373 | cdev_del(&me->cdev); |
| 3374 | cdev_init_bail: |
| 3375 | unregister_chrdev_region(me->dev_no, NUM_CHANNELS); |
| 3376 | alloc_chrdev_bail: |
| 3377 | register_bail: |
| 3378 | fastrpc_deinit(); |
| 3379 | return err; |
| 3380 | } |
| 3381 | |
| 3382 | static void __exit fastrpc_device_exit(void) |
| 3383 | { |
| 3384 | struct fastrpc_apps *me = &gfa; |
| 3385 | int i; |
| 3386 | |
| 3387 | fastrpc_file_list_dtor(me); |
| 3388 | fastrpc_deinit(); |
| 3389 | for (i = 0; i < NUM_CHANNELS; i++) { |
| 3390 | if (!gcinfo[i].name) |
| 3391 | continue; |
| 3392 | device_destroy(me->class, MKDEV(MAJOR(me->dev_no), i)); |
| 3393 | subsys_notif_unregister_notifier(me->channel[i].handle, |
| 3394 | &me->channel[i].nb); |
| 3395 | } |
| 3396 | class_destroy(me->class); |
| 3397 | cdev_del(&me->cdev); |
| 3398 | unregister_chrdev_region(me->dev_no, NUM_CHANNELS); |
| 3399 | ion_client_destroy(me->client); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3400 | debugfs_remove_recursive(debugfs_root); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3401 | } |
| 3402 | |
| 3403 | late_initcall(fastrpc_device_init); |
| 3404 | module_exit(fastrpc_device_exit); |
| 3405 | |
| 3406 | MODULE_LICENSE("GPL v2"); |