blob: 719a06f909a074914ee4e6ae8896ef8ed99bb80c [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 Humpherysef5bbbe2013-05-15 12:44:51 -070029#include <mach/iommu_domains.h>
Mitchel Humpherys42e806e2012-09-30 22:27:53 -070030#include <linux/scatterlist.h>
Mitchel Humpherys6b4c68c2013-02-06 12:03:20 -080031#include <linux/fs.h>
32#include <linux/uaccess.h>
33#include <linux/device.h>
Mitchel Humpherysef5bbbe2013-05-15 12:44:51 -070034#include <linux/of.h>
35#include <linux/iommu.h>
Mitchel Humpherys6b4c68c2013-02-06 12:03:20 -080036
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(&current->mm->mmap_sem);\
50 } while (0)
51
52#define UNLOCK_MMAP(kernel)\
53 do {\
54 if (!kernel)\
55 up_read(&current->mm->mmap_sem);\
56 } while (0)
57
58
59static inline uint32_t buf_page_start(void *buf)
60{
61 uint32_t start = (uint32_t) buf & PAGE_MASK;
62 return start;
63}
64
65static inline uint32_t buf_page_offset(void *buf)
66{
67 uint32_t offset = (uint32_t) buf & (PAGE_SIZE - 1);
68 return offset;
69}
70
71static 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
79static 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
85static 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 Humpherysef5bbbe2013-05-15 12:44:51 -070090 uint32_t end = buf_page_start((void *)((uint32_t)addr + sz - 1));
Mitchel Humpherys6b4c68c2013-02-06 12:03:20 -080091 uint32_t len = nr_pages << PAGE_SHIFT;
Mitchel Humpherysef5bbbe2013-05-15 12:44:51 -070092 unsigned long pfn, pfnend;
Mitchel Humpherys6b4c68c2013-02-06 12:03:20 -080093 int n = -1, err = 0;
94
95 VERIFY(err, 0 != access_ok(access ? VERIFY_WRITE : VERIFY_READ,
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -080096 (void __user *)start, len));
Mitchel Humpherys6b4c68c2013-02-06 12:03:20 -080097 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 Humpherysef5bbbe2013-05-15 12:44:51 -0700109 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 Humpherys6b4c68c2013-02-06 12:03:20 -0800115 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 Humpherys79d361e2012-08-29 16:20:15 -0700124
125struct smq_invoke_ctx {
126 struct completion work;
127 int retval;
128 atomic_t free;
129};
130
131struct smq_context_list {
132 struct smq_invoke_ctx *ls;
133 int size;
134 int last;
135};
136
Mitchel Humpherysef5bbbe2013-05-15 12:44:51 -0700137struct fastrpc_smmu {
138 struct iommu_group *group;
139 struct iommu_domain *domain;
140 int domain_id;
141 bool enabled;
142};
143
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700144struct 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 Humpherys55877652013-02-02 11:23:42 -0800150 struct class *class;
151 struct device *dev;
Mitchel Humpherysef5bbbe2013-05-15 12:44:51 -0700152 struct fastrpc_smmu smmu;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700153 dev_t dev_no;
154 spinlock_t wrlock;
155 spinlock_t hlock;
156 struct hlist_head htbl[RPC_HASH_SZ];
157};
158
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800159struct 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 Humpherys79d361e2012-08-29 16:20:15 -0700168struct 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 Humpherys0d99a792013-03-05 13:41:14 -0800176struct file_data {
177 spinlock_t hlock;
178 struct hlist_head hlst;
179};
180
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700181struct fastrpc_device {
182 uint32_t tgid;
183 struct hlist_node hn;
184 struct fastrpc_buf buf;
185};
186
187static struct fastrpc_apps gfa;
188
189static void free_mem(struct fastrpc_buf *buf)
190{
191 struct fastrpc_apps *me = &gfa;
192
Mitchel Humpherysef5bbbe2013-05-15 12:44:51 -0700193 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 Humpherys79d361e2012-08-29 16:20:15 -0700199 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 Humpherys0d99a792013-03-05 13:41:14 -0800208static void free_map(struct fastrpc_mmap *map)
209{
210 struct fastrpc_apps *me = &gfa;
Mitchel Humpherysef5bbbe2013-05-15 12:44:51 -0700211 if (!IS_ERR_OR_NULL(map->handle)) {
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800212 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 Humpherys79d361e2012-08-29 16:20:15 -0700221static int alloc_mem(struct fastrpc_buf *buf)
222{
Mitchel Humpherysef5bbbe2013-05-15 12:44:51 -0700223 struct fastrpc_apps *me = &gfa;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700224 struct ion_client *clnt = gfa.iclient;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700225 struct sg_table *sg;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700226 int err = 0;
Mitchel Humpherysef5bbbe2013-05-15 12:44:51 -0700227 unsigned int heap;
228 unsigned long len;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800229 buf->handle = 0;
230 buf->virt = 0;
Mitchel Humpherysef5bbbe2013-05-15 12:44:51 -0700231 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 Humpherys42e806e2012-09-30 22:27:53 -0700234 VERIFY(err, 0 == IS_ERR_OR_NULL(buf->handle));
235 if (err)
236 goto bail;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700237 VERIFY(err, 0 != (buf->virt = ion_map_kernel(clnt, buf->handle)));
238 if (err)
239 goto bail;
Mitchel Humpherysef5bbbe2013-05-15 12:44:51 -0700240 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 Humpherys79d361e2012-08-29 16:20:15 -0700256 bail:
257 if (err && !IS_ERR_OR_NULL(buf->handle))
258 free_mem(buf);
259 return err;
260}
261
262static int context_list_ctor(struct smq_context_list *me, int size)
263{
264 int err = 0;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700265 VERIFY(err, 0 != (me->ls = kzalloc(size, GFP_KERNEL)));
266 if (err)
267 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700268 me->size = size / sizeof(*me->ls);
269 me->last = 0;
270 bail:
271 return err;
272}
273
274static void context_list_dtor(struct smq_context_list *me)
275{
276 kfree(me->ls);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700277}
278
279static void context_list_alloc_ctx(struct smq_context_list *me,
280 struct smq_invoke_ctx **po)
281{
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700282 int i = me->last;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700283 struct smq_invoke_ctx *ctx;
284
285 for (;;) {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700286 i = i % me->size;
287 ctx = &me->ls[i];
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700288 if (atomic_read(&ctx->free) == 0)
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700289 if (atomic_cmpxchg(&ctx->free, 0, 1) == 0)
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700290 break;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700291 i++;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700292 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700293 me->last = i;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700294 ctx->retval = -1;
295 init_completion(&ctx->work);
296 *po = ctx;
297}
298
299static void context_free(struct smq_invoke_ctx *me)
300{
301 if (me)
302 atomic_set(&me->free, 0);
303}
304
305static void context_notify_user(struct smq_invoke_ctx *me, int retval)
306{
307 me->retval = retval;
308 complete(&me->work);
309}
310
311static void context_notify_all_users(struct smq_context_list *me)
312{
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700313 int i;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700314
315 if (!me->ls)
316 return;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700317 for (i = 0; i < me->size; ++i) {
318 if (atomic_read(&me->ls[i].free) != 0)
319 complete(&me->ls[i].work);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700320 }
321}
322
323static 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 Humpherys42e806e2012-09-30 22:27:53 -0700328 int i, rlen, err = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700329 int inbufs = REMOTE_SCALARS_INBUFS(sc);
330 int outbufs = REMOTE_SCALARS_OUTBUFS(sc);
331
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700332 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 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 pgstart->addr = obuf->phys;
348 pgstart->size = obuf->size;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700349 for (i = 0; i < inbufs + outbufs; ++i) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700350 void *buf;
351 int len, num;
352
Mitchel Humpherysf581c512012-10-19 11:29:36 -0700353 list[i].num = 0;
354 list[i].pgidx = 0;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700355 len = pra[i].buf.len;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700356 if (!len)
357 continue;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700358 buf = pra[i].buf.pv;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700359 num = buf_num_pages(buf, len);
360 if (!kernel)
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700361 list[i].num = buf_get_pages(buf, len, num,
362 i >= inbufs, pages, rlen / sizeof(*pages));
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700363 else
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700364 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 Humpherys79d361e2012-08-29 16:20:15 -0700371 } else if (rlen > sizeof(*pages)) {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700372 list[i].pgidx = pages - pgstart;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700373 pages = pages + 1;
374 } else {
375 if (obuf->handle != ibuf->handle)
376 free_mem(obuf);
377 obuf->size += buf_page_size(sizeof(*pages));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700378 VERIFY(err, 0 == alloc_mem(obuf));
379 if (err)
380 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700381 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 Humpherys79d361e2012-08-29 16:20:15 -0700390 return err;
391}
392
393static 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 Humpherys42e806e2012-09-30 22:27:53 -0700402 int i, rlen, size, used, inh, bufs = 0, err = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700403 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 Humpherys42e806e2012-09-30 22:27:53 -0700408 used = ALIGN(pbuf->used, BALIGN);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700409 args = (void *)((char *)pbuf->virt + used);
410 rlen = pbuf->size - used;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700411 for (i = 0; i < inbufs + outbufs; ++i) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700412
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700413 rpra[i].buf.len = pra[i].buf.len;
Mitchel Humpherysf581c512012-10-19 11:29:36 -0700414 if (!rpra[i].buf.len)
415 continue;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700416 if (list[i].num) {
417 rpra[i].buf.pv = pra[i].buf.pv;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700418 continue;
419 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700420 if (rlen < pra[i].buf.len) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700421 struct fastrpc_buf *b;
422 pbuf->used = pbuf->size - rlen;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700423 VERIFY(err, 0 != (b = krealloc(obufs,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700424 (bufs + 1) * sizeof(*obufs), GFP_KERNEL)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700425 if (err)
426 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700427 obufs = b;
428 pbuf = obufs + bufs;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700429 pbuf->size = buf_num_pages(0, pra[i].buf.len) *
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700430 PAGE_SIZE;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700431 VERIFY(err, 0 == alloc_mem(pbuf));
432 if (err)
433 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700434 bufs++;
435 args = pbuf->virt;
436 rlen = pbuf->size;
437 }
Mitchel Humpherys0bc3aa52013-02-02 11:31:15 -0800438 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 Humpherys42e806e2012-09-30 22:27:53 -0700444 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 Humpherys79d361e2012-08-29 16:20:15 -0700453 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700454 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 Humpherys79d361e2012-08-29 16:20:15 -0700457 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700458 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 Humpherys79d361e2012-08-29 16:20:15 -0700462 }
463 pbuf->used = pbuf->size - rlen;
464 size = sizeof(*rpra) * REMOTE_SCALARS_INHANDLES(sc);
465 if (size) {
466 inh = inbufs + outbufs;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700467 if (!kernel) {
468 VERIFY(err, 0 == copy_from_user(&rpra[inh], &upra[inh],
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700469 size));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700470 if (err)
471 goto bail;
472 } else {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700473 memmove(&rpra[inh], &upra[inh], size);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700474 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700475 }
476 dmac_flush_range(rpra, (char *)rpra + used);
477 bail:
478 *abufs = obufs;
479 *nbufs = bufs;
480 return err;
481}
482
483static int put_args(uint32_t kernel, uint32_t sc, remote_arg_t *pra,
484 remote_arg_t *rpra, remote_arg_t *upra)
485{
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700486 int i, inbufs, outbufs, outh, size;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700487 int err = 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 (rpra[i].buf.pv != pra[i].buf.pv) {
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800493 if (!kernel) {
494 VERIFY(err, 0 == copy_to_user(pra[i].buf.pv,
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700495 rpra[i].buf.pv, rpra[i].buf.len));
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800496 if (err)
497 goto bail;
498 } else {
499 memmove(pra[i].buf.pv, rpra[i].buf.pv,
500 rpra[i].buf.len);
501 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700502 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700503 }
504 size = sizeof(*rpra) * REMOTE_SCALARS_OUTHANDLES(sc);
505 if (size) {
506 outh = inbufs + outbufs + REMOTE_SCALARS_INHANDLES(sc);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700507 if (!kernel) {
508 VERIFY(err, 0 == copy_to_user(&upra[outh], &rpra[outh],
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700509 size));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700510 if (err)
511 goto bail;
512 } else {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700513 memmove(&upra[outh], &rpra[outh], size);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700514 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700515 }
516 bail:
517 return err;
518}
519
520static void inv_args(uint32_t sc, remote_arg_t *rpra, int used)
521{
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700522 int i, inbufs, outbufs;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700523 int inv = 0;
524
525 inbufs = REMOTE_SCALARS_INBUFS(sc);
526 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700527 for (i = inbufs; i < inbufs + outbufs; ++i) {
528 if (buf_page_start(rpra) == buf_page_start(rpra[i].buf.pv))
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700529 inv = 1;
Mitchel Humpherysf581c512012-10-19 11:29:36 -0700530 else if (rpra[i].buf.len)
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700531 dmac_inv_range(rpra[i].buf.pv,
532 (char *)rpra[i].buf.pv + rpra[i].buf.len);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700533 }
534
535 if (inv || REMOTE_SCALARS_OUTHANDLES(sc))
536 dmac_inv_range(rpra, (char *)rpra + used);
537}
538
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800539static int fastrpc_invoke_send(struct fastrpc_apps *me,
540 uint32_t kernel, uint32_t handle,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700541 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 Humpherys79d361e2012-08-29 16:20:15 -0700546 msg.pid = current->tgid;
547 msg.tid = current->pid;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800548 if (kernel)
549 msg.pid = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700550 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 Humpherys42e806e2012-09-30 22:27:53 -0700558 VERIFY(err, len == sizeof(msg));
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700559 return err;
560}
561
562static void fastrpc_deinit(void)
563{
564 struct fastrpc_apps *me = &gfa;
565
Matt Wagantall818e23f2013-04-22 20:36:52 -0700566 smd_close(me->chan);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700567 context_list_dtor(&me->clst);
Matt Wagantall818e23f2013-04-22 20:36:52 -0700568 ion_client_destroy(me->iclient);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700569 me->iclient = 0;
570 me->chan = 0;
571}
572
573static 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 Humpherys42e806e2012-09-30 22:27:53 -0700580 VERIFY(err, sizeof(rsp) ==
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700581 smd_read_from_cb(me->chan, &rsp, sizeof(rsp)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700582 if (err)
583 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700584 context_notify_user(rsp.ctx, rsp.retval);
585 } while (!err);
586 bail:
587 return;
588}
589
590static 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
607static int fastrpc_init(void)
608{
609 int err = 0;
610 struct fastrpc_apps *me = &gfa;
Mitchel Humpherysef5bbbe2013-05-15 12:44:51 -0700611 struct device_node *node;
612 bool enabled = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700613
614 if (me->chan == 0) {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700615 int i;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700616 spin_lock_init(&me->hlock);
617 spin_lock_init(&me->wrlock);
618 init_completion(&me->work);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700619 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 Wagantall818e23f2013-04-22 20:36:52 -0700623 goto context_list_bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700624 me->iclient = msm_ion_client_create(ION_HEAP_CARVEOUT_MASK,
625 DEVICE_NAME);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700626 VERIFY(err, 0 == IS_ERR_OR_NULL(me->iclient));
627 if (err)
Matt Wagantall818e23f2013-04-22 20:36:52 -0700628 goto ion_bail;
Mitchel Humpherysef5bbbe2013-05-15 12:44:51 -0700629 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 Humpherys42e806e2012-09-30 22:27:53 -0700645 VERIFY(err, 0 == smd_named_open_on_edge(FASTRPC_SMD_GUID,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700646 SMD_APPS_QDSP, &me->chan,
647 me, smd_event_handler));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700648 if (err)
Matt Wagantall818e23f2013-04-22 20:36:52 -0700649 goto smd_bail;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700650 VERIFY(err, 0 != wait_for_completion_timeout(&me->work,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700651 RPC_TIMEOUT));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700652 if (err)
Matt Wagantall818e23f2013-04-22 20:36:52 -0700653 goto completion_bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700654 }
Matt Wagantall818e23f2013-04-22 20:36:52 -0700655
656 return 0;
657
658completion_bail:
659 smd_close(me->chan);
660smd_bail:
661 ion_client_destroy(me->iclient);
662ion_bail:
663 context_list_dtor(&me->clst);
664context_list_bail:
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700665 return err;
666}
667
668static void free_dev(struct fastrpc_device *dev)
669{
670 if (dev) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700671 free_mem(&dev->buf);
672 kfree(dev);
Mitchel Humpherys6b4c68c2013-02-06 12:03:20 -0800673 module_put(THIS_MODULE);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700674 }
675}
676
677static int alloc_dev(struct fastrpc_device **dev)
678{
679 int err = 0;
680 struct fastrpc_device *fd = 0;
681
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700682 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 Humpherys0d99a792013-03-05 13:41:14 -0800688
689 INIT_HLIST_NODE(&fd->hn);
690
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700691 fd->buf.size = PAGE_SIZE;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700692 VERIFY(err, 0 == alloc_mem(&fd->buf));
693 if (err)
694 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700695 fd->tgid = current->tgid;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800696
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700697 *dev = fd;
698 bail:
699 if (err)
700 free_dev(fd);
701 return err;
702}
703
704static int get_dev(struct fastrpc_apps *me, struct fastrpc_device **rdev)
705{
706 struct hlist_head *head;
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800707 struct fastrpc_device *dev = 0, *devfree = 0;
708 struct hlist_node *pos, *n;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700709 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 Humpherys8388b3b2013-03-04 18:07:45 -0800714 hlist_for_each_entry_safe(dev, pos, n, head, hn) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700715 if (dev->tgid == current->tgid) {
716 hlist_del(&dev->hn);
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800717 devfree = dev;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700718 break;
719 }
720 }
721 spin_unlock(&me->hlock);
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800722 VERIFY(err, devfree != 0);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700723 if (err)
724 goto bail;
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800725 *rdev = devfree;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700726 bail:
727 if (err) {
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -0800728 free_dev(devfree);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700729 err = alloc_dev(rdev);
730 }
731 return err;
732}
733
734static 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
746static int fastrpc_release_current_dsp_process(void);
747
748static 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 Humpherys42e806e2012-09-30 22:27:53 -0700757 int i, nbufs = 0, err = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700758
759 sc = invoke->sc;
760 obuf.handle = 0;
Mitchel Humpherysef5bbbe2013-05-15 12:44:51 -0700761 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 Humpherys79d361e2012-08-29 16:20:15 -0700767 if (REMOTE_SCALARS_LENGTH(sc)) {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700768 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 Humpherys79d361e2012-08-29 16:20:15 -0700775 rpra = (remote_arg_t *)obuf.virt;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700776 VERIFY(err, 0 == get_args(kernel, sc, pra, rpra, invoke->pra,
777 &obuf, &abufs, &nbufs));
778 if (err)
779 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700780 }
781
782 context_list_alloc_ctx(&me->clst, &ctx);
Mitchel Humpherys0d99a792013-03-05 13:41:14 -0800783 VERIFY(err, 0 == fastrpc_invoke_send(me, kernel, invoke->handle, sc,
784 ctx, &obuf));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700785 if (err)
786 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700787 inv_args(sc, rpra, obuf.used);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700788 VERIFY(err, 0 == (interrupted =
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700789 wait_for_completion_interruptible(&ctx->work)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700790 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 Humpherys79d361e2012-08-29 16:20:15 -0700798 bail:
799 if (interrupted) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700800 if (!kernel)
801 (void)fastrpc_release_current_dsp_process();
802 wait_for_completion(&ctx->work);
803 }
804 context_free(ctx);
Mitchel Humpherys6b4c68c2013-02-06 12:03:20 -0800805
Mitchel Humpherysef5bbbe2013-05-15 12:44:51 -0700806 if (me->smmu.enabled)
807 iommu_detach_group(me->smmu.domain, me->smmu.group);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700808 for (i = 0, b = abufs; i < nbufs; ++i, ++b)
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700809 free_mem(b);
Mitchel Humpherys6b4c68c2013-02-06 12:03:20 -0800810
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700811 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
820static 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 Humpherys0d99a792013-03-05 13:41:14 -0800834 VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra)));
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700835 return err;
836}
837
838static 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 Humpherys0d99a792013-03-05 13:41:14 -0800852 VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra)));
853 return err;
854}
855
856static 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;
894bail:
895 return err;
896}
897
898static 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
923static 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);
944bail:
945 if (mapfree) {
946 free_map(mapfree);
947 kfree(mapfree);
948 }
949 return err;
950}
951
952
953static 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 Humpherys79d361e2012-08-29 16:20:15 -07001001 return err;
1002}
1003
1004static 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 Humpherys8388b3b2013-03-04 18:07:45 -08001009 struct hlist_node *pos, *n;
1010 struct fastrpc_device *dev, *devfree;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001011
1012 rnext:
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -08001013 devfree = dev = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001014 spin_lock(&me->hlock);
1015 head = &me->htbl[h];
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -08001016 hlist_for_each_entry_safe(dev, pos, n, head, hn) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001017 if (dev->tgid == current->tgid) {
1018 hlist_del(&dev->hn);
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -08001019 devfree = dev;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001020 break;
1021 }
1022 }
1023 spin_unlock(&me->hlock);
Mitchel Humpherys8388b3b2013-03-04 18:07:45 -08001024 if (devfree) {
1025 free_dev(devfree);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001026 goto rnext;
1027 }
1028 return;
1029}
1030
1031static int fastrpc_device_release(struct inode *inode, struct file *file)
1032{
Mitchel Humpherys0d99a792013-03-05 13:41:14 -08001033 struct file_data *fdata = (struct file_data *)file->private_data;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001034 (void)fastrpc_release_current_dsp_process();
1035 cleanup_current_dev();
Mitchel Humpherys0d99a792013-03-05 13:41:14 -08001036 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 Humpherys79d361e2012-08-29 16:20:15 -07001047 return 0;
1048}
1049
1050static int fastrpc_device_open(struct inode *inode, struct file *filp)
1051{
1052 int err = 0;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -08001053 filp->private_data = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001054 if (0 != try_module_get(THIS_MODULE)) {
Mitchel Humpherys0d99a792013-03-05 13:41:14 -08001055 struct file_data *fdata = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001056 /* This call will cause a dev to be created
1057 * which will addref this module
1058 */
Mitchel Humpherys0d99a792013-03-05 13:41:14 -08001059 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 Humpherys42e806e2012-09-30 22:27:53 -07001066 VERIFY(err, 0 == fastrpc_create_current_dsp_process());
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001067 if (err)
Mitchel Humpherys0d99a792013-03-05 13:41:14 -08001068 goto bail;
1069 filp->private_data = fdata;
1070bail:
1071 if (err) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001072 cleanup_current_dev();
Mitchel Humpherys0d99a792013-03-05 13:41:14 -08001073 kfree(fdata);
1074 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001075 module_put(THIS_MODULE);
1076 }
1077 return err;
1078}
1079
1080
1081static 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 Humpherys0d99a792013-03-05 13:41:14 -08001086 struct fastrpc_ioctl_mmap mmap;
1087 struct fastrpc_ioctl_munmap munmap;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001088 remote_arg_t *pra = 0;
1089 void *param = (char *)ioctl_param;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -08001090 struct file_data *fdata = (struct file_data *)file->private_data;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001091 int bufs, err = 0;
1092
1093 switch (ioctl_num) {
1094 case FASTRPC_IOCTL_INVOKE:
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001095 VERIFY(err, 0 == copy_from_user(&invoke, param,
1096 sizeof(invoke)));
1097 if (err)
1098 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001099 bufs = REMOTE_SCALARS_INBUFS(invoke.sc) +
1100 REMOTE_SCALARS_OUTBUFS(invoke.sc);
1101 if (bufs) {
1102 bufs = bufs * sizeof(*pra);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001103 VERIFY(err, 0 != (pra = kmalloc(bufs, GFP_KERNEL)));
1104 if (err)
1105 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001106 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001107 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 Humpherys79d361e2012-08-29 16:20:15 -07001111 pra)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001112 if (err)
1113 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001114 break;
Mitchel Humpherys0d99a792013-03-05 13:41:14 -08001115 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 Humpherys79d361e2012-08-29 16:20:15 -07001138 default:
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001139 err = -ENOTTY;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001140 break;
1141 }
1142 bail:
1143 kfree(pra);
1144 return err;
1145}
1146
1147static const struct file_operations fops = {
1148 .open = fastrpc_device_open,
1149 .release = fastrpc_device_release,
1150 .unlocked_ioctl = fastrpc_device_ioctl,
1151};
1152
1153static int __init fastrpc_device_init(void)
1154{
1155 struct fastrpc_apps *me = &gfa;
1156 int err = 0;
1157
Mitchel Humpherysef5bbbe2013-05-15 12:44:51 -07001158 memset(me, 0, sizeof(*me));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001159 VERIFY(err, 0 == fastrpc_init());
1160 if (err)
Matt Wagantall818e23f2013-04-22 20:36:52 -07001161 goto fastrpc_bail;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001162 VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, 1, DEVICE_NAME));
1163 if (err)
Matt Wagantall818e23f2013-04-22 20:36:52 -07001164 goto alloc_chrdev_bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001165 cdev_init(&me->cdev, &fops);
1166 me->cdev.owner = THIS_MODULE;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -07001167 VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0), 1));
1168 if (err)
Matt Wagantall818e23f2013-04-22 20:36:52 -07001169 goto cdev_init_bail;
Mitchel Humpherys55877652013-02-02 11:23:42 -08001170 me->class = class_create(THIS_MODULE, "chardrv");
1171 VERIFY(err, !IS_ERR(me->class));
1172 if (err)
Matt Wagantall818e23f2013-04-22 20:36:52 -07001173 goto class_create_bail;
Mitchel Humpherys55877652013-02-02 11:23:42 -08001174 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 Wagantall818e23f2013-04-22 20:36:52 -07001178 goto device_create_bail;
Mitchel Humpherys55877652013-02-02 11:23:42 -08001179 pr_info("'created /dev/%s c %d 0'\n", DEVICE_NAME, MAJOR(me->dev_no));
Matt Wagantall818e23f2013-04-22 20:36:52 -07001180
1181 return 0;
1182
1183device_create_bail:
1184 class_destroy(me->class);
1185class_create_bail:
1186 cdev_del(&me->cdev);
1187cdev_init_bail:
1188 unregister_chrdev_region(me->dev_no, 1);
1189alloc_chrdev_bail:
1190 fastrpc_deinit();
1191fastrpc_bail:
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001192 return err;
1193}
1194
1195static void __exit fastrpc_device_exit(void)
1196{
1197 struct fastrpc_apps *me = &gfa;
1198
1199 fastrpc_deinit();
Mitchel Humpherys55877652013-02-02 11:23:42 -08001200 device_destroy(me->class, MKDEV(MAJOR(me->dev_no), 0));
1201 class_destroy(me->class);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001202 cdev_del(&me->cdev);
1203 unregister_chrdev_region(me->dev_no, 1);
1204}
1205
1206module_init(fastrpc_device_init);
1207module_exit(fastrpc_device_exit);
1208
1209MODULE_LICENSE("GPL v2");