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
);
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(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(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
);
736 /* If guest supports masking, irqfd is already setup, unmask it.
737 * Otherwise, set it up now.
739 if (k
->guest_notifier_mask
) {
740 k
->guest_notifier_mask(vdev
, queue_no
, false);
741 /* Test after unmasking to avoid losing events. */
742 if (k
->guest_notifier_pending
&&
743 k
->guest_notifier_pending(vdev
, queue_no
)) {
744 event_notifier_set(n
);
747 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
752 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy
*proxy
,
753 unsigned int queue_no
,
756 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
757 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
759 /* If guest supports masking, keep irqfd but mask it.
760 * Otherwise, clean it up now.
762 if (k
->guest_notifier_mask
) {
763 k
->guest_notifier_mask(vdev
, queue_no
, true);
765 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
769 static int virtio_pci_vector_unmask(PCIDevice
*dev
, unsigned vector
,
772 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
773 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
774 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
775 int ret
, index
, unmasked
= 0;
778 index
= virtio_get_queue_index(vq
);
779 if (!virtio_queue_get_num(vdev
, index
)) {
782 if (index
< proxy
->nvqs_with_notifiers
) {
783 ret
= virtio_pci_vq_vector_unmask(proxy
, index
, vector
, msg
);
789 vq
= virtio_vector_next_queue(vq
);
795 vq
= virtio_vector_first_queue(vdev
, vector
);
796 while (vq
&& unmasked
>= 0) {
797 index
= virtio_get_queue_index(vq
);
798 if (index
< proxy
->nvqs_with_notifiers
) {
799 virtio_pci_vq_vector_mask(proxy
, index
, vector
);
802 vq
= virtio_vector_next_queue(vq
);
807 static void virtio_pci_vector_mask(PCIDevice
*dev
, unsigned vector
)
809 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
810 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
811 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
815 index
= virtio_get_queue_index(vq
);
816 if (!virtio_queue_get_num(vdev
, index
)) {
819 if (index
< proxy
->nvqs_with_notifiers
) {
820 virtio_pci_vq_vector_mask(proxy
, index
, vector
);
822 vq
= virtio_vector_next_queue(vq
);
826 static void virtio_pci_vector_poll(PCIDevice
*dev
,
827 unsigned int vector_start
,
828 unsigned int vector_end
)
830 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
831 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
832 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
835 EventNotifier
*notifier
;
838 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
839 if (!virtio_queue_get_num(vdev
, queue_no
)) {
842 vector
= virtio_queue_vector(vdev
, queue_no
);
843 if (vector
< vector_start
|| vector
>= vector_end
||
844 !msix_is_masked(dev
, vector
)) {
847 vq
= virtio_get_queue(vdev
, queue_no
);
848 notifier
= virtio_queue_get_guest_notifier(vq
);
849 if (k
->guest_notifier_pending
) {
850 if (k
->guest_notifier_pending(vdev
, queue_no
)) {
851 msix_set_pending(dev
, vector
);
853 } else if (event_notifier_test_and_clear(notifier
)) {
854 msix_set_pending(dev
, vector
);
859 static int virtio_pci_set_guest_notifier(DeviceState
*d
, int n
, bool assign
,
862 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
863 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
864 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
865 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
866 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
869 int r
= event_notifier_init(notifier
, 0);
873 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
875 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
876 event_notifier_cleanup(notifier
);
879 if (!msix_enabled(&proxy
->pci_dev
) && vdc
->guest_notifier_mask
) {
880 vdc
->guest_notifier_mask(vdev
, n
, !assign
);
886 static bool virtio_pci_query_guest_notifiers(DeviceState
*d
)
888 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
889 return msix_enabled(&proxy
->pci_dev
);
892 static int virtio_pci_set_guest_notifiers(DeviceState
*d
, int nvqs
, bool assign
)
894 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
895 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
896 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
898 bool with_irqfd
= msix_enabled(&proxy
->pci_dev
) &&
899 kvm_msi_via_irqfd_enabled();
901 nvqs
= MIN(nvqs
, VIRTIO_QUEUE_MAX
);
903 /* When deassigning, pass a consistent nvqs value
904 * to avoid leaking notifiers.
906 assert(assign
|| nvqs
== proxy
->nvqs_with_notifiers
);
908 proxy
->nvqs_with_notifiers
= nvqs
;
910 /* Must unset vector notifier while guest notifier is still assigned */
911 if ((proxy
->vector_irqfd
|| k
->guest_notifier_mask
) && !assign
) {
912 msix_unset_vector_notifiers(&proxy
->pci_dev
);
913 if (proxy
->vector_irqfd
) {
914 kvm_virtio_pci_vector_release(proxy
, nvqs
);
915 g_free(proxy
->vector_irqfd
);
916 proxy
->vector_irqfd
= NULL
;
920 for (n
= 0; n
< nvqs
; n
++) {
921 if (!virtio_queue_get_num(vdev
, n
)) {
925 r
= virtio_pci_set_guest_notifier(d
, n
, assign
, with_irqfd
);
931 /* Must set vector notifier after guest notifier has been assigned */
932 if ((with_irqfd
|| k
->guest_notifier_mask
) && assign
) {
934 proxy
->vector_irqfd
=
935 g_malloc0(sizeof(*proxy
->vector_irqfd
) *
936 msix_nr_vectors_allocated(&proxy
->pci_dev
));
937 r
= kvm_virtio_pci_vector_use(proxy
, nvqs
);
942 r
= msix_set_vector_notifiers(&proxy
->pci_dev
,
943 virtio_pci_vector_unmask
,
944 virtio_pci_vector_mask
,
945 virtio_pci_vector_poll
);
947 goto notifiers_error
;
956 kvm_virtio_pci_vector_release(proxy
, nvqs
);
960 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
963 virtio_pci_set_guest_notifier(d
, n
, !assign
, with_irqfd
);
968 static int virtio_pci_set_host_notifier(DeviceState
*d
, int n
, bool assign
)
970 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
972 /* Stop using ioeventfd for virtqueue kick if the device starts using host
973 * notifiers. This makes it easy to avoid stepping on each others' toes.
975 proxy
->ioeventfd_disabled
= assign
;
977 virtio_pci_stop_ioeventfd(proxy
);
979 /* We don't need to start here: it's not needed because backend
980 * currently only stops on status change away from ok,
981 * reset, vmstop and such. If we do add code to start here,
982 * need to check vmstate, device state etc. */
983 return virtio_pci_set_host_notifier_internal(proxy
, n
, assign
, false);
986 static void virtio_pci_vmstate_change(DeviceState
*d
, bool running
)
988 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
989 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
992 /* Old QEMU versions did not set bus master enable on status write.
993 * Detect DRIVER set and enable it.
995 if ((proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION
) &&
996 (vdev
->status
& VIRTIO_CONFIG_S_DRIVER
) &&
997 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
998 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
999 proxy
->pci_dev
.config
[PCI_COMMAND
] |
1000 PCI_COMMAND_MASTER
, 1);
1002 virtio_pci_start_ioeventfd(proxy
);
1004 virtio_pci_stop_ioeventfd(proxy
);
1008 #ifdef CONFIG_VIRTFS
1009 static void virtio_9p_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1011 V9fsPCIState
*dev
= VIRTIO_9P_PCI(vpci_dev
);
1012 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1014 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1015 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1018 static Property virtio_9p_pci_properties
[] = {
1019 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1020 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1021 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1022 DEFINE_PROP_END_OF_LIST(),
1025 static void virtio_9p_pci_class_init(ObjectClass
*klass
, void *data
)
1027 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1028 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1029 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1031 k
->realize
= virtio_9p_pci_realize
;
1032 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1033 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_9P
;
1034 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1035 pcidev_k
->class_id
= 0x2;
1036 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1037 dc
->props
= virtio_9p_pci_properties
;
1040 static void virtio_9p_pci_instance_init(Object
*obj
)
1042 V9fsPCIState
*dev
= VIRTIO_9P_PCI(obj
);
1044 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1048 static const TypeInfo virtio_9p_pci_info
= {
1049 .name
= TYPE_VIRTIO_9P_PCI
,
1050 .parent
= TYPE_VIRTIO_PCI
,
1051 .instance_size
= sizeof(V9fsPCIState
),
1052 .instance_init
= virtio_9p_pci_instance_init
,
1053 .class_init
= virtio_9p_pci_class_init
,
1055 #endif /* CONFIG_VIRTFS */
1058 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
1061 static int virtio_pci_query_nvectors(DeviceState
*d
)
1063 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1065 return proxy
->nvectors
;
1068 static int virtio_pci_add_mem_cap(VirtIOPCIProxy
*proxy
,
1069 struct virtio_pci_cap
*cap
)
1071 PCIDevice
*dev
= &proxy
->pci_dev
;
1074 offset
= pci_add_capability(dev
, PCI_CAP_ID_VNDR
, 0, cap
->cap_len
);
1077 assert(cap
->cap_len
>= sizeof *cap
);
1078 memcpy(dev
->config
+ offset
+ PCI_CAP_FLAGS
, &cap
->cap_len
,
1079 cap
->cap_len
- PCI_CAP_FLAGS
);
1084 static uint64_t virtio_pci_common_read(void *opaque
, hwaddr addr
,
1087 VirtIOPCIProxy
*proxy
= opaque
;
1088 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1093 case VIRTIO_PCI_COMMON_DFSELECT
:
1094 val
= proxy
->dfselect
;
1096 case VIRTIO_PCI_COMMON_DF
:
1097 if (proxy
->dfselect
<= 1) {
1098 val
= vdev
->host_features
>> (32 * proxy
->dfselect
);
1101 case VIRTIO_PCI_COMMON_GFSELECT
:
1102 val
= proxy
->gfselect
;
1104 case VIRTIO_PCI_COMMON_GF
:
1105 if (proxy
->gfselect
< ARRAY_SIZE(proxy
->guest_features
)) {
1106 val
= proxy
->guest_features
[proxy
->gfselect
];
1109 case VIRTIO_PCI_COMMON_MSIX
:
1110 val
= vdev
->config_vector
;
1112 case VIRTIO_PCI_COMMON_NUMQ
:
1113 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; ++i
) {
1114 if (virtio_queue_get_num(vdev
, i
)) {
1119 case VIRTIO_PCI_COMMON_STATUS
:
1122 case VIRTIO_PCI_COMMON_CFGGENERATION
:
1123 val
= vdev
->generation
;
1125 case VIRTIO_PCI_COMMON_Q_SELECT
:
1126 val
= vdev
->queue_sel
;
1128 case VIRTIO_PCI_COMMON_Q_SIZE
:
1129 val
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
1131 case VIRTIO_PCI_COMMON_Q_MSIX
:
1132 val
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
1134 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1135 val
= proxy
->vqs
[vdev
->queue_sel
].enabled
;
1137 case VIRTIO_PCI_COMMON_Q_NOFF
:
1138 /* Simply map queues in order */
1139 val
= vdev
->queue_sel
;
1141 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1142 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[0];
1144 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1145 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[1];
1147 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1148 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[0];
1150 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1151 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[1];
1153 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1154 val
= proxy
->vqs
[vdev
->queue_sel
].used
[0];
1156 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1157 val
= proxy
->vqs
[vdev
->queue_sel
].used
[1];
1166 static void virtio_pci_common_write(void *opaque
, hwaddr addr
,
1167 uint64_t val
, unsigned size
)
1169 VirtIOPCIProxy
*proxy
= opaque
;
1170 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1173 case VIRTIO_PCI_COMMON_DFSELECT
:
1174 proxy
->dfselect
= val
;
1176 case VIRTIO_PCI_COMMON_GFSELECT
:
1177 proxy
->gfselect
= val
;
1179 case VIRTIO_PCI_COMMON_GF
:
1180 if (proxy
->gfselect
< ARRAY_SIZE(proxy
->guest_features
)) {
1181 proxy
->guest_features
[proxy
->gfselect
] = val
;
1182 virtio_set_features(vdev
,
1183 (((uint64_t)proxy
->guest_features
[1]) << 32) |
1184 proxy
->guest_features
[0]);
1187 case VIRTIO_PCI_COMMON_MSIX
:
1188 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
1189 /* Make it possible for guest to discover an error took place. */
1190 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0) {
1191 val
= VIRTIO_NO_VECTOR
;
1193 vdev
->config_vector
= val
;
1195 case VIRTIO_PCI_COMMON_STATUS
:
1196 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
1197 virtio_pci_stop_ioeventfd(proxy
);
1200 virtio_set_status(vdev
, val
& 0xFF);
1202 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
1203 virtio_pci_start_ioeventfd(proxy
);
1206 if (vdev
->status
== 0) {
1208 msix_unuse_all_vectors(&proxy
->pci_dev
);
1212 case VIRTIO_PCI_COMMON_Q_SELECT
:
1213 if (val
< VIRTIO_QUEUE_MAX
) {
1214 vdev
->queue_sel
= val
;
1217 case VIRTIO_PCI_COMMON_Q_SIZE
:
1218 proxy
->vqs
[vdev
->queue_sel
].num
= val
;
1220 case VIRTIO_PCI_COMMON_Q_MSIX
:
1221 msix_vector_unuse(&proxy
->pci_dev
,
1222 virtio_queue_vector(vdev
, vdev
->queue_sel
));
1223 /* Make it possible for guest to discover an error took place. */
1224 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0) {
1225 val
= VIRTIO_NO_VECTOR
;
1227 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
1229 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1230 /* TODO: need a way to put num back on reset. */
1231 virtio_queue_set_num(vdev
, vdev
->queue_sel
,
1232 proxy
->vqs
[vdev
->queue_sel
].num
);
1233 virtio_queue_set_rings(vdev
, vdev
->queue_sel
,
1234 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].desc
[1]) << 32 |
1235 proxy
->vqs
[vdev
->queue_sel
].desc
[0],
1236 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].avail
[1]) << 32 |
1237 proxy
->vqs
[vdev
->queue_sel
].avail
[0],
1238 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].used
[1]) << 32 |
1239 proxy
->vqs
[vdev
->queue_sel
].used
[0]);
1241 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1242 proxy
->vqs
[vdev
->queue_sel
].desc
[0] = val
;
1244 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1245 proxy
->vqs
[vdev
->queue_sel
].desc
[1] = val
;
1247 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1248 proxy
->vqs
[vdev
->queue_sel
].avail
[0] = val
;
1250 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1251 proxy
->vqs
[vdev
->queue_sel
].avail
[1] = val
;
1253 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1254 proxy
->vqs
[vdev
->queue_sel
].used
[0] = val
;
1256 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1257 proxy
->vqs
[vdev
->queue_sel
].used
[1] = val
;
1265 static uint64_t virtio_pci_notify_read(void *opaque
, hwaddr addr
,
1271 static void virtio_pci_notify_write(void *opaque
, hwaddr addr
,
1272 uint64_t val
, unsigned size
)
1274 VirtIODevice
*vdev
= opaque
;
1275 unsigned queue
= addr
/ QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
;
1277 if (queue
< VIRTIO_QUEUE_MAX
) {
1278 virtio_queue_notify(vdev
, queue
);
1282 static uint64_t virtio_pci_isr_read(void *opaque
, hwaddr addr
,
1285 VirtIOPCIProxy
*proxy
= opaque
;
1286 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1287 uint64_t val
= vdev
->isr
;
1290 pci_irq_deassert(&proxy
->pci_dev
);
1295 static void virtio_pci_isr_write(void *opaque
, hwaddr addr
,
1296 uint64_t val
, unsigned size
)
1300 static uint64_t virtio_pci_device_read(void *opaque
, hwaddr addr
,
1303 VirtIODevice
*vdev
= opaque
;
1308 val
= virtio_config_modern_readb(vdev
, addr
);
1311 val
= virtio_config_modern_readw(vdev
, addr
);
1314 val
= virtio_config_modern_readl(vdev
, addr
);
1320 static void virtio_pci_device_write(void *opaque
, hwaddr addr
,
1321 uint64_t val
, unsigned size
)
1323 VirtIODevice
*vdev
= opaque
;
1326 virtio_config_modern_writeb(vdev
, addr
, val
);
1329 virtio_config_modern_writew(vdev
, addr
, val
);
1332 virtio_config_modern_writel(vdev
, addr
, val
);
1337 static void virtio_pci_modern_regions_init(VirtIOPCIProxy
*proxy
)
1339 static const MemoryRegionOps common_ops
= {
1340 .read
= virtio_pci_common_read
,
1341 .write
= virtio_pci_common_write
,
1343 .min_access_size
= 1,
1344 .max_access_size
= 4,
1346 .endianness
= DEVICE_LITTLE_ENDIAN
,
1348 static const MemoryRegionOps isr_ops
= {
1349 .read
= virtio_pci_isr_read
,
1350 .write
= virtio_pci_isr_write
,
1352 .min_access_size
= 1,
1353 .max_access_size
= 4,
1355 .endianness
= DEVICE_LITTLE_ENDIAN
,
1357 static const MemoryRegionOps device_ops
= {
1358 .read
= virtio_pci_device_read
,
1359 .write
= virtio_pci_device_write
,
1361 .min_access_size
= 1,
1362 .max_access_size
= 4,
1364 .endianness
= DEVICE_LITTLE_ENDIAN
,
1366 static const MemoryRegionOps notify_ops
= {
1367 .read
= virtio_pci_notify_read
,
1368 .write
= virtio_pci_notify_write
,
1370 .min_access_size
= 1,
1371 .max_access_size
= 4,
1373 .endianness
= DEVICE_LITTLE_ENDIAN
,
1376 memory_region_init_io(&proxy
->common
.mr
, OBJECT(proxy
),
1379 "virtio-pci-common",
1380 proxy
->common
.size
);
1382 memory_region_init_io(&proxy
->isr
.mr
, OBJECT(proxy
),
1388 memory_region_init_io(&proxy
->device
.mr
, OBJECT(proxy
),
1390 virtio_bus_get_device(&proxy
->bus
),
1391 "virtio-pci-device",
1392 proxy
->device
.size
);
1394 memory_region_init_io(&proxy
->notify
.mr
, OBJECT(proxy
),
1396 virtio_bus_get_device(&proxy
->bus
),
1397 "virtio-pci-notify",
1398 proxy
->notify
.size
);
1401 static void virtio_pci_modern_region_map(VirtIOPCIProxy
*proxy
,
1402 VirtIOPCIRegion
*region
,
1403 struct virtio_pci_cap
*cap
)
1405 memory_region_add_subregion(&proxy
->modern_bar
,
1409 cap
->cfg_type
= region
->type
;
1410 cap
->bar
= proxy
->modern_mem_bar
;
1411 cap
->offset
= cpu_to_le32(region
->offset
);
1412 cap
->length
= cpu_to_le32(region
->size
);
1413 virtio_pci_add_mem_cap(proxy
, cap
);
1416 /* This is called by virtio-bus just after the device is plugged. */
1417 static void virtio_pci_device_plugged(DeviceState
*d
, Error
**errp
)
1419 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1420 VirtioBusState
*bus
= &proxy
->bus
;
1421 bool legacy
= !(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_LEGACY
);
1422 bool modern
= !(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_MODERN
);
1425 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1427 config
= proxy
->pci_dev
.config
;
1428 if (proxy
->class_code
) {
1429 pci_config_set_class(config
, proxy
->class_code
);
1433 /* legacy and transitional */
1434 pci_set_word(config
+ PCI_SUBSYSTEM_VENDOR_ID
,
1435 pci_get_word(config
+ PCI_VENDOR_ID
));
1436 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, virtio_bus_get_vdev_id(bus
));
1438 /* pure virtio-1.0 */
1439 pci_set_word(config
+ PCI_VENDOR_ID
,
1440 PCI_VENDOR_ID_REDHAT_QUMRANET
);
1441 pci_set_word(config
+ PCI_DEVICE_ID
,
1442 0x1040 + virtio_bus_get_vdev_id(bus
));
1443 pci_config_set_revision(config
, 1);
1445 config
[PCI_INTERRUPT_PIN
] = 1;
1449 struct virtio_pci_cap cap
= {
1450 .cap_len
= sizeof cap
,
1452 struct virtio_pci_notify_cap notify
= {
1453 .cap
.cap_len
= sizeof notify
,
1454 .notify_off_multiplier
=
1455 cpu_to_le32(QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
),
1457 struct virtio_pci_cfg_cap cfg
= {
1458 .cap
.cap_len
= sizeof cfg
,
1459 .cap
.cfg_type
= VIRTIO_PCI_CAP_PCI_CFG
,
1461 struct virtio_pci_cfg_cap
*cfg_mask
;
1463 /* TODO: add io access for speed */
1465 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_VERSION_1
);
1466 virtio_pci_modern_regions_init(proxy
);
1467 virtio_pci_modern_region_map(proxy
, &proxy
->common
, &cap
);
1468 virtio_pci_modern_region_map(proxy
, &proxy
->isr
, &cap
);
1469 virtio_pci_modern_region_map(proxy
, &proxy
->device
, &cap
);
1470 virtio_pci_modern_region_map(proxy
, &proxy
->notify
, ¬ify
.cap
);
1472 pci_register_bar(&proxy
->pci_dev
, proxy
->modern_mem_bar
,
1473 PCI_BASE_ADDRESS_SPACE_MEMORY
|
1474 PCI_BASE_ADDRESS_MEM_PREFETCH
|
1475 PCI_BASE_ADDRESS_MEM_TYPE_64
,
1476 &proxy
->modern_bar
);
1478 proxy
->config_cap
= virtio_pci_add_mem_cap(proxy
, &cfg
.cap
);
1479 cfg_mask
= (void *)(proxy
->pci_dev
.wmask
+ proxy
->config_cap
);
1480 pci_set_byte(&cfg_mask
->cap
.bar
, ~0x0);
1481 pci_set_long((uint8_t *)&cfg_mask
->cap
.offset
, ~0x0);
1482 pci_set_long((uint8_t *)&cfg_mask
->cap
.length
, ~0x0);
1483 pci_set_long(cfg_mask
->pci_cfg_data
, ~0x0);
1486 if (proxy
->nvectors
&&
1487 msix_init_exclusive_bar(&proxy
->pci_dev
, proxy
->nvectors
,
1489 error_report("unable to init msix vectors to %" PRIu32
,
1491 proxy
->nvectors
= 0;
1494 proxy
->pci_dev
.config_write
= virtio_write_config
;
1495 proxy
->pci_dev
.config_read
= virtio_read_config
;
1498 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
)
1499 + virtio_bus_get_vdev_config_len(bus
);
1500 if (size
& (size
- 1)) {
1501 size
= 1 << qemu_fls(size
);
1504 memory_region_init_io(&proxy
->bar
, OBJECT(proxy
),
1505 &virtio_pci_config_ops
,
1506 proxy
, "virtio-pci", size
);
1508 pci_register_bar(&proxy
->pci_dev
, proxy
->legacy_io_bar
,
1509 PCI_BASE_ADDRESS_SPACE_IO
, &proxy
->bar
);
1512 if (!kvm_has_many_ioeventfds()) {
1513 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
1516 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_BAD_FEATURE
);
1519 static void virtio_pci_device_unplugged(DeviceState
*d
)
1521 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1523 virtio_pci_stop_ioeventfd(proxy
);
1526 static void virtio_pci_realize(PCIDevice
*pci_dev
, Error
**errp
)
1528 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
1529 VirtioPCIClass
*k
= VIRTIO_PCI_GET_CLASS(pci_dev
);
1532 * virtio pci bar layout used by default.
1533 * subclasses can re-arrange things if needed.
1535 * region 0 -- virtio legacy io bar
1536 * region 1 -- msi-x bar
1537 * region 4+5 -- virtio modern memory (64bit) bar
1540 proxy
->legacy_io_bar
= 0;
1541 proxy
->msix_bar
= 1;
1542 proxy
->modern_mem_bar
= 4;
1544 proxy
->common
.offset
= 0x0;
1545 proxy
->common
.size
= 0x1000;
1546 proxy
->common
.type
= VIRTIO_PCI_CAP_COMMON_CFG
;
1548 proxy
->isr
.offset
= 0x1000;
1549 proxy
->isr
.size
= 0x1000;
1550 proxy
->isr
.type
= VIRTIO_PCI_CAP_ISR_CFG
;
1552 proxy
->device
.offset
= 0x2000;
1553 proxy
->device
.size
= 0x1000;
1554 proxy
->device
.type
= VIRTIO_PCI_CAP_DEVICE_CFG
;
1556 proxy
->notify
.offset
= 0x3000;
1557 proxy
->notify
.size
=
1558 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
* VIRTIO_QUEUE_MAX
;
1559 proxy
->notify
.type
= VIRTIO_PCI_CAP_NOTIFY_CFG
;
1561 /* subclasses can enforce modern, so do this unconditionally */
1562 memory_region_init(&proxy
->modern_bar
, OBJECT(proxy
), "virtio-pci",
1563 2 * QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
*
1566 memory_region_init_alias(&proxy
->modern_cfg
,
1571 memory_region_size(&proxy
->modern_bar
));
1573 address_space_init(&proxy
->modern_as
, &proxy
->modern_cfg
, "virtio-pci-cfg-as");
1575 virtio_pci_bus_new(&proxy
->bus
, sizeof(proxy
->bus
), proxy
);
1577 k
->realize(proxy
, errp
);
1581 static void virtio_pci_exit(PCIDevice
*pci_dev
)
1583 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
1585 msix_uninit_exclusive_bar(pci_dev
);
1586 address_space_destroy(&proxy
->modern_as
);
1589 static void virtio_pci_reset(DeviceState
*qdev
)
1591 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1592 VirtioBusState
*bus
= VIRTIO_BUS(&proxy
->bus
);
1593 virtio_pci_stop_ioeventfd(proxy
);
1594 virtio_bus_reset(bus
);
1595 msix_unuse_all_vectors(&proxy
->pci_dev
);
1598 static Property virtio_pci_properties
[] = {
1599 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy
, flags
,
1600 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT
, false),
1601 DEFINE_PROP_BIT("disable-legacy", VirtIOPCIProxy
, flags
,
1602 VIRTIO_PCI_FLAG_DISABLE_LEGACY_BIT
, false),
1603 DEFINE_PROP_BIT("disable-modern", VirtIOPCIProxy
, flags
,
1604 VIRTIO_PCI_FLAG_DISABLE_MODERN_BIT
, true),
1605 DEFINE_PROP_END_OF_LIST(),
1608 static void virtio_pci_class_init(ObjectClass
*klass
, void *data
)
1610 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1611 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1613 dc
->props
= virtio_pci_properties
;
1614 k
->realize
= virtio_pci_realize
;
1615 k
->exit
= virtio_pci_exit
;
1616 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1617 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1618 k
->class_id
= PCI_CLASS_OTHERS
;
1619 dc
->reset
= virtio_pci_reset
;
1622 static const TypeInfo virtio_pci_info
= {
1623 .name
= TYPE_VIRTIO_PCI
,
1624 .parent
= TYPE_PCI_DEVICE
,
1625 .instance_size
= sizeof(VirtIOPCIProxy
),
1626 .class_init
= virtio_pci_class_init
,
1627 .class_size
= sizeof(VirtioPCIClass
),
1631 /* virtio-blk-pci */
1633 static Property virtio_blk_pci_properties
[] = {
1634 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1635 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1636 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1637 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1638 DEFINE_PROP_END_OF_LIST(),
1641 static void virtio_blk_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1643 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(vpci_dev
);
1644 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1646 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1647 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1650 static void virtio_blk_pci_class_init(ObjectClass
*klass
, void *data
)
1652 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1653 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1654 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1656 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1657 dc
->props
= virtio_blk_pci_properties
;
1658 k
->realize
= virtio_blk_pci_realize
;
1659 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1660 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
1661 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1662 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1665 static void virtio_blk_pci_instance_init(Object
*obj
)
1667 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(obj
);
1669 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1671 object_property_add_alias(obj
, "iothread", OBJECT(&dev
->vdev
),"iothread",
1673 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
1674 "bootindex", &error_abort
);
1677 static const TypeInfo virtio_blk_pci_info
= {
1678 .name
= TYPE_VIRTIO_BLK_PCI
,
1679 .parent
= TYPE_VIRTIO_PCI
,
1680 .instance_size
= sizeof(VirtIOBlkPCI
),
1681 .instance_init
= virtio_blk_pci_instance_init
,
1682 .class_init
= virtio_blk_pci_class_init
,
1685 /* virtio-scsi-pci */
1687 static Property virtio_scsi_pci_properties
[] = {
1688 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1689 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1690 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1691 DEV_NVECTORS_UNSPECIFIED
),
1692 DEFINE_PROP_END_OF_LIST(),
1695 static void virtio_scsi_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1697 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(vpci_dev
);
1698 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1699 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1700 DeviceState
*proxy
= DEVICE(vpci_dev
);
1703 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1704 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1708 * For command line compatibility, this sets the virtio-scsi-device bus
1712 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1713 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1717 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1718 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1721 static void virtio_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1723 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1724 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1725 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1727 k
->realize
= virtio_scsi_pci_realize
;
1728 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1729 dc
->props
= virtio_scsi_pci_properties
;
1730 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1731 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1732 pcidev_k
->revision
= 0x00;
1733 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1736 static void virtio_scsi_pci_instance_init(Object
*obj
)
1738 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(obj
);
1740 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1742 object_property_add_alias(obj
, "iothread", OBJECT(&dev
->vdev
), "iothread",
1746 static const TypeInfo virtio_scsi_pci_info
= {
1747 .name
= TYPE_VIRTIO_SCSI_PCI
,
1748 .parent
= TYPE_VIRTIO_PCI
,
1749 .instance_size
= sizeof(VirtIOSCSIPCI
),
1750 .instance_init
= virtio_scsi_pci_instance_init
,
1751 .class_init
= virtio_scsi_pci_class_init
,
1754 /* vhost-scsi-pci */
1756 #ifdef CONFIG_VHOST_SCSI
1757 static Property vhost_scsi_pci_properties
[] = {
1758 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1759 DEV_NVECTORS_UNSPECIFIED
),
1760 DEFINE_PROP_END_OF_LIST(),
1763 static void vhost_scsi_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1765 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(vpci_dev
);
1766 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1767 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
1769 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1770 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
1773 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1774 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1777 static void vhost_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
1779 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1780 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1781 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1782 k
->realize
= vhost_scsi_pci_realize
;
1783 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1784 dc
->props
= vhost_scsi_pci_properties
;
1785 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1786 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
1787 pcidev_k
->revision
= 0x00;
1788 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1791 static void vhost_scsi_pci_instance_init(Object
*obj
)
1793 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(obj
);
1795 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1797 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
1798 "bootindex", &error_abort
);
1801 static const TypeInfo vhost_scsi_pci_info
= {
1802 .name
= TYPE_VHOST_SCSI_PCI
,
1803 .parent
= TYPE_VIRTIO_PCI
,
1804 .instance_size
= sizeof(VHostSCSIPCI
),
1805 .instance_init
= vhost_scsi_pci_instance_init
,
1806 .class_init
= vhost_scsi_pci_class_init
,
1810 /* virtio-balloon-pci */
1812 static Property virtio_balloon_pci_properties
[] = {
1813 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1814 DEFINE_PROP_END_OF_LIST(),
1817 static void virtio_balloon_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1819 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(vpci_dev
);
1820 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1822 if (vpci_dev
->class_code
!= PCI_CLASS_OTHERS
&&
1823 vpci_dev
->class_code
!= PCI_CLASS_MEMORY_RAM
) { /* qemu < 1.1 */
1824 vpci_dev
->class_code
= PCI_CLASS_OTHERS
;
1827 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1828 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1831 static void virtio_balloon_pci_class_init(ObjectClass
*klass
, void *data
)
1833 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1834 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1835 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1836 k
->realize
= virtio_balloon_pci_realize
;
1837 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1838 dc
->props
= virtio_balloon_pci_properties
;
1839 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1840 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BALLOON
;
1841 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1842 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
1845 static void virtio_balloon_pci_instance_init(Object
*obj
)
1847 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(obj
);
1849 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1850 TYPE_VIRTIO_BALLOON
);
1851 object_property_add_alias(obj
, "guest-stats", OBJECT(&dev
->vdev
),
1852 "guest-stats", &error_abort
);
1853 object_property_add_alias(obj
, "guest-stats-polling-interval",
1855 "guest-stats-polling-interval", &error_abort
);
1858 static const TypeInfo virtio_balloon_pci_info
= {
1859 .name
= TYPE_VIRTIO_BALLOON_PCI
,
1860 .parent
= TYPE_VIRTIO_PCI
,
1861 .instance_size
= sizeof(VirtIOBalloonPCI
),
1862 .instance_init
= virtio_balloon_pci_instance_init
,
1863 .class_init
= virtio_balloon_pci_class_init
,
1866 /* virtio-serial-pci */
1868 static void virtio_serial_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1870 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(vpci_dev
);
1871 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1872 DeviceState
*proxy
= DEVICE(vpci_dev
);
1875 if (vpci_dev
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
1876 vpci_dev
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
1877 vpci_dev
->class_code
!= PCI_CLASS_OTHERS
) { /* qemu-kvm */
1878 vpci_dev
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
1881 /* backwards-compatibility with machines that were created with
1882 DEV_NVECTORS_UNSPECIFIED */
1883 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1884 vpci_dev
->nvectors
= dev
->vdev
.serial
.max_virtserial_ports
+ 1;
1888 * For command line compatibility, this sets the virtio-serial-device bus
1892 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1893 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1897 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1898 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1901 static Property virtio_serial_pci_properties
[] = {
1902 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1903 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1904 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1905 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1906 DEFINE_PROP_END_OF_LIST(),
1909 static void virtio_serial_pci_class_init(ObjectClass
*klass
, void *data
)
1911 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1912 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1913 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1914 k
->realize
= virtio_serial_pci_realize
;
1915 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
1916 dc
->props
= virtio_serial_pci_properties
;
1917 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1918 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_CONSOLE
;
1919 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1920 pcidev_k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
1923 static void virtio_serial_pci_instance_init(Object
*obj
)
1925 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(obj
);
1927 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1928 TYPE_VIRTIO_SERIAL
);
1931 static const TypeInfo virtio_serial_pci_info
= {
1932 .name
= TYPE_VIRTIO_SERIAL_PCI
,
1933 .parent
= TYPE_VIRTIO_PCI
,
1934 .instance_size
= sizeof(VirtIOSerialPCI
),
1935 .instance_init
= virtio_serial_pci_instance_init
,
1936 .class_init
= virtio_serial_pci_class_init
,
1939 /* virtio-net-pci */
1941 static Property virtio_net_properties
[] = {
1942 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1943 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, false),
1944 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
1945 DEFINE_PROP_END_OF_LIST(),
1948 static void virtio_net_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1950 DeviceState
*qdev
= DEVICE(vpci_dev
);
1951 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(vpci_dev
);
1952 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1954 virtio_net_set_netclient_name(&dev
->vdev
, qdev
->id
,
1955 object_get_typename(OBJECT(qdev
)));
1956 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1957 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1960 static void virtio_net_pci_class_init(ObjectClass
*klass
, void *data
)
1962 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1963 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1964 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_CLASS(klass
);
1966 k
->romfile
= "efi-virtio.rom";
1967 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1968 k
->device_id
= PCI_DEVICE_ID_VIRTIO_NET
;
1969 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1970 k
->class_id
= PCI_CLASS_NETWORK_ETHERNET
;
1971 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
1972 dc
->props
= virtio_net_properties
;
1973 vpciklass
->realize
= virtio_net_pci_realize
;
1976 static void virtio_net_pci_instance_init(Object
*obj
)
1978 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(obj
);
1980 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1982 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
1983 "bootindex", &error_abort
);
1986 static const TypeInfo virtio_net_pci_info
= {
1987 .name
= TYPE_VIRTIO_NET_PCI
,
1988 .parent
= TYPE_VIRTIO_PCI
,
1989 .instance_size
= sizeof(VirtIONetPCI
),
1990 .instance_init
= virtio_net_pci_instance_init
,
1991 .class_init
= virtio_net_pci_class_init
,
1994 /* virtio-rng-pci */
1996 static Property virtio_rng_pci_properties
[] = {
1997 DEFINE_PROP_END_OF_LIST(),
2000 static void virtio_rng_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2002 VirtIORngPCI
*vrng
= VIRTIO_RNG_PCI(vpci_dev
);
2003 DeviceState
*vdev
= DEVICE(&vrng
->vdev
);
2006 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2007 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
2009 error_propagate(errp
, err
);
2013 object_property_set_link(OBJECT(vrng
),
2014 OBJECT(vrng
->vdev
.conf
.rng
), "rng",
2018 static void virtio_rng_pci_class_init(ObjectClass
*klass
, void *data
)
2020 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2021 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2022 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2024 k
->realize
= virtio_rng_pci_realize
;
2025 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
2026 dc
->props
= virtio_rng_pci_properties
;
2028 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2029 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_RNG
;
2030 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
2031 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
2034 static void virtio_rng_initfn(Object
*obj
)
2036 VirtIORngPCI
*dev
= VIRTIO_RNG_PCI(obj
);
2038 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2040 object_property_add_alias(obj
, "rng", OBJECT(&dev
->vdev
), "rng",
2044 static const TypeInfo virtio_rng_pci_info
= {
2045 .name
= TYPE_VIRTIO_RNG_PCI
,
2046 .parent
= TYPE_VIRTIO_PCI
,
2047 .instance_size
= sizeof(VirtIORngPCI
),
2048 .instance_init
= virtio_rng_initfn
,
2049 .class_init
= virtio_rng_pci_class_init
,
2052 /* virtio-input-pci */
2054 static Property virtio_input_pci_properties
[] = {
2055 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
2056 DEFINE_PROP_END_OF_LIST(),
2059 static void virtio_input_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2061 VirtIOInputPCI
*vinput
= VIRTIO_INPUT_PCI(vpci_dev
);
2062 DeviceState
*vdev
= DEVICE(&vinput
->vdev
);
2064 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2065 /* force virtio-1.0 */
2066 vpci_dev
->flags
&= ~VIRTIO_PCI_FLAG_DISABLE_MODERN
;
2067 vpci_dev
->flags
|= VIRTIO_PCI_FLAG_DISABLE_LEGACY
;
2068 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2071 static void virtio_input_pci_class_init(ObjectClass
*klass
, void *data
)
2073 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2074 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2075 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2077 dc
->props
= virtio_input_pci_properties
;
2078 k
->realize
= virtio_input_pci_realize
;
2079 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
2081 pcidev_k
->class_id
= PCI_CLASS_INPUT_OTHER
;
2084 static void virtio_input_hid_kbd_pci_class_init(ObjectClass
*klass
, void *data
)
2086 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2088 pcidev_k
->class_id
= PCI_CLASS_INPUT_KEYBOARD
;
2091 static void virtio_input_hid_mouse_pci_class_init(ObjectClass
*klass
,
2094 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2096 pcidev_k
->class_id
= PCI_CLASS_INPUT_MOUSE
;
2099 static void virtio_keyboard_initfn(Object
*obj
)
2101 VirtIOInputHIDPCI
*dev
= VIRTIO_INPUT_HID_PCI(obj
);
2103 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2104 TYPE_VIRTIO_KEYBOARD
);
2107 static void virtio_mouse_initfn(Object
*obj
)
2109 VirtIOInputHIDPCI
*dev
= VIRTIO_INPUT_HID_PCI(obj
);
2111 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2115 static void virtio_tablet_initfn(Object
*obj
)
2117 VirtIOInputHIDPCI
*dev
= VIRTIO_INPUT_HID_PCI(obj
);
2119 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2120 TYPE_VIRTIO_TABLET
);
2123 static void virtio_host_initfn(Object
*obj
)
2125 VirtIOInputHostPCI
*dev
= VIRTIO_INPUT_HOST_PCI(obj
);
2127 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2128 TYPE_VIRTIO_INPUT_HOST
);
2131 static const TypeInfo virtio_input_pci_info
= {
2132 .name
= TYPE_VIRTIO_INPUT_PCI
,
2133 .parent
= TYPE_VIRTIO_PCI
,
2134 .instance_size
= sizeof(VirtIOInputPCI
),
2135 .class_init
= virtio_input_pci_class_init
,
2139 static const TypeInfo virtio_input_hid_pci_info
= {
2140 .name
= TYPE_VIRTIO_INPUT_HID_PCI
,
2141 .parent
= TYPE_VIRTIO_INPUT_PCI
,
2142 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2146 static const TypeInfo virtio_keyboard_pci_info
= {
2147 .name
= TYPE_VIRTIO_KEYBOARD_PCI
,
2148 .parent
= TYPE_VIRTIO_INPUT_HID_PCI
,
2149 .class_init
= virtio_input_hid_kbd_pci_class_init
,
2150 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2151 .instance_init
= virtio_keyboard_initfn
,
2154 static const TypeInfo virtio_mouse_pci_info
= {
2155 .name
= TYPE_VIRTIO_MOUSE_PCI
,
2156 .parent
= TYPE_VIRTIO_INPUT_HID_PCI
,
2157 .class_init
= virtio_input_hid_mouse_pci_class_init
,
2158 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2159 .instance_init
= virtio_mouse_initfn
,
2162 static const TypeInfo virtio_tablet_pci_info
= {
2163 .name
= TYPE_VIRTIO_TABLET_PCI
,
2164 .parent
= TYPE_VIRTIO_INPUT_HID_PCI
,
2165 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2166 .instance_init
= virtio_tablet_initfn
,
2169 static const TypeInfo virtio_host_pci_info
= {
2170 .name
= TYPE_VIRTIO_INPUT_HOST_PCI
,
2171 .parent
= TYPE_VIRTIO_INPUT_PCI
,
2172 .instance_size
= sizeof(VirtIOInputHostPCI
),
2173 .instance_init
= virtio_host_initfn
,
2176 /* virtio-pci-bus */
2178 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
2179 VirtIOPCIProxy
*dev
)
2181 DeviceState
*qdev
= DEVICE(dev
);
2182 char virtio_bus_name
[] = "virtio-bus";
2184 qbus_create_inplace(bus
, bus_size
, TYPE_VIRTIO_PCI_BUS
, qdev
,
2188 static void virtio_pci_bus_class_init(ObjectClass
*klass
, void *data
)
2190 BusClass
*bus_class
= BUS_CLASS(klass
);
2191 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
2192 bus_class
->max_dev
= 1;
2193 k
->notify
= virtio_pci_notify
;
2194 k
->save_config
= virtio_pci_save_config
;
2195 k
->load_config
= virtio_pci_load_config
;
2196 k
->save_queue
= virtio_pci_save_queue
;
2197 k
->load_queue
= virtio_pci_load_queue
;
2198 k
->query_guest_notifiers
= virtio_pci_query_guest_notifiers
;
2199 k
->set_host_notifier
= virtio_pci_set_host_notifier
;
2200 k
->set_guest_notifiers
= virtio_pci_set_guest_notifiers
;
2201 k
->vmstate_change
= virtio_pci_vmstate_change
;
2202 k
->device_plugged
= virtio_pci_device_plugged
;
2203 k
->device_unplugged
= virtio_pci_device_unplugged
;
2204 k
->query_nvectors
= virtio_pci_query_nvectors
;
2207 static const TypeInfo virtio_pci_bus_info
= {
2208 .name
= TYPE_VIRTIO_PCI_BUS
,
2209 .parent
= TYPE_VIRTIO_BUS
,
2210 .instance_size
= sizeof(VirtioPCIBusState
),
2211 .class_init
= virtio_pci_bus_class_init
,
2214 static void virtio_pci_register_types(void)
2216 type_register_static(&virtio_rng_pci_info
);
2217 type_register_static(&virtio_input_pci_info
);
2218 type_register_static(&virtio_input_hid_pci_info
);
2219 type_register_static(&virtio_keyboard_pci_info
);
2220 type_register_static(&virtio_mouse_pci_info
);
2221 type_register_static(&virtio_tablet_pci_info
);
2222 type_register_static(&virtio_host_pci_info
);
2223 type_register_static(&virtio_pci_bus_info
);
2224 type_register_static(&virtio_pci_info
);
2225 #ifdef CONFIG_VIRTFS
2226 type_register_static(&virtio_9p_pci_info
);
2228 type_register_static(&virtio_blk_pci_info
);
2229 type_register_static(&virtio_scsi_pci_info
);
2230 type_register_static(&virtio_balloon_pci_info
);
2231 type_register_static(&virtio_serial_pci_info
);
2232 type_register_static(&virtio_net_pci_info
);
2233 #ifdef CONFIG_VHOST_SCSI
2234 type_register_static(&vhost_scsi_pci_info
);
2238 type_init(virtio_pci_register_types
)