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 "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" 16 17 /* 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. 22 * 23 * The following qvirtio_readX/writeX() functions handle Legacy and VIRTIO 1.0 24 * accesses seamlessly. 25 */ 26 static uint16_t qvirtio_readw(QVirtioDevice *d, QTestState *qts, uint64_t addr) 27 { 28 uint16_t val = qtest_readw(qts, addr); 29 30 if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) { 31 val = bswap16(val); 32 } 33 return val; 34 } 35 36 static uint32_t qvirtio_readl(QVirtioDevice *d, QTestState *qts, uint64_t addr) 37 { 38 uint32_t val = qtest_readl(qts, addr); 39 40 if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) { 41 val = bswap32(val); 42 } 43 return val; 44 } 45 46 static void qvirtio_writew(QVirtioDevice *d, QTestState *qts, 47 uint64_t addr, uint16_t val) 48 { 49 if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) { 50 val = bswap16(val); 51 } 52 qtest_writew(qts, addr, val); 53 } 54 55 static void qvirtio_writel(QVirtioDevice *d, QTestState *qts, 56 uint64_t addr, uint32_t val) 57 { 58 if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) { 59 val = bswap32(val); 60 } 61 qtest_writel(qts, addr, val); 62 } 63 64 static void qvirtio_writeq(QVirtioDevice *d, QTestState *qts, 65 uint64_t addr, uint64_t val) 66 { 67 if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) { 68 val = bswap64(val); 69 } 70 qtest_writeq(qts, addr, val); 71 } 72 73 uint8_t qvirtio_config_readb(QVirtioDevice *d, uint64_t addr) 74 { 75 g_assert_true(d->features_negotiated); 76 return d->bus->config_readb(d, addr); 77 } 78 79 uint16_t qvirtio_config_readw(QVirtioDevice *d, uint64_t addr) 80 { 81 g_assert_true(d->features_negotiated); 82 return d->bus->config_readw(d, addr); 83 } 84 85 uint32_t qvirtio_config_readl(QVirtioDevice *d, uint64_t addr) 86 { 87 g_assert_true(d->features_negotiated); 88 return d->bus->config_readl(d, addr); 89 } 90 91 uint64_t qvirtio_config_readq(QVirtioDevice *d, uint64_t addr) 92 { 93 g_assert_true(d->features_negotiated); 94 return d->bus->config_readq(d, addr); 95 } 96 97 uint64_t qvirtio_get_features(QVirtioDevice *d) 98 { 99 return d->bus->get_features(d); 100 } 101 102 void qvirtio_set_features(QVirtioDevice *d, uint64_t features) 103 { 104 g_assert(!(features & QVIRTIO_F_BAD_FEATURE)); 105 106 d->features = features; 107 d->bus->set_features(d, features); 108 109 /* 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. 113 */ 114 if (features & (1ull << VIRTIO_F_VERSION_1)) { 115 uint8_t status = d->bus->get_status(d) | 116 VIRTIO_CONFIG_S_FEATURES_OK; 117 118 d->bus->set_status(d, status); 119 g_assert_cmphex(d->bus->get_status(d), ==, status); 120 } 121 122 d->features_negotiated = true; 123 } 124 125 QVirtQueue *qvirtqueue_setup(QVirtioDevice *d, 126 QGuestAllocator *alloc, uint16_t index) 127 { 128 g_assert_true(d->features_negotiated); 129 return d->bus->virtqueue_setup(d, alloc, index); 130 } 131 132 void qvirtqueue_cleanup(const QVirtioBus *bus, QVirtQueue *vq, 133 QGuestAllocator *alloc) 134 { 135 return bus->virtqueue_cleanup(vq, alloc); 136 } 137 138 void qvirtio_reset(QVirtioDevice *d) 139 { 140 d->bus->set_status(d, 0); 141 g_assert_cmphex(d->bus->get_status(d), ==, 0); 142 d->features_negotiated = false; 143 } 144 145 void qvirtio_set_acknowledge(QVirtioDevice *d) 146 { 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); 149 } 150 151 void qvirtio_set_driver(QVirtioDevice *d) 152 { 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); 156 } 157 158 void qvirtio_set_driver_ok(QVirtioDevice *d) 159 { 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)); 165 } 166 167 void qvirtio_wait_queue_isr(QTestState *qts, QVirtioDevice *d, 168 QVirtQueue *vq, gint64 timeout_us) 169 { 170 gint64 start_time = g_get_monotonic_time(); 171 172 for (;;) { 173 if (d->bus->get_queue_isr_status(d, vq)) { 174 return; 175 } 176 g_assert(g_get_monotonic_time() - start_time <= timeout_us); 177 } 178 } 179 180 /* Wait for the status byte at given guest memory address to be set 181 * 182 * The virtqueue interrupt must not be raised, making this useful for testing 183 * event_index functionality. 184 */ 185 uint8_t qvirtio_wait_status_byte_no_isr(QTestState *qts, QVirtioDevice *d, 186 QVirtQueue *vq, 187 uint64_t addr, 188 gint64 timeout_us) 189 { 190 gint64 start_time = g_get_monotonic_time(); 191 uint8_t val; 192 193 while ((val = qtest_readb(qts, addr)) == 0xff) { 194 g_assert(!d->bus->get_queue_isr_status(d, vq)); 195 g_assert(g_get_monotonic_time() - start_time <= timeout_us); 196 } 197 return val; 198 } 199 200 /* 201 * qvirtio_wait_used_elem: 202 * @desc_idx: The next expected vq->desc[] index in the used ring 203 * @len: A pointer that is filled with the length written into the buffer, may 204 * be NULL 205 * @timeout_us: How many microseconds to wait before failing 206 * 207 * This function waits for the next completed request on the used ring. 208 */ 209 void qvirtio_wait_used_elem(QTestState *qts, QVirtioDevice *d, 210 QVirtQueue *vq, 211 uint32_t desc_idx, 212 uint32_t *len, 213 gint64 timeout_us) 214 { 215 gint64 start_time = g_get_monotonic_time(); 216 217 for (;;) { 218 uint32_t got_desc_idx; 219 220 221 if (d->bus->get_queue_isr_status(d, vq) && 222 qvirtqueue_get_buf(qts, vq, &got_desc_idx, len)) { 223 g_assert_cmpint(got_desc_idx, ==, desc_idx); 224 return; 225 } 226 g_assert(g_get_monotonic_time() - start_time <= timeout_us); 227 } 228 } 229 230 void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us) 231 { 232 d->bus->wait_config_isr_status(d, timeout_us); 233 } 234 235 void qvring_init(QTestState *qts, const QGuestAllocator *alloc, QVirtQueue *vq, 236 uint64_t addr) 237 { 238 int i; 239 240 vq->desc = addr; 241 vq->avail = vq->desc + vq->size * sizeof(struct vring_desc); 242 vq->used = (uint64_t)((vq->avail + sizeof(uint16_t) * (3 + vq->size) 243 + vq->align - 1) & ~(vq->align - 1)); 244 245 for (i = 0; i < vq->size - 1; i++) { 246 /* vq->desc[i].addr */ 247 qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * i), 0); 248 /* vq->desc[i].next */ 249 qvirtio_writew(vq->vdev, qts, vq->desc + (16 * i) + 14, i + 1); 250 } 251 252 /* vq->avail->flags */ 253 qvirtio_writew(vq->vdev, qts, vq->avail, 0); 254 /* vq->avail->idx */ 255 qvirtio_writew(vq->vdev, qts, vq->avail + 2, 0); 256 /* vq->avail->used_event */ 257 qvirtio_writew(vq->vdev, qts, vq->avail + 4 + (2 * vq->size), 0); 258 259 /* vq->used->flags */ 260 qvirtio_writew(vq->vdev, qts, vq->used, 0); 261 /* vq->used->idx */ 262 qvirtio_writew(vq->vdev, qts, vq->used + 2, 0); 263 /* vq->used->avail_event */ 264 qvirtio_writew(vq->vdev, qts, vq->used + 4 + 265 sizeof(struct vring_used_elem) * vq->size, 0); 266 } 267 268 QVRingIndirectDesc *qvring_indirect_desc_setup(QTestState *qs, QVirtioDevice *d, 269 QGuestAllocator *alloc, 270 uint16_t elem) 271 { 272 int i; 273 QVRingIndirectDesc *indirect = g_malloc(sizeof(*indirect)); 274 275 indirect->index = 0; 276 indirect->elem = elem; 277 indirect->desc = guest_alloc(alloc, sizeof(struct vring_desc) * elem); 278 279 for (i = 0; i < elem; ++i) { 280 /* indirect->desc[i].addr */ 281 qvirtio_writeq(d, qs, indirect->desc + (16 * i), 0); 282 283 /* 284 * If it's not the last element of the ring, set 285 * the chain (VRING_DESC_F_NEXT) flag and 286 * desc->next. Clear the last element - there's 287 * no guarantee that guest_alloc() will do it. 288 */ 289 if (i != elem - 1) { 290 /* indirect->desc[i].flags */ 291 qvirtio_writew(d, qs, indirect->desc + (16 * i) + 12, 292 VRING_DESC_F_NEXT); 293 294 /* indirect->desc[i].next */ 295 qvirtio_writew(d, qs, indirect->desc + (16 * i) + 14, i + 1); 296 } else { 297 qvirtio_writew(d, qs, indirect->desc + (16 * i) + 12, 0); 298 qvirtio_writew(d, qs, indirect->desc + (16 * i) + 14, 0); 299 } 300 } 301 302 return indirect; 303 } 304 305 void qvring_indirect_desc_add(QVirtioDevice *d, QTestState *qts, 306 QVRingIndirectDesc *indirect, 307 uint64_t data, uint32_t len, bool write) 308 { 309 uint16_t flags; 310 311 g_assert_cmpint(indirect->index, <, indirect->elem); 312 313 flags = qvirtio_readw(d, qts, indirect->desc + 314 (16 * indirect->index) + 12); 315 316 if (write) { 317 flags |= VRING_DESC_F_WRITE; 318 } 319 320 /* indirect->desc[indirect->index].addr */ 321 qvirtio_writeq(d, qts, indirect->desc + (16 * indirect->index), data); 322 /* indirect->desc[indirect->index].len */ 323 qvirtio_writel(d, qts, indirect->desc + (16 * indirect->index) + 8, len); 324 /* indirect->desc[indirect->index].flags */ 325 qvirtio_writew(d, qts, indirect->desc + (16 * indirect->index) + 12, 326 flags); 327 328 indirect->index++; 329 } 330 331 uint32_t qvirtqueue_add(QTestState *qts, QVirtQueue *vq, uint64_t data, 332 uint32_t len, bool write, bool next) 333 { 334 uint16_t flags = 0; 335 vq->num_free--; 336 337 if (write) { 338 flags |= VRING_DESC_F_WRITE; 339 } 340 341 if (next) { 342 flags |= VRING_DESC_F_NEXT; 343 } 344 345 /* vq->desc[vq->free_head].addr */ 346 qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * vq->free_head), data); 347 /* vq->desc[vq->free_head].len */ 348 qvirtio_writel(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 8, len); 349 /* vq->desc[vq->free_head].flags */ 350 qvirtio_writew(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 12, flags); 351 352 return vq->free_head++; /* Return and increase, in this order */ 353 } 354 355 uint32_t qvirtqueue_add_indirect(QTestState *qts, QVirtQueue *vq, 356 QVRingIndirectDesc *indirect) 357 { 358 g_assert(vq->indirect); 359 g_assert_cmpint(vq->size, >=, indirect->elem); 360 g_assert_cmpint(indirect->index, ==, indirect->elem); 361 362 vq->num_free--; 363 364 /* vq->desc[vq->free_head].addr */ 365 qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * vq->free_head), 366 indirect->desc); 367 /* vq->desc[vq->free_head].len */ 368 qvirtio_writel(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 8, 369 sizeof(struct vring_desc) * indirect->elem); 370 /* vq->desc[vq->free_head].flags */ 371 qvirtio_writew(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 12, 372 VRING_DESC_F_INDIRECT); 373 374 return vq->free_head++; /* Return and increase, in this order */ 375 } 376 377 void qvirtqueue_kick(QTestState *qts, QVirtioDevice *d, QVirtQueue *vq, 378 uint32_t free_head) 379 { 380 /* vq->avail->idx */ 381 uint16_t idx = qvirtio_readw(d, qts, vq->avail + 2); 382 /* vq->used->flags */ 383 uint16_t flags; 384 /* vq->used->avail_event */ 385 uint16_t avail_event; 386 387 /* vq->avail->ring[idx % vq->size] */ 388 qvirtio_writew(d, qts, vq->avail + 4 + (2 * (idx % vq->size)), free_head); 389 /* vq->avail->idx */ 390 qvirtio_writew(d, qts, vq->avail + 2, idx + 1); 391 392 /* Must read after idx is updated */ 393 flags = qvirtio_readw(d, qts, vq->used); 394 avail_event = qvirtio_readw(d, qts, vq->used + 4 + 395 sizeof(struct vring_used_elem) * vq->size); 396 397 /* < 1 because we add elements to avail queue one by one */ 398 if ((flags & VRING_USED_F_NO_NOTIFY) == 0 && 399 (!vq->event || (uint16_t)(idx-avail_event) < 1)) { 400 d->bus->virtqueue_kick(d, vq); 401 } 402 } 403 404 /* 405 * qvirtqueue_get_buf: 406 * @desc_idx: A pointer that is filled with the vq->desc[] index, may be NULL 407 * @len: A pointer that is filled with the length written into the buffer, may 408 * be NULL 409 * 410 * This function gets the next used element if there is one ready. 411 * 412 * Returns: true if an element was ready, false otherwise 413 */ 414 bool qvirtqueue_get_buf(QTestState *qts, QVirtQueue *vq, uint32_t *desc_idx, 415 uint32_t *len) 416 { 417 uint16_t idx; 418 uint64_t elem_addr, addr; 419 420 idx = qvirtio_readw(vq->vdev, qts, 421 vq->used + offsetof(struct vring_used, idx)); 422 if (idx == vq->last_used_idx) { 423 return false; 424 } 425 426 elem_addr = vq->used + 427 offsetof(struct vring_used, ring) + 428 (vq->last_used_idx % vq->size) * 429 sizeof(struct vring_used_elem); 430 431 if (desc_idx) { 432 addr = elem_addr + offsetof(struct vring_used_elem, id); 433 *desc_idx = qvirtio_readl(vq->vdev, qts, addr); 434 } 435 436 if (len) { 437 addr = elem_addr + offsetof(struct vring_used_elem, len); 438 *len = qvirtio_readw(vq->vdev, qts, addr); 439 } 440 441 vq->last_used_idx++; 442 return true; 443 } 444 445 void qvirtqueue_set_used_event(QTestState *qts, QVirtQueue *vq, uint16_t idx) 446 { 447 g_assert(vq->event); 448 449 /* vq->avail->used_event */ 450 qvirtio_writew(vq->vdev, qts, vq->avail + 4 + (2 * vq->size), idx); 451 } 452 453 void qvirtio_start_device(QVirtioDevice *vdev) 454 { 455 qvirtio_reset(vdev); 456 qvirtio_set_acknowledge(vdev); 457 qvirtio_set_driver(vdev); 458 } 459 460 bool qvirtio_is_big_endian(QVirtioDevice *d) 461 { 462 return d->big_endian; 463 } 464