blob: 51578e04a8b1bad864f84fd205d1e8af9e04e004 [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
Matt Wagantall818e23f2013-04-22 20:36:52 -0700530 smd_close(me->chan);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700531 context_list_dtor(&me->clst);
Matt Wagantall818e23f2013-04-22 20:36:52 -0700532 ion_client_destroy(me->iclient);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700533 me->iclient = 0;
534 me->chan = 0;
535}
536
537static void fastrpc_read_handler(void)
538{
539 struct fastrpc_apps *me = &gfa;
540 struct smq_invoke_rsp rsp;
541 int err = 0;
542
543 do {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700544 VERIFY(err, sizeof(rsp) ==
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700545 smd_read_from_cb(me->chan, &rsp, sizeof(rsp)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700546 if (err)
547 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700548 context_notify_user(rsp.ctx, rsp.retval);
549 } while (!err);
550 bail:
551 return;
552}
553
554static void smd_event_handler(void *priv, unsigned event)
555{
556 struct fastrpc_apps *me = (struct fastrpc_apps *)priv;
557
558 switch (event) {
559 case SMD_EVENT_OPEN:
560 complete(&(me->work));
561 break;
562 case SMD_EVENT_CLOSE:
563 context_notify_all_users(&me->clst);
564 break;
565 case SMD_EVENT_DATA:
566 fastrpc_read_handler();
567 break;
568 }
569}
570
571static int fastrpc_init(void)
572{
573 int err = 0;
574 struct fastrpc_apps *me = &gfa;
575
576 if (me->chan == 0) {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700577 int i;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700578 spin_lock_init(&me->hlock);
579 spin_lock_init(&me->wrlock);
580 init_completion(&me->work);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700581 for (i = 0; i < RPC_HASH_SZ; ++i)
582 INIT_HLIST_HEAD(&me->htbl[i]);
583 VERIFY(err, 0 == context_list_ctor(&me->clst, SZ_4K));
584 if (err)
Matt Wagantall818e23f2013-04-22 20:36:52 -0700585 goto context_list_bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700586 me->iclient = msm_ion_client_create(ION_HEAP_CARVEOUT_MASK,
587 DEVICE_NAME);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700588 VERIFY(err, 0 == IS_ERR_OR_NULL(me->iclient));
589 if (err)
Matt Wagantall818e23f2013-04-22 20:36:52 -0700590 goto ion_bail;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700591 VERIFY(err, 0 == smd_named_open_on_edge(FASTRPC_SMD_GUID,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700592 SMD_APPS_QDSP, &me->chan,
593 me, smd_event_handler));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700594 if (err)
Matt Wagantall818e23f2013-04-22 20:36:52 -0700595 goto smd_bail;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700596 VERIFY(err, 0 != wait_for_completion_timeout(&me->work,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700597 RPC_TIMEOUT));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700598 if (err)
Matt Wagantall818e23f2013-04-22 20:36:52 -0700599 goto completion_bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700600 }
Matt Wagantall818e23f2013-04-22 20:36:52 -0700601
602 return 0;
603
604completion_bail:
605 smd_close(me->chan);
606smd_bail:
607 ion_client_destroy(me->iclient);
608ion_bail:
609 context_list_dtor(&me->clst);
610context_list_bail:
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700611 return err;
612}
613
614static void free_dev(struct fastrpc_device *dev)
615{
616 if (dev) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700617 free_mem(&dev->buf);
618 kfree(dev);
Mitchel Humpherys6b4c68c2013-02-06 12:03:20 -0800619 module_put(THIS_MODULE);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700620 }
621}
622
623static int alloc_dev(struct fastrpc_device **dev)
624{
625 int err = 0;
626 struct fastrpc_device *fd = 0;
627
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700628 VERIFY(err, 0 != try_module_get(THIS_MODULE));
629 if (err)
630 goto bail;
631 VERIFY(err, 0 != (fd = kzalloc(sizeof(*fd), GFP_KERNEL)));
632 if (err)
633 goto bail;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800634
635 INIT_HLIST_NODE(&fd->hn);
636
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700637 fd->buf.size = PAGE_SIZE;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700638 VERIFY(err, 0 == alloc_mem(&fd->buf));
639 if (err)
640 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700641 fd->tgid = current->tgid;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800642
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700643 *dev = fd;
644 bail:
645 if (err)
646 free_dev(fd);
647 return err;
648}
649
650static int get_dev(struct fastrpc_apps *me, struct fastrpc_device **rdev)
651{
652 struct hlist_head *head;
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800653 struct fastrpc_device *dev = 0, *devfree = 0;
654 struct hlist_node *pos, *n;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700655 uint32_t h = hash_32(current->tgid, RPC_HASH_BITS);
656 int err = 0;
657
658 spin_lock(&me->hlock);
659 head = &me->htbl[h];
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800660 hlist_for_each_entry_safe(dev, pos, n, head, hn) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700661 if (dev->tgid == current->tgid) {
662 hlist_del(&dev->hn);
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800663 devfree = dev;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700664 break;
665 }
666 }
667 spin_unlock(&me->hlock);
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800668 VERIFY(err, devfree != 0);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700669 if (err)
670 goto bail;
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800671 *rdev = devfree;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700672 bail:
673 if (err) {
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800674 free_dev(devfree);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700675 err = alloc_dev(rdev);
676 }
677 return err;
678}
679
680static void add_dev(struct fastrpc_apps *me, struct fastrpc_device *dev)
681{
682 struct hlist_head *head;
683 uint32_t h = hash_32(current->tgid, RPC_HASH_BITS);
684
685 spin_lock(&me->hlock);
686 head = &me->htbl[h];
687 hlist_add_head(&dev->hn, head);
688 spin_unlock(&me->hlock);
689 return;
690}
691
692static int fastrpc_release_current_dsp_process(void);
693
694static int fastrpc_internal_invoke(struct fastrpc_apps *me, uint32_t kernel,
695 struct fastrpc_ioctl_invoke *invoke, remote_arg_t *pra)
696{
697 remote_arg_t *rpra = 0;
698 struct fastrpc_device *dev = 0;
699 struct smq_invoke_ctx *ctx = 0;
700 struct fastrpc_buf obuf, *abufs = 0, *b;
701 int interrupted = 0;
702 uint32_t sc;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700703 int i, nbufs = 0, err = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700704
705 sc = invoke->sc;
706 obuf.handle = 0;
707 if (REMOTE_SCALARS_LENGTH(sc)) {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700708 VERIFY(err, 0 == get_dev(me, &dev));
709 if (err)
710 goto bail;
711 VERIFY(err, 0 == get_page_list(kernel, sc, pra, &dev->buf,
712 &obuf));
713 if (err)
714 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700715 rpra = (remote_arg_t *)obuf.virt;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700716 VERIFY(err, 0 == get_args(kernel, sc, pra, rpra, invoke->pra,
717 &obuf, &abufs, &nbufs));
718 if (err)
719 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700720 }
721
722 context_list_alloc_ctx(&me->clst, &ctx);
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800723 VERIFY(err, 0 == fastrpc_invoke_send(me, kernel, invoke->handle, sc,
724 ctx, &obuf));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700725 if (err)
726 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700727 inv_args(sc, rpra, obuf.used);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700728 VERIFY(err, 0 == (interrupted =
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700729 wait_for_completion_interruptible(&ctx->work)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700730 if (err)
731 goto bail;
732 VERIFY(err, 0 == (err = ctx->retval));
733 if (err)
734 goto bail;
735 VERIFY(err, 0 == put_args(kernel, sc, pra, rpra, invoke->pra));
736 if (err)
737 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700738 bail:
739 if (interrupted) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700740 if (!kernel)
741 (void)fastrpc_release_current_dsp_process();
742 wait_for_completion(&ctx->work);
743 }
744 context_free(ctx);
Mitchel Humpherys6b4c68c2013-02-06 12:03:20 -0800745
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700746 for (i = 0, b = abufs; i < nbufs; ++i, ++b)
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700747 free_mem(b);
Mitchel Humpherys6b4c68c2013-02-06 12:03:20 -0800748
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700749 kfree(abufs);
750 if (dev) {
751 add_dev(me, dev);
752 if (obuf.handle != dev->buf.handle)
753 free_mem(&obuf);
754 }
755 return err;
756}
757
758static int fastrpc_create_current_dsp_process(void)
759{
760 int err = 0;
761 struct fastrpc_ioctl_invoke ioctl;
762 struct fastrpc_apps *me = &gfa;
763 remote_arg_t ra[1];
764 int tgid = 0;
765
766 tgid = current->tgid;
767 ra[0].buf.pv = &tgid;
768 ra[0].buf.len = sizeof(tgid);
769 ioctl.handle = 1;
770 ioctl.sc = REMOTE_SCALARS_MAKE(0, 1, 0);
771 ioctl.pra = ra;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800772 VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra)));
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700773 return err;
774}
775
776static int fastrpc_release_current_dsp_process(void)
777{
778 int err = 0;
779 struct fastrpc_apps *me = &gfa;
780 struct fastrpc_ioctl_invoke ioctl;
781 remote_arg_t ra[1];
782 int tgid = 0;
783
784 tgid = current->tgid;
785 ra[0].buf.pv = &tgid;
786 ra[0].buf.len = sizeof(tgid);
787 ioctl.handle = 1;
788 ioctl.sc = REMOTE_SCALARS_MAKE(1, 1, 0);
789 ioctl.pra = ra;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800790 VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra)));
791 return err;
792}
793
794static int fastrpc_mmap_on_dsp(struct fastrpc_apps *me,
795 struct fastrpc_ioctl_mmap *mmap,
796 struct smq_phy_page *pages,
797 int num)
798{
799 struct fastrpc_ioctl_invoke ioctl;
800 remote_arg_t ra[3];
801 int err = 0;
802 struct {
803 int pid;
804 uint32_t flags;
805 uint32_t vaddrin;
806 int num;
807 } inargs;
808
809 struct {
810 uint32_t vaddrout;
811 } routargs;
812 inargs.pid = current->tgid;
813 inargs.vaddrin = mmap->vaddrin;
814 inargs.flags = mmap->flags;
815 inargs.num = num;
816 ra[0].buf.pv = &inargs;
817 ra[0].buf.len = sizeof(inargs);
818
819 ra[1].buf.pv = pages;
820 ra[1].buf.len = num * sizeof(*pages);
821
822 ra[2].buf.pv = &routargs;
823 ra[2].buf.len = sizeof(routargs);
824
825 ioctl.handle = 1;
826 ioctl.sc = REMOTE_SCALARS_MAKE(2, 2, 1);
827 ioctl.pra = ra;
828 VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra)));
829 mmap->vaddrout = routargs.vaddrout;
830 if (err)
831 goto bail;
832bail:
833 return err;
834}
835
836static int fastrpc_munmap_on_dsp(struct fastrpc_apps *me,
837 struct fastrpc_ioctl_munmap *munmap)
838{
839 struct fastrpc_ioctl_invoke ioctl;
840 remote_arg_t ra[1];
841 int err = 0;
842 struct {
843 int pid;
844 uint32_t vaddrout;
845 int size;
846 } inargs;
847
848 inargs.pid = current->tgid;
849 inargs.size = munmap->size;
850 inargs.vaddrout = munmap->vaddrout;
851 ra[0].buf.pv = &inargs;
852 ra[0].buf.len = sizeof(inargs);
853
854 ioctl.handle = 1;
855 ioctl.sc = REMOTE_SCALARS_MAKE(3, 1, 0);
856 ioctl.pra = ra;
857 VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra)));
858 return err;
859}
860
861static int fastrpc_internal_munmap(struct fastrpc_apps *me,
862 struct file_data *fdata,
863 struct fastrpc_ioctl_munmap *munmap)
864{
865 int err = 0;
866 struct fastrpc_mmap *map = 0, *mapfree = 0;
867 struct hlist_node *pos, *n;
868 VERIFY(err, 0 == (err = fastrpc_munmap_on_dsp(me, munmap)));
869 if (err)
870 goto bail;
871 spin_lock(&fdata->hlock);
872 hlist_for_each_entry_safe(map, pos, n, &fdata->hlst, hn) {
873 if (map->vaddrout == munmap->vaddrout &&
874 map->size == munmap->size) {
875 hlist_del(&map->hn);
876 mapfree = map;
877 map = 0;
878 break;
879 }
880 }
881 spin_unlock(&fdata->hlock);
882bail:
883 if (mapfree) {
884 free_map(mapfree);
885 kfree(mapfree);
886 }
887 return err;
888}
889
890
891static int fastrpc_internal_mmap(struct fastrpc_apps *me,
892 struct file_data *fdata,
893 struct fastrpc_ioctl_mmap *mmap)
894{
895 struct ion_client *clnt = gfa.iclient;
896 struct fastrpc_mmap *map = 0;
897 struct smq_phy_page *pages = 0;
898 void *buf;
899 int len;
900 int num;
901 int err = 0;
902
903 VERIFY(err, 0 != (map = kzalloc(sizeof(*map), GFP_KERNEL)));
904 if (err)
905 goto bail;
906 map->handle = ion_import_dma_buf(clnt, mmap->fd);
907 VERIFY(err, 0 == IS_ERR_OR_NULL(map->handle));
908 if (err)
909 goto bail;
910 VERIFY(err, 0 != (map->virt = ion_map_kernel(clnt, map->handle)));
911 if (err)
912 goto bail;
913 buf = (void *)mmap->vaddrin;
914 len = mmap->size;
915 num = buf_num_pages(buf, len);
916 VERIFY(err, 0 != (pages = kzalloc(num * sizeof(*pages), GFP_KERNEL)));
917 if (err)
918 goto bail;
919 VERIFY(err, 0 < (num = buf_get_pages(buf, len, num, 1, pages, num)));
920 if (err)
921 goto bail;
922
923 VERIFY(err, 0 == fastrpc_mmap_on_dsp(me, mmap, pages, num));
924 if (err)
925 goto bail;
926 map->vaddrin = mmap->vaddrin;
927 map->vaddrout = mmap->vaddrout;
928 map->size = mmap->size;
929 INIT_HLIST_NODE(&map->hn);
930 spin_lock(&fdata->hlock);
931 hlist_add_head(&map->hn, &fdata->hlst);
932 spin_unlock(&fdata->hlock);
933 bail:
934 if (err && map) {
935 free_map(map);
936 kfree(map);
937 }
938 kfree(pages);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700939 return err;
940}
941
942static void cleanup_current_dev(void)
943{
944 struct fastrpc_apps *me = &gfa;
945 uint32_t h = hash_32(current->tgid, RPC_HASH_BITS);
946 struct hlist_head *head;
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800947 struct hlist_node *pos, *n;
948 struct fastrpc_device *dev, *devfree;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700949
950 rnext:
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800951 devfree = dev = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700952 spin_lock(&me->hlock);
953 head = &me->htbl[h];
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800954 hlist_for_each_entry_safe(dev, pos, n, head, hn) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700955 if (dev->tgid == current->tgid) {
956 hlist_del(&dev->hn);
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800957 devfree = dev;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700958 break;
959 }
960 }
961 spin_unlock(&me->hlock);
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800962 if (devfree) {
963 free_dev(devfree);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700964 goto rnext;
965 }
966 return;
967}
968
969static int fastrpc_device_release(struct inode *inode, struct file *file)
970{
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800971 struct file_data *fdata = (struct file_data *)file->private_data;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700972 (void)fastrpc_release_current_dsp_process();
973 cleanup_current_dev();
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800974 if (fdata) {
975 struct fastrpc_mmap *map;
976 struct hlist_node *n, *pos;
977 file->private_data = 0;
978 hlist_for_each_entry_safe(map, pos, n, &fdata->hlst, hn) {
979 hlist_del(&map->hn);
980 free_map(map);
981 kfree(map);
982 }
983 kfree(fdata);
984 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700985 return 0;
986}
987
988static int fastrpc_device_open(struct inode *inode, struct file *filp)
989{
990 int err = 0;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800991 filp->private_data = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700992 if (0 != try_module_get(THIS_MODULE)) {
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800993 struct file_data *fdata = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700994 /* This call will cause a dev to be created
995 * which will addref this module
996 */
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800997 VERIFY(err, 0 != (fdata = kzalloc(sizeof(*fdata), GFP_KERNEL)));
998 if (err)
999 goto bail;
1000
1001 spin_lock_init(&fdata->hlock);
1002 INIT_HLIST_HEAD(&fdata->hlst);
1003
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001004 VERIFY(err, 0 == fastrpc_create_current_dsp_process());
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001005 if (err)
Mitchel Humpherys0d99a792013-03-05 13:41:14 -08001006 goto bail;
1007 filp->private_data = fdata;
1008bail:
1009 if (err) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001010 cleanup_current_dev();
Mitchel Humpherys0d99a792013-03-05 13:41:14 -08001011 kfree(fdata);
1012 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001013 module_put(THIS_MODULE);
1014 }
1015 return err;
1016}
1017
1018
1019static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num,
1020 unsigned long ioctl_param)
1021{
1022 struct fastrpc_apps *me = &gfa;
1023 struct fastrpc_ioctl_invoke invoke;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -08001024 struct fastrpc_ioctl_mmap mmap;
1025 struct fastrpc_ioctl_munmap munmap;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001026 remote_arg_t *pra = 0;
1027 void *param = (char *)ioctl_param;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -08001028 struct file_data *fdata = (struct file_data *)file->private_data;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001029 int bufs, err = 0;
1030
1031 switch (ioctl_num) {
1032 case FASTRPC_IOCTL_INVOKE:
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001033 VERIFY(err, 0 == copy_from_user(&invoke, param,
1034 sizeof(invoke)));
1035 if (err)
1036 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001037 bufs = REMOTE_SCALARS_INBUFS(invoke.sc) +
1038 REMOTE_SCALARS_OUTBUFS(invoke.sc);
1039 if (bufs) {
1040 bufs = bufs * sizeof(*pra);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001041 VERIFY(err, 0 != (pra = kmalloc(bufs, GFP_KERNEL)));
1042 if (err)
1043 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001044 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001045 VERIFY(err, 0 == copy_from_user(pra, invoke.pra, bufs));
1046 if (err)
1047 goto bail;
1048 VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 0, &invoke,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001049 pra)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001050 if (err)
1051 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001052 break;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -08001053 case FASTRPC_IOCTL_MMAP:
1054 VERIFY(err, 0 == copy_from_user(&mmap, param,
1055 sizeof(mmap)));
1056 if (err)
1057 goto bail;
1058 VERIFY(err, 0 == (err = fastrpc_internal_mmap(me, fdata,
1059 &mmap)));
1060 if (err)
1061 goto bail;
1062 VERIFY(err, 0 == copy_to_user(param, &mmap, sizeof(mmap)));
1063 if (err)
1064 goto bail;
1065 break;
1066 case FASTRPC_IOCTL_MUNMAP:
1067 VERIFY(err, 0 == copy_from_user(&munmap, param,
1068 sizeof(munmap)));
1069 if (err)
1070 goto bail;
1071 VERIFY(err, 0 == (err = fastrpc_internal_munmap(me, fdata,
1072 &munmap)));
1073 if (err)
1074 goto bail;
1075 break;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001076 default:
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001077 err = -ENOTTY;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001078 break;
1079 }
1080 bail:
1081 kfree(pra);
1082 return err;
1083}
1084
1085static const struct file_operations fops = {
1086 .open = fastrpc_device_open,
1087 .release = fastrpc_device_release,
1088 .unlocked_ioctl = fastrpc_device_ioctl,
1089};
1090
1091static int __init fastrpc_device_init(void)
1092{
1093 struct fastrpc_apps *me = &gfa;
1094 int err = 0;
1095
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001096 VERIFY(err, 0 == fastrpc_init());
1097 if (err)
Matt Wagantall818e23f2013-04-22 20:36:52 -07001098 goto fastrpc_bail;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001099 VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, 1, DEVICE_NAME));
1100 if (err)
Matt Wagantall818e23f2013-04-22 20:36:52 -07001101 goto alloc_chrdev_bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001102 cdev_init(&me->cdev, &fops);
1103 me->cdev.owner = THIS_MODULE;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001104 VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0), 1));
1105 if (err)
Matt Wagantall818e23f2013-04-22 20:36:52 -07001106 goto cdev_init_bail;
Mitchel Humpherys55877652013-02-02 11:23:42 -08001107 me->class = class_create(THIS_MODULE, "chardrv");
1108 VERIFY(err, !IS_ERR(me->class));
1109 if (err)
Matt Wagantall818e23f2013-04-22 20:36:52 -07001110 goto class_create_bail;
Mitchel Humpherys55877652013-02-02 11:23:42 -08001111 me->dev = device_create(me->class, NULL, MKDEV(MAJOR(me->dev_no), 0),
1112 NULL, DEVICE_NAME);
1113 VERIFY(err, !IS_ERR(me->dev));
1114 if (err)
Matt Wagantall818e23f2013-04-22 20:36:52 -07001115 goto device_create_bail;
Mitchel Humpherys55877652013-02-02 11:23:42 -08001116 pr_info("'created /dev/%s c %d 0'\n", DEVICE_NAME, MAJOR(me->dev_no));
Matt Wagantall818e23f2013-04-22 20:36:52 -07001117
1118 return 0;
1119
1120device_create_bail:
1121 class_destroy(me->class);
1122class_create_bail:
1123 cdev_del(&me->cdev);
1124cdev_init_bail:
1125 unregister_chrdev_region(me->dev_no, 1);
1126alloc_chrdev_bail:
1127 fastrpc_deinit();
1128fastrpc_bail:
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001129 return err;
1130}
1131
1132static void __exit fastrpc_device_exit(void)
1133{
1134 struct fastrpc_apps *me = &gfa;
1135
1136 fastrpc_deinit();
Mitchel Humpherys55877652013-02-02 11:23:42 -08001137 device_destroy(me->class, MKDEV(MAJOR(me->dev_no), 0));
1138 class_destroy(me->class);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001139 cdev_del(&me->cdev);
1140 unregister_chrdev_region(me->dev_no, 1);
1141}
1142
1143module_init(fastrpc_device_init);
1144module_exit(fastrpc_device_exit);
1145
1146MODULE_LICENSE("GPL v2");