OSDN Git Service

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