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