xref: /qemu/hw/vfio-user/protocol.h (revision ca1add1696dd53f905c2a17f36159d54e9b6f527)
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,
420b3d881aSJohn Levon     VFIO_USER_MAX,
430b3d881aSJohn Levon };
440b3d881aSJohn Levon 
450b3d881aSJohn Levon /* VFIOUserHdr flags */
460b3d881aSJohn Levon #define VFIO_USER_REQUEST       0x0
470b3d881aSJohn Levon #define VFIO_USER_REPLY         0x1
480b3d881aSJohn Levon #define VFIO_USER_TYPE          0xF
490b3d881aSJohn Levon 
500b3d881aSJohn Levon #define VFIO_USER_NO_REPLY      0x10
510b3d881aSJohn Levon #define VFIO_USER_ERROR         0x20
520b3d881aSJohn Levon 
5336227628SJohn Levon 
5436227628SJohn Levon /*
5536227628SJohn Levon  * VFIO_USER_VERSION
5636227628SJohn Levon  */
5736227628SJohn Levon typedef struct {
5836227628SJohn Levon     VFIOUserHdr hdr;
5936227628SJohn Levon     uint16_t major;
6036227628SJohn Levon     uint16_t minor;
6136227628SJohn Levon     char capabilities[];
6236227628SJohn Levon } VFIOUserVersion;
6336227628SJohn Levon 
6436227628SJohn Levon #define VFIO_USER_MAJOR_VER     0
6536227628SJohn Levon #define VFIO_USER_MINOR_VER     0
6636227628SJohn Levon 
6736227628SJohn Levon #define VFIO_USER_CAP           "capabilities"
6836227628SJohn Levon 
6936227628SJohn Levon /* "capabilities" members */
7036227628SJohn Levon #define VFIO_USER_CAP_MAX_FDS   "max_msg_fds"
7136227628SJohn Levon #define VFIO_USER_CAP_MAX_XFER  "max_data_xfer_size"
7236227628SJohn Levon #define VFIO_USER_CAP_PGSIZES   "pgsizes"
7336227628SJohn Levon #define VFIO_USER_CAP_MAP_MAX   "max_dma_maps"
7436227628SJohn Levon #define VFIO_USER_CAP_MIGR      "migration"
7536227628SJohn Levon 
7636227628SJohn Levon /* "migration" members */
7736227628SJohn Levon #define VFIO_USER_CAP_PGSIZE            "pgsize"
7836227628SJohn Levon #define VFIO_USER_CAP_MAX_BITMAP        "max_bitmap_size"
7936227628SJohn Levon 
8036227628SJohn Levon /*
8136227628SJohn Levon  * Max FDs mainly comes into play when a device supports multiple interrupts
8236227628SJohn Levon  * where each ones uses an eventfd to inject it into the guest.
8336227628SJohn Levon  * It is clamped by the the number of FDs the qio channel supports in a
8436227628SJohn Levon  * single message.
8536227628SJohn Levon  */
8636227628SJohn Levon #define VFIO_USER_DEF_MAX_FDS   8
8736227628SJohn Levon #define VFIO_USER_MAX_MAX_FDS   16
8836227628SJohn Levon 
8936227628SJohn Levon /*
9036227628SJohn Levon  * Max transfer limits the amount of data in region and DMA messages.
9136227628SJohn Levon  * Region R/W will be very small (limited by how much a single instruction
9236227628SJohn Levon  * can process) so just use a reasonable limit here.
9336227628SJohn Levon  */
9436227628SJohn Levon #define VFIO_USER_DEF_MAX_XFER  (1024 * 1024)
9536227628SJohn Levon #define VFIO_USER_MAX_MAX_XFER  (64 * 1024 * 1024)
9636227628SJohn Levon 
9736227628SJohn Levon /*
9836227628SJohn Levon  * Default pagesizes supported is 4k.
9936227628SJohn Levon  */
10036227628SJohn Levon #define VFIO_USER_DEF_PGSIZE    4096
10136227628SJohn Levon 
10236227628SJohn Levon /*
10336227628SJohn Levon  * Default max number of DMA mappings is stolen from the
10436227628SJohn Levon  * linux kernel "dma_entry_limit"
10536227628SJohn Levon  */
10636227628SJohn Levon #define VFIO_USER_DEF_MAP_MAX   65535
10736227628SJohn Levon 
10836227628SJohn Levon /*
10936227628SJohn Levon  * Default max bitmap size is also take from the linux kernel,
11036227628SJohn Levon  * where usage of signed ints limits the VA range to 2^31 bytes.
11136227628SJohn Levon  * Dividing that by the number of bits per byte yields 256MB
11236227628SJohn Levon  */
11336227628SJohn Levon #define VFIO_USER_DEF_MAX_BITMAP (256 * 1024 * 1024)
11436227628SJohn Levon 
1153bdb738bSJohn Levon /*
1163bdb738bSJohn Levon  * VFIO_USER_DEVICE_GET_INFO
1173bdb738bSJohn Levon  * imported from struct vfio_device_info
1183bdb738bSJohn Levon  */
1193bdb738bSJohn Levon typedef struct {
1203bdb738bSJohn Levon     VFIOUserHdr hdr;
1213bdb738bSJohn Levon     uint32_t argsz;
1223bdb738bSJohn Levon     uint32_t flags;
1233bdb738bSJohn Levon     uint32_t num_regions;
1243bdb738bSJohn Levon     uint32_t num_irqs;
1253bdb738bSJohn Levon } VFIOUserDeviceInfo;
1263bdb738bSJohn Levon 
127667866d6SJohn Levon /*
128667866d6SJohn Levon  * VFIO_USER_DEVICE_GET_REGION_INFO
129667866d6SJohn Levon  * imported from struct vfio_region_info
130667866d6SJohn Levon  */
131667866d6SJohn Levon typedef struct {
132667866d6SJohn Levon     VFIOUserHdr hdr;
133667866d6SJohn Levon     uint32_t argsz;
134667866d6SJohn Levon     uint32_t flags;
135667866d6SJohn Levon     uint32_t index;
136667866d6SJohn Levon     uint32_t cap_offset;
137667866d6SJohn Levon     uint64_t size;
138667866d6SJohn Levon     uint64_t offset;
139667866d6SJohn Levon } VFIOUserRegionInfo;
140667866d6SJohn Levon 
1411ed50fcbSJohn Levon /*
142*ca1add16SJohn Levon  * VFIO_USER_DEVICE_GET_IRQ_INFO
143*ca1add16SJohn Levon  * imported from struct vfio_irq_info
144*ca1add16SJohn Levon  */
145*ca1add16SJohn Levon typedef struct {
146*ca1add16SJohn Levon     VFIOUserHdr hdr;
147*ca1add16SJohn Levon     uint32_t argsz;
148*ca1add16SJohn Levon     uint32_t flags;
149*ca1add16SJohn Levon     uint32_t index;
150*ca1add16SJohn Levon     uint32_t count;
151*ca1add16SJohn Levon } VFIOUserIRQInfo;
152*ca1add16SJohn Levon 
153*ca1add16SJohn Levon /*
154*ca1add16SJohn Levon  * VFIO_USER_DEVICE_SET_IRQS
155*ca1add16SJohn Levon  * imported from struct vfio_irq_set
156*ca1add16SJohn Levon  */
157*ca1add16SJohn Levon typedef struct {
158*ca1add16SJohn Levon     VFIOUserHdr hdr;
159*ca1add16SJohn Levon     uint32_t argsz;
160*ca1add16SJohn Levon     uint32_t flags;
161*ca1add16SJohn Levon     uint32_t index;
162*ca1add16SJohn Levon     uint32_t start;
163*ca1add16SJohn Levon     uint32_t count;
164*ca1add16SJohn Levon } VFIOUserIRQSet;
165*ca1add16SJohn Levon 
166*ca1add16SJohn Levon /*
1671ed50fcbSJohn Levon  * VFIO_USER_REGION_READ
1681ed50fcbSJohn Levon  * VFIO_USER_REGION_WRITE
1691ed50fcbSJohn Levon  */
1701ed50fcbSJohn Levon typedef struct {
1711ed50fcbSJohn Levon     VFIOUserHdr hdr;
1721ed50fcbSJohn Levon     uint64_t offset;
1731ed50fcbSJohn Levon     uint32_t region;
1741ed50fcbSJohn Levon     uint32_t count;
1751ed50fcbSJohn Levon     char data[];
1761ed50fcbSJohn Levon } VFIOUserRegionRW;
1771ed50fcbSJohn Levon 
1780b3d881aSJohn Levon #endif /* VFIO_USER_PROTOCOL_H */
179