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> 20c68cfb71SSrinivas Kandagatla #include <uapi/misc/fastrpc.h> 21f6f9279fSSrinivas Kandagatla 22f6f9279fSSrinivas Kandagatla #define ADSP_DOMAIN_ID (0) 23f6f9279fSSrinivas Kandagatla #define MDSP_DOMAIN_ID (1) 24f6f9279fSSrinivas Kandagatla #define SDSP_DOMAIN_ID (2) 25f6f9279fSSrinivas Kandagatla #define CDSP_DOMAIN_ID (3) 26f6f9279fSSrinivas Kandagatla #define FASTRPC_DEV_MAX 4 /* adsp, mdsp, slpi, cdsp*/ 27f6f9279fSSrinivas Kandagatla #define FASTRPC_MAX_SESSIONS 9 /*8 compute, 1 cpz*/ 28c68cfb71SSrinivas Kandagatla #define FASTRPC_ALIGN 128 29c68cfb71SSrinivas Kandagatla #define FASTRPC_MAX_FDLIST 16 30c68cfb71SSrinivas Kandagatla #define FASTRPC_MAX_CRCLIST 64 31c68cfb71SSrinivas Kandagatla #define FASTRPC_PHYS(p) ((p) & 0xffffffff) 32f6f9279fSSrinivas Kandagatla #define FASTRPC_CTX_MAX (256) 33d73f71c7SSrinivas Kandagatla #define FASTRPC_INIT_HANDLE 1 34f6f9279fSSrinivas Kandagatla #define FASTRPC_CTXID_MASK (0xFF0) 35f1cf11c2SSrinivas Kandagatla #define INIT_FILELEN_MAX (64 * 1024 * 1024) 36f6f9279fSSrinivas Kandagatla #define FASTRPC_DEVICE_NAME "fastrpc" 37*2419e55eSJorge Ramirez-Ortiz #define ADSP_MMAP_ADD_PAGES 0x1000 38f6f9279fSSrinivas Kandagatla 39c68cfb71SSrinivas Kandagatla /* Retrives number of input buffers from the scalars parameter */ 40c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_INBUFS(sc) (((sc) >> 16) & 0x0ff) 41c68cfb71SSrinivas Kandagatla 42c68cfb71SSrinivas Kandagatla /* Retrives number of output buffers from the scalars parameter */ 43c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_OUTBUFS(sc) (((sc) >> 8) & 0x0ff) 44c68cfb71SSrinivas Kandagatla 45c68cfb71SSrinivas Kandagatla /* Retrives number of input handles from the scalars parameter */ 46c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_INHANDLES(sc) (((sc) >> 4) & 0x0f) 47c68cfb71SSrinivas Kandagatla 48c68cfb71SSrinivas Kandagatla /* Retrives number of output handles from the scalars parameter */ 49c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_OUTHANDLES(sc) ((sc) & 0x0f) 50c68cfb71SSrinivas Kandagatla 51c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_LENGTH(sc) (REMOTE_SCALARS_INBUFS(sc) + \ 52c68cfb71SSrinivas Kandagatla REMOTE_SCALARS_OUTBUFS(sc) + \ 53c68cfb71SSrinivas Kandagatla REMOTE_SCALARS_INHANDLES(sc)+ \ 54c68cfb71SSrinivas Kandagatla REMOTE_SCALARS_OUTHANDLES(sc)) 55c68cfb71SSrinivas Kandagatla #define FASTRPC_BUILD_SCALARS(attr, method, in, out, oin, oout) \ 56c68cfb71SSrinivas Kandagatla (((attr & 0x07) << 29) | \ 57c68cfb71SSrinivas Kandagatla ((method & 0x1f) << 24) | \ 58c68cfb71SSrinivas Kandagatla ((in & 0xff) << 16) | \ 59c68cfb71SSrinivas Kandagatla ((out & 0xff) << 8) | \ 60c68cfb71SSrinivas Kandagatla ((oin & 0x0f) << 4) | \ 61c68cfb71SSrinivas Kandagatla (oout & 0x0f)) 62c68cfb71SSrinivas Kandagatla 63c68cfb71SSrinivas Kandagatla #define FASTRPC_SCALARS(method, in, out) \ 64c68cfb71SSrinivas Kandagatla FASTRPC_BUILD_SCALARS(0, method, in, out, 0, 0) 65c68cfb71SSrinivas Kandagatla 66d73f71c7SSrinivas Kandagatla #define FASTRPC_CREATE_PROCESS_NARGS 6 67d73f71c7SSrinivas Kandagatla /* Remote Method id table */ 68d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_ATTACH 0 69d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_RELEASE 1 70*2419e55eSJorge Ramirez-Ortiz #define FASTRPC_RMID_INIT_MMAP 4 71*2419e55eSJorge Ramirez-Ortiz #define FASTRPC_RMID_INIT_MUNMAP 5 72d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE 6 73d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE_ATTR 7 74d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE_STATIC 8 75d73f71c7SSrinivas Kandagatla 76f6f9279fSSrinivas Kandagatla #define miscdev_to_cctx(d) container_of(d, struct fastrpc_channel_ctx, miscdev) 77f6f9279fSSrinivas Kandagatla 78f6f9279fSSrinivas Kandagatla static const char *domains[FASTRPC_DEV_MAX] = { "adsp", "mdsp", 79f6f9279fSSrinivas Kandagatla "sdsp", "cdsp"}; 80c68cfb71SSrinivas Kandagatla struct fastrpc_phy_page { 81c68cfb71SSrinivas Kandagatla u64 addr; /* physical address */ 82c68cfb71SSrinivas Kandagatla u64 size; /* size of contiguous region */ 83c68cfb71SSrinivas Kandagatla }; 84c68cfb71SSrinivas Kandagatla 85c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_buf { 86c68cfb71SSrinivas Kandagatla u32 num; /* number of contiguous regions */ 87c68cfb71SSrinivas Kandagatla u32 pgidx; /* index to start of contiguous region */ 88c68cfb71SSrinivas Kandagatla }; 89c68cfb71SSrinivas Kandagatla 90c68cfb71SSrinivas Kandagatla struct fastrpc_remote_arg { 91c68cfb71SSrinivas Kandagatla u64 pv; 92c68cfb71SSrinivas Kandagatla u64 len; 93c68cfb71SSrinivas Kandagatla }; 94c68cfb71SSrinivas Kandagatla 95*2419e55eSJorge Ramirez-Ortiz struct fastrpc_mmap_rsp_msg { 96*2419e55eSJorge Ramirez-Ortiz u64 vaddr; 97*2419e55eSJorge Ramirez-Ortiz }; 98*2419e55eSJorge Ramirez-Ortiz 99*2419e55eSJorge Ramirez-Ortiz struct fastrpc_mmap_req_msg { 100*2419e55eSJorge Ramirez-Ortiz s32 pgid; 101*2419e55eSJorge Ramirez-Ortiz u32 flags; 102*2419e55eSJorge Ramirez-Ortiz u64 vaddr; 103*2419e55eSJorge Ramirez-Ortiz s32 num; 104*2419e55eSJorge Ramirez-Ortiz }; 105*2419e55eSJorge Ramirez-Ortiz 106*2419e55eSJorge Ramirez-Ortiz struct fastrpc_munmap_req_msg { 107*2419e55eSJorge Ramirez-Ortiz s32 pgid; 108*2419e55eSJorge Ramirez-Ortiz u64 vaddr; 109*2419e55eSJorge Ramirez-Ortiz u64 size; 110*2419e55eSJorge Ramirez-Ortiz }; 111*2419e55eSJorge Ramirez-Ortiz 112c68cfb71SSrinivas Kandagatla struct fastrpc_msg { 113c68cfb71SSrinivas Kandagatla int pid; /* process group id */ 114c68cfb71SSrinivas Kandagatla int tid; /* thread id */ 115c68cfb71SSrinivas Kandagatla u64 ctx; /* invoke caller context */ 116c68cfb71SSrinivas Kandagatla u32 handle; /* handle to invoke */ 117c68cfb71SSrinivas Kandagatla u32 sc; /* scalars structure describing the data */ 118c68cfb71SSrinivas Kandagatla u64 addr; /* physical address */ 119c68cfb71SSrinivas Kandagatla u64 size; /* size of contiguous region */ 120c68cfb71SSrinivas Kandagatla }; 121c68cfb71SSrinivas Kandagatla 122c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_rsp { 123c68cfb71SSrinivas Kandagatla u64 ctx; /* invoke caller context */ 124c68cfb71SSrinivas Kandagatla int retval; /* invoke return value */ 125c68cfb71SSrinivas Kandagatla }; 126c68cfb71SSrinivas Kandagatla 12725e8dfb8SSrinivas Kandagatla struct fastrpc_buf_overlap { 12825e8dfb8SSrinivas Kandagatla u64 start; 12925e8dfb8SSrinivas Kandagatla u64 end; 13025e8dfb8SSrinivas Kandagatla int raix; 13125e8dfb8SSrinivas Kandagatla u64 mstart; 13225e8dfb8SSrinivas Kandagatla u64 mend; 13325e8dfb8SSrinivas Kandagatla u64 offset; 13425e8dfb8SSrinivas Kandagatla }; 13525e8dfb8SSrinivas Kandagatla 136c68cfb71SSrinivas Kandagatla struct fastrpc_buf { 137c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl; 1386cffd795SSrinivas Kandagatla struct dma_buf *dmabuf; 139c68cfb71SSrinivas Kandagatla struct device *dev; 140c68cfb71SSrinivas Kandagatla void *virt; 141c68cfb71SSrinivas Kandagatla u64 phys; 142c68cfb71SSrinivas Kandagatla u64 size; 1436cffd795SSrinivas Kandagatla /* Lock for dma buf attachments */ 1446cffd795SSrinivas Kandagatla struct mutex lock; 1456cffd795SSrinivas Kandagatla struct list_head attachments; 146*2419e55eSJorge Ramirez-Ortiz /* mmap support */ 147*2419e55eSJorge Ramirez-Ortiz struct list_head node; /* list of user requested mmaps */ 148*2419e55eSJorge Ramirez-Ortiz uintptr_t raddr; 1496cffd795SSrinivas Kandagatla }; 1506cffd795SSrinivas Kandagatla 1516cffd795SSrinivas Kandagatla struct fastrpc_dma_buf_attachment { 1526cffd795SSrinivas Kandagatla struct device *dev; 1536cffd795SSrinivas Kandagatla struct sg_table sgt; 1546cffd795SSrinivas Kandagatla struct list_head node; 155c68cfb71SSrinivas Kandagatla }; 156c68cfb71SSrinivas Kandagatla 157c68cfb71SSrinivas Kandagatla struct fastrpc_map { 158c68cfb71SSrinivas Kandagatla struct list_head node; 159c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl; 160c68cfb71SSrinivas Kandagatla int fd; 161c68cfb71SSrinivas Kandagatla struct dma_buf *buf; 162c68cfb71SSrinivas Kandagatla struct sg_table *table; 163c68cfb71SSrinivas Kandagatla struct dma_buf_attachment *attach; 164c68cfb71SSrinivas Kandagatla u64 phys; 165c68cfb71SSrinivas Kandagatla u64 size; 166c68cfb71SSrinivas Kandagatla void *va; 167c68cfb71SSrinivas Kandagatla u64 len; 168c68cfb71SSrinivas Kandagatla struct kref refcount; 169c68cfb71SSrinivas Kandagatla }; 170c68cfb71SSrinivas Kandagatla 171c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx { 172c68cfb71SSrinivas Kandagatla int nscalars; 173c68cfb71SSrinivas Kandagatla int nbufs; 174c68cfb71SSrinivas Kandagatla int retval; 175c68cfb71SSrinivas Kandagatla int pid; 176c68cfb71SSrinivas Kandagatla int tgid; 177c68cfb71SSrinivas Kandagatla u32 sc; 178c68cfb71SSrinivas Kandagatla u32 *crc; 179c68cfb71SSrinivas Kandagatla u64 ctxid; 180c68cfb71SSrinivas Kandagatla u64 msg_sz; 181c68cfb71SSrinivas Kandagatla struct kref refcount; 182c68cfb71SSrinivas Kandagatla struct list_head node; /* list of ctxs */ 183c68cfb71SSrinivas Kandagatla struct completion work; 1848e7389c7SThierry Escande struct work_struct put_work; 185c68cfb71SSrinivas Kandagatla struct fastrpc_msg msg; 186c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl; 187c68cfb71SSrinivas Kandagatla struct fastrpc_remote_arg *rpra; 188c68cfb71SSrinivas Kandagatla struct fastrpc_map **maps; 189c68cfb71SSrinivas Kandagatla struct fastrpc_buf *buf; 190c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_args *args; 19125e8dfb8SSrinivas Kandagatla struct fastrpc_buf_overlap *olaps; 192c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 193c68cfb71SSrinivas Kandagatla }; 194f6f9279fSSrinivas Kandagatla 195f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx { 196f6f9279fSSrinivas Kandagatla struct device *dev; 197f6f9279fSSrinivas Kandagatla int sid; 198f6f9279fSSrinivas Kandagatla bool used; 199f6f9279fSSrinivas Kandagatla bool valid; 200f6f9279fSSrinivas Kandagatla }; 201f6f9279fSSrinivas Kandagatla 202f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx { 203f6f9279fSSrinivas Kandagatla int domain_id; 204f6f9279fSSrinivas Kandagatla int sesscount; 205f6f9279fSSrinivas Kandagatla struct rpmsg_device *rpdev; 206f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS]; 207f6f9279fSSrinivas Kandagatla spinlock_t lock; 208f6f9279fSSrinivas Kandagatla struct idr ctx_idr; 209f6f9279fSSrinivas Kandagatla struct list_head users; 210f6f9279fSSrinivas Kandagatla struct miscdevice miscdev; 211278d56f9SBjorn Andersson struct kref refcount; 212f6f9279fSSrinivas Kandagatla }; 213f6f9279fSSrinivas Kandagatla 214f6f9279fSSrinivas Kandagatla struct fastrpc_user { 215f6f9279fSSrinivas Kandagatla struct list_head user; 216f6f9279fSSrinivas Kandagatla struct list_head maps; 217f6f9279fSSrinivas Kandagatla struct list_head pending; 218*2419e55eSJorge Ramirez-Ortiz struct list_head mmaps; 219f6f9279fSSrinivas Kandagatla 220f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 221f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *sctx; 222c68cfb71SSrinivas Kandagatla struct fastrpc_buf *init_mem; 223f6f9279fSSrinivas Kandagatla 224f6f9279fSSrinivas Kandagatla int tgid; 225f6f9279fSSrinivas Kandagatla int pd; 226f6f9279fSSrinivas Kandagatla /* Lock for lists */ 227f6f9279fSSrinivas Kandagatla spinlock_t lock; 228f6f9279fSSrinivas Kandagatla /* lock for allocations */ 229f6f9279fSSrinivas Kandagatla struct mutex mutex; 230f6f9279fSSrinivas Kandagatla }; 231f6f9279fSSrinivas Kandagatla 232c68cfb71SSrinivas Kandagatla static void fastrpc_free_map(struct kref *ref) 233c68cfb71SSrinivas Kandagatla { 234c68cfb71SSrinivas Kandagatla struct fastrpc_map *map; 235c68cfb71SSrinivas Kandagatla 236c68cfb71SSrinivas Kandagatla map = container_of(ref, struct fastrpc_map, refcount); 237c68cfb71SSrinivas Kandagatla 238c68cfb71SSrinivas Kandagatla if (map->table) { 239c68cfb71SSrinivas Kandagatla dma_buf_unmap_attachment(map->attach, map->table, 240c68cfb71SSrinivas Kandagatla DMA_BIDIRECTIONAL); 241c68cfb71SSrinivas Kandagatla dma_buf_detach(map->buf, map->attach); 242c68cfb71SSrinivas Kandagatla dma_buf_put(map->buf); 243c68cfb71SSrinivas Kandagatla } 244c68cfb71SSrinivas Kandagatla 245c68cfb71SSrinivas Kandagatla kfree(map); 246c68cfb71SSrinivas Kandagatla } 247c68cfb71SSrinivas Kandagatla 248c68cfb71SSrinivas Kandagatla static void fastrpc_map_put(struct fastrpc_map *map) 249c68cfb71SSrinivas Kandagatla { 250c68cfb71SSrinivas Kandagatla if (map) 251c68cfb71SSrinivas Kandagatla kref_put(&map->refcount, fastrpc_free_map); 252c68cfb71SSrinivas Kandagatla } 253c68cfb71SSrinivas Kandagatla 254c68cfb71SSrinivas Kandagatla static void fastrpc_map_get(struct fastrpc_map *map) 255c68cfb71SSrinivas Kandagatla { 256c68cfb71SSrinivas Kandagatla if (map) 257c68cfb71SSrinivas Kandagatla kref_get(&map->refcount); 258c68cfb71SSrinivas Kandagatla } 259c68cfb71SSrinivas Kandagatla 260c68cfb71SSrinivas Kandagatla static int fastrpc_map_find(struct fastrpc_user *fl, int fd, 261c68cfb71SSrinivas Kandagatla struct fastrpc_map **ppmap) 262c68cfb71SSrinivas Kandagatla { 263c68cfb71SSrinivas Kandagatla struct fastrpc_map *map = NULL; 264c68cfb71SSrinivas Kandagatla 265c68cfb71SSrinivas Kandagatla mutex_lock(&fl->mutex); 266c68cfb71SSrinivas Kandagatla list_for_each_entry(map, &fl->maps, node) { 267c68cfb71SSrinivas Kandagatla if (map->fd == fd) { 268c68cfb71SSrinivas Kandagatla fastrpc_map_get(map); 269c68cfb71SSrinivas Kandagatla *ppmap = map; 270c68cfb71SSrinivas Kandagatla mutex_unlock(&fl->mutex); 271c68cfb71SSrinivas Kandagatla return 0; 272c68cfb71SSrinivas Kandagatla } 273c68cfb71SSrinivas Kandagatla } 274c68cfb71SSrinivas Kandagatla mutex_unlock(&fl->mutex); 275c68cfb71SSrinivas Kandagatla 276c68cfb71SSrinivas Kandagatla return -ENOENT; 277c68cfb71SSrinivas Kandagatla } 278c68cfb71SSrinivas Kandagatla 279c68cfb71SSrinivas Kandagatla static void fastrpc_buf_free(struct fastrpc_buf *buf) 280c68cfb71SSrinivas Kandagatla { 281c68cfb71SSrinivas Kandagatla dma_free_coherent(buf->dev, buf->size, buf->virt, 282c68cfb71SSrinivas Kandagatla FASTRPC_PHYS(buf->phys)); 283c68cfb71SSrinivas Kandagatla kfree(buf); 284c68cfb71SSrinivas Kandagatla } 285c68cfb71SSrinivas Kandagatla 286c68cfb71SSrinivas Kandagatla static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev, 287c68cfb71SSrinivas Kandagatla u64 size, struct fastrpc_buf **obuf) 288c68cfb71SSrinivas Kandagatla { 289c68cfb71SSrinivas Kandagatla struct fastrpc_buf *buf; 290c68cfb71SSrinivas Kandagatla 291c68cfb71SSrinivas Kandagatla buf = kzalloc(sizeof(*buf), GFP_KERNEL); 292c68cfb71SSrinivas Kandagatla if (!buf) 293c68cfb71SSrinivas Kandagatla return -ENOMEM; 294c68cfb71SSrinivas Kandagatla 2956cffd795SSrinivas Kandagatla INIT_LIST_HEAD(&buf->attachments); 296*2419e55eSJorge Ramirez-Ortiz INIT_LIST_HEAD(&buf->node); 2976cffd795SSrinivas Kandagatla mutex_init(&buf->lock); 2986cffd795SSrinivas Kandagatla 299c68cfb71SSrinivas Kandagatla buf->fl = fl; 300c68cfb71SSrinivas Kandagatla buf->virt = NULL; 301c68cfb71SSrinivas Kandagatla buf->phys = 0; 302c68cfb71SSrinivas Kandagatla buf->size = size; 303c68cfb71SSrinivas Kandagatla buf->dev = dev; 304*2419e55eSJorge Ramirez-Ortiz buf->raddr = 0; 305c68cfb71SSrinivas Kandagatla 306c68cfb71SSrinivas Kandagatla buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys, 307c68cfb71SSrinivas Kandagatla GFP_KERNEL); 30841db5f83SJorge Ramirez-Ortiz if (!buf->virt) { 30941db5f83SJorge Ramirez-Ortiz mutex_destroy(&buf->lock); 31041db5f83SJorge Ramirez-Ortiz kfree(buf); 311c68cfb71SSrinivas Kandagatla return -ENOMEM; 31241db5f83SJorge Ramirez-Ortiz } 313c68cfb71SSrinivas Kandagatla 314c68cfb71SSrinivas Kandagatla if (fl->sctx && fl->sctx->sid) 315c68cfb71SSrinivas Kandagatla buf->phys += ((u64)fl->sctx->sid << 32); 316c68cfb71SSrinivas Kandagatla 317c68cfb71SSrinivas Kandagatla *obuf = buf; 318c68cfb71SSrinivas Kandagatla 319c68cfb71SSrinivas Kandagatla return 0; 320c68cfb71SSrinivas Kandagatla } 321c68cfb71SSrinivas Kandagatla 322278d56f9SBjorn Andersson static void fastrpc_channel_ctx_free(struct kref *ref) 323278d56f9SBjorn Andersson { 324278d56f9SBjorn Andersson struct fastrpc_channel_ctx *cctx; 325278d56f9SBjorn Andersson 326278d56f9SBjorn Andersson cctx = container_of(ref, struct fastrpc_channel_ctx, refcount); 327278d56f9SBjorn Andersson 328278d56f9SBjorn Andersson kfree(cctx); 329278d56f9SBjorn Andersson } 330278d56f9SBjorn Andersson 331278d56f9SBjorn Andersson static void fastrpc_channel_ctx_get(struct fastrpc_channel_ctx *cctx) 332278d56f9SBjorn Andersson { 333278d56f9SBjorn Andersson kref_get(&cctx->refcount); 334278d56f9SBjorn Andersson } 335278d56f9SBjorn Andersson 336278d56f9SBjorn Andersson static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx) 337278d56f9SBjorn Andersson { 338278d56f9SBjorn Andersson kref_put(&cctx->refcount, fastrpc_channel_ctx_free); 339278d56f9SBjorn Andersson } 340278d56f9SBjorn Andersson 341c68cfb71SSrinivas Kandagatla static void fastrpc_context_free(struct kref *ref) 342c68cfb71SSrinivas Kandagatla { 343c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx; 344c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 345977e6c8dSSrinivas Kandagatla unsigned long flags; 346c68cfb71SSrinivas Kandagatla int i; 347c68cfb71SSrinivas Kandagatla 348c68cfb71SSrinivas Kandagatla ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount); 349c68cfb71SSrinivas Kandagatla cctx = ctx->cctx; 350c68cfb71SSrinivas Kandagatla 351c68cfb71SSrinivas Kandagatla for (i = 0; i < ctx->nscalars; i++) 352c68cfb71SSrinivas Kandagatla fastrpc_map_put(ctx->maps[i]); 353c68cfb71SSrinivas Kandagatla 354c68cfb71SSrinivas Kandagatla if (ctx->buf) 355c68cfb71SSrinivas Kandagatla fastrpc_buf_free(ctx->buf); 356c68cfb71SSrinivas Kandagatla 357977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 358c68cfb71SSrinivas Kandagatla idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4); 359977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 360c68cfb71SSrinivas Kandagatla 361c68cfb71SSrinivas Kandagatla kfree(ctx->maps); 36225e8dfb8SSrinivas Kandagatla kfree(ctx->olaps); 363c68cfb71SSrinivas Kandagatla kfree(ctx); 364278d56f9SBjorn Andersson 365278d56f9SBjorn Andersson fastrpc_channel_ctx_put(cctx); 366c68cfb71SSrinivas Kandagatla } 367c68cfb71SSrinivas Kandagatla 368c68cfb71SSrinivas Kandagatla static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx) 369c68cfb71SSrinivas Kandagatla { 370c68cfb71SSrinivas Kandagatla kref_get(&ctx->refcount); 371c68cfb71SSrinivas Kandagatla } 372c68cfb71SSrinivas Kandagatla 373c68cfb71SSrinivas Kandagatla static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx) 374c68cfb71SSrinivas Kandagatla { 375c68cfb71SSrinivas Kandagatla kref_put(&ctx->refcount, fastrpc_context_free); 376c68cfb71SSrinivas Kandagatla } 377c68cfb71SSrinivas Kandagatla 3788e7389c7SThierry Escande static void fastrpc_context_put_wq(struct work_struct *work) 3798e7389c7SThierry Escande { 3808e7389c7SThierry Escande struct fastrpc_invoke_ctx *ctx = 3818e7389c7SThierry Escande container_of(work, struct fastrpc_invoke_ctx, put_work); 3828e7389c7SThierry Escande 3838e7389c7SThierry Escande fastrpc_context_put(ctx); 3848e7389c7SThierry Escande } 3858e7389c7SThierry Escande 38625e8dfb8SSrinivas Kandagatla #define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1) 38725e8dfb8SSrinivas Kandagatla static int olaps_cmp(const void *a, const void *b) 38825e8dfb8SSrinivas Kandagatla { 38925e8dfb8SSrinivas Kandagatla struct fastrpc_buf_overlap *pa = (struct fastrpc_buf_overlap *)a; 39025e8dfb8SSrinivas Kandagatla struct fastrpc_buf_overlap *pb = (struct fastrpc_buf_overlap *)b; 39125e8dfb8SSrinivas Kandagatla /* sort with lowest starting buffer first */ 39225e8dfb8SSrinivas Kandagatla int st = CMP(pa->start, pb->start); 39325e8dfb8SSrinivas Kandagatla /* sort with highest ending buffer first */ 39425e8dfb8SSrinivas Kandagatla int ed = CMP(pb->end, pa->end); 39525e8dfb8SSrinivas Kandagatla 39625e8dfb8SSrinivas Kandagatla return st == 0 ? ed : st; 39725e8dfb8SSrinivas Kandagatla } 39825e8dfb8SSrinivas Kandagatla 39925e8dfb8SSrinivas Kandagatla static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx) 40025e8dfb8SSrinivas Kandagatla { 40125e8dfb8SSrinivas Kandagatla u64 max_end = 0; 40225e8dfb8SSrinivas Kandagatla int i; 40325e8dfb8SSrinivas Kandagatla 40425e8dfb8SSrinivas Kandagatla for (i = 0; i < ctx->nbufs; ++i) { 40525e8dfb8SSrinivas Kandagatla ctx->olaps[i].start = ctx->args[i].ptr; 40625e8dfb8SSrinivas Kandagatla ctx->olaps[i].end = ctx->olaps[i].start + ctx->args[i].length; 40725e8dfb8SSrinivas Kandagatla ctx->olaps[i].raix = i; 40825e8dfb8SSrinivas Kandagatla } 40925e8dfb8SSrinivas Kandagatla 41025e8dfb8SSrinivas Kandagatla sort(ctx->olaps, ctx->nbufs, sizeof(*ctx->olaps), olaps_cmp, NULL); 41125e8dfb8SSrinivas Kandagatla 41225e8dfb8SSrinivas Kandagatla for (i = 0; i < ctx->nbufs; ++i) { 41325e8dfb8SSrinivas Kandagatla /* Falling inside previous range */ 41425e8dfb8SSrinivas Kandagatla if (ctx->olaps[i].start < max_end) { 41525e8dfb8SSrinivas Kandagatla ctx->olaps[i].mstart = max_end; 41625e8dfb8SSrinivas Kandagatla ctx->olaps[i].mend = ctx->olaps[i].end; 41725e8dfb8SSrinivas Kandagatla ctx->olaps[i].offset = max_end - ctx->olaps[i].start; 41825e8dfb8SSrinivas Kandagatla 41925e8dfb8SSrinivas Kandagatla if (ctx->olaps[i].end > max_end) { 42025e8dfb8SSrinivas Kandagatla max_end = ctx->olaps[i].end; 42125e8dfb8SSrinivas Kandagatla } else { 42225e8dfb8SSrinivas Kandagatla ctx->olaps[i].mend = 0; 42325e8dfb8SSrinivas Kandagatla ctx->olaps[i].mstart = 0; 42425e8dfb8SSrinivas Kandagatla } 42525e8dfb8SSrinivas Kandagatla 42625e8dfb8SSrinivas Kandagatla } else { 42725e8dfb8SSrinivas Kandagatla ctx->olaps[i].mend = ctx->olaps[i].end; 42825e8dfb8SSrinivas Kandagatla ctx->olaps[i].mstart = ctx->olaps[i].start; 42925e8dfb8SSrinivas Kandagatla ctx->olaps[i].offset = 0; 43025e8dfb8SSrinivas Kandagatla max_end = ctx->olaps[i].end; 43125e8dfb8SSrinivas Kandagatla } 43225e8dfb8SSrinivas Kandagatla } 43325e8dfb8SSrinivas Kandagatla } 43425e8dfb8SSrinivas Kandagatla 435c68cfb71SSrinivas Kandagatla static struct fastrpc_invoke_ctx *fastrpc_context_alloc( 436c68cfb71SSrinivas Kandagatla struct fastrpc_user *user, u32 kernel, u32 sc, 437c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_args *args) 438c68cfb71SSrinivas Kandagatla { 439c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = user->cctx; 440c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx = NULL; 441977e6c8dSSrinivas Kandagatla unsigned long flags; 442c68cfb71SSrinivas Kandagatla int ret; 443c68cfb71SSrinivas Kandagatla 444c68cfb71SSrinivas Kandagatla ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 445c68cfb71SSrinivas Kandagatla if (!ctx) 446c68cfb71SSrinivas Kandagatla return ERR_PTR(-ENOMEM); 447c68cfb71SSrinivas Kandagatla 448c68cfb71SSrinivas Kandagatla INIT_LIST_HEAD(&ctx->node); 449c68cfb71SSrinivas Kandagatla ctx->fl = user; 450c68cfb71SSrinivas Kandagatla ctx->nscalars = REMOTE_SCALARS_LENGTH(sc); 451c68cfb71SSrinivas Kandagatla ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) + 452c68cfb71SSrinivas Kandagatla REMOTE_SCALARS_OUTBUFS(sc); 453c68cfb71SSrinivas Kandagatla 454c68cfb71SSrinivas Kandagatla if (ctx->nscalars) { 455c68cfb71SSrinivas Kandagatla ctx->maps = kcalloc(ctx->nscalars, 456c68cfb71SSrinivas Kandagatla sizeof(*ctx->maps), GFP_KERNEL); 457c68cfb71SSrinivas Kandagatla if (!ctx->maps) { 458c68cfb71SSrinivas Kandagatla kfree(ctx); 459c68cfb71SSrinivas Kandagatla return ERR_PTR(-ENOMEM); 460c68cfb71SSrinivas Kandagatla } 46125e8dfb8SSrinivas Kandagatla ctx->olaps = kcalloc(ctx->nscalars, 46225e8dfb8SSrinivas Kandagatla sizeof(*ctx->olaps), GFP_KERNEL); 46325e8dfb8SSrinivas Kandagatla if (!ctx->olaps) { 46425e8dfb8SSrinivas Kandagatla kfree(ctx->maps); 46525e8dfb8SSrinivas Kandagatla kfree(ctx); 46625e8dfb8SSrinivas Kandagatla return ERR_PTR(-ENOMEM); 46725e8dfb8SSrinivas Kandagatla } 468c68cfb71SSrinivas Kandagatla ctx->args = args; 46925e8dfb8SSrinivas Kandagatla fastrpc_get_buff_overlaps(ctx); 470c68cfb71SSrinivas Kandagatla } 471c68cfb71SSrinivas Kandagatla 472278d56f9SBjorn Andersson /* Released in fastrpc_context_put() */ 473278d56f9SBjorn Andersson fastrpc_channel_ctx_get(cctx); 474278d56f9SBjorn Andersson 475c68cfb71SSrinivas Kandagatla ctx->sc = sc; 476c68cfb71SSrinivas Kandagatla ctx->retval = -1; 477c68cfb71SSrinivas Kandagatla ctx->pid = current->pid; 478c68cfb71SSrinivas Kandagatla ctx->tgid = user->tgid; 479c68cfb71SSrinivas Kandagatla ctx->cctx = cctx; 480c68cfb71SSrinivas Kandagatla init_completion(&ctx->work); 4818e7389c7SThierry Escande INIT_WORK(&ctx->put_work, fastrpc_context_put_wq); 482c68cfb71SSrinivas Kandagatla 483c68cfb71SSrinivas Kandagatla spin_lock(&user->lock); 484c68cfb71SSrinivas Kandagatla list_add_tail(&ctx->node, &user->pending); 485c68cfb71SSrinivas Kandagatla spin_unlock(&user->lock); 486c68cfb71SSrinivas Kandagatla 487977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 488c68cfb71SSrinivas Kandagatla ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1, 489c68cfb71SSrinivas Kandagatla FASTRPC_CTX_MAX, GFP_ATOMIC); 490c68cfb71SSrinivas Kandagatla if (ret < 0) { 491977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 492c68cfb71SSrinivas Kandagatla goto err_idr; 493c68cfb71SSrinivas Kandagatla } 494c68cfb71SSrinivas Kandagatla ctx->ctxid = ret << 4; 495977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 496c68cfb71SSrinivas Kandagatla 497c68cfb71SSrinivas Kandagatla kref_init(&ctx->refcount); 498c68cfb71SSrinivas Kandagatla 499c68cfb71SSrinivas Kandagatla return ctx; 500c68cfb71SSrinivas Kandagatla err_idr: 501c68cfb71SSrinivas Kandagatla spin_lock(&user->lock); 502c68cfb71SSrinivas Kandagatla list_del(&ctx->node); 503c68cfb71SSrinivas Kandagatla spin_unlock(&user->lock); 504278d56f9SBjorn Andersson fastrpc_channel_ctx_put(cctx); 505c68cfb71SSrinivas Kandagatla kfree(ctx->maps); 50625e8dfb8SSrinivas Kandagatla kfree(ctx->olaps); 507c68cfb71SSrinivas Kandagatla kfree(ctx); 508c68cfb71SSrinivas Kandagatla 509c68cfb71SSrinivas Kandagatla return ERR_PTR(ret); 510c68cfb71SSrinivas Kandagatla } 511c68cfb71SSrinivas Kandagatla 5126cffd795SSrinivas Kandagatla static struct sg_table * 5136cffd795SSrinivas Kandagatla fastrpc_map_dma_buf(struct dma_buf_attachment *attachment, 5146cffd795SSrinivas Kandagatla enum dma_data_direction dir) 5156cffd795SSrinivas Kandagatla { 5166cffd795SSrinivas Kandagatla struct fastrpc_dma_buf_attachment *a = attachment->priv; 5176cffd795SSrinivas Kandagatla struct sg_table *table; 5186cffd795SSrinivas Kandagatla 5196cffd795SSrinivas Kandagatla table = &a->sgt; 5206cffd795SSrinivas Kandagatla 5216cffd795SSrinivas Kandagatla if (!dma_map_sg(attachment->dev, table->sgl, table->nents, dir)) 5226cffd795SSrinivas Kandagatla return ERR_PTR(-ENOMEM); 5236cffd795SSrinivas Kandagatla 5246cffd795SSrinivas Kandagatla return table; 5256cffd795SSrinivas Kandagatla } 5266cffd795SSrinivas Kandagatla 5276cffd795SSrinivas Kandagatla static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach, 5286cffd795SSrinivas Kandagatla struct sg_table *table, 5296cffd795SSrinivas Kandagatla enum dma_data_direction dir) 5306cffd795SSrinivas Kandagatla { 5316cffd795SSrinivas Kandagatla dma_unmap_sg(attach->dev, table->sgl, table->nents, dir); 5326cffd795SSrinivas Kandagatla } 5336cffd795SSrinivas Kandagatla 5346cffd795SSrinivas Kandagatla static void fastrpc_release(struct dma_buf *dmabuf) 5356cffd795SSrinivas Kandagatla { 5366cffd795SSrinivas Kandagatla struct fastrpc_buf *buffer = dmabuf->priv; 5376cffd795SSrinivas Kandagatla 5386cffd795SSrinivas Kandagatla fastrpc_buf_free(buffer); 5396cffd795SSrinivas Kandagatla } 5406cffd795SSrinivas Kandagatla 5416cffd795SSrinivas Kandagatla static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf, 5426cffd795SSrinivas Kandagatla struct dma_buf_attachment *attachment) 5436cffd795SSrinivas Kandagatla { 5446cffd795SSrinivas Kandagatla struct fastrpc_dma_buf_attachment *a; 5456cffd795SSrinivas Kandagatla struct fastrpc_buf *buffer = dmabuf->priv; 5466cffd795SSrinivas Kandagatla int ret; 5476cffd795SSrinivas Kandagatla 5486cffd795SSrinivas Kandagatla a = kzalloc(sizeof(*a), GFP_KERNEL); 5496cffd795SSrinivas Kandagatla if (!a) 5506cffd795SSrinivas Kandagatla return -ENOMEM; 5516cffd795SSrinivas Kandagatla 5526cffd795SSrinivas Kandagatla ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt, 5536cffd795SSrinivas Kandagatla FASTRPC_PHYS(buffer->phys), buffer->size); 5546cffd795SSrinivas Kandagatla if (ret < 0) { 5556cffd795SSrinivas Kandagatla dev_err(buffer->dev, "failed to get scatterlist from DMA API\n"); 5566cffd795SSrinivas Kandagatla return -EINVAL; 5576cffd795SSrinivas Kandagatla } 5586cffd795SSrinivas Kandagatla 5596cffd795SSrinivas Kandagatla a->dev = attachment->dev; 5606cffd795SSrinivas Kandagatla INIT_LIST_HEAD(&a->node); 5616cffd795SSrinivas Kandagatla attachment->priv = a; 5626cffd795SSrinivas Kandagatla 5636cffd795SSrinivas Kandagatla mutex_lock(&buffer->lock); 5646cffd795SSrinivas Kandagatla list_add(&a->node, &buffer->attachments); 5656cffd795SSrinivas Kandagatla mutex_unlock(&buffer->lock); 5666cffd795SSrinivas Kandagatla 5676cffd795SSrinivas Kandagatla return 0; 5686cffd795SSrinivas Kandagatla } 5696cffd795SSrinivas Kandagatla 5706cffd795SSrinivas Kandagatla static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf, 5716cffd795SSrinivas Kandagatla struct dma_buf_attachment *attachment) 5726cffd795SSrinivas Kandagatla { 5736cffd795SSrinivas Kandagatla struct fastrpc_dma_buf_attachment *a = attachment->priv; 5746cffd795SSrinivas Kandagatla struct fastrpc_buf *buffer = dmabuf->priv; 5756cffd795SSrinivas Kandagatla 5766cffd795SSrinivas Kandagatla mutex_lock(&buffer->lock); 5776cffd795SSrinivas Kandagatla list_del(&a->node); 5786cffd795SSrinivas Kandagatla mutex_unlock(&buffer->lock); 579cf61860eSSrinivas Kandagatla sg_free_table(&a->sgt); 5806cffd795SSrinivas Kandagatla kfree(a); 5816cffd795SSrinivas Kandagatla } 5826cffd795SSrinivas Kandagatla 5836cffd795SSrinivas Kandagatla static void *fastrpc_kmap(struct dma_buf *dmabuf, unsigned long pgnum) 5846cffd795SSrinivas Kandagatla { 5856cffd795SSrinivas Kandagatla struct fastrpc_buf *buf = dmabuf->priv; 5866cffd795SSrinivas Kandagatla 5876cffd795SSrinivas Kandagatla return buf->virt ? buf->virt + pgnum * PAGE_SIZE : NULL; 5886cffd795SSrinivas Kandagatla } 5896cffd795SSrinivas Kandagatla 5906cffd795SSrinivas Kandagatla static void *fastrpc_vmap(struct dma_buf *dmabuf) 5916cffd795SSrinivas Kandagatla { 5926cffd795SSrinivas Kandagatla struct fastrpc_buf *buf = dmabuf->priv; 5936cffd795SSrinivas Kandagatla 5946cffd795SSrinivas Kandagatla return buf->virt; 5956cffd795SSrinivas Kandagatla } 5966cffd795SSrinivas Kandagatla 5976cffd795SSrinivas Kandagatla static int fastrpc_mmap(struct dma_buf *dmabuf, 5986cffd795SSrinivas Kandagatla struct vm_area_struct *vma) 5996cffd795SSrinivas Kandagatla { 6006cffd795SSrinivas Kandagatla struct fastrpc_buf *buf = dmabuf->priv; 6016cffd795SSrinivas Kandagatla size_t size = vma->vm_end - vma->vm_start; 6026cffd795SSrinivas Kandagatla 6036cffd795SSrinivas Kandagatla return dma_mmap_coherent(buf->dev, vma, buf->virt, 6046cffd795SSrinivas Kandagatla FASTRPC_PHYS(buf->phys), size); 6056cffd795SSrinivas Kandagatla } 6066cffd795SSrinivas Kandagatla 6076cffd795SSrinivas Kandagatla static const struct dma_buf_ops fastrpc_dma_buf_ops = { 6086cffd795SSrinivas Kandagatla .attach = fastrpc_dma_buf_attach, 6096cffd795SSrinivas Kandagatla .detach = fastrpc_dma_buf_detatch, 6106cffd795SSrinivas Kandagatla .map_dma_buf = fastrpc_map_dma_buf, 6116cffd795SSrinivas Kandagatla .unmap_dma_buf = fastrpc_unmap_dma_buf, 6126cffd795SSrinivas Kandagatla .mmap = fastrpc_mmap, 6136cffd795SSrinivas Kandagatla .map = fastrpc_kmap, 6146cffd795SSrinivas Kandagatla .vmap = fastrpc_vmap, 6156cffd795SSrinivas Kandagatla .release = fastrpc_release, 6166cffd795SSrinivas Kandagatla }; 6176cffd795SSrinivas Kandagatla 618c68cfb71SSrinivas Kandagatla static int fastrpc_map_create(struct fastrpc_user *fl, int fd, 619c68cfb71SSrinivas Kandagatla u64 len, struct fastrpc_map **ppmap) 620c68cfb71SSrinivas Kandagatla { 621c68cfb71SSrinivas Kandagatla struct fastrpc_session_ctx *sess = fl->sctx; 622c68cfb71SSrinivas Kandagatla struct fastrpc_map *map = NULL; 623c68cfb71SSrinivas Kandagatla int err = 0; 624c68cfb71SSrinivas Kandagatla 625c68cfb71SSrinivas Kandagatla if (!fastrpc_map_find(fl, fd, ppmap)) 626c68cfb71SSrinivas Kandagatla return 0; 627c68cfb71SSrinivas Kandagatla 628c68cfb71SSrinivas Kandagatla map = kzalloc(sizeof(*map), GFP_KERNEL); 629c68cfb71SSrinivas Kandagatla if (!map) 630c68cfb71SSrinivas Kandagatla return -ENOMEM; 631c68cfb71SSrinivas Kandagatla 632c68cfb71SSrinivas Kandagatla INIT_LIST_HEAD(&map->node); 633c68cfb71SSrinivas Kandagatla map->fl = fl; 634c68cfb71SSrinivas Kandagatla map->fd = fd; 635c68cfb71SSrinivas Kandagatla map->buf = dma_buf_get(fd); 636682a6044SWei Yongjun if (IS_ERR(map->buf)) { 637682a6044SWei Yongjun err = PTR_ERR(map->buf); 638c68cfb71SSrinivas Kandagatla goto get_err; 639c68cfb71SSrinivas Kandagatla } 640c68cfb71SSrinivas Kandagatla 641c68cfb71SSrinivas Kandagatla map->attach = dma_buf_attach(map->buf, sess->dev); 642c68cfb71SSrinivas Kandagatla if (IS_ERR(map->attach)) { 643c68cfb71SSrinivas Kandagatla dev_err(sess->dev, "Failed to attach dmabuf\n"); 644c68cfb71SSrinivas Kandagatla err = PTR_ERR(map->attach); 645c68cfb71SSrinivas Kandagatla goto attach_err; 646c68cfb71SSrinivas Kandagatla } 647c68cfb71SSrinivas Kandagatla 648c68cfb71SSrinivas Kandagatla map->table = dma_buf_map_attachment(map->attach, DMA_BIDIRECTIONAL); 649c68cfb71SSrinivas Kandagatla if (IS_ERR(map->table)) { 650c68cfb71SSrinivas Kandagatla err = PTR_ERR(map->table); 651c68cfb71SSrinivas Kandagatla goto map_err; 652c68cfb71SSrinivas Kandagatla } 653c68cfb71SSrinivas Kandagatla 654c68cfb71SSrinivas Kandagatla map->phys = sg_dma_address(map->table->sgl); 655c68cfb71SSrinivas Kandagatla map->phys += ((u64)fl->sctx->sid << 32); 656c68cfb71SSrinivas Kandagatla map->size = len; 657c68cfb71SSrinivas Kandagatla map->va = sg_virt(map->table->sgl); 658c68cfb71SSrinivas Kandagatla map->len = len; 659c68cfb71SSrinivas Kandagatla kref_init(&map->refcount); 660c68cfb71SSrinivas Kandagatla 661c68cfb71SSrinivas Kandagatla spin_lock(&fl->lock); 662c68cfb71SSrinivas Kandagatla list_add_tail(&map->node, &fl->maps); 663c68cfb71SSrinivas Kandagatla spin_unlock(&fl->lock); 664c68cfb71SSrinivas Kandagatla *ppmap = map; 665c68cfb71SSrinivas Kandagatla 666c68cfb71SSrinivas Kandagatla return 0; 667c68cfb71SSrinivas Kandagatla 668c68cfb71SSrinivas Kandagatla map_err: 669c68cfb71SSrinivas Kandagatla dma_buf_detach(map->buf, map->attach); 670c68cfb71SSrinivas Kandagatla attach_err: 671c68cfb71SSrinivas Kandagatla dma_buf_put(map->buf); 672c68cfb71SSrinivas Kandagatla get_err: 673c68cfb71SSrinivas Kandagatla kfree(map); 674c68cfb71SSrinivas Kandagatla 675c68cfb71SSrinivas Kandagatla return err; 676c68cfb71SSrinivas Kandagatla } 677c68cfb71SSrinivas Kandagatla 678c68cfb71SSrinivas Kandagatla /* 679c68cfb71SSrinivas Kandagatla * Fastrpc payload buffer with metadata looks like: 680c68cfb71SSrinivas Kandagatla * 681c68cfb71SSrinivas Kandagatla * >>>>>> START of METADATA <<<<<<<<< 682c68cfb71SSrinivas Kandagatla * +---------------------------------+ 683c68cfb71SSrinivas Kandagatla * | Arguments | 684c68cfb71SSrinivas Kandagatla * | type:(struct fastrpc_remote_arg)| 685c68cfb71SSrinivas Kandagatla * | (0 - N) | 686c68cfb71SSrinivas Kandagatla * +---------------------------------+ 687c68cfb71SSrinivas Kandagatla * | Invoke Buffer list | 688c68cfb71SSrinivas Kandagatla * | type:(struct fastrpc_invoke_buf)| 689c68cfb71SSrinivas Kandagatla * | (0 - N) | 690c68cfb71SSrinivas Kandagatla * +---------------------------------+ 691c68cfb71SSrinivas Kandagatla * | Page info list | 692c68cfb71SSrinivas Kandagatla * | type:(struct fastrpc_phy_page) | 693c68cfb71SSrinivas Kandagatla * | (0 - N) | 694c68cfb71SSrinivas Kandagatla * +---------------------------------+ 695c68cfb71SSrinivas Kandagatla * | Optional info | 696c68cfb71SSrinivas Kandagatla * |(can be specific to SoC/Firmware)| 697c68cfb71SSrinivas Kandagatla * +---------------------------------+ 698c68cfb71SSrinivas Kandagatla * >>>>>>>> END of METADATA <<<<<<<<< 699c68cfb71SSrinivas Kandagatla * +---------------------------------+ 700c68cfb71SSrinivas Kandagatla * | Inline ARGS | 701c68cfb71SSrinivas Kandagatla * | (0-N) | 702c68cfb71SSrinivas Kandagatla * +---------------------------------+ 703c68cfb71SSrinivas Kandagatla */ 704c68cfb71SSrinivas Kandagatla 705c68cfb71SSrinivas Kandagatla static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx) 706c68cfb71SSrinivas Kandagatla { 707c68cfb71SSrinivas Kandagatla int size = 0; 708c68cfb71SSrinivas Kandagatla 709c68cfb71SSrinivas Kandagatla size = (sizeof(struct fastrpc_remote_arg) + 710c68cfb71SSrinivas Kandagatla sizeof(struct fastrpc_invoke_buf) + 711c68cfb71SSrinivas Kandagatla sizeof(struct fastrpc_phy_page)) * ctx->nscalars + 712c68cfb71SSrinivas Kandagatla sizeof(u64) * FASTRPC_MAX_FDLIST + 713c68cfb71SSrinivas Kandagatla sizeof(u32) * FASTRPC_MAX_CRCLIST; 714c68cfb71SSrinivas Kandagatla 715c68cfb71SSrinivas Kandagatla return size; 716c68cfb71SSrinivas Kandagatla } 717c68cfb71SSrinivas Kandagatla 718c68cfb71SSrinivas Kandagatla static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen) 719c68cfb71SSrinivas Kandagatla { 720c68cfb71SSrinivas Kandagatla u64 size = 0; 721c68cfb71SSrinivas Kandagatla int i; 722c68cfb71SSrinivas Kandagatla 723c68cfb71SSrinivas Kandagatla size = ALIGN(metalen, FASTRPC_ALIGN); 724c68cfb71SSrinivas Kandagatla for (i = 0; i < ctx->nscalars; i++) { 725c68cfb71SSrinivas Kandagatla if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) { 72625e8dfb8SSrinivas Kandagatla 72725e8dfb8SSrinivas Kandagatla if (ctx->olaps[i].offset == 0) 728c68cfb71SSrinivas Kandagatla size = ALIGN(size, FASTRPC_ALIGN); 72925e8dfb8SSrinivas Kandagatla 73025e8dfb8SSrinivas Kandagatla size += (ctx->olaps[i].mend - ctx->olaps[i].mstart); 731c68cfb71SSrinivas Kandagatla } 732c68cfb71SSrinivas Kandagatla } 733c68cfb71SSrinivas Kandagatla 734c68cfb71SSrinivas Kandagatla return size; 735c68cfb71SSrinivas Kandagatla } 736c68cfb71SSrinivas Kandagatla 737c68cfb71SSrinivas Kandagatla static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx) 738c68cfb71SSrinivas Kandagatla { 739c68cfb71SSrinivas Kandagatla struct device *dev = ctx->fl->sctx->dev; 740c68cfb71SSrinivas Kandagatla int i, err; 741c68cfb71SSrinivas Kandagatla 742c68cfb71SSrinivas Kandagatla for (i = 0; i < ctx->nscalars; ++i) { 743c68cfb71SSrinivas Kandagatla /* Make sure reserved field is set to 0 */ 744c68cfb71SSrinivas Kandagatla if (ctx->args[i].reserved) 745c68cfb71SSrinivas Kandagatla return -EINVAL; 746c68cfb71SSrinivas Kandagatla 747c68cfb71SSrinivas Kandagatla if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 || 748c68cfb71SSrinivas Kandagatla ctx->args[i].length == 0) 749c68cfb71SSrinivas Kandagatla continue; 750c68cfb71SSrinivas Kandagatla 751c68cfb71SSrinivas Kandagatla err = fastrpc_map_create(ctx->fl, ctx->args[i].fd, 752c68cfb71SSrinivas Kandagatla ctx->args[i].length, &ctx->maps[i]); 753c68cfb71SSrinivas Kandagatla if (err) { 754c68cfb71SSrinivas Kandagatla dev_err(dev, "Error Creating map %d\n", err); 755c68cfb71SSrinivas Kandagatla return -EINVAL; 756c68cfb71SSrinivas Kandagatla } 757c68cfb71SSrinivas Kandagatla 758c68cfb71SSrinivas Kandagatla } 759c68cfb71SSrinivas Kandagatla return 0; 760c68cfb71SSrinivas Kandagatla } 761c68cfb71SSrinivas Kandagatla 762c68cfb71SSrinivas Kandagatla static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx) 763c68cfb71SSrinivas Kandagatla { 764c68cfb71SSrinivas Kandagatla struct device *dev = ctx->fl->sctx->dev; 765c68cfb71SSrinivas Kandagatla struct fastrpc_remote_arg *rpra; 766c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_buf *list; 767c68cfb71SSrinivas Kandagatla struct fastrpc_phy_page *pages; 76825e8dfb8SSrinivas Kandagatla int inbufs, i, oix, err = 0; 76925e8dfb8SSrinivas Kandagatla u64 len, rlen, pkt_size; 77002b45b47SSrinivas Kandagatla u64 pg_start, pg_end; 771c68cfb71SSrinivas Kandagatla uintptr_t args; 772c68cfb71SSrinivas Kandagatla int metalen; 773c68cfb71SSrinivas Kandagatla 774c68cfb71SSrinivas Kandagatla inbufs = REMOTE_SCALARS_INBUFS(ctx->sc); 775c68cfb71SSrinivas Kandagatla metalen = fastrpc_get_meta_size(ctx); 776c68cfb71SSrinivas Kandagatla pkt_size = fastrpc_get_payload_size(ctx, metalen); 777c68cfb71SSrinivas Kandagatla 778c68cfb71SSrinivas Kandagatla err = fastrpc_create_maps(ctx); 779c68cfb71SSrinivas Kandagatla if (err) 780c68cfb71SSrinivas Kandagatla return err; 781c68cfb71SSrinivas Kandagatla 782c68cfb71SSrinivas Kandagatla ctx->msg_sz = pkt_size; 783c68cfb71SSrinivas Kandagatla 784c68cfb71SSrinivas Kandagatla err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf); 785c68cfb71SSrinivas Kandagatla if (err) 786c68cfb71SSrinivas Kandagatla return err; 787c68cfb71SSrinivas Kandagatla 788c68cfb71SSrinivas Kandagatla rpra = ctx->buf->virt; 789c68cfb71SSrinivas Kandagatla list = ctx->buf->virt + ctx->nscalars * sizeof(*rpra); 790c68cfb71SSrinivas Kandagatla pages = ctx->buf->virt + ctx->nscalars * (sizeof(*list) + 791c68cfb71SSrinivas Kandagatla sizeof(*rpra)); 792c68cfb71SSrinivas Kandagatla args = (uintptr_t)ctx->buf->virt + metalen; 793c68cfb71SSrinivas Kandagatla rlen = pkt_size - metalen; 794c68cfb71SSrinivas Kandagatla ctx->rpra = rpra; 795c68cfb71SSrinivas Kandagatla 79625e8dfb8SSrinivas Kandagatla for (oix = 0; oix < ctx->nbufs; ++oix) { 79725e8dfb8SSrinivas Kandagatla int mlen; 79825e8dfb8SSrinivas Kandagatla 79925e8dfb8SSrinivas Kandagatla i = ctx->olaps[oix].raix; 80025e8dfb8SSrinivas Kandagatla len = ctx->args[i].length; 801c68cfb71SSrinivas Kandagatla 802c68cfb71SSrinivas Kandagatla rpra[i].pv = 0; 803c68cfb71SSrinivas Kandagatla rpra[i].len = len; 804c68cfb71SSrinivas Kandagatla list[i].num = len ? 1 : 0; 805c68cfb71SSrinivas Kandagatla list[i].pgidx = i; 806c68cfb71SSrinivas Kandagatla 807c68cfb71SSrinivas Kandagatla if (!len) 808c68cfb71SSrinivas Kandagatla continue; 809c68cfb71SSrinivas Kandagatla 810c68cfb71SSrinivas Kandagatla if (ctx->maps[i]) { 81180f3afd7SSrinivas Kandagatla struct vm_area_struct *vma = NULL; 81280f3afd7SSrinivas Kandagatla 813c68cfb71SSrinivas Kandagatla rpra[i].pv = (u64) ctx->args[i].ptr; 814c68cfb71SSrinivas Kandagatla pages[i].addr = ctx->maps[i]->phys; 81580f3afd7SSrinivas Kandagatla 81680f3afd7SSrinivas Kandagatla vma = find_vma(current->mm, ctx->args[i].ptr); 81780f3afd7SSrinivas Kandagatla if (vma) 81880f3afd7SSrinivas Kandagatla pages[i].addr += ctx->args[i].ptr - 81980f3afd7SSrinivas Kandagatla vma->vm_start; 82080f3afd7SSrinivas Kandagatla 82102b45b47SSrinivas Kandagatla pg_start = (ctx->args[i].ptr & PAGE_MASK) >> PAGE_SHIFT; 82202b45b47SSrinivas Kandagatla pg_end = ((ctx->args[i].ptr + len - 1) & PAGE_MASK) >> 82302b45b47SSrinivas Kandagatla PAGE_SHIFT; 82402b45b47SSrinivas Kandagatla pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE; 82502b45b47SSrinivas Kandagatla 826c68cfb71SSrinivas Kandagatla } else { 82725e8dfb8SSrinivas Kandagatla 82825e8dfb8SSrinivas Kandagatla if (ctx->olaps[oix].offset == 0) { 829c68cfb71SSrinivas Kandagatla rlen -= ALIGN(args, FASTRPC_ALIGN) - args; 830c68cfb71SSrinivas Kandagatla args = ALIGN(args, FASTRPC_ALIGN); 83125e8dfb8SSrinivas Kandagatla } 83225e8dfb8SSrinivas Kandagatla 83325e8dfb8SSrinivas Kandagatla mlen = ctx->olaps[oix].mend - ctx->olaps[oix].mstart; 83425e8dfb8SSrinivas Kandagatla 83525e8dfb8SSrinivas Kandagatla if (rlen < mlen) 836c68cfb71SSrinivas Kandagatla goto bail; 837c68cfb71SSrinivas Kandagatla 83825e8dfb8SSrinivas Kandagatla rpra[i].pv = args - ctx->olaps[oix].offset; 83925e8dfb8SSrinivas Kandagatla pages[i].addr = ctx->buf->phys - 84025e8dfb8SSrinivas Kandagatla ctx->olaps[oix].offset + 84125e8dfb8SSrinivas Kandagatla (pkt_size - rlen); 842c68cfb71SSrinivas Kandagatla pages[i].addr = pages[i].addr & PAGE_MASK; 84325e8dfb8SSrinivas Kandagatla 84402b45b47SSrinivas Kandagatla pg_start = (args & PAGE_MASK) >> PAGE_SHIFT; 84502b45b47SSrinivas Kandagatla pg_end = ((args + len - 1) & PAGE_MASK) >> PAGE_SHIFT; 84602b45b47SSrinivas Kandagatla pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE; 84725e8dfb8SSrinivas Kandagatla args = args + mlen; 84825e8dfb8SSrinivas Kandagatla rlen -= mlen; 849c68cfb71SSrinivas Kandagatla } 850c68cfb71SSrinivas Kandagatla 851c68cfb71SSrinivas Kandagatla if (i < inbufs && !ctx->maps[i]) { 852c68cfb71SSrinivas Kandagatla void *dst = (void *)(uintptr_t)rpra[i].pv; 853c68cfb71SSrinivas Kandagatla void *src = (void *)(uintptr_t)ctx->args[i].ptr; 854c68cfb71SSrinivas Kandagatla 855c68cfb71SSrinivas Kandagatla if (!kernel) { 856c68cfb71SSrinivas Kandagatla if (copy_from_user(dst, (void __user *)src, 857c68cfb71SSrinivas Kandagatla len)) { 858c68cfb71SSrinivas Kandagatla err = -EFAULT; 859c68cfb71SSrinivas Kandagatla goto bail; 860c68cfb71SSrinivas Kandagatla } 861c68cfb71SSrinivas Kandagatla } else { 862c68cfb71SSrinivas Kandagatla memcpy(dst, src, len); 863c68cfb71SSrinivas Kandagatla } 864c68cfb71SSrinivas Kandagatla } 865c68cfb71SSrinivas Kandagatla } 866c68cfb71SSrinivas Kandagatla 867c68cfb71SSrinivas Kandagatla for (i = ctx->nbufs; i < ctx->nscalars; ++i) { 868c68cfb71SSrinivas Kandagatla rpra[i].pv = (u64) ctx->args[i].ptr; 869c68cfb71SSrinivas Kandagatla rpra[i].len = ctx->args[i].length; 870c68cfb71SSrinivas Kandagatla list[i].num = ctx->args[i].length ? 1 : 0; 871c68cfb71SSrinivas Kandagatla list[i].pgidx = i; 872c68cfb71SSrinivas Kandagatla pages[i].addr = ctx->maps[i]->phys; 873c68cfb71SSrinivas Kandagatla pages[i].size = ctx->maps[i]->size; 874c68cfb71SSrinivas Kandagatla } 875c68cfb71SSrinivas Kandagatla 876c68cfb71SSrinivas Kandagatla bail: 877c68cfb71SSrinivas Kandagatla if (err) 878c68cfb71SSrinivas Kandagatla dev_err(dev, "Error: get invoke args failed:%d\n", err); 879c68cfb71SSrinivas Kandagatla 880c68cfb71SSrinivas Kandagatla return err; 881c68cfb71SSrinivas Kandagatla } 882c68cfb71SSrinivas Kandagatla 883c68cfb71SSrinivas Kandagatla static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx, 884c68cfb71SSrinivas Kandagatla u32 kernel) 885c68cfb71SSrinivas Kandagatla { 886c68cfb71SSrinivas Kandagatla struct fastrpc_remote_arg *rpra = ctx->rpra; 887c68cfb71SSrinivas Kandagatla int i, inbufs; 888c68cfb71SSrinivas Kandagatla 889c68cfb71SSrinivas Kandagatla inbufs = REMOTE_SCALARS_INBUFS(ctx->sc); 890c68cfb71SSrinivas Kandagatla 891c68cfb71SSrinivas Kandagatla for (i = inbufs; i < ctx->nbufs; ++i) { 892c68cfb71SSrinivas Kandagatla void *src = (void *)(uintptr_t)rpra[i].pv; 893c68cfb71SSrinivas Kandagatla void *dst = (void *)(uintptr_t)ctx->args[i].ptr; 894c68cfb71SSrinivas Kandagatla u64 len = rpra[i].len; 895c68cfb71SSrinivas Kandagatla 896c68cfb71SSrinivas Kandagatla if (!kernel) { 897c68cfb71SSrinivas Kandagatla if (copy_to_user((void __user *)dst, src, len)) 898c68cfb71SSrinivas Kandagatla return -EFAULT; 899c68cfb71SSrinivas Kandagatla } else { 900c68cfb71SSrinivas Kandagatla memcpy(dst, src, len); 901c68cfb71SSrinivas Kandagatla } 902c68cfb71SSrinivas Kandagatla } 903c68cfb71SSrinivas Kandagatla 904c68cfb71SSrinivas Kandagatla return 0; 905c68cfb71SSrinivas Kandagatla } 906c68cfb71SSrinivas Kandagatla 907c68cfb71SSrinivas Kandagatla static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx, 908c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx, 909c68cfb71SSrinivas Kandagatla u32 kernel, uint32_t handle) 910c68cfb71SSrinivas Kandagatla { 911c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 912c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl = ctx->fl; 913c68cfb71SSrinivas Kandagatla struct fastrpc_msg *msg = &ctx->msg; 914c68cfb71SSrinivas Kandagatla 915c68cfb71SSrinivas Kandagatla cctx = fl->cctx; 916c68cfb71SSrinivas Kandagatla msg->pid = fl->tgid; 917c68cfb71SSrinivas Kandagatla msg->tid = current->pid; 918c68cfb71SSrinivas Kandagatla 919c68cfb71SSrinivas Kandagatla if (kernel) 920c68cfb71SSrinivas Kandagatla msg->pid = 0; 921c68cfb71SSrinivas Kandagatla 922c68cfb71SSrinivas Kandagatla msg->ctx = ctx->ctxid | fl->pd; 923c68cfb71SSrinivas Kandagatla msg->handle = handle; 924c68cfb71SSrinivas Kandagatla msg->sc = ctx->sc; 925c68cfb71SSrinivas Kandagatla msg->addr = ctx->buf ? ctx->buf->phys : 0; 926c68cfb71SSrinivas Kandagatla msg->size = roundup(ctx->msg_sz, PAGE_SIZE); 927c68cfb71SSrinivas Kandagatla fastrpc_context_get(ctx); 928c68cfb71SSrinivas Kandagatla 929c68cfb71SSrinivas Kandagatla return rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg)); 930c68cfb71SSrinivas Kandagatla } 931c68cfb71SSrinivas Kandagatla 932c68cfb71SSrinivas Kandagatla static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, 933c68cfb71SSrinivas Kandagatla u32 handle, u32 sc, 934c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_args *args) 935c68cfb71SSrinivas Kandagatla { 936c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx = NULL; 937c68cfb71SSrinivas Kandagatla int err = 0; 938c68cfb71SSrinivas Kandagatla 939c68cfb71SSrinivas Kandagatla if (!fl->sctx) 940c68cfb71SSrinivas Kandagatla return -EINVAL; 941c68cfb71SSrinivas Kandagatla 9422e369878SBjorn Andersson if (!fl->cctx->rpdev) 9432e369878SBjorn Andersson return -EPIPE; 9442e369878SBjorn Andersson 945c68cfb71SSrinivas Kandagatla ctx = fastrpc_context_alloc(fl, kernel, sc, args); 946c68cfb71SSrinivas Kandagatla if (IS_ERR(ctx)) 947c68cfb71SSrinivas Kandagatla return PTR_ERR(ctx); 948c68cfb71SSrinivas Kandagatla 949c68cfb71SSrinivas Kandagatla if (ctx->nscalars) { 950c68cfb71SSrinivas Kandagatla err = fastrpc_get_args(kernel, ctx); 951c68cfb71SSrinivas Kandagatla if (err) 952c68cfb71SSrinivas Kandagatla goto bail; 953c68cfb71SSrinivas Kandagatla } 954415a0729SSrinivas Kandagatla 955415a0729SSrinivas Kandagatla /* make sure that all CPU memory writes are seen by DSP */ 956415a0729SSrinivas Kandagatla dma_wmb(); 957c68cfb71SSrinivas Kandagatla /* Send invoke buffer to remote dsp */ 958c68cfb71SSrinivas Kandagatla err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle); 959c68cfb71SSrinivas Kandagatla if (err) 960c68cfb71SSrinivas Kandagatla goto bail; 961c68cfb71SSrinivas Kandagatla 962c68cfb71SSrinivas Kandagatla /* Wait for remote dsp to respond or time out */ 963c68cfb71SSrinivas Kandagatla err = wait_for_completion_interruptible(&ctx->work); 964c68cfb71SSrinivas Kandagatla if (err) 965c68cfb71SSrinivas Kandagatla goto bail; 966c68cfb71SSrinivas Kandagatla 967c68cfb71SSrinivas Kandagatla /* Check the response from remote dsp */ 968c68cfb71SSrinivas Kandagatla err = ctx->retval; 969c68cfb71SSrinivas Kandagatla if (err) 970c68cfb71SSrinivas Kandagatla goto bail; 971c68cfb71SSrinivas Kandagatla 972c68cfb71SSrinivas Kandagatla if (ctx->nscalars) { 973415a0729SSrinivas Kandagatla /* make sure that all memory writes by DSP are seen by CPU */ 974415a0729SSrinivas Kandagatla dma_rmb(); 975c68cfb71SSrinivas Kandagatla /* populate all the output buffers with results */ 976c68cfb71SSrinivas Kandagatla err = fastrpc_put_args(ctx, kernel); 977c68cfb71SSrinivas Kandagatla if (err) 978c68cfb71SSrinivas Kandagatla goto bail; 979c68cfb71SSrinivas Kandagatla } 980c68cfb71SSrinivas Kandagatla 981c68cfb71SSrinivas Kandagatla bail: 982c68cfb71SSrinivas Kandagatla /* We are done with this compute context, remove it from pending list */ 983c68cfb71SSrinivas Kandagatla spin_lock(&fl->lock); 984c68cfb71SSrinivas Kandagatla list_del(&ctx->node); 985c68cfb71SSrinivas Kandagatla spin_unlock(&fl->lock); 986c68cfb71SSrinivas Kandagatla fastrpc_context_put(ctx); 987c68cfb71SSrinivas Kandagatla 988c68cfb71SSrinivas Kandagatla if (err) 989c68cfb71SSrinivas Kandagatla dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err); 990c68cfb71SSrinivas Kandagatla 991c68cfb71SSrinivas Kandagatla return err; 992c68cfb71SSrinivas Kandagatla } 993c68cfb71SSrinivas Kandagatla 994d73f71c7SSrinivas Kandagatla static int fastrpc_init_create_process(struct fastrpc_user *fl, 995d73f71c7SSrinivas Kandagatla char __user *argp) 996d73f71c7SSrinivas Kandagatla { 997d73f71c7SSrinivas Kandagatla struct fastrpc_init_create init; 998d73f71c7SSrinivas Kandagatla struct fastrpc_invoke_args *args; 999d73f71c7SSrinivas Kandagatla struct fastrpc_phy_page pages[1]; 1000d73f71c7SSrinivas Kandagatla struct fastrpc_map *map = NULL; 1001d73f71c7SSrinivas Kandagatla struct fastrpc_buf *imem = NULL; 1002d73f71c7SSrinivas Kandagatla int memlen; 1003d73f71c7SSrinivas Kandagatla int err; 1004d73f71c7SSrinivas Kandagatla struct { 1005d73f71c7SSrinivas Kandagatla int pgid; 1006d73f71c7SSrinivas Kandagatla u32 namelen; 1007d73f71c7SSrinivas Kandagatla u32 filelen; 1008d73f71c7SSrinivas Kandagatla u32 pageslen; 1009d73f71c7SSrinivas Kandagatla u32 attrs; 1010d73f71c7SSrinivas Kandagatla u32 siglen; 1011d73f71c7SSrinivas Kandagatla } inbuf; 1012d73f71c7SSrinivas Kandagatla u32 sc; 1013d73f71c7SSrinivas Kandagatla 1014d73f71c7SSrinivas Kandagatla args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL); 1015d73f71c7SSrinivas Kandagatla if (!args) 1016d73f71c7SSrinivas Kandagatla return -ENOMEM; 1017d73f71c7SSrinivas Kandagatla 1018d73f71c7SSrinivas Kandagatla if (copy_from_user(&init, argp, sizeof(init))) { 1019d73f71c7SSrinivas Kandagatla err = -EFAULT; 1020b49f6d83SThierry Escande goto err; 1021d73f71c7SSrinivas Kandagatla } 1022d73f71c7SSrinivas Kandagatla 1023d73f71c7SSrinivas Kandagatla if (init.filelen > INIT_FILELEN_MAX) { 1024d73f71c7SSrinivas Kandagatla err = -EINVAL; 1025b49f6d83SThierry Escande goto err; 1026d73f71c7SSrinivas Kandagatla } 1027d73f71c7SSrinivas Kandagatla 1028d73f71c7SSrinivas Kandagatla inbuf.pgid = fl->tgid; 1029d73f71c7SSrinivas Kandagatla inbuf.namelen = strlen(current->comm) + 1; 1030d73f71c7SSrinivas Kandagatla inbuf.filelen = init.filelen; 1031d73f71c7SSrinivas Kandagatla inbuf.pageslen = 1; 1032d73f71c7SSrinivas Kandagatla inbuf.attrs = init.attrs; 1033d73f71c7SSrinivas Kandagatla inbuf.siglen = init.siglen; 1034d73f71c7SSrinivas Kandagatla fl->pd = 1; 1035d73f71c7SSrinivas Kandagatla 1036d73f71c7SSrinivas Kandagatla if (init.filelen && init.filefd) { 1037d73f71c7SSrinivas Kandagatla err = fastrpc_map_create(fl, init.filefd, init.filelen, &map); 1038d73f71c7SSrinivas Kandagatla if (err) 1039b49f6d83SThierry Escande goto err; 1040d73f71c7SSrinivas Kandagatla } 1041d73f71c7SSrinivas Kandagatla 1042d73f71c7SSrinivas Kandagatla memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4), 1043d73f71c7SSrinivas Kandagatla 1024 * 1024); 1044d73f71c7SSrinivas Kandagatla err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen, 1045d73f71c7SSrinivas Kandagatla &imem); 1046b49f6d83SThierry Escande if (err) 1047b49f6d83SThierry Escande goto err_alloc; 1048d73f71c7SSrinivas Kandagatla 1049d73f71c7SSrinivas Kandagatla fl->init_mem = imem; 1050d73f71c7SSrinivas Kandagatla args[0].ptr = (u64)(uintptr_t)&inbuf; 1051d73f71c7SSrinivas Kandagatla args[0].length = sizeof(inbuf); 1052d73f71c7SSrinivas Kandagatla args[0].fd = -1; 1053d73f71c7SSrinivas Kandagatla 1054d73f71c7SSrinivas Kandagatla args[1].ptr = (u64)(uintptr_t)current->comm; 1055d73f71c7SSrinivas Kandagatla args[1].length = inbuf.namelen; 1056d73f71c7SSrinivas Kandagatla args[1].fd = -1; 1057d73f71c7SSrinivas Kandagatla 1058d73f71c7SSrinivas Kandagatla args[2].ptr = (u64) init.file; 1059d73f71c7SSrinivas Kandagatla args[2].length = inbuf.filelen; 1060d73f71c7SSrinivas Kandagatla args[2].fd = init.filefd; 1061d73f71c7SSrinivas Kandagatla 1062d73f71c7SSrinivas Kandagatla pages[0].addr = imem->phys; 1063d73f71c7SSrinivas Kandagatla pages[0].size = imem->size; 1064d73f71c7SSrinivas Kandagatla 1065d73f71c7SSrinivas Kandagatla args[3].ptr = (u64)(uintptr_t) pages; 1066d73f71c7SSrinivas Kandagatla args[3].length = 1 * sizeof(*pages); 1067d73f71c7SSrinivas Kandagatla args[3].fd = -1; 1068d73f71c7SSrinivas Kandagatla 1069d73f71c7SSrinivas Kandagatla args[4].ptr = (u64)(uintptr_t)&inbuf.attrs; 1070d73f71c7SSrinivas Kandagatla args[4].length = sizeof(inbuf.attrs); 1071d73f71c7SSrinivas Kandagatla args[4].fd = -1; 1072d73f71c7SSrinivas Kandagatla 1073d73f71c7SSrinivas Kandagatla args[5].ptr = (u64)(uintptr_t) &inbuf.siglen; 1074d73f71c7SSrinivas Kandagatla args[5].length = sizeof(inbuf.siglen); 1075d73f71c7SSrinivas Kandagatla args[5].fd = -1; 1076d73f71c7SSrinivas Kandagatla 1077d73f71c7SSrinivas Kandagatla sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0); 1078d73f71c7SSrinivas Kandagatla if (init.attrs) 1079d73f71c7SSrinivas Kandagatla sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0); 1080d73f71c7SSrinivas Kandagatla 1081d73f71c7SSrinivas Kandagatla err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, 1082d73f71c7SSrinivas Kandagatla sc, args); 1083b49f6d83SThierry Escande if (err) 1084b49f6d83SThierry Escande goto err_invoke; 1085d73f71c7SSrinivas Kandagatla 1086b49f6d83SThierry Escande kfree(args); 1087b49f6d83SThierry Escande 1088b49f6d83SThierry Escande return 0; 1089b49f6d83SThierry Escande 1090b49f6d83SThierry Escande err_invoke: 1091b49f6d83SThierry Escande fl->init_mem = NULL; 1092d73f71c7SSrinivas Kandagatla fastrpc_buf_free(imem); 1093b49f6d83SThierry Escande err_alloc: 1094b49f6d83SThierry Escande if (map) { 1095b49f6d83SThierry Escande spin_lock(&fl->lock); 1096b49f6d83SThierry Escande list_del(&map->node); 1097b49f6d83SThierry Escande spin_unlock(&fl->lock); 1098b49f6d83SThierry Escande fastrpc_map_put(map); 1099d73f71c7SSrinivas Kandagatla } 1100b49f6d83SThierry Escande err: 1101d73f71c7SSrinivas Kandagatla kfree(args); 1102d73f71c7SSrinivas Kandagatla 1103d73f71c7SSrinivas Kandagatla return err; 1104d73f71c7SSrinivas Kandagatla } 1105d73f71c7SSrinivas Kandagatla 1106f6f9279fSSrinivas Kandagatla static struct fastrpc_session_ctx *fastrpc_session_alloc( 1107f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx) 1108f6f9279fSSrinivas Kandagatla { 1109f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *session = NULL; 1110977e6c8dSSrinivas Kandagatla unsigned long flags; 1111f6f9279fSSrinivas Kandagatla int i; 1112f6f9279fSSrinivas Kandagatla 1113977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1114f6f9279fSSrinivas Kandagatla for (i = 0; i < cctx->sesscount; i++) { 1115f6f9279fSSrinivas Kandagatla if (!cctx->session[i].used && cctx->session[i].valid) { 1116f6f9279fSSrinivas Kandagatla cctx->session[i].used = true; 1117f6f9279fSSrinivas Kandagatla session = &cctx->session[i]; 1118f6f9279fSSrinivas Kandagatla break; 1119f6f9279fSSrinivas Kandagatla } 1120f6f9279fSSrinivas Kandagatla } 1121977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1122f6f9279fSSrinivas Kandagatla 1123f6f9279fSSrinivas Kandagatla return session; 1124f6f9279fSSrinivas Kandagatla } 1125f6f9279fSSrinivas Kandagatla 1126f6f9279fSSrinivas Kandagatla static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx, 1127f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *session) 1128f6f9279fSSrinivas Kandagatla { 1129977e6c8dSSrinivas Kandagatla unsigned long flags; 1130977e6c8dSSrinivas Kandagatla 1131977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1132f6f9279fSSrinivas Kandagatla session->used = false; 1133977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1134f6f9279fSSrinivas Kandagatla } 1135f6f9279fSSrinivas Kandagatla 1136d73f71c7SSrinivas Kandagatla static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl) 1137d73f71c7SSrinivas Kandagatla { 1138d73f71c7SSrinivas Kandagatla struct fastrpc_invoke_args args[1]; 1139d73f71c7SSrinivas Kandagatla int tgid = 0; 1140d73f71c7SSrinivas Kandagatla u32 sc; 1141d73f71c7SSrinivas Kandagatla 1142d73f71c7SSrinivas Kandagatla tgid = fl->tgid; 1143d73f71c7SSrinivas Kandagatla args[0].ptr = (u64)(uintptr_t) &tgid; 1144d73f71c7SSrinivas Kandagatla args[0].length = sizeof(tgid); 1145d73f71c7SSrinivas Kandagatla args[0].fd = -1; 1146d73f71c7SSrinivas Kandagatla args[0].reserved = 0; 1147d73f71c7SSrinivas Kandagatla sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0); 1148d73f71c7SSrinivas Kandagatla 1149d73f71c7SSrinivas Kandagatla return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, 1150d73f71c7SSrinivas Kandagatla sc, &args[0]); 1151d73f71c7SSrinivas Kandagatla } 1152d73f71c7SSrinivas Kandagatla 1153f6f9279fSSrinivas Kandagatla static int fastrpc_device_release(struct inode *inode, struct file *file) 1154f6f9279fSSrinivas Kandagatla { 1155f6f9279fSSrinivas Kandagatla struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data; 1156f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = fl->cctx; 1157c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx, *n; 1158c68cfb71SSrinivas Kandagatla struct fastrpc_map *map, *m; 1159*2419e55eSJorge Ramirez-Ortiz struct fastrpc_buf *buf, *b; 1160977e6c8dSSrinivas Kandagatla unsigned long flags; 1161f6f9279fSSrinivas Kandagatla 1162d73f71c7SSrinivas Kandagatla fastrpc_release_current_dsp_process(fl); 1163d73f71c7SSrinivas Kandagatla 1164977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1165f6f9279fSSrinivas Kandagatla list_del(&fl->user); 1166977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1167f6f9279fSSrinivas Kandagatla 1168c68cfb71SSrinivas Kandagatla if (fl->init_mem) 1169c68cfb71SSrinivas Kandagatla fastrpc_buf_free(fl->init_mem); 1170c68cfb71SSrinivas Kandagatla 1171c68cfb71SSrinivas Kandagatla list_for_each_entry_safe(ctx, n, &fl->pending, node) { 1172c68cfb71SSrinivas Kandagatla list_del(&ctx->node); 1173c68cfb71SSrinivas Kandagatla fastrpc_context_put(ctx); 1174c68cfb71SSrinivas Kandagatla } 1175c68cfb71SSrinivas Kandagatla 1176c68cfb71SSrinivas Kandagatla list_for_each_entry_safe(map, m, &fl->maps, node) { 1177c68cfb71SSrinivas Kandagatla list_del(&map->node); 1178c68cfb71SSrinivas Kandagatla fastrpc_map_put(map); 1179c68cfb71SSrinivas Kandagatla } 1180c68cfb71SSrinivas Kandagatla 1181*2419e55eSJorge Ramirez-Ortiz list_for_each_entry_safe(buf, b, &fl->mmaps, node) { 1182*2419e55eSJorge Ramirez-Ortiz list_del(&buf->node); 1183*2419e55eSJorge Ramirez-Ortiz fastrpc_buf_free(buf); 1184*2419e55eSJorge Ramirez-Ortiz } 1185*2419e55eSJorge Ramirez-Ortiz 1186f6f9279fSSrinivas Kandagatla fastrpc_session_free(cctx, fl->sctx); 1187278d56f9SBjorn Andersson fastrpc_channel_ctx_put(cctx); 1188f6f9279fSSrinivas Kandagatla 1189f6f9279fSSrinivas Kandagatla mutex_destroy(&fl->mutex); 1190f6f9279fSSrinivas Kandagatla kfree(fl); 1191f6f9279fSSrinivas Kandagatla file->private_data = NULL; 1192f6f9279fSSrinivas Kandagatla 1193f6f9279fSSrinivas Kandagatla return 0; 1194f6f9279fSSrinivas Kandagatla } 1195f6f9279fSSrinivas Kandagatla 1196f6f9279fSSrinivas Kandagatla static int fastrpc_device_open(struct inode *inode, struct file *filp) 1197f6f9279fSSrinivas Kandagatla { 1198f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = miscdev_to_cctx(filp->private_data); 1199f6f9279fSSrinivas Kandagatla struct fastrpc_user *fl = NULL; 1200977e6c8dSSrinivas Kandagatla unsigned long flags; 1201f6f9279fSSrinivas Kandagatla 1202f6f9279fSSrinivas Kandagatla fl = kzalloc(sizeof(*fl), GFP_KERNEL); 1203f6f9279fSSrinivas Kandagatla if (!fl) 1204f6f9279fSSrinivas Kandagatla return -ENOMEM; 1205f6f9279fSSrinivas Kandagatla 1206278d56f9SBjorn Andersson /* Released in fastrpc_device_release() */ 1207278d56f9SBjorn Andersson fastrpc_channel_ctx_get(cctx); 1208278d56f9SBjorn Andersson 1209f6f9279fSSrinivas Kandagatla filp->private_data = fl; 1210f6f9279fSSrinivas Kandagatla spin_lock_init(&fl->lock); 1211f6f9279fSSrinivas Kandagatla mutex_init(&fl->mutex); 1212f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&fl->pending); 1213f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&fl->maps); 1214*2419e55eSJorge Ramirez-Ortiz INIT_LIST_HEAD(&fl->mmaps); 1215f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&fl->user); 1216f6f9279fSSrinivas Kandagatla fl->tgid = current->tgid; 1217f6f9279fSSrinivas Kandagatla fl->cctx = cctx; 12187c11df42SThierry Escande 12197c11df42SThierry Escande fl->sctx = fastrpc_session_alloc(cctx); 12207c11df42SThierry Escande if (!fl->sctx) { 12217c11df42SThierry Escande dev_err(&cctx->rpdev->dev, "No session available\n"); 12227c11df42SThierry Escande mutex_destroy(&fl->mutex); 12237c11df42SThierry Escande kfree(fl); 12247c11df42SThierry Escande 12257c11df42SThierry Escande return -EBUSY; 12267c11df42SThierry Escande } 12277c11df42SThierry Escande 1228977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1229f6f9279fSSrinivas Kandagatla list_add_tail(&fl->user, &cctx->users); 1230977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1231f6f9279fSSrinivas Kandagatla 1232f6f9279fSSrinivas Kandagatla return 0; 1233f6f9279fSSrinivas Kandagatla } 1234f6f9279fSSrinivas Kandagatla 12356cffd795SSrinivas Kandagatla static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp) 12366cffd795SSrinivas Kandagatla { 12376cffd795SSrinivas Kandagatla struct fastrpc_alloc_dma_buf bp; 12386cffd795SSrinivas Kandagatla DEFINE_DMA_BUF_EXPORT_INFO(exp_info); 12396cffd795SSrinivas Kandagatla struct fastrpc_buf *buf = NULL; 12406cffd795SSrinivas Kandagatla int err; 12416cffd795SSrinivas Kandagatla 12426cffd795SSrinivas Kandagatla if (copy_from_user(&bp, argp, sizeof(bp))) 12436cffd795SSrinivas Kandagatla return -EFAULT; 12446cffd795SSrinivas Kandagatla 12456cffd795SSrinivas Kandagatla err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf); 12466cffd795SSrinivas Kandagatla if (err) 12476cffd795SSrinivas Kandagatla return err; 12486cffd795SSrinivas Kandagatla exp_info.ops = &fastrpc_dma_buf_ops; 12496cffd795SSrinivas Kandagatla exp_info.size = bp.size; 12506cffd795SSrinivas Kandagatla exp_info.flags = O_RDWR; 12516cffd795SSrinivas Kandagatla exp_info.priv = buf; 12526cffd795SSrinivas Kandagatla buf->dmabuf = dma_buf_export(&exp_info); 12536cffd795SSrinivas Kandagatla if (IS_ERR(buf->dmabuf)) { 12546cffd795SSrinivas Kandagatla err = PTR_ERR(buf->dmabuf); 12556cffd795SSrinivas Kandagatla fastrpc_buf_free(buf); 12566cffd795SSrinivas Kandagatla return err; 12576cffd795SSrinivas Kandagatla } 12586cffd795SSrinivas Kandagatla 12596cffd795SSrinivas Kandagatla bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE); 12606cffd795SSrinivas Kandagatla if (bp.fd < 0) { 12616cffd795SSrinivas Kandagatla dma_buf_put(buf->dmabuf); 12626cffd795SSrinivas Kandagatla return -EINVAL; 12636cffd795SSrinivas Kandagatla } 12646cffd795SSrinivas Kandagatla 12656cffd795SSrinivas Kandagatla if (copy_to_user(argp, &bp, sizeof(bp))) { 12666cffd795SSrinivas Kandagatla dma_buf_put(buf->dmabuf); 12676cffd795SSrinivas Kandagatla return -EFAULT; 12686cffd795SSrinivas Kandagatla } 12696cffd795SSrinivas Kandagatla 12706cffd795SSrinivas Kandagatla return 0; 12716cffd795SSrinivas Kandagatla } 12726cffd795SSrinivas Kandagatla 1273d73f71c7SSrinivas Kandagatla static int fastrpc_init_attach(struct fastrpc_user *fl) 1274d73f71c7SSrinivas Kandagatla { 1275d73f71c7SSrinivas Kandagatla struct fastrpc_invoke_args args[1]; 1276d73f71c7SSrinivas Kandagatla int tgid = fl->tgid; 1277d73f71c7SSrinivas Kandagatla u32 sc; 1278d73f71c7SSrinivas Kandagatla 1279d73f71c7SSrinivas Kandagatla args[0].ptr = (u64)(uintptr_t) &tgid; 1280d73f71c7SSrinivas Kandagatla args[0].length = sizeof(tgid); 1281d73f71c7SSrinivas Kandagatla args[0].fd = -1; 1282d73f71c7SSrinivas Kandagatla args[0].reserved = 0; 1283d73f71c7SSrinivas Kandagatla sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0); 1284d73f71c7SSrinivas Kandagatla fl->pd = 0; 1285d73f71c7SSrinivas Kandagatla 1286d73f71c7SSrinivas Kandagatla return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, 1287d73f71c7SSrinivas Kandagatla sc, &args[0]); 1288d73f71c7SSrinivas Kandagatla } 1289d73f71c7SSrinivas Kandagatla 1290c68cfb71SSrinivas Kandagatla static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp) 1291c68cfb71SSrinivas Kandagatla { 1292c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_args *args = NULL; 1293c68cfb71SSrinivas Kandagatla struct fastrpc_invoke inv; 1294c68cfb71SSrinivas Kandagatla u32 nscalars; 1295c68cfb71SSrinivas Kandagatla int err; 1296c68cfb71SSrinivas Kandagatla 1297c68cfb71SSrinivas Kandagatla if (copy_from_user(&inv, argp, sizeof(inv))) 1298c68cfb71SSrinivas Kandagatla return -EFAULT; 1299c68cfb71SSrinivas Kandagatla 1300c68cfb71SSrinivas Kandagatla /* nscalars is truncated here to max supported value */ 1301c68cfb71SSrinivas Kandagatla nscalars = REMOTE_SCALARS_LENGTH(inv.sc); 1302c68cfb71SSrinivas Kandagatla if (nscalars) { 1303c68cfb71SSrinivas Kandagatla args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL); 1304c68cfb71SSrinivas Kandagatla if (!args) 1305c68cfb71SSrinivas Kandagatla return -ENOMEM; 1306c68cfb71SSrinivas Kandagatla 1307c68cfb71SSrinivas Kandagatla if (copy_from_user(args, (void __user *)(uintptr_t)inv.args, 1308c68cfb71SSrinivas Kandagatla nscalars * sizeof(*args))) { 1309c68cfb71SSrinivas Kandagatla kfree(args); 1310c68cfb71SSrinivas Kandagatla return -EFAULT; 1311c68cfb71SSrinivas Kandagatla } 1312c68cfb71SSrinivas Kandagatla } 1313c68cfb71SSrinivas Kandagatla 1314c68cfb71SSrinivas Kandagatla err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args); 1315c68cfb71SSrinivas Kandagatla kfree(args); 1316c68cfb71SSrinivas Kandagatla 1317c68cfb71SSrinivas Kandagatla return err; 1318c68cfb71SSrinivas Kandagatla } 1319c68cfb71SSrinivas Kandagatla 1320*2419e55eSJorge Ramirez-Ortiz static int fastrpc_req_munmap_impl(struct fastrpc_user *fl, 1321*2419e55eSJorge Ramirez-Ortiz struct fastrpc_req_munmap *req) 1322*2419e55eSJorge Ramirez-Ortiz { 1323*2419e55eSJorge Ramirez-Ortiz struct fastrpc_invoke_args args[1] = { [0] = { 0 } }; 1324*2419e55eSJorge Ramirez-Ortiz struct fastrpc_buf *buf, *b; 1325*2419e55eSJorge Ramirez-Ortiz struct fastrpc_munmap_req_msg req_msg; 1326*2419e55eSJorge Ramirez-Ortiz struct device *dev = fl->sctx->dev; 1327*2419e55eSJorge Ramirez-Ortiz int err; 1328*2419e55eSJorge Ramirez-Ortiz u32 sc; 1329*2419e55eSJorge Ramirez-Ortiz 1330*2419e55eSJorge Ramirez-Ortiz spin_lock(&fl->lock); 1331*2419e55eSJorge Ramirez-Ortiz list_for_each_entry_safe(buf, b, &fl->mmaps, node) { 1332*2419e55eSJorge Ramirez-Ortiz if ((buf->raddr == req->vaddrout) && (buf->size == req->size)) 1333*2419e55eSJorge Ramirez-Ortiz break; 1334*2419e55eSJorge Ramirez-Ortiz buf = NULL; 1335*2419e55eSJorge Ramirez-Ortiz } 1336*2419e55eSJorge Ramirez-Ortiz spin_unlock(&fl->lock); 1337*2419e55eSJorge Ramirez-Ortiz 1338*2419e55eSJorge Ramirez-Ortiz if (!buf) { 1339*2419e55eSJorge Ramirez-Ortiz dev_err(dev, "mmap not in list\n"); 1340*2419e55eSJorge Ramirez-Ortiz return -EINVAL; 1341*2419e55eSJorge Ramirez-Ortiz } 1342*2419e55eSJorge Ramirez-Ortiz 1343*2419e55eSJorge Ramirez-Ortiz req_msg.pgid = fl->tgid; 1344*2419e55eSJorge Ramirez-Ortiz req_msg.size = buf->size; 1345*2419e55eSJorge Ramirez-Ortiz req_msg.vaddr = buf->raddr; 1346*2419e55eSJorge Ramirez-Ortiz 1347*2419e55eSJorge Ramirez-Ortiz args[0].ptr = (u64) (uintptr_t) &req_msg; 1348*2419e55eSJorge Ramirez-Ortiz args[0].length = sizeof(req_msg); 1349*2419e55eSJorge Ramirez-Ortiz 1350*2419e55eSJorge Ramirez-Ortiz sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MUNMAP, 1, 0); 1351*2419e55eSJorge Ramirez-Ortiz err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, 1352*2419e55eSJorge Ramirez-Ortiz &args[0]); 1353*2419e55eSJorge Ramirez-Ortiz if (!err) { 1354*2419e55eSJorge Ramirez-Ortiz dev_dbg(dev, "unmmap\tpt 0x%09lx OK\n", buf->raddr); 1355*2419e55eSJorge Ramirez-Ortiz spin_lock(&fl->lock); 1356*2419e55eSJorge Ramirez-Ortiz list_del(&buf->node); 1357*2419e55eSJorge Ramirez-Ortiz spin_unlock(&fl->lock); 1358*2419e55eSJorge Ramirez-Ortiz fastrpc_buf_free(buf); 1359*2419e55eSJorge Ramirez-Ortiz } else { 1360*2419e55eSJorge Ramirez-Ortiz dev_err(dev, "unmmap\tpt 0x%09lx ERROR\n", buf->raddr); 1361*2419e55eSJorge Ramirez-Ortiz } 1362*2419e55eSJorge Ramirez-Ortiz 1363*2419e55eSJorge Ramirez-Ortiz return err; 1364*2419e55eSJorge Ramirez-Ortiz } 1365*2419e55eSJorge Ramirez-Ortiz 1366*2419e55eSJorge Ramirez-Ortiz static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp) 1367*2419e55eSJorge Ramirez-Ortiz { 1368*2419e55eSJorge Ramirez-Ortiz struct fastrpc_req_munmap req; 1369*2419e55eSJorge Ramirez-Ortiz 1370*2419e55eSJorge Ramirez-Ortiz if (copy_from_user(&req, argp, sizeof(req))) 1371*2419e55eSJorge Ramirez-Ortiz return -EFAULT; 1372*2419e55eSJorge Ramirez-Ortiz 1373*2419e55eSJorge Ramirez-Ortiz return fastrpc_req_munmap_impl(fl, &req); 1374*2419e55eSJorge Ramirez-Ortiz } 1375*2419e55eSJorge Ramirez-Ortiz 1376*2419e55eSJorge Ramirez-Ortiz static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp) 1377*2419e55eSJorge Ramirez-Ortiz { 1378*2419e55eSJorge Ramirez-Ortiz struct fastrpc_invoke_args args[3] = { [0 ... 2] = { 0 } }; 1379*2419e55eSJorge Ramirez-Ortiz struct fastrpc_buf *buf = NULL; 1380*2419e55eSJorge Ramirez-Ortiz struct fastrpc_mmap_req_msg req_msg; 1381*2419e55eSJorge Ramirez-Ortiz struct fastrpc_mmap_rsp_msg rsp_msg; 1382*2419e55eSJorge Ramirez-Ortiz struct fastrpc_req_munmap req_unmap; 1383*2419e55eSJorge Ramirez-Ortiz struct fastrpc_phy_page pages; 1384*2419e55eSJorge Ramirez-Ortiz struct fastrpc_req_mmap req; 1385*2419e55eSJorge Ramirez-Ortiz struct device *dev = fl->sctx->dev; 1386*2419e55eSJorge Ramirez-Ortiz int err; 1387*2419e55eSJorge Ramirez-Ortiz u32 sc; 1388*2419e55eSJorge Ramirez-Ortiz 1389*2419e55eSJorge Ramirez-Ortiz if (copy_from_user(&req, argp, sizeof(req))) 1390*2419e55eSJorge Ramirez-Ortiz return -EFAULT; 1391*2419e55eSJorge Ramirez-Ortiz 1392*2419e55eSJorge Ramirez-Ortiz if (req.flags != ADSP_MMAP_ADD_PAGES) { 1393*2419e55eSJorge Ramirez-Ortiz dev_err(dev, "flag not supported 0x%x\n", req.flags); 1394*2419e55eSJorge Ramirez-Ortiz return -EINVAL; 1395*2419e55eSJorge Ramirez-Ortiz } 1396*2419e55eSJorge Ramirez-Ortiz 1397*2419e55eSJorge Ramirez-Ortiz if (req.vaddrin) { 1398*2419e55eSJorge Ramirez-Ortiz dev_err(dev, "adding user allocated pages is not supported\n"); 1399*2419e55eSJorge Ramirez-Ortiz return -EINVAL; 1400*2419e55eSJorge Ramirez-Ortiz } 1401*2419e55eSJorge Ramirez-Ortiz 1402*2419e55eSJorge Ramirez-Ortiz err = fastrpc_buf_alloc(fl, fl->sctx->dev, req.size, &buf); 1403*2419e55eSJorge Ramirez-Ortiz if (err) { 1404*2419e55eSJorge Ramirez-Ortiz dev_err(dev, "failed to allocate buffer\n"); 1405*2419e55eSJorge Ramirez-Ortiz return err; 1406*2419e55eSJorge Ramirez-Ortiz } 1407*2419e55eSJorge Ramirez-Ortiz 1408*2419e55eSJorge Ramirez-Ortiz req_msg.pgid = fl->tgid; 1409*2419e55eSJorge Ramirez-Ortiz req_msg.flags = req.flags; 1410*2419e55eSJorge Ramirez-Ortiz req_msg.vaddr = req.vaddrin; 1411*2419e55eSJorge Ramirez-Ortiz req_msg.num = sizeof(pages); 1412*2419e55eSJorge Ramirez-Ortiz 1413*2419e55eSJorge Ramirez-Ortiz args[0].ptr = (u64) (uintptr_t) &req_msg; 1414*2419e55eSJorge Ramirez-Ortiz args[0].length = sizeof(req_msg); 1415*2419e55eSJorge Ramirez-Ortiz 1416*2419e55eSJorge Ramirez-Ortiz pages.addr = buf->phys; 1417*2419e55eSJorge Ramirez-Ortiz pages.size = buf->size; 1418*2419e55eSJorge Ramirez-Ortiz 1419*2419e55eSJorge Ramirez-Ortiz args[1].ptr = (u64) (uintptr_t) &pages; 1420*2419e55eSJorge Ramirez-Ortiz args[1].length = sizeof(pages); 1421*2419e55eSJorge Ramirez-Ortiz 1422*2419e55eSJorge Ramirez-Ortiz args[2].ptr = (u64) (uintptr_t) &rsp_msg; 1423*2419e55eSJorge Ramirez-Ortiz args[2].length = sizeof(rsp_msg); 1424*2419e55eSJorge Ramirez-Ortiz 1425*2419e55eSJorge Ramirez-Ortiz sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MMAP, 2, 1); 1426*2419e55eSJorge Ramirez-Ortiz err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, 1427*2419e55eSJorge Ramirez-Ortiz &args[0]); 1428*2419e55eSJorge Ramirez-Ortiz if (err) { 1429*2419e55eSJorge Ramirez-Ortiz dev_err(dev, "mmap error (len 0x%08llx)\n", buf->size); 1430*2419e55eSJorge Ramirez-Ortiz goto err_invoke; 1431*2419e55eSJorge Ramirez-Ortiz } 1432*2419e55eSJorge Ramirez-Ortiz 1433*2419e55eSJorge Ramirez-Ortiz /* update the buffer to be able to deallocate the memory on the DSP */ 1434*2419e55eSJorge Ramirez-Ortiz buf->raddr = (uintptr_t) rsp_msg.vaddr; 1435*2419e55eSJorge Ramirez-Ortiz 1436*2419e55eSJorge Ramirez-Ortiz /* let the client know the address to use */ 1437*2419e55eSJorge Ramirez-Ortiz req.vaddrout = rsp_msg.vaddr; 1438*2419e55eSJorge Ramirez-Ortiz 1439*2419e55eSJorge Ramirez-Ortiz spin_lock(&fl->lock); 1440*2419e55eSJorge Ramirez-Ortiz list_add_tail(&buf->node, &fl->mmaps); 1441*2419e55eSJorge Ramirez-Ortiz spin_unlock(&fl->lock); 1442*2419e55eSJorge Ramirez-Ortiz 1443*2419e55eSJorge Ramirez-Ortiz if (copy_to_user((void __user *)argp, &req, sizeof(req))) { 1444*2419e55eSJorge Ramirez-Ortiz /* unmap the memory and release the buffer */ 1445*2419e55eSJorge Ramirez-Ortiz req_unmap.vaddrout = buf->raddr; 1446*2419e55eSJorge Ramirez-Ortiz req_unmap.size = buf->size; 1447*2419e55eSJorge Ramirez-Ortiz fastrpc_req_munmap_impl(fl, &req_unmap); 1448*2419e55eSJorge Ramirez-Ortiz return -EFAULT; 1449*2419e55eSJorge Ramirez-Ortiz } 1450*2419e55eSJorge Ramirez-Ortiz 1451*2419e55eSJorge Ramirez-Ortiz dev_dbg(dev, "mmap\t\tpt 0x%09lx OK [len 0x%08llx]\n", 1452*2419e55eSJorge Ramirez-Ortiz buf->raddr, buf->size); 1453*2419e55eSJorge Ramirez-Ortiz 1454*2419e55eSJorge Ramirez-Ortiz return 0; 1455*2419e55eSJorge Ramirez-Ortiz 1456*2419e55eSJorge Ramirez-Ortiz err_invoke: 1457*2419e55eSJorge Ramirez-Ortiz fastrpc_buf_free(buf); 1458*2419e55eSJorge Ramirez-Ortiz 1459*2419e55eSJorge Ramirez-Ortiz return err; 1460*2419e55eSJorge Ramirez-Ortiz } 1461*2419e55eSJorge Ramirez-Ortiz 1462c68cfb71SSrinivas Kandagatla static long fastrpc_device_ioctl(struct file *file, unsigned int cmd, 1463c68cfb71SSrinivas Kandagatla unsigned long arg) 1464c68cfb71SSrinivas Kandagatla { 1465c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data; 1466c68cfb71SSrinivas Kandagatla char __user *argp = (char __user *)arg; 1467c68cfb71SSrinivas Kandagatla int err; 1468c68cfb71SSrinivas Kandagatla 1469c68cfb71SSrinivas Kandagatla switch (cmd) { 1470c68cfb71SSrinivas Kandagatla case FASTRPC_IOCTL_INVOKE: 1471c68cfb71SSrinivas Kandagatla err = fastrpc_invoke(fl, argp); 1472c68cfb71SSrinivas Kandagatla break; 1473d73f71c7SSrinivas Kandagatla case FASTRPC_IOCTL_INIT_ATTACH: 1474d73f71c7SSrinivas Kandagatla err = fastrpc_init_attach(fl); 1475d73f71c7SSrinivas Kandagatla break; 1476d73f71c7SSrinivas Kandagatla case FASTRPC_IOCTL_INIT_CREATE: 1477d73f71c7SSrinivas Kandagatla err = fastrpc_init_create_process(fl, argp); 1478d73f71c7SSrinivas Kandagatla break; 14796cffd795SSrinivas Kandagatla case FASTRPC_IOCTL_ALLOC_DMA_BUFF: 14806cffd795SSrinivas Kandagatla err = fastrpc_dmabuf_alloc(fl, argp); 14816cffd795SSrinivas Kandagatla break; 1482*2419e55eSJorge Ramirez-Ortiz case FASTRPC_IOCTL_MMAP: 1483*2419e55eSJorge Ramirez-Ortiz err = fastrpc_req_mmap(fl, argp); 1484*2419e55eSJorge Ramirez-Ortiz break; 1485*2419e55eSJorge Ramirez-Ortiz case FASTRPC_IOCTL_MUNMAP: 1486*2419e55eSJorge Ramirez-Ortiz err = fastrpc_req_munmap(fl, argp); 1487*2419e55eSJorge Ramirez-Ortiz break; 1488c68cfb71SSrinivas Kandagatla default: 1489c68cfb71SSrinivas Kandagatla err = -ENOTTY; 1490c68cfb71SSrinivas Kandagatla break; 1491c68cfb71SSrinivas Kandagatla } 1492c68cfb71SSrinivas Kandagatla 1493c68cfb71SSrinivas Kandagatla return err; 1494c68cfb71SSrinivas Kandagatla } 1495c68cfb71SSrinivas Kandagatla 1496f6f9279fSSrinivas Kandagatla static const struct file_operations fastrpc_fops = { 1497f6f9279fSSrinivas Kandagatla .open = fastrpc_device_open, 1498f6f9279fSSrinivas Kandagatla .release = fastrpc_device_release, 1499c68cfb71SSrinivas Kandagatla .unlocked_ioctl = fastrpc_device_ioctl, 1500c68cfb71SSrinivas Kandagatla .compat_ioctl = fastrpc_device_ioctl, 1501f6f9279fSSrinivas Kandagatla }; 1502f6f9279fSSrinivas Kandagatla 1503f6f9279fSSrinivas Kandagatla static int fastrpc_cb_probe(struct platform_device *pdev) 1504f6f9279fSSrinivas Kandagatla { 1505f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 1506f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *sess; 1507f6f9279fSSrinivas Kandagatla struct device *dev = &pdev->dev; 1508f6f9279fSSrinivas Kandagatla int i, sessions = 0; 1509977e6c8dSSrinivas Kandagatla unsigned long flags; 151001b76c32SBo YU int rc; 1511f6f9279fSSrinivas Kandagatla 1512f6f9279fSSrinivas Kandagatla cctx = dev_get_drvdata(dev->parent); 1513f6f9279fSSrinivas Kandagatla if (!cctx) 1514f6f9279fSSrinivas Kandagatla return -EINVAL; 1515f6f9279fSSrinivas Kandagatla 1516f6f9279fSSrinivas Kandagatla of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions); 1517f6f9279fSSrinivas Kandagatla 1518977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1519f6f9279fSSrinivas Kandagatla sess = &cctx->session[cctx->sesscount]; 1520f6f9279fSSrinivas Kandagatla sess->used = false; 1521f6f9279fSSrinivas Kandagatla sess->valid = true; 1522f6f9279fSSrinivas Kandagatla sess->dev = dev; 1523f6f9279fSSrinivas Kandagatla dev_set_drvdata(dev, sess); 1524f6f9279fSSrinivas Kandagatla 1525f6f9279fSSrinivas Kandagatla if (of_property_read_u32(dev->of_node, "reg", &sess->sid)) 1526f6f9279fSSrinivas Kandagatla dev_info(dev, "FastRPC Session ID not specified in DT\n"); 1527f6f9279fSSrinivas Kandagatla 1528f6f9279fSSrinivas Kandagatla if (sessions > 0) { 1529f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *dup_sess; 1530f6f9279fSSrinivas Kandagatla 1531f6f9279fSSrinivas Kandagatla for (i = 1; i < sessions; i++) { 1532f6f9279fSSrinivas Kandagatla if (cctx->sesscount++ >= FASTRPC_MAX_SESSIONS) 1533f6f9279fSSrinivas Kandagatla break; 1534f6f9279fSSrinivas Kandagatla dup_sess = &cctx->session[cctx->sesscount]; 1535f6f9279fSSrinivas Kandagatla memcpy(dup_sess, sess, sizeof(*dup_sess)); 1536f6f9279fSSrinivas Kandagatla } 1537f6f9279fSSrinivas Kandagatla } 1538f6f9279fSSrinivas Kandagatla cctx->sesscount++; 1539977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 154001b76c32SBo YU rc = dma_set_mask(dev, DMA_BIT_MASK(32)); 154101b76c32SBo YU if (rc) { 154201b76c32SBo YU dev_err(dev, "32-bit DMA enable failed\n"); 154301b76c32SBo YU return rc; 154401b76c32SBo YU } 1545f6f9279fSSrinivas Kandagatla 1546f6f9279fSSrinivas Kandagatla return 0; 1547f6f9279fSSrinivas Kandagatla } 1548f6f9279fSSrinivas Kandagatla 1549f6f9279fSSrinivas Kandagatla static int fastrpc_cb_remove(struct platform_device *pdev) 1550f6f9279fSSrinivas Kandagatla { 1551f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent); 1552f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev); 1553977e6c8dSSrinivas Kandagatla unsigned long flags; 1554f6f9279fSSrinivas Kandagatla int i; 1555f6f9279fSSrinivas Kandagatla 1556977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1557f6f9279fSSrinivas Kandagatla for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) { 1558f6f9279fSSrinivas Kandagatla if (cctx->session[i].sid == sess->sid) { 1559f6f9279fSSrinivas Kandagatla cctx->session[i].valid = false; 1560f6f9279fSSrinivas Kandagatla cctx->sesscount--; 1561f6f9279fSSrinivas Kandagatla } 1562f6f9279fSSrinivas Kandagatla } 1563977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1564f6f9279fSSrinivas Kandagatla 1565f6f9279fSSrinivas Kandagatla return 0; 1566f6f9279fSSrinivas Kandagatla } 1567f6f9279fSSrinivas Kandagatla 1568f6f9279fSSrinivas Kandagatla static const struct of_device_id fastrpc_match_table[] = { 1569f6f9279fSSrinivas Kandagatla { .compatible = "qcom,fastrpc-compute-cb", }, 1570f6f9279fSSrinivas Kandagatla {} 1571f6f9279fSSrinivas Kandagatla }; 1572f6f9279fSSrinivas Kandagatla 1573f6f9279fSSrinivas Kandagatla static struct platform_driver fastrpc_cb_driver = { 1574f6f9279fSSrinivas Kandagatla .probe = fastrpc_cb_probe, 1575f6f9279fSSrinivas Kandagatla .remove = fastrpc_cb_remove, 1576f6f9279fSSrinivas Kandagatla .driver = { 1577f6f9279fSSrinivas Kandagatla .name = "qcom,fastrpc-cb", 1578f6f9279fSSrinivas Kandagatla .of_match_table = fastrpc_match_table, 1579f6f9279fSSrinivas Kandagatla .suppress_bind_attrs = true, 1580f6f9279fSSrinivas Kandagatla }, 1581f6f9279fSSrinivas Kandagatla }; 1582f6f9279fSSrinivas Kandagatla 1583f6f9279fSSrinivas Kandagatla static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) 1584f6f9279fSSrinivas Kandagatla { 1585f6f9279fSSrinivas Kandagatla struct device *rdev = &rpdev->dev; 1586f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *data; 1587f6f9279fSSrinivas Kandagatla int i, err, domain_id = -1; 1588f6f9279fSSrinivas Kandagatla const char *domain; 1589f6f9279fSSrinivas Kandagatla 1590f6f9279fSSrinivas Kandagatla err = of_property_read_string(rdev->of_node, "label", &domain); 1591f6f9279fSSrinivas Kandagatla if (err) { 1592f6f9279fSSrinivas Kandagatla dev_info(rdev, "FastRPC Domain not specified in DT\n"); 1593f6f9279fSSrinivas Kandagatla return err; 1594f6f9279fSSrinivas Kandagatla } 1595f6f9279fSSrinivas Kandagatla 1596f6f9279fSSrinivas Kandagatla for (i = 0; i <= CDSP_DOMAIN_ID; i++) { 1597f6f9279fSSrinivas Kandagatla if (!strcmp(domains[i], domain)) { 1598f6f9279fSSrinivas Kandagatla domain_id = i; 1599f6f9279fSSrinivas Kandagatla break; 1600f6f9279fSSrinivas Kandagatla } 1601f6f9279fSSrinivas Kandagatla } 1602f6f9279fSSrinivas Kandagatla 1603f6f9279fSSrinivas Kandagatla if (domain_id < 0) { 1604f6f9279fSSrinivas Kandagatla dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id); 1605f6f9279fSSrinivas Kandagatla return -EINVAL; 1606f6f9279fSSrinivas Kandagatla } 1607f6f9279fSSrinivas Kandagatla 1608278d56f9SBjorn Andersson data = kzalloc(sizeof(*data), GFP_KERNEL); 1609278d56f9SBjorn Andersson if (!data) 1610278d56f9SBjorn Andersson return -ENOMEM; 1611278d56f9SBjorn Andersson 1612f6f9279fSSrinivas Kandagatla data->miscdev.minor = MISC_DYNAMIC_MINOR; 1613f6f9279fSSrinivas Kandagatla data->miscdev.name = kasprintf(GFP_KERNEL, "fastrpc-%s", 1614f6f9279fSSrinivas Kandagatla domains[domain_id]); 1615f6f9279fSSrinivas Kandagatla data->miscdev.fops = &fastrpc_fops; 1616f6f9279fSSrinivas Kandagatla err = misc_register(&data->miscdev); 1617f6f9279fSSrinivas Kandagatla if (err) 1618f6f9279fSSrinivas Kandagatla return err; 1619f6f9279fSSrinivas Kandagatla 1620278d56f9SBjorn Andersson kref_init(&data->refcount); 1621278d56f9SBjorn Andersson 1622f6f9279fSSrinivas Kandagatla dev_set_drvdata(&rpdev->dev, data); 1623f6f9279fSSrinivas Kandagatla dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32)); 1624f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&data->users); 1625f6f9279fSSrinivas Kandagatla spin_lock_init(&data->lock); 1626f6f9279fSSrinivas Kandagatla idr_init(&data->ctx_idr); 1627f6f9279fSSrinivas Kandagatla data->domain_id = domain_id; 1628f6f9279fSSrinivas Kandagatla data->rpdev = rpdev; 1629f6f9279fSSrinivas Kandagatla 1630f6f9279fSSrinivas Kandagatla return of_platform_populate(rdev->of_node, NULL, NULL, rdev); 1631f6f9279fSSrinivas Kandagatla } 1632f6f9279fSSrinivas Kandagatla 1633c68cfb71SSrinivas Kandagatla static void fastrpc_notify_users(struct fastrpc_user *user) 1634c68cfb71SSrinivas Kandagatla { 1635c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx; 1636c68cfb71SSrinivas Kandagatla 1637c68cfb71SSrinivas Kandagatla spin_lock(&user->lock); 1638c68cfb71SSrinivas Kandagatla list_for_each_entry(ctx, &user->pending, node) 1639c68cfb71SSrinivas Kandagatla complete(&ctx->work); 1640c68cfb71SSrinivas Kandagatla spin_unlock(&user->lock); 1641c68cfb71SSrinivas Kandagatla } 1642c68cfb71SSrinivas Kandagatla 1643f6f9279fSSrinivas Kandagatla static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev) 1644f6f9279fSSrinivas Kandagatla { 1645f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev); 1646c68cfb71SSrinivas Kandagatla struct fastrpc_user *user; 1647977e6c8dSSrinivas Kandagatla unsigned long flags; 1648c68cfb71SSrinivas Kandagatla 1649977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1650c68cfb71SSrinivas Kandagatla list_for_each_entry(user, &cctx->users, user) 1651c68cfb71SSrinivas Kandagatla fastrpc_notify_users(user); 1652977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1653f6f9279fSSrinivas Kandagatla 1654f6f9279fSSrinivas Kandagatla misc_deregister(&cctx->miscdev); 1655f6f9279fSSrinivas Kandagatla of_platform_depopulate(&rpdev->dev); 1656278d56f9SBjorn Andersson 16572e369878SBjorn Andersson cctx->rpdev = NULL; 1658278d56f9SBjorn Andersson fastrpc_channel_ctx_put(cctx); 1659f6f9279fSSrinivas Kandagatla } 1660f6f9279fSSrinivas Kandagatla 1661f6f9279fSSrinivas Kandagatla static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data, 1662f6f9279fSSrinivas Kandagatla int len, void *priv, u32 addr) 1663f6f9279fSSrinivas Kandagatla { 1664c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev); 1665c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_rsp *rsp = data; 1666c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx; 1667c68cfb71SSrinivas Kandagatla unsigned long flags; 1668c68cfb71SSrinivas Kandagatla unsigned long ctxid; 1669c68cfb71SSrinivas Kandagatla 1670c68cfb71SSrinivas Kandagatla if (len < sizeof(*rsp)) 1671c68cfb71SSrinivas Kandagatla return -EINVAL; 1672c68cfb71SSrinivas Kandagatla 1673c68cfb71SSrinivas Kandagatla ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4); 1674c68cfb71SSrinivas Kandagatla 1675c68cfb71SSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1676c68cfb71SSrinivas Kandagatla ctx = idr_find(&cctx->ctx_idr, ctxid); 1677c68cfb71SSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1678c68cfb71SSrinivas Kandagatla 1679c68cfb71SSrinivas Kandagatla if (!ctx) { 1680c68cfb71SSrinivas Kandagatla dev_err(&rpdev->dev, "No context ID matches response\n"); 1681c68cfb71SSrinivas Kandagatla return -ENOENT; 1682c68cfb71SSrinivas Kandagatla } 1683c68cfb71SSrinivas Kandagatla 1684c68cfb71SSrinivas Kandagatla ctx->retval = rsp->retval; 1685c68cfb71SSrinivas Kandagatla complete(&ctx->work); 16868e7389c7SThierry Escande 16878e7389c7SThierry Escande /* 16888e7389c7SThierry Escande * The DMA buffer associated with the context cannot be freed in 16898e7389c7SThierry Escande * interrupt context so schedule it through a worker thread to 16908e7389c7SThierry Escande * avoid a kernel BUG. 16918e7389c7SThierry Escande */ 16928e7389c7SThierry Escande schedule_work(&ctx->put_work); 1693c68cfb71SSrinivas Kandagatla 1694f6f9279fSSrinivas Kandagatla return 0; 1695f6f9279fSSrinivas Kandagatla } 1696f6f9279fSSrinivas Kandagatla 1697f6f9279fSSrinivas Kandagatla static const struct of_device_id fastrpc_rpmsg_of_match[] = { 1698f6f9279fSSrinivas Kandagatla { .compatible = "qcom,fastrpc" }, 1699f6f9279fSSrinivas Kandagatla { }, 1700f6f9279fSSrinivas Kandagatla }; 1701f6f9279fSSrinivas Kandagatla MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match); 1702f6f9279fSSrinivas Kandagatla 1703f6f9279fSSrinivas Kandagatla static struct rpmsg_driver fastrpc_driver = { 1704f6f9279fSSrinivas Kandagatla .probe = fastrpc_rpmsg_probe, 1705f6f9279fSSrinivas Kandagatla .remove = fastrpc_rpmsg_remove, 1706f6f9279fSSrinivas Kandagatla .callback = fastrpc_rpmsg_callback, 1707f6f9279fSSrinivas Kandagatla .drv = { 1708f6f9279fSSrinivas Kandagatla .name = "qcom,fastrpc", 1709f6f9279fSSrinivas Kandagatla .of_match_table = fastrpc_rpmsg_of_match, 1710f6f9279fSSrinivas Kandagatla }, 1711f6f9279fSSrinivas Kandagatla }; 1712f6f9279fSSrinivas Kandagatla 1713f6f9279fSSrinivas Kandagatla static int fastrpc_init(void) 1714f6f9279fSSrinivas Kandagatla { 1715f6f9279fSSrinivas Kandagatla int ret; 1716f6f9279fSSrinivas Kandagatla 1717f6f9279fSSrinivas Kandagatla ret = platform_driver_register(&fastrpc_cb_driver); 1718f6f9279fSSrinivas Kandagatla if (ret < 0) { 1719f6f9279fSSrinivas Kandagatla pr_err("fastrpc: failed to register cb driver\n"); 1720f6f9279fSSrinivas Kandagatla return ret; 1721f6f9279fSSrinivas Kandagatla } 1722f6f9279fSSrinivas Kandagatla 1723f6f9279fSSrinivas Kandagatla ret = register_rpmsg_driver(&fastrpc_driver); 1724f6f9279fSSrinivas Kandagatla if (ret < 0) { 1725f6f9279fSSrinivas Kandagatla pr_err("fastrpc: failed to register rpmsg driver\n"); 1726f6f9279fSSrinivas Kandagatla platform_driver_unregister(&fastrpc_cb_driver); 1727f6f9279fSSrinivas Kandagatla return ret; 1728f6f9279fSSrinivas Kandagatla } 1729f6f9279fSSrinivas Kandagatla 1730f6f9279fSSrinivas Kandagatla return 0; 1731f6f9279fSSrinivas Kandagatla } 1732f6f9279fSSrinivas Kandagatla module_init(fastrpc_init); 1733f6f9279fSSrinivas Kandagatla 1734f6f9279fSSrinivas Kandagatla static void fastrpc_exit(void) 1735f6f9279fSSrinivas Kandagatla { 1736f6f9279fSSrinivas Kandagatla platform_driver_unregister(&fastrpc_cb_driver); 1737f6f9279fSSrinivas Kandagatla unregister_rpmsg_driver(&fastrpc_driver); 1738f6f9279fSSrinivas Kandagatla } 1739f6f9279fSSrinivas Kandagatla module_exit(fastrpc_exit); 1740f6f9279fSSrinivas Kandagatla 1741f6f9279fSSrinivas Kandagatla MODULE_LICENSE("GPL v2"); 1742