4 * Copyright IBM, Corp. 2007
5 * Copyright (c) 2009 CodeSourcery
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paul Brook <paul@codesourcery.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
14 * Contributions after 2012-01-13 are licensed under the terms of the
15 * GNU GPL, version 2 or (at your option) any later version.
20 #include "hw/virtio/virtio.h"
21 #include "hw/virtio/virtio-blk.h"
22 #include "hw/virtio/virtio-net.h"
23 #include "hw/virtio/virtio-serial.h"
24 #include "hw/virtio/virtio-scsi.h"
25 #include "hw/virtio/virtio-balloon.h"
26 #include "hw/pci/pci.h"
27 #include "qemu/error-report.h"
28 #include "hw/pci/msi.h"
29 #include "hw/pci/msix.h"
30 #include "hw/loader.h"
31 #include "sysemu/kvm.h"
32 #include "sysemu/blockdev.h"
33 #include "virtio-pci.h"
34 #include "qemu/range.h"
35 #include "hw/virtio/virtio-bus.h"
36 #include "qapi/visitor.h"
38 /* from Linux's linux/virtio_pci.h */
40 /* A 32-bit r/o bitmask of the features supported by the host */
41 #define VIRTIO_PCI_HOST_FEATURES 0
43 /* A 32-bit r/w bitmask of features activated by the guest */
44 #define VIRTIO_PCI_GUEST_FEATURES 4
46 /* A 32-bit r/w PFN for the currently selected queue */
47 #define VIRTIO_PCI_QUEUE_PFN 8
49 /* A 16-bit r/o queue size for the currently selected queue */
50 #define VIRTIO_PCI_QUEUE_NUM 12
52 /* A 16-bit r/w queue selector */
53 #define VIRTIO_PCI_QUEUE_SEL 14
55 /* A 16-bit r/w queue notifier */
56 #define VIRTIO_PCI_QUEUE_NOTIFY 16
58 /* An 8-bit device status register. */
59 #define VIRTIO_PCI_STATUS 18
61 /* An 8-bit r/o interrupt status register. Reading the value will return the
62 * current contents of the ISR and will also clear it. This is effectively
63 * a read-and-acknowledge. */
64 #define VIRTIO_PCI_ISR 19
66 /* MSI-X registers: only enabled if MSI-X is enabled. */
67 /* A 16-bit vector for configuration changes. */
68 #define VIRTIO_MSI_CONFIG_VECTOR 20
69 /* A 16-bit vector for selected queue notifications. */
70 #define VIRTIO_MSI_QUEUE_VECTOR 22
72 /* Config space size */
73 #define VIRTIO_PCI_CONFIG_NOMSI 20
74 #define VIRTIO_PCI_CONFIG_MSI 24
75 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
76 VIRTIO_PCI_CONFIG_MSI : \
77 VIRTIO_PCI_CONFIG_NOMSI)
79 /* The remaining space is defined by each driver as the per-driver
80 * configuration space */
81 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
82 VIRTIO_PCI_CONFIG_MSI : \
83 VIRTIO_PCI_CONFIG_NOMSI)
85 /* How many bits to shift physical queue address written to QUEUE_PFN.
86 * 12 is historical, and due to x86 page size. */
87 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
89 /* Flags track per-device state like workarounds for quirks in older guests. */
90 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
92 /* QEMU doesn't strictly need write barriers since everything runs in
93 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
94 * KVM or if kqemu gets SMP support.
96 #define wmb() do { } while (0)
98 /* HACK for virtio to determine if it's running a big endian guest */
99 bool virtio_is_big_endian(void);
102 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
103 static inline VirtIOPCIProxy
*to_virtio_pci_proxy(DeviceState
*d
)
105 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
108 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
109 * be careful and test performance if you change this.
111 static inline VirtIOPCIProxy
*to_virtio_pci_proxy_fast(DeviceState
*d
)
113 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
116 static void virtio_pci_notify(DeviceState
*d
, uint16_t vector
)
118 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy_fast(d
);
119 if (msix_enabled(&proxy
->pci_dev
))
120 msix_notify(&proxy
->pci_dev
, vector
);
122 qemu_set_irq(proxy
->pci_dev
.irq
[0], proxy
->vdev
->isr
& 1);
125 static void virtio_pci_save_config(DeviceState
*d
, QEMUFile
*f
)
127 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
128 pci_device_save(&proxy
->pci_dev
, f
);
129 msix_save(&proxy
->pci_dev
, f
);
130 if (msix_present(&proxy
->pci_dev
))
131 qemu_put_be16(f
, proxy
->vdev
->config_vector
);
134 static void virtio_pci_save_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
136 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
137 if (msix_present(&proxy
->pci_dev
))
138 qemu_put_be16(f
, virtio_queue_vector(proxy
->vdev
, n
));
141 static int virtio_pci_load_config(DeviceState
*d
, QEMUFile
*f
)
143 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
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
, &proxy
->vdev
->config_vector
);
154 proxy
->vdev
->config_vector
= VIRTIO_NO_VECTOR
;
156 if (proxy
->vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
157 return msix_vector_use(&proxy
->pci_dev
, proxy
->vdev
->config_vector
);
162 static int virtio_pci_load_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
164 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
166 if (msix_present(&proxy
->pci_dev
)) {
167 qemu_get_be16s(f
, &vector
);
169 vector
= VIRTIO_NO_VECTOR
;
171 virtio_queue_set_vector(proxy
->vdev
, n
, vector
);
172 if (vector
!= VIRTIO_NO_VECTOR
) {
173 return msix_vector_use(&proxy
->pci_dev
, vector
);
178 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy
*proxy
,
179 int n
, bool assign
, bool set_handler
)
181 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
182 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
186 r
= event_notifier_init(notifier
, 1);
188 error_report("%s: unable to init event notifier: %d",
192 virtio_queue_set_host_notifier_fd_handler(vq
, true, set_handler
);
193 memory_region_add_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
196 memory_region_del_eventfd(&proxy
->bar
, VIRTIO_PCI_QUEUE_NOTIFY
, 2,
198 virtio_queue_set_host_notifier_fd_handler(vq
, false, false);
199 event_notifier_cleanup(notifier
);
204 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
208 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) ||
209 proxy
->ioeventfd_disabled
||
210 proxy
->ioeventfd_started
) {
214 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
215 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
219 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, true, true);
224 proxy
->ioeventfd_started
= true;
229 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
233 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
236 proxy
->ioeventfd_started
= false;
237 error_report("%s: failed. Fallback to a userspace (slower).", __func__
);
240 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
245 if (!proxy
->ioeventfd_started
) {
249 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
250 if (!virtio_queue_get_num(proxy
->vdev
, n
)) {
254 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
257 proxy
->ioeventfd_started
= false;
260 static void virtio_pci_reset(DeviceState
*d
)
262 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
263 virtio_pci_stop_ioeventfd(proxy
);
264 virtio_reset(proxy
->vdev
);
265 msix_unuse_all_vectors(&proxy
->pci_dev
);
266 proxy
->flags
&= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
269 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
271 VirtIOPCIProxy
*proxy
= opaque
;
272 VirtIODevice
*vdev
= proxy
->vdev
;
276 case VIRTIO_PCI_GUEST_FEATURES
:
277 /* Guest does not negotiate properly? We have to assume nothing. */
278 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
279 val
= vdev
->bad_features
? vdev
->bad_features(vdev
) : 0;
281 virtio_set_features(vdev
, val
);
283 case VIRTIO_PCI_QUEUE_PFN
:
284 pa
= (hwaddr
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
286 virtio_pci_stop_ioeventfd(proxy
);
287 virtio_reset(proxy
->vdev
);
288 msix_unuse_all_vectors(&proxy
->pci_dev
);
291 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
293 case VIRTIO_PCI_QUEUE_SEL
:
294 if (val
< VIRTIO_PCI_QUEUE_MAX
)
295 vdev
->queue_sel
= val
;
297 case VIRTIO_PCI_QUEUE_NOTIFY
:
298 if (val
< VIRTIO_PCI_QUEUE_MAX
) {
299 virtio_queue_notify(vdev
, val
);
302 case VIRTIO_PCI_STATUS
:
303 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
304 virtio_pci_stop_ioeventfd(proxy
);
307 virtio_set_status(vdev
, val
& 0xFF);
309 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
310 virtio_pci_start_ioeventfd(proxy
);
313 if (vdev
->status
== 0) {
314 virtio_reset(proxy
->vdev
);
315 msix_unuse_all_vectors(&proxy
->pci_dev
);
318 /* Linux before 2.6.34 sets the device as OK without enabling
319 the PCI device bus master bit. In this case we need to disable
320 some safety checks. */
321 if ((val
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
322 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
323 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
326 case VIRTIO_MSI_CONFIG_VECTOR
:
327 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
328 /* Make it possible for guest to discover an error took place. */
329 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
330 val
= VIRTIO_NO_VECTOR
;
331 vdev
->config_vector
= val
;
333 case VIRTIO_MSI_QUEUE_VECTOR
:
334 msix_vector_unuse(&proxy
->pci_dev
,
335 virtio_queue_vector(vdev
, vdev
->queue_sel
));
336 /* Make it possible for guest to discover an error took place. */
337 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
338 val
= VIRTIO_NO_VECTOR
;
339 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
342 error_report("%s: unexpected address 0x%x value 0x%x",
343 __func__
, addr
, val
);
348 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
350 VirtIODevice
*vdev
= proxy
->vdev
;
351 uint32_t ret
= 0xFFFFFFFF;
354 case VIRTIO_PCI_HOST_FEATURES
:
355 ret
= proxy
->host_features
;
357 case VIRTIO_PCI_GUEST_FEATURES
:
358 ret
= vdev
->guest_features
;
360 case VIRTIO_PCI_QUEUE_PFN
:
361 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
362 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
364 case VIRTIO_PCI_QUEUE_NUM
:
365 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
367 case VIRTIO_PCI_QUEUE_SEL
:
368 ret
= vdev
->queue_sel
;
370 case VIRTIO_PCI_STATUS
:
374 /* reading from the ISR also clears it. */
377 qemu_set_irq(proxy
->pci_dev
.irq
[0], 0);
379 case VIRTIO_MSI_CONFIG_VECTOR
:
380 ret
= vdev
->config_vector
;
382 case VIRTIO_MSI_QUEUE_VECTOR
:
383 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
392 static uint64_t virtio_pci_config_read(void *opaque
, hwaddr addr
,
395 VirtIOPCIProxy
*proxy
= opaque
;
396 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
399 return virtio_ioport_read(proxy
, addr
);
405 val
= virtio_config_readb(proxy
->vdev
, addr
);
408 val
= virtio_config_readw(proxy
->vdev
, addr
);
409 if (virtio_is_big_endian()) {
414 val
= virtio_config_readl(proxy
->vdev
, addr
);
415 if (virtio_is_big_endian()) {
423 static void virtio_pci_config_write(void *opaque
, hwaddr addr
,
424 uint64_t val
, unsigned size
)
426 VirtIOPCIProxy
*proxy
= opaque
;
427 uint32_t config
= VIRTIO_PCI_CONFIG(&proxy
->pci_dev
);
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(proxy
->vdev
, addr
, val
);
442 if (virtio_is_big_endian()) {
445 virtio_config_writew(proxy
->vdev
, addr
, val
);
448 if (virtio_is_big_endian()) {
451 virtio_config_writel(proxy
->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
);
471 pci_default_write_config(pci_dev
, address
, val
, len
);
473 if (range_covers_byte(address
, len
, PCI_COMMAND
) &&
474 !(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
) &&
475 !(proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG
)) {
476 virtio_pci_stop_ioeventfd(proxy
);
477 virtio_set_status(proxy
->vdev
,
478 proxy
->vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
482 static unsigned virtio_pci_get_features(DeviceState
*d
)
484 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
485 return proxy
->host_features
;
488 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy
*proxy
,
489 unsigned int queue_no
,
493 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
496 if (irqfd
->users
== 0) {
497 ret
= kvm_irqchip_add_msi_route(kvm_state
, msg
);
507 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy
*proxy
,
510 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
511 if (--irqfd
->users
== 0) {
512 kvm_irqchip_release_virq(kvm_state
, irqfd
->virq
);
516 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy
*proxy
,
517 unsigned int queue_no
,
520 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
521 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, queue_no
);
522 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
524 ret
= kvm_irqchip_add_irqfd_notifier(kvm_state
, n
, irqfd
->virq
);
528 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy
*proxy
,
529 unsigned int queue_no
,
532 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, queue_no
);
533 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
534 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
537 ret
= kvm_irqchip_remove_irqfd_notifier(kvm_state
, n
, irqfd
->virq
);
541 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy
*proxy
, int nvqs
)
543 PCIDevice
*dev
= &proxy
->pci_dev
;
544 VirtIODevice
*vdev
= proxy
->vdev
;
549 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
550 if (!virtio_queue_get_num(vdev
, queue_no
)) {
553 vector
= virtio_queue_vector(vdev
, queue_no
);
554 if (vector
>= msix_nr_vectors_allocated(dev
)) {
557 msg
= msix_get_message(dev
, vector
);
558 ret
= kvm_virtio_pci_vq_vector_use(proxy
, queue_no
, vector
, msg
);
562 /* If guest supports masking, set up irqfd now.
563 * Otherwise, delay until unmasked in the frontend.
565 if (proxy
->vdev
->guest_notifier_mask
) {
566 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
568 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
576 while (--queue_no
>= 0) {
577 vector
= virtio_queue_vector(vdev
, queue_no
);
578 if (vector
>= msix_nr_vectors_allocated(dev
)) {
581 if (proxy
->vdev
->guest_notifier_mask
) {
582 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
584 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
589 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy
*proxy
, int nvqs
)
591 PCIDevice
*dev
= &proxy
->pci_dev
;
592 VirtIODevice
*vdev
= proxy
->vdev
;
596 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
597 if (!virtio_queue_get_num(vdev
, queue_no
)) {
600 vector
= virtio_queue_vector(vdev
, queue_no
);
601 if (vector
>= msix_nr_vectors_allocated(dev
)) {
604 /* If guest supports masking, clean up irqfd now.
605 * Otherwise, it was cleaned when masked in the frontend.
607 if (proxy
->vdev
->guest_notifier_mask
) {
608 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
610 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
614 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy
*proxy
,
615 unsigned int queue_no
,
619 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, queue_no
);
620 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
624 if (proxy
->vector_irqfd
) {
625 irqfd
= &proxy
->vector_irqfd
[vector
];
626 if (irqfd
->msg
.data
!= msg
.data
|| irqfd
->msg
.address
!= msg
.address
) {
627 ret
= kvm_irqchip_update_msi_route(kvm_state
, irqfd
->virq
, msg
);
634 /* If guest supports masking, irqfd is already setup, unmask it.
635 * Otherwise, set it up now.
637 if (proxy
->vdev
->guest_notifier_mask
) {
638 proxy
->vdev
->guest_notifier_mask(proxy
->vdev
, queue_no
, false);
639 /* Test after unmasking to avoid losing events. */
640 if (proxy
->vdev
->guest_notifier_pending
&&
641 proxy
->vdev
->guest_notifier_pending(proxy
->vdev
, queue_no
)) {
642 event_notifier_set(n
);
645 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
650 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy
*proxy
,
651 unsigned int queue_no
,
654 /* If guest supports masking, keep irqfd but mask it.
655 * Otherwise, clean it up now.
657 if (proxy
->vdev
->guest_notifier_mask
) {
658 proxy
->vdev
->guest_notifier_mask(proxy
->vdev
, queue_no
, true);
660 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
664 static int virtio_pci_vector_unmask(PCIDevice
*dev
, unsigned vector
,
667 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
668 VirtIODevice
*vdev
= proxy
->vdev
;
671 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
672 if (!virtio_queue_get_num(vdev
, queue_no
)) {
675 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
678 ret
= virtio_pci_vq_vector_unmask(proxy
, queue_no
, vector
, msg
);
686 while (--queue_no
>= 0) {
687 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
690 virtio_pci_vq_vector_mask(proxy
, queue_no
, vector
);
695 static void virtio_pci_vector_mask(PCIDevice
*dev
, unsigned vector
)
697 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
698 VirtIODevice
*vdev
= proxy
->vdev
;
701 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
702 if (!virtio_queue_get_num(vdev
, queue_no
)) {
705 if (virtio_queue_vector(vdev
, queue_no
) != vector
) {
708 virtio_pci_vq_vector_mask(proxy
, queue_no
, vector
);
712 static void virtio_pci_vector_poll(PCIDevice
*dev
,
713 unsigned int vector_start
,
714 unsigned int vector_end
)
716 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
717 VirtIODevice
*vdev
= proxy
->vdev
;
720 EventNotifier
*notifier
;
723 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
724 if (!virtio_queue_get_num(vdev
, queue_no
)) {
727 vector
= virtio_queue_vector(vdev
, queue_no
);
728 if (vector
< vector_start
|| vector
>= vector_end
||
729 !msix_is_masked(dev
, vector
)) {
732 vq
= virtio_get_queue(vdev
, queue_no
);
733 notifier
= virtio_queue_get_guest_notifier(vq
);
734 if (vdev
->guest_notifier_pending
) {
735 if (vdev
->guest_notifier_pending(vdev
, queue_no
)) {
736 msix_set_pending(dev
, vector
);
738 } else if (event_notifier_test_and_clear(notifier
)) {
739 msix_set_pending(dev
, vector
);
744 static int virtio_pci_set_guest_notifier(DeviceState
*d
, int n
, bool assign
,
747 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
748 VirtQueue
*vq
= virtio_get_queue(proxy
->vdev
, n
);
749 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
752 int r
= event_notifier_init(notifier
, 0);
756 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
758 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
759 event_notifier_cleanup(notifier
);
765 static bool virtio_pci_query_guest_notifiers(DeviceState
*d
)
767 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
768 return msix_enabled(&proxy
->pci_dev
);
771 static int virtio_pci_set_guest_notifiers(DeviceState
*d
, int nvqs
, bool assign
)
773 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
774 VirtIODevice
*vdev
= proxy
->vdev
;
776 bool with_irqfd
= msix_enabled(&proxy
->pci_dev
) &&
777 kvm_msi_via_irqfd_enabled();
779 nvqs
= MIN(nvqs
, VIRTIO_PCI_QUEUE_MAX
);
781 /* When deassigning, pass a consistent nvqs value
782 * to avoid leaking notifiers.
784 assert(assign
|| nvqs
== proxy
->nvqs_with_notifiers
);
786 proxy
->nvqs_with_notifiers
= nvqs
;
788 /* Must unset vector notifier while guest notifier is still assigned */
789 if ((proxy
->vector_irqfd
|| vdev
->guest_notifier_mask
) && !assign
) {
790 msix_unset_vector_notifiers(&proxy
->pci_dev
);
791 if (proxy
->vector_irqfd
) {
792 kvm_virtio_pci_vector_release(proxy
, nvqs
);
793 g_free(proxy
->vector_irqfd
);
794 proxy
->vector_irqfd
= NULL
;
798 for (n
= 0; n
< nvqs
; n
++) {
799 if (!virtio_queue_get_num(vdev
, n
)) {
803 r
= virtio_pci_set_guest_notifier(d
, n
, assign
,
804 kvm_msi_via_irqfd_enabled());
810 /* Must set vector notifier after guest notifier has been assigned */
811 if ((with_irqfd
|| vdev
->guest_notifier_mask
) && assign
) {
813 proxy
->vector_irqfd
=
814 g_malloc0(sizeof(*proxy
->vector_irqfd
) *
815 msix_nr_vectors_allocated(&proxy
->pci_dev
));
816 r
= kvm_virtio_pci_vector_use(proxy
, nvqs
);
821 r
= msix_set_vector_notifiers(&proxy
->pci_dev
,
822 virtio_pci_vector_unmask
,
823 virtio_pci_vector_mask
,
824 virtio_pci_vector_poll
);
826 goto notifiers_error
;
835 kvm_virtio_pci_vector_release(proxy
, nvqs
);
839 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
842 virtio_pci_set_guest_notifier(d
, n
, !assign
, with_irqfd
);
847 static int virtio_pci_set_host_notifier(DeviceState
*d
, int n
, bool assign
)
849 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
851 /* Stop using ioeventfd for virtqueue kick if the device starts using host
852 * notifiers. This makes it easy to avoid stepping on each others' toes.
854 proxy
->ioeventfd_disabled
= assign
;
856 virtio_pci_stop_ioeventfd(proxy
);
858 /* We don't need to start here: it's not needed because backend
859 * currently only stops on status change away from ok,
860 * reset, vmstop and such. If we do add code to start here,
861 * need to check vmstate, device state etc. */
862 return virtio_pci_set_host_notifier_internal(proxy
, n
, assign
, false);
865 static void virtio_pci_vmstate_change(DeviceState
*d
, bool running
)
867 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
870 /* Try to find out if the guest has bus master disabled, but is
871 in ready state. Then we have a buggy guest OS. */
872 if ((proxy
->vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
873 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
874 proxy
->flags
|= VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
876 virtio_pci_start_ioeventfd(proxy
);
878 virtio_pci_stop_ioeventfd(proxy
);
882 static const VirtIOBindings virtio_pci_bindings
= {
883 .notify
= virtio_pci_notify
,
884 .save_config
= virtio_pci_save_config
,
885 .load_config
= virtio_pci_load_config
,
886 .save_queue
= virtio_pci_save_queue
,
887 .load_queue
= virtio_pci_load_queue
,
888 .get_features
= virtio_pci_get_features
,
889 .query_guest_notifiers
= virtio_pci_query_guest_notifiers
,
890 .set_host_notifier
= virtio_pci_set_host_notifier
,
891 .set_guest_notifiers
= virtio_pci_set_guest_notifiers
,
892 .vmstate_change
= virtio_pci_vmstate_change
,
895 void virtio_init_pci(VirtIOPCIProxy
*proxy
, VirtIODevice
*vdev
)
902 config
= proxy
->pci_dev
.config
;
904 if (proxy
->class_code
) {
905 pci_config_set_class(config
, proxy
->class_code
);
907 pci_set_word(config
+ PCI_SUBSYSTEM_VENDOR_ID
,
908 pci_get_word(config
+ PCI_VENDOR_ID
));
909 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, vdev
->device_id
);
910 config
[PCI_INTERRUPT_PIN
] = 1;
912 if (vdev
->nvectors
&&
913 msix_init_exclusive_bar(&proxy
->pci_dev
, vdev
->nvectors
, 1)) {
917 proxy
->pci_dev
.config_write
= virtio_write_config
;
919 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
) + vdev
->config_len
;
921 size
= 1 << qemu_fls(size
);
923 memory_region_init_io(&proxy
->bar
, &virtio_pci_config_ops
, proxy
,
925 pci_register_bar(&proxy
->pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
,
928 if (!kvm_has_many_ioeventfds()) {
929 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
932 virtio_bind_device(vdev
, &virtio_pci_bindings
, DEVICE(proxy
));
933 proxy
->host_features
|= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY
;
934 proxy
->host_features
|= 0x1 << VIRTIO_F_BAD_FEATURE
;
935 proxy
->host_features
= vdev
->get_features(vdev
, proxy
->host_features
);
938 static void virtio_exit_pci(PCIDevice
*pci_dev
)
940 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
942 memory_region_destroy(&proxy
->bar
);
943 msix_uninit_exclusive_bar(pci_dev
);
946 static int virtio_rng_init_pci(PCIDevice
*pci_dev
)
948 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
951 if (proxy
->rng
.rng
== NULL
) {
952 proxy
->rng
.default_backend
= RNG_RANDOM(object_new(TYPE_RNG_RANDOM
));
954 object_property_add_child(OBJECT(pci_dev
),
956 OBJECT(proxy
->rng
.default_backend
),
959 object_property_set_link(OBJECT(pci_dev
),
960 OBJECT(proxy
->rng
.default_backend
),
964 vdev
= virtio_rng_init(&pci_dev
->qdev
, &proxy
->rng
);
968 virtio_init_pci(proxy
, vdev
);
972 static void virtio_rng_exit_pci(PCIDevice
*pci_dev
)
974 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
976 virtio_pci_stop_ioeventfd(proxy
);
977 virtio_rng_exit(proxy
->vdev
);
978 virtio_exit_pci(pci_dev
);
981 static void virtio_rng_initfn(Object
*obj
)
983 PCIDevice
*pci_dev
= PCI_DEVICE(obj
);
984 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
986 object_property_add_link(obj
, "rng", TYPE_RNG_BACKEND
,
987 (Object
**)&proxy
->rng
.rng
, NULL
);
990 static Property virtio_rng_properties
[] = {
991 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
992 /* Set a default rate limit of 2^47 bytes per minute or roughly 2TB/s. If
993 you have an entropy source capable of generating more entropy than this
994 and you can pass it through via virtio-rng, then hats off to you. Until
995 then, this is unlimited for all practical purposes.
997 DEFINE_PROP_UINT64("max-bytes", VirtIOPCIProxy
, rng
.max_bytes
, INT64_MAX
),
998 DEFINE_PROP_UINT32("period", VirtIOPCIProxy
, rng
.period_ms
, 1 << 16),
999 DEFINE_PROP_END_OF_LIST(),
1002 static void virtio_rng_class_init(ObjectClass
*klass
, void *data
)
1004 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1005 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1007 k
->init
= virtio_rng_init_pci
;
1008 k
->exit
= virtio_rng_exit_pci
;
1009 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1010 k
->device_id
= PCI_DEVICE_ID_VIRTIO_RNG
;
1011 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1012 k
->class_id
= PCI_CLASS_OTHERS
;
1013 dc
->reset
= virtio_pci_reset
;
1014 dc
->props
= virtio_rng_properties
;
1017 static const TypeInfo virtio_rng_info
= {
1018 .name
= "virtio-rng-pci",
1019 .parent
= TYPE_PCI_DEVICE
,
1020 .instance_size
= sizeof(VirtIOPCIProxy
),
1021 .instance_init
= virtio_rng_initfn
,
1022 .class_init
= virtio_rng_class_init
,
1025 #ifdef CONFIG_VIRTFS
1026 static int virtio_9p_init_pci(PCIDevice
*pci_dev
)
1028 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
1031 vdev
= virtio_9p_init(&pci_dev
->qdev
, &proxy
->fsconf
);
1032 vdev
->nvectors
= proxy
->nvectors
;
1033 virtio_init_pci(proxy
, vdev
);
1034 /* make the actual value visible */
1035 proxy
->nvectors
= vdev
->nvectors
;
1039 static Property virtio_9p_properties
[] = {
1040 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1041 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1042 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1043 DEFINE_PROP_STRING("mount_tag", VirtIOPCIProxy
, fsconf
.tag
),
1044 DEFINE_PROP_STRING("fsdev", VirtIOPCIProxy
, fsconf
.fsdev_id
),
1045 DEFINE_PROP_END_OF_LIST(),
1048 static void virtio_9p_class_init(ObjectClass
*klass
, void *data
)
1050 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1051 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1053 k
->init
= virtio_9p_init_pci
;
1054 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1055 k
->device_id
= PCI_DEVICE_ID_VIRTIO_9P
;
1056 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1058 dc
->props
= virtio_9p_properties
;
1059 dc
->reset
= virtio_pci_reset
;
1062 static const TypeInfo virtio_9p_info
= {
1063 .name
= "virtio-9p-pci",
1064 .parent
= TYPE_PCI_DEVICE
,
1065 .instance_size
= sizeof(VirtIOPCIProxy
),
1066 .class_init
= virtio_9p_class_init
,
1071 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
1074 /* This is called by virtio-bus just after the device is plugged. */
1075 static void virtio_pci_device_plugged(DeviceState
*d
)
1077 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1078 VirtioBusState
*bus
= &proxy
->bus
;
1082 proxy
->vdev
= bus
->vdev
;
1084 config
= proxy
->pci_dev
.config
;
1085 if (proxy
->class_code
) {
1086 pci_config_set_class(config
, proxy
->class_code
);
1088 pci_set_word(config
+ PCI_SUBSYSTEM_VENDOR_ID
,
1089 pci_get_word(config
+ PCI_VENDOR_ID
));
1090 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, virtio_bus_get_vdev_id(bus
));
1091 config
[PCI_INTERRUPT_PIN
] = 1;
1093 if (proxy
->nvectors
&&
1094 msix_init_exclusive_bar(&proxy
->pci_dev
, proxy
->nvectors
, 1)) {
1095 proxy
->nvectors
= 0;
1098 proxy
->pci_dev
.config_write
= virtio_write_config
;
1100 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
)
1101 + virtio_bus_get_vdev_config_len(bus
);
1102 if (size
& (size
- 1)) {
1103 size
= 1 << qemu_fls(size
);
1106 memory_region_init_io(&proxy
->bar
, &virtio_pci_config_ops
, proxy
,
1107 "virtio-pci", size
);
1108 pci_register_bar(&proxy
->pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
,
1111 if (!kvm_has_many_ioeventfds()) {
1112 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
1115 proxy
->host_features
|= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY
;
1116 proxy
->host_features
|= 0x1 << VIRTIO_F_BAD_FEATURE
;
1117 proxy
->host_features
= virtio_bus_get_vdev_features(bus
,
1118 proxy
->host_features
);
1121 static int virtio_pci_init(PCIDevice
*pci_dev
)
1123 VirtIOPCIProxy
*dev
= VIRTIO_PCI(pci_dev
);
1124 VirtioPCIClass
*k
= VIRTIO_PCI_GET_CLASS(pci_dev
);
1125 virtio_pci_bus_new(&dev
->bus
, dev
);
1126 if (k
->init
!= NULL
) {
1127 return k
->init(dev
);
1132 static void virtio_pci_exit(PCIDevice
*pci_dev
)
1134 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
1135 virtio_pci_stop_ioeventfd(proxy
);
1136 virtio_exit_pci(pci_dev
);
1140 * This will be renamed virtio_pci_reset at the end of the series.
1141 * virtio_pci_reset is still in use at this moment.
1143 static void virtio_pci_rst(DeviceState
*qdev
)
1145 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1146 VirtioBusState
*bus
= VIRTIO_BUS(&proxy
->bus
);
1147 virtio_pci_stop_ioeventfd(proxy
);
1148 virtio_bus_reset(bus
);
1149 msix_unuse_all_vectors(&proxy
->pci_dev
);
1150 proxy
->flags
&= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG
;
1153 static void virtio_pci_class_init(ObjectClass
*klass
, void *data
)
1155 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1156 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1158 k
->init
= virtio_pci_init
;
1159 k
->exit
= virtio_pci_exit
;
1160 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1161 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1162 k
->class_id
= PCI_CLASS_OTHERS
;
1163 dc
->reset
= virtio_pci_rst
;
1166 static const TypeInfo virtio_pci_info
= {
1167 .name
= TYPE_VIRTIO_PCI
,
1168 .parent
= TYPE_PCI_DEVICE
,
1169 .instance_size
= sizeof(VirtIOPCIProxy
),
1170 .class_init
= virtio_pci_class_init
,
1171 .class_size
= sizeof(VirtioPCIClass
),
1175 /* virtio-blk-pci */
1177 static Property virtio_blk_pci_properties
[] = {
1178 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
1179 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1180 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1181 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1182 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
1183 DEFINE_PROP_BIT("x-data-plane", VirtIOBlkPCI
, blk
.data_plane
, 0, false),
1185 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy
, host_features
),
1186 DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlkPCI
, blk
),
1187 DEFINE_PROP_END_OF_LIST(),
1190 static int virtio_blk_pci_init(VirtIOPCIProxy
*vpci_dev
)
1192 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(vpci_dev
);
1193 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1194 virtio_blk_set_conf(vdev
, &(dev
->blk
));
1195 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1196 if (qdev_init(vdev
) < 0) {
1202 static void virtio_blk_pci_class_init(ObjectClass
*klass
, void *data
)
1204 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1205 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1206 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1208 dc
->props
= virtio_blk_pci_properties
;
1209 k
->init
= virtio_blk_pci_init
;
1210 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1211 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
1212 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1213 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1216 static void virtio_blk_pci_instance_init(Object
*obj
)
1218 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(obj
);
1219 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_BLK
);
1220 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1223 static const TypeInfo virtio_blk_pci_info
= {
1224 .name
= TYPE_VIRTIO_BLK_PCI
,
1225 .parent
= TYPE_VIRTIO_PCI
,
1226 .instance_size
= sizeof(VirtIOBlkPCI
),
1227 .instance_init
= virtio_blk_pci_instance_init
,
1228 .class_init
= virtio_blk_pci_class_init
,
1231 /* virtio-scsi-pci */
1233 static Property virtio_scsi_pci_properties
[] = {
1234 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1235 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1236 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1237 DEV_NVECTORS_UNSPECIFIED
),
1238 DEFINE_VIRTIO_SCSI_FEATURES(VirtIOPCIProxy
, host_features
),
1239 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSIPCI
, vdev
.parent_obj
.conf
),
1240 DEFINE_PROP_END_OF_LIST(),
1243 static int virtio_scsi_pci_init_pci(VirtIOPCIProxy
*vpci_dev
)
1245 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(vpci_dev
);
1246 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1247 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1249 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1250 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1253 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1254 if (qdev_init(vdev
) < 0) {
1260 static void virtio_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1262 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1263 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1264 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1265 k
->init
= virtio_scsi_pci_init_pci
;
1266 dc
->props
= virtio_scsi_pci_properties
;
1267 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1268 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1269 pcidev_k
->revision
= 0x00;
1270 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1273 static void virtio_scsi_pci_instance_init(Object
*obj
)
1275 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(obj
);
1276 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_SCSI
);
1277 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1280 static const TypeInfo virtio_scsi_pci_info
= {
1281 .name
= TYPE_VIRTIO_SCSI_PCI
,
1282 .parent
= TYPE_VIRTIO_PCI
,
1283 .instance_size
= sizeof(VirtIOSCSIPCI
),
1284 .instance_init
= virtio_scsi_pci_instance_init
,
1285 .class_init
= virtio_scsi_pci_class_init
,
1288 /* vhost-scsi-pci */
1290 #ifdef CONFIG_VHOST_SCSI
1291 static Property vhost_scsi_pci_properties
[] = {
1292 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1293 DEV_NVECTORS_UNSPECIFIED
),
1294 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1295 DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSIPCI
, vdev
.parent_obj
.conf
),
1296 DEFINE_PROP_END_OF_LIST(),
1299 static int vhost_scsi_pci_init_pci(VirtIOPCIProxy
*vpci_dev
)
1301 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(vpci_dev
);
1302 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1303 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1305 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1306 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1309 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1310 if (qdev_init(vdev
) < 0) {
1316 static void vhost_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1318 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1319 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1320 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1321 k
->init
= vhost_scsi_pci_init_pci
;
1322 dc
->props
= vhost_scsi_pci_properties
;
1323 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1324 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1325 pcidev_k
->revision
= 0x00;
1326 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1329 static void vhost_scsi_pci_instance_init(Object
*obj
)
1331 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(obj
);
1332 object_initialize(OBJECT(&dev
->vdev
), TYPE_VHOST_SCSI
);
1333 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1336 static const TypeInfo vhost_scsi_pci_info
= {
1337 .name
= TYPE_VHOST_SCSI_PCI
,
1338 .parent
= TYPE_VIRTIO_PCI
,
1339 .instance_size
= sizeof(VHostSCSIPCI
),
1340 .instance_init
= vhost_scsi_pci_instance_init
,
1341 .class_init
= vhost_scsi_pci_class_init
,
1345 /* virtio-balloon-pci */
1347 static void balloon_pci_stats_get_all(Object
*obj
, struct Visitor
*v
,
1348 void *opaque
, const char *name
,
1351 VirtIOBalloonPCI
*dev
= opaque
;
1352 object_property_get(OBJECT(&dev
->vdev
), v
, "guest-stats", errp
);
1355 static void balloon_pci_stats_get_poll_interval(Object
*obj
, struct Visitor
*v
,
1356 void *opaque
, const char *name
,
1359 VirtIOBalloonPCI
*dev
= opaque
;
1360 object_property_get(OBJECT(&dev
->vdev
), v
, "guest-stats-polling-interval",
1364 static void balloon_pci_stats_set_poll_interval(Object
*obj
, struct Visitor
*v
,
1365 void *opaque
, const char *name
,
1368 VirtIOBalloonPCI
*dev
= opaque
;
1369 object_property_set(OBJECT(&dev
->vdev
), v
, "guest-stats-polling-interval",
1373 static Property virtio_balloon_pci_properties
[] = {
1374 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1375 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
1376 DEFINE_PROP_END_OF_LIST(),
1379 static int virtio_balloon_pci_init(VirtIOPCIProxy
*vpci_dev
)
1381 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(vpci_dev
);
1382 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1384 if (vpci_dev
->class_code
!= PCI_CLASS_OTHERS
&&
1385 vpci_dev
->class_code
!= PCI_CLASS_MEMORY_RAM
) { /* qemu < 1.1 */
1386 vpci_dev
->class_code
= PCI_CLASS_OTHERS
;
1389 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1390 if (qdev_init(vdev
) < 0) {
1396 static void virtio_balloon_pci_class_init(ObjectClass
*klass
, void *data
)
1398 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1399 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1400 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1401 k
->init
= virtio_balloon_pci_init
;
1402 dc
->props
= virtio_balloon_pci_properties
;
1403 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1404 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BALLOON
;
1405 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1406 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
1409 static void virtio_balloon_pci_instance_init(Object
*obj
)
1411 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(obj
);
1412 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_BALLOON
);
1413 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1415 object_property_add(obj
, "guest-stats", "guest statistics",
1416 balloon_pci_stats_get_all
, NULL
, NULL
, dev
,
1419 object_property_add(obj
, "guest-stats-polling-interval", "int",
1420 balloon_pci_stats_get_poll_interval
,
1421 balloon_pci_stats_set_poll_interval
,
1425 static const TypeInfo virtio_balloon_pci_info
= {
1426 .name
= TYPE_VIRTIO_BALLOON_PCI
,
1427 .parent
= TYPE_VIRTIO_PCI
,
1428 .instance_size
= sizeof(VirtIOBalloonPCI
),
1429 .instance_init
= virtio_balloon_pci_instance_init
,
1430 .class_init
= virtio_balloon_pci_class_init
,
1433 /* virtio-serial-pci */
1435 static int virtio_serial_pci_init(VirtIOPCIProxy
*vpci_dev
)
1437 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(vpci_dev
);
1438 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1440 if (vpci_dev
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
1441 vpci_dev
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
1442 vpci_dev
->class_code
!= PCI_CLASS_OTHERS
) { /* qemu-kvm */
1443 vpci_dev
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
1446 /* backwards-compatibility with machines that were created with
1447 DEV_NVECTORS_UNSPECIFIED */
1448 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1449 vpci_dev
->nvectors
= dev
->vdev
.serial
.max_virtserial_ports
+ 1;
1452 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1453 if (qdev_init(vdev
) < 0) {
1459 static Property virtio_serial_pci_properties
[] = {
1460 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1461 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1462 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1463 DEFINE_PROP_HEX32("class", VirtIOPCIProxy
, class_code
, 0),
1464 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy
, host_features
),
1465 DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtIOSerialPCI
, vdev
.serial
),
1466 DEFINE_PROP_END_OF_LIST(),
1469 static void virtio_serial_pci_class_init(ObjectClass
*klass
, void *data
)
1471 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1472 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1473 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1474 k
->init
= virtio_serial_pci_init
;
1475 dc
->props
= virtio_serial_pci_properties
;
1476 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1477 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_CONSOLE
;
1478 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1479 pcidev_k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
1482 static void virtio_serial_pci_instance_init(Object
*obj
)
1484 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(obj
);
1485 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_SERIAL
);
1486 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1489 static const TypeInfo virtio_serial_pci_info
= {
1490 .name
= TYPE_VIRTIO_SERIAL_PCI
,
1491 .parent
= TYPE_VIRTIO_PCI
,
1492 .instance_size
= sizeof(VirtIOSerialPCI
),
1493 .instance_init
= virtio_serial_pci_instance_init
,
1494 .class_init
= virtio_serial_pci_class_init
,
1497 /* virtio-net-pci */
1499 static Property virtio_net_properties
[] = {
1500 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1501 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, false),
1502 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
1503 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy
, host_features
),
1504 DEFINE_NIC_PROPERTIES(VirtIONetPCI
, vdev
.nic_conf
),
1505 DEFINE_VIRTIO_NET_PROPERTIES(VirtIONetPCI
, vdev
.net_conf
),
1506 DEFINE_PROP_END_OF_LIST(),
1509 static int virtio_net_pci_init(VirtIOPCIProxy
*vpci_dev
)
1511 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(vpci_dev
);
1512 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1514 virtio_net_set_config_size(&dev
->vdev
, vpci_dev
->host_features
);
1515 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1516 if (qdev_init(vdev
) < 0) {
1522 static void virtio_net_pci_class_init(ObjectClass
*klass
, void *data
)
1524 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1525 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1526 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_CLASS(klass
);
1528 k
->romfile
= "efi-virtio.rom";
1529 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1530 k
->device_id
= PCI_DEVICE_ID_VIRTIO_NET
;
1531 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1532 k
->class_id
= PCI_CLASS_NETWORK_ETHERNET
;
1533 dc
->props
= virtio_net_properties
;
1534 vpciklass
->init
= virtio_net_pci_init
;
1537 static void virtio_net_pci_instance_init(Object
*obj
)
1539 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(obj
);
1540 object_initialize(OBJECT(&dev
->vdev
), TYPE_VIRTIO_NET
);
1541 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1544 static const TypeInfo virtio_net_pci_info
= {
1545 .name
= TYPE_VIRTIO_NET_PCI
,
1546 .parent
= TYPE_VIRTIO_PCI
,
1547 .instance_size
= sizeof(VirtIONetPCI
),
1548 .instance_init
= virtio_net_pci_instance_init
,
1549 .class_init
= virtio_net_pci_class_init
,
1552 /* virtio-pci-bus */
1554 void virtio_pci_bus_new(VirtioBusState
*bus
, VirtIOPCIProxy
*dev
)
1556 DeviceState
*qdev
= DEVICE(dev
);
1558 qbus_create_inplace((BusState
*)bus
, TYPE_VIRTIO_PCI_BUS
, qdev
, NULL
);
1560 qbus
->allow_hotplug
= 1;
1563 static void virtio_pci_bus_class_init(ObjectClass
*klass
, void *data
)
1565 BusClass
*bus_class
= BUS_CLASS(klass
);
1566 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
1567 bus_class
->max_dev
= 1;
1568 k
->notify
= virtio_pci_notify
;
1569 k
->save_config
= virtio_pci_save_config
;
1570 k
->load_config
= virtio_pci_load_config
;
1571 k
->save_queue
= virtio_pci_save_queue
;
1572 k
->load_queue
= virtio_pci_load_queue
;
1573 k
->get_features
= virtio_pci_get_features
;
1574 k
->query_guest_notifiers
= virtio_pci_query_guest_notifiers
;
1575 k
->set_host_notifier
= virtio_pci_set_host_notifier
;
1576 k
->set_guest_notifiers
= virtio_pci_set_guest_notifiers
;
1577 k
->vmstate_change
= virtio_pci_vmstate_change
;
1578 k
->device_plugged
= virtio_pci_device_plugged
;
1581 static const TypeInfo virtio_pci_bus_info
= {
1582 .name
= TYPE_VIRTIO_PCI_BUS
,
1583 .parent
= TYPE_VIRTIO_BUS
,
1584 .instance_size
= sizeof(VirtioPCIBusState
),
1585 .class_init
= virtio_pci_bus_class_init
,
1588 static void virtio_pci_register_types(void)
1590 type_register_static(&virtio_rng_info
);
1591 type_register_static(&virtio_pci_bus_info
);
1592 type_register_static(&virtio_pci_info
);
1593 #ifdef CONFIG_VIRTFS
1594 type_register_static(&virtio_9p_info
);
1596 type_register_static(&virtio_blk_pci_info
);
1597 type_register_static(&virtio_scsi_pci_info
);
1598 type_register_static(&virtio_balloon_pci_info
);
1599 type_register_static(&virtio_serial_pci_info
);
1600 type_register_static(&virtio_net_pci_info
);
1601 #ifdef CONFIG_VHOST_SCSI
1602 type_register_static(&vhost_scsi_pci_info
);
1606 type_init(virtio_pci_register_types
)