Commit | Line | Data |
---|---|---|
f6f9279f SK |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | // Copyright (c) 2011-2018, The Linux Foundation. All rights reserved. | |
3 | // Copyright (c) 2018, Linaro Limited | |
4 | ||
c68cfb71 | 5 | #include <linux/completion.h> |
f6f9279f | 6 | #include <linux/device.h> |
c68cfb71 | 7 | #include <linux/dma-buf.h> |
f6f9279f SK |
8 | #include <linux/dma-mapping.h> |
9 | #include <linux/idr.h> | |
10 | #include <linux/list.h> | |
11 | #include <linux/miscdevice.h> | |
12 | #include <linux/module.h> | |
13 | #include <linux/of_address.h> | |
14 | #include <linux/of.h> | |
15 | #include <linux/of_platform.h> | |
16 | #include <linux/rpmsg.h> | |
17 | #include <linux/scatterlist.h> | |
18 | #include <linux/slab.h> | |
c68cfb71 | 19 | #include <uapi/misc/fastrpc.h> |
f6f9279f SK |
20 | |
21 | #define ADSP_DOMAIN_ID (0) | |
22 | #define MDSP_DOMAIN_ID (1) | |
23 | #define SDSP_DOMAIN_ID (2) | |
24 | #define CDSP_DOMAIN_ID (3) | |
25 | #define FASTRPC_DEV_MAX 4 /* adsp, mdsp, slpi, cdsp*/ | |
26 | #define FASTRPC_MAX_SESSIONS 9 /*8 compute, 1 cpz*/ | |
c68cfb71 SK |
27 | #define FASTRPC_ALIGN 128 |
28 | #define FASTRPC_MAX_FDLIST 16 | |
29 | #define FASTRPC_MAX_CRCLIST 64 | |
30 | #define FASTRPC_PHYS(p) ((p) & 0xffffffff) | |
f6f9279f | 31 | #define FASTRPC_CTX_MAX (256) |
d73f71c7 | 32 | #define FASTRPC_INIT_HANDLE 1 |
f6f9279f | 33 | #define FASTRPC_CTXID_MASK (0xFF0) |
d73f71c7 SK |
34 | #define INIT_FILELEN_MAX (2 * 1024 * 1024) |
35 | #define INIT_MEMLEN_MAX (8 * 1024 * 1024) | |
f6f9279f SK |
36 | #define FASTRPC_DEVICE_NAME "fastrpc" |
37 | ||
c68cfb71 SK |
38 | /* Retrives number of input buffers from the scalars parameter */ |
39 | #define REMOTE_SCALARS_INBUFS(sc) (((sc) >> 16) & 0x0ff) | |
40 | ||
41 | /* Retrives number of output buffers from the scalars parameter */ | |
42 | #define REMOTE_SCALARS_OUTBUFS(sc) (((sc) >> 8) & 0x0ff) | |
43 | ||
44 | /* Retrives number of input handles from the scalars parameter */ | |
45 | #define REMOTE_SCALARS_INHANDLES(sc) (((sc) >> 4) & 0x0f) | |
46 | ||
47 | /* Retrives number of output handles from the scalars parameter */ | |
48 | #define REMOTE_SCALARS_OUTHANDLES(sc) ((sc) & 0x0f) | |
49 | ||
50 | #define REMOTE_SCALARS_LENGTH(sc) (REMOTE_SCALARS_INBUFS(sc) + \ | |
51 | REMOTE_SCALARS_OUTBUFS(sc) + \ | |
52 | REMOTE_SCALARS_INHANDLES(sc)+ \ | |
53 | REMOTE_SCALARS_OUTHANDLES(sc)) | |
54 | #define FASTRPC_BUILD_SCALARS(attr, method, in, out, oin, oout) \ | |
55 | (((attr & 0x07) << 29) | \ | |
56 | ((method & 0x1f) << 24) | \ | |
57 | ((in & 0xff) << 16) | \ | |
58 | ((out & 0xff) << 8) | \ | |
59 | ((oin & 0x0f) << 4) | \ | |
60 | (oout & 0x0f)) | |
61 | ||
62 | #define FASTRPC_SCALARS(method, in, out) \ | |
63 | FASTRPC_BUILD_SCALARS(0, method, in, out, 0, 0) | |
64 | ||
d73f71c7 SK |
65 | #define FASTRPC_CREATE_PROCESS_NARGS 6 |
66 | /* Remote Method id table */ | |
67 | #define FASTRPC_RMID_INIT_ATTACH 0 | |
68 | #define FASTRPC_RMID_INIT_RELEASE 1 | |
69 | #define FASTRPC_RMID_INIT_CREATE 6 | |
70 | #define FASTRPC_RMID_INIT_CREATE_ATTR 7 | |
71 | #define FASTRPC_RMID_INIT_CREATE_STATIC 8 | |
72 | ||
f6f9279f SK |
73 | #define miscdev_to_cctx(d) container_of(d, struct fastrpc_channel_ctx, miscdev) |
74 | ||
75 | static const char *domains[FASTRPC_DEV_MAX] = { "adsp", "mdsp", | |
76 | "sdsp", "cdsp"}; | |
c68cfb71 SK |
77 | struct fastrpc_phy_page { |
78 | u64 addr; /* physical address */ | |
79 | u64 size; /* size of contiguous region */ | |
80 | }; | |
81 | ||
82 | struct fastrpc_invoke_buf { | |
83 | u32 num; /* number of contiguous regions */ | |
84 | u32 pgidx; /* index to start of contiguous region */ | |
85 | }; | |
86 | ||
87 | struct fastrpc_remote_arg { | |
88 | u64 pv; | |
89 | u64 len; | |
90 | }; | |
91 | ||
92 | struct fastrpc_msg { | |
93 | int pid; /* process group id */ | |
94 | int tid; /* thread id */ | |
95 | u64 ctx; /* invoke caller context */ | |
96 | u32 handle; /* handle to invoke */ | |
97 | u32 sc; /* scalars structure describing the data */ | |
98 | u64 addr; /* physical address */ | |
99 | u64 size; /* size of contiguous region */ | |
100 | }; | |
101 | ||
102 | struct fastrpc_invoke_rsp { | |
103 | u64 ctx; /* invoke caller context */ | |
104 | int retval; /* invoke return value */ | |
105 | }; | |
106 | ||
107 | struct fastrpc_buf { | |
108 | struct fastrpc_user *fl; | |
6cffd795 | 109 | struct dma_buf *dmabuf; |
c68cfb71 SK |
110 | struct device *dev; |
111 | void *virt; | |
112 | u64 phys; | |
113 | u64 size; | |
6cffd795 SK |
114 | /* Lock for dma buf attachments */ |
115 | struct mutex lock; | |
116 | struct list_head attachments; | |
117 | }; | |
118 | ||
119 | struct fastrpc_dma_buf_attachment { | |
120 | struct device *dev; | |
121 | struct sg_table sgt; | |
122 | struct list_head node; | |
c68cfb71 SK |
123 | }; |
124 | ||
125 | struct fastrpc_map { | |
126 | struct list_head node; | |
127 | struct fastrpc_user *fl; | |
128 | int fd; | |
129 | struct dma_buf *buf; | |
130 | struct sg_table *table; | |
131 | struct dma_buf_attachment *attach; | |
132 | u64 phys; | |
133 | u64 size; | |
134 | void *va; | |
135 | u64 len; | |
136 | struct kref refcount; | |
137 | }; | |
138 | ||
139 | struct fastrpc_invoke_ctx { | |
140 | int nscalars; | |
141 | int nbufs; | |
142 | int retval; | |
143 | int pid; | |
144 | int tgid; | |
145 | u32 sc; | |
146 | u32 *crc; | |
147 | u64 ctxid; | |
148 | u64 msg_sz; | |
149 | struct kref refcount; | |
150 | struct list_head node; /* list of ctxs */ | |
151 | struct completion work; | |
8e7389c7 | 152 | struct work_struct put_work; |
c68cfb71 SK |
153 | struct fastrpc_msg msg; |
154 | struct fastrpc_user *fl; | |
155 | struct fastrpc_remote_arg *rpra; | |
156 | struct fastrpc_map **maps; | |
157 | struct fastrpc_buf *buf; | |
158 | struct fastrpc_invoke_args *args; | |
159 | struct fastrpc_channel_ctx *cctx; | |
160 | }; | |
f6f9279f SK |
161 | |
162 | struct fastrpc_session_ctx { | |
163 | struct device *dev; | |
164 | int sid; | |
165 | bool used; | |
166 | bool valid; | |
167 | }; | |
168 | ||
169 | struct fastrpc_channel_ctx { | |
170 | int domain_id; | |
171 | int sesscount; | |
172 | struct rpmsg_device *rpdev; | |
173 | struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS]; | |
174 | spinlock_t lock; | |
175 | struct idr ctx_idr; | |
176 | struct list_head users; | |
177 | struct miscdevice miscdev; | |
178 | }; | |
179 | ||
180 | struct fastrpc_user { | |
181 | struct list_head user; | |
182 | struct list_head maps; | |
183 | struct list_head pending; | |
184 | ||
185 | struct fastrpc_channel_ctx *cctx; | |
186 | struct fastrpc_session_ctx *sctx; | |
c68cfb71 | 187 | struct fastrpc_buf *init_mem; |
f6f9279f SK |
188 | |
189 | int tgid; | |
190 | int pd; | |
191 | /* Lock for lists */ | |
192 | spinlock_t lock; | |
193 | /* lock for allocations */ | |
194 | struct mutex mutex; | |
195 | }; | |
196 | ||
c68cfb71 SK |
197 | static void fastrpc_free_map(struct kref *ref) |
198 | { | |
199 | struct fastrpc_map *map; | |
200 | ||
201 | map = container_of(ref, struct fastrpc_map, refcount); | |
202 | ||
203 | if (map->table) { | |
204 | dma_buf_unmap_attachment(map->attach, map->table, | |
205 | DMA_BIDIRECTIONAL); | |
206 | dma_buf_detach(map->buf, map->attach); | |
207 | dma_buf_put(map->buf); | |
208 | } | |
209 | ||
210 | kfree(map); | |
211 | } | |
212 | ||
213 | static void fastrpc_map_put(struct fastrpc_map *map) | |
214 | { | |
215 | if (map) | |
216 | kref_put(&map->refcount, fastrpc_free_map); | |
217 | } | |
218 | ||
219 | static void fastrpc_map_get(struct fastrpc_map *map) | |
220 | { | |
221 | if (map) | |
222 | kref_get(&map->refcount); | |
223 | } | |
224 | ||
225 | static int fastrpc_map_find(struct fastrpc_user *fl, int fd, | |
226 | struct fastrpc_map **ppmap) | |
227 | { | |
228 | struct fastrpc_map *map = NULL; | |
229 | ||
230 | mutex_lock(&fl->mutex); | |
231 | list_for_each_entry(map, &fl->maps, node) { | |
232 | if (map->fd == fd) { | |
233 | fastrpc_map_get(map); | |
234 | *ppmap = map; | |
235 | mutex_unlock(&fl->mutex); | |
236 | return 0; | |
237 | } | |
238 | } | |
239 | mutex_unlock(&fl->mutex); | |
240 | ||
241 | return -ENOENT; | |
242 | } | |
243 | ||
244 | static void fastrpc_buf_free(struct fastrpc_buf *buf) | |
245 | { | |
246 | dma_free_coherent(buf->dev, buf->size, buf->virt, | |
247 | FASTRPC_PHYS(buf->phys)); | |
248 | kfree(buf); | |
249 | } | |
250 | ||
251 | static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev, | |
252 | u64 size, struct fastrpc_buf **obuf) | |
253 | { | |
254 | struct fastrpc_buf *buf; | |
255 | ||
256 | buf = kzalloc(sizeof(*buf), GFP_KERNEL); | |
257 | if (!buf) | |
258 | return -ENOMEM; | |
259 | ||
6cffd795 SK |
260 | INIT_LIST_HEAD(&buf->attachments); |
261 | mutex_init(&buf->lock); | |
262 | ||
c68cfb71 SK |
263 | buf->fl = fl; |
264 | buf->virt = NULL; | |
265 | buf->phys = 0; | |
266 | buf->size = size; | |
267 | buf->dev = dev; | |
268 | ||
269 | buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys, | |
270 | GFP_KERNEL); | |
271 | if (!buf->virt) | |
272 | return -ENOMEM; | |
273 | ||
274 | if (fl->sctx && fl->sctx->sid) | |
275 | buf->phys += ((u64)fl->sctx->sid << 32); | |
276 | ||
277 | *obuf = buf; | |
278 | ||
279 | return 0; | |
280 | } | |
281 | ||
282 | static void fastrpc_context_free(struct kref *ref) | |
283 | { | |
284 | struct fastrpc_invoke_ctx *ctx; | |
285 | struct fastrpc_channel_ctx *cctx; | |
286 | int i; | |
287 | ||
288 | ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount); | |
289 | cctx = ctx->cctx; | |
290 | ||
291 | for (i = 0; i < ctx->nscalars; i++) | |
292 | fastrpc_map_put(ctx->maps[i]); | |
293 | ||
294 | if (ctx->buf) | |
295 | fastrpc_buf_free(ctx->buf); | |
296 | ||
297 | spin_lock(&cctx->lock); | |
298 | idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4); | |
299 | spin_unlock(&cctx->lock); | |
300 | ||
301 | kfree(ctx->maps); | |
302 | kfree(ctx); | |
303 | } | |
304 | ||
305 | static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx) | |
306 | { | |
307 | kref_get(&ctx->refcount); | |
308 | } | |
309 | ||
310 | static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx) | |
311 | { | |
312 | kref_put(&ctx->refcount, fastrpc_context_free); | |
313 | } | |
314 | ||
8e7389c7 TE |
315 | static void fastrpc_context_put_wq(struct work_struct *work) |
316 | { | |
317 | struct fastrpc_invoke_ctx *ctx = | |
318 | container_of(work, struct fastrpc_invoke_ctx, put_work); | |
319 | ||
320 | fastrpc_context_put(ctx); | |
321 | } | |
322 | ||
c68cfb71 SK |
323 | static struct fastrpc_invoke_ctx *fastrpc_context_alloc( |
324 | struct fastrpc_user *user, u32 kernel, u32 sc, | |
325 | struct fastrpc_invoke_args *args) | |
326 | { | |
327 | struct fastrpc_channel_ctx *cctx = user->cctx; | |
328 | struct fastrpc_invoke_ctx *ctx = NULL; | |
329 | int ret; | |
330 | ||
331 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
332 | if (!ctx) | |
333 | return ERR_PTR(-ENOMEM); | |
334 | ||
335 | INIT_LIST_HEAD(&ctx->node); | |
336 | ctx->fl = user; | |
337 | ctx->nscalars = REMOTE_SCALARS_LENGTH(sc); | |
338 | ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) + | |
339 | REMOTE_SCALARS_OUTBUFS(sc); | |
340 | ||
341 | if (ctx->nscalars) { | |
342 | ctx->maps = kcalloc(ctx->nscalars, | |
343 | sizeof(*ctx->maps), GFP_KERNEL); | |
344 | if (!ctx->maps) { | |
345 | kfree(ctx); | |
346 | return ERR_PTR(-ENOMEM); | |
347 | } | |
348 | ctx->args = args; | |
349 | } | |
350 | ||
351 | ctx->sc = sc; | |
352 | ctx->retval = -1; | |
353 | ctx->pid = current->pid; | |
354 | ctx->tgid = user->tgid; | |
355 | ctx->cctx = cctx; | |
356 | init_completion(&ctx->work); | |
8e7389c7 | 357 | INIT_WORK(&ctx->put_work, fastrpc_context_put_wq); |
c68cfb71 SK |
358 | |
359 | spin_lock(&user->lock); | |
360 | list_add_tail(&ctx->node, &user->pending); | |
361 | spin_unlock(&user->lock); | |
362 | ||
363 | spin_lock(&cctx->lock); | |
364 | ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1, | |
365 | FASTRPC_CTX_MAX, GFP_ATOMIC); | |
366 | if (ret < 0) { | |
367 | spin_unlock(&cctx->lock); | |
368 | goto err_idr; | |
369 | } | |
370 | ctx->ctxid = ret << 4; | |
371 | spin_unlock(&cctx->lock); | |
372 | ||
373 | kref_init(&ctx->refcount); | |
374 | ||
375 | return ctx; | |
376 | err_idr: | |
377 | spin_lock(&user->lock); | |
378 | list_del(&ctx->node); | |
379 | spin_unlock(&user->lock); | |
380 | kfree(ctx->maps); | |
381 | kfree(ctx); | |
382 | ||
383 | return ERR_PTR(ret); | |
384 | } | |
385 | ||
6cffd795 SK |
386 | static struct sg_table * |
387 | fastrpc_map_dma_buf(struct dma_buf_attachment *attachment, | |
388 | enum dma_data_direction dir) | |
389 | { | |
390 | struct fastrpc_dma_buf_attachment *a = attachment->priv; | |
391 | struct sg_table *table; | |
392 | ||
393 | table = &a->sgt; | |
394 | ||
395 | if (!dma_map_sg(attachment->dev, table->sgl, table->nents, dir)) | |
396 | return ERR_PTR(-ENOMEM); | |
397 | ||
398 | return table; | |
399 | } | |
400 | ||
401 | static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach, | |
402 | struct sg_table *table, | |
403 | enum dma_data_direction dir) | |
404 | { | |
405 | dma_unmap_sg(attach->dev, table->sgl, table->nents, dir); | |
406 | } | |
407 | ||
408 | static void fastrpc_release(struct dma_buf *dmabuf) | |
409 | { | |
410 | struct fastrpc_buf *buffer = dmabuf->priv; | |
411 | ||
412 | fastrpc_buf_free(buffer); | |
413 | } | |
414 | ||
415 | static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf, | |
416 | struct dma_buf_attachment *attachment) | |
417 | { | |
418 | struct fastrpc_dma_buf_attachment *a; | |
419 | struct fastrpc_buf *buffer = dmabuf->priv; | |
420 | int ret; | |
421 | ||
422 | a = kzalloc(sizeof(*a), GFP_KERNEL); | |
423 | if (!a) | |
424 | return -ENOMEM; | |
425 | ||
426 | ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt, | |
427 | FASTRPC_PHYS(buffer->phys), buffer->size); | |
428 | if (ret < 0) { | |
429 | dev_err(buffer->dev, "failed to get scatterlist from DMA API\n"); | |
430 | return -EINVAL; | |
431 | } | |
432 | ||
433 | a->dev = attachment->dev; | |
434 | INIT_LIST_HEAD(&a->node); | |
435 | attachment->priv = a; | |
436 | ||
437 | mutex_lock(&buffer->lock); | |
438 | list_add(&a->node, &buffer->attachments); | |
439 | mutex_unlock(&buffer->lock); | |
440 | ||
441 | return 0; | |
442 | } | |
443 | ||
444 | static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf, | |
445 | struct dma_buf_attachment *attachment) | |
446 | { | |
447 | struct fastrpc_dma_buf_attachment *a = attachment->priv; | |
448 | struct fastrpc_buf *buffer = dmabuf->priv; | |
449 | ||
450 | mutex_lock(&buffer->lock); | |
451 | list_del(&a->node); | |
452 | mutex_unlock(&buffer->lock); | |
453 | kfree(a); | |
454 | } | |
455 | ||
456 | static void *fastrpc_kmap(struct dma_buf *dmabuf, unsigned long pgnum) | |
457 | { | |
458 | struct fastrpc_buf *buf = dmabuf->priv; | |
459 | ||
460 | return buf->virt ? buf->virt + pgnum * PAGE_SIZE : NULL; | |
461 | } | |
462 | ||
463 | static void *fastrpc_vmap(struct dma_buf *dmabuf) | |
464 | { | |
465 | struct fastrpc_buf *buf = dmabuf->priv; | |
466 | ||
467 | return buf->virt; | |
468 | } | |
469 | ||
470 | static int fastrpc_mmap(struct dma_buf *dmabuf, | |
471 | struct vm_area_struct *vma) | |
472 | { | |
473 | struct fastrpc_buf *buf = dmabuf->priv; | |
474 | size_t size = vma->vm_end - vma->vm_start; | |
475 | ||
476 | return dma_mmap_coherent(buf->dev, vma, buf->virt, | |
477 | FASTRPC_PHYS(buf->phys), size); | |
478 | } | |
479 | ||
480 | static const struct dma_buf_ops fastrpc_dma_buf_ops = { | |
481 | .attach = fastrpc_dma_buf_attach, | |
482 | .detach = fastrpc_dma_buf_detatch, | |
483 | .map_dma_buf = fastrpc_map_dma_buf, | |
484 | .unmap_dma_buf = fastrpc_unmap_dma_buf, | |
485 | .mmap = fastrpc_mmap, | |
486 | .map = fastrpc_kmap, | |
487 | .vmap = fastrpc_vmap, | |
488 | .release = fastrpc_release, | |
489 | }; | |
490 | ||
c68cfb71 SK |
491 | static int fastrpc_map_create(struct fastrpc_user *fl, int fd, |
492 | u64 len, struct fastrpc_map **ppmap) | |
493 | { | |
494 | struct fastrpc_session_ctx *sess = fl->sctx; | |
495 | struct fastrpc_map *map = NULL; | |
496 | int err = 0; | |
497 | ||
498 | if (!fastrpc_map_find(fl, fd, ppmap)) | |
499 | return 0; | |
500 | ||
501 | map = kzalloc(sizeof(*map), GFP_KERNEL); | |
502 | if (!map) | |
503 | return -ENOMEM; | |
504 | ||
505 | INIT_LIST_HEAD(&map->node); | |
506 | map->fl = fl; | |
507 | map->fd = fd; | |
508 | map->buf = dma_buf_get(fd); | |
682a6044 WY |
509 | if (IS_ERR(map->buf)) { |
510 | err = PTR_ERR(map->buf); | |
c68cfb71 SK |
511 | goto get_err; |
512 | } | |
513 | ||
514 | map->attach = dma_buf_attach(map->buf, sess->dev); | |
515 | if (IS_ERR(map->attach)) { | |
516 | dev_err(sess->dev, "Failed to attach dmabuf\n"); | |
517 | err = PTR_ERR(map->attach); | |
518 | goto attach_err; | |
519 | } | |
520 | ||
521 | map->table = dma_buf_map_attachment(map->attach, DMA_BIDIRECTIONAL); | |
522 | if (IS_ERR(map->table)) { | |
523 | err = PTR_ERR(map->table); | |
524 | goto map_err; | |
525 | } | |
526 | ||
527 | map->phys = sg_dma_address(map->table->sgl); | |
528 | map->phys += ((u64)fl->sctx->sid << 32); | |
529 | map->size = len; | |
530 | map->va = sg_virt(map->table->sgl); | |
531 | map->len = len; | |
532 | kref_init(&map->refcount); | |
533 | ||
534 | spin_lock(&fl->lock); | |
535 | list_add_tail(&map->node, &fl->maps); | |
536 | spin_unlock(&fl->lock); | |
537 | *ppmap = map; | |
538 | ||
539 | return 0; | |
540 | ||
541 | map_err: | |
542 | dma_buf_detach(map->buf, map->attach); | |
543 | attach_err: | |
544 | dma_buf_put(map->buf); | |
545 | get_err: | |
546 | kfree(map); | |
547 | ||
548 | return err; | |
549 | } | |
550 | ||
551 | /* | |
552 | * Fastrpc payload buffer with metadata looks like: | |
553 | * | |
554 | * >>>>>> START of METADATA <<<<<<<<< | |
555 | * +---------------------------------+ | |
556 | * | Arguments | | |
557 | * | type:(struct fastrpc_remote_arg)| | |
558 | * | (0 - N) | | |
559 | * +---------------------------------+ | |
560 | * | Invoke Buffer list | | |
561 | * | type:(struct fastrpc_invoke_buf)| | |
562 | * | (0 - N) | | |
563 | * +---------------------------------+ | |
564 | * | Page info list | | |
565 | * | type:(struct fastrpc_phy_page) | | |
566 | * | (0 - N) | | |
567 | * +---------------------------------+ | |
568 | * | Optional info | | |
569 | * |(can be specific to SoC/Firmware)| | |
570 | * +---------------------------------+ | |
571 | * >>>>>>>> END of METADATA <<<<<<<<< | |
572 | * +---------------------------------+ | |
573 | * | Inline ARGS | | |
574 | * | (0-N) | | |
575 | * +---------------------------------+ | |
576 | */ | |
577 | ||
578 | static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx) | |
579 | { | |
580 | int size = 0; | |
581 | ||
582 | size = (sizeof(struct fastrpc_remote_arg) + | |
583 | sizeof(struct fastrpc_invoke_buf) + | |
584 | sizeof(struct fastrpc_phy_page)) * ctx->nscalars + | |
585 | sizeof(u64) * FASTRPC_MAX_FDLIST + | |
586 | sizeof(u32) * FASTRPC_MAX_CRCLIST; | |
587 | ||
588 | return size; | |
589 | } | |
590 | ||
591 | static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen) | |
592 | { | |
593 | u64 size = 0; | |
594 | int i; | |
595 | ||
596 | size = ALIGN(metalen, FASTRPC_ALIGN); | |
597 | for (i = 0; i < ctx->nscalars; i++) { | |
598 | if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) { | |
599 | size = ALIGN(size, FASTRPC_ALIGN); | |
600 | size += ctx->args[i].length; | |
601 | } | |
602 | } | |
603 | ||
604 | return size; | |
605 | } | |
606 | ||
607 | static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx) | |
608 | { | |
609 | struct device *dev = ctx->fl->sctx->dev; | |
610 | int i, err; | |
611 | ||
612 | for (i = 0; i < ctx->nscalars; ++i) { | |
613 | /* Make sure reserved field is set to 0 */ | |
614 | if (ctx->args[i].reserved) | |
615 | return -EINVAL; | |
616 | ||
617 | if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 || | |
618 | ctx->args[i].length == 0) | |
619 | continue; | |
620 | ||
621 | err = fastrpc_map_create(ctx->fl, ctx->args[i].fd, | |
622 | ctx->args[i].length, &ctx->maps[i]); | |
623 | if (err) { | |
624 | dev_err(dev, "Error Creating map %d\n", err); | |
625 | return -EINVAL; | |
626 | } | |
627 | ||
628 | } | |
629 | return 0; | |
630 | } | |
631 | ||
632 | static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx) | |
633 | { | |
634 | struct device *dev = ctx->fl->sctx->dev; | |
635 | struct fastrpc_remote_arg *rpra; | |
636 | struct fastrpc_invoke_buf *list; | |
637 | struct fastrpc_phy_page *pages; | |
638 | int inbufs, i, err = 0; | |
639 | u64 rlen, pkt_size; | |
640 | uintptr_t args; | |
641 | int metalen; | |
642 | ||
643 | ||
644 | inbufs = REMOTE_SCALARS_INBUFS(ctx->sc); | |
645 | metalen = fastrpc_get_meta_size(ctx); | |
646 | pkt_size = fastrpc_get_payload_size(ctx, metalen); | |
647 | ||
648 | err = fastrpc_create_maps(ctx); | |
649 | if (err) | |
650 | return err; | |
651 | ||
652 | ctx->msg_sz = pkt_size; | |
653 | ||
654 | err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf); | |
655 | if (err) | |
656 | return err; | |
657 | ||
658 | rpra = ctx->buf->virt; | |
659 | list = ctx->buf->virt + ctx->nscalars * sizeof(*rpra); | |
660 | pages = ctx->buf->virt + ctx->nscalars * (sizeof(*list) + | |
661 | sizeof(*rpra)); | |
662 | args = (uintptr_t)ctx->buf->virt + metalen; | |
663 | rlen = pkt_size - metalen; | |
664 | ctx->rpra = rpra; | |
665 | ||
666 | for (i = 0; i < ctx->nbufs; ++i) { | |
667 | u64 len = ctx->args[i].length; | |
668 | ||
669 | rpra[i].pv = 0; | |
670 | rpra[i].len = len; | |
671 | list[i].num = len ? 1 : 0; | |
672 | list[i].pgidx = i; | |
673 | ||
674 | if (!len) | |
675 | continue; | |
676 | ||
677 | pages[i].size = roundup(len, PAGE_SIZE); | |
678 | ||
679 | if (ctx->maps[i]) { | |
680 | rpra[i].pv = (u64) ctx->args[i].ptr; | |
681 | pages[i].addr = ctx->maps[i]->phys; | |
682 | } else { | |
683 | rlen -= ALIGN(args, FASTRPC_ALIGN) - args; | |
684 | args = ALIGN(args, FASTRPC_ALIGN); | |
685 | if (rlen < len) | |
686 | goto bail; | |
687 | ||
688 | rpra[i].pv = args; | |
689 | pages[i].addr = ctx->buf->phys + (pkt_size - rlen); | |
690 | pages[i].addr = pages[i].addr & PAGE_MASK; | |
691 | args = args + len; | |
692 | rlen -= len; | |
693 | } | |
694 | ||
695 | if (i < inbufs && !ctx->maps[i]) { | |
696 | void *dst = (void *)(uintptr_t)rpra[i].pv; | |
697 | void *src = (void *)(uintptr_t)ctx->args[i].ptr; | |
698 | ||
699 | if (!kernel) { | |
700 | if (copy_from_user(dst, (void __user *)src, | |
701 | len)) { | |
702 | err = -EFAULT; | |
703 | goto bail; | |
704 | } | |
705 | } else { | |
706 | memcpy(dst, src, len); | |
707 | } | |
708 | } | |
709 | } | |
710 | ||
711 | for (i = ctx->nbufs; i < ctx->nscalars; ++i) { | |
712 | rpra[i].pv = (u64) ctx->args[i].ptr; | |
713 | rpra[i].len = ctx->args[i].length; | |
714 | list[i].num = ctx->args[i].length ? 1 : 0; | |
715 | list[i].pgidx = i; | |
716 | pages[i].addr = ctx->maps[i]->phys; | |
717 | pages[i].size = ctx->maps[i]->size; | |
718 | } | |
719 | ||
720 | bail: | |
721 | if (err) | |
722 | dev_err(dev, "Error: get invoke args failed:%d\n", err); | |
723 | ||
724 | return err; | |
725 | } | |
726 | ||
727 | static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx, | |
728 | u32 kernel) | |
729 | { | |
730 | struct fastrpc_remote_arg *rpra = ctx->rpra; | |
731 | int i, inbufs; | |
732 | ||
733 | inbufs = REMOTE_SCALARS_INBUFS(ctx->sc); | |
734 | ||
735 | for (i = inbufs; i < ctx->nbufs; ++i) { | |
736 | void *src = (void *)(uintptr_t)rpra[i].pv; | |
737 | void *dst = (void *)(uintptr_t)ctx->args[i].ptr; | |
738 | u64 len = rpra[i].len; | |
739 | ||
740 | if (!kernel) { | |
741 | if (copy_to_user((void __user *)dst, src, len)) | |
742 | return -EFAULT; | |
743 | } else { | |
744 | memcpy(dst, src, len); | |
745 | } | |
746 | } | |
747 | ||
748 | return 0; | |
749 | } | |
750 | ||
751 | static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx, | |
752 | struct fastrpc_invoke_ctx *ctx, | |
753 | u32 kernel, uint32_t handle) | |
754 | { | |
755 | struct fastrpc_channel_ctx *cctx; | |
756 | struct fastrpc_user *fl = ctx->fl; | |
757 | struct fastrpc_msg *msg = &ctx->msg; | |
758 | ||
759 | cctx = fl->cctx; | |
760 | msg->pid = fl->tgid; | |
761 | msg->tid = current->pid; | |
762 | ||
763 | if (kernel) | |
764 | msg->pid = 0; | |
765 | ||
766 | msg->ctx = ctx->ctxid | fl->pd; | |
767 | msg->handle = handle; | |
768 | msg->sc = ctx->sc; | |
769 | msg->addr = ctx->buf ? ctx->buf->phys : 0; | |
770 | msg->size = roundup(ctx->msg_sz, PAGE_SIZE); | |
771 | fastrpc_context_get(ctx); | |
772 | ||
773 | return rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg)); | |
774 | } | |
775 | ||
776 | static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, | |
777 | u32 handle, u32 sc, | |
778 | struct fastrpc_invoke_args *args) | |
779 | { | |
780 | struct fastrpc_invoke_ctx *ctx = NULL; | |
781 | int err = 0; | |
782 | ||
783 | if (!fl->sctx) | |
784 | return -EINVAL; | |
785 | ||
786 | ctx = fastrpc_context_alloc(fl, kernel, sc, args); | |
787 | if (IS_ERR(ctx)) | |
788 | return PTR_ERR(ctx); | |
789 | ||
790 | if (ctx->nscalars) { | |
791 | err = fastrpc_get_args(kernel, ctx); | |
792 | if (err) | |
793 | goto bail; | |
794 | } | |
795 | /* Send invoke buffer to remote dsp */ | |
796 | err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle); | |
797 | if (err) | |
798 | goto bail; | |
799 | ||
800 | /* Wait for remote dsp to respond or time out */ | |
801 | err = wait_for_completion_interruptible(&ctx->work); | |
802 | if (err) | |
803 | goto bail; | |
804 | ||
805 | /* Check the response from remote dsp */ | |
806 | err = ctx->retval; | |
807 | if (err) | |
808 | goto bail; | |
809 | ||
810 | if (ctx->nscalars) { | |
811 | /* populate all the output buffers with results */ | |
812 | err = fastrpc_put_args(ctx, kernel); | |
813 | if (err) | |
814 | goto bail; | |
815 | } | |
816 | ||
817 | bail: | |
818 | /* We are done with this compute context, remove it from pending list */ | |
819 | spin_lock(&fl->lock); | |
820 | list_del(&ctx->node); | |
821 | spin_unlock(&fl->lock); | |
822 | fastrpc_context_put(ctx); | |
823 | ||
824 | if (err) | |
825 | dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err); | |
826 | ||
827 | return err; | |
828 | } | |
829 | ||
d73f71c7 SK |
830 | static int fastrpc_init_create_process(struct fastrpc_user *fl, |
831 | char __user *argp) | |
832 | { | |
833 | struct fastrpc_init_create init; | |
834 | struct fastrpc_invoke_args *args; | |
835 | struct fastrpc_phy_page pages[1]; | |
836 | struct fastrpc_map *map = NULL; | |
837 | struct fastrpc_buf *imem = NULL; | |
838 | int memlen; | |
839 | int err; | |
840 | struct { | |
841 | int pgid; | |
842 | u32 namelen; | |
843 | u32 filelen; | |
844 | u32 pageslen; | |
845 | u32 attrs; | |
846 | u32 siglen; | |
847 | } inbuf; | |
848 | u32 sc; | |
849 | ||
850 | args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL); | |
851 | if (!args) | |
852 | return -ENOMEM; | |
853 | ||
854 | if (copy_from_user(&init, argp, sizeof(init))) { | |
855 | err = -EFAULT; | |
856 | goto bail; | |
857 | } | |
858 | ||
859 | if (init.filelen > INIT_FILELEN_MAX) { | |
860 | err = -EINVAL; | |
861 | goto bail; | |
862 | } | |
863 | ||
864 | inbuf.pgid = fl->tgid; | |
865 | inbuf.namelen = strlen(current->comm) + 1; | |
866 | inbuf.filelen = init.filelen; | |
867 | inbuf.pageslen = 1; | |
868 | inbuf.attrs = init.attrs; | |
869 | inbuf.siglen = init.siglen; | |
870 | fl->pd = 1; | |
871 | ||
872 | if (init.filelen && init.filefd) { | |
873 | err = fastrpc_map_create(fl, init.filefd, init.filelen, &map); | |
874 | if (err) | |
875 | goto bail; | |
876 | } | |
877 | ||
878 | memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4), | |
879 | 1024 * 1024); | |
880 | err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen, | |
881 | &imem); | |
882 | if (err) { | |
883 | fastrpc_map_put(map); | |
884 | goto bail; | |
885 | } | |
886 | ||
887 | fl->init_mem = imem; | |
888 | args[0].ptr = (u64)(uintptr_t)&inbuf; | |
889 | args[0].length = sizeof(inbuf); | |
890 | args[0].fd = -1; | |
891 | ||
892 | args[1].ptr = (u64)(uintptr_t)current->comm; | |
893 | args[1].length = inbuf.namelen; | |
894 | args[1].fd = -1; | |
895 | ||
896 | args[2].ptr = (u64) init.file; | |
897 | args[2].length = inbuf.filelen; | |
898 | args[2].fd = init.filefd; | |
899 | ||
900 | pages[0].addr = imem->phys; | |
901 | pages[0].size = imem->size; | |
902 | ||
903 | args[3].ptr = (u64)(uintptr_t) pages; | |
904 | args[3].length = 1 * sizeof(*pages); | |
905 | args[3].fd = -1; | |
906 | ||
907 | args[4].ptr = (u64)(uintptr_t)&inbuf.attrs; | |
908 | args[4].length = sizeof(inbuf.attrs); | |
909 | args[4].fd = -1; | |
910 | ||
911 | args[5].ptr = (u64)(uintptr_t) &inbuf.siglen; | |
912 | args[5].length = sizeof(inbuf.siglen); | |
913 | args[5].fd = -1; | |
914 | ||
915 | sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0); | |
916 | if (init.attrs) | |
917 | sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0); | |
918 | ||
919 | err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, | |
920 | sc, args); | |
921 | ||
922 | if (err) { | |
923 | fastrpc_map_put(map); | |
924 | fastrpc_buf_free(imem); | |
925 | } | |
926 | ||
927 | bail: | |
928 | kfree(args); | |
929 | ||
930 | return err; | |
931 | } | |
932 | ||
f6f9279f SK |
933 | static struct fastrpc_session_ctx *fastrpc_session_alloc( |
934 | struct fastrpc_channel_ctx *cctx) | |
935 | { | |
936 | struct fastrpc_session_ctx *session = NULL; | |
937 | int i; | |
938 | ||
939 | spin_lock(&cctx->lock); | |
940 | for (i = 0; i < cctx->sesscount; i++) { | |
941 | if (!cctx->session[i].used && cctx->session[i].valid) { | |
942 | cctx->session[i].used = true; | |
943 | session = &cctx->session[i]; | |
944 | break; | |
945 | } | |
946 | } | |
947 | spin_unlock(&cctx->lock); | |
948 | ||
949 | return session; | |
950 | } | |
951 | ||
952 | static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx, | |
953 | struct fastrpc_session_ctx *session) | |
954 | { | |
955 | spin_lock(&cctx->lock); | |
956 | session->used = false; | |
957 | spin_unlock(&cctx->lock); | |
958 | } | |
959 | ||
d73f71c7 SK |
960 | static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl) |
961 | { | |
962 | struct fastrpc_invoke_args args[1]; | |
963 | int tgid = 0; | |
964 | u32 sc; | |
965 | ||
966 | tgid = fl->tgid; | |
967 | args[0].ptr = (u64)(uintptr_t) &tgid; | |
968 | args[0].length = sizeof(tgid); | |
969 | args[0].fd = -1; | |
970 | args[0].reserved = 0; | |
971 | sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0); | |
972 | ||
973 | return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, | |
974 | sc, &args[0]); | |
975 | } | |
976 | ||
f6f9279f SK |
977 | static int fastrpc_device_release(struct inode *inode, struct file *file) |
978 | { | |
979 | struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data; | |
980 | struct fastrpc_channel_ctx *cctx = fl->cctx; | |
c68cfb71 SK |
981 | struct fastrpc_invoke_ctx *ctx, *n; |
982 | struct fastrpc_map *map, *m; | |
f6f9279f | 983 | |
d73f71c7 SK |
984 | fastrpc_release_current_dsp_process(fl); |
985 | ||
f6f9279f SK |
986 | spin_lock(&cctx->lock); |
987 | list_del(&fl->user); | |
988 | spin_unlock(&cctx->lock); | |
989 | ||
c68cfb71 SK |
990 | if (fl->init_mem) |
991 | fastrpc_buf_free(fl->init_mem); | |
992 | ||
993 | list_for_each_entry_safe(ctx, n, &fl->pending, node) { | |
994 | list_del(&ctx->node); | |
995 | fastrpc_context_put(ctx); | |
996 | } | |
997 | ||
998 | list_for_each_entry_safe(map, m, &fl->maps, node) { | |
999 | list_del(&map->node); | |
1000 | fastrpc_map_put(map); | |
1001 | } | |
1002 | ||
f6f9279f SK |
1003 | fastrpc_session_free(cctx, fl->sctx); |
1004 | ||
1005 | mutex_destroy(&fl->mutex); | |
1006 | kfree(fl); | |
1007 | file->private_data = NULL; | |
1008 | ||
1009 | return 0; | |
1010 | } | |
1011 | ||
1012 | static int fastrpc_device_open(struct inode *inode, struct file *filp) | |
1013 | { | |
1014 | struct fastrpc_channel_ctx *cctx = miscdev_to_cctx(filp->private_data); | |
1015 | struct fastrpc_user *fl = NULL; | |
1016 | ||
1017 | fl = kzalloc(sizeof(*fl), GFP_KERNEL); | |
1018 | if (!fl) | |
1019 | return -ENOMEM; | |
1020 | ||
1021 | filp->private_data = fl; | |
1022 | spin_lock_init(&fl->lock); | |
1023 | mutex_init(&fl->mutex); | |
1024 | INIT_LIST_HEAD(&fl->pending); | |
1025 | INIT_LIST_HEAD(&fl->maps); | |
1026 | INIT_LIST_HEAD(&fl->user); | |
1027 | fl->tgid = current->tgid; | |
1028 | fl->cctx = cctx; | |
7c11df42 TE |
1029 | |
1030 | fl->sctx = fastrpc_session_alloc(cctx); | |
1031 | if (!fl->sctx) { | |
1032 | dev_err(&cctx->rpdev->dev, "No session available\n"); | |
1033 | mutex_destroy(&fl->mutex); | |
1034 | kfree(fl); | |
1035 | ||
1036 | return -EBUSY; | |
1037 | } | |
1038 | ||
f6f9279f SK |
1039 | spin_lock(&cctx->lock); |
1040 | list_add_tail(&fl->user, &cctx->users); | |
1041 | spin_unlock(&cctx->lock); | |
f6f9279f SK |
1042 | |
1043 | return 0; | |
1044 | } | |
1045 | ||
6cffd795 SK |
1046 | static int fastrpc_dmabuf_free(struct fastrpc_user *fl, char __user *argp) |
1047 | { | |
1048 | struct dma_buf *buf; | |
1049 | int info; | |
1050 | ||
1051 | if (copy_from_user(&info, argp, sizeof(info))) | |
1052 | return -EFAULT; | |
1053 | ||
1054 | buf = dma_buf_get(info); | |
1055 | if (IS_ERR_OR_NULL(buf)) | |
1056 | return -EINVAL; | |
1057 | /* | |
1058 | * one for the last get and other for the ALLOC_DMA_BUFF ioctl | |
1059 | */ | |
1060 | dma_buf_put(buf); | |
1061 | dma_buf_put(buf); | |
1062 | ||
1063 | return 0; | |
1064 | } | |
1065 | ||
1066 | static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp) | |
1067 | { | |
1068 | struct fastrpc_alloc_dma_buf bp; | |
1069 | DEFINE_DMA_BUF_EXPORT_INFO(exp_info); | |
1070 | struct fastrpc_buf *buf = NULL; | |
1071 | int err; | |
1072 | ||
1073 | if (copy_from_user(&bp, argp, sizeof(bp))) | |
1074 | return -EFAULT; | |
1075 | ||
1076 | err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf); | |
1077 | if (err) | |
1078 | return err; | |
1079 | exp_info.ops = &fastrpc_dma_buf_ops; | |
1080 | exp_info.size = bp.size; | |
1081 | exp_info.flags = O_RDWR; | |
1082 | exp_info.priv = buf; | |
1083 | buf->dmabuf = dma_buf_export(&exp_info); | |
1084 | if (IS_ERR(buf->dmabuf)) { | |
1085 | err = PTR_ERR(buf->dmabuf); | |
1086 | fastrpc_buf_free(buf); | |
1087 | return err; | |
1088 | } | |
1089 | ||
1090 | bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE); | |
1091 | if (bp.fd < 0) { | |
1092 | dma_buf_put(buf->dmabuf); | |
1093 | return -EINVAL; | |
1094 | } | |
1095 | ||
1096 | if (copy_to_user(argp, &bp, sizeof(bp))) { | |
1097 | dma_buf_put(buf->dmabuf); | |
1098 | return -EFAULT; | |
1099 | } | |
1100 | ||
1101 | get_dma_buf(buf->dmabuf); | |
1102 | ||
1103 | return 0; | |
1104 | } | |
1105 | ||
d73f71c7 SK |
1106 | static int fastrpc_init_attach(struct fastrpc_user *fl) |
1107 | { | |
1108 | struct fastrpc_invoke_args args[1]; | |
1109 | int tgid = fl->tgid; | |
1110 | u32 sc; | |
1111 | ||
1112 | args[0].ptr = (u64)(uintptr_t) &tgid; | |
1113 | args[0].length = sizeof(tgid); | |
1114 | args[0].fd = -1; | |
1115 | args[0].reserved = 0; | |
1116 | sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0); | |
1117 | fl->pd = 0; | |
1118 | ||
1119 | return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, | |
1120 | sc, &args[0]); | |
1121 | } | |
1122 | ||
c68cfb71 SK |
1123 | static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp) |
1124 | { | |
1125 | struct fastrpc_invoke_args *args = NULL; | |
1126 | struct fastrpc_invoke inv; | |
1127 | u32 nscalars; | |
1128 | int err; | |
1129 | ||
1130 | if (copy_from_user(&inv, argp, sizeof(inv))) | |
1131 | return -EFAULT; | |
1132 | ||
1133 | /* nscalars is truncated here to max supported value */ | |
1134 | nscalars = REMOTE_SCALARS_LENGTH(inv.sc); | |
1135 | if (nscalars) { | |
1136 | args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL); | |
1137 | if (!args) | |
1138 | return -ENOMEM; | |
1139 | ||
1140 | if (copy_from_user(args, (void __user *)(uintptr_t)inv.args, | |
1141 | nscalars * sizeof(*args))) { | |
1142 | kfree(args); | |
1143 | return -EFAULT; | |
1144 | } | |
1145 | } | |
1146 | ||
1147 | err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args); | |
1148 | kfree(args); | |
1149 | ||
1150 | return err; | |
1151 | } | |
1152 | ||
1153 | static long fastrpc_device_ioctl(struct file *file, unsigned int cmd, | |
1154 | unsigned long arg) | |
1155 | { | |
1156 | struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data; | |
1157 | char __user *argp = (char __user *)arg; | |
1158 | int err; | |
1159 | ||
1160 | switch (cmd) { | |
1161 | case FASTRPC_IOCTL_INVOKE: | |
1162 | err = fastrpc_invoke(fl, argp); | |
1163 | break; | |
d73f71c7 SK |
1164 | case FASTRPC_IOCTL_INIT_ATTACH: |
1165 | err = fastrpc_init_attach(fl); | |
1166 | break; | |
1167 | case FASTRPC_IOCTL_INIT_CREATE: | |
1168 | err = fastrpc_init_create_process(fl, argp); | |
1169 | break; | |
6cffd795 SK |
1170 | case FASTRPC_IOCTL_FREE_DMA_BUFF: |
1171 | err = fastrpc_dmabuf_free(fl, argp); | |
1172 | break; | |
1173 | case FASTRPC_IOCTL_ALLOC_DMA_BUFF: | |
1174 | err = fastrpc_dmabuf_alloc(fl, argp); | |
1175 | break; | |
c68cfb71 SK |
1176 | default: |
1177 | err = -ENOTTY; | |
1178 | break; | |
1179 | } | |
1180 | ||
1181 | return err; | |
1182 | } | |
1183 | ||
f6f9279f SK |
1184 | static const struct file_operations fastrpc_fops = { |
1185 | .open = fastrpc_device_open, | |
1186 | .release = fastrpc_device_release, | |
c68cfb71 SK |
1187 | .unlocked_ioctl = fastrpc_device_ioctl, |
1188 | .compat_ioctl = fastrpc_device_ioctl, | |
f6f9279f SK |
1189 | }; |
1190 | ||
1191 | static int fastrpc_cb_probe(struct platform_device *pdev) | |
1192 | { | |
1193 | struct fastrpc_channel_ctx *cctx; | |
1194 | struct fastrpc_session_ctx *sess; | |
1195 | struct device *dev = &pdev->dev; | |
1196 | int i, sessions = 0; | |
1197 | ||
1198 | cctx = dev_get_drvdata(dev->parent); | |
1199 | if (!cctx) | |
1200 | return -EINVAL; | |
1201 | ||
1202 | of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions); | |
1203 | ||
1204 | spin_lock(&cctx->lock); | |
1205 | sess = &cctx->session[cctx->sesscount]; | |
1206 | sess->used = false; | |
1207 | sess->valid = true; | |
1208 | sess->dev = dev; | |
1209 | dev_set_drvdata(dev, sess); | |
1210 | ||
1211 | if (of_property_read_u32(dev->of_node, "reg", &sess->sid)) | |
1212 | dev_info(dev, "FastRPC Session ID not specified in DT\n"); | |
1213 | ||
1214 | if (sessions > 0) { | |
1215 | struct fastrpc_session_ctx *dup_sess; | |
1216 | ||
1217 | for (i = 1; i < sessions; i++) { | |
1218 | if (cctx->sesscount++ >= FASTRPC_MAX_SESSIONS) | |
1219 | break; | |
1220 | dup_sess = &cctx->session[cctx->sesscount]; | |
1221 | memcpy(dup_sess, sess, sizeof(*dup_sess)); | |
1222 | } | |
1223 | } | |
1224 | cctx->sesscount++; | |
1225 | spin_unlock(&cctx->lock); | |
1226 | dma_set_mask(dev, DMA_BIT_MASK(32)); | |
1227 | ||
1228 | return 0; | |
1229 | } | |
1230 | ||
1231 | static int fastrpc_cb_remove(struct platform_device *pdev) | |
1232 | { | |
1233 | struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent); | |
1234 | struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev); | |
1235 | int i; | |
1236 | ||
1237 | spin_lock(&cctx->lock); | |
1238 | for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) { | |
1239 | if (cctx->session[i].sid == sess->sid) { | |
1240 | cctx->session[i].valid = false; | |
1241 | cctx->sesscount--; | |
1242 | } | |
1243 | } | |
1244 | spin_unlock(&cctx->lock); | |
1245 | ||
1246 | return 0; | |
1247 | } | |
1248 | ||
1249 | static const struct of_device_id fastrpc_match_table[] = { | |
1250 | { .compatible = "qcom,fastrpc-compute-cb", }, | |
1251 | {} | |
1252 | }; | |
1253 | ||
1254 | static struct platform_driver fastrpc_cb_driver = { | |
1255 | .probe = fastrpc_cb_probe, | |
1256 | .remove = fastrpc_cb_remove, | |
1257 | .driver = { | |
1258 | .name = "qcom,fastrpc-cb", | |
1259 | .of_match_table = fastrpc_match_table, | |
1260 | .suppress_bind_attrs = true, | |
1261 | }, | |
1262 | }; | |
1263 | ||
1264 | static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) | |
1265 | { | |
1266 | struct device *rdev = &rpdev->dev; | |
1267 | struct fastrpc_channel_ctx *data; | |
1268 | int i, err, domain_id = -1; | |
1269 | const char *domain; | |
1270 | ||
1271 | data = devm_kzalloc(rdev, sizeof(*data), GFP_KERNEL); | |
1272 | if (!data) | |
1273 | return -ENOMEM; | |
1274 | ||
1275 | err = of_property_read_string(rdev->of_node, "label", &domain); | |
1276 | if (err) { | |
1277 | dev_info(rdev, "FastRPC Domain not specified in DT\n"); | |
1278 | return err; | |
1279 | } | |
1280 | ||
1281 | for (i = 0; i <= CDSP_DOMAIN_ID; i++) { | |
1282 | if (!strcmp(domains[i], domain)) { | |
1283 | domain_id = i; | |
1284 | break; | |
1285 | } | |
1286 | } | |
1287 | ||
1288 | if (domain_id < 0) { | |
1289 | dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id); | |
1290 | return -EINVAL; | |
1291 | } | |
1292 | ||
1293 | data->miscdev.minor = MISC_DYNAMIC_MINOR; | |
1294 | data->miscdev.name = kasprintf(GFP_KERNEL, "fastrpc-%s", | |
1295 | domains[domain_id]); | |
1296 | data->miscdev.fops = &fastrpc_fops; | |
1297 | err = misc_register(&data->miscdev); | |
1298 | if (err) | |
1299 | return err; | |
1300 | ||
1301 | dev_set_drvdata(&rpdev->dev, data); | |
1302 | dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32)); | |
1303 | INIT_LIST_HEAD(&data->users); | |
1304 | spin_lock_init(&data->lock); | |
1305 | idr_init(&data->ctx_idr); | |
1306 | data->domain_id = domain_id; | |
1307 | data->rpdev = rpdev; | |
1308 | ||
1309 | return of_platform_populate(rdev->of_node, NULL, NULL, rdev); | |
1310 | } | |
1311 | ||
c68cfb71 SK |
1312 | static void fastrpc_notify_users(struct fastrpc_user *user) |
1313 | { | |
1314 | struct fastrpc_invoke_ctx *ctx; | |
1315 | ||
1316 | spin_lock(&user->lock); | |
1317 | list_for_each_entry(ctx, &user->pending, node) | |
1318 | complete(&ctx->work); | |
1319 | spin_unlock(&user->lock); | |
1320 | } | |
1321 | ||
f6f9279f SK |
1322 | static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev) |
1323 | { | |
1324 | struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev); | |
c68cfb71 SK |
1325 | struct fastrpc_user *user; |
1326 | ||
1327 | spin_lock(&cctx->lock); | |
1328 | list_for_each_entry(user, &cctx->users, user) | |
1329 | fastrpc_notify_users(user); | |
1330 | spin_unlock(&cctx->lock); | |
f6f9279f SK |
1331 | |
1332 | misc_deregister(&cctx->miscdev); | |
1333 | of_platform_depopulate(&rpdev->dev); | |
1334 | kfree(cctx); | |
1335 | } | |
1336 | ||
1337 | static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data, | |
1338 | int len, void *priv, u32 addr) | |
1339 | { | |
c68cfb71 SK |
1340 | struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev); |
1341 | struct fastrpc_invoke_rsp *rsp = data; | |
1342 | struct fastrpc_invoke_ctx *ctx; | |
1343 | unsigned long flags; | |
1344 | unsigned long ctxid; | |
1345 | ||
1346 | if (len < sizeof(*rsp)) | |
1347 | return -EINVAL; | |
1348 | ||
1349 | ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4); | |
1350 | ||
1351 | spin_lock_irqsave(&cctx->lock, flags); | |
1352 | ctx = idr_find(&cctx->ctx_idr, ctxid); | |
1353 | spin_unlock_irqrestore(&cctx->lock, flags); | |
1354 | ||
1355 | if (!ctx) { | |
1356 | dev_err(&rpdev->dev, "No context ID matches response\n"); | |
1357 | return -ENOENT; | |
1358 | } | |
1359 | ||
1360 | ctx->retval = rsp->retval; | |
1361 | complete(&ctx->work); | |
8e7389c7 TE |
1362 | |
1363 | /* | |
1364 | * The DMA buffer associated with the context cannot be freed in | |
1365 | * interrupt context so schedule it through a worker thread to | |
1366 | * avoid a kernel BUG. | |
1367 | */ | |
1368 | schedule_work(&ctx->put_work); | |
c68cfb71 | 1369 | |
f6f9279f SK |
1370 | return 0; |
1371 | } | |
1372 | ||
1373 | static const struct of_device_id fastrpc_rpmsg_of_match[] = { | |
1374 | { .compatible = "qcom,fastrpc" }, | |
1375 | { }, | |
1376 | }; | |
1377 | MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match); | |
1378 | ||
1379 | static struct rpmsg_driver fastrpc_driver = { | |
1380 | .probe = fastrpc_rpmsg_probe, | |
1381 | .remove = fastrpc_rpmsg_remove, | |
1382 | .callback = fastrpc_rpmsg_callback, | |
1383 | .drv = { | |
1384 | .name = "qcom,fastrpc", | |
1385 | .of_match_table = fastrpc_rpmsg_of_match, | |
1386 | }, | |
1387 | }; | |
1388 | ||
1389 | static int fastrpc_init(void) | |
1390 | { | |
1391 | int ret; | |
1392 | ||
1393 | ret = platform_driver_register(&fastrpc_cb_driver); | |
1394 | if (ret < 0) { | |
1395 | pr_err("fastrpc: failed to register cb driver\n"); | |
1396 | return ret; | |
1397 | } | |
1398 | ||
1399 | ret = register_rpmsg_driver(&fastrpc_driver); | |
1400 | if (ret < 0) { | |
1401 | pr_err("fastrpc: failed to register rpmsg driver\n"); | |
1402 | platform_driver_unregister(&fastrpc_cb_driver); | |
1403 | return ret; | |
1404 | } | |
1405 | ||
1406 | return 0; | |
1407 | } | |
1408 | module_init(fastrpc_init); | |
1409 | ||
1410 | static void fastrpc_exit(void) | |
1411 | { | |
1412 | platform_driver_unregister(&fastrpc_cb_driver); | |
1413 | unregister_rpmsg_driver(&fastrpc_driver); | |
1414 | } | |
1415 | module_exit(fastrpc_exit); | |
1416 | ||
1417 | MODULE_LICENSE("GPL v2"); |