xref: /openbmc/linux/drivers/misc/fastrpc.c (revision 3c7d0079a1831118ef232bd9c2f34d058a1f31c2)
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>
9265751a5SDmitry Osipenko #include <linux/dma-resv.h>
10f6f9279fSSrinivas Kandagatla #include <linux/idr.h>
11f6f9279fSSrinivas Kandagatla #include <linux/list.h>
12f6f9279fSSrinivas Kandagatla #include <linux/miscdevice.h>
13f6f9279fSSrinivas Kandagatla #include <linux/module.h>
14f6f9279fSSrinivas Kandagatla #include <linux/of_address.h>
15f6f9279fSSrinivas Kandagatla #include <linux/of.h>
1625e8dfb8SSrinivas Kandagatla #include <linux/sort.h>
17f6f9279fSSrinivas Kandagatla #include <linux/of_platform.h>
18f6f9279fSSrinivas Kandagatla #include <linux/rpmsg.h>
19f6f9279fSSrinivas Kandagatla #include <linux/scatterlist.h>
20f6f9279fSSrinivas Kandagatla #include <linux/slab.h>
213bf90ecaSElliot Berman #include <linux/firmware/qcom/qcom_scm.h>
22c68cfb71SSrinivas Kandagatla #include <uapi/misc/fastrpc.h>
231ce91d45SAbel Vesa #include <linux/of_reserved_mem.h>
24f6f9279fSSrinivas Kandagatla 
25f6f9279fSSrinivas Kandagatla #define ADSP_DOMAIN_ID (0)
26f6f9279fSSrinivas Kandagatla #define MDSP_DOMAIN_ID (1)
27f6f9279fSSrinivas Kandagatla #define SDSP_DOMAIN_ID (2)
28f6f9279fSSrinivas Kandagatla #define CDSP_DOMAIN_ID (3)
29f6f9279fSSrinivas Kandagatla #define FASTRPC_DEV_MAX		4 /* adsp, mdsp, slpi, cdsp*/
30689a2d9fSJohan Hovold #define FASTRPC_MAX_SESSIONS	14
31e90d9119SVamsi Krishna Gattupalli #define FASTRPC_MAX_VMIDS	16
32c68cfb71SSrinivas Kandagatla #define FASTRPC_ALIGN		128
33c68cfb71SSrinivas Kandagatla #define FASTRPC_MAX_FDLIST	16
34c68cfb71SSrinivas Kandagatla #define FASTRPC_MAX_CRCLIST	64
35c68cfb71SSrinivas Kandagatla #define FASTRPC_PHYS(p)	((p) & 0xffffffff)
36f6f9279fSSrinivas Kandagatla #define FASTRPC_CTX_MAX (256)
37d73f71c7SSrinivas Kandagatla #define FASTRPC_INIT_HANDLE	1
386c16fd8bSJeya R #define FASTRPC_DSP_UTILITIES_HANDLE	2
39f6f9279fSSrinivas Kandagatla #define FASTRPC_CTXID_MASK (0xFF0)
40efcd2390SJorge Ramirez-Ortiz #define INIT_FILELEN_MAX (2 * 1024 * 1024)
4108715610SAbel Vesa #define INIT_FILE_NAMELEN_MAX (128)
42f6f9279fSSrinivas Kandagatla #define FASTRPC_DEVICE_NAME	"fastrpc"
4308715610SAbel Vesa 
4408715610SAbel Vesa /* Add memory to static PD pool, protection thru XPU */
4508715610SAbel Vesa #define ADSP_MMAP_HEAP_ADDR  4
4608715610SAbel Vesa /* MAP static DMA buffer on DSP User PD */
4708715610SAbel Vesa #define ADSP_MMAP_DMA_BUFFER  6
4808715610SAbel Vesa /* Add memory to static PD pool protection thru hypervisor */
4908715610SAbel Vesa #define ADSP_MMAP_REMOTE_HEAP_ADDR  8
5008715610SAbel Vesa /* Add memory to userPD pool, for user heap */
512419e55eSJorge Ramirez-Ortiz #define ADSP_MMAP_ADD_PAGES 0x1000
5208715610SAbel Vesa /* Add memory to userPD pool, for LLC heap */
5308715610SAbel Vesa #define ADSP_MMAP_ADD_PAGES_LLC 0x3000,
5408715610SAbel Vesa 
556c16fd8bSJeya R #define DSP_UNSUPPORTED_API (0x80000414)
566c16fd8bSJeya R /* MAX NUMBER of DSP ATTRIBUTES SUPPORTED */
576c16fd8bSJeya R #define FASTRPC_MAX_DSP_ATTRIBUTES (256)
586c16fd8bSJeya R #define FASTRPC_MAX_DSP_ATTRIBUTES_LEN (sizeof(u32) * FASTRPC_MAX_DSP_ATTRIBUTES)
59f6f9279fSSrinivas Kandagatla 
60c68cfb71SSrinivas Kandagatla /* Retrives number of input buffers from the scalars parameter */
61c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_INBUFS(sc)	(((sc) >> 16) & 0x0ff)
62c68cfb71SSrinivas Kandagatla 
63c68cfb71SSrinivas Kandagatla /* Retrives number of output buffers from the scalars parameter */
64c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_OUTBUFS(sc)	(((sc) >> 8) & 0x0ff)
65c68cfb71SSrinivas Kandagatla 
66c68cfb71SSrinivas Kandagatla /* Retrives number of input handles from the scalars parameter */
67c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_INHANDLES(sc)	(((sc) >> 4) & 0x0f)
68c68cfb71SSrinivas Kandagatla 
69c68cfb71SSrinivas Kandagatla /* Retrives number of output handles from the scalars parameter */
70c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_OUTHANDLES(sc)	((sc) & 0x0f)
71c68cfb71SSrinivas Kandagatla 
72c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_LENGTH(sc)	(REMOTE_SCALARS_INBUFS(sc) +   \
73c68cfb71SSrinivas Kandagatla 					 REMOTE_SCALARS_OUTBUFS(sc) +  \
74c68cfb71SSrinivas Kandagatla 					 REMOTE_SCALARS_INHANDLES(sc)+ \
75c68cfb71SSrinivas Kandagatla 					 REMOTE_SCALARS_OUTHANDLES(sc))
76c68cfb71SSrinivas Kandagatla #define FASTRPC_BUILD_SCALARS(attr, method, in, out, oin, oout)  \
77c68cfb71SSrinivas Kandagatla 				(((attr & 0x07) << 29) |		\
78c68cfb71SSrinivas Kandagatla 				((method & 0x1f) << 24) |	\
79c68cfb71SSrinivas Kandagatla 				((in & 0xff) << 16) |		\
80c68cfb71SSrinivas Kandagatla 				((out & 0xff) <<  8) |		\
81c68cfb71SSrinivas Kandagatla 				((oin & 0x0f) <<  4) |		\
82c68cfb71SSrinivas Kandagatla 				(oout & 0x0f))
83c68cfb71SSrinivas Kandagatla 
84c68cfb71SSrinivas Kandagatla #define FASTRPC_SCALARS(method, in, out) \
85c68cfb71SSrinivas Kandagatla 		FASTRPC_BUILD_SCALARS(0, method, in, out, 0, 0)
86c68cfb71SSrinivas Kandagatla 
87d73f71c7SSrinivas Kandagatla #define FASTRPC_CREATE_PROCESS_NARGS	6
8808715610SAbel Vesa #define FASTRPC_CREATE_STATIC_PROCESS_NARGS	3
89d73f71c7SSrinivas Kandagatla /* Remote Method id table */
90d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_ATTACH	0
91d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_RELEASE	1
922419e55eSJorge Ramirez-Ortiz #define FASTRPC_RMID_INIT_MMAP		4
932419e55eSJorge Ramirez-Ortiz #define FASTRPC_RMID_INIT_MUNMAP	5
94d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE	6
95d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE_ATTR	7
96d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE_STATIC	8
975c1b97c7SJeya R #define FASTRPC_RMID_INIT_MEM_MAP      10
985c1b97c7SJeya R #define FASTRPC_RMID_INIT_MEM_UNMAP    11
99d73f71c7SSrinivas Kandagatla 
10084195d20SJonathan Marek /* Protection Domain(PD) ids */
1011959ab9eSAbel Vesa #define ROOT_PD		(0)
10284195d20SJonathan Marek #define USER_PD		(1)
10384195d20SJonathan Marek #define SENSORS_PD	(2)
10484195d20SJonathan Marek 
105965602eaSSrinivas Kandagatla #define miscdev_to_fdevice(d) container_of(d, struct fastrpc_device, miscdev)
106f6f9279fSSrinivas Kandagatla 
107f6f9279fSSrinivas Kandagatla static const char *domains[FASTRPC_DEV_MAX] = { "adsp", "mdsp",
108f6f9279fSSrinivas Kandagatla 						"sdsp", "cdsp"};
109c68cfb71SSrinivas Kandagatla struct fastrpc_phy_page {
110c68cfb71SSrinivas Kandagatla 	u64 addr;		/* physical address */
111c68cfb71SSrinivas Kandagatla 	u64 size;		/* size of contiguous region */
112c68cfb71SSrinivas Kandagatla };
113c68cfb71SSrinivas Kandagatla 
114c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_buf {
115c68cfb71SSrinivas Kandagatla 	u32 num;		/* number of contiguous regions */
116c68cfb71SSrinivas Kandagatla 	u32 pgidx;		/* index to start of contiguous region */
117c68cfb71SSrinivas Kandagatla };
118c68cfb71SSrinivas Kandagatla 
11935a82b87SVamsi Krishna Gattupalli struct fastrpc_remote_dmahandle {
12035a82b87SVamsi Krishna Gattupalli 	s32 fd;		/* dma handle fd */
12135a82b87SVamsi Krishna Gattupalli 	u32 offset;	/* dma handle offset */
12235a82b87SVamsi Krishna Gattupalli 	u32 len;	/* dma handle length */
12335a82b87SVamsi Krishna Gattupalli };
12435a82b87SVamsi Krishna Gattupalli 
12535a82b87SVamsi Krishna Gattupalli struct fastrpc_remote_buf {
12635a82b87SVamsi Krishna Gattupalli 	u64 pv;		/* buffer pointer */
12735a82b87SVamsi Krishna Gattupalli 	u64 len;	/* length of buffer */
12835a82b87SVamsi Krishna Gattupalli };
12935a82b87SVamsi Krishna Gattupalli 
13035a82b87SVamsi Krishna Gattupalli union fastrpc_remote_arg {
13135a82b87SVamsi Krishna Gattupalli 	struct fastrpc_remote_buf buf;
13235a82b87SVamsi Krishna Gattupalli 	struct fastrpc_remote_dmahandle dma;
133c68cfb71SSrinivas Kandagatla };
134c68cfb71SSrinivas Kandagatla 
1352419e55eSJorge Ramirez-Ortiz struct fastrpc_mmap_rsp_msg {
1362419e55eSJorge Ramirez-Ortiz 	u64 vaddr;
1372419e55eSJorge Ramirez-Ortiz };
1382419e55eSJorge Ramirez-Ortiz 
1392419e55eSJorge Ramirez-Ortiz struct fastrpc_mmap_req_msg {
1402419e55eSJorge Ramirez-Ortiz 	s32 pgid;
1412419e55eSJorge Ramirez-Ortiz 	u32 flags;
1422419e55eSJorge Ramirez-Ortiz 	u64 vaddr;
1432419e55eSJorge Ramirez-Ortiz 	s32 num;
1442419e55eSJorge Ramirez-Ortiz };
1452419e55eSJorge Ramirez-Ortiz 
1465c1b97c7SJeya R struct fastrpc_mem_map_req_msg {
1475c1b97c7SJeya R 	s32 pgid;
1485c1b97c7SJeya R 	s32 fd;
1495c1b97c7SJeya R 	s32 offset;
1505c1b97c7SJeya R 	u32 flags;
1515c1b97c7SJeya R 	u64 vaddrin;
1525c1b97c7SJeya R 	s32 num;
1535c1b97c7SJeya R 	s32 data_len;
1545c1b97c7SJeya R };
1555c1b97c7SJeya R 
1562419e55eSJorge Ramirez-Ortiz struct fastrpc_munmap_req_msg {
1572419e55eSJorge Ramirez-Ortiz 	s32 pgid;
1582419e55eSJorge Ramirez-Ortiz 	u64 vaddr;
1592419e55eSJorge Ramirez-Ortiz 	u64 size;
1602419e55eSJorge Ramirez-Ortiz };
1612419e55eSJorge Ramirez-Ortiz 
1625c1b97c7SJeya R struct fastrpc_mem_unmap_req_msg {
1635c1b97c7SJeya R 	s32 pgid;
1645c1b97c7SJeya R 	s32 fd;
1655c1b97c7SJeya R 	u64 vaddrin;
1665c1b97c7SJeya R 	u64 len;
1675c1b97c7SJeya R };
1685c1b97c7SJeya R 
169c68cfb71SSrinivas Kandagatla struct fastrpc_msg {
170c68cfb71SSrinivas Kandagatla 	int pid;		/* process group id */
171c68cfb71SSrinivas Kandagatla 	int tid;		/* thread id */
172c68cfb71SSrinivas Kandagatla 	u64 ctx;		/* invoke caller context */
173c68cfb71SSrinivas Kandagatla 	u32 handle;	/* handle to invoke */
174c68cfb71SSrinivas Kandagatla 	u32 sc;		/* scalars structure describing the data */
175c68cfb71SSrinivas Kandagatla 	u64 addr;		/* physical address */
176c68cfb71SSrinivas Kandagatla 	u64 size;		/* size of contiguous region */
177c68cfb71SSrinivas Kandagatla };
178c68cfb71SSrinivas Kandagatla 
179c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_rsp {
180c68cfb71SSrinivas Kandagatla 	u64 ctx;		/* invoke caller context */
181c68cfb71SSrinivas Kandagatla 	int retval;		/* invoke return value */
182c68cfb71SSrinivas Kandagatla };
183c68cfb71SSrinivas Kandagatla 
18425e8dfb8SSrinivas Kandagatla struct fastrpc_buf_overlap {
18525e8dfb8SSrinivas Kandagatla 	u64 start;
18625e8dfb8SSrinivas Kandagatla 	u64 end;
18725e8dfb8SSrinivas Kandagatla 	int raix;
18825e8dfb8SSrinivas Kandagatla 	u64 mstart;
18925e8dfb8SSrinivas Kandagatla 	u64 mend;
19025e8dfb8SSrinivas Kandagatla 	u64 offset;
19125e8dfb8SSrinivas Kandagatla };
19225e8dfb8SSrinivas Kandagatla 
193c68cfb71SSrinivas Kandagatla struct fastrpc_buf {
194c68cfb71SSrinivas Kandagatla 	struct fastrpc_user *fl;
1956cffd795SSrinivas Kandagatla 	struct dma_buf *dmabuf;
196c68cfb71SSrinivas Kandagatla 	struct device *dev;
197c68cfb71SSrinivas Kandagatla 	void *virt;
198c68cfb71SSrinivas Kandagatla 	u64 phys;
199c68cfb71SSrinivas Kandagatla 	u64 size;
2006cffd795SSrinivas Kandagatla 	/* Lock for dma buf attachments */
2016cffd795SSrinivas Kandagatla 	struct mutex lock;
2026cffd795SSrinivas Kandagatla 	struct list_head attachments;
2032419e55eSJorge Ramirez-Ortiz 	/* mmap support */
2042419e55eSJorge Ramirez-Ortiz 	struct list_head node; /* list of user requested mmaps */
2052419e55eSJorge Ramirez-Ortiz 	uintptr_t raddr;
2066cffd795SSrinivas Kandagatla };
2076cffd795SSrinivas Kandagatla 
2086cffd795SSrinivas Kandagatla struct fastrpc_dma_buf_attachment {
2096cffd795SSrinivas Kandagatla 	struct device *dev;
2106cffd795SSrinivas Kandagatla 	struct sg_table sgt;
2116cffd795SSrinivas Kandagatla 	struct list_head node;
212c68cfb71SSrinivas Kandagatla };
213c68cfb71SSrinivas Kandagatla 
214c68cfb71SSrinivas Kandagatla struct fastrpc_map {
215c68cfb71SSrinivas Kandagatla 	struct list_head node;
216c68cfb71SSrinivas Kandagatla 	struct fastrpc_user *fl;
217c68cfb71SSrinivas Kandagatla 	int fd;
218c68cfb71SSrinivas Kandagatla 	struct dma_buf *buf;
219c68cfb71SSrinivas Kandagatla 	struct sg_table *table;
220c68cfb71SSrinivas Kandagatla 	struct dma_buf_attachment *attach;
221c68cfb71SSrinivas Kandagatla 	u64 phys;
222c68cfb71SSrinivas Kandagatla 	u64 size;
223c68cfb71SSrinivas Kandagatla 	void *va;
224c68cfb71SSrinivas Kandagatla 	u64 len;
2255c1b97c7SJeya R 	u64 raddr;
226e90d9119SVamsi Krishna Gattupalli 	u32 attr;
227c68cfb71SSrinivas Kandagatla 	struct kref refcount;
228c68cfb71SSrinivas Kandagatla };
229c68cfb71SSrinivas Kandagatla 
230c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx {
231c68cfb71SSrinivas Kandagatla 	int nscalars;
232c68cfb71SSrinivas Kandagatla 	int nbufs;
233c68cfb71SSrinivas Kandagatla 	int retval;
234c68cfb71SSrinivas Kandagatla 	int pid;
235c68cfb71SSrinivas Kandagatla 	int tgid;
236c68cfb71SSrinivas Kandagatla 	u32 sc;
237c68cfb71SSrinivas Kandagatla 	u32 *crc;
238c68cfb71SSrinivas Kandagatla 	u64 ctxid;
239c68cfb71SSrinivas Kandagatla 	u64 msg_sz;
240c68cfb71SSrinivas Kandagatla 	struct kref refcount;
241c68cfb71SSrinivas Kandagatla 	struct list_head node; /* list of ctxs */
242c68cfb71SSrinivas Kandagatla 	struct completion work;
2438e7389c7SThierry Escande 	struct work_struct put_work;
244c68cfb71SSrinivas Kandagatla 	struct fastrpc_msg msg;
245c68cfb71SSrinivas Kandagatla 	struct fastrpc_user *fl;
24635a82b87SVamsi Krishna Gattupalli 	union fastrpc_remote_arg *rpra;
247c68cfb71SSrinivas Kandagatla 	struct fastrpc_map **maps;
248c68cfb71SSrinivas Kandagatla 	struct fastrpc_buf *buf;
249c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_args *args;
25025e8dfb8SSrinivas Kandagatla 	struct fastrpc_buf_overlap *olaps;
251c68cfb71SSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx;
252c68cfb71SSrinivas Kandagatla };
253f6f9279fSSrinivas Kandagatla 
254f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx {
255f6f9279fSSrinivas Kandagatla 	struct device *dev;
256f6f9279fSSrinivas Kandagatla 	int sid;
257f6f9279fSSrinivas Kandagatla 	bool used;
258f6f9279fSSrinivas Kandagatla 	bool valid;
259f6f9279fSSrinivas Kandagatla };
260f6f9279fSSrinivas Kandagatla 
261f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx {
262f6f9279fSSrinivas Kandagatla 	int domain_id;
263f6f9279fSSrinivas Kandagatla 	int sesscount;
264e90d9119SVamsi Krishna Gattupalli 	int vmcount;
265968a26a0SElliot Berman 	u64 perms;
266e90d9119SVamsi Krishna Gattupalli 	struct qcom_scm_vmperm vmperms[FASTRPC_MAX_VMIDS];
267f6f9279fSSrinivas Kandagatla 	struct rpmsg_device *rpdev;
268f6f9279fSSrinivas Kandagatla 	struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS];
269f6f9279fSSrinivas Kandagatla 	spinlock_t lock;
270f6f9279fSSrinivas Kandagatla 	struct idr ctx_idr;
271f6f9279fSSrinivas Kandagatla 	struct list_head users;
272278d56f9SBjorn Andersson 	struct kref refcount;
2736c16fd8bSJeya R 	/* Flag if dsp attributes are cached */
2746c16fd8bSJeya R 	bool valid_attributes;
2756c16fd8bSJeya R 	u32 dsp_attributes[FASTRPC_MAX_DSP_ATTRIBUTES];
2763abe3ab3SSrinivas Kandagatla 	struct fastrpc_device *secure_fdevice;
277965602eaSSrinivas Kandagatla 	struct fastrpc_device *fdevice;
27808715610SAbel Vesa 	struct fastrpc_buf *remote_heap;
27976e8e4acSAbel Vesa 	struct list_head invoke_interrupted_mmaps;
2803abe3ab3SSrinivas Kandagatla 	bool secure;
2817f1f4812SJeya R 	bool unsigned_support;
2829bde43a0SAbel Vesa 	u64 dma_mask;
283965602eaSSrinivas Kandagatla };
284965602eaSSrinivas Kandagatla 
285965602eaSSrinivas Kandagatla struct fastrpc_device {
286965602eaSSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx;
287965602eaSSrinivas Kandagatla 	struct miscdevice miscdev;
2883abe3ab3SSrinivas Kandagatla 	bool secure;
289f6f9279fSSrinivas Kandagatla };
290f6f9279fSSrinivas Kandagatla 
291f6f9279fSSrinivas Kandagatla struct fastrpc_user {
292f6f9279fSSrinivas Kandagatla 	struct list_head user;
293f6f9279fSSrinivas Kandagatla 	struct list_head maps;
294f6f9279fSSrinivas Kandagatla 	struct list_head pending;
2952419e55eSJorge Ramirez-Ortiz 	struct list_head mmaps;
296f6f9279fSSrinivas Kandagatla 
297f6f9279fSSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx;
298f6f9279fSSrinivas Kandagatla 	struct fastrpc_session_ctx *sctx;
299c68cfb71SSrinivas Kandagatla 	struct fastrpc_buf *init_mem;
300f6f9279fSSrinivas Kandagatla 
301f6f9279fSSrinivas Kandagatla 	int tgid;
302f6f9279fSSrinivas Kandagatla 	int pd;
3037f1f4812SJeya R 	bool is_secure_dev;
304f6f9279fSSrinivas Kandagatla 	/* Lock for lists */
305f6f9279fSSrinivas Kandagatla 	spinlock_t lock;
306f6f9279fSSrinivas Kandagatla 	/* lock for allocations */
307f6f9279fSSrinivas Kandagatla 	struct mutex mutex;
308f6f9279fSSrinivas Kandagatla };
309f6f9279fSSrinivas Kandagatla 
310c68cfb71SSrinivas Kandagatla static void fastrpc_free_map(struct kref *ref)
311c68cfb71SSrinivas Kandagatla {
312c68cfb71SSrinivas Kandagatla 	struct fastrpc_map *map;
313c68cfb71SSrinivas Kandagatla 
314c68cfb71SSrinivas Kandagatla 	map = container_of(ref, struct fastrpc_map, refcount);
315c68cfb71SSrinivas Kandagatla 
316c68cfb71SSrinivas Kandagatla 	if (map->table) {
317e90d9119SVamsi Krishna Gattupalli 		if (map->attr & FASTRPC_ATTR_SECUREMAP) {
318e90d9119SVamsi Krishna Gattupalli 			struct qcom_scm_vmperm perm;
319a6e766deSEkansh Gupta 			int vmid = map->fl->cctx->vmperms[0].vmid;
320a6e766deSEkansh Gupta 			u64 src_perms = BIT(QCOM_SCM_VMID_HLOS) | BIT(vmid);
321e90d9119SVamsi Krishna Gattupalli 			int err = 0;
322e90d9119SVamsi Krishna Gattupalli 
323e90d9119SVamsi Krishna Gattupalli 			perm.vmid = QCOM_SCM_VMID_HLOS;
324e90d9119SVamsi Krishna Gattupalli 			perm.perm = QCOM_SCM_PERM_RWX;
325e90d9119SVamsi Krishna Gattupalli 			err = qcom_scm_assign_mem(map->phys, map->size,
326a6e766deSEkansh Gupta 				&src_perms, &perm, 1);
327e90d9119SVamsi Krishna Gattupalli 			if (err) {
328e90d9119SVamsi Krishna Gattupalli 				dev_err(map->fl->sctx->dev, "Failed to assign memory phys 0x%llx size 0x%llx err %d",
329e90d9119SVamsi Krishna Gattupalli 						map->phys, map->size, err);
330e90d9119SVamsi Krishna Gattupalli 				return;
331e90d9119SVamsi Krishna Gattupalli 			}
332e90d9119SVamsi Krishna Gattupalli 		}
333791da5c7SDmitry Osipenko 		dma_buf_unmap_attachment_unlocked(map->attach, map->table,
334c68cfb71SSrinivas Kandagatla 						  DMA_BIDIRECTIONAL);
335c68cfb71SSrinivas Kandagatla 		dma_buf_detach(map->buf, map->attach);
336c68cfb71SSrinivas Kandagatla 		dma_buf_put(map->buf);
337c68cfb71SSrinivas Kandagatla 	}
338c68cfb71SSrinivas Kandagatla 
3395bb96c8fSAbel Vesa 	if (map->fl) {
3405bb96c8fSAbel Vesa 		spin_lock(&map->fl->lock);
3415bb96c8fSAbel Vesa 		list_del(&map->node);
3425bb96c8fSAbel Vesa 		spin_unlock(&map->fl->lock);
3435bb96c8fSAbel Vesa 		map->fl = NULL;
3445bb96c8fSAbel Vesa 	}
3455bb96c8fSAbel Vesa 
346c68cfb71SSrinivas Kandagatla 	kfree(map);
347c68cfb71SSrinivas Kandagatla }
348c68cfb71SSrinivas Kandagatla 
349c68cfb71SSrinivas Kandagatla static void fastrpc_map_put(struct fastrpc_map *map)
350c68cfb71SSrinivas Kandagatla {
351c68cfb71SSrinivas Kandagatla 	if (map)
352c68cfb71SSrinivas Kandagatla 		kref_put(&map->refcount, fastrpc_free_map);
353c68cfb71SSrinivas Kandagatla }
354c68cfb71SSrinivas Kandagatla 
35596b328d1SOla Jeppsson static int fastrpc_map_get(struct fastrpc_map *map)
356c68cfb71SSrinivas Kandagatla {
35796b328d1SOla Jeppsson 	if (!map)
35896b328d1SOla Jeppsson 		return -ENOENT;
35996b328d1SOla Jeppsson 
36096b328d1SOla Jeppsson 	return kref_get_unless_zero(&map->refcount) ? 0 : -ENOENT;
361c68cfb71SSrinivas Kandagatla }
362c68cfb71SSrinivas Kandagatla 
3638f6c1d8cSVamsi Krishna Gattupalli 
3648f6c1d8cSVamsi Krishna Gattupalli static int fastrpc_map_lookup(struct fastrpc_user *fl, int fd,
3659446fa16SAbel Vesa 			    struct fastrpc_map **ppmap, bool take_ref)
366c68cfb71SSrinivas Kandagatla {
3679446fa16SAbel Vesa 	struct fastrpc_session_ctx *sess = fl->sctx;
368c68cfb71SSrinivas Kandagatla 	struct fastrpc_map *map = NULL;
3699446fa16SAbel Vesa 	int ret = -ENOENT;
370c68cfb71SSrinivas Kandagatla 
3719446fa16SAbel Vesa 	spin_lock(&fl->lock);
372c68cfb71SSrinivas Kandagatla 	list_for_each_entry(map, &fl->maps, node) {
3739446fa16SAbel Vesa 		if (map->fd != fd)
3749446fa16SAbel Vesa 			continue;
3759446fa16SAbel Vesa 
3769446fa16SAbel Vesa 		if (take_ref) {
3779446fa16SAbel Vesa 			ret = fastrpc_map_get(map);
3789446fa16SAbel Vesa 			if (ret) {
3799446fa16SAbel Vesa 				dev_dbg(sess->dev, "%s: Failed to get map fd=%d ret=%d\n",
3809446fa16SAbel Vesa 					__func__, fd, ret);
3819446fa16SAbel Vesa 				break;
3829446fa16SAbel Vesa 			}
3839446fa16SAbel Vesa 		}
3849446fa16SAbel Vesa 
385c68cfb71SSrinivas Kandagatla 		*ppmap = map;
3869446fa16SAbel Vesa 		ret = 0;
3879446fa16SAbel Vesa 		break;
388c68cfb71SSrinivas Kandagatla 	}
3899446fa16SAbel Vesa 	spin_unlock(&fl->lock);
3908f6c1d8cSVamsi Krishna Gattupalli 
3918f6c1d8cSVamsi Krishna Gattupalli 	return ret;
3928f6c1d8cSVamsi Krishna Gattupalli }
3938f6c1d8cSVamsi Krishna Gattupalli 
394c68cfb71SSrinivas Kandagatla static void fastrpc_buf_free(struct fastrpc_buf *buf)
395c68cfb71SSrinivas Kandagatla {
396c68cfb71SSrinivas Kandagatla 	dma_free_coherent(buf->dev, buf->size, buf->virt,
397c68cfb71SSrinivas Kandagatla 			  FASTRPC_PHYS(buf->phys));
398c68cfb71SSrinivas Kandagatla 	kfree(buf);
399c68cfb71SSrinivas Kandagatla }
400c68cfb71SSrinivas Kandagatla 
4016f18c7e8SAbel Vesa static int __fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
402c68cfb71SSrinivas Kandagatla 			     u64 size, struct fastrpc_buf **obuf)
403c68cfb71SSrinivas Kandagatla {
404c68cfb71SSrinivas Kandagatla 	struct fastrpc_buf *buf;
405c68cfb71SSrinivas Kandagatla 
406c68cfb71SSrinivas Kandagatla 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
407c68cfb71SSrinivas Kandagatla 	if (!buf)
408c68cfb71SSrinivas Kandagatla 		return -ENOMEM;
409c68cfb71SSrinivas Kandagatla 
4106cffd795SSrinivas Kandagatla 	INIT_LIST_HEAD(&buf->attachments);
4112419e55eSJorge Ramirez-Ortiz 	INIT_LIST_HEAD(&buf->node);
4126cffd795SSrinivas Kandagatla 	mutex_init(&buf->lock);
4136cffd795SSrinivas Kandagatla 
414c68cfb71SSrinivas Kandagatla 	buf->fl = fl;
415c68cfb71SSrinivas Kandagatla 	buf->virt = NULL;
416c68cfb71SSrinivas Kandagatla 	buf->phys = 0;
417c68cfb71SSrinivas Kandagatla 	buf->size = size;
418c68cfb71SSrinivas Kandagatla 	buf->dev = dev;
4192419e55eSJorge Ramirez-Ortiz 	buf->raddr = 0;
420c68cfb71SSrinivas Kandagatla 
421c68cfb71SSrinivas Kandagatla 	buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys,
422c68cfb71SSrinivas Kandagatla 				       GFP_KERNEL);
42341db5f83SJorge Ramirez-Ortiz 	if (!buf->virt) {
42441db5f83SJorge Ramirez-Ortiz 		mutex_destroy(&buf->lock);
42541db5f83SJorge Ramirez-Ortiz 		kfree(buf);
426c68cfb71SSrinivas Kandagatla 		return -ENOMEM;
42741db5f83SJorge Ramirez-Ortiz 	}
428c68cfb71SSrinivas Kandagatla 
429c68cfb71SSrinivas Kandagatla 	*obuf = buf;
430c68cfb71SSrinivas Kandagatla 
431c68cfb71SSrinivas Kandagatla 	return 0;
432c68cfb71SSrinivas Kandagatla }
433c68cfb71SSrinivas Kandagatla 
4346f18c7e8SAbel Vesa static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
4356f18c7e8SAbel Vesa 			     u64 size, struct fastrpc_buf **obuf)
4366f18c7e8SAbel Vesa {
4376f18c7e8SAbel Vesa 	int ret;
4386f18c7e8SAbel Vesa 	struct fastrpc_buf *buf;
4396f18c7e8SAbel Vesa 
4406f18c7e8SAbel Vesa 	ret = __fastrpc_buf_alloc(fl, dev, size, obuf);
4416f18c7e8SAbel Vesa 	if (ret)
4426f18c7e8SAbel Vesa 		return ret;
4436f18c7e8SAbel Vesa 
4446f18c7e8SAbel Vesa 	buf = *obuf;
4456f18c7e8SAbel Vesa 
4466f18c7e8SAbel Vesa 	if (fl->sctx && fl->sctx->sid)
4476f18c7e8SAbel Vesa 		buf->phys += ((u64)fl->sctx->sid << 32);
4486f18c7e8SAbel Vesa 
4496f18c7e8SAbel Vesa 	return 0;
4506f18c7e8SAbel Vesa }
4516f18c7e8SAbel Vesa 
4526f18c7e8SAbel Vesa static int fastrpc_remote_heap_alloc(struct fastrpc_user *fl, struct device *dev,
4536f18c7e8SAbel Vesa 				     u64 size, struct fastrpc_buf **obuf)
4546f18c7e8SAbel Vesa {
4556f18c7e8SAbel Vesa 	struct device *rdev = &fl->cctx->rpdev->dev;
4566f18c7e8SAbel Vesa 
4576f18c7e8SAbel Vesa 	return  __fastrpc_buf_alloc(fl, rdev, size, obuf);
4586f18c7e8SAbel Vesa }
4596f18c7e8SAbel Vesa 
460278d56f9SBjorn Andersson static void fastrpc_channel_ctx_free(struct kref *ref)
461278d56f9SBjorn Andersson {
462278d56f9SBjorn Andersson 	struct fastrpc_channel_ctx *cctx;
463278d56f9SBjorn Andersson 
464278d56f9SBjorn Andersson 	cctx = container_of(ref, struct fastrpc_channel_ctx, refcount);
465278d56f9SBjorn Andersson 
466278d56f9SBjorn Andersson 	kfree(cctx);
467278d56f9SBjorn Andersson }
468278d56f9SBjorn Andersson 
469278d56f9SBjorn Andersson static void fastrpc_channel_ctx_get(struct fastrpc_channel_ctx *cctx)
470278d56f9SBjorn Andersson {
471278d56f9SBjorn Andersson 	kref_get(&cctx->refcount);
472278d56f9SBjorn Andersson }
473278d56f9SBjorn Andersson 
474278d56f9SBjorn Andersson static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx)
475278d56f9SBjorn Andersson {
476278d56f9SBjorn Andersson 	kref_put(&cctx->refcount, fastrpc_channel_ctx_free);
477278d56f9SBjorn Andersson }
478278d56f9SBjorn Andersson 
479c68cfb71SSrinivas Kandagatla static void fastrpc_context_free(struct kref *ref)
480c68cfb71SSrinivas Kandagatla {
481c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_ctx *ctx;
482c68cfb71SSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx;
483977e6c8dSSrinivas Kandagatla 	unsigned long flags;
484c68cfb71SSrinivas Kandagatla 	int i;
485c68cfb71SSrinivas Kandagatla 
486c68cfb71SSrinivas Kandagatla 	ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
487c68cfb71SSrinivas Kandagatla 	cctx = ctx->cctx;
488c68cfb71SSrinivas Kandagatla 
4898f6c1d8cSVamsi Krishna Gattupalli 	for (i = 0; i < ctx->nbufs; i++)
490c68cfb71SSrinivas Kandagatla 		fastrpc_map_put(ctx->maps[i]);
491c68cfb71SSrinivas Kandagatla 
492c68cfb71SSrinivas Kandagatla 	if (ctx->buf)
493c68cfb71SSrinivas Kandagatla 		fastrpc_buf_free(ctx->buf);
494c68cfb71SSrinivas Kandagatla 
495977e6c8dSSrinivas Kandagatla 	spin_lock_irqsave(&cctx->lock, flags);
496c68cfb71SSrinivas Kandagatla 	idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4);
497977e6c8dSSrinivas Kandagatla 	spin_unlock_irqrestore(&cctx->lock, flags);
498c68cfb71SSrinivas Kandagatla 
499c68cfb71SSrinivas Kandagatla 	kfree(ctx->maps);
50025e8dfb8SSrinivas Kandagatla 	kfree(ctx->olaps);
501c68cfb71SSrinivas Kandagatla 	kfree(ctx);
502278d56f9SBjorn Andersson 
503278d56f9SBjorn Andersson 	fastrpc_channel_ctx_put(cctx);
504c68cfb71SSrinivas Kandagatla }
505c68cfb71SSrinivas Kandagatla 
506c68cfb71SSrinivas Kandagatla static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx)
507c68cfb71SSrinivas Kandagatla {
508c68cfb71SSrinivas Kandagatla 	kref_get(&ctx->refcount);
509c68cfb71SSrinivas Kandagatla }
510c68cfb71SSrinivas Kandagatla 
511c68cfb71SSrinivas Kandagatla static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx)
512c68cfb71SSrinivas Kandagatla {
513c68cfb71SSrinivas Kandagatla 	kref_put(&ctx->refcount, fastrpc_context_free);
514c68cfb71SSrinivas Kandagatla }
515c68cfb71SSrinivas Kandagatla 
5168e7389c7SThierry Escande static void fastrpc_context_put_wq(struct work_struct *work)
5178e7389c7SThierry Escande {
5188e7389c7SThierry Escande 	struct fastrpc_invoke_ctx *ctx =
5198e7389c7SThierry Escande 			container_of(work, struct fastrpc_invoke_ctx, put_work);
5208e7389c7SThierry Escande 
5218e7389c7SThierry Escande 	fastrpc_context_put(ctx);
5228e7389c7SThierry Escande }
5238e7389c7SThierry Escande 
52425e8dfb8SSrinivas Kandagatla #define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)
52525e8dfb8SSrinivas Kandagatla static int olaps_cmp(const void *a, const void *b)
52625e8dfb8SSrinivas Kandagatla {
52725e8dfb8SSrinivas Kandagatla 	struct fastrpc_buf_overlap *pa = (struct fastrpc_buf_overlap *)a;
52825e8dfb8SSrinivas Kandagatla 	struct fastrpc_buf_overlap *pb = (struct fastrpc_buf_overlap *)b;
52925e8dfb8SSrinivas Kandagatla 	/* sort with lowest starting buffer first */
53025e8dfb8SSrinivas Kandagatla 	int st = CMP(pa->start, pb->start);
53125e8dfb8SSrinivas Kandagatla 	/* sort with highest ending buffer first */
53225e8dfb8SSrinivas Kandagatla 	int ed = CMP(pb->end, pa->end);
53325e8dfb8SSrinivas Kandagatla 
53425e8dfb8SSrinivas Kandagatla 	return st == 0 ? ed : st;
53525e8dfb8SSrinivas Kandagatla }
53625e8dfb8SSrinivas Kandagatla 
53725e8dfb8SSrinivas Kandagatla static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
53825e8dfb8SSrinivas Kandagatla {
53925e8dfb8SSrinivas Kandagatla 	u64 max_end = 0;
54025e8dfb8SSrinivas Kandagatla 	int i;
54125e8dfb8SSrinivas Kandagatla 
54225e8dfb8SSrinivas Kandagatla 	for (i = 0; i < ctx->nbufs; ++i) {
54325e8dfb8SSrinivas Kandagatla 		ctx->olaps[i].start = ctx->args[i].ptr;
54425e8dfb8SSrinivas Kandagatla 		ctx->olaps[i].end = ctx->olaps[i].start + ctx->args[i].length;
54525e8dfb8SSrinivas Kandagatla 		ctx->olaps[i].raix = i;
54625e8dfb8SSrinivas Kandagatla 	}
54725e8dfb8SSrinivas Kandagatla 
54825e8dfb8SSrinivas Kandagatla 	sort(ctx->olaps, ctx->nbufs, sizeof(*ctx->olaps), olaps_cmp, NULL);
54925e8dfb8SSrinivas Kandagatla 
55025e8dfb8SSrinivas Kandagatla 	for (i = 0; i < ctx->nbufs; ++i) {
55125e8dfb8SSrinivas Kandagatla 		/* Falling inside previous range */
55225e8dfb8SSrinivas Kandagatla 		if (ctx->olaps[i].start < max_end) {
55325e8dfb8SSrinivas Kandagatla 			ctx->olaps[i].mstart = max_end;
55425e8dfb8SSrinivas Kandagatla 			ctx->olaps[i].mend = ctx->olaps[i].end;
55525e8dfb8SSrinivas Kandagatla 			ctx->olaps[i].offset = max_end - ctx->olaps[i].start;
55625e8dfb8SSrinivas Kandagatla 
55725e8dfb8SSrinivas Kandagatla 			if (ctx->olaps[i].end > max_end) {
55825e8dfb8SSrinivas Kandagatla 				max_end = ctx->olaps[i].end;
55925e8dfb8SSrinivas Kandagatla 			} else {
56025e8dfb8SSrinivas Kandagatla 				ctx->olaps[i].mend = 0;
56125e8dfb8SSrinivas Kandagatla 				ctx->olaps[i].mstart = 0;
56225e8dfb8SSrinivas Kandagatla 			}
56325e8dfb8SSrinivas Kandagatla 
56425e8dfb8SSrinivas Kandagatla 		} else  {
56525e8dfb8SSrinivas Kandagatla 			ctx->olaps[i].mend = ctx->olaps[i].end;
56625e8dfb8SSrinivas Kandagatla 			ctx->olaps[i].mstart = ctx->olaps[i].start;
56725e8dfb8SSrinivas Kandagatla 			ctx->olaps[i].offset = 0;
56825e8dfb8SSrinivas Kandagatla 			max_end = ctx->olaps[i].end;
56925e8dfb8SSrinivas Kandagatla 		}
57025e8dfb8SSrinivas Kandagatla 	}
57125e8dfb8SSrinivas Kandagatla }
57225e8dfb8SSrinivas Kandagatla 
573c68cfb71SSrinivas Kandagatla static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
574c68cfb71SSrinivas Kandagatla 			struct fastrpc_user *user, u32 kernel, u32 sc,
575c68cfb71SSrinivas Kandagatla 			struct fastrpc_invoke_args *args)
576c68cfb71SSrinivas Kandagatla {
577c68cfb71SSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx = user->cctx;
578c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_ctx *ctx = NULL;
579977e6c8dSSrinivas Kandagatla 	unsigned long flags;
580c68cfb71SSrinivas Kandagatla 	int ret;
581c68cfb71SSrinivas Kandagatla 
582c68cfb71SSrinivas Kandagatla 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
583c68cfb71SSrinivas Kandagatla 	if (!ctx)
584c68cfb71SSrinivas Kandagatla 		return ERR_PTR(-ENOMEM);
585c68cfb71SSrinivas Kandagatla 
586c68cfb71SSrinivas Kandagatla 	INIT_LIST_HEAD(&ctx->node);
587c68cfb71SSrinivas Kandagatla 	ctx->fl = user;
588c68cfb71SSrinivas Kandagatla 	ctx->nscalars = REMOTE_SCALARS_LENGTH(sc);
589c68cfb71SSrinivas Kandagatla 	ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) +
590c68cfb71SSrinivas Kandagatla 		     REMOTE_SCALARS_OUTBUFS(sc);
591c68cfb71SSrinivas Kandagatla 
592c68cfb71SSrinivas Kandagatla 	if (ctx->nscalars) {
593c68cfb71SSrinivas Kandagatla 		ctx->maps = kcalloc(ctx->nscalars,
594c68cfb71SSrinivas Kandagatla 				    sizeof(*ctx->maps), GFP_KERNEL);
595c68cfb71SSrinivas Kandagatla 		if (!ctx->maps) {
596c68cfb71SSrinivas Kandagatla 			kfree(ctx);
597c68cfb71SSrinivas Kandagatla 			return ERR_PTR(-ENOMEM);
598c68cfb71SSrinivas Kandagatla 		}
59925e8dfb8SSrinivas Kandagatla 		ctx->olaps = kcalloc(ctx->nscalars,
60025e8dfb8SSrinivas Kandagatla 				    sizeof(*ctx->olaps), GFP_KERNEL);
60125e8dfb8SSrinivas Kandagatla 		if (!ctx->olaps) {
60225e8dfb8SSrinivas Kandagatla 			kfree(ctx->maps);
60325e8dfb8SSrinivas Kandagatla 			kfree(ctx);
60425e8dfb8SSrinivas Kandagatla 			return ERR_PTR(-ENOMEM);
60525e8dfb8SSrinivas Kandagatla 		}
606c68cfb71SSrinivas Kandagatla 		ctx->args = args;
60725e8dfb8SSrinivas Kandagatla 		fastrpc_get_buff_overlaps(ctx);
608c68cfb71SSrinivas Kandagatla 	}
609c68cfb71SSrinivas Kandagatla 
610278d56f9SBjorn Andersson 	/* Released in fastrpc_context_put() */
611278d56f9SBjorn Andersson 	fastrpc_channel_ctx_get(cctx);
612278d56f9SBjorn Andersson 
613c68cfb71SSrinivas Kandagatla 	ctx->sc = sc;
614c68cfb71SSrinivas Kandagatla 	ctx->retval = -1;
615c68cfb71SSrinivas Kandagatla 	ctx->pid = current->pid;
616c68cfb71SSrinivas Kandagatla 	ctx->tgid = user->tgid;
617c68cfb71SSrinivas Kandagatla 	ctx->cctx = cctx;
618c68cfb71SSrinivas Kandagatla 	init_completion(&ctx->work);
6198e7389c7SThierry Escande 	INIT_WORK(&ctx->put_work, fastrpc_context_put_wq);
620c68cfb71SSrinivas Kandagatla 
621c68cfb71SSrinivas Kandagatla 	spin_lock(&user->lock);
622c68cfb71SSrinivas Kandagatla 	list_add_tail(&ctx->node, &user->pending);
623c68cfb71SSrinivas Kandagatla 	spin_unlock(&user->lock);
624c68cfb71SSrinivas Kandagatla 
625977e6c8dSSrinivas Kandagatla 	spin_lock_irqsave(&cctx->lock, flags);
626c68cfb71SSrinivas Kandagatla 	ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1,
627c68cfb71SSrinivas Kandagatla 			       FASTRPC_CTX_MAX, GFP_ATOMIC);
628c68cfb71SSrinivas Kandagatla 	if (ret < 0) {
629977e6c8dSSrinivas Kandagatla 		spin_unlock_irqrestore(&cctx->lock, flags);
630c68cfb71SSrinivas Kandagatla 		goto err_idr;
631c68cfb71SSrinivas Kandagatla 	}
632c68cfb71SSrinivas Kandagatla 	ctx->ctxid = ret << 4;
633977e6c8dSSrinivas Kandagatla 	spin_unlock_irqrestore(&cctx->lock, flags);
634c68cfb71SSrinivas Kandagatla 
635c68cfb71SSrinivas Kandagatla 	kref_init(&ctx->refcount);
636c68cfb71SSrinivas Kandagatla 
637c68cfb71SSrinivas Kandagatla 	return ctx;
638c68cfb71SSrinivas Kandagatla err_idr:
639c68cfb71SSrinivas Kandagatla 	spin_lock(&user->lock);
640c68cfb71SSrinivas Kandagatla 	list_del(&ctx->node);
641c68cfb71SSrinivas Kandagatla 	spin_unlock(&user->lock);
642278d56f9SBjorn Andersson 	fastrpc_channel_ctx_put(cctx);
643c68cfb71SSrinivas Kandagatla 	kfree(ctx->maps);
64425e8dfb8SSrinivas Kandagatla 	kfree(ctx->olaps);
645c68cfb71SSrinivas Kandagatla 	kfree(ctx);
646c68cfb71SSrinivas Kandagatla 
647c68cfb71SSrinivas Kandagatla 	return ERR_PTR(ret);
648c68cfb71SSrinivas Kandagatla }
649c68cfb71SSrinivas Kandagatla 
6506cffd795SSrinivas Kandagatla static struct sg_table *
6516cffd795SSrinivas Kandagatla fastrpc_map_dma_buf(struct dma_buf_attachment *attachment,
6526cffd795SSrinivas Kandagatla 		    enum dma_data_direction dir)
6536cffd795SSrinivas Kandagatla {
6546cffd795SSrinivas Kandagatla 	struct fastrpc_dma_buf_attachment *a = attachment->priv;
6556cffd795SSrinivas Kandagatla 	struct sg_table *table;
656b212658aSJonathan Marek 	int ret;
6576cffd795SSrinivas Kandagatla 
6586cffd795SSrinivas Kandagatla 	table = &a->sgt;
6596cffd795SSrinivas Kandagatla 
660b212658aSJonathan Marek 	ret = dma_map_sgtable(attachment->dev, table, dir, 0);
661b212658aSJonathan Marek 	if (ret)
662b212658aSJonathan Marek 		table = ERR_PTR(ret);
6636cffd795SSrinivas Kandagatla 	return table;
6646cffd795SSrinivas Kandagatla }
6656cffd795SSrinivas Kandagatla 
6666cffd795SSrinivas Kandagatla static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach,
6676cffd795SSrinivas Kandagatla 				  struct sg_table *table,
6686cffd795SSrinivas Kandagatla 				  enum dma_data_direction dir)
6696cffd795SSrinivas Kandagatla {
6707cd7edb8SMarek Szyprowski 	dma_unmap_sgtable(attach->dev, table, dir, 0);
6716cffd795SSrinivas Kandagatla }
6726cffd795SSrinivas Kandagatla 
6736cffd795SSrinivas Kandagatla static void fastrpc_release(struct dma_buf *dmabuf)
6746cffd795SSrinivas Kandagatla {
6756cffd795SSrinivas Kandagatla 	struct fastrpc_buf *buffer = dmabuf->priv;
6766cffd795SSrinivas Kandagatla 
6776cffd795SSrinivas Kandagatla 	fastrpc_buf_free(buffer);
6786cffd795SSrinivas Kandagatla }
6796cffd795SSrinivas Kandagatla 
6806cffd795SSrinivas Kandagatla static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf,
6816cffd795SSrinivas Kandagatla 				  struct dma_buf_attachment *attachment)
6826cffd795SSrinivas Kandagatla {
6836cffd795SSrinivas Kandagatla 	struct fastrpc_dma_buf_attachment *a;
6846cffd795SSrinivas Kandagatla 	struct fastrpc_buf *buffer = dmabuf->priv;
6856cffd795SSrinivas Kandagatla 	int ret;
6866cffd795SSrinivas Kandagatla 
6876cffd795SSrinivas Kandagatla 	a = kzalloc(sizeof(*a), GFP_KERNEL);
6886cffd795SSrinivas Kandagatla 	if (!a)
6896cffd795SSrinivas Kandagatla 		return -ENOMEM;
6906cffd795SSrinivas Kandagatla 
6916cffd795SSrinivas Kandagatla 	ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt,
6926cffd795SSrinivas Kandagatla 			      FASTRPC_PHYS(buffer->phys), buffer->size);
6936cffd795SSrinivas Kandagatla 	if (ret < 0) {
6946cffd795SSrinivas Kandagatla 		dev_err(buffer->dev, "failed to get scatterlist from DMA API\n");
695fc739a05SNavid Emamdoost 		kfree(a);
6966cffd795SSrinivas Kandagatla 		return -EINVAL;
6976cffd795SSrinivas Kandagatla 	}
6986cffd795SSrinivas Kandagatla 
6996cffd795SSrinivas Kandagatla 	a->dev = attachment->dev;
7006cffd795SSrinivas Kandagatla 	INIT_LIST_HEAD(&a->node);
7016cffd795SSrinivas Kandagatla 	attachment->priv = a;
7026cffd795SSrinivas Kandagatla 
7036cffd795SSrinivas Kandagatla 	mutex_lock(&buffer->lock);
7046cffd795SSrinivas Kandagatla 	list_add(&a->node, &buffer->attachments);
7056cffd795SSrinivas Kandagatla 	mutex_unlock(&buffer->lock);
7066cffd795SSrinivas Kandagatla 
7076cffd795SSrinivas Kandagatla 	return 0;
7086cffd795SSrinivas Kandagatla }
7096cffd795SSrinivas Kandagatla 
7106cffd795SSrinivas Kandagatla static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf,
7116cffd795SSrinivas Kandagatla 				    struct dma_buf_attachment *attachment)
7126cffd795SSrinivas Kandagatla {
7136cffd795SSrinivas Kandagatla 	struct fastrpc_dma_buf_attachment *a = attachment->priv;
7146cffd795SSrinivas Kandagatla 	struct fastrpc_buf *buffer = dmabuf->priv;
7156cffd795SSrinivas Kandagatla 
7166cffd795SSrinivas Kandagatla 	mutex_lock(&buffer->lock);
7176cffd795SSrinivas Kandagatla 	list_del(&a->node);
7186cffd795SSrinivas Kandagatla 	mutex_unlock(&buffer->lock);
719cf61860eSSrinivas Kandagatla 	sg_free_table(&a->sgt);
7206cffd795SSrinivas Kandagatla 	kfree(a);
7216cffd795SSrinivas Kandagatla }
7226cffd795SSrinivas Kandagatla 
7237938f421SLucas De Marchi static int fastrpc_vmap(struct dma_buf *dmabuf, struct iosys_map *map)
7246cffd795SSrinivas Kandagatla {
7256cffd795SSrinivas Kandagatla 	struct fastrpc_buf *buf = dmabuf->priv;
7266cffd795SSrinivas Kandagatla 
7277938f421SLucas De Marchi 	iosys_map_set_vaddr(map, buf->virt);
7286619ccf1SThomas Zimmermann 
7296619ccf1SThomas Zimmermann 	return 0;
7306cffd795SSrinivas Kandagatla }
7316cffd795SSrinivas Kandagatla 
7326cffd795SSrinivas Kandagatla static int fastrpc_mmap(struct dma_buf *dmabuf,
7336cffd795SSrinivas Kandagatla 			struct vm_area_struct *vma)
7346cffd795SSrinivas Kandagatla {
7356cffd795SSrinivas Kandagatla 	struct fastrpc_buf *buf = dmabuf->priv;
7366cffd795SSrinivas Kandagatla 	size_t size = vma->vm_end - vma->vm_start;
7376cffd795SSrinivas Kandagatla 
738265751a5SDmitry Osipenko 	dma_resv_assert_held(dmabuf->resv);
739265751a5SDmitry Osipenko 
7406cffd795SSrinivas Kandagatla 	return dma_mmap_coherent(buf->dev, vma, buf->virt,
7416cffd795SSrinivas Kandagatla 				 FASTRPC_PHYS(buf->phys), size);
7426cffd795SSrinivas Kandagatla }
7436cffd795SSrinivas Kandagatla 
7446cffd795SSrinivas Kandagatla static const struct dma_buf_ops fastrpc_dma_buf_ops = {
7456cffd795SSrinivas Kandagatla 	.attach = fastrpc_dma_buf_attach,
7466cffd795SSrinivas Kandagatla 	.detach = fastrpc_dma_buf_detatch,
7476cffd795SSrinivas Kandagatla 	.map_dma_buf = fastrpc_map_dma_buf,
7486cffd795SSrinivas Kandagatla 	.unmap_dma_buf = fastrpc_unmap_dma_buf,
7496cffd795SSrinivas Kandagatla 	.mmap = fastrpc_mmap,
7506cffd795SSrinivas Kandagatla 	.vmap = fastrpc_vmap,
7516cffd795SSrinivas Kandagatla 	.release = fastrpc_release,
7526cffd795SSrinivas Kandagatla };
7536cffd795SSrinivas Kandagatla 
754c68cfb71SSrinivas Kandagatla static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
755e90d9119SVamsi Krishna Gattupalli 			      u64 len, u32 attr, struct fastrpc_map **ppmap)
756c68cfb71SSrinivas Kandagatla {
757c68cfb71SSrinivas Kandagatla 	struct fastrpc_session_ctx *sess = fl->sctx;
758c68cfb71SSrinivas Kandagatla 	struct fastrpc_map *map = NULL;
759c68cfb71SSrinivas Kandagatla 	int err = 0;
760c68cfb71SSrinivas Kandagatla 
7619446fa16SAbel Vesa 	if (!fastrpc_map_lookup(fl, fd, ppmap, true))
762c68cfb71SSrinivas Kandagatla 		return 0;
763c68cfb71SSrinivas Kandagatla 
764c68cfb71SSrinivas Kandagatla 	map = kzalloc(sizeof(*map), GFP_KERNEL);
765c68cfb71SSrinivas Kandagatla 	if (!map)
766c68cfb71SSrinivas Kandagatla 		return -ENOMEM;
767c68cfb71SSrinivas Kandagatla 
768c68cfb71SSrinivas Kandagatla 	INIT_LIST_HEAD(&map->node);
769334f1a1cSAbel Vesa 	kref_init(&map->refcount);
770334f1a1cSAbel Vesa 
771c68cfb71SSrinivas Kandagatla 	map->fl = fl;
772c68cfb71SSrinivas Kandagatla 	map->fd = fd;
773c68cfb71SSrinivas Kandagatla 	map->buf = dma_buf_get(fd);
774682a6044SWei Yongjun 	if (IS_ERR(map->buf)) {
775682a6044SWei Yongjun 		err = PTR_ERR(map->buf);
776c68cfb71SSrinivas Kandagatla 		goto get_err;
777c68cfb71SSrinivas Kandagatla 	}
778c68cfb71SSrinivas Kandagatla 
779c68cfb71SSrinivas Kandagatla 	map->attach = dma_buf_attach(map->buf, sess->dev);
780c68cfb71SSrinivas Kandagatla 	if (IS_ERR(map->attach)) {
781c68cfb71SSrinivas Kandagatla 		dev_err(sess->dev, "Failed to attach dmabuf\n");
782c68cfb71SSrinivas Kandagatla 		err = PTR_ERR(map->attach);
783c68cfb71SSrinivas Kandagatla 		goto attach_err;
784c68cfb71SSrinivas Kandagatla 	}
785c68cfb71SSrinivas Kandagatla 
786791da5c7SDmitry Osipenko 	map->table = dma_buf_map_attachment_unlocked(map->attach, DMA_BIDIRECTIONAL);
787c68cfb71SSrinivas Kandagatla 	if (IS_ERR(map->table)) {
788c68cfb71SSrinivas Kandagatla 		err = PTR_ERR(map->table);
789c68cfb71SSrinivas Kandagatla 		goto map_err;
790c68cfb71SSrinivas Kandagatla 	}
791c68cfb71SSrinivas Kandagatla 
792a6e766deSEkansh Gupta 	if (attr & FASTRPC_ATTR_SECUREMAP) {
793a6e766deSEkansh Gupta 		map->phys = sg_phys(map->table->sgl);
794a6e766deSEkansh Gupta 	} else {
795c68cfb71SSrinivas Kandagatla 		map->phys = sg_dma_address(map->table->sgl);
796c68cfb71SSrinivas Kandagatla 		map->phys += ((u64)fl->sctx->sid << 32);
797a6e766deSEkansh Gupta 	}
798c68cfb71SSrinivas Kandagatla 	map->size = len;
799c68cfb71SSrinivas Kandagatla 	map->va = sg_virt(map->table->sgl);
800c68cfb71SSrinivas Kandagatla 	map->len = len;
801c68cfb71SSrinivas Kandagatla 
802e90d9119SVamsi Krishna Gattupalli 	if (attr & FASTRPC_ATTR_SECUREMAP) {
803e90d9119SVamsi Krishna Gattupalli 		/*
804e90d9119SVamsi Krishna Gattupalli 		 * If subsystem VMIDs are defined in DTSI, then do
805e90d9119SVamsi Krishna Gattupalli 		 * hyp_assign from HLOS to those VM(s)
806e90d9119SVamsi Krishna Gattupalli 		 */
807a6e766deSEkansh Gupta 		u64 src_perms = BIT(QCOM_SCM_VMID_HLOS);
808a6e766deSEkansh Gupta 		struct qcom_scm_vmperm dst_perms[2] = {0};
809a6e766deSEkansh Gupta 
810a6e766deSEkansh Gupta 		dst_perms[0].vmid = QCOM_SCM_VMID_HLOS;
811a6e766deSEkansh Gupta 		dst_perms[0].perm = QCOM_SCM_PERM_RW;
812a6e766deSEkansh Gupta 		dst_perms[1].vmid = fl->cctx->vmperms[0].vmid;
813a6e766deSEkansh Gupta 		dst_perms[1].perm = QCOM_SCM_PERM_RWX;
814e90d9119SVamsi Krishna Gattupalli 		map->attr = attr;
815a6e766deSEkansh Gupta 		err = qcom_scm_assign_mem(map->phys, (u64)map->size, &src_perms, dst_perms, 2);
816e90d9119SVamsi Krishna Gattupalli 		if (err) {
817e90d9119SVamsi Krishna Gattupalli 			dev_err(sess->dev, "Failed to assign memory with phys 0x%llx size 0x%llx err %d",
818e90d9119SVamsi Krishna Gattupalli 					map->phys, map->size, err);
819e90d9119SVamsi Krishna Gattupalli 			goto map_err;
820e90d9119SVamsi Krishna Gattupalli 		}
821e90d9119SVamsi Krishna Gattupalli 	}
822c68cfb71SSrinivas Kandagatla 	spin_lock(&fl->lock);
823c68cfb71SSrinivas Kandagatla 	list_add_tail(&map->node, &fl->maps);
824c68cfb71SSrinivas Kandagatla 	spin_unlock(&fl->lock);
825c68cfb71SSrinivas Kandagatla 	*ppmap = map;
826c68cfb71SSrinivas Kandagatla 
827c68cfb71SSrinivas Kandagatla 	return 0;
828c68cfb71SSrinivas Kandagatla 
829c68cfb71SSrinivas Kandagatla map_err:
830c68cfb71SSrinivas Kandagatla 	dma_buf_detach(map->buf, map->attach);
831c68cfb71SSrinivas Kandagatla attach_err:
832c68cfb71SSrinivas Kandagatla 	dma_buf_put(map->buf);
833c68cfb71SSrinivas Kandagatla get_err:
834334f1a1cSAbel Vesa 	fastrpc_map_put(map);
835c68cfb71SSrinivas Kandagatla 
836c68cfb71SSrinivas Kandagatla 	return err;
837c68cfb71SSrinivas Kandagatla }
838c68cfb71SSrinivas Kandagatla 
839c68cfb71SSrinivas Kandagatla /*
840c68cfb71SSrinivas Kandagatla  * Fastrpc payload buffer with metadata looks like:
841c68cfb71SSrinivas Kandagatla  *
842c68cfb71SSrinivas Kandagatla  * >>>>>>  START of METADATA <<<<<<<<<
843c68cfb71SSrinivas Kandagatla  * +---------------------------------+
844c68cfb71SSrinivas Kandagatla  * |           Arguments             |
84535a82b87SVamsi Krishna Gattupalli  * | type:(union fastrpc_remote_arg)|
846c68cfb71SSrinivas Kandagatla  * |             (0 - N)             |
847c68cfb71SSrinivas Kandagatla  * +---------------------------------+
848c68cfb71SSrinivas Kandagatla  * |         Invoke Buffer list      |
849c68cfb71SSrinivas Kandagatla  * | type:(struct fastrpc_invoke_buf)|
850c68cfb71SSrinivas Kandagatla  * |           (0 - N)               |
851c68cfb71SSrinivas Kandagatla  * +---------------------------------+
852c68cfb71SSrinivas Kandagatla  * |         Page info list          |
853c68cfb71SSrinivas Kandagatla  * | type:(struct fastrpc_phy_page)  |
854c68cfb71SSrinivas Kandagatla  * |             (0 - N)             |
855c68cfb71SSrinivas Kandagatla  * +---------------------------------+
856c68cfb71SSrinivas Kandagatla  * |         Optional info           |
857c68cfb71SSrinivas Kandagatla  * |(can be specific to SoC/Firmware)|
858c68cfb71SSrinivas Kandagatla  * +---------------------------------+
859c68cfb71SSrinivas Kandagatla  * >>>>>>>>  END of METADATA <<<<<<<<<
860c68cfb71SSrinivas Kandagatla  * +---------------------------------+
861c68cfb71SSrinivas Kandagatla  * |         Inline ARGS             |
862c68cfb71SSrinivas Kandagatla  * |            (0-N)                |
863c68cfb71SSrinivas Kandagatla  * +---------------------------------+
864c68cfb71SSrinivas Kandagatla  */
865c68cfb71SSrinivas Kandagatla 
866c68cfb71SSrinivas Kandagatla static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx)
867c68cfb71SSrinivas Kandagatla {
868c68cfb71SSrinivas Kandagatla 	int size = 0;
869c68cfb71SSrinivas Kandagatla 
87035a82b87SVamsi Krishna Gattupalli 	size = (sizeof(struct fastrpc_remote_buf) +
871c68cfb71SSrinivas Kandagatla 		sizeof(struct fastrpc_invoke_buf) +
872c68cfb71SSrinivas Kandagatla 		sizeof(struct fastrpc_phy_page)) * ctx->nscalars +
873c68cfb71SSrinivas Kandagatla 		sizeof(u64) * FASTRPC_MAX_FDLIST +
874c68cfb71SSrinivas Kandagatla 		sizeof(u32) * FASTRPC_MAX_CRCLIST;
875c68cfb71SSrinivas Kandagatla 
876c68cfb71SSrinivas Kandagatla 	return size;
877c68cfb71SSrinivas Kandagatla }
878c68cfb71SSrinivas Kandagatla 
879c68cfb71SSrinivas Kandagatla static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen)
880c68cfb71SSrinivas Kandagatla {
881c68cfb71SSrinivas Kandagatla 	u64 size = 0;
8823a1bf591SJeya R 	int oix;
883c68cfb71SSrinivas Kandagatla 
884c68cfb71SSrinivas Kandagatla 	size = ALIGN(metalen, FASTRPC_ALIGN);
8853a1bf591SJeya R 	for (oix = 0; oix < ctx->nbufs; oix++) {
8863a1bf591SJeya R 		int i = ctx->olaps[oix].raix;
8873a1bf591SJeya R 
888c68cfb71SSrinivas Kandagatla 		if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) {
88925e8dfb8SSrinivas Kandagatla 
8903a1bf591SJeya R 			if (ctx->olaps[oix].offset == 0)
891c68cfb71SSrinivas Kandagatla 				size = ALIGN(size, FASTRPC_ALIGN);
89225e8dfb8SSrinivas Kandagatla 
8933a1bf591SJeya R 			size += (ctx->olaps[oix].mend - ctx->olaps[oix].mstart);
894c68cfb71SSrinivas Kandagatla 		}
895c68cfb71SSrinivas Kandagatla 	}
896c68cfb71SSrinivas Kandagatla 
897c68cfb71SSrinivas Kandagatla 	return size;
898c68cfb71SSrinivas Kandagatla }
899c68cfb71SSrinivas Kandagatla 
900c68cfb71SSrinivas Kandagatla static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
901c68cfb71SSrinivas Kandagatla {
902c68cfb71SSrinivas Kandagatla 	struct device *dev = ctx->fl->sctx->dev;
903c68cfb71SSrinivas Kandagatla 	int i, err;
904c68cfb71SSrinivas Kandagatla 
905c68cfb71SSrinivas Kandagatla 	for (i = 0; i < ctx->nscalars; ++i) {
906c68cfb71SSrinivas Kandagatla 
907c68cfb71SSrinivas Kandagatla 		if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 ||
908c68cfb71SSrinivas Kandagatla 		    ctx->args[i].length == 0)
909c68cfb71SSrinivas Kandagatla 			continue;
910c68cfb71SSrinivas Kandagatla 
911c68cfb71SSrinivas Kandagatla 		err = fastrpc_map_create(ctx->fl, ctx->args[i].fd,
912e90d9119SVamsi Krishna Gattupalli 			 ctx->args[i].length, ctx->args[i].attr, &ctx->maps[i]);
913c68cfb71SSrinivas Kandagatla 		if (err) {
914c68cfb71SSrinivas Kandagatla 			dev_err(dev, "Error Creating map %d\n", err);
915c68cfb71SSrinivas Kandagatla 			return -EINVAL;
916c68cfb71SSrinivas Kandagatla 		}
917c68cfb71SSrinivas Kandagatla 
918c68cfb71SSrinivas Kandagatla 	}
919c68cfb71SSrinivas Kandagatla 	return 0;
920c68cfb71SSrinivas Kandagatla }
921c68cfb71SSrinivas Kandagatla 
92254f7c85bSVamsi Krishna Gattupalli static struct fastrpc_invoke_buf *fastrpc_invoke_buf_start(union fastrpc_remote_arg *pra, int len)
92354f7c85bSVamsi Krishna Gattupalli {
92454f7c85bSVamsi Krishna Gattupalli 	return (struct fastrpc_invoke_buf *)(&pra[len]);
92554f7c85bSVamsi Krishna Gattupalli }
92654f7c85bSVamsi Krishna Gattupalli 
92754f7c85bSVamsi Krishna Gattupalli static struct fastrpc_phy_page *fastrpc_phy_page_start(struct fastrpc_invoke_buf *buf, int len)
92854f7c85bSVamsi Krishna Gattupalli {
92954f7c85bSVamsi Krishna Gattupalli 	return (struct fastrpc_phy_page *)(&buf[len]);
93054f7c85bSVamsi Krishna Gattupalli }
93154f7c85bSVamsi Krishna Gattupalli 
932c68cfb71SSrinivas Kandagatla static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
933c68cfb71SSrinivas Kandagatla {
934c68cfb71SSrinivas Kandagatla 	struct device *dev = ctx->fl->sctx->dev;
93535a82b87SVamsi Krishna Gattupalli 	union fastrpc_remote_arg *rpra;
936c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_buf *list;
937c68cfb71SSrinivas Kandagatla 	struct fastrpc_phy_page *pages;
93825e8dfb8SSrinivas Kandagatla 	int inbufs, i, oix, err = 0;
93925e8dfb8SSrinivas Kandagatla 	u64 len, rlen, pkt_size;
94002b45b47SSrinivas Kandagatla 	u64 pg_start, pg_end;
941c68cfb71SSrinivas Kandagatla 	uintptr_t args;
942c68cfb71SSrinivas Kandagatla 	int metalen;
943c68cfb71SSrinivas Kandagatla 
944c68cfb71SSrinivas Kandagatla 	inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
945c68cfb71SSrinivas Kandagatla 	metalen = fastrpc_get_meta_size(ctx);
946c68cfb71SSrinivas Kandagatla 	pkt_size = fastrpc_get_payload_size(ctx, metalen);
947c68cfb71SSrinivas Kandagatla 
948c68cfb71SSrinivas Kandagatla 	err = fastrpc_create_maps(ctx);
949c68cfb71SSrinivas Kandagatla 	if (err)
950c68cfb71SSrinivas Kandagatla 		return err;
951c68cfb71SSrinivas Kandagatla 
952c68cfb71SSrinivas Kandagatla 	ctx->msg_sz = pkt_size;
953c68cfb71SSrinivas Kandagatla 
954c68cfb71SSrinivas Kandagatla 	err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf);
955c68cfb71SSrinivas Kandagatla 	if (err)
956c68cfb71SSrinivas Kandagatla 		return err;
957c68cfb71SSrinivas Kandagatla 
958c68cfb71SSrinivas Kandagatla 	rpra = ctx->buf->virt;
95954f7c85bSVamsi Krishna Gattupalli 	list = fastrpc_invoke_buf_start(rpra, ctx->nscalars);
96054f7c85bSVamsi Krishna Gattupalli 	pages = fastrpc_phy_page_start(list, ctx->nscalars);
961c68cfb71SSrinivas Kandagatla 	args = (uintptr_t)ctx->buf->virt + metalen;
962c68cfb71SSrinivas Kandagatla 	rlen = pkt_size - metalen;
963c68cfb71SSrinivas Kandagatla 	ctx->rpra = rpra;
964c68cfb71SSrinivas Kandagatla 
96525e8dfb8SSrinivas Kandagatla 	for (oix = 0; oix < ctx->nbufs; ++oix) {
96625e8dfb8SSrinivas Kandagatla 		int mlen;
96725e8dfb8SSrinivas Kandagatla 
96825e8dfb8SSrinivas Kandagatla 		i = ctx->olaps[oix].raix;
96925e8dfb8SSrinivas Kandagatla 		len = ctx->args[i].length;
970c68cfb71SSrinivas Kandagatla 
97135a82b87SVamsi Krishna Gattupalli 		rpra[i].buf.pv = 0;
97235a82b87SVamsi Krishna Gattupalli 		rpra[i].buf.len = len;
973c68cfb71SSrinivas Kandagatla 		list[i].num = len ? 1 : 0;
974c68cfb71SSrinivas Kandagatla 		list[i].pgidx = i;
975c68cfb71SSrinivas Kandagatla 
976c68cfb71SSrinivas Kandagatla 		if (!len)
977c68cfb71SSrinivas Kandagatla 			continue;
978c68cfb71SSrinivas Kandagatla 
979c68cfb71SSrinivas Kandagatla 		if (ctx->maps[i]) {
98080f3afd7SSrinivas Kandagatla 			struct vm_area_struct *vma = NULL;
98180f3afd7SSrinivas Kandagatla 
98235a82b87SVamsi Krishna Gattupalli 			rpra[i].buf.pv = (u64) ctx->args[i].ptr;
983c68cfb71SSrinivas Kandagatla 			pages[i].addr = ctx->maps[i]->phys;
98480f3afd7SSrinivas Kandagatla 
985f9a470dbSSrinivas Kandagatla 			mmap_read_lock(current->mm);
98680f3afd7SSrinivas Kandagatla 			vma = find_vma(current->mm, ctx->args[i].ptr);
98780f3afd7SSrinivas Kandagatla 			if (vma)
98880f3afd7SSrinivas Kandagatla 				pages[i].addr += ctx->args[i].ptr -
98980f3afd7SSrinivas Kandagatla 						 vma->vm_start;
990f9a470dbSSrinivas Kandagatla 			mmap_read_unlock(current->mm);
99180f3afd7SSrinivas Kandagatla 
99202b45b47SSrinivas Kandagatla 			pg_start = (ctx->args[i].ptr & PAGE_MASK) >> PAGE_SHIFT;
99302b45b47SSrinivas Kandagatla 			pg_end = ((ctx->args[i].ptr + len - 1) & PAGE_MASK) >>
99402b45b47SSrinivas Kandagatla 				  PAGE_SHIFT;
99502b45b47SSrinivas Kandagatla 			pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
99602b45b47SSrinivas Kandagatla 
997c68cfb71SSrinivas Kandagatla 		} else {
99825e8dfb8SSrinivas Kandagatla 
99925e8dfb8SSrinivas Kandagatla 			if (ctx->olaps[oix].offset == 0) {
1000c68cfb71SSrinivas Kandagatla 				rlen -= ALIGN(args, FASTRPC_ALIGN) - args;
1001c68cfb71SSrinivas Kandagatla 				args = ALIGN(args, FASTRPC_ALIGN);
100225e8dfb8SSrinivas Kandagatla 			}
100325e8dfb8SSrinivas Kandagatla 
100425e8dfb8SSrinivas Kandagatla 			mlen = ctx->olaps[oix].mend - ctx->olaps[oix].mstart;
100525e8dfb8SSrinivas Kandagatla 
100625e8dfb8SSrinivas Kandagatla 			if (rlen < mlen)
1007c68cfb71SSrinivas Kandagatla 				goto bail;
1008c68cfb71SSrinivas Kandagatla 
100935a82b87SVamsi Krishna Gattupalli 			rpra[i].buf.pv = args - ctx->olaps[oix].offset;
101025e8dfb8SSrinivas Kandagatla 			pages[i].addr = ctx->buf->phys -
101125e8dfb8SSrinivas Kandagatla 					ctx->olaps[oix].offset +
101225e8dfb8SSrinivas Kandagatla 					(pkt_size - rlen);
1013c68cfb71SSrinivas Kandagatla 			pages[i].addr = pages[i].addr &	PAGE_MASK;
101425e8dfb8SSrinivas Kandagatla 
101502b45b47SSrinivas Kandagatla 			pg_start = (args & PAGE_MASK) >> PAGE_SHIFT;
101602b45b47SSrinivas Kandagatla 			pg_end = ((args + len - 1) & PAGE_MASK) >> PAGE_SHIFT;
101702b45b47SSrinivas Kandagatla 			pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
101825e8dfb8SSrinivas Kandagatla 			args = args + mlen;
101925e8dfb8SSrinivas Kandagatla 			rlen -= mlen;
1020c68cfb71SSrinivas Kandagatla 		}
1021c68cfb71SSrinivas Kandagatla 
1022c68cfb71SSrinivas Kandagatla 		if (i < inbufs && !ctx->maps[i]) {
102335a82b87SVamsi Krishna Gattupalli 			void *dst = (void *)(uintptr_t)rpra[i].buf.pv;
1024c68cfb71SSrinivas Kandagatla 			void *src = (void *)(uintptr_t)ctx->args[i].ptr;
1025c68cfb71SSrinivas Kandagatla 
1026c68cfb71SSrinivas Kandagatla 			if (!kernel) {
1027c68cfb71SSrinivas Kandagatla 				if (copy_from_user(dst, (void __user *)src,
1028c68cfb71SSrinivas Kandagatla 						   len)) {
1029c68cfb71SSrinivas Kandagatla 					err = -EFAULT;
1030c68cfb71SSrinivas Kandagatla 					goto bail;
1031c68cfb71SSrinivas Kandagatla 				}
1032c68cfb71SSrinivas Kandagatla 			} else {
1033c68cfb71SSrinivas Kandagatla 				memcpy(dst, src, len);
1034c68cfb71SSrinivas Kandagatla 			}
1035c68cfb71SSrinivas Kandagatla 		}
1036c68cfb71SSrinivas Kandagatla 	}
1037c68cfb71SSrinivas Kandagatla 
1038c68cfb71SSrinivas Kandagatla 	for (i = ctx->nbufs; i < ctx->nscalars; ++i) {
1039c68cfb71SSrinivas Kandagatla 		list[i].num = ctx->args[i].length ? 1 : 0;
1040c68cfb71SSrinivas Kandagatla 		list[i].pgidx = i;
104135a82b87SVamsi Krishna Gattupalli 		if (ctx->maps[i]) {
1042c68cfb71SSrinivas Kandagatla 			pages[i].addr = ctx->maps[i]->phys;
1043c68cfb71SSrinivas Kandagatla 			pages[i].size = ctx->maps[i]->size;
1044c68cfb71SSrinivas Kandagatla 		}
104535a82b87SVamsi Krishna Gattupalli 		rpra[i].dma.fd = ctx->args[i].fd;
104635a82b87SVamsi Krishna Gattupalli 		rpra[i].dma.len = ctx->args[i].length;
104735a82b87SVamsi Krishna Gattupalli 		rpra[i].dma.offset = (u64) ctx->args[i].ptr;
104835a82b87SVamsi Krishna Gattupalli 	}
1049c68cfb71SSrinivas Kandagatla 
1050c68cfb71SSrinivas Kandagatla bail:
1051c68cfb71SSrinivas Kandagatla 	if (err)
1052c68cfb71SSrinivas Kandagatla 		dev_err(dev, "Error: get invoke args failed:%d\n", err);
1053c68cfb71SSrinivas Kandagatla 
1054c68cfb71SSrinivas Kandagatla 	return err;
1055c68cfb71SSrinivas Kandagatla }
1056c68cfb71SSrinivas Kandagatla 
1057c68cfb71SSrinivas Kandagatla static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
1058c68cfb71SSrinivas Kandagatla 			    u32 kernel)
1059c68cfb71SSrinivas Kandagatla {
106035a82b87SVamsi Krishna Gattupalli 	union fastrpc_remote_arg *rpra = ctx->rpra;
10618f6c1d8cSVamsi Krishna Gattupalli 	struct fastrpc_user *fl = ctx->fl;
10628f6c1d8cSVamsi Krishna Gattupalli 	struct fastrpc_map *mmap = NULL;
10638f6c1d8cSVamsi Krishna Gattupalli 	struct fastrpc_invoke_buf *list;
10648f6c1d8cSVamsi Krishna Gattupalli 	struct fastrpc_phy_page *pages;
10658f6c1d8cSVamsi Krishna Gattupalli 	u64 *fdlist;
10668f6c1d8cSVamsi Krishna Gattupalli 	int i, inbufs, outbufs, handles;
1067c68cfb71SSrinivas Kandagatla 
1068c68cfb71SSrinivas Kandagatla 	inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
10698f6c1d8cSVamsi Krishna Gattupalli 	outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc);
10708f6c1d8cSVamsi Krishna Gattupalli 	handles = REMOTE_SCALARS_INHANDLES(ctx->sc) + REMOTE_SCALARS_OUTHANDLES(ctx->sc);
10718f6c1d8cSVamsi Krishna Gattupalli 	list = fastrpc_invoke_buf_start(rpra, ctx->nscalars);
10728f6c1d8cSVamsi Krishna Gattupalli 	pages = fastrpc_phy_page_start(list, ctx->nscalars);
10738f6c1d8cSVamsi Krishna Gattupalli 	fdlist = (uint64_t *)(pages + inbufs + outbufs + handles);
1074c68cfb71SSrinivas Kandagatla 
1075c68cfb71SSrinivas Kandagatla 	for (i = inbufs; i < ctx->nbufs; ++i) {
1076847afd7bSJeya R 		if (!ctx->maps[i]) {
107735a82b87SVamsi Krishna Gattupalli 			void *src = (void *)(uintptr_t)rpra[i].buf.pv;
1078c68cfb71SSrinivas Kandagatla 			void *dst = (void *)(uintptr_t)ctx->args[i].ptr;
107935a82b87SVamsi Krishna Gattupalli 			u64 len = rpra[i].buf.len;
1080c68cfb71SSrinivas Kandagatla 
1081c68cfb71SSrinivas Kandagatla 			if (!kernel) {
1082c68cfb71SSrinivas Kandagatla 				if (copy_to_user((void __user *)dst, src, len))
1083c68cfb71SSrinivas Kandagatla 					return -EFAULT;
1084c68cfb71SSrinivas Kandagatla 			} else {
1085c68cfb71SSrinivas Kandagatla 				memcpy(dst, src, len);
1086c68cfb71SSrinivas Kandagatla 			}
1087c68cfb71SSrinivas Kandagatla 		}
1088847afd7bSJeya R 	}
1089c68cfb71SSrinivas Kandagatla 
10908f6c1d8cSVamsi Krishna Gattupalli 	for (i = 0; i < FASTRPC_MAX_FDLIST; i++) {
10918f6c1d8cSVamsi Krishna Gattupalli 		if (!fdlist[i])
10928f6c1d8cSVamsi Krishna Gattupalli 			break;
10939446fa16SAbel Vesa 		if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap, false))
10948f6c1d8cSVamsi Krishna Gattupalli 			fastrpc_map_put(mmap);
10958f6c1d8cSVamsi Krishna Gattupalli 	}
10968f6c1d8cSVamsi Krishna Gattupalli 
1097c68cfb71SSrinivas Kandagatla 	return 0;
1098c68cfb71SSrinivas Kandagatla }
1099c68cfb71SSrinivas Kandagatla 
1100c68cfb71SSrinivas Kandagatla static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx,
1101c68cfb71SSrinivas Kandagatla 			       struct fastrpc_invoke_ctx *ctx,
1102c68cfb71SSrinivas Kandagatla 			       u32 kernel, uint32_t handle)
1103c68cfb71SSrinivas Kandagatla {
1104c68cfb71SSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx;
1105c68cfb71SSrinivas Kandagatla 	struct fastrpc_user *fl = ctx->fl;
1106c68cfb71SSrinivas Kandagatla 	struct fastrpc_msg *msg = &ctx->msg;
110774003385SSrinivas Kandagatla 	int ret;
1108c68cfb71SSrinivas Kandagatla 
1109c68cfb71SSrinivas Kandagatla 	cctx = fl->cctx;
1110c68cfb71SSrinivas Kandagatla 	msg->pid = fl->tgid;
1111c68cfb71SSrinivas Kandagatla 	msg->tid = current->pid;
1112c68cfb71SSrinivas Kandagatla 
1113c68cfb71SSrinivas Kandagatla 	if (kernel)
1114c68cfb71SSrinivas Kandagatla 		msg->pid = 0;
1115c68cfb71SSrinivas Kandagatla 
1116c68cfb71SSrinivas Kandagatla 	msg->ctx = ctx->ctxid | fl->pd;
1117c68cfb71SSrinivas Kandagatla 	msg->handle = handle;
1118c68cfb71SSrinivas Kandagatla 	msg->sc = ctx->sc;
1119c68cfb71SSrinivas Kandagatla 	msg->addr = ctx->buf ? ctx->buf->phys : 0;
1120c68cfb71SSrinivas Kandagatla 	msg->size = roundup(ctx->msg_sz, PAGE_SIZE);
1121c68cfb71SSrinivas Kandagatla 	fastrpc_context_get(ctx);
1122c68cfb71SSrinivas Kandagatla 
112374003385SSrinivas Kandagatla 	ret = rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg));
112474003385SSrinivas Kandagatla 
112574003385SSrinivas Kandagatla 	if (ret)
112674003385SSrinivas Kandagatla 		fastrpc_context_put(ctx);
112774003385SSrinivas Kandagatla 
112874003385SSrinivas Kandagatla 	return ret;
112974003385SSrinivas Kandagatla 
1130c68cfb71SSrinivas Kandagatla }
1131c68cfb71SSrinivas Kandagatla 
1132c68cfb71SSrinivas Kandagatla static int fastrpc_internal_invoke(struct fastrpc_user *fl,  u32 kernel,
1133c68cfb71SSrinivas Kandagatla 				   u32 handle, u32 sc,
1134c68cfb71SSrinivas Kandagatla 				   struct fastrpc_invoke_args *args)
1135c68cfb71SSrinivas Kandagatla {
1136c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_ctx *ctx = NULL;
113776e8e4acSAbel Vesa 	struct fastrpc_buf *buf, *b;
113876e8e4acSAbel Vesa 
1139c68cfb71SSrinivas Kandagatla 	int err = 0;
1140c68cfb71SSrinivas Kandagatla 
1141c68cfb71SSrinivas Kandagatla 	if (!fl->sctx)
1142c68cfb71SSrinivas Kandagatla 		return -EINVAL;
1143c68cfb71SSrinivas Kandagatla 
11442e369878SBjorn Andersson 	if (!fl->cctx->rpdev)
11452e369878SBjorn Andersson 		return -EPIPE;
11462e369878SBjorn Andersson 
114720c40794SDmitry Baryshkov 	if (handle == FASTRPC_INIT_HANDLE && !kernel) {
114820c40794SDmitry Baryshkov 		dev_warn_ratelimited(fl->sctx->dev, "user app trying to send a kernel RPC message (%d)\n",  handle);
114920c40794SDmitry Baryshkov 		return -EPERM;
115020c40794SDmitry Baryshkov 	}
115120c40794SDmitry Baryshkov 
1152c68cfb71SSrinivas Kandagatla 	ctx = fastrpc_context_alloc(fl, kernel, sc, args);
1153c68cfb71SSrinivas Kandagatla 	if (IS_ERR(ctx))
1154c68cfb71SSrinivas Kandagatla 		return PTR_ERR(ctx);
1155c68cfb71SSrinivas Kandagatla 
1156c68cfb71SSrinivas Kandagatla 	if (ctx->nscalars) {
1157c68cfb71SSrinivas Kandagatla 		err = fastrpc_get_args(kernel, ctx);
1158c68cfb71SSrinivas Kandagatla 		if (err)
1159c68cfb71SSrinivas Kandagatla 			goto bail;
1160c68cfb71SSrinivas Kandagatla 	}
1161415a0729SSrinivas Kandagatla 
1162415a0729SSrinivas Kandagatla 	/* make sure that all CPU memory writes are seen by DSP */
1163415a0729SSrinivas Kandagatla 	dma_wmb();
1164c68cfb71SSrinivas Kandagatla 	/* Send invoke buffer to remote dsp */
1165c68cfb71SSrinivas Kandagatla 	err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle);
1166c68cfb71SSrinivas Kandagatla 	if (err)
1167c68cfb71SSrinivas Kandagatla 		goto bail;
1168c68cfb71SSrinivas Kandagatla 
116955bcda35SJorge Ramirez-Ortiz 	if (kernel) {
117055bcda35SJorge Ramirez-Ortiz 		if (!wait_for_completion_timeout(&ctx->work, 10 * HZ))
117155bcda35SJorge Ramirez-Ortiz 			err = -ETIMEDOUT;
117255bcda35SJorge Ramirez-Ortiz 	} else {
1173c68cfb71SSrinivas Kandagatla 		err = wait_for_completion_interruptible(&ctx->work);
117455bcda35SJorge Ramirez-Ortiz 	}
117555bcda35SJorge Ramirez-Ortiz 
1176c68cfb71SSrinivas Kandagatla 	if (err)
1177c68cfb71SSrinivas Kandagatla 		goto bail;
1178c68cfb71SSrinivas Kandagatla 
1179c68cfb71SSrinivas Kandagatla 	/* Check the response from remote dsp */
1180c68cfb71SSrinivas Kandagatla 	err = ctx->retval;
1181c68cfb71SSrinivas Kandagatla 	if (err)
1182c68cfb71SSrinivas Kandagatla 		goto bail;
1183c68cfb71SSrinivas Kandagatla 
1184c68cfb71SSrinivas Kandagatla 	if (ctx->nscalars) {
1185415a0729SSrinivas Kandagatla 		/* make sure that all memory writes by DSP are seen by CPU */
1186415a0729SSrinivas Kandagatla 		dma_rmb();
1187c68cfb71SSrinivas Kandagatla 		/* populate all the output buffers with results */
1188c68cfb71SSrinivas Kandagatla 		err = fastrpc_put_args(ctx, kernel);
1189c68cfb71SSrinivas Kandagatla 		if (err)
1190c68cfb71SSrinivas Kandagatla 			goto bail;
1191c68cfb71SSrinivas Kandagatla 	}
1192c68cfb71SSrinivas Kandagatla 
1193c68cfb71SSrinivas Kandagatla bail:
1194387f6255SJorge Ramirez-Ortiz 	if (err != -ERESTARTSYS && err != -ETIMEDOUT) {
1195387f6255SJorge Ramirez-Ortiz 		/* We are done with this compute context */
1196c68cfb71SSrinivas Kandagatla 		spin_lock(&fl->lock);
1197c68cfb71SSrinivas Kandagatla 		list_del(&ctx->node);
1198c68cfb71SSrinivas Kandagatla 		spin_unlock(&fl->lock);
1199c68cfb71SSrinivas Kandagatla 		fastrpc_context_put(ctx);
1200387f6255SJorge Ramirez-Ortiz 	}
120108715610SAbel Vesa 
120276e8e4acSAbel Vesa 	if (err == -ERESTARTSYS) {
120376e8e4acSAbel Vesa 		list_for_each_entry_safe(buf, b, &fl->mmaps, node) {
120476e8e4acSAbel Vesa 			list_del(&buf->node);
120576e8e4acSAbel Vesa 			list_add_tail(&buf->node, &fl->cctx->invoke_interrupted_mmaps);
120676e8e4acSAbel Vesa 		}
120776e8e4acSAbel Vesa 	}
120876e8e4acSAbel Vesa 
1209c68cfb71SSrinivas Kandagatla 	if (err)
1210c68cfb71SSrinivas Kandagatla 		dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err);
1211c68cfb71SSrinivas Kandagatla 
1212c68cfb71SSrinivas Kandagatla 	return err;
1213c68cfb71SSrinivas Kandagatla }
1214c68cfb71SSrinivas Kandagatla 
12157f1f4812SJeya R static bool is_session_rejected(struct fastrpc_user *fl, bool unsigned_pd_request)
12167f1f4812SJeya R {
12177f1f4812SJeya R 	/* Check if the device node is non-secure and channel is secure*/
12187f1f4812SJeya R 	if (!fl->is_secure_dev && fl->cctx->secure) {
12197f1f4812SJeya R 		/*
12207f1f4812SJeya R 		 * Allow untrusted applications to offload only to Unsigned PD when
12217f1f4812SJeya R 		 * channel is configured as secure and block untrusted apps on channel
12227f1f4812SJeya R 		 * that does not support unsigned PD offload
12237f1f4812SJeya R 		 */
12247f1f4812SJeya R 		if (!fl->cctx->unsigned_support || !unsigned_pd_request) {
12257f1f4812SJeya R 			dev_err(&fl->cctx->rpdev->dev, "Error: Untrusted application trying to offload to signed PD");
12267f1f4812SJeya R 			return true;
12277f1f4812SJeya R 		}
12287f1f4812SJeya R 	}
12297f1f4812SJeya R 
12307f1f4812SJeya R 	return false;
12317f1f4812SJeya R }
12327f1f4812SJeya R 
123308715610SAbel Vesa static int fastrpc_init_create_static_process(struct fastrpc_user *fl,
123408715610SAbel Vesa 					      char __user *argp)
123508715610SAbel Vesa {
123608715610SAbel Vesa 	struct fastrpc_init_create_static init;
123708715610SAbel Vesa 	struct fastrpc_invoke_args *args;
123808715610SAbel Vesa 	struct fastrpc_phy_page pages[1];
123908715610SAbel Vesa 	char *name;
124008715610SAbel Vesa 	int err;
124108715610SAbel Vesa 	struct {
124208715610SAbel Vesa 		int pgid;
124308715610SAbel Vesa 		u32 namelen;
124408715610SAbel Vesa 		u32 pageslen;
124508715610SAbel Vesa 	} inbuf;
124608715610SAbel Vesa 	u32 sc;
124708715610SAbel Vesa 
124808715610SAbel Vesa 	args = kcalloc(FASTRPC_CREATE_STATIC_PROCESS_NARGS, sizeof(*args), GFP_KERNEL);
124908715610SAbel Vesa 	if (!args)
125008715610SAbel Vesa 		return -ENOMEM;
125108715610SAbel Vesa 
125208715610SAbel Vesa 	if (copy_from_user(&init, argp, sizeof(init))) {
125308715610SAbel Vesa 		err = -EFAULT;
125408715610SAbel Vesa 		goto err;
125508715610SAbel Vesa 	}
125608715610SAbel Vesa 
125708715610SAbel Vesa 	if (init.namelen > INIT_FILE_NAMELEN_MAX) {
125808715610SAbel Vesa 		err = -EINVAL;
125908715610SAbel Vesa 		goto err;
126008715610SAbel Vesa 	}
126108715610SAbel Vesa 
126208715610SAbel Vesa 	name = kzalloc(init.namelen, GFP_KERNEL);
126308715610SAbel Vesa 	if (!name) {
126408715610SAbel Vesa 		err = -ENOMEM;
126508715610SAbel Vesa 		goto err;
126608715610SAbel Vesa 	}
126708715610SAbel Vesa 
126808715610SAbel Vesa 	if (copy_from_user(name, (void __user *)(uintptr_t)init.name, init.namelen)) {
126908715610SAbel Vesa 		err = -EFAULT;
127008715610SAbel Vesa 		goto err_name;
127108715610SAbel Vesa 	}
127208715610SAbel Vesa 
127308715610SAbel Vesa 	if (!fl->cctx->remote_heap) {
127408715610SAbel Vesa 		err = fastrpc_remote_heap_alloc(fl, fl->sctx->dev, init.memlen,
127508715610SAbel Vesa 						&fl->cctx->remote_heap);
127608715610SAbel Vesa 		if (err)
127708715610SAbel Vesa 			goto err_name;
127808715610SAbel Vesa 
127908715610SAbel Vesa 		/* Map if we have any heap VMIDs associated with this ADSP Static Process. */
128008715610SAbel Vesa 		if (fl->cctx->vmcount) {
128108715610SAbel Vesa 			err = qcom_scm_assign_mem(fl->cctx->remote_heap->phys,
1282aaca766cSElliot Berman 							(u64)fl->cctx->remote_heap->size,
1283aaca766cSElliot Berman 							&fl->cctx->perms,
128408715610SAbel Vesa 							fl->cctx->vmperms, fl->cctx->vmcount);
128508715610SAbel Vesa 			if (err) {
128608715610SAbel Vesa 				dev_err(fl->sctx->dev, "Failed to assign memory with phys 0x%llx size 0x%llx err %d",
128708715610SAbel Vesa 					fl->cctx->remote_heap->phys, fl->cctx->remote_heap->size, err);
128808715610SAbel Vesa 				goto err_map;
128908715610SAbel Vesa 			}
129008715610SAbel Vesa 		}
129108715610SAbel Vesa 	}
129208715610SAbel Vesa 
129308715610SAbel Vesa 	inbuf.pgid = fl->tgid;
129408715610SAbel Vesa 	inbuf.namelen = init.namelen;
129508715610SAbel Vesa 	inbuf.pageslen = 0;
129608715610SAbel Vesa 	fl->pd = USER_PD;
129708715610SAbel Vesa 
129808715610SAbel Vesa 	args[0].ptr = (u64)(uintptr_t)&inbuf;
129908715610SAbel Vesa 	args[0].length = sizeof(inbuf);
130008715610SAbel Vesa 	args[0].fd = -1;
130108715610SAbel Vesa 
130208715610SAbel Vesa 	args[1].ptr = (u64)(uintptr_t)name;
130308715610SAbel Vesa 	args[1].length = inbuf.namelen;
130408715610SAbel Vesa 	args[1].fd = -1;
130508715610SAbel Vesa 
130608715610SAbel Vesa 	pages[0].addr = fl->cctx->remote_heap->phys;
130708715610SAbel Vesa 	pages[0].size = fl->cctx->remote_heap->size;
130808715610SAbel Vesa 
130908715610SAbel Vesa 	args[2].ptr = (u64)(uintptr_t) pages;
131008715610SAbel Vesa 	args[2].length = sizeof(*pages);
131108715610SAbel Vesa 	args[2].fd = -1;
131208715610SAbel Vesa 
131308715610SAbel Vesa 	sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_STATIC, 3, 0);
131408715610SAbel Vesa 
131508715610SAbel Vesa 	err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
131608715610SAbel Vesa 				      sc, args);
131708715610SAbel Vesa 	if (err)
131808715610SAbel Vesa 		goto err_invoke;
131908715610SAbel Vesa 
132008715610SAbel Vesa 	kfree(args);
132108715610SAbel Vesa 
132208715610SAbel Vesa 	return 0;
132308715610SAbel Vesa err_invoke:
132408715610SAbel Vesa 	if (fl->cctx->vmcount) {
132508715610SAbel Vesa 		struct qcom_scm_vmperm perm;
132608715610SAbel Vesa 
132708715610SAbel Vesa 		perm.vmid = QCOM_SCM_VMID_HLOS;
132808715610SAbel Vesa 		perm.perm = QCOM_SCM_PERM_RWX;
132908715610SAbel Vesa 		err = qcom_scm_assign_mem(fl->cctx->remote_heap->phys,
133008715610SAbel Vesa 						(u64)fl->cctx->remote_heap->size,
1331aaca766cSElliot Berman 						&fl->cctx->perms, &perm, 1);
133208715610SAbel Vesa 		if (err)
133308715610SAbel Vesa 			dev_err(fl->sctx->dev, "Failed to assign memory phys 0x%llx size 0x%llx err %d",
133408715610SAbel Vesa 				fl->cctx->remote_heap->phys, fl->cctx->remote_heap->size, err);
133508715610SAbel Vesa 	}
133608715610SAbel Vesa err_map:
133708715610SAbel Vesa 	fastrpc_buf_free(fl->cctx->remote_heap);
133808715610SAbel Vesa err_name:
133908715610SAbel Vesa 	kfree(name);
134008715610SAbel Vesa err:
134108715610SAbel Vesa 	kfree(args);
134208715610SAbel Vesa 
134308715610SAbel Vesa 	return err;
134408715610SAbel Vesa }
134508715610SAbel Vesa 
1346d73f71c7SSrinivas Kandagatla static int fastrpc_init_create_process(struct fastrpc_user *fl,
1347d73f71c7SSrinivas Kandagatla 					char __user *argp)
1348d73f71c7SSrinivas Kandagatla {
1349d73f71c7SSrinivas Kandagatla 	struct fastrpc_init_create init;
1350d73f71c7SSrinivas Kandagatla 	struct fastrpc_invoke_args *args;
1351d73f71c7SSrinivas Kandagatla 	struct fastrpc_phy_page pages[1];
1352d73f71c7SSrinivas Kandagatla 	struct fastrpc_map *map = NULL;
1353d73f71c7SSrinivas Kandagatla 	struct fastrpc_buf *imem = NULL;
1354d73f71c7SSrinivas Kandagatla 	int memlen;
1355d73f71c7SSrinivas Kandagatla 	int err;
1356d73f71c7SSrinivas Kandagatla 	struct {
1357d73f71c7SSrinivas Kandagatla 		int pgid;
1358d73f71c7SSrinivas Kandagatla 		u32 namelen;
1359d73f71c7SSrinivas Kandagatla 		u32 filelen;
1360d73f71c7SSrinivas Kandagatla 		u32 pageslen;
1361d73f71c7SSrinivas Kandagatla 		u32 attrs;
1362d73f71c7SSrinivas Kandagatla 		u32 siglen;
1363d73f71c7SSrinivas Kandagatla 	} inbuf;
1364d73f71c7SSrinivas Kandagatla 	u32 sc;
13657f1f4812SJeya R 	bool unsigned_module = false;
1366d73f71c7SSrinivas Kandagatla 
1367d73f71c7SSrinivas Kandagatla 	args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL);
1368d73f71c7SSrinivas Kandagatla 	if (!args)
1369d73f71c7SSrinivas Kandagatla 		return -ENOMEM;
1370d73f71c7SSrinivas Kandagatla 
1371d73f71c7SSrinivas Kandagatla 	if (copy_from_user(&init, argp, sizeof(init))) {
1372d73f71c7SSrinivas Kandagatla 		err = -EFAULT;
1373b49f6d83SThierry Escande 		goto err;
1374d73f71c7SSrinivas Kandagatla 	}
1375d73f71c7SSrinivas Kandagatla 
13767f1f4812SJeya R 	if (init.attrs & FASTRPC_MODE_UNSIGNED_MODULE)
13777f1f4812SJeya R 		unsigned_module = true;
13787f1f4812SJeya R 
13797f1f4812SJeya R 	if (is_session_rejected(fl, unsigned_module)) {
13807f1f4812SJeya R 		err = -ECONNREFUSED;
13817f1f4812SJeya R 		goto err;
13827f1f4812SJeya R 	}
13837f1f4812SJeya R 
1384d73f71c7SSrinivas Kandagatla 	if (init.filelen > INIT_FILELEN_MAX) {
1385d73f71c7SSrinivas Kandagatla 		err = -EINVAL;
1386b49f6d83SThierry Escande 		goto err;
1387d73f71c7SSrinivas Kandagatla 	}
1388d73f71c7SSrinivas Kandagatla 
1389d73f71c7SSrinivas Kandagatla 	inbuf.pgid = fl->tgid;
1390d73f71c7SSrinivas Kandagatla 	inbuf.namelen = strlen(current->comm) + 1;
1391d73f71c7SSrinivas Kandagatla 	inbuf.filelen = init.filelen;
1392d73f71c7SSrinivas Kandagatla 	inbuf.pageslen = 1;
1393d73f71c7SSrinivas Kandagatla 	inbuf.attrs = init.attrs;
1394d73f71c7SSrinivas Kandagatla 	inbuf.siglen = init.siglen;
139584195d20SJonathan Marek 	fl->pd = USER_PD;
1396d73f71c7SSrinivas Kandagatla 
1397d73f71c7SSrinivas Kandagatla 	if (init.filelen && init.filefd) {
1398e90d9119SVamsi Krishna Gattupalli 		err = fastrpc_map_create(fl, init.filefd, init.filelen, 0, &map);
1399d73f71c7SSrinivas Kandagatla 		if (err)
1400b49f6d83SThierry Escande 			goto err;
1401d73f71c7SSrinivas Kandagatla 	}
1402d73f71c7SSrinivas Kandagatla 
1403d73f71c7SSrinivas Kandagatla 	memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4),
1404d73f71c7SSrinivas Kandagatla 		       1024 * 1024);
1405d73f71c7SSrinivas Kandagatla 	err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen,
1406d73f71c7SSrinivas Kandagatla 				&imem);
1407b49f6d83SThierry Escande 	if (err)
1408b49f6d83SThierry Escande 		goto err_alloc;
1409d73f71c7SSrinivas Kandagatla 
1410d73f71c7SSrinivas Kandagatla 	fl->init_mem = imem;
1411d73f71c7SSrinivas Kandagatla 	args[0].ptr = (u64)(uintptr_t)&inbuf;
1412d73f71c7SSrinivas Kandagatla 	args[0].length = sizeof(inbuf);
1413d73f71c7SSrinivas Kandagatla 	args[0].fd = -1;
1414d73f71c7SSrinivas Kandagatla 
1415d73f71c7SSrinivas Kandagatla 	args[1].ptr = (u64)(uintptr_t)current->comm;
1416d73f71c7SSrinivas Kandagatla 	args[1].length = inbuf.namelen;
1417d73f71c7SSrinivas Kandagatla 	args[1].fd = -1;
1418d73f71c7SSrinivas Kandagatla 
1419d73f71c7SSrinivas Kandagatla 	args[2].ptr = (u64) init.file;
1420d73f71c7SSrinivas Kandagatla 	args[2].length = inbuf.filelen;
1421d73f71c7SSrinivas Kandagatla 	args[2].fd = init.filefd;
1422d73f71c7SSrinivas Kandagatla 
1423d73f71c7SSrinivas Kandagatla 	pages[0].addr = imem->phys;
1424d73f71c7SSrinivas Kandagatla 	pages[0].size = imem->size;
1425d73f71c7SSrinivas Kandagatla 
1426d73f71c7SSrinivas Kandagatla 	args[3].ptr = (u64)(uintptr_t) pages;
1427d73f71c7SSrinivas Kandagatla 	args[3].length = 1 * sizeof(*pages);
1428d73f71c7SSrinivas Kandagatla 	args[3].fd = -1;
1429d73f71c7SSrinivas Kandagatla 
1430d73f71c7SSrinivas Kandagatla 	args[4].ptr = (u64)(uintptr_t)&inbuf.attrs;
1431d73f71c7SSrinivas Kandagatla 	args[4].length = sizeof(inbuf.attrs);
1432d73f71c7SSrinivas Kandagatla 	args[4].fd = -1;
1433d73f71c7SSrinivas Kandagatla 
1434d73f71c7SSrinivas Kandagatla 	args[5].ptr = (u64)(uintptr_t) &inbuf.siglen;
1435d73f71c7SSrinivas Kandagatla 	args[5].length = sizeof(inbuf.siglen);
1436d73f71c7SSrinivas Kandagatla 	args[5].fd = -1;
1437d73f71c7SSrinivas Kandagatla 
1438d73f71c7SSrinivas Kandagatla 	sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0);
1439d73f71c7SSrinivas Kandagatla 	if (init.attrs)
1440d73f71c7SSrinivas Kandagatla 		sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0);
1441d73f71c7SSrinivas Kandagatla 
1442d73f71c7SSrinivas Kandagatla 	err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1443d73f71c7SSrinivas Kandagatla 				      sc, args);
1444b49f6d83SThierry Escande 	if (err)
1445b49f6d83SThierry Escande 		goto err_invoke;
1446d73f71c7SSrinivas Kandagatla 
1447b49f6d83SThierry Escande 	kfree(args);
1448b49f6d83SThierry Escande 
1449b49f6d83SThierry Escande 	return 0;
1450b49f6d83SThierry Escande 
1451b49f6d83SThierry Escande err_invoke:
1452b49f6d83SThierry Escande 	fl->init_mem = NULL;
1453d73f71c7SSrinivas Kandagatla 	fastrpc_buf_free(imem);
1454b49f6d83SThierry Escande err_alloc:
1455b49f6d83SThierry Escande 	fastrpc_map_put(map);
1456b49f6d83SThierry Escande err:
1457d73f71c7SSrinivas Kandagatla 	kfree(args);
1458d73f71c7SSrinivas Kandagatla 
1459d73f71c7SSrinivas Kandagatla 	return err;
1460d73f71c7SSrinivas Kandagatla }
1461d73f71c7SSrinivas Kandagatla 
1462f6f9279fSSrinivas Kandagatla static struct fastrpc_session_ctx *fastrpc_session_alloc(
1463f6f9279fSSrinivas Kandagatla 					struct fastrpc_channel_ctx *cctx)
1464f6f9279fSSrinivas Kandagatla {
1465f6f9279fSSrinivas Kandagatla 	struct fastrpc_session_ctx *session = NULL;
1466977e6c8dSSrinivas Kandagatla 	unsigned long flags;
1467f6f9279fSSrinivas Kandagatla 	int i;
1468f6f9279fSSrinivas Kandagatla 
1469977e6c8dSSrinivas Kandagatla 	spin_lock_irqsave(&cctx->lock, flags);
1470f6f9279fSSrinivas Kandagatla 	for (i = 0; i < cctx->sesscount; i++) {
1471f6f9279fSSrinivas Kandagatla 		if (!cctx->session[i].used && cctx->session[i].valid) {
1472f6f9279fSSrinivas Kandagatla 			cctx->session[i].used = true;
1473f6f9279fSSrinivas Kandagatla 			session = &cctx->session[i];
1474f6f9279fSSrinivas Kandagatla 			break;
1475f6f9279fSSrinivas Kandagatla 		}
1476f6f9279fSSrinivas Kandagatla 	}
1477977e6c8dSSrinivas Kandagatla 	spin_unlock_irqrestore(&cctx->lock, flags);
1478f6f9279fSSrinivas Kandagatla 
1479f6f9279fSSrinivas Kandagatla 	return session;
1480f6f9279fSSrinivas Kandagatla }
1481f6f9279fSSrinivas Kandagatla 
1482f6f9279fSSrinivas Kandagatla static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx,
1483f6f9279fSSrinivas Kandagatla 				 struct fastrpc_session_ctx *session)
1484f6f9279fSSrinivas Kandagatla {
1485977e6c8dSSrinivas Kandagatla 	unsigned long flags;
1486977e6c8dSSrinivas Kandagatla 
1487977e6c8dSSrinivas Kandagatla 	spin_lock_irqsave(&cctx->lock, flags);
1488f6f9279fSSrinivas Kandagatla 	session->used = false;
1489977e6c8dSSrinivas Kandagatla 	spin_unlock_irqrestore(&cctx->lock, flags);
1490f6f9279fSSrinivas Kandagatla }
1491f6f9279fSSrinivas Kandagatla 
1492d73f71c7SSrinivas Kandagatla static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl)
1493d73f71c7SSrinivas Kandagatla {
1494d73f71c7SSrinivas Kandagatla 	struct fastrpc_invoke_args args[1];
1495d73f71c7SSrinivas Kandagatla 	int tgid = 0;
1496d73f71c7SSrinivas Kandagatla 	u32 sc;
1497d73f71c7SSrinivas Kandagatla 
1498d73f71c7SSrinivas Kandagatla 	tgid = fl->tgid;
1499d73f71c7SSrinivas Kandagatla 	args[0].ptr = (u64)(uintptr_t) &tgid;
1500d73f71c7SSrinivas Kandagatla 	args[0].length = sizeof(tgid);
1501d73f71c7SSrinivas Kandagatla 	args[0].fd = -1;
1502d73f71c7SSrinivas Kandagatla 	sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0);
1503d73f71c7SSrinivas Kandagatla 
1504d73f71c7SSrinivas Kandagatla 	return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1505d73f71c7SSrinivas Kandagatla 				       sc, &args[0]);
1506d73f71c7SSrinivas Kandagatla }
1507d73f71c7SSrinivas Kandagatla 
1508f6f9279fSSrinivas Kandagatla static int fastrpc_device_release(struct inode *inode, struct file *file)
1509f6f9279fSSrinivas Kandagatla {
1510f6f9279fSSrinivas Kandagatla 	struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1511f6f9279fSSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx = fl->cctx;
1512c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_ctx *ctx, *n;
1513c68cfb71SSrinivas Kandagatla 	struct fastrpc_map *map, *m;
15142419e55eSJorge Ramirez-Ortiz 	struct fastrpc_buf *buf, *b;
1515977e6c8dSSrinivas Kandagatla 	unsigned long flags;
1516f6f9279fSSrinivas Kandagatla 
1517d73f71c7SSrinivas Kandagatla 	fastrpc_release_current_dsp_process(fl);
1518d73f71c7SSrinivas Kandagatla 
1519977e6c8dSSrinivas Kandagatla 	spin_lock_irqsave(&cctx->lock, flags);
1520f6f9279fSSrinivas Kandagatla 	list_del(&fl->user);
1521977e6c8dSSrinivas Kandagatla 	spin_unlock_irqrestore(&cctx->lock, flags);
1522f6f9279fSSrinivas Kandagatla 
1523c68cfb71SSrinivas Kandagatla 	if (fl->init_mem)
1524c68cfb71SSrinivas Kandagatla 		fastrpc_buf_free(fl->init_mem);
1525c68cfb71SSrinivas Kandagatla 
1526c68cfb71SSrinivas Kandagatla 	list_for_each_entry_safe(ctx, n, &fl->pending, node) {
1527c68cfb71SSrinivas Kandagatla 		list_del(&ctx->node);
1528c68cfb71SSrinivas Kandagatla 		fastrpc_context_put(ctx);
1529c68cfb71SSrinivas Kandagatla 	}
1530c68cfb71SSrinivas Kandagatla 
15315bb96c8fSAbel Vesa 	list_for_each_entry_safe(map, m, &fl->maps, node)
1532c68cfb71SSrinivas Kandagatla 		fastrpc_map_put(map);
1533c68cfb71SSrinivas Kandagatla 
15342419e55eSJorge Ramirez-Ortiz 	list_for_each_entry_safe(buf, b, &fl->mmaps, node) {
15352419e55eSJorge Ramirez-Ortiz 		list_del(&buf->node);
15362419e55eSJorge Ramirez-Ortiz 		fastrpc_buf_free(buf);
15372419e55eSJorge Ramirez-Ortiz 	}
15382419e55eSJorge Ramirez-Ortiz 
1539f6f9279fSSrinivas Kandagatla 	fastrpc_session_free(cctx, fl->sctx);
1540278d56f9SBjorn Andersson 	fastrpc_channel_ctx_put(cctx);
1541f6f9279fSSrinivas Kandagatla 
1542f6f9279fSSrinivas Kandagatla 	mutex_destroy(&fl->mutex);
1543f6f9279fSSrinivas Kandagatla 	kfree(fl);
1544f6f9279fSSrinivas Kandagatla 	file->private_data = NULL;
1545f6f9279fSSrinivas Kandagatla 
1546f6f9279fSSrinivas Kandagatla 	return 0;
1547f6f9279fSSrinivas Kandagatla }
1548f6f9279fSSrinivas Kandagatla 
1549f6f9279fSSrinivas Kandagatla static int fastrpc_device_open(struct inode *inode, struct file *filp)
1550f6f9279fSSrinivas Kandagatla {
1551965602eaSSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx;
1552965602eaSSrinivas Kandagatla 	struct fastrpc_device *fdevice;
1553f6f9279fSSrinivas Kandagatla 	struct fastrpc_user *fl = NULL;
1554977e6c8dSSrinivas Kandagatla 	unsigned long flags;
1555f6f9279fSSrinivas Kandagatla 
1556965602eaSSrinivas Kandagatla 	fdevice = miscdev_to_fdevice(filp->private_data);
1557965602eaSSrinivas Kandagatla 	cctx = fdevice->cctx;
1558965602eaSSrinivas Kandagatla 
1559f6f9279fSSrinivas Kandagatla 	fl = kzalloc(sizeof(*fl), GFP_KERNEL);
1560f6f9279fSSrinivas Kandagatla 	if (!fl)
1561f6f9279fSSrinivas Kandagatla 		return -ENOMEM;
1562f6f9279fSSrinivas Kandagatla 
1563278d56f9SBjorn Andersson 	/* Released in fastrpc_device_release() */
1564278d56f9SBjorn Andersson 	fastrpc_channel_ctx_get(cctx);
1565278d56f9SBjorn Andersson 
1566f6f9279fSSrinivas Kandagatla 	filp->private_data = fl;
1567f6f9279fSSrinivas Kandagatla 	spin_lock_init(&fl->lock);
1568f6f9279fSSrinivas Kandagatla 	mutex_init(&fl->mutex);
1569f6f9279fSSrinivas Kandagatla 	INIT_LIST_HEAD(&fl->pending);
1570f6f9279fSSrinivas Kandagatla 	INIT_LIST_HEAD(&fl->maps);
15712419e55eSJorge Ramirez-Ortiz 	INIT_LIST_HEAD(&fl->mmaps);
1572f6f9279fSSrinivas Kandagatla 	INIT_LIST_HEAD(&fl->user);
1573f6f9279fSSrinivas Kandagatla 	fl->tgid = current->tgid;
1574f6f9279fSSrinivas Kandagatla 	fl->cctx = cctx;
15757f1f4812SJeya R 	fl->is_secure_dev = fdevice->secure;
15767c11df42SThierry Escande 
15777c11df42SThierry Escande 	fl->sctx = fastrpc_session_alloc(cctx);
15787c11df42SThierry Escande 	if (!fl->sctx) {
15797c11df42SThierry Escande 		dev_err(&cctx->rpdev->dev, "No session available\n");
15807c11df42SThierry Escande 		mutex_destroy(&fl->mutex);
15817c11df42SThierry Escande 		kfree(fl);
15827c11df42SThierry Escande 
15837c11df42SThierry Escande 		return -EBUSY;
15847c11df42SThierry Escande 	}
15857c11df42SThierry Escande 
1586977e6c8dSSrinivas Kandagatla 	spin_lock_irqsave(&cctx->lock, flags);
1587f6f9279fSSrinivas Kandagatla 	list_add_tail(&fl->user, &cctx->users);
1588977e6c8dSSrinivas Kandagatla 	spin_unlock_irqrestore(&cctx->lock, flags);
1589f6f9279fSSrinivas Kandagatla 
1590f6f9279fSSrinivas Kandagatla 	return 0;
1591f6f9279fSSrinivas Kandagatla }
1592f6f9279fSSrinivas Kandagatla 
15936cffd795SSrinivas Kandagatla static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
15946cffd795SSrinivas Kandagatla {
15956cffd795SSrinivas Kandagatla 	struct fastrpc_alloc_dma_buf bp;
15966cffd795SSrinivas Kandagatla 	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
15976cffd795SSrinivas Kandagatla 	struct fastrpc_buf *buf = NULL;
15986cffd795SSrinivas Kandagatla 	int err;
15996cffd795SSrinivas Kandagatla 
16006cffd795SSrinivas Kandagatla 	if (copy_from_user(&bp, argp, sizeof(bp)))
16016cffd795SSrinivas Kandagatla 		return -EFAULT;
16026cffd795SSrinivas Kandagatla 
16036cffd795SSrinivas Kandagatla 	err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf);
16046cffd795SSrinivas Kandagatla 	if (err)
16056cffd795SSrinivas Kandagatla 		return err;
16066cffd795SSrinivas Kandagatla 	exp_info.ops = &fastrpc_dma_buf_ops;
16076cffd795SSrinivas Kandagatla 	exp_info.size = bp.size;
16086cffd795SSrinivas Kandagatla 	exp_info.flags = O_RDWR;
16096cffd795SSrinivas Kandagatla 	exp_info.priv = buf;
16106cffd795SSrinivas Kandagatla 	buf->dmabuf = dma_buf_export(&exp_info);
16116cffd795SSrinivas Kandagatla 	if (IS_ERR(buf->dmabuf)) {
16126cffd795SSrinivas Kandagatla 		err = PTR_ERR(buf->dmabuf);
16136cffd795SSrinivas Kandagatla 		fastrpc_buf_free(buf);
16146cffd795SSrinivas Kandagatla 		return err;
16156cffd795SSrinivas Kandagatla 	}
16166cffd795SSrinivas Kandagatla 
16176cffd795SSrinivas Kandagatla 	bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE);
16186cffd795SSrinivas Kandagatla 	if (bp.fd < 0) {
16196cffd795SSrinivas Kandagatla 		dma_buf_put(buf->dmabuf);
16206cffd795SSrinivas Kandagatla 		return -EINVAL;
16216cffd795SSrinivas Kandagatla 	}
16226cffd795SSrinivas Kandagatla 
16236cffd795SSrinivas Kandagatla 	if (copy_to_user(argp, &bp, sizeof(bp))) {
162446963e2eSMathias Krause 		/*
162546963e2eSMathias Krause 		 * The usercopy failed, but we can't do much about it, as
162646963e2eSMathias Krause 		 * dma_buf_fd() already called fd_install() and made the
162746963e2eSMathias Krause 		 * file descriptor accessible for the current process. It
162846963e2eSMathias Krause 		 * might already be closed and dmabuf no longer valid when
162946963e2eSMathias Krause 		 * we reach this point. Therefore "leak" the fd and rely on
163046963e2eSMathias Krause 		 * the process exit path to do any required cleanup.
163146963e2eSMathias Krause 		 */
16326cffd795SSrinivas Kandagatla 		return -EFAULT;
16336cffd795SSrinivas Kandagatla 	}
16346cffd795SSrinivas Kandagatla 
16356cffd795SSrinivas Kandagatla 	return 0;
16366cffd795SSrinivas Kandagatla }
16376cffd795SSrinivas Kandagatla 
16386010d9beSJonathan Marek static int fastrpc_init_attach(struct fastrpc_user *fl, int pd)
1639d73f71c7SSrinivas Kandagatla {
1640d73f71c7SSrinivas Kandagatla 	struct fastrpc_invoke_args args[1];
1641d73f71c7SSrinivas Kandagatla 	int tgid = fl->tgid;
1642d73f71c7SSrinivas Kandagatla 	u32 sc;
1643d73f71c7SSrinivas Kandagatla 
1644d73f71c7SSrinivas Kandagatla 	args[0].ptr = (u64)(uintptr_t) &tgid;
1645d73f71c7SSrinivas Kandagatla 	args[0].length = sizeof(tgid);
1646d73f71c7SSrinivas Kandagatla 	args[0].fd = -1;
1647d73f71c7SSrinivas Kandagatla 	sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0);
16486010d9beSJonathan Marek 	fl->pd = pd;
1649d73f71c7SSrinivas Kandagatla 
1650d73f71c7SSrinivas Kandagatla 	return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1651d73f71c7SSrinivas Kandagatla 				       sc, &args[0]);
1652d73f71c7SSrinivas Kandagatla }
1653d73f71c7SSrinivas Kandagatla 
1654c68cfb71SSrinivas Kandagatla static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp)
1655c68cfb71SSrinivas Kandagatla {
1656c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_args *args = NULL;
1657c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke inv;
1658c68cfb71SSrinivas Kandagatla 	u32 nscalars;
1659c68cfb71SSrinivas Kandagatla 	int err;
1660c68cfb71SSrinivas Kandagatla 
1661c68cfb71SSrinivas Kandagatla 	if (copy_from_user(&inv, argp, sizeof(inv)))
1662c68cfb71SSrinivas Kandagatla 		return -EFAULT;
1663c68cfb71SSrinivas Kandagatla 
1664c68cfb71SSrinivas Kandagatla 	/* nscalars is truncated here to max supported value */
1665c68cfb71SSrinivas Kandagatla 	nscalars = REMOTE_SCALARS_LENGTH(inv.sc);
1666c68cfb71SSrinivas Kandagatla 	if (nscalars) {
1667c68cfb71SSrinivas Kandagatla 		args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL);
1668c68cfb71SSrinivas Kandagatla 		if (!args)
1669c68cfb71SSrinivas Kandagatla 			return -ENOMEM;
1670c68cfb71SSrinivas Kandagatla 
1671c68cfb71SSrinivas Kandagatla 		if (copy_from_user(args, (void __user *)(uintptr_t)inv.args,
1672c68cfb71SSrinivas Kandagatla 				   nscalars * sizeof(*args))) {
1673c68cfb71SSrinivas Kandagatla 			kfree(args);
1674c68cfb71SSrinivas Kandagatla 			return -EFAULT;
1675c68cfb71SSrinivas Kandagatla 		}
1676c68cfb71SSrinivas Kandagatla 	}
1677c68cfb71SSrinivas Kandagatla 
1678c68cfb71SSrinivas Kandagatla 	err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args);
1679c68cfb71SSrinivas Kandagatla 	kfree(args);
1680c68cfb71SSrinivas Kandagatla 
1681c68cfb71SSrinivas Kandagatla 	return err;
1682c68cfb71SSrinivas Kandagatla }
1683c68cfb71SSrinivas Kandagatla 
16846c16fd8bSJeya R static int fastrpc_get_info_from_dsp(struct fastrpc_user *fl, uint32_t *dsp_attr_buf,
16856c16fd8bSJeya R 				     uint32_t dsp_attr_buf_len)
16866c16fd8bSJeya R {
16876c16fd8bSJeya R 	struct fastrpc_invoke_args args[2] = { 0 };
16886c16fd8bSJeya R 
16896c16fd8bSJeya R 	/* Capability filled in userspace */
16906c16fd8bSJeya R 	dsp_attr_buf[0] = 0;
16916c16fd8bSJeya R 
16926c16fd8bSJeya R 	args[0].ptr = (u64)(uintptr_t)&dsp_attr_buf_len;
16936c16fd8bSJeya R 	args[0].length = sizeof(dsp_attr_buf_len);
16946c16fd8bSJeya R 	args[0].fd = -1;
16956c16fd8bSJeya R 	args[1].ptr = (u64)(uintptr_t)&dsp_attr_buf[1];
16966c16fd8bSJeya R 	args[1].length = dsp_attr_buf_len;
16976c16fd8bSJeya R 	args[1].fd = -1;
1698f667f56bSAbel Vesa 	fl->pd = USER_PD;
16996c16fd8bSJeya R 
17006c16fd8bSJeya R 	return fastrpc_internal_invoke(fl, true, FASTRPC_DSP_UTILITIES_HANDLE,
17016c16fd8bSJeya R 				       FASTRPC_SCALARS(0, 1, 1), args);
17026c16fd8bSJeya R }
17036c16fd8bSJeya R 
17046c16fd8bSJeya R static int fastrpc_get_info_from_kernel(struct fastrpc_ioctl_capability *cap,
17056c16fd8bSJeya R 					struct fastrpc_user *fl)
17066c16fd8bSJeya R {
17076c16fd8bSJeya R 	struct fastrpc_channel_ctx *cctx = fl->cctx;
17086c16fd8bSJeya R 	uint32_t attribute_id = cap->attribute_id;
17096c16fd8bSJeya R 	uint32_t *dsp_attributes;
17106c16fd8bSJeya R 	unsigned long flags;
17116c16fd8bSJeya R 	uint32_t domain = cap->domain;
17126c16fd8bSJeya R 	int err;
17136c16fd8bSJeya R 
17146c16fd8bSJeya R 	spin_lock_irqsave(&cctx->lock, flags);
17156c16fd8bSJeya R 	/* check if we already have queried dsp for attributes */
17166c16fd8bSJeya R 	if (cctx->valid_attributes) {
17176c16fd8bSJeya R 		spin_unlock_irqrestore(&cctx->lock, flags);
17186c16fd8bSJeya R 		goto done;
17196c16fd8bSJeya R 	}
17206c16fd8bSJeya R 	spin_unlock_irqrestore(&cctx->lock, flags);
17216c16fd8bSJeya R 
17226c16fd8bSJeya R 	dsp_attributes = kzalloc(FASTRPC_MAX_DSP_ATTRIBUTES_LEN, GFP_KERNEL);
17236c16fd8bSJeya R 	if (!dsp_attributes)
17246c16fd8bSJeya R 		return -ENOMEM;
17256c16fd8bSJeya R 
17266c16fd8bSJeya R 	err = fastrpc_get_info_from_dsp(fl, dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES_LEN);
17276c16fd8bSJeya R 	if (err == DSP_UNSUPPORTED_API) {
17286c16fd8bSJeya R 		dev_info(&cctx->rpdev->dev,
17296c16fd8bSJeya R 			 "Warning: DSP capabilities not supported on domain: %d\n", domain);
17306c16fd8bSJeya R 		kfree(dsp_attributes);
17316c16fd8bSJeya R 		return -EOPNOTSUPP;
17326c16fd8bSJeya R 	} else if (err) {
17336c16fd8bSJeya R 		dev_err(&cctx->rpdev->dev, "Error: dsp information is incorrect err: %d\n", err);
17346c16fd8bSJeya R 		kfree(dsp_attributes);
17356c16fd8bSJeya R 		return err;
17366c16fd8bSJeya R 	}
17376c16fd8bSJeya R 
17386c16fd8bSJeya R 	spin_lock_irqsave(&cctx->lock, flags);
17396c16fd8bSJeya R 	memcpy(cctx->dsp_attributes, dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES_LEN);
17406c16fd8bSJeya R 	cctx->valid_attributes = true;
17416c16fd8bSJeya R 	spin_unlock_irqrestore(&cctx->lock, flags);
17426c16fd8bSJeya R 	kfree(dsp_attributes);
17436c16fd8bSJeya R done:
17446c16fd8bSJeya R 	cap->capability = cctx->dsp_attributes[attribute_id];
17456c16fd8bSJeya R 	return 0;
17466c16fd8bSJeya R }
17476c16fd8bSJeya R 
17486c16fd8bSJeya R static int fastrpc_get_dsp_info(struct fastrpc_user *fl, char __user *argp)
17496c16fd8bSJeya R {
17506c16fd8bSJeya R 	struct fastrpc_ioctl_capability cap = {0};
17516c16fd8bSJeya R 	int err = 0;
17526c16fd8bSJeya R 
17536c16fd8bSJeya R 	if (copy_from_user(&cap, argp, sizeof(cap)))
17546c16fd8bSJeya R 		return  -EFAULT;
17556c16fd8bSJeya R 
17566c16fd8bSJeya R 	cap.capability = 0;
17576c16fd8bSJeya R 	if (cap.domain >= FASTRPC_DEV_MAX) {
17586c16fd8bSJeya R 		dev_err(&fl->cctx->rpdev->dev, "Error: Invalid domain id:%d, err:%d\n",
17596c16fd8bSJeya R 			cap.domain, err);
17606c16fd8bSJeya R 		return -ECHRNG;
17616c16fd8bSJeya R 	}
17626c16fd8bSJeya R 
17636c16fd8bSJeya R 	/* Fastrpc Capablities does not support modem domain */
17646c16fd8bSJeya R 	if (cap.domain == MDSP_DOMAIN_ID) {
17656c16fd8bSJeya R 		dev_err(&fl->cctx->rpdev->dev, "Error: modem not supported %d\n", err);
17666c16fd8bSJeya R 		return -ECHRNG;
17676c16fd8bSJeya R 	}
17686c16fd8bSJeya R 
17696c16fd8bSJeya R 	if (cap.attribute_id >= FASTRPC_MAX_DSP_ATTRIBUTES) {
17706c16fd8bSJeya R 		dev_err(&fl->cctx->rpdev->dev, "Error: invalid attribute: %d, err: %d\n",
17716c16fd8bSJeya R 			cap.attribute_id, err);
17726c16fd8bSJeya R 		return -EOVERFLOW;
17736c16fd8bSJeya R 	}
17746c16fd8bSJeya R 
17756c16fd8bSJeya R 	err = fastrpc_get_info_from_kernel(&cap, fl);
17766c16fd8bSJeya R 	if (err)
17776c16fd8bSJeya R 		return err;
17786c16fd8bSJeya R 
17796c16fd8bSJeya R 	if (copy_to_user(argp, &cap.capability, sizeof(cap.capability)))
17806c16fd8bSJeya R 		return -EFAULT;
17816c16fd8bSJeya R 
17826c16fd8bSJeya R 	return 0;
17836c16fd8bSJeya R }
17846c16fd8bSJeya R 
178572fa6f78SAbel Vesa static int fastrpc_req_munmap_impl(struct fastrpc_user *fl, struct fastrpc_buf *buf)
17862419e55eSJorge Ramirez-Ortiz {
17872419e55eSJorge Ramirez-Ortiz 	struct fastrpc_invoke_args args[1] = { [0] = { 0 } };
17882419e55eSJorge Ramirez-Ortiz 	struct fastrpc_munmap_req_msg req_msg;
17892419e55eSJorge Ramirez-Ortiz 	struct device *dev = fl->sctx->dev;
17902419e55eSJorge Ramirez-Ortiz 	int err;
17912419e55eSJorge Ramirez-Ortiz 	u32 sc;
17922419e55eSJorge Ramirez-Ortiz 
17932419e55eSJorge Ramirez-Ortiz 	req_msg.pgid = fl->tgid;
17942419e55eSJorge Ramirez-Ortiz 	req_msg.size = buf->size;
17952419e55eSJorge Ramirez-Ortiz 	req_msg.vaddr = buf->raddr;
17962419e55eSJorge Ramirez-Ortiz 
17972419e55eSJorge Ramirez-Ortiz 	args[0].ptr = (u64) (uintptr_t) &req_msg;
17982419e55eSJorge Ramirez-Ortiz 	args[0].length = sizeof(req_msg);
17992419e55eSJorge Ramirez-Ortiz 
18002419e55eSJorge Ramirez-Ortiz 	sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MUNMAP, 1, 0);
18012419e55eSJorge Ramirez-Ortiz 	err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc,
18022419e55eSJorge Ramirez-Ortiz 				      &args[0]);
18032419e55eSJorge Ramirez-Ortiz 	if (!err) {
18042419e55eSJorge Ramirez-Ortiz 		dev_dbg(dev, "unmmap\tpt 0x%09lx OK\n", buf->raddr);
18052419e55eSJorge Ramirez-Ortiz 		spin_lock(&fl->lock);
18062419e55eSJorge Ramirez-Ortiz 		list_del(&buf->node);
18072419e55eSJorge Ramirez-Ortiz 		spin_unlock(&fl->lock);
18082419e55eSJorge Ramirez-Ortiz 		fastrpc_buf_free(buf);
18092419e55eSJorge Ramirez-Ortiz 	} else {
18102419e55eSJorge Ramirez-Ortiz 		dev_err(dev, "unmmap\tpt 0x%09lx ERROR\n", buf->raddr);
18112419e55eSJorge Ramirez-Ortiz 	}
18122419e55eSJorge Ramirez-Ortiz 
18132419e55eSJorge Ramirez-Ortiz 	return err;
18142419e55eSJorge Ramirez-Ortiz }
18152419e55eSJorge Ramirez-Ortiz 
18162419e55eSJorge Ramirez-Ortiz static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)
18172419e55eSJorge Ramirez-Ortiz {
181872fa6f78SAbel Vesa 	struct fastrpc_buf *buf = NULL, *iter, *b;
18192419e55eSJorge Ramirez-Ortiz 	struct fastrpc_req_munmap req;
182072fa6f78SAbel Vesa 	struct device *dev = fl->sctx->dev;
18212419e55eSJorge Ramirez-Ortiz 
18222419e55eSJorge Ramirez-Ortiz 	if (copy_from_user(&req, argp, sizeof(req)))
18232419e55eSJorge Ramirez-Ortiz 		return -EFAULT;
18242419e55eSJorge Ramirez-Ortiz 
182572fa6f78SAbel Vesa 	spin_lock(&fl->lock);
182672fa6f78SAbel Vesa 	list_for_each_entry_safe(iter, b, &fl->mmaps, node) {
182772fa6f78SAbel Vesa 		if ((iter->raddr == req.vaddrout) && (iter->size == req.size)) {
182872fa6f78SAbel Vesa 			buf = iter;
182972fa6f78SAbel Vesa 			break;
183072fa6f78SAbel Vesa 		}
183172fa6f78SAbel Vesa 	}
183272fa6f78SAbel Vesa 	spin_unlock(&fl->lock);
183372fa6f78SAbel Vesa 
183472fa6f78SAbel Vesa 	if (!buf) {
183572fa6f78SAbel Vesa 		dev_err(dev, "mmap\t\tpt 0x%09llx [len 0x%08llx] not in list\n",
183672fa6f78SAbel Vesa 			req.vaddrout, req.size);
183772fa6f78SAbel Vesa 		return -EINVAL;
183872fa6f78SAbel Vesa 	}
183972fa6f78SAbel Vesa 
184072fa6f78SAbel Vesa 	return fastrpc_req_munmap_impl(fl, buf);
18412419e55eSJorge Ramirez-Ortiz }
18422419e55eSJorge Ramirez-Ortiz 
18432419e55eSJorge Ramirez-Ortiz static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp)
18442419e55eSJorge Ramirez-Ortiz {
18452419e55eSJorge Ramirez-Ortiz 	struct fastrpc_invoke_args args[3] = { [0 ... 2] = { 0 } };
18462419e55eSJorge Ramirez-Ortiz 	struct fastrpc_buf *buf = NULL;
18472419e55eSJorge Ramirez-Ortiz 	struct fastrpc_mmap_req_msg req_msg;
18482419e55eSJorge Ramirez-Ortiz 	struct fastrpc_mmap_rsp_msg rsp_msg;
18492419e55eSJorge Ramirez-Ortiz 	struct fastrpc_phy_page pages;
18502419e55eSJorge Ramirez-Ortiz 	struct fastrpc_req_mmap req;
18512419e55eSJorge Ramirez-Ortiz 	struct device *dev = fl->sctx->dev;
18522419e55eSJorge Ramirez-Ortiz 	int err;
18532419e55eSJorge Ramirez-Ortiz 	u32 sc;
18542419e55eSJorge Ramirez-Ortiz 
18552419e55eSJorge Ramirez-Ortiz 	if (copy_from_user(&req, argp, sizeof(req)))
18562419e55eSJorge Ramirez-Ortiz 		return -EFAULT;
18572419e55eSJorge Ramirez-Ortiz 
1858532ad70cSAbel Vesa 	if (req.flags != ADSP_MMAP_ADD_PAGES && req.flags != ADSP_MMAP_REMOTE_HEAP_ADDR) {
18592419e55eSJorge Ramirez-Ortiz 		dev_err(dev, "flag not supported 0x%x\n", req.flags);
1860532ad70cSAbel Vesa 
18612419e55eSJorge Ramirez-Ortiz 		return -EINVAL;
18622419e55eSJorge Ramirez-Ortiz 	}
18632419e55eSJorge Ramirez-Ortiz 
18642419e55eSJorge Ramirez-Ortiz 	if (req.vaddrin) {
18652419e55eSJorge Ramirez-Ortiz 		dev_err(dev, "adding user allocated pages is not supported\n");
18662419e55eSJorge Ramirez-Ortiz 		return -EINVAL;
18672419e55eSJorge Ramirez-Ortiz 	}
18682419e55eSJorge Ramirez-Ortiz 
18692419e55eSJorge Ramirez-Ortiz 	err = fastrpc_buf_alloc(fl, fl->sctx->dev, req.size, &buf);
18702419e55eSJorge Ramirez-Ortiz 	if (err) {
18712419e55eSJorge Ramirez-Ortiz 		dev_err(dev, "failed to allocate buffer\n");
18722419e55eSJorge Ramirez-Ortiz 		return err;
18732419e55eSJorge Ramirez-Ortiz 	}
18742419e55eSJorge Ramirez-Ortiz 
18752419e55eSJorge Ramirez-Ortiz 	req_msg.pgid = fl->tgid;
18762419e55eSJorge Ramirez-Ortiz 	req_msg.flags = req.flags;
18772419e55eSJorge Ramirez-Ortiz 	req_msg.vaddr = req.vaddrin;
18782419e55eSJorge Ramirez-Ortiz 	req_msg.num = sizeof(pages);
18792419e55eSJorge Ramirez-Ortiz 
18802419e55eSJorge Ramirez-Ortiz 	args[0].ptr = (u64) (uintptr_t) &req_msg;
18812419e55eSJorge Ramirez-Ortiz 	args[0].length = sizeof(req_msg);
18822419e55eSJorge Ramirez-Ortiz 
18832419e55eSJorge Ramirez-Ortiz 	pages.addr = buf->phys;
18842419e55eSJorge Ramirez-Ortiz 	pages.size = buf->size;
18852419e55eSJorge Ramirez-Ortiz 
18862419e55eSJorge Ramirez-Ortiz 	args[1].ptr = (u64) (uintptr_t) &pages;
18872419e55eSJorge Ramirez-Ortiz 	args[1].length = sizeof(pages);
18882419e55eSJorge Ramirez-Ortiz 
18892419e55eSJorge Ramirez-Ortiz 	args[2].ptr = (u64) (uintptr_t) &rsp_msg;
18902419e55eSJorge Ramirez-Ortiz 	args[2].length = sizeof(rsp_msg);
18912419e55eSJorge Ramirez-Ortiz 
18922419e55eSJorge Ramirez-Ortiz 	sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MMAP, 2, 1);
18932419e55eSJorge Ramirez-Ortiz 	err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc,
18942419e55eSJorge Ramirez-Ortiz 				      &args[0]);
18952419e55eSJorge Ramirez-Ortiz 	if (err) {
18962419e55eSJorge Ramirez-Ortiz 		dev_err(dev, "mmap error (len 0x%08llx)\n", buf->size);
18972419e55eSJorge Ramirez-Ortiz 		goto err_invoke;
18982419e55eSJorge Ramirez-Ortiz 	}
18992419e55eSJorge Ramirez-Ortiz 
19002419e55eSJorge Ramirez-Ortiz 	/* update the buffer to be able to deallocate the memory on the DSP */
19012419e55eSJorge Ramirez-Ortiz 	buf->raddr = (uintptr_t) rsp_msg.vaddr;
19022419e55eSJorge Ramirez-Ortiz 
19032419e55eSJorge Ramirez-Ortiz 	/* let the client know the address to use */
19042419e55eSJorge Ramirez-Ortiz 	req.vaddrout = rsp_msg.vaddr;
19052419e55eSJorge Ramirez-Ortiz 
1906532ad70cSAbel Vesa 	/* Add memory to static PD pool, protection thru hypervisor */
1907*3c7d0079SEkansh Gupta 	if (req.flags == ADSP_MMAP_REMOTE_HEAP_ADDR && fl->cctx->vmcount) {
1908532ad70cSAbel Vesa 		struct qcom_scm_vmperm perm;
1909532ad70cSAbel Vesa 
1910532ad70cSAbel Vesa 		perm.vmid = QCOM_SCM_VMID_HLOS;
1911532ad70cSAbel Vesa 		perm.perm = QCOM_SCM_PERM_RWX;
1912532ad70cSAbel Vesa 		err = qcom_scm_assign_mem(buf->phys, buf->size,
1913aaca766cSElliot Berman 			&fl->cctx->perms, &perm, 1);
1914532ad70cSAbel Vesa 		if (err) {
1915532ad70cSAbel Vesa 			dev_err(fl->sctx->dev, "Failed to assign memory phys 0x%llx size 0x%llx err %d",
1916532ad70cSAbel Vesa 					buf->phys, buf->size, err);
1917532ad70cSAbel Vesa 			goto err_assign;
1918532ad70cSAbel Vesa 		}
1919532ad70cSAbel Vesa 	}
1920532ad70cSAbel Vesa 
19212419e55eSJorge Ramirez-Ortiz 	spin_lock(&fl->lock);
19222419e55eSJorge Ramirez-Ortiz 	list_add_tail(&buf->node, &fl->mmaps);
19232419e55eSJorge Ramirez-Ortiz 	spin_unlock(&fl->lock);
19242419e55eSJorge Ramirez-Ortiz 
19252419e55eSJorge Ramirez-Ortiz 	if (copy_to_user((void __user *)argp, &req, sizeof(req))) {
192672fa6f78SAbel Vesa 		err = -EFAULT;
192772fa6f78SAbel Vesa 		goto err_assign;
19282419e55eSJorge Ramirez-Ortiz 	}
19292419e55eSJorge Ramirez-Ortiz 
19302419e55eSJorge Ramirez-Ortiz 	dev_dbg(dev, "mmap\t\tpt 0x%09lx OK [len 0x%08llx]\n",
19312419e55eSJorge Ramirez-Ortiz 		buf->raddr, buf->size);
19322419e55eSJorge Ramirez-Ortiz 
19332419e55eSJorge Ramirez-Ortiz 	return 0;
19342419e55eSJorge Ramirez-Ortiz 
193572fa6f78SAbel Vesa err_assign:
193672fa6f78SAbel Vesa 	fastrpc_req_munmap_impl(fl, buf);
19372419e55eSJorge Ramirez-Ortiz err_invoke:
19382419e55eSJorge Ramirez-Ortiz 	fastrpc_buf_free(buf);
19392419e55eSJorge Ramirez-Ortiz 
19402419e55eSJorge Ramirez-Ortiz 	return err;
19412419e55eSJorge Ramirez-Ortiz }
19422419e55eSJorge Ramirez-Ortiz 
19435c1b97c7SJeya R static int fastrpc_req_mem_unmap_impl(struct fastrpc_user *fl, struct fastrpc_mem_unmap *req)
19445c1b97c7SJeya R {
19455c1b97c7SJeya R 	struct fastrpc_invoke_args args[1] = { [0] = { 0 } };
1946c5c07c59SSrinivas Kandagatla 	struct fastrpc_map *map = NULL, *iter, *m;
19475c1b97c7SJeya R 	struct fastrpc_mem_unmap_req_msg req_msg = { 0 };
19485c1b97c7SJeya R 	int err = 0;
19495c1b97c7SJeya R 	u32 sc;
19505c1b97c7SJeya R 	struct device *dev = fl->sctx->dev;
19515c1b97c7SJeya R 
19525c1b97c7SJeya R 	spin_lock(&fl->lock);
1953c5c07c59SSrinivas Kandagatla 	list_for_each_entry_safe(iter, m, &fl->maps, node) {
1954c5c07c59SSrinivas Kandagatla 		if ((req->fd < 0 || iter->fd == req->fd) && (iter->raddr == req->vaddr)) {
1955c5c07c59SSrinivas Kandagatla 			map = iter;
19565c1b97c7SJeya R 			break;
1957c5c07c59SSrinivas Kandagatla 		}
19585c1b97c7SJeya R 	}
19595c1b97c7SJeya R 
19605c1b97c7SJeya R 	spin_unlock(&fl->lock);
19615c1b97c7SJeya R 
19625c1b97c7SJeya R 	if (!map) {
19635c1b97c7SJeya R 		dev_err(dev, "map not in list\n");
19645c1b97c7SJeya R 		return -EINVAL;
19655c1b97c7SJeya R 	}
19665c1b97c7SJeya R 
19675c1b97c7SJeya R 	req_msg.pgid = fl->tgid;
19685c1b97c7SJeya R 	req_msg.len = map->len;
19695c1b97c7SJeya R 	req_msg.vaddrin = map->raddr;
19705c1b97c7SJeya R 	req_msg.fd = map->fd;
19715c1b97c7SJeya R 
19725c1b97c7SJeya R 	args[0].ptr = (u64) (uintptr_t) &req_msg;
19735c1b97c7SJeya R 	args[0].length = sizeof(req_msg);
19745c1b97c7SJeya R 
19755c1b97c7SJeya R 	sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MEM_UNMAP, 1, 0);
19765c1b97c7SJeya R 	err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc,
19775c1b97c7SJeya R 				      &args[0]);
19785c1b97c7SJeya R 	fastrpc_map_put(map);
19795c1b97c7SJeya R 	if (err)
19805c1b97c7SJeya R 		dev_err(dev, "unmmap\tpt fd = %d, 0x%09llx error\n",  map->fd, map->raddr);
19815c1b97c7SJeya R 
19825c1b97c7SJeya R 	return err;
19835c1b97c7SJeya R }
19845c1b97c7SJeya R 
19855c1b97c7SJeya R static int fastrpc_req_mem_unmap(struct fastrpc_user *fl, char __user *argp)
19865c1b97c7SJeya R {
19875c1b97c7SJeya R 	struct fastrpc_mem_unmap req;
19885c1b97c7SJeya R 
19895c1b97c7SJeya R 	if (copy_from_user(&req, argp, sizeof(req)))
19905c1b97c7SJeya R 		return -EFAULT;
19915c1b97c7SJeya R 
19925c1b97c7SJeya R 	return fastrpc_req_mem_unmap_impl(fl, &req);
19935c1b97c7SJeya R }
19945c1b97c7SJeya R 
19955c1b97c7SJeya R static int fastrpc_req_mem_map(struct fastrpc_user *fl, char __user *argp)
19965c1b97c7SJeya R {
19975c1b97c7SJeya R 	struct fastrpc_invoke_args args[4] = { [0 ... 3] = { 0 } };
19985c1b97c7SJeya R 	struct fastrpc_mem_map_req_msg req_msg = { 0 };
19995c1b97c7SJeya R 	struct fastrpc_mmap_rsp_msg rsp_msg = { 0 };
20005c1b97c7SJeya R 	struct fastrpc_mem_unmap req_unmap = { 0 };
20015c1b97c7SJeya R 	struct fastrpc_phy_page pages = { 0 };
20025c1b97c7SJeya R 	struct fastrpc_mem_map req;
20035c1b97c7SJeya R 	struct device *dev = fl->sctx->dev;
20045c1b97c7SJeya R 	struct fastrpc_map *map = NULL;
20055c1b97c7SJeya R 	int err;
20065c1b97c7SJeya R 	u32 sc;
20075c1b97c7SJeya R 
20085c1b97c7SJeya R 	if (copy_from_user(&req, argp, sizeof(req)))
20095c1b97c7SJeya R 		return -EFAULT;
20105c1b97c7SJeya R 
20115c1b97c7SJeya R 	/* create SMMU mapping */
20123abe3ab3SSrinivas Kandagatla 	err = fastrpc_map_create(fl, req.fd, req.length, 0, &map);
20135c1b97c7SJeya R 	if (err) {
20145c1b97c7SJeya R 		dev_err(dev, "failed to map buffer, fd = %d\n", req.fd);
20155c1b97c7SJeya R 		return err;
20165c1b97c7SJeya R 	}
20175c1b97c7SJeya R 
20185c1b97c7SJeya R 	req_msg.pgid = fl->tgid;
20195c1b97c7SJeya R 	req_msg.fd = req.fd;
20205c1b97c7SJeya R 	req_msg.offset = req.offset;
20215c1b97c7SJeya R 	req_msg.vaddrin = req.vaddrin;
20225c1b97c7SJeya R 	map->va = (void *) (uintptr_t) req.vaddrin;
20235c1b97c7SJeya R 	req_msg.flags = req.flags;
20245c1b97c7SJeya R 	req_msg.num = sizeof(pages);
20255c1b97c7SJeya R 	req_msg.data_len = 0;
20265c1b97c7SJeya R 
20275c1b97c7SJeya R 	args[0].ptr = (u64) (uintptr_t) &req_msg;
20285c1b97c7SJeya R 	args[0].length = sizeof(req_msg);
20295c1b97c7SJeya R 
20305c1b97c7SJeya R 	pages.addr = map->phys;
20315c1b97c7SJeya R 	pages.size = map->size;
20325c1b97c7SJeya R 
20335c1b97c7SJeya R 	args[1].ptr = (u64) (uintptr_t) &pages;
20345c1b97c7SJeya R 	args[1].length = sizeof(pages);
20355c1b97c7SJeya R 
20365c1b97c7SJeya R 	args[2].ptr = (u64) (uintptr_t) &pages;
20375c1b97c7SJeya R 	args[2].length = 0;
20385c1b97c7SJeya R 
20395c1b97c7SJeya R 	args[3].ptr = (u64) (uintptr_t) &rsp_msg;
20405c1b97c7SJeya R 	args[3].length = sizeof(rsp_msg);
20415c1b97c7SJeya R 
20425c1b97c7SJeya R 	sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MEM_MAP, 3, 1);
20435c1b97c7SJeya R 	err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, &args[0]);
20445c1b97c7SJeya R 	if (err) {
20455c1b97c7SJeya R 		dev_err(dev, "mem mmap error, fd %d, vaddr %llx, size %lld\n",
20465c1b97c7SJeya R 			req.fd, req.vaddrin, map->size);
20475c1b97c7SJeya R 		goto err_invoke;
20485c1b97c7SJeya R 	}
20495c1b97c7SJeya R 
20505c1b97c7SJeya R 	/* update the buffer to be able to deallocate the memory on the DSP */
20515c1b97c7SJeya R 	map->raddr = rsp_msg.vaddr;
20525c1b97c7SJeya R 
20535c1b97c7SJeya R 	/* let the client know the address to use */
20545c1b97c7SJeya R 	req.vaddrout = rsp_msg.vaddr;
20555c1b97c7SJeya R 
20565c1b97c7SJeya R 	if (copy_to_user((void __user *)argp, &req, sizeof(req))) {
20575c1b97c7SJeya R 		/* unmap the memory and release the buffer */
20585c1b97c7SJeya R 		req_unmap.vaddr = (uintptr_t) rsp_msg.vaddr;
20595c1b97c7SJeya R 		req_unmap.length = map->size;
20605c1b97c7SJeya R 		fastrpc_req_mem_unmap_impl(fl, &req_unmap);
20615c1b97c7SJeya R 		return -EFAULT;
20625c1b97c7SJeya R 	}
20635c1b97c7SJeya R 
20645c1b97c7SJeya R 	return 0;
20655c1b97c7SJeya R 
20665c1b97c7SJeya R err_invoke:
20675c1b97c7SJeya R 	fastrpc_map_put(map);
20685c1b97c7SJeya R 
20695c1b97c7SJeya R 	return err;
20705c1b97c7SJeya R }
20715c1b97c7SJeya R 
2072c68cfb71SSrinivas Kandagatla static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
2073c68cfb71SSrinivas Kandagatla 				 unsigned long arg)
2074c68cfb71SSrinivas Kandagatla {
2075c68cfb71SSrinivas Kandagatla 	struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
2076c68cfb71SSrinivas Kandagatla 	char __user *argp = (char __user *)arg;
2077c68cfb71SSrinivas Kandagatla 	int err;
2078c68cfb71SSrinivas Kandagatla 
2079c68cfb71SSrinivas Kandagatla 	switch (cmd) {
2080c68cfb71SSrinivas Kandagatla 	case FASTRPC_IOCTL_INVOKE:
2081c68cfb71SSrinivas Kandagatla 		err = fastrpc_invoke(fl, argp);
2082c68cfb71SSrinivas Kandagatla 		break;
2083d73f71c7SSrinivas Kandagatla 	case FASTRPC_IOCTL_INIT_ATTACH:
20841959ab9eSAbel Vesa 		err = fastrpc_init_attach(fl, ROOT_PD);
20856010d9beSJonathan Marek 		break;
20866010d9beSJonathan Marek 	case FASTRPC_IOCTL_INIT_ATTACH_SNS:
20876010d9beSJonathan Marek 		err = fastrpc_init_attach(fl, SENSORS_PD);
2088d73f71c7SSrinivas Kandagatla 		break;
208908715610SAbel Vesa 	case FASTRPC_IOCTL_INIT_CREATE_STATIC:
209008715610SAbel Vesa 		err = fastrpc_init_create_static_process(fl, argp);
209108715610SAbel Vesa 		break;
2092d73f71c7SSrinivas Kandagatla 	case FASTRPC_IOCTL_INIT_CREATE:
2093d73f71c7SSrinivas Kandagatla 		err = fastrpc_init_create_process(fl, argp);
2094d73f71c7SSrinivas Kandagatla 		break;
20956cffd795SSrinivas Kandagatla 	case FASTRPC_IOCTL_ALLOC_DMA_BUFF:
20966cffd795SSrinivas Kandagatla 		err = fastrpc_dmabuf_alloc(fl, argp);
20976cffd795SSrinivas Kandagatla 		break;
20982419e55eSJorge Ramirez-Ortiz 	case FASTRPC_IOCTL_MMAP:
20992419e55eSJorge Ramirez-Ortiz 		err = fastrpc_req_mmap(fl, argp);
21002419e55eSJorge Ramirez-Ortiz 		break;
21012419e55eSJorge Ramirez-Ortiz 	case FASTRPC_IOCTL_MUNMAP:
21022419e55eSJorge Ramirez-Ortiz 		err = fastrpc_req_munmap(fl, argp);
21032419e55eSJorge Ramirez-Ortiz 		break;
21045c1b97c7SJeya R 	case FASTRPC_IOCTL_MEM_MAP:
21055c1b97c7SJeya R 		err = fastrpc_req_mem_map(fl, argp);
21065c1b97c7SJeya R 		break;
21075c1b97c7SJeya R 	case FASTRPC_IOCTL_MEM_UNMAP:
21085c1b97c7SJeya R 		err = fastrpc_req_mem_unmap(fl, argp);
21095c1b97c7SJeya R 		break;
21106c16fd8bSJeya R 	case FASTRPC_IOCTL_GET_DSP_INFO:
21116c16fd8bSJeya R 		err = fastrpc_get_dsp_info(fl, argp);
21126c16fd8bSJeya R 		break;
2113c68cfb71SSrinivas Kandagatla 	default:
2114c68cfb71SSrinivas Kandagatla 		err = -ENOTTY;
2115c68cfb71SSrinivas Kandagatla 		break;
2116c68cfb71SSrinivas Kandagatla 	}
2117c68cfb71SSrinivas Kandagatla 
2118c68cfb71SSrinivas Kandagatla 	return err;
2119c68cfb71SSrinivas Kandagatla }
2120c68cfb71SSrinivas Kandagatla 
2121f6f9279fSSrinivas Kandagatla static const struct file_operations fastrpc_fops = {
2122f6f9279fSSrinivas Kandagatla 	.open = fastrpc_device_open,
2123f6f9279fSSrinivas Kandagatla 	.release = fastrpc_device_release,
2124c68cfb71SSrinivas Kandagatla 	.unlocked_ioctl = fastrpc_device_ioctl,
2125c68cfb71SSrinivas Kandagatla 	.compat_ioctl = fastrpc_device_ioctl,
2126f6f9279fSSrinivas Kandagatla };
2127f6f9279fSSrinivas Kandagatla 
2128f6f9279fSSrinivas Kandagatla static int fastrpc_cb_probe(struct platform_device *pdev)
2129f6f9279fSSrinivas Kandagatla {
2130f6f9279fSSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx;
2131f6f9279fSSrinivas Kandagatla 	struct fastrpc_session_ctx *sess;
2132f6f9279fSSrinivas Kandagatla 	struct device *dev = &pdev->dev;
2133f6f9279fSSrinivas Kandagatla 	int i, sessions = 0;
2134977e6c8dSSrinivas Kandagatla 	unsigned long flags;
213501b76c32SBo YU 	int rc;
2136f6f9279fSSrinivas Kandagatla 
2137f6f9279fSSrinivas Kandagatla 	cctx = dev_get_drvdata(dev->parent);
2138f6f9279fSSrinivas Kandagatla 	if (!cctx)
2139f6f9279fSSrinivas Kandagatla 		return -EINVAL;
2140f6f9279fSSrinivas Kandagatla 
2141f6f9279fSSrinivas Kandagatla 	of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions);
2142f6f9279fSSrinivas Kandagatla 
2143977e6c8dSSrinivas Kandagatla 	spin_lock_irqsave(&cctx->lock, flags);
21449baa1415SJohan Hovold 	if (cctx->sesscount >= FASTRPC_MAX_SESSIONS) {
21459baa1415SJohan Hovold 		dev_err(&pdev->dev, "too many sessions\n");
21469baa1415SJohan Hovold 		spin_unlock_irqrestore(&cctx->lock, flags);
21479baa1415SJohan Hovold 		return -ENOSPC;
21489baa1415SJohan Hovold 	}
2149d245f43aSJohan Hovold 	sess = &cctx->session[cctx->sesscount++];
2150f6f9279fSSrinivas Kandagatla 	sess->used = false;
2151f6f9279fSSrinivas Kandagatla 	sess->valid = true;
2152f6f9279fSSrinivas Kandagatla 	sess->dev = dev;
2153f6f9279fSSrinivas Kandagatla 	dev_set_drvdata(dev, sess);
2154f6f9279fSSrinivas Kandagatla 
2155f6f9279fSSrinivas Kandagatla 	if (of_property_read_u32(dev->of_node, "reg", &sess->sid))
2156f6f9279fSSrinivas Kandagatla 		dev_info(dev, "FastRPC Session ID not specified in DT\n");
2157f6f9279fSSrinivas Kandagatla 
2158f6f9279fSSrinivas Kandagatla 	if (sessions > 0) {
2159f6f9279fSSrinivas Kandagatla 		struct fastrpc_session_ctx *dup_sess;
2160f6f9279fSSrinivas Kandagatla 
2161f6f9279fSSrinivas Kandagatla 		for (i = 1; i < sessions; i++) {
2162d245f43aSJohan Hovold 			if (cctx->sesscount >= FASTRPC_MAX_SESSIONS)
2163f6f9279fSSrinivas Kandagatla 				break;
2164d245f43aSJohan Hovold 			dup_sess = &cctx->session[cctx->sesscount++];
2165f6f9279fSSrinivas Kandagatla 			memcpy(dup_sess, sess, sizeof(*dup_sess));
2166f6f9279fSSrinivas Kandagatla 		}
2167f6f9279fSSrinivas Kandagatla 	}
2168977e6c8dSSrinivas Kandagatla 	spin_unlock_irqrestore(&cctx->lock, flags);
216901b76c32SBo YU 	rc = dma_set_mask(dev, DMA_BIT_MASK(32));
217001b76c32SBo YU 	if (rc) {
217101b76c32SBo YU 		dev_err(dev, "32-bit DMA enable failed\n");
217201b76c32SBo YU 		return rc;
217301b76c32SBo YU 	}
2174f6f9279fSSrinivas Kandagatla 
2175f6f9279fSSrinivas Kandagatla 	return 0;
2176f6f9279fSSrinivas Kandagatla }
2177f6f9279fSSrinivas Kandagatla 
2178f6f9279fSSrinivas Kandagatla static int fastrpc_cb_remove(struct platform_device *pdev)
2179f6f9279fSSrinivas Kandagatla {
2180f6f9279fSSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent);
2181f6f9279fSSrinivas Kandagatla 	struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev);
2182977e6c8dSSrinivas Kandagatla 	unsigned long flags;
2183f6f9279fSSrinivas Kandagatla 	int i;
2184f6f9279fSSrinivas Kandagatla 
2185977e6c8dSSrinivas Kandagatla 	spin_lock_irqsave(&cctx->lock, flags);
2186f6f9279fSSrinivas Kandagatla 	for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) {
2187f6f9279fSSrinivas Kandagatla 		if (cctx->session[i].sid == sess->sid) {
2188f6f9279fSSrinivas Kandagatla 			cctx->session[i].valid = false;
2189f6f9279fSSrinivas Kandagatla 			cctx->sesscount--;
2190f6f9279fSSrinivas Kandagatla 		}
2191f6f9279fSSrinivas Kandagatla 	}
2192977e6c8dSSrinivas Kandagatla 	spin_unlock_irqrestore(&cctx->lock, flags);
2193f6f9279fSSrinivas Kandagatla 
2194f6f9279fSSrinivas Kandagatla 	return 0;
2195f6f9279fSSrinivas Kandagatla }
2196f6f9279fSSrinivas Kandagatla 
2197f6f9279fSSrinivas Kandagatla static const struct of_device_id fastrpc_match_table[] = {
2198f6f9279fSSrinivas Kandagatla 	{ .compatible = "qcom,fastrpc-compute-cb", },
2199f6f9279fSSrinivas Kandagatla 	{}
2200f6f9279fSSrinivas Kandagatla };
2201f6f9279fSSrinivas Kandagatla 
2202f6f9279fSSrinivas Kandagatla static struct platform_driver fastrpc_cb_driver = {
2203f6f9279fSSrinivas Kandagatla 	.probe = fastrpc_cb_probe,
2204f6f9279fSSrinivas Kandagatla 	.remove = fastrpc_cb_remove,
2205f6f9279fSSrinivas Kandagatla 	.driver = {
2206f6f9279fSSrinivas Kandagatla 		.name = "qcom,fastrpc-cb",
2207f6f9279fSSrinivas Kandagatla 		.of_match_table = fastrpc_match_table,
2208f6f9279fSSrinivas Kandagatla 		.suppress_bind_attrs = true,
2209f6f9279fSSrinivas Kandagatla 	},
2210f6f9279fSSrinivas Kandagatla };
2211f6f9279fSSrinivas Kandagatla 
2212965602eaSSrinivas Kandagatla static int fastrpc_device_register(struct device *dev, struct fastrpc_channel_ctx *cctx,
22133abe3ab3SSrinivas Kandagatla 				   bool is_secured, const char *domain)
2214965602eaSSrinivas Kandagatla {
2215965602eaSSrinivas Kandagatla 	struct fastrpc_device *fdev;
2216965602eaSSrinivas Kandagatla 	int err;
2217965602eaSSrinivas Kandagatla 
2218965602eaSSrinivas Kandagatla 	fdev = devm_kzalloc(dev, sizeof(*fdev), GFP_KERNEL);
2219965602eaSSrinivas Kandagatla 	if (!fdev)
2220965602eaSSrinivas Kandagatla 		return -ENOMEM;
2221965602eaSSrinivas Kandagatla 
22223abe3ab3SSrinivas Kandagatla 	fdev->secure = is_secured;
2223965602eaSSrinivas Kandagatla 	fdev->cctx = cctx;
2224965602eaSSrinivas Kandagatla 	fdev->miscdev.minor = MISC_DYNAMIC_MINOR;
2225965602eaSSrinivas Kandagatla 	fdev->miscdev.fops = &fastrpc_fops;
22263abe3ab3SSrinivas Kandagatla 	fdev->miscdev.name = devm_kasprintf(dev, GFP_KERNEL, "fastrpc-%s%s",
22273abe3ab3SSrinivas Kandagatla 					    domain, is_secured ? "-secure" : "");
2228965602eaSSrinivas Kandagatla 	err = misc_register(&fdev->miscdev);
22293abe3ab3SSrinivas Kandagatla 	if (!err) {
22303abe3ab3SSrinivas Kandagatla 		if (is_secured)
22313abe3ab3SSrinivas Kandagatla 			cctx->secure_fdevice = fdev;
22323abe3ab3SSrinivas Kandagatla 		else
2233965602eaSSrinivas Kandagatla 			cctx->fdevice = fdev;
22343abe3ab3SSrinivas Kandagatla 	}
2235965602eaSSrinivas Kandagatla 
2236965602eaSSrinivas Kandagatla 	return err;
2237965602eaSSrinivas Kandagatla }
2238965602eaSSrinivas Kandagatla 
2239f6f9279fSSrinivas Kandagatla static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
2240f6f9279fSSrinivas Kandagatla {
2241f6f9279fSSrinivas Kandagatla 	struct device *rdev = &rpdev->dev;
2242f6f9279fSSrinivas Kandagatla 	struct fastrpc_channel_ctx *data;
2243e90d9119SVamsi Krishna Gattupalli 	int i, err, domain_id = -1, vmcount;
2244f6f9279fSSrinivas Kandagatla 	const char *domain;
22453abe3ab3SSrinivas Kandagatla 	bool secure_dsp;
2246e90d9119SVamsi Krishna Gattupalli 	unsigned int vmids[FASTRPC_MAX_VMIDS];
2247f6f9279fSSrinivas Kandagatla 
2248f6f9279fSSrinivas Kandagatla 	err = of_property_read_string(rdev->of_node, "label", &domain);
2249f6f9279fSSrinivas Kandagatla 	if (err) {
2250f6f9279fSSrinivas Kandagatla 		dev_info(rdev, "FastRPC Domain not specified in DT\n");
2251f6f9279fSSrinivas Kandagatla 		return err;
2252f6f9279fSSrinivas Kandagatla 	}
2253f6f9279fSSrinivas Kandagatla 
2254f6f9279fSSrinivas Kandagatla 	for (i = 0; i <= CDSP_DOMAIN_ID; i++) {
2255f6f9279fSSrinivas Kandagatla 		if (!strcmp(domains[i], domain)) {
2256f6f9279fSSrinivas Kandagatla 			domain_id = i;
2257f6f9279fSSrinivas Kandagatla 			break;
2258f6f9279fSSrinivas Kandagatla 		}
2259f6f9279fSSrinivas Kandagatla 	}
2260f6f9279fSSrinivas Kandagatla 
2261f6f9279fSSrinivas Kandagatla 	if (domain_id < 0) {
2262f6f9279fSSrinivas Kandagatla 		dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id);
2263f6f9279fSSrinivas Kandagatla 		return -EINVAL;
2264f6f9279fSSrinivas Kandagatla 	}
2265f6f9279fSSrinivas Kandagatla 
22661ce91d45SAbel Vesa 	if (of_reserved_mem_device_init_by_idx(rdev, rdev->of_node, 0))
22671ce91d45SAbel Vesa 		dev_info(rdev, "no reserved DMA memory for FASTRPC\n");
22681ce91d45SAbel Vesa 
2269e90d9119SVamsi Krishna Gattupalli 	vmcount = of_property_read_variable_u32_array(rdev->of_node,
2270e90d9119SVamsi Krishna Gattupalli 				"qcom,vmids", &vmids[0], 0, FASTRPC_MAX_VMIDS);
2271e90d9119SVamsi Krishna Gattupalli 	if (vmcount < 0)
2272e90d9119SVamsi Krishna Gattupalli 		vmcount = 0;
2273e90d9119SVamsi Krishna Gattupalli 	else if (!qcom_scm_is_available())
2274e90d9119SVamsi Krishna Gattupalli 		return -EPROBE_DEFER;
2275e90d9119SVamsi Krishna Gattupalli 
2276278d56f9SBjorn Andersson 	data = kzalloc(sizeof(*data), GFP_KERNEL);
2277278d56f9SBjorn Andersson 	if (!data)
2278278d56f9SBjorn Andersson 		return -ENOMEM;
2279278d56f9SBjorn Andersson 
2280e90d9119SVamsi Krishna Gattupalli 	if (vmcount) {
2281e90d9119SVamsi Krishna Gattupalli 		data->vmcount = vmcount;
2282e90d9119SVamsi Krishna Gattupalli 		data->perms = BIT(QCOM_SCM_VMID_HLOS);
2283e90d9119SVamsi Krishna Gattupalli 		for (i = 0; i < data->vmcount; i++) {
2284e90d9119SVamsi Krishna Gattupalli 			data->vmperms[i].vmid = vmids[i];
2285e90d9119SVamsi Krishna Gattupalli 			data->vmperms[i].perm = QCOM_SCM_PERM_RWX;
2286e90d9119SVamsi Krishna Gattupalli 		}
2287e90d9119SVamsi Krishna Gattupalli 	}
22883abe3ab3SSrinivas Kandagatla 
22893abe3ab3SSrinivas Kandagatla 	secure_dsp = !(of_property_read_bool(rdev->of_node, "qcom,non-secure-domain"));
22903abe3ab3SSrinivas Kandagatla 	data->secure = secure_dsp;
22913abe3ab3SSrinivas Kandagatla 
22923abe3ab3SSrinivas Kandagatla 	switch (domain_id) {
22933abe3ab3SSrinivas Kandagatla 	case ADSP_DOMAIN_ID:
22943abe3ab3SSrinivas Kandagatla 	case MDSP_DOMAIN_ID:
22953abe3ab3SSrinivas Kandagatla 	case SDSP_DOMAIN_ID:
22967f1f4812SJeya R 		/* Unsigned PD offloading is only supported on CDSP*/
22977f1f4812SJeya R 		data->unsigned_support = false;
22983abe3ab3SSrinivas Kandagatla 		err = fastrpc_device_register(rdev, data, secure_dsp, domains[domain_id]);
22993abe3ab3SSrinivas Kandagatla 		if (err)
23003abe3ab3SSrinivas Kandagatla 			goto fdev_error;
23013abe3ab3SSrinivas Kandagatla 		break;
23023abe3ab3SSrinivas Kandagatla 	case CDSP_DOMAIN_ID:
23037f1f4812SJeya R 		data->unsigned_support = true;
23043abe3ab3SSrinivas Kandagatla 		/* Create both device nodes so that we can allow both Signed and Unsigned PD */
23053abe3ab3SSrinivas Kandagatla 		err = fastrpc_device_register(rdev, data, true, domains[domain_id]);
23063abe3ab3SSrinivas Kandagatla 		if (err)
23073abe3ab3SSrinivas Kandagatla 			goto fdev_error;
23083abe3ab3SSrinivas Kandagatla 
23093abe3ab3SSrinivas Kandagatla 		err = fastrpc_device_register(rdev, data, false, domains[domain_id]);
23103abe3ab3SSrinivas Kandagatla 		if (err)
23113abe3ab3SSrinivas Kandagatla 			goto fdev_error;
23123abe3ab3SSrinivas Kandagatla 		break;
23133abe3ab3SSrinivas Kandagatla 	default:
23143abe3ab3SSrinivas Kandagatla 		err = -EINVAL;
23153abe3ab3SSrinivas Kandagatla 		goto fdev_error;
23160978de9fSSrinivas Kandagatla 	}
2317f6f9279fSSrinivas Kandagatla 
2318278d56f9SBjorn Andersson 	kref_init(&data->refcount);
2319278d56f9SBjorn Andersson 
2320f6f9279fSSrinivas Kandagatla 	dev_set_drvdata(&rpdev->dev, data);
23219bde43a0SAbel Vesa 	rdev->dma_mask = &data->dma_mask;
2322f6f9279fSSrinivas Kandagatla 	dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
2323f6f9279fSSrinivas Kandagatla 	INIT_LIST_HEAD(&data->users);
232476e8e4acSAbel Vesa 	INIT_LIST_HEAD(&data->invoke_interrupted_mmaps);
2325f6f9279fSSrinivas Kandagatla 	spin_lock_init(&data->lock);
2326f6f9279fSSrinivas Kandagatla 	idr_init(&data->ctx_idr);
2327f6f9279fSSrinivas Kandagatla 	data->domain_id = domain_id;
2328f6f9279fSSrinivas Kandagatla 	data->rpdev = rpdev;
2329f6f9279fSSrinivas Kandagatla 
233011819ed2SChristophe JAILLET 	err = of_platform_populate(rdev->of_node, NULL, NULL, rdev);
233111819ed2SChristophe JAILLET 	if (err)
233211819ed2SChristophe JAILLET 		goto populate_error;
233311819ed2SChristophe JAILLET 
233411819ed2SChristophe JAILLET 	return 0;
233511819ed2SChristophe JAILLET 
233611819ed2SChristophe JAILLET populate_error:
233711819ed2SChristophe JAILLET 	if (data->fdevice)
233811819ed2SChristophe JAILLET 		misc_deregister(&data->fdevice->miscdev);
233911819ed2SChristophe JAILLET 	if (data->secure_fdevice)
234011819ed2SChristophe JAILLET 		misc_deregister(&data->secure_fdevice->miscdev);
234111819ed2SChristophe JAILLET 
23423abe3ab3SSrinivas Kandagatla fdev_error:
23433abe3ab3SSrinivas Kandagatla 	kfree(data);
23443abe3ab3SSrinivas Kandagatla 	return err;
2345f6f9279fSSrinivas Kandagatla }
2346f6f9279fSSrinivas Kandagatla 
2347c68cfb71SSrinivas Kandagatla static void fastrpc_notify_users(struct fastrpc_user *user)
2348c68cfb71SSrinivas Kandagatla {
2349c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_ctx *ctx;
2350c68cfb71SSrinivas Kandagatla 
2351c68cfb71SSrinivas Kandagatla 	spin_lock(&user->lock);
2352c68cfb71SSrinivas Kandagatla 	list_for_each_entry(ctx, &user->pending, node)
2353c68cfb71SSrinivas Kandagatla 		complete(&ctx->work);
2354c68cfb71SSrinivas Kandagatla 	spin_unlock(&user->lock);
2355c68cfb71SSrinivas Kandagatla }
2356c68cfb71SSrinivas Kandagatla 
2357f6f9279fSSrinivas Kandagatla static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
2358f6f9279fSSrinivas Kandagatla {
2359f6f9279fSSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
236076e8e4acSAbel Vesa 	struct fastrpc_buf *buf, *b;
2361c68cfb71SSrinivas Kandagatla 	struct fastrpc_user *user;
2362977e6c8dSSrinivas Kandagatla 	unsigned long flags;
2363c68cfb71SSrinivas Kandagatla 
2364977e6c8dSSrinivas Kandagatla 	spin_lock_irqsave(&cctx->lock, flags);
2365c68cfb71SSrinivas Kandagatla 	list_for_each_entry(user, &cctx->users, user)
2366c68cfb71SSrinivas Kandagatla 		fastrpc_notify_users(user);
2367977e6c8dSSrinivas Kandagatla 	spin_unlock_irqrestore(&cctx->lock, flags);
2368f6f9279fSSrinivas Kandagatla 
2369965602eaSSrinivas Kandagatla 	if (cctx->fdevice)
2370965602eaSSrinivas Kandagatla 		misc_deregister(&cctx->fdevice->miscdev);
2371965602eaSSrinivas Kandagatla 
23723abe3ab3SSrinivas Kandagatla 	if (cctx->secure_fdevice)
23733abe3ab3SSrinivas Kandagatla 		misc_deregister(&cctx->secure_fdevice->miscdev);
23743abe3ab3SSrinivas Kandagatla 
237576e8e4acSAbel Vesa 	list_for_each_entry_safe(buf, b, &cctx->invoke_interrupted_mmaps, node)
237676e8e4acSAbel Vesa 		list_del(&buf->node);
237776e8e4acSAbel Vesa 
237808715610SAbel Vesa 	if (cctx->remote_heap)
237908715610SAbel Vesa 		fastrpc_buf_free(cctx->remote_heap);
238008715610SAbel Vesa 
2381f6f9279fSSrinivas Kandagatla 	of_platform_depopulate(&rpdev->dev);
2382278d56f9SBjorn Andersson 
23832e369878SBjorn Andersson 	cctx->rpdev = NULL;
2384278d56f9SBjorn Andersson 	fastrpc_channel_ctx_put(cctx);
2385f6f9279fSSrinivas Kandagatla }
2386f6f9279fSSrinivas Kandagatla 
2387f6f9279fSSrinivas Kandagatla static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
2388f6f9279fSSrinivas Kandagatla 				  int len, void *priv, u32 addr)
2389f6f9279fSSrinivas Kandagatla {
2390c68cfb71SSrinivas Kandagatla 	struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
2391c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_rsp *rsp = data;
2392c68cfb71SSrinivas Kandagatla 	struct fastrpc_invoke_ctx *ctx;
2393c68cfb71SSrinivas Kandagatla 	unsigned long flags;
2394c68cfb71SSrinivas Kandagatla 	unsigned long ctxid;
2395c68cfb71SSrinivas Kandagatla 
2396c68cfb71SSrinivas Kandagatla 	if (len < sizeof(*rsp))
2397c68cfb71SSrinivas Kandagatla 		return -EINVAL;
2398c68cfb71SSrinivas Kandagatla 
2399c68cfb71SSrinivas Kandagatla 	ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4);
2400c68cfb71SSrinivas Kandagatla 
2401c68cfb71SSrinivas Kandagatla 	spin_lock_irqsave(&cctx->lock, flags);
2402c68cfb71SSrinivas Kandagatla 	ctx = idr_find(&cctx->ctx_idr, ctxid);
2403c68cfb71SSrinivas Kandagatla 	spin_unlock_irqrestore(&cctx->lock, flags);
2404c68cfb71SSrinivas Kandagatla 
2405c68cfb71SSrinivas Kandagatla 	if (!ctx) {
2406c68cfb71SSrinivas Kandagatla 		dev_err(&rpdev->dev, "No context ID matches response\n");
2407c68cfb71SSrinivas Kandagatla 		return -ENOENT;
2408c68cfb71SSrinivas Kandagatla 	}
2409c68cfb71SSrinivas Kandagatla 
2410c68cfb71SSrinivas Kandagatla 	ctx->retval = rsp->retval;
2411c68cfb71SSrinivas Kandagatla 	complete(&ctx->work);
24128e7389c7SThierry Escande 
24138e7389c7SThierry Escande 	/*
24148e7389c7SThierry Escande 	 * The DMA buffer associated with the context cannot be freed in
24158e7389c7SThierry Escande 	 * interrupt context so schedule it through a worker thread to
24168e7389c7SThierry Escande 	 * avoid a kernel BUG.
24178e7389c7SThierry Escande 	 */
24188e7389c7SThierry Escande 	schedule_work(&ctx->put_work);
2419c68cfb71SSrinivas Kandagatla 
2420f6f9279fSSrinivas Kandagatla 	return 0;
2421f6f9279fSSrinivas Kandagatla }
2422f6f9279fSSrinivas Kandagatla 
2423f6f9279fSSrinivas Kandagatla static const struct of_device_id fastrpc_rpmsg_of_match[] = {
2424f6f9279fSSrinivas Kandagatla 	{ .compatible = "qcom,fastrpc" },
2425f6f9279fSSrinivas Kandagatla 	{ },
2426f6f9279fSSrinivas Kandagatla };
2427f6f9279fSSrinivas Kandagatla MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match);
2428f6f9279fSSrinivas Kandagatla 
2429f6f9279fSSrinivas Kandagatla static struct rpmsg_driver fastrpc_driver = {
2430f6f9279fSSrinivas Kandagatla 	.probe = fastrpc_rpmsg_probe,
2431f6f9279fSSrinivas Kandagatla 	.remove = fastrpc_rpmsg_remove,
2432f6f9279fSSrinivas Kandagatla 	.callback = fastrpc_rpmsg_callback,
2433f6f9279fSSrinivas Kandagatla 	.drv = {
2434f6f9279fSSrinivas Kandagatla 		.name = "qcom,fastrpc",
2435f6f9279fSSrinivas Kandagatla 		.of_match_table = fastrpc_rpmsg_of_match,
2436f6f9279fSSrinivas Kandagatla 	},
2437f6f9279fSSrinivas Kandagatla };
2438f6f9279fSSrinivas Kandagatla 
2439f6f9279fSSrinivas Kandagatla static int fastrpc_init(void)
2440f6f9279fSSrinivas Kandagatla {
2441f6f9279fSSrinivas Kandagatla 	int ret;
2442f6f9279fSSrinivas Kandagatla 
2443f6f9279fSSrinivas Kandagatla 	ret = platform_driver_register(&fastrpc_cb_driver);
2444f6f9279fSSrinivas Kandagatla 	if (ret < 0) {
2445f6f9279fSSrinivas Kandagatla 		pr_err("fastrpc: failed to register cb driver\n");
2446f6f9279fSSrinivas Kandagatla 		return ret;
2447f6f9279fSSrinivas Kandagatla 	}
2448f6f9279fSSrinivas Kandagatla 
2449f6f9279fSSrinivas Kandagatla 	ret = register_rpmsg_driver(&fastrpc_driver);
2450f6f9279fSSrinivas Kandagatla 	if (ret < 0) {
2451f6f9279fSSrinivas Kandagatla 		pr_err("fastrpc: failed to register rpmsg driver\n");
2452f6f9279fSSrinivas Kandagatla 		platform_driver_unregister(&fastrpc_cb_driver);
2453f6f9279fSSrinivas Kandagatla 		return ret;
2454f6f9279fSSrinivas Kandagatla 	}
2455f6f9279fSSrinivas Kandagatla 
2456f6f9279fSSrinivas Kandagatla 	return 0;
2457f6f9279fSSrinivas Kandagatla }
2458f6f9279fSSrinivas Kandagatla module_init(fastrpc_init);
2459f6f9279fSSrinivas Kandagatla 
2460f6f9279fSSrinivas Kandagatla static void fastrpc_exit(void)
2461f6f9279fSSrinivas Kandagatla {
2462f6f9279fSSrinivas Kandagatla 	platform_driver_unregister(&fastrpc_cb_driver);
2463f6f9279fSSrinivas Kandagatla 	unregister_rpmsg_driver(&fastrpc_driver);
2464f6f9279fSSrinivas Kandagatla }
2465f6f9279fSSrinivas Kandagatla module_exit(fastrpc_exit);
2466f6f9279fSSrinivas Kandagatla 
2467f6f9279fSSrinivas Kandagatla MODULE_LICENSE("GPL v2");
246816b0314aSGreg Kroah-Hartman MODULE_IMPORT_NS(DMA_BUF);
2469