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 "exec/memop.h"
21 #include "standard-headers/linux/virtio_pci.h"
22 #include "hw/boards.h"
23 #include "hw/virtio/virtio.h"
24 #include "migration/qemu-file-types.h"
25 #include "hw/pci/pci.h"
26 #include "hw/pci/pci_bus.h"
27 #include "hw/qdev-properties.h"
28 #include "qapi/error.h"
29 #include "qemu/error-report.h"
31 #include "qemu/module.h"
32 #include "hw/pci/msi.h"
33 #include "hw/pci/msix.h"
34 #include "hw/loader.h"
35 #include "sysemu/kvm.h"
36 #include "hw/virtio/virtio-pci.h"
37 #include "qemu/range.h"
38 #include "hw/virtio/virtio-bus.h"
39 #include "qapi/visitor.h"
40 #include "sysemu/replay.h"
43 #define VIRTIO_PCI_REGION_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_present(dev))
45 #undef VIRTIO_PCI_CONFIG
47 /* The remaining space is defined by each driver as the per-driver
48 * configuration space */
49 #define VIRTIO_PCI_CONFIG_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_enabled(dev))
51 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
53 static void virtio_pci_reset(DeviceState
*qdev
);
56 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
57 static inline VirtIOPCIProxy
*to_virtio_pci_proxy(DeviceState
*d
)
59 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
62 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
63 * be careful and test performance if you change this.
65 static inline VirtIOPCIProxy
*to_virtio_pci_proxy_fast(DeviceState
*d
)
67 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
70 static void virtio_pci_notify(DeviceState
*d
, uint16_t vector
)
72 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy_fast(d
);
74 if (msix_enabled(&proxy
->pci_dev
)) {
75 if (vector
!= VIRTIO_NO_VECTOR
) {
76 msix_notify(&proxy
->pci_dev
, vector
);
79 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
80 pci_set_irq(&proxy
->pci_dev
, qatomic_read(&vdev
->isr
) & 1);
84 static void virtio_pci_save_config(DeviceState
*d
, QEMUFile
*f
)
86 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
87 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
89 pci_device_save(&proxy
->pci_dev
, f
);
90 msix_save(&proxy
->pci_dev
, f
);
91 if (msix_present(&proxy
->pci_dev
))
92 qemu_put_be16(f
, vdev
->config_vector
);
95 static const VMStateDescription vmstate_virtio_pci_modern_queue_state
= {
96 .name
= "virtio_pci/modern_queue_state",
98 .minimum_version_id
= 1,
99 .fields
= (VMStateField
[]) {
100 VMSTATE_UINT16(num
, VirtIOPCIQueue
),
101 VMSTATE_UNUSED(1), /* enabled was stored as be16 */
102 VMSTATE_BOOL(enabled
, VirtIOPCIQueue
),
103 VMSTATE_UINT32_ARRAY(desc
, VirtIOPCIQueue
, 2),
104 VMSTATE_UINT32_ARRAY(avail
, VirtIOPCIQueue
, 2),
105 VMSTATE_UINT32_ARRAY(used
, VirtIOPCIQueue
, 2),
106 VMSTATE_END_OF_LIST()
110 static bool virtio_pci_modern_state_needed(void *opaque
)
112 VirtIOPCIProxy
*proxy
= opaque
;
114 return virtio_pci_modern(proxy
);
117 static const VMStateDescription vmstate_virtio_pci_modern_state_sub
= {
118 .name
= "virtio_pci/modern_state",
120 .minimum_version_id
= 1,
121 .needed
= &virtio_pci_modern_state_needed
,
122 .fields
= (VMStateField
[]) {
123 VMSTATE_UINT32(dfselect
, VirtIOPCIProxy
),
124 VMSTATE_UINT32(gfselect
, VirtIOPCIProxy
),
125 VMSTATE_UINT32_ARRAY(guest_features
, VirtIOPCIProxy
, 2),
126 VMSTATE_STRUCT_ARRAY(vqs
, VirtIOPCIProxy
, VIRTIO_QUEUE_MAX
, 0,
127 vmstate_virtio_pci_modern_queue_state
,
129 VMSTATE_END_OF_LIST()
133 static const VMStateDescription vmstate_virtio_pci
= {
134 .name
= "virtio_pci",
136 .minimum_version_id
= 1,
137 .fields
= (VMStateField
[]) {
138 VMSTATE_END_OF_LIST()
140 .subsections
= (const VMStateDescription
*[]) {
141 &vmstate_virtio_pci_modern_state_sub
,
146 static bool virtio_pci_has_extra_state(DeviceState
*d
)
148 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
150 return proxy
->flags
& VIRTIO_PCI_FLAG_MIGRATE_EXTRA
;
153 static void virtio_pci_save_extra_state(DeviceState
*d
, QEMUFile
*f
)
155 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
157 vmstate_save_state(f
, &vmstate_virtio_pci
, proxy
, NULL
);
160 static int virtio_pci_load_extra_state(DeviceState
*d
, QEMUFile
*f
)
162 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
164 return vmstate_load_state(f
, &vmstate_virtio_pci
, proxy
, 1);
167 static void virtio_pci_save_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
169 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
170 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
172 if (msix_present(&proxy
->pci_dev
))
173 qemu_put_be16(f
, virtio_queue_vector(vdev
, n
));
176 static int virtio_pci_load_config(DeviceState
*d
, QEMUFile
*f
)
178 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
179 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
183 ret
= pci_device_load(&proxy
->pci_dev
, f
);
187 msix_unuse_all_vectors(&proxy
->pci_dev
);
188 msix_load(&proxy
->pci_dev
, f
);
189 if (msix_present(&proxy
->pci_dev
)) {
190 qemu_get_be16s(f
, &vector
);
192 if (vector
!= VIRTIO_NO_VECTOR
&& vector
>= proxy
->nvectors
) {
196 vector
= VIRTIO_NO_VECTOR
;
198 vdev
->config_vector
= vector
;
199 if (vector
!= VIRTIO_NO_VECTOR
) {
200 msix_vector_use(&proxy
->pci_dev
, vector
);
205 static int virtio_pci_load_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
207 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
208 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
211 if (msix_present(&proxy
->pci_dev
)) {
212 qemu_get_be16s(f
, &vector
);
213 if (vector
!= VIRTIO_NO_VECTOR
&& vector
>= proxy
->nvectors
) {
217 vector
= VIRTIO_NO_VECTOR
;
219 virtio_queue_set_vector(vdev
, n
, vector
);
220 if (vector
!= VIRTIO_NO_VECTOR
) {
221 msix_vector_use(&proxy
->pci_dev
, vector
);
227 static bool virtio_pci_ioeventfd_enabled(DeviceState
*d
)
229 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
231 return (proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) != 0;
234 #define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000
236 static inline int virtio_pci_queue_mem_mult(struct VirtIOPCIProxy
*proxy
)
238 return (proxy
->flags
& VIRTIO_PCI_FLAG_PAGE_PER_VQ
) ?
239 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
: 4;
242 static int virtio_pci_ioeventfd_assign(DeviceState
*d
, EventNotifier
*notifier
,
245 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
246 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
247 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
248 bool legacy
= virtio_pci_legacy(proxy
);
249 bool modern
= virtio_pci_modern(proxy
);
250 bool fast_mmio
= kvm_ioeventfd_any_length_enabled();
251 bool modern_pio
= proxy
->flags
& VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY
;
252 MemoryRegion
*modern_mr
= &proxy
->notify
.mr
;
253 MemoryRegion
*modern_notify_mr
= &proxy
->notify_pio
.mr
;
254 MemoryRegion
*legacy_mr
= &proxy
->bar
;
255 hwaddr modern_addr
= virtio_pci_queue_mem_mult(proxy
) *
256 virtio_get_queue_index(vq
);
257 hwaddr legacy_addr
= VIRTIO_PCI_QUEUE_NOTIFY
;
262 memory_region_add_eventfd(modern_mr
, modern_addr
, 0,
265 memory_region_add_eventfd(modern_mr
, modern_addr
, 2,
269 memory_region_add_eventfd(modern_notify_mr
, 0, 2,
274 memory_region_add_eventfd(legacy_mr
, legacy_addr
, 2,
280 memory_region_del_eventfd(modern_mr
, modern_addr
, 0,
283 memory_region_del_eventfd(modern_mr
, modern_addr
, 2,
287 memory_region_del_eventfd(modern_notify_mr
, 0, 2,
292 memory_region_del_eventfd(legacy_mr
, legacy_addr
, 2,
299 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
301 virtio_bus_start_ioeventfd(&proxy
->bus
);
304 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
306 virtio_bus_stop_ioeventfd(&proxy
->bus
);
309 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
311 VirtIOPCIProxy
*proxy
= opaque
;
312 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
317 case VIRTIO_PCI_GUEST_FEATURES
:
318 /* Guest does not negotiate properly? We have to assume nothing. */
319 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
320 val
= virtio_bus_get_vdev_bad_features(&proxy
->bus
);
322 virtio_set_features(vdev
, val
);
324 case VIRTIO_PCI_QUEUE_PFN
:
325 pa
= (hwaddr
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
327 virtio_pci_reset(DEVICE(proxy
));
330 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
332 case VIRTIO_PCI_QUEUE_SEL
:
333 if (val
< VIRTIO_QUEUE_MAX
)
334 vdev
->queue_sel
= val
;
336 case VIRTIO_PCI_QUEUE_NOTIFY
:
337 if (val
< VIRTIO_QUEUE_MAX
) {
338 virtio_queue_notify(vdev
, val
);
341 case VIRTIO_PCI_STATUS
:
342 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
343 virtio_pci_stop_ioeventfd(proxy
);
346 virtio_set_status(vdev
, val
& 0xFF);
348 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
349 virtio_pci_start_ioeventfd(proxy
);
352 if (vdev
->status
== 0) {
353 virtio_pci_reset(DEVICE(proxy
));
356 /* Linux before 2.6.34 drives the device without enabling
357 the PCI device bus master bit. Enable it automatically
358 for the guest. This is a PCI spec violation but so is
359 initiating DMA with bus master bit clear. */
360 if (val
== (VIRTIO_CONFIG_S_ACKNOWLEDGE
| VIRTIO_CONFIG_S_DRIVER
)) {
361 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
362 proxy
->pci_dev
.config
[PCI_COMMAND
] |
363 PCI_COMMAND_MASTER
, 1);
366 case VIRTIO_MSI_CONFIG_VECTOR
:
367 if (vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
368 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
370 /* Make it possible for guest to discover an error took place. */
371 if (val
< proxy
->nvectors
) {
372 msix_vector_use(&proxy
->pci_dev
, val
);
374 val
= VIRTIO_NO_VECTOR
;
376 vdev
->config_vector
= val
;
378 case VIRTIO_MSI_QUEUE_VECTOR
:
379 vector
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
380 if (vector
!= VIRTIO_NO_VECTOR
) {
381 msix_vector_unuse(&proxy
->pci_dev
, vector
);
383 /* Make it possible for guest to discover an error took place. */
384 if (val
< proxy
->nvectors
) {
385 msix_vector_use(&proxy
->pci_dev
, val
);
387 val
= VIRTIO_NO_VECTOR
;
389 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
392 qemu_log_mask(LOG_GUEST_ERROR
,
393 "%s: unexpected address 0x%x value 0x%x\n",
394 __func__
, addr
, val
);
399 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
401 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
402 uint32_t ret
= 0xFFFFFFFF;
405 case VIRTIO_PCI_HOST_FEATURES
:
406 ret
= vdev
->host_features
;
408 case VIRTIO_PCI_GUEST_FEATURES
:
409 ret
= vdev
->guest_features
;
411 case VIRTIO_PCI_QUEUE_PFN
:
412 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
413 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
415 case VIRTIO_PCI_QUEUE_NUM
:
416 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
418 case VIRTIO_PCI_QUEUE_SEL
:
419 ret
= vdev
->queue_sel
;
421 case VIRTIO_PCI_STATUS
:
425 /* reading from the ISR also clears it. */
426 ret
= qatomic_xchg(&vdev
->isr
, 0);
427 pci_irq_deassert(&proxy
->pci_dev
);
429 case VIRTIO_MSI_CONFIG_VECTOR
:
430 ret
= vdev
->config_vector
;
432 case VIRTIO_MSI_QUEUE_VECTOR
:
433 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
442 static uint64_t virtio_pci_config_read(void *opaque
, hwaddr addr
,
445 VirtIOPCIProxy
*proxy
= opaque
;
446 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
447 uint32_t config
= VIRTIO_PCI_CONFIG_SIZE(&proxy
->pci_dev
);
455 return virtio_ioport_read(proxy
, addr
);
461 val
= virtio_config_readb(vdev
, addr
);
464 val
= virtio_config_readw(vdev
, addr
);
465 if (virtio_is_big_endian(vdev
)) {
470 val
= virtio_config_readl(vdev
, addr
);
471 if (virtio_is_big_endian(vdev
)) {
479 static void virtio_pci_config_write(void *opaque
, hwaddr addr
,
480 uint64_t val
, unsigned size
)
482 VirtIOPCIProxy
*proxy
= opaque
;
483 uint32_t config
= VIRTIO_PCI_CONFIG_SIZE(&proxy
->pci_dev
);
484 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
491 virtio_ioport_write(proxy
, addr
, val
);
496 * Virtio-PCI is odd. Ioports are LE but config space is target native
501 virtio_config_writeb(vdev
, addr
, val
);
504 if (virtio_is_big_endian(vdev
)) {
507 virtio_config_writew(vdev
, addr
, val
);
510 if (virtio_is_big_endian(vdev
)) {
513 virtio_config_writel(vdev
, addr
, val
);
518 static const MemoryRegionOps virtio_pci_config_ops
= {
519 .read
= virtio_pci_config_read
,
520 .write
= virtio_pci_config_write
,
522 .min_access_size
= 1,
523 .max_access_size
= 4,
525 .endianness
= DEVICE_LITTLE_ENDIAN
,
528 static MemoryRegion
*virtio_address_space_lookup(VirtIOPCIProxy
*proxy
,
529 hwaddr
*off
, int len
)
532 VirtIOPCIRegion
*reg
;
534 for (i
= 0; i
< ARRAY_SIZE(proxy
->regs
); ++i
) {
535 reg
= &proxy
->regs
[i
];
536 if (*off
>= reg
->offset
&&
537 *off
+ len
<= reg
->offset
+ reg
->size
) {
546 /* Below are generic functions to do memcpy from/to an address space,
547 * without byteswaps, with input validation.
549 * As regular address_space_* APIs all do some kind of byteswap at least for
550 * some host/target combinations, we are forced to explicitly convert to a
551 * known-endianness integer value.
552 * It doesn't really matter which endian format to go through, so the code
553 * below selects the endian that causes the least amount of work on the given
556 * Note: host pointer must be aligned.
559 void virtio_address_space_write(VirtIOPCIProxy
*proxy
, hwaddr addr
,
560 const uint8_t *buf
, int len
)
565 /* address_space_* APIs assume an aligned address.
566 * As address is under guest control, handle illegal values.
570 mr
= virtio_address_space_lookup(proxy
, &addr
, len
);
575 /* Make sure caller aligned buf properly */
576 assert(!(((uintptr_t)buf
) & (len
- 1)));
580 val
= pci_get_byte(buf
);
583 val
= pci_get_word(buf
);
586 val
= pci_get_long(buf
);
589 /* As length is under guest control, handle illegal values. */
592 memory_region_dispatch_write(mr
, addr
, val
, size_memop(len
) | MO_LE
,
593 MEMTXATTRS_UNSPECIFIED
);
597 virtio_address_space_read(VirtIOPCIProxy
*proxy
, hwaddr addr
,
598 uint8_t *buf
, int len
)
603 /* address_space_* APIs assume an aligned address.
604 * As address is under guest control, handle illegal values.
608 mr
= virtio_address_space_lookup(proxy
, &addr
, len
);
613 /* Make sure caller aligned buf properly */
614 assert(!(((uintptr_t)buf
) & (len
- 1)));
616 memory_region_dispatch_read(mr
, addr
, &val
, size_memop(len
) | MO_LE
,
617 MEMTXATTRS_UNSPECIFIED
);
620 pci_set_byte(buf
, val
);
623 pci_set_word(buf
, val
);
626 pci_set_long(buf
, val
);
629 /* As length is under guest control, handle illegal values. */
634 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
635 uint32_t val
, int len
)
637 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
638 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
639 struct virtio_pci_cfg_cap
*cfg
;
641 pci_default_write_config(pci_dev
, address
, val
, len
);
643 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_FLR
) {
644 pcie_cap_flr_write_config(pci_dev
, address
, val
, len
);
647 if (range_covers_byte(address
, len
, PCI_COMMAND
)) {
648 if (!(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
649 virtio_set_disabled(vdev
, true);
650 virtio_pci_stop_ioeventfd(proxy
);
651 virtio_set_status(vdev
, vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
653 virtio_set_disabled(vdev
, false);
657 if (proxy
->config_cap
&&
658 ranges_overlap(address
, len
, proxy
->config_cap
+ offsetof(struct virtio_pci_cfg_cap
,
660 sizeof cfg
->pci_cfg_data
)) {
664 cfg
= (void *)(proxy
->pci_dev
.config
+ proxy
->config_cap
);
665 off
= le32_to_cpu(cfg
->cap
.offset
);
666 len
= le32_to_cpu(cfg
->cap
.length
);
668 if (len
== 1 || len
== 2 || len
== 4) {
669 assert(len
<= sizeof cfg
->pci_cfg_data
);
670 virtio_address_space_write(proxy
, off
, cfg
->pci_cfg_data
, len
);
675 static uint32_t virtio_read_config(PCIDevice
*pci_dev
,
676 uint32_t address
, int len
)
678 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
679 struct virtio_pci_cfg_cap
*cfg
;
681 if (proxy
->config_cap
&&
682 ranges_overlap(address
, len
, proxy
->config_cap
+ offsetof(struct virtio_pci_cfg_cap
,
684 sizeof cfg
->pci_cfg_data
)) {
688 cfg
= (void *)(proxy
->pci_dev
.config
+ proxy
->config_cap
);
689 off
= le32_to_cpu(cfg
->cap
.offset
);
690 len
= le32_to_cpu(cfg
->cap
.length
);
692 if (len
== 1 || len
== 2 || len
== 4) {
693 assert(len
<= sizeof cfg
->pci_cfg_data
);
694 virtio_address_space_read(proxy
, off
, cfg
->pci_cfg_data
, len
);
698 return pci_default_read_config(pci_dev
, address
, len
);
701 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy
*proxy
,
702 unsigned int queue_no
,
705 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
708 if (irqfd
->users
== 0) {
709 KVMRouteChange c
= kvm_irqchip_begin_route_changes(kvm_state
);
710 ret
= kvm_irqchip_add_msi_route(&c
, vector
, &proxy
->pci_dev
);
714 kvm_irqchip_commit_route_changes(&c
);
721 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy
*proxy
,
724 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
725 if (--irqfd
->users
== 0) {
726 kvm_irqchip_release_virq(kvm_state
, irqfd
->virq
);
730 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy
*proxy
,
731 unsigned int queue_no
,
734 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
735 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
736 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
737 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
738 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, n
, NULL
, irqfd
->virq
);
741 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy
*proxy
,
742 unsigned int queue_no
,
745 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
746 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
747 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
748 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
751 ret
= kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state
, n
, irqfd
->virq
);
755 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy
*proxy
, int nvqs
)
757 PCIDevice
*dev
= &proxy
->pci_dev
;
758 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
759 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 ret
= kvm_virtio_pci_vq_vector_use(proxy
, queue_no
, vector
);
775 /* If guest supports masking, set up irqfd now.
776 * Otherwise, delay until unmasked in the frontend.
778 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
779 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
781 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
789 while (--queue_no
>= 0) {
790 vector
= virtio_queue_vector(vdev
, queue_no
);
791 if (vector
>= msix_nr_vectors_allocated(dev
)) {
794 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
795 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
797 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
802 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy
*proxy
, int nvqs
)
804 PCIDevice
*dev
= &proxy
->pci_dev
;
805 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
808 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
810 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
811 if (!virtio_queue_get_num(vdev
, queue_no
)) {
814 vector
= virtio_queue_vector(vdev
, queue_no
);
815 if (vector
>= msix_nr_vectors_allocated(dev
)) {
818 /* If guest supports masking, clean up irqfd now.
819 * Otherwise, it was cleaned when masked in the frontend.
821 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
822 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
824 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
828 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy
*proxy
,
829 unsigned int queue_no
,
833 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
834 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
835 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
836 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
840 if (proxy
->vector_irqfd
) {
841 irqfd
= &proxy
->vector_irqfd
[vector
];
842 if (irqfd
->msg
.data
!= msg
.data
|| irqfd
->msg
.address
!= msg
.address
) {
843 ret
= kvm_irqchip_update_msi_route(kvm_state
, irqfd
->virq
, msg
,
848 kvm_irqchip_commit_routes(kvm_state
);
852 /* If guest supports masking, irqfd is already setup, unmask it.
853 * Otherwise, set it up now.
855 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
856 k
->guest_notifier_mask(vdev
, queue_no
, false);
857 /* Test after unmasking to avoid losing events. */
858 if (k
->guest_notifier_pending
&&
859 k
->guest_notifier_pending(vdev
, queue_no
)) {
860 event_notifier_set(n
);
863 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
868 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy
*proxy
,
869 unsigned int queue_no
,
872 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
873 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
875 /* If guest supports masking, keep irqfd but mask it.
876 * Otherwise, clean it up now.
878 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
879 k
->guest_notifier_mask(vdev
, queue_no
, true);
881 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
885 static int virtio_pci_vector_unmask(PCIDevice
*dev
, unsigned vector
,
888 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
889 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
890 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
891 int ret
, index
, unmasked
= 0;
894 index
= virtio_get_queue_index(vq
);
895 if (!virtio_queue_get_num(vdev
, index
)) {
898 if (index
< proxy
->nvqs_with_notifiers
) {
899 ret
= virtio_pci_vq_vector_unmask(proxy
, index
, vector
, msg
);
905 vq
= virtio_vector_next_queue(vq
);
911 vq
= virtio_vector_first_queue(vdev
, vector
);
912 while (vq
&& unmasked
>= 0) {
913 index
= virtio_get_queue_index(vq
);
914 if (index
< proxy
->nvqs_with_notifiers
) {
915 virtio_pci_vq_vector_mask(proxy
, index
, vector
);
918 vq
= virtio_vector_next_queue(vq
);
923 static void virtio_pci_vector_mask(PCIDevice
*dev
, unsigned vector
)
925 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
926 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
927 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
931 index
= virtio_get_queue_index(vq
);
932 if (!virtio_queue_get_num(vdev
, index
)) {
935 if (index
< proxy
->nvqs_with_notifiers
) {
936 virtio_pci_vq_vector_mask(proxy
, index
, vector
);
938 vq
= virtio_vector_next_queue(vq
);
942 static void virtio_pci_vector_poll(PCIDevice
*dev
,
943 unsigned int vector_start
,
944 unsigned int vector_end
)
946 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
947 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
948 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
951 EventNotifier
*notifier
;
954 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
955 if (!virtio_queue_get_num(vdev
, queue_no
)) {
958 vector
= virtio_queue_vector(vdev
, queue_no
);
959 if (vector
< vector_start
|| vector
>= vector_end
||
960 !msix_is_masked(dev
, vector
)) {
963 vq
= virtio_get_queue(vdev
, queue_no
);
964 notifier
= virtio_queue_get_guest_notifier(vq
);
965 if (k
->guest_notifier_pending
) {
966 if (k
->guest_notifier_pending(vdev
, queue_no
)) {
967 msix_set_pending(dev
, vector
);
969 } else if (event_notifier_test_and_clear(notifier
)) {
970 msix_set_pending(dev
, vector
);
975 static int virtio_pci_set_guest_notifier(DeviceState
*d
, int n
, bool assign
,
978 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
979 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
980 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
981 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
982 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
985 int r
= event_notifier_init(notifier
, 0);
989 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
991 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
992 event_notifier_cleanup(notifier
);
995 if (!msix_enabled(&proxy
->pci_dev
) &&
996 vdev
->use_guest_notifier_mask
&&
997 vdc
->guest_notifier_mask
) {
998 vdc
->guest_notifier_mask(vdev
, n
, !assign
);
1004 static bool virtio_pci_query_guest_notifiers(DeviceState
*d
)
1006 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1007 return msix_enabled(&proxy
->pci_dev
);
1010 static int virtio_pci_set_guest_notifiers(DeviceState
*d
, int nvqs
, bool assign
)
1012 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1013 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1014 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1016 bool with_irqfd
= msix_enabled(&proxy
->pci_dev
) &&
1017 kvm_msi_via_irqfd_enabled();
1019 nvqs
= MIN(nvqs
, VIRTIO_QUEUE_MAX
);
1022 * When deassigning, pass a consistent nvqs value to avoid leaking
1023 * notifiers. But first check we've actually been configured, exit
1024 * early if we haven't.
1026 if (!assign
&& !proxy
->nvqs_with_notifiers
) {
1029 assert(assign
|| nvqs
== proxy
->nvqs_with_notifiers
);
1031 proxy
->nvqs_with_notifiers
= nvqs
;
1033 /* Must unset vector notifier while guest notifier is still assigned */
1034 if ((proxy
->vector_irqfd
|| k
->guest_notifier_mask
) && !assign
) {
1035 msix_unset_vector_notifiers(&proxy
->pci_dev
);
1036 if (proxy
->vector_irqfd
) {
1037 kvm_virtio_pci_vector_release(proxy
, nvqs
);
1038 g_free(proxy
->vector_irqfd
);
1039 proxy
->vector_irqfd
= NULL
;
1043 for (n
= 0; n
< nvqs
; n
++) {
1044 if (!virtio_queue_get_num(vdev
, n
)) {
1048 r
= virtio_pci_set_guest_notifier(d
, n
, assign
, with_irqfd
);
1054 /* Must set vector notifier after guest notifier has been assigned */
1055 if ((with_irqfd
|| k
->guest_notifier_mask
) && assign
) {
1057 proxy
->vector_irqfd
=
1058 g_malloc0(sizeof(*proxy
->vector_irqfd
) *
1059 msix_nr_vectors_allocated(&proxy
->pci_dev
));
1060 r
= kvm_virtio_pci_vector_use(proxy
, nvqs
);
1065 r
= msix_set_vector_notifiers(&proxy
->pci_dev
,
1066 virtio_pci_vector_unmask
,
1067 virtio_pci_vector_mask
,
1068 virtio_pci_vector_poll
);
1070 goto notifiers_error
;
1079 kvm_virtio_pci_vector_release(proxy
, nvqs
);
1083 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
1086 virtio_pci_set_guest_notifier(d
, n
, !assign
, with_irqfd
);
1091 static int virtio_pci_set_host_notifier_mr(DeviceState
*d
, int n
,
1092 MemoryRegion
*mr
, bool assign
)
1094 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1097 if (n
>= VIRTIO_QUEUE_MAX
|| !virtio_pci_modern(proxy
) ||
1098 virtio_pci_queue_mem_mult(proxy
) != memory_region_size(mr
)) {
1103 offset
= virtio_pci_queue_mem_mult(proxy
) * n
;
1104 memory_region_add_subregion_overlap(&proxy
->notify
.mr
, offset
, mr
, 1);
1106 memory_region_del_subregion(&proxy
->notify
.mr
, mr
);
1112 static void virtio_pci_vmstate_change(DeviceState
*d
, bool running
)
1114 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1115 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1118 /* Old QEMU versions did not set bus master enable on status write.
1119 * Detect DRIVER set and enable it.
1121 if ((proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION
) &&
1122 (vdev
->status
& VIRTIO_CONFIG_S_DRIVER
) &&
1123 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
1124 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
1125 proxy
->pci_dev
.config
[PCI_COMMAND
] |
1126 PCI_COMMAND_MASTER
, 1);
1128 virtio_pci_start_ioeventfd(proxy
);
1130 virtio_pci_stop_ioeventfd(proxy
);
1135 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
1138 static int virtio_pci_query_nvectors(DeviceState
*d
)
1140 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1142 return proxy
->nvectors
;
1145 static AddressSpace
*virtio_pci_get_dma_as(DeviceState
*d
)
1147 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1148 PCIDevice
*dev
= &proxy
->pci_dev
;
1150 return pci_get_address_space(dev
);
1153 static bool virtio_pci_iommu_enabled(DeviceState
*d
)
1155 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1156 PCIDevice
*dev
= &proxy
->pci_dev
;
1157 AddressSpace
*dma_as
= pci_device_iommu_address_space(dev
);
1159 if (dma_as
== &address_space_memory
) {
1166 static bool virtio_pci_queue_enabled(DeviceState
*d
, int n
)
1168 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1169 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1171 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
1172 return proxy
->vqs
[n
].enabled
;
1175 return virtio_queue_enabled_legacy(vdev
, n
);
1178 static int virtio_pci_add_mem_cap(VirtIOPCIProxy
*proxy
,
1179 struct virtio_pci_cap
*cap
)
1181 PCIDevice
*dev
= &proxy
->pci_dev
;
1184 offset
= pci_add_capability(dev
, PCI_CAP_ID_VNDR
, 0,
1185 cap
->cap_len
, &error_abort
);
1187 assert(cap
->cap_len
>= sizeof *cap
);
1188 memcpy(dev
->config
+ offset
+ PCI_CAP_FLAGS
, &cap
->cap_len
,
1189 cap
->cap_len
- PCI_CAP_FLAGS
);
1194 static uint64_t virtio_pci_common_read(void *opaque
, hwaddr addr
,
1197 VirtIOPCIProxy
*proxy
= opaque
;
1198 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1207 case VIRTIO_PCI_COMMON_DFSELECT
:
1208 val
= proxy
->dfselect
;
1210 case VIRTIO_PCI_COMMON_DF
:
1211 if (proxy
->dfselect
<= 1) {
1212 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1214 val
= (vdev
->host_features
& ~vdc
->legacy_features
) >>
1215 (32 * proxy
->dfselect
);
1218 case VIRTIO_PCI_COMMON_GFSELECT
:
1219 val
= proxy
->gfselect
;
1221 case VIRTIO_PCI_COMMON_GF
:
1222 if (proxy
->gfselect
< ARRAY_SIZE(proxy
->guest_features
)) {
1223 val
= proxy
->guest_features
[proxy
->gfselect
];
1226 case VIRTIO_PCI_COMMON_MSIX
:
1227 val
= vdev
->config_vector
;
1229 case VIRTIO_PCI_COMMON_NUMQ
:
1230 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; ++i
) {
1231 if (virtio_queue_get_num(vdev
, i
)) {
1236 case VIRTIO_PCI_COMMON_STATUS
:
1239 case VIRTIO_PCI_COMMON_CFGGENERATION
:
1240 val
= vdev
->generation
;
1242 case VIRTIO_PCI_COMMON_Q_SELECT
:
1243 val
= vdev
->queue_sel
;
1245 case VIRTIO_PCI_COMMON_Q_SIZE
:
1246 val
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
1248 case VIRTIO_PCI_COMMON_Q_MSIX
:
1249 val
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
1251 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1252 val
= proxy
->vqs
[vdev
->queue_sel
].enabled
;
1254 case VIRTIO_PCI_COMMON_Q_NOFF
:
1255 /* Simply map queues in order */
1256 val
= vdev
->queue_sel
;
1258 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1259 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[0];
1261 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1262 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[1];
1264 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1265 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[0];
1267 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1268 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[1];
1270 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1271 val
= proxy
->vqs
[vdev
->queue_sel
].used
[0];
1273 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1274 val
= proxy
->vqs
[vdev
->queue_sel
].used
[1];
1276 case VIRTIO_PCI_COMMON_Q_RESET
:
1277 val
= proxy
->vqs
[vdev
->queue_sel
].reset
;
1286 static void virtio_pci_common_write(void *opaque
, hwaddr addr
,
1287 uint64_t val
, unsigned size
)
1289 VirtIOPCIProxy
*proxy
= opaque
;
1290 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1298 case VIRTIO_PCI_COMMON_DFSELECT
:
1299 proxy
->dfselect
= val
;
1301 case VIRTIO_PCI_COMMON_GFSELECT
:
1302 proxy
->gfselect
= val
;
1304 case VIRTIO_PCI_COMMON_GF
:
1305 if (proxy
->gfselect
< ARRAY_SIZE(proxy
->guest_features
)) {
1306 proxy
->guest_features
[proxy
->gfselect
] = val
;
1307 virtio_set_features(vdev
,
1308 (((uint64_t)proxy
->guest_features
[1]) << 32) |
1309 proxy
->guest_features
[0]);
1312 case VIRTIO_PCI_COMMON_MSIX
:
1313 if (vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
1314 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
1316 /* Make it possible for guest to discover an error took place. */
1317 if (val
< proxy
->nvectors
) {
1318 msix_vector_use(&proxy
->pci_dev
, val
);
1320 val
= VIRTIO_NO_VECTOR
;
1322 vdev
->config_vector
= val
;
1324 case VIRTIO_PCI_COMMON_STATUS
:
1325 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
1326 virtio_pci_stop_ioeventfd(proxy
);
1329 virtio_set_status(vdev
, val
& 0xFF);
1331 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
1332 virtio_pci_start_ioeventfd(proxy
);
1335 if (vdev
->status
== 0) {
1336 virtio_pci_reset(DEVICE(proxy
));
1340 case VIRTIO_PCI_COMMON_Q_SELECT
:
1341 if (val
< VIRTIO_QUEUE_MAX
) {
1342 vdev
->queue_sel
= val
;
1345 case VIRTIO_PCI_COMMON_Q_SIZE
:
1346 proxy
->vqs
[vdev
->queue_sel
].num
= val
;
1347 virtio_queue_set_num(vdev
, vdev
->queue_sel
,
1348 proxy
->vqs
[vdev
->queue_sel
].num
);
1350 case VIRTIO_PCI_COMMON_Q_MSIX
:
1351 vector
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
1352 if (vector
!= VIRTIO_NO_VECTOR
) {
1353 msix_vector_unuse(&proxy
->pci_dev
, vector
);
1355 /* Make it possible for guest to discover an error took place. */
1356 if (val
< proxy
->nvectors
) {
1357 msix_vector_use(&proxy
->pci_dev
, val
);
1359 val
= VIRTIO_NO_VECTOR
;
1361 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
1363 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1365 virtio_queue_set_num(vdev
, vdev
->queue_sel
,
1366 proxy
->vqs
[vdev
->queue_sel
].num
);
1367 virtio_queue_set_rings(vdev
, vdev
->queue_sel
,
1368 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].desc
[1]) << 32 |
1369 proxy
->vqs
[vdev
->queue_sel
].desc
[0],
1370 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].avail
[1]) << 32 |
1371 proxy
->vqs
[vdev
->queue_sel
].avail
[0],
1372 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].used
[1]) << 32 |
1373 proxy
->vqs
[vdev
->queue_sel
].used
[0]);
1374 proxy
->vqs
[vdev
->queue_sel
].enabled
= 1;
1375 proxy
->vqs
[vdev
->queue_sel
].reset
= 0;
1376 virtio_queue_enable(vdev
, vdev
->queue_sel
);
1378 virtio_error(vdev
, "wrong value for queue_enable %"PRIx64
, val
);
1381 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1382 proxy
->vqs
[vdev
->queue_sel
].desc
[0] = val
;
1384 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1385 proxy
->vqs
[vdev
->queue_sel
].desc
[1] = val
;
1387 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1388 proxy
->vqs
[vdev
->queue_sel
].avail
[0] = val
;
1390 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1391 proxy
->vqs
[vdev
->queue_sel
].avail
[1] = val
;
1393 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1394 proxy
->vqs
[vdev
->queue_sel
].used
[0] = val
;
1396 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1397 proxy
->vqs
[vdev
->queue_sel
].used
[1] = val
;
1399 case VIRTIO_PCI_COMMON_Q_RESET
:
1401 proxy
->vqs
[vdev
->queue_sel
].reset
= 1;
1403 virtio_queue_reset(vdev
, vdev
->queue_sel
);
1405 proxy
->vqs
[vdev
->queue_sel
].reset
= 0;
1406 proxy
->vqs
[vdev
->queue_sel
].enabled
= 0;
1415 static uint64_t virtio_pci_notify_read(void *opaque
, hwaddr addr
,
1418 VirtIOPCIProxy
*proxy
= opaque
;
1419 if (virtio_bus_get_device(&proxy
->bus
) == NULL
) {
1426 static void virtio_pci_notify_write(void *opaque
, hwaddr addr
,
1427 uint64_t val
, unsigned size
)
1429 VirtIOPCIProxy
*proxy
= opaque
;
1430 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1432 unsigned queue
= addr
/ virtio_pci_queue_mem_mult(proxy
);
1434 if (vdev
!= NULL
&& queue
< VIRTIO_QUEUE_MAX
) {
1435 trace_virtio_pci_notify_write(addr
, val
, size
);
1436 virtio_queue_notify(vdev
, queue
);
1440 static void virtio_pci_notify_write_pio(void *opaque
, hwaddr addr
,
1441 uint64_t val
, unsigned size
)
1443 VirtIOPCIProxy
*proxy
= opaque
;
1444 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1446 unsigned queue
= val
;
1448 if (vdev
!= NULL
&& queue
< VIRTIO_QUEUE_MAX
) {
1449 trace_virtio_pci_notify_write_pio(addr
, val
, size
);
1450 virtio_queue_notify(vdev
, queue
);
1454 static uint64_t virtio_pci_isr_read(void *opaque
, hwaddr addr
,
1457 VirtIOPCIProxy
*proxy
= opaque
;
1458 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1465 val
= qatomic_xchg(&vdev
->isr
, 0);
1466 pci_irq_deassert(&proxy
->pci_dev
);
1470 static void virtio_pci_isr_write(void *opaque
, hwaddr addr
,
1471 uint64_t val
, unsigned size
)
1475 static uint64_t virtio_pci_device_read(void *opaque
, hwaddr addr
,
1478 VirtIOPCIProxy
*proxy
= opaque
;
1479 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1488 val
= virtio_config_modern_readb(vdev
, addr
);
1491 val
= virtio_config_modern_readw(vdev
, addr
);
1494 val
= virtio_config_modern_readl(vdev
, addr
);
1503 static void virtio_pci_device_write(void *opaque
, hwaddr addr
,
1504 uint64_t val
, unsigned size
)
1506 VirtIOPCIProxy
*proxy
= opaque
;
1507 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1515 virtio_config_modern_writeb(vdev
, addr
, val
);
1518 virtio_config_modern_writew(vdev
, addr
, val
);
1521 virtio_config_modern_writel(vdev
, addr
, val
);
1526 static void virtio_pci_modern_regions_init(VirtIOPCIProxy
*proxy
,
1527 const char *vdev_name
)
1529 static const MemoryRegionOps common_ops
= {
1530 .read
= virtio_pci_common_read
,
1531 .write
= virtio_pci_common_write
,
1533 .min_access_size
= 1,
1534 .max_access_size
= 4,
1536 .endianness
= DEVICE_LITTLE_ENDIAN
,
1538 static const MemoryRegionOps isr_ops
= {
1539 .read
= virtio_pci_isr_read
,
1540 .write
= virtio_pci_isr_write
,
1542 .min_access_size
= 1,
1543 .max_access_size
= 4,
1545 .endianness
= DEVICE_LITTLE_ENDIAN
,
1547 static const MemoryRegionOps device_ops
= {
1548 .read
= virtio_pci_device_read
,
1549 .write
= virtio_pci_device_write
,
1551 .min_access_size
= 1,
1552 .max_access_size
= 4,
1554 .endianness
= DEVICE_LITTLE_ENDIAN
,
1556 static const MemoryRegionOps notify_ops
= {
1557 .read
= virtio_pci_notify_read
,
1558 .write
= virtio_pci_notify_write
,
1560 .min_access_size
= 1,
1561 .max_access_size
= 4,
1563 .endianness
= DEVICE_LITTLE_ENDIAN
,
1565 static const MemoryRegionOps notify_pio_ops
= {
1566 .read
= virtio_pci_notify_read
,
1567 .write
= virtio_pci_notify_write_pio
,
1569 .min_access_size
= 1,
1570 .max_access_size
= 4,
1572 .endianness
= DEVICE_LITTLE_ENDIAN
,
1574 g_autoptr(GString
) name
= g_string_new(NULL
);
1576 g_string_printf(name
, "virtio-pci-common-%s", vdev_name
);
1577 memory_region_init_io(&proxy
->common
.mr
, OBJECT(proxy
),
1581 proxy
->common
.size
);
1583 g_string_printf(name
, "virtio-pci-isr-%s", vdev_name
);
1584 memory_region_init_io(&proxy
->isr
.mr
, OBJECT(proxy
),
1590 g_string_printf(name
, "virtio-pci-device-%s", vdev_name
);
1591 memory_region_init_io(&proxy
->device
.mr
, OBJECT(proxy
),
1595 proxy
->device
.size
);
1597 g_string_printf(name
, "virtio-pci-notify-%s", vdev_name
);
1598 memory_region_init_io(&proxy
->notify
.mr
, OBJECT(proxy
),
1602 proxy
->notify
.size
);
1604 g_string_printf(name
, "virtio-pci-notify-pio-%s", vdev_name
);
1605 memory_region_init_io(&proxy
->notify_pio
.mr
, OBJECT(proxy
),
1609 proxy
->notify_pio
.size
);
1612 static void virtio_pci_modern_region_map(VirtIOPCIProxy
*proxy
,
1613 VirtIOPCIRegion
*region
,
1614 struct virtio_pci_cap
*cap
,
1618 memory_region_add_subregion(mr
, region
->offset
, ®ion
->mr
);
1620 cap
->cfg_type
= region
->type
;
1622 cap
->offset
= cpu_to_le32(region
->offset
);
1623 cap
->length
= cpu_to_le32(region
->size
);
1624 virtio_pci_add_mem_cap(proxy
, cap
);
1628 static void virtio_pci_modern_mem_region_map(VirtIOPCIProxy
*proxy
,
1629 VirtIOPCIRegion
*region
,
1630 struct virtio_pci_cap
*cap
)
1632 virtio_pci_modern_region_map(proxy
, region
, cap
,
1633 &proxy
->modern_bar
, proxy
->modern_mem_bar_idx
);
1636 static void virtio_pci_modern_io_region_map(VirtIOPCIProxy
*proxy
,
1637 VirtIOPCIRegion
*region
,
1638 struct virtio_pci_cap
*cap
)
1640 virtio_pci_modern_region_map(proxy
, region
, cap
,
1641 &proxy
->io_bar
, proxy
->modern_io_bar_idx
);
1644 static void virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy
*proxy
,
1645 VirtIOPCIRegion
*region
)
1647 memory_region_del_subregion(&proxy
->modern_bar
,
1651 static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy
*proxy
,
1652 VirtIOPCIRegion
*region
)
1654 memory_region_del_subregion(&proxy
->io_bar
,
1658 static void virtio_pci_pre_plugged(DeviceState
*d
, Error
**errp
)
1660 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1661 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1663 if (virtio_pci_modern(proxy
)) {
1664 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_VERSION_1
);
1667 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_BAD_FEATURE
);
1670 /* This is called by virtio-bus just after the device is plugged. */
1671 static void virtio_pci_device_plugged(DeviceState
*d
, Error
**errp
)
1673 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1674 VirtioBusState
*bus
= &proxy
->bus
;
1675 bool legacy
= virtio_pci_legacy(proxy
);
1677 bool modern_pio
= proxy
->flags
& VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY
;
1680 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1683 * Virtio capabilities present without
1684 * VIRTIO_F_VERSION_1 confuses guests
1686 if (!proxy
->ignore_backend_features
&&
1687 !virtio_has_feature(vdev
->host_features
, VIRTIO_F_VERSION_1
)) {
1688 virtio_pci_disable_modern(proxy
);
1691 error_setg(errp
, "Device doesn't support modern mode, and legacy"
1692 " mode is disabled");
1693 error_append_hint(errp
, "Set disable-legacy to off\n");
1699 modern
= virtio_pci_modern(proxy
);
1701 config
= proxy
->pci_dev
.config
;
1702 if (proxy
->class_code
) {
1703 pci_config_set_class(config
, proxy
->class_code
);
1707 if (!virtio_legacy_allowed(vdev
)) {
1709 * To avoid migration issues, we allow legacy mode when legacy
1710 * check is disabled in the old machine types (< 5.1).
1712 if (virtio_legacy_check_disabled(vdev
)) {
1713 warn_report("device is modern-only, but for backward "
1714 "compatibility legacy is allowed");
1717 "device is modern-only, use disable-legacy=on");
1721 if (virtio_host_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
)) {
1722 error_setg(errp
, "VIRTIO_F_IOMMU_PLATFORM was supported by"
1723 " neither legacy nor transitional device");
1727 * Legacy and transitional devices use specific subsystem IDs.
1728 * Note that the subsystem vendor ID (config + PCI_SUBSYSTEM_VENDOR_ID)
1729 * is set to PCI_SUBVENDOR_ID_REDHAT_QUMRANET by default.
1731 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, virtio_bus_get_vdev_id(bus
));
1733 /* pure virtio-1.0 */
1734 pci_set_word(config
+ PCI_VENDOR_ID
,
1735 PCI_VENDOR_ID_REDHAT_QUMRANET
);
1736 pci_set_word(config
+ PCI_DEVICE_ID
,
1737 PCI_DEVICE_ID_VIRTIO_10_BASE
+ virtio_bus_get_vdev_id(bus
));
1738 pci_config_set_revision(config
, 1);
1740 config
[PCI_INTERRUPT_PIN
] = 1;
1744 struct virtio_pci_cap cap
= {
1745 .cap_len
= sizeof cap
,
1747 struct virtio_pci_notify_cap notify
= {
1748 .cap
.cap_len
= sizeof notify
,
1749 .notify_off_multiplier
=
1750 cpu_to_le32(virtio_pci_queue_mem_mult(proxy
)),
1752 struct virtio_pci_cfg_cap cfg
= {
1753 .cap
.cap_len
= sizeof cfg
,
1754 .cap
.cfg_type
= VIRTIO_PCI_CAP_PCI_CFG
,
1756 struct virtio_pci_notify_cap notify_pio
= {
1757 .cap
.cap_len
= sizeof notify
,
1758 .notify_off_multiplier
= cpu_to_le32(0x0),
1761 struct virtio_pci_cfg_cap
*cfg_mask
;
1763 virtio_pci_modern_regions_init(proxy
, vdev
->name
);
1765 virtio_pci_modern_mem_region_map(proxy
, &proxy
->common
, &cap
);
1766 virtio_pci_modern_mem_region_map(proxy
, &proxy
->isr
, &cap
);
1767 virtio_pci_modern_mem_region_map(proxy
, &proxy
->device
, &cap
);
1768 virtio_pci_modern_mem_region_map(proxy
, &proxy
->notify
, ¬ify
.cap
);
1771 memory_region_init(&proxy
->io_bar
, OBJECT(proxy
),
1772 "virtio-pci-io", 0x4);
1774 pci_register_bar(&proxy
->pci_dev
, proxy
->modern_io_bar_idx
,
1775 PCI_BASE_ADDRESS_SPACE_IO
, &proxy
->io_bar
);
1777 virtio_pci_modern_io_region_map(proxy
, &proxy
->notify_pio
,
1781 pci_register_bar(&proxy
->pci_dev
, proxy
->modern_mem_bar_idx
,
1782 PCI_BASE_ADDRESS_SPACE_MEMORY
|
1783 PCI_BASE_ADDRESS_MEM_PREFETCH
|
1784 PCI_BASE_ADDRESS_MEM_TYPE_64
,
1785 &proxy
->modern_bar
);
1787 proxy
->config_cap
= virtio_pci_add_mem_cap(proxy
, &cfg
.cap
);
1788 cfg_mask
= (void *)(proxy
->pci_dev
.wmask
+ proxy
->config_cap
);
1789 pci_set_byte(&cfg_mask
->cap
.bar
, ~0x0);
1790 pci_set_long((uint8_t *)&cfg_mask
->cap
.offset
, ~0x0);
1791 pci_set_long((uint8_t *)&cfg_mask
->cap
.length
, ~0x0);
1792 pci_set_long(cfg_mask
->pci_cfg_data
, ~0x0);
1795 if (proxy
->nvectors
) {
1796 int err
= msix_init_exclusive_bar(&proxy
->pci_dev
, proxy
->nvectors
,
1797 proxy
->msix_bar_idx
, NULL
);
1799 /* Notice when a system that supports MSIx can't initialize it */
1800 if (err
!= -ENOTSUP
) {
1801 warn_report("unable to init msix vectors to %" PRIu32
,
1804 proxy
->nvectors
= 0;
1808 proxy
->pci_dev
.config_write
= virtio_write_config
;
1809 proxy
->pci_dev
.config_read
= virtio_read_config
;
1812 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
)
1813 + virtio_bus_get_vdev_config_len(bus
);
1814 size
= pow2ceil(size
);
1816 memory_region_init_io(&proxy
->bar
, OBJECT(proxy
),
1817 &virtio_pci_config_ops
,
1818 proxy
, "virtio-pci", size
);
1820 pci_register_bar(&proxy
->pci_dev
, proxy
->legacy_io_bar_idx
,
1821 PCI_BASE_ADDRESS_SPACE_IO
, &proxy
->bar
);
1825 static void virtio_pci_device_unplugged(DeviceState
*d
)
1827 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1828 bool modern
= virtio_pci_modern(proxy
);
1829 bool modern_pio
= proxy
->flags
& VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY
;
1831 virtio_pci_stop_ioeventfd(proxy
);
1834 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->common
);
1835 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->isr
);
1836 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->device
);
1837 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->notify
);
1839 virtio_pci_modern_io_region_unmap(proxy
, &proxy
->notify_pio
);
1844 static void virtio_pci_realize(PCIDevice
*pci_dev
, Error
**errp
)
1846 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
1847 VirtioPCIClass
*k
= VIRTIO_PCI_GET_CLASS(pci_dev
);
1848 bool pcie_port
= pci_bus_is_express(pci_get_bus(pci_dev
)) &&
1849 !pci_bus_is_root(pci_get_bus(pci_dev
));
1851 if (kvm_enabled() && !kvm_has_many_ioeventfds()) {
1852 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
1855 /* fd-based ioevents can't be synchronized in record/replay */
1856 if (replay_mode
!= REPLAY_MODE_NONE
) {
1857 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
1861 * virtio pci bar layout used by default.
1862 * subclasses can re-arrange things if needed.
1864 * region 0 -- virtio legacy io bar
1865 * region 1 -- msi-x bar
1866 * region 2 -- virtio modern io bar (off by default)
1867 * region 4+5 -- virtio modern memory (64bit) bar
1870 proxy
->legacy_io_bar_idx
= 0;
1871 proxy
->msix_bar_idx
= 1;
1872 proxy
->modern_io_bar_idx
= 2;
1873 proxy
->modern_mem_bar_idx
= 4;
1875 proxy
->common
.offset
= 0x0;
1876 proxy
->common
.size
= 0x1000;
1877 proxy
->common
.type
= VIRTIO_PCI_CAP_COMMON_CFG
;
1879 proxy
->isr
.offset
= 0x1000;
1880 proxy
->isr
.size
= 0x1000;
1881 proxy
->isr
.type
= VIRTIO_PCI_CAP_ISR_CFG
;
1883 proxy
->device
.offset
= 0x2000;
1884 proxy
->device
.size
= 0x1000;
1885 proxy
->device
.type
= VIRTIO_PCI_CAP_DEVICE_CFG
;
1887 proxy
->notify
.offset
= 0x3000;
1888 proxy
->notify
.size
= virtio_pci_queue_mem_mult(proxy
) * VIRTIO_QUEUE_MAX
;
1889 proxy
->notify
.type
= VIRTIO_PCI_CAP_NOTIFY_CFG
;
1891 proxy
->notify_pio
.offset
= 0x0;
1892 proxy
->notify_pio
.size
= 0x4;
1893 proxy
->notify_pio
.type
= VIRTIO_PCI_CAP_NOTIFY_CFG
;
1895 /* subclasses can enforce modern, so do this unconditionally */
1896 memory_region_init(&proxy
->modern_bar
, OBJECT(proxy
), "virtio-pci",
1897 /* PCI BAR regions must be powers of 2 */
1898 pow2ceil(proxy
->notify
.offset
+ proxy
->notify
.size
));
1900 if (proxy
->disable_legacy
== ON_OFF_AUTO_AUTO
) {
1901 proxy
->disable_legacy
= pcie_port
? ON_OFF_AUTO_ON
: ON_OFF_AUTO_OFF
;
1904 if (!virtio_pci_modern(proxy
) && !virtio_pci_legacy(proxy
)) {
1905 error_setg(errp
, "device cannot work as neither modern nor legacy mode"
1907 error_append_hint(errp
, "Set either disable-modern or disable-legacy"
1912 if (pcie_port
&& pci_is_express(pci_dev
)) {
1914 uint16_t last_pcie_cap_offset
= PCI_CONFIG_SPACE_SIZE
;
1916 pos
= pcie_endpoint_cap_init(pci_dev
, 0);
1919 pos
= pci_add_capability(pci_dev
, PCI_CAP_ID_PM
, 0,
1920 PCI_PM_SIZEOF
, errp
);
1925 pci_dev
->exp
.pm_cap
= pos
;
1928 * Indicates that this function complies with revision 1.2 of the
1929 * PCI Power Management Interface Specification.
1931 pci_set_word(pci_dev
->config
+ pos
+ PCI_PM_PMC
, 0x3);
1933 if (proxy
->flags
& VIRTIO_PCI_FLAG_AER
) {
1934 pcie_aer_init(pci_dev
, PCI_ERR_VER
, last_pcie_cap_offset
,
1935 PCI_ERR_SIZEOF
, NULL
);
1936 last_pcie_cap_offset
+= PCI_ERR_SIZEOF
;
1939 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_DEVERR
) {
1940 /* Init error enabling flags */
1941 pcie_cap_deverr_init(pci_dev
);
1944 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_LNKCTL
) {
1945 /* Init Link Control Register */
1946 pcie_cap_lnkctl_init(pci_dev
);
1949 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_PM
) {
1950 /* Init Power Management Control Register */
1951 pci_set_word(pci_dev
->wmask
+ pos
+ PCI_PM_CTRL
,
1952 PCI_PM_CTRL_STATE_MASK
);
1955 if (proxy
->flags
& VIRTIO_PCI_FLAG_ATS
) {
1956 pcie_ats_init(pci_dev
, last_pcie_cap_offset
,
1957 proxy
->flags
& VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED
);
1958 last_pcie_cap_offset
+= PCI_EXT_CAP_ATS_SIZEOF
;
1961 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_FLR
) {
1962 /* Set Function Level Reset capability bit */
1963 pcie_cap_flr_init(pci_dev
);
1967 * make future invocations of pci_is_express() return false
1968 * and pci_config_size() return PCI_CONFIG_SPACE_SIZE.
1970 pci_dev
->cap_present
&= ~QEMU_PCI_CAP_EXPRESS
;
1973 virtio_pci_bus_new(&proxy
->bus
, sizeof(proxy
->bus
), proxy
);
1975 k
->realize(proxy
, errp
);
1979 static void virtio_pci_exit(PCIDevice
*pci_dev
)
1981 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
1982 bool pcie_port
= pci_bus_is_express(pci_get_bus(pci_dev
)) &&
1983 !pci_bus_is_root(pci_get_bus(pci_dev
));
1985 msix_uninit_exclusive_bar(pci_dev
);
1986 if (proxy
->flags
& VIRTIO_PCI_FLAG_AER
&& pcie_port
&&
1987 pci_is_express(pci_dev
)) {
1988 pcie_aer_exit(pci_dev
);
1992 static void virtio_pci_reset(DeviceState
*qdev
)
1994 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1995 VirtioBusState
*bus
= VIRTIO_BUS(&proxy
->bus
);
1998 virtio_bus_reset(bus
);
1999 msix_unuse_all_vectors(&proxy
->pci_dev
);
2001 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2002 proxy
->vqs
[i
].enabled
= 0;
2003 proxy
->vqs
[i
].reset
= 0;
2004 proxy
->vqs
[i
].num
= 0;
2005 proxy
->vqs
[i
].desc
[0] = proxy
->vqs
[i
].desc
[1] = 0;
2006 proxy
->vqs
[i
].avail
[0] = proxy
->vqs
[i
].avail
[1] = 0;
2007 proxy
->vqs
[i
].used
[0] = proxy
->vqs
[i
].used
[1] = 0;
2011 static void virtio_pci_bus_reset_hold(Object
*obj
)
2013 PCIDevice
*dev
= PCI_DEVICE(obj
);
2014 DeviceState
*qdev
= DEVICE(obj
);
2016 virtio_pci_reset(qdev
);
2018 if (pci_is_express(dev
)) {
2019 pcie_cap_deverr_reset(dev
);
2020 pcie_cap_lnkctl_reset(dev
);
2022 pci_set_word(dev
->config
+ dev
->exp
.pm_cap
+ PCI_PM_CTRL
, 0);
2026 static Property virtio_pci_properties
[] = {
2027 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy
, flags
,
2028 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT
, false),
2029 DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy
, flags
,
2030 VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT
, true),
2031 DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy
, flags
,
2032 VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT
, false),
2033 DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy
, flags
,
2034 VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT
, false),
2035 DEFINE_PROP_BIT("page-per-vq", VirtIOPCIProxy
, flags
,
2036 VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT
, false),
2037 DEFINE_PROP_BOOL("x-ignore-backend-features", VirtIOPCIProxy
,
2038 ignore_backend_features
, false),
2039 DEFINE_PROP_BIT("ats", VirtIOPCIProxy
, flags
,
2040 VIRTIO_PCI_FLAG_ATS_BIT
, false),
2041 DEFINE_PROP_BIT("x-ats-page-aligned", VirtIOPCIProxy
, flags
,
2042 VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED_BIT
, true),
2043 DEFINE_PROP_BIT("x-pcie-deverr-init", VirtIOPCIProxy
, flags
,
2044 VIRTIO_PCI_FLAG_INIT_DEVERR_BIT
, true),
2045 DEFINE_PROP_BIT("x-pcie-lnkctl-init", VirtIOPCIProxy
, flags
,
2046 VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT
, true),
2047 DEFINE_PROP_BIT("x-pcie-pm-init", VirtIOPCIProxy
, flags
,
2048 VIRTIO_PCI_FLAG_INIT_PM_BIT
, true),
2049 DEFINE_PROP_BIT("x-pcie-flr-init", VirtIOPCIProxy
, flags
,
2050 VIRTIO_PCI_FLAG_INIT_FLR_BIT
, true),
2051 DEFINE_PROP_BIT("aer", VirtIOPCIProxy
, flags
,
2052 VIRTIO_PCI_FLAG_AER_BIT
, false),
2053 DEFINE_PROP_END_OF_LIST(),
2056 static void virtio_pci_dc_realize(DeviceState
*qdev
, Error
**errp
)
2058 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_GET_CLASS(qdev
);
2059 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
2060 PCIDevice
*pci_dev
= &proxy
->pci_dev
;
2062 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_PCIE
) &&
2063 virtio_pci_modern(proxy
)) {
2064 pci_dev
->cap_present
|= QEMU_PCI_CAP_EXPRESS
;
2067 vpciklass
->parent_dc_realize(qdev
, errp
);
2070 static void virtio_pci_class_init(ObjectClass
*klass
, void *data
)
2072 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2073 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2074 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_CLASS(klass
);
2075 ResettableClass
*rc
= RESETTABLE_CLASS(klass
);
2077 device_class_set_props(dc
, virtio_pci_properties
);
2078 k
->realize
= virtio_pci_realize
;
2079 k
->exit
= virtio_pci_exit
;
2080 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2081 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
2082 k
->class_id
= PCI_CLASS_OTHERS
;
2083 device_class_set_parent_realize(dc
, virtio_pci_dc_realize
,
2084 &vpciklass
->parent_dc_realize
);
2085 rc
->phases
.hold
= virtio_pci_bus_reset_hold
;
2088 static const TypeInfo virtio_pci_info
= {
2089 .name
= TYPE_VIRTIO_PCI
,
2090 .parent
= TYPE_PCI_DEVICE
,
2091 .instance_size
= sizeof(VirtIOPCIProxy
),
2092 .class_init
= virtio_pci_class_init
,
2093 .class_size
= sizeof(VirtioPCIClass
),
2097 static Property virtio_pci_generic_properties
[] = {
2098 DEFINE_PROP_ON_OFF_AUTO("disable-legacy", VirtIOPCIProxy
, disable_legacy
,
2100 DEFINE_PROP_BOOL("disable-modern", VirtIOPCIProxy
, disable_modern
, false),
2101 DEFINE_PROP_END_OF_LIST(),
2104 static void virtio_pci_base_class_init(ObjectClass
*klass
, void *data
)
2106 const VirtioPCIDeviceTypeInfo
*t
= data
;
2107 if (t
->class_init
) {
2108 t
->class_init(klass
, NULL
);
2112 static void virtio_pci_generic_class_init(ObjectClass
*klass
, void *data
)
2114 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2116 device_class_set_props(dc
, virtio_pci_generic_properties
);
2119 static void virtio_pci_transitional_instance_init(Object
*obj
)
2121 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(obj
);
2123 proxy
->disable_legacy
= ON_OFF_AUTO_OFF
;
2124 proxy
->disable_modern
= false;
2127 static void virtio_pci_non_transitional_instance_init(Object
*obj
)
2129 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(obj
);
2131 proxy
->disable_legacy
= ON_OFF_AUTO_ON
;
2132 proxy
->disable_modern
= false;
2135 void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo
*t
)
2137 char *base_name
= NULL
;
2138 TypeInfo base_type_info
= {
2139 .name
= t
->base_name
,
2140 .parent
= t
->parent
? t
->parent
: TYPE_VIRTIO_PCI
,
2141 .instance_size
= t
->instance_size
,
2142 .instance_init
= t
->instance_init
,
2143 .class_size
= t
->class_size
,
2145 .interfaces
= t
->interfaces
,
2147 TypeInfo generic_type_info
= {
2148 .name
= t
->generic_name
,
2149 .parent
= base_type_info
.name
,
2150 .class_init
= virtio_pci_generic_class_init
,
2151 .interfaces
= (InterfaceInfo
[]) {
2152 { INTERFACE_PCIE_DEVICE
},
2153 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
2158 if (!base_type_info
.name
) {
2159 /* No base type -> register a single generic device type */
2160 /* use intermediate %s-base-type to add generic device props */
2161 base_name
= g_strdup_printf("%s-base-type", t
->generic_name
);
2162 base_type_info
.name
= base_name
;
2163 base_type_info
.class_init
= virtio_pci_generic_class_init
;
2165 generic_type_info
.parent
= base_name
;
2166 generic_type_info
.class_init
= virtio_pci_base_class_init
;
2167 generic_type_info
.class_data
= (void *)t
;
2169 assert(!t
->non_transitional_name
);
2170 assert(!t
->transitional_name
);
2172 base_type_info
.class_init
= virtio_pci_base_class_init
;
2173 base_type_info
.class_data
= (void *)t
;
2176 type_register(&base_type_info
);
2177 if (generic_type_info
.name
) {
2178 type_register(&generic_type_info
);
2181 if (t
->non_transitional_name
) {
2182 const TypeInfo non_transitional_type_info
= {
2183 .name
= t
->non_transitional_name
,
2184 .parent
= base_type_info
.name
,
2185 .instance_init
= virtio_pci_non_transitional_instance_init
,
2186 .interfaces
= (InterfaceInfo
[]) {
2187 { INTERFACE_PCIE_DEVICE
},
2188 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
2192 type_register(&non_transitional_type_info
);
2195 if (t
->transitional_name
) {
2196 const TypeInfo transitional_type_info
= {
2197 .name
= t
->transitional_name
,
2198 .parent
= base_type_info
.name
,
2199 .instance_init
= virtio_pci_transitional_instance_init
,
2200 .interfaces
= (InterfaceInfo
[]) {
2202 * Transitional virtio devices work only as Conventional PCI
2203 * devices because they require PIO ports.
2205 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
2209 type_register(&transitional_type_info
);
2214 unsigned virtio_pci_optimal_num_queues(unsigned fixed_queues
)
2217 * 1:1 vq to vCPU mapping is ideal because the same vCPU that submitted
2218 * virtqueue buffers can handle their completion. When a different vCPU
2219 * handles completion it may need to IPI the vCPU that submitted the
2220 * request and this adds overhead.
2222 * Virtqueues consume guest RAM and MSI-X vectors. This is wasteful in
2223 * guests with very many vCPUs and a device that is only used by a few
2224 * vCPUs. Unfortunately optimizing that case requires manual pinning inside
2225 * the guest, so those users might as well manually set the number of
2226 * queues. There is no upper limit that can be applied automatically and
2227 * doing so arbitrarily would result in a sudden performance drop once the
2228 * threshold number of vCPUs is exceeded.
2230 unsigned num_queues
= current_machine
->smp
.cpus
;
2233 * The maximum number of MSI-X vectors is PCI_MSIX_FLAGS_QSIZE + 1, but the
2234 * config change interrupt and the fixed virtqueues must be taken into
2237 num_queues
= MIN(num_queues
, PCI_MSIX_FLAGS_QSIZE
- fixed_queues
);
2240 * There is a limit to how many virtqueues a device can have.
2242 return MIN(num_queues
, VIRTIO_QUEUE_MAX
- fixed_queues
);
2245 /* virtio-pci-bus */
2247 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
2248 VirtIOPCIProxy
*dev
)
2250 DeviceState
*qdev
= DEVICE(dev
);
2251 char virtio_bus_name
[] = "virtio-bus";
2253 qbus_init(bus
, bus_size
, TYPE_VIRTIO_PCI_BUS
, qdev
, virtio_bus_name
);
2256 static void virtio_pci_bus_class_init(ObjectClass
*klass
, void *data
)
2258 BusClass
*bus_class
= BUS_CLASS(klass
);
2259 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
2260 bus_class
->max_dev
= 1;
2261 k
->notify
= virtio_pci_notify
;
2262 k
->save_config
= virtio_pci_save_config
;
2263 k
->load_config
= virtio_pci_load_config
;
2264 k
->save_queue
= virtio_pci_save_queue
;
2265 k
->load_queue
= virtio_pci_load_queue
;
2266 k
->save_extra_state
= virtio_pci_save_extra_state
;
2267 k
->load_extra_state
= virtio_pci_load_extra_state
;
2268 k
->has_extra_state
= virtio_pci_has_extra_state
;
2269 k
->query_guest_notifiers
= virtio_pci_query_guest_notifiers
;
2270 k
->set_guest_notifiers
= virtio_pci_set_guest_notifiers
;
2271 k
->set_host_notifier_mr
= virtio_pci_set_host_notifier_mr
;
2272 k
->vmstate_change
= virtio_pci_vmstate_change
;
2273 k
->pre_plugged
= virtio_pci_pre_plugged
;
2274 k
->device_plugged
= virtio_pci_device_plugged
;
2275 k
->device_unplugged
= virtio_pci_device_unplugged
;
2276 k
->query_nvectors
= virtio_pci_query_nvectors
;
2277 k
->ioeventfd_enabled
= virtio_pci_ioeventfd_enabled
;
2278 k
->ioeventfd_assign
= virtio_pci_ioeventfd_assign
;
2279 k
->get_dma_as
= virtio_pci_get_dma_as
;
2280 k
->iommu_enabled
= virtio_pci_iommu_enabled
;
2281 k
->queue_enabled
= virtio_pci_queue_enabled
;
2284 static const TypeInfo virtio_pci_bus_info
= {
2285 .name
= TYPE_VIRTIO_PCI_BUS
,
2286 .parent
= TYPE_VIRTIO_BUS
,
2287 .instance_size
= sizeof(VirtioPCIBusState
),
2288 .class_size
= sizeof(VirtioPCIBusClass
),
2289 .class_init
= virtio_pci_bus_class_init
,
2292 static void virtio_pci_register_types(void)
2295 type_register_static(&virtio_pci_bus_info
);
2296 type_register_static(&virtio_pci_info
);
2299 type_init(virtio_pci_register_types
)