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>
16d9c58aebSRob Herring #include <linux/platform_device.h>
1725e8dfb8SSrinivas Kandagatla #include <linux/sort.h>
18f6f9279fSSrinivas Kandagatla #include <linux/of_platform.h>
19f6f9279fSSrinivas Kandagatla #include <linux/rpmsg.h>
20f6f9279fSSrinivas Kandagatla #include <linux/scatterlist.h>
21f6f9279fSSrinivas Kandagatla #include <linux/slab.h>
223bf90ecaSElliot Berman #include <linux/firmware/qcom/qcom_scm.h>
23c68cfb71SSrinivas Kandagatla #include <uapi/misc/fastrpc.h>
241ce91d45SAbel Vesa #include <linux/of_reserved_mem.h>
25f6f9279fSSrinivas Kandagatla
26f6f9279fSSrinivas Kandagatla #define ADSP_DOMAIN_ID (0)
27f6f9279fSSrinivas Kandagatla #define MDSP_DOMAIN_ID (1)
28f6f9279fSSrinivas Kandagatla #define SDSP_DOMAIN_ID (2)
29f6f9279fSSrinivas Kandagatla #define CDSP_DOMAIN_ID (3)
30f6f9279fSSrinivas Kandagatla #define FASTRPC_DEV_MAX 4 /* adsp, mdsp, slpi, cdsp*/
31689a2d9fSJohan Hovold #define FASTRPC_MAX_SESSIONS 14
32e90d9119SVamsi Krishna Gattupalli #define FASTRPC_MAX_VMIDS 16
33c68cfb71SSrinivas Kandagatla #define FASTRPC_ALIGN 128
34c68cfb71SSrinivas Kandagatla #define FASTRPC_MAX_FDLIST 16
35c68cfb71SSrinivas Kandagatla #define FASTRPC_MAX_CRCLIST 64
36c68cfb71SSrinivas Kandagatla #define FASTRPC_PHYS(p) ((p) & 0xffffffff)
37f6f9279fSSrinivas Kandagatla #define FASTRPC_CTX_MAX (256)
38d73f71c7SSrinivas Kandagatla #define FASTRPC_INIT_HANDLE 1
396c16fd8bSJeya R #define FASTRPC_DSP_UTILITIES_HANDLE 2
40f6f9279fSSrinivas Kandagatla #define FASTRPC_CTXID_MASK (0xFF0)
41efcd2390SJorge Ramirez-Ortiz #define INIT_FILELEN_MAX (2 * 1024 * 1024)
4208715610SAbel Vesa #define INIT_FILE_NAMELEN_MAX (128)
43f6f9279fSSrinivas Kandagatla #define FASTRPC_DEVICE_NAME "fastrpc"
4408715610SAbel Vesa
4508715610SAbel Vesa /* Add memory to static PD pool, protection thru XPU */
4608715610SAbel Vesa #define ADSP_MMAP_HEAP_ADDR 4
4708715610SAbel Vesa /* MAP static DMA buffer on DSP User PD */
4808715610SAbel Vesa #define ADSP_MMAP_DMA_BUFFER 6
4908715610SAbel Vesa /* Add memory to static PD pool protection thru hypervisor */
5008715610SAbel Vesa #define ADSP_MMAP_REMOTE_HEAP_ADDR 8
5108715610SAbel Vesa /* Add memory to userPD pool, for user heap */
522419e55eSJorge Ramirez-Ortiz #define ADSP_MMAP_ADD_PAGES 0x1000
5308715610SAbel Vesa /* Add memory to userPD pool, for LLC heap */
5408715610SAbel Vesa #define ADSP_MMAP_ADD_PAGES_LLC 0x3000,
5508715610SAbel Vesa
566c16fd8bSJeya R #define DSP_UNSUPPORTED_API (0x80000414)
576c16fd8bSJeya R /* MAX NUMBER of DSP ATTRIBUTES SUPPORTED */
586c16fd8bSJeya R #define FASTRPC_MAX_DSP_ATTRIBUTES (256)
596c16fd8bSJeya R #define FASTRPC_MAX_DSP_ATTRIBUTES_LEN (sizeof(u32) * FASTRPC_MAX_DSP_ATTRIBUTES)
60f6f9279fSSrinivas Kandagatla
61c68cfb71SSrinivas Kandagatla /* Retrives number of input buffers from the scalars parameter */
62c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_INBUFS(sc) (((sc) >> 16) & 0x0ff)
63c68cfb71SSrinivas Kandagatla
64c68cfb71SSrinivas Kandagatla /* Retrives number of output buffers from the scalars parameter */
65c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_OUTBUFS(sc) (((sc) >> 8) & 0x0ff)
66c68cfb71SSrinivas Kandagatla
67c68cfb71SSrinivas Kandagatla /* Retrives number of input handles from the scalars parameter */
68c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_INHANDLES(sc) (((sc) >> 4) & 0x0f)
69c68cfb71SSrinivas Kandagatla
70c68cfb71SSrinivas Kandagatla /* Retrives number of output handles from the scalars parameter */
71c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_OUTHANDLES(sc) ((sc) & 0x0f)
72c68cfb71SSrinivas Kandagatla
73c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_LENGTH(sc) (REMOTE_SCALARS_INBUFS(sc) + \
74c68cfb71SSrinivas Kandagatla REMOTE_SCALARS_OUTBUFS(sc) + \
75c68cfb71SSrinivas Kandagatla REMOTE_SCALARS_INHANDLES(sc)+ \
76c68cfb71SSrinivas Kandagatla REMOTE_SCALARS_OUTHANDLES(sc))
77c68cfb71SSrinivas Kandagatla #define FASTRPC_BUILD_SCALARS(attr, method, in, out, oin, oout) \
78c68cfb71SSrinivas Kandagatla (((attr & 0x07) << 29) | \
79c68cfb71SSrinivas Kandagatla ((method & 0x1f) << 24) | \
80c68cfb71SSrinivas Kandagatla ((in & 0xff) << 16) | \
81c68cfb71SSrinivas Kandagatla ((out & 0xff) << 8) | \
82c68cfb71SSrinivas Kandagatla ((oin & 0x0f) << 4) | \
83c68cfb71SSrinivas Kandagatla (oout & 0x0f))
84c68cfb71SSrinivas Kandagatla
85c68cfb71SSrinivas Kandagatla #define FASTRPC_SCALARS(method, in, out) \
86c68cfb71SSrinivas Kandagatla FASTRPC_BUILD_SCALARS(0, method, in, out, 0, 0)
87c68cfb71SSrinivas Kandagatla
88d73f71c7SSrinivas Kandagatla #define FASTRPC_CREATE_PROCESS_NARGS 6
8908715610SAbel Vesa #define FASTRPC_CREATE_STATIC_PROCESS_NARGS 3
90d73f71c7SSrinivas Kandagatla /* Remote Method id table */
91d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_ATTACH 0
92d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_RELEASE 1
932419e55eSJorge Ramirez-Ortiz #define FASTRPC_RMID_INIT_MMAP 4
942419e55eSJorge Ramirez-Ortiz #define FASTRPC_RMID_INIT_MUNMAP 5
95d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE 6
96d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE_ATTR 7
97d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE_STATIC 8
985c1b97c7SJeya R #define FASTRPC_RMID_INIT_MEM_MAP 10
995c1b97c7SJeya R #define FASTRPC_RMID_INIT_MEM_UNMAP 11
100d73f71c7SSrinivas Kandagatla
10184195d20SJonathan Marek /* Protection Domain(PD) ids */
1021959ab9eSAbel Vesa #define ROOT_PD (0)
10384195d20SJonathan Marek #define USER_PD (1)
10484195d20SJonathan Marek #define SENSORS_PD (2)
10584195d20SJonathan Marek
106965602eaSSrinivas Kandagatla #define miscdev_to_fdevice(d) container_of(d, struct fastrpc_device, miscdev)
107f6f9279fSSrinivas Kandagatla
108f6f9279fSSrinivas Kandagatla static const char *domains[FASTRPC_DEV_MAX] = { "adsp", "mdsp",
109f6f9279fSSrinivas Kandagatla "sdsp", "cdsp"};
110c68cfb71SSrinivas Kandagatla struct fastrpc_phy_page {
111c68cfb71SSrinivas Kandagatla u64 addr; /* physical address */
112c68cfb71SSrinivas Kandagatla u64 size; /* size of contiguous region */
113c68cfb71SSrinivas Kandagatla };
114c68cfb71SSrinivas Kandagatla
115c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_buf {
116c68cfb71SSrinivas Kandagatla u32 num; /* number of contiguous regions */
117c68cfb71SSrinivas Kandagatla u32 pgidx; /* index to start of contiguous region */
118c68cfb71SSrinivas Kandagatla };
119c68cfb71SSrinivas Kandagatla
12035a82b87SVamsi Krishna Gattupalli struct fastrpc_remote_dmahandle {
12135a82b87SVamsi Krishna Gattupalli s32 fd; /* dma handle fd */
12235a82b87SVamsi Krishna Gattupalli u32 offset; /* dma handle offset */
12335a82b87SVamsi Krishna Gattupalli u32 len; /* dma handle length */
12435a82b87SVamsi Krishna Gattupalli };
12535a82b87SVamsi Krishna Gattupalli
12635a82b87SVamsi Krishna Gattupalli struct fastrpc_remote_buf {
12735a82b87SVamsi Krishna Gattupalli u64 pv; /* buffer pointer */
12835a82b87SVamsi Krishna Gattupalli u64 len; /* length of buffer */
12935a82b87SVamsi Krishna Gattupalli };
13035a82b87SVamsi Krishna Gattupalli
13135a82b87SVamsi Krishna Gattupalli union fastrpc_remote_arg {
13235a82b87SVamsi Krishna Gattupalli struct fastrpc_remote_buf buf;
13335a82b87SVamsi Krishna Gattupalli struct fastrpc_remote_dmahandle dma;
134c68cfb71SSrinivas Kandagatla };
135c68cfb71SSrinivas Kandagatla
1362419e55eSJorge Ramirez-Ortiz struct fastrpc_mmap_rsp_msg {
1372419e55eSJorge Ramirez-Ortiz u64 vaddr;
1382419e55eSJorge Ramirez-Ortiz };
1392419e55eSJorge Ramirez-Ortiz
1402419e55eSJorge Ramirez-Ortiz struct fastrpc_mmap_req_msg {
1412419e55eSJorge Ramirez-Ortiz s32 pgid;
1422419e55eSJorge Ramirez-Ortiz u32 flags;
1432419e55eSJorge Ramirez-Ortiz u64 vaddr;
1442419e55eSJorge Ramirez-Ortiz s32 num;
1452419e55eSJorge Ramirez-Ortiz };
1462419e55eSJorge Ramirez-Ortiz
1475c1b97c7SJeya R struct fastrpc_mem_map_req_msg {
1485c1b97c7SJeya R s32 pgid;
1495c1b97c7SJeya R s32 fd;
1505c1b97c7SJeya R s32 offset;
1515c1b97c7SJeya R u32 flags;
1525c1b97c7SJeya R u64 vaddrin;
1535c1b97c7SJeya R s32 num;
1545c1b97c7SJeya R s32 data_len;
1555c1b97c7SJeya R };
1565c1b97c7SJeya R
1572419e55eSJorge Ramirez-Ortiz struct fastrpc_munmap_req_msg {
1582419e55eSJorge Ramirez-Ortiz s32 pgid;
1592419e55eSJorge Ramirez-Ortiz u64 vaddr;
1602419e55eSJorge Ramirez-Ortiz u64 size;
1612419e55eSJorge Ramirez-Ortiz };
1622419e55eSJorge Ramirez-Ortiz
1635c1b97c7SJeya R struct fastrpc_mem_unmap_req_msg {
1645c1b97c7SJeya R s32 pgid;
1655c1b97c7SJeya R s32 fd;
1665c1b97c7SJeya R u64 vaddrin;
1675c1b97c7SJeya R u64 len;
1685c1b97c7SJeya R };
1695c1b97c7SJeya R
170c68cfb71SSrinivas Kandagatla struct fastrpc_msg {
171c68cfb71SSrinivas Kandagatla int pid; /* process group id */
172c68cfb71SSrinivas Kandagatla int tid; /* thread id */
173c68cfb71SSrinivas Kandagatla u64 ctx; /* invoke caller context */
174c68cfb71SSrinivas Kandagatla u32 handle; /* handle to invoke */
175c68cfb71SSrinivas Kandagatla u32 sc; /* scalars structure describing the data */
176c68cfb71SSrinivas Kandagatla u64 addr; /* physical address */
177c68cfb71SSrinivas Kandagatla u64 size; /* size of contiguous region */
178c68cfb71SSrinivas Kandagatla };
179c68cfb71SSrinivas Kandagatla
180c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_rsp {
181c68cfb71SSrinivas Kandagatla u64 ctx; /* invoke caller context */
182c68cfb71SSrinivas Kandagatla int retval; /* invoke return value */
183c68cfb71SSrinivas Kandagatla };
184c68cfb71SSrinivas Kandagatla
18525e8dfb8SSrinivas Kandagatla struct fastrpc_buf_overlap {
18625e8dfb8SSrinivas Kandagatla u64 start;
18725e8dfb8SSrinivas Kandagatla u64 end;
18825e8dfb8SSrinivas Kandagatla int raix;
18925e8dfb8SSrinivas Kandagatla u64 mstart;
19025e8dfb8SSrinivas Kandagatla u64 mend;
19125e8dfb8SSrinivas Kandagatla u64 offset;
19225e8dfb8SSrinivas Kandagatla };
19325e8dfb8SSrinivas Kandagatla
194c68cfb71SSrinivas Kandagatla struct fastrpc_buf {
195c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl;
1966cffd795SSrinivas Kandagatla struct dma_buf *dmabuf;
197c68cfb71SSrinivas Kandagatla struct device *dev;
198c68cfb71SSrinivas Kandagatla void *virt;
199c68cfb71SSrinivas Kandagatla u64 phys;
200c68cfb71SSrinivas Kandagatla u64 size;
2016cffd795SSrinivas Kandagatla /* Lock for dma buf attachments */
2026cffd795SSrinivas Kandagatla struct mutex lock;
2036cffd795SSrinivas Kandagatla struct list_head attachments;
2042419e55eSJorge Ramirez-Ortiz /* mmap support */
2052419e55eSJorge Ramirez-Ortiz struct list_head node; /* list of user requested mmaps */
2062419e55eSJorge Ramirez-Ortiz uintptr_t raddr;
2076cffd795SSrinivas Kandagatla };
2086cffd795SSrinivas Kandagatla
2096cffd795SSrinivas Kandagatla struct fastrpc_dma_buf_attachment {
2106cffd795SSrinivas Kandagatla struct device *dev;
2116cffd795SSrinivas Kandagatla struct sg_table sgt;
2126cffd795SSrinivas Kandagatla struct list_head node;
213c68cfb71SSrinivas Kandagatla };
214c68cfb71SSrinivas Kandagatla
215c68cfb71SSrinivas Kandagatla struct fastrpc_map {
216c68cfb71SSrinivas Kandagatla struct list_head node;
217c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl;
218c68cfb71SSrinivas Kandagatla int fd;
219c68cfb71SSrinivas Kandagatla struct dma_buf *buf;
220c68cfb71SSrinivas Kandagatla struct sg_table *table;
221c68cfb71SSrinivas Kandagatla struct dma_buf_attachment *attach;
222c68cfb71SSrinivas Kandagatla u64 phys;
223c68cfb71SSrinivas Kandagatla u64 size;
224c68cfb71SSrinivas Kandagatla void *va;
225c68cfb71SSrinivas Kandagatla u64 len;
2265c1b97c7SJeya R u64 raddr;
227e90d9119SVamsi Krishna Gattupalli u32 attr;
228c68cfb71SSrinivas Kandagatla struct kref refcount;
229c68cfb71SSrinivas Kandagatla };
230c68cfb71SSrinivas Kandagatla
231c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx {
232c68cfb71SSrinivas Kandagatla int nscalars;
233c68cfb71SSrinivas Kandagatla int nbufs;
234c68cfb71SSrinivas Kandagatla int retval;
235c68cfb71SSrinivas Kandagatla int pid;
236c68cfb71SSrinivas Kandagatla int tgid;
237c68cfb71SSrinivas Kandagatla u32 sc;
238c68cfb71SSrinivas Kandagatla u32 *crc;
239c68cfb71SSrinivas Kandagatla u64 ctxid;
240c68cfb71SSrinivas Kandagatla u64 msg_sz;
241c68cfb71SSrinivas Kandagatla struct kref refcount;
242c68cfb71SSrinivas Kandagatla struct list_head node; /* list of ctxs */
243c68cfb71SSrinivas Kandagatla struct completion work;
2448e7389c7SThierry Escande struct work_struct put_work;
245c68cfb71SSrinivas Kandagatla struct fastrpc_msg msg;
246c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl;
24735a82b87SVamsi Krishna Gattupalli union fastrpc_remote_arg *rpra;
248c68cfb71SSrinivas Kandagatla struct fastrpc_map **maps;
249c68cfb71SSrinivas Kandagatla struct fastrpc_buf *buf;
250c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_args *args;
25125e8dfb8SSrinivas Kandagatla struct fastrpc_buf_overlap *olaps;
252c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx;
253c68cfb71SSrinivas Kandagatla };
254f6f9279fSSrinivas Kandagatla
255f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx {
256f6f9279fSSrinivas Kandagatla struct device *dev;
257f6f9279fSSrinivas Kandagatla int sid;
258f6f9279fSSrinivas Kandagatla bool used;
259f6f9279fSSrinivas Kandagatla bool valid;
260f6f9279fSSrinivas Kandagatla };
261f6f9279fSSrinivas Kandagatla
262f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx {
263f6f9279fSSrinivas Kandagatla int domain_id;
264f6f9279fSSrinivas Kandagatla int sesscount;
265e90d9119SVamsi Krishna Gattupalli int vmcount;
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
fastrpc_free_map(struct kref * ref)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
fastrpc_map_put(struct fastrpc_map * map)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
fastrpc_map_get(struct fastrpc_map * map)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
fastrpc_map_lookup(struct fastrpc_user * fl,int fd,struct fastrpc_map ** ppmap,bool take_ref)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
fastrpc_buf_free(struct fastrpc_buf * buf)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
__fastrpc_buf_alloc(struct fastrpc_user * fl,struct device * dev,u64 size,struct fastrpc_buf ** obuf)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
fastrpc_buf_alloc(struct fastrpc_user * fl,struct device * dev,u64 size,struct fastrpc_buf ** obuf)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
fastrpc_remote_heap_alloc(struct fastrpc_user * fl,struct device * dev,u64 size,struct fastrpc_buf ** obuf)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
fastrpc_channel_ctx_free(struct kref * ref)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
fastrpc_channel_ctx_get(struct fastrpc_channel_ctx * cctx)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
fastrpc_channel_ctx_put(struct fastrpc_channel_ctx * cctx)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
fastrpc_context_free(struct kref * ref)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
fastrpc_context_get(struct fastrpc_invoke_ctx * ctx)506c68cfb71SSrinivas Kandagatla static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx)
507c68cfb71SSrinivas Kandagatla {
508c68cfb71SSrinivas Kandagatla kref_get(&ctx->refcount);
509c68cfb71SSrinivas Kandagatla }
510c68cfb71SSrinivas Kandagatla
fastrpc_context_put(struct fastrpc_invoke_ctx * ctx)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
fastrpc_context_put_wq(struct work_struct * work)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)
olaps_cmp(const void * a,const void * b)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
fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx * ctx)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
fastrpc_context_alloc(struct fastrpc_user * user,u32 kernel,u32 sc,struct fastrpc_invoke_args * args)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 *
fastrpc_map_dma_buf(struct dma_buf_attachment * attachment,enum dma_data_direction dir)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
fastrpc_unmap_dma_buf(struct dma_buf_attachment * attach,struct sg_table * table,enum dma_data_direction dir)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
fastrpc_release(struct dma_buf * dmabuf)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
fastrpc_dma_buf_attach(struct dma_buf * dmabuf,struct dma_buf_attachment * attachment)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
fastrpc_dma_buf_detatch(struct dma_buf * dmabuf,struct dma_buf_attachment * attachment)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
fastrpc_vmap(struct dma_buf * dmabuf,struct iosys_map * map)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
fastrpc_mmap(struct dma_buf * dmabuf,struct vm_area_struct * vma)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
fastrpc_map_create(struct fastrpc_user * fl,int fd,u64 len,u32 attr,struct fastrpc_map ** ppmap)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;
759a2cb9cd6SEkansh Gupta struct sg_table *table;
760c68cfb71SSrinivas Kandagatla int err = 0;
761c68cfb71SSrinivas Kandagatla
7629446fa16SAbel Vesa if (!fastrpc_map_lookup(fl, fd, ppmap, true))
763c68cfb71SSrinivas Kandagatla return 0;
764c68cfb71SSrinivas Kandagatla
765c68cfb71SSrinivas Kandagatla map = kzalloc(sizeof(*map), GFP_KERNEL);
766c68cfb71SSrinivas Kandagatla if (!map)
767c68cfb71SSrinivas Kandagatla return -ENOMEM;
768c68cfb71SSrinivas Kandagatla
769c68cfb71SSrinivas Kandagatla INIT_LIST_HEAD(&map->node);
770334f1a1cSAbel Vesa kref_init(&map->refcount);
771334f1a1cSAbel Vesa
772c68cfb71SSrinivas Kandagatla map->fl = fl;
773c68cfb71SSrinivas Kandagatla map->fd = fd;
774c68cfb71SSrinivas Kandagatla map->buf = dma_buf_get(fd);
775682a6044SWei Yongjun if (IS_ERR(map->buf)) {
776682a6044SWei Yongjun err = PTR_ERR(map->buf);
777c68cfb71SSrinivas Kandagatla goto get_err;
778c68cfb71SSrinivas Kandagatla }
779c68cfb71SSrinivas Kandagatla
780c68cfb71SSrinivas Kandagatla map->attach = dma_buf_attach(map->buf, sess->dev);
781c68cfb71SSrinivas Kandagatla if (IS_ERR(map->attach)) {
782c68cfb71SSrinivas Kandagatla dev_err(sess->dev, "Failed to attach dmabuf\n");
783c68cfb71SSrinivas Kandagatla err = PTR_ERR(map->attach);
784c68cfb71SSrinivas Kandagatla goto attach_err;
785c68cfb71SSrinivas Kandagatla }
786c68cfb71SSrinivas Kandagatla
787a2cb9cd6SEkansh Gupta table = dma_buf_map_attachment_unlocked(map->attach, DMA_BIDIRECTIONAL);
788a2cb9cd6SEkansh Gupta if (IS_ERR(table)) {
789a2cb9cd6SEkansh Gupta err = PTR_ERR(table);
790c68cfb71SSrinivas Kandagatla goto map_err;
791c68cfb71SSrinivas Kandagatla }
792a2cb9cd6SEkansh Gupta map->table = table;
793c68cfb71SSrinivas Kandagatla
794a6e766deSEkansh Gupta if (attr & FASTRPC_ATTR_SECUREMAP) {
795a6e766deSEkansh Gupta map->phys = sg_phys(map->table->sgl);
796a6e766deSEkansh Gupta } else {
797c68cfb71SSrinivas Kandagatla map->phys = sg_dma_address(map->table->sgl);
798c68cfb71SSrinivas Kandagatla map->phys += ((u64)fl->sctx->sid << 32);
799a6e766deSEkansh Gupta }
800c68cfb71SSrinivas Kandagatla map->size = len;
801c68cfb71SSrinivas Kandagatla map->va = sg_virt(map->table->sgl);
802c68cfb71SSrinivas Kandagatla map->len = len;
803c68cfb71SSrinivas Kandagatla
804e90d9119SVamsi Krishna Gattupalli if (attr & FASTRPC_ATTR_SECUREMAP) {
805e90d9119SVamsi Krishna Gattupalli /*
806e90d9119SVamsi Krishna Gattupalli * If subsystem VMIDs are defined in DTSI, then do
807e90d9119SVamsi Krishna Gattupalli * hyp_assign from HLOS to those VM(s)
808e90d9119SVamsi Krishna Gattupalli */
809a6e766deSEkansh Gupta u64 src_perms = BIT(QCOM_SCM_VMID_HLOS);
810a6e766deSEkansh Gupta struct qcom_scm_vmperm dst_perms[2] = {0};
811a6e766deSEkansh Gupta
812a6e766deSEkansh Gupta dst_perms[0].vmid = QCOM_SCM_VMID_HLOS;
813a6e766deSEkansh Gupta dst_perms[0].perm = QCOM_SCM_PERM_RW;
814a6e766deSEkansh Gupta dst_perms[1].vmid = fl->cctx->vmperms[0].vmid;
815a6e766deSEkansh Gupta dst_perms[1].perm = QCOM_SCM_PERM_RWX;
816e90d9119SVamsi Krishna Gattupalli map->attr = attr;
817a6e766deSEkansh Gupta err = qcom_scm_assign_mem(map->phys, (u64)map->size, &src_perms, dst_perms, 2);
818e90d9119SVamsi Krishna Gattupalli if (err) {
819e90d9119SVamsi Krishna Gattupalli dev_err(sess->dev, "Failed to assign memory with phys 0x%llx size 0x%llx err %d",
820e90d9119SVamsi Krishna Gattupalli map->phys, map->size, err);
821e90d9119SVamsi Krishna Gattupalli goto map_err;
822e90d9119SVamsi Krishna Gattupalli }
823e90d9119SVamsi Krishna Gattupalli }
824c68cfb71SSrinivas Kandagatla spin_lock(&fl->lock);
825c68cfb71SSrinivas Kandagatla list_add_tail(&map->node, &fl->maps);
826c68cfb71SSrinivas Kandagatla spin_unlock(&fl->lock);
827c68cfb71SSrinivas Kandagatla *ppmap = map;
828c68cfb71SSrinivas Kandagatla
829c68cfb71SSrinivas Kandagatla return 0;
830c68cfb71SSrinivas Kandagatla
831c68cfb71SSrinivas Kandagatla map_err:
832c68cfb71SSrinivas Kandagatla dma_buf_detach(map->buf, map->attach);
833c68cfb71SSrinivas Kandagatla attach_err:
834c68cfb71SSrinivas Kandagatla dma_buf_put(map->buf);
835c68cfb71SSrinivas Kandagatla get_err:
836334f1a1cSAbel Vesa fastrpc_map_put(map);
837c68cfb71SSrinivas Kandagatla
838c68cfb71SSrinivas Kandagatla return err;
839c68cfb71SSrinivas Kandagatla }
840c68cfb71SSrinivas Kandagatla
841c68cfb71SSrinivas Kandagatla /*
842c68cfb71SSrinivas Kandagatla * Fastrpc payload buffer with metadata looks like:
843c68cfb71SSrinivas Kandagatla *
844c68cfb71SSrinivas Kandagatla * >>>>>> START of METADATA <<<<<<<<<
845c68cfb71SSrinivas Kandagatla * +---------------------------------+
846c68cfb71SSrinivas Kandagatla * | Arguments |
84735a82b87SVamsi Krishna Gattupalli * | type:(union fastrpc_remote_arg)|
848c68cfb71SSrinivas Kandagatla * | (0 - N) |
849c68cfb71SSrinivas Kandagatla * +---------------------------------+
850c68cfb71SSrinivas Kandagatla * | Invoke Buffer list |
851c68cfb71SSrinivas Kandagatla * | type:(struct fastrpc_invoke_buf)|
852c68cfb71SSrinivas Kandagatla * | (0 - N) |
853c68cfb71SSrinivas Kandagatla * +---------------------------------+
854c68cfb71SSrinivas Kandagatla * | Page info list |
855c68cfb71SSrinivas Kandagatla * | type:(struct fastrpc_phy_page) |
856c68cfb71SSrinivas Kandagatla * | (0 - N) |
857c68cfb71SSrinivas Kandagatla * +---------------------------------+
858c68cfb71SSrinivas Kandagatla * | Optional info |
859c68cfb71SSrinivas Kandagatla * |(can be specific to SoC/Firmware)|
860c68cfb71SSrinivas Kandagatla * +---------------------------------+
861c68cfb71SSrinivas Kandagatla * >>>>>>>> END of METADATA <<<<<<<<<
862c68cfb71SSrinivas Kandagatla * +---------------------------------+
863c68cfb71SSrinivas Kandagatla * | Inline ARGS |
864c68cfb71SSrinivas Kandagatla * | (0-N) |
865c68cfb71SSrinivas Kandagatla * +---------------------------------+
866c68cfb71SSrinivas Kandagatla */
867c68cfb71SSrinivas Kandagatla
fastrpc_get_meta_size(struct fastrpc_invoke_ctx * ctx)868c68cfb71SSrinivas Kandagatla static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx)
869c68cfb71SSrinivas Kandagatla {
870c68cfb71SSrinivas Kandagatla int size = 0;
871c68cfb71SSrinivas Kandagatla
87235a82b87SVamsi Krishna Gattupalli size = (sizeof(struct fastrpc_remote_buf) +
873c68cfb71SSrinivas Kandagatla sizeof(struct fastrpc_invoke_buf) +
874c68cfb71SSrinivas Kandagatla sizeof(struct fastrpc_phy_page)) * ctx->nscalars +
875c68cfb71SSrinivas Kandagatla sizeof(u64) * FASTRPC_MAX_FDLIST +
876c68cfb71SSrinivas Kandagatla sizeof(u32) * FASTRPC_MAX_CRCLIST;
877c68cfb71SSrinivas Kandagatla
878c68cfb71SSrinivas Kandagatla return size;
879c68cfb71SSrinivas Kandagatla }
880c68cfb71SSrinivas Kandagatla
fastrpc_get_payload_size(struct fastrpc_invoke_ctx * ctx,int metalen)881c68cfb71SSrinivas Kandagatla static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen)
882c68cfb71SSrinivas Kandagatla {
883c68cfb71SSrinivas Kandagatla u64 size = 0;
8843a1bf591SJeya R int oix;
885c68cfb71SSrinivas Kandagatla
886c68cfb71SSrinivas Kandagatla size = ALIGN(metalen, FASTRPC_ALIGN);
8873a1bf591SJeya R for (oix = 0; oix < ctx->nbufs; oix++) {
8883a1bf591SJeya R int i = ctx->olaps[oix].raix;
8893a1bf591SJeya R
890c68cfb71SSrinivas Kandagatla if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) {
89125e8dfb8SSrinivas Kandagatla
8923a1bf591SJeya R if (ctx->olaps[oix].offset == 0)
893c68cfb71SSrinivas Kandagatla size = ALIGN(size, FASTRPC_ALIGN);
89425e8dfb8SSrinivas Kandagatla
8953a1bf591SJeya R size += (ctx->olaps[oix].mend - ctx->olaps[oix].mstart);
896c68cfb71SSrinivas Kandagatla }
897c68cfb71SSrinivas Kandagatla }
898c68cfb71SSrinivas Kandagatla
899c68cfb71SSrinivas Kandagatla return size;
900c68cfb71SSrinivas Kandagatla }
901c68cfb71SSrinivas Kandagatla
fastrpc_create_maps(struct fastrpc_invoke_ctx * ctx)902c68cfb71SSrinivas Kandagatla static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
903c68cfb71SSrinivas Kandagatla {
904c68cfb71SSrinivas Kandagatla struct device *dev = ctx->fl->sctx->dev;
905c68cfb71SSrinivas Kandagatla int i, err;
906c68cfb71SSrinivas Kandagatla
907c68cfb71SSrinivas Kandagatla for (i = 0; i < ctx->nscalars; ++i) {
908c68cfb71SSrinivas Kandagatla
909c68cfb71SSrinivas Kandagatla if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 ||
910c68cfb71SSrinivas Kandagatla ctx->args[i].length == 0)
911c68cfb71SSrinivas Kandagatla continue;
912c68cfb71SSrinivas Kandagatla
913c68cfb71SSrinivas Kandagatla err = fastrpc_map_create(ctx->fl, ctx->args[i].fd,
914e90d9119SVamsi Krishna Gattupalli ctx->args[i].length, ctx->args[i].attr, &ctx->maps[i]);
915c68cfb71SSrinivas Kandagatla if (err) {
916c68cfb71SSrinivas Kandagatla dev_err(dev, "Error Creating map %d\n", err);
917c68cfb71SSrinivas Kandagatla return -EINVAL;
918c68cfb71SSrinivas Kandagatla }
919c68cfb71SSrinivas Kandagatla
920c68cfb71SSrinivas Kandagatla }
921c68cfb71SSrinivas Kandagatla return 0;
922c68cfb71SSrinivas Kandagatla }
923c68cfb71SSrinivas Kandagatla
fastrpc_invoke_buf_start(union fastrpc_remote_arg * pra,int len)92454f7c85bSVamsi Krishna Gattupalli static struct fastrpc_invoke_buf *fastrpc_invoke_buf_start(union fastrpc_remote_arg *pra, int len)
92554f7c85bSVamsi Krishna Gattupalli {
92654f7c85bSVamsi Krishna Gattupalli return (struct fastrpc_invoke_buf *)(&pra[len]);
92754f7c85bSVamsi Krishna Gattupalli }
92854f7c85bSVamsi Krishna Gattupalli
fastrpc_phy_page_start(struct fastrpc_invoke_buf * buf,int len)92954f7c85bSVamsi Krishna Gattupalli static struct fastrpc_phy_page *fastrpc_phy_page_start(struct fastrpc_invoke_buf *buf, int len)
93054f7c85bSVamsi Krishna Gattupalli {
93154f7c85bSVamsi Krishna Gattupalli return (struct fastrpc_phy_page *)(&buf[len]);
93254f7c85bSVamsi Krishna Gattupalli }
93354f7c85bSVamsi Krishna Gattupalli
fastrpc_get_args(u32 kernel,struct fastrpc_invoke_ctx * ctx)934c68cfb71SSrinivas Kandagatla static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
935c68cfb71SSrinivas Kandagatla {
936c68cfb71SSrinivas Kandagatla struct device *dev = ctx->fl->sctx->dev;
93735a82b87SVamsi Krishna Gattupalli union fastrpc_remote_arg *rpra;
938c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_buf *list;
939c68cfb71SSrinivas Kandagatla struct fastrpc_phy_page *pages;
94025e8dfb8SSrinivas Kandagatla int inbufs, i, oix, err = 0;
94125e8dfb8SSrinivas Kandagatla u64 len, rlen, pkt_size;
94202b45b47SSrinivas Kandagatla u64 pg_start, pg_end;
943c68cfb71SSrinivas Kandagatla uintptr_t args;
944c68cfb71SSrinivas Kandagatla int metalen;
945c68cfb71SSrinivas Kandagatla
946c68cfb71SSrinivas Kandagatla inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
947c68cfb71SSrinivas Kandagatla metalen = fastrpc_get_meta_size(ctx);
948c68cfb71SSrinivas Kandagatla pkt_size = fastrpc_get_payload_size(ctx, metalen);
949c68cfb71SSrinivas Kandagatla
950c68cfb71SSrinivas Kandagatla err = fastrpc_create_maps(ctx);
951c68cfb71SSrinivas Kandagatla if (err)
952c68cfb71SSrinivas Kandagatla return err;
953c68cfb71SSrinivas Kandagatla
954c68cfb71SSrinivas Kandagatla ctx->msg_sz = pkt_size;
955c68cfb71SSrinivas Kandagatla
956c68cfb71SSrinivas Kandagatla err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf);
957c68cfb71SSrinivas Kandagatla if (err)
958c68cfb71SSrinivas Kandagatla return err;
959c68cfb71SSrinivas Kandagatla
9601c29d801SEkansh Gupta memset(ctx->buf->virt, 0, pkt_size);
961c68cfb71SSrinivas Kandagatla rpra = ctx->buf->virt;
96254f7c85bSVamsi Krishna Gattupalli list = fastrpc_invoke_buf_start(rpra, ctx->nscalars);
96354f7c85bSVamsi Krishna Gattupalli pages = fastrpc_phy_page_start(list, ctx->nscalars);
964c68cfb71SSrinivas Kandagatla args = (uintptr_t)ctx->buf->virt + metalen;
965c68cfb71SSrinivas Kandagatla rlen = pkt_size - metalen;
966c68cfb71SSrinivas Kandagatla ctx->rpra = rpra;
967c68cfb71SSrinivas Kandagatla
96825e8dfb8SSrinivas Kandagatla for (oix = 0; oix < ctx->nbufs; ++oix) {
96925e8dfb8SSrinivas Kandagatla int mlen;
97025e8dfb8SSrinivas Kandagatla
97125e8dfb8SSrinivas Kandagatla i = ctx->olaps[oix].raix;
97225e8dfb8SSrinivas Kandagatla len = ctx->args[i].length;
973c68cfb71SSrinivas Kandagatla
97435a82b87SVamsi Krishna Gattupalli rpra[i].buf.pv = 0;
97535a82b87SVamsi Krishna Gattupalli rpra[i].buf.len = len;
976c68cfb71SSrinivas Kandagatla list[i].num = len ? 1 : 0;
977c68cfb71SSrinivas Kandagatla list[i].pgidx = i;
978c68cfb71SSrinivas Kandagatla
979c68cfb71SSrinivas Kandagatla if (!len)
980c68cfb71SSrinivas Kandagatla continue;
981c68cfb71SSrinivas Kandagatla
982c68cfb71SSrinivas Kandagatla if (ctx->maps[i]) {
98380f3afd7SSrinivas Kandagatla struct vm_area_struct *vma = NULL;
98480f3afd7SSrinivas Kandagatla
98535a82b87SVamsi Krishna Gattupalli rpra[i].buf.pv = (u64) ctx->args[i].ptr;
986c68cfb71SSrinivas Kandagatla pages[i].addr = ctx->maps[i]->phys;
98780f3afd7SSrinivas Kandagatla
988f9a470dbSSrinivas Kandagatla mmap_read_lock(current->mm);
98980f3afd7SSrinivas Kandagatla vma = find_vma(current->mm, ctx->args[i].ptr);
99080f3afd7SSrinivas Kandagatla if (vma)
99180f3afd7SSrinivas Kandagatla pages[i].addr += ctx->args[i].ptr -
99280f3afd7SSrinivas Kandagatla vma->vm_start;
993f9a470dbSSrinivas Kandagatla mmap_read_unlock(current->mm);
99480f3afd7SSrinivas Kandagatla
99502b45b47SSrinivas Kandagatla pg_start = (ctx->args[i].ptr & PAGE_MASK) >> PAGE_SHIFT;
99602b45b47SSrinivas Kandagatla pg_end = ((ctx->args[i].ptr + len - 1) & PAGE_MASK) >>
99702b45b47SSrinivas Kandagatla PAGE_SHIFT;
99802b45b47SSrinivas Kandagatla pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
99902b45b47SSrinivas Kandagatla
1000c68cfb71SSrinivas Kandagatla } else {
100125e8dfb8SSrinivas Kandagatla
100225e8dfb8SSrinivas Kandagatla if (ctx->olaps[oix].offset == 0) {
1003c68cfb71SSrinivas Kandagatla rlen -= ALIGN(args, FASTRPC_ALIGN) - args;
1004c68cfb71SSrinivas Kandagatla args = ALIGN(args, FASTRPC_ALIGN);
100525e8dfb8SSrinivas Kandagatla }
100625e8dfb8SSrinivas Kandagatla
100725e8dfb8SSrinivas Kandagatla mlen = ctx->olaps[oix].mend - ctx->olaps[oix].mstart;
100825e8dfb8SSrinivas Kandagatla
100925e8dfb8SSrinivas Kandagatla if (rlen < mlen)
1010c68cfb71SSrinivas Kandagatla goto bail;
1011c68cfb71SSrinivas Kandagatla
101235a82b87SVamsi Krishna Gattupalli rpra[i].buf.pv = args - ctx->olaps[oix].offset;
101325e8dfb8SSrinivas Kandagatla pages[i].addr = ctx->buf->phys -
101425e8dfb8SSrinivas Kandagatla ctx->olaps[oix].offset +
101525e8dfb8SSrinivas Kandagatla (pkt_size - rlen);
1016c68cfb71SSrinivas Kandagatla pages[i].addr = pages[i].addr & PAGE_MASK;
101725e8dfb8SSrinivas Kandagatla
101802b45b47SSrinivas Kandagatla pg_start = (args & PAGE_MASK) >> PAGE_SHIFT;
101902b45b47SSrinivas Kandagatla pg_end = ((args + len - 1) & PAGE_MASK) >> PAGE_SHIFT;
102002b45b47SSrinivas Kandagatla pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE;
102125e8dfb8SSrinivas Kandagatla args = args + mlen;
102225e8dfb8SSrinivas Kandagatla rlen -= mlen;
1023c68cfb71SSrinivas Kandagatla }
1024c68cfb71SSrinivas Kandagatla
1025c68cfb71SSrinivas Kandagatla if (i < inbufs && !ctx->maps[i]) {
102635a82b87SVamsi Krishna Gattupalli void *dst = (void *)(uintptr_t)rpra[i].buf.pv;
1027c68cfb71SSrinivas Kandagatla void *src = (void *)(uintptr_t)ctx->args[i].ptr;
1028c68cfb71SSrinivas Kandagatla
1029c68cfb71SSrinivas Kandagatla if (!kernel) {
1030c68cfb71SSrinivas Kandagatla if (copy_from_user(dst, (void __user *)src,
1031c68cfb71SSrinivas Kandagatla len)) {
1032c68cfb71SSrinivas Kandagatla err = -EFAULT;
1033c68cfb71SSrinivas Kandagatla goto bail;
1034c68cfb71SSrinivas Kandagatla }
1035c68cfb71SSrinivas Kandagatla } else {
1036c68cfb71SSrinivas Kandagatla memcpy(dst, src, len);
1037c68cfb71SSrinivas Kandagatla }
1038c68cfb71SSrinivas Kandagatla }
1039c68cfb71SSrinivas Kandagatla }
1040c68cfb71SSrinivas Kandagatla
1041c68cfb71SSrinivas Kandagatla for (i = ctx->nbufs; i < ctx->nscalars; ++i) {
1042c68cfb71SSrinivas Kandagatla list[i].num = ctx->args[i].length ? 1 : 0;
1043c68cfb71SSrinivas Kandagatla list[i].pgidx = i;
104435a82b87SVamsi Krishna Gattupalli if (ctx->maps[i]) {
1045c68cfb71SSrinivas Kandagatla pages[i].addr = ctx->maps[i]->phys;
1046c68cfb71SSrinivas Kandagatla pages[i].size = ctx->maps[i]->size;
1047c68cfb71SSrinivas Kandagatla }
104835a82b87SVamsi Krishna Gattupalli rpra[i].dma.fd = ctx->args[i].fd;
104935a82b87SVamsi Krishna Gattupalli rpra[i].dma.len = ctx->args[i].length;
105035a82b87SVamsi Krishna Gattupalli rpra[i].dma.offset = (u64) ctx->args[i].ptr;
105135a82b87SVamsi Krishna Gattupalli }
1052c68cfb71SSrinivas Kandagatla
1053c68cfb71SSrinivas Kandagatla bail:
1054c68cfb71SSrinivas Kandagatla if (err)
1055c68cfb71SSrinivas Kandagatla dev_err(dev, "Error: get invoke args failed:%d\n", err);
1056c68cfb71SSrinivas Kandagatla
1057c68cfb71SSrinivas Kandagatla return err;
1058c68cfb71SSrinivas Kandagatla }
1059c68cfb71SSrinivas Kandagatla
fastrpc_put_args(struct fastrpc_invoke_ctx * ctx,u32 kernel)1060c68cfb71SSrinivas Kandagatla static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
1061c68cfb71SSrinivas Kandagatla u32 kernel)
1062c68cfb71SSrinivas Kandagatla {
106335a82b87SVamsi Krishna Gattupalli union fastrpc_remote_arg *rpra = ctx->rpra;
10648f6c1d8cSVamsi Krishna Gattupalli struct fastrpc_user *fl = ctx->fl;
10658f6c1d8cSVamsi Krishna Gattupalli struct fastrpc_map *mmap = NULL;
10668f6c1d8cSVamsi Krishna Gattupalli struct fastrpc_invoke_buf *list;
10678f6c1d8cSVamsi Krishna Gattupalli struct fastrpc_phy_page *pages;
10688f6c1d8cSVamsi Krishna Gattupalli u64 *fdlist;
10698f6c1d8cSVamsi Krishna Gattupalli int i, inbufs, outbufs, handles;
1070c68cfb71SSrinivas Kandagatla
1071c68cfb71SSrinivas Kandagatla inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
10728f6c1d8cSVamsi Krishna Gattupalli outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc);
10738f6c1d8cSVamsi Krishna Gattupalli handles = REMOTE_SCALARS_INHANDLES(ctx->sc) + REMOTE_SCALARS_OUTHANDLES(ctx->sc);
10748f6c1d8cSVamsi Krishna Gattupalli list = fastrpc_invoke_buf_start(rpra, ctx->nscalars);
10758f6c1d8cSVamsi Krishna Gattupalli pages = fastrpc_phy_page_start(list, ctx->nscalars);
10768f6c1d8cSVamsi Krishna Gattupalli fdlist = (uint64_t *)(pages + inbufs + outbufs + handles);
1077c68cfb71SSrinivas Kandagatla
1078c68cfb71SSrinivas Kandagatla for (i = inbufs; i < ctx->nbufs; ++i) {
1079847afd7bSJeya R if (!ctx->maps[i]) {
108035a82b87SVamsi Krishna Gattupalli void *src = (void *)(uintptr_t)rpra[i].buf.pv;
1081c68cfb71SSrinivas Kandagatla void *dst = (void *)(uintptr_t)ctx->args[i].ptr;
108235a82b87SVamsi Krishna Gattupalli u64 len = rpra[i].buf.len;
1083c68cfb71SSrinivas Kandagatla
1084c68cfb71SSrinivas Kandagatla if (!kernel) {
1085c68cfb71SSrinivas Kandagatla if (copy_to_user((void __user *)dst, src, len))
1086c68cfb71SSrinivas Kandagatla return -EFAULT;
1087c68cfb71SSrinivas Kandagatla } else {
1088c68cfb71SSrinivas Kandagatla memcpy(dst, src, len);
1089c68cfb71SSrinivas Kandagatla }
1090c68cfb71SSrinivas Kandagatla }
1091847afd7bSJeya R }
1092c68cfb71SSrinivas Kandagatla
109320648430SEkansh Gupta /* Clean up fdlist which is updated by DSP */
10948f6c1d8cSVamsi Krishna Gattupalli for (i = 0; i < FASTRPC_MAX_FDLIST; i++) {
10958f6c1d8cSVamsi Krishna Gattupalli if (!fdlist[i])
10968f6c1d8cSVamsi Krishna Gattupalli break;
10979446fa16SAbel Vesa if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap, false))
10988f6c1d8cSVamsi Krishna Gattupalli fastrpc_map_put(mmap);
10998f6c1d8cSVamsi Krishna Gattupalli }
11008f6c1d8cSVamsi Krishna Gattupalli
1101c68cfb71SSrinivas Kandagatla return 0;
1102c68cfb71SSrinivas Kandagatla }
1103c68cfb71SSrinivas Kandagatla
fastrpc_invoke_send(struct fastrpc_session_ctx * sctx,struct fastrpc_invoke_ctx * ctx,u32 kernel,uint32_t handle)1104c68cfb71SSrinivas Kandagatla static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx,
1105c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx,
1106c68cfb71SSrinivas Kandagatla u32 kernel, uint32_t handle)
1107c68cfb71SSrinivas Kandagatla {
1108c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx;
1109c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl = ctx->fl;
1110c68cfb71SSrinivas Kandagatla struct fastrpc_msg *msg = &ctx->msg;
111174003385SSrinivas Kandagatla int ret;
1112c68cfb71SSrinivas Kandagatla
1113c68cfb71SSrinivas Kandagatla cctx = fl->cctx;
1114c68cfb71SSrinivas Kandagatla msg->pid = fl->tgid;
1115c68cfb71SSrinivas Kandagatla msg->tid = current->pid;
1116c68cfb71SSrinivas Kandagatla
1117c68cfb71SSrinivas Kandagatla if (kernel)
1118c68cfb71SSrinivas Kandagatla msg->pid = 0;
1119c68cfb71SSrinivas Kandagatla
1120c68cfb71SSrinivas Kandagatla msg->ctx = ctx->ctxid | fl->pd;
1121c68cfb71SSrinivas Kandagatla msg->handle = handle;
1122c68cfb71SSrinivas Kandagatla msg->sc = ctx->sc;
1123c68cfb71SSrinivas Kandagatla msg->addr = ctx->buf ? ctx->buf->phys : 0;
1124c68cfb71SSrinivas Kandagatla msg->size = roundup(ctx->msg_sz, PAGE_SIZE);
1125c68cfb71SSrinivas Kandagatla fastrpc_context_get(ctx);
1126c68cfb71SSrinivas Kandagatla
112774003385SSrinivas Kandagatla ret = rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg));
112874003385SSrinivas Kandagatla
112974003385SSrinivas Kandagatla if (ret)
113074003385SSrinivas Kandagatla fastrpc_context_put(ctx);
113174003385SSrinivas Kandagatla
113274003385SSrinivas Kandagatla return ret;
113374003385SSrinivas Kandagatla
1134c68cfb71SSrinivas Kandagatla }
1135c68cfb71SSrinivas Kandagatla
fastrpc_internal_invoke(struct fastrpc_user * fl,u32 kernel,u32 handle,u32 sc,struct fastrpc_invoke_args * args)1136c68cfb71SSrinivas Kandagatla static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
1137c68cfb71SSrinivas Kandagatla u32 handle, u32 sc,
1138c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_args *args)
1139c68cfb71SSrinivas Kandagatla {
1140c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx = NULL;
114176e8e4acSAbel Vesa struct fastrpc_buf *buf, *b;
114276e8e4acSAbel Vesa
1143c68cfb71SSrinivas Kandagatla int err = 0;
1144c68cfb71SSrinivas Kandagatla
1145c68cfb71SSrinivas Kandagatla if (!fl->sctx)
1146c68cfb71SSrinivas Kandagatla return -EINVAL;
1147c68cfb71SSrinivas Kandagatla
11482e369878SBjorn Andersson if (!fl->cctx->rpdev)
11492e369878SBjorn Andersson return -EPIPE;
11502e369878SBjorn Andersson
115120c40794SDmitry Baryshkov if (handle == FASTRPC_INIT_HANDLE && !kernel) {
115220c40794SDmitry Baryshkov dev_warn_ratelimited(fl->sctx->dev, "user app trying to send a kernel RPC message (%d)\n", handle);
115320c40794SDmitry Baryshkov return -EPERM;
115420c40794SDmitry Baryshkov }
115520c40794SDmitry Baryshkov
1156c68cfb71SSrinivas Kandagatla ctx = fastrpc_context_alloc(fl, kernel, sc, args);
1157c68cfb71SSrinivas Kandagatla if (IS_ERR(ctx))
1158c68cfb71SSrinivas Kandagatla return PTR_ERR(ctx);
1159c68cfb71SSrinivas Kandagatla
1160c68cfb71SSrinivas Kandagatla err = fastrpc_get_args(kernel, ctx);
1161c68cfb71SSrinivas Kandagatla if (err)
1162c68cfb71SSrinivas Kandagatla goto bail;
1163415a0729SSrinivas Kandagatla
1164415a0729SSrinivas Kandagatla /* make sure that all CPU memory writes are seen by DSP */
1165415a0729SSrinivas Kandagatla dma_wmb();
1166c68cfb71SSrinivas Kandagatla /* Send invoke buffer to remote dsp */
1167c68cfb71SSrinivas Kandagatla err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle);
1168c68cfb71SSrinivas Kandagatla if (err)
1169c68cfb71SSrinivas Kandagatla goto bail;
1170c68cfb71SSrinivas Kandagatla
117155bcda35SJorge Ramirez-Ortiz if (kernel) {
117255bcda35SJorge Ramirez-Ortiz if (!wait_for_completion_timeout(&ctx->work, 10 * HZ))
117355bcda35SJorge Ramirez-Ortiz err = -ETIMEDOUT;
117455bcda35SJorge Ramirez-Ortiz } else {
1175c68cfb71SSrinivas Kandagatla err = wait_for_completion_interruptible(&ctx->work);
117655bcda35SJorge Ramirez-Ortiz }
117755bcda35SJorge Ramirez-Ortiz
1178c68cfb71SSrinivas Kandagatla if (err)
1179c68cfb71SSrinivas Kandagatla goto bail;
1180c68cfb71SSrinivas Kandagatla
1181415a0729SSrinivas Kandagatla /* make sure that all memory writes by DSP are seen by CPU */
1182415a0729SSrinivas Kandagatla dma_rmb();
1183c68cfb71SSrinivas Kandagatla /* populate all the output buffers with results */
1184c68cfb71SSrinivas Kandagatla err = fastrpc_put_args(ctx, kernel);
1185c68cfb71SSrinivas Kandagatla if (err)
1186c68cfb71SSrinivas Kandagatla goto bail;
1187c68cfb71SSrinivas Kandagatla
11881c809359SEkansh Gupta /* Check the response from remote dsp */
11891c809359SEkansh Gupta err = ctx->retval;
11901c809359SEkansh Gupta if (err)
11911c809359SEkansh Gupta goto bail;
11921c809359SEkansh Gupta
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
is_session_rejected(struct fastrpc_user * fl,bool unsigned_pd_request)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
fastrpc_init_create_static_process(struct fastrpc_user * fl,char __user * argp)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;
1241aeab0a85SEkansh Gupta bool scm_done = false;
124208715610SAbel Vesa struct {
124308715610SAbel Vesa int pgid;
124408715610SAbel Vesa u32 namelen;
124508715610SAbel Vesa u32 pageslen;
124608715610SAbel Vesa } inbuf;
124708715610SAbel Vesa u32 sc;
124808715610SAbel Vesa
124908715610SAbel Vesa args = kcalloc(FASTRPC_CREATE_STATIC_PROCESS_NARGS, sizeof(*args), GFP_KERNEL);
125008715610SAbel Vesa if (!args)
125108715610SAbel Vesa return -ENOMEM;
125208715610SAbel Vesa
125308715610SAbel Vesa if (copy_from_user(&init, argp, sizeof(init))) {
125408715610SAbel Vesa err = -EFAULT;
125508715610SAbel Vesa goto err;
125608715610SAbel Vesa }
125708715610SAbel Vesa
125808715610SAbel Vesa if (init.namelen > INIT_FILE_NAMELEN_MAX) {
125908715610SAbel Vesa err = -EINVAL;
126008715610SAbel Vesa goto err;
126108715610SAbel Vesa }
126208715610SAbel Vesa
126308715610SAbel Vesa name = kzalloc(init.namelen, GFP_KERNEL);
126408715610SAbel Vesa if (!name) {
126508715610SAbel Vesa err = -ENOMEM;
126608715610SAbel Vesa goto err;
126708715610SAbel Vesa }
126808715610SAbel Vesa
126908715610SAbel Vesa if (copy_from_user(name, (void __user *)(uintptr_t)init.name, init.namelen)) {
127008715610SAbel Vesa err = -EFAULT;
127108715610SAbel Vesa goto err_name;
127208715610SAbel Vesa }
127308715610SAbel Vesa
127408715610SAbel Vesa if (!fl->cctx->remote_heap) {
127508715610SAbel Vesa err = fastrpc_remote_heap_alloc(fl, fl->sctx->dev, init.memlen,
127608715610SAbel Vesa &fl->cctx->remote_heap);
127708715610SAbel Vesa if (err)
127808715610SAbel Vesa goto err_name;
127908715610SAbel Vesa
128008715610SAbel Vesa /* Map if we have any heap VMIDs associated with this ADSP Static Process. */
128108715610SAbel Vesa if (fl->cctx->vmcount) {
1282a62a30ccSEkansh Gupta u64 src_perms = BIT(QCOM_SCM_VMID_HLOS);
1283a62a30ccSEkansh Gupta
128408715610SAbel Vesa err = qcom_scm_assign_mem(fl->cctx->remote_heap->phys,
1285aaca766cSElliot Berman (u64)fl->cctx->remote_heap->size,
1286a62a30ccSEkansh Gupta &src_perms,
128708715610SAbel Vesa fl->cctx->vmperms, fl->cctx->vmcount);
128808715610SAbel Vesa if (err) {
128908715610SAbel Vesa dev_err(fl->sctx->dev, "Failed to assign memory with phys 0x%llx size 0x%llx err %d",
129008715610SAbel Vesa fl->cctx->remote_heap->phys, fl->cctx->remote_heap->size, err);
129108715610SAbel Vesa goto err_map;
129208715610SAbel Vesa }
1293aeab0a85SEkansh Gupta scm_done = true;
129408715610SAbel Vesa }
129508715610SAbel Vesa }
129608715610SAbel Vesa
129708715610SAbel Vesa inbuf.pgid = fl->tgid;
129808715610SAbel Vesa inbuf.namelen = init.namelen;
129908715610SAbel Vesa inbuf.pageslen = 0;
130008715610SAbel Vesa fl->pd = USER_PD;
130108715610SAbel Vesa
130208715610SAbel Vesa args[0].ptr = (u64)(uintptr_t)&inbuf;
130308715610SAbel Vesa args[0].length = sizeof(inbuf);
130408715610SAbel Vesa args[0].fd = -1;
130508715610SAbel Vesa
130608715610SAbel Vesa args[1].ptr = (u64)(uintptr_t)name;
130708715610SAbel Vesa args[1].length = inbuf.namelen;
130808715610SAbel Vesa args[1].fd = -1;
130908715610SAbel Vesa
131008715610SAbel Vesa pages[0].addr = fl->cctx->remote_heap->phys;
131108715610SAbel Vesa pages[0].size = fl->cctx->remote_heap->size;
131208715610SAbel Vesa
131308715610SAbel Vesa args[2].ptr = (u64)(uintptr_t) pages;
131408715610SAbel Vesa args[2].length = sizeof(*pages);
131508715610SAbel Vesa args[2].fd = -1;
131608715610SAbel Vesa
131708715610SAbel Vesa sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_STATIC, 3, 0);
131808715610SAbel Vesa
131908715610SAbel Vesa err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
132008715610SAbel Vesa sc, args);
132108715610SAbel Vesa if (err)
132208715610SAbel Vesa goto err_invoke;
132308715610SAbel Vesa
132408715610SAbel Vesa kfree(args);
13258b8b82dcSEkansh Gupta kfree(name);
132608715610SAbel Vesa
132708715610SAbel Vesa return 0;
132808715610SAbel Vesa err_invoke:
1329aeab0a85SEkansh Gupta if (fl->cctx->vmcount && scm_done) {
1330fe6518d5SEkansh Gupta u64 src_perms = 0;
1331fe6518d5SEkansh Gupta struct qcom_scm_vmperm dst_perms;
1332fe6518d5SEkansh Gupta u32 i;
133308715610SAbel Vesa
1334fe6518d5SEkansh Gupta for (i = 0; i < fl->cctx->vmcount; i++)
1335fe6518d5SEkansh Gupta src_perms |= BIT(fl->cctx->vmperms[i].vmid);
1336fe6518d5SEkansh Gupta
1337fe6518d5SEkansh Gupta dst_perms.vmid = QCOM_SCM_VMID_HLOS;
1338fe6518d5SEkansh Gupta dst_perms.perm = QCOM_SCM_PERM_RWX;
133908715610SAbel Vesa err = qcom_scm_assign_mem(fl->cctx->remote_heap->phys,
134008715610SAbel Vesa (u64)fl->cctx->remote_heap->size,
1341fe6518d5SEkansh Gupta &src_perms, &dst_perms, 1);
134208715610SAbel Vesa if (err)
134308715610SAbel Vesa dev_err(fl->sctx->dev, "Failed to assign memory phys 0x%llx size 0x%llx err %d",
134408715610SAbel Vesa fl->cctx->remote_heap->phys, fl->cctx->remote_heap->size, err);
134508715610SAbel Vesa }
134608715610SAbel Vesa err_map:
134708715610SAbel Vesa fastrpc_buf_free(fl->cctx->remote_heap);
134808715610SAbel Vesa err_name:
134908715610SAbel Vesa kfree(name);
135008715610SAbel Vesa err:
135108715610SAbel Vesa kfree(args);
135208715610SAbel Vesa
135308715610SAbel Vesa return err;
135408715610SAbel Vesa }
135508715610SAbel Vesa
fastrpc_init_create_process(struct fastrpc_user * fl,char __user * argp)1356d73f71c7SSrinivas Kandagatla static int fastrpc_init_create_process(struct fastrpc_user *fl,
1357d73f71c7SSrinivas Kandagatla char __user *argp)
1358d73f71c7SSrinivas Kandagatla {
1359d73f71c7SSrinivas Kandagatla struct fastrpc_init_create init;
1360d73f71c7SSrinivas Kandagatla struct fastrpc_invoke_args *args;
1361d73f71c7SSrinivas Kandagatla struct fastrpc_phy_page pages[1];
1362d73f71c7SSrinivas Kandagatla struct fastrpc_map *map = NULL;
1363d73f71c7SSrinivas Kandagatla struct fastrpc_buf *imem = NULL;
1364d73f71c7SSrinivas Kandagatla int memlen;
1365d73f71c7SSrinivas Kandagatla int err;
1366d73f71c7SSrinivas Kandagatla struct {
1367d73f71c7SSrinivas Kandagatla int pgid;
1368d73f71c7SSrinivas Kandagatla u32 namelen;
1369d73f71c7SSrinivas Kandagatla u32 filelen;
1370d73f71c7SSrinivas Kandagatla u32 pageslen;
1371d73f71c7SSrinivas Kandagatla u32 attrs;
1372d73f71c7SSrinivas Kandagatla u32 siglen;
1373d73f71c7SSrinivas Kandagatla } inbuf;
1374d73f71c7SSrinivas Kandagatla u32 sc;
13757f1f4812SJeya R bool unsigned_module = false;
1376d73f71c7SSrinivas Kandagatla
1377d73f71c7SSrinivas Kandagatla args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL);
1378d73f71c7SSrinivas Kandagatla if (!args)
1379d73f71c7SSrinivas Kandagatla return -ENOMEM;
1380d73f71c7SSrinivas Kandagatla
1381d73f71c7SSrinivas Kandagatla if (copy_from_user(&init, argp, sizeof(init))) {
1382d73f71c7SSrinivas Kandagatla err = -EFAULT;
1383b49f6d83SThierry Escande goto err;
1384d73f71c7SSrinivas Kandagatla }
1385d73f71c7SSrinivas Kandagatla
13867f1f4812SJeya R if (init.attrs & FASTRPC_MODE_UNSIGNED_MODULE)
13877f1f4812SJeya R unsigned_module = true;
13887f1f4812SJeya R
13897f1f4812SJeya R if (is_session_rejected(fl, unsigned_module)) {
13907f1f4812SJeya R err = -ECONNREFUSED;
13917f1f4812SJeya R goto err;
13927f1f4812SJeya R }
13937f1f4812SJeya R
1394d73f71c7SSrinivas Kandagatla if (init.filelen > INIT_FILELEN_MAX) {
1395d73f71c7SSrinivas Kandagatla err = -EINVAL;
1396b49f6d83SThierry Escande goto err;
1397d73f71c7SSrinivas Kandagatla }
1398d73f71c7SSrinivas Kandagatla
1399d73f71c7SSrinivas Kandagatla inbuf.pgid = fl->tgid;
1400d73f71c7SSrinivas Kandagatla inbuf.namelen = strlen(current->comm) + 1;
1401d73f71c7SSrinivas Kandagatla inbuf.filelen = init.filelen;
1402d73f71c7SSrinivas Kandagatla inbuf.pageslen = 1;
1403d73f71c7SSrinivas Kandagatla inbuf.attrs = init.attrs;
1404d73f71c7SSrinivas Kandagatla inbuf.siglen = init.siglen;
140584195d20SJonathan Marek fl->pd = USER_PD;
1406d73f71c7SSrinivas Kandagatla
1407d73f71c7SSrinivas Kandagatla if (init.filelen && init.filefd) {
1408e90d9119SVamsi Krishna Gattupalli err = fastrpc_map_create(fl, init.filefd, init.filelen, 0, &map);
1409d73f71c7SSrinivas Kandagatla if (err)
1410b49f6d83SThierry Escande goto err;
1411d73f71c7SSrinivas Kandagatla }
1412d73f71c7SSrinivas Kandagatla
1413d73f71c7SSrinivas Kandagatla memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4),
1414d73f71c7SSrinivas Kandagatla 1024 * 1024);
1415d73f71c7SSrinivas Kandagatla err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen,
1416d73f71c7SSrinivas Kandagatla &imem);
1417b49f6d83SThierry Escande if (err)
1418b49f6d83SThierry Escande goto err_alloc;
1419d73f71c7SSrinivas Kandagatla
1420d73f71c7SSrinivas Kandagatla fl->init_mem = imem;
1421d73f71c7SSrinivas Kandagatla args[0].ptr = (u64)(uintptr_t)&inbuf;
1422d73f71c7SSrinivas Kandagatla args[0].length = sizeof(inbuf);
1423d73f71c7SSrinivas Kandagatla args[0].fd = -1;
1424d73f71c7SSrinivas Kandagatla
1425d73f71c7SSrinivas Kandagatla args[1].ptr = (u64)(uintptr_t)current->comm;
1426d73f71c7SSrinivas Kandagatla args[1].length = inbuf.namelen;
1427d73f71c7SSrinivas Kandagatla args[1].fd = -1;
1428d73f71c7SSrinivas Kandagatla
1429d73f71c7SSrinivas Kandagatla args[2].ptr = (u64) init.file;
1430d73f71c7SSrinivas Kandagatla args[2].length = inbuf.filelen;
1431d73f71c7SSrinivas Kandagatla args[2].fd = init.filefd;
1432d73f71c7SSrinivas Kandagatla
1433d73f71c7SSrinivas Kandagatla pages[0].addr = imem->phys;
1434d73f71c7SSrinivas Kandagatla pages[0].size = imem->size;
1435d73f71c7SSrinivas Kandagatla
1436d73f71c7SSrinivas Kandagatla args[3].ptr = (u64)(uintptr_t) pages;
1437d73f71c7SSrinivas Kandagatla args[3].length = 1 * sizeof(*pages);
1438d73f71c7SSrinivas Kandagatla args[3].fd = -1;
1439d73f71c7SSrinivas Kandagatla
1440d73f71c7SSrinivas Kandagatla args[4].ptr = (u64)(uintptr_t)&inbuf.attrs;
1441d73f71c7SSrinivas Kandagatla args[4].length = sizeof(inbuf.attrs);
1442d73f71c7SSrinivas Kandagatla args[4].fd = -1;
1443d73f71c7SSrinivas Kandagatla
1444d73f71c7SSrinivas Kandagatla args[5].ptr = (u64)(uintptr_t) &inbuf.siglen;
1445d73f71c7SSrinivas Kandagatla args[5].length = sizeof(inbuf.siglen);
1446d73f71c7SSrinivas Kandagatla args[5].fd = -1;
1447d73f71c7SSrinivas Kandagatla
1448d73f71c7SSrinivas Kandagatla sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0);
1449d73f71c7SSrinivas Kandagatla if (init.attrs)
14500b4e32dfSEkansh Gupta sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 4, 0);
1451d73f71c7SSrinivas Kandagatla
1452d73f71c7SSrinivas Kandagatla err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1453d73f71c7SSrinivas Kandagatla sc, args);
1454b49f6d83SThierry Escande if (err)
1455b49f6d83SThierry Escande goto err_invoke;
1456d73f71c7SSrinivas Kandagatla
1457b49f6d83SThierry Escande kfree(args);
1458b49f6d83SThierry Escande
1459b49f6d83SThierry Escande return 0;
1460b49f6d83SThierry Escande
1461b49f6d83SThierry Escande err_invoke:
1462b49f6d83SThierry Escande fl->init_mem = NULL;
1463d73f71c7SSrinivas Kandagatla fastrpc_buf_free(imem);
1464b49f6d83SThierry Escande err_alloc:
1465b49f6d83SThierry Escande fastrpc_map_put(map);
1466b49f6d83SThierry Escande err:
1467d73f71c7SSrinivas Kandagatla kfree(args);
1468d73f71c7SSrinivas Kandagatla
1469d73f71c7SSrinivas Kandagatla return err;
1470d73f71c7SSrinivas Kandagatla }
1471d73f71c7SSrinivas Kandagatla
fastrpc_session_alloc(struct fastrpc_channel_ctx * cctx)1472f6f9279fSSrinivas Kandagatla static struct fastrpc_session_ctx *fastrpc_session_alloc(
1473f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx)
1474f6f9279fSSrinivas Kandagatla {
1475f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *session = NULL;
1476977e6c8dSSrinivas Kandagatla unsigned long flags;
1477f6f9279fSSrinivas Kandagatla int i;
1478f6f9279fSSrinivas Kandagatla
1479977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags);
1480f6f9279fSSrinivas Kandagatla for (i = 0; i < cctx->sesscount; i++) {
1481f6f9279fSSrinivas Kandagatla if (!cctx->session[i].used && cctx->session[i].valid) {
1482f6f9279fSSrinivas Kandagatla cctx->session[i].used = true;
1483f6f9279fSSrinivas Kandagatla session = &cctx->session[i];
1484f6f9279fSSrinivas Kandagatla break;
1485f6f9279fSSrinivas Kandagatla }
1486f6f9279fSSrinivas Kandagatla }
1487977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags);
1488f6f9279fSSrinivas Kandagatla
1489f6f9279fSSrinivas Kandagatla return session;
1490f6f9279fSSrinivas Kandagatla }
1491f6f9279fSSrinivas Kandagatla
fastrpc_session_free(struct fastrpc_channel_ctx * cctx,struct fastrpc_session_ctx * session)1492f6f9279fSSrinivas Kandagatla static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx,
1493f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *session)
1494f6f9279fSSrinivas Kandagatla {
1495977e6c8dSSrinivas Kandagatla unsigned long flags;
1496977e6c8dSSrinivas Kandagatla
1497977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags);
1498f6f9279fSSrinivas Kandagatla session->used = false;
1499977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags);
1500f6f9279fSSrinivas Kandagatla }
1501f6f9279fSSrinivas Kandagatla
fastrpc_release_current_dsp_process(struct fastrpc_user * fl)1502d73f71c7SSrinivas Kandagatla static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl)
1503d73f71c7SSrinivas Kandagatla {
1504d73f71c7SSrinivas Kandagatla struct fastrpc_invoke_args args[1];
1505d73f71c7SSrinivas Kandagatla int tgid = 0;
1506d73f71c7SSrinivas Kandagatla u32 sc;
1507d73f71c7SSrinivas Kandagatla
1508d73f71c7SSrinivas Kandagatla tgid = fl->tgid;
1509d73f71c7SSrinivas Kandagatla args[0].ptr = (u64)(uintptr_t) &tgid;
1510d73f71c7SSrinivas Kandagatla args[0].length = sizeof(tgid);
1511d73f71c7SSrinivas Kandagatla args[0].fd = -1;
1512d73f71c7SSrinivas Kandagatla sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0);
1513d73f71c7SSrinivas Kandagatla
1514d73f71c7SSrinivas Kandagatla return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1515d73f71c7SSrinivas Kandagatla sc, &args[0]);
1516d73f71c7SSrinivas Kandagatla }
1517d73f71c7SSrinivas Kandagatla
fastrpc_device_release(struct inode * inode,struct file * file)1518f6f9279fSSrinivas Kandagatla static int fastrpc_device_release(struct inode *inode, struct file *file)
1519f6f9279fSSrinivas Kandagatla {
1520f6f9279fSSrinivas Kandagatla struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
1521f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = fl->cctx;
1522c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx, *n;
1523c68cfb71SSrinivas Kandagatla struct fastrpc_map *map, *m;
15242419e55eSJorge Ramirez-Ortiz struct fastrpc_buf *buf, *b;
1525977e6c8dSSrinivas Kandagatla unsigned long flags;
1526f6f9279fSSrinivas Kandagatla
1527d73f71c7SSrinivas Kandagatla fastrpc_release_current_dsp_process(fl);
1528d73f71c7SSrinivas Kandagatla
1529977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags);
1530f6f9279fSSrinivas Kandagatla list_del(&fl->user);
1531977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags);
1532f6f9279fSSrinivas Kandagatla
1533c68cfb71SSrinivas Kandagatla if (fl->init_mem)
1534c68cfb71SSrinivas Kandagatla fastrpc_buf_free(fl->init_mem);
1535c68cfb71SSrinivas Kandagatla
1536c68cfb71SSrinivas Kandagatla list_for_each_entry_safe(ctx, n, &fl->pending, node) {
1537c68cfb71SSrinivas Kandagatla list_del(&ctx->node);
1538c68cfb71SSrinivas Kandagatla fastrpc_context_put(ctx);
1539c68cfb71SSrinivas Kandagatla }
1540c68cfb71SSrinivas Kandagatla
15415bb96c8fSAbel Vesa list_for_each_entry_safe(map, m, &fl->maps, node)
1542c68cfb71SSrinivas Kandagatla fastrpc_map_put(map);
1543c68cfb71SSrinivas Kandagatla
15442419e55eSJorge Ramirez-Ortiz list_for_each_entry_safe(buf, b, &fl->mmaps, node) {
15452419e55eSJorge Ramirez-Ortiz list_del(&buf->node);
15462419e55eSJorge Ramirez-Ortiz fastrpc_buf_free(buf);
15472419e55eSJorge Ramirez-Ortiz }
15482419e55eSJorge Ramirez-Ortiz
1549f6f9279fSSrinivas Kandagatla fastrpc_session_free(cctx, fl->sctx);
1550278d56f9SBjorn Andersson fastrpc_channel_ctx_put(cctx);
1551f6f9279fSSrinivas Kandagatla
1552f6f9279fSSrinivas Kandagatla mutex_destroy(&fl->mutex);
1553f6f9279fSSrinivas Kandagatla kfree(fl);
1554f6f9279fSSrinivas Kandagatla file->private_data = NULL;
1555f6f9279fSSrinivas Kandagatla
1556f6f9279fSSrinivas Kandagatla return 0;
1557f6f9279fSSrinivas Kandagatla }
1558f6f9279fSSrinivas Kandagatla
fastrpc_device_open(struct inode * inode,struct file * filp)1559f6f9279fSSrinivas Kandagatla static int fastrpc_device_open(struct inode *inode, struct file *filp)
1560f6f9279fSSrinivas Kandagatla {
1561965602eaSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx;
1562965602eaSSrinivas Kandagatla struct fastrpc_device *fdevice;
1563f6f9279fSSrinivas Kandagatla struct fastrpc_user *fl = NULL;
1564977e6c8dSSrinivas Kandagatla unsigned long flags;
1565f6f9279fSSrinivas Kandagatla
1566965602eaSSrinivas Kandagatla fdevice = miscdev_to_fdevice(filp->private_data);
1567965602eaSSrinivas Kandagatla cctx = fdevice->cctx;
1568965602eaSSrinivas Kandagatla
1569f6f9279fSSrinivas Kandagatla fl = kzalloc(sizeof(*fl), GFP_KERNEL);
1570f6f9279fSSrinivas Kandagatla if (!fl)
1571f6f9279fSSrinivas Kandagatla return -ENOMEM;
1572f6f9279fSSrinivas Kandagatla
1573278d56f9SBjorn Andersson /* Released in fastrpc_device_release() */
1574278d56f9SBjorn Andersson fastrpc_channel_ctx_get(cctx);
1575278d56f9SBjorn Andersson
1576f6f9279fSSrinivas Kandagatla filp->private_data = fl;
1577f6f9279fSSrinivas Kandagatla spin_lock_init(&fl->lock);
1578f6f9279fSSrinivas Kandagatla mutex_init(&fl->mutex);
1579f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&fl->pending);
1580f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&fl->maps);
15812419e55eSJorge Ramirez-Ortiz INIT_LIST_HEAD(&fl->mmaps);
1582f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&fl->user);
1583f6f9279fSSrinivas Kandagatla fl->tgid = current->tgid;
1584f6f9279fSSrinivas Kandagatla fl->cctx = cctx;
15857f1f4812SJeya R fl->is_secure_dev = fdevice->secure;
15867c11df42SThierry Escande
15877c11df42SThierry Escande fl->sctx = fastrpc_session_alloc(cctx);
15887c11df42SThierry Escande if (!fl->sctx) {
15897c11df42SThierry Escande dev_err(&cctx->rpdev->dev, "No session available\n");
15907c11df42SThierry Escande mutex_destroy(&fl->mutex);
15917c11df42SThierry Escande kfree(fl);
15927c11df42SThierry Escande
15937c11df42SThierry Escande return -EBUSY;
15947c11df42SThierry Escande }
15957c11df42SThierry Escande
1596977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags);
1597f6f9279fSSrinivas Kandagatla list_add_tail(&fl->user, &cctx->users);
1598977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags);
1599f6f9279fSSrinivas Kandagatla
1600f6f9279fSSrinivas Kandagatla return 0;
1601f6f9279fSSrinivas Kandagatla }
1602f6f9279fSSrinivas Kandagatla
fastrpc_dmabuf_alloc(struct fastrpc_user * fl,char __user * argp)16036cffd795SSrinivas Kandagatla static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp)
16046cffd795SSrinivas Kandagatla {
16056cffd795SSrinivas Kandagatla struct fastrpc_alloc_dma_buf bp;
16066cffd795SSrinivas Kandagatla DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
16076cffd795SSrinivas Kandagatla struct fastrpc_buf *buf = NULL;
16086cffd795SSrinivas Kandagatla int err;
16096cffd795SSrinivas Kandagatla
16106cffd795SSrinivas Kandagatla if (copy_from_user(&bp, argp, sizeof(bp)))
16116cffd795SSrinivas Kandagatla return -EFAULT;
16126cffd795SSrinivas Kandagatla
16136cffd795SSrinivas Kandagatla err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf);
16146cffd795SSrinivas Kandagatla if (err)
16156cffd795SSrinivas Kandagatla return err;
16166cffd795SSrinivas Kandagatla exp_info.ops = &fastrpc_dma_buf_ops;
16176cffd795SSrinivas Kandagatla exp_info.size = bp.size;
16186cffd795SSrinivas Kandagatla exp_info.flags = O_RDWR;
16196cffd795SSrinivas Kandagatla exp_info.priv = buf;
16206cffd795SSrinivas Kandagatla buf->dmabuf = dma_buf_export(&exp_info);
16216cffd795SSrinivas Kandagatla if (IS_ERR(buf->dmabuf)) {
16226cffd795SSrinivas Kandagatla err = PTR_ERR(buf->dmabuf);
16236cffd795SSrinivas Kandagatla fastrpc_buf_free(buf);
16246cffd795SSrinivas Kandagatla return err;
16256cffd795SSrinivas Kandagatla }
16266cffd795SSrinivas Kandagatla
16276cffd795SSrinivas Kandagatla bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE);
16286cffd795SSrinivas Kandagatla if (bp.fd < 0) {
16296cffd795SSrinivas Kandagatla dma_buf_put(buf->dmabuf);
16306cffd795SSrinivas Kandagatla return -EINVAL;
16316cffd795SSrinivas Kandagatla }
16326cffd795SSrinivas Kandagatla
16336cffd795SSrinivas Kandagatla if (copy_to_user(argp, &bp, sizeof(bp))) {
163446963e2eSMathias Krause /*
163546963e2eSMathias Krause * The usercopy failed, but we can't do much about it, as
163646963e2eSMathias Krause * dma_buf_fd() already called fd_install() and made the
163746963e2eSMathias Krause * file descriptor accessible for the current process. It
163846963e2eSMathias Krause * might already be closed and dmabuf no longer valid when
163946963e2eSMathias Krause * we reach this point. Therefore "leak" the fd and rely on
164046963e2eSMathias Krause * the process exit path to do any required cleanup.
164146963e2eSMathias Krause */
16426cffd795SSrinivas Kandagatla return -EFAULT;
16436cffd795SSrinivas Kandagatla }
16446cffd795SSrinivas Kandagatla
16456cffd795SSrinivas Kandagatla return 0;
16466cffd795SSrinivas Kandagatla }
16476cffd795SSrinivas Kandagatla
fastrpc_init_attach(struct fastrpc_user * fl,int pd)16486010d9beSJonathan Marek static int fastrpc_init_attach(struct fastrpc_user *fl, int pd)
1649d73f71c7SSrinivas Kandagatla {
1650d73f71c7SSrinivas Kandagatla struct fastrpc_invoke_args args[1];
1651d73f71c7SSrinivas Kandagatla int tgid = fl->tgid;
1652d73f71c7SSrinivas Kandagatla u32 sc;
1653d73f71c7SSrinivas Kandagatla
1654d73f71c7SSrinivas Kandagatla args[0].ptr = (u64)(uintptr_t) &tgid;
1655d73f71c7SSrinivas Kandagatla args[0].length = sizeof(tgid);
1656d73f71c7SSrinivas Kandagatla args[0].fd = -1;
1657d73f71c7SSrinivas Kandagatla sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0);
16586010d9beSJonathan Marek fl->pd = pd;
1659d73f71c7SSrinivas Kandagatla
1660d73f71c7SSrinivas Kandagatla return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE,
1661d73f71c7SSrinivas Kandagatla sc, &args[0]);
1662d73f71c7SSrinivas Kandagatla }
1663d73f71c7SSrinivas Kandagatla
fastrpc_invoke(struct fastrpc_user * fl,char __user * argp)1664c68cfb71SSrinivas Kandagatla static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp)
1665c68cfb71SSrinivas Kandagatla {
1666c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_args *args = NULL;
1667c68cfb71SSrinivas Kandagatla struct fastrpc_invoke inv;
1668c68cfb71SSrinivas Kandagatla u32 nscalars;
1669c68cfb71SSrinivas Kandagatla int err;
1670c68cfb71SSrinivas Kandagatla
1671c68cfb71SSrinivas Kandagatla if (copy_from_user(&inv, argp, sizeof(inv)))
1672c68cfb71SSrinivas Kandagatla return -EFAULT;
1673c68cfb71SSrinivas Kandagatla
1674c68cfb71SSrinivas Kandagatla /* nscalars is truncated here to max supported value */
1675c68cfb71SSrinivas Kandagatla nscalars = REMOTE_SCALARS_LENGTH(inv.sc);
1676c68cfb71SSrinivas Kandagatla if (nscalars) {
1677c68cfb71SSrinivas Kandagatla args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL);
1678c68cfb71SSrinivas Kandagatla if (!args)
1679c68cfb71SSrinivas Kandagatla return -ENOMEM;
1680c68cfb71SSrinivas Kandagatla
1681c68cfb71SSrinivas Kandagatla if (copy_from_user(args, (void __user *)(uintptr_t)inv.args,
1682c68cfb71SSrinivas Kandagatla nscalars * sizeof(*args))) {
1683c68cfb71SSrinivas Kandagatla kfree(args);
1684c68cfb71SSrinivas Kandagatla return -EFAULT;
1685c68cfb71SSrinivas Kandagatla }
1686c68cfb71SSrinivas Kandagatla }
1687c68cfb71SSrinivas Kandagatla
1688c68cfb71SSrinivas Kandagatla err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args);
1689c68cfb71SSrinivas Kandagatla kfree(args);
1690c68cfb71SSrinivas Kandagatla
1691c68cfb71SSrinivas Kandagatla return err;
1692c68cfb71SSrinivas Kandagatla }
1693c68cfb71SSrinivas Kandagatla
fastrpc_get_info_from_dsp(struct fastrpc_user * fl,uint32_t * dsp_attr_buf,uint32_t dsp_attr_buf_len)16946c16fd8bSJeya R static int fastrpc_get_info_from_dsp(struct fastrpc_user *fl, uint32_t *dsp_attr_buf,
16956c16fd8bSJeya R uint32_t dsp_attr_buf_len)
16966c16fd8bSJeya R {
16976c16fd8bSJeya R struct fastrpc_invoke_args args[2] = { 0 };
16986c16fd8bSJeya R
16992b2fb816SEkansh Gupta /*
17002b2fb816SEkansh Gupta * Capability filled in userspace. This carries the information
17012b2fb816SEkansh Gupta * about the remoteproc support which is fetched from the remoteproc
17022b2fb816SEkansh Gupta * sysfs node by userspace.
17032b2fb816SEkansh Gupta */
17046c16fd8bSJeya R dsp_attr_buf[0] = 0;
17052b2fb816SEkansh Gupta dsp_attr_buf_len -= 1;
17066c16fd8bSJeya R
17076c16fd8bSJeya R args[0].ptr = (u64)(uintptr_t)&dsp_attr_buf_len;
17086c16fd8bSJeya R args[0].length = sizeof(dsp_attr_buf_len);
17096c16fd8bSJeya R args[0].fd = -1;
17106c16fd8bSJeya R args[1].ptr = (u64)(uintptr_t)&dsp_attr_buf[1];
17112b2fb816SEkansh Gupta args[1].length = dsp_attr_buf_len * sizeof(u32);
17126c16fd8bSJeya R args[1].fd = -1;
17136c16fd8bSJeya R
17146c16fd8bSJeya R return fastrpc_internal_invoke(fl, true, FASTRPC_DSP_UTILITIES_HANDLE,
17156c16fd8bSJeya R FASTRPC_SCALARS(0, 1, 1), args);
17166c16fd8bSJeya R }
17176c16fd8bSJeya R
fastrpc_get_info_from_kernel(struct fastrpc_ioctl_capability * cap,struct fastrpc_user * fl)17186c16fd8bSJeya R static int fastrpc_get_info_from_kernel(struct fastrpc_ioctl_capability *cap,
17196c16fd8bSJeya R struct fastrpc_user *fl)
17206c16fd8bSJeya R {
17216c16fd8bSJeya R struct fastrpc_channel_ctx *cctx = fl->cctx;
17226c16fd8bSJeya R uint32_t attribute_id = cap->attribute_id;
17236c16fd8bSJeya R uint32_t *dsp_attributes;
17246c16fd8bSJeya R unsigned long flags;
17256c16fd8bSJeya R uint32_t domain = cap->domain;
17266c16fd8bSJeya R int err;
17276c16fd8bSJeya R
17286c16fd8bSJeya R spin_lock_irqsave(&cctx->lock, flags);
17296c16fd8bSJeya R /* check if we already have queried dsp for attributes */
17306c16fd8bSJeya R if (cctx->valid_attributes) {
17316c16fd8bSJeya R spin_unlock_irqrestore(&cctx->lock, flags);
17326c16fd8bSJeya R goto done;
17336c16fd8bSJeya R }
17346c16fd8bSJeya R spin_unlock_irqrestore(&cctx->lock, flags);
17356c16fd8bSJeya R
17366c16fd8bSJeya R dsp_attributes = kzalloc(FASTRPC_MAX_DSP_ATTRIBUTES_LEN, GFP_KERNEL);
17376c16fd8bSJeya R if (!dsp_attributes)
17386c16fd8bSJeya R return -ENOMEM;
17396c16fd8bSJeya R
17402b2fb816SEkansh Gupta err = fastrpc_get_info_from_dsp(fl, dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES);
17416c16fd8bSJeya R if (err == DSP_UNSUPPORTED_API) {
17426c16fd8bSJeya R dev_info(&cctx->rpdev->dev,
17436c16fd8bSJeya R "Warning: DSP capabilities not supported on domain: %d\n", domain);
17446c16fd8bSJeya R kfree(dsp_attributes);
17456c16fd8bSJeya R return -EOPNOTSUPP;
17466c16fd8bSJeya R } else if (err) {
17476c16fd8bSJeya R dev_err(&cctx->rpdev->dev, "Error: dsp information is incorrect err: %d\n", err);
17486c16fd8bSJeya R kfree(dsp_attributes);
17496c16fd8bSJeya R return err;
17506c16fd8bSJeya R }
17516c16fd8bSJeya R
17526c16fd8bSJeya R spin_lock_irqsave(&cctx->lock, flags);
17536c16fd8bSJeya R memcpy(cctx->dsp_attributes, dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES_LEN);
17546c16fd8bSJeya R cctx->valid_attributes = true;
17556c16fd8bSJeya R spin_unlock_irqrestore(&cctx->lock, flags);
17566c16fd8bSJeya R kfree(dsp_attributes);
17576c16fd8bSJeya R done:
17586c16fd8bSJeya R cap->capability = cctx->dsp_attributes[attribute_id];
17596c16fd8bSJeya R return 0;
17606c16fd8bSJeya R }
17616c16fd8bSJeya R
fastrpc_get_dsp_info(struct fastrpc_user * fl,char __user * argp)17626c16fd8bSJeya R static int fastrpc_get_dsp_info(struct fastrpc_user *fl, char __user *argp)
17636c16fd8bSJeya R {
17646c16fd8bSJeya R struct fastrpc_ioctl_capability cap = {0};
17656c16fd8bSJeya R int err = 0;
17666c16fd8bSJeya R
17676c16fd8bSJeya R if (copy_from_user(&cap, argp, sizeof(cap)))
17686c16fd8bSJeya R return -EFAULT;
17696c16fd8bSJeya R
17706c16fd8bSJeya R cap.capability = 0;
17716c16fd8bSJeya R if (cap.domain >= FASTRPC_DEV_MAX) {
17726c16fd8bSJeya R dev_err(&fl->cctx->rpdev->dev, "Error: Invalid domain id:%d, err:%d\n",
17736c16fd8bSJeya R cap.domain, err);
17746c16fd8bSJeya R return -ECHRNG;
17756c16fd8bSJeya R }
17766c16fd8bSJeya R
17776c16fd8bSJeya R /* Fastrpc Capablities does not support modem domain */
17786c16fd8bSJeya R if (cap.domain == MDSP_DOMAIN_ID) {
17796c16fd8bSJeya R dev_err(&fl->cctx->rpdev->dev, "Error: modem not supported %d\n", err);
17806c16fd8bSJeya R return -ECHRNG;
17816c16fd8bSJeya R }
17826c16fd8bSJeya R
17836c16fd8bSJeya R if (cap.attribute_id >= FASTRPC_MAX_DSP_ATTRIBUTES) {
17846c16fd8bSJeya R dev_err(&fl->cctx->rpdev->dev, "Error: invalid attribute: %d, err: %d\n",
17856c16fd8bSJeya R cap.attribute_id, err);
17866c16fd8bSJeya R return -EOVERFLOW;
17876c16fd8bSJeya R }
17886c16fd8bSJeya R
17896c16fd8bSJeya R err = fastrpc_get_info_from_kernel(&cap, fl);
17906c16fd8bSJeya R if (err)
17916c16fd8bSJeya R return err;
17926c16fd8bSJeya R
1793ba602c70SEkansh Gupta if (copy_to_user(argp, &cap, sizeof(cap)))
17946c16fd8bSJeya R return -EFAULT;
17956c16fd8bSJeya R
17966c16fd8bSJeya R return 0;
17976c16fd8bSJeya R }
17986c16fd8bSJeya R
fastrpc_req_munmap_impl(struct fastrpc_user * fl,struct fastrpc_buf * buf)179972fa6f78SAbel Vesa static int fastrpc_req_munmap_impl(struct fastrpc_user *fl, struct fastrpc_buf *buf)
18002419e55eSJorge Ramirez-Ortiz {
18012419e55eSJorge Ramirez-Ortiz struct fastrpc_invoke_args args[1] = { [0] = { 0 } };
18022419e55eSJorge Ramirez-Ortiz struct fastrpc_munmap_req_msg req_msg;
18032419e55eSJorge Ramirez-Ortiz struct device *dev = fl->sctx->dev;
18042419e55eSJorge Ramirez-Ortiz int err;
18052419e55eSJorge Ramirez-Ortiz u32 sc;
18062419e55eSJorge Ramirez-Ortiz
18072419e55eSJorge Ramirez-Ortiz req_msg.pgid = fl->tgid;
18082419e55eSJorge Ramirez-Ortiz req_msg.size = buf->size;
18092419e55eSJorge Ramirez-Ortiz req_msg.vaddr = buf->raddr;
18102419e55eSJorge Ramirez-Ortiz
18112419e55eSJorge Ramirez-Ortiz args[0].ptr = (u64) (uintptr_t) &req_msg;
18122419e55eSJorge Ramirez-Ortiz args[0].length = sizeof(req_msg);
18132419e55eSJorge Ramirez-Ortiz
18142419e55eSJorge Ramirez-Ortiz sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MUNMAP, 1, 0);
18152419e55eSJorge Ramirez-Ortiz err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc,
18162419e55eSJorge Ramirez-Ortiz &args[0]);
18172419e55eSJorge Ramirez-Ortiz if (!err) {
18182419e55eSJorge Ramirez-Ortiz dev_dbg(dev, "unmmap\tpt 0x%09lx OK\n", buf->raddr);
18192419e55eSJorge Ramirez-Ortiz spin_lock(&fl->lock);
18202419e55eSJorge Ramirez-Ortiz list_del(&buf->node);
18212419e55eSJorge Ramirez-Ortiz spin_unlock(&fl->lock);
18222419e55eSJorge Ramirez-Ortiz fastrpc_buf_free(buf);
18232419e55eSJorge Ramirez-Ortiz } else {
18242419e55eSJorge Ramirez-Ortiz dev_err(dev, "unmmap\tpt 0x%09lx ERROR\n", buf->raddr);
18252419e55eSJorge Ramirez-Ortiz }
18262419e55eSJorge Ramirez-Ortiz
18272419e55eSJorge Ramirez-Ortiz return err;
18282419e55eSJorge Ramirez-Ortiz }
18292419e55eSJorge Ramirez-Ortiz
fastrpc_req_munmap(struct fastrpc_user * fl,char __user * argp)18302419e55eSJorge Ramirez-Ortiz static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp)
18312419e55eSJorge Ramirez-Ortiz {
183272fa6f78SAbel Vesa struct fastrpc_buf *buf = NULL, *iter, *b;
18332419e55eSJorge Ramirez-Ortiz struct fastrpc_req_munmap req;
183472fa6f78SAbel Vesa struct device *dev = fl->sctx->dev;
18352419e55eSJorge Ramirez-Ortiz
18362419e55eSJorge Ramirez-Ortiz if (copy_from_user(&req, argp, sizeof(req)))
18372419e55eSJorge Ramirez-Ortiz return -EFAULT;
18382419e55eSJorge Ramirez-Ortiz
183972fa6f78SAbel Vesa spin_lock(&fl->lock);
184072fa6f78SAbel Vesa list_for_each_entry_safe(iter, b, &fl->mmaps, node) {
184172fa6f78SAbel Vesa if ((iter->raddr == req.vaddrout) && (iter->size == req.size)) {
184272fa6f78SAbel Vesa buf = iter;
184372fa6f78SAbel Vesa break;
184472fa6f78SAbel Vesa }
184572fa6f78SAbel Vesa }
184672fa6f78SAbel Vesa spin_unlock(&fl->lock);
184772fa6f78SAbel Vesa
184872fa6f78SAbel Vesa if (!buf) {
184972fa6f78SAbel Vesa dev_err(dev, "mmap\t\tpt 0x%09llx [len 0x%08llx] not in list\n",
185072fa6f78SAbel Vesa req.vaddrout, req.size);
185172fa6f78SAbel Vesa return -EINVAL;
185272fa6f78SAbel Vesa }
185372fa6f78SAbel Vesa
185472fa6f78SAbel Vesa return fastrpc_req_munmap_impl(fl, buf);
18552419e55eSJorge Ramirez-Ortiz }
18562419e55eSJorge Ramirez-Ortiz
fastrpc_req_mmap(struct fastrpc_user * fl,char __user * argp)18572419e55eSJorge Ramirez-Ortiz static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp)
18582419e55eSJorge Ramirez-Ortiz {
18592419e55eSJorge Ramirez-Ortiz struct fastrpc_invoke_args args[3] = { [0 ... 2] = { 0 } };
18602419e55eSJorge Ramirez-Ortiz struct fastrpc_buf *buf = NULL;
18612419e55eSJorge Ramirez-Ortiz struct fastrpc_mmap_req_msg req_msg;
18622419e55eSJorge Ramirez-Ortiz struct fastrpc_mmap_rsp_msg rsp_msg;
18632419e55eSJorge Ramirez-Ortiz struct fastrpc_phy_page pages;
18642419e55eSJorge Ramirez-Ortiz struct fastrpc_req_mmap req;
18652419e55eSJorge Ramirez-Ortiz struct device *dev = fl->sctx->dev;
18662419e55eSJorge Ramirez-Ortiz int err;
18672419e55eSJorge Ramirez-Ortiz u32 sc;
18682419e55eSJorge Ramirez-Ortiz
18692419e55eSJorge Ramirez-Ortiz if (copy_from_user(&req, argp, sizeof(req)))
18702419e55eSJorge Ramirez-Ortiz return -EFAULT;
18712419e55eSJorge Ramirez-Ortiz
1872532ad70cSAbel Vesa if (req.flags != ADSP_MMAP_ADD_PAGES && req.flags != ADSP_MMAP_REMOTE_HEAP_ADDR) {
18732419e55eSJorge Ramirez-Ortiz dev_err(dev, "flag not supported 0x%x\n", req.flags);
1874532ad70cSAbel Vesa
18752419e55eSJorge Ramirez-Ortiz return -EINVAL;
18762419e55eSJorge Ramirez-Ortiz }
18772419e55eSJorge Ramirez-Ortiz
18782419e55eSJorge Ramirez-Ortiz if (req.vaddrin) {
18792419e55eSJorge Ramirez-Ortiz dev_err(dev, "adding user allocated pages is not supported\n");
18802419e55eSJorge Ramirez-Ortiz return -EINVAL;
18812419e55eSJorge Ramirez-Ortiz }
18822419e55eSJorge Ramirez-Ortiz
1883ada6c2d9SEkansh Gupta if (req.flags == ADSP_MMAP_REMOTE_HEAP_ADDR)
1884ada6c2d9SEkansh Gupta err = fastrpc_remote_heap_alloc(fl, dev, req.size, &buf);
1885ada6c2d9SEkansh Gupta else
1886ada6c2d9SEkansh Gupta err = fastrpc_buf_alloc(fl, dev, req.size, &buf);
1887ada6c2d9SEkansh Gupta
18882419e55eSJorge Ramirez-Ortiz if (err) {
18892419e55eSJorge Ramirez-Ortiz dev_err(dev, "failed to allocate buffer\n");
18902419e55eSJorge Ramirez-Ortiz return err;
18912419e55eSJorge Ramirez-Ortiz }
18922419e55eSJorge Ramirez-Ortiz
18932419e55eSJorge Ramirez-Ortiz req_msg.pgid = fl->tgid;
18942419e55eSJorge Ramirez-Ortiz req_msg.flags = req.flags;
18952419e55eSJorge Ramirez-Ortiz req_msg.vaddr = req.vaddrin;
18962419e55eSJorge Ramirez-Ortiz req_msg.num = sizeof(pages);
18972419e55eSJorge Ramirez-Ortiz
18982419e55eSJorge Ramirez-Ortiz args[0].ptr = (u64) (uintptr_t) &req_msg;
18992419e55eSJorge Ramirez-Ortiz args[0].length = sizeof(req_msg);
19002419e55eSJorge Ramirez-Ortiz
19012419e55eSJorge Ramirez-Ortiz pages.addr = buf->phys;
19022419e55eSJorge Ramirez-Ortiz pages.size = buf->size;
19032419e55eSJorge Ramirez-Ortiz
19042419e55eSJorge Ramirez-Ortiz args[1].ptr = (u64) (uintptr_t) &pages;
19052419e55eSJorge Ramirez-Ortiz args[1].length = sizeof(pages);
19062419e55eSJorge Ramirez-Ortiz
19072419e55eSJorge Ramirez-Ortiz args[2].ptr = (u64) (uintptr_t) &rsp_msg;
19082419e55eSJorge Ramirez-Ortiz args[2].length = sizeof(rsp_msg);
19092419e55eSJorge Ramirez-Ortiz
19102419e55eSJorge Ramirez-Ortiz sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MMAP, 2, 1);
19112419e55eSJorge Ramirez-Ortiz err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc,
19122419e55eSJorge Ramirez-Ortiz &args[0]);
19132419e55eSJorge Ramirez-Ortiz if (err) {
19142419e55eSJorge Ramirez-Ortiz dev_err(dev, "mmap error (len 0x%08llx)\n", buf->size);
1915*f77dc8a7SSukrut Bellary fastrpc_buf_free(buf);
1916*f77dc8a7SSukrut Bellary return err;
19172419e55eSJorge Ramirez-Ortiz }
19182419e55eSJorge Ramirez-Ortiz
19192419e55eSJorge Ramirez-Ortiz /* update the buffer to be able to deallocate the memory on the DSP */
19202419e55eSJorge Ramirez-Ortiz buf->raddr = (uintptr_t) rsp_msg.vaddr;
19212419e55eSJorge Ramirez-Ortiz
19222419e55eSJorge Ramirez-Ortiz /* let the client know the address to use */
19232419e55eSJorge Ramirez-Ortiz req.vaddrout = rsp_msg.vaddr;
19242419e55eSJorge Ramirez-Ortiz
1925532ad70cSAbel Vesa /* Add memory to static PD pool, protection thru hypervisor */
19263c7d0079SEkansh Gupta if (req.flags == ADSP_MMAP_REMOTE_HEAP_ADDR && fl->cctx->vmcount) {
1927a62a30ccSEkansh Gupta u64 src_perms = BIT(QCOM_SCM_VMID_HLOS);
1928a62a30ccSEkansh Gupta
1929ada6c2d9SEkansh Gupta err = qcom_scm_assign_mem(buf->phys, (u64)buf->size,
1930a62a30ccSEkansh Gupta &src_perms, fl->cctx->vmperms, fl->cctx->vmcount);
1931532ad70cSAbel Vesa if (err) {
1932532ad70cSAbel Vesa dev_err(fl->sctx->dev, "Failed to assign memory phys 0x%llx size 0x%llx err %d",
1933532ad70cSAbel Vesa buf->phys, buf->size, err);
1934532ad70cSAbel Vesa goto err_assign;
1935532ad70cSAbel Vesa }
1936532ad70cSAbel Vesa }
1937532ad70cSAbel Vesa
19382419e55eSJorge Ramirez-Ortiz spin_lock(&fl->lock);
19392419e55eSJorge Ramirez-Ortiz list_add_tail(&buf->node, &fl->mmaps);
19402419e55eSJorge Ramirez-Ortiz spin_unlock(&fl->lock);
19412419e55eSJorge Ramirez-Ortiz
19422419e55eSJorge Ramirez-Ortiz if (copy_to_user((void __user *)argp, &req, sizeof(req))) {
194372fa6f78SAbel Vesa err = -EFAULT;
194472fa6f78SAbel Vesa goto err_assign;
19452419e55eSJorge Ramirez-Ortiz }
19462419e55eSJorge Ramirez-Ortiz
19472419e55eSJorge Ramirez-Ortiz dev_dbg(dev, "mmap\t\tpt 0x%09lx OK [len 0x%08llx]\n",
19482419e55eSJorge Ramirez-Ortiz buf->raddr, buf->size);
19492419e55eSJorge Ramirez-Ortiz
19502419e55eSJorge Ramirez-Ortiz return 0;
19512419e55eSJorge Ramirez-Ortiz
195272fa6f78SAbel Vesa err_assign:
195372fa6f78SAbel Vesa fastrpc_req_munmap_impl(fl, buf);
19542419e55eSJorge Ramirez-Ortiz
19552419e55eSJorge Ramirez-Ortiz return err;
19562419e55eSJorge Ramirez-Ortiz }
19572419e55eSJorge Ramirez-Ortiz
fastrpc_req_mem_unmap_impl(struct fastrpc_user * fl,struct fastrpc_mem_unmap * req)19585c1b97c7SJeya R static int fastrpc_req_mem_unmap_impl(struct fastrpc_user *fl, struct fastrpc_mem_unmap *req)
19595c1b97c7SJeya R {
19605c1b97c7SJeya R struct fastrpc_invoke_args args[1] = { [0] = { 0 } };
1961c5c07c59SSrinivas Kandagatla struct fastrpc_map *map = NULL, *iter, *m;
19625c1b97c7SJeya R struct fastrpc_mem_unmap_req_msg req_msg = { 0 };
19635c1b97c7SJeya R int err = 0;
19645c1b97c7SJeya R u32 sc;
19655c1b97c7SJeya R struct device *dev = fl->sctx->dev;
19665c1b97c7SJeya R
19675c1b97c7SJeya R spin_lock(&fl->lock);
1968c5c07c59SSrinivas Kandagatla list_for_each_entry_safe(iter, m, &fl->maps, node) {
1969c5c07c59SSrinivas Kandagatla if ((req->fd < 0 || iter->fd == req->fd) && (iter->raddr == req->vaddr)) {
1970c5c07c59SSrinivas Kandagatla map = iter;
19715c1b97c7SJeya R break;
1972c5c07c59SSrinivas Kandagatla }
19735c1b97c7SJeya R }
19745c1b97c7SJeya R
19755c1b97c7SJeya R spin_unlock(&fl->lock);
19765c1b97c7SJeya R
19775c1b97c7SJeya R if (!map) {
19785c1b97c7SJeya R dev_err(dev, "map not in list\n");
19795c1b97c7SJeya R return -EINVAL;
19805c1b97c7SJeya R }
19815c1b97c7SJeya R
19825c1b97c7SJeya R req_msg.pgid = fl->tgid;
19835c1b97c7SJeya R req_msg.len = map->len;
19845c1b97c7SJeya R req_msg.vaddrin = map->raddr;
19855c1b97c7SJeya R req_msg.fd = map->fd;
19865c1b97c7SJeya R
19875c1b97c7SJeya R args[0].ptr = (u64) (uintptr_t) &req_msg;
19885c1b97c7SJeya R args[0].length = sizeof(req_msg);
19895c1b97c7SJeya R
19905c1b97c7SJeya R sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MEM_UNMAP, 1, 0);
19915c1b97c7SJeya R err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc,
19925c1b97c7SJeya R &args[0]);
199350914338SEkansh Gupta if (err) {
19945c1b97c7SJeya R dev_err(dev, "unmmap\tpt fd = %d, 0x%09llx error\n", map->fd, map->raddr);
19955c1b97c7SJeya R return err;
19965c1b97c7SJeya R }
199750914338SEkansh Gupta fastrpc_map_put(map);
199850914338SEkansh Gupta
199950914338SEkansh Gupta return 0;
200050914338SEkansh Gupta }
20015c1b97c7SJeya R
fastrpc_req_mem_unmap(struct fastrpc_user * fl,char __user * argp)20025c1b97c7SJeya R static int fastrpc_req_mem_unmap(struct fastrpc_user *fl, char __user *argp)
20035c1b97c7SJeya R {
20045c1b97c7SJeya R struct fastrpc_mem_unmap req;
20055c1b97c7SJeya R
20065c1b97c7SJeya R if (copy_from_user(&req, argp, sizeof(req)))
20075c1b97c7SJeya R return -EFAULT;
20085c1b97c7SJeya R
20095c1b97c7SJeya R return fastrpc_req_mem_unmap_impl(fl, &req);
20105c1b97c7SJeya R }
20115c1b97c7SJeya R
fastrpc_req_mem_map(struct fastrpc_user * fl,char __user * argp)20125c1b97c7SJeya R static int fastrpc_req_mem_map(struct fastrpc_user *fl, char __user *argp)
20135c1b97c7SJeya R {
20145c1b97c7SJeya R struct fastrpc_invoke_args args[4] = { [0 ... 3] = { 0 } };
20155c1b97c7SJeya R struct fastrpc_mem_map_req_msg req_msg = { 0 };
20165c1b97c7SJeya R struct fastrpc_mmap_rsp_msg rsp_msg = { 0 };
20175c1b97c7SJeya R struct fastrpc_mem_unmap req_unmap = { 0 };
20185c1b97c7SJeya R struct fastrpc_phy_page pages = { 0 };
20195c1b97c7SJeya R struct fastrpc_mem_map req;
20205c1b97c7SJeya R struct device *dev = fl->sctx->dev;
20215c1b97c7SJeya R struct fastrpc_map *map = NULL;
20225c1b97c7SJeya R int err;
20235c1b97c7SJeya R u32 sc;
20245c1b97c7SJeya R
20255c1b97c7SJeya R if (copy_from_user(&req, argp, sizeof(req)))
20265c1b97c7SJeya R return -EFAULT;
20275c1b97c7SJeya R
20285c1b97c7SJeya R /* create SMMU mapping */
20293abe3ab3SSrinivas Kandagatla err = fastrpc_map_create(fl, req.fd, req.length, 0, &map);
20305c1b97c7SJeya R if (err) {
20315c1b97c7SJeya R dev_err(dev, "failed to map buffer, fd = %d\n", req.fd);
20325c1b97c7SJeya R return err;
20335c1b97c7SJeya R }
20345c1b97c7SJeya R
20355c1b97c7SJeya R req_msg.pgid = fl->tgid;
20365c1b97c7SJeya R req_msg.fd = req.fd;
20375c1b97c7SJeya R req_msg.offset = req.offset;
20385c1b97c7SJeya R req_msg.vaddrin = req.vaddrin;
20395c1b97c7SJeya R map->va = (void *) (uintptr_t) req.vaddrin;
20405c1b97c7SJeya R req_msg.flags = req.flags;
20415c1b97c7SJeya R req_msg.num = sizeof(pages);
20425c1b97c7SJeya R req_msg.data_len = 0;
20435c1b97c7SJeya R
20445c1b97c7SJeya R args[0].ptr = (u64) (uintptr_t) &req_msg;
20455c1b97c7SJeya R args[0].length = sizeof(req_msg);
20465c1b97c7SJeya R
20475c1b97c7SJeya R pages.addr = map->phys;
20485c1b97c7SJeya R pages.size = map->size;
20495c1b97c7SJeya R
20505c1b97c7SJeya R args[1].ptr = (u64) (uintptr_t) &pages;
20515c1b97c7SJeya R args[1].length = sizeof(pages);
20525c1b97c7SJeya R
20535c1b97c7SJeya R args[2].ptr = (u64) (uintptr_t) &pages;
20545c1b97c7SJeya R args[2].length = 0;
20555c1b97c7SJeya R
20565c1b97c7SJeya R args[3].ptr = (u64) (uintptr_t) &rsp_msg;
20575c1b97c7SJeya R args[3].length = sizeof(rsp_msg);
20585c1b97c7SJeya R
20595c1b97c7SJeya R sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MEM_MAP, 3, 1);
20605c1b97c7SJeya R err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, &args[0]);
20615c1b97c7SJeya R if (err) {
20625c1b97c7SJeya R dev_err(dev, "mem mmap error, fd %d, vaddr %llx, size %lld\n",
20635c1b97c7SJeya R req.fd, req.vaddrin, map->size);
20645c1b97c7SJeya R goto err_invoke;
20655c1b97c7SJeya R }
20665c1b97c7SJeya R
20675c1b97c7SJeya R /* update the buffer to be able to deallocate the memory on the DSP */
20685c1b97c7SJeya R map->raddr = rsp_msg.vaddr;
20695c1b97c7SJeya R
20705c1b97c7SJeya R /* let the client know the address to use */
20715c1b97c7SJeya R req.vaddrout = rsp_msg.vaddr;
20725c1b97c7SJeya R
20735c1b97c7SJeya R if (copy_to_user((void __user *)argp, &req, sizeof(req))) {
20745c1b97c7SJeya R /* unmap the memory and release the buffer */
20755c1b97c7SJeya R req_unmap.vaddr = (uintptr_t) rsp_msg.vaddr;
20765c1b97c7SJeya R req_unmap.length = map->size;
20775c1b97c7SJeya R fastrpc_req_mem_unmap_impl(fl, &req_unmap);
20785c1b97c7SJeya R return -EFAULT;
20795c1b97c7SJeya R }
20805c1b97c7SJeya R
20815c1b97c7SJeya R return 0;
20825c1b97c7SJeya R
20835c1b97c7SJeya R err_invoke:
20845c1b97c7SJeya R fastrpc_map_put(map);
20855c1b97c7SJeya R
20865c1b97c7SJeya R return err;
20875c1b97c7SJeya R }
20885c1b97c7SJeya R
fastrpc_device_ioctl(struct file * file,unsigned int cmd,unsigned long arg)2089c68cfb71SSrinivas Kandagatla static long fastrpc_device_ioctl(struct file *file, unsigned int cmd,
2090c68cfb71SSrinivas Kandagatla unsigned long arg)
2091c68cfb71SSrinivas Kandagatla {
2092c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data;
2093c68cfb71SSrinivas Kandagatla char __user *argp = (char __user *)arg;
2094c68cfb71SSrinivas Kandagatla int err;
2095c68cfb71SSrinivas Kandagatla
2096c68cfb71SSrinivas Kandagatla switch (cmd) {
2097c68cfb71SSrinivas Kandagatla case FASTRPC_IOCTL_INVOKE:
2098c68cfb71SSrinivas Kandagatla err = fastrpc_invoke(fl, argp);
2099c68cfb71SSrinivas Kandagatla break;
2100d73f71c7SSrinivas Kandagatla case FASTRPC_IOCTL_INIT_ATTACH:
21011959ab9eSAbel Vesa err = fastrpc_init_attach(fl, ROOT_PD);
21026010d9beSJonathan Marek break;
21036010d9beSJonathan Marek case FASTRPC_IOCTL_INIT_ATTACH_SNS:
21046010d9beSJonathan Marek err = fastrpc_init_attach(fl, SENSORS_PD);
2105d73f71c7SSrinivas Kandagatla break;
210608715610SAbel Vesa case FASTRPC_IOCTL_INIT_CREATE_STATIC:
210708715610SAbel Vesa err = fastrpc_init_create_static_process(fl, argp);
210808715610SAbel Vesa break;
2109d73f71c7SSrinivas Kandagatla case FASTRPC_IOCTL_INIT_CREATE:
2110d73f71c7SSrinivas Kandagatla err = fastrpc_init_create_process(fl, argp);
2111d73f71c7SSrinivas Kandagatla break;
21126cffd795SSrinivas Kandagatla case FASTRPC_IOCTL_ALLOC_DMA_BUFF:
21136cffd795SSrinivas Kandagatla err = fastrpc_dmabuf_alloc(fl, argp);
21146cffd795SSrinivas Kandagatla break;
21152419e55eSJorge Ramirez-Ortiz case FASTRPC_IOCTL_MMAP:
21162419e55eSJorge Ramirez-Ortiz err = fastrpc_req_mmap(fl, argp);
21172419e55eSJorge Ramirez-Ortiz break;
21182419e55eSJorge Ramirez-Ortiz case FASTRPC_IOCTL_MUNMAP:
21192419e55eSJorge Ramirez-Ortiz err = fastrpc_req_munmap(fl, argp);
21202419e55eSJorge Ramirez-Ortiz break;
21215c1b97c7SJeya R case FASTRPC_IOCTL_MEM_MAP:
21225c1b97c7SJeya R err = fastrpc_req_mem_map(fl, argp);
21235c1b97c7SJeya R break;
21245c1b97c7SJeya R case FASTRPC_IOCTL_MEM_UNMAP:
21255c1b97c7SJeya R err = fastrpc_req_mem_unmap(fl, argp);
21265c1b97c7SJeya R break;
21276c16fd8bSJeya R case FASTRPC_IOCTL_GET_DSP_INFO:
21286c16fd8bSJeya R err = fastrpc_get_dsp_info(fl, argp);
21296c16fd8bSJeya R break;
2130c68cfb71SSrinivas Kandagatla default:
2131c68cfb71SSrinivas Kandagatla err = -ENOTTY;
2132c68cfb71SSrinivas Kandagatla break;
2133c68cfb71SSrinivas Kandagatla }
2134c68cfb71SSrinivas Kandagatla
2135c68cfb71SSrinivas Kandagatla return err;
2136c68cfb71SSrinivas Kandagatla }
2137c68cfb71SSrinivas Kandagatla
2138f6f9279fSSrinivas Kandagatla static const struct file_operations fastrpc_fops = {
2139f6f9279fSSrinivas Kandagatla .open = fastrpc_device_open,
2140f6f9279fSSrinivas Kandagatla .release = fastrpc_device_release,
2141c68cfb71SSrinivas Kandagatla .unlocked_ioctl = fastrpc_device_ioctl,
2142c68cfb71SSrinivas Kandagatla .compat_ioctl = fastrpc_device_ioctl,
2143f6f9279fSSrinivas Kandagatla };
2144f6f9279fSSrinivas Kandagatla
fastrpc_cb_probe(struct platform_device * pdev)2145f6f9279fSSrinivas Kandagatla static int fastrpc_cb_probe(struct platform_device *pdev)
2146f6f9279fSSrinivas Kandagatla {
2147f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx;
2148f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *sess;
2149f6f9279fSSrinivas Kandagatla struct device *dev = &pdev->dev;
2150f6f9279fSSrinivas Kandagatla int i, sessions = 0;
2151977e6c8dSSrinivas Kandagatla unsigned long flags;
215201b76c32SBo YU int rc;
2153f6f9279fSSrinivas Kandagatla
2154f6f9279fSSrinivas Kandagatla cctx = dev_get_drvdata(dev->parent);
2155f6f9279fSSrinivas Kandagatla if (!cctx)
2156f6f9279fSSrinivas Kandagatla return -EINVAL;
2157f6f9279fSSrinivas Kandagatla
2158f6f9279fSSrinivas Kandagatla of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions);
2159f6f9279fSSrinivas Kandagatla
2160977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags);
21619baa1415SJohan Hovold if (cctx->sesscount >= FASTRPC_MAX_SESSIONS) {
21629baa1415SJohan Hovold dev_err(&pdev->dev, "too many sessions\n");
21639baa1415SJohan Hovold spin_unlock_irqrestore(&cctx->lock, flags);
21649baa1415SJohan Hovold return -ENOSPC;
21659baa1415SJohan Hovold }
2166d245f43aSJohan Hovold sess = &cctx->session[cctx->sesscount++];
2167f6f9279fSSrinivas Kandagatla sess->used = false;
2168f6f9279fSSrinivas Kandagatla sess->valid = true;
2169f6f9279fSSrinivas Kandagatla sess->dev = dev;
2170f6f9279fSSrinivas Kandagatla dev_set_drvdata(dev, sess);
2171f6f9279fSSrinivas Kandagatla
2172f6f9279fSSrinivas Kandagatla if (of_property_read_u32(dev->of_node, "reg", &sess->sid))
2173f6f9279fSSrinivas Kandagatla dev_info(dev, "FastRPC Session ID not specified in DT\n");
2174f6f9279fSSrinivas Kandagatla
2175f6f9279fSSrinivas Kandagatla if (sessions > 0) {
2176f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *dup_sess;
2177f6f9279fSSrinivas Kandagatla
2178f6f9279fSSrinivas Kandagatla for (i = 1; i < sessions; i++) {
2179d245f43aSJohan Hovold if (cctx->sesscount >= FASTRPC_MAX_SESSIONS)
2180f6f9279fSSrinivas Kandagatla break;
2181d245f43aSJohan Hovold dup_sess = &cctx->session[cctx->sesscount++];
2182f6f9279fSSrinivas Kandagatla memcpy(dup_sess, sess, sizeof(*dup_sess));
2183f6f9279fSSrinivas Kandagatla }
2184f6f9279fSSrinivas Kandagatla }
2185977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags);
218601b76c32SBo YU rc = dma_set_mask(dev, DMA_BIT_MASK(32));
218701b76c32SBo YU if (rc) {
218801b76c32SBo YU dev_err(dev, "32-bit DMA enable failed\n");
218901b76c32SBo YU return rc;
219001b76c32SBo YU }
2191f6f9279fSSrinivas Kandagatla
2192f6f9279fSSrinivas Kandagatla return 0;
2193f6f9279fSSrinivas Kandagatla }
2194f6f9279fSSrinivas Kandagatla
fastrpc_cb_remove(struct platform_device * pdev)2195f6f9279fSSrinivas Kandagatla static int fastrpc_cb_remove(struct platform_device *pdev)
2196f6f9279fSSrinivas Kandagatla {
2197f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent);
2198f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev);
2199977e6c8dSSrinivas Kandagatla unsigned long flags;
2200f6f9279fSSrinivas Kandagatla int i;
2201f6f9279fSSrinivas Kandagatla
2202977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags);
2203e3c4e10eSEkansh Gupta for (i = 0; i < FASTRPC_MAX_SESSIONS; i++) {
2204f6f9279fSSrinivas Kandagatla if (cctx->session[i].sid == sess->sid) {
2205f6f9279fSSrinivas Kandagatla cctx->session[i].valid = false;
2206f6f9279fSSrinivas Kandagatla cctx->sesscount--;
2207f6f9279fSSrinivas Kandagatla }
2208f6f9279fSSrinivas Kandagatla }
2209977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags);
2210f6f9279fSSrinivas Kandagatla
2211f6f9279fSSrinivas Kandagatla return 0;
2212f6f9279fSSrinivas Kandagatla }
2213f6f9279fSSrinivas Kandagatla
2214f6f9279fSSrinivas Kandagatla static const struct of_device_id fastrpc_match_table[] = {
2215f6f9279fSSrinivas Kandagatla { .compatible = "qcom,fastrpc-compute-cb", },
2216f6f9279fSSrinivas Kandagatla {}
2217f6f9279fSSrinivas Kandagatla };
2218f6f9279fSSrinivas Kandagatla
2219f6f9279fSSrinivas Kandagatla static struct platform_driver fastrpc_cb_driver = {
2220f6f9279fSSrinivas Kandagatla .probe = fastrpc_cb_probe,
2221f6f9279fSSrinivas Kandagatla .remove = fastrpc_cb_remove,
2222f6f9279fSSrinivas Kandagatla .driver = {
2223f6f9279fSSrinivas Kandagatla .name = "qcom,fastrpc-cb",
2224f6f9279fSSrinivas Kandagatla .of_match_table = fastrpc_match_table,
2225f6f9279fSSrinivas Kandagatla .suppress_bind_attrs = true,
2226f6f9279fSSrinivas Kandagatla },
2227f6f9279fSSrinivas Kandagatla };
2228f6f9279fSSrinivas Kandagatla
fastrpc_device_register(struct device * dev,struct fastrpc_channel_ctx * cctx,bool is_secured,const char * domain)2229965602eaSSrinivas Kandagatla static int fastrpc_device_register(struct device *dev, struct fastrpc_channel_ctx *cctx,
22303abe3ab3SSrinivas Kandagatla bool is_secured, const char *domain)
2231965602eaSSrinivas Kandagatla {
2232965602eaSSrinivas Kandagatla struct fastrpc_device *fdev;
2233965602eaSSrinivas Kandagatla int err;
2234965602eaSSrinivas Kandagatla
2235965602eaSSrinivas Kandagatla fdev = devm_kzalloc(dev, sizeof(*fdev), GFP_KERNEL);
2236965602eaSSrinivas Kandagatla if (!fdev)
2237965602eaSSrinivas Kandagatla return -ENOMEM;
2238965602eaSSrinivas Kandagatla
22393abe3ab3SSrinivas Kandagatla fdev->secure = is_secured;
2240965602eaSSrinivas Kandagatla fdev->cctx = cctx;
2241965602eaSSrinivas Kandagatla fdev->miscdev.minor = MISC_DYNAMIC_MINOR;
2242965602eaSSrinivas Kandagatla fdev->miscdev.fops = &fastrpc_fops;
22433abe3ab3SSrinivas Kandagatla fdev->miscdev.name = devm_kasprintf(dev, GFP_KERNEL, "fastrpc-%s%s",
22443abe3ab3SSrinivas Kandagatla domain, is_secured ? "-secure" : "");
2245af2e19d8SClaudiu Beznea if (!fdev->miscdev.name)
2246af2e19d8SClaudiu Beznea return -ENOMEM;
2247af2e19d8SClaudiu Beznea
2248965602eaSSrinivas Kandagatla err = misc_register(&fdev->miscdev);
22493abe3ab3SSrinivas Kandagatla if (!err) {
22503abe3ab3SSrinivas Kandagatla if (is_secured)
22513abe3ab3SSrinivas Kandagatla cctx->secure_fdevice = fdev;
22523abe3ab3SSrinivas Kandagatla else
2253965602eaSSrinivas Kandagatla cctx->fdevice = fdev;
22543abe3ab3SSrinivas Kandagatla }
2255965602eaSSrinivas Kandagatla
2256965602eaSSrinivas Kandagatla return err;
2257965602eaSSrinivas Kandagatla }
2258965602eaSSrinivas Kandagatla
fastrpc_rpmsg_probe(struct rpmsg_device * rpdev)2259f6f9279fSSrinivas Kandagatla static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
2260f6f9279fSSrinivas Kandagatla {
2261f6f9279fSSrinivas Kandagatla struct device *rdev = &rpdev->dev;
2262f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *data;
2263e90d9119SVamsi Krishna Gattupalli int i, err, domain_id = -1, vmcount;
2264f6f9279fSSrinivas Kandagatla const char *domain;
22653abe3ab3SSrinivas Kandagatla bool secure_dsp;
2266e90d9119SVamsi Krishna Gattupalli unsigned int vmids[FASTRPC_MAX_VMIDS];
2267f6f9279fSSrinivas Kandagatla
2268f6f9279fSSrinivas Kandagatla err = of_property_read_string(rdev->of_node, "label", &domain);
2269f6f9279fSSrinivas Kandagatla if (err) {
2270f6f9279fSSrinivas Kandagatla dev_info(rdev, "FastRPC Domain not specified in DT\n");
2271f6f9279fSSrinivas Kandagatla return err;
2272f6f9279fSSrinivas Kandagatla }
2273f6f9279fSSrinivas Kandagatla
2274f6f9279fSSrinivas Kandagatla for (i = 0; i <= CDSP_DOMAIN_ID; i++) {
2275f6f9279fSSrinivas Kandagatla if (!strcmp(domains[i], domain)) {
2276f6f9279fSSrinivas Kandagatla domain_id = i;
2277f6f9279fSSrinivas Kandagatla break;
2278f6f9279fSSrinivas Kandagatla }
2279f6f9279fSSrinivas Kandagatla }
2280f6f9279fSSrinivas Kandagatla
2281f6f9279fSSrinivas Kandagatla if (domain_id < 0) {
2282f6f9279fSSrinivas Kandagatla dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id);
2283f6f9279fSSrinivas Kandagatla return -EINVAL;
2284f6f9279fSSrinivas Kandagatla }
2285f6f9279fSSrinivas Kandagatla
22861ce91d45SAbel Vesa if (of_reserved_mem_device_init_by_idx(rdev, rdev->of_node, 0))
22871ce91d45SAbel Vesa dev_info(rdev, "no reserved DMA memory for FASTRPC\n");
22881ce91d45SAbel Vesa
2289e90d9119SVamsi Krishna Gattupalli vmcount = of_property_read_variable_u32_array(rdev->of_node,
2290e90d9119SVamsi Krishna Gattupalli "qcom,vmids", &vmids[0], 0, FASTRPC_MAX_VMIDS);
2291e90d9119SVamsi Krishna Gattupalli if (vmcount < 0)
2292e90d9119SVamsi Krishna Gattupalli vmcount = 0;
2293e90d9119SVamsi Krishna Gattupalli else if (!qcom_scm_is_available())
2294e90d9119SVamsi Krishna Gattupalli return -EPROBE_DEFER;
2295e90d9119SVamsi Krishna Gattupalli
2296278d56f9SBjorn Andersson data = kzalloc(sizeof(*data), GFP_KERNEL);
2297278d56f9SBjorn Andersson if (!data)
2298278d56f9SBjorn Andersson return -ENOMEM;
2299278d56f9SBjorn Andersson
2300e90d9119SVamsi Krishna Gattupalli if (vmcount) {
2301e90d9119SVamsi Krishna Gattupalli data->vmcount = vmcount;
2302e90d9119SVamsi Krishna Gattupalli for (i = 0; i < data->vmcount; i++) {
2303e90d9119SVamsi Krishna Gattupalli data->vmperms[i].vmid = vmids[i];
2304e90d9119SVamsi Krishna Gattupalli data->vmperms[i].perm = QCOM_SCM_PERM_RWX;
2305e90d9119SVamsi Krishna Gattupalli }
2306e90d9119SVamsi Krishna Gattupalli }
23073abe3ab3SSrinivas Kandagatla
23083abe3ab3SSrinivas Kandagatla secure_dsp = !(of_property_read_bool(rdev->of_node, "qcom,non-secure-domain"));
23093abe3ab3SSrinivas Kandagatla data->secure = secure_dsp;
23103abe3ab3SSrinivas Kandagatla
23113abe3ab3SSrinivas Kandagatla switch (domain_id) {
23123abe3ab3SSrinivas Kandagatla case ADSP_DOMAIN_ID:
23133abe3ab3SSrinivas Kandagatla case MDSP_DOMAIN_ID:
23143abe3ab3SSrinivas Kandagatla case SDSP_DOMAIN_ID:
23157f1f4812SJeya R /* Unsigned PD offloading is only supported on CDSP*/
23167f1f4812SJeya R data->unsigned_support = false;
23173abe3ab3SSrinivas Kandagatla err = fastrpc_device_register(rdev, data, secure_dsp, domains[domain_id]);
23183abe3ab3SSrinivas Kandagatla if (err)
23193abe3ab3SSrinivas Kandagatla goto fdev_error;
23203abe3ab3SSrinivas Kandagatla break;
23213abe3ab3SSrinivas Kandagatla case CDSP_DOMAIN_ID:
23227f1f4812SJeya R data->unsigned_support = true;
23233abe3ab3SSrinivas Kandagatla /* Create both device nodes so that we can allow both Signed and Unsigned PD */
23243abe3ab3SSrinivas Kandagatla err = fastrpc_device_register(rdev, data, true, domains[domain_id]);
23253abe3ab3SSrinivas Kandagatla if (err)
23263abe3ab3SSrinivas Kandagatla goto fdev_error;
23273abe3ab3SSrinivas Kandagatla
23283abe3ab3SSrinivas Kandagatla err = fastrpc_device_register(rdev, data, false, domains[domain_id]);
23293abe3ab3SSrinivas Kandagatla if (err)
23303abe3ab3SSrinivas Kandagatla goto fdev_error;
23313abe3ab3SSrinivas Kandagatla break;
23323abe3ab3SSrinivas Kandagatla default:
23333abe3ab3SSrinivas Kandagatla err = -EINVAL;
23343abe3ab3SSrinivas Kandagatla goto fdev_error;
23350978de9fSSrinivas Kandagatla }
2336f6f9279fSSrinivas Kandagatla
2337278d56f9SBjorn Andersson kref_init(&data->refcount);
2338278d56f9SBjorn Andersson
2339f6f9279fSSrinivas Kandagatla dev_set_drvdata(&rpdev->dev, data);
23409bde43a0SAbel Vesa rdev->dma_mask = &data->dma_mask;
2341f6f9279fSSrinivas Kandagatla dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
2342f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&data->users);
234376e8e4acSAbel Vesa INIT_LIST_HEAD(&data->invoke_interrupted_mmaps);
2344f6f9279fSSrinivas Kandagatla spin_lock_init(&data->lock);
2345f6f9279fSSrinivas Kandagatla idr_init(&data->ctx_idr);
2346f6f9279fSSrinivas Kandagatla data->domain_id = domain_id;
2347f6f9279fSSrinivas Kandagatla data->rpdev = rpdev;
2348f6f9279fSSrinivas Kandagatla
234911819ed2SChristophe JAILLET err = of_platform_populate(rdev->of_node, NULL, NULL, rdev);
235011819ed2SChristophe JAILLET if (err)
235111819ed2SChristophe JAILLET goto populate_error;
235211819ed2SChristophe JAILLET
235311819ed2SChristophe JAILLET return 0;
235411819ed2SChristophe JAILLET
235511819ed2SChristophe JAILLET populate_error:
235611819ed2SChristophe JAILLET if (data->fdevice)
235711819ed2SChristophe JAILLET misc_deregister(&data->fdevice->miscdev);
235811819ed2SChristophe JAILLET if (data->secure_fdevice)
235911819ed2SChristophe JAILLET misc_deregister(&data->secure_fdevice->miscdev);
236011819ed2SChristophe JAILLET
23613abe3ab3SSrinivas Kandagatla fdev_error:
23623abe3ab3SSrinivas Kandagatla kfree(data);
23633abe3ab3SSrinivas Kandagatla return err;
2364f6f9279fSSrinivas Kandagatla }
2365f6f9279fSSrinivas Kandagatla
fastrpc_notify_users(struct fastrpc_user * user)2366c68cfb71SSrinivas Kandagatla static void fastrpc_notify_users(struct fastrpc_user *user)
2367c68cfb71SSrinivas Kandagatla {
2368c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx;
2369c68cfb71SSrinivas Kandagatla
2370c68cfb71SSrinivas Kandagatla spin_lock(&user->lock);
2371b6a06285SRichard Acayan list_for_each_entry(ctx, &user->pending, node) {
2372b6a06285SRichard Acayan ctx->retval = -EPIPE;
2373c68cfb71SSrinivas Kandagatla complete(&ctx->work);
2374b6a06285SRichard Acayan }
2375c68cfb71SSrinivas Kandagatla spin_unlock(&user->lock);
2376c68cfb71SSrinivas Kandagatla }
2377c68cfb71SSrinivas Kandagatla
fastrpc_rpmsg_remove(struct rpmsg_device * rpdev)2378f6f9279fSSrinivas Kandagatla static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
2379f6f9279fSSrinivas Kandagatla {
2380f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
238176e8e4acSAbel Vesa struct fastrpc_buf *buf, *b;
2382c68cfb71SSrinivas Kandagatla struct fastrpc_user *user;
2383977e6c8dSSrinivas Kandagatla unsigned long flags;
2384c68cfb71SSrinivas Kandagatla
238546248400SRichard Acayan /* No invocations past this point */
2386977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags);
238746248400SRichard Acayan cctx->rpdev = NULL;
2388c68cfb71SSrinivas Kandagatla list_for_each_entry(user, &cctx->users, user)
2389c68cfb71SSrinivas Kandagatla fastrpc_notify_users(user);
2390977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags);
2391f6f9279fSSrinivas Kandagatla
2392965602eaSSrinivas Kandagatla if (cctx->fdevice)
2393965602eaSSrinivas Kandagatla misc_deregister(&cctx->fdevice->miscdev);
2394965602eaSSrinivas Kandagatla
23953abe3ab3SSrinivas Kandagatla if (cctx->secure_fdevice)
23963abe3ab3SSrinivas Kandagatla misc_deregister(&cctx->secure_fdevice->miscdev);
23973abe3ab3SSrinivas Kandagatla
239876e8e4acSAbel Vesa list_for_each_entry_safe(buf, b, &cctx->invoke_interrupted_mmaps, node)
239976e8e4acSAbel Vesa list_del(&buf->node);
240076e8e4acSAbel Vesa
240108715610SAbel Vesa if (cctx->remote_heap)
240208715610SAbel Vesa fastrpc_buf_free(cctx->remote_heap);
240308715610SAbel Vesa
2404f6f9279fSSrinivas Kandagatla of_platform_depopulate(&rpdev->dev);
2405278d56f9SBjorn Andersson
2406278d56f9SBjorn Andersson fastrpc_channel_ctx_put(cctx);
2407f6f9279fSSrinivas Kandagatla }
2408f6f9279fSSrinivas Kandagatla
fastrpc_rpmsg_callback(struct rpmsg_device * rpdev,void * data,int len,void * priv,u32 addr)2409f6f9279fSSrinivas Kandagatla static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
2410f6f9279fSSrinivas Kandagatla int len, void *priv, u32 addr)
2411f6f9279fSSrinivas Kandagatla {
2412c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
2413c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_rsp *rsp = data;
2414c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx;
2415c68cfb71SSrinivas Kandagatla unsigned long flags;
2416c68cfb71SSrinivas Kandagatla unsigned long ctxid;
2417c68cfb71SSrinivas Kandagatla
2418c68cfb71SSrinivas Kandagatla if (len < sizeof(*rsp))
2419c68cfb71SSrinivas Kandagatla return -EINVAL;
2420c68cfb71SSrinivas Kandagatla
2421c68cfb71SSrinivas Kandagatla ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4);
2422c68cfb71SSrinivas Kandagatla
2423c68cfb71SSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags);
2424c68cfb71SSrinivas Kandagatla ctx = idr_find(&cctx->ctx_idr, ctxid);
2425c68cfb71SSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags);
2426c68cfb71SSrinivas Kandagatla
2427c68cfb71SSrinivas Kandagatla if (!ctx) {
2428c68cfb71SSrinivas Kandagatla dev_err(&rpdev->dev, "No context ID matches response\n");
2429c68cfb71SSrinivas Kandagatla return -ENOENT;
2430c68cfb71SSrinivas Kandagatla }
2431c68cfb71SSrinivas Kandagatla
2432c68cfb71SSrinivas Kandagatla ctx->retval = rsp->retval;
2433c68cfb71SSrinivas Kandagatla complete(&ctx->work);
24348e7389c7SThierry Escande
24358e7389c7SThierry Escande /*
24368e7389c7SThierry Escande * The DMA buffer associated with the context cannot be freed in
24378e7389c7SThierry Escande * interrupt context so schedule it through a worker thread to
24388e7389c7SThierry Escande * avoid a kernel BUG.
24398e7389c7SThierry Escande */
24408e7389c7SThierry Escande schedule_work(&ctx->put_work);
2441c68cfb71SSrinivas Kandagatla
2442f6f9279fSSrinivas Kandagatla return 0;
2443f6f9279fSSrinivas Kandagatla }
2444f6f9279fSSrinivas Kandagatla
2445f6f9279fSSrinivas Kandagatla static const struct of_device_id fastrpc_rpmsg_of_match[] = {
2446f6f9279fSSrinivas Kandagatla { .compatible = "qcom,fastrpc" },
2447f6f9279fSSrinivas Kandagatla { },
2448f6f9279fSSrinivas Kandagatla };
2449f6f9279fSSrinivas Kandagatla MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match);
2450f6f9279fSSrinivas Kandagatla
2451f6f9279fSSrinivas Kandagatla static struct rpmsg_driver fastrpc_driver = {
2452f6f9279fSSrinivas Kandagatla .probe = fastrpc_rpmsg_probe,
2453f6f9279fSSrinivas Kandagatla .remove = fastrpc_rpmsg_remove,
2454f6f9279fSSrinivas Kandagatla .callback = fastrpc_rpmsg_callback,
2455f6f9279fSSrinivas Kandagatla .drv = {
2456f6f9279fSSrinivas Kandagatla .name = "qcom,fastrpc",
2457f6f9279fSSrinivas Kandagatla .of_match_table = fastrpc_rpmsg_of_match,
2458f6f9279fSSrinivas Kandagatla },
2459f6f9279fSSrinivas Kandagatla };
2460f6f9279fSSrinivas Kandagatla
fastrpc_init(void)2461f6f9279fSSrinivas Kandagatla static int fastrpc_init(void)
2462f6f9279fSSrinivas Kandagatla {
2463f6f9279fSSrinivas Kandagatla int ret;
2464f6f9279fSSrinivas Kandagatla
2465f6f9279fSSrinivas Kandagatla ret = platform_driver_register(&fastrpc_cb_driver);
2466f6f9279fSSrinivas Kandagatla if (ret < 0) {
2467f6f9279fSSrinivas Kandagatla pr_err("fastrpc: failed to register cb driver\n");
2468f6f9279fSSrinivas Kandagatla return ret;
2469f6f9279fSSrinivas Kandagatla }
2470f6f9279fSSrinivas Kandagatla
2471f6f9279fSSrinivas Kandagatla ret = register_rpmsg_driver(&fastrpc_driver);
2472f6f9279fSSrinivas Kandagatla if (ret < 0) {
2473f6f9279fSSrinivas Kandagatla pr_err("fastrpc: failed to register rpmsg driver\n");
2474f6f9279fSSrinivas Kandagatla platform_driver_unregister(&fastrpc_cb_driver);
2475f6f9279fSSrinivas Kandagatla return ret;
2476f6f9279fSSrinivas Kandagatla }
2477f6f9279fSSrinivas Kandagatla
2478f6f9279fSSrinivas Kandagatla return 0;
2479f6f9279fSSrinivas Kandagatla }
2480f6f9279fSSrinivas Kandagatla module_init(fastrpc_init);
2481f6f9279fSSrinivas Kandagatla
fastrpc_exit(void)2482f6f9279fSSrinivas Kandagatla static void fastrpc_exit(void)
2483f6f9279fSSrinivas Kandagatla {
2484f6f9279fSSrinivas Kandagatla platform_driver_unregister(&fastrpc_cb_driver);
2485f6f9279fSSrinivas Kandagatla unregister_rpmsg_driver(&fastrpc_driver);
2486f6f9279fSSrinivas Kandagatla }
2487f6f9279fSSrinivas Kandagatla module_exit(fastrpc_exit);
2488f6f9279fSSrinivas Kandagatla
2489f6f9279fSSrinivas Kandagatla MODULE_LICENSE("GPL v2");
249016b0314aSGreg Kroah-Hartman MODULE_IMPORT_NS(DMA_BUF);
2491