xref: /openbmc/qemu/tests/qtest/libqos/virtio.c (revision 1466ef6c)
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 "qemu/bswap.h"
12 #include "libqtest.h"
13 #include "virtio.h"
14 #include "standard-headers/linux/virtio_config.h"
15 #include "standard-headers/linux/virtio_ring.h"
16 
17 /*
18  * qtest_readX/writeX() functions transfer host endian from/to guest endian.
19  * This works great for Legacy VIRTIO devices where we need guest endian
20  * accesses.  For VIRTIO 1.0 the vring is little-endian so the automatic guest
21  * endianness conversion is not wanted.
22  *
23  * The following qvirtio_readX/writeX() functions handle Legacy and VIRTIO 1.0
24  * accesses seamlessly.
25  */
26 static uint16_t qvirtio_readw(QVirtioDevice *d, QTestState *qts, uint64_t addr)
27 {
28     uint16_t val = qtest_readw(qts, addr);
29 
30     if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
31         val = bswap16(val);
32     }
33     return val;
34 }
35 
36 static uint32_t qvirtio_readl(QVirtioDevice *d, QTestState *qts, uint64_t addr)
37 {
38     uint32_t val = qtest_readl(qts, addr);
39 
40     if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
41         val = bswap32(val);
42     }
43     return val;
44 }
45 
46 static void qvirtio_writew(QVirtioDevice *d, QTestState *qts,
47                            uint64_t addr, uint16_t val)
48 {
49     if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
50         val = bswap16(val);
51     }
52     qtest_writew(qts, addr, val);
53 }
54 
55 static void qvirtio_writel(QVirtioDevice *d, QTestState *qts,
56                            uint64_t addr, uint32_t val)
57 {
58     if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
59         val = bswap32(val);
60     }
61     qtest_writel(qts, addr, val);
62 }
63 
64 static void qvirtio_writeq(QVirtioDevice *d, QTestState *qts,
65                            uint64_t addr, uint64_t val)
66 {
67     if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
68         val = bswap64(val);
69     }
70     qtest_writeq(qts, addr, val);
71 }
72 
73 uint8_t qvirtio_config_readb(QVirtioDevice *d, uint64_t addr)
74 {
75     g_assert_true(d->features_negotiated);
76     return d->bus->config_readb(d, addr);
77 }
78 
79 uint16_t qvirtio_config_readw(QVirtioDevice *d, uint64_t addr)
80 {
81     g_assert_true(d->features_negotiated);
82     return d->bus->config_readw(d, addr);
83 }
84 
85 uint32_t qvirtio_config_readl(QVirtioDevice *d, uint64_t addr)
86 {
87     g_assert_true(d->features_negotiated);
88     return d->bus->config_readl(d, addr);
89 }
90 
91 uint64_t qvirtio_config_readq(QVirtioDevice *d, uint64_t addr)
92 {
93     g_assert_true(d->features_negotiated);
94     return d->bus->config_readq(d, addr);
95 }
96 
97 uint64_t qvirtio_get_features(QVirtioDevice *d)
98 {
99     return d->bus->get_features(d);
100 }
101 
102 void qvirtio_set_features(QVirtioDevice *d, uint64_t features)
103 {
104     d->features = features;
105     d->bus->set_features(d, features);
106 
107     /*
108      * This could be a separate function for drivers that want to access
109      * configuration space before setting FEATURES_OK, but no existing users
110      * need that and it's less code for callers if this is done implicitly.
111     */
112     if (features & (1ull << VIRTIO_F_VERSION_1)) {
113         uint8_t status = d->bus->get_status(d) |
114                          VIRTIO_CONFIG_S_FEATURES_OK;
115 
116         d->bus->set_status(d, status);
117         g_assert_cmphex(d->bus->get_status(d), ==, status);
118     }
119 
120     d->features_negotiated = true;
121 }
122 
123 QVirtQueue *qvirtqueue_setup(QVirtioDevice *d,
124                              QGuestAllocator *alloc, uint16_t index)
125 {
126     g_assert_true(d->features_negotiated);
127     return d->bus->virtqueue_setup(d, alloc, index);
128 }
129 
130 void qvirtqueue_cleanup(const QVirtioBus *bus, QVirtQueue *vq,
131                         QGuestAllocator *alloc)
132 {
133     return bus->virtqueue_cleanup(vq, alloc);
134 }
135 
136 void qvirtio_reset(QVirtioDevice *d)
137 {
138     d->bus->set_status(d, 0);
139     g_assert_cmphex(d->bus->get_status(d), ==, 0);
140     d->features_negotiated = false;
141 }
142 
143 void qvirtio_set_acknowledge(QVirtioDevice *d)
144 {
145     d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_ACKNOWLEDGE);
146     g_assert_cmphex(d->bus->get_status(d), ==, VIRTIO_CONFIG_S_ACKNOWLEDGE);
147 }
148 
149 void qvirtio_set_driver(QVirtioDevice *d)
150 {
151     d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_DRIVER);
152     g_assert_cmphex(d->bus->get_status(d), ==,
153                     VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_ACKNOWLEDGE);
154 }
155 
156 void qvirtio_set_driver_ok(QVirtioDevice *d)
157 {
158     d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_DRIVER_OK);
159     g_assert_cmphex(d->bus->get_status(d), ==, VIRTIO_CONFIG_S_DRIVER_OK |
160                     VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_ACKNOWLEDGE |
161                     (d->features & (1ull << VIRTIO_F_VERSION_1) ?
162                      VIRTIO_CONFIG_S_FEATURES_OK : 0));
163 }
164 
165 void qvirtio_wait_queue_isr(QTestState *qts, QVirtioDevice *d,
166                             QVirtQueue *vq, gint64 timeout_us)
167 {
168     gint64 start_time = g_get_monotonic_time();
169 
170     for (;;) {
171         qtest_clock_step(qts, 100);
172         if (d->bus->get_queue_isr_status(d, vq)) {
173             return;
174         }
175         g_assert(g_get_monotonic_time() - start_time <= timeout_us);
176     }
177 }
178 
179 /* Wait for the status byte at given guest memory address to be set
180  *
181  * The virtqueue interrupt must not be raised, making this useful for testing
182  * event_index functionality.
183  */
184 uint8_t qvirtio_wait_status_byte_no_isr(QTestState *qts, QVirtioDevice *d,
185                                         QVirtQueue *vq,
186                                         uint64_t addr,
187                                         gint64 timeout_us)
188 {
189     gint64 start_time = g_get_monotonic_time();
190     uint8_t val;
191 
192     while ((val = qtest_readb(qts, addr)) == 0xff) {
193         qtest_clock_step(qts, 100);
194         g_assert(!d->bus->get_queue_isr_status(d, vq));
195         g_assert(g_get_monotonic_time() - start_time <= timeout_us);
196     }
197     return val;
198 }
199 
200 /*
201  * qvirtio_wait_used_elem:
202  * @desc_idx: The next expected vq->desc[] index in the used ring
203  * @len: A pointer that is filled with the length written into the buffer, may
204  *       be NULL
205  * @timeout_us: How many microseconds to wait before failing
206  *
207  * This function waits for the next completed request on the used ring.
208  */
209 void qvirtio_wait_used_elem(QTestState *qts, QVirtioDevice *d,
210                             QVirtQueue *vq,
211                             uint32_t desc_idx,
212                             uint32_t *len,
213                             gint64 timeout_us)
214 {
215     gint64 start_time = g_get_monotonic_time();
216 
217     for (;;) {
218         uint32_t got_desc_idx;
219 
220         qtest_clock_step(qts, 100);
221 
222         if (d->bus->get_queue_isr_status(d, vq) &&
223             qvirtqueue_get_buf(qts, vq, &got_desc_idx, len)) {
224             g_assert_cmpint(got_desc_idx, ==, desc_idx);
225             return;
226         }
227 
228         g_assert(g_get_monotonic_time() - start_time <= timeout_us);
229     }
230 }
231 
232 void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us)
233 {
234     d->bus->wait_config_isr_status(d, timeout_us);
235 }
236 
237 void qvring_init(QTestState *qts, const QGuestAllocator *alloc, QVirtQueue *vq,
238                  uint64_t addr)
239 {
240     int i;
241 
242     vq->desc = addr;
243     vq->avail = vq->desc + vq->size * sizeof(struct vring_desc);
244     vq->used = (uint64_t)((vq->avail + sizeof(uint16_t) * (3 + vq->size)
245         + vq->align - 1) & ~(vq->align - 1));
246 
247     for (i = 0; i < vq->size - 1; i++) {
248         /* vq->desc[i].addr */
249         qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * i), 0);
250         /* vq->desc[i].next */
251         qvirtio_writew(vq->vdev, qts, vq->desc + (16 * i) + 14, i + 1);
252     }
253 
254     /* vq->avail->flags */
255     qvirtio_writew(vq->vdev, qts, vq->avail, 0);
256     /* vq->avail->idx */
257     qvirtio_writew(vq->vdev, qts, vq->avail + 2, 0);
258     /* vq->avail->used_event */
259     qvirtio_writew(vq->vdev, qts, vq->avail + 4 + (2 * vq->size), 0);
260 
261     /* vq->used->flags */
262     qvirtio_writew(vq->vdev, qts, vq->used, 0);
263     /* vq->used->idx */
264     qvirtio_writew(vq->vdev, qts, vq->used + 2, 0);
265     /* vq->used->avail_event */
266     qvirtio_writew(vq->vdev, qts, vq->used + 2 +
267                    sizeof(struct vring_used_elem) * vq->size, 0);
268 }
269 
270 QVRingIndirectDesc *qvring_indirect_desc_setup(QTestState *qs, QVirtioDevice *d,
271                                                QGuestAllocator *alloc,
272                                                uint16_t elem)
273 {
274     int i;
275     QVRingIndirectDesc *indirect = g_malloc(sizeof(*indirect));
276 
277     indirect->index = 0;
278     indirect->elem = elem;
279     indirect->desc = guest_alloc(alloc, sizeof(struct vring_desc) * elem);
280 
281     for (i = 0; i < elem - 1; ++i) {
282         /* indirect->desc[i].addr */
283         qvirtio_writeq(d, qs, indirect->desc + (16 * i), 0);
284         /* indirect->desc[i].flags */
285         qvirtio_writew(d, qs, indirect->desc + (16 * i) + 12,
286                        VRING_DESC_F_NEXT);
287         /* indirect->desc[i].next */
288         qvirtio_writew(d, qs, indirect->desc + (16 * i) + 14, i + 1);
289     }
290 
291     return indirect;
292 }
293 
294 void qvring_indirect_desc_add(QVirtioDevice *d, QTestState *qts,
295                               QVRingIndirectDesc *indirect,
296                               uint64_t data, uint32_t len, bool write)
297 {
298     uint16_t flags;
299 
300     g_assert_cmpint(indirect->index, <, indirect->elem);
301 
302     flags = qvirtio_readw(d, qts, indirect->desc +
303                                   (16 * indirect->index) + 12);
304 
305     if (write) {
306         flags |= VRING_DESC_F_WRITE;
307     }
308 
309     /* indirect->desc[indirect->index].addr */
310     qvirtio_writeq(d, qts, indirect->desc + (16 * indirect->index), data);
311     /* indirect->desc[indirect->index].len */
312     qvirtio_writel(d, qts, indirect->desc + (16 * indirect->index) + 8, len);
313     /* indirect->desc[indirect->index].flags */
314     qvirtio_writew(d, qts, indirect->desc + (16 * indirect->index) + 12,
315                    flags);
316 
317     indirect->index++;
318 }
319 
320 uint32_t qvirtqueue_add(QTestState *qts, QVirtQueue *vq, uint64_t data,
321                         uint32_t len, bool write, bool next)
322 {
323     uint16_t flags = 0;
324     vq->num_free--;
325 
326     if (write) {
327         flags |= VRING_DESC_F_WRITE;
328     }
329 
330     if (next) {
331         flags |= VRING_DESC_F_NEXT;
332     }
333 
334     /* vq->desc[vq->free_head].addr */
335     qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * vq->free_head), data);
336     /* vq->desc[vq->free_head].len */
337     qvirtio_writel(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 8, len);
338     /* vq->desc[vq->free_head].flags */
339     qvirtio_writew(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 12, flags);
340 
341     return vq->free_head++; /* Return and increase, in this order */
342 }
343 
344 uint32_t qvirtqueue_add_indirect(QTestState *qts, QVirtQueue *vq,
345                                  QVRingIndirectDesc *indirect)
346 {
347     g_assert(vq->indirect);
348     g_assert_cmpint(vq->size, >=, indirect->elem);
349     g_assert_cmpint(indirect->index, ==, indirect->elem);
350 
351     vq->num_free--;
352 
353     /* vq->desc[vq->free_head].addr */
354     qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * vq->free_head),
355                    indirect->desc);
356     /* vq->desc[vq->free_head].len */
357     qvirtio_writel(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 8,
358                    sizeof(struct vring_desc) * indirect->elem);
359     /* vq->desc[vq->free_head].flags */
360     qvirtio_writew(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 12,
361                    VRING_DESC_F_INDIRECT);
362 
363     return vq->free_head++; /* Return and increase, in this order */
364 }
365 
366 void qvirtqueue_kick(QTestState *qts, QVirtioDevice *d, QVirtQueue *vq,
367                      uint32_t free_head)
368 {
369     /* vq->avail->idx */
370     uint16_t idx = qvirtio_readw(d, qts, vq->avail + 2);
371     /* vq->used->flags */
372     uint16_t flags;
373     /* vq->used->avail_event */
374     uint16_t avail_event;
375 
376     /* vq->avail->ring[idx % vq->size] */
377     qvirtio_writew(d, qts, vq->avail + 4 + (2 * (idx % vq->size)), free_head);
378     /* vq->avail->idx */
379     qvirtio_writew(d, qts, vq->avail + 2, idx + 1);
380 
381     /* Must read after idx is updated */
382     flags = qvirtio_readw(d, qts, vq->avail);
383     avail_event = qvirtio_readw(d, qts, vq->used + 4 +
384                                 sizeof(struct vring_used_elem) * vq->size);
385 
386     /* < 1 because we add elements to avail queue one by one */
387     if ((flags & VRING_USED_F_NO_NOTIFY) == 0 &&
388                             (!vq->event || (uint16_t)(idx-avail_event) < 1)) {
389         d->bus->virtqueue_kick(d, vq);
390     }
391 }
392 
393 /*
394  * qvirtqueue_get_buf:
395  * @desc_idx: A pointer that is filled with the vq->desc[] index, may be NULL
396  * @len: A pointer that is filled with the length written into the buffer, may
397  *       be NULL
398  *
399  * This function gets the next used element if there is one ready.
400  *
401  * Returns: true if an element was ready, false otherwise
402  */
403 bool qvirtqueue_get_buf(QTestState *qts, QVirtQueue *vq, uint32_t *desc_idx,
404                         uint32_t *len)
405 {
406     uint16_t idx;
407     uint64_t elem_addr, addr;
408 
409     idx = qvirtio_readw(vq->vdev, qts,
410                         vq->used + offsetof(struct vring_used, idx));
411     if (idx == vq->last_used_idx) {
412         return false;
413     }
414 
415     elem_addr = vq->used +
416         offsetof(struct vring_used, ring) +
417         (vq->last_used_idx % vq->size) *
418         sizeof(struct vring_used_elem);
419 
420     if (desc_idx) {
421         addr = elem_addr + offsetof(struct vring_used_elem, id);
422         *desc_idx = qvirtio_readl(vq->vdev, qts, addr);
423     }
424 
425     if (len) {
426         addr = elem_addr + offsetof(struct vring_used_elem, len);
427         *len = qvirtio_readw(vq->vdev, qts, addr);
428     }
429 
430     vq->last_used_idx++;
431     return true;
432 }
433 
434 void qvirtqueue_set_used_event(QTestState *qts, QVirtQueue *vq, uint16_t idx)
435 {
436     g_assert(vq->event);
437 
438     /* vq->avail->used_event */
439     qvirtio_writew(vq->vdev, qts, vq->avail + 4 + (2 * vq->size), idx);
440 }
441 
442 void qvirtio_start_device(QVirtioDevice *vdev)
443 {
444     qvirtio_reset(vdev);
445     qvirtio_set_acknowledge(vdev);
446     qvirtio_set_driver(vdev);
447 }
448 
449 bool qvirtio_is_big_endian(QVirtioDevice *d)
450 {
451     return d->big_endian;
452 }
453