block/nfs: fix calculation of allocated file size
[qemu.git] / tests / libqos / virtio-mmio.c
blobb3e62e77d815ceb46247b1f0644496a2c1073feb
1 /*
2 * libqos virtio MMIO driver
4 * Copyright (c) 2014 Marc MarĂ­
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 */
10 #include <glib.h>
11 #include <stdio.h>
12 #include "libqtest.h"
13 #include "libqos/virtio.h"
14 #include "libqos/virtio-mmio.h"
15 #include "libqos/malloc.h"
16 #include "libqos/malloc-generic.h"
18 static uint8_t qvirtio_mmio_config_readb(QVirtioDevice *d, uint64_t addr)
20 QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
21 return readb(dev->addr + addr);
24 static uint16_t qvirtio_mmio_config_readw(QVirtioDevice *d, uint64_t addr)
26 QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
27 return readw(dev->addr + addr);
30 static uint32_t qvirtio_mmio_config_readl(QVirtioDevice *d, uint64_t addr)
32 QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
33 return readl(dev->addr + addr);
36 static uint64_t qvirtio_mmio_config_readq(QVirtioDevice *d, uint64_t addr)
38 QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
39 return readq(dev->addr + addr);
42 static uint32_t qvirtio_mmio_get_features(QVirtioDevice *d)
44 QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
45 writel(dev->addr + QVIRTIO_MMIO_HOST_FEATURES_SEL, 0);
46 return readl(dev->addr + QVIRTIO_MMIO_HOST_FEATURES);
49 static void qvirtio_mmio_set_features(QVirtioDevice *d, uint32_t features)
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);
57 static uint32_t qvirtio_mmio_get_guest_features(QVirtioDevice *d)
59 QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
60 return dev->features;
63 static uint8_t qvirtio_mmio_get_status(QVirtioDevice *d)
65 QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
66 return (uint8_t)readl(dev->addr + QVIRTIO_MMIO_DEVICE_STATUS);
69 static void qvirtio_mmio_set_status(QVirtioDevice *d, uint8_t status)
71 QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
72 writel(dev->addr + QVIRTIO_MMIO_DEVICE_STATUS, (uint32_t)status);
75 static bool qvirtio_mmio_get_queue_isr_status(QVirtioDevice *d, QVirtQueue *vq)
77 QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
78 uint32_t isr;
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;
86 return false;
89 static bool qvirtio_mmio_get_config_isr_status(QVirtioDevice *d)
91 QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
92 uint32_t isr;
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;
100 return false;
103 static void qvirtio_mmio_queue_select(QVirtioDevice *d, uint16_t index)
105 QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
106 writel(dev->addr + QVIRTIO_MMIO_QUEUE_SEL, (uint32_t)index);
108 g_assert_cmphex(readl(dev->addr + QVIRTIO_MMIO_QUEUE_PFN), ==, 0);
111 static uint16_t qvirtio_mmio_get_queue_size(QVirtioDevice *d)
113 QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
114 return (uint16_t)readl(dev->addr + QVIRTIO_MMIO_QUEUE_NUM_MAX);
117 static void qvirtio_mmio_set_queue_address(QVirtioDevice *d, uint32_t pfn)
119 QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
120 writel(dev->addr + QVIRTIO_MMIO_QUEUE_PFN, pfn);
123 static QVirtQueue *qvirtio_mmio_virtqueue_setup(QVirtioDevice *d,
124 QGuestAllocator *alloc, uint16_t index)
126 QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
127 QVirtQueue *vq;
128 uint64_t addr;
130 vq = g_malloc0(sizeof(*vq));
131 qvirtio_mmio_queue_select(d, index);
132 writel(dev->addr + QVIRTIO_MMIO_QUEUE_ALIGN, dev->page_size);
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 & QVIRTIO_F_RING_INDIRECT_DESC) != 0;
140 vq->event = (dev->features & QVIRTIO_F_RING_EVENT_IDX) != 0;
142 writel(dev->addr + QVIRTIO_MMIO_QUEUE_NUM, vq->size);
144 /* Check different than 0 */
145 g_assert_cmpint(vq->size, !=, 0);
147 /* Check power of 2 */
148 g_assert_cmpint(vq->size & (vq->size - 1), ==, 0);
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);
154 return vq;
157 static void qvirtio_mmio_virtqueue_kick(QVirtioDevice *d, QVirtQueue *vq)
159 QVirtioMMIODevice *dev = (QVirtioMMIODevice *)d;
160 writel(dev->addr + QVIRTIO_MMIO_QUEUE_NOTIFY, vq->index);
163 const QVirtioBus qvirtio_mmio = {
164 .config_readb = qvirtio_mmio_config_readb,
165 .config_readw = qvirtio_mmio_config_readw,
166 .config_readl = qvirtio_mmio_config_readl,
167 .config_readq = qvirtio_mmio_config_readq,
168 .get_features = qvirtio_mmio_get_features,
169 .set_features = qvirtio_mmio_set_features,
170 .get_guest_features = qvirtio_mmio_get_guest_features,
171 .get_status = qvirtio_mmio_get_status,
172 .set_status = qvirtio_mmio_set_status,
173 .get_queue_isr_status = qvirtio_mmio_get_queue_isr_status,
174 .get_config_isr_status = qvirtio_mmio_get_config_isr_status,
175 .queue_select = qvirtio_mmio_queue_select,
176 .get_queue_size = qvirtio_mmio_get_queue_size,
177 .set_queue_address = qvirtio_mmio_set_queue_address,
178 .virtqueue_setup = qvirtio_mmio_virtqueue_setup,
179 .virtqueue_kick = qvirtio_mmio_virtqueue_kick,
182 QVirtioMMIODevice *qvirtio_mmio_init_device(uint64_t addr, uint32_t page_size)
184 QVirtioMMIODevice *dev;
185 uint32_t magic;
186 dev = g_malloc0(sizeof(*dev));
188 magic = readl(addr + QVIRTIO_MMIO_MAGIC_VALUE);
189 g_assert(magic == ('v' | 'i' << 8 | 'r' << 16 | 't' << 24));
191 dev->addr = addr;
192 dev->page_size = page_size;
193 dev->vdev.device_type = readl(addr + QVIRTIO_MMIO_DEVICE_ID);
195 writel(addr + QVIRTIO_MMIO_GUEST_PAGE_SIZE, page_size);
197 return dev;