xref: /qemu/tests/qtest/libqos/virtio-pci.c (revision b243c73c)
11cf4323eSThomas Huth /*
21cf4323eSThomas Huth  * libqos virtio PCI driver
31cf4323eSThomas Huth  *
41cf4323eSThomas Huth  * Copyright (c) 2014 Marc Marí
51cf4323eSThomas Huth  *
61cf4323eSThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2 or later.
71cf4323eSThomas Huth  * See the COPYING file in the top-level directory.
81cf4323eSThomas Huth  */
91cf4323eSThomas Huth 
101cf4323eSThomas Huth #include "qemu/osdep.h"
11907b5105SMarc-André Lureau #include "../libqtest.h"
12a2ce7dbdSPaolo Bonzini #include "virtio.h"
13a2ce7dbdSPaolo Bonzini #include "virtio-pci.h"
14a2ce7dbdSPaolo Bonzini #include "pci.h"
15a2ce7dbdSPaolo Bonzini #include "pci-pc.h"
16b243c73cSXuzhou Cheng #include "libqos-malloc.h"
17a2ce7dbdSPaolo Bonzini #include "malloc-pc.h"
18a2ce7dbdSPaolo Bonzini #include "qgraph.h"
191cf4323eSThomas Huth #include "standard-headers/linux/virtio_ring.h"
201cf4323eSThomas Huth #include "standard-headers/linux/virtio_pci.h"
211cf4323eSThomas Huth 
221cf4323eSThomas Huth #include "hw/pci/pci.h"
231cf4323eSThomas Huth #include "hw/pci/pci_regs.h"
241cf4323eSThomas Huth 
251cf4323eSThomas Huth #include "virtio-pci-modern.h"
261cf4323eSThomas Huth 
271cf4323eSThomas Huth /* virtio-pci is a superclass of all virtio-xxx-pci devices;
281cf4323eSThomas Huth  * the relation between virtio-pci and virtio-xxx-pci is implicit,
291cf4323eSThomas Huth  * and therefore virtio-pci does not produce virtio and is not
301cf4323eSThomas Huth  * reached by any edge, not even as a "contains" edge.
311cf4323eSThomas Huth  * In facts, every device is a QVirtioPCIDevice with
321cf4323eSThomas Huth  * additional fields, since every one has its own
331cf4323eSThomas Huth  * number of queues and various attributes.
341cf4323eSThomas Huth  * Virtio-pci provides default functions to start the
351cf4323eSThomas Huth  * hw and destroy the object, and nodes that want to
361cf4323eSThomas Huth  * override them should always remember to call the
371cf4323eSThomas Huth  * original qvirtio_pci_destructor and qvirtio_pci_start_hw.
381cf4323eSThomas Huth  */
391cf4323eSThomas Huth 
401cf4323eSThomas Huth #define CONFIG_BASE(dev) (VIRTIO_PCI_CONFIG_OFF((dev)->pdev->msix_enabled))
411cf4323eSThomas Huth 
qvirtio_pci_config_readb(QVirtioDevice * d,uint64_t off)421cf4323eSThomas Huth static uint8_t qvirtio_pci_config_readb(QVirtioDevice *d, uint64_t off)
431cf4323eSThomas Huth {
441cf4323eSThomas Huth     QVirtioPCIDevice *dev = container_of(d, QVirtioPCIDevice, vdev);
451cf4323eSThomas Huth     return qpci_io_readb(dev->pdev, dev->bar, CONFIG_BASE(dev) + off);
461cf4323eSThomas Huth }
471cf4323eSThomas Huth 
481cf4323eSThomas Huth /* PCI is always read in little-endian order
491cf4323eSThomas Huth  * but virtio ( < 1.0) is in guest order
501cf4323eSThomas Huth  * so with a big-endian guest the order has been reversed,
511cf4323eSThomas Huth  * reverse it again
521cf4323eSThomas Huth  * virtio-1.0 is always little-endian, like PCI
531cf4323eSThomas Huth  */
541cf4323eSThomas Huth 
qvirtio_pci_config_readw(QVirtioDevice * d,uint64_t off)551cf4323eSThomas Huth static uint16_t qvirtio_pci_config_readw(QVirtioDevice *d, uint64_t off)
561cf4323eSThomas Huth {
571cf4323eSThomas Huth     QVirtioPCIDevice *dev = container_of(d, QVirtioPCIDevice, vdev);
581cf4323eSThomas Huth     uint16_t value;
591cf4323eSThomas Huth 
601cf4323eSThomas Huth     value = qpci_io_readw(dev->pdev, dev->bar, CONFIG_BASE(dev) + off);
611cf4323eSThomas Huth     if (qvirtio_is_big_endian(d)) {
621cf4323eSThomas Huth         value = bswap16(value);
631cf4323eSThomas Huth     }
641cf4323eSThomas Huth     return value;
651cf4323eSThomas Huth }
661cf4323eSThomas Huth 
qvirtio_pci_config_readl(QVirtioDevice * d,uint64_t off)671cf4323eSThomas Huth static uint32_t qvirtio_pci_config_readl(QVirtioDevice *d, uint64_t off)
681cf4323eSThomas Huth {
691cf4323eSThomas Huth     QVirtioPCIDevice *dev = container_of(d, QVirtioPCIDevice, vdev);
701cf4323eSThomas Huth     uint32_t value;
711cf4323eSThomas Huth 
721cf4323eSThomas Huth     value = qpci_io_readl(dev->pdev, dev->bar, CONFIG_BASE(dev) + off);
731cf4323eSThomas Huth     if (qvirtio_is_big_endian(d)) {
741cf4323eSThomas Huth         value = bswap32(value);
751cf4323eSThomas Huth     }
761cf4323eSThomas Huth     return value;
771cf4323eSThomas Huth }
781cf4323eSThomas Huth 
qvirtio_pci_config_readq(QVirtioDevice * d,uint64_t off)791cf4323eSThomas Huth static uint64_t qvirtio_pci_config_readq(QVirtioDevice *d, uint64_t off)
801cf4323eSThomas Huth {
811cf4323eSThomas Huth     QVirtioPCIDevice *dev = container_of(d, QVirtioPCIDevice, vdev);
821cf4323eSThomas Huth     uint64_t val;
831cf4323eSThomas Huth 
841cf4323eSThomas Huth     val = qpci_io_readq(dev->pdev, dev->bar, CONFIG_BASE(dev) + off);
851cf4323eSThomas Huth     if (qvirtio_is_big_endian(d)) {
861cf4323eSThomas Huth         val = bswap64(val);
871cf4323eSThomas Huth     }
881cf4323eSThomas Huth 
891cf4323eSThomas Huth     return val;
901cf4323eSThomas Huth }
911cf4323eSThomas Huth 
qvirtio_pci_get_features(QVirtioDevice * d)921cf4323eSThomas Huth static uint64_t qvirtio_pci_get_features(QVirtioDevice *d)
931cf4323eSThomas Huth {
941cf4323eSThomas Huth     QVirtioPCIDevice *dev = container_of(d, QVirtioPCIDevice, vdev);
951cf4323eSThomas Huth     return qpci_io_readl(dev->pdev, dev->bar, VIRTIO_PCI_HOST_FEATURES);
961cf4323eSThomas Huth }
971cf4323eSThomas Huth 
qvirtio_pci_set_features(QVirtioDevice * d,uint64_t features)981cf4323eSThomas Huth static void qvirtio_pci_set_features(QVirtioDevice *d, uint64_t features)
991cf4323eSThomas Huth {
1001cf4323eSThomas Huth     QVirtioPCIDevice *dev = container_of(d, QVirtioPCIDevice, vdev);
1011cf4323eSThomas Huth     qpci_io_writel(dev->pdev, dev->bar, VIRTIO_PCI_GUEST_FEATURES, features);
1021cf4323eSThomas Huth }
1031cf4323eSThomas Huth 
qvirtio_pci_get_guest_features(QVirtioDevice * d)1041cf4323eSThomas Huth static uint64_t qvirtio_pci_get_guest_features(QVirtioDevice *d)
1051cf4323eSThomas Huth {
1061cf4323eSThomas Huth     QVirtioPCIDevice *dev = container_of(d, QVirtioPCIDevice, vdev);
1071cf4323eSThomas Huth     return qpci_io_readl(dev->pdev, dev->bar, VIRTIO_PCI_GUEST_FEATURES);
1081cf4323eSThomas Huth }
1091cf4323eSThomas Huth 
qvirtio_pci_get_status(QVirtioDevice * d)1101cf4323eSThomas Huth static uint8_t qvirtio_pci_get_status(QVirtioDevice *d)
1111cf4323eSThomas Huth {
1121cf4323eSThomas Huth     QVirtioPCIDevice *dev = container_of(d, QVirtioPCIDevice, vdev);
1131cf4323eSThomas Huth     return qpci_io_readb(dev->pdev, dev->bar, VIRTIO_PCI_STATUS);
1141cf4323eSThomas Huth }
1151cf4323eSThomas Huth 
qvirtio_pci_set_status(QVirtioDevice * d,uint8_t status)1161cf4323eSThomas Huth static void qvirtio_pci_set_status(QVirtioDevice *d, uint8_t status)
1171cf4323eSThomas Huth {
1181cf4323eSThomas Huth     QVirtioPCIDevice *dev = container_of(d, QVirtioPCIDevice, vdev);
1191cf4323eSThomas Huth     qpci_io_writeb(dev->pdev, dev->bar, VIRTIO_PCI_STATUS, status);
1201cf4323eSThomas Huth }
1211cf4323eSThomas Huth 
qvirtio_pci_get_queue_isr_status(QVirtioDevice * d,QVirtQueue * vq)1221cf4323eSThomas Huth static bool qvirtio_pci_get_queue_isr_status(QVirtioDevice *d, QVirtQueue *vq)
1231cf4323eSThomas Huth {
1241cf4323eSThomas Huth     QVirtioPCIDevice *dev = container_of(d, QVirtioPCIDevice, vdev);
1251cf4323eSThomas Huth     QVirtQueuePCI *vqpci = (QVirtQueuePCI *)vq;
1261cf4323eSThomas Huth     uint32_t data;
1271cf4323eSThomas Huth 
1281cf4323eSThomas Huth     if (dev->pdev->msix_enabled) {
1291cf4323eSThomas Huth         g_assert_cmpint(vqpci->msix_entry, !=, -1);
1301cf4323eSThomas Huth         if (qpci_msix_masked(dev->pdev, vqpci->msix_entry)) {
1311cf4323eSThomas Huth             /* No ISR checking should be done if masked, but read anyway */
1321cf4323eSThomas Huth             return qpci_msix_pending(dev->pdev, vqpci->msix_entry);
1331cf4323eSThomas Huth         } else {
1341cf4323eSThomas Huth             data = qtest_readl(dev->pdev->bus->qts, vqpci->msix_addr);
1351cf4323eSThomas Huth             if (data == vqpci->msix_data) {
1361cf4323eSThomas Huth                 qtest_writel(dev->pdev->bus->qts, vqpci->msix_addr, 0);
1371cf4323eSThomas Huth                 return true;
1381cf4323eSThomas Huth             } else {
1391cf4323eSThomas Huth                 return false;
1401cf4323eSThomas Huth             }
1411cf4323eSThomas Huth         }
1421cf4323eSThomas Huth     } else {
1431cf4323eSThomas Huth         return qpci_io_readb(dev->pdev, dev->bar, VIRTIO_PCI_ISR) & 1;
1441cf4323eSThomas Huth     }
1451cf4323eSThomas Huth }
1461cf4323eSThomas Huth 
qvirtio_pci_get_config_isr_status(QVirtioDevice * d)1471cf4323eSThomas Huth static bool qvirtio_pci_get_config_isr_status(QVirtioDevice *d)
1481cf4323eSThomas Huth {
1491cf4323eSThomas Huth     QVirtioPCIDevice *dev = container_of(d, QVirtioPCIDevice, vdev);
1501cf4323eSThomas Huth     uint32_t data;
1511cf4323eSThomas Huth 
1521cf4323eSThomas Huth     if (dev->pdev->msix_enabled) {
1531cf4323eSThomas Huth         g_assert_cmpint(dev->config_msix_entry, !=, -1);
1541cf4323eSThomas Huth         if (qpci_msix_masked(dev->pdev, dev->config_msix_entry)) {
1551cf4323eSThomas Huth             /* No ISR checking should be done if masked, but read anyway */
1561cf4323eSThomas Huth             return qpci_msix_pending(dev->pdev, dev->config_msix_entry);
1571cf4323eSThomas Huth         } else {
1581cf4323eSThomas Huth             data = qtest_readl(dev->pdev->bus->qts, dev->config_msix_addr);
1591cf4323eSThomas Huth             if (data == dev->config_msix_data) {
1601cf4323eSThomas Huth                 qtest_writel(dev->pdev->bus->qts, dev->config_msix_addr, 0);
1611cf4323eSThomas Huth                 return true;
1621cf4323eSThomas Huth             } else {
1631cf4323eSThomas Huth                 return false;
1641cf4323eSThomas Huth             }
1651cf4323eSThomas Huth         }
1661cf4323eSThomas Huth     } else {
1671cf4323eSThomas Huth         return qpci_io_readb(dev->pdev, dev->bar, VIRTIO_PCI_ISR) & 2;
1681cf4323eSThomas Huth     }
1691cf4323eSThomas Huth }
1701cf4323eSThomas Huth 
qvirtio_pci_wait_config_isr_status(QVirtioDevice * d,gint64 timeout_us)1711cf4323eSThomas Huth static void qvirtio_pci_wait_config_isr_status(QVirtioDevice *d,
1721cf4323eSThomas Huth                                                gint64 timeout_us)
1731cf4323eSThomas Huth {
1741cf4323eSThomas Huth     QVirtioPCIDevice *dev = container_of(d, QVirtioPCIDevice, vdev);
1751cf4323eSThomas Huth     gint64 start_time = g_get_monotonic_time();
1761cf4323eSThomas Huth 
1771cf4323eSThomas Huth     do {
1781cf4323eSThomas Huth         g_assert(g_get_monotonic_time() - start_time <= timeout_us);
1791cf4323eSThomas Huth         qtest_clock_step(dev->pdev->bus->qts, 100);
1801cf4323eSThomas Huth     } while (!qvirtio_pci_get_config_isr_status(d));
1811cf4323eSThomas Huth }
1821cf4323eSThomas Huth 
qvirtio_pci_queue_select(QVirtioDevice * d,uint16_t index)1831cf4323eSThomas Huth static void qvirtio_pci_queue_select(QVirtioDevice *d, uint16_t index)
1841cf4323eSThomas Huth {
1851cf4323eSThomas Huth     QVirtioPCIDevice *dev = container_of(d, QVirtioPCIDevice, vdev);
1861cf4323eSThomas Huth     qpci_io_writeb(dev->pdev, dev->bar, VIRTIO_PCI_QUEUE_SEL, index);
1871cf4323eSThomas Huth }
1881cf4323eSThomas Huth 
qvirtio_pci_get_queue_size(QVirtioDevice * d)1891cf4323eSThomas Huth static uint16_t qvirtio_pci_get_queue_size(QVirtioDevice *d)
1901cf4323eSThomas Huth {
1911cf4323eSThomas Huth     QVirtioPCIDevice *dev = container_of(d, QVirtioPCIDevice, vdev);
1921cf4323eSThomas Huth     return qpci_io_readw(dev->pdev, dev->bar, VIRTIO_PCI_QUEUE_NUM);
1931cf4323eSThomas Huth }
1941cf4323eSThomas Huth 
qvirtio_pci_set_queue_address(QVirtioDevice * d,QVirtQueue * vq)1951cf4323eSThomas Huth static void qvirtio_pci_set_queue_address(QVirtioDevice *d, QVirtQueue *vq)
1961cf4323eSThomas Huth {
1971cf4323eSThomas Huth     QVirtioPCIDevice *dev = container_of(d, QVirtioPCIDevice, vdev);
1981cf4323eSThomas Huth     uint64_t pfn = vq->desc / VIRTIO_PCI_VRING_ALIGN;
1991cf4323eSThomas Huth 
2001cf4323eSThomas Huth     qpci_io_writel(dev->pdev, dev->bar, VIRTIO_PCI_QUEUE_PFN, pfn);
2011cf4323eSThomas Huth }
2021cf4323eSThomas Huth 
qvirtio_pci_virtqueue_setup_common(QVirtioDevice * d,QGuestAllocator * alloc,uint16_t index)2031cf4323eSThomas Huth QVirtQueue *qvirtio_pci_virtqueue_setup_common(QVirtioDevice *d,
2041cf4323eSThomas Huth                                                QGuestAllocator *alloc,
2051cf4323eSThomas Huth                                                uint16_t index)
2061cf4323eSThomas Huth {
2071cf4323eSThomas Huth     uint64_t feat;
2081cf4323eSThomas Huth     uint64_t addr;
2091cf4323eSThomas Huth     QVirtQueuePCI *vqpci;
2101cf4323eSThomas Huth     QVirtioPCIDevice *qvpcidev = container_of(d, QVirtioPCIDevice, vdev);
2111cf4323eSThomas Huth 
2121cf4323eSThomas Huth     vqpci = g_malloc0(sizeof(*vqpci));
2131cf4323eSThomas Huth     feat = d->bus->get_guest_features(d);
2141cf4323eSThomas Huth 
2151cf4323eSThomas Huth     d->bus->queue_select(d, index);
2161cf4323eSThomas Huth     vqpci->vq.vdev = d;
2171cf4323eSThomas Huth     vqpci->vq.index = index;
2181cf4323eSThomas Huth     vqpci->vq.size = d->bus->get_queue_size(d);
2191cf4323eSThomas Huth     vqpci->vq.free_head = 0;
2201cf4323eSThomas Huth     vqpci->vq.num_free = vqpci->vq.size;
2211cf4323eSThomas Huth     vqpci->vq.align = VIRTIO_PCI_VRING_ALIGN;
2221cf4323eSThomas Huth     vqpci->vq.indirect = feat & (1ull << VIRTIO_RING_F_INDIRECT_DESC);
2231cf4323eSThomas Huth     vqpci->vq.event = feat & (1ull << VIRTIO_RING_F_EVENT_IDX);
2241cf4323eSThomas Huth 
2251cf4323eSThomas Huth     vqpci->msix_entry = -1;
2261cf4323eSThomas Huth     vqpci->msix_addr = 0;
2271cf4323eSThomas Huth     vqpci->msix_data = 0x12345678;
2281cf4323eSThomas Huth 
2291cf4323eSThomas Huth     /* Check different than 0 */
2301cf4323eSThomas Huth     g_assert_cmpint(vqpci->vq.size, !=, 0);
2311cf4323eSThomas Huth 
2321cf4323eSThomas Huth     /* Check power of 2 */
2331cf4323eSThomas Huth     g_assert_cmpint(vqpci->vq.size & (vqpci->vq.size - 1), ==, 0);
2341cf4323eSThomas Huth 
2351cf4323eSThomas Huth     addr = guest_alloc(alloc, qvring_size(vqpci->vq.size,
2361cf4323eSThomas Huth                                           VIRTIO_PCI_VRING_ALIGN));
2371cf4323eSThomas Huth     qvring_init(qvpcidev->pdev->bus->qts, alloc, &vqpci->vq, addr);
2381cf4323eSThomas Huth     d->bus->set_queue_address(d, &vqpci->vq);
2391cf4323eSThomas Huth 
2401cf4323eSThomas Huth     return &vqpci->vq;
2411cf4323eSThomas Huth }
2421cf4323eSThomas Huth 
qvirtio_pci_virtqueue_cleanup_common(QVirtQueue * vq,QGuestAllocator * alloc)2431cf4323eSThomas Huth void qvirtio_pci_virtqueue_cleanup_common(QVirtQueue *vq,
2441cf4323eSThomas Huth                                           QGuestAllocator *alloc)
2451cf4323eSThomas Huth {
2461cf4323eSThomas Huth     QVirtQueuePCI *vqpci = container_of(vq, QVirtQueuePCI, vq);
2471cf4323eSThomas Huth 
2481cf4323eSThomas Huth     guest_free(alloc, vq->desc);
2491cf4323eSThomas Huth     g_free(vqpci);
2501cf4323eSThomas Huth }
2511cf4323eSThomas Huth 
qvirtio_pci_virtqueue_kick(QVirtioDevice * d,QVirtQueue * vq)2521cf4323eSThomas Huth static void qvirtio_pci_virtqueue_kick(QVirtioDevice *d, QVirtQueue *vq)
2531cf4323eSThomas Huth {
2541cf4323eSThomas Huth     QVirtioPCIDevice *dev = container_of(d, QVirtioPCIDevice, vdev);
2551cf4323eSThomas Huth     qpci_io_writew(dev->pdev, dev->bar, VIRTIO_PCI_QUEUE_NOTIFY, vq->index);
2561cf4323eSThomas Huth }
2571cf4323eSThomas Huth 
2581cf4323eSThomas Huth static const QVirtioBus qvirtio_pci_legacy = {
2591cf4323eSThomas Huth     .config_readb = qvirtio_pci_config_readb,
2601cf4323eSThomas Huth     .config_readw = qvirtio_pci_config_readw,
2611cf4323eSThomas Huth     .config_readl = qvirtio_pci_config_readl,
2621cf4323eSThomas Huth     .config_readq = qvirtio_pci_config_readq,
2631cf4323eSThomas Huth     .get_features = qvirtio_pci_get_features,
2641cf4323eSThomas Huth     .set_features = qvirtio_pci_set_features,
2651cf4323eSThomas Huth     .get_guest_features = qvirtio_pci_get_guest_features,
2661cf4323eSThomas Huth     .get_status = qvirtio_pci_get_status,
2671cf4323eSThomas Huth     .set_status = qvirtio_pci_set_status,
2681cf4323eSThomas Huth     .get_queue_isr_status = qvirtio_pci_get_queue_isr_status,
2691cf4323eSThomas Huth     .wait_config_isr_status = qvirtio_pci_wait_config_isr_status,
2701cf4323eSThomas Huth     .queue_select = qvirtio_pci_queue_select,
2711cf4323eSThomas Huth     .get_queue_size = qvirtio_pci_get_queue_size,
2721cf4323eSThomas Huth     .set_queue_address = qvirtio_pci_set_queue_address,
2731cf4323eSThomas Huth     .virtqueue_setup = qvirtio_pci_virtqueue_setup_common,
2741cf4323eSThomas Huth     .virtqueue_cleanup = qvirtio_pci_virtqueue_cleanup_common,
2751cf4323eSThomas Huth     .virtqueue_kick = qvirtio_pci_virtqueue_kick,
2761cf4323eSThomas Huth };
2771cf4323eSThomas Huth 
qvirtio_pci_set_config_vector(QVirtioPCIDevice * d,uint16_t entry)2781cf4323eSThomas Huth static void qvirtio_pci_set_config_vector(QVirtioPCIDevice *d, uint16_t entry)
2791cf4323eSThomas Huth {
2801cf4323eSThomas Huth     uint16_t vector;
2811cf4323eSThomas Huth 
2821cf4323eSThomas Huth     qpci_io_writew(d->pdev, d->bar, VIRTIO_MSI_CONFIG_VECTOR, entry);
2831cf4323eSThomas Huth     vector = qpci_io_readw(d->pdev, d->bar, VIRTIO_MSI_CONFIG_VECTOR);
2841cf4323eSThomas Huth     g_assert_cmphex(vector, !=, VIRTIO_MSI_NO_VECTOR);
2851cf4323eSThomas Huth }
2861cf4323eSThomas Huth 
qvirtio_pci_set_queue_vector(QVirtioPCIDevice * d,uint16_t vq_idx,uint16_t entry)2871cf4323eSThomas Huth static void qvirtio_pci_set_queue_vector(QVirtioPCIDevice *d, uint16_t vq_idx,
2881cf4323eSThomas Huth                                          uint16_t entry)
2891cf4323eSThomas Huth {
2901cf4323eSThomas Huth     uint16_t vector;
2911cf4323eSThomas Huth 
2921cf4323eSThomas Huth     qvirtio_pci_queue_select(&d->vdev, vq_idx);
2931cf4323eSThomas Huth     qpci_io_writew(d->pdev, d->bar, VIRTIO_MSI_QUEUE_VECTOR, entry);
2941cf4323eSThomas Huth     vector = qpci_io_readw(d->pdev, d->bar, VIRTIO_MSI_QUEUE_VECTOR);
2951cf4323eSThomas Huth     g_assert_cmphex(vector, !=, VIRTIO_MSI_NO_VECTOR);
2961cf4323eSThomas Huth }
2971cf4323eSThomas Huth 
2981cf4323eSThomas Huth static const QVirtioPCIMSIXOps qvirtio_pci_msix_ops_legacy = {
2991cf4323eSThomas Huth     .set_config_vector = qvirtio_pci_set_config_vector,
3001cf4323eSThomas Huth     .set_queue_vector = qvirtio_pci_set_queue_vector,
3011cf4323eSThomas Huth };
3021cf4323eSThomas Huth 
qvirtio_pci_device_enable(QVirtioPCIDevice * d)3031cf4323eSThomas Huth void qvirtio_pci_device_enable(QVirtioPCIDevice *d)
3041cf4323eSThomas Huth {
3051cf4323eSThomas Huth     qpci_device_enable(d->pdev);
3061cf4323eSThomas Huth     d->bar = qpci_iomap(d->pdev, d->bar_idx, NULL);
3071cf4323eSThomas Huth }
3081cf4323eSThomas Huth 
qvirtio_pci_device_disable(QVirtioPCIDevice * d)3091cf4323eSThomas Huth void qvirtio_pci_device_disable(QVirtioPCIDevice *d)
3101cf4323eSThomas Huth {
3111cf4323eSThomas Huth     qpci_iounmap(d->pdev, d->bar);
3121cf4323eSThomas Huth }
3131cf4323eSThomas Huth 
qvirtqueue_pci_msix_setup(QVirtioPCIDevice * d,QVirtQueuePCI * vqpci,QGuestAllocator * alloc,uint16_t entry)3141cf4323eSThomas Huth void qvirtqueue_pci_msix_setup(QVirtioPCIDevice *d, QVirtQueuePCI *vqpci,
3151cf4323eSThomas Huth                                         QGuestAllocator *alloc, uint16_t entry)
3161cf4323eSThomas Huth {
3171cf4323eSThomas Huth     uint32_t control;
3181cf4323eSThomas Huth     uint64_t off;
3191cf4323eSThomas Huth 
3201cf4323eSThomas Huth     g_assert(d->pdev->msix_enabled);
3211cf4323eSThomas Huth     off = d->pdev->msix_table_off + (entry * 16);
3221cf4323eSThomas Huth 
3231cf4323eSThomas Huth     g_assert_cmpint(entry, >=, 0);
3241cf4323eSThomas Huth     g_assert_cmpint(entry, <, qpci_msix_table_size(d->pdev));
3251cf4323eSThomas Huth     vqpci->msix_entry = entry;
3261cf4323eSThomas Huth 
3271cf4323eSThomas Huth     vqpci->msix_addr = guest_alloc(alloc, 4);
3281cf4323eSThomas Huth     qpci_io_writel(d->pdev, d->pdev->msix_table_bar,
3291cf4323eSThomas Huth                    off + PCI_MSIX_ENTRY_LOWER_ADDR, vqpci->msix_addr & ~0UL);
3301cf4323eSThomas Huth     qpci_io_writel(d->pdev, d->pdev->msix_table_bar,
3311cf4323eSThomas Huth                    off + PCI_MSIX_ENTRY_UPPER_ADDR,
3321cf4323eSThomas Huth                    (vqpci->msix_addr >> 32) & ~0UL);
3331cf4323eSThomas Huth     qpci_io_writel(d->pdev, d->pdev->msix_table_bar,
3341cf4323eSThomas Huth                    off + PCI_MSIX_ENTRY_DATA, vqpci->msix_data);
3351cf4323eSThomas Huth 
3361cf4323eSThomas Huth     control = qpci_io_readl(d->pdev, d->pdev->msix_table_bar,
3371cf4323eSThomas Huth                             off + PCI_MSIX_ENTRY_VECTOR_CTRL);
3381cf4323eSThomas Huth     qpci_io_writel(d->pdev, d->pdev->msix_table_bar,
3391cf4323eSThomas Huth                    off + PCI_MSIX_ENTRY_VECTOR_CTRL,
3401cf4323eSThomas Huth                    control & ~PCI_MSIX_ENTRY_CTRL_MASKBIT);
3411cf4323eSThomas Huth 
3421cf4323eSThomas Huth     d->msix_ops->set_queue_vector(d, vqpci->vq.index, entry);
3431cf4323eSThomas Huth }
3441cf4323eSThomas Huth 
qvirtio_pci_set_msix_configuration_vector(QVirtioPCIDevice * d,QGuestAllocator * alloc,uint16_t entry)3451cf4323eSThomas Huth void qvirtio_pci_set_msix_configuration_vector(QVirtioPCIDevice *d,
3461cf4323eSThomas Huth                                         QGuestAllocator *alloc, uint16_t entry)
3471cf4323eSThomas Huth {
3481cf4323eSThomas Huth     uint32_t control;
3491cf4323eSThomas Huth     uint64_t off;
3501cf4323eSThomas Huth 
3511cf4323eSThomas Huth     g_assert(d->pdev->msix_enabled);
3521cf4323eSThomas Huth     off = d->pdev->msix_table_off + (entry * 16);
3531cf4323eSThomas Huth 
3541cf4323eSThomas Huth     g_assert_cmpint(entry, >=, 0);
3551cf4323eSThomas Huth     g_assert_cmpint(entry, <, qpci_msix_table_size(d->pdev));
3561cf4323eSThomas Huth     d->config_msix_entry = entry;
3571cf4323eSThomas Huth 
3581cf4323eSThomas Huth     d->config_msix_data = 0x12345678;
3591cf4323eSThomas Huth     d->config_msix_addr = guest_alloc(alloc, 4);
3601cf4323eSThomas Huth 
3611cf4323eSThomas Huth     qpci_io_writel(d->pdev, d->pdev->msix_table_bar,
3621cf4323eSThomas Huth                    off + PCI_MSIX_ENTRY_LOWER_ADDR, d->config_msix_addr & ~0UL);
3631cf4323eSThomas Huth     qpci_io_writel(d->pdev, d->pdev->msix_table_bar,
3641cf4323eSThomas Huth                    off + PCI_MSIX_ENTRY_UPPER_ADDR,
3651cf4323eSThomas Huth                    (d->config_msix_addr >> 32) & ~0UL);
3661cf4323eSThomas Huth     qpci_io_writel(d->pdev, d->pdev->msix_table_bar,
3671cf4323eSThomas Huth                    off + PCI_MSIX_ENTRY_DATA, d->config_msix_data);
3681cf4323eSThomas Huth 
3691cf4323eSThomas Huth     control = qpci_io_readl(d->pdev, d->pdev->msix_table_bar,
3701cf4323eSThomas Huth                             off + PCI_MSIX_ENTRY_VECTOR_CTRL);
3711cf4323eSThomas Huth     qpci_io_writel(d->pdev, d->pdev->msix_table_bar,
3721cf4323eSThomas Huth                    off + PCI_MSIX_ENTRY_VECTOR_CTRL,
3731cf4323eSThomas Huth                    control & ~PCI_MSIX_ENTRY_CTRL_MASKBIT);
3741cf4323eSThomas Huth 
3751cf4323eSThomas Huth     d->msix_ops->set_config_vector(d, entry);
3761cf4323eSThomas Huth }
3771cf4323eSThomas Huth 
qvirtio_pci_destructor(QOSGraphObject * obj)3781cf4323eSThomas Huth void qvirtio_pci_destructor(QOSGraphObject *obj)
3791cf4323eSThomas Huth {
3801cf4323eSThomas Huth     QVirtioPCIDevice *dev = (QVirtioPCIDevice *)obj;
3811cf4323eSThomas Huth     qvirtio_pci_device_disable(dev);
3821cf4323eSThomas Huth     g_free(dev->pdev);
3831cf4323eSThomas Huth }
3841cf4323eSThomas Huth 
qvirtio_pci_start_hw(QOSGraphObject * obj)3851cf4323eSThomas Huth void qvirtio_pci_start_hw(QOSGraphObject *obj)
3861cf4323eSThomas Huth {
3871cf4323eSThomas Huth     QVirtioPCIDevice *dev = (QVirtioPCIDevice *)obj;
3881cf4323eSThomas Huth     qvirtio_pci_device_enable(dev);
3891cf4323eSThomas Huth     qvirtio_start_device(&dev->vdev);
3901cf4323eSThomas Huth }
3911cf4323eSThomas Huth 
qvirtio_pci_init_legacy(QVirtioPCIDevice * dev)3921cf4323eSThomas Huth static void qvirtio_pci_init_legacy(QVirtioPCIDevice *dev)
3931cf4323eSThomas Huth {
3941cf4323eSThomas Huth     dev->vdev.device_type = qpci_config_readw(dev->pdev, PCI_SUBSYSTEM_ID);
3951cf4323eSThomas Huth     dev->bar_idx = 0;
3961cf4323eSThomas Huth     dev->vdev.bus = &qvirtio_pci_legacy;
3971cf4323eSThomas Huth     dev->msix_ops = &qvirtio_pci_msix_ops_legacy;
3981cf4323eSThomas Huth     dev->vdev.big_endian = qtest_big_endian(dev->pdev->bus->qts);
3991cf4323eSThomas Huth }
4001cf4323eSThomas Huth 
qvirtio_pci_init_from_pcidev(QVirtioPCIDevice * dev,QPCIDevice * pci_dev)4011cf4323eSThomas Huth static void qvirtio_pci_init_from_pcidev(QVirtioPCIDevice *dev, QPCIDevice *pci_dev)
4021cf4323eSThomas Huth {
4031cf4323eSThomas Huth     dev->pdev = pci_dev;
4041cf4323eSThomas Huth     dev->config_msix_entry = -1;
4051cf4323eSThomas Huth 
4061cf4323eSThomas Huth     if (!qvirtio_pci_init_virtio_1(dev)) {
4071cf4323eSThomas Huth         qvirtio_pci_init_legacy(dev);
4081cf4323eSThomas Huth     }
4091cf4323eSThomas Huth 
4101cf4323eSThomas Huth     /* each virtio-xxx-pci device should override at least this function */
4111cf4323eSThomas Huth     dev->obj.get_driver = NULL;
4121cf4323eSThomas Huth     dev->obj.start_hw = qvirtio_pci_start_hw;
4131cf4323eSThomas Huth     dev->obj.destructor = qvirtio_pci_destructor;
4141cf4323eSThomas Huth }
4151cf4323eSThomas Huth 
virtio_pci_init(QVirtioPCIDevice * dev,QPCIBus * bus,QPCIAddress * addr)4161cf4323eSThomas Huth void virtio_pci_init(QVirtioPCIDevice *dev, QPCIBus *bus, QPCIAddress * addr)
4171cf4323eSThomas Huth {
4181cf4323eSThomas Huth     QPCIDevice *pci_dev = qpci_device_find(bus, addr->devfn);
4191cf4323eSThomas Huth     g_assert_nonnull(pci_dev);
4201cf4323eSThomas Huth     qvirtio_pci_init_from_pcidev(dev, pci_dev);
4211cf4323eSThomas Huth }
4221cf4323eSThomas Huth 
virtio_pci_new(QPCIBus * bus,QPCIAddress * addr)4231cf4323eSThomas Huth QVirtioPCIDevice *virtio_pci_new(QPCIBus *bus, QPCIAddress * addr)
4241cf4323eSThomas Huth {
4251cf4323eSThomas Huth     QVirtioPCIDevice *dev;
4261cf4323eSThomas Huth     QPCIDevice *pci_dev = qpci_device_find(bus, addr->devfn);
4271cf4323eSThomas Huth     if (!pci_dev) {
4281cf4323eSThomas Huth         return NULL;
4291cf4323eSThomas Huth     }
4301cf4323eSThomas Huth 
4311cf4323eSThomas Huth     dev = g_new0(QVirtioPCIDevice, 1);
4321cf4323eSThomas Huth     qvirtio_pci_init_from_pcidev(dev, pci_dev);
4331cf4323eSThomas Huth     dev->obj.free = g_free;
4341cf4323eSThomas Huth     return dev;
4351cf4323eSThomas Huth }
436