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.
18 #include "qemu/osdep.h"
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 "qapi/error.h"
30 #include "qemu/error-report.h"
31 #include "hw/pci/msi.h"
32 #include "hw/pci/msix.h"
33 #include "hw/loader.h"
34 #include "sysemu/kvm.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
,
50 static void virtio_pci_reset(DeviceState
*qdev
);
53 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
54 static inline VirtIOPCIProxy
*to_virtio_pci_proxy(DeviceState
*d
)
56 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
59 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
60 * be careful and test performance if you change this.
62 static inline VirtIOPCIProxy
*to_virtio_pci_proxy_fast(DeviceState
*d
)
64 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
67 static void virtio_pci_notify(DeviceState
*d
, uint16_t vector
)
69 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy_fast(d
);
71 if (msix_enabled(&proxy
->pci_dev
))
72 msix_notify(&proxy
->pci_dev
, vector
);
74 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
75 pci_set_irq(&proxy
->pci_dev
, atomic_read(&vdev
->isr
) & 1);
79 static void virtio_pci_save_config(DeviceState
*d
, QEMUFile
*f
)
81 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
82 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
84 pci_device_save(&proxy
->pci_dev
, f
);
85 msix_save(&proxy
->pci_dev
, f
);
86 if (msix_present(&proxy
->pci_dev
))
87 qemu_put_be16(f
, vdev
->config_vector
);
90 static const VMStateDescription vmstate_virtio_pci_modern_queue_state
= {
91 .name
= "virtio_pci/modern_queue_state",
93 .minimum_version_id
= 1,
94 .fields
= (VMStateField
[]) {
95 VMSTATE_UINT16(num
, VirtIOPCIQueue
),
96 VMSTATE_UNUSED(1), /* enabled was stored as be16 */
97 VMSTATE_BOOL(enabled
, VirtIOPCIQueue
),
98 VMSTATE_UINT32_ARRAY(desc
, VirtIOPCIQueue
, 2),
99 VMSTATE_UINT32_ARRAY(avail
, VirtIOPCIQueue
, 2),
100 VMSTATE_UINT32_ARRAY(used
, VirtIOPCIQueue
, 2),
101 VMSTATE_END_OF_LIST()
105 static bool virtio_pci_modern_state_needed(void *opaque
)
107 VirtIOPCIProxy
*proxy
= opaque
;
109 return virtio_pci_modern(proxy
);
112 static const VMStateDescription vmstate_virtio_pci_modern_state_sub
= {
113 .name
= "virtio_pci/modern_state",
115 .minimum_version_id
= 1,
116 .needed
= &virtio_pci_modern_state_needed
,
117 .fields
= (VMStateField
[]) {
118 VMSTATE_UINT32(dfselect
, VirtIOPCIProxy
),
119 VMSTATE_UINT32(gfselect
, VirtIOPCIProxy
),
120 VMSTATE_UINT32_ARRAY(guest_features
, VirtIOPCIProxy
, 2),
121 VMSTATE_STRUCT_ARRAY(vqs
, VirtIOPCIProxy
, VIRTIO_QUEUE_MAX
, 0,
122 vmstate_virtio_pci_modern_queue_state
,
124 VMSTATE_END_OF_LIST()
128 static const VMStateDescription vmstate_virtio_pci
= {
129 .name
= "virtio_pci",
131 .minimum_version_id
= 1,
132 .minimum_version_id_old
= 1,
133 .fields
= (VMStateField
[]) {
134 VMSTATE_END_OF_LIST()
136 .subsections
= (const VMStateDescription
*[]) {
137 &vmstate_virtio_pci_modern_state_sub
,
142 static bool virtio_pci_has_extra_state(DeviceState
*d
)
144 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
146 return proxy
->flags
& VIRTIO_PCI_FLAG_MIGRATE_EXTRA
;
149 static void virtio_pci_save_extra_state(DeviceState
*d
, QEMUFile
*f
)
151 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
153 vmstate_save_state(f
, &vmstate_virtio_pci
, proxy
, NULL
);
156 static int virtio_pci_load_extra_state(DeviceState
*d
, QEMUFile
*f
)
158 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
160 return vmstate_load_state(f
, &vmstate_virtio_pci
, proxy
, 1);
163 static void virtio_pci_save_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
165 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
166 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
168 if (msix_present(&proxy
->pci_dev
))
169 qemu_put_be16(f
, virtio_queue_vector(vdev
, n
));
172 static int virtio_pci_load_config(DeviceState
*d
, QEMUFile
*f
)
174 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
175 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
178 ret
= pci_device_load(&proxy
->pci_dev
, f
);
182 msix_unuse_all_vectors(&proxy
->pci_dev
);
183 msix_load(&proxy
->pci_dev
, f
);
184 if (msix_present(&proxy
->pci_dev
)) {
185 qemu_get_be16s(f
, &vdev
->config_vector
);
187 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
189 if (vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
190 return msix_vector_use(&proxy
->pci_dev
, vdev
->config_vector
);
195 static int virtio_pci_load_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
197 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
198 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
201 if (msix_present(&proxy
->pci_dev
)) {
202 qemu_get_be16s(f
, &vector
);
204 vector
= VIRTIO_NO_VECTOR
;
206 virtio_queue_set_vector(vdev
, n
, vector
);
207 if (vector
!= VIRTIO_NO_VECTOR
) {
208 return msix_vector_use(&proxy
->pci_dev
, vector
);
214 static bool virtio_pci_ioeventfd_enabled(DeviceState
*d
)
216 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
218 return (proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) != 0;
221 #define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000
223 static inline int virtio_pci_queue_mem_mult(struct VirtIOPCIProxy
*proxy
)
225 return (proxy
->flags
& VIRTIO_PCI_FLAG_PAGE_PER_VQ
) ?
226 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
: 4;
229 static int virtio_pci_ioeventfd_assign(DeviceState
*d
, EventNotifier
*notifier
,
232 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
233 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
234 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
235 bool legacy
= virtio_pci_legacy(proxy
);
236 bool modern
= virtio_pci_modern(proxy
);
237 bool fast_mmio
= kvm_ioeventfd_any_length_enabled();
238 bool modern_pio
= proxy
->flags
& VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY
;
239 MemoryRegion
*modern_mr
= &proxy
->notify
.mr
;
240 MemoryRegion
*modern_notify_mr
= &proxy
->notify_pio
.mr
;
241 MemoryRegion
*legacy_mr
= &proxy
->bar
;
242 hwaddr modern_addr
= virtio_pci_queue_mem_mult(proxy
) *
243 virtio_get_queue_index(vq
);
244 hwaddr legacy_addr
= VIRTIO_PCI_QUEUE_NOTIFY
;
249 memory_region_add_eventfd(modern_mr
, modern_addr
, 0,
252 memory_region_add_eventfd(modern_mr
, modern_addr
, 2,
256 memory_region_add_eventfd(modern_notify_mr
, 0, 2,
261 memory_region_add_eventfd(legacy_mr
, legacy_addr
, 2,
267 memory_region_del_eventfd(modern_mr
, modern_addr
, 0,
270 memory_region_del_eventfd(modern_mr
, modern_addr
, 2,
274 memory_region_del_eventfd(modern_notify_mr
, 0, 2,
279 memory_region_del_eventfd(legacy_mr
, legacy_addr
, 2,
286 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
288 virtio_bus_start_ioeventfd(&proxy
->bus
);
291 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
293 virtio_bus_stop_ioeventfd(&proxy
->bus
);
296 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
298 VirtIOPCIProxy
*proxy
= opaque
;
299 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
303 case VIRTIO_PCI_GUEST_FEATURES
:
304 /* Guest does not negotiate properly? We have to assume nothing. */
305 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
306 val
= virtio_bus_get_vdev_bad_features(&proxy
->bus
);
308 virtio_set_features(vdev
, val
);
310 case VIRTIO_PCI_QUEUE_PFN
:
311 pa
= (hwaddr
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
313 virtio_pci_reset(DEVICE(proxy
));
316 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
318 case VIRTIO_PCI_QUEUE_SEL
:
319 if (val
< VIRTIO_QUEUE_MAX
)
320 vdev
->queue_sel
= val
;
322 case VIRTIO_PCI_QUEUE_NOTIFY
:
323 if (val
< VIRTIO_QUEUE_MAX
) {
324 virtio_queue_notify(vdev
, val
);
327 case VIRTIO_PCI_STATUS
:
328 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
329 virtio_pci_stop_ioeventfd(proxy
);
332 virtio_set_status(vdev
, val
& 0xFF);
334 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
335 virtio_pci_start_ioeventfd(proxy
);
338 if (vdev
->status
== 0) {
339 virtio_pci_reset(DEVICE(proxy
));
342 /* Linux before 2.6.34 drives the device without enabling
343 the PCI device bus master bit. Enable it automatically
344 for the guest. This is a PCI spec violation but so is
345 initiating DMA with bus master bit clear. */
346 if (val
== (VIRTIO_CONFIG_S_ACKNOWLEDGE
| VIRTIO_CONFIG_S_DRIVER
)) {
347 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
348 proxy
->pci_dev
.config
[PCI_COMMAND
] |
349 PCI_COMMAND_MASTER
, 1);
352 case VIRTIO_MSI_CONFIG_VECTOR
:
353 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
354 /* Make it possible for guest to discover an error took place. */
355 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
356 val
= VIRTIO_NO_VECTOR
;
357 vdev
->config_vector
= val
;
359 case VIRTIO_MSI_QUEUE_VECTOR
:
360 msix_vector_unuse(&proxy
->pci_dev
,
361 virtio_queue_vector(vdev
, vdev
->queue_sel
));
362 /* Make it possible for guest to discover an error took place. */
363 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
364 val
= VIRTIO_NO_VECTOR
;
365 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
368 error_report("%s: unexpected address 0x%x value 0x%x",
369 __func__
, addr
, val
);
374 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
376 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
377 uint32_t ret
= 0xFFFFFFFF;
380 case VIRTIO_PCI_HOST_FEATURES
:
381 ret
= vdev
->host_features
;
383 case VIRTIO_PCI_GUEST_FEATURES
:
384 ret
= vdev
->guest_features
;
386 case VIRTIO_PCI_QUEUE_PFN
:
387 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
388 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
390 case VIRTIO_PCI_QUEUE_NUM
:
391 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
393 case VIRTIO_PCI_QUEUE_SEL
:
394 ret
= vdev
->queue_sel
;
396 case VIRTIO_PCI_STATUS
:
400 /* reading from the ISR also clears it. */
401 ret
= atomic_xchg(&vdev
->isr
, 0);
402 pci_irq_deassert(&proxy
->pci_dev
);
404 case VIRTIO_MSI_CONFIG_VECTOR
:
405 ret
= vdev
->config_vector
;
407 case VIRTIO_MSI_QUEUE_VECTOR
:
408 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
417 static uint64_t virtio_pci_config_read(void *opaque
, hwaddr addr
,
420 VirtIOPCIProxy
*proxy
= opaque
;
421 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
422 uint32_t config
= VIRTIO_PCI_CONFIG_SIZE(&proxy
->pci_dev
);
425 return virtio_ioport_read(proxy
, addr
);
431 val
= virtio_config_readb(vdev
, addr
);
434 val
= virtio_config_readw(vdev
, addr
);
435 if (virtio_is_big_endian(vdev
)) {
440 val
= virtio_config_readl(vdev
, addr
);
441 if (virtio_is_big_endian(vdev
)) {
449 static void virtio_pci_config_write(void *opaque
, hwaddr addr
,
450 uint64_t val
, unsigned size
)
452 VirtIOPCIProxy
*proxy
= opaque
;
453 uint32_t config
= VIRTIO_PCI_CONFIG_SIZE(&proxy
->pci_dev
);
454 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
456 virtio_ioport_write(proxy
, addr
, val
);
461 * Virtio-PCI is odd. Ioports are LE but config space is target native
466 virtio_config_writeb(vdev
, addr
, val
);
469 if (virtio_is_big_endian(vdev
)) {
472 virtio_config_writew(vdev
, addr
, val
);
475 if (virtio_is_big_endian(vdev
)) {
478 virtio_config_writel(vdev
, addr
, val
);
483 static const MemoryRegionOps virtio_pci_config_ops
= {
484 .read
= virtio_pci_config_read
,
485 .write
= virtio_pci_config_write
,
487 .min_access_size
= 1,
488 .max_access_size
= 4,
490 .endianness
= DEVICE_LITTLE_ENDIAN
,
493 static MemoryRegion
*virtio_address_space_lookup(VirtIOPCIProxy
*proxy
,
494 hwaddr
*off
, int len
)
497 VirtIOPCIRegion
*reg
;
499 for (i
= 0; i
< ARRAY_SIZE(proxy
->regs
); ++i
) {
500 reg
= &proxy
->regs
[i
];
501 if (*off
>= reg
->offset
&&
502 *off
+ len
<= reg
->offset
+ reg
->size
) {
511 /* Below are generic functions to do memcpy from/to an address space,
512 * without byteswaps, with input validation.
514 * As regular address_space_* APIs all do some kind of byteswap at least for
515 * some host/target combinations, we are forced to explicitly convert to a
516 * known-endianness integer value.
517 * It doesn't really matter which endian format to go through, so the code
518 * below selects the endian that causes the least amount of work on the given
521 * Note: host pointer must be aligned.
524 void virtio_address_space_write(VirtIOPCIProxy
*proxy
, hwaddr addr
,
525 const uint8_t *buf
, int len
)
530 /* address_space_* APIs assume an aligned address.
531 * As address is under guest control, handle illegal values.
535 mr
= virtio_address_space_lookup(proxy
, &addr
, len
);
540 /* Make sure caller aligned buf properly */
541 assert(!(((uintptr_t)buf
) & (len
- 1)));
545 val
= pci_get_byte(buf
);
548 val
= cpu_to_le16(pci_get_word(buf
));
551 val
= cpu_to_le32(pci_get_long(buf
));
554 /* As length is under guest control, handle illegal values. */
557 memory_region_dispatch_write(mr
, addr
, val
, len
, MEMTXATTRS_UNSPECIFIED
);
561 virtio_address_space_read(VirtIOPCIProxy
*proxy
, hwaddr addr
,
562 uint8_t *buf
, int len
)
567 /* address_space_* APIs assume an aligned address.
568 * As address is under guest control, handle illegal values.
572 mr
= virtio_address_space_lookup(proxy
, &addr
, len
);
577 /* Make sure caller aligned buf properly */
578 assert(!(((uintptr_t)buf
) & (len
- 1)));
580 memory_region_dispatch_read(mr
, addr
, &val
, len
, MEMTXATTRS_UNSPECIFIED
);
583 pci_set_byte(buf
, val
);
586 pci_set_word(buf
, le16_to_cpu(val
));
589 pci_set_long(buf
, le32_to_cpu(val
));
592 /* As length is under guest control, handle illegal values. */
597 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
598 uint32_t val
, int len
)
600 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
601 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
602 struct virtio_pci_cfg_cap
*cfg
;
604 pci_default_write_config(pci_dev
, address
, val
, len
);
606 if (range_covers_byte(address
, len
, PCI_COMMAND
) &&
607 !(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
608 virtio_pci_stop_ioeventfd(proxy
);
609 virtio_set_status(vdev
, vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
612 if (proxy
->config_cap
&&
613 ranges_overlap(address
, len
, proxy
->config_cap
+ offsetof(struct virtio_pci_cfg_cap
,
615 sizeof cfg
->pci_cfg_data
)) {
619 cfg
= (void *)(proxy
->pci_dev
.config
+ proxy
->config_cap
);
620 off
= le32_to_cpu(cfg
->cap
.offset
);
621 len
= le32_to_cpu(cfg
->cap
.length
);
623 if (len
== 1 || len
== 2 || len
== 4) {
624 assert(len
<= sizeof cfg
->pci_cfg_data
);
625 virtio_address_space_write(proxy
, off
, cfg
->pci_cfg_data
, len
);
630 static uint32_t virtio_read_config(PCIDevice
*pci_dev
,
631 uint32_t address
, int len
)
633 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
634 struct virtio_pci_cfg_cap
*cfg
;
636 if (proxy
->config_cap
&&
637 ranges_overlap(address
, len
, proxy
->config_cap
+ offsetof(struct virtio_pci_cfg_cap
,
639 sizeof cfg
->pci_cfg_data
)) {
643 cfg
= (void *)(proxy
->pci_dev
.config
+ proxy
->config_cap
);
644 off
= le32_to_cpu(cfg
->cap
.offset
);
645 len
= le32_to_cpu(cfg
->cap
.length
);
647 if (len
== 1 || len
== 2 || len
== 4) {
648 assert(len
<= sizeof cfg
->pci_cfg_data
);
649 virtio_address_space_read(proxy
, off
, cfg
->pci_cfg_data
, len
);
653 return pci_default_read_config(pci_dev
, address
, len
);
656 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy
*proxy
,
657 unsigned int queue_no
,
660 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
663 if (irqfd
->users
== 0) {
664 ret
= kvm_irqchip_add_msi_route(kvm_state
, vector
, &proxy
->pci_dev
);
674 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy
*proxy
,
677 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
678 if (--irqfd
->users
== 0) {
679 kvm_irqchip_release_virq(kvm_state
, irqfd
->virq
);
683 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy
*proxy
,
684 unsigned int queue_no
,
687 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
688 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
689 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
690 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
691 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, n
, NULL
, irqfd
->virq
);
694 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy
*proxy
,
695 unsigned int queue_no
,
698 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
699 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
700 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
701 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
704 ret
= kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state
, n
, irqfd
->virq
);
708 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy
*proxy
, int nvqs
)
710 PCIDevice
*dev
= &proxy
->pci_dev
;
711 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
712 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
716 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
717 if (!virtio_queue_get_num(vdev
, queue_no
)) {
720 vector
= virtio_queue_vector(vdev
, queue_no
);
721 if (vector
>= msix_nr_vectors_allocated(dev
)) {
724 ret
= kvm_virtio_pci_vq_vector_use(proxy
, queue_no
, vector
);
728 /* If guest supports masking, set up irqfd now.
729 * Otherwise, delay until unmasked in the frontend.
731 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
732 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
734 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
742 while (--queue_no
>= 0) {
743 vector
= virtio_queue_vector(vdev
, queue_no
);
744 if (vector
>= msix_nr_vectors_allocated(dev
)) {
747 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
748 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
750 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
755 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy
*proxy
, int nvqs
)
757 PCIDevice
*dev
= &proxy
->pci_dev
;
758 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
761 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
763 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
764 if (!virtio_queue_get_num(vdev
, queue_no
)) {
767 vector
= virtio_queue_vector(vdev
, queue_no
);
768 if (vector
>= msix_nr_vectors_allocated(dev
)) {
771 /* If guest supports masking, clean up irqfd now.
772 * Otherwise, it was cleaned when masked in the frontend.
774 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
775 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
777 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
781 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy
*proxy
,
782 unsigned int queue_no
,
786 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
787 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
788 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
789 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
793 if (proxy
->vector_irqfd
) {
794 irqfd
= &proxy
->vector_irqfd
[vector
];
795 if (irqfd
->msg
.data
!= msg
.data
|| irqfd
->msg
.address
!= msg
.address
) {
796 ret
= kvm_irqchip_update_msi_route(kvm_state
, irqfd
->virq
, msg
,
801 kvm_irqchip_commit_routes(kvm_state
);
805 /* If guest supports masking, irqfd is already setup, unmask it.
806 * Otherwise, set it up now.
808 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
809 k
->guest_notifier_mask(vdev
, queue_no
, false);
810 /* Test after unmasking to avoid losing events. */
811 if (k
->guest_notifier_pending
&&
812 k
->guest_notifier_pending(vdev
, queue_no
)) {
813 event_notifier_set(n
);
816 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
821 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy
*proxy
,
822 unsigned int queue_no
,
825 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
826 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
828 /* If guest supports masking, keep irqfd but mask it.
829 * Otherwise, clean it up now.
831 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
832 k
->guest_notifier_mask(vdev
, queue_no
, true);
834 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
838 static int virtio_pci_vector_unmask(PCIDevice
*dev
, unsigned vector
,
841 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
842 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
843 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
844 int ret
, index
, unmasked
= 0;
847 index
= virtio_get_queue_index(vq
);
848 if (!virtio_queue_get_num(vdev
, index
)) {
851 if (index
< proxy
->nvqs_with_notifiers
) {
852 ret
= virtio_pci_vq_vector_unmask(proxy
, index
, vector
, msg
);
858 vq
= virtio_vector_next_queue(vq
);
864 vq
= virtio_vector_first_queue(vdev
, vector
);
865 while (vq
&& unmasked
>= 0) {
866 index
= virtio_get_queue_index(vq
);
867 if (index
< proxy
->nvqs_with_notifiers
) {
868 virtio_pci_vq_vector_mask(proxy
, index
, vector
);
871 vq
= virtio_vector_next_queue(vq
);
876 static void virtio_pci_vector_mask(PCIDevice
*dev
, unsigned vector
)
878 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
879 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
880 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
884 index
= virtio_get_queue_index(vq
);
885 if (!virtio_queue_get_num(vdev
, index
)) {
888 if (index
< proxy
->nvqs_with_notifiers
) {
889 virtio_pci_vq_vector_mask(proxy
, index
, vector
);
891 vq
= virtio_vector_next_queue(vq
);
895 static void virtio_pci_vector_poll(PCIDevice
*dev
,
896 unsigned int vector_start
,
897 unsigned int vector_end
)
899 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
900 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
901 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
904 EventNotifier
*notifier
;
907 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
908 if (!virtio_queue_get_num(vdev
, queue_no
)) {
911 vector
= virtio_queue_vector(vdev
, queue_no
);
912 if (vector
< vector_start
|| vector
>= vector_end
||
913 !msix_is_masked(dev
, vector
)) {
916 vq
= virtio_get_queue(vdev
, queue_no
);
917 notifier
= virtio_queue_get_guest_notifier(vq
);
918 if (k
->guest_notifier_pending
) {
919 if (k
->guest_notifier_pending(vdev
, queue_no
)) {
920 msix_set_pending(dev
, vector
);
922 } else if (event_notifier_test_and_clear(notifier
)) {
923 msix_set_pending(dev
, vector
);
928 static int virtio_pci_set_guest_notifier(DeviceState
*d
, int n
, bool assign
,
931 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
932 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
933 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
934 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
935 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
938 int r
= event_notifier_init(notifier
, 0);
942 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
944 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
945 event_notifier_cleanup(notifier
);
948 if (!msix_enabled(&proxy
->pci_dev
) &&
949 vdev
->use_guest_notifier_mask
&&
950 vdc
->guest_notifier_mask
) {
951 vdc
->guest_notifier_mask(vdev
, n
, !assign
);
957 static bool virtio_pci_query_guest_notifiers(DeviceState
*d
)
959 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
960 return msix_enabled(&proxy
->pci_dev
);
963 static int virtio_pci_set_guest_notifiers(DeviceState
*d
, int nvqs
, bool assign
)
965 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
966 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
967 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
969 bool with_irqfd
= msix_enabled(&proxy
->pci_dev
) &&
970 kvm_msi_via_irqfd_enabled();
972 nvqs
= MIN(nvqs
, VIRTIO_QUEUE_MAX
);
974 /* When deassigning, pass a consistent nvqs value
975 * to avoid leaking notifiers.
977 assert(assign
|| nvqs
== proxy
->nvqs_with_notifiers
);
979 proxy
->nvqs_with_notifiers
= nvqs
;
981 /* Must unset vector notifier while guest notifier is still assigned */
982 if ((proxy
->vector_irqfd
|| k
->guest_notifier_mask
) && !assign
) {
983 msix_unset_vector_notifiers(&proxy
->pci_dev
);
984 if (proxy
->vector_irqfd
) {
985 kvm_virtio_pci_vector_release(proxy
, nvqs
);
986 g_free(proxy
->vector_irqfd
);
987 proxy
->vector_irqfd
= NULL
;
991 for (n
= 0; n
< nvqs
; n
++) {
992 if (!virtio_queue_get_num(vdev
, n
)) {
996 r
= virtio_pci_set_guest_notifier(d
, n
, assign
, with_irqfd
);
1002 /* Must set vector notifier after guest notifier has been assigned */
1003 if ((with_irqfd
|| k
->guest_notifier_mask
) && assign
) {
1005 proxy
->vector_irqfd
=
1006 g_malloc0(sizeof(*proxy
->vector_irqfd
) *
1007 msix_nr_vectors_allocated(&proxy
->pci_dev
));
1008 r
= kvm_virtio_pci_vector_use(proxy
, nvqs
);
1013 r
= msix_set_vector_notifiers(&proxy
->pci_dev
,
1014 virtio_pci_vector_unmask
,
1015 virtio_pci_vector_mask
,
1016 virtio_pci_vector_poll
);
1018 goto notifiers_error
;
1027 kvm_virtio_pci_vector_release(proxy
, nvqs
);
1031 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
1034 virtio_pci_set_guest_notifier(d
, n
, !assign
, with_irqfd
);
1039 static int virtio_pci_set_host_notifier_mr(DeviceState
*d
, int n
,
1040 MemoryRegion
*mr
, bool assign
)
1042 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1045 if (n
>= VIRTIO_QUEUE_MAX
|| !virtio_pci_modern(proxy
) ||
1046 virtio_pci_queue_mem_mult(proxy
) != memory_region_size(mr
)) {
1051 offset
= virtio_pci_queue_mem_mult(proxy
) * n
;
1052 memory_region_add_subregion_overlap(&proxy
->notify
.mr
, offset
, mr
, 1);
1054 memory_region_del_subregion(&proxy
->notify
.mr
, mr
);
1060 static void virtio_pci_vmstate_change(DeviceState
*d
, bool running
)
1062 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1063 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1066 /* Old QEMU versions did not set bus master enable on status write.
1067 * Detect DRIVER set and enable it.
1069 if ((proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION
) &&
1070 (vdev
->status
& VIRTIO_CONFIG_S_DRIVER
) &&
1071 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
1072 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
1073 proxy
->pci_dev
.config
[PCI_COMMAND
] |
1074 PCI_COMMAND_MASTER
, 1);
1076 virtio_pci_start_ioeventfd(proxy
);
1078 virtio_pci_stop_ioeventfd(proxy
);
1082 #ifdef CONFIG_VIRTFS
1083 static void virtio_9p_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1085 V9fsPCIState
*dev
= VIRTIO_9P_PCI(vpci_dev
);
1086 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1088 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1089 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1092 static Property virtio_9p_pci_properties
[] = {
1093 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1094 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1095 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1096 DEFINE_PROP_END_OF_LIST(),
1099 static void virtio_9p_pci_class_init(ObjectClass
*klass
, void *data
)
1101 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1102 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1103 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1105 k
->realize
= virtio_9p_pci_realize
;
1106 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1107 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_9P
;
1108 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1109 pcidev_k
->class_id
= 0x2;
1110 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1111 dc
->props
= virtio_9p_pci_properties
;
1114 static void virtio_9p_pci_instance_init(Object
*obj
)
1116 V9fsPCIState
*dev
= VIRTIO_9P_PCI(obj
);
1118 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1122 static const TypeInfo virtio_9p_pci_info
= {
1123 .name
= TYPE_VIRTIO_9P_PCI
,
1124 .parent
= TYPE_VIRTIO_PCI
,
1125 .instance_size
= sizeof(V9fsPCIState
),
1126 .instance_init
= virtio_9p_pci_instance_init
,
1127 .class_init
= virtio_9p_pci_class_init
,
1129 #endif /* CONFIG_VIRTFS */
1132 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
1135 static int virtio_pci_query_nvectors(DeviceState
*d
)
1137 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1139 return proxy
->nvectors
;
1142 static AddressSpace
*virtio_pci_get_dma_as(DeviceState
*d
)
1144 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1145 PCIDevice
*dev
= &proxy
->pci_dev
;
1147 return pci_get_address_space(dev
);
1150 static int virtio_pci_add_mem_cap(VirtIOPCIProxy
*proxy
,
1151 struct virtio_pci_cap
*cap
)
1153 PCIDevice
*dev
= &proxy
->pci_dev
;
1156 offset
= pci_add_capability(dev
, PCI_CAP_ID_VNDR
, 0,
1157 cap
->cap_len
, &error_abort
);
1159 assert(cap
->cap_len
>= sizeof *cap
);
1160 memcpy(dev
->config
+ offset
+ PCI_CAP_FLAGS
, &cap
->cap_len
,
1161 cap
->cap_len
- PCI_CAP_FLAGS
);
1166 static uint64_t virtio_pci_common_read(void *opaque
, hwaddr addr
,
1169 VirtIOPCIProxy
*proxy
= opaque
;
1170 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1175 case VIRTIO_PCI_COMMON_DFSELECT
:
1176 val
= proxy
->dfselect
;
1178 case VIRTIO_PCI_COMMON_DF
:
1179 if (proxy
->dfselect
<= 1) {
1180 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1182 val
= (vdev
->host_features
& ~vdc
->legacy_features
) >>
1183 (32 * proxy
->dfselect
);
1186 case VIRTIO_PCI_COMMON_GFSELECT
:
1187 val
= proxy
->gfselect
;
1189 case VIRTIO_PCI_COMMON_GF
:
1190 if (proxy
->gfselect
< ARRAY_SIZE(proxy
->guest_features
)) {
1191 val
= proxy
->guest_features
[proxy
->gfselect
];
1194 case VIRTIO_PCI_COMMON_MSIX
:
1195 val
= vdev
->config_vector
;
1197 case VIRTIO_PCI_COMMON_NUMQ
:
1198 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; ++i
) {
1199 if (virtio_queue_get_num(vdev
, i
)) {
1204 case VIRTIO_PCI_COMMON_STATUS
:
1207 case VIRTIO_PCI_COMMON_CFGGENERATION
:
1208 val
= vdev
->generation
;
1210 case VIRTIO_PCI_COMMON_Q_SELECT
:
1211 val
= vdev
->queue_sel
;
1213 case VIRTIO_PCI_COMMON_Q_SIZE
:
1214 val
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
1216 case VIRTIO_PCI_COMMON_Q_MSIX
:
1217 val
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
1219 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1220 val
= proxy
->vqs
[vdev
->queue_sel
].enabled
;
1222 case VIRTIO_PCI_COMMON_Q_NOFF
:
1223 /* Simply map queues in order */
1224 val
= vdev
->queue_sel
;
1226 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1227 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[0];
1229 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1230 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[1];
1232 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1233 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[0];
1235 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1236 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[1];
1238 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1239 val
= proxy
->vqs
[vdev
->queue_sel
].used
[0];
1241 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1242 val
= proxy
->vqs
[vdev
->queue_sel
].used
[1];
1251 static void virtio_pci_common_write(void *opaque
, hwaddr addr
,
1252 uint64_t val
, unsigned size
)
1254 VirtIOPCIProxy
*proxy
= opaque
;
1255 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1258 case VIRTIO_PCI_COMMON_DFSELECT
:
1259 proxy
->dfselect
= val
;
1261 case VIRTIO_PCI_COMMON_GFSELECT
:
1262 proxy
->gfselect
= val
;
1264 case VIRTIO_PCI_COMMON_GF
:
1265 if (proxy
->gfselect
< ARRAY_SIZE(proxy
->guest_features
)) {
1266 proxy
->guest_features
[proxy
->gfselect
] = val
;
1267 virtio_set_features(vdev
,
1268 (((uint64_t)proxy
->guest_features
[1]) << 32) |
1269 proxy
->guest_features
[0]);
1272 case VIRTIO_PCI_COMMON_MSIX
:
1273 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
1274 /* Make it possible for guest to discover an error took place. */
1275 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0) {
1276 val
= VIRTIO_NO_VECTOR
;
1278 vdev
->config_vector
= val
;
1280 case VIRTIO_PCI_COMMON_STATUS
:
1281 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
1282 virtio_pci_stop_ioeventfd(proxy
);
1285 virtio_set_status(vdev
, val
& 0xFF);
1287 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
1288 virtio_pci_start_ioeventfd(proxy
);
1291 if (vdev
->status
== 0) {
1292 virtio_pci_reset(DEVICE(proxy
));
1296 case VIRTIO_PCI_COMMON_Q_SELECT
:
1297 if (val
< VIRTIO_QUEUE_MAX
) {
1298 vdev
->queue_sel
= val
;
1301 case VIRTIO_PCI_COMMON_Q_SIZE
:
1302 proxy
->vqs
[vdev
->queue_sel
].num
= val
;
1304 case VIRTIO_PCI_COMMON_Q_MSIX
:
1305 msix_vector_unuse(&proxy
->pci_dev
,
1306 virtio_queue_vector(vdev
, vdev
->queue_sel
));
1307 /* Make it possible for guest to discover an error took place. */
1308 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0) {
1309 val
= VIRTIO_NO_VECTOR
;
1311 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
1313 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1314 virtio_queue_set_num(vdev
, vdev
->queue_sel
,
1315 proxy
->vqs
[vdev
->queue_sel
].num
);
1316 virtio_queue_set_rings(vdev
, vdev
->queue_sel
,
1317 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].desc
[1]) << 32 |
1318 proxy
->vqs
[vdev
->queue_sel
].desc
[0],
1319 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].avail
[1]) << 32 |
1320 proxy
->vqs
[vdev
->queue_sel
].avail
[0],
1321 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].used
[1]) << 32 |
1322 proxy
->vqs
[vdev
->queue_sel
].used
[0]);
1323 proxy
->vqs
[vdev
->queue_sel
].enabled
= 1;
1325 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1326 proxy
->vqs
[vdev
->queue_sel
].desc
[0] = val
;
1328 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1329 proxy
->vqs
[vdev
->queue_sel
].desc
[1] = val
;
1331 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1332 proxy
->vqs
[vdev
->queue_sel
].avail
[0] = val
;
1334 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1335 proxy
->vqs
[vdev
->queue_sel
].avail
[1] = val
;
1337 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1338 proxy
->vqs
[vdev
->queue_sel
].used
[0] = val
;
1340 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1341 proxy
->vqs
[vdev
->queue_sel
].used
[1] = val
;
1349 static uint64_t virtio_pci_notify_read(void *opaque
, hwaddr addr
,
1355 static void virtio_pci_notify_write(void *opaque
, hwaddr addr
,
1356 uint64_t val
, unsigned size
)
1358 VirtIODevice
*vdev
= opaque
;
1359 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(DEVICE(vdev
)->parent_bus
->parent
);
1360 unsigned queue
= addr
/ virtio_pci_queue_mem_mult(proxy
);
1362 if (queue
< VIRTIO_QUEUE_MAX
) {
1363 virtio_queue_notify(vdev
, queue
);
1367 static void virtio_pci_notify_write_pio(void *opaque
, hwaddr addr
,
1368 uint64_t val
, unsigned size
)
1370 VirtIODevice
*vdev
= opaque
;
1371 unsigned queue
= val
;
1373 if (queue
< VIRTIO_QUEUE_MAX
) {
1374 virtio_queue_notify(vdev
, queue
);
1378 static uint64_t virtio_pci_isr_read(void *opaque
, hwaddr addr
,
1381 VirtIOPCIProxy
*proxy
= opaque
;
1382 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1383 uint64_t val
= atomic_xchg(&vdev
->isr
, 0);
1384 pci_irq_deassert(&proxy
->pci_dev
);
1389 static void virtio_pci_isr_write(void *opaque
, hwaddr addr
,
1390 uint64_t val
, unsigned size
)
1394 static uint64_t virtio_pci_device_read(void *opaque
, hwaddr addr
,
1397 VirtIODevice
*vdev
= opaque
;
1402 val
= virtio_config_modern_readb(vdev
, addr
);
1405 val
= virtio_config_modern_readw(vdev
, addr
);
1408 val
= virtio_config_modern_readl(vdev
, addr
);
1414 static void virtio_pci_device_write(void *opaque
, hwaddr addr
,
1415 uint64_t val
, unsigned size
)
1417 VirtIODevice
*vdev
= opaque
;
1420 virtio_config_modern_writeb(vdev
, addr
, val
);
1423 virtio_config_modern_writew(vdev
, addr
, val
);
1426 virtio_config_modern_writel(vdev
, addr
, val
);
1431 static void virtio_pci_modern_regions_init(VirtIOPCIProxy
*proxy
)
1433 static const MemoryRegionOps common_ops
= {
1434 .read
= virtio_pci_common_read
,
1435 .write
= virtio_pci_common_write
,
1437 .min_access_size
= 1,
1438 .max_access_size
= 4,
1440 .endianness
= DEVICE_LITTLE_ENDIAN
,
1442 static const MemoryRegionOps isr_ops
= {
1443 .read
= virtio_pci_isr_read
,
1444 .write
= virtio_pci_isr_write
,
1446 .min_access_size
= 1,
1447 .max_access_size
= 4,
1449 .endianness
= DEVICE_LITTLE_ENDIAN
,
1451 static const MemoryRegionOps device_ops
= {
1452 .read
= virtio_pci_device_read
,
1453 .write
= virtio_pci_device_write
,
1455 .min_access_size
= 1,
1456 .max_access_size
= 4,
1458 .endianness
= DEVICE_LITTLE_ENDIAN
,
1460 static const MemoryRegionOps notify_ops
= {
1461 .read
= virtio_pci_notify_read
,
1462 .write
= virtio_pci_notify_write
,
1464 .min_access_size
= 1,
1465 .max_access_size
= 4,
1467 .endianness
= DEVICE_LITTLE_ENDIAN
,
1469 static const MemoryRegionOps notify_pio_ops
= {
1470 .read
= virtio_pci_notify_read
,
1471 .write
= virtio_pci_notify_write_pio
,
1473 .min_access_size
= 1,
1474 .max_access_size
= 4,
1476 .endianness
= DEVICE_LITTLE_ENDIAN
,
1480 memory_region_init_io(&proxy
->common
.mr
, OBJECT(proxy
),
1483 "virtio-pci-common",
1484 proxy
->common
.size
);
1486 memory_region_init_io(&proxy
->isr
.mr
, OBJECT(proxy
),
1492 memory_region_init_io(&proxy
->device
.mr
, OBJECT(proxy
),
1494 virtio_bus_get_device(&proxy
->bus
),
1495 "virtio-pci-device",
1496 proxy
->device
.size
);
1498 memory_region_init_io(&proxy
->notify
.mr
, OBJECT(proxy
),
1500 virtio_bus_get_device(&proxy
->bus
),
1501 "virtio-pci-notify",
1502 proxy
->notify
.size
);
1504 memory_region_init_io(&proxy
->notify_pio
.mr
, OBJECT(proxy
),
1506 virtio_bus_get_device(&proxy
->bus
),
1507 "virtio-pci-notify-pio",
1508 proxy
->notify_pio
.size
);
1511 static void virtio_pci_modern_region_map(VirtIOPCIProxy
*proxy
,
1512 VirtIOPCIRegion
*region
,
1513 struct virtio_pci_cap
*cap
,
1517 memory_region_add_subregion(mr
, region
->offset
, ®ion
->mr
);
1519 cap
->cfg_type
= region
->type
;
1521 cap
->offset
= cpu_to_le32(region
->offset
);
1522 cap
->length
= cpu_to_le32(region
->size
);
1523 virtio_pci_add_mem_cap(proxy
, cap
);
1527 static void virtio_pci_modern_mem_region_map(VirtIOPCIProxy
*proxy
,
1528 VirtIOPCIRegion
*region
,
1529 struct virtio_pci_cap
*cap
)
1531 virtio_pci_modern_region_map(proxy
, region
, cap
,
1532 &proxy
->modern_bar
, proxy
->modern_mem_bar_idx
);
1535 static void virtio_pci_modern_io_region_map(VirtIOPCIProxy
*proxy
,
1536 VirtIOPCIRegion
*region
,
1537 struct virtio_pci_cap
*cap
)
1539 virtio_pci_modern_region_map(proxy
, region
, cap
,
1540 &proxy
->io_bar
, proxy
->modern_io_bar_idx
);
1543 static void virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy
*proxy
,
1544 VirtIOPCIRegion
*region
)
1546 memory_region_del_subregion(&proxy
->modern_bar
,
1550 static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy
*proxy
,
1551 VirtIOPCIRegion
*region
)
1553 memory_region_del_subregion(&proxy
->io_bar
,
1557 static void virtio_pci_pre_plugged(DeviceState
*d
, Error
**errp
)
1559 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1560 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1562 if (virtio_pci_modern(proxy
)) {
1563 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_VERSION_1
);
1566 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_BAD_FEATURE
);
1569 /* This is called by virtio-bus just after the device is plugged. */
1570 static void virtio_pci_device_plugged(DeviceState
*d
, Error
**errp
)
1572 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1573 VirtioBusState
*bus
= &proxy
->bus
;
1574 bool legacy
= virtio_pci_legacy(proxy
);
1576 bool modern_pio
= proxy
->flags
& VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY
;
1579 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1582 * Virtio capabilities present without
1583 * VIRTIO_F_VERSION_1 confuses guests
1585 if (!proxy
->ignore_backend_features
&&
1586 !virtio_has_feature(vdev
->host_features
, VIRTIO_F_VERSION_1
)) {
1587 virtio_pci_disable_modern(proxy
);
1590 error_setg(errp
, "Device doesn't support modern mode, and legacy"
1591 " mode is disabled");
1592 error_append_hint(errp
, "Set disable-legacy to off\n");
1598 modern
= virtio_pci_modern(proxy
);
1600 config
= proxy
->pci_dev
.config
;
1601 if (proxy
->class_code
) {
1602 pci_config_set_class(config
, proxy
->class_code
);
1606 if (virtio_host_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
)) {
1607 error_setg(errp
, "VIRTIO_F_IOMMU_PLATFORM was supported by"
1608 " neither legacy nor transitional device");
1612 * Legacy and transitional devices use specific subsystem IDs.
1613 * Note that the subsystem vendor ID (config + PCI_SUBSYSTEM_VENDOR_ID)
1614 * is set to PCI_SUBVENDOR_ID_REDHAT_QUMRANET by default.
1616 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, virtio_bus_get_vdev_id(bus
));
1618 /* pure virtio-1.0 */
1619 pci_set_word(config
+ PCI_VENDOR_ID
,
1620 PCI_VENDOR_ID_REDHAT_QUMRANET
);
1621 pci_set_word(config
+ PCI_DEVICE_ID
,
1622 0x1040 + virtio_bus_get_vdev_id(bus
));
1623 pci_config_set_revision(config
, 1);
1625 config
[PCI_INTERRUPT_PIN
] = 1;
1629 struct virtio_pci_cap cap
= {
1630 .cap_len
= sizeof cap
,
1632 struct virtio_pci_notify_cap notify
= {
1633 .cap
.cap_len
= sizeof notify
,
1634 .notify_off_multiplier
=
1635 cpu_to_le32(virtio_pci_queue_mem_mult(proxy
)),
1637 struct virtio_pci_cfg_cap cfg
= {
1638 .cap
.cap_len
= sizeof cfg
,
1639 .cap
.cfg_type
= VIRTIO_PCI_CAP_PCI_CFG
,
1641 struct virtio_pci_notify_cap notify_pio
= {
1642 .cap
.cap_len
= sizeof notify
,
1643 .notify_off_multiplier
= cpu_to_le32(0x0),
1646 struct virtio_pci_cfg_cap
*cfg_mask
;
1648 virtio_pci_modern_regions_init(proxy
);
1650 virtio_pci_modern_mem_region_map(proxy
, &proxy
->common
, &cap
);
1651 virtio_pci_modern_mem_region_map(proxy
, &proxy
->isr
, &cap
);
1652 virtio_pci_modern_mem_region_map(proxy
, &proxy
->device
, &cap
);
1653 virtio_pci_modern_mem_region_map(proxy
, &proxy
->notify
, ¬ify
.cap
);
1656 memory_region_init(&proxy
->io_bar
, OBJECT(proxy
),
1657 "virtio-pci-io", 0x4);
1659 pci_register_bar(&proxy
->pci_dev
, proxy
->modern_io_bar_idx
,
1660 PCI_BASE_ADDRESS_SPACE_IO
, &proxy
->io_bar
);
1662 virtio_pci_modern_io_region_map(proxy
, &proxy
->notify_pio
,
1666 pci_register_bar(&proxy
->pci_dev
, proxy
->modern_mem_bar_idx
,
1667 PCI_BASE_ADDRESS_SPACE_MEMORY
|
1668 PCI_BASE_ADDRESS_MEM_PREFETCH
|
1669 PCI_BASE_ADDRESS_MEM_TYPE_64
,
1670 &proxy
->modern_bar
);
1672 proxy
->config_cap
= virtio_pci_add_mem_cap(proxy
, &cfg
.cap
);
1673 cfg_mask
= (void *)(proxy
->pci_dev
.wmask
+ proxy
->config_cap
);
1674 pci_set_byte(&cfg_mask
->cap
.bar
, ~0x0);
1675 pci_set_long((uint8_t *)&cfg_mask
->cap
.offset
, ~0x0);
1676 pci_set_long((uint8_t *)&cfg_mask
->cap
.length
, ~0x0);
1677 pci_set_long(cfg_mask
->pci_cfg_data
, ~0x0);
1680 if (proxy
->nvectors
) {
1681 int err
= msix_init_exclusive_bar(&proxy
->pci_dev
, proxy
->nvectors
,
1682 proxy
->msix_bar_idx
, NULL
);
1684 /* Notice when a system that supports MSIx can't initialize it */
1685 if (err
!= -ENOTSUP
) {
1686 warn_report("unable to init msix vectors to %" PRIu32
,
1689 proxy
->nvectors
= 0;
1693 proxy
->pci_dev
.config_write
= virtio_write_config
;
1694 proxy
->pci_dev
.config_read
= virtio_read_config
;
1697 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
)
1698 + virtio_bus_get_vdev_config_len(bus
);
1699 size
= pow2ceil(size
);
1701 memory_region_init_io(&proxy
->bar
, OBJECT(proxy
),
1702 &virtio_pci_config_ops
,
1703 proxy
, "virtio-pci", size
);
1705 pci_register_bar(&proxy
->pci_dev
, proxy
->legacy_io_bar_idx
,
1706 PCI_BASE_ADDRESS_SPACE_IO
, &proxy
->bar
);
1710 static void virtio_pci_device_unplugged(DeviceState
*d
)
1712 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1713 bool modern
= virtio_pci_modern(proxy
);
1714 bool modern_pio
= proxy
->flags
& VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY
;
1716 virtio_pci_stop_ioeventfd(proxy
);
1719 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->common
);
1720 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->isr
);
1721 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->device
);
1722 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->notify
);
1724 virtio_pci_modern_io_region_unmap(proxy
, &proxy
->notify_pio
);
1729 static void virtio_pci_realize(PCIDevice
*pci_dev
, Error
**errp
)
1731 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
1732 VirtioPCIClass
*k
= VIRTIO_PCI_GET_CLASS(pci_dev
);
1733 bool pcie_port
= pci_bus_is_express(pci_get_bus(pci_dev
)) &&
1734 !pci_bus_is_root(pci_get_bus(pci_dev
));
1736 if (kvm_enabled() && !kvm_has_many_ioeventfds()) {
1737 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
1741 * virtio pci bar layout used by default.
1742 * subclasses can re-arrange things if needed.
1744 * region 0 -- virtio legacy io bar
1745 * region 1 -- msi-x bar
1746 * region 4+5 -- virtio modern memory (64bit) bar
1749 proxy
->legacy_io_bar_idx
= 0;
1750 proxy
->msix_bar_idx
= 1;
1751 proxy
->modern_io_bar_idx
= 2;
1752 proxy
->modern_mem_bar_idx
= 4;
1754 proxy
->common
.offset
= 0x0;
1755 proxy
->common
.size
= 0x1000;
1756 proxy
->common
.type
= VIRTIO_PCI_CAP_COMMON_CFG
;
1758 proxy
->isr
.offset
= 0x1000;
1759 proxy
->isr
.size
= 0x1000;
1760 proxy
->isr
.type
= VIRTIO_PCI_CAP_ISR_CFG
;
1762 proxy
->device
.offset
= 0x2000;
1763 proxy
->device
.size
= 0x1000;
1764 proxy
->device
.type
= VIRTIO_PCI_CAP_DEVICE_CFG
;
1766 proxy
->notify
.offset
= 0x3000;
1767 proxy
->notify
.size
= virtio_pci_queue_mem_mult(proxy
) * VIRTIO_QUEUE_MAX
;
1768 proxy
->notify
.type
= VIRTIO_PCI_CAP_NOTIFY_CFG
;
1770 proxy
->notify_pio
.offset
= 0x0;
1771 proxy
->notify_pio
.size
= 0x4;
1772 proxy
->notify_pio
.type
= VIRTIO_PCI_CAP_NOTIFY_CFG
;
1774 /* subclasses can enforce modern, so do this unconditionally */
1775 memory_region_init(&proxy
->modern_bar
, OBJECT(proxy
), "virtio-pci",
1776 /* PCI BAR regions must be powers of 2 */
1777 pow2ceil(proxy
->notify
.offset
+ proxy
->notify
.size
));
1779 if (proxy
->disable_legacy
== ON_OFF_AUTO_AUTO
) {
1780 proxy
->disable_legacy
= pcie_port
? ON_OFF_AUTO_ON
: ON_OFF_AUTO_OFF
;
1783 if (!virtio_pci_modern(proxy
) && !virtio_pci_legacy(proxy
)) {
1784 error_setg(errp
, "device cannot work as neither modern nor legacy mode"
1786 error_append_hint(errp
, "Set either disable-modern or disable-legacy"
1791 if (pcie_port
&& pci_is_express(pci_dev
)) {
1794 pos
= pcie_endpoint_cap_init(pci_dev
, 0);
1797 pos
= pci_add_capability(pci_dev
, PCI_CAP_ID_PM
, 0,
1798 PCI_PM_SIZEOF
, errp
);
1803 pci_dev
->exp
.pm_cap
= pos
;
1806 * Indicates that this function complies with revision 1.2 of the
1807 * PCI Power Management Interface Specification.
1809 pci_set_word(pci_dev
->config
+ pos
+ PCI_PM_PMC
, 0x3);
1811 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_DEVERR
) {
1812 /* Init error enabling flags */
1813 pcie_cap_deverr_init(pci_dev
);
1816 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_LNKCTL
) {
1817 /* Init Link Control Register */
1818 pcie_cap_lnkctl_init(pci_dev
);
1821 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_PM
) {
1822 /* Init Power Management Control Register */
1823 pci_set_word(pci_dev
->wmask
+ pos
+ PCI_PM_CTRL
,
1824 PCI_PM_CTRL_STATE_MASK
);
1827 if (proxy
->flags
& VIRTIO_PCI_FLAG_ATS
) {
1828 pcie_ats_init(pci_dev
, 256);
1833 * make future invocations of pci_is_express() return false
1834 * and pci_config_size() return PCI_CONFIG_SPACE_SIZE.
1836 pci_dev
->cap_present
&= ~QEMU_PCI_CAP_EXPRESS
;
1839 virtio_pci_bus_new(&proxy
->bus
, sizeof(proxy
->bus
), proxy
);
1841 k
->realize(proxy
, errp
);
1845 static void virtio_pci_exit(PCIDevice
*pci_dev
)
1847 msix_uninit_exclusive_bar(pci_dev
);
1850 static void virtio_pci_reset(DeviceState
*qdev
)
1852 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1853 VirtioBusState
*bus
= VIRTIO_BUS(&proxy
->bus
);
1854 PCIDevice
*dev
= PCI_DEVICE(qdev
);
1857 virtio_pci_stop_ioeventfd(proxy
);
1858 virtio_bus_reset(bus
);
1859 msix_unuse_all_vectors(&proxy
->pci_dev
);
1861 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1862 proxy
->vqs
[i
].enabled
= 0;
1863 proxy
->vqs
[i
].num
= 0;
1864 proxy
->vqs
[i
].desc
[0] = proxy
->vqs
[i
].desc
[1] = 0;
1865 proxy
->vqs
[i
].avail
[0] = proxy
->vqs
[i
].avail
[1] = 0;
1866 proxy
->vqs
[i
].used
[0] = proxy
->vqs
[i
].used
[1] = 0;
1869 if (pci_is_express(dev
)) {
1870 pcie_cap_deverr_reset(dev
);
1871 pcie_cap_lnkctl_reset(dev
);
1873 pci_set_word(dev
->config
+ dev
->exp
.pm_cap
+ PCI_PM_CTRL
, 0);
1877 static Property virtio_pci_properties
[] = {
1878 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy
, flags
,
1879 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT
, false),
1880 DEFINE_PROP_ON_OFF_AUTO("disable-legacy", VirtIOPCIProxy
, disable_legacy
,
1882 DEFINE_PROP_BOOL("disable-modern", VirtIOPCIProxy
, disable_modern
, false),
1883 DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy
, flags
,
1884 VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT
, true),
1885 DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy
, flags
,
1886 VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT
, false),
1887 DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy
, flags
,
1888 VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT
, false),
1889 DEFINE_PROP_BIT("page-per-vq", VirtIOPCIProxy
, flags
,
1890 VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT
, false),
1891 DEFINE_PROP_BOOL("x-ignore-backend-features", VirtIOPCIProxy
,
1892 ignore_backend_features
, false),
1893 DEFINE_PROP_BIT("ats", VirtIOPCIProxy
, flags
,
1894 VIRTIO_PCI_FLAG_ATS_BIT
, false),
1895 DEFINE_PROP_BIT("x-pcie-deverr-init", VirtIOPCIProxy
, flags
,
1896 VIRTIO_PCI_FLAG_INIT_DEVERR_BIT
, true),
1897 DEFINE_PROP_BIT("x-pcie-lnkctl-init", VirtIOPCIProxy
, flags
,
1898 VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT
, true),
1899 DEFINE_PROP_BIT("x-pcie-pm-init", VirtIOPCIProxy
, flags
,
1900 VIRTIO_PCI_FLAG_INIT_PM_BIT
, true),
1901 DEFINE_PROP_END_OF_LIST(),
1904 static void virtio_pci_dc_realize(DeviceState
*qdev
, Error
**errp
)
1906 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_GET_CLASS(qdev
);
1907 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1908 PCIDevice
*pci_dev
= &proxy
->pci_dev
;
1910 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_PCIE
) &&
1911 virtio_pci_modern(proxy
)) {
1912 pci_dev
->cap_present
|= QEMU_PCI_CAP_EXPRESS
;
1915 vpciklass
->parent_dc_realize(qdev
, errp
);
1918 static void virtio_pci_class_init(ObjectClass
*klass
, void *data
)
1920 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1921 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1922 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_CLASS(klass
);
1924 dc
->props
= virtio_pci_properties
;
1925 k
->realize
= virtio_pci_realize
;
1926 k
->exit
= virtio_pci_exit
;
1927 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1928 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1929 k
->class_id
= PCI_CLASS_OTHERS
;
1930 device_class_set_parent_realize(dc
, virtio_pci_dc_realize
,
1931 &vpciklass
->parent_dc_realize
);
1932 dc
->reset
= virtio_pci_reset
;
1935 static const TypeInfo virtio_pci_info
= {
1936 .name
= TYPE_VIRTIO_PCI
,
1937 .parent
= TYPE_PCI_DEVICE
,
1938 .instance_size
= sizeof(VirtIOPCIProxy
),
1939 .class_init
= virtio_pci_class_init
,
1940 .class_size
= sizeof(VirtioPCIClass
),
1942 .interfaces
= (InterfaceInfo
[]) {
1943 { INTERFACE_PCIE_DEVICE
},
1944 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
1949 /* virtio-blk-pci */
1951 static Property virtio_blk_pci_properties
[] = {
1952 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1953 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1954 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1955 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1956 DEV_NVECTORS_UNSPECIFIED
),
1957 DEFINE_PROP_END_OF_LIST(),
1960 static void virtio_blk_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1962 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(vpci_dev
);
1963 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1965 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1966 vpci_dev
->nvectors
= dev
->vdev
.conf
.num_queues
+ 1;
1969 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1970 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1973 static void virtio_blk_pci_class_init(ObjectClass
*klass
, void *data
)
1975 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1976 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1977 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1979 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1980 dc
->props
= virtio_blk_pci_properties
;
1981 k
->realize
= virtio_blk_pci_realize
;
1982 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1983 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
1984 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1985 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1988 static void virtio_blk_pci_instance_init(Object
*obj
)
1990 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(obj
);
1992 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1994 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
1995 "bootindex", &error_abort
);
1998 static const TypeInfo virtio_blk_pci_info
= {
1999 .name
= TYPE_VIRTIO_BLK_PCI
,
2000 .parent
= TYPE_VIRTIO_PCI
,
2001 .instance_size
= sizeof(VirtIOBlkPCI
),
2002 .instance_init
= virtio_blk_pci_instance_init
,
2003 .class_init
= virtio_blk_pci_class_init
,
2006 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2007 /* vhost-user-blk */
2009 static Property vhost_user_blk_pci_properties
[] = {
2010 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
2011 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
2012 DEV_NVECTORS_UNSPECIFIED
),
2013 DEFINE_PROP_END_OF_LIST(),
2016 static void vhost_user_blk_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2018 VHostUserBlkPCI
*dev
= VHOST_USER_BLK_PCI(vpci_dev
);
2019 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
2021 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
2022 vpci_dev
->nvectors
= dev
->vdev
.num_queues
+ 1;
2025 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2026 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2029 static void vhost_user_blk_pci_class_init(ObjectClass
*klass
, void *data
)
2031 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2032 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2033 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2035 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
2036 dc
->props
= vhost_user_blk_pci_properties
;
2037 k
->realize
= vhost_user_blk_pci_realize
;
2038 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2039 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
2040 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
2041 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
2044 static void vhost_user_blk_pci_instance_init(Object
*obj
)
2046 VHostUserBlkPCI
*dev
= VHOST_USER_BLK_PCI(obj
);
2048 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2049 TYPE_VHOST_USER_BLK
);
2050 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
2051 "bootindex", &error_abort
);
2054 static const TypeInfo vhost_user_blk_pci_info
= {
2055 .name
= TYPE_VHOST_USER_BLK_PCI
,
2056 .parent
= TYPE_VIRTIO_PCI
,
2057 .instance_size
= sizeof(VHostUserBlkPCI
),
2058 .instance_init
= vhost_user_blk_pci_instance_init
,
2059 .class_init
= vhost_user_blk_pci_class_init
,
2063 /* virtio-scsi-pci */
2065 static Property virtio_scsi_pci_properties
[] = {
2066 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
2067 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
2068 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
2069 DEV_NVECTORS_UNSPECIFIED
),
2070 DEFINE_PROP_END_OF_LIST(),
2073 static void virtio_scsi_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2075 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(vpci_dev
);
2076 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
2077 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
2078 DeviceState
*proxy
= DEVICE(vpci_dev
);
2081 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
2082 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
2086 * For command line compatibility, this sets the virtio-scsi-device bus
2090 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
2091 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
2095 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2096 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2099 static void virtio_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
2101 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2102 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2103 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2105 k
->realize
= virtio_scsi_pci_realize
;
2106 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
2107 dc
->props
= virtio_scsi_pci_properties
;
2108 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2109 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
2110 pcidev_k
->revision
= 0x00;
2111 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
2114 static void virtio_scsi_pci_instance_init(Object
*obj
)
2116 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(obj
);
2118 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2122 static const TypeInfo virtio_scsi_pci_info
= {
2123 .name
= TYPE_VIRTIO_SCSI_PCI
,
2124 .parent
= TYPE_VIRTIO_PCI
,
2125 .instance_size
= sizeof(VirtIOSCSIPCI
),
2126 .instance_init
= virtio_scsi_pci_instance_init
,
2127 .class_init
= virtio_scsi_pci_class_init
,
2130 /* vhost-scsi-pci */
2132 #ifdef CONFIG_VHOST_SCSI
2133 static Property vhost_scsi_pci_properties
[] = {
2134 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
2135 DEV_NVECTORS_UNSPECIFIED
),
2136 DEFINE_PROP_END_OF_LIST(),
2139 static void vhost_scsi_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2141 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(vpci_dev
);
2142 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
2143 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
2145 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
2146 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
2149 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2150 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2153 static void vhost_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
2155 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2156 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2157 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2158 k
->realize
= vhost_scsi_pci_realize
;
2159 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
2160 dc
->props
= vhost_scsi_pci_properties
;
2161 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2162 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
2163 pcidev_k
->revision
= 0x00;
2164 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
2167 static void vhost_scsi_pci_instance_init(Object
*obj
)
2169 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(obj
);
2171 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2173 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
2174 "bootindex", &error_abort
);
2177 static const TypeInfo vhost_scsi_pci_info
= {
2178 .name
= TYPE_VHOST_SCSI_PCI
,
2179 .parent
= TYPE_VIRTIO_PCI
,
2180 .instance_size
= sizeof(VHostSCSIPCI
),
2181 .instance_init
= vhost_scsi_pci_instance_init
,
2182 .class_init
= vhost_scsi_pci_class_init
,
2186 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2187 /* vhost-user-scsi-pci */
2188 static Property vhost_user_scsi_pci_properties
[] = {
2189 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
2190 DEV_NVECTORS_UNSPECIFIED
),
2191 DEFINE_PROP_END_OF_LIST(),
2194 static void vhost_user_scsi_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2196 VHostUserSCSIPCI
*dev
= VHOST_USER_SCSI_PCI(vpci_dev
);
2197 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
2198 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
2200 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
2201 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
2204 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2205 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2208 static void vhost_user_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
2210 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2211 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2212 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2213 k
->realize
= vhost_user_scsi_pci_realize
;
2214 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
2215 dc
->props
= vhost_user_scsi_pci_properties
;
2216 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2217 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
2218 pcidev_k
->revision
= 0x00;
2219 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
2222 static void vhost_user_scsi_pci_instance_init(Object
*obj
)
2224 VHostUserSCSIPCI
*dev
= VHOST_USER_SCSI_PCI(obj
);
2226 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2227 TYPE_VHOST_USER_SCSI
);
2228 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
2229 "bootindex", &error_abort
);
2232 static const TypeInfo vhost_user_scsi_pci_info
= {
2233 .name
= TYPE_VHOST_USER_SCSI_PCI
,
2234 .parent
= TYPE_VIRTIO_PCI
,
2235 .instance_size
= sizeof(VHostUserSCSIPCI
),
2236 .instance_init
= vhost_user_scsi_pci_instance_init
,
2237 .class_init
= vhost_user_scsi_pci_class_init
,
2241 /* vhost-vsock-pci */
2243 #ifdef CONFIG_VHOST_VSOCK
2244 static Property vhost_vsock_pci_properties
[] = {
2245 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
2246 DEFINE_PROP_END_OF_LIST(),
2249 static void vhost_vsock_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2251 VHostVSockPCI
*dev
= VHOST_VSOCK_PCI(vpci_dev
);
2252 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
2254 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2255 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2258 static void vhost_vsock_pci_class_init(ObjectClass
*klass
, void *data
)
2260 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2261 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2262 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2263 k
->realize
= vhost_vsock_pci_realize
;
2264 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
2265 dc
->props
= vhost_vsock_pci_properties
;
2266 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2267 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_VSOCK
;
2268 pcidev_k
->revision
= 0x00;
2269 pcidev_k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
2272 static void vhost_vsock_pci_instance_init(Object
*obj
)
2274 VHostVSockPCI
*dev
= VHOST_VSOCK_PCI(obj
);
2276 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2280 static const TypeInfo vhost_vsock_pci_info
= {
2281 .name
= TYPE_VHOST_VSOCK_PCI
,
2282 .parent
= TYPE_VIRTIO_PCI
,
2283 .instance_size
= sizeof(VHostVSockPCI
),
2284 .instance_init
= vhost_vsock_pci_instance_init
,
2285 .class_init
= vhost_vsock_pci_class_init
,
2289 /* virtio-balloon-pci */
2291 static Property virtio_balloon_pci_properties
[] = {
2292 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
2293 DEFINE_PROP_END_OF_LIST(),
2296 static void virtio_balloon_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2298 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(vpci_dev
);
2299 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
2301 if (vpci_dev
->class_code
!= PCI_CLASS_OTHERS
&&
2302 vpci_dev
->class_code
!= PCI_CLASS_MEMORY_RAM
) { /* qemu < 1.1 */
2303 vpci_dev
->class_code
= PCI_CLASS_OTHERS
;
2306 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2307 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2310 static void virtio_balloon_pci_class_init(ObjectClass
*klass
, void *data
)
2312 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2313 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2314 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2315 k
->realize
= virtio_balloon_pci_realize
;
2316 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
2317 dc
->props
= virtio_balloon_pci_properties
;
2318 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2319 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BALLOON
;
2320 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
2321 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
2324 static void virtio_balloon_pci_instance_init(Object
*obj
)
2326 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(obj
);
2328 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2329 TYPE_VIRTIO_BALLOON
);
2330 object_property_add_alias(obj
, "guest-stats", OBJECT(&dev
->vdev
),
2331 "guest-stats", &error_abort
);
2332 object_property_add_alias(obj
, "guest-stats-polling-interval",
2334 "guest-stats-polling-interval", &error_abort
);
2337 static const TypeInfo virtio_balloon_pci_info
= {
2338 .name
= TYPE_VIRTIO_BALLOON_PCI
,
2339 .parent
= TYPE_VIRTIO_PCI
,
2340 .instance_size
= sizeof(VirtIOBalloonPCI
),
2341 .instance_init
= virtio_balloon_pci_instance_init
,
2342 .class_init
= virtio_balloon_pci_class_init
,
2345 /* virtio-serial-pci */
2347 static void virtio_serial_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2349 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(vpci_dev
);
2350 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
2351 DeviceState
*proxy
= DEVICE(vpci_dev
);
2354 if (vpci_dev
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
2355 vpci_dev
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
2356 vpci_dev
->class_code
!= PCI_CLASS_OTHERS
) { /* qemu-kvm */
2357 vpci_dev
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
2360 /* backwards-compatibility with machines that were created with
2361 DEV_NVECTORS_UNSPECIFIED */
2362 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
2363 vpci_dev
->nvectors
= dev
->vdev
.serial
.max_virtserial_ports
+ 1;
2367 * For command line compatibility, this sets the virtio-serial-device bus
2371 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
2372 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
2376 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2377 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2380 static Property virtio_serial_pci_properties
[] = {
2381 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
2382 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
2383 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
2384 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
2385 DEFINE_PROP_END_OF_LIST(),
2388 static void virtio_serial_pci_class_init(ObjectClass
*klass
, void *data
)
2390 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2391 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2392 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2393 k
->realize
= virtio_serial_pci_realize
;
2394 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
2395 dc
->props
= virtio_serial_pci_properties
;
2396 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2397 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_CONSOLE
;
2398 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
2399 pcidev_k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
2402 static void virtio_serial_pci_instance_init(Object
*obj
)
2404 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(obj
);
2406 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2407 TYPE_VIRTIO_SERIAL
);
2410 static const TypeInfo virtio_serial_pci_info
= {
2411 .name
= TYPE_VIRTIO_SERIAL_PCI
,
2412 .parent
= TYPE_VIRTIO_PCI
,
2413 .instance_size
= sizeof(VirtIOSerialPCI
),
2414 .instance_init
= virtio_serial_pci_instance_init
,
2415 .class_init
= virtio_serial_pci_class_init
,
2418 /* virtio-net-pci */
2420 static Property virtio_net_properties
[] = {
2421 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
2422 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
2423 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
2424 DEFINE_PROP_END_OF_LIST(),
2427 static void virtio_net_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2429 DeviceState
*qdev
= DEVICE(vpci_dev
);
2430 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(vpci_dev
);
2431 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
2433 virtio_net_set_netclient_name(&dev
->vdev
, qdev
->id
,
2434 object_get_typename(OBJECT(qdev
)));
2435 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2436 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2439 static void virtio_net_pci_class_init(ObjectClass
*klass
, void *data
)
2441 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2442 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2443 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_CLASS(klass
);
2445 k
->romfile
= "efi-virtio.rom";
2446 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2447 k
->device_id
= PCI_DEVICE_ID_VIRTIO_NET
;
2448 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
2449 k
->class_id
= PCI_CLASS_NETWORK_ETHERNET
;
2450 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
2451 dc
->props
= virtio_net_properties
;
2452 vpciklass
->realize
= virtio_net_pci_realize
;
2455 static void virtio_net_pci_instance_init(Object
*obj
)
2457 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(obj
);
2459 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2461 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
2462 "bootindex", &error_abort
);
2465 static const TypeInfo virtio_net_pci_info
= {
2466 .name
= TYPE_VIRTIO_NET_PCI
,
2467 .parent
= TYPE_VIRTIO_PCI
,
2468 .instance_size
= sizeof(VirtIONetPCI
),
2469 .instance_init
= virtio_net_pci_instance_init
,
2470 .class_init
= virtio_net_pci_class_init
,
2473 /* virtio-rng-pci */
2475 static void virtio_rng_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2477 VirtIORngPCI
*vrng
= VIRTIO_RNG_PCI(vpci_dev
);
2478 DeviceState
*vdev
= DEVICE(&vrng
->vdev
);
2481 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2482 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
2484 error_propagate(errp
, err
);
2488 object_property_set_link(OBJECT(vrng
),
2489 OBJECT(vrng
->vdev
.conf
.rng
), "rng",
2493 static void virtio_rng_pci_class_init(ObjectClass
*klass
, void *data
)
2495 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2496 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2497 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2499 k
->realize
= virtio_rng_pci_realize
;
2500 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
2502 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2503 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_RNG
;
2504 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
2505 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
2508 static void virtio_rng_initfn(Object
*obj
)
2510 VirtIORngPCI
*dev
= VIRTIO_RNG_PCI(obj
);
2512 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2516 static const TypeInfo virtio_rng_pci_info
= {
2517 .name
= TYPE_VIRTIO_RNG_PCI
,
2518 .parent
= TYPE_VIRTIO_PCI
,
2519 .instance_size
= sizeof(VirtIORngPCI
),
2520 .instance_init
= virtio_rng_initfn
,
2521 .class_init
= virtio_rng_pci_class_init
,
2524 /* virtio-input-pci */
2526 static Property virtio_input_pci_properties
[] = {
2527 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
2528 DEFINE_PROP_END_OF_LIST(),
2531 static void virtio_input_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2533 VirtIOInputPCI
*vinput
= VIRTIO_INPUT_PCI(vpci_dev
);
2534 DeviceState
*vdev
= DEVICE(&vinput
->vdev
);
2536 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2537 virtio_pci_force_virtio_1(vpci_dev
);
2538 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2541 static void virtio_input_pci_class_init(ObjectClass
*klass
, void *data
)
2543 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2544 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2545 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2547 dc
->props
= virtio_input_pci_properties
;
2548 k
->realize
= virtio_input_pci_realize
;
2549 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
2551 pcidev_k
->class_id
= PCI_CLASS_INPUT_OTHER
;
2554 static void virtio_input_hid_kbd_pci_class_init(ObjectClass
*klass
, void *data
)
2556 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2558 pcidev_k
->class_id
= PCI_CLASS_INPUT_KEYBOARD
;
2561 static void virtio_input_hid_mouse_pci_class_init(ObjectClass
*klass
,
2564 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2566 pcidev_k
->class_id
= PCI_CLASS_INPUT_MOUSE
;
2569 static void virtio_keyboard_initfn(Object
*obj
)
2571 VirtIOInputHIDPCI
*dev
= VIRTIO_INPUT_HID_PCI(obj
);
2573 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2574 TYPE_VIRTIO_KEYBOARD
);
2577 static void virtio_mouse_initfn(Object
*obj
)
2579 VirtIOInputHIDPCI
*dev
= VIRTIO_INPUT_HID_PCI(obj
);
2581 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2585 static void virtio_tablet_initfn(Object
*obj
)
2587 VirtIOInputHIDPCI
*dev
= VIRTIO_INPUT_HID_PCI(obj
);
2589 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2590 TYPE_VIRTIO_TABLET
);
2593 static const TypeInfo virtio_input_pci_info
= {
2594 .name
= TYPE_VIRTIO_INPUT_PCI
,
2595 .parent
= TYPE_VIRTIO_PCI
,
2596 .instance_size
= sizeof(VirtIOInputPCI
),
2597 .class_init
= virtio_input_pci_class_init
,
2601 static const TypeInfo virtio_input_hid_pci_info
= {
2602 .name
= TYPE_VIRTIO_INPUT_HID_PCI
,
2603 .parent
= TYPE_VIRTIO_INPUT_PCI
,
2604 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2608 static const TypeInfo virtio_keyboard_pci_info
= {
2609 .name
= TYPE_VIRTIO_KEYBOARD_PCI
,
2610 .parent
= TYPE_VIRTIO_INPUT_HID_PCI
,
2611 .class_init
= virtio_input_hid_kbd_pci_class_init
,
2612 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2613 .instance_init
= virtio_keyboard_initfn
,
2616 static const TypeInfo virtio_mouse_pci_info
= {
2617 .name
= TYPE_VIRTIO_MOUSE_PCI
,
2618 .parent
= TYPE_VIRTIO_INPUT_HID_PCI
,
2619 .class_init
= virtio_input_hid_mouse_pci_class_init
,
2620 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2621 .instance_init
= virtio_mouse_initfn
,
2624 static const TypeInfo virtio_tablet_pci_info
= {
2625 .name
= TYPE_VIRTIO_TABLET_PCI
,
2626 .parent
= TYPE_VIRTIO_INPUT_HID_PCI
,
2627 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2628 .instance_init
= virtio_tablet_initfn
,
2632 static void virtio_host_initfn(Object
*obj
)
2634 VirtIOInputHostPCI
*dev
= VIRTIO_INPUT_HOST_PCI(obj
);
2636 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2637 TYPE_VIRTIO_INPUT_HOST
);
2640 static const TypeInfo virtio_host_pci_info
= {
2641 .name
= TYPE_VIRTIO_INPUT_HOST_PCI
,
2642 .parent
= TYPE_VIRTIO_INPUT_PCI
,
2643 .instance_size
= sizeof(VirtIOInputHostPCI
),
2644 .instance_init
= virtio_host_initfn
,
2648 /* virtio-pci-bus */
2650 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
2651 VirtIOPCIProxy
*dev
)
2653 DeviceState
*qdev
= DEVICE(dev
);
2654 char virtio_bus_name
[] = "virtio-bus";
2656 qbus_create_inplace(bus
, bus_size
, TYPE_VIRTIO_PCI_BUS
, qdev
,
2660 static void virtio_pci_bus_class_init(ObjectClass
*klass
, void *data
)
2662 BusClass
*bus_class
= BUS_CLASS(klass
);
2663 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
2664 bus_class
->max_dev
= 1;
2665 k
->notify
= virtio_pci_notify
;
2666 k
->save_config
= virtio_pci_save_config
;
2667 k
->load_config
= virtio_pci_load_config
;
2668 k
->save_queue
= virtio_pci_save_queue
;
2669 k
->load_queue
= virtio_pci_load_queue
;
2670 k
->save_extra_state
= virtio_pci_save_extra_state
;
2671 k
->load_extra_state
= virtio_pci_load_extra_state
;
2672 k
->has_extra_state
= virtio_pci_has_extra_state
;
2673 k
->query_guest_notifiers
= virtio_pci_query_guest_notifiers
;
2674 k
->set_guest_notifiers
= virtio_pci_set_guest_notifiers
;
2675 k
->set_host_notifier_mr
= virtio_pci_set_host_notifier_mr
;
2676 k
->vmstate_change
= virtio_pci_vmstate_change
;
2677 k
->pre_plugged
= virtio_pci_pre_plugged
;
2678 k
->device_plugged
= virtio_pci_device_plugged
;
2679 k
->device_unplugged
= virtio_pci_device_unplugged
;
2680 k
->query_nvectors
= virtio_pci_query_nvectors
;
2681 k
->ioeventfd_enabled
= virtio_pci_ioeventfd_enabled
;
2682 k
->ioeventfd_assign
= virtio_pci_ioeventfd_assign
;
2683 k
->get_dma_as
= virtio_pci_get_dma_as
;
2686 static const TypeInfo virtio_pci_bus_info
= {
2687 .name
= TYPE_VIRTIO_PCI_BUS
,
2688 .parent
= TYPE_VIRTIO_BUS
,
2689 .instance_size
= sizeof(VirtioPCIBusState
),
2690 .class_init
= virtio_pci_bus_class_init
,
2693 static void virtio_pci_register_types(void)
2695 type_register_static(&virtio_rng_pci_info
);
2696 type_register_static(&virtio_input_pci_info
);
2697 type_register_static(&virtio_input_hid_pci_info
);
2698 type_register_static(&virtio_keyboard_pci_info
);
2699 type_register_static(&virtio_mouse_pci_info
);
2700 type_register_static(&virtio_tablet_pci_info
);
2702 type_register_static(&virtio_host_pci_info
);
2704 type_register_static(&virtio_pci_bus_info
);
2705 type_register_static(&virtio_pci_info
);
2706 #ifdef CONFIG_VIRTFS
2707 type_register_static(&virtio_9p_pci_info
);
2709 type_register_static(&virtio_blk_pci_info
);
2710 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2711 type_register_static(&vhost_user_blk_pci_info
);
2713 type_register_static(&virtio_scsi_pci_info
);
2714 type_register_static(&virtio_balloon_pci_info
);
2715 type_register_static(&virtio_serial_pci_info
);
2716 type_register_static(&virtio_net_pci_info
);
2717 #ifdef CONFIG_VHOST_SCSI
2718 type_register_static(&vhost_scsi_pci_info
);
2720 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2721 type_register_static(&vhost_user_scsi_pci_info
);
2723 #ifdef CONFIG_VHOST_VSOCK
2724 type_register_static(&vhost_vsock_pci_info
);
2728 type_init(virtio_pci_register_types
)