4 * Copyright IBM, Corp. 2007
5 * Copyright (c) 2009 CodeSourcery
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paul Brook <paul@codesourcery.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
14 * Contributions after 2012-01-13 are licensed under the terms of the
15 * GNU GPL, version 2 or (at your option) any later version.
20 #include "hw/virtio/virtio.h"
21 #include "hw/virtio/virtio-blk.h"
22 #include "hw/virtio/virtio-net.h"
23 #include "hw/virtio/virtio-serial.h"
24 #include "hw/virtio/virtio-scsi.h"
25 #include "hw/virtio/virtio-balloon.h"
26 #include "hw/pci/pci.h"
27 #include "qemu/error-report.h"
28 #include "hw/pci/msi.h"
29 #include "hw/pci/msix.h"
30 #include "hw/loader.h"
31 #include "sysemu/kvm.h"
32 #include "sysemu/blockdev.h"
33 #include "virtio-pci.h"
34 #include "qemu/range.h"
35 #include "hw/virtio/virtio-bus.h"
36 #include "qapi/visitor.h"
38 /* from Linux's linux/virtio_pci.h */
40 /* A 32-bit r/o bitmask of the features supported by the host */
41 #define VIRTIO_PCI_HOST_FEATURES 0
43 /* A 32-bit r/w bitmask of features activated by the guest */
44 #define VIRTIO_PCI_GUEST_FEATURES 4
46 /* A 32-bit r/w PFN for the currently selected queue */
47 #define VIRTIO_PCI_QUEUE_PFN 8
49 /* A 16-bit r/o queue size for the currently selected queue */
50 #define VIRTIO_PCI_QUEUE_NUM 12
52 /* A 16-bit r/w queue selector */
53 #define VIRTIO_PCI_QUEUE_SEL 14
55 /* A 16-bit r/w queue notifier */
56 #define VIRTIO_PCI_QUEUE_NOTIFY 16
58 /* An 8-bit device status register. */
59 #define VIRTIO_PCI_STATUS 18
61 /* An 8-bit r/o interrupt status register. Reading the value will return the
62 * current contents of the ISR and will also clear it. This is effectively
63 * a read-and-acknowledge. */
64 #define VIRTIO_PCI_ISR 19
66 /* MSI-X registers: only enabled if MSI-X is enabled. */
67 /* A 16-bit vector for configuration changes. */
68 #define VIRTIO_MSI_CONFIG_VECTOR 20
69 /* A 16-bit vector for selected queue notifications. */
70 #define VIRTIO_MSI_QUEUE_VECTOR 22
72 /* Config space size */
73 #define VIRTIO_PCI_CONFIG_NOMSI 20
74 #define VIRTIO_PCI_CONFIG_MSI 24
75 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
76 VIRTIO_PCI_CONFIG_MSI : \
77 VIRTIO_PCI_CONFIG_NOMSI)
79 /* The remaining space is defined by each driver as the per-driver
80 * configuration space */
81 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
82 VIRTIO_PCI_CONFIG_MSI : \
83 VIRTIO_PCI_CONFIG_NOMSI)
85 /* How many bits to shift physical queue address written to QUEUE_PFN.
86 * 12 is historical, and due to x86 page size. */
87 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
89 /* Flags track per-device state like workarounds for quirks in older guests. */
90 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
92 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
96 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
97 static inline VirtIOPCIProxy
*to_virtio_pci_proxy(DeviceState
*d
)
99 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
102 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
103 * be careful and test performance if you change this.
105 static inline VirtIOPCIProxy
*to_virtio_pci_proxy_fast(DeviceState
*d
)
107 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
110 static void virtio_pci_notify(DeviceState
*d
, uint16_t vector
)
112 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy_fast(d
);
114 if (msix_enabled(&proxy
->pci_dev
))
115 msix_notify(&proxy
->pci_dev
, vector
);
117 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
118 pci_set_irq(&proxy
->pci_dev
, vdev
->isr
& 1);
122 static void virtio_pci_save_config(DeviceState
*d
, QEMUFile
*f
)
124 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
125 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
127 pci_device_save(&proxy
->pci_dev
, f
);
128 msix_save(&proxy
->pci_dev
, f
);
129 if (msix_present(&proxy
->pci_dev
))
130 qemu_put_be16(f
, vdev
->config_vector
);
133 static void virtio_pci_save_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
135 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
136 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
138 if (msix_present(&proxy
->pci_dev
))
139 qemu_put_be16(f
, virtio_queue_vector(vdev
, n
));
142 static int virtio_pci_load_config(DeviceState
*d
, QEMUFile
*f
)
144 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
145 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
148 ret
= pci_device_load(&proxy
->pci_dev
, f
);
152 msix_unuse_all_vectors(&proxy
->pci_dev
);
153 msix_load(&proxy
->pci_dev
, f
);
154 if (msix_present(&proxy
->pci_dev
)) {
155 qemu_get_be16s(f
, &vdev
->config_vector
);
157 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
159 if (vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
160 return msix_vector_use(&proxy
->pci_dev
, vdev
->config_vector
);
165 static int virtio_pci_load_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
167 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
168 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
171 if (msix_present(&proxy
->pci_dev
)) {
172 qemu_get_be16s(f
, &vector
);
174 vector
= VIRTIO_NO_VECTOR
;
176 virtio_queue_set_vector(vdev
, n
, vector
);
177 if (vector
!= VIRTIO_NO_VECTOR
) {
178 return msix_vector_use(&proxy
->pci_dev
, vector
);
183 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy
*proxy
,
184 int n
, bool assign
, bool set_handler
)
186 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
187 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
188 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
192 r
= event_notifier_init(notifier
, 1);
194 error_report("%s: unable to init event notifier: %d",
198 virtio_queue_set_host_notifier_fd_handler(vq
, true, set_handler
);
199 memory_region_add_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
202 memory_region_del_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
204 virtio_queue_set_host_notifier_fd_handler(vq
, false, false);
205 event_notifier_cleanup(notifier
);
210 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
212 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
215 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) ||
216 proxy
->ioeventfd_disabled
||
217 proxy
->ioeventfd_started
) {
221 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
222 if (!virtio_queue_get_num(vdev
, n
)) {
226 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, true, true);
231 proxy
->ioeventfd_started
= true;
236 if (!virtio_queue_get_num(vdev
, n
)) {
240 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
243 proxy
->ioeventfd_started
= false;
244 error_report("%s: failed. Fallback to a userspace (slower).", __func__
);
247 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
249 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
253 if (!proxy
->ioeventfd_started
) {
257 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
258 if (!virtio_queue_get_num(vdev
, n
)) {
262 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
265 proxy
->ioeventfd_started
= false;
268 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
270 VirtIOPCIProxy
*proxy
= opaque
;
271 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
275 case VIRTIO_PCI_GUEST_FEATURES
:
276 /* Guest does not negotiate properly? We have to assume nothing. */
277 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
278 val
= virtio_bus_get_vdev_bad_features(&proxy
->bus
);
280 virtio_set_features(vdev
, val
);
282 case VIRTIO_PCI_QUEUE_PFN
:
283 pa
= (hwaddr
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
285 virtio_pci_stop_ioeventfd(proxy
);
287 msix_unuse_all_vectors(&proxy
->pci_dev
);
290 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
292 case VIRTIO_PCI_QUEUE_SEL
:
293 if (val
< VIRTIO_PCI_QUEUE_MAX
)
294 vdev
->queue_sel
= val
;
296 case VIRTIO_PCI_QUEUE_NOTIFY
:
297 if (val
< VIRTIO_PCI_QUEUE_MAX
) {
298 virtio_queue_notify(vdev
, val
);
301 case VIRTIO_PCI_STATUS
:
302 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
303 virtio_pci_stop_ioeventfd(proxy
);
306 virtio_set_status(vdev
, val
& 0xFF);
308 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
309 virtio_pci_start_ioeventfd(proxy
);
312 if (vdev
->status
== 0) {
314 msix_unuse_all_vectors(&proxy
->pci_dev
);
317 /* Linux before 2.6.34 drives the device without enabling
318 the PCI device bus master bit. Enable it automatically
319 for the guest. This is a PCI spec violation but so is
320 initiating DMA with bus master bit clear. */
321 if (val
== (VIRTIO_CONFIG_S_ACKNOWLEDGE
| VIRTIO_CONFIG_S_DRIVER
)) {
322 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
323 proxy
->pci_dev
.config
[PCI_COMMAND
] |
324 PCI_COMMAND_MASTER
, 1);
327 /* Linux before 2.6.34 sets the device as OK without enabling
328 the PCI device bus master bit. In this case we need to disable
329 some safety checks. */
330 if ((val
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
331 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
332 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
335 case VIRTIO_MSI_CONFIG_VECTOR
:
336 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
337 /* Make it possible for guest to discover an error took place. */
338 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
339 val
= VIRTIO_NO_VECTOR
;
340 vdev
->config_vector
= val
;
342 case VIRTIO_MSI_QUEUE_VECTOR
:
343 msix_vector_unuse(&proxy
->pci_dev
,
344 virtio_queue_vector(vdev
, vdev
->queue_sel
));
345 /* Make it possible for guest to discover an error took place. */
346 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
347 val
= VIRTIO_NO_VECTOR
;
348 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
351 error_report("%s: unexpected address 0x%x value 0x%x",
352 __func__
, addr
, val
);
357 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
359 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
360 uint32_t ret
= 0xFFFFFFFF;
363 case VIRTIO_PCI_HOST_FEATURES
:
364 ret
= proxy
->host_features
;
366 case VIRTIO_PCI_GUEST_FEATURES
:
367 ret
= vdev
->guest_features
;
369 case VIRTIO_PCI_QUEUE_PFN
:
370 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
371 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
373 case VIRTIO_PCI_QUEUE_NUM
:
374 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
376 case VIRTIO_PCI_QUEUE_SEL
:
377 ret
= vdev
->queue_sel
;
379 case VIRTIO_PCI_STATUS
:
383 /* reading from the ISR also clears it. */
386 pci_irq_deassert(&proxy
->pci_dev
);
388 case VIRTIO_MSI_CONFIG_VECTOR
:
389 ret
= vdev
->config_vector
;
391 case VIRTIO_MSI_QUEUE_VECTOR
:
392 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
401 static uint64_t virtio_pci_config_read(void *opaque
, hwaddr addr
,
404 VirtIOPCIProxy
*proxy
= opaque
;
405 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
406 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
409 return virtio_ioport_read(proxy
, addr
);
415 val
= virtio_config_readb(vdev
, addr
);
418 val
= virtio_config_readw(vdev
, addr
);
419 if (virtio_is_big_endian(vdev
)) {
424 val
= virtio_config_readl(vdev
, addr
);
425 if (virtio_is_big_endian(vdev
)) {
433 static void virtio_pci_config_write(void *opaque
, hwaddr addr
,
434 uint64_t val
, unsigned size
)
436 VirtIOPCIProxy
*proxy
= opaque
;
437 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
438 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
440 virtio_ioport_write(proxy
, addr
, val
);
445 * Virtio-PCI is odd. Ioports are LE but config space is target native
450 virtio_config_writeb(vdev
, addr
, val
);
453 if (virtio_is_big_endian(vdev
)) {
456 virtio_config_writew(vdev
, addr
, val
);
459 if (virtio_is_big_endian(vdev
)) {
462 virtio_config_writel(vdev
, addr
, val
);
467 static const MemoryRegionOps virtio_pci_config_ops
= {
468 .read
= virtio_pci_config_read
,
469 .write
= virtio_pci_config_write
,
471 .min_access_size
= 1,
472 .max_access_size
= 4,
474 .endianness
= DEVICE_LITTLE_ENDIAN
,
477 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
478 uint32_t val
, int len
)
480 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
481 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
483 pci_default_write_config(pci_dev
, address
, val
, len
);
485 if (range_covers_byte(address
, len
, PCI_COMMAND
) &&
486 !(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
) &&
487 !(proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG
)) {
488 virtio_pci_stop_ioeventfd(proxy
);
489 virtio_set_status(vdev
, vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
493 static unsigned virtio_pci_get_features(DeviceState
*d
)
495 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
496 return proxy
->host_features
;
499 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy
*proxy
,
500 unsigned int queue_no
,
504 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
507 if (irqfd
->users
== 0) {
508 ret
= kvm_irqchip_add_msi_route(kvm_state
, msg
);
518 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy
*proxy
,
521 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
522 if (--irqfd
->users
== 0) {
523 kvm_irqchip_release_virq(kvm_state
, irqfd
->virq
);
527 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy
*proxy
,
528 unsigned int queue_no
,
531 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
532 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
533 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
534 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
536 ret
= kvm_irqchip_add_irqfd_notifier(kvm_state
, n
, NULL
, irqfd
->virq
);
540 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy
*proxy
,
541 unsigned int queue_no
,
544 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
545 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
546 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
547 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
550 ret
= kvm_irqchip_remove_irqfd_notifier(kvm_state
, n
, irqfd
->virq
);
554 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy
*proxy
, int nvqs
)
556 PCIDevice
*dev
= &proxy
->pci_dev
;
557 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
558 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
563 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
564 if (!virtio_queue_get_num(vdev
, queue_no
)) {
567 vector
= virtio_queue_vector(vdev
, queue_no
);
568 if (vector
>= msix_nr_vectors_allocated(dev
)) {
571 msg
= msix_get_message(dev
, vector
);
572 ret
= kvm_virtio_pci_vq_vector_use(proxy
, queue_no
, vector
, msg
);
576 /* If guest supports masking, set up irqfd now.
577 * Otherwise, delay until unmasked in the frontend.
579 if (k
->guest_notifier_mask
) {
580 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
582 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
590 while (--queue_no
>= 0) {
591 vector
= virtio_queue_vector(vdev
, queue_no
);
592 if (vector
>= msix_nr_vectors_allocated(dev
)) {
595 if (k
->guest_notifier_mask
) {
596 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
598 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
603 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy
*proxy
, int nvqs
)
605 PCIDevice
*dev
= &proxy
->pci_dev
;
606 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
609 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
611 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
612 if (!virtio_queue_get_num(vdev
, queue_no
)) {
615 vector
= virtio_queue_vector(vdev
, queue_no
);
616 if (vector
>= msix_nr_vectors_allocated(dev
)) {
619 /* If guest supports masking, clean up irqfd now.
620 * Otherwise, it was cleaned when masked in the frontend.
622 if (k
->guest_notifier_mask
) {
623 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
625 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
629 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy
*proxy
,
630 unsigned int queue_no
,
634 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
635 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
636 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
637 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
641 if (proxy
->vector_irqfd
) {
642 irqfd
= &proxy
->vector_irqfd
[vector
];
643 if (irqfd
->msg
.data
!= msg
.data
|| irqfd
->msg
.address
!= msg
.address
) {
644 ret
= kvm_irqchip_update_msi_route(kvm_state
, irqfd
->virq
, msg
);
651 /* If guest supports masking, irqfd is already setup, unmask it.
652 * Otherwise, set it up now.
654 if (k
->guest_notifier_mask
) {
655 k
->guest_notifier_mask(vdev
, queue_no
, false);
656 /* Test after unmasking to avoid losing events. */
657 if (k
->guest_notifier_pending
&&
658 k
->guest_notifier_pending(vdev
, queue_no
)) {
659 event_notifier_set(n
);
662 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
667 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy
*proxy
,
668 unsigned int queue_no
,
671 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
672 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
674 /* If guest supports masking, keep irqfd but mask it.
675 * Otherwise, clean it up now.
677 if (k
->guest_notifier_mask
) {
678 k
->guest_notifier_mask(vdev
, queue_no
, true);
680 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
684 static int virtio_pci_vector_unmask(PCIDevice
*dev
, unsigned vector
,
687 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
688 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
691 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
692 if (!virtio_queue_get_num(vdev
, queue_no
)) {
695 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
698 ret
= virtio_pci_vq_vector_unmask(proxy
, queue_no
, vector
, msg
);
706 while (--queue_no
>= 0) {
707 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
710 virtio_pci_vq_vector_mask(proxy
, queue_no
, vector
);
715 static void virtio_pci_vector_mask(PCIDevice
*dev
, unsigned vector
)
717 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
718 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
721 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
722 if (!virtio_queue_get_num(vdev
, queue_no
)) {
725 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
728 virtio_pci_vq_vector_mask(proxy
, queue_no
, vector
);
732 static void virtio_pci_vector_poll(PCIDevice
*dev
,
733 unsigned int vector_start
,
734 unsigned int vector_end
)
736 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
737 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
738 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
741 EventNotifier
*notifier
;
744 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
745 if (!virtio_queue_get_num(vdev
, queue_no
)) {
748 vector
= virtio_queue_vector(vdev
, queue_no
);
749 if (vector
< vector_start
|| vector
>= vector_end
||
750 !msix_is_masked(dev
, vector
)) {
753 vq
= virtio_get_queue(vdev
, queue_no
);
754 notifier
= virtio_queue_get_guest_notifier(vq
);
755 if (k
->guest_notifier_pending
) {
756 if (k
->guest_notifier_pending(vdev
, queue_no
)) {
757 msix_set_pending(dev
, vector
);
759 } else if (event_notifier_test_and_clear(notifier
)) {
760 msix_set_pending(dev
, vector
);
765 static int virtio_pci_set_guest_notifier(DeviceState
*d
, int n
, bool assign
,
768 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
769 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
770 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
771 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
772 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
775 int r
= event_notifier_init(notifier
, 0);
779 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
781 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
782 event_notifier_cleanup(notifier
);
785 if (!msix_enabled(&proxy
->pci_dev
) && vdc
->guest_notifier_mask
) {
786 vdc
->guest_notifier_mask(vdev
, n
, !assign
);
792 static bool virtio_pci_query_guest_notifiers(DeviceState
*d
)
794 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
795 return msix_enabled(&proxy
->pci_dev
);
798 static int virtio_pci_set_guest_notifiers(DeviceState
*d
, int nvqs
, bool assign
)
800 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
801 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
802 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
804 bool with_irqfd
= msix_enabled(&proxy
->pci_dev
) &&
805 kvm_msi_via_irqfd_enabled();
807 nvqs
= MIN(nvqs
, VIRTIO_PCI_QUEUE_MAX
);
809 /* When deassigning, pass a consistent nvqs value
810 * to avoid leaking notifiers.
812 assert(assign
|| nvqs
== proxy
->nvqs_with_notifiers
);
814 proxy
->nvqs_with_notifiers
= nvqs
;
816 /* Must unset vector notifier while guest notifier is still assigned */
817 if ((proxy
->vector_irqfd
|| k
->guest_notifier_mask
) && !assign
) {
818 msix_unset_vector_notifiers(&proxy
->pci_dev
);
819 if (proxy
->vector_irqfd
) {
820 kvm_virtio_pci_vector_release(proxy
, nvqs
);
821 g_free(proxy
->vector_irqfd
);
822 proxy
->vector_irqfd
= NULL
;
826 for (n
= 0; n
< nvqs
; n
++) {
827 if (!virtio_queue_get_num(vdev
, n
)) {
831 r
= virtio_pci_set_guest_notifier(d
, n
, assign
, with_irqfd
);
837 /* Must set vector notifier after guest notifier has been assigned */
838 if ((with_irqfd
|| k
->guest_notifier_mask
) && assign
) {
840 proxy
->vector_irqfd
=
841 g_malloc0(sizeof(*proxy
->vector_irqfd
) *
842 msix_nr_vectors_allocated(&proxy
->pci_dev
));
843 r
= kvm_virtio_pci_vector_use(proxy
, nvqs
);
848 r
= msix_set_vector_notifiers(&proxy
->pci_dev
,
849 virtio_pci_vector_unmask
,
850 virtio_pci_vector_mask
,
851 virtio_pci_vector_poll
);
853 goto notifiers_error
;
862 kvm_virtio_pci_vector_release(proxy
, nvqs
);
866 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
869 virtio_pci_set_guest_notifier(d
, n
, !assign
, with_irqfd
);
874 static int virtio_pci_set_host_notifier(DeviceState
*d
, int n
, bool assign
)
876 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
878 /* Stop using ioeventfd for virtqueue kick if the device starts using host
879 * notifiers. This makes it easy to avoid stepping on each others' toes.
881 proxy
->ioeventfd_disabled
= assign
;
883 virtio_pci_stop_ioeventfd(proxy
);
885 /* We don't need to start here: it's not needed because backend
886 * currently only stops on status change away from ok,
887 * reset, vmstop and such. If we do add code to start here,
888 * need to check vmstate, device state etc. */
889 return virtio_pci_set_host_notifier_internal(proxy
, n
, assign
, false);
892 static void virtio_pci_vmstate_change(DeviceState
*d
, bool running
)
894 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
895 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
898 /* Try to find out if the guest has bus master disabled, but is
899 in ready state. Then we have a buggy guest OS. */
900 if ((vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
901 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
902 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
904 virtio_pci_start_ioeventfd(proxy
);
906 virtio_pci_stop_ioeventfd(proxy
);
911 static int virtio_9p_init_pci(VirtIOPCIProxy
*vpci_dev
)
913 V9fsPCIState
*dev
= VIRTIO_9P_PCI(vpci_dev
);
914 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
916 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
917 if (qdev_init(vdev
) < 0) {
923 static Property virtio_9p_pci_properties
[] = {
924 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
925 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
926 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
927 DEFINE_PROP_END_OF_LIST(),
930 static void virtio_9p_pci_class_init(ObjectClass
*klass
, void *data
)
932 DeviceClass
*dc
= DEVICE_CLASS(klass
);
933 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
934 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
936 k
->init
= virtio_9p_init_pci
;
937 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
938 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_9P
;
939 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
940 pcidev_k
->class_id
= 0x2;
941 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
942 dc
->props
= virtio_9p_pci_properties
;
945 static void virtio_9p_pci_instance_init(Object
*obj
)
947 V9fsPCIState
*dev
= VIRTIO_9P_PCI(obj
);
949 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
953 static const TypeInfo virtio_9p_pci_info
= {
954 .name
= TYPE_VIRTIO_9P_PCI
,
955 .parent
= TYPE_VIRTIO_PCI
,
956 .instance_size
= sizeof(V9fsPCIState
),
957 .instance_init
= virtio_9p_pci_instance_init
,
958 .class_init
= virtio_9p_pci_class_init
,
960 #endif /* CONFIG_VIRTFS */
963 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
966 /* This is called by virtio-bus just after the device is plugged. */
967 static void virtio_pci_device_plugged(DeviceState
*d
)
969 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
970 VirtioBusState
*bus
= &proxy
->bus
;
974 config
= proxy
->pci_dev
.config
;
975 if (proxy
->class_code
) {
976 pci_config_set_class(config
, proxy
->class_code
);
978 pci_set_word(config
+ PCI_SUBSYSTEM_VENDOR_ID
,
979 pci_get_word(config
+ PCI_VENDOR_ID
));
980 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, virtio_bus_get_vdev_id(bus
));
981 config
[PCI_INTERRUPT_PIN
] = 1;
983 if (proxy
->nvectors
&&
984 msix_init_exclusive_bar(&proxy
->pci_dev
, proxy
->nvectors
, 1)) {
985 error_report("unable to init msix vectors to %" PRIu32
,
990 proxy
->pci_dev
.config_write
= virtio_write_config
;
992 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
)
993 + virtio_bus_get_vdev_config_len(bus
);
994 if (size
& (size
- 1)) {
995 size
= 1 << qemu_fls(size
);
998 memory_region_init_io(&proxy
->bar
, OBJECT(proxy
), &virtio_pci_config_ops
,
999 proxy
, "virtio-pci", size
);
1000 pci_register_bar(&proxy
->pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
,
1003 if (!kvm_has_many_ioeventfds()) {
1004 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
1007 proxy
->host_features
|= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY
;
1008 proxy
->host_features
|= 0x1 << VIRTIO_F_BAD_FEATURE
;
1009 proxy
->host_features
= virtio_bus_get_vdev_features(bus
,
1010 proxy
->host_features
);
1013 static void virtio_pci_device_unplugged(DeviceState
*d
)
1015 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1017 virtio_pci_stop_ioeventfd(proxy
);
1020 static int virtio_pci_init(PCIDevice
*pci_dev
)
1022 VirtIOPCIProxy
*dev
= VIRTIO_PCI(pci_dev
);
1023 VirtioPCIClass
*k
= VIRTIO_PCI_GET_CLASS(pci_dev
);
1024 virtio_pci_bus_new(&dev
->bus
, sizeof(dev
->bus
), dev
);
1025 if (k
->init
!= NULL
) {
1026 return k
->init(dev
);
1031 static void virtio_pci_exit(PCIDevice
*pci_dev
)
1033 msix_uninit_exclusive_bar(pci_dev
);
1036 static void virtio_pci_reset(DeviceState
*qdev
)
1038 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1039 VirtioBusState
*bus
= VIRTIO_BUS(&proxy
->bus
);
1040 virtio_pci_stop_ioeventfd(proxy
);
1041 virtio_bus_reset(bus
);
1042 msix_unuse_all_vectors(&proxy
->pci_dev
);
1043 proxy
->flags
&= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
1046 static Property virtio_pci_properties
[] = {
1047 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1048 DEFINE_PROP_END_OF_LIST(),
1051 static void virtio_pci_class_init(ObjectClass
*klass
, void *data
)
1053 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1054 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1056 dc
->props
= virtio_pci_properties
;
1057 k
->init
= virtio_pci_init
;
1058 k
->exit
= virtio_pci_exit
;
1059 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1060 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1061 k
->class_id
= PCI_CLASS_OTHERS
;
1062 dc
->reset
= virtio_pci_reset
;
1065 static const TypeInfo virtio_pci_info
= {
1066 .name
= TYPE_VIRTIO_PCI
,
1067 .parent
= TYPE_PCI_DEVICE
,
1068 .instance_size
= sizeof(VirtIOPCIProxy
),
1069 .class_init
= virtio_pci_class_init
,
1070 .class_size
= sizeof(VirtioPCIClass
),
1074 /* virtio-blk-pci */
1076 static Property virtio_blk_pci_properties
[] = {
1077 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1078 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1079 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1080 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1081 DEFINE_PROP_END_OF_LIST(),
1084 static int virtio_blk_pci_init(VirtIOPCIProxy
*vpci_dev
)
1086 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(vpci_dev
);
1087 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1088 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1089 if (qdev_init(vdev
) < 0) {
1095 static void virtio_blk_pci_class_init(ObjectClass
*klass
, void *data
)
1097 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1098 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1099 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1101 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1102 dc
->props
= virtio_blk_pci_properties
;
1103 k
->init
= virtio_blk_pci_init
;
1104 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1105 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
1106 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1107 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1110 static void virtio_blk_pci_instance_init(Object
*obj
)
1112 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(obj
);
1114 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1116 object_property_add_alias(obj
, "iothread", OBJECT(&dev
->vdev
),"iothread",
1118 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
1119 "bootindex", &error_abort
);
1122 static const TypeInfo virtio_blk_pci_info
= {
1123 .name
= TYPE_VIRTIO_BLK_PCI
,
1124 .parent
= TYPE_VIRTIO_PCI
,
1125 .instance_size
= sizeof(VirtIOBlkPCI
),
1126 .instance_init
= virtio_blk_pci_instance_init
,
1127 .class_init
= virtio_blk_pci_class_init
,
1130 /* virtio-scsi-pci */
1132 static Property virtio_scsi_pci_properties
[] = {
1133 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1134 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1135 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1136 DEV_NVECTORS_UNSPECIFIED
),
1137 DEFINE_VIRTIO_SCSI_FEATURES(VirtIOPCIProxy
, host_features
),
1138 DEFINE_PROP_END_OF_LIST(),
1141 static int virtio_scsi_pci_init_pci(VirtIOPCIProxy
*vpci_dev
)
1143 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(vpci_dev
);
1144 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1145 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1146 DeviceState
*proxy
= DEVICE(vpci_dev
);
1149 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1150 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1154 * For command line compatibility, this sets the virtio-scsi-device bus
1158 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1159 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1163 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1164 if (qdev_init(vdev
) < 0) {
1170 static void virtio_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1172 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1173 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1174 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1175 k
->init
= virtio_scsi_pci_init_pci
;
1176 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1177 dc
->props
= virtio_scsi_pci_properties
;
1178 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1179 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1180 pcidev_k
->revision
= 0x00;
1181 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1184 static void virtio_scsi_pci_instance_init(Object
*obj
)
1186 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(obj
);
1188 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1190 object_property_add_alias(obj
, "iothread", OBJECT(&dev
->vdev
), "iothread",
1194 static const TypeInfo virtio_scsi_pci_info
= {
1195 .name
= TYPE_VIRTIO_SCSI_PCI
,
1196 .parent
= TYPE_VIRTIO_PCI
,
1197 .instance_size
= sizeof(VirtIOSCSIPCI
),
1198 .instance_init
= virtio_scsi_pci_instance_init
,
1199 .class_init
= virtio_scsi_pci_class_init
,
1202 /* vhost-scsi-pci */
1204 #ifdef CONFIG_VHOST_SCSI
1205 static Property vhost_scsi_pci_properties
[] = {
1206 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1207 DEV_NVECTORS_UNSPECIFIED
),
1208 DEFINE_PROP_END_OF_LIST(),
1211 static int vhost_scsi_pci_init_pci(VirtIOPCIProxy
*vpci_dev
)
1213 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(vpci_dev
);
1214 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1215 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1217 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1218 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1221 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1222 if (qdev_init(vdev
) < 0) {
1228 static void vhost_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1230 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1231 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1232 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1233 k
->init
= vhost_scsi_pci_init_pci
;
1234 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1235 dc
->props
= vhost_scsi_pci_properties
;
1236 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1237 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1238 pcidev_k
->revision
= 0x00;
1239 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1242 static void vhost_scsi_pci_instance_init(Object
*obj
)
1244 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(obj
);
1246 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1250 static const TypeInfo vhost_scsi_pci_info
= {
1251 .name
= TYPE_VHOST_SCSI_PCI
,
1252 .parent
= TYPE_VIRTIO_PCI
,
1253 .instance_size
= sizeof(VHostSCSIPCI
),
1254 .instance_init
= vhost_scsi_pci_instance_init
,
1255 .class_init
= vhost_scsi_pci_class_init
,
1259 /* virtio-balloon-pci */
1261 static void balloon_pci_stats_get_all(Object
*obj
, struct Visitor
*v
,
1262 void *opaque
, const char *name
,
1265 VirtIOBalloonPCI
*dev
= opaque
;
1266 object_property_get(OBJECT(&dev
->vdev
), v
, "guest-stats", errp
);
1269 static void balloon_pci_stats_get_poll_interval(Object
*obj
, struct Visitor
*v
,
1270 void *opaque
, const char *name
,
1273 VirtIOBalloonPCI
*dev
= opaque
;
1274 object_property_get(OBJECT(&dev
->vdev
), v
, "guest-stats-polling-interval",
1278 static void balloon_pci_stats_set_poll_interval(Object
*obj
, struct Visitor
*v
,
1279 void *opaque
, const char *name
,
1282 VirtIOBalloonPCI
*dev
= opaque
;
1283 object_property_set(OBJECT(&dev
->vdev
), v
, "guest-stats-polling-interval",
1287 static Property virtio_balloon_pci_properties
[] = {
1288 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1289 DEFINE_PROP_END_OF_LIST(),
1292 static int virtio_balloon_pci_init(VirtIOPCIProxy
*vpci_dev
)
1294 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(vpci_dev
);
1295 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1297 if (vpci_dev
->class_code
!= PCI_CLASS_OTHERS
&&
1298 vpci_dev
->class_code
!= PCI_CLASS_MEMORY_RAM
) { /* qemu < 1.1 */
1299 vpci_dev
->class_code
= PCI_CLASS_OTHERS
;
1302 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1303 if (qdev_init(vdev
) < 0) {
1309 static void virtio_balloon_pci_class_init(ObjectClass
*klass
, void *data
)
1311 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1312 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1313 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1314 k
->init
= virtio_balloon_pci_init
;
1315 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1316 dc
->props
= virtio_balloon_pci_properties
;
1317 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1318 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BALLOON
;
1319 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1320 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
1323 static void virtio_balloon_pci_instance_init(Object
*obj
)
1325 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(obj
);
1326 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_BALLOON
);
1327 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1328 object_unref(OBJECT(&dev
->vdev
));
1329 object_property_add(obj
, "guest-stats", "guest statistics",
1330 balloon_pci_stats_get_all
, NULL
, NULL
, dev
,
1333 object_property_add(obj
, "guest-stats-polling-interval", "int",
1334 balloon_pci_stats_get_poll_interval
,
1335 balloon_pci_stats_set_poll_interval
,
1339 static const TypeInfo virtio_balloon_pci_info
= {
1340 .name
= TYPE_VIRTIO_BALLOON_PCI
,
1341 .parent
= TYPE_VIRTIO_PCI
,
1342 .instance_size
= sizeof(VirtIOBalloonPCI
),
1343 .instance_init
= virtio_balloon_pci_instance_init
,
1344 .class_init
= virtio_balloon_pci_class_init
,
1347 /* virtio-serial-pci */
1349 static int virtio_serial_pci_init(VirtIOPCIProxy
*vpci_dev
)
1351 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(vpci_dev
);
1352 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1353 DeviceState
*proxy
= DEVICE(vpci_dev
);
1356 if (vpci_dev
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
1357 vpci_dev
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
1358 vpci_dev
->class_code
!= PCI_CLASS_OTHERS
) { /* qemu-kvm */
1359 vpci_dev
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
1362 /* backwards-compatibility with machines that were created with
1363 DEV_NVECTORS_UNSPECIFIED */
1364 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1365 vpci_dev
->nvectors
= dev
->vdev
.serial
.max_virtserial_ports
+ 1;
1369 * For command line compatibility, this sets the virtio-serial-device bus
1373 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1374 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1378 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1379 if (qdev_init(vdev
) < 0) {
1385 static Property virtio_serial_pci_properties
[] = {
1386 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1387 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1388 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1389 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1390 DEFINE_PROP_END_OF_LIST(),
1393 static void virtio_serial_pci_class_init(ObjectClass
*klass
, void *data
)
1395 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1396 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1397 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1398 k
->init
= virtio_serial_pci_init
;
1399 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
1400 dc
->props
= virtio_serial_pci_properties
;
1401 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1402 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_CONSOLE
;
1403 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1404 pcidev_k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
1407 static void virtio_serial_pci_instance_init(Object
*obj
)
1409 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(obj
);
1411 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1412 TYPE_VIRTIO_SERIAL
);
1415 static const TypeInfo virtio_serial_pci_info
= {
1416 .name
= TYPE_VIRTIO_SERIAL_PCI
,
1417 .parent
= TYPE_VIRTIO_PCI
,
1418 .instance_size
= sizeof(VirtIOSerialPCI
),
1419 .instance_init
= virtio_serial_pci_instance_init
,
1420 .class_init
= virtio_serial_pci_class_init
,
1423 /* virtio-net-pci */
1425 static Property virtio_net_properties
[] = {
1426 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1427 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, false),
1428 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
1429 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy
, host_features
),
1430 DEFINE_PROP_END_OF_LIST(),
1433 static int virtio_net_pci_init(VirtIOPCIProxy
*vpci_dev
)
1435 DeviceState
*qdev
= DEVICE(vpci_dev
);
1436 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(vpci_dev
);
1437 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1439 virtio_net_set_config_size(&dev
->vdev
, vpci_dev
->host_features
);
1440 virtio_net_set_netclient_name(&dev
->vdev
, qdev
->id
,
1441 object_get_typename(OBJECT(qdev
)));
1442 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1443 if (qdev_init(vdev
) < 0) {
1449 static void virtio_net_pci_class_init(ObjectClass
*klass
, void *data
)
1451 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1452 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1453 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_CLASS(klass
);
1455 k
->romfile
= "efi-virtio.rom";
1456 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1457 k
->device_id
= PCI_DEVICE_ID_VIRTIO_NET
;
1458 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1459 k
->class_id
= PCI_CLASS_NETWORK_ETHERNET
;
1460 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
1461 dc
->props
= virtio_net_properties
;
1462 vpciklass
->init
= virtio_net_pci_init
;
1465 static void virtio_net_pci_instance_init(Object
*obj
)
1467 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(obj
);
1469 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1471 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
1472 "bootindex", &error_abort
);
1475 static const TypeInfo virtio_net_pci_info
= {
1476 .name
= TYPE_VIRTIO_NET_PCI
,
1477 .parent
= TYPE_VIRTIO_PCI
,
1478 .instance_size
= sizeof(VirtIONetPCI
),
1479 .instance_init
= virtio_net_pci_instance_init
,
1480 .class_init
= virtio_net_pci_class_init
,
1483 /* virtio-rng-pci */
1485 static Property virtio_rng_pci_properties
[] = {
1486 DEFINE_PROP_END_OF_LIST(),
1489 static int virtio_rng_pci_init(VirtIOPCIProxy
*vpci_dev
)
1491 VirtIORngPCI
*vrng
= VIRTIO_RNG_PCI(vpci_dev
);
1492 DeviceState
*vdev
= DEVICE(&vrng
->vdev
);
1494 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1495 if (qdev_init(vdev
) < 0) {
1499 object_property_set_link(OBJECT(vrng
),
1500 OBJECT(vrng
->vdev
.conf
.rng
), "rng",
1506 static void virtio_rng_pci_class_init(ObjectClass
*klass
, void *data
)
1508 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1509 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1510 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1512 k
->init
= virtio_rng_pci_init
;
1513 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1514 dc
->props
= virtio_rng_pci_properties
;
1516 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1517 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_RNG
;
1518 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1519 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
1522 static void virtio_rng_initfn(Object
*obj
)
1524 VirtIORngPCI
*dev
= VIRTIO_RNG_PCI(obj
);
1526 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1528 object_property_add_link(obj
, "rng", TYPE_RNG_BACKEND
,
1529 (Object
**)&dev
->vdev
.conf
.rng
,
1530 qdev_prop_allow_set_link_before_realize
,
1531 OBJ_PROP_LINK_UNREF_ON_RELEASE
, NULL
);
1535 static const TypeInfo virtio_rng_pci_info
= {
1536 .name
= TYPE_VIRTIO_RNG_PCI
,
1537 .parent
= TYPE_VIRTIO_PCI
,
1538 .instance_size
= sizeof(VirtIORngPCI
),
1539 .instance_init
= virtio_rng_initfn
,
1540 .class_init
= virtio_rng_pci_class_init
,
1543 /* virtio-pci-bus */
1545 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
1546 VirtIOPCIProxy
*dev
)
1548 DeviceState
*qdev
= DEVICE(dev
);
1550 char virtio_bus_name
[] = "virtio-bus";
1552 qbus_create_inplace(bus
, bus_size
, TYPE_VIRTIO_PCI_BUS
, qdev
,
1555 qbus
->allow_hotplug
= 1;
1558 static void virtio_pci_bus_class_init(ObjectClass
*klass
, void *data
)
1560 BusClass
*bus_class
= BUS_CLASS(klass
);
1561 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
1562 bus_class
->max_dev
= 1;
1563 k
->notify
= virtio_pci_notify
;
1564 k
->save_config
= virtio_pci_save_config
;
1565 k
->load_config
= virtio_pci_load_config
;
1566 k
->save_queue
= virtio_pci_save_queue
;
1567 k
->load_queue
= virtio_pci_load_queue
;
1568 k
->get_features
= virtio_pci_get_features
;
1569 k
->query_guest_notifiers
= virtio_pci_query_guest_notifiers
;
1570 k
->set_host_notifier
= virtio_pci_set_host_notifier
;
1571 k
->set_guest_notifiers
= virtio_pci_set_guest_notifiers
;
1572 k
->vmstate_change
= virtio_pci_vmstate_change
;
1573 k
->device_plugged
= virtio_pci_device_plugged
;
1574 k
->device_unplugged
= virtio_pci_device_unplugged
;
1577 static const TypeInfo virtio_pci_bus_info
= {
1578 .name
= TYPE_VIRTIO_PCI_BUS
,
1579 .parent
= TYPE_VIRTIO_BUS
,
1580 .instance_size
= sizeof(VirtioPCIBusState
),
1581 .class_init
= virtio_pci_bus_class_init
,
1584 static void virtio_pci_register_types(void)
1586 type_register_static(&virtio_rng_pci_info
);
1587 type_register_static(&virtio_pci_bus_info
);
1588 type_register_static(&virtio_pci_info
);
1589 #ifdef CONFIG_VIRTFS
1590 type_register_static(&virtio_9p_pci_info
);
1592 type_register_static(&virtio_blk_pci_info
);
1593 type_register_static(&virtio_scsi_pci_info
);
1594 type_register_static(&virtio_balloon_pci_info
);
1595 type_register_static(&virtio_serial_pci_info
);
1596 type_register_static(&virtio_net_pci_info
);
1597 #ifdef CONFIG_VHOST_SCSI
1598 type_register_static(&vhost_scsi_pci_info
);
1602 type_init(virtio_pci_register_types
)