From patchwork Wed Jul 20 17:04:21 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Przywara X-Patchwork-Id: 9240013 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id BCC6860867 for ; Wed, 20 Jul 2016 17:04:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A601027CE5 for ; Wed, 20 Jul 2016 17:04:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9755727D9B; Wed, 20 Jul 2016 17:04:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7C39B27D29 for ; Wed, 20 Jul 2016 17:04:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754521AbcGTRD6 (ORCPT ); Wed, 20 Jul 2016 13:03:58 -0400 Received: from foss.arm.com ([217.140.101.70]:43644 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753948AbcGTRDw (ORCPT ); Wed, 20 Jul 2016 13:03:52 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 03D0F3A1; Wed, 20 Jul 2016 10:05:02 -0700 (PDT) Received: from e104803-lin.lan (unknown [10.1.207.46]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 50DDD3F387; Wed, 20 Jul 2016 10:03:50 -0700 (PDT) From: Andre Przywara To: Will Deacon , Marc Zyngier Cc: kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH v7 01/15] FDT: introduce global phandle allocation Date: Wed, 20 Jul 2016 18:04:21 +0100 Message-Id: <20160720170435.28090-2-andre.przywara@arm.com> X-Mailer: git-send-email 2.9.0 In-Reply-To: <20160720170435.28090-1-andre.przywara@arm.com> References: <20160720170435.28090-1-andre.przywara@arm.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Allocating an FDT phandle (a unique identifier) using a static variable in a static inline function defined in a header file works only if all users are in the same source file. So trying to allocate a handle from two different compilation units fails. Introduce global phandle allocation and reference code to properly allocate unique phandles. Signed-off-by: Andre Przywara --- Makefile | 1 + arm/fdt.c | 2 +- arm/gic.c | 2 +- include/kvm/fdt.h | 10 +++++----- kvm-fdt.c | 26 ++++++++++++++++++++++++++ 5 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 kvm-fdt.c diff --git a/Makefile b/Makefile index 1f0196f..e4a4002 100644 --- a/Makefile +++ b/Makefile @@ -98,6 +98,7 @@ OBJS += kvm-ipc.o OBJS += builtin-sandbox.o OBJS += virtio/mmio.o OBJS += hw/i8042.o +OBJS += kvm-fdt.o # Translate uname -m into ARCH string ARCH ?= $(shell uname -m | sed -e s/i.86/i386/ -e s/ppc.*/powerpc/ \ diff --git a/arm/fdt.c b/arm/fdt.c index 381d48f..8bcfffb 100644 --- a/arm/fdt.c +++ b/arm/fdt.c @@ -114,7 +114,7 @@ static int setup_fdt(struct kvm *kvm) { struct device_header *dev_hdr; u8 staging_fdt[FDT_MAX_SIZE]; - u32 gic_phandle = fdt__alloc_phandle(); + u32 gic_phandle = fdt__get_phandle(PHANDLE_GIC); u64 mem_reg_prop[] = { cpu_to_fdt64(kvm->arch.memory_guest_start), cpu_to_fdt64(kvm->ram_size), diff --git a/arm/gic.c b/arm/gic.c index d6d6dd0..b60437e 100644 --- a/arm/gic.c +++ b/arm/gic.c @@ -222,7 +222,7 @@ void gic__generate_fdt_nodes(void *fdt, u32 phandle, enum irqchip_type type) _FDT(fdt_property_cell(fdt, "#interrupt-cells", GIC_FDT_IRQ_NUM_CELLS)); _FDT(fdt_property(fdt, "interrupt-controller", NULL, 0)); _FDT(fdt_property(fdt, "reg", reg_prop, sizeof(reg_prop))); - _FDT(fdt_property_cell(fdt, "phandle", phandle)); + _FDT(fdt_property_cell(fdt, "phandle", fdt__get_phandle(PHANDLE_GIC))); _FDT(fdt_end_node(fdt)); } diff --git a/include/kvm/fdt.h b/include/kvm/fdt.h index 53d85a4..cd2bb72 100644 --- a/include/kvm/fdt.h +++ b/include/kvm/fdt.h @@ -8,6 +8,10 @@ #include #define FDT_MAX_SIZE 0x10000 +#define FDT_INVALID_PHANDLE 0 +#define FDT_IS_VALID_PHANDLE(phandle) ((phandle) != FDT_INVALID_PHANDLE) + +enum phandles {PHANDLE_GIC, PHANDLES_MAX}; /* Those definitions are generic FDT values for specifying IRQ * types and are used in the Linux kernel internally as well as in @@ -33,10 +37,6 @@ enum irq_type { } \ } while (0) -static inline u32 fdt__alloc_phandle(void) -{ - static u32 phandle = 0; - return ++phandle; -} +u32 fdt__get_phandle(enum phandles phandle); #endif /* KVM__FDT_H */ diff --git a/kvm-fdt.c b/kvm-fdt.c new file mode 100644 index 0000000..d05f3fe --- /dev/null +++ b/kvm-fdt.c @@ -0,0 +1,26 @@ +/* + * Commonly used FDT functions. + */ + +#include +#include "kvm/fdt.h" +#include "kvm/util.h" + +u32 phandles[PHANDLES_MAX] = {}; +u32 next_phandle = 1; + +u32 fdt__get_phandle(enum phandles phandle) +{ + u32 ret; + + if (phandle >= PHANDLES_MAX) + return FDT_INVALID_PHANDLE; + + ret = phandles[phandle]; + if (ret == FDT_INVALID_PHANDLE) { + ret = next_phandle++; + phandles[phandle] = ret; + } + + return ret; +}