Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012, The Linux Foundation. All rights reserved. |
| 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 | */ |
Mitchel Humpherys | bffc579 | 2013-02-06 12:03:20 -0800 | [diff] [blame^] | 14 | #include "adsprpc_shared.h" |
| 15 | |
| 16 | #ifdef __KERNEL__ |
| 17 | |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/completion.h> |
| 20 | #include <linux/pagemap.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/sched.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/cdev.h> |
| 26 | #include <linux/list.h> |
| 27 | #include <linux/hash.h> |
| 28 | #include <linux/msm_ion.h> |
| 29 | #include <mach/msm_smd.h> |
| 30 | #include <mach/ion.h> |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 31 | #include <linux/scatterlist.h> |
Mitchel Humpherys | bffc579 | 2013-02-06 12:03:20 -0800 | [diff] [blame^] | 32 | #include <linux/fs.h> |
| 33 | #include <linux/uaccess.h> |
| 34 | #include <linux/device.h> |
| 35 | |
| 36 | #ifndef ION_ADSPRPC_HEAP_ID |
| 37 | #define ION_ADSPRPC_HEAP_ID ION_AUDIO_HEAP_ID |
| 38 | #endif /*ION_ADSPRPC_HEAP_ID*/ |
| 39 | |
| 40 | #define RPC_TIMEOUT (5 * HZ) |
| 41 | #define RPC_HASH_BITS 5 |
| 42 | #define RPC_HASH_SZ (1 << RPC_HASH_BITS) |
| 43 | #define BALIGN 32 |
| 44 | |
| 45 | #define LOCK_MMAP(kernel)\ |
| 46 | do {\ |
| 47 | if (!kernel)\ |
| 48 | down_read(¤t->mm->mmap_sem);\ |
| 49 | } while (0) |
| 50 | |
| 51 | #define UNLOCK_MMAP(kernel)\ |
| 52 | do {\ |
| 53 | if (!kernel)\ |
| 54 | up_read(¤t->mm->mmap_sem);\ |
| 55 | } while (0) |
| 56 | |
| 57 | |
| 58 | static inline uint32_t buf_page_start(void *buf) |
| 59 | { |
| 60 | uint32_t start = (uint32_t) buf & PAGE_MASK; |
| 61 | return start; |
| 62 | } |
| 63 | |
| 64 | static inline uint32_t buf_page_offset(void *buf) |
| 65 | { |
| 66 | uint32_t offset = (uint32_t) buf & (PAGE_SIZE - 1); |
| 67 | return offset; |
| 68 | } |
| 69 | |
| 70 | static inline int buf_num_pages(void *buf, int len) |
| 71 | { |
| 72 | uint32_t start = buf_page_start(buf) >> PAGE_SHIFT; |
| 73 | uint32_t end = (((uint32_t) buf + len - 1) & PAGE_MASK) >> PAGE_SHIFT; |
| 74 | int nPages = end - start + 1; |
| 75 | return nPages; |
| 76 | } |
| 77 | |
| 78 | static inline uint32_t buf_page_size(uint32_t size) |
| 79 | { |
| 80 | uint32_t sz = (size + (PAGE_SIZE - 1)) & PAGE_MASK; |
| 81 | return sz > PAGE_SIZE ? sz : PAGE_SIZE; |
| 82 | } |
| 83 | |
| 84 | static inline int buf_get_pages(void *addr, int sz, int nr_pages, int access, |
| 85 | struct smq_phy_page *pages, int nr_elems) |
| 86 | { |
| 87 | struct vm_area_struct *vma; |
| 88 | uint32_t start = buf_page_start(addr); |
| 89 | uint32_t len = nr_pages << PAGE_SHIFT; |
| 90 | unsigned long pfn; |
| 91 | int n = -1, err = 0; |
| 92 | |
| 93 | VERIFY(err, 0 != access_ok(access ? VERIFY_WRITE : VERIFY_READ, |
| 94 | (void __user *)start, len)); |
| 95 | if (err) |
| 96 | goto bail; |
| 97 | VERIFY(err, 0 != (vma = find_vma(current->mm, start))); |
| 98 | if (err) |
| 99 | goto bail; |
| 100 | VERIFY(err, ((uint32_t)addr + sz) <= vma->vm_end); |
| 101 | if (err) |
| 102 | goto bail; |
| 103 | n = 0; |
| 104 | VERIFY(err, 0 == follow_pfn(vma, start, &pfn)); |
| 105 | if (err) |
| 106 | goto bail; |
| 107 | VERIFY(err, nr_elems > 0); |
| 108 | if (err) |
| 109 | goto bail; |
| 110 | pages->addr = __pfn_to_phys(pfn); |
| 111 | pages->size = len; |
| 112 | n++; |
| 113 | bail: |
| 114 | return n; |
| 115 | } |
| 116 | |
| 117 | #endif /*__KERNEL__*/ |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 118 | |
| 119 | struct smq_invoke_ctx { |
| 120 | struct completion work; |
| 121 | int retval; |
| 122 | atomic_t free; |
| 123 | }; |
| 124 | |
| 125 | struct smq_context_list { |
| 126 | struct smq_invoke_ctx *ls; |
| 127 | int size; |
| 128 | int last; |
| 129 | }; |
| 130 | |
| 131 | struct fastrpc_apps { |
| 132 | smd_channel_t *chan; |
| 133 | struct smq_context_list clst; |
| 134 | struct completion work; |
| 135 | struct ion_client *iclient; |
| 136 | struct cdev cdev; |
| 137 | dev_t dev_no; |
| 138 | spinlock_t wrlock; |
| 139 | spinlock_t hlock; |
| 140 | struct hlist_head htbl[RPC_HASH_SZ]; |
| 141 | }; |
| 142 | |
| 143 | struct fastrpc_buf { |
| 144 | struct ion_handle *handle; |
| 145 | void *virt; |
| 146 | ion_phys_addr_t phys; |
| 147 | int size; |
| 148 | int used; |
| 149 | }; |
| 150 | |
| 151 | struct fastrpc_device { |
| 152 | uint32_t tgid; |
| 153 | struct hlist_node hn; |
| 154 | struct fastrpc_buf buf; |
| 155 | }; |
| 156 | |
| 157 | static struct fastrpc_apps gfa; |
| 158 | |
| 159 | static void free_mem(struct fastrpc_buf *buf) |
| 160 | { |
| 161 | struct fastrpc_apps *me = &gfa; |
| 162 | |
| 163 | if (buf->handle) { |
| 164 | if (buf->virt) { |
| 165 | ion_unmap_kernel(me->iclient, buf->handle); |
| 166 | buf->virt = 0; |
| 167 | } |
| 168 | ion_free(me->iclient, buf->handle); |
| 169 | buf->handle = 0; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | static int alloc_mem(struct fastrpc_buf *buf) |
| 174 | { |
| 175 | struct ion_client *clnt = gfa.iclient; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 176 | struct sg_table *sg; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 177 | int err = 0; |
| 178 | |
| 179 | buf->handle = ion_alloc(clnt, buf->size, SZ_4K, |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 180 | ION_HEAP(ION_AUDIO_HEAP_ID), 0); |
| 181 | VERIFY(err, 0 == IS_ERR_OR_NULL(buf->handle)); |
| 182 | if (err) |
| 183 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 184 | buf->virt = 0; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 185 | VERIFY(err, 0 != (buf->virt = ion_map_kernel(clnt, buf->handle))); |
| 186 | if (err) |
| 187 | goto bail; |
| 188 | VERIFY(err, 0 != (sg = ion_sg_table(clnt, buf->handle))); |
| 189 | if (err) |
| 190 | goto bail; |
| 191 | VERIFY(err, 1 == sg->nents); |
| 192 | if (err) |
| 193 | goto bail; |
| 194 | buf->phys = sg_dma_address(sg->sgl); |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 195 | bail: |
| 196 | if (err && !IS_ERR_OR_NULL(buf->handle)) |
| 197 | free_mem(buf); |
| 198 | return err; |
| 199 | } |
| 200 | |
| 201 | static int context_list_ctor(struct smq_context_list *me, int size) |
| 202 | { |
| 203 | int err = 0; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 204 | VERIFY(err, 0 != (me->ls = kzalloc(size, GFP_KERNEL))); |
| 205 | if (err) |
| 206 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 207 | me->size = size / sizeof(*me->ls); |
| 208 | me->last = 0; |
| 209 | bail: |
| 210 | return err; |
| 211 | } |
| 212 | |
| 213 | static void context_list_dtor(struct smq_context_list *me) |
| 214 | { |
| 215 | kfree(me->ls); |
| 216 | me->ls = 0; |
| 217 | } |
| 218 | |
| 219 | static void context_list_alloc_ctx(struct smq_context_list *me, |
| 220 | struct smq_invoke_ctx **po) |
| 221 | { |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 222 | int i = me->last; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 223 | struct smq_invoke_ctx *ctx; |
| 224 | |
| 225 | for (;;) { |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 226 | i = i % me->size; |
| 227 | ctx = &me->ls[i]; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 228 | if (atomic_read(&ctx->free) == 0) |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 229 | if (atomic_cmpxchg(&ctx->free, 0, 1) == 0) |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 230 | break; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 231 | i++; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 232 | } |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 233 | me->last = i; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 234 | ctx->retval = -1; |
| 235 | init_completion(&ctx->work); |
| 236 | *po = ctx; |
| 237 | } |
| 238 | |
| 239 | static void context_free(struct smq_invoke_ctx *me) |
| 240 | { |
| 241 | if (me) |
| 242 | atomic_set(&me->free, 0); |
| 243 | } |
| 244 | |
| 245 | static void context_notify_user(struct smq_invoke_ctx *me, int retval) |
| 246 | { |
| 247 | me->retval = retval; |
| 248 | complete(&me->work); |
| 249 | } |
| 250 | |
| 251 | static void context_notify_all_users(struct smq_context_list *me) |
| 252 | { |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 253 | int i; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 254 | |
| 255 | if (!me->ls) |
| 256 | return; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 257 | for (i = 0; i < me->size; ++i) { |
| 258 | if (atomic_read(&me->ls[i].free) != 0) |
| 259 | complete(&me->ls[i].work); |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
| 263 | static int get_page_list(uint32_t kernel, uint32_t sc, remote_arg_t *pra, |
| 264 | struct fastrpc_buf *ibuf, struct fastrpc_buf *obuf) |
| 265 | { |
| 266 | struct smq_phy_page *pgstart, *pages; |
| 267 | struct smq_invoke_buf *list; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 268 | int i, rlen, err = 0; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 269 | int inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 270 | int outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
| 271 | |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 272 | LOCK_MMAP(kernel); |
| 273 | *obuf = *ibuf; |
| 274 | retry: |
| 275 | list = smq_invoke_buf_start((remote_arg_t *)obuf->virt, sc); |
| 276 | pgstart = smq_phy_page_start(sc, list); |
| 277 | pages = pgstart + 1; |
| 278 | rlen = obuf->size - ((uint32_t)pages - (uint32_t)obuf->virt); |
| 279 | if (rlen < 0) { |
| 280 | rlen = ((uint32_t)pages - (uint32_t)obuf->virt) - obuf->size; |
| 281 | obuf->size += buf_page_size(rlen); |
| 282 | obuf->handle = 0; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 283 | VERIFY(err, 0 == alloc_mem(obuf)); |
| 284 | if (err) |
| 285 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 286 | goto retry; |
| 287 | } |
| 288 | pgstart->addr = obuf->phys; |
| 289 | pgstart->size = obuf->size; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 290 | for (i = 0; i < inbufs + outbufs; ++i) { |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 291 | void *buf; |
| 292 | int len, num; |
| 293 | |
Mitchel Humpherys | 6873f4b | 2012-10-19 11:29:36 -0700 | [diff] [blame] | 294 | list[i].num = 0; |
| 295 | list[i].pgidx = 0; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 296 | len = pra[i].buf.len; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 297 | if (!len) |
| 298 | continue; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 299 | buf = pra[i].buf.pv; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 300 | num = buf_num_pages(buf, len); |
| 301 | if (!kernel) |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 302 | list[i].num = buf_get_pages(buf, len, num, |
| 303 | i >= inbufs, pages, rlen / sizeof(*pages)); |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 304 | else |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 305 | list[i].num = 0; |
| 306 | VERIFY(err, list[i].num >= 0); |
| 307 | if (err) |
| 308 | goto bail; |
| 309 | if (list[i].num) { |
| 310 | list[i].pgidx = pages - pgstart; |
| 311 | pages = pages + list[i].num; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 312 | } else if (rlen > sizeof(*pages)) { |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 313 | list[i].pgidx = pages - pgstart; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 314 | pages = pages + 1; |
| 315 | } else { |
| 316 | if (obuf->handle != ibuf->handle) |
| 317 | free_mem(obuf); |
| 318 | obuf->size += buf_page_size(sizeof(*pages)); |
| 319 | obuf->handle = 0; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 320 | VERIFY(err, 0 == alloc_mem(obuf)); |
| 321 | if (err) |
| 322 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 323 | goto retry; |
| 324 | } |
| 325 | rlen = obuf->size - ((uint32_t) pages - (uint32_t) obuf->virt); |
| 326 | } |
| 327 | obuf->used = obuf->size - rlen; |
| 328 | bail: |
| 329 | if (err && (obuf->handle != ibuf->handle)) |
| 330 | free_mem(obuf); |
| 331 | UNLOCK_MMAP(kernel); |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 332 | return err; |
| 333 | } |
| 334 | |
| 335 | static int get_args(uint32_t kernel, uint32_t sc, remote_arg_t *pra, |
| 336 | remote_arg_t *rpra, remote_arg_t *upra, |
| 337 | struct fastrpc_buf *ibuf, struct fastrpc_buf **abufs, |
| 338 | int *nbufs) |
| 339 | { |
| 340 | struct smq_invoke_buf *list; |
| 341 | struct fastrpc_buf *pbuf = ibuf, *obufs = 0; |
| 342 | struct smq_phy_page *pages; |
| 343 | void *args; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 344 | int i, rlen, size, used, inh, bufs = 0, err = 0; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 345 | int inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 346 | int outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
| 347 | |
| 348 | list = smq_invoke_buf_start(rpra, sc); |
| 349 | pages = smq_phy_page_start(sc, list); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 350 | used = ALIGN(pbuf->used, BALIGN); |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 351 | args = (void *)((char *)pbuf->virt + used); |
| 352 | rlen = pbuf->size - used; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 353 | for (i = 0; i < inbufs + outbufs; ++i) { |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 354 | int num; |
| 355 | |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 356 | rpra[i].buf.len = pra[i].buf.len; |
Mitchel Humpherys | 6873f4b | 2012-10-19 11:29:36 -0700 | [diff] [blame] | 357 | if (!rpra[i].buf.len) |
| 358 | continue; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 359 | if (list[i].num) { |
| 360 | rpra[i].buf.pv = pra[i].buf.pv; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 361 | continue; |
| 362 | } |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 363 | if (rlen < pra[i].buf.len) { |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 364 | struct fastrpc_buf *b; |
| 365 | pbuf->used = pbuf->size - rlen; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 366 | VERIFY(err, 0 != (b = krealloc(obufs, |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 367 | (bufs + 1) * sizeof(*obufs), GFP_KERNEL))); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 368 | if (err) |
| 369 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 370 | obufs = b; |
| 371 | pbuf = obufs + bufs; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 372 | pbuf->size = buf_num_pages(0, pra[i].buf.len) * |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 373 | PAGE_SIZE; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 374 | VERIFY(err, 0 == alloc_mem(pbuf)); |
| 375 | if (err) |
| 376 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 377 | bufs++; |
| 378 | args = pbuf->virt; |
| 379 | rlen = pbuf->size; |
| 380 | } |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 381 | num = buf_num_pages(args, pra[i].buf.len); |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 382 | if (pbuf == ibuf) { |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 383 | list[i].num = num; |
| 384 | list[i].pgidx = 0; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 385 | } else { |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 386 | list[i].num = 1; |
| 387 | pages[list[i].pgidx].addr = |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 388 | buf_page_start((void *)(pbuf->phys + |
| 389 | (pbuf->size - rlen))); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 390 | pages[list[i].pgidx].size = |
| 391 | buf_page_size(pra[i].buf.len); |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 392 | } |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 393 | if (i < inbufs) { |
| 394 | if (!kernel) { |
| 395 | VERIFY(err, 0 == copy_from_user(args, |
| 396 | pra[i].buf.pv, pra[i].buf.len)); |
| 397 | if (err) |
| 398 | goto bail; |
| 399 | } else { |
| 400 | memmove(args, pra[i].buf.pv, pra[i].buf.len); |
| 401 | } |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 402 | } |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 403 | rpra[i].buf.pv = args; |
| 404 | args = (void *)((char *)args + ALIGN(pra[i].buf.len, BALIGN)); |
| 405 | rlen -= ALIGN(pra[i].buf.len, BALIGN); |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 406 | } |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 407 | for (i = 0; i < inbufs; ++i) { |
| 408 | if (rpra[i].buf.len) |
| 409 | dmac_flush_range(rpra[i].buf.pv, |
| 410 | (char *)rpra[i].buf.pv + rpra[i].buf.len); |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 411 | } |
| 412 | pbuf->used = pbuf->size - rlen; |
| 413 | size = sizeof(*rpra) * REMOTE_SCALARS_INHANDLES(sc); |
| 414 | if (size) { |
| 415 | inh = inbufs + outbufs; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 416 | if (!kernel) { |
| 417 | VERIFY(err, 0 == copy_from_user(&rpra[inh], &upra[inh], |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 418 | size)); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 419 | if (err) |
| 420 | goto bail; |
| 421 | } else { |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 422 | memmove(&rpra[inh], &upra[inh], size); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 423 | } |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 424 | } |
| 425 | dmac_flush_range(rpra, (char *)rpra + used); |
| 426 | bail: |
| 427 | *abufs = obufs; |
| 428 | *nbufs = bufs; |
| 429 | return err; |
| 430 | } |
| 431 | |
| 432 | static int put_args(uint32_t kernel, uint32_t sc, remote_arg_t *pra, |
| 433 | remote_arg_t *rpra, remote_arg_t *upra) |
| 434 | { |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 435 | int i, inbufs, outbufs, outh, size; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 436 | int err = 0; |
| 437 | |
| 438 | inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 439 | outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 440 | for (i = inbufs; i < inbufs + outbufs; ++i) { |
| 441 | if (rpra[i].buf.pv != pra[i].buf.pv) { |
| 442 | VERIFY(err, 0 == copy_to_user(pra[i].buf.pv, |
| 443 | rpra[i].buf.pv, rpra[i].buf.len)); |
| 444 | if (err) |
| 445 | goto bail; |
| 446 | } |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 447 | } |
| 448 | size = sizeof(*rpra) * REMOTE_SCALARS_OUTHANDLES(sc); |
| 449 | if (size) { |
| 450 | outh = inbufs + outbufs + REMOTE_SCALARS_INHANDLES(sc); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 451 | if (!kernel) { |
| 452 | VERIFY(err, 0 == copy_to_user(&upra[outh], &rpra[outh], |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 453 | size)); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 454 | if (err) |
| 455 | goto bail; |
| 456 | } else { |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 457 | memmove(&upra[outh], &rpra[outh], size); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 458 | } |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 459 | } |
| 460 | bail: |
| 461 | return err; |
| 462 | } |
| 463 | |
| 464 | static void inv_args(uint32_t sc, remote_arg_t *rpra, int used) |
| 465 | { |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 466 | int i, inbufs, outbufs; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 467 | int inv = 0; |
| 468 | |
| 469 | inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 470 | outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 471 | for (i = inbufs; i < inbufs + outbufs; ++i) { |
| 472 | if (buf_page_start(rpra) == buf_page_start(rpra[i].buf.pv)) |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 473 | inv = 1; |
Mitchel Humpherys | 6873f4b | 2012-10-19 11:29:36 -0700 | [diff] [blame] | 474 | else if (rpra[i].buf.len) |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 475 | dmac_inv_range(rpra[i].buf.pv, |
| 476 | (char *)rpra[i].buf.pv + rpra[i].buf.len); |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | if (inv || REMOTE_SCALARS_OUTHANDLES(sc)) |
| 480 | dmac_inv_range(rpra, (char *)rpra + used); |
| 481 | } |
| 482 | |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 483 | static int fastrpc_invoke_send(struct fastrpc_apps *me, uint32_t handle, |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 484 | uint32_t sc, struct smq_invoke_ctx *ctx, |
| 485 | struct fastrpc_buf *buf) |
| 486 | { |
| 487 | struct smq_msg msg; |
| 488 | int err = 0, len; |
| 489 | |
| 490 | msg.pid = current->tgid; |
| 491 | msg.tid = current->pid; |
| 492 | msg.invoke.header.ctx = ctx; |
| 493 | msg.invoke.header.handle = handle; |
| 494 | msg.invoke.header.sc = sc; |
| 495 | msg.invoke.page.addr = buf->phys; |
| 496 | msg.invoke.page.size = buf_page_size(buf->used); |
| 497 | spin_lock(&me->wrlock); |
| 498 | len = smd_write(me->chan, &msg, sizeof(msg)); |
| 499 | spin_unlock(&me->wrlock); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 500 | VERIFY(err, len == sizeof(msg)); |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 501 | return err; |
| 502 | } |
| 503 | |
| 504 | static void fastrpc_deinit(void) |
| 505 | { |
| 506 | struct fastrpc_apps *me = &gfa; |
| 507 | |
| 508 | if (me->chan) |
| 509 | (void)smd_close(me->chan); |
| 510 | context_list_dtor(&me->clst); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 511 | if (me->iclient) |
| 512 | ion_client_destroy(me->iclient); |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 513 | me->iclient = 0; |
| 514 | me->chan = 0; |
| 515 | } |
| 516 | |
| 517 | static void fastrpc_read_handler(void) |
| 518 | { |
| 519 | struct fastrpc_apps *me = &gfa; |
| 520 | struct smq_invoke_rsp rsp; |
| 521 | int err = 0; |
| 522 | |
| 523 | do { |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 524 | VERIFY(err, sizeof(rsp) == |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 525 | smd_read_from_cb(me->chan, &rsp, sizeof(rsp))); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 526 | if (err) |
| 527 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 528 | context_notify_user(rsp.ctx, rsp.retval); |
| 529 | } while (!err); |
| 530 | bail: |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | static void smd_event_handler(void *priv, unsigned event) |
| 535 | { |
| 536 | struct fastrpc_apps *me = (struct fastrpc_apps *)priv; |
| 537 | |
| 538 | switch (event) { |
| 539 | case SMD_EVENT_OPEN: |
| 540 | complete(&(me->work)); |
| 541 | break; |
| 542 | case SMD_EVENT_CLOSE: |
| 543 | context_notify_all_users(&me->clst); |
| 544 | break; |
| 545 | case SMD_EVENT_DATA: |
| 546 | fastrpc_read_handler(); |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | static int fastrpc_init(void) |
| 552 | { |
| 553 | int err = 0; |
| 554 | struct fastrpc_apps *me = &gfa; |
| 555 | |
| 556 | if (me->chan == 0) { |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 557 | int i; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 558 | spin_lock_init(&me->hlock); |
| 559 | spin_lock_init(&me->wrlock); |
| 560 | init_completion(&me->work); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 561 | for (i = 0; i < RPC_HASH_SZ; ++i) |
| 562 | INIT_HLIST_HEAD(&me->htbl[i]); |
| 563 | VERIFY(err, 0 == context_list_ctor(&me->clst, SZ_4K)); |
| 564 | if (err) |
| 565 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 566 | me->iclient = msm_ion_client_create(ION_HEAP_CARVEOUT_MASK, |
| 567 | DEVICE_NAME); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 568 | VERIFY(err, 0 == IS_ERR_OR_NULL(me->iclient)); |
| 569 | if (err) |
| 570 | goto bail; |
| 571 | VERIFY(err, 0 == smd_named_open_on_edge(FASTRPC_SMD_GUID, |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 572 | SMD_APPS_QDSP, &me->chan, |
| 573 | me, smd_event_handler)); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 574 | if (err) |
| 575 | goto bail; |
| 576 | VERIFY(err, 0 != wait_for_completion_timeout(&me->work, |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 577 | RPC_TIMEOUT)); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 578 | if (err) |
| 579 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 580 | } |
| 581 | bail: |
| 582 | if (err) |
| 583 | fastrpc_deinit(); |
| 584 | return err; |
| 585 | } |
| 586 | |
| 587 | static void free_dev(struct fastrpc_device *dev) |
| 588 | { |
| 589 | if (dev) { |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 590 | free_mem(&dev->buf); |
| 591 | kfree(dev); |
Mitchel Humpherys | bffc579 | 2013-02-06 12:03:20 -0800 | [diff] [blame^] | 592 | module_put(THIS_MODULE); |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
| 596 | static int alloc_dev(struct fastrpc_device **dev) |
| 597 | { |
| 598 | int err = 0; |
| 599 | struct fastrpc_device *fd = 0; |
| 600 | |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 601 | VERIFY(err, 0 != try_module_get(THIS_MODULE)); |
| 602 | if (err) |
| 603 | goto bail; |
| 604 | VERIFY(err, 0 != (fd = kzalloc(sizeof(*fd), GFP_KERNEL))); |
| 605 | if (err) |
| 606 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 607 | fd->buf.size = PAGE_SIZE; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 608 | VERIFY(err, 0 == alloc_mem(&fd->buf)); |
| 609 | if (err) |
| 610 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 611 | fd->tgid = current->tgid; |
| 612 | INIT_HLIST_NODE(&fd->hn); |
| 613 | *dev = fd; |
| 614 | bail: |
| 615 | if (err) |
| 616 | free_dev(fd); |
| 617 | return err; |
| 618 | } |
| 619 | |
| 620 | static int get_dev(struct fastrpc_apps *me, struct fastrpc_device **rdev) |
| 621 | { |
| 622 | struct hlist_head *head; |
| 623 | struct fastrpc_device *dev = 0; |
| 624 | struct hlist_node *n; |
| 625 | uint32_t h = hash_32(current->tgid, RPC_HASH_BITS); |
| 626 | int err = 0; |
| 627 | |
| 628 | spin_lock(&me->hlock); |
| 629 | head = &me->htbl[h]; |
| 630 | hlist_for_each_entry(dev, n, head, hn) { |
| 631 | if (dev->tgid == current->tgid) { |
| 632 | hlist_del(&dev->hn); |
| 633 | break; |
| 634 | } |
| 635 | } |
| 636 | spin_unlock(&me->hlock); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 637 | VERIFY(err, dev != 0); |
| 638 | if (err) |
| 639 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 640 | *rdev = dev; |
| 641 | bail: |
| 642 | if (err) { |
| 643 | free_dev(dev); |
| 644 | err = alloc_dev(rdev); |
| 645 | } |
| 646 | return err; |
| 647 | } |
| 648 | |
| 649 | static void add_dev(struct fastrpc_apps *me, struct fastrpc_device *dev) |
| 650 | { |
| 651 | struct hlist_head *head; |
| 652 | uint32_t h = hash_32(current->tgid, RPC_HASH_BITS); |
| 653 | |
| 654 | spin_lock(&me->hlock); |
| 655 | head = &me->htbl[h]; |
| 656 | hlist_add_head(&dev->hn, head); |
| 657 | spin_unlock(&me->hlock); |
| 658 | return; |
| 659 | } |
| 660 | |
| 661 | static int fastrpc_release_current_dsp_process(void); |
| 662 | |
| 663 | static int fastrpc_internal_invoke(struct fastrpc_apps *me, uint32_t kernel, |
| 664 | struct fastrpc_ioctl_invoke *invoke, remote_arg_t *pra) |
| 665 | { |
| 666 | remote_arg_t *rpra = 0; |
| 667 | struct fastrpc_device *dev = 0; |
| 668 | struct smq_invoke_ctx *ctx = 0; |
| 669 | struct fastrpc_buf obuf, *abufs = 0, *b; |
| 670 | int interrupted = 0; |
| 671 | uint32_t sc; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 672 | int i, nbufs = 0, err = 0; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 673 | |
| 674 | sc = invoke->sc; |
| 675 | obuf.handle = 0; |
| 676 | if (REMOTE_SCALARS_LENGTH(sc)) { |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 677 | VERIFY(err, 0 == get_dev(me, &dev)); |
| 678 | if (err) |
| 679 | goto bail; |
| 680 | VERIFY(err, 0 == get_page_list(kernel, sc, pra, &dev->buf, |
| 681 | &obuf)); |
| 682 | if (err) |
| 683 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 684 | rpra = (remote_arg_t *)obuf.virt; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 685 | VERIFY(err, 0 == get_args(kernel, sc, pra, rpra, invoke->pra, |
| 686 | &obuf, &abufs, &nbufs)); |
| 687 | if (err) |
| 688 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | context_list_alloc_ctx(&me->clst, &ctx); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 692 | VERIFY(err, 0 == fastrpc_invoke_send(me, invoke->handle, sc, ctx, |
| 693 | &obuf)); |
| 694 | if (err) |
| 695 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 696 | inv_args(sc, rpra, obuf.used); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 697 | VERIFY(err, 0 == (interrupted = |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 698 | wait_for_completion_interruptible(&ctx->work))); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 699 | if (err) |
| 700 | goto bail; |
| 701 | VERIFY(err, 0 == (err = ctx->retval)); |
| 702 | if (err) |
| 703 | goto bail; |
| 704 | VERIFY(err, 0 == put_args(kernel, sc, pra, rpra, invoke->pra)); |
| 705 | if (err) |
| 706 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 707 | bail: |
| 708 | if (interrupted) { |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 709 | if (!kernel) |
| 710 | (void)fastrpc_release_current_dsp_process(); |
| 711 | wait_for_completion(&ctx->work); |
| 712 | } |
| 713 | context_free(ctx); |
Mitchel Humpherys | bffc579 | 2013-02-06 12:03:20 -0800 | [diff] [blame^] | 714 | |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 715 | for (i = 0, b = abufs; i < nbufs; ++i, ++b) |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 716 | free_mem(b); |
Mitchel Humpherys | bffc579 | 2013-02-06 12:03:20 -0800 | [diff] [blame^] | 717 | |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 718 | kfree(abufs); |
| 719 | if (dev) { |
| 720 | add_dev(me, dev); |
| 721 | if (obuf.handle != dev->buf.handle) |
| 722 | free_mem(&obuf); |
| 723 | } |
| 724 | return err; |
| 725 | } |
| 726 | |
| 727 | static int fastrpc_create_current_dsp_process(void) |
| 728 | { |
| 729 | int err = 0; |
| 730 | struct fastrpc_ioctl_invoke ioctl; |
| 731 | struct fastrpc_apps *me = &gfa; |
| 732 | remote_arg_t ra[1]; |
| 733 | int tgid = 0; |
| 734 | |
| 735 | tgid = current->tgid; |
| 736 | ra[0].buf.pv = &tgid; |
| 737 | ra[0].buf.len = sizeof(tgid); |
| 738 | ioctl.handle = 1; |
| 739 | ioctl.sc = REMOTE_SCALARS_MAKE(0, 1, 0); |
| 740 | ioctl.pra = ra; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 741 | VERIFY(err, 0 == fastrpc_internal_invoke(me, 1, &ioctl, ra)); |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 742 | return err; |
| 743 | } |
| 744 | |
| 745 | static int fastrpc_release_current_dsp_process(void) |
| 746 | { |
| 747 | int err = 0; |
| 748 | struct fastrpc_apps *me = &gfa; |
| 749 | struct fastrpc_ioctl_invoke ioctl; |
| 750 | remote_arg_t ra[1]; |
| 751 | int tgid = 0; |
| 752 | |
| 753 | tgid = current->tgid; |
| 754 | ra[0].buf.pv = &tgid; |
| 755 | ra[0].buf.len = sizeof(tgid); |
| 756 | ioctl.handle = 1; |
| 757 | ioctl.sc = REMOTE_SCALARS_MAKE(1, 1, 0); |
| 758 | ioctl.pra = ra; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 759 | VERIFY(err, 0 == fastrpc_internal_invoke(me, 1, &ioctl, ra)); |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 760 | return err; |
| 761 | } |
| 762 | |
| 763 | static void cleanup_current_dev(void) |
| 764 | { |
| 765 | struct fastrpc_apps *me = &gfa; |
| 766 | uint32_t h = hash_32(current->tgid, RPC_HASH_BITS); |
| 767 | struct hlist_head *head; |
| 768 | struct hlist_node *pos; |
| 769 | struct fastrpc_device *dev; |
| 770 | |
| 771 | rnext: |
| 772 | dev = 0; |
| 773 | spin_lock(&me->hlock); |
| 774 | head = &me->htbl[h]; |
| 775 | hlist_for_each_entry(dev, pos, head, hn) { |
| 776 | if (dev->tgid == current->tgid) { |
| 777 | hlist_del(&dev->hn); |
| 778 | break; |
| 779 | } |
| 780 | } |
| 781 | spin_unlock(&me->hlock); |
| 782 | if (dev) { |
| 783 | free_dev(dev); |
| 784 | goto rnext; |
| 785 | } |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | static int fastrpc_device_release(struct inode *inode, struct file *file) |
| 790 | { |
| 791 | (void)fastrpc_release_current_dsp_process(); |
| 792 | cleanup_current_dev(); |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | static int fastrpc_device_open(struct inode *inode, struct file *filp) |
| 797 | { |
| 798 | int err = 0; |
| 799 | |
| 800 | if (0 != try_module_get(THIS_MODULE)) { |
| 801 | /* This call will cause a dev to be created |
| 802 | * which will addref this module |
| 803 | */ |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 804 | VERIFY(err, 0 == fastrpc_create_current_dsp_process()); |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 805 | if (err) |
| 806 | cleanup_current_dev(); |
| 807 | module_put(THIS_MODULE); |
| 808 | } |
| 809 | return err; |
| 810 | } |
| 811 | |
| 812 | |
| 813 | static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num, |
| 814 | unsigned long ioctl_param) |
| 815 | { |
| 816 | struct fastrpc_apps *me = &gfa; |
| 817 | struct fastrpc_ioctl_invoke invoke; |
| 818 | remote_arg_t *pra = 0; |
| 819 | void *param = (char *)ioctl_param; |
| 820 | int bufs, err = 0; |
| 821 | |
| 822 | switch (ioctl_num) { |
| 823 | case FASTRPC_IOCTL_INVOKE: |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 824 | VERIFY(err, 0 == copy_from_user(&invoke, param, |
| 825 | sizeof(invoke))); |
| 826 | if (err) |
| 827 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 828 | bufs = REMOTE_SCALARS_INBUFS(invoke.sc) + |
| 829 | REMOTE_SCALARS_OUTBUFS(invoke.sc); |
| 830 | if (bufs) { |
| 831 | bufs = bufs * sizeof(*pra); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 832 | VERIFY(err, 0 != (pra = kmalloc(bufs, GFP_KERNEL))); |
| 833 | if (err) |
| 834 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 835 | } |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 836 | VERIFY(err, 0 == copy_from_user(pra, invoke.pra, bufs)); |
| 837 | if (err) |
| 838 | goto bail; |
| 839 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 0, &invoke, |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 840 | pra))); |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 841 | if (err) |
| 842 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 843 | break; |
| 844 | default: |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 845 | err = -ENOTTY; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 846 | break; |
| 847 | } |
| 848 | bail: |
| 849 | kfree(pra); |
| 850 | return err; |
| 851 | } |
| 852 | |
Mitchel Humpherys | bffc579 | 2013-02-06 12:03:20 -0800 | [diff] [blame^] | 853 | #ifdef __KERNEL__ |
| 854 | |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 855 | static const struct file_operations fops = { |
| 856 | .open = fastrpc_device_open, |
| 857 | .release = fastrpc_device_release, |
| 858 | .unlocked_ioctl = fastrpc_device_ioctl, |
| 859 | }; |
| 860 | |
Mitchel Humpherys | bffc579 | 2013-02-06 12:03:20 -0800 | [diff] [blame^] | 861 | #endif /*__KERNEL__*/ |
| 862 | |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 863 | static int __init fastrpc_device_init(void) |
| 864 | { |
| 865 | struct fastrpc_apps *me = &gfa; |
| 866 | int err = 0; |
| 867 | |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 868 | VERIFY(err, 0 == fastrpc_init()); |
| 869 | if (err) |
| 870 | goto bail; |
| 871 | VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, 1, DEVICE_NAME)); |
| 872 | if (err) |
| 873 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 874 | cdev_init(&me->cdev, &fops); |
| 875 | me->cdev.owner = THIS_MODULE; |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 876 | VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0), 1)); |
| 877 | if (err) |
| 878 | goto bail; |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 879 | pr_info("'mknod /dev/%s c %d 0'\n", DEVICE_NAME, MAJOR(me->dev_no)); |
| 880 | bail: |
Mitchel Humpherys | b6a022f | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 881 | if (err) { |
| 882 | if (me->dev_no) |
| 883 | unregister_chrdev_region(me->dev_no, 1); |
| 884 | fastrpc_deinit(); |
| 885 | } |
Mitchel Humpherys | 065497f | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 886 | return err; |
| 887 | } |
| 888 | |
| 889 | static void __exit fastrpc_device_exit(void) |
| 890 | { |
| 891 | struct fastrpc_apps *me = &gfa; |
| 892 | |
| 893 | fastrpc_deinit(); |
| 894 | cdev_del(&me->cdev); |
| 895 | unregister_chrdev_region(me->dev_no, 1); |
| 896 | } |
| 897 | |
| 898 | module_init(fastrpc_device_init); |
| 899 | module_exit(fastrpc_device_exit); |
| 900 | |
| 901 | MODULE_LICENSE("GPL v2"); |