OSDN Git Service

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