Patchwork [RFC,1/3] qdev: Add support for recursive realization

login
register
mail settings
Submitter Andreas Färber
Date July 15, 2013, 1:40 p.m.
Message ID <1373895639-21476-2-git-send-email-afaerber@suse.de>
Download mbox | patch
Permalink /patch/259043/
State New
Headers show

Comments

Andreas Färber - July 15, 2013, 1:40 p.m.
Add realize_children and unrealize_children QOM methods to DeviceClass.
Call them after realized = true and before realized = false respectively.

The default implementation walks busses and realizes their devices.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
[AF: Transferred from Object to DeviceState]
Signed-off-by: Andreas Färber <afaerber@suse.de>
---
 hw/core/qdev.c         | 58 ++++++++++++++++++++++++++++++++++++++++++++++----
 include/hw/qdev-core.h |  4 ++++
 2 files changed, 58 insertions(+), 4 deletions(-)

Patch

diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index 9190a7e..33e874a 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -183,6 +183,29 @@  static void device_realize(DeviceState *dev, Error **err)
     }
 }
 
+static int device_realize_one(DeviceState *dev, void *opaque)
+{
+    Error **errp = opaque;
+    Error *err = NULL;
+
+    object_property_set_bool(OBJECT(dev), true, "realized", &err);
+    if (err != NULL) {
+        error_propagate(errp, err);
+        return -1;
+    }
+    return 0;
+}
+
+static int bus_realize_children(BusState *bus, void *opaque)
+{
+    return qbus_walk_children(bus, device_realize_one, NULL, opaque);
+}
+
+static void device_realize_children(DeviceState *dev, Error **errp)
+{
+    qdev_walk_children(dev, NULL, bus_realize_children, errp);
+}
+
 static void device_unrealize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
@@ -196,6 +219,29 @@  static void device_unrealize(DeviceState *dev, Error **errp)
     }
 }
 
+static int device_unrealize_one(DeviceState *dev, void *opaque)
+{
+    Error **errp = opaque;
+    Error *err = NULL;
+
+    object_property_set_bool(OBJECT(dev), false, "realized", &err);
+    if (err != NULL) {
+        error_propagate(errp, err);
+        return -1;
+    }
+    return 0;
+}
+
+static int bus_unrealize_children(BusState *bus, void *opaque)
+{
+    return qbus_walk_children(bus, device_unrealize_one, NULL, opaque);
+}
+
+static void device_unrealize_children(DeviceState *dev, Error **errp)
+{
+    qdev_walk_children(dev, NULL, bus_unrealize_children, errp);
+}
+
 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
                                  int required_for_version)
 {
@@ -678,7 +724,7 @@  static bool device_get_realized(Object *obj, Error **err)
     return dev->realized;
 }
 
-static void device_set_realized(Object *obj, bool value, Error **err)
+static void device_set_realized(Object *obj, bool value, Error **errp)
 {
     DeviceState *dev = DEVICE(obj);
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
@@ -704,24 +750,26 @@  static void device_set_realized(Object *obj, bool value, Error **err)
                                            dev->instance_id_alias,
                                            dev->alias_required_for_version);
         }
+        dev->realized = true;
+        dc->realize_children(dev, errp);
         if (dev->hotplugged && local_err == NULL) {
             device_reset(dev);
         }
     } else if (!value && dev->realized) {
+        dc->unrealize_children(dev, errp);
         if (qdev_get_vmsd(dev)) {
             vmstate_unregister(dev, qdev_get_vmsd(dev), dev);
         }
         if (dc->unrealize) {
             dc->unrealize(dev, &local_err);
         }
+        dev->realized = false;
     }
 
     if (local_err != NULL) {
-        error_propagate(err, local_err);
+        error_propagate(errp, local_err);
         return;
     }
-
-    dev->realized = value;
 }
 
 static void device_initfn(Object *obj)
@@ -825,7 +873,9 @@  static void device_class_init(ObjectClass *class, void *data)
 
     class->unparent = device_unparent;
     dc->realize = device_realize;
+    dc->realize_children = device_realize_children;
     dc->unrealize = device_unrealize;
+    dc->unrealize_children = device_unrealize_children;
 }
 
 void device_reset(DeviceState *dev)
diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
index 7fbffcb..81dc2f3 100644
--- a/include/hw/qdev-core.h
+++ b/include/hw/qdev-core.h
@@ -21,7 +21,9 @@  typedef int (*qdev_initfn)(DeviceState *dev);
 typedef int (*qdev_event)(DeviceState *dev);
 typedef void (*qdev_resetfn)(DeviceState *dev);
 typedef void (*DeviceRealize)(DeviceState *dev, Error **errp);
+typedef void (*DeviceRealizeChildren)(DeviceState *dev, Error **errp);
 typedef void (*DeviceUnrealize)(DeviceState *dev, Error **errp);
+typedef void (*DeviceUnrealizeChildren)(DeviceState *dev, Error **errp);
 
 struct VMStateDescription;
 
@@ -88,7 +90,9 @@  typedef struct DeviceClass {
     /* callbacks */
     void (*reset)(DeviceState *dev);
     DeviceRealize realize;
+    DeviceRealizeChildren realize_children;
     DeviceUnrealize unrealize;
+    DeviceUnrealizeChildren unrealize_children;
 
     /* device state */
     const struct VMStateDescription *vmsd;