OSDN Git Service

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