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