xref: /openbmc/linux/drivers/misc/fastrpc.c (revision 415a0729bd1225f0ffbc0ba82888dd65772554f7)
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)
32d73f71c7SSrinivas Kandagatla #define FASTRPC_INIT_HANDLE	1
33f6f9279fSSrinivas Kandagatla #define FASTRPC_CTXID_MASK (0xFF0)
34d73f71c7SSrinivas Kandagatla #define INIT_FILELEN_MAX (2 * 1024 * 1024)
35d73f71c7SSrinivas 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 
65d73f71c7SSrinivas Kandagatla #define FASTRPC_CREATE_PROCESS_NARGS	6
66d73f71c7SSrinivas Kandagatla /* Remote Method id table */
67d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_ATTACH	0
68d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_RELEASE	1
69d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE	6
70d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE_ATTR	7
71d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE_STATIC	8
72d73f71c7SSrinivas 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;
1096cffd795SSrinivas Kandagatla 	struct dma_buf *dmabuf;
110c68cfb71SSrinivas Kandagatla 	struct device *dev;
111c68cfb71SSrinivas Kandagatla 	void *virt;
112c68cfb71SSrinivas Kandagatla 	u64 phys;
113c68cfb71SSrinivas Kandagatla 	u64 size;
1146cffd795SSrinivas Kandagatla 	/* Lock for dma buf attachments */
1156cffd795SSrinivas Kandagatla 	struct mutex lock;
1166cffd795SSrinivas Kandagatla 	struct list_head attachments;
1176cffd795SSrinivas Kandagatla };
1186cffd795SSrinivas Kandagatla 
1196cffd795SSrinivas Kandagatla struct fastrpc_dma_buf_attachment {
1206cffd795SSrinivas Kandagatla 	struct device *dev;
1216cffd795SSrinivas Kandagatla 	struct sg_table sgt;
1226cffd795SSrinivas Kandagatla 	struct list_head node;
123c68cfb71SSrinivas Kandagatla };
124c68cfb71SSrinivas Kandagatla 
125c68cfb71SSrinivas Kandagatla struct fastrpc_map {
126c68cfb71SSrinivas Kandagatla 	struct list_head node;
127c68cfb71SSrinivas Kandagatla 	struct fastrpc_user *fl;
128c68cfb71SSrinivas Kandagatla 	int fd;
129c68cfb71SSrinivas Kandagatla 	struct dma_buf *buf;
130c68cfb71SSrinivas Kandagatla 	struct sg_table *table;
131c68cfb71SSrinivas Kandagatla 	struct dma_buf_attachment *attach;
132c68cfb71SSrinivas Kandagatla 	u64 phys;
133c68cfb71SSrinivas Kandagatla 	u64 size;
134c68cfb71SSrinivas Kandagatla 	void *va;
135c68cfb71SSrinivas Kandagatla 	u64 len;
136c68cfb71SSrinivas Kandagatla 	struct kref refcount;
137c68cfb71SSrinivas Kandagatla };
138c68cfb71SSrinivas Kandagatla 
139c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx {
140c68cfb71SSrinivas Kandagatla 	int nscalars;
141c68cfb71SSrinivas Kandagatla 	int nbufs;
142c68cfb71SSrinivas Kandagatla 	int retval;
143c68cfb71SSrinivas Kandagatla 	int pid;
144c68cfb71SSrinivas Kandagatla 	int tgid;
145c68cfb71SSrinivas Kandagatla 	u32 sc;
146c68cfb71SSrinivas Kandagatla 	u32 *crc;
147c68cfb71SSrinivas Kandagatla 	u64 ctxid;
148c68cfb71SSrinivas Kandagatla 	u64 msg_sz;
149c68cfb71SSrinivas Kandagatla 	struct kref refcount;
150c68cfb71SSrinivas Kandagatla 	struct list_head node; /* list of ctxs */
151c68cfb71SSrinivas Kandagatla 	struct completion work;
1528e7389c7SThierry Escande 	struct work_struct put_work;
153c68cfb71SSrinivas Kandagatla 	struct fastrpc_msg msg;
154c68cfb71SSrinivas Kandagatla 	struct fastrpc_user *fl;
155c68cfb71SSrinivas Kandagatla 	struct fastrpc_remote_arg *rpra;
156c68cfb71SSrinivas Kandagatla 	struct fastrpc_map **maps;
157c68cfb71SSrinivas Kandagatla 	struct fastrpc_buf *buf;
158c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_args *args;
159c68cfb71SSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx;
160c68cfb71SSrinivas Kandagatla };
161f6f9279fSSrinivas Kandagatla 
162f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx {
163f6f9279fSSrinivas Kandagatla 	struct device *dev;
164f6f9279fSSrinivas Kandagatla 	int sid;
165f6f9279fSSrinivas Kandagatla 	bool used;
166f6f9279fSSrinivas Kandagatla 	bool valid;
167f6f9279fSSrinivas Kandagatla };
168f6f9279fSSrinivas Kandagatla 
169f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx {
170f6f9279fSSrinivas Kandagatla 	int domain_id;
171f6f9279fSSrinivas Kandagatla 	int sesscount;
172f6f9279fSSrinivas Kandagatla 	struct rpmsg_device *rpdev;
173f6f9279fSSrinivas Kandagatla 	struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS];
174f6f9279fSSrinivas Kandagatla 	spinlock_t lock;
175f6f9279fSSrinivas Kandagatla 	struct idr ctx_idr;
176f6f9279fSSrinivas Kandagatla 	struct list_head users;
177f6f9279fSSrinivas Kandagatla 	struct miscdevice miscdev;
178f6f9279fSSrinivas Kandagatla };
179f6f9279fSSrinivas Kandagatla 
180f6f9279fSSrinivas Kandagatla struct fastrpc_user {
181f6f9279fSSrinivas Kandagatla 	struct list_head user;
182f6f9279fSSrinivas Kandagatla 	struct list_head maps;
183f6f9279fSSrinivas Kandagatla 	struct list_head pending;
184f6f9279fSSrinivas Kandagatla 
185f6f9279fSSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx;
186f6f9279fSSrinivas Kandagatla 	struct fastrpc_session_ctx *sctx;
187c68cfb71SSrinivas Kandagatla 	struct fastrpc_buf *init_mem;
188f6f9279fSSrinivas Kandagatla 
189f6f9279fSSrinivas Kandagatla 	int tgid;
190f6f9279fSSrinivas Kandagatla 	int pd;
191f6f9279fSSrinivas Kandagatla 	/* Lock for lists */
192f6f9279fSSrinivas Kandagatla 	spinlock_t lock;
193f6f9279fSSrinivas Kandagatla 	/* lock for allocations */
194f6f9279fSSrinivas Kandagatla 	struct mutex mutex;
195f6f9279fSSrinivas Kandagatla };
196f6f9279fSSrinivas Kandagatla 
197c68cfb71SSrinivas Kandagatla static void fastrpc_free_map(struct kref *ref)
198c68cfb71SSrinivas Kandagatla {
199c68cfb71SSrinivas Kandagatla 	struct fastrpc_map *map;
200c68cfb71SSrinivas Kandagatla 
201c68cfb71SSrinivas Kandagatla 	map = container_of(ref, struct fastrpc_map, refcount);
202c68cfb71SSrinivas Kandagatla 
203c68cfb71SSrinivas Kandagatla 	if (map->table) {
204c68cfb71SSrinivas Kandagatla 		dma_buf_unmap_attachment(map->attach, map->table,
205c68cfb71SSrinivas Kandagatla 					 DMA_BIDIRECTIONAL);
206c68cfb71SSrinivas Kandagatla 		dma_buf_detach(map->buf, map->attach);
207c68cfb71SSrinivas Kandagatla 		dma_buf_put(map->buf);
208c68cfb71SSrinivas Kandagatla 	}
209c68cfb71SSrinivas Kandagatla 
210c68cfb71SSrinivas Kandagatla 	kfree(map);
211c68cfb71SSrinivas Kandagatla }
212c68cfb71SSrinivas Kandagatla 
213c68cfb71SSrinivas Kandagatla static void fastrpc_map_put(struct fastrpc_map *map)
214c68cfb71SSrinivas Kandagatla {
215c68cfb71SSrinivas Kandagatla 	if (map)
216c68cfb71SSrinivas Kandagatla 		kref_put(&map->refcount, fastrpc_free_map);
217c68cfb71SSrinivas Kandagatla }
218c68cfb71SSrinivas Kandagatla 
219c68cfb71SSrinivas Kandagatla static void fastrpc_map_get(struct fastrpc_map *map)
220c68cfb71SSrinivas Kandagatla {
221c68cfb71SSrinivas Kandagatla 	if (map)
222c68cfb71SSrinivas Kandagatla 		kref_get(&map->refcount);
223c68cfb71SSrinivas Kandagatla }
224c68cfb71SSrinivas Kandagatla 
225c68cfb71SSrinivas Kandagatla static int fastrpc_map_find(struct fastrpc_user *fl, int fd,
226c68cfb71SSrinivas Kandagatla 			    struct fastrpc_map **ppmap)
227c68cfb71SSrinivas Kandagatla {
228c68cfb71SSrinivas Kandagatla 	struct fastrpc_map *map = NULL;
229c68cfb71SSrinivas Kandagatla 
230c68cfb71SSrinivas Kandagatla 	mutex_lock(&fl->mutex);
231c68cfb71SSrinivas Kandagatla 	list_for_each_entry(map, &fl->maps, node) {
232c68cfb71SSrinivas Kandagatla 		if (map->fd == fd) {
233c68cfb71SSrinivas Kandagatla 			fastrpc_map_get(map);
234c68cfb71SSrinivas Kandagatla 			*ppmap = map;
235c68cfb71SSrinivas Kandagatla 			mutex_unlock(&fl->mutex);
236c68cfb71SSrinivas Kandagatla 			return 0;
237c68cfb71SSrinivas Kandagatla 		}
238c68cfb71SSrinivas Kandagatla 	}
239c68cfb71SSrinivas Kandagatla 	mutex_unlock(&fl->mutex);
240c68cfb71SSrinivas Kandagatla 
241c68cfb71SSrinivas Kandagatla 	return -ENOENT;
242c68cfb71SSrinivas Kandagatla }
243c68cfb71SSrinivas Kandagatla 
244c68cfb71SSrinivas Kandagatla static void fastrpc_buf_free(struct fastrpc_buf *buf)
245c68cfb71SSrinivas Kandagatla {
246c68cfb71SSrinivas Kandagatla 	dma_free_coherent(buf->dev, buf->size, buf->virt,
247c68cfb71SSrinivas Kandagatla 			  FASTRPC_PHYS(buf->phys));
248c68cfb71SSrinivas Kandagatla 	kfree(buf);
249c68cfb71SSrinivas Kandagatla }
250c68cfb71SSrinivas Kandagatla 
251c68cfb71SSrinivas Kandagatla static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
252c68cfb71SSrinivas Kandagatla 			     u64 size, struct fastrpc_buf **obuf)
253c68cfb71SSrinivas Kandagatla {
254c68cfb71SSrinivas Kandagatla 	struct fastrpc_buf *buf;
255c68cfb71SSrinivas Kandagatla 
256c68cfb71SSrinivas Kandagatla 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
257c68cfb71SSrinivas Kandagatla 	if (!buf)
258c68cfb71SSrinivas Kandagatla 		return -ENOMEM;
259c68cfb71SSrinivas Kandagatla 
2606cffd795SSrinivas Kandagatla 	INIT_LIST_HEAD(&buf->attachments);
2616cffd795SSrinivas Kandagatla 	mutex_init(&buf->lock);
2626cffd795SSrinivas Kandagatla 
263c68cfb71SSrinivas Kandagatla 	buf->fl = fl;
264c68cfb71SSrinivas Kandagatla 	buf->virt = NULL;
265c68cfb71SSrinivas Kandagatla 	buf->phys = 0;
266c68cfb71SSrinivas Kandagatla 	buf->size = size;
267c68cfb71SSrinivas Kandagatla 	buf->dev = dev;
268c68cfb71SSrinivas Kandagatla 
269c68cfb71SSrinivas Kandagatla 	buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys,
270c68cfb71SSrinivas Kandagatla 				       GFP_KERNEL);
271c68cfb71SSrinivas Kandagatla 	if (!buf->virt)
272c68cfb71SSrinivas Kandagatla 		return -ENOMEM;
273c68cfb71SSrinivas Kandagatla 
274c68cfb71SSrinivas Kandagatla 	if (fl->sctx && fl->sctx->sid)
275c68cfb71SSrinivas Kandagatla 		buf->phys += ((u64)fl->sctx->sid << 32);
276c68cfb71SSrinivas Kandagatla 
277c68cfb71SSrinivas Kandagatla 	*obuf = buf;
278c68cfb71SSrinivas Kandagatla 
279c68cfb71SSrinivas Kandagatla 	return 0;
280c68cfb71SSrinivas Kandagatla }
281c68cfb71SSrinivas Kandagatla 
282c68cfb71SSrinivas Kandagatla static void fastrpc_context_free(struct kref *ref)
283c68cfb71SSrinivas Kandagatla {
284c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_ctx *ctx;
285c68cfb71SSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx;
286c68cfb71SSrinivas Kandagatla 	int i;
287c68cfb71SSrinivas Kandagatla 
288c68cfb71SSrinivas Kandagatla 	ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
289c68cfb71SSrinivas Kandagatla 	cctx = ctx->cctx;
290c68cfb71SSrinivas Kandagatla 
291c68cfb71SSrinivas Kandagatla 	for (i = 0; i < ctx->nscalars; i++)
292c68cfb71SSrinivas Kandagatla 		fastrpc_map_put(ctx->maps[i]);
293c68cfb71SSrinivas Kandagatla 
294c68cfb71SSrinivas Kandagatla 	if (ctx->buf)
295c68cfb71SSrinivas Kandagatla 		fastrpc_buf_free(ctx->buf);
296c68cfb71SSrinivas Kandagatla 
297c68cfb71SSrinivas Kandagatla 	spin_lock(&cctx->lock);
298c68cfb71SSrinivas Kandagatla 	idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4);
299c68cfb71SSrinivas Kandagatla 	spin_unlock(&cctx->lock);
300c68cfb71SSrinivas Kandagatla 
301c68cfb71SSrinivas Kandagatla 	kfree(ctx->maps);
302c68cfb71SSrinivas Kandagatla 	kfree(ctx);
303c68cfb71SSrinivas Kandagatla }
304c68cfb71SSrinivas Kandagatla 
305c68cfb71SSrinivas Kandagatla static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx)
306c68cfb71SSrinivas Kandagatla {
307c68cfb71SSrinivas Kandagatla 	kref_get(&ctx->refcount);
308c68cfb71SSrinivas Kandagatla }
309c68cfb71SSrinivas Kandagatla 
310c68cfb71SSrinivas Kandagatla static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx)
311c68cfb71SSrinivas Kandagatla {
312c68cfb71SSrinivas Kandagatla 	kref_put(&ctx->refcount, fastrpc_context_free);
313c68cfb71SSrinivas Kandagatla }
314c68cfb71SSrinivas Kandagatla 
3158e7389c7SThierry Escande static void fastrpc_context_put_wq(struct work_struct *work)
3168e7389c7SThierry Escande {
3178e7389c7SThierry Escande 	struct fastrpc_invoke_ctx *ctx =
3188e7389c7SThierry Escande 			container_of(work, struct fastrpc_invoke_ctx, put_work);
3198e7389c7SThierry Escande 
3208e7389c7SThierry Escande 	fastrpc_context_put(ctx);
3218e7389c7SThierry Escande }
3228e7389c7SThierry Escande 
323c68cfb71SSrinivas Kandagatla static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
324c68cfb71SSrinivas Kandagatla 			struct fastrpc_user *user, u32 kernel, u32 sc,
325c68cfb71SSrinivas Kandagatla 			struct fastrpc_invoke_args *args)
326c68cfb71SSrinivas Kandagatla {
327c68cfb71SSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx = user->cctx;
328c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_ctx *ctx = NULL;
329c68cfb71SSrinivas Kandagatla 	int ret;
330c68cfb71SSrinivas Kandagatla 
331c68cfb71SSrinivas Kandagatla 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
332c68cfb71SSrinivas Kandagatla 	if (!ctx)
333c68cfb71SSrinivas Kandagatla 		return ERR_PTR(-ENOMEM);
334c68cfb71SSrinivas Kandagatla 
335c68cfb71SSrinivas Kandagatla 	INIT_LIST_HEAD(&ctx->node);
336c68cfb71SSrinivas Kandagatla 	ctx->fl = user;
337c68cfb71SSrinivas Kandagatla 	ctx->nscalars = REMOTE_SCALARS_LENGTH(sc);
338c68cfb71SSrinivas Kandagatla 	ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) +
339c68cfb71SSrinivas Kandagatla 		     REMOTE_SCALARS_OUTBUFS(sc);
340c68cfb71SSrinivas Kandagatla 
341c68cfb71SSrinivas Kandagatla 	if (ctx->nscalars) {
342c68cfb71SSrinivas Kandagatla 		ctx->maps = kcalloc(ctx->nscalars,
343c68cfb71SSrinivas Kandagatla 				    sizeof(*ctx->maps), GFP_KERNEL);
344c68cfb71SSrinivas Kandagatla 		if (!ctx->maps) {
345c68cfb71SSrinivas Kandagatla 			kfree(ctx);
346c68cfb71SSrinivas Kandagatla 			return ERR_PTR(-ENOMEM);
347c68cfb71SSrinivas Kandagatla 		}
348c68cfb71SSrinivas Kandagatla 		ctx->args = args;
349c68cfb71SSrinivas Kandagatla 	}
350c68cfb71SSrinivas Kandagatla 
351c68cfb71SSrinivas Kandagatla 	ctx->sc = sc;
352c68cfb71SSrinivas Kandagatla 	ctx->retval = -1;
353c68cfb71SSrinivas Kandagatla 	ctx->pid = current->pid;
354c68cfb71SSrinivas Kandagatla 	ctx->tgid = user->tgid;
355c68cfb71SSrinivas Kandagatla 	ctx->cctx = cctx;
356c68cfb71SSrinivas Kandagatla 	init_completion(&ctx->work);
3578e7389c7SThierry Escande 	INIT_WORK(&ctx->put_work, fastrpc_context_put_wq);
358c68cfb71SSrinivas Kandagatla 
359c68cfb71SSrinivas Kandagatla 	spin_lock(&user->lock);
360c68cfb71SSrinivas Kandagatla 	list_add_tail(&ctx->node, &user->pending);
361c68cfb71SSrinivas Kandagatla 	spin_unlock(&user->lock);
362c68cfb71SSrinivas Kandagatla 
363c68cfb71SSrinivas Kandagatla 	spin_lock(&cctx->lock);
364c68cfb71SSrinivas Kandagatla 	ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1,
365c68cfb71SSrinivas Kandagatla 			       FASTRPC_CTX_MAX, GFP_ATOMIC);
366c68cfb71SSrinivas Kandagatla 	if (ret < 0) {
367c68cfb71SSrinivas Kandagatla 		spin_unlock(&cctx->lock);
368c68cfb71SSrinivas Kandagatla 		goto err_idr;
369c68cfb71SSrinivas Kandagatla 	}
370c68cfb71SSrinivas Kandagatla 	ctx->ctxid = ret << 4;
371c68cfb71SSrinivas Kandagatla 	spin_unlock(&cctx->lock);
372c68cfb71SSrinivas Kandagatla 
373c68cfb71SSrinivas Kandagatla 	kref_init(&ctx->refcount);
374c68cfb71SSrinivas Kandagatla 
375c68cfb71SSrinivas Kandagatla 	return ctx;
376c68cfb71SSrinivas Kandagatla err_idr:
377c68cfb71SSrinivas Kandagatla 	spin_lock(&user->lock);
378c68cfb71SSrinivas Kandagatla 	list_del(&ctx->node);
379c68cfb71SSrinivas Kandagatla 	spin_unlock(&user->lock);
380c68cfb71SSrinivas Kandagatla 	kfree(ctx->maps);
381c68cfb71SSrinivas Kandagatla 	kfree(ctx);
382c68cfb71SSrinivas Kandagatla 
383c68cfb71SSrinivas Kandagatla 	return ERR_PTR(ret);
384c68cfb71SSrinivas Kandagatla }
385c68cfb71SSrinivas Kandagatla 
3866cffd795SSrinivas Kandagatla static struct sg_table *
3876cffd795SSrinivas Kandagatla fastrpc_map_dma_buf(struct dma_buf_attachment *attachment,
3886cffd795SSrinivas Kandagatla 		    enum dma_data_direction dir)
3896cffd795SSrinivas Kandagatla {
3906cffd795SSrinivas Kandagatla 	struct fastrpc_dma_buf_attachment *a = attachment->priv;
3916cffd795SSrinivas Kandagatla 	struct sg_table *table;
3926cffd795SSrinivas Kandagatla 
3936cffd795SSrinivas Kandagatla 	table = &a->sgt;
3946cffd795SSrinivas Kandagatla 
3956cffd795SSrinivas Kandagatla 	if (!dma_map_sg(attachment->dev, table->sgl, table->nents, dir))
3966cffd795SSrinivas Kandagatla 		return ERR_PTR(-ENOMEM);
3976cffd795SSrinivas Kandagatla 
3986cffd795SSrinivas Kandagatla 	return table;
3996cffd795SSrinivas Kandagatla }
4006cffd795SSrinivas Kandagatla 
4016cffd795SSrinivas Kandagatla static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach,
4026cffd795SSrinivas Kandagatla 				  struct sg_table *table,
4036cffd795SSrinivas Kandagatla 				  enum dma_data_direction dir)
4046cffd795SSrinivas Kandagatla {
4056cffd795SSrinivas Kandagatla 	dma_unmap_sg(attach->dev, table->sgl, table->nents, dir);
4066cffd795SSrinivas Kandagatla }
4076cffd795SSrinivas Kandagatla 
4086cffd795SSrinivas Kandagatla static void fastrpc_release(struct dma_buf *dmabuf)
4096cffd795SSrinivas Kandagatla {
4106cffd795SSrinivas Kandagatla 	struct fastrpc_buf *buffer = dmabuf->priv;
4116cffd795SSrinivas Kandagatla 
4126cffd795SSrinivas Kandagatla 	fastrpc_buf_free(buffer);
4136cffd795SSrinivas Kandagatla }
4146cffd795SSrinivas Kandagatla 
4156cffd795SSrinivas Kandagatla static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf,
4166cffd795SSrinivas Kandagatla 				  struct dma_buf_attachment *attachment)
4176cffd795SSrinivas Kandagatla {
4186cffd795SSrinivas Kandagatla 	struct fastrpc_dma_buf_attachment *a;
4196cffd795SSrinivas Kandagatla 	struct fastrpc_buf *buffer = dmabuf->priv;
4206cffd795SSrinivas Kandagatla 	int ret;
4216cffd795SSrinivas Kandagatla 
4226cffd795SSrinivas Kandagatla 	a = kzalloc(sizeof(*a), GFP_KERNEL);
4236cffd795SSrinivas Kandagatla 	if (!a)
4246cffd795SSrinivas Kandagatla 		return -ENOMEM;
4256cffd795SSrinivas Kandagatla 
4266cffd795SSrinivas Kandagatla 	ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt,
4276cffd795SSrinivas Kandagatla 			      FASTRPC_PHYS(buffer->phys), buffer->size);
4286cffd795SSrinivas Kandagatla 	if (ret < 0) {
4296cffd795SSrinivas Kandagatla 		dev_err(buffer->dev, "failed to get scatterlist from DMA API\n");
4306cffd795SSrinivas Kandagatla 		return -EINVAL;
4316cffd795SSrinivas Kandagatla 	}
4326cffd795SSrinivas Kandagatla 
4336cffd795SSrinivas Kandagatla 	a->dev = attachment->dev;
4346cffd795SSrinivas Kandagatla 	INIT_LIST_HEAD(&a->node);
4356cffd795SSrinivas Kandagatla 	attachment->priv = a;
4366cffd795SSrinivas Kandagatla 
4376cffd795SSrinivas Kandagatla 	mutex_lock(&buffer->lock);
4386cffd795SSrinivas Kandagatla 	list_add(&a->node, &buffer->attachments);
4396cffd795SSrinivas Kandagatla 	mutex_unlock(&buffer->lock);
4406cffd795SSrinivas Kandagatla 
4416cffd795SSrinivas Kandagatla 	return 0;
4426cffd795SSrinivas Kandagatla }
4436cffd795SSrinivas Kandagatla 
4446cffd795SSrinivas Kandagatla static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf,
4456cffd795SSrinivas Kandagatla 				    struct dma_buf_attachment *attachment)
4466cffd795SSrinivas Kandagatla {
4476cffd795SSrinivas Kandagatla 	struct fastrpc_dma_buf_attachment *a = attachment->priv;
4486cffd795SSrinivas Kandagatla 	struct fastrpc_buf *buffer = dmabuf->priv;
4496cffd795SSrinivas Kandagatla 
4506cffd795SSrinivas Kandagatla 	mutex_lock(&buffer->lock);
4516cffd795SSrinivas Kandagatla 	list_del(&a->node);
4526cffd795SSrinivas Kandagatla 	mutex_unlock(&buffer->lock);
4536cffd795SSrinivas Kandagatla 	kfree(a);
4546cffd795SSrinivas Kandagatla }
4556cffd795SSrinivas Kandagatla 
4566cffd795SSrinivas Kandagatla static void *fastrpc_kmap(struct dma_buf *dmabuf, unsigned long pgnum)
4576cffd795SSrinivas Kandagatla {
4586cffd795SSrinivas Kandagatla 	struct fastrpc_buf *buf = dmabuf->priv;
4596cffd795SSrinivas Kandagatla 
4606cffd795SSrinivas Kandagatla 	return buf->virt ? buf->virt + pgnum * PAGE_SIZE : NULL;
4616cffd795SSrinivas Kandagatla }
4626cffd795SSrinivas Kandagatla 
4636cffd795SSrinivas Kandagatla static void *fastrpc_vmap(struct dma_buf *dmabuf)
4646cffd795SSrinivas Kandagatla {
4656cffd795SSrinivas Kandagatla 	struct fastrpc_buf *buf = dmabuf->priv;
4666cffd795SSrinivas Kandagatla 
4676cffd795SSrinivas Kandagatla 	return buf->virt;
4686cffd795SSrinivas Kandagatla }
4696cffd795SSrinivas Kandagatla 
4706cffd795SSrinivas Kandagatla static int fastrpc_mmap(struct dma_buf *dmabuf,
4716cffd795SSrinivas Kandagatla 			struct vm_area_struct *vma)
4726cffd795SSrinivas Kandagatla {
4736cffd795SSrinivas Kandagatla 	struct fastrpc_buf *buf = dmabuf->priv;
4746cffd795SSrinivas Kandagatla 	size_t size = vma->vm_end - vma->vm_start;
4756cffd795SSrinivas Kandagatla 
4766cffd795SSrinivas Kandagatla 	return dma_mmap_coherent(buf->dev, vma, buf->virt,
4776cffd795SSrinivas Kandagatla 				 FASTRPC_PHYS(buf->phys), size);
4786cffd795SSrinivas Kandagatla }
4796cffd795SSrinivas Kandagatla 
4806cffd795SSrinivas Kandagatla static const struct dma_buf_ops fastrpc_dma_buf_ops = {
4816cffd795SSrinivas Kandagatla 	.attach = fastrpc_dma_buf_attach,
4826cffd795SSrinivas Kandagatla 	.detach = fastrpc_dma_buf_detatch,
4836cffd795SSrinivas Kandagatla 	.map_dma_buf = fastrpc_map_dma_buf,
4846cffd795SSrinivas Kandagatla 	.unmap_dma_buf = fastrpc_unmap_dma_buf,
4856cffd795SSrinivas Kandagatla 	.mmap = fastrpc_mmap,
4866cffd795SSrinivas Kandagatla 	.map = fastrpc_kmap,
4876cffd795SSrinivas Kandagatla 	.vmap = fastrpc_vmap,
4886cffd795SSrinivas Kandagatla 	.release = fastrpc_release,
4896cffd795SSrinivas Kandagatla };
4906cffd795SSrinivas Kandagatla 
491c68cfb71SSrinivas Kandagatla static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
492c68cfb71SSrinivas Kandagatla 			      u64 len, struct fastrpc_map **ppmap)
493c68cfb71SSrinivas Kandagatla {
494c68cfb71SSrinivas Kandagatla 	struct fastrpc_session_ctx *sess = fl->sctx;
495c68cfb71SSrinivas Kandagatla 	struct fastrpc_map *map = NULL;
496c68cfb71SSrinivas Kandagatla 	int err = 0;
497c68cfb71SSrinivas Kandagatla 
498c68cfb71SSrinivas Kandagatla 	if (!fastrpc_map_find(fl, fd, ppmap))
499c68cfb71SSrinivas Kandagatla 		return 0;
500c68cfb71SSrinivas Kandagatla 
501c68cfb71SSrinivas Kandagatla 	map = kzalloc(sizeof(*map), GFP_KERNEL);
502c68cfb71SSrinivas Kandagatla 	if (!map)
503c68cfb71SSrinivas Kandagatla 		return -ENOMEM;
504c68cfb71SSrinivas Kandagatla 
505c68cfb71SSrinivas Kandagatla 	INIT_LIST_HEAD(&map->node);
506c68cfb71SSrinivas Kandagatla 	map->fl = fl;
507c68cfb71SSrinivas Kandagatla 	map->fd = fd;
508c68cfb71SSrinivas Kandagatla 	map->buf = dma_buf_get(fd);
509682a6044SWei Yongjun 	if (IS_ERR(map->buf)) {
510682a6044SWei Yongjun 		err = PTR_ERR(map->buf);
511c68cfb71SSrinivas Kandagatla 		goto get_err;
512c68cfb71SSrinivas Kandagatla 	}
513c68cfb71SSrinivas Kandagatla 
514c68cfb71SSrinivas Kandagatla 	map->attach = dma_buf_attach(map->buf, sess->dev);
515c68cfb71SSrinivas Kandagatla 	if (IS_ERR(map->attach)) {
516c68cfb71SSrinivas Kandagatla 		dev_err(sess->dev, "Failed to attach dmabuf\n");
517c68cfb71SSrinivas Kandagatla 		err = PTR_ERR(map->attach);
518c68cfb71SSrinivas Kandagatla 		goto attach_err;
519c68cfb71SSrinivas Kandagatla 	}
520c68cfb71SSrinivas Kandagatla 
521c68cfb71SSrinivas Kandagatla 	map->table = dma_buf_map_attachment(map->attach, DMA_BIDIRECTIONAL);
522c68cfb71SSrinivas Kandagatla 	if (IS_ERR(map->table)) {
523c68cfb71SSrinivas Kandagatla 		err = PTR_ERR(map->table);
524c68cfb71SSrinivas Kandagatla 		goto map_err;
525c68cfb71SSrinivas Kandagatla 	}
526c68cfb71SSrinivas Kandagatla 
527c68cfb71SSrinivas Kandagatla 	map->phys = sg_dma_address(map->table->sgl);
528c68cfb71SSrinivas Kandagatla 	map->phys += ((u64)fl->sctx->sid << 32);
529c68cfb71SSrinivas Kandagatla 	map->size = len;
530c68cfb71SSrinivas Kandagatla 	map->va = sg_virt(map->table->sgl);
531c68cfb71SSrinivas Kandagatla 	map->len = len;
532c68cfb71SSrinivas Kandagatla 	kref_init(&map->refcount);
533c68cfb71SSrinivas Kandagatla 
534c68cfb71SSrinivas Kandagatla 	spin_lock(&fl->lock);
535c68cfb71SSrinivas Kandagatla 	list_add_tail(&map->node, &fl->maps);
536c68cfb71SSrinivas Kandagatla 	spin_unlock(&fl->lock);
537c68cfb71SSrinivas Kandagatla 	*ppmap = map;
538c68cfb71SSrinivas Kandagatla 
539c68cfb71SSrinivas Kandagatla 	return 0;
540c68cfb71SSrinivas Kandagatla 
541c68cfb71SSrinivas Kandagatla map_err:
542c68cfb71SSrinivas Kandagatla 	dma_buf_detach(map->buf, map->attach);
543c68cfb71SSrinivas Kandagatla attach_err:
544c68cfb71SSrinivas Kandagatla 	dma_buf_put(map->buf);
545c68cfb71SSrinivas Kandagatla get_err:
546c68cfb71SSrinivas Kandagatla 	kfree(map);
547c68cfb71SSrinivas Kandagatla 
548c68cfb71SSrinivas Kandagatla 	return err;
549c68cfb71SSrinivas Kandagatla }
550c68cfb71SSrinivas Kandagatla 
551c68cfb71SSrinivas Kandagatla /*
552c68cfb71SSrinivas Kandagatla  * Fastrpc payload buffer with metadata looks like:
553c68cfb71SSrinivas Kandagatla  *
554c68cfb71SSrinivas Kandagatla  * >>>>>>  START of METADATA <<<<<<<<<
555c68cfb71SSrinivas Kandagatla  * +---------------------------------+
556c68cfb71SSrinivas Kandagatla  * |           Arguments             |
557c68cfb71SSrinivas Kandagatla  * | type:(struct fastrpc_remote_arg)|
558c68cfb71SSrinivas Kandagatla  * |             (0 - N)             |
559c68cfb71SSrinivas Kandagatla  * +---------------------------------+
560c68cfb71SSrinivas Kandagatla  * |         Invoke Buffer list      |
561c68cfb71SSrinivas Kandagatla  * | type:(struct fastrpc_invoke_buf)|
562c68cfb71SSrinivas Kandagatla  * |           (0 - N)               |
563c68cfb71SSrinivas Kandagatla  * +---------------------------------+
564c68cfb71SSrinivas Kandagatla  * |         Page info list          |
565c68cfb71SSrinivas Kandagatla  * | type:(struct fastrpc_phy_page)  |
566c68cfb71SSrinivas Kandagatla  * |             (0 - N)             |
567c68cfb71SSrinivas Kandagatla  * +---------------------------------+
568c68cfb71SSrinivas Kandagatla  * |         Optional info           |
569c68cfb71SSrinivas Kandagatla  * |(can be specific to SoC/Firmware)|
570c68cfb71SSrinivas Kandagatla  * +---------------------------------+
571c68cfb71SSrinivas Kandagatla  * >>>>>>>>  END of METADATA <<<<<<<<<
572c68cfb71SSrinivas Kandagatla  * +---------------------------------+
573c68cfb71SSrinivas Kandagatla  * |         Inline ARGS             |
574c68cfb71SSrinivas Kandagatla  * |            (0-N)                |
575c68cfb71SSrinivas Kandagatla  * +---------------------------------+
576c68cfb71SSrinivas Kandagatla  */
577c68cfb71SSrinivas Kandagatla 
578c68cfb71SSrinivas Kandagatla static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx)
579c68cfb71SSrinivas Kandagatla {
580c68cfb71SSrinivas Kandagatla 	int size = 0;
581c68cfb71SSrinivas Kandagatla 
582c68cfb71SSrinivas Kandagatla 	size = (sizeof(struct fastrpc_remote_arg) +
583c68cfb71SSrinivas Kandagatla 		sizeof(struct fastrpc_invoke_buf) +
584c68cfb71SSrinivas Kandagatla 		sizeof(struct fastrpc_phy_page)) * ctx->nscalars +
585c68cfb71SSrinivas Kandagatla 		sizeof(u64) * FASTRPC_MAX_FDLIST +
586c68cfb71SSrinivas Kandagatla 		sizeof(u32) * FASTRPC_MAX_CRCLIST;
587c68cfb71SSrinivas Kandagatla 
588c68cfb71SSrinivas Kandagatla 	return size;
589c68cfb71SSrinivas Kandagatla }
590c68cfb71SSrinivas Kandagatla 
591c68cfb71SSrinivas Kandagatla static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen)
592c68cfb71SSrinivas Kandagatla {
593c68cfb71SSrinivas Kandagatla 	u64 size = 0;
594c68cfb71SSrinivas Kandagatla 	int i;
595c68cfb71SSrinivas Kandagatla 
596c68cfb71SSrinivas Kandagatla 	size = ALIGN(metalen, FASTRPC_ALIGN);
597c68cfb71SSrinivas Kandagatla 	for (i = 0; i < ctx->nscalars; i++) {
598c68cfb71SSrinivas Kandagatla 		if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) {
599c68cfb71SSrinivas Kandagatla 			size = ALIGN(size, FASTRPC_ALIGN);
600c68cfb71SSrinivas Kandagatla 			size += ctx->args[i].length;
601c68cfb71SSrinivas Kandagatla 		}
602c68cfb71SSrinivas Kandagatla 	}
603c68cfb71SSrinivas Kandagatla 
604c68cfb71SSrinivas Kandagatla 	return size;
605c68cfb71SSrinivas Kandagatla }
606c68cfb71SSrinivas Kandagatla 
607c68cfb71SSrinivas Kandagatla static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
608c68cfb71SSrinivas Kandagatla {
609c68cfb71SSrinivas Kandagatla 	struct device *dev = ctx->fl->sctx->dev;
610c68cfb71SSrinivas Kandagatla 	int i, err;
611c68cfb71SSrinivas Kandagatla 
612c68cfb71SSrinivas Kandagatla 	for (i = 0; i < ctx->nscalars; ++i) {
613c68cfb71SSrinivas Kandagatla 		/* Make sure reserved field is set to 0 */
614c68cfb71SSrinivas Kandagatla 		if (ctx->args[i].reserved)
615c68cfb71SSrinivas Kandagatla 			return -EINVAL;
616c68cfb71SSrinivas Kandagatla 
617c68cfb71SSrinivas Kandagatla 		if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 ||
618c68cfb71SSrinivas Kandagatla 		    ctx->args[i].length == 0)
619c68cfb71SSrinivas Kandagatla 			continue;
620c68cfb71SSrinivas Kandagatla 
621c68cfb71SSrinivas Kandagatla 		err = fastrpc_map_create(ctx->fl, ctx->args[i].fd,
622c68cfb71SSrinivas Kandagatla 					 ctx->args[i].length, &ctx->maps[i]);
623c68cfb71SSrinivas Kandagatla 		if (err) {
624c68cfb71SSrinivas Kandagatla 			dev_err(dev, "Error Creating map %d\n", err);
625c68cfb71SSrinivas Kandagatla 			return -EINVAL;
626c68cfb71SSrinivas Kandagatla 		}
627c68cfb71SSrinivas Kandagatla 
628c68cfb71SSrinivas Kandagatla 	}
629c68cfb71SSrinivas Kandagatla 	return 0;
630c68cfb71SSrinivas Kandagatla }
631c68cfb71SSrinivas Kandagatla 
632c68cfb71SSrinivas Kandagatla static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
633c68cfb71SSrinivas Kandagatla {
634c68cfb71SSrinivas Kandagatla 	struct device *dev = ctx->fl->sctx->dev;
635c68cfb71SSrinivas Kandagatla 	struct fastrpc_remote_arg *rpra;
636c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_buf *list;
637c68cfb71SSrinivas Kandagatla 	struct fastrpc_phy_page *pages;
638c68cfb71SSrinivas Kandagatla 	int inbufs, i, err = 0;
639c68cfb71SSrinivas Kandagatla 	u64 rlen, pkt_size;
640c68cfb71SSrinivas Kandagatla 	uintptr_t args;
641c68cfb71SSrinivas Kandagatla 	int metalen;
642c68cfb71SSrinivas Kandagatla 
643c68cfb71SSrinivas Kandagatla 
644c68cfb71SSrinivas Kandagatla 	inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
645c68cfb71SSrinivas Kandagatla 	metalen = fastrpc_get_meta_size(ctx);
646c68cfb71SSrinivas Kandagatla 	pkt_size = fastrpc_get_payload_size(ctx, metalen);
647c68cfb71SSrinivas Kandagatla 
648c68cfb71SSrinivas Kandagatla 	err = fastrpc_create_maps(ctx);
649c68cfb71SSrinivas Kandagatla 	if (err)
650c68cfb71SSrinivas Kandagatla 		return err;
651c68cfb71SSrinivas Kandagatla 
652c68cfb71SSrinivas Kandagatla 	ctx->msg_sz = pkt_size;
653c68cfb71SSrinivas Kandagatla 
654c68cfb71SSrinivas Kandagatla 	err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf);
655c68cfb71SSrinivas Kandagatla 	if (err)
656c68cfb71SSrinivas Kandagatla 		return err;
657c68cfb71SSrinivas Kandagatla 
658c68cfb71SSrinivas Kandagatla 	rpra = ctx->buf->virt;
659c68cfb71SSrinivas Kandagatla 	list = ctx->buf->virt + ctx->nscalars * sizeof(*rpra);
660c68cfb71SSrinivas Kandagatla 	pages = ctx->buf->virt + ctx->nscalars * (sizeof(*list) +
661c68cfb71SSrinivas Kandagatla 		sizeof(*rpra));
662c68cfb71SSrinivas Kandagatla 	args = (uintptr_t)ctx->buf->virt + metalen;
663c68cfb71SSrinivas Kandagatla 	rlen = pkt_size - metalen;
664c68cfb71SSrinivas Kandagatla 	ctx->rpra = rpra;
665c68cfb71SSrinivas Kandagatla 
666c68cfb71SSrinivas Kandagatla 	for (i = 0; i < ctx->nbufs; ++i) {
667c68cfb71SSrinivas Kandagatla 		u64 len = ctx->args[i].length;
668c68cfb71SSrinivas Kandagatla 
669c68cfb71SSrinivas Kandagatla 		rpra[i].pv = 0;
670c68cfb71SSrinivas Kandagatla 		rpra[i].len = len;
671c68cfb71SSrinivas Kandagatla 		list[i].num = len ? 1 : 0;
672c68cfb71SSrinivas Kandagatla 		list[i].pgidx = i;
673c68cfb71SSrinivas Kandagatla 
674c68cfb71SSrinivas Kandagatla 		if (!len)
675c68cfb71SSrinivas Kandagatla 			continue;
676c68cfb71SSrinivas Kandagatla 
677c68cfb71SSrinivas Kandagatla 		pages[i].size = roundup(len, PAGE_SIZE);
678c68cfb71SSrinivas Kandagatla 
679c68cfb71SSrinivas Kandagatla 		if (ctx->maps[i]) {
680c68cfb71SSrinivas Kandagatla 			rpra[i].pv = (u64) ctx->args[i].ptr;
681c68cfb71SSrinivas Kandagatla 			pages[i].addr = ctx->maps[i]->phys;
682c68cfb71SSrinivas Kandagatla 		} else {
683c68cfb71SSrinivas Kandagatla 			rlen -= ALIGN(args, FASTRPC_ALIGN) - args;
684c68cfb71SSrinivas Kandagatla 			args = ALIGN(args, FASTRPC_ALIGN);
685c68cfb71SSrinivas Kandagatla 			if (rlen < len)
686c68cfb71SSrinivas Kandagatla 				goto bail;
687c68cfb71SSrinivas Kandagatla 
688c68cfb71SSrinivas Kandagatla 			rpra[i].pv = args;
689c68cfb71SSrinivas Kandagatla 			pages[i].addr = ctx->buf->phys + (pkt_size - rlen);
690c68cfb71SSrinivas Kandagatla 			pages[i].addr = pages[i].addr &	PAGE_MASK;
691c68cfb71SSrinivas Kandagatla 			args = args + len;
692c68cfb71SSrinivas Kandagatla 			rlen -= len;
693c68cfb71SSrinivas Kandagatla 		}
694c68cfb71SSrinivas Kandagatla 
695c68cfb71SSrinivas Kandagatla 		if (i < inbufs && !ctx->maps[i]) {
696c68cfb71SSrinivas Kandagatla 			void *dst = (void *)(uintptr_t)rpra[i].pv;
697c68cfb71SSrinivas Kandagatla 			void *src = (void *)(uintptr_t)ctx->args[i].ptr;
698c68cfb71SSrinivas Kandagatla 
699c68cfb71SSrinivas Kandagatla 			if (!kernel) {
700c68cfb71SSrinivas Kandagatla 				if (copy_from_user(dst, (void __user *)src,
701c68cfb71SSrinivas Kandagatla 						   len)) {
702c68cfb71SSrinivas Kandagatla 					err = -EFAULT;
703c68cfb71SSrinivas Kandagatla 					goto bail;
704c68cfb71SSrinivas Kandagatla 				}
705c68cfb71SSrinivas Kandagatla 			} else {
706c68cfb71SSrinivas Kandagatla 				memcpy(dst, src, len);
707c68cfb71SSrinivas Kandagatla 			}
708c68cfb71SSrinivas Kandagatla 		}
709c68cfb71SSrinivas Kandagatla 	}
710c68cfb71SSrinivas Kandagatla 
711c68cfb71SSrinivas Kandagatla 	for (i = ctx->nbufs; i < ctx->nscalars; ++i) {
712c68cfb71SSrinivas Kandagatla 		rpra[i].pv = (u64) ctx->args[i].ptr;
713c68cfb71SSrinivas Kandagatla 		rpra[i].len = ctx->args[i].length;
714c68cfb71SSrinivas Kandagatla 		list[i].num = ctx->args[i].length ? 1 : 0;
715c68cfb71SSrinivas Kandagatla 		list[i].pgidx = i;
716c68cfb71SSrinivas Kandagatla 		pages[i].addr = ctx->maps[i]->phys;
717c68cfb71SSrinivas Kandagatla 		pages[i].size = ctx->maps[i]->size;
718c68cfb71SSrinivas Kandagatla 	}
719c68cfb71SSrinivas Kandagatla 
720c68cfb71SSrinivas Kandagatla bail:
721c68cfb71SSrinivas Kandagatla 	if (err)
722c68cfb71SSrinivas Kandagatla 		dev_err(dev, "Error: get invoke args failed:%d\n", err);
723c68cfb71SSrinivas Kandagatla 
724c68cfb71SSrinivas Kandagatla 	return err;
725c68cfb71SSrinivas Kandagatla }
726c68cfb71SSrinivas Kandagatla 
727c68cfb71SSrinivas Kandagatla static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
728c68cfb71SSrinivas Kandagatla 			    u32 kernel)
729c68cfb71SSrinivas Kandagatla {
730c68cfb71SSrinivas Kandagatla 	struct fastrpc_remote_arg *rpra = ctx->rpra;
731c68cfb71SSrinivas Kandagatla 	int i, inbufs;
732c68cfb71SSrinivas Kandagatla 
733c68cfb71SSrinivas Kandagatla 	inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
734c68cfb71SSrinivas Kandagatla 
735c68cfb71SSrinivas Kandagatla 	for (i = inbufs; i < ctx->nbufs; ++i) {
736c68cfb71SSrinivas Kandagatla 		void *src = (void *)(uintptr_t)rpra[i].pv;
737c68cfb71SSrinivas Kandagatla 		void *dst = (void *)(uintptr_t)ctx->args[i].ptr;
738c68cfb71SSrinivas Kandagatla 		u64 len = rpra[i].len;
739c68cfb71SSrinivas Kandagatla 
740c68cfb71SSrinivas Kandagatla 		if (!kernel) {
741c68cfb71SSrinivas Kandagatla 			if (copy_to_user((void __user *)dst, src, len))
742c68cfb71SSrinivas Kandagatla 				return -EFAULT;
743c68cfb71SSrinivas Kandagatla 		} else {
744c68cfb71SSrinivas Kandagatla 			memcpy(dst, src, len);
745c68cfb71SSrinivas Kandagatla 		}
746c68cfb71SSrinivas Kandagatla 	}
747c68cfb71SSrinivas Kandagatla 
748c68cfb71SSrinivas Kandagatla 	return 0;
749c68cfb71SSrinivas Kandagatla }
750c68cfb71SSrinivas Kandagatla 
751c68cfb71SSrinivas Kandagatla static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx,
752c68cfb71SSrinivas Kandagatla 			       struct fastrpc_invoke_ctx *ctx,
753c68cfb71SSrinivas Kandagatla 			       u32 kernel, uint32_t handle)
754c68cfb71SSrinivas Kandagatla {
755c68cfb71SSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx;
756c68cfb71SSrinivas Kandagatla 	struct fastrpc_user *fl = ctx->fl;
757c68cfb71SSrinivas Kandagatla 	struct fastrpc_msg *msg = &ctx->msg;
758c68cfb71SSrinivas Kandagatla 
759c68cfb71SSrinivas Kandagatla 	cctx = fl->cctx;
760c68cfb71SSrinivas Kandagatla 	msg->pid = fl->tgid;
761c68cfb71SSrinivas Kandagatla 	msg->tid = current->pid;
762c68cfb71SSrinivas Kandagatla 
763c68cfb71SSrinivas Kandagatla 	if (kernel)
764c68cfb71SSrinivas Kandagatla 		msg->pid = 0;
765c68cfb71SSrinivas Kandagatla 
766c68cfb71SSrinivas Kandagatla 	msg->ctx = ctx->ctxid | fl->pd;
767c68cfb71SSrinivas Kandagatla 	msg->handle = handle;
768c68cfb71SSrinivas Kandagatla 	msg->sc = ctx->sc;
769c68cfb71SSrinivas Kandagatla 	msg->addr = ctx->buf ? ctx->buf->phys : 0;
770c68cfb71SSrinivas Kandagatla 	msg->size = roundup(ctx->msg_sz, PAGE_SIZE);
771c68cfb71SSrinivas Kandagatla 	fastrpc_context_get(ctx);
772c68cfb71SSrinivas Kandagatla 
773c68cfb71SSrinivas Kandagatla 	return rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg));
774c68cfb71SSrinivas Kandagatla }
775c68cfb71SSrinivas Kandagatla 
776c68cfb71SSrinivas Kandagatla static int fastrpc_internal_invoke(struct fastrpc_user *fl,  u32 kernel,
777c68cfb71SSrinivas Kandagatla 				   u32 handle, u32 sc,
778c68cfb71SSrinivas Kandagatla 				   struct fastrpc_invoke_args *args)
779c68cfb71SSrinivas Kandagatla {
780c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_ctx *ctx = NULL;
781c68cfb71SSrinivas Kandagatla 	int err = 0;
782c68cfb71SSrinivas Kandagatla 
783c68cfb71SSrinivas Kandagatla 	if (!fl->sctx)
784c68cfb71SSrinivas Kandagatla 		return -EINVAL;
785c68cfb71SSrinivas Kandagatla 
786c68cfb71SSrinivas Kandagatla 	ctx = fastrpc_context_alloc(fl, kernel, sc, args);
787c68cfb71SSrinivas Kandagatla 	if (IS_ERR(ctx))
788c68cfb71SSrinivas Kandagatla 		return PTR_ERR(ctx);
789c68cfb71SSrinivas Kandagatla 
790c68cfb71SSrinivas Kandagatla 	if (ctx->nscalars) {
791c68cfb71SSrinivas Kandagatla 		err = fastrpc_get_args(kernel, ctx);
792c68cfb71SSrinivas Kandagatla 		if (err)
793c68cfb71SSrinivas Kandagatla 			goto bail;
794c68cfb71SSrinivas Kandagatla 	}
795*415a0729SSrinivas Kandagatla 
796*415a0729SSrinivas Kandagatla 	/* make sure that all CPU memory writes are seen by DSP */
797*415a0729SSrinivas Kandagatla 	dma_wmb();
798c68cfb71SSrinivas Kandagatla 	/* Send invoke buffer to remote dsp */
799c68cfb71SSrinivas Kandagatla 	err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle);
800c68cfb71SSrinivas Kandagatla 	if (err)
801c68cfb71SSrinivas Kandagatla 		goto bail;
802c68cfb71SSrinivas Kandagatla 
803c68cfb71SSrinivas Kandagatla 	/* Wait for remote dsp to respond or time out */
804c68cfb71SSrinivas Kandagatla 	err = wait_for_completion_interruptible(&ctx->work);
805c68cfb71SSrinivas Kandagatla 	if (err)
806c68cfb71SSrinivas Kandagatla 		goto bail;
807c68cfb71SSrinivas Kandagatla 
808c68cfb71SSrinivas Kandagatla 	/* Check the response from remote dsp */
809c68cfb71SSrinivas Kandagatla 	err = ctx->retval;
810c68cfb71SSrinivas Kandagatla 	if (err)
811c68cfb71SSrinivas Kandagatla 		goto bail;
812c68cfb71SSrinivas Kandagatla 
813c68cfb71SSrinivas Kandagatla 	if (ctx->nscalars) {
814*415a0729SSrinivas Kandagatla 		/* make sure that all memory writes by DSP are seen by CPU */
815*415a0729SSrinivas Kandagatla 		dma_rmb();
816c68cfb71SSrinivas Kandagatla 		/* populate all the output buffers with results */
817c68cfb71SSrinivas Kandagatla 		err = fastrpc_put_args(ctx, kernel);
818c68cfb71SSrinivas Kandagatla 		if (err)
819c68cfb71SSrinivas Kandagatla 			goto bail;
820c68cfb71SSrinivas Kandagatla 	}
821c68cfb71SSrinivas Kandagatla 
822c68cfb71SSrinivas Kandagatla bail:
823c68cfb71SSrinivas Kandagatla 	/* We are done with this compute context, remove it from pending list */
824c68cfb71SSrinivas Kandagatla 	spin_lock(&fl->lock);
825c68cfb71SSrinivas Kandagatla 	list_del(&ctx->node);
826c68cfb71SSrinivas Kandagatla 	spin_unlock(&fl->lock);
827c68cfb71SSrinivas Kandagatla 	fastrpc_context_put(ctx);
828c68cfb71SSrinivas Kandagatla 
829c68cfb71SSrinivas Kandagatla 	if (err)
830c68cfb71SSrinivas Kandagatla 		dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err);
831c68cfb71SSrinivas Kandagatla 
832c68cfb71SSrinivas Kandagatla 	return err;
833c68cfb71SSrinivas Kandagatla }
834c68cfb71SSrinivas Kandagatla 
835d73f71c7SSrinivas Kandagatla static int fastrpc_init_create_process(struct fastrpc_user *fl,
836d73f71c7SSrinivas Kandagatla 					char __user *argp)
837d73f71c7SSrinivas Kandagatla {
838d73f71c7SSrinivas Kandagatla 	struct fastrpc_init_create init;
839d73f71c7SSrinivas Kandagatla 	struct fastrpc_invoke_args *args;
840d73f71c7SSrinivas Kandagatla 	struct fastrpc_phy_page pages[1];
841d73f71c7SSrinivas Kandagatla 	struct fastrpc_map *map = NULL;
842d73f71c7SSrinivas Kandagatla 	struct fastrpc_buf *imem = NULL;
843d73f71c7SSrinivas Kandagatla 	int memlen;
844d73f71c7SSrinivas Kandagatla 	int err;
845d73f71c7SSrinivas Kandagatla 	struct {
846d73f71c7SSrinivas Kandagatla 		int pgid;
847d73f71c7SSrinivas Kandagatla 		u32 namelen;
848d73f71c7SSrinivas Kandagatla 		u32 filelen;
849d73f71c7SSrinivas Kandagatla 		u32 pageslen;
850d73f71c7SSrinivas Kandagatla 		u32 attrs;
851d73f71c7SSrinivas Kandagatla 		u32 siglen;
852d73f71c7SSrinivas Kandagatla 	} inbuf;
853d73f71c7SSrinivas Kandagatla 	u32 sc;
854d73f71c7SSrinivas Kandagatla 
855d73f71c7SSrinivas Kandagatla 	args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL);
856d73f71c7SSrinivas Kandagatla 	if (!args)
857d73f71c7SSrinivas Kandagatla 		return -ENOMEM;
858d73f71c7SSrinivas Kandagatla 
859d73f71c7SSrinivas Kandagatla 	if (copy_from_user(&init, argp, sizeof(init))) {
860d73f71c7SSrinivas Kandagatla 		err = -EFAULT;
861b49f6d83SThierry Escande 		goto err;
862d73f71c7SSrinivas Kandagatla 	}
863d73f71c7SSrinivas Kandagatla 
864d73f71c7SSrinivas Kandagatla 	if (init.filelen > INIT_FILELEN_MAX) {
865d73f71c7SSrinivas Kandagatla 		err = -EINVAL;
866b49f6d83SThierry Escande 		goto err;
867d73f71c7SSrinivas Kandagatla 	}
868d73f71c7SSrinivas Kandagatla 
869d73f71c7SSrinivas Kandagatla 	inbuf.pgid = fl->tgid;
870d73f71c7SSrinivas Kandagatla 	inbuf.namelen = strlen(current->comm) + 1;
871d73f71c7SSrinivas Kandagatla 	inbuf.filelen = init.filelen;
872d73f71c7SSrinivas Kandagatla 	inbuf.pageslen = 1;
873d73f71c7SSrinivas Kandagatla 	inbuf.attrs = init.attrs;
874d73f71c7SSrinivas Kandagatla 	inbuf.siglen = init.siglen;
875d73f71c7SSrinivas Kandagatla 	fl->pd = 1;
876d73f71c7SSrinivas Kandagatla 
877d73f71c7SSrinivas Kandagatla 	if (init.filelen && init.filefd) {
878d73f71c7SSrinivas Kandagatla 		err = fastrpc_map_create(fl, init.filefd, init.filelen, &map);
879d73f71c7SSrinivas Kandagatla 		if (err)
880b49f6d83SThierry Escande 			goto err;
881d73f71c7SSrinivas Kandagatla 	}
882d73f71c7SSrinivas Kandagatla 
883d73f71c7SSrinivas Kandagatla 	memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4),
884d73f71c7SSrinivas Kandagatla 		       1024 * 1024);
885d73f71c7SSrinivas Kandagatla 	err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen,
886d73f71c7SSrinivas Kandagatla 				&imem);
887b49f6d83SThierry Escande 	if (err)
888b49f6d83SThierry Escande 		goto err_alloc;
889d73f71c7SSrinivas Kandagatla 
890d73f71c7SSrinivas Kandagatla 	fl->init_mem = imem;
891d73f71c7SSrinivas Kandagatla 	args[0].ptr = (u64)(uintptr_t)&inbuf;
892d73f71c7SSrinivas Kandagatla 	args[0].length = sizeof(inbuf);
893d73f71c7SSrinivas Kandagatla 	args[0].fd = -1;
894d73f71c7SSrinivas Kandagatla 
895d73f71c7SSrinivas Kandagatla 	args[1].ptr = (u64)(uintptr_t)current->comm;
896d73f71c7SSrinivas Kandagatla 	args[1].length = inbuf.namelen;
897d73f71c7SSrinivas Kandagatla 	args[1].fd = -1;
898d73f71c7SSrinivas Kandagatla 
899d73f71c7SSrinivas Kandagatla 	args[2].ptr = (u64) init.file;
900d73f71c7SSrinivas Kandagatla 	args[2].length = inbuf.filelen;
901d73f71c7SSrinivas Kandagatla 	args[2].fd = init.filefd;
902d73f71c7SSrinivas Kandagatla 
903d73f71c7SSrinivas Kandagatla 	pages[0].addr = imem->phys;
904d73f71c7SSrinivas Kandagatla 	pages[0].size = imem->size;
905d73f71c7SSrinivas Kandagatla 
906d73f71c7SSrinivas Kandagatla 	args[3].ptr = (u64)(uintptr_t) pages;
907d73f71c7SSrinivas Kandagatla 	args[3].length = 1 * sizeof(*pages);
908d73f71c7SSrinivas Kandagatla 	args[3].fd = -1;
909d73f71c7SSrinivas Kandagatla 
910d73f71c7SSrinivas Kandagatla 	args[4].ptr = (u64)(uintptr_t)&inbuf.attrs;
911d73f71c7SSrinivas Kandagatla 	args[4].length = sizeof(inbuf.attrs);
912d73f71c7SSrinivas Kandagatla 	args[4].fd = -1;
913d73f71c7SSrinivas Kandagatla 
914d73f71c7SSrinivas Kandagatla 	args[5].ptr = (u64)(uintptr_t) &inbuf.siglen;
915d73f71c7SSrinivas Kandagatla 	args[5].length = sizeof(inbuf.siglen);
916d73f71c7SSrinivas Kandagatla 	args[5].fd = -1;
917d73f71c7SSrinivas Kandagatla 
918d73f71c7SSrinivas Kandagatla 	sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0);
919d73f71c7SSrinivas Kandagatla 	if (init.attrs)
920d73f71c7SSrinivas Kandagatla 		sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0);
921d73f71c7SSrinivas Kandagatla 
922d73f71c7SSrinivas Kandagatla 	err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
923d73f71c7SSrinivas Kandagatla 				      sc, args);
924b49f6d83SThierry Escande 	if (err)
925b49f6d83SThierry Escande 		goto err_invoke;
926d73f71c7SSrinivas Kandagatla 
927b49f6d83SThierry Escande 	kfree(args);
928b49f6d83SThierry Escande 
929b49f6d83SThierry Escande 	return 0;
930b49f6d83SThierry Escande 
931b49f6d83SThierry Escande err_invoke:
932b49f6d83SThierry Escande 	fl->init_mem = NULL;
933d73f71c7SSrinivas Kandagatla 	fastrpc_buf_free(imem);
934b49f6d83SThierry Escande err_alloc:
935b49f6d83SThierry Escande 	if (map) {
936b49f6d83SThierry Escande 		spin_lock(&fl->lock);
937b49f6d83SThierry Escande 		list_del(&map->node);
938b49f6d83SThierry Escande 		spin_unlock(&fl->lock);
939b49f6d83SThierry Escande 		fastrpc_map_put(map);
940d73f71c7SSrinivas Kandagatla 	}
941b49f6d83SThierry Escande err:
942d73f71c7SSrinivas Kandagatla 	kfree(args);
943d73f71c7SSrinivas Kandagatla 
944d73f71c7SSrinivas Kandagatla 	return err;
945d73f71c7SSrinivas Kandagatla }
946d73f71c7SSrinivas Kandagatla 
947f6f9279fSSrinivas Kandagatla static struct fastrpc_session_ctx *fastrpc_session_alloc(
948f6f9279fSSrinivas Kandagatla 					struct fastrpc_channel_ctx *cctx)
949f6f9279fSSrinivas Kandagatla {
950f6f9279fSSrinivas Kandagatla 	struct fastrpc_session_ctx *session = NULL;
951f6f9279fSSrinivas Kandagatla 	int i;
952f6f9279fSSrinivas Kandagatla 
953f6f9279fSSrinivas Kandagatla 	spin_lock(&cctx->lock);
954f6f9279fSSrinivas Kandagatla 	for (i = 0; i < cctx->sesscount; i++) {
955f6f9279fSSrinivas Kandagatla 		if (!cctx->session[i].used && cctx->session[i].valid) {
956f6f9279fSSrinivas Kandagatla 			cctx->session[i].used = true;
957f6f9279fSSrinivas Kandagatla 			session = &cctx->session[i];
958f6f9279fSSrinivas Kandagatla 			break;
959f6f9279fSSrinivas Kandagatla 		}
960f6f9279fSSrinivas Kandagatla 	}
961f6f9279fSSrinivas Kandagatla 	spin_unlock(&cctx->lock);
962f6f9279fSSrinivas Kandagatla 
963f6f9279fSSrinivas Kandagatla 	return session;
964f6f9279fSSrinivas Kandagatla }
965f6f9279fSSrinivas Kandagatla 
966f6f9279fSSrinivas Kandagatla static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx,
967f6f9279fSSrinivas Kandagatla 				 struct fastrpc_session_ctx *session)
968f6f9279fSSrinivas Kandagatla {
969f6f9279fSSrinivas Kandagatla 	spin_lock(&cctx->lock);
970f6f9279fSSrinivas Kandagatla 	session->used = false;
971f6f9279fSSrinivas Kandagatla 	spin_unlock(&cctx->lock);
972f6f9279fSSrinivas Kandagatla }
973f6f9279fSSrinivas Kandagatla 
974d73f71c7SSrinivas Kandagatla static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl)
975d73f71c7SSrinivas Kandagatla {
976d73f71c7SSrinivas Kandagatla 	struct fastrpc_invoke_args args[1];
977d73f71c7SSrinivas Kandagatla 	int tgid = 0;
978d73f71c7SSrinivas Kandagatla 	u32 sc;
979d73f71c7SSrinivas Kandagatla 
980d73f71c7SSrinivas Kandagatla 	tgid = fl->tgid;
981d73f71c7SSrinivas Kandagatla 	args[0].ptr = (u64)(uintptr_t) &tgid;
982d73f71c7SSrinivas Kandagatla 	args[0].length = sizeof(tgid);
983d73f71c7SSrinivas Kandagatla 	args[0].fd = -1;
984d73f71c7SSrinivas Kandagatla 	args[0].reserved = 0;
985d73f71c7SSrinivas Kandagatla 	sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0);
986d73f71c7SSrinivas Kandagatla 
987d73f71c7SSrinivas Kandagatla 	return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
988d73f71c7SSrinivas Kandagatla 				       sc, &args[0]);
989d73f71c7SSrinivas Kandagatla }
990d73f71c7SSrinivas Kandagatla 
991f6f9279fSSrinivas Kandagatla static int fastrpc_device_release(struct inode *inode, struct file *file)
992f6f9279fSSrinivas Kandagatla {
993f6f9279fSSrinivas Kandagatla 	struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
994f6f9279fSSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx = fl->cctx;
995c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_ctx *ctx, *n;
996c68cfb71SSrinivas Kandagatla 	struct fastrpc_map *map, *m;
997f6f9279fSSrinivas Kandagatla 
998d73f71c7SSrinivas Kandagatla 	fastrpc_release_current_dsp_process(fl);
999d73f71c7SSrinivas Kandagatla 
1000f6f9279fSSrinivas Kandagatla 	spin_lock(&cctx->lock);
1001f6f9279fSSrinivas Kandagatla 	list_del(&fl->user);
1002f6f9279fSSrinivas Kandagatla 	spin_unlock(&cctx->lock);
1003f6f9279fSSrinivas Kandagatla 
1004c68cfb71SSrinivas Kandagatla 	if (fl->init_mem)
1005c68cfb71SSrinivas Kandagatla 		fastrpc_buf_free(fl->init_mem);
1006c68cfb71SSrinivas Kandagatla 
1007c68cfb71SSrinivas Kandagatla 	list_for_each_entry_safe(ctx, n, &fl->pending, node) {
1008c68cfb71SSrinivas Kandagatla 		list_del(&ctx->node);
1009c68cfb71SSrinivas Kandagatla 		fastrpc_context_put(ctx);
1010c68cfb71SSrinivas Kandagatla 	}
1011c68cfb71SSrinivas Kandagatla 
1012c68cfb71SSrinivas Kandagatla 	list_for_each_entry_safe(map, m, &fl->maps, node) {
1013c68cfb71SSrinivas Kandagatla 		list_del(&map->node);
1014c68cfb71SSrinivas Kandagatla 		fastrpc_map_put(map);
1015c68cfb71SSrinivas Kandagatla 	}
1016c68cfb71SSrinivas Kandagatla 
1017f6f9279fSSrinivas Kandagatla 	fastrpc_session_free(cctx, fl->sctx);
1018f6f9279fSSrinivas Kandagatla 
1019f6f9279fSSrinivas Kandagatla 	mutex_destroy(&fl->mutex);
1020f6f9279fSSrinivas Kandagatla 	kfree(fl);
1021f6f9279fSSrinivas Kandagatla 	file->private_data = NULL;
1022f6f9279fSSrinivas Kandagatla 
1023f6f9279fSSrinivas Kandagatla 	return 0;
1024f6f9279fSSrinivas Kandagatla }
1025f6f9279fSSrinivas Kandagatla 
1026f6f9279fSSrinivas Kandagatla static int fastrpc_device_open(struct inode *inode, struct file *filp)
1027f6f9279fSSrinivas Kandagatla {
1028f6f9279fSSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx = miscdev_to_cctx(filp->private_data);
1029f6f9279fSSrinivas Kandagatla 	struct fastrpc_user *fl = NULL;
1030f6f9279fSSrinivas Kandagatla 
1031f6f9279fSSrinivas Kandagatla 	fl = kzalloc(sizeof(*fl), GFP_KERNEL);
1032f6f9279fSSrinivas Kandagatla 	if (!fl)
1033f6f9279fSSrinivas Kandagatla 		return -ENOMEM;
1034f6f9279fSSrinivas Kandagatla 
1035f6f9279fSSrinivas Kandagatla 	filp->private_data = fl;
1036f6f9279fSSrinivas Kandagatla 	spin_lock_init(&fl->lock);
1037f6f9279fSSrinivas Kandagatla 	mutex_init(&fl->mutex);
1038f6f9279fSSrinivas Kandagatla 	INIT_LIST_HEAD(&fl->pending);
1039f6f9279fSSrinivas Kandagatla 	INIT_LIST_HEAD(&fl->maps);
1040f6f9279fSSrinivas Kandagatla 	INIT_LIST_HEAD(&fl->user);
1041f6f9279fSSrinivas Kandagatla 	fl->tgid = current->tgid;
1042f6f9279fSSrinivas Kandagatla 	fl->cctx = cctx;
10437c11df42SThierry Escande 
10447c11df42SThierry Escande 	fl->sctx = fastrpc_session_alloc(cctx);
10457c11df42SThierry Escande 	if (!fl->sctx) {
10467c11df42SThierry Escande 		dev_err(&cctx->rpdev->dev, "No session available\n");
10477c11df42SThierry Escande 		mutex_destroy(&fl->mutex);
10487c11df42SThierry Escande 		kfree(fl);
10497c11df42SThierry Escande 
10507c11df42SThierry Escande 		return -EBUSY;
10517c11df42SThierry Escande 	}
10527c11df42SThierry Escande 
1053f6f9279fSSrinivas Kandagatla 	spin_lock(&cctx->lock);
1054f6f9279fSSrinivas Kandagatla 	list_add_tail(&fl->user, &cctx->users);
1055f6f9279fSSrinivas Kandagatla 	spin_unlock(&cctx->lock);
1056f6f9279fSSrinivas Kandagatla 
1057f6f9279fSSrinivas Kandagatla 	return 0;
1058f6f9279fSSrinivas Kandagatla }
1059f6f9279fSSrinivas Kandagatla 
10606cffd795SSrinivas Kandagatla static int fastrpc_dmabuf_free(struct fastrpc_user *fl, char __user *argp)
10616cffd795SSrinivas Kandagatla {
10626cffd795SSrinivas Kandagatla 	struct dma_buf *buf;
10636cffd795SSrinivas Kandagatla 	int info;
10646cffd795SSrinivas Kandagatla 
10656cffd795SSrinivas Kandagatla 	if (copy_from_user(&info, argp, sizeof(info)))
10666cffd795SSrinivas Kandagatla 		return -EFAULT;
10676cffd795SSrinivas Kandagatla 
10686cffd795SSrinivas Kandagatla 	buf = dma_buf_get(info);
10696cffd795SSrinivas Kandagatla 	if (IS_ERR_OR_NULL(buf))
10706cffd795SSrinivas Kandagatla 		return -EINVAL;
10716cffd795SSrinivas Kandagatla 	/*
10726cffd795SSrinivas Kandagatla 	 * one for the last get and other for the ALLOC_DMA_BUFF ioctl
10736cffd795SSrinivas Kandagatla 	 */
10746cffd795SSrinivas Kandagatla 	dma_buf_put(buf);
10756cffd795SSrinivas Kandagatla 	dma_buf_put(buf);
10766cffd795SSrinivas Kandagatla 
10776cffd795SSrinivas Kandagatla 	return 0;
10786cffd795SSrinivas Kandagatla }
10796cffd795SSrinivas Kandagatla 
10806cffd795SSrinivas Kandagatla static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
10816cffd795SSrinivas Kandagatla {
10826cffd795SSrinivas Kandagatla 	struct fastrpc_alloc_dma_buf bp;
10836cffd795SSrinivas Kandagatla 	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
10846cffd795SSrinivas Kandagatla 	struct fastrpc_buf *buf = NULL;
10856cffd795SSrinivas Kandagatla 	int err;
10866cffd795SSrinivas Kandagatla 
10876cffd795SSrinivas Kandagatla 	if (copy_from_user(&bp, argp, sizeof(bp)))
10886cffd795SSrinivas Kandagatla 		return -EFAULT;
10896cffd795SSrinivas Kandagatla 
10906cffd795SSrinivas Kandagatla 	err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf);
10916cffd795SSrinivas Kandagatla 	if (err)
10926cffd795SSrinivas Kandagatla 		return err;
10936cffd795SSrinivas Kandagatla 	exp_info.ops = &fastrpc_dma_buf_ops;
10946cffd795SSrinivas Kandagatla 	exp_info.size = bp.size;
10956cffd795SSrinivas Kandagatla 	exp_info.flags = O_RDWR;
10966cffd795SSrinivas Kandagatla 	exp_info.priv = buf;
10976cffd795SSrinivas Kandagatla 	buf->dmabuf = dma_buf_export(&exp_info);
10986cffd795SSrinivas Kandagatla 	if (IS_ERR(buf->dmabuf)) {
10996cffd795SSrinivas Kandagatla 		err = PTR_ERR(buf->dmabuf);
11006cffd795SSrinivas Kandagatla 		fastrpc_buf_free(buf);
11016cffd795SSrinivas Kandagatla 		return err;
11026cffd795SSrinivas Kandagatla 	}
11036cffd795SSrinivas Kandagatla 
11046cffd795SSrinivas Kandagatla 	bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE);
11056cffd795SSrinivas Kandagatla 	if (bp.fd < 0) {
11066cffd795SSrinivas Kandagatla 		dma_buf_put(buf->dmabuf);
11076cffd795SSrinivas Kandagatla 		return -EINVAL;
11086cffd795SSrinivas Kandagatla 	}
11096cffd795SSrinivas Kandagatla 
11106cffd795SSrinivas Kandagatla 	if (copy_to_user(argp, &bp, sizeof(bp))) {
11116cffd795SSrinivas Kandagatla 		dma_buf_put(buf->dmabuf);
11126cffd795SSrinivas Kandagatla 		return -EFAULT;
11136cffd795SSrinivas Kandagatla 	}
11146cffd795SSrinivas Kandagatla 
11156cffd795SSrinivas Kandagatla 	get_dma_buf(buf->dmabuf);
11166cffd795SSrinivas Kandagatla 
11176cffd795SSrinivas Kandagatla 	return 0;
11186cffd795SSrinivas Kandagatla }
11196cffd795SSrinivas Kandagatla 
1120d73f71c7SSrinivas Kandagatla static int fastrpc_init_attach(struct fastrpc_user *fl)
1121d73f71c7SSrinivas Kandagatla {
1122d73f71c7SSrinivas Kandagatla 	struct fastrpc_invoke_args args[1];
1123d73f71c7SSrinivas Kandagatla 	int tgid = fl->tgid;
1124d73f71c7SSrinivas Kandagatla 	u32 sc;
1125d73f71c7SSrinivas Kandagatla 
1126d73f71c7SSrinivas Kandagatla 	args[0].ptr = (u64)(uintptr_t) &tgid;
1127d73f71c7SSrinivas Kandagatla 	args[0].length = sizeof(tgid);
1128d73f71c7SSrinivas Kandagatla 	args[0].fd = -1;
1129d73f71c7SSrinivas Kandagatla 	args[0].reserved = 0;
1130d73f71c7SSrinivas Kandagatla 	sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0);
1131d73f71c7SSrinivas Kandagatla 	fl->pd = 0;
1132d73f71c7SSrinivas Kandagatla 
1133d73f71c7SSrinivas Kandagatla 	return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1134d73f71c7SSrinivas Kandagatla 				       sc, &args[0]);
1135d73f71c7SSrinivas Kandagatla }
1136d73f71c7SSrinivas Kandagatla 
1137c68cfb71SSrinivas Kandagatla static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp)
1138c68cfb71SSrinivas Kandagatla {
1139c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_args *args = NULL;
1140c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke inv;
1141c68cfb71SSrinivas Kandagatla 	u32 nscalars;
1142c68cfb71SSrinivas Kandagatla 	int err;
1143c68cfb71SSrinivas Kandagatla 
1144c68cfb71SSrinivas Kandagatla 	if (copy_from_user(&inv, argp, sizeof(inv)))
1145c68cfb71SSrinivas Kandagatla 		return -EFAULT;
1146c68cfb71SSrinivas Kandagatla 
1147c68cfb71SSrinivas Kandagatla 	/* nscalars is truncated here to max supported value */
1148c68cfb71SSrinivas Kandagatla 	nscalars = REMOTE_SCALARS_LENGTH(inv.sc);
1149c68cfb71SSrinivas Kandagatla 	if (nscalars) {
1150c68cfb71SSrinivas Kandagatla 		args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL);
1151c68cfb71SSrinivas Kandagatla 		if (!args)
1152c68cfb71SSrinivas Kandagatla 			return -ENOMEM;
1153c68cfb71SSrinivas Kandagatla 
1154c68cfb71SSrinivas Kandagatla 		if (copy_from_user(args, (void __user *)(uintptr_t)inv.args,
1155c68cfb71SSrinivas Kandagatla 				   nscalars * sizeof(*args))) {
1156c68cfb71SSrinivas Kandagatla 			kfree(args);
1157c68cfb71SSrinivas Kandagatla 			return -EFAULT;
1158c68cfb71SSrinivas Kandagatla 		}
1159c68cfb71SSrinivas Kandagatla 	}
1160c68cfb71SSrinivas Kandagatla 
1161c68cfb71SSrinivas Kandagatla 	err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args);
1162c68cfb71SSrinivas Kandagatla 	kfree(args);
1163c68cfb71SSrinivas Kandagatla 
1164c68cfb71SSrinivas Kandagatla 	return err;
1165c68cfb71SSrinivas Kandagatla }
1166c68cfb71SSrinivas Kandagatla 
1167c68cfb71SSrinivas Kandagatla static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
1168c68cfb71SSrinivas Kandagatla 				 unsigned long arg)
1169c68cfb71SSrinivas Kandagatla {
1170c68cfb71SSrinivas Kandagatla 	struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1171c68cfb71SSrinivas Kandagatla 	char __user *argp = (char __user *)arg;
1172c68cfb71SSrinivas Kandagatla 	int err;
1173c68cfb71SSrinivas Kandagatla 
1174c68cfb71SSrinivas Kandagatla 	switch (cmd) {
1175c68cfb71SSrinivas Kandagatla 	case FASTRPC_IOCTL_INVOKE:
1176c68cfb71SSrinivas Kandagatla 		err = fastrpc_invoke(fl, argp);
1177c68cfb71SSrinivas Kandagatla 		break;
1178d73f71c7SSrinivas Kandagatla 	case FASTRPC_IOCTL_INIT_ATTACH:
1179d73f71c7SSrinivas Kandagatla 		err = fastrpc_init_attach(fl);
1180d73f71c7SSrinivas Kandagatla 		break;
1181d73f71c7SSrinivas Kandagatla 	case FASTRPC_IOCTL_INIT_CREATE:
1182d73f71c7SSrinivas Kandagatla 		err = fastrpc_init_create_process(fl, argp);
1183d73f71c7SSrinivas Kandagatla 		break;
11846cffd795SSrinivas Kandagatla 	case FASTRPC_IOCTL_FREE_DMA_BUFF:
11856cffd795SSrinivas Kandagatla 		err = fastrpc_dmabuf_free(fl, argp);
11866cffd795SSrinivas Kandagatla 		break;
11876cffd795SSrinivas Kandagatla 	case FASTRPC_IOCTL_ALLOC_DMA_BUFF:
11886cffd795SSrinivas Kandagatla 		err = fastrpc_dmabuf_alloc(fl, argp);
11896cffd795SSrinivas Kandagatla 		break;
1190c68cfb71SSrinivas Kandagatla 	default:
1191c68cfb71SSrinivas Kandagatla 		err = -ENOTTY;
1192c68cfb71SSrinivas Kandagatla 		break;
1193c68cfb71SSrinivas Kandagatla 	}
1194c68cfb71SSrinivas Kandagatla 
1195c68cfb71SSrinivas Kandagatla 	return err;
1196c68cfb71SSrinivas Kandagatla }
1197c68cfb71SSrinivas Kandagatla 
1198f6f9279fSSrinivas Kandagatla static const struct file_operations fastrpc_fops = {
1199f6f9279fSSrinivas Kandagatla 	.open = fastrpc_device_open,
1200f6f9279fSSrinivas Kandagatla 	.release = fastrpc_device_release,
1201c68cfb71SSrinivas Kandagatla 	.unlocked_ioctl = fastrpc_device_ioctl,
1202c68cfb71SSrinivas Kandagatla 	.compat_ioctl = fastrpc_device_ioctl,
1203f6f9279fSSrinivas Kandagatla };
1204f6f9279fSSrinivas Kandagatla 
1205f6f9279fSSrinivas Kandagatla static int fastrpc_cb_probe(struct platform_device *pdev)
1206f6f9279fSSrinivas Kandagatla {
1207f6f9279fSSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx;
1208f6f9279fSSrinivas Kandagatla 	struct fastrpc_session_ctx *sess;
1209f6f9279fSSrinivas Kandagatla 	struct device *dev = &pdev->dev;
1210f6f9279fSSrinivas Kandagatla 	int i, sessions = 0;
1211f6f9279fSSrinivas Kandagatla 
1212f6f9279fSSrinivas Kandagatla 	cctx = dev_get_drvdata(dev->parent);
1213f6f9279fSSrinivas Kandagatla 	if (!cctx)
1214f6f9279fSSrinivas Kandagatla 		return -EINVAL;
1215f6f9279fSSrinivas Kandagatla 
1216f6f9279fSSrinivas Kandagatla 	of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions);
1217f6f9279fSSrinivas Kandagatla 
1218f6f9279fSSrinivas Kandagatla 	spin_lock(&cctx->lock);
1219f6f9279fSSrinivas Kandagatla 	sess = &cctx->session[cctx->sesscount];
1220f6f9279fSSrinivas Kandagatla 	sess->used = false;
1221f6f9279fSSrinivas Kandagatla 	sess->valid = true;
1222f6f9279fSSrinivas Kandagatla 	sess->dev = dev;
1223f6f9279fSSrinivas Kandagatla 	dev_set_drvdata(dev, sess);
1224f6f9279fSSrinivas Kandagatla 
1225f6f9279fSSrinivas Kandagatla 	if (of_property_read_u32(dev->of_node, "reg", &sess->sid))
1226f6f9279fSSrinivas Kandagatla 		dev_info(dev, "FastRPC Session ID not specified in DT\n");
1227f6f9279fSSrinivas Kandagatla 
1228f6f9279fSSrinivas Kandagatla 	if (sessions > 0) {
1229f6f9279fSSrinivas Kandagatla 		struct fastrpc_session_ctx *dup_sess;
1230f6f9279fSSrinivas Kandagatla 
1231f6f9279fSSrinivas Kandagatla 		for (i = 1; i < sessions; i++) {
1232f6f9279fSSrinivas Kandagatla 			if (cctx->sesscount++ >= FASTRPC_MAX_SESSIONS)
1233f6f9279fSSrinivas Kandagatla 				break;
1234f6f9279fSSrinivas Kandagatla 			dup_sess = &cctx->session[cctx->sesscount];
1235f6f9279fSSrinivas Kandagatla 			memcpy(dup_sess, sess, sizeof(*dup_sess));
1236f6f9279fSSrinivas Kandagatla 		}
1237f6f9279fSSrinivas Kandagatla 	}
1238f6f9279fSSrinivas Kandagatla 	cctx->sesscount++;
1239f6f9279fSSrinivas Kandagatla 	spin_unlock(&cctx->lock);
1240f6f9279fSSrinivas Kandagatla 	dma_set_mask(dev, DMA_BIT_MASK(32));
1241f6f9279fSSrinivas Kandagatla 
1242f6f9279fSSrinivas Kandagatla 	return 0;
1243f6f9279fSSrinivas Kandagatla }
1244f6f9279fSSrinivas Kandagatla 
1245f6f9279fSSrinivas Kandagatla static int fastrpc_cb_remove(struct platform_device *pdev)
1246f6f9279fSSrinivas Kandagatla {
1247f6f9279fSSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent);
1248f6f9279fSSrinivas Kandagatla 	struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev);
1249f6f9279fSSrinivas Kandagatla 	int i;
1250f6f9279fSSrinivas Kandagatla 
1251f6f9279fSSrinivas Kandagatla 	spin_lock(&cctx->lock);
1252f6f9279fSSrinivas Kandagatla 	for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) {
1253f6f9279fSSrinivas Kandagatla 		if (cctx->session[i].sid == sess->sid) {
1254f6f9279fSSrinivas Kandagatla 			cctx->session[i].valid = false;
1255f6f9279fSSrinivas Kandagatla 			cctx->sesscount--;
1256f6f9279fSSrinivas Kandagatla 		}
1257f6f9279fSSrinivas Kandagatla 	}
1258f6f9279fSSrinivas Kandagatla 	spin_unlock(&cctx->lock);
1259f6f9279fSSrinivas Kandagatla 
1260f6f9279fSSrinivas Kandagatla 	return 0;
1261f6f9279fSSrinivas Kandagatla }
1262f6f9279fSSrinivas Kandagatla 
1263f6f9279fSSrinivas Kandagatla static const struct of_device_id fastrpc_match_table[] = {
1264f6f9279fSSrinivas Kandagatla 	{ .compatible = "qcom,fastrpc-compute-cb", },
1265f6f9279fSSrinivas Kandagatla 	{}
1266f6f9279fSSrinivas Kandagatla };
1267f6f9279fSSrinivas Kandagatla 
1268f6f9279fSSrinivas Kandagatla static struct platform_driver fastrpc_cb_driver = {
1269f6f9279fSSrinivas Kandagatla 	.probe = fastrpc_cb_probe,
1270f6f9279fSSrinivas Kandagatla 	.remove = fastrpc_cb_remove,
1271f6f9279fSSrinivas Kandagatla 	.driver = {
1272f6f9279fSSrinivas Kandagatla 		.name = "qcom,fastrpc-cb",
1273f6f9279fSSrinivas Kandagatla 		.of_match_table = fastrpc_match_table,
1274f6f9279fSSrinivas Kandagatla 		.suppress_bind_attrs = true,
1275f6f9279fSSrinivas Kandagatla 	},
1276f6f9279fSSrinivas Kandagatla };
1277f6f9279fSSrinivas Kandagatla 
1278f6f9279fSSrinivas Kandagatla static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
1279f6f9279fSSrinivas Kandagatla {
1280f6f9279fSSrinivas Kandagatla 	struct device *rdev = &rpdev->dev;
1281f6f9279fSSrinivas Kandagatla 	struct fastrpc_channel_ctx *data;
1282f6f9279fSSrinivas Kandagatla 	int i, err, domain_id = -1;
1283f6f9279fSSrinivas Kandagatla 	const char *domain;
1284f6f9279fSSrinivas Kandagatla 
1285f6f9279fSSrinivas Kandagatla 	data = devm_kzalloc(rdev, sizeof(*data), GFP_KERNEL);
1286f6f9279fSSrinivas Kandagatla 	if (!data)
1287f6f9279fSSrinivas Kandagatla 		return -ENOMEM;
1288f6f9279fSSrinivas Kandagatla 
1289f6f9279fSSrinivas Kandagatla 	err = of_property_read_string(rdev->of_node, "label", &domain);
1290f6f9279fSSrinivas Kandagatla 	if (err) {
1291f6f9279fSSrinivas Kandagatla 		dev_info(rdev, "FastRPC Domain not specified in DT\n");
1292f6f9279fSSrinivas Kandagatla 		return err;
1293f6f9279fSSrinivas Kandagatla 	}
1294f6f9279fSSrinivas Kandagatla 
1295f6f9279fSSrinivas Kandagatla 	for (i = 0; i <= CDSP_DOMAIN_ID; i++) {
1296f6f9279fSSrinivas Kandagatla 		if (!strcmp(domains[i], domain)) {
1297f6f9279fSSrinivas Kandagatla 			domain_id = i;
1298f6f9279fSSrinivas Kandagatla 			break;
1299f6f9279fSSrinivas Kandagatla 		}
1300f6f9279fSSrinivas Kandagatla 	}
1301f6f9279fSSrinivas Kandagatla 
1302f6f9279fSSrinivas Kandagatla 	if (domain_id < 0) {
1303f6f9279fSSrinivas Kandagatla 		dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id);
1304f6f9279fSSrinivas Kandagatla 		return -EINVAL;
1305f6f9279fSSrinivas Kandagatla 	}
1306f6f9279fSSrinivas Kandagatla 
1307f6f9279fSSrinivas Kandagatla 	data->miscdev.minor = MISC_DYNAMIC_MINOR;
1308f6f9279fSSrinivas Kandagatla 	data->miscdev.name = kasprintf(GFP_KERNEL, "fastrpc-%s",
1309f6f9279fSSrinivas Kandagatla 				domains[domain_id]);
1310f6f9279fSSrinivas Kandagatla 	data->miscdev.fops = &fastrpc_fops;
1311f6f9279fSSrinivas Kandagatla 	err = misc_register(&data->miscdev);
1312f6f9279fSSrinivas Kandagatla 	if (err)
1313f6f9279fSSrinivas Kandagatla 		return err;
1314f6f9279fSSrinivas Kandagatla 
1315f6f9279fSSrinivas Kandagatla 	dev_set_drvdata(&rpdev->dev, data);
1316f6f9279fSSrinivas Kandagatla 	dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
1317f6f9279fSSrinivas Kandagatla 	INIT_LIST_HEAD(&data->users);
1318f6f9279fSSrinivas Kandagatla 	spin_lock_init(&data->lock);
1319f6f9279fSSrinivas Kandagatla 	idr_init(&data->ctx_idr);
1320f6f9279fSSrinivas Kandagatla 	data->domain_id = domain_id;
1321f6f9279fSSrinivas Kandagatla 	data->rpdev = rpdev;
1322f6f9279fSSrinivas Kandagatla 
1323f6f9279fSSrinivas Kandagatla 	return of_platform_populate(rdev->of_node, NULL, NULL, rdev);
1324f6f9279fSSrinivas Kandagatla }
1325f6f9279fSSrinivas Kandagatla 
1326c68cfb71SSrinivas Kandagatla static void fastrpc_notify_users(struct fastrpc_user *user)
1327c68cfb71SSrinivas Kandagatla {
1328c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_ctx *ctx;
1329c68cfb71SSrinivas Kandagatla 
1330c68cfb71SSrinivas Kandagatla 	spin_lock(&user->lock);
1331c68cfb71SSrinivas Kandagatla 	list_for_each_entry(ctx, &user->pending, node)
1332c68cfb71SSrinivas Kandagatla 		complete(&ctx->work);
1333c68cfb71SSrinivas Kandagatla 	spin_unlock(&user->lock);
1334c68cfb71SSrinivas Kandagatla }
1335c68cfb71SSrinivas Kandagatla 
1336f6f9279fSSrinivas Kandagatla static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
1337f6f9279fSSrinivas Kandagatla {
1338f6f9279fSSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
1339c68cfb71SSrinivas Kandagatla 	struct fastrpc_user *user;
1340c68cfb71SSrinivas Kandagatla 
1341c68cfb71SSrinivas Kandagatla 	spin_lock(&cctx->lock);
1342c68cfb71SSrinivas Kandagatla 	list_for_each_entry(user, &cctx->users, user)
1343c68cfb71SSrinivas Kandagatla 		fastrpc_notify_users(user);
1344c68cfb71SSrinivas Kandagatla 	spin_unlock(&cctx->lock);
1345f6f9279fSSrinivas Kandagatla 
1346f6f9279fSSrinivas Kandagatla 	misc_deregister(&cctx->miscdev);
1347f6f9279fSSrinivas Kandagatla 	of_platform_depopulate(&rpdev->dev);
1348f6f9279fSSrinivas Kandagatla 	kfree(cctx);
1349f6f9279fSSrinivas Kandagatla }
1350f6f9279fSSrinivas Kandagatla 
1351f6f9279fSSrinivas Kandagatla static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
1352f6f9279fSSrinivas Kandagatla 				  int len, void *priv, u32 addr)
1353f6f9279fSSrinivas Kandagatla {
1354c68cfb71SSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
1355c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_rsp *rsp = data;
1356c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_ctx *ctx;
1357c68cfb71SSrinivas Kandagatla 	unsigned long flags;
1358c68cfb71SSrinivas Kandagatla 	unsigned long ctxid;
1359c68cfb71SSrinivas Kandagatla 
1360c68cfb71SSrinivas Kandagatla 	if (len < sizeof(*rsp))
1361c68cfb71SSrinivas Kandagatla 		return -EINVAL;
1362c68cfb71SSrinivas Kandagatla 
1363c68cfb71SSrinivas Kandagatla 	ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4);
1364c68cfb71SSrinivas Kandagatla 
1365c68cfb71SSrinivas Kandagatla 	spin_lock_irqsave(&cctx->lock, flags);
1366c68cfb71SSrinivas Kandagatla 	ctx = idr_find(&cctx->ctx_idr, ctxid);
1367c68cfb71SSrinivas Kandagatla 	spin_unlock_irqrestore(&cctx->lock, flags);
1368c68cfb71SSrinivas Kandagatla 
1369c68cfb71SSrinivas Kandagatla 	if (!ctx) {
1370c68cfb71SSrinivas Kandagatla 		dev_err(&rpdev->dev, "No context ID matches response\n");
1371c68cfb71SSrinivas Kandagatla 		return -ENOENT;
1372c68cfb71SSrinivas Kandagatla 	}
1373c68cfb71SSrinivas Kandagatla 
1374c68cfb71SSrinivas Kandagatla 	ctx->retval = rsp->retval;
1375c68cfb71SSrinivas Kandagatla 	complete(&ctx->work);
13768e7389c7SThierry Escande 
13778e7389c7SThierry Escande 	/*
13788e7389c7SThierry Escande 	 * The DMA buffer associated with the context cannot be freed in
13798e7389c7SThierry Escande 	 * interrupt context so schedule it through a worker thread to
13808e7389c7SThierry Escande 	 * avoid a kernel BUG.
13818e7389c7SThierry Escande 	 */
13828e7389c7SThierry Escande 	schedule_work(&ctx->put_work);
1383c68cfb71SSrinivas Kandagatla 
1384f6f9279fSSrinivas Kandagatla 	return 0;
1385f6f9279fSSrinivas Kandagatla }
1386f6f9279fSSrinivas Kandagatla 
1387f6f9279fSSrinivas Kandagatla static const struct of_device_id fastrpc_rpmsg_of_match[] = {
1388f6f9279fSSrinivas Kandagatla 	{ .compatible = "qcom,fastrpc" },
1389f6f9279fSSrinivas Kandagatla 	{ },
1390f6f9279fSSrinivas Kandagatla };
1391f6f9279fSSrinivas Kandagatla MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match);
1392f6f9279fSSrinivas Kandagatla 
1393f6f9279fSSrinivas Kandagatla static struct rpmsg_driver fastrpc_driver = {
1394f6f9279fSSrinivas Kandagatla 	.probe = fastrpc_rpmsg_probe,
1395f6f9279fSSrinivas Kandagatla 	.remove = fastrpc_rpmsg_remove,
1396f6f9279fSSrinivas Kandagatla 	.callback = fastrpc_rpmsg_callback,
1397f6f9279fSSrinivas Kandagatla 	.drv = {
1398f6f9279fSSrinivas Kandagatla 		.name = "qcom,fastrpc",
1399f6f9279fSSrinivas Kandagatla 		.of_match_table = fastrpc_rpmsg_of_match,
1400f6f9279fSSrinivas Kandagatla 	},
1401f6f9279fSSrinivas Kandagatla };
1402f6f9279fSSrinivas Kandagatla 
1403f6f9279fSSrinivas Kandagatla static int fastrpc_init(void)
1404f6f9279fSSrinivas Kandagatla {
1405f6f9279fSSrinivas Kandagatla 	int ret;
1406f6f9279fSSrinivas Kandagatla 
1407f6f9279fSSrinivas Kandagatla 	ret = platform_driver_register(&fastrpc_cb_driver);
1408f6f9279fSSrinivas Kandagatla 	if (ret < 0) {
1409f6f9279fSSrinivas Kandagatla 		pr_err("fastrpc: failed to register cb driver\n");
1410f6f9279fSSrinivas Kandagatla 		return ret;
1411f6f9279fSSrinivas Kandagatla 	}
1412f6f9279fSSrinivas Kandagatla 
1413f6f9279fSSrinivas Kandagatla 	ret = register_rpmsg_driver(&fastrpc_driver);
1414f6f9279fSSrinivas Kandagatla 	if (ret < 0) {
1415f6f9279fSSrinivas Kandagatla 		pr_err("fastrpc: failed to register rpmsg driver\n");
1416f6f9279fSSrinivas Kandagatla 		platform_driver_unregister(&fastrpc_cb_driver);
1417f6f9279fSSrinivas Kandagatla 		return ret;
1418f6f9279fSSrinivas Kandagatla 	}
1419f6f9279fSSrinivas Kandagatla 
1420f6f9279fSSrinivas Kandagatla 	return 0;
1421f6f9279fSSrinivas Kandagatla }
1422f6f9279fSSrinivas Kandagatla module_init(fastrpc_init);
1423f6f9279fSSrinivas Kandagatla 
1424f6f9279fSSrinivas Kandagatla static void fastrpc_exit(void)
1425f6f9279fSSrinivas Kandagatla {
1426f6f9279fSSrinivas Kandagatla 	platform_driver_unregister(&fastrpc_cb_driver);
1427f6f9279fSSrinivas Kandagatla 	unregister_rpmsg_driver(&fastrpc_driver);
1428f6f9279fSSrinivas Kandagatla }
1429f6f9279fSSrinivas Kandagatla module_exit(fastrpc_exit);
1430f6f9279fSSrinivas Kandagatla 
1431f6f9279fSSrinivas Kandagatla MODULE_LICENSE("GPL v2");
1432