1f6f9279fSSrinivas Kandagatla // SPDX-License-Identifier: GPL-2.0 2f6f9279fSSrinivas Kandagatla // Copyright (c) 2011-2018, The Linux Foundation. All rights reserved. 3f6f9279fSSrinivas Kandagatla // Copyright (c) 2018, Linaro Limited 4f6f9279fSSrinivas Kandagatla 5c68cfb71SSrinivas Kandagatla #include <linux/completion.h> 6f6f9279fSSrinivas Kandagatla #include <linux/device.h> 7c68cfb71SSrinivas Kandagatla #include <linux/dma-buf.h> 8f6f9279fSSrinivas Kandagatla #include <linux/dma-mapping.h> 9f6f9279fSSrinivas Kandagatla #include <linux/idr.h> 10f6f9279fSSrinivas Kandagatla #include <linux/list.h> 11f6f9279fSSrinivas Kandagatla #include <linux/miscdevice.h> 12f6f9279fSSrinivas Kandagatla #include <linux/module.h> 13f6f9279fSSrinivas Kandagatla #include <linux/of_address.h> 14f6f9279fSSrinivas Kandagatla #include <linux/of.h> 15f6f9279fSSrinivas Kandagatla #include <linux/of_platform.h> 16f6f9279fSSrinivas Kandagatla #include <linux/rpmsg.h> 17f6f9279fSSrinivas Kandagatla #include <linux/scatterlist.h> 18f6f9279fSSrinivas Kandagatla #include <linux/slab.h> 19c68cfb71SSrinivas Kandagatla #include <uapi/misc/fastrpc.h> 20f6f9279fSSrinivas Kandagatla 21f6f9279fSSrinivas Kandagatla #define ADSP_DOMAIN_ID (0) 22f6f9279fSSrinivas Kandagatla #define MDSP_DOMAIN_ID (1) 23f6f9279fSSrinivas Kandagatla #define SDSP_DOMAIN_ID (2) 24f6f9279fSSrinivas Kandagatla #define CDSP_DOMAIN_ID (3) 25f6f9279fSSrinivas Kandagatla #define FASTRPC_DEV_MAX 4 /* adsp, mdsp, slpi, cdsp*/ 26f6f9279fSSrinivas Kandagatla #define FASTRPC_MAX_SESSIONS 9 /*8 compute, 1 cpz*/ 27c68cfb71SSrinivas Kandagatla #define FASTRPC_ALIGN 128 28c68cfb71SSrinivas Kandagatla #define FASTRPC_MAX_FDLIST 16 29c68cfb71SSrinivas Kandagatla #define FASTRPC_MAX_CRCLIST 64 30c68cfb71SSrinivas Kandagatla #define FASTRPC_PHYS(p) ((p) & 0xffffffff) 31f6f9279fSSrinivas Kandagatla #define FASTRPC_CTX_MAX (256) 32d73f71c7SSrinivas Kandagatla #define FASTRPC_INIT_HANDLE 1 33f6f9279fSSrinivas Kandagatla #define FASTRPC_CTXID_MASK (0xFF0) 34d73f71c7SSrinivas Kandagatla #define INIT_FILELEN_MAX (2 * 1024 * 1024) 35d73f71c7SSrinivas Kandagatla #define INIT_MEMLEN_MAX (8 * 1024 * 1024) 36f6f9279fSSrinivas Kandagatla #define FASTRPC_DEVICE_NAME "fastrpc" 37f6f9279fSSrinivas Kandagatla 38c68cfb71SSrinivas Kandagatla /* Retrives number of input buffers from the scalars parameter */ 39c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_INBUFS(sc) (((sc) >> 16) & 0x0ff) 40c68cfb71SSrinivas Kandagatla 41c68cfb71SSrinivas Kandagatla /* Retrives number of output buffers from the scalars parameter */ 42c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_OUTBUFS(sc) (((sc) >> 8) & 0x0ff) 43c68cfb71SSrinivas Kandagatla 44c68cfb71SSrinivas Kandagatla /* Retrives number of input handles from the scalars parameter */ 45c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_INHANDLES(sc) (((sc) >> 4) & 0x0f) 46c68cfb71SSrinivas Kandagatla 47c68cfb71SSrinivas Kandagatla /* Retrives number of output handles from the scalars parameter */ 48c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_OUTHANDLES(sc) ((sc) & 0x0f) 49c68cfb71SSrinivas Kandagatla 50c68cfb71SSrinivas Kandagatla #define REMOTE_SCALARS_LENGTH(sc) (REMOTE_SCALARS_INBUFS(sc) + \ 51c68cfb71SSrinivas Kandagatla REMOTE_SCALARS_OUTBUFS(sc) + \ 52c68cfb71SSrinivas Kandagatla REMOTE_SCALARS_INHANDLES(sc)+ \ 53c68cfb71SSrinivas Kandagatla REMOTE_SCALARS_OUTHANDLES(sc)) 54c68cfb71SSrinivas Kandagatla #define FASTRPC_BUILD_SCALARS(attr, method, in, out, oin, oout) \ 55c68cfb71SSrinivas Kandagatla (((attr & 0x07) << 29) | \ 56c68cfb71SSrinivas Kandagatla ((method & 0x1f) << 24) | \ 57c68cfb71SSrinivas Kandagatla ((in & 0xff) << 16) | \ 58c68cfb71SSrinivas Kandagatla ((out & 0xff) << 8) | \ 59c68cfb71SSrinivas Kandagatla ((oin & 0x0f) << 4) | \ 60c68cfb71SSrinivas Kandagatla (oout & 0x0f)) 61c68cfb71SSrinivas Kandagatla 62c68cfb71SSrinivas Kandagatla #define FASTRPC_SCALARS(method, in, out) \ 63c68cfb71SSrinivas Kandagatla FASTRPC_BUILD_SCALARS(0, method, in, out, 0, 0) 64c68cfb71SSrinivas Kandagatla 65d73f71c7SSrinivas Kandagatla #define FASTRPC_CREATE_PROCESS_NARGS 6 66d73f71c7SSrinivas Kandagatla /* Remote Method id table */ 67d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_ATTACH 0 68d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_RELEASE 1 69d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE 6 70d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE_ATTR 7 71d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE_STATIC 8 72d73f71c7SSrinivas Kandagatla 73f6f9279fSSrinivas Kandagatla #define miscdev_to_cctx(d) container_of(d, struct fastrpc_channel_ctx, miscdev) 74f6f9279fSSrinivas Kandagatla 75f6f9279fSSrinivas Kandagatla static const char *domains[FASTRPC_DEV_MAX] = { "adsp", "mdsp", 76f6f9279fSSrinivas Kandagatla "sdsp", "cdsp"}; 77c68cfb71SSrinivas Kandagatla struct fastrpc_phy_page { 78c68cfb71SSrinivas Kandagatla u64 addr; /* physical address */ 79c68cfb71SSrinivas Kandagatla u64 size; /* size of contiguous region */ 80c68cfb71SSrinivas Kandagatla }; 81c68cfb71SSrinivas Kandagatla 82c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_buf { 83c68cfb71SSrinivas Kandagatla u32 num; /* number of contiguous regions */ 84c68cfb71SSrinivas Kandagatla u32 pgidx; /* index to start of contiguous region */ 85c68cfb71SSrinivas Kandagatla }; 86c68cfb71SSrinivas Kandagatla 87c68cfb71SSrinivas Kandagatla struct fastrpc_remote_arg { 88c68cfb71SSrinivas Kandagatla u64 pv; 89c68cfb71SSrinivas Kandagatla u64 len; 90c68cfb71SSrinivas Kandagatla }; 91c68cfb71SSrinivas Kandagatla 92c68cfb71SSrinivas Kandagatla struct fastrpc_msg { 93c68cfb71SSrinivas Kandagatla int pid; /* process group id */ 94c68cfb71SSrinivas Kandagatla int tid; /* thread id */ 95c68cfb71SSrinivas Kandagatla u64 ctx; /* invoke caller context */ 96c68cfb71SSrinivas Kandagatla u32 handle; /* handle to invoke */ 97c68cfb71SSrinivas Kandagatla u32 sc; /* scalars structure describing the data */ 98c68cfb71SSrinivas Kandagatla u64 addr; /* physical address */ 99c68cfb71SSrinivas Kandagatla u64 size; /* size of contiguous region */ 100c68cfb71SSrinivas Kandagatla }; 101c68cfb71SSrinivas Kandagatla 102c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_rsp { 103c68cfb71SSrinivas Kandagatla u64 ctx; /* invoke caller context */ 104c68cfb71SSrinivas Kandagatla int retval; /* invoke return value */ 105c68cfb71SSrinivas Kandagatla }; 106c68cfb71SSrinivas Kandagatla 107c68cfb71SSrinivas Kandagatla struct fastrpc_buf { 108c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl; 1096cffd795SSrinivas Kandagatla struct dma_buf *dmabuf; 110c68cfb71SSrinivas Kandagatla struct device *dev; 111c68cfb71SSrinivas Kandagatla void *virt; 112c68cfb71SSrinivas Kandagatla u64 phys; 113c68cfb71SSrinivas Kandagatla u64 size; 1146cffd795SSrinivas Kandagatla /* Lock for dma buf attachments */ 1156cffd795SSrinivas Kandagatla struct mutex lock; 1166cffd795SSrinivas Kandagatla struct list_head attachments; 1176cffd795SSrinivas Kandagatla }; 1186cffd795SSrinivas Kandagatla 1196cffd795SSrinivas Kandagatla struct fastrpc_dma_buf_attachment { 1206cffd795SSrinivas Kandagatla struct device *dev; 1216cffd795SSrinivas Kandagatla struct sg_table sgt; 1226cffd795SSrinivas Kandagatla struct list_head node; 123c68cfb71SSrinivas Kandagatla }; 124c68cfb71SSrinivas Kandagatla 125c68cfb71SSrinivas Kandagatla struct fastrpc_map { 126c68cfb71SSrinivas Kandagatla struct list_head node; 127c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl; 128c68cfb71SSrinivas Kandagatla int fd; 129c68cfb71SSrinivas Kandagatla struct dma_buf *buf; 130c68cfb71SSrinivas Kandagatla struct sg_table *table; 131c68cfb71SSrinivas Kandagatla struct dma_buf_attachment *attach; 132c68cfb71SSrinivas Kandagatla u64 phys; 133c68cfb71SSrinivas Kandagatla u64 size; 134c68cfb71SSrinivas Kandagatla void *va; 135c68cfb71SSrinivas Kandagatla u64 len; 136c68cfb71SSrinivas Kandagatla struct kref refcount; 137c68cfb71SSrinivas Kandagatla }; 138c68cfb71SSrinivas Kandagatla 139c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx { 140c68cfb71SSrinivas Kandagatla int nscalars; 141c68cfb71SSrinivas Kandagatla int nbufs; 142c68cfb71SSrinivas Kandagatla int retval; 143c68cfb71SSrinivas Kandagatla int pid; 144c68cfb71SSrinivas Kandagatla int tgid; 145c68cfb71SSrinivas Kandagatla u32 sc; 146c68cfb71SSrinivas Kandagatla u32 *crc; 147c68cfb71SSrinivas Kandagatla u64 ctxid; 148c68cfb71SSrinivas Kandagatla u64 msg_sz; 149c68cfb71SSrinivas Kandagatla struct kref refcount; 150c68cfb71SSrinivas Kandagatla struct list_head node; /* list of ctxs */ 151c68cfb71SSrinivas Kandagatla struct completion work; 152c68cfb71SSrinivas Kandagatla struct fastrpc_msg msg; 153c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl; 154c68cfb71SSrinivas Kandagatla struct fastrpc_remote_arg *rpra; 155c68cfb71SSrinivas Kandagatla struct fastrpc_map **maps; 156c68cfb71SSrinivas Kandagatla struct fastrpc_buf *buf; 157c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_args *args; 158c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 159c68cfb71SSrinivas Kandagatla }; 160f6f9279fSSrinivas Kandagatla 161f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx { 162f6f9279fSSrinivas Kandagatla struct device *dev; 163f6f9279fSSrinivas Kandagatla int sid; 164f6f9279fSSrinivas Kandagatla bool used; 165f6f9279fSSrinivas Kandagatla bool valid; 166f6f9279fSSrinivas Kandagatla }; 167f6f9279fSSrinivas Kandagatla 168f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx { 169f6f9279fSSrinivas Kandagatla int domain_id; 170f6f9279fSSrinivas Kandagatla int sesscount; 171f6f9279fSSrinivas Kandagatla struct rpmsg_device *rpdev; 172f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS]; 173f6f9279fSSrinivas Kandagatla spinlock_t lock; 174f6f9279fSSrinivas Kandagatla struct idr ctx_idr; 175f6f9279fSSrinivas Kandagatla struct list_head users; 176f6f9279fSSrinivas Kandagatla struct miscdevice miscdev; 177f6f9279fSSrinivas Kandagatla }; 178f6f9279fSSrinivas Kandagatla 179f6f9279fSSrinivas Kandagatla struct fastrpc_user { 180f6f9279fSSrinivas Kandagatla struct list_head user; 181f6f9279fSSrinivas Kandagatla struct list_head maps; 182f6f9279fSSrinivas Kandagatla struct list_head pending; 183f6f9279fSSrinivas Kandagatla 184f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 185f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *sctx; 186c68cfb71SSrinivas Kandagatla struct fastrpc_buf *init_mem; 187f6f9279fSSrinivas Kandagatla 188f6f9279fSSrinivas Kandagatla int tgid; 189f6f9279fSSrinivas Kandagatla int pd; 190f6f9279fSSrinivas Kandagatla /* Lock for lists */ 191f6f9279fSSrinivas Kandagatla spinlock_t lock; 192f6f9279fSSrinivas Kandagatla /* lock for allocations */ 193f6f9279fSSrinivas Kandagatla struct mutex mutex; 194f6f9279fSSrinivas Kandagatla }; 195f6f9279fSSrinivas Kandagatla 196c68cfb71SSrinivas Kandagatla static void fastrpc_free_map(struct kref *ref) 197c68cfb71SSrinivas Kandagatla { 198c68cfb71SSrinivas Kandagatla struct fastrpc_map *map; 199c68cfb71SSrinivas Kandagatla 200c68cfb71SSrinivas Kandagatla map = container_of(ref, struct fastrpc_map, refcount); 201c68cfb71SSrinivas Kandagatla 202c68cfb71SSrinivas Kandagatla if (map->table) { 203c68cfb71SSrinivas Kandagatla dma_buf_unmap_attachment(map->attach, map->table, 204c68cfb71SSrinivas Kandagatla DMA_BIDIRECTIONAL); 205c68cfb71SSrinivas Kandagatla dma_buf_detach(map->buf, map->attach); 206c68cfb71SSrinivas Kandagatla dma_buf_put(map->buf); 207c68cfb71SSrinivas Kandagatla } 208c68cfb71SSrinivas Kandagatla 209c68cfb71SSrinivas Kandagatla kfree(map); 210c68cfb71SSrinivas Kandagatla } 211c68cfb71SSrinivas Kandagatla 212c68cfb71SSrinivas Kandagatla static void fastrpc_map_put(struct fastrpc_map *map) 213c68cfb71SSrinivas Kandagatla { 214c68cfb71SSrinivas Kandagatla if (map) 215c68cfb71SSrinivas Kandagatla kref_put(&map->refcount, fastrpc_free_map); 216c68cfb71SSrinivas Kandagatla } 217c68cfb71SSrinivas Kandagatla 218c68cfb71SSrinivas Kandagatla static void fastrpc_map_get(struct fastrpc_map *map) 219c68cfb71SSrinivas Kandagatla { 220c68cfb71SSrinivas Kandagatla if (map) 221c68cfb71SSrinivas Kandagatla kref_get(&map->refcount); 222c68cfb71SSrinivas Kandagatla } 223c68cfb71SSrinivas Kandagatla 224c68cfb71SSrinivas Kandagatla static int fastrpc_map_find(struct fastrpc_user *fl, int fd, 225c68cfb71SSrinivas Kandagatla struct fastrpc_map **ppmap) 226c68cfb71SSrinivas Kandagatla { 227c68cfb71SSrinivas Kandagatla struct fastrpc_map *map = NULL; 228c68cfb71SSrinivas Kandagatla 229c68cfb71SSrinivas Kandagatla mutex_lock(&fl->mutex); 230c68cfb71SSrinivas Kandagatla list_for_each_entry(map, &fl->maps, node) { 231c68cfb71SSrinivas Kandagatla if (map->fd == fd) { 232c68cfb71SSrinivas Kandagatla fastrpc_map_get(map); 233c68cfb71SSrinivas Kandagatla *ppmap = map; 234c68cfb71SSrinivas Kandagatla mutex_unlock(&fl->mutex); 235c68cfb71SSrinivas Kandagatla return 0; 236c68cfb71SSrinivas Kandagatla } 237c68cfb71SSrinivas Kandagatla } 238c68cfb71SSrinivas Kandagatla mutex_unlock(&fl->mutex); 239c68cfb71SSrinivas Kandagatla 240c68cfb71SSrinivas Kandagatla return -ENOENT; 241c68cfb71SSrinivas Kandagatla } 242c68cfb71SSrinivas Kandagatla 243c68cfb71SSrinivas Kandagatla static void fastrpc_buf_free(struct fastrpc_buf *buf) 244c68cfb71SSrinivas Kandagatla { 245c68cfb71SSrinivas Kandagatla dma_free_coherent(buf->dev, buf->size, buf->virt, 246c68cfb71SSrinivas Kandagatla FASTRPC_PHYS(buf->phys)); 247c68cfb71SSrinivas Kandagatla kfree(buf); 248c68cfb71SSrinivas Kandagatla } 249c68cfb71SSrinivas Kandagatla 250c68cfb71SSrinivas Kandagatla static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev, 251c68cfb71SSrinivas Kandagatla u64 size, struct fastrpc_buf **obuf) 252c68cfb71SSrinivas Kandagatla { 253c68cfb71SSrinivas Kandagatla struct fastrpc_buf *buf; 254c68cfb71SSrinivas Kandagatla 255c68cfb71SSrinivas Kandagatla buf = kzalloc(sizeof(*buf), GFP_KERNEL); 256c68cfb71SSrinivas Kandagatla if (!buf) 257c68cfb71SSrinivas Kandagatla return -ENOMEM; 258c68cfb71SSrinivas Kandagatla 2596cffd795SSrinivas Kandagatla INIT_LIST_HEAD(&buf->attachments); 2606cffd795SSrinivas Kandagatla mutex_init(&buf->lock); 2616cffd795SSrinivas Kandagatla 262c68cfb71SSrinivas Kandagatla buf->fl = fl; 263c68cfb71SSrinivas Kandagatla buf->virt = NULL; 264c68cfb71SSrinivas Kandagatla buf->phys = 0; 265c68cfb71SSrinivas Kandagatla buf->size = size; 266c68cfb71SSrinivas Kandagatla buf->dev = dev; 267c68cfb71SSrinivas Kandagatla 268c68cfb71SSrinivas Kandagatla buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys, 269c68cfb71SSrinivas Kandagatla GFP_KERNEL); 270c68cfb71SSrinivas Kandagatla if (!buf->virt) 271c68cfb71SSrinivas Kandagatla return -ENOMEM; 272c68cfb71SSrinivas Kandagatla 273c68cfb71SSrinivas Kandagatla if (fl->sctx && fl->sctx->sid) 274c68cfb71SSrinivas Kandagatla buf->phys += ((u64)fl->sctx->sid << 32); 275c68cfb71SSrinivas Kandagatla 276c68cfb71SSrinivas Kandagatla *obuf = buf; 277c68cfb71SSrinivas Kandagatla 278c68cfb71SSrinivas Kandagatla return 0; 279c68cfb71SSrinivas Kandagatla } 280c68cfb71SSrinivas Kandagatla 281c68cfb71SSrinivas Kandagatla static void fastrpc_context_free(struct kref *ref) 282c68cfb71SSrinivas Kandagatla { 283c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx; 284c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 285c68cfb71SSrinivas Kandagatla int i; 286c68cfb71SSrinivas Kandagatla 287c68cfb71SSrinivas Kandagatla ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount); 288c68cfb71SSrinivas Kandagatla cctx = ctx->cctx; 289c68cfb71SSrinivas Kandagatla 290c68cfb71SSrinivas Kandagatla for (i = 0; i < ctx->nscalars; i++) 291c68cfb71SSrinivas Kandagatla fastrpc_map_put(ctx->maps[i]); 292c68cfb71SSrinivas Kandagatla 293c68cfb71SSrinivas Kandagatla if (ctx->buf) 294c68cfb71SSrinivas Kandagatla fastrpc_buf_free(ctx->buf); 295c68cfb71SSrinivas Kandagatla 296c68cfb71SSrinivas Kandagatla spin_lock(&cctx->lock); 297c68cfb71SSrinivas Kandagatla idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4); 298c68cfb71SSrinivas Kandagatla spin_unlock(&cctx->lock); 299c68cfb71SSrinivas Kandagatla 300c68cfb71SSrinivas Kandagatla kfree(ctx->maps); 301c68cfb71SSrinivas Kandagatla kfree(ctx); 302c68cfb71SSrinivas Kandagatla } 303c68cfb71SSrinivas Kandagatla 304c68cfb71SSrinivas Kandagatla static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx) 305c68cfb71SSrinivas Kandagatla { 306c68cfb71SSrinivas Kandagatla kref_get(&ctx->refcount); 307c68cfb71SSrinivas Kandagatla } 308c68cfb71SSrinivas Kandagatla 309c68cfb71SSrinivas Kandagatla static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx) 310c68cfb71SSrinivas Kandagatla { 311c68cfb71SSrinivas Kandagatla kref_put(&ctx->refcount, fastrpc_context_free); 312c68cfb71SSrinivas Kandagatla } 313c68cfb71SSrinivas Kandagatla 314c68cfb71SSrinivas Kandagatla static struct fastrpc_invoke_ctx *fastrpc_context_alloc( 315c68cfb71SSrinivas Kandagatla struct fastrpc_user *user, u32 kernel, u32 sc, 316c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_args *args) 317c68cfb71SSrinivas Kandagatla { 318c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = user->cctx; 319c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx = NULL; 320c68cfb71SSrinivas Kandagatla int ret; 321c68cfb71SSrinivas Kandagatla 322c68cfb71SSrinivas Kandagatla ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 323c68cfb71SSrinivas Kandagatla if (!ctx) 324c68cfb71SSrinivas Kandagatla return ERR_PTR(-ENOMEM); 325c68cfb71SSrinivas Kandagatla 326c68cfb71SSrinivas Kandagatla INIT_LIST_HEAD(&ctx->node); 327c68cfb71SSrinivas Kandagatla ctx->fl = user; 328c68cfb71SSrinivas Kandagatla ctx->nscalars = REMOTE_SCALARS_LENGTH(sc); 329c68cfb71SSrinivas Kandagatla ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) + 330c68cfb71SSrinivas Kandagatla REMOTE_SCALARS_OUTBUFS(sc); 331c68cfb71SSrinivas Kandagatla 332c68cfb71SSrinivas Kandagatla if (ctx->nscalars) { 333c68cfb71SSrinivas Kandagatla ctx->maps = kcalloc(ctx->nscalars, 334c68cfb71SSrinivas Kandagatla sizeof(*ctx->maps), GFP_KERNEL); 335c68cfb71SSrinivas Kandagatla if (!ctx->maps) { 336c68cfb71SSrinivas Kandagatla kfree(ctx); 337c68cfb71SSrinivas Kandagatla return ERR_PTR(-ENOMEM); 338c68cfb71SSrinivas Kandagatla } 339c68cfb71SSrinivas Kandagatla ctx->args = args; 340c68cfb71SSrinivas Kandagatla } 341c68cfb71SSrinivas Kandagatla 342c68cfb71SSrinivas Kandagatla ctx->sc = sc; 343c68cfb71SSrinivas Kandagatla ctx->retval = -1; 344c68cfb71SSrinivas Kandagatla ctx->pid = current->pid; 345c68cfb71SSrinivas Kandagatla ctx->tgid = user->tgid; 346c68cfb71SSrinivas Kandagatla ctx->cctx = cctx; 347c68cfb71SSrinivas Kandagatla init_completion(&ctx->work); 348c68cfb71SSrinivas Kandagatla 349c68cfb71SSrinivas Kandagatla spin_lock(&user->lock); 350c68cfb71SSrinivas Kandagatla list_add_tail(&ctx->node, &user->pending); 351c68cfb71SSrinivas Kandagatla spin_unlock(&user->lock); 352c68cfb71SSrinivas Kandagatla 353c68cfb71SSrinivas Kandagatla spin_lock(&cctx->lock); 354c68cfb71SSrinivas Kandagatla ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1, 355c68cfb71SSrinivas Kandagatla FASTRPC_CTX_MAX, GFP_ATOMIC); 356c68cfb71SSrinivas Kandagatla if (ret < 0) { 357c68cfb71SSrinivas Kandagatla spin_unlock(&cctx->lock); 358c68cfb71SSrinivas Kandagatla goto err_idr; 359c68cfb71SSrinivas Kandagatla } 360c68cfb71SSrinivas Kandagatla ctx->ctxid = ret << 4; 361c68cfb71SSrinivas Kandagatla spin_unlock(&cctx->lock); 362c68cfb71SSrinivas Kandagatla 363c68cfb71SSrinivas Kandagatla kref_init(&ctx->refcount); 364c68cfb71SSrinivas Kandagatla 365c68cfb71SSrinivas Kandagatla return ctx; 366c68cfb71SSrinivas Kandagatla err_idr: 367c68cfb71SSrinivas Kandagatla spin_lock(&user->lock); 368c68cfb71SSrinivas Kandagatla list_del(&ctx->node); 369c68cfb71SSrinivas Kandagatla spin_unlock(&user->lock); 370c68cfb71SSrinivas Kandagatla kfree(ctx->maps); 371c68cfb71SSrinivas Kandagatla kfree(ctx); 372c68cfb71SSrinivas Kandagatla 373c68cfb71SSrinivas Kandagatla return ERR_PTR(ret); 374c68cfb71SSrinivas Kandagatla } 375c68cfb71SSrinivas Kandagatla 3766cffd795SSrinivas Kandagatla static struct sg_table * 3776cffd795SSrinivas Kandagatla fastrpc_map_dma_buf(struct dma_buf_attachment *attachment, 3786cffd795SSrinivas Kandagatla enum dma_data_direction dir) 3796cffd795SSrinivas Kandagatla { 3806cffd795SSrinivas Kandagatla struct fastrpc_dma_buf_attachment *a = attachment->priv; 3816cffd795SSrinivas Kandagatla struct sg_table *table; 3826cffd795SSrinivas Kandagatla 3836cffd795SSrinivas Kandagatla table = &a->sgt; 3846cffd795SSrinivas Kandagatla 3856cffd795SSrinivas Kandagatla if (!dma_map_sg(attachment->dev, table->sgl, table->nents, dir)) 3866cffd795SSrinivas Kandagatla return ERR_PTR(-ENOMEM); 3876cffd795SSrinivas Kandagatla 3886cffd795SSrinivas Kandagatla return table; 3896cffd795SSrinivas Kandagatla } 3906cffd795SSrinivas Kandagatla 3916cffd795SSrinivas Kandagatla static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach, 3926cffd795SSrinivas Kandagatla struct sg_table *table, 3936cffd795SSrinivas Kandagatla enum dma_data_direction dir) 3946cffd795SSrinivas Kandagatla { 3956cffd795SSrinivas Kandagatla dma_unmap_sg(attach->dev, table->sgl, table->nents, dir); 3966cffd795SSrinivas Kandagatla } 3976cffd795SSrinivas Kandagatla 3986cffd795SSrinivas Kandagatla static void fastrpc_release(struct dma_buf *dmabuf) 3996cffd795SSrinivas Kandagatla { 4006cffd795SSrinivas Kandagatla struct fastrpc_buf *buffer = dmabuf->priv; 4016cffd795SSrinivas Kandagatla 4026cffd795SSrinivas Kandagatla fastrpc_buf_free(buffer); 4036cffd795SSrinivas Kandagatla } 4046cffd795SSrinivas Kandagatla 4056cffd795SSrinivas Kandagatla static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf, 4066cffd795SSrinivas Kandagatla struct dma_buf_attachment *attachment) 4076cffd795SSrinivas Kandagatla { 4086cffd795SSrinivas Kandagatla struct fastrpc_dma_buf_attachment *a; 4096cffd795SSrinivas Kandagatla struct fastrpc_buf *buffer = dmabuf->priv; 4106cffd795SSrinivas Kandagatla int ret; 4116cffd795SSrinivas Kandagatla 4126cffd795SSrinivas Kandagatla a = kzalloc(sizeof(*a), GFP_KERNEL); 4136cffd795SSrinivas Kandagatla if (!a) 4146cffd795SSrinivas Kandagatla return -ENOMEM; 4156cffd795SSrinivas Kandagatla 4166cffd795SSrinivas Kandagatla ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt, 4176cffd795SSrinivas Kandagatla FASTRPC_PHYS(buffer->phys), buffer->size); 4186cffd795SSrinivas Kandagatla if (ret < 0) { 4196cffd795SSrinivas Kandagatla dev_err(buffer->dev, "failed to get scatterlist from DMA API\n"); 4206cffd795SSrinivas Kandagatla return -EINVAL; 4216cffd795SSrinivas Kandagatla } 4226cffd795SSrinivas Kandagatla 4236cffd795SSrinivas Kandagatla a->dev = attachment->dev; 4246cffd795SSrinivas Kandagatla INIT_LIST_HEAD(&a->node); 4256cffd795SSrinivas Kandagatla attachment->priv = a; 4266cffd795SSrinivas Kandagatla 4276cffd795SSrinivas Kandagatla mutex_lock(&buffer->lock); 4286cffd795SSrinivas Kandagatla list_add(&a->node, &buffer->attachments); 4296cffd795SSrinivas Kandagatla mutex_unlock(&buffer->lock); 4306cffd795SSrinivas Kandagatla 4316cffd795SSrinivas Kandagatla return 0; 4326cffd795SSrinivas Kandagatla } 4336cffd795SSrinivas Kandagatla 4346cffd795SSrinivas Kandagatla static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf, 4356cffd795SSrinivas Kandagatla struct dma_buf_attachment *attachment) 4366cffd795SSrinivas Kandagatla { 4376cffd795SSrinivas Kandagatla struct fastrpc_dma_buf_attachment *a = attachment->priv; 4386cffd795SSrinivas Kandagatla struct fastrpc_buf *buffer = dmabuf->priv; 4396cffd795SSrinivas Kandagatla 4406cffd795SSrinivas Kandagatla mutex_lock(&buffer->lock); 4416cffd795SSrinivas Kandagatla list_del(&a->node); 4426cffd795SSrinivas Kandagatla mutex_unlock(&buffer->lock); 4436cffd795SSrinivas Kandagatla kfree(a); 4446cffd795SSrinivas Kandagatla } 4456cffd795SSrinivas Kandagatla 4466cffd795SSrinivas Kandagatla static void *fastrpc_kmap(struct dma_buf *dmabuf, unsigned long pgnum) 4476cffd795SSrinivas Kandagatla { 4486cffd795SSrinivas Kandagatla struct fastrpc_buf *buf = dmabuf->priv; 4496cffd795SSrinivas Kandagatla 4506cffd795SSrinivas Kandagatla return buf->virt ? buf->virt + pgnum * PAGE_SIZE : NULL; 4516cffd795SSrinivas Kandagatla } 4526cffd795SSrinivas Kandagatla 4536cffd795SSrinivas Kandagatla static void *fastrpc_vmap(struct dma_buf *dmabuf) 4546cffd795SSrinivas Kandagatla { 4556cffd795SSrinivas Kandagatla struct fastrpc_buf *buf = dmabuf->priv; 4566cffd795SSrinivas Kandagatla 4576cffd795SSrinivas Kandagatla return buf->virt; 4586cffd795SSrinivas Kandagatla } 4596cffd795SSrinivas Kandagatla 4606cffd795SSrinivas Kandagatla static int fastrpc_mmap(struct dma_buf *dmabuf, 4616cffd795SSrinivas Kandagatla struct vm_area_struct *vma) 4626cffd795SSrinivas Kandagatla { 4636cffd795SSrinivas Kandagatla struct fastrpc_buf *buf = dmabuf->priv; 4646cffd795SSrinivas Kandagatla size_t size = vma->vm_end - vma->vm_start; 4656cffd795SSrinivas Kandagatla 4666cffd795SSrinivas Kandagatla return dma_mmap_coherent(buf->dev, vma, buf->virt, 4676cffd795SSrinivas Kandagatla FASTRPC_PHYS(buf->phys), size); 4686cffd795SSrinivas Kandagatla } 4696cffd795SSrinivas Kandagatla 4706cffd795SSrinivas Kandagatla static const struct dma_buf_ops fastrpc_dma_buf_ops = { 4716cffd795SSrinivas Kandagatla .attach = fastrpc_dma_buf_attach, 4726cffd795SSrinivas Kandagatla .detach = fastrpc_dma_buf_detatch, 4736cffd795SSrinivas Kandagatla .map_dma_buf = fastrpc_map_dma_buf, 4746cffd795SSrinivas Kandagatla .unmap_dma_buf = fastrpc_unmap_dma_buf, 4756cffd795SSrinivas Kandagatla .mmap = fastrpc_mmap, 4766cffd795SSrinivas Kandagatla .map = fastrpc_kmap, 4776cffd795SSrinivas Kandagatla .vmap = fastrpc_vmap, 4786cffd795SSrinivas Kandagatla .release = fastrpc_release, 4796cffd795SSrinivas Kandagatla }; 4806cffd795SSrinivas Kandagatla 481c68cfb71SSrinivas Kandagatla static int fastrpc_map_create(struct fastrpc_user *fl, int fd, 482c68cfb71SSrinivas Kandagatla u64 len, struct fastrpc_map **ppmap) 483c68cfb71SSrinivas Kandagatla { 484c68cfb71SSrinivas Kandagatla struct fastrpc_session_ctx *sess = fl->sctx; 485c68cfb71SSrinivas Kandagatla struct fastrpc_map *map = NULL; 486c68cfb71SSrinivas Kandagatla int err = 0; 487c68cfb71SSrinivas Kandagatla 488c68cfb71SSrinivas Kandagatla if (!fastrpc_map_find(fl, fd, ppmap)) 489c68cfb71SSrinivas Kandagatla return 0; 490c68cfb71SSrinivas Kandagatla 491c68cfb71SSrinivas Kandagatla map = kzalloc(sizeof(*map), GFP_KERNEL); 492c68cfb71SSrinivas Kandagatla if (!map) 493c68cfb71SSrinivas Kandagatla return -ENOMEM; 494c68cfb71SSrinivas Kandagatla 495c68cfb71SSrinivas Kandagatla INIT_LIST_HEAD(&map->node); 496c68cfb71SSrinivas Kandagatla map->fl = fl; 497c68cfb71SSrinivas Kandagatla map->fd = fd; 498c68cfb71SSrinivas Kandagatla map->buf = dma_buf_get(fd); 499682a6044SWei Yongjun if (IS_ERR(map->buf)) { 500682a6044SWei Yongjun err = PTR_ERR(map->buf); 501c68cfb71SSrinivas Kandagatla goto get_err; 502c68cfb71SSrinivas Kandagatla } 503c68cfb71SSrinivas Kandagatla 504c68cfb71SSrinivas Kandagatla map->attach = dma_buf_attach(map->buf, sess->dev); 505c68cfb71SSrinivas Kandagatla if (IS_ERR(map->attach)) { 506c68cfb71SSrinivas Kandagatla dev_err(sess->dev, "Failed to attach dmabuf\n"); 507c68cfb71SSrinivas Kandagatla err = PTR_ERR(map->attach); 508c68cfb71SSrinivas Kandagatla goto attach_err; 509c68cfb71SSrinivas Kandagatla } 510c68cfb71SSrinivas Kandagatla 511c68cfb71SSrinivas Kandagatla map->table = dma_buf_map_attachment(map->attach, DMA_BIDIRECTIONAL); 512c68cfb71SSrinivas Kandagatla if (IS_ERR(map->table)) { 513c68cfb71SSrinivas Kandagatla err = PTR_ERR(map->table); 514c68cfb71SSrinivas Kandagatla goto map_err; 515c68cfb71SSrinivas Kandagatla } 516c68cfb71SSrinivas Kandagatla 517c68cfb71SSrinivas Kandagatla map->phys = sg_dma_address(map->table->sgl); 518c68cfb71SSrinivas Kandagatla map->phys += ((u64)fl->sctx->sid << 32); 519c68cfb71SSrinivas Kandagatla map->size = len; 520c68cfb71SSrinivas Kandagatla map->va = sg_virt(map->table->sgl); 521c68cfb71SSrinivas Kandagatla map->len = len; 522c68cfb71SSrinivas Kandagatla kref_init(&map->refcount); 523c68cfb71SSrinivas Kandagatla 524c68cfb71SSrinivas Kandagatla spin_lock(&fl->lock); 525c68cfb71SSrinivas Kandagatla list_add_tail(&map->node, &fl->maps); 526c68cfb71SSrinivas Kandagatla spin_unlock(&fl->lock); 527c68cfb71SSrinivas Kandagatla *ppmap = map; 528c68cfb71SSrinivas Kandagatla 529c68cfb71SSrinivas Kandagatla return 0; 530c68cfb71SSrinivas Kandagatla 531c68cfb71SSrinivas Kandagatla map_err: 532c68cfb71SSrinivas Kandagatla dma_buf_detach(map->buf, map->attach); 533c68cfb71SSrinivas Kandagatla attach_err: 534c68cfb71SSrinivas Kandagatla dma_buf_put(map->buf); 535c68cfb71SSrinivas Kandagatla get_err: 536c68cfb71SSrinivas Kandagatla kfree(map); 537c68cfb71SSrinivas Kandagatla 538c68cfb71SSrinivas Kandagatla return err; 539c68cfb71SSrinivas Kandagatla } 540c68cfb71SSrinivas Kandagatla 541c68cfb71SSrinivas Kandagatla /* 542c68cfb71SSrinivas Kandagatla * Fastrpc payload buffer with metadata looks like: 543c68cfb71SSrinivas Kandagatla * 544c68cfb71SSrinivas Kandagatla * >>>>>> START of METADATA <<<<<<<<< 545c68cfb71SSrinivas Kandagatla * +---------------------------------+ 546c68cfb71SSrinivas Kandagatla * | Arguments | 547c68cfb71SSrinivas Kandagatla * | type:(struct fastrpc_remote_arg)| 548c68cfb71SSrinivas Kandagatla * | (0 - N) | 549c68cfb71SSrinivas Kandagatla * +---------------------------------+ 550c68cfb71SSrinivas Kandagatla * | Invoke Buffer list | 551c68cfb71SSrinivas Kandagatla * | type:(struct fastrpc_invoke_buf)| 552c68cfb71SSrinivas Kandagatla * | (0 - N) | 553c68cfb71SSrinivas Kandagatla * +---------------------------------+ 554c68cfb71SSrinivas Kandagatla * | Page info list | 555c68cfb71SSrinivas Kandagatla * | type:(struct fastrpc_phy_page) | 556c68cfb71SSrinivas Kandagatla * | (0 - N) | 557c68cfb71SSrinivas Kandagatla * +---------------------------------+ 558c68cfb71SSrinivas Kandagatla * | Optional info | 559c68cfb71SSrinivas Kandagatla * |(can be specific to SoC/Firmware)| 560c68cfb71SSrinivas Kandagatla * +---------------------------------+ 561c68cfb71SSrinivas Kandagatla * >>>>>>>> END of METADATA <<<<<<<<< 562c68cfb71SSrinivas Kandagatla * +---------------------------------+ 563c68cfb71SSrinivas Kandagatla * | Inline ARGS | 564c68cfb71SSrinivas Kandagatla * | (0-N) | 565c68cfb71SSrinivas Kandagatla * +---------------------------------+ 566c68cfb71SSrinivas Kandagatla */ 567c68cfb71SSrinivas Kandagatla 568c68cfb71SSrinivas Kandagatla static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx) 569c68cfb71SSrinivas Kandagatla { 570c68cfb71SSrinivas Kandagatla int size = 0; 571c68cfb71SSrinivas Kandagatla 572c68cfb71SSrinivas Kandagatla size = (sizeof(struct fastrpc_remote_arg) + 573c68cfb71SSrinivas Kandagatla sizeof(struct fastrpc_invoke_buf) + 574c68cfb71SSrinivas Kandagatla sizeof(struct fastrpc_phy_page)) * ctx->nscalars + 575c68cfb71SSrinivas Kandagatla sizeof(u64) * FASTRPC_MAX_FDLIST + 576c68cfb71SSrinivas Kandagatla sizeof(u32) * FASTRPC_MAX_CRCLIST; 577c68cfb71SSrinivas Kandagatla 578c68cfb71SSrinivas Kandagatla return size; 579c68cfb71SSrinivas Kandagatla } 580c68cfb71SSrinivas Kandagatla 581c68cfb71SSrinivas Kandagatla static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen) 582c68cfb71SSrinivas Kandagatla { 583c68cfb71SSrinivas Kandagatla u64 size = 0; 584c68cfb71SSrinivas Kandagatla int i; 585c68cfb71SSrinivas Kandagatla 586c68cfb71SSrinivas Kandagatla size = ALIGN(metalen, FASTRPC_ALIGN); 587c68cfb71SSrinivas Kandagatla for (i = 0; i < ctx->nscalars; i++) { 588c68cfb71SSrinivas Kandagatla if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) { 589c68cfb71SSrinivas Kandagatla size = ALIGN(size, FASTRPC_ALIGN); 590c68cfb71SSrinivas Kandagatla size += ctx->args[i].length; 591c68cfb71SSrinivas Kandagatla } 592c68cfb71SSrinivas Kandagatla } 593c68cfb71SSrinivas Kandagatla 594c68cfb71SSrinivas Kandagatla return size; 595c68cfb71SSrinivas Kandagatla } 596c68cfb71SSrinivas Kandagatla 597c68cfb71SSrinivas Kandagatla static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx) 598c68cfb71SSrinivas Kandagatla { 599c68cfb71SSrinivas Kandagatla struct device *dev = ctx->fl->sctx->dev; 600c68cfb71SSrinivas Kandagatla int i, err; 601c68cfb71SSrinivas Kandagatla 602c68cfb71SSrinivas Kandagatla for (i = 0; i < ctx->nscalars; ++i) { 603c68cfb71SSrinivas Kandagatla /* Make sure reserved field is set to 0 */ 604c68cfb71SSrinivas Kandagatla if (ctx->args[i].reserved) 605c68cfb71SSrinivas Kandagatla return -EINVAL; 606c68cfb71SSrinivas Kandagatla 607c68cfb71SSrinivas Kandagatla if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 || 608c68cfb71SSrinivas Kandagatla ctx->args[i].length == 0) 609c68cfb71SSrinivas Kandagatla continue; 610c68cfb71SSrinivas Kandagatla 611c68cfb71SSrinivas Kandagatla err = fastrpc_map_create(ctx->fl, ctx->args[i].fd, 612c68cfb71SSrinivas Kandagatla ctx->args[i].length, &ctx->maps[i]); 613c68cfb71SSrinivas Kandagatla if (err) { 614c68cfb71SSrinivas Kandagatla dev_err(dev, "Error Creating map %d\n", err); 615c68cfb71SSrinivas Kandagatla return -EINVAL; 616c68cfb71SSrinivas Kandagatla } 617c68cfb71SSrinivas Kandagatla 618c68cfb71SSrinivas Kandagatla } 619c68cfb71SSrinivas Kandagatla return 0; 620c68cfb71SSrinivas Kandagatla } 621c68cfb71SSrinivas Kandagatla 622c68cfb71SSrinivas Kandagatla static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx) 623c68cfb71SSrinivas Kandagatla { 624c68cfb71SSrinivas Kandagatla struct device *dev = ctx->fl->sctx->dev; 625c68cfb71SSrinivas Kandagatla struct fastrpc_remote_arg *rpra; 626c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_buf *list; 627c68cfb71SSrinivas Kandagatla struct fastrpc_phy_page *pages; 628c68cfb71SSrinivas Kandagatla int inbufs, i, err = 0; 629c68cfb71SSrinivas Kandagatla u64 rlen, pkt_size; 630c68cfb71SSrinivas Kandagatla uintptr_t args; 631c68cfb71SSrinivas Kandagatla int metalen; 632c68cfb71SSrinivas Kandagatla 633c68cfb71SSrinivas Kandagatla 634c68cfb71SSrinivas Kandagatla inbufs = REMOTE_SCALARS_INBUFS(ctx->sc); 635c68cfb71SSrinivas Kandagatla metalen = fastrpc_get_meta_size(ctx); 636c68cfb71SSrinivas Kandagatla pkt_size = fastrpc_get_payload_size(ctx, metalen); 637c68cfb71SSrinivas Kandagatla 638c68cfb71SSrinivas Kandagatla err = fastrpc_create_maps(ctx); 639c68cfb71SSrinivas Kandagatla if (err) 640c68cfb71SSrinivas Kandagatla return err; 641c68cfb71SSrinivas Kandagatla 642c68cfb71SSrinivas Kandagatla ctx->msg_sz = pkt_size; 643c68cfb71SSrinivas Kandagatla 644c68cfb71SSrinivas Kandagatla err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf); 645c68cfb71SSrinivas Kandagatla if (err) 646c68cfb71SSrinivas Kandagatla return err; 647c68cfb71SSrinivas Kandagatla 648c68cfb71SSrinivas Kandagatla rpra = ctx->buf->virt; 649c68cfb71SSrinivas Kandagatla list = ctx->buf->virt + ctx->nscalars * sizeof(*rpra); 650c68cfb71SSrinivas Kandagatla pages = ctx->buf->virt + ctx->nscalars * (sizeof(*list) + 651c68cfb71SSrinivas Kandagatla sizeof(*rpra)); 652c68cfb71SSrinivas Kandagatla args = (uintptr_t)ctx->buf->virt + metalen; 653c68cfb71SSrinivas Kandagatla rlen = pkt_size - metalen; 654c68cfb71SSrinivas Kandagatla ctx->rpra = rpra; 655c68cfb71SSrinivas Kandagatla 656c68cfb71SSrinivas Kandagatla for (i = 0; i < ctx->nbufs; ++i) { 657c68cfb71SSrinivas Kandagatla u64 len = ctx->args[i].length; 658c68cfb71SSrinivas Kandagatla 659c68cfb71SSrinivas Kandagatla rpra[i].pv = 0; 660c68cfb71SSrinivas Kandagatla rpra[i].len = len; 661c68cfb71SSrinivas Kandagatla list[i].num = len ? 1 : 0; 662c68cfb71SSrinivas Kandagatla list[i].pgidx = i; 663c68cfb71SSrinivas Kandagatla 664c68cfb71SSrinivas Kandagatla if (!len) 665c68cfb71SSrinivas Kandagatla continue; 666c68cfb71SSrinivas Kandagatla 667c68cfb71SSrinivas Kandagatla pages[i].size = roundup(len, PAGE_SIZE); 668c68cfb71SSrinivas Kandagatla 669c68cfb71SSrinivas Kandagatla if (ctx->maps[i]) { 670c68cfb71SSrinivas Kandagatla rpra[i].pv = (u64) ctx->args[i].ptr; 671c68cfb71SSrinivas Kandagatla pages[i].addr = ctx->maps[i]->phys; 672c68cfb71SSrinivas Kandagatla } else { 673c68cfb71SSrinivas Kandagatla rlen -= ALIGN(args, FASTRPC_ALIGN) - args; 674c68cfb71SSrinivas Kandagatla args = ALIGN(args, FASTRPC_ALIGN); 675c68cfb71SSrinivas Kandagatla if (rlen < len) 676c68cfb71SSrinivas Kandagatla goto bail; 677c68cfb71SSrinivas Kandagatla 678c68cfb71SSrinivas Kandagatla rpra[i].pv = args; 679c68cfb71SSrinivas Kandagatla pages[i].addr = ctx->buf->phys + (pkt_size - rlen); 680c68cfb71SSrinivas Kandagatla pages[i].addr = pages[i].addr & PAGE_MASK; 681c68cfb71SSrinivas Kandagatla args = args + len; 682c68cfb71SSrinivas Kandagatla rlen -= len; 683c68cfb71SSrinivas Kandagatla } 684c68cfb71SSrinivas Kandagatla 685c68cfb71SSrinivas Kandagatla if (i < inbufs && !ctx->maps[i]) { 686c68cfb71SSrinivas Kandagatla void *dst = (void *)(uintptr_t)rpra[i].pv; 687c68cfb71SSrinivas Kandagatla void *src = (void *)(uintptr_t)ctx->args[i].ptr; 688c68cfb71SSrinivas Kandagatla 689c68cfb71SSrinivas Kandagatla if (!kernel) { 690c68cfb71SSrinivas Kandagatla if (copy_from_user(dst, (void __user *)src, 691c68cfb71SSrinivas Kandagatla len)) { 692c68cfb71SSrinivas Kandagatla err = -EFAULT; 693c68cfb71SSrinivas Kandagatla goto bail; 694c68cfb71SSrinivas Kandagatla } 695c68cfb71SSrinivas Kandagatla } else { 696c68cfb71SSrinivas Kandagatla memcpy(dst, src, len); 697c68cfb71SSrinivas Kandagatla } 698c68cfb71SSrinivas Kandagatla } 699c68cfb71SSrinivas Kandagatla } 700c68cfb71SSrinivas Kandagatla 701c68cfb71SSrinivas Kandagatla for (i = ctx->nbufs; i < ctx->nscalars; ++i) { 702c68cfb71SSrinivas Kandagatla rpra[i].pv = (u64) ctx->args[i].ptr; 703c68cfb71SSrinivas Kandagatla rpra[i].len = ctx->args[i].length; 704c68cfb71SSrinivas Kandagatla list[i].num = ctx->args[i].length ? 1 : 0; 705c68cfb71SSrinivas Kandagatla list[i].pgidx = i; 706c68cfb71SSrinivas Kandagatla pages[i].addr = ctx->maps[i]->phys; 707c68cfb71SSrinivas Kandagatla pages[i].size = ctx->maps[i]->size; 708c68cfb71SSrinivas Kandagatla } 709c68cfb71SSrinivas Kandagatla 710c68cfb71SSrinivas Kandagatla bail: 711c68cfb71SSrinivas Kandagatla if (err) 712c68cfb71SSrinivas Kandagatla dev_err(dev, "Error: get invoke args failed:%d\n", err); 713c68cfb71SSrinivas Kandagatla 714c68cfb71SSrinivas Kandagatla return err; 715c68cfb71SSrinivas Kandagatla } 716c68cfb71SSrinivas Kandagatla 717c68cfb71SSrinivas Kandagatla static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx, 718c68cfb71SSrinivas Kandagatla u32 kernel) 719c68cfb71SSrinivas Kandagatla { 720c68cfb71SSrinivas Kandagatla struct fastrpc_remote_arg *rpra = ctx->rpra; 721c68cfb71SSrinivas Kandagatla int i, inbufs; 722c68cfb71SSrinivas Kandagatla 723c68cfb71SSrinivas Kandagatla inbufs = REMOTE_SCALARS_INBUFS(ctx->sc); 724c68cfb71SSrinivas Kandagatla 725c68cfb71SSrinivas Kandagatla for (i = inbufs; i < ctx->nbufs; ++i) { 726c68cfb71SSrinivas Kandagatla void *src = (void *)(uintptr_t)rpra[i].pv; 727c68cfb71SSrinivas Kandagatla void *dst = (void *)(uintptr_t)ctx->args[i].ptr; 728c68cfb71SSrinivas Kandagatla u64 len = rpra[i].len; 729c68cfb71SSrinivas Kandagatla 730c68cfb71SSrinivas Kandagatla if (!kernel) { 731c68cfb71SSrinivas Kandagatla if (copy_to_user((void __user *)dst, src, len)) 732c68cfb71SSrinivas Kandagatla return -EFAULT; 733c68cfb71SSrinivas Kandagatla } else { 734c68cfb71SSrinivas Kandagatla memcpy(dst, src, len); 735c68cfb71SSrinivas Kandagatla } 736c68cfb71SSrinivas Kandagatla } 737c68cfb71SSrinivas Kandagatla 738c68cfb71SSrinivas Kandagatla return 0; 739c68cfb71SSrinivas Kandagatla } 740c68cfb71SSrinivas Kandagatla 741c68cfb71SSrinivas Kandagatla static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx, 742c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx, 743c68cfb71SSrinivas Kandagatla u32 kernel, uint32_t handle) 744c68cfb71SSrinivas Kandagatla { 745c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 746c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl = ctx->fl; 747c68cfb71SSrinivas Kandagatla struct fastrpc_msg *msg = &ctx->msg; 748c68cfb71SSrinivas Kandagatla 749c68cfb71SSrinivas Kandagatla cctx = fl->cctx; 750c68cfb71SSrinivas Kandagatla msg->pid = fl->tgid; 751c68cfb71SSrinivas Kandagatla msg->tid = current->pid; 752c68cfb71SSrinivas Kandagatla 753c68cfb71SSrinivas Kandagatla if (kernel) 754c68cfb71SSrinivas Kandagatla msg->pid = 0; 755c68cfb71SSrinivas Kandagatla 756c68cfb71SSrinivas Kandagatla msg->ctx = ctx->ctxid | fl->pd; 757c68cfb71SSrinivas Kandagatla msg->handle = handle; 758c68cfb71SSrinivas Kandagatla msg->sc = ctx->sc; 759c68cfb71SSrinivas Kandagatla msg->addr = ctx->buf ? ctx->buf->phys : 0; 760c68cfb71SSrinivas Kandagatla msg->size = roundup(ctx->msg_sz, PAGE_SIZE); 761c68cfb71SSrinivas Kandagatla fastrpc_context_get(ctx); 762c68cfb71SSrinivas Kandagatla 763c68cfb71SSrinivas Kandagatla return rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg)); 764c68cfb71SSrinivas Kandagatla } 765c68cfb71SSrinivas Kandagatla 766c68cfb71SSrinivas Kandagatla static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, 767c68cfb71SSrinivas Kandagatla u32 handle, u32 sc, 768c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_args *args) 769c68cfb71SSrinivas Kandagatla { 770c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx = NULL; 771c68cfb71SSrinivas Kandagatla int err = 0; 772c68cfb71SSrinivas Kandagatla 773c68cfb71SSrinivas Kandagatla if (!fl->sctx) 774c68cfb71SSrinivas Kandagatla return -EINVAL; 775c68cfb71SSrinivas Kandagatla 776c68cfb71SSrinivas Kandagatla ctx = fastrpc_context_alloc(fl, kernel, sc, args); 777c68cfb71SSrinivas Kandagatla if (IS_ERR(ctx)) 778c68cfb71SSrinivas Kandagatla return PTR_ERR(ctx); 779c68cfb71SSrinivas Kandagatla 780c68cfb71SSrinivas Kandagatla if (ctx->nscalars) { 781c68cfb71SSrinivas Kandagatla err = fastrpc_get_args(kernel, ctx); 782c68cfb71SSrinivas Kandagatla if (err) 783c68cfb71SSrinivas Kandagatla goto bail; 784c68cfb71SSrinivas Kandagatla } 785c68cfb71SSrinivas Kandagatla /* Send invoke buffer to remote dsp */ 786c68cfb71SSrinivas Kandagatla err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle); 787c68cfb71SSrinivas Kandagatla if (err) 788c68cfb71SSrinivas Kandagatla goto bail; 789c68cfb71SSrinivas Kandagatla 790c68cfb71SSrinivas Kandagatla /* Wait for remote dsp to respond or time out */ 791c68cfb71SSrinivas Kandagatla err = wait_for_completion_interruptible(&ctx->work); 792c68cfb71SSrinivas Kandagatla if (err) 793c68cfb71SSrinivas Kandagatla goto bail; 794c68cfb71SSrinivas Kandagatla 795c68cfb71SSrinivas Kandagatla /* Check the response from remote dsp */ 796c68cfb71SSrinivas Kandagatla err = ctx->retval; 797c68cfb71SSrinivas Kandagatla if (err) 798c68cfb71SSrinivas Kandagatla goto bail; 799c68cfb71SSrinivas Kandagatla 800c68cfb71SSrinivas Kandagatla if (ctx->nscalars) { 801c68cfb71SSrinivas Kandagatla /* populate all the output buffers with results */ 802c68cfb71SSrinivas Kandagatla err = fastrpc_put_args(ctx, kernel); 803c68cfb71SSrinivas Kandagatla if (err) 804c68cfb71SSrinivas Kandagatla goto bail; 805c68cfb71SSrinivas Kandagatla } 806c68cfb71SSrinivas Kandagatla 807c68cfb71SSrinivas Kandagatla bail: 808c68cfb71SSrinivas Kandagatla /* We are done with this compute context, remove it from pending list */ 809c68cfb71SSrinivas Kandagatla spin_lock(&fl->lock); 810c68cfb71SSrinivas Kandagatla list_del(&ctx->node); 811c68cfb71SSrinivas Kandagatla spin_unlock(&fl->lock); 812c68cfb71SSrinivas Kandagatla fastrpc_context_put(ctx); 813c68cfb71SSrinivas Kandagatla 814c68cfb71SSrinivas Kandagatla if (err) 815c68cfb71SSrinivas Kandagatla dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err); 816c68cfb71SSrinivas Kandagatla 817c68cfb71SSrinivas Kandagatla return err; 818c68cfb71SSrinivas Kandagatla } 819c68cfb71SSrinivas Kandagatla 820d73f71c7SSrinivas Kandagatla static int fastrpc_init_create_process(struct fastrpc_user *fl, 821d73f71c7SSrinivas Kandagatla char __user *argp) 822d73f71c7SSrinivas Kandagatla { 823d73f71c7SSrinivas Kandagatla struct fastrpc_init_create init; 824d73f71c7SSrinivas Kandagatla struct fastrpc_invoke_args *args; 825d73f71c7SSrinivas Kandagatla struct fastrpc_phy_page pages[1]; 826d73f71c7SSrinivas Kandagatla struct fastrpc_map *map = NULL; 827d73f71c7SSrinivas Kandagatla struct fastrpc_buf *imem = NULL; 828d73f71c7SSrinivas Kandagatla int memlen; 829d73f71c7SSrinivas Kandagatla int err; 830d73f71c7SSrinivas Kandagatla struct { 831d73f71c7SSrinivas Kandagatla int pgid; 832d73f71c7SSrinivas Kandagatla u32 namelen; 833d73f71c7SSrinivas Kandagatla u32 filelen; 834d73f71c7SSrinivas Kandagatla u32 pageslen; 835d73f71c7SSrinivas Kandagatla u32 attrs; 836d73f71c7SSrinivas Kandagatla u32 siglen; 837d73f71c7SSrinivas Kandagatla } inbuf; 838d73f71c7SSrinivas Kandagatla u32 sc; 839d73f71c7SSrinivas Kandagatla 840d73f71c7SSrinivas Kandagatla args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL); 841d73f71c7SSrinivas Kandagatla if (!args) 842d73f71c7SSrinivas Kandagatla return -ENOMEM; 843d73f71c7SSrinivas Kandagatla 844d73f71c7SSrinivas Kandagatla if (copy_from_user(&init, argp, sizeof(init))) { 845d73f71c7SSrinivas Kandagatla err = -EFAULT; 846d73f71c7SSrinivas Kandagatla goto bail; 847d73f71c7SSrinivas Kandagatla } 848d73f71c7SSrinivas Kandagatla 849d73f71c7SSrinivas Kandagatla if (init.filelen > INIT_FILELEN_MAX) { 850d73f71c7SSrinivas Kandagatla err = -EINVAL; 851d73f71c7SSrinivas Kandagatla goto bail; 852d73f71c7SSrinivas Kandagatla } 853d73f71c7SSrinivas Kandagatla 854d73f71c7SSrinivas Kandagatla inbuf.pgid = fl->tgid; 855d73f71c7SSrinivas Kandagatla inbuf.namelen = strlen(current->comm) + 1; 856d73f71c7SSrinivas Kandagatla inbuf.filelen = init.filelen; 857d73f71c7SSrinivas Kandagatla inbuf.pageslen = 1; 858d73f71c7SSrinivas Kandagatla inbuf.attrs = init.attrs; 859d73f71c7SSrinivas Kandagatla inbuf.siglen = init.siglen; 860d73f71c7SSrinivas Kandagatla fl->pd = 1; 861d73f71c7SSrinivas Kandagatla 862d73f71c7SSrinivas Kandagatla if (init.filelen && init.filefd) { 863d73f71c7SSrinivas Kandagatla err = fastrpc_map_create(fl, init.filefd, init.filelen, &map); 864d73f71c7SSrinivas Kandagatla if (err) 865d73f71c7SSrinivas Kandagatla goto bail; 866d73f71c7SSrinivas Kandagatla } 867d73f71c7SSrinivas Kandagatla 868d73f71c7SSrinivas Kandagatla memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4), 869d73f71c7SSrinivas Kandagatla 1024 * 1024); 870d73f71c7SSrinivas Kandagatla err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen, 871d73f71c7SSrinivas Kandagatla &imem); 872d73f71c7SSrinivas Kandagatla if (err) { 873d73f71c7SSrinivas Kandagatla fastrpc_map_put(map); 874d73f71c7SSrinivas Kandagatla goto bail; 875d73f71c7SSrinivas Kandagatla } 876d73f71c7SSrinivas Kandagatla 877d73f71c7SSrinivas Kandagatla fl->init_mem = imem; 878d73f71c7SSrinivas Kandagatla args[0].ptr = (u64)(uintptr_t)&inbuf; 879d73f71c7SSrinivas Kandagatla args[0].length = sizeof(inbuf); 880d73f71c7SSrinivas Kandagatla args[0].fd = -1; 881d73f71c7SSrinivas Kandagatla 882d73f71c7SSrinivas Kandagatla args[1].ptr = (u64)(uintptr_t)current->comm; 883d73f71c7SSrinivas Kandagatla args[1].length = inbuf.namelen; 884d73f71c7SSrinivas Kandagatla args[1].fd = -1; 885d73f71c7SSrinivas Kandagatla 886d73f71c7SSrinivas Kandagatla args[2].ptr = (u64) init.file; 887d73f71c7SSrinivas Kandagatla args[2].length = inbuf.filelen; 888d73f71c7SSrinivas Kandagatla args[2].fd = init.filefd; 889d73f71c7SSrinivas Kandagatla 890d73f71c7SSrinivas Kandagatla pages[0].addr = imem->phys; 891d73f71c7SSrinivas Kandagatla pages[0].size = imem->size; 892d73f71c7SSrinivas Kandagatla 893d73f71c7SSrinivas Kandagatla args[3].ptr = (u64)(uintptr_t) pages; 894d73f71c7SSrinivas Kandagatla args[3].length = 1 * sizeof(*pages); 895d73f71c7SSrinivas Kandagatla args[3].fd = -1; 896d73f71c7SSrinivas Kandagatla 897d73f71c7SSrinivas Kandagatla args[4].ptr = (u64)(uintptr_t)&inbuf.attrs; 898d73f71c7SSrinivas Kandagatla args[4].length = sizeof(inbuf.attrs); 899d73f71c7SSrinivas Kandagatla args[4].fd = -1; 900d73f71c7SSrinivas Kandagatla 901d73f71c7SSrinivas Kandagatla args[5].ptr = (u64)(uintptr_t) &inbuf.siglen; 902d73f71c7SSrinivas Kandagatla args[5].length = sizeof(inbuf.siglen); 903d73f71c7SSrinivas Kandagatla args[5].fd = -1; 904d73f71c7SSrinivas Kandagatla 905d73f71c7SSrinivas Kandagatla sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0); 906d73f71c7SSrinivas Kandagatla if (init.attrs) 907d73f71c7SSrinivas Kandagatla sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0); 908d73f71c7SSrinivas Kandagatla 909d73f71c7SSrinivas Kandagatla err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, 910d73f71c7SSrinivas Kandagatla sc, args); 911d73f71c7SSrinivas Kandagatla 912d73f71c7SSrinivas Kandagatla if (err) { 913d73f71c7SSrinivas Kandagatla fastrpc_map_put(map); 914d73f71c7SSrinivas Kandagatla fastrpc_buf_free(imem); 915d73f71c7SSrinivas Kandagatla } 916d73f71c7SSrinivas Kandagatla 917d73f71c7SSrinivas Kandagatla bail: 918d73f71c7SSrinivas Kandagatla kfree(args); 919d73f71c7SSrinivas Kandagatla 920d73f71c7SSrinivas Kandagatla return err; 921d73f71c7SSrinivas Kandagatla } 922d73f71c7SSrinivas Kandagatla 923f6f9279fSSrinivas Kandagatla static struct fastrpc_session_ctx *fastrpc_session_alloc( 924f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx) 925f6f9279fSSrinivas Kandagatla { 926f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *session = NULL; 927f6f9279fSSrinivas Kandagatla int i; 928f6f9279fSSrinivas Kandagatla 929f6f9279fSSrinivas Kandagatla spin_lock(&cctx->lock); 930f6f9279fSSrinivas Kandagatla for (i = 0; i < cctx->sesscount; i++) { 931f6f9279fSSrinivas Kandagatla if (!cctx->session[i].used && cctx->session[i].valid) { 932f6f9279fSSrinivas Kandagatla cctx->session[i].used = true; 933f6f9279fSSrinivas Kandagatla session = &cctx->session[i]; 934f6f9279fSSrinivas Kandagatla break; 935f6f9279fSSrinivas Kandagatla } 936f6f9279fSSrinivas Kandagatla } 937f6f9279fSSrinivas Kandagatla spin_unlock(&cctx->lock); 938f6f9279fSSrinivas Kandagatla 939f6f9279fSSrinivas Kandagatla return session; 940f6f9279fSSrinivas Kandagatla } 941f6f9279fSSrinivas Kandagatla 942f6f9279fSSrinivas Kandagatla static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx, 943f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *session) 944f6f9279fSSrinivas Kandagatla { 945f6f9279fSSrinivas Kandagatla spin_lock(&cctx->lock); 946f6f9279fSSrinivas Kandagatla session->used = false; 947f6f9279fSSrinivas Kandagatla spin_unlock(&cctx->lock); 948f6f9279fSSrinivas Kandagatla } 949f6f9279fSSrinivas Kandagatla 950d73f71c7SSrinivas Kandagatla static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl) 951d73f71c7SSrinivas Kandagatla { 952d73f71c7SSrinivas Kandagatla struct fastrpc_invoke_args args[1]; 953d73f71c7SSrinivas Kandagatla int tgid = 0; 954d73f71c7SSrinivas Kandagatla u32 sc; 955d73f71c7SSrinivas Kandagatla 956d73f71c7SSrinivas Kandagatla tgid = fl->tgid; 957d73f71c7SSrinivas Kandagatla args[0].ptr = (u64)(uintptr_t) &tgid; 958d73f71c7SSrinivas Kandagatla args[0].length = sizeof(tgid); 959d73f71c7SSrinivas Kandagatla args[0].fd = -1; 960d73f71c7SSrinivas Kandagatla args[0].reserved = 0; 961d73f71c7SSrinivas Kandagatla sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0); 962d73f71c7SSrinivas Kandagatla 963d73f71c7SSrinivas Kandagatla return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, 964d73f71c7SSrinivas Kandagatla sc, &args[0]); 965d73f71c7SSrinivas Kandagatla } 966d73f71c7SSrinivas Kandagatla 967f6f9279fSSrinivas Kandagatla static int fastrpc_device_release(struct inode *inode, struct file *file) 968f6f9279fSSrinivas Kandagatla { 969f6f9279fSSrinivas Kandagatla struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data; 970f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = fl->cctx; 971c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx, *n; 972c68cfb71SSrinivas Kandagatla struct fastrpc_map *map, *m; 973f6f9279fSSrinivas Kandagatla 974d73f71c7SSrinivas Kandagatla fastrpc_release_current_dsp_process(fl); 975d73f71c7SSrinivas Kandagatla 976f6f9279fSSrinivas Kandagatla spin_lock(&cctx->lock); 977f6f9279fSSrinivas Kandagatla list_del(&fl->user); 978f6f9279fSSrinivas Kandagatla spin_unlock(&cctx->lock); 979f6f9279fSSrinivas Kandagatla 980c68cfb71SSrinivas Kandagatla if (fl->init_mem) 981c68cfb71SSrinivas Kandagatla fastrpc_buf_free(fl->init_mem); 982c68cfb71SSrinivas Kandagatla 983c68cfb71SSrinivas Kandagatla list_for_each_entry_safe(ctx, n, &fl->pending, node) { 984c68cfb71SSrinivas Kandagatla list_del(&ctx->node); 985c68cfb71SSrinivas Kandagatla fastrpc_context_put(ctx); 986c68cfb71SSrinivas Kandagatla } 987c68cfb71SSrinivas Kandagatla 988c68cfb71SSrinivas Kandagatla list_for_each_entry_safe(map, m, &fl->maps, node) { 989c68cfb71SSrinivas Kandagatla list_del(&map->node); 990c68cfb71SSrinivas Kandagatla fastrpc_map_put(map); 991c68cfb71SSrinivas Kandagatla } 992c68cfb71SSrinivas Kandagatla 993f6f9279fSSrinivas Kandagatla fastrpc_session_free(cctx, fl->sctx); 994f6f9279fSSrinivas Kandagatla 995f6f9279fSSrinivas Kandagatla mutex_destroy(&fl->mutex); 996f6f9279fSSrinivas Kandagatla kfree(fl); 997f6f9279fSSrinivas Kandagatla file->private_data = NULL; 998f6f9279fSSrinivas Kandagatla 999f6f9279fSSrinivas Kandagatla return 0; 1000f6f9279fSSrinivas Kandagatla } 1001f6f9279fSSrinivas Kandagatla 1002f6f9279fSSrinivas Kandagatla static int fastrpc_device_open(struct inode *inode, struct file *filp) 1003f6f9279fSSrinivas Kandagatla { 1004f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = miscdev_to_cctx(filp->private_data); 1005f6f9279fSSrinivas Kandagatla struct fastrpc_user *fl = NULL; 1006f6f9279fSSrinivas Kandagatla 1007f6f9279fSSrinivas Kandagatla fl = kzalloc(sizeof(*fl), GFP_KERNEL); 1008f6f9279fSSrinivas Kandagatla if (!fl) 1009f6f9279fSSrinivas Kandagatla return -ENOMEM; 1010f6f9279fSSrinivas Kandagatla 1011f6f9279fSSrinivas Kandagatla filp->private_data = fl; 1012f6f9279fSSrinivas Kandagatla spin_lock_init(&fl->lock); 1013f6f9279fSSrinivas Kandagatla mutex_init(&fl->mutex); 1014f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&fl->pending); 1015f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&fl->maps); 1016f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&fl->user); 1017f6f9279fSSrinivas Kandagatla fl->tgid = current->tgid; 1018f6f9279fSSrinivas Kandagatla fl->cctx = cctx; 10197c11df42SThierry Escande 10207c11df42SThierry Escande fl->sctx = fastrpc_session_alloc(cctx); 10217c11df42SThierry Escande if (!fl->sctx) { 10227c11df42SThierry Escande dev_err(&cctx->rpdev->dev, "No session available\n"); 10237c11df42SThierry Escande mutex_destroy(&fl->mutex); 10247c11df42SThierry Escande kfree(fl); 10257c11df42SThierry Escande 10267c11df42SThierry Escande return -EBUSY; 10277c11df42SThierry Escande } 10287c11df42SThierry Escande 1029f6f9279fSSrinivas Kandagatla spin_lock(&cctx->lock); 1030f6f9279fSSrinivas Kandagatla list_add_tail(&fl->user, &cctx->users); 1031f6f9279fSSrinivas Kandagatla spin_unlock(&cctx->lock); 1032f6f9279fSSrinivas Kandagatla 1033f6f9279fSSrinivas Kandagatla return 0; 1034f6f9279fSSrinivas Kandagatla } 1035f6f9279fSSrinivas Kandagatla 10366cffd795SSrinivas Kandagatla static int fastrpc_dmabuf_free(struct fastrpc_user *fl, char __user *argp) 10376cffd795SSrinivas Kandagatla { 10386cffd795SSrinivas Kandagatla struct dma_buf *buf; 10396cffd795SSrinivas Kandagatla int info; 10406cffd795SSrinivas Kandagatla 10416cffd795SSrinivas Kandagatla if (copy_from_user(&info, argp, sizeof(info))) 10426cffd795SSrinivas Kandagatla return -EFAULT; 10436cffd795SSrinivas Kandagatla 10446cffd795SSrinivas Kandagatla buf = dma_buf_get(info); 10456cffd795SSrinivas Kandagatla if (IS_ERR_OR_NULL(buf)) 10466cffd795SSrinivas Kandagatla return -EINVAL; 10476cffd795SSrinivas Kandagatla /* 10486cffd795SSrinivas Kandagatla * one for the last get and other for the ALLOC_DMA_BUFF ioctl 10496cffd795SSrinivas Kandagatla */ 10506cffd795SSrinivas Kandagatla dma_buf_put(buf); 10516cffd795SSrinivas Kandagatla dma_buf_put(buf); 10526cffd795SSrinivas Kandagatla 10536cffd795SSrinivas Kandagatla return 0; 10546cffd795SSrinivas Kandagatla } 10556cffd795SSrinivas Kandagatla 10566cffd795SSrinivas Kandagatla static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp) 10576cffd795SSrinivas Kandagatla { 10586cffd795SSrinivas Kandagatla struct fastrpc_alloc_dma_buf bp; 10596cffd795SSrinivas Kandagatla DEFINE_DMA_BUF_EXPORT_INFO(exp_info); 10606cffd795SSrinivas Kandagatla struct fastrpc_buf *buf = NULL; 10616cffd795SSrinivas Kandagatla int err; 10626cffd795SSrinivas Kandagatla 10636cffd795SSrinivas Kandagatla if (copy_from_user(&bp, argp, sizeof(bp))) 10646cffd795SSrinivas Kandagatla return -EFAULT; 10656cffd795SSrinivas Kandagatla 10666cffd795SSrinivas Kandagatla err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf); 10676cffd795SSrinivas Kandagatla if (err) 10686cffd795SSrinivas Kandagatla return err; 10696cffd795SSrinivas Kandagatla exp_info.ops = &fastrpc_dma_buf_ops; 10706cffd795SSrinivas Kandagatla exp_info.size = bp.size; 10716cffd795SSrinivas Kandagatla exp_info.flags = O_RDWR; 10726cffd795SSrinivas Kandagatla exp_info.priv = buf; 10736cffd795SSrinivas Kandagatla buf->dmabuf = dma_buf_export(&exp_info); 10746cffd795SSrinivas Kandagatla if (IS_ERR(buf->dmabuf)) { 10756cffd795SSrinivas Kandagatla err = PTR_ERR(buf->dmabuf); 10766cffd795SSrinivas Kandagatla fastrpc_buf_free(buf); 10776cffd795SSrinivas Kandagatla return err; 10786cffd795SSrinivas Kandagatla } 10796cffd795SSrinivas Kandagatla 10806cffd795SSrinivas Kandagatla bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE); 10816cffd795SSrinivas Kandagatla if (bp.fd < 0) { 10826cffd795SSrinivas Kandagatla dma_buf_put(buf->dmabuf); 10836cffd795SSrinivas Kandagatla return -EINVAL; 10846cffd795SSrinivas Kandagatla } 10856cffd795SSrinivas Kandagatla 10866cffd795SSrinivas Kandagatla if (copy_to_user(argp, &bp, sizeof(bp))) { 10876cffd795SSrinivas Kandagatla dma_buf_put(buf->dmabuf); 10886cffd795SSrinivas Kandagatla return -EFAULT; 10896cffd795SSrinivas Kandagatla } 10906cffd795SSrinivas Kandagatla 10916cffd795SSrinivas Kandagatla get_dma_buf(buf->dmabuf); 10926cffd795SSrinivas Kandagatla 10936cffd795SSrinivas Kandagatla return 0; 10946cffd795SSrinivas Kandagatla } 10956cffd795SSrinivas Kandagatla 1096d73f71c7SSrinivas Kandagatla static int fastrpc_init_attach(struct fastrpc_user *fl) 1097d73f71c7SSrinivas Kandagatla { 1098d73f71c7SSrinivas Kandagatla struct fastrpc_invoke_args args[1]; 1099d73f71c7SSrinivas Kandagatla int tgid = fl->tgid; 1100d73f71c7SSrinivas Kandagatla u32 sc; 1101d73f71c7SSrinivas Kandagatla 1102d73f71c7SSrinivas Kandagatla args[0].ptr = (u64)(uintptr_t) &tgid; 1103d73f71c7SSrinivas Kandagatla args[0].length = sizeof(tgid); 1104d73f71c7SSrinivas Kandagatla args[0].fd = -1; 1105d73f71c7SSrinivas Kandagatla args[0].reserved = 0; 1106d73f71c7SSrinivas Kandagatla sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0); 1107d73f71c7SSrinivas Kandagatla fl->pd = 0; 1108d73f71c7SSrinivas Kandagatla 1109d73f71c7SSrinivas Kandagatla return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, 1110d73f71c7SSrinivas Kandagatla sc, &args[0]); 1111d73f71c7SSrinivas Kandagatla } 1112d73f71c7SSrinivas Kandagatla 1113c68cfb71SSrinivas Kandagatla static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp) 1114c68cfb71SSrinivas Kandagatla { 1115c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_args *args = NULL; 1116c68cfb71SSrinivas Kandagatla struct fastrpc_invoke inv; 1117c68cfb71SSrinivas Kandagatla u32 nscalars; 1118c68cfb71SSrinivas Kandagatla int err; 1119c68cfb71SSrinivas Kandagatla 1120c68cfb71SSrinivas Kandagatla if (copy_from_user(&inv, argp, sizeof(inv))) 1121c68cfb71SSrinivas Kandagatla return -EFAULT; 1122c68cfb71SSrinivas Kandagatla 1123c68cfb71SSrinivas Kandagatla /* nscalars is truncated here to max supported value */ 1124c68cfb71SSrinivas Kandagatla nscalars = REMOTE_SCALARS_LENGTH(inv.sc); 1125c68cfb71SSrinivas Kandagatla if (nscalars) { 1126c68cfb71SSrinivas Kandagatla args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL); 1127c68cfb71SSrinivas Kandagatla if (!args) 1128c68cfb71SSrinivas Kandagatla return -ENOMEM; 1129c68cfb71SSrinivas Kandagatla 1130c68cfb71SSrinivas Kandagatla if (copy_from_user(args, (void __user *)(uintptr_t)inv.args, 1131c68cfb71SSrinivas Kandagatla nscalars * sizeof(*args))) { 1132c68cfb71SSrinivas Kandagatla kfree(args); 1133c68cfb71SSrinivas Kandagatla return -EFAULT; 1134c68cfb71SSrinivas Kandagatla } 1135c68cfb71SSrinivas Kandagatla } 1136c68cfb71SSrinivas Kandagatla 1137c68cfb71SSrinivas Kandagatla err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args); 1138c68cfb71SSrinivas Kandagatla kfree(args); 1139c68cfb71SSrinivas Kandagatla 1140c68cfb71SSrinivas Kandagatla return err; 1141c68cfb71SSrinivas Kandagatla } 1142c68cfb71SSrinivas Kandagatla 1143c68cfb71SSrinivas Kandagatla static long fastrpc_device_ioctl(struct file *file, unsigned int cmd, 1144c68cfb71SSrinivas Kandagatla unsigned long arg) 1145c68cfb71SSrinivas Kandagatla { 1146c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data; 1147c68cfb71SSrinivas Kandagatla char __user *argp = (char __user *)arg; 1148c68cfb71SSrinivas Kandagatla int err; 1149c68cfb71SSrinivas Kandagatla 1150c68cfb71SSrinivas Kandagatla switch (cmd) { 1151c68cfb71SSrinivas Kandagatla case FASTRPC_IOCTL_INVOKE: 1152c68cfb71SSrinivas Kandagatla err = fastrpc_invoke(fl, argp); 1153c68cfb71SSrinivas Kandagatla break; 1154d73f71c7SSrinivas Kandagatla case FASTRPC_IOCTL_INIT_ATTACH: 1155d73f71c7SSrinivas Kandagatla err = fastrpc_init_attach(fl); 1156d73f71c7SSrinivas Kandagatla break; 1157d73f71c7SSrinivas Kandagatla case FASTRPC_IOCTL_INIT_CREATE: 1158d73f71c7SSrinivas Kandagatla err = fastrpc_init_create_process(fl, argp); 1159d73f71c7SSrinivas Kandagatla break; 11606cffd795SSrinivas Kandagatla case FASTRPC_IOCTL_FREE_DMA_BUFF: 11616cffd795SSrinivas Kandagatla err = fastrpc_dmabuf_free(fl, argp); 11626cffd795SSrinivas Kandagatla break; 11636cffd795SSrinivas Kandagatla case FASTRPC_IOCTL_ALLOC_DMA_BUFF: 11646cffd795SSrinivas Kandagatla err = fastrpc_dmabuf_alloc(fl, argp); 11656cffd795SSrinivas Kandagatla break; 1166c68cfb71SSrinivas Kandagatla default: 1167c68cfb71SSrinivas Kandagatla err = -ENOTTY; 1168c68cfb71SSrinivas Kandagatla break; 1169c68cfb71SSrinivas Kandagatla } 1170c68cfb71SSrinivas Kandagatla 1171c68cfb71SSrinivas Kandagatla return err; 1172c68cfb71SSrinivas Kandagatla } 1173c68cfb71SSrinivas Kandagatla 1174f6f9279fSSrinivas Kandagatla static const struct file_operations fastrpc_fops = { 1175f6f9279fSSrinivas Kandagatla .open = fastrpc_device_open, 1176f6f9279fSSrinivas Kandagatla .release = fastrpc_device_release, 1177c68cfb71SSrinivas Kandagatla .unlocked_ioctl = fastrpc_device_ioctl, 1178c68cfb71SSrinivas Kandagatla .compat_ioctl = fastrpc_device_ioctl, 1179f6f9279fSSrinivas Kandagatla }; 1180f6f9279fSSrinivas Kandagatla 1181f6f9279fSSrinivas Kandagatla static int fastrpc_cb_probe(struct platform_device *pdev) 1182f6f9279fSSrinivas Kandagatla { 1183f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 1184f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *sess; 1185f6f9279fSSrinivas Kandagatla struct device *dev = &pdev->dev; 1186f6f9279fSSrinivas Kandagatla int i, sessions = 0; 1187*01b76c32SBo YU int rc; 1188f6f9279fSSrinivas Kandagatla 1189f6f9279fSSrinivas Kandagatla cctx = dev_get_drvdata(dev->parent); 1190f6f9279fSSrinivas Kandagatla if (!cctx) 1191f6f9279fSSrinivas Kandagatla return -EINVAL; 1192f6f9279fSSrinivas Kandagatla 1193f6f9279fSSrinivas Kandagatla of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions); 1194f6f9279fSSrinivas Kandagatla 1195f6f9279fSSrinivas Kandagatla spin_lock(&cctx->lock); 1196f6f9279fSSrinivas Kandagatla sess = &cctx->session[cctx->sesscount]; 1197f6f9279fSSrinivas Kandagatla sess->used = false; 1198f6f9279fSSrinivas Kandagatla sess->valid = true; 1199f6f9279fSSrinivas Kandagatla sess->dev = dev; 1200f6f9279fSSrinivas Kandagatla dev_set_drvdata(dev, sess); 1201f6f9279fSSrinivas Kandagatla 1202f6f9279fSSrinivas Kandagatla if (of_property_read_u32(dev->of_node, "reg", &sess->sid)) 1203f6f9279fSSrinivas Kandagatla dev_info(dev, "FastRPC Session ID not specified in DT\n"); 1204f6f9279fSSrinivas Kandagatla 1205f6f9279fSSrinivas Kandagatla if (sessions > 0) { 1206f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *dup_sess; 1207f6f9279fSSrinivas Kandagatla 1208f6f9279fSSrinivas Kandagatla for (i = 1; i < sessions; i++) { 1209f6f9279fSSrinivas Kandagatla if (cctx->sesscount++ >= FASTRPC_MAX_SESSIONS) 1210f6f9279fSSrinivas Kandagatla break; 1211f6f9279fSSrinivas Kandagatla dup_sess = &cctx->session[cctx->sesscount]; 1212f6f9279fSSrinivas Kandagatla memcpy(dup_sess, sess, sizeof(*dup_sess)); 1213f6f9279fSSrinivas Kandagatla } 1214f6f9279fSSrinivas Kandagatla } 1215f6f9279fSSrinivas Kandagatla cctx->sesscount++; 1216f6f9279fSSrinivas Kandagatla spin_unlock(&cctx->lock); 1217*01b76c32SBo YU rc = dma_set_mask(dev, DMA_BIT_MASK(32)); 1218*01b76c32SBo YU if (rc) { 1219*01b76c32SBo YU dev_err(dev, "32-bit DMA enable failed\n"); 1220*01b76c32SBo YU return rc; 1221*01b76c32SBo YU } 1222f6f9279fSSrinivas Kandagatla 1223f6f9279fSSrinivas Kandagatla return 0; 1224f6f9279fSSrinivas Kandagatla } 1225f6f9279fSSrinivas Kandagatla 1226f6f9279fSSrinivas Kandagatla static int fastrpc_cb_remove(struct platform_device *pdev) 1227f6f9279fSSrinivas Kandagatla { 1228f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent); 1229f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev); 1230f6f9279fSSrinivas Kandagatla int i; 1231f6f9279fSSrinivas Kandagatla 1232f6f9279fSSrinivas Kandagatla spin_lock(&cctx->lock); 1233f6f9279fSSrinivas Kandagatla for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) { 1234f6f9279fSSrinivas Kandagatla if (cctx->session[i].sid == sess->sid) { 1235f6f9279fSSrinivas Kandagatla cctx->session[i].valid = false; 1236f6f9279fSSrinivas Kandagatla cctx->sesscount--; 1237f6f9279fSSrinivas Kandagatla } 1238f6f9279fSSrinivas Kandagatla } 1239f6f9279fSSrinivas Kandagatla spin_unlock(&cctx->lock); 1240f6f9279fSSrinivas Kandagatla 1241f6f9279fSSrinivas Kandagatla return 0; 1242f6f9279fSSrinivas Kandagatla } 1243f6f9279fSSrinivas Kandagatla 1244f6f9279fSSrinivas Kandagatla static const struct of_device_id fastrpc_match_table[] = { 1245f6f9279fSSrinivas Kandagatla { .compatible = "qcom,fastrpc-compute-cb", }, 1246f6f9279fSSrinivas Kandagatla {} 1247f6f9279fSSrinivas Kandagatla }; 1248f6f9279fSSrinivas Kandagatla 1249f6f9279fSSrinivas Kandagatla static struct platform_driver fastrpc_cb_driver = { 1250f6f9279fSSrinivas Kandagatla .probe = fastrpc_cb_probe, 1251f6f9279fSSrinivas Kandagatla .remove = fastrpc_cb_remove, 1252f6f9279fSSrinivas Kandagatla .driver = { 1253f6f9279fSSrinivas Kandagatla .name = "qcom,fastrpc-cb", 1254f6f9279fSSrinivas Kandagatla .of_match_table = fastrpc_match_table, 1255f6f9279fSSrinivas Kandagatla .suppress_bind_attrs = true, 1256f6f9279fSSrinivas Kandagatla }, 1257f6f9279fSSrinivas Kandagatla }; 1258f6f9279fSSrinivas Kandagatla 1259f6f9279fSSrinivas Kandagatla static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) 1260f6f9279fSSrinivas Kandagatla { 1261f6f9279fSSrinivas Kandagatla struct device *rdev = &rpdev->dev; 1262f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *data; 1263f6f9279fSSrinivas Kandagatla int i, err, domain_id = -1; 1264f6f9279fSSrinivas Kandagatla const char *domain; 1265f6f9279fSSrinivas Kandagatla 1266f6f9279fSSrinivas Kandagatla data = devm_kzalloc(rdev, sizeof(*data), GFP_KERNEL); 1267f6f9279fSSrinivas Kandagatla if (!data) 1268f6f9279fSSrinivas Kandagatla return -ENOMEM; 1269f6f9279fSSrinivas Kandagatla 1270f6f9279fSSrinivas Kandagatla err = of_property_read_string(rdev->of_node, "label", &domain); 1271f6f9279fSSrinivas Kandagatla if (err) { 1272f6f9279fSSrinivas Kandagatla dev_info(rdev, "FastRPC Domain not specified in DT\n"); 1273f6f9279fSSrinivas Kandagatla return err; 1274f6f9279fSSrinivas Kandagatla } 1275f6f9279fSSrinivas Kandagatla 1276f6f9279fSSrinivas Kandagatla for (i = 0; i <= CDSP_DOMAIN_ID; i++) { 1277f6f9279fSSrinivas Kandagatla if (!strcmp(domains[i], domain)) { 1278f6f9279fSSrinivas Kandagatla domain_id = i; 1279f6f9279fSSrinivas Kandagatla break; 1280f6f9279fSSrinivas Kandagatla } 1281f6f9279fSSrinivas Kandagatla } 1282f6f9279fSSrinivas Kandagatla 1283f6f9279fSSrinivas Kandagatla if (domain_id < 0) { 1284f6f9279fSSrinivas Kandagatla dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id); 1285f6f9279fSSrinivas Kandagatla return -EINVAL; 1286f6f9279fSSrinivas Kandagatla } 1287f6f9279fSSrinivas Kandagatla 1288f6f9279fSSrinivas Kandagatla data->miscdev.minor = MISC_DYNAMIC_MINOR; 1289f6f9279fSSrinivas Kandagatla data->miscdev.name = kasprintf(GFP_KERNEL, "fastrpc-%s", 1290f6f9279fSSrinivas Kandagatla domains[domain_id]); 1291f6f9279fSSrinivas Kandagatla data->miscdev.fops = &fastrpc_fops; 1292f6f9279fSSrinivas Kandagatla err = misc_register(&data->miscdev); 1293f6f9279fSSrinivas Kandagatla if (err) 1294f6f9279fSSrinivas Kandagatla return err; 1295f6f9279fSSrinivas Kandagatla 1296f6f9279fSSrinivas Kandagatla dev_set_drvdata(&rpdev->dev, data); 1297f6f9279fSSrinivas Kandagatla dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32)); 1298f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&data->users); 1299f6f9279fSSrinivas Kandagatla spin_lock_init(&data->lock); 1300f6f9279fSSrinivas Kandagatla idr_init(&data->ctx_idr); 1301f6f9279fSSrinivas Kandagatla data->domain_id = domain_id; 1302f6f9279fSSrinivas Kandagatla data->rpdev = rpdev; 1303f6f9279fSSrinivas Kandagatla 1304f6f9279fSSrinivas Kandagatla return of_platform_populate(rdev->of_node, NULL, NULL, rdev); 1305f6f9279fSSrinivas Kandagatla } 1306f6f9279fSSrinivas Kandagatla 1307c68cfb71SSrinivas Kandagatla static void fastrpc_notify_users(struct fastrpc_user *user) 1308c68cfb71SSrinivas Kandagatla { 1309c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx; 1310c68cfb71SSrinivas Kandagatla 1311c68cfb71SSrinivas Kandagatla spin_lock(&user->lock); 1312c68cfb71SSrinivas Kandagatla list_for_each_entry(ctx, &user->pending, node) 1313c68cfb71SSrinivas Kandagatla complete(&ctx->work); 1314c68cfb71SSrinivas Kandagatla spin_unlock(&user->lock); 1315c68cfb71SSrinivas Kandagatla } 1316c68cfb71SSrinivas Kandagatla 1317f6f9279fSSrinivas Kandagatla static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev) 1318f6f9279fSSrinivas Kandagatla { 1319f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev); 1320c68cfb71SSrinivas Kandagatla struct fastrpc_user *user; 1321c68cfb71SSrinivas Kandagatla 1322c68cfb71SSrinivas Kandagatla spin_lock(&cctx->lock); 1323c68cfb71SSrinivas Kandagatla list_for_each_entry(user, &cctx->users, user) 1324c68cfb71SSrinivas Kandagatla fastrpc_notify_users(user); 1325c68cfb71SSrinivas Kandagatla spin_unlock(&cctx->lock); 1326f6f9279fSSrinivas Kandagatla 1327f6f9279fSSrinivas Kandagatla misc_deregister(&cctx->miscdev); 1328f6f9279fSSrinivas Kandagatla of_platform_depopulate(&rpdev->dev); 1329f6f9279fSSrinivas Kandagatla kfree(cctx); 1330f6f9279fSSrinivas Kandagatla } 1331f6f9279fSSrinivas Kandagatla 1332f6f9279fSSrinivas Kandagatla static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data, 1333f6f9279fSSrinivas Kandagatla int len, void *priv, u32 addr) 1334f6f9279fSSrinivas Kandagatla { 1335c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev); 1336c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_rsp *rsp = data; 1337c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx; 1338c68cfb71SSrinivas Kandagatla unsigned long flags; 1339c68cfb71SSrinivas Kandagatla unsigned long ctxid; 1340c68cfb71SSrinivas Kandagatla 1341c68cfb71SSrinivas Kandagatla if (len < sizeof(*rsp)) 1342c68cfb71SSrinivas Kandagatla return -EINVAL; 1343c68cfb71SSrinivas Kandagatla 1344c68cfb71SSrinivas Kandagatla ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4); 1345c68cfb71SSrinivas Kandagatla 1346c68cfb71SSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1347c68cfb71SSrinivas Kandagatla ctx = idr_find(&cctx->ctx_idr, ctxid); 1348c68cfb71SSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1349c68cfb71SSrinivas Kandagatla 1350c68cfb71SSrinivas Kandagatla if (!ctx) { 1351c68cfb71SSrinivas Kandagatla dev_err(&rpdev->dev, "No context ID matches response\n"); 1352c68cfb71SSrinivas Kandagatla return -ENOENT; 1353c68cfb71SSrinivas Kandagatla } 1354c68cfb71SSrinivas Kandagatla 1355c68cfb71SSrinivas Kandagatla ctx->retval = rsp->retval; 1356c68cfb71SSrinivas Kandagatla complete(&ctx->work); 1357c68cfb71SSrinivas Kandagatla fastrpc_context_put(ctx); 1358c68cfb71SSrinivas Kandagatla 1359f6f9279fSSrinivas Kandagatla return 0; 1360f6f9279fSSrinivas Kandagatla } 1361f6f9279fSSrinivas Kandagatla 1362f6f9279fSSrinivas Kandagatla static const struct of_device_id fastrpc_rpmsg_of_match[] = { 1363f6f9279fSSrinivas Kandagatla { .compatible = "qcom,fastrpc" }, 1364f6f9279fSSrinivas Kandagatla { }, 1365f6f9279fSSrinivas Kandagatla }; 1366f6f9279fSSrinivas Kandagatla MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match); 1367f6f9279fSSrinivas Kandagatla 1368f6f9279fSSrinivas Kandagatla static struct rpmsg_driver fastrpc_driver = { 1369f6f9279fSSrinivas Kandagatla .probe = fastrpc_rpmsg_probe, 1370f6f9279fSSrinivas Kandagatla .remove = fastrpc_rpmsg_remove, 1371f6f9279fSSrinivas Kandagatla .callback = fastrpc_rpmsg_callback, 1372f6f9279fSSrinivas Kandagatla .drv = { 1373f6f9279fSSrinivas Kandagatla .name = "qcom,fastrpc", 1374f6f9279fSSrinivas Kandagatla .of_match_table = fastrpc_rpmsg_of_match, 1375f6f9279fSSrinivas Kandagatla }, 1376f6f9279fSSrinivas Kandagatla }; 1377f6f9279fSSrinivas Kandagatla 1378f6f9279fSSrinivas Kandagatla static int fastrpc_init(void) 1379f6f9279fSSrinivas Kandagatla { 1380f6f9279fSSrinivas Kandagatla int ret; 1381f6f9279fSSrinivas Kandagatla 1382f6f9279fSSrinivas Kandagatla ret = platform_driver_register(&fastrpc_cb_driver); 1383f6f9279fSSrinivas Kandagatla if (ret < 0) { 1384f6f9279fSSrinivas Kandagatla pr_err("fastrpc: failed to register cb driver\n"); 1385f6f9279fSSrinivas Kandagatla return ret; 1386f6f9279fSSrinivas Kandagatla } 1387f6f9279fSSrinivas Kandagatla 1388f6f9279fSSrinivas Kandagatla ret = register_rpmsg_driver(&fastrpc_driver); 1389f6f9279fSSrinivas Kandagatla if (ret < 0) { 1390f6f9279fSSrinivas Kandagatla pr_err("fastrpc: failed to register rpmsg driver\n"); 1391f6f9279fSSrinivas Kandagatla platform_driver_unregister(&fastrpc_cb_driver); 1392f6f9279fSSrinivas Kandagatla return ret; 1393f6f9279fSSrinivas Kandagatla } 1394f6f9279fSSrinivas Kandagatla 1395f6f9279fSSrinivas Kandagatla return 0; 1396f6f9279fSSrinivas Kandagatla } 1397f6f9279fSSrinivas Kandagatla module_init(fastrpc_init); 1398f6f9279fSSrinivas Kandagatla 1399f6f9279fSSrinivas Kandagatla static void fastrpc_exit(void) 1400f6f9279fSSrinivas Kandagatla { 1401f6f9279fSSrinivas Kandagatla platform_driver_unregister(&fastrpc_cb_driver); 1402f6f9279fSSrinivas Kandagatla unregister_rpmsg_driver(&fastrpc_driver); 1403f6f9279fSSrinivas Kandagatla } 1404f6f9279fSSrinivas Kandagatla module_exit(fastrpc_exit); 1405f6f9279fSSrinivas Kandagatla 1406f6f9279fSSrinivas Kandagatla MODULE_LICENSE("GPL v2"); 1407