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, 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 !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
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_started(DeviceState *d)
267 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
269 return proxy->ioeventfd_started;
272 static void virtio_pci_ioeventfd_set_started(DeviceState *d, bool started,
275 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
277 proxy->ioeventfd_started = started;
280 static bool virtio_pci_ioeventfd_disabled(DeviceState *d)
282 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
284 return proxy->ioeventfd_disabled ||
285 !(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD);
288 static void virtio_pci_ioeventfd_set_disabled(DeviceState *d, bool disabled)
290 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
292 proxy->ioeventfd_disabled = disabled;
295 #define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000
297 static int virtio_pci_ioeventfd_assign(DeviceState *d, EventNotifier *notifier,
300 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
301 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
302 VirtQueue *vq = virtio_get_queue(vdev, n);
303 bool legacy = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_LEGACY);
304 bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
305 bool fast_mmio = kvm_ioeventfd_any_length_enabled();
306 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
307 MemoryRegion *modern_mr = &proxy->notify.mr;
308 MemoryRegion *modern_notify_mr = &proxy->notify_pio.mr;
309 MemoryRegion *legacy_mr = &proxy->bar;
310 hwaddr modern_addr = QEMU_VIRTIO_PCI_QUEUE_MEM_MULT *
311 virtio_get_queue_index(vq);
312 hwaddr legacy_addr = VIRTIO_PCI_QUEUE_NOTIFY;
317 memory_region_add_eventfd(modern_mr, modern_addr, 0,
320 memory_region_add_eventfd(modern_mr, modern_addr, 2,
324 memory_region_add_eventfd(modern_notify_mr, 0, 2,
329 memory_region_add_eventfd(legacy_mr, legacy_addr, 2,
335 memory_region_del_eventfd(modern_mr, modern_addr, 0,
338 memory_region_del_eventfd(modern_mr, modern_addr, 2,
342 memory_region_del_eventfd(modern_notify_mr, 0, 2,
347 memory_region_del_eventfd(legacy_mr, legacy_addr, 2,
354 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
356 virtio_bus_start_ioeventfd(&proxy->bus);
359 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
361 virtio_bus_stop_ioeventfd(&proxy->bus);
364 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
366 VirtIOPCIProxy *proxy = opaque;
367 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
371 case VIRTIO_PCI_GUEST_FEATURES:
372 /* Guest does not negotiate properly? We have to assume nothing. */
373 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
374 val = virtio_bus_get_vdev_bad_features(&proxy->bus);
376 virtio_set_features(vdev, val);
378 case VIRTIO_PCI_QUEUE_PFN:
379 pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
381 virtio_pci_reset(DEVICE(proxy));
384 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
386 case VIRTIO_PCI_QUEUE_SEL:
387 if (val < VIRTIO_QUEUE_MAX)
388 vdev->queue_sel = val;
390 case VIRTIO_PCI_QUEUE_NOTIFY:
391 if (val < VIRTIO_QUEUE_MAX) {
392 virtio_queue_notify(vdev, val);
395 case VIRTIO_PCI_STATUS:
396 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
397 virtio_pci_stop_ioeventfd(proxy);
400 virtio_set_status(vdev, val & 0xFF);
402 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
403 virtio_pci_start_ioeventfd(proxy);
406 if (vdev->status == 0) {
407 virtio_pci_reset(DEVICE(proxy));
410 /* Linux before 2.6.34 drives the device without enabling
411 the PCI device bus master bit. Enable it automatically
412 for the guest. This is a PCI spec violation but so is
413 initiating DMA with bus master bit clear. */
414 if (val == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER)) {
415 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
416 proxy->pci_dev.config[PCI_COMMAND] |
417 PCI_COMMAND_MASTER, 1);
420 case VIRTIO_MSI_CONFIG_VECTOR:
421 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
422 /* Make it possible for guest to discover an error took place. */
423 if (msix_vector_use(&proxy->pci_dev, val) < 0)
424 val = VIRTIO_NO_VECTOR;
425 vdev->config_vector = val;
427 case VIRTIO_MSI_QUEUE_VECTOR:
428 msix_vector_unuse(&proxy->pci_dev,
429 virtio_queue_vector(vdev, vdev->queue_sel));
430 /* Make it possible for guest to discover an error took place. */
431 if (msix_vector_use(&proxy->pci_dev, val) < 0)
432 val = VIRTIO_NO_VECTOR;
433 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
436 error_report("%s: unexpected address 0x%x value 0x%x",
437 __func__, addr, val);
442 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
444 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
445 uint32_t ret = 0xFFFFFFFF;
448 case VIRTIO_PCI_HOST_FEATURES:
449 ret = vdev->host_features;
451 case VIRTIO_PCI_GUEST_FEATURES:
452 ret = vdev->guest_features;
454 case VIRTIO_PCI_QUEUE_PFN:
455 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
456 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
458 case VIRTIO_PCI_QUEUE_NUM:
459 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
461 case VIRTIO_PCI_QUEUE_SEL:
462 ret = vdev->queue_sel;
464 case VIRTIO_PCI_STATUS:
468 /* reading from the ISR also clears it. */
471 pci_irq_deassert(&proxy->pci_dev);
473 case VIRTIO_MSI_CONFIG_VECTOR:
474 ret = vdev->config_vector;
476 case VIRTIO_MSI_QUEUE_VECTOR:
477 ret = virtio_queue_vector(vdev, vdev->queue_sel);
486 static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
489 VirtIOPCIProxy *proxy = opaque;
490 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
491 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
494 return virtio_ioport_read(proxy, addr);
500 val = virtio_config_readb(vdev, addr);
503 val = virtio_config_readw(vdev, addr);
504 if (virtio_is_big_endian(vdev)) {
509 val = virtio_config_readl(vdev, addr);
510 if (virtio_is_big_endian(vdev)) {
518 static void virtio_pci_config_write(void *opaque, hwaddr addr,
519 uint64_t val, unsigned size)
521 VirtIOPCIProxy *proxy = opaque;
522 uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
523 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
525 virtio_ioport_write(proxy, addr, val);
530 * Virtio-PCI is odd. Ioports are LE but config space is target native
535 virtio_config_writeb(vdev, addr, val);
538 if (virtio_is_big_endian(vdev)) {
541 virtio_config_writew(vdev, addr, val);
544 if (virtio_is_big_endian(vdev)) {
547 virtio_config_writel(vdev, addr, val);
552 static const MemoryRegionOps virtio_pci_config_ops = {
553 .read = virtio_pci_config_read,
554 .write = virtio_pci_config_write,
556 .min_access_size = 1,
557 .max_access_size = 4,
559 .endianness = DEVICE_LITTLE_ENDIAN,
562 /* Below are generic functions to do memcpy from/to an address space,
563 * without byteswaps, with input validation.
565 * As regular address_space_* APIs all do some kind of byteswap at least for
566 * some host/target combinations, we are forced to explicitly convert to a
567 * known-endianness integer value.
568 * It doesn't really matter which endian format to go through, so the code
569 * below selects the endian that causes the least amount of work on the given
572 * Note: host pointer must be aligned.
575 void virtio_address_space_write(AddressSpace *as, hwaddr addr,
576 const uint8_t *buf, int len)
580 /* address_space_* APIs assume an aligned address.
581 * As address is under guest control, handle illegal values.
585 /* Make sure caller aligned buf properly */
586 assert(!(((uintptr_t)buf) & (len - 1)));
590 val = pci_get_byte(buf);
591 address_space_stb(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
594 val = pci_get_word(buf);
595 address_space_stw_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
598 val = pci_get_long(buf);
599 address_space_stl_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
602 /* As length is under guest control, handle illegal values. */
608 virtio_address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len)
612 /* address_space_* APIs assume an aligned address.
613 * As address is under guest control, handle illegal values.
617 /* Make sure caller aligned buf properly */
618 assert(!(((uintptr_t)buf) & (len - 1)));
622 val = address_space_ldub(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
623 pci_set_byte(buf, val);
626 val = address_space_lduw_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
627 pci_set_word(buf, val);
630 val = address_space_ldl_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
631 pci_set_long(buf, val);
634 /* As length is under guest control, handle illegal values. */
639 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
640 uint32_t val, int len)
642 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
643 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
644 struct virtio_pci_cfg_cap *cfg;
646 pci_default_write_config(pci_dev, address, val, len);
648 if (range_covers_byte(address, len, PCI_COMMAND) &&
649 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
650 virtio_pci_stop_ioeventfd(proxy);
651 virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
654 if (proxy->config_cap &&
655 ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
657 sizeof cfg->pci_cfg_data)) {
661 cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
662 off = le32_to_cpu(cfg->cap.offset);
663 len = le32_to_cpu(cfg->cap.length);
665 if (len == 1 || len == 2 || len == 4) {
666 assert(len <= sizeof cfg->pci_cfg_data);
667 virtio_address_space_write(&proxy->modern_as, off,
668 cfg->pci_cfg_data, len);
673 static uint32_t virtio_read_config(PCIDevice *pci_dev,
674 uint32_t address, int len)
676 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
677 struct virtio_pci_cfg_cap *cfg;
679 if (proxy->config_cap &&
680 ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
682 sizeof cfg->pci_cfg_data)) {
686 cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
687 off = le32_to_cpu(cfg->cap.offset);
688 len = le32_to_cpu(cfg->cap.length);
690 if (len == 1 || len == 2 || len == 4) {
691 assert(len <= sizeof cfg->pci_cfg_data);
692 virtio_address_space_read(&proxy->modern_as, off,
693 cfg->pci_cfg_data, len);
697 return pci_default_read_config(pci_dev, address, len);
700 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
701 unsigned int queue_no,
704 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
707 if (irqfd->users == 0) {
708 ret = kvm_irqchip_add_msi_route(kvm_state, vector, &proxy->pci_dev);
718 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
721 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
722 if (--irqfd->users == 0) {
723 kvm_irqchip_release_virq(kvm_state, irqfd->virq);
727 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
728 unsigned int queue_no,
731 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
732 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
733 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
734 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
735 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, irqfd->virq);
738 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
739 unsigned int queue_no,
742 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
743 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
744 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
745 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
748 ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->virq);
752 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
754 PCIDevice *dev = &proxy->pci_dev;
755 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
756 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
760 for (queue_no = 0; queue_no < nvqs; queue_no++) {
761 if (!virtio_queue_get_num(vdev, queue_no)) {
764 vector = virtio_queue_vector(vdev, queue_no);
765 if (vector >= msix_nr_vectors_allocated(dev)) {
768 ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector);
772 /* If guest supports masking, set up irqfd now.
773 * Otherwise, delay until unmasked in the frontend.
775 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
776 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
778 kvm_virtio_pci_vq_vector_release(proxy, vector);
786 while (--queue_no >= 0) {
787 vector = virtio_queue_vector(vdev, queue_no);
788 if (vector >= msix_nr_vectors_allocated(dev)) {
791 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
792 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
794 kvm_virtio_pci_vq_vector_release(proxy, vector);
799 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
801 PCIDevice *dev = &proxy->pci_dev;
802 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
805 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
807 for (queue_no = 0; queue_no < nvqs; queue_no++) {
808 if (!virtio_queue_get_num(vdev, queue_no)) {
811 vector = virtio_queue_vector(vdev, queue_no);
812 if (vector >= msix_nr_vectors_allocated(dev)) {
815 /* If guest supports masking, clean up irqfd now.
816 * Otherwise, it was cleaned when masked in the frontend.
818 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
819 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
821 kvm_virtio_pci_vq_vector_release(proxy, vector);
825 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
826 unsigned int queue_no,
830 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
831 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
832 VirtQueue *vq = virtio_get_queue(vdev, queue_no);
833 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
837 if (proxy->vector_irqfd) {
838 irqfd = &proxy->vector_irqfd[vector];
839 if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) {
840 ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg,
848 /* If guest supports masking, irqfd is already setup, unmask it.
849 * Otherwise, set it up now.
851 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
852 k->guest_notifier_mask(vdev, queue_no, false);
853 /* Test after unmasking to avoid losing events. */
854 if (k->guest_notifier_pending &&
855 k->guest_notifier_pending(vdev, queue_no)) {
856 event_notifier_set(n);
859 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
864 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
865 unsigned int queue_no,
868 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
869 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
871 /* If guest supports masking, keep irqfd but mask it.
872 * Otherwise, clean it up now.
874 if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
875 k->guest_notifier_mask(vdev, queue_no, true);
877 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
881 static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
884 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
885 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
886 VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
887 int ret, index, unmasked = 0;
890 index = virtio_get_queue_index(vq);
891 if (!virtio_queue_get_num(vdev, index)) {
894 if (index < proxy->nvqs_with_notifiers) {
895 ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg);
901 vq = virtio_vector_next_queue(vq);
907 vq = virtio_vector_first_queue(vdev, vector);
908 while (vq && unmasked >= 0) {
909 index = virtio_get_queue_index(vq);
910 if (index < proxy->nvqs_with_notifiers) {
911 virtio_pci_vq_vector_mask(proxy, index, vector);
914 vq = virtio_vector_next_queue(vq);
919 static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
921 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
922 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
923 VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
927 index = virtio_get_queue_index(vq);
928 if (!virtio_queue_get_num(vdev, index)) {
931 if (index < proxy->nvqs_with_notifiers) {
932 virtio_pci_vq_vector_mask(proxy, index, vector);
934 vq = virtio_vector_next_queue(vq);
938 static void virtio_pci_vector_poll(PCIDevice *dev,
939 unsigned int vector_start,
940 unsigned int vector_end)
942 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
943 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
944 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
947 EventNotifier *notifier;
950 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
951 if (!virtio_queue_get_num(vdev, queue_no)) {
954 vector = virtio_queue_vector(vdev, queue_no);
955 if (vector < vector_start || vector >= vector_end ||
956 !msix_is_masked(dev, vector)) {
959 vq = virtio_get_queue(vdev, queue_no);
960 notifier = virtio_queue_get_guest_notifier(vq);
961 if (k->guest_notifier_pending) {
962 if (k->guest_notifier_pending(vdev, queue_no)) {
963 msix_set_pending(dev, vector);
965 } else if (event_notifier_test_and_clear(notifier)) {
966 msix_set_pending(dev, vector);
971 static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
974 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
975 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
976 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
977 VirtQueue *vq = virtio_get_queue(vdev, n);
978 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
981 int r = event_notifier_init(notifier, 0);
985 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
987 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
988 event_notifier_cleanup(notifier);
991 if (!msix_enabled(&proxy->pci_dev) &&
992 vdev->use_guest_notifier_mask &&
993 vdc->guest_notifier_mask) {
994 vdc->guest_notifier_mask(vdev, n, !assign);
1000 static bool virtio_pci_query_guest_notifiers(DeviceState *d)
1002 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
1003 return msix_enabled(&proxy->pci_dev);
1006 static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
1008 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
1009 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1010 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1012 bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
1013 kvm_msi_via_irqfd_enabled();
1015 nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX);
1017 /* When deassigning, pass a consistent nvqs value
1018 * to avoid leaking notifiers.
1020 assert(assign || nvqs == proxy->nvqs_with_notifiers);
1022 proxy->nvqs_with_notifiers = nvqs;
1024 /* Must unset vector notifier while guest notifier is still assigned */
1025 if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) {
1026 msix_unset_vector_notifiers(&proxy->pci_dev);
1027 if (proxy->vector_irqfd) {
1028 kvm_virtio_pci_vector_release(proxy, nvqs);
1029 g_free(proxy->vector_irqfd);
1030 proxy->vector_irqfd = NULL;
1034 for (n = 0; n < nvqs; n++) {
1035 if (!virtio_queue_get_num(vdev, n)) {
1039 r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd);
1045 /* Must set vector notifier after guest notifier has been assigned */
1046 if ((with_irqfd || k->guest_notifier_mask) && assign) {
1048 proxy->vector_irqfd =
1049 g_malloc0(sizeof(*proxy->vector_irqfd) *
1050 msix_nr_vectors_allocated(&proxy->pci_dev));
1051 r = kvm_virtio_pci_vector_use(proxy, nvqs);
1056 r = msix_set_vector_notifiers(&proxy->pci_dev,
1057 virtio_pci_vector_unmask,
1058 virtio_pci_vector_mask,
1059 virtio_pci_vector_poll);
1061 goto notifiers_error;
1070 kvm_virtio_pci_vector_release(proxy, nvqs);
1074 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
1077 virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
1082 static void virtio_pci_vmstate_change(DeviceState *d, bool running)
1084 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
1085 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1088 /* Old QEMU versions did not set bus master enable on status write.
1089 * Detect DRIVER set and enable it.
1091 if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) &&
1092 (vdev->status & VIRTIO_CONFIG_S_DRIVER) &&
1093 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
1094 pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
1095 proxy->pci_dev.config[PCI_COMMAND] |
1096 PCI_COMMAND_MASTER, 1);
1098 virtio_pci_start_ioeventfd(proxy);
1100 virtio_pci_stop_ioeventfd(proxy);
1104 #ifdef CONFIG_VIRTFS
1105 static void virtio_9p_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1107 V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev);
1108 DeviceState *vdev = DEVICE(&dev->vdev);
1110 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1111 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1114 static Property virtio_9p_pci_properties[] = {
1115 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1116 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1117 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1118 DEFINE_PROP_END_OF_LIST(),
1121 static void virtio_9p_pci_class_init(ObjectClass *klass, void *data)
1123 DeviceClass *dc = DEVICE_CLASS(klass);
1124 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1125 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1127 k->realize = virtio_9p_pci_realize;
1128 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1129 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P;
1130 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1131 pcidev_k->class_id = 0x2;
1132 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1133 dc->props = virtio_9p_pci_properties;
1136 static void virtio_9p_pci_instance_init(Object *obj)
1138 V9fsPCIState *dev = VIRTIO_9P_PCI(obj);
1140 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1144 static const TypeInfo virtio_9p_pci_info = {
1145 .name = TYPE_VIRTIO_9P_PCI,
1146 .parent = TYPE_VIRTIO_PCI,
1147 .instance_size = sizeof(V9fsPCIState),
1148 .instance_init = virtio_9p_pci_instance_init,
1149 .class_init = virtio_9p_pci_class_init,
1151 #endif /* CONFIG_VIRTFS */
1154 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
1157 static int virtio_pci_query_nvectors(DeviceState *d)
1159 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1161 return proxy->nvectors;
1164 static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
1165 struct virtio_pci_cap *cap)
1167 PCIDevice *dev = &proxy->pci_dev;
1170 offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 0, cap->cap_len);
1173 assert(cap->cap_len >= sizeof *cap);
1174 memcpy(dev->config + offset + PCI_CAP_FLAGS, &cap->cap_len,
1175 cap->cap_len - PCI_CAP_FLAGS);
1180 static uint64_t virtio_pci_common_read(void *opaque, hwaddr addr,
1183 VirtIOPCIProxy *proxy = opaque;
1184 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1189 case VIRTIO_PCI_COMMON_DFSELECT:
1190 val = proxy->dfselect;
1192 case VIRTIO_PCI_COMMON_DF:
1193 if (proxy->dfselect <= 1) {
1194 val = (vdev->host_features & ~VIRTIO_LEGACY_FEATURES) >>
1195 (32 * proxy->dfselect);
1198 case VIRTIO_PCI_COMMON_GFSELECT:
1199 val = proxy->gfselect;
1201 case VIRTIO_PCI_COMMON_GF:
1202 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1203 val = proxy->guest_features[proxy->gfselect];
1206 case VIRTIO_PCI_COMMON_MSIX:
1207 val = vdev->config_vector;
1209 case VIRTIO_PCI_COMMON_NUMQ:
1210 for (i = 0; i < VIRTIO_QUEUE_MAX; ++i) {
1211 if (virtio_queue_get_num(vdev, i)) {
1216 case VIRTIO_PCI_COMMON_STATUS:
1219 case VIRTIO_PCI_COMMON_CFGGENERATION:
1220 val = vdev->generation;
1222 case VIRTIO_PCI_COMMON_Q_SELECT:
1223 val = vdev->queue_sel;
1225 case VIRTIO_PCI_COMMON_Q_SIZE:
1226 val = virtio_queue_get_num(vdev, vdev->queue_sel);
1228 case VIRTIO_PCI_COMMON_Q_MSIX:
1229 val = virtio_queue_vector(vdev, vdev->queue_sel);
1231 case VIRTIO_PCI_COMMON_Q_ENABLE:
1232 val = proxy->vqs[vdev->queue_sel].enabled;
1234 case VIRTIO_PCI_COMMON_Q_NOFF:
1235 /* Simply map queues in order */
1236 val = vdev->queue_sel;
1238 case VIRTIO_PCI_COMMON_Q_DESCLO:
1239 val = proxy->vqs[vdev->queue_sel].desc[0];
1241 case VIRTIO_PCI_COMMON_Q_DESCHI:
1242 val = proxy->vqs[vdev->queue_sel].desc[1];
1244 case VIRTIO_PCI_COMMON_Q_AVAILLO:
1245 val = proxy->vqs[vdev->queue_sel].avail[0];
1247 case VIRTIO_PCI_COMMON_Q_AVAILHI:
1248 val = proxy->vqs[vdev->queue_sel].avail[1];
1250 case VIRTIO_PCI_COMMON_Q_USEDLO:
1251 val = proxy->vqs[vdev->queue_sel].used[0];
1253 case VIRTIO_PCI_COMMON_Q_USEDHI:
1254 val = proxy->vqs[vdev->queue_sel].used[1];
1263 static void virtio_pci_common_write(void *opaque, hwaddr addr,
1264 uint64_t val, unsigned size)
1266 VirtIOPCIProxy *proxy = opaque;
1267 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1270 case VIRTIO_PCI_COMMON_DFSELECT:
1271 proxy->dfselect = val;
1273 case VIRTIO_PCI_COMMON_GFSELECT:
1274 proxy->gfselect = val;
1276 case VIRTIO_PCI_COMMON_GF:
1277 if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1278 proxy->guest_features[proxy->gfselect] = val;
1279 virtio_set_features(vdev,
1280 (((uint64_t)proxy->guest_features[1]) << 32) |
1281 proxy->guest_features[0]);
1284 case VIRTIO_PCI_COMMON_MSIX:
1285 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
1286 /* Make it possible for guest to discover an error took place. */
1287 if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1288 val = VIRTIO_NO_VECTOR;
1290 vdev->config_vector = val;
1292 case VIRTIO_PCI_COMMON_STATUS:
1293 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
1294 virtio_pci_stop_ioeventfd(proxy);
1297 virtio_set_status(vdev, val & 0xFF);
1299 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
1300 virtio_pci_start_ioeventfd(proxy);
1303 if (vdev->status == 0) {
1304 virtio_pci_reset(DEVICE(proxy));
1308 case VIRTIO_PCI_COMMON_Q_SELECT:
1309 if (val < VIRTIO_QUEUE_MAX) {
1310 vdev->queue_sel = val;
1313 case VIRTIO_PCI_COMMON_Q_SIZE:
1314 proxy->vqs[vdev->queue_sel].num = val;
1316 case VIRTIO_PCI_COMMON_Q_MSIX:
1317 msix_vector_unuse(&proxy->pci_dev,
1318 virtio_queue_vector(vdev, vdev->queue_sel));
1319 /* Make it possible for guest to discover an error took place. */
1320 if (msix_vector_use(&proxy->pci_dev, val) < 0) {
1321 val = VIRTIO_NO_VECTOR;
1323 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
1325 case VIRTIO_PCI_COMMON_Q_ENABLE:
1326 /* TODO: need a way to put num back on reset. */
1327 virtio_queue_set_num(vdev, vdev->queue_sel,
1328 proxy->vqs[vdev->queue_sel].num);
1329 virtio_queue_set_rings(vdev, vdev->queue_sel,
1330 ((uint64_t)proxy->vqs[vdev->queue_sel].desc[1]) << 32 |
1331 proxy->vqs[vdev->queue_sel].desc[0],
1332 ((uint64_t)proxy->vqs[vdev->queue_sel].avail[1]) << 32 |
1333 proxy->vqs[vdev->queue_sel].avail[0],
1334 ((uint64_t)proxy->vqs[vdev->queue_sel].used[1]) << 32 |
1335 proxy->vqs[vdev->queue_sel].used[0]);
1336 proxy->vqs[vdev->queue_sel].enabled = 1;
1338 case VIRTIO_PCI_COMMON_Q_DESCLO:
1339 proxy->vqs[vdev->queue_sel].desc[0] = val;
1341 case VIRTIO_PCI_COMMON_Q_DESCHI:
1342 proxy->vqs[vdev->queue_sel].desc[1] = val;
1344 case VIRTIO_PCI_COMMON_Q_AVAILLO:
1345 proxy->vqs[vdev->queue_sel].avail[0] = val;
1347 case VIRTIO_PCI_COMMON_Q_AVAILHI:
1348 proxy->vqs[vdev->queue_sel].avail[1] = val;
1350 case VIRTIO_PCI_COMMON_Q_USEDLO:
1351 proxy->vqs[vdev->queue_sel].used[0] = val;
1353 case VIRTIO_PCI_COMMON_Q_USEDHI:
1354 proxy->vqs[vdev->queue_sel].used[1] = val;
1362 static uint64_t virtio_pci_notify_read(void *opaque, hwaddr addr,
1368 static void virtio_pci_notify_write(void *opaque, hwaddr addr,
1369 uint64_t val, unsigned size)
1371 VirtIODevice *vdev = opaque;
1372 unsigned queue = addr / QEMU_VIRTIO_PCI_QUEUE_MEM_MULT;
1374 if (queue < VIRTIO_QUEUE_MAX) {
1375 virtio_queue_notify(vdev, queue);
1379 static void virtio_pci_notify_write_pio(void *opaque, hwaddr addr,
1380 uint64_t val, unsigned size)
1382 VirtIODevice *vdev = opaque;
1383 unsigned queue = val;
1385 if (queue < VIRTIO_QUEUE_MAX) {
1386 virtio_queue_notify(vdev, queue);
1390 static uint64_t virtio_pci_isr_read(void *opaque, hwaddr addr,
1393 VirtIOPCIProxy *proxy = opaque;
1394 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1395 uint64_t val = vdev->isr;
1398 pci_irq_deassert(&proxy->pci_dev);
1403 static void virtio_pci_isr_write(void *opaque, hwaddr addr,
1404 uint64_t val, unsigned size)
1408 static uint64_t virtio_pci_device_read(void *opaque, hwaddr addr,
1411 VirtIODevice *vdev = opaque;
1416 val = virtio_config_modern_readb(vdev, addr);
1419 val = virtio_config_modern_readw(vdev, addr);
1422 val = virtio_config_modern_readl(vdev, addr);
1428 static void virtio_pci_device_write(void *opaque, hwaddr addr,
1429 uint64_t val, unsigned size)
1431 VirtIODevice *vdev = opaque;
1434 virtio_config_modern_writeb(vdev, addr, val);
1437 virtio_config_modern_writew(vdev, addr, val);
1440 virtio_config_modern_writel(vdev, addr, val);
1445 static void virtio_pci_modern_regions_init(VirtIOPCIProxy *proxy)
1447 static const MemoryRegionOps common_ops = {
1448 .read = virtio_pci_common_read,
1449 .write = virtio_pci_common_write,
1451 .min_access_size = 1,
1452 .max_access_size = 4,
1454 .endianness = DEVICE_LITTLE_ENDIAN,
1456 static const MemoryRegionOps isr_ops = {
1457 .read = virtio_pci_isr_read,
1458 .write = virtio_pci_isr_write,
1460 .min_access_size = 1,
1461 .max_access_size = 4,
1463 .endianness = DEVICE_LITTLE_ENDIAN,
1465 static const MemoryRegionOps device_ops = {
1466 .read = virtio_pci_device_read,
1467 .write = virtio_pci_device_write,
1469 .min_access_size = 1,
1470 .max_access_size = 4,
1472 .endianness = DEVICE_LITTLE_ENDIAN,
1474 static const MemoryRegionOps notify_ops = {
1475 .read = virtio_pci_notify_read,
1476 .write = virtio_pci_notify_write,
1478 .min_access_size = 1,
1479 .max_access_size = 4,
1481 .endianness = DEVICE_LITTLE_ENDIAN,
1483 static const MemoryRegionOps notify_pio_ops = {
1484 .read = virtio_pci_notify_read,
1485 .write = virtio_pci_notify_write_pio,
1487 .min_access_size = 1,
1488 .max_access_size = 4,
1490 .endianness = DEVICE_LITTLE_ENDIAN,
1494 memory_region_init_io(&proxy->common.mr, OBJECT(proxy),
1497 "virtio-pci-common",
1498 proxy->common.size);
1500 memory_region_init_io(&proxy->isr.mr, OBJECT(proxy),
1506 memory_region_init_io(&proxy->device.mr, OBJECT(proxy),
1508 virtio_bus_get_device(&proxy->bus),
1509 "virtio-pci-device",
1510 proxy->device.size);
1512 memory_region_init_io(&proxy->notify.mr, OBJECT(proxy),
1514 virtio_bus_get_device(&proxy->bus),
1515 "virtio-pci-notify",
1516 proxy->notify.size);
1518 memory_region_init_io(&proxy->notify_pio.mr, OBJECT(proxy),
1520 virtio_bus_get_device(&proxy->bus),
1521 "virtio-pci-notify-pio",
1522 proxy->notify.size);
1525 static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy,
1526 VirtIOPCIRegion *region,
1527 struct virtio_pci_cap *cap,
1531 memory_region_add_subregion(mr, region->offset, ®ion->mr);
1533 cap->cfg_type = region->type;
1535 cap->offset = cpu_to_le32(region->offset);
1536 cap->length = cpu_to_le32(region->size);
1537 virtio_pci_add_mem_cap(proxy, cap);
1541 static void virtio_pci_modern_mem_region_map(VirtIOPCIProxy *proxy,
1542 VirtIOPCIRegion *region,
1543 struct virtio_pci_cap *cap)
1545 virtio_pci_modern_region_map(proxy, region, cap,
1546 &proxy->modern_bar, proxy->modern_mem_bar);
1549 static void virtio_pci_modern_io_region_map(VirtIOPCIProxy *proxy,
1550 VirtIOPCIRegion *region,
1551 struct virtio_pci_cap *cap)
1553 virtio_pci_modern_region_map(proxy, region, cap,
1554 &proxy->io_bar, proxy->modern_io_bar);
1557 static void virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy *proxy,
1558 VirtIOPCIRegion *region)
1560 memory_region_del_subregion(&proxy->modern_bar,
1564 static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy *proxy,
1565 VirtIOPCIRegion *region)
1567 memory_region_del_subregion(&proxy->io_bar,
1571 /* This is called by virtio-bus just after the device is plugged. */
1572 static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
1574 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1575 VirtioBusState *bus = &proxy->bus;
1576 bool legacy = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_LEGACY);
1577 bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
1578 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1581 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1583 config = proxy->pci_dev.config;
1584 if (proxy->class_code) {
1585 pci_config_set_class(config, proxy->class_code);
1589 /* legacy and transitional */
1590 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
1591 pci_get_word(config + PCI_VENDOR_ID));
1592 pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
1594 /* pure virtio-1.0 */
1595 pci_set_word(config + PCI_VENDOR_ID,
1596 PCI_VENDOR_ID_REDHAT_QUMRANET);
1597 pci_set_word(config + PCI_DEVICE_ID,
1598 0x1040 + virtio_bus_get_vdev_id(bus));
1599 pci_config_set_revision(config, 1);
1601 config[PCI_INTERRUPT_PIN] = 1;
1605 struct virtio_pci_cap cap = {
1606 .cap_len = sizeof cap,
1608 struct virtio_pci_notify_cap notify = {
1609 .cap.cap_len = sizeof notify,
1610 .notify_off_multiplier =
1611 cpu_to_le32(QEMU_VIRTIO_PCI_QUEUE_MEM_MULT),
1613 struct virtio_pci_cfg_cap cfg = {
1614 .cap.cap_len = sizeof cfg,
1615 .cap.cfg_type = VIRTIO_PCI_CAP_PCI_CFG,
1617 struct virtio_pci_notify_cap notify_pio = {
1618 .cap.cap_len = sizeof notify,
1619 .notify_off_multiplier = cpu_to_le32(0x0),
1622 struct virtio_pci_cfg_cap *cfg_mask;
1624 virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
1625 virtio_pci_modern_regions_init(proxy);
1627 virtio_pci_modern_mem_region_map(proxy, &proxy->common, &cap);
1628 virtio_pci_modern_mem_region_map(proxy, &proxy->isr, &cap);
1629 virtio_pci_modern_mem_region_map(proxy, &proxy->device, &cap);
1630 virtio_pci_modern_mem_region_map(proxy, &proxy->notify, ¬ify.cap);
1633 memory_region_init(&proxy->io_bar, OBJECT(proxy),
1634 "virtio-pci-io", 0x4);
1636 pci_register_bar(&proxy->pci_dev, proxy->modern_io_bar,
1637 PCI_BASE_ADDRESS_SPACE_IO, &proxy->io_bar);
1639 virtio_pci_modern_io_region_map(proxy, &proxy->notify_pio,
1643 pci_register_bar(&proxy->pci_dev, proxy->modern_mem_bar,
1644 PCI_BASE_ADDRESS_SPACE_MEMORY |
1645 PCI_BASE_ADDRESS_MEM_PREFETCH |
1646 PCI_BASE_ADDRESS_MEM_TYPE_64,
1647 &proxy->modern_bar);
1649 proxy->config_cap = virtio_pci_add_mem_cap(proxy, &cfg.cap);
1650 cfg_mask = (void *)(proxy->pci_dev.wmask + proxy->config_cap);
1651 pci_set_byte(&cfg_mask->cap.bar, ~0x0);
1652 pci_set_long((uint8_t *)&cfg_mask->cap.offset, ~0x0);
1653 pci_set_long((uint8_t *)&cfg_mask->cap.length, ~0x0);
1654 pci_set_long(cfg_mask->pci_cfg_data, ~0x0);
1657 if (proxy->nvectors) {
1658 int err = msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors,
1661 /* Notice when a system that supports MSIx can't initialize it. */
1662 if (err != -ENOTSUP) {
1663 error_report("unable to init msix vectors to %" PRIu32,
1666 proxy->nvectors = 0;
1670 proxy->pci_dev.config_write = virtio_write_config;
1671 proxy->pci_dev.config_read = virtio_read_config;
1674 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
1675 + virtio_bus_get_vdev_config_len(bus);
1676 size = pow2ceil(size);
1678 memory_region_init_io(&proxy->bar, OBJECT(proxy),
1679 &virtio_pci_config_ops,
1680 proxy, "virtio-pci", size);
1682 pci_register_bar(&proxy->pci_dev, proxy->legacy_io_bar,
1683 PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar);
1686 if (!kvm_has_many_ioeventfds()) {
1687 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
1690 virtio_add_feature(&vdev->host_features, VIRTIO_F_BAD_FEATURE);
1693 static void virtio_pci_device_unplugged(DeviceState *d)
1695 VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1696 bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
1697 bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1699 virtio_pci_stop_ioeventfd(proxy);
1702 virtio_pci_modern_mem_region_unmap(proxy, &proxy->common);
1703 virtio_pci_modern_mem_region_unmap(proxy, &proxy->isr);
1704 virtio_pci_modern_mem_region_unmap(proxy, &proxy->device);
1705 virtio_pci_modern_mem_region_unmap(proxy, &proxy->notify);
1707 virtio_pci_modern_io_region_unmap(proxy, &proxy->notify_pio);
1712 static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
1714 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1715 VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
1718 * virtio pci bar layout used by default.
1719 * subclasses can re-arrange things if needed.
1721 * region 0 -- virtio legacy io bar
1722 * region 1 -- msi-x bar
1723 * region 4+5 -- virtio modern memory (64bit) bar
1726 proxy->legacy_io_bar = 0;
1727 proxy->msix_bar = 1;
1728 proxy->modern_io_bar = 2;
1729 proxy->modern_mem_bar = 4;
1731 proxy->common.offset = 0x0;
1732 proxy->common.size = 0x1000;
1733 proxy->common.type = VIRTIO_PCI_CAP_COMMON_CFG;
1735 proxy->isr.offset = 0x1000;
1736 proxy->isr.size = 0x1000;
1737 proxy->isr.type = VIRTIO_PCI_CAP_ISR_CFG;
1739 proxy->device.offset = 0x2000;
1740 proxy->device.size = 0x1000;
1741 proxy->device.type = VIRTIO_PCI_CAP_DEVICE_CFG;
1743 proxy->notify.offset = 0x3000;
1744 proxy->notify.size =
1745 QEMU_VIRTIO_PCI_QUEUE_MEM_MULT * VIRTIO_QUEUE_MAX;
1746 proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
1748 proxy->notify_pio.offset = 0x0;
1749 proxy->notify_pio.size = 0x4;
1750 proxy->notify_pio.type = VIRTIO_PCI_CAP_NOTIFY_CFG;
1752 /* subclasses can enforce modern, so do this unconditionally */
1753 memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci",
1754 2 * QEMU_VIRTIO_PCI_QUEUE_MEM_MULT *
1757 memory_region_init_alias(&proxy->modern_cfg,
1762 memory_region_size(&proxy->modern_bar));
1764 address_space_init(&proxy->modern_as, &proxy->modern_cfg, "virtio-pci-cfg-as");
1766 if (pci_is_express(pci_dev) && pci_bus_is_express(pci_dev->bus) &&
1767 !pci_bus_is_root(pci_dev->bus)) {
1770 pos = pcie_endpoint_cap_init(pci_dev, 0);
1773 pos = pci_add_capability(pci_dev, PCI_CAP_ID_PM, 0, PCI_PM_SIZEOF);
1777 * Indicates that this function complies with revision 1.2 of the
1778 * PCI Power Management Interface Specification.
1780 pci_set_word(pci_dev->config + pos + PCI_PM_PMC, 0x3);
1783 * make future invocations of pci_is_express() return false
1784 * and pci_config_size() return PCI_CONFIG_SPACE_SIZE.
1786 pci_dev->cap_present &= ~QEMU_PCI_CAP_EXPRESS;
1789 virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
1791 k->realize(proxy, errp);
1795 static void virtio_pci_exit(PCIDevice *pci_dev)
1797 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1799 msix_uninit_exclusive_bar(pci_dev);
1800 address_space_destroy(&proxy->modern_as);
1803 static void virtio_pci_reset(DeviceState *qdev)
1805 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1806 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
1809 virtio_pci_stop_ioeventfd(proxy);
1810 virtio_bus_reset(bus);
1811 msix_unuse_all_vectors(&proxy->pci_dev);
1813 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1814 proxy->vqs[i].enabled = 0;
1818 static Property virtio_pci_properties[] = {
1819 DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
1820 VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
1821 DEFINE_PROP_BIT("disable-legacy", VirtIOPCIProxy, flags,
1822 VIRTIO_PCI_FLAG_DISABLE_LEGACY_BIT, false),
1823 DEFINE_PROP_BIT("disable-modern", VirtIOPCIProxy, flags,
1824 VIRTIO_PCI_FLAG_DISABLE_MODERN_BIT, true),
1825 DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy, flags,
1826 VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT, true),
1827 DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy, flags,
1828 VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT, false),
1829 DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy, flags,
1830 VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT, false),
1831 DEFINE_PROP_END_OF_LIST(),
1834 static void virtio_pci_dc_realize(DeviceState *qdev, Error **errp)
1836 VirtioPCIClass *vpciklass = VIRTIO_PCI_GET_CLASS(qdev);
1837 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1838 PCIDevice *pci_dev = &proxy->pci_dev;
1840 if (!(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_PCIE) &&
1841 !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN)) {
1842 pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
1845 vpciklass->parent_dc_realize(qdev, errp);
1848 static void virtio_pci_class_init(ObjectClass *klass, void *data)
1850 DeviceClass *dc = DEVICE_CLASS(klass);
1851 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1852 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
1854 dc->props = virtio_pci_properties;
1855 k->realize = virtio_pci_realize;
1856 k->exit = virtio_pci_exit;
1857 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1858 k->revision = VIRTIO_PCI_ABI_VERSION;
1859 k->class_id = PCI_CLASS_OTHERS;
1860 vpciklass->parent_dc_realize = dc->realize;
1861 dc->realize = virtio_pci_dc_realize;
1862 dc->reset = virtio_pci_reset;
1865 static const TypeInfo virtio_pci_info = {
1866 .name = TYPE_VIRTIO_PCI,
1867 .parent = TYPE_PCI_DEVICE,
1868 .instance_size = sizeof(VirtIOPCIProxy),
1869 .class_init = virtio_pci_class_init,
1870 .class_size = sizeof(VirtioPCIClass),
1874 /* virtio-blk-pci */
1876 static Property virtio_blk_pci_properties[] = {
1877 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1878 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1879 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1880 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1881 DEFINE_PROP_END_OF_LIST(),
1884 static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1886 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
1887 DeviceState *vdev = DEVICE(&dev->vdev);
1889 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1890 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1893 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data)
1895 DeviceClass *dc = DEVICE_CLASS(klass);
1896 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1897 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1899 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1900 dc->props = virtio_blk_pci_properties;
1901 k->realize = virtio_blk_pci_realize;
1902 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1903 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
1904 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1905 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1908 static void virtio_blk_pci_instance_init(Object *obj)
1910 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj);
1912 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1914 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread",
1916 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
1917 "bootindex", &error_abort);
1920 static const TypeInfo virtio_blk_pci_info = {
1921 .name = TYPE_VIRTIO_BLK_PCI,
1922 .parent = TYPE_VIRTIO_PCI,
1923 .instance_size = sizeof(VirtIOBlkPCI),
1924 .instance_init = virtio_blk_pci_instance_init,
1925 .class_init = virtio_blk_pci_class_init,
1928 /* virtio-scsi-pci */
1930 static Property virtio_scsi_pci_properties[] = {
1931 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1932 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1933 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1934 DEV_NVECTORS_UNSPECIFIED),
1935 DEFINE_PROP_END_OF_LIST(),
1938 static void virtio_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1940 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
1941 DeviceState *vdev = DEVICE(&dev->vdev);
1942 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1943 DeviceState *proxy = DEVICE(vpci_dev);
1946 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1947 vpci_dev->nvectors = vs->conf.num_queues + 3;
1951 * For command line compatibility, this sets the virtio-scsi-device bus
1955 bus_name = g_strdup_printf("%s.0", proxy->id);
1956 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1960 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1961 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1964 static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data)
1966 DeviceClass *dc = DEVICE_CLASS(klass);
1967 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1968 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1970 k->realize = virtio_scsi_pci_realize;
1971 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1972 dc->props = virtio_scsi_pci_properties;
1973 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1974 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1975 pcidev_k->revision = 0x00;
1976 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1979 static void virtio_scsi_pci_instance_init(Object *obj)
1981 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj);
1983 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1985 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread",
1989 static const TypeInfo virtio_scsi_pci_info = {
1990 .name = TYPE_VIRTIO_SCSI_PCI,
1991 .parent = TYPE_VIRTIO_PCI,
1992 .instance_size = sizeof(VirtIOSCSIPCI),
1993 .instance_init = virtio_scsi_pci_instance_init,
1994 .class_init = virtio_scsi_pci_class_init,
1997 /* vhost-scsi-pci */
1999 #ifdef CONFIG_VHOST_SCSI
2000 static Property vhost_scsi_pci_properties[] = {
2001 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
2002 DEV_NVECTORS_UNSPECIFIED),
2003 DEFINE_PROP_END_OF_LIST(),
2006 static void vhost_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2008 VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev);
2009 DeviceState *vdev = DEVICE(&dev->vdev);
2010 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
2012 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
2013 vpci_dev->nvectors = vs->conf.num_queues + 3;
2016 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2017 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2020 static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data)
2022 DeviceClass *dc = DEVICE_CLASS(klass);
2023 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2024 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2025 k->realize = vhost_scsi_pci_realize;
2026 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2027 dc->props = vhost_scsi_pci_properties;
2028 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2029 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
2030 pcidev_k->revision = 0x00;
2031 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
2034 static void vhost_scsi_pci_instance_init(Object *obj)
2036 VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj);
2038 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2040 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
2041 "bootindex", &error_abort);
2044 static const TypeInfo vhost_scsi_pci_info = {
2045 .name = TYPE_VHOST_SCSI_PCI,
2046 .parent = TYPE_VIRTIO_PCI,
2047 .instance_size = sizeof(VHostSCSIPCI),
2048 .instance_init = vhost_scsi_pci_instance_init,
2049 .class_init = vhost_scsi_pci_class_init,
2053 /* virtio-balloon-pci */
2055 static Property virtio_balloon_pci_properties[] = {
2056 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2057 DEFINE_PROP_END_OF_LIST(),
2060 static void virtio_balloon_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2062 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev);
2063 DeviceState *vdev = DEVICE(&dev->vdev);
2065 if (vpci_dev->class_code != PCI_CLASS_OTHERS &&
2066 vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
2067 vpci_dev->class_code = PCI_CLASS_OTHERS;
2070 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2071 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2074 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data)
2076 DeviceClass *dc = DEVICE_CLASS(klass);
2077 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2078 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2079 k->realize = virtio_balloon_pci_realize;
2080 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2081 dc->props = virtio_balloon_pci_properties;
2082 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2083 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
2084 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2085 pcidev_k->class_id = PCI_CLASS_OTHERS;
2088 static void virtio_balloon_pci_instance_init(Object *obj)
2090 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj);
2092 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2093 TYPE_VIRTIO_BALLOON);
2094 object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev),
2095 "guest-stats", &error_abort);
2096 object_property_add_alias(obj, "guest-stats-polling-interval",
2098 "guest-stats-polling-interval", &error_abort);
2101 static const TypeInfo virtio_balloon_pci_info = {
2102 .name = TYPE_VIRTIO_BALLOON_PCI,
2103 .parent = TYPE_VIRTIO_PCI,
2104 .instance_size = sizeof(VirtIOBalloonPCI),
2105 .instance_init = virtio_balloon_pci_instance_init,
2106 .class_init = virtio_balloon_pci_class_init,
2109 /* virtio-serial-pci */
2111 static void virtio_serial_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2113 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
2114 DeviceState *vdev = DEVICE(&dev->vdev);
2115 DeviceState *proxy = DEVICE(vpci_dev);
2118 if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
2119 vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
2120 vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */
2121 vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER;
2124 /* backwards-compatibility with machines that were created with
2125 DEV_NVECTORS_UNSPECIFIED */
2126 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
2127 vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1;
2131 * For command line compatibility, this sets the virtio-serial-device bus
2135 bus_name = g_strdup_printf("%s.0", proxy->id);
2136 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
2140 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2141 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2144 static Property virtio_serial_pci_properties[] = {
2145 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
2146 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
2147 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
2148 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2149 DEFINE_PROP_END_OF_LIST(),
2152 static void virtio_serial_pci_class_init(ObjectClass *klass, void *data)
2154 DeviceClass *dc = DEVICE_CLASS(klass);
2155 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2156 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2157 k->realize = virtio_serial_pci_realize;
2158 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2159 dc->props = virtio_serial_pci_properties;
2160 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2161 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
2162 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2163 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
2166 static void virtio_serial_pci_instance_init(Object *obj)
2168 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj);
2170 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2171 TYPE_VIRTIO_SERIAL);
2174 static const TypeInfo virtio_serial_pci_info = {
2175 .name = TYPE_VIRTIO_SERIAL_PCI,
2176 .parent = TYPE_VIRTIO_PCI,
2177 .instance_size = sizeof(VirtIOSerialPCI),
2178 .instance_init = virtio_serial_pci_instance_init,
2179 .class_init = virtio_serial_pci_class_init,
2182 /* virtio-net-pci */
2184 static Property virtio_net_properties[] = {
2185 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
2186 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
2187 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
2188 DEFINE_PROP_END_OF_LIST(),
2191 static void virtio_net_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2193 DeviceState *qdev = DEVICE(vpci_dev);
2194 VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
2195 DeviceState *vdev = DEVICE(&dev->vdev);
2197 virtio_net_set_netclient_name(&dev->vdev, qdev->id,
2198 object_get_typename(OBJECT(qdev)));
2199 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2200 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2203 static void virtio_net_pci_class_init(ObjectClass *klass, void *data)
2205 DeviceClass *dc = DEVICE_CLASS(klass);
2206 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
2207 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
2209 k->romfile = "efi-virtio.rom";
2210 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2211 k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
2212 k->revision = VIRTIO_PCI_ABI_VERSION;
2213 k->class_id = PCI_CLASS_NETWORK_ETHERNET;
2214 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
2215 dc->props = virtio_net_properties;
2216 vpciklass->realize = virtio_net_pci_realize;
2219 static void virtio_net_pci_instance_init(Object *obj)
2221 VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
2223 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2225 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
2226 "bootindex", &error_abort);
2229 static const TypeInfo virtio_net_pci_info = {
2230 .name = TYPE_VIRTIO_NET_PCI,
2231 .parent = TYPE_VIRTIO_PCI,
2232 .instance_size = sizeof(VirtIONetPCI),
2233 .instance_init = virtio_net_pci_instance_init,
2234 .class_init = virtio_net_pci_class_init,
2237 /* virtio-rng-pci */
2239 static void virtio_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2241 VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev);
2242 DeviceState *vdev = DEVICE(&vrng->vdev);
2245 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2246 object_property_set_bool(OBJECT(vdev), true, "realized", &err);
2248 error_propagate(errp, err);
2252 object_property_set_link(OBJECT(vrng),
2253 OBJECT(vrng->vdev.conf.rng), "rng",
2257 static void virtio_rng_pci_class_init(ObjectClass *klass, void *data)
2259 DeviceClass *dc = DEVICE_CLASS(klass);
2260 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2261 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2263 k->realize = virtio_rng_pci_realize;
2264 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2266 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
2267 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
2268 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
2269 pcidev_k->class_id = PCI_CLASS_OTHERS;
2272 static void virtio_rng_initfn(Object *obj)
2274 VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj);
2276 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2278 object_property_add_alias(obj, "rng", OBJECT(&dev->vdev), "rng",
2282 static const TypeInfo virtio_rng_pci_info = {
2283 .name = TYPE_VIRTIO_RNG_PCI,
2284 .parent = TYPE_VIRTIO_PCI,
2285 .instance_size = sizeof(VirtIORngPCI),
2286 .instance_init = virtio_rng_initfn,
2287 .class_init = virtio_rng_pci_class_init,
2290 /* virtio-input-pci */
2292 static Property virtio_input_pci_properties[] = {
2293 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
2294 DEFINE_PROP_END_OF_LIST(),
2297 static void virtio_input_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2299 VirtIOInputPCI *vinput = VIRTIO_INPUT_PCI(vpci_dev);
2300 DeviceState *vdev = DEVICE(&vinput->vdev);
2302 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2303 /* force virtio-1.0 */
2304 vpci_dev->flags &= ~VIRTIO_PCI_FLAG_DISABLE_MODERN;
2305 vpci_dev->flags |= VIRTIO_PCI_FLAG_DISABLE_LEGACY;
2306 object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2309 static void virtio_input_pci_class_init(ObjectClass *klass, void *data)
2311 DeviceClass *dc = DEVICE_CLASS(klass);
2312 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
2313 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2315 dc->props = virtio_input_pci_properties;
2316 k->realize = virtio_input_pci_realize;
2317 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2319 pcidev_k->class_id = PCI_CLASS_INPUT_OTHER;
2322 static void virtio_input_hid_kbd_pci_class_init(ObjectClass *klass, void *data)
2324 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2326 pcidev_k->class_id = PCI_CLASS_INPUT_KEYBOARD;
2329 static void virtio_input_hid_mouse_pci_class_init(ObjectClass *klass,
2332 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2334 pcidev_k->class_id = PCI_CLASS_INPUT_MOUSE;
2337 static void virtio_keyboard_initfn(Object *obj)
2339 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2341 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2342 TYPE_VIRTIO_KEYBOARD);
2345 static void virtio_mouse_initfn(Object *obj)
2347 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2349 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2353 static void virtio_tablet_initfn(Object *obj)
2355 VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2357 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2358 TYPE_VIRTIO_TABLET);
2361 static const TypeInfo virtio_input_pci_info = {
2362 .name = TYPE_VIRTIO_INPUT_PCI,
2363 .parent = TYPE_VIRTIO_PCI,
2364 .instance_size = sizeof(VirtIOInputPCI),
2365 .class_init = virtio_input_pci_class_init,
2369 static const TypeInfo virtio_input_hid_pci_info = {
2370 .name = TYPE_VIRTIO_INPUT_HID_PCI,
2371 .parent = TYPE_VIRTIO_INPUT_PCI,
2372 .instance_size = sizeof(VirtIOInputHIDPCI),
2376 static const TypeInfo virtio_keyboard_pci_info = {
2377 .name = TYPE_VIRTIO_KEYBOARD_PCI,
2378 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2379 .class_init = virtio_input_hid_kbd_pci_class_init,
2380 .instance_size = sizeof(VirtIOInputHIDPCI),
2381 .instance_init = virtio_keyboard_initfn,
2384 static const TypeInfo virtio_mouse_pci_info = {
2385 .name = TYPE_VIRTIO_MOUSE_PCI,
2386 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2387 .class_init = virtio_input_hid_mouse_pci_class_init,
2388 .instance_size = sizeof(VirtIOInputHIDPCI),
2389 .instance_init = virtio_mouse_initfn,
2392 static const TypeInfo virtio_tablet_pci_info = {
2393 .name = TYPE_VIRTIO_TABLET_PCI,
2394 .parent = TYPE_VIRTIO_INPUT_HID_PCI,
2395 .instance_size = sizeof(VirtIOInputHIDPCI),
2396 .instance_init = virtio_tablet_initfn,
2400 static void virtio_host_initfn(Object *obj)
2402 VirtIOInputHostPCI *dev = VIRTIO_INPUT_HOST_PCI(obj);
2404 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
2405 TYPE_VIRTIO_INPUT_HOST);
2408 static const TypeInfo virtio_host_pci_info = {
2409 .name = TYPE_VIRTIO_INPUT_HOST_PCI,
2410 .parent = TYPE_VIRTIO_INPUT_PCI,
2411 .instance_size = sizeof(VirtIOInputHostPCI),
2412 .instance_init = virtio_host_initfn,
2416 /* virtio-pci-bus */
2418 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
2419 VirtIOPCIProxy *dev)
2421 DeviceState *qdev = DEVICE(dev);
2422 char virtio_bus_name[] = "virtio-bus";
2424 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev,
2428 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
2430 BusClass *bus_class = BUS_CLASS(klass);
2431 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
2432 bus_class->max_dev = 1;
2433 k->notify = virtio_pci_notify;
2434 k->save_config = virtio_pci_save_config;
2435 k->load_config = virtio_pci_load_config;
2436 k->save_queue = virtio_pci_save_queue;
2437 k->load_queue = virtio_pci_load_queue;
2438 k->save_extra_state = virtio_pci_save_extra_state;
2439 k->load_extra_state = virtio_pci_load_extra_state;
2440 k->has_extra_state = virtio_pci_has_extra_state;
2441 k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
2442 k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
2443 k->vmstate_change = virtio_pci_vmstate_change;
2444 k->device_plugged = virtio_pci_device_plugged;
2445 k->device_unplugged = virtio_pci_device_unplugged;
2446 k->query_nvectors = virtio_pci_query_nvectors;
2447 k->ioeventfd_started = virtio_pci_ioeventfd_started;
2448 k->ioeventfd_set_started = virtio_pci_ioeventfd_set_started;
2449 k->ioeventfd_disabled = virtio_pci_ioeventfd_disabled;
2450 k->ioeventfd_set_disabled = virtio_pci_ioeventfd_set_disabled;
2451 k->ioeventfd_assign = virtio_pci_ioeventfd_assign;
2454 static const TypeInfo virtio_pci_bus_info = {
2455 .name = TYPE_VIRTIO_PCI_BUS,
2456 .parent = TYPE_VIRTIO_BUS,
2457 .instance_size = sizeof(VirtioPCIBusState),
2458 .class_init = virtio_pci_bus_class_init,
2461 static void virtio_pci_register_types(void)
2463 type_register_static(&virtio_rng_pci_info);
2464 type_register_static(&virtio_input_pci_info);
2465 type_register_static(&virtio_input_hid_pci_info);
2466 type_register_static(&virtio_keyboard_pci_info);
2467 type_register_static(&virtio_mouse_pci_info);
2468 type_register_static(&virtio_tablet_pci_info);
2470 type_register_static(&virtio_host_pci_info);
2472 type_register_static(&virtio_pci_bus_info);
2473 type_register_static(&virtio_pci_info);
2474 #ifdef CONFIG_VIRTFS
2475 type_register_static(&virtio_9p_pci_info);
2477 type_register_static(&virtio_blk_pci_info);
2478 type_register_static(&virtio_scsi_pci_info);
2479 type_register_static(&virtio_balloon_pci_info);
2480 type_register_static(&virtio_serial_pci_info);
2481 type_register_static(&virtio_net_pci_info);
2482 #ifdef CONFIG_VHOST_SCSI
2483 type_register_static(&vhost_scsi_pci_info);
2487 type_init(virtio_pci_register_types)