From patchwork Fri Nov 16 15:35:57 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: fred.konrad@greensocs.com X-Patchwork-Id: 199663 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 74CF92C0082 for ; Sat, 17 Nov 2012 02:36:45 +1100 (EST) Received: from localhost ([::1]:55051 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TZNyF-0005um-Bu for incoming@patchwork.ozlabs.org; Fri, 16 Nov 2012 10:36:43 -0500 Received: from eggs.gnu.org ([208.118.235.92]:45044) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TZNxx-0005kG-IW for qemu-devel@nongnu.org; Fri, 16 Nov 2012 10:36:28 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TZNxu-0002bh-GL for qemu-devel@nongnu.org; Fri, 16 Nov 2012 10:36:25 -0500 Received: from greensocs.com ([87.106.252.221]:38704 helo=s15328186.onlinehome-server.info) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TZNxu-0002bd-68 for qemu-devel@nongnu.org; Fri, 16 Nov 2012 10:36:22 -0500 Received: from localhost (unknown [127.0.0.1]) by s15328186.onlinehome-server.info (Postfix) with ESMTP id 7374140F4C8 for ; Fri, 16 Nov 2012 15:36:21 +0000 (UTC) Received: from s15328186.onlinehome-server.info ([127.0.0.1]) by localhost (s15328186.onlinehome-server.info [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 4NeAu4ZefROC; Fri, 16 Nov 2012 16:36:13 +0100 (CET) Received: by s15328186.onlinehome-server.info (Postfix, from userid 491) id 606C840F4C9; Fri, 16 Nov 2012 16:36:13 +0100 (CET) Received: from compaq.katmai.xl.cx.katmai.xl.cx (bon31-2-82-224-104-128.fbx.proxad.net [82.224.104.128]) by s15328186.onlinehome-server.info (Postfix) with ESMTPSA id C3DA240EF3D; Fri, 16 Nov 2012 16:36:12 +0100 (CET) From: fred.konrad@greensocs.com To: qemu-devel@nongnu.org Date: Fri, 16 Nov 2012 16:35:57 +0100 Message-Id: <1353080159-10536-2-git-send-email-fred.konrad@greensocs.com> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: <1353080159-10536-1-git-send-email-fred.konrad@greensocs.com> References: <1353080159-10536-1-git-send-email-fred.konrad@greensocs.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 87.106.252.221 Cc: mark.burton@greensocs.com, KONRAD Frederic Subject: [Qemu-devel] [RFC PATCH 1/3] virtio-bus : Introduce VirtioBus. 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 From: KONRAD Frederic This patch create a new VirtioBus, which can be added to Virtio transports like virtio-pci, virtio-mmio,... One VirtIODevice can be connected to this device, like virtio-blk in the 3rd patch. The VirtioBus shares through a VirtioBusInfo structure : * two callbacks with the transport : init_cb and exit_cb, which must be called by the VirtIODevice, after the initialization and before the destruction, to put the right PCI IDs and/or stop the event fd. * a VirtIOBindings structure. Signed-off-by: KONRAD Frederic --- hw/Makefile.objs | 1 + hw/virtio-bus.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/virtio-bus.h | 49 ++++++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 hw/virtio-bus.c create mode 100644 hw/virtio-bus.h diff --git a/hw/Makefile.objs b/hw/Makefile.objs index af4ab0c..1b25d77 100644 --- a/hw/Makefile.objs +++ b/hw/Makefile.objs @@ -1,6 +1,7 @@ common-obj-y = usb/ ide/ common-obj-y += loader.o common-obj-$(CONFIG_VIRTIO) += virtio-console.o +common-obj-$(CONFIG_VIRTIO) += virtio-bus.o common-obj-$(CONFIG_VIRTIO_PCI) += virtio-pci.o common-obj-y += fw_cfg.o common-obj-$(CONFIG_PCI) += pci.o pci_bridge.o pci_bridge_dev.o diff --git a/hw/virtio-bus.c b/hw/virtio-bus.c new file mode 100644 index 0000000..b2e7e9c --- /dev/null +++ b/hw/virtio-bus.c @@ -0,0 +1,111 @@ +/* + * VirtioBus + * + * Copyright (C) 2012 : GreenSocs Ltd + * http://www.greensocs.com/ , email: info@greensocs.com + * + * Developed by : + * Frederic Konrad + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + */ + +#include "hw.h" +#include "qemu-error.h" +#include "qdev.h" +#include "virtio-bus.h" +#include "virtio.h" + +#define DEBUG_VIRTIO_BUS + +#ifdef DEBUG_VIRTIO_BUS + +#define DPRINTF(fmt, ...) \ +do { printf("virtio_bus: " fmt , ## __VA_ARGS__); } while (0) +#else +#define DPRINTF(fmt, ...) do {} while (0) +#endif + +static char *virtio_bus_get_fw_dev_path(DeviceState *dev); + +static void virtio_bus_class_init(ObjectClass *klass, void *data) +{ + BusClass *k = BUS_CLASS(klass); + k->get_fw_dev_path = virtio_bus_get_fw_dev_path; +} + +static const TypeInfo virtio_bus_info = { + .name = TYPE_VIRTIO_BUS, + .parent = TYPE_BUS, + .instance_size = sizeof(VirtioBus), + .class_init = virtio_bus_class_init, +}; + +/* VirtioBus */ + +static int next_virtio_bus; + +/* Create a virtio bus, and attach to transport. */ +void virtio_bus_new(VirtioBus *bus, DeviceState *host, + const VirtioBusInfo *info) +{ + /* + * Setting name to NULL return always "virtio.0" + * as bus name in info qtree. + */ + char *bus_name = g_strdup_printf("%s.%d", BUS_NAME, next_virtio_bus); + qbus_create_inplace(&bus->qbus, TYPE_VIRTIO_BUS, host, bus_name); + bus->busnr = next_virtio_bus++; + bus->info = info; + /* no hotplug for the moment ? */ + bus->qbus.allow_hotplug = 0; + bus->bus_in_use = false; + DPRINTF("bus %s created\n", bus_name); +} + +/* Bind the VirtIODevice to the VirtioBus. */ +void virtio_bus_bind_device(VirtioBus *bus) +{ + assert(bus != NULL); + assert(bus->vdev != NULL); + virtio_bind_device(bus->vdev, &(bus->info->virtio_bindings), + bus->qbus.parent); +} + +/* This must be called to when the VirtIODevice init */ +int virtio_bus_init_cb(VirtioBus *bus, VirtIODevice *vdev) +{ + if (bus->bus_in_use == true) { + error_report("%s in use.\n", bus->qbus.name); + return -1; + } + assert(bus->info->init_cb != NULL); + /* keep the VirtIODevice in the VirtioBus */ + bus->vdev = vdev; + bus->info->init_cb(bus->qbus.parent); + + bus->bus_in_use = true; + return 0; +} + +/* This must be called when the VirtIODevice exit */ +void virtio_bus_exit_cb(VirtioBus *bus) +{ + assert(bus->info->exit_cb != NULL); + bus->info->exit_cb(bus->qbus.parent); + bus->bus_in_use = false; +} + +static char *virtio_bus_get_fw_dev_path(DeviceState *dev) +{ + return g_strdup_printf("%s", qdev_fw_name(dev)); +} + + +static void virtio_register_types(void) +{ + type_register_static(&virtio_bus_info); +} + +type_init(virtio_register_types) diff --git a/hw/virtio-bus.h b/hw/virtio-bus.h new file mode 100644 index 0000000..6ea5035 --- /dev/null +++ b/hw/virtio-bus.h @@ -0,0 +1,49 @@ +/* + * VirtioBus + * + * Copyright (C) 2012 : GreenSocs Ltd + * http://www.greensocs.com/ , email: info@greensocs.com + * + * Developed by : + * Frederic Konrad + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + */ + +#ifndef _VIRTIO_BUS_H_ +#define _VIRTIO_BUS_H_ + +#include "qdev.h" +#include "sysemu.h" +#include "virtio.h" + +#define TYPE_VIRTIO_BUS "VIRTIO" +#define BUS_NAME "virtio" + +typedef struct VirtioBus VirtioBus; +typedef struct VirtioBusInfo VirtioBusInfo; + +struct VirtioBusInfo { + void (*init_cb)(DeviceState *dev); + void (*exit_cb)(DeviceState *dev); + VirtIOBindings virtio_bindings; +}; + +struct VirtioBus { + BusState qbus; + int busnr; + bool bus_in_use; + uint16_t pci_device_id; + uint16_t pci_class; + VirtIODevice *vdev; + const VirtioBusInfo *info; +}; + +void virtio_bus_new(VirtioBus *bus, DeviceState *host, + const VirtioBusInfo *info); +void virtio_bus_bind_device(VirtioBus *bus); +int virtio_bus_init_cb(VirtioBus *bus, VirtIODevice *dev); +void virtio_bus_exit_cb(VirtioBus *bus); + +#endif