Introduce virtio-bus.
v3 cover Introduce virtio-bus.
From: KONRAD Frederic <[email protected]>
As said on IRC, I send only VirtioBus as old implementation of VirtioPCI is not longer making sense after the changes on VirtioBus.
As anthony said, the better way to do this refactoring is :
* Introducing a virtio-bus which extends bus-state.
* Implementing a virtio-pci-bus which extends virtio-bus.
* Implementing virtio-device which extends device-states.
This patch introduce virtio-bus, which is abstract and extends bus-state.
The VirtioBusClass still has init and exit methods, which must be called by the VirtIODevice, and have all the VirtioBindings methods. The futur virtio-x-bus, will have to implement this methods to be a virtio-bus.
The second patch is a modification of qdev-monitor.c, it forces the function qbus_find_recursive(..) to return a non full bus, and return an error if the desired bus ( with "bus=" option ) is full. It add a max_dev field to the bus_state structure. If max_dev=0 it has no limitation, if not the maximum amount of device connected to the bus is max_dev.
Changes v2 -> v3:
* Added VirtioBusClass.
* Renamed VirtioBus -> VirtioBusState.
* Renamed qbus -> parent_obj.
* Plug the device only in a non-full bus.
Changes v1 -> v2:
* All the little fix you suggest ( License, Debug printf, naming convention, ...)
* Added get_virtio_device_id(), and remove the pci_id* from the VirtioBus structure.
* Added virtio_bus_reset().
* Added cast macros VIRTIO_BUS.
* Added virtio_bus_plug_device.
* Replaced the old-style "bus->qbus" by BUS() macro.
Fred
KONRAD Frederic (2):
virtio-bus : Introduce virtio-bus
qbus : add a maximum device.
hw/qdev-core.h | 1 + hw/qdev-monitor.c | 12 +++++ hw/virtio-bus.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/virtio-bus.h | 74 +++++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+), 0 deletions(-) create mode 100644 hw/virtio-bus.c create mode 100644 hw/virtio-bus.h
RFC PATCH v3 1/2 virtio-bus : Introduce virtio-bus
From: KONRAD Frederic <[email protected]>
This patch create VirtioBus which is abstract, we can create virtio-x-bus which extends this virtio-bus.
The virtio-x-bus need to implement all the methods from the VirtioBusClass.
--- hw/virtio-bus.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/virtio-bus.h | 74 ++++++++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+), 0 deletions(-) create mode 100644 hw/virtio-bus.c create mode 100644 hw/virtio-bus.h
hw/virtio-bus.c new file +118 -0
@@ -0,0 +1,118 @@ +/* +* VirtioBus +* +* Copyright (C) 2012 : GreenSocs Ltd +* http://www.greensocs.com/ , email: [email protected] +* +* Developed by : +* Frederic Konrad <[email protected]> +* +* 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 + +/* Is the bus used ? */ +static inline int virtio_bus_in_use (VirtioBusState *bus) +{ +return (bus->vdev !=NULL ); +} + +/* Plug the VirtIODevice */ +int virtio_bus_plug_device (VirtioBusState *bus,VirtIODevice *vdev) +{ +BusState *qbus =BUS (bus); +VirtioBusClass *klass =VIRTIO_BUS_GET_CLASS (bus); +DPRINTF ("plug device into %s.\n" , qbus->name); +if (virtio_bus_in_use (bus)) { +/* that couldn't be reached if we modify qdev-monitor.c */ +error_report ("%s in use.\n" , qbus->name); +return -1 ; + } + +/* keep the VirtIODevice in the VirtioBus. */ + bus->vdev = vdev; + +/* call the "transport" callback. */ +if (klass->init !=NULL ) { + klass->init (qbus->parent); + } +return 0 ; +} + +/* Bind the VirtIODevice to the VirtioBus. */ +void virtio_bus_bind_device (VirtioBusState *bus) +{ +BusState *qbus =BUS (bus); +VirtioBusClass *klass =VIRTIO_BUS_GET_CLASS (bus); +assert (bus !=NULL ); +assert (bus->vdev !=NULL ); +/* +* Can we do that this way ? +*/ + 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 the virtio device id of the plugged device. */ +uint16_t get_virtio_device_id (VirtioBusState *bus) +{ +return bus->vdev->device_id; +} + +static void virtio_bus_class_init (ObjectClass *klass,void *data) +{ +/* Set the default value here. */ +} + +static TypeInfo virtio_bus_info = { + .name =TYPE_VIRTIO_BUS , + .parent =TYPE_BUS , + .instance_size =sizeof (VirtioBusState), +/* Abstract the VIRTIO-BUS */ + .class_init = virtio_bus_class_init, + .abstract = true, + .class_size =sizeof (VirtioBusClass), +}; + +static void virtio_register_types (void ) +{ +type_register_static (&virtio_bus_info); +} + +type_init (virtio_register_types)
hw/virtio-bus.h new file +74 -0
74 lines not on this page · raw message
Signature · 1 line
1.7.1
RFC PATCH v3 2/2 qbus : add a maximum device.
In reply to [email protected]From: KONRAD Frederic <[email protected]>
Only one device can be connected to virtio-bus.
This patch add a field max_dev which is :
* the maximum amount of devices connected on the bus ( when
* max_dev!=0 ).
* have no effect ( when max_dev=0 ).
The function qbus_find_recursive is modified :
* to return a non full bus when the "bus=" option is not present.
* to give an error when the "bus=" option is pointing a full bus.
--- hw/qdev-core.h | 1 + hw/qdev-monitor.c | 12 ++++++++++++ 2 files changed, 13 insertions(+), 0 deletions(-)
hw/qdev-core.h +1 -0
@@ -118,6 +118,7 @@ struct BusState {bool qom_allocated;bool glib_allocated;int max_index; +int max_dev; QTAILQ_HEAD(ChildrenHead, BusChild) children;QLIST_ENTRY (BusState) sibling; };
hw/qdev-monitor.c +12 -0
@@ -293,6 +293,18 @@ static BusState *qbus_find_recursive(BusState *bus, const char *name, (strcmp(object_get_typename(OBJECT(bus)), bus_typename) != 0)) { match =0 ; } + +/* Check if max_dev is reached */ +if ((bus->max_dev !=0 ) && (bus->max_dev <= bus->max_index)) { +if (name !=NULL ) { +error_report ("maximum amount of devices reached for %s\n" , + bus->name); +return NULL ; + }else { + match =0 ; + } + } +if (match) {return bus; }
Signature · 1 line
1.7.1