| 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_DEVICE_FEATURE = 16, |
| 44 | VFIO_USER_MAX, |
| 45 | }; |
| 46 | |
| 47 | /* VFIOUserHdr flags */ |
| 48 | #define VFIO_USER_REQUEST 0x0 |
| 49 | #define VFIO_USER_REPLY 0x1 |
| 50 | #define VFIO_USER_TYPE 0xF |
| 51 | |
| 52 | #define VFIO_USER_NO_REPLY 0x10 |
| 53 | #define VFIO_USER_ERROR 0x20 |
| 54 | |
| 55 | |
| 56 | /* |
| 57 | * VFIO_USER_VERSION |
| 58 | */ |
| 59 | typedef struct { |
| 60 | VFIOUserHdr hdr; |
| 61 | uint16_t major; |
| 62 | uint16_t minor; |
| 63 | char capabilities[]; |
| 64 | } VFIOUserVersion; |
| 65 | |
| 66 | #define VFIO_USER_MAJOR_VER 0 |
| 67 | #define VFIO_USER_MINOR_VER 0 |
| 68 | |
| 69 | #define VFIO_USER_CAP "capabilities" |
| 70 | |
| 71 | /* "capabilities" members */ |
| 72 | #define VFIO_USER_CAP_MAX_FDS "max_msg_fds" |
| 73 | #define VFIO_USER_CAP_MAX_XFER "max_data_xfer_size" |
| 74 | #define VFIO_USER_CAP_PGSIZES "pgsizes" |
| 75 | #define VFIO_USER_CAP_MAP_MAX "max_dma_maps" |
| 76 | #define VFIO_USER_CAP_MIGR "migration" |
| 77 | #define VFIO_USER_CAP_MULTI "write_multiple" |
| 78 | |
| 79 | /* "migration" members */ |
| 80 | #define VFIO_USER_CAP_PGSIZE "pgsize" |
| 81 | #define VFIO_USER_CAP_MAX_BITMAP "max_bitmap_size" |
| 82 | |
| 83 | /* |
| 84 | * Max FDs mainly comes into play when a device supports multiple interrupts |
| 85 | * where each ones uses an eventfd to inject it into the guest. |
| 86 | * It is clamped by the the number of FDs the qio channel supports in a |
| 87 | * single message. |
| 88 | */ |
| 89 | #define VFIO_USER_DEF_MAX_FDS 8 |
| 90 | #define VFIO_USER_MAX_MAX_FDS 16 |
| 91 | |
| 92 | /* |
| 93 | * Max transfer limits the amount of data in region and DMA messages. |
| 94 | * Region R/W will be very small (limited by how much a single instruction |
| 95 | * can process) so just use a reasonable limit here. |
| 96 | */ |
| 97 | #define VFIO_USER_DEF_MAX_XFER (1024 * 1024) |
| 98 | #define VFIO_USER_MAX_MAX_XFER (64 * 1024 * 1024) |
| 99 | |
| 100 | /* |
| 101 | * Default pagesizes supported is 4k. |
| 102 | */ |
| 103 | #define VFIO_USER_DEF_PGSIZE 4096 |
| 104 | |
| 105 | /* |
| 106 | * Default max number of DMA mappings is stolen from the |
| 107 | * linux kernel "dma_entry_limit" |
| 108 | */ |
| 109 | #define VFIO_USER_DEF_MAP_MAX 65535 |
| 110 | |
| 111 | /* |
| 112 | * Default max bitmap size is also take from the linux kernel, |
| 113 | * where usage of signed ints limits the VA range to 2^31 bytes. |
| 114 | * Dividing that by the number of bits per byte yields 256MB |
| 115 | */ |
| 116 | #define VFIO_USER_DEF_MAX_BITMAP (256 * 1024 * 1024) |
| 117 | |
| 118 | /* |
| 119 | * VFIO_USER_DMA_MAP |
| 120 | * imported from struct vfio_iommu_type1_dma_map |
| 121 | */ |
| 122 | typedef struct { |
| 123 | VFIOUserHdr hdr; |
| 124 | uint32_t argsz; |
| 125 | uint32_t flags; |
| 126 | uint64_t offset; /* FD offset */ |
| 127 | uint64_t iova; |
| 128 | uint64_t size; |
| 129 | } VFIOUserDMAMap; |
| 130 | |
| 131 | /* |
| 132 | * VFIO_USER_DMA_UNMAP |
| 133 | * imported from struct vfio_iommu_type1_dma_unmap |
| 134 | */ |
| 135 | typedef struct { |
| 136 | VFIOUserHdr hdr; |
| 137 | uint32_t argsz; |
| 138 | uint32_t flags; |
| 139 | uint64_t iova; |
| 140 | uint64_t size; |
| 141 | } VFIOUserDMAUnmap; |
| 142 | |
| 143 | /* |
| 144 | * VFIO_USER_DEVICE_GET_INFO |
| 145 | * imported from struct vfio_device_info |
| 146 | */ |
| 147 | typedef struct { |
| 148 | VFIOUserHdr hdr; |
| 149 | uint32_t argsz; |
| 150 | uint32_t flags; |
| 151 | uint32_t num_regions; |
| 152 | uint32_t num_irqs; |
| 153 | } VFIOUserDeviceInfo; |
| 154 | |
| 155 | /* |
| 156 | * VFIO_USER_DEVICE_GET_REGION_INFO |
| 157 | * imported from struct vfio_region_info |
| 158 | */ |
| 159 | typedef struct { |
| 160 | VFIOUserHdr hdr; |
| 161 | uint32_t argsz; |
| 162 | uint32_t flags; |
| 163 | uint32_t index; |
| 164 | uint32_t cap_offset; |
| 165 | uint64_t size; |
| 166 | uint64_t offset; |
| 167 | } VFIOUserRegionInfo; |
| 168 | |
| 169 | /* |
| 170 | * VFIO_USER_DEVICE_GET_IRQ_INFO |
| 171 | * imported from struct vfio_irq_info |
| 172 | */ |
| 173 | typedef struct { |
| 174 | VFIOUserHdr hdr; |
| 175 | uint32_t argsz; |
| 176 | uint32_t flags; |
| 177 | uint32_t index; |
| 178 | uint32_t count; |
| 179 | } VFIOUserIRQInfo; |
| 180 | |
| 181 | /* |
| 182 | * VFIO_USER_DEVICE_SET_IRQS |
| 183 | * imported from struct vfio_irq_set |
| 184 | */ |
| 185 | typedef struct { |
| 186 | VFIOUserHdr hdr; |
| 187 | uint32_t argsz; |
| 188 | uint32_t flags; |
| 189 | uint32_t index; |
| 190 | uint32_t start; |
| 191 | uint32_t count; |
| 192 | } VFIOUserIRQSet; |
| 193 | |
| 194 | /* |
| 195 | * VFIO_USER_REGION_READ |
| 196 | * VFIO_USER_REGION_WRITE |
| 197 | */ |
| 198 | typedef struct { |
| 199 | VFIOUserHdr hdr; |
| 200 | uint64_t offset; |
| 201 | uint32_t region; |
| 202 | uint32_t count; |
| 203 | char data[]; |
| 204 | } VFIOUserRegionRW; |
| 205 | |
| 206 | /* |
| 207 | * VFIO_USER_DMA_READ |
| 208 | * VFIO_USER_DMA_WRITE |
| 209 | */ |
| 210 | typedef struct { |
| 211 | VFIOUserHdr hdr; |
| 212 | uint64_t offset; |
| 213 | uint64_t count; |
| 214 | char data[]; |
| 215 | } VFIOUserDMARW; |
| 216 | |
| 217 | /* imported from struct vfio_bitmap */ |
| 218 | typedef struct { |
| 219 | uint64_t pgsize; |
| 220 | uint64_t size; |
| 221 | char data[]; |
| 222 | } VFIOUserBitmap; |
| 223 | |
| 224 | /* |
| 225 | * VFIO_USER_REGION_WRITE_MULTI |
| 226 | */ |
| 227 | #define VFIO_USER_MULTI_DATA 8 |
| 228 | #define VFIO_USER_MULTI_MAX 200 |
| 229 | |
| 230 | typedef struct { |
| 231 | uint64_t offset; |
| 232 | uint32_t region; |
| 233 | uint32_t count; |
| 234 | char data[VFIO_USER_MULTI_DATA]; |
| 235 | } VFIOUserWROne; |
| 236 | |
| 237 | typedef struct { |
| 238 | VFIOUserHdr hdr; |
| 239 | uint64_t wr_cnt; |
| 240 | VFIOUserWROne wrs[VFIO_USER_MULTI_MAX]; |
| 241 | } VFIOUserWRMulti; |
| 242 | |
| 243 | /* |
| 244 | * VFIO_USER_DEVICE_FEATURE |
| 245 | * imported from struct vfio_device_feature |
| 246 | */ |
| 247 | typedef struct { |
| 248 | VFIOUserHdr hdr; |
| 249 | uint32_t argsz; |
| 250 | uint32_t flags; |
| 251 | char data[]; |
| 252 | } VFIOUserDeviceFeature; |
| 253 | |
| 254 | #endif /* VFIO_USER_PROTOCOL_H */ |