Patchwork [RFC,V8,02/15] virtio-bus : Introduce virtio-bus

login
register
mail settings
Submitter fred.konrad@greensocs.com
Date Dec. 19, 2012, 9:53 a.m.
Message ID <1355910821-21302-3-git-send-email-fred.konrad@greensocs.com>
Download mbox | patch
Permalink /patch/207284/
State New
Headers show

Comments

fred.konrad@greensocs.com - Dec. 19, 2012, 9:53 a.m.
From: KONRAD Frederic <fred.konrad@greensocs.com>

Introduce virtio-bus. Refactored transport device will create a bus which
extends virtio-bus.

Signed-off-by: KONRAD Frederic <fred.konrad@greensocs.com>
---
 hw/Makefile.objs |   1 +
 hw/virtio-bus.c  | 169 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/virtio-bus.h  |  98 ++++++++++++++++++++++++++++++++
 3 files changed, 268 insertions(+)
 create mode 100644 hw/virtio-bus.c
 create mode 100644 hw/virtio-bus.h
Anthony Liguori - Jan. 2, 2013, 2:12 p.m.
fred.konrad@greensocs.com writes:

> From: KONRAD Frederic <fred.konrad@greensocs.com>
>
> Introduce virtio-bus. Refactored transport device will create a bus which
> extends virtio-bus.
>
> Signed-off-by: KONRAD Frederic <fred.konrad@greensocs.com>
> ---
>  hw/Makefile.objs |   1 +
>  hw/virtio-bus.c  | 169 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  hw/virtio-bus.h  |  98 ++++++++++++++++++++++++++++++++
>  3 files changed, 268 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 d581d8d..6fa4de4 100644
> --- a/hw/Makefile.objs
> +++ b/hw/Makefile.objs
> @@ -3,6 +3,7 @@ common-obj-y += loader.o
>  common-obj-$(CONFIG_VIRTIO) += virtio-console.o
>  common-obj-$(CONFIG_VIRTIO) += virtio-rng.o
>  common-obj-$(CONFIG_VIRTIO_PCI) += virtio-pci.o
> +common-obj-$(CONFIG_VIRTIO) += virtio-bus.o
>  common-obj-y += fw_cfg.o
>  common-obj-$(CONFIG_PCI) += pci.o pci_bridge.o pci_bridge_dev.o
>  common-obj-$(CONFIG_PCI) += msix.o msi.o
> diff --git a/hw/virtio-bus.c b/hw/virtio-bus.c
> new file mode 100644
> index 0000000..7a3d06e
> --- /dev/null
> +++ b/hw/virtio-bus.c
> @@ -0,0 +1,169 @@
> +/*
> + * VirtioBus
> + *
> + *  Copyright (C) 2012 : GreenSocs Ltd
> + *      http://www.greensocs.com/ , email: info@greensocs.com
> + *
> + *  Developed by :
> + *  Frederic Konrad   <fred.konrad@greensocs.com>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation, either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License along
> + * with this program; if not, see <http://www.gnu.org/licenses/>.
> + *
> + */
> +
> +#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
> +
> +/* Plug the VirtIODevice */
> +int virtio_bus_plug_device(VirtIODevice *vdev)
> +{
> +    DeviceState *qdev = DEVICE(vdev);
> +    BusState *qbus = BUS(qdev_get_parent_bus(qdev));
> +    VirtioBusState *bus = VIRTIO_BUS(qbus);
> +    VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
> +    DPRINTF("%s : plug device.\n", qbus->name);
> +
> +    bus->vdev = vdev;
> +
> +    if (klass->device_plugged != NULL) {
> +        klass->device_plugged(qbus->parent);
> +    }
> +
> +    /*
> +     * The lines below will disappear when we drop VirtIOBindings, at the end
> +     * of the serie.

s/serie/series/g

> +     */
> +    bus->bindings.notify = klass->notify;
> +    bus->bindings.save_config = klass->save_config;
> +    bus->bindings.save_queue = klass->save_queue;
> +    bus->bindings.load_config = klass->load_config;
> +    bus->bindings.load_queue = klass->load_queue;
> +    bus->bindings.load_done = klass->load_done;
> +    bus->bindings.get_features = klass->get_features;
> +    bus->bindings.query_guest_notifiers = klass->query_guest_notifiers;
> +    bus->bindings.set_guest_notifiers = klass->set_guest_notifiers;
> +    bus->bindings.set_host_notifier = klass->set_host_notifier;
> +    bus->bindings.vmstate_change = klass->vmstate_change;
> +    virtio_bind_device(bus->vdev, &(bus->bindings), qbus->parent);
> +    /*
> +     */

No need for empty comment or the parens around bus->bindings.

> +
> +    return 0;
> +}
> +
> +/* Reset the virtio_bus */
> +void virtio_bus_reset(VirtioBusState *bus)
> +{
> +    DPRINTF("%s : reset device.\n", qbus->name);
> +    if (bus->vdev != NULL) {
> +        virtio_reset(bus->vdev);
> +    }
> +}
> +
> +/* Destroy the VirtIODevice */
> +void virtio_bus_destroy_device(VirtioBusState *bus)
> +{
> +    DeviceState *qdev;
> +    BusState *qbus = BUS(bus);
> +    VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
> +    DPRINTF("%s : remove device.\n", qbus->name);
> +
> +    if (bus->vdev != NULL) {
> +        if (klass->device_unplug != NULL) {
> +            klass->device_unplug(qbus->parent);
> +        }
> +        qdev = DEVICE(bus->vdev);
> +        qdev_free(qdev);
> +        bus->vdev = NULL;
> +    }
> +}
> +
> +/* Get the device id of the plugged device. */
> +uint16_t get_virtio_device_id(VirtioBusState *bus)
> +{
> +    assert(bus->vdev != NULL);
> +    return bus->vdev->device_id;
> +}
> +
> +/* Get the nvectors field of the plugged device. */
> +int get_virtio_device_nvectors(VirtioBusState *bus)
> +{
> +    assert(bus->vdev != NULL);
> +    return bus->vdev->nvectors;
> +}
> +
> +/* Set the nvectors field of the plugged device. */
> +void set_virtio_device_nvectors(VirtioBusState *bus, int nvectors)
> +{
> +    assert(bus->vdev != NULL);
> +    bus->vdev->nvectors = nvectors;
> +}
> +
> +/* Get the config_len field of the plugged device. */
> +size_t get_virtio_device_config_len(VirtioBusState *bus)
> +{
> +    assert(bus->vdev != NULL);
> +    return bus->vdev->config_len;
> +}
> +
> +/* Get the features of the plugged device. */
> +uint32_t get_virtio_device_features(VirtioBusState *bus,
> +                                    uint32_t requested_features)
> +{
> +    VirtioDeviceClass *k;
> +    assert(bus->vdev != NULL);
> +    k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
> +    assert(k->get_features != NULL);
> +    return k->get_features(bus->vdev, requested_features);
> +}
> +
> +/* Get the bad features of the plugged device. */
> +uint32_t get_virtio_device_bad_features(VirtioBusState *bus)
> +{
> +    VirtioDeviceClass *k;
> +    assert(bus->vdev != NULL);
> +    k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
> +    if (k->bad_features != NULL) {
> +        return k->bad_features(bus->vdev);
> +    } else {
> +        return 0;
> +    }
> +}

