This introduces three changes - first, it sets the realized property for BusState in the setter functions for DeviceState so that a call to the (device) setter will also set the value for any children buses appropriately. Second, it introduces a bool called realized_cache that tracks the most recent value of the "realized" property so that an event can be sent when the device is being removed. Third, it reorders code in device_unparent so that unrealize can be recursively called for its children
Signed-off-by: Bandan Das <b...@redhat.com> --- hw/core/qdev.c | 57 +++++++++++++++++++++++++++++++++++++++++--------- include/hw/qdev-core.h | 1 + 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index b503cc8..a51238c 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -740,7 +740,9 @@ static void device_set_realized(Object *obj, bool value, Error **err) { DeviceState *dev = DEVICE(obj); DeviceClass *dc = DEVICE_GET_CLASS(dev); + BusState *bus; Error *local_err = NULL; + bool rcache = dev->realized_cache; if (value && !dev->realized) { if (!obj->parent && local_err == NULL) { @@ -753,8 +755,13 @@ static void device_set_realized(Object *obj, bool value, Error **err) g_free(name); } + dev->realized_cache = true; + if (dc->realize) { dc->realize(dev, &local_err); + if (local_err != NULL) { + goto error; + } } if (qdev_get_vmsd(dev) && local_err == NULL) { @@ -762,24 +769,50 @@ static void device_set_realized(Object *obj, bool value, Error **err) dev->instance_id_alias, dev->alias_required_for_version); } + + QLIST_FOREACH(bus, &dev->child_bus, sibling) { + object_property_set_bool(OBJECT(bus), true, + "realized", &local_err); + + if (local_err != NULL) { + goto error; + } + } + if (dev->hotplugged && local_err == NULL) { device_reset(dev); } + } else if (!value && dev->realized) { + + QLIST_FOREACH(bus, &dev->child_bus, sibling) { + object_property_set_bool(OBJECT(bus), false, + "realized", &local_err); + + if (local_err != NULL) { + goto error; + } + } + if (qdev_get_vmsd(dev)) { vmstate_unregister(dev, qdev_get_vmsd(dev), dev); } + if (dc->unrealize) { dc->unrealize(dev, &local_err); - } - } - if (local_err != NULL) { - error_propagate(err, local_err); - return; + if (local_err != NULL) { + goto error; + } + } } dev->realized = value; + return; + +error: + dev->realized_cache = rcache; + error_propagate(err, local_err); } static void device_initfn(Object *obj) @@ -796,6 +829,7 @@ static void device_initfn(Object *obj) dev->instance_id_alias = -1; dev->realized = false; + dev->realized_cache = false; object_property_add_bool(obj, "realized", device_get_realized, device_set_realized, NULL); @@ -854,15 +888,17 @@ static void device_unparent(Object *obj) DeviceState *dev = DEVICE(obj); BusState *bus; QObject *event_data; - bool have_realized = dev->realized; + /* Call this first so that the change can propagate */ + + if (dev->realized) { + object_property_set_bool(obj, false, "realized", NULL); + } while (dev->num_child_bus) { bus = QLIST_FIRST(&dev->child_bus); qbus_free(bus); } - if (dev->realized) { - object_property_set_bool(obj, false, "realized", NULL); - } + if (dev->parent_bus) { bus_remove_child(dev->parent_bus, dev); object_unref(OBJECT(dev->parent_bus)); @@ -870,9 +906,10 @@ static void device_unparent(Object *obj) } /* Only send event if the device had been completely realized */ - if (have_realized) { + if (!dev->realized && dev->realized_cache) { gchar *path = object_get_canonical_path(OBJECT(dev)); + dev->realized_cache = false; if (dev->id) { event_data = qobject_from_jsonf("{ 'device': %s, 'path': %s }", dev->id, path); diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index 405b029..82f86e7 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -142,6 +142,7 @@ struct DeviceState { int num_child_bus; int instance_id_alias; int alias_required_for_version; + bool realized_cache; }; #define TYPE_BUS "bus" -- 1.8.3.1