From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:58990) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tsls2-0006no-2U for qemu-devel@nongnu.org; Tue, 08 Jan 2013 21:58:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Tslrz-0004wN-PU for qemu-devel@nongnu.org; Tue, 08 Jan 2013 21:58:25 -0500 Received: from cantor2.suse.de ([195.135.220.15]:55847 helo=mx2.suse.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tslrz-0004wH-D2 for qemu-devel@nongnu.org; Tue, 08 Jan 2013 21:58:23 -0500 From: =?UTF-8?q?Andreas=20F=C3=A4rber?= Date: Wed, 9 Jan 2013 03:58:11 +0100 Message-Id: <1357700291-15024-3-git-send-email-afaerber@suse.de> In-Reply-To: <1357700291-15024-1-git-send-email-afaerber@suse.de> References: <1357700291-15024-1-git-send-email-afaerber@suse.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH v3 2/2] qdev: Prepare "realized" property List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, ehabkost@redhat.com, anthony@codemonkey.ws, =?UTF-8?q?Andreas=20F=C3=A4rber?= Introduce the QOM realizefn suggested by Anthony. Detailed documentation is supplied in the qdev header. For now this implements a default DeviceClass::realize callback that just wraps DeviceClass::init, which it deprecates. Once all devices have been converted to DeviceClass::realize, DeviceClass::init is to be removed. Signed-off-by: Paolo Bonzini Signed-off-by: Andreas F=C3=A4rber Cc: Anthony Liguori --- hw/qdev-core.h | 52 +++++++++++++++++++++++++++++- hw/qdev.c | 96 ++++++++++++++++++++++++++++++++++++++++++--------= ------ 2 Dateien ge=C3=A4ndert, 123 Zeilen hinzugef=C3=BCgt(+), 25 Zeilen entfe= rnt(-) diff --git a/hw/qdev-core.h b/hw/qdev-core.h index 494902d..487a2bf 100644 --- a/hw/qdev-core.h +++ b/hw/qdev-core.h @@ -20,11 +20,59 @@ enum { 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 (*DeviceUnrealize)(DeviceState *dev, Error **errp); =20 struct VMStateDescription; =20 +/** + * DeviceClass: + * @props: Properties accessing state fields. + * @realize: Callback function invoked when the #DeviceState:realized + * property is changed to %true. The default invokes @init if not %NULL. + * @unrealize: Callback function invoked when the #DeviceState:realized + * property is changed to %false. + * @init: Callback function invoked when the #DeviceState::realized prop= erty + * is changed to %true. Deprecated, new types inheriting directly from + * TYPE_DEVICE should use @realize instead, new leaf types should consul= t + * their respective parent type. + * + * # Realization # + * Devices are constructed in two stages, + * 1) object instantiation via object_initialize() and + * 2) device realization via #DeviceState:realized property. + * The former may not fail (it might assert or exit), the latter may ret= urn + * error information to the caller and must be re-entrant. + * Trivial field initializations should go into #TypeInfo.instance_init. + * Operations depending on @props static properties should go into @real= ize. + * After successful realization, setting static properties will fail. + * + * As an interim step, the #DeviceState:realized property is set by depr= ecated + * functions qdev_init() and qdev_init_nofail(). + * In the future, devices will propagate this state change to their chil= dren + * and along busses they expose. + * The point in time will be deferred to machine creation, so that value= s + * set in @realize will not be introspectable beforehand. Therefore devi= ces + * must not create children during @realize; they should initialize them= via + * object_initialize() in their own #TypeInfo.instance_init and forward = the + * realization events appropriately. + * + * The @init callback is considered private to a particular bus implemen= tation + * (immediate abstract child types of TYPE_DEVICE). Derived leaf types s= et an + * "init" callback on their parent class instead. + * Any type may override the @realize and/or @unrealize callbacks but ne= eds + * to call (and thus save) the parent type's implementation if so desire= d. + * Usually this means storing the previous value of, e.g., @realized ins= ide + * the type's class structure and overwriting it with a function that fi= rst + * invokes the stored callback, then performs any additional steps. + * If a type derived directly from TYPE_DEVICE implements @realize, it d= oes + * not need to implement @init and therefore does not need to store and = call + * #DeviceClass' default @realize callback. + */ typedef struct DeviceClass { + /*< private >*/ ObjectClass parent_class; + /*< public >*/ =20 const char *fw_name; const char *desc; @@ -33,12 +81,14 @@ typedef struct DeviceClass { =20 /* callbacks */ void (*reset)(DeviceState *dev); + DeviceRealize realize; + DeviceUnrealize unrealize; =20 /* device state */ const struct VMStateDescription *vmsd; =20 /* Private to qdev / bus. */ - qdev_initfn init; + qdev_initfn init; /* TODO remove, once users are converted to realiz= e */ qdev_event unplug; qdev_event exit; const char *bus_type; diff --git a/hw/qdev.c b/hw/qdev.c index 07eef5c..13697c8 100644 --- a/hw/qdev.c +++ b/hw/qdev.c @@ -148,37 +148,30 @@ DeviceState *qdev_try_create(BusState *bus, const c= har *type) Return 0 on success. */ int qdev_init(DeviceState *dev) { - DeviceClass *dc =3D DEVICE_GET_CLASS(dev); - int rc; + Error *local_err =3D NULL; =20 assert(!dev->realized); =20 - rc =3D dc->init(dev); - if (rc < 0) { + object_property_set_bool(OBJECT(dev), true, "realized", &local_err); + if (local_err !=3D NULL) { + error_free(local_err); qdev_free(dev); - return rc; + return -1; } + return 0; +} =20 - if (!OBJECT(dev)->parent) { - static int unattached_count =3D 0; - gchar *name =3D g_strdup_printf("device[%d]", unattached_count++= ); - - object_property_add_child(container_get(qdev_get_machine(), - "/unattached"), - name, OBJECT(dev), NULL); - g_free(name); - } +static void device_realize(DeviceState *dev, Error **err) +{ + DeviceClass *dc =3D DEVICE_GET_CLASS(dev); =20 - if (qdev_get_vmsd(dev)) { - vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), dev, - dev->instance_id_alias, - dev->alias_required_for_version); - } - dev->realized =3D true; - if (dev->hotplugged) { - device_reset(dev); + if (dc->init) { + int rc =3D dc->init(dev); + if (rc < 0) { + error_setg(err, "Device initialization failed."); + return; + } } - return 0; } =20 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id, @@ -636,6 +629,55 @@ void qdev_property_add_static(DeviceState *dev, Prop= erty *prop, assert_no_error(local_err); } =20 +static bool device_get_realized(Object *obj, Error **err) +{ + DeviceState *dev =3D DEVICE(obj); + return dev->realized; +} + +static void device_set_realized(Object *obj, bool value, Error **err) +{ + DeviceState *dev =3D DEVICE(obj); + DeviceClass *dc =3D DEVICE_GET_CLASS(dev); + Error *local_err =3D NULL; + + if (value && !dev->realized) { + if (dc->realize) { + dc->realize(dev, &local_err); + } + + if (!obj->parent && local_err =3D=3D NULL) { + static int unattached_count; + gchar *name =3D g_strdup_printf("device[%d]", unattached_cou= nt++); + + object_property_add_child(container_get(qdev_get_machine(), + "/unattached"), + name, obj, &local_err); + g_free(name); + } + + if (qdev_get_vmsd(dev) && local_err =3D=3D NULL) { + vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev), = dev, + dev->instance_id_alias, + dev->alias_required_for_versi= on); + } + if (dev->hotplugged && local_err =3D=3D NULL) { + device_reset(dev); + } + } else if (!value && dev->realized) { + if (dc->unrealize) { + dc->unrealize(dev, &local_err); + } + } + + if (local_err !=3D NULL) { + error_propagate(err, local_err); + return; + } + + dev->realized =3D value; +} + static void device_initfn(Object *obj) { DeviceState *dev =3D DEVICE(obj); @@ -650,6 +692,9 @@ static void device_initfn(Object *obj) dev->instance_id_alias =3D -1; dev->realized =3D false; =20 + object_property_add_bool(obj, "realized", + device_get_realized, device_set_realized, N= ULL); + class =3D object_get_class(OBJECT(dev)); do { for (prop =3D DEVICE_CLASS(class)->props; prop && prop->name; pr= op++) { @@ -709,7 +754,10 @@ static void device_unparent(Object *obj) =20 static void device_class_init(ObjectClass *class, void *data) { + DeviceClass *dc =3D DEVICE_CLASS(class); + class->unparent =3D device_unparent; + dc->realize =3D device_realize; } =20 void device_reset(DeviceState *dev) @@ -732,7 +780,7 @@ Object *qdev_get_machine(void) return dev; } =20 -static TypeInfo device_type_info =3D { +static const TypeInfo device_type_info =3D { .name =3D TYPE_DEVICE, .parent =3D TYPE_OBJECT, .instance_size =3D sizeof(DeviceState), --=20 1.7.10.4