blob: ea317698a85426b2f40dd2ec081890627b4da18c [file] [log] [blame]
Mitchel Humpherys79d361e2012-08-29 16:20:15 -07001/*
2 * Copyright (c) 2012, The Linux Foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
Mitchel Humpherys42e806e2012-09-30 22:27:53 -070014#include <linux/scatterlist.h>
Mitchel Humpherys79d361e2012-08-29 16:20:15 -070015#include "adsprpc.h"
16
17struct smq_invoke_ctx {
18 struct completion work;
19 int retval;
20 atomic_t free;
21};
22
23struct smq_context_list {
24 struct smq_invoke_ctx *ls;
25 int size;
26 int last;
27};
28
29struct fastrpc_apps {
30 smd_channel_t *chan;
31 struct smq_context_list clst;
32 struct completion work;
33 struct ion_client *iclient;
34 struct cdev cdev;
35 dev_t dev_no;
36 spinlock_t wrlock;
37 spinlock_t hlock;
38 struct hlist_head htbl[RPC_HASH_SZ];
39};
40
41struct fastrpc_buf {
42 struct ion_handle *handle;
43 void *virt;
44 ion_phys_addr_t phys;
45 int size;
46 int used;
47};
48
49struct fastrpc_device {
50 uint32_t tgid;
51 struct hlist_node hn;
52 struct fastrpc_buf buf;
53};
54
55static struct fastrpc_apps gfa;
56
57static void free_mem(struct fastrpc_buf *buf)
58{
59 struct fastrpc_apps *me = &gfa;
60
61 if (buf->handle) {
62 if (buf->virt) {
63 ion_unmap_kernel(me->iclient, buf->handle);
64 buf->virt = 0;
65 }
66 ion_free(me->iclient, buf->handle);
67 buf->handle = 0;
68 }
69}
70
71static int alloc_mem(struct fastrpc_buf *buf)
72{
73 struct ion_client *clnt = gfa.iclient;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -070074 struct sg_table *sg;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -070075 int err = 0;
76
77 buf->handle = ion_alloc(clnt, buf->size, SZ_4K,
Hanumant Singh7d72bad2012-08-29 18:39:44 -070078 ION_HEAP(ION_AUDIO_HEAP_ID), 0);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -070079 VERIFY(err, 0 == IS_ERR_OR_NULL(buf->handle));
80 if (err)
81 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -070082 buf->virt = 0;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -070083 VERIFY(err, 0 != (buf->virt = ion_map_kernel(clnt, buf->handle)));
84 if (err)
85 goto bail;
86 VERIFY(err, 0 != (sg = ion_sg_table(clnt, buf->handle)));
87 if (err)
88 goto bail;
89 VERIFY(err, 1 == sg->nents);
90 if (err)
91 goto bail;
92 buf->phys = sg_dma_address(sg->sgl);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -070093 bail:
94 if (err && !IS_ERR_OR_NULL(buf->handle))
95 free_mem(buf);
96 return err;
97}
98
99static int context_list_ctor(struct smq_context_list *me, int size)
100{
101 int err = 0;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700102 VERIFY(err, 0 != (me->ls = kzalloc(size, GFP_KERNEL)));
103 if (err)
104 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700105 me->size = size / sizeof(*me->ls);
106 me->last = 0;
107 bail:
108 return err;
109}
110
111static void context_list_dtor(struct smq_context_list *me)
112{
113 kfree(me->ls);
114 me->ls = 0;
115}
116
117static void context_list_alloc_ctx(struct smq_context_list *me,
118 struct smq_invoke_ctx **po)
119{
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700120 int i = me->last;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700121 struct smq_invoke_ctx *ctx;
122
123 for (;;) {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700124 i = i % me->size;
125 ctx = &me->ls[i];
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700126 if (atomic_read(&ctx->free) == 0)
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700127 if (atomic_cmpxchg(&ctx->free, 0, 1) == 0)
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700128 break;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700129 i++;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700130 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700131 me->last = i;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700132 ctx->retval = -1;
133 init_completion(&ctx->work);
134 *po = ctx;
135}
136
137static void context_free(struct smq_invoke_ctx *me)
138{
139 if (me)
140 atomic_set(&me->free, 0);
141}
142
143static void context_notify_user(struct smq_invoke_ctx *me, int retval)
144{
145 me->retval = retval;
146 complete(&me->work);
147}
148
149static void context_notify_all_users(struct smq_context_list *me)
150{
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700151 int i;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700152
153 if (!me->ls)
154 return;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700155 for (i = 0; i < me->size; ++i) {
156 if (atomic_read(&me->ls[i].free) != 0)
157 complete(&me->ls[i].work);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700158 }
159}
160
161static int get_page_list(uint32_t kernel, uint32_t sc, remote_arg_t *pra,
162 struct fastrpc_buf *ibuf, struct fastrpc_buf *obuf)
163{
164 struct smq_phy_page *pgstart, *pages;
165 struct smq_invoke_buf *list;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700166 int i, rlen, err = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700167 int inbufs = REMOTE_SCALARS_INBUFS(sc);
168 int outbufs = REMOTE_SCALARS_OUTBUFS(sc);
169
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700170 LOCK_MMAP(kernel);
171 *obuf = *ibuf;
172 retry:
173 list = smq_invoke_buf_start((remote_arg_t *)obuf->virt, sc);
174 pgstart = smq_phy_page_start(sc, list);
175 pages = pgstart + 1;
176 rlen = obuf->size - ((uint32_t)pages - (uint32_t)obuf->virt);
177 if (rlen < 0) {
178 rlen = ((uint32_t)pages - (uint32_t)obuf->virt) - obuf->size;
179 obuf->size += buf_page_size(rlen);
180 obuf->handle = 0;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700181 VERIFY(err, 0 == alloc_mem(obuf));
182 if (err)
183 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700184 goto retry;
185 }
186 pgstart->addr = obuf->phys;
187 pgstart->size = obuf->size;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700188 for (i = 0; i < inbufs + outbufs; ++i) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700189 void *buf;
190 int len, num;
191
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700192 len = pra[i].buf.len;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700193 if (!len)
194 continue;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700195 buf = pra[i].buf.pv;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700196 num = buf_num_pages(buf, len);
197 if (!kernel)
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700198 list[i].num = buf_get_pages(buf, len, num,
199 i >= inbufs, pages, rlen / sizeof(*pages));
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700200 else
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700201 list[i].num = 0;
202 VERIFY(err, list[i].num >= 0);
203 if (err)
204 goto bail;
205 if (list[i].num) {
206 list[i].pgidx = pages - pgstart;
207 pages = pages + list[i].num;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700208 } else if (rlen > sizeof(*pages)) {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700209 list[i].pgidx = pages - pgstart;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700210 pages = pages + 1;
211 } else {
212 if (obuf->handle != ibuf->handle)
213 free_mem(obuf);
214 obuf->size += buf_page_size(sizeof(*pages));
215 obuf->handle = 0;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700216 VERIFY(err, 0 == alloc_mem(obuf));
217 if (err)
218 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700219 goto retry;
220 }
221 rlen = obuf->size - ((uint32_t) pages - (uint32_t) obuf->virt);
222 }
223 obuf->used = obuf->size - rlen;
224 bail:
225 if (err && (obuf->handle != ibuf->handle))
226 free_mem(obuf);
227 UNLOCK_MMAP(kernel);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700228 return err;
229}
230
231static int get_args(uint32_t kernel, uint32_t sc, remote_arg_t *pra,
232 remote_arg_t *rpra, remote_arg_t *upra,
233 struct fastrpc_buf *ibuf, struct fastrpc_buf **abufs,
234 int *nbufs)
235{
236 struct smq_invoke_buf *list;
237 struct fastrpc_buf *pbuf = ibuf, *obufs = 0;
238 struct smq_phy_page *pages;
239 void *args;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700240 int i, rlen, size, used, inh, bufs = 0, err = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700241 int inbufs = REMOTE_SCALARS_INBUFS(sc);
242 int outbufs = REMOTE_SCALARS_OUTBUFS(sc);
243
244 list = smq_invoke_buf_start(rpra, sc);
245 pages = smq_phy_page_start(sc, list);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700246 used = ALIGN(pbuf->used, BALIGN);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700247 args = (void *)((char *)pbuf->virt + used);
248 rlen = pbuf->size - used;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700249 for (i = 0; i < inbufs + outbufs; ++i) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700250 int num;
251
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700252 rpra[i].buf.len = pra[i].buf.len;
253 if (list[i].num) {
254 rpra[i].buf.pv = pra[i].buf.pv;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700255 continue;
256 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700257 if (rlen < pra[i].buf.len) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700258 struct fastrpc_buf *b;
259 pbuf->used = pbuf->size - rlen;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700260 VERIFY(err, 0 != (b = krealloc(obufs,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700261 (bufs + 1) * sizeof(*obufs), GFP_KERNEL)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700262 if (err)
263 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700264 obufs = b;
265 pbuf = obufs + bufs;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700266 pbuf->size = buf_num_pages(0, pra[i].buf.len) *
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700267 PAGE_SIZE;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700268 VERIFY(err, 0 == alloc_mem(pbuf));
269 if (err)
270 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700271 bufs++;
272 args = pbuf->virt;
273 rlen = pbuf->size;
274 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700275 num = buf_num_pages(args, pra[i].buf.len);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700276 if (pbuf == ibuf) {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700277 list[i].num = num;
278 list[i].pgidx = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700279 } else {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700280 list[i].num = 1;
281 pages[list[i].pgidx].addr =
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700282 buf_page_start((void *)(pbuf->phys +
283 (pbuf->size - rlen)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700284 pages[list[i].pgidx].size =
285 buf_page_size(pra[i].buf.len);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700286 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700287 if (i < inbufs) {
288 if (!kernel) {
289 VERIFY(err, 0 == copy_from_user(args,
290 pra[i].buf.pv, pra[i].buf.len));
291 if (err)
292 goto bail;
293 } else {
294 memmove(args, pra[i].buf.pv, pra[i].buf.len);
295 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700296 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700297 rpra[i].buf.pv = args;
298 args = (void *)((char *)args + ALIGN(pra[i].buf.len, BALIGN));
299 rlen -= ALIGN(pra[i].buf.len, BALIGN);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700300 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700301 for (i = 0; i < inbufs; ++i) {
302 if (rpra[i].buf.len)
303 dmac_flush_range(rpra[i].buf.pv,
304 (char *)rpra[i].buf.pv + rpra[i].buf.len);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700305 }
306 pbuf->used = pbuf->size - rlen;
307 size = sizeof(*rpra) * REMOTE_SCALARS_INHANDLES(sc);
308 if (size) {
309 inh = inbufs + outbufs;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700310 if (!kernel) {
311 VERIFY(err, 0 == copy_from_user(&rpra[inh], &upra[inh],
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700312 size));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700313 if (err)
314 goto bail;
315 } else {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700316 memmove(&rpra[inh], &upra[inh], size);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700317 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700318 }
319 dmac_flush_range(rpra, (char *)rpra + used);
320 bail:
321 *abufs = obufs;
322 *nbufs = bufs;
323 return err;
324}
325
326static int put_args(uint32_t kernel, uint32_t sc, remote_arg_t *pra,
327 remote_arg_t *rpra, remote_arg_t *upra)
328{
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700329 int i, inbufs, outbufs, outh, size;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700330 int err = 0;
331
332 inbufs = REMOTE_SCALARS_INBUFS(sc);
333 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700334 for (i = inbufs; i < inbufs + outbufs; ++i) {
335 if (rpra[i].buf.pv != pra[i].buf.pv) {
336 VERIFY(err, 0 == copy_to_user(pra[i].buf.pv,
337 rpra[i].buf.pv, rpra[i].buf.len));
338 if (err)
339 goto bail;
340 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700341 }
342 size = sizeof(*rpra) * REMOTE_SCALARS_OUTHANDLES(sc);
343 if (size) {
344 outh = inbufs + outbufs + REMOTE_SCALARS_INHANDLES(sc);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700345 if (!kernel) {
346 VERIFY(err, 0 == copy_to_user(&upra[outh], &rpra[outh],
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700347 size));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700348 if (err)
349 goto bail;
350 } else {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700351 memmove(&upra[outh], &rpra[outh], size);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700352 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700353 }
354 bail:
355 return err;
356}
357
358static void inv_args(uint32_t sc, remote_arg_t *rpra, int used)
359{
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700360 int i, inbufs, outbufs;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700361 int inv = 0;
362
363 inbufs = REMOTE_SCALARS_INBUFS(sc);
364 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700365 for (i = inbufs; i < inbufs + outbufs; ++i) {
366 if (buf_page_start(rpra) == buf_page_start(rpra[i].buf.pv))
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700367 inv = 1;
368 else
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700369 dmac_inv_range(rpra[i].buf.pv,
370 (char *)rpra[i].buf.pv + rpra[i].buf.len);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700371 }
372
373 if (inv || REMOTE_SCALARS_OUTHANDLES(sc))
374 dmac_inv_range(rpra, (char *)rpra + used);
375}
376
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700377static int fastrpc_invoke_send(struct fastrpc_apps *me, uint32_t handle,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700378 uint32_t sc, struct smq_invoke_ctx *ctx,
379 struct fastrpc_buf *buf)
380{
381 struct smq_msg msg;
382 int err = 0, len;
383
384 msg.pid = current->tgid;
385 msg.tid = current->pid;
386 msg.invoke.header.ctx = ctx;
387 msg.invoke.header.handle = handle;
388 msg.invoke.header.sc = sc;
389 msg.invoke.page.addr = buf->phys;
390 msg.invoke.page.size = buf_page_size(buf->used);
391 spin_lock(&me->wrlock);
392 len = smd_write(me->chan, &msg, sizeof(msg));
393 spin_unlock(&me->wrlock);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700394 VERIFY(err, len == sizeof(msg));
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700395 return err;
396}
397
398static void fastrpc_deinit(void)
399{
400 struct fastrpc_apps *me = &gfa;
401
402 if (me->chan)
403 (void)smd_close(me->chan);
404 context_list_dtor(&me->clst);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700405 if (me->iclient)
406 ion_client_destroy(me->iclient);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700407 me->iclient = 0;
408 me->chan = 0;
409}
410
411static void fastrpc_read_handler(void)
412{
413 struct fastrpc_apps *me = &gfa;
414 struct smq_invoke_rsp rsp;
415 int err = 0;
416
417 do {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700418 VERIFY(err, sizeof(rsp) ==
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700419 smd_read_from_cb(me->chan, &rsp, sizeof(rsp)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700420 if (err)
421 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700422 context_notify_user(rsp.ctx, rsp.retval);
423 } while (!err);
424 bail:
425 return;
426}
427
428static void smd_event_handler(void *priv, unsigned event)
429{
430 struct fastrpc_apps *me = (struct fastrpc_apps *)priv;
431
432 switch (event) {
433 case SMD_EVENT_OPEN:
434 complete(&(me->work));
435 break;
436 case SMD_EVENT_CLOSE:
437 context_notify_all_users(&me->clst);
438 break;
439 case SMD_EVENT_DATA:
440 fastrpc_read_handler();
441 break;
442 }
443}
444
445static int fastrpc_init(void)
446{
447 int err = 0;
448 struct fastrpc_apps *me = &gfa;
449
450 if (me->chan == 0) {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700451 int i;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700452 spin_lock_init(&me->hlock);
453 spin_lock_init(&me->wrlock);
454 init_completion(&me->work);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700455 for (i = 0; i < RPC_HASH_SZ; ++i)
456 INIT_HLIST_HEAD(&me->htbl[i]);
457 VERIFY(err, 0 == context_list_ctor(&me->clst, SZ_4K));
458 if (err)
459 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700460 me->iclient = msm_ion_client_create(ION_HEAP_CARVEOUT_MASK,
461 DEVICE_NAME);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700462 VERIFY(err, 0 == IS_ERR_OR_NULL(me->iclient));
463 if (err)
464 goto bail;
465 VERIFY(err, 0 == smd_named_open_on_edge(FASTRPC_SMD_GUID,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700466 SMD_APPS_QDSP, &me->chan,
467 me, smd_event_handler));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700468 if (err)
469 goto bail;
470 VERIFY(err, 0 != wait_for_completion_timeout(&me->work,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700471 RPC_TIMEOUT));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700472 if (err)
473 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700474 }
475 bail:
476 if (err)
477 fastrpc_deinit();
478 return err;
479}
480
481static void free_dev(struct fastrpc_device *dev)
482{
483 if (dev) {
484 module_put(THIS_MODULE);
485 free_mem(&dev->buf);
486 kfree(dev);
487 }
488}
489
490static int alloc_dev(struct fastrpc_device **dev)
491{
492 int err = 0;
493 struct fastrpc_device *fd = 0;
494
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700495 VERIFY(err, 0 != try_module_get(THIS_MODULE));
496 if (err)
497 goto bail;
498 VERIFY(err, 0 != (fd = kzalloc(sizeof(*fd), GFP_KERNEL)));
499 if (err)
500 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700501 fd->buf.size = PAGE_SIZE;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700502 VERIFY(err, 0 == alloc_mem(&fd->buf));
503 if (err)
504 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700505 fd->tgid = current->tgid;
506 INIT_HLIST_NODE(&fd->hn);
507 *dev = fd;
508 bail:
509 if (err)
510 free_dev(fd);
511 return err;
512}
513
514static int get_dev(struct fastrpc_apps *me, struct fastrpc_device **rdev)
515{
516 struct hlist_head *head;
517 struct fastrpc_device *dev = 0;
518 struct hlist_node *n;
519 uint32_t h = hash_32(current->tgid, RPC_HASH_BITS);
520 int err = 0;
521
522 spin_lock(&me->hlock);
523 head = &me->htbl[h];
524 hlist_for_each_entry(dev, n, head, hn) {
525 if (dev->tgid == current->tgid) {
526 hlist_del(&dev->hn);
527 break;
528 }
529 }
530 spin_unlock(&me->hlock);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700531 VERIFY(err, dev != 0);
532 if (err)
533 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700534 *rdev = dev;
535 bail:
536 if (err) {
537 free_dev(dev);
538 err = alloc_dev(rdev);
539 }
540 return err;
541}
542
543static void add_dev(struct fastrpc_apps *me, struct fastrpc_device *dev)
544{
545 struct hlist_head *head;
546 uint32_t h = hash_32(current->tgid, RPC_HASH_BITS);
547
548 spin_lock(&me->hlock);
549 head = &me->htbl[h];
550 hlist_add_head(&dev->hn, head);
551 spin_unlock(&me->hlock);
552 return;
553}
554
555static int fastrpc_release_current_dsp_process(void);
556
557static int fastrpc_internal_invoke(struct fastrpc_apps *me, uint32_t kernel,
558 struct fastrpc_ioctl_invoke *invoke, remote_arg_t *pra)
559{
560 remote_arg_t *rpra = 0;
561 struct fastrpc_device *dev = 0;
562 struct smq_invoke_ctx *ctx = 0;
563 struct fastrpc_buf obuf, *abufs = 0, *b;
564 int interrupted = 0;
565 uint32_t sc;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700566 int i, nbufs = 0, err = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700567
568 sc = invoke->sc;
569 obuf.handle = 0;
570 if (REMOTE_SCALARS_LENGTH(sc)) {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700571 VERIFY(err, 0 == get_dev(me, &dev));
572 if (err)
573 goto bail;
574 VERIFY(err, 0 == get_page_list(kernel, sc, pra, &dev->buf,
575 &obuf));
576 if (err)
577 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700578 rpra = (remote_arg_t *)obuf.virt;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700579 VERIFY(err, 0 == get_args(kernel, sc, pra, rpra, invoke->pra,
580 &obuf, &abufs, &nbufs));
581 if (err)
582 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700583 }
584
585 context_list_alloc_ctx(&me->clst, &ctx);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700586 VERIFY(err, 0 == fastrpc_invoke_send(me, invoke->handle, sc, ctx,
587 &obuf));
588 if (err)
589 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700590 inv_args(sc, rpra, obuf.used);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700591 VERIFY(err, 0 == (interrupted =
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700592 wait_for_completion_interruptible(&ctx->work)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700593 if (err)
594 goto bail;
595 VERIFY(err, 0 == (err = ctx->retval));
596 if (err)
597 goto bail;
598 VERIFY(err, 0 == put_args(kernel, sc, pra, rpra, invoke->pra));
599 if (err)
600 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700601 bail:
602 if (interrupted) {
603 init_completion(&ctx->work);
604 if (!kernel)
605 (void)fastrpc_release_current_dsp_process();
606 wait_for_completion(&ctx->work);
607 }
608 context_free(ctx);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700609 for (i = 0, b = abufs; i < nbufs; ++i, ++b)
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700610 free_mem(b);
611 kfree(abufs);
612 if (dev) {
613 add_dev(me, dev);
614 if (obuf.handle != dev->buf.handle)
615 free_mem(&obuf);
616 }
617 return err;
618}
619
620static int fastrpc_create_current_dsp_process(void)
621{
622 int err = 0;
623 struct fastrpc_ioctl_invoke ioctl;
624 struct fastrpc_apps *me = &gfa;
625 remote_arg_t ra[1];
626 int tgid = 0;
627
628 tgid = current->tgid;
629 ra[0].buf.pv = &tgid;
630 ra[0].buf.len = sizeof(tgid);
631 ioctl.handle = 1;
632 ioctl.sc = REMOTE_SCALARS_MAKE(0, 1, 0);
633 ioctl.pra = ra;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700634 VERIFY(err, 0 == fastrpc_internal_invoke(me, 1, &ioctl, ra));
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700635 return err;
636}
637
638static int fastrpc_release_current_dsp_process(void)
639{
640 int err = 0;
641 struct fastrpc_apps *me = &gfa;
642 struct fastrpc_ioctl_invoke ioctl;
643 remote_arg_t ra[1];
644 int tgid = 0;
645
646 tgid = current->tgid;
647 ra[0].buf.pv = &tgid;
648 ra[0].buf.len = sizeof(tgid);
649 ioctl.handle = 1;
650 ioctl.sc = REMOTE_SCALARS_MAKE(1, 1, 0);
651 ioctl.pra = ra;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700652 VERIFY(err, 0 == fastrpc_internal_invoke(me, 1, &ioctl, ra));
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700653 return err;
654}
655
656static void cleanup_current_dev(void)
657{
658 struct fastrpc_apps *me = &gfa;
659 uint32_t h = hash_32(current->tgid, RPC_HASH_BITS);
660 struct hlist_head *head;
661 struct hlist_node *pos;
662 struct fastrpc_device *dev;
663
664 rnext:
665 dev = 0;
666 spin_lock(&me->hlock);
667 head = &me->htbl[h];
668 hlist_for_each_entry(dev, pos, head, hn) {
669 if (dev->tgid == current->tgid) {
670 hlist_del(&dev->hn);
671 break;
672 }
673 }
674 spin_unlock(&me->hlock);
675 if (dev) {
676 free_dev(dev);
677 goto rnext;
678 }
679 return;
680}
681
682static int fastrpc_device_release(struct inode *inode, struct file *file)
683{
684 (void)fastrpc_release_current_dsp_process();
685 cleanup_current_dev();
686 return 0;
687}
688
689static int fastrpc_device_open(struct inode *inode, struct file *filp)
690{
691 int err = 0;
692
693 if (0 != try_module_get(THIS_MODULE)) {
694 /* This call will cause a dev to be created
695 * which will addref this module
696 */
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700697 VERIFY(err, 0 == fastrpc_create_current_dsp_process());
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700698 if (err)
699 cleanup_current_dev();
700 module_put(THIS_MODULE);
701 }
702 return err;
703}
704
705
706static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num,
707 unsigned long ioctl_param)
708{
709 struct fastrpc_apps *me = &gfa;
710 struct fastrpc_ioctl_invoke invoke;
711 remote_arg_t *pra = 0;
712 void *param = (char *)ioctl_param;
713 int bufs, err = 0;
714
715 switch (ioctl_num) {
716 case FASTRPC_IOCTL_INVOKE:
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700717 VERIFY(err, 0 == copy_from_user(&invoke, param,
718 sizeof(invoke)));
719 if (err)
720 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700721 bufs = REMOTE_SCALARS_INBUFS(invoke.sc) +
722 REMOTE_SCALARS_OUTBUFS(invoke.sc);
723 if (bufs) {
724 bufs = bufs * sizeof(*pra);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700725 VERIFY(err, 0 != (pra = kmalloc(bufs, GFP_KERNEL)));
726 if (err)
727 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700728 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700729 VERIFY(err, 0 == copy_from_user(pra, invoke.pra, bufs));
730 if (err)
731 goto bail;
732 VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 0, &invoke,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700733 pra)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700734 if (err)
735 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700736 break;
737 default:
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700738 err = -ENOTTY;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700739 break;
740 }
741 bail:
742 kfree(pra);
743 return err;
744}
745
746static const struct file_operations fops = {
747 .open = fastrpc_device_open,
748 .release = fastrpc_device_release,
749 .unlocked_ioctl = fastrpc_device_ioctl,
750};
751
752static int __init fastrpc_device_init(void)
753{
754 struct fastrpc_apps *me = &gfa;
755 int err = 0;
756
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700757 VERIFY(err, 0 == fastrpc_init());
758 if (err)
759 goto bail;
760 VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, 1, DEVICE_NAME));
761 if (err)
762 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700763 cdev_init(&me->cdev, &fops);
764 me->cdev.owner = THIS_MODULE;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700765 VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0), 1));
766 if (err)
767 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700768 pr_info("'mknod /dev/%s c %d 0'\n", DEVICE_NAME, MAJOR(me->dev_no));
769 bail:
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700770 if (err) {
771 if (me->dev_no)
772 unregister_chrdev_region(me->dev_no, 1);
773 fastrpc_deinit();
774 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700775 return err;
776}
777
778static void __exit fastrpc_device_exit(void)
779{
780 struct fastrpc_apps *me = &gfa;
781
782 fastrpc_deinit();
783 cdev_del(&me->cdev);
784 unregister_chrdev_region(me->dev_no, 1);
785}
786
787module_init(fastrpc_device_init);
788module_exit(fastrpc_device_exit);
789
790MODULE_LICENSE("GPL v2");