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