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 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
93 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
94 static inline VirtIOPCIProxy
*to_virtio_pci_proxy(DeviceState
*d
)
96 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
99 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
100 * be careful and test performance if you change this.
102 static inline VirtIOPCIProxy
*to_virtio_pci_proxy_fast(DeviceState
*d
)
104 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
107 static void virtio_pci_notify(DeviceState
*d
, uint16_t vector
)
109 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy_fast(d
);
111 if (msix_enabled(&proxy
->pci_dev
))
112 msix_notify(&proxy
->pci_dev
, vector
);
114 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
115 pci_set_irq(&proxy
->pci_dev
, vdev
->isr
& 1);
119 static void virtio_pci_save_config(DeviceState
*d
, QEMUFile
*f
)
121 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
122 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
124 pci_device_save(&proxy
->pci_dev
, f
);
125 msix_save(&proxy
->pci_dev
, f
);
126 if (msix_present(&proxy
->pci_dev
))
127 qemu_put_be16(f
, vdev
->config_vector
);
130 static void virtio_pci_save_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
132 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
133 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
135 if (msix_present(&proxy
->pci_dev
))
136 qemu_put_be16(f
, virtio_queue_vector(vdev
, n
));
139 static int virtio_pci_load_config(DeviceState
*d
, QEMUFile
*f
)
141 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
142 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
145 ret
= pci_device_load(&proxy
->pci_dev
, f
);
149 msix_unuse_all_vectors(&proxy
->pci_dev
);
150 msix_load(&proxy
->pci_dev
, f
);
151 if (msix_present(&proxy
->pci_dev
)) {
152 qemu_get_be16s(f
, &vdev
->config_vector
);
154 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
156 if (vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
157 return msix_vector_use(&proxy
->pci_dev
, vdev
->config_vector
);
162 static int virtio_pci_load_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
164 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
165 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
168 if (msix_present(&proxy
->pci_dev
)) {
169 qemu_get_be16s(f
, &vector
);
171 vector
= VIRTIO_NO_VECTOR
;
173 virtio_queue_set_vector(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 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
184 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
185 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
189 r
= event_notifier_init(notifier
, 1);
191 error_report("%s: unable to init event notifier: %d",
195 virtio_queue_set_host_notifier_fd_handler(vq
, true, set_handler
);
196 memory_region_add_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
199 memory_region_del_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
201 virtio_queue_set_host_notifier_fd_handler(vq
, false, false);
202 event_notifier_cleanup(notifier
);
207 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
209 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
212 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) ||
213 proxy
->ioeventfd_disabled
||
214 proxy
->ioeventfd_started
) {
218 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
219 if (!virtio_queue_get_num(vdev
, n
)) {
223 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, true, true);
228 proxy
->ioeventfd_started
= true;
233 if (!virtio_queue_get_num(vdev
, n
)) {
237 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
240 proxy
->ioeventfd_started
= false;
241 error_report("%s: failed. Fallback to a userspace (slower).", __func__
);
244 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
246 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
250 if (!proxy
->ioeventfd_started
) {
254 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
255 if (!virtio_queue_get_num(vdev
, n
)) {
259 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
262 proxy
->ioeventfd_started
= false;
265 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
267 VirtIOPCIProxy
*proxy
= opaque
;
268 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
272 case VIRTIO_PCI_GUEST_FEATURES
:
273 /* Guest does not negotiate properly? We have to assume nothing. */
274 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
275 val
= virtio_bus_get_vdev_bad_features(&proxy
->bus
);
277 virtio_set_features(vdev
, val
);
279 case VIRTIO_PCI_QUEUE_PFN
:
280 pa
= (hwaddr
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
282 virtio_pci_stop_ioeventfd(proxy
);
284 msix_unuse_all_vectors(&proxy
->pci_dev
);
287 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
289 case VIRTIO_PCI_QUEUE_SEL
:
290 if (val
< VIRTIO_PCI_QUEUE_MAX
)
291 vdev
->queue_sel
= val
;
293 case VIRTIO_PCI_QUEUE_NOTIFY
:
294 if (val
< VIRTIO_PCI_QUEUE_MAX
) {
295 virtio_queue_notify(vdev
, val
);
298 case VIRTIO_PCI_STATUS
:
299 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
300 virtio_pci_stop_ioeventfd(proxy
);
303 virtio_set_status(vdev
, val
& 0xFF);
305 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
306 virtio_pci_start_ioeventfd(proxy
);
309 if (vdev
->status
== 0) {
311 msix_unuse_all_vectors(&proxy
->pci_dev
);
314 /* Linux before 2.6.34 drives the device without enabling
315 the PCI device bus master bit. Enable it automatically
316 for the guest. This is a PCI spec violation but so is
317 initiating DMA with bus master bit clear. */
318 if (val
== (VIRTIO_CONFIG_S_ACKNOWLEDGE
| VIRTIO_CONFIG_S_DRIVER
)) {
319 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
320 proxy
->pci_dev
.config
[PCI_COMMAND
] |
321 PCI_COMMAND_MASTER
, 1);
324 case VIRTIO_MSI_CONFIG_VECTOR
:
325 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
326 /* Make it possible for guest to discover an error took place. */
327 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
328 val
= VIRTIO_NO_VECTOR
;
329 vdev
->config_vector
= val
;
331 case VIRTIO_MSI_QUEUE_VECTOR
:
332 msix_vector_unuse(&proxy
->pci_dev
,
333 virtio_queue_vector(vdev
, vdev
->queue_sel
));
334 /* Make it possible for guest to discover an error took place. */
335 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
336 val
= VIRTIO_NO_VECTOR
;
337 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
340 error_report("%s: unexpected address 0x%x value 0x%x",
341 __func__
, addr
, val
);
346 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
348 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
349 uint32_t ret
= 0xFFFFFFFF;
352 case VIRTIO_PCI_HOST_FEATURES
:
353 ret
= proxy
->host_features
;
355 case VIRTIO_PCI_GUEST_FEATURES
:
356 ret
= vdev
->guest_features
;
358 case VIRTIO_PCI_QUEUE_PFN
:
359 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
360 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
362 case VIRTIO_PCI_QUEUE_NUM
:
363 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
365 case VIRTIO_PCI_QUEUE_SEL
:
366 ret
= vdev
->queue_sel
;
368 case VIRTIO_PCI_STATUS
:
372 /* reading from the ISR also clears it. */
375 pci_irq_deassert(&proxy
->pci_dev
);
377 case VIRTIO_MSI_CONFIG_VECTOR
:
378 ret
= vdev
->config_vector
;
380 case VIRTIO_MSI_QUEUE_VECTOR
:
381 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
390 static uint64_t virtio_pci_config_read(void *opaque
, hwaddr addr
,
393 VirtIOPCIProxy
*proxy
= opaque
;
394 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
395 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
398 return virtio_ioport_read(proxy
, addr
);
404 val
= virtio_config_readb(vdev
, addr
);
407 val
= virtio_config_readw(vdev
, addr
);
408 if (virtio_is_big_endian(vdev
)) {
413 val
= virtio_config_readl(vdev
, addr
);
414 if (virtio_is_big_endian(vdev
)) {
422 static void virtio_pci_config_write(void *opaque
, hwaddr addr
,
423 uint64_t val
, unsigned size
)
425 VirtIOPCIProxy
*proxy
= opaque
;
426 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
427 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
429 virtio_ioport_write(proxy
, addr
, val
);
434 * Virtio-PCI is odd. Ioports are LE but config space is target native
439 virtio_config_writeb(vdev
, addr
, val
);
442 if (virtio_is_big_endian(vdev
)) {
445 virtio_config_writew(vdev
, addr
, val
);
448 if (virtio_is_big_endian(vdev
)) {
451 virtio_config_writel(vdev
, addr
, val
);
456 static const MemoryRegionOps virtio_pci_config_ops
= {
457 .read
= virtio_pci_config_read
,
458 .write
= virtio_pci_config_write
,
460 .min_access_size
= 1,
461 .max_access_size
= 4,
463 .endianness
= DEVICE_LITTLE_ENDIAN
,
466 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
467 uint32_t val
, int len
)
469 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
470 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
472 uint8_t cmd
= proxy
->pci_dev
.config
[PCI_COMMAND
];
474 pci_default_write_config(pci_dev
, address
, val
, len
);
476 if (range_covers_byte(address
, len
, PCI_COMMAND
) &&
477 !(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
) &&
478 (cmd
& PCI_COMMAND_MASTER
)) {
479 /* Bus driver disables bus mastering - make it act
480 * as a kind of reset to render the device quiescent. */
481 virtio_pci_stop_ioeventfd(proxy
);
483 msix_unuse_all_vectors(&proxy
->pci_dev
);
487 static unsigned virtio_pci_get_features(DeviceState
*d
)
489 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
490 return proxy
->host_features
;
493 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy
*proxy
,
494 unsigned int queue_no
,
498 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
501 if (irqfd
->users
== 0) {
502 ret
= kvm_irqchip_add_msi_route(kvm_state
, msg
);
512 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy
*proxy
,
515 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
516 if (--irqfd
->users
== 0) {
517 kvm_irqchip_release_virq(kvm_state
, irqfd
->virq
);
521 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy
*proxy
,
522 unsigned int queue_no
,
525 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
526 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
527 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
528 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
530 ret
= kvm_irqchip_add_irqfd_notifier(kvm_state
, n
, NULL
, irqfd
->virq
);
534 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy
*proxy
,
535 unsigned int queue_no
,
538 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
539 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
540 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
541 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
544 ret
= kvm_irqchip_remove_irqfd_notifier(kvm_state
, n
, irqfd
->virq
);
548 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy
*proxy
, int nvqs
)
550 PCIDevice
*dev
= &proxy
->pci_dev
;
551 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
552 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
557 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
558 if (!virtio_queue_get_num(vdev
, queue_no
)) {
561 vector
= virtio_queue_vector(vdev
, queue_no
);
562 if (vector
>= msix_nr_vectors_allocated(dev
)) {
565 msg
= msix_get_message(dev
, vector
);
566 ret
= kvm_virtio_pci_vq_vector_use(proxy
, queue_no
, vector
, msg
);
570 /* If guest supports masking, set up irqfd now.
571 * Otherwise, delay until unmasked in the frontend.
573 if (k
->guest_notifier_mask
) {
574 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
576 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
584 while (--queue_no
>= 0) {
585 vector
= virtio_queue_vector(vdev
, queue_no
);
586 if (vector
>= msix_nr_vectors_allocated(dev
)) {
589 if (k
->guest_notifier_mask
) {
590 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
592 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
597 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy
*proxy
, int nvqs
)
599 PCIDevice
*dev
= &proxy
->pci_dev
;
600 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
603 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
605 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
606 if (!virtio_queue_get_num(vdev
, queue_no
)) {
609 vector
= virtio_queue_vector(vdev
, queue_no
);
610 if (vector
>= msix_nr_vectors_allocated(dev
)) {
613 /* If guest supports masking, clean up irqfd now.
614 * Otherwise, it was cleaned when masked in the frontend.
616 if (k
->guest_notifier_mask
) {
617 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
619 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
623 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy
*proxy
,
624 unsigned int queue_no
,
628 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
629 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
630 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
631 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
635 if (proxy
->vector_irqfd
) {
636 irqfd
= &proxy
->vector_irqfd
[vector
];
637 if (irqfd
->msg
.data
!= msg
.data
|| irqfd
->msg
.address
!= msg
.address
) {
638 ret
= kvm_irqchip_update_msi_route(kvm_state
, irqfd
->virq
, msg
);
645 /* If guest supports masking, irqfd is already setup, unmask it.
646 * Otherwise, set it up now.
648 if (k
->guest_notifier_mask
) {
649 k
->guest_notifier_mask(vdev
, queue_no
, false);
650 /* Test after unmasking to avoid losing events. */
651 if (k
->guest_notifier_pending
&&
652 k
->guest_notifier_pending(vdev
, queue_no
)) {
653 event_notifier_set(n
);
656 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
661 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy
*proxy
,
662 unsigned int queue_no
,
665 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
666 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
668 /* If guest supports masking, keep irqfd but mask it.
669 * Otherwise, clean it up now.
671 if (k
->guest_notifier_mask
) {
672 k
->guest_notifier_mask(vdev
, queue_no
, true);
674 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
678 static int virtio_pci_vector_unmask(PCIDevice
*dev
, unsigned vector
,
681 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
682 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
685 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
686 if (!virtio_queue_get_num(vdev
, queue_no
)) {
689 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
692 ret
= virtio_pci_vq_vector_unmask(proxy
, queue_no
, vector
, msg
);
700 while (--queue_no
>= 0) {
701 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
704 virtio_pci_vq_vector_mask(proxy
, queue_no
, vector
);
709 static void virtio_pci_vector_mask(PCIDevice
*dev
, unsigned vector
)
711 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
712 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
715 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
716 if (!virtio_queue_get_num(vdev
, queue_no
)) {
719 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
722 virtio_pci_vq_vector_mask(proxy
, queue_no
, vector
);
726 static void virtio_pci_vector_poll(PCIDevice
*dev
,
727 unsigned int vector_start
,
728 unsigned int vector_end
)
730 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
731 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
732 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
735 EventNotifier
*notifier
;
738 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
739 if (!virtio_queue_get_num(vdev
, queue_no
)) {
742 vector
= virtio_queue_vector(vdev
, queue_no
);
743 if (vector
< vector_start
|| vector
>= vector_end
||
744 !msix_is_masked(dev
, vector
)) {
747 vq
= virtio_get_queue(vdev
, queue_no
);
748 notifier
= virtio_queue_get_guest_notifier(vq
);
749 if (k
->guest_notifier_pending
) {
750 if (k
->guest_notifier_pending(vdev
, queue_no
)) {
751 msix_set_pending(dev
, vector
);
753 } else if (event_notifier_test_and_clear(notifier
)) {
754 msix_set_pending(dev
, vector
);
759 static int virtio_pci_set_guest_notifier(DeviceState
*d
, int n
, bool assign
,
762 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
763 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
764 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
765 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
766 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
769 int r
= event_notifier_init(notifier
, 0);
773 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
775 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
776 event_notifier_cleanup(notifier
);
779 if (!msix_enabled(&proxy
->pci_dev
) && vdc
->guest_notifier_mask
) {
780 vdc
->guest_notifier_mask(vdev
, n
, !assign
);
786 static bool virtio_pci_query_guest_notifiers(DeviceState
*d
)
788 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
789 return msix_enabled(&proxy
->pci_dev
);
792 static int virtio_pci_set_guest_notifiers(DeviceState
*d
, int nvqs
, bool assign
)
794 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
795 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
796 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
798 bool with_irqfd
= msix_enabled(&proxy
->pci_dev
) &&
799 kvm_msi_via_irqfd_enabled();
801 nvqs
= MIN(nvqs
, VIRTIO_PCI_QUEUE_MAX
);
803 /* When deassigning, pass a consistent nvqs value
804 * to avoid leaking notifiers.
806 assert(assign
|| nvqs
== proxy
->nvqs_with_notifiers
);
808 proxy
->nvqs_with_notifiers
= nvqs
;
810 /* Must unset vector notifier while guest notifier is still assigned */
811 if ((proxy
->vector_irqfd
|| k
->guest_notifier_mask
) && !assign
) {
812 msix_unset_vector_notifiers(&proxy
->pci_dev
);
813 if (proxy
->vector_irqfd
) {
814 kvm_virtio_pci_vector_release(proxy
, nvqs
);
815 g_free(proxy
->vector_irqfd
);
816 proxy
->vector_irqfd
= NULL
;
820 for (n
= 0; n
< nvqs
; n
++) {
821 if (!virtio_queue_get_num(vdev
, n
)) {
825 r
= virtio_pci_set_guest_notifier(d
, n
, assign
, with_irqfd
);
831 /* Must set vector notifier after guest notifier has been assigned */
832 if ((with_irqfd
|| k
->guest_notifier_mask
) && assign
) {
834 proxy
->vector_irqfd
=
835 g_malloc0(sizeof(*proxy
->vector_irqfd
) *
836 msix_nr_vectors_allocated(&proxy
->pci_dev
));
837 r
= kvm_virtio_pci_vector_use(proxy
, nvqs
);
842 r
= msix_set_vector_notifiers(&proxy
->pci_dev
,
843 virtio_pci_vector_unmask
,
844 virtio_pci_vector_mask
,
845 virtio_pci_vector_poll
);
847 goto notifiers_error
;
856 kvm_virtio_pci_vector_release(proxy
, nvqs
);
860 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
863 virtio_pci_set_guest_notifier(d
, n
, !assign
, with_irqfd
);
868 static int virtio_pci_set_host_notifier(DeviceState
*d
, int n
, bool assign
)
870 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
872 /* Stop using ioeventfd for virtqueue kick if the device starts using host
873 * notifiers. This makes it easy to avoid stepping on each others' toes.
875 proxy
->ioeventfd_disabled
= assign
;
877 virtio_pci_stop_ioeventfd(proxy
);
879 /* We don't need to start here: it's not needed because backend
880 * currently only stops on status change away from ok,
881 * reset, vmstop and such. If we do add code to start here,
882 * need to check vmstate, device state etc. */
883 return virtio_pci_set_host_notifier_internal(proxy
, n
, assign
, false);
886 static void virtio_pci_vmstate_change(DeviceState
*d
, bool running
)
888 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
889 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
892 /* Linux before 2.6.34 drives the device without enabling
893 the PCI device bus master bit. Enable it automatically
894 for the guest. This is a PCI spec violation but so is
895 initiating DMA with bus master bit clear.
896 Note: this only makes a difference when migrating
897 across QEMU versions from an old QEMU, as for new QEMU
898 bus master and driver bits are always in sync.
899 TODO: consider enabling conditionally for compat machine types. */
900 if (vdev
->status
& (VIRTIO_CONFIG_S_ACKNOWLEDGE
|
901 VIRTIO_CONFIG_S_DRIVER
)) {
902 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
903 proxy
->pci_dev
.config
[PCI_COMMAND
] |
904 PCI_COMMAND_MASTER
, 1);
906 virtio_pci_start_ioeventfd(proxy
);
908 virtio_pci_stop_ioeventfd(proxy
);
913 static int virtio_9p_init_pci(VirtIOPCIProxy
*vpci_dev
)
915 V9fsPCIState
*dev
= VIRTIO_9P_PCI(vpci_dev
);
916 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
918 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
919 if (qdev_init(vdev
) < 0) {
925 static Property virtio_9p_pci_properties
[] = {
926 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
927 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
928 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
929 DEFINE_VIRTIO_9P_PROPERTIES(V9fsPCIState
, vdev
.fsconf
),
930 DEFINE_PROP_END_OF_LIST(),
933 static void virtio_9p_pci_class_init(ObjectClass
*klass
, void *data
)
935 DeviceClass
*dc
= DEVICE_CLASS(klass
);
936 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
937 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
939 k
->init
= virtio_9p_init_pci
;
940 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
941 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_9P
;
942 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
943 pcidev_k
->class_id
= 0x2;
944 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
945 dc
->props
= virtio_9p_pci_properties
;
948 static void virtio_9p_pci_instance_init(Object
*obj
)
950 V9fsPCIState
*dev
= VIRTIO_9P_PCI(obj
);
951 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_9P
);
952 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
955 static const TypeInfo virtio_9p_pci_info
= {
956 .name
= TYPE_VIRTIO_9P_PCI
,
957 .parent
= TYPE_VIRTIO_PCI
,
958 .instance_size
= sizeof(V9fsPCIState
),
959 .instance_init
= virtio_9p_pci_instance_init
,
960 .class_init
= virtio_9p_pci_class_init
,
962 #endif /* CONFIG_VIRTFS */
965 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
968 /* This is called by virtio-bus just after the device is plugged. */
969 static void virtio_pci_device_plugged(DeviceState
*d
)
971 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
972 VirtioBusState
*bus
= &proxy
->bus
;
976 config
= proxy
->pci_dev
.config
;
977 if (proxy
->class_code
) {
978 pci_config_set_class(config
, proxy
->class_code
);
980 pci_set_word(config
+ PCI_SUBSYSTEM_VENDOR_ID
,
981 pci_get_word(config
+ PCI_VENDOR_ID
));
982 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, virtio_bus_get_vdev_id(bus
));
983 config
[PCI_INTERRUPT_PIN
] = 1;
985 if (proxy
->nvectors
&&
986 msix_init_exclusive_bar(&proxy
->pci_dev
, proxy
->nvectors
, 1)) {
987 error_report("unable to init msix vectors to %" PRIu32
,
992 proxy
->pci_dev
.config_write
= virtio_write_config
;
994 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
)
995 + virtio_bus_get_vdev_config_len(bus
);
996 if (size
& (size
- 1)) {
997 size
= 1 << qemu_fls(size
);
1000 memory_region_init_io(&proxy
->bar
, OBJECT(proxy
), &virtio_pci_config_ops
,
1001 proxy
, "virtio-pci", size
);
1002 pci_register_bar(&proxy
->pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
,
1005 if (!kvm_has_many_ioeventfds()) {
1006 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
1009 proxy
->host_features
|= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY
;
1010 proxy
->host_features
|= 0x1 << VIRTIO_F_BAD_FEATURE
;
1011 proxy
->host_features
= virtio_bus_get_vdev_features(bus
,
1012 proxy
->host_features
);
1015 static void virtio_pci_device_unplugged(DeviceState
*d
)
1017 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1019 virtio_pci_stop_ioeventfd(proxy
);
1022 static int virtio_pci_init(PCIDevice
*pci_dev
)
1024 VirtIOPCIProxy
*dev
= VIRTIO_PCI(pci_dev
);
1025 VirtioPCIClass
*k
= VIRTIO_PCI_GET_CLASS(pci_dev
);
1026 virtio_pci_bus_new(&dev
->bus
, sizeof(dev
->bus
), dev
);
1027 if (k
->init
!= NULL
) {
1028 return k
->init(dev
);
1033 static void virtio_pci_exit(PCIDevice
*pci_dev
)
1035 msix_uninit_exclusive_bar(pci_dev
);
1038 static void virtio_pci_reset(DeviceState
*qdev
)
1040 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1041 VirtioBusState
*bus
= VIRTIO_BUS(&proxy
->bus
);
1042 virtio_pci_stop_ioeventfd(proxy
);
1043 virtio_bus_reset(bus
);
1044 msix_unuse_all_vectors(&proxy
->pci_dev
);
1047 static Property virtio_pci_properties
[] = {
1048 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1049 DEFINE_PROP_END_OF_LIST(),
1052 static void virtio_pci_class_init(ObjectClass
*klass
, void *data
)
1054 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1055 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1057 dc
->props
= virtio_pci_properties
;
1058 k
->init
= virtio_pci_init
;
1059 k
->exit
= virtio_pci_exit
;
1060 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1061 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1062 k
->class_id
= PCI_CLASS_OTHERS
;
1063 dc
->reset
= virtio_pci_reset
;
1066 static const TypeInfo virtio_pci_info
= {
1067 .name
= TYPE_VIRTIO_PCI
,
1068 .parent
= TYPE_PCI_DEVICE
,
1069 .instance_size
= sizeof(VirtIOPCIProxy
),
1070 .class_init
= virtio_pci_class_init
,
1071 .class_size
= sizeof(VirtioPCIClass
),
1075 /* virtio-blk-pci */
1077 static Property virtio_blk_pci_properties
[] = {
1078 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1079 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1080 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1081 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1082 DEFINE_PROP_END_OF_LIST(),
1085 static int virtio_blk_pci_init(VirtIOPCIProxy
*vpci_dev
)
1087 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(vpci_dev
);
1088 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1089 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1090 if (qdev_init(vdev
) < 0) {
1096 static void virtio_blk_pci_class_init(ObjectClass
*klass
, void *data
)
1098 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1099 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1100 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1102 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1103 dc
->props
= virtio_blk_pci_properties
;
1104 k
->init
= virtio_blk_pci_init
;
1105 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1106 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
1107 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1108 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1111 static void virtio_blk_pci_instance_init(Object
*obj
)
1113 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(obj
);
1114 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_BLK
);
1115 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1116 object_unref(OBJECT(&dev
->vdev
));
1117 qdev_alias_all_properties(DEVICE(&dev
->vdev
), obj
);
1118 object_property_add_alias(obj
, "iothread", OBJECT(&dev
->vdev
),"iothread",
1122 static const TypeInfo virtio_blk_pci_info
= {
1123 .name
= TYPE_VIRTIO_BLK_PCI
,
1124 .parent
= TYPE_VIRTIO_PCI
,
1125 .instance_size
= sizeof(VirtIOBlkPCI
),
1126 .instance_init
= virtio_blk_pci_instance_init
,
1127 .class_init
= virtio_blk_pci_class_init
,
1130 /* virtio-scsi-pci */
1132 static Property virtio_scsi_pci_properties
[] = {
1133 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1134 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1135 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1136 DEV_NVECTORS_UNSPECIFIED
),
1137 DEFINE_VIRTIO_SCSI_FEATURES(VirtIOPCIProxy
, host_features
),
1138 DEFINE_PROP_END_OF_LIST(),
1141 static int virtio_scsi_pci_init_pci(VirtIOPCIProxy
*vpci_dev
)
1143 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(vpci_dev
);
1144 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1145 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1146 DeviceState
*proxy
= DEVICE(vpci_dev
);
1149 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1150 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1154 * For command line compatibility, this sets the virtio-scsi-device bus
1158 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1159 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1163 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1164 if (qdev_init(vdev
) < 0) {
1170 static void virtio_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1172 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1173 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1174 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1175 k
->init
= virtio_scsi_pci_init_pci
;
1176 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1177 dc
->props
= virtio_scsi_pci_properties
;
1178 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1179 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1180 pcidev_k
->revision
= 0x00;
1181 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1184 static void virtio_scsi_pci_instance_init(Object
*obj
)
1186 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(obj
);
1187 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_SCSI
);
1188 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1189 object_unref(OBJECT(&dev
->vdev
));
1190 qdev_alias_all_properties(DEVICE(&dev
->vdev
), obj
);
1193 static const TypeInfo virtio_scsi_pci_info
= {
1194 .name
= TYPE_VIRTIO_SCSI_PCI
,
1195 .parent
= TYPE_VIRTIO_PCI
,
1196 .instance_size
= sizeof(VirtIOSCSIPCI
),
1197 .instance_init
= virtio_scsi_pci_instance_init
,
1198 .class_init
= virtio_scsi_pci_class_init
,
1201 /* vhost-scsi-pci */
1203 #ifdef CONFIG_VHOST_SCSI
1204 static Property vhost_scsi_pci_properties
[] = {
1205 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1206 DEV_NVECTORS_UNSPECIFIED
),
1207 DEFINE_PROP_END_OF_LIST(),
1210 static int vhost_scsi_pci_init_pci(VirtIOPCIProxy
*vpci_dev
)
1212 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(vpci_dev
);
1213 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1214 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1216 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1217 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1220 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1221 if (qdev_init(vdev
) < 0) {
1227 static void vhost_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1229 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1230 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1231 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1232 k
->init
= vhost_scsi_pci_init_pci
;
1233 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1234 dc
->props
= vhost_scsi_pci_properties
;
1235 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1236 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1237 pcidev_k
->revision
= 0x00;
1238 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1241 static void vhost_scsi_pci_instance_init(Object
*obj
)
1243 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(obj
);
1244 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VHOST_SCSI
);
1245 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1246 object_unref(OBJECT(&dev
->vdev
));
1247 qdev_alias_all_properties(DEVICE(&dev
->vdev
), obj
);
1250 static const TypeInfo vhost_scsi_pci_info
= {
1251 .name
= TYPE_VHOST_SCSI_PCI
,
1252 .parent
= TYPE_VIRTIO_PCI
,
1253 .instance_size
= sizeof(VHostSCSIPCI
),
1254 .instance_init
= vhost_scsi_pci_instance_init
,
1255 .class_init
= vhost_scsi_pci_class_init
,
1259 /* virtio-balloon-pci */
1261 static void balloon_pci_stats_get_all(Object
*obj
, struct Visitor
*v
,
1262 void *opaque
, const char *name
,
1265 VirtIOBalloonPCI
*dev
= opaque
;
1266 object_property_get(OBJECT(&dev
->vdev
), v
, "guest-stats", errp
);
1269 static void balloon_pci_stats_get_poll_interval(Object
*obj
, struct Visitor
*v
,
1270 void *opaque
, const char *name
,
1273 VirtIOBalloonPCI
*dev
= opaque
;
1274 object_property_get(OBJECT(&dev
->vdev
), v
, "guest-stats-polling-interval",
1278 static void balloon_pci_stats_set_poll_interval(Object
*obj
, struct Visitor
*v
,
1279 void *opaque
, const char *name
,
1282 VirtIOBalloonPCI
*dev
= opaque
;
1283 object_property_set(OBJECT(&dev
->vdev
), v
, "guest-stats-polling-interval",
1287 static Property virtio_balloon_pci_properties
[] = {
1288 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1289 DEFINE_PROP_END_OF_LIST(),
1292 static int virtio_balloon_pci_init(VirtIOPCIProxy
*vpci_dev
)
1294 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(vpci_dev
);
1295 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1297 if (vpci_dev
->class_code
!= PCI_CLASS_OTHERS
&&
1298 vpci_dev
->class_code
!= PCI_CLASS_MEMORY_RAM
) { /* qemu < 1.1 */
1299 vpci_dev
->class_code
= PCI_CLASS_OTHERS
;
1302 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1303 if (qdev_init(vdev
) < 0) {
1309 static void virtio_balloon_pci_class_init(ObjectClass
*klass
, void *data
)
1311 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1312 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1313 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1314 k
->init
= virtio_balloon_pci_init
;
1315 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1316 dc
->props
= virtio_balloon_pci_properties
;
1317 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1318 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BALLOON
;
1319 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1320 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
1323 static void virtio_balloon_pci_instance_init(Object
*obj
)
1325 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(obj
);
1326 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_BALLOON
);
1327 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1329 object_property_add(obj
, "guest-stats", "guest statistics",
1330 balloon_pci_stats_get_all
, NULL
, NULL
, dev
,
1333 object_property_add(obj
, "guest-stats-polling-interval", "int",
1334 balloon_pci_stats_get_poll_interval
,
1335 balloon_pci_stats_set_poll_interval
,
1339 static const TypeInfo virtio_balloon_pci_info
= {
1340 .name
= TYPE_VIRTIO_BALLOON_PCI
,
1341 .parent
= TYPE_VIRTIO_PCI
,
1342 .instance_size
= sizeof(VirtIOBalloonPCI
),
1343 .instance_init
= virtio_balloon_pci_instance_init
,
1344 .class_init
= virtio_balloon_pci_class_init
,
1347 /* virtio-serial-pci */
1349 static int virtio_serial_pci_init(VirtIOPCIProxy
*vpci_dev
)
1351 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(vpci_dev
);
1352 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1353 DeviceState
*proxy
= DEVICE(vpci_dev
);
1356 if (vpci_dev
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
1357 vpci_dev
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
1358 vpci_dev
->class_code
!= PCI_CLASS_OTHERS
) { /* qemu-kvm */
1359 vpci_dev
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
1362 /* backwards-compatibility with machines that were created with
1363 DEV_NVECTORS_UNSPECIFIED */
1364 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1365 vpci_dev
->nvectors
= dev
->vdev
.serial
.max_virtserial_ports
+ 1;
1369 * For command line compatibility, this sets the virtio-serial-device bus
1373 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1374 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1378 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1379 if (qdev_init(vdev
) < 0) {
1385 static Property virtio_serial_pci_properties
[] = {
1386 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1387 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1388 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1389 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1390 DEFINE_PROP_END_OF_LIST(),
1393 static void virtio_serial_pci_class_init(ObjectClass
*klass
, void *data
)
1395 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1396 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1397 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1398 k
->init
= virtio_serial_pci_init
;
1399 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
1400 dc
->props
= virtio_serial_pci_properties
;
1401 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1402 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_CONSOLE
;
1403 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1404 pcidev_k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
1407 static void virtio_serial_pci_instance_init(Object
*obj
)
1409 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(obj
);
1410 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_SERIAL
);
1411 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1412 qdev_alias_all_properties(DEVICE(&dev
->vdev
), obj
);
1413 object_unref(OBJECT(&dev
->vdev
));
1416 static const TypeInfo virtio_serial_pci_info
= {
1417 .name
= TYPE_VIRTIO_SERIAL_PCI
,
1418 .parent
= TYPE_VIRTIO_PCI
,
1419 .instance_size
= sizeof(VirtIOSerialPCI
),
1420 .instance_init
= virtio_serial_pci_instance_init
,
1421 .class_init
= virtio_serial_pci_class_init
,
1424 /* virtio-net-pci */
1426 static Property virtio_net_properties
[] = {
1427 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1428 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, false),
1429 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
1430 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy
, host_features
),
1431 DEFINE_PROP_END_OF_LIST(),
1434 static int virtio_net_pci_init(VirtIOPCIProxy
*vpci_dev
)
1436 DeviceState
*qdev
= DEVICE(vpci_dev
);
1437 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(vpci_dev
);
1438 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1440 virtio_net_set_config_size(&dev
->vdev
, vpci_dev
->host_features
);
1441 virtio_net_set_netclient_name(&dev
->vdev
, qdev
->id
,
1442 object_get_typename(OBJECT(qdev
)));
1443 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1444 if (qdev_init(vdev
) < 0) {
1450 static void virtio_net_pci_class_init(ObjectClass
*klass
, void *data
)
1452 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1453 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1454 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_CLASS(klass
);
1456 k
->romfile
= "efi-virtio.rom";
1457 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1458 k
->device_id
= PCI_DEVICE_ID_VIRTIO_NET
;
1459 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1460 k
->class_id
= PCI_CLASS_NETWORK_ETHERNET
;
1461 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
1462 dc
->props
= virtio_net_properties
;
1463 vpciklass
->init
= virtio_net_pci_init
;
1466 static void virtio_net_pci_instance_init(Object
*obj
)
1468 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(obj
);
1469 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_NET
);
1470 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1471 object_unref(OBJECT(&dev
->vdev
));
1472 qdev_alias_all_properties(DEVICE(&dev
->vdev
), obj
);
1475 static const TypeInfo virtio_net_pci_info
= {
1476 .name
= TYPE_VIRTIO_NET_PCI
,
1477 .parent
= TYPE_VIRTIO_PCI
,
1478 .instance_size
= sizeof(VirtIONetPCI
),
1479 .instance_init
= virtio_net_pci_instance_init
,
1480 .class_init
= virtio_net_pci_class_init
,
1483 /* virtio-rng-pci */
1485 static Property virtio_rng_pci_properties
[] = {
1486 DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORngPCI
, vdev
.conf
),
1487 DEFINE_PROP_END_OF_LIST(),
1490 static int virtio_rng_pci_init(VirtIOPCIProxy
*vpci_dev
)
1492 VirtIORngPCI
*vrng
= VIRTIO_RNG_PCI(vpci_dev
);
1493 DeviceState
*vdev
= DEVICE(&vrng
->vdev
);
1495 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1496 if (qdev_init(vdev
) < 0) {
1500 object_property_set_link(OBJECT(vrng
),
1501 OBJECT(vrng
->vdev
.conf
.rng
), "rng",
1507 static void virtio_rng_pci_class_init(ObjectClass
*klass
, void *data
)
1509 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1510 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1511 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1513 k
->init
= virtio_rng_pci_init
;
1514 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1515 dc
->props
= virtio_rng_pci_properties
;
1517 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1518 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_RNG
;
1519 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1520 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
1523 static void virtio_rng_initfn(Object
*obj
)
1525 VirtIORngPCI
*dev
= VIRTIO_RNG_PCI(obj
);
1526 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_RNG
);
1527 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1528 object_property_add_link(obj
, "rng", TYPE_RNG_BACKEND
,
1529 (Object
**)&dev
->vdev
.conf
.rng
,
1530 qdev_prop_allow_set_link_before_realize
,
1531 OBJ_PROP_LINK_UNREF_ON_RELEASE
, NULL
);
1535 static const TypeInfo virtio_rng_pci_info
= {
1536 .name
= TYPE_VIRTIO_RNG_PCI
,
1537 .parent
= TYPE_VIRTIO_PCI
,
1538 .instance_size
= sizeof(VirtIORngPCI
),
1539 .instance_init
= virtio_rng_initfn
,
1540 .class_init
= virtio_rng_pci_class_init
,
1543 /* virtio-pci-bus */
1545 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
1546 VirtIOPCIProxy
*dev
)
1548 DeviceState
*qdev
= DEVICE(dev
);
1550 char virtio_bus_name
[] = "virtio-bus";
1552 qbus_create_inplace(bus
, bus_size
, TYPE_VIRTIO_PCI_BUS
, qdev
,
1555 qbus
->allow_hotplug
= 1;
1558 static void virtio_pci_bus_class_init(ObjectClass
*klass
, void *data
)
1560 BusClass
*bus_class
= BUS_CLASS(klass
);
1561 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
1562 bus_class
->max_dev
= 1;
1563 k
->notify
= virtio_pci_notify
;
1564 k
->save_config
= virtio_pci_save_config
;
1565 k
->load_config
= virtio_pci_load_config
;
1566 k
->save_queue
= virtio_pci_save_queue
;
1567 k
->load_queue
= virtio_pci_load_queue
;
1568 k
->get_features
= virtio_pci_get_features
;
1569 k
->query_guest_notifiers
= virtio_pci_query_guest_notifiers
;
1570 k
->set_host_notifier
= virtio_pci_set_host_notifier
;
1571 k
->set_guest_notifiers
= virtio_pci_set_guest_notifiers
;
1572 k
->vmstate_change
= virtio_pci_vmstate_change
;
1573 k
->device_plugged
= virtio_pci_device_plugged
;
1574 k
->device_unplugged
= virtio_pci_device_unplugged
;
1577 static const TypeInfo virtio_pci_bus_info
= {
1578 .name
= TYPE_VIRTIO_PCI_BUS
,
1579 .parent
= TYPE_VIRTIO_BUS
,
1580 .instance_size
= sizeof(VirtioPCIBusState
),
1581 .class_init
= virtio_pci_bus_class_init
,
1584 static void virtio_pci_register_types(void)
1586 type_register_static(&virtio_rng_pci_info
);
1587 type_register_static(&virtio_pci_bus_info
);
1588 type_register_static(&virtio_pci_info
);
1589 #ifdef CONFIG_VIRTFS
1590 type_register_static(&virtio_9p_pci_info
);
1592 type_register_static(&virtio_blk_pci_info
);
1593 type_register_static(&virtio_scsi_pci_info
);
1594 type_register_static(&virtio_balloon_pci_info
);
1595 type_register_static(&virtio_serial_pci_info
);
1596 type_register_static(&virtio_net_pci_info
);
1597 #ifdef CONFIG_VHOST_SCSI
1598 type_register_static(&vhost_scsi_pci_info
);
1602 type_init(virtio_pci_register_types
)