From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:33629) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TcmKZ-0007Vz-BP for qemu-devel@nongnu.org; Sun, 25 Nov 2012 19:13:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TcmKU-0000Kq-TL for qemu-devel@nongnu.org; Sun, 25 Nov 2012 19:13:47 -0500 Received: from cantor2.suse.de ([195.135.220.15]:46166 helo=mx2.suse.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TcmKU-0000A7-GI for qemu-devel@nongnu.org; Sun, 25 Nov 2012 19:13:42 -0500 From: =?UTF-8?q?Andreas=20F=C3=A4rber?= Date: Mon, 26 Nov 2012 01:12:16 +0100 Message-Id: <1353888766-6951-5-git-send-email-afaerber@suse.de> In-Reply-To: <1353888766-6951-1-git-send-email-afaerber@suse.de> References: <1353888766-6951-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] [RFC 04/34] qdev: Prepare "realized" property List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Paolo Bonzini , =?UTF-8?q?Andreas=20F=C3=A4rber?= , anthony@codemonkey.ws Based on earlier patches by Paolo and me, introduce the QOM realizefn at device level only, as requested by Anthony. For now this just wraps the qdev initfn. Signed-off-by: Paolo Bonzini Signed-off-by: Andreas F=C3=A4rber Cc: Anthony Liguori --- hw/qdev-core.h | 4 +++ hw/qdev.c | 100 ++++++++++++++++++++++++++++++++++++++++++--------= ------ 2 Dateien ge=C3=A4ndert, 80 Zeilen hinzugef=C3=BCgt(+), 24 Zeilen entfer= nt(-) diff --git a/hw/qdev-core.h b/hw/qdev-core.h index f0d3a5e..580a811 100644 --- a/hw/qdev-core.h +++ b/hw/qdev-core.h @@ -29,6 +29,8 @@ 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 **err); +typedef void (*DeviceUnrealize)(DeviceState *dev, Error **err); =20 struct VMStateDescription; =20 @@ -47,6 +49,8 @@ typedef struct DeviceClass { const struct VMStateDescription *vmsd; =20 /* Private to qdev / bus. */ + DeviceRealize realize; + DeviceUnrealize unrealize; qdev_initfn init; qdev_event unplug; qdev_event exit; diff --git a/hw/qdev.c b/hw/qdev.c index bce6ad5..d7b6320 100644 --- a/hw/qdev.c +++ b/hw/qdev.c @@ -147,37 +147,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); object_delete(OBJECT(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, @@ -649,6 +642,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); @@ -663,6 +705,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++) { @@ -714,6 +759,12 @@ static void device_class_base_init(ObjectClass *clas= s, void *data) klass->props =3D NULL; } =20 +static void device_class_init(ObjectClass *oc, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(oc); + dc->realize =3D device_realize; +} + void device_reset(DeviceState *dev) { DeviceClass *klass =3D DEVICE_GET_CLASS(dev); @@ -734,13 +785,14 @@ 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), .instance_init =3D device_initfn, .instance_finalize =3D device_finalize, .class_base_init =3D device_class_base_init, + .class_init =3D device_class_init, .abstract =3D true, .class_size =3D sizeof(DeviceClass), }; --=20 1.7.10.4