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
, NULL
, 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 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
915 dc
->props
= virtio_9p_pci_properties
;
918 static void virtio_9p_pci_instance_init(Object
*obj
)
920 V9fsPCIState
*dev
= VIRTIO_9P_PCI(obj
);
921 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_9P
);
922 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
925 static const TypeInfo virtio_9p_pci_info
= {
926 .name
= TYPE_VIRTIO_9P_PCI
,
927 .parent
= TYPE_VIRTIO_PCI
,
928 .instance_size
= sizeof(V9fsPCIState
),
929 .instance_init
= virtio_9p_pci_instance_init
,
930 .class_init
= virtio_9p_pci_class_init
,
932 #endif /* CONFIG_VIRTFS */
935 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
938 /* This is called by virtio-bus just after the device is plugged. */
939 static void virtio_pci_device_plugged(DeviceState
*d
)
941 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
942 VirtioBusState
*bus
= &proxy
->bus
;
946 proxy
->vdev
= bus
->vdev
;
948 config
= proxy
->pci_dev
.config
;
949 if (proxy
->class_code
) {
950 pci_config_set_class(config
, proxy
->class_code
);
952 pci_set_word(config
+ PCI_SUBSYSTEM_VENDOR_ID
,
953 pci_get_word(config
+ PCI_VENDOR_ID
));
954 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, virtio_bus_get_vdev_id(bus
));
955 config
[PCI_INTERRUPT_PIN
] = 1;
957 if (proxy
->nvectors
&&
958 msix_init_exclusive_bar(&proxy
->pci_dev
, proxy
->nvectors
, 1)) {
962 proxy
->pci_dev
.config_write
= virtio_write_config
;
964 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
)
965 + virtio_bus_get_vdev_config_len(bus
);
966 if (size
& (size
- 1)) {
967 size
= 1 << qemu_fls(size
);
970 memory_region_init_io(&proxy
->bar
, OBJECT(proxy
), &virtio_pci_config_ops
,
971 proxy
, "virtio-pci", size
);
972 pci_register_bar(&proxy
->pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
,
975 if (!kvm_has_many_ioeventfds()) {
976 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
979 proxy
->host_features
|= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY
;
980 proxy
->host_features
|= 0x1 << VIRTIO_F_BAD_FEATURE
;
981 proxy
->host_features
= virtio_bus_get_vdev_features(bus
,
982 proxy
->host_features
);
985 static int virtio_pci_init(PCIDevice
*pci_dev
)
987 VirtIOPCIProxy
*dev
= VIRTIO_PCI(pci_dev
);
988 VirtioPCIClass
*k
= VIRTIO_PCI_GET_CLASS(pci_dev
);
989 virtio_pci_bus_new(&dev
->bus
, dev
);
990 if (k
->init
!= NULL
) {
996 static void virtio_pci_exit(PCIDevice
*pci_dev
)
998 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
999 virtio_pci_stop_ioeventfd(proxy
);
1000 memory_region_destroy(&proxy
->bar
);
1001 msix_uninit_exclusive_bar(pci_dev
);
1004 static void virtio_pci_reset(DeviceState
*qdev
)
1006 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1007 VirtioBusState
*bus
= VIRTIO_BUS(&proxy
->bus
);
1008 virtio_pci_stop_ioeventfd(proxy
);
1009 virtio_bus_reset(bus
);
1010 msix_unuse_all_vectors(&proxy
->pci_dev
);
1011 proxy
->flags
&= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
1014 static void virtio_pci_class_init(ObjectClass
*klass
, void *data
)
1016 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1017 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1019 k
->init
= virtio_pci_init
;
1020 k
->exit
= virtio_pci_exit
;
1021 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1022 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1023 k
->class_id
= PCI_CLASS_OTHERS
;
1024 dc
->reset
= virtio_pci_reset
;
1027 static const TypeInfo virtio_pci_info
= {
1028 .name
= TYPE_VIRTIO_PCI
,
1029 .parent
= TYPE_PCI_DEVICE
,
1030 .instance_size
= sizeof(VirtIOPCIProxy
),
1031 .class_init
= virtio_pci_class_init
,
1032 .class_size
= sizeof(VirtioPCIClass
),
1036 /* virtio-blk-pci */
1038 static Property virtio_blk_pci_properties
[] = {
1039 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
1040 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1041 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1042 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1043 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
1044 DEFINE_PROP_BIT("x-data-plane", VirtIOBlkPCI
, blk
.data_plane
, 0, false),
1046 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy
, host_features
),
1047 DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlkPCI
, blk
),
1048 DEFINE_PROP_END_OF_LIST(),
1051 static int virtio_blk_pci_init(VirtIOPCIProxy
*vpci_dev
)
1053 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(vpci_dev
);
1054 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1055 virtio_blk_set_conf(vdev
, &(dev
->blk
));
1056 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1057 if (qdev_init(vdev
) < 0) {
1063 static void virtio_blk_pci_class_init(ObjectClass
*klass
, void *data
)
1065 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1066 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1067 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1069 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1070 dc
->props
= virtio_blk_pci_properties
;
1071 k
->init
= virtio_blk_pci_init
;
1072 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1073 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
1074 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1075 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1078 static void virtio_blk_pci_instance_init(Object
*obj
)
1080 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(obj
);
1081 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_BLK
);
1082 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1085 static const TypeInfo virtio_blk_pci_info
= {
1086 .name
= TYPE_VIRTIO_BLK_PCI
,
1087 .parent
= TYPE_VIRTIO_PCI
,
1088 .instance_size
= sizeof(VirtIOBlkPCI
),
1089 .instance_init
= virtio_blk_pci_instance_init
,
1090 .class_init
= virtio_blk_pci_class_init
,
1093 /* virtio-scsi-pci */
1095 static Property virtio_scsi_pci_properties
[] = {
1096 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1097 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1098 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1099 DEV_NVECTORS_UNSPECIFIED
),
1100 DEFINE_VIRTIO_SCSI_FEATURES(VirtIOPCIProxy
, host_features
),
1101 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSIPCI
, vdev
.parent_obj
.conf
),
1102 DEFINE_PROP_END_OF_LIST(),
1105 static int virtio_scsi_pci_init_pci(VirtIOPCIProxy
*vpci_dev
)
1107 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(vpci_dev
);
1108 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1109 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1110 DeviceState
*proxy
= DEVICE(vpci_dev
);
1113 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1114 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1118 * For command line compatibility, this sets the virtio-scsi-device bus
1122 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1123 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1127 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1128 if (qdev_init(vdev
) < 0) {
1134 static void virtio_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1136 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1137 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1138 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1139 k
->init
= virtio_scsi_pci_init_pci
;
1140 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1141 dc
->props
= virtio_scsi_pci_properties
;
1142 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1143 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1144 pcidev_k
->revision
= 0x00;
1145 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1148 static void virtio_scsi_pci_instance_init(Object
*obj
)
1150 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(obj
);
1151 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_SCSI
);
1152 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1155 static const TypeInfo virtio_scsi_pci_info
= {
1156 .name
= TYPE_VIRTIO_SCSI_PCI
,
1157 .parent
= TYPE_VIRTIO_PCI
,
1158 .instance_size
= sizeof(VirtIOSCSIPCI
),
1159 .instance_init
= virtio_scsi_pci_instance_init
,
1160 .class_init
= virtio_scsi_pci_class_init
,
1163 /* vhost-scsi-pci */
1165 #ifdef CONFIG_VHOST_SCSI
1166 static Property vhost_scsi_pci_properties
[] = {
1167 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1168 DEV_NVECTORS_UNSPECIFIED
),
1169 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1170 DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSIPCI
, vdev
.parent_obj
.conf
),
1171 DEFINE_PROP_END_OF_LIST(),
1174 static int vhost_scsi_pci_init_pci(VirtIOPCIProxy
*vpci_dev
)
1176 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(vpci_dev
);
1177 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1178 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1180 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1181 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1184 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1185 if (qdev_init(vdev
) < 0) {
1191 static void vhost_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1193 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1194 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1195 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1196 k
->init
= vhost_scsi_pci_init_pci
;
1197 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1198 dc
->props
= vhost_scsi_pci_properties
;
1199 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1200 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1201 pcidev_k
->revision
= 0x00;
1202 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1205 static void vhost_scsi_pci_instance_init(Object
*obj
)
1207 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(obj
);
1208 object_initialize(OBJECT(&dev
->vdev
), TYPE_VHOST_SCSI
);
1209 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1212 static const TypeInfo vhost_scsi_pci_info
= {
1213 .name
= TYPE_VHOST_SCSI_PCI
,
1214 .parent
= TYPE_VIRTIO_PCI
,
1215 .instance_size
= sizeof(VHostSCSIPCI
),
1216 .instance_init
= vhost_scsi_pci_instance_init
,
1217 .class_init
= vhost_scsi_pci_class_init
,
1221 /* virtio-balloon-pci */
1223 static void balloon_pci_stats_get_all(Object
*obj
, struct Visitor
*v
,
1224 void *opaque
, const char *name
,
1227 VirtIOBalloonPCI
*dev
= opaque
;
1228 object_property_get(OBJECT(&dev
->vdev
), v
, "guest-stats", errp
);
1231 static void balloon_pci_stats_get_poll_interval(Object
*obj
, struct Visitor
*v
,
1232 void *opaque
, const char *name
,
1235 VirtIOBalloonPCI
*dev
= opaque
;
1236 object_property_get(OBJECT(&dev
->vdev
), v
, "guest-stats-polling-interval",
1240 static void balloon_pci_stats_set_poll_interval(Object
*obj
, struct Visitor
*v
,
1241 void *opaque
, const char *name
,
1244 VirtIOBalloonPCI
*dev
= opaque
;
1245 object_property_set(OBJECT(&dev
->vdev
), v
, "guest-stats-polling-interval",
1249 static Property virtio_balloon_pci_properties
[] = {
1250 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1251 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
1252 DEFINE_PROP_END_OF_LIST(),
1255 static int virtio_balloon_pci_init(VirtIOPCIProxy
*vpci_dev
)
1257 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(vpci_dev
);
1258 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1260 if (vpci_dev
->class_code
!= PCI_CLASS_OTHERS
&&
1261 vpci_dev
->class_code
!= PCI_CLASS_MEMORY_RAM
) { /* qemu < 1.1 */
1262 vpci_dev
->class_code
= PCI_CLASS_OTHERS
;
1265 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1266 if (qdev_init(vdev
) < 0) {
1272 static void virtio_balloon_pci_class_init(ObjectClass
*klass
, void *data
)
1274 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1275 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1276 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1277 k
->init
= virtio_balloon_pci_init
;
1278 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1279 dc
->props
= virtio_balloon_pci_properties
;
1280 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1281 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BALLOON
;
1282 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1283 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
1286 static void virtio_balloon_pci_instance_init(Object
*obj
)
1288 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(obj
);
1289 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_BALLOON
);
1290 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1292 object_property_add(obj
, "guest-stats", "guest statistics",
1293 balloon_pci_stats_get_all
, NULL
, NULL
, dev
,
1296 object_property_add(obj
, "guest-stats-polling-interval", "int",
1297 balloon_pci_stats_get_poll_interval
,
1298 balloon_pci_stats_set_poll_interval
,
1302 static const TypeInfo virtio_balloon_pci_info
= {
1303 .name
= TYPE_VIRTIO_BALLOON_PCI
,
1304 .parent
= TYPE_VIRTIO_PCI
,
1305 .instance_size
= sizeof(VirtIOBalloonPCI
),
1306 .instance_init
= virtio_balloon_pci_instance_init
,
1307 .class_init
= virtio_balloon_pci_class_init
,
1310 /* virtio-serial-pci */
1312 static int virtio_serial_pci_init(VirtIOPCIProxy
*vpci_dev
)
1314 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(vpci_dev
);
1315 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1316 DeviceState
*proxy
= DEVICE(vpci_dev
);
1319 if (vpci_dev
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
1320 vpci_dev
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
1321 vpci_dev
->class_code
!= PCI_CLASS_OTHERS
) { /* qemu-kvm */
1322 vpci_dev
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
1325 /* backwards-compatibility with machines that were created with
1326 DEV_NVECTORS_UNSPECIFIED */
1327 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1328 vpci_dev
->nvectors
= dev
->vdev
.serial
.max_virtserial_ports
+ 1;
1332 * For command line compatibility, this sets the virtio-serial-device bus
1336 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1337 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1341 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1342 if (qdev_init(vdev
) < 0) {
1348 static Property virtio_serial_pci_properties
[] = {
1349 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1350 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1351 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1352 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
1353 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1354 DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtIOSerialPCI
, vdev
.serial
),
1355 DEFINE_PROP_END_OF_LIST(),
1358 static void virtio_serial_pci_class_init(ObjectClass
*klass
, void *data
)
1360 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1361 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1362 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1363 k
->init
= virtio_serial_pci_init
;
1364 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
1365 dc
->props
= virtio_serial_pci_properties
;
1366 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1367 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_CONSOLE
;
1368 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1369 pcidev_k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
1372 static void virtio_serial_pci_instance_init(Object
*obj
)
1374 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(obj
);
1375 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_SERIAL
);
1376 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1379 static const TypeInfo virtio_serial_pci_info
= {
1380 .name
= TYPE_VIRTIO_SERIAL_PCI
,
1381 .parent
= TYPE_VIRTIO_PCI
,
1382 .instance_size
= sizeof(VirtIOSerialPCI
),
1383 .instance_init
= virtio_serial_pci_instance_init
,
1384 .class_init
= virtio_serial_pci_class_init
,
1387 /* virtio-net-pci */
1389 static Property virtio_net_properties
[] = {
1390 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1391 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, false),
1392 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
1393 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy
, host_features
),
1394 DEFINE_NIC_PROPERTIES(VirtIONetPCI
, vdev
.nic_conf
),
1395 DEFINE_VIRTIO_NET_PROPERTIES(VirtIONetPCI
, vdev
.net_conf
),
1396 DEFINE_PROP_END_OF_LIST(),
1399 static int virtio_net_pci_init(VirtIOPCIProxy
*vpci_dev
)
1401 DeviceState
*qdev
= DEVICE(vpci_dev
);
1402 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(vpci_dev
);
1403 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1405 virtio_net_set_config_size(&dev
->vdev
, vpci_dev
->host_features
);
1406 virtio_net_set_netclient_name(&dev
->vdev
, qdev
->id
,
1407 object_get_typename(OBJECT(qdev
)));
1408 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1409 if (qdev_init(vdev
) < 0) {
1415 static void virtio_net_pci_class_init(ObjectClass
*klass
, void *data
)
1417 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1418 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1419 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_CLASS(klass
);
1421 k
->romfile
= "efi-virtio.rom";
1422 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1423 k
->device_id
= PCI_DEVICE_ID_VIRTIO_NET
;
1424 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1425 k
->class_id
= PCI_CLASS_NETWORK_ETHERNET
;
1426 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
1427 dc
->props
= virtio_net_properties
;
1428 vpciklass
->init
= virtio_net_pci_init
;
1431 static void virtio_net_pci_instance_init(Object
*obj
)
1433 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(obj
);
1434 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_NET
);
1435 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1438 static const TypeInfo virtio_net_pci_info
= {
1439 .name
= TYPE_VIRTIO_NET_PCI
,
1440 .parent
= TYPE_VIRTIO_PCI
,
1441 .instance_size
= sizeof(VirtIONetPCI
),
1442 .instance_init
= virtio_net_pci_instance_init
,
1443 .class_init
= virtio_net_pci_class_init
,
1446 /* virtio-rng-pci */
1448 static Property virtio_rng_pci_properties
[] = {
1449 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1450 DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORngPCI
, vdev
.conf
),
1451 DEFINE_PROP_END_OF_LIST(),
1454 static int virtio_rng_pci_init(VirtIOPCIProxy
*vpci_dev
)
1456 VirtIORngPCI
*vrng
= VIRTIO_RNG_PCI(vpci_dev
);
1457 DeviceState
*vdev
= DEVICE(&vrng
->vdev
);
1459 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1460 if (qdev_init(vdev
) < 0) {
1464 object_property_set_link(OBJECT(vrng
),
1465 OBJECT(vrng
->vdev
.conf
.rng
), "rng",
1471 static void virtio_rng_pci_class_init(ObjectClass
*klass
, void *data
)
1473 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1474 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1475 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1477 k
->init
= virtio_rng_pci_init
;
1478 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1479 dc
->props
= virtio_rng_pci_properties
;
1481 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1482 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_RNG
;
1483 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1484 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
1487 static void virtio_rng_initfn(Object
*obj
)
1489 VirtIORngPCI
*dev
= VIRTIO_RNG_PCI(obj
);
1490 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_RNG
);
1491 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1492 object_property_add_link(obj
, "rng", TYPE_RNG_BACKEND
,
1493 (Object
**)&dev
->vdev
.conf
.rng
, NULL
);
1497 static const TypeInfo virtio_rng_pci_info
= {
1498 .name
= TYPE_VIRTIO_RNG_PCI
,
1499 .parent
= TYPE_VIRTIO_PCI
,
1500 .instance_size
= sizeof(VirtIORngPCI
),
1501 .instance_init
= virtio_rng_initfn
,
1502 .class_init
= virtio_rng_pci_class_init
,
1505 /* virtio-pci-bus */
1507 static void virtio_pci_bus_new(VirtioBusState
*bus
, VirtIOPCIProxy
*dev
)
1509 DeviceState
*qdev
= DEVICE(dev
);
1511 char virtio_bus_name
[] = "virtio-bus";
1513 qbus_create_inplace((BusState
*)bus
, TYPE_VIRTIO_PCI_BUS
, qdev
,
1516 qbus
->allow_hotplug
= 1;
1519 static void virtio_pci_bus_class_init(ObjectClass
*klass
, void *data
)
1521 BusClass
*bus_class
= BUS_CLASS(klass
);
1522 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
1523 bus_class
->max_dev
= 1;
1524 k
->notify
= virtio_pci_notify
;
1525 k
->save_config
= virtio_pci_save_config
;
1526 k
->load_config
= virtio_pci_load_config
;
1527 k
->save_queue
= virtio_pci_save_queue
;
1528 k
->load_queue
= virtio_pci_load_queue
;
1529 k
->get_features
= virtio_pci_get_features
;
1530 k
->query_guest_notifiers
= virtio_pci_query_guest_notifiers
;
1531 k
->set_host_notifier
= virtio_pci_set_host_notifier
;
1532 k
->set_guest_notifiers
= virtio_pci_set_guest_notifiers
;
1533 k
->vmstate_change
= virtio_pci_vmstate_change
;
1534 k
->device_plugged
= virtio_pci_device_plugged
;
1537 static const TypeInfo virtio_pci_bus_info
= {
1538 .name
= TYPE_VIRTIO_PCI_BUS
,
1539 .parent
= TYPE_VIRTIO_BUS
,
1540 .instance_size
= sizeof(VirtioPCIBusState
),
1541 .class_init
= virtio_pci_bus_class_init
,
1544 static void virtio_pci_register_types(void)
1546 type_register_static(&virtio_rng_pci_info
);
1547 type_register_static(&virtio_pci_bus_info
);
1548 type_register_static(&virtio_pci_info
);
1549 #ifdef CONFIG_VIRTFS
1550 type_register_static(&virtio_9p_pci_info
);
1552 type_register_static(&virtio_blk_pci_info
);
1553 type_register_static(&virtio_scsi_pci_info
);
1554 type_register_static(&virtio_balloon_pci_info
);
1555 type_register_static(&virtio_serial_pci_info
);
1556 type_register_static(&virtio_net_pci_info
);
1557 #ifdef CONFIG_VHOST_SCSI
1558 type_register_static(&vhost_scsi_pci_info
);
1562 type_init(virtio_pci_register_types
)