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