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.
20 #include "virtio-blk.h"
21 #include "virtio-net.h"
22 #include "virtio-serial.h"
24 #include "qemu-error.h"
30 #include "virtio-pci.h"
33 #include "../tizen/src/hw/maru_device_ids.h"
36 /* from Linux's linux/virtio_pci.h */
38 /* A 32-bit r/o bitmask of the features supported by the host */
39 #define VIRTIO_PCI_HOST_FEATURES 0
41 /* A 32-bit r/w bitmask of features activated by the guest */
42 #define VIRTIO_PCI_GUEST_FEATURES 4
44 /* A 32-bit r/w PFN for the currently selected queue */
45 #define VIRTIO_PCI_QUEUE_PFN 8
47 /* A 16-bit r/o queue size for the currently selected queue */
48 #define VIRTIO_PCI_QUEUE_NUM 12
50 /* A 16-bit r/w queue selector */
51 #define VIRTIO_PCI_QUEUE_SEL 14
53 /* A 16-bit r/w queue notifier */
54 #define VIRTIO_PCI_QUEUE_NOTIFY 16
56 /* An 8-bit device status register. */
57 #define VIRTIO_PCI_STATUS 18
59 /* An 8-bit r/o interrupt status register. Reading the value will return the
60 * current contents of the ISR and will also clear it. This is effectively
61 * a read-and-acknowledge. */
62 #define VIRTIO_PCI_ISR 19
64 /* MSI-X registers: only enabled if MSI-X is enabled. */
65 /* A 16-bit vector for configuration changes. */
66 #define VIRTIO_MSI_CONFIG_VECTOR 20
67 /* A 16-bit vector for selected queue notifications. */
68 #define VIRTIO_MSI_QUEUE_VECTOR 22
70 /* Config space size */
71 #define VIRTIO_PCI_CONFIG_NOMSI 20
72 #define VIRTIO_PCI_CONFIG_MSI 24
73 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
74 VIRTIO_PCI_CONFIG_MSI : \
75 VIRTIO_PCI_CONFIG_NOMSI)
77 /* The remaining space is defined by each driver as the per-driver
78 * configuration space */
79 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
80 VIRTIO_PCI_CONFIG_MSI : \
81 VIRTIO_PCI_CONFIG_NOMSI)
83 /* How many bits to shift physical queue address written to QUEUE_PFN.
84 * 12 is historical, and due to x86 page size. */
85 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
87 /* Flags track per-device state like workarounds for quirks in older guests. */
88 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
90 /* QEMU doesn't strictly need write barriers since everything runs in
91 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
92 * KVM or if kqemu gets SMP support.
94 #define wmb() do { } while (0)
98 static void virtio_pci_notify(void *opaque, uint16_t vector)
100 VirtIOPCIProxy *proxy = opaque;
101 if (msix_enabled(&proxy->pci_dev))
102 msix_notify(&proxy->pci_dev, vector);
104 qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
107 static void virtio_pci_save_config(void * opaque, QEMUFile *f)
109 VirtIOPCIProxy *proxy = opaque;
110 pci_device_save(&proxy->pci_dev, f);
111 msix_save(&proxy->pci_dev, f);
112 if (msix_present(&proxy->pci_dev))
113 qemu_put_be16(f, proxy->vdev->config_vector);
116 static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f)
118 VirtIOPCIProxy *proxy = opaque;
119 if (msix_present(&proxy->pci_dev))
120 qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
123 static int virtio_pci_load_config(void * opaque, QEMUFile *f)
125 VirtIOPCIProxy *proxy = opaque;
127 ret = pci_device_load(&proxy->pci_dev, f);
131 msix_load(&proxy->pci_dev, f);
132 if (msix_present(&proxy->pci_dev)) {
133 qemu_get_be16s(f, &proxy->vdev->config_vector);
135 proxy->vdev->config_vector = VIRTIO_NO_VECTOR;
137 if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
138 return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
143 static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
145 VirtIOPCIProxy *proxy = opaque;
147 if (msix_present(&proxy->pci_dev)) {
148 qemu_get_be16s(f, &vector);
150 vector = VIRTIO_NO_VECTOR;
152 virtio_queue_set_vector(proxy->vdev, n, vector);
153 if (vector != VIRTIO_NO_VECTOR) {
154 return msix_vector_use(&proxy->pci_dev, vector);
159 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
162 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
163 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
167 r = event_notifier_init(notifier, 1);
169 error_report("%s: unable to init event notifier: %d",
173 memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
174 true, n, event_notifier_get_fd(notifier));
176 memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
177 true, n, event_notifier_get_fd(notifier));
178 /* Handle the race condition where the guest kicked and we deassigned
179 * before we got around to handling the kick.
181 if (event_notifier_test_and_clear(notifier)) {
182 virtio_queue_notify_vq(vq);
185 event_notifier_cleanup(notifier);
190 static void virtio_pci_host_notifier_read(void *opaque)
192 VirtQueue *vq = opaque;
193 EventNotifier *n = virtio_queue_get_host_notifier(vq);
194 if (event_notifier_test_and_clear(n)) {
195 virtio_queue_notify_vq(vq);
199 static void virtio_pci_set_host_notifier_fd_handler(VirtIOPCIProxy *proxy,
202 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
203 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
205 qemu_set_fd_handler(event_notifier_get_fd(notifier),
206 virtio_pci_host_notifier_read, NULL, vq);
208 qemu_set_fd_handler(event_notifier_get_fd(notifier),
213 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
217 if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
218 proxy->ioeventfd_disabled ||
219 proxy->ioeventfd_started) {
223 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
224 if (!virtio_queue_get_num(proxy->vdev, n)) {
228 r = virtio_pci_set_host_notifier_internal(proxy, n, true);
233 virtio_pci_set_host_notifier_fd_handler(proxy, n, true);
235 proxy->ioeventfd_started = true;
240 if (!virtio_queue_get_num(proxy->vdev, n)) {
244 virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
245 r = virtio_pci_set_host_notifier_internal(proxy, n, false);
248 proxy->ioeventfd_started = false;
249 error_report("%s: failed. Fallback to a userspace (slower).", __func__);
252 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
257 if (!proxy->ioeventfd_started) {
261 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
262 if (!virtio_queue_get_num(proxy->vdev, n)) {
266 virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
267 r = virtio_pci_set_host_notifier_internal(proxy, n, false);
270 proxy->ioeventfd_started = false;
273 void virtio_pci_reset(DeviceState *d)
275 VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev);
276 virtio_pci_stop_ioeventfd(proxy);
277 virtio_reset(proxy->vdev);
278 msix_reset(&proxy->pci_dev);
279 proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
282 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
284 VirtIOPCIProxy *proxy = opaque;
285 VirtIODevice *vdev = proxy->vdev;
286 target_phys_addr_t pa;
289 case VIRTIO_PCI_GUEST_FEATURES:
290 /* Guest does not negotiate properly? We have to assume nothing. */
291 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
292 val = vdev->bad_features ? vdev->bad_features(vdev) : 0;
294 virtio_set_features(vdev, val);
296 case VIRTIO_PCI_QUEUE_PFN:
297 pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
299 virtio_pci_stop_ioeventfd(proxy);
300 virtio_reset(proxy->vdev);
301 msix_unuse_all_vectors(&proxy->pci_dev);
304 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
306 case VIRTIO_PCI_QUEUE_SEL:
307 if (val < VIRTIO_PCI_QUEUE_MAX)
308 vdev->queue_sel = val;
310 case VIRTIO_PCI_QUEUE_NOTIFY:
311 if (val < VIRTIO_PCI_QUEUE_MAX) {
312 virtio_queue_notify(vdev, val);
315 case VIRTIO_PCI_STATUS:
316 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
317 virtio_pci_stop_ioeventfd(proxy);
320 virtio_set_status(vdev, val & 0xFF);
322 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
323 virtio_pci_start_ioeventfd(proxy);
326 if (vdev->status == 0) {
327 virtio_reset(proxy->vdev);
328 msix_unuse_all_vectors(&proxy->pci_dev);
331 /* Linux before 2.6.34 sets the device as OK without enabling
332 the PCI device bus master bit. In this case we need to disable
333 some safety checks. */
334 if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
335 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
336 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
339 case VIRTIO_MSI_CONFIG_VECTOR:
340 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
341 /* Make it possible for guest to discover an error took place. */
342 if (msix_vector_use(&proxy->pci_dev, val) < 0)
343 val = VIRTIO_NO_VECTOR;
344 vdev->config_vector = val;
346 case VIRTIO_MSI_QUEUE_VECTOR:
347 msix_vector_unuse(&proxy->pci_dev,
348 virtio_queue_vector(vdev, vdev->queue_sel));
349 /* Make it possible for guest to discover an error took place. */
350 if (msix_vector_use(&proxy->pci_dev, val) < 0)
351 val = VIRTIO_NO_VECTOR;
352 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
355 error_report("%s: unexpected address 0x%x value 0x%x",
356 __func__, addr, val);
361 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
363 VirtIODevice *vdev = proxy->vdev;
364 uint32_t ret = 0xFFFFFFFF;
367 case VIRTIO_PCI_HOST_FEATURES:
368 ret = proxy->host_features;
370 case VIRTIO_PCI_GUEST_FEATURES:
371 ret = vdev->guest_features;
373 case VIRTIO_PCI_QUEUE_PFN:
374 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
375 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
377 case VIRTIO_PCI_QUEUE_NUM:
378 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
380 case VIRTIO_PCI_QUEUE_SEL:
381 ret = vdev->queue_sel;
383 case VIRTIO_PCI_STATUS:
387 /* reading from the ISR also clears it. */
390 qemu_set_irq(proxy->pci_dev.irq[0], 0);
392 case VIRTIO_MSI_CONFIG_VECTOR:
393 ret = vdev->config_vector;
395 case VIRTIO_MSI_QUEUE_VECTOR:
396 ret = virtio_queue_vector(vdev, vdev->queue_sel);
405 static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
407 VirtIOPCIProxy *proxy = opaque;
408 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
410 return virtio_ioport_read(proxy, addr);
412 return virtio_config_readb(proxy->vdev, addr);
415 static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
417 VirtIOPCIProxy *proxy = opaque;
418 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
420 return virtio_ioport_read(proxy, addr);
422 return virtio_config_readw(proxy->vdev, addr);
425 static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
427 VirtIOPCIProxy *proxy = opaque;
428 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
430 return virtio_ioport_read(proxy, addr);
432 return virtio_config_readl(proxy->vdev, addr);
435 static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
437 VirtIOPCIProxy *proxy = opaque;
438 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
440 virtio_ioport_write(proxy, addr, val);
444 virtio_config_writeb(proxy->vdev, addr, val);
447 static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
449 VirtIOPCIProxy *proxy = opaque;
450 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
452 virtio_ioport_write(proxy, addr, val);
456 virtio_config_writew(proxy->vdev, addr, val);
459 static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
461 VirtIOPCIProxy *proxy = opaque;
462 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
464 virtio_ioport_write(proxy, addr, val);
468 virtio_config_writel(proxy->vdev, addr, val);
471 const MemoryRegionPortio virtio_portio[] = {
472 { 0, 0x10000, 1, .write = virtio_pci_config_writeb, },
473 { 0, 0x10000, 2, .write = virtio_pci_config_writew, },
474 { 0, 0x10000, 4, .write = virtio_pci_config_writel, },
475 { 0, 0x10000, 1, .read = virtio_pci_config_readb, },
476 { 0, 0x10000, 2, .read = virtio_pci_config_readw, },
477 { 0, 0x10000, 4, .read = virtio_pci_config_readl, },
481 static const MemoryRegionOps virtio_pci_config_ops = {
482 .old_portio = virtio_portio,
483 .endianness = DEVICE_LITTLE_ENDIAN,
486 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
487 uint32_t val, int len)
489 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
491 pci_default_write_config(pci_dev, address, val, len);
493 if (range_covers_byte(address, len, PCI_COMMAND) &&
494 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
495 !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
496 virtio_pci_stop_ioeventfd(proxy);
497 virtio_set_status(proxy->vdev,
498 proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
501 msix_write_config(pci_dev, address, val, len);
504 static unsigned virtio_pci_get_features(void *opaque)
506 VirtIOPCIProxy *proxy = opaque;
507 return proxy->host_features;
510 static void virtio_pci_guest_notifier_read(void *opaque)
512 VirtQueue *vq = opaque;
513 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
514 if (event_notifier_test_and_clear(n)) {
519 static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign)
521 VirtIOPCIProxy *proxy = opaque;
522 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
523 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
526 int r = event_notifier_init(notifier, 0);
530 qemu_set_fd_handler(event_notifier_get_fd(notifier),
531 virtio_pci_guest_notifier_read, NULL, vq);
533 qemu_set_fd_handler(event_notifier_get_fd(notifier),
535 event_notifier_cleanup(notifier);
541 static bool virtio_pci_query_guest_notifiers(void *opaque)
543 VirtIOPCIProxy *proxy = opaque;
544 return msix_enabled(&proxy->pci_dev);
547 static int virtio_pci_set_guest_notifiers(void *opaque, bool assign)
549 VirtIOPCIProxy *proxy = opaque;
550 VirtIODevice *vdev = proxy->vdev;
553 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
554 if (!virtio_queue_get_num(vdev, n)) {
558 r = virtio_pci_set_guest_notifier(opaque, n, assign);
567 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
569 virtio_pci_set_guest_notifier(opaque, n, !assign);
574 static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign)
576 VirtIOPCIProxy *proxy = opaque;
578 /* Stop using ioeventfd for virtqueue kick if the device starts using host
579 * notifiers. This makes it easy to avoid stepping on each others' toes.
581 proxy->ioeventfd_disabled = assign;
583 virtio_pci_stop_ioeventfd(proxy);
585 /* We don't need to start here: it's not needed because backend
586 * currently only stops on status change away from ok,
587 * reset, vmstop and such. If we do add code to start here,
588 * need to check vmstate, device state etc. */
589 return virtio_pci_set_host_notifier_internal(proxy, n, assign);
592 static void virtio_pci_vmstate_change(void *opaque, bool running)
594 VirtIOPCIProxy *proxy = opaque;
597 /* Try to find out if the guest has bus master disabled, but is
598 in ready state. Then we have a buggy guest OS. */
599 if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
600 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
601 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
603 virtio_pci_start_ioeventfd(proxy);
605 virtio_pci_stop_ioeventfd(proxy);
609 static const VirtIOBindings virtio_pci_bindings = {
610 .notify = virtio_pci_notify,
611 .save_config = virtio_pci_save_config,
612 .load_config = virtio_pci_load_config,
613 .save_queue = virtio_pci_save_queue,
614 .load_queue = virtio_pci_load_queue,
615 .get_features = virtio_pci_get_features,
616 .query_guest_notifiers = virtio_pci_query_guest_notifiers,
617 .set_host_notifier = virtio_pci_set_host_notifier,
618 .set_guest_notifiers = virtio_pci_set_guest_notifiers,
619 .vmstate_change = virtio_pci_vmstate_change,
622 void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev)
629 config = proxy->pci_dev.config;
631 if (proxy->class_code) {
632 pci_config_set_class(config, proxy->class_code);
634 pci_set_word(config + 0x2c, pci_get_word(config + PCI_VENDOR_ID));
635 pci_set_word(config + 0x2e, vdev->device_id);
638 memory_region_init(&proxy->msix_bar, "virtio-msix", 4096);
639 if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors,
640 &proxy->msix_bar, 1, 0)) {
641 pci_register_bar(&proxy->pci_dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY,
646 proxy->pci_dev.config_write = virtio_write_config;
648 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
650 size = 1 << qemu_fls(size);
652 memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy,
654 pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
657 if (!kvm_has_many_ioeventfds()) {
658 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
661 virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
662 proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
663 proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
664 proxy->host_features = vdev->get_features(vdev, proxy->host_features);
667 static int virtio_blk_init_pci(PCIDevice *pci_dev)
669 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
672 if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
673 proxy->class_code != PCI_CLASS_STORAGE_OTHER)
674 proxy->class_code = PCI_CLASS_STORAGE_SCSI;
676 vdev = virtio_blk_init(&pci_dev->qdev, &proxy->block,
677 &proxy->block_serial);
681 vdev->nvectors = proxy->nvectors;
682 virtio_init_pci(proxy, vdev);
683 /* make the actual value visible */
684 proxy->nvectors = vdev->nvectors;
688 static int virtio_exit_pci(PCIDevice *pci_dev)
690 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
693 memory_region_destroy(&proxy->bar);
694 r = msix_uninit(pci_dev, &proxy->msix_bar);
695 memory_region_destroy(&proxy->msix_bar);
699 static int virtio_blk_exit_pci(PCIDevice *pci_dev)
701 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
703 virtio_pci_stop_ioeventfd(proxy);
704 virtio_blk_exit(proxy->vdev);
705 blockdev_mark_auto_del(proxy->block.bs);
706 return virtio_exit_pci(pci_dev);
709 static int virtio_serial_init_pci(PCIDevice *pci_dev)
711 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
714 if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
715 proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
716 proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */
717 proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;
719 vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial);
723 vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
724 ? proxy->serial.max_virtserial_ports + 1
726 virtio_init_pci(proxy, vdev);
727 proxy->nvectors = vdev->nvectors;
731 static int virtio_serial_exit_pci(PCIDevice *pci_dev)
733 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
735 virtio_pci_stop_ioeventfd(proxy);
736 virtio_serial_exit(proxy->vdev);
737 return virtio_exit_pci(pci_dev);
740 static int virtio_net_init_pci(PCIDevice *pci_dev)
742 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
745 vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net);
747 vdev->nvectors = proxy->nvectors;
748 virtio_init_pci(proxy, vdev);
750 /* make the actual value visible */
751 proxy->nvectors = vdev->nvectors;
755 static int virtio_net_exit_pci(PCIDevice *pci_dev)
757 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
759 virtio_pci_stop_ioeventfd(proxy);
760 virtio_net_exit(proxy->vdev);
761 return virtio_exit_pci(pci_dev);
764 static int virtio_balloon_init_pci(PCIDevice *pci_dev)
766 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
769 vdev = virtio_balloon_init(&pci_dev->qdev);
773 virtio_init_pci(proxy, vdev);
777 static int virtio_balloon_exit_pci(PCIDevice *pci_dev)
779 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
781 virtio_pci_stop_ioeventfd(proxy);
782 virtio_balloon_exit(proxy->vdev);
783 return virtio_exit_pci(pci_dev);
786 #if defined(CONFIG_MARU) && (!defined(CONFIG_DARWIN))
787 extern VirtIODevice *virtio_gl_init(DeviceState *dev);
788 static int virtio_gl_init_pci(PCIDevice *pci_dev)
790 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
793 vdev = virtio_gl_init(&pci_dev->qdev);
797 virtio_init_pci(proxy, vdev);
803 static int maru_virtio_touchscreen_init_pci(PCIDevice *pci_dev)
805 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
808 vdev = maru_virtio_touchscreen_init(&pci_dev->qdev);
812 virtio_init_pci(proxy, vdev);
816 static int maru_virtio_touchscreen_exit_pci(PCIDevice *pci_dev)
818 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
820 virtio_pci_stop_ioeventfd(proxy);
821 maru_virtio_touchscreen_exit(proxy->vdev);
822 return virtio_exit_pci(pci_dev);
827 static PCIDeviceInfo virtio_info[] = {
829 .qdev.name = "virtio-blk-pci",
830 .qdev.alias = "virtio-blk",
831 .qdev.size = sizeof(VirtIOPCIProxy),
832 .init = virtio_blk_init_pci,
833 .exit = virtio_blk_exit_pci,
834 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
835 .device_id = PCI_DEVICE_ID_VIRTIO_BLOCK,
836 .revision = VIRTIO_PCI_ABI_VERSION,
837 .class_id = PCI_CLASS_STORAGE_SCSI,
838 .qdev.props = (Property[]) {
839 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
840 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, block),
841 DEFINE_PROP_STRING("serial", VirtIOPCIProxy, block_serial),
842 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
843 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
844 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
845 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
846 DEFINE_PROP_END_OF_LIST(),
848 .qdev.reset = virtio_pci_reset,
850 .qdev.name = "virtio-net-pci",
851 .qdev.alias = "virtio-net",
852 .qdev.size = sizeof(VirtIOPCIProxy),
853 .init = virtio_net_init_pci,
854 .exit = virtio_net_exit_pci,
855 .romfile = "pxe-virtio.rom",
856 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
857 .device_id = PCI_DEVICE_ID_VIRTIO_NET,
858 .revision = VIRTIO_PCI_ABI_VERSION,
859 .class_id = PCI_CLASS_NETWORK_ETHERNET,
860 .qdev.props = (Property[]) {
861 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
862 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
863 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
864 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
865 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic),
866 DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy,
867 net.txtimer, TX_TIMER_INTERVAL),
868 DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy,
869 net.txburst, TX_BURST),
870 DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx),
871 DEFINE_PROP_END_OF_LIST(),
873 .qdev.reset = virtio_pci_reset,
875 .qdev.name = "virtio-serial-pci",
876 .qdev.alias = "virtio-serial",
877 .qdev.size = sizeof(VirtIOPCIProxy),
878 .init = virtio_serial_init_pci,
879 .exit = virtio_serial_exit_pci,
880 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
881 .device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE,
882 .revision = VIRTIO_PCI_ABI_VERSION,
883 .class_id = PCI_CLASS_COMMUNICATION_OTHER,
884 .qdev.props = (Property[]) {
885 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
886 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
887 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
888 DEV_NVECTORS_UNSPECIFIED),
889 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
890 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
891 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy,
892 serial.max_virtserial_ports, 31),
893 DEFINE_PROP_END_OF_LIST(),
895 .qdev.reset = virtio_pci_reset,
897 .qdev.name = "virtio-balloon-pci",
898 .qdev.alias = "virtio-balloon",
899 .qdev.size = sizeof(VirtIOPCIProxy),
900 .init = virtio_balloon_init_pci,
901 .exit = virtio_balloon_exit_pci,
902 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
903 .device_id = PCI_DEVICE_ID_VIRTIO_BALLOON,
904 .revision = VIRTIO_PCI_ABI_VERSION,
905 .class_id = PCI_CLASS_MEMORY_RAM,
906 .qdev.props = (Property[]) {
907 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
908 DEFINE_PROP_END_OF_LIST(),
910 .qdev.reset = virtio_pci_reset,
912 #if defined(CONFIG_MARU) && (!defined(CONFIG_DARWIN))
913 .qdev.name = "virtio-gl-pci",
914 .qdev.alias = "virtio-gl",
915 .qdev.size = sizeof(VirtIOPCIProxy),
916 .init = virtio_gl_init_pci,
917 .exit = virtio_exit_pci,
918 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
919 .device_id = PCI_DEVICE_ID_VIRTIO_GL,
920 .revision = VIRTIO_PCI_ABI_VERSION,
921 .class_id = PCI_CLASS_OTHERS,
922 .qdev.props = (Property[]) {
923 DEFINE_PROP_END_OF_LIST(),
925 .qdev.reset = virtio_pci_reset,
930 .qdev.name = "virtio-touchscreen-pci",
931 .qdev.alias = "virtio-touchscreen",
932 .qdev.size = sizeof(VirtIOPCIProxy),
933 .init = maru_virtio_touchscreen_init_pci,
934 .exit = maru_virtio_touchscreen_exit_pci,
935 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
936 .device_id = PCI_DEVICE_ID_VIRTIO_TOUCHSCREEN,
937 .revision = VIRTIO_PCI_ABI_VERSION,
938 .class_id = PCI_CLASS_OTHERS,
939 .qdev.props = (Property[]) {
940 DEFINE_PROP_END_OF_LIST(),
942 .qdev.reset = virtio_pci_reset,
950 static void virtio_pci_register_devices(void)
952 pci_qdev_register_many(virtio_info);
955 device_init(virtio_pci_register_devices)