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> 15*25e8dfb8SSrinivas 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) 35d73f71c7SSrinivas Kandagatla #define INIT_FILELEN_MAX (2 * 1024 * 1024) 36d73f71c7SSrinivas Kandagatla #define INIT_MEMLEN_MAX (8 * 1024 * 1024) 37f6f9279fSSrinivas Kandagatla #define FASTRPC_DEVICE_NAME "fastrpc" 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 70d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE 6 71d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE_ATTR 7 72d73f71c7SSrinivas Kandagatla #define FASTRPC_RMID_INIT_CREATE_STATIC 8 73d73f71c7SSrinivas Kandagatla 74f6f9279fSSrinivas Kandagatla #define miscdev_to_cctx(d) container_of(d, struct fastrpc_channel_ctx, miscdev) 75f6f9279fSSrinivas Kandagatla 76f6f9279fSSrinivas Kandagatla static const char *domains[FASTRPC_DEV_MAX] = { "adsp", "mdsp", 77f6f9279fSSrinivas Kandagatla "sdsp", "cdsp"}; 78c68cfb71SSrinivas Kandagatla struct fastrpc_phy_page { 79c68cfb71SSrinivas Kandagatla u64 addr; /* physical address */ 80c68cfb71SSrinivas Kandagatla u64 size; /* size of contiguous region */ 81c68cfb71SSrinivas Kandagatla }; 82c68cfb71SSrinivas Kandagatla 83c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_buf { 84c68cfb71SSrinivas Kandagatla u32 num; /* number of contiguous regions */ 85c68cfb71SSrinivas Kandagatla u32 pgidx; /* index to start of contiguous region */ 86c68cfb71SSrinivas Kandagatla }; 87c68cfb71SSrinivas Kandagatla 88c68cfb71SSrinivas Kandagatla struct fastrpc_remote_arg { 89c68cfb71SSrinivas Kandagatla u64 pv; 90c68cfb71SSrinivas Kandagatla u64 len; 91c68cfb71SSrinivas Kandagatla }; 92c68cfb71SSrinivas Kandagatla 93c68cfb71SSrinivas Kandagatla struct fastrpc_msg { 94c68cfb71SSrinivas Kandagatla int pid; /* process group id */ 95c68cfb71SSrinivas Kandagatla int tid; /* thread id */ 96c68cfb71SSrinivas Kandagatla u64 ctx; /* invoke caller context */ 97c68cfb71SSrinivas Kandagatla u32 handle; /* handle to invoke */ 98c68cfb71SSrinivas Kandagatla u32 sc; /* scalars structure describing the data */ 99c68cfb71SSrinivas Kandagatla u64 addr; /* physical address */ 100c68cfb71SSrinivas Kandagatla u64 size; /* size of contiguous region */ 101c68cfb71SSrinivas Kandagatla }; 102c68cfb71SSrinivas Kandagatla 103c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_rsp { 104c68cfb71SSrinivas Kandagatla u64 ctx; /* invoke caller context */ 105c68cfb71SSrinivas Kandagatla int retval; /* invoke return value */ 106c68cfb71SSrinivas Kandagatla }; 107c68cfb71SSrinivas Kandagatla 108*25e8dfb8SSrinivas Kandagatla struct fastrpc_buf_overlap { 109*25e8dfb8SSrinivas Kandagatla u64 start; 110*25e8dfb8SSrinivas Kandagatla u64 end; 111*25e8dfb8SSrinivas Kandagatla int raix; 112*25e8dfb8SSrinivas Kandagatla u64 mstart; 113*25e8dfb8SSrinivas Kandagatla u64 mend; 114*25e8dfb8SSrinivas Kandagatla u64 offset; 115*25e8dfb8SSrinivas Kandagatla }; 116*25e8dfb8SSrinivas Kandagatla 117c68cfb71SSrinivas Kandagatla struct fastrpc_buf { 118c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl; 1196cffd795SSrinivas Kandagatla struct dma_buf *dmabuf; 120c68cfb71SSrinivas Kandagatla struct device *dev; 121c68cfb71SSrinivas Kandagatla void *virt; 122c68cfb71SSrinivas Kandagatla u64 phys; 123c68cfb71SSrinivas Kandagatla u64 size; 1246cffd795SSrinivas Kandagatla /* Lock for dma buf attachments */ 1256cffd795SSrinivas Kandagatla struct mutex lock; 1266cffd795SSrinivas Kandagatla struct list_head attachments; 1276cffd795SSrinivas Kandagatla }; 1286cffd795SSrinivas Kandagatla 1296cffd795SSrinivas Kandagatla struct fastrpc_dma_buf_attachment { 1306cffd795SSrinivas Kandagatla struct device *dev; 1316cffd795SSrinivas Kandagatla struct sg_table sgt; 1326cffd795SSrinivas Kandagatla struct list_head node; 133c68cfb71SSrinivas Kandagatla }; 134c68cfb71SSrinivas Kandagatla 135c68cfb71SSrinivas Kandagatla struct fastrpc_map { 136c68cfb71SSrinivas Kandagatla struct list_head node; 137c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl; 138c68cfb71SSrinivas Kandagatla int fd; 139c68cfb71SSrinivas Kandagatla struct dma_buf *buf; 140c68cfb71SSrinivas Kandagatla struct sg_table *table; 141c68cfb71SSrinivas Kandagatla struct dma_buf_attachment *attach; 142c68cfb71SSrinivas Kandagatla u64 phys; 143c68cfb71SSrinivas Kandagatla u64 size; 144c68cfb71SSrinivas Kandagatla void *va; 145c68cfb71SSrinivas Kandagatla u64 len; 146c68cfb71SSrinivas Kandagatla struct kref refcount; 147c68cfb71SSrinivas Kandagatla }; 148c68cfb71SSrinivas Kandagatla 149c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx { 150c68cfb71SSrinivas Kandagatla int nscalars; 151c68cfb71SSrinivas Kandagatla int nbufs; 152c68cfb71SSrinivas Kandagatla int retval; 153c68cfb71SSrinivas Kandagatla int pid; 154c68cfb71SSrinivas Kandagatla int tgid; 155c68cfb71SSrinivas Kandagatla u32 sc; 156c68cfb71SSrinivas Kandagatla u32 *crc; 157c68cfb71SSrinivas Kandagatla u64 ctxid; 158c68cfb71SSrinivas Kandagatla u64 msg_sz; 159c68cfb71SSrinivas Kandagatla struct kref refcount; 160c68cfb71SSrinivas Kandagatla struct list_head node; /* list of ctxs */ 161c68cfb71SSrinivas Kandagatla struct completion work; 1628e7389c7SThierry Escande struct work_struct put_work; 163c68cfb71SSrinivas Kandagatla struct fastrpc_msg msg; 164c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl; 165c68cfb71SSrinivas Kandagatla struct fastrpc_remote_arg *rpra; 166c68cfb71SSrinivas Kandagatla struct fastrpc_map **maps; 167c68cfb71SSrinivas Kandagatla struct fastrpc_buf *buf; 168c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_args *args; 169*25e8dfb8SSrinivas Kandagatla struct fastrpc_buf_overlap *olaps; 170c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 171c68cfb71SSrinivas Kandagatla }; 172f6f9279fSSrinivas Kandagatla 173f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx { 174f6f9279fSSrinivas Kandagatla struct device *dev; 175f6f9279fSSrinivas Kandagatla int sid; 176f6f9279fSSrinivas Kandagatla bool used; 177f6f9279fSSrinivas Kandagatla bool valid; 178f6f9279fSSrinivas Kandagatla }; 179f6f9279fSSrinivas Kandagatla 180f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx { 181f6f9279fSSrinivas Kandagatla int domain_id; 182f6f9279fSSrinivas Kandagatla int sesscount; 183f6f9279fSSrinivas Kandagatla struct rpmsg_device *rpdev; 184f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS]; 185f6f9279fSSrinivas Kandagatla spinlock_t lock; 186f6f9279fSSrinivas Kandagatla struct idr ctx_idr; 187f6f9279fSSrinivas Kandagatla struct list_head users; 188f6f9279fSSrinivas Kandagatla struct miscdevice miscdev; 189f6f9279fSSrinivas Kandagatla }; 190f6f9279fSSrinivas Kandagatla 191f6f9279fSSrinivas Kandagatla struct fastrpc_user { 192f6f9279fSSrinivas Kandagatla struct list_head user; 193f6f9279fSSrinivas Kandagatla struct list_head maps; 194f6f9279fSSrinivas Kandagatla struct list_head pending; 195f6f9279fSSrinivas Kandagatla 196f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 197f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *sctx; 198c68cfb71SSrinivas Kandagatla struct fastrpc_buf *init_mem; 199f6f9279fSSrinivas Kandagatla 200f6f9279fSSrinivas Kandagatla int tgid; 201f6f9279fSSrinivas Kandagatla int pd; 202f6f9279fSSrinivas Kandagatla /* Lock for lists */ 203f6f9279fSSrinivas Kandagatla spinlock_t lock; 204f6f9279fSSrinivas Kandagatla /* lock for allocations */ 205f6f9279fSSrinivas Kandagatla struct mutex mutex; 206f6f9279fSSrinivas Kandagatla }; 207f6f9279fSSrinivas Kandagatla 208c68cfb71SSrinivas Kandagatla static void fastrpc_free_map(struct kref *ref) 209c68cfb71SSrinivas Kandagatla { 210c68cfb71SSrinivas Kandagatla struct fastrpc_map *map; 211c68cfb71SSrinivas Kandagatla 212c68cfb71SSrinivas Kandagatla map = container_of(ref, struct fastrpc_map, refcount); 213c68cfb71SSrinivas Kandagatla 214c68cfb71SSrinivas Kandagatla if (map->table) { 215c68cfb71SSrinivas Kandagatla dma_buf_unmap_attachment(map->attach, map->table, 216c68cfb71SSrinivas Kandagatla DMA_BIDIRECTIONAL); 217c68cfb71SSrinivas Kandagatla dma_buf_detach(map->buf, map->attach); 218c68cfb71SSrinivas Kandagatla dma_buf_put(map->buf); 219c68cfb71SSrinivas Kandagatla } 220c68cfb71SSrinivas Kandagatla 221c68cfb71SSrinivas Kandagatla kfree(map); 222c68cfb71SSrinivas Kandagatla } 223c68cfb71SSrinivas Kandagatla 224c68cfb71SSrinivas Kandagatla static void fastrpc_map_put(struct fastrpc_map *map) 225c68cfb71SSrinivas Kandagatla { 226c68cfb71SSrinivas Kandagatla if (map) 227c68cfb71SSrinivas Kandagatla kref_put(&map->refcount, fastrpc_free_map); 228c68cfb71SSrinivas Kandagatla } 229c68cfb71SSrinivas Kandagatla 230c68cfb71SSrinivas Kandagatla static void fastrpc_map_get(struct fastrpc_map *map) 231c68cfb71SSrinivas Kandagatla { 232c68cfb71SSrinivas Kandagatla if (map) 233c68cfb71SSrinivas Kandagatla kref_get(&map->refcount); 234c68cfb71SSrinivas Kandagatla } 235c68cfb71SSrinivas Kandagatla 236c68cfb71SSrinivas Kandagatla static int fastrpc_map_find(struct fastrpc_user *fl, int fd, 237c68cfb71SSrinivas Kandagatla struct fastrpc_map **ppmap) 238c68cfb71SSrinivas Kandagatla { 239c68cfb71SSrinivas Kandagatla struct fastrpc_map *map = NULL; 240c68cfb71SSrinivas Kandagatla 241c68cfb71SSrinivas Kandagatla mutex_lock(&fl->mutex); 242c68cfb71SSrinivas Kandagatla list_for_each_entry(map, &fl->maps, node) { 243c68cfb71SSrinivas Kandagatla if (map->fd == fd) { 244c68cfb71SSrinivas Kandagatla fastrpc_map_get(map); 245c68cfb71SSrinivas Kandagatla *ppmap = map; 246c68cfb71SSrinivas Kandagatla mutex_unlock(&fl->mutex); 247c68cfb71SSrinivas Kandagatla return 0; 248c68cfb71SSrinivas Kandagatla } 249c68cfb71SSrinivas Kandagatla } 250c68cfb71SSrinivas Kandagatla mutex_unlock(&fl->mutex); 251c68cfb71SSrinivas Kandagatla 252c68cfb71SSrinivas Kandagatla return -ENOENT; 253c68cfb71SSrinivas Kandagatla } 254c68cfb71SSrinivas Kandagatla 255c68cfb71SSrinivas Kandagatla static void fastrpc_buf_free(struct fastrpc_buf *buf) 256c68cfb71SSrinivas Kandagatla { 257c68cfb71SSrinivas Kandagatla dma_free_coherent(buf->dev, buf->size, buf->virt, 258c68cfb71SSrinivas Kandagatla FASTRPC_PHYS(buf->phys)); 259c68cfb71SSrinivas Kandagatla kfree(buf); 260c68cfb71SSrinivas Kandagatla } 261c68cfb71SSrinivas Kandagatla 262c68cfb71SSrinivas Kandagatla static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev, 263c68cfb71SSrinivas Kandagatla u64 size, struct fastrpc_buf **obuf) 264c68cfb71SSrinivas Kandagatla { 265c68cfb71SSrinivas Kandagatla struct fastrpc_buf *buf; 266c68cfb71SSrinivas Kandagatla 267c68cfb71SSrinivas Kandagatla buf = kzalloc(sizeof(*buf), GFP_KERNEL); 268c68cfb71SSrinivas Kandagatla if (!buf) 269c68cfb71SSrinivas Kandagatla return -ENOMEM; 270c68cfb71SSrinivas Kandagatla 2716cffd795SSrinivas Kandagatla INIT_LIST_HEAD(&buf->attachments); 2726cffd795SSrinivas Kandagatla mutex_init(&buf->lock); 2736cffd795SSrinivas Kandagatla 274c68cfb71SSrinivas Kandagatla buf->fl = fl; 275c68cfb71SSrinivas Kandagatla buf->virt = NULL; 276c68cfb71SSrinivas Kandagatla buf->phys = 0; 277c68cfb71SSrinivas Kandagatla buf->size = size; 278c68cfb71SSrinivas Kandagatla buf->dev = dev; 279c68cfb71SSrinivas Kandagatla 280c68cfb71SSrinivas Kandagatla buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys, 281c68cfb71SSrinivas Kandagatla GFP_KERNEL); 282c68cfb71SSrinivas Kandagatla if (!buf->virt) 283c68cfb71SSrinivas Kandagatla return -ENOMEM; 284c68cfb71SSrinivas Kandagatla 285c68cfb71SSrinivas Kandagatla if (fl->sctx && fl->sctx->sid) 286c68cfb71SSrinivas Kandagatla buf->phys += ((u64)fl->sctx->sid << 32); 287c68cfb71SSrinivas Kandagatla 288c68cfb71SSrinivas Kandagatla *obuf = buf; 289c68cfb71SSrinivas Kandagatla 290c68cfb71SSrinivas Kandagatla return 0; 291c68cfb71SSrinivas Kandagatla } 292c68cfb71SSrinivas Kandagatla 293c68cfb71SSrinivas Kandagatla static void fastrpc_context_free(struct kref *ref) 294c68cfb71SSrinivas Kandagatla { 295c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx; 296c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 297977e6c8dSSrinivas Kandagatla unsigned long flags; 298c68cfb71SSrinivas Kandagatla int i; 299c68cfb71SSrinivas Kandagatla 300c68cfb71SSrinivas Kandagatla ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount); 301c68cfb71SSrinivas Kandagatla cctx = ctx->cctx; 302c68cfb71SSrinivas Kandagatla 303c68cfb71SSrinivas Kandagatla for (i = 0; i < ctx->nscalars; i++) 304c68cfb71SSrinivas Kandagatla fastrpc_map_put(ctx->maps[i]); 305c68cfb71SSrinivas Kandagatla 306c68cfb71SSrinivas Kandagatla if (ctx->buf) 307c68cfb71SSrinivas Kandagatla fastrpc_buf_free(ctx->buf); 308c68cfb71SSrinivas Kandagatla 309977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 310c68cfb71SSrinivas Kandagatla idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4); 311977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 312c68cfb71SSrinivas Kandagatla 313c68cfb71SSrinivas Kandagatla kfree(ctx->maps); 314*25e8dfb8SSrinivas Kandagatla kfree(ctx->olaps); 315c68cfb71SSrinivas Kandagatla kfree(ctx); 316c68cfb71SSrinivas Kandagatla } 317c68cfb71SSrinivas Kandagatla 318c68cfb71SSrinivas Kandagatla static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx) 319c68cfb71SSrinivas Kandagatla { 320c68cfb71SSrinivas Kandagatla kref_get(&ctx->refcount); 321c68cfb71SSrinivas Kandagatla } 322c68cfb71SSrinivas Kandagatla 323c68cfb71SSrinivas Kandagatla static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx) 324c68cfb71SSrinivas Kandagatla { 325c68cfb71SSrinivas Kandagatla kref_put(&ctx->refcount, fastrpc_context_free); 326c68cfb71SSrinivas Kandagatla } 327c68cfb71SSrinivas Kandagatla 3288e7389c7SThierry Escande static void fastrpc_context_put_wq(struct work_struct *work) 3298e7389c7SThierry Escande { 3308e7389c7SThierry Escande struct fastrpc_invoke_ctx *ctx = 3318e7389c7SThierry Escande container_of(work, struct fastrpc_invoke_ctx, put_work); 3328e7389c7SThierry Escande 3338e7389c7SThierry Escande fastrpc_context_put(ctx); 3348e7389c7SThierry Escande } 3358e7389c7SThierry Escande 336*25e8dfb8SSrinivas Kandagatla #define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1) 337*25e8dfb8SSrinivas Kandagatla static int olaps_cmp(const void *a, const void *b) 338*25e8dfb8SSrinivas Kandagatla { 339*25e8dfb8SSrinivas Kandagatla struct fastrpc_buf_overlap *pa = (struct fastrpc_buf_overlap *)a; 340*25e8dfb8SSrinivas Kandagatla struct fastrpc_buf_overlap *pb = (struct fastrpc_buf_overlap *)b; 341*25e8dfb8SSrinivas Kandagatla /* sort with lowest starting buffer first */ 342*25e8dfb8SSrinivas Kandagatla int st = CMP(pa->start, pb->start); 343*25e8dfb8SSrinivas Kandagatla /* sort with highest ending buffer first */ 344*25e8dfb8SSrinivas Kandagatla int ed = CMP(pb->end, pa->end); 345*25e8dfb8SSrinivas Kandagatla 346*25e8dfb8SSrinivas Kandagatla return st == 0 ? ed : st; 347*25e8dfb8SSrinivas Kandagatla } 348*25e8dfb8SSrinivas Kandagatla 349*25e8dfb8SSrinivas Kandagatla static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx) 350*25e8dfb8SSrinivas Kandagatla { 351*25e8dfb8SSrinivas Kandagatla u64 max_end = 0; 352*25e8dfb8SSrinivas Kandagatla int i; 353*25e8dfb8SSrinivas Kandagatla 354*25e8dfb8SSrinivas Kandagatla for (i = 0; i < ctx->nbufs; ++i) { 355*25e8dfb8SSrinivas Kandagatla ctx->olaps[i].start = ctx->args[i].ptr; 356*25e8dfb8SSrinivas Kandagatla ctx->olaps[i].end = ctx->olaps[i].start + ctx->args[i].length; 357*25e8dfb8SSrinivas Kandagatla ctx->olaps[i].raix = i; 358*25e8dfb8SSrinivas Kandagatla } 359*25e8dfb8SSrinivas Kandagatla 360*25e8dfb8SSrinivas Kandagatla sort(ctx->olaps, ctx->nbufs, sizeof(*ctx->olaps), olaps_cmp, NULL); 361*25e8dfb8SSrinivas Kandagatla 362*25e8dfb8SSrinivas Kandagatla for (i = 0; i < ctx->nbufs; ++i) { 363*25e8dfb8SSrinivas Kandagatla /* Falling inside previous range */ 364*25e8dfb8SSrinivas Kandagatla if (ctx->olaps[i].start < max_end) { 365*25e8dfb8SSrinivas Kandagatla ctx->olaps[i].mstart = max_end; 366*25e8dfb8SSrinivas Kandagatla ctx->olaps[i].mend = ctx->olaps[i].end; 367*25e8dfb8SSrinivas Kandagatla ctx->olaps[i].offset = max_end - ctx->olaps[i].start; 368*25e8dfb8SSrinivas Kandagatla 369*25e8dfb8SSrinivas Kandagatla if (ctx->olaps[i].end > max_end) { 370*25e8dfb8SSrinivas Kandagatla max_end = ctx->olaps[i].end; 371*25e8dfb8SSrinivas Kandagatla } else { 372*25e8dfb8SSrinivas Kandagatla ctx->olaps[i].mend = 0; 373*25e8dfb8SSrinivas Kandagatla ctx->olaps[i].mstart = 0; 374*25e8dfb8SSrinivas Kandagatla } 375*25e8dfb8SSrinivas Kandagatla 376*25e8dfb8SSrinivas Kandagatla } else { 377*25e8dfb8SSrinivas Kandagatla ctx->olaps[i].mend = ctx->olaps[i].end; 378*25e8dfb8SSrinivas Kandagatla ctx->olaps[i].mstart = ctx->olaps[i].start; 379*25e8dfb8SSrinivas Kandagatla ctx->olaps[i].offset = 0; 380*25e8dfb8SSrinivas Kandagatla max_end = ctx->olaps[i].end; 381*25e8dfb8SSrinivas Kandagatla } 382*25e8dfb8SSrinivas Kandagatla } 383*25e8dfb8SSrinivas Kandagatla } 384*25e8dfb8SSrinivas Kandagatla 385c68cfb71SSrinivas Kandagatla static struct fastrpc_invoke_ctx *fastrpc_context_alloc( 386c68cfb71SSrinivas Kandagatla struct fastrpc_user *user, u32 kernel, u32 sc, 387c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_args *args) 388c68cfb71SSrinivas Kandagatla { 389c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = user->cctx; 390c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx = NULL; 391977e6c8dSSrinivas Kandagatla unsigned long flags; 392c68cfb71SSrinivas Kandagatla int ret; 393c68cfb71SSrinivas Kandagatla 394c68cfb71SSrinivas Kandagatla ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 395c68cfb71SSrinivas Kandagatla if (!ctx) 396c68cfb71SSrinivas Kandagatla return ERR_PTR(-ENOMEM); 397c68cfb71SSrinivas Kandagatla 398c68cfb71SSrinivas Kandagatla INIT_LIST_HEAD(&ctx->node); 399c68cfb71SSrinivas Kandagatla ctx->fl = user; 400c68cfb71SSrinivas Kandagatla ctx->nscalars = REMOTE_SCALARS_LENGTH(sc); 401c68cfb71SSrinivas Kandagatla ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) + 402c68cfb71SSrinivas Kandagatla REMOTE_SCALARS_OUTBUFS(sc); 403c68cfb71SSrinivas Kandagatla 404c68cfb71SSrinivas Kandagatla if (ctx->nscalars) { 405c68cfb71SSrinivas Kandagatla ctx->maps = kcalloc(ctx->nscalars, 406c68cfb71SSrinivas Kandagatla sizeof(*ctx->maps), GFP_KERNEL); 407c68cfb71SSrinivas Kandagatla if (!ctx->maps) { 408c68cfb71SSrinivas Kandagatla kfree(ctx); 409c68cfb71SSrinivas Kandagatla return ERR_PTR(-ENOMEM); 410c68cfb71SSrinivas Kandagatla } 411*25e8dfb8SSrinivas Kandagatla ctx->olaps = kcalloc(ctx->nscalars, 412*25e8dfb8SSrinivas Kandagatla sizeof(*ctx->olaps), GFP_KERNEL); 413*25e8dfb8SSrinivas Kandagatla if (!ctx->olaps) { 414*25e8dfb8SSrinivas Kandagatla kfree(ctx->maps); 415*25e8dfb8SSrinivas Kandagatla kfree(ctx); 416*25e8dfb8SSrinivas Kandagatla return ERR_PTR(-ENOMEM); 417*25e8dfb8SSrinivas Kandagatla } 418c68cfb71SSrinivas Kandagatla ctx->args = args; 419*25e8dfb8SSrinivas Kandagatla fastrpc_get_buff_overlaps(ctx); 420c68cfb71SSrinivas Kandagatla } 421c68cfb71SSrinivas Kandagatla 422c68cfb71SSrinivas Kandagatla ctx->sc = sc; 423c68cfb71SSrinivas Kandagatla ctx->retval = -1; 424c68cfb71SSrinivas Kandagatla ctx->pid = current->pid; 425c68cfb71SSrinivas Kandagatla ctx->tgid = user->tgid; 426c68cfb71SSrinivas Kandagatla ctx->cctx = cctx; 427c68cfb71SSrinivas Kandagatla init_completion(&ctx->work); 4288e7389c7SThierry Escande INIT_WORK(&ctx->put_work, fastrpc_context_put_wq); 429c68cfb71SSrinivas Kandagatla 430c68cfb71SSrinivas Kandagatla spin_lock(&user->lock); 431c68cfb71SSrinivas Kandagatla list_add_tail(&ctx->node, &user->pending); 432c68cfb71SSrinivas Kandagatla spin_unlock(&user->lock); 433c68cfb71SSrinivas Kandagatla 434977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 435c68cfb71SSrinivas Kandagatla ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1, 436c68cfb71SSrinivas Kandagatla FASTRPC_CTX_MAX, GFP_ATOMIC); 437c68cfb71SSrinivas Kandagatla if (ret < 0) { 438977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 439c68cfb71SSrinivas Kandagatla goto err_idr; 440c68cfb71SSrinivas Kandagatla } 441c68cfb71SSrinivas Kandagatla ctx->ctxid = ret << 4; 442977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 443c68cfb71SSrinivas Kandagatla 444c68cfb71SSrinivas Kandagatla kref_init(&ctx->refcount); 445c68cfb71SSrinivas Kandagatla 446c68cfb71SSrinivas Kandagatla return ctx; 447c68cfb71SSrinivas Kandagatla err_idr: 448c68cfb71SSrinivas Kandagatla spin_lock(&user->lock); 449c68cfb71SSrinivas Kandagatla list_del(&ctx->node); 450c68cfb71SSrinivas Kandagatla spin_unlock(&user->lock); 451c68cfb71SSrinivas Kandagatla kfree(ctx->maps); 452*25e8dfb8SSrinivas Kandagatla kfree(ctx->olaps); 453c68cfb71SSrinivas Kandagatla kfree(ctx); 454c68cfb71SSrinivas Kandagatla 455c68cfb71SSrinivas Kandagatla return ERR_PTR(ret); 456c68cfb71SSrinivas Kandagatla } 457c68cfb71SSrinivas Kandagatla 4586cffd795SSrinivas Kandagatla static struct sg_table * 4596cffd795SSrinivas Kandagatla fastrpc_map_dma_buf(struct dma_buf_attachment *attachment, 4606cffd795SSrinivas Kandagatla enum dma_data_direction dir) 4616cffd795SSrinivas Kandagatla { 4626cffd795SSrinivas Kandagatla struct fastrpc_dma_buf_attachment *a = attachment->priv; 4636cffd795SSrinivas Kandagatla struct sg_table *table; 4646cffd795SSrinivas Kandagatla 4656cffd795SSrinivas Kandagatla table = &a->sgt; 4666cffd795SSrinivas Kandagatla 4676cffd795SSrinivas Kandagatla if (!dma_map_sg(attachment->dev, table->sgl, table->nents, dir)) 4686cffd795SSrinivas Kandagatla return ERR_PTR(-ENOMEM); 4696cffd795SSrinivas Kandagatla 4706cffd795SSrinivas Kandagatla return table; 4716cffd795SSrinivas Kandagatla } 4726cffd795SSrinivas Kandagatla 4736cffd795SSrinivas Kandagatla static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach, 4746cffd795SSrinivas Kandagatla struct sg_table *table, 4756cffd795SSrinivas Kandagatla enum dma_data_direction dir) 4766cffd795SSrinivas Kandagatla { 4776cffd795SSrinivas Kandagatla dma_unmap_sg(attach->dev, table->sgl, table->nents, dir); 4786cffd795SSrinivas Kandagatla } 4796cffd795SSrinivas Kandagatla 4806cffd795SSrinivas Kandagatla static void fastrpc_release(struct dma_buf *dmabuf) 4816cffd795SSrinivas Kandagatla { 4826cffd795SSrinivas Kandagatla struct fastrpc_buf *buffer = dmabuf->priv; 4836cffd795SSrinivas Kandagatla 4846cffd795SSrinivas Kandagatla fastrpc_buf_free(buffer); 4856cffd795SSrinivas Kandagatla } 4866cffd795SSrinivas Kandagatla 4876cffd795SSrinivas Kandagatla static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf, 4886cffd795SSrinivas Kandagatla struct dma_buf_attachment *attachment) 4896cffd795SSrinivas Kandagatla { 4906cffd795SSrinivas Kandagatla struct fastrpc_dma_buf_attachment *a; 4916cffd795SSrinivas Kandagatla struct fastrpc_buf *buffer = dmabuf->priv; 4926cffd795SSrinivas Kandagatla int ret; 4936cffd795SSrinivas Kandagatla 4946cffd795SSrinivas Kandagatla a = kzalloc(sizeof(*a), GFP_KERNEL); 4956cffd795SSrinivas Kandagatla if (!a) 4966cffd795SSrinivas Kandagatla return -ENOMEM; 4976cffd795SSrinivas Kandagatla 4986cffd795SSrinivas Kandagatla ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt, 4996cffd795SSrinivas Kandagatla FASTRPC_PHYS(buffer->phys), buffer->size); 5006cffd795SSrinivas Kandagatla if (ret < 0) { 5016cffd795SSrinivas Kandagatla dev_err(buffer->dev, "failed to get scatterlist from DMA API\n"); 5026cffd795SSrinivas Kandagatla return -EINVAL; 5036cffd795SSrinivas Kandagatla } 5046cffd795SSrinivas Kandagatla 5056cffd795SSrinivas Kandagatla a->dev = attachment->dev; 5066cffd795SSrinivas Kandagatla INIT_LIST_HEAD(&a->node); 5076cffd795SSrinivas Kandagatla attachment->priv = a; 5086cffd795SSrinivas Kandagatla 5096cffd795SSrinivas Kandagatla mutex_lock(&buffer->lock); 5106cffd795SSrinivas Kandagatla list_add(&a->node, &buffer->attachments); 5116cffd795SSrinivas Kandagatla mutex_unlock(&buffer->lock); 5126cffd795SSrinivas Kandagatla 5136cffd795SSrinivas Kandagatla return 0; 5146cffd795SSrinivas Kandagatla } 5156cffd795SSrinivas Kandagatla 5166cffd795SSrinivas Kandagatla static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf, 5176cffd795SSrinivas Kandagatla struct dma_buf_attachment *attachment) 5186cffd795SSrinivas Kandagatla { 5196cffd795SSrinivas Kandagatla struct fastrpc_dma_buf_attachment *a = attachment->priv; 5206cffd795SSrinivas Kandagatla struct fastrpc_buf *buffer = dmabuf->priv; 5216cffd795SSrinivas Kandagatla 5226cffd795SSrinivas Kandagatla mutex_lock(&buffer->lock); 5236cffd795SSrinivas Kandagatla list_del(&a->node); 5246cffd795SSrinivas Kandagatla mutex_unlock(&buffer->lock); 5256cffd795SSrinivas Kandagatla kfree(a); 5266cffd795SSrinivas Kandagatla } 5276cffd795SSrinivas Kandagatla 5286cffd795SSrinivas Kandagatla static void *fastrpc_kmap(struct dma_buf *dmabuf, unsigned long pgnum) 5296cffd795SSrinivas Kandagatla { 5306cffd795SSrinivas Kandagatla struct fastrpc_buf *buf = dmabuf->priv; 5316cffd795SSrinivas Kandagatla 5326cffd795SSrinivas Kandagatla return buf->virt ? buf->virt + pgnum * PAGE_SIZE : NULL; 5336cffd795SSrinivas Kandagatla } 5346cffd795SSrinivas Kandagatla 5356cffd795SSrinivas Kandagatla static void *fastrpc_vmap(struct dma_buf *dmabuf) 5366cffd795SSrinivas Kandagatla { 5376cffd795SSrinivas Kandagatla struct fastrpc_buf *buf = dmabuf->priv; 5386cffd795SSrinivas Kandagatla 5396cffd795SSrinivas Kandagatla return buf->virt; 5406cffd795SSrinivas Kandagatla } 5416cffd795SSrinivas Kandagatla 5426cffd795SSrinivas Kandagatla static int fastrpc_mmap(struct dma_buf *dmabuf, 5436cffd795SSrinivas Kandagatla struct vm_area_struct *vma) 5446cffd795SSrinivas Kandagatla { 5456cffd795SSrinivas Kandagatla struct fastrpc_buf *buf = dmabuf->priv; 5466cffd795SSrinivas Kandagatla size_t size = vma->vm_end - vma->vm_start; 5476cffd795SSrinivas Kandagatla 5486cffd795SSrinivas Kandagatla return dma_mmap_coherent(buf->dev, vma, buf->virt, 5496cffd795SSrinivas Kandagatla FASTRPC_PHYS(buf->phys), size); 5506cffd795SSrinivas Kandagatla } 5516cffd795SSrinivas Kandagatla 5526cffd795SSrinivas Kandagatla static const struct dma_buf_ops fastrpc_dma_buf_ops = { 5536cffd795SSrinivas Kandagatla .attach = fastrpc_dma_buf_attach, 5546cffd795SSrinivas Kandagatla .detach = fastrpc_dma_buf_detatch, 5556cffd795SSrinivas Kandagatla .map_dma_buf = fastrpc_map_dma_buf, 5566cffd795SSrinivas Kandagatla .unmap_dma_buf = fastrpc_unmap_dma_buf, 5576cffd795SSrinivas Kandagatla .mmap = fastrpc_mmap, 5586cffd795SSrinivas Kandagatla .map = fastrpc_kmap, 5596cffd795SSrinivas Kandagatla .vmap = fastrpc_vmap, 5606cffd795SSrinivas Kandagatla .release = fastrpc_release, 5616cffd795SSrinivas Kandagatla }; 5626cffd795SSrinivas Kandagatla 563c68cfb71SSrinivas Kandagatla static int fastrpc_map_create(struct fastrpc_user *fl, int fd, 564c68cfb71SSrinivas Kandagatla u64 len, struct fastrpc_map **ppmap) 565c68cfb71SSrinivas Kandagatla { 566c68cfb71SSrinivas Kandagatla struct fastrpc_session_ctx *sess = fl->sctx; 567c68cfb71SSrinivas Kandagatla struct fastrpc_map *map = NULL; 568c68cfb71SSrinivas Kandagatla int err = 0; 569c68cfb71SSrinivas Kandagatla 570c68cfb71SSrinivas Kandagatla if (!fastrpc_map_find(fl, fd, ppmap)) 571c68cfb71SSrinivas Kandagatla return 0; 572c68cfb71SSrinivas Kandagatla 573c68cfb71SSrinivas Kandagatla map = kzalloc(sizeof(*map), GFP_KERNEL); 574c68cfb71SSrinivas Kandagatla if (!map) 575c68cfb71SSrinivas Kandagatla return -ENOMEM; 576c68cfb71SSrinivas Kandagatla 577c68cfb71SSrinivas Kandagatla INIT_LIST_HEAD(&map->node); 578c68cfb71SSrinivas Kandagatla map->fl = fl; 579c68cfb71SSrinivas Kandagatla map->fd = fd; 580c68cfb71SSrinivas Kandagatla map->buf = dma_buf_get(fd); 581682a6044SWei Yongjun if (IS_ERR(map->buf)) { 582682a6044SWei Yongjun err = PTR_ERR(map->buf); 583c68cfb71SSrinivas Kandagatla goto get_err; 584c68cfb71SSrinivas Kandagatla } 585c68cfb71SSrinivas Kandagatla 586c68cfb71SSrinivas Kandagatla map->attach = dma_buf_attach(map->buf, sess->dev); 587c68cfb71SSrinivas Kandagatla if (IS_ERR(map->attach)) { 588c68cfb71SSrinivas Kandagatla dev_err(sess->dev, "Failed to attach dmabuf\n"); 589c68cfb71SSrinivas Kandagatla err = PTR_ERR(map->attach); 590c68cfb71SSrinivas Kandagatla goto attach_err; 591c68cfb71SSrinivas Kandagatla } 592c68cfb71SSrinivas Kandagatla 593c68cfb71SSrinivas Kandagatla map->table = dma_buf_map_attachment(map->attach, DMA_BIDIRECTIONAL); 594c68cfb71SSrinivas Kandagatla if (IS_ERR(map->table)) { 595c68cfb71SSrinivas Kandagatla err = PTR_ERR(map->table); 596c68cfb71SSrinivas Kandagatla goto map_err; 597c68cfb71SSrinivas Kandagatla } 598c68cfb71SSrinivas Kandagatla 599c68cfb71SSrinivas Kandagatla map->phys = sg_dma_address(map->table->sgl); 600c68cfb71SSrinivas Kandagatla map->phys += ((u64)fl->sctx->sid << 32); 601c68cfb71SSrinivas Kandagatla map->size = len; 602c68cfb71SSrinivas Kandagatla map->va = sg_virt(map->table->sgl); 603c68cfb71SSrinivas Kandagatla map->len = len; 604c68cfb71SSrinivas Kandagatla kref_init(&map->refcount); 605c68cfb71SSrinivas Kandagatla 606c68cfb71SSrinivas Kandagatla spin_lock(&fl->lock); 607c68cfb71SSrinivas Kandagatla list_add_tail(&map->node, &fl->maps); 608c68cfb71SSrinivas Kandagatla spin_unlock(&fl->lock); 609c68cfb71SSrinivas Kandagatla *ppmap = map; 610c68cfb71SSrinivas Kandagatla 611c68cfb71SSrinivas Kandagatla return 0; 612c68cfb71SSrinivas Kandagatla 613c68cfb71SSrinivas Kandagatla map_err: 614c68cfb71SSrinivas Kandagatla dma_buf_detach(map->buf, map->attach); 615c68cfb71SSrinivas Kandagatla attach_err: 616c68cfb71SSrinivas Kandagatla dma_buf_put(map->buf); 617c68cfb71SSrinivas Kandagatla get_err: 618c68cfb71SSrinivas Kandagatla kfree(map); 619c68cfb71SSrinivas Kandagatla 620c68cfb71SSrinivas Kandagatla return err; 621c68cfb71SSrinivas Kandagatla } 622c68cfb71SSrinivas Kandagatla 623c68cfb71SSrinivas Kandagatla /* 624c68cfb71SSrinivas Kandagatla * Fastrpc payload buffer with metadata looks like: 625c68cfb71SSrinivas Kandagatla * 626c68cfb71SSrinivas Kandagatla * >>>>>> START of METADATA <<<<<<<<< 627c68cfb71SSrinivas Kandagatla * +---------------------------------+ 628c68cfb71SSrinivas Kandagatla * | Arguments | 629c68cfb71SSrinivas Kandagatla * | type:(struct fastrpc_remote_arg)| 630c68cfb71SSrinivas Kandagatla * | (0 - N) | 631c68cfb71SSrinivas Kandagatla * +---------------------------------+ 632c68cfb71SSrinivas Kandagatla * | Invoke Buffer list | 633c68cfb71SSrinivas Kandagatla * | type:(struct fastrpc_invoke_buf)| 634c68cfb71SSrinivas Kandagatla * | (0 - N) | 635c68cfb71SSrinivas Kandagatla * +---------------------------------+ 636c68cfb71SSrinivas Kandagatla * | Page info list | 637c68cfb71SSrinivas Kandagatla * | type:(struct fastrpc_phy_page) | 638c68cfb71SSrinivas Kandagatla * | (0 - N) | 639c68cfb71SSrinivas Kandagatla * +---------------------------------+ 640c68cfb71SSrinivas Kandagatla * | Optional info | 641c68cfb71SSrinivas Kandagatla * |(can be specific to SoC/Firmware)| 642c68cfb71SSrinivas Kandagatla * +---------------------------------+ 643c68cfb71SSrinivas Kandagatla * >>>>>>>> END of METADATA <<<<<<<<< 644c68cfb71SSrinivas Kandagatla * +---------------------------------+ 645c68cfb71SSrinivas Kandagatla * | Inline ARGS | 646c68cfb71SSrinivas Kandagatla * | (0-N) | 647c68cfb71SSrinivas Kandagatla * +---------------------------------+ 648c68cfb71SSrinivas Kandagatla */ 649c68cfb71SSrinivas Kandagatla 650c68cfb71SSrinivas Kandagatla static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx) 651c68cfb71SSrinivas Kandagatla { 652c68cfb71SSrinivas Kandagatla int size = 0; 653c68cfb71SSrinivas Kandagatla 654c68cfb71SSrinivas Kandagatla size = (sizeof(struct fastrpc_remote_arg) + 655c68cfb71SSrinivas Kandagatla sizeof(struct fastrpc_invoke_buf) + 656c68cfb71SSrinivas Kandagatla sizeof(struct fastrpc_phy_page)) * ctx->nscalars + 657c68cfb71SSrinivas Kandagatla sizeof(u64) * FASTRPC_MAX_FDLIST + 658c68cfb71SSrinivas Kandagatla sizeof(u32) * FASTRPC_MAX_CRCLIST; 659c68cfb71SSrinivas Kandagatla 660c68cfb71SSrinivas Kandagatla return size; 661c68cfb71SSrinivas Kandagatla } 662c68cfb71SSrinivas Kandagatla 663c68cfb71SSrinivas Kandagatla static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen) 664c68cfb71SSrinivas Kandagatla { 665c68cfb71SSrinivas Kandagatla u64 size = 0; 666c68cfb71SSrinivas Kandagatla int i; 667c68cfb71SSrinivas Kandagatla 668c68cfb71SSrinivas Kandagatla size = ALIGN(metalen, FASTRPC_ALIGN); 669c68cfb71SSrinivas Kandagatla for (i = 0; i < ctx->nscalars; i++) { 670c68cfb71SSrinivas Kandagatla if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) { 671*25e8dfb8SSrinivas Kandagatla 672*25e8dfb8SSrinivas Kandagatla if (ctx->olaps[i].offset == 0) 673c68cfb71SSrinivas Kandagatla size = ALIGN(size, FASTRPC_ALIGN); 674*25e8dfb8SSrinivas Kandagatla 675*25e8dfb8SSrinivas Kandagatla size += (ctx->olaps[i].mend - ctx->olaps[i].mstart); 676c68cfb71SSrinivas Kandagatla } 677c68cfb71SSrinivas Kandagatla } 678c68cfb71SSrinivas Kandagatla 679c68cfb71SSrinivas Kandagatla return size; 680c68cfb71SSrinivas Kandagatla } 681c68cfb71SSrinivas Kandagatla 682c68cfb71SSrinivas Kandagatla static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx) 683c68cfb71SSrinivas Kandagatla { 684c68cfb71SSrinivas Kandagatla struct device *dev = ctx->fl->sctx->dev; 685c68cfb71SSrinivas Kandagatla int i, err; 686c68cfb71SSrinivas Kandagatla 687c68cfb71SSrinivas Kandagatla for (i = 0; i < ctx->nscalars; ++i) { 688c68cfb71SSrinivas Kandagatla /* Make sure reserved field is set to 0 */ 689c68cfb71SSrinivas Kandagatla if (ctx->args[i].reserved) 690c68cfb71SSrinivas Kandagatla return -EINVAL; 691c68cfb71SSrinivas Kandagatla 692c68cfb71SSrinivas Kandagatla if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 || 693c68cfb71SSrinivas Kandagatla ctx->args[i].length == 0) 694c68cfb71SSrinivas Kandagatla continue; 695c68cfb71SSrinivas Kandagatla 696c68cfb71SSrinivas Kandagatla err = fastrpc_map_create(ctx->fl, ctx->args[i].fd, 697c68cfb71SSrinivas Kandagatla ctx->args[i].length, &ctx->maps[i]); 698c68cfb71SSrinivas Kandagatla if (err) { 699c68cfb71SSrinivas Kandagatla dev_err(dev, "Error Creating map %d\n", err); 700c68cfb71SSrinivas Kandagatla return -EINVAL; 701c68cfb71SSrinivas Kandagatla } 702c68cfb71SSrinivas Kandagatla 703c68cfb71SSrinivas Kandagatla } 704c68cfb71SSrinivas Kandagatla return 0; 705c68cfb71SSrinivas Kandagatla } 706c68cfb71SSrinivas Kandagatla 707c68cfb71SSrinivas Kandagatla static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx) 708c68cfb71SSrinivas Kandagatla { 709c68cfb71SSrinivas Kandagatla struct device *dev = ctx->fl->sctx->dev; 710c68cfb71SSrinivas Kandagatla struct fastrpc_remote_arg *rpra; 711c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_buf *list; 712c68cfb71SSrinivas Kandagatla struct fastrpc_phy_page *pages; 713*25e8dfb8SSrinivas Kandagatla int inbufs, i, oix, err = 0; 714*25e8dfb8SSrinivas Kandagatla u64 len, rlen, pkt_size; 715c68cfb71SSrinivas Kandagatla uintptr_t args; 716c68cfb71SSrinivas Kandagatla int metalen; 717c68cfb71SSrinivas Kandagatla 718c68cfb71SSrinivas Kandagatla inbufs = REMOTE_SCALARS_INBUFS(ctx->sc); 719c68cfb71SSrinivas Kandagatla metalen = fastrpc_get_meta_size(ctx); 720c68cfb71SSrinivas Kandagatla pkt_size = fastrpc_get_payload_size(ctx, metalen); 721c68cfb71SSrinivas Kandagatla 722c68cfb71SSrinivas Kandagatla err = fastrpc_create_maps(ctx); 723c68cfb71SSrinivas Kandagatla if (err) 724c68cfb71SSrinivas Kandagatla return err; 725c68cfb71SSrinivas Kandagatla 726c68cfb71SSrinivas Kandagatla ctx->msg_sz = pkt_size; 727c68cfb71SSrinivas Kandagatla 728c68cfb71SSrinivas Kandagatla err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf); 729c68cfb71SSrinivas Kandagatla if (err) 730c68cfb71SSrinivas Kandagatla return err; 731c68cfb71SSrinivas Kandagatla 732c68cfb71SSrinivas Kandagatla rpra = ctx->buf->virt; 733c68cfb71SSrinivas Kandagatla list = ctx->buf->virt + ctx->nscalars * sizeof(*rpra); 734c68cfb71SSrinivas Kandagatla pages = ctx->buf->virt + ctx->nscalars * (sizeof(*list) + 735c68cfb71SSrinivas Kandagatla sizeof(*rpra)); 736c68cfb71SSrinivas Kandagatla args = (uintptr_t)ctx->buf->virt + metalen; 737c68cfb71SSrinivas Kandagatla rlen = pkt_size - metalen; 738c68cfb71SSrinivas Kandagatla ctx->rpra = rpra; 739c68cfb71SSrinivas Kandagatla 740*25e8dfb8SSrinivas Kandagatla for (oix = 0; oix < ctx->nbufs; ++oix) { 741*25e8dfb8SSrinivas Kandagatla int mlen; 742*25e8dfb8SSrinivas Kandagatla 743*25e8dfb8SSrinivas Kandagatla i = ctx->olaps[oix].raix; 744*25e8dfb8SSrinivas Kandagatla len = ctx->args[i].length; 745c68cfb71SSrinivas Kandagatla 746c68cfb71SSrinivas Kandagatla rpra[i].pv = 0; 747c68cfb71SSrinivas Kandagatla rpra[i].len = len; 748c68cfb71SSrinivas Kandagatla list[i].num = len ? 1 : 0; 749c68cfb71SSrinivas Kandagatla list[i].pgidx = i; 750c68cfb71SSrinivas Kandagatla 751c68cfb71SSrinivas Kandagatla if (!len) 752c68cfb71SSrinivas Kandagatla continue; 753c68cfb71SSrinivas Kandagatla 754c68cfb71SSrinivas Kandagatla pages[i].size = roundup(len, PAGE_SIZE); 755c68cfb71SSrinivas Kandagatla 756c68cfb71SSrinivas Kandagatla if (ctx->maps[i]) { 75780f3afd7SSrinivas Kandagatla struct vm_area_struct *vma = NULL; 75880f3afd7SSrinivas Kandagatla 759c68cfb71SSrinivas Kandagatla rpra[i].pv = (u64) ctx->args[i].ptr; 760c68cfb71SSrinivas Kandagatla pages[i].addr = ctx->maps[i]->phys; 76180f3afd7SSrinivas Kandagatla 76280f3afd7SSrinivas Kandagatla vma = find_vma(current->mm, ctx->args[i].ptr); 76380f3afd7SSrinivas Kandagatla if (vma) 76480f3afd7SSrinivas Kandagatla pages[i].addr += ctx->args[i].ptr - 76580f3afd7SSrinivas Kandagatla vma->vm_start; 76680f3afd7SSrinivas Kandagatla 767c68cfb71SSrinivas Kandagatla } else { 768*25e8dfb8SSrinivas Kandagatla 769*25e8dfb8SSrinivas Kandagatla if (ctx->olaps[oix].offset == 0) { 770c68cfb71SSrinivas Kandagatla rlen -= ALIGN(args, FASTRPC_ALIGN) - args; 771c68cfb71SSrinivas Kandagatla args = ALIGN(args, FASTRPC_ALIGN); 772*25e8dfb8SSrinivas Kandagatla } 773*25e8dfb8SSrinivas Kandagatla 774*25e8dfb8SSrinivas Kandagatla mlen = ctx->olaps[oix].mend - ctx->olaps[oix].mstart; 775*25e8dfb8SSrinivas Kandagatla 776*25e8dfb8SSrinivas Kandagatla if (rlen < mlen) 777c68cfb71SSrinivas Kandagatla goto bail; 778c68cfb71SSrinivas Kandagatla 779*25e8dfb8SSrinivas Kandagatla rpra[i].pv = args - ctx->olaps[oix].offset; 780*25e8dfb8SSrinivas Kandagatla pages[i].addr = ctx->buf->phys - 781*25e8dfb8SSrinivas Kandagatla ctx->olaps[oix].offset + 782*25e8dfb8SSrinivas Kandagatla (pkt_size - rlen); 783c68cfb71SSrinivas Kandagatla pages[i].addr = pages[i].addr & PAGE_MASK; 784*25e8dfb8SSrinivas Kandagatla 785*25e8dfb8SSrinivas Kandagatla args = args + mlen; 786*25e8dfb8SSrinivas Kandagatla rlen -= mlen; 787c68cfb71SSrinivas Kandagatla } 788c68cfb71SSrinivas Kandagatla 789c68cfb71SSrinivas Kandagatla if (i < inbufs && !ctx->maps[i]) { 790c68cfb71SSrinivas Kandagatla void *dst = (void *)(uintptr_t)rpra[i].pv; 791c68cfb71SSrinivas Kandagatla void *src = (void *)(uintptr_t)ctx->args[i].ptr; 792c68cfb71SSrinivas Kandagatla 793c68cfb71SSrinivas Kandagatla if (!kernel) { 794c68cfb71SSrinivas Kandagatla if (copy_from_user(dst, (void __user *)src, 795c68cfb71SSrinivas Kandagatla len)) { 796c68cfb71SSrinivas Kandagatla err = -EFAULT; 797c68cfb71SSrinivas Kandagatla goto bail; 798c68cfb71SSrinivas Kandagatla } 799c68cfb71SSrinivas Kandagatla } else { 800c68cfb71SSrinivas Kandagatla memcpy(dst, src, len); 801c68cfb71SSrinivas Kandagatla } 802c68cfb71SSrinivas Kandagatla } 803c68cfb71SSrinivas Kandagatla } 804c68cfb71SSrinivas Kandagatla 805c68cfb71SSrinivas Kandagatla for (i = ctx->nbufs; i < ctx->nscalars; ++i) { 806c68cfb71SSrinivas Kandagatla rpra[i].pv = (u64) ctx->args[i].ptr; 807c68cfb71SSrinivas Kandagatla rpra[i].len = ctx->args[i].length; 808c68cfb71SSrinivas Kandagatla list[i].num = ctx->args[i].length ? 1 : 0; 809c68cfb71SSrinivas Kandagatla list[i].pgidx = i; 810c68cfb71SSrinivas Kandagatla pages[i].addr = ctx->maps[i]->phys; 811c68cfb71SSrinivas Kandagatla pages[i].size = ctx->maps[i]->size; 812c68cfb71SSrinivas Kandagatla } 813c68cfb71SSrinivas Kandagatla 814c68cfb71SSrinivas Kandagatla bail: 815c68cfb71SSrinivas Kandagatla if (err) 816c68cfb71SSrinivas Kandagatla dev_err(dev, "Error: get invoke args failed:%d\n", err); 817c68cfb71SSrinivas Kandagatla 818c68cfb71SSrinivas Kandagatla return err; 819c68cfb71SSrinivas Kandagatla } 820c68cfb71SSrinivas Kandagatla 821c68cfb71SSrinivas Kandagatla static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx, 822c68cfb71SSrinivas Kandagatla u32 kernel) 823c68cfb71SSrinivas Kandagatla { 824c68cfb71SSrinivas Kandagatla struct fastrpc_remote_arg *rpra = ctx->rpra; 825c68cfb71SSrinivas Kandagatla int i, inbufs; 826c68cfb71SSrinivas Kandagatla 827c68cfb71SSrinivas Kandagatla inbufs = REMOTE_SCALARS_INBUFS(ctx->sc); 828c68cfb71SSrinivas Kandagatla 829c68cfb71SSrinivas Kandagatla for (i = inbufs; i < ctx->nbufs; ++i) { 830c68cfb71SSrinivas Kandagatla void *src = (void *)(uintptr_t)rpra[i].pv; 831c68cfb71SSrinivas Kandagatla void *dst = (void *)(uintptr_t)ctx->args[i].ptr; 832c68cfb71SSrinivas Kandagatla u64 len = rpra[i].len; 833c68cfb71SSrinivas Kandagatla 834c68cfb71SSrinivas Kandagatla if (!kernel) { 835c68cfb71SSrinivas Kandagatla if (copy_to_user((void __user *)dst, src, len)) 836c68cfb71SSrinivas Kandagatla return -EFAULT; 837c68cfb71SSrinivas Kandagatla } else { 838c68cfb71SSrinivas Kandagatla memcpy(dst, src, len); 839c68cfb71SSrinivas Kandagatla } 840c68cfb71SSrinivas Kandagatla } 841c68cfb71SSrinivas Kandagatla 842c68cfb71SSrinivas Kandagatla return 0; 843c68cfb71SSrinivas Kandagatla } 844c68cfb71SSrinivas Kandagatla 845c68cfb71SSrinivas Kandagatla static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx, 846c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx, 847c68cfb71SSrinivas Kandagatla u32 kernel, uint32_t handle) 848c68cfb71SSrinivas Kandagatla { 849c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 850c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl = ctx->fl; 851c68cfb71SSrinivas Kandagatla struct fastrpc_msg *msg = &ctx->msg; 852c68cfb71SSrinivas Kandagatla 853c68cfb71SSrinivas Kandagatla cctx = fl->cctx; 854c68cfb71SSrinivas Kandagatla msg->pid = fl->tgid; 855c68cfb71SSrinivas Kandagatla msg->tid = current->pid; 856c68cfb71SSrinivas Kandagatla 857c68cfb71SSrinivas Kandagatla if (kernel) 858c68cfb71SSrinivas Kandagatla msg->pid = 0; 859c68cfb71SSrinivas Kandagatla 860c68cfb71SSrinivas Kandagatla msg->ctx = ctx->ctxid | fl->pd; 861c68cfb71SSrinivas Kandagatla msg->handle = handle; 862c68cfb71SSrinivas Kandagatla msg->sc = ctx->sc; 863c68cfb71SSrinivas Kandagatla msg->addr = ctx->buf ? ctx->buf->phys : 0; 864c68cfb71SSrinivas Kandagatla msg->size = roundup(ctx->msg_sz, PAGE_SIZE); 865c68cfb71SSrinivas Kandagatla fastrpc_context_get(ctx); 866c68cfb71SSrinivas Kandagatla 867c68cfb71SSrinivas Kandagatla return rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg)); 868c68cfb71SSrinivas Kandagatla } 869c68cfb71SSrinivas Kandagatla 870c68cfb71SSrinivas Kandagatla static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, 871c68cfb71SSrinivas Kandagatla u32 handle, u32 sc, 872c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_args *args) 873c68cfb71SSrinivas Kandagatla { 874c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx = NULL; 875c68cfb71SSrinivas Kandagatla int err = 0; 876c68cfb71SSrinivas Kandagatla 877c68cfb71SSrinivas Kandagatla if (!fl->sctx) 878c68cfb71SSrinivas Kandagatla return -EINVAL; 879c68cfb71SSrinivas Kandagatla 880c68cfb71SSrinivas Kandagatla ctx = fastrpc_context_alloc(fl, kernel, sc, args); 881c68cfb71SSrinivas Kandagatla if (IS_ERR(ctx)) 882c68cfb71SSrinivas Kandagatla return PTR_ERR(ctx); 883c68cfb71SSrinivas Kandagatla 884c68cfb71SSrinivas Kandagatla if (ctx->nscalars) { 885c68cfb71SSrinivas Kandagatla err = fastrpc_get_args(kernel, ctx); 886c68cfb71SSrinivas Kandagatla if (err) 887c68cfb71SSrinivas Kandagatla goto bail; 888c68cfb71SSrinivas Kandagatla } 889415a0729SSrinivas Kandagatla 890415a0729SSrinivas Kandagatla /* make sure that all CPU memory writes are seen by DSP */ 891415a0729SSrinivas Kandagatla dma_wmb(); 892c68cfb71SSrinivas Kandagatla /* Send invoke buffer to remote dsp */ 893c68cfb71SSrinivas Kandagatla err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle); 894c68cfb71SSrinivas Kandagatla if (err) 895c68cfb71SSrinivas Kandagatla goto bail; 896c68cfb71SSrinivas Kandagatla 897c68cfb71SSrinivas Kandagatla /* Wait for remote dsp to respond or time out */ 898c68cfb71SSrinivas Kandagatla err = wait_for_completion_interruptible(&ctx->work); 899c68cfb71SSrinivas Kandagatla if (err) 900c68cfb71SSrinivas Kandagatla goto bail; 901c68cfb71SSrinivas Kandagatla 902c68cfb71SSrinivas Kandagatla /* Check the response from remote dsp */ 903c68cfb71SSrinivas Kandagatla err = ctx->retval; 904c68cfb71SSrinivas Kandagatla if (err) 905c68cfb71SSrinivas Kandagatla goto bail; 906c68cfb71SSrinivas Kandagatla 907c68cfb71SSrinivas Kandagatla if (ctx->nscalars) { 908415a0729SSrinivas Kandagatla /* make sure that all memory writes by DSP are seen by CPU */ 909415a0729SSrinivas Kandagatla dma_rmb(); 910c68cfb71SSrinivas Kandagatla /* populate all the output buffers with results */ 911c68cfb71SSrinivas Kandagatla err = fastrpc_put_args(ctx, kernel); 912c68cfb71SSrinivas Kandagatla if (err) 913c68cfb71SSrinivas Kandagatla goto bail; 914c68cfb71SSrinivas Kandagatla } 915c68cfb71SSrinivas Kandagatla 916c68cfb71SSrinivas Kandagatla bail: 917c68cfb71SSrinivas Kandagatla /* We are done with this compute context, remove it from pending list */ 918c68cfb71SSrinivas Kandagatla spin_lock(&fl->lock); 919c68cfb71SSrinivas Kandagatla list_del(&ctx->node); 920c68cfb71SSrinivas Kandagatla spin_unlock(&fl->lock); 921c68cfb71SSrinivas Kandagatla fastrpc_context_put(ctx); 922c68cfb71SSrinivas Kandagatla 923c68cfb71SSrinivas Kandagatla if (err) 924c68cfb71SSrinivas Kandagatla dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err); 925c68cfb71SSrinivas Kandagatla 926c68cfb71SSrinivas Kandagatla return err; 927c68cfb71SSrinivas Kandagatla } 928c68cfb71SSrinivas Kandagatla 929d73f71c7SSrinivas Kandagatla static int fastrpc_init_create_process(struct fastrpc_user *fl, 930d73f71c7SSrinivas Kandagatla char __user *argp) 931d73f71c7SSrinivas Kandagatla { 932d73f71c7SSrinivas Kandagatla struct fastrpc_init_create init; 933d73f71c7SSrinivas Kandagatla struct fastrpc_invoke_args *args; 934d73f71c7SSrinivas Kandagatla struct fastrpc_phy_page pages[1]; 935d73f71c7SSrinivas Kandagatla struct fastrpc_map *map = NULL; 936d73f71c7SSrinivas Kandagatla struct fastrpc_buf *imem = NULL; 937d73f71c7SSrinivas Kandagatla int memlen; 938d73f71c7SSrinivas Kandagatla int err; 939d73f71c7SSrinivas Kandagatla struct { 940d73f71c7SSrinivas Kandagatla int pgid; 941d73f71c7SSrinivas Kandagatla u32 namelen; 942d73f71c7SSrinivas Kandagatla u32 filelen; 943d73f71c7SSrinivas Kandagatla u32 pageslen; 944d73f71c7SSrinivas Kandagatla u32 attrs; 945d73f71c7SSrinivas Kandagatla u32 siglen; 946d73f71c7SSrinivas Kandagatla } inbuf; 947d73f71c7SSrinivas Kandagatla u32 sc; 948d73f71c7SSrinivas Kandagatla 949d73f71c7SSrinivas Kandagatla args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL); 950d73f71c7SSrinivas Kandagatla if (!args) 951d73f71c7SSrinivas Kandagatla return -ENOMEM; 952d73f71c7SSrinivas Kandagatla 953d73f71c7SSrinivas Kandagatla if (copy_from_user(&init, argp, sizeof(init))) { 954d73f71c7SSrinivas Kandagatla err = -EFAULT; 955b49f6d83SThierry Escande goto err; 956d73f71c7SSrinivas Kandagatla } 957d73f71c7SSrinivas Kandagatla 958d73f71c7SSrinivas Kandagatla if (init.filelen > INIT_FILELEN_MAX) { 959d73f71c7SSrinivas Kandagatla err = -EINVAL; 960b49f6d83SThierry Escande goto err; 961d73f71c7SSrinivas Kandagatla } 962d73f71c7SSrinivas Kandagatla 963d73f71c7SSrinivas Kandagatla inbuf.pgid = fl->tgid; 964d73f71c7SSrinivas Kandagatla inbuf.namelen = strlen(current->comm) + 1; 965d73f71c7SSrinivas Kandagatla inbuf.filelen = init.filelen; 966d73f71c7SSrinivas Kandagatla inbuf.pageslen = 1; 967d73f71c7SSrinivas Kandagatla inbuf.attrs = init.attrs; 968d73f71c7SSrinivas Kandagatla inbuf.siglen = init.siglen; 969d73f71c7SSrinivas Kandagatla fl->pd = 1; 970d73f71c7SSrinivas Kandagatla 971d73f71c7SSrinivas Kandagatla if (init.filelen && init.filefd) { 972d73f71c7SSrinivas Kandagatla err = fastrpc_map_create(fl, init.filefd, init.filelen, &map); 973d73f71c7SSrinivas Kandagatla if (err) 974b49f6d83SThierry Escande goto err; 975d73f71c7SSrinivas Kandagatla } 976d73f71c7SSrinivas Kandagatla 977d73f71c7SSrinivas Kandagatla memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4), 978d73f71c7SSrinivas Kandagatla 1024 * 1024); 979d73f71c7SSrinivas Kandagatla err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen, 980d73f71c7SSrinivas Kandagatla &imem); 981b49f6d83SThierry Escande if (err) 982b49f6d83SThierry Escande goto err_alloc; 983d73f71c7SSrinivas Kandagatla 984d73f71c7SSrinivas Kandagatla fl->init_mem = imem; 985d73f71c7SSrinivas Kandagatla args[0].ptr = (u64)(uintptr_t)&inbuf; 986d73f71c7SSrinivas Kandagatla args[0].length = sizeof(inbuf); 987d73f71c7SSrinivas Kandagatla args[0].fd = -1; 988d73f71c7SSrinivas Kandagatla 989d73f71c7SSrinivas Kandagatla args[1].ptr = (u64)(uintptr_t)current->comm; 990d73f71c7SSrinivas Kandagatla args[1].length = inbuf.namelen; 991d73f71c7SSrinivas Kandagatla args[1].fd = -1; 992d73f71c7SSrinivas Kandagatla 993d73f71c7SSrinivas Kandagatla args[2].ptr = (u64) init.file; 994d73f71c7SSrinivas Kandagatla args[2].length = inbuf.filelen; 995d73f71c7SSrinivas Kandagatla args[2].fd = init.filefd; 996d73f71c7SSrinivas Kandagatla 997d73f71c7SSrinivas Kandagatla pages[0].addr = imem->phys; 998d73f71c7SSrinivas Kandagatla pages[0].size = imem->size; 999d73f71c7SSrinivas Kandagatla 1000d73f71c7SSrinivas Kandagatla args[3].ptr = (u64)(uintptr_t) pages; 1001d73f71c7SSrinivas Kandagatla args[3].length = 1 * sizeof(*pages); 1002d73f71c7SSrinivas Kandagatla args[3].fd = -1; 1003d73f71c7SSrinivas Kandagatla 1004d73f71c7SSrinivas Kandagatla args[4].ptr = (u64)(uintptr_t)&inbuf.attrs; 1005d73f71c7SSrinivas Kandagatla args[4].length = sizeof(inbuf.attrs); 1006d73f71c7SSrinivas Kandagatla args[4].fd = -1; 1007d73f71c7SSrinivas Kandagatla 1008d73f71c7SSrinivas Kandagatla args[5].ptr = (u64)(uintptr_t) &inbuf.siglen; 1009d73f71c7SSrinivas Kandagatla args[5].length = sizeof(inbuf.siglen); 1010d73f71c7SSrinivas Kandagatla args[5].fd = -1; 1011d73f71c7SSrinivas Kandagatla 1012d73f71c7SSrinivas Kandagatla sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0); 1013d73f71c7SSrinivas Kandagatla if (init.attrs) 1014d73f71c7SSrinivas Kandagatla sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0); 1015d73f71c7SSrinivas Kandagatla 1016d73f71c7SSrinivas Kandagatla err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, 1017d73f71c7SSrinivas Kandagatla sc, args); 1018b49f6d83SThierry Escande if (err) 1019b49f6d83SThierry Escande goto err_invoke; 1020d73f71c7SSrinivas Kandagatla 1021b49f6d83SThierry Escande kfree(args); 1022b49f6d83SThierry Escande 1023b49f6d83SThierry Escande return 0; 1024b49f6d83SThierry Escande 1025b49f6d83SThierry Escande err_invoke: 1026b49f6d83SThierry Escande fl->init_mem = NULL; 1027d73f71c7SSrinivas Kandagatla fastrpc_buf_free(imem); 1028b49f6d83SThierry Escande err_alloc: 1029b49f6d83SThierry Escande if (map) { 1030b49f6d83SThierry Escande spin_lock(&fl->lock); 1031b49f6d83SThierry Escande list_del(&map->node); 1032b49f6d83SThierry Escande spin_unlock(&fl->lock); 1033b49f6d83SThierry Escande fastrpc_map_put(map); 1034d73f71c7SSrinivas Kandagatla } 1035b49f6d83SThierry Escande err: 1036d73f71c7SSrinivas Kandagatla kfree(args); 1037d73f71c7SSrinivas Kandagatla 1038d73f71c7SSrinivas Kandagatla return err; 1039d73f71c7SSrinivas Kandagatla } 1040d73f71c7SSrinivas Kandagatla 1041f6f9279fSSrinivas Kandagatla static struct fastrpc_session_ctx *fastrpc_session_alloc( 1042f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx) 1043f6f9279fSSrinivas Kandagatla { 1044f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *session = NULL; 1045977e6c8dSSrinivas Kandagatla unsigned long flags; 1046f6f9279fSSrinivas Kandagatla int i; 1047f6f9279fSSrinivas Kandagatla 1048977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1049f6f9279fSSrinivas Kandagatla for (i = 0; i < cctx->sesscount; i++) { 1050f6f9279fSSrinivas Kandagatla if (!cctx->session[i].used && cctx->session[i].valid) { 1051f6f9279fSSrinivas Kandagatla cctx->session[i].used = true; 1052f6f9279fSSrinivas Kandagatla session = &cctx->session[i]; 1053f6f9279fSSrinivas Kandagatla break; 1054f6f9279fSSrinivas Kandagatla } 1055f6f9279fSSrinivas Kandagatla } 1056977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1057f6f9279fSSrinivas Kandagatla 1058f6f9279fSSrinivas Kandagatla return session; 1059f6f9279fSSrinivas Kandagatla } 1060f6f9279fSSrinivas Kandagatla 1061f6f9279fSSrinivas Kandagatla static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx, 1062f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *session) 1063f6f9279fSSrinivas Kandagatla { 1064977e6c8dSSrinivas Kandagatla unsigned long flags; 1065977e6c8dSSrinivas Kandagatla 1066977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1067f6f9279fSSrinivas Kandagatla session->used = false; 1068977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1069f6f9279fSSrinivas Kandagatla } 1070f6f9279fSSrinivas Kandagatla 1071d73f71c7SSrinivas Kandagatla static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl) 1072d73f71c7SSrinivas Kandagatla { 1073d73f71c7SSrinivas Kandagatla struct fastrpc_invoke_args args[1]; 1074d73f71c7SSrinivas Kandagatla int tgid = 0; 1075d73f71c7SSrinivas Kandagatla u32 sc; 1076d73f71c7SSrinivas Kandagatla 1077d73f71c7SSrinivas Kandagatla tgid = fl->tgid; 1078d73f71c7SSrinivas Kandagatla args[0].ptr = (u64)(uintptr_t) &tgid; 1079d73f71c7SSrinivas Kandagatla args[0].length = sizeof(tgid); 1080d73f71c7SSrinivas Kandagatla args[0].fd = -1; 1081d73f71c7SSrinivas Kandagatla args[0].reserved = 0; 1082d73f71c7SSrinivas Kandagatla sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0); 1083d73f71c7SSrinivas Kandagatla 1084d73f71c7SSrinivas Kandagatla return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, 1085d73f71c7SSrinivas Kandagatla sc, &args[0]); 1086d73f71c7SSrinivas Kandagatla } 1087d73f71c7SSrinivas Kandagatla 1088f6f9279fSSrinivas Kandagatla static int fastrpc_device_release(struct inode *inode, struct file *file) 1089f6f9279fSSrinivas Kandagatla { 1090f6f9279fSSrinivas Kandagatla struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data; 1091f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = fl->cctx; 1092c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx, *n; 1093c68cfb71SSrinivas Kandagatla struct fastrpc_map *map, *m; 1094977e6c8dSSrinivas Kandagatla unsigned long flags; 1095f6f9279fSSrinivas Kandagatla 1096d73f71c7SSrinivas Kandagatla fastrpc_release_current_dsp_process(fl); 1097d73f71c7SSrinivas Kandagatla 1098977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1099f6f9279fSSrinivas Kandagatla list_del(&fl->user); 1100977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1101f6f9279fSSrinivas Kandagatla 1102c68cfb71SSrinivas Kandagatla if (fl->init_mem) 1103c68cfb71SSrinivas Kandagatla fastrpc_buf_free(fl->init_mem); 1104c68cfb71SSrinivas Kandagatla 1105c68cfb71SSrinivas Kandagatla list_for_each_entry_safe(ctx, n, &fl->pending, node) { 1106c68cfb71SSrinivas Kandagatla list_del(&ctx->node); 1107c68cfb71SSrinivas Kandagatla fastrpc_context_put(ctx); 1108c68cfb71SSrinivas Kandagatla } 1109c68cfb71SSrinivas Kandagatla 1110c68cfb71SSrinivas Kandagatla list_for_each_entry_safe(map, m, &fl->maps, node) { 1111c68cfb71SSrinivas Kandagatla list_del(&map->node); 1112c68cfb71SSrinivas Kandagatla fastrpc_map_put(map); 1113c68cfb71SSrinivas Kandagatla } 1114c68cfb71SSrinivas Kandagatla 1115f6f9279fSSrinivas Kandagatla fastrpc_session_free(cctx, fl->sctx); 1116f6f9279fSSrinivas Kandagatla 1117f6f9279fSSrinivas Kandagatla mutex_destroy(&fl->mutex); 1118f6f9279fSSrinivas Kandagatla kfree(fl); 1119f6f9279fSSrinivas Kandagatla file->private_data = NULL; 1120f6f9279fSSrinivas Kandagatla 1121f6f9279fSSrinivas Kandagatla return 0; 1122f6f9279fSSrinivas Kandagatla } 1123f6f9279fSSrinivas Kandagatla 1124f6f9279fSSrinivas Kandagatla static int fastrpc_device_open(struct inode *inode, struct file *filp) 1125f6f9279fSSrinivas Kandagatla { 1126f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = miscdev_to_cctx(filp->private_data); 1127f6f9279fSSrinivas Kandagatla struct fastrpc_user *fl = NULL; 1128977e6c8dSSrinivas Kandagatla unsigned long flags; 1129f6f9279fSSrinivas Kandagatla 1130f6f9279fSSrinivas Kandagatla fl = kzalloc(sizeof(*fl), GFP_KERNEL); 1131f6f9279fSSrinivas Kandagatla if (!fl) 1132f6f9279fSSrinivas Kandagatla return -ENOMEM; 1133f6f9279fSSrinivas Kandagatla 1134f6f9279fSSrinivas Kandagatla filp->private_data = fl; 1135f6f9279fSSrinivas Kandagatla spin_lock_init(&fl->lock); 1136f6f9279fSSrinivas Kandagatla mutex_init(&fl->mutex); 1137f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&fl->pending); 1138f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&fl->maps); 1139f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&fl->user); 1140f6f9279fSSrinivas Kandagatla fl->tgid = current->tgid; 1141f6f9279fSSrinivas Kandagatla fl->cctx = cctx; 11427c11df42SThierry Escande 11437c11df42SThierry Escande fl->sctx = fastrpc_session_alloc(cctx); 11447c11df42SThierry Escande if (!fl->sctx) { 11457c11df42SThierry Escande dev_err(&cctx->rpdev->dev, "No session available\n"); 11467c11df42SThierry Escande mutex_destroy(&fl->mutex); 11477c11df42SThierry Escande kfree(fl); 11487c11df42SThierry Escande 11497c11df42SThierry Escande return -EBUSY; 11507c11df42SThierry Escande } 11517c11df42SThierry Escande 1152977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1153f6f9279fSSrinivas Kandagatla list_add_tail(&fl->user, &cctx->users); 1154977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1155f6f9279fSSrinivas Kandagatla 1156f6f9279fSSrinivas Kandagatla return 0; 1157f6f9279fSSrinivas Kandagatla } 1158f6f9279fSSrinivas Kandagatla 11596cffd795SSrinivas Kandagatla static int fastrpc_dmabuf_free(struct fastrpc_user *fl, char __user *argp) 11606cffd795SSrinivas Kandagatla { 11616cffd795SSrinivas Kandagatla struct dma_buf *buf; 11626cffd795SSrinivas Kandagatla int info; 11636cffd795SSrinivas Kandagatla 11646cffd795SSrinivas Kandagatla if (copy_from_user(&info, argp, sizeof(info))) 11656cffd795SSrinivas Kandagatla return -EFAULT; 11666cffd795SSrinivas Kandagatla 11676cffd795SSrinivas Kandagatla buf = dma_buf_get(info); 11686cffd795SSrinivas Kandagatla if (IS_ERR_OR_NULL(buf)) 11696cffd795SSrinivas Kandagatla return -EINVAL; 11706cffd795SSrinivas Kandagatla /* 11716cffd795SSrinivas Kandagatla * one for the last get and other for the ALLOC_DMA_BUFF ioctl 11726cffd795SSrinivas Kandagatla */ 11736cffd795SSrinivas Kandagatla dma_buf_put(buf); 11746cffd795SSrinivas Kandagatla dma_buf_put(buf); 11756cffd795SSrinivas Kandagatla 11766cffd795SSrinivas Kandagatla return 0; 11776cffd795SSrinivas Kandagatla } 11786cffd795SSrinivas Kandagatla 11796cffd795SSrinivas Kandagatla static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp) 11806cffd795SSrinivas Kandagatla { 11816cffd795SSrinivas Kandagatla struct fastrpc_alloc_dma_buf bp; 11826cffd795SSrinivas Kandagatla DEFINE_DMA_BUF_EXPORT_INFO(exp_info); 11836cffd795SSrinivas Kandagatla struct fastrpc_buf *buf = NULL; 11846cffd795SSrinivas Kandagatla int err; 11856cffd795SSrinivas Kandagatla 11866cffd795SSrinivas Kandagatla if (copy_from_user(&bp, argp, sizeof(bp))) 11876cffd795SSrinivas Kandagatla return -EFAULT; 11886cffd795SSrinivas Kandagatla 11896cffd795SSrinivas Kandagatla err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf); 11906cffd795SSrinivas Kandagatla if (err) 11916cffd795SSrinivas Kandagatla return err; 11926cffd795SSrinivas Kandagatla exp_info.ops = &fastrpc_dma_buf_ops; 11936cffd795SSrinivas Kandagatla exp_info.size = bp.size; 11946cffd795SSrinivas Kandagatla exp_info.flags = O_RDWR; 11956cffd795SSrinivas Kandagatla exp_info.priv = buf; 11966cffd795SSrinivas Kandagatla buf->dmabuf = dma_buf_export(&exp_info); 11976cffd795SSrinivas Kandagatla if (IS_ERR(buf->dmabuf)) { 11986cffd795SSrinivas Kandagatla err = PTR_ERR(buf->dmabuf); 11996cffd795SSrinivas Kandagatla fastrpc_buf_free(buf); 12006cffd795SSrinivas Kandagatla return err; 12016cffd795SSrinivas Kandagatla } 12026cffd795SSrinivas Kandagatla 12036cffd795SSrinivas Kandagatla bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE); 12046cffd795SSrinivas Kandagatla if (bp.fd < 0) { 12056cffd795SSrinivas Kandagatla dma_buf_put(buf->dmabuf); 12066cffd795SSrinivas Kandagatla return -EINVAL; 12076cffd795SSrinivas Kandagatla } 12086cffd795SSrinivas Kandagatla 12096cffd795SSrinivas Kandagatla if (copy_to_user(argp, &bp, sizeof(bp))) { 12106cffd795SSrinivas Kandagatla dma_buf_put(buf->dmabuf); 12116cffd795SSrinivas Kandagatla return -EFAULT; 12126cffd795SSrinivas Kandagatla } 12136cffd795SSrinivas Kandagatla 12146cffd795SSrinivas Kandagatla get_dma_buf(buf->dmabuf); 12156cffd795SSrinivas Kandagatla 12166cffd795SSrinivas Kandagatla return 0; 12176cffd795SSrinivas Kandagatla } 12186cffd795SSrinivas Kandagatla 1219d73f71c7SSrinivas Kandagatla static int fastrpc_init_attach(struct fastrpc_user *fl) 1220d73f71c7SSrinivas Kandagatla { 1221d73f71c7SSrinivas Kandagatla struct fastrpc_invoke_args args[1]; 1222d73f71c7SSrinivas Kandagatla int tgid = fl->tgid; 1223d73f71c7SSrinivas Kandagatla u32 sc; 1224d73f71c7SSrinivas Kandagatla 1225d73f71c7SSrinivas Kandagatla args[0].ptr = (u64)(uintptr_t) &tgid; 1226d73f71c7SSrinivas Kandagatla args[0].length = sizeof(tgid); 1227d73f71c7SSrinivas Kandagatla args[0].fd = -1; 1228d73f71c7SSrinivas Kandagatla args[0].reserved = 0; 1229d73f71c7SSrinivas Kandagatla sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0); 1230d73f71c7SSrinivas Kandagatla fl->pd = 0; 1231d73f71c7SSrinivas Kandagatla 1232d73f71c7SSrinivas Kandagatla return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, 1233d73f71c7SSrinivas Kandagatla sc, &args[0]); 1234d73f71c7SSrinivas Kandagatla } 1235d73f71c7SSrinivas Kandagatla 1236c68cfb71SSrinivas Kandagatla static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp) 1237c68cfb71SSrinivas Kandagatla { 1238c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_args *args = NULL; 1239c68cfb71SSrinivas Kandagatla struct fastrpc_invoke inv; 1240c68cfb71SSrinivas Kandagatla u32 nscalars; 1241c68cfb71SSrinivas Kandagatla int err; 1242c68cfb71SSrinivas Kandagatla 1243c68cfb71SSrinivas Kandagatla if (copy_from_user(&inv, argp, sizeof(inv))) 1244c68cfb71SSrinivas Kandagatla return -EFAULT; 1245c68cfb71SSrinivas Kandagatla 1246c68cfb71SSrinivas Kandagatla /* nscalars is truncated here to max supported value */ 1247c68cfb71SSrinivas Kandagatla nscalars = REMOTE_SCALARS_LENGTH(inv.sc); 1248c68cfb71SSrinivas Kandagatla if (nscalars) { 1249c68cfb71SSrinivas Kandagatla args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL); 1250c68cfb71SSrinivas Kandagatla if (!args) 1251c68cfb71SSrinivas Kandagatla return -ENOMEM; 1252c68cfb71SSrinivas Kandagatla 1253c68cfb71SSrinivas Kandagatla if (copy_from_user(args, (void __user *)(uintptr_t)inv.args, 1254c68cfb71SSrinivas Kandagatla nscalars * sizeof(*args))) { 1255c68cfb71SSrinivas Kandagatla kfree(args); 1256c68cfb71SSrinivas Kandagatla return -EFAULT; 1257c68cfb71SSrinivas Kandagatla } 1258c68cfb71SSrinivas Kandagatla } 1259c68cfb71SSrinivas Kandagatla 1260c68cfb71SSrinivas Kandagatla err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args); 1261c68cfb71SSrinivas Kandagatla kfree(args); 1262c68cfb71SSrinivas Kandagatla 1263c68cfb71SSrinivas Kandagatla return err; 1264c68cfb71SSrinivas Kandagatla } 1265c68cfb71SSrinivas Kandagatla 1266c68cfb71SSrinivas Kandagatla static long fastrpc_device_ioctl(struct file *file, unsigned int cmd, 1267c68cfb71SSrinivas Kandagatla unsigned long arg) 1268c68cfb71SSrinivas Kandagatla { 1269c68cfb71SSrinivas Kandagatla struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data; 1270c68cfb71SSrinivas Kandagatla char __user *argp = (char __user *)arg; 1271c68cfb71SSrinivas Kandagatla int err; 1272c68cfb71SSrinivas Kandagatla 1273c68cfb71SSrinivas Kandagatla switch (cmd) { 1274c68cfb71SSrinivas Kandagatla case FASTRPC_IOCTL_INVOKE: 1275c68cfb71SSrinivas Kandagatla err = fastrpc_invoke(fl, argp); 1276c68cfb71SSrinivas Kandagatla break; 1277d73f71c7SSrinivas Kandagatla case FASTRPC_IOCTL_INIT_ATTACH: 1278d73f71c7SSrinivas Kandagatla err = fastrpc_init_attach(fl); 1279d73f71c7SSrinivas Kandagatla break; 1280d73f71c7SSrinivas Kandagatla case FASTRPC_IOCTL_INIT_CREATE: 1281d73f71c7SSrinivas Kandagatla err = fastrpc_init_create_process(fl, argp); 1282d73f71c7SSrinivas Kandagatla break; 12836cffd795SSrinivas Kandagatla case FASTRPC_IOCTL_FREE_DMA_BUFF: 12846cffd795SSrinivas Kandagatla err = fastrpc_dmabuf_free(fl, argp); 12856cffd795SSrinivas Kandagatla break; 12866cffd795SSrinivas Kandagatla case FASTRPC_IOCTL_ALLOC_DMA_BUFF: 12876cffd795SSrinivas Kandagatla err = fastrpc_dmabuf_alloc(fl, argp); 12886cffd795SSrinivas Kandagatla break; 1289c68cfb71SSrinivas Kandagatla default: 1290c68cfb71SSrinivas Kandagatla err = -ENOTTY; 1291c68cfb71SSrinivas Kandagatla break; 1292c68cfb71SSrinivas Kandagatla } 1293c68cfb71SSrinivas Kandagatla 1294c68cfb71SSrinivas Kandagatla return err; 1295c68cfb71SSrinivas Kandagatla } 1296c68cfb71SSrinivas Kandagatla 1297f6f9279fSSrinivas Kandagatla static const struct file_operations fastrpc_fops = { 1298f6f9279fSSrinivas Kandagatla .open = fastrpc_device_open, 1299f6f9279fSSrinivas Kandagatla .release = fastrpc_device_release, 1300c68cfb71SSrinivas Kandagatla .unlocked_ioctl = fastrpc_device_ioctl, 1301c68cfb71SSrinivas Kandagatla .compat_ioctl = fastrpc_device_ioctl, 1302f6f9279fSSrinivas Kandagatla }; 1303f6f9279fSSrinivas Kandagatla 1304f6f9279fSSrinivas Kandagatla static int fastrpc_cb_probe(struct platform_device *pdev) 1305f6f9279fSSrinivas Kandagatla { 1306f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx; 1307f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *sess; 1308f6f9279fSSrinivas Kandagatla struct device *dev = &pdev->dev; 1309f6f9279fSSrinivas Kandagatla int i, sessions = 0; 1310977e6c8dSSrinivas Kandagatla unsigned long flags; 1311f6f9279fSSrinivas Kandagatla 1312f6f9279fSSrinivas Kandagatla cctx = dev_get_drvdata(dev->parent); 1313f6f9279fSSrinivas Kandagatla if (!cctx) 1314f6f9279fSSrinivas Kandagatla return -EINVAL; 1315f6f9279fSSrinivas Kandagatla 1316f6f9279fSSrinivas Kandagatla of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions); 1317f6f9279fSSrinivas Kandagatla 1318977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1319f6f9279fSSrinivas Kandagatla sess = &cctx->session[cctx->sesscount]; 1320f6f9279fSSrinivas Kandagatla sess->used = false; 1321f6f9279fSSrinivas Kandagatla sess->valid = true; 1322f6f9279fSSrinivas Kandagatla sess->dev = dev; 1323f6f9279fSSrinivas Kandagatla dev_set_drvdata(dev, sess); 1324f6f9279fSSrinivas Kandagatla 1325f6f9279fSSrinivas Kandagatla if (of_property_read_u32(dev->of_node, "reg", &sess->sid)) 1326f6f9279fSSrinivas Kandagatla dev_info(dev, "FastRPC Session ID not specified in DT\n"); 1327f6f9279fSSrinivas Kandagatla 1328f6f9279fSSrinivas Kandagatla if (sessions > 0) { 1329f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *dup_sess; 1330f6f9279fSSrinivas Kandagatla 1331f6f9279fSSrinivas Kandagatla for (i = 1; i < sessions; i++) { 1332f6f9279fSSrinivas Kandagatla if (cctx->sesscount++ >= FASTRPC_MAX_SESSIONS) 1333f6f9279fSSrinivas Kandagatla break; 1334f6f9279fSSrinivas Kandagatla dup_sess = &cctx->session[cctx->sesscount]; 1335f6f9279fSSrinivas Kandagatla memcpy(dup_sess, sess, sizeof(*dup_sess)); 1336f6f9279fSSrinivas Kandagatla } 1337f6f9279fSSrinivas Kandagatla } 1338f6f9279fSSrinivas Kandagatla cctx->sesscount++; 1339977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1340f6f9279fSSrinivas Kandagatla dma_set_mask(dev, DMA_BIT_MASK(32)); 1341f6f9279fSSrinivas Kandagatla 1342f6f9279fSSrinivas Kandagatla return 0; 1343f6f9279fSSrinivas Kandagatla } 1344f6f9279fSSrinivas Kandagatla 1345f6f9279fSSrinivas Kandagatla static int fastrpc_cb_remove(struct platform_device *pdev) 1346f6f9279fSSrinivas Kandagatla { 1347f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent); 1348f6f9279fSSrinivas Kandagatla struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev); 1349977e6c8dSSrinivas Kandagatla unsigned long flags; 1350f6f9279fSSrinivas Kandagatla int i; 1351f6f9279fSSrinivas Kandagatla 1352977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1353f6f9279fSSrinivas Kandagatla for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) { 1354f6f9279fSSrinivas Kandagatla if (cctx->session[i].sid == sess->sid) { 1355f6f9279fSSrinivas Kandagatla cctx->session[i].valid = false; 1356f6f9279fSSrinivas Kandagatla cctx->sesscount--; 1357f6f9279fSSrinivas Kandagatla } 1358f6f9279fSSrinivas Kandagatla } 1359977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1360f6f9279fSSrinivas Kandagatla 1361f6f9279fSSrinivas Kandagatla return 0; 1362f6f9279fSSrinivas Kandagatla } 1363f6f9279fSSrinivas Kandagatla 1364f6f9279fSSrinivas Kandagatla static const struct of_device_id fastrpc_match_table[] = { 1365f6f9279fSSrinivas Kandagatla { .compatible = "qcom,fastrpc-compute-cb", }, 1366f6f9279fSSrinivas Kandagatla {} 1367f6f9279fSSrinivas Kandagatla }; 1368f6f9279fSSrinivas Kandagatla 1369f6f9279fSSrinivas Kandagatla static struct platform_driver fastrpc_cb_driver = { 1370f6f9279fSSrinivas Kandagatla .probe = fastrpc_cb_probe, 1371f6f9279fSSrinivas Kandagatla .remove = fastrpc_cb_remove, 1372f6f9279fSSrinivas Kandagatla .driver = { 1373f6f9279fSSrinivas Kandagatla .name = "qcom,fastrpc-cb", 1374f6f9279fSSrinivas Kandagatla .of_match_table = fastrpc_match_table, 1375f6f9279fSSrinivas Kandagatla .suppress_bind_attrs = true, 1376f6f9279fSSrinivas Kandagatla }, 1377f6f9279fSSrinivas Kandagatla }; 1378f6f9279fSSrinivas Kandagatla 1379f6f9279fSSrinivas Kandagatla static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) 1380f6f9279fSSrinivas Kandagatla { 1381f6f9279fSSrinivas Kandagatla struct device *rdev = &rpdev->dev; 1382f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *data; 1383f6f9279fSSrinivas Kandagatla int i, err, domain_id = -1; 1384f6f9279fSSrinivas Kandagatla const char *domain; 1385f6f9279fSSrinivas Kandagatla 1386f6f9279fSSrinivas Kandagatla data = devm_kzalloc(rdev, sizeof(*data), GFP_KERNEL); 1387f6f9279fSSrinivas Kandagatla if (!data) 1388f6f9279fSSrinivas Kandagatla return -ENOMEM; 1389f6f9279fSSrinivas Kandagatla 1390f6f9279fSSrinivas Kandagatla err = of_property_read_string(rdev->of_node, "label", &domain); 1391f6f9279fSSrinivas Kandagatla if (err) { 1392f6f9279fSSrinivas Kandagatla dev_info(rdev, "FastRPC Domain not specified in DT\n"); 1393f6f9279fSSrinivas Kandagatla return err; 1394f6f9279fSSrinivas Kandagatla } 1395f6f9279fSSrinivas Kandagatla 1396f6f9279fSSrinivas Kandagatla for (i = 0; i <= CDSP_DOMAIN_ID; i++) { 1397f6f9279fSSrinivas Kandagatla if (!strcmp(domains[i], domain)) { 1398f6f9279fSSrinivas Kandagatla domain_id = i; 1399f6f9279fSSrinivas Kandagatla break; 1400f6f9279fSSrinivas Kandagatla } 1401f6f9279fSSrinivas Kandagatla } 1402f6f9279fSSrinivas Kandagatla 1403f6f9279fSSrinivas Kandagatla if (domain_id < 0) { 1404f6f9279fSSrinivas Kandagatla dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id); 1405f6f9279fSSrinivas Kandagatla return -EINVAL; 1406f6f9279fSSrinivas Kandagatla } 1407f6f9279fSSrinivas Kandagatla 1408f6f9279fSSrinivas Kandagatla data->miscdev.minor = MISC_DYNAMIC_MINOR; 1409f6f9279fSSrinivas Kandagatla data->miscdev.name = kasprintf(GFP_KERNEL, "fastrpc-%s", 1410f6f9279fSSrinivas Kandagatla domains[domain_id]); 1411f6f9279fSSrinivas Kandagatla data->miscdev.fops = &fastrpc_fops; 1412f6f9279fSSrinivas Kandagatla err = misc_register(&data->miscdev); 1413f6f9279fSSrinivas Kandagatla if (err) 1414f6f9279fSSrinivas Kandagatla return err; 1415f6f9279fSSrinivas Kandagatla 1416f6f9279fSSrinivas Kandagatla dev_set_drvdata(&rpdev->dev, data); 1417f6f9279fSSrinivas Kandagatla dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32)); 1418f6f9279fSSrinivas Kandagatla INIT_LIST_HEAD(&data->users); 1419f6f9279fSSrinivas Kandagatla spin_lock_init(&data->lock); 1420f6f9279fSSrinivas Kandagatla idr_init(&data->ctx_idr); 1421f6f9279fSSrinivas Kandagatla data->domain_id = domain_id; 1422f6f9279fSSrinivas Kandagatla data->rpdev = rpdev; 1423f6f9279fSSrinivas Kandagatla 1424f6f9279fSSrinivas Kandagatla return of_platform_populate(rdev->of_node, NULL, NULL, rdev); 1425f6f9279fSSrinivas Kandagatla } 1426f6f9279fSSrinivas Kandagatla 1427c68cfb71SSrinivas Kandagatla static void fastrpc_notify_users(struct fastrpc_user *user) 1428c68cfb71SSrinivas Kandagatla { 1429c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx; 1430c68cfb71SSrinivas Kandagatla 1431c68cfb71SSrinivas Kandagatla spin_lock(&user->lock); 1432c68cfb71SSrinivas Kandagatla list_for_each_entry(ctx, &user->pending, node) 1433c68cfb71SSrinivas Kandagatla complete(&ctx->work); 1434c68cfb71SSrinivas Kandagatla spin_unlock(&user->lock); 1435c68cfb71SSrinivas Kandagatla } 1436c68cfb71SSrinivas Kandagatla 1437f6f9279fSSrinivas Kandagatla static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev) 1438f6f9279fSSrinivas Kandagatla { 1439f6f9279fSSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev); 1440c68cfb71SSrinivas Kandagatla struct fastrpc_user *user; 1441977e6c8dSSrinivas Kandagatla unsigned long flags; 1442c68cfb71SSrinivas Kandagatla 1443977e6c8dSSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1444c68cfb71SSrinivas Kandagatla list_for_each_entry(user, &cctx->users, user) 1445c68cfb71SSrinivas Kandagatla fastrpc_notify_users(user); 1446977e6c8dSSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1447f6f9279fSSrinivas Kandagatla 1448f6f9279fSSrinivas Kandagatla misc_deregister(&cctx->miscdev); 1449f6f9279fSSrinivas Kandagatla of_platform_depopulate(&rpdev->dev); 1450f6f9279fSSrinivas Kandagatla kfree(cctx); 1451f6f9279fSSrinivas Kandagatla } 1452f6f9279fSSrinivas Kandagatla 1453f6f9279fSSrinivas Kandagatla static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data, 1454f6f9279fSSrinivas Kandagatla int len, void *priv, u32 addr) 1455f6f9279fSSrinivas Kandagatla { 1456c68cfb71SSrinivas Kandagatla struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev); 1457c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_rsp *rsp = data; 1458c68cfb71SSrinivas Kandagatla struct fastrpc_invoke_ctx *ctx; 1459c68cfb71SSrinivas Kandagatla unsigned long flags; 1460c68cfb71SSrinivas Kandagatla unsigned long ctxid; 1461c68cfb71SSrinivas Kandagatla 1462c68cfb71SSrinivas Kandagatla if (len < sizeof(*rsp)) 1463c68cfb71SSrinivas Kandagatla return -EINVAL; 1464c68cfb71SSrinivas Kandagatla 1465c68cfb71SSrinivas Kandagatla ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4); 1466c68cfb71SSrinivas Kandagatla 1467c68cfb71SSrinivas Kandagatla spin_lock_irqsave(&cctx->lock, flags); 1468c68cfb71SSrinivas Kandagatla ctx = idr_find(&cctx->ctx_idr, ctxid); 1469c68cfb71SSrinivas Kandagatla spin_unlock_irqrestore(&cctx->lock, flags); 1470c68cfb71SSrinivas Kandagatla 1471c68cfb71SSrinivas Kandagatla if (!ctx) { 1472c68cfb71SSrinivas Kandagatla dev_err(&rpdev->dev, "No context ID matches response\n"); 1473c68cfb71SSrinivas Kandagatla return -ENOENT; 1474c68cfb71SSrinivas Kandagatla } 1475c68cfb71SSrinivas Kandagatla 1476c68cfb71SSrinivas Kandagatla ctx->retval = rsp->retval; 1477c68cfb71SSrinivas Kandagatla complete(&ctx->work); 14788e7389c7SThierry Escande 14798e7389c7SThierry Escande /* 14808e7389c7SThierry Escande * The DMA buffer associated with the context cannot be freed in 14818e7389c7SThierry Escande * interrupt context so schedule it through a worker thread to 14828e7389c7SThierry Escande * avoid a kernel BUG. 14838e7389c7SThierry Escande */ 14848e7389c7SThierry Escande schedule_work(&ctx->put_work); 1485c68cfb71SSrinivas Kandagatla 1486f6f9279fSSrinivas Kandagatla return 0; 1487f6f9279fSSrinivas Kandagatla } 1488f6f9279fSSrinivas Kandagatla 1489f6f9279fSSrinivas Kandagatla static const struct of_device_id fastrpc_rpmsg_of_match[] = { 1490f6f9279fSSrinivas Kandagatla { .compatible = "qcom,fastrpc" }, 1491f6f9279fSSrinivas Kandagatla { }, 1492f6f9279fSSrinivas Kandagatla }; 1493f6f9279fSSrinivas Kandagatla MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match); 1494f6f9279fSSrinivas Kandagatla 1495f6f9279fSSrinivas Kandagatla static struct rpmsg_driver fastrpc_driver = { 1496f6f9279fSSrinivas Kandagatla .probe = fastrpc_rpmsg_probe, 1497f6f9279fSSrinivas Kandagatla .remove = fastrpc_rpmsg_remove, 1498f6f9279fSSrinivas Kandagatla .callback = fastrpc_rpmsg_callback, 1499f6f9279fSSrinivas Kandagatla .drv = { 1500f6f9279fSSrinivas Kandagatla .name = "qcom,fastrpc", 1501f6f9279fSSrinivas Kandagatla .of_match_table = fastrpc_rpmsg_of_match, 1502f6f9279fSSrinivas Kandagatla }, 1503f6f9279fSSrinivas Kandagatla }; 1504f6f9279fSSrinivas Kandagatla 1505f6f9279fSSrinivas Kandagatla static int fastrpc_init(void) 1506f6f9279fSSrinivas Kandagatla { 1507f6f9279fSSrinivas Kandagatla int ret; 1508f6f9279fSSrinivas Kandagatla 1509f6f9279fSSrinivas Kandagatla ret = platform_driver_register(&fastrpc_cb_driver); 1510f6f9279fSSrinivas Kandagatla if (ret < 0) { 1511f6f9279fSSrinivas Kandagatla pr_err("fastrpc: failed to register cb driver\n"); 1512f6f9279fSSrinivas Kandagatla return ret; 1513f6f9279fSSrinivas Kandagatla } 1514f6f9279fSSrinivas Kandagatla 1515f6f9279fSSrinivas Kandagatla ret = register_rpmsg_driver(&fastrpc_driver); 1516f6f9279fSSrinivas Kandagatla if (ret < 0) { 1517f6f9279fSSrinivas Kandagatla pr_err("fastrpc: failed to register rpmsg driver\n"); 1518f6f9279fSSrinivas Kandagatla platform_driver_unregister(&fastrpc_cb_driver); 1519f6f9279fSSrinivas Kandagatla return ret; 1520f6f9279fSSrinivas Kandagatla } 1521f6f9279fSSrinivas Kandagatla 1522f6f9279fSSrinivas Kandagatla return 0; 1523f6f9279fSSrinivas Kandagatla } 1524f6f9279fSSrinivas Kandagatla module_init(fastrpc_init); 1525f6f9279fSSrinivas Kandagatla 1526f6f9279fSSrinivas Kandagatla static void fastrpc_exit(void) 1527f6f9279fSSrinivas Kandagatla { 1528f6f9279fSSrinivas Kandagatla platform_driver_unregister(&fastrpc_cb_driver); 1529f6f9279fSSrinivas Kandagatla unregister_rpmsg_driver(&fastrpc_driver); 1530f6f9279fSSrinivas Kandagatla } 1531f6f9279fSSrinivas Kandagatla module_exit(fastrpc_exit); 1532f6f9279fSSrinivas Kandagatla 1533f6f9279fSSrinivas Kandagatla MODULE_LICENSE("GPL v2"); 1534