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