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 "standard-headers/linux/virtio_pci.h"
21 #include "hw/virtio/virtio.h"
22 #include "hw/virtio/virtio-blk.h"
23 #include "hw/virtio/virtio-net.h"
24 #include "hw/virtio/virtio-serial.h"
25 #include "hw/virtio/virtio-scsi.h"
26 #include "hw/virtio/virtio-balloon.h"
27 #include "hw/virtio/virtio-input.h"
28 #include "hw/pci/pci.h"
29 #include "qemu/error-report.h"
30 #include "hw/pci/msi.h"
31 #include "hw/pci/msix.h"
32 #include "hw/loader.h"
33 #include "sysemu/kvm.h"
34 #include "sysemu/block-backend.h"
35 #include "virtio-pci.h"
36 #include "qemu/range.h"
37 #include "hw/virtio/virtio-bus.h"
38 #include "qapi/visitor.h"
40 #define VIRTIO_PCI_REGION_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_present(dev))
42 #undef VIRTIO_PCI_CONFIG
44 /* The remaining space is defined by each driver as the per-driver
45 * configuration space */
46 #define VIRTIO_PCI_CONFIG_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_enabled(dev))
48 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
52 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
53 static inline VirtIOPCIProxy
*to_virtio_pci_proxy(DeviceState
*d
)
55 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
58 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
59 * be careful and test performance if you change this.
61 static inline VirtIOPCIProxy
*to_virtio_pci_proxy_fast(DeviceState
*d
)
63 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
66 static void virtio_pci_notify(DeviceState
*d
, uint16_t vector
)
68 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy_fast(d
);
70 if (msix_enabled(&proxy
->pci_dev
))
71 msix_notify(&proxy
->pci_dev
, vector
);
73 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
74 pci_set_irq(&proxy
->pci_dev
, vdev
->isr
& 1);
78 static void virtio_pci_save_config(DeviceState
*d
, QEMUFile
*f
)
80 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
81 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
83 pci_device_save(&proxy
->pci_dev
, f
);
84 msix_save(&proxy
->pci_dev
, f
);
85 if (msix_present(&proxy
->pci_dev
))
86 qemu_put_be16(f
, vdev
->config_vector
);
89 static void virtio_pci_save_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
91 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
92 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
94 if (msix_present(&proxy
->pci_dev
))
95 qemu_put_be16(f
, virtio_queue_vector(vdev
, n
));
98 static int virtio_pci_load_config(DeviceState
*d
, QEMUFile
*f
)
100 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
101 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
104 ret
= pci_device_load(&proxy
->pci_dev
, f
);
108 msix_unuse_all_vectors(&proxy
->pci_dev
);
109 msix_load(&proxy
->pci_dev
, f
);
110 if (msix_present(&proxy
->pci_dev
)) {
111 qemu_get_be16s(f
, &vdev
->config_vector
);
113 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
115 if (vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
116 return msix_vector_use(&proxy
->pci_dev
, vdev
->config_vector
);
121 static int virtio_pci_load_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
123 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
124 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
127 if (msix_present(&proxy
->pci_dev
)) {
128 qemu_get_be16s(f
, &vector
);
130 vector
= VIRTIO_NO_VECTOR
;
132 virtio_queue_set_vector(vdev
, n
, vector
);
133 if (vector
!= VIRTIO_NO_VECTOR
) {
134 return msix_vector_use(&proxy
->pci_dev
, vector
);
139 #define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000
141 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy
*proxy
,
142 int n
, bool assign
, bool set_handler
)
144 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
145 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
146 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
147 bool legacy
= !(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_LEGACY
);
148 bool modern
= !(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_MODERN
);
149 MemoryRegion
*modern_mr
= &proxy
->notify
.mr
;
150 MemoryRegion
*legacy_mr
= &proxy
->bar
;
151 hwaddr modern_addr
= QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
*
152 virtio_get_queue_index(vq
);
153 hwaddr legacy_addr
= VIRTIO_PCI_QUEUE_NOTIFY
;
157 r
= event_notifier_init(notifier
, 1);
159 error_report("%s: unable to init event notifier: %d",
163 virtio_queue_set_host_notifier_fd_handler(vq
, true, set_handler
);
165 memory_region_add_eventfd(modern_mr
, modern_addr
, 2,
169 memory_region_add_eventfd(legacy_mr
, legacy_addr
, 2,
174 memory_region_del_eventfd(modern_mr
, modern_addr
, 2,
178 memory_region_del_eventfd(legacy_mr
, legacy_addr
, 2,
181 virtio_queue_set_host_notifier_fd_handler(vq
, false, false);
182 event_notifier_cleanup(notifier
);
187 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
189 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
192 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) ||
193 proxy
->ioeventfd_disabled
||
194 proxy
->ioeventfd_started
) {
198 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
199 if (!virtio_queue_get_num(vdev
, n
)) {
203 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, true, true);
208 proxy
->ioeventfd_started
= true;
213 if (!virtio_queue_get_num(vdev
, n
)) {
217 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
220 proxy
->ioeventfd_started
= false;
221 error_report("%s: failed. Fallback to a userspace (slower).", __func__
);
224 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
226 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
230 if (!proxy
->ioeventfd_started
) {
234 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
235 if (!virtio_queue_get_num(vdev
, n
)) {
239 r
= virtio_pci_set_host_notifier_internal(proxy
, n
, false, false);
242 proxy
->ioeventfd_started
= false;
245 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
247 VirtIOPCIProxy
*proxy
= opaque
;
248 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
252 case VIRTIO_PCI_GUEST_FEATURES
:
253 /* Guest does not negotiate properly? We have to assume nothing. */
254 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
255 val
= virtio_bus_get_vdev_bad_features(&proxy
->bus
);
257 virtio_set_features(vdev
, val
);
259 case VIRTIO_PCI_QUEUE_PFN
:
260 pa
= (hwaddr
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
262 virtio_pci_stop_ioeventfd(proxy
);
264 msix_unuse_all_vectors(&proxy
->pci_dev
);
267 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
269 case VIRTIO_PCI_QUEUE_SEL
:
270 if (val
< VIRTIO_QUEUE_MAX
)
271 vdev
->queue_sel
= val
;
273 case VIRTIO_PCI_QUEUE_NOTIFY
:
274 if (val
< VIRTIO_QUEUE_MAX
) {
275 virtio_queue_notify(vdev
, val
);
278 case VIRTIO_PCI_STATUS
:
279 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
280 virtio_pci_stop_ioeventfd(proxy
);
283 virtio_set_status(vdev
, val
& 0xFF);
285 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
286 virtio_pci_start_ioeventfd(proxy
);
289 if (vdev
->status
== 0) {
291 msix_unuse_all_vectors(&proxy
->pci_dev
);
294 /* Linux before 2.6.34 drives the device without enabling
295 the PCI device bus master bit. Enable it automatically
296 for the guest. This is a PCI spec violation but so is
297 initiating DMA with bus master bit clear. */
298 if (val
== (VIRTIO_CONFIG_S_ACKNOWLEDGE
| VIRTIO_CONFIG_S_DRIVER
)) {
299 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
300 proxy
->pci_dev
.config
[PCI_COMMAND
] |
301 PCI_COMMAND_MASTER
, 1);
304 case VIRTIO_MSI_CONFIG_VECTOR
:
305 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
306 /* Make it possible for guest to discover an error took place. */
307 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
308 val
= VIRTIO_NO_VECTOR
;
309 vdev
->config_vector
= val
;
311 case VIRTIO_MSI_QUEUE_VECTOR
:
312 msix_vector_unuse(&proxy
->pci_dev
,
313 virtio_queue_vector(vdev
, vdev
->queue_sel
));
314 /* Make it possible for guest to discover an error took place. */
315 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
316 val
= VIRTIO_NO_VECTOR
;
317 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
320 error_report("%s: unexpected address 0x%x value 0x%x",
321 __func__
, addr
, val
);
326 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
328 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
329 uint32_t ret
= 0xFFFFFFFF;
332 case VIRTIO_PCI_HOST_FEATURES
:
333 ret
= vdev
->host_features
;
335 case VIRTIO_PCI_GUEST_FEATURES
:
336 ret
= vdev
->guest_features
;
338 case VIRTIO_PCI_QUEUE_PFN
:
339 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
340 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
342 case VIRTIO_PCI_QUEUE_NUM
:
343 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
345 case VIRTIO_PCI_QUEUE_SEL
:
346 ret
= vdev
->queue_sel
;
348 case VIRTIO_PCI_STATUS
:
352 /* reading from the ISR also clears it. */
355 pci_irq_deassert(&proxy
->pci_dev
);
357 case VIRTIO_MSI_CONFIG_VECTOR
:
358 ret
= vdev
->config_vector
;
360 case VIRTIO_MSI_QUEUE_VECTOR
:
361 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
370 static uint64_t virtio_pci_config_read(void *opaque
, hwaddr addr
,
373 VirtIOPCIProxy
*proxy
= opaque
;
374 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
375 uint32_t config
= VIRTIO_PCI_CONFIG_SIZE(&proxy
->pci_dev
);
378 return virtio_ioport_read(proxy
, addr
);
384 val
= virtio_config_readb(vdev
, addr
);
387 val
= virtio_config_readw(vdev
, addr
);
388 if (virtio_is_big_endian(vdev
)) {
393 val
= virtio_config_readl(vdev
, addr
);
394 if (virtio_is_big_endian(vdev
)) {
402 static void virtio_pci_config_write(void *opaque
, hwaddr addr
,
403 uint64_t val
, unsigned size
)
405 VirtIOPCIProxy
*proxy
= opaque
;
406 uint32_t config
= VIRTIO_PCI_CONFIG_SIZE(&proxy
->pci_dev
);
407 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
409 virtio_ioport_write(proxy
, addr
, val
);
414 * Virtio-PCI is odd. Ioports are LE but config space is target native
419 virtio_config_writeb(vdev
, addr
, val
);
422 if (virtio_is_big_endian(vdev
)) {
425 virtio_config_writew(vdev
, addr
, val
);
428 if (virtio_is_big_endian(vdev
)) {
431 virtio_config_writel(vdev
, addr
, val
);
436 static const MemoryRegionOps virtio_pci_config_ops
= {
437 .read
= virtio_pci_config_read
,
438 .write
= virtio_pci_config_write
,
440 .min_access_size
= 1,
441 .max_access_size
= 4,
443 .endianness
= DEVICE_LITTLE_ENDIAN
,
446 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
447 uint32_t val
, int len
)
449 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
450 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
452 pci_default_write_config(pci_dev
, address
, val
, len
);
454 if (range_covers_byte(address
, len
, PCI_COMMAND
) &&
455 !(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
456 virtio_pci_stop_ioeventfd(proxy
);
457 virtio_set_status(vdev
, vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
461 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy
*proxy
,
462 unsigned int queue_no
,
466 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
469 if (irqfd
->users
== 0) {
470 ret
= kvm_irqchip_add_msi_route(kvm_state
, msg
);
480 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy
*proxy
,
483 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
484 if (--irqfd
->users
== 0) {
485 kvm_irqchip_release_virq(kvm_state
, irqfd
->virq
);
489 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy
*proxy
,
490 unsigned int queue_no
,
493 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
494 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
495 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
496 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
498 ret
= kvm_irqchip_add_irqfd_notifier(kvm_state
, n
, NULL
, irqfd
->virq
);
502 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy
*proxy
,
503 unsigned int queue_no
,
506 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
507 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
508 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
509 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
512 ret
= kvm_irqchip_remove_irqfd_notifier(kvm_state
, n
, irqfd
->virq
);
516 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy
*proxy
, int nvqs
)
518 PCIDevice
*dev
= &proxy
->pci_dev
;
519 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
520 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
525 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
526 if (!virtio_queue_get_num(vdev
, queue_no
)) {
529 vector
= virtio_queue_vector(vdev
, queue_no
);
530 if (vector
>= msix_nr_vectors_allocated(dev
)) {
533 msg
= msix_get_message(dev
, vector
);
534 ret
= kvm_virtio_pci_vq_vector_use(proxy
, queue_no
, vector
, msg
);
538 /* If guest supports masking, set up irqfd now.
539 * Otherwise, delay until unmasked in the frontend.
541 if (k
->guest_notifier_mask
) {
542 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
544 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
552 while (--queue_no
>= 0) {
553 vector
= virtio_queue_vector(vdev
, queue_no
);
554 if (vector
>= msix_nr_vectors_allocated(dev
)) {
557 if (k
->guest_notifier_mask
) {
558 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
560 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
565 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy
*proxy
, int nvqs
)
567 PCIDevice
*dev
= &proxy
->pci_dev
;
568 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
571 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
573 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
574 if (!virtio_queue_get_num(vdev
, queue_no
)) {
577 vector
= virtio_queue_vector(vdev
, queue_no
);
578 if (vector
>= msix_nr_vectors_allocated(dev
)) {
581 /* If guest supports masking, clean up irqfd now.
582 * Otherwise, it was cleaned when masked in the frontend.
584 if (k
->guest_notifier_mask
) {
585 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
587 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
591 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy
*proxy
,
592 unsigned int queue_no
,
596 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
597 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
598 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
599 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
603 if (proxy
->vector_irqfd
) {
604 irqfd
= &proxy
->vector_irqfd
[vector
];
605 if (irqfd
->msg
.data
!= msg
.data
|| irqfd
->msg
.address
!= msg
.address
) {
606 ret
= kvm_irqchip_update_msi_route(kvm_state
, irqfd
->virq
, msg
);
613 /* If guest supports masking, irqfd is already setup, unmask it.
614 * Otherwise, set it up now.
616 if (k
->guest_notifier_mask
) {
617 k
->guest_notifier_mask(vdev
, queue_no
, false);
618 /* Test after unmasking to avoid losing events. */
619 if (k
->guest_notifier_pending
&&
620 k
->guest_notifier_pending(vdev
, queue_no
)) {
621 event_notifier_set(n
);
624 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
629 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy
*proxy
,
630 unsigned int queue_no
,
633 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
634 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
636 /* If guest supports masking, keep irqfd but mask it.
637 * Otherwise, clean it up now.
639 if (k
->guest_notifier_mask
) {
640 k
->guest_notifier_mask(vdev
, queue_no
, true);
642 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
646 static int virtio_pci_vector_unmask(PCIDevice
*dev
, unsigned vector
,
649 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
650 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
651 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
652 int ret
, index
, unmasked
= 0;
655 index
= virtio_get_queue_index(vq
);
656 if (!virtio_queue_get_num(vdev
, index
)) {
659 if (index
< proxy
->nvqs_with_notifiers
) {
660 ret
= virtio_pci_vq_vector_unmask(proxy
, index
, vector
, msg
);
666 vq
= virtio_vector_next_queue(vq
);
672 vq
= virtio_vector_first_queue(vdev
, vector
);
673 while (vq
&& unmasked
>= 0) {
674 index
= virtio_get_queue_index(vq
);
675 if (index
< proxy
->nvqs_with_notifiers
) {
676 virtio_pci_vq_vector_mask(proxy
, index
, vector
);
679 vq
= virtio_vector_next_queue(vq
);
684 static void virtio_pci_vector_mask(PCIDevice
*dev
, unsigned vector
)
686 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
687 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
688 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
692 index
= virtio_get_queue_index(vq
);
693 if (!virtio_queue_get_num(vdev
, index
)) {
696 if (index
< proxy
->nvqs_with_notifiers
) {
697 virtio_pci_vq_vector_mask(proxy
, index
, vector
);
699 vq
= virtio_vector_next_queue(vq
);
703 static void virtio_pci_vector_poll(PCIDevice
*dev
,
704 unsigned int vector_start
,
705 unsigned int vector_end
)
707 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
708 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
709 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
712 EventNotifier
*notifier
;
715 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
716 if (!virtio_queue_get_num(vdev
, queue_no
)) {
719 vector
= virtio_queue_vector(vdev
, queue_no
);
720 if (vector
< vector_start
|| vector
>= vector_end
||
721 !msix_is_masked(dev
, vector
)) {
724 vq
= virtio_get_queue(vdev
, queue_no
);
725 notifier
= virtio_queue_get_guest_notifier(vq
);
726 if (k
->guest_notifier_pending
) {
727 if (k
->guest_notifier_pending(vdev
, queue_no
)) {
728 msix_set_pending(dev
, vector
);
730 } else if (event_notifier_test_and_clear(notifier
)) {
731 msix_set_pending(dev
, vector
);
736 static int virtio_pci_set_guest_notifier(DeviceState
*d
, int n
, bool assign
,
739 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
740 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
741 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
742 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
743 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
746 int r
= event_notifier_init(notifier
, 0);
750 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
752 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
753 event_notifier_cleanup(notifier
);
756 if (!msix_enabled(&proxy
->pci_dev
) && vdc
->guest_notifier_mask
) {
757 vdc
->guest_notifier_mask(vdev
, n
, !assign
);
763 static bool virtio_pci_query_guest_notifiers(DeviceState
*d
)
765 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
766 return msix_enabled(&proxy
->pci_dev
);
769 static int virtio_pci_set_guest_notifiers(DeviceState
*d
, int nvqs
, bool assign
)
771 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
772 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
773 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
775 bool with_irqfd
= msix_enabled(&proxy
->pci_dev
) &&
776 kvm_msi_via_irqfd_enabled();
778 nvqs
= MIN(nvqs
, VIRTIO_QUEUE_MAX
);
780 /* When deassigning, pass a consistent nvqs value
781 * to avoid leaking notifiers.
783 assert(assign
|| nvqs
== proxy
->nvqs_with_notifiers
);
785 proxy
->nvqs_with_notifiers
= nvqs
;
787 /* Must unset vector notifier while guest notifier is still assigned */
788 if ((proxy
->vector_irqfd
|| k
->guest_notifier_mask
) && !assign
) {
789 msix_unset_vector_notifiers(&proxy
->pci_dev
);
790 if (proxy
->vector_irqfd
) {
791 kvm_virtio_pci_vector_release(proxy
, nvqs
);
792 g_free(proxy
->vector_irqfd
);
793 proxy
->vector_irqfd
= NULL
;
797 for (n
= 0; n
< nvqs
; n
++) {
798 if (!virtio_queue_get_num(vdev
, n
)) {
802 r
= virtio_pci_set_guest_notifier(d
, n
, assign
, with_irqfd
);
808 /* Must set vector notifier after guest notifier has been assigned */
809 if ((with_irqfd
|| k
->guest_notifier_mask
) && assign
) {
811 proxy
->vector_irqfd
=
812 g_malloc0(sizeof(*proxy
->vector_irqfd
) *
813 msix_nr_vectors_allocated(&proxy
->pci_dev
));
814 r
= kvm_virtio_pci_vector_use(proxy
, nvqs
);
819 r
= msix_set_vector_notifiers(&proxy
->pci_dev
,
820 virtio_pci_vector_unmask
,
821 virtio_pci_vector_mask
,
822 virtio_pci_vector_poll
);
824 goto notifiers_error
;
833 kvm_virtio_pci_vector_release(proxy
, nvqs
);
837 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
840 virtio_pci_set_guest_notifier(d
, n
, !assign
, with_irqfd
);
845 static int virtio_pci_set_host_notifier(DeviceState
*d
, int n
, bool assign
)
847 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
849 /* Stop using ioeventfd for virtqueue kick if the device starts using host
850 * notifiers. This makes it easy to avoid stepping on each others' toes.
852 proxy
->ioeventfd_disabled
= assign
;
854 virtio_pci_stop_ioeventfd(proxy
);
856 /* We don't need to start here: it's not needed because backend
857 * currently only stops on status change away from ok,
858 * reset, vmstop and such. If we do add code to start here,
859 * need to check vmstate, device state etc. */
860 return virtio_pci_set_host_notifier_internal(proxy
, n
, assign
, false);
863 static void virtio_pci_vmstate_change(DeviceState
*d
, bool running
)
865 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
866 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
869 /* Old QEMU versions did not set bus master enable on status write.
870 * Detect DRIVER set and enable it.
872 if ((proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION
) &&
873 (vdev
->status
& VIRTIO_CONFIG_S_DRIVER
) &&
874 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
875 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
876 proxy
->pci_dev
.config
[PCI_COMMAND
] |
877 PCI_COMMAND_MASTER
, 1);
879 virtio_pci_start_ioeventfd(proxy
);
881 virtio_pci_stop_ioeventfd(proxy
);
886 static void virtio_9p_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
888 V9fsPCIState
*dev
= VIRTIO_9P_PCI(vpci_dev
);
889 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
891 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
892 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
895 static Property virtio_9p_pci_properties
[] = {
896 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
897 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
898 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
899 DEFINE_PROP_END_OF_LIST(),
902 static void virtio_9p_pci_class_init(ObjectClass
*klass
, void *data
)
904 DeviceClass
*dc
= DEVICE_CLASS(klass
);
905 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
906 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
908 k
->realize
= virtio_9p_pci_realize
;
909 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
910 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_9P
;
911 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
912 pcidev_k
->class_id
= 0x2;
913 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
914 dc
->props
= virtio_9p_pci_properties
;
917 static void virtio_9p_pci_instance_init(Object
*obj
)
919 V9fsPCIState
*dev
= VIRTIO_9P_PCI(obj
);
921 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
925 static const TypeInfo virtio_9p_pci_info
= {
926 .name
= TYPE_VIRTIO_9P_PCI
,
927 .parent
= TYPE_VIRTIO_PCI
,
928 .instance_size
= sizeof(V9fsPCIState
),
929 .instance_init
= virtio_9p_pci_instance_init
,
930 .class_init
= virtio_9p_pci_class_init
,
932 #endif /* CONFIG_VIRTFS */
935 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
938 static int virtio_pci_query_nvectors(DeviceState
*d
)
940 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
942 return proxy
->nvectors
;
945 static void virtio_pci_add_mem_cap(VirtIOPCIProxy
*proxy
,
946 struct virtio_pci_cap
*cap
)
948 PCIDevice
*dev
= &proxy
->pci_dev
;
951 offset
= pci_add_capability(dev
, PCI_CAP_ID_VNDR
, 0, cap
->cap_len
);
954 assert(cap
->cap_len
>= sizeof *cap
);
955 memcpy(dev
->config
+ offset
+ PCI_CAP_FLAGS
, &cap
->cap_len
,
956 cap
->cap_len
- PCI_CAP_FLAGS
);
959 static uint64_t virtio_pci_common_read(void *opaque
, hwaddr addr
,
962 VirtIOPCIProxy
*proxy
= opaque
;
963 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
968 case VIRTIO_PCI_COMMON_DFSELECT
:
969 val
= proxy
->dfselect
;
971 case VIRTIO_PCI_COMMON_DF
:
972 if (proxy
->dfselect
<= 1) {
973 val
= vdev
->host_features
>> (32 * proxy
->dfselect
);
976 case VIRTIO_PCI_COMMON_GFSELECT
:
977 val
= proxy
->gfselect
;
979 case VIRTIO_PCI_COMMON_GF
:
980 if (proxy
->gfselect
<= ARRAY_SIZE(proxy
->guest_features
)) {
981 val
= proxy
->guest_features
[proxy
->gfselect
];
984 case VIRTIO_PCI_COMMON_MSIX
:
985 val
= vdev
->config_vector
;
987 case VIRTIO_PCI_COMMON_NUMQ
:
988 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; ++i
) {
989 if (virtio_queue_get_num(vdev
, i
)) {
994 case VIRTIO_PCI_COMMON_STATUS
:
997 case VIRTIO_PCI_COMMON_CFGGENERATION
:
998 val
= vdev
->generation
;
1000 case VIRTIO_PCI_COMMON_Q_SELECT
:
1001 val
= vdev
->queue_sel
;
1003 case VIRTIO_PCI_COMMON_Q_SIZE
:
1004 val
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
1006 case VIRTIO_PCI_COMMON_Q_MSIX
:
1007 val
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
1009 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1010 val
= proxy
->vqs
[vdev
->queue_sel
].enabled
;
1012 case VIRTIO_PCI_COMMON_Q_NOFF
:
1013 /* Simply map queues in order */
1014 val
= vdev
->queue_sel
;
1016 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1017 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[0];
1019 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1020 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[1];
1022 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1023 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[0];
1025 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1026 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[1];
1028 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1029 val
= proxy
->vqs
[vdev
->queue_sel
].used
[0];
1031 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1032 val
= proxy
->vqs
[vdev
->queue_sel
].used
[1];
1041 static void virtio_pci_common_write(void *opaque
, hwaddr addr
,
1042 uint64_t val
, unsigned size
)
1044 VirtIOPCIProxy
*proxy
= opaque
;
1045 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1048 case VIRTIO_PCI_COMMON_DFSELECT
:
1049 proxy
->dfselect
= val
;
1051 case VIRTIO_PCI_COMMON_GFSELECT
:
1052 proxy
->gfselect
= val
;
1054 case VIRTIO_PCI_COMMON_GF
:
1055 if (proxy
->gfselect
<= ARRAY_SIZE(proxy
->guest_features
)) {
1056 proxy
->guest_features
[proxy
->gfselect
] = val
;
1057 virtio_set_features(vdev
,
1058 (((uint64_t)proxy
->guest_features
[1]) << 32) |
1059 proxy
->guest_features
[0]);
1062 case VIRTIO_PCI_COMMON_MSIX
:
1063 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
1064 /* Make it possible for guest to discover an error took place. */
1065 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0) {
1066 val
= VIRTIO_NO_VECTOR
;
1068 vdev
->config_vector
= val
;
1070 case VIRTIO_PCI_COMMON_STATUS
:
1071 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
1072 virtio_pci_stop_ioeventfd(proxy
);
1075 virtio_set_status(vdev
, val
& 0xFF);
1077 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
1078 virtio_pci_start_ioeventfd(proxy
);
1081 if (vdev
->status
== 0) {
1083 msix_unuse_all_vectors(&proxy
->pci_dev
);
1087 case VIRTIO_PCI_COMMON_Q_SELECT
:
1088 if (val
< VIRTIO_QUEUE_MAX
) {
1089 vdev
->queue_sel
= val
;
1092 case VIRTIO_PCI_COMMON_Q_SIZE
:
1093 proxy
->vqs
[vdev
->queue_sel
].num
= val
;
1095 case VIRTIO_PCI_COMMON_Q_MSIX
:
1096 msix_vector_unuse(&proxy
->pci_dev
,
1097 virtio_queue_vector(vdev
, vdev
->queue_sel
));
1098 /* Make it possible for guest to discover an error took place. */
1099 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0) {
1100 val
= VIRTIO_NO_VECTOR
;
1102 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
1104 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1105 /* TODO: need a way to put num back on reset. */
1106 virtio_queue_set_num(vdev
, vdev
->queue_sel
,
1107 proxy
->vqs
[vdev
->queue_sel
].num
);
1108 virtio_queue_set_rings(vdev
, vdev
->queue_sel
,
1109 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].desc
[1]) << 32 |
1110 proxy
->vqs
[vdev
->queue_sel
].desc
[0],
1111 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].avail
[1]) << 32 |
1112 proxy
->vqs
[vdev
->queue_sel
].avail
[0],
1113 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].used
[1]) << 32 |
1114 proxy
->vqs
[vdev
->queue_sel
].used
[0]);
1116 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1117 proxy
->vqs
[vdev
->queue_sel
].desc
[0] = val
;
1119 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1120 proxy
->vqs
[vdev
->queue_sel
].desc
[1] = val
;
1122 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1123 proxy
->vqs
[vdev
->queue_sel
].avail
[0] = val
;
1125 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1126 proxy
->vqs
[vdev
->queue_sel
].avail
[1] = val
;
1128 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1129 proxy
->vqs
[vdev
->queue_sel
].used
[0] = val
;
1131 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1132 proxy
->vqs
[vdev
->queue_sel
].used
[1] = val
;
1140 static uint64_t virtio_pci_notify_read(void *opaque
, hwaddr addr
,
1146 static void virtio_pci_notify_write(void *opaque
, hwaddr addr
,
1147 uint64_t val
, unsigned size
)
1149 VirtIODevice
*vdev
= opaque
;
1150 unsigned queue
= addr
/ QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
;
1152 if (queue
< VIRTIO_QUEUE_MAX
) {
1153 virtio_queue_notify(vdev
, queue
);
1157 static uint64_t virtio_pci_isr_read(void *opaque
, hwaddr addr
,
1160 VirtIOPCIProxy
*proxy
= opaque
;
1161 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1162 uint64_t val
= vdev
->isr
;
1165 pci_irq_deassert(&proxy
->pci_dev
);
1170 static void virtio_pci_isr_write(void *opaque
, hwaddr addr
,
1171 uint64_t val
, unsigned size
)
1175 static uint64_t virtio_pci_device_read(void *opaque
, hwaddr addr
,
1178 VirtIODevice
*vdev
= opaque
;
1183 val
= virtio_config_modern_readb(vdev
, addr
);
1186 val
= virtio_config_modern_readw(vdev
, addr
);
1189 val
= virtio_config_modern_readl(vdev
, addr
);
1195 static void virtio_pci_device_write(void *opaque
, hwaddr addr
,
1196 uint64_t val
, unsigned size
)
1198 VirtIODevice
*vdev
= opaque
;
1201 virtio_config_modern_writeb(vdev
, addr
, val
);
1204 virtio_config_modern_writew(vdev
, addr
, val
);
1207 virtio_config_modern_writel(vdev
, addr
, val
);
1212 static void virtio_pci_modern_regions_init(VirtIOPCIProxy
*proxy
)
1214 static const MemoryRegionOps common_ops
= {
1215 .read
= virtio_pci_common_read
,
1216 .write
= virtio_pci_common_write
,
1218 .min_access_size
= 1,
1219 .max_access_size
= 4,
1221 .endianness
= DEVICE_LITTLE_ENDIAN
,
1223 static const MemoryRegionOps isr_ops
= {
1224 .read
= virtio_pci_isr_read
,
1225 .write
= virtio_pci_isr_write
,
1227 .min_access_size
= 1,
1228 .max_access_size
= 4,
1230 .endianness
= DEVICE_LITTLE_ENDIAN
,
1232 static const MemoryRegionOps device_ops
= {
1233 .read
= virtio_pci_device_read
,
1234 .write
= virtio_pci_device_write
,
1236 .min_access_size
= 1,
1237 .max_access_size
= 4,
1239 .endianness
= DEVICE_LITTLE_ENDIAN
,
1241 static const MemoryRegionOps notify_ops
= {
1242 .read
= virtio_pci_notify_read
,
1243 .write
= virtio_pci_notify_write
,
1245 .min_access_size
= 1,
1246 .max_access_size
= 4,
1248 .endianness
= DEVICE_LITTLE_ENDIAN
,
1251 memory_region_init_io(&proxy
->common
.mr
, OBJECT(proxy
),
1254 "virtio-pci-common",
1255 proxy
->common
.size
);
1257 memory_region_init_io(&proxy
->isr
.mr
, OBJECT(proxy
),
1263 memory_region_init_io(&proxy
->device
.mr
, OBJECT(proxy
),
1265 virtio_bus_get_device(&proxy
->bus
),
1266 "virtio-pci-device",
1267 proxy
->device
.size
);
1269 memory_region_init_io(&proxy
->notify
.mr
, OBJECT(proxy
),
1271 virtio_bus_get_device(&proxy
->bus
),
1272 "virtio-pci-notify",
1273 proxy
->notify
.size
);
1276 static void virtio_pci_modern_region_map(VirtIOPCIProxy
*proxy
,
1277 VirtIOPCIRegion
*region
,
1278 struct virtio_pci_cap
*cap
)
1280 memory_region_add_subregion(&proxy
->modern_bar
,
1284 cap
->cfg_type
= region
->type
;
1285 cap
->bar
= proxy
->modern_mem_bar
;
1286 cap
->offset
= cpu_to_le32(region
->offset
);
1287 cap
->length
= cpu_to_le32(region
->size
);
1288 virtio_pci_add_mem_cap(proxy
, cap
);
1291 /* This is called by virtio-bus just after the device is plugged. */
1292 static void virtio_pci_device_plugged(DeviceState
*d
, Error
**errp
)
1294 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1295 VirtioBusState
*bus
= &proxy
->bus
;
1296 bool legacy
= !(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_LEGACY
);
1297 bool modern
= !(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_MODERN
);
1300 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1302 config
= proxy
->pci_dev
.config
;
1303 if (proxy
->class_code
) {
1304 pci_config_set_class(config
, proxy
->class_code
);
1308 /* legacy and transitional */
1309 pci_set_word(config
+ PCI_SUBSYSTEM_VENDOR_ID
,
1310 pci_get_word(config
+ PCI_VENDOR_ID
));
1311 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, virtio_bus_get_vdev_id(bus
));
1313 /* pure virtio-1.0 */
1314 pci_set_word(config
+ PCI_VENDOR_ID
,
1315 PCI_VENDOR_ID_REDHAT_QUMRANET
);
1316 pci_set_word(config
+ PCI_DEVICE_ID
,
1317 0x1040 + virtio_bus_get_vdev_id(bus
));
1318 pci_config_set_revision(config
, 1);
1320 config
[PCI_INTERRUPT_PIN
] = 1;
1324 struct virtio_pci_cap cap
= {
1325 .cap_len
= sizeof cap
,
1327 struct virtio_pci_notify_cap notify
= {
1328 .cap
.cap_len
= sizeof notify
,
1329 .notify_off_multiplier
=
1330 cpu_to_le32(QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
),
1333 /* TODO: add io access for speed */
1335 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_VERSION_1
);
1336 virtio_pci_modern_regions_init(proxy
);
1337 virtio_pci_modern_region_map(proxy
, &proxy
->common
, &cap
);
1338 virtio_pci_modern_region_map(proxy
, &proxy
->isr
, &cap
);
1339 virtio_pci_modern_region_map(proxy
, &proxy
->device
, &cap
);
1340 virtio_pci_modern_region_map(proxy
, &proxy
->notify
, ¬ify
.cap
);
1341 pci_register_bar(&proxy
->pci_dev
, proxy
->modern_mem_bar
,
1342 PCI_BASE_ADDRESS_SPACE_MEMORY
|
1343 PCI_BASE_ADDRESS_MEM_PREFETCH
|
1344 PCI_BASE_ADDRESS_MEM_TYPE_64
,
1345 &proxy
->modern_bar
);
1348 if (proxy
->nvectors
&&
1349 msix_init_exclusive_bar(&proxy
->pci_dev
, proxy
->nvectors
,
1351 error_report("unable to init msix vectors to %" PRIu32
,
1353 proxy
->nvectors
= 0;
1356 proxy
->pci_dev
.config_write
= virtio_write_config
;
1359 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
)
1360 + virtio_bus_get_vdev_config_len(bus
);
1361 if (size
& (size
- 1)) {
1362 size
= 1 << qemu_fls(size
);
1365 memory_region_init_io(&proxy
->bar
, OBJECT(proxy
),
1366 &virtio_pci_config_ops
,
1367 proxy
, "virtio-pci", size
);
1369 pci_register_bar(&proxy
->pci_dev
, proxy
->legacy_io_bar
,
1370 PCI_BASE_ADDRESS_SPACE_IO
, &proxy
->bar
);
1373 if (!kvm_has_many_ioeventfds()) {
1374 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
1377 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_BAD_FEATURE
);
1380 static void virtio_pci_device_unplugged(DeviceState
*d
)
1382 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1384 virtio_pci_stop_ioeventfd(proxy
);
1387 static void virtio_pci_realize(PCIDevice
*pci_dev
, Error
**errp
)
1389 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
1390 VirtioPCIClass
*k
= VIRTIO_PCI_GET_CLASS(pci_dev
);
1393 * virtio pci bar layout used by default.
1394 * subclasses can re-arrange things if needed.
1396 * region 0 -- virtio legacy io bar
1397 * region 1 -- msi-x bar
1398 * region 4+5 -- virtio modern memory (64bit) bar
1401 proxy
->legacy_io_bar
= 0;
1402 proxy
->msix_bar
= 1;
1403 proxy
->modern_mem_bar
= 4;
1405 proxy
->common
.offset
= 0x0;
1406 proxy
->common
.size
= 0x1000;
1407 proxy
->common
.type
= VIRTIO_PCI_CAP_COMMON_CFG
;
1409 proxy
->isr
.offset
= 0x1000;
1410 proxy
->isr
.size
= 0x1000;
1411 proxy
->isr
.type
= VIRTIO_PCI_CAP_ISR_CFG
;
1413 proxy
->device
.offset
= 0x2000;
1414 proxy
->device
.size
= 0x1000;
1415 proxy
->device
.type
= VIRTIO_PCI_CAP_DEVICE_CFG
;
1417 proxy
->notify
.offset
= 0x3000;
1418 proxy
->notify
.size
=
1419 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
* VIRTIO_QUEUE_MAX
;
1420 proxy
->notify
.type
= VIRTIO_PCI_CAP_NOTIFY_CFG
;
1422 /* subclasses can enforce modern, so do this unconditionally */
1423 memory_region_init(&proxy
->modern_bar
, OBJECT(proxy
), "virtio-pci",
1424 2 * QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
*
1427 virtio_pci_bus_new(&proxy
->bus
, sizeof(proxy
->bus
), proxy
);
1429 k
->realize(proxy
, errp
);
1433 static void virtio_pci_exit(PCIDevice
*pci_dev
)
1435 msix_uninit_exclusive_bar(pci_dev
);
1438 static void virtio_pci_reset(DeviceState
*qdev
)
1440 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1441 VirtioBusState
*bus
= VIRTIO_BUS(&proxy
->bus
);
1442 virtio_pci_stop_ioeventfd(proxy
);
1443 virtio_bus_reset(bus
);
1444 msix_unuse_all_vectors(&proxy
->pci_dev
);
1447 static Property virtio_pci_properties
[] = {
1448 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy
, flags
,
1449 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT
, false),
1450 DEFINE_PROP_BIT("disable-legacy", VirtIOPCIProxy
, flags
,
1451 VIRTIO_PCI_FLAG_DISABLE_LEGACY_BIT
, false),
1452 DEFINE_PROP_BIT("disable-modern", VirtIOPCIProxy
, flags
,
1453 VIRTIO_PCI_FLAG_DISABLE_MODERN_BIT
, true),
1454 DEFINE_PROP_END_OF_LIST(),
1457 static void virtio_pci_class_init(ObjectClass
*klass
, void *data
)
1459 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1460 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1462 dc
->props
= virtio_pci_properties
;
1463 k
->realize
= virtio_pci_realize
;
1464 k
->exit
= virtio_pci_exit
;
1465 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1466 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1467 k
->class_id
= PCI_CLASS_OTHERS
;
1468 dc
->reset
= virtio_pci_reset
;
1471 static const TypeInfo virtio_pci_info
= {
1472 .name
= TYPE_VIRTIO_PCI
,
1473 .parent
= TYPE_PCI_DEVICE
,
1474 .instance_size
= sizeof(VirtIOPCIProxy
),
1475 .class_init
= virtio_pci_class_init
,
1476 .class_size
= sizeof(VirtioPCIClass
),
1480 /* virtio-blk-pci */
1482 static Property virtio_blk_pci_properties
[] = {
1483 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1484 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1485 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1486 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1487 DEFINE_PROP_END_OF_LIST(),
1490 static void virtio_blk_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1492 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(vpci_dev
);
1493 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1495 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1496 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1499 static void virtio_blk_pci_class_init(ObjectClass
*klass
, void *data
)
1501 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1502 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1503 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1505 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1506 dc
->props
= virtio_blk_pci_properties
;
1507 k
->realize
= virtio_blk_pci_realize
;
1508 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1509 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
1510 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1511 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1514 static void virtio_blk_pci_instance_init(Object
*obj
)
1516 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(obj
);
1518 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1520 object_property_add_alias(obj
, "iothread", OBJECT(&dev
->vdev
),"iothread",
1522 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
1523 "bootindex", &error_abort
);
1526 static const TypeInfo virtio_blk_pci_info
= {
1527 .name
= TYPE_VIRTIO_BLK_PCI
,
1528 .parent
= TYPE_VIRTIO_PCI
,
1529 .instance_size
= sizeof(VirtIOBlkPCI
),
1530 .instance_init
= virtio_blk_pci_instance_init
,
1531 .class_init
= virtio_blk_pci_class_init
,
1534 /* virtio-scsi-pci */
1536 static Property virtio_scsi_pci_properties
[] = {
1537 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1538 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1539 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1540 DEV_NVECTORS_UNSPECIFIED
),
1541 DEFINE_PROP_END_OF_LIST(),
1544 static void virtio_scsi_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1546 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(vpci_dev
);
1547 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1548 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1549 DeviceState
*proxy
= DEVICE(vpci_dev
);
1552 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1553 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1557 * For command line compatibility, this sets the virtio-scsi-device bus
1561 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1562 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1566 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1567 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1570 static void virtio_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1572 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1573 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1574 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1576 k
->realize
= virtio_scsi_pci_realize
;
1577 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1578 dc
->props
= virtio_scsi_pci_properties
;
1579 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1580 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1581 pcidev_k
->revision
= 0x00;
1582 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1585 static void virtio_scsi_pci_instance_init(Object
*obj
)
1587 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(obj
);
1589 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1591 object_property_add_alias(obj
, "iothread", OBJECT(&dev
->vdev
), "iothread",
1595 static const TypeInfo virtio_scsi_pci_info
= {
1596 .name
= TYPE_VIRTIO_SCSI_PCI
,
1597 .parent
= TYPE_VIRTIO_PCI
,
1598 .instance_size
= sizeof(VirtIOSCSIPCI
),
1599 .instance_init
= virtio_scsi_pci_instance_init
,
1600 .class_init
= virtio_scsi_pci_class_init
,
1603 /* vhost-scsi-pci */
1605 #ifdef CONFIG_VHOST_SCSI
1606 static Property vhost_scsi_pci_properties
[] = {
1607 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1608 DEV_NVECTORS_UNSPECIFIED
),
1609 DEFINE_PROP_END_OF_LIST(),
1612 static void vhost_scsi_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1614 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(vpci_dev
);
1615 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1616 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1618 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1619 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1622 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1623 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1626 static void vhost_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1628 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1629 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1630 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1631 k
->realize
= vhost_scsi_pci_realize
;
1632 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1633 dc
->props
= vhost_scsi_pci_properties
;
1634 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1635 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1636 pcidev_k
->revision
= 0x00;
1637 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1640 static void vhost_scsi_pci_instance_init(Object
*obj
)
1642 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(obj
);
1644 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1646 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
1647 "bootindex", &error_abort
);
1650 static const TypeInfo vhost_scsi_pci_info
= {
1651 .name
= TYPE_VHOST_SCSI_PCI
,
1652 .parent
= TYPE_VIRTIO_PCI
,
1653 .instance_size
= sizeof(VHostSCSIPCI
),
1654 .instance_init
= vhost_scsi_pci_instance_init
,
1655 .class_init
= vhost_scsi_pci_class_init
,
1659 /* virtio-balloon-pci */
1661 static Property virtio_balloon_pci_properties
[] = {
1662 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1663 DEFINE_PROP_END_OF_LIST(),
1666 static void virtio_balloon_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1668 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(vpci_dev
);
1669 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1671 if (vpci_dev
->class_code
!= PCI_CLASS_OTHERS
&&
1672 vpci_dev
->class_code
!= PCI_CLASS_MEMORY_RAM
) { /* qemu < 1.1 */
1673 vpci_dev
->class_code
= PCI_CLASS_OTHERS
;
1676 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1677 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1680 static void virtio_balloon_pci_class_init(ObjectClass
*klass
, void *data
)
1682 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1683 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1684 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1685 k
->realize
= virtio_balloon_pci_realize
;
1686 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1687 dc
->props
= virtio_balloon_pci_properties
;
1688 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1689 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BALLOON
;
1690 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1691 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
1694 static void virtio_balloon_pci_instance_init(Object
*obj
)
1696 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(obj
);
1698 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1699 TYPE_VIRTIO_BALLOON
);
1700 object_property_add_alias(obj
, "guest-stats", OBJECT(&dev
->vdev
),
1701 "guest-stats", &error_abort
);
1702 object_property_add_alias(obj
, "guest-stats-polling-interval",
1704 "guest-stats-polling-interval", &error_abort
);
1707 static const TypeInfo virtio_balloon_pci_info
= {
1708 .name
= TYPE_VIRTIO_BALLOON_PCI
,
1709 .parent
= TYPE_VIRTIO_PCI
,
1710 .instance_size
= sizeof(VirtIOBalloonPCI
),
1711 .instance_init
= virtio_balloon_pci_instance_init
,
1712 .class_init
= virtio_balloon_pci_class_init
,
1715 /* virtio-serial-pci */
1717 static void virtio_serial_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1719 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(vpci_dev
);
1720 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1721 DeviceState
*proxy
= DEVICE(vpci_dev
);
1724 if (vpci_dev
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
1725 vpci_dev
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
1726 vpci_dev
->class_code
!= PCI_CLASS_OTHERS
) { /* qemu-kvm */
1727 vpci_dev
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
1730 /* backwards-compatibility with machines that were created with
1731 DEV_NVECTORS_UNSPECIFIED */
1732 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1733 vpci_dev
->nvectors
= dev
->vdev
.serial
.max_virtserial_ports
+ 1;
1737 * For command line compatibility, this sets the virtio-serial-device bus
1741 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1742 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1746 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1747 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1750 static Property virtio_serial_pci_properties
[] = {
1751 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1752 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1753 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1754 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1755 DEFINE_PROP_END_OF_LIST(),
1758 static void virtio_serial_pci_class_init(ObjectClass
*klass
, void *data
)
1760 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1761 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1762 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1763 k
->realize
= virtio_serial_pci_realize
;
1764 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
1765 dc
->props
= virtio_serial_pci_properties
;
1766 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1767 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_CONSOLE
;
1768 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1769 pcidev_k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
1772 static void virtio_serial_pci_instance_init(Object
*obj
)
1774 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(obj
);
1776 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1777 TYPE_VIRTIO_SERIAL
);
1780 static const TypeInfo virtio_serial_pci_info
= {
1781 .name
= TYPE_VIRTIO_SERIAL_PCI
,
1782 .parent
= TYPE_VIRTIO_PCI
,
1783 .instance_size
= sizeof(VirtIOSerialPCI
),
1784 .instance_init
= virtio_serial_pci_instance_init
,
1785 .class_init
= virtio_serial_pci_class_init
,
1788 /* virtio-net-pci */
1790 static Property virtio_net_properties
[] = {
1791 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1792 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, false),
1793 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
1794 DEFINE_PROP_END_OF_LIST(),
1797 static void virtio_net_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1799 DeviceState
*qdev
= DEVICE(vpci_dev
);
1800 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(vpci_dev
);
1801 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1803 virtio_net_set_netclient_name(&dev
->vdev
, qdev
->id
,
1804 object_get_typename(OBJECT(qdev
)));
1805 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1806 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1809 static void virtio_net_pci_class_init(ObjectClass
*klass
, void *data
)
1811 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1812 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1813 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_CLASS(klass
);
1815 k
->romfile
= "efi-virtio.rom";
1816 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1817 k
->device_id
= PCI_DEVICE_ID_VIRTIO_NET
;
1818 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1819 k
->class_id
= PCI_CLASS_NETWORK_ETHERNET
;
1820 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
1821 dc
->props
= virtio_net_properties
;
1822 vpciklass
->realize
= virtio_net_pci_realize
;
1825 static void virtio_net_pci_instance_init(Object
*obj
)
1827 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(obj
);
1829 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1831 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
1832 "bootindex", &error_abort
);
1835 static const TypeInfo virtio_net_pci_info
= {
1836 .name
= TYPE_VIRTIO_NET_PCI
,
1837 .parent
= TYPE_VIRTIO_PCI
,
1838 .instance_size
= sizeof(VirtIONetPCI
),
1839 .instance_init
= virtio_net_pci_instance_init
,
1840 .class_init
= virtio_net_pci_class_init
,
1843 /* virtio-rng-pci */
1845 static Property virtio_rng_pci_properties
[] = {
1846 DEFINE_PROP_END_OF_LIST(),
1849 static void virtio_rng_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1851 VirtIORngPCI
*vrng
= VIRTIO_RNG_PCI(vpci_dev
);
1852 DeviceState
*vdev
= DEVICE(&vrng
->vdev
);
1855 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1856 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1858 error_propagate(errp
, err
);
1862 object_property_set_link(OBJECT(vrng
),
1863 OBJECT(vrng
->vdev
.conf
.rng
), "rng",
1867 static void virtio_rng_pci_class_init(ObjectClass
*klass
, void *data
)
1869 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1870 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1871 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1873 k
->realize
= virtio_rng_pci_realize
;
1874 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1875 dc
->props
= virtio_rng_pci_properties
;
1877 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1878 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_RNG
;
1879 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1880 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
1883 static void virtio_rng_initfn(Object
*obj
)
1885 VirtIORngPCI
*dev
= VIRTIO_RNG_PCI(obj
);
1887 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1889 object_property_add_alias(obj
, "rng", OBJECT(&dev
->vdev
), "rng",
1893 static const TypeInfo virtio_rng_pci_info
= {
1894 .name
= TYPE_VIRTIO_RNG_PCI
,
1895 .parent
= TYPE_VIRTIO_PCI
,
1896 .instance_size
= sizeof(VirtIORngPCI
),
1897 .instance_init
= virtio_rng_initfn
,
1898 .class_init
= virtio_rng_pci_class_init
,
1901 /* virtio-input-pci */
1903 static Property virtio_input_hid_pci_properties
[] = {
1904 DEFINE_VIRTIO_INPUT_PROPERTIES(VirtIOInputPCI
, vdev
.input
),
1905 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1906 DEFINE_PROP_END_OF_LIST(),
1909 static void virtio_input_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1911 VirtIOInputPCI
*vinput
= VIRTIO_INPUT_PCI(vpci_dev
);
1912 DeviceState
*vdev
= DEVICE(&vinput
->vdev
);
1914 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1915 /* force virtio-1.0 */
1916 vpci_dev
->flags
&= ~VIRTIO_PCI_FLAG_DISABLE_MODERN
;
1917 vpci_dev
->flags
|= VIRTIO_PCI_FLAG_DISABLE_LEGACY
;
1918 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1921 static void virtio_input_pci_class_init(ObjectClass
*klass
, void *data
)
1923 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1924 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1925 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1927 k
->realize
= virtio_input_pci_realize
;
1928 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
1930 pcidev_k
->class_id
= PCI_CLASS_INPUT_OTHER
;
1933 static void virtio_input_hid_pci_class_init(ObjectClass
*klass
, void *data
)
1935 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1937 dc
->props
= virtio_input_hid_pci_properties
;
1940 static void virtio_input_hid_kbd_pci_class_init(ObjectClass
*klass
, void *data
)
1942 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1944 pcidev_k
->class_id
= PCI_CLASS_INPUT_KEYBOARD
;
1947 static void virtio_input_hid_mouse_pci_class_init(ObjectClass
*klass
,
1950 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1952 pcidev_k
->class_id
= PCI_CLASS_INPUT_MOUSE
;
1955 static void virtio_keyboard_initfn(Object
*obj
)
1957 VirtIOInputHIDPCI
*dev
= VIRTIO_INPUT_HID_PCI(obj
);
1958 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_KEYBOARD
);
1959 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1962 static void virtio_mouse_initfn(Object
*obj
)
1964 VirtIOInputHIDPCI
*dev
= VIRTIO_INPUT_HID_PCI(obj
);
1965 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_MOUSE
);
1966 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1969 static void virtio_tablet_initfn(Object
*obj
)
1971 VirtIOInputHIDPCI
*dev
= VIRTIO_INPUT_HID_PCI(obj
);
1972 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_TABLET
);
1973 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1976 static const TypeInfo virtio_input_pci_info
= {
1977 .name
= TYPE_VIRTIO_INPUT_PCI
,
1978 .parent
= TYPE_VIRTIO_PCI
,
1979 .instance_size
= sizeof(VirtIOInputPCI
),
1980 .class_init
= virtio_input_pci_class_init
,
1984 static const TypeInfo virtio_input_hid_pci_info
= {
1985 .name
= TYPE_VIRTIO_INPUT_HID_PCI
,
1986 .parent
= TYPE_VIRTIO_INPUT_PCI
,
1987 .instance_size
= sizeof(VirtIOInputHIDPCI
),
1988 .class_init
= virtio_input_hid_pci_class_init
,
1992 static const TypeInfo virtio_keyboard_pci_info
= {
1993 .name
= TYPE_VIRTIO_KEYBOARD_PCI
,
1994 .parent
= TYPE_VIRTIO_INPUT_HID_PCI
,
1995 .class_init
= virtio_input_hid_kbd_pci_class_init
,
1996 .instance_size
= sizeof(VirtIOInputHIDPCI
),
1997 .instance_init
= virtio_keyboard_initfn
,
2000 static const TypeInfo virtio_mouse_pci_info
= {
2001 .name
= TYPE_VIRTIO_MOUSE_PCI
,
2002 .parent
= TYPE_VIRTIO_INPUT_HID_PCI
,
2003 .class_init
= virtio_input_hid_mouse_pci_class_init
,
2004 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2005 .instance_init
= virtio_mouse_initfn
,
2008 static const TypeInfo virtio_tablet_pci_info
= {
2009 .name
= TYPE_VIRTIO_TABLET_PCI
,
2010 .parent
= TYPE_VIRTIO_INPUT_HID_PCI
,
2011 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2012 .instance_init
= virtio_tablet_initfn
,
2015 /* virtio-pci-bus */
2017 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
2018 VirtIOPCIProxy
*dev
)
2020 DeviceState
*qdev
= DEVICE(dev
);
2021 char virtio_bus_name
[] = "virtio-bus";
2023 qbus_create_inplace(bus
, bus_size
, TYPE_VIRTIO_PCI_BUS
, qdev
,
2027 static void virtio_pci_bus_class_init(ObjectClass
*klass
, void *data
)
2029 BusClass
*bus_class
= BUS_CLASS(klass
);
2030 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
2031 bus_class
->max_dev
= 1;
2032 k
->notify
= virtio_pci_notify
;
2033 k
->save_config
= virtio_pci_save_config
;
2034 k
->load_config
= virtio_pci_load_config
;
2035 k
->save_queue
= virtio_pci_save_queue
;
2036 k
->load_queue
= virtio_pci_load_queue
;
2037 k
->query_guest_notifiers
= virtio_pci_query_guest_notifiers
;
2038 k
->set_host_notifier
= virtio_pci_set_host_notifier
;
2039 k
->set_guest_notifiers
= virtio_pci_set_guest_notifiers
;
2040 k
->vmstate_change
= virtio_pci_vmstate_change
;
2041 k
->device_plugged
= virtio_pci_device_plugged
;
2042 k
->device_unplugged
= virtio_pci_device_unplugged
;
2043 k
->query_nvectors
= virtio_pci_query_nvectors
;
2046 static const TypeInfo virtio_pci_bus_info
= {
2047 .name
= TYPE_VIRTIO_PCI_BUS
,
2048 .parent
= TYPE_VIRTIO_BUS
,
2049 .instance_size
= sizeof(VirtioPCIBusState
),
2050 .class_init
= virtio_pci_bus_class_init
,
2053 static void virtio_pci_register_types(void)
2055 type_register_static(&virtio_rng_pci_info
);
2056 type_register_static(&virtio_input_pci_info
);
2057 type_register_static(&virtio_input_hid_pci_info
);
2058 type_register_static(&virtio_keyboard_pci_info
);
2059 type_register_static(&virtio_mouse_pci_info
);
2060 type_register_static(&virtio_tablet_pci_info
);
2061 type_register_static(&virtio_pci_bus_info
);
2062 type_register_static(&virtio_pci_info
);
2063 #ifdef CONFIG_VIRTFS
2064 type_register_static(&virtio_9p_pci_info
);
2066 type_register_static(&virtio_blk_pci_info
);
2067 type_register_static(&virtio_scsi_pci_info
);
2068 type_register_static(&virtio_balloon_pci_info
);
2069 type_register_static(&virtio_serial_pci_info
);
2070 type_register_static(&virtio_net_pci_info
);
2071 #ifdef CONFIG_VHOST_SCSI
2072 type_register_static(&vhost_scsi_pci_info
);
2076 type_init(virtio_pci_register_types
)