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