xref: /qemu/tests/qtest/libqos/virtio.c (revision 56140fbb8f2bb717eb2670aa1056aeaefe8b0246)
1 /*
2  * libqos virtio 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 "standard-headers/linux/virtio_config.h"
14 #include "standard-headers/linux/virtio_ring.h"
15 
16 uint8_t qvirtio_config_readb(QVirtioDevice *d, uint64_t addr)
17 {
18     g_assert_true(d->features_negotiated);
19     return d->bus->config_readb(d, addr);
20 }
21 
22 uint16_t qvirtio_config_readw(QVirtioDevice *d, uint64_t addr)
23 {
24     g_assert_true(d->features_negotiated);
25     return d->bus->config_readw(d, addr);
26 }
27 
28 uint32_t qvirtio_config_readl(QVirtioDevice *d, uint64_t addr)
29 {
30     g_assert_true(d->features_negotiated);
31     return d->bus->config_readl(d, addr);
32 }
33 
34 uint64_t qvirtio_config_readq(QVirtioDevice *d, uint64_t addr)
35 {
36     g_assert_true(d->features_negotiated);
37     return d->bus->config_readq(d, addr);
38 }
39 
40 uint64_t qvirtio_get_features(QVirtioDevice *d)
41 {
42     return d->bus->get_features(d);
43 }
44 
45 void qvirtio_set_features(QVirtioDevice *d, uint64_t features)
46 {
47     d->features = features;
48     d->bus->set_features(d, features);
49     d->features_negotiated = true;
50 }
51 
52 QVirtQueue *qvirtqueue_setup(QVirtioDevice *d,
53                              QGuestAllocator *alloc, uint16_t index)
54 {
55     g_assert_true(d->features_negotiated);
56     return d->bus->virtqueue_setup(d, alloc, index);
57 }
58 
59 void qvirtqueue_cleanup(const QVirtioBus *bus, QVirtQueue *vq,
60                         QGuestAllocator *alloc)
61 {
62     return bus->virtqueue_cleanup(vq, alloc);
63 }
64 
65 void qvirtio_reset(QVirtioDevice *d)
66 {
67     d->bus->set_status(d, 0);
68     g_assert_cmphex(d->bus->get_status(d), ==, 0);
69     d->features_negotiated = false;
70 }
71 
72 void qvirtio_set_acknowledge(QVirtioDevice *d)
73 {
74     d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_ACKNOWLEDGE);
75     g_assert_cmphex(d->bus->get_status(d), ==, VIRTIO_CONFIG_S_ACKNOWLEDGE);
76 }
77 
78 void qvirtio_set_driver(QVirtioDevice *d)
79 {
80     d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_DRIVER);
81     g_assert_cmphex(d->bus->get_status(d), ==,
82                     VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_ACKNOWLEDGE);
83 }
84 
85 void qvirtio_set_driver_ok(QVirtioDevice *d)
86 {
87     d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_DRIVER_OK);
88     g_assert_cmphex(d->bus->get_status(d), ==, VIRTIO_CONFIG_S_DRIVER_OK |
89                     VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_ACKNOWLEDGE);
90 }
91 
92 void qvirtio_wait_queue_isr(QTestState *qts, QVirtioDevice *d,
93                             QVirtQueue *vq, gint64 timeout_us)
94 {
95     gint64 start_time = g_get_monotonic_time();
96 
97     for (;;) {
98         qtest_clock_step(qts, 100);
99         if (d->bus->get_queue_isr_status(d, vq)) {
100             return;
101         }
102         g_assert(g_get_monotonic_time() - start_time <= timeout_us);
103     }
104 }
105 
106 /* Wait for the status byte at given guest memory address to be set
107  *
108  * The virtqueue interrupt must not be raised, making this useful for testing
109  * event_index functionality.
110  */
111 uint8_t qvirtio_wait_status_byte_no_isr(QTestState *qts, QVirtioDevice *d,
112                                         QVirtQueue *vq,
113                                         uint64_t addr,
114                                         gint64 timeout_us)
115 {
116     gint64 start_time = g_get_monotonic_time();
117     uint8_t val;
118 
119     while ((val = qtest_readb(qts, addr)) == 0xff) {
120         qtest_clock_step(qts, 100);
121         g_assert(!d->bus->get_queue_isr_status(d, vq));
122         g_assert(g_get_monotonic_time() - start_time <= timeout_us);
123     }
124     return val;
125 }
126 
127 /*
128  * qvirtio_wait_used_elem:
129  * @desc_idx: The next expected vq->desc[] index in the used ring
130  * @len: A pointer that is filled with the length written into the buffer, may
131  *       be NULL
132  * @timeout_us: How many microseconds to wait before failing
133  *
134  * This function waits for the next completed request on the used ring.
135  */
136 void qvirtio_wait_used_elem(QTestState *qts, QVirtioDevice *d,
137                             QVirtQueue *vq,
138                             uint32_t desc_idx,
139                             uint32_t *len,
140                             gint64 timeout_us)
141 {
142     gint64 start_time = g_get_monotonic_time();
143 
144     for (;;) {
145         uint32_t got_desc_idx;
146 
147         qtest_clock_step(qts, 100);
148 
149         if (d->bus->get_queue_isr_status(d, vq) &&
150             qvirtqueue_get_buf(qts, vq, &got_desc_idx, len)) {
151             g_assert_cmpint(got_desc_idx, ==, desc_idx);
152             return;
153         }
154 
155         g_assert(g_get_monotonic_time() - start_time <= timeout_us);
156     }
157 }
158 
159 void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us)
160 {
161     d->bus->wait_config_isr_status(d, timeout_us);
162 }
163 
164 void qvring_init(QTestState *qts, const QGuestAllocator *alloc, QVirtQueue *vq,
165                  uint64_t addr)
166 {
167     int i;
168 
169     vq->desc = addr;
170     vq->avail = vq->desc + vq->size * sizeof(struct vring_desc);
171     vq->used = (uint64_t)((vq->avail + sizeof(uint16_t) * (3 + vq->size)
172         + vq->align - 1) & ~(vq->align - 1));
173 
174     for (i = 0; i < vq->size - 1; i++) {
175         /* vq->desc[i].addr */
176         qtest_writeq(qts, vq->desc + (16 * i), 0);
177         /* vq->desc[i].next */
178         qtest_writew(qts, vq->desc + (16 * i) + 14, i + 1);
179     }
180 
181     /* vq->avail->flags */
182     qtest_writew(qts, vq->avail, 0);
183     /* vq->avail->idx */
184     qtest_writew(qts, vq->avail + 2, 0);
185     /* vq->avail->used_event */
186     qtest_writew(qts, vq->avail + 4 + (2 * vq->size), 0);
187 
188     /* vq->used->flags */
189     qtest_writew(qts, vq->used, 0);
190     /* vq->used->avail_event */
191     qtest_writew(qts, vq->used + 2 + sizeof(struct vring_used_elem) * vq->size,
192                  0);
193 }
194 
195 QVRingIndirectDesc *qvring_indirect_desc_setup(QTestState *qs, QVirtioDevice *d,
196                                                QGuestAllocator *alloc,
197                                                uint16_t elem)
198 {
199     int i;
200     QVRingIndirectDesc *indirect = g_malloc(sizeof(*indirect));
201 
202     indirect->index = 0;
203     indirect->elem = elem;
204     indirect->desc = guest_alloc(alloc, sizeof(struct vring_desc) * elem);
205 
206     for (i = 0; i < elem - 1; ++i) {
207         /* indirect->desc[i].addr */
208         qtest_writeq(qs, indirect->desc + (16 * i), 0);
209         /* indirect->desc[i].flags */
210         qtest_writew(qs, indirect->desc + (16 * i) + 12, VRING_DESC_F_NEXT);
211         /* indirect->desc[i].next */
212         qtest_writew(qs, indirect->desc + (16 * i) + 14, i + 1);
213     }
214 
215     return indirect;
216 }
217 
218 void qvring_indirect_desc_add(QTestState *qts, QVRingIndirectDesc *indirect,
219                               uint64_t data, uint32_t len, bool write)
220 {
221     uint16_t flags;
222 
223     g_assert_cmpint(indirect->index, <, indirect->elem);
224 
225     flags = qtest_readw(qts, indirect->desc + (16 * indirect->index) + 12);
226 
227     if (write) {
228         flags |= VRING_DESC_F_WRITE;
229     }
230 
231     /* indirect->desc[indirect->index].addr */
232     qtest_writeq(qts, indirect->desc + (16 * indirect->index), data);
233     /* indirect->desc[indirect->index].len */
234     qtest_writel(qts, indirect->desc + (16 * indirect->index) + 8, len);
235     /* indirect->desc[indirect->index].flags */
236     qtest_writew(qts, indirect->desc + (16 * indirect->index) + 12, flags);
237 
238     indirect->index++;
239 }
240 
241 uint32_t qvirtqueue_add(QTestState *qts, QVirtQueue *vq, uint64_t data,
242                         uint32_t len, bool write, bool next)
243 {
244     uint16_t flags = 0;
245     vq->num_free--;
246 
247     if (write) {
248         flags |= VRING_DESC_F_WRITE;
249     }
250 
251     if (next) {
252         flags |= VRING_DESC_F_NEXT;
253     }
254 
255     /* vq->desc[vq->free_head].addr */
256     qtest_writeq(qts, vq->desc + (16 * vq->free_head), data);
257     /* vq->desc[vq->free_head].len */
258     qtest_writel(qts, vq->desc + (16 * vq->free_head) + 8, len);
259     /* vq->desc[vq->free_head].flags */
260     qtest_writew(qts, vq->desc + (16 * vq->free_head) + 12, flags);
261 
262     return vq->free_head++; /* Return and increase, in this order */
263 }
264 
265 uint32_t qvirtqueue_add_indirect(QTestState *qts, QVirtQueue *vq,
266                                  QVRingIndirectDesc *indirect)
267 {
268     g_assert(vq->indirect);
269     g_assert_cmpint(vq->size, >=, indirect->elem);
270     g_assert_cmpint(indirect->index, ==, indirect->elem);
271 
272     vq->num_free--;
273 
274     /* vq->desc[vq->free_head].addr */
275     qtest_writeq(qts, vq->desc + (16 * vq->free_head), indirect->desc);
276     /* vq->desc[vq->free_head].len */
277     qtest_writel(qts, vq->desc + (16 * vq->free_head) + 8,
278            sizeof(struct vring_desc) * indirect->elem);
279     /* vq->desc[vq->free_head].flags */
280     qtest_writew(qts, vq->desc + (16 * vq->free_head) + 12,
281                  VRING_DESC_F_INDIRECT);
282 
283     return vq->free_head++; /* Return and increase, in this order */
284 }
285 
286 void qvirtqueue_kick(QTestState *qts, QVirtioDevice *d, QVirtQueue *vq,
287                      uint32_t free_head)
288 {
289     /* vq->avail->idx */
290     uint16_t idx = qtest_readw(qts, vq->avail + 2);
291     /* vq->used->flags */
292     uint16_t flags;
293     /* vq->used->avail_event */
294     uint16_t avail_event;
295 
296     /* vq->avail->ring[idx % vq->size] */
297     qtest_writew(qts, vq->avail + 4 + (2 * (idx % vq->size)), free_head);
298     /* vq->avail->idx */
299     qtest_writew(qts, vq->avail + 2, idx + 1);
300 
301     /* Must read after idx is updated */
302     flags = qtest_readw(qts, vq->avail);
303     avail_event = qtest_readw(qts, vq->used + 4 +
304                                    sizeof(struct vring_used_elem) * vq->size);
305 
306     /* < 1 because we add elements to avail queue one by one */
307     if ((flags & VRING_USED_F_NO_NOTIFY) == 0 &&
308                             (!vq->event || (uint16_t)(idx-avail_event) < 1)) {
309         d->bus->virtqueue_kick(d, vq);
310     }
311 }
312 
313 /*
314  * qvirtqueue_get_buf:
315  * @desc_idx: A pointer that is filled with the vq->desc[] index, may be NULL
316  * @len: A pointer that is filled with the length written into the buffer, may
317  *       be NULL
318  *
319  * This function gets the next used element if there is one ready.
320  *
321  * Returns: true if an element was ready, false otherwise
322  */
323 bool qvirtqueue_get_buf(QTestState *qts, QVirtQueue *vq, uint32_t *desc_idx,
324                         uint32_t *len)
325 {
326     uint16_t idx;
327     uint64_t elem_addr, addr;
328 
329     idx = qtest_readw(qts, vq->used + offsetof(struct vring_used, idx));
330     if (idx == vq->last_used_idx) {
331         return false;
332     }
333 
334     elem_addr = vq->used +
335         offsetof(struct vring_used, ring) +
336         (vq->last_used_idx % vq->size) *
337         sizeof(struct vring_used_elem);
338 
339     if (desc_idx) {
340         addr = elem_addr + offsetof(struct vring_used_elem, id);
341         *desc_idx = qtest_readl(qts, addr);
342     }
343 
344     if (len) {
345         addr = elem_addr + offsetof(struct vring_used_elem, len);
346         *len = qtest_readw(qts, addr);
347     }
348 
349     vq->last_used_idx++;
350     return true;
351 }
352 
353 void qvirtqueue_set_used_event(QTestState *qts, QVirtQueue *vq, uint16_t idx)
354 {
355     g_assert(vq->event);
356 
357     /* vq->avail->used_event */
358     qtest_writew(qts, vq->avail + 4 + (2 * vq->size), idx);
359 }
360 
361 void qvirtio_start_device(QVirtioDevice *vdev)
362 {
363     qvirtio_reset(vdev);
364     qvirtio_set_acknowledge(vdev);
365     qvirtio_set_driver(vdev);
366 }
367 
368 bool qvirtio_is_big_endian(QVirtioDevice *d)
369 {
370     return d->big_endian;
371 }
372