From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrew Jones Subject: [kvm-unit-tests PATCH 1/2] devicetree: search up tree in dt_get_nr_cells Date: Tue, 10 May 2016 19:32:05 +0200 Message-ID: <1462901526-11013-2-git-send-email-drjones@redhat.com> References: <1462901526-11013-1-git-send-email-drjones@redhat.com> Cc: lvivier@redhat.com, thuth@redhat.com, pbonzini@redhat.com To: kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:60558 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751033AbcEJRcN (ORCPT ); Tue, 10 May 2016 13:32:13 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0F3AFC097268 for ; Tue, 10 May 2016 17:32:13 +0000 (UTC) In-Reply-To: <1462901526-11013-1-git-send-email-drjones@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: Search up the tree until we find #address-cells/#size-cells. Also only assign outputs if both address and size are found. Signed-off-by: Andrew Jones --- lib/devicetree.c | 17 +++++++++++++---- lib/devicetree.h | 3 ++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/devicetree.c b/lib/devicetree.c index a5c7f7c69ddfd..d3751e2b7e7f9 100644 --- a/lib/devicetree.c +++ b/lib/devicetree.c @@ -24,21 +24,30 @@ int dt_get_nr_cells(int fdtnode, u32 *nr_address_cells, u32 *nr_size_cells) { const struct fdt_property *prop; u32 *nr_cells; - int len; + int len, nac, nsc; + + while (1) { + prop = fdt_get_property(fdt, fdtnode, "#address-cells", &len); + if (prop != NULL || len != -FDT_ERR_NOTFOUND) + break; + fdtnode = fdt_parent_offset(fdt, fdtnode); + } - prop = fdt_get_property(fdt, fdtnode, "#address-cells", &len); if (prop == NULL) return len; nr_cells = (u32 *)prop->data; - *nr_address_cells = fdt32_to_cpu(*nr_cells); + nac = fdt32_to_cpu(*nr_cells); prop = fdt_get_property(fdt, fdtnode, "#size-cells", &len); if (prop == NULL) return len; nr_cells = (u32 *)prop->data; - *nr_size_cells = fdt32_to_cpu(*nr_cells); + nsc = fdt32_to_cpu(*nr_cells); + + *nr_address_cells = nac; + *nr_size_cells = nsc; return 0; } diff --git a/lib/devicetree.h b/lib/devicetree.h index c8c86eeae28b6..09f2509409615 100644 --- a/lib/devicetree.h +++ b/lib/devicetree.h @@ -168,7 +168,8 @@ extern int dt_pbus_get_base_compatible(const char *compatible, /* * dt_get_nr_cells sets @nr_address_cells and @nr_size_cells to the - * #address-cells and #size-cells properties of @fdtnode + * #address-cells and #size-cells properties of @fdtnode, potentially + * searching up the tree to find them in a parent node. * returns * - zero on success * - a negative FDT_ERR_* value on failure -- 2.4.11