xref: /qemu/hw/vfio-user/protocol.h (revision ca1add1696dd53f905c2a17f36159d54e9b6f527)
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_MAX,
43 };
44 
45 /* VFIOUserHdr flags */
46 #define VFIO_USER_REQUEST       0x0
47 #define VFIO_USER_REPLY         0x1
48 #define VFIO_USER_TYPE          0xF
49 
50 #define VFIO_USER_NO_REPLY      0x10
51 #define VFIO_USER_ERROR         0x20
52 
53 
54 /*
55  * VFIO_USER_VERSION
56  */
57 typedef struct {
58     VFIOUserHdr hdr;
59     uint16_t major;
60     uint16_t minor;
61     char capabilities[];
62 } VFIOUserVersion;
63 
64 #define VFIO_USER_MAJOR_VER     0
65 #define VFIO_USER_MINOR_VER     0
66 
67 #define VFIO_USER_CAP           "capabilities"
68 
69 /* "capabilities" members */
70 #define VFIO_USER_CAP_MAX_FDS   "max_msg_fds"
71 #define VFIO_USER_CAP_MAX_XFER  "max_data_xfer_size"
72 #define VFIO_USER_CAP_PGSIZES   "pgsizes"
73 #define VFIO_USER_CAP_MAP_MAX   "max_dma_maps"
74 #define VFIO_USER_CAP_MIGR      "migration"
75 
76 /* "migration" members */
77 #define VFIO_USER_CAP_PGSIZE            "pgsize"
78 #define VFIO_USER_CAP_MAX_BITMAP        "max_bitmap_size"
79 
80 /*
81  * Max FDs mainly comes into play when a device supports multiple interrupts
82  * where each ones uses an eventfd to inject it into the guest.
83  * It is clamped by the the number of FDs the qio channel supports in a
84  * single message.
85  */
86 #define VFIO_USER_DEF_MAX_FDS   8
87 #define VFIO_USER_MAX_MAX_FDS   16
88 
89 /*
90  * Max transfer limits the amount of data in region and DMA messages.
91  * Region R/W will be very small (limited by how much a single instruction
92  * can process) so just use a reasonable limit here.
93  */
94 #define VFIO_USER_DEF_MAX_XFER  (1024 * 1024)
95 #define VFIO_USER_MAX_MAX_XFER  (64 * 1024 * 1024)
96 
97 /*
98  * Default pagesizes supported is 4k.
99  */
100 #define VFIO_USER_DEF_PGSIZE    4096
101 
102 /*
103  * Default max number of DMA mappings is stolen from the
104  * linux kernel "dma_entry_limit"
105  */
106 #define VFIO_USER_DEF_MAP_MAX   65535
107 
108 /*
109  * Default max bitmap size is also take from the linux kernel,
110  * where usage of signed ints limits the VA range to 2^31 bytes.
111  * Dividing that by the number of bits per byte yields 256MB
112  */
113 #define VFIO_USER_DEF_MAX_BITMAP (256 * 1024 * 1024)
114 
115 /*
116  * VFIO_USER_DEVICE_GET_INFO
117  * imported from struct vfio_device_info
118  */
119 typedef struct {
120     VFIOUserHdr hdr;
121     uint32_t argsz;
122     uint32_t flags;
123     uint32_t num_regions;
124     uint32_t num_irqs;
125 } VFIOUserDeviceInfo;
126 
127 /*
128  * VFIO_USER_DEVICE_GET_REGION_INFO
129  * imported from struct vfio_region_info
130  */
131 typedef struct {
132     VFIOUserHdr hdr;
133     uint32_t argsz;
134     uint32_t flags;
135     uint32_t index;
136     uint32_t cap_offset;
137     uint64_t size;
138     uint64_t offset;
139 } VFIOUserRegionInfo;
140 
141 /*
142  * VFIO_USER_DEVICE_GET_IRQ_INFO
143  * imported from struct vfio_irq_info
144  */
145 typedef struct {
146     VFIOUserHdr hdr;
147     uint32_t argsz;
148     uint32_t flags;
149     uint32_t index;
150     uint32_t count;
151 } VFIOUserIRQInfo;
152 
153 /*
154  * VFIO_USER_DEVICE_SET_IRQS
155  * imported from struct vfio_irq_set
156  */
157 typedef struct {
158     VFIOUserHdr hdr;
159     uint32_t argsz;
160     uint32_t flags;
161     uint32_t index;
162     uint32_t start;
163     uint32_t count;
164 } VFIOUserIRQSet;
165 
166 /*
167  * VFIO_USER_REGION_READ
168  * VFIO_USER_REGION_WRITE
169  */
170 typedef struct {
171     VFIOUserHdr hdr;
172     uint64_t offset;
173     uint32_t region;
174     uint32_t count;
175     char data[];
176 } VFIOUserRegionRW;
177 
178 #endif /* VFIO_USER_PROTOCOL_H */
179