These functions should be in the form:

virtio_device_get_bad_features()

With 'virtio_device_' as the common prefix.

> +static const TypeInfo virtio_bus_info = {
> +    .name = TYPE_VIRTIO_BUS,
> +    .parent = TYPE_BUS,
> +    .instance_size = sizeof(VirtioBusState),
> +    .abstract = true,
> +    .class_size = sizeof(VirtioBusClass),
> +};
> +
> +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..a2e2012
> --- /dev/null
> +++ b/hw/virtio-bus.h
> @@ -0,0 +1,98 @@
> +/*
> + * VirtioBus
> + *
> + *  Copyright (C) 2012 : GreenSocs Ltd
> + *      http://www.greensocs.com/ , email: info@greensocs.com
> + *
> + *  Developed by :
> + *  Frederic Konrad   <fred.konrad@greensocs.com>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation, either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License along
> + * with this program; if not, see <http://www.gnu.org/licenses/>.
> + *
> + */
> +
> +#ifndef VIRTIO_BUS_H
> +#define VIRTIO_BUS_H
> +
> +#include "qdev.h"
> +#include "sysemu.h"
> +#include "virtio.h"
> +
> +#define TYPE_VIRTIO_BUS "virtio-bus"
> +#define VIRTIO_BUS_GET_CLASS(obj) \
> +        OBJECT_GET_CLASS(VirtioBusClass, obj, TYPE_VIRTIO_BUS)
> +#define VIRTIO_BUS_CLASS(klass) \
> +        OBJECT_CLASS_CHECK(VirtioBusClass, klass, TYPE_VIRTIO_BUS)
> +#define VIRTIO_BUS(obj) OBJECT_CHECK(VirtioBusState, (obj), TYPE_VIRTIO_BUS)
> +
> +typedef struct VirtioBusState VirtioBusState;
> +
> +typedef struct VirtioBusClass {
> +    /* This is what a VirtioBus must implement */
> +    BusClass parent;
> +    void (*notify)(void *opaque, uint16_t vector);
> +    void (*save_config)(void *opaque, QEMUFile *f);
> +    void (*save_queue)(void *opaque, int n, QEMUFile *f);
> +    int (*load_config)(void *opaque, QEMUFile *f);
> +    int (*load_queue)(void *opaque, int n, QEMUFile *f);
> +    int (*load_done)(void *opaque, QEMUFile *f);
> +    unsigned (*get_features)(void *opaque);
> +    bool (*query_guest_notifiers)(void *opaque);
> +    int (*set_guest_notifiers)(void *opaque, bool assigned);
> +    int (*set_host_notifier)(void *opaque, int n, bool assigned);
> +    void (*vmstate_change)(void *opaque, bool running);
> +    /*
> +     * transport independent init function.
> +     * This is called by virtio-bus just after the device is plugged.
> +     */
> +    void (*device_plugged)(void *opaque);
> +    /*
> +     * transport independent exit function.
> +     * This is called by virtio-bus just before the device is unplugged.
> +     */
> +    void (*device_unplug)(void *opaque);

These should not take a 'void *' but rather a VirtIODevice.  You can
just do a cast in the short term to handle the mismatch with
VirtIOBindings.

Regards,

Anthony Liguori

> +} VirtioBusClass;
> +
> +struct VirtioBusState {
> +    BusState parent_obj;
> +    /*
> +     * Only one VirtIODevice can be plugged on the bus.
> +     */
> +    VirtIODevice *vdev;
> +    /*
> +     * This will be removed at the end of the serie.
> +     */
> +    VirtIOBindings bindings;
> +    /*
> +     */
> +};
> +
> +int virtio_bus_plug_device(VirtIODevice *vdev);
> +void virtio_bus_reset(VirtioBusState *bus);
> +void virtio_bus_destroy_device(VirtioBusState *bus);
> +/* Get the device id of the plugged device. */
> +uint16_t get_virtio_device_id(VirtioBusState *bus);
> +/* Get the nvectors field of the plugged device. */
> +int get_virtio_device_nvectors(VirtioBusState *bus);
> +/* Set the nvectors field of the plugged device. */
> +void set_virtio_device_nvectors(VirtioBusState *bus, int nvectors);
> +/* Get the config_len field of the plugged device. */
> +size_t get_virtio_device_config_len(VirtioBusState *bus);
> +/* Get the features of the plugged device. */
> +uint32_t get_virtio_device_features(VirtioBusState *bus,
> +                                    uint32_t requested_features);
> +/* Get the bad features of the plugged device. */
> +uint32_t get_virtio_device_bad_features(VirtioBusState *bus);
> +
> +#endif /* VIRTIO_BUS_H */
> -- 
> 1.7.11.7
fred.konrad@greensocs.com - Jan. 3, 2013, 9:57 a.m.
On 02/01/2013 15:12, Anthony Liguori wrote:
> fred.konrad@greensocs.com writes:
>
>> From: KONRAD Frederic <fred.konrad@greensocs.com>
>>
>> Introduce virtio-bus. Refactored transport device will create a bus which
>> extends virtio-bus.
>>
>> Signed-off-by: KONRAD Frederic <fred.konrad@greensocs.com>
>> ---
>>   hw/Makefile.objs |   1 +
>>   hw/virtio-bus.c  | 169 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>   hw/virtio-bus.h  |  98 ++++++++++++++++++++++++++++++++
>>   3 files changed, 268 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 d581d8d..6fa4de4 100644
>> --- a/hw/Makefile.objs
>> +++ b/hw/Makefile.objs
>> @@ -3,6 +3,7 @@ common-obj-y += loader.o
>>   common-obj-$(CONFIG_VIRTIO) += virtio-console.o
>>   common-obj-$(CONFIG_VIRTIO) += virtio-rng.o
>>   common-obj-$(CONFIG_VIRTIO_PCI) += virtio-pci.o
>> +common-obj-$(CONFIG_VIRTIO) += virtio-bus.o
>>   common-obj-y += fw_cfg.o
>>   common-obj-$(CONFIG_PCI) += pci.o pci_bridge.o pci_bridge_dev.o
>>   common-obj-$(CONFIG_PCI) += msix.o msi.o
>> diff --git a/hw/virtio-bus.c b/hw/virtio-bus.c
>> new file mode 100644
>> index 0000000..7a3d06e
>> --- /dev/null
>> +++ b/hw/virtio-bus.c
>> @@ -0,0 +1,169 @@
>> +/*
>> + * VirtioBus
>> + *
>> + *  Copyright (C) 2012 : GreenSocs Ltd
>> + *      http://www.greensocs.com/ , email: info@greensocs.com
>> + *
>> + *  Developed by :
>> + *  Frederic Konrad   <fred.konrad@greensocs.com>
>> + *
>> + * This program is free software; you can redistribute it and/or modify
>> + * it under the terms of the GNU General Public License as published by
>> + * the Free Software Foundation, either version 2 of the License, or
>> + * (at your option) any later version.
>> + *
>> + * This program is distributed in the hope that it will be useful,
>> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> + * GNU General Public License for more details.
>> + *
>> + * You should have received a copy of the GNU General Public License along
>> + * with this program; if not, see <http://www.gnu.org/licenses/>.
>> + *
>> + */
>> +
>> +#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
>> +
>> +/* Plug the VirtIODevice */
>> +int virtio_bus_plug_device(VirtIODevice *vdev)
>> +{
>> +    DeviceState *qdev = DEVICE(vdev);
>> +    BusState *qbus = BUS(qdev_get_parent_bus(qdev));
>> +    VirtioBusState *bus = VIRTIO_BUS(qbus);
>> +    VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
>> +    DPRINTF("%s : plug device.\n", qbus->name);
>> +
>> +    bus->vdev = vdev;
>> +
>> +    if (klass->device_plugged != NULL) {
>> +        klass->device_plugged(qbus->parent);
>> +    }
>> +
>> +    /*
>> +     * The lines below will disappear when we drop VirtIOBindings, at the end
>> +     * of the serie.
> s/serie/series/g
>
>> +     */
>> +    bus->bindings.notify = klass->notify;
>> +    bus->bindings.save_config = klass->save_config;
>> +    bus->bindings.save_queue = klass->save_queue;
>> +    bus->bindings.load_config = klass->load_config;
>> +    bus->bindings.load_queue = klass->load_queue;
>> +    bus->bindings.load_done = klass->load_done;
>> +    bus->bindings.get_features = klass->get_features;
>> +    bus->bindings.query_guest_notifiers = klass->query_guest_notifiers;
>> +    bus->bindings.set_guest_notifiers = klass->set_guest_notifiers;
>> +    bus->bindings.set_host_notifier = klass->set_host_notifier;
>> +    bus->bindings.vmstate_change = klass->vmstate_change;
>> +    virtio_bind_device(bus->vdev, &(bus->bindings), qbus->parent);
>> +    /*
>> +     */
> No need for empty comment or the parens around bus->bindings.
>
>> +
>> +    return 0;
>> +}
>> +
>> +/* Reset the virtio_bus */
>> +void virtio_bus_reset(VirtioBusState *bus)
>> +{
>> +    DPRINTF("%s : reset device.\n", qbus->name);
>> +    if (bus->vdev != NULL) {
>> +        virtio_reset(bus->vdev);
>> +    }
>> +}
>> +
>> +/* Destroy the VirtIODevice */
>> +void virtio_bus_destroy_device(VirtioBusState *bus)
>> +{
>> +    DeviceState *qdev;
>> +    BusState *qbus = BUS(bus);
>> +    VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
>> +    DPRINTF("%s : remove device.\n", qbus->name);
>> +
>> +    if (bus->vdev != NULL) {
>> +        if (klass->device_unplug != NULL) {
>> +            klass->device_unplug(qbus->parent);
>> +        }
>> +        qdev = DEVICE(bus->vdev);
>> +        qdev_free(qdev);
>> +        bus->vdev = NULL;
>> +    }
>> +}
>> +
>> +/* Get the device id of the plugged device. */
>> +uint16_t get_virtio_device_id(VirtioBusState *bus)
>> +{
>> +    assert(bus->vdev != NULL);
>> +    return bus->vdev->device_id;
>> +}
>> +
>> +/* Get the nvectors field of the plugged device. */
>> +int get_virtio_device_nvectors(VirtioBusState *bus)
>> +{
>> +    assert(bus->vdev != NULL);
>> +    return bus->vdev->nvectors;
>> +}
>> +
>> +/* Set the nvectors field of the plugged device. */
>> +void set_virtio_device_nvectors(VirtioBusState *bus, int nvectors)
>> +{
>> +    assert(bus->vdev != NULL);
>> +    bus->vdev->nvectors = nvectors;
>> +}
>> +
>> +/* Get the config_len field of the plugged device. */
>> +size_t get_virtio_device_config_len(VirtioBusState *bus)
>> +{
>> +    assert(bus->vdev != NULL);
>> +    return bus->vdev->config_len;
>> +}
>> +
>> +/* Get the features of the plugged device. */
>> +uint32_t get_virtio_device_features(VirtioBusState *bus,
>> +                                    uint32_t requested_features)
>> +{
>> +    VirtioDeviceClass *k;
>> +    assert(bus->vdev != NULL);
>> +    k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
>> +    assert(k->get_features != NULL);
>> +    return k->get_features(bus->vdev, requested_features);
>> +}
>> +
>> +/* Get the bad features of the plugged device. */
>> +uint32_t get_virtio_device_bad_features(VirtioBusState *bus)
>> +{
>> +    VirtioDeviceClass *k;
>> +    assert(bus->vdev != NULL);
>> +    k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
>> +    if (k->bad_features != NULL) {
>> +        return k->bad_features(bus->vdev);
>> +    } else {
>> +        return 0;
>> +    }
>> +}
> These functions should be in the form:
>
> virtio_device_get_bad_features()
>
> With 'virtio_device_' as the common prefix.
>
>> +static const TypeInfo virtio_bus_info = {
>> +    .name = TYPE_VIRTIO_BUS,
>> +    .parent = TYPE_BUS,
>> +    .instance_size = sizeof(VirtioBusState),
>> +    .abstract = true,
>> +    .class_size = sizeof(VirtioBusClass),
>> +};
>> +
>> +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..a2e2012
>> --- /dev/null
>> +++ b/hw/virtio-bus.h
>> @@ -0,0 +1,98 @@
>> +/*
>> + * VirtioBus
>> + *
>> + *  Copyright (C) 2012 : GreenSocs Ltd
>> + *      http://www.greensocs.com/ , email: info@greensocs.com
>> + *
>> + *  Developed by :
>> + *  Frederic Konrad   <fred.konrad@greensocs.com>
>> + *
>> + * This program is free software; you can redistribute it and/or modify
>> + * it under the terms of the GNU General Public License as published by
>> + * the Free Software Foundation, either version 2 of the License, or
>> + * (at your option) any later version.
>> + *
>> + * This program is distributed in the hope that it will be useful,
>> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> + * GNU General Public License for more details.
>> + *
>> + * You should have received a copy of the GNU General Public License along
>> + * with this program; if not, see <http://www.gnu.org/licenses/>.
>> + *
>> + */
>> +
>> +#ifndef VIRTIO_BUS_H
>> +#define VIRTIO_BUS_H
>> +
>> +#include "qdev.h"
>> +#include "sysemu.h"
>> +#include "virtio.h"
>> +
>> +#define TYPE_VIRTIO_BUS "virtio-bus"
>> +#define VIRTIO_BUS_GET_CLASS(obj) \
>> +        OBJECT_GET_CLASS(VirtioBusClass, obj, TYPE_VIRTIO_BUS)
>> +#define VIRTIO_BUS_CLASS(klass) \
>> +        OBJECT_CLASS_CHECK(VirtioBusClass, klass, TYPE_VIRTIO_BUS)
>> +#define VIRTIO_BUS(obj) OBJECT_CHECK(VirtioBusState, (obj), TYPE_VIRTIO_BUS)
>> +
>> +typedef struct VirtioBusState VirtioBusState;
>> +
>> +typedef struct VirtioBusClass {
>> +    /* This is what a VirtioBus must implement */
>> +    BusClass parent;
>> +    void (*notify)(void *opaque, uint16_t vector);
>> +    void (*save_config)(void *opaque, QEMUFile *f);
>> +    void (*save_queue)(void *opaque, int n, QEMUFile *f);
>> +    int (*load_config)(void *opaque, QEMUFile *f);
>> +    int (*load_queue)(void *opaque, int n, QEMUFile *f);
>> +    int (*load_done)(void *opaque, QEMUFile *f);
>> +    unsigned (*get_features)(void *opaque);
>> +    bool (*query_guest_notifiers)(void *opaque);
>> +    int (*set_guest_notifiers)(void *opaque, bool assigned);
>> +    int (*set_host_notifier)(void *opaque, int n, bool assigned);
>> +    void (*vmstate_change)(void *opaque, bool running);
>> +    /*
>> +     * transport independent init function.
>> +     * This is called by virtio-bus just after the device is plugged.
>> +     */
>> +    void (*device_plugged)(void *opaque);
>> +    /*
>> +     * transport independent exit function.
>> +     * This is called by virtio-bus just before the device is unplugged.
>> +     */
>> +    void (*device_unplug)(void *opaque);
> These should not take a 'void *' but rather a VirtIODevice.  You can
> just do a cast in the short term to handle the mismatch with
> VirtIOBindings.
Are you sure ?

Here the opaque argument is not pointing a VirtIODevice but a transport.

eg for virtio-pci :

static void virtio_pci_notify(void *opaque, uint16_t vector)
{
     VirtIOPCIProxy *proxy = opaque;
     if (msix_enabled(&proxy->pci_dev))
         msix_notify(&proxy->pci_dev, vector);
     else
         qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
}

>
> Regards,
>
> Anthony Liguori
>
>> +} VirtioBusClass;
>> +
>> +struct VirtioBusState {
>> +    BusState parent_obj;
>> +    /*
>> +     * Only one VirtIODevice can be plugged on the bus.
>> +     */
>> +    VirtIODevice *vdev;
>> +    /*
>> +     * This will be removed at the end of the serie.
>> +     */
>> +    VirtIOBindings bindings;
>> +    /*
>> +     */
>> +};
>> +
>> +int virtio_bus_plug_device(VirtIODevice *vdev);
>> +void virtio_bus_reset(VirtioBusState *bus);
>> +void virtio_bus_destroy_device(VirtioBusState *bus);
>> +/* Get the device id of the plugged device. */
>> +uint16_t get_virtio_device_id(VirtioBusState *bus);
>> +/* Get the nvectors field of the plugged device. */
>> +int get_virtio_device_nvectors(VirtioBusState *bus);
>> +/* Set the nvectors field of the plugged device. */
>> +void set_virtio_device_nvectors(VirtioBusState *bus, int nvectors);
>> +/* Get the config_len field of the plugged device. */
>> +size_t get_virtio_device_config_len(VirtioBusState *bus);
>> +/* Get the features of the plugged device. */
>> +uint32_t get_virtio_device_features(VirtioBusState *bus,
>> +                                    uint32_t requested_features);
>> +/* Get the bad features of the plugged device. */
>> +uint32_t get_virtio_device_bad_features(VirtioBusState *bus);
>> +
>> +#endif /* VIRTIO_BUS_H */
>> -- 
>> 1.7.11.7

