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