qemu/tests/libqos/virtio-mmio.c
<<
>>
Prefs
   1/*
   2 * libqos virtio MMIO driver
   3 *
   4 * Copyright (c) 2014 Marc MarĂ­
   5 *
   6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
   7 * See the COPYING file in the top-level directory.
   8 */
   9
  10#include "qemu/osdep.h"
  11#include "libqtest.h"
  12#include "libqos/virtio.h"
  13#include "libqos/virtio-mmio.h"
  14#include "libqos/malloc.h"
  15#include "libqos/malloc-generic.h"
  16#include "standard-headers/linux/virtio_ring.h"
  17
  18static uint8_t qvirtio_mmio_config_readb(QVirtioDevice *d, uint64_t addr)
  19{
  20    QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
  21    return readb(dev->addr + addr);
  22}
  23
  24static uint16_t qvirtio_mmio_config_readw(QVirtioDevice *d, uint64_t addr)
  25{
  26    QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
  27    return readw(dev->addr + addr);
  28}
  29
  30static uint32_t qvirtio_mmio_config_readl(QVirtioDevice *d, uint64_t addr)
  31{
  32    QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
  33    return readl(dev->addr + addr);
  34}
  35
  36static uint64_t qvirtio_mmio_config_readq(QVirtioDevice *d, uint64_t addr)
  37{
  38    QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
  39    return readq(dev->addr + addr);
  40}
  41
  42static uint32_t qvirtio_mmio_get_features(QVirtioDevice *d)
  43{
  44    QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
  45    writel(dev->addr + QVIRTIO_MMIO_HOST_FEATURES_SEL, 0);
  46    return readl(dev->addr + QVIRTIO_MMIO_HOST_FEATURES);
  47}
  48
  49static void qvirtio_mmio_set_features(QVirtioDevice *d, uint32_t features)
  50{
  51    QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
  52    dev->features = features;
  53    writel(dev->addr + QVIRTIO_MMIO_GUEST_FEATURES_SEL, 0);
  54    writel(dev->addr + QVIRTIO_MMIO_GUEST_FEATURES, features);
  55}
  56
  57static uint32_t qvirtio_mmio_get_guest_features(QVirtioDevice *d)
  58{
  59    QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
  60    return dev->features;
  61}
  62
  63static uint8_t qvirtio_mmio_get_status(QVirtioDevice *d)
  64{
  65    QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
  66    return (uint8_t)readl(dev->addr + QVIRTIO_MMIO_DEVICE_STATUS);
  67}
  68
  69static void qvirtio_mmio_set_status(QVirtioDevice *d, uint8_t status)
  70{
  71    QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
  72    writel(dev->addr + QVIRTIO_MMIO_DEVICE_STATUS, (uint32_t)status);
  73}
  74
  75static bool qvirtio_mmio_get_queue_isr_status(QVirtioDevice *d, QVirtQueue *vq)
  76{
  77    QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
  78    uint32_t isr;
  79
  80    isr = readl(dev->addr + QVIRTIO_MMIO_INTERRUPT_STATUS) & 1;
  81    if (isr != 0) {
  82        writel(dev->addr + QVIRTIO_MMIO_INTERRUPT_ACK, 1);
  83        return true;
  84    }
  85
  86    return false;
  87}
  88
  89static bool qvirtio_mmio_get_config_isr_status(QVirtioDevice *d)
  90{
  91    QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
  92    uint32_t isr;
  93
  94    isr = readl(dev->addr + QVIRTIO_MMIO_INTERRUPT_STATUS) & 2;
  95    if (isr != 0) {
  96        writel(dev->addr + QVIRTIO_MMIO_INTERRUPT_ACK, 2);
  97        return true;
  98    }
  99
 100    return false;
 101}
 102
 103static void qvirtio_mmio_queue_select(QVirtioDevice *d, uint16_t index)
 104{
 105    QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
 106    writel(dev->addr + QVIRTIO_MMIO_QUEUE_SEL, (uint32_t)index);
 107
 108    g_assert_cmphex(readl(dev->addr + QVIRTIO_MMIO_QUEUE_PFN), ==, 0);
 109}
 110
 111static uint16_t qvirtio_mmio_get_queue_size(QVirtioDevice *d)
 112{
 113    QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
 114    return (uint16_t)readl(dev->addr + QVIRTIO_MMIO_QUEUE_NUM_MAX);
 115}
 116
 117static void qvirtio_mmio_set_queue_address(QVirtioDevice *d, uint32_t pfn)
 118{
 119    QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
 120    writel(dev->addr + QVIRTIO_MMIO_QUEUE_PFN, pfn);
 121}
 122
 123static QVirtQueue *qvirtio_mmio_virtqueue_setup(QVirtioDevice *d,
 124                                        QGuestAllocator *alloc, uint16_t index)
 125{
 126    QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
 127    QVirtQueue *vq;
 128    uint64_t addr;
 129
 130    vq = g_malloc0(sizeof(*vq));
 131    qvirtio_mmio_queue_select(d, index);
 132    writel(dev->addr + QVIRTIO_MMIO_QUEUE_ALIGN, dev->page_size);
 133
 134    vq->index = index;
 135    vq->size = qvirtio_mmio_get_queue_size(d);
 136    vq->free_head = 0;
 137    vq->num_free = vq->size;
 138    vq->align = dev->page_size;
 139    vq->indirect = (dev->features & (1u << VIRTIO_RING_F_INDIRECT_DESC)) != 0;
 140    vq->event = (dev->features & (1u << VIRTIO_RING_F_EVENT_IDX)) != 0;
 141
 142    writel(dev->addr + QVIRTIO_MMIO_QUEUE_NUM, vq->size);
 143
 144    /* Check different than 0 */
 145    g_assert_cmpint(vq->size, !=, 0);
 146
 147    /* Check power of 2 */
 148    g_assert_cmpint(vq->size & (vq->size - 1), ==, 0);
 149
 150    addr = guest_alloc(alloc, qvring_size(vq->size, dev->page_size));
 151    qvring_init(alloc, vq, addr);
 152    qvirtio_mmio_set_queue_address(d, vq->desc / dev->page_size);
 153
 154    return vq;
 155}
 156
 157static void qvirtio_mmio_virtqueue_cleanup(QVirtQueue *vq,
 158                                           QGuestAllocator *alloc)
 159{
 160    guest_free(alloc, vq->desc);
 161    g_free(vq);
 162}
 163
 164static void qvirtio_mmio_virtqueue_kick(QVirtioDevice *d, QVirtQueue *vq)
 165{
 166    QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
 167    writel(dev->addr + QVIRTIO_MMIO_QUEUE_NOTIFY, vq->index);
 168}
 169
 170const QVirtioBus qvirtio_mmio = {
 171    .config_readb = qvirtio_mmio_config_readb,
 172    .config_readw = qvirtio_mmio_config_readw,
 173    .config_readl = qvirtio_mmio_config_readl,
 174    .config_readq = qvirtio_mmio_config_readq,
 175    .get_features = qvirtio_mmio_get_features,
 176    .set_features = qvirtio_mmio_set_features,
 177    .get_guest_features = qvirtio_mmio_get_guest_features,
 178    .get_status = qvirtio_mmio_get_status,
 179    .set_status = qvirtio_mmio_set_status,
 180    .get_queue_isr_status = qvirtio_mmio_get_queue_isr_status,
 181    .get_config_isr_status = qvirtio_mmio_get_config_isr_status,
 182    .queue_select = qvirtio_mmio_queue_select,
 183    .get_queue_size = qvirtio_mmio_get_queue_size,
 184    .set_queue_address = qvirtio_mmio_set_queue_address,
 185    .virtqueue_setup = qvirtio_mmio_virtqueue_setup,
 186    .virtqueue_cleanup = qvirtio_mmio_virtqueue_cleanup,
 187    .virtqueue_kick = qvirtio_mmio_virtqueue_kick,
 188};
 189
 190QVirtioMMIODevice *qvirtio_mmio_init_device(uint64_t addr, uint32_t page_size)
 191{
 192    QVirtioMMIODevice *dev;
 193    uint32_t magic;
 194    dev = g_malloc0(sizeof(*dev));
 195
 196    magic = readl(addr + QVIRTIO_MMIO_MAGIC_VALUE);
 197    g_assert(magic == ('v' | 'i' << 8 | 'r' << 16 | 't' << 24));
 198
 199    dev->addr = addr;
 200    dev->page_size = page_size;
 201    dev->vdev.device_type = readl(addr + QVIRTIO_MMIO_DEVICE_ID);
 202
 203    writel(addr + QVIRTIO_MMIO_GUEST_PAGE_SIZE, page_size);
 204
 205    return dev;
 206}
 207