2014-09-01 14:07:55 +04:00
|
|
|
/*
|
|
|
|
* libqos virtio driver
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Marc Marí
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
2016-02-08 21:08:51 +03:00
|
|
|
#include "qemu/osdep.h"
|
2019-10-23 13:04:18 +03:00
|
|
|
#include "qemu/bswap.h"
|
2022-03-30 12:39:05 +03:00
|
|
|
#include "../libqtest.h"
|
2020-08-04 21:00:40 +03:00
|
|
|
#include "virtio.h"
|
2016-05-09 15:47:36 +03:00
|
|
|
#include "standard-headers/linux/virtio_config.h"
|
2016-05-09 15:47:37 +03:00
|
|
|
#include "standard-headers/linux/virtio_ring.h"
|
2014-09-01 14:07:55 +04:00
|
|
|
|
2019-10-23 13:04:18 +03:00
|
|
|
/*
|
|
|
|
* qtest_readX/writeX() functions transfer host endian from/to guest endian.
|
|
|
|
* This works great for Legacy VIRTIO devices where we need guest endian
|
|
|
|
* accesses. For VIRTIO 1.0 the vring is little-endian so the automatic guest
|
|
|
|
* endianness conversion is not wanted.
|
|
|
|
*
|
|
|
|
* The following qvirtio_readX/writeX() functions handle Legacy and VIRTIO 1.0
|
|
|
|
* accesses seamlessly.
|
|
|
|
*/
|
|
|
|
static uint16_t qvirtio_readw(QVirtioDevice *d, QTestState *qts, uint64_t addr)
|
|
|
|
{
|
|
|
|
uint16_t val = qtest_readw(qts, addr);
|
|
|
|
|
|
|
|
if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
|
|
|
|
val = bswap16(val);
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t qvirtio_readl(QVirtioDevice *d, QTestState *qts, uint64_t addr)
|
|
|
|
{
|
|
|
|
uint32_t val = qtest_readl(qts, addr);
|
|
|
|
|
|
|
|
if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
|
|
|
|
val = bswap32(val);
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qvirtio_writew(QVirtioDevice *d, QTestState *qts,
|
|
|
|
uint64_t addr, uint16_t val)
|
|
|
|
{
|
|
|
|
if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
|
|
|
|
val = bswap16(val);
|
|
|
|
}
|
|
|
|
qtest_writew(qts, addr, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qvirtio_writel(QVirtioDevice *d, QTestState *qts,
|
|
|
|
uint64_t addr, uint32_t val)
|
|
|
|
{
|
|
|
|
if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
|
|
|
|
val = bswap32(val);
|
|
|
|
}
|
|
|
|
qtest_writel(qts, addr, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qvirtio_writeq(QVirtioDevice *d, QTestState *qts,
|
|
|
|
uint64_t addr, uint64_t val)
|
|
|
|
{
|
|
|
|
if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
|
|
|
|
val = bswap64(val);
|
|
|
|
}
|
|
|
|
qtest_writeq(qts, addr, val);
|
|
|
|
}
|
|
|
|
|
2016-10-17 13:30:21 +03:00
|
|
|
uint8_t qvirtio_config_readb(QVirtioDevice *d, uint64_t addr)
|
2014-09-01 14:07:55 +04:00
|
|
|
{
|
2019-10-23 13:04:16 +03:00
|
|
|
g_assert_true(d->features_negotiated);
|
2016-10-17 13:30:21 +03:00
|
|
|
return d->bus->config_readb(d, addr);
|
2014-09-01 14:07:55 +04:00
|
|
|
}
|
|
|
|
|
2016-10-17 13:30:21 +03:00
|
|
|
uint16_t qvirtio_config_readw(QVirtioDevice *d, uint64_t addr)
|
2014-09-01 14:07:55 +04:00
|
|
|
{
|
2019-10-23 13:04:16 +03:00
|
|
|
g_assert_true(d->features_negotiated);
|
2016-10-17 13:30:21 +03:00
|
|
|
return d->bus->config_readw(d, addr);
|
2014-09-01 14:07:55 +04:00
|
|
|
}
|
|
|
|
|
2016-10-17 13:30:21 +03:00
|
|
|
uint32_t qvirtio_config_readl(QVirtioDevice *d, uint64_t addr)
|
2014-09-01 14:07:55 +04:00
|
|
|
{
|
2019-10-23 13:04:16 +03:00
|
|
|
g_assert_true(d->features_negotiated);
|
2016-10-17 13:30:21 +03:00
|
|
|
return d->bus->config_readl(d, addr);
|
2014-09-01 14:07:55 +04:00
|
|
|
}
|
|
|
|
|
2016-10-17 13:30:21 +03:00
|
|
|
uint64_t qvirtio_config_readq(QVirtioDevice *d, uint64_t addr)
|
2014-09-01 14:07:55 +04:00
|
|
|
{
|
2019-10-23 13:04:16 +03:00
|
|
|
g_assert_true(d->features_negotiated);
|
2016-10-17 13:30:21 +03:00
|
|
|
return d->bus->config_readq(d, addr);
|
2014-09-01 14:07:55 +04:00
|
|
|
}
|
|
|
|
|
2019-10-23 13:04:12 +03:00
|
|
|
uint64_t qvirtio_get_features(QVirtioDevice *d)
|
2014-09-01 14:07:56 +04:00
|
|
|
{
|
2016-10-17 13:30:21 +03:00
|
|
|
return d->bus->get_features(d);
|
2014-09-01 14:07:56 +04:00
|
|
|
}
|
|
|
|
|
2019-10-23 13:04:12 +03:00
|
|
|
void qvirtio_set_features(QVirtioDevice *d, uint64_t features)
|
2014-09-01 14:07:56 +04:00
|
|
|
{
|
2022-08-02 12:50:07 +03:00
|
|
|
g_assert(!(features & QVIRTIO_F_BAD_FEATURE));
|
|
|
|
|
2018-07-30 23:21:42 +03:00
|
|
|
d->features = features;
|
2016-10-17 13:30:21 +03:00
|
|
|
d->bus->set_features(d, features);
|
2019-10-23 13:04:17 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This could be a separate function for drivers that want to access
|
|
|
|
* configuration space before setting FEATURES_OK, but no existing users
|
|
|
|
* need that and it's less code for callers if this is done implicitly.
|
2022-08-02 12:50:10 +03:00
|
|
|
*/
|
2019-10-23 13:04:17 +03:00
|
|
|
if (features & (1ull << VIRTIO_F_VERSION_1)) {
|
|
|
|
uint8_t status = d->bus->get_status(d) |
|
|
|
|
VIRTIO_CONFIG_S_FEATURES_OK;
|
|
|
|
|
|
|
|
d->bus->set_status(d, status);
|
|
|
|
g_assert_cmphex(d->bus->get_status(d), ==, status);
|
|
|
|
}
|
|
|
|
|
2019-10-23 13:04:16 +03:00
|
|
|
d->features_negotiated = true;
|
2014-09-01 14:07:56 +04:00
|
|
|
}
|
|
|
|
|
2016-10-17 13:30:21 +03:00
|
|
|
QVirtQueue *qvirtqueue_setup(QVirtioDevice *d,
|
|
|
|
QGuestAllocator *alloc, uint16_t index)
|
2014-09-01 14:07:56 +04:00
|
|
|
{
|
2019-10-23 13:04:16 +03:00
|
|
|
g_assert_true(d->features_negotiated);
|
2016-10-17 13:30:21 +03:00
|
|
|
return d->bus->virtqueue_setup(d, alloc, index);
|
2014-09-01 14:07:56 +04:00
|
|
|
}
|
|
|
|
|
2016-05-05 18:53:35 +03:00
|
|
|
void qvirtqueue_cleanup(const QVirtioBus *bus, QVirtQueue *vq,
|
|
|
|
QGuestAllocator *alloc)
|
|
|
|
{
|
|
|
|
return bus->virtqueue_cleanup(vq, alloc);
|
|
|
|
}
|
|
|
|
|
2016-10-17 13:30:21 +03:00
|
|
|
void qvirtio_reset(QVirtioDevice *d)
|
2014-09-01 14:07:55 +04:00
|
|
|
{
|
2016-10-17 13:30:21 +03:00
|
|
|
d->bus->set_status(d, 0);
|
|
|
|
g_assert_cmphex(d->bus->get_status(d), ==, 0);
|
2019-10-23 13:04:16 +03:00
|
|
|
d->features_negotiated = false;
|
2014-09-01 14:07:55 +04:00
|
|
|
}
|
|
|
|
|
2016-10-17 13:30:21 +03:00
|
|
|
void qvirtio_set_acknowledge(QVirtioDevice *d)
|
2014-09-01 14:07:55 +04:00
|
|
|
{
|
2016-10-17 13:30:21 +03:00
|
|
|
d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_ACKNOWLEDGE);
|
|
|
|
g_assert_cmphex(d->bus->get_status(d), ==, VIRTIO_CONFIG_S_ACKNOWLEDGE);
|
2014-09-01 14:07:55 +04:00
|
|
|
}
|
|
|
|
|
2016-10-17 13:30:21 +03:00
|
|
|
void qvirtio_set_driver(QVirtioDevice *d)
|
2014-09-01 14:07:55 +04:00
|
|
|
{
|
2016-10-17 13:30:21 +03:00
|
|
|
d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_DRIVER);
|
|
|
|
g_assert_cmphex(d->bus->get_status(d), ==,
|
2016-05-09 15:47:36 +03:00
|
|
|
VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_ACKNOWLEDGE);
|
2014-09-01 14:07:55 +04:00
|
|
|
}
|
2014-09-01 14:07:56 +04:00
|
|
|
|
2016-10-17 13:30:21 +03:00
|
|
|
void qvirtio_set_driver_ok(QVirtioDevice *d)
|
2014-09-01 14:07:56 +04:00
|
|
|
{
|
2016-10-17 13:30:21 +03:00
|
|
|
d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_DRIVER_OK);
|
|
|
|
g_assert_cmphex(d->bus->get_status(d), ==, VIRTIO_CONFIG_S_DRIVER_OK |
|
2019-10-23 13:04:17 +03:00
|
|
|
VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_ACKNOWLEDGE |
|
|
|
|
(d->features & (1ull << VIRTIO_F_VERSION_1) ?
|
|
|
|
VIRTIO_CONFIG_S_FEATURES_OK : 0));
|
2014-09-01 14:07:56 +04:00
|
|
|
}
|
|
|
|
|
2019-09-03 09:18:46 +03:00
|
|
|
void qvirtio_wait_queue_isr(QTestState *qts, QVirtioDevice *d,
|
2014-09-29 19:40:12 +04:00
|
|
|
QVirtQueue *vq, gint64 timeout_us)
|
2014-09-01 14:07:59 +04:00
|
|
|
{
|
2014-09-29 19:40:12 +04:00
|
|
|
gint64 start_time = g_get_monotonic_time();
|
|
|
|
|
|
|
|
for (;;) {
|
2019-09-03 09:18:46 +03:00
|
|
|
qtest_clock_step(qts, 100);
|
2016-10-17 13:30:21 +03:00
|
|
|
if (d->bus->get_queue_isr_status(d, vq)) {
|
2014-09-29 19:40:12 +04:00
|
|
|
return;
|
2014-09-01 14:07:59 +04:00
|
|
|
}
|
2014-09-29 19:40:12 +04:00
|
|
|
g_assert(g_get_monotonic_time() - start_time <= timeout_us);
|
|
|
|
}
|
2014-09-01 14:07:59 +04:00
|
|
|
}
|
|
|
|
|
2014-09-29 19:40:11 +04:00
|
|
|
/* Wait for the status byte at given guest memory address to be set
|
|
|
|
*
|
|
|
|
* The virtqueue interrupt must not be raised, making this useful for testing
|
|
|
|
* event_index functionality.
|
|
|
|
*/
|
2019-05-18 11:23:24 +03:00
|
|
|
uint8_t qvirtio_wait_status_byte_no_isr(QTestState *qts, QVirtioDevice *d,
|
2014-09-29 19:40:11 +04:00
|
|
|
QVirtQueue *vq,
|
|
|
|
uint64_t addr,
|
|
|
|
gint64 timeout_us)
|
|
|
|
{
|
|
|
|
gint64 start_time = g_get_monotonic_time();
|
|
|
|
uint8_t val;
|
|
|
|
|
2019-09-03 09:18:46 +03:00
|
|
|
while ((val = qtest_readb(qts, addr)) == 0xff) {
|
|
|
|
qtest_clock_step(qts, 100);
|
2016-10-17 13:30:21 +03:00
|
|
|
g_assert(!d->bus->get_queue_isr_status(d, vq));
|
2014-09-29 19:40:11 +04:00
|
|
|
g_assert(g_get_monotonic_time() - start_time <= timeout_us);
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2017-06-28 21:47:20 +03:00
|
|
|
/*
|
|
|
|
* qvirtio_wait_used_elem:
|
|
|
|
* @desc_idx: The next expected vq->desc[] index in the used ring
|
2018-02-01 23:21:28 +03:00
|
|
|
* @len: A pointer that is filled with the length written into the buffer, may
|
|
|
|
* be NULL
|
2017-06-28 21:47:20 +03:00
|
|
|
* @timeout_us: How many microseconds to wait before failing
|
|
|
|
*
|
|
|
|
* This function waits for the next completed request on the used ring.
|
|
|
|
*/
|
2019-05-18 11:23:24 +03:00
|
|
|
void qvirtio_wait_used_elem(QTestState *qts, QVirtioDevice *d,
|
2017-06-28 21:47:20 +03:00
|
|
|
QVirtQueue *vq,
|
|
|
|
uint32_t desc_idx,
|
2018-02-01 23:21:28 +03:00
|
|
|
uint32_t *len,
|
2017-06-28 21:47:20 +03:00
|
|
|
gint64 timeout_us)
|
|
|
|
{
|
|
|
|
gint64 start_time = g_get_monotonic_time();
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
uint32_t got_desc_idx;
|
|
|
|
|
2019-09-03 09:18:46 +03:00
|
|
|
qtest_clock_step(qts, 100);
|
2017-06-28 21:47:20 +03:00
|
|
|
|
|
|
|
if (d->bus->get_queue_isr_status(d, vq) &&
|
2019-05-18 11:23:24 +03:00
|
|
|
qvirtqueue_get_buf(qts, vq, &got_desc_idx, len)) {
|
2017-06-28 21:47:20 +03:00
|
|
|
g_assert_cmpint(got_desc_idx, ==, desc_idx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert(g_get_monotonic_time() - start_time <= timeout_us);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-17 13:30:21 +03:00
|
|
|
void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us)
|
2014-09-01 14:07:56 +04:00
|
|
|
{
|
2019-09-03 09:18:46 +03:00
|
|
|
d->bus->wait_config_isr_status(d, timeout_us);
|
2014-09-01 14:07:56 +04:00
|
|
|
}
|
|
|
|
|
2019-05-15 20:43:23 +03:00
|
|
|
void qvring_init(QTestState *qts, const QGuestAllocator *alloc, QVirtQueue *vq,
|
|
|
|
uint64_t addr)
|
2014-09-01 14:07:56 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
vq->desc = addr;
|
2016-05-09 15:47:38 +03:00
|
|
|
vq->avail = vq->desc + vq->size * sizeof(struct vring_desc);
|
2014-09-01 14:07:56 +04:00
|
|
|
vq->used = (uint64_t)((vq->avail + sizeof(uint16_t) * (3 + vq->size)
|
|
|
|
+ vq->align - 1) & ~(vq->align - 1));
|
|
|
|
|
|
|
|
for (i = 0; i < vq->size - 1; i++) {
|
|
|
|
/* vq->desc[i].addr */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * i), 0);
|
2014-09-01 14:07:56 +04:00
|
|
|
/* vq->desc[i].next */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writew(vq->vdev, qts, vq->desc + (16 * i) + 14, i + 1);
|
2014-09-01 14:07:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vq->avail->flags */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writew(vq->vdev, qts, vq->avail, 0);
|
2014-09-01 14:07:56 +04:00
|
|
|
/* vq->avail->idx */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writew(vq->vdev, qts, vq->avail + 2, 0);
|
2014-09-01 14:08:00 +04:00
|
|
|
/* vq->avail->used_event */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writew(vq->vdev, qts, vq->avail + 4 + (2 * vq->size), 0);
|
2014-09-01 14:07:56 +04:00
|
|
|
|
|
|
|
/* vq->used->flags */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writew(vq->vdev, qts, vq->used, 0);
|
2022-04-06 20:33:56 +03:00
|
|
|
/* vq->used->idx */
|
|
|
|
qvirtio_writew(vq->vdev, qts, vq->used + 2, 0);
|
2014-09-01 14:08:00 +04:00
|
|
|
/* vq->used->avail_event */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writew(vq->vdev, qts, vq->used + 2 +
|
|
|
|
sizeof(struct vring_used_elem) * vq->size, 0);
|
2014-09-01 14:07:56 +04:00
|
|
|
}
|
|
|
|
|
2019-05-18 11:23:24 +03:00
|
|
|
QVRingIndirectDesc *qvring_indirect_desc_setup(QTestState *qs, QVirtioDevice *d,
|
|
|
|
QGuestAllocator *alloc,
|
|
|
|
uint16_t elem)
|
2014-09-01 14:07:57 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
QVRingIndirectDesc *indirect = g_malloc(sizeof(*indirect));
|
|
|
|
|
|
|
|
indirect->index = 0;
|
|
|
|
indirect->elem = elem;
|
2016-05-09 15:47:38 +03:00
|
|
|
indirect->desc = guest_alloc(alloc, sizeof(struct vring_desc) * elem);
|
2014-09-01 14:07:57 +04:00
|
|
|
|
|
|
|
for (i = 0; i < elem - 1; ++i) {
|
|
|
|
/* indirect->desc[i].addr */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writeq(d, qs, indirect->desc + (16 * i), 0);
|
2014-09-01 14:07:57 +04:00
|
|
|
/* indirect->desc[i].flags */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writew(d, qs, indirect->desc + (16 * i) + 12,
|
|
|
|
VRING_DESC_F_NEXT);
|
2014-09-01 14:07:57 +04:00
|
|
|
/* indirect->desc[i].next */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writew(d, qs, indirect->desc + (16 * i) + 14, i + 1);
|
2014-09-01 14:07:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return indirect;
|
|
|
|
}
|
|
|
|
|
2019-10-23 13:04:18 +03:00
|
|
|
void qvring_indirect_desc_add(QVirtioDevice *d, QTestState *qts,
|
|
|
|
QVRingIndirectDesc *indirect,
|
2019-05-18 11:23:24 +03:00
|
|
|
uint64_t data, uint32_t len, bool write)
|
2014-09-01 14:07:57 +04:00
|
|
|
{
|
|
|
|
uint16_t flags;
|
|
|
|
|
|
|
|
g_assert_cmpint(indirect->index, <, indirect->elem);
|
|
|
|
|
2019-10-23 13:04:18 +03:00
|
|
|
flags = qvirtio_readw(d, qts, indirect->desc +
|
|
|
|
(16 * indirect->index) + 12);
|
2014-09-01 14:07:57 +04:00
|
|
|
|
|
|
|
if (write) {
|
2016-05-09 15:47:37 +03:00
|
|
|
flags |= VRING_DESC_F_WRITE;
|
2014-09-01 14:07:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* indirect->desc[indirect->index].addr */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writeq(d, qts, indirect->desc + (16 * indirect->index), data);
|
2014-09-01 14:07:57 +04:00
|
|
|
/* indirect->desc[indirect->index].len */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writel(d, qts, indirect->desc + (16 * indirect->index) + 8, len);
|
2014-09-01 14:07:57 +04:00
|
|
|
/* indirect->desc[indirect->index].flags */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writew(d, qts, indirect->desc + (16 * indirect->index) + 12,
|
|
|
|
flags);
|
2014-09-01 14:07:57 +04:00
|
|
|
|
|
|
|
indirect->index++;
|
|
|
|
}
|
|
|
|
|
2019-05-18 11:23:24 +03:00
|
|
|
uint32_t qvirtqueue_add(QTestState *qts, QVirtQueue *vq, uint64_t data,
|
|
|
|
uint32_t len, bool write, bool next)
|
2014-09-01 14:07:56 +04:00
|
|
|
{
|
|
|
|
uint16_t flags = 0;
|
|
|
|
vq->num_free--;
|
|
|
|
|
|
|
|
if (write) {
|
2016-05-09 15:47:37 +03:00
|
|
|
flags |= VRING_DESC_F_WRITE;
|
2014-09-01 14:07:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (next) {
|
2016-05-09 15:47:37 +03:00
|
|
|
flags |= VRING_DESC_F_NEXT;
|
2014-09-01 14:07:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vq->desc[vq->free_head].addr */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * vq->free_head), data);
|
2014-09-01 14:07:56 +04:00
|
|
|
/* vq->desc[vq->free_head].len */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writel(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 8, len);
|
2014-09-01 14:07:56 +04:00
|
|
|
/* vq->desc[vq->free_head].flags */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writew(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 12, flags);
|
2014-09-01 14:07:56 +04:00
|
|
|
|
|
|
|
return vq->free_head++; /* Return and increase, in this order */
|
|
|
|
}
|
|
|
|
|
2019-05-18 11:23:24 +03:00
|
|
|
uint32_t qvirtqueue_add_indirect(QTestState *qts, QVirtQueue *vq,
|
|
|
|
QVRingIndirectDesc *indirect)
|
2014-09-01 14:07:57 +04:00
|
|
|
{
|
|
|
|
g_assert(vq->indirect);
|
|
|
|
g_assert_cmpint(vq->size, >=, indirect->elem);
|
|
|
|
g_assert_cmpint(indirect->index, ==, indirect->elem);
|
|
|
|
|
|
|
|
vq->num_free--;
|
|
|
|
|
|
|
|
/* vq->desc[vq->free_head].addr */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * vq->free_head),
|
|
|
|
indirect->desc);
|
2014-09-01 14:07:57 +04:00
|
|
|
/* vq->desc[vq->free_head].len */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writel(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 8,
|
|
|
|
sizeof(struct vring_desc) * indirect->elem);
|
2014-09-01 14:07:57 +04:00
|
|
|
/* vq->desc[vq->free_head].flags */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writew(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 12,
|
|
|
|
VRING_DESC_F_INDIRECT);
|
2014-09-01 14:07:57 +04:00
|
|
|
|
|
|
|
return vq->free_head++; /* Return and increase, in this order */
|
|
|
|
}
|
|
|
|
|
2019-05-18 11:23:24 +03:00
|
|
|
void qvirtqueue_kick(QTestState *qts, QVirtioDevice *d, QVirtQueue *vq,
|
|
|
|
uint32_t free_head)
|
2014-09-01 14:07:56 +04:00
|
|
|
{
|
|
|
|
/* vq->avail->idx */
|
2019-10-23 13:04:18 +03:00
|
|
|
uint16_t idx = qvirtio_readw(d, qts, vq->avail + 2);
|
2014-09-01 14:08:00 +04:00
|
|
|
/* vq->used->flags */
|
|
|
|
uint16_t flags;
|
|
|
|
/* vq->used->avail_event */
|
|
|
|
uint16_t avail_event;
|
2014-09-01 14:07:56 +04:00
|
|
|
|
|
|
|
/* vq->avail->ring[idx % vq->size] */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writew(d, qts, vq->avail + 4 + (2 * (idx % vq->size)), free_head);
|
2014-09-01 14:07:56 +04:00
|
|
|
/* vq->avail->idx */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writew(d, qts, vq->avail + 2, idx + 1);
|
2014-09-01 14:07:56 +04:00
|
|
|
|
2014-09-01 14:08:00 +04:00
|
|
|
/* Must read after idx is updated */
|
2019-10-23 13:04:18 +03:00
|
|
|
flags = qvirtio_readw(d, qts, vq->avail);
|
|
|
|
avail_event = qvirtio_readw(d, qts, vq->used + 4 +
|
|
|
|
sizeof(struct vring_used_elem) * vq->size);
|
2014-09-01 14:08:00 +04:00
|
|
|
|
|
|
|
/* < 1 because we add elements to avail queue one by one */
|
2016-05-09 15:47:37 +03:00
|
|
|
if ((flags & VRING_USED_F_NO_NOTIFY) == 0 &&
|
2014-09-01 14:08:00 +04:00
|
|
|
(!vq->event || (uint16_t)(idx-avail_event) < 1)) {
|
2016-10-17 13:30:21 +03:00
|
|
|
d->bus->virtqueue_kick(d, vq);
|
2014-09-01 14:08:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-28 21:47:20 +03:00
|
|
|
/*
|
|
|
|
* qvirtqueue_get_buf:
|
|
|
|
* @desc_idx: A pointer that is filled with the vq->desc[] index, may be NULL
|
2018-02-01 23:21:28 +03:00
|
|
|
* @len: A pointer that is filled with the length written into the buffer, may
|
|
|
|
* be NULL
|
2017-06-28 21:47:20 +03:00
|
|
|
*
|
|
|
|
* This function gets the next used element if there is one ready.
|
|
|
|
*
|
|
|
|
* Returns: true if an element was ready, false otherwise
|
|
|
|
*/
|
2019-05-18 11:23:24 +03:00
|
|
|
bool qvirtqueue_get_buf(QTestState *qts, QVirtQueue *vq, uint32_t *desc_idx,
|
|
|
|
uint32_t *len)
|
2017-06-28 21:47:20 +03:00
|
|
|
{
|
|
|
|
uint16_t idx;
|
2019-05-18 11:23:24 +03:00
|
|
|
uint64_t elem_addr, addr;
|
2017-06-28 21:47:20 +03:00
|
|
|
|
2019-10-23 13:04:18 +03:00
|
|
|
idx = qvirtio_readw(vq->vdev, qts,
|
|
|
|
vq->used + offsetof(struct vring_used, idx));
|
2017-06-28 21:47:20 +03:00
|
|
|
if (idx == vq->last_used_idx) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-01 23:21:28 +03:00
|
|
|
elem_addr = vq->used +
|
|
|
|
offsetof(struct vring_used, ring) +
|
|
|
|
(vq->last_used_idx % vq->size) *
|
|
|
|
sizeof(struct vring_used_elem);
|
2017-06-28 21:47:20 +03:00
|
|
|
|
2018-02-01 23:21:28 +03:00
|
|
|
if (desc_idx) {
|
2019-05-18 11:23:24 +03:00
|
|
|
addr = elem_addr + offsetof(struct vring_used_elem, id);
|
2019-10-23 13:04:18 +03:00
|
|
|
*desc_idx = qvirtio_readl(vq->vdev, qts, addr);
|
2017-06-28 21:47:20 +03:00
|
|
|
}
|
|
|
|
|
2018-02-01 23:21:28 +03:00
|
|
|
if (len) {
|
2019-05-18 11:23:24 +03:00
|
|
|
addr = elem_addr + offsetof(struct vring_used_elem, len);
|
2019-10-23 13:04:18 +03:00
|
|
|
*len = qvirtio_readw(vq->vdev, qts, addr);
|
2018-02-01 23:21:28 +03:00
|
|
|
}
|
|
|
|
|
2017-06-28 21:47:20 +03:00
|
|
|
vq->last_used_idx++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-18 11:23:24 +03:00
|
|
|
void qvirtqueue_set_used_event(QTestState *qts, QVirtQueue *vq, uint16_t idx)
|
2014-09-01 14:08:00 +04:00
|
|
|
{
|
|
|
|
g_assert(vq->event);
|
|
|
|
|
|
|
|
/* vq->avail->used_event */
|
2019-10-23 13:04:18 +03:00
|
|
|
qvirtio_writew(vq->vdev, qts, vq->avail + 4 + (2 * vq->size), idx);
|
2014-09-01 14:07:56 +04:00
|
|
|
}
|
2017-08-31 17:40:08 +03:00
|
|
|
|
2018-07-25 17:25:43 +03:00
|
|
|
void qvirtio_start_device(QVirtioDevice *vdev)
|
|
|
|
{
|
|
|
|
qvirtio_reset(vdev);
|
|
|
|
qvirtio_set_acknowledge(vdev);
|
|
|
|
qvirtio_set_driver(vdev);
|
|
|
|
}
|
2018-10-07 10:37:44 +03:00
|
|
|
|
|
|
|
bool qvirtio_is_big_endian(QVirtioDevice *d)
|
|
|
|
{
|
|
|
|
return d->big_endian;
|
|
|
|
}
|