blob: 822da91618118366e8194ba13bcfd1accbdbe874 [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 Humpherysf581c512012-10-19 11:29:36 -0700192 list[i].num = 0;
193 list[i].pgidx = 0;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700194 len = pra[i].buf.len;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700195 if (!len)
196 continue;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700197 buf = pra[i].buf.pv;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700198 num = buf_num_pages(buf, len);
199 if (!kernel)
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700200 list[i].num = buf_get_pages(buf, len, num,
201 i >= inbufs, pages, rlen / sizeof(*pages));
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700202 else
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700203 list[i].num = 0;
204 VERIFY(err, list[i].num >= 0);
205 if (err)
206 goto bail;
207 if (list[i].num) {
208 list[i].pgidx = pages - pgstart;
209 pages = pages + list[i].num;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700210 } else if (rlen > sizeof(*pages)) {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700211 list[i].pgidx = pages - pgstart;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700212 pages = pages + 1;
213 } else {
214 if (obuf->handle != ibuf->handle)
215 free_mem(obuf);
216 obuf->size += buf_page_size(sizeof(*pages));
217 obuf->handle = 0;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700218 VERIFY(err, 0 == alloc_mem(obuf));
219 if (err)
220 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700221 goto retry;
222 }
223 rlen = obuf->size - ((uint32_t) pages - (uint32_t) obuf->virt);
224 }
225 obuf->used = obuf->size - rlen;
226 bail:
227 if (err && (obuf->handle != ibuf->handle))
228 free_mem(obuf);
229 UNLOCK_MMAP(kernel);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700230 return err;
231}
232
233static int get_args(uint32_t kernel, uint32_t sc, remote_arg_t *pra,
234 remote_arg_t *rpra, remote_arg_t *upra,
235 struct fastrpc_buf *ibuf, struct fastrpc_buf **abufs,
236 int *nbufs)
237{
238 struct smq_invoke_buf *list;
239 struct fastrpc_buf *pbuf = ibuf, *obufs = 0;
240 struct smq_phy_page *pages;
241 void *args;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700242 int i, rlen, size, used, inh, bufs = 0, err = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700243 int inbufs = REMOTE_SCALARS_INBUFS(sc);
244 int outbufs = REMOTE_SCALARS_OUTBUFS(sc);
245
246 list = smq_invoke_buf_start(rpra, sc);
247 pages = smq_phy_page_start(sc, list);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700248 used = ALIGN(pbuf->used, BALIGN);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700249 args = (void *)((char *)pbuf->virt + used);
250 rlen = pbuf->size - used;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700251 for (i = 0; i < inbufs + outbufs; ++i) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700252 int num;
253
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700254 rpra[i].buf.len = pra[i].buf.len;
Mitchel Humpherysf581c512012-10-19 11:29:36 -0700255 if (!rpra[i].buf.len)
256 continue;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700257 if (list[i].num) {
258 rpra[i].buf.pv = pra[i].buf.pv;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700259 continue;
260 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700261 if (rlen < pra[i].buf.len) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700262 struct fastrpc_buf *b;
263 pbuf->used = pbuf->size - rlen;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700264 VERIFY(err, 0 != (b = krealloc(obufs,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700265 (bufs + 1) * sizeof(*obufs), GFP_KERNEL)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700266 if (err)
267 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700268 obufs = b;
269 pbuf = obufs + bufs;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700270 pbuf->size = buf_num_pages(0, pra[i].buf.len) *
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700271 PAGE_SIZE;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700272 VERIFY(err, 0 == alloc_mem(pbuf));
273 if (err)
274 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700275 bufs++;
276 args = pbuf->virt;
277 rlen = pbuf->size;
278 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700279 num = buf_num_pages(args, pra[i].buf.len);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700280 if (pbuf == ibuf) {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700281 list[i].num = num;
282 list[i].pgidx = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700283 } else {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700284 list[i].num = 1;
285 pages[list[i].pgidx].addr =
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700286 buf_page_start((void *)(pbuf->phys +
287 (pbuf->size - rlen)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700288 pages[list[i].pgidx].size =
289 buf_page_size(pra[i].buf.len);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700290 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700291 if (i < inbufs) {
292 if (!kernel) {
293 VERIFY(err, 0 == copy_from_user(args,
294 pra[i].buf.pv, pra[i].buf.len));
295 if (err)
296 goto bail;
297 } else {
298 memmove(args, pra[i].buf.pv, pra[i].buf.len);
299 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700300 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700301 rpra[i].buf.pv = args;
302 args = (void *)((char *)args + ALIGN(pra[i].buf.len, BALIGN));
303 rlen -= ALIGN(pra[i].buf.len, BALIGN);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700304 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700305 for (i = 0; i < inbufs; ++i) {
306 if (rpra[i].buf.len)
307 dmac_flush_range(rpra[i].buf.pv,
308 (char *)rpra[i].buf.pv + rpra[i].buf.len);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700309 }
310 pbuf->used = pbuf->size - rlen;
311 size = sizeof(*rpra) * REMOTE_SCALARS_INHANDLES(sc);
312 if (size) {
313 inh = inbufs + outbufs;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700314 if (!kernel) {
315 VERIFY(err, 0 == copy_from_user(&rpra[inh], &upra[inh],
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700316 size));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700317 if (err)
318 goto bail;
319 } else {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700320 memmove(&rpra[inh], &upra[inh], size);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700321 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700322 }
323 dmac_flush_range(rpra, (char *)rpra + used);
324 bail:
325 *abufs = obufs;
326 *nbufs = bufs;
327 return err;
328}
329
330static int put_args(uint32_t kernel, uint32_t sc, remote_arg_t *pra,
331 remote_arg_t *rpra, remote_arg_t *upra)
332{
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700333 int i, inbufs, outbufs, outh, size;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700334 int err = 0;
335
336 inbufs = REMOTE_SCALARS_INBUFS(sc);
337 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700338 for (i = inbufs; i < inbufs + outbufs; ++i) {
339 if (rpra[i].buf.pv != pra[i].buf.pv) {
340 VERIFY(err, 0 == copy_to_user(pra[i].buf.pv,
341 rpra[i].buf.pv, rpra[i].buf.len));
342 if (err)
343 goto bail;
344 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700345 }
346 size = sizeof(*rpra) * REMOTE_SCALARS_OUTHANDLES(sc);
347 if (size) {
348 outh = inbufs + outbufs + REMOTE_SCALARS_INHANDLES(sc);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700349 if (!kernel) {
350 VERIFY(err, 0 == copy_to_user(&upra[outh], &rpra[outh],
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700351 size));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700352 if (err)
353 goto bail;
354 } else {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700355 memmove(&upra[outh], &rpra[outh], size);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700356 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700357 }
358 bail:
359 return err;
360}
361
362static void inv_args(uint32_t sc, remote_arg_t *rpra, int used)
363{
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700364 int i, inbufs, outbufs;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700365 int inv = 0;
366
367 inbufs = REMOTE_SCALARS_INBUFS(sc);
368 outbufs = REMOTE_SCALARS_OUTBUFS(sc);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700369 for (i = inbufs; i < inbufs + outbufs; ++i) {
370 if (buf_page_start(rpra) == buf_page_start(rpra[i].buf.pv))
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700371 inv = 1;
Mitchel Humpherysf581c512012-10-19 11:29:36 -0700372 else if (rpra[i].buf.len)
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700373 dmac_inv_range(rpra[i].buf.pv,
374 (char *)rpra[i].buf.pv + rpra[i].buf.len);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700375 }
376
377 if (inv || REMOTE_SCALARS_OUTHANDLES(sc))
378 dmac_inv_range(rpra, (char *)rpra + used);
379}
380
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700381static int fastrpc_invoke_send(struct fastrpc_apps *me, uint32_t handle,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700382 uint32_t sc, struct smq_invoke_ctx *ctx,
383 struct fastrpc_buf *buf)
384{
385 struct smq_msg msg;
386 int err = 0, len;
387
388 msg.pid = current->tgid;
389 msg.tid = current->pid;
390 msg.invoke.header.ctx = ctx;
391 msg.invoke.header.handle = handle;
392 msg.invoke.header.sc = sc;
393 msg.invoke.page.addr = buf->phys;
394 msg.invoke.page.size = buf_page_size(buf->used);
395 spin_lock(&me->wrlock);
396 len = smd_write(me->chan, &msg, sizeof(msg));
397 spin_unlock(&me->wrlock);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700398 VERIFY(err, len == sizeof(msg));
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700399 return err;
400}
401
402static void fastrpc_deinit(void)
403{
404 struct fastrpc_apps *me = &gfa;
405
406 if (me->chan)
407 (void)smd_close(me->chan);
408 context_list_dtor(&me->clst);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700409 if (me->iclient)
410 ion_client_destroy(me->iclient);
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700411 me->iclient = 0;
412 me->chan = 0;
413}
414
415static void fastrpc_read_handler(void)
416{
417 struct fastrpc_apps *me = &gfa;
418 struct smq_invoke_rsp rsp;
419 int err = 0;
420
421 do {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700422 VERIFY(err, sizeof(rsp) ==
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700423 smd_read_from_cb(me->chan, &rsp, sizeof(rsp)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700424 if (err)
425 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700426 context_notify_user(rsp.ctx, rsp.retval);
427 } while (!err);
428 bail:
429 return;
430}
431
432static void smd_event_handler(void *priv, unsigned event)
433{
434 struct fastrpc_apps *me = (struct fastrpc_apps *)priv;
435
436 switch (event) {
437 case SMD_EVENT_OPEN:
438 complete(&(me->work));
439 break;
440 case SMD_EVENT_CLOSE:
441 context_notify_all_users(&me->clst);
442 break;
443 case SMD_EVENT_DATA:
444 fastrpc_read_handler();
445 break;
446 }
447}
448
449static int fastrpc_init(void)
450{
451 int err = 0;
452 struct fastrpc_apps *me = &gfa;
453
454 if (me->chan == 0) {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700455 int i;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700456 spin_lock_init(&me->hlock);
457 spin_lock_init(&me->wrlock);
458 init_completion(&me->work);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700459 for (i = 0; i < RPC_HASH_SZ; ++i)
460 INIT_HLIST_HEAD(&me->htbl[i]);
461 VERIFY(err, 0 == context_list_ctor(&me->clst, SZ_4K));
462 if (err)
463 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700464 me->iclient = msm_ion_client_create(ION_HEAP_CARVEOUT_MASK,
465 DEVICE_NAME);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700466 VERIFY(err, 0 == IS_ERR_OR_NULL(me->iclient));
467 if (err)
468 goto bail;
469 VERIFY(err, 0 == smd_named_open_on_edge(FASTRPC_SMD_GUID,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700470 SMD_APPS_QDSP, &me->chan,
471 me, smd_event_handler));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700472 if (err)
473 goto bail;
474 VERIFY(err, 0 != wait_for_completion_timeout(&me->work,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700475 RPC_TIMEOUT));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700476 if (err)
477 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700478 }
479 bail:
480 if (err)
481 fastrpc_deinit();
482 return err;
483}
484
485static void free_dev(struct fastrpc_device *dev)
486{
487 if (dev) {
488 module_put(THIS_MODULE);
489 free_mem(&dev->buf);
490 kfree(dev);
491 }
492}
493
494static int alloc_dev(struct fastrpc_device **dev)
495{
496 int err = 0;
497 struct fastrpc_device *fd = 0;
498
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700499 VERIFY(err, 0 != try_module_get(THIS_MODULE));
500 if (err)
501 goto bail;
502 VERIFY(err, 0 != (fd = kzalloc(sizeof(*fd), GFP_KERNEL)));
503 if (err)
504 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700505 fd->buf.size = PAGE_SIZE;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700506 VERIFY(err, 0 == alloc_mem(&fd->buf));
507 if (err)
508 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700509 fd->tgid = current->tgid;
510 INIT_HLIST_NODE(&fd->hn);
511 *dev = fd;
512 bail:
513 if (err)
514 free_dev(fd);
515 return err;
516}
517
518static int get_dev(struct fastrpc_apps *me, struct fastrpc_device **rdev)
519{
520 struct hlist_head *head;
521 struct fastrpc_device *dev = 0;
522 struct hlist_node *n;
523 uint32_t h = hash_32(current->tgid, RPC_HASH_BITS);
524 int err = 0;
525
526 spin_lock(&me->hlock);
527 head = &me->htbl[h];
528 hlist_for_each_entry(dev, n, head, hn) {
529 if (dev->tgid == current->tgid) {
530 hlist_del(&dev->hn);
531 break;
532 }
533 }
534 spin_unlock(&me->hlock);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700535 VERIFY(err, dev != 0);
536 if (err)
537 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700538 *rdev = dev;
539 bail:
540 if (err) {
541 free_dev(dev);
542 err = alloc_dev(rdev);
543 }
544 return err;
545}
546
547static void add_dev(struct fastrpc_apps *me, struct fastrpc_device *dev)
548{
549 struct hlist_head *head;
550 uint32_t h = hash_32(current->tgid, RPC_HASH_BITS);
551
552 spin_lock(&me->hlock);
553 head = &me->htbl[h];
554 hlist_add_head(&dev->hn, head);
555 spin_unlock(&me->hlock);
556 return;
557}
558
559static int fastrpc_release_current_dsp_process(void);
560
561static int fastrpc_internal_invoke(struct fastrpc_apps *me, uint32_t kernel,
562 struct fastrpc_ioctl_invoke *invoke, remote_arg_t *pra)
563{
564 remote_arg_t *rpra = 0;
565 struct fastrpc_device *dev = 0;
566 struct smq_invoke_ctx *ctx = 0;
567 struct fastrpc_buf obuf, *abufs = 0, *b;
568 int interrupted = 0;
569 uint32_t sc;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700570 int i, nbufs = 0, err = 0;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700571
572 sc = invoke->sc;
573 obuf.handle = 0;
574 if (REMOTE_SCALARS_LENGTH(sc)) {
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700575 VERIFY(err, 0 == get_dev(me, &dev));
576 if (err)
577 goto bail;
578 VERIFY(err, 0 == get_page_list(kernel, sc, pra, &dev->buf,
579 &obuf));
580 if (err)
581 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700582 rpra = (remote_arg_t *)obuf.virt;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700583 VERIFY(err, 0 == get_args(kernel, sc, pra, rpra, invoke->pra,
584 &obuf, &abufs, &nbufs));
585 if (err)
586 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700587 }
588
589 context_list_alloc_ctx(&me->clst, &ctx);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700590 VERIFY(err, 0 == fastrpc_invoke_send(me, invoke->handle, sc, ctx,
591 &obuf));
592 if (err)
593 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700594 inv_args(sc, rpra, obuf.used);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700595 VERIFY(err, 0 == (interrupted =
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700596 wait_for_completion_interruptible(&ctx->work)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700597 if (err)
598 goto bail;
599 VERIFY(err, 0 == (err = ctx->retval));
600 if (err)
601 goto bail;
602 VERIFY(err, 0 == put_args(kernel, sc, pra, rpra, invoke->pra));
603 if (err)
604 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700605 bail:
606 if (interrupted) {
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700607 if (!kernel)
608 (void)fastrpc_release_current_dsp_process();
609 wait_for_completion(&ctx->work);
610 }
611 context_free(ctx);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700612 for (i = 0, b = abufs; i < nbufs; ++i, ++b)
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700613 free_mem(b);
614 kfree(abufs);
615 if (dev) {
616 add_dev(me, dev);
617 if (obuf.handle != dev->buf.handle)
618 free_mem(&obuf);
619 }
620 return err;
621}
622
623static int fastrpc_create_current_dsp_process(void)
624{
625 int err = 0;
626 struct fastrpc_ioctl_invoke ioctl;
627 struct fastrpc_apps *me = &gfa;
628 remote_arg_t ra[1];
629 int tgid = 0;
630
631 tgid = current->tgid;
632 ra[0].buf.pv = &tgid;
633 ra[0].buf.len = sizeof(tgid);
634 ioctl.handle = 1;
635 ioctl.sc = REMOTE_SCALARS_MAKE(0, 1, 0);
636 ioctl.pra = ra;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700637 VERIFY(err, 0 == fastrpc_internal_invoke(me, 1, &ioctl, ra));
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700638 return err;
639}
640
641static int fastrpc_release_current_dsp_process(void)
642{
643 int err = 0;
644 struct fastrpc_apps *me = &gfa;
645 struct fastrpc_ioctl_invoke ioctl;
646 remote_arg_t ra[1];
647 int tgid = 0;
648
649 tgid = current->tgid;
650 ra[0].buf.pv = &tgid;
651 ra[0].buf.len = sizeof(tgid);
652 ioctl.handle = 1;
653 ioctl.sc = REMOTE_SCALARS_MAKE(1, 1, 0);
654 ioctl.pra = ra;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700655 VERIFY(err, 0 == fastrpc_internal_invoke(me, 1, &ioctl, ra));
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700656 return err;
657}
658
659static void cleanup_current_dev(void)
660{
661 struct fastrpc_apps *me = &gfa;
662 uint32_t h = hash_32(current->tgid, RPC_HASH_BITS);
663 struct hlist_head *head;
664 struct hlist_node *pos;
665 struct fastrpc_device *dev;
666
667 rnext:
668 dev = 0;
669 spin_lock(&me->hlock);
670 head = &me->htbl[h];
671 hlist_for_each_entry(dev, pos, head, hn) {
672 if (dev->tgid == current->tgid) {
673 hlist_del(&dev->hn);
674 break;
675 }
676 }
677 spin_unlock(&me->hlock);
678 if (dev) {
679 free_dev(dev);
680 goto rnext;
681 }
682 return;
683}
684
685static int fastrpc_device_release(struct inode *inode, struct file *file)
686{
687 (void)fastrpc_release_current_dsp_process();
688 cleanup_current_dev();
689 return 0;
690}
691
692static int fastrpc_device_open(struct inode *inode, struct file *filp)
693{
694 int err = 0;
695
696 if (0 != try_module_get(THIS_MODULE)) {
697 /* This call will cause a dev to be created
698 * which will addref this module
699 */
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700700 VERIFY(err, 0 == fastrpc_create_current_dsp_process());
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700701 if (err)
702 cleanup_current_dev();
703 module_put(THIS_MODULE);
704 }
705 return err;
706}
707
708
709static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num,
710 unsigned long ioctl_param)
711{
712 struct fastrpc_apps *me = &gfa;
713 struct fastrpc_ioctl_invoke invoke;
714 remote_arg_t *pra = 0;
715 void *param = (char *)ioctl_param;
716 int bufs, err = 0;
717
718 switch (ioctl_num) {
719 case FASTRPC_IOCTL_INVOKE:
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700720 VERIFY(err, 0 == copy_from_user(&invoke, param,
721 sizeof(invoke)));
722 if (err)
723 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700724 bufs = REMOTE_SCALARS_INBUFS(invoke.sc) +
725 REMOTE_SCALARS_OUTBUFS(invoke.sc);
726 if (bufs) {
727 bufs = bufs * sizeof(*pra);
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700728 VERIFY(err, 0 != (pra = kmalloc(bufs, GFP_KERNEL)));
729 if (err)
730 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700731 }
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700732 VERIFY(err, 0 == copy_from_user(pra, invoke.pra, bufs));
733 if (err)
734 goto bail;
735 VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 0, &invoke,
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700736 pra)));
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700737 if (err)
738 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700739 break;
740 default:
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700741 err = -ENOTTY;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700742 break;
743 }
744 bail:
745 kfree(pra);
746 return err;
747}
748
749static const struct file_operations fops = {
750 .open = fastrpc_device_open,
751 .release = fastrpc_device_release,
752 .unlocked_ioctl = fastrpc_device_ioctl,
753};
754
755static int __init fastrpc_device_init(void)
756{
757 struct fastrpc_apps *me = &gfa;
758 int err = 0;
759
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700760 VERIFY(err, 0 == fastrpc_init());
761 if (err)
762 goto bail;
763 VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, 1, DEVICE_NAME));
764 if (err)
765 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700766 cdev_init(&me->cdev, &fops);
767 me->cdev.owner = THIS_MODULE;
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700768 VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0), 1));
769 if (err)
770 goto bail;
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700771 pr_info("'mknod /dev/%s c %d 0'\n", DEVICE_NAME, MAJOR(me->dev_no));
772 bail:
Mitchel Humpherys42e806e2012-09-30 22:27:53 -0700773 if (err) {
774 if (me->dev_no)
775 unregister_chrdev_region(me->dev_no, 1);
776 fastrpc_deinit();
777 }
Mitchel Humpherys79d361e2012-08-29 16:20:15 -0700778 return err;
779}
780
781static void __exit fastrpc_device_exit(void)
782{
783 struct fastrpc_apps *me = &gfa;
784
785 fastrpc_deinit();
786 cdev_del(&me->cdev);
787 unregister_chrdev_region(me->dev_no, 1);
788}
789
790module_init(fastrpc_device_init);
791module_exit(fastrpc_device_exit);
792
793MODULE_LICENSE("GPL v2");