On Sat, Nov 01, 2014 at 06:02:30PM +0100, Marc Marí wrote: > Add virtio MMIO support. > Add virtio-blk-test MMIO test case. > > Signed-off-by: Marc Marí <marc.mari.barc...@gmail.com> > --- > tests/Makefile | 4 +- > tests/libqos/virtio-mmio.c | 190 > ++++++++++++++++++++++++++++++++++++++++++++ > tests/libqos/virtio-mmio.h | 46 +++++++++++ > tests/virtio-blk-test.c | 143 +++++++++++++++++++++++++++++++-- > 4 files changed, 375 insertions(+), 8 deletions(-) > create mode 100644 tests/libqos/virtio-mmio.c > create mode 100644 tests/libqos/virtio-mmio.h > > diff --git a/tests/Makefile b/tests/Makefile > index 4ae0ca4..f193b03 100644 > --- a/tests/Makefile > +++ b/tests/Makefile > @@ -183,6 +183,8 @@ gcov-files-sparc-y += hw/timer/m48t59.c > gcov-files-sparc64-y += hw/timer/m48t59.c > check-qtest-arm-y = tests/tmp105-test$(EXESUF) > gcov-files-arm-y += hw/misc/tmp105.c > +check-qtest-arm-y += tests/virtio-blk-test$(EXESUF) > +gcov-files-arm-y += arm-softmmu/hw/block/virtio-blk.c > check-qtest-ppc-y += tests/boot-order-test$(EXESUF) > check-qtest-ppc64-y += tests/boot-order-test$(EXESUF) > check-qtest-ppc64-y += tests/spapr-phb-test$(EXESUF) > @@ -300,8 +302,8 @@ libqos-obj-y += tests/libqos/i2c.o > libqos-pc-obj-y = $(libqos-obj-y) tests/libqos/pci-pc.o > libqos-pc-obj-y += tests/libqos/malloc-pc.o > libqos-omap-obj-y = $(libqos-obj-y) tests/libqos/i2c-omap.o > -libqos-virtio-obj-y = $(libqos-obj-y) $(libqos-pc-obj-y) > tests/libqos/virtio.o tests/libqos/virtio-pci.o > libqos-usb-obj-y = $(libqos-pc-obj-y) tests/libqos/usb.o > +libqos-virtio-obj-y = $(libqos-pc-obj-y) tests/libqos/virtio.o > tests/libqos/virtio-pci.o tests/libqos/virtio-mmio.o > tests/libqos/malloc-generic.o > > tests/rtc-test$(EXESUF): tests/rtc-test.o > tests/m48t59-test$(EXESUF): tests/m48t59-test.o > diff --git a/tests/libqos/virtio-mmio.c b/tests/libqos/virtio-mmio.c > new file mode 100644 > index 0000000..6896f7b > --- /dev/null > +++ b/tests/libqos/virtio-mmio.c > @@ -0,0 +1,190 @@ > +/* > + * libqos virtio MMIO 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. > + */ > + > +#include <glib.h> > +#include "libqtest.h" > +#include "libqos/virtio.h" > +#include "libqos/virtio-mmio.h" > +#include "libqos/malloc.h" > +#include "libqos/malloc-generic.h" > +#include <stdio.h>
Please include system headers before user headers. > +QVirtioMMIODevice *qvirtio_mmio_init_device(uint64_t addr, uint32_t > page_size) > +{ > + QVirtioMMIODevice *dev; > + uint32_t magic; > + dev = g_malloc0(sizeof(*dev)); > + > + magic = readl(addr + QVIRTIO_MMIO_MAGIC_VALUE); > + g_assert(magic == ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)); The magic value is little-endian according to the virtio 1.0 specification and the Linux kernel's virtio_mmio implementation. QEMU's virtio-mmio is native endian (i.e. host endian) and I think this is a bug. qtest's readl() is host-endian so it matches what QEMU does. I suppose you can leave this for now but it may need to be changed in the future. > +static void mmio_basic(void) > +{ > + QVirtioMMIODevice *dev; > + QVirtQueue *vq; > + QGuestAllocator *alloc; > + QVirtioBlkReq req; > + int n_size = TEST_IMAGE_SIZE / 2; > + uint64_t req_addr; > + uint64_t capacity; > + uint32_t features; > + uint32_t free_head; > + uint8_t status; > + char *data; > + > + arm_test_start(); > + > + dev = qvirtio_mmio_init_device(MMIO_DEV_BASE_ADDR, MMIO_PAGE_SIZE); > + g_assert(dev != NULL); > + g_assert_cmphex(dev->vdev.device_type, ==, QVIRTIO_BLK_DEVICE_ID); > + > + qvirtio_reset(&qvirtio_mmio, &dev->vdev); > + qvirtio_set_acknowledge(&qvirtio_mmio, &dev->vdev); > + qvirtio_set_driver(&qvirtio_mmio, &dev->vdev); > + > + capacity = qvirtio_config_readq(&qvirtio_mmio, &dev->vdev, > + > QVIRTIO_MMIO_DEVICE_SPECIFIC); > + g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512); > + > + features = qvirtio_get_features(&qvirtio_mmio, &dev->vdev); > + features = features & ~(QVIRTIO_F_RING_INDIRECT_DESC | > + QVIRTIO_F_RING_EVENT_IDX | > QVIRTIO_BLK_F_SCSI); > + qvirtio_set_features(&qvirtio_mmio, &dev->vdev, features); > + > + alloc = generic_alloc_init(MMIO_RAM_ADDR, MMIO_RAM_SIZE, MMIO_PAGE_SIZE); > + vq = qvirtqueue_setup(&qvirtio_mmio, &dev->vdev, alloc, 0); > + > + qvirtio_set_driver_ok(&qvirtio_mmio, &dev->vdev); > + > + qmp("{ 'execute': 'block_resize', 'arguments': { 'device': 'drive0', " > + " 'size': %d } }", > n_size); > + > + qvirtio_wait_queue_isr(&qvirtio_mmio, &dev->vdev, vq, > + QVIRTIO_BLK_TIMEOUT_US); > + > + capacity = qvirtio_config_readq(&qvirtio_mmio, &dev->vdev, > + > QVIRTIO_MMIO_DEVICE_SPECIFIC); > + g_assert_cmpint(capacity, ==, n_size / 512); > + > + /* Write request */ > + req.type = QVIRTIO_BLK_T_OUT; > + req.ioprio = 1; > + req.sector = 0; > + req.data = g_malloc0(512); > + strcpy(req.data, "TEST"); > + > + req_addr = virtio_blk_request(alloc, &req, 512); > + > + g_free(req.data); > + > + free_head = qvirtqueue_add(vq, req_addr, 528, false, true); > + qvirtqueue_add(vq, req_addr + 528, 1, true, false); > + qvirtqueue_kick(&qvirtio_mmio, &dev->vdev, vq, free_head); > + > + qvirtio_wait_queue_isr(&qvirtio_mmio, &dev->vdev, vq, > + QVIRTIO_BLK_TIMEOUT_US); > + status = readb(req_addr + 528); > + g_assert_cmpint(status, ==, 0); > + > + guest_free(alloc, req_addr); > + > + /* Read request */ > + req.type = QVIRTIO_BLK_T_IN; > + req.ioprio = 1; > + req.sector = 0; > + req.data = g_malloc0(512); > + > + req_addr = virtio_blk_request(alloc, &req, 512); > + > + g_free(req.data); > + > + free_head = qvirtqueue_add(vq, req_addr, 16, false, true); > + qvirtqueue_add(vq, req_addr + 16, 513, true, false); > + > + qvirtqueue_kick(&qvirtio_mmio, &dev->vdev, vq, free_head); > + > + qvirtio_wait_queue_isr(&qvirtio_mmio, &dev->vdev, vq, > + QVIRTIO_BLK_TIMEOUT_US); > + status = readb(req_addr + 528); > + g_assert_cmpint(status, ==, 0); > + > + data = g_malloc0(512); > + memread(req_addr + 16, data, 512); > + g_assert_cmpstr(data, ==, "TEST"); > + g_free(data); There is a lot of code duplication here. Can the test logic but shared between PCI and MMIO?
pgpVfY89xQGNK.pgp
Description: PGP signature