qemu-nbd: Replace BSDism <err.h> by error_report()
[qemu/ar7.git] / tests / libqos / virtio.c
blob3205b88d90107741fd813ae07e9522fd77ebeaab
1 /*
2 * libqos virtio 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 "libqtest.h"
12 #include "libqos/virtio.h"
14 uint8_t qvirtio_config_readb(const QVirtioBus *bus, QVirtioDevice *d,
15 uint64_t addr)
17 return bus->config_readb(d, addr);
20 uint16_t qvirtio_config_readw(const QVirtioBus *bus, QVirtioDevice *d,
21 uint64_t addr)
23 return bus->config_readw(d, addr);
26 uint32_t qvirtio_config_readl(const QVirtioBus *bus, QVirtioDevice *d,
27 uint64_t addr)
29 return bus->config_readl(d, addr);
32 uint64_t qvirtio_config_readq(const QVirtioBus *bus, QVirtioDevice *d,
33 uint64_t addr)
35 return bus->config_readq(d, addr);
38 uint32_t qvirtio_get_features(const QVirtioBus *bus, QVirtioDevice *d)
40 return bus->get_features(d);
43 void qvirtio_set_features(const QVirtioBus *bus, QVirtioDevice *d,
44 uint32_t features)
46 bus->set_features(d, features);
49 QVirtQueue *qvirtqueue_setup(const QVirtioBus *bus, QVirtioDevice *d,
50 QGuestAllocator *alloc, uint16_t index)
52 return bus->virtqueue_setup(d, alloc, index);
55 void qvirtio_reset(const QVirtioBus *bus, QVirtioDevice *d)
57 bus->set_status(d, QVIRTIO_RESET);
58 g_assert_cmphex(bus->get_status(d), ==, QVIRTIO_RESET);
61 void qvirtio_set_acknowledge(const QVirtioBus *bus, QVirtioDevice *d)
63 bus->set_status(d, bus->get_status(d) | QVIRTIO_ACKNOWLEDGE);
64 g_assert_cmphex(bus->get_status(d), ==, QVIRTIO_ACKNOWLEDGE);
67 void qvirtio_set_driver(const QVirtioBus *bus, QVirtioDevice *d)
69 bus->set_status(d, bus->get_status(d) | QVIRTIO_DRIVER);
70 g_assert_cmphex(bus->get_status(d), ==,
71 QVIRTIO_DRIVER | QVIRTIO_ACKNOWLEDGE);
74 void qvirtio_set_driver_ok(const QVirtioBus *bus, QVirtioDevice *d)
76 bus->set_status(d, bus->get_status(d) | QVIRTIO_DRIVER_OK);
77 g_assert_cmphex(bus->get_status(d), ==,
78 QVIRTIO_DRIVER_OK | QVIRTIO_DRIVER | QVIRTIO_ACKNOWLEDGE);
81 void qvirtio_wait_queue_isr(const QVirtioBus *bus, QVirtioDevice *d,
82 QVirtQueue *vq, gint64 timeout_us)
84 gint64 start_time = g_get_monotonic_time();
86 for (;;) {
87 clock_step(100);
88 if (bus->get_queue_isr_status(d, vq)) {
89 return;
91 g_assert(g_get_monotonic_time() - start_time <= timeout_us);
95 /* Wait for the status byte at given guest memory address to be set
97 * The virtqueue interrupt must not be raised, making this useful for testing
98 * event_index functionality.
100 uint8_t qvirtio_wait_status_byte_no_isr(const QVirtioBus *bus,
101 QVirtioDevice *d,
102 QVirtQueue *vq,
103 uint64_t addr,
104 gint64 timeout_us)
106 gint64 start_time = g_get_monotonic_time();
107 uint8_t val;
109 while ((val = readb(addr)) == 0xff) {
110 clock_step(100);
111 g_assert(!bus->get_queue_isr_status(d, vq));
112 g_assert(g_get_monotonic_time() - start_time <= timeout_us);
114 return val;
117 void qvirtio_wait_config_isr(const QVirtioBus *bus, QVirtioDevice *d,
118 gint64 timeout_us)
120 gint64 start_time = g_get_monotonic_time();
122 for (;;) {
123 clock_step(100);
124 if (bus->get_config_isr_status(d)) {
125 return;
127 g_assert(g_get_monotonic_time() - start_time <= timeout_us);
131 void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr)
133 int i;
135 vq->desc = addr;
136 vq->avail = vq->desc + vq->size*sizeof(QVRingDesc);
137 vq->used = (uint64_t)((vq->avail + sizeof(uint16_t) * (3 + vq->size)
138 + vq->align - 1) & ~(vq->align - 1));
140 for (i = 0; i < vq->size - 1; i++) {
141 /* vq->desc[i].addr */
142 writew(vq->desc + (16 * i), 0);
143 /* vq->desc[i].next */
144 writew(vq->desc + (16 * i) + 14, i + 1);
147 /* vq->avail->flags */
148 writew(vq->avail, 0);
149 /* vq->avail->idx */
150 writew(vq->avail + 2, 0);
151 /* vq->avail->used_event */
152 writew(vq->avail + 4 + (2 * vq->size), 0);
154 /* vq->used->flags */
155 writew(vq->used, 0);
156 /* vq->used->avail_event */
157 writew(vq->used+2+(sizeof(struct QVRingUsedElem)*vq->size), 0);
160 QVRingIndirectDesc *qvring_indirect_desc_setup(QVirtioDevice *d,
161 QGuestAllocator *alloc, uint16_t elem)
163 int i;
164 QVRingIndirectDesc *indirect = g_malloc(sizeof(*indirect));
166 indirect->index = 0;
167 indirect->elem = elem;
168 indirect->desc = guest_alloc(alloc, sizeof(QVRingDesc)*elem);
170 for (i = 0; i < elem - 1; ++i) {
171 /* indirect->desc[i].addr */
172 writeq(indirect->desc + (16 * i), 0);
173 /* indirect->desc[i].flags */
174 writew(indirect->desc + (16 * i) + 12, QVRING_DESC_F_NEXT);
175 /* indirect->desc[i].next */
176 writew(indirect->desc + (16 * i) + 14, i + 1);
179 return indirect;
182 void qvring_indirect_desc_add(QVRingIndirectDesc *indirect, uint64_t data,
183 uint32_t len, bool write)
185 uint16_t flags;
187 g_assert_cmpint(indirect->index, <, indirect->elem);
189 flags = readw(indirect->desc + (16 * indirect->index) + 12);
191 if (write) {
192 flags |= QVRING_DESC_F_WRITE;
195 /* indirect->desc[indirect->index].addr */
196 writeq(indirect->desc + (16 * indirect->index), data);
197 /* indirect->desc[indirect->index].len */
198 writel(indirect->desc + (16 * indirect->index) + 8, len);
199 /* indirect->desc[indirect->index].flags */
200 writew(indirect->desc + (16 * indirect->index) + 12, flags);
202 indirect->index++;
205 uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write,
206 bool next)
208 uint16_t flags = 0;
209 vq->num_free--;
211 if (write) {
212 flags |= QVRING_DESC_F_WRITE;
215 if (next) {
216 flags |= QVRING_DESC_F_NEXT;
219 /* vq->desc[vq->free_head].addr */
220 writeq(vq->desc + (16 * vq->free_head), data);
221 /* vq->desc[vq->free_head].len */
222 writel(vq->desc + (16 * vq->free_head) + 8, len);
223 /* vq->desc[vq->free_head].flags */
224 writew(vq->desc + (16 * vq->free_head) + 12, flags);
226 return vq->free_head++; /* Return and increase, in this order */
229 uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect)
231 g_assert(vq->indirect);
232 g_assert_cmpint(vq->size, >=, indirect->elem);
233 g_assert_cmpint(indirect->index, ==, indirect->elem);
235 vq->num_free--;
237 /* vq->desc[vq->free_head].addr */
238 writeq(vq->desc + (16 * vq->free_head), indirect->desc);
239 /* vq->desc[vq->free_head].len */
240 writel(vq->desc + (16 * vq->free_head) + 8,
241 sizeof(QVRingDesc) * indirect->elem);
242 /* vq->desc[vq->free_head].flags */
243 writew(vq->desc + (16 * vq->free_head) + 12, QVRING_DESC_F_INDIRECT);
245 return vq->free_head++; /* Return and increase, in this order */
248 void qvirtqueue_kick(const QVirtioBus *bus, QVirtioDevice *d, QVirtQueue *vq,
249 uint32_t free_head)
251 /* vq->avail->idx */
252 uint16_t idx = readl(vq->avail + 2);
253 /* vq->used->flags */
254 uint16_t flags;
255 /* vq->used->avail_event */
256 uint16_t avail_event;
258 /* vq->avail->ring[idx % vq->size] */
259 writel(vq->avail + 4 + (2 * (idx % vq->size)), free_head);
260 /* vq->avail->idx */
261 writel(vq->avail + 2, idx + 1);
263 /* Must read after idx is updated */
264 flags = readw(vq->avail);
265 avail_event = readw(vq->used + 4 +
266 (sizeof(struct QVRingUsedElem) * vq->size));
268 /* < 1 because we add elements to avail queue one by one */
269 if ((flags & QVRING_USED_F_NO_NOTIFY) == 0 &&
270 (!vq->event || (uint16_t)(idx-avail_event) < 1)) {
271 bus->virtqueue_kick(d, vq);
275 void qvirtqueue_set_used_event(QVirtQueue *vq, uint16_t idx)
277 g_assert(vq->event);
279 /* vq->avail->used_event */
280 writew(vq->avail + 4 + (2 * vq->size), idx);