From patchwork Tue Aug 20 07:19:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 1149840 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46CMcN5KM1z9sN4 for ; Tue, 20 Aug 2019 17:19:32 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=popple.id.au Received: from bilbo.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 46CMcN2yxzzDqfM for ; Tue, 20 Aug 2019 17:19:32 +1000 (AEST) X-Original-To: pdbg@lists.ozlabs.org Delivered-To: pdbg@lists.ozlabs.org Received: from ozlabs.org (bilbo.ozlabs.org [IPv6:2401:3900:2:1::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 46CMc33vxCzDqf6 for ; Tue, 20 Aug 2019 17:19:15 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=popple.id.au Received: from authenticated.ozlabs.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mail.ozlabs.org (Postfix) with ESMTPSA id 46CMc3246jz9sN4; Tue, 20 Aug 2019 17:19:15 +1000 (AEST) From: Alistair Popple To: pdbg@lists.ozlabs.org Date: Tue, 20 Aug 2019 17:19:05 +1000 Message-Id: <20190820071906.25950-4-alistair@popple.id.au> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190820071906.25950-1-alistair@popple.id.au> References: <20190820071906.25950-1-alistair@popple.id.au> MIME-Version: 1.0 Subject: [Pdbg] [PATCH 4/5] libpdbg: Rework dt_new_node() X-BeenThere: pdbg@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "mailing list for https://github.com/open-power/pdbg development" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: pdbg-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Pdbg" Simplify the control logic for dt_new_node() by splitting out the initialisation from the HW unit. Should be no functional change. Signed-off-by: Alistair Popple --- libpdbg/device.c | 89 +++++++++++++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 35 deletions(-) diff --git a/libpdbg/device.c b/libpdbg/device.c index 2fcb184..c4cbc44 100644 --- a/libpdbg/device.c +++ b/libpdbg/device.c @@ -64,60 +64,79 @@ static const char *take_name(const char *name) return name; } -static struct pdbg_target *dt_new_node(const char *name, const void *fdt, int node_offset) +/* Adds information representing an actual target */ +static struct pdbg_target *pdbg_target_new(const void *fdt, int node_offset) { + struct pdbg_target *target; + struct pdbg_target_class *target_class; const struct hw_unit_info *hw_info = NULL; const struct fdt_property *prop; - struct pdbg_target *node; - size_t size = sizeof(*node); - - if (fdt) { - prop = fdt_get_property(fdt, node_offset, "compatible", NULL); - if (prop) { - int i, prop_len = fdt32_to_cpu(prop->len); - - /* - * If I understand correctly, the property we have - * here can be a stringlist with a few compatible - * strings - */ - i = 0; - while (i < prop_len) { - hw_info = pdbg_hwunit_find_compatible(&prop->data[i]); - if (hw_info) { - size = hw_info->size; - break; - } - - i += strlen(&prop->data[i]) + 1; + size_t size; + + prop = fdt_get_property(fdt, node_offset, "compatible", NULL); + if (prop) { + int i, prop_len = fdt32_to_cpu(prop->len); + + /* + * If I understand correctly, the property we have + * here can be a stringlist with a few compatible + * strings + */ + i = 0; + while (i < prop_len) { + hw_info = pdbg_hwunit_find_compatible(&prop->data[i]); + if (hw_info) { + size = hw_info->size; + break; } + + i += strlen(&prop->data[i]) + 1; } } - node = calloc(1, size); - if (!node) { + if (!hw_info) + /* Couldn't find anything implementing this target */ + return NULL; + + target = calloc(1, size); + if (!target) { prerror("Failed to allocate node\n"); abort(); } - if (hw_info) { - struct pdbg_target_class *target_class; + /* hw_info->hw_unit points to a per-target struct type. This + * works because the first member in the per-target struct is + * guaranteed to be the struct pdbg_target (see the comment + * above DECLARE_HW_UNIT). */ + memcpy(target, hw_info->hw_unit, size); + target_class = get_target_class(target->class); + list_add_tail(&target_class->targets, &target->class_link); + + return target; +} - /* hw_info->hw_unit points to a per-target struct type. This - * works because the first member in the per-target struct is - * guaranteed to be the struct pdbg_target (see the comment - * above DECLARE_HW_UNIT). */ - memcpy(node, hw_info->hw_unit, size); - target_class = get_target_class(node->class); - list_add_tail(&target_class->targets, &node->class_link); +static struct pdbg_target *dt_new_node(const char *name, const void *fdt, int node_offset) +{ + struct pdbg_target *node = NULL; + size_t size = sizeof(*node); + + if (fdt) + node = pdbg_target_new(fdt, node_offset); + + if (!node) + node = calloc(1, size); + + if (!node) { + prerror("Failed to allocate node\n"); + abort(); } node->dn_name = take_name(name); node->parent = NULL; list_head_init(&node->properties); list_head_init(&node->children); - /* FIXME: locking? */ node->phandle = ++last_phandle; + return node; }