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