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