blob: d78327f1dd655904eca942c74278e85c65c96cf6 [file] [log] [blame]
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001/*
Mitchel Humpherys6b4c68c2013-02-06 12:03:20 -08002 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07003 *
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 Humpherys6b4c68c2013-02-06 12:03:20 -080014#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 Humpherys42e806e2012-09-30 22:27:53 -070029#include <linux/scatterlist.h>
Mitchel Humpherys6b4c68c2013-02-06 12:03:20 -080030#include <linux/fs.h>
31#include <linux/uaccess.h>
32#include <linux/device.h>
33
34#ifndef ION_ADSPRPC_HEAP_ID
35#define ION_ADSPRPC_HEAP_ID ION_AUDIO_HEAP_ID
36#endif /*ION_ADSPRPC_HEAP_ID*/
37
38#define RPC_TIMEOUT (5 * HZ)
39#define RPC_HASH_BITS 5
40#define RPC_HASH_SZ (1 << RPC_HASH_BITS)
41#define BALIGN 32
42
43#define LOCK_MMAP(kernel)\
44 do {\
45 if (!kernel)\
46 down_read(&current->mm->mmap_sem);\
47 } while (0)
48
49#define UNLOCK_MMAP(kernel)\
50 do {\
51 if (!kernel)\
52 up_read(&current->mm->mmap_sem);\
53 } while (0)
54
55
56static inline uint32_t buf_page_start(void *buf)
57{
58 uint32_t start = (uint32_t) buf & PAGE_MASK;
59 return start;
60}
61
62static inline uint32_t buf_page_offset(void *buf)
63{
64 uint32_t offset = (uint32_t) buf & (PAGE_SIZE - 1);
65 return offset;
66}
67
68static inline int buf_num_pages(void *buf, int len)
69{
70 uint32_t start = buf_page_start(buf) >> PAGE_SHIFT;
71 uint32_t end = (((uint32_t) buf + len - 1) & PAGE_MASK) >> PAGE_SHIFT;
72 int nPages = end - start + 1;
73 return nPages;
74}
75
76static inline uint32_t buf_page_size(uint32_t size)
77{
78 uint32_t sz = (size + (PAGE_SIZE - 1)) & PAGE_MASK;
79 return sz > PAGE_SIZE ? sz : PAGE_SIZE;
80}
81
82static inline int buf_get_pages(void *addr, int sz, int nr_pages, int access,
83 struct smq_phy_page *pages, int nr_elems)
84{
85 struct vm_area_struct *vma;
86 uint32_t start = buf_page_start(addr);
87 uint32_t len = nr_pages << PAGE_SHIFT;
88 unsigned long pfn;
89 int n = -1, err = 0;
90
91 VERIFY(err, 0 != access_ok(access ? VERIFY_WRITE : VERIFY_READ,
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -080092 (void __user *)start, len));
Mitchel Humpherys6b4c68c2013-02-06 12:03:20 -080093 if (err)
94 goto bail;
95 VERIFY(err, 0 != (vma = find_vma(current->mm, start)));
96 if (err)
97 goto bail;
98 VERIFY(err, ((uint32_t)addr + sz) <= vma->vm_end);
99 if (err)
100 goto bail;
101 n = 0;
102 VERIFY(err, 0 == follow_pfn(vma, start, &pfn));
103 if (err)
104 goto bail;
105 VERIFY(err, nr_elems > 0);
106 if (err)
107 goto bail;
108 pages->addr = __pfn_to_phys(pfn);
109 pages->size = len;
110 n++;
111 bail:
112 return n;
113}
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700114
115struct smq_invoke_ctx {
116 struct completion work;
117 int retval;
118 atomic_t free;
119};
120
121struct smq_context_list {
122 struct smq_invoke_ctx *ls;
123 int size;
124 int last;
125};
126
127struct fastrpc_apps {
128 smd_channel_t *chan;
129 struct smq_context_list clst;
130 struct completion work;
131 struct ion_client *iclient;
132 struct cdev cdev;
Mitchel Humpherys55877652013-02-02 11:23:42 -0800133 struct class *class;
134 struct device *dev;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700135 dev_t dev_no;
136 spinlock_t wrlock;
137 spinlock_t hlock;
138 struct hlist_head htbl[RPC_HASH_SZ];
139};
140
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800141struct fastrpc_mmap {
142 struct hlist_node hn;
143 struct ion_handle *handle;
144 void *virt;
145 uint32_t vaddrin;
146 uint32_t vaddrout;
147 int size;
148};
149
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700150struct fastrpc_buf {
151 struct ion_handle *handle;
152 void *virt;
153 ion_phys_addr_t phys;
154 int size;
155 int used;
156};
157
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800158struct file_data {
159 spinlock_t hlock;
160 struct hlist_head hlst;
161};
162
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700163struct fastrpc_device {
164 uint32_t tgid;
165 struct hlist_node hn;
166 struct fastrpc_buf buf;
167};
168
169static struct fastrpc_apps gfa;
170
171static void free_mem(struct fastrpc_buf *buf)
172{
173 struct fastrpc_apps *me = &gfa;
174
175 if (buf->handle) {
176 if (buf->virt) {
177 ion_unmap_kernel(me->iclient, buf->handle);
178 buf->virt = 0;
179 }
180 ion_free(me->iclient, buf->handle);
181 buf->handle = 0;
182 }
183}
184
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800185static void free_map(struct fastrpc_mmap *map)
186{
187 struct fastrpc_apps *me = &gfa;
188 if (map->handle) {
189 if (map->virt) {
190 ion_unmap_kernel(me->iclient, map->handle);
191 map->virt = 0;
192 }
193 ion_free(me->iclient, map->handle);
194 }
195 map->handle = 0;
196}
197
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700198static int alloc_mem(struct fastrpc_buf *buf)
199{
200 struct ion_client *clnt = gfa.iclient;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700201 struct sg_table *sg;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700202 int err = 0;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800203 buf->handle = 0;
204 buf->virt = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700205 buf->handle = ion_alloc(clnt, buf->size, SZ_4K,
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700206 ION_HEAP(ION_AUDIO_HEAP_ID), 0);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700207 VERIFY(err, 0 == IS_ERR_OR_NULL(buf->handle));
208 if (err)
209 goto bail;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700210 VERIFY(err, 0 != (buf->virt = ion_map_kernel(clnt, buf->handle)));
211 if (err)
212 goto bail;
213 VERIFY(err, 0 != (sg = ion_sg_table(clnt, buf->handle)));
214 if (err)
215 goto bail;
216 VERIFY(err, 1 == sg->nents);
217 if (err)
218 goto bail;
219 buf->phys = sg_dma_address(sg->sgl);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700220 bail:
221 if (err && !IS_ERR_OR_NULL(buf->handle))
222 free_mem(buf);
223 return err;
224}
225
226static int context_list_ctor(struct smq_context_list *me, int size)
227{
228 int err = 0;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700229 VERIFY(err, 0 != (me->ls = kzalloc(size, GFP_KERNEL)));
230 if (err)
231 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700232 me->size = size / sizeof(*me->ls);
233 me->last = 0;
234 bail:
235 return err;
236}
237
238static void context_list_dtor(struct smq_context_list *me)
239{
240 kfree(me->ls);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700241}
242
243static void context_list_alloc_ctx(struct smq_context_list *me,
244 struct smq_invoke_ctx **po)
245{
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700246 int i = me->last;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700247 struct smq_invoke_ctx *ctx;
248
249 for (;;) {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700250 i = i % me->size;
251 ctx = &me->ls[i];
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700252 if (atomic_read(&ctx->free) == 0)
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700253 if (atomic_cmpxchg(&ctx->free, 0, 1) == 0)
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700254 break;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700255 i++;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700256 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700257 me->last = i;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700258 ctx->retval = -1;
259 init_completion(&ctx->work);
260 *po = ctx;
261}
262
263static void context_free(struct smq_invoke_ctx *me)
264{
265 if (me)
266 atomic_set(&me->free, 0);
267}
268
269static void context_notify_user(struct smq_invoke_ctx *me, int retval)
270{
271 me->retval = retval;
272 complete(&me->work);
273}
274
275static void context_notify_all_users(struct smq_context_list *me)
276{
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700277 int i;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700278
279 if (!me->ls)
280 return;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700281 for (i = 0; i < me->size; ++i) {
282 if (atomic_read(&me->ls[i].free) != 0)
283 complete(&me->ls[i].work);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700284 }
285}
286
287static int get_page_list(uint32_t kernel, uint32_t sc, remote_arg_t *pra,
288 struct fastrpc_buf *ibuf, struct fastrpc_buf *obuf)
289{
290 struct smq_phy_page *pgstart, *pages;
291 struct smq_invoke_buf *list;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700292 int i, rlen, err = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700293 int inbufs = REMOTE_SCALARS_INBUFS(sc);
294 int outbufs = REMOTE_SCALARS_OUTBUFS(sc);
295
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700296 LOCK_MMAP(kernel);
297 *obuf = *ibuf;
298 retry:
299 list = smq_invoke_buf_start((remote_arg_t *)obuf->virt, sc);
300 pgstart = smq_phy_page_start(sc, list);
301 pages = pgstart + 1;
302 rlen = obuf->size - ((uint32_t)pages - (uint32_t)obuf->virt);
303 if (rlen < 0) {
304 rlen = ((uint32_t)pages - (uint32_t)obuf->virt) - obuf->size;
305 obuf->size += buf_page_size(rlen);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700306 VERIFY(err, 0 == alloc_mem(obuf));
307 if (err)
308 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700309 goto retry;
310 }
311 pgstart->addr = obuf->phys;
312 pgstart->size = obuf->size;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700313 for (i = 0; i < inbufs + outbufs; ++i) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700314 void *buf;
315 int len, num;
316
Mitchel Humpherysf581c512012-10-19 11:29:36 -0700317 list[i].num = 0;
318 list[i].pgidx = 0;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700319 len = pra[i].buf.len;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700320 if (!len)
321 continue;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700322 buf = pra[i].buf.pv;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700323 num = buf_num_pages(buf, len);
324 if (!kernel)
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700325 list[i].num = buf_get_pages(buf, len, num,
326 i >= inbufs, pages, rlen / sizeof(*pages));
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700327 else
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700328 list[i].num = 0;
329 VERIFY(err, list[i].num >= 0);
330 if (err)
331 goto bail;
332 if (list[i].num) {
333 list[i].pgidx = pages - pgstart;
334 pages = pages + list[i].num;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700335 } else if (rlen > sizeof(*pages)) {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700336 list[i].pgidx = pages - pgstart;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700337 pages = pages + 1;
338 } else {
339 if (obuf->handle != ibuf->handle)
340 free_mem(obuf);
341 obuf->size += buf_page_size(sizeof(*pages));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700342 VERIFY(err, 0 == alloc_mem(obuf));
343 if (err)
344 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700345 goto retry;
346 }
347 rlen = obuf->size - ((uint32_t) pages - (uint32_t) obuf->virt);
348 }
349 obuf->used = obuf->size - rlen;
350 bail:
351 if (err && (obuf->handle != ibuf->handle))
352 free_mem(obuf);
353 UNLOCK_MMAP(kernel);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700354 return err;
355}
356
357static int get_args(uint32_t kernel, uint32_t sc, remote_arg_t *pra,
358 remote_arg_t *rpra, remote_arg_t *upra,
359 struct fastrpc_buf *ibuf, struct fastrpc_buf **abufs,
360 int *nbufs)
361{
362 struct smq_invoke_buf *list;
363 struct fastrpc_buf *pbuf = ibuf, *obufs = 0;
364 struct smq_phy_page *pages;
365 void *args;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700366 int i, rlen, size, used, inh, bufs = 0, err = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700367 int inbufs = REMOTE_SCALARS_INBUFS(sc);
368 int outbufs = REMOTE_SCALARS_OUTBUFS(sc);
369
370 list = smq_invoke_buf_start(rpra, sc);
371 pages = smq_phy_page_start(sc, list);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700372 used = ALIGN(pbuf->used, BALIGN);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700373 args = (void *)((char *)pbuf->virt + used);
374 rlen = pbuf->size - used;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700375 for (i = 0; i < inbufs + outbufs; ++i) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700376
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700377 rpra[i].buf.len = pra[i].buf.len;
Mitchel Humpherysf581c512012-10-19 11:29:36 -0700378 if (!rpra[i].buf.len)
379 continue;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700380 if (list[i].num) {
381 rpra[i].buf.pv = pra[i].buf.pv;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700382 continue;
383 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700384 if (rlen < pra[i].buf.len) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700385 struct fastrpc_buf *b;
386 pbuf->used = pbuf->size - rlen;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700387 VERIFY(err, 0 != (b = krealloc(obufs,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700388 (bufs + 1) * sizeof(*obufs), GFP_KERNEL)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700389 if (err)
390 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700391 obufs = b;
392 pbuf = obufs + bufs;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700393 pbuf->size = buf_num_pages(0, pra[i].buf.len) *
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700394 PAGE_SIZE;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700395 VERIFY(err, 0 == alloc_mem(pbuf));
396 if (err)
397 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700398 bufs++;
399 args = pbuf->virt;
400 rlen = pbuf->size;
401 }
Mitchel Humpherys0bc3aa52013-02-02 11:31:15 -0800402 list[i].num = 1;
403 pages[list[i].pgidx].addr =
404 buf_page_start((void *)(pbuf->phys +
405 (pbuf->size - rlen)));
406 pages[list[i].pgidx].size =
407 buf_page_size(pra[i].buf.len);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700408 if (i < inbufs) {
409 if (!kernel) {
410 VERIFY(err, 0 == copy_from_user(args,
411 pra[i].buf.pv, pra[i].buf.len));
412 if (err)
413 goto bail;
414 } else {
415 memmove(args, pra[i].buf.pv, pra[i].buf.len);
416 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700417 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700418 rpra[i].buf.pv = args;
419 args = (void *)((char *)args + ALIGN(pra[i].buf.len, BALIGN));
420 rlen -= ALIGN(pra[i].buf.len, BALIGN);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700421 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700422 for (i = 0; i < inbufs; ++i) {
423 if (rpra[i].buf.len)
424 dmac_flush_range(rpra[i].buf.pv,
425 (char *)rpra[i].buf.pv + rpra[i].buf.len);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700426 }
427 pbuf->used = pbuf->size - rlen;
428 size = sizeof(*rpra) * REMOTE_SCALARS_INHANDLES(sc);
429 if (size) {
430 inh = inbufs + outbufs;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700431 if (!kernel) {
432 VERIFY(err, 0 == copy_from_user(&rpra[inh], &upra[inh],
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700433 size));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700434 if (err)
435 goto bail;
436 } else {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700437 memmove(&rpra[inh], &upra[inh], size);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700438 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700439 }
440 dmac_flush_range(rpra, (char *)rpra + used);
441 bail:
442 *abufs = obufs;
443 *nbufs = bufs;
444 return err;
445}
446
447static int put_args(uint32_t kernel, uint32_t sc, remote_arg_t *pra,
448 remote_arg_t *rpra, remote_arg_t *upra)
449{
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700450 int i, inbufs, outbufs, outh, size;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700451 int err = 0;
452
453 inbufs = REMOTE_SCALARS_INBUFS(sc);
454 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700455 for (i = inbufs; i < inbufs + outbufs; ++i) {
456 if (rpra[i].buf.pv != pra[i].buf.pv) {
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800457 if (!kernel) {
458 VERIFY(err, 0 == copy_to_user(pra[i].buf.pv,
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700459 rpra[i].buf.pv, rpra[i].buf.len));
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800460 if (err)
461 goto bail;
462 } else {
463 memmove(pra[i].buf.pv, rpra[i].buf.pv,
464 rpra[i].buf.len);
465 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700466 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700467 }
468 size = sizeof(*rpra) * REMOTE_SCALARS_OUTHANDLES(sc);
469 if (size) {
470 outh = inbufs + outbufs + REMOTE_SCALARS_INHANDLES(sc);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700471 if (!kernel) {
472 VERIFY(err, 0 == copy_to_user(&upra[outh], &rpra[outh],
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700473 size));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700474 if (err)
475 goto bail;
476 } else {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700477 memmove(&upra[outh], &rpra[outh], size);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700478 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700479 }
480 bail:
481 return err;
482}
483
484static void inv_args(uint32_t sc, remote_arg_t *rpra, int used)
485{
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700486 int i, inbufs, outbufs;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700487 int inv = 0;
488
489 inbufs = REMOTE_SCALARS_INBUFS(sc);
490 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700491 for (i = inbufs; i < inbufs + outbufs; ++i) {
492 if (buf_page_start(rpra) == buf_page_start(rpra[i].buf.pv))
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700493 inv = 1;
Mitchel Humpherysf581c512012-10-19 11:29:36 -0700494 else if (rpra[i].buf.len)
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700495 dmac_inv_range(rpra[i].buf.pv,
496 (char *)rpra[i].buf.pv + rpra[i].buf.len);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700497 }
498
499 if (inv || REMOTE_SCALARS_OUTHANDLES(sc))
500 dmac_inv_range(rpra, (char *)rpra + used);
501}
502
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800503static int fastrpc_invoke_send(struct fastrpc_apps *me,
504 uint32_t kernel, uint32_t handle,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700505 uint32_t sc, struct smq_invoke_ctx *ctx,
506 struct fastrpc_buf *buf)
507{
508 struct smq_msg msg;
509 int err = 0, len;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700510 msg.pid = current->tgid;
511 msg.tid = current->pid;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800512 if (kernel)
513 msg.pid = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700514 msg.invoke.header.ctx = ctx;
515 msg.invoke.header.handle = handle;
516 msg.invoke.header.sc = sc;
517 msg.invoke.page.addr = buf->phys;
518 msg.invoke.page.size = buf_page_size(buf->used);
519 spin_lock(&me->wrlock);
520 len = smd_write(me->chan, &msg, sizeof(msg));
521 spin_unlock(&me->wrlock);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700522 VERIFY(err, len == sizeof(msg));
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700523 return err;
524}
525
526static void fastrpc_deinit(void)
527{
528 struct fastrpc_apps *me = &gfa;
529
530 if (me->chan)
531 (void)smd_close(me->chan);
532 context_list_dtor(&me->clst);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700533 if (me->iclient)
534 ion_client_destroy(me->iclient);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700535 me->iclient = 0;
536 me->chan = 0;
537}
538
539static void fastrpc_read_handler(void)
540{
541 struct fastrpc_apps *me = &gfa;
542 struct smq_invoke_rsp rsp;
543 int err = 0;
544
545 do {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700546 VERIFY(err, sizeof(rsp) ==
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700547 smd_read_from_cb(me->chan, &rsp, sizeof(rsp)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700548 if (err)
549 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700550 context_notify_user(rsp.ctx, rsp.retval);
551 } while (!err);
552 bail:
553 return;
554}
555
556static void smd_event_handler(void *priv, unsigned event)
557{
558 struct fastrpc_apps *me = (struct fastrpc_apps *)priv;
559
560 switch (event) {
561 case SMD_EVENT_OPEN:
562 complete(&(me->work));
563 break;
564 case SMD_EVENT_CLOSE:
565 context_notify_all_users(&me->clst);
566 break;
567 case SMD_EVENT_DATA:
568 fastrpc_read_handler();
569 break;
570 }
571}
572
573static int fastrpc_init(void)
574{
575 int err = 0;
576 struct fastrpc_apps *me = &gfa;
577
578 if (me->chan == 0) {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700579 int i;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700580 spin_lock_init(&me->hlock);
581 spin_lock_init(&me->wrlock);
582 init_completion(&me->work);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700583 for (i = 0; i < RPC_HASH_SZ; ++i)
584 INIT_HLIST_HEAD(&me->htbl[i]);
585 VERIFY(err, 0 == context_list_ctor(&me->clst, SZ_4K));
586 if (err)
587 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700588 me->iclient = msm_ion_client_create(ION_HEAP_CARVEOUT_MASK,
589 DEVICE_NAME);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700590 VERIFY(err, 0 == IS_ERR_OR_NULL(me->iclient));
591 if (err)
592 goto bail;
593 VERIFY(err, 0 == smd_named_open_on_edge(FASTRPC_SMD_GUID,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700594 SMD_APPS_QDSP, &me->chan,
595 me, smd_event_handler));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700596 if (err)
597 goto bail;
598 VERIFY(err, 0 != wait_for_completion_timeout(&me->work,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700599 RPC_TIMEOUT));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700600 if (err)
601 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700602 }
603 bail:
604 if (err)
605 fastrpc_deinit();
606 return err;
607}
608
609static void free_dev(struct fastrpc_device *dev)
610{
611 if (dev) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700612 free_mem(&dev->buf);
613 kfree(dev);
Mitchel Humpherys6b4c68c2013-02-06 12:03:20 -0800614 module_put(THIS_MODULE);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700615 }
616}
617
618static int alloc_dev(struct fastrpc_device **dev)
619{
620 int err = 0;
621 struct fastrpc_device *fd = 0;
622
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700623 VERIFY(err, 0 != try_module_get(THIS_MODULE));
624 if (err)
625 goto bail;
626 VERIFY(err, 0 != (fd = kzalloc(sizeof(*fd), GFP_KERNEL)));
627 if (err)
628 goto bail;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800629
630 INIT_HLIST_NODE(&fd->hn);
631
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700632 fd->buf.size = PAGE_SIZE;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700633 VERIFY(err, 0 == alloc_mem(&fd->buf));
634 if (err)
635 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700636 fd->tgid = current->tgid;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800637
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700638 *dev = fd;
639 bail:
640 if (err)
641 free_dev(fd);
642 return err;
643}
644
645static int get_dev(struct fastrpc_apps *me, struct fastrpc_device **rdev)
646{
647 struct hlist_head *head;
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800648 struct fastrpc_device *dev = 0, *devfree = 0;
649 struct hlist_node *pos, *n;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700650 uint32_t h = hash_32(current->tgid, RPC_HASH_BITS);
651 int err = 0;
652
653 spin_lock(&me->hlock);
654 head = &me->htbl[h];
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800655 hlist_for_each_entry_safe(dev, pos, n, head, hn) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700656 if (dev->tgid == current->tgid) {
657 hlist_del(&dev->hn);
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800658 devfree = dev;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700659 break;
660 }
661 }
662 spin_unlock(&me->hlock);
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800663 VERIFY(err, devfree != 0);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700664 if (err)
665 goto bail;
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800666 *rdev = devfree;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700667 bail:
668 if (err) {
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800669 free_dev(devfree);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700670 err = alloc_dev(rdev);
671 }
672 return err;
673}
674
675static void add_dev(struct fastrpc_apps *me, struct fastrpc_device *dev)
676{
677 struct hlist_head *head;
678 uint32_t h = hash_32(current->tgid, RPC_HASH_BITS);
679
680 spin_lock(&me->hlock);
681 head = &me->htbl[h];
682 hlist_add_head(&dev->hn, head);
683 spin_unlock(&me->hlock);
684 return;
685}
686
687static int fastrpc_release_current_dsp_process(void);
688
689static int fastrpc_internal_invoke(struct fastrpc_apps *me, uint32_t kernel,
690 struct fastrpc_ioctl_invoke *invoke, remote_arg_t *pra)
691{
692 remote_arg_t *rpra = 0;
693 struct fastrpc_device *dev = 0;
694 struct smq_invoke_ctx *ctx = 0;
695 struct fastrpc_buf obuf, *abufs = 0, *b;
696 int interrupted = 0;
697 uint32_t sc;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700698 int i, nbufs = 0, err = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700699
700 sc = invoke->sc;
701 obuf.handle = 0;
702 if (REMOTE_SCALARS_LENGTH(sc)) {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700703 VERIFY(err, 0 == get_dev(me, &dev));
704 if (err)
705 goto bail;
706 VERIFY(err, 0 == get_page_list(kernel, sc, pra, &dev->buf,
707 &obuf));
708 if (err)
709 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700710 rpra = (remote_arg_t *)obuf.virt;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700711 VERIFY(err, 0 == get_args(kernel, sc, pra, rpra, invoke->pra,
712 &obuf, &abufs, &nbufs));
713 if (err)
714 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700715 }
716
717 context_list_alloc_ctx(&me->clst, &ctx);
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800718 VERIFY(err, 0 == fastrpc_invoke_send(me, kernel, invoke->handle, sc,
719 ctx, &obuf));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700720 if (err)
721 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700722 inv_args(sc, rpra, obuf.used);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700723 VERIFY(err, 0 == (interrupted =
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700724 wait_for_completion_interruptible(&ctx->work)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700725 if (err)
726 goto bail;
727 VERIFY(err, 0 == (err = ctx->retval));
728 if (err)
729 goto bail;
730 VERIFY(err, 0 == put_args(kernel, sc, pra, rpra, invoke->pra));
731 if (err)
732 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700733 bail:
734 if (interrupted) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700735 if (!kernel)
736 (void)fastrpc_release_current_dsp_process();
737 wait_for_completion(&ctx->work);
738 }
739 context_free(ctx);
Mitchel Humpherys6b4c68c2013-02-06 12:03:20 -0800740
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700741 for (i = 0, b = abufs; i < nbufs; ++i, ++b)
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700742 free_mem(b);
Mitchel Humpherys6b4c68c2013-02-06 12:03:20 -0800743
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700744 kfree(abufs);
745 if (dev) {
746 add_dev(me, dev);
747 if (obuf.handle != dev->buf.handle)
748 free_mem(&obuf);
749 }
750 return err;
751}
752
753static int fastrpc_create_current_dsp_process(void)
754{
755 int err = 0;
756 struct fastrpc_ioctl_invoke ioctl;
757 struct fastrpc_apps *me = &gfa;
758 remote_arg_t ra[1];
759 int tgid = 0;
760
761 tgid = current->tgid;
762 ra[0].buf.pv = &tgid;
763 ra[0].buf.len = sizeof(tgid);
764 ioctl.handle = 1;
765 ioctl.sc = REMOTE_SCALARS_MAKE(0, 1, 0);
766 ioctl.pra = ra;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800767 VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra)));
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700768 return err;
769}
770
771static int fastrpc_release_current_dsp_process(void)
772{
773 int err = 0;
774 struct fastrpc_apps *me = &gfa;
775 struct fastrpc_ioctl_invoke ioctl;
776 remote_arg_t ra[1];
777 int tgid = 0;
778
779 tgid = current->tgid;
780 ra[0].buf.pv = &tgid;
781 ra[0].buf.len = sizeof(tgid);
782 ioctl.handle = 1;
783 ioctl.sc = REMOTE_SCALARS_MAKE(1, 1, 0);
784 ioctl.pra = ra;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800785 VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra)));
786 return err;
787}
788
789static int fastrpc_mmap_on_dsp(struct fastrpc_apps *me,
790 struct fastrpc_ioctl_mmap *mmap,
791 struct smq_phy_page *pages,
792 int num)
793{
794 struct fastrpc_ioctl_invoke ioctl;
795 remote_arg_t ra[3];
796 int err = 0;
797 struct {
798 int pid;
799 uint32_t flags;
800 uint32_t vaddrin;
801 int num;
802 } inargs;
803
804 struct {
805 uint32_t vaddrout;
806 } routargs;
807 inargs.pid = current->tgid;
808 inargs.vaddrin = mmap->vaddrin;
809 inargs.flags = mmap->flags;
810 inargs.num = num;
811 ra[0].buf.pv = &inargs;
812 ra[0].buf.len = sizeof(inargs);
813
814 ra[1].buf.pv = pages;
815 ra[1].buf.len = num * sizeof(*pages);
816
817 ra[2].buf.pv = &routargs;
818 ra[2].buf.len = sizeof(routargs);
819
820 ioctl.handle = 1;
821 ioctl.sc = REMOTE_SCALARS_MAKE(2, 2, 1);
822 ioctl.pra = ra;
823 VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra)));
824 mmap->vaddrout = routargs.vaddrout;
825 if (err)
826 goto bail;
827bail:
828 return err;
829}
830
831static int fastrpc_munmap_on_dsp(struct fastrpc_apps *me,
832 struct fastrpc_ioctl_munmap *munmap)
833{
834 struct fastrpc_ioctl_invoke ioctl;
835 remote_arg_t ra[1];
836 int err = 0;
837 struct {
838 int pid;
839 uint32_t vaddrout;
840 int size;
841 } inargs;
842
843 inargs.pid = current->tgid;
844 inargs.size = munmap->size;
845 inargs.vaddrout = munmap->vaddrout;
846 ra[0].buf.pv = &inargs;
847 ra[0].buf.len = sizeof(inargs);
848
849 ioctl.handle = 1;
850 ioctl.sc = REMOTE_SCALARS_MAKE(3, 1, 0);
851 ioctl.pra = ra;
852 VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra)));
853 return err;
854}
855
856static int fastrpc_internal_munmap(struct fastrpc_apps *me,
857 struct file_data *fdata,
858 struct fastrpc_ioctl_munmap *munmap)
859{
860 int err = 0;
861 struct fastrpc_mmap *map = 0, *mapfree = 0;
862 struct hlist_node *pos, *n;
863 VERIFY(err, 0 == (err = fastrpc_munmap_on_dsp(me, munmap)));
864 if (err)
865 goto bail;
866 spin_lock(&fdata->hlock);
867 hlist_for_each_entry_safe(map, pos, n, &fdata->hlst, hn) {
868 if (map->vaddrout == munmap->vaddrout &&
869 map->size == munmap->size) {
870 hlist_del(&map->hn);
871 mapfree = map;
872 map = 0;
873 break;
874 }
875 }
876 spin_unlock(&fdata->hlock);
877bail:
878 if (mapfree) {
879 free_map(mapfree);
880 kfree(mapfree);
881 }
882 return err;
883}
884
885
886static int fastrpc_internal_mmap(struct fastrpc_apps *me,
887 struct file_data *fdata,
888 struct fastrpc_ioctl_mmap *mmap)
889{
890 struct ion_client *clnt = gfa.iclient;
891 struct fastrpc_mmap *map = 0;
892 struct smq_phy_page *pages = 0;
893 void *buf;
894 int len;
895 int num;
896 int err = 0;
897
898 VERIFY(err, 0 != (map = kzalloc(sizeof(*map), GFP_KERNEL)));
899 if (err)
900 goto bail;
901 map->handle = ion_import_dma_buf(clnt, mmap->fd);
902 VERIFY(err, 0 == IS_ERR_OR_NULL(map->handle));
903 if (err)
904 goto bail;
905 VERIFY(err, 0 != (map->virt = ion_map_kernel(clnt, map->handle)));
906 if (err)
907 goto bail;
908 buf = (void *)mmap->vaddrin;
909 len = mmap->size;
910 num = buf_num_pages(buf, len);
911 VERIFY(err, 0 != (pages = kzalloc(num * sizeof(*pages), GFP_KERNEL)));
912 if (err)
913 goto bail;
914 VERIFY(err, 0 < (num = buf_get_pages(buf, len, num, 1, pages, num)));
915 if (err)
916 goto bail;
917
918 VERIFY(err, 0 == fastrpc_mmap_on_dsp(me, mmap, pages, num));
919 if (err)
920 goto bail;
921 map->vaddrin = mmap->vaddrin;
922 map->vaddrout = mmap->vaddrout;
923 map->size = mmap->size;
924 INIT_HLIST_NODE(&map->hn);
925 spin_lock(&fdata->hlock);
926 hlist_add_head(&map->hn, &fdata->hlst);
927 spin_unlock(&fdata->hlock);
928 bail:
929 if (err && map) {
930 free_map(map);
931 kfree(map);
932 }
933 kfree(pages);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700934 return err;
935}
936
937static void cleanup_current_dev(void)
938{
939 struct fastrpc_apps *me = &gfa;
940 uint32_t h = hash_32(current->tgid, RPC_HASH_BITS);
941 struct hlist_head *head;
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800942 struct hlist_node *pos, *n;
943 struct fastrpc_device *dev, *devfree;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700944
945 rnext:
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800946 devfree = dev = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700947 spin_lock(&me->hlock);
948 head = &me->htbl[h];
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800949 hlist_for_each_entry_safe(dev, pos, n, head, hn) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700950 if (dev->tgid == current->tgid) {
951 hlist_del(&dev->hn);
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800952 devfree = dev;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700953 break;
954 }
955 }
956 spin_unlock(&me->hlock);
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800957 if (devfree) {
958 free_dev(devfree);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700959 goto rnext;
960 }
961 return;
962}
963
964static int fastrpc_device_release(struct inode *inode, struct file *file)
965{
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800966 struct file_data *fdata = (struct file_data *)file->private_data;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700967 (void)fastrpc_release_current_dsp_process();
968 cleanup_current_dev();
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800969 if (fdata) {
970 struct fastrpc_mmap *map;
971 struct hlist_node *n, *pos;
972 file->private_data = 0;
973 hlist_for_each_entry_safe(map, pos, n, &fdata->hlst, hn) {
974 hlist_del(&map->hn);
975 free_map(map);
976 kfree(map);
977 }
978 kfree(fdata);
979 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700980 return 0;
981}
982
983static int fastrpc_device_open(struct inode *inode, struct file *filp)
984{
985 int err = 0;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800986 filp->private_data = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700987 if (0 != try_module_get(THIS_MODULE)) {
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800988 struct file_data *fdata = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700989 /* This call will cause a dev to be created
990 * which will addref this module
991 */
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800992 VERIFY(err, 0 != (fdata = kzalloc(sizeof(*fdata), GFP_KERNEL)));
993 if (err)
994 goto bail;
995
996 spin_lock_init(&fdata->hlock);
997 INIT_HLIST_HEAD(&fdata->hlst);
998
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700999 VERIFY(err, 0 == fastrpc_create_current_dsp_process());
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001000 if (err)
Mitchel Humpherys0d99a792013-03-05 13:41:14 -08001001 goto bail;
1002 filp->private_data = fdata;
1003bail:
1004 if (err) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001005 cleanup_current_dev();
Mitchel Humpherys0d99a792013-03-05 13:41:14 -08001006 kfree(fdata);
1007 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001008 module_put(THIS_MODULE);
1009 }
1010 return err;
1011}
1012
1013
1014static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num,
1015 unsigned long ioctl_param)
1016{
1017 struct fastrpc_apps *me = &gfa;
1018 struct fastrpc_ioctl_invoke invoke;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -08001019 struct fastrpc_ioctl_mmap mmap;
1020 struct fastrpc_ioctl_munmap munmap;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001021 remote_arg_t *pra = 0;
1022 void *param = (char *)ioctl_param;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -08001023 struct file_data *fdata = (struct file_data *)file->private_data;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001024 int bufs, err = 0;
1025
1026 switch (ioctl_num) {
1027 case FASTRPC_IOCTL_INVOKE:
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001028 VERIFY(err, 0 == copy_from_user(&invoke, param,
1029 sizeof(invoke)));
1030 if (err)
1031 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001032 bufs = REMOTE_SCALARS_INBUFS(invoke.sc) +
1033 REMOTE_SCALARS_OUTBUFS(invoke.sc);
1034 if (bufs) {
1035 bufs = bufs * sizeof(*pra);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001036 VERIFY(err, 0 != (pra = kmalloc(bufs, GFP_KERNEL)));
1037 if (err)
1038 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001039 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001040 VERIFY(err, 0 == copy_from_user(pra, invoke.pra, bufs));
1041 if (err)
1042 goto bail;
1043 VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 0, &invoke,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001044 pra)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001045 if (err)
1046 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001047 break;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -08001048 case FASTRPC_IOCTL_MMAP:
1049 VERIFY(err, 0 == copy_from_user(&mmap, param,
1050 sizeof(mmap)));
1051 if (err)
1052 goto bail;
1053 VERIFY(err, 0 == (err = fastrpc_internal_mmap(me, fdata,
1054 &mmap)));
1055 if (err)
1056 goto bail;
1057 VERIFY(err, 0 == copy_to_user(param, &mmap, sizeof(mmap)));
1058 if (err)
1059 goto bail;
1060 break;
1061 case FASTRPC_IOCTL_MUNMAP:
1062 VERIFY(err, 0 == copy_from_user(&munmap, param,
1063 sizeof(munmap)));
1064 if (err)
1065 goto bail;
1066 VERIFY(err, 0 == (err = fastrpc_internal_munmap(me, fdata,
1067 &munmap)));
1068 if (err)
1069 goto bail;
1070 break;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001071 default:
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001072 err = -ENOTTY;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001073 break;
1074 }
1075 bail:
1076 kfree(pra);
1077 return err;
1078}
1079
1080static const struct file_operations fops = {
1081 .open = fastrpc_device_open,
1082 .release = fastrpc_device_release,
1083 .unlocked_ioctl = fastrpc_device_ioctl,
1084};
1085
1086static int __init fastrpc_device_init(void)
1087{
1088 struct fastrpc_apps *me = &gfa;
1089 int err = 0;
1090
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001091 VERIFY(err, 0 == fastrpc_init());
1092 if (err)
1093 goto bail;
1094 VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, 1, DEVICE_NAME));
1095 if (err)
1096 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001097 cdev_init(&me->cdev, &fops);
1098 me->cdev.owner = THIS_MODULE;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001099 VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0), 1));
1100 if (err)
1101 goto bail;
Mitchel Humpherys55877652013-02-02 11:23:42 -08001102 me->class = class_create(THIS_MODULE, "chardrv");
1103 VERIFY(err, !IS_ERR(me->class));
1104 if (err)
1105 goto bail;
1106 me->dev = device_create(me->class, NULL, MKDEV(MAJOR(me->dev_no), 0),
1107 NULL, DEVICE_NAME);
1108 VERIFY(err, !IS_ERR(me->dev));
1109 if (err)
1110 goto bail;
1111 pr_info("'created /dev/%s c %d 0'\n", DEVICE_NAME, MAJOR(me->dev_no));
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001112 bail:
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001113 if (err) {
1114 if (me->dev_no)
1115 unregister_chrdev_region(me->dev_no, 1);
Mitchel Humpherys55877652013-02-02 11:23:42 -08001116 if (me->class)
1117 class_destroy(me->class);
1118 if (me->cdev.owner)
1119 cdev_del(&me->cdev);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001120 fastrpc_deinit();
1121 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001122 return err;
1123}
1124
1125static void __exit fastrpc_device_exit(void)
1126{
1127 struct fastrpc_apps *me = &gfa;
1128
1129 fastrpc_deinit();
Mitchel Humpherys55877652013-02-02 11:23:42 -08001130 device_destroy(me->class, MKDEV(MAJOR(me->dev_no), 0));
1131 class_destroy(me->class);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001132 cdev_del(&me->cdev);
1133 unregister_chrdev_region(me->dev_no, 1);
1134}
1135
1136module_init(fastrpc_device_init);
1137module_exit(fastrpc_device_exit);
1138
1139MODULE_LICENSE("GPL v2");