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 "virtio-pci.h"
37 #include "qemu/range.h"
38 #include "hw/virtio/virtio-bus.h"
39 #include "qapi/visitor.h"
41 #define VIRTIO_PCI_REGION_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_present(dev))
43 #undef VIRTIO_PCI_CONFIG
45 /* The remaining space is defined by each driver as the per-driver
46 * configuration space */
47 #define VIRTIO_PCI_CONFIG_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_enabled(dev))
49 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
51 static void virtio_pci_reset(DeviceState
*qdev
);
54 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
55 static inline VirtIOPCIProxy
*to_virtio_pci_proxy(DeviceState
*d
)
57 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
60 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
61 * be careful and test performance if you change this.
63 static inline VirtIOPCIProxy
*to_virtio_pci_proxy_fast(DeviceState
*d
)
65 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
68 static void virtio_pci_notify(DeviceState
*d
, uint16_t vector
)
70 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy_fast(d
);
72 if (msix_enabled(&proxy
->pci_dev
))
73 msix_notify(&proxy
->pci_dev
, vector
);
75 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
76 pci_set_irq(&proxy
->pci_dev
, qatomic_read(&vdev
->isr
) & 1);
80 static void virtio_pci_save_config(DeviceState
*d
, QEMUFile
*f
)
82 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
83 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
85 pci_device_save(&proxy
->pci_dev
, f
);
86 msix_save(&proxy
->pci_dev
, f
);
87 if (msix_present(&proxy
->pci_dev
))
88 qemu_put_be16(f
, vdev
->config_vector
);
91 static const VMStateDescription vmstate_virtio_pci_modern_queue_state
= {
92 .name
= "virtio_pci/modern_queue_state",
94 .minimum_version_id
= 1,
95 .fields
= (VMStateField
[]) {
96 VMSTATE_UINT16(num
, VirtIOPCIQueue
),
97 VMSTATE_UNUSED(1), /* enabled was stored as be16 */
98 VMSTATE_BOOL(enabled
, VirtIOPCIQueue
),
99 VMSTATE_UINT32_ARRAY(desc
, VirtIOPCIQueue
, 2),
100 VMSTATE_UINT32_ARRAY(avail
, VirtIOPCIQueue
, 2),
101 VMSTATE_UINT32_ARRAY(used
, VirtIOPCIQueue
, 2),
102 VMSTATE_END_OF_LIST()
106 static bool virtio_pci_modern_state_needed(void *opaque
)
108 VirtIOPCIProxy
*proxy
= opaque
;
110 return virtio_pci_modern(proxy
);
113 static const VMStateDescription vmstate_virtio_pci_modern_state_sub
= {
114 .name
= "virtio_pci/modern_state",
116 .minimum_version_id
= 1,
117 .needed
= &virtio_pci_modern_state_needed
,
118 .fields
= (VMStateField
[]) {
119 VMSTATE_UINT32(dfselect
, VirtIOPCIProxy
),
120 VMSTATE_UINT32(gfselect
, VirtIOPCIProxy
),
121 VMSTATE_UINT32_ARRAY(guest_features
, VirtIOPCIProxy
, 2),
122 VMSTATE_STRUCT_ARRAY(vqs
, VirtIOPCIProxy
, VIRTIO_QUEUE_MAX
, 0,
123 vmstate_virtio_pci_modern_queue_state
,
125 VMSTATE_END_OF_LIST()
129 static const VMStateDescription vmstate_virtio_pci
= {
130 .name
= "virtio_pci",
132 .minimum_version_id
= 1,
133 .minimum_version_id_old
= 1,
134 .fields
= (VMStateField
[]) {
135 VMSTATE_END_OF_LIST()
137 .subsections
= (const VMStateDescription
*[]) {
138 &vmstate_virtio_pci_modern_state_sub
,
143 static bool virtio_pci_has_extra_state(DeviceState
*d
)
145 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
147 return proxy
->flags
& VIRTIO_PCI_FLAG_MIGRATE_EXTRA
;
150 static void virtio_pci_save_extra_state(DeviceState
*d
, QEMUFile
*f
)
152 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
154 vmstate_save_state(f
, &vmstate_virtio_pci
, proxy
, NULL
);
157 static int virtio_pci_load_extra_state(DeviceState
*d
, QEMUFile
*f
)
159 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
161 return vmstate_load_state(f
, &vmstate_virtio_pci
, proxy
, 1);
164 static void virtio_pci_save_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
166 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
167 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
169 if (msix_present(&proxy
->pci_dev
))
170 qemu_put_be16(f
, virtio_queue_vector(vdev
, n
));
173 static int virtio_pci_load_config(DeviceState
*d
, QEMUFile
*f
)
175 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
176 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
179 ret
= pci_device_load(&proxy
->pci_dev
, f
);
183 msix_unuse_all_vectors(&proxy
->pci_dev
);
184 msix_load(&proxy
->pci_dev
, f
);
185 if (msix_present(&proxy
->pci_dev
)) {
186 qemu_get_be16s(f
, &vdev
->config_vector
);
188 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
190 if (vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
191 return msix_vector_use(&proxy
->pci_dev
, vdev
->config_vector
);
196 static int virtio_pci_load_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
198 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
199 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
202 if (msix_present(&proxy
->pci_dev
)) {
203 qemu_get_be16s(f
, &vector
);
205 vector
= VIRTIO_NO_VECTOR
;
207 virtio_queue_set_vector(vdev
, n
, vector
);
208 if (vector
!= VIRTIO_NO_VECTOR
) {
209 return msix_vector_use(&proxy
->pci_dev
, vector
);
215 static bool virtio_pci_ioeventfd_enabled(DeviceState
*d
)
217 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
219 return (proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) != 0;
222 #define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000
224 static inline int virtio_pci_queue_mem_mult(struct VirtIOPCIProxy
*proxy
)
226 return (proxy
->flags
& VIRTIO_PCI_FLAG_PAGE_PER_VQ
) ?
227 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
: 4;
230 static int virtio_pci_ioeventfd_assign(DeviceState
*d
, EventNotifier
*notifier
,
233 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
234 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
235 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
236 bool legacy
= virtio_pci_legacy(proxy
);
237 bool modern
= virtio_pci_modern(proxy
);
238 bool fast_mmio
= kvm_ioeventfd_any_length_enabled();
239 bool modern_pio
= proxy
->flags
& VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY
;
240 MemoryRegion
*modern_mr
= &proxy
->notify
.mr
;
241 MemoryRegion
*modern_notify_mr
= &proxy
->notify_pio
.mr
;
242 MemoryRegion
*legacy_mr
= &proxy
->bar
;
243 hwaddr modern_addr
= virtio_pci_queue_mem_mult(proxy
) *
244 virtio_get_queue_index(vq
);
245 hwaddr legacy_addr
= VIRTIO_PCI_QUEUE_NOTIFY
;
250 memory_region_add_eventfd(modern_mr
, modern_addr
, 0,
253 memory_region_add_eventfd(modern_mr
, modern_addr
, 2,
257 memory_region_add_eventfd(modern_notify_mr
, 0, 2,
262 memory_region_add_eventfd(legacy_mr
, legacy_addr
, 2,
268 memory_region_del_eventfd(modern_mr
, modern_addr
, 0,
271 memory_region_del_eventfd(modern_mr
, modern_addr
, 2,
275 memory_region_del_eventfd(modern_notify_mr
, 0, 2,
280 memory_region_del_eventfd(legacy_mr
, legacy_addr
, 2,
287 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
289 virtio_bus_start_ioeventfd(&proxy
->bus
);
292 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
294 virtio_bus_stop_ioeventfd(&proxy
->bus
);
297 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
299 VirtIOPCIProxy
*proxy
= opaque
;
300 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
304 case VIRTIO_PCI_GUEST_FEATURES
:
305 /* Guest does not negotiate properly? We have to assume nothing. */
306 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
307 val
= virtio_bus_get_vdev_bad_features(&proxy
->bus
);
309 virtio_set_features(vdev
, val
);
311 case VIRTIO_PCI_QUEUE_PFN
:
312 pa
= (hwaddr
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
314 virtio_pci_reset(DEVICE(proxy
));
317 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
319 case VIRTIO_PCI_QUEUE_SEL
:
320 if (val
< VIRTIO_QUEUE_MAX
)
321 vdev
->queue_sel
= val
;
323 case VIRTIO_PCI_QUEUE_NOTIFY
:
324 if (val
< VIRTIO_QUEUE_MAX
) {
325 virtio_queue_notify(vdev
, val
);
328 case VIRTIO_PCI_STATUS
:
329 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
330 virtio_pci_stop_ioeventfd(proxy
);
333 virtio_set_status(vdev
, val
& 0xFF);
335 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
336 virtio_pci_start_ioeventfd(proxy
);
339 if (vdev
->status
== 0) {
340 virtio_pci_reset(DEVICE(proxy
));
343 /* Linux before 2.6.34 drives the device without enabling
344 the PCI device bus master bit. Enable it automatically
345 for the guest. This is a PCI spec violation but so is
346 initiating DMA with bus master bit clear. */
347 if (val
== (VIRTIO_CONFIG_S_ACKNOWLEDGE
| VIRTIO_CONFIG_S_DRIVER
)) {
348 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
349 proxy
->pci_dev
.config
[PCI_COMMAND
] |
350 PCI_COMMAND_MASTER
, 1);
353 case VIRTIO_MSI_CONFIG_VECTOR
:
354 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
355 /* Make it possible for guest to discover an error took place. */
356 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
357 val
= VIRTIO_NO_VECTOR
;
358 vdev
->config_vector
= val
;
360 case VIRTIO_MSI_QUEUE_VECTOR
:
361 msix_vector_unuse(&proxy
->pci_dev
,
362 virtio_queue_vector(vdev
, vdev
->queue_sel
));
363 /* Make it possible for guest to discover an error took place. */
364 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
365 val
= VIRTIO_NO_VECTOR
;
366 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
369 qemu_log_mask(LOG_GUEST_ERROR
,
370 "%s: unexpected address 0x%x value 0x%x\n",
371 __func__
, addr
, val
);
376 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
378 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
379 uint32_t ret
= 0xFFFFFFFF;
382 case VIRTIO_PCI_HOST_FEATURES
:
383 ret
= vdev
->host_features
;
385 case VIRTIO_PCI_GUEST_FEATURES
:
386 ret
= vdev
->guest_features
;
388 case VIRTIO_PCI_QUEUE_PFN
:
389 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
390 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
392 case VIRTIO_PCI_QUEUE_NUM
:
393 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
395 case VIRTIO_PCI_QUEUE_SEL
:
396 ret
= vdev
->queue_sel
;
398 case VIRTIO_PCI_STATUS
:
402 /* reading from the ISR also clears it. */
403 ret
= qatomic_xchg(&vdev
->isr
, 0);
404 pci_irq_deassert(&proxy
->pci_dev
);
406 case VIRTIO_MSI_CONFIG_VECTOR
:
407 ret
= vdev
->config_vector
;
409 case VIRTIO_MSI_QUEUE_VECTOR
:
410 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
419 static uint64_t virtio_pci_config_read(void *opaque
, hwaddr addr
,
422 VirtIOPCIProxy
*proxy
= opaque
;
423 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
424 uint32_t config
= VIRTIO_PCI_CONFIG_SIZE(&proxy
->pci_dev
);
427 return virtio_ioport_read(proxy
, addr
);
433 val
= virtio_config_readb(vdev
, addr
);
436 val
= virtio_config_readw(vdev
, addr
);
437 if (virtio_is_big_endian(vdev
)) {
442 val
= virtio_config_readl(vdev
, addr
);
443 if (virtio_is_big_endian(vdev
)) {
451 static void virtio_pci_config_write(void *opaque
, hwaddr addr
,
452 uint64_t val
, unsigned size
)
454 VirtIOPCIProxy
*proxy
= opaque
;
455 uint32_t config
= VIRTIO_PCI_CONFIG_SIZE(&proxy
->pci_dev
);
456 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
458 virtio_ioport_write(proxy
, addr
, val
);
463 * Virtio-PCI is odd. Ioports are LE but config space is target native
468 virtio_config_writeb(vdev
, addr
, val
);
471 if (virtio_is_big_endian(vdev
)) {
474 virtio_config_writew(vdev
, addr
, val
);
477 if (virtio_is_big_endian(vdev
)) {
480 virtio_config_writel(vdev
, addr
, val
);
485 static const MemoryRegionOps virtio_pci_config_ops
= {
486 .read
= virtio_pci_config_read
,
487 .write
= virtio_pci_config_write
,
489 .min_access_size
= 1,
490 .max_access_size
= 4,
492 .endianness
= DEVICE_LITTLE_ENDIAN
,
495 static MemoryRegion
*virtio_address_space_lookup(VirtIOPCIProxy
*proxy
,
496 hwaddr
*off
, int len
)
499 VirtIOPCIRegion
*reg
;
501 for (i
= 0; i
< ARRAY_SIZE(proxy
->regs
); ++i
) {
502 reg
= &proxy
->regs
[i
];
503 if (*off
>= reg
->offset
&&
504 *off
+ len
<= reg
->offset
+ reg
->size
) {
513 /* Below are generic functions to do memcpy from/to an address space,
514 * without byteswaps, with input validation.
516 * As regular address_space_* APIs all do some kind of byteswap at least for
517 * some host/target combinations, we are forced to explicitly convert to a
518 * known-endianness integer value.
519 * It doesn't really matter which endian format to go through, so the code
520 * below selects the endian that causes the least amount of work on the given
523 * Note: host pointer must be aligned.
526 void virtio_address_space_write(VirtIOPCIProxy
*proxy
, hwaddr addr
,
527 const uint8_t *buf
, int len
)
532 /* address_space_* APIs assume an aligned address.
533 * As address is under guest control, handle illegal values.
537 mr
= virtio_address_space_lookup(proxy
, &addr
, len
);
542 /* Make sure caller aligned buf properly */
543 assert(!(((uintptr_t)buf
) & (len
- 1)));
547 val
= pci_get_byte(buf
);
550 val
= pci_get_word(buf
);
553 val
= pci_get_long(buf
);
556 /* As length is under guest control, handle illegal values. */
559 memory_region_dispatch_write(mr
, addr
, val
, size_memop(len
) | MO_LE
,
560 MEMTXATTRS_UNSPECIFIED
);
564 virtio_address_space_read(VirtIOPCIProxy
*proxy
, hwaddr addr
,
565 uint8_t *buf
, int len
)
570 /* address_space_* APIs assume an aligned address.
571 * As address is under guest control, handle illegal values.
575 mr
= virtio_address_space_lookup(proxy
, &addr
, len
);
580 /* Make sure caller aligned buf properly */
581 assert(!(((uintptr_t)buf
) & (len
- 1)));
583 memory_region_dispatch_read(mr
, addr
, &val
, size_memop(len
) | MO_LE
,
584 MEMTXATTRS_UNSPECIFIED
);
587 pci_set_byte(buf
, val
);
590 pci_set_word(buf
, val
);
593 pci_set_long(buf
, val
);
596 /* As length is under guest control, handle illegal values. */
601 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
602 uint32_t val
, int len
)
604 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
605 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
606 struct virtio_pci_cfg_cap
*cfg
;
608 pci_default_write_config(pci_dev
, address
, val
, len
);
610 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_FLR
) {
611 pcie_cap_flr_write_config(pci_dev
, address
, val
, len
);
614 if (range_covers_byte(address
, len
, PCI_COMMAND
)) {
615 if (!(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
616 virtio_set_disabled(vdev
, true);
617 virtio_pci_stop_ioeventfd(proxy
);
618 virtio_set_status(vdev
, vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
620 virtio_set_disabled(vdev
, false);
624 if (proxy
->config_cap
&&
625 ranges_overlap(address
, len
, proxy
->config_cap
+ offsetof(struct virtio_pci_cfg_cap
,
627 sizeof cfg
->pci_cfg_data
)) {
631 cfg
= (void *)(proxy
->pci_dev
.config
+ proxy
->config_cap
);
632 off
= le32_to_cpu(cfg
->cap
.offset
);
633 len
= le32_to_cpu(cfg
->cap
.length
);
635 if (len
== 1 || len
== 2 || len
== 4) {
636 assert(len
<= sizeof cfg
->pci_cfg_data
);
637 virtio_address_space_write(proxy
, off
, cfg
->pci_cfg_data
, len
);
642 static uint32_t virtio_read_config(PCIDevice
*pci_dev
,
643 uint32_t address
, int len
)
645 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
646 struct virtio_pci_cfg_cap
*cfg
;
648 if (proxy
->config_cap
&&
649 ranges_overlap(address
, len
, proxy
->config_cap
+ offsetof(struct virtio_pci_cfg_cap
,
651 sizeof cfg
->pci_cfg_data
)) {
655 cfg
= (void *)(proxy
->pci_dev
.config
+ proxy
->config_cap
);
656 off
= le32_to_cpu(cfg
->cap
.offset
);
657 len
= le32_to_cpu(cfg
->cap
.length
);
659 if (len
== 1 || len
== 2 || len
== 4) {
660 assert(len
<= sizeof cfg
->pci_cfg_data
);
661 virtio_address_space_read(proxy
, off
, cfg
->pci_cfg_data
, len
);
665 return pci_default_read_config(pci_dev
, address
, len
);
668 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy
*proxy
,
669 unsigned int queue_no
,
672 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
675 if (irqfd
->users
== 0) {
676 ret
= kvm_irqchip_add_msi_route(kvm_state
, vector
, &proxy
->pci_dev
);
686 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy
*proxy
,
689 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
690 if (--irqfd
->users
== 0) {
691 kvm_irqchip_release_virq(kvm_state
, irqfd
->virq
);
695 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy
*proxy
,
696 unsigned int queue_no
,
699 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
700 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
701 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
702 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
703 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, n
, NULL
, irqfd
->virq
);
706 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy
*proxy
,
707 unsigned int queue_no
,
710 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
711 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
712 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
713 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
716 ret
= kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state
, n
, irqfd
->virq
);
720 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy
*proxy
, int nvqs
)
722 PCIDevice
*dev
= &proxy
->pci_dev
;
723 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
724 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
728 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
729 if (!virtio_queue_get_num(vdev
, queue_no
)) {
732 vector
= virtio_queue_vector(vdev
, queue_no
);
733 if (vector
>= msix_nr_vectors_allocated(dev
)) {
736 ret
= kvm_virtio_pci_vq_vector_use(proxy
, queue_no
, vector
);
740 /* If guest supports masking, set up irqfd now.
741 * Otherwise, delay until unmasked in the frontend.
743 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
744 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
746 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
754 while (--queue_no
>= 0) {
755 vector
= virtio_queue_vector(vdev
, queue_no
);
756 if (vector
>= msix_nr_vectors_allocated(dev
)) {
759 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
760 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
762 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
767 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy
*proxy
, int nvqs
)
769 PCIDevice
*dev
= &proxy
->pci_dev
;
770 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
773 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
775 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
776 if (!virtio_queue_get_num(vdev
, queue_no
)) {
779 vector
= virtio_queue_vector(vdev
, queue_no
);
780 if (vector
>= msix_nr_vectors_allocated(dev
)) {
783 /* If guest supports masking, clean up irqfd now.
784 * Otherwise, it was cleaned when masked in the frontend.
786 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
787 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
789 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
793 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy
*proxy
,
794 unsigned int queue_no
,
798 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
799 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
800 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
801 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
805 if (proxy
->vector_irqfd
) {
806 irqfd
= &proxy
->vector_irqfd
[vector
];
807 if (irqfd
->msg
.data
!= msg
.data
|| irqfd
->msg
.address
!= msg
.address
) {
808 ret
= kvm_irqchip_update_msi_route(kvm_state
, irqfd
->virq
, msg
,
813 kvm_irqchip_commit_routes(kvm_state
);
817 /* If guest supports masking, irqfd is already setup, unmask it.
818 * Otherwise, set it up now.
820 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
821 k
->guest_notifier_mask(vdev
, queue_no
, false);
822 /* Test after unmasking to avoid losing events. */
823 if (k
->guest_notifier_pending
&&
824 k
->guest_notifier_pending(vdev
, queue_no
)) {
825 event_notifier_set(n
);
828 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
833 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy
*proxy
,
834 unsigned int queue_no
,
837 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
838 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
840 /* If guest supports masking, keep irqfd but mask it.
841 * Otherwise, clean it up now.
843 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
844 k
->guest_notifier_mask(vdev
, queue_no
, true);
846 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
850 static int virtio_pci_vector_unmask(PCIDevice
*dev
, unsigned vector
,
853 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
854 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
855 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
856 int ret
, index
, unmasked
= 0;
859 index
= virtio_get_queue_index(vq
);
860 if (!virtio_queue_get_num(vdev
, index
)) {
863 if (index
< proxy
->nvqs_with_notifiers
) {
864 ret
= virtio_pci_vq_vector_unmask(proxy
, index
, vector
, msg
);
870 vq
= virtio_vector_next_queue(vq
);
876 vq
= virtio_vector_first_queue(vdev
, vector
);
877 while (vq
&& unmasked
>= 0) {
878 index
= virtio_get_queue_index(vq
);
879 if (index
< proxy
->nvqs_with_notifiers
) {
880 virtio_pci_vq_vector_mask(proxy
, index
, vector
);
883 vq
= virtio_vector_next_queue(vq
);
888 static void virtio_pci_vector_mask(PCIDevice
*dev
, unsigned vector
)
890 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
891 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
892 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
896 index
= virtio_get_queue_index(vq
);
897 if (!virtio_queue_get_num(vdev
, index
)) {
900 if (index
< proxy
->nvqs_with_notifiers
) {
901 virtio_pci_vq_vector_mask(proxy
, index
, vector
);
903 vq
= virtio_vector_next_queue(vq
);
907 static void virtio_pci_vector_poll(PCIDevice
*dev
,
908 unsigned int vector_start
,
909 unsigned int vector_end
)
911 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
912 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
913 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
916 EventNotifier
*notifier
;
919 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
920 if (!virtio_queue_get_num(vdev
, queue_no
)) {
923 vector
= virtio_queue_vector(vdev
, queue_no
);
924 if (vector
< vector_start
|| vector
>= vector_end
||
925 !msix_is_masked(dev
, vector
)) {
928 vq
= virtio_get_queue(vdev
, queue_no
);
929 notifier
= virtio_queue_get_guest_notifier(vq
);
930 if (k
->guest_notifier_pending
) {
931 if (k
->guest_notifier_pending(vdev
, queue_no
)) {
932 msix_set_pending(dev
, vector
);
934 } else if (event_notifier_test_and_clear(notifier
)) {
935 msix_set_pending(dev
, vector
);
940 static int virtio_pci_set_guest_notifier(DeviceState
*d
, int n
, bool assign
,
943 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
944 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
945 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
946 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
947 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
950 int r
= event_notifier_init(notifier
, 0);
954 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
956 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
957 event_notifier_cleanup(notifier
);
960 if (!msix_enabled(&proxy
->pci_dev
) &&
961 vdev
->use_guest_notifier_mask
&&
962 vdc
->guest_notifier_mask
) {
963 vdc
->guest_notifier_mask(vdev
, n
, !assign
);
969 static bool virtio_pci_query_guest_notifiers(DeviceState
*d
)
971 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
972 return msix_enabled(&proxy
->pci_dev
);
975 static int virtio_pci_set_guest_notifiers(DeviceState
*d
, int nvqs
, bool assign
)
977 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
978 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
979 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
981 bool with_irqfd
= msix_enabled(&proxy
->pci_dev
) &&
982 kvm_msi_via_irqfd_enabled();
984 nvqs
= MIN(nvqs
, VIRTIO_QUEUE_MAX
);
986 /* When deassigning, pass a consistent nvqs value
987 * to avoid leaking notifiers.
989 assert(assign
|| nvqs
== proxy
->nvqs_with_notifiers
);
991 proxy
->nvqs_with_notifiers
= nvqs
;
993 /* Must unset vector notifier while guest notifier is still assigned */
994 if ((proxy
->vector_irqfd
|| k
->guest_notifier_mask
) && !assign
) {
995 msix_unset_vector_notifiers(&proxy
->pci_dev
);
996 if (proxy
->vector_irqfd
) {
997 kvm_virtio_pci_vector_release(proxy
, nvqs
);
998 g_free(proxy
->vector_irqfd
);
999 proxy
->vector_irqfd
= NULL
;
1003 for (n
= 0; n
< nvqs
; n
++) {
1004 if (!virtio_queue_get_num(vdev
, n
)) {
1008 r
= virtio_pci_set_guest_notifier(d
, n
, assign
, with_irqfd
);
1014 /* Must set vector notifier after guest notifier has been assigned */
1015 if ((with_irqfd
|| k
->guest_notifier_mask
) && assign
) {
1017 proxy
->vector_irqfd
=
1018 g_malloc0(sizeof(*proxy
->vector_irqfd
) *
1019 msix_nr_vectors_allocated(&proxy
->pci_dev
));
1020 r
= kvm_virtio_pci_vector_use(proxy
, nvqs
);
1025 r
= msix_set_vector_notifiers(&proxy
->pci_dev
,
1026 virtio_pci_vector_unmask
,
1027 virtio_pci_vector_mask
,
1028 virtio_pci_vector_poll
);
1030 goto notifiers_error
;
1039 kvm_virtio_pci_vector_release(proxy
, nvqs
);
1043 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
1046 virtio_pci_set_guest_notifier(d
, n
, !assign
, with_irqfd
);
1051 static int virtio_pci_set_host_notifier_mr(DeviceState
*d
, int n
,
1052 MemoryRegion
*mr
, bool assign
)
1054 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1057 if (n
>= VIRTIO_QUEUE_MAX
|| !virtio_pci_modern(proxy
) ||
1058 virtio_pci_queue_mem_mult(proxy
) != memory_region_size(mr
)) {
1063 offset
= virtio_pci_queue_mem_mult(proxy
) * n
;
1064 memory_region_add_subregion_overlap(&proxy
->notify
.mr
, offset
, mr
, 1);
1066 memory_region_del_subregion(&proxy
->notify
.mr
, mr
);
1072 static void virtio_pci_vmstate_change(DeviceState
*d
, bool running
)
1074 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1075 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1078 /* Old QEMU versions did not set bus master enable on status write.
1079 * Detect DRIVER set and enable it.
1081 if ((proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION
) &&
1082 (vdev
->status
& VIRTIO_CONFIG_S_DRIVER
) &&
1083 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
1084 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
1085 proxy
->pci_dev
.config
[PCI_COMMAND
] |
1086 PCI_COMMAND_MASTER
, 1);
1088 virtio_pci_start_ioeventfd(proxy
);
1090 virtio_pci_stop_ioeventfd(proxy
);
1095 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
1098 static int virtio_pci_query_nvectors(DeviceState
*d
)
1100 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1102 return proxy
->nvectors
;
1105 static AddressSpace
*virtio_pci_get_dma_as(DeviceState
*d
)
1107 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1108 PCIDevice
*dev
= &proxy
->pci_dev
;
1110 return pci_get_address_space(dev
);
1113 static bool virtio_pci_queue_enabled(DeviceState
*d
, int n
)
1115 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1116 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1118 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
1119 return proxy
->vqs
[n
].enabled
;
1122 return virtio_queue_enabled_legacy(vdev
, n
);
1125 static int virtio_pci_add_mem_cap(VirtIOPCIProxy
*proxy
,
1126 struct virtio_pci_cap
*cap
)
1128 PCIDevice
*dev
= &proxy
->pci_dev
;
1131 offset
= pci_add_capability(dev
, PCI_CAP_ID_VNDR
, 0,
1132 cap
->cap_len
, &error_abort
);
1134 assert(cap
->cap_len
>= sizeof *cap
);
1135 memcpy(dev
->config
+ offset
+ PCI_CAP_FLAGS
, &cap
->cap_len
,
1136 cap
->cap_len
- PCI_CAP_FLAGS
);
1141 static uint64_t virtio_pci_common_read(void *opaque
, hwaddr addr
,
1144 VirtIOPCIProxy
*proxy
= opaque
;
1145 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1150 case VIRTIO_PCI_COMMON_DFSELECT
:
1151 val
= proxy
->dfselect
;
1153 case VIRTIO_PCI_COMMON_DF
:
1154 if (proxy
->dfselect
<= 1) {
1155 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1157 val
= (vdev
->host_features
& ~vdc
->legacy_features
) >>
1158 (32 * proxy
->dfselect
);
1161 case VIRTIO_PCI_COMMON_GFSELECT
:
1162 val
= proxy
->gfselect
;
1164 case VIRTIO_PCI_COMMON_GF
:
1165 if (proxy
->gfselect
< ARRAY_SIZE(proxy
->guest_features
)) {
1166 val
= proxy
->guest_features
[proxy
->gfselect
];
1169 case VIRTIO_PCI_COMMON_MSIX
:
1170 val
= vdev
->config_vector
;
1172 case VIRTIO_PCI_COMMON_NUMQ
:
1173 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; ++i
) {
1174 if (virtio_queue_get_num(vdev
, i
)) {
1179 case VIRTIO_PCI_COMMON_STATUS
:
1182 case VIRTIO_PCI_COMMON_CFGGENERATION
:
1183 val
= vdev
->generation
;
1185 case VIRTIO_PCI_COMMON_Q_SELECT
:
1186 val
= vdev
->queue_sel
;
1188 case VIRTIO_PCI_COMMON_Q_SIZE
:
1189 val
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
1191 case VIRTIO_PCI_COMMON_Q_MSIX
:
1192 val
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
1194 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1195 val
= proxy
->vqs
[vdev
->queue_sel
].enabled
;
1197 case VIRTIO_PCI_COMMON_Q_NOFF
:
1198 /* Simply map queues in order */
1199 val
= vdev
->queue_sel
;
1201 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1202 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[0];
1204 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1205 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[1];
1207 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1208 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[0];
1210 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1211 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[1];
1213 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1214 val
= proxy
->vqs
[vdev
->queue_sel
].used
[0];
1216 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1217 val
= proxy
->vqs
[vdev
->queue_sel
].used
[1];
1226 static void virtio_pci_common_write(void *opaque
, hwaddr addr
,
1227 uint64_t val
, unsigned size
)
1229 VirtIOPCIProxy
*proxy
= opaque
;
1230 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1233 case VIRTIO_PCI_COMMON_DFSELECT
:
1234 proxy
->dfselect
= val
;
1236 case VIRTIO_PCI_COMMON_GFSELECT
:
1237 proxy
->gfselect
= val
;
1239 case VIRTIO_PCI_COMMON_GF
:
1240 if (proxy
->gfselect
< ARRAY_SIZE(proxy
->guest_features
)) {
1241 proxy
->guest_features
[proxy
->gfselect
] = val
;
1242 virtio_set_features(vdev
,
1243 (((uint64_t)proxy
->guest_features
[1]) << 32) |
1244 proxy
->guest_features
[0]);
1247 case VIRTIO_PCI_COMMON_MSIX
:
1248 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
1249 /* Make it possible for guest to discover an error took place. */
1250 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0) {
1251 val
= VIRTIO_NO_VECTOR
;
1253 vdev
->config_vector
= val
;
1255 case VIRTIO_PCI_COMMON_STATUS
:
1256 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
1257 virtio_pci_stop_ioeventfd(proxy
);
1260 virtio_set_status(vdev
, val
& 0xFF);
1262 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
1263 virtio_pci_start_ioeventfd(proxy
);
1266 if (vdev
->status
== 0) {
1267 virtio_pci_reset(DEVICE(proxy
));
1271 case VIRTIO_PCI_COMMON_Q_SELECT
:
1272 if (val
< VIRTIO_QUEUE_MAX
) {
1273 vdev
->queue_sel
= val
;
1276 case VIRTIO_PCI_COMMON_Q_SIZE
:
1277 proxy
->vqs
[vdev
->queue_sel
].num
= val
;
1278 virtio_queue_set_num(vdev
, vdev
->queue_sel
,
1279 proxy
->vqs
[vdev
->queue_sel
].num
);
1281 case VIRTIO_PCI_COMMON_Q_MSIX
:
1282 msix_vector_unuse(&proxy
->pci_dev
,
1283 virtio_queue_vector(vdev
, vdev
->queue_sel
));
1284 /* Make it possible for guest to discover an error took place. */
1285 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0) {
1286 val
= VIRTIO_NO_VECTOR
;
1288 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
1290 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1292 virtio_queue_set_num(vdev
, vdev
->queue_sel
,
1293 proxy
->vqs
[vdev
->queue_sel
].num
);
1294 virtio_queue_set_rings(vdev
, vdev
->queue_sel
,
1295 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].desc
[1]) << 32 |
1296 proxy
->vqs
[vdev
->queue_sel
].desc
[0],
1297 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].avail
[1]) << 32 |
1298 proxy
->vqs
[vdev
->queue_sel
].avail
[0],
1299 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].used
[1]) << 32 |
1300 proxy
->vqs
[vdev
->queue_sel
].used
[0]);
1301 proxy
->vqs
[vdev
->queue_sel
].enabled
= 1;
1303 virtio_error(vdev
, "wrong value for queue_enable %"PRIx64
, val
);
1306 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1307 proxy
->vqs
[vdev
->queue_sel
].desc
[0] = val
;
1309 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1310 proxy
->vqs
[vdev
->queue_sel
].desc
[1] = val
;
1312 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1313 proxy
->vqs
[vdev
->queue_sel
].avail
[0] = val
;
1315 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1316 proxy
->vqs
[vdev
->queue_sel
].avail
[1] = val
;
1318 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1319 proxy
->vqs
[vdev
->queue_sel
].used
[0] = val
;
1321 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1322 proxy
->vqs
[vdev
->queue_sel
].used
[1] = val
;
1330 static uint64_t virtio_pci_notify_read(void *opaque
, hwaddr addr
,
1336 static void virtio_pci_notify_write(void *opaque
, hwaddr addr
,
1337 uint64_t val
, unsigned size
)
1339 VirtIOPCIProxy
*proxy
= opaque
;
1340 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1342 unsigned queue
= addr
/ virtio_pci_queue_mem_mult(proxy
);
1344 if (vdev
!= NULL
&& queue
< VIRTIO_QUEUE_MAX
) {
1345 virtio_queue_notify(vdev
, queue
);
1349 static void virtio_pci_notify_write_pio(void *opaque
, hwaddr addr
,
1350 uint64_t val
, unsigned size
)
1352 VirtIOPCIProxy
*proxy
= opaque
;
1353 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1355 unsigned queue
= val
;
1357 if (vdev
!= NULL
&& queue
< VIRTIO_QUEUE_MAX
) {
1358 virtio_queue_notify(vdev
, queue
);
1362 static uint64_t virtio_pci_isr_read(void *opaque
, hwaddr addr
,
1365 VirtIOPCIProxy
*proxy
= opaque
;
1366 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1373 val
= qatomic_xchg(&vdev
->isr
, 0);
1374 pci_irq_deassert(&proxy
->pci_dev
);
1378 static void virtio_pci_isr_write(void *opaque
, hwaddr addr
,
1379 uint64_t val
, unsigned size
)
1383 static uint64_t virtio_pci_device_read(void *opaque
, hwaddr addr
,
1386 VirtIOPCIProxy
*proxy
= opaque
;
1387 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1396 val
= virtio_config_modern_readb(vdev
, addr
);
1399 val
= virtio_config_modern_readw(vdev
, addr
);
1402 val
= virtio_config_modern_readl(vdev
, addr
);
1411 static void virtio_pci_device_write(void *opaque
, hwaddr addr
,
1412 uint64_t val
, unsigned size
)
1414 VirtIOPCIProxy
*proxy
= opaque
;
1415 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1423 virtio_config_modern_writeb(vdev
, addr
, val
);
1426 virtio_config_modern_writew(vdev
, addr
, val
);
1429 virtio_config_modern_writel(vdev
, addr
, val
);
1434 static void virtio_pci_modern_regions_init(VirtIOPCIProxy
*proxy
,
1435 const char *vdev_name
)
1437 static const MemoryRegionOps common_ops
= {
1438 .read
= virtio_pci_common_read
,
1439 .write
= virtio_pci_common_write
,
1441 .min_access_size
= 1,
1442 .max_access_size
= 4,
1444 .endianness
= DEVICE_LITTLE_ENDIAN
,
1446 static const MemoryRegionOps isr_ops
= {
1447 .read
= virtio_pci_isr_read
,
1448 .write
= virtio_pci_isr_write
,
1450 .min_access_size
= 1,
1451 .max_access_size
= 4,
1453 .endianness
= DEVICE_LITTLE_ENDIAN
,
1455 static const MemoryRegionOps device_ops
= {
1456 .read
= virtio_pci_device_read
,
1457 .write
= virtio_pci_device_write
,
1459 .min_access_size
= 1,
1460 .max_access_size
= 4,
1462 .endianness
= DEVICE_LITTLE_ENDIAN
,
1464 static const MemoryRegionOps notify_ops
= {
1465 .read
= virtio_pci_notify_read
,
1466 .write
= virtio_pci_notify_write
,
1468 .min_access_size
= 1,
1469 .max_access_size
= 4,
1471 .endianness
= DEVICE_LITTLE_ENDIAN
,
1473 static const MemoryRegionOps notify_pio_ops
= {
1474 .read
= virtio_pci_notify_read
,
1475 .write
= virtio_pci_notify_write_pio
,
1477 .min_access_size
= 1,
1478 .max_access_size
= 4,
1480 .endianness
= DEVICE_LITTLE_ENDIAN
,
1482 g_autoptr(GString
) name
= g_string_new(NULL
);
1484 g_string_printf(name
, "virtio-pci-common-%s", vdev_name
);
1485 memory_region_init_io(&proxy
->common
.mr
, OBJECT(proxy
),
1489 proxy
->common
.size
);
1491 g_string_printf(name
, "virtio-pci-isr-%s", vdev_name
);
1492 memory_region_init_io(&proxy
->isr
.mr
, OBJECT(proxy
),
1498 g_string_printf(name
, "virtio-pci-device-%s", vdev_name
);
1499 memory_region_init_io(&proxy
->device
.mr
, OBJECT(proxy
),
1503 proxy
->device
.size
);
1505 g_string_printf(name
, "virtio-pci-notify-%s", vdev_name
);
1506 memory_region_init_io(&proxy
->notify
.mr
, OBJECT(proxy
),
1510 proxy
->notify
.size
);
1512 g_string_printf(name
, "virtio-pci-notify-pio-%s", vdev_name
);
1513 memory_region_init_io(&proxy
->notify_pio
.mr
, OBJECT(proxy
),
1517 proxy
->notify_pio
.size
);
1520 static void virtio_pci_modern_region_map(VirtIOPCIProxy
*proxy
,
1521 VirtIOPCIRegion
*region
,
1522 struct virtio_pci_cap
*cap
,
1526 memory_region_add_subregion(mr
, region
->offset
, ®ion
->mr
);
1528 cap
->cfg_type
= region
->type
;
1530 cap
->offset
= cpu_to_le32(region
->offset
);
1531 cap
->length
= cpu_to_le32(region
->size
);
1532 virtio_pci_add_mem_cap(proxy
, cap
);
1536 static void virtio_pci_modern_mem_region_map(VirtIOPCIProxy
*proxy
,
1537 VirtIOPCIRegion
*region
,
1538 struct virtio_pci_cap
*cap
)
1540 virtio_pci_modern_region_map(proxy
, region
, cap
,
1541 &proxy
->modern_bar
, proxy
->modern_mem_bar_idx
);
1544 static void virtio_pci_modern_io_region_map(VirtIOPCIProxy
*proxy
,
1545 VirtIOPCIRegion
*region
,
1546 struct virtio_pci_cap
*cap
)
1548 virtio_pci_modern_region_map(proxy
, region
, cap
,
1549 &proxy
->io_bar
, proxy
->modern_io_bar_idx
);
1552 static void virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy
*proxy
,
1553 VirtIOPCIRegion
*region
)
1555 memory_region_del_subregion(&proxy
->modern_bar
,
1559 static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy
*proxy
,
1560 VirtIOPCIRegion
*region
)
1562 memory_region_del_subregion(&proxy
->io_bar
,
1566 static void virtio_pci_pre_plugged(DeviceState
*d
, Error
**errp
)
1568 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1569 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1571 if (virtio_pci_modern(proxy
)) {
1572 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_VERSION_1
);
1575 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_BAD_FEATURE
);
1578 /* This is called by virtio-bus just after the device is plugged. */
1579 static void virtio_pci_device_plugged(DeviceState
*d
, Error
**errp
)
1581 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1582 VirtioBusState
*bus
= &proxy
->bus
;
1583 bool legacy
= virtio_pci_legacy(proxy
);
1585 bool modern_pio
= proxy
->flags
& VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY
;
1588 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1591 * Virtio capabilities present without
1592 * VIRTIO_F_VERSION_1 confuses guests
1594 if (!proxy
->ignore_backend_features
&&
1595 !virtio_has_feature(vdev
->host_features
, VIRTIO_F_VERSION_1
)) {
1596 virtio_pci_disable_modern(proxy
);
1599 error_setg(errp
, "Device doesn't support modern mode, and legacy"
1600 " mode is disabled");
1601 error_append_hint(errp
, "Set disable-legacy to off\n");
1607 modern
= virtio_pci_modern(proxy
);
1609 config
= proxy
->pci_dev
.config
;
1610 if (proxy
->class_code
) {
1611 pci_config_set_class(config
, proxy
->class_code
);
1615 if (!virtio_legacy_allowed(vdev
)) {
1617 * To avoid migration issues, we allow legacy mode when legacy
1618 * check is disabled in the old machine types (< 5.1).
1620 if (virtio_legacy_check_disabled(vdev
)) {
1621 warn_report("device is modern-only, but for backward "
1622 "compatibility legacy is allowed");
1625 "device is modern-only, use disable-legacy=on");
1629 if (virtio_host_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
)) {
1630 error_setg(errp
, "VIRTIO_F_IOMMU_PLATFORM was supported by"
1631 " neither legacy nor transitional device");
1635 * Legacy and transitional devices use specific subsystem IDs.
1636 * Note that the subsystem vendor ID (config + PCI_SUBSYSTEM_VENDOR_ID)
1637 * is set to PCI_SUBVENDOR_ID_REDHAT_QUMRANET by default.
1639 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, virtio_bus_get_vdev_id(bus
));
1641 /* pure virtio-1.0 */
1642 pci_set_word(config
+ PCI_VENDOR_ID
,
1643 PCI_VENDOR_ID_REDHAT_QUMRANET
);
1644 pci_set_word(config
+ PCI_DEVICE_ID
,
1645 0x1040 + virtio_bus_get_vdev_id(bus
));
1646 pci_config_set_revision(config
, 1);
1648 config
[PCI_INTERRUPT_PIN
] = 1;
1652 struct virtio_pci_cap cap
= {
1653 .cap_len
= sizeof cap
,
1655 struct virtio_pci_notify_cap notify
= {
1656 .cap
.cap_len
= sizeof notify
,
1657 .notify_off_multiplier
=
1658 cpu_to_le32(virtio_pci_queue_mem_mult(proxy
)),
1660 struct virtio_pci_cfg_cap cfg
= {
1661 .cap
.cap_len
= sizeof cfg
,
1662 .cap
.cfg_type
= VIRTIO_PCI_CAP_PCI_CFG
,
1664 struct virtio_pci_notify_cap notify_pio
= {
1665 .cap
.cap_len
= sizeof notify
,
1666 .notify_off_multiplier
= cpu_to_le32(0x0),
1669 struct virtio_pci_cfg_cap
*cfg_mask
;
1671 virtio_pci_modern_regions_init(proxy
, vdev
->name
);
1673 virtio_pci_modern_mem_region_map(proxy
, &proxy
->common
, &cap
);
1674 virtio_pci_modern_mem_region_map(proxy
, &proxy
->isr
, &cap
);
1675 virtio_pci_modern_mem_region_map(proxy
, &proxy
->device
, &cap
);
1676 virtio_pci_modern_mem_region_map(proxy
, &proxy
->notify
, ¬ify
.cap
);
1679 memory_region_init(&proxy
->io_bar
, OBJECT(proxy
),
1680 "virtio-pci-io", 0x4);
1682 pci_register_bar(&proxy
->pci_dev
, proxy
->modern_io_bar_idx
,
1683 PCI_BASE_ADDRESS_SPACE_IO
, &proxy
->io_bar
);
1685 virtio_pci_modern_io_region_map(proxy
, &proxy
->notify_pio
,
1689 pci_register_bar(&proxy
->pci_dev
, proxy
->modern_mem_bar_idx
,
1690 PCI_BASE_ADDRESS_SPACE_MEMORY
|
1691 PCI_BASE_ADDRESS_MEM_PREFETCH
|
1692 PCI_BASE_ADDRESS_MEM_TYPE_64
,
1693 &proxy
->modern_bar
);
1695 proxy
->config_cap
= virtio_pci_add_mem_cap(proxy
, &cfg
.cap
);
1696 cfg_mask
= (void *)(proxy
->pci_dev
.wmask
+ proxy
->config_cap
);
1697 pci_set_byte(&cfg_mask
->cap
.bar
, ~0x0);
1698 pci_set_long((uint8_t *)&cfg_mask
->cap
.offset
, ~0x0);
1699 pci_set_long((uint8_t *)&cfg_mask
->cap
.length
, ~0x0);
1700 pci_set_long(cfg_mask
->pci_cfg_data
, ~0x0);
1703 if (proxy
->nvectors
) {
1704 int err
= msix_init_exclusive_bar(&proxy
->pci_dev
, proxy
->nvectors
,
1705 proxy
->msix_bar_idx
, NULL
);
1707 /* Notice when a system that supports MSIx can't initialize it */
1708 if (err
!= -ENOTSUP
) {
1709 warn_report("unable to init msix vectors to %" PRIu32
,
1712 proxy
->nvectors
= 0;
1716 proxy
->pci_dev
.config_write
= virtio_write_config
;
1717 proxy
->pci_dev
.config_read
= virtio_read_config
;
1720 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
)
1721 + virtio_bus_get_vdev_config_len(bus
);
1722 size
= pow2ceil(size
);
1724 memory_region_init_io(&proxy
->bar
, OBJECT(proxy
),
1725 &virtio_pci_config_ops
,
1726 proxy
, "virtio-pci", size
);
1728 pci_register_bar(&proxy
->pci_dev
, proxy
->legacy_io_bar_idx
,
1729 PCI_BASE_ADDRESS_SPACE_IO
, &proxy
->bar
);
1733 static void virtio_pci_device_unplugged(DeviceState
*d
)
1735 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1736 bool modern
= virtio_pci_modern(proxy
);
1737 bool modern_pio
= proxy
->flags
& VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY
;
1739 virtio_pci_stop_ioeventfd(proxy
);
1742 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->common
);
1743 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->isr
);
1744 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->device
);
1745 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->notify
);
1747 virtio_pci_modern_io_region_unmap(proxy
, &proxy
->notify_pio
);
1752 static void virtio_pci_realize(PCIDevice
*pci_dev
, Error
**errp
)
1754 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
1755 VirtioPCIClass
*k
= VIRTIO_PCI_GET_CLASS(pci_dev
);
1756 bool pcie_port
= pci_bus_is_express(pci_get_bus(pci_dev
)) &&
1757 !pci_bus_is_root(pci_get_bus(pci_dev
));
1759 if (kvm_enabled() && !kvm_has_many_ioeventfds()) {
1760 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
1764 * virtio pci bar layout used by default.
1765 * subclasses can re-arrange things if needed.
1767 * region 0 -- virtio legacy io bar
1768 * region 1 -- msi-x bar
1769 * region 2 -- virtio modern io bar (off by default)
1770 * region 4+5 -- virtio modern memory (64bit) bar
1773 proxy
->legacy_io_bar_idx
= 0;
1774 proxy
->msix_bar_idx
= 1;
1775 proxy
->modern_io_bar_idx
= 2;
1776 proxy
->modern_mem_bar_idx
= 4;
1778 proxy
->common
.offset
= 0x0;
1779 proxy
->common
.size
= 0x1000;
1780 proxy
->common
.type
= VIRTIO_PCI_CAP_COMMON_CFG
;
1782 proxy
->isr
.offset
= 0x1000;
1783 proxy
->isr
.size
= 0x1000;
1784 proxy
->isr
.type
= VIRTIO_PCI_CAP_ISR_CFG
;
1786 proxy
->device
.offset
= 0x2000;
1787 proxy
->device
.size
= 0x1000;
1788 proxy
->device
.type
= VIRTIO_PCI_CAP_DEVICE_CFG
;
1790 proxy
->notify
.offset
= 0x3000;
1791 proxy
->notify
.size
= virtio_pci_queue_mem_mult(proxy
) * VIRTIO_QUEUE_MAX
;
1792 proxy
->notify
.type
= VIRTIO_PCI_CAP_NOTIFY_CFG
;
1794 proxy
->notify_pio
.offset
= 0x0;
1795 proxy
->notify_pio
.size
= 0x4;
1796 proxy
->notify_pio
.type
= VIRTIO_PCI_CAP_NOTIFY_CFG
;
1798 /* subclasses can enforce modern, so do this unconditionally */
1799 memory_region_init(&proxy
->modern_bar
, OBJECT(proxy
), "virtio-pci",
1800 /* PCI BAR regions must be powers of 2 */
1801 pow2ceil(proxy
->notify
.offset
+ proxy
->notify
.size
));
1803 if (proxy
->disable_legacy
== ON_OFF_AUTO_AUTO
) {
1804 proxy
->disable_legacy
= pcie_port
? ON_OFF_AUTO_ON
: ON_OFF_AUTO_OFF
;
1807 if (!virtio_pci_modern(proxy
) && !virtio_pci_legacy(proxy
)) {
1808 error_setg(errp
, "device cannot work as neither modern nor legacy mode"
1810 error_append_hint(errp
, "Set either disable-modern or disable-legacy"
1815 if (pcie_port
&& pci_is_express(pci_dev
)) {
1817 uint16_t last_pcie_cap_offset
= PCI_CONFIG_SPACE_SIZE
;
1819 pos
= pcie_endpoint_cap_init(pci_dev
, 0);
1822 pos
= pci_add_capability(pci_dev
, PCI_CAP_ID_PM
, 0,
1823 PCI_PM_SIZEOF
, errp
);
1828 pci_dev
->exp
.pm_cap
= pos
;
1831 * Indicates that this function complies with revision 1.2 of the
1832 * PCI Power Management Interface Specification.
1834 pci_set_word(pci_dev
->config
+ pos
+ PCI_PM_PMC
, 0x3);
1836 if (proxy
->flags
& VIRTIO_PCI_FLAG_AER
) {
1837 pcie_aer_init(pci_dev
, PCI_ERR_VER
, last_pcie_cap_offset
,
1838 PCI_ERR_SIZEOF
, NULL
);
1839 last_pcie_cap_offset
+= PCI_ERR_SIZEOF
;
1842 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_DEVERR
) {
1843 /* Init error enabling flags */
1844 pcie_cap_deverr_init(pci_dev
);
1847 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_LNKCTL
) {
1848 /* Init Link Control Register */
1849 pcie_cap_lnkctl_init(pci_dev
);
1852 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_PM
) {
1853 /* Init Power Management Control Register */
1854 pci_set_word(pci_dev
->wmask
+ pos
+ PCI_PM_CTRL
,
1855 PCI_PM_CTRL_STATE_MASK
);
1858 if (proxy
->flags
& VIRTIO_PCI_FLAG_ATS
) {
1859 pcie_ats_init(pci_dev
, last_pcie_cap_offset
,
1860 proxy
->flags
& VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED
);
1861 last_pcie_cap_offset
+= PCI_EXT_CAP_ATS_SIZEOF
;
1864 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_FLR
) {
1865 /* Set Function Level Reset capability bit */
1866 pcie_cap_flr_init(pci_dev
);
1870 * make future invocations of pci_is_express() return false
1871 * and pci_config_size() return PCI_CONFIG_SPACE_SIZE.
1873 pci_dev
->cap_present
&= ~QEMU_PCI_CAP_EXPRESS
;
1876 virtio_pci_bus_new(&proxy
->bus
, sizeof(proxy
->bus
), proxy
);
1878 k
->realize(proxy
, errp
);
1882 static void virtio_pci_exit(PCIDevice
*pci_dev
)
1884 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
1885 bool pcie_port
= pci_bus_is_express(pci_get_bus(pci_dev
)) &&
1886 !pci_bus_is_root(pci_get_bus(pci_dev
));
1888 msix_uninit_exclusive_bar(pci_dev
);
1889 if (proxy
->flags
& VIRTIO_PCI_FLAG_AER
&& pcie_port
&&
1890 pci_is_express(pci_dev
)) {
1891 pcie_aer_exit(pci_dev
);
1895 static void virtio_pci_reset(DeviceState
*qdev
)
1897 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1898 VirtioBusState
*bus
= VIRTIO_BUS(&proxy
->bus
);
1899 PCIDevice
*dev
= PCI_DEVICE(qdev
);
1902 virtio_pci_stop_ioeventfd(proxy
);
1903 virtio_bus_reset(bus
);
1904 msix_unuse_all_vectors(&proxy
->pci_dev
);
1906 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1907 proxy
->vqs
[i
].enabled
= 0;
1908 proxy
->vqs
[i
].num
= 0;
1909 proxy
->vqs
[i
].desc
[0] = proxy
->vqs
[i
].desc
[1] = 0;
1910 proxy
->vqs
[i
].avail
[0] = proxy
->vqs
[i
].avail
[1] = 0;
1911 proxy
->vqs
[i
].used
[0] = proxy
->vqs
[i
].used
[1] = 0;
1914 if (pci_is_express(dev
)) {
1915 pcie_cap_deverr_reset(dev
);
1916 pcie_cap_lnkctl_reset(dev
);
1918 pci_set_word(dev
->config
+ dev
->exp
.pm_cap
+ PCI_PM_CTRL
, 0);
1922 static Property virtio_pci_properties
[] = {
1923 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy
, flags
,
1924 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT
, false),
1925 DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy
, flags
,
1926 VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT
, true),
1927 DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy
, flags
,
1928 VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT
, false),
1929 DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy
, flags
,
1930 VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT
, false),
1931 DEFINE_PROP_BIT("page-per-vq", VirtIOPCIProxy
, flags
,
1932 VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT
, false),
1933 DEFINE_PROP_BOOL("x-ignore-backend-features", VirtIOPCIProxy
,
1934 ignore_backend_features
, false),
1935 DEFINE_PROP_BIT("ats", VirtIOPCIProxy
, flags
,
1936 VIRTIO_PCI_FLAG_ATS_BIT
, false),
1937 DEFINE_PROP_BIT("x-ats-page-aligned", VirtIOPCIProxy
, flags
,
1938 VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED_BIT
, true),
1939 DEFINE_PROP_BIT("x-pcie-deverr-init", VirtIOPCIProxy
, flags
,
1940 VIRTIO_PCI_FLAG_INIT_DEVERR_BIT
, true),
1941 DEFINE_PROP_BIT("x-pcie-lnkctl-init", VirtIOPCIProxy
, flags
,
1942 VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT
, true),
1943 DEFINE_PROP_BIT("x-pcie-pm-init", VirtIOPCIProxy
, flags
,
1944 VIRTIO_PCI_FLAG_INIT_PM_BIT
, true),
1945 DEFINE_PROP_BIT("x-pcie-flr-init", VirtIOPCIProxy
, flags
,
1946 VIRTIO_PCI_FLAG_INIT_FLR_BIT
, true),
1947 DEFINE_PROP_BIT("aer", VirtIOPCIProxy
, flags
,
1948 VIRTIO_PCI_FLAG_AER_BIT
, false),
1949 DEFINE_PROP_END_OF_LIST(),
1952 static void virtio_pci_dc_realize(DeviceState
*qdev
, Error
**errp
)
1954 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_GET_CLASS(qdev
);
1955 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1956 PCIDevice
*pci_dev
= &proxy
->pci_dev
;
1958 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_PCIE
) &&
1959 virtio_pci_modern(proxy
)) {
1960 pci_dev
->cap_present
|= QEMU_PCI_CAP_EXPRESS
;
1963 vpciklass
->parent_dc_realize(qdev
, errp
);
1966 static void virtio_pci_class_init(ObjectClass
*klass
, void *data
)
1968 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1969 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1970 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_CLASS(klass
);
1972 device_class_set_props(dc
, virtio_pci_properties
);
1973 k
->realize
= virtio_pci_realize
;
1974 k
->exit
= virtio_pci_exit
;
1975 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1976 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1977 k
->class_id
= PCI_CLASS_OTHERS
;
1978 device_class_set_parent_realize(dc
, virtio_pci_dc_realize
,
1979 &vpciklass
->parent_dc_realize
);
1980 dc
->reset
= virtio_pci_reset
;
1983 static const TypeInfo virtio_pci_info
= {
1984 .name
= TYPE_VIRTIO_PCI
,
1985 .parent
= TYPE_PCI_DEVICE
,
1986 .instance_size
= sizeof(VirtIOPCIProxy
),
1987 .class_init
= virtio_pci_class_init
,
1988 .class_size
= sizeof(VirtioPCIClass
),
1992 static Property virtio_pci_generic_properties
[] = {
1993 DEFINE_PROP_ON_OFF_AUTO("disable-legacy", VirtIOPCIProxy
, disable_legacy
,
1995 DEFINE_PROP_BOOL("disable-modern", VirtIOPCIProxy
, disable_modern
, false),
1996 DEFINE_PROP_END_OF_LIST(),
1999 static void virtio_pci_base_class_init(ObjectClass
*klass
, void *data
)
2001 const VirtioPCIDeviceTypeInfo
*t
= data
;
2002 if (t
->class_init
) {
2003 t
->class_init(klass
, NULL
);
2007 static void virtio_pci_generic_class_init(ObjectClass
*klass
, void *data
)
2009 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2011 device_class_set_props(dc
, virtio_pci_generic_properties
);
2014 static void virtio_pci_transitional_instance_init(Object
*obj
)
2016 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(obj
);
2018 proxy
->disable_legacy
= ON_OFF_AUTO_OFF
;
2019 proxy
->disable_modern
= false;
2022 static void virtio_pci_non_transitional_instance_init(Object
*obj
)
2024 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(obj
);
2026 proxy
->disable_legacy
= ON_OFF_AUTO_ON
;
2027 proxy
->disable_modern
= false;
2030 void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo
*t
)
2032 char *base_name
= NULL
;
2033 TypeInfo base_type_info
= {
2034 .name
= t
->base_name
,
2035 .parent
= t
->parent
? t
->parent
: TYPE_VIRTIO_PCI
,
2036 .instance_size
= t
->instance_size
,
2037 .instance_init
= t
->instance_init
,
2038 .class_size
= t
->class_size
,
2040 .interfaces
= t
->interfaces
,
2042 TypeInfo generic_type_info
= {
2043 .name
= t
->generic_name
,
2044 .parent
= base_type_info
.name
,
2045 .class_init
= virtio_pci_generic_class_init
,
2046 .interfaces
= (InterfaceInfo
[]) {
2047 { INTERFACE_PCIE_DEVICE
},
2048 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
2053 if (!base_type_info
.name
) {
2054 /* No base type -> register a single generic device type */
2055 /* use intermediate %s-base-type to add generic device props */
2056 base_name
= g_strdup_printf("%s-base-type", t
->generic_name
);
2057 base_type_info
.name
= base_name
;
2058 base_type_info
.class_init
= virtio_pci_generic_class_init
;
2060 generic_type_info
.parent
= base_name
;
2061 generic_type_info
.class_init
= virtio_pci_base_class_init
;
2062 generic_type_info
.class_data
= (void *)t
;
2064 assert(!t
->non_transitional_name
);
2065 assert(!t
->transitional_name
);
2067 base_type_info
.class_init
= virtio_pci_base_class_init
;
2068 base_type_info
.class_data
= (void *)t
;
2071 type_register(&base_type_info
);
2072 if (generic_type_info
.name
) {
2073 type_register(&generic_type_info
);
2076 if (t
->non_transitional_name
) {
2077 const TypeInfo non_transitional_type_info
= {
2078 .name
= t
->non_transitional_name
,
2079 .parent
= base_type_info
.name
,
2080 .instance_init
= virtio_pci_non_transitional_instance_init
,
2081 .interfaces
= (InterfaceInfo
[]) {
2082 { INTERFACE_PCIE_DEVICE
},
2083 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
2087 type_register(&non_transitional_type_info
);
2090 if (t
->transitional_name
) {
2091 const TypeInfo transitional_type_info
= {
2092 .name
= t
->transitional_name
,
2093 .parent
= base_type_info
.name
,
2094 .instance_init
= virtio_pci_transitional_instance_init
,
2095 .interfaces
= (InterfaceInfo
[]) {
2097 * Transitional virtio devices work only as Conventional PCI
2098 * devices because they require PIO ports.
2100 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
2104 type_register(&transitional_type_info
);
2109 unsigned virtio_pci_optimal_num_queues(unsigned fixed_queues
)
2112 * 1:1 vq to vCPU mapping is ideal because the same vCPU that submitted
2113 * virtqueue buffers can handle their completion. When a different vCPU
2114 * handles completion it may need to IPI the vCPU that submitted the
2115 * request and this adds overhead.
2117 * Virtqueues consume guest RAM and MSI-X vectors. This is wasteful in
2118 * guests with very many vCPUs and a device that is only used by a few
2119 * vCPUs. Unfortunately optimizing that case requires manual pinning inside
2120 * the guest, so those users might as well manually set the number of
2121 * queues. There is no upper limit that can be applied automatically and
2122 * doing so arbitrarily would result in a sudden performance drop once the
2123 * threshold number of vCPUs is exceeded.
2125 unsigned num_queues
= current_machine
->smp
.cpus
;
2128 * The maximum number of MSI-X vectors is PCI_MSIX_FLAGS_QSIZE + 1, but the
2129 * config change interrupt and the fixed virtqueues must be taken into
2132 num_queues
= MIN(num_queues
, PCI_MSIX_FLAGS_QSIZE
- fixed_queues
);
2135 * There is a limit to how many virtqueues a device can have.
2137 return MIN(num_queues
, VIRTIO_QUEUE_MAX
- fixed_queues
);
2140 /* virtio-pci-bus */
2142 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
2143 VirtIOPCIProxy
*dev
)
2145 DeviceState
*qdev
= DEVICE(dev
);
2146 char virtio_bus_name
[] = "virtio-bus";
2148 qbus_create_inplace(bus
, bus_size
, TYPE_VIRTIO_PCI_BUS
, qdev
,
2152 static void virtio_pci_bus_class_init(ObjectClass
*klass
, void *data
)
2154 BusClass
*bus_class
= BUS_CLASS(klass
);
2155 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
2156 bus_class
->max_dev
= 1;
2157 k
->notify
= virtio_pci_notify
;
2158 k
->save_config
= virtio_pci_save_config
;
2159 k
->load_config
= virtio_pci_load_config
;
2160 k
->save_queue
= virtio_pci_save_queue
;
2161 k
->load_queue
= virtio_pci_load_queue
;
2162 k
->save_extra_state
= virtio_pci_save_extra_state
;
2163 k
->load_extra_state
= virtio_pci_load_extra_state
;
2164 k
->has_extra_state
= virtio_pci_has_extra_state
;
2165 k
->query_guest_notifiers
= virtio_pci_query_guest_notifiers
;
2166 k
->set_guest_notifiers
= virtio_pci_set_guest_notifiers
;
2167 k
->set_host_notifier_mr
= virtio_pci_set_host_notifier_mr
;
2168 k
->vmstate_change
= virtio_pci_vmstate_change
;
2169 k
->pre_plugged
= virtio_pci_pre_plugged
;
2170 k
->device_plugged
= virtio_pci_device_plugged
;
2171 k
->device_unplugged
= virtio_pci_device_unplugged
;
2172 k
->query_nvectors
= virtio_pci_query_nvectors
;
2173 k
->ioeventfd_enabled
= virtio_pci_ioeventfd_enabled
;
2174 k
->ioeventfd_assign
= virtio_pci_ioeventfd_assign
;
2175 k
->get_dma_as
= virtio_pci_get_dma_as
;
2176 k
->queue_enabled
= virtio_pci_queue_enabled
;
2179 static const TypeInfo virtio_pci_bus_info
= {
2180 .name
= TYPE_VIRTIO_PCI_BUS
,
2181 .parent
= TYPE_VIRTIO_BUS
,
2182 .instance_size
= sizeof(VirtioPCIBusState
),
2183 .class_size
= sizeof(VirtioPCIBusClass
),
2184 .class_init
= virtio_pci_bus_class_init
,
2187 static void virtio_pci_register_types(void)
2190 type_register_static(&virtio_pci_bus_info
);
2191 type_register_static(&virtio_pci_info
);
2194 type_init(virtio_pci_register_types
)