xref: /qemu/include/hw/virtio/virtio-pci.h (revision f2784eed306449c3d04a71a05ed6463b8289aedf)
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 "qapi/error.h"
19 #include "hw/pci/msi.h"
20 #include "hw/virtio/virtio-bus.h"
21 
22 typedef struct VirtIOPCIProxy VirtIOPCIProxy;
23 
24 /* virtio-pci-bus */
25 
26 typedef struct VirtioBusState VirtioPCIBusState;
27 typedef struct VirtioBusClass VirtioPCIBusClass;
28 
29 #define TYPE_VIRTIO_PCI_BUS "virtio-pci-bus"
30 #define VIRTIO_PCI_BUS(obj) \
31         OBJECT_CHECK(VirtioPCIBusState, (obj), TYPE_VIRTIO_PCI_BUS)
32 #define VIRTIO_PCI_BUS_GET_CLASS(obj) \
33         OBJECT_GET_CLASS(VirtioPCIBusClass, obj, TYPE_VIRTIO_PCI_BUS)
34 #define VIRTIO_PCI_BUS_CLASS(klass) \
35         OBJECT_CLASS_CHECK(VirtioPCIBusClass, klass, TYPE_VIRTIO_PCI_BUS)
36 
37 enum {
38     VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT,
39     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT,
40     VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT,
41     VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT,
42     VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT,
43     VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT,
44     VIRTIO_PCI_FLAG_ATS_BIT,
45     VIRTIO_PCI_FLAG_INIT_DEVERR_BIT,
46     VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT,
47     VIRTIO_PCI_FLAG_INIT_PM_BIT,
48 };
49 
50 /* Need to activate work-arounds for buggy guests at vmstate load. */
51 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION \
52     (1 << VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT)
53 
54 /* Performance improves when virtqueue kick processing is decoupled from the
55  * vcpu thread using ioeventfd for some devices. */
56 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD   (1 << VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT)
57 
58 /* virtio version flags */
59 #define VIRTIO_PCI_FLAG_DISABLE_PCIE (1 << VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT)
60 
61 /* migrate extra state */
62 #define VIRTIO_PCI_FLAG_MIGRATE_EXTRA (1 << VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT)
63 
64 /* have pio notification for modern device ? */
65 #define VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY \
66     (1 << VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT)
67 
68 /* page per vq flag to be used by split drivers within guests */
69 #define VIRTIO_PCI_FLAG_PAGE_PER_VQ \
70     (1 << VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT)
71 
72 /* address space translation service */
73 #define VIRTIO_PCI_FLAG_ATS (1 << VIRTIO_PCI_FLAG_ATS_BIT)
74 
75 /* Init error enabling flags */
76 #define VIRTIO_PCI_FLAG_INIT_DEVERR (1 << VIRTIO_PCI_FLAG_INIT_DEVERR_BIT)
77 
78 /* Init Link Control register */
79 #define VIRTIO_PCI_FLAG_INIT_LNKCTL (1 << VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT)
80 
81 /* Init Power Management */
82 #define VIRTIO_PCI_FLAG_INIT_PM (1 << VIRTIO_PCI_FLAG_INIT_PM_BIT)
83 
84 typedef struct {
85     MSIMessage msg;
86     int virq;
87     unsigned int users;
88 } VirtIOIRQFD;
89 
90 /*
91  * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
92  */
93 #define TYPE_VIRTIO_PCI "virtio-pci"
94 #define VIRTIO_PCI_GET_CLASS(obj) \
95         OBJECT_GET_CLASS(VirtioPCIClass, obj, TYPE_VIRTIO_PCI)
96 #define VIRTIO_PCI_CLASS(klass) \
97         OBJECT_CLASS_CHECK(VirtioPCIClass, klass, TYPE_VIRTIO_PCI)
98 #define VIRTIO_PCI(obj) \
99         OBJECT_CHECK(VirtIOPCIProxy, (obj), TYPE_VIRTIO_PCI)
100 
101 typedef struct VirtioPCIClass {
102     PCIDeviceClass parent_class;
103     DeviceRealize parent_dc_realize;
104     void (*realize)(VirtIOPCIProxy *vpci_dev, Error **errp);
105 } VirtioPCIClass;
106 
107 typedef struct VirtIOPCIRegion {
108     MemoryRegion mr;
109     uint32_t offset;
110     uint32_t size;
111     uint32_t type;
112 } VirtIOPCIRegion;
113 
114 typedef struct VirtIOPCIQueue {
115   uint16_t num;
116   bool enabled;
117   uint32_t desc[2];
118   uint32_t avail[2];
119   uint32_t used[2];
120 } VirtIOPCIQueue;
121 
122 typedef enum {
123     VIRTIO_PCI_MODE_LEGACY,
124     VIRTIO_PCI_MODE_TRANSITIONAL,
125     VIRTIO_PCI_MODE_MODERN,
126 } VirtIOPCIMode;
127 
128 struct VirtIOPCIProxy {
129     PCIDevice pci_dev;
130     MemoryRegion bar;
131     union {
132         struct {
133             VirtIOPCIRegion common;
134             VirtIOPCIRegion isr;
135             VirtIOPCIRegion device;
136             VirtIOPCIRegion notify;
137             VirtIOPCIRegion notify_pio;
138         };
139         VirtIOPCIRegion regs[5];
140     };
141     MemoryRegion modern_bar;
142     MemoryRegion io_bar;
143     uint32_t legacy_io_bar_idx;
144     uint32_t msix_bar_idx;
145     uint32_t modern_io_bar_idx;
146     uint32_t modern_mem_bar_idx;
147     int config_cap;
148     uint32_t flags;
149     bool disable_modern;
150     bool ignore_backend_features;
151     OnOffAuto disable_legacy;
152     VirtIOPCIMode mode;
153     uint32_t class_code;
154     uint32_t nvectors;
155     uint32_t dfselect;
156     uint32_t gfselect;
157     uint32_t guest_features[2];
158     VirtIOPCIQueue vqs[VIRTIO_QUEUE_MAX];
159 
160     VirtIOIRQFD *vector_irqfd;
161     int nvqs_with_notifiers;
162     VirtioBusState bus;
163 };
164 
165 static inline bool virtio_pci_modern(VirtIOPCIProxy *proxy)
166 {
167     return proxy->mode != VIRTIO_PCI_MODE_LEGACY;
168 }
169 
170 static inline bool virtio_pci_legacy(VirtIOPCIProxy *proxy)
171 {
172     return proxy->mode != VIRTIO_PCI_MODE_MODERN;
173 }
174 
175 static inline bool virtio_pci_force_virtio_1(VirtIOPCIProxy *proxy,
176                                              Error **errp)
177 {
178     if (proxy->disable_legacy == ON_OFF_AUTO_OFF) {
179         error_setg(errp, "Unable to set disable-legacy=off on a virtio-1.0 "
180                    "only device");
181         return false;
182     }
183     if (proxy->disable_modern == true) {
184         error_setg(errp, "Unable to set disable-modern=on on a virtio-1.0 "
185                    "only device");
186         return false;
187     }
188     proxy->mode = VIRTIO_PCI_MODE_MODERN;
189     return true;
190 }
191 
192 static inline void virtio_pci_disable_modern(VirtIOPCIProxy *proxy)
193 {
194     proxy->mode = VIRTIO_PCI_MODE_LEGACY;
195 }
196 
197 /*
198  * virtio-input-pci: This extends VirtioPCIProxy.
199  */
200 #define TYPE_VIRTIO_INPUT_PCI "virtio-input-pci"
201 
202 /* Virtio ABI version, if we increment this, we break the guest driver. */
203 #define VIRTIO_PCI_ABI_VERSION          0
204 
205 /* Input for virtio_pci_types_register() */
206 typedef struct VirtioPCIDeviceTypeInfo {
207     /*
208      * Common base class for the subclasses below.
209      *
210      * Required only if transitional_name or non_transitional_name is set.
211      *
212      * We need a separate base type instead of making all types
213      * inherit from generic_name for two reasons:
214      * 1) generic_name implements INTERFACE_PCIE_DEVICE, but
215      *    transitional_name does not.
216      * 2) generic_name has the "disable-legacy" and "disable-modern"
217      *    properties, transitional_name and non_transitional name don't.
218      */
219     const char *base_name;
220     /*
221      * Generic device type.  Optional.
222      *
223      * Supports both transitional and non-transitional modes,
224      * using the disable-legacy and disable-modern properties.
225      * If disable-legacy=auto, (non-)transitional mode is selected
226      * depending on the bus where the device is plugged.
227      *
228      * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE,
229      * but PCI Express is supported only in non-transitional mode.
230      *
231      * The only type implemented by QEMU 3.1 and older.
232      */
233     const char *generic_name;
234     /*
235      * The transitional device type.  Optional.
236      *
237      * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE.
238      */
239     const char *transitional_name;
240     /*
241      * The non-transitional device type.  Optional.
242      *
243      * Implements INTERFACE_CONVENTIONAL_PCI_DEVICE only.
244      */
245     const char *non_transitional_name;
246 
247     /* Parent type.  If NULL, TYPE_VIRTIO_PCI is used */
248     const char *parent;
249 
250     /* Same as TypeInfo fields: */
251     size_t instance_size;
252     size_t class_size;
253     void (*instance_init)(Object *obj);
254     void (*class_init)(ObjectClass *klass, void *data);
255 } VirtioPCIDeviceTypeInfo;
256 
257 /* Register virtio-pci type(s).  @t must be static. */
258 void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo *t);
259 
260 #endif
261