2015-02-25 00:21:55 +03:00
|
|
|
/*
|
|
|
|
* libqos virtio MMIO driver
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Marc Marí
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
2016-02-08 21:08:51 +03:00
|
|
|
#include "qemu/osdep.h"
|
2022-03-30 12:39:05 +03:00
|
|
|
#include "../libqtest.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2020-08-04 21:00:40 +03:00
|
|
|
#include "virtio.h"
|
|
|
|
#include "virtio-mmio.h"
|
2022-08-24 12:40:03 +03:00
|
|
|
#include "libqos-malloc.h"
|
2020-08-04 21:00:40 +03:00
|
|
|
#include "qgraph.h"
|
2016-05-09 15:47:37 +03:00
|
|
|
#include "standard-headers/linux/virtio_ring.h"
|
2015-02-25 00:21:55 +03:00
|
|
|
|
2016-10-20 06:08:07 +03:00
|
|
|
static uint8_t qvirtio_mmio_config_readb(QVirtioDevice *d, uint64_t off)
|
2015-02-25 00:21:55 +03:00
|
|
|
{
|
2018-07-19 16:14:44 +03:00
|
|
|
QVirtioMMIODevice *dev = container_of(d, QVirtioMMIODevice, vdev);
|
|
|
|
return qtest_readb(dev->qts, dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
|
2015-02-25 00:21:55 +03:00
|
|
|
}
|
|
|
|
|
2016-10-20 06:08:07 +03:00
|
|
|
static uint16_t qvirtio_mmio_config_readw(QVirtioDevice *d, uint64_t off)
|
2015-02-25 00:21:55 +03:00
|
|
|
{
|
2018-07-19 16:14:44 +03:00
|
|
|
QVirtioMMIODevice *dev = container_of(d, QVirtioMMIODevice, vdev);
|
|
|
|
return qtest_readw(dev->qts, dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
|
2015-02-25 00:21:55 +03:00
|
|
|
}
|
|
|
|
|
2016-10-20 06:08:07 +03:00
|
|
|
static uint32_t qvirtio_mmio_config_readl(QVirtioDevice *d, uint64_t off)
|
2015-02-25 00:21:55 +03:00
|
|
|
{
|
2018-07-19 16:14:44 +03:00
|
|
|
QVirtioMMIODevice *dev = container_of(d, QVirtioMMIODevice, vdev);
|
|
|
|
return qtest_readl(dev->qts, dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
|
2015-02-25 00:21:55 +03:00
|
|
|
}
|
|
|
|
|
2016-10-20 06:08:07 +03:00
|
|
|
static uint64_t qvirtio_mmio_config_readq(QVirtioDevice *d, uint64_t off)
|
2015-02-25 00:21:55 +03:00
|
|
|
{
|
2018-07-19 16:14:44 +03:00
|
|
|
QVirtioMMIODevice *dev = container_of(d, QVirtioMMIODevice, vdev);
|
|
|
|
return qtest_readq(dev->qts, dev->addr + QVIRTIO_MMIO_DEVICE_SPECIFIC + off);
|
2015-02-25 00:21:55 +03:00
|
|
|
}
|
|
|
|
|
2019-10-23 13:04:12 +03:00
|
|
|
static uint64_t qvirtio_mmio_get_features(QVirtioDevice *d)
|
2015-02-25 00:21:55 +03:00
|
|
|
{
|
2018-07-19 16:14:44 +03:00
|
|
|
QVirtioMMIODevice *dev = container_of(d, QVirtioMMIODevice, vdev);
|
2019-10-23 13:04:12 +03:00
|
|
|
uint64_t lo;
|
|
|
|
uint64_t hi = 0;
|
|
|
|
|
2018-07-19 16:14:44 +03:00
|
|
|
qtest_writel(dev->qts, dev->addr + QVIRTIO_MMIO_HOST_FEATURES_SEL, 0);
|
2019-10-23 13:04:12 +03:00
|
|
|
lo = qtest_readl(dev->qts, dev->addr + QVIRTIO_MMIO_HOST_FEATURES);
|
|
|
|
|
|
|
|
if (dev->version >= 2) {
|
|
|
|
qtest_writel(dev->qts, dev->addr + QVIRTIO_MMIO_HOST_FEATURES_SEL, 1);
|
|
|
|
hi = qtest_readl(dev->qts, dev->addr + QVIRTIO_MMIO_HOST_FEATURES);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (hi << 32) | lo;
|
2015-02-25 00:21:55 +03:00
|
|
|
}
|
|
|
|
|
2019-10-23 13:04:12 +03:00
|
|
|
static void qvirtio_mmio_set_features(QVirtioDevice *d, uint64_t features)
|
2015-02-25 00:21:55 +03:00
|
|
|
{
|
2018-07-19 16:14:44 +03:00
|
|
|
QVirtioMMIODevice *dev = container_of(d, QVirtioMMIODevice, vdev);
|
2015-02-25 00:21:55 +03:00
|
|
|
dev->features = features;
|
2018-07-19 16:14:44 +03:00
|
|
|
qtest_writel(dev->qts, dev->addr + QVIRTIO_MMIO_GUEST_FEATURES_SEL, 0);
|
|
|
|
qtest_writel(dev->qts, dev->addr + QVIRTIO_MMIO_GUEST_FEATURES, features);
|
2019-10-23 13:04:12 +03:00
|
|
|
|
|
|
|
if (dev->version >= 2) {
|
|
|
|
qtest_writel(dev->qts, dev->addr + QVIRTIO_MMIO_GUEST_FEATURES_SEL, 1);
|
|
|
|
qtest_writel(dev->qts, dev->addr + QVIRTIO_MMIO_GUEST_FEATURES,
|
|
|
|
features >> 32);
|
|
|
|
}
|
2015-02-25 00:21:55 +03:00
|
|
|
}
|
|
|
|
|
2019-10-23 13:04:12 +03:00
|
|
|
static uint64_t qvirtio_mmio_get_guest_features(QVirtioDevice *d)
|
2015-02-25 00:21:55 +03:00
|
|
|
{
|
2018-07-19 16:14:44 +03:00
|
|
|
QVirtioMMIODevice *dev = container_of(d, QVirtioMMIODevice, vdev);
|
2015-02-25 00:21:55 +03:00
|
|
|
return dev->features;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t qvirtio_mmio_get_status(QVirtioDevice *d)
|
|
|
|
{
|
2018-07-19 16:14:44 +03:00
|
|
|
QVirtioMMIODevice *dev = container_of(d, QVirtioMMIODevice, vdev);
|
|
|
|
return (uint8_t)qtest_readl(dev->qts, dev->addr + QVIRTIO_MMIO_DEVICE_STATUS);
|
2015-02-25 00:21:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qvirtio_mmio_set_status(QVirtioDevice *d, uint8_t status)
|
|
|
|
{
|
2018-07-19 16:14:44 +03:00
|
|
|
QVirtioMMIODevice *dev = container_of(d, QVirtioMMIODevice, vdev);
|
|
|
|
qtest_writel(dev->qts, dev->addr + QVIRTIO_MMIO_DEVICE_STATUS, (uint32_t)status);
|
2015-02-25 00:21:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool qvirtio_mmio_get_queue_isr_status(QVirtioDevice *d, QVirtQueue *vq)
|
|
|
|
{
|
2018-07-19 16:14:44 +03:00
|
|
|
QVirtioMMIODevice *dev = container_of(d, QVirtioMMIODevice, vdev);
|
2015-02-25 00:21:55 +03:00
|
|
|
uint32_t isr;
|
|
|
|
|
2018-07-19 16:14:44 +03:00
|
|
|
isr = qtest_readl(dev->qts, dev->addr + QVIRTIO_MMIO_INTERRUPT_STATUS) & 1;
|
2015-02-25 00:21:55 +03:00
|
|
|
if (isr != 0) {
|
2018-07-19 16:14:44 +03:00
|
|
|
qtest_writel(dev->qts, dev->addr + QVIRTIO_MMIO_INTERRUPT_ACK, 1);
|
2015-02-25 00:21:55 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool qvirtio_mmio_get_config_isr_status(QVirtioDevice *d)
|
|
|
|
{
|
2018-07-19 16:14:44 +03:00
|
|
|
QVirtioMMIODevice *dev = container_of(d, QVirtioMMIODevice, vdev);
|
2015-02-25 00:21:55 +03:00
|
|
|
uint32_t isr;
|
|
|
|
|
2018-07-19 16:14:44 +03:00
|
|
|
isr = qtest_readl(dev->qts, dev->addr + QVIRTIO_MMIO_INTERRUPT_STATUS) & 2;
|
2015-02-25 00:21:55 +03:00
|
|
|
if (isr != 0) {
|
2018-07-19 16:14:44 +03:00
|
|
|
qtest_writel(dev->qts, dev->addr + QVIRTIO_MMIO_INTERRUPT_ACK, 2);
|
2015-02-25 00:21:55 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-03 09:18:46 +03:00
|
|
|
static void qvirtio_mmio_wait_config_isr_status(QVirtioDevice *d,
|
|
|
|
gint64 timeout_us)
|
|
|
|
{
|
|
|
|
QVirtioMMIODevice *dev = container_of(d, QVirtioMMIODevice, vdev);
|
|
|
|
gint64 start_time = g_get_monotonic_time();
|
|
|
|
|
|
|
|
do {
|
|
|
|
g_assert(g_get_monotonic_time() - start_time <= timeout_us);
|
|
|
|
qtest_clock_step(dev->qts, 100);
|
|
|
|
} while (!qvirtio_mmio_get_config_isr_status(d));
|
|
|
|
}
|
|
|
|
|
2015-02-25 00:21:55 +03:00
|
|
|
static void qvirtio_mmio_queue_select(QVirtioDevice *d, uint16_t index)
|
|
|
|
{
|
2018-07-19 16:14:44 +03:00
|
|
|
QVirtioMMIODevice *dev = container_of(d, QVirtioMMIODevice, vdev);
|
|
|
|
qtest_writel(dev->qts, dev->addr + QVIRTIO_MMIO_QUEUE_SEL, (uint32_t)index);
|
2015-02-25 00:21:55 +03:00
|
|
|
|
2018-07-19 16:14:44 +03:00
|
|
|
g_assert_cmphex(qtest_readl(dev->qts, dev->addr + QVIRTIO_MMIO_QUEUE_PFN), ==, 0);
|
2015-02-25 00:21:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t qvirtio_mmio_get_queue_size(QVirtioDevice *d)
|
|
|
|
{
|
2018-07-19 16:14:44 +03:00
|
|
|
QVirtioMMIODevice *dev = container_of(d, QVirtioMMIODevice, vdev);
|
|
|
|
return (uint16_t)qtest_readl(dev->qts, dev->addr + QVIRTIO_MMIO_QUEUE_NUM_MAX);
|
2015-02-25 00:21:55 +03:00
|
|
|
}
|
|
|
|
|
2019-10-23 13:04:20 +03:00
|
|
|
static void qvirtio_mmio_set_queue_address(QVirtioDevice *d, QVirtQueue *vq)
|
2015-02-25 00:21:55 +03:00
|
|
|
{
|
2018-07-19 16:14:44 +03:00
|
|
|
QVirtioMMIODevice *dev = container_of(d, QVirtioMMIODevice, vdev);
|
2019-10-23 13:04:20 +03:00
|
|
|
uint64_t pfn = vq->desc / dev->page_size;
|
|
|
|
|
2018-07-19 16:14:44 +03:00
|
|
|
qtest_writel(dev->qts, dev->addr + QVIRTIO_MMIO_QUEUE_PFN, pfn);
|
2015-02-25 00:21:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static QVirtQueue *qvirtio_mmio_virtqueue_setup(QVirtioDevice *d,
|
|
|
|
QGuestAllocator *alloc, uint16_t index)
|
|
|
|
{
|
2018-07-19 16:14:44 +03:00
|
|
|
QVirtioMMIODevice *dev = container_of(d, QVirtioMMIODevice, vdev);
|
2015-02-25 00:21:55 +03:00
|
|
|
QVirtQueue *vq;
|
|
|
|
uint64_t addr;
|
|
|
|
|
|
|
|
vq = g_malloc0(sizeof(*vq));
|
2019-10-23 13:04:18 +03:00
|
|
|
vq->vdev = d;
|
2015-02-25 00:21:55 +03:00
|
|
|
qvirtio_mmio_queue_select(d, index);
|
2018-07-19 16:14:44 +03:00
|
|
|
qtest_writel(dev->qts, dev->addr + QVIRTIO_MMIO_QUEUE_ALIGN, dev->page_size);
|
2015-02-25 00:21:55 +03:00
|
|
|
|
|
|
|
vq->index = index;
|
|
|
|
vq->size = qvirtio_mmio_get_queue_size(d);
|
|
|
|
vq->free_head = 0;
|
|
|
|
vq->num_free = vq->size;
|
|
|
|
vq->align = dev->page_size;
|
2019-10-23 13:04:12 +03:00
|
|
|
vq->indirect = dev->features & (1ull << VIRTIO_RING_F_INDIRECT_DESC);
|
|
|
|
vq->event = dev->features & (1ull << VIRTIO_RING_F_EVENT_IDX);
|
2015-02-25 00:21:55 +03:00
|
|
|
|
2018-07-19 16:14:44 +03:00
|
|
|
qtest_writel(dev->qts, dev->addr + QVIRTIO_MMIO_QUEUE_NUM, vq->size);
|
2015-02-25 00:21:55 +03:00
|
|
|
|
|
|
|
/* Check different than 0 */
|
|
|
|
g_assert_cmpint(vq->size, !=, 0);
|
|
|
|
|
|
|
|
/* Check power of 2 */
|
|
|
|
g_assert_cmpint(vq->size & (vq->size - 1), ==, 0);
|
|
|
|
|
|
|
|
addr = guest_alloc(alloc, qvring_size(vq->size, dev->page_size));
|
2019-05-15 20:43:23 +03:00
|
|
|
qvring_init(dev->qts, alloc, vq, addr);
|
2019-10-23 13:04:20 +03:00
|
|
|
qvirtio_mmio_set_queue_address(d, vq);
|
2015-02-25 00:21:55 +03:00
|
|
|
|
|
|
|
return vq;
|
|
|
|
}
|
|
|
|
|
2016-05-05 18:53:35 +03:00
|
|
|
static void qvirtio_mmio_virtqueue_cleanup(QVirtQueue *vq,
|
|
|
|
QGuestAllocator *alloc)
|
|
|
|
{
|
|
|
|
guest_free(alloc, vq->desc);
|
|
|
|
g_free(vq);
|
|
|
|
}
|
|
|
|
|
2015-02-25 00:21:55 +03:00
|
|
|
static void qvirtio_mmio_virtqueue_kick(QVirtioDevice *d, QVirtQueue *vq)
|
|
|
|
{
|
2018-07-19 16:14:44 +03:00
|
|
|
QVirtioMMIODevice *dev = container_of(d, QVirtioMMIODevice, vdev);
|
|
|
|
qtest_writel(dev->qts, dev->addr + QVIRTIO_MMIO_QUEUE_NOTIFY, vq->index);
|
2015-02-25 00:21:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const QVirtioBus qvirtio_mmio = {
|
|
|
|
.config_readb = qvirtio_mmio_config_readb,
|
|
|
|
.config_readw = qvirtio_mmio_config_readw,
|
|
|
|
.config_readl = qvirtio_mmio_config_readl,
|
|
|
|
.config_readq = qvirtio_mmio_config_readq,
|
|
|
|
.get_features = qvirtio_mmio_get_features,
|
|
|
|
.set_features = qvirtio_mmio_set_features,
|
|
|
|
.get_guest_features = qvirtio_mmio_get_guest_features,
|
|
|
|
.get_status = qvirtio_mmio_get_status,
|
|
|
|
.set_status = qvirtio_mmio_set_status,
|
|
|
|
.get_queue_isr_status = qvirtio_mmio_get_queue_isr_status,
|
2019-09-03 09:18:46 +03:00
|
|
|
.wait_config_isr_status = qvirtio_mmio_wait_config_isr_status,
|
2015-02-25 00:21:55 +03:00
|
|
|
.queue_select = qvirtio_mmio_queue_select,
|
|
|
|
.get_queue_size = qvirtio_mmio_get_queue_size,
|
|
|
|
.set_queue_address = qvirtio_mmio_set_queue_address,
|
|
|
|
.virtqueue_setup = qvirtio_mmio_virtqueue_setup,
|
2016-05-05 18:53:35 +03:00
|
|
|
.virtqueue_cleanup = qvirtio_mmio_virtqueue_cleanup,
|
2015-02-25 00:21:55 +03:00
|
|
|
.virtqueue_kick = qvirtio_mmio_virtqueue_kick,
|
|
|
|
};
|
|
|
|
|
2018-07-19 16:14:44 +03:00
|
|
|
static void *qvirtio_mmio_get_driver(void *obj, const char *interface)
|
2015-02-25 00:21:55 +03:00
|
|
|
{
|
2018-07-19 16:14:44 +03:00
|
|
|
QVirtioMMIODevice *virtio_mmio = obj;
|
|
|
|
if (!g_strcmp0(interface, "virtio-bus")) {
|
|
|
|
return &virtio_mmio->vdev;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "%s not present in virtio-mmio\n", interface);
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qvirtio_mmio_start_hw(QOSGraphObject *obj)
|
|
|
|
{
|
|
|
|
QVirtioMMIODevice *dev = (QVirtioMMIODevice *) obj;
|
|
|
|
qvirtio_start_device(&dev->vdev);
|
|
|
|
}
|
2015-02-25 00:21:55 +03:00
|
|
|
|
2018-07-19 16:14:44 +03:00
|
|
|
void qvirtio_mmio_init_device(QVirtioMMIODevice *dev, QTestState *qts,
|
|
|
|
uint64_t addr, uint32_t page_size)
|
|
|
|
{
|
|
|
|
uint32_t magic;
|
|
|
|
magic = qtest_readl(qts, addr + QVIRTIO_MMIO_MAGIC_VALUE);
|
2015-02-25 00:21:55 +03:00
|
|
|
g_assert(magic == ('v' | 'i' << 8 | 'r' << 16 | 't' << 24));
|
|
|
|
|
2019-10-23 13:04:11 +03:00
|
|
|
dev->version = qtest_readl(qts, addr + QVIRTIO_MMIO_VERSION);
|
|
|
|
g_assert(dev->version == 1 || dev->version == 2);
|
|
|
|
|
2018-07-19 16:14:44 +03:00
|
|
|
dev->qts = qts;
|
2015-02-25 00:21:55 +03:00
|
|
|
dev->addr = addr;
|
|
|
|
dev->page_size = page_size;
|
2018-07-19 16:14:44 +03:00
|
|
|
dev->vdev.device_type = qtest_readl(qts, addr + QVIRTIO_MMIO_DEVICE_ID);
|
2016-10-17 13:30:21 +03:00
|
|
|
dev->vdev.bus = &qvirtio_mmio;
|
2015-02-25 00:21:55 +03:00
|
|
|
|
2018-07-19 16:14:44 +03:00
|
|
|
qtest_writel(qts, addr + QVIRTIO_MMIO_GUEST_PAGE_SIZE, page_size);
|
|
|
|
|
|
|
|
dev->obj.get_driver = qvirtio_mmio_get_driver;
|
|
|
|
dev->obj.start_hw = qvirtio_mmio_start_hw;
|
|
|
|
}
|
2015-02-25 00:21:55 +03:00
|
|
|
|
2018-07-19 16:14:44 +03:00
|
|
|
static void virtio_mmio_register_nodes(void)
|
|
|
|
{
|
|
|
|
qos_node_create_driver("virtio-mmio", NULL);
|
|
|
|
qos_node_produces("virtio-mmio", "virtio-bus");
|
2015-02-25 00:21:55 +03:00
|
|
|
}
|
2018-07-19 16:14:44 +03:00
|
|
|
|
|
|
|
libqos_init(virtio_mmio_register_nodes);
|