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