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/virtio/virtio.h"
23 #include "migration/qemu-file-types.h"
24 #include "hw/pci/pci.h"
25 #include "hw/pci/pci_bus.h"
26 #include "hw/qdev-properties.h"
27 #include "qapi/error.h"
28 #include "qemu/error-report.h"
29 #include "qemu/module.h"
30 #include "hw/pci/msi.h"
31 #include "hw/pci/msix.h"
32 #include "hw/loader.h"
33 #include "sysemu/kvm.h"
34 #include "virtio-pci.h"
35 #include "qemu/range.h"
36 #include "hw/virtio/virtio-bus.h"
37 #include "qapi/visitor.h"
39 #define VIRTIO_PCI_REGION_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_present(dev))
41 #undef VIRTIO_PCI_CONFIG
43 /* The remaining space is defined by each driver as the per-driver
44 * configuration space */
45 #define VIRTIO_PCI_CONFIG_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_enabled(dev))
47 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
49 static void virtio_pci_reset(DeviceState
*qdev
);
52 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
53 static inline VirtIOPCIProxy
*to_virtio_pci_proxy(DeviceState
*d
)
55 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
58 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
59 * be careful and test performance if you change this.
61 static inline VirtIOPCIProxy
*to_virtio_pci_proxy_fast(DeviceState
*d
)
63 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
66 static void virtio_pci_notify(DeviceState
*d
, uint16_t vector
)
68 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy_fast(d
);
70 if (msix_enabled(&proxy
->pci_dev
))
71 msix_notify(&proxy
->pci_dev
, vector
);
73 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
74 pci_set_irq(&proxy
->pci_dev
, atomic_read(&vdev
->isr
) & 1);
78 static void virtio_pci_save_config(DeviceState
*d
, QEMUFile
*f
)
80 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
81 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
83 pci_device_save(&proxy
->pci_dev
, f
);
84 msix_save(&proxy
->pci_dev
, f
);
85 if (msix_present(&proxy
->pci_dev
))
86 qemu_put_be16(f
, vdev
->config_vector
);
89 static const VMStateDescription vmstate_virtio_pci_modern_queue_state
= {
90 .name
= "virtio_pci/modern_queue_state",
92 .minimum_version_id
= 1,
93 .fields
= (VMStateField
[]) {
94 VMSTATE_UINT16(num
, VirtIOPCIQueue
),
95 VMSTATE_UNUSED(1), /* enabled was stored as be16 */
96 VMSTATE_BOOL(enabled
, VirtIOPCIQueue
),
97 VMSTATE_UINT32_ARRAY(desc
, VirtIOPCIQueue
, 2),
98 VMSTATE_UINT32_ARRAY(avail
, VirtIOPCIQueue
, 2),
99 VMSTATE_UINT32_ARRAY(used
, VirtIOPCIQueue
, 2),
100 VMSTATE_END_OF_LIST()
104 static bool virtio_pci_modern_state_needed(void *opaque
)
106 VirtIOPCIProxy
*proxy
= opaque
;
108 return virtio_pci_modern(proxy
);
111 static const VMStateDescription vmstate_virtio_pci_modern_state_sub
= {
112 .name
= "virtio_pci/modern_state",
114 .minimum_version_id
= 1,
115 .needed
= &virtio_pci_modern_state_needed
,
116 .fields
= (VMStateField
[]) {
117 VMSTATE_UINT32(dfselect
, VirtIOPCIProxy
),
118 VMSTATE_UINT32(gfselect
, VirtIOPCIProxy
),
119 VMSTATE_UINT32_ARRAY(guest_features
, VirtIOPCIProxy
, 2),
120 VMSTATE_STRUCT_ARRAY(vqs
, VirtIOPCIProxy
, VIRTIO_QUEUE_MAX
, 0,
121 vmstate_virtio_pci_modern_queue_state
,
123 VMSTATE_END_OF_LIST()
127 static const VMStateDescription vmstate_virtio_pci
= {
128 .name
= "virtio_pci",
130 .minimum_version_id
= 1,
131 .minimum_version_id_old
= 1,
132 .fields
= (VMStateField
[]) {
133 VMSTATE_END_OF_LIST()
135 .subsections
= (const VMStateDescription
*[]) {
136 &vmstate_virtio_pci_modern_state_sub
,
141 static bool virtio_pci_has_extra_state(DeviceState
*d
)
143 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
145 return proxy
->flags
& VIRTIO_PCI_FLAG_MIGRATE_EXTRA
;
148 static void virtio_pci_save_extra_state(DeviceState
*d
, QEMUFile
*f
)
150 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
152 vmstate_save_state(f
, &vmstate_virtio_pci
, proxy
, NULL
);
155 static int virtio_pci_load_extra_state(DeviceState
*d
, QEMUFile
*f
)
157 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
159 return vmstate_load_state(f
, &vmstate_virtio_pci
, proxy
, 1);
162 static void virtio_pci_save_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
164 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
165 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
167 if (msix_present(&proxy
->pci_dev
))
168 qemu_put_be16(f
, virtio_queue_vector(vdev
, n
));
171 static int virtio_pci_load_config(DeviceState
*d
, QEMUFile
*f
)
173 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
174 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
177 ret
= pci_device_load(&proxy
->pci_dev
, f
);
181 msix_unuse_all_vectors(&proxy
->pci_dev
);
182 msix_load(&proxy
->pci_dev
, f
);
183 if (msix_present(&proxy
->pci_dev
)) {
184 qemu_get_be16s(f
, &vdev
->config_vector
);
186 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
188 if (vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
189 return msix_vector_use(&proxy
->pci_dev
, vdev
->config_vector
);
194 static int virtio_pci_load_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
196 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
197 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
200 if (msix_present(&proxy
->pci_dev
)) {
201 qemu_get_be16s(f
, &vector
);
203 vector
= VIRTIO_NO_VECTOR
;
205 virtio_queue_set_vector(vdev
, n
, vector
);
206 if (vector
!= VIRTIO_NO_VECTOR
) {
207 return msix_vector_use(&proxy
->pci_dev
, vector
);
213 static bool virtio_pci_ioeventfd_enabled(DeviceState
*d
)
215 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
217 return (proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) != 0;
220 #define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000
222 static inline int virtio_pci_queue_mem_mult(struct VirtIOPCIProxy
*proxy
)
224 return (proxy
->flags
& VIRTIO_PCI_FLAG_PAGE_PER_VQ
) ?
225 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
: 4;
228 static int virtio_pci_ioeventfd_assign(DeviceState
*d
, EventNotifier
*notifier
,
231 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
232 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
233 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
234 bool legacy
= virtio_pci_legacy(proxy
);
235 bool modern
= virtio_pci_modern(proxy
);
236 bool fast_mmio
= kvm_ioeventfd_any_length_enabled();
237 bool modern_pio
= proxy
->flags
& VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY
;
238 MemoryRegion
*modern_mr
= &proxy
->notify
.mr
;
239 MemoryRegion
*modern_notify_mr
= &proxy
->notify_pio
.mr
;
240 MemoryRegion
*legacy_mr
= &proxy
->bar
;
241 hwaddr modern_addr
= virtio_pci_queue_mem_mult(proxy
) *
242 virtio_get_queue_index(vq
);
243 hwaddr legacy_addr
= VIRTIO_PCI_QUEUE_NOTIFY
;
248 memory_region_add_eventfd(modern_mr
, modern_addr
, 0,
251 memory_region_add_eventfd(modern_mr
, modern_addr
, 2,
255 memory_region_add_eventfd(modern_notify_mr
, 0, 2,
260 memory_region_add_eventfd(legacy_mr
, legacy_addr
, 2,
266 memory_region_del_eventfd(modern_mr
, modern_addr
, 0,
269 memory_region_del_eventfd(modern_mr
, modern_addr
, 2,
273 memory_region_del_eventfd(modern_notify_mr
, 0, 2,
278 memory_region_del_eventfd(legacy_mr
, legacy_addr
, 2,
285 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
287 virtio_bus_start_ioeventfd(&proxy
->bus
);
290 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
292 virtio_bus_stop_ioeventfd(&proxy
->bus
);
295 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
297 VirtIOPCIProxy
*proxy
= opaque
;
298 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
302 case VIRTIO_PCI_GUEST_FEATURES
:
303 /* Guest does not negotiate properly? We have to assume nothing. */
304 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
305 val
= virtio_bus_get_vdev_bad_features(&proxy
->bus
);
307 virtio_set_features(vdev
, val
);
309 case VIRTIO_PCI_QUEUE_PFN
:
310 pa
= (hwaddr
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
312 virtio_pci_reset(DEVICE(proxy
));
315 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
317 case VIRTIO_PCI_QUEUE_SEL
:
318 if (val
< VIRTIO_QUEUE_MAX
)
319 vdev
->queue_sel
= val
;
321 case VIRTIO_PCI_QUEUE_NOTIFY
:
322 if (val
< VIRTIO_QUEUE_MAX
) {
323 virtio_queue_notify(vdev
, val
);
326 case VIRTIO_PCI_STATUS
:
327 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
328 virtio_pci_stop_ioeventfd(proxy
);
331 virtio_set_status(vdev
, val
& 0xFF);
333 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
334 virtio_pci_start_ioeventfd(proxy
);
337 if (vdev
->status
== 0) {
338 virtio_pci_reset(DEVICE(proxy
));
341 /* Linux before 2.6.34 drives the device without enabling
342 the PCI device bus master bit. Enable it automatically
343 for the guest. This is a PCI spec violation but so is
344 initiating DMA with bus master bit clear. */
345 if (val
== (VIRTIO_CONFIG_S_ACKNOWLEDGE
| VIRTIO_CONFIG_S_DRIVER
)) {
346 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
347 proxy
->pci_dev
.config
[PCI_COMMAND
] |
348 PCI_COMMAND_MASTER
, 1);
351 case VIRTIO_MSI_CONFIG_VECTOR
:
352 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
353 /* Make it possible for guest to discover an error took place. */
354 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
355 val
= VIRTIO_NO_VECTOR
;
356 vdev
->config_vector
= val
;
358 case VIRTIO_MSI_QUEUE_VECTOR
:
359 msix_vector_unuse(&proxy
->pci_dev
,
360 virtio_queue_vector(vdev
, vdev
->queue_sel
));
361 /* Make it possible for guest to discover an error took place. */
362 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
363 val
= VIRTIO_NO_VECTOR
;
364 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
367 error_report("%s: unexpected address 0x%x value 0x%x",
368 __func__
, addr
, val
);
373 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
375 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
376 uint32_t ret
= 0xFFFFFFFF;
379 case VIRTIO_PCI_HOST_FEATURES
:
380 ret
= vdev
->host_features
;
382 case VIRTIO_PCI_GUEST_FEATURES
:
383 ret
= vdev
->guest_features
;
385 case VIRTIO_PCI_QUEUE_PFN
:
386 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
387 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
389 case VIRTIO_PCI_QUEUE_NUM
:
390 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
392 case VIRTIO_PCI_QUEUE_SEL
:
393 ret
= vdev
->queue_sel
;
395 case VIRTIO_PCI_STATUS
:
399 /* reading from the ISR also clears it. */
400 ret
= atomic_xchg(&vdev
->isr
, 0);
401 pci_irq_deassert(&proxy
->pci_dev
);
403 case VIRTIO_MSI_CONFIG_VECTOR
:
404 ret
= vdev
->config_vector
;
406 case VIRTIO_MSI_QUEUE_VECTOR
:
407 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
416 static uint64_t virtio_pci_config_read(void *opaque
, hwaddr addr
,
419 VirtIOPCIProxy
*proxy
= opaque
;
420 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
421 uint32_t config
= VIRTIO_PCI_CONFIG_SIZE(&proxy
->pci_dev
);
424 return virtio_ioport_read(proxy
, addr
);
430 val
= virtio_config_readb(vdev
, addr
);
433 val
= virtio_config_readw(vdev
, addr
);
434 if (virtio_is_big_endian(vdev
)) {
439 val
= virtio_config_readl(vdev
, addr
);
440 if (virtio_is_big_endian(vdev
)) {
448 static void virtio_pci_config_write(void *opaque
, hwaddr addr
,
449 uint64_t val
, unsigned size
)
451 VirtIOPCIProxy
*proxy
= opaque
;
452 uint32_t config
= VIRTIO_PCI_CONFIG_SIZE(&proxy
->pci_dev
);
453 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
455 virtio_ioport_write(proxy
, addr
, val
);
460 * Virtio-PCI is odd. Ioports are LE but config space is target native
465 virtio_config_writeb(vdev
, addr
, val
);
468 if (virtio_is_big_endian(vdev
)) {
471 virtio_config_writew(vdev
, addr
, val
);
474 if (virtio_is_big_endian(vdev
)) {
477 virtio_config_writel(vdev
, addr
, val
);
482 static const MemoryRegionOps virtio_pci_config_ops
= {
483 .read
= virtio_pci_config_read
,
484 .write
= virtio_pci_config_write
,
486 .min_access_size
= 1,
487 .max_access_size
= 4,
489 .endianness
= DEVICE_LITTLE_ENDIAN
,
492 static MemoryRegion
*virtio_address_space_lookup(VirtIOPCIProxy
*proxy
,
493 hwaddr
*off
, int len
)
496 VirtIOPCIRegion
*reg
;
498 for (i
= 0; i
< ARRAY_SIZE(proxy
->regs
); ++i
) {
499 reg
= &proxy
->regs
[i
];
500 if (*off
>= reg
->offset
&&
501 *off
+ len
<= reg
->offset
+ reg
->size
) {
510 /* Below are generic functions to do memcpy from/to an address space,
511 * without byteswaps, with input validation.
513 * As regular address_space_* APIs all do some kind of byteswap at least for
514 * some host/target combinations, we are forced to explicitly convert to a
515 * known-endianness integer value.
516 * It doesn't really matter which endian format to go through, so the code
517 * below selects the endian that causes the least amount of work on the given
520 * Note: host pointer must be aligned.
523 void virtio_address_space_write(VirtIOPCIProxy
*proxy
, hwaddr addr
,
524 const uint8_t *buf
, int len
)
529 /* address_space_* APIs assume an aligned address.
530 * As address is under guest control, handle illegal values.
534 mr
= virtio_address_space_lookup(proxy
, &addr
, len
);
539 /* Make sure caller aligned buf properly */
540 assert(!(((uintptr_t)buf
) & (len
- 1)));
544 val
= pci_get_byte(buf
);
547 val
= pci_get_word(buf
);
550 val
= pci_get_long(buf
);
553 /* As length is under guest control, handle illegal values. */
556 memory_region_dispatch_write(mr
, addr
, val
, size_memop(len
) | MO_LE
,
557 MEMTXATTRS_UNSPECIFIED
);
561 virtio_address_space_read(VirtIOPCIProxy
*proxy
, hwaddr addr
,
562 uint8_t *buf
, int len
)
567 /* address_space_* APIs assume an aligned address.
568 * As address is under guest control, handle illegal values.
572 mr
= virtio_address_space_lookup(proxy
, &addr
, len
);
577 /* Make sure caller aligned buf properly */
578 assert(!(((uintptr_t)buf
) & (len
- 1)));
580 memory_region_dispatch_read(mr
, addr
, &val
, size_memop(len
) | MO_LE
,
581 MEMTXATTRS_UNSPECIFIED
);
584 pci_set_byte(buf
, val
);
587 pci_set_word(buf
, val
);
590 pci_set_long(buf
, val
);
593 /* As length is under guest control, handle illegal values. */
598 static void virtio_write_config(PCIDevice
*pci_dev
, uint32_t address
,
599 uint32_t val
, int len
)
601 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
602 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
603 struct virtio_pci_cfg_cap
*cfg
;
605 pci_default_write_config(pci_dev
, address
, val
, len
);
607 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_FLR
) {
608 pcie_cap_flr_write_config(pci_dev
, address
, val
, len
);
611 if (range_covers_byte(address
, len
, PCI_COMMAND
) &&
612 !(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
613 virtio_pci_stop_ioeventfd(proxy
);
614 virtio_set_status(vdev
, vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
617 if (proxy
->config_cap
&&
618 ranges_overlap(address
, len
, proxy
->config_cap
+ offsetof(struct virtio_pci_cfg_cap
,
620 sizeof cfg
->pci_cfg_data
)) {
624 cfg
= (void *)(proxy
->pci_dev
.config
+ proxy
->config_cap
);
625 off
= le32_to_cpu(cfg
->cap
.offset
);
626 len
= le32_to_cpu(cfg
->cap
.length
);
628 if (len
== 1 || len
== 2 || len
== 4) {
629 assert(len
<= sizeof cfg
->pci_cfg_data
);
630 virtio_address_space_write(proxy
, off
, cfg
->pci_cfg_data
, len
);
635 static uint32_t virtio_read_config(PCIDevice
*pci_dev
,
636 uint32_t address
, int len
)
638 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
639 struct virtio_pci_cfg_cap
*cfg
;
641 if (proxy
->config_cap
&&
642 ranges_overlap(address
, len
, proxy
->config_cap
+ offsetof(struct virtio_pci_cfg_cap
,
644 sizeof cfg
->pci_cfg_data
)) {
648 cfg
= (void *)(proxy
->pci_dev
.config
+ proxy
->config_cap
);
649 off
= le32_to_cpu(cfg
->cap
.offset
);
650 len
= le32_to_cpu(cfg
->cap
.length
);
652 if (len
== 1 || len
== 2 || len
== 4) {
653 assert(len
<= sizeof cfg
->pci_cfg_data
);
654 virtio_address_space_read(proxy
, off
, cfg
->pci_cfg_data
, len
);
658 return pci_default_read_config(pci_dev
, address
, len
);
661 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy
*proxy
,
662 unsigned int queue_no
,
665 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
668 if (irqfd
->users
== 0) {
669 ret
= kvm_irqchip_add_msi_route(kvm_state
, vector
, &proxy
->pci_dev
);
679 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy
*proxy
,
682 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
683 if (--irqfd
->users
== 0) {
684 kvm_irqchip_release_virq(kvm_state
, irqfd
->virq
);
688 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy
*proxy
,
689 unsigned int queue_no
,
692 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
693 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
694 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
695 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
696 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, n
, NULL
, irqfd
->virq
);
699 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy
*proxy
,
700 unsigned int queue_no
,
703 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
704 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
705 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
706 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
709 ret
= kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state
, n
, irqfd
->virq
);
713 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy
*proxy
, int nvqs
)
715 PCIDevice
*dev
= &proxy
->pci_dev
;
716 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
717 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
721 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
722 if (!virtio_queue_get_num(vdev
, queue_no
)) {
725 vector
= virtio_queue_vector(vdev
, queue_no
);
726 if (vector
>= msix_nr_vectors_allocated(dev
)) {
729 ret
= kvm_virtio_pci_vq_vector_use(proxy
, queue_no
, vector
);
733 /* If guest supports masking, set up irqfd now.
734 * Otherwise, delay until unmasked in the frontend.
736 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
737 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
739 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
747 while (--queue_no
>= 0) {
748 vector
= virtio_queue_vector(vdev
, queue_no
);
749 if (vector
>= msix_nr_vectors_allocated(dev
)) {
752 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
753 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
755 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
760 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy
*proxy
, int nvqs
)
762 PCIDevice
*dev
= &proxy
->pci_dev
;
763 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
766 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
768 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
769 if (!virtio_queue_get_num(vdev
, queue_no
)) {
772 vector
= virtio_queue_vector(vdev
, queue_no
);
773 if (vector
>= msix_nr_vectors_allocated(dev
)) {
776 /* If guest supports masking, clean up irqfd now.
777 * Otherwise, it was cleaned when masked in the frontend.
779 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
780 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
782 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
786 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy
*proxy
,
787 unsigned int queue_no
,
791 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
792 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
793 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
794 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
798 if (proxy
->vector_irqfd
) {
799 irqfd
= &proxy
->vector_irqfd
[vector
];
800 if (irqfd
->msg
.data
!= msg
.data
|| irqfd
->msg
.address
!= msg
.address
) {
801 ret
= kvm_irqchip_update_msi_route(kvm_state
, irqfd
->virq
, msg
,
806 kvm_irqchip_commit_routes(kvm_state
);
810 /* If guest supports masking, irqfd is already setup, unmask it.
811 * Otherwise, set it up now.
813 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
814 k
->guest_notifier_mask(vdev
, queue_no
, false);
815 /* Test after unmasking to avoid losing events. */
816 if (k
->guest_notifier_pending
&&
817 k
->guest_notifier_pending(vdev
, queue_no
)) {
818 event_notifier_set(n
);
821 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
826 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy
*proxy
,
827 unsigned int queue_no
,
830 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
831 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
833 /* If guest supports masking, keep irqfd but mask it.
834 * Otherwise, clean it up now.
836 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
837 k
->guest_notifier_mask(vdev
, queue_no
, true);
839 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
843 static int virtio_pci_vector_unmask(PCIDevice
*dev
, unsigned vector
,
846 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
847 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
848 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
849 int ret
, index
, unmasked
= 0;
852 index
= virtio_get_queue_index(vq
);
853 if (!virtio_queue_get_num(vdev
, index
)) {
856 if (index
< proxy
->nvqs_with_notifiers
) {
857 ret
= virtio_pci_vq_vector_unmask(proxy
, index
, vector
, msg
);
863 vq
= virtio_vector_next_queue(vq
);
869 vq
= virtio_vector_first_queue(vdev
, vector
);
870 while (vq
&& unmasked
>= 0) {
871 index
= virtio_get_queue_index(vq
);
872 if (index
< proxy
->nvqs_with_notifiers
) {
873 virtio_pci_vq_vector_mask(proxy
, index
, vector
);
876 vq
= virtio_vector_next_queue(vq
);
881 static void virtio_pci_vector_mask(PCIDevice
*dev
, unsigned vector
)
883 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
884 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
885 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
889 index
= virtio_get_queue_index(vq
);
890 if (!virtio_queue_get_num(vdev
, index
)) {
893 if (index
< proxy
->nvqs_with_notifiers
) {
894 virtio_pci_vq_vector_mask(proxy
, index
, vector
);
896 vq
= virtio_vector_next_queue(vq
);
900 static void virtio_pci_vector_poll(PCIDevice
*dev
,
901 unsigned int vector_start
,
902 unsigned int vector_end
)
904 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
905 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
906 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
909 EventNotifier
*notifier
;
912 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
913 if (!virtio_queue_get_num(vdev
, queue_no
)) {
916 vector
= virtio_queue_vector(vdev
, queue_no
);
917 if (vector
< vector_start
|| vector
>= vector_end
||
918 !msix_is_masked(dev
, vector
)) {
921 vq
= virtio_get_queue(vdev
, queue_no
);
922 notifier
= virtio_queue_get_guest_notifier(vq
);
923 if (k
->guest_notifier_pending
) {
924 if (k
->guest_notifier_pending(vdev
, queue_no
)) {
925 msix_set_pending(dev
, vector
);
927 } else if (event_notifier_test_and_clear(notifier
)) {
928 msix_set_pending(dev
, vector
);
933 static int virtio_pci_set_guest_notifier(DeviceState
*d
, int n
, bool assign
,
936 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
937 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
938 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
939 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
940 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
943 int r
= event_notifier_init(notifier
, 0);
947 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
949 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
950 event_notifier_cleanup(notifier
);
953 if (!msix_enabled(&proxy
->pci_dev
) &&
954 vdev
->use_guest_notifier_mask
&&
955 vdc
->guest_notifier_mask
) {
956 vdc
->guest_notifier_mask(vdev
, n
, !assign
);
962 static bool virtio_pci_query_guest_notifiers(DeviceState
*d
)
964 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
965 return msix_enabled(&proxy
->pci_dev
);
968 static int virtio_pci_set_guest_notifiers(DeviceState
*d
, int nvqs
, bool assign
)
970 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
971 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
972 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
974 bool with_irqfd
= msix_enabled(&proxy
->pci_dev
) &&
975 kvm_msi_via_irqfd_enabled();
977 nvqs
= MIN(nvqs
, VIRTIO_QUEUE_MAX
);
979 /* When deassigning, pass a consistent nvqs value
980 * to avoid leaking notifiers.
982 assert(assign
|| nvqs
== proxy
->nvqs_with_notifiers
);
984 proxy
->nvqs_with_notifiers
= nvqs
;
986 /* Must unset vector notifier while guest notifier is still assigned */
987 if ((proxy
->vector_irqfd
|| k
->guest_notifier_mask
) && !assign
) {
988 msix_unset_vector_notifiers(&proxy
->pci_dev
);
989 if (proxy
->vector_irqfd
) {
990 kvm_virtio_pci_vector_release(proxy
, nvqs
);
991 g_free(proxy
->vector_irqfd
);
992 proxy
->vector_irqfd
= NULL
;
996 for (n
= 0; n
< nvqs
; n
++) {
997 if (!virtio_queue_get_num(vdev
, n
)) {
1001 r
= virtio_pci_set_guest_notifier(d
, n
, assign
, with_irqfd
);
1007 /* Must set vector notifier after guest notifier has been assigned */
1008 if ((with_irqfd
|| k
->guest_notifier_mask
) && assign
) {
1010 proxy
->vector_irqfd
=
1011 g_malloc0(sizeof(*proxy
->vector_irqfd
) *
1012 msix_nr_vectors_allocated(&proxy
->pci_dev
));
1013 r
= kvm_virtio_pci_vector_use(proxy
, nvqs
);
1018 r
= msix_set_vector_notifiers(&proxy
->pci_dev
,
1019 virtio_pci_vector_unmask
,
1020 virtio_pci_vector_mask
,
1021 virtio_pci_vector_poll
);
1023 goto notifiers_error
;
1032 kvm_virtio_pci_vector_release(proxy
, nvqs
);
1036 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
1039 virtio_pci_set_guest_notifier(d
, n
, !assign
, with_irqfd
);
1044 static int virtio_pci_set_host_notifier_mr(DeviceState
*d
, int n
,
1045 MemoryRegion
*mr
, bool assign
)
1047 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1050 if (n
>= VIRTIO_QUEUE_MAX
|| !virtio_pci_modern(proxy
) ||
1051 virtio_pci_queue_mem_mult(proxy
) != memory_region_size(mr
)) {
1056 offset
= virtio_pci_queue_mem_mult(proxy
) * n
;
1057 memory_region_add_subregion_overlap(&proxy
->notify
.mr
, offset
, mr
, 1);
1059 memory_region_del_subregion(&proxy
->notify
.mr
, mr
);
1065 static void virtio_pci_vmstate_change(DeviceState
*d
, bool running
)
1067 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1068 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1071 /* Old QEMU versions did not set bus master enable on status write.
1072 * Detect DRIVER set and enable it.
1074 if ((proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION
) &&
1075 (vdev
->status
& VIRTIO_CONFIG_S_DRIVER
) &&
1076 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
1077 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
1078 proxy
->pci_dev
.config
[PCI_COMMAND
] |
1079 PCI_COMMAND_MASTER
, 1);
1081 virtio_pci_start_ioeventfd(proxy
);
1083 virtio_pci_stop_ioeventfd(proxy
);
1088 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
1091 static int virtio_pci_query_nvectors(DeviceState
*d
)
1093 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1095 return proxy
->nvectors
;
1098 static AddressSpace
*virtio_pci_get_dma_as(DeviceState
*d
)
1100 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1101 PCIDevice
*dev
= &proxy
->pci_dev
;
1103 return pci_get_address_space(dev
);
1106 static int virtio_pci_add_mem_cap(VirtIOPCIProxy
*proxy
,
1107 struct virtio_pci_cap
*cap
)
1109 PCIDevice
*dev
= &proxy
->pci_dev
;
1112 offset
= pci_add_capability(dev
, PCI_CAP_ID_VNDR
, 0,
1113 cap
->cap_len
, &error_abort
);
1115 assert(cap
->cap_len
>= sizeof *cap
);
1116 memcpy(dev
->config
+ offset
+ PCI_CAP_FLAGS
, &cap
->cap_len
,
1117 cap
->cap_len
- PCI_CAP_FLAGS
);
1122 static uint64_t virtio_pci_common_read(void *opaque
, hwaddr addr
,
1125 VirtIOPCIProxy
*proxy
= opaque
;
1126 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1131 case VIRTIO_PCI_COMMON_DFSELECT
:
1132 val
= proxy
->dfselect
;
1134 case VIRTIO_PCI_COMMON_DF
:
1135 if (proxy
->dfselect
<= 1) {
1136 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1138 val
= (vdev
->host_features
& ~vdc
->legacy_features
) >>
1139 (32 * proxy
->dfselect
);
1142 case VIRTIO_PCI_COMMON_GFSELECT
:
1143 val
= proxy
->gfselect
;
1145 case VIRTIO_PCI_COMMON_GF
:
1146 if (proxy
->gfselect
< ARRAY_SIZE(proxy
->guest_features
)) {
1147 val
= proxy
->guest_features
[proxy
->gfselect
];
1150 case VIRTIO_PCI_COMMON_MSIX
:
1151 val
= vdev
->config_vector
;
1153 case VIRTIO_PCI_COMMON_NUMQ
:
1154 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; ++i
) {
1155 if (virtio_queue_get_num(vdev
, i
)) {
1160 case VIRTIO_PCI_COMMON_STATUS
:
1163 case VIRTIO_PCI_COMMON_CFGGENERATION
:
1164 val
= vdev
->generation
;
1166 case VIRTIO_PCI_COMMON_Q_SELECT
:
1167 val
= vdev
->queue_sel
;
1169 case VIRTIO_PCI_COMMON_Q_SIZE
:
1170 val
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
1172 case VIRTIO_PCI_COMMON_Q_MSIX
:
1173 val
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
1175 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1176 val
= proxy
->vqs
[vdev
->queue_sel
].enabled
;
1178 case VIRTIO_PCI_COMMON_Q_NOFF
:
1179 /* Simply map queues in order */
1180 val
= vdev
->queue_sel
;
1182 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1183 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[0];
1185 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1186 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[1];
1188 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1189 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[0];
1191 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1192 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[1];
1194 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1195 val
= proxy
->vqs
[vdev
->queue_sel
].used
[0];
1197 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1198 val
= proxy
->vqs
[vdev
->queue_sel
].used
[1];
1207 static void virtio_pci_common_write(void *opaque
, hwaddr addr
,
1208 uint64_t val
, unsigned size
)
1210 VirtIOPCIProxy
*proxy
= opaque
;
1211 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1214 case VIRTIO_PCI_COMMON_DFSELECT
:
1215 proxy
->dfselect
= val
;
1217 case VIRTIO_PCI_COMMON_GFSELECT
:
1218 proxy
->gfselect
= val
;
1220 case VIRTIO_PCI_COMMON_GF
:
1221 if (proxy
->gfselect
< ARRAY_SIZE(proxy
->guest_features
)) {
1222 proxy
->guest_features
[proxy
->gfselect
] = val
;
1223 virtio_set_features(vdev
,
1224 (((uint64_t)proxy
->guest_features
[1]) << 32) |
1225 proxy
->guest_features
[0]);
1228 case VIRTIO_PCI_COMMON_MSIX
:
1229 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
1230 /* Make it possible for guest to discover an error took place. */
1231 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0) {
1232 val
= VIRTIO_NO_VECTOR
;
1234 vdev
->config_vector
= val
;
1236 case VIRTIO_PCI_COMMON_STATUS
:
1237 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
1238 virtio_pci_stop_ioeventfd(proxy
);
1241 virtio_set_status(vdev
, val
& 0xFF);
1243 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
1244 virtio_pci_start_ioeventfd(proxy
);
1247 if (vdev
->status
== 0) {
1248 virtio_pci_reset(DEVICE(proxy
));
1252 case VIRTIO_PCI_COMMON_Q_SELECT
:
1253 if (val
< VIRTIO_QUEUE_MAX
) {
1254 vdev
->queue_sel
= val
;
1257 case VIRTIO_PCI_COMMON_Q_SIZE
:
1258 proxy
->vqs
[vdev
->queue_sel
].num
= val
;
1260 case VIRTIO_PCI_COMMON_Q_MSIX
:
1261 msix_vector_unuse(&proxy
->pci_dev
,
1262 virtio_queue_vector(vdev
, vdev
->queue_sel
));
1263 /* Make it possible for guest to discover an error took place. */
1264 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0) {
1265 val
= VIRTIO_NO_VECTOR
;
1267 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
1269 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1270 virtio_queue_set_num(vdev
, vdev
->queue_sel
,
1271 proxy
->vqs
[vdev
->queue_sel
].num
);
1272 virtio_queue_set_rings(vdev
, vdev
->queue_sel
,
1273 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].desc
[1]) << 32 |
1274 proxy
->vqs
[vdev
->queue_sel
].desc
[0],
1275 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].avail
[1]) << 32 |
1276 proxy
->vqs
[vdev
->queue_sel
].avail
[0],
1277 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].used
[1]) << 32 |
1278 proxy
->vqs
[vdev
->queue_sel
].used
[0]);
1279 proxy
->vqs
[vdev
->queue_sel
].enabled
= 1;
1281 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1282 proxy
->vqs
[vdev
->queue_sel
].desc
[0] = val
;
1284 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1285 proxy
->vqs
[vdev
->queue_sel
].desc
[1] = val
;
1287 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1288 proxy
->vqs
[vdev
->queue_sel
].avail
[0] = val
;
1290 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1291 proxy
->vqs
[vdev
->queue_sel
].avail
[1] = val
;
1293 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1294 proxy
->vqs
[vdev
->queue_sel
].used
[0] = val
;
1296 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1297 proxy
->vqs
[vdev
->queue_sel
].used
[1] = val
;
1305 static uint64_t virtio_pci_notify_read(void *opaque
, hwaddr addr
,
1311 static void virtio_pci_notify_write(void *opaque
, hwaddr addr
,
1312 uint64_t val
, unsigned size
)
1314 VirtIODevice
*vdev
= opaque
;
1315 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(DEVICE(vdev
)->parent_bus
->parent
);
1316 unsigned queue
= addr
/ virtio_pci_queue_mem_mult(proxy
);
1318 if (queue
< VIRTIO_QUEUE_MAX
) {
1319 virtio_queue_notify(vdev
, queue
);
1323 static void virtio_pci_notify_write_pio(void *opaque
, hwaddr addr
,
1324 uint64_t val
, unsigned size
)
1326 VirtIODevice
*vdev
= opaque
;
1327 unsigned queue
= val
;
1329 if (queue
< VIRTIO_QUEUE_MAX
) {
1330 virtio_queue_notify(vdev
, queue
);
1334 static uint64_t virtio_pci_isr_read(void *opaque
, hwaddr addr
,
1337 VirtIOPCIProxy
*proxy
= opaque
;
1338 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1339 uint64_t val
= atomic_xchg(&vdev
->isr
, 0);
1340 pci_irq_deassert(&proxy
->pci_dev
);
1345 static void virtio_pci_isr_write(void *opaque
, hwaddr addr
,
1346 uint64_t val
, unsigned size
)
1350 static uint64_t virtio_pci_device_read(void *opaque
, hwaddr addr
,
1353 VirtIODevice
*vdev
= opaque
;
1358 val
= virtio_config_modern_readb(vdev
, addr
);
1361 val
= virtio_config_modern_readw(vdev
, addr
);
1364 val
= virtio_config_modern_readl(vdev
, addr
);
1370 static void virtio_pci_device_write(void *opaque
, hwaddr addr
,
1371 uint64_t val
, unsigned size
)
1373 VirtIODevice
*vdev
= opaque
;
1376 virtio_config_modern_writeb(vdev
, addr
, val
);
1379 virtio_config_modern_writew(vdev
, addr
, val
);
1382 virtio_config_modern_writel(vdev
, addr
, val
);
1387 static void virtio_pci_modern_regions_init(VirtIOPCIProxy
*proxy
)
1389 static const MemoryRegionOps common_ops
= {
1390 .read
= virtio_pci_common_read
,
1391 .write
= virtio_pci_common_write
,
1393 .min_access_size
= 1,
1394 .max_access_size
= 4,
1396 .endianness
= DEVICE_LITTLE_ENDIAN
,
1398 static const MemoryRegionOps isr_ops
= {
1399 .read
= virtio_pci_isr_read
,
1400 .write
= virtio_pci_isr_write
,
1402 .min_access_size
= 1,
1403 .max_access_size
= 4,
1405 .endianness
= DEVICE_LITTLE_ENDIAN
,
1407 static const MemoryRegionOps device_ops
= {
1408 .read
= virtio_pci_device_read
,
1409 .write
= virtio_pci_device_write
,
1411 .min_access_size
= 1,
1412 .max_access_size
= 4,
1414 .endianness
= DEVICE_LITTLE_ENDIAN
,
1416 static const MemoryRegionOps notify_ops
= {
1417 .read
= virtio_pci_notify_read
,
1418 .write
= virtio_pci_notify_write
,
1420 .min_access_size
= 1,
1421 .max_access_size
= 4,
1423 .endianness
= DEVICE_LITTLE_ENDIAN
,
1425 static const MemoryRegionOps notify_pio_ops
= {
1426 .read
= virtio_pci_notify_read
,
1427 .write
= virtio_pci_notify_write_pio
,
1429 .min_access_size
= 1,
1430 .max_access_size
= 4,
1432 .endianness
= DEVICE_LITTLE_ENDIAN
,
1436 memory_region_init_io(&proxy
->common
.mr
, OBJECT(proxy
),
1439 "virtio-pci-common",
1440 proxy
->common
.size
);
1442 memory_region_init_io(&proxy
->isr
.mr
, OBJECT(proxy
),
1448 memory_region_init_io(&proxy
->device
.mr
, OBJECT(proxy
),
1450 virtio_bus_get_device(&proxy
->bus
),
1451 "virtio-pci-device",
1452 proxy
->device
.size
);
1454 memory_region_init_io(&proxy
->notify
.mr
, OBJECT(proxy
),
1456 virtio_bus_get_device(&proxy
->bus
),
1457 "virtio-pci-notify",
1458 proxy
->notify
.size
);
1460 memory_region_init_io(&proxy
->notify_pio
.mr
, OBJECT(proxy
),
1462 virtio_bus_get_device(&proxy
->bus
),
1463 "virtio-pci-notify-pio",
1464 proxy
->notify_pio
.size
);
1467 static void virtio_pci_modern_region_map(VirtIOPCIProxy
*proxy
,
1468 VirtIOPCIRegion
*region
,
1469 struct virtio_pci_cap
*cap
,
1473 memory_region_add_subregion(mr
, region
->offset
, ®ion
->mr
);
1475 cap
->cfg_type
= region
->type
;
1477 cap
->offset
= cpu_to_le32(region
->offset
);
1478 cap
->length
= cpu_to_le32(region
->size
);
1479 virtio_pci_add_mem_cap(proxy
, cap
);
1483 static void virtio_pci_modern_mem_region_map(VirtIOPCIProxy
*proxy
,
1484 VirtIOPCIRegion
*region
,
1485 struct virtio_pci_cap
*cap
)
1487 virtio_pci_modern_region_map(proxy
, region
, cap
,
1488 &proxy
->modern_bar
, proxy
->modern_mem_bar_idx
);
1491 static void virtio_pci_modern_io_region_map(VirtIOPCIProxy
*proxy
,
1492 VirtIOPCIRegion
*region
,
1493 struct virtio_pci_cap
*cap
)
1495 virtio_pci_modern_region_map(proxy
, region
, cap
,
1496 &proxy
->io_bar
, proxy
->modern_io_bar_idx
);
1499 static void virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy
*proxy
,
1500 VirtIOPCIRegion
*region
)
1502 memory_region_del_subregion(&proxy
->modern_bar
,
1506 static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy
*proxy
,
1507 VirtIOPCIRegion
*region
)
1509 memory_region_del_subregion(&proxy
->io_bar
,
1513 static void virtio_pci_pre_plugged(DeviceState
*d
, Error
**errp
)
1515 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1516 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1518 if (virtio_pci_modern(proxy
)) {
1519 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_VERSION_1
);
1522 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_BAD_FEATURE
);
1525 /* This is called by virtio-bus just after the device is plugged. */
1526 static void virtio_pci_device_plugged(DeviceState
*d
, Error
**errp
)
1528 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1529 VirtioBusState
*bus
= &proxy
->bus
;
1530 bool legacy
= virtio_pci_legacy(proxy
);
1532 bool modern_pio
= proxy
->flags
& VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY
;
1535 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1538 * Virtio capabilities present without
1539 * VIRTIO_F_VERSION_1 confuses guests
1541 if (!proxy
->ignore_backend_features
&&
1542 !virtio_has_feature(vdev
->host_features
, VIRTIO_F_VERSION_1
)) {
1543 virtio_pci_disable_modern(proxy
);
1546 error_setg(errp
, "Device doesn't support modern mode, and legacy"
1547 " mode is disabled");
1548 error_append_hint(errp
, "Set disable-legacy to off\n");
1554 modern
= virtio_pci_modern(proxy
);
1556 config
= proxy
->pci_dev
.config
;
1557 if (proxy
->class_code
) {
1558 pci_config_set_class(config
, proxy
->class_code
);
1562 if (virtio_host_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
)) {
1563 error_setg(errp
, "VIRTIO_F_IOMMU_PLATFORM was supported by"
1564 " neither legacy nor transitional device");
1568 * Legacy and transitional devices use specific subsystem IDs.
1569 * Note that the subsystem vendor ID (config + PCI_SUBSYSTEM_VENDOR_ID)
1570 * is set to PCI_SUBVENDOR_ID_REDHAT_QUMRANET by default.
1572 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, virtio_bus_get_vdev_id(bus
));
1574 /* pure virtio-1.0 */
1575 pci_set_word(config
+ PCI_VENDOR_ID
,
1576 PCI_VENDOR_ID_REDHAT_QUMRANET
);
1577 pci_set_word(config
+ PCI_DEVICE_ID
,
1578 0x1040 + virtio_bus_get_vdev_id(bus
));
1579 pci_config_set_revision(config
, 1);
1581 config
[PCI_INTERRUPT_PIN
] = 1;
1585 struct virtio_pci_cap cap
= {
1586 .cap_len
= sizeof cap
,
1588 struct virtio_pci_notify_cap notify
= {
1589 .cap
.cap_len
= sizeof notify
,
1590 .notify_off_multiplier
=
1591 cpu_to_le32(virtio_pci_queue_mem_mult(proxy
)),
1593 struct virtio_pci_cfg_cap cfg
= {
1594 .cap
.cap_len
= sizeof cfg
,
1595 .cap
.cfg_type
= VIRTIO_PCI_CAP_PCI_CFG
,
1597 struct virtio_pci_notify_cap notify_pio
= {
1598 .cap
.cap_len
= sizeof notify
,
1599 .notify_off_multiplier
= cpu_to_le32(0x0),
1602 struct virtio_pci_cfg_cap
*cfg_mask
;
1604 virtio_pci_modern_regions_init(proxy
);
1606 virtio_pci_modern_mem_region_map(proxy
, &proxy
->common
, &cap
);
1607 virtio_pci_modern_mem_region_map(proxy
, &proxy
->isr
, &cap
);
1608 virtio_pci_modern_mem_region_map(proxy
, &proxy
->device
, &cap
);
1609 virtio_pci_modern_mem_region_map(proxy
, &proxy
->notify
, ¬ify
.cap
);
1612 memory_region_init(&proxy
->io_bar
, OBJECT(proxy
),
1613 "virtio-pci-io", 0x4);
1615 pci_register_bar(&proxy
->pci_dev
, proxy
->modern_io_bar_idx
,
1616 PCI_BASE_ADDRESS_SPACE_IO
, &proxy
->io_bar
);
1618 virtio_pci_modern_io_region_map(proxy
, &proxy
->notify_pio
,
1622 pci_register_bar(&proxy
->pci_dev
, proxy
->modern_mem_bar_idx
,
1623 PCI_BASE_ADDRESS_SPACE_MEMORY
|
1624 PCI_BASE_ADDRESS_MEM_PREFETCH
|
1625 PCI_BASE_ADDRESS_MEM_TYPE_64
,
1626 &proxy
->modern_bar
);
1628 proxy
->config_cap
= virtio_pci_add_mem_cap(proxy
, &cfg
.cap
);
1629 cfg_mask
= (void *)(proxy
->pci_dev
.wmask
+ proxy
->config_cap
);
1630 pci_set_byte(&cfg_mask
->cap
.bar
, ~0x0);
1631 pci_set_long((uint8_t *)&cfg_mask
->cap
.offset
, ~0x0);
1632 pci_set_long((uint8_t *)&cfg_mask
->cap
.length
, ~0x0);
1633 pci_set_long(cfg_mask
->pci_cfg_data
, ~0x0);
1636 if (proxy
->nvectors
) {
1637 int err
= msix_init_exclusive_bar(&proxy
->pci_dev
, proxy
->nvectors
,
1638 proxy
->msix_bar_idx
, NULL
);
1640 /* Notice when a system that supports MSIx can't initialize it */
1641 if (err
!= -ENOTSUP
) {
1642 warn_report("unable to init msix vectors to %" PRIu32
,
1645 proxy
->nvectors
= 0;
1649 proxy
->pci_dev
.config_write
= virtio_write_config
;
1650 proxy
->pci_dev
.config_read
= virtio_read_config
;
1653 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
)
1654 + virtio_bus_get_vdev_config_len(bus
);
1655 size
= pow2ceil(size
);
1657 memory_region_init_io(&proxy
->bar
, OBJECT(proxy
),
1658 &virtio_pci_config_ops
,
1659 proxy
, "virtio-pci", size
);
1661 pci_register_bar(&proxy
->pci_dev
, proxy
->legacy_io_bar_idx
,
1662 PCI_BASE_ADDRESS_SPACE_IO
, &proxy
->bar
);
1666 static void virtio_pci_device_unplugged(DeviceState
*d
)
1668 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1669 bool modern
= virtio_pci_modern(proxy
);
1670 bool modern_pio
= proxy
->flags
& VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY
;
1672 virtio_pci_stop_ioeventfd(proxy
);
1675 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->common
);
1676 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->isr
);
1677 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->device
);
1678 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->notify
);
1680 virtio_pci_modern_io_region_unmap(proxy
, &proxy
->notify_pio
);
1685 static void virtio_pci_realize(PCIDevice
*pci_dev
, Error
**errp
)
1687 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
1688 VirtioPCIClass
*k
= VIRTIO_PCI_GET_CLASS(pci_dev
);
1689 bool pcie_port
= pci_bus_is_express(pci_get_bus(pci_dev
)) &&
1690 !pci_bus_is_root(pci_get_bus(pci_dev
));
1692 if (kvm_enabled() && !kvm_has_many_ioeventfds()) {
1693 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
1697 * virtio pci bar layout used by default.
1698 * subclasses can re-arrange things if needed.
1700 * region 0 -- virtio legacy io bar
1701 * region 1 -- msi-x bar
1702 * region 4+5 -- virtio modern memory (64bit) bar
1705 proxy
->legacy_io_bar_idx
= 0;
1706 proxy
->msix_bar_idx
= 1;
1707 proxy
->modern_io_bar_idx
= 2;
1708 proxy
->modern_mem_bar_idx
= 4;
1710 proxy
->common
.offset
= 0x0;
1711 proxy
->common
.size
= 0x1000;
1712 proxy
->common
.type
= VIRTIO_PCI_CAP_COMMON_CFG
;
1714 proxy
->isr
.offset
= 0x1000;
1715 proxy
->isr
.size
= 0x1000;
1716 proxy
->isr
.type
= VIRTIO_PCI_CAP_ISR_CFG
;
1718 proxy
->device
.offset
= 0x2000;
1719 proxy
->device
.size
= 0x1000;
1720 proxy
->device
.type
= VIRTIO_PCI_CAP_DEVICE_CFG
;
1722 proxy
->notify
.offset
= 0x3000;
1723 proxy
->notify
.size
= virtio_pci_queue_mem_mult(proxy
) * VIRTIO_QUEUE_MAX
;
1724 proxy
->notify
.type
= VIRTIO_PCI_CAP_NOTIFY_CFG
;
1726 proxy
->notify_pio
.offset
= 0x0;
1727 proxy
->notify_pio
.size
= 0x4;
1728 proxy
->notify_pio
.type
= VIRTIO_PCI_CAP_NOTIFY_CFG
;
1730 /* subclasses can enforce modern, so do this unconditionally */
1731 memory_region_init(&proxy
->modern_bar
, OBJECT(proxy
), "virtio-pci",
1732 /* PCI BAR regions must be powers of 2 */
1733 pow2ceil(proxy
->notify
.offset
+ proxy
->notify
.size
));
1735 if (proxy
->disable_legacy
== ON_OFF_AUTO_AUTO
) {
1736 proxy
->disable_legacy
= pcie_port
? ON_OFF_AUTO_ON
: ON_OFF_AUTO_OFF
;
1739 if (!virtio_pci_modern(proxy
) && !virtio_pci_legacy(proxy
)) {
1740 error_setg(errp
, "device cannot work as neither modern nor legacy mode"
1742 error_append_hint(errp
, "Set either disable-modern or disable-legacy"
1747 if (pcie_port
&& pci_is_express(pci_dev
)) {
1750 pos
= pcie_endpoint_cap_init(pci_dev
, 0);
1753 pos
= pci_add_capability(pci_dev
, PCI_CAP_ID_PM
, 0,
1754 PCI_PM_SIZEOF
, errp
);
1759 pci_dev
->exp
.pm_cap
= pos
;
1762 * Indicates that this function complies with revision 1.2 of the
1763 * PCI Power Management Interface Specification.
1765 pci_set_word(pci_dev
->config
+ pos
+ PCI_PM_PMC
, 0x3);
1767 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_DEVERR
) {
1768 /* Init error enabling flags */
1769 pcie_cap_deverr_init(pci_dev
);
1772 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_LNKCTL
) {
1773 /* Init Link Control Register */
1774 pcie_cap_lnkctl_init(pci_dev
);
1777 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_PM
) {
1778 /* Init Power Management Control Register */
1779 pci_set_word(pci_dev
->wmask
+ pos
+ PCI_PM_CTRL
,
1780 PCI_PM_CTRL_STATE_MASK
);
1783 if (proxy
->flags
& VIRTIO_PCI_FLAG_ATS
) {
1784 pcie_ats_init(pci_dev
, 256);
1787 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_FLR
) {
1788 /* Set Function Level Reset capability bit */
1789 pcie_cap_flr_init(pci_dev
);
1793 * make future invocations of pci_is_express() return false
1794 * and pci_config_size() return PCI_CONFIG_SPACE_SIZE.
1796 pci_dev
->cap_present
&= ~QEMU_PCI_CAP_EXPRESS
;
1799 virtio_pci_bus_new(&proxy
->bus
, sizeof(proxy
->bus
), proxy
);
1801 k
->realize(proxy
, errp
);
1805 static void virtio_pci_exit(PCIDevice
*pci_dev
)
1807 msix_uninit_exclusive_bar(pci_dev
);
1810 static void virtio_pci_reset(DeviceState
*qdev
)
1812 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1813 VirtioBusState
*bus
= VIRTIO_BUS(&proxy
->bus
);
1814 PCIDevice
*dev
= PCI_DEVICE(qdev
);
1817 virtio_pci_stop_ioeventfd(proxy
);
1818 virtio_bus_reset(bus
);
1819 msix_unuse_all_vectors(&proxy
->pci_dev
);
1821 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1822 proxy
->vqs
[i
].enabled
= 0;
1823 proxy
->vqs
[i
].num
= 0;
1824 proxy
->vqs
[i
].desc
[0] = proxy
->vqs
[i
].desc
[1] = 0;
1825 proxy
->vqs
[i
].avail
[0] = proxy
->vqs
[i
].avail
[1] = 0;
1826 proxy
->vqs
[i
].used
[0] = proxy
->vqs
[i
].used
[1] = 0;
1829 if (pci_is_express(dev
)) {
1830 pcie_cap_deverr_reset(dev
);
1831 pcie_cap_lnkctl_reset(dev
);
1833 pci_set_word(dev
->config
+ dev
->exp
.pm_cap
+ PCI_PM_CTRL
, 0);
1837 static Property virtio_pci_properties
[] = {
1838 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy
, flags
,
1839 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT
, false),
1840 DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy
, flags
,
1841 VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT
, true),
1842 DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy
, flags
,
1843 VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT
, false),
1844 DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy
, flags
,
1845 VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT
, false),
1846 DEFINE_PROP_BIT("page-per-vq", VirtIOPCIProxy
, flags
,
1847 VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT
, false),
1848 DEFINE_PROP_BOOL("x-ignore-backend-features", VirtIOPCIProxy
,
1849 ignore_backend_features
, false),
1850 DEFINE_PROP_BIT("ats", VirtIOPCIProxy
, flags
,
1851 VIRTIO_PCI_FLAG_ATS_BIT
, false),
1852 DEFINE_PROP_BIT("x-pcie-deverr-init", VirtIOPCIProxy
, flags
,
1853 VIRTIO_PCI_FLAG_INIT_DEVERR_BIT
, true),
1854 DEFINE_PROP_BIT("x-pcie-lnkctl-init", VirtIOPCIProxy
, flags
,
1855 VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT
, true),
1856 DEFINE_PROP_BIT("x-pcie-pm-init", VirtIOPCIProxy
, flags
,
1857 VIRTIO_PCI_FLAG_INIT_PM_BIT
, true),
1858 DEFINE_PROP_BIT("x-pcie-flr-init", VirtIOPCIProxy
, flags
,
1859 VIRTIO_PCI_FLAG_INIT_FLR_BIT
, true),
1860 DEFINE_PROP_END_OF_LIST(),
1863 static void virtio_pci_dc_realize(DeviceState
*qdev
, Error
**errp
)
1865 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_GET_CLASS(qdev
);
1866 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1867 PCIDevice
*pci_dev
= &proxy
->pci_dev
;
1869 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_PCIE
) &&
1870 virtio_pci_modern(proxy
)) {
1871 pci_dev
->cap_present
|= QEMU_PCI_CAP_EXPRESS
;
1874 vpciklass
->parent_dc_realize(qdev
, errp
);
1877 static void virtio_pci_class_init(ObjectClass
*klass
, void *data
)
1879 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1880 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1881 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_CLASS(klass
);
1883 dc
->props
= virtio_pci_properties
;
1884 k
->realize
= virtio_pci_realize
;
1885 k
->exit
= virtio_pci_exit
;
1886 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1887 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1888 k
->class_id
= PCI_CLASS_OTHERS
;
1889 device_class_set_parent_realize(dc
, virtio_pci_dc_realize
,
1890 &vpciklass
->parent_dc_realize
);
1891 dc
->reset
= virtio_pci_reset
;
1894 static const TypeInfo virtio_pci_info
= {
1895 .name
= TYPE_VIRTIO_PCI
,
1896 .parent
= TYPE_PCI_DEVICE
,
1897 .instance_size
= sizeof(VirtIOPCIProxy
),
1898 .class_init
= virtio_pci_class_init
,
1899 .class_size
= sizeof(VirtioPCIClass
),
1903 static Property virtio_pci_generic_properties
[] = {
1904 DEFINE_PROP_ON_OFF_AUTO("disable-legacy", VirtIOPCIProxy
, disable_legacy
,
1906 DEFINE_PROP_BOOL("disable-modern", VirtIOPCIProxy
, disable_modern
, false),
1907 DEFINE_PROP_END_OF_LIST(),
1910 static void virtio_pci_base_class_init(ObjectClass
*klass
, void *data
)
1912 const VirtioPCIDeviceTypeInfo
*t
= data
;
1913 if (t
->class_init
) {
1914 t
->class_init(klass
, NULL
);
1918 static void virtio_pci_generic_class_init(ObjectClass
*klass
, void *data
)
1920 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1922 dc
->props
= virtio_pci_generic_properties
;
1925 static void virtio_pci_transitional_instance_init(Object
*obj
)
1927 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(obj
);
1929 proxy
->disable_legacy
= ON_OFF_AUTO_OFF
;
1930 proxy
->disable_modern
= false;
1933 static void virtio_pci_non_transitional_instance_init(Object
*obj
)
1935 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(obj
);
1937 proxy
->disable_legacy
= ON_OFF_AUTO_ON
;
1938 proxy
->disable_modern
= false;
1941 void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo
*t
)
1943 char *base_name
= NULL
;
1944 TypeInfo base_type_info
= {
1945 .name
= t
->base_name
,
1946 .parent
= t
->parent
? t
->parent
: TYPE_VIRTIO_PCI
,
1947 .instance_size
= t
->instance_size
,
1948 .instance_init
= t
->instance_init
,
1949 .class_size
= t
->class_size
,
1951 .interfaces
= t
->interfaces
,
1953 TypeInfo generic_type_info
= {
1954 .name
= t
->generic_name
,
1955 .parent
= base_type_info
.name
,
1956 .class_init
= virtio_pci_generic_class_init
,
1957 .interfaces
= (InterfaceInfo
[]) {
1958 { INTERFACE_PCIE_DEVICE
},
1959 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
1964 if (!base_type_info
.name
) {
1965 /* No base type -> register a single generic device type */
1966 /* use intermediate %s-base-type to add generic device props */
1967 base_name
= g_strdup_printf("%s-base-type", t
->generic_name
);
1968 base_type_info
.name
= base_name
;
1969 base_type_info
.class_init
= virtio_pci_generic_class_init
;
1971 generic_type_info
.parent
= base_name
;
1972 generic_type_info
.class_init
= virtio_pci_base_class_init
;
1973 generic_type_info
.class_data
= (void *)t
;
1975 assert(!t
->non_transitional_name
);
1976 assert(!t
->transitional_name
);
1978 base_type_info
.class_init
= virtio_pci_base_class_init
;
1979 base_type_info
.class_data
= (void *)t
;
1982 type_register(&base_type_info
);
1983 if (generic_type_info
.name
) {
1984 type_register(&generic_type_info
);
1987 if (t
->non_transitional_name
) {
1988 const TypeInfo non_transitional_type_info
= {
1989 .name
= t
->non_transitional_name
,
1990 .parent
= base_type_info
.name
,
1991 .instance_init
= virtio_pci_non_transitional_instance_init
,
1992 .interfaces
= (InterfaceInfo
[]) {
1993 { INTERFACE_PCIE_DEVICE
},
1994 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
1998 type_register(&non_transitional_type_info
);
2001 if (t
->transitional_name
) {
2002 const TypeInfo transitional_type_info
= {
2003 .name
= t
->transitional_name
,
2004 .parent
= base_type_info
.name
,
2005 .instance_init
= virtio_pci_transitional_instance_init
,
2006 .interfaces
= (InterfaceInfo
[]) {
2008 * Transitional virtio devices work only as Conventional PCI
2009 * devices because they require PIO ports.
2011 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
2015 type_register(&transitional_type_info
);
2020 /* virtio-pci-bus */
2022 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
2023 VirtIOPCIProxy
*dev
)
2025 DeviceState
*qdev
= DEVICE(dev
);
2026 char virtio_bus_name
[] = "virtio-bus";
2028 qbus_create_inplace(bus
, bus_size
, TYPE_VIRTIO_PCI_BUS
, qdev
,
2032 static void virtio_pci_bus_class_init(ObjectClass
*klass
, void *data
)
2034 BusClass
*bus_class
= BUS_CLASS(klass
);
2035 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
2036 bus_class
->max_dev
= 1;
2037 k
->notify
= virtio_pci_notify
;
2038 k
->save_config
= virtio_pci_save_config
;
2039 k
->load_config
= virtio_pci_load_config
;
2040 k
->save_queue
= virtio_pci_save_queue
;
2041 k
->load_queue
= virtio_pci_load_queue
;
2042 k
->save_extra_state
= virtio_pci_save_extra_state
;
2043 k
->load_extra_state
= virtio_pci_load_extra_state
;
2044 k
->has_extra_state
= virtio_pci_has_extra_state
;
2045 k
->query_guest_notifiers
= virtio_pci_query_guest_notifiers
;
2046 k
->set_guest_notifiers
= virtio_pci_set_guest_notifiers
;
2047 k
->set_host_notifier_mr
= virtio_pci_set_host_notifier_mr
;
2048 k
->vmstate_change
= virtio_pci_vmstate_change
;
2049 k
->pre_plugged
= virtio_pci_pre_plugged
;
2050 k
->device_plugged
= virtio_pci_device_plugged
;
2051 k
->device_unplugged
= virtio_pci_device_unplugged
;
2052 k
->query_nvectors
= virtio_pci_query_nvectors
;
2053 k
->ioeventfd_enabled
= virtio_pci_ioeventfd_enabled
;
2054 k
->ioeventfd_assign
= virtio_pci_ioeventfd_assign
;
2055 k
->get_dma_as
= virtio_pci_get_dma_as
;
2058 static const TypeInfo virtio_pci_bus_info
= {
2059 .name
= TYPE_VIRTIO_PCI_BUS
,
2060 .parent
= TYPE_VIRTIO_BUS
,
2061 .instance_size
= sizeof(VirtioPCIBusState
),
2062 .class_init
= virtio_pci_bus_class_init
,
2065 static void virtio_pci_register_types(void)
2068 type_register_static(&virtio_pci_bus_info
);
2069 type_register_static(&virtio_pci_info
);
2072 type_init(virtio_pci_register_types
)