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