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 /* QEMU doesn't strictly need write barriers since everything runs in
93 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
94 * KVM or if kqemu gets SMP support.
96 #define wmb() do { } while (0)
98 /* HACK for virtio to determine if it's running a big endian guest */
99 bool virtio_is_big_endian(void);
101 static void virtio_pci_bus_new(VirtioBusState
*bus
, VirtIOPCIProxy
*dev
);
104 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
105 static inline VirtIOPCIProxy
*to_virtio_pci_proxy(DeviceState
*d
)
107 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
110 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
111 * be careful and test performance if you change this.
113 static inline VirtIOPCIProxy
*to_virtio_pci_proxy_fast(DeviceState
*d
)
115 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
118 static void virtio_pci_notify(DeviceState
*d
, uint16_t vector
)
120 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy_fast(d
);
121 if (msix_enabled(&proxy
->pci_dev
))
122 msix_notify(&proxy
->pci_dev
, vector
);
124 qemu_set_irq(proxy
->pci_dev
.irq
[0], proxy
->vdev
->isr
& 1);
127 static void virtio_pci_save_config(DeviceState
*d
, QEMUFile
*f
)
129 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
130 pci_device_save(&proxy
->pci_dev
, f
);
131 msix_save(&proxy
->pci_dev
, f
);
132 if (msix_present(&proxy
->pci_dev
))
133 qemu_put_be16(f
, proxy
->vdev
->config_vector
);
136 static void virtio_pci_save_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
138 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
139 if (msix_present(&proxy
->pci_dev
))
140 qemu_put_be16(f
, virtio_queue_vector(proxy
->vdev
, n
));
143 static int virtio_pci_load_config(DeviceState
*d
, QEMUFile
*f
)
145 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
147 ret
= pci_device_load(&proxy
->pci_dev
, f
);
151 msix_unuse_all_vectors(&proxy
->pci_dev
);
152 msix_load(&proxy
->pci_dev
, f
);
153 if (msix_present(&proxy
->pci_dev
)) {
154 qemu_get_be16s(f
, &proxy
->vdev
->config_vector
);
156 proxy
->vdev
->config_vector
= VIRTIO_NO_VECTOR
;
158 if (proxy
->vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
159 return msix_vector_use(&proxy
->pci_dev
, proxy
->vdev
->config_vector
);
164 static int virtio_pci_load_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
166 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
168 if (msix_present(&proxy
->pci_dev
)) {
169 qemu_get_be16s(f
, &vector
);
171 vector
= VIRTIO_NO_VECTOR
;
173 virtio_queue_set_vector(proxy
->vdev
, n
, vector
);
174 if (vector
!= VIRTIO_NO_VECTOR
) {
175 return msix_vector_use(&proxy
->pci_dev
, vector
);
180 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy
*proxy
,
181 int n
, bool assign
, bool set_handler
)
183 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
184 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
188 r
= event_notifier_init(notifier
, 1);
190 error_report("%s: unable to init event notifier: %d",
194 virtio_queue_set_host_notifier_fd_handler(vq
, true, set_handler
);
195 memory_region_add_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
198 memory_region_del_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
200 virtio_queue_set_host_notifier_fd_handler(vq
, false, false);
201 event_notifier_cleanup(notifier
);
206 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
210 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) ||
211 proxy
->ioeventfd_disabled
||
212 proxy
->ioeventfd_started
) {
216 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
217 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
221 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, true, true);
226 proxy
->ioeventfd_started
= true;
231 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
235 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
238 proxy
->ioeventfd_started
= false;
239 error_report("%s: failed. Fallback to a userspace (slower).", __func__
);
242 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
247 if (!proxy
->ioeventfd_started
) {
251 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
252 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
256 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
259 proxy
->ioeventfd_started
= false;
262 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
264 VirtIOPCIProxy
*proxy
= opaque
;
265 VirtIODevice
*vdev
= proxy
->vdev
;
269 case VIRTIO_PCI_GUEST_FEATURES
:
270 /* Guest does not negotiate properly? We have to assume nothing. */
271 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
272 val
= virtio_bus_get_vdev_bad_features(&proxy
->bus
);
274 virtio_set_features(vdev
, val
);
276 case VIRTIO_PCI_QUEUE_PFN
:
277 pa
= (hwaddr
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
279 virtio_pci_stop_ioeventfd(proxy
);
280 virtio_reset(proxy
->vdev
);
281 msix_unuse_all_vectors(&proxy
->pci_dev
);
284 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
286 case VIRTIO_PCI_QUEUE_SEL
:
287 if (val
< VIRTIO_PCI_QUEUE_MAX
)
288 vdev
->queue_sel
= val
;
290 case VIRTIO_PCI_QUEUE_NOTIFY
:
291 if (val
< VIRTIO_PCI_QUEUE_MAX
) {
292 virtio_queue_notify(vdev
, val
);
295 case VIRTIO_PCI_STATUS
:
296 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
297 virtio_pci_stop_ioeventfd(proxy
);
300 virtio_set_status(vdev
, val
& 0xFF);
302 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
303 virtio_pci_start_ioeventfd(proxy
);
306 if (vdev
->status
== 0) {
307 virtio_reset(proxy
->vdev
);
308 msix_unuse_all_vectors(&proxy
->pci_dev
);
311 /* Linux before 2.6.34 sets the device as OK without enabling
312 the PCI device bus master bit. In this case we need to disable
313 some safety checks. */
314 if ((val
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
315 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
316 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
319 case VIRTIO_MSI_CONFIG_VECTOR
:
320 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
321 /* Make it possible for guest to discover an error took place. */
322 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
323 val
= VIRTIO_NO_VECTOR
;
324 vdev
->config_vector
= val
;
326 case VIRTIO_MSI_QUEUE_VECTOR
:
327 msix_vector_unuse(&proxy
->pci_dev
,
328 virtio_queue_vector(vdev
, vdev
->queue_sel
));
329 /* Make it possible for guest to discover an error took place. */
330 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
331 val
= VIRTIO_NO_VECTOR
;
332 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
335 error_report("%s: unexpected address 0x%x value 0x%x",
336 __func__
, addr
, val
);
341 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
343 VirtIODevice
*vdev
= proxy
->vdev
;
344 uint32_t ret
= 0xFFFFFFFF;
347 case VIRTIO_PCI_HOST_FEATURES
:
348 ret
= proxy
->host_features
;
350 case VIRTIO_PCI_GUEST_FEATURES
:
351 ret
= vdev
->guest_features
;
353 case VIRTIO_PCI_QUEUE_PFN
:
354 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
355 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
357 case VIRTIO_PCI_QUEUE_NUM
:
358 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
360 case VIRTIO_PCI_QUEUE_SEL
:
361 ret
= vdev
->queue_sel
;
363 case VIRTIO_PCI_STATUS
:
367 /* reading from the ISR also clears it. */
370 qemu_set_irq(proxy
->pci_dev
.irq
[0], 0);
372 case VIRTIO_MSI_CONFIG_VECTOR
:
373 ret
= vdev
->config_vector
;
375 case VIRTIO_MSI_QUEUE_VECTOR
:
376 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
385 static uint64_t virtio_pci_config_read(void *opaque
, hwaddr addr
,
388 VirtIOPCIProxy
*proxy
= opaque
;
389 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
392 return virtio_ioport_read(proxy
, addr
);
398 val
= virtio_config_readb(proxy
->vdev
, addr
);
401 val
= virtio_config_readw(proxy
->vdev
, addr
);
402 if (virtio_is_big_endian()) {
407 val
= virtio_config_readl(proxy
->vdev
, addr
);
408 if (virtio_is_big_endian()) {
416 static void virtio_pci_config_write(void *opaque
, hwaddr addr
,
417 uint64_t val
, unsigned size
)
419 VirtIOPCIProxy
*proxy
= opaque
;
420 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
422 virtio_ioport_write(proxy
, addr
, val
);
427 * Virtio-PCI is odd. Ioports are LE but config space is target native
432 virtio_config_writeb(proxy
->vdev
, addr
, val
);
435 if (virtio_is_big_endian()) {
438 virtio_config_writew(proxy
->vdev
, addr
, val
);
441 if (virtio_is_big_endian()) {
444 virtio_config_writel(proxy
->vdev
, addr
, val
);
449 static const MemoryRegionOps virtio_pci_config_ops
= {
450 .read
= virtio_pci_config_read
,
451 .write
= virtio_pci_config_write
,
453 .min_access_size
= 1,
454 .max_access_size
= 4,
456 .endianness
= DEVICE_LITTLE_ENDIAN
,
459 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
460 uint32_t val
, int len
)
462 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
464 pci_default_write_config(pci_dev
, address
, val
, len
);
466 if (range_covers_byte(address
, len
, PCI_COMMAND
) &&
467 !(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
) &&
468 !(proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG
)) {
469 virtio_pci_stop_ioeventfd(proxy
);
470 virtio_set_status(proxy
->vdev
,
471 proxy
->vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
475 static unsigned virtio_pci_get_features(DeviceState
*d
)
477 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
478 return proxy
->host_features
;
481 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy
*proxy
,
482 unsigned int queue_no
,
486 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
489 if (irqfd
->users
== 0) {
490 ret
= kvm_irqchip_add_msi_route(kvm_state
, msg
);
500 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy
*proxy
,
503 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
504 if (--irqfd
->users
== 0) {
505 kvm_irqchip_release_virq(kvm_state
, irqfd
->virq
);
509 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy
*proxy
,
510 unsigned int queue_no
,
513 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
514 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, queue_no
);
515 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
517 ret
= kvm_irqchip_add_irqfd_notifier(kvm_state
, n
, irqfd
->virq
);
521 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy
*proxy
,
522 unsigned int queue_no
,
525 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, queue_no
);
526 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
527 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
530 ret
= kvm_irqchip_remove_irqfd_notifier(kvm_state
, n
, irqfd
->virq
);
534 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy
*proxy
, int nvqs
)
536 PCIDevice
*dev
= &proxy
->pci_dev
;
537 VirtIODevice
*vdev
= proxy
->vdev
;
538 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
543 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
544 if (!virtio_queue_get_num(vdev
, queue_no
)) {
547 vector
= virtio_queue_vector(vdev
, queue_no
);
548 if (vector
>= msix_nr_vectors_allocated(dev
)) {
551 msg
= msix_get_message(dev
, vector
);
552 ret
= kvm_virtio_pci_vq_vector_use(proxy
, queue_no
, vector
, msg
);
556 /* If guest supports masking, set up irqfd now.
557 * Otherwise, delay until unmasked in the frontend.
559 if (k
->guest_notifier_mask
) {
560 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
562 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
570 while (--queue_no
>= 0) {
571 vector
= virtio_queue_vector(vdev
, queue_no
);
572 if (vector
>= msix_nr_vectors_allocated(dev
)) {
575 if (k
->guest_notifier_mask
) {
576 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
578 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
583 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy
*proxy
, int nvqs
)
585 PCIDevice
*dev
= &proxy
->pci_dev
;
586 VirtIODevice
*vdev
= proxy
->vdev
;
589 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
591 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
592 if (!virtio_queue_get_num(vdev
, queue_no
)) {
595 vector
= virtio_queue_vector(vdev
, queue_no
);
596 if (vector
>= msix_nr_vectors_allocated(dev
)) {
599 /* If guest supports masking, clean up irqfd now.
600 * Otherwise, it was cleaned when masked in the frontend.
602 if (k
->guest_notifier_mask
) {
603 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
605 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
609 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy
*proxy
,
610 unsigned int queue_no
,
614 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(proxy
->vdev
);
615 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, queue_no
);
616 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
620 if (proxy
->vector_irqfd
) {
621 irqfd
= &proxy
->vector_irqfd
[vector
];
622 if (irqfd
->msg
.data
!= msg
.data
|| irqfd
->msg
.address
!= msg
.address
) {
623 ret
= kvm_irqchip_update_msi_route(kvm_state
, irqfd
->virq
, msg
);
630 /* If guest supports masking, irqfd is already setup, unmask it.
631 * Otherwise, set it up now.
633 if (k
->guest_notifier_mask
) {
634 k
->guest_notifier_mask(proxy
->vdev
, queue_no
, false);
635 /* Test after unmasking to avoid losing events. */
636 if (k
->guest_notifier_pending
&&
637 k
->guest_notifier_pending(proxy
->vdev
, queue_no
)) {
638 event_notifier_set(n
);
641 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
646 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy
*proxy
,
647 unsigned int queue_no
,
650 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(proxy
->vdev
);
652 /* If guest supports masking, keep irqfd but mask it.
653 * Otherwise, clean it up now.
655 if (k
->guest_notifier_mask
) {
656 k
->guest_notifier_mask(proxy
->vdev
, queue_no
, true);
658 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
662 static int virtio_pci_vector_unmask(PCIDevice
*dev
, unsigned vector
,
665 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
666 VirtIODevice
*vdev
= proxy
->vdev
;
669 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
670 if (!virtio_queue_get_num(vdev
, queue_no
)) {
673 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
676 ret
= virtio_pci_vq_vector_unmask(proxy
, queue_no
, vector
, msg
);
684 while (--queue_no
>= 0) {
685 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
688 virtio_pci_vq_vector_mask(proxy
, queue_no
, vector
);
693 static void virtio_pci_vector_mask(PCIDevice
*dev
, unsigned vector
)
695 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
696 VirtIODevice
*vdev
= proxy
->vdev
;
699 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
700 if (!virtio_queue_get_num(vdev
, queue_no
)) {
703 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
706 virtio_pci_vq_vector_mask(proxy
, queue_no
, vector
);
710 static void virtio_pci_vector_poll(PCIDevice
*dev
,
711 unsigned int vector_start
,
712 unsigned int vector_end
)
714 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
715 VirtIODevice
*vdev
= proxy
->vdev
;
716 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
719 EventNotifier
*notifier
;
722 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
723 if (!virtio_queue_get_num(vdev
, queue_no
)) {
726 vector
= virtio_queue_vector(vdev
, queue_no
);
727 if (vector
< vector_start
|| vector
>= vector_end
||
728 !msix_is_masked(dev
, vector
)) {
731 vq
= virtio_get_queue(vdev
, queue_no
);
732 notifier
= virtio_queue_get_guest_notifier(vq
);
733 if (k
->guest_notifier_pending
) {
734 if (k
->guest_notifier_pending(vdev
, queue_no
)) {
735 msix_set_pending(dev
, vector
);
737 } else if (event_notifier_test_and_clear(notifier
)) {
738 msix_set_pending(dev
, vector
);
743 static int virtio_pci_set_guest_notifier(DeviceState
*d
, int n
, bool assign
,
746 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
747 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(proxy
->vdev
);
748 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
749 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
752 int r
= event_notifier_init(notifier
, 0);
756 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
758 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
759 event_notifier_cleanup(notifier
);
762 if (!msix_enabled(&proxy
->pci_dev
) && vdc
->guest_notifier_mask
) {
763 vdc
->guest_notifier_mask(proxy
->vdev
, n
, !assign
);
769 static bool virtio_pci_query_guest_notifiers(DeviceState
*d
)
771 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
772 return msix_enabled(&proxy
->pci_dev
);
775 static int virtio_pci_set_guest_notifiers(DeviceState
*d
, int nvqs
, bool assign
)
777 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
778 VirtIODevice
*vdev
= proxy
->vdev
;
779 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
781 bool with_irqfd
= msix_enabled(&proxy
->pci_dev
) &&
782 kvm_msi_via_irqfd_enabled();
784 nvqs
= MIN(nvqs
, VIRTIO_PCI_QUEUE_MAX
);
786 /* When deassigning, pass a consistent nvqs value
787 * to avoid leaking notifiers.
789 assert(assign
|| nvqs
== proxy
->nvqs_with_notifiers
);
791 proxy
->nvqs_with_notifiers
= nvqs
;
793 /* Must unset vector notifier while guest notifier is still assigned */
794 if ((proxy
->vector_irqfd
|| k
->guest_notifier_mask
) && !assign
) {
795 msix_unset_vector_notifiers(&proxy
->pci_dev
);
796 if (proxy
->vector_irqfd
) {
797 kvm_virtio_pci_vector_release(proxy
, nvqs
);
798 g_free(proxy
->vector_irqfd
);
799 proxy
->vector_irqfd
= NULL
;
803 for (n
= 0; n
< nvqs
; n
++) {
804 if (!virtio_queue_get_num(vdev
, n
)) {
808 r
= virtio_pci_set_guest_notifier(d
, n
, assign
,
809 kvm_msi_via_irqfd_enabled());
815 /* Must set vector notifier after guest notifier has been assigned */
816 if ((with_irqfd
|| k
->guest_notifier_mask
) && assign
) {
818 proxy
->vector_irqfd
=
819 g_malloc0(sizeof(*proxy
->vector_irqfd
) *
820 msix_nr_vectors_allocated(&proxy
->pci_dev
));
821 r
= kvm_virtio_pci_vector_use(proxy
, nvqs
);
826 r
= msix_set_vector_notifiers(&proxy
->pci_dev
,
827 virtio_pci_vector_unmask
,
828 virtio_pci_vector_mask
,
829 virtio_pci_vector_poll
);
831 goto notifiers_error
;
840 kvm_virtio_pci_vector_release(proxy
, nvqs
);
844 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
847 virtio_pci_set_guest_notifier(d
, n
, !assign
, with_irqfd
);
852 static int virtio_pci_set_host_notifier(DeviceState
*d
, int n
, bool assign
)
854 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
856 /* Stop using ioeventfd for virtqueue kick if the device starts using host
857 * notifiers. This makes it easy to avoid stepping on each others' toes.
859 proxy
->ioeventfd_disabled
= assign
;
861 virtio_pci_stop_ioeventfd(proxy
);
863 /* We don't need to start here: it's not needed because backend
864 * currently only stops on status change away from ok,
865 * reset, vmstop and such. If we do add code to start here,
866 * need to check vmstate, device state etc. */
867 return virtio_pci_set_host_notifier_internal(proxy
, n
, assign
, false);
870 static void virtio_pci_vmstate_change(DeviceState
*d
, bool running
)
872 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
875 /* Try to find out if the guest has bus master disabled, but is
876 in ready state. Then we have a buggy guest OS. */
877 if ((proxy
->vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
878 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
879 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
881 virtio_pci_start_ioeventfd(proxy
);
883 virtio_pci_stop_ioeventfd(proxy
);
888 static int virtio_9p_init_pci(VirtIOPCIProxy
*vpci_dev
)
890 V9fsPCIState
*dev
= VIRTIO_9P_PCI(vpci_dev
);
891 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
893 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
894 if (qdev_init(vdev
) < 0) {
900 static Property virtio_9p_pci_properties
[] = {
901 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
902 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
903 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
904 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
905 DEFINE_VIRTIO_9P_PROPERTIES(V9fsPCIState
, vdev
.fsconf
),
906 DEFINE_PROP_END_OF_LIST(),
909 static void virtio_9p_pci_class_init(ObjectClass
*klass
, void *data
)
911 DeviceClass
*dc
= DEVICE_CLASS(klass
);
912 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
913 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
915 k
->init
= virtio_9p_init_pci
;
916 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
917 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_9P
;
918 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
919 pcidev_k
->class_id
= 0x2;
920 dc
->props
= virtio_9p_pci_properties
;
923 static void virtio_9p_pci_instance_init(Object
*obj
)
925 V9fsPCIState
*dev
= VIRTIO_9P_PCI(obj
);
926 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_9P
);
927 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
930 static const TypeInfo virtio_9p_pci_info
= {
931 .name
= TYPE_VIRTIO_9P_PCI
,
932 .parent
= TYPE_VIRTIO_PCI
,
933 .instance_size
= sizeof(V9fsPCIState
),
934 .instance_init
= virtio_9p_pci_instance_init
,
935 .class_init
= virtio_9p_pci_class_init
,
937 #endif /* CONFIG_VIRTFS */
940 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
943 /* This is called by virtio-bus just after the device is plugged. */
944 static void virtio_pci_device_plugged(DeviceState
*d
)
946 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
947 VirtioBusState
*bus
= &proxy
->bus
;
951 proxy
->vdev
= bus
->vdev
;
953 config
= proxy
->pci_dev
.config
;
954 if (proxy
->class_code
) {
955 pci_config_set_class(config
, proxy
->class_code
);
957 pci_set_word(config
+ PCI_SUBSYSTEM_VENDOR_ID
,
958 pci_get_word(config
+ PCI_VENDOR_ID
));
959 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, virtio_bus_get_vdev_id(bus
));
960 config
[PCI_INTERRUPT_PIN
] = 1;
962 if (proxy
->nvectors
&&
963 msix_init_exclusive_bar(&proxy
->pci_dev
, proxy
->nvectors
, 1)) {
967 proxy
->pci_dev
.config_write
= virtio_write_config
;
969 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
)
970 + virtio_bus_get_vdev_config_len(bus
);
971 if (size
& (size
- 1)) {
972 size
= 1 << qemu_fls(size
);
975 memory_region_init_io(&proxy
->bar
, &virtio_pci_config_ops
, proxy
,
977 pci_register_bar(&proxy
->pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
,
980 if (!kvm_has_many_ioeventfds()) {
981 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
984 proxy
->host_features
|= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY
;
985 proxy
->host_features
|= 0x1 << VIRTIO_F_BAD_FEATURE
;
986 proxy
->host_features
= virtio_bus_get_vdev_features(bus
,
987 proxy
->host_features
);
990 static int virtio_pci_init(PCIDevice
*pci_dev
)
992 VirtIOPCIProxy
*dev
= VIRTIO_PCI(pci_dev
);
993 VirtioPCIClass
*k
= VIRTIO_PCI_GET_CLASS(pci_dev
);
994 virtio_pci_bus_new(&dev
->bus
, dev
);
995 if (k
->init
!= NULL
) {
1001 static void virtio_pci_exit(PCIDevice
*pci_dev
)
1003 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
1004 virtio_pci_stop_ioeventfd(proxy
);
1005 memory_region_destroy(&proxy
->bar
);
1006 msix_uninit_exclusive_bar(pci_dev
);
1009 static void virtio_pci_reset(DeviceState
*qdev
)
1011 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1012 VirtioBusState
*bus
= VIRTIO_BUS(&proxy
->bus
);
1013 virtio_pci_stop_ioeventfd(proxy
);
1014 virtio_bus_reset(bus
);
1015 msix_unuse_all_vectors(&proxy
->pci_dev
);
1016 proxy
->flags
&= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
1019 static void virtio_pci_class_init(ObjectClass
*klass
, void *data
)
1021 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1022 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1024 k
->init
= virtio_pci_init
;
1025 k
->exit
= virtio_pci_exit
;
1026 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1027 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1028 k
->class_id
= PCI_CLASS_OTHERS
;
1029 dc
->reset
= virtio_pci_reset
;
1032 static const TypeInfo virtio_pci_info
= {
1033 .name
= TYPE_VIRTIO_PCI
,
1034 .parent
= TYPE_PCI_DEVICE
,
1035 .instance_size
= sizeof(VirtIOPCIProxy
),
1036 .class_init
= virtio_pci_class_init
,
1037 .class_size
= sizeof(VirtioPCIClass
),
1041 /* virtio-blk-pci */
1043 static Property virtio_blk_pci_properties
[] = {
1044 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
1045 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1046 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1047 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1048 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
1049 DEFINE_PROP_BIT("x-data-plane", VirtIOBlkPCI
, blk
.data_plane
, 0, false),
1051 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy
, host_features
),
1052 DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlkPCI
, blk
),
1053 DEFINE_PROP_END_OF_LIST(),
1056 static int virtio_blk_pci_init(VirtIOPCIProxy
*vpci_dev
)
1058 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(vpci_dev
);
1059 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1060 virtio_blk_set_conf(vdev
, &(dev
->blk
));
1061 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1062 if (qdev_init(vdev
) < 0) {
1068 static void virtio_blk_pci_class_init(ObjectClass
*klass
, void *data
)
1070 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1071 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1072 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1074 dc
->props
= virtio_blk_pci_properties
;
1075 k
->init
= virtio_blk_pci_init
;
1076 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1077 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
1078 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1079 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1082 static void virtio_blk_pci_instance_init(Object
*obj
)
1084 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(obj
);
1085 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_BLK
);
1086 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1089 static const TypeInfo virtio_blk_pci_info
= {
1090 .name
= TYPE_VIRTIO_BLK_PCI
,
1091 .parent
= TYPE_VIRTIO_PCI
,
1092 .instance_size
= sizeof(VirtIOBlkPCI
),
1093 .instance_init
= virtio_blk_pci_instance_init
,
1094 .class_init
= virtio_blk_pci_class_init
,
1097 /* virtio-scsi-pci */
1099 static Property virtio_scsi_pci_properties
[] = {
1100 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1101 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1102 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1103 DEV_NVECTORS_UNSPECIFIED
),
1104 DEFINE_VIRTIO_SCSI_FEATURES(VirtIOPCIProxy
, host_features
),
1105 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSIPCI
, vdev
.parent_obj
.conf
),
1106 DEFINE_PROP_END_OF_LIST(),
1109 static int virtio_scsi_pci_init_pci(VirtIOPCIProxy
*vpci_dev
)
1111 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(vpci_dev
);
1112 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1113 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1114 DeviceState
*proxy
= DEVICE(vpci_dev
);
1117 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1118 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1122 * For command line compatibility, this sets the virtio-scsi-device bus
1126 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1127 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1131 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1132 if (qdev_init(vdev
) < 0) {
1138 static void virtio_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1140 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1141 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1142 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1143 k
->init
= virtio_scsi_pci_init_pci
;
1144 dc
->props
= virtio_scsi_pci_properties
;
1145 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1146 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1147 pcidev_k
->revision
= 0x00;
1148 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1151 static void virtio_scsi_pci_instance_init(Object
*obj
)
1153 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(obj
);
1154 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_SCSI
);
1155 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1158 static const TypeInfo virtio_scsi_pci_info
= {
1159 .name
= TYPE_VIRTIO_SCSI_PCI
,
1160 .parent
= TYPE_VIRTIO_PCI
,
1161 .instance_size
= sizeof(VirtIOSCSIPCI
),
1162 .instance_init
= virtio_scsi_pci_instance_init
,
1163 .class_init
= virtio_scsi_pci_class_init
,
1166 /* vhost-scsi-pci */
1168 #ifdef CONFIG_VHOST_SCSI
1169 static Property vhost_scsi_pci_properties
[] = {
1170 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1171 DEV_NVECTORS_UNSPECIFIED
),
1172 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1173 DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSIPCI
, vdev
.parent_obj
.conf
),
1174 DEFINE_PROP_END_OF_LIST(),
1177 static int vhost_scsi_pci_init_pci(VirtIOPCIProxy
*vpci_dev
)
1179 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(vpci_dev
);
1180 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1181 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1183 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1184 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1187 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1188 if (qdev_init(vdev
) < 0) {
1194 static void vhost_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1196 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1197 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1198 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1199 k
->init
= vhost_scsi_pci_init_pci
;
1200 dc
->props
= vhost_scsi_pci_properties
;
1201 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1202 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1203 pcidev_k
->revision
= 0x00;
1204 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1207 static void vhost_scsi_pci_instance_init(Object
*obj
)
1209 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(obj
);
1210 object_initialize(OBJECT(&dev
->vdev
), TYPE_VHOST_SCSI
);
1211 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1214 static const TypeInfo vhost_scsi_pci_info
= {
1215 .name
= TYPE_VHOST_SCSI_PCI
,
1216 .parent
= TYPE_VIRTIO_PCI
,
1217 .instance_size
= sizeof(VHostSCSIPCI
),
1218 .instance_init
= vhost_scsi_pci_instance_init
,
1219 .class_init
= vhost_scsi_pci_class_init
,
1223 /* virtio-balloon-pci */
1225 static void balloon_pci_stats_get_all(Object
*obj
, struct Visitor
*v
,
1226 void *opaque
, const char *name
,
1229 VirtIOBalloonPCI
*dev
= opaque
;
1230 object_property_get(OBJECT(&dev
->vdev
), v
, "guest-stats", errp
);
1233 static void balloon_pci_stats_get_poll_interval(Object
*obj
, struct Visitor
*v
,
1234 void *opaque
, const char *name
,
1237 VirtIOBalloonPCI
*dev
= opaque
;
1238 object_property_get(OBJECT(&dev
->vdev
), v
, "guest-stats-polling-interval",
1242 static void balloon_pci_stats_set_poll_interval(Object
*obj
, struct Visitor
*v
,
1243 void *opaque
, const char *name
,
1246 VirtIOBalloonPCI
*dev
= opaque
;
1247 object_property_set(OBJECT(&dev
->vdev
), v
, "guest-stats-polling-interval",
1251 static Property virtio_balloon_pci_properties
[] = {
1252 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1253 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
1254 DEFINE_PROP_END_OF_LIST(),
1257 static int virtio_balloon_pci_init(VirtIOPCIProxy
*vpci_dev
)
1259 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(vpci_dev
);
1260 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1262 if (vpci_dev
->class_code
!= PCI_CLASS_OTHERS
&&
1263 vpci_dev
->class_code
!= PCI_CLASS_MEMORY_RAM
) { /* qemu < 1.1 */
1264 vpci_dev
->class_code
= PCI_CLASS_OTHERS
;
1267 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1268 if (qdev_init(vdev
) < 0) {
1274 static void virtio_balloon_pci_class_init(ObjectClass
*klass
, void *data
)
1276 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1277 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1278 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1279 k
->init
= virtio_balloon_pci_init
;
1280 dc
->props
= virtio_balloon_pci_properties
;
1281 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1282 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BALLOON
;
1283 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1284 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
1287 static void virtio_balloon_pci_instance_init(Object
*obj
)
1289 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(obj
);
1290 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_BALLOON
);
1291 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1293 object_property_add(obj
, "guest-stats", "guest statistics",
1294 balloon_pci_stats_get_all
, NULL
, NULL
, dev
,
1297 object_property_add(obj
, "guest-stats-polling-interval", "int",
1298 balloon_pci_stats_get_poll_interval
,
1299 balloon_pci_stats_set_poll_interval
,
1303 static const TypeInfo virtio_balloon_pci_info
= {
1304 .name
= TYPE_VIRTIO_BALLOON_PCI
,
1305 .parent
= TYPE_VIRTIO_PCI
,
1306 .instance_size
= sizeof(VirtIOBalloonPCI
),
1307 .instance_init
= virtio_balloon_pci_instance_init
,
1308 .class_init
= virtio_balloon_pci_class_init
,
1311 /* virtio-serial-pci */
1313 static int virtio_serial_pci_init(VirtIOPCIProxy
*vpci_dev
)
1315 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(vpci_dev
);
1316 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1317 DeviceState
*proxy
= DEVICE(vpci_dev
);
1320 if (vpci_dev
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
1321 vpci_dev
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
1322 vpci_dev
->class_code
!= PCI_CLASS_OTHERS
) { /* qemu-kvm */
1323 vpci_dev
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
1326 /* backwards-compatibility with machines that were created with
1327 DEV_NVECTORS_UNSPECIFIED */
1328 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1329 vpci_dev
->nvectors
= dev
->vdev
.serial
.max_virtserial_ports
+ 1;
1333 * For command line compatibility, this sets the virtio-serial-device bus
1337 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1338 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1342 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1343 if (qdev_init(vdev
) < 0) {
1349 static Property virtio_serial_pci_properties
[] = {
1350 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1351 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1352 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1353 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
1354 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1355 DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtIOSerialPCI
, vdev
.serial
),
1356 DEFINE_PROP_END_OF_LIST(),
1359 static void virtio_serial_pci_class_init(ObjectClass
*klass
, void *data
)
1361 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1362 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1363 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1364 k
->init
= virtio_serial_pci_init
;
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 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(vpci_dev
);
1402 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1404 virtio_net_set_config_size(&dev
->vdev
, vpci_dev
->host_features
);
1405 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1406 if (qdev_init(vdev
) < 0) {
1412 static void virtio_net_pci_class_init(ObjectClass
*klass
, void *data
)
1414 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1415 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1416 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_CLASS(klass
);
1418 k
->romfile
= "efi-virtio.rom";
1419 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1420 k
->device_id
= PCI_DEVICE_ID_VIRTIO_NET
;
1421 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1422 k
->class_id
= PCI_CLASS_NETWORK_ETHERNET
;
1423 dc
->props
= virtio_net_properties
;
1424 vpciklass
->init
= virtio_net_pci_init
;
1427 static void virtio_net_pci_instance_init(Object
*obj
)
1429 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(obj
);
1430 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_NET
);
1431 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1434 static const TypeInfo virtio_net_pci_info
= {
1435 .name
= TYPE_VIRTIO_NET_PCI
,
1436 .parent
= TYPE_VIRTIO_PCI
,
1437 .instance_size
= sizeof(VirtIONetPCI
),
1438 .instance_init
= virtio_net_pci_instance_init
,
1439 .class_init
= virtio_net_pci_class_init
,
1442 /* virtio-rng-pci */
1444 static Property virtio_rng_pci_properties
[] = {
1445 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1446 DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORngPCI
, vdev
.conf
),
1447 DEFINE_PROP_END_OF_LIST(),
1450 static int virtio_rng_pci_init(VirtIOPCIProxy
*vpci_dev
)
1452 VirtIORngPCI
*vrng
= VIRTIO_RNG_PCI(vpci_dev
);
1453 DeviceState
*vdev
= DEVICE(&vrng
->vdev
);
1455 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1456 if (qdev_init(vdev
) < 0) {
1460 object_property_set_link(OBJECT(vrng
),
1461 OBJECT(vrng
->vdev
.conf
.default_backend
), "rng",
1467 static void virtio_rng_pci_class_init(ObjectClass
*klass
, void *data
)
1469 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1470 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1471 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1473 k
->init
= virtio_rng_pci_init
;
1474 dc
->props
= virtio_rng_pci_properties
;
1476 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1477 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_RNG
;
1478 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1479 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
1482 static void virtio_rng_initfn(Object
*obj
)
1484 VirtIORngPCI
*dev
= VIRTIO_RNG_PCI(obj
);
1485 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_RNG
);
1486 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1487 object_property_add_link(obj
, "rng", TYPE_RNG_BACKEND
,
1488 (Object
**)&dev
->vdev
.conf
.rng
, NULL
);
1492 static const TypeInfo virtio_rng_pci_info
= {
1493 .name
= TYPE_VIRTIO_RNG_PCI
,
1494 .parent
= TYPE_VIRTIO_PCI
,
1495 .instance_size
= sizeof(VirtIORngPCI
),
1496 .instance_init
= virtio_rng_initfn
,
1497 .class_init
= virtio_rng_pci_class_init
,
1500 /* virtio-pci-bus */
1502 static void virtio_pci_bus_new(VirtioBusState
*bus
, VirtIOPCIProxy
*dev
)
1504 DeviceState
*qdev
= DEVICE(dev
);
1506 char virtio_bus_name
[] = "virtio-bus";
1508 qbus_create_inplace((BusState
*)bus
, TYPE_VIRTIO_PCI_BUS
, qdev
,
1511 qbus
->allow_hotplug
= 1;
1514 static void virtio_pci_bus_class_init(ObjectClass
*klass
, void *data
)
1516 BusClass
*bus_class
= BUS_CLASS(klass
);
1517 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
1518 bus_class
->max_dev
= 1;
1519 k
->notify
= virtio_pci_notify
;
1520 k
->save_config
= virtio_pci_save_config
;
1521 k
->load_config
= virtio_pci_load_config
;
1522 k
->save_queue
= virtio_pci_save_queue
;
1523 k
->load_queue
= virtio_pci_load_queue
;
1524 k
->get_features
= virtio_pci_get_features
;
1525 k
->query_guest_notifiers
= virtio_pci_query_guest_notifiers
;
1526 k
->set_host_notifier
= virtio_pci_set_host_notifier
;
1527 k
->set_guest_notifiers
= virtio_pci_set_guest_notifiers
;
1528 k
->vmstate_change
= virtio_pci_vmstate_change
;
1529 k
->device_plugged
= virtio_pci_device_plugged
;
1532 static const TypeInfo virtio_pci_bus_info
= {
1533 .name
= TYPE_VIRTIO_PCI_BUS
,
1534 .parent
= TYPE_VIRTIO_BUS
,
1535 .instance_size
= sizeof(VirtioPCIBusState
),
1536 .class_init
= virtio_pci_bus_class_init
,
1539 static void virtio_pci_register_types(void)
1541 type_register_static(&virtio_rng_pci_info
);
1542 type_register_static(&virtio_pci_bus_info
);
1543 type_register_static(&virtio_pci_info
);
1544 #ifdef CONFIG_VIRTFS
1545 type_register_static(&virtio_9p_pci_info
);
1547 type_register_static(&virtio_blk_pci_info
);
1548 type_register_static(&virtio_scsi_pci_info
);
1549 type_register_static(&virtio_balloon_pci_info
);
1550 type_register_static(&virtio_serial_pci_info
);
1551 type_register_static(&virtio_net_pci_info
);
1552 #ifdef CONFIG_VHOST_SCSI
1553 type_register_static(&vhost_scsi_pci_info
);
1557 type_init(virtio_pci_register_types
)