1f6f9279fSSrinivas Kandagatla // SPDX-License-Identifier: GPL-2.0 2f6f9279fSSrinivas Kandagatla // Copyright (c) 2011-2018, The Linux Foundation. All rights reserved. 3f6f9279fSSrinivas Kandagatla // Copyright (c) 2018, Linaro Limited 4f6f9279fSSrinivas Kandagatla 5c68cfb71SSrinivas Kandagatla #include <linux/completion.h> 6f6f9279fSSrinivas Kandagatla #include <linux/device.h> 7c68cfb71SSrinivas Kandagatla #include <linux/dma-buf.h> 8f6f9279fSSrinivas Kandagatla #include <linux/dma-mapping.h> 9f6f9279fSSrinivas Kandagatla #include <linux/idr.h> 10f6f9279fSSrinivas Kandagatla #include <linux/list.h> 11f6f9279fSSrinivas Kandagatla #include <linux/miscdevice.h> 12f6f9279fSSrinivas Kandagatla #include <linux/module.h> 13f6f9279fSSrinivas Kandagatla #include <linux/of_address.h> 14f6f9279fSSrinivas Kandagatla #include <linux/of.h> 1525e8dfb8SSrinivas Kandagatla #include <linux/sort.h> 16f6f9279fSSrinivas Kandagatla #include <linux/of_platform.h> 17f6f9279fSSrinivas Kandagatla #include <linux/rpmsg.h> 18f6f9279fSSrinivas Kandagatla #include <linux/scatterlist.h> 19f6f9279fSSrinivas Kandagatla #include <linux/slab.h> 20e90d9119SVamsi Krishna Gattupalli #include <linux/qcom_scm.h> 21c68cfb71SSrinivas Kandagatla #include <uapi/misc/fastrpc.h> 221ce91d45SAbel Vesa #include <linux/of_reserved_mem.h> 23f6f9279fSSrinivas Kandagatla 24f6f9279fSSrinivas Kandagatla #define ADSP_DOMAIN_ID (0) 25f6f9279fSSrinivas Kandagatla #define MDSP_DOMAIN_ID (1) 26f6f9279fSSrinivas Kandagatla #define SDSP_DOMAIN_ID (2) 27f6f9279fSSrinivas Kandagatla #define CDSP_DOMAIN_ID (3) 28f6f9279fSSrinivas Kandagatla #define FASTRPC_DEV_MAX 4 /* adsp, mdsp, slpi, cdsp*/ 29689a2d9fSJohan Hovold #define FASTRPC_MAX_SESSIONS 14 30e90d9119SVamsi Krishna Gattupalli #define FASTRPC_MAX_VMIDS 16 31c68cfb71SSrinivas Kandagatla #define FASTRPC_ALIGN 128 32c68cfb71SSrinivas Kandagatla #define FASTRPC_MAX_FDLIST 16 33c68cfb71SSrinivas Kandagatla #define FASTRPC_MAX_CRCLIST 64 34c68cfb71SSrinivas Kandagatla #define FASTRPC_PHYS(p) ((p) & 0xffffffff) 35f6f9279fSSrinivas Kandagatla #define FASTRPC_CTX_MAX (256) 36d73f71c7SSrinivas Kandagatla #define FASTRPC_INIT_HANDLE 1 376c16fd8bSJeya R #define FASTRPC_DSP_UTILITIES_HANDLE 2 38f6f9279fSSrinivas Kandagatla #define FASTRPC_CTXID_MASK (0xFF0) 39efcd2390SJorge Ramirez-Ortiz #define INIT_FILELEN_MAX (2 * 1024 * 1024) 4008715610SAbel Vesa #define INIT_FILE_NAMELEN_MAX (128) 41f6f9279fSSrinivas Kandagatla #define FASTRPC_DEVICE_NAME "fastrpc" 4208715610SAbel Vesa 4308715610SAbel Vesa /* Add memory to static PD pool, protection thru XPU */ 4408715610SAbel Vesa #define ADSP_MMAP_HEAP_ADDR 4 4508715610SAbel Vesa /* MAP static DMA buffer on DSP User PD */ 4608715610SAbel Vesa #define ADSP_MMAP_DMA_BUFFER 6 4708715610SAbel Vesa /* Add memory to static PD pool protection thru hypervisor */ 4808715610SAbel Vesa #define ADSP_MMAP_REMOTE_HEAP_ADDR 8 4908715610SAbel Vesa /* Add memory to userPD pool, for user heap */ 502419e55eSJorge Ramirez-Ortiz #define ADSP_MMAP_ADD_PAGES 0x1000 5108715610SAbel Vesa /* Add memory to userPD pool, for LLC heap */ 5208715610SAbel Vesa #define ADSP_MMAP_ADD_PAGES_LLC 0x3000, 5308715610SAbel Vesa 546c16fd8bSJeya R #define DSP_UNSUPPORTED_API (0x80000414) 556c16fd8bSJeya R /* MAX NUMBER of DSP ATTRIBUTES SUPPORTED */ 566c16fd8bSJeya R #define FASTRPC_MAX_DSP_ATTRIBUTES (256) 576c16fd8bSJeya R #define FASTRPC_MAX_DSP_ATTRIBUTES_LEN (sizeof(u32) * FASTRPC_MAX_DSP_ATTRIBUTES) 58f6f9279fSSrinivas Kandagatla 59c68cfb71SSrinivas Kandagatla /* Retrives number of input buffers from the scalars parameter */ 60c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_INBUFS(sc) (((sc) >> 16) & 0x0ff) 61c68cfb71SSrinivas Kandagatla 62c68cfb71SSrinivas Kandagatla /* Retrives number of output buffers from the scalars parameter */ 63c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_OUTBUFS(sc) (((sc) >> 8) & 0x0ff) 64c68cfb71SSrinivas Kandagatla 65c68cfb71SSrinivas Kandagatla /* Retrives number of input handles from the scalars parameter */ 66c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_INHANDLES(sc) (((sc) >> 4) & 0x0f) 67c68cfb71SSrinivas Kandagatla 68c68cfb71SSrinivas Kandagatla /* Retrives number of output handles from the scalars parameter */ 69c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_OUTHANDLES(sc) ((sc) & 0x0f) 70c68cfb71SSrinivas Kandagatla 71c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_LENGTH(sc) (REMOTE_SCALARS_INBUFS(sc) + \ 72c68cfb71SSrinivas Kandagatla REMOTE_SCALARS_OUTBUFS(sc) + \ 73c68cfb71SSrinivas Kandagatla REMOTE_SCALARS_INHANDLES(sc)+ \ 74c68cfb71SSrinivas Kandagatla REMOTE_SCALARS_OUTHANDLES(sc)) 75c68cfb71SSrinivas Kandagatla #define FASTRPC_BUILD_SCALARS(attr, method, in, out, oin, oout) \ 76c68cfb71SSrinivas Kandagatla (((attr & 0x07) << 29) | \ 77c68cfb71SSrinivas Kandagatla ((method & 0x1f) << 24) | \ 78c68cfb71SSrinivas Kandagatla ((in & 0xff) << 16) | \ 79c68cfb71SSrinivas Kandagatla ((out & 0xff) << 8) | \ 80c68cfb71SSrinivas Kandagatla ((oin & 0x0f) << 4) | \ 81c68cfb71SSrinivas Kandagatla (oout & 0x0f)) 82c68cfb71SSrinivas Kandagatla 83c68cfb71SSrinivas Kandagatla #define FASTRPC_SCALARS(method, in, out) \ 84c68cfb71SSrinivas Kandagatla FASTRPC_BUILD_SCALARS(0, method, in, out, 0, 0) 85c68cfb71SSrinivas Kandagatla 86d73f71c7SSrinivas Kandagatla #define FASTRPC_CREATE_PROCESS_NARGS 6 8708715610SAbel Vesa #define FASTRPC_CREATE_STATIC_PROCESS_NARGS 3 88d73f71c7SSrinivas Kandagatla /* Remote Method id table */ 89d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_ATTACH 0 90d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_RELEASE 1 912419e55eSJorge Ramirez-Ortiz #define FASTRPC_RMID_INIT_MMAP 4 922419e55eSJorge Ramirez-Ortiz #define FASTRPC_RMID_INIT_MUNMAP 5 93d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE 6 94d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE_ATTR 7 95d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE_STATIC 8 965c1b97c7SJeya R #define FASTRPC_RMID_INIT_MEM_MAP 10 975c1b97c7SJeya R #define FASTRPC_RMID_INIT_MEM_UNMAP 11 98d73f71c7SSrinivas Kandagatla 9984195d20SJonathan Marek /* Protection Domain(PD) ids */ 1001959ab9eSAbel Vesa #define ROOT_PD (0) 10184195d20SJonathan Marek #define USER_PD (1) 10284195d20SJonathan Marek #define SENSORS_PD (2) 10384195d20SJonathan Marek 104965602eaSSrinivas Kandagatla #define miscdev_to_fdevice(d) container_of(d, struct fastrpc_device, miscdev) 105f6f9279fSSrinivas Kandagatla 106f6f9279fSSrinivas Kandagatla static const char *domains[FASTRPC_DEV_MAX] = { "adsp", "mdsp", 107f6f9279fSSrinivas Kandagatla "sdsp", "cdsp"}; 108c68cfb71SSrinivas Kandagatla struct fastrpc_phy_page { 109c68cfb71SSrinivas Kandagatla u64 addr; /* physical address */ 110c68cfb71SSrinivas Kandagatla u64 size; /* size of contiguous region */ 111c68cfb71SSrinivas Kandagatla }; 112c68cfb71SSrinivas Kandagatla 113c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_buf { 114c68cfb71SSrinivas Kandagatla u32 num; /* number of contiguous regions */ 115c68cfb71SSrinivas Kandagatla u32 pgidx; /* index to start of contiguous region */ 116c68cfb71SSrinivas Kandagatla }; 117c68cfb71SSrinivas Kandagatla 11835a82b87SVamsi Krishna Gattupalli struct fastrpc_remote_dmahandle { 11935a82b87SVamsi Krishna Gattupalli s32 fd; /* dma handle fd */ 12035a82b87SVamsi Krishna Gattupalli u32 offset; /* dma handle offset */ 12135a82b87SVamsi Krishna Gattupalli u32 len; /* dma handle length */ 12235a82b87SVamsi Krishna Gattupalli }; 12335a82b87SVamsi Krishna Gattupalli 12435a82b87SVamsi Krishna Gattupalli struct fastrpc_remote_buf { 12535a82b87SVamsi Krishna Gattupalli u64 pv; /* buffer pointer */ 12635a82b87SVamsi Krishna Gattupalli u64 len; /* length of buffer */ 12735a82b87SVamsi Krishna Gattupalli }; 12835a82b87SVamsi Krishna Gattupalli 12935a82b87SVamsi Krishna Gattupalli union fastrpc_remote_arg { 13035a82b87SVamsi Krishna Gattupalli struct fastrpc_remote_buf buf; 13135a82b87SVamsi Krishna Gattupalli struct fastrpc_remote_dmahandle dma; 132c68cfb71SSrinivas Kandagatla }; 133c68cfb71SSrinivas Kandagatla 1342419e55eSJorge Ramirez-Ortiz struct fastrpc_mmap_rsp_msg { 1352419e55eSJorge Ramirez-Ortiz u64 vaddr; 1362419e55eSJorge Ramirez-Ortiz }; 1372419e55eSJorge Ramirez-Ortiz 1382419e55eSJorge Ramirez-Ortiz struct fastrpc_mmap_req_msg { 1392419e55eSJorge Ramirez-Ortiz s32 pgid; 1402419e55eSJorge Ramirez-Ortiz u32 flags; 1412419e55eSJorge Ramirez-Ortiz u64 vaddr; 1422419e55eSJorge Ramirez-Ortiz s32 num; 1432419e55eSJorge Ramirez-Ortiz }; 1442419e55eSJorge Ramirez-Ortiz 1455c1b97c7SJeya R struct fastrpc_mem_map_req_msg { 1465c1b97c7SJeya R s32 pgid; 1475c1b97c7SJeya R s32 fd; 1485c1b97c7SJeya R s32 offset; 1495c1b97c7SJeya R u32 flags; 1505c1b97c7SJeya R u64 vaddrin; 1515c1b97c7SJeya R s32 num; 1525c1b97c7SJeya R s32 data_len; 1535c1b97c7SJeya R }; 1545c1b97c7SJeya R 1552419e55eSJorge Ramirez-Ortiz struct fastrpc_munmap_req_msg { 1562419e55eSJorge Ramirez-Ortiz s32 pgid; 1572419e55eSJorge Ramirez-Ortiz u64 vaddr; 1582419e55eSJorge Ramirez-Ortiz u64 size; 1592419e55eSJorge Ramirez-Ortiz }; 1602419e55eSJorge Ramirez-Ortiz 1615c1b97c7SJeya R struct fastrpc_mem_unmap_req_msg { 1625c1b97c7SJeya R s32 pgid; 1635c1b97c7SJeya R s32 fd; 1645c1b97c7SJeya R u64 vaddrin; 1655c1b97c7SJeya R u64 len; 1665c1b97c7SJeya R }; 1675c1b97c7SJeya R 168c68cfb71SSrinivas Kandagatla struct fastrpc_msg { 169c68cfb71SSrinivas Kandagatla int pid; /* process group id */ 170c68cfb71SSrinivas Kandagatla int tid; /* thread id */ 171c68cfb71SSrinivas Kandagatla u64 ctx; /* invoke caller context */ 172c68cfb71SSrinivas Kandagatla u32 handle; /* handle to invoke */ 173c68cfb71SSrinivas Kandagatla u32 sc; /* scalars structure describing the data */ 174c68cfb71SSrinivas Kandagatla u64 addr; /* physical address */ 175c68cfb71SSrinivas Kandagatla u64 size; /* size of contiguous region */ 176c68cfb71SSrinivas Kandagatla }; 177c68cfb71SSrinivas Kandagatla 178c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_rsp { 179c68cfb71SSrinivas Kandagatla u64 ctx; /* invoke caller context */ 180c68cfb71SSrinivas Kandagatla int retval; /* invoke return value */ 181c68cfb71SSrinivas Kandagatla }; 182c68cfb71SSrinivas Kandagatla 18325e8dfb8SSrinivas Kandagatla struct fastrpc_buf_overlap { 18425e8dfb8SSrinivas Kandagatla u64 start; 18525e8dfb8SSrinivas Kandagatla u64 end; 18625e8dfb8SSrinivas Kandagatla int raix; 18725e8dfb8SSrinivas Kandagatla u64 mstart; 18825e8dfb8SSrinivas Kandagatla u64 mend; 18925e8dfb8SSrinivas Kandagatla u64 offset; 19025e8dfb8SSrinivas Kandagatla }; 19125e8dfb8SSrinivas Kandagatla 192c68cfb71SSrinivas Kandagatla struct fastrpc_buf { 193c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl; 1946cffd795SSrinivas Kandagatla struct dma_buf *dmabuf; 195c68cfb71SSrinivas Kandagatla struct device *dev; 196c68cfb71SSrinivas Kandagatla void *virt; 197c68cfb71SSrinivas Kandagatla u64 phys; 198c68cfb71SSrinivas Kandagatla u64 size; 1996cffd795SSrinivas Kandagatla /* Lock for dma buf attachments */ 2006cffd795SSrinivas Kandagatla struct mutex lock; 2016cffd795SSrinivas Kandagatla struct list_head attachments; 2022419e55eSJorge Ramirez-Ortiz /* mmap support */ 2032419e55eSJorge Ramirez-Ortiz struct list_head node; /* list of user requested mmaps */ 2042419e55eSJorge Ramirez-Ortiz uintptr_t raddr; 2056cffd795SSrinivas Kandagatla }; 2066cffd795SSrinivas Kandagatla 2076cffd795SSrinivas Kandagatla struct fastrpc_dma_buf_attachment { 2086cffd795SSrinivas Kandagatla struct device *dev; 2096cffd795SSrinivas Kandagatla struct sg_table sgt; 2106cffd795SSrinivas Kandagatla struct list_head node; 211c68cfb71SSrinivas Kandagatla }; 212c68cfb71SSrinivas Kandagatla 213c68cfb71SSrinivas Kandagatla struct fastrpc_map { 214c68cfb71SSrinivas Kandagatla struct list_head node; 215c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl; 216c68cfb71SSrinivas Kandagatla int fd; 217c68cfb71SSrinivas Kandagatla struct dma_buf *buf; 218c68cfb71SSrinivas Kandagatla struct sg_table *table; 219c68cfb71SSrinivas Kandagatla struct dma_buf_attachment *attach; 220c68cfb71SSrinivas Kandagatla u64 phys; 221c68cfb71SSrinivas Kandagatla u64 size; 222c68cfb71SSrinivas Kandagatla void *va; 223c68cfb71SSrinivas Kandagatla u64 len; 2245c1b97c7SJeya R u64 raddr; 225e90d9119SVamsi Krishna Gattupalli u32 attr; 226c68cfb71SSrinivas Kandagatla struct kref refcount; 227c68cfb71SSrinivas Kandagatla }; 228c68cfb71SSrinivas Kandagatla 229c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx { 230c68cfb71SSrinivas Kandagatla int nscalars; 231c68cfb71SSrinivas Kandagatla int nbufs; 232c68cfb71SSrinivas Kandagatla int retval; 233c68cfb71SSrinivas Kandagatla int pid; 234c68cfb71SSrinivas Kandagatla int tgid; 235c68cfb71SSrinivas Kandagatla u32 sc; 236c68cfb71SSrinivas Kandagatla u32 *crc; 237c68cfb71SSrinivas Kandagatla u64 ctxid; 238c68cfb71SSrinivas Kandagatla u64 msg_sz; 239c68cfb71SSrinivas Kandagatla struct kref refcount; 240c68cfb71SSrinivas Kandagatla struct list_head node; /* list of ctxs */ 241c68cfb71SSrinivas Kandagatla struct completion work; 2428e7389c7SThierry Escande struct work_struct put_work; 243c68cfb71SSrinivas Kandagatla struct fastrpc_msg msg; 244c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl; 24535a82b87SVamsi Krishna Gattupalli union fastrpc_remote_arg *rpra; 246c68cfb71SSrinivas Kandagatla struct fastrpc_map **maps; 247c68cfb71SSrinivas Kandagatla struct fastrpc_buf *buf; 248c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_args *args; 24925e8dfb8SSrinivas Kandagatla struct fastrpc_buf_overlap *olaps; 250c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 251c68cfb71SSrinivas Kandagatla }; 252f6f9279fSSrinivas Kandagatla 253f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx { 254f6f9279fSSrinivas Kandagatla struct device *dev; 255f6f9279fSSrinivas Kandagatla int sid; 256f6f9279fSSrinivas Kandagatla bool used; 257f6f9279fSSrinivas Kandagatla bool valid; 258f6f9279fSSrinivas Kandagatla }; 259f6f9279fSSrinivas Kandagatla 260f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx { 261f6f9279fSSrinivas Kandagatla int domain_id; 262f6f9279fSSrinivas Kandagatla int sesscount; 263e90d9119SVamsi Krishna Gattupalli int vmcount; 264e90d9119SVamsi Krishna Gattupalli u32 perms; 265e90d9119SVamsi Krishna Gattupalli struct qcom_scm_vmperm vmperms[FASTRPC_MAX_VMIDS]; 266f6f9279fSSrinivas Kandagatla struct rpmsg_device *rpdev; 267f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS]; 268f6f9279fSSrinivas Kandagatla spinlock_t lock; 269f6f9279fSSrinivas Kandagatla struct idr ctx_idr; 270f6f9279fSSrinivas Kandagatla struct list_head users; 271278d56f9SBjorn Andersson struct kref refcount; 2726c16fd8bSJeya R /* Flag if dsp attributes are cached */ 2736c16fd8bSJeya R bool valid_attributes; 2746c16fd8bSJeya R u32 dsp_attributes[FASTRPC_MAX_DSP_ATTRIBUTES]; 2753abe3ab3SSrinivas Kandagatla struct fastrpc_device *secure_fdevice; 276965602eaSSrinivas Kandagatla struct fastrpc_device *fdevice; 27708715610SAbel Vesa struct fastrpc_buf *remote_heap; 27876e8e4acSAbel Vesa struct list_head invoke_interrupted_mmaps; 2793abe3ab3SSrinivas Kandagatla bool secure; 2807f1f4812SJeya R bool unsigned_support; 281*9bde43a0SAbel Vesa u64 dma_mask; 282965602eaSSrinivas Kandagatla }; 283965602eaSSrinivas Kandagatla 284965602eaSSrinivas Kandagatla struct fastrpc_device { 285965602eaSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 286965602eaSSrinivas Kandagatla struct miscdevice miscdev; 2873abe3ab3SSrinivas Kandagatla bool secure; 288f6f9279fSSrinivas Kandagatla }; 289f6f9279fSSrinivas Kandagatla 290f6f9279fSSrinivas Kandagatla struct fastrpc_user { 291f6f9279fSSrinivas Kandagatla struct list_head user; 292f6f9279fSSrinivas Kandagatla struct list_head maps; 293f6f9279fSSrinivas Kandagatla struct list_head pending; 2942419e55eSJorge Ramirez-Ortiz struct list_head mmaps; 295f6f9279fSSrinivas Kandagatla 296f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 297f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *sctx; 298c68cfb71SSrinivas Kandagatla struct fastrpc_buf *init_mem; 299f6f9279fSSrinivas Kandagatla 300f6f9279fSSrinivas Kandagatla int tgid; 301f6f9279fSSrinivas Kandagatla int pd; 3027f1f4812SJeya R bool is_secure_dev; 303f6f9279fSSrinivas Kandagatla /* Lock for lists */ 304f6f9279fSSrinivas Kandagatla spinlock_t lock; 305f6f9279fSSrinivas Kandagatla /* lock for allocations */ 306f6f9279fSSrinivas Kandagatla struct mutex mutex; 307f6f9279fSSrinivas Kandagatla }; 308f6f9279fSSrinivas Kandagatla 309c68cfb71SSrinivas Kandagatla static void fastrpc_free_map(struct kref *ref) 310c68cfb71SSrinivas Kandagatla { 311c68cfb71SSrinivas Kandagatla struct fastrpc_map *map; 312c68cfb71SSrinivas Kandagatla 313c68cfb71SSrinivas Kandagatla map = container_of(ref, struct fastrpc_map, refcount); 314c68cfb71SSrinivas Kandagatla 315c68cfb71SSrinivas Kandagatla if (map->table) { 316e90d9119SVamsi Krishna Gattupalli if (map->attr & FASTRPC_ATTR_SECUREMAP) { 317e90d9119SVamsi Krishna Gattupalli struct qcom_scm_vmperm perm; 318e90d9119SVamsi Krishna Gattupalli int err = 0; 319e90d9119SVamsi Krishna Gattupalli 320e90d9119SVamsi Krishna Gattupalli perm.vmid = QCOM_SCM_VMID_HLOS; 321e90d9119SVamsi Krishna Gattupalli perm.perm = QCOM_SCM_PERM_RWX; 322e90d9119SVamsi Krishna Gattupalli err = qcom_scm_assign_mem(map->phys, map->size, 323e90d9119SVamsi Krishna Gattupalli &(map->fl->cctx->vmperms[0].vmid), &perm, 1); 324e90d9119SVamsi Krishna Gattupalli if (err) { 325e90d9119SVamsi Krishna Gattupalli dev_err(map->fl->sctx->dev, "Failed to assign memory phys 0x%llx size 0x%llx err %d", 326e90d9119SVamsi Krishna Gattupalli map->phys, map->size, err); 327e90d9119SVamsi Krishna Gattupalli return; 328e90d9119SVamsi Krishna Gattupalli } 329e90d9119SVamsi Krishna Gattupalli } 330c68cfb71SSrinivas Kandagatla dma_buf_unmap_attachment(map->attach, map->table, 331c68cfb71SSrinivas Kandagatla DMA_BIDIRECTIONAL); 332c68cfb71SSrinivas Kandagatla dma_buf_detach(map->buf, map->attach); 333c68cfb71SSrinivas Kandagatla dma_buf_put(map->buf); 334c68cfb71SSrinivas Kandagatla } 335c68cfb71SSrinivas Kandagatla 336c68cfb71SSrinivas Kandagatla kfree(map); 337c68cfb71SSrinivas Kandagatla } 338c68cfb71SSrinivas Kandagatla 339c68cfb71SSrinivas Kandagatla static void fastrpc_map_put(struct fastrpc_map *map) 340c68cfb71SSrinivas Kandagatla { 341c68cfb71SSrinivas Kandagatla if (map) 342c68cfb71SSrinivas Kandagatla kref_put(&map->refcount, fastrpc_free_map); 343c68cfb71SSrinivas Kandagatla } 344c68cfb71SSrinivas Kandagatla 345c68cfb71SSrinivas Kandagatla static void fastrpc_map_get(struct fastrpc_map *map) 346c68cfb71SSrinivas Kandagatla { 347c68cfb71SSrinivas Kandagatla if (map) 348c68cfb71SSrinivas Kandagatla kref_get(&map->refcount); 349c68cfb71SSrinivas Kandagatla } 350c68cfb71SSrinivas Kandagatla 3518f6c1d8cSVamsi Krishna Gattupalli 3528f6c1d8cSVamsi Krishna Gattupalli static int fastrpc_map_lookup(struct fastrpc_user *fl, int fd, 353c68cfb71SSrinivas Kandagatla struct fastrpc_map **ppmap) 354c68cfb71SSrinivas Kandagatla { 355c68cfb71SSrinivas Kandagatla struct fastrpc_map *map = NULL; 356c68cfb71SSrinivas Kandagatla 357c68cfb71SSrinivas Kandagatla mutex_lock(&fl->mutex); 358c68cfb71SSrinivas Kandagatla list_for_each_entry(map, &fl->maps, node) { 359c68cfb71SSrinivas Kandagatla if (map->fd == fd) { 360c68cfb71SSrinivas Kandagatla *ppmap = map; 361c68cfb71SSrinivas Kandagatla mutex_unlock(&fl->mutex); 362c68cfb71SSrinivas Kandagatla return 0; 363c68cfb71SSrinivas Kandagatla } 364c68cfb71SSrinivas Kandagatla } 365c68cfb71SSrinivas Kandagatla mutex_unlock(&fl->mutex); 366c68cfb71SSrinivas Kandagatla 367c68cfb71SSrinivas Kandagatla return -ENOENT; 368c68cfb71SSrinivas Kandagatla } 369c68cfb71SSrinivas Kandagatla 3708f6c1d8cSVamsi Krishna Gattupalli static int fastrpc_map_find(struct fastrpc_user *fl, int fd, 3718f6c1d8cSVamsi Krishna Gattupalli struct fastrpc_map **ppmap) 3728f6c1d8cSVamsi Krishna Gattupalli { 3738f6c1d8cSVamsi Krishna Gattupalli int ret = fastrpc_map_lookup(fl, fd, ppmap); 3748f6c1d8cSVamsi Krishna Gattupalli 3758f6c1d8cSVamsi Krishna Gattupalli if (!ret) 3768f6c1d8cSVamsi Krishna Gattupalli fastrpc_map_get(*ppmap); 3778f6c1d8cSVamsi Krishna Gattupalli 3788f6c1d8cSVamsi Krishna Gattupalli return ret; 3798f6c1d8cSVamsi Krishna Gattupalli } 3808f6c1d8cSVamsi Krishna Gattupalli 381c68cfb71SSrinivas Kandagatla static void fastrpc_buf_free(struct fastrpc_buf *buf) 382c68cfb71SSrinivas Kandagatla { 383c68cfb71SSrinivas Kandagatla dma_free_coherent(buf->dev, buf->size, buf->virt, 384c68cfb71SSrinivas Kandagatla FASTRPC_PHYS(buf->phys)); 385c68cfb71SSrinivas Kandagatla kfree(buf); 386c68cfb71SSrinivas Kandagatla } 387c68cfb71SSrinivas Kandagatla 3886f18c7e8SAbel Vesa static int __fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev, 389c68cfb71SSrinivas Kandagatla u64 size, struct fastrpc_buf **obuf) 390c68cfb71SSrinivas Kandagatla { 391c68cfb71SSrinivas Kandagatla struct fastrpc_buf *buf; 392c68cfb71SSrinivas Kandagatla 393c68cfb71SSrinivas Kandagatla buf = kzalloc(sizeof(*buf), GFP_KERNEL); 394c68cfb71SSrinivas Kandagatla if (!buf) 395c68cfb71SSrinivas Kandagatla return -ENOMEM; 396c68cfb71SSrinivas Kandagatla 3976cffd795SSrinivas Kandagatla INIT_LIST_HEAD(&buf->attachments); 3982419e55eSJorge Ramirez-Ortiz INIT_LIST_HEAD(&buf->node); 3996cffd795SSrinivas Kandagatla mutex_init(&buf->lock); 4006cffd795SSrinivas Kandagatla 401c68cfb71SSrinivas Kandagatla buf->fl = fl; 402c68cfb71SSrinivas Kandagatla buf->virt = NULL; 403c68cfb71SSrinivas Kandagatla buf->phys = 0; 404c68cfb71SSrinivas Kandagatla buf->size = size; 405c68cfb71SSrinivas Kandagatla buf->dev = dev; 4062419e55eSJorge Ramirez-Ortiz buf->raddr = 0; 407c68cfb71SSrinivas Kandagatla 408c68cfb71SSrinivas Kandagatla buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys, 409c68cfb71SSrinivas Kandagatla GFP_KERNEL); 41041db5f83SJorge Ramirez-Ortiz if (!buf->virt) { 41141db5f83SJorge Ramirez-Ortiz mutex_destroy(&buf->lock); 41241db5f83SJorge Ramirez-Ortiz kfree(buf); 413c68cfb71SSrinivas Kandagatla return -ENOMEM; 41441db5f83SJorge Ramirez-Ortiz } 415c68cfb71SSrinivas Kandagatla 416c68cfb71SSrinivas Kandagatla *obuf = buf; 417c68cfb71SSrinivas Kandagatla 418c68cfb71SSrinivas Kandagatla return 0; 419c68cfb71SSrinivas Kandagatla } 420c68cfb71SSrinivas Kandagatla 4216f18c7e8SAbel Vesa static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev, 4226f18c7e8SAbel Vesa u64 size, struct fastrpc_buf **obuf) 4236f18c7e8SAbel Vesa { 4246f18c7e8SAbel Vesa int ret; 4256f18c7e8SAbel Vesa struct fastrpc_buf *buf; 4266f18c7e8SAbel Vesa 4276f18c7e8SAbel Vesa ret = __fastrpc_buf_alloc(fl, dev, size, obuf); 4286f18c7e8SAbel Vesa if (ret) 4296f18c7e8SAbel Vesa return ret; 4306f18c7e8SAbel Vesa 4316f18c7e8SAbel Vesa buf = *obuf; 4326f18c7e8SAbel Vesa 4336f18c7e8SAbel Vesa if (fl->sctx && fl->sctx->sid) 4346f18c7e8SAbel Vesa buf->phys += ((u64)fl->sctx->sid << 32); 4356f18c7e8SAbel Vesa 4366f18c7e8SAbel Vesa return 0; 4376f18c7e8SAbel Vesa } 4386f18c7e8SAbel Vesa 4396f18c7e8SAbel Vesa static int fastrpc_remote_heap_alloc(struct fastrpc_user *fl, struct device *dev, 4406f18c7e8SAbel Vesa u64 size, struct fastrpc_buf **obuf) 4416f18c7e8SAbel Vesa { 4426f18c7e8SAbel Vesa struct device *rdev = &fl->cctx->rpdev->dev; 4436f18c7e8SAbel Vesa 4446f18c7e8SAbel Vesa return __fastrpc_buf_alloc(fl, rdev, size, obuf); 4456f18c7e8SAbel Vesa } 4466f18c7e8SAbel Vesa 447278d56f9SBjorn Andersson static void fastrpc_channel_ctx_free(struct kref *ref) 448278d56f9SBjorn Andersson { 449278d56f9SBjorn Andersson struct fastrpc_channel_ctx *cctx; 450278d56f9SBjorn Andersson 451278d56f9SBjorn Andersson cctx = container_of(ref, struct fastrpc_channel_ctx, refcount); 452278d56f9SBjorn Andersson 453278d56f9SBjorn Andersson kfree(cctx); 454278d56f9SBjorn Andersson } 455278d56f9SBjorn Andersson 456278d56f9SBjorn Andersson static void fastrpc_channel_ctx_get(struct fastrpc_channel_ctx *cctx) 457278d56f9SBjorn Andersson { 458278d56f9SBjorn Andersson kref_get(&cctx->refcount); 459278d56f9SBjorn Andersson } 460278d56f9SBjorn Andersson 461278d56f9SBjorn Andersson static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx) 462278d56f9SBjorn Andersson { 463278d56f9SBjorn Andersson kref_put(&cctx->refcount, fastrpc_channel_ctx_free); 464278d56f9SBjorn Andersson } 465278d56f9SBjorn Andersson 466c68cfb71SSrinivas Kandagatla static void fastrpc_context_free(struct kref *ref) 467c68cfb71SSrinivas Kandagatla { 468c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx; 469c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 470977e6c8dSSrinivas Kandagatla unsigned long flags; 471c68cfb71SSrinivas Kandagatla int i; 472c68cfb71SSrinivas Kandagatla 473c68cfb71SSrinivas Kandagatla ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount); 474c68cfb71SSrinivas Kandagatla cctx = ctx->cctx; 475c68cfb71SSrinivas Kandagatla 4768f6c1d8cSVamsi Krishna Gattupalli for (i = 0; i < ctx->nbufs; i++) 477c68cfb71SSrinivas Kandagatla fastrpc_map_put(ctx->maps[i]); 478c68cfb71SSrinivas Kandagatla 479c68cfb71SSrinivas Kandagatla if (ctx->buf) 480c68cfb71SSrinivas Kandagatla fastrpc_buf_free(ctx->buf); 481c68cfb71SSrinivas Kandagatla 482977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 483c68cfb71SSrinivas Kandagatla idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4); 484977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 485c68cfb71SSrinivas Kandagatla 486c68cfb71SSrinivas Kandagatla kfree(ctx->maps); 48725e8dfb8SSrinivas Kandagatla kfree(ctx->olaps); 488c68cfb71SSrinivas Kandagatla kfree(ctx); 489278d56f9SBjorn Andersson 490278d56f9SBjorn Andersson fastrpc_channel_ctx_put(cctx); 491c68cfb71SSrinivas Kandagatla } 492c68cfb71SSrinivas Kandagatla 493c68cfb71SSrinivas Kandagatla static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx) 494c68cfb71SSrinivas Kandagatla { 495c68cfb71SSrinivas Kandagatla kref_get(&ctx->refcount); 496c68cfb71SSrinivas Kandagatla } 497c68cfb71SSrinivas Kandagatla 498c68cfb71SSrinivas Kandagatla static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx) 499c68cfb71SSrinivas Kandagatla { 500c68cfb71SSrinivas Kandagatla kref_put(&ctx->refcount, fastrpc_context_free); 501c68cfb71SSrinivas Kandagatla } 502c68cfb71SSrinivas Kandagatla 5038e7389c7SThierry Escande static void fastrpc_context_put_wq(struct work_struct *work) 5048e7389c7SThierry Escande { 5058e7389c7SThierry Escande struct fastrpc_invoke_ctx *ctx = 5068e7389c7SThierry Escande container_of(work, struct fastrpc_invoke_ctx, put_work); 5078e7389c7SThierry Escande 5088e7389c7SThierry Escande fastrpc_context_put(ctx); 5098e7389c7SThierry Escande } 5108e7389c7SThierry Escande 51125e8dfb8SSrinivas Kandagatla #define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1) 51225e8dfb8SSrinivas Kandagatla static int olaps_cmp(const void *a, const void *b) 51325e8dfb8SSrinivas Kandagatla { 51425e8dfb8SSrinivas Kandagatla struct fastrpc_buf_overlap *pa = (struct fastrpc_buf_overlap *)a; 51525e8dfb8SSrinivas Kandagatla struct fastrpc_buf_overlap *pb = (struct fastrpc_buf_overlap *)b; 51625e8dfb8SSrinivas Kandagatla /* sort with lowest starting buffer first */ 51725e8dfb8SSrinivas Kandagatla int st = CMP(pa->start, pb->start); 51825e8dfb8SSrinivas Kandagatla /* sort with highest ending buffer first */ 51925e8dfb8SSrinivas Kandagatla int ed = CMP(pb->end, pa->end); 52025e8dfb8SSrinivas Kandagatla 52125e8dfb8SSrinivas Kandagatla return st == 0 ? ed : st; 52225e8dfb8SSrinivas Kandagatla } 52325e8dfb8SSrinivas Kandagatla 52425e8dfb8SSrinivas Kandagatla static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx) 52525e8dfb8SSrinivas Kandagatla { 52625e8dfb8SSrinivas Kandagatla u64 max_end = 0; 52725e8dfb8SSrinivas Kandagatla int i; 52825e8dfb8SSrinivas Kandagatla 52925e8dfb8SSrinivas Kandagatla for (i = 0; i < ctx->nbufs; ++i) { 53025e8dfb8SSrinivas Kandagatla ctx->olaps[i].start = ctx->args[i].ptr; 53125e8dfb8SSrinivas Kandagatla ctx->olaps[i].end = ctx->olaps[i].start + ctx->args[i].length; 53225e8dfb8SSrinivas Kandagatla ctx->olaps[i].raix = i; 53325e8dfb8SSrinivas Kandagatla } 53425e8dfb8SSrinivas Kandagatla 53525e8dfb8SSrinivas Kandagatla sort(ctx->olaps, ctx->nbufs, sizeof(*ctx->olaps), olaps_cmp, NULL); 53625e8dfb8SSrinivas Kandagatla 53725e8dfb8SSrinivas Kandagatla for (i = 0; i < ctx->nbufs; ++i) { 53825e8dfb8SSrinivas Kandagatla /* Falling inside previous range */ 53925e8dfb8SSrinivas Kandagatla if (ctx->olaps[i].start < max_end) { 54025e8dfb8SSrinivas Kandagatla ctx->olaps[i].mstart = max_end; 54125e8dfb8SSrinivas Kandagatla ctx->olaps[i].mend = ctx->olaps[i].end; 54225e8dfb8SSrinivas Kandagatla ctx->olaps[i].offset = max_end - ctx->olaps[i].start; 54325e8dfb8SSrinivas Kandagatla 54425e8dfb8SSrinivas Kandagatla if (ctx->olaps[i].end > max_end) { 54525e8dfb8SSrinivas Kandagatla max_end = ctx->olaps[i].end; 54625e8dfb8SSrinivas Kandagatla } else { 54725e8dfb8SSrinivas Kandagatla ctx->olaps[i].mend = 0; 54825e8dfb8SSrinivas Kandagatla ctx->olaps[i].mstart = 0; 54925e8dfb8SSrinivas Kandagatla } 55025e8dfb8SSrinivas Kandagatla 55125e8dfb8SSrinivas Kandagatla } else { 55225e8dfb8SSrinivas Kandagatla ctx->olaps[i].mend = ctx->olaps[i].end; 55325e8dfb8SSrinivas Kandagatla ctx->olaps[i].mstart = ctx->olaps[i].start; 55425e8dfb8SSrinivas Kandagatla ctx->olaps[i].offset = 0; 55525e8dfb8SSrinivas Kandagatla max_end = ctx->olaps[i].end; 55625e8dfb8SSrinivas Kandagatla } 55725e8dfb8SSrinivas Kandagatla } 55825e8dfb8SSrinivas Kandagatla } 55925e8dfb8SSrinivas Kandagatla 560c68cfb71SSrinivas Kandagatla static struct fastrpc_invoke_ctx *fastrpc_context_alloc( 561c68cfb71SSrinivas Kandagatla struct fastrpc_user *user, u32 kernel, u32 sc, 562c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_args *args) 563c68cfb71SSrinivas Kandagatla { 564c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = user->cctx; 565c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx = NULL; 566977e6c8dSSrinivas Kandagatla unsigned long flags; 567c68cfb71SSrinivas Kandagatla int ret; 568c68cfb71SSrinivas Kandagatla 569c68cfb71SSrinivas Kandagatla ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 570c68cfb71SSrinivas Kandagatla if (!ctx) 571c68cfb71SSrinivas Kandagatla return ERR_PTR(-ENOMEM); 572c68cfb71SSrinivas Kandagatla 573c68cfb71SSrinivas Kandagatla INIT_LIST_HEAD(&ctx->node); 574c68cfb71SSrinivas Kandagatla ctx->fl = user; 575c68cfb71SSrinivas Kandagatla ctx->nscalars = REMOTE_SCALARS_LENGTH(sc); 576c68cfb71SSrinivas Kandagatla ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) + 577c68cfb71SSrinivas Kandagatla REMOTE_SCALARS_OUTBUFS(sc); 578c68cfb71SSrinivas Kandagatla 579c68cfb71SSrinivas Kandagatla if (ctx->nscalars) { 580c68cfb71SSrinivas Kandagatla ctx->maps = kcalloc(ctx->nscalars, 581c68cfb71SSrinivas Kandagatla sizeof(*ctx->maps), GFP_KERNEL); 582c68cfb71SSrinivas Kandagatla if (!ctx->maps) { 583c68cfb71SSrinivas Kandagatla kfree(ctx); 584c68cfb71SSrinivas Kandagatla return ERR_PTR(-ENOMEM); 585c68cfb71SSrinivas Kandagatla } 58625e8dfb8SSrinivas Kandagatla ctx->olaps = kcalloc(ctx->nscalars, 58725e8dfb8SSrinivas Kandagatla sizeof(*ctx->olaps), GFP_KERNEL); 58825e8dfb8SSrinivas Kandagatla if (!ctx->olaps) { 58925e8dfb8SSrinivas Kandagatla kfree(ctx->maps); 59025e8dfb8SSrinivas Kandagatla kfree(ctx); 59125e8dfb8SSrinivas Kandagatla return ERR_PTR(-ENOMEM); 59225e8dfb8SSrinivas Kandagatla } 593c68cfb71SSrinivas Kandagatla ctx->args = args; 59425e8dfb8SSrinivas Kandagatla fastrpc_get_buff_overlaps(ctx); 595c68cfb71SSrinivas Kandagatla } 596c68cfb71SSrinivas Kandagatla 597278d56f9SBjorn Andersson /* Released in fastrpc_context_put() */ 598278d56f9SBjorn Andersson fastrpc_channel_ctx_get(cctx); 599278d56f9SBjorn Andersson 600c68cfb71SSrinivas Kandagatla ctx->sc = sc; 601c68cfb71SSrinivas Kandagatla ctx->retval = -1; 602c68cfb71SSrinivas Kandagatla ctx->pid = current->pid; 603c68cfb71SSrinivas Kandagatla ctx->tgid = user->tgid; 604c68cfb71SSrinivas Kandagatla ctx->cctx = cctx; 605c68cfb71SSrinivas Kandagatla init_completion(&ctx->work); 6068e7389c7SThierry Escande INIT_WORK(&ctx->put_work, fastrpc_context_put_wq); 607c68cfb71SSrinivas Kandagatla 608c68cfb71SSrinivas Kandagatla spin_lock(&user->lock); 609c68cfb71SSrinivas Kandagatla list_add_tail(&ctx->node, &user->pending); 610c68cfb71SSrinivas Kandagatla spin_unlock(&user->lock); 611c68cfb71SSrinivas Kandagatla 612977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 613c68cfb71SSrinivas Kandagatla ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1, 614c68cfb71SSrinivas Kandagatla FASTRPC_CTX_MAX, GFP_ATOMIC); 615c68cfb71SSrinivas Kandagatla if (ret < 0) { 616977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 617c68cfb71SSrinivas Kandagatla goto err_idr; 618c68cfb71SSrinivas Kandagatla } 619c68cfb71SSrinivas Kandagatla ctx->ctxid = ret << 4; 620977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 621c68cfb71SSrinivas Kandagatla 622c68cfb71SSrinivas Kandagatla kref_init(&ctx->refcount); 623c68cfb71SSrinivas Kandagatla 624c68cfb71SSrinivas Kandagatla return ctx; 625c68cfb71SSrinivas Kandagatla err_idr: 626c68cfb71SSrinivas Kandagatla spin_lock(&user->lock); 627c68cfb71SSrinivas Kandagatla list_del(&ctx->node); 628c68cfb71SSrinivas Kandagatla spin_unlock(&user->lock); 629278d56f9SBjorn Andersson fastrpc_channel_ctx_put(cctx); 630c68cfb71SSrinivas Kandagatla kfree(ctx->maps); 63125e8dfb8SSrinivas Kandagatla kfree(ctx->olaps); 632c68cfb71SSrinivas Kandagatla kfree(ctx); 633c68cfb71SSrinivas Kandagatla 634c68cfb71SSrinivas Kandagatla return ERR_PTR(ret); 635c68cfb71SSrinivas Kandagatla } 636c68cfb71SSrinivas Kandagatla 6376cffd795SSrinivas Kandagatla static struct sg_table * 6386cffd795SSrinivas Kandagatla fastrpc_map_dma_buf(struct dma_buf_attachment *attachment, 6396cffd795SSrinivas Kandagatla enum dma_data_direction dir) 6406cffd795SSrinivas Kandagatla { 6416cffd795SSrinivas Kandagatla struct fastrpc_dma_buf_attachment *a = attachment->priv; 6426cffd795SSrinivas Kandagatla struct sg_table *table; 643b212658aSJonathan Marek int ret; 6446cffd795SSrinivas Kandagatla 6456cffd795SSrinivas Kandagatla table = &a->sgt; 6466cffd795SSrinivas Kandagatla 647b212658aSJonathan Marek ret = dma_map_sgtable(attachment->dev, table, dir, 0); 648b212658aSJonathan Marek if (ret) 649b212658aSJonathan Marek table = ERR_PTR(ret); 6506cffd795SSrinivas Kandagatla return table; 6516cffd795SSrinivas Kandagatla } 6526cffd795SSrinivas Kandagatla 6536cffd795SSrinivas Kandagatla static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach, 6546cffd795SSrinivas Kandagatla struct sg_table *table, 6556cffd795SSrinivas Kandagatla enum dma_data_direction dir) 6566cffd795SSrinivas Kandagatla { 6577cd7edb8SMarek Szyprowski dma_unmap_sgtable(attach->dev, table, dir, 0); 6586cffd795SSrinivas Kandagatla } 6596cffd795SSrinivas Kandagatla 6606cffd795SSrinivas Kandagatla static void fastrpc_release(struct dma_buf *dmabuf) 6616cffd795SSrinivas Kandagatla { 6626cffd795SSrinivas Kandagatla struct fastrpc_buf *buffer = dmabuf->priv; 6636cffd795SSrinivas Kandagatla 6646cffd795SSrinivas Kandagatla fastrpc_buf_free(buffer); 6656cffd795SSrinivas Kandagatla } 6666cffd795SSrinivas Kandagatla 6676cffd795SSrinivas Kandagatla static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf, 6686cffd795SSrinivas Kandagatla struct dma_buf_attachment *attachment) 6696cffd795SSrinivas Kandagatla { 6706cffd795SSrinivas Kandagatla struct fastrpc_dma_buf_attachment *a; 6716cffd795SSrinivas Kandagatla struct fastrpc_buf *buffer = dmabuf->priv; 6726cffd795SSrinivas Kandagatla int ret; 6736cffd795SSrinivas Kandagatla 6746cffd795SSrinivas Kandagatla a = kzalloc(sizeof(*a), GFP_KERNEL); 6756cffd795SSrinivas Kandagatla if (!a) 6766cffd795SSrinivas Kandagatla return -ENOMEM; 6776cffd795SSrinivas Kandagatla 6786cffd795SSrinivas Kandagatla ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt, 6796cffd795SSrinivas Kandagatla FASTRPC_PHYS(buffer->phys), buffer->size); 6806cffd795SSrinivas Kandagatla if (ret < 0) { 6816cffd795SSrinivas Kandagatla dev_err(buffer->dev, "failed to get scatterlist from DMA API\n"); 682fc739a05SNavid Emamdoost kfree(a); 6836cffd795SSrinivas Kandagatla return -EINVAL; 6846cffd795SSrinivas Kandagatla } 6856cffd795SSrinivas Kandagatla 6866cffd795SSrinivas Kandagatla a->dev = attachment->dev; 6876cffd795SSrinivas Kandagatla INIT_LIST_HEAD(&a->node); 6886cffd795SSrinivas Kandagatla attachment->priv = a; 6896cffd795SSrinivas Kandagatla 6906cffd795SSrinivas Kandagatla mutex_lock(&buffer->lock); 6916cffd795SSrinivas Kandagatla list_add(&a->node, &buffer->attachments); 6926cffd795SSrinivas Kandagatla mutex_unlock(&buffer->lock); 6936cffd795SSrinivas Kandagatla 6946cffd795SSrinivas Kandagatla return 0; 6956cffd795SSrinivas Kandagatla } 6966cffd795SSrinivas Kandagatla 6976cffd795SSrinivas Kandagatla static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf, 6986cffd795SSrinivas Kandagatla struct dma_buf_attachment *attachment) 6996cffd795SSrinivas Kandagatla { 7006cffd795SSrinivas Kandagatla struct fastrpc_dma_buf_attachment *a = attachment->priv; 7016cffd795SSrinivas Kandagatla struct fastrpc_buf *buffer = dmabuf->priv; 7026cffd795SSrinivas Kandagatla 7036cffd795SSrinivas Kandagatla mutex_lock(&buffer->lock); 7046cffd795SSrinivas Kandagatla list_del(&a->node); 7056cffd795SSrinivas Kandagatla mutex_unlock(&buffer->lock); 706cf61860eSSrinivas Kandagatla sg_free_table(&a->sgt); 7076cffd795SSrinivas Kandagatla kfree(a); 7086cffd795SSrinivas Kandagatla } 7096cffd795SSrinivas Kandagatla 7107938f421SLucas De Marchi static int fastrpc_vmap(struct dma_buf *dmabuf, struct iosys_map *map) 7116cffd795SSrinivas Kandagatla { 7126cffd795SSrinivas Kandagatla struct fastrpc_buf *buf = dmabuf->priv; 7136cffd795SSrinivas Kandagatla 7147938f421SLucas De Marchi iosys_map_set_vaddr(map, buf->virt); 7156619ccf1SThomas Zimmermann 7166619ccf1SThomas Zimmermann return 0; 7176cffd795SSrinivas Kandagatla } 7186cffd795SSrinivas Kandagatla 7196cffd795SSrinivas Kandagatla static int fastrpc_mmap(struct dma_buf *dmabuf, 7206cffd795SSrinivas Kandagatla struct vm_area_struct *vma) 7216cffd795SSrinivas Kandagatla { 7226cffd795SSrinivas Kandagatla struct fastrpc_buf *buf = dmabuf->priv; 7236cffd795SSrinivas Kandagatla size_t size = vma->vm_end - vma->vm_start; 7246cffd795SSrinivas Kandagatla 7256cffd795SSrinivas Kandagatla return dma_mmap_coherent(buf->dev, vma, buf->virt, 7266cffd795SSrinivas Kandagatla FASTRPC_PHYS(buf->phys), size); 7276cffd795SSrinivas Kandagatla } 7286cffd795SSrinivas Kandagatla 7296cffd795SSrinivas Kandagatla static const struct dma_buf_ops fastrpc_dma_buf_ops = { 7306cffd795SSrinivas Kandagatla .attach = fastrpc_dma_buf_attach, 7316cffd795SSrinivas Kandagatla .detach = fastrpc_dma_buf_detatch, 7326cffd795SSrinivas Kandagatla .map_dma_buf = fastrpc_map_dma_buf, 7336cffd795SSrinivas Kandagatla .unmap_dma_buf = fastrpc_unmap_dma_buf, 7346cffd795SSrinivas Kandagatla .mmap = fastrpc_mmap, 7356cffd795SSrinivas Kandagatla .vmap = fastrpc_vmap, 7366cffd795SSrinivas Kandagatla .release = fastrpc_release, 7376cffd795SSrinivas Kandagatla }; 7386cffd795SSrinivas Kandagatla 739c68cfb71SSrinivas Kandagatla static int fastrpc_map_create(struct fastrpc_user *fl, int fd, 740e90d9119SVamsi Krishna Gattupalli u64 len, u32 attr, struct fastrpc_map **ppmap) 741c68cfb71SSrinivas Kandagatla { 742c68cfb71SSrinivas Kandagatla struct fastrpc_session_ctx *sess = fl->sctx; 743c68cfb71SSrinivas Kandagatla struct fastrpc_map *map = NULL; 744c68cfb71SSrinivas Kandagatla int err = 0; 745c68cfb71SSrinivas Kandagatla 746c68cfb71SSrinivas Kandagatla if (!fastrpc_map_find(fl, fd, ppmap)) 747c68cfb71SSrinivas Kandagatla return 0; 748c68cfb71SSrinivas Kandagatla 749c68cfb71SSrinivas Kandagatla map = kzalloc(sizeof(*map), GFP_KERNEL); 750c68cfb71SSrinivas Kandagatla if (!map) 751c68cfb71SSrinivas Kandagatla return -ENOMEM; 752c68cfb71SSrinivas Kandagatla 753c68cfb71SSrinivas Kandagatla INIT_LIST_HEAD(&map->node); 754334f1a1cSAbel Vesa kref_init(&map->refcount); 755334f1a1cSAbel Vesa 756c68cfb71SSrinivas Kandagatla map->fl = fl; 757c68cfb71SSrinivas Kandagatla map->fd = fd; 758c68cfb71SSrinivas Kandagatla map->buf = dma_buf_get(fd); 759682a6044SWei Yongjun if (IS_ERR(map->buf)) { 760682a6044SWei Yongjun err = PTR_ERR(map->buf); 761c68cfb71SSrinivas Kandagatla goto get_err; 762c68cfb71SSrinivas Kandagatla } 763c68cfb71SSrinivas Kandagatla 764c68cfb71SSrinivas Kandagatla map->attach = dma_buf_attach(map->buf, sess->dev); 765c68cfb71SSrinivas Kandagatla if (IS_ERR(map->attach)) { 766c68cfb71SSrinivas Kandagatla dev_err(sess->dev, "Failed to attach dmabuf\n"); 767c68cfb71SSrinivas Kandagatla err = PTR_ERR(map->attach); 768c68cfb71SSrinivas Kandagatla goto attach_err; 769c68cfb71SSrinivas Kandagatla } 770c68cfb71SSrinivas Kandagatla 771c68cfb71SSrinivas Kandagatla map->table = dma_buf_map_attachment(map->attach, DMA_BIDIRECTIONAL); 772c68cfb71SSrinivas Kandagatla if (IS_ERR(map->table)) { 773c68cfb71SSrinivas Kandagatla err = PTR_ERR(map->table); 774c68cfb71SSrinivas Kandagatla goto map_err; 775c68cfb71SSrinivas Kandagatla } 776c68cfb71SSrinivas Kandagatla 777c68cfb71SSrinivas Kandagatla map->phys = sg_dma_address(map->table->sgl); 778c68cfb71SSrinivas Kandagatla map->phys += ((u64)fl->sctx->sid << 32); 779c68cfb71SSrinivas Kandagatla map->size = len; 780c68cfb71SSrinivas Kandagatla map->va = sg_virt(map->table->sgl); 781c68cfb71SSrinivas Kandagatla map->len = len; 782c68cfb71SSrinivas Kandagatla 783e90d9119SVamsi Krishna Gattupalli if (attr & FASTRPC_ATTR_SECUREMAP) { 784e90d9119SVamsi Krishna Gattupalli /* 785e90d9119SVamsi Krishna Gattupalli * If subsystem VMIDs are defined in DTSI, then do 786e90d9119SVamsi Krishna Gattupalli * hyp_assign from HLOS to those VM(s) 787e90d9119SVamsi Krishna Gattupalli */ 788e90d9119SVamsi Krishna Gattupalli unsigned int perms = BIT(QCOM_SCM_VMID_HLOS); 789e90d9119SVamsi Krishna Gattupalli 790e90d9119SVamsi Krishna Gattupalli map->attr = attr; 791e90d9119SVamsi Krishna Gattupalli err = qcom_scm_assign_mem(map->phys, (u64)map->size, &perms, 792e90d9119SVamsi Krishna Gattupalli fl->cctx->vmperms, fl->cctx->vmcount); 793e90d9119SVamsi Krishna Gattupalli if (err) { 794e90d9119SVamsi Krishna Gattupalli dev_err(sess->dev, "Failed to assign memory with phys 0x%llx size 0x%llx err %d", 795e90d9119SVamsi Krishna Gattupalli map->phys, map->size, err); 796e90d9119SVamsi Krishna Gattupalli goto map_err; 797e90d9119SVamsi Krishna Gattupalli } 798e90d9119SVamsi Krishna Gattupalli } 799c68cfb71SSrinivas Kandagatla spin_lock(&fl->lock); 800c68cfb71SSrinivas Kandagatla list_add_tail(&map->node, &fl->maps); 801c68cfb71SSrinivas Kandagatla spin_unlock(&fl->lock); 802c68cfb71SSrinivas Kandagatla *ppmap = map; 803c68cfb71SSrinivas Kandagatla 804c68cfb71SSrinivas Kandagatla return 0; 805c68cfb71SSrinivas Kandagatla 806c68cfb71SSrinivas Kandagatla map_err: 807c68cfb71SSrinivas Kandagatla dma_buf_detach(map->buf, map->attach); 808c68cfb71SSrinivas Kandagatla attach_err: 809c68cfb71SSrinivas Kandagatla dma_buf_put(map->buf); 810c68cfb71SSrinivas Kandagatla get_err: 811334f1a1cSAbel Vesa fastrpc_map_put(map); 812c68cfb71SSrinivas Kandagatla 813c68cfb71SSrinivas Kandagatla return err; 814c68cfb71SSrinivas Kandagatla } 815c68cfb71SSrinivas Kandagatla 816c68cfb71SSrinivas Kandagatla /* 817c68cfb71SSrinivas Kandagatla * Fastrpc payload buffer with metadata looks like: 818c68cfb71SSrinivas Kandagatla * 819c68cfb71SSrinivas Kandagatla * >>>>>> START of METADATA <<<<<<<<< 820c68cfb71SSrinivas Kandagatla * +---------------------------------+ 821c68cfb71SSrinivas Kandagatla * | Arguments | 82235a82b87SVamsi Krishna Gattupalli * | type:(union fastrpc_remote_arg)| 823c68cfb71SSrinivas Kandagatla * | (0 - N) | 824c68cfb71SSrinivas Kandagatla * +---------------------------------+ 825c68cfb71SSrinivas Kandagatla * | Invoke Buffer list | 826c68cfb71SSrinivas Kandagatla * | type:(struct fastrpc_invoke_buf)| 827c68cfb71SSrinivas Kandagatla * | (0 - N) | 828c68cfb71SSrinivas Kandagatla * +---------------------------------+ 829c68cfb71SSrinivas Kandagatla * | Page info list | 830c68cfb71SSrinivas Kandagatla * | type:(struct fastrpc_phy_page) | 831c68cfb71SSrinivas Kandagatla * | (0 - N) | 832c68cfb71SSrinivas Kandagatla * +---------------------------------+ 833c68cfb71SSrinivas Kandagatla * | Optional info | 834c68cfb71SSrinivas Kandagatla * |(can be specific to SoC/Firmware)| 835c68cfb71SSrinivas Kandagatla * +---------------------------------+ 836c68cfb71SSrinivas Kandagatla * >>>>>>>> END of METADATA <<<<<<<<< 837c68cfb71SSrinivas Kandagatla * +---------------------------------+ 838c68cfb71SSrinivas Kandagatla * | Inline ARGS | 839c68cfb71SSrinivas Kandagatla * | (0-N) | 840c68cfb71SSrinivas Kandagatla * +---------------------------------+ 841c68cfb71SSrinivas Kandagatla */ 842c68cfb71SSrinivas Kandagatla 843c68cfb71SSrinivas Kandagatla static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx) 844c68cfb71SSrinivas Kandagatla { 845c68cfb71SSrinivas Kandagatla int size = 0; 846c68cfb71SSrinivas Kandagatla 84735a82b87SVamsi Krishna Gattupalli size = (sizeof(struct fastrpc_remote_buf) + 848c68cfb71SSrinivas Kandagatla sizeof(struct fastrpc_invoke_buf) + 849c68cfb71SSrinivas Kandagatla sizeof(struct fastrpc_phy_page)) * ctx->nscalars + 850c68cfb71SSrinivas Kandagatla sizeof(u64) * FASTRPC_MAX_FDLIST + 851c68cfb71SSrinivas Kandagatla sizeof(u32) * FASTRPC_MAX_CRCLIST; 852c68cfb71SSrinivas Kandagatla 853c68cfb71SSrinivas Kandagatla return size; 854c68cfb71SSrinivas Kandagatla } 855c68cfb71SSrinivas Kandagatla 856c68cfb71SSrinivas Kandagatla static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen) 857c68cfb71SSrinivas Kandagatla { 858c68cfb71SSrinivas Kandagatla u64 size = 0; 8593a1bf591SJeya R int oix; 860c68cfb71SSrinivas Kandagatla 861c68cfb71SSrinivas Kandagatla size = ALIGN(metalen, FASTRPC_ALIGN); 8623a1bf591SJeya R for (oix = 0; oix < ctx->nbufs; oix++) { 8633a1bf591SJeya R int i = ctx->olaps[oix].raix; 8643a1bf591SJeya R 865c68cfb71SSrinivas Kandagatla if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) { 86625e8dfb8SSrinivas Kandagatla 8673a1bf591SJeya R if (ctx->olaps[oix].offset == 0) 868c68cfb71SSrinivas Kandagatla size = ALIGN(size, FASTRPC_ALIGN); 86925e8dfb8SSrinivas Kandagatla 8703a1bf591SJeya R size += (ctx->olaps[oix].mend - ctx->olaps[oix].mstart); 871c68cfb71SSrinivas Kandagatla } 872c68cfb71SSrinivas Kandagatla } 873c68cfb71SSrinivas Kandagatla 874c68cfb71SSrinivas Kandagatla return size; 875c68cfb71SSrinivas Kandagatla } 876c68cfb71SSrinivas Kandagatla 877c68cfb71SSrinivas Kandagatla static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx) 878c68cfb71SSrinivas Kandagatla { 879c68cfb71SSrinivas Kandagatla struct device *dev = ctx->fl->sctx->dev; 880c68cfb71SSrinivas Kandagatla int i, err; 881c68cfb71SSrinivas Kandagatla 882c68cfb71SSrinivas Kandagatla for (i = 0; i < ctx->nscalars; ++i) { 883c68cfb71SSrinivas Kandagatla 884c68cfb71SSrinivas Kandagatla if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 || 885c68cfb71SSrinivas Kandagatla ctx->args[i].length == 0) 886c68cfb71SSrinivas Kandagatla continue; 887c68cfb71SSrinivas Kandagatla 888c68cfb71SSrinivas Kandagatla err = fastrpc_map_create(ctx->fl, ctx->args[i].fd, 889e90d9119SVamsi Krishna Gattupalli ctx->args[i].length, ctx->args[i].attr, &ctx->maps[i]); 890c68cfb71SSrinivas Kandagatla if (err) { 891c68cfb71SSrinivas Kandagatla dev_err(dev, "Error Creating map %d\n", err); 892c68cfb71SSrinivas Kandagatla return -EINVAL; 893c68cfb71SSrinivas Kandagatla } 894c68cfb71SSrinivas Kandagatla 895c68cfb71SSrinivas Kandagatla } 896c68cfb71SSrinivas Kandagatla return 0; 897c68cfb71SSrinivas Kandagatla } 898c68cfb71SSrinivas Kandagatla 89954f7c85bSVamsi Krishna Gattupalli static struct fastrpc_invoke_buf *fastrpc_invoke_buf_start(union fastrpc_remote_arg *pra, int len) 90054f7c85bSVamsi Krishna Gattupalli { 90154f7c85bSVamsi Krishna Gattupalli return (struct fastrpc_invoke_buf *)(&pra[len]); 90254f7c85bSVamsi Krishna Gattupalli } 90354f7c85bSVamsi Krishna Gattupalli 90454f7c85bSVamsi Krishna Gattupalli static struct fastrpc_phy_page *fastrpc_phy_page_start(struct fastrpc_invoke_buf *buf, int len) 90554f7c85bSVamsi Krishna Gattupalli { 90654f7c85bSVamsi Krishna Gattupalli return (struct fastrpc_phy_page *)(&buf[len]); 90754f7c85bSVamsi Krishna Gattupalli } 90854f7c85bSVamsi Krishna Gattupalli 909c68cfb71SSrinivas Kandagatla static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx) 910c68cfb71SSrinivas Kandagatla { 911c68cfb71SSrinivas Kandagatla struct device *dev = ctx->fl->sctx->dev; 91235a82b87SVamsi Krishna Gattupalli union fastrpc_remote_arg *rpra; 913c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_buf *list; 914c68cfb71SSrinivas Kandagatla struct fastrpc_phy_page *pages; 91525e8dfb8SSrinivas Kandagatla int inbufs, i, oix, err = 0; 91625e8dfb8SSrinivas Kandagatla u64 len, rlen, pkt_size; 91702b45b47SSrinivas Kandagatla u64 pg_start, pg_end; 918c68cfb71SSrinivas Kandagatla uintptr_t args; 919c68cfb71SSrinivas Kandagatla int metalen; 920c68cfb71SSrinivas Kandagatla 921c68cfb71SSrinivas Kandagatla inbufs = REMOTE_SCALARS_INBUFS(ctx->sc); 922c68cfb71SSrinivas Kandagatla metalen = fastrpc_get_meta_size(ctx); 923c68cfb71SSrinivas Kandagatla pkt_size = fastrpc_get_payload_size(ctx, metalen); 924c68cfb71SSrinivas Kandagatla 925c68cfb71SSrinivas Kandagatla err = fastrpc_create_maps(ctx); 926c68cfb71SSrinivas Kandagatla if (err) 927c68cfb71SSrinivas Kandagatla return err; 928c68cfb71SSrinivas Kandagatla 929c68cfb71SSrinivas Kandagatla ctx->msg_sz = pkt_size; 930c68cfb71SSrinivas Kandagatla 931c68cfb71SSrinivas Kandagatla err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf); 932c68cfb71SSrinivas Kandagatla if (err) 933c68cfb71SSrinivas Kandagatla return err; 934c68cfb71SSrinivas Kandagatla 935c68cfb71SSrinivas Kandagatla rpra = ctx->buf->virt; 93654f7c85bSVamsi Krishna Gattupalli list = fastrpc_invoke_buf_start(rpra, ctx->nscalars); 93754f7c85bSVamsi Krishna Gattupalli pages = fastrpc_phy_page_start(list, ctx->nscalars); 938c68cfb71SSrinivas Kandagatla args = (uintptr_t)ctx->buf->virt + metalen; 939c68cfb71SSrinivas Kandagatla rlen = pkt_size - metalen; 940c68cfb71SSrinivas Kandagatla ctx->rpra = rpra; 941c68cfb71SSrinivas Kandagatla 94225e8dfb8SSrinivas Kandagatla for (oix = 0; oix < ctx->nbufs; ++oix) { 94325e8dfb8SSrinivas Kandagatla int mlen; 94425e8dfb8SSrinivas Kandagatla 94525e8dfb8SSrinivas Kandagatla i = ctx->olaps[oix].raix; 94625e8dfb8SSrinivas Kandagatla len = ctx->args[i].length; 947c68cfb71SSrinivas Kandagatla 94835a82b87SVamsi Krishna Gattupalli rpra[i].buf.pv = 0; 94935a82b87SVamsi Krishna Gattupalli rpra[i].buf.len = len; 950c68cfb71SSrinivas Kandagatla list[i].num = len ? 1 : 0; 951c68cfb71SSrinivas Kandagatla list[i].pgidx = i; 952c68cfb71SSrinivas Kandagatla 953c68cfb71SSrinivas Kandagatla if (!len) 954c68cfb71SSrinivas Kandagatla continue; 955c68cfb71SSrinivas Kandagatla 956c68cfb71SSrinivas Kandagatla if (ctx->maps[i]) { 95780f3afd7SSrinivas Kandagatla struct vm_area_struct *vma = NULL; 95880f3afd7SSrinivas Kandagatla 95935a82b87SVamsi Krishna Gattupalli rpra[i].buf.pv = (u64) ctx->args[i].ptr; 960c68cfb71SSrinivas Kandagatla pages[i].addr = ctx->maps[i]->phys; 96180f3afd7SSrinivas Kandagatla 962f9a470dbSSrinivas Kandagatla mmap_read_lock(current->mm); 96380f3afd7SSrinivas Kandagatla vma = find_vma(current->mm, ctx->args[i].ptr); 96480f3afd7SSrinivas Kandagatla if (vma) 96580f3afd7SSrinivas Kandagatla pages[i].addr += ctx->args[i].ptr - 96680f3afd7SSrinivas Kandagatla vma->vm_start; 967f9a470dbSSrinivas Kandagatla mmap_read_unlock(current->mm); 96880f3afd7SSrinivas Kandagatla 96902b45b47SSrinivas Kandagatla pg_start = (ctx->args[i].ptr & PAGE_MASK) >> PAGE_SHIFT; 97002b45b47SSrinivas Kandagatla pg_end = ((ctx->args[i].ptr + len - 1) & PAGE_MASK) >> 97102b45b47SSrinivas Kandagatla PAGE_SHIFT; 97202b45b47SSrinivas Kandagatla pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE; 97302b45b47SSrinivas Kandagatla 974c68cfb71SSrinivas Kandagatla } else { 97525e8dfb8SSrinivas Kandagatla 97625e8dfb8SSrinivas Kandagatla if (ctx->olaps[oix].offset == 0) { 977c68cfb71SSrinivas Kandagatla rlen -= ALIGN(args, FASTRPC_ALIGN) - args; 978c68cfb71SSrinivas Kandagatla args = ALIGN(args, FASTRPC_ALIGN); 97925e8dfb8SSrinivas Kandagatla } 98025e8dfb8SSrinivas Kandagatla 98125e8dfb8SSrinivas Kandagatla mlen = ctx->olaps[oix].mend - ctx->olaps[oix].mstart; 98225e8dfb8SSrinivas Kandagatla 98325e8dfb8SSrinivas Kandagatla if (rlen < mlen) 984c68cfb71SSrinivas Kandagatla goto bail; 985c68cfb71SSrinivas Kandagatla 98635a82b87SVamsi Krishna Gattupalli rpra[i].buf.pv = args - ctx->olaps[oix].offset; 98725e8dfb8SSrinivas Kandagatla pages[i].addr = ctx->buf->phys - 98825e8dfb8SSrinivas Kandagatla ctx->olaps[oix].offset + 98925e8dfb8SSrinivas Kandagatla (pkt_size - rlen); 990c68cfb71SSrinivas Kandagatla pages[i].addr = pages[i].addr & PAGE_MASK; 99125e8dfb8SSrinivas Kandagatla 99202b45b47SSrinivas Kandagatla pg_start = (args & PAGE_MASK) >> PAGE_SHIFT; 99302b45b47SSrinivas Kandagatla pg_end = ((args + len - 1) & PAGE_MASK) >> PAGE_SHIFT; 99402b45b47SSrinivas Kandagatla pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE; 99525e8dfb8SSrinivas Kandagatla args = args + mlen; 99625e8dfb8SSrinivas Kandagatla rlen -= mlen; 997c68cfb71SSrinivas Kandagatla } 998c68cfb71SSrinivas Kandagatla 999c68cfb71SSrinivas Kandagatla if (i < inbufs && !ctx->maps[i]) { 100035a82b87SVamsi Krishna Gattupalli void *dst = (void *)(uintptr_t)rpra[i].buf.pv; 1001c68cfb71SSrinivas Kandagatla void *src = (void *)(uintptr_t)ctx->args[i].ptr; 1002c68cfb71SSrinivas Kandagatla 1003c68cfb71SSrinivas Kandagatla if (!kernel) { 1004c68cfb71SSrinivas Kandagatla if (copy_from_user(dst, (void __user *)src, 1005c68cfb71SSrinivas Kandagatla len)) { 1006c68cfb71SSrinivas Kandagatla err = -EFAULT; 1007c68cfb71SSrinivas Kandagatla goto bail; 1008c68cfb71SSrinivas Kandagatla } 1009c68cfb71SSrinivas Kandagatla } else { 1010c68cfb71SSrinivas Kandagatla memcpy(dst, src, len); 1011c68cfb71SSrinivas Kandagatla } 1012c68cfb71SSrinivas Kandagatla } 1013c68cfb71SSrinivas Kandagatla } 1014c68cfb71SSrinivas Kandagatla 1015c68cfb71SSrinivas Kandagatla for (i = ctx->nbufs; i < ctx->nscalars; ++i) { 1016c68cfb71SSrinivas Kandagatla list[i].num = ctx->args[i].length ? 1 : 0; 1017c68cfb71SSrinivas Kandagatla list[i].pgidx = i; 101835a82b87SVamsi Krishna Gattupalli if (ctx->maps[i]) { 1019c68cfb71SSrinivas Kandagatla pages[i].addr = ctx->maps[i]->phys; 1020c68cfb71SSrinivas Kandagatla pages[i].size = ctx->maps[i]->size; 1021c68cfb71SSrinivas Kandagatla } 102235a82b87SVamsi Krishna Gattupalli rpra[i].dma.fd = ctx->args[i].fd; 102335a82b87SVamsi Krishna Gattupalli rpra[i].dma.len = ctx->args[i].length; 102435a82b87SVamsi Krishna Gattupalli rpra[i].dma.offset = (u64) ctx->args[i].ptr; 102535a82b87SVamsi Krishna Gattupalli } 1026c68cfb71SSrinivas Kandagatla 1027c68cfb71SSrinivas Kandagatla bail: 1028c68cfb71SSrinivas Kandagatla if (err) 1029c68cfb71SSrinivas Kandagatla dev_err(dev, "Error: get invoke args failed:%d\n", err); 1030c68cfb71SSrinivas Kandagatla 1031c68cfb71SSrinivas Kandagatla return err; 1032c68cfb71SSrinivas Kandagatla } 1033c68cfb71SSrinivas Kandagatla 1034c68cfb71SSrinivas Kandagatla static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx, 1035c68cfb71SSrinivas Kandagatla u32 kernel) 1036c68cfb71SSrinivas Kandagatla { 103735a82b87SVamsi Krishna Gattupalli union fastrpc_remote_arg *rpra = ctx->rpra; 10388f6c1d8cSVamsi Krishna Gattupalli struct fastrpc_user *fl = ctx->fl; 10398f6c1d8cSVamsi Krishna Gattupalli struct fastrpc_map *mmap = NULL; 10408f6c1d8cSVamsi Krishna Gattupalli struct fastrpc_invoke_buf *list; 10418f6c1d8cSVamsi Krishna Gattupalli struct fastrpc_phy_page *pages; 10428f6c1d8cSVamsi Krishna Gattupalli u64 *fdlist; 10438f6c1d8cSVamsi Krishna Gattupalli int i, inbufs, outbufs, handles; 1044c68cfb71SSrinivas Kandagatla 1045c68cfb71SSrinivas Kandagatla inbufs = REMOTE_SCALARS_INBUFS(ctx->sc); 10468f6c1d8cSVamsi Krishna Gattupalli outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc); 10478f6c1d8cSVamsi Krishna Gattupalli handles = REMOTE_SCALARS_INHANDLES(ctx->sc) + REMOTE_SCALARS_OUTHANDLES(ctx->sc); 10488f6c1d8cSVamsi Krishna Gattupalli list = fastrpc_invoke_buf_start(rpra, ctx->nscalars); 10498f6c1d8cSVamsi Krishna Gattupalli pages = fastrpc_phy_page_start(list, ctx->nscalars); 10508f6c1d8cSVamsi Krishna Gattupalli fdlist = (uint64_t *)(pages + inbufs + outbufs + handles); 1051c68cfb71SSrinivas Kandagatla 1052c68cfb71SSrinivas Kandagatla for (i = inbufs; i < ctx->nbufs; ++i) { 1053847afd7bSJeya R if (!ctx->maps[i]) { 105435a82b87SVamsi Krishna Gattupalli void *src = (void *)(uintptr_t)rpra[i].buf.pv; 1055c68cfb71SSrinivas Kandagatla void *dst = (void *)(uintptr_t)ctx->args[i].ptr; 105635a82b87SVamsi Krishna Gattupalli u64 len = rpra[i].buf.len; 1057c68cfb71SSrinivas Kandagatla 1058c68cfb71SSrinivas Kandagatla if (!kernel) { 1059c68cfb71SSrinivas Kandagatla if (copy_to_user((void __user *)dst, src, len)) 1060c68cfb71SSrinivas Kandagatla return -EFAULT; 1061c68cfb71SSrinivas Kandagatla } else { 1062c68cfb71SSrinivas Kandagatla memcpy(dst, src, len); 1063c68cfb71SSrinivas Kandagatla } 1064c68cfb71SSrinivas Kandagatla } 1065847afd7bSJeya R } 1066c68cfb71SSrinivas Kandagatla 10678f6c1d8cSVamsi Krishna Gattupalli for (i = 0; i < FASTRPC_MAX_FDLIST; i++) { 10688f6c1d8cSVamsi Krishna Gattupalli if (!fdlist[i]) 10698f6c1d8cSVamsi Krishna Gattupalli break; 10708f6c1d8cSVamsi Krishna Gattupalli if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap)) 10718f6c1d8cSVamsi Krishna Gattupalli fastrpc_map_put(mmap); 10728f6c1d8cSVamsi Krishna Gattupalli } 10738f6c1d8cSVamsi Krishna Gattupalli 1074c68cfb71SSrinivas Kandagatla return 0; 1075c68cfb71SSrinivas Kandagatla } 1076c68cfb71SSrinivas Kandagatla 1077c68cfb71SSrinivas Kandagatla static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx, 1078c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx, 1079c68cfb71SSrinivas Kandagatla u32 kernel, uint32_t handle) 1080c68cfb71SSrinivas Kandagatla { 1081c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 1082c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl = ctx->fl; 1083c68cfb71SSrinivas Kandagatla struct fastrpc_msg *msg = &ctx->msg; 108474003385SSrinivas Kandagatla int ret; 1085c68cfb71SSrinivas Kandagatla 1086c68cfb71SSrinivas Kandagatla cctx = fl->cctx; 1087c68cfb71SSrinivas Kandagatla msg->pid = fl->tgid; 1088c68cfb71SSrinivas Kandagatla msg->tid = current->pid; 1089c68cfb71SSrinivas Kandagatla 1090c68cfb71SSrinivas Kandagatla if (kernel) 1091c68cfb71SSrinivas Kandagatla msg->pid = 0; 1092c68cfb71SSrinivas Kandagatla 1093c68cfb71SSrinivas Kandagatla msg->ctx = ctx->ctxid | fl->pd; 1094c68cfb71SSrinivas Kandagatla msg->handle = handle; 1095c68cfb71SSrinivas Kandagatla msg->sc = ctx->sc; 1096c68cfb71SSrinivas Kandagatla msg->addr = ctx->buf ? ctx->buf->phys : 0; 1097c68cfb71SSrinivas Kandagatla msg->size = roundup(ctx->msg_sz, PAGE_SIZE); 1098c68cfb71SSrinivas Kandagatla fastrpc_context_get(ctx); 1099c68cfb71SSrinivas Kandagatla 110074003385SSrinivas Kandagatla ret = rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg)); 110174003385SSrinivas Kandagatla 110274003385SSrinivas Kandagatla if (ret) 110374003385SSrinivas Kandagatla fastrpc_context_put(ctx); 110474003385SSrinivas Kandagatla 110574003385SSrinivas Kandagatla return ret; 110674003385SSrinivas Kandagatla 1107c68cfb71SSrinivas Kandagatla } 1108c68cfb71SSrinivas Kandagatla 1109c68cfb71SSrinivas Kandagatla static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, 1110c68cfb71SSrinivas Kandagatla u32 handle, u32 sc, 1111c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_args *args) 1112c68cfb71SSrinivas Kandagatla { 1113c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx = NULL; 111476e8e4acSAbel Vesa struct fastrpc_buf *buf, *b; 111576e8e4acSAbel Vesa 1116c68cfb71SSrinivas Kandagatla int err = 0; 1117c68cfb71SSrinivas Kandagatla 1118c68cfb71SSrinivas Kandagatla if (!fl->sctx) 1119c68cfb71SSrinivas Kandagatla return -EINVAL; 1120c68cfb71SSrinivas Kandagatla 11212e369878SBjorn Andersson if (!fl->cctx->rpdev) 11222e369878SBjorn Andersson return -EPIPE; 11232e369878SBjorn Andersson 112420c40794SDmitry Baryshkov if (handle == FASTRPC_INIT_HANDLE && !kernel) { 112520c40794SDmitry Baryshkov dev_warn_ratelimited(fl->sctx->dev, "user app trying to send a kernel RPC message (%d)\n", handle); 112620c40794SDmitry Baryshkov return -EPERM; 112720c40794SDmitry Baryshkov } 112820c40794SDmitry Baryshkov 1129c68cfb71SSrinivas Kandagatla ctx = fastrpc_context_alloc(fl, kernel, sc, args); 1130c68cfb71SSrinivas Kandagatla if (IS_ERR(ctx)) 1131c68cfb71SSrinivas Kandagatla return PTR_ERR(ctx); 1132c68cfb71SSrinivas Kandagatla 1133c68cfb71SSrinivas Kandagatla if (ctx->nscalars) { 1134c68cfb71SSrinivas Kandagatla err = fastrpc_get_args(kernel, ctx); 1135c68cfb71SSrinivas Kandagatla if (err) 1136c68cfb71SSrinivas Kandagatla goto bail; 1137c68cfb71SSrinivas Kandagatla } 1138415a0729SSrinivas Kandagatla 1139415a0729SSrinivas Kandagatla /* make sure that all CPU memory writes are seen by DSP */ 1140415a0729SSrinivas Kandagatla dma_wmb(); 1141c68cfb71SSrinivas Kandagatla /* Send invoke buffer to remote dsp */ 1142c68cfb71SSrinivas Kandagatla err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle); 1143c68cfb71SSrinivas Kandagatla if (err) 1144c68cfb71SSrinivas Kandagatla goto bail; 1145c68cfb71SSrinivas Kandagatla 114655bcda35SJorge Ramirez-Ortiz if (kernel) { 114755bcda35SJorge Ramirez-Ortiz if (!wait_for_completion_timeout(&ctx->work, 10 * HZ)) 114855bcda35SJorge Ramirez-Ortiz err = -ETIMEDOUT; 114955bcda35SJorge Ramirez-Ortiz } else { 1150c68cfb71SSrinivas Kandagatla err = wait_for_completion_interruptible(&ctx->work); 115155bcda35SJorge Ramirez-Ortiz } 115255bcda35SJorge Ramirez-Ortiz 1153c68cfb71SSrinivas Kandagatla if (err) 1154c68cfb71SSrinivas Kandagatla goto bail; 1155c68cfb71SSrinivas Kandagatla 1156c68cfb71SSrinivas Kandagatla /* Check the response from remote dsp */ 1157c68cfb71SSrinivas Kandagatla err = ctx->retval; 1158c68cfb71SSrinivas Kandagatla if (err) 1159c68cfb71SSrinivas Kandagatla goto bail; 1160c68cfb71SSrinivas Kandagatla 1161c68cfb71SSrinivas Kandagatla if (ctx->nscalars) { 1162415a0729SSrinivas Kandagatla /* make sure that all memory writes by DSP are seen by CPU */ 1163415a0729SSrinivas Kandagatla dma_rmb(); 1164c68cfb71SSrinivas Kandagatla /* populate all the output buffers with results */ 1165c68cfb71SSrinivas Kandagatla err = fastrpc_put_args(ctx, kernel); 1166c68cfb71SSrinivas Kandagatla if (err) 1167c68cfb71SSrinivas Kandagatla goto bail; 1168c68cfb71SSrinivas Kandagatla } 1169c68cfb71SSrinivas Kandagatla 1170c68cfb71SSrinivas Kandagatla bail: 1171387f6255SJorge Ramirez-Ortiz if (err != -ERESTARTSYS && err != -ETIMEDOUT) { 1172387f6255SJorge Ramirez-Ortiz /* We are done with this compute context */ 1173c68cfb71SSrinivas Kandagatla spin_lock(&fl->lock); 1174c68cfb71SSrinivas Kandagatla list_del(&ctx->node); 1175c68cfb71SSrinivas Kandagatla spin_unlock(&fl->lock); 1176c68cfb71SSrinivas Kandagatla fastrpc_context_put(ctx); 1177387f6255SJorge Ramirez-Ortiz } 117808715610SAbel Vesa 117976e8e4acSAbel Vesa if (err == -ERESTARTSYS) { 118076e8e4acSAbel Vesa list_for_each_entry_safe(buf, b, &fl->mmaps, node) { 118176e8e4acSAbel Vesa list_del(&buf->node); 118276e8e4acSAbel Vesa list_add_tail(&buf->node, &fl->cctx->invoke_interrupted_mmaps); 118376e8e4acSAbel Vesa } 118476e8e4acSAbel Vesa } 118576e8e4acSAbel Vesa 1186c68cfb71SSrinivas Kandagatla if (err) 1187c68cfb71SSrinivas Kandagatla dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err); 1188c68cfb71SSrinivas Kandagatla 1189c68cfb71SSrinivas Kandagatla return err; 1190c68cfb71SSrinivas Kandagatla } 1191c68cfb71SSrinivas Kandagatla 11927f1f4812SJeya R static bool is_session_rejected(struct fastrpc_user *fl, bool unsigned_pd_request) 11937f1f4812SJeya R { 11947f1f4812SJeya R /* Check if the device node is non-secure and channel is secure*/ 11957f1f4812SJeya R if (!fl->is_secure_dev && fl->cctx->secure) { 11967f1f4812SJeya R /* 11977f1f4812SJeya R * Allow untrusted applications to offload only to Unsigned PD when 11987f1f4812SJeya R * channel is configured as secure and block untrusted apps on channel 11997f1f4812SJeya R * that does not support unsigned PD offload 12007f1f4812SJeya R */ 12017f1f4812SJeya R if (!fl->cctx->unsigned_support || !unsigned_pd_request) { 12027f1f4812SJeya R dev_err(&fl->cctx->rpdev->dev, "Error: Untrusted application trying to offload to signed PD"); 12037f1f4812SJeya R return true; 12047f1f4812SJeya R } 12057f1f4812SJeya R } 12067f1f4812SJeya R 12077f1f4812SJeya R return false; 12087f1f4812SJeya R } 12097f1f4812SJeya R 121008715610SAbel Vesa static int fastrpc_init_create_static_process(struct fastrpc_user *fl, 121108715610SAbel Vesa char __user *argp) 121208715610SAbel Vesa { 121308715610SAbel Vesa struct fastrpc_init_create_static init; 121408715610SAbel Vesa struct fastrpc_invoke_args *args; 121508715610SAbel Vesa struct fastrpc_phy_page pages[1]; 121608715610SAbel Vesa char *name; 121708715610SAbel Vesa int err; 121808715610SAbel Vesa struct { 121908715610SAbel Vesa int pgid; 122008715610SAbel Vesa u32 namelen; 122108715610SAbel Vesa u32 pageslen; 122208715610SAbel Vesa } inbuf; 122308715610SAbel Vesa u32 sc; 122408715610SAbel Vesa 122508715610SAbel Vesa args = kcalloc(FASTRPC_CREATE_STATIC_PROCESS_NARGS, sizeof(*args), GFP_KERNEL); 122608715610SAbel Vesa if (!args) 122708715610SAbel Vesa return -ENOMEM; 122808715610SAbel Vesa 122908715610SAbel Vesa if (copy_from_user(&init, argp, sizeof(init))) { 123008715610SAbel Vesa err = -EFAULT; 123108715610SAbel Vesa goto err; 123208715610SAbel Vesa } 123308715610SAbel Vesa 123408715610SAbel Vesa if (init.namelen > INIT_FILE_NAMELEN_MAX) { 123508715610SAbel Vesa err = -EINVAL; 123608715610SAbel Vesa goto err; 123708715610SAbel Vesa } 123808715610SAbel Vesa 123908715610SAbel Vesa name = kzalloc(init.namelen, GFP_KERNEL); 124008715610SAbel Vesa if (!name) { 124108715610SAbel Vesa err = -ENOMEM; 124208715610SAbel Vesa goto err; 124308715610SAbel Vesa } 124408715610SAbel Vesa 124508715610SAbel Vesa if (copy_from_user(name, (void __user *)(uintptr_t)init.name, init.namelen)) { 124608715610SAbel Vesa err = -EFAULT; 124708715610SAbel Vesa goto err_name; 124808715610SAbel Vesa } 124908715610SAbel Vesa 125008715610SAbel Vesa if (!fl->cctx->remote_heap) { 125108715610SAbel Vesa err = fastrpc_remote_heap_alloc(fl, fl->sctx->dev, init.memlen, 125208715610SAbel Vesa &fl->cctx->remote_heap); 125308715610SAbel Vesa if (err) 125408715610SAbel Vesa goto err_name; 125508715610SAbel Vesa 125608715610SAbel Vesa /* Map if we have any heap VMIDs associated with this ADSP Static Process. */ 125708715610SAbel Vesa if (fl->cctx->vmcount) { 125808715610SAbel Vesa unsigned int perms = BIT(QCOM_SCM_VMID_HLOS); 125908715610SAbel Vesa 126008715610SAbel Vesa err = qcom_scm_assign_mem(fl->cctx->remote_heap->phys, 126108715610SAbel Vesa (u64)fl->cctx->remote_heap->size, &perms, 126208715610SAbel Vesa fl->cctx->vmperms, fl->cctx->vmcount); 126308715610SAbel Vesa if (err) { 126408715610SAbel Vesa dev_err(fl->sctx->dev, "Failed to assign memory with phys 0x%llx size 0x%llx err %d", 126508715610SAbel Vesa fl->cctx->remote_heap->phys, fl->cctx->remote_heap->size, err); 126608715610SAbel Vesa goto err_map; 126708715610SAbel Vesa } 126808715610SAbel Vesa } 126908715610SAbel Vesa } 127008715610SAbel Vesa 127108715610SAbel Vesa inbuf.pgid = fl->tgid; 127208715610SAbel Vesa inbuf.namelen = init.namelen; 127308715610SAbel Vesa inbuf.pageslen = 0; 127408715610SAbel Vesa fl->pd = USER_PD; 127508715610SAbel Vesa 127608715610SAbel Vesa args[0].ptr = (u64)(uintptr_t)&inbuf; 127708715610SAbel Vesa args[0].length = sizeof(inbuf); 127808715610SAbel Vesa args[0].fd = -1; 127908715610SAbel Vesa 128008715610SAbel Vesa args[1].ptr = (u64)(uintptr_t)name; 128108715610SAbel Vesa args[1].length = inbuf.namelen; 128208715610SAbel Vesa args[1].fd = -1; 128308715610SAbel Vesa 128408715610SAbel Vesa pages[0].addr = fl->cctx->remote_heap->phys; 128508715610SAbel Vesa pages[0].size = fl->cctx->remote_heap->size; 128608715610SAbel Vesa 128708715610SAbel Vesa args[2].ptr = (u64)(uintptr_t) pages; 128808715610SAbel Vesa args[2].length = sizeof(*pages); 128908715610SAbel Vesa args[2].fd = -1; 129008715610SAbel Vesa 129108715610SAbel Vesa sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_STATIC, 3, 0); 129208715610SAbel Vesa 129308715610SAbel Vesa err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, 129408715610SAbel Vesa sc, args); 129508715610SAbel Vesa if (err) 129608715610SAbel Vesa goto err_invoke; 129708715610SAbel Vesa 129808715610SAbel Vesa kfree(args); 129908715610SAbel Vesa 130008715610SAbel Vesa return 0; 130108715610SAbel Vesa err_invoke: 130208715610SAbel Vesa if (fl->cctx->vmcount) { 130308715610SAbel Vesa struct qcom_scm_vmperm perm; 130408715610SAbel Vesa 130508715610SAbel Vesa perm.vmid = QCOM_SCM_VMID_HLOS; 130608715610SAbel Vesa perm.perm = QCOM_SCM_PERM_RWX; 130708715610SAbel Vesa err = qcom_scm_assign_mem(fl->cctx->remote_heap->phys, 130808715610SAbel Vesa (u64)fl->cctx->remote_heap->size, 130908715610SAbel Vesa &(fl->cctx->vmperms[0].vmid), &perm, 1); 131008715610SAbel Vesa if (err) 131108715610SAbel Vesa dev_err(fl->sctx->dev, "Failed to assign memory phys 0x%llx size 0x%llx err %d", 131208715610SAbel Vesa fl->cctx->remote_heap->phys, fl->cctx->remote_heap->size, err); 131308715610SAbel Vesa } 131408715610SAbel Vesa err_map: 131508715610SAbel Vesa fastrpc_buf_free(fl->cctx->remote_heap); 131608715610SAbel Vesa err_name: 131708715610SAbel Vesa kfree(name); 131808715610SAbel Vesa err: 131908715610SAbel Vesa kfree(args); 132008715610SAbel Vesa 132108715610SAbel Vesa return err; 132208715610SAbel Vesa } 132308715610SAbel Vesa 1324d73f71c7SSrinivas Kandagatla static int fastrpc_init_create_process(struct fastrpc_user *fl, 1325d73f71c7SSrinivas Kandagatla char __user *argp) 1326d73f71c7SSrinivas Kandagatla { 1327d73f71c7SSrinivas Kandagatla struct fastrpc_init_create init; 1328d73f71c7SSrinivas Kandagatla struct fastrpc_invoke_args *args; 1329d73f71c7SSrinivas Kandagatla struct fastrpc_phy_page pages[1]; 1330d73f71c7SSrinivas Kandagatla struct fastrpc_map *map = NULL; 1331d73f71c7SSrinivas Kandagatla struct fastrpc_buf *imem = NULL; 1332d73f71c7SSrinivas Kandagatla int memlen; 1333d73f71c7SSrinivas Kandagatla int err; 1334d73f71c7SSrinivas Kandagatla struct { 1335d73f71c7SSrinivas Kandagatla int pgid; 1336d73f71c7SSrinivas Kandagatla u32 namelen; 1337d73f71c7SSrinivas Kandagatla u32 filelen; 1338d73f71c7SSrinivas Kandagatla u32 pageslen; 1339d73f71c7SSrinivas Kandagatla u32 attrs; 1340d73f71c7SSrinivas Kandagatla u32 siglen; 1341d73f71c7SSrinivas Kandagatla } inbuf; 1342d73f71c7SSrinivas Kandagatla u32 sc; 13437f1f4812SJeya R bool unsigned_module = false; 1344d73f71c7SSrinivas Kandagatla 1345d73f71c7SSrinivas Kandagatla args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL); 1346d73f71c7SSrinivas Kandagatla if (!args) 1347d73f71c7SSrinivas Kandagatla return -ENOMEM; 1348d73f71c7SSrinivas Kandagatla 1349d73f71c7SSrinivas Kandagatla if (copy_from_user(&init, argp, sizeof(init))) { 1350d73f71c7SSrinivas Kandagatla err = -EFAULT; 1351b49f6d83SThierry Escande goto err; 1352d73f71c7SSrinivas Kandagatla } 1353d73f71c7SSrinivas Kandagatla 13547f1f4812SJeya R if (init.attrs & FASTRPC_MODE_UNSIGNED_MODULE) 13557f1f4812SJeya R unsigned_module = true; 13567f1f4812SJeya R 13577f1f4812SJeya R if (is_session_rejected(fl, unsigned_module)) { 13587f1f4812SJeya R err = -ECONNREFUSED; 13597f1f4812SJeya R goto err; 13607f1f4812SJeya R } 13617f1f4812SJeya R 1362d73f71c7SSrinivas Kandagatla if (init.filelen > INIT_FILELEN_MAX) { 1363d73f71c7SSrinivas Kandagatla err = -EINVAL; 1364b49f6d83SThierry Escande goto err; 1365d73f71c7SSrinivas Kandagatla } 1366d73f71c7SSrinivas Kandagatla 1367d73f71c7SSrinivas Kandagatla inbuf.pgid = fl->tgid; 1368d73f71c7SSrinivas Kandagatla inbuf.namelen = strlen(current->comm) + 1; 1369d73f71c7SSrinivas Kandagatla inbuf.filelen = init.filelen; 1370d73f71c7SSrinivas Kandagatla inbuf.pageslen = 1; 1371d73f71c7SSrinivas Kandagatla inbuf.attrs = init.attrs; 1372d73f71c7SSrinivas Kandagatla inbuf.siglen = init.siglen; 137384195d20SJonathan Marek fl->pd = USER_PD; 1374d73f71c7SSrinivas Kandagatla 1375d73f71c7SSrinivas Kandagatla if (init.filelen && init.filefd) { 1376e90d9119SVamsi Krishna Gattupalli err = fastrpc_map_create(fl, init.filefd, init.filelen, 0, &map); 1377d73f71c7SSrinivas Kandagatla if (err) 1378b49f6d83SThierry Escande goto err; 1379d73f71c7SSrinivas Kandagatla } 1380d73f71c7SSrinivas Kandagatla 1381d73f71c7SSrinivas Kandagatla memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4), 1382d73f71c7SSrinivas Kandagatla 1024 * 1024); 1383d73f71c7SSrinivas Kandagatla err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen, 1384d73f71c7SSrinivas Kandagatla &imem); 1385b49f6d83SThierry Escande if (err) 1386b49f6d83SThierry Escande goto err_alloc; 1387d73f71c7SSrinivas Kandagatla 1388d73f71c7SSrinivas Kandagatla fl->init_mem = imem; 1389d73f71c7SSrinivas Kandagatla args[0].ptr = (u64)(uintptr_t)&inbuf; 1390d73f71c7SSrinivas Kandagatla args[0].length = sizeof(inbuf); 1391d73f71c7SSrinivas Kandagatla args[0].fd = -1; 1392d73f71c7SSrinivas Kandagatla 1393d73f71c7SSrinivas Kandagatla args[1].ptr = (u64)(uintptr_t)current->comm; 1394d73f71c7SSrinivas Kandagatla args[1].length = inbuf.namelen; 1395d73f71c7SSrinivas Kandagatla args[1].fd = -1; 1396d73f71c7SSrinivas Kandagatla 1397d73f71c7SSrinivas Kandagatla args[2].ptr = (u64) init.file; 1398d73f71c7SSrinivas Kandagatla args[2].length = inbuf.filelen; 1399d73f71c7SSrinivas Kandagatla args[2].fd = init.filefd; 1400d73f71c7SSrinivas Kandagatla 1401d73f71c7SSrinivas Kandagatla pages[0].addr = imem->phys; 1402d73f71c7SSrinivas Kandagatla pages[0].size = imem->size; 1403d73f71c7SSrinivas Kandagatla 1404d73f71c7SSrinivas Kandagatla args[3].ptr = (u64)(uintptr_t) pages; 1405d73f71c7SSrinivas Kandagatla args[3].length = 1 * sizeof(*pages); 1406d73f71c7SSrinivas Kandagatla args[3].fd = -1; 1407d73f71c7SSrinivas Kandagatla 1408d73f71c7SSrinivas Kandagatla args[4].ptr = (u64)(uintptr_t)&inbuf.attrs; 1409d73f71c7SSrinivas Kandagatla args[4].length = sizeof(inbuf.attrs); 1410d73f71c7SSrinivas Kandagatla args[4].fd = -1; 1411d73f71c7SSrinivas Kandagatla 1412d73f71c7SSrinivas Kandagatla args[5].ptr = (u64)(uintptr_t) &inbuf.siglen; 1413d73f71c7SSrinivas Kandagatla args[5].length = sizeof(inbuf.siglen); 1414d73f71c7SSrinivas Kandagatla args[5].fd = -1; 1415d73f71c7SSrinivas Kandagatla 1416d73f71c7SSrinivas Kandagatla sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0); 1417d73f71c7SSrinivas Kandagatla if (init.attrs) 1418d73f71c7SSrinivas Kandagatla sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0); 1419d73f71c7SSrinivas Kandagatla 1420d73f71c7SSrinivas Kandagatla err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, 1421d73f71c7SSrinivas Kandagatla sc, args); 1422b49f6d83SThierry Escande if (err) 1423b49f6d83SThierry Escande goto err_invoke; 1424d73f71c7SSrinivas Kandagatla 1425b49f6d83SThierry Escande kfree(args); 1426b49f6d83SThierry Escande 1427b49f6d83SThierry Escande return 0; 1428b49f6d83SThierry Escande 1429b49f6d83SThierry Escande err_invoke: 1430b49f6d83SThierry Escande fl->init_mem = NULL; 1431d73f71c7SSrinivas Kandagatla fastrpc_buf_free(imem); 1432b49f6d83SThierry Escande err_alloc: 1433b49f6d83SThierry Escande if (map) { 1434b49f6d83SThierry Escande spin_lock(&fl->lock); 1435b49f6d83SThierry Escande list_del(&map->node); 1436b49f6d83SThierry Escande spin_unlock(&fl->lock); 1437b49f6d83SThierry Escande fastrpc_map_put(map); 1438d73f71c7SSrinivas Kandagatla } 1439b49f6d83SThierry Escande err: 1440d73f71c7SSrinivas Kandagatla kfree(args); 1441d73f71c7SSrinivas Kandagatla 1442d73f71c7SSrinivas Kandagatla return err; 1443d73f71c7SSrinivas Kandagatla } 1444d73f71c7SSrinivas Kandagatla 1445f6f9279fSSrinivas Kandagatla static struct fastrpc_session_ctx *fastrpc_session_alloc( 1446f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx) 1447f6f9279fSSrinivas Kandagatla { 1448f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *session = NULL; 1449977e6c8dSSrinivas Kandagatla unsigned long flags; 1450f6f9279fSSrinivas Kandagatla int i; 1451f6f9279fSSrinivas Kandagatla 1452977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1453f6f9279fSSrinivas Kandagatla for (i = 0; i < cctx->sesscount; i++) { 1454f6f9279fSSrinivas Kandagatla if (!cctx->session[i].used && cctx->session[i].valid) { 1455f6f9279fSSrinivas Kandagatla cctx->session[i].used = true; 1456f6f9279fSSrinivas Kandagatla session = &cctx->session[i]; 1457f6f9279fSSrinivas Kandagatla break; 1458f6f9279fSSrinivas Kandagatla } 1459f6f9279fSSrinivas Kandagatla } 1460977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1461f6f9279fSSrinivas Kandagatla 1462f6f9279fSSrinivas Kandagatla return session; 1463f6f9279fSSrinivas Kandagatla } 1464f6f9279fSSrinivas Kandagatla 1465f6f9279fSSrinivas Kandagatla static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx, 1466f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *session) 1467f6f9279fSSrinivas Kandagatla { 1468977e6c8dSSrinivas Kandagatla unsigned long flags; 1469977e6c8dSSrinivas Kandagatla 1470977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1471f6f9279fSSrinivas Kandagatla session->used = false; 1472977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1473f6f9279fSSrinivas Kandagatla } 1474f6f9279fSSrinivas Kandagatla 1475d73f71c7SSrinivas Kandagatla static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl) 1476d73f71c7SSrinivas Kandagatla { 1477d73f71c7SSrinivas Kandagatla struct fastrpc_invoke_args args[1]; 1478d73f71c7SSrinivas Kandagatla int tgid = 0; 1479d73f71c7SSrinivas Kandagatla u32 sc; 1480d73f71c7SSrinivas Kandagatla 1481d73f71c7SSrinivas Kandagatla tgid = fl->tgid; 1482d73f71c7SSrinivas Kandagatla args[0].ptr = (u64)(uintptr_t) &tgid; 1483d73f71c7SSrinivas Kandagatla args[0].length = sizeof(tgid); 1484d73f71c7SSrinivas Kandagatla args[0].fd = -1; 1485d73f71c7SSrinivas Kandagatla sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0); 1486d73f71c7SSrinivas Kandagatla 1487d73f71c7SSrinivas Kandagatla return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, 1488d73f71c7SSrinivas Kandagatla sc, &args[0]); 1489d73f71c7SSrinivas Kandagatla } 1490d73f71c7SSrinivas Kandagatla 1491f6f9279fSSrinivas Kandagatla static int fastrpc_device_release(struct inode *inode, struct file *file) 1492f6f9279fSSrinivas Kandagatla { 1493f6f9279fSSrinivas Kandagatla struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data; 1494f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = fl->cctx; 1495c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx, *n; 1496c68cfb71SSrinivas Kandagatla struct fastrpc_map *map, *m; 14972419e55eSJorge Ramirez-Ortiz struct fastrpc_buf *buf, *b; 1498977e6c8dSSrinivas Kandagatla unsigned long flags; 1499f6f9279fSSrinivas Kandagatla 1500d73f71c7SSrinivas Kandagatla fastrpc_release_current_dsp_process(fl); 1501d73f71c7SSrinivas Kandagatla 1502977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1503f6f9279fSSrinivas Kandagatla list_del(&fl->user); 1504977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1505f6f9279fSSrinivas Kandagatla 1506c68cfb71SSrinivas Kandagatla if (fl->init_mem) 1507c68cfb71SSrinivas Kandagatla fastrpc_buf_free(fl->init_mem); 1508c68cfb71SSrinivas Kandagatla 1509c68cfb71SSrinivas Kandagatla list_for_each_entry_safe(ctx, n, &fl->pending, node) { 1510c68cfb71SSrinivas Kandagatla list_del(&ctx->node); 1511c68cfb71SSrinivas Kandagatla fastrpc_context_put(ctx); 1512c68cfb71SSrinivas Kandagatla } 1513c68cfb71SSrinivas Kandagatla 1514c68cfb71SSrinivas Kandagatla list_for_each_entry_safe(map, m, &fl->maps, node) { 1515c68cfb71SSrinivas Kandagatla list_del(&map->node); 1516c68cfb71SSrinivas Kandagatla fastrpc_map_put(map); 1517c68cfb71SSrinivas Kandagatla } 1518c68cfb71SSrinivas Kandagatla 15192419e55eSJorge Ramirez-Ortiz list_for_each_entry_safe(buf, b, &fl->mmaps, node) { 15202419e55eSJorge Ramirez-Ortiz list_del(&buf->node); 15212419e55eSJorge Ramirez-Ortiz fastrpc_buf_free(buf); 15222419e55eSJorge Ramirez-Ortiz } 15232419e55eSJorge Ramirez-Ortiz 1524f6f9279fSSrinivas Kandagatla fastrpc_session_free(cctx, fl->sctx); 1525278d56f9SBjorn Andersson fastrpc_channel_ctx_put(cctx); 1526f6f9279fSSrinivas Kandagatla 1527f6f9279fSSrinivas Kandagatla mutex_destroy(&fl->mutex); 1528f6f9279fSSrinivas Kandagatla kfree(fl); 1529f6f9279fSSrinivas Kandagatla file->private_data = NULL; 1530f6f9279fSSrinivas Kandagatla 1531f6f9279fSSrinivas Kandagatla return 0; 1532f6f9279fSSrinivas Kandagatla } 1533f6f9279fSSrinivas Kandagatla 1534f6f9279fSSrinivas Kandagatla static int fastrpc_device_open(struct inode *inode, struct file *filp) 1535f6f9279fSSrinivas Kandagatla { 1536965602eaSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 1537965602eaSSrinivas Kandagatla struct fastrpc_device *fdevice; 1538f6f9279fSSrinivas Kandagatla struct fastrpc_user *fl = NULL; 1539977e6c8dSSrinivas Kandagatla unsigned long flags; 1540f6f9279fSSrinivas Kandagatla 1541965602eaSSrinivas Kandagatla fdevice = miscdev_to_fdevice(filp->private_data); 1542965602eaSSrinivas Kandagatla cctx = fdevice->cctx; 1543965602eaSSrinivas Kandagatla 1544f6f9279fSSrinivas Kandagatla fl = kzalloc(sizeof(*fl), GFP_KERNEL); 1545f6f9279fSSrinivas Kandagatla if (!fl) 1546f6f9279fSSrinivas Kandagatla return -ENOMEM; 1547f6f9279fSSrinivas Kandagatla 1548278d56f9SBjorn Andersson /* Released in fastrpc_device_release() */ 1549278d56f9SBjorn Andersson fastrpc_channel_ctx_get(cctx); 1550278d56f9SBjorn Andersson 1551f6f9279fSSrinivas Kandagatla filp->private_data = fl; 1552f6f9279fSSrinivas Kandagatla spin_lock_init(&fl->lock); 1553f6f9279fSSrinivas Kandagatla mutex_init(&fl->mutex); 1554f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&fl->pending); 1555f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&fl->maps); 15562419e55eSJorge Ramirez-Ortiz INIT_LIST_HEAD(&fl->mmaps); 1557f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&fl->user); 1558f6f9279fSSrinivas Kandagatla fl->tgid = current->tgid; 1559f6f9279fSSrinivas Kandagatla fl->cctx = cctx; 15607f1f4812SJeya R fl->is_secure_dev = fdevice->secure; 15617c11df42SThierry Escande 15627c11df42SThierry Escande fl->sctx = fastrpc_session_alloc(cctx); 15637c11df42SThierry Escande if (!fl->sctx) { 15647c11df42SThierry Escande dev_err(&cctx->rpdev->dev, "No session available\n"); 15657c11df42SThierry Escande mutex_destroy(&fl->mutex); 15667c11df42SThierry Escande kfree(fl); 15677c11df42SThierry Escande 15687c11df42SThierry Escande return -EBUSY; 15697c11df42SThierry Escande } 15707c11df42SThierry Escande 1571977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1572f6f9279fSSrinivas Kandagatla list_add_tail(&fl->user, &cctx->users); 1573977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1574f6f9279fSSrinivas Kandagatla 1575f6f9279fSSrinivas Kandagatla return 0; 1576f6f9279fSSrinivas Kandagatla } 1577f6f9279fSSrinivas Kandagatla 15786cffd795SSrinivas Kandagatla static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp) 15796cffd795SSrinivas Kandagatla { 15806cffd795SSrinivas Kandagatla struct fastrpc_alloc_dma_buf bp; 15816cffd795SSrinivas Kandagatla DEFINE_DMA_BUF_EXPORT_INFO(exp_info); 15826cffd795SSrinivas Kandagatla struct fastrpc_buf *buf = NULL; 15836cffd795SSrinivas Kandagatla int err; 15846cffd795SSrinivas Kandagatla 15856cffd795SSrinivas Kandagatla if (copy_from_user(&bp, argp, sizeof(bp))) 15866cffd795SSrinivas Kandagatla return -EFAULT; 15876cffd795SSrinivas Kandagatla 15886cffd795SSrinivas Kandagatla err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf); 15896cffd795SSrinivas Kandagatla if (err) 15906cffd795SSrinivas Kandagatla return err; 15916cffd795SSrinivas Kandagatla exp_info.ops = &fastrpc_dma_buf_ops; 15926cffd795SSrinivas Kandagatla exp_info.size = bp.size; 15936cffd795SSrinivas Kandagatla exp_info.flags = O_RDWR; 15946cffd795SSrinivas Kandagatla exp_info.priv = buf; 15956cffd795SSrinivas Kandagatla buf->dmabuf = dma_buf_export(&exp_info); 15966cffd795SSrinivas Kandagatla if (IS_ERR(buf->dmabuf)) { 15976cffd795SSrinivas Kandagatla err = PTR_ERR(buf->dmabuf); 15986cffd795SSrinivas Kandagatla fastrpc_buf_free(buf); 15996cffd795SSrinivas Kandagatla return err; 16006cffd795SSrinivas Kandagatla } 16016cffd795SSrinivas Kandagatla 16026cffd795SSrinivas Kandagatla bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE); 16036cffd795SSrinivas Kandagatla if (bp.fd < 0) { 16046cffd795SSrinivas Kandagatla dma_buf_put(buf->dmabuf); 16056cffd795SSrinivas Kandagatla return -EINVAL; 16066cffd795SSrinivas Kandagatla } 16076cffd795SSrinivas Kandagatla 16086cffd795SSrinivas Kandagatla if (copy_to_user(argp, &bp, sizeof(bp))) { 160946963e2eSMathias Krause /* 161046963e2eSMathias Krause * The usercopy failed, but we can't do much about it, as 161146963e2eSMathias Krause * dma_buf_fd() already called fd_install() and made the 161246963e2eSMathias Krause * file descriptor accessible for the current process. It 161346963e2eSMathias Krause * might already be closed and dmabuf no longer valid when 161446963e2eSMathias Krause * we reach this point. Therefore "leak" the fd and rely on 161546963e2eSMathias Krause * the process exit path to do any required cleanup. 161646963e2eSMathias Krause */ 16176cffd795SSrinivas Kandagatla return -EFAULT; 16186cffd795SSrinivas Kandagatla } 16196cffd795SSrinivas Kandagatla 16206cffd795SSrinivas Kandagatla return 0; 16216cffd795SSrinivas Kandagatla } 16226cffd795SSrinivas Kandagatla 16236010d9beSJonathan Marek static int fastrpc_init_attach(struct fastrpc_user *fl, int pd) 1624d73f71c7SSrinivas Kandagatla { 1625d73f71c7SSrinivas Kandagatla struct fastrpc_invoke_args args[1]; 1626d73f71c7SSrinivas Kandagatla int tgid = fl->tgid; 1627d73f71c7SSrinivas Kandagatla u32 sc; 1628d73f71c7SSrinivas Kandagatla 1629d73f71c7SSrinivas Kandagatla args[0].ptr = (u64)(uintptr_t) &tgid; 1630d73f71c7SSrinivas Kandagatla args[0].length = sizeof(tgid); 1631d73f71c7SSrinivas Kandagatla args[0].fd = -1; 1632d73f71c7SSrinivas Kandagatla sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0); 16336010d9beSJonathan Marek fl->pd = pd; 1634d73f71c7SSrinivas Kandagatla 1635d73f71c7SSrinivas Kandagatla return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, 1636d73f71c7SSrinivas Kandagatla sc, &args[0]); 1637d73f71c7SSrinivas Kandagatla } 1638d73f71c7SSrinivas Kandagatla 1639c68cfb71SSrinivas Kandagatla static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp) 1640c68cfb71SSrinivas Kandagatla { 1641c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_args *args = NULL; 1642c68cfb71SSrinivas Kandagatla struct fastrpc_invoke inv; 1643c68cfb71SSrinivas Kandagatla u32 nscalars; 1644c68cfb71SSrinivas Kandagatla int err; 1645c68cfb71SSrinivas Kandagatla 1646c68cfb71SSrinivas Kandagatla if (copy_from_user(&inv, argp, sizeof(inv))) 1647c68cfb71SSrinivas Kandagatla return -EFAULT; 1648c68cfb71SSrinivas Kandagatla 1649c68cfb71SSrinivas Kandagatla /* nscalars is truncated here to max supported value */ 1650c68cfb71SSrinivas Kandagatla nscalars = REMOTE_SCALARS_LENGTH(inv.sc); 1651c68cfb71SSrinivas Kandagatla if (nscalars) { 1652c68cfb71SSrinivas Kandagatla args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL); 1653c68cfb71SSrinivas Kandagatla if (!args) 1654c68cfb71SSrinivas Kandagatla return -ENOMEM; 1655c68cfb71SSrinivas Kandagatla 1656c68cfb71SSrinivas Kandagatla if (copy_from_user(args, (void __user *)(uintptr_t)inv.args, 1657c68cfb71SSrinivas Kandagatla nscalars * sizeof(*args))) { 1658c68cfb71SSrinivas Kandagatla kfree(args); 1659c68cfb71SSrinivas Kandagatla return -EFAULT; 1660c68cfb71SSrinivas Kandagatla } 1661c68cfb71SSrinivas Kandagatla } 1662c68cfb71SSrinivas Kandagatla 1663c68cfb71SSrinivas Kandagatla err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args); 1664c68cfb71SSrinivas Kandagatla kfree(args); 1665c68cfb71SSrinivas Kandagatla 1666c68cfb71SSrinivas Kandagatla return err; 1667c68cfb71SSrinivas Kandagatla } 1668c68cfb71SSrinivas Kandagatla 16696c16fd8bSJeya R static int fastrpc_get_info_from_dsp(struct fastrpc_user *fl, uint32_t *dsp_attr_buf, 16706c16fd8bSJeya R uint32_t dsp_attr_buf_len) 16716c16fd8bSJeya R { 16726c16fd8bSJeya R struct fastrpc_invoke_args args[2] = { 0 }; 16736c16fd8bSJeya R 16746c16fd8bSJeya R /* Capability filled in userspace */ 16756c16fd8bSJeya R dsp_attr_buf[0] = 0; 16766c16fd8bSJeya R 16776c16fd8bSJeya R args[0].ptr = (u64)(uintptr_t)&dsp_attr_buf_len; 16786c16fd8bSJeya R args[0].length = sizeof(dsp_attr_buf_len); 16796c16fd8bSJeya R args[0].fd = -1; 16806c16fd8bSJeya R args[1].ptr = (u64)(uintptr_t)&dsp_attr_buf[1]; 16816c16fd8bSJeya R args[1].length = dsp_attr_buf_len; 16826c16fd8bSJeya R args[1].fd = -1; 1683f667f56bSAbel Vesa fl->pd = USER_PD; 16846c16fd8bSJeya R 16856c16fd8bSJeya R return fastrpc_internal_invoke(fl, true, FASTRPC_DSP_UTILITIES_HANDLE, 16866c16fd8bSJeya R FASTRPC_SCALARS(0, 1, 1), args); 16876c16fd8bSJeya R } 16886c16fd8bSJeya R 16896c16fd8bSJeya R static int fastrpc_get_info_from_kernel(struct fastrpc_ioctl_capability *cap, 16906c16fd8bSJeya R struct fastrpc_user *fl) 16916c16fd8bSJeya R { 16926c16fd8bSJeya R struct fastrpc_channel_ctx *cctx = fl->cctx; 16936c16fd8bSJeya R uint32_t attribute_id = cap->attribute_id; 16946c16fd8bSJeya R uint32_t *dsp_attributes; 16956c16fd8bSJeya R unsigned long flags; 16966c16fd8bSJeya R uint32_t domain = cap->domain; 16976c16fd8bSJeya R int err; 16986c16fd8bSJeya R 16996c16fd8bSJeya R spin_lock_irqsave(&cctx->lock, flags); 17006c16fd8bSJeya R /* check if we already have queried dsp for attributes */ 17016c16fd8bSJeya R if (cctx->valid_attributes) { 17026c16fd8bSJeya R spin_unlock_irqrestore(&cctx->lock, flags); 17036c16fd8bSJeya R goto done; 17046c16fd8bSJeya R } 17056c16fd8bSJeya R spin_unlock_irqrestore(&cctx->lock, flags); 17066c16fd8bSJeya R 17076c16fd8bSJeya R dsp_attributes = kzalloc(FASTRPC_MAX_DSP_ATTRIBUTES_LEN, GFP_KERNEL); 17086c16fd8bSJeya R if (!dsp_attributes) 17096c16fd8bSJeya R return -ENOMEM; 17106c16fd8bSJeya R 17116c16fd8bSJeya R err = fastrpc_get_info_from_dsp(fl, dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES_LEN); 17126c16fd8bSJeya R if (err == DSP_UNSUPPORTED_API) { 17136c16fd8bSJeya R dev_info(&cctx->rpdev->dev, 17146c16fd8bSJeya R "Warning: DSP capabilities not supported on domain: %d\n", domain); 17156c16fd8bSJeya R kfree(dsp_attributes); 17166c16fd8bSJeya R return -EOPNOTSUPP; 17176c16fd8bSJeya R } else if (err) { 17186c16fd8bSJeya R dev_err(&cctx->rpdev->dev, "Error: dsp information is incorrect err: %d\n", err); 17196c16fd8bSJeya R kfree(dsp_attributes); 17206c16fd8bSJeya R return err; 17216c16fd8bSJeya R } 17226c16fd8bSJeya R 17236c16fd8bSJeya R spin_lock_irqsave(&cctx->lock, flags); 17246c16fd8bSJeya R memcpy(cctx->dsp_attributes, dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES_LEN); 17256c16fd8bSJeya R cctx->valid_attributes = true; 17266c16fd8bSJeya R spin_unlock_irqrestore(&cctx->lock, flags); 17276c16fd8bSJeya R kfree(dsp_attributes); 17286c16fd8bSJeya R done: 17296c16fd8bSJeya R cap->capability = cctx->dsp_attributes[attribute_id]; 17306c16fd8bSJeya R return 0; 17316c16fd8bSJeya R } 17326c16fd8bSJeya R 17336c16fd8bSJeya R static int fastrpc_get_dsp_info(struct fastrpc_user *fl, char __user *argp) 17346c16fd8bSJeya R { 17356c16fd8bSJeya R struct fastrpc_ioctl_capability cap = {0}; 17366c16fd8bSJeya R int err = 0; 17376c16fd8bSJeya R 17386c16fd8bSJeya R if (copy_from_user(&cap, argp, sizeof(cap))) 17396c16fd8bSJeya R return -EFAULT; 17406c16fd8bSJeya R 17416c16fd8bSJeya R cap.capability = 0; 17426c16fd8bSJeya R if (cap.domain >= FASTRPC_DEV_MAX) { 17436c16fd8bSJeya R dev_err(&fl->cctx->rpdev->dev, "Error: Invalid domain id:%d, err:%d\n", 17446c16fd8bSJeya R cap.domain, err); 17456c16fd8bSJeya R return -ECHRNG; 17466c16fd8bSJeya R } 17476c16fd8bSJeya R 17486c16fd8bSJeya R /* Fastrpc Capablities does not support modem domain */ 17496c16fd8bSJeya R if (cap.domain == MDSP_DOMAIN_ID) { 17506c16fd8bSJeya R dev_err(&fl->cctx->rpdev->dev, "Error: modem not supported %d\n", err); 17516c16fd8bSJeya R return -ECHRNG; 17526c16fd8bSJeya R } 17536c16fd8bSJeya R 17546c16fd8bSJeya R if (cap.attribute_id >= FASTRPC_MAX_DSP_ATTRIBUTES) { 17556c16fd8bSJeya R dev_err(&fl->cctx->rpdev->dev, "Error: invalid attribute: %d, err: %d\n", 17566c16fd8bSJeya R cap.attribute_id, err); 17576c16fd8bSJeya R return -EOVERFLOW; 17586c16fd8bSJeya R } 17596c16fd8bSJeya R 17606c16fd8bSJeya R err = fastrpc_get_info_from_kernel(&cap, fl); 17616c16fd8bSJeya R if (err) 17626c16fd8bSJeya R return err; 17636c16fd8bSJeya R 17646c16fd8bSJeya R if (copy_to_user(argp, &cap.capability, sizeof(cap.capability))) 17656c16fd8bSJeya R return -EFAULT; 17666c16fd8bSJeya R 17676c16fd8bSJeya R return 0; 17686c16fd8bSJeya R } 17696c16fd8bSJeya R 177072fa6f78SAbel Vesa static int fastrpc_req_munmap_impl(struct fastrpc_user *fl, struct fastrpc_buf *buf) 17712419e55eSJorge Ramirez-Ortiz { 17722419e55eSJorge Ramirez-Ortiz struct fastrpc_invoke_args args[1] = { [0] = { 0 } }; 17732419e55eSJorge Ramirez-Ortiz struct fastrpc_munmap_req_msg req_msg; 17742419e55eSJorge Ramirez-Ortiz struct device *dev = fl->sctx->dev; 17752419e55eSJorge Ramirez-Ortiz int err; 17762419e55eSJorge Ramirez-Ortiz u32 sc; 17772419e55eSJorge Ramirez-Ortiz 17782419e55eSJorge Ramirez-Ortiz req_msg.pgid = fl->tgid; 17792419e55eSJorge Ramirez-Ortiz req_msg.size = buf->size; 17802419e55eSJorge Ramirez-Ortiz req_msg.vaddr = buf->raddr; 17812419e55eSJorge Ramirez-Ortiz 17822419e55eSJorge Ramirez-Ortiz args[0].ptr = (u64) (uintptr_t) &req_msg; 17832419e55eSJorge Ramirez-Ortiz args[0].length = sizeof(req_msg); 17842419e55eSJorge Ramirez-Ortiz 17852419e55eSJorge Ramirez-Ortiz sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MUNMAP, 1, 0); 17862419e55eSJorge Ramirez-Ortiz err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, 17872419e55eSJorge Ramirez-Ortiz &args[0]); 17882419e55eSJorge Ramirez-Ortiz if (!err) { 17892419e55eSJorge Ramirez-Ortiz dev_dbg(dev, "unmmap\tpt 0x%09lx OK\n", buf->raddr); 17902419e55eSJorge Ramirez-Ortiz spin_lock(&fl->lock); 17912419e55eSJorge Ramirez-Ortiz list_del(&buf->node); 17922419e55eSJorge Ramirez-Ortiz spin_unlock(&fl->lock); 17932419e55eSJorge Ramirez-Ortiz fastrpc_buf_free(buf); 17942419e55eSJorge Ramirez-Ortiz } else { 17952419e55eSJorge Ramirez-Ortiz dev_err(dev, "unmmap\tpt 0x%09lx ERROR\n", buf->raddr); 17962419e55eSJorge Ramirez-Ortiz } 17972419e55eSJorge Ramirez-Ortiz 17982419e55eSJorge Ramirez-Ortiz return err; 17992419e55eSJorge Ramirez-Ortiz } 18002419e55eSJorge Ramirez-Ortiz 18012419e55eSJorge Ramirez-Ortiz static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp) 18022419e55eSJorge Ramirez-Ortiz { 180372fa6f78SAbel Vesa struct fastrpc_buf *buf = NULL, *iter, *b; 18042419e55eSJorge Ramirez-Ortiz struct fastrpc_req_munmap req; 180572fa6f78SAbel Vesa struct device *dev = fl->sctx->dev; 18062419e55eSJorge Ramirez-Ortiz 18072419e55eSJorge Ramirez-Ortiz if (copy_from_user(&req, argp, sizeof(req))) 18082419e55eSJorge Ramirez-Ortiz return -EFAULT; 18092419e55eSJorge Ramirez-Ortiz 181072fa6f78SAbel Vesa spin_lock(&fl->lock); 181172fa6f78SAbel Vesa list_for_each_entry_safe(iter, b, &fl->mmaps, node) { 181272fa6f78SAbel Vesa if ((iter->raddr == req.vaddrout) && (iter->size == req.size)) { 181372fa6f78SAbel Vesa buf = iter; 181472fa6f78SAbel Vesa break; 181572fa6f78SAbel Vesa } 181672fa6f78SAbel Vesa } 181772fa6f78SAbel Vesa spin_unlock(&fl->lock); 181872fa6f78SAbel Vesa 181972fa6f78SAbel Vesa if (!buf) { 182072fa6f78SAbel Vesa dev_err(dev, "mmap\t\tpt 0x%09llx [len 0x%08llx] not in list\n", 182172fa6f78SAbel Vesa req.vaddrout, req.size); 182272fa6f78SAbel Vesa return -EINVAL; 182372fa6f78SAbel Vesa } 182472fa6f78SAbel Vesa 182572fa6f78SAbel Vesa return fastrpc_req_munmap_impl(fl, buf); 18262419e55eSJorge Ramirez-Ortiz } 18272419e55eSJorge Ramirez-Ortiz 18282419e55eSJorge Ramirez-Ortiz static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp) 18292419e55eSJorge Ramirez-Ortiz { 18302419e55eSJorge Ramirez-Ortiz struct fastrpc_invoke_args args[3] = { [0 ... 2] = { 0 } }; 18312419e55eSJorge Ramirez-Ortiz struct fastrpc_buf *buf = NULL; 18322419e55eSJorge Ramirez-Ortiz struct fastrpc_mmap_req_msg req_msg; 18332419e55eSJorge Ramirez-Ortiz struct fastrpc_mmap_rsp_msg rsp_msg; 18342419e55eSJorge Ramirez-Ortiz struct fastrpc_phy_page pages; 18352419e55eSJorge Ramirez-Ortiz struct fastrpc_req_mmap req; 18362419e55eSJorge Ramirez-Ortiz struct device *dev = fl->sctx->dev; 18372419e55eSJorge Ramirez-Ortiz int err; 18382419e55eSJorge Ramirez-Ortiz u32 sc; 18392419e55eSJorge Ramirez-Ortiz 18402419e55eSJorge Ramirez-Ortiz if (copy_from_user(&req, argp, sizeof(req))) 18412419e55eSJorge Ramirez-Ortiz return -EFAULT; 18422419e55eSJorge Ramirez-Ortiz 1843532ad70cSAbel Vesa if (req.flags != ADSP_MMAP_ADD_PAGES && req.flags != ADSP_MMAP_REMOTE_HEAP_ADDR) { 18442419e55eSJorge Ramirez-Ortiz dev_err(dev, "flag not supported 0x%x\n", req.flags); 1845532ad70cSAbel Vesa 18462419e55eSJorge Ramirez-Ortiz return -EINVAL; 18472419e55eSJorge Ramirez-Ortiz } 18482419e55eSJorge Ramirez-Ortiz 18492419e55eSJorge Ramirez-Ortiz if (req.vaddrin) { 18502419e55eSJorge Ramirez-Ortiz dev_err(dev, "adding user allocated pages is not supported\n"); 18512419e55eSJorge Ramirez-Ortiz return -EINVAL; 18522419e55eSJorge Ramirez-Ortiz } 18532419e55eSJorge Ramirez-Ortiz 18542419e55eSJorge Ramirez-Ortiz err = fastrpc_buf_alloc(fl, fl->sctx->dev, req.size, &buf); 18552419e55eSJorge Ramirez-Ortiz if (err) { 18562419e55eSJorge Ramirez-Ortiz dev_err(dev, "failed to allocate buffer\n"); 18572419e55eSJorge Ramirez-Ortiz return err; 18582419e55eSJorge Ramirez-Ortiz } 18592419e55eSJorge Ramirez-Ortiz 18602419e55eSJorge Ramirez-Ortiz req_msg.pgid = fl->tgid; 18612419e55eSJorge Ramirez-Ortiz req_msg.flags = req.flags; 18622419e55eSJorge Ramirez-Ortiz req_msg.vaddr = req.vaddrin; 18632419e55eSJorge Ramirez-Ortiz req_msg.num = sizeof(pages); 18642419e55eSJorge Ramirez-Ortiz 18652419e55eSJorge Ramirez-Ortiz args[0].ptr = (u64) (uintptr_t) &req_msg; 18662419e55eSJorge Ramirez-Ortiz args[0].length = sizeof(req_msg); 18672419e55eSJorge Ramirez-Ortiz 18682419e55eSJorge Ramirez-Ortiz pages.addr = buf->phys; 18692419e55eSJorge Ramirez-Ortiz pages.size = buf->size; 18702419e55eSJorge Ramirez-Ortiz 18712419e55eSJorge Ramirez-Ortiz args[1].ptr = (u64) (uintptr_t) &pages; 18722419e55eSJorge Ramirez-Ortiz args[1].length = sizeof(pages); 18732419e55eSJorge Ramirez-Ortiz 18742419e55eSJorge Ramirez-Ortiz args[2].ptr = (u64) (uintptr_t) &rsp_msg; 18752419e55eSJorge Ramirez-Ortiz args[2].length = sizeof(rsp_msg); 18762419e55eSJorge Ramirez-Ortiz 18772419e55eSJorge Ramirez-Ortiz sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MMAP, 2, 1); 18782419e55eSJorge Ramirez-Ortiz err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, 18792419e55eSJorge Ramirez-Ortiz &args[0]); 18802419e55eSJorge Ramirez-Ortiz if (err) { 18812419e55eSJorge Ramirez-Ortiz dev_err(dev, "mmap error (len 0x%08llx)\n", buf->size); 18822419e55eSJorge Ramirez-Ortiz goto err_invoke; 18832419e55eSJorge Ramirez-Ortiz } 18842419e55eSJorge Ramirez-Ortiz 18852419e55eSJorge Ramirez-Ortiz /* update the buffer to be able to deallocate the memory on the DSP */ 18862419e55eSJorge Ramirez-Ortiz buf->raddr = (uintptr_t) rsp_msg.vaddr; 18872419e55eSJorge Ramirez-Ortiz 18882419e55eSJorge Ramirez-Ortiz /* let the client know the address to use */ 18892419e55eSJorge Ramirez-Ortiz req.vaddrout = rsp_msg.vaddr; 18902419e55eSJorge Ramirez-Ortiz 1891532ad70cSAbel Vesa /* Add memory to static PD pool, protection thru hypervisor */ 1892532ad70cSAbel Vesa if (req.flags != ADSP_MMAP_REMOTE_HEAP_ADDR && fl->cctx->vmcount) { 1893532ad70cSAbel Vesa struct qcom_scm_vmperm perm; 1894532ad70cSAbel Vesa int err = 0; 1895532ad70cSAbel Vesa 1896532ad70cSAbel Vesa perm.vmid = QCOM_SCM_VMID_HLOS; 1897532ad70cSAbel Vesa perm.perm = QCOM_SCM_PERM_RWX; 1898532ad70cSAbel Vesa err = qcom_scm_assign_mem(buf->phys, buf->size, 1899532ad70cSAbel Vesa &(fl->cctx->vmperms[0].vmid), &perm, 1); 1900532ad70cSAbel Vesa if (err) { 1901532ad70cSAbel Vesa dev_err(fl->sctx->dev, "Failed to assign memory phys 0x%llx size 0x%llx err %d", 1902532ad70cSAbel Vesa buf->phys, buf->size, err); 1903532ad70cSAbel Vesa goto err_assign; 1904532ad70cSAbel Vesa } 1905532ad70cSAbel Vesa } 1906532ad70cSAbel Vesa 19072419e55eSJorge Ramirez-Ortiz spin_lock(&fl->lock); 19082419e55eSJorge Ramirez-Ortiz list_add_tail(&buf->node, &fl->mmaps); 19092419e55eSJorge Ramirez-Ortiz spin_unlock(&fl->lock); 19102419e55eSJorge Ramirez-Ortiz 19112419e55eSJorge Ramirez-Ortiz if (copy_to_user((void __user *)argp, &req, sizeof(req))) { 191272fa6f78SAbel Vesa err = -EFAULT; 191372fa6f78SAbel Vesa goto err_assign; 19142419e55eSJorge Ramirez-Ortiz } 19152419e55eSJorge Ramirez-Ortiz 19162419e55eSJorge Ramirez-Ortiz dev_dbg(dev, "mmap\t\tpt 0x%09lx OK [len 0x%08llx]\n", 19172419e55eSJorge Ramirez-Ortiz buf->raddr, buf->size); 19182419e55eSJorge Ramirez-Ortiz 19192419e55eSJorge Ramirez-Ortiz return 0; 19202419e55eSJorge Ramirez-Ortiz 192172fa6f78SAbel Vesa err_assign: 192272fa6f78SAbel Vesa fastrpc_req_munmap_impl(fl, buf); 19232419e55eSJorge Ramirez-Ortiz err_invoke: 19242419e55eSJorge Ramirez-Ortiz fastrpc_buf_free(buf); 19252419e55eSJorge Ramirez-Ortiz 19262419e55eSJorge Ramirez-Ortiz return err; 19272419e55eSJorge Ramirez-Ortiz } 19282419e55eSJorge Ramirez-Ortiz 19295c1b97c7SJeya R static int fastrpc_req_mem_unmap_impl(struct fastrpc_user *fl, struct fastrpc_mem_unmap *req) 19305c1b97c7SJeya R { 19315c1b97c7SJeya R struct fastrpc_invoke_args args[1] = { [0] = { 0 } }; 1932c5c07c59SSrinivas Kandagatla struct fastrpc_map *map = NULL, *iter, *m; 19335c1b97c7SJeya R struct fastrpc_mem_unmap_req_msg req_msg = { 0 }; 19345c1b97c7SJeya R int err = 0; 19355c1b97c7SJeya R u32 sc; 19365c1b97c7SJeya R struct device *dev = fl->sctx->dev; 19375c1b97c7SJeya R 19385c1b97c7SJeya R spin_lock(&fl->lock); 1939c5c07c59SSrinivas Kandagatla list_for_each_entry_safe(iter, m, &fl->maps, node) { 1940c5c07c59SSrinivas Kandagatla if ((req->fd < 0 || iter->fd == req->fd) && (iter->raddr == req->vaddr)) { 1941c5c07c59SSrinivas Kandagatla map = iter; 19425c1b97c7SJeya R break; 1943c5c07c59SSrinivas Kandagatla } 19445c1b97c7SJeya R } 19455c1b97c7SJeya R 19465c1b97c7SJeya R spin_unlock(&fl->lock); 19475c1b97c7SJeya R 19485c1b97c7SJeya R if (!map) { 19495c1b97c7SJeya R dev_err(dev, "map not in list\n"); 19505c1b97c7SJeya R return -EINVAL; 19515c1b97c7SJeya R } 19525c1b97c7SJeya R 19535c1b97c7SJeya R req_msg.pgid = fl->tgid; 19545c1b97c7SJeya R req_msg.len = map->len; 19555c1b97c7SJeya R req_msg.vaddrin = map->raddr; 19565c1b97c7SJeya R req_msg.fd = map->fd; 19575c1b97c7SJeya R 19585c1b97c7SJeya R args[0].ptr = (u64) (uintptr_t) &req_msg; 19595c1b97c7SJeya R args[0].length = sizeof(req_msg); 19605c1b97c7SJeya R 19615c1b97c7SJeya R sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MEM_UNMAP, 1, 0); 19625c1b97c7SJeya R err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, 19635c1b97c7SJeya R &args[0]); 19645c1b97c7SJeya R fastrpc_map_put(map); 19655c1b97c7SJeya R if (err) 19665c1b97c7SJeya R dev_err(dev, "unmmap\tpt fd = %d, 0x%09llx error\n", map->fd, map->raddr); 19675c1b97c7SJeya R 19685c1b97c7SJeya R return err; 19695c1b97c7SJeya R } 19705c1b97c7SJeya R 19715c1b97c7SJeya R static int fastrpc_req_mem_unmap(struct fastrpc_user *fl, char __user *argp) 19725c1b97c7SJeya R { 19735c1b97c7SJeya R struct fastrpc_mem_unmap req; 19745c1b97c7SJeya R 19755c1b97c7SJeya R if (copy_from_user(&req, argp, sizeof(req))) 19765c1b97c7SJeya R return -EFAULT; 19775c1b97c7SJeya R 19785c1b97c7SJeya R return fastrpc_req_mem_unmap_impl(fl, &req); 19795c1b97c7SJeya R } 19805c1b97c7SJeya R 19815c1b97c7SJeya R static int fastrpc_req_mem_map(struct fastrpc_user *fl, char __user *argp) 19825c1b97c7SJeya R { 19835c1b97c7SJeya R struct fastrpc_invoke_args args[4] = { [0 ... 3] = { 0 } }; 19845c1b97c7SJeya R struct fastrpc_mem_map_req_msg req_msg = { 0 }; 19855c1b97c7SJeya R struct fastrpc_mmap_rsp_msg rsp_msg = { 0 }; 19865c1b97c7SJeya R struct fastrpc_mem_unmap req_unmap = { 0 }; 19875c1b97c7SJeya R struct fastrpc_phy_page pages = { 0 }; 19885c1b97c7SJeya R struct fastrpc_mem_map req; 19895c1b97c7SJeya R struct device *dev = fl->sctx->dev; 19905c1b97c7SJeya R struct fastrpc_map *map = NULL; 19915c1b97c7SJeya R int err; 19925c1b97c7SJeya R u32 sc; 19935c1b97c7SJeya R 19945c1b97c7SJeya R if (copy_from_user(&req, argp, sizeof(req))) 19955c1b97c7SJeya R return -EFAULT; 19965c1b97c7SJeya R 19975c1b97c7SJeya R /* create SMMU mapping */ 19983abe3ab3SSrinivas Kandagatla err = fastrpc_map_create(fl, req.fd, req.length, 0, &map); 19995c1b97c7SJeya R if (err) { 20005c1b97c7SJeya R dev_err(dev, "failed to map buffer, fd = %d\n", req.fd); 20015c1b97c7SJeya R return err; 20025c1b97c7SJeya R } 20035c1b97c7SJeya R 20045c1b97c7SJeya R req_msg.pgid = fl->tgid; 20055c1b97c7SJeya R req_msg.fd = req.fd; 20065c1b97c7SJeya R req_msg.offset = req.offset; 20075c1b97c7SJeya R req_msg.vaddrin = req.vaddrin; 20085c1b97c7SJeya R map->va = (void *) (uintptr_t) req.vaddrin; 20095c1b97c7SJeya R req_msg.flags = req.flags; 20105c1b97c7SJeya R req_msg.num = sizeof(pages); 20115c1b97c7SJeya R req_msg.data_len = 0; 20125c1b97c7SJeya R 20135c1b97c7SJeya R args[0].ptr = (u64) (uintptr_t) &req_msg; 20145c1b97c7SJeya R args[0].length = sizeof(req_msg); 20155c1b97c7SJeya R 20165c1b97c7SJeya R pages.addr = map->phys; 20175c1b97c7SJeya R pages.size = map->size; 20185c1b97c7SJeya R 20195c1b97c7SJeya R args[1].ptr = (u64) (uintptr_t) &pages; 20205c1b97c7SJeya R args[1].length = sizeof(pages); 20215c1b97c7SJeya R 20225c1b97c7SJeya R args[2].ptr = (u64) (uintptr_t) &pages; 20235c1b97c7SJeya R args[2].length = 0; 20245c1b97c7SJeya R 20255c1b97c7SJeya R args[3].ptr = (u64) (uintptr_t) &rsp_msg; 20265c1b97c7SJeya R args[3].length = sizeof(rsp_msg); 20275c1b97c7SJeya R 20285c1b97c7SJeya R sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MEM_MAP, 3, 1); 20295c1b97c7SJeya R err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, &args[0]); 20305c1b97c7SJeya R if (err) { 20315c1b97c7SJeya R dev_err(dev, "mem mmap error, fd %d, vaddr %llx, size %lld\n", 20325c1b97c7SJeya R req.fd, req.vaddrin, map->size); 20335c1b97c7SJeya R goto err_invoke; 20345c1b97c7SJeya R } 20355c1b97c7SJeya R 20365c1b97c7SJeya R /* update the buffer to be able to deallocate the memory on the DSP */ 20375c1b97c7SJeya R map->raddr = rsp_msg.vaddr; 20385c1b97c7SJeya R 20395c1b97c7SJeya R /* let the client know the address to use */ 20405c1b97c7SJeya R req.vaddrout = rsp_msg.vaddr; 20415c1b97c7SJeya R 20425c1b97c7SJeya R if (copy_to_user((void __user *)argp, &req, sizeof(req))) { 20435c1b97c7SJeya R /* unmap the memory and release the buffer */ 20445c1b97c7SJeya R req_unmap.vaddr = (uintptr_t) rsp_msg.vaddr; 20455c1b97c7SJeya R req_unmap.length = map->size; 20465c1b97c7SJeya R fastrpc_req_mem_unmap_impl(fl, &req_unmap); 20475c1b97c7SJeya R return -EFAULT; 20485c1b97c7SJeya R } 20495c1b97c7SJeya R 20505c1b97c7SJeya R return 0; 20515c1b97c7SJeya R 20525c1b97c7SJeya R err_invoke: 20535c1b97c7SJeya R fastrpc_map_put(map); 20545c1b97c7SJeya R 20555c1b97c7SJeya R return err; 20565c1b97c7SJeya R } 20575c1b97c7SJeya R 2058c68cfb71SSrinivas Kandagatla static long fastrpc_device_ioctl(struct file *file, unsigned int cmd, 2059c68cfb71SSrinivas Kandagatla unsigned long arg) 2060c68cfb71SSrinivas Kandagatla { 2061c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data; 2062c68cfb71SSrinivas Kandagatla char __user *argp = (char __user *)arg; 2063c68cfb71SSrinivas Kandagatla int err; 2064c68cfb71SSrinivas Kandagatla 2065c68cfb71SSrinivas Kandagatla switch (cmd) { 2066c68cfb71SSrinivas Kandagatla case FASTRPC_IOCTL_INVOKE: 2067c68cfb71SSrinivas Kandagatla err = fastrpc_invoke(fl, argp); 2068c68cfb71SSrinivas Kandagatla break; 2069d73f71c7SSrinivas Kandagatla case FASTRPC_IOCTL_INIT_ATTACH: 20701959ab9eSAbel Vesa err = fastrpc_init_attach(fl, ROOT_PD); 20716010d9beSJonathan Marek break; 20726010d9beSJonathan Marek case FASTRPC_IOCTL_INIT_ATTACH_SNS: 20736010d9beSJonathan Marek err = fastrpc_init_attach(fl, SENSORS_PD); 2074d73f71c7SSrinivas Kandagatla break; 207508715610SAbel Vesa case FASTRPC_IOCTL_INIT_CREATE_STATIC: 207608715610SAbel Vesa err = fastrpc_init_create_static_process(fl, argp); 207708715610SAbel Vesa break; 2078d73f71c7SSrinivas Kandagatla case FASTRPC_IOCTL_INIT_CREATE: 2079d73f71c7SSrinivas Kandagatla err = fastrpc_init_create_process(fl, argp); 2080d73f71c7SSrinivas Kandagatla break; 20816cffd795SSrinivas Kandagatla case FASTRPC_IOCTL_ALLOC_DMA_BUFF: 20826cffd795SSrinivas Kandagatla err = fastrpc_dmabuf_alloc(fl, argp); 20836cffd795SSrinivas Kandagatla break; 20842419e55eSJorge Ramirez-Ortiz case FASTRPC_IOCTL_MMAP: 20852419e55eSJorge Ramirez-Ortiz err = fastrpc_req_mmap(fl, argp); 20862419e55eSJorge Ramirez-Ortiz break; 20872419e55eSJorge Ramirez-Ortiz case FASTRPC_IOCTL_MUNMAP: 20882419e55eSJorge Ramirez-Ortiz err = fastrpc_req_munmap(fl, argp); 20892419e55eSJorge Ramirez-Ortiz break; 20905c1b97c7SJeya R case FASTRPC_IOCTL_MEM_MAP: 20915c1b97c7SJeya R err = fastrpc_req_mem_map(fl, argp); 20925c1b97c7SJeya R break; 20935c1b97c7SJeya R case FASTRPC_IOCTL_MEM_UNMAP: 20945c1b97c7SJeya R err = fastrpc_req_mem_unmap(fl, argp); 20955c1b97c7SJeya R break; 20966c16fd8bSJeya R case FASTRPC_IOCTL_GET_DSP_INFO: 20976c16fd8bSJeya R err = fastrpc_get_dsp_info(fl, argp); 20986c16fd8bSJeya R break; 2099c68cfb71SSrinivas Kandagatla default: 2100c68cfb71SSrinivas Kandagatla err = -ENOTTY; 2101c68cfb71SSrinivas Kandagatla break; 2102c68cfb71SSrinivas Kandagatla } 2103c68cfb71SSrinivas Kandagatla 2104c68cfb71SSrinivas Kandagatla return err; 2105c68cfb71SSrinivas Kandagatla } 2106c68cfb71SSrinivas Kandagatla 2107f6f9279fSSrinivas Kandagatla static const struct file_operations fastrpc_fops = { 2108f6f9279fSSrinivas Kandagatla .open = fastrpc_device_open, 2109f6f9279fSSrinivas Kandagatla .release = fastrpc_device_release, 2110c68cfb71SSrinivas Kandagatla .unlocked_ioctl = fastrpc_device_ioctl, 2111c68cfb71SSrinivas Kandagatla .compat_ioctl = fastrpc_device_ioctl, 2112f6f9279fSSrinivas Kandagatla }; 2113f6f9279fSSrinivas Kandagatla 2114f6f9279fSSrinivas Kandagatla static int fastrpc_cb_probe(struct platform_device *pdev) 2115f6f9279fSSrinivas Kandagatla { 2116f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 2117f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *sess; 2118f6f9279fSSrinivas Kandagatla struct device *dev = &pdev->dev; 2119f6f9279fSSrinivas Kandagatla int i, sessions = 0; 2120977e6c8dSSrinivas Kandagatla unsigned long flags; 212101b76c32SBo YU int rc; 2122f6f9279fSSrinivas Kandagatla 2123f6f9279fSSrinivas Kandagatla cctx = dev_get_drvdata(dev->parent); 2124f6f9279fSSrinivas Kandagatla if (!cctx) 2125f6f9279fSSrinivas Kandagatla return -EINVAL; 2126f6f9279fSSrinivas Kandagatla 2127f6f9279fSSrinivas Kandagatla of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions); 2128f6f9279fSSrinivas Kandagatla 2129977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 21309baa1415SJohan Hovold if (cctx->sesscount >= FASTRPC_MAX_SESSIONS) { 21319baa1415SJohan Hovold dev_err(&pdev->dev, "too many sessions\n"); 21329baa1415SJohan Hovold spin_unlock_irqrestore(&cctx->lock, flags); 21339baa1415SJohan Hovold return -ENOSPC; 21349baa1415SJohan Hovold } 2135d245f43aSJohan Hovold sess = &cctx->session[cctx->sesscount++]; 2136f6f9279fSSrinivas Kandagatla sess->used = false; 2137f6f9279fSSrinivas Kandagatla sess->valid = true; 2138f6f9279fSSrinivas Kandagatla sess->dev = dev; 2139f6f9279fSSrinivas Kandagatla dev_set_drvdata(dev, sess); 2140f6f9279fSSrinivas Kandagatla 2141f6f9279fSSrinivas Kandagatla if (of_property_read_u32(dev->of_node, "reg", &sess->sid)) 2142f6f9279fSSrinivas Kandagatla dev_info(dev, "FastRPC Session ID not specified in DT\n"); 2143f6f9279fSSrinivas Kandagatla 2144f6f9279fSSrinivas Kandagatla if (sessions > 0) { 2145f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *dup_sess; 2146f6f9279fSSrinivas Kandagatla 2147f6f9279fSSrinivas Kandagatla for (i = 1; i < sessions; i++) { 2148d245f43aSJohan Hovold if (cctx->sesscount >= FASTRPC_MAX_SESSIONS) 2149f6f9279fSSrinivas Kandagatla break; 2150d245f43aSJohan Hovold dup_sess = &cctx->session[cctx->sesscount++]; 2151f6f9279fSSrinivas Kandagatla memcpy(dup_sess, sess, sizeof(*dup_sess)); 2152f6f9279fSSrinivas Kandagatla } 2153f6f9279fSSrinivas Kandagatla } 2154977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 215501b76c32SBo YU rc = dma_set_mask(dev, DMA_BIT_MASK(32)); 215601b76c32SBo YU if (rc) { 215701b76c32SBo YU dev_err(dev, "32-bit DMA enable failed\n"); 215801b76c32SBo YU return rc; 215901b76c32SBo YU } 2160f6f9279fSSrinivas Kandagatla 2161f6f9279fSSrinivas Kandagatla return 0; 2162f6f9279fSSrinivas Kandagatla } 2163f6f9279fSSrinivas Kandagatla 2164f6f9279fSSrinivas Kandagatla static int fastrpc_cb_remove(struct platform_device *pdev) 2165f6f9279fSSrinivas Kandagatla { 2166f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent); 2167f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev); 2168977e6c8dSSrinivas Kandagatla unsigned long flags; 2169f6f9279fSSrinivas Kandagatla int i; 2170f6f9279fSSrinivas Kandagatla 2171977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 2172f6f9279fSSrinivas Kandagatla for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) { 2173f6f9279fSSrinivas Kandagatla if (cctx->session[i].sid == sess->sid) { 2174f6f9279fSSrinivas Kandagatla cctx->session[i].valid = false; 2175f6f9279fSSrinivas Kandagatla cctx->sesscount--; 2176f6f9279fSSrinivas Kandagatla } 2177f6f9279fSSrinivas Kandagatla } 2178977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 2179f6f9279fSSrinivas Kandagatla 2180f6f9279fSSrinivas Kandagatla return 0; 2181f6f9279fSSrinivas Kandagatla } 2182f6f9279fSSrinivas Kandagatla 2183f6f9279fSSrinivas Kandagatla static const struct of_device_id fastrpc_match_table[] = { 2184f6f9279fSSrinivas Kandagatla { .compatible = "qcom,fastrpc-compute-cb", }, 2185f6f9279fSSrinivas Kandagatla {} 2186f6f9279fSSrinivas Kandagatla }; 2187f6f9279fSSrinivas Kandagatla 2188f6f9279fSSrinivas Kandagatla static struct platform_driver fastrpc_cb_driver = { 2189f6f9279fSSrinivas Kandagatla .probe = fastrpc_cb_probe, 2190f6f9279fSSrinivas Kandagatla .remove = fastrpc_cb_remove, 2191f6f9279fSSrinivas Kandagatla .driver = { 2192f6f9279fSSrinivas Kandagatla .name = "qcom,fastrpc-cb", 2193f6f9279fSSrinivas Kandagatla .of_match_table = fastrpc_match_table, 2194f6f9279fSSrinivas Kandagatla .suppress_bind_attrs = true, 2195f6f9279fSSrinivas Kandagatla }, 2196f6f9279fSSrinivas Kandagatla }; 2197f6f9279fSSrinivas Kandagatla 2198965602eaSSrinivas Kandagatla static int fastrpc_device_register(struct device *dev, struct fastrpc_channel_ctx *cctx, 21993abe3ab3SSrinivas Kandagatla bool is_secured, const char *domain) 2200965602eaSSrinivas Kandagatla { 2201965602eaSSrinivas Kandagatla struct fastrpc_device *fdev; 2202965602eaSSrinivas Kandagatla int err; 2203965602eaSSrinivas Kandagatla 2204965602eaSSrinivas Kandagatla fdev = devm_kzalloc(dev, sizeof(*fdev), GFP_KERNEL); 2205965602eaSSrinivas Kandagatla if (!fdev) 2206965602eaSSrinivas Kandagatla return -ENOMEM; 2207965602eaSSrinivas Kandagatla 22083abe3ab3SSrinivas Kandagatla fdev->secure = is_secured; 2209965602eaSSrinivas Kandagatla fdev->cctx = cctx; 2210965602eaSSrinivas Kandagatla fdev->miscdev.minor = MISC_DYNAMIC_MINOR; 2211965602eaSSrinivas Kandagatla fdev->miscdev.fops = &fastrpc_fops; 22123abe3ab3SSrinivas Kandagatla fdev->miscdev.name = devm_kasprintf(dev, GFP_KERNEL, "fastrpc-%s%s", 22133abe3ab3SSrinivas Kandagatla domain, is_secured ? "-secure" : ""); 2214965602eaSSrinivas Kandagatla err = misc_register(&fdev->miscdev); 22153abe3ab3SSrinivas Kandagatla if (!err) { 22163abe3ab3SSrinivas Kandagatla if (is_secured) 22173abe3ab3SSrinivas Kandagatla cctx->secure_fdevice = fdev; 22183abe3ab3SSrinivas Kandagatla else 2219965602eaSSrinivas Kandagatla cctx->fdevice = fdev; 22203abe3ab3SSrinivas Kandagatla } 2221965602eaSSrinivas Kandagatla 2222965602eaSSrinivas Kandagatla return err; 2223965602eaSSrinivas Kandagatla } 2224965602eaSSrinivas Kandagatla 2225f6f9279fSSrinivas Kandagatla static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) 2226f6f9279fSSrinivas Kandagatla { 2227f6f9279fSSrinivas Kandagatla struct device *rdev = &rpdev->dev; 2228f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *data; 2229e90d9119SVamsi Krishna Gattupalli int i, err, domain_id = -1, vmcount; 2230f6f9279fSSrinivas Kandagatla const char *domain; 22313abe3ab3SSrinivas Kandagatla bool secure_dsp; 2232e90d9119SVamsi Krishna Gattupalli unsigned int vmids[FASTRPC_MAX_VMIDS]; 2233f6f9279fSSrinivas Kandagatla 2234f6f9279fSSrinivas Kandagatla err = of_property_read_string(rdev->of_node, "label", &domain); 2235f6f9279fSSrinivas Kandagatla if (err) { 2236f6f9279fSSrinivas Kandagatla dev_info(rdev, "FastRPC Domain not specified in DT\n"); 2237f6f9279fSSrinivas Kandagatla return err; 2238f6f9279fSSrinivas Kandagatla } 2239f6f9279fSSrinivas Kandagatla 2240f6f9279fSSrinivas Kandagatla for (i = 0; i <= CDSP_DOMAIN_ID; i++) { 2241f6f9279fSSrinivas Kandagatla if (!strcmp(domains[i], domain)) { 2242f6f9279fSSrinivas Kandagatla domain_id = i; 2243f6f9279fSSrinivas Kandagatla break; 2244f6f9279fSSrinivas Kandagatla } 2245f6f9279fSSrinivas Kandagatla } 2246f6f9279fSSrinivas Kandagatla 2247f6f9279fSSrinivas Kandagatla if (domain_id < 0) { 2248f6f9279fSSrinivas Kandagatla dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id); 2249f6f9279fSSrinivas Kandagatla return -EINVAL; 2250f6f9279fSSrinivas Kandagatla } 2251f6f9279fSSrinivas Kandagatla 22521ce91d45SAbel Vesa if (of_reserved_mem_device_init_by_idx(rdev, rdev->of_node, 0)) 22531ce91d45SAbel Vesa dev_info(rdev, "no reserved DMA memory for FASTRPC\n"); 22541ce91d45SAbel Vesa 2255e90d9119SVamsi Krishna Gattupalli vmcount = of_property_read_variable_u32_array(rdev->of_node, 2256e90d9119SVamsi Krishna Gattupalli "qcom,vmids", &vmids[0], 0, FASTRPC_MAX_VMIDS); 2257e90d9119SVamsi Krishna Gattupalli if (vmcount < 0) 2258e90d9119SVamsi Krishna Gattupalli vmcount = 0; 2259e90d9119SVamsi Krishna Gattupalli else if (!qcom_scm_is_available()) 2260e90d9119SVamsi Krishna Gattupalli return -EPROBE_DEFER; 2261e90d9119SVamsi Krishna Gattupalli 2262278d56f9SBjorn Andersson data = kzalloc(sizeof(*data), GFP_KERNEL); 2263278d56f9SBjorn Andersson if (!data) 2264278d56f9SBjorn Andersson return -ENOMEM; 2265278d56f9SBjorn Andersson 2266e90d9119SVamsi Krishna Gattupalli if (vmcount) { 2267e90d9119SVamsi Krishna Gattupalli data->vmcount = vmcount; 2268e90d9119SVamsi Krishna Gattupalli data->perms = BIT(QCOM_SCM_VMID_HLOS); 2269e90d9119SVamsi Krishna Gattupalli for (i = 0; i < data->vmcount; i++) { 2270e90d9119SVamsi Krishna Gattupalli data->vmperms[i].vmid = vmids[i]; 2271e90d9119SVamsi Krishna Gattupalli data->vmperms[i].perm = QCOM_SCM_PERM_RWX; 2272e90d9119SVamsi Krishna Gattupalli } 2273e90d9119SVamsi Krishna Gattupalli } 22743abe3ab3SSrinivas Kandagatla 22753abe3ab3SSrinivas Kandagatla secure_dsp = !(of_property_read_bool(rdev->of_node, "qcom,non-secure-domain")); 22763abe3ab3SSrinivas Kandagatla data->secure = secure_dsp; 22773abe3ab3SSrinivas Kandagatla 22783abe3ab3SSrinivas Kandagatla switch (domain_id) { 22793abe3ab3SSrinivas Kandagatla case ADSP_DOMAIN_ID: 22803abe3ab3SSrinivas Kandagatla case MDSP_DOMAIN_ID: 22813abe3ab3SSrinivas Kandagatla case SDSP_DOMAIN_ID: 22827f1f4812SJeya R /* Unsigned PD offloading is only supported on CDSP*/ 22837f1f4812SJeya R data->unsigned_support = false; 22843abe3ab3SSrinivas Kandagatla err = fastrpc_device_register(rdev, data, secure_dsp, domains[domain_id]); 22853abe3ab3SSrinivas Kandagatla if (err) 22863abe3ab3SSrinivas Kandagatla goto fdev_error; 22873abe3ab3SSrinivas Kandagatla break; 22883abe3ab3SSrinivas Kandagatla case CDSP_DOMAIN_ID: 22897f1f4812SJeya R data->unsigned_support = true; 22903abe3ab3SSrinivas Kandagatla /* Create both device nodes so that we can allow both Signed and Unsigned PD */ 22913abe3ab3SSrinivas Kandagatla err = fastrpc_device_register(rdev, data, true, domains[domain_id]); 22923abe3ab3SSrinivas Kandagatla if (err) 22933abe3ab3SSrinivas Kandagatla goto fdev_error; 22943abe3ab3SSrinivas Kandagatla 22953abe3ab3SSrinivas Kandagatla err = fastrpc_device_register(rdev, data, false, domains[domain_id]); 22963abe3ab3SSrinivas Kandagatla if (err) 22973abe3ab3SSrinivas Kandagatla goto fdev_error; 22983abe3ab3SSrinivas Kandagatla break; 22993abe3ab3SSrinivas Kandagatla default: 23003abe3ab3SSrinivas Kandagatla err = -EINVAL; 23013abe3ab3SSrinivas Kandagatla goto fdev_error; 23020978de9fSSrinivas Kandagatla } 2303f6f9279fSSrinivas Kandagatla 2304278d56f9SBjorn Andersson kref_init(&data->refcount); 2305278d56f9SBjorn Andersson 2306f6f9279fSSrinivas Kandagatla dev_set_drvdata(&rpdev->dev, data); 2307*9bde43a0SAbel Vesa rdev->dma_mask = &data->dma_mask; 2308f6f9279fSSrinivas Kandagatla dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32)); 2309f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&data->users); 231076e8e4acSAbel Vesa INIT_LIST_HEAD(&data->invoke_interrupted_mmaps); 2311f6f9279fSSrinivas Kandagatla spin_lock_init(&data->lock); 2312f6f9279fSSrinivas Kandagatla idr_init(&data->ctx_idr); 2313f6f9279fSSrinivas Kandagatla data->domain_id = domain_id; 2314f6f9279fSSrinivas Kandagatla data->rpdev = rpdev; 2315f6f9279fSSrinivas Kandagatla 2316f6f9279fSSrinivas Kandagatla return of_platform_populate(rdev->of_node, NULL, NULL, rdev); 23173abe3ab3SSrinivas Kandagatla fdev_error: 23183abe3ab3SSrinivas Kandagatla kfree(data); 23193abe3ab3SSrinivas Kandagatla return err; 2320f6f9279fSSrinivas Kandagatla } 2321f6f9279fSSrinivas Kandagatla 2322c68cfb71SSrinivas Kandagatla static void fastrpc_notify_users(struct fastrpc_user *user) 2323c68cfb71SSrinivas Kandagatla { 2324c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx; 2325c68cfb71SSrinivas Kandagatla 2326c68cfb71SSrinivas Kandagatla spin_lock(&user->lock); 2327c68cfb71SSrinivas Kandagatla list_for_each_entry(ctx, &user->pending, node) 2328c68cfb71SSrinivas Kandagatla complete(&ctx->work); 2329c68cfb71SSrinivas Kandagatla spin_unlock(&user->lock); 2330c68cfb71SSrinivas Kandagatla } 2331c68cfb71SSrinivas Kandagatla 2332f6f9279fSSrinivas Kandagatla static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev) 2333f6f9279fSSrinivas Kandagatla { 2334f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev); 233576e8e4acSAbel Vesa struct fastrpc_buf *buf, *b; 2336c68cfb71SSrinivas Kandagatla struct fastrpc_user *user; 2337977e6c8dSSrinivas Kandagatla unsigned long flags; 2338c68cfb71SSrinivas Kandagatla 2339977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 2340c68cfb71SSrinivas Kandagatla list_for_each_entry(user, &cctx->users, user) 2341c68cfb71SSrinivas Kandagatla fastrpc_notify_users(user); 2342977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 2343f6f9279fSSrinivas Kandagatla 2344965602eaSSrinivas Kandagatla if (cctx->fdevice) 2345965602eaSSrinivas Kandagatla misc_deregister(&cctx->fdevice->miscdev); 2346965602eaSSrinivas Kandagatla 23473abe3ab3SSrinivas Kandagatla if (cctx->secure_fdevice) 23483abe3ab3SSrinivas Kandagatla misc_deregister(&cctx->secure_fdevice->miscdev); 23493abe3ab3SSrinivas Kandagatla 235076e8e4acSAbel Vesa list_for_each_entry_safe(buf, b, &cctx->invoke_interrupted_mmaps, node) 235176e8e4acSAbel Vesa list_del(&buf->node); 235276e8e4acSAbel Vesa 235308715610SAbel Vesa if (cctx->remote_heap) 235408715610SAbel Vesa fastrpc_buf_free(cctx->remote_heap); 235508715610SAbel Vesa 2356f6f9279fSSrinivas Kandagatla of_platform_depopulate(&rpdev->dev); 2357278d56f9SBjorn Andersson 23582e369878SBjorn Andersson cctx->rpdev = NULL; 2359278d56f9SBjorn Andersson fastrpc_channel_ctx_put(cctx); 2360f6f9279fSSrinivas Kandagatla } 2361f6f9279fSSrinivas Kandagatla 2362f6f9279fSSrinivas Kandagatla static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data, 2363f6f9279fSSrinivas Kandagatla int len, void *priv, u32 addr) 2364f6f9279fSSrinivas Kandagatla { 2365c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev); 2366c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_rsp *rsp = data; 2367c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx; 2368c68cfb71SSrinivas Kandagatla unsigned long flags; 2369c68cfb71SSrinivas Kandagatla unsigned long ctxid; 2370c68cfb71SSrinivas Kandagatla 2371c68cfb71SSrinivas Kandagatla if (len < sizeof(*rsp)) 2372c68cfb71SSrinivas Kandagatla return -EINVAL; 2373c68cfb71SSrinivas Kandagatla 2374c68cfb71SSrinivas Kandagatla ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4); 2375c68cfb71SSrinivas Kandagatla 2376c68cfb71SSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 2377c68cfb71SSrinivas Kandagatla ctx = idr_find(&cctx->ctx_idr, ctxid); 2378c68cfb71SSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 2379c68cfb71SSrinivas Kandagatla 2380c68cfb71SSrinivas Kandagatla if (!ctx) { 2381c68cfb71SSrinivas Kandagatla dev_err(&rpdev->dev, "No context ID matches response\n"); 2382c68cfb71SSrinivas Kandagatla return -ENOENT; 2383c68cfb71SSrinivas Kandagatla } 2384c68cfb71SSrinivas Kandagatla 2385c68cfb71SSrinivas Kandagatla ctx->retval = rsp->retval; 2386c68cfb71SSrinivas Kandagatla complete(&ctx->work); 23878e7389c7SThierry Escande 23888e7389c7SThierry Escande /* 23898e7389c7SThierry Escande * The DMA buffer associated with the context cannot be freed in 23908e7389c7SThierry Escande * interrupt context so schedule it through a worker thread to 23918e7389c7SThierry Escande * avoid a kernel BUG. 23928e7389c7SThierry Escande */ 23938e7389c7SThierry Escande schedule_work(&ctx->put_work); 2394c68cfb71SSrinivas Kandagatla 2395f6f9279fSSrinivas Kandagatla return 0; 2396f6f9279fSSrinivas Kandagatla } 2397f6f9279fSSrinivas Kandagatla 2398f6f9279fSSrinivas Kandagatla static const struct of_device_id fastrpc_rpmsg_of_match[] = { 2399f6f9279fSSrinivas Kandagatla { .compatible = "qcom,fastrpc" }, 2400f6f9279fSSrinivas Kandagatla { }, 2401f6f9279fSSrinivas Kandagatla }; 2402f6f9279fSSrinivas Kandagatla MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match); 2403f6f9279fSSrinivas Kandagatla 2404f6f9279fSSrinivas Kandagatla static struct rpmsg_driver fastrpc_driver = { 2405f6f9279fSSrinivas Kandagatla .probe = fastrpc_rpmsg_probe, 2406f6f9279fSSrinivas Kandagatla .remove = fastrpc_rpmsg_remove, 2407f6f9279fSSrinivas Kandagatla .callback = fastrpc_rpmsg_callback, 2408f6f9279fSSrinivas Kandagatla .drv = { 2409f6f9279fSSrinivas Kandagatla .name = "qcom,fastrpc", 2410f6f9279fSSrinivas Kandagatla .of_match_table = fastrpc_rpmsg_of_match, 2411f6f9279fSSrinivas Kandagatla }, 2412f6f9279fSSrinivas Kandagatla }; 2413f6f9279fSSrinivas Kandagatla 2414f6f9279fSSrinivas Kandagatla static int fastrpc_init(void) 2415f6f9279fSSrinivas Kandagatla { 2416f6f9279fSSrinivas Kandagatla int ret; 2417f6f9279fSSrinivas Kandagatla 2418f6f9279fSSrinivas Kandagatla ret = platform_driver_register(&fastrpc_cb_driver); 2419f6f9279fSSrinivas Kandagatla if (ret < 0) { 2420f6f9279fSSrinivas Kandagatla pr_err("fastrpc: failed to register cb driver\n"); 2421f6f9279fSSrinivas Kandagatla return ret; 2422f6f9279fSSrinivas Kandagatla } 2423f6f9279fSSrinivas Kandagatla 2424f6f9279fSSrinivas Kandagatla ret = register_rpmsg_driver(&fastrpc_driver); 2425f6f9279fSSrinivas Kandagatla if (ret < 0) { 2426f6f9279fSSrinivas Kandagatla pr_err("fastrpc: failed to register rpmsg driver\n"); 2427f6f9279fSSrinivas Kandagatla platform_driver_unregister(&fastrpc_cb_driver); 2428f6f9279fSSrinivas Kandagatla return ret; 2429f6f9279fSSrinivas Kandagatla } 2430f6f9279fSSrinivas Kandagatla 2431f6f9279fSSrinivas Kandagatla return 0; 2432f6f9279fSSrinivas Kandagatla } 2433f6f9279fSSrinivas Kandagatla module_init(fastrpc_init); 2434f6f9279fSSrinivas Kandagatla 2435f6f9279fSSrinivas Kandagatla static void fastrpc_exit(void) 2436f6f9279fSSrinivas Kandagatla { 2437f6f9279fSSrinivas Kandagatla platform_driver_unregister(&fastrpc_cb_driver); 2438f6f9279fSSrinivas Kandagatla unregister_rpmsg_driver(&fastrpc_driver); 2439f6f9279fSSrinivas Kandagatla } 2440f6f9279fSSrinivas Kandagatla module_exit(fastrpc_exit); 2441f6f9279fSSrinivas Kandagatla 2442f6f9279fSSrinivas Kandagatla MODULE_LICENSE("GPL v2"); 244316b0314aSGreg Kroah-Hartman MODULE_IMPORT_NS(DMA_BUF); 2444