Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / tests / qtest / libqos / virtio.c
bloba21b6eee9cd637ef2ae0b49fe1402a7058fafdde
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 "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"
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.
23 * The following qvirtio_readX/writeX() functions handle Legacy and VIRTIO 1.0
24 * accesses seamlessly.
26 static uint16_t qvirtio_readw(QVirtioDevice *d, QTestState *qts, uint64_t addr)
28 uint16_t val = qtest_readw(qts, addr);
30 if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
31 val = bswap16(val);
33 return val;
36 static uint32_t qvirtio_readl(QVirtioDevice *d, QTestState *qts, uint64_t addr)
38 uint32_t val = qtest_readl(qts, addr);
40 if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
41 val = bswap32(val);
43 return val;
46 static void qvirtio_writew(QVirtioDevice *d, QTestState *qts,
47 uint64_t addr, uint16_t val)
49 if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
50 val = bswap16(val);
52 qtest_writew(qts, addr, val);
55 static void qvirtio_writel(QVirtioDevice *d, QTestState *qts,
56 uint64_t addr, uint32_t val)
58 if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
59 val = bswap32(val);
61 qtest_writel(qts, addr, val);
64 static void qvirtio_writeq(QVirtioDevice *d, QTestState *qts,
65 uint64_t addr, uint64_t val)
67 if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
68 val = bswap64(val);
70 qtest_writeq(qts, addr, val);
73 uint8_t qvirtio_config_readb(QVirtioDevice *d, uint64_t addr)
75 g_assert_true(d->features_negotiated);
76 return d->bus->config_readb(d, addr);
79 uint16_t qvirtio_config_readw(QVirtioDevice *d, uint64_t addr)
81 g_assert_true(d->features_negotiated);
82 return d->bus->config_readw(d, addr);
85 uint32_t qvirtio_config_readl(QVirtioDevice *d, uint64_t addr)
87 g_assert_true(d->features_negotiated);
88 return d->bus->config_readl(d, addr);
91 uint64_t qvirtio_config_readq(QVirtioDevice *d, uint64_t addr)
93 g_assert_true(d->features_negotiated);
94 return d->bus->config_readq(d, addr);
97 uint64_t qvirtio_get_features(QVirtioDevice *d)
99 return d->bus->get_features(d);
102 void qvirtio_set_features(QVirtioDevice *d, uint64_t features)
104 g_assert(!(features & QVIRTIO_F_BAD_FEATURE));
106 d->features = features;
107 d->bus->set_features(d, features);
110 * This could be a separate function for drivers that want to access
111 * configuration space before setting FEATURES_OK, but no existing users
112 * need that and it's less code for callers if this is done implicitly.
114 if (features & (1ull << VIRTIO_F_VERSION_1)) {
115 uint8_t status = d->bus->get_status(d) |
116 VIRTIO_CONFIG_S_FEATURES_OK;
118 d->bus->set_status(d, status);
119 g_assert_cmphex(d->bus->get_status(d), ==, status);
122 d->features_negotiated = true;
125 QVirtQueue *qvirtqueue_setup(QVirtioDevice *d,
126 QGuestAllocator *alloc, uint16_t index)
128 g_assert_true(d->features_negotiated);
129 return d->bus->virtqueue_setup(d, alloc, index);
132 void qvirtqueue_cleanup(const QVirtioBus *bus, QVirtQueue *vq,
133 QGuestAllocator *alloc)
135 return bus->virtqueue_cleanup(vq, alloc);
138 void qvirtio_reset(QVirtioDevice *d)
140 d->bus->set_status(d, 0);
141 g_assert_cmphex(d->bus->get_status(d), ==, 0);
142 d->features_negotiated = false;
145 void qvirtio_set_acknowledge(QVirtioDevice *d)
147 d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_ACKNOWLEDGE);
148 g_assert_cmphex(d->bus->get_status(d), ==, VIRTIO_CONFIG_S_ACKNOWLEDGE);
151 void qvirtio_set_driver(QVirtioDevice *d)
153 d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_DRIVER);
154 g_assert_cmphex(d->bus->get_status(d), ==,
155 VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_ACKNOWLEDGE);
158 void qvirtio_set_driver_ok(QVirtioDevice *d)
160 d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_DRIVER_OK);
161 g_assert_cmphex(d->bus->get_status(d), ==, VIRTIO_CONFIG_S_DRIVER_OK |
162 VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_ACKNOWLEDGE |
163 (d->features & (1ull << VIRTIO_F_VERSION_1) ?
164 VIRTIO_CONFIG_S_FEATURES_OK : 0));
167 void qvirtio_wait_queue_isr(QTestState *qts, QVirtioDevice *d,
168 QVirtQueue *vq, gint64 timeout_us)
170 gint64 start_time = g_get_monotonic_time();
172 for (;;) {
173 qtest_clock_step(qts, 100);
174 if (d->bus->get_queue_isr_status(d, vq)) {
175 return;
177 g_assert(g_get_monotonic_time() - start_time <= timeout_us);
181 /* Wait for the status byte at given guest memory address to be set
183 * The virtqueue interrupt must not be raised, making this useful for testing
184 * event_index functionality.
186 uint8_t qvirtio_wait_status_byte_no_isr(QTestState *qts, QVirtioDevice *d,
187 QVirtQueue *vq,
188 uint64_t addr,
189 gint64 timeout_us)
191 gint64 start_time = g_get_monotonic_time();
192 uint8_t val;
194 while ((val = qtest_readb(qts, addr)) == 0xff) {
195 qtest_clock_step(qts, 100);
196 g_assert(!d->bus->get_queue_isr_status(d, vq));
197 g_assert(g_get_monotonic_time() - start_time <= timeout_us);
199 return val;
203 * qvirtio_wait_used_elem:
204 * @desc_idx: The next expected vq->desc[] index in the used ring
205 * @len: A pointer that is filled with the length written into the buffer, may
206 * be NULL
207 * @timeout_us: How many microseconds to wait before failing
209 * This function waits for the next completed request on the used ring.
211 void qvirtio_wait_used_elem(QTestState *qts, QVirtioDevice *d,
212 QVirtQueue *vq,
213 uint32_t desc_idx,
214 uint32_t *len,
215 gint64 timeout_us)
217 gint64 start_time = g_get_monotonic_time();
219 for (;;) {
220 uint32_t got_desc_idx;
222 qtest_clock_step(qts, 100);
224 if (d->bus->get_queue_isr_status(d, vq) &&
225 qvirtqueue_get_buf(qts, vq, &got_desc_idx, len)) {
226 g_assert_cmpint(got_desc_idx, ==, desc_idx);
227 return;
230 g_assert(g_get_monotonic_time() - start_time <= timeout_us);
234 void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us)
236 d->bus->wait_config_isr_status(d, timeout_us);
239 void qvring_init(QTestState *qts, const QGuestAllocator *alloc, QVirtQueue *vq,
240 uint64_t addr)
242 int i;
244 vq->desc = addr;
245 vq->avail = vq->desc + vq->size * sizeof(struct vring_desc);
246 vq->used = (uint64_t)((vq->avail + sizeof(uint16_t) * (3 + vq->size)
247 + vq->align - 1) & ~(vq->align - 1));
249 for (i = 0; i < vq->size - 1; i++) {
250 /* vq->desc[i].addr */
251 qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * i), 0);
252 /* vq->desc[i].next */
253 qvirtio_writew(vq->vdev, qts, vq->desc + (16 * i) + 14, i + 1);
256 /* vq->avail->flags */
257 qvirtio_writew(vq->vdev, qts, vq->avail, 0);
258 /* vq->avail->idx */
259 qvirtio_writew(vq->vdev, qts, vq->avail + 2, 0);
260 /* vq->avail->used_event */
261 qvirtio_writew(vq->vdev, qts, vq->avail + 4 + (2 * vq->size), 0);
263 /* vq->used->flags */
264 qvirtio_writew(vq->vdev, qts, vq->used, 0);
265 /* vq->used->idx */
266 qvirtio_writew(vq->vdev, qts, vq->used + 2, 0);
267 /* vq->used->avail_event */
268 qvirtio_writew(vq->vdev, qts, vq->used + 4 +
269 sizeof(struct vring_used_elem) * vq->size, 0);
272 QVRingIndirectDesc *qvring_indirect_desc_setup(QTestState *qs, QVirtioDevice *d,
273 QGuestAllocator *alloc,
274 uint16_t elem)
276 int i;
277 QVRingIndirectDesc *indirect = g_malloc(sizeof(*indirect));
279 indirect->index = 0;
280 indirect->elem = elem;
281 indirect->desc = guest_alloc(alloc, sizeof(struct vring_desc) * elem);
283 for (i = 0; i < elem; ++i) {
284 /* indirect->desc[i].addr */
285 qvirtio_writeq(d, qs, indirect->desc + (16 * i), 0);
288 * If it's not the last element of the ring, set
289 * the chain (VRING_DESC_F_NEXT) flag and
290 * desc->next. Clear the last element - there's
291 * no guarantee that guest_alloc() will do it.
293 if (i != elem - 1) {
294 /* indirect->desc[i].flags */
295 qvirtio_writew(d, qs, indirect->desc + (16 * i) + 12,
296 VRING_DESC_F_NEXT);
298 /* indirect->desc[i].next */
299 qvirtio_writew(d, qs, indirect->desc + (16 * i) + 14, i + 1);
300 } else {
301 qvirtio_writew(d, qs, indirect->desc + (16 * i) + 12, 0);
302 qvirtio_writew(d, qs, indirect->desc + (16 * i) + 14, 0);
306 return indirect;
309 void qvring_indirect_desc_add(QVirtioDevice *d, QTestState *qts,
310 QVRingIndirectDesc *indirect,
311 uint64_t data, uint32_t len, bool write)
313 uint16_t flags;
315 g_assert_cmpint(indirect->index, <, indirect->elem);
317 flags = qvirtio_readw(d, qts, indirect->desc +
318 (16 * indirect->index) + 12);
320 if (write) {
321 flags |= VRING_DESC_F_WRITE;
324 /* indirect->desc[indirect->index].addr */
325 qvirtio_writeq(d, qts, indirect->desc + (16 * indirect->index), data);
326 /* indirect->desc[indirect->index].len */
327 qvirtio_writel(d, qts, indirect->desc + (16 * indirect->index) + 8, len);
328 /* indirect->desc[indirect->index].flags */
329 qvirtio_writew(d, qts, indirect->desc + (16 * indirect->index) + 12,
330 flags);
332 indirect->index++;
335 uint32_t qvirtqueue_add(QTestState *qts, QVirtQueue *vq, uint64_t data,
336 uint32_t len, bool write, bool next)
338 uint16_t flags = 0;
339 vq->num_free--;
341 if (write) {
342 flags |= VRING_DESC_F_WRITE;
345 if (next) {
346 flags |= VRING_DESC_F_NEXT;
349 /* vq->desc[vq->free_head].addr */
350 qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * vq->free_head), data);
351 /* vq->desc[vq->free_head].len */
352 qvirtio_writel(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 8, len);
353 /* vq->desc[vq->free_head].flags */
354 qvirtio_writew(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 12, flags);
356 return vq->free_head++; /* Return and increase, in this order */
359 uint32_t qvirtqueue_add_indirect(QTestState *qts, QVirtQueue *vq,
360 QVRingIndirectDesc *indirect)
362 g_assert(vq->indirect);
363 g_assert_cmpint(vq->size, >=, indirect->elem);
364 g_assert_cmpint(indirect->index, ==, indirect->elem);
366 vq->num_free--;
368 /* vq->desc[vq->free_head].addr */
369 qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * vq->free_head),
370 indirect->desc);
371 /* vq->desc[vq->free_head].len */
372 qvirtio_writel(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 8,
373 sizeof(struct vring_desc) * indirect->elem);
374 /* vq->desc[vq->free_head].flags */
375 qvirtio_writew(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 12,
376 VRING_DESC_F_INDIRECT);
378 return vq->free_head++; /* Return and increase, in this order */
381 void qvirtqueue_kick(QTestState *qts, QVirtioDevice *d, QVirtQueue *vq,
382 uint32_t free_head)
384 /* vq->avail->idx */
385 uint16_t idx = qvirtio_readw(d, qts, vq->avail + 2);
386 /* vq->used->flags */
387 uint16_t flags;
388 /* vq->used->avail_event */
389 uint16_t avail_event;
391 /* vq->avail->ring[idx % vq->size] */
392 qvirtio_writew(d, qts, vq->avail + 4 + (2 * (idx % vq->size)), free_head);
393 /* vq->avail->idx */
394 qvirtio_writew(d, qts, vq->avail + 2, idx + 1);
396 /* Must read after idx is updated */
397 flags = qvirtio_readw(d, qts, vq->used);
398 avail_event = qvirtio_readw(d, qts, vq->used + 4 +
399 sizeof(struct vring_used_elem) * vq->size);
401 /* < 1 because we add elements to avail queue one by one */
402 if ((flags & VRING_USED_F_NO_NOTIFY) == 0 &&
403 (!vq->event || (uint16_t)(idx-avail_event) < 1)) {
404 d->bus->virtqueue_kick(d, vq);
409 * qvirtqueue_get_buf:
410 * @desc_idx: A pointer that is filled with the vq->desc[] index, may be NULL
411 * @len: A pointer that is filled with the length written into the buffer, may
412 * be NULL
414 * This function gets the next used element if there is one ready.
416 * Returns: true if an element was ready, false otherwise
418 bool qvirtqueue_get_buf(QTestState *qts, QVirtQueue *vq, uint32_t *desc_idx,
419 uint32_t *len)
421 uint16_t idx;
422 uint64_t elem_addr, addr;
424 idx = qvirtio_readw(vq->vdev, qts,
425 vq->used + offsetof(struct vring_used, idx));
426 if (idx == vq->last_used_idx) {
427 return false;
430 elem_addr = vq->used +
431 offsetof(struct vring_used, ring) +
432 (vq->last_used_idx % vq->size) *
433 sizeof(struct vring_used_elem);
435 if (desc_idx) {
436 addr = elem_addr + offsetof(struct vring_used_elem, id);
437 *desc_idx = qvirtio_readl(vq->vdev, qts, addr);
440 if (len) {
441 addr = elem_addr + offsetof(struct vring_used_elem, len);
442 *len = qvirtio_readw(vq->vdev, qts, addr);
445 vq->last_used_idx++;
446 return true;
449 void qvirtqueue_set_used_event(QTestState *qts, QVirtQueue *vq, uint16_t idx)
451 g_assert(vq->event);
453 /* vq->avail->used_event */
454 qvirtio_writew(vq->vdev, qts, vq->avail + 4 + (2 * vq->size), idx);
457 void qvirtio_start_device(QVirtioDevice *vdev)
459 qvirtio_reset(vdev);
460 qvirtio_set_acknowledge(vdev);
461 qvirtio_set_driver(vdev);
464 bool qvirtio_is_big_endian(QVirtioDevice *d)
466 return d->big_endian;