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.
10 #include "qemu/osdep.h"
12 #include "libqos/virtio.h"
13 #include "standard-headers/linux/virtio_config.h"
14 #include "standard-headers/linux/virtio_ring.h"
16 uint8_t qvirtio_config_readb(QVirtioDevice
*d
, uint64_t addr
)
18 return d
->bus
->config_readb(d
, addr
);
21 uint16_t qvirtio_config_readw(QVirtioDevice
*d
, uint64_t addr
)
23 return d
->bus
->config_readw(d
, addr
);
26 uint32_t qvirtio_config_readl(QVirtioDevice
*d
, uint64_t addr
)
28 return d
->bus
->config_readl(d
, addr
);
31 uint64_t qvirtio_config_readq(QVirtioDevice
*d
, uint64_t addr
)
33 return d
->bus
->config_readq(d
, addr
);
36 uint32_t qvirtio_get_features(QVirtioDevice
*d
)
38 return d
->bus
->get_features(d
);
41 void qvirtio_set_features(QVirtioDevice
*d
, uint32_t features
)
43 d
->features
= features
;
44 d
->bus
->set_features(d
, features
);
47 QVirtQueue
*qvirtqueue_setup(QVirtioDevice
*d
,
48 QGuestAllocator
*alloc
, uint16_t index
)
50 return d
->bus
->virtqueue_setup(d
, alloc
, index
);
53 void qvirtqueue_cleanup(const QVirtioBus
*bus
, QVirtQueue
*vq
,
54 QGuestAllocator
*alloc
)
56 return bus
->virtqueue_cleanup(vq
, alloc
);
59 void qvirtio_reset(QVirtioDevice
*d
)
61 d
->bus
->set_status(d
, 0);
62 g_assert_cmphex(d
->bus
->get_status(d
), ==, 0);
65 void qvirtio_set_acknowledge(QVirtioDevice
*d
)
67 d
->bus
->set_status(d
, d
->bus
->get_status(d
) | VIRTIO_CONFIG_S_ACKNOWLEDGE
);
68 g_assert_cmphex(d
->bus
->get_status(d
), ==, VIRTIO_CONFIG_S_ACKNOWLEDGE
);
71 void qvirtio_set_driver(QVirtioDevice
*d
)
73 d
->bus
->set_status(d
, d
->bus
->get_status(d
) | VIRTIO_CONFIG_S_DRIVER
);
74 g_assert_cmphex(d
->bus
->get_status(d
), ==,
75 VIRTIO_CONFIG_S_DRIVER
| VIRTIO_CONFIG_S_ACKNOWLEDGE
);
78 void qvirtio_set_driver_ok(QVirtioDevice
*d
)
80 d
->bus
->set_status(d
, d
->bus
->get_status(d
) | VIRTIO_CONFIG_S_DRIVER_OK
);
81 g_assert_cmphex(d
->bus
->get_status(d
), ==, VIRTIO_CONFIG_S_DRIVER_OK
|
82 VIRTIO_CONFIG_S_DRIVER
| VIRTIO_CONFIG_S_ACKNOWLEDGE
);
85 void qvirtio_wait_queue_isr(QVirtioDevice
*d
,
86 QVirtQueue
*vq
, gint64 timeout_us
)
88 gint64 start_time
= g_get_monotonic_time();
92 if (d
->bus
->get_queue_isr_status(d
, vq
)) {
95 g_assert(g_get_monotonic_time() - start_time
<= timeout_us
);
99 /* Wait for the status byte at given guest memory address to be set
101 * The virtqueue interrupt must not be raised, making this useful for testing
102 * event_index functionality.
104 uint8_t qvirtio_wait_status_byte_no_isr(QTestState
*qts
, QVirtioDevice
*d
,
109 gint64 start_time
= g_get_monotonic_time();
112 while ((val
= readb(addr
)) == 0xff) {
114 g_assert(!d
->bus
->get_queue_isr_status(d
, vq
));
115 g_assert(g_get_monotonic_time() - start_time
<= timeout_us
);
121 * qvirtio_wait_used_elem:
122 * @desc_idx: The next expected vq->desc[] index in the used ring
123 * @len: A pointer that is filled with the length written into the buffer, may
125 * @timeout_us: How many microseconds to wait before failing
127 * This function waits for the next completed request on the used ring.
129 void qvirtio_wait_used_elem(QTestState
*qts
, QVirtioDevice
*d
,
135 gint64 start_time
= g_get_monotonic_time();
138 uint32_t got_desc_idx
;
142 if (d
->bus
->get_queue_isr_status(d
, vq
) &&
143 qvirtqueue_get_buf(qts
, vq
, &got_desc_idx
, len
)) {
144 g_assert_cmpint(got_desc_idx
, ==, desc_idx
);
148 g_assert(g_get_monotonic_time() - start_time
<= timeout_us
);
152 void qvirtio_wait_config_isr(QVirtioDevice
*d
, gint64 timeout_us
)
154 gint64 start_time
= g_get_monotonic_time();
158 if (d
->bus
->get_config_isr_status(d
)) {
161 g_assert(g_get_monotonic_time() - start_time
<= timeout_us
);
165 void qvring_init(QTestState
*qts
, const QGuestAllocator
*alloc
, QVirtQueue
*vq
,
171 vq
->avail
= vq
->desc
+ vq
->size
* sizeof(struct vring_desc
);
172 vq
->used
= (uint64_t)((vq
->avail
+ sizeof(uint16_t) * (3 + vq
->size
)
173 + vq
->align
- 1) & ~(vq
->align
- 1));
175 for (i
= 0; i
< vq
->size
- 1; i
++) {
176 /* vq->desc[i].addr */
177 qtest_writeq(qts
, vq
->desc
+ (16 * i
), 0);
178 /* vq->desc[i].next */
179 qtest_writew(qts
, vq
->desc
+ (16 * i
) + 14, i
+ 1);
182 /* vq->avail->flags */
183 qtest_writew(qts
, vq
->avail
, 0);
185 qtest_writew(qts
, vq
->avail
+ 2, 0);
186 /* vq->avail->used_event */
187 qtest_writew(qts
, vq
->avail
+ 4 + (2 * vq
->size
), 0);
189 /* vq->used->flags */
190 qtest_writew(qts
, vq
->used
, 0);
191 /* vq->used->avail_event */
192 qtest_writew(qts
, vq
->used
+ 2 + sizeof(struct vring_used_elem
) * vq
->size
,
196 QVRingIndirectDesc
*qvring_indirect_desc_setup(QTestState
*qs
, QVirtioDevice
*d
,
197 QGuestAllocator
*alloc
,
201 QVRingIndirectDesc
*indirect
= g_malloc(sizeof(*indirect
));
204 indirect
->elem
= elem
;
205 indirect
->desc
= guest_alloc(alloc
, sizeof(struct vring_desc
) * elem
);
207 for (i
= 0; i
< elem
- 1; ++i
) {
208 /* indirect->desc[i].addr */
209 qtest_writeq(qs
, indirect
->desc
+ (16 * i
), 0);
210 /* indirect->desc[i].flags */
211 qtest_writew(qs
, indirect
->desc
+ (16 * i
) + 12, VRING_DESC_F_NEXT
);
212 /* indirect->desc[i].next */
213 qtest_writew(qs
, indirect
->desc
+ (16 * i
) + 14, i
+ 1);
219 void qvring_indirect_desc_add(QTestState
*qts
, QVRingIndirectDesc
*indirect
,
220 uint64_t data
, uint32_t len
, bool write
)
224 g_assert_cmpint(indirect
->index
, <, indirect
->elem
);
226 flags
= qtest_readw(qts
, indirect
->desc
+ (16 * indirect
->index
) + 12);
229 flags
|= VRING_DESC_F_WRITE
;
232 /* indirect->desc[indirect->index].addr */
233 qtest_writeq(qts
, indirect
->desc
+ (16 * indirect
->index
), data
);
234 /* indirect->desc[indirect->index].len */
235 qtest_writel(qts
, indirect
->desc
+ (16 * indirect
->index
) + 8, len
);
236 /* indirect->desc[indirect->index].flags */
237 qtest_writew(qts
, indirect
->desc
+ (16 * indirect
->index
) + 12, flags
);
242 uint32_t qvirtqueue_add(QTestState
*qts
, QVirtQueue
*vq
, uint64_t data
,
243 uint32_t len
, bool write
, bool next
)
249 flags
|= VRING_DESC_F_WRITE
;
253 flags
|= VRING_DESC_F_NEXT
;
256 /* vq->desc[vq->free_head].addr */
257 qtest_writeq(qts
, vq
->desc
+ (16 * vq
->free_head
), data
);
258 /* vq->desc[vq->free_head].len */
259 qtest_writel(qts
, vq
->desc
+ (16 * vq
->free_head
) + 8, len
);
260 /* vq->desc[vq->free_head].flags */
261 qtest_writew(qts
, vq
->desc
+ (16 * vq
->free_head
) + 12, flags
);
263 return vq
->free_head
++; /* Return and increase, in this order */
266 uint32_t qvirtqueue_add_indirect(QTestState
*qts
, QVirtQueue
*vq
,
267 QVRingIndirectDesc
*indirect
)
269 g_assert(vq
->indirect
);
270 g_assert_cmpint(vq
->size
, >=, indirect
->elem
);
271 g_assert_cmpint(indirect
->index
, ==, indirect
->elem
);
275 /* vq->desc[vq->free_head].addr */
276 qtest_writeq(qts
, vq
->desc
+ (16 * vq
->free_head
), indirect
->desc
);
277 /* vq->desc[vq->free_head].len */
278 qtest_writel(qts
, vq
->desc
+ (16 * vq
->free_head
) + 8,
279 sizeof(struct vring_desc
) * indirect
->elem
);
280 /* vq->desc[vq->free_head].flags */
281 qtest_writew(qts
, vq
->desc
+ (16 * vq
->free_head
) + 12,
282 VRING_DESC_F_INDIRECT
);
284 return vq
->free_head
++; /* Return and increase, in this order */
287 void qvirtqueue_kick(QTestState
*qts
, QVirtioDevice
*d
, QVirtQueue
*vq
,
291 uint16_t idx
= qtest_readw(qts
, vq
->avail
+ 2);
292 /* vq->used->flags */
294 /* vq->used->avail_event */
295 uint16_t avail_event
;
297 /* vq->avail->ring[idx % vq->size] */
298 qtest_writew(qts
, vq
->avail
+ 4 + (2 * (idx
% vq
->size
)), free_head
);
300 qtest_writew(qts
, vq
->avail
+ 2, idx
+ 1);
302 /* Must read after idx is updated */
303 flags
= qtest_readw(qts
, vq
->avail
);
304 avail_event
= qtest_readw(qts
, vq
->used
+ 4 +
305 sizeof(struct vring_used_elem
) * vq
->size
);
307 /* < 1 because we add elements to avail queue one by one */
308 if ((flags
& VRING_USED_F_NO_NOTIFY
) == 0 &&
309 (!vq
->event
|| (uint16_t)(idx
-avail_event
) < 1)) {
310 d
->bus
->virtqueue_kick(d
, vq
);
315 * qvirtqueue_get_buf:
316 * @desc_idx: A pointer that is filled with the vq->desc[] index, may be NULL
317 * @len: A pointer that is filled with the length written into the buffer, may
320 * This function gets the next used element if there is one ready.
322 * Returns: true if an element was ready, false otherwise
324 bool qvirtqueue_get_buf(QTestState
*qts
, QVirtQueue
*vq
, uint32_t *desc_idx
,
328 uint64_t elem_addr
, addr
;
330 idx
= qtest_readw(qts
, vq
->used
+ offsetof(struct vring_used
, idx
));
331 if (idx
== vq
->last_used_idx
) {
335 elem_addr
= vq
->used
+
336 offsetof(struct vring_used
, ring
) +
337 (vq
->last_used_idx
% vq
->size
) *
338 sizeof(struct vring_used_elem
);
341 addr
= elem_addr
+ offsetof(struct vring_used_elem
, id
);
342 *desc_idx
= qtest_readl(qts
, addr
);
346 addr
= elem_addr
+ offsetof(struct vring_used_elem
, len
);
347 *len
= qtest_readw(qts
, addr
);
354 void qvirtqueue_set_used_event(QTestState
*qts
, QVirtQueue
*vq
, uint16_t idx
)
358 /* vq->avail->used_event */
359 qtest_writew(qts
, vq
->avail
+ 4 + (2 * vq
->size
), idx
);
362 void qvirtio_start_device(QVirtioDevice
*vdev
)
365 qvirtio_set_acknowledge(vdev
);
366 qvirtio_set_driver(vdev
);
369 bool qvirtio_is_big_endian(QVirtioDevice
*d
)
371 return d
->big_endian
;