From patchwork Wed Dec 4 16:30:27 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Jones X-Patchwork-Id: 296580 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 2C5812C0097 for ; Thu, 5 Dec 2013 03:31:28 +1100 (EST) Received: from localhost ([::1]:49196 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VoFMD-00038H-2r for incoming@patchwork.ozlabs.org; Wed, 04 Dec 2013 11:31:25 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58372) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VoFLq-00037w-Uo for qemu-devel@nongnu.org; Wed, 04 Dec 2013 11:31:08 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VoFLk-0007KY-R0 for qemu-devel@nongnu.org; Wed, 04 Dec 2013 11:31:02 -0500 Received: from mx1.redhat.com ([209.132.183.28]:2792) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VoFLk-0007KD-IG for qemu-devel@nongnu.org; Wed, 04 Dec 2013 11:30:56 -0500 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id rB4GUhA4009509 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 4 Dec 2013 11:30:44 -0500 Received: from hawk.usersys.redhat.com.com (dhcp-1-158.brq.redhat.com [10.34.1.158]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id rB4GUV6u030899; Wed, 4 Dec 2013 11:30:32 -0500 From: Andrew Jones To: qemu-devel@nongnu.org Date: Wed, 4 Dec 2013 17:30:27 +0100 Message-Id: <1386174627-21822-1-git-send-email-drjones@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kvmarm@lists.cs.columbia.edu Subject: [Qemu-devel] [PATCH v2] virtio: Introduce virtio-testdev X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This is a virtio version of hw/misc/debugexit and should evolve into a virtio version of pc-testdev. pc-testdev uses the PC's ISA bus, whereas this testdev can be plugged into a virtio-mmio transport, which is needed for kvm-unit-tests/arm. virtio-testdev uses the virtio device config space as a communication channel, and implements an RTAS-like protocol through it allowing guests to execute commands. Only three commands are currently implemented; 1) VERSION: for version compatibility checks 2) CLEAR: set all the config space back to zero 3) EXIT: exit() from qemu with a status code --- v2: - No real functional changes, just added some comments and changed register bank accesses to use byte offsets, allowing the driver implementation to better mirror this device's implementation. Signed-off-by: Andrew Jones --- hw/virtio/Makefile.objs | 1 + hw/virtio/virtio-testdev.c | 154 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 hw/virtio/virtio-testdev.c diff --git a/hw/virtio/Makefile.objs b/hw/virtio/Makefile.objs index 1ba53d9cc316c..b63fea4a175f0 100644 --- a/hw/virtio/Makefile.objs +++ b/hw/virtio/Makefile.objs @@ -3,6 +3,7 @@ common-obj-$(CONFIG_VIRTIO_PCI) += virtio-pci.o common-obj-y += virtio-bus.o common-obj-y += virtio-mmio.o common-obj-$(CONFIG_VIRTIO_BLK_DATA_PLANE) += dataplane/ +common-obj-y += virtio-testdev.o obj-y += virtio.o virtio-balloon.o obj-$(CONFIG_LINUX) += vhost.o diff --git a/hw/virtio/virtio-testdev.c b/hw/virtio/virtio-testdev.c new file mode 100644 index 0000000000000..6f6a16e42006c --- /dev/null +++ b/hw/virtio/virtio-testdev.c @@ -0,0 +1,154 @@ +/* + * Virtio Test Device + * + * Copyright (C) 2013 Red Hat, Inc. + * Copyright (C) 2013 Andrew Jones + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + * + */ +#include "hw/virtio/virtio-bus.h" + +#define VIRTIO_ID_TESTDEV 0xffff + +#define TYPE_VIRTIO_TESTDEV "virtio-testdev" +#define VIRTIO_TESTDEV(obj) \ + OBJECT_CHECK(VirtIOTestdev, (obj), TYPE_VIRTIO_TESTDEV) + +#define TESTDEV_MAJOR_VER 1 +#define TESTDEV_MINOR_VER 1 + +/* + * Size of the register bank in bytes. The max is determined + * by the device's config space. For virtio-mmio devices we + * only have 256 bytes, so that's our max, but we don't need + * that much anyway. 64 bytes gives us token, nargs, nrets, + * and an additional 13 4-byte registers for input/output. + * That'll do. + */ +#define CONFIG_SIZE 64 + +enum { + VERSION = 1, + CLEAR, + EXIT, +}; + +#define TOKEN_OFFSET 0x0 +#define NARGS_OFFSET 0x4 +#define NRETS_OFFSET 0x8 +#define ARG_OFFSET(n) (0xc + (n) * 4) +#define __RET_OFFSET(nargs, n) (ARG_OFFSET(nargs) + (n) * 4) +#define RET_OFFSET(d, n) __RET_OFFSET(config_readl(d, NARGS_OFFSET), n) + +typedef struct VirtIOTestdev { + VirtIODevice parent_obj; + uint8_t config[CONFIG_SIZE]; +} VirtIOTestdev; + +static uint32_t config_readl(VirtIOTestdev *dev, unsigned offset) +{ + uint32_t *config = (uint32_t *)&dev->config[0]; + + if (offset > (CONFIG_SIZE - 4)) { + return 0; + } + + return le32_to_cpu(config[offset / 4]); +} + +static void config_writel(VirtIOTestdev *dev, unsigned offset, uint32_t val) +{ + uint32_t *config = (uint32_t *)&dev->config[0]; + + if (offset <= (CONFIG_SIZE - 4)) { + config[offset / 4] = cpu_to_le32(val); + } +} + +static void virtio_testdev_get_config(VirtIODevice *vdev, uint8_t *config_data) +{ + VirtIOTestdev *dev = VIRTIO_TESTDEV(vdev); + memcpy(config_data, &dev->config[0], CONFIG_SIZE); +} + +static void virtio_testdev_set_config(VirtIODevice *vdev, + const uint8_t *config_data) +{ + VirtIOTestdev *dev = VIRTIO_TESTDEV(vdev); + uint32_t token; + + memcpy(&dev->config[0], config_data, CONFIG_SIZE); + + token = config_readl(dev, TOKEN_OFFSET); + + /* + * The token register must always remain zero in order to + * avoid re-executing operations while new operation + * arguments are being filled in. + */ + config_writel(dev, TOKEN_OFFSET, 0); + + switch (token) { + case 0: + /* + * No token yet, so we were just filling in arguments, and + * now there's nothing left to do. + */ + return; + case VERSION: + config_writel(dev, RET_OFFSET(dev, 0), + (TESTDEV_MAJOR_VER << 16) | TESTDEV_MINOR_VER); + break; + case EXIT: + exit((config_readl(dev, ARG_OFFSET(0)) << 1) | 1); + case CLEAR: + default: + /* The CLEAR op and unknown ops reset all registers */ + memset(&dev->config[0], 0, CONFIG_SIZE); + } +} + +static uint32_t virtio_testdev_get_features(VirtIODevice *vdev, uint32_t f) +{ + return f; +} + +static int virtio_testdev_init(VirtIODevice *vdev) +{ + virtio_init(vdev, "virtio-testdev", VIRTIO_ID_TESTDEV, CONFIG_SIZE); + return 0; +} + +static int virtio_testdev_exit(DeviceState *qdev) +{ + virtio_cleanup(VIRTIO_DEVICE(qdev)); + return 0; +} + +static void virtio_testdev_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); + dc->exit = virtio_testdev_exit; + set_bit(DEVICE_CATEGORY_MISC, dc->categories); + vdc->init = virtio_testdev_init; + vdc->get_config = virtio_testdev_get_config; + vdc->set_config = virtio_testdev_set_config; + vdc->get_features = virtio_testdev_get_features; +} + +static const TypeInfo virtio_testdev_info = { + .name = TYPE_VIRTIO_TESTDEV, + .parent = TYPE_VIRTIO_DEVICE, + .instance_size = sizeof(VirtIOTestdev), + .class_init = virtio_testdev_class_init, +}; + +static void virtio_register_types(void) +{ + type_register_static(&virtio_testdev_info); +} + +type_init(virtio_register_types)