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 /* HACK for virtio to determine if it's running a big endian guest */
93 bool virtio_is_big_endian(void);
95 static void virtio_pci_bus_new(VirtioBusState
*bus
, VirtIOPCIProxy
*dev
);
98 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
99 static inline VirtIOPCIProxy
*to_virtio_pci_proxy(DeviceState
*d
)
101 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
104 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
105 * be careful and test performance if you change this.
107 static inline VirtIOPCIProxy
*to_virtio_pci_proxy_fast(DeviceState
*d
)
109 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
112 static void virtio_pci_notify(DeviceState
*d
, uint16_t vector
)
114 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy_fast(d
);
115 if (msix_enabled(&proxy
->pci_dev
))
116 msix_notify(&proxy
->pci_dev
, vector
);
118 qemu_set_irq(proxy
->pci_dev
.irq
[0], proxy
->vdev
->isr
& 1);
121 static void virtio_pci_save_config(DeviceState
*d
, QEMUFile
*f
)
123 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
124 pci_device_save(&proxy
->pci_dev
, f
);
125 msix_save(&proxy
->pci_dev
, f
);
126 if (msix_present(&proxy
->pci_dev
))
127 qemu_put_be16(f
, proxy
->vdev
->config_vector
);
130 static void virtio_pci_save_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
132 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
133 if (msix_present(&proxy
->pci_dev
))
134 qemu_put_be16(f
, virtio_queue_vector(proxy
->vdev
, n
));
137 static int virtio_pci_load_config(DeviceState
*d
, QEMUFile
*f
)
139 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
141 ret
= pci_device_load(&proxy
->pci_dev
, f
);
145 msix_unuse_all_vectors(&proxy
->pci_dev
);
146 msix_load(&proxy
->pci_dev
, f
);
147 if (msix_present(&proxy
->pci_dev
)) {
148 qemu_get_be16s(f
, &proxy
->vdev
->config_vector
);
150 proxy
->vdev
->config_vector
= VIRTIO_NO_VECTOR
;
152 if (proxy
->vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
153 return msix_vector_use(&proxy
->pci_dev
, proxy
->vdev
->config_vector
);
158 static int virtio_pci_load_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
160 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
162 if (msix_present(&proxy
->pci_dev
)) {
163 qemu_get_be16s(f
, &vector
);
165 vector
= VIRTIO_NO_VECTOR
;
167 virtio_queue_set_vector(proxy
->vdev
, n
, vector
);
168 if (vector
!= VIRTIO_NO_VECTOR
) {
169 return msix_vector_use(&proxy
->pci_dev
, vector
);
174 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy
*proxy
,
175 int n
, bool assign
, bool set_handler
)
177 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
178 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
182 r
= event_notifier_init(notifier
, 1);
184 error_report("%s: unable to init event notifier: %d",
188 virtio_queue_set_host_notifier_fd_handler(vq
, true, set_handler
);
189 memory_region_add_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
192 memory_region_del_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
194 virtio_queue_set_host_notifier_fd_handler(vq
, false, false);
195 event_notifier_cleanup(notifier
);
200 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
204 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) ||
205 proxy
->ioeventfd_disabled
||
206 proxy
->ioeventfd_started
) {
210 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
211 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
215 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, true, true);
220 proxy
->ioeventfd_started
= true;
225 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
229 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
232 proxy
->ioeventfd_started
= false;
233 error_report("%s: failed. Fallback to a userspace (slower).", __func__
);
236 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
241 if (!proxy
->ioeventfd_started
) {
245 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
246 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
250 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
253 proxy
->ioeventfd_started
= false;
256 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
258 VirtIOPCIProxy
*proxy
= opaque
;
259 VirtIODevice
*vdev
= proxy
->vdev
;
263 case VIRTIO_PCI_GUEST_FEATURES
:
264 /* Guest does not negotiate properly? We have to assume nothing. */
265 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
266 val
= virtio_bus_get_vdev_bad_features(&proxy
->bus
);
268 virtio_set_features(vdev
, val
);
270 case VIRTIO_PCI_QUEUE_PFN
:
271 pa
= (hwaddr
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
273 virtio_pci_stop_ioeventfd(proxy
);
274 virtio_reset(proxy
->vdev
);
275 msix_unuse_all_vectors(&proxy
->pci_dev
);
278 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
280 case VIRTIO_PCI_QUEUE_SEL
:
281 if (val
< VIRTIO_PCI_QUEUE_MAX
)
282 vdev
->queue_sel
= val
;
284 case VIRTIO_PCI_QUEUE_NOTIFY
:
285 if (val
< VIRTIO_PCI_QUEUE_MAX
) {
286 virtio_queue_notify(vdev
, val
);
289 case VIRTIO_PCI_STATUS
:
290 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
291 virtio_pci_stop_ioeventfd(proxy
);
294 virtio_set_status(vdev
, val
& 0xFF);
296 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
297 virtio_pci_start_ioeventfd(proxy
);
300 if (vdev
->status
== 0) {
301 virtio_reset(proxy
->vdev
);
302 msix_unuse_all_vectors(&proxy
->pci_dev
);
305 /* Linux before 2.6.34 sets the device as OK without enabling
306 the PCI device bus master bit. In this case we need to disable
307 some safety checks. */
308 if ((val
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
309 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
310 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
313 case VIRTIO_MSI_CONFIG_VECTOR
:
314 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
315 /* Make it possible for guest to discover an error took place. */
316 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
317 val
= VIRTIO_NO_VECTOR
;
318 vdev
->config_vector
= val
;
320 case VIRTIO_MSI_QUEUE_VECTOR
:
321 msix_vector_unuse(&proxy
->pci_dev
,
322 virtio_queue_vector(vdev
, vdev
->queue_sel
));
323 /* Make it possible for guest to discover an error took place. */
324 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
325 val
= VIRTIO_NO_VECTOR
;
326 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
329 error_report("%s: unexpected address 0x%x value 0x%x",
330 __func__
, addr
, val
);
335 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
337 VirtIODevice
*vdev
= proxy
->vdev
;
338 uint32_t ret
= 0xFFFFFFFF;
341 case VIRTIO_PCI_HOST_FEATURES
:
342 ret
= proxy
->host_features
;
344 case VIRTIO_PCI_GUEST_FEATURES
:
345 ret
= vdev
->guest_features
;
347 case VIRTIO_PCI_QUEUE_PFN
:
348 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
349 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
351 case VIRTIO_PCI_QUEUE_NUM
:
352 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
354 case VIRTIO_PCI_QUEUE_SEL
:
355 ret
= vdev
->queue_sel
;
357 case VIRTIO_PCI_STATUS
:
361 /* reading from the ISR also clears it. */
364 qemu_set_irq(proxy
->pci_dev
.irq
[0], 0);
366 case VIRTIO_MSI_CONFIG_VECTOR
:
367 ret
= vdev
->config_vector
;
369 case VIRTIO_MSI_QUEUE_VECTOR
:
370 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
379 static uint64_t virtio_pci_config_read(void *opaque
, hwaddr addr
,
382 VirtIOPCIProxy
*proxy
= opaque
;
383 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
386 return virtio_ioport_read(proxy
, addr
);
392 val
= virtio_config_readb(proxy
->vdev
, addr
);
395 val
= virtio_config_readw(proxy
->vdev
, addr
);
396 if (virtio_is_big_endian()) {
401 val
= virtio_config_readl(proxy
->vdev
, addr
);
402 if (virtio_is_big_endian()) {
410 static void virtio_pci_config_write(void *opaque
, hwaddr addr
,
411 uint64_t val
, unsigned size
)
413 VirtIOPCIProxy
*proxy
= opaque
;
414 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
416 virtio_ioport_write(proxy
, addr
, val
);
421 * Virtio-PCI is odd. Ioports are LE but config space is target native
426 virtio_config_writeb(proxy
->vdev
, addr
, val
);
429 if (virtio_is_big_endian()) {
432 virtio_config_writew(proxy
->vdev
, addr
, val
);
435 if (virtio_is_big_endian()) {
438 virtio_config_writel(proxy
->vdev
, addr
, val
);
443 static const MemoryRegionOps virtio_pci_config_ops
= {
444 .read
= virtio_pci_config_read
,
445 .write
= virtio_pci_config_write
,
447 .min_access_size
= 1,
448 .max_access_size
= 4,
450 .endianness
= DEVICE_LITTLE_ENDIAN
,
453 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
454 uint32_t val
, int len
)
456 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
458 pci_default_write_config(pci_dev
, address
, val
, len
);
460 if (range_covers_byte(address
, len
, PCI_COMMAND
) &&
461 !(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
) &&
462 !(proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG
)) {
463 virtio_pci_stop_ioeventfd(proxy
);
464 virtio_set_status(proxy
->vdev
,
465 proxy
->vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
469 static unsigned virtio_pci_get_features(DeviceState
*d
)
471 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
472 return proxy
->host_features
;
475 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy
*proxy
,
476 unsigned int queue_no
,
480 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
483 if (irqfd
->users
== 0) {
484 ret
= kvm_irqchip_add_msi_route(kvm_state
, msg
);
494 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy
*proxy
,
497 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
498 if (--irqfd
->users
== 0) {
499 kvm_irqchip_release_virq(kvm_state
, irqfd
->virq
);
503 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy
*proxy
,
504 unsigned int queue_no
,
507 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
508 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, queue_no
);
509 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
511 ret
= kvm_irqchip_add_irqfd_notifier(kvm_state
, n
, irqfd
->virq
);
515 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy
*proxy
,
516 unsigned int queue_no
,
519 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, queue_no
);
520 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
521 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
524 ret
= kvm_irqchip_remove_irqfd_notifier(kvm_state
, n
, irqfd
->virq
);
528 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy
*proxy
, int nvqs
)
530 PCIDevice
*dev
= &proxy
->pci_dev
;
531 VirtIODevice
*vdev
= proxy
->vdev
;
532 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
537 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
538 if (!virtio_queue_get_num(vdev
, queue_no
)) {
541 vector
= virtio_queue_vector(vdev
, queue_no
);
542 if (vector
>= msix_nr_vectors_allocated(dev
)) {
545 msg
= msix_get_message(dev
, vector
);
546 ret
= kvm_virtio_pci_vq_vector_use(proxy
, queue_no
, vector
, msg
);
550 /* If guest supports masking, set up irqfd now.
551 * Otherwise, delay until unmasked in the frontend.
553 if (k
->guest_notifier_mask
) {
554 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
556 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
564 while (--queue_no
>= 0) {
565 vector
= virtio_queue_vector(vdev
, queue_no
);
566 if (vector
>= msix_nr_vectors_allocated(dev
)) {
569 if (k
->guest_notifier_mask
) {
570 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
572 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
577 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy
*proxy
, int nvqs
)
579 PCIDevice
*dev
= &proxy
->pci_dev
;
580 VirtIODevice
*vdev
= proxy
->vdev
;
583 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
585 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
586 if (!virtio_queue_get_num(vdev
, queue_no
)) {
589 vector
= virtio_queue_vector(vdev
, queue_no
);
590 if (vector
>= msix_nr_vectors_allocated(dev
)) {
593 /* If guest supports masking, clean up irqfd now.
594 * Otherwise, it was cleaned when masked in the frontend.
596 if (k
->guest_notifier_mask
) {
597 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
599 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
603 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy
*proxy
,
604 unsigned int queue_no
,
608 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(proxy
->vdev
);
609 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, queue_no
);
610 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
614 if (proxy
->vector_irqfd
) {
615 irqfd
= &proxy
->vector_irqfd
[vector
];
616 if (irqfd
->msg
.data
!= msg
.data
|| irqfd
->msg
.address
!= msg
.address
) {
617 ret
= kvm_irqchip_update_msi_route(kvm_state
, irqfd
->virq
, msg
);
624 /* If guest supports masking, irqfd is already setup, unmask it.
625 * Otherwise, set it up now.
627 if (k
->guest_notifier_mask
) {
628 k
->guest_notifier_mask(proxy
->vdev
, queue_no
, false);
629 /* Test after unmasking to avoid losing events. */
630 if (k
->guest_notifier_pending
&&
631 k
->guest_notifier_pending(proxy
->vdev
, queue_no
)) {
632 event_notifier_set(n
);
635 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
640 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy
*proxy
,
641 unsigned int queue_no
,
644 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(proxy
->vdev
);
646 /* If guest supports masking, keep irqfd but mask it.
647 * Otherwise, clean it up now.
649 if (k
->guest_notifier_mask
) {
650 k
->guest_notifier_mask(proxy
->vdev
, queue_no
, true);
652 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
656 static int virtio_pci_vector_unmask(PCIDevice
*dev
, unsigned vector
,
659 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
660 VirtIODevice
*vdev
= proxy
->vdev
;
663 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
664 if (!virtio_queue_get_num(vdev
, queue_no
)) {
667 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
670 ret
= virtio_pci_vq_vector_unmask(proxy
, queue_no
, vector
, msg
);
678 while (--queue_no
>= 0) {
679 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
682 virtio_pci_vq_vector_mask(proxy
, queue_no
, vector
);
687 static void virtio_pci_vector_mask(PCIDevice
*dev
, unsigned vector
)
689 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
690 VirtIODevice
*vdev
= proxy
->vdev
;
693 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
694 if (!virtio_queue_get_num(vdev
, queue_no
)) {
697 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
700 virtio_pci_vq_vector_mask(proxy
, queue_no
, vector
);
704 static void virtio_pci_vector_poll(PCIDevice
*dev
,
705 unsigned int vector_start
,
706 unsigned int vector_end
)
708 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
709 VirtIODevice
*vdev
= proxy
->vdev
;
710 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
713 EventNotifier
*notifier
;
716 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
717 if (!virtio_queue_get_num(vdev
, queue_no
)) {
720 vector
= virtio_queue_vector(vdev
, queue_no
);
721 if (vector
< vector_start
|| vector
>= vector_end
||
722 !msix_is_masked(dev
, vector
)) {
725 vq
= virtio_get_queue(vdev
, queue_no
);
726 notifier
= virtio_queue_get_guest_notifier(vq
);
727 if (k
->guest_notifier_pending
) {
728 if (k
->guest_notifier_pending(vdev
, queue_no
)) {
729 msix_set_pending(dev
, vector
);
731 } else if (event_notifier_test_and_clear(notifier
)) {
732 msix_set_pending(dev
, vector
);
737 static int virtio_pci_set_guest_notifier(DeviceState
*d
, int n
, bool assign
,
740 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
741 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(proxy
->vdev
);
742 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
743 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
746 int r
= event_notifier_init(notifier
, 0);
750 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
752 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
753 event_notifier_cleanup(notifier
);
756 if (!msix_enabled(&proxy
->pci_dev
) && vdc
->guest_notifier_mask
) {
757 vdc
->guest_notifier_mask(proxy
->vdev
, n
, !assign
);
763 static bool virtio_pci_query_guest_notifiers(DeviceState
*d
)
765 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
766 return msix_enabled(&proxy
->pci_dev
);
769 static int virtio_pci_set_guest_notifiers(DeviceState
*d
, int nvqs
, bool assign
)
771 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
772 VirtIODevice
*vdev
= proxy
->vdev
;
773 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
775 bool with_irqfd
= msix_enabled(&proxy
->pci_dev
) &&
776 kvm_msi_via_irqfd_enabled();
778 nvqs
= MIN(nvqs
, VIRTIO_PCI_QUEUE_MAX
);
780 /* When deassigning, pass a consistent nvqs value
781 * to avoid leaking notifiers.
783 assert(assign
|| nvqs
== proxy
->nvqs_with_notifiers
);
785 proxy
->nvqs_with_notifiers
= nvqs
;
787 /* Must unset vector notifier while guest notifier is still assigned */
788 if ((proxy
->vector_irqfd
|| k
->guest_notifier_mask
) && !assign
) {
789 msix_unset_vector_notifiers(&proxy
->pci_dev
);
790 if (proxy
->vector_irqfd
) {
791 kvm_virtio_pci_vector_release(proxy
, nvqs
);
792 g_free(proxy
->vector_irqfd
);
793 proxy
->vector_irqfd
= NULL
;
797 for (n
= 0; n
< nvqs
; n
++) {
798 if (!virtio_queue_get_num(vdev
, n
)) {
802 r
= virtio_pci_set_guest_notifier(d
, n
, assign
,
803 kvm_msi_via_irqfd_enabled());
809 /* Must set vector notifier after guest notifier has been assigned */
810 if ((with_irqfd
|| k
->guest_notifier_mask
) && assign
) {
812 proxy
->vector_irqfd
=
813 g_malloc0(sizeof(*proxy
->vector_irqfd
) *
814 msix_nr_vectors_allocated(&proxy
->pci_dev
));
815 r
= kvm_virtio_pci_vector_use(proxy
, nvqs
);
820 r
= msix_set_vector_notifiers(&proxy
->pci_dev
,
821 virtio_pci_vector_unmask
,
822 virtio_pci_vector_mask
,
823 virtio_pci_vector_poll
);
825 goto notifiers_error
;
834 kvm_virtio_pci_vector_release(proxy
, nvqs
);
838 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
841 virtio_pci_set_guest_notifier(d
, n
, !assign
, with_irqfd
);
846 static int virtio_pci_set_host_notifier(DeviceState
*d
, int n
, bool assign
)
848 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
850 /* Stop using ioeventfd for virtqueue kick if the device starts using host
851 * notifiers. This makes it easy to avoid stepping on each others' toes.
853 proxy
->ioeventfd_disabled
= assign
;
855 virtio_pci_stop_ioeventfd(proxy
);
857 /* We don't need to start here: it's not needed because backend
858 * currently only stops on status change away from ok,
859 * reset, vmstop and such. If we do add code to start here,
860 * need to check vmstate, device state etc. */
861 return virtio_pci_set_host_notifier_internal(proxy
, n
, assign
, false);
864 static void virtio_pci_vmstate_change(DeviceState
*d
, bool running
)
866 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
869 /* Try to find out if the guest has bus master disabled, but is
870 in ready state. Then we have a buggy guest OS. */
871 if ((proxy
->vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
872 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
873 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
875 virtio_pci_start_ioeventfd(proxy
);
877 virtio_pci_stop_ioeventfd(proxy
);
882 static int virtio_9p_init_pci(VirtIOPCIProxy
*vpci_dev
)
884 V9fsPCIState
*dev
= VIRTIO_9P_PCI(vpci_dev
);
885 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
887 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
888 if (qdev_init(vdev
) < 0) {
894 static Property virtio_9p_pci_properties
[] = {
895 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
896 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
897 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
898 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
899 DEFINE_VIRTIO_9P_PROPERTIES(V9fsPCIState
, vdev
.fsconf
),
900 DEFINE_PROP_END_OF_LIST(),
903 static void virtio_9p_pci_class_init(ObjectClass
*klass
, void *data
)
905 DeviceClass
*dc
= DEVICE_CLASS(klass
);
906 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
907 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
909 k
->init
= virtio_9p_init_pci
;
910 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
911 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_9P
;
912 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
913 pcidev_k
->class_id
= 0x2;
914 dc
->props
= virtio_9p_pci_properties
;
917 static void virtio_9p_pci_instance_init(Object
*obj
)
919 V9fsPCIState
*dev
= VIRTIO_9P_PCI(obj
);
920 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_9P
);
921 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
924 static const TypeInfo virtio_9p_pci_info
= {
925 .name
= TYPE_VIRTIO_9P_PCI
,
926 .parent
= TYPE_VIRTIO_PCI
,
927 .instance_size
= sizeof(V9fsPCIState
),
928 .instance_init
= virtio_9p_pci_instance_init
,
929 .class_init
= virtio_9p_pci_class_init
,
931 #endif /* CONFIG_VIRTFS */
934 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
937 /* This is called by virtio-bus just after the device is plugged. */
938 static void virtio_pci_device_plugged(DeviceState
*d
)
940 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
941 VirtioBusState
*bus
= &proxy
->bus
;
945 proxy
->vdev
= bus
->vdev
;
947 config
= proxy
->pci_dev
.config
;
948 if (proxy
->class_code
) {
949 pci_config_set_class(config
, proxy
->class_code
);
951 pci_set_word(config
+ PCI_SUBSYSTEM_VENDOR_ID
,
952 pci_get_word(config
+ PCI_VENDOR_ID
));
953 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, virtio_bus_get_vdev_id(bus
));
954 config
[PCI_INTERRUPT_PIN
] = 1;
956 if (proxy
->nvectors
&&
957 msix_init_exclusive_bar(&proxy
->pci_dev
, proxy
->nvectors
, 1)) {
961 proxy
->pci_dev
.config_write
= virtio_write_config
;
963 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
)
964 + virtio_bus_get_vdev_config_len(bus
);
965 if (size
& (size
- 1)) {
966 size
= 1 << qemu_fls(size
);
969 memory_region_init_io(&proxy
->bar
, &virtio_pci_config_ops
, proxy
,
971 pci_register_bar(&proxy
->pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
,
974 if (!kvm_has_many_ioeventfds()) {
975 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
978 proxy
->host_features
|= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY
;
979 proxy
->host_features
|= 0x1 << VIRTIO_F_BAD_FEATURE
;
980 proxy
->host_features
= virtio_bus_get_vdev_features(bus
,
981 proxy
->host_features
);
984 static int virtio_pci_init(PCIDevice
*pci_dev
)
986 VirtIOPCIProxy
*dev
= VIRTIO_PCI(pci_dev
);
987 VirtioPCIClass
*k
= VIRTIO_PCI_GET_CLASS(pci_dev
);
988 virtio_pci_bus_new(&dev
->bus
, dev
);
989 if (k
->init
!= NULL
) {
995 static void virtio_pci_exit(PCIDevice
*pci_dev
)
997 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
998 virtio_pci_stop_ioeventfd(proxy
);
999 memory_region_destroy(&proxy
->bar
);
1000 msix_uninit_exclusive_bar(pci_dev
);
1003 static void virtio_pci_reset(DeviceState
*qdev
)
1005 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1006 VirtioBusState
*bus
= VIRTIO_BUS(&proxy
->bus
);
1007 virtio_pci_stop_ioeventfd(proxy
);
1008 virtio_bus_reset(bus
);
1009 msix_unuse_all_vectors(&proxy
->pci_dev
);
1010 proxy
->flags
&= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
1013 static void virtio_pci_class_init(ObjectClass
*klass
, void *data
)
1015 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1016 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1018 k
->init
= virtio_pci_init
;
1019 k
->exit
= virtio_pci_exit
;
1020 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1021 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1022 k
->class_id
= PCI_CLASS_OTHERS
;
1023 dc
->reset
= virtio_pci_reset
;
1026 static const TypeInfo virtio_pci_info
= {
1027 .name
= TYPE_VIRTIO_PCI
,
1028 .parent
= TYPE_PCI_DEVICE
,
1029 .instance_size
= sizeof(VirtIOPCIProxy
),
1030 .class_init
= virtio_pci_class_init
,
1031 .class_size
= sizeof(VirtioPCIClass
),
1035 /* virtio-blk-pci */
1037 static Property virtio_blk_pci_properties
[] = {
1038 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
1039 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1040 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1041 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1042 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
1043 DEFINE_PROP_BIT("x-data-plane", VirtIOBlkPCI
, blk
.data_plane
, 0, false),
1045 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy
, host_features
),
1046 DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlkPCI
, blk
),
1047 DEFINE_PROP_END_OF_LIST(),
1050 static int virtio_blk_pci_init(VirtIOPCIProxy
*vpci_dev
)
1052 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(vpci_dev
);
1053 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1054 virtio_blk_set_conf(vdev
, &(dev
->blk
));
1055 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1056 if (qdev_init(vdev
) < 0) {
1062 static void virtio_blk_pci_class_init(ObjectClass
*klass
, void *data
)
1064 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1065 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1066 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1068 dc
->props
= virtio_blk_pci_properties
;
1069 k
->init
= virtio_blk_pci_init
;
1070 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1071 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
1072 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1073 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1076 static void virtio_blk_pci_instance_init(Object
*obj
)
1078 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(obj
);
1079 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_BLK
);
1080 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1083 static const TypeInfo virtio_blk_pci_info
= {
1084 .name
= TYPE_VIRTIO_BLK_PCI
,
1085 .parent
= TYPE_VIRTIO_PCI
,
1086 .instance_size
= sizeof(VirtIOBlkPCI
),
1087 .instance_init
= virtio_blk_pci_instance_init
,
1088 .class_init
= virtio_blk_pci_class_init
,
1091 /* virtio-scsi-pci */
1093 static Property virtio_scsi_pci_properties
[] = {
1094 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1095 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1096 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1097 DEV_NVECTORS_UNSPECIFIED
),
1098 DEFINE_VIRTIO_SCSI_FEATURES(VirtIOPCIProxy
, host_features
),
1099 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSIPCI
, vdev
.parent_obj
.conf
),
1100 DEFINE_PROP_END_OF_LIST(),
1103 static int virtio_scsi_pci_init_pci(VirtIOPCIProxy
*vpci_dev
)
1105 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(vpci_dev
);
1106 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1107 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1108 DeviceState
*proxy
= DEVICE(vpci_dev
);
1111 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1112 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1116 * For command line compatibility, this sets the virtio-scsi-device bus
1120 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1121 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1125 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1126 if (qdev_init(vdev
) < 0) {
1132 static void virtio_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1134 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1135 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1136 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1137 k
->init
= virtio_scsi_pci_init_pci
;
1138 dc
->props
= virtio_scsi_pci_properties
;
1139 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1140 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1141 pcidev_k
->revision
= 0x00;
1142 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1145 static void virtio_scsi_pci_instance_init(Object
*obj
)
1147 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(obj
);
1148 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_SCSI
);
1149 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1152 static const TypeInfo virtio_scsi_pci_info
= {
1153 .name
= TYPE_VIRTIO_SCSI_PCI
,
1154 .parent
= TYPE_VIRTIO_PCI
,
1155 .instance_size
= sizeof(VirtIOSCSIPCI
),
1156 .instance_init
= virtio_scsi_pci_instance_init
,
1157 .class_init
= virtio_scsi_pci_class_init
,
1160 /* vhost-scsi-pci */
1162 #ifdef CONFIG_VHOST_SCSI
1163 static Property vhost_scsi_pci_properties
[] = {
1164 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1165 DEV_NVECTORS_UNSPECIFIED
),
1166 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1167 DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSIPCI
, vdev
.parent_obj
.conf
),
1168 DEFINE_PROP_END_OF_LIST(),
1171 static int vhost_scsi_pci_init_pci(VirtIOPCIProxy
*vpci_dev
)
1173 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(vpci_dev
);
1174 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1175 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1177 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1178 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1181 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1182 if (qdev_init(vdev
) < 0) {
1188 static void vhost_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1190 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1191 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1192 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1193 k
->init
= vhost_scsi_pci_init_pci
;
1194 dc
->props
= vhost_scsi_pci_properties
;
1195 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1196 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1197 pcidev_k
->revision
= 0x00;
1198 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1201 static void vhost_scsi_pci_instance_init(Object
*obj
)
1203 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(obj
);
1204 object_initialize(OBJECT(&dev
->vdev
), TYPE_VHOST_SCSI
);
1205 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1208 static const TypeInfo vhost_scsi_pci_info
= {
1209 .name
= TYPE_VHOST_SCSI_PCI
,
1210 .parent
= TYPE_VIRTIO_PCI
,
1211 .instance_size
= sizeof(VHostSCSIPCI
),
1212 .instance_init
= vhost_scsi_pci_instance_init
,
1213 .class_init
= vhost_scsi_pci_class_init
,
1217 /* virtio-balloon-pci */
1219 static void balloon_pci_stats_get_all(Object
*obj
, struct Visitor
*v
,
1220 void *opaque
, const char *name
,
1223 VirtIOBalloonPCI
*dev
= opaque
;
1224 object_property_get(OBJECT(&dev
->vdev
), v
, "guest-stats", errp
);
1227 static void balloon_pci_stats_get_poll_interval(Object
*obj
, struct Visitor
*v
,
1228 void *opaque
, const char *name
,
1231 VirtIOBalloonPCI
*dev
= opaque
;
1232 object_property_get(OBJECT(&dev
->vdev
), v
, "guest-stats-polling-interval",
1236 static void balloon_pci_stats_set_poll_interval(Object
*obj
, struct Visitor
*v
,
1237 void *opaque
, const char *name
,
1240 VirtIOBalloonPCI
*dev
= opaque
;
1241 object_property_set(OBJECT(&dev
->vdev
), v
, "guest-stats-polling-interval",
1245 static Property virtio_balloon_pci_properties
[] = {
1246 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1247 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
1248 DEFINE_PROP_END_OF_LIST(),
1251 static int virtio_balloon_pci_init(VirtIOPCIProxy
*vpci_dev
)
1253 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(vpci_dev
);
1254 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1256 if (vpci_dev
->class_code
!= PCI_CLASS_OTHERS
&&
1257 vpci_dev
->class_code
!= PCI_CLASS_MEMORY_RAM
) { /* qemu < 1.1 */
1258 vpci_dev
->class_code
= PCI_CLASS_OTHERS
;
1261 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1262 if (qdev_init(vdev
) < 0) {
1268 static void virtio_balloon_pci_class_init(ObjectClass
*klass
, void *data
)
1270 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1271 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1272 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1273 k
->init
= virtio_balloon_pci_init
;
1274 dc
->props
= virtio_balloon_pci_properties
;
1275 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1276 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BALLOON
;
1277 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1278 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
1281 static void virtio_balloon_pci_instance_init(Object
*obj
)
1283 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(obj
);
1284 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_BALLOON
);
1285 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1287 object_property_add(obj
, "guest-stats", "guest statistics",
1288 balloon_pci_stats_get_all
, NULL
, NULL
, dev
,
1291 object_property_add(obj
, "guest-stats-polling-interval", "int",
1292 balloon_pci_stats_get_poll_interval
,
1293 balloon_pci_stats_set_poll_interval
,
1297 static const TypeInfo virtio_balloon_pci_info
= {
1298 .name
= TYPE_VIRTIO_BALLOON_PCI
,
1299 .parent
= TYPE_VIRTIO_PCI
,
1300 .instance_size
= sizeof(VirtIOBalloonPCI
),
1301 .instance_init
= virtio_balloon_pci_instance_init
,
1302 .class_init
= virtio_balloon_pci_class_init
,
1305 /* virtio-serial-pci */
1307 static int virtio_serial_pci_init(VirtIOPCIProxy
*vpci_dev
)
1309 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(vpci_dev
);
1310 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1311 DeviceState
*proxy
= DEVICE(vpci_dev
);
1314 if (vpci_dev
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
1315 vpci_dev
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
1316 vpci_dev
->class_code
!= PCI_CLASS_OTHERS
) { /* qemu-kvm */
1317 vpci_dev
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
1320 /* backwards-compatibility with machines that were created with
1321 DEV_NVECTORS_UNSPECIFIED */
1322 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1323 vpci_dev
->nvectors
= dev
->vdev
.serial
.max_virtserial_ports
+ 1;
1327 * For command line compatibility, this sets the virtio-serial-device bus
1331 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1332 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1336 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1337 if (qdev_init(vdev
) < 0) {
1343 static Property virtio_serial_pci_properties
[] = {
1344 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1345 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1346 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1347 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
1348 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1349 DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtIOSerialPCI
, vdev
.serial
),
1350 DEFINE_PROP_END_OF_LIST(),
1353 static void virtio_serial_pci_class_init(ObjectClass
*klass
, void *data
)
1355 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1356 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1357 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1358 k
->init
= virtio_serial_pci_init
;
1359 dc
->props
= virtio_serial_pci_properties
;
1360 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1361 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_CONSOLE
;
1362 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1363 pcidev_k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
1366 static void virtio_serial_pci_instance_init(Object
*obj
)
1368 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(obj
);
1369 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_SERIAL
);
1370 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1373 static const TypeInfo virtio_serial_pci_info
= {
1374 .name
= TYPE_VIRTIO_SERIAL_PCI
,
1375 .parent
= TYPE_VIRTIO_PCI
,
1376 .instance_size
= sizeof(VirtIOSerialPCI
),
1377 .instance_init
= virtio_serial_pci_instance_init
,
1378 .class_init
= virtio_serial_pci_class_init
,
1381 /* virtio-net-pci */
1383 static Property virtio_net_properties
[] = {
1384 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1385 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, false),
1386 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
1387 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy
, host_features
),
1388 DEFINE_NIC_PROPERTIES(VirtIONetPCI
, vdev
.nic_conf
),
1389 DEFINE_VIRTIO_NET_PROPERTIES(VirtIONetPCI
, vdev
.net_conf
),
1390 DEFINE_PROP_END_OF_LIST(),
1393 static int virtio_net_pci_init(VirtIOPCIProxy
*vpci_dev
)
1395 DeviceState
*qdev
= DEVICE(vpci_dev
);
1396 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(vpci_dev
);
1397 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1399 virtio_net_set_config_size(&dev
->vdev
, vpci_dev
->host_features
);
1400 virtio_net_set_netclient_name(&dev
->vdev
, qdev
->id
,
1401 object_get_typename(OBJECT(qdev
)));
1402 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1403 if (qdev_init(vdev
) < 0) {
1409 static void virtio_net_pci_class_init(ObjectClass
*klass
, void *data
)
1411 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1412 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1413 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_CLASS(klass
);
1415 k
->romfile
= "efi-virtio.rom";
1416 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1417 k
->device_id
= PCI_DEVICE_ID_VIRTIO_NET
;
1418 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1419 k
->class_id
= PCI_CLASS_NETWORK_ETHERNET
;
1420 dc
->props
= virtio_net_properties
;
1421 vpciklass
->init
= virtio_net_pci_init
;
1424 static void virtio_net_pci_instance_init(Object
*obj
)
1426 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(obj
);
1427 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_NET
);
1428 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1431 static const TypeInfo virtio_net_pci_info
= {
1432 .name
= TYPE_VIRTIO_NET_PCI
,
1433 .parent
= TYPE_VIRTIO_PCI
,
1434 .instance_size
= sizeof(VirtIONetPCI
),
1435 .instance_init
= virtio_net_pci_instance_init
,
1436 .class_init
= virtio_net_pci_class_init
,
1439 /* virtio-rng-pci */
1441 static Property virtio_rng_pci_properties
[] = {
1442 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1443 DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORngPCI
, vdev
.conf
),
1444 DEFINE_PROP_END_OF_LIST(),
1447 static int virtio_rng_pci_init(VirtIOPCIProxy
*vpci_dev
)
1449 VirtIORngPCI
*vrng
= VIRTIO_RNG_PCI(vpci_dev
);
1450 DeviceState
*vdev
= DEVICE(&vrng
->vdev
);
1452 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1453 if (qdev_init(vdev
) < 0) {
1457 object_property_set_link(OBJECT(vrng
),
1458 OBJECT(vrng
->vdev
.conf
.default_backend
), "rng",
1464 static void virtio_rng_pci_class_init(ObjectClass
*klass
, void *data
)
1466 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1467 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1468 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1470 k
->init
= virtio_rng_pci_init
;
1471 dc
->props
= virtio_rng_pci_properties
;
1473 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1474 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_RNG
;
1475 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1476 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
1479 static void virtio_rng_initfn(Object
*obj
)
1481 VirtIORngPCI
*dev
= VIRTIO_RNG_PCI(obj
);
1482 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_RNG
);
1483 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1484 object_property_add_link(obj
, "rng", TYPE_RNG_BACKEND
,
1485 (Object
**)&dev
->vdev
.conf
.rng
, NULL
);
1489 static const TypeInfo virtio_rng_pci_info
= {
1490 .name
= TYPE_VIRTIO_RNG_PCI
,
1491 .parent
= TYPE_VIRTIO_PCI
,
1492 .instance_size
= sizeof(VirtIORngPCI
),
1493 .instance_init
= virtio_rng_initfn
,
1494 .class_init
= virtio_rng_pci_class_init
,
1497 /* virtio-pci-bus */
1499 static void virtio_pci_bus_new(VirtioBusState
*bus
, VirtIOPCIProxy
*dev
)
1501 DeviceState
*qdev
= DEVICE(dev
);
1503 char virtio_bus_name
[] = "virtio-bus";
1505 qbus_create_inplace((BusState
*)bus
, TYPE_VIRTIO_PCI_BUS
, qdev
,
1508 qbus
->allow_hotplug
= 1;
1511 static void virtio_pci_bus_class_init(ObjectClass
*klass
, void *data
)
1513 BusClass
*bus_class
= BUS_CLASS(klass
);
1514 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
1515 bus_class
->max_dev
= 1;
1516 k
->notify
= virtio_pci_notify
;
1517 k
->save_config
= virtio_pci_save_config
;
1518 k
->load_config
= virtio_pci_load_config
;
1519 k
->save_queue
= virtio_pci_save_queue
;
1520 k
->load_queue
= virtio_pci_load_queue
;
1521 k
->get_features
= virtio_pci_get_features
;
1522 k
->query_guest_notifiers
= virtio_pci_query_guest_notifiers
;
1523 k
->set_host_notifier
= virtio_pci_set_host_notifier
;
1524 k
->set_guest_notifiers
= virtio_pci_set_guest_notifiers
;
1525 k
->vmstate_change
= virtio_pci_vmstate_change
;
1526 k
->device_plugged
= virtio_pci_device_plugged
;
1529 static const TypeInfo virtio_pci_bus_info
= {
1530 .name
= TYPE_VIRTIO_PCI_BUS
,
1531 .parent
= TYPE_VIRTIO_BUS
,
1532 .instance_size
= sizeof(VirtioPCIBusState
),
1533 .class_init
= virtio_pci_bus_class_init
,
1536 static void virtio_pci_register_types(void)
1538 type_register_static(&virtio_rng_pci_info
);
1539 type_register_static(&virtio_pci_bus_info
);
1540 type_register_static(&virtio_pci_info
);
1541 #ifdef CONFIG_VIRTFS
1542 type_register_static(&virtio_9p_pci_info
);
1544 type_register_static(&virtio_blk_pci_info
);
1545 type_register_static(&virtio_scsi_pci_info
);
1546 type_register_static(&virtio_balloon_pci_info
);
1547 type_register_static(&virtio_serial_pci_info
);
1548 type_register_static(&virtio_net_pci_info
);
1549 #ifdef CONFIG_VHOST_SCSI
1550 type_register_static(&vhost_scsi_pci_info
);
1554 type_init(virtio_pci_register_types
)