xref: /qemu/hw/vfio-user/protocol.h (revision aec6836c73403cffa56b9a4c5556451ee16071fe)
10b3d881aSJohn Levon #ifndef VFIO_USER_PROTOCOL_H
20b3d881aSJohn Levon #define VFIO_USER_PROTOCOL_H
30b3d881aSJohn Levon 
40b3d881aSJohn Levon /*
50b3d881aSJohn Levon  * vfio protocol over a UNIX socket.
60b3d881aSJohn Levon  *
70b3d881aSJohn Levon  * Copyright © 2018, 2021 Oracle and/or its affiliates.
80b3d881aSJohn Levon  *
90b3d881aSJohn Levon  * Each message has a standard header that describes the command
100b3d881aSJohn Levon  * being sent, which is almost always a VFIO ioctl().
110b3d881aSJohn Levon  *
120b3d881aSJohn Levon  * The header may be followed by command-specific data, such as the
130b3d881aSJohn Levon  * region and offset info for read and write commands.
140b3d881aSJohn Levon  *
150b3d881aSJohn Levon  * SPDX-License-Identifier: GPL-2.0-or-later
160b3d881aSJohn Levon  */
170b3d881aSJohn Levon 
180b3d881aSJohn Levon typedef struct {
190b3d881aSJohn Levon     uint16_t id;
200b3d881aSJohn Levon     uint16_t command;
210b3d881aSJohn Levon     uint32_t size;
220b3d881aSJohn Levon     uint32_t flags;
230b3d881aSJohn Levon     uint32_t error_reply;
240b3d881aSJohn Levon } VFIOUserHdr;
250b3d881aSJohn Levon 
260b3d881aSJohn Levon /* VFIOUserHdr commands */
270b3d881aSJohn Levon enum vfio_user_command {
280b3d881aSJohn Levon     VFIO_USER_VERSION                   = 1,
290b3d881aSJohn Levon     VFIO_USER_DMA_MAP                   = 2,
300b3d881aSJohn Levon     VFIO_USER_DMA_UNMAP                 = 3,
310b3d881aSJohn Levon     VFIO_USER_DEVICE_GET_INFO           = 4,
320b3d881aSJohn Levon     VFIO_USER_DEVICE_GET_REGION_INFO    = 5,
330b3d881aSJohn Levon     VFIO_USER_DEVICE_GET_REGION_IO_FDS  = 6,
340b3d881aSJohn Levon     VFIO_USER_DEVICE_GET_IRQ_INFO       = 7,
350b3d881aSJohn Levon     VFIO_USER_DEVICE_SET_IRQS           = 8,
360b3d881aSJohn Levon     VFIO_USER_REGION_READ               = 9,
370b3d881aSJohn Levon     VFIO_USER_REGION_WRITE              = 10,
380b3d881aSJohn Levon     VFIO_USER_DMA_READ                  = 11,
390b3d881aSJohn Levon     VFIO_USER_DMA_WRITE                 = 12,
400b3d881aSJohn Levon     VFIO_USER_DEVICE_RESET              = 13,
410b3d881aSJohn Levon     VFIO_USER_DIRTY_PAGES               = 14,
42*1a0c32a9SJohn Levon     VFIO_USER_REGION_WRITE_MULTI        = 15,
430b3d881aSJohn Levon     VFIO_USER_MAX,
440b3d881aSJohn Levon };
450b3d881aSJohn Levon 
460b3d881aSJohn Levon /* VFIOUserHdr flags */
470b3d881aSJohn Levon #define VFIO_USER_REQUEST       0x0
480b3d881aSJohn Levon #define VFIO_USER_REPLY         0x1
490b3d881aSJohn Levon #define VFIO_USER_TYPE          0xF
500b3d881aSJohn Levon 
510b3d881aSJohn Levon #define VFIO_USER_NO_REPLY      0x10
520b3d881aSJohn Levon #define VFIO_USER_ERROR         0x20
530b3d881aSJohn Levon 
5436227628SJohn Levon 
5536227628SJohn Levon /*
5636227628SJohn Levon  * VFIO_USER_VERSION
5736227628SJohn Levon  */
5836227628SJohn Levon typedef struct {
5936227628SJohn Levon     VFIOUserHdr hdr;
6036227628SJohn Levon     uint16_t major;
6136227628SJohn Levon     uint16_t minor;
6236227628SJohn Levon     char capabilities[];
6336227628SJohn Levon } VFIOUserVersion;
6436227628SJohn Levon 
6536227628SJohn Levon #define VFIO_USER_MAJOR_VER     0
6636227628SJohn Levon #define VFIO_USER_MINOR_VER     0
6736227628SJohn Levon 
6836227628SJohn Levon #define VFIO_USER_CAP           "capabilities"
6936227628SJohn Levon 
7036227628SJohn Levon /* "capabilities" members */
7136227628SJohn Levon #define VFIO_USER_CAP_MAX_FDS   "max_msg_fds"
7236227628SJohn Levon #define VFIO_USER_CAP_MAX_XFER  "max_data_xfer_size"
7336227628SJohn Levon #define VFIO_USER_CAP_PGSIZES   "pgsizes"
7436227628SJohn Levon #define VFIO_USER_CAP_MAP_MAX   "max_dma_maps"
7536227628SJohn Levon #define VFIO_USER_CAP_MIGR      "migration"
76*1a0c32a9SJohn Levon #define VFIO_USER_CAP_MULTI     "write_multiple"
7736227628SJohn Levon 
7836227628SJohn Levon /* "migration" members */
7936227628SJohn Levon #define VFIO_USER_CAP_PGSIZE            "pgsize"
8036227628SJohn Levon #define VFIO_USER_CAP_MAX_BITMAP        "max_bitmap_size"
8136227628SJohn Levon 
8236227628SJohn Levon /*
8336227628SJohn Levon  * Max FDs mainly comes into play when a device supports multiple interrupts
8436227628SJohn Levon  * where each ones uses an eventfd to inject it into the guest.
8536227628SJohn Levon  * It is clamped by the the number of FDs the qio channel supports in a
8636227628SJohn Levon  * single message.
8736227628SJohn Levon  */
8836227628SJohn Levon #define VFIO_USER_DEF_MAX_FDS   8
8936227628SJohn Levon #define VFIO_USER_MAX_MAX_FDS   16
9036227628SJohn Levon 
9136227628SJohn Levon /*
9236227628SJohn Levon  * Max transfer limits the amount of data in region and DMA messages.
9336227628SJohn Levon  * Region R/W will be very small (limited by how much a single instruction
9436227628SJohn Levon  * can process) so just use a reasonable limit here.
9536227628SJohn Levon  */
9636227628SJohn Levon #define VFIO_USER_DEF_MAX_XFER  (1024 * 1024)
9736227628SJohn Levon #define VFIO_USER_MAX_MAX_XFER  (64 * 1024 * 1024)
9836227628SJohn Levon 
9936227628SJohn Levon /*
10036227628SJohn Levon  * Default pagesizes supported is 4k.
10136227628SJohn Levon  */
10236227628SJohn Levon #define VFIO_USER_DEF_PGSIZE    4096
10336227628SJohn Levon 
10436227628SJohn Levon /*
10536227628SJohn Levon  * Default max number of DMA mappings is stolen from the
10636227628SJohn Levon  * linux kernel "dma_entry_limit"
10736227628SJohn Levon  */
10836227628SJohn Levon #define VFIO_USER_DEF_MAP_MAX   65535
10936227628SJohn Levon 
11036227628SJohn Levon /*
11136227628SJohn Levon  * Default max bitmap size is also take from the linux kernel,
11236227628SJohn Levon  * where usage of signed ints limits the VA range to 2^31 bytes.
11336227628SJohn Levon  * Dividing that by the number of bits per byte yields 256MB
11436227628SJohn Levon  */
11536227628SJohn Levon #define VFIO_USER_DEF_MAX_BITMAP (256 * 1024 * 1024)
11636227628SJohn Levon 
1173bdb738bSJohn Levon /*
11818e899e6SJohn Levon  * VFIO_USER_DMA_MAP
11918e899e6SJohn Levon  * imported from struct vfio_iommu_type1_dma_map
12018e899e6SJohn Levon  */
12118e899e6SJohn Levon typedef struct {
12218e899e6SJohn Levon     VFIOUserHdr hdr;
12318e899e6SJohn Levon     uint32_t argsz;
12418e899e6SJohn Levon     uint32_t flags;
12518e899e6SJohn Levon     uint64_t offset;    /* FD offset */
12618e899e6SJohn Levon     uint64_t iova;
12718e899e6SJohn Levon     uint64_t size;
12818e899e6SJohn Levon } VFIOUserDMAMap;
12918e899e6SJohn Levon 
13018e899e6SJohn Levon /*
13118e899e6SJohn Levon  * VFIO_USER_DMA_UNMAP
13218e899e6SJohn Levon  * imported from struct vfio_iommu_type1_dma_unmap
13318e899e6SJohn Levon  */
13418e899e6SJohn Levon typedef struct {
13518e899e6SJohn Levon     VFIOUserHdr hdr;
13618e899e6SJohn Levon     uint32_t argsz;
13718e899e6SJohn Levon     uint32_t flags;
13818e899e6SJohn Levon     uint64_t iova;
13918e899e6SJohn Levon     uint64_t size;
14018e899e6SJohn Levon } VFIOUserDMAUnmap;
14118e899e6SJohn Levon 
14218e899e6SJohn Levon /*
1433bdb738bSJohn Levon  * VFIO_USER_DEVICE_GET_INFO
1443bdb738bSJohn Levon  * imported from struct vfio_device_info
1453bdb738bSJohn Levon  */
1463bdb738bSJohn Levon typedef struct {
1473bdb738bSJohn Levon     VFIOUserHdr hdr;
1483bdb738bSJohn Levon     uint32_t argsz;
1493bdb738bSJohn Levon     uint32_t flags;
1503bdb738bSJohn Levon     uint32_t num_regions;
1513bdb738bSJohn Levon     uint32_t num_irqs;
1523bdb738bSJohn Levon } VFIOUserDeviceInfo;
1533bdb738bSJohn Levon 
154667866d6SJohn Levon /*
155667866d6SJohn Levon  * VFIO_USER_DEVICE_GET_REGION_INFO
156667866d6SJohn Levon  * imported from struct vfio_region_info
157667866d6SJohn Levon  */
158667866d6SJohn Levon typedef struct {
159667866d6SJohn Levon     VFIOUserHdr hdr;
160667866d6SJohn Levon     uint32_t argsz;
161667866d6SJohn Levon     uint32_t flags;
162667866d6SJohn Levon     uint32_t index;
163667866d6SJohn Levon     uint32_t cap_offset;
164667866d6SJohn Levon     uint64_t size;
165667866d6SJohn Levon     uint64_t offset;
166667866d6SJohn Levon } VFIOUserRegionInfo;
167667866d6SJohn Levon 
1681ed50fcbSJohn Levon /*
169ca1add16SJohn Levon  * VFIO_USER_DEVICE_GET_IRQ_INFO
170ca1add16SJohn Levon  * imported from struct vfio_irq_info
171ca1add16SJohn Levon  */
172ca1add16SJohn Levon typedef struct {
173ca1add16SJohn Levon     VFIOUserHdr hdr;
174ca1add16SJohn Levon     uint32_t argsz;
175ca1add16SJohn Levon     uint32_t flags;
176ca1add16SJohn Levon     uint32_t index;
177ca1add16SJohn Levon     uint32_t count;
178ca1add16SJohn Levon } VFIOUserIRQInfo;
179ca1add16SJohn Levon 
180ca1add16SJohn Levon /*
181ca1add16SJohn Levon  * VFIO_USER_DEVICE_SET_IRQS
182ca1add16SJohn Levon  * imported from struct vfio_irq_set
183ca1add16SJohn Levon  */
184ca1add16SJohn Levon typedef struct {
185ca1add16SJohn Levon     VFIOUserHdr hdr;
186ca1add16SJohn Levon     uint32_t argsz;
187ca1add16SJohn Levon     uint32_t flags;
188ca1add16SJohn Levon     uint32_t index;
189ca1add16SJohn Levon     uint32_t start;
190ca1add16SJohn Levon     uint32_t count;
191ca1add16SJohn Levon } VFIOUserIRQSet;
192ca1add16SJohn Levon 
193ca1add16SJohn Levon /*
1941ed50fcbSJohn Levon  * VFIO_USER_REGION_READ
1951ed50fcbSJohn Levon  * VFIO_USER_REGION_WRITE
1961ed50fcbSJohn Levon  */
1971ed50fcbSJohn Levon typedef struct {
1981ed50fcbSJohn Levon     VFIOUserHdr hdr;
1991ed50fcbSJohn Levon     uint64_t offset;
2001ed50fcbSJohn Levon     uint32_t region;
2011ed50fcbSJohn Levon     uint32_t count;
2021ed50fcbSJohn Levon     char data[];
2031ed50fcbSJohn Levon } VFIOUserRegionRW;
2041ed50fcbSJohn Levon 
205c6ac52a4SJohn Levon /*
206c6ac52a4SJohn Levon  * VFIO_USER_DMA_READ
207c6ac52a4SJohn Levon  * VFIO_USER_DMA_WRITE
208c6ac52a4SJohn Levon  */
209c6ac52a4SJohn Levon typedef struct {
210c6ac52a4SJohn Levon     VFIOUserHdr hdr;
211c6ac52a4SJohn Levon     uint64_t offset;
212c6ac52a4SJohn Levon     uint32_t count;
213c6ac52a4SJohn Levon     char data[];
214c6ac52a4SJohn Levon } VFIOUserDMARW;
215c6ac52a4SJohn Levon 
21618e899e6SJohn Levon /* imported from struct vfio_bitmap */
21718e899e6SJohn Levon typedef struct {
21818e899e6SJohn Levon     uint64_t pgsize;
21918e899e6SJohn Levon     uint64_t size;
22018e899e6SJohn Levon     char data[];
22118e899e6SJohn Levon } VFIOUserBitmap;
22218e899e6SJohn Levon 
223*1a0c32a9SJohn Levon /*
224*1a0c32a9SJohn Levon  * VFIO_USER_REGION_WRITE_MULTI
225*1a0c32a9SJohn Levon  */
226*1a0c32a9SJohn Levon #define VFIO_USER_MULTI_DATA  8
227*1a0c32a9SJohn Levon #define VFIO_USER_MULTI_MAX   200
228*1a0c32a9SJohn Levon 
229*1a0c32a9SJohn Levon typedef struct {
230*1a0c32a9SJohn Levon     uint64_t offset;
231*1a0c32a9SJohn Levon     uint32_t region;
232*1a0c32a9SJohn Levon     uint32_t count;
233*1a0c32a9SJohn Levon     char data[VFIO_USER_MULTI_DATA];
234*1a0c32a9SJohn Levon } VFIOUserWROne;
235*1a0c32a9SJohn Levon 
236*1a0c32a9SJohn Levon typedef struct {
237*1a0c32a9SJohn Levon     VFIOUserHdr hdr;
238*1a0c32a9SJohn Levon     uint64_t wr_cnt;
239*1a0c32a9SJohn Levon     VFIOUserWROne wrs[VFIO_USER_MULTI_MAX];
240*1a0c32a9SJohn Levon } VFIOUserWRMulti;
241*1a0c32a9SJohn Levon 
2420b3d881aSJohn Levon #endif /* VFIO_USER_PROTOCOL_H */
243