blob: 59ee6de262291df918049247ad5fe2fb0c528236 [file] [log] [blame]
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
3// Copyright (c) 2018, Linaro Limited
4
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00005#include <linux/completion.h>
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00006#include <linux/device.h>
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00007#include <linux/dma-buf.h>
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00008#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>
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +000015#include <linux/sort.h>
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +000016#include <linux/of_platform.h>
17#include <linux/rpmsg.h>
18#include <linux/scatterlist.h>
19#include <linux/slab.h>
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +000020#include <uapi/misc/fastrpc.h>
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +000021
22#define ADSP_DOMAIN_ID (0)
23#define MDSP_DOMAIN_ID (1)
24#define SDSP_DOMAIN_ID (2)
25#define CDSP_DOMAIN_ID (3)
26#define FASTRPC_DEV_MAX 4 /* adsp, mdsp, slpi, cdsp*/
27#define FASTRPC_MAX_SESSIONS 9 /*8 compute, 1 cpz*/
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +000028#define FASTRPC_ALIGN 128
29#define FASTRPC_MAX_FDLIST 16
30#define FASTRPC_MAX_CRCLIST 64
31#define FASTRPC_PHYS(p) ((p) & 0xffffffff)
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +000032#define FASTRPC_CTX_MAX (256)
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +000033#define FASTRPC_INIT_HANDLE 1
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +000034#define FASTRPC_CTXID_MASK (0xFF0)
Srinivas Kandagatlaf1cf11c2019-03-07 10:12:29 +000035#define INIT_FILELEN_MAX (64 * 1024 * 1024)
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +000036#define INIT_MEMLEN_MAX (8 * 1024 * 1024)
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +000037#define FASTRPC_DEVICE_NAME "fastrpc"
38
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +000039/* Retrives number of input buffers from the scalars parameter */
40#define REMOTE_SCALARS_INBUFS(sc) (((sc) >> 16) & 0x0ff)
41
42/* Retrives number of output buffers from the scalars parameter */
43#define REMOTE_SCALARS_OUTBUFS(sc) (((sc) >> 8) & 0x0ff)
44
45/* Retrives number of input handles from the scalars parameter */
46#define REMOTE_SCALARS_INHANDLES(sc) (((sc) >> 4) & 0x0f)
47
48/* Retrives number of output handles from the scalars parameter */
49#define REMOTE_SCALARS_OUTHANDLES(sc) ((sc) & 0x0f)
50
51#define REMOTE_SCALARS_LENGTH(sc) (REMOTE_SCALARS_INBUFS(sc) + \
52 REMOTE_SCALARS_OUTBUFS(sc) + \
53 REMOTE_SCALARS_INHANDLES(sc)+ \
54 REMOTE_SCALARS_OUTHANDLES(sc))
55#define FASTRPC_BUILD_SCALARS(attr, method, in, out, oin, oout) \
56 (((attr & 0x07) << 29) | \
57 ((method & 0x1f) << 24) | \
58 ((in & 0xff) << 16) | \
59 ((out & 0xff) << 8) | \
60 ((oin & 0x0f) << 4) | \
61 (oout & 0x0f))
62
63#define FASTRPC_SCALARS(method, in, out) \
64 FASTRPC_BUILD_SCALARS(0, method, in, out, 0, 0)
65
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +000066#define FASTRPC_CREATE_PROCESS_NARGS 6
67/* Remote Method id table */
68#define FASTRPC_RMID_INIT_ATTACH 0
69#define FASTRPC_RMID_INIT_RELEASE 1
70#define FASTRPC_RMID_INIT_CREATE 6
71#define FASTRPC_RMID_INIT_CREATE_ATTR 7
72#define FASTRPC_RMID_INIT_CREATE_STATIC 8
73
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +000074#define miscdev_to_cctx(d) container_of(d, struct fastrpc_channel_ctx, miscdev)
75
76static const char *domains[FASTRPC_DEV_MAX] = { "adsp", "mdsp",
77 "sdsp", "cdsp"};
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +000078struct fastrpc_phy_page {
79 u64 addr; /* physical address */
80 u64 size; /* size of contiguous region */
81};
82
83struct fastrpc_invoke_buf {
84 u32 num; /* number of contiguous regions */
85 u32 pgidx; /* index to start of contiguous region */
86};
87
88struct fastrpc_remote_arg {
89 u64 pv;
90 u64 len;
91};
92
93struct fastrpc_msg {
94 int pid; /* process group id */
95 int tid; /* thread id */
96 u64 ctx; /* invoke caller context */
97 u32 handle; /* handle to invoke */
98 u32 sc; /* scalars structure describing the data */
99 u64 addr; /* physical address */
100 u64 size; /* size of contiguous region */
101};
102
103struct fastrpc_invoke_rsp {
104 u64 ctx; /* invoke caller context */
105 int retval; /* invoke return value */
106};
107
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000108struct fastrpc_buf_overlap {
109 u64 start;
110 u64 end;
111 int raix;
112 u64 mstart;
113 u64 mend;
114 u64 offset;
115};
116
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000117struct fastrpc_buf {
118 struct fastrpc_user *fl;
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +0000119 struct dma_buf *dmabuf;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000120 struct device *dev;
121 void *virt;
122 u64 phys;
123 u64 size;
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +0000124 /* Lock for dma buf attachments */
125 struct mutex lock;
126 struct list_head attachments;
127};
128
129struct fastrpc_dma_buf_attachment {
130 struct device *dev;
131 struct sg_table sgt;
132 struct list_head node;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000133};
134
135struct fastrpc_map {
136 struct list_head node;
137 struct fastrpc_user *fl;
138 int fd;
139 struct dma_buf *buf;
140 struct sg_table *table;
141 struct dma_buf_attachment *attach;
142 u64 phys;
143 u64 size;
144 void *va;
145 u64 len;
146 struct kref refcount;
147};
148
149struct fastrpc_invoke_ctx {
150 int nscalars;
151 int nbufs;
152 int retval;
153 int pid;
154 int tgid;
155 u32 sc;
156 u32 *crc;
157 u64 ctxid;
158 u64 msg_sz;
159 struct kref refcount;
160 struct list_head node; /* list of ctxs */
161 struct completion work;
Thierry Escande8e7389c2019-03-07 10:12:22 +0000162 struct work_struct put_work;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000163 struct fastrpc_msg msg;
164 struct fastrpc_user *fl;
165 struct fastrpc_remote_arg *rpra;
166 struct fastrpc_map **maps;
167 struct fastrpc_buf *buf;
168 struct fastrpc_invoke_args *args;
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000169 struct fastrpc_buf_overlap *olaps;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000170 struct fastrpc_channel_ctx *cctx;
171};
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +0000172
173struct fastrpc_session_ctx {
174 struct device *dev;
175 int sid;
176 bool used;
177 bool valid;
178};
179
180struct fastrpc_channel_ctx {
181 int domain_id;
182 int sesscount;
183 struct rpmsg_device *rpdev;
184 struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS];
185 spinlock_t lock;
186 struct idr ctx_idr;
187 struct list_head users;
188 struct miscdevice miscdev;
Bjorn Andersson278d56f2019-08-29 10:29:22 +0100189 struct kref refcount;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +0000190};
191
192struct fastrpc_user {
193 struct list_head user;
194 struct list_head maps;
195 struct list_head pending;
196
197 struct fastrpc_channel_ctx *cctx;
198 struct fastrpc_session_ctx *sctx;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000199 struct fastrpc_buf *init_mem;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +0000200
201 int tgid;
202 int pd;
203 /* Lock for lists */
204 spinlock_t lock;
205 /* lock for allocations */
206 struct mutex mutex;
207};
208
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000209static void fastrpc_free_map(struct kref *ref)
210{
211 struct fastrpc_map *map;
212
213 map = container_of(ref, struct fastrpc_map, refcount);
214
215 if (map->table) {
216 dma_buf_unmap_attachment(map->attach, map->table,
217 DMA_BIDIRECTIONAL);
218 dma_buf_detach(map->buf, map->attach);
219 dma_buf_put(map->buf);
220 }
221
222 kfree(map);
223}
224
225static void fastrpc_map_put(struct fastrpc_map *map)
226{
227 if (map)
228 kref_put(&map->refcount, fastrpc_free_map);
229}
230
231static void fastrpc_map_get(struct fastrpc_map *map)
232{
233 if (map)
234 kref_get(&map->refcount);
235}
236
237static int fastrpc_map_find(struct fastrpc_user *fl, int fd,
238 struct fastrpc_map **ppmap)
239{
240 struct fastrpc_map *map = NULL;
241
242 mutex_lock(&fl->mutex);
243 list_for_each_entry(map, &fl->maps, node) {
244 if (map->fd == fd) {
245 fastrpc_map_get(map);
246 *ppmap = map;
247 mutex_unlock(&fl->mutex);
248 return 0;
249 }
250 }
251 mutex_unlock(&fl->mutex);
252
253 return -ENOENT;
254}
255
256static void fastrpc_buf_free(struct fastrpc_buf *buf)
257{
258 dma_free_coherent(buf->dev, buf->size, buf->virt,
259 FASTRPC_PHYS(buf->phys));
260 kfree(buf);
261}
262
263static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
264 u64 size, struct fastrpc_buf **obuf)
265{
266 struct fastrpc_buf *buf;
267
268 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
269 if (!buf)
270 return -ENOMEM;
271
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +0000272 INIT_LIST_HEAD(&buf->attachments);
273 mutex_init(&buf->lock);
274
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000275 buf->fl = fl;
276 buf->virt = NULL;
277 buf->phys = 0;
278 buf->size = size;
279 buf->dev = dev;
280
281 buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys,
282 GFP_KERNEL);
Jorge Ramirez-Ortiz41db5f82019-07-05 10:13:03 +0200283 if (!buf->virt) {
284 mutex_destroy(&buf->lock);
285 kfree(buf);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000286 return -ENOMEM;
Jorge Ramirez-Ortiz41db5f82019-07-05 10:13:03 +0200287 }
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000288
289 if (fl->sctx && fl->sctx->sid)
290 buf->phys += ((u64)fl->sctx->sid << 32);
291
292 *obuf = buf;
293
294 return 0;
295}
296
Bjorn Andersson278d56f2019-08-29 10:29:22 +0100297static void fastrpc_channel_ctx_free(struct kref *ref)
298{
299 struct fastrpc_channel_ctx *cctx;
300
301 cctx = container_of(ref, struct fastrpc_channel_ctx, refcount);
302
303 kfree(cctx);
304}
305
306static void fastrpc_channel_ctx_get(struct fastrpc_channel_ctx *cctx)
307{
308 kref_get(&cctx->refcount);
309}
310
311static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx)
312{
313 kref_put(&cctx->refcount, fastrpc_channel_ctx_free);
314}
315
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000316static void fastrpc_context_free(struct kref *ref)
317{
318 struct fastrpc_invoke_ctx *ctx;
319 struct fastrpc_channel_ctx *cctx;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000320 unsigned long flags;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000321 int i;
322
323 ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
324 cctx = ctx->cctx;
325
326 for (i = 0; i < ctx->nscalars; i++)
327 fastrpc_map_put(ctx->maps[i]);
328
329 if (ctx->buf)
330 fastrpc_buf_free(ctx->buf);
331
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000332 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000333 idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4);
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000334 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000335
336 kfree(ctx->maps);
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000337 kfree(ctx->olaps);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000338 kfree(ctx);
Bjorn Andersson278d56f2019-08-29 10:29:22 +0100339
340 fastrpc_channel_ctx_put(cctx);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000341}
342
343static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx)
344{
345 kref_get(&ctx->refcount);
346}
347
348static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx)
349{
350 kref_put(&ctx->refcount, fastrpc_context_free);
351}
352
Thierry Escande8e7389c2019-03-07 10:12:22 +0000353static void fastrpc_context_put_wq(struct work_struct *work)
354{
355 struct fastrpc_invoke_ctx *ctx =
356 container_of(work, struct fastrpc_invoke_ctx, put_work);
357
358 fastrpc_context_put(ctx);
359}
360
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000361#define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)
362static int olaps_cmp(const void *a, const void *b)
363{
364 struct fastrpc_buf_overlap *pa = (struct fastrpc_buf_overlap *)a;
365 struct fastrpc_buf_overlap *pb = (struct fastrpc_buf_overlap *)b;
366 /* sort with lowest starting buffer first */
367 int st = CMP(pa->start, pb->start);
368 /* sort with highest ending buffer first */
369 int ed = CMP(pb->end, pa->end);
370
371 return st == 0 ? ed : st;
372}
373
374static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
375{
376 u64 max_end = 0;
377 int i;
378
379 for (i = 0; i < ctx->nbufs; ++i) {
380 ctx->olaps[i].start = ctx->args[i].ptr;
381 ctx->olaps[i].end = ctx->olaps[i].start + ctx->args[i].length;
382 ctx->olaps[i].raix = i;
383 }
384
385 sort(ctx->olaps, ctx->nbufs, sizeof(*ctx->olaps), olaps_cmp, NULL);
386
387 for (i = 0; i < ctx->nbufs; ++i) {
388 /* Falling inside previous range */
389 if (ctx->olaps[i].start < max_end) {
390 ctx->olaps[i].mstart = max_end;
391 ctx->olaps[i].mend = ctx->olaps[i].end;
392 ctx->olaps[i].offset = max_end - ctx->olaps[i].start;
393
394 if (ctx->olaps[i].end > max_end) {
395 max_end = ctx->olaps[i].end;
396 } else {
397 ctx->olaps[i].mend = 0;
398 ctx->olaps[i].mstart = 0;
399 }
400
401 } else {
402 ctx->olaps[i].mend = ctx->olaps[i].end;
403 ctx->olaps[i].mstart = ctx->olaps[i].start;
404 ctx->olaps[i].offset = 0;
405 max_end = ctx->olaps[i].end;
406 }
407 }
408}
409
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000410static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
411 struct fastrpc_user *user, u32 kernel, u32 sc,
412 struct fastrpc_invoke_args *args)
413{
414 struct fastrpc_channel_ctx *cctx = user->cctx;
415 struct fastrpc_invoke_ctx *ctx = NULL;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000416 unsigned long flags;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000417 int ret;
418
419 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
420 if (!ctx)
421 return ERR_PTR(-ENOMEM);
422
423 INIT_LIST_HEAD(&ctx->node);
424 ctx->fl = user;
425 ctx->nscalars = REMOTE_SCALARS_LENGTH(sc);
426 ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) +
427 REMOTE_SCALARS_OUTBUFS(sc);
428
429 if (ctx->nscalars) {
430 ctx->maps = kcalloc(ctx->nscalars,
431 sizeof(*ctx->maps), GFP_KERNEL);
432 if (!ctx->maps) {
433 kfree(ctx);
434 return ERR_PTR(-ENOMEM);
435 }
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000436 ctx->olaps = kcalloc(ctx->nscalars,
437 sizeof(*ctx->olaps), GFP_KERNEL);
438 if (!ctx->olaps) {
439 kfree(ctx->maps);
440 kfree(ctx);
441 return ERR_PTR(-ENOMEM);
442 }
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000443 ctx->args = args;
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000444 fastrpc_get_buff_overlaps(ctx);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000445 }
446
Bjorn Andersson278d56f2019-08-29 10:29:22 +0100447 /* Released in fastrpc_context_put() */
448 fastrpc_channel_ctx_get(cctx);
449
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000450 ctx->sc = sc;
451 ctx->retval = -1;
452 ctx->pid = current->pid;
453 ctx->tgid = user->tgid;
454 ctx->cctx = cctx;
455 init_completion(&ctx->work);
Thierry Escande8e7389c2019-03-07 10:12:22 +0000456 INIT_WORK(&ctx->put_work, fastrpc_context_put_wq);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000457
458 spin_lock(&user->lock);
459 list_add_tail(&ctx->node, &user->pending);
460 spin_unlock(&user->lock);
461
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000462 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000463 ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1,
464 FASTRPC_CTX_MAX, GFP_ATOMIC);
465 if (ret < 0) {
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000466 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000467 goto err_idr;
468 }
469 ctx->ctxid = ret << 4;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +0000470 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000471
472 kref_init(&ctx->refcount);
473
474 return ctx;
475err_idr:
476 spin_lock(&user->lock);
477 list_del(&ctx->node);
478 spin_unlock(&user->lock);
Bjorn Andersson278d56f2019-08-29 10:29:22 +0100479 fastrpc_channel_ctx_put(cctx);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000480 kfree(ctx->maps);
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000481 kfree(ctx->olaps);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000482 kfree(ctx);
483
484 return ERR_PTR(ret);
485}
486
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +0000487static struct sg_table *
488fastrpc_map_dma_buf(struct dma_buf_attachment *attachment,
489 enum dma_data_direction dir)
490{
491 struct fastrpc_dma_buf_attachment *a = attachment->priv;
492 struct sg_table *table;
493
494 table = &a->sgt;
495
496 if (!dma_map_sg(attachment->dev, table->sgl, table->nents, dir))
497 return ERR_PTR(-ENOMEM);
498
499 return table;
500}
501
502static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach,
503 struct sg_table *table,
504 enum dma_data_direction dir)
505{
506 dma_unmap_sg(attach->dev, table->sgl, table->nents, dir);
507}
508
509static void fastrpc_release(struct dma_buf *dmabuf)
510{
511 struct fastrpc_buf *buffer = dmabuf->priv;
512
513 fastrpc_buf_free(buffer);
514}
515
516static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf,
517 struct dma_buf_attachment *attachment)
518{
519 struct fastrpc_dma_buf_attachment *a;
520 struct fastrpc_buf *buffer = dmabuf->priv;
521 int ret;
522
523 a = kzalloc(sizeof(*a), GFP_KERNEL);
524 if (!a)
525 return -ENOMEM;
526
527 ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt,
528 FASTRPC_PHYS(buffer->phys), buffer->size);
529 if (ret < 0) {
530 dev_err(buffer->dev, "failed to get scatterlist from DMA API\n");
531 return -EINVAL;
532 }
533
534 a->dev = attachment->dev;
535 INIT_LIST_HEAD(&a->node);
536 attachment->priv = a;
537
538 mutex_lock(&buffer->lock);
539 list_add(&a->node, &buffer->attachments);
540 mutex_unlock(&buffer->lock);
541
542 return 0;
543}
544
545static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf,
546 struct dma_buf_attachment *attachment)
547{
548 struct fastrpc_dma_buf_attachment *a = attachment->priv;
549 struct fastrpc_buf *buffer = dmabuf->priv;
550
551 mutex_lock(&buffer->lock);
552 list_del(&a->node);
553 mutex_unlock(&buffer->lock);
554 kfree(a);
555}
556
557static void *fastrpc_kmap(struct dma_buf *dmabuf, unsigned long pgnum)
558{
559 struct fastrpc_buf *buf = dmabuf->priv;
560
561 return buf->virt ? buf->virt + pgnum * PAGE_SIZE : NULL;
562}
563
564static void *fastrpc_vmap(struct dma_buf *dmabuf)
565{
566 struct fastrpc_buf *buf = dmabuf->priv;
567
568 return buf->virt;
569}
570
571static int fastrpc_mmap(struct dma_buf *dmabuf,
572 struct vm_area_struct *vma)
573{
574 struct fastrpc_buf *buf = dmabuf->priv;
575 size_t size = vma->vm_end - vma->vm_start;
576
577 return dma_mmap_coherent(buf->dev, vma, buf->virt,
578 FASTRPC_PHYS(buf->phys), size);
579}
580
581static const struct dma_buf_ops fastrpc_dma_buf_ops = {
582 .attach = fastrpc_dma_buf_attach,
583 .detach = fastrpc_dma_buf_detatch,
584 .map_dma_buf = fastrpc_map_dma_buf,
585 .unmap_dma_buf = fastrpc_unmap_dma_buf,
586 .mmap = fastrpc_mmap,
587 .map = fastrpc_kmap,
588 .vmap = fastrpc_vmap,
589 .release = fastrpc_release,
590};
591
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000592static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
593 u64 len, struct fastrpc_map **ppmap)
594{
595 struct fastrpc_session_ctx *sess = fl->sctx;
596 struct fastrpc_map *map = NULL;
597 int err = 0;
598
599 if (!fastrpc_map_find(fl, fd, ppmap))
600 return 0;
601
602 map = kzalloc(sizeof(*map), GFP_KERNEL);
603 if (!map)
604 return -ENOMEM;
605
606 INIT_LIST_HEAD(&map->node);
607 map->fl = fl;
608 map->fd = fd;
609 map->buf = dma_buf_get(fd);
Wei Yongjun682a6042019-02-16 01:35:43 +0000610 if (IS_ERR(map->buf)) {
611 err = PTR_ERR(map->buf);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000612 goto get_err;
613 }
614
615 map->attach = dma_buf_attach(map->buf, sess->dev);
616 if (IS_ERR(map->attach)) {
617 dev_err(sess->dev, "Failed to attach dmabuf\n");
618 err = PTR_ERR(map->attach);
619 goto attach_err;
620 }
621
622 map->table = dma_buf_map_attachment(map->attach, DMA_BIDIRECTIONAL);
623 if (IS_ERR(map->table)) {
624 err = PTR_ERR(map->table);
625 goto map_err;
626 }
627
628 map->phys = sg_dma_address(map->table->sgl);
629 map->phys += ((u64)fl->sctx->sid << 32);
630 map->size = len;
631 map->va = sg_virt(map->table->sgl);
632 map->len = len;
633 kref_init(&map->refcount);
634
635 spin_lock(&fl->lock);
636 list_add_tail(&map->node, &fl->maps);
637 spin_unlock(&fl->lock);
638 *ppmap = map;
639
640 return 0;
641
642map_err:
643 dma_buf_detach(map->buf, map->attach);
644attach_err:
645 dma_buf_put(map->buf);
646get_err:
647 kfree(map);
648
649 return err;
650}
651
652/*
653 * Fastrpc payload buffer with metadata looks like:
654 *
655 * >>>>>> START of METADATA <<<<<<<<<
656 * +---------------------------------+
657 * | Arguments |
658 * | type:(struct fastrpc_remote_arg)|
659 * | (0 - N) |
660 * +---------------------------------+
661 * | Invoke Buffer list |
662 * | type:(struct fastrpc_invoke_buf)|
663 * | (0 - N) |
664 * +---------------------------------+
665 * | Page info list |
666 * | type:(struct fastrpc_phy_page) |
667 * | (0 - N) |
668 * +---------------------------------+
669 * | Optional info |
670 * |(can be specific to SoC/Firmware)|
671 * +---------------------------------+
672 * >>>>>>>> END of METADATA <<<<<<<<<
673 * +---------------------------------+
674 * | Inline ARGS |
675 * | (0-N) |
676 * +---------------------------------+
677 */
678
679static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx)
680{
681 int size = 0;
682
683 size = (sizeof(struct fastrpc_remote_arg) +
684 sizeof(struct fastrpc_invoke_buf) +
685 sizeof(struct fastrpc_phy_page)) * ctx->nscalars +
686 sizeof(u64) * FASTRPC_MAX_FDLIST +
687 sizeof(u32) * FASTRPC_MAX_CRCLIST;
688
689 return size;
690}
691
692static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen)
693{
694 u64 size = 0;
695 int i;
696
697 size = ALIGN(metalen, FASTRPC_ALIGN);
698 for (i = 0; i < ctx->nscalars; i++) {
699 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) {
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000700
701 if (ctx->olaps[i].offset == 0)
702 size = ALIGN(size, FASTRPC_ALIGN);
703
704 size += (ctx->olaps[i].mend - ctx->olaps[i].mstart);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000705 }
706 }
707
708 return size;
709}
710
711static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
712{
713 struct device *dev = ctx->fl->sctx->dev;
714 int i, err;
715
716 for (i = 0; i < ctx->nscalars; ++i) {
717 /* Make sure reserved field is set to 0 */
718 if (ctx->args[i].reserved)
719 return -EINVAL;
720
721 if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 ||
722 ctx->args[i].length == 0)
723 continue;
724
725 err = fastrpc_map_create(ctx->fl, ctx->args[i].fd,
726 ctx->args[i].length, &ctx->maps[i]);
727 if (err) {
728 dev_err(dev, "Error Creating map %d\n", err);
729 return -EINVAL;
730 }
731
732 }
733 return 0;
734}
735
736static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
737{
738 struct device *dev = ctx->fl->sctx->dev;
739 struct fastrpc_remote_arg *rpra;
740 struct fastrpc_invoke_buf *list;
741 struct fastrpc_phy_page *pages;
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000742 int inbufs, i, oix, err = 0;
743 u64 len, rlen, pkt_size;
Srinivas Kandagatla02b45b42019-03-07 10:12:28 +0000744 u64 pg_start, pg_end;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000745 uintptr_t args;
746 int metalen;
747
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000748 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
749 metalen = fastrpc_get_meta_size(ctx);
750 pkt_size = fastrpc_get_payload_size(ctx, metalen);
751
752 err = fastrpc_create_maps(ctx);
753 if (err)
754 return err;
755
756 ctx->msg_sz = pkt_size;
757
758 err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf);
759 if (err)
760 return err;
761
762 rpra = ctx->buf->virt;
763 list = ctx->buf->virt + ctx->nscalars * sizeof(*rpra);
764 pages = ctx->buf->virt + ctx->nscalars * (sizeof(*list) +
765 sizeof(*rpra));
766 args = (uintptr_t)ctx->buf->virt + metalen;
767 rlen = pkt_size - metalen;
768 ctx->rpra = rpra;
769
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000770 for (oix = 0; oix < ctx->nbufs; ++oix) {
771 int mlen;
772
773 i = ctx->olaps[oix].raix;
774 len = ctx->args[i].length;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000775
776 rpra[i].pv = 0;
777 rpra[i].len = len;
778 list[i].num = len ? 1 : 0;
779 list[i].pgidx = i;
780
781 if (!len)
782 continue;
783
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000784 if (ctx->maps[i]) {
Srinivas Kandagatla80f3afd2019-03-07 10:12:26 +0000785 struct vm_area_struct *vma = NULL;
786
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000787 rpra[i].pv = (u64) ctx->args[i].ptr;
788 pages[i].addr = ctx->maps[i]->phys;
Srinivas Kandagatla80f3afd2019-03-07 10:12:26 +0000789
790 vma = find_vma(current->mm, ctx->args[i].ptr);
791 if (vma)
792 pages[i].addr += ctx->args[i].ptr -
793 vma->vm_start;
794
Srinivas Kandagatla02b45b42019-03-07 10:12:28 +0000795 pg_start = (ctx->args[i].ptr & PAGE_MASK) >> PAGE_SHIFT;
796 pg_end = ((ctx->args[i].ptr + len - 1) & PAGE_MASK) >>
797 PAGE_SHIFT;
798 pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
799
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000800 } else {
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000801
802 if (ctx->olaps[oix].offset == 0) {
803 rlen -= ALIGN(args, FASTRPC_ALIGN) - args;
804 args = ALIGN(args, FASTRPC_ALIGN);
805 }
806
807 mlen = ctx->olaps[oix].mend - ctx->olaps[oix].mstart;
808
809 if (rlen < mlen)
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000810 goto bail;
811
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000812 rpra[i].pv = args - ctx->olaps[oix].offset;
813 pages[i].addr = ctx->buf->phys -
814 ctx->olaps[oix].offset +
815 (pkt_size - rlen);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000816 pages[i].addr = pages[i].addr & PAGE_MASK;
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000817
Srinivas Kandagatla02b45b42019-03-07 10:12:28 +0000818 pg_start = (args & PAGE_MASK) >> PAGE_SHIFT;
819 pg_end = ((args + len - 1) & PAGE_MASK) >> PAGE_SHIFT;
820 pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
Srinivas Kandagatla25e8dfb2019-03-07 10:12:27 +0000821 args = args + mlen;
822 rlen -= mlen;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000823 }
824
825 if (i < inbufs && !ctx->maps[i]) {
826 void *dst = (void *)(uintptr_t)rpra[i].pv;
827 void *src = (void *)(uintptr_t)ctx->args[i].ptr;
828
829 if (!kernel) {
830 if (copy_from_user(dst, (void __user *)src,
831 len)) {
832 err = -EFAULT;
833 goto bail;
834 }
835 } else {
836 memcpy(dst, src, len);
837 }
838 }
839 }
840
841 for (i = ctx->nbufs; i < ctx->nscalars; ++i) {
842 rpra[i].pv = (u64) ctx->args[i].ptr;
843 rpra[i].len = ctx->args[i].length;
844 list[i].num = ctx->args[i].length ? 1 : 0;
845 list[i].pgidx = i;
846 pages[i].addr = ctx->maps[i]->phys;
847 pages[i].size = ctx->maps[i]->size;
848 }
849
850bail:
851 if (err)
852 dev_err(dev, "Error: get invoke args failed:%d\n", err);
853
854 return err;
855}
856
857static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
858 u32 kernel)
859{
860 struct fastrpc_remote_arg *rpra = ctx->rpra;
861 int i, inbufs;
862
863 inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
864
865 for (i = inbufs; i < ctx->nbufs; ++i) {
866 void *src = (void *)(uintptr_t)rpra[i].pv;
867 void *dst = (void *)(uintptr_t)ctx->args[i].ptr;
868 u64 len = rpra[i].len;
869
870 if (!kernel) {
871 if (copy_to_user((void __user *)dst, src, len))
872 return -EFAULT;
873 } else {
874 memcpy(dst, src, len);
875 }
876 }
877
878 return 0;
879}
880
881static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx,
882 struct fastrpc_invoke_ctx *ctx,
883 u32 kernel, uint32_t handle)
884{
885 struct fastrpc_channel_ctx *cctx;
886 struct fastrpc_user *fl = ctx->fl;
887 struct fastrpc_msg *msg = &ctx->msg;
888
889 cctx = fl->cctx;
890 msg->pid = fl->tgid;
891 msg->tid = current->pid;
892
893 if (kernel)
894 msg->pid = 0;
895
896 msg->ctx = ctx->ctxid | fl->pd;
897 msg->handle = handle;
898 msg->sc = ctx->sc;
899 msg->addr = ctx->buf ? ctx->buf->phys : 0;
900 msg->size = roundup(ctx->msg_sz, PAGE_SIZE);
901 fastrpc_context_get(ctx);
902
903 return rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg));
904}
905
906static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
907 u32 handle, u32 sc,
908 struct fastrpc_invoke_args *args)
909{
910 struct fastrpc_invoke_ctx *ctx = NULL;
911 int err = 0;
912
913 if (!fl->sctx)
914 return -EINVAL;
915
Bjorn Andersson2e369872019-08-29 10:29:23 +0100916 if (!fl->cctx->rpdev)
917 return -EPIPE;
918
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000919 ctx = fastrpc_context_alloc(fl, kernel, sc, args);
920 if (IS_ERR(ctx))
921 return PTR_ERR(ctx);
922
923 if (ctx->nscalars) {
924 err = fastrpc_get_args(kernel, ctx);
925 if (err)
926 goto bail;
927 }
Srinivas Kandagatla415a0722019-03-07 10:12:24 +0000928
929 /* make sure that all CPU memory writes are seen by DSP */
930 dma_wmb();
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000931 /* Send invoke buffer to remote dsp */
932 err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle);
933 if (err)
934 goto bail;
935
936 /* Wait for remote dsp to respond or time out */
937 err = wait_for_completion_interruptible(&ctx->work);
938 if (err)
939 goto bail;
940
941 /* Check the response from remote dsp */
942 err = ctx->retval;
943 if (err)
944 goto bail;
945
946 if (ctx->nscalars) {
Srinivas Kandagatla415a0722019-03-07 10:12:24 +0000947 /* make sure that all memory writes by DSP are seen by CPU */
948 dma_rmb();
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +0000949 /* populate all the output buffers with results */
950 err = fastrpc_put_args(ctx, kernel);
951 if (err)
952 goto bail;
953 }
954
955bail:
956 /* We are done with this compute context, remove it from pending list */
957 spin_lock(&fl->lock);
958 list_del(&ctx->node);
959 spin_unlock(&fl->lock);
960 fastrpc_context_put(ctx);
961
962 if (err)
963 dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err);
964
965 return err;
966}
967
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000968static int fastrpc_init_create_process(struct fastrpc_user *fl,
969 char __user *argp)
970{
971 struct fastrpc_init_create init;
972 struct fastrpc_invoke_args *args;
973 struct fastrpc_phy_page pages[1];
974 struct fastrpc_map *map = NULL;
975 struct fastrpc_buf *imem = NULL;
976 int memlen;
977 int err;
978 struct {
979 int pgid;
980 u32 namelen;
981 u32 filelen;
982 u32 pageslen;
983 u32 attrs;
984 u32 siglen;
985 } inbuf;
986 u32 sc;
987
988 args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL);
989 if (!args)
990 return -ENOMEM;
991
992 if (copy_from_user(&init, argp, sizeof(init))) {
993 err = -EFAULT;
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000994 goto err;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +0000995 }
996
997 if (init.filelen > INIT_FILELEN_MAX) {
998 err = -EINVAL;
Thierry Escandeb49f6d82019-03-07 10:12:23 +0000999 goto err;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001000 }
1001
1002 inbuf.pgid = fl->tgid;
1003 inbuf.namelen = strlen(current->comm) + 1;
1004 inbuf.filelen = init.filelen;
1005 inbuf.pageslen = 1;
1006 inbuf.attrs = init.attrs;
1007 inbuf.siglen = init.siglen;
1008 fl->pd = 1;
1009
1010 if (init.filelen && init.filefd) {
1011 err = fastrpc_map_create(fl, init.filefd, init.filelen, &map);
1012 if (err)
Thierry Escandeb49f6d82019-03-07 10:12:23 +00001013 goto err;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001014 }
1015
1016 memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4),
1017 1024 * 1024);
1018 err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen,
1019 &imem);
Thierry Escandeb49f6d82019-03-07 10:12:23 +00001020 if (err)
1021 goto err_alloc;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001022
1023 fl->init_mem = imem;
1024 args[0].ptr = (u64)(uintptr_t)&inbuf;
1025 args[0].length = sizeof(inbuf);
1026 args[0].fd = -1;
1027
1028 args[1].ptr = (u64)(uintptr_t)current->comm;
1029 args[1].length = inbuf.namelen;
1030 args[1].fd = -1;
1031
1032 args[2].ptr = (u64) init.file;
1033 args[2].length = inbuf.filelen;
1034 args[2].fd = init.filefd;
1035
1036 pages[0].addr = imem->phys;
1037 pages[0].size = imem->size;
1038
1039 args[3].ptr = (u64)(uintptr_t) pages;
1040 args[3].length = 1 * sizeof(*pages);
1041 args[3].fd = -1;
1042
1043 args[4].ptr = (u64)(uintptr_t)&inbuf.attrs;
1044 args[4].length = sizeof(inbuf.attrs);
1045 args[4].fd = -1;
1046
1047 args[5].ptr = (u64)(uintptr_t) &inbuf.siglen;
1048 args[5].length = sizeof(inbuf.siglen);
1049 args[5].fd = -1;
1050
1051 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0);
1052 if (init.attrs)
1053 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0);
1054
1055 err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1056 sc, args);
Thierry Escandeb49f6d82019-03-07 10:12:23 +00001057 if (err)
1058 goto err_invoke;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001059
Thierry Escandeb49f6d82019-03-07 10:12:23 +00001060 kfree(args);
1061
1062 return 0;
1063
1064err_invoke:
1065 fl->init_mem = NULL;
1066 fastrpc_buf_free(imem);
1067err_alloc:
1068 if (map) {
1069 spin_lock(&fl->lock);
1070 list_del(&map->node);
1071 spin_unlock(&fl->lock);
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001072 fastrpc_map_put(map);
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001073 }
Thierry Escandeb49f6d82019-03-07 10:12:23 +00001074err:
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001075 kfree(args);
1076
1077 return err;
1078}
1079
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001080static struct fastrpc_session_ctx *fastrpc_session_alloc(
1081 struct fastrpc_channel_ctx *cctx)
1082{
1083 struct fastrpc_session_ctx *session = NULL;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001084 unsigned long flags;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001085 int i;
1086
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001087 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001088 for (i = 0; i < cctx->sesscount; i++) {
1089 if (!cctx->session[i].used && cctx->session[i].valid) {
1090 cctx->session[i].used = true;
1091 session = &cctx->session[i];
1092 break;
1093 }
1094 }
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001095 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001096
1097 return session;
1098}
1099
1100static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx,
1101 struct fastrpc_session_ctx *session)
1102{
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001103 unsigned long flags;
1104
1105 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001106 session->used = false;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001107 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001108}
1109
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001110static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl)
1111{
1112 struct fastrpc_invoke_args args[1];
1113 int tgid = 0;
1114 u32 sc;
1115
1116 tgid = fl->tgid;
1117 args[0].ptr = (u64)(uintptr_t) &tgid;
1118 args[0].length = sizeof(tgid);
1119 args[0].fd = -1;
1120 args[0].reserved = 0;
1121 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0);
1122
1123 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1124 sc, &args[0]);
1125}
1126
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001127static int fastrpc_device_release(struct inode *inode, struct file *file)
1128{
1129 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1130 struct fastrpc_channel_ctx *cctx = fl->cctx;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001131 struct fastrpc_invoke_ctx *ctx, *n;
1132 struct fastrpc_map *map, *m;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001133 unsigned long flags;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001134
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001135 fastrpc_release_current_dsp_process(fl);
1136
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001137 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001138 list_del(&fl->user);
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001139 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001140
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001141 if (fl->init_mem)
1142 fastrpc_buf_free(fl->init_mem);
1143
1144 list_for_each_entry_safe(ctx, n, &fl->pending, node) {
1145 list_del(&ctx->node);
1146 fastrpc_context_put(ctx);
1147 }
1148
1149 list_for_each_entry_safe(map, m, &fl->maps, node) {
1150 list_del(&map->node);
1151 fastrpc_map_put(map);
1152 }
1153
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001154 fastrpc_session_free(cctx, fl->sctx);
Bjorn Andersson278d56f2019-08-29 10:29:22 +01001155 fastrpc_channel_ctx_put(cctx);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001156
1157 mutex_destroy(&fl->mutex);
1158 kfree(fl);
1159 file->private_data = NULL;
1160
1161 return 0;
1162}
1163
1164static int fastrpc_device_open(struct inode *inode, struct file *filp)
1165{
1166 struct fastrpc_channel_ctx *cctx = miscdev_to_cctx(filp->private_data);
1167 struct fastrpc_user *fl = NULL;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001168 unsigned long flags;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001169
1170 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
1171 if (!fl)
1172 return -ENOMEM;
1173
Bjorn Andersson278d56f2019-08-29 10:29:22 +01001174 /* Released in fastrpc_device_release() */
1175 fastrpc_channel_ctx_get(cctx);
1176
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001177 filp->private_data = fl;
1178 spin_lock_init(&fl->lock);
1179 mutex_init(&fl->mutex);
1180 INIT_LIST_HEAD(&fl->pending);
1181 INIT_LIST_HEAD(&fl->maps);
1182 INIT_LIST_HEAD(&fl->user);
1183 fl->tgid = current->tgid;
1184 fl->cctx = cctx;
Thierry Escande7c11df42019-02-15 10:40:07 +00001185
1186 fl->sctx = fastrpc_session_alloc(cctx);
1187 if (!fl->sctx) {
1188 dev_err(&cctx->rpdev->dev, "No session available\n");
1189 mutex_destroy(&fl->mutex);
1190 kfree(fl);
1191
1192 return -EBUSY;
1193 }
1194
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001195 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001196 list_add_tail(&fl->user, &cctx->users);
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001197 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001198
1199 return 0;
1200}
1201
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +00001202static int fastrpc_dmabuf_free(struct fastrpc_user *fl, char __user *argp)
1203{
1204 struct dma_buf *buf;
1205 int info;
1206
1207 if (copy_from_user(&info, argp, sizeof(info)))
1208 return -EFAULT;
1209
1210 buf = dma_buf_get(info);
1211 if (IS_ERR_OR_NULL(buf))
1212 return -EINVAL;
1213 /*
1214 * one for the last get and other for the ALLOC_DMA_BUFF ioctl
1215 */
1216 dma_buf_put(buf);
1217 dma_buf_put(buf);
1218
1219 return 0;
1220}
1221
1222static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
1223{
1224 struct fastrpc_alloc_dma_buf bp;
1225 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
1226 struct fastrpc_buf *buf = NULL;
1227 int err;
1228
1229 if (copy_from_user(&bp, argp, sizeof(bp)))
1230 return -EFAULT;
1231
1232 err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf);
1233 if (err)
1234 return err;
1235 exp_info.ops = &fastrpc_dma_buf_ops;
1236 exp_info.size = bp.size;
1237 exp_info.flags = O_RDWR;
1238 exp_info.priv = buf;
1239 buf->dmabuf = dma_buf_export(&exp_info);
1240 if (IS_ERR(buf->dmabuf)) {
1241 err = PTR_ERR(buf->dmabuf);
1242 fastrpc_buf_free(buf);
1243 return err;
1244 }
1245
1246 bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE);
1247 if (bp.fd < 0) {
1248 dma_buf_put(buf->dmabuf);
1249 return -EINVAL;
1250 }
1251
1252 if (copy_to_user(argp, &bp, sizeof(bp))) {
1253 dma_buf_put(buf->dmabuf);
1254 return -EFAULT;
1255 }
1256
1257 get_dma_buf(buf->dmabuf);
1258
1259 return 0;
1260}
1261
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001262static int fastrpc_init_attach(struct fastrpc_user *fl)
1263{
1264 struct fastrpc_invoke_args args[1];
1265 int tgid = fl->tgid;
1266 u32 sc;
1267
1268 args[0].ptr = (u64)(uintptr_t) &tgid;
1269 args[0].length = sizeof(tgid);
1270 args[0].fd = -1;
1271 args[0].reserved = 0;
1272 sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0);
1273 fl->pd = 0;
1274
1275 return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1276 sc, &args[0]);
1277}
1278
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001279static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp)
1280{
1281 struct fastrpc_invoke_args *args = NULL;
1282 struct fastrpc_invoke inv;
1283 u32 nscalars;
1284 int err;
1285
1286 if (copy_from_user(&inv, argp, sizeof(inv)))
1287 return -EFAULT;
1288
1289 /* nscalars is truncated here to max supported value */
1290 nscalars = REMOTE_SCALARS_LENGTH(inv.sc);
1291 if (nscalars) {
1292 args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL);
1293 if (!args)
1294 return -ENOMEM;
1295
1296 if (copy_from_user(args, (void __user *)(uintptr_t)inv.args,
1297 nscalars * sizeof(*args))) {
1298 kfree(args);
1299 return -EFAULT;
1300 }
1301 }
1302
1303 err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args);
1304 kfree(args);
1305
1306 return err;
1307}
1308
1309static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
1310 unsigned long arg)
1311{
1312 struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1313 char __user *argp = (char __user *)arg;
1314 int err;
1315
1316 switch (cmd) {
1317 case FASTRPC_IOCTL_INVOKE:
1318 err = fastrpc_invoke(fl, argp);
1319 break;
Srinivas Kandagatlad73f71c2019-02-08 17:11:26 +00001320 case FASTRPC_IOCTL_INIT_ATTACH:
1321 err = fastrpc_init_attach(fl);
1322 break;
1323 case FASTRPC_IOCTL_INIT_CREATE:
1324 err = fastrpc_init_create_process(fl, argp);
1325 break;
Srinivas Kandagatla6cffd792019-02-08 17:11:27 +00001326 case FASTRPC_IOCTL_FREE_DMA_BUFF:
1327 err = fastrpc_dmabuf_free(fl, argp);
1328 break;
1329 case FASTRPC_IOCTL_ALLOC_DMA_BUFF:
1330 err = fastrpc_dmabuf_alloc(fl, argp);
1331 break;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001332 default:
1333 err = -ENOTTY;
1334 break;
1335 }
1336
1337 return err;
1338}
1339
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001340static const struct file_operations fastrpc_fops = {
1341 .open = fastrpc_device_open,
1342 .release = fastrpc_device_release,
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001343 .unlocked_ioctl = fastrpc_device_ioctl,
1344 .compat_ioctl = fastrpc_device_ioctl,
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001345};
1346
1347static int fastrpc_cb_probe(struct platform_device *pdev)
1348{
1349 struct fastrpc_channel_ctx *cctx;
1350 struct fastrpc_session_ctx *sess;
1351 struct device *dev = &pdev->dev;
1352 int i, sessions = 0;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001353 unsigned long flags;
Bo YU01b76c32019-03-28 03:47:37 -04001354 int rc;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001355
1356 cctx = dev_get_drvdata(dev->parent);
1357 if (!cctx)
1358 return -EINVAL;
1359
1360 of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions);
1361
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001362 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001363 sess = &cctx->session[cctx->sesscount];
1364 sess->used = false;
1365 sess->valid = true;
1366 sess->dev = dev;
1367 dev_set_drvdata(dev, sess);
1368
1369 if (of_property_read_u32(dev->of_node, "reg", &sess->sid))
1370 dev_info(dev, "FastRPC Session ID not specified in DT\n");
1371
1372 if (sessions > 0) {
1373 struct fastrpc_session_ctx *dup_sess;
1374
1375 for (i = 1; i < sessions; i++) {
1376 if (cctx->sesscount++ >= FASTRPC_MAX_SESSIONS)
1377 break;
1378 dup_sess = &cctx->session[cctx->sesscount];
1379 memcpy(dup_sess, sess, sizeof(*dup_sess));
1380 }
1381 }
1382 cctx->sesscount++;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001383 spin_unlock_irqrestore(&cctx->lock, flags);
Bo YU01b76c32019-03-28 03:47:37 -04001384 rc = dma_set_mask(dev, DMA_BIT_MASK(32));
1385 if (rc) {
1386 dev_err(dev, "32-bit DMA enable failed\n");
1387 return rc;
1388 }
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001389
1390 return 0;
1391}
1392
1393static int fastrpc_cb_remove(struct platform_device *pdev)
1394{
1395 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent);
1396 struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev);
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001397 unsigned long flags;
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001398 int i;
1399
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001400 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001401 for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) {
1402 if (cctx->session[i].sid == sess->sid) {
1403 cctx->session[i].valid = false;
1404 cctx->sesscount--;
1405 }
1406 }
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001407 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001408
1409 return 0;
1410}
1411
1412static const struct of_device_id fastrpc_match_table[] = {
1413 { .compatible = "qcom,fastrpc-compute-cb", },
1414 {}
1415};
1416
1417static struct platform_driver fastrpc_cb_driver = {
1418 .probe = fastrpc_cb_probe,
1419 .remove = fastrpc_cb_remove,
1420 .driver = {
1421 .name = "qcom,fastrpc-cb",
1422 .of_match_table = fastrpc_match_table,
1423 .suppress_bind_attrs = true,
1424 },
1425};
1426
1427static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
1428{
1429 struct device *rdev = &rpdev->dev;
1430 struct fastrpc_channel_ctx *data;
1431 int i, err, domain_id = -1;
1432 const char *domain;
1433
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001434 err = of_property_read_string(rdev->of_node, "label", &domain);
1435 if (err) {
1436 dev_info(rdev, "FastRPC Domain not specified in DT\n");
1437 return err;
1438 }
1439
1440 for (i = 0; i <= CDSP_DOMAIN_ID; i++) {
1441 if (!strcmp(domains[i], domain)) {
1442 domain_id = i;
1443 break;
1444 }
1445 }
1446
1447 if (domain_id < 0) {
1448 dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id);
1449 return -EINVAL;
1450 }
1451
Bjorn Andersson278d56f2019-08-29 10:29:22 +01001452 data = kzalloc(sizeof(*data), GFP_KERNEL);
1453 if (!data)
1454 return -ENOMEM;
1455
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001456 data->miscdev.minor = MISC_DYNAMIC_MINOR;
1457 data->miscdev.name = kasprintf(GFP_KERNEL, "fastrpc-%s",
1458 domains[domain_id]);
1459 data->miscdev.fops = &fastrpc_fops;
1460 err = misc_register(&data->miscdev);
1461 if (err)
1462 return err;
1463
Bjorn Andersson278d56f2019-08-29 10:29:22 +01001464 kref_init(&data->refcount);
1465
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001466 dev_set_drvdata(&rpdev->dev, data);
1467 dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
1468 INIT_LIST_HEAD(&data->users);
1469 spin_lock_init(&data->lock);
1470 idr_init(&data->ctx_idr);
1471 data->domain_id = domain_id;
1472 data->rpdev = rpdev;
1473
1474 return of_platform_populate(rdev->of_node, NULL, NULL, rdev);
1475}
1476
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001477static void fastrpc_notify_users(struct fastrpc_user *user)
1478{
1479 struct fastrpc_invoke_ctx *ctx;
1480
1481 spin_lock(&user->lock);
1482 list_for_each_entry(ctx, &user->pending, node)
1483 complete(&ctx->work);
1484 spin_unlock(&user->lock);
1485}
1486
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001487static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
1488{
1489 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001490 struct fastrpc_user *user;
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001491 unsigned long flags;
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001492
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001493 spin_lock_irqsave(&cctx->lock, flags);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001494 list_for_each_entry(user, &cctx->users, user)
1495 fastrpc_notify_users(user);
Srinivas Kandagatla977e6c82019-03-07 10:12:25 +00001496 spin_unlock_irqrestore(&cctx->lock, flags);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001497
1498 misc_deregister(&cctx->miscdev);
1499 of_platform_depopulate(&rpdev->dev);
Bjorn Andersson278d56f2019-08-29 10:29:22 +01001500
Bjorn Andersson2e369872019-08-29 10:29:23 +01001501 cctx->rpdev = NULL;
Bjorn Andersson278d56f2019-08-29 10:29:22 +01001502 fastrpc_channel_ctx_put(cctx);
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001503}
1504
1505static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
1506 int len, void *priv, u32 addr)
1507{
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001508 struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
1509 struct fastrpc_invoke_rsp *rsp = data;
1510 struct fastrpc_invoke_ctx *ctx;
1511 unsigned long flags;
1512 unsigned long ctxid;
1513
1514 if (len < sizeof(*rsp))
1515 return -EINVAL;
1516
1517 ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4);
1518
1519 spin_lock_irqsave(&cctx->lock, flags);
1520 ctx = idr_find(&cctx->ctx_idr, ctxid);
1521 spin_unlock_irqrestore(&cctx->lock, flags);
1522
1523 if (!ctx) {
1524 dev_err(&rpdev->dev, "No context ID matches response\n");
1525 return -ENOENT;
1526 }
1527
1528 ctx->retval = rsp->retval;
1529 complete(&ctx->work);
Thierry Escande8e7389c2019-03-07 10:12:22 +00001530
1531 /*
1532 * The DMA buffer associated with the context cannot be freed in
1533 * interrupt context so schedule it through a worker thread to
1534 * avoid a kernel BUG.
1535 */
1536 schedule_work(&ctx->put_work);
Srinivas Kandagatlac68cfb72019-02-08 17:11:25 +00001537
Srinivas Kandagatlaf6f92792019-02-08 17:11:24 +00001538 return 0;
1539}
1540
1541static const struct of_device_id fastrpc_rpmsg_of_match[] = {
1542 { .compatible = "qcom,fastrpc" },
1543 { },
1544};
1545MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match);
1546
1547static struct rpmsg_driver fastrpc_driver = {
1548 .probe = fastrpc_rpmsg_probe,
1549 .remove = fastrpc_rpmsg_remove,
1550 .callback = fastrpc_rpmsg_callback,
1551 .drv = {
1552 .name = "qcom,fastrpc",
1553 .of_match_table = fastrpc_rpmsg_of_match,
1554 },
1555};
1556
1557static int fastrpc_init(void)
1558{
1559 int ret;
1560
1561 ret = platform_driver_register(&fastrpc_cb_driver);
1562 if (ret < 0) {
1563 pr_err("fastrpc: failed to register cb driver\n");
1564 return ret;
1565 }
1566
1567 ret = register_rpmsg_driver(&fastrpc_driver);
1568 if (ret < 0) {
1569 pr_err("fastrpc: failed to register rpmsg driver\n");
1570 platform_driver_unregister(&fastrpc_cb_driver);
1571 return ret;
1572 }
1573
1574 return 0;
1575}
1576module_init(fastrpc_init);
1577
1578static void fastrpc_exit(void)
1579{
1580 platform_driver_unregister(&fastrpc_cb_driver);
1581 unregister_rpmsg_driver(&fastrpc_driver);
1582}
1583module_exit(fastrpc_exit);
1584
1585MODULE_LICENSE("GPL v2");