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 void virtio_pci_load_modern_queue_state(VirtIOPCIQueue *vq,
94 vq->num = qemu_get_be16(f);
95 vq->enabled = qemu_get_be16(f);
96 vq->desc[0] = qemu_get_be32(f);
97 vq->desc[1] = qemu_get_be32(f);
98 vq->avail[0] = qemu_get_be32(f);
99 vq->avail[1] = qemu_get_be32(f);
100 vq->used[0] = qemu_get_be32(f);
101 vq->used[1] = qemu_get_be32(f);
104 static bool virtio_pci_has_extra_state(DeviceState *d)
106 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
108 return proxy->flags & VIRTIO_PCI_FLAG_MIGRATE_EXTRA;
111 static int get_virtio_pci_modern_state(QEMUFile *f, void *pv, size_t size)
113 VirtIOPCIProxy *proxy = pv;
116 proxy->dfselect = qemu_get_be32(f);
117 proxy->gfselect = qemu_get_be32(f);
118 proxy->guest_features[0] = qemu_get_be32(f);
119 proxy->guest_features[1] = qemu_get_be32(f);
120 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
121 virtio_pci_load_modern_queue_state(&proxy->vqs[i], f);
127 static void virtio_pci_save_modern_queue_state(VirtIOPCIQueue *vq,
130 qemu_put_be16(f, vq->num);
131 qemu_put_be16(f, vq->enabled);
132 qemu_put_be32(f, vq->desc[0]);
133 qemu_put_be32(f, vq->desc[1]);
134 qemu_put_be32(f, vq->avail[0]);
135 qemu_put_be32(f, vq->avail[1]);
136 qemu_put_be32(f, vq->used[0]);
137 qemu_put_be32(f, vq->used[1]);
140 static void put_virtio_pci_modern_state(QEMUFile *f, void *pv, size_t size)
142 VirtIOPCIProxy *proxy = pv;
145 qemu_put_be32(f, proxy->dfselect);
146 qemu_put_be32(f, proxy->gfselect);
147 qemu_put_be32(f, proxy->guest_features[0]);
148 qemu_put_be32(f, proxy->guest_features[1]);
149 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
150 virtio_pci_save_modern_queue_state(&proxy->vqs[i], f);
154 static const VMStateInfo vmstate_info_virtio_pci_modern_state = {
155 .name = "virtqueue_state",
156 .get = get_virtio_pci_modern_state,
157 .put = put_virtio_pci_modern_state,
160 static bool virtio_pci_modern_state_needed(void *opaque)
162 VirtIOPCIProxy *proxy = opaque;
164 return virtio_pci_modern(proxy);
167 static const VMStateDescription vmstate_virtio_pci_modern_state = {
168 .name = "virtio_pci/modern_state",
170 .minimum_version_id = 1,
171 .needed = &virtio_pci_modern_state_needed,
172 .fields = (VMStateField[]) {
174 .name = "modern_state",
176 .field_exists = NULL,
178 .info = &vmstate_info_virtio_pci_modern_state,
182 VMSTATE_END_OF_LIST()
186 static const VMStateDescription vmstate_virtio_pci = {
187 .name = "virtio_pci",
189 .minimum_version_id = 1,
190 .minimum_version_id_old = 1,
191 .fields = (VMStateField[]) {
192 VMSTATE_END_OF_LIST()
194 .subsections = (const VMStateDescription*[]) {
195 &vmstate_virtio_pci_modern_state,
200 static void virtio_pci_save_extra_state(DeviceState *d, QEMUFile *f)
202 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
204 vmstate_save_state(f, &vmstate_virtio_pci, proxy, NULL);
207 static int virtio_pci_load_extra_state(DeviceState *d, QEMUFile *f)
209 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
211 return vmstate_load_state(f, &vmstate_virtio_pci, proxy, 1);
214 static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f)
216 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
217 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
219 if (msix_present(&proxy->pci_dev))
220 qemu_put_be16(f, virtio_queue_vector(vdev, n));
223 static int virtio_pci_load_config(DeviceState *d, QEMUFile *f)
225 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
226 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
229 ret = pci_device_load(&proxy->pci_dev, f);
233 msix_unuse_all_vectors(&proxy->pci_dev);
234 msix_load(&proxy->pci_dev, f);
235 if (msix_present(&proxy->pci_dev)) {
236 qemu_get_be16s(f, &vdev->config_vector);
238 vdev->config_vector = VIRTIO_NO_VECTOR;
240 if (vdev->config_vector != VIRTIO_NO_VECTOR) {
241 return msix_vector_use(&proxy->pci_dev, vdev->config_vector);
246 static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f)
248 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
249 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
252 if (msix_present(&proxy->pci_dev)) {
253 qemu_get_be16s(f, &vector);
255 vector = VIRTIO_NO_VECTOR;
257 virtio_queue_set_vector(vdev, n, vector);
258 if (vector != VIRTIO_NO_VECTOR) {
259 return msix_vector_use(&proxy->pci_dev, vector);
265 static bool virtio_pci_ioeventfd_enabled(DeviceState *d)
267 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
269 return (proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) != 0;
272 #define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000
274 static inline int virtio_pci_queue_mem_mult(struct VirtIOPCIProxy *proxy)
276 return (proxy->flags & VIRTIO_PCI_FLAG_PAGE_PER_VQ) ?
277 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT : 4;
280 static int virtio_pci_ioeventfd_assign(DeviceState *d, EventNotifier *notifier,
283 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
284 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
285 VirtQueue *vq = virtio_get_queue(vdev, n);
286 bool legacy = virtio_pci_legacy(proxy);
287 bool modern = virtio_pci_modern(proxy);
288 bool fast_mmio = kvm_ioeventfd_any_length_enabled();
289 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
290 MemoryRegion *modern_mr = &proxy->notify.mr;
291 MemoryRegion *modern_notify_mr = &proxy->notify_pio.mr;
292 MemoryRegion *legacy_mr = &proxy->bar;
293 hwaddr modern_addr = virtio_pci_queue_mem_mult(proxy) *
294 virtio_get_queue_index(vq);
295 hwaddr legacy_addr = VIRTIO_PCI_QUEUE_NOTIFY;
300 memory_region_add_eventfd(modern_mr, modern_addr, 0,
303 memory_region_add_eventfd(modern_mr, modern_addr, 2,
307 memory_region_add_eventfd(modern_notify_mr, 0, 2,
312 memory_region_add_eventfd(legacy_mr, legacy_addr, 2,
318 memory_region_del_eventfd(modern_mr, modern_addr, 0,
321 memory_region_del_eventfd(modern_mr, modern_addr, 2,
325 memory_region_del_eventfd(modern_notify_mr, 0, 2,
330 memory_region_del_eventfd(legacy_mr, legacy_addr, 2,
337 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
339 virtio_bus_start_ioeventfd(&proxy->bus);
342 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
344 virtio_bus_stop_ioeventfd(&proxy->bus);
347 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
349 VirtIOPCIProxy *proxy = opaque;
350 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
354 case VIRTIO_PCI_GUEST_FEATURES:
355 /* Guest does not negotiate properly? We have to assume nothing. */
356 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
357 val = virtio_bus_get_vdev_bad_features(&proxy->bus);
359 virtio_set_features(vdev, val);
361 case VIRTIO_PCI_QUEUE_PFN:
362 pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
364 virtio_pci_reset(DEVICE(proxy));
367 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
369 case VIRTIO_PCI_QUEUE_SEL:
370 if (val < VIRTIO_QUEUE_MAX)
371 vdev->queue_sel = val;
373 case VIRTIO_PCI_QUEUE_NOTIFY:
374 if (val < VIRTIO_QUEUE_MAX) {
375 virtio_queue_notify(vdev, val);
378 case VIRTIO_PCI_STATUS:
379 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
380 virtio_pci_stop_ioeventfd(proxy);
383 virtio_set_status(vdev, val & 0xFF);
385 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
386 virtio_pci_start_ioeventfd(proxy);
389 if (vdev->status == 0) {
390 virtio_pci_reset(DEVICE(proxy));
393 /* Linux before 2.6.34 drives the device without enabling
394 the PCI device bus master bit. Enable it automatically
395 for the guest. This is a PCI spec violation but so is
396 initiating DMA with bus master bit clear. */
397 if (val == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER)) {
398 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
399 proxy->pci_dev.config[PCI_COMMAND] |
400 PCI_COMMAND_MASTER, 1);
403 case VIRTIO_MSI_CONFIG_VECTOR:
404 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
405 /* Make it possible for guest to discover an error took place. */
406 if (msix_vector_use(&proxy->pci_dev, val) < 0)
407 val = VIRTIO_NO_VECTOR;
408 vdev->config_vector = val;
410 case VIRTIO_MSI_QUEUE_VECTOR:
411 msix_vector_unuse(&proxy->pci_dev,
412 virtio_queue_vector(vdev, vdev->queue_sel));
413 /* Make it possible for guest to discover an error took place. */
414 if (msix_vector_use(&proxy->pci_dev, val) < 0)
415 val = VIRTIO_NO_VECTOR;
416 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
419 error_report("%s: unexpected address 0x%x value 0x%x",
420 __func__, addr, val);
425 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
427 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
428 uint32_t ret = 0xFFFFFFFF;
431 case VIRTIO_PCI_HOST_FEATURES:
432 ret = vdev->host_features;
434 case VIRTIO_PCI_GUEST_FEATURES:
435 ret = vdev->guest_features;
437 case VIRTIO_PCI_QUEUE_PFN:
438 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
439 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
441 case VIRTIO_PCI_QUEUE_NUM:
442 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
444 case VIRTIO_PCI_QUEUE_SEL:
445 ret = vdev->queue_sel;
447 case VIRTIO_PCI_STATUS:
451 /* reading from the ISR also clears it. */
452 ret = atomic_xchg(&vdev->isr, 0);
453 pci_irq_deassert(&proxy->pci_dev);
455 case VIRTIO_MSI_CONFIG_VECTOR:
456 ret = vdev->config_vector;
458 case VIRTIO_MSI_QUEUE_VECTOR:
459 ret = virtio_queue_vector(vdev, vdev->queue_sel);
468 static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
471 VirtIOPCIProxy *proxy = opaque;
472 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
473 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
476 return virtio_ioport_read(proxy, addr);
482 val = virtio_config_readb(vdev, addr);
485 val = virtio_config_readw(vdev, addr);
486 if (virtio_is_big_endian(vdev)) {
491 val = virtio_config_readl(vdev, addr);
492 if (virtio_is_big_endian(vdev)) {
500 static void virtio_pci_config_write(void *opaque, hwaddr addr,
501 uint64_t val, unsigned size)
503 VirtIOPCIProxy *proxy = opaque;
504 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
505 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
507 virtio_ioport_write(proxy, addr, val);
512 * Virtio-PCI is odd. Ioports are LE but config space is target native
517 virtio_config_writeb(vdev, addr, val);
520 if (virtio_is_big_endian(vdev)) {
523 virtio_config_writew(vdev, addr, val);
526 if (virtio_is_big_endian(vdev)) {
529 virtio_config_writel(vdev, addr, val);
534 static const MemoryRegionOps virtio_pci_config_ops = {
535 .read = virtio_pci_config_read,
536 .write = virtio_pci_config_write,
538 .min_access_size = 1,
539 .max_access_size = 4,
541 .endianness = DEVICE_LITTLE_ENDIAN,
544 /* Below are generic functions to do memcpy from/to an address space,
545 * without byteswaps, with input validation.
547 * As regular address_space_* APIs all do some kind of byteswap at least for
548 * some host/target combinations, we are forced to explicitly convert to a
549 * known-endianness integer value.
550 * It doesn't really matter which endian format to go through, so the code
551 * below selects the endian that causes the least amount of work on the given
554 * Note: host pointer must be aligned.
557 void virtio_address_space_write(AddressSpace *as, hwaddr addr,
558 const uint8_t *buf, int len)
562 /* address_space_* APIs assume an aligned address.
563 * As address is under guest control, handle illegal values.
567 /* Make sure caller aligned buf properly */
568 assert(!(((uintptr_t)buf) & (len - 1)));
572 val = pci_get_byte(buf);
573 address_space_stb(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
576 val = pci_get_word(buf);
577 address_space_stw_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
580 val = pci_get_long(buf);
581 address_space_stl_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
584 /* As length is under guest control, handle illegal values. */
590 virtio_address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len)
594 /* address_space_* APIs assume an aligned address.
595 * As address is under guest control, handle illegal values.
599 /* Make sure caller aligned buf properly */
600 assert(!(((uintptr_t)buf) & (len - 1)));
604 val = address_space_ldub(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
605 pci_set_byte(buf, val);
608 val = address_space_lduw_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
609 pci_set_word(buf, val);
612 val = address_space_ldl_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
613 pci_set_long(buf, val);
616 /* As length is under guest control, handle illegal values. */
621 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
622 uint32_t val, int len)
624 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
625 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
626 struct virtio_pci_cfg_cap *cfg;
628 pci_default_write_config(pci_dev, address, val, len);
630 if (range_covers_byte(address, len, PCI_COMMAND) &&
631 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
632 virtio_pci_stop_ioeventfd(proxy);
633 virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
636 if (proxy->config_cap &&
637 ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
639 sizeof cfg->pci_cfg_data)) {
643 cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
644 off = le32_to_cpu(cfg->cap.offset);
645 len = le32_to_cpu(cfg->cap.length);
647 if (len == 1 || len == 2 || len == 4) {
648 assert(len <= sizeof cfg->pci_cfg_data);
649 virtio_address_space_write(&proxy->modern_as, off,
650 cfg->pci_cfg_data, len);
655 static uint32_t virtio_read_config(PCIDevice *pci_dev,
656 uint32_t address, int len)
658 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
659 struct virtio_pci_cfg_cap *cfg;
661 if (proxy->config_cap &&
662 ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
664 sizeof cfg->pci_cfg_data)) {
668 cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
669 off = le32_to_cpu(cfg->cap.offset);
670 len = le32_to_cpu(cfg->cap.length);
672 if (len == 1 || len == 2 || len == 4) {
673 assert(len <= sizeof cfg->pci_cfg_data);
674 virtio_address_space_read(&proxy->modern_as, off,
675 cfg->pci_cfg_data, len);
679 return pci_default_read_config(pci_dev, address, len);
682 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
683 unsigned int queue_no,
686 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
689 if (irqfd->users == 0) {
690 ret = kvm_irqchip_add_msi_route(kvm_state, vector, &proxy->pci_dev);
700 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
703 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
704 if (--irqfd->users == 0) {
705 kvm_irqchip_release_virq(kvm_state, irqfd->virq);
709 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
710 unsigned int queue_no,
713 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
714 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
715 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
716 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
717 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, irqfd->virq);
720 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
721 unsigned int queue_no,
724 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
725 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
726 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
727 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
730 ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->virq);
734 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
736 PCIDevice *dev = &proxy->pci_dev;
737 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
738 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
742 for (queue_no = 0; queue_no < nvqs; queue_no++) {
743 if (!virtio_queue_get_num(vdev, queue_no)) {
746 vector = virtio_queue_vector(vdev, queue_no);
747 if (vector >= msix_nr_vectors_allocated(dev)) {
750 ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector);
754 /* If guest supports masking, set up irqfd now.
755 * Otherwise, delay until unmasked in the frontend.
757 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
758 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
760 kvm_virtio_pci_vq_vector_release(proxy, vector);
768 while (--queue_no >= 0) {
769 vector = virtio_queue_vector(vdev, queue_no);
770 if (vector >= msix_nr_vectors_allocated(dev)) {
773 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
774 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
776 kvm_virtio_pci_vq_vector_release(proxy, vector);
781 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
783 PCIDevice *dev = &proxy->pci_dev;
784 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
787 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
789 for (queue_no = 0; queue_no < nvqs; queue_no++) {
790 if (!virtio_queue_get_num(vdev, queue_no)) {
793 vector = virtio_queue_vector(vdev, queue_no);
794 if (vector >= msix_nr_vectors_allocated(dev)) {
797 /* If guest supports masking, clean up irqfd now.
798 * Otherwise, it was cleaned when masked in the frontend.
800 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
801 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
803 kvm_virtio_pci_vq_vector_release(proxy, vector);
807 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
808 unsigned int queue_no,
812 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
813 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
814 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
815 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
819 if (proxy->vector_irqfd) {
820 irqfd = &proxy->vector_irqfd[vector];
821 if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) {
822 ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg,
827 kvm_irqchip_commit_routes(kvm_state);
831 /* If guest supports masking, irqfd is already setup, unmask it.
832 * Otherwise, set it up now.
834 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
835 k->guest_notifier_mask(vdev, queue_no, false);
836 /* Test after unmasking to avoid losing events. */
837 if (k->guest_notifier_pending &&
838 k->guest_notifier_pending(vdev, queue_no)) {
839 event_notifier_set(n);
842 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
847 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
848 unsigned int queue_no,
851 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
852 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
854 /* If guest supports masking, keep irqfd but mask it.
855 * Otherwise, clean it up now.
857 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
858 k->guest_notifier_mask(vdev, queue_no, true);
860 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
864 static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
867 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
868 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
869 VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
870 int ret, index, unmasked = 0;
873 index = virtio_get_queue_index(vq);
874 if (!virtio_queue_get_num(vdev, index)) {
877 if (index < proxy->nvqs_with_notifiers) {
878 ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg);
884 vq = virtio_vector_next_queue(vq);
890 vq = virtio_vector_first_queue(vdev, vector);
891 while (vq && unmasked >= 0) {
892 index = virtio_get_queue_index(vq);
893 if (index < proxy->nvqs_with_notifiers) {
894 virtio_pci_vq_vector_mask(proxy, index, vector);
897 vq = virtio_vector_next_queue(vq);
902 static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
904 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
905 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
906 VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
910 index = virtio_get_queue_index(vq);
911 if (!virtio_queue_get_num(vdev, index)) {
914 if (index < proxy->nvqs_with_notifiers) {
915 virtio_pci_vq_vector_mask(proxy, index, vector);
917 vq = virtio_vector_next_queue(vq);
921 static void virtio_pci_vector_poll(PCIDevice *dev,
922 unsigned int vector_start,
923 unsigned int vector_end)
925 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
926 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
927 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
930 EventNotifier *notifier;
933 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
934 if (!virtio_queue_get_num(vdev, queue_no)) {
937 vector = virtio_queue_vector(vdev, queue_no);
938 if (vector < vector_start || vector >= vector_end ||
939 !msix_is_masked(dev, vector)) {
942 vq = virtio_get_queue(vdev, queue_no);
943 notifier = virtio_queue_get_guest_notifier(vq);
944 if (k->guest_notifier_pending) {
945 if (k->guest_notifier_pending(vdev, queue_no)) {
946 msix_set_pending(dev, vector);
948 } else if (event_notifier_test_and_clear(notifier)) {
949 msix_set_pending(dev, vector);
954 static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
957 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
958 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
959 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
960 VirtQueue *vq = virtio_get_queue(vdev, n);
961 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
964 int r = event_notifier_init(notifier, 0);
968 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
970 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
971 event_notifier_cleanup(notifier);
974 if (!msix_enabled(&proxy->pci_dev) &&
975 vdev->use_guest_notifier_mask &&
976 vdc->guest_notifier_mask) {
977 vdc->guest_notifier_mask(vdev, n, !assign);
983 static bool virtio_pci_query_guest_notifiers(DeviceState *d)
985 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
986 return msix_enabled(&proxy->pci_dev);
989 static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
991 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
992 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
993 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
995 bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
996 kvm_msi_via_irqfd_enabled();
998 nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX);
1000 /* When deassigning, pass a consistent nvqs value
1001 * to avoid leaking notifiers.
1003 assert(assign || nvqs == proxy->nvqs_with_notifiers);
1005 proxy->nvqs_with_notifiers = nvqs;
1007 /* Must unset vector notifier while guest notifier is still assigned */
1008 if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) {
1009 msix_unset_vector_notifiers(&proxy->pci_dev);
1010 if (proxy->vector_irqfd) {
1011 kvm_virtio_pci_vector_release(proxy, nvqs);
1012 g_free(proxy->vector_irqfd);
1013 proxy->vector_irqfd = NULL;
1017 for (n = 0; n < nvqs; n++) {
1018 if (!virtio_queue_get_num(vdev, n)) {
1022 r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd);
1028 /* Must set vector notifier after guest notifier has been assigned */
1029 if ((with_irqfd || k->guest_notifier_mask) && assign) {
1031 proxy->vector_irqfd =
1032 g_malloc0(sizeof(*proxy->vector_irqfd) *
1033 msix_nr_vectors_allocated(&proxy->pci_dev));
1034 r = kvm_virtio_pci_vector_use(proxy, nvqs);
1039 r = msix_set_vector_notifiers(&proxy->pci_dev,
1040 virtio_pci_vector_unmask,
1041 virtio_pci_vector_mask,
1042 virtio_pci_vector_poll);
1044 goto notifiers_error;
1053 kvm_virtio_pci_vector_release(proxy, nvqs);
1057 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
1060 virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
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);
1087 #ifdef CONFIG_VIRTFS
1088 static void virtio_9p_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1090 V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev);
1091 DeviceState *vdev = DEVICE(&dev->vdev);
1093 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1094 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1097 static Property virtio_9p_pci_properties[] = {
1098 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1099 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1100 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1101 DEFINE_PROP_END_OF_LIST(),
1104 static void virtio_9p_pci_class_init(ObjectClass *klass, void *data)
1106 DeviceClass *dc = DEVICE_CLASS(klass);
1107 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1108 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1110 k->realize = virtio_9p_pci_realize;
1111 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1112 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P;
1113 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1114 pcidev_k->class_id = 0x2;
1115 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1116 dc->props = virtio_9p_pci_properties;
1119 static void virtio_9p_pci_instance_init(Object *obj)
1121 V9fsPCIState *dev = VIRTIO_9P_PCI(obj);
1123 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1127 static const TypeInfo virtio_9p_pci_info = {
1128 .name = TYPE_VIRTIO_9P_PCI,
1129 .parent = TYPE_VIRTIO_PCI,
1130 .instance_size = sizeof(V9fsPCIState),
1131 .instance_init = virtio_9p_pci_instance_init,
1132 .class_init = virtio_9p_pci_class_init,
1134 #endif /* CONFIG_VIRTFS */
1137 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
1140 static int virtio_pci_query_nvectors(DeviceState *d)
1142 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1144 return proxy->nvectors;
1147 static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
1148 struct virtio_pci_cap *cap)
1150 PCIDevice *dev = &proxy->pci_dev;
1153 offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 0, cap->cap_len);
1156 assert(cap->cap_len >= sizeof *cap);
1157 memcpy(dev->config + offset + PCI_CAP_FLAGS, &cap->cap_len,
1158 cap->cap_len - PCI_CAP_FLAGS);
1163 static uint64_t virtio_pci_common_read(void *opaque, hwaddr addr,
1166 VirtIOPCIProxy *proxy = opaque;
1167 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1172 case VIRTIO_PCI_COMMON_DFSELECT:
1173 val = proxy->dfselect;
1175 case VIRTIO_PCI_COMMON_DF:
1176 if (proxy->dfselect <= 1) {
1177 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
1179 val = (vdev->host_features & ~vdc->legacy_features) >>
1180 (32 * proxy->dfselect);
1183 case VIRTIO_PCI_COMMON_GFSELECT:
1184 val = proxy->gfselect;
1186 case VIRTIO_PCI_COMMON_GF:
1187 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1188 val = proxy->guest_features[proxy->gfselect];
1191 case VIRTIO_PCI_COMMON_MSIX:
1192 val = vdev->config_vector;
1194 case VIRTIO_PCI_COMMON_NUMQ:
1195 for (i = 0; i < VIRTIO_QUEUE_MAX; ++i) {
1196 if (virtio_queue_get_num(vdev, i)) {
1201 case VIRTIO_PCI_COMMON_STATUS:
1204 case VIRTIO_PCI_COMMON_CFGGENERATION:
1205 val = vdev->generation;
1207 case VIRTIO_PCI_COMMON_Q_SELECT:
1208 val = vdev->queue_sel;
1210 case VIRTIO_PCI_COMMON_Q_SIZE:
1211 val = virtio_queue_get_num(vdev, vdev->queue_sel);
1213 case VIRTIO_PCI_COMMON_Q_MSIX:
1214 val = virtio_queue_vector(vdev, vdev->queue_sel);
1216 case VIRTIO_PCI_COMMON_Q_ENABLE:
1217 val = proxy->vqs[vdev->queue_sel].enabled;
1219 case VIRTIO_PCI_COMMON_Q_NOFF:
1220 /* Simply map queues in order */
1221 val = vdev->queue_sel;
1223 case VIRTIO_PCI_COMMON_Q_DESCLO:
1224 val = proxy->vqs[vdev->queue_sel].desc[0];
1226 case VIRTIO_PCI_COMMON_Q_DESCHI:
1227 val = proxy->vqs[vdev->queue_sel].desc[1];
1229 case VIRTIO_PCI_COMMON_Q_AVAILLO:
1230 val = proxy->vqs[vdev->queue_sel].avail[0];
1232 case VIRTIO_PCI_COMMON_Q_AVAILHI:
1233 val = proxy->vqs[vdev->queue_sel].avail[1];
1235 case VIRTIO_PCI_COMMON_Q_USEDLO:
1236 val = proxy->vqs[vdev->queue_sel].used[0];
1238 case VIRTIO_PCI_COMMON_Q_USEDHI:
1239 val = proxy->vqs[vdev->queue_sel].used[1];
1248 static void virtio_pci_common_write(void *opaque, hwaddr addr,
1249 uint64_t val, unsigned size)
1251 VirtIOPCIProxy *proxy = opaque;
1252 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1255 case VIRTIO_PCI_COMMON_DFSELECT:
1256 proxy->dfselect = val;
1258 case VIRTIO_PCI_COMMON_GFSELECT:
1259 proxy->gfselect = val;
1261 case VIRTIO_PCI_COMMON_GF:
1262 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1263 proxy->guest_features[proxy->gfselect] = val;
1264 virtio_set_features(vdev,
1265 (((uint64_t)proxy->guest_features[1]) << 32) |
1266 proxy->guest_features[0]);
1269 case VIRTIO_PCI_COMMON_MSIX:
1270 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
1271 /* Make it possible for guest to discover an error took place. */
1272 if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1273 val = VIRTIO_NO_VECTOR;
1275 vdev->config_vector = val;
1277 case VIRTIO_PCI_COMMON_STATUS:
1278 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
1279 virtio_pci_stop_ioeventfd(proxy);
1282 virtio_set_status(vdev, val & 0xFF);
1284 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
1285 virtio_pci_start_ioeventfd(proxy);
1288 if (vdev->status == 0) {
1289 virtio_pci_reset(DEVICE(proxy));
1293 case VIRTIO_PCI_COMMON_Q_SELECT:
1294 if (val < VIRTIO_QUEUE_MAX) {
1295 vdev->queue_sel = val;
1298 case VIRTIO_PCI_COMMON_Q_SIZE:
1299 proxy->vqs[vdev->queue_sel].num = val;
1301 case VIRTIO_PCI_COMMON_Q_MSIX:
1302 msix_vector_unuse(&proxy->pci_dev,
1303 virtio_queue_vector(vdev, vdev->queue_sel));
1304 /* Make it possible for guest to discover an error took place. */
1305 if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1306 val = VIRTIO_NO_VECTOR;
1308 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
1310 case VIRTIO_PCI_COMMON_Q_ENABLE:
1311 /* TODO: need a way to put num back on reset. */
1312 virtio_queue_set_num(vdev, vdev->queue_sel,
1313 proxy->vqs[vdev->queue_sel].num);
1314 virtio_queue_set_rings(vdev, vdev->queue_sel,
1315 ((uint64_t)proxy->vqs[vdev->queue_sel].desc[1]) << 32 |
1316 proxy->vqs[vdev->queue_sel].desc[0],
1317 ((uint64_t)proxy->vqs[vdev->queue_sel].avail[1]) << 32 |
1318 proxy->vqs[vdev->queue_sel].avail[0],
1319 ((uint64_t)proxy->vqs[vdev->queue_sel].used[1]) << 32 |
1320 proxy->vqs[vdev->queue_sel].used[0]);
1321 proxy->vqs[vdev->queue_sel].enabled = 1;
1323 case VIRTIO_PCI_COMMON_Q_DESCLO:
1324 proxy->vqs[vdev->queue_sel].desc[0] = val;
1326 case VIRTIO_PCI_COMMON_Q_DESCHI:
1327 proxy->vqs[vdev->queue_sel].desc[1] = val;
1329 case VIRTIO_PCI_COMMON_Q_AVAILLO:
1330 proxy->vqs[vdev->queue_sel].avail[0] = val;
1332 case VIRTIO_PCI_COMMON_Q_AVAILHI:
1333 proxy->vqs[vdev->queue_sel].avail[1] = val;
1335 case VIRTIO_PCI_COMMON_Q_USEDLO:
1336 proxy->vqs[vdev->queue_sel].used[0] = val;
1338 case VIRTIO_PCI_COMMON_Q_USEDHI:
1339 proxy->vqs[vdev->queue_sel].used[1] = val;
1347 static uint64_t virtio_pci_notify_read(void *opaque, hwaddr addr,
1353 static void virtio_pci_notify_write(void *opaque, hwaddr addr,
1354 uint64_t val, unsigned size)
1356 VirtIODevice *vdev = opaque;
1357 VirtIOPCIProxy *proxy = VIRTIO_PCI(DEVICE(vdev)->parent_bus->parent);
1358 unsigned queue = addr / virtio_pci_queue_mem_mult(proxy);
1360 if (queue < VIRTIO_QUEUE_MAX) {
1361 virtio_queue_notify(vdev, queue);
1365 static void virtio_pci_notify_write_pio(void *opaque, hwaddr addr,
1366 uint64_t val, unsigned size)
1368 VirtIODevice *vdev = opaque;
1369 unsigned queue = val;
1371 if (queue < VIRTIO_QUEUE_MAX) {
1372 virtio_queue_notify(vdev, queue);
1376 static uint64_t virtio_pci_isr_read(void *opaque, hwaddr addr,
1379 VirtIOPCIProxy *proxy = opaque;
1380 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1381 uint64_t val = atomic_xchg(&vdev->isr, 0);
1382 pci_irq_deassert(&proxy->pci_dev);
1387 static void virtio_pci_isr_write(void *opaque, hwaddr addr,
1388 uint64_t val, unsigned size)
1392 static uint64_t virtio_pci_device_read(void *opaque, hwaddr addr,
1395 VirtIODevice *vdev = opaque;
1400 val = virtio_config_modern_readb(vdev, addr);
1403 val = virtio_config_modern_readw(vdev, addr);
1406 val = virtio_config_modern_readl(vdev, addr);
1412 static void virtio_pci_device_write(void *opaque, hwaddr addr,
1413 uint64_t val, unsigned size)
1415 VirtIODevice *vdev = opaque;
1418 virtio_config_modern_writeb(vdev, addr, val);
1421 virtio_config_modern_writew(vdev, addr, val);
1424 virtio_config_modern_writel(vdev, addr, val);
1429 static void virtio_pci_modern_regions_init(VirtIOPCIProxy *proxy)
1431 static const MemoryRegionOps common_ops = {
1432 .read = virtio_pci_common_read,
1433 .write = virtio_pci_common_write,
1435 .min_access_size = 1,
1436 .max_access_size = 4,
1438 .endianness = DEVICE_LITTLE_ENDIAN,
1440 static const MemoryRegionOps isr_ops = {
1441 .read = virtio_pci_isr_read,
1442 .write = virtio_pci_isr_write,
1444 .min_access_size = 1,
1445 .max_access_size = 4,
1447 .endianness = DEVICE_LITTLE_ENDIAN,
1449 static const MemoryRegionOps device_ops = {
1450 .read = virtio_pci_device_read,
1451 .write = virtio_pci_device_write,
1453 .min_access_size = 1,
1454 .max_access_size = 4,
1456 .endianness = DEVICE_LITTLE_ENDIAN,
1458 static const MemoryRegionOps notify_ops = {
1459 .read = virtio_pci_notify_read,
1460 .write = virtio_pci_notify_write,
1462 .min_access_size = 1,
1463 .max_access_size = 4,
1465 .endianness = DEVICE_LITTLE_ENDIAN,
1467 static const MemoryRegionOps notify_pio_ops = {
1468 .read = virtio_pci_notify_read,
1469 .write = virtio_pci_notify_write_pio,
1471 .min_access_size = 1,
1472 .max_access_size = 4,
1474 .endianness = DEVICE_LITTLE_ENDIAN,
1478 memory_region_init_io(&proxy->common.mr, OBJECT(proxy),
1481 "virtio-pci-common",
1482 proxy->common.size);
1484 memory_region_init_io(&proxy->isr.mr, OBJECT(proxy),
1490 memory_region_init_io(&proxy->device.mr, OBJECT(proxy),
1492 virtio_bus_get_device(&proxy->bus),
1493 "virtio-pci-device",
1494 proxy->device.size);
1496 memory_region_init_io(&proxy->notify.mr, OBJECT(proxy),
1498 virtio_bus_get_device(&proxy->bus),
1499 "virtio-pci-notify",
1500 proxy->notify.size);
1502 memory_region_init_io(&proxy->notify_pio.mr, OBJECT(proxy),
1504 virtio_bus_get_device(&proxy->bus),
1505 "virtio-pci-notify-pio",
1506 proxy->notify_pio.size);
1509 static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy,
1510 VirtIOPCIRegion *region,
1511 struct virtio_pci_cap *cap,
1515 memory_region_add_subregion(mr, region->offset, ®ion->mr);
1517 cap->cfg_type = region->type;
1519 cap->offset = cpu_to_le32(region->offset);
1520 cap->length = cpu_to_le32(region->size);
1521 virtio_pci_add_mem_cap(proxy, cap);
1525 static void virtio_pci_modern_mem_region_map(VirtIOPCIProxy *proxy,
1526 VirtIOPCIRegion *region,
1527 struct virtio_pci_cap *cap)
1529 virtio_pci_modern_region_map(proxy, region, cap,
1530 &proxy->modern_bar, proxy->modern_mem_bar_idx);
1533 static void virtio_pci_modern_io_region_map(VirtIOPCIProxy *proxy,
1534 VirtIOPCIRegion *region,
1535 struct virtio_pci_cap *cap)
1537 virtio_pci_modern_region_map(proxy, region, cap,
1538 &proxy->io_bar, proxy->modern_io_bar_idx);
1541 static void virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy *proxy,
1542 VirtIOPCIRegion *region)
1544 memory_region_del_subregion(&proxy->modern_bar,
1548 static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy *proxy,
1549 VirtIOPCIRegion *region)
1551 memory_region_del_subregion(&proxy->io_bar,
1555 static void virtio_pci_pre_plugged(DeviceState *d, Error **errp)
1557 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1558 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1560 if (virtio_pci_modern(proxy)) {
1561 virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
1564 virtio_add_feature(&vdev->host_features, VIRTIO_F_BAD_FEATURE);
1567 /* This is called by virtio-bus just after the device is plugged. */
1568 static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
1570 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1571 VirtioBusState *bus = &proxy->bus;
1572 bool legacy = virtio_pci_legacy(proxy);
1574 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1577 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1580 * Virtio capabilities present without
1581 * VIRTIO_F_VERSION_1 confuses guests
1583 if (!proxy->ignore_backend_features &&
1584 !virtio_has_feature(vdev->host_features, VIRTIO_F_VERSION_1)) {
1585 virtio_pci_disable_modern(proxy);
1588 error_setg(errp, "Device doesn't support modern mode, and legacy"
1589 " mode is disabled");
1590 error_append_hint(errp, "Set disable-legacy to off\n");
1596 modern = virtio_pci_modern(proxy);
1598 config = proxy->pci_dev.config;
1599 if (proxy->class_code) {
1600 pci_config_set_class(config, proxy->class_code);
1604 /* legacy and transitional */
1605 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
1606 pci_get_word(config + PCI_VENDOR_ID));
1607 pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
1609 /* pure virtio-1.0 */
1610 pci_set_word(config + PCI_VENDOR_ID,
1611 PCI_VENDOR_ID_REDHAT_QUMRANET);
1612 pci_set_word(config + PCI_DEVICE_ID,
1613 0x1040 + virtio_bus_get_vdev_id(bus));
1614 pci_config_set_revision(config, 1);
1616 config[PCI_INTERRUPT_PIN] = 1;
1620 struct virtio_pci_cap cap = {
1621 .cap_len = sizeof cap,
1623 struct virtio_pci_notify_cap notify = {
1624 .cap.cap_len = sizeof notify,
1625 .notify_off_multiplier =
1626 cpu_to_le32(virtio_pci_queue_mem_mult(proxy)),
1628 struct virtio_pci_cfg_cap cfg = {
1629 .cap.cap_len = sizeof cfg,
1630 .cap.cfg_type = VIRTIO_PCI_CAP_PCI_CFG,
1632 struct virtio_pci_notify_cap notify_pio = {
1633 .cap.cap_len = sizeof notify,
1634 .notify_off_multiplier = cpu_to_le32(0x0),
1637 struct virtio_pci_cfg_cap *cfg_mask;
1639 virtio_pci_modern_regions_init(proxy);
1641 virtio_pci_modern_mem_region_map(proxy, &proxy->common, &cap);
1642 virtio_pci_modern_mem_region_map(proxy, &proxy->isr, &cap);
1643 virtio_pci_modern_mem_region_map(proxy, &proxy->device, &cap);
1644 virtio_pci_modern_mem_region_map(proxy, &proxy->notify, ¬ify.cap);
1647 memory_region_init(&proxy->io_bar, OBJECT(proxy),
1648 "virtio-pci-io", 0x4);
1650 pci_register_bar(&proxy->pci_dev, proxy->modern_io_bar_idx,
1651 PCI_BASE_ADDRESS_SPACE_IO, &proxy->io_bar);
1653 virtio_pci_modern_io_region_map(proxy, &proxy->notify_pio,
1657 pci_register_bar(&proxy->pci_dev, proxy->modern_mem_bar_idx,
1658 PCI_BASE_ADDRESS_SPACE_MEMORY |
1659 PCI_BASE_ADDRESS_MEM_PREFETCH |
1660 PCI_BASE_ADDRESS_MEM_TYPE_64,
1661 &proxy->modern_bar);
1663 proxy->config_cap = virtio_pci_add_mem_cap(proxy, &cfg.cap);
1664 cfg_mask = (void *)(proxy->pci_dev.wmask + proxy->config_cap);
1665 pci_set_byte(&cfg_mask->cap.bar, ~0x0);
1666 pci_set_long((uint8_t *)&cfg_mask->cap.offset, ~0x0);
1667 pci_set_long((uint8_t *)&cfg_mask->cap.length, ~0x0);
1668 pci_set_long(cfg_mask->pci_cfg_data, ~0x0);
1671 if (proxy->nvectors) {
1672 int err = msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors,
1673 proxy->msix_bar_idx);
1675 /* Notice when a system that supports MSIx can't initialize it. */
1676 if (err != -ENOTSUP) {
1677 error_report("unable to init msix vectors to %" PRIu32,
1680 proxy->nvectors = 0;
1684 proxy->pci_dev.config_write = virtio_write_config;
1685 proxy->pci_dev.config_read = virtio_read_config;
1688 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
1689 + virtio_bus_get_vdev_config_len(bus);
1690 size = pow2ceil(size);
1692 memory_region_init_io(&proxy->bar, OBJECT(proxy),
1693 &virtio_pci_config_ops,
1694 proxy, "virtio-pci", size);
1696 pci_register_bar(&proxy->pci_dev, proxy->legacy_io_bar_idx,
1697 PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar);
1701 static void virtio_pci_device_unplugged(DeviceState *d)
1703 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1704 bool modern = virtio_pci_modern(proxy);
1705 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1707 virtio_pci_stop_ioeventfd(proxy);
1710 virtio_pci_modern_mem_region_unmap(proxy, &proxy->common);
1711 virtio_pci_modern_mem_region_unmap(proxy, &proxy->isr);
1712 virtio_pci_modern_mem_region_unmap(proxy, &proxy->device);
1713 virtio_pci_modern_mem_region_unmap(proxy, &proxy->notify);
1715 virtio_pci_modern_io_region_unmap(proxy, &proxy->notify_pio);
1720 static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
1722 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1723 VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
1724 bool pcie_port = pci_bus_is_express(pci_dev->bus) &&
1725 !pci_bus_is_root(pci_dev->bus);
1727 if (!kvm_has_many_ioeventfds()) {
1728 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
1732 * virtio pci bar layout used by default.
1733 * subclasses can re-arrange things if needed.
1735 * region 0 -- virtio legacy io bar
1736 * region 1 -- msi-x bar
1737 * region 4+5 -- virtio modern memory (64bit) bar
1740 proxy->legacy_io_bar_idx = 0;
1741 proxy->msix_bar_idx = 1;
1742 proxy->modern_io_bar_idx = 2;
1743 proxy->modern_mem_bar_idx = 4;
1745 proxy->common.offset = 0x0;
1746 proxy->common.size = 0x1000;
1747 proxy->common.type = VIRTIO_PCI_CAP_COMMON_CFG;
1749 proxy->isr.offset = 0x1000;
1750 proxy->isr.size = 0x1000;
1751 proxy->isr.type = VIRTIO_PCI_CAP_ISR_CFG;
1753 proxy->device.offset = 0x2000;
1754 proxy->device.size = 0x1000;
1755 proxy->device.type = VIRTIO_PCI_CAP_DEVICE_CFG;
1757 proxy->notify.offset = 0x3000;
1758 proxy->notify.size = virtio_pci_queue_mem_mult(proxy) * VIRTIO_QUEUE_MAX;
1759 proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
1761 proxy->notify_pio.offset = 0x0;
1762 proxy->notify_pio.size = 0x4;
1763 proxy->notify_pio.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
1765 /* subclasses can enforce modern, so do this unconditionally */
1766 memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci",
1767 /* PCI BAR regions must be powers of 2 */
1768 pow2ceil(proxy->notify.offset + proxy->notify.size));
1770 memory_region_init_alias(&proxy->modern_cfg,
1775 memory_region_size(&proxy->modern_bar));
1777 address_space_init(&proxy->modern_as, &proxy->modern_cfg, "virtio-pci-cfg-as");
1779 if (proxy->disable_legacy == ON_OFF_AUTO_AUTO) {
1780 proxy->disable_legacy = pcie_port ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
1783 if (!virtio_pci_modern(proxy) && !virtio_pci_legacy(proxy)) {
1784 error_setg(errp, "device cannot work as neither modern nor legacy mode"
1786 error_append_hint(errp, "Set either disable-modern or disable-legacy"
1791 if (pcie_port && pci_is_express(pci_dev)) {
1794 pos = pcie_endpoint_cap_init(pci_dev, 0);
1797 pos = pci_add_capability(pci_dev, PCI_CAP_ID_PM, 0, PCI_PM_SIZEOF);
1801 * Indicates that this function complies with revision 1.2 of the
1802 * PCI Power Management Interface Specification.
1804 pci_set_word(pci_dev->config + pos + PCI_PM_PMC, 0x3);
1807 * make future invocations of pci_is_express() return false
1808 * and pci_config_size() return PCI_CONFIG_SPACE_SIZE.
1810 pci_dev->cap_present &= ~QEMU_PCI_CAP_EXPRESS;
1813 virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
1815 k->realize(proxy, errp);
1819 static void virtio_pci_exit(PCIDevice *pci_dev)
1821 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1823 msix_uninit_exclusive_bar(pci_dev);
1824 address_space_destroy(&proxy->modern_as);
1827 static void virtio_pci_reset(DeviceState *qdev)
1829 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1830 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
1833 virtio_pci_stop_ioeventfd(proxy);
1834 virtio_bus_reset(bus);
1835 msix_unuse_all_vectors(&proxy->pci_dev);
1837 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1838 proxy->vqs[i].enabled = 0;
1842 static Property virtio_pci_properties[] = {
1843 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
1844 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
1845 DEFINE_PROP_ON_OFF_AUTO("disable-legacy", VirtIOPCIProxy, disable_legacy,
1847 DEFINE_PROP_BOOL("disable-modern", VirtIOPCIProxy, disable_modern, false),
1848 DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy, flags,
1849 VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT, true),
1850 DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy, flags,
1851 VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT, false),
1852 DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy, flags,
1853 VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT, false),
1854 DEFINE_PROP_BIT("page-per-vq", VirtIOPCIProxy, flags,
1855 VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT, false),
1856 DEFINE_PROP_BOOL("x-ignore-backend-features", VirtIOPCIProxy,
1857 ignore_backend_features, false),
1858 DEFINE_PROP_END_OF_LIST(),
1861 static void virtio_pci_dc_realize(DeviceState *qdev, Error **errp)
1863 VirtioPCIClass *vpciklass = VIRTIO_PCI_GET_CLASS(qdev);
1864 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1865 PCIDevice *pci_dev = &proxy->pci_dev;
1867 if (!(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_PCIE) &&
1868 virtio_pci_modern(proxy)) {
1869 pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
1872 vpciklass->parent_dc_realize(qdev, errp);
1875 static void virtio_pci_class_init(ObjectClass *klass, void *data)
1877 DeviceClass *dc = DEVICE_CLASS(klass);
1878 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1879 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
1881 dc->props = virtio_pci_properties;
1882 k->realize = virtio_pci_realize;
1883 k->exit = virtio_pci_exit;
1884 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1885 k->revision = VIRTIO_PCI_ABI_VERSION;
1886 k->class_id = PCI_CLASS_OTHERS;
1887 vpciklass->parent_dc_realize = dc->realize;
1888 dc->realize = virtio_pci_dc_realize;
1889 dc->reset = virtio_pci_reset;
1892 static const TypeInfo virtio_pci_info = {
1893 .name = TYPE_VIRTIO_PCI,
1894 .parent = TYPE_PCI_DEVICE,
1895 .instance_size = sizeof(VirtIOPCIProxy),
1896 .class_init = virtio_pci_class_init,
1897 .class_size = sizeof(VirtioPCIClass),
1901 /* virtio-blk-pci */
1903 static Property virtio_blk_pci_properties[] = {
1904 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1905 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1906 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1907 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1908 DEFINE_PROP_END_OF_LIST(),
1911 static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1913 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
1914 DeviceState *vdev = DEVICE(&dev->vdev);
1916 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1917 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1920 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data)
1922 DeviceClass *dc = DEVICE_CLASS(klass);
1923 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1924 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1926 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1927 dc->props = virtio_blk_pci_properties;
1928 k->realize = virtio_blk_pci_realize;
1929 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1930 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
1931 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1932 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1935 static void virtio_blk_pci_instance_init(Object *obj)
1937 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj);
1939 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1941 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread",
1943 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
1944 "bootindex", &error_abort);
1947 static const TypeInfo virtio_blk_pci_info = {
1948 .name = TYPE_VIRTIO_BLK_PCI,
1949 .parent = TYPE_VIRTIO_PCI,
1950 .instance_size = sizeof(VirtIOBlkPCI),
1951 .instance_init = virtio_blk_pci_instance_init,
1952 .class_init = virtio_blk_pci_class_init,
1955 /* virtio-scsi-pci */
1957 static Property virtio_scsi_pci_properties[] = {
1958 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1959 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1960 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1961 DEV_NVECTORS_UNSPECIFIED),
1962 DEFINE_PROP_END_OF_LIST(),
1965 static void virtio_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1967 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
1968 DeviceState *vdev = DEVICE(&dev->vdev);
1969 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1970 DeviceState *proxy = DEVICE(vpci_dev);
1973 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1974 vpci_dev->nvectors = vs->conf.num_queues + 3;
1978 * For command line compatibility, this sets the virtio-scsi-device bus
1982 bus_name = g_strdup_printf("%s.0", proxy->id);
1983 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1987 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1988 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1991 static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data)
1993 DeviceClass *dc = DEVICE_CLASS(klass);
1994 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1995 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1997 k->realize = virtio_scsi_pci_realize;
1998 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1999 dc->props = virtio_scsi_pci_properties;
2000 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2001 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
2002 pcidev_k->revision = 0x00;
2003 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
2006 static void virtio_scsi_pci_instance_init(Object *obj)
2008 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj);
2010 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2012 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread",
2016 static const TypeInfo virtio_scsi_pci_info = {
2017 .name = TYPE_VIRTIO_SCSI_PCI,
2018 .parent = TYPE_VIRTIO_PCI,
2019 .instance_size = sizeof(VirtIOSCSIPCI),
2020 .instance_init = virtio_scsi_pci_instance_init,
2021 .class_init = virtio_scsi_pci_class_init,
2024 /* vhost-scsi-pci */
2026 #ifdef CONFIG_VHOST_SCSI
2027 static Property vhost_scsi_pci_properties[] = {
2028 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
2029 DEV_NVECTORS_UNSPECIFIED),
2030 DEFINE_PROP_END_OF_LIST(),
2033 static void vhost_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2035 VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev);
2036 DeviceState *vdev = DEVICE(&dev->vdev);
2037 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
2039 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
2040 vpci_dev->nvectors = vs->conf.num_queues + 3;
2043 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2044 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2047 static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data)
2049 DeviceClass *dc = DEVICE_CLASS(klass);
2050 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2051 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2052 k->realize = vhost_scsi_pci_realize;
2053 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2054 dc->props = vhost_scsi_pci_properties;
2055 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2056 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
2057 pcidev_k->revision = 0x00;
2058 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
2061 static void vhost_scsi_pci_instance_init(Object *obj)
2063 VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj);
2065 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2067 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
2068 "bootindex", &error_abort);
2071 static const TypeInfo vhost_scsi_pci_info = {
2072 .name = TYPE_VHOST_SCSI_PCI,
2073 .parent = TYPE_VIRTIO_PCI,
2074 .instance_size = sizeof(VHostSCSIPCI),
2075 .instance_init = vhost_scsi_pci_instance_init,
2076 .class_init = vhost_scsi_pci_class_init,
2080 /* vhost-vsock-pci */
2082 #ifdef CONFIG_VHOST_VSOCK
2083 static Property vhost_vsock_pci_properties[] = {
2084 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
2085 DEFINE_PROP_END_OF_LIST(),
2088 static void vhost_vsock_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2090 VHostVSockPCI *dev = VHOST_VSOCK_PCI(vpci_dev);
2091 DeviceState *vdev = DEVICE(&dev->vdev);
2093 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2094 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2097 static void vhost_vsock_pci_class_init(ObjectClass *klass, void *data)
2099 DeviceClass *dc = DEVICE_CLASS(klass);
2100 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2101 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2102 k->realize = vhost_vsock_pci_realize;
2103 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2104 dc->props = vhost_vsock_pci_properties;
2105 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2106 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_VSOCK;
2107 pcidev_k->revision = 0x00;
2108 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
2111 static void vhost_vsock_pci_instance_init(Object *obj)
2113 VHostVSockPCI *dev = VHOST_VSOCK_PCI(obj);
2115 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2119 static const TypeInfo vhost_vsock_pci_info = {
2120 .name = TYPE_VHOST_VSOCK_PCI,
2121 .parent = TYPE_VIRTIO_PCI,
2122 .instance_size = sizeof(VHostVSockPCI),
2123 .instance_init = vhost_vsock_pci_instance_init,
2124 .class_init = vhost_vsock_pci_class_init,
2128 /* virtio-balloon-pci */
2130 static Property virtio_balloon_pci_properties[] = {
2131 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2132 DEFINE_PROP_END_OF_LIST(),
2135 static void virtio_balloon_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2137 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev);
2138 DeviceState *vdev = DEVICE(&dev->vdev);
2140 if (vpci_dev->class_code != PCI_CLASS_OTHERS &&
2141 vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
2142 vpci_dev->class_code = PCI_CLASS_OTHERS;
2145 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2146 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2149 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data)
2151 DeviceClass *dc = DEVICE_CLASS(klass);
2152 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2153 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2154 k->realize = virtio_balloon_pci_realize;
2155 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2156 dc->props = virtio_balloon_pci_properties;
2157 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2158 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
2159 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2160 pcidev_k->class_id = PCI_CLASS_OTHERS;
2163 static void virtio_balloon_pci_instance_init(Object *obj)
2165 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj);
2167 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2168 TYPE_VIRTIO_BALLOON);
2169 object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev),
2170 "guest-stats", &error_abort);
2171 object_property_add_alias(obj, "guest-stats-polling-interval",
2173 "guest-stats-polling-interval", &error_abort);
2176 static const TypeInfo virtio_balloon_pci_info = {
2177 .name = TYPE_VIRTIO_BALLOON_PCI,
2178 .parent = TYPE_VIRTIO_PCI,
2179 .instance_size = sizeof(VirtIOBalloonPCI),
2180 .instance_init = virtio_balloon_pci_instance_init,
2181 .class_init = virtio_balloon_pci_class_init,
2184 /* virtio-serial-pci */
2186 static void virtio_serial_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2188 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
2189 DeviceState *vdev = DEVICE(&dev->vdev);
2190 DeviceState *proxy = DEVICE(vpci_dev);
2193 if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
2194 vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
2195 vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */
2196 vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER;
2199 /* backwards-compatibility with machines that were created with
2200 DEV_NVECTORS_UNSPECIFIED */
2201 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
2202 vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1;
2206 * For command line compatibility, this sets the virtio-serial-device bus
2210 bus_name = g_strdup_printf("%s.0", proxy->id);
2211 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
2215 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2216 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2219 static Property virtio_serial_pci_properties[] = {
2220 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
2221 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
2222 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
2223 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2224 DEFINE_PROP_END_OF_LIST(),
2227 static void virtio_serial_pci_class_init(ObjectClass *klass, void *data)
2229 DeviceClass *dc = DEVICE_CLASS(klass);
2230 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2231 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2232 k->realize = virtio_serial_pci_realize;
2233 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2234 dc->props = virtio_serial_pci_properties;
2235 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2236 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
2237 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2238 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
2241 static void virtio_serial_pci_instance_init(Object *obj)
2243 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj);
2245 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2246 TYPE_VIRTIO_SERIAL);
2249 static const TypeInfo virtio_serial_pci_info = {
2250 .name = TYPE_VIRTIO_SERIAL_PCI,
2251 .parent = TYPE_VIRTIO_PCI,
2252 .instance_size = sizeof(VirtIOSerialPCI),
2253 .instance_init = virtio_serial_pci_instance_init,
2254 .class_init = virtio_serial_pci_class_init,
2257 /* virtio-net-pci */
2259 static Property virtio_net_properties[] = {
2260 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
2261 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
2262 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
2263 DEFINE_PROP_END_OF_LIST(),
2266 static void virtio_net_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2268 DeviceState *qdev = DEVICE(vpci_dev);
2269 VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
2270 DeviceState *vdev = DEVICE(&dev->vdev);
2272 virtio_net_set_netclient_name(&dev->vdev, qdev->id,
2273 object_get_typename(OBJECT(qdev)));
2274 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2275 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2278 static void virtio_net_pci_class_init(ObjectClass *klass, void *data)
2280 DeviceClass *dc = DEVICE_CLASS(klass);
2281 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
2282 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
2284 k->romfile = "efi-virtio.rom";
2285 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2286 k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
2287 k->revision = VIRTIO_PCI_ABI_VERSION;
2288 k->class_id = PCI_CLASS_NETWORK_ETHERNET;
2289 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
2290 dc->props = virtio_net_properties;
2291 vpciklass->realize = virtio_net_pci_realize;
2294 static void virtio_net_pci_instance_init(Object *obj)
2296 VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
2298 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2300 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
2301 "bootindex", &error_abort);
2304 static const TypeInfo virtio_net_pci_info = {
2305 .name = TYPE_VIRTIO_NET_PCI,
2306 .parent = TYPE_VIRTIO_PCI,
2307 .instance_size = sizeof(VirtIONetPCI),
2308 .instance_init = virtio_net_pci_instance_init,
2309 .class_init = virtio_net_pci_class_init,
2312 /* virtio-rng-pci */
2314 static void virtio_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2316 VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev);
2317 DeviceState *vdev = DEVICE(&vrng->vdev);
2320 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2321 object_property_set_bool(OBJECT(vdev), true, "realized", &err);
2323 error_propagate(errp, err);
2327 object_property_set_link(OBJECT(vrng),
2328 OBJECT(vrng->vdev.conf.rng), "rng",
2332 static void virtio_rng_pci_class_init(ObjectClass *klass, void *data)
2334 DeviceClass *dc = DEVICE_CLASS(klass);
2335 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2336 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2338 k->realize = virtio_rng_pci_realize;
2339 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2341 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2342 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
2343 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2344 pcidev_k->class_id = PCI_CLASS_OTHERS;
2347 static void virtio_rng_initfn(Object *obj)
2349 VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj);
2351 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2353 object_property_add_alias(obj, "rng", OBJECT(&dev->vdev), "rng",
2357 static const TypeInfo virtio_rng_pci_info = {
2358 .name = TYPE_VIRTIO_RNG_PCI,
2359 .parent = TYPE_VIRTIO_PCI,
2360 .instance_size = sizeof(VirtIORngPCI),
2361 .instance_init = virtio_rng_initfn,
2362 .class_init = virtio_rng_pci_class_init,
2365 /* virtio-input-pci */
2367 static Property virtio_input_pci_properties[] = {
2368 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
2369 DEFINE_PROP_END_OF_LIST(),
2372 static void virtio_input_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2374 VirtIOInputPCI *vinput = VIRTIO_INPUT_PCI(vpci_dev);
2375 DeviceState *vdev = DEVICE(&vinput->vdev);
2377 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2378 virtio_pci_force_virtio_1(vpci_dev);
2379 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2382 static void virtio_input_pci_class_init(ObjectClass *klass, void *data)
2384 DeviceClass *dc = DEVICE_CLASS(klass);
2385 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2386 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2388 dc->props = virtio_input_pci_properties;
2389 k->realize = virtio_input_pci_realize;
2390 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2392 pcidev_k->class_id = PCI_CLASS_INPUT_OTHER;
2395 static void virtio_input_hid_kbd_pci_class_init(ObjectClass *klass, void *data)
2397 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2399 pcidev_k->class_id = PCI_CLASS_INPUT_KEYBOARD;
2402 static void virtio_input_hid_mouse_pci_class_init(ObjectClass *klass,
2405 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2407 pcidev_k->class_id = PCI_CLASS_INPUT_MOUSE;
2410 static void virtio_keyboard_initfn(Object *obj)
2412 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2414 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2415 TYPE_VIRTIO_KEYBOARD);
2418 static void virtio_mouse_initfn(Object *obj)
2420 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2422 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2426 static void virtio_tablet_initfn(Object *obj)
2428 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2430 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2431 TYPE_VIRTIO_TABLET);
2434 static const TypeInfo virtio_input_pci_info = {
2435 .name = TYPE_VIRTIO_INPUT_PCI,
2436 .parent = TYPE_VIRTIO_PCI,
2437 .instance_size = sizeof(VirtIOInputPCI),
2438 .class_init = virtio_input_pci_class_init,
2442 static const TypeInfo virtio_input_hid_pci_info = {
2443 .name = TYPE_VIRTIO_INPUT_HID_PCI,
2444 .parent = TYPE_VIRTIO_INPUT_PCI,
2445 .instance_size = sizeof(VirtIOInputHIDPCI),
2449 static const TypeInfo virtio_keyboard_pci_info = {
2450 .name = TYPE_VIRTIO_KEYBOARD_PCI,
2451 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2452 .class_init = virtio_input_hid_kbd_pci_class_init,
2453 .instance_size = sizeof(VirtIOInputHIDPCI),
2454 .instance_init = virtio_keyboard_initfn,
2457 static const TypeInfo virtio_mouse_pci_info = {
2458 .name = TYPE_VIRTIO_MOUSE_PCI,
2459 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2460 .class_init = virtio_input_hid_mouse_pci_class_init,
2461 .instance_size = sizeof(VirtIOInputHIDPCI),
2462 .instance_init = virtio_mouse_initfn,
2465 static const TypeInfo virtio_tablet_pci_info = {
2466 .name = TYPE_VIRTIO_TABLET_PCI,
2467 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2468 .instance_size = sizeof(VirtIOInputHIDPCI),
2469 .instance_init = virtio_tablet_initfn,
2473 static void virtio_host_initfn(Object *obj)
2475 VirtIOInputHostPCI *dev = VIRTIO_INPUT_HOST_PCI(obj);
2477 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2478 TYPE_VIRTIO_INPUT_HOST);
2481 static const TypeInfo virtio_host_pci_info = {
2482 .name = TYPE_VIRTIO_INPUT_HOST_PCI,
2483 .parent = TYPE_VIRTIO_INPUT_PCI,
2484 .instance_size = sizeof(VirtIOInputHostPCI),
2485 .instance_init = virtio_host_initfn,
2489 /* virtio-pci-bus */
2491 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
2492 VirtIOPCIProxy *dev)
2494 DeviceState *qdev = DEVICE(dev);
2495 char virtio_bus_name[] = "virtio-bus";
2497 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev,
2501 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
2503 BusClass *bus_class = BUS_CLASS(klass);
2504 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
2505 bus_class->max_dev = 1;
2506 k->notify = virtio_pci_notify;
2507 k->save_config = virtio_pci_save_config;
2508 k->load_config = virtio_pci_load_config;
2509 k->save_queue = virtio_pci_save_queue;
2510 k->load_queue = virtio_pci_load_queue;
2511 k->save_extra_state = virtio_pci_save_extra_state;
2512 k->load_extra_state = virtio_pci_load_extra_state;
2513 k->has_extra_state = virtio_pci_has_extra_state;
2514 k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
2515 k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
2516 k->vmstate_change = virtio_pci_vmstate_change;
2517 k->pre_plugged = virtio_pci_pre_plugged;
2518 k->device_plugged = virtio_pci_device_plugged;
2519 k->device_unplugged = virtio_pci_device_unplugged;
2520 k->query_nvectors = virtio_pci_query_nvectors;
2521 k->ioeventfd_enabled = virtio_pci_ioeventfd_enabled;
2522 k->ioeventfd_assign = virtio_pci_ioeventfd_assign;
2525 static const TypeInfo virtio_pci_bus_info = {
2526 .name = TYPE_VIRTIO_PCI_BUS,
2527 .parent = TYPE_VIRTIO_BUS,
2528 .instance_size = sizeof(VirtioPCIBusState),
2529 .class_init = virtio_pci_bus_class_init,
2532 static void virtio_pci_register_types(void)
2534 type_register_static(&virtio_rng_pci_info);
2535 type_register_static(&virtio_input_pci_info);
2536 type_register_static(&virtio_input_hid_pci_info);
2537 type_register_static(&virtio_keyboard_pci_info);
2538 type_register_static(&virtio_mouse_pci_info);
2539 type_register_static(&virtio_tablet_pci_info);
2541 type_register_static(&virtio_host_pci_info);
2543 type_register_static(&virtio_pci_bus_info);
2544 type_register_static(&virtio_pci_info);
2545 #ifdef CONFIG_VIRTFS
2546 type_register_static(&virtio_9p_pci_info);
2548 type_register_static(&virtio_blk_pci_info);
2549 type_register_static(&virtio_scsi_pci_info);
2550 type_register_static(&virtio_balloon_pci_info);
2551 type_register_static(&virtio_serial_pci_info);
2552 type_register_static(&virtio_net_pci_info);
2553 #ifdef CONFIG_VHOST_SCSI
2554 type_register_static(&vhost_scsi_pci_info);
2556 #ifdef CONFIG_VHOST_VSOCK
2557 type_register_static(&vhost_vsock_pci_info);
2561 type_init(virtio_pci_register_types)