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 /* Below are generic functions to do memcpy from/to an address space,
447 * without byteswaps, with input validation.
449 * As regular address_space_* APIs all do some kind of byteswap at least for
450 * some host/target combinations, we are forced to explicitly convert to a
451 * known-endianness integer value.
452 * It doesn't really matter which endian format to go through, so the code
453 * below selects the endian that causes the least amount of work on the given
456 * Note: host pointer must be aligned.
459 void virtio_address_space_write(AddressSpace
*as
, hwaddr addr
,
460 const uint8_t *buf
, int len
)
464 /* address_space_* APIs assume an aligned address.
465 * As address is under guest control, handle illegal values.
469 /* Make sure caller aligned buf properly */
470 assert(!(((uintptr_t)buf
) & (len
- 1)));
474 val
= pci_get_byte(buf
);
475 address_space_stb(as
, addr
, val
, MEMTXATTRS_UNSPECIFIED
, NULL
);
478 val
= pci_get_word(buf
);
479 address_space_stw_le(as
, addr
, val
, MEMTXATTRS_UNSPECIFIED
, NULL
);
482 val
= pci_get_long(buf
);
483 address_space_stl_le(as
, addr
, val
, MEMTXATTRS_UNSPECIFIED
, NULL
);
486 /* As length is under guest control, handle illegal values. */
492 virtio_address_space_read(AddressSpace
*as
, hwaddr addr
, uint8_t *buf
, int len
)
496 /* address_space_* APIs assume an aligned address.
497 * As address is under guest control, handle illegal values.
501 /* Make sure caller aligned buf properly */
502 assert(!(((uintptr_t)buf
) & (len
- 1)));
506 val
= address_space_ldub(as
, addr
, MEMTXATTRS_UNSPECIFIED
, NULL
);
507 pci_set_byte(buf
, val
);
510 val
= address_space_lduw_le(as
, addr
, MEMTXATTRS_UNSPECIFIED
, NULL
);
511 pci_set_word(buf
, val
);
514 val
= address_space_ldl_le(as
, addr
, MEMTXATTRS_UNSPECIFIED
, NULL
);
515 pci_set_long(buf
, val
);
518 /* As length is under guest control, handle illegal values. */
523 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
524 uint32_t val
, int len
)
526 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
527 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
528 struct virtio_pci_cfg_cap
*cfg
;
530 pci_default_write_config(pci_dev
, address
, val
, len
);
532 if (range_covers_byte(address
, len
, PCI_COMMAND
) &&
533 !(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
534 virtio_pci_stop_ioeventfd(proxy
);
535 virtio_set_status(vdev
, vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
538 if (proxy
->config_cap
&&
539 ranges_overlap(address
, len
, proxy
->config_cap
+ offsetof(struct virtio_pci_cfg_cap
,
541 sizeof cfg
->pci_cfg_data
)) {
545 cfg
= (void *)(proxy
->pci_dev
.config
+ proxy
->config_cap
);
546 off
= le32_to_cpu(cfg
->cap
.offset
);
547 len
= le32_to_cpu(cfg
->cap
.length
);
549 if (len
== 1 || len
== 2 || len
== 4) {
550 assert(len
<= sizeof cfg
->pci_cfg_data
);
551 virtio_address_space_write(&proxy
->modern_as
, off
,
552 cfg
->pci_cfg_data
, len
);
557 static uint32_t virtio_read_config(PCIDevice
*pci_dev
,
558 uint32_t address
, int len
)
560 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
561 struct virtio_pci_cfg_cap
*cfg
;
563 if (proxy
->config_cap
&&
564 ranges_overlap(address
, len
, proxy
->config_cap
+ offsetof(struct virtio_pci_cfg_cap
,
566 sizeof cfg
->pci_cfg_data
)) {
570 cfg
= (void *)(proxy
->pci_dev
.config
+ proxy
->config_cap
);
571 off
= le32_to_cpu(cfg
->cap
.offset
);
572 len
= le32_to_cpu(cfg
->cap
.length
);
574 if (len
== 1 || len
== 2 || len
== 4) {
575 assert(len
<= sizeof cfg
->pci_cfg_data
);
576 virtio_address_space_read(&proxy
->modern_as
, off
,
577 cfg
->pci_cfg_data
, len
);
581 return pci_default_read_config(pci_dev
, address
, len
);
584 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy
*proxy
,
585 unsigned int queue_no
,
589 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
592 if (irqfd
->users
== 0) {
593 ret
= kvm_irqchip_add_msi_route(kvm_state
, msg
, &proxy
->pci_dev
);
603 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy
*proxy
,
606 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
607 if (--irqfd
->users
== 0) {
608 kvm_irqchip_release_virq(kvm_state
, irqfd
->virq
);
612 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy
*proxy
,
613 unsigned int queue_no
,
616 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
617 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
618 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
619 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
621 ret
= kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, n
, NULL
, irqfd
->virq
);
625 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy
*proxy
,
626 unsigned int queue_no
,
629 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
630 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
631 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
632 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
635 ret
= kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state
, n
, irqfd
->virq
);
639 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy
*proxy
, int nvqs
)
641 PCIDevice
*dev
= &proxy
->pci_dev
;
642 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
643 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
648 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
649 if (!virtio_queue_get_num(vdev
, queue_no
)) {
652 vector
= virtio_queue_vector(vdev
, queue_no
);
653 if (vector
>= msix_nr_vectors_allocated(dev
)) {
656 msg
= msix_get_message(dev
, vector
);
657 ret
= kvm_virtio_pci_vq_vector_use(proxy
, queue_no
, vector
, msg
);
661 /* If guest supports masking, set up irqfd now.
662 * Otherwise, delay until unmasked in the frontend.
664 if (k
->guest_notifier_mask
) {
665 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
667 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
675 while (--queue_no
>= 0) {
676 vector
= virtio_queue_vector(vdev
, queue_no
);
677 if (vector
>= msix_nr_vectors_allocated(dev
)) {
680 if (k
->guest_notifier_mask
) {
681 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
683 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
688 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy
*proxy
, int nvqs
)
690 PCIDevice
*dev
= &proxy
->pci_dev
;
691 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
694 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
696 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
697 if (!virtio_queue_get_num(vdev
, queue_no
)) {
700 vector
= virtio_queue_vector(vdev
, queue_no
);
701 if (vector
>= msix_nr_vectors_allocated(dev
)) {
704 /* If guest supports masking, clean up irqfd now.
705 * Otherwise, it was cleaned when masked in the frontend.
707 if (k
->guest_notifier_mask
) {
708 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
710 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
714 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy
*proxy
,
715 unsigned int queue_no
,
719 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
720 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
721 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
722 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
726 if (proxy
->vector_irqfd
) {
727 irqfd
= &proxy
->vector_irqfd
[vector
];
728 if (irqfd
->msg
.data
!= msg
.data
|| irqfd
->msg
.address
!= msg
.address
) {
729 ret
= kvm_irqchip_update_msi_route(kvm_state
, irqfd
->virq
, msg
,
737 /* If guest supports masking, irqfd is already setup, unmask it.
738 * Otherwise, set it up now.
740 if (k
->guest_notifier_mask
) {
741 k
->guest_notifier_mask(vdev
, queue_no
, false);
742 /* Test after unmasking to avoid losing events. */
743 if (k
->guest_notifier_pending
&&
744 k
->guest_notifier_pending(vdev
, queue_no
)) {
745 event_notifier_set(n
);
748 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
753 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy
*proxy
,
754 unsigned int queue_no
,
757 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
758 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
760 /* If guest supports masking, keep irqfd but mask it.
761 * Otherwise, clean it up now.
763 if (k
->guest_notifier_mask
) {
764 k
->guest_notifier_mask(vdev
, queue_no
, true);
766 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
770 static int virtio_pci_vector_unmask(PCIDevice
*dev
, unsigned vector
,
773 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
774 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
775 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
776 int ret
, index
, unmasked
= 0;
779 index
= virtio_get_queue_index(vq
);
780 if (!virtio_queue_get_num(vdev
, index
)) {
783 if (index
< proxy
->nvqs_with_notifiers
) {
784 ret
= virtio_pci_vq_vector_unmask(proxy
, index
, vector
, msg
);
790 vq
= virtio_vector_next_queue(vq
);
796 vq
= virtio_vector_first_queue(vdev
, vector
);
797 while (vq
&& unmasked
>= 0) {
798 index
= virtio_get_queue_index(vq
);
799 if (index
< proxy
->nvqs_with_notifiers
) {
800 virtio_pci_vq_vector_mask(proxy
, index
, vector
);
803 vq
= virtio_vector_next_queue(vq
);
808 static void virtio_pci_vector_mask(PCIDevice
*dev
, unsigned vector
)
810 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
811 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
812 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
816 index
= virtio_get_queue_index(vq
);
817 if (!virtio_queue_get_num(vdev
, index
)) {
820 if (index
< proxy
->nvqs_with_notifiers
) {
821 virtio_pci_vq_vector_mask(proxy
, index
, vector
);
823 vq
= virtio_vector_next_queue(vq
);
827 static void virtio_pci_vector_poll(PCIDevice
*dev
,
828 unsigned int vector_start
,
829 unsigned int vector_end
)
831 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
832 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
833 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
836 EventNotifier
*notifier
;
839 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
840 if (!virtio_queue_get_num(vdev
, queue_no
)) {
843 vector
= virtio_queue_vector(vdev
, queue_no
);
844 if (vector
< vector_start
|| vector
>= vector_end
||
845 !msix_is_masked(dev
, vector
)) {
848 vq
= virtio_get_queue(vdev
, queue_no
);
849 notifier
= virtio_queue_get_guest_notifier(vq
);
850 if (k
->guest_notifier_pending
) {
851 if (k
->guest_notifier_pending(vdev
, queue_no
)) {
852 msix_set_pending(dev
, vector
);
854 } else if (event_notifier_test_and_clear(notifier
)) {
855 msix_set_pending(dev
, vector
);
860 static int virtio_pci_set_guest_notifier(DeviceState
*d
, int n
, bool assign
,
863 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
864 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
865 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
866 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
867 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
870 int r
= event_notifier_init(notifier
, 0);
874 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
876 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
877 event_notifier_cleanup(notifier
);
880 if (!msix_enabled(&proxy
->pci_dev
) && vdc
->guest_notifier_mask
) {
881 vdc
->guest_notifier_mask(vdev
, n
, !assign
);
887 static bool virtio_pci_query_guest_notifiers(DeviceState
*d
)
889 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
890 return msix_enabled(&proxy
->pci_dev
);
893 static int virtio_pci_set_guest_notifiers(DeviceState
*d
, int nvqs
, bool assign
)
895 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
896 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
897 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
899 bool with_irqfd
= msix_enabled(&proxy
->pci_dev
) &&
900 kvm_msi_via_irqfd_enabled();
902 nvqs
= MIN(nvqs
, VIRTIO_QUEUE_MAX
);
904 /* When deassigning, pass a consistent nvqs value
905 * to avoid leaking notifiers.
907 assert(assign
|| nvqs
== proxy
->nvqs_with_notifiers
);
909 proxy
->nvqs_with_notifiers
= nvqs
;
911 /* Must unset vector notifier while guest notifier is still assigned */
912 if ((proxy
->vector_irqfd
|| k
->guest_notifier_mask
) && !assign
) {
913 msix_unset_vector_notifiers(&proxy
->pci_dev
);
914 if (proxy
->vector_irqfd
) {
915 kvm_virtio_pci_vector_release(proxy
, nvqs
);
916 g_free(proxy
->vector_irqfd
);
917 proxy
->vector_irqfd
= NULL
;
921 for (n
= 0; n
< nvqs
; n
++) {
922 if (!virtio_queue_get_num(vdev
, n
)) {
926 r
= virtio_pci_set_guest_notifier(d
, n
, assign
, with_irqfd
);
932 /* Must set vector notifier after guest notifier has been assigned */
933 if ((with_irqfd
|| k
->guest_notifier_mask
) && assign
) {
935 proxy
->vector_irqfd
=
936 g_malloc0(sizeof(*proxy
->vector_irqfd
) *
937 msix_nr_vectors_allocated(&proxy
->pci_dev
));
938 r
= kvm_virtio_pci_vector_use(proxy
, nvqs
);
943 r
= msix_set_vector_notifiers(&proxy
->pci_dev
,
944 virtio_pci_vector_unmask
,
945 virtio_pci_vector_mask
,
946 virtio_pci_vector_poll
);
948 goto notifiers_error
;
957 kvm_virtio_pci_vector_release(proxy
, nvqs
);
961 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
964 virtio_pci_set_guest_notifier(d
, n
, !assign
, with_irqfd
);
969 static int virtio_pci_set_host_notifier(DeviceState
*d
, int n
, bool assign
)
971 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
973 /* Stop using ioeventfd for virtqueue kick if the device starts using host
974 * notifiers. This makes it easy to avoid stepping on each others' toes.
976 proxy
->ioeventfd_disabled
= assign
;
978 virtio_pci_stop_ioeventfd(proxy
);
980 /* We don't need to start here: it's not needed because backend
981 * currently only stops on status change away from ok,
982 * reset, vmstop and such. If we do add code to start here,
983 * need to check vmstate, device state etc. */
984 return virtio_pci_set_host_notifier_internal(proxy
, n
, assign
, false);
987 static void virtio_pci_vmstate_change(DeviceState
*d
, bool running
)
989 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
990 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
993 /* Old QEMU versions did not set bus master enable on status write.
994 * Detect DRIVER set and enable it.
996 if ((proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION
) &&
997 (vdev
->status
& VIRTIO_CONFIG_S_DRIVER
) &&
998 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
999 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
1000 proxy
->pci_dev
.config
[PCI_COMMAND
] |
1001 PCI_COMMAND_MASTER
, 1);
1003 virtio_pci_start_ioeventfd(proxy
);
1005 virtio_pci_stop_ioeventfd(proxy
);
1009 #ifdef CONFIG_VIRTFS
1010 static void virtio_9p_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1012 V9fsPCIState
*dev
= VIRTIO_9P_PCI(vpci_dev
);
1013 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1015 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1016 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1019 static Property virtio_9p_pci_properties
[] = {
1020 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1021 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1022 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1023 DEFINE_PROP_END_OF_LIST(),
1026 static void virtio_9p_pci_class_init(ObjectClass
*klass
, void *data
)
1028 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1029 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1030 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1032 k
->realize
= virtio_9p_pci_realize
;
1033 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1034 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_9P
;
1035 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1036 pcidev_k
->class_id
= 0x2;
1037 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1038 dc
->props
= virtio_9p_pci_properties
;
1041 static void virtio_9p_pci_instance_init(Object
*obj
)
1043 V9fsPCIState
*dev
= VIRTIO_9P_PCI(obj
);
1045 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1049 static const TypeInfo virtio_9p_pci_info
= {
1050 .name
= TYPE_VIRTIO_9P_PCI
,
1051 .parent
= TYPE_VIRTIO_PCI
,
1052 .instance_size
= sizeof(V9fsPCIState
),
1053 .instance_init
= virtio_9p_pci_instance_init
,
1054 .class_init
= virtio_9p_pci_class_init
,
1056 #endif /* CONFIG_VIRTFS */
1059 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
1062 static int virtio_pci_query_nvectors(DeviceState
*d
)
1064 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1066 return proxy
->nvectors
;
1069 static int virtio_pci_add_mem_cap(VirtIOPCIProxy
*proxy
,
1070 struct virtio_pci_cap
*cap
)
1072 PCIDevice
*dev
= &proxy
->pci_dev
;
1075 offset
= pci_add_capability(dev
, PCI_CAP_ID_VNDR
, 0, cap
->cap_len
);
1078 assert(cap
->cap_len
>= sizeof *cap
);
1079 memcpy(dev
->config
+ offset
+ PCI_CAP_FLAGS
, &cap
->cap_len
,
1080 cap
->cap_len
- PCI_CAP_FLAGS
);
1085 static uint64_t virtio_pci_common_read(void *opaque
, hwaddr addr
,
1088 VirtIOPCIProxy
*proxy
= opaque
;
1089 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1094 case VIRTIO_PCI_COMMON_DFSELECT
:
1095 val
= proxy
->dfselect
;
1097 case VIRTIO_PCI_COMMON_DF
:
1098 if (proxy
->dfselect
<= 1) {
1099 val
= (vdev
->host_features
& ~VIRTIO_LEGACY_FEATURES
) >>
1100 (32 * proxy
->dfselect
);
1103 case VIRTIO_PCI_COMMON_GFSELECT
:
1104 val
= proxy
->gfselect
;
1106 case VIRTIO_PCI_COMMON_GF
:
1107 if (proxy
->gfselect
< ARRAY_SIZE(proxy
->guest_features
)) {
1108 val
= proxy
->guest_features
[proxy
->gfselect
];
1111 case VIRTIO_PCI_COMMON_MSIX
:
1112 val
= vdev
->config_vector
;
1114 case VIRTIO_PCI_COMMON_NUMQ
:
1115 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; ++i
) {
1116 if (virtio_queue_get_num(vdev
, i
)) {
1121 case VIRTIO_PCI_COMMON_STATUS
:
1124 case VIRTIO_PCI_COMMON_CFGGENERATION
:
1125 val
= vdev
->generation
;
1127 case VIRTIO_PCI_COMMON_Q_SELECT
:
1128 val
= vdev
->queue_sel
;
1130 case VIRTIO_PCI_COMMON_Q_SIZE
:
1131 val
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
1133 case VIRTIO_PCI_COMMON_Q_MSIX
:
1134 val
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
1136 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1137 val
= proxy
->vqs
[vdev
->queue_sel
].enabled
;
1139 case VIRTIO_PCI_COMMON_Q_NOFF
:
1140 /* Simply map queues in order */
1141 val
= vdev
->queue_sel
;
1143 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1144 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[0];
1146 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1147 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[1];
1149 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1150 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[0];
1152 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1153 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[1];
1155 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1156 val
= proxy
->vqs
[vdev
->queue_sel
].used
[0];
1158 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1159 val
= proxy
->vqs
[vdev
->queue_sel
].used
[1];
1168 static void virtio_pci_common_write(void *opaque
, hwaddr addr
,
1169 uint64_t val
, unsigned size
)
1171 VirtIOPCIProxy
*proxy
= opaque
;
1172 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1175 case VIRTIO_PCI_COMMON_DFSELECT
:
1176 proxy
->dfselect
= val
;
1178 case VIRTIO_PCI_COMMON_GFSELECT
:
1179 proxy
->gfselect
= val
;
1181 case VIRTIO_PCI_COMMON_GF
:
1182 if (proxy
->gfselect
< ARRAY_SIZE(proxy
->guest_features
)) {
1183 proxy
->guest_features
[proxy
->gfselect
] = val
;
1184 virtio_set_features(vdev
,
1185 (((uint64_t)proxy
->guest_features
[1]) << 32) |
1186 proxy
->guest_features
[0]);
1189 case VIRTIO_PCI_COMMON_MSIX
:
1190 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
1191 /* Make it possible for guest to discover an error took place. */
1192 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0) {
1193 val
= VIRTIO_NO_VECTOR
;
1195 vdev
->config_vector
= val
;
1197 case VIRTIO_PCI_COMMON_STATUS
:
1198 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
1199 virtio_pci_stop_ioeventfd(proxy
);
1202 virtio_set_status(vdev
, val
& 0xFF);
1204 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
1205 virtio_pci_start_ioeventfd(proxy
);
1208 if (vdev
->status
== 0) {
1210 msix_unuse_all_vectors(&proxy
->pci_dev
);
1214 case VIRTIO_PCI_COMMON_Q_SELECT
:
1215 if (val
< VIRTIO_QUEUE_MAX
) {
1216 vdev
->queue_sel
= val
;
1219 case VIRTIO_PCI_COMMON_Q_SIZE
:
1220 proxy
->vqs
[vdev
->queue_sel
].num
= val
;
1222 case VIRTIO_PCI_COMMON_Q_MSIX
:
1223 msix_vector_unuse(&proxy
->pci_dev
,
1224 virtio_queue_vector(vdev
, vdev
->queue_sel
));
1225 /* Make it possible for guest to discover an error took place. */
1226 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0) {
1227 val
= VIRTIO_NO_VECTOR
;
1229 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
1231 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1232 /* TODO: need a way to put num back on reset. */
1233 virtio_queue_set_num(vdev
, vdev
->queue_sel
,
1234 proxy
->vqs
[vdev
->queue_sel
].num
);
1235 virtio_queue_set_rings(vdev
, vdev
->queue_sel
,
1236 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].desc
[1]) << 32 |
1237 proxy
->vqs
[vdev
->queue_sel
].desc
[0],
1238 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].avail
[1]) << 32 |
1239 proxy
->vqs
[vdev
->queue_sel
].avail
[0],
1240 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].used
[1]) << 32 |
1241 proxy
->vqs
[vdev
->queue_sel
].used
[0]);
1243 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1244 proxy
->vqs
[vdev
->queue_sel
].desc
[0] = val
;
1246 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1247 proxy
->vqs
[vdev
->queue_sel
].desc
[1] = val
;
1249 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1250 proxy
->vqs
[vdev
->queue_sel
].avail
[0] = val
;
1252 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1253 proxy
->vqs
[vdev
->queue_sel
].avail
[1] = val
;
1255 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1256 proxy
->vqs
[vdev
->queue_sel
].used
[0] = val
;
1258 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1259 proxy
->vqs
[vdev
->queue_sel
].used
[1] = val
;
1267 static uint64_t virtio_pci_notify_read(void *opaque
, hwaddr addr
,
1273 static void virtio_pci_notify_write(void *opaque
, hwaddr addr
,
1274 uint64_t val
, unsigned size
)
1276 VirtIODevice
*vdev
= opaque
;
1277 unsigned queue
= addr
/ QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
;
1279 if (queue
< VIRTIO_QUEUE_MAX
) {
1280 virtio_queue_notify(vdev
, queue
);
1284 static uint64_t virtio_pci_isr_read(void *opaque
, hwaddr addr
,
1287 VirtIOPCIProxy
*proxy
= opaque
;
1288 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1289 uint64_t val
= vdev
->isr
;
1292 pci_irq_deassert(&proxy
->pci_dev
);
1297 static void virtio_pci_isr_write(void *opaque
, hwaddr addr
,
1298 uint64_t val
, unsigned size
)
1302 static uint64_t virtio_pci_device_read(void *opaque
, hwaddr addr
,
1305 VirtIODevice
*vdev
= opaque
;
1310 val
= virtio_config_modern_readb(vdev
, addr
);
1313 val
= virtio_config_modern_readw(vdev
, addr
);
1316 val
= virtio_config_modern_readl(vdev
, addr
);
1322 static void virtio_pci_device_write(void *opaque
, hwaddr addr
,
1323 uint64_t val
, unsigned size
)
1325 VirtIODevice
*vdev
= opaque
;
1328 virtio_config_modern_writeb(vdev
, addr
, val
);
1331 virtio_config_modern_writew(vdev
, addr
, val
);
1334 virtio_config_modern_writel(vdev
, addr
, val
);
1339 static void virtio_pci_modern_regions_init(VirtIOPCIProxy
*proxy
)
1341 static const MemoryRegionOps common_ops
= {
1342 .read
= virtio_pci_common_read
,
1343 .write
= virtio_pci_common_write
,
1345 .min_access_size
= 1,
1346 .max_access_size
= 4,
1348 .endianness
= DEVICE_LITTLE_ENDIAN
,
1350 static const MemoryRegionOps isr_ops
= {
1351 .read
= virtio_pci_isr_read
,
1352 .write
= virtio_pci_isr_write
,
1354 .min_access_size
= 1,
1355 .max_access_size
= 4,
1357 .endianness
= DEVICE_LITTLE_ENDIAN
,
1359 static const MemoryRegionOps device_ops
= {
1360 .read
= virtio_pci_device_read
,
1361 .write
= virtio_pci_device_write
,
1363 .min_access_size
= 1,
1364 .max_access_size
= 4,
1366 .endianness
= DEVICE_LITTLE_ENDIAN
,
1368 static const MemoryRegionOps notify_ops
= {
1369 .read
= virtio_pci_notify_read
,
1370 .write
= virtio_pci_notify_write
,
1372 .min_access_size
= 1,
1373 .max_access_size
= 4,
1375 .endianness
= DEVICE_LITTLE_ENDIAN
,
1378 memory_region_init_io(&proxy
->common
.mr
, OBJECT(proxy
),
1381 "virtio-pci-common",
1382 proxy
->common
.size
);
1384 memory_region_init_io(&proxy
->isr
.mr
, OBJECT(proxy
),
1390 memory_region_init_io(&proxy
->device
.mr
, OBJECT(proxy
),
1392 virtio_bus_get_device(&proxy
->bus
),
1393 "virtio-pci-device",
1394 proxy
->device
.size
);
1396 memory_region_init_io(&proxy
->notify
.mr
, OBJECT(proxy
),
1398 virtio_bus_get_device(&proxy
->bus
),
1399 "virtio-pci-notify",
1400 proxy
->notify
.size
);
1403 static void virtio_pci_modern_region_map(VirtIOPCIProxy
*proxy
,
1404 VirtIOPCIRegion
*region
,
1405 struct virtio_pci_cap
*cap
)
1407 memory_region_add_subregion(&proxy
->modern_bar
,
1411 cap
->cfg_type
= region
->type
;
1412 cap
->bar
= proxy
->modern_mem_bar
;
1413 cap
->offset
= cpu_to_le32(region
->offset
);
1414 cap
->length
= cpu_to_le32(region
->size
);
1415 virtio_pci_add_mem_cap(proxy
, cap
);
1418 static void virtio_pci_modern_region_unmap(VirtIOPCIProxy
*proxy
,
1419 VirtIOPCIRegion
*region
)
1421 memory_region_del_subregion(&proxy
->modern_bar
,
1425 /* This is called by virtio-bus just after the device is plugged. */
1426 static void virtio_pci_device_plugged(DeviceState
*d
, Error
**errp
)
1428 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1429 VirtioBusState
*bus
= &proxy
->bus
;
1430 bool legacy
= !(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_LEGACY
);
1431 bool modern
= !(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_MODERN
);
1434 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1436 config
= proxy
->pci_dev
.config
;
1437 if (proxy
->class_code
) {
1438 pci_config_set_class(config
, proxy
->class_code
);
1442 /* legacy and transitional */
1443 pci_set_word(config
+ PCI_SUBSYSTEM_VENDOR_ID
,
1444 pci_get_word(config
+ PCI_VENDOR_ID
));
1445 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, virtio_bus_get_vdev_id(bus
));
1447 /* pure virtio-1.0 */
1448 pci_set_word(config
+ PCI_VENDOR_ID
,
1449 PCI_VENDOR_ID_REDHAT_QUMRANET
);
1450 pci_set_word(config
+ PCI_DEVICE_ID
,
1451 0x1040 + virtio_bus_get_vdev_id(bus
));
1452 pci_config_set_revision(config
, 1);
1454 config
[PCI_INTERRUPT_PIN
] = 1;
1458 struct virtio_pci_cap cap
= {
1459 .cap_len
= sizeof cap
,
1461 struct virtio_pci_notify_cap notify
= {
1462 .cap
.cap_len
= sizeof notify
,
1463 .notify_off_multiplier
=
1464 cpu_to_le32(QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
),
1466 struct virtio_pci_cfg_cap cfg
= {
1467 .cap
.cap_len
= sizeof cfg
,
1468 .cap
.cfg_type
= VIRTIO_PCI_CAP_PCI_CFG
,
1470 struct virtio_pci_cfg_cap
*cfg_mask
;
1472 /* TODO: add io access for speed */
1474 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_VERSION_1
);
1475 virtio_pci_modern_regions_init(proxy
);
1476 virtio_pci_modern_region_map(proxy
, &proxy
->common
, &cap
);
1477 virtio_pci_modern_region_map(proxy
, &proxy
->isr
, &cap
);
1478 virtio_pci_modern_region_map(proxy
, &proxy
->device
, &cap
);
1479 virtio_pci_modern_region_map(proxy
, &proxy
->notify
, ¬ify
.cap
);
1481 pci_register_bar(&proxy
->pci_dev
, proxy
->modern_mem_bar
,
1482 PCI_BASE_ADDRESS_SPACE_MEMORY
|
1483 PCI_BASE_ADDRESS_MEM_PREFETCH
|
1484 PCI_BASE_ADDRESS_MEM_TYPE_64
,
1485 &proxy
->modern_bar
);
1487 proxy
->config_cap
= virtio_pci_add_mem_cap(proxy
, &cfg
.cap
);
1488 cfg_mask
= (void *)(proxy
->pci_dev
.wmask
+ proxy
->config_cap
);
1489 pci_set_byte(&cfg_mask
->cap
.bar
, ~0x0);
1490 pci_set_long((uint8_t *)&cfg_mask
->cap
.offset
, ~0x0);
1491 pci_set_long((uint8_t *)&cfg_mask
->cap
.length
, ~0x0);
1492 pci_set_long(cfg_mask
->pci_cfg_data
, ~0x0);
1495 if (proxy
->nvectors
) {
1496 int err
= msix_init_exclusive_bar(&proxy
->pci_dev
, proxy
->nvectors
,
1499 /* Notice when a system that supports MSIx can't initialize it. */
1500 if (err
!= -ENOTSUP
) {
1501 error_report("unable to init msix vectors to %" PRIu32
,
1504 proxy
->nvectors
= 0;
1508 proxy
->pci_dev
.config_write
= virtio_write_config
;
1509 proxy
->pci_dev
.config_read
= virtio_read_config
;
1512 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
)
1513 + virtio_bus_get_vdev_config_len(bus
);
1514 size
= pow2ceil(size
);
1516 memory_region_init_io(&proxy
->bar
, OBJECT(proxy
),
1517 &virtio_pci_config_ops
,
1518 proxy
, "virtio-pci", size
);
1520 pci_register_bar(&proxy
->pci_dev
, proxy
->legacy_io_bar
,
1521 PCI_BASE_ADDRESS_SPACE_IO
, &proxy
->bar
);
1524 if (!kvm_has_many_ioeventfds()) {
1525 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
1528 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_BAD_FEATURE
);
1531 static void virtio_pci_device_unplugged(DeviceState
*d
)
1533 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1534 bool modern
= !(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_MODERN
);
1536 virtio_pci_stop_ioeventfd(proxy
);
1539 virtio_pci_modern_region_unmap(proxy
, &proxy
->common
);
1540 virtio_pci_modern_region_unmap(proxy
, &proxy
->isr
);
1541 virtio_pci_modern_region_unmap(proxy
, &proxy
->device
);
1542 virtio_pci_modern_region_unmap(proxy
, &proxy
->notify
);
1546 static void virtio_pci_realize(PCIDevice
*pci_dev
, Error
**errp
)
1548 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
1549 VirtioPCIClass
*k
= VIRTIO_PCI_GET_CLASS(pci_dev
);
1552 * virtio pci bar layout used by default.
1553 * subclasses can re-arrange things if needed.
1555 * region 0 -- virtio legacy io bar
1556 * region 1 -- msi-x bar
1557 * region 4+5 -- virtio modern memory (64bit) bar
1560 proxy
->legacy_io_bar
= 0;
1561 proxy
->msix_bar
= 1;
1562 proxy
->modern_mem_bar
= 4;
1564 proxy
->common
.offset
= 0x0;
1565 proxy
->common
.size
= 0x1000;
1566 proxy
->common
.type
= VIRTIO_PCI_CAP_COMMON_CFG
;
1568 proxy
->isr
.offset
= 0x1000;
1569 proxy
->isr
.size
= 0x1000;
1570 proxy
->isr
.type
= VIRTIO_PCI_CAP_ISR_CFG
;
1572 proxy
->device
.offset
= 0x2000;
1573 proxy
->device
.size
= 0x1000;
1574 proxy
->device
.type
= VIRTIO_PCI_CAP_DEVICE_CFG
;
1576 proxy
->notify
.offset
= 0x3000;
1577 proxy
->notify
.size
=
1578 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
* VIRTIO_QUEUE_MAX
;
1579 proxy
->notify
.type
= VIRTIO_PCI_CAP_NOTIFY_CFG
;
1581 /* subclasses can enforce modern, so do this unconditionally */
1582 memory_region_init(&proxy
->modern_bar
, OBJECT(proxy
), "virtio-pci",
1583 2 * QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
*
1586 memory_region_init_alias(&proxy
->modern_cfg
,
1591 memory_region_size(&proxy
->modern_bar
));
1593 address_space_init(&proxy
->modern_as
, &proxy
->modern_cfg
, "virtio-pci-cfg-as");
1595 virtio_pci_bus_new(&proxy
->bus
, sizeof(proxy
->bus
), proxy
);
1597 k
->realize(proxy
, errp
);
1601 static void virtio_pci_exit(PCIDevice
*pci_dev
)
1603 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
1605 msix_uninit_exclusive_bar(pci_dev
);
1606 address_space_destroy(&proxy
->modern_as
);
1609 static void virtio_pci_reset(DeviceState
*qdev
)
1611 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1612 VirtioBusState
*bus
= VIRTIO_BUS(&proxy
->bus
);
1613 virtio_pci_stop_ioeventfd(proxy
);
1614 virtio_bus_reset(bus
);
1615 msix_unuse_all_vectors(&proxy
->pci_dev
);
1618 static Property virtio_pci_properties
[] = {
1619 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy
, flags
,
1620 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT
, false),
1621 DEFINE_PROP_BIT("disable-legacy", VirtIOPCIProxy
, flags
,
1622 VIRTIO_PCI_FLAG_DISABLE_LEGACY_BIT
, false),
1623 DEFINE_PROP_BIT("disable-modern", VirtIOPCIProxy
, flags
,
1624 VIRTIO_PCI_FLAG_DISABLE_MODERN_BIT
, true),
1625 DEFINE_PROP_END_OF_LIST(),
1628 static void virtio_pci_class_init(ObjectClass
*klass
, void *data
)
1630 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1631 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1633 dc
->props
= virtio_pci_properties
;
1634 k
->realize
= virtio_pci_realize
;
1635 k
->exit
= virtio_pci_exit
;
1636 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1637 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1638 k
->class_id
= PCI_CLASS_OTHERS
;
1639 dc
->reset
= virtio_pci_reset
;
1642 static const TypeInfo virtio_pci_info
= {
1643 .name
= TYPE_VIRTIO_PCI
,
1644 .parent
= TYPE_PCI_DEVICE
,
1645 .instance_size
= sizeof(VirtIOPCIProxy
),
1646 .class_init
= virtio_pci_class_init
,
1647 .class_size
= sizeof(VirtioPCIClass
),
1651 /* virtio-blk-pci */
1653 static Property virtio_blk_pci_properties
[] = {
1654 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1655 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1656 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1657 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1658 DEFINE_PROP_END_OF_LIST(),
1661 static void virtio_blk_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1663 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(vpci_dev
);
1664 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1666 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1667 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1670 static void virtio_blk_pci_class_init(ObjectClass
*klass
, void *data
)
1672 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1673 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1674 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1676 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1677 dc
->props
= virtio_blk_pci_properties
;
1678 k
->realize
= virtio_blk_pci_realize
;
1679 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1680 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
1681 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1682 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1685 static void virtio_blk_pci_instance_init(Object
*obj
)
1687 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(obj
);
1689 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1691 object_property_add_alias(obj
, "iothread", OBJECT(&dev
->vdev
),"iothread",
1693 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
1694 "bootindex", &error_abort
);
1697 static const TypeInfo virtio_blk_pci_info
= {
1698 .name
= TYPE_VIRTIO_BLK_PCI
,
1699 .parent
= TYPE_VIRTIO_PCI
,
1700 .instance_size
= sizeof(VirtIOBlkPCI
),
1701 .instance_init
= virtio_blk_pci_instance_init
,
1702 .class_init
= virtio_blk_pci_class_init
,
1705 /* virtio-scsi-pci */
1707 static Property virtio_scsi_pci_properties
[] = {
1708 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1709 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1710 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1711 DEV_NVECTORS_UNSPECIFIED
),
1712 DEFINE_PROP_END_OF_LIST(),
1715 static void virtio_scsi_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1717 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(vpci_dev
);
1718 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1719 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1720 DeviceState
*proxy
= DEVICE(vpci_dev
);
1723 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1724 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1728 * For command line compatibility, this sets the virtio-scsi-device bus
1732 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1733 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1737 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1738 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1741 static void virtio_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1743 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1744 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1745 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1747 k
->realize
= virtio_scsi_pci_realize
;
1748 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1749 dc
->props
= virtio_scsi_pci_properties
;
1750 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1751 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1752 pcidev_k
->revision
= 0x00;
1753 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1756 static void virtio_scsi_pci_instance_init(Object
*obj
)
1758 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(obj
);
1760 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1762 object_property_add_alias(obj
, "iothread", OBJECT(&dev
->vdev
), "iothread",
1766 static const TypeInfo virtio_scsi_pci_info
= {
1767 .name
= TYPE_VIRTIO_SCSI_PCI
,
1768 .parent
= TYPE_VIRTIO_PCI
,
1769 .instance_size
= sizeof(VirtIOSCSIPCI
),
1770 .instance_init
= virtio_scsi_pci_instance_init
,
1771 .class_init
= virtio_scsi_pci_class_init
,
1774 /* vhost-scsi-pci */
1776 #ifdef CONFIG_VHOST_SCSI
1777 static Property vhost_scsi_pci_properties
[] = {
1778 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1779 DEV_NVECTORS_UNSPECIFIED
),
1780 DEFINE_PROP_END_OF_LIST(),
1783 static void vhost_scsi_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1785 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(vpci_dev
);
1786 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1787 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1789 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1790 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1793 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1794 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1797 static void vhost_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1799 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1800 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1801 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1802 k
->realize
= vhost_scsi_pci_realize
;
1803 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1804 dc
->props
= vhost_scsi_pci_properties
;
1805 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1806 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1807 pcidev_k
->revision
= 0x00;
1808 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1811 static void vhost_scsi_pci_instance_init(Object
*obj
)
1813 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(obj
);
1815 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1817 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
1818 "bootindex", &error_abort
);
1821 static const TypeInfo vhost_scsi_pci_info
= {
1822 .name
= TYPE_VHOST_SCSI_PCI
,
1823 .parent
= TYPE_VIRTIO_PCI
,
1824 .instance_size
= sizeof(VHostSCSIPCI
),
1825 .instance_init
= vhost_scsi_pci_instance_init
,
1826 .class_init
= vhost_scsi_pci_class_init
,
1830 /* virtio-balloon-pci */
1832 static Property virtio_balloon_pci_properties
[] = {
1833 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1834 DEFINE_PROP_END_OF_LIST(),
1837 static void virtio_balloon_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1839 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(vpci_dev
);
1840 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1842 if (vpci_dev
->class_code
!= PCI_CLASS_OTHERS
&&
1843 vpci_dev
->class_code
!= PCI_CLASS_MEMORY_RAM
) { /* qemu < 1.1 */
1844 vpci_dev
->class_code
= PCI_CLASS_OTHERS
;
1847 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1848 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1851 static void virtio_balloon_pci_class_init(ObjectClass
*klass
, void *data
)
1853 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1854 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1855 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1856 k
->realize
= virtio_balloon_pci_realize
;
1857 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1858 dc
->props
= virtio_balloon_pci_properties
;
1859 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1860 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BALLOON
;
1861 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1862 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
1865 static void virtio_balloon_pci_instance_init(Object
*obj
)
1867 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(obj
);
1869 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1870 TYPE_VIRTIO_BALLOON
);
1871 object_property_add_alias(obj
, "guest-stats", OBJECT(&dev
->vdev
),
1872 "guest-stats", &error_abort
);
1873 object_property_add_alias(obj
, "guest-stats-polling-interval",
1875 "guest-stats-polling-interval", &error_abort
);
1878 static const TypeInfo virtio_balloon_pci_info
= {
1879 .name
= TYPE_VIRTIO_BALLOON_PCI
,
1880 .parent
= TYPE_VIRTIO_PCI
,
1881 .instance_size
= sizeof(VirtIOBalloonPCI
),
1882 .instance_init
= virtio_balloon_pci_instance_init
,
1883 .class_init
= virtio_balloon_pci_class_init
,
1886 /* virtio-serial-pci */
1888 static void virtio_serial_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1890 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(vpci_dev
);
1891 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1892 DeviceState
*proxy
= DEVICE(vpci_dev
);
1895 if (vpci_dev
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
1896 vpci_dev
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
1897 vpci_dev
->class_code
!= PCI_CLASS_OTHERS
) { /* qemu-kvm */
1898 vpci_dev
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
1901 /* backwards-compatibility with machines that were created with
1902 DEV_NVECTORS_UNSPECIFIED */
1903 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1904 vpci_dev
->nvectors
= dev
->vdev
.serial
.max_virtserial_ports
+ 1;
1908 * For command line compatibility, this sets the virtio-serial-device bus
1912 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1913 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1917 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1918 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1921 static Property virtio_serial_pci_properties
[] = {
1922 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1923 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1924 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1925 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1926 DEFINE_PROP_END_OF_LIST(),
1929 static void virtio_serial_pci_class_init(ObjectClass
*klass
, void *data
)
1931 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1932 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1933 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1934 k
->realize
= virtio_serial_pci_realize
;
1935 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
1936 dc
->props
= virtio_serial_pci_properties
;
1937 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1938 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_CONSOLE
;
1939 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1940 pcidev_k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
1943 static void virtio_serial_pci_instance_init(Object
*obj
)
1945 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(obj
);
1947 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1948 TYPE_VIRTIO_SERIAL
);
1951 static const TypeInfo virtio_serial_pci_info
= {
1952 .name
= TYPE_VIRTIO_SERIAL_PCI
,
1953 .parent
= TYPE_VIRTIO_PCI
,
1954 .instance_size
= sizeof(VirtIOSerialPCI
),
1955 .instance_init
= virtio_serial_pci_instance_init
,
1956 .class_init
= virtio_serial_pci_class_init
,
1959 /* virtio-net-pci */
1961 static Property virtio_net_properties
[] = {
1962 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1963 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, false),
1964 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
1965 DEFINE_PROP_END_OF_LIST(),
1968 static void virtio_net_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1970 DeviceState
*qdev
= DEVICE(vpci_dev
);
1971 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(vpci_dev
);
1972 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1974 virtio_net_set_netclient_name(&dev
->vdev
, qdev
->id
,
1975 object_get_typename(OBJECT(qdev
)));
1976 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1977 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1980 static void virtio_net_pci_class_init(ObjectClass
*klass
, void *data
)
1982 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1983 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1984 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_CLASS(klass
);
1986 k
->romfile
= "efi-virtio.rom";
1987 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1988 k
->device_id
= PCI_DEVICE_ID_VIRTIO_NET
;
1989 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1990 k
->class_id
= PCI_CLASS_NETWORK_ETHERNET
;
1991 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
1992 dc
->props
= virtio_net_properties
;
1993 vpciklass
->realize
= virtio_net_pci_realize
;
1996 static void virtio_net_pci_instance_init(Object
*obj
)
1998 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(obj
);
2000 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2002 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
2003 "bootindex", &error_abort
);
2006 static const TypeInfo virtio_net_pci_info
= {
2007 .name
= TYPE_VIRTIO_NET_PCI
,
2008 .parent
= TYPE_VIRTIO_PCI
,
2009 .instance_size
= sizeof(VirtIONetPCI
),
2010 .instance_init
= virtio_net_pci_instance_init
,
2011 .class_init
= virtio_net_pci_class_init
,
2014 /* virtio-rng-pci */
2016 static void virtio_rng_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2018 VirtIORngPCI
*vrng
= VIRTIO_RNG_PCI(vpci_dev
);
2019 DeviceState
*vdev
= DEVICE(&vrng
->vdev
);
2022 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2023 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
2025 error_propagate(errp
, err
);
2029 object_property_set_link(OBJECT(vrng
),
2030 OBJECT(vrng
->vdev
.conf
.rng
), "rng",
2034 static void virtio_rng_pci_class_init(ObjectClass
*klass
, void *data
)
2036 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2037 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2038 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2040 k
->realize
= virtio_rng_pci_realize
;
2041 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
2043 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2044 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_RNG
;
2045 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
2046 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
2049 static void virtio_rng_initfn(Object
*obj
)
2051 VirtIORngPCI
*dev
= VIRTIO_RNG_PCI(obj
);
2053 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2055 object_property_add_alias(obj
, "rng", OBJECT(&dev
->vdev
), "rng",
2059 static const TypeInfo virtio_rng_pci_info
= {
2060 .name
= TYPE_VIRTIO_RNG_PCI
,
2061 .parent
= TYPE_VIRTIO_PCI
,
2062 .instance_size
= sizeof(VirtIORngPCI
),
2063 .instance_init
= virtio_rng_initfn
,
2064 .class_init
= virtio_rng_pci_class_init
,
2067 /* virtio-input-pci */
2069 static Property virtio_input_pci_properties
[] = {
2070 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
2071 DEFINE_PROP_END_OF_LIST(),
2074 static void virtio_input_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2076 VirtIOInputPCI
*vinput
= VIRTIO_INPUT_PCI(vpci_dev
);
2077 DeviceState
*vdev
= DEVICE(&vinput
->vdev
);
2079 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2080 /* force virtio-1.0 */
2081 vpci_dev
->flags
&= ~VIRTIO_PCI_FLAG_DISABLE_MODERN
;
2082 vpci_dev
->flags
|= VIRTIO_PCI_FLAG_DISABLE_LEGACY
;
2083 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2086 static void virtio_input_pci_class_init(ObjectClass
*klass
, void *data
)
2088 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2089 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2090 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2092 dc
->props
= virtio_input_pci_properties
;
2093 k
->realize
= virtio_input_pci_realize
;
2094 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
2096 pcidev_k
->class_id
= PCI_CLASS_INPUT_OTHER
;
2099 static void virtio_input_hid_kbd_pci_class_init(ObjectClass
*klass
, void *data
)
2101 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2103 pcidev_k
->class_id
= PCI_CLASS_INPUT_KEYBOARD
;
2106 static void virtio_input_hid_mouse_pci_class_init(ObjectClass
*klass
,
2109 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2111 pcidev_k
->class_id
= PCI_CLASS_INPUT_MOUSE
;
2114 static void virtio_keyboard_initfn(Object
*obj
)
2116 VirtIOInputHIDPCI
*dev
= VIRTIO_INPUT_HID_PCI(obj
);
2118 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2119 TYPE_VIRTIO_KEYBOARD
);
2122 static void virtio_mouse_initfn(Object
*obj
)
2124 VirtIOInputHIDPCI
*dev
= VIRTIO_INPUT_HID_PCI(obj
);
2126 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2130 static void virtio_tablet_initfn(Object
*obj
)
2132 VirtIOInputHIDPCI
*dev
= VIRTIO_INPUT_HID_PCI(obj
);
2134 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2135 TYPE_VIRTIO_TABLET
);
2138 static const TypeInfo virtio_input_pci_info
= {
2139 .name
= TYPE_VIRTIO_INPUT_PCI
,
2140 .parent
= TYPE_VIRTIO_PCI
,
2141 .instance_size
= sizeof(VirtIOInputPCI
),
2142 .class_init
= virtio_input_pci_class_init
,
2146 static const TypeInfo virtio_input_hid_pci_info
= {
2147 .name
= TYPE_VIRTIO_INPUT_HID_PCI
,
2148 .parent
= TYPE_VIRTIO_INPUT_PCI
,
2149 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2153 static const TypeInfo virtio_keyboard_pci_info
= {
2154 .name
= TYPE_VIRTIO_KEYBOARD_PCI
,
2155 .parent
= TYPE_VIRTIO_INPUT_HID_PCI
,
2156 .class_init
= virtio_input_hid_kbd_pci_class_init
,
2157 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2158 .instance_init
= virtio_keyboard_initfn
,
2161 static const TypeInfo virtio_mouse_pci_info
= {
2162 .name
= TYPE_VIRTIO_MOUSE_PCI
,
2163 .parent
= TYPE_VIRTIO_INPUT_HID_PCI
,
2164 .class_init
= virtio_input_hid_mouse_pci_class_init
,
2165 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2166 .instance_init
= virtio_mouse_initfn
,
2169 static const TypeInfo virtio_tablet_pci_info
= {
2170 .name
= TYPE_VIRTIO_TABLET_PCI
,
2171 .parent
= TYPE_VIRTIO_INPUT_HID_PCI
,
2172 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2173 .instance_init
= virtio_tablet_initfn
,
2177 static void virtio_host_initfn(Object
*obj
)
2179 VirtIOInputHostPCI
*dev
= VIRTIO_INPUT_HOST_PCI(obj
);
2181 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2182 TYPE_VIRTIO_INPUT_HOST
);
2185 static const TypeInfo virtio_host_pci_info
= {
2186 .name
= TYPE_VIRTIO_INPUT_HOST_PCI
,
2187 .parent
= TYPE_VIRTIO_INPUT_PCI
,
2188 .instance_size
= sizeof(VirtIOInputHostPCI
),
2189 .instance_init
= virtio_host_initfn
,
2193 /* virtio-pci-bus */
2195 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
2196 VirtIOPCIProxy
*dev
)
2198 DeviceState
*qdev
= DEVICE(dev
);
2199 char virtio_bus_name
[] = "virtio-bus";
2201 qbus_create_inplace(bus
, bus_size
, TYPE_VIRTIO_PCI_BUS
, qdev
,
2205 static void virtio_pci_bus_class_init(ObjectClass
*klass
, void *data
)
2207 BusClass
*bus_class
= BUS_CLASS(klass
);
2208 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
2209 bus_class
->max_dev
= 1;
2210 k
->notify
= virtio_pci_notify
;
2211 k
->save_config
= virtio_pci_save_config
;
2212 k
->load_config
= virtio_pci_load_config
;
2213 k
->save_queue
= virtio_pci_save_queue
;
2214 k
->load_queue
= virtio_pci_load_queue
;
2215 k
->query_guest_notifiers
= virtio_pci_query_guest_notifiers
;
2216 k
->set_host_notifier
= virtio_pci_set_host_notifier
;
2217 k
->set_guest_notifiers
= virtio_pci_set_guest_notifiers
;
2218 k
->vmstate_change
= virtio_pci_vmstate_change
;
2219 k
->device_plugged
= virtio_pci_device_plugged
;
2220 k
->device_unplugged
= virtio_pci_device_unplugged
;
2221 k
->query_nvectors
= virtio_pci_query_nvectors
;
2224 static const TypeInfo virtio_pci_bus_info
= {
2225 .name
= TYPE_VIRTIO_PCI_BUS
,
2226 .parent
= TYPE_VIRTIO_BUS
,
2227 .instance_size
= sizeof(VirtioPCIBusState
),
2228 .class_init
= virtio_pci_bus_class_init
,
2231 static void virtio_pci_register_types(void)
2233 type_register_static(&virtio_rng_pci_info
);
2234 type_register_static(&virtio_input_pci_info
);
2235 type_register_static(&virtio_input_hid_pci_info
);
2236 type_register_static(&virtio_keyboard_pci_info
);
2237 type_register_static(&virtio_mouse_pci_info
);
2238 type_register_static(&virtio_tablet_pci_info
);
2240 type_register_static(&virtio_host_pci_info
);
2242 type_register_static(&virtio_pci_bus_info
);
2243 type_register_static(&virtio_pci_info
);
2244 #ifdef CONFIG_VIRTFS
2245 type_register_static(&virtio_9p_pci_info
);
2247 type_register_static(&virtio_blk_pci_info
);
2248 type_register_static(&virtio_scsi_pci_info
);
2249 type_register_static(&virtio_balloon_pci_info
);
2250 type_register_static(&virtio_serial_pci_info
);
2251 type_register_static(&virtio_net_pci_info
);
2252 #ifdef CONFIG_VHOST_SCSI
2253 type_register_static(&vhost_scsi_pci_info
);
2257 type_init(virtio_pci_register_types
)