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 "standard-headers/linux/virtio_ids.h"
23 #include "hw/boards.h"
24 #include "hw/virtio/virtio.h"
25 #include "migration/qemu-file-types.h"
26 #include "hw/pci/pci.h"
27 #include "hw/pci/pci_bus.h"
28 #include "hw/qdev-properties.h"
29 #include "qapi/error.h"
30 #include "qemu/error-report.h"
32 #include "qemu/module.h"
33 #include "hw/pci/msi.h"
34 #include "hw/pci/msix.h"
35 #include "hw/loader.h"
36 #include "sysemu/kvm.h"
37 #include "hw/virtio/virtio-pci.h"
38 #include "qemu/range.h"
39 #include "hw/virtio/virtio-bus.h"
40 #include "qapi/visitor.h"
41 #include "sysemu/replay.h"
44 #define VIRTIO_PCI_REGION_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_present(dev))
46 #undef VIRTIO_PCI_CONFIG
48 /* The remaining space is defined by each driver as the per-driver
49 * configuration space */
50 #define VIRTIO_PCI_CONFIG_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_enabled(dev))
52 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
54 static void virtio_pci_reset(DeviceState
*qdev
);
57 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
58 static inline VirtIOPCIProxy
*to_virtio_pci_proxy(DeviceState
*d
)
60 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
63 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
64 * be careful and test performance if you change this.
66 static inline VirtIOPCIProxy
*to_virtio_pci_proxy_fast(DeviceState
*d
)
68 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
71 static void virtio_pci_notify(DeviceState
*d
, uint16_t vector
)
73 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy_fast(d
);
75 if (msix_enabled(&proxy
->pci_dev
)) {
76 if (vector
!= VIRTIO_NO_VECTOR
) {
77 msix_notify(&proxy
->pci_dev
, vector
);
80 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
81 pci_set_irq(&proxy
->pci_dev
, qatomic_read(&vdev
->isr
) & 1);
85 static void virtio_pci_save_config(DeviceState
*d
, QEMUFile
*f
)
87 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
88 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
90 pci_device_save(&proxy
->pci_dev
, f
);
91 msix_save(&proxy
->pci_dev
, f
);
92 if (msix_present(&proxy
->pci_dev
))
93 qemu_put_be16(f
, vdev
->config_vector
);
96 static const VMStateDescription vmstate_virtio_pci_modern_queue_state
= {
97 .name
= "virtio_pci/modern_queue_state",
99 .minimum_version_id
= 1,
100 .fields
= (const VMStateField
[]) {
101 VMSTATE_UINT16(num
, VirtIOPCIQueue
),
102 VMSTATE_UNUSED(1), /* enabled was stored as be16 */
103 VMSTATE_BOOL(enabled
, VirtIOPCIQueue
),
104 VMSTATE_UINT32_ARRAY(desc
, VirtIOPCIQueue
, 2),
105 VMSTATE_UINT32_ARRAY(avail
, VirtIOPCIQueue
, 2),
106 VMSTATE_UINT32_ARRAY(used
, VirtIOPCIQueue
, 2),
107 VMSTATE_END_OF_LIST()
111 static bool virtio_pci_modern_state_needed(void *opaque
)
113 VirtIOPCIProxy
*proxy
= opaque
;
115 return virtio_pci_modern(proxy
);
118 static const VMStateDescription vmstate_virtio_pci_modern_state_sub
= {
119 .name
= "virtio_pci/modern_state",
121 .minimum_version_id
= 1,
122 .needed
= &virtio_pci_modern_state_needed
,
123 .fields
= (const VMStateField
[]) {
124 VMSTATE_UINT32(dfselect
, VirtIOPCIProxy
),
125 VMSTATE_UINT32(gfselect
, VirtIOPCIProxy
),
126 VMSTATE_UINT32_ARRAY(guest_features
, VirtIOPCIProxy
, 2),
127 VMSTATE_STRUCT_ARRAY(vqs
, VirtIOPCIProxy
, VIRTIO_QUEUE_MAX
, 0,
128 vmstate_virtio_pci_modern_queue_state
,
130 VMSTATE_END_OF_LIST()
134 static const VMStateDescription vmstate_virtio_pci
= {
135 .name
= "virtio_pci",
137 .minimum_version_id
= 1,
138 .fields
= (const VMStateField
[]) {
139 VMSTATE_END_OF_LIST()
141 .subsections
= (const VMStateDescription
* const []) {
142 &vmstate_virtio_pci_modern_state_sub
,
147 static bool virtio_pci_has_extra_state(DeviceState
*d
)
149 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
151 return proxy
->flags
& VIRTIO_PCI_FLAG_MIGRATE_EXTRA
;
154 static void virtio_pci_save_extra_state(DeviceState
*d
, QEMUFile
*f
)
156 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
158 vmstate_save_state(f
, &vmstate_virtio_pci
, proxy
, NULL
);
161 static int virtio_pci_load_extra_state(DeviceState
*d
, QEMUFile
*f
)
163 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
165 return vmstate_load_state(f
, &vmstate_virtio_pci
, proxy
, 1);
168 static void virtio_pci_save_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
170 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
171 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
173 if (msix_present(&proxy
->pci_dev
))
174 qemu_put_be16(f
, virtio_queue_vector(vdev
, n
));
177 static int virtio_pci_load_config(DeviceState
*d
, QEMUFile
*f
)
179 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
180 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
184 ret
= pci_device_load(&proxy
->pci_dev
, f
);
188 msix_unuse_all_vectors(&proxy
->pci_dev
);
189 msix_load(&proxy
->pci_dev
, f
);
190 if (msix_present(&proxy
->pci_dev
)) {
191 qemu_get_be16s(f
, &vector
);
193 if (vector
!= VIRTIO_NO_VECTOR
&& vector
>= proxy
->nvectors
) {
197 vector
= VIRTIO_NO_VECTOR
;
199 vdev
->config_vector
= vector
;
200 if (vector
!= VIRTIO_NO_VECTOR
) {
201 msix_vector_use(&proxy
->pci_dev
, vector
);
206 static int virtio_pci_load_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
208 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
209 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
212 if (msix_present(&proxy
->pci_dev
)) {
213 qemu_get_be16s(f
, &vector
);
214 if (vector
!= VIRTIO_NO_VECTOR
&& vector
>= proxy
->nvectors
) {
218 vector
= VIRTIO_NO_VECTOR
;
220 virtio_queue_set_vector(vdev
, n
, vector
);
221 if (vector
!= VIRTIO_NO_VECTOR
) {
222 msix_vector_use(&proxy
->pci_dev
, vector
);
228 typedef struct VirtIOPCIIDInfo
{
231 /* pci device id for the transitional device */
232 uint16_t trans_devid
;
236 static const VirtIOPCIIDInfo virtio_pci_id_info
[] = {
238 .vdev_id
= VIRTIO_ID_CRYPTO
,
239 .class_id
= PCI_CLASS_OTHERS
,
241 .vdev_id
= VIRTIO_ID_FS
,
242 .class_id
= PCI_CLASS_STORAGE_OTHER
,
244 .vdev_id
= VIRTIO_ID_NET
,
245 .trans_devid
= PCI_DEVICE_ID_VIRTIO_NET
,
246 .class_id
= PCI_CLASS_NETWORK_ETHERNET
,
248 .vdev_id
= VIRTIO_ID_BLOCK
,
249 .trans_devid
= PCI_DEVICE_ID_VIRTIO_BLOCK
,
250 .class_id
= PCI_CLASS_STORAGE_SCSI
,
252 .vdev_id
= VIRTIO_ID_CONSOLE
,
253 .trans_devid
= PCI_DEVICE_ID_VIRTIO_CONSOLE
,
254 .class_id
= PCI_CLASS_COMMUNICATION_OTHER
,
256 .vdev_id
= VIRTIO_ID_SCSI
,
257 .trans_devid
= PCI_DEVICE_ID_VIRTIO_SCSI
,
258 .class_id
= PCI_CLASS_STORAGE_SCSI
260 .vdev_id
= VIRTIO_ID_9P
,
261 .trans_devid
= PCI_DEVICE_ID_VIRTIO_9P
,
262 .class_id
= PCI_BASE_CLASS_NETWORK
,
264 .vdev_id
= VIRTIO_ID_BALLOON
,
265 .trans_devid
= PCI_DEVICE_ID_VIRTIO_BALLOON
,
266 .class_id
= PCI_CLASS_OTHERS
,
268 .vdev_id
= VIRTIO_ID_RNG
,
269 .trans_devid
= PCI_DEVICE_ID_VIRTIO_RNG
,
270 .class_id
= PCI_CLASS_OTHERS
,
274 static const VirtIOPCIIDInfo
*virtio_pci_get_id_info(uint16_t vdev_id
)
276 const VirtIOPCIIDInfo
*info
= NULL
;
279 for (i
= 0; i
< ARRAY_SIZE(virtio_pci_id_info
); i
++) {
280 if (virtio_pci_id_info
[i
].vdev_id
== vdev_id
) {
281 info
= &virtio_pci_id_info
[i
];
287 /* The device id is invalid or not added to the id_info yet. */
288 error_report("Invalid virtio device(id %u)", vdev_id
);
296 * Get the Transitional Device ID for the specific device, return
297 * zero if the device is non-transitional.
299 uint16_t virtio_pci_get_trans_devid(uint16_t device_id
)
301 return virtio_pci_get_id_info(device_id
)->trans_devid
;
305 * Get the Class ID for the specific device.
307 uint16_t virtio_pci_get_class_id(uint16_t device_id
)
309 return virtio_pci_get_id_info(device_id
)->class_id
;
312 static bool virtio_pci_ioeventfd_enabled(DeviceState
*d
)
314 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
316 return (proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) != 0;
319 #define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000
321 static inline int virtio_pci_queue_mem_mult(struct VirtIOPCIProxy
*proxy
)
323 return (proxy
->flags
& VIRTIO_PCI_FLAG_PAGE_PER_VQ
) ?
324 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
: 4;
327 static int virtio_pci_ioeventfd_assign(DeviceState
*d
, EventNotifier
*notifier
,
330 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
331 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
332 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
333 bool legacy
= virtio_pci_legacy(proxy
);
334 bool modern
= virtio_pci_modern(proxy
);
335 bool modern_pio
= proxy
->flags
& VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY
;
336 MemoryRegion
*modern_mr
= &proxy
->notify
.mr
;
337 MemoryRegion
*modern_notify_mr
= &proxy
->notify_pio
.mr
;
338 MemoryRegion
*legacy_mr
= &proxy
->bar
;
339 hwaddr modern_addr
= virtio_pci_queue_mem_mult(proxy
) *
340 virtio_get_queue_index(vq
);
341 hwaddr legacy_addr
= VIRTIO_PCI_QUEUE_NOTIFY
;
345 memory_region_add_eventfd(modern_mr
, modern_addr
, 0,
348 memory_region_add_eventfd(modern_notify_mr
, 0, 2,
353 memory_region_add_eventfd(legacy_mr
, legacy_addr
, 2,
358 memory_region_del_eventfd(modern_mr
, modern_addr
, 0,
361 memory_region_del_eventfd(modern_notify_mr
, 0, 2,
366 memory_region_del_eventfd(legacy_mr
, legacy_addr
, 2,
373 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
375 virtio_bus_start_ioeventfd(&proxy
->bus
);
378 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
380 virtio_bus_stop_ioeventfd(&proxy
->bus
);
383 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
385 VirtIOPCIProxy
*proxy
= opaque
;
386 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
391 case VIRTIO_PCI_GUEST_FEATURES
:
392 /* Guest does not negotiate properly? We have to assume nothing. */
393 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
394 val
= virtio_bus_get_vdev_bad_features(&proxy
->bus
);
396 virtio_set_features(vdev
, val
);
398 case VIRTIO_PCI_QUEUE_PFN
:
399 pa
= (hwaddr
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
401 virtio_pci_reset(DEVICE(proxy
));
404 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
406 case VIRTIO_PCI_QUEUE_SEL
:
407 if (val
< VIRTIO_QUEUE_MAX
)
408 vdev
->queue_sel
= val
;
410 case VIRTIO_PCI_QUEUE_NOTIFY
:
411 if (val
< VIRTIO_QUEUE_MAX
) {
412 virtio_queue_notify(vdev
, val
);
415 case VIRTIO_PCI_STATUS
:
416 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
417 virtio_pci_stop_ioeventfd(proxy
);
420 virtio_set_status(vdev
, val
& 0xFF);
422 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
423 virtio_pci_start_ioeventfd(proxy
);
426 if (vdev
->status
== 0) {
427 virtio_pci_reset(DEVICE(proxy
));
430 /* Linux before 2.6.34 drives the device without enabling
431 the PCI device bus master bit. Enable it automatically
432 for the guest. This is a PCI spec violation but so is
433 initiating DMA with bus master bit clear. */
434 if (val
== (VIRTIO_CONFIG_S_ACKNOWLEDGE
| VIRTIO_CONFIG_S_DRIVER
)) {
435 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
436 proxy
->pci_dev
.config
[PCI_COMMAND
] |
437 PCI_COMMAND_MASTER
, 1);
440 case VIRTIO_MSI_CONFIG_VECTOR
:
441 if (vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
442 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
444 /* Make it possible for guest to discover an error took place. */
445 if (val
< proxy
->nvectors
) {
446 msix_vector_use(&proxy
->pci_dev
, val
);
448 val
= VIRTIO_NO_VECTOR
;
450 vdev
->config_vector
= val
;
452 case VIRTIO_MSI_QUEUE_VECTOR
:
453 vector
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
454 if (vector
!= VIRTIO_NO_VECTOR
) {
455 msix_vector_unuse(&proxy
->pci_dev
, vector
);
457 /* Make it possible for guest to discover an error took place. */
458 if (val
< proxy
->nvectors
) {
459 msix_vector_use(&proxy
->pci_dev
, val
);
461 val
= VIRTIO_NO_VECTOR
;
463 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
466 qemu_log_mask(LOG_GUEST_ERROR
,
467 "%s: unexpected address 0x%x value 0x%x\n",
468 __func__
, addr
, val
);
473 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
475 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
476 uint32_t ret
= 0xFFFFFFFF;
479 case VIRTIO_PCI_HOST_FEATURES
:
480 ret
= vdev
->host_features
;
482 case VIRTIO_PCI_GUEST_FEATURES
:
483 ret
= vdev
->guest_features
;
485 case VIRTIO_PCI_QUEUE_PFN
:
486 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
487 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
489 case VIRTIO_PCI_QUEUE_NUM
:
490 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
492 case VIRTIO_PCI_QUEUE_SEL
:
493 ret
= vdev
->queue_sel
;
495 case VIRTIO_PCI_STATUS
:
499 /* reading from the ISR also clears it. */
500 ret
= qatomic_xchg(&vdev
->isr
, 0);
501 pci_irq_deassert(&proxy
->pci_dev
);
503 case VIRTIO_MSI_CONFIG_VECTOR
:
504 ret
= vdev
->config_vector
;
506 case VIRTIO_MSI_QUEUE_VECTOR
:
507 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
516 static uint64_t virtio_pci_config_read(void *opaque
, hwaddr addr
,
519 VirtIOPCIProxy
*proxy
= opaque
;
520 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
521 uint32_t config
= VIRTIO_PCI_CONFIG_SIZE(&proxy
->pci_dev
);
529 return virtio_ioport_read(proxy
, addr
);
535 val
= virtio_config_readb(vdev
, addr
);
538 val
= virtio_config_readw(vdev
, addr
);
539 if (virtio_is_big_endian(vdev
)) {
544 val
= virtio_config_readl(vdev
, addr
);
545 if (virtio_is_big_endian(vdev
)) {
553 static void virtio_pci_config_write(void *opaque
, hwaddr addr
,
554 uint64_t val
, unsigned size
)
556 VirtIOPCIProxy
*proxy
= opaque
;
557 uint32_t config
= VIRTIO_PCI_CONFIG_SIZE(&proxy
->pci_dev
);
558 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
565 virtio_ioport_write(proxy
, addr
, val
);
570 * Virtio-PCI is odd. Ioports are LE but config space is target native
575 virtio_config_writeb(vdev
, addr
, val
);
578 if (virtio_is_big_endian(vdev
)) {
581 virtio_config_writew(vdev
, addr
, val
);
584 if (virtio_is_big_endian(vdev
)) {
587 virtio_config_writel(vdev
, addr
, val
);
592 static const MemoryRegionOps virtio_pci_config_ops
= {
593 .read
= virtio_pci_config_read
,
594 .write
= virtio_pci_config_write
,
596 .min_access_size
= 1,
597 .max_access_size
= 4,
599 .endianness
= DEVICE_LITTLE_ENDIAN
,
602 static MemoryRegion
*virtio_address_space_lookup(VirtIOPCIProxy
*proxy
,
603 hwaddr
*off
, int len
)
606 VirtIOPCIRegion
*reg
;
608 for (i
= 0; i
< ARRAY_SIZE(proxy
->regs
); ++i
) {
609 reg
= &proxy
->regs
[i
];
610 if (*off
>= reg
->offset
&&
611 *off
+ len
<= reg
->offset
+ reg
->size
) {
620 /* Below are generic functions to do memcpy from/to an address space,
621 * without byteswaps, with input validation.
623 * As regular address_space_* APIs all do some kind of byteswap at least for
624 * some host/target combinations, we are forced to explicitly convert to a
625 * known-endianness integer value.
626 * It doesn't really matter which endian format to go through, so the code
627 * below selects the endian that causes the least amount of work on the given
630 * Note: host pointer must be aligned.
633 void virtio_address_space_write(VirtIOPCIProxy
*proxy
, hwaddr addr
,
634 const uint8_t *buf
, int len
)
639 /* address_space_* APIs assume an aligned address.
640 * As address is under guest control, handle illegal values.
644 mr
= virtio_address_space_lookup(proxy
, &addr
, len
);
649 /* Make sure caller aligned buf properly */
650 assert(!(((uintptr_t)buf
) & (len
- 1)));
654 val
= pci_get_byte(buf
);
657 val
= pci_get_word(buf
);
660 val
= pci_get_long(buf
);
663 /* As length is under guest control, handle illegal values. */
666 memory_region_dispatch_write(mr
, addr
, val
, size_memop(len
) | MO_LE
,
667 MEMTXATTRS_UNSPECIFIED
);
671 virtio_address_space_read(VirtIOPCIProxy
*proxy
, hwaddr addr
,
672 uint8_t *buf
, int len
)
677 /* address_space_* APIs assume an aligned address.
678 * As address is under guest control, handle illegal values.
682 mr
= virtio_address_space_lookup(proxy
, &addr
, len
);
687 /* Make sure caller aligned buf properly */
688 assert(!(((uintptr_t)buf
) & (len
- 1)));
690 memory_region_dispatch_read(mr
, addr
, &val
, size_memop(len
) | MO_LE
,
691 MEMTXATTRS_UNSPECIFIED
);
694 pci_set_byte(buf
, val
);
697 pci_set_word(buf
, val
);
700 pci_set_long(buf
, val
);
703 /* As length is under guest control, handle illegal values. */
708 static void virtio_pci_ats_ctrl_trigger(PCIDevice
*pci_dev
, bool enable
)
710 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
711 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
712 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
714 vdev
->device_iotlb_enabled
= enable
;
716 if (k
->toggle_device_iotlb
) {
717 k
->toggle_device_iotlb(vdev
);
721 static void pcie_ats_config_write(PCIDevice
*dev
, uint32_t address
,
722 uint32_t val
, int len
)
725 uint16_t ats_cap
= dev
->exp
.ats_cap
;
727 if (!ats_cap
|| address
< ats_cap
) {
730 off
= address
- ats_cap
;
731 if (off
>= PCI_EXT_CAP_ATS_SIZEOF
) {
735 if (range_covers_byte(off
, len
, PCI_ATS_CTRL
+ 1)) {
736 virtio_pci_ats_ctrl_trigger(dev
, !!(val
& PCI_ATS_CTRL_ENABLE
));
740 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
741 uint32_t val
, int len
)
743 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
744 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
745 struct virtio_pci_cfg_cap
*cfg
;
747 pci_default_write_config(pci_dev
, address
, val
, len
);
749 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_FLR
) {
750 pcie_cap_flr_write_config(pci_dev
, address
, val
, len
);
753 if (proxy
->flags
& VIRTIO_PCI_FLAG_ATS
) {
754 pcie_ats_config_write(pci_dev
, address
, val
, len
);
757 if (range_covers_byte(address
, len
, PCI_COMMAND
)) {
758 if (!(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
759 virtio_set_disabled(vdev
, true);
760 virtio_pci_stop_ioeventfd(proxy
);
761 virtio_set_status(vdev
, vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
763 virtio_set_disabled(vdev
, false);
767 if (proxy
->config_cap
&&
768 ranges_overlap(address
, len
, proxy
->config_cap
+ offsetof(struct virtio_pci_cfg_cap
,
770 sizeof cfg
->pci_cfg_data
)) {
774 cfg
= (void *)(proxy
->pci_dev
.config
+ proxy
->config_cap
);
775 off
= le32_to_cpu(cfg
->cap
.offset
);
776 caplen
= le32_to_cpu(cfg
->cap
.length
);
778 if (caplen
== 1 || caplen
== 2 || caplen
== 4) {
779 assert(caplen
<= sizeof cfg
->pci_cfg_data
);
780 virtio_address_space_write(proxy
, off
, cfg
->pci_cfg_data
, caplen
);
785 static uint32_t virtio_read_config(PCIDevice
*pci_dev
,
786 uint32_t address
, int len
)
788 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
789 struct virtio_pci_cfg_cap
*cfg
;
791 if (proxy
->config_cap
&&
792 ranges_overlap(address
, len
, proxy
->config_cap
+ offsetof(struct virtio_pci_cfg_cap
,
794 sizeof cfg
->pci_cfg_data
)) {
798 cfg
= (void *)(proxy
->pci_dev
.config
+ proxy
->config_cap
);
799 off
= le32_to_cpu(cfg
->cap
.offset
);
800 caplen
= le32_to_cpu(cfg
->cap
.length
);
802 if (caplen
== 1 || caplen
== 2 || caplen
== 4) {
803 assert(caplen
<= sizeof cfg
->pci_cfg_data
);
804 virtio_address_space_read(proxy
, off
, cfg
->pci_cfg_data
, caplen
);
808 return pci_default_read_config(pci_dev
, address
, len
);
811 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy
*proxy
,
814 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
817 if (irqfd
->users
== 0) {
818 KVMRouteChange c
= kvm_irqchip_begin_route_changes(kvm_state
);
819 ret
= kvm_irqchip_add_msi_route(&c
, vector
, &proxy
->pci_dev
);
823 kvm_irqchip_commit_route_changes(&c
);
830 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy
*proxy
,
833 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
834 if (--irqfd
->users
== 0) {
835 kvm_irqchip_release_virq(kvm_state
, irqfd
->virq
);
839 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy
*proxy
,
843 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
844 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, n
, NULL
, irqfd
->virq
);
847 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy
*proxy
,
851 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
854 ret
= kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state
, n
, irqfd
->virq
);
857 static int virtio_pci_get_notifier(VirtIOPCIProxy
*proxy
, int queue_no
,
858 EventNotifier
**n
, unsigned int *vector
)
860 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
863 if (queue_no
== VIRTIO_CONFIG_IRQ_IDX
) {
864 *n
= virtio_config_get_guest_notifier(vdev
);
865 *vector
= vdev
->config_vector
;
867 if (!virtio_queue_get_num(vdev
, queue_no
)) {
870 *vector
= virtio_queue_vector(vdev
, queue_no
);
871 vq
= virtio_get_queue(vdev
, queue_no
);
872 *n
= virtio_queue_get_guest_notifier(vq
);
877 static int kvm_virtio_pci_vector_use_one(VirtIOPCIProxy
*proxy
, int queue_no
)
882 PCIDevice
*dev
= &proxy
->pci_dev
;
883 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
884 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
886 ret
= virtio_pci_get_notifier(proxy
, queue_no
, &n
, &vector
);
890 if (vector
>= msix_nr_vectors_allocated(dev
)) {
893 ret
= kvm_virtio_pci_vq_vector_use(proxy
, vector
);
898 * If guest supports masking, set up irqfd now.
899 * Otherwise, delay until unmasked in the frontend.
901 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
902 ret
= kvm_virtio_pci_irqfd_use(proxy
, n
, vector
);
904 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
912 vector
= virtio_queue_vector(vdev
, queue_no
);
913 if (vector
>= msix_nr_vectors_allocated(dev
)) {
916 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
917 ret
= virtio_pci_get_notifier(proxy
, queue_no
, &n
, &vector
);
921 kvm_virtio_pci_irqfd_release(proxy
, n
, vector
);
925 static int kvm_virtio_pci_vector_vq_use(VirtIOPCIProxy
*proxy
, int nvqs
)
929 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
931 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
932 if (!virtio_queue_get_num(vdev
, queue_no
)) {
935 ret
= kvm_virtio_pci_vector_use_one(proxy
, queue_no
);
940 static int kvm_virtio_pci_vector_config_use(VirtIOPCIProxy
*proxy
)
942 return kvm_virtio_pci_vector_use_one(proxy
, VIRTIO_CONFIG_IRQ_IDX
);
945 static void kvm_virtio_pci_vector_release_one(VirtIOPCIProxy
*proxy
,
948 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
952 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
953 PCIDevice
*dev
= &proxy
->pci_dev
;
955 ret
= virtio_pci_get_notifier(proxy
, queue_no
, &n
, &vector
);
959 if (vector
>= msix_nr_vectors_allocated(dev
)) {
962 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
963 kvm_virtio_pci_irqfd_release(proxy
, n
, vector
);
965 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
968 static void kvm_virtio_pci_vector_vq_release(VirtIOPCIProxy
*proxy
, int nvqs
)
971 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
973 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
974 if (!virtio_queue_get_num(vdev
, queue_no
)) {
977 kvm_virtio_pci_vector_release_one(proxy
, queue_no
);
981 static void kvm_virtio_pci_vector_config_release(VirtIOPCIProxy
*proxy
)
983 kvm_virtio_pci_vector_release_one(proxy
, VIRTIO_CONFIG_IRQ_IDX
);
986 static int virtio_pci_one_vector_unmask(VirtIOPCIProxy
*proxy
,
987 unsigned int queue_no
,
992 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
993 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
997 if (proxy
->vector_irqfd
) {
998 irqfd
= &proxy
->vector_irqfd
[vector
];
999 if (irqfd
->msg
.data
!= msg
.data
|| irqfd
->msg
.address
!= msg
.address
) {
1000 ret
= kvm_irqchip_update_msi_route(kvm_state
, irqfd
->virq
, msg
,
1005 kvm_irqchip_commit_routes(kvm_state
);
1009 /* If guest supports masking, irqfd is already setup, unmask it.
1010 * Otherwise, set it up now.
1012 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
1013 k
->guest_notifier_mask(vdev
, queue_no
, false);
1014 /* Test after unmasking to avoid losing events. */
1015 if (k
->guest_notifier_pending
&&
1016 k
->guest_notifier_pending(vdev
, queue_no
)) {
1017 event_notifier_set(n
);
1020 ret
= kvm_virtio_pci_irqfd_use(proxy
, n
, vector
);
1025 static void virtio_pci_one_vector_mask(VirtIOPCIProxy
*proxy
,
1026 unsigned int queue_no
,
1027 unsigned int vector
,
1030 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1031 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1033 /* If guest supports masking, keep irqfd but mask it.
1034 * Otherwise, clean it up now.
1036 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
1037 k
->guest_notifier_mask(vdev
, queue_no
, true);
1039 kvm_virtio_pci_irqfd_release(proxy
, n
, vector
);
1043 static int virtio_pci_vector_unmask(PCIDevice
*dev
, unsigned vector
,
1046 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
1047 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1048 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
1050 int ret
, index
, unmasked
= 0;
1053 index
= virtio_get_queue_index(vq
);
1054 if (!virtio_queue_get_num(vdev
, index
)) {
1057 if (index
< proxy
->nvqs_with_notifiers
) {
1058 n
= virtio_queue_get_guest_notifier(vq
);
1059 ret
= virtio_pci_one_vector_unmask(proxy
, index
, vector
, msg
, n
);
1065 vq
= virtio_vector_next_queue(vq
);
1067 /* unmask config intr */
1068 if (vector
== vdev
->config_vector
) {
1069 n
= virtio_config_get_guest_notifier(vdev
);
1070 ret
= virtio_pci_one_vector_unmask(proxy
, VIRTIO_CONFIG_IRQ_IDX
, vector
,
1078 n
= virtio_config_get_guest_notifier(vdev
);
1079 virtio_pci_one_vector_mask(proxy
, VIRTIO_CONFIG_IRQ_IDX
, vector
, n
);
1081 vq
= virtio_vector_first_queue(vdev
, vector
);
1082 while (vq
&& unmasked
>= 0) {
1083 index
= virtio_get_queue_index(vq
);
1084 if (index
< proxy
->nvqs_with_notifiers
) {
1085 n
= virtio_queue_get_guest_notifier(vq
);
1086 virtio_pci_one_vector_mask(proxy
, index
, vector
, n
);
1089 vq
= virtio_vector_next_queue(vq
);
1094 static void virtio_pci_vector_mask(PCIDevice
*dev
, unsigned vector
)
1096 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
1097 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1098 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
1103 index
= virtio_get_queue_index(vq
);
1104 n
= virtio_queue_get_guest_notifier(vq
);
1105 if (!virtio_queue_get_num(vdev
, index
)) {
1108 if (index
< proxy
->nvqs_with_notifiers
) {
1109 virtio_pci_one_vector_mask(proxy
, index
, vector
, n
);
1111 vq
= virtio_vector_next_queue(vq
);
1114 if (vector
== vdev
->config_vector
) {
1115 n
= virtio_config_get_guest_notifier(vdev
);
1116 virtio_pci_one_vector_mask(proxy
, VIRTIO_CONFIG_IRQ_IDX
, vector
, n
);
1120 static void virtio_pci_vector_poll(PCIDevice
*dev
,
1121 unsigned int vector_start
,
1122 unsigned int vector_end
)
1124 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
1125 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1126 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1128 unsigned int vector
;
1129 EventNotifier
*notifier
;
1132 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
1133 ret
= virtio_pci_get_notifier(proxy
, queue_no
, ¬ifier
, &vector
);
1137 if (vector
< vector_start
|| vector
>= vector_end
||
1138 !msix_is_masked(dev
, vector
)) {
1141 if (k
->guest_notifier_pending
) {
1142 if (k
->guest_notifier_pending(vdev
, queue_no
)) {
1143 msix_set_pending(dev
, vector
);
1145 } else if (event_notifier_test_and_clear(notifier
)) {
1146 msix_set_pending(dev
, vector
);
1149 /* poll the config intr */
1150 ret
= virtio_pci_get_notifier(proxy
, VIRTIO_CONFIG_IRQ_IDX
, ¬ifier
,
1155 if (vector
< vector_start
|| vector
>= vector_end
||
1156 !msix_is_masked(dev
, vector
)) {
1159 if (k
->guest_notifier_pending
) {
1160 if (k
->guest_notifier_pending(vdev
, VIRTIO_CONFIG_IRQ_IDX
)) {
1161 msix_set_pending(dev
, vector
);
1163 } else if (event_notifier_test_and_clear(notifier
)) {
1164 msix_set_pending(dev
, vector
);
1168 void virtio_pci_set_guest_notifier_fd_handler(VirtIODevice
*vdev
, VirtQueue
*vq
,
1172 if (n
== VIRTIO_CONFIG_IRQ_IDX
) {
1173 virtio_config_set_guest_notifier_fd_handler(vdev
, assign
, with_irqfd
);
1175 virtio_queue_set_guest_notifier_fd_handler(vq
, assign
, with_irqfd
);
1179 static int virtio_pci_set_guest_notifier(DeviceState
*d
, int n
, bool assign
,
1182 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1183 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1184 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1185 VirtQueue
*vq
= NULL
;
1186 EventNotifier
*notifier
= NULL
;
1188 if (n
== VIRTIO_CONFIG_IRQ_IDX
) {
1189 notifier
= virtio_config_get_guest_notifier(vdev
);
1191 vq
= virtio_get_queue(vdev
, n
);
1192 notifier
= virtio_queue_get_guest_notifier(vq
);
1196 int r
= event_notifier_init(notifier
, 0);
1200 virtio_pci_set_guest_notifier_fd_handler(vdev
, vq
, n
, true, with_irqfd
);
1202 virtio_pci_set_guest_notifier_fd_handler(vdev
, vq
, n
, false,
1204 event_notifier_cleanup(notifier
);
1207 if (!msix_enabled(&proxy
->pci_dev
) &&
1208 vdev
->use_guest_notifier_mask
&&
1209 vdc
->guest_notifier_mask
) {
1210 vdc
->guest_notifier_mask(vdev
, n
, !assign
);
1216 static bool virtio_pci_query_guest_notifiers(DeviceState
*d
)
1218 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1219 return msix_enabled(&proxy
->pci_dev
);
1222 static int virtio_pci_set_guest_notifiers(DeviceState
*d
, int nvqs
, bool assign
)
1224 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1225 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1226 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1228 bool with_irqfd
= msix_enabled(&proxy
->pci_dev
) &&
1229 kvm_msi_via_irqfd_enabled();
1231 nvqs
= MIN(nvqs
, VIRTIO_QUEUE_MAX
);
1234 * When deassigning, pass a consistent nvqs value to avoid leaking
1235 * notifiers. But first check we've actually been configured, exit
1236 * early if we haven't.
1238 if (!assign
&& !proxy
->nvqs_with_notifiers
) {
1241 assert(assign
|| nvqs
== proxy
->nvqs_with_notifiers
);
1243 proxy
->nvqs_with_notifiers
= nvqs
;
1245 /* Must unset vector notifier while guest notifier is still assigned */
1246 if ((proxy
->vector_irqfd
||
1247 (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
)) &&
1249 msix_unset_vector_notifiers(&proxy
->pci_dev
);
1250 if (proxy
->vector_irqfd
) {
1251 kvm_virtio_pci_vector_vq_release(proxy
, nvqs
);
1252 kvm_virtio_pci_vector_config_release(proxy
);
1253 g_free(proxy
->vector_irqfd
);
1254 proxy
->vector_irqfd
= NULL
;
1258 for (n
= 0; n
< nvqs
; n
++) {
1259 if (!virtio_queue_get_num(vdev
, n
)) {
1263 r
= virtio_pci_set_guest_notifier(d
, n
, assign
, with_irqfd
);
1268 r
= virtio_pci_set_guest_notifier(d
, VIRTIO_CONFIG_IRQ_IDX
, assign
,
1271 goto config_assign_error
;
1273 /* Must set vector notifier after guest notifier has been assigned */
1275 (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
)) &&
1278 proxy
->vector_irqfd
=
1279 g_malloc0(sizeof(*proxy
->vector_irqfd
) *
1280 msix_nr_vectors_allocated(&proxy
->pci_dev
));
1281 r
= kvm_virtio_pci_vector_vq_use(proxy
, nvqs
);
1283 goto config_assign_error
;
1285 r
= kvm_virtio_pci_vector_config_use(proxy
);
1291 r
= msix_set_vector_notifiers(&proxy
->pci_dev
, virtio_pci_vector_unmask
,
1292 virtio_pci_vector_mask
,
1293 virtio_pci_vector_poll
);
1295 goto notifiers_error
;
1304 kvm_virtio_pci_vector_vq_release(proxy
, nvqs
);
1308 kvm_virtio_pci_vector_config_release(proxy
);
1310 config_assign_error
:
1311 virtio_pci_set_guest_notifier(d
, VIRTIO_CONFIG_IRQ_IDX
, !assign
,
1314 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
1317 virtio_pci_set_guest_notifier(d
, n
, !assign
, with_irqfd
);
1319 g_free(proxy
->vector_irqfd
);
1320 proxy
->vector_irqfd
= NULL
;
1324 static int virtio_pci_set_host_notifier_mr(DeviceState
*d
, int n
,
1325 MemoryRegion
*mr
, bool assign
)
1327 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1330 if (n
>= VIRTIO_QUEUE_MAX
|| !virtio_pci_modern(proxy
) ||
1331 virtio_pci_queue_mem_mult(proxy
) != memory_region_size(mr
)) {
1336 offset
= virtio_pci_queue_mem_mult(proxy
) * n
;
1337 memory_region_add_subregion_overlap(&proxy
->notify
.mr
, offset
, mr
, 1);
1339 memory_region_del_subregion(&proxy
->notify
.mr
, mr
);
1345 static void virtio_pci_vmstate_change(DeviceState
*d
, bool running
)
1347 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1348 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1351 /* Old QEMU versions did not set bus master enable on status write.
1352 * Detect DRIVER set and enable it.
1354 if ((proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION
) &&
1355 (vdev
->status
& VIRTIO_CONFIG_S_DRIVER
) &&
1356 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
1357 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
1358 proxy
->pci_dev
.config
[PCI_COMMAND
] |
1359 PCI_COMMAND_MASTER
, 1);
1361 virtio_pci_start_ioeventfd(proxy
);
1363 virtio_pci_stop_ioeventfd(proxy
);
1368 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
1371 static int virtio_pci_query_nvectors(DeviceState
*d
)
1373 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1375 return proxy
->nvectors
;
1378 static AddressSpace
*virtio_pci_get_dma_as(DeviceState
*d
)
1380 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1381 PCIDevice
*dev
= &proxy
->pci_dev
;
1383 return pci_get_address_space(dev
);
1386 static bool virtio_pci_iommu_enabled(DeviceState
*d
)
1388 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1389 PCIDevice
*dev
= &proxy
->pci_dev
;
1390 AddressSpace
*dma_as
= pci_device_iommu_address_space(dev
);
1392 if (dma_as
== &address_space_memory
) {
1399 static bool virtio_pci_queue_enabled(DeviceState
*d
, int n
)
1401 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1402 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1404 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
1405 return proxy
->vqs
[n
].enabled
;
1408 return virtio_queue_enabled_legacy(vdev
, n
);
1411 static int virtio_pci_add_mem_cap(VirtIOPCIProxy
*proxy
,
1412 struct virtio_pci_cap
*cap
)
1414 PCIDevice
*dev
= &proxy
->pci_dev
;
1417 offset
= pci_add_capability(dev
, PCI_CAP_ID_VNDR
, 0,
1418 cap
->cap_len
, &error_abort
);
1420 assert(cap
->cap_len
>= sizeof *cap
);
1421 memcpy(dev
->config
+ offset
+ PCI_CAP_FLAGS
, &cap
->cap_len
,
1422 cap
->cap_len
- PCI_CAP_FLAGS
);
1427 int virtio_pci_add_shm_cap(VirtIOPCIProxy
*proxy
,
1428 uint8_t bar
, uint64_t offset
, uint64_t length
,
1431 struct virtio_pci_cap64 cap
= {
1432 .cap
.cap_len
= sizeof cap
,
1433 .cap
.cfg_type
= VIRTIO_PCI_CAP_SHARED_MEMORY_CFG
,
1437 cap
.cap
.length
= cpu_to_le32(length
);
1438 cap
.length_hi
= cpu_to_le32(length
>> 32);
1439 cap
.cap
.offset
= cpu_to_le32(offset
);
1440 cap
.offset_hi
= cpu_to_le32(offset
>> 32);
1442 return virtio_pci_add_mem_cap(proxy
, &cap
.cap
);
1445 static uint64_t virtio_pci_common_read(void *opaque
, hwaddr addr
,
1448 VirtIOPCIProxy
*proxy
= opaque
;
1449 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1458 case VIRTIO_PCI_COMMON_DFSELECT
:
1459 val
= proxy
->dfselect
;
1461 case VIRTIO_PCI_COMMON_DF
:
1462 if (proxy
->dfselect
<= 1) {
1463 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1465 val
= (vdev
->host_features
& ~vdc
->legacy_features
) >>
1466 (32 * proxy
->dfselect
);
1469 case VIRTIO_PCI_COMMON_GFSELECT
:
1470 val
= proxy
->gfselect
;
1472 case VIRTIO_PCI_COMMON_GF
:
1473 if (proxy
->gfselect
< ARRAY_SIZE(proxy
->guest_features
)) {
1474 val
= proxy
->guest_features
[proxy
->gfselect
];
1477 case VIRTIO_PCI_COMMON_MSIX
:
1478 val
= vdev
->config_vector
;
1480 case VIRTIO_PCI_COMMON_NUMQ
:
1481 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; ++i
) {
1482 if (virtio_queue_get_num(vdev
, i
)) {
1487 case VIRTIO_PCI_COMMON_STATUS
:
1490 case VIRTIO_PCI_COMMON_CFGGENERATION
:
1491 val
= vdev
->generation
;
1493 case VIRTIO_PCI_COMMON_Q_SELECT
:
1494 val
= vdev
->queue_sel
;
1496 case VIRTIO_PCI_COMMON_Q_SIZE
:
1497 val
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
1499 case VIRTIO_PCI_COMMON_Q_MSIX
:
1500 val
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
1502 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1503 val
= proxy
->vqs
[vdev
->queue_sel
].enabled
;
1505 case VIRTIO_PCI_COMMON_Q_NOFF
:
1506 /* Simply map queues in order */
1507 val
= vdev
->queue_sel
;
1509 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1510 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[0];
1512 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1513 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[1];
1515 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1516 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[0];
1518 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1519 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[1];
1521 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1522 val
= proxy
->vqs
[vdev
->queue_sel
].used
[0];
1524 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1525 val
= proxy
->vqs
[vdev
->queue_sel
].used
[1];
1527 case VIRTIO_PCI_COMMON_Q_RESET
:
1528 val
= proxy
->vqs
[vdev
->queue_sel
].reset
;
1537 static void virtio_pci_common_write(void *opaque
, hwaddr addr
,
1538 uint64_t val
, unsigned size
)
1540 VirtIOPCIProxy
*proxy
= opaque
;
1541 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1549 case VIRTIO_PCI_COMMON_DFSELECT
:
1550 proxy
->dfselect
= val
;
1552 case VIRTIO_PCI_COMMON_GFSELECT
:
1553 proxy
->gfselect
= val
;
1555 case VIRTIO_PCI_COMMON_GF
:
1556 if (proxy
->gfselect
< ARRAY_SIZE(proxy
->guest_features
)) {
1557 proxy
->guest_features
[proxy
->gfselect
] = val
;
1558 virtio_set_features(vdev
,
1559 (((uint64_t)proxy
->guest_features
[1]) << 32) |
1560 proxy
->guest_features
[0]);
1563 case VIRTIO_PCI_COMMON_MSIX
:
1564 if (vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
1565 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
1567 /* Make it possible for guest to discover an error took place. */
1568 if (val
< proxy
->nvectors
) {
1569 msix_vector_use(&proxy
->pci_dev
, val
);
1571 val
= VIRTIO_NO_VECTOR
;
1573 vdev
->config_vector
= val
;
1575 case VIRTIO_PCI_COMMON_STATUS
:
1576 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
1577 virtio_pci_stop_ioeventfd(proxy
);
1580 virtio_set_status(vdev
, val
& 0xFF);
1582 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
1583 virtio_pci_start_ioeventfd(proxy
);
1586 if (vdev
->status
== 0) {
1587 virtio_pci_reset(DEVICE(proxy
));
1591 case VIRTIO_PCI_COMMON_Q_SELECT
:
1592 if (val
< VIRTIO_QUEUE_MAX
) {
1593 vdev
->queue_sel
= val
;
1596 case VIRTIO_PCI_COMMON_Q_SIZE
:
1597 proxy
->vqs
[vdev
->queue_sel
].num
= val
;
1598 virtio_queue_set_num(vdev
, vdev
->queue_sel
,
1599 proxy
->vqs
[vdev
->queue_sel
].num
);
1600 virtio_init_region_cache(vdev
, vdev
->queue_sel
);
1602 case VIRTIO_PCI_COMMON_Q_MSIX
:
1603 vector
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
1604 if (vector
!= VIRTIO_NO_VECTOR
) {
1605 msix_vector_unuse(&proxy
->pci_dev
, vector
);
1607 /* Make it possible for guest to discover an error took place. */
1608 if (val
< proxy
->nvectors
) {
1609 msix_vector_use(&proxy
->pci_dev
, val
);
1611 val
= VIRTIO_NO_VECTOR
;
1613 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
1615 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1617 virtio_queue_set_num(vdev
, vdev
->queue_sel
,
1618 proxy
->vqs
[vdev
->queue_sel
].num
);
1619 virtio_queue_set_rings(vdev
, vdev
->queue_sel
,
1620 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].desc
[1]) << 32 |
1621 proxy
->vqs
[vdev
->queue_sel
].desc
[0],
1622 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].avail
[1]) << 32 |
1623 proxy
->vqs
[vdev
->queue_sel
].avail
[0],
1624 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].used
[1]) << 32 |
1625 proxy
->vqs
[vdev
->queue_sel
].used
[0]);
1626 proxy
->vqs
[vdev
->queue_sel
].enabled
= 1;
1627 proxy
->vqs
[vdev
->queue_sel
].reset
= 0;
1628 virtio_queue_enable(vdev
, vdev
->queue_sel
);
1630 virtio_error(vdev
, "wrong value for queue_enable %"PRIx64
, val
);
1633 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1634 proxy
->vqs
[vdev
->queue_sel
].desc
[0] = val
;
1636 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1637 proxy
->vqs
[vdev
->queue_sel
].desc
[1] = val
;
1639 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1640 proxy
->vqs
[vdev
->queue_sel
].avail
[0] = val
;
1642 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1643 proxy
->vqs
[vdev
->queue_sel
].avail
[1] = val
;
1645 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1646 proxy
->vqs
[vdev
->queue_sel
].used
[0] = val
;
1648 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1649 proxy
->vqs
[vdev
->queue_sel
].used
[1] = val
;
1651 case VIRTIO_PCI_COMMON_Q_RESET
:
1653 proxy
->vqs
[vdev
->queue_sel
].reset
= 1;
1655 virtio_queue_reset(vdev
, vdev
->queue_sel
);
1657 proxy
->vqs
[vdev
->queue_sel
].reset
= 0;
1658 proxy
->vqs
[vdev
->queue_sel
].enabled
= 0;
1667 static uint64_t virtio_pci_notify_read(void *opaque
, hwaddr addr
,
1670 VirtIOPCIProxy
*proxy
= opaque
;
1671 if (virtio_bus_get_device(&proxy
->bus
) == NULL
) {
1678 static void virtio_pci_notify_write(void *opaque
, hwaddr addr
,
1679 uint64_t val
, unsigned size
)
1681 VirtIOPCIProxy
*proxy
= opaque
;
1682 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1684 unsigned queue
= addr
/ virtio_pci_queue_mem_mult(proxy
);
1686 if (vdev
!= NULL
&& queue
< VIRTIO_QUEUE_MAX
) {
1687 trace_virtio_pci_notify_write(addr
, val
, size
);
1688 virtio_queue_notify(vdev
, queue
);
1692 static void virtio_pci_notify_write_pio(void *opaque
, hwaddr addr
,
1693 uint64_t val
, unsigned size
)
1695 VirtIOPCIProxy
*proxy
= opaque
;
1696 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1698 unsigned queue
= val
;
1700 if (vdev
!= NULL
&& queue
< VIRTIO_QUEUE_MAX
) {
1701 trace_virtio_pci_notify_write_pio(addr
, val
, size
);
1702 virtio_queue_notify(vdev
, queue
);
1706 static uint64_t virtio_pci_isr_read(void *opaque
, hwaddr addr
,
1709 VirtIOPCIProxy
*proxy
= opaque
;
1710 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1717 val
= qatomic_xchg(&vdev
->isr
, 0);
1718 pci_irq_deassert(&proxy
->pci_dev
);
1722 static void virtio_pci_isr_write(void *opaque
, hwaddr addr
,
1723 uint64_t val
, unsigned size
)
1727 static uint64_t virtio_pci_device_read(void *opaque
, hwaddr addr
,
1730 VirtIOPCIProxy
*proxy
= opaque
;
1731 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1740 val
= virtio_config_modern_readb(vdev
, addr
);
1743 val
= virtio_config_modern_readw(vdev
, addr
);
1746 val
= virtio_config_modern_readl(vdev
, addr
);
1755 static void virtio_pci_device_write(void *opaque
, hwaddr addr
,
1756 uint64_t val
, unsigned size
)
1758 VirtIOPCIProxy
*proxy
= opaque
;
1759 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1767 virtio_config_modern_writeb(vdev
, addr
, val
);
1770 virtio_config_modern_writew(vdev
, addr
, val
);
1773 virtio_config_modern_writel(vdev
, addr
, val
);
1778 static void virtio_pci_modern_regions_init(VirtIOPCIProxy
*proxy
,
1779 const char *vdev_name
)
1781 static const MemoryRegionOps common_ops
= {
1782 .read
= virtio_pci_common_read
,
1783 .write
= virtio_pci_common_write
,
1785 .min_access_size
= 1,
1786 .max_access_size
= 4,
1788 .endianness
= DEVICE_LITTLE_ENDIAN
,
1790 static const MemoryRegionOps isr_ops
= {
1791 .read
= virtio_pci_isr_read
,
1792 .write
= virtio_pci_isr_write
,
1794 .min_access_size
= 1,
1795 .max_access_size
= 4,
1797 .endianness
= DEVICE_LITTLE_ENDIAN
,
1799 static const MemoryRegionOps device_ops
= {
1800 .read
= virtio_pci_device_read
,
1801 .write
= virtio_pci_device_write
,
1803 .min_access_size
= 1,
1804 .max_access_size
= 4,
1806 .endianness
= DEVICE_LITTLE_ENDIAN
,
1808 static const MemoryRegionOps notify_ops
= {
1809 .read
= virtio_pci_notify_read
,
1810 .write
= virtio_pci_notify_write
,
1812 .min_access_size
= 1,
1813 .max_access_size
= 4,
1815 .endianness
= DEVICE_LITTLE_ENDIAN
,
1817 static const MemoryRegionOps notify_pio_ops
= {
1818 .read
= virtio_pci_notify_read
,
1819 .write
= virtio_pci_notify_write_pio
,
1821 .min_access_size
= 1,
1822 .max_access_size
= 4,
1824 .endianness
= DEVICE_LITTLE_ENDIAN
,
1826 g_autoptr(GString
) name
= g_string_new(NULL
);
1828 g_string_printf(name
, "virtio-pci-common-%s", vdev_name
);
1829 memory_region_init_io(&proxy
->common
.mr
, OBJECT(proxy
),
1833 proxy
->common
.size
);
1835 g_string_printf(name
, "virtio-pci-isr-%s", vdev_name
);
1836 memory_region_init_io(&proxy
->isr
.mr
, OBJECT(proxy
),
1842 g_string_printf(name
, "virtio-pci-device-%s", vdev_name
);
1843 memory_region_init_io(&proxy
->device
.mr
, OBJECT(proxy
),
1847 proxy
->device
.size
);
1849 g_string_printf(name
, "virtio-pci-notify-%s", vdev_name
);
1850 memory_region_init_io(&proxy
->notify
.mr
, OBJECT(proxy
),
1854 proxy
->notify
.size
);
1856 g_string_printf(name
, "virtio-pci-notify-pio-%s", vdev_name
);
1857 memory_region_init_io(&proxy
->notify_pio
.mr
, OBJECT(proxy
),
1861 proxy
->notify_pio
.size
);
1864 static void virtio_pci_modern_region_map(VirtIOPCIProxy
*proxy
,
1865 VirtIOPCIRegion
*region
,
1866 struct virtio_pci_cap
*cap
,
1870 memory_region_add_subregion(mr
, region
->offset
, ®ion
->mr
);
1872 cap
->cfg_type
= region
->type
;
1874 cap
->offset
= cpu_to_le32(region
->offset
);
1875 cap
->length
= cpu_to_le32(region
->size
);
1876 virtio_pci_add_mem_cap(proxy
, cap
);
1880 static void virtio_pci_modern_mem_region_map(VirtIOPCIProxy
*proxy
,
1881 VirtIOPCIRegion
*region
,
1882 struct virtio_pci_cap
*cap
)
1884 virtio_pci_modern_region_map(proxy
, region
, cap
,
1885 &proxy
->modern_bar
, proxy
->modern_mem_bar_idx
);
1888 static void virtio_pci_modern_io_region_map(VirtIOPCIProxy
*proxy
,
1889 VirtIOPCIRegion
*region
,
1890 struct virtio_pci_cap
*cap
)
1892 virtio_pci_modern_region_map(proxy
, region
, cap
,
1893 &proxy
->io_bar
, proxy
->modern_io_bar_idx
);
1896 static void virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy
*proxy
,
1897 VirtIOPCIRegion
*region
)
1899 memory_region_del_subregion(&proxy
->modern_bar
,
1903 static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy
*proxy
,
1904 VirtIOPCIRegion
*region
)
1906 memory_region_del_subregion(&proxy
->io_bar
,
1910 static void virtio_pci_pre_plugged(DeviceState
*d
, Error
**errp
)
1912 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1913 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1915 if (virtio_pci_modern(proxy
)) {
1916 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_VERSION_1
);
1919 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_BAD_FEATURE
);
1922 /* This is called by virtio-bus just after the device is plugged. */
1923 static void virtio_pci_device_plugged(DeviceState
*d
, Error
**errp
)
1925 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1926 VirtioBusState
*bus
= &proxy
->bus
;
1927 bool legacy
= virtio_pci_legacy(proxy
);
1929 bool modern_pio
= proxy
->flags
& VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY
;
1932 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1935 * Virtio capabilities present without
1936 * VIRTIO_F_VERSION_1 confuses guests
1938 if (!proxy
->ignore_backend_features
&&
1939 !virtio_has_feature(vdev
->host_features
, VIRTIO_F_VERSION_1
)) {
1940 virtio_pci_disable_modern(proxy
);
1943 error_setg(errp
, "Device doesn't support modern mode, and legacy"
1944 " mode is disabled");
1945 error_append_hint(errp
, "Set disable-legacy to off\n");
1951 modern
= virtio_pci_modern(proxy
);
1953 config
= proxy
->pci_dev
.config
;
1954 if (proxy
->class_code
) {
1955 pci_config_set_class(config
, proxy
->class_code
);
1959 if (!virtio_legacy_allowed(vdev
)) {
1961 * To avoid migration issues, we allow legacy mode when legacy
1962 * check is disabled in the old machine types (< 5.1).
1964 if (virtio_legacy_check_disabled(vdev
)) {
1965 warn_report("device is modern-only, but for backward "
1966 "compatibility legacy is allowed");
1969 "device is modern-only, use disable-legacy=on");
1973 if (virtio_host_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
)) {
1974 error_setg(errp
, "VIRTIO_F_IOMMU_PLATFORM was supported by"
1975 " neither legacy nor transitional device");
1979 * Legacy and transitional devices use specific subsystem IDs.
1980 * Note that the subsystem vendor ID (config + PCI_SUBSYSTEM_VENDOR_ID)
1981 * is set to PCI_SUBVENDOR_ID_REDHAT_QUMRANET by default.
1983 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, virtio_bus_get_vdev_id(bus
));
1984 if (proxy
->trans_devid
) {
1985 pci_config_set_device_id(config
, proxy
->trans_devid
);
1988 /* pure virtio-1.0 */
1989 pci_set_word(config
+ PCI_VENDOR_ID
,
1990 PCI_VENDOR_ID_REDHAT_QUMRANET
);
1991 pci_set_word(config
+ PCI_DEVICE_ID
,
1992 PCI_DEVICE_ID_VIRTIO_10_BASE
+ virtio_bus_get_vdev_id(bus
));
1993 pci_config_set_revision(config
, 1);
1995 config
[PCI_INTERRUPT_PIN
] = 1;
1999 struct virtio_pci_cap cap
= {
2000 .cap_len
= sizeof cap
,
2002 struct virtio_pci_notify_cap notify
= {
2003 .cap
.cap_len
= sizeof notify
,
2004 .notify_off_multiplier
=
2005 cpu_to_le32(virtio_pci_queue_mem_mult(proxy
)),
2007 struct virtio_pci_cfg_cap cfg
= {
2008 .cap
.cap_len
= sizeof cfg
,
2009 .cap
.cfg_type
= VIRTIO_PCI_CAP_PCI_CFG
,
2011 struct virtio_pci_notify_cap notify_pio
= {
2012 .cap
.cap_len
= sizeof notify
,
2013 .notify_off_multiplier
= cpu_to_le32(0x0),
2016 struct virtio_pci_cfg_cap
*cfg_mask
;
2018 virtio_pci_modern_regions_init(proxy
, vdev
->name
);
2020 virtio_pci_modern_mem_region_map(proxy
, &proxy
->common
, &cap
);
2021 virtio_pci_modern_mem_region_map(proxy
, &proxy
->isr
, &cap
);
2022 virtio_pci_modern_mem_region_map(proxy
, &proxy
->device
, &cap
);
2023 virtio_pci_modern_mem_region_map(proxy
, &proxy
->notify
, ¬ify
.cap
);
2026 memory_region_init(&proxy
->io_bar
, OBJECT(proxy
),
2027 "virtio-pci-io", 0x4);
2029 pci_register_bar(&proxy
->pci_dev
, proxy
->modern_io_bar_idx
,
2030 PCI_BASE_ADDRESS_SPACE_IO
, &proxy
->io_bar
);
2032 virtio_pci_modern_io_region_map(proxy
, &proxy
->notify_pio
,
2036 pci_register_bar(&proxy
->pci_dev
, proxy
->modern_mem_bar_idx
,
2037 PCI_BASE_ADDRESS_SPACE_MEMORY
|
2038 PCI_BASE_ADDRESS_MEM_PREFETCH
|
2039 PCI_BASE_ADDRESS_MEM_TYPE_64
,
2040 &proxy
->modern_bar
);
2042 proxy
->config_cap
= virtio_pci_add_mem_cap(proxy
, &cfg
.cap
);
2043 cfg_mask
= (void *)(proxy
->pci_dev
.wmask
+ proxy
->config_cap
);
2044 pci_set_byte(&cfg_mask
->cap
.bar
, ~0x0);
2045 pci_set_long((uint8_t *)&cfg_mask
->cap
.offset
, ~0x0);
2046 pci_set_long((uint8_t *)&cfg_mask
->cap
.length
, ~0x0);
2047 pci_set_long(cfg_mask
->pci_cfg_data
, ~0x0);
2050 if (proxy
->nvectors
) {
2051 int err
= msix_init_exclusive_bar(&proxy
->pci_dev
, proxy
->nvectors
,
2052 proxy
->msix_bar_idx
, NULL
);
2054 /* Notice when a system that supports MSIx can't initialize it */
2055 if (err
!= -ENOTSUP
) {
2056 warn_report("unable to init msix vectors to %" PRIu32
,
2059 proxy
->nvectors
= 0;
2063 proxy
->pci_dev
.config_write
= virtio_write_config
;
2064 proxy
->pci_dev
.config_read
= virtio_read_config
;
2067 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
)
2068 + virtio_bus_get_vdev_config_len(bus
);
2069 size
= pow2ceil(size
);
2071 memory_region_init_io(&proxy
->bar
, OBJECT(proxy
),
2072 &virtio_pci_config_ops
,
2073 proxy
, "virtio-pci", size
);
2075 pci_register_bar(&proxy
->pci_dev
, proxy
->legacy_io_bar_idx
,
2076 PCI_BASE_ADDRESS_SPACE_IO
, &proxy
->bar
);
2080 static void virtio_pci_device_unplugged(DeviceState
*d
)
2082 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
2083 bool modern
= virtio_pci_modern(proxy
);
2084 bool modern_pio
= proxy
->flags
& VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY
;
2086 virtio_pci_stop_ioeventfd(proxy
);
2089 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->common
);
2090 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->isr
);
2091 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->device
);
2092 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->notify
);
2094 virtio_pci_modern_io_region_unmap(proxy
, &proxy
->notify_pio
);
2099 static void virtio_pci_realize(PCIDevice
*pci_dev
, Error
**errp
)
2101 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
2102 VirtioPCIClass
*k
= VIRTIO_PCI_GET_CLASS(pci_dev
);
2103 bool pcie_port
= pci_bus_is_express(pci_get_bus(pci_dev
)) &&
2104 !pci_bus_is_root(pci_get_bus(pci_dev
));
2106 /* fd-based ioevents can't be synchronized in record/replay */
2107 if (replay_mode
!= REPLAY_MODE_NONE
) {
2108 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
2112 * virtio pci bar layout used by default.
2113 * subclasses can re-arrange things if needed.
2115 * region 0 -- virtio legacy io bar
2116 * region 1 -- msi-x bar
2117 * region 2 -- virtio modern io bar (off by default)
2118 * region 4+5 -- virtio modern memory (64bit) bar
2121 proxy
->legacy_io_bar_idx
= 0;
2122 proxy
->msix_bar_idx
= 1;
2123 proxy
->modern_io_bar_idx
= 2;
2124 proxy
->modern_mem_bar_idx
= 4;
2126 proxy
->common
.offset
= 0x0;
2127 proxy
->common
.size
= 0x1000;
2128 proxy
->common
.type
= VIRTIO_PCI_CAP_COMMON_CFG
;
2130 proxy
->isr
.offset
= 0x1000;
2131 proxy
->isr
.size
= 0x1000;
2132 proxy
->isr
.type
= VIRTIO_PCI_CAP_ISR_CFG
;
2134 proxy
->device
.offset
= 0x2000;
2135 proxy
->device
.size
= 0x1000;
2136 proxy
->device
.type
= VIRTIO_PCI_CAP_DEVICE_CFG
;
2138 proxy
->notify
.offset
= 0x3000;
2139 proxy
->notify
.size
= virtio_pci_queue_mem_mult(proxy
) * VIRTIO_QUEUE_MAX
;
2140 proxy
->notify
.type
= VIRTIO_PCI_CAP_NOTIFY_CFG
;
2142 proxy
->notify_pio
.offset
= 0x0;
2143 proxy
->notify_pio
.size
= 0x4;
2144 proxy
->notify_pio
.type
= VIRTIO_PCI_CAP_NOTIFY_CFG
;
2146 /* subclasses can enforce modern, so do this unconditionally */
2147 memory_region_init(&proxy
->modern_bar
, OBJECT(proxy
), "virtio-pci",
2148 /* PCI BAR regions must be powers of 2 */
2149 pow2ceil(proxy
->notify
.offset
+ proxy
->notify
.size
));
2151 if (proxy
->disable_legacy
== ON_OFF_AUTO_AUTO
) {
2152 proxy
->disable_legacy
= pcie_port
? ON_OFF_AUTO_ON
: ON_OFF_AUTO_OFF
;
2155 if (!virtio_pci_modern(proxy
) && !virtio_pci_legacy(proxy
)) {
2156 error_setg(errp
, "device cannot work as neither modern nor legacy mode"
2158 error_append_hint(errp
, "Set either disable-modern or disable-legacy"
2163 if (pcie_port
&& pci_is_express(pci_dev
)) {
2165 uint16_t last_pcie_cap_offset
= PCI_CONFIG_SPACE_SIZE
;
2167 pos
= pcie_endpoint_cap_init(pci_dev
, 0);
2170 pos
= pci_add_capability(pci_dev
, PCI_CAP_ID_PM
, 0,
2171 PCI_PM_SIZEOF
, errp
);
2176 pci_dev
->exp
.pm_cap
= pos
;
2179 * Indicates that this function complies with revision 1.2 of the
2180 * PCI Power Management Interface Specification.
2182 pci_set_word(pci_dev
->config
+ pos
+ PCI_PM_PMC
, 0x3);
2184 if (proxy
->flags
& VIRTIO_PCI_FLAG_AER
) {
2185 pcie_aer_init(pci_dev
, PCI_ERR_VER
, last_pcie_cap_offset
,
2186 PCI_ERR_SIZEOF
, NULL
);
2187 last_pcie_cap_offset
+= PCI_ERR_SIZEOF
;
2190 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_DEVERR
) {
2191 /* Init error enabling flags */
2192 pcie_cap_deverr_init(pci_dev
);
2195 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_LNKCTL
) {
2196 /* Init Link Control Register */
2197 pcie_cap_lnkctl_init(pci_dev
);
2200 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_PM
) {
2201 /* Init Power Management Control Register */
2202 pci_set_word(pci_dev
->wmask
+ pos
+ PCI_PM_CTRL
,
2203 PCI_PM_CTRL_STATE_MASK
);
2206 if (proxy
->flags
& VIRTIO_PCI_FLAG_ATS
) {
2207 pcie_ats_init(pci_dev
, last_pcie_cap_offset
,
2208 proxy
->flags
& VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED
);
2209 last_pcie_cap_offset
+= PCI_EXT_CAP_ATS_SIZEOF
;
2212 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_FLR
) {
2213 /* Set Function Level Reset capability bit */
2214 pcie_cap_flr_init(pci_dev
);
2218 * make future invocations of pci_is_express() return false
2219 * and pci_config_size() return PCI_CONFIG_SPACE_SIZE.
2221 pci_dev
->cap_present
&= ~QEMU_PCI_CAP_EXPRESS
;
2224 virtio_pci_bus_new(&proxy
->bus
, sizeof(proxy
->bus
), proxy
);
2226 k
->realize(proxy
, errp
);
2230 static void virtio_pci_exit(PCIDevice
*pci_dev
)
2232 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
2233 bool pcie_port
= pci_bus_is_express(pci_get_bus(pci_dev
)) &&
2234 !pci_bus_is_root(pci_get_bus(pci_dev
));
2236 msix_uninit_exclusive_bar(pci_dev
);
2237 if (proxy
->flags
& VIRTIO_PCI_FLAG_AER
&& pcie_port
&&
2238 pci_is_express(pci_dev
)) {
2239 pcie_aer_exit(pci_dev
);
2243 static void virtio_pci_reset(DeviceState
*qdev
)
2245 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
2246 VirtioBusState
*bus
= VIRTIO_BUS(&proxy
->bus
);
2249 virtio_bus_reset(bus
);
2250 msix_unuse_all_vectors(&proxy
->pci_dev
);
2252 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2253 proxy
->vqs
[i
].enabled
= 0;
2254 proxy
->vqs
[i
].reset
= 0;
2255 proxy
->vqs
[i
].num
= 0;
2256 proxy
->vqs
[i
].desc
[0] = proxy
->vqs
[i
].desc
[1] = 0;
2257 proxy
->vqs
[i
].avail
[0] = proxy
->vqs
[i
].avail
[1] = 0;
2258 proxy
->vqs
[i
].used
[0] = proxy
->vqs
[i
].used
[1] = 0;
2262 static void virtio_pci_bus_reset_hold(Object
*obj
)
2264 PCIDevice
*dev
= PCI_DEVICE(obj
);
2265 DeviceState
*qdev
= DEVICE(obj
);
2267 virtio_pci_reset(qdev
);
2269 if (pci_is_express(dev
)) {
2270 pcie_cap_deverr_reset(dev
);
2271 pcie_cap_lnkctl_reset(dev
);
2273 pci_set_word(dev
->config
+ dev
->exp
.pm_cap
+ PCI_PM_CTRL
, 0);
2277 static Property virtio_pci_properties
[] = {
2278 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy
, flags
,
2279 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT
, false),
2280 DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy
, flags
,
2281 VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT
, true),
2282 DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy
, flags
,
2283 VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT
, false),
2284 DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy
, flags
,
2285 VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT
, false),
2286 DEFINE_PROP_BIT("page-per-vq", VirtIOPCIProxy
, flags
,
2287 VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT
, false),
2288 DEFINE_PROP_BOOL("x-ignore-backend-features", VirtIOPCIProxy
,
2289 ignore_backend_features
, false),
2290 DEFINE_PROP_BIT("ats", VirtIOPCIProxy
, flags
,
2291 VIRTIO_PCI_FLAG_ATS_BIT
, false),
2292 DEFINE_PROP_BIT("x-ats-page-aligned", VirtIOPCIProxy
, flags
,
2293 VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED_BIT
, true),
2294 DEFINE_PROP_BIT("x-pcie-deverr-init", VirtIOPCIProxy
, flags
,
2295 VIRTIO_PCI_FLAG_INIT_DEVERR_BIT
, true),
2296 DEFINE_PROP_BIT("x-pcie-lnkctl-init", VirtIOPCIProxy
, flags
,
2297 VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT
, true),
2298 DEFINE_PROP_BIT("x-pcie-pm-init", VirtIOPCIProxy
, flags
,
2299 VIRTIO_PCI_FLAG_INIT_PM_BIT
, true),
2300 DEFINE_PROP_BIT("x-pcie-flr-init", VirtIOPCIProxy
, flags
,
2301 VIRTIO_PCI_FLAG_INIT_FLR_BIT
, true),
2302 DEFINE_PROP_BIT("aer", VirtIOPCIProxy
, flags
,
2303 VIRTIO_PCI_FLAG_AER_BIT
, false),
2304 DEFINE_PROP_END_OF_LIST(),
2307 static void virtio_pci_dc_realize(DeviceState
*qdev
, Error
**errp
)
2309 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_GET_CLASS(qdev
);
2310 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
2311 PCIDevice
*pci_dev
= &proxy
->pci_dev
;
2313 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_PCIE
) &&
2314 virtio_pci_modern(proxy
)) {
2315 pci_dev
->cap_present
|= QEMU_PCI_CAP_EXPRESS
;
2318 vpciklass
->parent_dc_realize(qdev
, errp
);
2321 static void virtio_pci_class_init(ObjectClass
*klass
, void *data
)
2323 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2324 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2325 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_CLASS(klass
);
2326 ResettableClass
*rc
= RESETTABLE_CLASS(klass
);
2328 device_class_set_props(dc
, virtio_pci_properties
);
2329 k
->realize
= virtio_pci_realize
;
2330 k
->exit
= virtio_pci_exit
;
2331 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2332 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
2333 k
->class_id
= PCI_CLASS_OTHERS
;
2334 device_class_set_parent_realize(dc
, virtio_pci_dc_realize
,
2335 &vpciklass
->parent_dc_realize
);
2336 rc
->phases
.hold
= virtio_pci_bus_reset_hold
;
2339 static const TypeInfo virtio_pci_info
= {
2340 .name
= TYPE_VIRTIO_PCI
,
2341 .parent
= TYPE_PCI_DEVICE
,
2342 .instance_size
= sizeof(VirtIOPCIProxy
),
2343 .class_init
= virtio_pci_class_init
,
2344 .class_size
= sizeof(VirtioPCIClass
),
2348 static Property virtio_pci_generic_properties
[] = {
2349 DEFINE_PROP_ON_OFF_AUTO("disable-legacy", VirtIOPCIProxy
, disable_legacy
,
2351 DEFINE_PROP_BOOL("disable-modern", VirtIOPCIProxy
, disable_modern
, false),
2352 DEFINE_PROP_END_OF_LIST(),
2355 static void virtio_pci_base_class_init(ObjectClass
*klass
, void *data
)
2357 const VirtioPCIDeviceTypeInfo
*t
= data
;
2358 if (t
->class_init
) {
2359 t
->class_init(klass
, NULL
);
2363 static void virtio_pci_generic_class_init(ObjectClass
*klass
, void *data
)
2365 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2367 device_class_set_props(dc
, virtio_pci_generic_properties
);
2370 static void virtio_pci_transitional_instance_init(Object
*obj
)
2372 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(obj
);
2374 proxy
->disable_legacy
= ON_OFF_AUTO_OFF
;
2375 proxy
->disable_modern
= false;
2378 static void virtio_pci_non_transitional_instance_init(Object
*obj
)
2380 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(obj
);
2382 proxy
->disable_legacy
= ON_OFF_AUTO_ON
;
2383 proxy
->disable_modern
= false;
2386 void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo
*t
)
2388 char *base_name
= NULL
;
2389 TypeInfo base_type_info
= {
2390 .name
= t
->base_name
,
2391 .parent
= t
->parent
? t
->parent
: TYPE_VIRTIO_PCI
,
2392 .instance_size
= t
->instance_size
,
2393 .instance_init
= t
->instance_init
,
2394 .instance_finalize
= t
->instance_finalize
,
2395 .class_size
= t
->class_size
,
2397 .interfaces
= t
->interfaces
,
2399 TypeInfo generic_type_info
= {
2400 .name
= t
->generic_name
,
2401 .parent
= base_type_info
.name
,
2402 .class_init
= virtio_pci_generic_class_init
,
2403 .interfaces
= (InterfaceInfo
[]) {
2404 { INTERFACE_PCIE_DEVICE
},
2405 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
2410 if (!base_type_info
.name
) {
2411 /* No base type -> register a single generic device type */
2412 /* use intermediate %s-base-type to add generic device props */
2413 base_name
= g_strdup_printf("%s-base-type", t
->generic_name
);
2414 base_type_info
.name
= base_name
;
2415 base_type_info
.class_init
= virtio_pci_generic_class_init
;
2417 generic_type_info
.parent
= base_name
;
2418 generic_type_info
.class_init
= virtio_pci_base_class_init
;
2419 generic_type_info
.class_data
= (void *)t
;
2421 assert(!t
->non_transitional_name
);
2422 assert(!t
->transitional_name
);
2424 base_type_info
.class_init
= virtio_pci_base_class_init
;
2425 base_type_info
.class_data
= (void *)t
;
2428 type_register(&base_type_info
);
2429 if (generic_type_info
.name
) {
2430 type_register(&generic_type_info
);
2433 if (t
->non_transitional_name
) {
2434 const TypeInfo non_transitional_type_info
= {
2435 .name
= t
->non_transitional_name
,
2436 .parent
= base_type_info
.name
,
2437 .instance_init
= virtio_pci_non_transitional_instance_init
,
2438 .interfaces
= (InterfaceInfo
[]) {
2439 { INTERFACE_PCIE_DEVICE
},
2440 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
2444 type_register(&non_transitional_type_info
);
2447 if (t
->transitional_name
) {
2448 const TypeInfo transitional_type_info
= {
2449 .name
= t
->transitional_name
,
2450 .parent
= base_type_info
.name
,
2451 .instance_init
= virtio_pci_transitional_instance_init
,
2452 .interfaces
= (InterfaceInfo
[]) {
2454 * Transitional virtio devices work only as Conventional PCI
2455 * devices because they require PIO ports.
2457 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
2461 type_register(&transitional_type_info
);
2466 unsigned virtio_pci_optimal_num_queues(unsigned fixed_queues
)
2469 * 1:1 vq to vCPU mapping is ideal because the same vCPU that submitted
2470 * virtqueue buffers can handle their completion. When a different vCPU
2471 * handles completion it may need to IPI the vCPU that submitted the
2472 * request and this adds overhead.
2474 * Virtqueues consume guest RAM and MSI-X vectors. This is wasteful in
2475 * guests with very many vCPUs and a device that is only used by a few
2476 * vCPUs. Unfortunately optimizing that case requires manual pinning inside
2477 * the guest, so those users might as well manually set the number of
2478 * queues. There is no upper limit that can be applied automatically and
2479 * doing so arbitrarily would result in a sudden performance drop once the
2480 * threshold number of vCPUs is exceeded.
2482 unsigned num_queues
= current_machine
->smp
.cpus
;
2485 * The maximum number of MSI-X vectors is PCI_MSIX_FLAGS_QSIZE + 1, but the
2486 * config change interrupt and the fixed virtqueues must be taken into
2489 num_queues
= MIN(num_queues
, PCI_MSIX_FLAGS_QSIZE
- fixed_queues
);
2492 * There is a limit to how many virtqueues a device can have.
2494 return MIN(num_queues
, VIRTIO_QUEUE_MAX
- fixed_queues
);
2497 /* virtio-pci-bus */
2499 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
2500 VirtIOPCIProxy
*dev
)
2502 DeviceState
*qdev
= DEVICE(dev
);
2503 char virtio_bus_name
[] = "virtio-bus";
2505 qbus_init(bus
, bus_size
, TYPE_VIRTIO_PCI_BUS
, qdev
, virtio_bus_name
);
2508 static void virtio_pci_bus_class_init(ObjectClass
*klass
, void *data
)
2510 BusClass
*bus_class
= BUS_CLASS(klass
);
2511 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
2512 bus_class
->max_dev
= 1;
2513 k
->notify
= virtio_pci_notify
;
2514 k
->save_config
= virtio_pci_save_config
;
2515 k
->load_config
= virtio_pci_load_config
;
2516 k
->save_queue
= virtio_pci_save_queue
;
2517 k
->load_queue
= virtio_pci_load_queue
;
2518 k
->save_extra_state
= virtio_pci_save_extra_state
;
2519 k
->load_extra_state
= virtio_pci_load_extra_state
;
2520 k
->has_extra_state
= virtio_pci_has_extra_state
;
2521 k
->query_guest_notifiers
= virtio_pci_query_guest_notifiers
;
2522 k
->set_guest_notifiers
= virtio_pci_set_guest_notifiers
;
2523 k
->set_host_notifier_mr
= virtio_pci_set_host_notifier_mr
;
2524 k
->vmstate_change
= virtio_pci_vmstate_change
;
2525 k
->pre_plugged
= virtio_pci_pre_plugged
;
2526 k
->device_plugged
= virtio_pci_device_plugged
;
2527 k
->device_unplugged
= virtio_pci_device_unplugged
;
2528 k
->query_nvectors
= virtio_pci_query_nvectors
;
2529 k
->ioeventfd_enabled
= virtio_pci_ioeventfd_enabled
;
2530 k
->ioeventfd_assign
= virtio_pci_ioeventfd_assign
;
2531 k
->get_dma_as
= virtio_pci_get_dma_as
;
2532 k
->iommu_enabled
= virtio_pci_iommu_enabled
;
2533 k
->queue_enabled
= virtio_pci_queue_enabled
;
2536 static const TypeInfo virtio_pci_bus_info
= {
2537 .name
= TYPE_VIRTIO_PCI_BUS
,
2538 .parent
= TYPE_VIRTIO_BUS
,
2539 .instance_size
= sizeof(VirtioPCIBusState
),
2540 .class_size
= sizeof(VirtioPCIBusClass
),
2541 .class_init
= virtio_pci_bus_class_init
,
2544 static void virtio_pci_register_types(void)
2547 type_register_static(&virtio_pci_bus_info
);
2548 type_register_static(&virtio_pci_info
);
2551 type_init(virtio_pci_register_types
)