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 AddressSpace *virtio_pci_get_dma_as(DeviceState *d)
1149 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1150 PCIDevice *dev = &proxy->pci_dev;
1152 return pci_get_address_space(dev);
1155 static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
1156 struct virtio_pci_cap *cap)
1158 PCIDevice *dev = &proxy->pci_dev;
1161 offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 0, cap->cap_len);
1164 assert(cap->cap_len >= sizeof *cap);
1165 memcpy(dev->config + offset + PCI_CAP_FLAGS, &cap->cap_len,
1166 cap->cap_len - PCI_CAP_FLAGS);
1171 static uint64_t virtio_pci_common_read(void *opaque, hwaddr addr,
1174 VirtIOPCIProxy *proxy = opaque;
1175 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1180 case VIRTIO_PCI_COMMON_DFSELECT:
1181 val = proxy->dfselect;
1183 case VIRTIO_PCI_COMMON_DF:
1184 if (proxy->dfselect <= 1) {
1185 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
1187 val = (vdev->host_features & ~vdc->legacy_features) >>
1188 (32 * proxy->dfselect);
1191 case VIRTIO_PCI_COMMON_GFSELECT:
1192 val = proxy->gfselect;
1194 case VIRTIO_PCI_COMMON_GF:
1195 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1196 val = proxy->guest_features[proxy->gfselect];
1199 case VIRTIO_PCI_COMMON_MSIX:
1200 val = vdev->config_vector;
1202 case VIRTIO_PCI_COMMON_NUMQ:
1203 for (i = 0; i < VIRTIO_QUEUE_MAX; ++i) {
1204 if (virtio_queue_get_num(vdev, i)) {
1209 case VIRTIO_PCI_COMMON_STATUS:
1212 case VIRTIO_PCI_COMMON_CFGGENERATION:
1213 val = vdev->generation;
1215 case VIRTIO_PCI_COMMON_Q_SELECT:
1216 val = vdev->queue_sel;
1218 case VIRTIO_PCI_COMMON_Q_SIZE:
1219 val = virtio_queue_get_num(vdev, vdev->queue_sel);
1221 case VIRTIO_PCI_COMMON_Q_MSIX:
1222 val = virtio_queue_vector(vdev, vdev->queue_sel);
1224 case VIRTIO_PCI_COMMON_Q_ENABLE:
1225 val = proxy->vqs[vdev->queue_sel].enabled;
1227 case VIRTIO_PCI_COMMON_Q_NOFF:
1228 /* Simply map queues in order */
1229 val = vdev->queue_sel;
1231 case VIRTIO_PCI_COMMON_Q_DESCLO:
1232 val = proxy->vqs[vdev->queue_sel].desc[0];
1234 case VIRTIO_PCI_COMMON_Q_DESCHI:
1235 val = proxy->vqs[vdev->queue_sel].desc[1];
1237 case VIRTIO_PCI_COMMON_Q_AVAILLO:
1238 val = proxy->vqs[vdev->queue_sel].avail[0];
1240 case VIRTIO_PCI_COMMON_Q_AVAILHI:
1241 val = proxy->vqs[vdev->queue_sel].avail[1];
1243 case VIRTIO_PCI_COMMON_Q_USEDLO:
1244 val = proxy->vqs[vdev->queue_sel].used[0];
1246 case VIRTIO_PCI_COMMON_Q_USEDHI:
1247 val = proxy->vqs[vdev->queue_sel].used[1];
1256 static void virtio_pci_common_write(void *opaque, hwaddr addr,
1257 uint64_t val, unsigned size)
1259 VirtIOPCIProxy *proxy = opaque;
1260 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1263 case VIRTIO_PCI_COMMON_DFSELECT:
1264 proxy->dfselect = val;
1266 case VIRTIO_PCI_COMMON_GFSELECT:
1267 proxy->gfselect = val;
1269 case VIRTIO_PCI_COMMON_GF:
1270 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1271 proxy->guest_features[proxy->gfselect] = val;
1272 virtio_set_features(vdev,
1273 (((uint64_t)proxy->guest_features[1]) << 32) |
1274 proxy->guest_features[0]);
1277 case VIRTIO_PCI_COMMON_MSIX:
1278 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
1279 /* Make it possible for guest to discover an error took place. */
1280 if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1281 val = VIRTIO_NO_VECTOR;
1283 vdev->config_vector = val;
1285 case VIRTIO_PCI_COMMON_STATUS:
1286 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
1287 virtio_pci_stop_ioeventfd(proxy);
1290 virtio_set_status(vdev, val & 0xFF);
1292 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
1293 virtio_pci_start_ioeventfd(proxy);
1296 if (vdev->status == 0) {
1297 virtio_pci_reset(DEVICE(proxy));
1301 case VIRTIO_PCI_COMMON_Q_SELECT:
1302 if (val < VIRTIO_QUEUE_MAX) {
1303 vdev->queue_sel = val;
1306 case VIRTIO_PCI_COMMON_Q_SIZE:
1307 proxy->vqs[vdev->queue_sel].num = val;
1309 case VIRTIO_PCI_COMMON_Q_MSIX:
1310 msix_vector_unuse(&proxy->pci_dev,
1311 virtio_queue_vector(vdev, vdev->queue_sel));
1312 /* Make it possible for guest to discover an error took place. */
1313 if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1314 val = VIRTIO_NO_VECTOR;
1316 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
1318 case VIRTIO_PCI_COMMON_Q_ENABLE:
1319 /* TODO: need a way to put num back on reset. */
1320 virtio_queue_set_num(vdev, vdev->queue_sel,
1321 proxy->vqs[vdev->queue_sel].num);
1322 virtio_queue_set_rings(vdev, vdev->queue_sel,
1323 ((uint64_t)proxy->vqs[vdev->queue_sel].desc[1]) << 32 |
1324 proxy->vqs[vdev->queue_sel].desc[0],
1325 ((uint64_t)proxy->vqs[vdev->queue_sel].avail[1]) << 32 |
1326 proxy->vqs[vdev->queue_sel].avail[0],
1327 ((uint64_t)proxy->vqs[vdev->queue_sel].used[1]) << 32 |
1328 proxy->vqs[vdev->queue_sel].used[0]);
1329 proxy->vqs[vdev->queue_sel].enabled = 1;
1331 case VIRTIO_PCI_COMMON_Q_DESCLO:
1332 proxy->vqs[vdev->queue_sel].desc[0] = val;
1334 case VIRTIO_PCI_COMMON_Q_DESCHI:
1335 proxy->vqs[vdev->queue_sel].desc[1] = val;
1337 case VIRTIO_PCI_COMMON_Q_AVAILLO:
1338 proxy->vqs[vdev->queue_sel].avail[0] = val;
1340 case VIRTIO_PCI_COMMON_Q_AVAILHI:
1341 proxy->vqs[vdev->queue_sel].avail[1] = val;
1343 case VIRTIO_PCI_COMMON_Q_USEDLO:
1344 proxy->vqs[vdev->queue_sel].used[0] = val;
1346 case VIRTIO_PCI_COMMON_Q_USEDHI:
1347 proxy->vqs[vdev->queue_sel].used[1] = val;
1355 static uint64_t virtio_pci_notify_read(void *opaque, hwaddr addr,
1361 static void virtio_pci_notify_write(void *opaque, hwaddr addr,
1362 uint64_t val, unsigned size)
1364 VirtIODevice *vdev = opaque;
1365 VirtIOPCIProxy *proxy = VIRTIO_PCI(DEVICE(vdev)->parent_bus->parent);
1366 unsigned queue = addr / virtio_pci_queue_mem_mult(proxy);
1368 if (queue < VIRTIO_QUEUE_MAX) {
1369 virtio_queue_notify(vdev, queue);
1373 static void virtio_pci_notify_write_pio(void *opaque, hwaddr addr,
1374 uint64_t val, unsigned size)
1376 VirtIODevice *vdev = opaque;
1377 unsigned queue = val;
1379 if (queue < VIRTIO_QUEUE_MAX) {
1380 virtio_queue_notify(vdev, queue);
1384 static uint64_t virtio_pci_isr_read(void *opaque, hwaddr addr,
1387 VirtIOPCIProxy *proxy = opaque;
1388 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1389 uint64_t val = atomic_xchg(&vdev->isr, 0);
1390 pci_irq_deassert(&proxy->pci_dev);
1395 static void virtio_pci_isr_write(void *opaque, hwaddr addr,
1396 uint64_t val, unsigned size)
1400 static uint64_t virtio_pci_device_read(void *opaque, hwaddr addr,
1403 VirtIODevice *vdev = opaque;
1408 val = virtio_config_modern_readb(vdev, addr);
1411 val = virtio_config_modern_readw(vdev, addr);
1414 val = virtio_config_modern_readl(vdev, addr);
1420 static void virtio_pci_device_write(void *opaque, hwaddr addr,
1421 uint64_t val, unsigned size)
1423 VirtIODevice *vdev = opaque;
1426 virtio_config_modern_writeb(vdev, addr, val);
1429 virtio_config_modern_writew(vdev, addr, val);
1432 virtio_config_modern_writel(vdev, addr, val);
1437 static void virtio_pci_modern_regions_init(VirtIOPCIProxy *proxy)
1439 static const MemoryRegionOps common_ops = {
1440 .read = virtio_pci_common_read,
1441 .write = virtio_pci_common_write,
1443 .min_access_size = 1,
1444 .max_access_size = 4,
1446 .endianness = DEVICE_LITTLE_ENDIAN,
1448 static const MemoryRegionOps isr_ops = {
1449 .read = virtio_pci_isr_read,
1450 .write = virtio_pci_isr_write,
1452 .min_access_size = 1,
1453 .max_access_size = 4,
1455 .endianness = DEVICE_LITTLE_ENDIAN,
1457 static const MemoryRegionOps device_ops = {
1458 .read = virtio_pci_device_read,
1459 .write = virtio_pci_device_write,
1461 .min_access_size = 1,
1462 .max_access_size = 4,
1464 .endianness = DEVICE_LITTLE_ENDIAN,
1466 static const MemoryRegionOps notify_ops = {
1467 .read = virtio_pci_notify_read,
1468 .write = virtio_pci_notify_write,
1470 .min_access_size = 1,
1471 .max_access_size = 4,
1473 .endianness = DEVICE_LITTLE_ENDIAN,
1475 static const MemoryRegionOps notify_pio_ops = {
1476 .read = virtio_pci_notify_read,
1477 .write = virtio_pci_notify_write_pio,
1479 .min_access_size = 1,
1480 .max_access_size = 4,
1482 .endianness = DEVICE_LITTLE_ENDIAN,
1486 memory_region_init_io(&proxy->common.mr, OBJECT(proxy),
1489 "virtio-pci-common",
1490 proxy->common.size);
1492 memory_region_init_io(&proxy->isr.mr, OBJECT(proxy),
1498 memory_region_init_io(&proxy->device.mr, OBJECT(proxy),
1500 virtio_bus_get_device(&proxy->bus),
1501 "virtio-pci-device",
1502 proxy->device.size);
1504 memory_region_init_io(&proxy->notify.mr, OBJECT(proxy),
1506 virtio_bus_get_device(&proxy->bus),
1507 "virtio-pci-notify",
1508 proxy->notify.size);
1510 memory_region_init_io(&proxy->notify_pio.mr, OBJECT(proxy),
1512 virtio_bus_get_device(&proxy->bus),
1513 "virtio-pci-notify-pio",
1514 proxy->notify_pio.size);
1517 static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy,
1518 VirtIOPCIRegion *region,
1519 struct virtio_pci_cap *cap,
1523 memory_region_add_subregion(mr, region->offset, ®ion->mr);
1525 cap->cfg_type = region->type;
1527 cap->offset = cpu_to_le32(region->offset);
1528 cap->length = cpu_to_le32(region->size);
1529 virtio_pci_add_mem_cap(proxy, cap);
1533 static void virtio_pci_modern_mem_region_map(VirtIOPCIProxy *proxy,
1534 VirtIOPCIRegion *region,
1535 struct virtio_pci_cap *cap)
1537 virtio_pci_modern_region_map(proxy, region, cap,
1538 &proxy->modern_bar, proxy->modern_mem_bar_idx);
1541 static void virtio_pci_modern_io_region_map(VirtIOPCIProxy *proxy,
1542 VirtIOPCIRegion *region,
1543 struct virtio_pci_cap *cap)
1545 virtio_pci_modern_region_map(proxy, region, cap,
1546 &proxy->io_bar, proxy->modern_io_bar_idx);
1549 static void virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy *proxy,
1550 VirtIOPCIRegion *region)
1552 memory_region_del_subregion(&proxy->modern_bar,
1556 static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy *proxy,
1557 VirtIOPCIRegion *region)
1559 memory_region_del_subregion(&proxy->io_bar,
1563 static void virtio_pci_pre_plugged(DeviceState *d, Error **errp)
1565 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1566 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1568 if (virtio_pci_modern(proxy)) {
1569 virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
1572 virtio_add_feature(&vdev->host_features, VIRTIO_F_BAD_FEATURE);
1575 /* This is called by virtio-bus just after the device is plugged. */
1576 static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
1578 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1579 VirtioBusState *bus = &proxy->bus;
1580 bool legacy = virtio_pci_legacy(proxy);
1582 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1585 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1588 * Virtio capabilities present without
1589 * VIRTIO_F_VERSION_1 confuses guests
1591 if (!proxy->ignore_backend_features &&
1592 !virtio_has_feature(vdev->host_features, VIRTIO_F_VERSION_1)) {
1593 virtio_pci_disable_modern(proxy);
1596 error_setg(errp, "Device doesn't support modern mode, and legacy"
1597 " mode is disabled");
1598 error_append_hint(errp, "Set disable-legacy to off\n");
1604 modern = virtio_pci_modern(proxy);
1606 config = proxy->pci_dev.config;
1607 if (proxy->class_code) {
1608 pci_config_set_class(config, proxy->class_code);
1612 if (virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) {
1613 error_setg(errp, "VIRTIO_F_IOMMU_PLATFORM was supported by"
1614 "neither legacy nor transitional device.");
1617 /* legacy and transitional */
1618 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
1619 pci_get_word(config + PCI_VENDOR_ID));
1620 pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
1622 /* pure virtio-1.0 */
1623 pci_set_word(config + PCI_VENDOR_ID,
1624 PCI_VENDOR_ID_REDHAT_QUMRANET);
1625 pci_set_word(config + PCI_DEVICE_ID,
1626 0x1040 + virtio_bus_get_vdev_id(bus));
1627 pci_config_set_revision(config, 1);
1629 config[PCI_INTERRUPT_PIN] = 1;
1633 struct virtio_pci_cap cap = {
1634 .cap_len = sizeof cap,
1636 struct virtio_pci_notify_cap notify = {
1637 .cap.cap_len = sizeof notify,
1638 .notify_off_multiplier =
1639 cpu_to_le32(virtio_pci_queue_mem_mult(proxy)),
1641 struct virtio_pci_cfg_cap cfg = {
1642 .cap.cap_len = sizeof cfg,
1643 .cap.cfg_type = VIRTIO_PCI_CAP_PCI_CFG,
1645 struct virtio_pci_notify_cap notify_pio = {
1646 .cap.cap_len = sizeof notify,
1647 .notify_off_multiplier = cpu_to_le32(0x0),
1650 struct virtio_pci_cfg_cap *cfg_mask;
1652 virtio_pci_modern_regions_init(proxy);
1654 virtio_pci_modern_mem_region_map(proxy, &proxy->common, &cap);
1655 virtio_pci_modern_mem_region_map(proxy, &proxy->isr, &cap);
1656 virtio_pci_modern_mem_region_map(proxy, &proxy->device, &cap);
1657 virtio_pci_modern_mem_region_map(proxy, &proxy->notify, ¬ify.cap);
1660 memory_region_init(&proxy->io_bar, OBJECT(proxy),
1661 "virtio-pci-io", 0x4);
1663 pci_register_bar(&proxy->pci_dev, proxy->modern_io_bar_idx,
1664 PCI_BASE_ADDRESS_SPACE_IO, &proxy->io_bar);
1666 virtio_pci_modern_io_region_map(proxy, &proxy->notify_pio,
1670 pci_register_bar(&proxy->pci_dev, proxy->modern_mem_bar_idx,
1671 PCI_BASE_ADDRESS_SPACE_MEMORY |
1672 PCI_BASE_ADDRESS_MEM_PREFETCH |
1673 PCI_BASE_ADDRESS_MEM_TYPE_64,
1674 &proxy->modern_bar);
1676 proxy->config_cap = virtio_pci_add_mem_cap(proxy, &cfg.cap);
1677 cfg_mask = (void *)(proxy->pci_dev.wmask + proxy->config_cap);
1678 pci_set_byte(&cfg_mask->cap.bar, ~0x0);
1679 pci_set_long((uint8_t *)&cfg_mask->cap.offset, ~0x0);
1680 pci_set_long((uint8_t *)&cfg_mask->cap.length, ~0x0);
1681 pci_set_long(cfg_mask->pci_cfg_data, ~0x0);
1684 if (proxy->nvectors) {
1685 int err = msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors,
1686 proxy->msix_bar_idx);
1688 /* Notice when a system that supports MSIx can't initialize it. */
1689 if (err != -ENOTSUP) {
1690 error_report("unable to init msix vectors to %" PRIu32,
1693 proxy->nvectors = 0;
1697 proxy->pci_dev.config_write = virtio_write_config;
1698 proxy->pci_dev.config_read = virtio_read_config;
1701 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
1702 + virtio_bus_get_vdev_config_len(bus);
1703 size = pow2ceil(size);
1705 memory_region_init_io(&proxy->bar, OBJECT(proxy),
1706 &virtio_pci_config_ops,
1707 proxy, "virtio-pci", size);
1709 pci_register_bar(&proxy->pci_dev, proxy->legacy_io_bar_idx,
1710 PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar);
1714 static void virtio_pci_device_unplugged(DeviceState *d)
1716 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1717 bool modern = virtio_pci_modern(proxy);
1718 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1720 virtio_pci_stop_ioeventfd(proxy);
1723 virtio_pci_modern_mem_region_unmap(proxy, &proxy->common);
1724 virtio_pci_modern_mem_region_unmap(proxy, &proxy->isr);
1725 virtio_pci_modern_mem_region_unmap(proxy, &proxy->device);
1726 virtio_pci_modern_mem_region_unmap(proxy, &proxy->notify);
1728 virtio_pci_modern_io_region_unmap(proxy, &proxy->notify_pio);
1733 static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
1735 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1736 VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
1737 bool pcie_port = pci_bus_is_express(pci_dev->bus) &&
1738 !pci_bus_is_root(pci_dev->bus);
1740 if (!kvm_has_many_ioeventfds()) {
1741 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
1745 * virtio pci bar layout used by default.
1746 * subclasses can re-arrange things if needed.
1748 * region 0 -- virtio legacy io bar
1749 * region 1 -- msi-x bar
1750 * region 4+5 -- virtio modern memory (64bit) bar
1753 proxy->legacy_io_bar_idx = 0;
1754 proxy->msix_bar_idx = 1;
1755 proxy->modern_io_bar_idx = 2;
1756 proxy->modern_mem_bar_idx = 4;
1758 proxy->common.offset = 0x0;
1759 proxy->common.size = 0x1000;
1760 proxy->common.type = VIRTIO_PCI_CAP_COMMON_CFG;
1762 proxy->isr.offset = 0x1000;
1763 proxy->isr.size = 0x1000;
1764 proxy->isr.type = VIRTIO_PCI_CAP_ISR_CFG;
1766 proxy->device.offset = 0x2000;
1767 proxy->device.size = 0x1000;
1768 proxy->device.type = VIRTIO_PCI_CAP_DEVICE_CFG;
1770 proxy->notify.offset = 0x3000;
1771 proxy->notify.size = virtio_pci_queue_mem_mult(proxy) * VIRTIO_QUEUE_MAX;
1772 proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
1774 proxy->notify_pio.offset = 0x0;
1775 proxy->notify_pio.size = 0x4;
1776 proxy->notify_pio.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
1778 /* subclasses can enforce modern, so do this unconditionally */
1779 memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci",
1780 /* PCI BAR regions must be powers of 2 */
1781 pow2ceil(proxy->notify.offset + proxy->notify.size));
1783 memory_region_init_alias(&proxy->modern_cfg,
1788 memory_region_size(&proxy->modern_bar));
1790 address_space_init(&proxy->modern_as, &proxy->modern_cfg, "virtio-pci-cfg-as");
1792 if (proxy->disable_legacy == ON_OFF_AUTO_AUTO) {
1793 proxy->disable_legacy = pcie_port ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
1796 if (!virtio_pci_modern(proxy) && !virtio_pci_legacy(proxy)) {
1797 error_setg(errp, "device cannot work as neither modern nor legacy mode"
1799 error_append_hint(errp, "Set either disable-modern or disable-legacy"
1804 if (pcie_port && pci_is_express(pci_dev)) {
1807 pos = pcie_endpoint_cap_init(pci_dev, 0);
1810 pos = pci_add_capability(pci_dev, PCI_CAP_ID_PM, 0, PCI_PM_SIZEOF);
1814 * Indicates that this function complies with revision 1.2 of the
1815 * PCI Power Management Interface Specification.
1817 pci_set_word(pci_dev->config + pos + PCI_PM_PMC, 0x3);
1820 * make future invocations of pci_is_express() return false
1821 * and pci_config_size() return PCI_CONFIG_SPACE_SIZE.
1823 pci_dev->cap_present &= ~QEMU_PCI_CAP_EXPRESS;
1826 virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
1828 k->realize(proxy, errp);
1832 static void virtio_pci_exit(PCIDevice *pci_dev)
1834 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1836 msix_uninit_exclusive_bar(pci_dev);
1837 address_space_destroy(&proxy->modern_as);
1840 static void virtio_pci_reset(DeviceState *qdev)
1842 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1843 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
1846 virtio_pci_stop_ioeventfd(proxy);
1847 virtio_bus_reset(bus);
1848 msix_unuse_all_vectors(&proxy->pci_dev);
1850 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1851 proxy->vqs[i].enabled = 0;
1855 static Property virtio_pci_properties[] = {
1856 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
1857 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
1858 DEFINE_PROP_ON_OFF_AUTO("disable-legacy", VirtIOPCIProxy, disable_legacy,
1860 DEFINE_PROP_BOOL("disable-modern", VirtIOPCIProxy, disable_modern, false),
1861 DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy, flags,
1862 VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT, true),
1863 DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy, flags,
1864 VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT, false),
1865 DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy, flags,
1866 VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT, false),
1867 DEFINE_PROP_BIT("page-per-vq", VirtIOPCIProxy, flags,
1868 VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT, false),
1869 DEFINE_PROP_BOOL("x-ignore-backend-features", VirtIOPCIProxy,
1870 ignore_backend_features, false),
1871 DEFINE_PROP_END_OF_LIST(),
1874 static void virtio_pci_dc_realize(DeviceState *qdev, Error **errp)
1876 VirtioPCIClass *vpciklass = VIRTIO_PCI_GET_CLASS(qdev);
1877 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1878 PCIDevice *pci_dev = &proxy->pci_dev;
1880 if (!(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_PCIE) &&
1881 virtio_pci_modern(proxy)) {
1882 pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
1885 vpciklass->parent_dc_realize(qdev, errp);
1888 static void virtio_pci_class_init(ObjectClass *klass, void *data)
1890 DeviceClass *dc = DEVICE_CLASS(klass);
1891 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1892 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
1894 dc->props = virtio_pci_properties;
1895 k->realize = virtio_pci_realize;
1896 k->exit = virtio_pci_exit;
1897 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1898 k->revision = VIRTIO_PCI_ABI_VERSION;
1899 k->class_id = PCI_CLASS_OTHERS;
1900 vpciklass->parent_dc_realize = dc->realize;
1901 dc->realize = virtio_pci_dc_realize;
1902 dc->reset = virtio_pci_reset;
1905 static const TypeInfo virtio_pci_info = {
1906 .name = TYPE_VIRTIO_PCI,
1907 .parent = TYPE_PCI_DEVICE,
1908 .instance_size = sizeof(VirtIOPCIProxy),
1909 .class_init = virtio_pci_class_init,
1910 .class_size = sizeof(VirtioPCIClass),
1914 /* virtio-blk-pci */
1916 static Property virtio_blk_pci_properties[] = {
1917 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1918 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1919 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1920 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1921 DEFINE_PROP_END_OF_LIST(),
1924 static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1926 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
1927 DeviceState *vdev = DEVICE(&dev->vdev);
1929 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1930 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1933 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data)
1935 DeviceClass *dc = DEVICE_CLASS(klass);
1936 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1937 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1939 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1940 dc->props = virtio_blk_pci_properties;
1941 k->realize = virtio_blk_pci_realize;
1942 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1943 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
1944 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1945 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1948 static void virtio_blk_pci_instance_init(Object *obj)
1950 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj);
1952 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1954 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread",
1956 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
1957 "bootindex", &error_abort);
1960 static const TypeInfo virtio_blk_pci_info = {
1961 .name = TYPE_VIRTIO_BLK_PCI,
1962 .parent = TYPE_VIRTIO_PCI,
1963 .instance_size = sizeof(VirtIOBlkPCI),
1964 .instance_init = virtio_blk_pci_instance_init,
1965 .class_init = virtio_blk_pci_class_init,
1968 /* virtio-scsi-pci */
1970 static Property virtio_scsi_pci_properties[] = {
1971 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1972 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1973 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1974 DEV_NVECTORS_UNSPECIFIED),
1975 DEFINE_PROP_END_OF_LIST(),
1978 static void virtio_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1980 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
1981 DeviceState *vdev = DEVICE(&dev->vdev);
1982 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1983 DeviceState *proxy = DEVICE(vpci_dev);
1986 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1987 vpci_dev->nvectors = vs->conf.num_queues + 3;
1991 * For command line compatibility, this sets the virtio-scsi-device bus
1995 bus_name = g_strdup_printf("%s.0", proxy->id);
1996 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
2000 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2001 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2004 static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data)
2006 DeviceClass *dc = DEVICE_CLASS(klass);
2007 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2008 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2010 k->realize = virtio_scsi_pci_realize;
2011 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2012 dc->props = virtio_scsi_pci_properties;
2013 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2014 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
2015 pcidev_k->revision = 0x00;
2016 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
2019 static void virtio_scsi_pci_instance_init(Object *obj)
2021 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj);
2023 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2025 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread",
2029 static const TypeInfo virtio_scsi_pci_info = {
2030 .name = TYPE_VIRTIO_SCSI_PCI,
2031 .parent = TYPE_VIRTIO_PCI,
2032 .instance_size = sizeof(VirtIOSCSIPCI),
2033 .instance_init = virtio_scsi_pci_instance_init,
2034 .class_init = virtio_scsi_pci_class_init,
2037 /* vhost-scsi-pci */
2039 #ifdef CONFIG_VHOST_SCSI
2040 static Property vhost_scsi_pci_properties[] = {
2041 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
2042 DEV_NVECTORS_UNSPECIFIED),
2043 DEFINE_PROP_END_OF_LIST(),
2046 static void vhost_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2048 VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev);
2049 DeviceState *vdev = DEVICE(&dev->vdev);
2050 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
2052 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
2053 vpci_dev->nvectors = vs->conf.num_queues + 3;
2056 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2057 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2060 static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data)
2062 DeviceClass *dc = DEVICE_CLASS(klass);
2063 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2064 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2065 k->realize = vhost_scsi_pci_realize;
2066 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2067 dc->props = vhost_scsi_pci_properties;
2068 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2069 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
2070 pcidev_k->revision = 0x00;
2071 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
2074 static void vhost_scsi_pci_instance_init(Object *obj)
2076 VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj);
2078 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2080 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
2081 "bootindex", &error_abort);
2084 static const TypeInfo vhost_scsi_pci_info = {
2085 .name = TYPE_VHOST_SCSI_PCI,
2086 .parent = TYPE_VIRTIO_PCI,
2087 .instance_size = sizeof(VHostSCSIPCI),
2088 .instance_init = vhost_scsi_pci_instance_init,
2089 .class_init = vhost_scsi_pci_class_init,
2093 /* vhost-vsock-pci */
2095 #ifdef CONFIG_VHOST_VSOCK
2096 static Property vhost_vsock_pci_properties[] = {
2097 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
2098 DEFINE_PROP_END_OF_LIST(),
2101 static void vhost_vsock_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2103 VHostVSockPCI *dev = VHOST_VSOCK_PCI(vpci_dev);
2104 DeviceState *vdev = DEVICE(&dev->vdev);
2106 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2107 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2110 static void vhost_vsock_pci_class_init(ObjectClass *klass, void *data)
2112 DeviceClass *dc = DEVICE_CLASS(klass);
2113 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2114 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2115 k->realize = vhost_vsock_pci_realize;
2116 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2117 dc->props = vhost_vsock_pci_properties;
2118 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2119 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_VSOCK;
2120 pcidev_k->revision = 0x00;
2121 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
2124 static void vhost_vsock_pci_instance_init(Object *obj)
2126 VHostVSockPCI *dev = VHOST_VSOCK_PCI(obj);
2128 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2132 static const TypeInfo vhost_vsock_pci_info = {
2133 .name = TYPE_VHOST_VSOCK_PCI,
2134 .parent = TYPE_VIRTIO_PCI,
2135 .instance_size = sizeof(VHostVSockPCI),
2136 .instance_init = vhost_vsock_pci_instance_init,
2137 .class_init = vhost_vsock_pci_class_init,
2141 /* virtio-balloon-pci */
2143 static Property virtio_balloon_pci_properties[] = {
2144 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2145 DEFINE_PROP_END_OF_LIST(),
2148 static void virtio_balloon_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2150 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev);
2151 DeviceState *vdev = DEVICE(&dev->vdev);
2153 if (vpci_dev->class_code != PCI_CLASS_OTHERS &&
2154 vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
2155 vpci_dev->class_code = PCI_CLASS_OTHERS;
2158 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2159 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2162 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data)
2164 DeviceClass *dc = DEVICE_CLASS(klass);
2165 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2166 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2167 k->realize = virtio_balloon_pci_realize;
2168 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2169 dc->props = virtio_balloon_pci_properties;
2170 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2171 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
2172 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2173 pcidev_k->class_id = PCI_CLASS_OTHERS;
2176 static void virtio_balloon_pci_instance_init(Object *obj)
2178 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj);
2180 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2181 TYPE_VIRTIO_BALLOON);
2182 object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev),
2183 "guest-stats", &error_abort);
2184 object_property_add_alias(obj, "guest-stats-polling-interval",
2186 "guest-stats-polling-interval", &error_abort);
2189 static const TypeInfo virtio_balloon_pci_info = {
2190 .name = TYPE_VIRTIO_BALLOON_PCI,
2191 .parent = TYPE_VIRTIO_PCI,
2192 .instance_size = sizeof(VirtIOBalloonPCI),
2193 .instance_init = virtio_balloon_pci_instance_init,
2194 .class_init = virtio_balloon_pci_class_init,
2197 /* virtio-serial-pci */
2199 static void virtio_serial_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2201 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
2202 DeviceState *vdev = DEVICE(&dev->vdev);
2203 DeviceState *proxy = DEVICE(vpci_dev);
2206 if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
2207 vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
2208 vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */
2209 vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER;
2212 /* backwards-compatibility with machines that were created with
2213 DEV_NVECTORS_UNSPECIFIED */
2214 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
2215 vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1;
2219 * For command line compatibility, this sets the virtio-serial-device bus
2223 bus_name = g_strdup_printf("%s.0", proxy->id);
2224 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
2228 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2229 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2232 static Property virtio_serial_pci_properties[] = {
2233 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
2234 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
2235 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
2236 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2237 DEFINE_PROP_END_OF_LIST(),
2240 static void virtio_serial_pci_class_init(ObjectClass *klass, void *data)
2242 DeviceClass *dc = DEVICE_CLASS(klass);
2243 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2244 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2245 k->realize = virtio_serial_pci_realize;
2246 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2247 dc->props = virtio_serial_pci_properties;
2248 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2249 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
2250 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2251 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
2254 static void virtio_serial_pci_instance_init(Object *obj)
2256 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj);
2258 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2259 TYPE_VIRTIO_SERIAL);
2262 static const TypeInfo virtio_serial_pci_info = {
2263 .name = TYPE_VIRTIO_SERIAL_PCI,
2264 .parent = TYPE_VIRTIO_PCI,
2265 .instance_size = sizeof(VirtIOSerialPCI),
2266 .instance_init = virtio_serial_pci_instance_init,
2267 .class_init = virtio_serial_pci_class_init,
2270 /* virtio-net-pci */
2272 static Property virtio_net_properties[] = {
2273 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
2274 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
2275 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
2276 DEFINE_PROP_END_OF_LIST(),
2279 static void virtio_net_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2281 DeviceState *qdev = DEVICE(vpci_dev);
2282 VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
2283 DeviceState *vdev = DEVICE(&dev->vdev);
2285 virtio_net_set_netclient_name(&dev->vdev, qdev->id,
2286 object_get_typename(OBJECT(qdev)));
2287 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2288 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2291 static void virtio_net_pci_class_init(ObjectClass *klass, void *data)
2293 DeviceClass *dc = DEVICE_CLASS(klass);
2294 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
2295 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
2297 k->romfile = "efi-virtio.rom";
2298 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2299 k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
2300 k->revision = VIRTIO_PCI_ABI_VERSION;
2301 k->class_id = PCI_CLASS_NETWORK_ETHERNET;
2302 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
2303 dc->props = virtio_net_properties;
2304 vpciklass->realize = virtio_net_pci_realize;
2307 static void virtio_net_pci_instance_init(Object *obj)
2309 VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
2311 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2313 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
2314 "bootindex", &error_abort);
2317 static const TypeInfo virtio_net_pci_info = {
2318 .name = TYPE_VIRTIO_NET_PCI,
2319 .parent = TYPE_VIRTIO_PCI,
2320 .instance_size = sizeof(VirtIONetPCI),
2321 .instance_init = virtio_net_pci_instance_init,
2322 .class_init = virtio_net_pci_class_init,
2325 /* virtio-rng-pci */
2327 static void virtio_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2329 VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev);
2330 DeviceState *vdev = DEVICE(&vrng->vdev);
2333 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2334 object_property_set_bool(OBJECT(vdev), true, "realized", &err);
2336 error_propagate(errp, err);
2340 object_property_set_link(OBJECT(vrng),
2341 OBJECT(vrng->vdev.conf.rng), "rng",
2345 static void virtio_rng_pci_class_init(ObjectClass *klass, void *data)
2347 DeviceClass *dc = DEVICE_CLASS(klass);
2348 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2349 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2351 k->realize = virtio_rng_pci_realize;
2352 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2354 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2355 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
2356 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2357 pcidev_k->class_id = PCI_CLASS_OTHERS;
2360 static void virtio_rng_initfn(Object *obj)
2362 VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj);
2364 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2366 object_property_add_alias(obj, "rng", OBJECT(&dev->vdev), "rng",
2370 static const TypeInfo virtio_rng_pci_info = {
2371 .name = TYPE_VIRTIO_RNG_PCI,
2372 .parent = TYPE_VIRTIO_PCI,
2373 .instance_size = sizeof(VirtIORngPCI),
2374 .instance_init = virtio_rng_initfn,
2375 .class_init = virtio_rng_pci_class_init,
2378 /* virtio-input-pci */
2380 static Property virtio_input_pci_properties[] = {
2381 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
2382 DEFINE_PROP_END_OF_LIST(),
2385 static void virtio_input_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2387 VirtIOInputPCI *vinput = VIRTIO_INPUT_PCI(vpci_dev);
2388 DeviceState *vdev = DEVICE(&vinput->vdev);
2390 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2391 virtio_pci_force_virtio_1(vpci_dev);
2392 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2395 static void virtio_input_pci_class_init(ObjectClass *klass, void *data)
2397 DeviceClass *dc = DEVICE_CLASS(klass);
2398 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2399 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2401 dc->props = virtio_input_pci_properties;
2402 k->realize = virtio_input_pci_realize;
2403 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2405 pcidev_k->class_id = PCI_CLASS_INPUT_OTHER;
2408 static void virtio_input_hid_kbd_pci_class_init(ObjectClass *klass, void *data)
2410 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2412 pcidev_k->class_id = PCI_CLASS_INPUT_KEYBOARD;
2415 static void virtio_input_hid_mouse_pci_class_init(ObjectClass *klass,
2418 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2420 pcidev_k->class_id = PCI_CLASS_INPUT_MOUSE;
2423 static void virtio_keyboard_initfn(Object *obj)
2425 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2427 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2428 TYPE_VIRTIO_KEYBOARD);
2431 static void virtio_mouse_initfn(Object *obj)
2433 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2435 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2439 static void virtio_tablet_initfn(Object *obj)
2441 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2443 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2444 TYPE_VIRTIO_TABLET);
2447 static const TypeInfo virtio_input_pci_info = {
2448 .name = TYPE_VIRTIO_INPUT_PCI,
2449 .parent = TYPE_VIRTIO_PCI,
2450 .instance_size = sizeof(VirtIOInputPCI),
2451 .class_init = virtio_input_pci_class_init,
2455 static const TypeInfo virtio_input_hid_pci_info = {
2456 .name = TYPE_VIRTIO_INPUT_HID_PCI,
2457 .parent = TYPE_VIRTIO_INPUT_PCI,
2458 .instance_size = sizeof(VirtIOInputHIDPCI),
2462 static const TypeInfo virtio_keyboard_pci_info = {
2463 .name = TYPE_VIRTIO_KEYBOARD_PCI,
2464 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2465 .class_init = virtio_input_hid_kbd_pci_class_init,
2466 .instance_size = sizeof(VirtIOInputHIDPCI),
2467 .instance_init = virtio_keyboard_initfn,
2470 static const TypeInfo virtio_mouse_pci_info = {
2471 .name = TYPE_VIRTIO_MOUSE_PCI,
2472 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2473 .class_init = virtio_input_hid_mouse_pci_class_init,
2474 .instance_size = sizeof(VirtIOInputHIDPCI),
2475 .instance_init = virtio_mouse_initfn,
2478 static const TypeInfo virtio_tablet_pci_info = {
2479 .name = TYPE_VIRTIO_TABLET_PCI,
2480 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2481 .instance_size = sizeof(VirtIOInputHIDPCI),
2482 .instance_init = virtio_tablet_initfn,
2486 static void virtio_host_initfn(Object *obj)
2488 VirtIOInputHostPCI *dev = VIRTIO_INPUT_HOST_PCI(obj);
2490 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2491 TYPE_VIRTIO_INPUT_HOST);
2494 static const TypeInfo virtio_host_pci_info = {
2495 .name = TYPE_VIRTIO_INPUT_HOST_PCI,
2496 .parent = TYPE_VIRTIO_INPUT_PCI,
2497 .instance_size = sizeof(VirtIOInputHostPCI),
2498 .instance_init = virtio_host_initfn,
2502 /* virtio-pci-bus */
2504 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
2505 VirtIOPCIProxy *dev)
2507 DeviceState *qdev = DEVICE(dev);
2508 char virtio_bus_name[] = "virtio-bus";
2510 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev,
2514 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
2516 BusClass *bus_class = BUS_CLASS(klass);
2517 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
2518 bus_class->max_dev = 1;
2519 k->notify = virtio_pci_notify;
2520 k->save_config = virtio_pci_save_config;
2521 k->load_config = virtio_pci_load_config;
2522 k->save_queue = virtio_pci_save_queue;
2523 k->load_queue = virtio_pci_load_queue;
2524 k->save_extra_state = virtio_pci_save_extra_state;
2525 k->load_extra_state = virtio_pci_load_extra_state;
2526 k->has_extra_state = virtio_pci_has_extra_state;
2527 k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
2528 k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
2529 k->vmstate_change = virtio_pci_vmstate_change;
2530 k->pre_plugged = virtio_pci_pre_plugged;
2531 k->device_plugged = virtio_pci_device_plugged;
2532 k->device_unplugged = virtio_pci_device_unplugged;
2533 k->query_nvectors = virtio_pci_query_nvectors;
2534 k->ioeventfd_enabled = virtio_pci_ioeventfd_enabled;
2535 k->ioeventfd_assign = virtio_pci_ioeventfd_assign;
2536 k->get_dma_as = virtio_pci_get_dma_as;
2539 static const TypeInfo virtio_pci_bus_info = {
2540 .name = TYPE_VIRTIO_PCI_BUS,
2541 .parent = TYPE_VIRTIO_BUS,
2542 .instance_size = sizeof(VirtioPCIBusState),
2543 .class_init = virtio_pci_bus_class_init,
2546 static void virtio_pci_register_types(void)
2548 type_register_static(&virtio_rng_pci_info);
2549 type_register_static(&virtio_input_pci_info);
2550 type_register_static(&virtio_input_hid_pci_info);
2551 type_register_static(&virtio_keyboard_pci_info);
2552 type_register_static(&virtio_mouse_pci_info);
2553 type_register_static(&virtio_tablet_pci_info);
2555 type_register_static(&virtio_host_pci_info);
2557 type_register_static(&virtio_pci_bus_info);
2558 type_register_static(&virtio_pci_info);
2559 #ifdef CONFIG_VIRTFS
2560 type_register_static(&virtio_9p_pci_info);
2562 type_register_static(&virtio_blk_pci_info);
2563 type_register_static(&virtio_scsi_pci_info);
2564 type_register_static(&virtio_balloon_pci_info);
2565 type_register_static(&virtio_serial_pci_info);
2566 type_register_static(&virtio_net_pci_info);
2567 #ifdef CONFIG_VHOST_SCSI
2568 type_register_static(&vhost_scsi_pci_info);
2570 #ifdef CONFIG_VHOST_VSOCK
2571 type_register_static(&vhost_vsock_pci_info);
2575 type_init(virtio_pci_register_types)