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.
19 #include "virtio-blk.h"
20 #include "virtio-net.h"
21 #include "virtio-serial.h"
23 #include "qemu-error.h"
29 #include "virtio-pci.h"
31 /* from Linux's linux/virtio_pci.h */
33 /* A 32-bit r/o bitmask of the features supported by the host */
34 #define VIRTIO_PCI_HOST_FEATURES 0
36 /* A 32-bit r/w bitmask of features activated by the guest */
37 #define VIRTIO_PCI_GUEST_FEATURES 4
39 /* A 32-bit r/w PFN for the currently selected queue */
40 #define VIRTIO_PCI_QUEUE_PFN 8
42 /* A 16-bit r/o queue size for the currently selected queue */
43 #define VIRTIO_PCI_QUEUE_NUM 12
45 /* A 16-bit r/w queue selector */
46 #define VIRTIO_PCI_QUEUE_SEL 14
48 /* A 16-bit r/w queue notifier */
49 #define VIRTIO_PCI_QUEUE_NOTIFY 16
51 /* An 8-bit device status register. */
52 #define VIRTIO_PCI_STATUS 18
54 /* An 8-bit r/o interrupt status register. Reading the value will return the
55 * current contents of the ISR and will also clear it. This is effectively
56 * a read-and-acknowledge. */
57 #define VIRTIO_PCI_ISR 19
59 /* MSI-X registers: only enabled if MSI-X is enabled. */
60 /* A 16-bit vector for configuration changes. */
61 #define VIRTIO_MSI_CONFIG_VECTOR 20
62 /* A 16-bit vector for selected queue notifications. */
63 #define VIRTIO_MSI_QUEUE_VECTOR 22
65 /* Config space size */
66 #define VIRTIO_PCI_CONFIG_NOMSI 20
67 #define VIRTIO_PCI_CONFIG_MSI 24
68 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
69 VIRTIO_PCI_CONFIG_MSI : \
70 VIRTIO_PCI_CONFIG_NOMSI)
72 /* The remaining space is defined by each driver as the per-driver
73 * configuration space */
74 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
75 VIRTIO_PCI_CONFIG_MSI : \
76 VIRTIO_PCI_CONFIG_NOMSI)
78 /* Virtio ABI version, if we increment this, we break the guest driver. */
79 #define VIRTIO_PCI_ABI_VERSION 0
81 /* How many bits to shift physical queue address written to QUEUE_PFN.
82 * 12 is historical, and due to x86 page size. */
83 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
85 /* Flags track per-device state like workarounds for quirks in older guests. */
86 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
88 /* Performance improves when virtqueue kick processing is decoupled from the
89 * vcpu thread using ioeventfd for some devices. */
90 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT 1
91 #define VIRTIO_PCI_FLAG_USE_IOEVENTFD (1 << VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT)
93 /* QEMU doesn't strictly need write barriers since everything runs in
94 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
95 * KVM or if kqemu gets SMP support.
97 #define wmb() do { } while (0)
101 static void virtio_pci_notify(void *opaque, uint16_t vector)
103 VirtIOPCIProxy *proxy = opaque;
104 if (msix_enabled(&proxy->pci_dev))
105 msix_notify(&proxy->pci_dev, vector);
107 qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
110 static void virtio_pci_save_config(void * opaque, QEMUFile *f)
112 VirtIOPCIProxy *proxy = opaque;
113 pci_device_save(&proxy->pci_dev, f);
114 msix_save(&proxy->pci_dev, f);
115 if (msix_present(&proxy->pci_dev))
116 qemu_put_be16(f, proxy->vdev->config_vector);
119 static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f)
121 VirtIOPCIProxy *proxy = opaque;
122 if (msix_present(&proxy->pci_dev))
123 qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
126 static int virtio_pci_load_config(void * opaque, QEMUFile *f)
128 VirtIOPCIProxy *proxy = opaque;
130 ret = pci_device_load(&proxy->pci_dev, f);
134 msix_load(&proxy->pci_dev, f);
135 if (msix_present(&proxy->pci_dev)) {
136 qemu_get_be16s(f, &proxy->vdev->config_vector);
138 proxy->vdev->config_vector = VIRTIO_NO_VECTOR;
140 if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
141 return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
146 static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
148 VirtIOPCIProxy *proxy = opaque;
150 if (msix_present(&proxy->pci_dev)) {
151 qemu_get_be16s(f, &vector);
153 vector = VIRTIO_NO_VECTOR;
155 virtio_queue_set_vector(proxy->vdev, n, vector);
156 if (vector != VIRTIO_NO_VECTOR) {
157 return msix_vector_use(&proxy->pci_dev, vector);
162 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
165 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
166 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
169 r = event_notifier_init(notifier, 1);
171 error_report("%s: unable to init event notifier: %d",
175 r = kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier),
176 proxy->addr + VIRTIO_PCI_QUEUE_NOTIFY,
179 error_report("%s: unable to map ioeventfd: %d",
181 event_notifier_cleanup(notifier);
184 r = kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier),
185 proxy->addr + VIRTIO_PCI_QUEUE_NOTIFY,
188 error_report("%s: unable to unmap ioeventfd: %d",
193 /* Handle the race condition where the guest kicked and we deassigned
194 * before we got around to handling the kick.
196 if (event_notifier_test_and_clear(notifier)) {
197 virtio_queue_notify_vq(vq);
200 event_notifier_cleanup(notifier);
205 static void virtio_pci_host_notifier_read(void *opaque)
207 VirtQueue *vq = opaque;
208 EventNotifier *n = virtio_queue_get_host_notifier(vq);
209 if (event_notifier_test_and_clear(n)) {
210 virtio_queue_notify_vq(vq);
214 static void virtio_pci_set_host_notifier_fd_handler(VirtIOPCIProxy *proxy,
217 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
218 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
220 qemu_set_fd_handler(event_notifier_get_fd(notifier),
221 virtio_pci_host_notifier_read, NULL, vq);
223 qemu_set_fd_handler(event_notifier_get_fd(notifier),
228 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
232 if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
233 proxy->ioeventfd_disabled ||
234 proxy->ioeventfd_started) {
238 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
239 if (!virtio_queue_get_num(proxy->vdev, n)) {
243 r = virtio_pci_set_host_notifier_internal(proxy, n, true);
248 virtio_pci_set_host_notifier_fd_handler(proxy, n, true);
250 proxy->ioeventfd_started = true;
255 if (!virtio_queue_get_num(proxy->vdev, n)) {
259 virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
260 r = virtio_pci_set_host_notifier_internal(proxy, n, false);
263 proxy->ioeventfd_started = false;
264 error_report("%s: failed. Fallback to a userspace (slower).", __func__);
267 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
272 if (!proxy->ioeventfd_started) {
276 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
277 if (!virtio_queue_get_num(proxy->vdev, n)) {
281 virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
282 r = virtio_pci_set_host_notifier_internal(proxy, n, false);
285 proxy->ioeventfd_started = false;
288 static void virtio_pci_reset(DeviceState *d)
290 VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev);
291 virtio_pci_stop_ioeventfd(proxy);
292 virtio_reset(proxy->vdev);
293 msix_reset(&proxy->pci_dev);
294 proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
297 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
299 VirtIOPCIProxy *proxy = opaque;
300 VirtIODevice *vdev = proxy->vdev;
301 target_phys_addr_t pa;
304 case VIRTIO_PCI_GUEST_FEATURES:
305 /* Guest does not negotiate properly? We have to assume nothing. */
306 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
307 if (vdev->bad_features)
308 val = proxy->host_features & vdev->bad_features(vdev);
312 if (vdev->set_features)
313 vdev->set_features(vdev, val);
314 vdev->guest_features = val;
316 case VIRTIO_PCI_QUEUE_PFN:
317 pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
319 virtio_pci_stop_ioeventfd(proxy);
320 virtio_reset(proxy->vdev);
321 msix_unuse_all_vectors(&proxy->pci_dev);
324 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
326 case VIRTIO_PCI_QUEUE_SEL:
327 if (val < VIRTIO_PCI_QUEUE_MAX)
328 vdev->queue_sel = val;
330 case VIRTIO_PCI_QUEUE_NOTIFY:
331 virtio_queue_notify(vdev, val);
333 case VIRTIO_PCI_STATUS:
334 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
335 virtio_pci_stop_ioeventfd(proxy);
338 virtio_set_status(vdev, val & 0xFF);
340 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
341 virtio_pci_start_ioeventfd(proxy);
344 if (vdev->status == 0) {
345 virtio_reset(proxy->vdev);
346 msix_unuse_all_vectors(&proxy->pci_dev);
349 /* Linux before 2.6.34 sets the device as OK without enabling
350 the PCI device bus master bit. In this case we need to disable
351 some safety checks. */
352 if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
353 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
354 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
357 case VIRTIO_MSI_CONFIG_VECTOR:
358 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
359 /* Make it possible for guest to discover an error took place. */
360 if (msix_vector_use(&proxy->pci_dev, val) < 0)
361 val = VIRTIO_NO_VECTOR;
362 vdev->config_vector = val;
364 case VIRTIO_MSI_QUEUE_VECTOR:
365 msix_vector_unuse(&proxy->pci_dev,
366 virtio_queue_vector(vdev, vdev->queue_sel));
367 /* Make it possible for guest to discover an error took place. */
368 if (msix_vector_use(&proxy->pci_dev, val) < 0)
369 val = VIRTIO_NO_VECTOR;
370 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
373 error_report("%s: unexpected address 0x%x value 0x%x",
374 __func__, addr, val);
379 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
381 VirtIODevice *vdev = proxy->vdev;
382 uint32_t ret = 0xFFFFFFFF;
385 case VIRTIO_PCI_HOST_FEATURES:
386 ret = proxy->host_features;
388 case VIRTIO_PCI_GUEST_FEATURES:
389 ret = vdev->guest_features;
391 case VIRTIO_PCI_QUEUE_PFN:
392 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
393 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
395 case VIRTIO_PCI_QUEUE_NUM:
396 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
398 case VIRTIO_PCI_QUEUE_SEL:
399 ret = vdev->queue_sel;
401 case VIRTIO_PCI_STATUS:
405 /* reading from the ISR also clears it. */
408 qemu_set_irq(proxy->pci_dev.irq[0], 0);
410 case VIRTIO_MSI_CONFIG_VECTOR:
411 ret = vdev->config_vector;
413 case VIRTIO_MSI_QUEUE_VECTOR:
414 ret = virtio_queue_vector(vdev, vdev->queue_sel);
423 static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
425 VirtIOPCIProxy *proxy = opaque;
426 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
429 return virtio_ioport_read(proxy, addr);
431 return virtio_config_readb(proxy->vdev, addr);
434 static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
436 VirtIOPCIProxy *proxy = opaque;
437 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
440 return virtio_ioport_read(proxy, addr);
442 return virtio_config_readw(proxy->vdev, addr);
445 static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
447 VirtIOPCIProxy *proxy = opaque;
448 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
451 return virtio_ioport_read(proxy, addr);
453 return virtio_config_readl(proxy->vdev, addr);
456 static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
458 VirtIOPCIProxy *proxy = opaque;
459 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
462 virtio_ioport_write(proxy, addr, val);
466 virtio_config_writeb(proxy->vdev, addr, val);
469 static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
471 VirtIOPCIProxy *proxy = opaque;
472 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
475 virtio_ioport_write(proxy, addr, val);
479 virtio_config_writew(proxy->vdev, addr, val);
482 static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
484 VirtIOPCIProxy *proxy = opaque;
485 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
488 virtio_ioport_write(proxy, addr, val);
492 virtio_config_writel(proxy->vdev, addr, val);
495 static void virtio_map(PCIDevice *pci_dev, int region_num,
496 pcibus_t addr, pcibus_t size, int type)
498 VirtIOPCIProxy *proxy = container_of(pci_dev, VirtIOPCIProxy, pci_dev);
499 VirtIODevice *vdev = proxy->vdev;
500 unsigned config_len = VIRTIO_PCI_REGION_SIZE(pci_dev) + vdev->config_len;
504 register_ioport_write(addr, config_len, 1, virtio_pci_config_writeb, proxy);
505 register_ioport_write(addr, config_len, 2, virtio_pci_config_writew, proxy);
506 register_ioport_write(addr, config_len, 4, virtio_pci_config_writel, proxy);
507 register_ioport_read(addr, config_len, 1, virtio_pci_config_readb, proxy);
508 register_ioport_read(addr, config_len, 2, virtio_pci_config_readw, proxy);
509 register_ioport_read(addr, config_len, 4, virtio_pci_config_readl, proxy);
511 if (vdev->config_len)
512 vdev->get_config(vdev, vdev->config);
515 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
516 uint32_t val, int len)
518 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
520 if (PCI_COMMAND == address) {
521 if (!(val & PCI_COMMAND_MASTER)) {
522 if (!(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
523 virtio_pci_stop_ioeventfd(proxy);
524 virtio_set_status(proxy->vdev,
525 proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
530 pci_default_write_config(pci_dev, address, val, len);
531 msix_write_config(pci_dev, address, val, len);
534 static unsigned virtio_pci_get_features(void *opaque)
536 VirtIOPCIProxy *proxy = opaque;
537 return proxy->host_features;
540 static void virtio_pci_guest_notifier_read(void *opaque)
542 VirtQueue *vq = opaque;
543 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
544 if (event_notifier_test_and_clear(n)) {
549 static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign)
551 VirtIOPCIProxy *proxy = opaque;
552 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
553 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
556 int r = event_notifier_init(notifier, 0);
560 qemu_set_fd_handler(event_notifier_get_fd(notifier),
561 virtio_pci_guest_notifier_read, NULL, vq);
563 qemu_set_fd_handler(event_notifier_get_fd(notifier),
565 event_notifier_cleanup(notifier);
571 static bool virtio_pci_query_guest_notifiers(void *opaque)
573 VirtIOPCIProxy *proxy = opaque;
574 return msix_enabled(&proxy->pci_dev);
577 static int virtio_pci_set_guest_notifiers(void *opaque, bool assign)
579 VirtIOPCIProxy *proxy = opaque;
580 VirtIODevice *vdev = proxy->vdev;
583 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
584 if (!virtio_queue_get_num(vdev, n)) {
588 r = virtio_pci_set_guest_notifier(opaque, n, assign);
597 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
599 virtio_pci_set_guest_notifier(opaque, n, !assign);
604 static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign)
606 VirtIOPCIProxy *proxy = opaque;
608 /* Stop using ioeventfd for virtqueue kick if the device starts using host
609 * notifiers. This makes it easy to avoid stepping on each others' toes.
611 proxy->ioeventfd_disabled = assign;
613 virtio_pci_stop_ioeventfd(proxy);
615 /* We don't need to start here: it's not needed because backend
616 * currently only stops on status change away from ok,
617 * reset, vmstop and such. If we do add code to start here,
618 * need to check vmstate, device state etc. */
619 return virtio_pci_set_host_notifier_internal(proxy, n, assign);
622 static void virtio_pci_vmstate_change(void *opaque, bool running)
624 VirtIOPCIProxy *proxy = opaque;
627 /* Try to find out if the guest has bus master disabled, but is
628 in ready state. Then we have a buggy guest OS. */
629 if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
630 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
631 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
633 virtio_pci_start_ioeventfd(proxy);
635 virtio_pci_stop_ioeventfd(proxy);
639 static const VirtIOBindings virtio_pci_bindings = {
640 .notify = virtio_pci_notify,
641 .save_config = virtio_pci_save_config,
642 .load_config = virtio_pci_load_config,
643 .save_queue = virtio_pci_save_queue,
644 .load_queue = virtio_pci_load_queue,
645 .get_features = virtio_pci_get_features,
646 .query_guest_notifiers = virtio_pci_query_guest_notifiers,
647 .set_host_notifier = virtio_pci_set_host_notifier,
648 .set_guest_notifiers = virtio_pci_set_guest_notifiers,
649 .vmstate_change = virtio_pci_vmstate_change,
652 void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev,
653 uint16_t vendor, uint16_t device,
654 uint16_t class_code, uint8_t pif)
661 config = proxy->pci_dev.config;
662 pci_config_set_vendor_id(config, vendor);
663 pci_config_set_device_id(config, device);
665 config[0x08] = VIRTIO_PCI_ABI_VERSION;
668 pci_config_set_class(config, class_code);
670 config[0x2c] = vendor & 0xFF;
671 config[0x2d] = (vendor >> 8) & 0xFF;
672 config[0x2e] = vdev->device_id & 0xFF;
673 config[0x2f] = (vdev->device_id >> 8) & 0xFF;
677 if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors, 1, 0)) {
678 pci_register_bar(&proxy->pci_dev, 1,
679 msix_bar_size(&proxy->pci_dev),
680 PCI_BASE_ADDRESS_SPACE_MEMORY,
685 proxy->pci_dev.config_write = virtio_write_config;
687 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
689 size = 1 << qemu_fls(size);
691 pci_register_bar(&proxy->pci_dev, 0, size, PCI_BASE_ADDRESS_SPACE_IO,
694 if (!kvm_has_many_ioeventfds()) {
695 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
698 virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
699 proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
700 proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
701 proxy->host_features = vdev->get_features(vdev, proxy->host_features);
704 static int virtio_blk_init_pci(PCIDevice *pci_dev)
706 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
709 if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
710 proxy->class_code != PCI_CLASS_STORAGE_OTHER)
711 proxy->class_code = PCI_CLASS_STORAGE_SCSI;
713 vdev = virtio_blk_init(&pci_dev->qdev, &proxy->block);
717 vdev->nvectors = proxy->nvectors;
718 virtio_init_pci(proxy, vdev,
719 PCI_VENDOR_ID_REDHAT_QUMRANET,
720 PCI_DEVICE_ID_VIRTIO_BLOCK,
721 proxy->class_code, 0x00);
722 /* make the actual value visible */
723 proxy->nvectors = vdev->nvectors;
727 static int virtio_exit_pci(PCIDevice *pci_dev)
729 return msix_uninit(pci_dev);
732 static int virtio_blk_exit_pci(PCIDevice *pci_dev)
734 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
736 virtio_pci_stop_ioeventfd(proxy);
737 virtio_blk_exit(proxy->vdev);
738 blockdev_mark_auto_del(proxy->block.bs);
739 return virtio_exit_pci(pci_dev);
742 static int virtio_serial_init_pci(PCIDevice *pci_dev)
744 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
747 if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
748 proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
749 proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */
750 proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;
752 vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial);
756 vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
757 ? proxy->serial.max_virtserial_ports + 1
759 virtio_init_pci(proxy, vdev,
760 PCI_VENDOR_ID_REDHAT_QUMRANET,
761 PCI_DEVICE_ID_VIRTIO_CONSOLE,
762 proxy->class_code, 0x00);
763 proxy->nvectors = vdev->nvectors;
767 static int virtio_serial_exit_pci(PCIDevice *pci_dev)
769 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
771 virtio_pci_stop_ioeventfd(proxy);
772 virtio_serial_exit(proxy->vdev);
773 return virtio_exit_pci(pci_dev);
776 static int virtio_net_init_pci(PCIDevice *pci_dev)
778 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
781 vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net);
783 vdev->nvectors = proxy->nvectors;
784 virtio_init_pci(proxy, vdev,
785 PCI_VENDOR_ID_REDHAT_QUMRANET,
786 PCI_DEVICE_ID_VIRTIO_NET,
787 PCI_CLASS_NETWORK_ETHERNET,
790 /* make the actual value visible */
791 proxy->nvectors = vdev->nvectors;
795 static int virtio_net_exit_pci(PCIDevice *pci_dev)
797 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
799 virtio_pci_stop_ioeventfd(proxy);
800 virtio_net_exit(proxy->vdev);
801 return virtio_exit_pci(pci_dev);
804 static int virtio_balloon_init_pci(PCIDevice *pci_dev)
806 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
809 vdev = virtio_balloon_init(&pci_dev->qdev);
810 virtio_init_pci(proxy, vdev,
811 PCI_VENDOR_ID_REDHAT_QUMRANET,
812 PCI_DEVICE_ID_VIRTIO_BALLOON,
813 PCI_CLASS_MEMORY_RAM,
818 static PCIDeviceInfo virtio_info[] = {
820 .qdev.name = "virtio-blk-pci",
821 .qdev.alias = "virtio-blk",
822 .qdev.size = sizeof(VirtIOPCIProxy),
823 .init = virtio_blk_init_pci,
824 .exit = virtio_blk_exit_pci,
825 .qdev.props = (Property[]) {
826 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
827 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, block),
828 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
829 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
830 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
831 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
832 DEFINE_PROP_END_OF_LIST(),
834 .qdev.reset = virtio_pci_reset,
836 .qdev.name = "virtio-net-pci",
837 .qdev.alias = "virtio-net",
838 .qdev.size = sizeof(VirtIOPCIProxy),
839 .init = virtio_net_init_pci,
840 .exit = virtio_net_exit_pci,
841 .romfile = "pxe-virtio.rom",
842 .qdev.props = (Property[]) {
843 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
844 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
845 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
846 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
847 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic),
848 DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy,
849 net.txtimer, TX_TIMER_INTERVAL),
850 DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy,
851 net.txburst, TX_BURST),
852 DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx),
853 DEFINE_PROP_END_OF_LIST(),
855 .qdev.reset = virtio_pci_reset,
857 .qdev.name = "virtio-serial-pci",
858 .qdev.alias = "virtio-serial",
859 .qdev.size = sizeof(VirtIOPCIProxy),
860 .init = virtio_serial_init_pci,
861 .exit = virtio_serial_exit_pci,
862 .qdev.props = (Property[]) {
863 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
864 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
865 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
866 DEV_NVECTORS_UNSPECIFIED),
867 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
868 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
869 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy,
870 serial.max_virtserial_ports, 31),
871 DEFINE_PROP_END_OF_LIST(),
873 .qdev.reset = virtio_pci_reset,
875 .qdev.name = "virtio-balloon-pci",
876 .qdev.alias = "virtio-balloon",
877 .qdev.size = sizeof(VirtIOPCIProxy),
878 .init = virtio_balloon_init_pci,
879 .exit = virtio_exit_pci,
880 .qdev.props = (Property[]) {
881 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
882 DEFINE_PROP_END_OF_LIST(),
884 .qdev.reset = virtio_pci_reset,
890 static void virtio_pci_register_devices(void)
892 pci_qdev_register_many(virtio_info);
895 device_init(virtio_pci_register_devices)