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
, size_t bus_size
,
99 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
100 static inline VirtIOPCIProxy
*to_virtio_pci_proxy(DeviceState
*d
)
102 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
105 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
106 * be careful and test performance if you change this.
108 static inline VirtIOPCIProxy
*to_virtio_pci_proxy_fast(DeviceState
*d
)
110 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
113 static void virtio_pci_notify(DeviceState
*d
, uint16_t vector
)
115 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy_fast(d
);
117 if (msix_enabled(&proxy
->pci_dev
))
118 msix_notify(&proxy
->pci_dev
, vector
);
120 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
121 pci_set_irq(&proxy
->pci_dev
, vdev
->isr
& 1);
125 static void virtio_pci_save_config(DeviceState
*d
, QEMUFile
*f
)
127 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
128 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
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
, 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 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
141 if (msix_present(&proxy
->pci_dev
))
142 qemu_put_be16(f
, virtio_queue_vector(vdev
, n
));
145 static int virtio_pci_load_config(DeviceState
*d
, QEMUFile
*f
)
147 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
148 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
151 ret
= pci_device_load(&proxy
->pci_dev
, f
);
155 msix_unuse_all_vectors(&proxy
->pci_dev
);
156 msix_load(&proxy
->pci_dev
, f
);
157 if (msix_present(&proxy
->pci_dev
)) {
158 qemu_get_be16s(f
, &vdev
->config_vector
);
160 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
162 if (vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
163 return msix_vector_use(&proxy
->pci_dev
, vdev
->config_vector
);
168 static int virtio_pci_load_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
170 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
171 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
174 if (msix_present(&proxy
->pci_dev
)) {
175 qemu_get_be16s(f
, &vector
);
177 vector
= VIRTIO_NO_VECTOR
;
179 virtio_queue_set_vector(vdev
, n
, vector
);
180 if (vector
!= VIRTIO_NO_VECTOR
) {
181 return msix_vector_use(&proxy
->pci_dev
, vector
);
186 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy
*proxy
,
187 int n
, bool assign
, bool set_handler
)
189 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
190 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
191 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
195 r
= event_notifier_init(notifier
, 1);
197 error_report("%s: unable to init event notifier: %d",
201 virtio_queue_set_host_notifier_fd_handler(vq
, true, set_handler
);
202 memory_region_add_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
205 memory_region_del_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
207 virtio_queue_set_host_notifier_fd_handler(vq
, false, false);
208 event_notifier_cleanup(notifier
);
213 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
215 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
218 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) ||
219 proxy
->ioeventfd_disabled
||
220 proxy
->ioeventfd_started
) {
224 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
225 if (!virtio_queue_get_num(vdev
, n
)) {
229 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, true, true);
234 proxy
->ioeventfd_started
= true;
239 if (!virtio_queue_get_num(vdev
, n
)) {
243 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
246 proxy
->ioeventfd_started
= false;
247 error_report("%s: failed. Fallback to a userspace (slower).", __func__
);
250 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
252 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
256 if (!proxy
->ioeventfd_started
) {
260 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
261 if (!virtio_queue_get_num(vdev
, n
)) {
265 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
268 proxy
->ioeventfd_started
= false;
271 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
273 VirtIOPCIProxy
*proxy
= opaque
;
274 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
278 case VIRTIO_PCI_GUEST_FEATURES
:
279 /* Guest does not negotiate properly? We have to assume nothing. */
280 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
281 val
= virtio_bus_get_vdev_bad_features(&proxy
->bus
);
283 virtio_set_features(vdev
, val
);
285 case VIRTIO_PCI_QUEUE_PFN
:
286 pa
= (hwaddr
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
288 virtio_pci_stop_ioeventfd(proxy
);
290 msix_unuse_all_vectors(&proxy
->pci_dev
);
293 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
295 case VIRTIO_PCI_QUEUE_SEL
:
296 if (val
< VIRTIO_PCI_QUEUE_MAX
)
297 vdev
->queue_sel
= val
;
299 case VIRTIO_PCI_QUEUE_NOTIFY
:
300 if (val
< VIRTIO_PCI_QUEUE_MAX
) {
301 virtio_queue_notify(vdev
, val
);
304 case VIRTIO_PCI_STATUS
:
305 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
306 virtio_pci_stop_ioeventfd(proxy
);
309 virtio_set_status(vdev
, val
& 0xFF);
311 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
312 virtio_pci_start_ioeventfd(proxy
);
315 if (vdev
->status
== 0) {
317 msix_unuse_all_vectors(&proxy
->pci_dev
);
320 /* Linux before 2.6.34 sets the device as OK without enabling
321 the PCI device bus master bit. In this case we need to disable
322 some safety checks. */
323 if ((val
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
324 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
325 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
328 case VIRTIO_MSI_CONFIG_VECTOR
:
329 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
330 /* Make it possible for guest to discover an error took place. */
331 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
332 val
= VIRTIO_NO_VECTOR
;
333 vdev
->config_vector
= val
;
335 case VIRTIO_MSI_QUEUE_VECTOR
:
336 msix_vector_unuse(&proxy
->pci_dev
,
337 virtio_queue_vector(vdev
, vdev
->queue_sel
));
338 /* Make it possible for guest to discover an error took place. */
339 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
340 val
= VIRTIO_NO_VECTOR
;
341 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
344 error_report("%s: unexpected address 0x%x value 0x%x",
345 __func__
, addr
, val
);
350 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
352 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
353 uint32_t ret
= 0xFFFFFFFF;
356 case VIRTIO_PCI_HOST_FEATURES
:
357 ret
= proxy
->host_features
;
359 case VIRTIO_PCI_GUEST_FEATURES
:
360 ret
= vdev
->guest_features
;
362 case VIRTIO_PCI_QUEUE_PFN
:
363 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
364 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
366 case VIRTIO_PCI_QUEUE_NUM
:
367 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
369 case VIRTIO_PCI_QUEUE_SEL
:
370 ret
= vdev
->queue_sel
;
372 case VIRTIO_PCI_STATUS
:
376 /* reading from the ISR also clears it. */
379 pci_irq_deassert(&proxy
->pci_dev
);
381 case VIRTIO_MSI_CONFIG_VECTOR
:
382 ret
= vdev
->config_vector
;
384 case VIRTIO_MSI_QUEUE_VECTOR
:
385 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
394 static uint64_t virtio_pci_config_read(void *opaque
, hwaddr addr
,
397 VirtIOPCIProxy
*proxy
= opaque
;
398 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
399 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
402 return virtio_ioport_read(proxy
, addr
);
408 val
= virtio_config_readb(vdev
, addr
);
411 val
= virtio_config_readw(vdev
, addr
);
412 if (virtio_is_big_endian()) {
417 val
= virtio_config_readl(vdev
, addr
);
418 if (virtio_is_big_endian()) {
426 static void virtio_pci_config_write(void *opaque
, hwaddr addr
,
427 uint64_t val
, unsigned size
)
429 VirtIOPCIProxy
*proxy
= opaque
;
430 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
431 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
433 virtio_ioport_write(proxy
, addr
, val
);
438 * Virtio-PCI is odd. Ioports are LE but config space is target native
443 virtio_config_writeb(vdev
, addr
, val
);
446 if (virtio_is_big_endian()) {
449 virtio_config_writew(vdev
, addr
, val
);
452 if (virtio_is_big_endian()) {
455 virtio_config_writel(vdev
, addr
, val
);
460 static const MemoryRegionOps virtio_pci_config_ops
= {
461 .read
= virtio_pci_config_read
,
462 .write
= virtio_pci_config_write
,
464 .min_access_size
= 1,
465 .max_access_size
= 4,
467 .endianness
= DEVICE_LITTLE_ENDIAN
,
470 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
471 uint32_t val
, int len
)
473 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
474 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
476 pci_default_write_config(pci_dev
, address
, val
, len
);
478 if (range_covers_byte(address
, len
, PCI_COMMAND
) &&
479 !(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
) &&
480 !(proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG
)) {
481 virtio_pci_stop_ioeventfd(proxy
);
482 virtio_set_status(vdev
, vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
486 static unsigned virtio_pci_get_features(DeviceState
*d
)
488 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
489 return proxy
->host_features
;
492 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy
*proxy
,
493 unsigned int queue_no
,
497 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
500 if (irqfd
->users
== 0) {
501 ret
= kvm_irqchip_add_msi_route(kvm_state
, msg
);
511 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy
*proxy
,
514 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
515 if (--irqfd
->users
== 0) {
516 kvm_irqchip_release_virq(kvm_state
, irqfd
->virq
);
520 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy
*proxy
,
521 unsigned int queue_no
,
524 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
525 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
526 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
527 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
529 ret
= kvm_irqchip_add_irqfd_notifier(kvm_state
, n
, NULL
, irqfd
->virq
);
533 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy
*proxy
,
534 unsigned int queue_no
,
537 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
538 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
539 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
540 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
543 ret
= kvm_irqchip_remove_irqfd_notifier(kvm_state
, n
, irqfd
->virq
);
547 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy
*proxy
, int nvqs
)
549 PCIDevice
*dev
= &proxy
->pci_dev
;
550 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
551 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
556 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
557 if (!virtio_queue_get_num(vdev
, queue_no
)) {
560 vector
= virtio_queue_vector(vdev
, queue_no
);
561 if (vector
>= msix_nr_vectors_allocated(dev
)) {
564 msg
= msix_get_message(dev
, vector
);
565 ret
= kvm_virtio_pci_vq_vector_use(proxy
, queue_no
, vector
, msg
);
569 /* If guest supports masking, set up irqfd now.
570 * Otherwise, delay until unmasked in the frontend.
572 if (k
->guest_notifier_mask
) {
573 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
575 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
583 while (--queue_no
>= 0) {
584 vector
= virtio_queue_vector(vdev
, queue_no
);
585 if (vector
>= msix_nr_vectors_allocated(dev
)) {
588 if (k
->guest_notifier_mask
) {
589 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
591 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
596 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy
*proxy
, int nvqs
)
598 PCIDevice
*dev
= &proxy
->pci_dev
;
599 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
602 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
604 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
605 if (!virtio_queue_get_num(vdev
, queue_no
)) {
608 vector
= virtio_queue_vector(vdev
, queue_no
);
609 if (vector
>= msix_nr_vectors_allocated(dev
)) {
612 /* If guest supports masking, clean up irqfd now.
613 * Otherwise, it was cleaned when masked in the frontend.
615 if (k
->guest_notifier_mask
) {
616 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
618 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
622 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy
*proxy
,
623 unsigned int queue_no
,
627 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
628 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
629 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
630 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
634 if (proxy
->vector_irqfd
) {
635 irqfd
= &proxy
->vector_irqfd
[vector
];
636 if (irqfd
->msg
.data
!= msg
.data
|| irqfd
->msg
.address
!= msg
.address
) {
637 ret
= kvm_irqchip_update_msi_route(kvm_state
, irqfd
->virq
, msg
);
644 /* If guest supports masking, irqfd is already setup, unmask it.
645 * Otherwise, set it up now.
647 if (k
->guest_notifier_mask
) {
648 k
->guest_notifier_mask(vdev
, queue_no
, false);
649 /* Test after unmasking to avoid losing events. */
650 if (k
->guest_notifier_pending
&&
651 k
->guest_notifier_pending(vdev
, queue_no
)) {
652 event_notifier_set(n
);
655 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
660 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy
*proxy
,
661 unsigned int queue_no
,
664 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
665 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
667 /* If guest supports masking, keep irqfd but mask it.
668 * Otherwise, clean it up now.
670 if (k
->guest_notifier_mask
) {
671 k
->guest_notifier_mask(vdev
, queue_no
, true);
673 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
677 static int virtio_pci_vector_unmask(PCIDevice
*dev
, unsigned vector
,
680 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
681 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
684 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
685 if (!virtio_queue_get_num(vdev
, queue_no
)) {
688 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
691 ret
= virtio_pci_vq_vector_unmask(proxy
, queue_no
, vector
, msg
);
699 while (--queue_no
>= 0) {
700 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
703 virtio_pci_vq_vector_mask(proxy
, queue_no
, vector
);
708 static void virtio_pci_vector_mask(PCIDevice
*dev
, unsigned vector
)
710 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
711 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
714 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
715 if (!virtio_queue_get_num(vdev
, queue_no
)) {
718 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
721 virtio_pci_vq_vector_mask(proxy
, queue_no
, vector
);
725 static void virtio_pci_vector_poll(PCIDevice
*dev
,
726 unsigned int vector_start
,
727 unsigned int vector_end
)
729 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
730 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
731 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
734 EventNotifier
*notifier
;
737 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
738 if (!virtio_queue_get_num(vdev
, queue_no
)) {
741 vector
= virtio_queue_vector(vdev
, queue_no
);
742 if (vector
< vector_start
|| vector
>= vector_end
||
743 !msix_is_masked(dev
, vector
)) {
746 vq
= virtio_get_queue(vdev
, queue_no
);
747 notifier
= virtio_queue_get_guest_notifier(vq
);
748 if (k
->guest_notifier_pending
) {
749 if (k
->guest_notifier_pending(vdev
, queue_no
)) {
750 msix_set_pending(dev
, vector
);
752 } else if (event_notifier_test_and_clear(notifier
)) {
753 msix_set_pending(dev
, vector
);
758 static int virtio_pci_set_guest_notifier(DeviceState
*d
, int n
, bool assign
,
761 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
762 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
763 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
764 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
765 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
768 int r
= event_notifier_init(notifier
, 0);
772 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
774 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
775 event_notifier_cleanup(notifier
);
778 if (!msix_enabled(&proxy
->pci_dev
) && vdc
->guest_notifier_mask
) {
779 vdc
->guest_notifier_mask(vdev
, n
, !assign
);
785 static bool virtio_pci_query_guest_notifiers(DeviceState
*d
)
787 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
788 return msix_enabled(&proxy
->pci_dev
);
791 static int virtio_pci_set_guest_notifiers(DeviceState
*d
, int nvqs
, bool assign
)
793 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
794 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
795 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
797 bool with_irqfd
= msix_enabled(&proxy
->pci_dev
) &&
798 kvm_msi_via_irqfd_enabled();
800 nvqs
= MIN(nvqs
, VIRTIO_PCI_QUEUE_MAX
);
802 /* When deassigning, pass a consistent nvqs value
803 * to avoid leaking notifiers.
805 assert(assign
|| nvqs
== proxy
->nvqs_with_notifiers
);
807 proxy
->nvqs_with_notifiers
= nvqs
;
809 /* Must unset vector notifier while guest notifier is still assigned */
810 if ((proxy
->vector_irqfd
|| k
->guest_notifier_mask
) && !assign
) {
811 msix_unset_vector_notifiers(&proxy
->pci_dev
);
812 if (proxy
->vector_irqfd
) {
813 kvm_virtio_pci_vector_release(proxy
, nvqs
);
814 g_free(proxy
->vector_irqfd
);
815 proxy
->vector_irqfd
= NULL
;
819 for (n
= 0; n
< nvqs
; n
++) {
820 if (!virtio_queue_get_num(vdev
, n
)) {
824 r
= virtio_pci_set_guest_notifier(d
, n
, assign
, with_irqfd
);
830 /* Must set vector notifier after guest notifier has been assigned */
831 if ((with_irqfd
|| k
->guest_notifier_mask
) && assign
) {
833 proxy
->vector_irqfd
=
834 g_malloc0(sizeof(*proxy
->vector_irqfd
) *
835 msix_nr_vectors_allocated(&proxy
->pci_dev
));
836 r
= kvm_virtio_pci_vector_use(proxy
, nvqs
);
841 r
= msix_set_vector_notifiers(&proxy
->pci_dev
,
842 virtio_pci_vector_unmask
,
843 virtio_pci_vector_mask
,
844 virtio_pci_vector_poll
);
846 goto notifiers_error
;
855 kvm_virtio_pci_vector_release(proxy
, nvqs
);
859 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
862 virtio_pci_set_guest_notifier(d
, n
, !assign
, with_irqfd
);
867 static int virtio_pci_set_host_notifier(DeviceState
*d
, int n
, bool assign
)
869 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
871 /* Stop using ioeventfd for virtqueue kick if the device starts using host
872 * notifiers. This makes it easy to avoid stepping on each others' toes.
874 proxy
->ioeventfd_disabled
= assign
;
876 virtio_pci_stop_ioeventfd(proxy
);
878 /* We don't need to start here: it's not needed because backend
879 * currently only stops on status change away from ok,
880 * reset, vmstop and such. If we do add code to start here,
881 * need to check vmstate, device state etc. */
882 return virtio_pci_set_host_notifier_internal(proxy
, n
, assign
, false);
885 static void virtio_pci_vmstate_change(DeviceState
*d
, bool running
)
887 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
888 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
891 /* Try to find out if the guest has bus master disabled, but is
892 in ready state. Then we have a buggy guest OS. */
893 if ((vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
894 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
895 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
897 virtio_pci_start_ioeventfd(proxy
);
899 virtio_pci_stop_ioeventfd(proxy
);
904 static int virtio_9p_init_pci(VirtIOPCIProxy
*vpci_dev
)
906 V9fsPCIState
*dev
= VIRTIO_9P_PCI(vpci_dev
);
907 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
909 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
910 if (qdev_init(vdev
) < 0) {
916 static Property virtio_9p_pci_properties
[] = {
917 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
918 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
919 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
920 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
921 DEFINE_VIRTIO_9P_PROPERTIES(V9fsPCIState
, vdev
.fsconf
),
922 DEFINE_PROP_END_OF_LIST(),
925 static void virtio_9p_pci_class_init(ObjectClass
*klass
, void *data
)
927 DeviceClass
*dc
= DEVICE_CLASS(klass
);
928 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
929 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
931 k
->init
= virtio_9p_init_pci
;
932 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
933 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_9P
;
934 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
935 pcidev_k
->class_id
= 0x2;
936 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
937 dc
->props
= virtio_9p_pci_properties
;
940 static void virtio_9p_pci_instance_init(Object
*obj
)
942 V9fsPCIState
*dev
= VIRTIO_9P_PCI(obj
);
943 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_9P
);
944 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
947 static const TypeInfo virtio_9p_pci_info
= {
948 .name
= TYPE_VIRTIO_9P_PCI
,
949 .parent
= TYPE_VIRTIO_PCI
,
950 .instance_size
= sizeof(V9fsPCIState
),
951 .instance_init
= virtio_9p_pci_instance_init
,
952 .class_init
= virtio_9p_pci_class_init
,
954 #endif /* CONFIG_VIRTFS */
957 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
960 /* This is called by virtio-bus just after the device is plugged. */
961 static void virtio_pci_device_plugged(DeviceState
*d
)
963 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
964 VirtioBusState
*bus
= &proxy
->bus
;
968 config
= proxy
->pci_dev
.config
;
969 if (proxy
->class_code
) {
970 pci_config_set_class(config
, proxy
->class_code
);
972 pci_set_word(config
+ PCI_SUBSYSTEM_VENDOR_ID
,
973 pci_get_word(config
+ PCI_VENDOR_ID
));
974 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, virtio_bus_get_vdev_id(bus
));
975 config
[PCI_INTERRUPT_PIN
] = 1;
977 if (proxy
->nvectors
&&
978 msix_init_exclusive_bar(&proxy
->pci_dev
, proxy
->nvectors
, 1)) {
982 proxy
->pci_dev
.config_write
= virtio_write_config
;
984 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
)
985 + virtio_bus_get_vdev_config_len(bus
);
986 if (size
& (size
- 1)) {
987 size
= 1 << qemu_fls(size
);
990 memory_region_init_io(&proxy
->bar
, OBJECT(proxy
), &virtio_pci_config_ops
,
991 proxy
, "virtio-pci", size
);
992 pci_register_bar(&proxy
->pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
,
995 if (!kvm_has_many_ioeventfds()) {
996 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
999 proxy
->host_features
|= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY
;
1000 proxy
->host_features
|= 0x1 << VIRTIO_F_BAD_FEATURE
;
1001 proxy
->host_features
= virtio_bus_get_vdev_features(bus
,
1002 proxy
->host_features
);
1005 static void virtio_pci_device_unplugged(DeviceState
*d
)
1007 PCIDevice
*pci_dev
= PCI_DEVICE(d
);
1008 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1010 virtio_pci_stop_ioeventfd(proxy
);
1011 msix_uninit_exclusive_bar(pci_dev
);
1014 static int virtio_pci_init(PCIDevice
*pci_dev
)
1016 VirtIOPCIProxy
*dev
= VIRTIO_PCI(pci_dev
);
1017 VirtioPCIClass
*k
= VIRTIO_PCI_GET_CLASS(pci_dev
);
1018 virtio_pci_bus_new(&dev
->bus
, sizeof(dev
->bus
), dev
);
1019 if (k
->init
!= NULL
) {
1020 return k
->init(dev
);
1025 static void virtio_pci_exit(PCIDevice
*pci_dev
)
1027 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
1028 memory_region_destroy(&proxy
->bar
);
1031 static void virtio_pci_reset(DeviceState
*qdev
)
1033 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1034 VirtioBusState
*bus
= VIRTIO_BUS(&proxy
->bus
);
1035 virtio_pci_stop_ioeventfd(proxy
);
1036 virtio_bus_reset(bus
);
1037 msix_unuse_all_vectors(&proxy
->pci_dev
);
1038 proxy
->flags
&= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
1041 static void virtio_pci_class_init(ObjectClass
*klass
, void *data
)
1043 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1044 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1046 k
->init
= virtio_pci_init
;
1047 k
->exit
= virtio_pci_exit
;
1048 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1049 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1050 k
->class_id
= PCI_CLASS_OTHERS
;
1051 dc
->reset
= virtio_pci_reset
;
1054 static const TypeInfo virtio_pci_info
= {
1055 .name
= TYPE_VIRTIO_PCI
,
1056 .parent
= TYPE_PCI_DEVICE
,
1057 .instance_size
= sizeof(VirtIOPCIProxy
),
1058 .class_init
= virtio_pci_class_init
,
1059 .class_size
= sizeof(VirtioPCIClass
),
1063 /* virtio-blk-pci */
1065 static Property virtio_blk_pci_properties
[] = {
1066 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1067 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1068 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1069 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1070 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
1071 DEFINE_PROP_BIT("x-data-plane", VirtIOBlkPCI
, blk
.data_plane
, 0, false),
1073 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy
, host_features
),
1074 DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlkPCI
, blk
),
1075 DEFINE_PROP_END_OF_LIST(),
1078 static int virtio_blk_pci_init(VirtIOPCIProxy
*vpci_dev
)
1080 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(vpci_dev
);
1081 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1082 virtio_blk_set_conf(vdev
, &(dev
->blk
));
1083 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1084 if (qdev_init(vdev
) < 0) {
1090 static void virtio_blk_pci_class_init(ObjectClass
*klass
, void *data
)
1092 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1093 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1094 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1096 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1097 dc
->props
= virtio_blk_pci_properties
;
1098 k
->init
= virtio_blk_pci_init
;
1099 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1100 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
1101 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1102 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1105 static void virtio_blk_pci_instance_init(Object
*obj
)
1107 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(obj
);
1108 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_BLK
);
1109 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1112 static const TypeInfo virtio_blk_pci_info
= {
1113 .name
= TYPE_VIRTIO_BLK_PCI
,
1114 .parent
= TYPE_VIRTIO_PCI
,
1115 .instance_size
= sizeof(VirtIOBlkPCI
),
1116 .instance_init
= virtio_blk_pci_instance_init
,
1117 .class_init
= virtio_blk_pci_class_init
,
1120 /* virtio-scsi-pci */
1122 static Property virtio_scsi_pci_properties
[] = {
1123 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1124 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1125 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1126 DEV_NVECTORS_UNSPECIFIED
),
1127 DEFINE_VIRTIO_SCSI_FEATURES(VirtIOPCIProxy
, host_features
),
1128 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSIPCI
, vdev
.parent_obj
.conf
),
1129 DEFINE_PROP_END_OF_LIST(),
1132 static int virtio_scsi_pci_init_pci(VirtIOPCIProxy
*vpci_dev
)
1134 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(vpci_dev
);
1135 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1136 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1137 DeviceState
*proxy
= DEVICE(vpci_dev
);
1140 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1141 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1145 * For command line compatibility, this sets the virtio-scsi-device bus
1149 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1150 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1154 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1155 if (qdev_init(vdev
) < 0) {
1161 static void virtio_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1163 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1164 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1165 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1166 k
->init
= virtio_scsi_pci_init_pci
;
1167 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1168 dc
->props
= virtio_scsi_pci_properties
;
1169 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1170 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1171 pcidev_k
->revision
= 0x00;
1172 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1175 static void virtio_scsi_pci_instance_init(Object
*obj
)
1177 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(obj
);
1178 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_SCSI
);
1179 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1182 static const TypeInfo virtio_scsi_pci_info
= {
1183 .name
= TYPE_VIRTIO_SCSI_PCI
,
1184 .parent
= TYPE_VIRTIO_PCI
,
1185 .instance_size
= sizeof(VirtIOSCSIPCI
),
1186 .instance_init
= virtio_scsi_pci_instance_init
,
1187 .class_init
= virtio_scsi_pci_class_init
,
1190 /* vhost-scsi-pci */
1192 #ifdef CONFIG_VHOST_SCSI
1193 static Property vhost_scsi_pci_properties
[] = {
1194 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1195 DEV_NVECTORS_UNSPECIFIED
),
1196 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1197 DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSIPCI
, vdev
.parent_obj
.conf
),
1198 DEFINE_PROP_END_OF_LIST(),
1201 static int vhost_scsi_pci_init_pci(VirtIOPCIProxy
*vpci_dev
)
1203 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(vpci_dev
);
1204 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1205 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1207 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1208 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1211 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1212 if (qdev_init(vdev
) < 0) {
1218 static void vhost_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1220 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1221 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1222 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1223 k
->init
= vhost_scsi_pci_init_pci
;
1224 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1225 dc
->props
= vhost_scsi_pci_properties
;
1226 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1227 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1228 pcidev_k
->revision
= 0x00;
1229 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1232 static void vhost_scsi_pci_instance_init(Object
*obj
)
1234 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(obj
);
1235 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VHOST_SCSI
);
1236 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1239 static const TypeInfo vhost_scsi_pci_info
= {
1240 .name
= TYPE_VHOST_SCSI_PCI
,
1241 .parent
= TYPE_VIRTIO_PCI
,
1242 .instance_size
= sizeof(VHostSCSIPCI
),
1243 .instance_init
= vhost_scsi_pci_instance_init
,
1244 .class_init
= vhost_scsi_pci_class_init
,
1248 /* virtio-balloon-pci */
1250 static void balloon_pci_stats_get_all(Object
*obj
, struct Visitor
*v
,
1251 void *opaque
, const char *name
,
1254 VirtIOBalloonPCI
*dev
= opaque
;
1255 object_property_get(OBJECT(&dev
->vdev
), v
, "guest-stats", errp
);
1258 static void balloon_pci_stats_get_poll_interval(Object
*obj
, struct Visitor
*v
,
1259 void *opaque
, const char *name
,
1262 VirtIOBalloonPCI
*dev
= opaque
;
1263 object_property_get(OBJECT(&dev
->vdev
), v
, "guest-stats-polling-interval",
1267 static void balloon_pci_stats_set_poll_interval(Object
*obj
, struct Visitor
*v
,
1268 void *opaque
, const char *name
,
1271 VirtIOBalloonPCI
*dev
= opaque
;
1272 object_property_set(OBJECT(&dev
->vdev
), v
, "guest-stats-polling-interval",
1276 static Property virtio_balloon_pci_properties
[] = {
1277 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1278 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1279 DEFINE_PROP_END_OF_LIST(),
1282 static int virtio_balloon_pci_init(VirtIOPCIProxy
*vpci_dev
)
1284 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(vpci_dev
);
1285 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1287 if (vpci_dev
->class_code
!= PCI_CLASS_OTHERS
&&
1288 vpci_dev
->class_code
!= PCI_CLASS_MEMORY_RAM
) { /* qemu < 1.1 */
1289 vpci_dev
->class_code
= PCI_CLASS_OTHERS
;
1292 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1293 if (qdev_init(vdev
) < 0) {
1299 static void virtio_balloon_pci_class_init(ObjectClass
*klass
, void *data
)
1301 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1302 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1303 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1304 k
->init
= virtio_balloon_pci_init
;
1305 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1306 dc
->props
= virtio_balloon_pci_properties
;
1307 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1308 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BALLOON
;
1309 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1310 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
1313 static void virtio_balloon_pci_instance_init(Object
*obj
)
1315 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(obj
);
1316 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_BALLOON
);
1317 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1319 object_property_add(obj
, "guest-stats", "guest statistics",
1320 balloon_pci_stats_get_all
, NULL
, NULL
, dev
,
1323 object_property_add(obj
, "guest-stats-polling-interval", "int",
1324 balloon_pci_stats_get_poll_interval
,
1325 balloon_pci_stats_set_poll_interval
,
1329 static const TypeInfo virtio_balloon_pci_info
= {
1330 .name
= TYPE_VIRTIO_BALLOON_PCI
,
1331 .parent
= TYPE_VIRTIO_PCI
,
1332 .instance_size
= sizeof(VirtIOBalloonPCI
),
1333 .instance_init
= virtio_balloon_pci_instance_init
,
1334 .class_init
= virtio_balloon_pci_class_init
,
1337 /* virtio-serial-pci */
1339 static int virtio_serial_pci_init(VirtIOPCIProxy
*vpci_dev
)
1341 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(vpci_dev
);
1342 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1343 DeviceState
*proxy
= DEVICE(vpci_dev
);
1346 if (vpci_dev
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
1347 vpci_dev
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
1348 vpci_dev
->class_code
!= PCI_CLASS_OTHERS
) { /* qemu-kvm */
1349 vpci_dev
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
1352 /* backwards-compatibility with machines that were created with
1353 DEV_NVECTORS_UNSPECIFIED */
1354 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1355 vpci_dev
->nvectors
= dev
->vdev
.serial
.max_virtserial_ports
+ 1;
1359 * For command line compatibility, this sets the virtio-serial-device bus
1363 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1364 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1368 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1369 if (qdev_init(vdev
) < 0) {
1375 static Property virtio_serial_pci_properties
[] = {
1376 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1377 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1378 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1379 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1380 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1381 DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtIOSerialPCI
, vdev
.serial
),
1382 DEFINE_PROP_END_OF_LIST(),
1385 static void virtio_serial_pci_class_init(ObjectClass
*klass
, void *data
)
1387 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1388 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1389 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1390 k
->init
= virtio_serial_pci_init
;
1391 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
1392 dc
->props
= virtio_serial_pci_properties
;
1393 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1394 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_CONSOLE
;
1395 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1396 pcidev_k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
1399 static void virtio_serial_pci_instance_init(Object
*obj
)
1401 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(obj
);
1402 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_SERIAL
);
1403 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1406 static const TypeInfo virtio_serial_pci_info
= {
1407 .name
= TYPE_VIRTIO_SERIAL_PCI
,
1408 .parent
= TYPE_VIRTIO_PCI
,
1409 .instance_size
= sizeof(VirtIOSerialPCI
),
1410 .instance_init
= virtio_serial_pci_instance_init
,
1411 .class_init
= virtio_serial_pci_class_init
,
1414 /* virtio-net-pci */
1416 static Property virtio_net_properties
[] = {
1417 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1418 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, false),
1419 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
1420 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy
, host_features
),
1421 DEFINE_NIC_PROPERTIES(VirtIONetPCI
, vdev
.nic_conf
),
1422 DEFINE_VIRTIO_NET_PROPERTIES(VirtIONetPCI
, vdev
.net_conf
),
1423 DEFINE_PROP_END_OF_LIST(),
1426 static int virtio_net_pci_init(VirtIOPCIProxy
*vpci_dev
)
1428 DeviceState
*qdev
= DEVICE(vpci_dev
);
1429 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(vpci_dev
);
1430 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1432 virtio_net_set_config_size(&dev
->vdev
, vpci_dev
->host_features
);
1433 virtio_net_set_netclient_name(&dev
->vdev
, qdev
->id
,
1434 object_get_typename(OBJECT(qdev
)));
1435 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1436 if (qdev_init(vdev
) < 0) {
1442 static void virtio_net_pci_class_init(ObjectClass
*klass
, void *data
)
1444 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1445 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1446 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_CLASS(klass
);
1448 k
->romfile
= "efi-virtio.rom";
1449 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1450 k
->device_id
= PCI_DEVICE_ID_VIRTIO_NET
;
1451 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1452 k
->class_id
= PCI_CLASS_NETWORK_ETHERNET
;
1453 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
1454 dc
->props
= virtio_net_properties
;
1455 vpciklass
->init
= virtio_net_pci_init
;
1458 static void virtio_net_pci_instance_init(Object
*obj
)
1460 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(obj
);
1461 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_NET
);
1462 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1465 static const TypeInfo virtio_net_pci_info
= {
1466 .name
= TYPE_VIRTIO_NET_PCI
,
1467 .parent
= TYPE_VIRTIO_PCI
,
1468 .instance_size
= sizeof(VirtIONetPCI
),
1469 .instance_init
= virtio_net_pci_instance_init
,
1470 .class_init
= virtio_net_pci_class_init
,
1473 /* virtio-rng-pci */
1475 static Property virtio_rng_pci_properties
[] = {
1476 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1477 DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORngPCI
, vdev
.conf
),
1478 DEFINE_PROP_END_OF_LIST(),
1481 static int virtio_rng_pci_init(VirtIOPCIProxy
*vpci_dev
)
1483 VirtIORngPCI
*vrng
= VIRTIO_RNG_PCI(vpci_dev
);
1484 DeviceState
*vdev
= DEVICE(&vrng
->vdev
);
1486 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1487 if (qdev_init(vdev
) < 0) {
1491 object_property_set_link(OBJECT(vrng
),
1492 OBJECT(vrng
->vdev
.conf
.rng
), "rng",
1498 static void virtio_rng_pci_class_init(ObjectClass
*klass
, void *data
)
1500 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1501 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1502 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1504 k
->init
= virtio_rng_pci_init
;
1505 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1506 dc
->props
= virtio_rng_pci_properties
;
1508 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1509 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_RNG
;
1510 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1511 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
1514 static void virtio_rng_initfn(Object
*obj
)
1516 VirtIORngPCI
*dev
= VIRTIO_RNG_PCI(obj
);
1517 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_RNG
);
1518 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1519 object_property_add_link(obj
, "rng", TYPE_RNG_BACKEND
,
1520 (Object
**)&dev
->vdev
.conf
.rng
, NULL
);
1524 static const TypeInfo virtio_rng_pci_info
= {
1525 .name
= TYPE_VIRTIO_RNG_PCI
,
1526 .parent
= TYPE_VIRTIO_PCI
,
1527 .instance_size
= sizeof(VirtIORngPCI
),
1528 .instance_init
= virtio_rng_initfn
,
1529 .class_init
= virtio_rng_pci_class_init
,
1532 /* virtio-pci-bus */
1534 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
1535 VirtIOPCIProxy
*dev
)
1537 DeviceState
*qdev
= DEVICE(dev
);
1539 char virtio_bus_name
[] = "virtio-bus";
1541 qbus_create_inplace(bus
, bus_size
, TYPE_VIRTIO_PCI_BUS
, qdev
,
1544 qbus
->allow_hotplug
= 1;
1547 static void virtio_pci_bus_class_init(ObjectClass
*klass
, void *data
)
1549 BusClass
*bus_class
= BUS_CLASS(klass
);
1550 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
1551 bus_class
->max_dev
= 1;
1552 k
->notify
= virtio_pci_notify
;
1553 k
->save_config
= virtio_pci_save_config
;
1554 k
->load_config
= virtio_pci_load_config
;
1555 k
->save_queue
= virtio_pci_save_queue
;
1556 k
->load_queue
= virtio_pci_load_queue
;
1557 k
->get_features
= virtio_pci_get_features
;
1558 k
->query_guest_notifiers
= virtio_pci_query_guest_notifiers
;
1559 k
->set_host_notifier
= virtio_pci_set_host_notifier
;
1560 k
->set_guest_notifiers
= virtio_pci_set_guest_notifiers
;
1561 k
->vmstate_change
= virtio_pci_vmstate_change
;
1562 k
->device_plugged
= virtio_pci_device_plugged
;
1563 k
->device_unplugged
= virtio_pci_device_unplugged
;
1566 static const TypeInfo virtio_pci_bus_info
= {
1567 .name
= TYPE_VIRTIO_PCI_BUS
,
1568 .parent
= TYPE_VIRTIO_BUS
,
1569 .instance_size
= sizeof(VirtioPCIBusState
),
1570 .class_init
= virtio_pci_bus_class_init
,
1573 static void virtio_pci_register_types(void)
1575 type_register_static(&virtio_rng_pci_info
);
1576 type_register_static(&virtio_pci_bus_info
);
1577 type_register_static(&virtio_pci_info
);
1578 #ifdef CONFIG_VIRTFS
1579 type_register_static(&virtio_9p_pci_info
);
1581 type_register_static(&virtio_blk_pci_info
);
1582 type_register_static(&virtio_scsi_pci_info
);
1583 type_register_static(&virtio_balloon_pci_info
);
1584 type_register_static(&virtio_serial_pci_info
);
1585 type_register_static(&virtio_net_pci_info
);
1586 #ifdef CONFIG_VHOST_SCSI
1587 type_register_static(&vhost_scsi_pci_info
);
1591 type_init(virtio_pci_register_types
)