From patchwork Thu Nov 22 14:50:51 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: 201059 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 9FD682C008F for ; Fri, 23 Nov 2012 01:52:10 +1100 (EST) Received: from localhost ([::1]:45787 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TbY8L-0004b1-Kj for incoming@patchwork.ozlabs.org; Thu, 22 Nov 2012 09:52:05 -0500 Received: from eggs.gnu.org ([208.118.235.92]:60701) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TbY81-00047z-F5 for qemu-devel@nongnu.org; Thu, 22 Nov 2012 09:51:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TbY7t-0007TV-Su for qemu-devel@nongnu.org; Thu, 22 Nov 2012 09:51:43 -0500 Received: from greensocs.com ([87.106.252.221]:35659 helo=s15328186.onlinehome-server.info) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TbY7t-0007TR-Ip for qemu-devel@nongnu.org; Thu, 22 Nov 2012 09:51:37 -0500 Received: from localhost (unknown [127.0.0.1]) by s15328186.onlinehome-server.info (Postfix) with ESMTP id EB16042A019; Thu, 22 Nov 2012 14:51:36 +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 yaLWLi9NZbXT; Thu, 22 Nov 2012 15:51:35 +0100 (CET) Received: by s15328186.onlinehome-server.info (Postfix, from userid 491) id BEB5C429FE1; Thu, 22 Nov 2012 15:51:34 +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 EC92440F4C8; Thu, 22 Nov 2012 15:51:33 +0100 (CET) From: fred.konrad@greensocs.com To: qemu-devel@nongnu.org Date: Thu, 22 Nov 2012 15:50:51 +0100 Message-Id: <1353595852-30776-3-git-send-email-fred.konrad@greensocs.com> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: <1353595852-30776-1-git-send-email-fred.konrad@greensocs.com> References: <1353595852-30776-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: peter.maydell@linaro.org, aliguori@us.ibm.com, e.voevodin@samsung.com, mark.burton@greensocs.com, stefanha@redhat.com, cornelia.huck@de.ibm.com, afaerber@suse.de, fred.konrad@greensocs.com Subject: [Qemu-devel] [RFC PATCH v2 2/3] virtio-pci : add a virtio-bus interface 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 add a VirtioBus in the VirtIOPCIProxy structure. It creates a new device : "virtio-pci" which init the VirtioBus. Two callback are written : * void virtio_pci_init_cb(DeviceState *dev) to initialize the PCI interface after the VirtIODevice init, it is approximately the same as virtio_init_pci. * void virtio_pci_exit_cb(DeviceState *dev) to stop the ioevent before the VirtIODevice exit. Signed-off-by: KONRAD Frederic --- hw/virtio-pci.c | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/virtio-pci.h | 6 +++ 2 files changed, 158 insertions(+) diff --git a/hw/virtio-pci.c b/hw/virtio-pci.c index 71f4fb5..78aa5e8 100644 --- a/hw/virtio-pci.c +++ b/hw/virtio-pci.c @@ -22,6 +22,7 @@ #include "virtio-net.h" #include "virtio-serial.h" #include "virtio-scsi.h" +#include "virtio-bus.h" #include "pci.h" #include "qemu-error.h" #include "msi.h" @@ -1117,15 +1118,166 @@ static TypeInfo virtio_scsi_info = { .instance_size = sizeof(VirtIOPCIProxy), .class_init = virtio_scsi_class_init, }; +/* The new virtio-pci device */ + +void virtio_pci_init_cb(DeviceState *dev) +{ + PCIDevice *d = DO_UPCAST(PCIDevice, qdev, dev); + VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, d); + uint8_t *config; + uint32_t size; + + /* Put the PCI IDs */ + switch (get_virtio_device_id(proxy->bus)) { + + case VIRTIO_ID_BLOCK: + pci_config_set_device_id(proxy->pci_dev.config, + PCI_DEVICE_ID_VIRTIO_BLOCK); + pci_config_set_class(proxy->pci_dev.config, PCI_CLASS_STORAGE_SCSI); + break; + default: + error_report("unknown device id\n"); + break; + + } + + /* virtio_init_pci code without bindings */ + + /* This should disappear */ + proxy->vdev = proxy->bus->vdev; + + config = proxy->pci_dev.config; + + if (proxy->class_code) { + pci_config_set_class(config, proxy->class_code); + } + pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID, + pci_get_word(config + PCI_VENDOR_ID)); + pci_set_word(config + PCI_SUBSYSTEM_ID, proxy->bus->vdev->device_id); + config[PCI_INTERRUPT_PIN] = 1; + + if (proxy->bus->vdev->nvectors && + msix_init_exclusive_bar(&proxy->pci_dev, proxy->bus->vdev->nvectors, + 1)) { + proxy->bus->vdev->nvectors = 0; + } + + proxy->pci_dev.config_write = virtio_write_config; + + size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + + proxy->bus->vdev->config_len; + if (size & (size-1)) { + size = 1 << qemu_fls(size); + } + + memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy, + "virtio-pci", size); + pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO, + &proxy->bar); + + if (!kvm_has_many_ioeventfds()) { + proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD; + } + + /* Bind the VirtIODevice to the VirtioBus. */ + virtio_bus_bind_device(proxy->bus); + + proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY; + proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE; + /* Should be modified */ + proxy->host_features = proxy->bus->vdev->get_features(proxy->bus->vdev, + proxy->host_features); +} + +void virtio_pci_exit_cb(DeviceState *dev) +{ + PCIDevice *d = DO_UPCAST(PCIDevice, qdev, dev); + VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, d); + /* Put the PCI IDs */ + pci_config_set_device_id(proxy->pci_dev.config, 0x0000); + pci_config_set_class(proxy->pci_dev.config, PCI_CLASS_OTHERS); + + virtio_pci_stop_ioeventfd(proxy); +} + +static const struct VirtioBusInfo virtio_bus_info = { + .init_cb = virtio_pci_init_cb, + .exit_cb = virtio_pci_exit_cb, + + .virtio_bindings = { + .notify = virtio_pci_notify, + .save_config = virtio_pci_save_config, + .load_config = virtio_pci_load_config, + .save_queue = virtio_pci_save_queue, + .load_queue = virtio_pci_load_queue, + .get_features = virtio_pci_get_features, + .query_guest_notifiers = virtio_pci_query_guest_notifiers, + .set_host_notifier = virtio_pci_set_host_notifier, + .set_guest_notifiers = virtio_pci_set_guest_notifiers, + .vmstate_change = virtio_pci_vmstate_change, + } +}; + +static int virtiopci_qdev_init(PCIDevice *dev) +{ + VirtIOPCIProxy *s = DO_UPCAST(VirtIOPCIProxy, pci_dev, dev); + DeviceState *qdev = DEVICE(dev); + + /* create a virtio-bus and attach virtio-pci to it */ + s->bus = virtio_bus_new(qdev, &virtio_bus_info); + + return 0; +} + +static Property virtiopci_properties[] = { + /* TODO : Add the correct properties */ + DEFINE_PROP_END_OF_LIST(), +}; + +static void virtiopci_class_init(ObjectClass *oc, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(oc); + PCIDeviceClass *pc = PCI_DEVICE_CLASS(oc); + + pc->init = virtiopci_qdev_init; + pc->exit = virtio_exit_pci; + pc->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; + pc->revision = VIRTIO_PCI_ABI_VERSION; + pc->class_id = PCI_CLASS_OTHERS; + /* TODO : Add the correct device information below */ + /* pc->exit = + * pc->device_id = + * pc->subsystem_vendor_id = + * pc->subsystem_id = + * dc->reset = + * dc->vmsd = + */ + dc->props = virtiopci_properties; + dc->desc = "virtio-pci transport."; +} + +static const TypeInfo virtio_pci_info = { + .name = "virtio-pci", + .parent = TYPE_PCI_DEVICE, + .instance_size = sizeof(VirtIOPCIProxy), + .class_init = virtiopci_class_init, +}; + + +/************************************/ + static void virtio_pci_register_types(void) { + /* This should disappear */ type_register_static(&virtio_blk_info); type_register_static(&virtio_net_info); type_register_static(&virtio_serial_info); type_register_static(&virtio_balloon_info); type_register_static(&virtio_scsi_info); type_register_static(&virtio_rng_info); + /* new virtio-pci device */ + type_register_static(&virtio_pci_info); } type_init(virtio_pci_register_types) diff --git a/hw/virtio-pci.h b/hw/virtio-pci.h index b58d9a2..a7a9847 100644 --- a/hw/virtio-pci.h +++ b/hw/virtio-pci.h @@ -20,6 +20,7 @@ #include "virtio-rng.h" #include "virtio-serial.h" #include "virtio-scsi.h" +#include "virtio-bus.h" /* Performance improves when virtqueue kick processing is decoupled from the * vcpu thread using ioeventfd for some devices. */ @@ -33,6 +34,7 @@ typedef struct { typedef struct { PCIDevice pci_dev; + /* This should be removed */ VirtIODevice *vdev; MemoryRegion bar; uint32_t flags; @@ -51,10 +53,14 @@ typedef struct { bool ioeventfd_disabled; bool ioeventfd_started; VirtIOIRQFD *vector_irqfd; + /* VirtIOBus to connect the VirtIODevice */ + VirtioBus *bus; } VirtIOPCIProxy; void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev); void virtio_pci_reset(DeviceState *d); +void virtio_pci_init_cb(DeviceState *dev); +void virtio_pci_exit_cb(DeviceState *dev); /* Virtio ABI version, if we increment this, we break the guest driver. */ #define VIRTIO_PCI_ABI_VERSION 0