Patch

diff --git a/hw/Makefile.objs b/hw/Makefile.objs
index d581d8d..6fa4de4 100644
--- a/hw/Makefile.objs
+++ b/hw/Makefile.objs
@@ -3,6 +3,7 @@  common-obj-y += loader.o
 common-obj-$(CONFIG_VIRTIO) += virtio-console.o
 common-obj-$(CONFIG_VIRTIO) += virtio-rng.o
 common-obj-$(CONFIG_VIRTIO_PCI) += virtio-pci.o
+common-obj-$(CONFIG_VIRTIO) += virtio-bus.o
 common-obj-y += fw_cfg.o
 common-obj-$(CONFIG_PCI) += pci.o pci_bridge.o pci_bridge_dev.o
 common-obj-$(CONFIG_PCI) += msix.o msi.o
diff --git a/hw/virtio-bus.c b/hw/virtio-bus.c
new file mode 100644
index 0000000..7a3d06e
--- /dev/null
+++ b/hw/virtio-bus.c
@@ -0,0 +1,169 @@ 
+/*
+ * VirtioBus
+ *
+ *  Copyright (C) 2012 : GreenSocs Ltd
+ *      http://www.greensocs.com/ , email: info@greensocs.com
+ *
+ *  Developed by :
+ *  Frederic Konrad   <fred.konrad@greensocs.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#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
+
+/* Plug the VirtIODevice */
+int virtio_bus_plug_device(VirtIODevice *vdev)
+{
+    DeviceState *qdev = DEVICE(vdev);
+    BusState *qbus = BUS(qdev_get_parent_bus(qdev));
+    VirtioBusState *bus = VIRTIO_BUS(qbus);
+    VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
+    DPRINTF("%s : plug device.\n", qbus->name);
+
+    bus->vdev = vdev;
+
+    if (klass->device_plugged != NULL) {
+        klass->device_plugged(qbus->parent);
+    }
+
+    /*
+     * The lines below will disappear when we drop VirtIOBindings, at the end
+     * of the serie.
+     */
+    bus->bindings.notify = klass->notify;
+    bus->bindings.save_config = klass->save_config;
+    bus->bindings.save_queue = klass->save_queue;
+    bus->bindings.load_config = klass->load_config;
+    bus->bindings.load_queue = klass->load_queue;
+    bus->bindings.load_done = klass->load_done;
+    bus->bindings.get_features = klass->get_features;
+    bus->bindings.query_guest_notifiers = klass->query_guest_notifiers;
+    bus->bindings.set_guest_notifiers = klass->set_guest_notifiers;
+    bus->bindings.set_host_notifier = klass->set_host_notifier;
+    bus->bindings.vmstate_change = klass->vmstate_change;
+    virtio_bind_device(bus->vdev, &(bus->bindings), qbus->parent);
+    /*
+     */
+
+    return 0;
+}
+
+/* Reset the virtio_bus */
+void virtio_bus_reset(VirtioBusState *bus)
+{
+    DPRINTF("%s : reset device.\n", qbus->name);
+    if (bus->vdev != NULL) {
+        virtio_reset(bus->vdev);
+    }
+}
+
+/* Destroy the VirtIODevice */
+void virtio_bus_destroy_device(VirtioBusState *bus)
+{
+    DeviceState *qdev;
+    BusState *qbus = BUS(bus);
+    VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
+    DPRINTF("%s : remove device.\n", qbus->name);
+
+    if (bus->vdev != NULL) {
+        if (klass->device_unplug != NULL) {
+            klass->device_unplug(qbus->parent);
+        }
+        qdev = DEVICE(bus->vdev);
+        qdev_free(qdev);
+        bus->vdev = NULL;
+    }
+}
+
+/* Get the device id of the plugged device. */
+uint16_t get_virtio_device_id(VirtioBusState *bus)
+{
+    assert(bus->vdev != NULL);
+    return bus->vdev->device_id;
+}
+
+/* Get the nvectors field of the plugged device. */
+int get_virtio_device_nvectors(VirtioBusState *bus)
+{
+    assert(bus->vdev != NULL);
+    return bus->vdev->nvectors;
+}
+
+/* Set the nvectors field of the plugged device. */
+void set_virtio_device_nvectors(VirtioBusState *bus, int nvectors)
+{
+    assert(bus->vdev != NULL);
+    bus->vdev->nvectors = nvectors;
+}
+
+/* Get the config_len field of the plugged device. */
+size_t get_virtio_device_config_len(VirtioBusState *bus)
+{
+    assert(bus->vdev != NULL);
+    return bus->vdev->config_len;
+}
+
+/* Get the features of the plugged device. */
+uint32_t get_virtio_device_features(VirtioBusState *bus,
+                                    uint32_t requested_features)
+{
+    VirtioDeviceClass *k;
+    assert(bus->vdev != NULL);
+    k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
+    assert(k->get_features != NULL);
+    return k->get_features(bus->vdev, requested_features);
+}
+
+/* Get the bad features of the plugged device. */
+uint32_t get_virtio_device_bad_features(VirtioBusState *bus)
+{
+    VirtioDeviceClass *k;
+    assert(bus->vdev != NULL);
+    k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
+    if (k->bad_features != NULL) {
+        return k->bad_features(bus->vdev);
+    } else {
+        return 0;
+    }
+}
+
+static const TypeInfo virtio_bus_info = {
+    .name = TYPE_VIRTIO_BUS,
+    .parent = TYPE_BUS,
+    .instance_size = sizeof(VirtioBusState),
+    .abstract = true,
+    .class_size = sizeof(VirtioBusClass),
+};
+
+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..a2e2012
--- /dev/null
+++ b/hw/virtio-bus.h
@@ -0,0 +1,98 @@ 
+/*
+ * VirtioBus
+ *
+ *  Copyright (C) 2012 : GreenSocs Ltd
+ *      http://www.greensocs.com/ , email: info@greensocs.com
+ *
+ *  Developed by :
+ *  Frederic Konrad   <fred.konrad@greensocs.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef VIRTIO_BUS_H
+#define VIRTIO_BUS_H
+
+#include "qdev.h"
+#include "sysemu.h"
+#include "virtio.h"
+
+#define TYPE_VIRTIO_BUS "virtio-bus"
+#define VIRTIO_BUS_GET_CLASS(obj) \
+        OBJECT_GET_CLASS(VirtioBusClass, obj, TYPE_VIRTIO_BUS)
+#define VIRTIO_BUS_CLASS(klass) \
+        OBJECT_CLASS_CHECK(VirtioBusClass, klass, TYPE_VIRTIO_BUS)
+#define VIRTIO_BUS(obj) OBJECT_CHECK(VirtioBusState, (obj), TYPE_VIRTIO_BUS)
+
+typedef struct VirtioBusState VirtioBusState;
+
+typedef struct VirtioBusClass {
+    /* This is what a VirtioBus must implement */
+    BusClass parent;
+    void (*notify)(void *opaque, uint16_t vector);
+    void (*save_config)(void *opaque, QEMUFile *f);
+    void (*save_queue)(void *opaque, int n, QEMUFile *f);
+    int (*load_config)(void *opaque, QEMUFile *f);
+    int (*load_queue)(void *opaque, int n, QEMUFile *f);
+    int (*load_done)(void *opaque, QEMUFile *f);
+    unsigned (*get_features)(void *opaque);
+    bool (*query_guest_notifiers)(void *opaque);
+    int (*set_guest_notifiers)(void *opaque, bool assigned);
+    int (*set_host_notifier)(void *opaque, int n, bool assigned);
+    void (*vmstate_change)(void *opaque, bool running);
+    /*
+     * transport independent init function.
+     * This is called by virtio-bus just after the device is plugged.
+     */
+    void (*device_plugged)(void *opaque);
+    /*
+     * transport independent exit function.
+     * This is called by virtio-bus just before the device is unplugged.
+     */
+    void (*device_unplug)(void *opaque);
+} VirtioBusClass;
+
+struct VirtioBusState {
+    BusState parent_obj;
+    /*
+     * Only one VirtIODevice can be plugged on the bus.
+     */
+    VirtIODevice *vdev;
+    /*
+     * This will be removed at the end of the serie.
+     */
+    VirtIOBindings bindings;
+    /*
+     */
+};
+
+int virtio_bus_plug_device(VirtIODevice *vdev);
+void virtio_bus_reset(VirtioBusState *bus);
+void virtio_bus_destroy_device(VirtioBusState *bus);
+/* Get the device id of the plugged device. */
+uint16_t get_virtio_device_id(VirtioBusState *bus);
+/* Get the nvectors field of the plugged device. */
+int get_virtio_device_nvectors(VirtioBusState *bus);
+/* Set the nvectors field of the plugged device. */
+void set_virtio_device_nvectors(VirtioBusState *bus, int nvectors);
+/* Get the config_len field of the plugged device. */
+size_t get_virtio_device_config_len(VirtioBusState *bus);
+/* Get the features of the plugged device. */
+uint32_t get_virtio_device_features(VirtioBusState *bus,
+                                    uint32_t requested_features);
+/* Get the bad features of the plugged device. */
+uint32_t get_virtio_device_bad_features(VirtioBusState *bus);
+
+#endif /* VIRTIO_BUS_H */