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