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