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