4 * Copyright IBM, Corp. 2007
5 * Copyright (c) 2009 CodeSourcery
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paul Brook <paul@codesourcery.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
14 * Contributions after 2012-01-13 are licensed under the terms of the
15 * GNU GPL, version 2 or (at your option) any later version.
18 #include "qemu/osdep.h"
20 #include "standard-headers/linux/virtio_pci.h"
21 #include "hw/virtio/virtio.h"
22 #include "hw/virtio/virtio-blk.h"
23 #include "hw/virtio/virtio-net.h"
24 #include "hw/virtio/virtio-serial.h"
25 #include "hw/virtio/virtio-scsi.h"
26 #include "hw/virtio/virtio-balloon.h"
27 #include "hw/virtio/virtio-input.h"
28 #include "hw/pci/pci.h"
29 #include "qapi/error.h"
30 #include "qemu/error-report.h"
31 #include "hw/pci/msi.h"
32 #include "hw/pci/msix.h"
33 #include "hw/loader.h"
34 #include "sysemu/kvm.h"
35 #include "sysemu/block-backend.h"
36 #include "virtio-pci.h"
37 #include "qemu/range.h"
38 #include "hw/virtio/virtio-bus.h"
39 #include "qapi/visitor.h"
41 #define VIRTIO_PCI_REGION_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_present(dev))
43 #undef VIRTIO_PCI_CONFIG
45 /* The remaining space is defined by each driver as the per-driver
46 * configuration space */
47 #define VIRTIO_PCI_CONFIG_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_enabled(dev))
49 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
51 static void virtio_pci_reset(DeviceState
*qdev
);
54 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
55 static inline VirtIOPCIProxy
*to_virtio_pci_proxy(DeviceState
*d
)
57 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
60 /* DeviceState to VirtIOPCIProxy. Note: used on datapath,
61 * be careful and test performance if you change this.
63 static inline VirtIOPCIProxy
*to_virtio_pci_proxy_fast(DeviceState
*d
)
65 return container_of(d
, VirtIOPCIProxy
, pci_dev
.qdev
);
68 static void virtio_pci_notify(DeviceState
*d
, uint16_t vector
)
70 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy_fast(d
);
72 if (msix_enabled(&proxy
->pci_dev
))
73 msix_notify(&proxy
->pci_dev
, vector
);
75 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
76 pci_set_irq(&proxy
->pci_dev
, atomic_read(&vdev
->isr
) & 1);
80 static void virtio_pci_save_config(DeviceState
*d
, QEMUFile
*f
)
82 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
83 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
85 pci_device_save(&proxy
->pci_dev
, f
);
86 msix_save(&proxy
->pci_dev
, f
);
87 if (msix_present(&proxy
->pci_dev
))
88 qemu_put_be16(f
, vdev
->config_vector
);
91 static const VMStateDescription vmstate_virtio_pci_modern_queue_state
= {
92 .name
= "virtio_pci/modern_queue_state",
94 .minimum_version_id
= 1,
95 .fields
= (VMStateField
[]) {
96 VMSTATE_UINT16(num
, VirtIOPCIQueue
),
97 VMSTATE_UNUSED(1), /* enabled was stored as be16 */
98 VMSTATE_BOOL(enabled
, VirtIOPCIQueue
),
99 VMSTATE_UINT32_ARRAY(desc
, VirtIOPCIQueue
, 2),
100 VMSTATE_UINT32_ARRAY(avail
, VirtIOPCIQueue
, 2),
101 VMSTATE_UINT32_ARRAY(used
, VirtIOPCIQueue
, 2),
102 VMSTATE_END_OF_LIST()
106 static bool virtio_pci_modern_state_needed(void *opaque
)
108 VirtIOPCIProxy
*proxy
= opaque
;
110 return virtio_pci_modern(proxy
);
113 static const VMStateDescription vmstate_virtio_pci_modern_state_sub
= {
114 .name
= "virtio_pci/modern_state",
116 .minimum_version_id
= 1,
117 .needed
= &virtio_pci_modern_state_needed
,
118 .fields
= (VMStateField
[]) {
119 VMSTATE_UINT32(dfselect
, VirtIOPCIProxy
),
120 VMSTATE_UINT32(gfselect
, VirtIOPCIProxy
),
121 VMSTATE_UINT32_ARRAY(guest_features
, VirtIOPCIProxy
, 2),
122 VMSTATE_STRUCT_ARRAY(vqs
, VirtIOPCIProxy
, VIRTIO_QUEUE_MAX
, 0,
123 vmstate_virtio_pci_modern_queue_state
,
125 VMSTATE_END_OF_LIST()
129 static const VMStateDescription vmstate_virtio_pci
= {
130 .name
= "virtio_pci",
132 .minimum_version_id
= 1,
133 .minimum_version_id_old
= 1,
134 .fields
= (VMStateField
[]) {
135 VMSTATE_END_OF_LIST()
137 .subsections
= (const VMStateDescription
*[]) {
138 &vmstate_virtio_pci_modern_state_sub
,
143 static bool virtio_pci_has_extra_state(DeviceState
*d
)
145 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
147 return proxy
->flags
& VIRTIO_PCI_FLAG_MIGRATE_EXTRA
;
150 static void virtio_pci_save_extra_state(DeviceState
*d
, QEMUFile
*f
)
152 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
154 vmstate_save_state(f
, &vmstate_virtio_pci
, proxy
, NULL
);
157 static int virtio_pci_load_extra_state(DeviceState
*d
, QEMUFile
*f
)
159 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
161 return vmstate_load_state(f
, &vmstate_virtio_pci
, proxy
, 1);
164 static void virtio_pci_save_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
166 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
167 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
169 if (msix_present(&proxy
->pci_dev
))
170 qemu_put_be16(f
, virtio_queue_vector(vdev
, n
));
173 static int virtio_pci_load_config(DeviceState
*d
, QEMUFile
*f
)
175 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
176 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
179 ret
= pci_device_load(&proxy
->pci_dev
, f
);
183 msix_unuse_all_vectors(&proxy
->pci_dev
);
184 msix_load(&proxy
->pci_dev
, f
);
185 if (msix_present(&proxy
->pci_dev
)) {
186 qemu_get_be16s(f
, &vdev
->config_vector
);
188 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
190 if (vdev
->config_vector
!= VIRTIO_NO_VECTOR
) {
191 return msix_vector_use(&proxy
->pci_dev
, vdev
->config_vector
);
196 static int virtio_pci_load_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
198 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
199 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
202 if (msix_present(&proxy
->pci_dev
)) {
203 qemu_get_be16s(f
, &vector
);
205 vector
= VIRTIO_NO_VECTOR
;
207 virtio_queue_set_vector(vdev
, n
, vector
);
208 if (vector
!= VIRTIO_NO_VECTOR
) {
209 return msix_vector_use(&proxy
->pci_dev
, vector
);
215 static bool virtio_pci_ioeventfd_enabled(DeviceState
*d
)
217 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
219 return (proxy
->flags
& VIRTIO_PCI_FLAG_USE_IOEVENTFD
) != 0;
222 #define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000
224 static inline int virtio_pci_queue_mem_mult(struct VirtIOPCIProxy
*proxy
)
226 return (proxy
->flags
& VIRTIO_PCI_FLAG_PAGE_PER_VQ
) ?
227 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT
: 4;
230 static int virtio_pci_ioeventfd_assign(DeviceState
*d
, EventNotifier
*notifier
,
233 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
234 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
235 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
236 bool legacy
= virtio_pci_legacy(proxy
);
237 bool modern
= virtio_pci_modern(proxy
);
238 bool fast_mmio
= kvm_ioeventfd_any_length_enabled();
239 bool modern_pio
= proxy
->flags
& VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY
;
240 MemoryRegion
*modern_mr
= &proxy
->notify
.mr
;
241 MemoryRegion
*modern_notify_mr
= &proxy
->notify_pio
.mr
;
242 MemoryRegion
*legacy_mr
= &proxy
->bar
;
243 hwaddr modern_addr
= virtio_pci_queue_mem_mult(proxy
) *
244 virtio_get_queue_index(vq
);
245 hwaddr legacy_addr
= VIRTIO_PCI_QUEUE_NOTIFY
;
250 memory_region_add_eventfd(modern_mr
, modern_addr
, 0,
253 memory_region_add_eventfd(modern_mr
, modern_addr
, 2,
257 memory_region_add_eventfd(modern_notify_mr
, 0, 2,
262 memory_region_add_eventfd(legacy_mr
, legacy_addr
, 2,
268 memory_region_del_eventfd(modern_mr
, modern_addr
, 0,
271 memory_region_del_eventfd(modern_mr
, modern_addr
, 2,
275 memory_region_del_eventfd(modern_notify_mr
, 0, 2,
280 memory_region_del_eventfd(legacy_mr
, legacy_addr
, 2,
287 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy
*proxy
)
289 virtio_bus_start_ioeventfd(&proxy
->bus
);
292 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy
*proxy
)
294 virtio_bus_stop_ioeventfd(&proxy
->bus
);
297 static void virtio_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
299 VirtIOPCIProxy
*proxy
= opaque
;
300 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
304 case VIRTIO_PCI_GUEST_FEATURES
:
305 /* Guest does not negotiate properly? We have to assume nothing. */
306 if (val
& (1 << VIRTIO_F_BAD_FEATURE
)) {
307 val
= virtio_bus_get_vdev_bad_features(&proxy
->bus
);
309 virtio_set_features(vdev
, val
);
311 case VIRTIO_PCI_QUEUE_PFN
:
312 pa
= (hwaddr
)val
<< VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
314 virtio_pci_reset(DEVICE(proxy
));
317 virtio_queue_set_addr(vdev
, vdev
->queue_sel
, pa
);
319 case VIRTIO_PCI_QUEUE_SEL
:
320 if (val
< VIRTIO_QUEUE_MAX
)
321 vdev
->queue_sel
= val
;
323 case VIRTIO_PCI_QUEUE_NOTIFY
:
324 if (val
< VIRTIO_QUEUE_MAX
) {
325 virtio_queue_notify(vdev
, val
);
328 case VIRTIO_PCI_STATUS
:
329 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
330 virtio_pci_stop_ioeventfd(proxy
);
333 virtio_set_status(vdev
, val
& 0xFF);
335 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
336 virtio_pci_start_ioeventfd(proxy
);
339 if (vdev
->status
== 0) {
340 virtio_pci_reset(DEVICE(proxy
));
343 /* Linux before 2.6.34 drives the device without enabling
344 the PCI device bus master bit. Enable it automatically
345 for the guest. This is a PCI spec violation but so is
346 initiating DMA with bus master bit clear. */
347 if (val
== (VIRTIO_CONFIG_S_ACKNOWLEDGE
| VIRTIO_CONFIG_S_DRIVER
)) {
348 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
349 proxy
->pci_dev
.config
[PCI_COMMAND
] |
350 PCI_COMMAND_MASTER
, 1);
353 case VIRTIO_MSI_CONFIG_VECTOR
:
354 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
355 /* Make it possible for guest to discover an error took place. */
356 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
357 val
= VIRTIO_NO_VECTOR
;
358 vdev
->config_vector
= val
;
360 case VIRTIO_MSI_QUEUE_VECTOR
:
361 msix_vector_unuse(&proxy
->pci_dev
,
362 virtio_queue_vector(vdev
, vdev
->queue_sel
));
363 /* Make it possible for guest to discover an error took place. */
364 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0)
365 val
= VIRTIO_NO_VECTOR
;
366 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
369 error_report("%s: unexpected address 0x%x value 0x%x",
370 __func__
, addr
, val
);
375 static uint32_t virtio_ioport_read(VirtIOPCIProxy
*proxy
, uint32_t addr
)
377 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
378 uint32_t ret
= 0xFFFFFFFF;
381 case VIRTIO_PCI_HOST_FEATURES
:
382 ret
= vdev
->host_features
;
384 case VIRTIO_PCI_GUEST_FEATURES
:
385 ret
= vdev
->guest_features
;
387 case VIRTIO_PCI_QUEUE_PFN
:
388 ret
= virtio_queue_get_addr(vdev
, vdev
->queue_sel
)
389 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
;
391 case VIRTIO_PCI_QUEUE_NUM
:
392 ret
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
394 case VIRTIO_PCI_QUEUE_SEL
:
395 ret
= vdev
->queue_sel
;
397 case VIRTIO_PCI_STATUS
:
401 /* reading from the ISR also clears it. */
402 ret
= atomic_xchg(&vdev
->isr
, 0);
403 pci_irq_deassert(&proxy
->pci_dev
);
405 case VIRTIO_MSI_CONFIG_VECTOR
:
406 ret
= vdev
->config_vector
;
408 case VIRTIO_MSI_QUEUE_VECTOR
:
409 ret
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
418 static uint64_t virtio_pci_config_read(void *opaque
, hwaddr addr
,
421 VirtIOPCIProxy
*proxy
= opaque
;
422 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
423 uint32_t config
= VIRTIO_PCI_CONFIG_SIZE(&proxy
->pci_dev
);
426 return virtio_ioport_read(proxy
, addr
);
432 val
= virtio_config_readb(vdev
, addr
);
435 val
= virtio_config_readw(vdev
, addr
);
436 if (virtio_is_big_endian(vdev
)) {
441 val
= virtio_config_readl(vdev
, addr
);
442 if (virtio_is_big_endian(vdev
)) {
450 static void virtio_pci_config_write(void *opaque
, hwaddr addr
,
451 uint64_t val
, unsigned size
)
453 VirtIOPCIProxy
*proxy
= opaque
;
454 uint32_t config
= VIRTIO_PCI_CONFIG_SIZE(&proxy
->pci_dev
);
455 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
457 virtio_ioport_write(proxy
, addr
, val
);
462 * Virtio-PCI is odd. Ioports are LE but config space is target native
467 virtio_config_writeb(vdev
, addr
, val
);
470 if (virtio_is_big_endian(vdev
)) {
473 virtio_config_writew(vdev
, addr
, val
);
476 if (virtio_is_big_endian(vdev
)) {
479 virtio_config_writel(vdev
, addr
, val
);
484 static const MemoryRegionOps virtio_pci_config_ops
= {
485 .read
= virtio_pci_config_read
,
486 .write
= virtio_pci_config_write
,
488 .min_access_size
= 1,
489 .max_access_size
= 4,
491 .endianness
= DEVICE_LITTLE_ENDIAN
,
494 static MemoryRegion
*virtio_address_space_lookup(VirtIOPCIProxy
*proxy
,
495 hwaddr
*off
, int len
)
498 VirtIOPCIRegion
*reg
;
500 for (i
= 0; i
< ARRAY_SIZE(proxy
->regs
); ++i
) {
501 reg
= &proxy
->regs
[i
];
502 if (*off
>= reg
->offset
&&
503 *off
+ len
<= reg
->offset
+ reg
->size
) {
512 /* Below are generic functions to do memcpy from/to an address space,
513 * without byteswaps, with input validation.
515 * As regular address_space_* APIs all do some kind of byteswap at least for
516 * some host/target combinations, we are forced to explicitly convert to a
517 * known-endianness integer value.
518 * It doesn't really matter which endian format to go through, so the code
519 * below selects the endian that causes the least amount of work on the given
522 * Note: host pointer must be aligned.
525 void virtio_address_space_write(VirtIOPCIProxy
*proxy
, hwaddr addr
,
526 const uint8_t *buf
, int len
)
531 /* address_space_* APIs assume an aligned address.
532 * As address is under guest control, handle illegal values.
536 mr
= virtio_address_space_lookup(proxy
, &addr
, len
);
541 /* Make sure caller aligned buf properly */
542 assert(!(((uintptr_t)buf
) & (len
- 1)));
546 val
= pci_get_byte(buf
);
549 val
= cpu_to_le16(pci_get_word(buf
));
552 val
= cpu_to_le32(pci_get_long(buf
));
555 /* As length is under guest control, handle illegal values. */
558 memory_region_dispatch_write(mr
, addr
, val
, len
, MEMTXATTRS_UNSPECIFIED
);
562 virtio_address_space_read(VirtIOPCIProxy
*proxy
, hwaddr addr
,
563 uint8_t *buf
, int len
)
568 /* address_space_* APIs assume an aligned address.
569 * As address is under guest control, handle illegal values.
573 mr
= virtio_address_space_lookup(proxy
, &addr
, len
);
578 /* Make sure caller aligned buf properly */
579 assert(!(((uintptr_t)buf
) & (len
- 1)));
581 memory_region_dispatch_read(mr
, addr
, &val
, len
, MEMTXATTRS_UNSPECIFIED
);
584 pci_set_byte(buf
, val
);
587 pci_set_word(buf
, le16_to_cpu(val
));
590 pci_set_long(buf
, le32_to_cpu(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
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, 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 (range_covers_byte(address
, len
, PCI_COMMAND
) &&
608 !(pci_dev
->config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
609 virtio_pci_stop_ioeventfd(proxy
);
610 virtio_set_status(vdev
, vdev
->status
& ~VIRTIO_CONFIG_S_DRIVER_OK
);
613 if (proxy
->config_cap
&&
614 ranges_overlap(address
, len
, proxy
->config_cap
+ offsetof(struct virtio_pci_cfg_cap
,
616 sizeof cfg
->pci_cfg_data
)) {
620 cfg
= (void *)(proxy
->pci_dev
.config
+ proxy
->config_cap
);
621 off
= le32_to_cpu(cfg
->cap
.offset
);
622 len
= le32_to_cpu(cfg
->cap
.length
);
624 if (len
== 1 || len
== 2 || len
== 4) {
625 assert(len
<= sizeof cfg
->pci_cfg_data
);
626 virtio_address_space_write(proxy
, off
, cfg
->pci_cfg_data
, len
);
631 static uint32_t virtio_read_config(PCIDevice
*pci_dev
,
632 uint32_t address
, int len
)
634 VirtIOPCIProxy
*proxy
= DO_UPCAST(VirtIOPCIProxy
, pci_dev
, pci_dev
);
635 struct virtio_pci_cfg_cap
*cfg
;
637 if (proxy
->config_cap
&&
638 ranges_overlap(address
, len
, proxy
->config_cap
+ offsetof(struct virtio_pci_cfg_cap
,
640 sizeof cfg
->pci_cfg_data
)) {
644 cfg
= (void *)(proxy
->pci_dev
.config
+ proxy
->config_cap
);
645 off
= le32_to_cpu(cfg
->cap
.offset
);
646 len
= le32_to_cpu(cfg
->cap
.length
);
648 if (len
== 1 || len
== 2 || len
== 4) {
649 assert(len
<= sizeof cfg
->pci_cfg_data
);
650 virtio_address_space_read(proxy
, off
, cfg
->pci_cfg_data
, len
);
654 return pci_default_read_config(pci_dev
, address
, len
);
657 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy
*proxy
,
658 unsigned int queue_no
,
661 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
664 if (irqfd
->users
== 0) {
665 ret
= kvm_irqchip_add_msi_route(kvm_state
, vector
, &proxy
->pci_dev
);
675 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy
*proxy
,
678 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
679 if (--irqfd
->users
== 0) {
680 kvm_irqchip_release_virq(kvm_state
, irqfd
->virq
);
684 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy
*proxy
,
685 unsigned int queue_no
,
688 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
689 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
690 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
691 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
692 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, n
, NULL
, irqfd
->virq
);
695 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy
*proxy
,
696 unsigned int queue_no
,
699 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
700 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
701 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
702 VirtIOIRQFD
*irqfd
= &proxy
->vector_irqfd
[vector
];
705 ret
= kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state
, n
, irqfd
->virq
);
709 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy
*proxy
, int nvqs
)
711 PCIDevice
*dev
= &proxy
->pci_dev
;
712 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
713 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
717 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
718 if (!virtio_queue_get_num(vdev
, queue_no
)) {
721 vector
= virtio_queue_vector(vdev
, queue_no
);
722 if (vector
>= msix_nr_vectors_allocated(dev
)) {
725 ret
= kvm_virtio_pci_vq_vector_use(proxy
, queue_no
, vector
);
729 /* If guest supports masking, set up irqfd now.
730 * Otherwise, delay until unmasked in the frontend.
732 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
733 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
735 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
743 while (--queue_no
>= 0) {
744 vector
= virtio_queue_vector(vdev
, queue_no
);
745 if (vector
>= msix_nr_vectors_allocated(dev
)) {
748 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
749 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
751 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
756 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy
*proxy
, int nvqs
)
758 PCIDevice
*dev
= &proxy
->pci_dev
;
759 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
762 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
764 for (queue_no
= 0; queue_no
< nvqs
; queue_no
++) {
765 if (!virtio_queue_get_num(vdev
, queue_no
)) {
768 vector
= virtio_queue_vector(vdev
, queue_no
);
769 if (vector
>= msix_nr_vectors_allocated(dev
)) {
772 /* If guest supports masking, clean up irqfd now.
773 * Otherwise, it was cleaned when masked in the frontend.
775 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
776 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
778 kvm_virtio_pci_vq_vector_release(proxy
, vector
);
782 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy
*proxy
,
783 unsigned int queue_no
,
787 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
788 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
789 VirtQueue
*vq
= virtio_get_queue(vdev
, queue_no
);
790 EventNotifier
*n
= virtio_queue_get_guest_notifier(vq
);
794 if (proxy
->vector_irqfd
) {
795 irqfd
= &proxy
->vector_irqfd
[vector
];
796 if (irqfd
->msg
.data
!= msg
.data
|| irqfd
->msg
.address
!= msg
.address
) {
797 ret
= kvm_irqchip_update_msi_route(kvm_state
, irqfd
->virq
, msg
,
802 kvm_irqchip_commit_routes(kvm_state
);
806 /* If guest supports masking, irqfd is already setup, unmask it.
807 * Otherwise, set it up now.
809 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
810 k
->guest_notifier_mask(vdev
, queue_no
, false);
811 /* Test after unmasking to avoid losing events. */
812 if (k
->guest_notifier_pending
&&
813 k
->guest_notifier_pending(vdev
, queue_no
)) {
814 event_notifier_set(n
);
817 ret
= kvm_virtio_pci_irqfd_use(proxy
, queue_no
, vector
);
822 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy
*proxy
,
823 unsigned int queue_no
,
826 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
827 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
829 /* If guest supports masking, keep irqfd but mask it.
830 * Otherwise, clean it up now.
832 if (vdev
->use_guest_notifier_mask
&& k
->guest_notifier_mask
) {
833 k
->guest_notifier_mask(vdev
, queue_no
, true);
835 kvm_virtio_pci_irqfd_release(proxy
, queue_no
, vector
);
839 static int virtio_pci_vector_unmask(PCIDevice
*dev
, unsigned vector
,
842 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
843 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
844 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
845 int ret
, index
, unmasked
= 0;
848 index
= virtio_get_queue_index(vq
);
849 if (!virtio_queue_get_num(vdev
, index
)) {
852 if (index
< proxy
->nvqs_with_notifiers
) {
853 ret
= virtio_pci_vq_vector_unmask(proxy
, index
, vector
, msg
);
859 vq
= virtio_vector_next_queue(vq
);
865 vq
= virtio_vector_first_queue(vdev
, vector
);
866 while (vq
&& unmasked
>= 0) {
867 index
= virtio_get_queue_index(vq
);
868 if (index
< proxy
->nvqs_with_notifiers
) {
869 virtio_pci_vq_vector_mask(proxy
, index
, vector
);
872 vq
= virtio_vector_next_queue(vq
);
877 static void virtio_pci_vector_mask(PCIDevice
*dev
, unsigned vector
)
879 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
880 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
881 VirtQueue
*vq
= virtio_vector_first_queue(vdev
, vector
);
885 index
= virtio_get_queue_index(vq
);
886 if (!virtio_queue_get_num(vdev
, index
)) {
889 if (index
< proxy
->nvqs_with_notifiers
) {
890 virtio_pci_vq_vector_mask(proxy
, index
, vector
);
892 vq
= virtio_vector_next_queue(vq
);
896 static void virtio_pci_vector_poll(PCIDevice
*dev
,
897 unsigned int vector_start
,
898 unsigned int vector_end
)
900 VirtIOPCIProxy
*proxy
= container_of(dev
, VirtIOPCIProxy
, pci_dev
);
901 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
902 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
905 EventNotifier
*notifier
;
908 for (queue_no
= 0; queue_no
< proxy
->nvqs_with_notifiers
; queue_no
++) {
909 if (!virtio_queue_get_num(vdev
, queue_no
)) {
912 vector
= virtio_queue_vector(vdev
, queue_no
);
913 if (vector
< vector_start
|| vector
>= vector_end
||
914 !msix_is_masked(dev
, vector
)) {
917 vq
= virtio_get_queue(vdev
, queue_no
);
918 notifier
= virtio_queue_get_guest_notifier(vq
);
919 if (k
->guest_notifier_pending
) {
920 if (k
->guest_notifier_pending(vdev
, queue_no
)) {
921 msix_set_pending(dev
, vector
);
923 } else if (event_notifier_test_and_clear(notifier
)) {
924 msix_set_pending(dev
, vector
);
929 static int virtio_pci_set_guest_notifier(DeviceState
*d
, int n
, bool assign
,
932 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
933 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
934 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
935 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
936 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
939 int r
= event_notifier_init(notifier
, 0);
943 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
945 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
946 event_notifier_cleanup(notifier
);
949 if (!msix_enabled(&proxy
->pci_dev
) &&
950 vdev
->use_guest_notifier_mask
&&
951 vdc
->guest_notifier_mask
) {
952 vdc
->guest_notifier_mask(vdev
, n
, !assign
);
958 static bool virtio_pci_query_guest_notifiers(DeviceState
*d
)
960 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
961 return msix_enabled(&proxy
->pci_dev
);
964 static int virtio_pci_set_guest_notifiers(DeviceState
*d
, int nvqs
, bool assign
)
966 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
967 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
968 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
970 bool with_irqfd
= msix_enabled(&proxy
->pci_dev
) &&
971 kvm_msi_via_irqfd_enabled();
973 nvqs
= MIN(nvqs
, VIRTIO_QUEUE_MAX
);
975 /* When deassigning, pass a consistent nvqs value
976 * to avoid leaking notifiers.
978 assert(assign
|| nvqs
== proxy
->nvqs_with_notifiers
);
980 proxy
->nvqs_with_notifiers
= nvqs
;
982 /* Must unset vector notifier while guest notifier is still assigned */
983 if ((proxy
->vector_irqfd
|| k
->guest_notifier_mask
) && !assign
) {
984 msix_unset_vector_notifiers(&proxy
->pci_dev
);
985 if (proxy
->vector_irqfd
) {
986 kvm_virtio_pci_vector_release(proxy
, nvqs
);
987 g_free(proxy
->vector_irqfd
);
988 proxy
->vector_irqfd
= NULL
;
992 for (n
= 0; n
< nvqs
; n
++) {
993 if (!virtio_queue_get_num(vdev
, n
)) {
997 r
= virtio_pci_set_guest_notifier(d
, n
, assign
, with_irqfd
);
1003 /* Must set vector notifier after guest notifier has been assigned */
1004 if ((with_irqfd
|| k
->guest_notifier_mask
) && assign
) {
1006 proxy
->vector_irqfd
=
1007 g_malloc0(sizeof(*proxy
->vector_irqfd
) *
1008 msix_nr_vectors_allocated(&proxy
->pci_dev
));
1009 r
= kvm_virtio_pci_vector_use(proxy
, nvqs
);
1014 r
= msix_set_vector_notifiers(&proxy
->pci_dev
,
1015 virtio_pci_vector_unmask
,
1016 virtio_pci_vector_mask
,
1017 virtio_pci_vector_poll
);
1019 goto notifiers_error
;
1028 kvm_virtio_pci_vector_release(proxy
, nvqs
);
1032 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
1035 virtio_pci_set_guest_notifier(d
, n
, !assign
, with_irqfd
);
1040 static int virtio_pci_set_host_notifier_mr(DeviceState
*d
, int n
,
1041 MemoryRegion
*mr
, bool assign
)
1043 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1046 if (n
>= VIRTIO_QUEUE_MAX
|| !virtio_pci_modern(proxy
) ||
1047 virtio_pci_queue_mem_mult(proxy
) != memory_region_size(mr
)) {
1052 offset
= virtio_pci_queue_mem_mult(proxy
) * n
;
1053 memory_region_add_subregion_overlap(&proxy
->notify
.mr
, offset
, mr
, 1);
1055 memory_region_del_subregion(&proxy
->notify
.mr
, mr
);
1061 static void virtio_pci_vmstate_change(DeviceState
*d
, bool running
)
1063 VirtIOPCIProxy
*proxy
= to_virtio_pci_proxy(d
);
1064 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1067 /* Old QEMU versions did not set bus master enable on status write.
1068 * Detect DRIVER set and enable it.
1070 if ((proxy
->flags
& VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION
) &&
1071 (vdev
->status
& VIRTIO_CONFIG_S_DRIVER
) &&
1072 !(proxy
->pci_dev
.config
[PCI_COMMAND
] & PCI_COMMAND_MASTER
)) {
1073 pci_default_write_config(&proxy
->pci_dev
, PCI_COMMAND
,
1074 proxy
->pci_dev
.config
[PCI_COMMAND
] |
1075 PCI_COMMAND_MASTER
, 1);
1077 virtio_pci_start_ioeventfd(proxy
);
1079 virtio_pci_stop_ioeventfd(proxy
);
1083 #ifdef CONFIG_VIRTFS
1084 static void virtio_9p_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1086 V9fsPCIState
*dev
= VIRTIO_9P_PCI(vpci_dev
);
1087 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1089 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1090 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1093 static Property virtio_9p_pci_properties
[] = {
1094 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1095 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1096 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
1097 DEFINE_PROP_END_OF_LIST(),
1100 static void virtio_9p_pci_class_init(ObjectClass
*klass
, void *data
)
1102 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1103 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1104 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1106 k
->realize
= virtio_9p_pci_realize
;
1107 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1108 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_9P
;
1109 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1110 pcidev_k
->class_id
= 0x2;
1111 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1112 dc
->props
= virtio_9p_pci_properties
;
1115 static void virtio_9p_pci_instance_init(Object
*obj
)
1117 V9fsPCIState
*dev
= VIRTIO_9P_PCI(obj
);
1119 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1123 static const TypeInfo virtio_9p_pci_info
= {
1124 .name
= TYPE_VIRTIO_9P_PCI
,
1125 .parent
= TYPE_VIRTIO_PCI
,
1126 .instance_size
= sizeof(V9fsPCIState
),
1127 .instance_init
= virtio_9p_pci_instance_init
,
1128 .class_init
= virtio_9p_pci_class_init
,
1130 #endif /* CONFIG_VIRTFS */
1133 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
1136 static int virtio_pci_query_nvectors(DeviceState
*d
)
1138 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1140 return proxy
->nvectors
;
1143 static AddressSpace
*virtio_pci_get_dma_as(DeviceState
*d
)
1145 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1146 PCIDevice
*dev
= &proxy
->pci_dev
;
1148 return pci_get_address_space(dev
);
1151 static int virtio_pci_add_mem_cap(VirtIOPCIProxy
*proxy
,
1152 struct virtio_pci_cap
*cap
)
1154 PCIDevice
*dev
= &proxy
->pci_dev
;
1157 offset
= pci_add_capability(dev
, PCI_CAP_ID_VNDR
, 0,
1158 cap
->cap_len
, &error_abort
);
1160 assert(cap
->cap_len
>= sizeof *cap
);
1161 memcpy(dev
->config
+ offset
+ PCI_CAP_FLAGS
, &cap
->cap_len
,
1162 cap
->cap_len
- PCI_CAP_FLAGS
);
1167 static uint64_t virtio_pci_common_read(void *opaque
, hwaddr addr
,
1170 VirtIOPCIProxy
*proxy
= opaque
;
1171 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1176 case VIRTIO_PCI_COMMON_DFSELECT
:
1177 val
= proxy
->dfselect
;
1179 case VIRTIO_PCI_COMMON_DF
:
1180 if (proxy
->dfselect
<= 1) {
1181 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1183 val
= (vdev
->host_features
& ~vdc
->legacy_features
) >>
1184 (32 * proxy
->dfselect
);
1187 case VIRTIO_PCI_COMMON_GFSELECT
:
1188 val
= proxy
->gfselect
;
1190 case VIRTIO_PCI_COMMON_GF
:
1191 if (proxy
->gfselect
< ARRAY_SIZE(proxy
->guest_features
)) {
1192 val
= proxy
->guest_features
[proxy
->gfselect
];
1195 case VIRTIO_PCI_COMMON_MSIX
:
1196 val
= vdev
->config_vector
;
1198 case VIRTIO_PCI_COMMON_NUMQ
:
1199 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; ++i
) {
1200 if (virtio_queue_get_num(vdev
, i
)) {
1205 case VIRTIO_PCI_COMMON_STATUS
:
1208 case VIRTIO_PCI_COMMON_CFGGENERATION
:
1209 val
= vdev
->generation
;
1211 case VIRTIO_PCI_COMMON_Q_SELECT
:
1212 val
= vdev
->queue_sel
;
1214 case VIRTIO_PCI_COMMON_Q_SIZE
:
1215 val
= virtio_queue_get_num(vdev
, vdev
->queue_sel
);
1217 case VIRTIO_PCI_COMMON_Q_MSIX
:
1218 val
= virtio_queue_vector(vdev
, vdev
->queue_sel
);
1220 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1221 val
= proxy
->vqs
[vdev
->queue_sel
].enabled
;
1223 case VIRTIO_PCI_COMMON_Q_NOFF
:
1224 /* Simply map queues in order */
1225 val
= vdev
->queue_sel
;
1227 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1228 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[0];
1230 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1231 val
= proxy
->vqs
[vdev
->queue_sel
].desc
[1];
1233 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1234 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[0];
1236 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1237 val
= proxy
->vqs
[vdev
->queue_sel
].avail
[1];
1239 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1240 val
= proxy
->vqs
[vdev
->queue_sel
].used
[0];
1242 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1243 val
= proxy
->vqs
[vdev
->queue_sel
].used
[1];
1252 static void virtio_pci_common_write(void *opaque
, hwaddr addr
,
1253 uint64_t val
, unsigned size
)
1255 VirtIOPCIProxy
*proxy
= opaque
;
1256 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1259 case VIRTIO_PCI_COMMON_DFSELECT
:
1260 proxy
->dfselect
= val
;
1262 case VIRTIO_PCI_COMMON_GFSELECT
:
1263 proxy
->gfselect
= val
;
1265 case VIRTIO_PCI_COMMON_GF
:
1266 if (proxy
->gfselect
< ARRAY_SIZE(proxy
->guest_features
)) {
1267 proxy
->guest_features
[proxy
->gfselect
] = val
;
1268 virtio_set_features(vdev
,
1269 (((uint64_t)proxy
->guest_features
[1]) << 32) |
1270 proxy
->guest_features
[0]);
1273 case VIRTIO_PCI_COMMON_MSIX
:
1274 msix_vector_unuse(&proxy
->pci_dev
, vdev
->config_vector
);
1275 /* Make it possible for guest to discover an error took place. */
1276 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0) {
1277 val
= VIRTIO_NO_VECTOR
;
1279 vdev
->config_vector
= val
;
1281 case VIRTIO_PCI_COMMON_STATUS
:
1282 if (!(val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
1283 virtio_pci_stop_ioeventfd(proxy
);
1286 virtio_set_status(vdev
, val
& 0xFF);
1288 if (val
& VIRTIO_CONFIG_S_DRIVER_OK
) {
1289 virtio_pci_start_ioeventfd(proxy
);
1292 if (vdev
->status
== 0) {
1293 virtio_pci_reset(DEVICE(proxy
));
1297 case VIRTIO_PCI_COMMON_Q_SELECT
:
1298 if (val
< VIRTIO_QUEUE_MAX
) {
1299 vdev
->queue_sel
= val
;
1302 case VIRTIO_PCI_COMMON_Q_SIZE
:
1303 proxy
->vqs
[vdev
->queue_sel
].num
= val
;
1305 case VIRTIO_PCI_COMMON_Q_MSIX
:
1306 msix_vector_unuse(&proxy
->pci_dev
,
1307 virtio_queue_vector(vdev
, vdev
->queue_sel
));
1308 /* Make it possible for guest to discover an error took place. */
1309 if (msix_vector_use(&proxy
->pci_dev
, val
) < 0) {
1310 val
= VIRTIO_NO_VECTOR
;
1312 virtio_queue_set_vector(vdev
, vdev
->queue_sel
, val
);
1314 case VIRTIO_PCI_COMMON_Q_ENABLE
:
1315 virtio_queue_set_num(vdev
, vdev
->queue_sel
,
1316 proxy
->vqs
[vdev
->queue_sel
].num
);
1317 virtio_queue_set_rings(vdev
, vdev
->queue_sel
,
1318 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].desc
[1]) << 32 |
1319 proxy
->vqs
[vdev
->queue_sel
].desc
[0],
1320 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].avail
[1]) << 32 |
1321 proxy
->vqs
[vdev
->queue_sel
].avail
[0],
1322 ((uint64_t)proxy
->vqs
[vdev
->queue_sel
].used
[1]) << 32 |
1323 proxy
->vqs
[vdev
->queue_sel
].used
[0]);
1324 proxy
->vqs
[vdev
->queue_sel
].enabled
= 1;
1326 case VIRTIO_PCI_COMMON_Q_DESCLO
:
1327 proxy
->vqs
[vdev
->queue_sel
].desc
[0] = val
;
1329 case VIRTIO_PCI_COMMON_Q_DESCHI
:
1330 proxy
->vqs
[vdev
->queue_sel
].desc
[1] = val
;
1332 case VIRTIO_PCI_COMMON_Q_AVAILLO
:
1333 proxy
->vqs
[vdev
->queue_sel
].avail
[0] = val
;
1335 case VIRTIO_PCI_COMMON_Q_AVAILHI
:
1336 proxy
->vqs
[vdev
->queue_sel
].avail
[1] = val
;
1338 case VIRTIO_PCI_COMMON_Q_USEDLO
:
1339 proxy
->vqs
[vdev
->queue_sel
].used
[0] = val
;
1341 case VIRTIO_PCI_COMMON_Q_USEDHI
:
1342 proxy
->vqs
[vdev
->queue_sel
].used
[1] = val
;
1350 static uint64_t virtio_pci_notify_read(void *opaque
, hwaddr addr
,
1356 static void virtio_pci_notify_write(void *opaque
, hwaddr addr
,
1357 uint64_t val
, unsigned size
)
1359 VirtIODevice
*vdev
= opaque
;
1360 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(DEVICE(vdev
)->parent_bus
->parent
);
1361 unsigned queue
= addr
/ virtio_pci_queue_mem_mult(proxy
);
1363 if (queue
< VIRTIO_QUEUE_MAX
) {
1364 virtio_queue_notify(vdev
, queue
);
1368 static void virtio_pci_notify_write_pio(void *opaque
, hwaddr addr
,
1369 uint64_t val
, unsigned size
)
1371 VirtIODevice
*vdev
= opaque
;
1372 unsigned queue
= val
;
1374 if (queue
< VIRTIO_QUEUE_MAX
) {
1375 virtio_queue_notify(vdev
, queue
);
1379 static uint64_t virtio_pci_isr_read(void *opaque
, hwaddr addr
,
1382 VirtIOPCIProxy
*proxy
= opaque
;
1383 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1384 uint64_t val
= atomic_xchg(&vdev
->isr
, 0);
1385 pci_irq_deassert(&proxy
->pci_dev
);
1390 static void virtio_pci_isr_write(void *opaque
, hwaddr addr
,
1391 uint64_t val
, unsigned size
)
1395 static uint64_t virtio_pci_device_read(void *opaque
, hwaddr addr
,
1398 VirtIODevice
*vdev
= opaque
;
1403 val
= virtio_config_modern_readb(vdev
, addr
);
1406 val
= virtio_config_modern_readw(vdev
, addr
);
1409 val
= virtio_config_modern_readl(vdev
, addr
);
1415 static void virtio_pci_device_write(void *opaque
, hwaddr addr
,
1416 uint64_t val
, unsigned size
)
1418 VirtIODevice
*vdev
= opaque
;
1421 virtio_config_modern_writeb(vdev
, addr
, val
);
1424 virtio_config_modern_writew(vdev
, addr
, val
);
1427 virtio_config_modern_writel(vdev
, addr
, val
);
1432 static void virtio_pci_modern_regions_init(VirtIOPCIProxy
*proxy
)
1434 static const MemoryRegionOps common_ops
= {
1435 .read
= virtio_pci_common_read
,
1436 .write
= virtio_pci_common_write
,
1438 .min_access_size
= 1,
1439 .max_access_size
= 4,
1441 .endianness
= DEVICE_LITTLE_ENDIAN
,
1443 static const MemoryRegionOps isr_ops
= {
1444 .read
= virtio_pci_isr_read
,
1445 .write
= virtio_pci_isr_write
,
1447 .min_access_size
= 1,
1448 .max_access_size
= 4,
1450 .endianness
= DEVICE_LITTLE_ENDIAN
,
1452 static const MemoryRegionOps device_ops
= {
1453 .read
= virtio_pci_device_read
,
1454 .write
= virtio_pci_device_write
,
1456 .min_access_size
= 1,
1457 .max_access_size
= 4,
1459 .endianness
= DEVICE_LITTLE_ENDIAN
,
1461 static const MemoryRegionOps notify_ops
= {
1462 .read
= virtio_pci_notify_read
,
1463 .write
= virtio_pci_notify_write
,
1465 .min_access_size
= 1,
1466 .max_access_size
= 4,
1468 .endianness
= DEVICE_LITTLE_ENDIAN
,
1470 static const MemoryRegionOps notify_pio_ops
= {
1471 .read
= virtio_pci_notify_read
,
1472 .write
= virtio_pci_notify_write_pio
,
1474 .min_access_size
= 1,
1475 .max_access_size
= 4,
1477 .endianness
= DEVICE_LITTLE_ENDIAN
,
1481 memory_region_init_io(&proxy
->common
.mr
, OBJECT(proxy
),
1484 "virtio-pci-common",
1485 proxy
->common
.size
);
1487 memory_region_init_io(&proxy
->isr
.mr
, OBJECT(proxy
),
1493 memory_region_init_io(&proxy
->device
.mr
, OBJECT(proxy
),
1495 virtio_bus_get_device(&proxy
->bus
),
1496 "virtio-pci-device",
1497 proxy
->device
.size
);
1499 memory_region_init_io(&proxy
->notify
.mr
, OBJECT(proxy
),
1501 virtio_bus_get_device(&proxy
->bus
),
1502 "virtio-pci-notify",
1503 proxy
->notify
.size
);
1505 memory_region_init_io(&proxy
->notify_pio
.mr
, OBJECT(proxy
),
1507 virtio_bus_get_device(&proxy
->bus
),
1508 "virtio-pci-notify-pio",
1509 proxy
->notify_pio
.size
);
1512 static void virtio_pci_modern_region_map(VirtIOPCIProxy
*proxy
,
1513 VirtIOPCIRegion
*region
,
1514 struct virtio_pci_cap
*cap
,
1518 memory_region_add_subregion(mr
, region
->offset
, ®ion
->mr
);
1520 cap
->cfg_type
= region
->type
;
1522 cap
->offset
= cpu_to_le32(region
->offset
);
1523 cap
->length
= cpu_to_le32(region
->size
);
1524 virtio_pci_add_mem_cap(proxy
, cap
);
1528 static void virtio_pci_modern_mem_region_map(VirtIOPCIProxy
*proxy
,
1529 VirtIOPCIRegion
*region
,
1530 struct virtio_pci_cap
*cap
)
1532 virtio_pci_modern_region_map(proxy
, region
, cap
,
1533 &proxy
->modern_bar
, proxy
->modern_mem_bar_idx
);
1536 static void virtio_pci_modern_io_region_map(VirtIOPCIProxy
*proxy
,
1537 VirtIOPCIRegion
*region
,
1538 struct virtio_pci_cap
*cap
)
1540 virtio_pci_modern_region_map(proxy
, region
, cap
,
1541 &proxy
->io_bar
, proxy
->modern_io_bar_idx
);
1544 static void virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy
*proxy
,
1545 VirtIOPCIRegion
*region
)
1547 memory_region_del_subregion(&proxy
->modern_bar
,
1551 static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy
*proxy
,
1552 VirtIOPCIRegion
*region
)
1554 memory_region_del_subregion(&proxy
->io_bar
,
1558 static void virtio_pci_pre_plugged(DeviceState
*d
, Error
**errp
)
1560 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1561 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1563 if (virtio_pci_modern(proxy
)) {
1564 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_VERSION_1
);
1567 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_BAD_FEATURE
);
1570 /* This is called by virtio-bus just after the device is plugged. */
1571 static void virtio_pci_device_plugged(DeviceState
*d
, Error
**errp
)
1573 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1574 VirtioBusState
*bus
= &proxy
->bus
;
1575 bool legacy
= virtio_pci_legacy(proxy
);
1577 bool modern_pio
= proxy
->flags
& VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY
;
1580 VirtIODevice
*vdev
= virtio_bus_get_device(&proxy
->bus
);
1583 * Virtio capabilities present without
1584 * VIRTIO_F_VERSION_1 confuses guests
1586 if (!proxy
->ignore_backend_features
&&
1587 !virtio_has_feature(vdev
->host_features
, VIRTIO_F_VERSION_1
)) {
1588 virtio_pci_disable_modern(proxy
);
1591 error_setg(errp
, "Device doesn't support modern mode, and legacy"
1592 " mode is disabled");
1593 error_append_hint(errp
, "Set disable-legacy to off\n");
1599 modern
= virtio_pci_modern(proxy
);
1601 config
= proxy
->pci_dev
.config
;
1602 if (proxy
->class_code
) {
1603 pci_config_set_class(config
, proxy
->class_code
);
1607 if (virtio_host_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
)) {
1608 error_setg(errp
, "VIRTIO_F_IOMMU_PLATFORM was supported by"
1609 " neither legacy nor transitional device");
1613 * Legacy and transitional devices use specific subsystem IDs.
1614 * Note that the subsystem vendor ID (config + PCI_SUBSYSTEM_VENDOR_ID)
1615 * is set to PCI_SUBVENDOR_ID_REDHAT_QUMRANET by default.
1617 pci_set_word(config
+ PCI_SUBSYSTEM_ID
, virtio_bus_get_vdev_id(bus
));
1619 /* pure virtio-1.0 */
1620 pci_set_word(config
+ PCI_VENDOR_ID
,
1621 PCI_VENDOR_ID_REDHAT_QUMRANET
);
1622 pci_set_word(config
+ PCI_DEVICE_ID
,
1623 0x1040 + virtio_bus_get_vdev_id(bus
));
1624 pci_config_set_revision(config
, 1);
1626 config
[PCI_INTERRUPT_PIN
] = 1;
1630 struct virtio_pci_cap cap
= {
1631 .cap_len
= sizeof cap
,
1633 struct virtio_pci_notify_cap notify
= {
1634 .cap
.cap_len
= sizeof notify
,
1635 .notify_off_multiplier
=
1636 cpu_to_le32(virtio_pci_queue_mem_mult(proxy
)),
1638 struct virtio_pci_cfg_cap cfg
= {
1639 .cap
.cap_len
= sizeof cfg
,
1640 .cap
.cfg_type
= VIRTIO_PCI_CAP_PCI_CFG
,
1642 struct virtio_pci_notify_cap notify_pio
= {
1643 .cap
.cap_len
= sizeof notify
,
1644 .notify_off_multiplier
= cpu_to_le32(0x0),
1647 struct virtio_pci_cfg_cap
*cfg_mask
;
1649 virtio_pci_modern_regions_init(proxy
);
1651 virtio_pci_modern_mem_region_map(proxy
, &proxy
->common
, &cap
);
1652 virtio_pci_modern_mem_region_map(proxy
, &proxy
->isr
, &cap
);
1653 virtio_pci_modern_mem_region_map(proxy
, &proxy
->device
, &cap
);
1654 virtio_pci_modern_mem_region_map(proxy
, &proxy
->notify
, ¬ify
.cap
);
1657 memory_region_init(&proxy
->io_bar
, OBJECT(proxy
),
1658 "virtio-pci-io", 0x4);
1660 pci_register_bar(&proxy
->pci_dev
, proxy
->modern_io_bar_idx
,
1661 PCI_BASE_ADDRESS_SPACE_IO
, &proxy
->io_bar
);
1663 virtio_pci_modern_io_region_map(proxy
, &proxy
->notify_pio
,
1667 pci_register_bar(&proxy
->pci_dev
, proxy
->modern_mem_bar_idx
,
1668 PCI_BASE_ADDRESS_SPACE_MEMORY
|
1669 PCI_BASE_ADDRESS_MEM_PREFETCH
|
1670 PCI_BASE_ADDRESS_MEM_TYPE_64
,
1671 &proxy
->modern_bar
);
1673 proxy
->config_cap
= virtio_pci_add_mem_cap(proxy
, &cfg
.cap
);
1674 cfg_mask
= (void *)(proxy
->pci_dev
.wmask
+ proxy
->config_cap
);
1675 pci_set_byte(&cfg_mask
->cap
.bar
, ~0x0);
1676 pci_set_long((uint8_t *)&cfg_mask
->cap
.offset
, ~0x0);
1677 pci_set_long((uint8_t *)&cfg_mask
->cap
.length
, ~0x0);
1678 pci_set_long(cfg_mask
->pci_cfg_data
, ~0x0);
1681 if (proxy
->nvectors
) {
1682 int err
= msix_init_exclusive_bar(&proxy
->pci_dev
, proxy
->nvectors
,
1683 proxy
->msix_bar_idx
, NULL
);
1685 /* Notice when a system that supports MSIx can't initialize it */
1686 if (err
!= -ENOTSUP
) {
1687 error_report("unable to init msix vectors to %" PRIu32
,
1690 proxy
->nvectors
= 0;
1694 proxy
->pci_dev
.config_write
= virtio_write_config
;
1695 proxy
->pci_dev
.config_read
= virtio_read_config
;
1698 size
= VIRTIO_PCI_REGION_SIZE(&proxy
->pci_dev
)
1699 + virtio_bus_get_vdev_config_len(bus
);
1700 size
= pow2ceil(size
);
1702 memory_region_init_io(&proxy
->bar
, OBJECT(proxy
),
1703 &virtio_pci_config_ops
,
1704 proxy
, "virtio-pci", size
);
1706 pci_register_bar(&proxy
->pci_dev
, proxy
->legacy_io_bar_idx
,
1707 PCI_BASE_ADDRESS_SPACE_IO
, &proxy
->bar
);
1711 static void virtio_pci_device_unplugged(DeviceState
*d
)
1713 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(d
);
1714 bool modern
= virtio_pci_modern(proxy
);
1715 bool modern_pio
= proxy
->flags
& VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY
;
1717 virtio_pci_stop_ioeventfd(proxy
);
1720 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->common
);
1721 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->isr
);
1722 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->device
);
1723 virtio_pci_modern_mem_region_unmap(proxy
, &proxy
->notify
);
1725 virtio_pci_modern_io_region_unmap(proxy
, &proxy
->notify_pio
);
1730 static void virtio_pci_realize(PCIDevice
*pci_dev
, Error
**errp
)
1732 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(pci_dev
);
1733 VirtioPCIClass
*k
= VIRTIO_PCI_GET_CLASS(pci_dev
);
1734 bool pcie_port
= pci_bus_is_express(pci_get_bus(pci_dev
)) &&
1735 !pci_bus_is_root(pci_get_bus(pci_dev
));
1737 if (kvm_enabled() && !kvm_has_many_ioeventfds()) {
1738 proxy
->flags
&= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD
;
1742 * virtio pci bar layout used by default.
1743 * subclasses can re-arrange things if needed.
1745 * region 0 -- virtio legacy io bar
1746 * region 1 -- msi-x bar
1747 * region 4+5 -- virtio modern memory (64bit) bar
1750 proxy
->legacy_io_bar_idx
= 0;
1751 proxy
->msix_bar_idx
= 1;
1752 proxy
->modern_io_bar_idx
= 2;
1753 proxy
->modern_mem_bar_idx
= 4;
1755 proxy
->common
.offset
= 0x0;
1756 proxy
->common
.size
= 0x1000;
1757 proxy
->common
.type
= VIRTIO_PCI_CAP_COMMON_CFG
;
1759 proxy
->isr
.offset
= 0x1000;
1760 proxy
->isr
.size
= 0x1000;
1761 proxy
->isr
.type
= VIRTIO_PCI_CAP_ISR_CFG
;
1763 proxy
->device
.offset
= 0x2000;
1764 proxy
->device
.size
= 0x1000;
1765 proxy
->device
.type
= VIRTIO_PCI_CAP_DEVICE_CFG
;
1767 proxy
->notify
.offset
= 0x3000;
1768 proxy
->notify
.size
= virtio_pci_queue_mem_mult(proxy
) * VIRTIO_QUEUE_MAX
;
1769 proxy
->notify
.type
= VIRTIO_PCI_CAP_NOTIFY_CFG
;
1771 proxy
->notify_pio
.offset
= 0x0;
1772 proxy
->notify_pio
.size
= 0x4;
1773 proxy
->notify_pio
.type
= VIRTIO_PCI_CAP_NOTIFY_CFG
;
1775 /* subclasses can enforce modern, so do this unconditionally */
1776 memory_region_init(&proxy
->modern_bar
, OBJECT(proxy
), "virtio-pci",
1777 /* PCI BAR regions must be powers of 2 */
1778 pow2ceil(proxy
->notify
.offset
+ proxy
->notify
.size
));
1780 if (proxy
->disable_legacy
== ON_OFF_AUTO_AUTO
) {
1781 proxy
->disable_legacy
= pcie_port
? ON_OFF_AUTO_ON
: ON_OFF_AUTO_OFF
;
1784 if (!virtio_pci_modern(proxy
) && !virtio_pci_legacy(proxy
)) {
1785 error_setg(errp
, "device cannot work as neither modern nor legacy mode"
1787 error_append_hint(errp
, "Set either disable-modern or disable-legacy"
1792 if (pcie_port
&& pci_is_express(pci_dev
)) {
1795 pos
= pcie_endpoint_cap_init(pci_dev
, 0);
1798 pos
= pci_add_capability(pci_dev
, PCI_CAP_ID_PM
, 0,
1799 PCI_PM_SIZEOF
, errp
);
1804 pci_dev
->exp
.pm_cap
= pos
;
1807 * Indicates that this function complies with revision 1.2 of the
1808 * PCI Power Management Interface Specification.
1810 pci_set_word(pci_dev
->config
+ pos
+ PCI_PM_PMC
, 0x3);
1812 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_DEVERR
) {
1813 /* Init error enabling flags */
1814 pcie_cap_deverr_init(pci_dev
);
1817 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_LNKCTL
) {
1818 /* Init Link Control Register */
1819 pcie_cap_lnkctl_init(pci_dev
);
1822 if (proxy
->flags
& VIRTIO_PCI_FLAG_INIT_PM
) {
1823 /* Init Power Management Control Register */
1824 pci_set_word(pci_dev
->wmask
+ pos
+ PCI_PM_CTRL
,
1825 PCI_PM_CTRL_STATE_MASK
);
1828 if (proxy
->flags
& VIRTIO_PCI_FLAG_ATS
) {
1829 pcie_ats_init(pci_dev
, 256);
1834 * make future invocations of pci_is_express() return false
1835 * and pci_config_size() return PCI_CONFIG_SPACE_SIZE.
1837 pci_dev
->cap_present
&= ~QEMU_PCI_CAP_EXPRESS
;
1840 virtio_pci_bus_new(&proxy
->bus
, sizeof(proxy
->bus
), proxy
);
1842 k
->realize(proxy
, errp
);
1846 static void virtio_pci_exit(PCIDevice
*pci_dev
)
1848 msix_uninit_exclusive_bar(pci_dev
);
1851 static void virtio_pci_reset(DeviceState
*qdev
)
1853 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1854 VirtioBusState
*bus
= VIRTIO_BUS(&proxy
->bus
);
1855 PCIDevice
*dev
= PCI_DEVICE(qdev
);
1858 virtio_pci_stop_ioeventfd(proxy
);
1859 virtio_bus_reset(bus
);
1860 msix_unuse_all_vectors(&proxy
->pci_dev
);
1862 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1863 proxy
->vqs
[i
].enabled
= 0;
1864 proxy
->vqs
[i
].num
= 0;
1865 proxy
->vqs
[i
].desc
[0] = proxy
->vqs
[i
].desc
[1] = 0;
1866 proxy
->vqs
[i
].avail
[0] = proxy
->vqs
[i
].avail
[1] = 0;
1867 proxy
->vqs
[i
].used
[0] = proxy
->vqs
[i
].used
[1] = 0;
1870 if (pci_is_express(dev
)) {
1871 pcie_cap_deverr_reset(dev
);
1872 pcie_cap_lnkctl_reset(dev
);
1874 pci_set_word(dev
->config
+ dev
->exp
.pm_cap
+ PCI_PM_CTRL
, 0);
1878 static Property virtio_pci_properties
[] = {
1879 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy
, flags
,
1880 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT
, false),
1881 DEFINE_PROP_ON_OFF_AUTO("disable-legacy", VirtIOPCIProxy
, disable_legacy
,
1883 DEFINE_PROP_BOOL("disable-modern", VirtIOPCIProxy
, disable_modern
, false),
1884 DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy
, flags
,
1885 VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT
, true),
1886 DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy
, flags
,
1887 VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT
, false),
1888 DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy
, flags
,
1889 VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT
, false),
1890 DEFINE_PROP_BIT("page-per-vq", VirtIOPCIProxy
, flags
,
1891 VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT
, false),
1892 DEFINE_PROP_BOOL("x-ignore-backend-features", VirtIOPCIProxy
,
1893 ignore_backend_features
, false),
1894 DEFINE_PROP_BIT("ats", VirtIOPCIProxy
, flags
,
1895 VIRTIO_PCI_FLAG_ATS_BIT
, false),
1896 DEFINE_PROP_BIT("x-pcie-deverr-init", VirtIOPCIProxy
, flags
,
1897 VIRTIO_PCI_FLAG_INIT_DEVERR_BIT
, true),
1898 DEFINE_PROP_BIT("x-pcie-lnkctl-init", VirtIOPCIProxy
, flags
,
1899 VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT
, true),
1900 DEFINE_PROP_BIT("x-pcie-pm-init", VirtIOPCIProxy
, flags
,
1901 VIRTIO_PCI_FLAG_INIT_PM_BIT
, true),
1902 DEFINE_PROP_END_OF_LIST(),
1905 static void virtio_pci_dc_realize(DeviceState
*qdev
, Error
**errp
)
1907 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_GET_CLASS(qdev
);
1908 VirtIOPCIProxy
*proxy
= VIRTIO_PCI(qdev
);
1909 PCIDevice
*pci_dev
= &proxy
->pci_dev
;
1911 if (!(proxy
->flags
& VIRTIO_PCI_FLAG_DISABLE_PCIE
) &&
1912 virtio_pci_modern(proxy
)) {
1913 pci_dev
->cap_present
|= QEMU_PCI_CAP_EXPRESS
;
1916 vpciklass
->parent_dc_realize(qdev
, errp
);
1919 static void virtio_pci_class_init(ObjectClass
*klass
, void *data
)
1921 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1922 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1923 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_CLASS(klass
);
1925 dc
->props
= virtio_pci_properties
;
1926 k
->realize
= virtio_pci_realize
;
1927 k
->exit
= virtio_pci_exit
;
1928 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1929 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1930 k
->class_id
= PCI_CLASS_OTHERS
;
1931 device_class_set_parent_realize(dc
, virtio_pci_dc_realize
,
1932 &vpciklass
->parent_dc_realize
);
1933 dc
->reset
= virtio_pci_reset
;
1936 static const TypeInfo virtio_pci_info
= {
1937 .name
= TYPE_VIRTIO_PCI
,
1938 .parent
= TYPE_PCI_DEVICE
,
1939 .instance_size
= sizeof(VirtIOPCIProxy
),
1940 .class_init
= virtio_pci_class_init
,
1941 .class_size
= sizeof(VirtioPCIClass
),
1943 .interfaces
= (InterfaceInfo
[]) {
1944 { INTERFACE_PCIE_DEVICE
},
1945 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
1950 /* virtio-blk-pci */
1952 static Property virtio_blk_pci_properties
[] = {
1953 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
1954 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
1955 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
1956 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
1957 DEV_NVECTORS_UNSPECIFIED
),
1958 DEFINE_PROP_END_OF_LIST(),
1961 static void virtio_blk_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
1963 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(vpci_dev
);
1964 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1966 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
1967 vpci_dev
->nvectors
= dev
->vdev
.conf
.num_queues
+ 1;
1970 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
1971 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1974 static void virtio_blk_pci_class_init(ObjectClass
*klass
, void *data
)
1976 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1977 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
1978 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
1980 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1981 dc
->props
= virtio_blk_pci_properties
;
1982 k
->realize
= virtio_blk_pci_realize
;
1983 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
1984 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
1985 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
1986 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1989 static void virtio_blk_pci_instance_init(Object
*obj
)
1991 VirtIOBlkPCI
*dev
= VIRTIO_BLK_PCI(obj
);
1993 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1995 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
1996 "bootindex", &error_abort
);
1999 static const TypeInfo virtio_blk_pci_info
= {
2000 .name
= TYPE_VIRTIO_BLK_PCI
,
2001 .parent
= TYPE_VIRTIO_PCI
,
2002 .instance_size
= sizeof(VirtIOBlkPCI
),
2003 .instance_init
= virtio_blk_pci_instance_init
,
2004 .class_init
= virtio_blk_pci_class_init
,
2007 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2008 /* vhost-user-blk */
2010 static Property vhost_user_blk_pci_properties
[] = {
2011 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
2012 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
2013 DEV_NVECTORS_UNSPECIFIED
),
2014 DEFINE_PROP_END_OF_LIST(),
2017 static void vhost_user_blk_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2019 VHostUserBlkPCI
*dev
= VHOST_USER_BLK_PCI(vpci_dev
);
2020 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
2022 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
2023 vpci_dev
->nvectors
= dev
->vdev
.num_queues
+ 1;
2026 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2027 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2030 static void vhost_user_blk_pci_class_init(ObjectClass
*klass
, void *data
)
2032 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2033 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2034 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2036 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
2037 dc
->props
= vhost_user_blk_pci_properties
;
2038 k
->realize
= vhost_user_blk_pci_realize
;
2039 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2040 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BLOCK
;
2041 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
2042 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
2045 static void vhost_user_blk_pci_instance_init(Object
*obj
)
2047 VHostUserBlkPCI
*dev
= VHOST_USER_BLK_PCI(obj
);
2049 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2050 TYPE_VHOST_USER_BLK
);
2051 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
2052 "bootindex", &error_abort
);
2055 static const TypeInfo vhost_user_blk_pci_info
= {
2056 .name
= TYPE_VHOST_USER_BLK_PCI
,
2057 .parent
= TYPE_VIRTIO_PCI
,
2058 .instance_size
= sizeof(VHostUserBlkPCI
),
2059 .instance_init
= vhost_user_blk_pci_instance_init
,
2060 .class_init
= vhost_user_blk_pci_class_init
,
2064 /* virtio-scsi-pci */
2066 static Property virtio_scsi_pci_properties
[] = {
2067 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
2068 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
2069 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
2070 DEV_NVECTORS_UNSPECIFIED
),
2071 DEFINE_PROP_END_OF_LIST(),
2074 static void virtio_scsi_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2076 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(vpci_dev
);
2077 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
2078 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
2079 DeviceState
*proxy
= DEVICE(vpci_dev
);
2082 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
2083 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
2087 * For command line compatibility, this sets the virtio-scsi-device bus
2091 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
2092 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
2096 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2097 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2100 static void virtio_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
2102 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2103 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2104 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2106 k
->realize
= virtio_scsi_pci_realize
;
2107 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
2108 dc
->props
= virtio_scsi_pci_properties
;
2109 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2110 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
2111 pcidev_k
->revision
= 0x00;
2112 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
2115 static void virtio_scsi_pci_instance_init(Object
*obj
)
2117 VirtIOSCSIPCI
*dev
= VIRTIO_SCSI_PCI(obj
);
2119 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2123 static const TypeInfo virtio_scsi_pci_info
= {
2124 .name
= TYPE_VIRTIO_SCSI_PCI
,
2125 .parent
= TYPE_VIRTIO_PCI
,
2126 .instance_size
= sizeof(VirtIOSCSIPCI
),
2127 .instance_init
= virtio_scsi_pci_instance_init
,
2128 .class_init
= virtio_scsi_pci_class_init
,
2131 /* vhost-scsi-pci */
2133 #ifdef CONFIG_VHOST_SCSI
2134 static Property vhost_scsi_pci_properties
[] = {
2135 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
2136 DEV_NVECTORS_UNSPECIFIED
),
2137 DEFINE_PROP_END_OF_LIST(),
2140 static void vhost_scsi_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2142 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(vpci_dev
);
2143 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
2144 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
2146 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
2147 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
2150 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2151 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2154 static void vhost_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
2156 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2157 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2158 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2159 k
->realize
= vhost_scsi_pci_realize
;
2160 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
2161 dc
->props
= vhost_scsi_pci_properties
;
2162 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2163 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
2164 pcidev_k
->revision
= 0x00;
2165 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
2168 static void vhost_scsi_pci_instance_init(Object
*obj
)
2170 VHostSCSIPCI
*dev
= VHOST_SCSI_PCI(obj
);
2172 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2174 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
2175 "bootindex", &error_abort
);
2178 static const TypeInfo vhost_scsi_pci_info
= {
2179 .name
= TYPE_VHOST_SCSI_PCI
,
2180 .parent
= TYPE_VIRTIO_PCI
,
2181 .instance_size
= sizeof(VHostSCSIPCI
),
2182 .instance_init
= vhost_scsi_pci_instance_init
,
2183 .class_init
= vhost_scsi_pci_class_init
,
2187 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2188 /* vhost-user-scsi-pci */
2189 static Property vhost_user_scsi_pci_properties
[] = {
2190 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
,
2191 DEV_NVECTORS_UNSPECIFIED
),
2192 DEFINE_PROP_END_OF_LIST(),
2195 static void vhost_user_scsi_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2197 VHostUserSCSIPCI
*dev
= VHOST_USER_SCSI_PCI(vpci_dev
);
2198 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
2199 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
2201 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
2202 vpci_dev
->nvectors
= vs
->conf
.num_queues
+ 3;
2205 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2206 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2209 static void vhost_user_scsi_pci_class_init(ObjectClass
*klass
, void *data
)
2211 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2212 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2213 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2214 k
->realize
= vhost_user_scsi_pci_realize
;
2215 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
2216 dc
->props
= vhost_user_scsi_pci_properties
;
2217 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2218 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_SCSI
;
2219 pcidev_k
->revision
= 0x00;
2220 pcidev_k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
2223 static void vhost_user_scsi_pci_instance_init(Object
*obj
)
2225 VHostUserSCSIPCI
*dev
= VHOST_USER_SCSI_PCI(obj
);
2227 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2228 TYPE_VHOST_USER_SCSI
);
2229 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
2230 "bootindex", &error_abort
);
2233 static const TypeInfo vhost_user_scsi_pci_info
= {
2234 .name
= TYPE_VHOST_USER_SCSI_PCI
,
2235 .parent
= TYPE_VIRTIO_PCI
,
2236 .instance_size
= sizeof(VHostUserSCSIPCI
),
2237 .instance_init
= vhost_user_scsi_pci_instance_init
,
2238 .class_init
= vhost_user_scsi_pci_class_init
,
2242 /* vhost-vsock-pci */
2244 #ifdef CONFIG_VHOST_VSOCK
2245 static Property vhost_vsock_pci_properties
[] = {
2246 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
2247 DEFINE_PROP_END_OF_LIST(),
2250 static void vhost_vsock_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2252 VHostVSockPCI
*dev
= VHOST_VSOCK_PCI(vpci_dev
);
2253 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
2255 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2256 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2259 static void vhost_vsock_pci_class_init(ObjectClass
*klass
, void *data
)
2261 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2262 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2263 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2264 k
->realize
= vhost_vsock_pci_realize
;
2265 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
2266 dc
->props
= vhost_vsock_pci_properties
;
2267 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2268 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_VSOCK
;
2269 pcidev_k
->revision
= 0x00;
2270 pcidev_k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
2273 static void vhost_vsock_pci_instance_init(Object
*obj
)
2275 VHostVSockPCI
*dev
= VHOST_VSOCK_PCI(obj
);
2277 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2281 static const TypeInfo vhost_vsock_pci_info
= {
2282 .name
= TYPE_VHOST_VSOCK_PCI
,
2283 .parent
= TYPE_VIRTIO_PCI
,
2284 .instance_size
= sizeof(VHostVSockPCI
),
2285 .instance_init
= vhost_vsock_pci_instance_init
,
2286 .class_init
= vhost_vsock_pci_class_init
,
2290 /* virtio-balloon-pci */
2292 static Property virtio_balloon_pci_properties
[] = {
2293 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
2294 DEFINE_PROP_END_OF_LIST(),
2297 static void virtio_balloon_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2299 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(vpci_dev
);
2300 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
2302 if (vpci_dev
->class_code
!= PCI_CLASS_OTHERS
&&
2303 vpci_dev
->class_code
!= PCI_CLASS_MEMORY_RAM
) { /* qemu < 1.1 */
2304 vpci_dev
->class_code
= PCI_CLASS_OTHERS
;
2307 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2308 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2311 static void virtio_balloon_pci_class_init(ObjectClass
*klass
, void *data
)
2313 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2314 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2315 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2316 k
->realize
= virtio_balloon_pci_realize
;
2317 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
2318 dc
->props
= virtio_balloon_pci_properties
;
2319 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2320 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_BALLOON
;
2321 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
2322 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
2325 static void virtio_balloon_pci_instance_init(Object
*obj
)
2327 VirtIOBalloonPCI
*dev
= VIRTIO_BALLOON_PCI(obj
);
2329 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2330 TYPE_VIRTIO_BALLOON
);
2331 object_property_add_alias(obj
, "guest-stats", OBJECT(&dev
->vdev
),
2332 "guest-stats", &error_abort
);
2333 object_property_add_alias(obj
, "guest-stats-polling-interval",
2335 "guest-stats-polling-interval", &error_abort
);
2338 static const TypeInfo virtio_balloon_pci_info
= {
2339 .name
= TYPE_VIRTIO_BALLOON_PCI
,
2340 .parent
= TYPE_VIRTIO_PCI
,
2341 .instance_size
= sizeof(VirtIOBalloonPCI
),
2342 .instance_init
= virtio_balloon_pci_instance_init
,
2343 .class_init
= virtio_balloon_pci_class_init
,
2346 /* virtio-serial-pci */
2348 static void virtio_serial_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2350 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(vpci_dev
);
2351 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
2352 DeviceState
*proxy
= DEVICE(vpci_dev
);
2355 if (vpci_dev
->class_code
!= PCI_CLASS_COMMUNICATION_OTHER
&&
2356 vpci_dev
->class_code
!= PCI_CLASS_DISPLAY_OTHER
&& /* qemu 0.10 */
2357 vpci_dev
->class_code
!= PCI_CLASS_OTHERS
) { /* qemu-kvm */
2358 vpci_dev
->class_code
= PCI_CLASS_COMMUNICATION_OTHER
;
2361 /* backwards-compatibility with machines that were created with
2362 DEV_NVECTORS_UNSPECIFIED */
2363 if (vpci_dev
->nvectors
== DEV_NVECTORS_UNSPECIFIED
) {
2364 vpci_dev
->nvectors
= dev
->vdev
.serial
.max_virtserial_ports
+ 1;
2368 * For command line compatibility, this sets the virtio-serial-device bus
2372 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
2373 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
2377 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2378 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2381 static Property virtio_serial_pci_properties
[] = {
2382 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
2383 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
2384 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
2385 DEFINE_PROP_UINT32("class", VirtIOPCIProxy
, class_code
, 0),
2386 DEFINE_PROP_END_OF_LIST(),
2389 static void virtio_serial_pci_class_init(ObjectClass
*klass
, void *data
)
2391 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2392 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2393 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2394 k
->realize
= virtio_serial_pci_realize
;
2395 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
2396 dc
->props
= virtio_serial_pci_properties
;
2397 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2398 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_CONSOLE
;
2399 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
2400 pcidev_k
->class_id
= PCI_CLASS_COMMUNICATION_OTHER
;
2403 static void virtio_serial_pci_instance_init(Object
*obj
)
2405 VirtIOSerialPCI
*dev
= VIRTIO_SERIAL_PCI(obj
);
2407 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2408 TYPE_VIRTIO_SERIAL
);
2411 static const TypeInfo virtio_serial_pci_info
= {
2412 .name
= TYPE_VIRTIO_SERIAL_PCI
,
2413 .parent
= TYPE_VIRTIO_PCI
,
2414 .instance_size
= sizeof(VirtIOSerialPCI
),
2415 .instance_init
= virtio_serial_pci_instance_init
,
2416 .class_init
= virtio_serial_pci_class_init
,
2419 /* virtio-net-pci */
2421 static Property virtio_net_properties
[] = {
2422 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy
, flags
,
2423 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT
, true),
2424 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 3),
2425 DEFINE_PROP_END_OF_LIST(),
2428 static void virtio_net_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2430 DeviceState
*qdev
= DEVICE(vpci_dev
);
2431 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(vpci_dev
);
2432 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
2434 virtio_net_set_netclient_name(&dev
->vdev
, qdev
->id
,
2435 object_get_typename(OBJECT(qdev
)));
2436 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2437 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2440 static void virtio_net_pci_class_init(ObjectClass
*klass
, void *data
)
2442 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2443 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2444 VirtioPCIClass
*vpciklass
= VIRTIO_PCI_CLASS(klass
);
2446 k
->romfile
= "efi-virtio.rom";
2447 k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2448 k
->device_id
= PCI_DEVICE_ID_VIRTIO_NET
;
2449 k
->revision
= VIRTIO_PCI_ABI_VERSION
;
2450 k
->class_id
= PCI_CLASS_NETWORK_ETHERNET
;
2451 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
2452 dc
->props
= virtio_net_properties
;
2453 vpciklass
->realize
= virtio_net_pci_realize
;
2456 static void virtio_net_pci_instance_init(Object
*obj
)
2458 VirtIONetPCI
*dev
= VIRTIO_NET_PCI(obj
);
2460 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2462 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
2463 "bootindex", &error_abort
);
2466 static const TypeInfo virtio_net_pci_info
= {
2467 .name
= TYPE_VIRTIO_NET_PCI
,
2468 .parent
= TYPE_VIRTIO_PCI
,
2469 .instance_size
= sizeof(VirtIONetPCI
),
2470 .instance_init
= virtio_net_pci_instance_init
,
2471 .class_init
= virtio_net_pci_class_init
,
2474 /* virtio-rng-pci */
2476 static void virtio_rng_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2478 VirtIORngPCI
*vrng
= VIRTIO_RNG_PCI(vpci_dev
);
2479 DeviceState
*vdev
= DEVICE(&vrng
->vdev
);
2482 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2483 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
2485 error_propagate(errp
, err
);
2489 object_property_set_link(OBJECT(vrng
),
2490 OBJECT(vrng
->vdev
.conf
.rng
), "rng",
2494 static void virtio_rng_pci_class_init(ObjectClass
*klass
, void *data
)
2496 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2497 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2498 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2500 k
->realize
= virtio_rng_pci_realize
;
2501 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
2503 pcidev_k
->vendor_id
= PCI_VENDOR_ID_REDHAT_QUMRANET
;
2504 pcidev_k
->device_id
= PCI_DEVICE_ID_VIRTIO_RNG
;
2505 pcidev_k
->revision
= VIRTIO_PCI_ABI_VERSION
;
2506 pcidev_k
->class_id
= PCI_CLASS_OTHERS
;
2509 static void virtio_rng_initfn(Object
*obj
)
2511 VirtIORngPCI
*dev
= VIRTIO_RNG_PCI(obj
);
2513 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2517 static const TypeInfo virtio_rng_pci_info
= {
2518 .name
= TYPE_VIRTIO_RNG_PCI
,
2519 .parent
= TYPE_VIRTIO_PCI
,
2520 .instance_size
= sizeof(VirtIORngPCI
),
2521 .instance_init
= virtio_rng_initfn
,
2522 .class_init
= virtio_rng_pci_class_init
,
2525 /* virtio-input-pci */
2527 static Property virtio_input_pci_properties
[] = {
2528 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy
, nvectors
, 2),
2529 DEFINE_PROP_END_OF_LIST(),
2532 static void virtio_input_pci_realize(VirtIOPCIProxy
*vpci_dev
, Error
**errp
)
2534 VirtIOInputPCI
*vinput
= VIRTIO_INPUT_PCI(vpci_dev
);
2535 DeviceState
*vdev
= DEVICE(&vinput
->vdev
);
2537 qdev_set_parent_bus(vdev
, BUS(&vpci_dev
->bus
));
2538 virtio_pci_force_virtio_1(vpci_dev
);
2539 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
2542 static void virtio_input_pci_class_init(ObjectClass
*klass
, void *data
)
2544 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2545 VirtioPCIClass
*k
= VIRTIO_PCI_CLASS(klass
);
2546 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2548 dc
->props
= virtio_input_pci_properties
;
2549 k
->realize
= virtio_input_pci_realize
;
2550 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
2552 pcidev_k
->class_id
= PCI_CLASS_INPUT_OTHER
;
2555 static void virtio_input_hid_kbd_pci_class_init(ObjectClass
*klass
, void *data
)
2557 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2559 pcidev_k
->class_id
= PCI_CLASS_INPUT_KEYBOARD
;
2562 static void virtio_input_hid_mouse_pci_class_init(ObjectClass
*klass
,
2565 PCIDeviceClass
*pcidev_k
= PCI_DEVICE_CLASS(klass
);
2567 pcidev_k
->class_id
= PCI_CLASS_INPUT_MOUSE
;
2570 static void virtio_keyboard_initfn(Object
*obj
)
2572 VirtIOInputHIDPCI
*dev
= VIRTIO_INPUT_HID_PCI(obj
);
2574 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2575 TYPE_VIRTIO_KEYBOARD
);
2578 static void virtio_mouse_initfn(Object
*obj
)
2580 VirtIOInputHIDPCI
*dev
= VIRTIO_INPUT_HID_PCI(obj
);
2582 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2586 static void virtio_tablet_initfn(Object
*obj
)
2588 VirtIOInputHIDPCI
*dev
= VIRTIO_INPUT_HID_PCI(obj
);
2590 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2591 TYPE_VIRTIO_TABLET
);
2594 static const TypeInfo virtio_input_pci_info
= {
2595 .name
= TYPE_VIRTIO_INPUT_PCI
,
2596 .parent
= TYPE_VIRTIO_PCI
,
2597 .instance_size
= sizeof(VirtIOInputPCI
),
2598 .class_init
= virtio_input_pci_class_init
,
2602 static const TypeInfo virtio_input_hid_pci_info
= {
2603 .name
= TYPE_VIRTIO_INPUT_HID_PCI
,
2604 .parent
= TYPE_VIRTIO_INPUT_PCI
,
2605 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2609 static const TypeInfo virtio_keyboard_pci_info
= {
2610 .name
= TYPE_VIRTIO_KEYBOARD_PCI
,
2611 .parent
= TYPE_VIRTIO_INPUT_HID_PCI
,
2612 .class_init
= virtio_input_hid_kbd_pci_class_init
,
2613 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2614 .instance_init
= virtio_keyboard_initfn
,
2617 static const TypeInfo virtio_mouse_pci_info
= {
2618 .name
= TYPE_VIRTIO_MOUSE_PCI
,
2619 .parent
= TYPE_VIRTIO_INPUT_HID_PCI
,
2620 .class_init
= virtio_input_hid_mouse_pci_class_init
,
2621 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2622 .instance_init
= virtio_mouse_initfn
,
2625 static const TypeInfo virtio_tablet_pci_info
= {
2626 .name
= TYPE_VIRTIO_TABLET_PCI
,
2627 .parent
= TYPE_VIRTIO_INPUT_HID_PCI
,
2628 .instance_size
= sizeof(VirtIOInputHIDPCI
),
2629 .instance_init
= virtio_tablet_initfn
,
2633 static void virtio_host_initfn(Object
*obj
)
2635 VirtIOInputHostPCI
*dev
= VIRTIO_INPUT_HOST_PCI(obj
);
2637 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
2638 TYPE_VIRTIO_INPUT_HOST
);
2641 static const TypeInfo virtio_host_pci_info
= {
2642 .name
= TYPE_VIRTIO_INPUT_HOST_PCI
,
2643 .parent
= TYPE_VIRTIO_INPUT_PCI
,
2644 .instance_size
= sizeof(VirtIOInputHostPCI
),
2645 .instance_init
= virtio_host_initfn
,
2649 /* virtio-pci-bus */
2651 static void virtio_pci_bus_new(VirtioBusState
*bus
, size_t bus_size
,
2652 VirtIOPCIProxy
*dev
)
2654 DeviceState
*qdev
= DEVICE(dev
);
2655 char virtio_bus_name
[] = "virtio-bus";
2657 qbus_create_inplace(bus
, bus_size
, TYPE_VIRTIO_PCI_BUS
, qdev
,
2661 static void virtio_pci_bus_class_init(ObjectClass
*klass
, void *data
)
2663 BusClass
*bus_class
= BUS_CLASS(klass
);
2664 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
2665 bus_class
->max_dev
= 1;
2666 k
->notify
= virtio_pci_notify
;
2667 k
->save_config
= virtio_pci_save_config
;
2668 k
->load_config
= virtio_pci_load_config
;
2669 k
->save_queue
= virtio_pci_save_queue
;
2670 k
->load_queue
= virtio_pci_load_queue
;
2671 k
->save_extra_state
= virtio_pci_save_extra_state
;
2672 k
->load_extra_state
= virtio_pci_load_extra_state
;
2673 k
->has_extra_state
= virtio_pci_has_extra_state
;
2674 k
->query_guest_notifiers
= virtio_pci_query_guest_notifiers
;
2675 k
->set_guest_notifiers
= virtio_pci_set_guest_notifiers
;
2676 k
->set_host_notifier_mr
= virtio_pci_set_host_notifier_mr
;
2677 k
->vmstate_change
= virtio_pci_vmstate_change
;
2678 k
->pre_plugged
= virtio_pci_pre_plugged
;
2679 k
->device_plugged
= virtio_pci_device_plugged
;
2680 k
->device_unplugged
= virtio_pci_device_unplugged
;
2681 k
->query_nvectors
= virtio_pci_query_nvectors
;
2682 k
->ioeventfd_enabled
= virtio_pci_ioeventfd_enabled
;
2683 k
->ioeventfd_assign
= virtio_pci_ioeventfd_assign
;
2684 k
->get_dma_as
= virtio_pci_get_dma_as
;
2687 static const TypeInfo virtio_pci_bus_info
= {
2688 .name
= TYPE_VIRTIO_PCI_BUS
,
2689 .parent
= TYPE_VIRTIO_BUS
,
2690 .instance_size
= sizeof(VirtioPCIBusState
),
2691 .class_init
= virtio_pci_bus_class_init
,
2694 static void virtio_pci_register_types(void)
2696 type_register_static(&virtio_rng_pci_info
);
2697 type_register_static(&virtio_input_pci_info
);
2698 type_register_static(&virtio_input_hid_pci_info
);
2699 type_register_static(&virtio_keyboard_pci_info
);
2700 type_register_static(&virtio_mouse_pci_info
);
2701 type_register_static(&virtio_tablet_pci_info
);
2703 type_register_static(&virtio_host_pci_info
);
2705 type_register_static(&virtio_pci_bus_info
);
2706 type_register_static(&virtio_pci_info
);
2707 #ifdef CONFIG_VIRTFS
2708 type_register_static(&virtio_9p_pci_info
);
2710 type_register_static(&virtio_blk_pci_info
);
2711 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2712 type_register_static(&vhost_user_blk_pci_info
);
2714 type_register_static(&virtio_scsi_pci_info
);
2715 type_register_static(&virtio_balloon_pci_info
);
2716 type_register_static(&virtio_serial_pci_info
);
2717 type_register_static(&virtio_net_pci_info
);
2718 #ifdef CONFIG_VHOST_SCSI
2719 type_register_static(&vhost_scsi_pci_info
);
2721 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2722 type_register_static(&vhost_user_scsi_pci_info
);
2724 #ifdef CONFIG_VHOST_VSOCK
2725 type_register_static(&vhost_vsock_pci_info
);
2729 type_init(virtio_pci_register_types
)