xref: /qemu/hw/vfio-user/protocol.h (revision 18e899e63dd9d05e567c081023ad7fc5b2c5dc9a)
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_DMA_MAP
117  * imported from struct vfio_iommu_type1_dma_map
118  */
119 typedef struct {
120     VFIOUserHdr hdr;
121     uint32_t argsz;
122     uint32_t flags;
123     uint64_t offset;    /* FD offset */
124     uint64_t iova;
125     uint64_t size;
126 } VFIOUserDMAMap;
127 
128 /*
129  * VFIO_USER_DMA_UNMAP
130  * imported from struct vfio_iommu_type1_dma_unmap
131  */
132 typedef struct {
133     VFIOUserHdr hdr;
134     uint32_t argsz;
135     uint32_t flags;
136     uint64_t iova;
137     uint64_t size;
138 } VFIOUserDMAUnmap;
139 
140 /*
141  * VFIO_USER_DEVICE_GET_INFO
142  * imported from struct vfio_device_info
143  */
144 typedef struct {
145     VFIOUserHdr hdr;
146     uint32_t argsz;
147     uint32_t flags;
148     uint32_t num_regions;
149     uint32_t num_irqs;
150 } VFIOUserDeviceInfo;
151 
152 /*
153  * VFIO_USER_DEVICE_GET_REGION_INFO
154  * imported from struct vfio_region_info
155  */
156 typedef struct {
157     VFIOUserHdr hdr;
158     uint32_t argsz;
159     uint32_t flags;
160     uint32_t index;
161     uint32_t cap_offset;
162     uint64_t size;
163     uint64_t offset;
164 } VFIOUserRegionInfo;
165 
166 /*
167  * VFIO_USER_DEVICE_GET_IRQ_INFO
168  * imported from struct vfio_irq_info
169  */
170 typedef struct {
171     VFIOUserHdr hdr;
172     uint32_t argsz;
173     uint32_t flags;
174     uint32_t index;
175     uint32_t count;
176 } VFIOUserIRQInfo;
177 
178 /*
179  * VFIO_USER_DEVICE_SET_IRQS
180  * imported from struct vfio_irq_set
181  */
182 typedef struct {
183     VFIOUserHdr hdr;
184     uint32_t argsz;
185     uint32_t flags;
186     uint32_t index;
187     uint32_t start;
188     uint32_t count;
189 } VFIOUserIRQSet;
190 
191 /*
192  * VFIO_USER_REGION_READ
193  * VFIO_USER_REGION_WRITE
194  */
195 typedef struct {
196     VFIOUserHdr hdr;
197     uint64_t offset;
198     uint32_t region;
199     uint32_t count;
200     char data[];
201 } VFIOUserRegionRW;
202 
203 /*imported from struct vfio_bitmap */
204 typedef struct {
205     uint64_t pgsize;
206     uint64_t size;
207     char data[];
208 } VFIOUserBitmap;
209 
210 #endif /* VFIO_USER_PROTOCOL_H */
211