From nobody Fri Oct 24 09:56:46 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1518789150212843.7237738337392; Fri, 16 Feb 2018 05:52:30 -0800 (PST) Received: from localhost ([::1]:50376 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1emgR7-0007Vo-WB for importer@patchew.org; Fri, 16 Feb 2018 08:52:26 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59430) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1emgKG-0001BU-SB for qemu-devel@nongnu.org; Fri, 16 Feb 2018 08:45:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1emgKF-0005Nu-SE for qemu-devel@nongnu.org; Fri, 16 Feb 2018 08:45:20 -0500 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:46464) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1emgKF-0005NW-Hn for qemu-devel@nongnu.org; Fri, 16 Feb 2018 08:45:19 -0500 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1emgKD-0002xn-Il; Fri, 16 Feb 2018 13:45:17 +0000 From: Peter Maydell To: qemu-devel@nongnu.org Date: Fri, 16 Feb 2018 13:45:15 +0000 Message-Id: <20180216134516.6269-2-peter.maydell@linaro.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180216134516.6269-1-peter.maydell@linaro.org> References: <20180216134516.6269-1-peter.maydell@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:8b0:1d0::2 Subject: [Qemu-devel] [PATCH 1/2] hw/sysbus.h: New sysbus_init_child() helper function X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Stefan Hajnoczi , patches@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" If you're using the increasingly-common QOM style of having container devices create their child objects in-place, you end up with a lot of boilerplate in the container's init function: object_initialize() to init the child object_property_add_child() to make the child a child of the parent qdev_set_parent_bus() to put the child on the sysbus default bus If you forget the second of these then things sort of work but trying to add a child to the child will segfault; if you forget the third then the device won't get reset. Provide a helper function sysbus_init_child() which does all these things for you, reducing the boilerplate and making it harder to get wrong. Code that used to look like this: object_initialize(&s->ic, sizeof(s->ic), TYPE_BCM2835_IC); object_property_add_child(obj, "ic", OBJECT(&s->ic), NULL); qdev_set_parent_bus(DEVICE(&s->ic), sysbus_get_default()); can now look like this: sysbus_init_child(obj, "ic", &s->ic, sizeof(s->ic), TYPE_BCM2835_IC); Signed-off-by: Peter Maydell --- include/hw/sysbus.h | 12 ++++++++++++ hw/core/sysbus.c | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/hw/sysbus.h b/include/hw/sysbus.h index e88bb6dae0..6095e24e86 100644 --- a/include/hw/sysbus.h +++ b/include/hw/sysbus.h @@ -118,4 +118,16 @@ static inline DeviceState *sysbus_try_create_simple(co= nst char *name, return sysbus_try_create_varargs(name, addr, irq, NULL); } =20 +/** + * sysbus_init_child: in-place initialize and parent a SysBus device + * @parent: object to parent the device to + * @childname: name to use as the child property name + * @child: child object + * @childsize: size of the storage for the object + * @childtype: type name of the child + */ +void sysbus_init_child(Object *parent, const char *childname, + void *child, size_t childsize, + const char *childtype); + #endif /* HW_SYSBUS_H */ diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c index 5d0887f499..acfa52dc68 100644 --- a/hw/core/sysbus.c +++ b/hw/core/sysbus.c @@ -21,6 +21,7 @@ #include "hw/sysbus.h" #include "monitor/monitor.h" #include "exec/address-spaces.h" +#include "qapi/error.h" =20 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent); static char *sysbus_get_fw_dev_path(DeviceState *dev); @@ -372,6 +373,19 @@ BusState *sysbus_get_default(void) return main_system_bus; } =20 +void sysbus_init_child(Object *parent, const char *childname, + void *child, size_t childsize, + const char *childtype) +{ + object_initialize(child, childsize, childtype); + /* error_abort is fine here because this can only fail for + * programming-error reasons: child already parented, or + * parent already has a child with the given name. + */ + object_property_add_child(parent, childname, OBJECT(child), &error_abo= rt); + qdev_set_parent_bus(DEVICE(child), sysbus_get_default()); +} + static void sysbus_register_types(void) { type_register_static(&system_bus_info); --=20 2.16.1 From nobody Fri Oct 24 09:56:46 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1518790003495364.08181022163524; Fri, 16 Feb 2018 06:06:43 -0800 (PST) Received: from localhost ([::1]:51923 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1emgeu-0002gZ-LM for importer@patchew.org; Fri, 16 Feb 2018 09:06:40 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34965) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1emgYH-0006Ca-G5 for qemu-devel@nongnu.org; Fri, 16 Feb 2018 08:59:50 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1emgYG-00007U-Ea for qemu-devel@nongnu.org; Fri, 16 Feb 2018 08:59:49 -0500 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:46480) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1emgYG-0008PP-5n for qemu-devel@nongnu.org; Fri, 16 Feb 2018 08:59:48 -0500 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1emgKE-0002y0-AH; Fri, 16 Feb 2018 13:45:18 +0000 From: Peter Maydell To: qemu-devel@nongnu.org Date: Fri, 16 Feb 2018 13:45:16 +0000 Message-Id: <20180216134516.6269-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180216134516.6269-1-peter.maydell@linaro.org> References: <20180216134516.6269-1-peter.maydell@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:8b0:1d0::2 Subject: [Qemu-devel] [PATCH 2/2] hw/arm/bcm2835_peripherals: Use sysbus_init_child() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Stefan Hajnoczi , patches@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Use the new sysbus_init_child() function rather than opencoding the init-and-parent operations. This commit was created with coccinelle: spatch --in-place -sp_file sysbus-init-child.spatch --keep-comments hw/arm/= bcm2835_peripherals.c and the following spatch file: @@ expression CHILD, CHILDTYPE, POBJ, CHILDNAME, ERREXPR; @@ - object_initialize(&CHILD, sizeof(CHILD), CHILDTYPE); - object_property_add_child(POBJ, CHILDNAME, OBJECT(&CHILD), ERREXPR); - qdev_set_parent_bus(DEVICE(&CHILD), sysbus_get_default()); + sysbus_init_child(POBJ, CHILDNAME, &CHILD, sizeof(CHILD), CHILDTYPE); Signed-off-by: Peter Maydell --- hw/arm/bcm2835_peripherals.c | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/hw/arm/bcm2835_peripherals.c b/hw/arm/bcm2835_peripherals.c index 13b63970d7..7968d611ef 100644 --- a/hw/arm/bcm2835_peripherals.c +++ b/hw/arm/bcm2835_peripherals.c @@ -41,9 +41,7 @@ static void bcm2835_peripherals_init(Object *obj) MBOX_CHAN_COUNT << MBOX_AS_CHAN_SHIFT); =20 /* Interrupt Controller */ - object_initialize(&s->ic, sizeof(s->ic), TYPE_BCM2835_IC); - object_property_add_child(obj, "ic", OBJECT(&s->ic), NULL); - qdev_set_parent_bus(DEVICE(&s->ic), sysbus_get_default()); + sysbus_init_child(obj, "ic", &s->ic, sizeof(s->ic), TYPE_BCM2835_IC); =20 /* UART0 */ s->uart0 =3D SYS_BUS_DEVICE(object_new("pl011")); @@ -51,14 +49,11 @@ static void bcm2835_peripherals_init(Object *obj) qdev_set_parent_bus(DEVICE(s->uart0), sysbus_get_default()); =20 /* AUX / UART1 */ - object_initialize(&s->aux, sizeof(s->aux), TYPE_BCM2835_AUX); - object_property_add_child(obj, "aux", OBJECT(&s->aux), NULL); - qdev_set_parent_bus(DEVICE(&s->aux), sysbus_get_default()); + sysbus_init_child(obj, "aux", &s->aux, sizeof(s->aux), TYPE_BCM2835_AU= X); =20 /* Mailboxes */ - object_initialize(&s->mboxes, sizeof(s->mboxes), TYPE_BCM2835_MBOX); - object_property_add_child(obj, "mbox", OBJECT(&s->mboxes), NULL); - qdev_set_parent_bus(DEVICE(&s->mboxes), sysbus_get_default()); + sysbus_init_child(obj, "mbox", &s->mboxes, sizeof(s->mboxes), + TYPE_BCM2835_MBOX); =20 object_property_add_const_link(OBJECT(&s->mboxes), "mbox-mr", OBJECT(&s->mbox_mr), &error_abort); @@ -86,32 +81,25 @@ static void bcm2835_peripherals_init(Object *obj) OBJECT(&s->gpu_bus_mr), &error_abort); =20 /* Random Number Generator */ - object_initialize(&s->rng, sizeof(s->rng), TYPE_BCM2835_RNG); - object_property_add_child(obj, "rng", OBJECT(&s->rng), NULL); - qdev_set_parent_bus(DEVICE(&s->rng), sysbus_get_default()); + sysbus_init_child(obj, "rng", &s->rng, sizeof(s->rng), TYPE_BCM2835_RN= G); =20 /* Extended Mass Media Controller */ - object_initialize(&s->sdhci, sizeof(s->sdhci), TYPE_SYSBUS_SDHCI); - object_property_add_child(obj, "sdhci", OBJECT(&s->sdhci), NULL); - qdev_set_parent_bus(DEVICE(&s->sdhci), sysbus_get_default()); + sysbus_init_child(obj, "sdhci", &s->sdhci, sizeof(s->sdhci), + TYPE_SYSBUS_SDHCI); =20 /* SDHOST */ - object_initialize(&s->sdhost, sizeof(s->sdhost), TYPE_BCM2835_SDHOST); - object_property_add_child(obj, "sdhost", OBJECT(&s->sdhost), NULL); - qdev_set_parent_bus(DEVICE(&s->sdhost), sysbus_get_default()); + sysbus_init_child(obj, "sdhost", &s->sdhost, sizeof(s->sdhost), + TYPE_BCM2835_SDHOST); =20 /* DMA Channels */ - object_initialize(&s->dma, sizeof(s->dma), TYPE_BCM2835_DMA); - object_property_add_child(obj, "dma", OBJECT(&s->dma), NULL); - qdev_set_parent_bus(DEVICE(&s->dma), sysbus_get_default()); + sysbus_init_child(obj, "dma", &s->dma, sizeof(s->dma), TYPE_BCM2835_DM= A); =20 object_property_add_const_link(OBJECT(&s->dma), "dma-mr", OBJECT(&s->gpu_bus_mr), &error_abort); =20 /* GPIO */ - object_initialize(&s->gpio, sizeof(s->gpio), TYPE_BCM2835_GPIO); - object_property_add_child(obj, "gpio", OBJECT(&s->gpio), NULL); - qdev_set_parent_bus(DEVICE(&s->gpio), sysbus_get_default()); + sysbus_init_child(obj, "gpio", &s->gpio, sizeof(s->gpio), + TYPE_BCM2835_GPIO); =20 object_property_add_const_link(OBJECT(&s->gpio), "sdbus-sdhci", OBJECT(&s->sdhci.sdbus), &error_abort); --=20 2.16.1