1 /* 2 * Copyright (C) 2016 Google, Inc. 3 * 4 * This software is licensed under the terms of the GNU General Public 5 * License version 2, as published by the Free Software Foundation, and 6 * may be copied, distributed, and modified under those terms. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 */ 14 15 #ifndef GOLDFISH_DMA_H 16 #define GOLDFISH_DMA_H 17 18 #include <linux/dma-mapping.h> 19 #include <linux/platform_device.h> 20 21 /* GOLDFISH DMA 22 * 23 * Goldfish DMA is an extension to the pipe device 24 * and is designed to facilitate high-speed RAM->RAM 25 * transfers from guest to host. 26 * 27 * Interface (guest side): 28 * 29 * The guest user calls goldfish_dma_alloc (ioctls) 30 * and then mmap() on a goldfish pipe fd, 31 * which means that it wants high-speed access to 32 * host-visible memory. 33 * 34 * The guest can then write into the pointer 35 * returned by mmap(), and these writes 36 * become immediately visible on the host without BQL 37 * or otherweise context switching. 38 * 39 * The main data structure tracking state is 40 * struct goldfish_dma_context, which is included 41 * as an extra pointer field in struct goldfish_pipe. 42 * Each such context is associated with possibly 43 * one physical address and size describing the 44 * allocated DMA region, and only one allocation 45 * is allowed for each pipe fd. Further allocations 46 * require more open()'s of pipe fd's. 47 * 48 * dma_alloc_coherent() is used to obtain contiguous 49 * physical memory regions, and we allocate and interact 50 * with this region on both guest and host through 51 * the following ioctls: 52 * 53 * - LOCK: lock the region for data access. 54 * - UNLOCK: unlock the region. This may also be done from the host 55 * through the WAKE_ON_UNLOCK_DMA procedure. 56 * - CREATE_REGION: initialize size info for a dma region. 57 * - GETOFF: send physical address to guest drivers. 58 * - (UN)MAPHOST: uses goldfish_pipe_cmd to tell the host to 59 * (un)map to the guest physical address associated 60 * with the current dma context. This makes the physically 61 * contiguous memory (in)visible to the host. 62 * 63 * Guest userspace obtains a pointer to the DMA memory 64 * through mmap(), which also lazily allocates the memory 65 * with dma_alloc_coherent. (On last pipe close(), the region is freed). 66 * The mmaped() region can handle very high bandwidth 67 * transfers, and pipe operations can be used at the same 68 * time to handle synchronization and command communication. 69 * 70 * This is the kernel header defining Goldfish DMA state. 71 * It consists mainly of a physical contiguous memory 72 * along with a mutex to coordinate access. 73 * 74 * The ioctls() and mmap() are implemented as ioctl 75 * and mmap handlers in the goldfish_pipe_v2.c. 76 */ 77 78 #define GOLDFISH_DMA_BUFFER_SIZE 32 * 1024 * 1024 79 80 struct goldfish_pipe_command; 81 82 struct goldfish_dma_context { 83 struct device *pdev_dev; /* pointer to feed to dma_***_coherent */ 84 void *dma_vaddr; /* kernel vaddr of dma region */ 85 u64 dma_size; /* size of dma region */ 86 u64 phys_begin; /* paddr of dma region */ 87 unsigned long pfn; /* pfn of dma region */ 88 u64 phys_end; /* paddr of dma region + dma_size */ 89 bool locked; /* marks whether the region is currently in use (unrelated to |mutex_lock|)*/ 90 /* |mutex_lock| synchronizes access to a DMA context. 91 * It protects the entire goldfish dma surface area: 92 * ioctl, mmap, and munmap.*/ 93 struct mutex mutex_lock; 94 }; 95 96 struct goldfish_dma_ioctl_info { 97 uint64_t phys_begin; 98 uint64_t size; 99 }; 100 101 /* There is an ioctl associated with goldfish dma driver. 102 * Make it conflict with ioctls that are not likely to be used 103 * in the emulator. 104 * 'G' 00-3F drivers/misc/sgi-gru/grulib.h conflict! 105 * 'G' 00-0F linux/gigaset_dev.h conflict! 106 */ 107 #define GOLDFISH_DMA_IOC_MAGIC 'G' 108 109 #define GOLDFISH_DMA_IOC_LOCK _IOWR(GOLDFISH_DMA_IOC_MAGIC, 0, struct goldfish_dma_ioctl_info) 110 #define GOLDFISH_DMA_IOC_UNLOCK _IOWR(GOLDFISH_DMA_IOC_MAGIC, 1, struct goldfish_dma_ioctl_info) 111 #define GOLDFISH_DMA_IOC_GETOFF _IOWR(GOLDFISH_DMA_IOC_MAGIC, 2, struct goldfish_dma_ioctl_info) 112 #define GOLDFISH_DMA_IOC_CREATE_REGION _IOWR(GOLDFISH_DMA_IOC_MAGIC, 3, struct goldfish_dma_ioctl_info) 113 114 #endif /* GOLDFISH_DMA_H */ 115