1 #ifndef VFIO_USER_PROTOCOL_H 2 #define VFIO_USER_PROTOCOL_H 3 4 /* 5 * vfio protocol over a UNIX socket. 6 * 7 * Copyright © 2018, 2021 Oracle and/or its affiliates. 8 * 9 * Each message has a standard header that describes the command 10 * being sent, which is almost always a VFIO ioctl(). 11 * 12 * The header may be followed by command-specific data, such as the 13 * region and offset info for read and write commands. 14 * 15 * SPDX-License-Identifier: GPL-2.0-or-later 16 */ 17 18 typedef struct { 19 uint16_t id; 20 uint16_t command; 21 uint32_t size; 22 uint32_t flags; 23 uint32_t error_reply; 24 } VFIOUserHdr; 25 26 /* VFIOUserHdr commands */ 27 enum vfio_user_command { 28 VFIO_USER_VERSION = 1, 29 VFIO_USER_DMA_MAP = 2, 30 VFIO_USER_DMA_UNMAP = 3, 31 VFIO_USER_DEVICE_GET_INFO = 4, 32 VFIO_USER_DEVICE_GET_REGION_INFO = 5, 33 VFIO_USER_DEVICE_GET_REGION_IO_FDS = 6, 34 VFIO_USER_DEVICE_GET_IRQ_INFO = 7, 35 VFIO_USER_DEVICE_SET_IRQS = 8, 36 VFIO_USER_REGION_READ = 9, 37 VFIO_USER_REGION_WRITE = 10, 38 VFIO_USER_DMA_READ = 11, 39 VFIO_USER_DMA_WRITE = 12, 40 VFIO_USER_DEVICE_RESET = 13, 41 VFIO_USER_DIRTY_PAGES = 14, 42 VFIO_USER_REGION_WRITE_MULTI = 15, 43 VFIO_USER_MAX, 44 }; 45 46 /* VFIOUserHdr flags */ 47 #define VFIO_USER_REQUEST 0x0 48 #define VFIO_USER_REPLY 0x1 49 #define VFIO_USER_TYPE 0xF 50 51 #define VFIO_USER_NO_REPLY 0x10 52 #define VFIO_USER_ERROR 0x20 53 54 55 /* 56 * VFIO_USER_VERSION 57 */ 58 typedef struct { 59 VFIOUserHdr hdr; 60 uint16_t major; 61 uint16_t minor; 62 char capabilities[]; 63 } VFIOUserVersion; 64 65 #define VFIO_USER_MAJOR_VER 0 66 #define VFIO_USER_MINOR_VER 0 67 68 #define VFIO_USER_CAP "capabilities" 69 70 /* "capabilities" members */ 71 #define VFIO_USER_CAP_MAX_FDS "max_msg_fds" 72 #define VFIO_USER_CAP_MAX_XFER "max_data_xfer_size" 73 #define VFIO_USER_CAP_PGSIZES "pgsizes" 74 #define VFIO_USER_CAP_MAP_MAX "max_dma_maps" 75 #define VFIO_USER_CAP_MIGR "migration" 76 #define VFIO_USER_CAP_MULTI "write_multiple" 77 78 /* "migration" members */ 79 #define VFIO_USER_CAP_PGSIZE "pgsize" 80 #define VFIO_USER_CAP_MAX_BITMAP "max_bitmap_size" 81 82 /* 83 * Max FDs mainly comes into play when a device supports multiple interrupts 84 * where each ones uses an eventfd to inject it into the guest. 85 * It is clamped by the the number of FDs the qio channel supports in a 86 * single message. 87 */ 88 #define VFIO_USER_DEF_MAX_FDS 8 89 #define VFIO_USER_MAX_MAX_FDS 16 90 91 /* 92 * Max transfer limits the amount of data in region and DMA messages. 93 * Region R/W will be very small (limited by how much a single instruction 94 * can process) so just use a reasonable limit here. 95 */ 96 #define VFIO_USER_DEF_MAX_XFER (1024 * 1024) 97 #define VFIO_USER_MAX_MAX_XFER (64 * 1024 * 1024) 98 99 /* 100 * Default pagesizes supported is 4k. 101 */ 102 #define VFIO_USER_DEF_PGSIZE 4096 103 104 /* 105 * Default max number of DMA mappings is stolen from the 106 * linux kernel "dma_entry_limit" 107 */ 108 #define VFIO_USER_DEF_MAP_MAX 65535 109 110 /* 111 * Default max bitmap size is also take from the linux kernel, 112 * where usage of signed ints limits the VA range to 2^31 bytes. 113 * Dividing that by the number of bits per byte yields 256MB 114 */ 115 #define VFIO_USER_DEF_MAX_BITMAP (256 * 1024 * 1024) 116 117 /* 118 * VFIO_USER_DMA_MAP 119 * imported from struct vfio_iommu_type1_dma_map 120 */ 121 typedef struct { 122 VFIOUserHdr hdr; 123 uint32_t argsz; 124 uint32_t flags; 125 uint64_t offset; /* FD offset */ 126 uint64_t iova; 127 uint64_t size; 128 } VFIOUserDMAMap; 129 130 /* 131 * VFIO_USER_DMA_UNMAP 132 * imported from struct vfio_iommu_type1_dma_unmap 133 */ 134 typedef struct { 135 VFIOUserHdr hdr; 136 uint32_t argsz; 137 uint32_t flags; 138 uint64_t iova; 139 uint64_t size; 140 } VFIOUserDMAUnmap; 141 142 /* 143 * VFIO_USER_DEVICE_GET_INFO 144 * imported from struct vfio_device_info 145 */ 146 typedef struct { 147 VFIOUserHdr hdr; 148 uint32_t argsz; 149 uint32_t flags; 150 uint32_t num_regions; 151 uint32_t num_irqs; 152 } VFIOUserDeviceInfo; 153 154 /* 155 * VFIO_USER_DEVICE_GET_REGION_INFO 156 * imported from struct vfio_region_info 157 */ 158 typedef struct { 159 VFIOUserHdr hdr; 160 uint32_t argsz; 161 uint32_t flags; 162 uint32_t index; 163 uint32_t cap_offset; 164 uint64_t size; 165 uint64_t offset; 166 } VFIOUserRegionInfo; 167 168 /* 169 * VFIO_USER_DEVICE_GET_IRQ_INFO 170 * imported from struct vfio_irq_info 171 */ 172 typedef struct { 173 VFIOUserHdr hdr; 174 uint32_t argsz; 175 uint32_t flags; 176 uint32_t index; 177 uint32_t count; 178 } VFIOUserIRQInfo; 179 180 /* 181 * VFIO_USER_DEVICE_SET_IRQS 182 * imported from struct vfio_irq_set 183 */ 184 typedef struct { 185 VFIOUserHdr hdr; 186 uint32_t argsz; 187 uint32_t flags; 188 uint32_t index; 189 uint32_t start; 190 uint32_t count; 191 } VFIOUserIRQSet; 192 193 /* 194 * VFIO_USER_REGION_READ 195 * VFIO_USER_REGION_WRITE 196 */ 197 typedef struct { 198 VFIOUserHdr hdr; 199 uint64_t offset; 200 uint32_t region; 201 uint32_t count; 202 char data[]; 203 } VFIOUserRegionRW; 204 205 /* 206 * VFIO_USER_DMA_READ 207 * VFIO_USER_DMA_WRITE 208 */ 209 typedef struct { 210 VFIOUserHdr hdr; 211 uint64_t offset; 212 uint32_t count; 213 char data[]; 214 } VFIOUserDMARW; 215 216 /* imported from struct vfio_bitmap */ 217 typedef struct { 218 uint64_t pgsize; 219 uint64_t size; 220 char data[]; 221 } VFIOUserBitmap; 222 223 /* 224 * VFIO_USER_REGION_WRITE_MULTI 225 */ 226 #define VFIO_USER_MULTI_DATA 8 227 #define VFIO_USER_MULTI_MAX 200 228 229 typedef struct { 230 uint64_t offset; 231 uint32_t region; 232 uint32_t count; 233 char data[VFIO_USER_MULTI_DATA]; 234 } VFIOUserWROne; 235 236 typedef struct { 237 VFIOUserHdr hdr; 238 uint64_t wr_cnt; 239 VFIOUserWROne wrs[VFIO_USER_MULTI_MAX]; 240 } VFIOUserWRMulti; 241 242 #endif /* VFIO_USER_PROTOCOL_H */ 243