xref: /qemu/include/hw/virtio/virtio-pci.h (revision fdfa3b1d6f9edd97c807df496a0d8e9ea49240da)
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 
23 /* virtio-pci-bus */
24 
25 typedef struct VirtioBusState VirtioPCIBusState;
26 typedef struct VirtioBusClass VirtioPCIBusClass;
27 
28 #define TYPE_VIRTIO_PCI_BUS "virtio-pci-bus"
29 DECLARE_OBJ_CHECKERS(VirtioPCIBusState, VirtioPCIBusClass,
30                      VIRTIO_PCI_BUS, TYPE_VIRTIO_PCI_BUS)
31 
32 enum {
33     VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT,
34     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT,
35     VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT,
36     VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT,
37     VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT,
38     VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT,
39     VIRTIO_PCI_FLAG_ATS_BIT,
40     VIRTIO_PCI_FLAG_INIT_DEVERR_BIT,
41     VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT,
42     VIRTIO_PCI_FLAG_INIT_PM_BIT,
43     VIRTIO_PCI_FLAG_INIT_FLR_BIT,
44     VIRTIO_PCI_FLAG_AER_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 /* Advanced Error Reporting capability */
85 #define VIRTIO_PCI_FLAG_AER (1 << VIRTIO_PCI_FLAG_AER_BIT)
86 
87 typedef struct {
88     MSIMessage msg;
89     int virq;
90     unsigned int users;
91 } VirtIOIRQFD;
92 
93 /*
94  * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
95  */
96 #define TYPE_VIRTIO_PCI "virtio-pci"
97 OBJECT_DECLARE_TYPE(VirtIOPCIProxy, VirtioPCIClass, VIRTIO_PCI)
98 
99 struct VirtioPCIClass {
100     PCIDeviceClass parent_class;
101     DeviceRealize parent_dc_realize;
102     void (*realize)(VirtIOPCIProxy *vpci_dev, Error **errp);
103 };
104 
105 typedef struct VirtIOPCIRegion {
106     MemoryRegion mr;
107     uint32_t offset;
108     uint32_t size;
109     uint32_t type;
110 } VirtIOPCIRegion;
111 
112 typedef struct VirtIOPCIQueue {
113   uint16_t num;
114   bool enabled;
115   uint32_t desc[2];
116   uint32_t avail[2];
117   uint32_t used[2];
118 } VirtIOPCIQueue;
119 
120 struct VirtIOPCIProxy {
121     PCIDevice pci_dev;
122     MemoryRegion bar;
123     union {
124         struct {
125             VirtIOPCIRegion common;
126             VirtIOPCIRegion isr;
127             VirtIOPCIRegion device;
128             VirtIOPCIRegion notify;
129             VirtIOPCIRegion notify_pio;
130         };
131         VirtIOPCIRegion regs[5];
132     };
133     MemoryRegion modern_bar;
134     MemoryRegion io_bar;
135     uint32_t legacy_io_bar_idx;
136     uint32_t msix_bar_idx;
137     uint32_t modern_io_bar_idx;
138     uint32_t modern_mem_bar_idx;
139     int config_cap;
140     uint32_t flags;
141     bool disable_modern;
142     bool ignore_backend_features;
143     OnOffAuto disable_legacy;
144     uint32_t class_code;
145     uint32_t nvectors;
146     uint32_t dfselect;
147     uint32_t gfselect;
148     uint32_t guest_features[2];
149     VirtIOPCIQueue vqs[VIRTIO_QUEUE_MAX];
150 
151     VirtIOIRQFD *vector_irqfd;
152     int nvqs_with_notifiers;
153     VirtioBusState bus;
154 };
155 
156 static inline bool virtio_pci_modern(VirtIOPCIProxy *proxy)
157 {
158     return !proxy->disable_modern;
159 }
160 
161 static inline bool virtio_pci_legacy(VirtIOPCIProxy *proxy)
162 {
163     return proxy->disable_legacy == ON_OFF_AUTO_OFF;
164 }
165 
166 static inline void virtio_pci_force_virtio_1(VirtIOPCIProxy *proxy)
167 {
168     proxy->disable_modern = false;
169     proxy->disable_legacy = ON_OFF_AUTO_ON;
170 }
171 
172 static inline void virtio_pci_disable_modern(VirtIOPCIProxy *proxy)
173 {
174     proxy->disable_modern = true;
175 }
176 
177 /*
178  * virtio-input-pci: This extends VirtioPCIProxy.
179  */
180 #define TYPE_VIRTIO_INPUT_PCI "virtio-input-pci"
181 
182 /* Virtio ABI version, if we increment this, we break the guest driver. */
183 #define VIRTIO_PCI_ABI_VERSION          0
184 
185 /* Input for virtio_pci_types_register() */
186 typedef struct VirtioPCIDeviceTypeInfo {
187     /*
188      * Common base class for the subclasses below.
189      *
190      * Required only if transitional_name or non_transitional_name is set.
191      *
192      * We need a separate base type instead of making all types
193      * inherit from generic_name for two reasons:
194      * 1) generic_name implements INTERFACE_PCIE_DEVICE, but
195      *    transitional_name does not.
196      * 2) generic_name has the "disable-legacy" and "disable-modern"
197      *    properties, transitional_name and non_transitional name don't.
198      */
199     const char *base_name;
200     /*
201      * Generic device type.  Optional.
202      *
203      * Supports both transitional and non-transitional modes,
204      * using the disable-legacy and disable-modern properties.
205      * If disable-legacy=auto, (non-)transitional mode is selected
206      * depending on the bus where the device is plugged.
207      *
208      * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE,
209      * but PCI Express is supported only in non-transitional mode.
210      *
211      * The only type implemented by QEMU 3.1 and older.
212      */
213     const char *generic_name;
214     /*
215      * The transitional device type.  Optional.
216      *
217      * Implements both INTERFACE_PCIE_DEVICE and INTERFACE_CONVENTIONAL_PCI_DEVICE.
218      */
219     const char *transitional_name;
220     /*
221      * The non-transitional device type.  Optional.
222      *
223      * Implements INTERFACE_CONVENTIONAL_PCI_DEVICE only.
224      */
225     const char *non_transitional_name;
226 
227     /* Parent type.  If NULL, TYPE_VIRTIO_PCI is used */
228     const char *parent;
229 
230     /* Same as TypeInfo fields: */
231     size_t instance_size;
232     size_t class_size;
233     void (*instance_init)(Object *obj);
234     void (*class_init)(ObjectClass *klass, void *data);
235     InterfaceInfo *interfaces;
236 } VirtioPCIDeviceTypeInfo;
237 
238 /* Register virtio-pci type(s).  @t must be static. */
239 void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo *t);
240 
241 /**
242  * virtio_pci_optimal_num_queues:
243  * @fixed_queues: number of queues that are always present
244  *
245  * Returns: The optimal number of queues for a multi-queue device, excluding
246  * @fixed_queues.
247  */
248 unsigned virtio_pci_optimal_num_queues(unsigned fixed_queues);
249 
250 #endif
251