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