xref: /qemu/include/hw/virtio/virtio-pci.h (revision 06d97bb63ba16a417137d820d320c7212e1043ce)
1 /*
2  * Virtio PCI Bindings
3  *
4  * Copyright IBM, Corp. 2007
5  * Copyright (c) 2009 CodeSourcery
6  *
7  * Authors:
8  *  Anthony Liguori   <aliguori@us.ibm.com>
9  *  Paul Brook        <paul@codesourcery.com>
10  *
11  * This work is licensed under the terms of the GNU GPL, version 2.  See
12  * the COPYING file in the top-level directory.
13  */
14 
15 #ifndef QEMU_VIRTIO_PCI_H
16 #define QEMU_VIRTIO_PCI_H
17 
18 #include "hw/pci/msi.h"
19 #include "hw/virtio/virtio-blk.h"
20 #include "hw/virtio/virtio-net.h"
21 #include "hw/virtio/virtio-serial.h"
22 #include "hw/virtio/virtio-scsi.h"
23 #include "hw/virtio/virtio-balloon.h"
24 #include "hw/virtio/virtio-bus.h"
25 #include "hw/virtio/virtio-gpu.h"
26 #include "hw/virtio/virtio-crypto.h"
27 #include "hw/virtio/vhost-user-scsi.h"
28 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
29 #include "hw/virtio/vhost-user-blk.h"
30 #endif
31 
32 #ifdef CONFIG_VIRTFS
33 #include "hw/9pfs/virtio-9p.h"
34 #endif
35 #ifdef CONFIG_VHOST_SCSI
36 #include "hw/virtio/vhost-scsi.h"
37 #endif
38 
39 typedef struct VirtIOPCIProxy VirtIOPCIProxy;
40 typedef struct VirtIOBlkPCI VirtIOBlkPCI;
41 typedef struct VirtIOSCSIPCI VirtIOSCSIPCI;
42 typedef struct VirtIOBalloonPCI VirtIOBalloonPCI;
43 typedef struct VirtIOSerialPCI VirtIOSerialPCI;
44 typedef struct VirtIONetPCI VirtIONetPCI;
45 typedef struct VHostSCSIPCI VHostSCSIPCI;
46 typedef struct VHostUserSCSIPCI VHostUserSCSIPCI;
47 typedef struct VHostUserBlkPCI VHostUserBlkPCI;
48 typedef struct VirtIOGPUPCI VirtIOGPUPCI;
49 typedef struct VirtIOCryptoPCI VirtIOCryptoPCI;
50 
51 /* virtio-pci-bus */
52 
53 typedef struct VirtioBusState VirtioPCIBusState;
54 typedef struct VirtioBusClass VirtioPCIBusClass;
55 
56 #define TYPE_VIRTIO_PCI_BUS "virtio-pci-bus"
57 #define VIRTIO_PCI_BUS(obj) \
58         OBJECT_CHECK(VirtioPCIBusState, (obj), TYPE_VIRTIO_PCI_BUS)
59 #define VIRTIO_PCI_BUS_GET_CLASS(obj) \
60         OBJECT_GET_CLASS(VirtioPCIBusClass, obj, TYPE_VIRTIO_PCI_BUS)
61 #define VIRTIO_PCI_BUS_CLASS(klass) \
62         OBJECT_CLASS_CHECK(VirtioPCIBusClass, klass, TYPE_VIRTIO_PCI_BUS)
63 
64 enum {
65     VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT,
66     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT,
67     VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT,
68     VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT,
69     VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT,
70     VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT,
71     VIRTIO_PCI_FLAG_ATS_BIT,
72     VIRTIO_PCI_FLAG_INIT_DEVERR_BIT,
73     VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT,
74     VIRTIO_PCI_FLAG_INIT_PM_BIT,
75 };
76 
77 /* Need to activate work-arounds for buggy guests at vmstate load. */
78 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION \
79     (1 << VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT)
80 
81 /* Performance improves when virtqueue kick processing is decoupled from the
82  * vcpu thread using ioeventfd for some devices. */
83 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD   (1 << VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT)
84 
85 /* virtio version flags */
86 #define VIRTIO_PCI_FLAG_DISABLE_PCIE (1 << VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT)
87 
88 /* migrate extra state */
89 #define VIRTIO_PCI_FLAG_MIGRATE_EXTRA (1 << VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT)
90 
91 /* have pio notification for modern device ? */
92 #define VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY \
93     (1 << VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT)
94 
95 /* page per vq flag to be used by split drivers within guests */
96 #define VIRTIO_PCI_FLAG_PAGE_PER_VQ \
97     (1 << VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT)
98 
99 /* address space translation service */
100 #define VIRTIO_PCI_FLAG_ATS (1 << VIRTIO_PCI_FLAG_ATS_BIT)
101 
102 /* Init error enabling flags */
103 #define VIRTIO_PCI_FLAG_INIT_DEVERR (1 << VIRTIO_PCI_FLAG_INIT_DEVERR_BIT)
104 
105 /* Init Link Control register */
106 #define VIRTIO_PCI_FLAG_INIT_LNKCTL (1 << VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT)
107 
108 /* Init Power Management */
109 #define VIRTIO_PCI_FLAG_INIT_PM (1 << VIRTIO_PCI_FLAG_INIT_PM_BIT)
110 
111 typedef struct {
112     MSIMessage msg;
113     int virq;
114     unsigned int users;
115 } VirtIOIRQFD;
116 
117 /*
118  * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
119  */
120 #define TYPE_VIRTIO_PCI "virtio-pci"
121 #define VIRTIO_PCI_GET_CLASS(obj) \
122         OBJECT_GET_CLASS(VirtioPCIClass, obj, TYPE_VIRTIO_PCI)
123 #define VIRTIO_PCI_CLASS(klass) \
124         OBJECT_CLASS_CHECK(VirtioPCIClass, klass, TYPE_VIRTIO_PCI)
125 #define VIRTIO_PCI(obj) \
126         OBJECT_CHECK(VirtIOPCIProxy, (obj), TYPE_VIRTIO_PCI)
127 
128 typedef struct VirtioPCIClass {
129     PCIDeviceClass parent_class;
130     DeviceRealize parent_dc_realize;
131     void (*realize)(VirtIOPCIProxy *vpci_dev, Error **errp);
132 } VirtioPCIClass;
133 
134 typedef struct VirtIOPCIRegion {
135     MemoryRegion mr;
136     uint32_t offset;
137     uint32_t size;
138     uint32_t type;
139 } VirtIOPCIRegion;
140 
141 typedef struct VirtIOPCIQueue {
142   uint16_t num;
143   bool enabled;
144   uint32_t desc[2];
145   uint32_t avail[2];
146   uint32_t used[2];
147 } VirtIOPCIQueue;
148 
149 struct VirtIOPCIProxy {
150     PCIDevice pci_dev;
151     MemoryRegion bar;
152     union {
153         struct {
154             VirtIOPCIRegion common;
155             VirtIOPCIRegion isr;
156             VirtIOPCIRegion device;
157             VirtIOPCIRegion notify;
158             VirtIOPCIRegion notify_pio;
159         };
160         VirtIOPCIRegion regs[5];
161     };
162     MemoryRegion modern_bar;
163     MemoryRegion io_bar;
164     uint32_t legacy_io_bar_idx;
165     uint32_t msix_bar_idx;
166     uint32_t modern_io_bar_idx;
167     uint32_t modern_mem_bar_idx;
168     int config_cap;
169     uint32_t flags;
170     bool disable_modern;
171     bool ignore_backend_features;
172     OnOffAuto disable_legacy;
173     uint32_t class_code;
174     uint32_t nvectors;
175     uint32_t dfselect;
176     uint32_t gfselect;
177     uint32_t guest_features[2];
178     VirtIOPCIQueue vqs[VIRTIO_QUEUE_MAX];
179 
180     VirtIOIRQFD *vector_irqfd;
181     int nvqs_with_notifiers;
182     VirtioBusState bus;
183 };
184 
185 static inline bool virtio_pci_modern(VirtIOPCIProxy *proxy)
186 {
187     return !proxy->disable_modern;
188 }
189 
190 static inline bool virtio_pci_legacy(VirtIOPCIProxy *proxy)
191 {
192     return proxy->disable_legacy == ON_OFF_AUTO_OFF;
193 }
194 
195 static inline void virtio_pci_force_virtio_1(VirtIOPCIProxy *proxy)
196 {
197     proxy->disable_modern = false;
198     proxy->disable_legacy = ON_OFF_AUTO_ON;
199 }
200 
201 static inline void virtio_pci_disable_modern(VirtIOPCIProxy *proxy)
202 {
203     proxy->disable_modern = true;
204 }
205 
206 /*
207  * virtio-scsi-pci: This extends VirtioPCIProxy.
208  */
209 #define TYPE_VIRTIO_SCSI_PCI "virtio-scsi-pci-base"
210 #define VIRTIO_SCSI_PCI(obj) \
211         OBJECT_CHECK(VirtIOSCSIPCI, (obj), TYPE_VIRTIO_SCSI_PCI)
212 
213 struct VirtIOSCSIPCI {
214     VirtIOPCIProxy parent_obj;
215     VirtIOSCSI vdev;
216 };
217 
218 #ifdef CONFIG_VHOST_SCSI
219 /*
220  * vhost-scsi-pci: This extends VirtioPCIProxy.
221  */
222 #define TYPE_VHOST_SCSI_PCI "vhost-scsi-pci-base"
223 #define VHOST_SCSI_PCI(obj) \
224         OBJECT_CHECK(VHostSCSIPCI, (obj), TYPE_VHOST_SCSI_PCI)
225 
226 struct VHostSCSIPCI {
227     VirtIOPCIProxy parent_obj;
228     VHostSCSI vdev;
229 };
230 #endif
231 
232 #define TYPE_VHOST_USER_SCSI_PCI "vhost-user-scsi-pci-base"
233 #define VHOST_USER_SCSI_PCI(obj) \
234         OBJECT_CHECK(VHostUserSCSIPCI, (obj), TYPE_VHOST_USER_SCSI_PCI)
235 
236 struct VHostUserSCSIPCI {
237     VirtIOPCIProxy parent_obj;
238     VHostUserSCSI vdev;
239 };
240 
241 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
242 /*
243  * vhost-user-blk-pci: This extends VirtioPCIProxy.
244  */
245 #define TYPE_VHOST_USER_BLK_PCI "vhost-user-blk-pci-base"
246 #define VHOST_USER_BLK_PCI(obj) \
247         OBJECT_CHECK(VHostUserBlkPCI, (obj), TYPE_VHOST_USER_BLK_PCI)
248 
249 struct VHostUserBlkPCI {
250     VirtIOPCIProxy parent_obj;
251     VHostUserBlk vdev;
252 };
253 #endif
254 
255 /*
256  * virtio-blk-pci: This extends VirtioPCIProxy.
257  */
258 #define TYPE_VIRTIO_BLK_PCI "virtio-blk-pci-base"
259 #define VIRTIO_BLK_PCI(obj) \
260         OBJECT_CHECK(VirtIOBlkPCI, (obj), TYPE_VIRTIO_BLK_PCI)
261 
262 struct VirtIOBlkPCI {
263     VirtIOPCIProxy parent_obj;
264     VirtIOBlock vdev;
265 };
266 
267 /*
268  * virtio-balloon-pci: This extends VirtioPCIProxy.
269  */
270 #define TYPE_VIRTIO_BALLOON_PCI "virtio-balloon-pci-base"
271 #define VIRTIO_BALLOON_PCI(obj) \
272         OBJECT_CHECK(VirtIOBalloonPCI, (obj), TYPE_VIRTIO_BALLOON_PCI)
273 
274 struct VirtIOBalloonPCI {
275     VirtIOPCIProxy parent_obj;
276     VirtIOBalloon vdev;
277 };
278 
279 /*
280  * virtio-serial-pci: This extends VirtioPCIProxy.
281  */
282 #define TYPE_VIRTIO_SERIAL_PCI "virtio-serial-pci-base"
283 #define VIRTIO_SERIAL_PCI(obj) \
284         OBJECT_CHECK(VirtIOSerialPCI, (obj), TYPE_VIRTIO_SERIAL_PCI)
285 
286 struct VirtIOSerialPCI {
287     VirtIOPCIProxy parent_obj;
288     VirtIOSerial vdev;
289 };
290 
291 /*
292  * virtio-net-pci: This extends VirtioPCIProxy.
293  */
294 #define TYPE_VIRTIO_NET_PCI "virtio-net-pci-base"
295 #define VIRTIO_NET_PCI(obj) \
296         OBJECT_CHECK(VirtIONetPCI, (obj), TYPE_VIRTIO_NET_PCI)
297 
298 struct VirtIONetPCI {
299     VirtIOPCIProxy parent_obj;
300     VirtIONet vdev;
301 };
302 
303 /*
304  * virtio-9p-pci: This extends VirtioPCIProxy.
305  */
306 
307 #ifdef CONFIG_VIRTFS
308 
309 #define TYPE_VIRTIO_9P_PCI "virtio-9p-pci-base"
310 #define VIRTIO_9P_PCI(obj) \
311         OBJECT_CHECK(V9fsPCIState, (obj), TYPE_VIRTIO_9P_PCI)
312 
313 typedef struct V9fsPCIState {
314     VirtIOPCIProxy parent_obj;
315     V9fsVirtioState vdev;
316 } V9fsPCIState;
317 
318 #endif
319 
320 /*
321  * virtio-input-pci: This extends VirtioPCIProxy.
322  */
323 #define TYPE_VIRTIO_INPUT_PCI "virtio-input-pci"
324 
325 /*
326  * virtio-gpu-pci: This extends VirtioPCIProxy.
327  */
328 #define TYPE_VIRTIO_GPU_PCI "virtio-gpu-pci"
329 #define VIRTIO_GPU_PCI(obj) \
330         OBJECT_CHECK(VirtIOGPUPCI, (obj), TYPE_VIRTIO_GPU_PCI)
331 
332 struct VirtIOGPUPCI {
333     VirtIOPCIProxy parent_obj;
334     VirtIOGPU vdev;
335 };
336 
337 /*
338  * virtio-crypto-pci: This extends VirtioPCIProxy.
339  */
340 #define TYPE_VIRTIO_CRYPTO_PCI "virtio-crypto-pci"
341 #define VIRTIO_CRYPTO_PCI(obj) \
342         OBJECT_CHECK(VirtIOCryptoPCI, (obj), TYPE_VIRTIO_CRYPTO_PCI)
343 
344 struct VirtIOCryptoPCI {
345     VirtIOPCIProxy parent_obj;
346     VirtIOCrypto vdev;
347 };
348 
349 /* Virtio ABI version, if we increment this, we break the guest driver. */
350 #define VIRTIO_PCI_ABI_VERSION          0
351 
352 /* Input for virtio_pci_types_register() */
353 typedef struct VirtioPCIDeviceTypeInfo {
354     /*
355      * Common base class for the subclasses below.
356      *
357      * Required only if transitional_name or non_transitional_name is set.
358      *
359      * We need a separate base type instead of making all types
360      * inherit from generic_name for two reasons:
361      * 1) generic_name implements INTERFACE_PCIE_DEVICE, but
362      *    transitional_name does not.
363      * 2) generic_name has the "disable-legacy" and "disable-modern"
364      *    properties, transitional_name and non_transitional name don't.
365      */
366     const char *base_name;
367     /*
368      * Generic device type.  Optional.
369      *
370      * Supports both transitional and non-transitional modes,
371      * using the disable-legacy and disable-modern properties.
372      * If disable-legacy=auto, (non-)transitional mode is selected
373      * depending on the bus where the device is plugged.
374      *
375      * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE,
376      * but PCI Express is supported only in non-transitional mode.
377      *
378      * The only type implemented by QEMU 3.1 and older.
379      */
380     const char *generic_name;
381     /*
382      * The transitional device type.  Optional.
383      *
384      * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE.
385      */
386     const char *transitional_name;
387     /*
388      * The non-transitional device type.  Optional.
389      *
390      * Implements INTERFACE_CONVENTIONAL_PCI_DEVICE only.
391      */
392     const char *non_transitional_name;
393 
394     /* Parent type.  If NULL, TYPE_VIRTIO_PCI is used */
395     const char *parent;
396 
397     /* Same as TypeInfo fields: */
398     size_t instance_size;
399     void (*instance_init)(Object *obj);
400     void (*class_init)(ObjectClass *klass, void *data);
401 } VirtioPCIDeviceTypeInfo;
402 
403 /* Register virtio-pci type(s).  @t must be static. */
404 void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo *t);
405 
406 #endif
407