Patchwork [2/8] qdev: add realized property and make adding child bus implied by realize

login
register
mail settings
Submitter Amit Shah
Date Oct. 26, 2012, 11:01 a.m.
Message ID <9e8d4c3f6c86a6ad42203950a474e730be60aa3b.1351248724.git.amit.shah@redhat.com>
Download mbox | patch
Permalink /patch/194436/
State New
Headers show

Comments

Amit Shah - Oct. 26, 2012, 11:01 a.m.
From: Anthony Liguori <aliguori@us.ibm.com>

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
 hw/qdev.c |   36 +++++++++++++++++++++++++++++++++++-
 1 files changed, 35 insertions(+), 1 deletions(-)

Patch

diff --git a/hw/qdev.c b/hw/qdev.c
index 9b9aba3..5e34c22 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -95,7 +95,6 @@  static void bus_add_child(BusState *bus, DeviceState *child)
 void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
 {
     dev->parent_bus = bus;
-    bus_add_child(bus, dev);
 }
 
 /* Create a new device.  This only initializes the device state structure
@@ -151,6 +150,10 @@  int qdev_init(DeviceState *dev)
 
     assert(dev->state == DEV_STATE_CREATED);
 
+    if (dev->parent_bus) {
+        bus_add_child(dev->parent_bus, dev);
+    }
+
     rc = dc->init(dev);
     if (rc < 0) {
         qdev_free(dev);
@@ -654,6 +657,33 @@  void qdev_property_add_static(DeviceState *dev, Property *prop,
     assert_no_error(local_err);
 }
 
+static bool qdev_prop_get_realized(Object *obj, Error **errp)
+{
+    DeviceState *dev = DEVICE(obj);
+
+    return (dev->state == DEV_STATE_INITIALIZED);
+}
+
+static void qdev_prop_set_realized(Object *obj, bool value, Error **errp)
+{
+    DeviceState *dev = DEVICE(obj);
+    bool realized = (dev->state == DEV_STATE_INITIALIZED);
+
+    if (realized == value) {
+        return;
+    }
+
+    if (realized && !value) {
+        error_set(errp, QERR_PERMISSION_DENIED);
+        return;
+    }
+
+    if (qdev_init(dev) < 0) {
+        error_set(errp, QERR_DEVICE_INIT_FAILED, "");
+        return;
+    }
+}
+
 static void device_initfn(Object *obj)
 {
     DeviceState *dev = DEVICE(obj);
@@ -678,6 +708,10 @@  static void device_initfn(Object *obj)
     } while (class != object_class_by_name(TYPE_DEVICE));
     qdev_prop_set_globals(dev);
 
+    object_property_add_bool(obj, "realized",
+                             qdev_prop_get_realized, qdev_prop_set_realized,
+                             NULL);
+
     object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
                              (Object **)&dev->parent_bus, NULL);
 }