Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1 | /* |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 2 | * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20: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 | */ |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 14 | #include "adsprpc_shared.h" |
| 15 | |
| 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 <mach/msm_smd.h> |
| 28 | #include <mach/ion.h> |
Mitchel Humpherys | ef5bbbe | 2013-05-15 12:44:51 -0700 | [diff] [blame^] | 29 | #include <mach/iommu_domains.h> |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 30 | #include <linux/scatterlist.h> |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 31 | #include <linux/fs.h> |
| 32 | #include <linux/uaccess.h> |
| 33 | #include <linux/device.h> |
Mitchel Humpherys | ef5bbbe | 2013-05-15 12:44:51 -0700 | [diff] [blame^] | 34 | #include <linux/of.h> |
| 35 | #include <linux/iommu.h> |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 36 | |
| 37 | #ifndef ION_ADSPRPC_HEAP_ID |
| 38 | #define ION_ADSPRPC_HEAP_ID ION_AUDIO_HEAP_ID |
| 39 | #endif /*ION_ADSPRPC_HEAP_ID*/ |
| 40 | |
| 41 | #define RPC_TIMEOUT (5 * HZ) |
| 42 | #define RPC_HASH_BITS 5 |
| 43 | #define RPC_HASH_SZ (1 << RPC_HASH_BITS) |
| 44 | #define BALIGN 32 |
| 45 | |
| 46 | #define LOCK_MMAP(kernel)\ |
| 47 | do {\ |
| 48 | if (!kernel)\ |
| 49 | down_read(¤t->mm->mmap_sem);\ |
| 50 | } while (0) |
| 51 | |
| 52 | #define UNLOCK_MMAP(kernel)\ |
| 53 | do {\ |
| 54 | if (!kernel)\ |
| 55 | up_read(¤t->mm->mmap_sem);\ |
| 56 | } while (0) |
| 57 | |
| 58 | |
| 59 | static inline uint32_t buf_page_start(void *buf) |
| 60 | { |
| 61 | uint32_t start = (uint32_t) buf & PAGE_MASK; |
| 62 | return start; |
| 63 | } |
| 64 | |
| 65 | static inline uint32_t buf_page_offset(void *buf) |
| 66 | { |
| 67 | uint32_t offset = (uint32_t) buf & (PAGE_SIZE - 1); |
| 68 | return offset; |
| 69 | } |
| 70 | |
| 71 | static inline int buf_num_pages(void *buf, int len) |
| 72 | { |
| 73 | uint32_t start = buf_page_start(buf) >> PAGE_SHIFT; |
| 74 | uint32_t end = (((uint32_t) buf + len - 1) & PAGE_MASK) >> PAGE_SHIFT; |
| 75 | int nPages = end - start + 1; |
| 76 | return nPages; |
| 77 | } |
| 78 | |
| 79 | static inline uint32_t buf_page_size(uint32_t size) |
| 80 | { |
| 81 | uint32_t sz = (size + (PAGE_SIZE - 1)) & PAGE_MASK; |
| 82 | return sz > PAGE_SIZE ? sz : PAGE_SIZE; |
| 83 | } |
| 84 | |
| 85 | static inline int buf_get_pages(void *addr, int sz, int nr_pages, int access, |
| 86 | struct smq_phy_page *pages, int nr_elems) |
| 87 | { |
| 88 | struct vm_area_struct *vma; |
| 89 | uint32_t start = buf_page_start(addr); |
Mitchel Humpherys | ef5bbbe | 2013-05-15 12:44:51 -0700 | [diff] [blame^] | 90 | uint32_t end = buf_page_start((void *)((uint32_t)addr + sz - 1)); |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 91 | uint32_t len = nr_pages << PAGE_SHIFT; |
Mitchel Humpherys | ef5bbbe | 2013-05-15 12:44:51 -0700 | [diff] [blame^] | 92 | unsigned long pfn, pfnend; |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 93 | int n = -1, err = 0; |
| 94 | |
| 95 | VERIFY(err, 0 != access_ok(access ? VERIFY_WRITE : VERIFY_READ, |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 96 | (void __user *)start, len)); |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 97 | if (err) |
| 98 | goto bail; |
| 99 | VERIFY(err, 0 != (vma = find_vma(current->mm, start))); |
| 100 | if (err) |
| 101 | goto bail; |
| 102 | VERIFY(err, ((uint32_t)addr + sz) <= vma->vm_end); |
| 103 | if (err) |
| 104 | goto bail; |
| 105 | n = 0; |
| 106 | VERIFY(err, 0 == follow_pfn(vma, start, &pfn)); |
| 107 | if (err) |
| 108 | goto bail; |
Mitchel Humpherys | ef5bbbe | 2013-05-15 12:44:51 -0700 | [diff] [blame^] | 109 | VERIFY(err, 0 == follow_pfn(vma, end, &pfnend)); |
| 110 | if (err) |
| 111 | goto bail; |
| 112 | VERIFY(err, (pfn + nr_pages - 1) == pfnend); |
| 113 | if (err) |
| 114 | goto bail; |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 115 | VERIFY(err, nr_elems > 0); |
| 116 | if (err) |
| 117 | goto bail; |
| 118 | pages->addr = __pfn_to_phys(pfn); |
| 119 | pages->size = len; |
| 120 | n++; |
| 121 | bail: |
| 122 | return n; |
| 123 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 124 | |
| 125 | struct smq_invoke_ctx { |
| 126 | struct completion work; |
| 127 | int retval; |
| 128 | atomic_t free; |
| 129 | }; |
| 130 | |
| 131 | struct smq_context_list { |
| 132 | struct smq_invoke_ctx *ls; |
| 133 | int size; |
| 134 | int last; |
| 135 | }; |
| 136 | |
Mitchel Humpherys | ef5bbbe | 2013-05-15 12:44:51 -0700 | [diff] [blame^] | 137 | struct fastrpc_smmu { |
| 138 | struct iommu_group *group; |
| 139 | struct iommu_domain *domain; |
| 140 | int domain_id; |
| 141 | bool enabled; |
| 142 | }; |
| 143 | |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 144 | struct fastrpc_apps { |
| 145 | smd_channel_t *chan; |
| 146 | struct smq_context_list clst; |
| 147 | struct completion work; |
| 148 | struct ion_client *iclient; |
| 149 | struct cdev cdev; |
Mitchel Humpherys | 5587765 | 2013-02-02 11:23:42 -0800 | [diff] [blame] | 150 | struct class *class; |
| 151 | struct device *dev; |
Mitchel Humpherys | ef5bbbe | 2013-05-15 12:44:51 -0700 | [diff] [blame^] | 152 | struct fastrpc_smmu smmu; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 153 | dev_t dev_no; |
| 154 | spinlock_t wrlock; |
| 155 | spinlock_t hlock; |
| 156 | struct hlist_head htbl[RPC_HASH_SZ]; |
| 157 | }; |
| 158 | |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 159 | struct fastrpc_mmap { |
| 160 | struct hlist_node hn; |
| 161 | struct ion_handle *handle; |
| 162 | void *virt; |
| 163 | uint32_t vaddrin; |
| 164 | uint32_t vaddrout; |
| 165 | int size; |
| 166 | }; |
| 167 | |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 168 | struct fastrpc_buf { |
| 169 | struct ion_handle *handle; |
| 170 | void *virt; |
| 171 | ion_phys_addr_t phys; |
| 172 | int size; |
| 173 | int used; |
| 174 | }; |
| 175 | |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 176 | struct file_data { |
| 177 | spinlock_t hlock; |
| 178 | struct hlist_head hlst; |
| 179 | }; |
| 180 | |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 181 | struct fastrpc_device { |
| 182 | uint32_t tgid; |
| 183 | struct hlist_node hn; |
| 184 | struct fastrpc_buf buf; |
| 185 | }; |
| 186 | |
| 187 | static struct fastrpc_apps gfa; |
| 188 | |
| 189 | static void free_mem(struct fastrpc_buf *buf) |
| 190 | { |
| 191 | struct fastrpc_apps *me = &gfa; |
| 192 | |
Mitchel Humpherys | ef5bbbe | 2013-05-15 12:44:51 -0700 | [diff] [blame^] | 193 | if (!IS_ERR_OR_NULL(buf->handle)) { |
| 194 | if (me->smmu.enabled && buf->phys) { |
| 195 | ion_unmap_iommu(me->iclient, buf->handle, |
| 196 | me->smmu.domain_id, 0); |
| 197 | buf->phys = 0; |
| 198 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 199 | if (buf->virt) { |
| 200 | ion_unmap_kernel(me->iclient, buf->handle); |
| 201 | buf->virt = 0; |
| 202 | } |
| 203 | ion_free(me->iclient, buf->handle); |
| 204 | buf->handle = 0; |
| 205 | } |
| 206 | } |
| 207 | |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 208 | static void free_map(struct fastrpc_mmap *map) |
| 209 | { |
| 210 | struct fastrpc_apps *me = &gfa; |
Mitchel Humpherys | ef5bbbe | 2013-05-15 12:44:51 -0700 | [diff] [blame^] | 211 | if (!IS_ERR_OR_NULL(map->handle)) { |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 212 | if (map->virt) { |
| 213 | ion_unmap_kernel(me->iclient, map->handle); |
| 214 | map->virt = 0; |
| 215 | } |
| 216 | ion_free(me->iclient, map->handle); |
| 217 | } |
| 218 | map->handle = 0; |
| 219 | } |
| 220 | |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 221 | static int alloc_mem(struct fastrpc_buf *buf) |
| 222 | { |
Mitchel Humpherys | ef5bbbe | 2013-05-15 12:44:51 -0700 | [diff] [blame^] | 223 | struct fastrpc_apps *me = &gfa; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 224 | struct ion_client *clnt = gfa.iclient; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 225 | struct sg_table *sg; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 226 | int err = 0; |
Mitchel Humpherys | ef5bbbe | 2013-05-15 12:44:51 -0700 | [diff] [blame^] | 227 | unsigned int heap; |
| 228 | unsigned long len; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 229 | buf->handle = 0; |
| 230 | buf->virt = 0; |
Mitchel Humpherys | ef5bbbe | 2013-05-15 12:44:51 -0700 | [diff] [blame^] | 231 | heap = me->smmu.enabled ? ION_HEAP(ION_IOMMU_HEAP_ID) : |
| 232 | ION_HEAP(ION_ADSP_HEAP_ID) | ION_HEAP(ION_AUDIO_HEAP_ID); |
| 233 | buf->handle = ion_alloc(clnt, buf->size, SZ_4K, heap, 0); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 234 | VERIFY(err, 0 == IS_ERR_OR_NULL(buf->handle)); |
| 235 | if (err) |
| 236 | goto bail; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 237 | VERIFY(err, 0 != (buf->virt = ion_map_kernel(clnt, buf->handle))); |
| 238 | if (err) |
| 239 | goto bail; |
Mitchel Humpherys | ef5bbbe | 2013-05-15 12:44:51 -0700 | [diff] [blame^] | 240 | if (me->smmu.enabled) { |
| 241 | len = buf->size; |
| 242 | VERIFY(err, 0 == ion_map_iommu(clnt, buf->handle, |
| 243 | me->smmu.domain_id, 0, SZ_4K, 0, |
| 244 | &buf->phys, &len, 0, 0)); |
| 245 | if (err) |
| 246 | goto bail; |
| 247 | } else { |
| 248 | VERIFY(err, 0 != (sg = ion_sg_table(clnt, buf->handle))); |
| 249 | if (err) |
| 250 | goto bail; |
| 251 | VERIFY(err, 1 == sg->nents); |
| 252 | if (err) |
| 253 | goto bail; |
| 254 | buf->phys = sg_dma_address(sg->sgl); |
| 255 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 256 | bail: |
| 257 | if (err && !IS_ERR_OR_NULL(buf->handle)) |
| 258 | free_mem(buf); |
| 259 | return err; |
| 260 | } |
| 261 | |
| 262 | static int context_list_ctor(struct smq_context_list *me, int size) |
| 263 | { |
| 264 | int err = 0; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 265 | VERIFY(err, 0 != (me->ls = kzalloc(size, GFP_KERNEL))); |
| 266 | if (err) |
| 267 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 268 | me->size = size / sizeof(*me->ls); |
| 269 | me->last = 0; |
| 270 | bail: |
| 271 | return err; |
| 272 | } |
| 273 | |
| 274 | static void context_list_dtor(struct smq_context_list *me) |
| 275 | { |
| 276 | kfree(me->ls); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static void context_list_alloc_ctx(struct smq_context_list *me, |
| 280 | struct smq_invoke_ctx **po) |
| 281 | { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 282 | int i = me->last; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 283 | struct smq_invoke_ctx *ctx; |
| 284 | |
| 285 | for (;;) { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 286 | i = i % me->size; |
| 287 | ctx = &me->ls[i]; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 288 | if (atomic_read(&ctx->free) == 0) |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 289 | if (atomic_cmpxchg(&ctx->free, 0, 1) == 0) |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 290 | break; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 291 | i++; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 292 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 293 | me->last = i; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 294 | ctx->retval = -1; |
| 295 | init_completion(&ctx->work); |
| 296 | *po = ctx; |
| 297 | } |
| 298 | |
| 299 | static void context_free(struct smq_invoke_ctx *me) |
| 300 | { |
| 301 | if (me) |
| 302 | atomic_set(&me->free, 0); |
| 303 | } |
| 304 | |
| 305 | static void context_notify_user(struct smq_invoke_ctx *me, int retval) |
| 306 | { |
| 307 | me->retval = retval; |
| 308 | complete(&me->work); |
| 309 | } |
| 310 | |
| 311 | static void context_notify_all_users(struct smq_context_list *me) |
| 312 | { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 313 | int i; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 314 | |
| 315 | if (!me->ls) |
| 316 | return; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 317 | for (i = 0; i < me->size; ++i) { |
| 318 | if (atomic_read(&me->ls[i].free) != 0) |
| 319 | complete(&me->ls[i].work); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | static int get_page_list(uint32_t kernel, uint32_t sc, remote_arg_t *pra, |
| 324 | struct fastrpc_buf *ibuf, struct fastrpc_buf *obuf) |
| 325 | { |
| 326 | struct smq_phy_page *pgstart, *pages; |
| 327 | struct smq_invoke_buf *list; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 328 | int i, rlen, err = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 329 | int inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 330 | int outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
| 331 | |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 332 | LOCK_MMAP(kernel); |
| 333 | *obuf = *ibuf; |
| 334 | retry: |
| 335 | list = smq_invoke_buf_start((remote_arg_t *)obuf->virt, sc); |
| 336 | pgstart = smq_phy_page_start(sc, list); |
| 337 | pages = pgstart + 1; |
| 338 | rlen = obuf->size - ((uint32_t)pages - (uint32_t)obuf->virt); |
| 339 | if (rlen < 0) { |
| 340 | rlen = ((uint32_t)pages - (uint32_t)obuf->virt) - obuf->size; |
| 341 | obuf->size += buf_page_size(rlen); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 342 | VERIFY(err, 0 == alloc_mem(obuf)); |
| 343 | if (err) |
| 344 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 345 | goto retry; |
| 346 | } |
| 347 | pgstart->addr = obuf->phys; |
| 348 | pgstart->size = obuf->size; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 349 | for (i = 0; i < inbufs + outbufs; ++i) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 350 | void *buf; |
| 351 | int len, num; |
| 352 | |
Mitchel Humpherys | f581c51 | 2012-10-19 11:29:36 -0700 | [diff] [blame] | 353 | list[i].num = 0; |
| 354 | list[i].pgidx = 0; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 355 | len = pra[i].buf.len; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 356 | if (!len) |
| 357 | continue; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 358 | buf = pra[i].buf.pv; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 359 | num = buf_num_pages(buf, len); |
| 360 | if (!kernel) |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 361 | list[i].num = buf_get_pages(buf, len, num, |
| 362 | i >= inbufs, pages, rlen / sizeof(*pages)); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 363 | else |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 364 | list[i].num = 0; |
| 365 | VERIFY(err, list[i].num >= 0); |
| 366 | if (err) |
| 367 | goto bail; |
| 368 | if (list[i].num) { |
| 369 | list[i].pgidx = pages - pgstart; |
| 370 | pages = pages + list[i].num; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 371 | } else if (rlen > sizeof(*pages)) { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 372 | list[i].pgidx = pages - pgstart; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 373 | pages = pages + 1; |
| 374 | } else { |
| 375 | if (obuf->handle != ibuf->handle) |
| 376 | free_mem(obuf); |
| 377 | obuf->size += buf_page_size(sizeof(*pages)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 378 | VERIFY(err, 0 == alloc_mem(obuf)); |
| 379 | if (err) |
| 380 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 381 | goto retry; |
| 382 | } |
| 383 | rlen = obuf->size - ((uint32_t) pages - (uint32_t) obuf->virt); |
| 384 | } |
| 385 | obuf->used = obuf->size - rlen; |
| 386 | bail: |
| 387 | if (err && (obuf->handle != ibuf->handle)) |
| 388 | free_mem(obuf); |
| 389 | UNLOCK_MMAP(kernel); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 390 | return err; |
| 391 | } |
| 392 | |
| 393 | static int get_args(uint32_t kernel, uint32_t sc, remote_arg_t *pra, |
| 394 | remote_arg_t *rpra, remote_arg_t *upra, |
| 395 | struct fastrpc_buf *ibuf, struct fastrpc_buf **abufs, |
| 396 | int *nbufs) |
| 397 | { |
| 398 | struct smq_invoke_buf *list; |
| 399 | struct fastrpc_buf *pbuf = ibuf, *obufs = 0; |
| 400 | struct smq_phy_page *pages; |
| 401 | void *args; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 402 | int i, rlen, size, used, inh, bufs = 0, err = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 403 | int inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 404 | int outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
| 405 | |
| 406 | list = smq_invoke_buf_start(rpra, sc); |
| 407 | pages = smq_phy_page_start(sc, list); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 408 | used = ALIGN(pbuf->used, BALIGN); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 409 | args = (void *)((char *)pbuf->virt + used); |
| 410 | rlen = pbuf->size - used; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 411 | for (i = 0; i < inbufs + outbufs; ++i) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 412 | |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 413 | rpra[i].buf.len = pra[i].buf.len; |
Mitchel Humpherys | f581c51 | 2012-10-19 11:29:36 -0700 | [diff] [blame] | 414 | if (!rpra[i].buf.len) |
| 415 | continue; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 416 | if (list[i].num) { |
| 417 | rpra[i].buf.pv = pra[i].buf.pv; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 418 | continue; |
| 419 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 420 | if (rlen < pra[i].buf.len) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 421 | struct fastrpc_buf *b; |
| 422 | pbuf->used = pbuf->size - rlen; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 423 | VERIFY(err, 0 != (b = krealloc(obufs, |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 424 | (bufs + 1) * sizeof(*obufs), GFP_KERNEL))); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 425 | if (err) |
| 426 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 427 | obufs = b; |
| 428 | pbuf = obufs + bufs; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 429 | pbuf->size = buf_num_pages(0, pra[i].buf.len) * |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 430 | PAGE_SIZE; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 431 | VERIFY(err, 0 == alloc_mem(pbuf)); |
| 432 | if (err) |
| 433 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 434 | bufs++; |
| 435 | args = pbuf->virt; |
| 436 | rlen = pbuf->size; |
| 437 | } |
Mitchel Humpherys | 0bc3aa5 | 2013-02-02 11:31:15 -0800 | [diff] [blame] | 438 | list[i].num = 1; |
| 439 | pages[list[i].pgidx].addr = |
| 440 | buf_page_start((void *)(pbuf->phys + |
| 441 | (pbuf->size - rlen))); |
| 442 | pages[list[i].pgidx].size = |
| 443 | buf_page_size(pra[i].buf.len); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 444 | if (i < inbufs) { |
| 445 | if (!kernel) { |
| 446 | VERIFY(err, 0 == copy_from_user(args, |
| 447 | pra[i].buf.pv, pra[i].buf.len)); |
| 448 | if (err) |
| 449 | goto bail; |
| 450 | } else { |
| 451 | memmove(args, pra[i].buf.pv, pra[i].buf.len); |
| 452 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 453 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 454 | rpra[i].buf.pv = args; |
| 455 | args = (void *)((char *)args + ALIGN(pra[i].buf.len, BALIGN)); |
| 456 | rlen -= ALIGN(pra[i].buf.len, BALIGN); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 457 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 458 | for (i = 0; i < inbufs; ++i) { |
| 459 | if (rpra[i].buf.len) |
| 460 | dmac_flush_range(rpra[i].buf.pv, |
| 461 | (char *)rpra[i].buf.pv + rpra[i].buf.len); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 462 | } |
| 463 | pbuf->used = pbuf->size - rlen; |
| 464 | size = sizeof(*rpra) * REMOTE_SCALARS_INHANDLES(sc); |
| 465 | if (size) { |
| 466 | inh = inbufs + outbufs; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 467 | if (!kernel) { |
| 468 | VERIFY(err, 0 == copy_from_user(&rpra[inh], &upra[inh], |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 469 | size)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 470 | if (err) |
| 471 | goto bail; |
| 472 | } else { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 473 | memmove(&rpra[inh], &upra[inh], size); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 474 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 475 | } |
| 476 | dmac_flush_range(rpra, (char *)rpra + used); |
| 477 | bail: |
| 478 | *abufs = obufs; |
| 479 | *nbufs = bufs; |
| 480 | return err; |
| 481 | } |
| 482 | |
| 483 | static int put_args(uint32_t kernel, uint32_t sc, remote_arg_t *pra, |
| 484 | remote_arg_t *rpra, remote_arg_t *upra) |
| 485 | { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 486 | int i, inbufs, outbufs, outh, size; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 487 | int err = 0; |
| 488 | |
| 489 | inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 490 | outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 491 | for (i = inbufs; i < inbufs + outbufs; ++i) { |
| 492 | if (rpra[i].buf.pv != pra[i].buf.pv) { |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 493 | if (!kernel) { |
| 494 | VERIFY(err, 0 == copy_to_user(pra[i].buf.pv, |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 495 | rpra[i].buf.pv, rpra[i].buf.len)); |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 496 | if (err) |
| 497 | goto bail; |
| 498 | } else { |
| 499 | memmove(pra[i].buf.pv, rpra[i].buf.pv, |
| 500 | rpra[i].buf.len); |
| 501 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 502 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 503 | } |
| 504 | size = sizeof(*rpra) * REMOTE_SCALARS_OUTHANDLES(sc); |
| 505 | if (size) { |
| 506 | outh = inbufs + outbufs + REMOTE_SCALARS_INHANDLES(sc); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 507 | if (!kernel) { |
| 508 | VERIFY(err, 0 == copy_to_user(&upra[outh], &rpra[outh], |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 509 | size)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 510 | if (err) |
| 511 | goto bail; |
| 512 | } else { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 513 | memmove(&upra[outh], &rpra[outh], size); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 514 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 515 | } |
| 516 | bail: |
| 517 | return err; |
| 518 | } |
| 519 | |
| 520 | static void inv_args(uint32_t sc, remote_arg_t *rpra, int used) |
| 521 | { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 522 | int i, inbufs, outbufs; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 523 | int inv = 0; |
| 524 | |
| 525 | inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 526 | outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 527 | for (i = inbufs; i < inbufs + outbufs; ++i) { |
| 528 | if (buf_page_start(rpra) == buf_page_start(rpra[i].buf.pv)) |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 529 | inv = 1; |
Mitchel Humpherys | f581c51 | 2012-10-19 11:29:36 -0700 | [diff] [blame] | 530 | else if (rpra[i].buf.len) |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 531 | dmac_inv_range(rpra[i].buf.pv, |
| 532 | (char *)rpra[i].buf.pv + rpra[i].buf.len); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | if (inv || REMOTE_SCALARS_OUTHANDLES(sc)) |
| 536 | dmac_inv_range(rpra, (char *)rpra + used); |
| 537 | } |
| 538 | |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 539 | static int fastrpc_invoke_send(struct fastrpc_apps *me, |
| 540 | uint32_t kernel, uint32_t handle, |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 541 | uint32_t sc, struct smq_invoke_ctx *ctx, |
| 542 | struct fastrpc_buf *buf) |
| 543 | { |
| 544 | struct smq_msg msg; |
| 545 | int err = 0, len; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 546 | msg.pid = current->tgid; |
| 547 | msg.tid = current->pid; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 548 | if (kernel) |
| 549 | msg.pid = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 550 | msg.invoke.header.ctx = ctx; |
| 551 | msg.invoke.header.handle = handle; |
| 552 | msg.invoke.header.sc = sc; |
| 553 | msg.invoke.page.addr = buf->phys; |
| 554 | msg.invoke.page.size = buf_page_size(buf->used); |
| 555 | spin_lock(&me->wrlock); |
| 556 | len = smd_write(me->chan, &msg, sizeof(msg)); |
| 557 | spin_unlock(&me->wrlock); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 558 | VERIFY(err, len == sizeof(msg)); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 559 | return err; |
| 560 | } |
| 561 | |
| 562 | static void fastrpc_deinit(void) |
| 563 | { |
| 564 | struct fastrpc_apps *me = &gfa; |
| 565 | |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame] | 566 | smd_close(me->chan); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 567 | context_list_dtor(&me->clst); |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame] | 568 | ion_client_destroy(me->iclient); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 569 | me->iclient = 0; |
| 570 | me->chan = 0; |
| 571 | } |
| 572 | |
| 573 | static void fastrpc_read_handler(void) |
| 574 | { |
| 575 | struct fastrpc_apps *me = &gfa; |
| 576 | struct smq_invoke_rsp rsp; |
| 577 | int err = 0; |
| 578 | |
| 579 | do { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 580 | VERIFY(err, sizeof(rsp) == |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 581 | smd_read_from_cb(me->chan, &rsp, sizeof(rsp))); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 582 | if (err) |
| 583 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 584 | context_notify_user(rsp.ctx, rsp.retval); |
| 585 | } while (!err); |
| 586 | bail: |
| 587 | return; |
| 588 | } |
| 589 | |
| 590 | static void smd_event_handler(void *priv, unsigned event) |
| 591 | { |
| 592 | struct fastrpc_apps *me = (struct fastrpc_apps *)priv; |
| 593 | |
| 594 | switch (event) { |
| 595 | case SMD_EVENT_OPEN: |
| 596 | complete(&(me->work)); |
| 597 | break; |
| 598 | case SMD_EVENT_CLOSE: |
| 599 | context_notify_all_users(&me->clst); |
| 600 | break; |
| 601 | case SMD_EVENT_DATA: |
| 602 | fastrpc_read_handler(); |
| 603 | break; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | static int fastrpc_init(void) |
| 608 | { |
| 609 | int err = 0; |
| 610 | struct fastrpc_apps *me = &gfa; |
Mitchel Humpherys | ef5bbbe | 2013-05-15 12:44:51 -0700 | [diff] [blame^] | 611 | struct device_node *node; |
| 612 | bool enabled = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 613 | |
| 614 | if (me->chan == 0) { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 615 | int i; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 616 | spin_lock_init(&me->hlock); |
| 617 | spin_lock_init(&me->wrlock); |
| 618 | init_completion(&me->work); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 619 | for (i = 0; i < RPC_HASH_SZ; ++i) |
| 620 | INIT_HLIST_HEAD(&me->htbl[i]); |
| 621 | VERIFY(err, 0 == context_list_ctor(&me->clst, SZ_4K)); |
| 622 | if (err) |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame] | 623 | goto context_list_bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 624 | me->iclient = msm_ion_client_create(ION_HEAP_CARVEOUT_MASK, |
| 625 | DEVICE_NAME); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 626 | VERIFY(err, 0 == IS_ERR_OR_NULL(me->iclient)); |
| 627 | if (err) |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame] | 628 | goto ion_bail; |
Mitchel Humpherys | ef5bbbe | 2013-05-15 12:44:51 -0700 | [diff] [blame^] | 629 | node = of_find_compatible_node(NULL, NULL, |
| 630 | "qcom,msm-audio-ion"); |
| 631 | if (node) |
| 632 | enabled = of_property_read_bool(node, |
| 633 | "qcom,smmu-enabled"); |
| 634 | if (enabled) |
| 635 | me->smmu.group = iommu_group_find("lpass_audio"); |
| 636 | if (me->smmu.group) |
| 637 | me->smmu.domain = iommu_group_get_iommudata( |
| 638 | me->smmu.group); |
| 639 | if (!IS_ERR_OR_NULL(me->smmu.domain)) { |
| 640 | me->smmu.domain_id = msm_find_domain_no( |
| 641 | me->smmu.domain); |
| 642 | if (me->smmu.domain_id >= 0) |
| 643 | me->smmu.enabled = enabled; |
| 644 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 645 | VERIFY(err, 0 == smd_named_open_on_edge(FASTRPC_SMD_GUID, |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 646 | SMD_APPS_QDSP, &me->chan, |
| 647 | me, smd_event_handler)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 648 | if (err) |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame] | 649 | goto smd_bail; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 650 | VERIFY(err, 0 != wait_for_completion_timeout(&me->work, |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 651 | RPC_TIMEOUT)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 652 | if (err) |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame] | 653 | goto completion_bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 654 | } |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame] | 655 | |
| 656 | return 0; |
| 657 | |
| 658 | completion_bail: |
| 659 | smd_close(me->chan); |
| 660 | smd_bail: |
| 661 | ion_client_destroy(me->iclient); |
| 662 | ion_bail: |
| 663 | context_list_dtor(&me->clst); |
| 664 | context_list_bail: |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 665 | return err; |
| 666 | } |
| 667 | |
| 668 | static void free_dev(struct fastrpc_device *dev) |
| 669 | { |
| 670 | if (dev) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 671 | free_mem(&dev->buf); |
| 672 | kfree(dev); |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 673 | module_put(THIS_MODULE); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
| 677 | static int alloc_dev(struct fastrpc_device **dev) |
| 678 | { |
| 679 | int err = 0; |
| 680 | struct fastrpc_device *fd = 0; |
| 681 | |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 682 | VERIFY(err, 0 != try_module_get(THIS_MODULE)); |
| 683 | if (err) |
| 684 | goto bail; |
| 685 | VERIFY(err, 0 != (fd = kzalloc(sizeof(*fd), GFP_KERNEL))); |
| 686 | if (err) |
| 687 | goto bail; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 688 | |
| 689 | INIT_HLIST_NODE(&fd->hn); |
| 690 | |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 691 | fd->buf.size = PAGE_SIZE; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 692 | VERIFY(err, 0 == alloc_mem(&fd->buf)); |
| 693 | if (err) |
| 694 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 695 | fd->tgid = current->tgid; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 696 | |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 697 | *dev = fd; |
| 698 | bail: |
| 699 | if (err) |
| 700 | free_dev(fd); |
| 701 | return err; |
| 702 | } |
| 703 | |
| 704 | static int get_dev(struct fastrpc_apps *me, struct fastrpc_device **rdev) |
| 705 | { |
| 706 | struct hlist_head *head; |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 707 | struct fastrpc_device *dev = 0, *devfree = 0; |
| 708 | struct hlist_node *pos, *n; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 709 | uint32_t h = hash_32(current->tgid, RPC_HASH_BITS); |
| 710 | int err = 0; |
| 711 | |
| 712 | spin_lock(&me->hlock); |
| 713 | head = &me->htbl[h]; |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 714 | hlist_for_each_entry_safe(dev, pos, n, head, hn) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 715 | if (dev->tgid == current->tgid) { |
| 716 | hlist_del(&dev->hn); |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 717 | devfree = dev; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 718 | break; |
| 719 | } |
| 720 | } |
| 721 | spin_unlock(&me->hlock); |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 722 | VERIFY(err, devfree != 0); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 723 | if (err) |
| 724 | goto bail; |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 725 | *rdev = devfree; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 726 | bail: |
| 727 | if (err) { |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 728 | free_dev(devfree); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 729 | err = alloc_dev(rdev); |
| 730 | } |
| 731 | return err; |
| 732 | } |
| 733 | |
| 734 | static void add_dev(struct fastrpc_apps *me, struct fastrpc_device *dev) |
| 735 | { |
| 736 | struct hlist_head *head; |
| 737 | uint32_t h = hash_32(current->tgid, RPC_HASH_BITS); |
| 738 | |
| 739 | spin_lock(&me->hlock); |
| 740 | head = &me->htbl[h]; |
| 741 | hlist_add_head(&dev->hn, head); |
| 742 | spin_unlock(&me->hlock); |
| 743 | return; |
| 744 | } |
| 745 | |
| 746 | static int fastrpc_release_current_dsp_process(void); |
| 747 | |
| 748 | static int fastrpc_internal_invoke(struct fastrpc_apps *me, uint32_t kernel, |
| 749 | struct fastrpc_ioctl_invoke *invoke, remote_arg_t *pra) |
| 750 | { |
| 751 | remote_arg_t *rpra = 0; |
| 752 | struct fastrpc_device *dev = 0; |
| 753 | struct smq_invoke_ctx *ctx = 0; |
| 754 | struct fastrpc_buf obuf, *abufs = 0, *b; |
| 755 | int interrupted = 0; |
| 756 | uint32_t sc; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 757 | int i, nbufs = 0, err = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 758 | |
| 759 | sc = invoke->sc; |
| 760 | obuf.handle = 0; |
Mitchel Humpherys | ef5bbbe | 2013-05-15 12:44:51 -0700 | [diff] [blame^] | 761 | if (me->smmu.enabled) { |
| 762 | VERIFY(err, 0 == iommu_attach_group(me->smmu.domain, |
| 763 | me->smmu.group)); |
| 764 | if (err) |
| 765 | return err; |
| 766 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 767 | if (REMOTE_SCALARS_LENGTH(sc)) { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 768 | VERIFY(err, 0 == get_dev(me, &dev)); |
| 769 | if (err) |
| 770 | goto bail; |
| 771 | VERIFY(err, 0 == get_page_list(kernel, sc, pra, &dev->buf, |
| 772 | &obuf)); |
| 773 | if (err) |
| 774 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 775 | rpra = (remote_arg_t *)obuf.virt; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 776 | VERIFY(err, 0 == get_args(kernel, sc, pra, rpra, invoke->pra, |
| 777 | &obuf, &abufs, &nbufs)); |
| 778 | if (err) |
| 779 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | context_list_alloc_ctx(&me->clst, &ctx); |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 783 | VERIFY(err, 0 == fastrpc_invoke_send(me, kernel, invoke->handle, sc, |
| 784 | ctx, &obuf)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 785 | if (err) |
| 786 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 787 | inv_args(sc, rpra, obuf.used); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 788 | VERIFY(err, 0 == (interrupted = |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 789 | wait_for_completion_interruptible(&ctx->work))); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 790 | if (err) |
| 791 | goto bail; |
| 792 | VERIFY(err, 0 == (err = ctx->retval)); |
| 793 | if (err) |
| 794 | goto bail; |
| 795 | VERIFY(err, 0 == put_args(kernel, sc, pra, rpra, invoke->pra)); |
| 796 | if (err) |
| 797 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 798 | bail: |
| 799 | if (interrupted) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 800 | if (!kernel) |
| 801 | (void)fastrpc_release_current_dsp_process(); |
| 802 | wait_for_completion(&ctx->work); |
| 803 | } |
| 804 | context_free(ctx); |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 805 | |
Mitchel Humpherys | ef5bbbe | 2013-05-15 12:44:51 -0700 | [diff] [blame^] | 806 | if (me->smmu.enabled) |
| 807 | iommu_detach_group(me->smmu.domain, me->smmu.group); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 808 | for (i = 0, b = abufs; i < nbufs; ++i, ++b) |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 809 | free_mem(b); |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 810 | |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 811 | kfree(abufs); |
| 812 | if (dev) { |
| 813 | add_dev(me, dev); |
| 814 | if (obuf.handle != dev->buf.handle) |
| 815 | free_mem(&obuf); |
| 816 | } |
| 817 | return err; |
| 818 | } |
| 819 | |
| 820 | static int fastrpc_create_current_dsp_process(void) |
| 821 | { |
| 822 | int err = 0; |
| 823 | struct fastrpc_ioctl_invoke ioctl; |
| 824 | struct fastrpc_apps *me = &gfa; |
| 825 | remote_arg_t ra[1]; |
| 826 | int tgid = 0; |
| 827 | |
| 828 | tgid = current->tgid; |
| 829 | ra[0].buf.pv = &tgid; |
| 830 | ra[0].buf.len = sizeof(tgid); |
| 831 | ioctl.handle = 1; |
| 832 | ioctl.sc = REMOTE_SCALARS_MAKE(0, 1, 0); |
| 833 | ioctl.pra = ra; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 834 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra))); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 835 | return err; |
| 836 | } |
| 837 | |
| 838 | static int fastrpc_release_current_dsp_process(void) |
| 839 | { |
| 840 | int err = 0; |
| 841 | struct fastrpc_apps *me = &gfa; |
| 842 | struct fastrpc_ioctl_invoke ioctl; |
| 843 | remote_arg_t ra[1]; |
| 844 | int tgid = 0; |
| 845 | |
| 846 | tgid = current->tgid; |
| 847 | ra[0].buf.pv = &tgid; |
| 848 | ra[0].buf.len = sizeof(tgid); |
| 849 | ioctl.handle = 1; |
| 850 | ioctl.sc = REMOTE_SCALARS_MAKE(1, 1, 0); |
| 851 | ioctl.pra = ra; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 852 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra))); |
| 853 | return err; |
| 854 | } |
| 855 | |
| 856 | static int fastrpc_mmap_on_dsp(struct fastrpc_apps *me, |
| 857 | struct fastrpc_ioctl_mmap *mmap, |
| 858 | struct smq_phy_page *pages, |
| 859 | int num) |
| 860 | { |
| 861 | struct fastrpc_ioctl_invoke ioctl; |
| 862 | remote_arg_t ra[3]; |
| 863 | int err = 0; |
| 864 | struct { |
| 865 | int pid; |
| 866 | uint32_t flags; |
| 867 | uint32_t vaddrin; |
| 868 | int num; |
| 869 | } inargs; |
| 870 | |
| 871 | struct { |
| 872 | uint32_t vaddrout; |
| 873 | } routargs; |
| 874 | inargs.pid = current->tgid; |
| 875 | inargs.vaddrin = mmap->vaddrin; |
| 876 | inargs.flags = mmap->flags; |
| 877 | inargs.num = num; |
| 878 | ra[0].buf.pv = &inargs; |
| 879 | ra[0].buf.len = sizeof(inargs); |
| 880 | |
| 881 | ra[1].buf.pv = pages; |
| 882 | ra[1].buf.len = num * sizeof(*pages); |
| 883 | |
| 884 | ra[2].buf.pv = &routargs; |
| 885 | ra[2].buf.len = sizeof(routargs); |
| 886 | |
| 887 | ioctl.handle = 1; |
| 888 | ioctl.sc = REMOTE_SCALARS_MAKE(2, 2, 1); |
| 889 | ioctl.pra = ra; |
| 890 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra))); |
| 891 | mmap->vaddrout = routargs.vaddrout; |
| 892 | if (err) |
| 893 | goto bail; |
| 894 | bail: |
| 895 | return err; |
| 896 | } |
| 897 | |
| 898 | static int fastrpc_munmap_on_dsp(struct fastrpc_apps *me, |
| 899 | struct fastrpc_ioctl_munmap *munmap) |
| 900 | { |
| 901 | struct fastrpc_ioctl_invoke ioctl; |
| 902 | remote_arg_t ra[1]; |
| 903 | int err = 0; |
| 904 | struct { |
| 905 | int pid; |
| 906 | uint32_t vaddrout; |
| 907 | int size; |
| 908 | } inargs; |
| 909 | |
| 910 | inargs.pid = current->tgid; |
| 911 | inargs.size = munmap->size; |
| 912 | inargs.vaddrout = munmap->vaddrout; |
| 913 | ra[0].buf.pv = &inargs; |
| 914 | ra[0].buf.len = sizeof(inargs); |
| 915 | |
| 916 | ioctl.handle = 1; |
| 917 | ioctl.sc = REMOTE_SCALARS_MAKE(3, 1, 0); |
| 918 | ioctl.pra = ra; |
| 919 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra))); |
| 920 | return err; |
| 921 | } |
| 922 | |
| 923 | static int fastrpc_internal_munmap(struct fastrpc_apps *me, |
| 924 | struct file_data *fdata, |
| 925 | struct fastrpc_ioctl_munmap *munmap) |
| 926 | { |
| 927 | int err = 0; |
| 928 | struct fastrpc_mmap *map = 0, *mapfree = 0; |
| 929 | struct hlist_node *pos, *n; |
| 930 | VERIFY(err, 0 == (err = fastrpc_munmap_on_dsp(me, munmap))); |
| 931 | if (err) |
| 932 | goto bail; |
| 933 | spin_lock(&fdata->hlock); |
| 934 | hlist_for_each_entry_safe(map, pos, n, &fdata->hlst, hn) { |
| 935 | if (map->vaddrout == munmap->vaddrout && |
| 936 | map->size == munmap->size) { |
| 937 | hlist_del(&map->hn); |
| 938 | mapfree = map; |
| 939 | map = 0; |
| 940 | break; |
| 941 | } |
| 942 | } |
| 943 | spin_unlock(&fdata->hlock); |
| 944 | bail: |
| 945 | if (mapfree) { |
| 946 | free_map(mapfree); |
| 947 | kfree(mapfree); |
| 948 | } |
| 949 | return err; |
| 950 | } |
| 951 | |
| 952 | |
| 953 | static int fastrpc_internal_mmap(struct fastrpc_apps *me, |
| 954 | struct file_data *fdata, |
| 955 | struct fastrpc_ioctl_mmap *mmap) |
| 956 | { |
| 957 | struct ion_client *clnt = gfa.iclient; |
| 958 | struct fastrpc_mmap *map = 0; |
| 959 | struct smq_phy_page *pages = 0; |
| 960 | void *buf; |
| 961 | int len; |
| 962 | int num; |
| 963 | int err = 0; |
| 964 | |
| 965 | VERIFY(err, 0 != (map = kzalloc(sizeof(*map), GFP_KERNEL))); |
| 966 | if (err) |
| 967 | goto bail; |
| 968 | map->handle = ion_import_dma_buf(clnt, mmap->fd); |
| 969 | VERIFY(err, 0 == IS_ERR_OR_NULL(map->handle)); |
| 970 | if (err) |
| 971 | goto bail; |
| 972 | VERIFY(err, 0 != (map->virt = ion_map_kernel(clnt, map->handle))); |
| 973 | if (err) |
| 974 | goto bail; |
| 975 | buf = (void *)mmap->vaddrin; |
| 976 | len = mmap->size; |
| 977 | num = buf_num_pages(buf, len); |
| 978 | VERIFY(err, 0 != (pages = kzalloc(num * sizeof(*pages), GFP_KERNEL))); |
| 979 | if (err) |
| 980 | goto bail; |
| 981 | VERIFY(err, 0 < (num = buf_get_pages(buf, len, num, 1, pages, num))); |
| 982 | if (err) |
| 983 | goto bail; |
| 984 | |
| 985 | VERIFY(err, 0 == fastrpc_mmap_on_dsp(me, mmap, pages, num)); |
| 986 | if (err) |
| 987 | goto bail; |
| 988 | map->vaddrin = mmap->vaddrin; |
| 989 | map->vaddrout = mmap->vaddrout; |
| 990 | map->size = mmap->size; |
| 991 | INIT_HLIST_NODE(&map->hn); |
| 992 | spin_lock(&fdata->hlock); |
| 993 | hlist_add_head(&map->hn, &fdata->hlst); |
| 994 | spin_unlock(&fdata->hlock); |
| 995 | bail: |
| 996 | if (err && map) { |
| 997 | free_map(map); |
| 998 | kfree(map); |
| 999 | } |
| 1000 | kfree(pages); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1001 | return err; |
| 1002 | } |
| 1003 | |
| 1004 | static void cleanup_current_dev(void) |
| 1005 | { |
| 1006 | struct fastrpc_apps *me = &gfa; |
| 1007 | uint32_t h = hash_32(current->tgid, RPC_HASH_BITS); |
| 1008 | struct hlist_head *head; |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 1009 | struct hlist_node *pos, *n; |
| 1010 | struct fastrpc_device *dev, *devfree; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1011 | |
| 1012 | rnext: |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 1013 | devfree = dev = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1014 | spin_lock(&me->hlock); |
| 1015 | head = &me->htbl[h]; |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 1016 | hlist_for_each_entry_safe(dev, pos, n, head, hn) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1017 | if (dev->tgid == current->tgid) { |
| 1018 | hlist_del(&dev->hn); |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 1019 | devfree = dev; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1020 | break; |
| 1021 | } |
| 1022 | } |
| 1023 | spin_unlock(&me->hlock); |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 1024 | if (devfree) { |
| 1025 | free_dev(devfree); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1026 | goto rnext; |
| 1027 | } |
| 1028 | return; |
| 1029 | } |
| 1030 | |
| 1031 | static int fastrpc_device_release(struct inode *inode, struct file *file) |
| 1032 | { |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 1033 | struct file_data *fdata = (struct file_data *)file->private_data; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1034 | (void)fastrpc_release_current_dsp_process(); |
| 1035 | cleanup_current_dev(); |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 1036 | if (fdata) { |
| 1037 | struct fastrpc_mmap *map; |
| 1038 | struct hlist_node *n, *pos; |
| 1039 | file->private_data = 0; |
| 1040 | hlist_for_each_entry_safe(map, pos, n, &fdata->hlst, hn) { |
| 1041 | hlist_del(&map->hn); |
| 1042 | free_map(map); |
| 1043 | kfree(map); |
| 1044 | } |
| 1045 | kfree(fdata); |
| 1046 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1047 | return 0; |
| 1048 | } |
| 1049 | |
| 1050 | static int fastrpc_device_open(struct inode *inode, struct file *filp) |
| 1051 | { |
| 1052 | int err = 0; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 1053 | filp->private_data = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1054 | if (0 != try_module_get(THIS_MODULE)) { |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 1055 | struct file_data *fdata = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1056 | /* This call will cause a dev to be created |
| 1057 | * which will addref this module |
| 1058 | */ |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 1059 | VERIFY(err, 0 != (fdata = kzalloc(sizeof(*fdata), GFP_KERNEL))); |
| 1060 | if (err) |
| 1061 | goto bail; |
| 1062 | |
| 1063 | spin_lock_init(&fdata->hlock); |
| 1064 | INIT_HLIST_HEAD(&fdata->hlst); |
| 1065 | |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1066 | VERIFY(err, 0 == fastrpc_create_current_dsp_process()); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1067 | if (err) |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 1068 | goto bail; |
| 1069 | filp->private_data = fdata; |
| 1070 | bail: |
| 1071 | if (err) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1072 | cleanup_current_dev(); |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 1073 | kfree(fdata); |
| 1074 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1075 | module_put(THIS_MODULE); |
| 1076 | } |
| 1077 | return err; |
| 1078 | } |
| 1079 | |
| 1080 | |
| 1081 | static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num, |
| 1082 | unsigned long ioctl_param) |
| 1083 | { |
| 1084 | struct fastrpc_apps *me = &gfa; |
| 1085 | struct fastrpc_ioctl_invoke invoke; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 1086 | struct fastrpc_ioctl_mmap mmap; |
| 1087 | struct fastrpc_ioctl_munmap munmap; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1088 | remote_arg_t *pra = 0; |
| 1089 | void *param = (char *)ioctl_param; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 1090 | struct file_data *fdata = (struct file_data *)file->private_data; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1091 | int bufs, err = 0; |
| 1092 | |
| 1093 | switch (ioctl_num) { |
| 1094 | case FASTRPC_IOCTL_INVOKE: |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1095 | VERIFY(err, 0 == copy_from_user(&invoke, param, |
| 1096 | sizeof(invoke))); |
| 1097 | if (err) |
| 1098 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1099 | bufs = REMOTE_SCALARS_INBUFS(invoke.sc) + |
| 1100 | REMOTE_SCALARS_OUTBUFS(invoke.sc); |
| 1101 | if (bufs) { |
| 1102 | bufs = bufs * sizeof(*pra); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1103 | VERIFY(err, 0 != (pra = kmalloc(bufs, GFP_KERNEL))); |
| 1104 | if (err) |
| 1105 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1106 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1107 | VERIFY(err, 0 == copy_from_user(pra, invoke.pra, bufs)); |
| 1108 | if (err) |
| 1109 | goto bail; |
| 1110 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 0, &invoke, |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1111 | pra))); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1112 | if (err) |
| 1113 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1114 | break; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 1115 | case FASTRPC_IOCTL_MMAP: |
| 1116 | VERIFY(err, 0 == copy_from_user(&mmap, param, |
| 1117 | sizeof(mmap))); |
| 1118 | if (err) |
| 1119 | goto bail; |
| 1120 | VERIFY(err, 0 == (err = fastrpc_internal_mmap(me, fdata, |
| 1121 | &mmap))); |
| 1122 | if (err) |
| 1123 | goto bail; |
| 1124 | VERIFY(err, 0 == copy_to_user(param, &mmap, sizeof(mmap))); |
| 1125 | if (err) |
| 1126 | goto bail; |
| 1127 | break; |
| 1128 | case FASTRPC_IOCTL_MUNMAP: |
| 1129 | VERIFY(err, 0 == copy_from_user(&munmap, param, |
| 1130 | sizeof(munmap))); |
| 1131 | if (err) |
| 1132 | goto bail; |
| 1133 | VERIFY(err, 0 == (err = fastrpc_internal_munmap(me, fdata, |
| 1134 | &munmap))); |
| 1135 | if (err) |
| 1136 | goto bail; |
| 1137 | break; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1138 | default: |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1139 | err = -ENOTTY; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1140 | break; |
| 1141 | } |
| 1142 | bail: |
| 1143 | kfree(pra); |
| 1144 | return err; |
| 1145 | } |
| 1146 | |
| 1147 | static const struct file_operations fops = { |
| 1148 | .open = fastrpc_device_open, |
| 1149 | .release = fastrpc_device_release, |
| 1150 | .unlocked_ioctl = fastrpc_device_ioctl, |
| 1151 | }; |
| 1152 | |
| 1153 | static int __init fastrpc_device_init(void) |
| 1154 | { |
| 1155 | struct fastrpc_apps *me = &gfa; |
| 1156 | int err = 0; |
| 1157 | |
Mitchel Humpherys | ef5bbbe | 2013-05-15 12:44:51 -0700 | [diff] [blame^] | 1158 | memset(me, 0, sizeof(*me)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1159 | VERIFY(err, 0 == fastrpc_init()); |
| 1160 | if (err) |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame] | 1161 | goto fastrpc_bail; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1162 | VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, 1, DEVICE_NAME)); |
| 1163 | if (err) |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame] | 1164 | goto alloc_chrdev_bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1165 | cdev_init(&me->cdev, &fops); |
| 1166 | me->cdev.owner = THIS_MODULE; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1167 | VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0), 1)); |
| 1168 | if (err) |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame] | 1169 | goto cdev_init_bail; |
Mitchel Humpherys | 5587765 | 2013-02-02 11:23:42 -0800 | [diff] [blame] | 1170 | me->class = class_create(THIS_MODULE, "chardrv"); |
| 1171 | VERIFY(err, !IS_ERR(me->class)); |
| 1172 | if (err) |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame] | 1173 | goto class_create_bail; |
Mitchel Humpherys | 5587765 | 2013-02-02 11:23:42 -0800 | [diff] [blame] | 1174 | me->dev = device_create(me->class, NULL, MKDEV(MAJOR(me->dev_no), 0), |
| 1175 | NULL, DEVICE_NAME); |
| 1176 | VERIFY(err, !IS_ERR(me->dev)); |
| 1177 | if (err) |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame] | 1178 | goto device_create_bail; |
Mitchel Humpherys | 5587765 | 2013-02-02 11:23:42 -0800 | [diff] [blame] | 1179 | pr_info("'created /dev/%s c %d 0'\n", DEVICE_NAME, MAJOR(me->dev_no)); |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame] | 1180 | |
| 1181 | return 0; |
| 1182 | |
| 1183 | device_create_bail: |
| 1184 | class_destroy(me->class); |
| 1185 | class_create_bail: |
| 1186 | cdev_del(&me->cdev); |
| 1187 | cdev_init_bail: |
| 1188 | unregister_chrdev_region(me->dev_no, 1); |
| 1189 | alloc_chrdev_bail: |
| 1190 | fastrpc_deinit(); |
| 1191 | fastrpc_bail: |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1192 | return err; |
| 1193 | } |
| 1194 | |
| 1195 | static void __exit fastrpc_device_exit(void) |
| 1196 | { |
| 1197 | struct fastrpc_apps *me = &gfa; |
| 1198 | |
| 1199 | fastrpc_deinit(); |
Mitchel Humpherys | 5587765 | 2013-02-02 11:23:42 -0800 | [diff] [blame] | 1200 | device_destroy(me->class, MKDEV(MAJOR(me->dev_no), 0)); |
| 1201 | class_destroy(me->class); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1202 | cdev_del(&me->cdev); |
| 1203 | unregister_chrdev_region(me->dev_no, 1); |
| 1204 | } |
| 1205 | |
| 1206 | module_init(fastrpc_device_init); |
| 1207 | module_exit(fastrpc_device_exit); |
| 1208 | |
| 1209 | MODULE_LICENSE("GPL v2"); |