From patchwork Thu Nov 15 05:52:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 10683539 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E95DF14D6 for ; Thu, 15 Nov 2018 05:54:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D5CE92C15A for ; Thu, 15 Nov 2018 05:54:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C9F3B2C248; Thu, 15 Nov 2018 05:54:45 +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=-3.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5AF602C15A for ; Thu, 15 Nov 2018 05:54:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=mUviTFlofVu6vcNhIyC3QCK+8+j2YUf04NLVz/3C1yM=; b=aLGLEeciD+6WTB FLUfRpl9qPa/HRa2pwiXOTtHc1d+Nd3I18Vm+nYdESwfPJ23CUsZi86vST3ThRn1WR0hLdLH0e/AC xHkeE8XLwjqGzSc3TVGM16S4b0rwA3bW0O/DKtHOOvL9aOMVt2PqyCkIgL1hjbYiT1Fn3SPVRBzI4 EZ3Z04KR3SEuU9z7xxwd+A+Y6WVJWQ6HoK7MTFEnz1eYZGDs5o3feJ/ypb0hUeN4CPM2JjkCe+hoo mVB+N0UP/LTqUExLkx3k6yx6L595CkD+aE2bzqGGn4SqgjTwlE1vByBKyPV2rCuSvRQHUlzbUG5Q9 RXNbGAzqJ7aUsnSL94JQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gNAbv-0007SJ-0i; Thu, 15 Nov 2018 05:54:39 +0000 Received: from mail-yw1-xc43.google.com ([2607:f8b0:4864:20::c43]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gNAa9-0005cd-1h for linux-arm-kernel@lists.infradead.org; Thu, 15 Nov 2018 05:52:51 +0000 Received: by mail-yw1-xc43.google.com with SMTP id k6-v6so8235803ywa.11 for ; Wed, 14 Nov 2018 21:52:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mrPWZ4GdwOXyyhtosApIY//VWuTOROBlwQRdZLFfLfw=; b=HiV+WPPxY/gSkZ6gC+DNCc9Y2vBWfNVgOPT3BWdpSNF+ZsRnS37STM1PxhFeWOxJk7 cFOvcEWifXIokZViFT+vbL4+egWfA/TxC+vIG0ft1dHcRhRNLy48J8+rUXOumtlY1ryE IJDckuWhvjAIuWei/B3sc/6i3+b0++1ZR75SQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mrPWZ4GdwOXyyhtosApIY//VWuTOROBlwQRdZLFfLfw=; b=brLAq48Lf8cG21znTmoC7IbswbbffKBJBqZaHSfOOqmTUk/TqUKO9//lOtFXKcCggL 0DOohDdpcrdTrD08a3ABHucCTStzH6mbMOu/BFOn3EovccGpQDpD8s0QBNWQ4iwkjqCc b4lK8K4csrqQl0T/T4lIvq7KzCRM2wlCiInQ5Rb77h4XfkiehpKLOL+byZwVQ2O1RNXg ZwGfyTSYFeG/LZC1zmxQthFT5/UNQIw/gDpxi4RLOIenrjHNGdVKAmxQzrQxFZ2Wv2+E 8DyBgHOfUGjEMNJP+kdUEMGFmLr2mFh2P+mlQ0c7SKGbVI3CpqZAW9BGuZyqXg+fRean /mxg== X-Gm-Message-State: AGRZ1gJCb+IDZLz2ALs5fDfJxty8yvGWf0yEdWhN4HfOYmoTfMqgWWKa 9N9S/ZDasrhLu5Oq+lzRLXcIfw== X-Google-Smtp-Source: AJdET5fkMvsm2DHLjgTVbC8xwZoGqVoWAD7HWYAuRL/sUOtxs2GKhrvUm8Sfip4SGiWaYCL36lo1+A== X-Received: by 2002:a81:2f89:: with SMTP id v131-v6mr4450717ywv.104.1542261157829; Wed, 14 Nov 2018 21:52:37 -0800 (PST) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id o131-v6sm6399857ywb.107.2018.11.14.21.52.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 14 Nov 2018 21:52:37 -0800 (PST) From: AKASHI Takahiro To: catalin.marinas@arm.com, will.deacon@arm.com, dhowells@redhat.com, vgoyal@redhat.com, herbert@gondor.apana.org.au, davem@davemloft.net, dyoung@redhat.com, bhe@redhat.com, arnd@arndb.de, schwidefsky@de.ibm.com, heiko.carstens@de.ibm.com Subject: [PATCH v16 06/16] lib: fdt: add a helper function for handling memory range property Date: Thu, 15 Nov 2018 14:52:45 +0900 Message-Id: <20181115055254.2812-7-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20181115055254.2812-1-takahiro.akashi@linaro.org> References: <20181115055254.2812-1-takahiro.akashi@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181114_215249_093097_E5DF5BF1 X-CRM114-Status: GOOD ( 16.85 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: devicetree@vger.kernel.org, ard.biesheuvel@linaro.org, bhsharma@redhat.com, kexec@lists.infradead.org, linux-kernel@vger.kernel.org, prudo@linux.ibm.com, AKASHI Takahiro , Rob Herring , james.morse@arm.com, Frank Rowand , linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Added function, fdt_setprop_reg(), will be used later to handle kexec-specific property in arm64's kexec_file implementation. It will possibly be merged into libfdt in the future. Signed-off-by: AKASHI Takahiro Cc: Rob Herring Cc: Frank Rowand Cc: devicetree@vger.kernel.org --- include/linux/libfdt.h | 26 ++++++++++++++++++++ lib/Makefile | 2 +- lib/fdt_addresses.c | 56 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 lib/fdt_addresses.c diff --git a/include/linux/libfdt.h b/include/linux/libfdt.h index 90ed4ebfa692..47c4dc9e135c 100644 --- a/include/linux/libfdt.h +++ b/include/linux/libfdt.h @@ -5,4 +5,30 @@ #include #include "../../scripts/dtc/libfdt/libfdt.h" +/** + * fdt_setprop_reg - add/set a memory region property + * @fdt: pointer to the device tree blob + * @nodeoffset: offset of the node to add a property at + * @name: name of property + * @addr: physical start address + * @size: size of region + * + * returns: + * 0, on success + * -FDT_ERR_BADLAYOUT, + * -FDT_ERR_BADMAGIC, + * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid + * #address-cells property + * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag + * -FDT_ERR_BADSTATE, + * -FDT_ERR_BADSTRUCTURE, + * -FDT_ERR_BADVERSION, + * -FDT_ERR_BADVALUE, addr or size doesn't fit to respective cells size + * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to + * contain a new property + * -FDT_ERR_TRUNCATED, standard meanings + */ +int fdt_setprop_reg(void *fdt, int nodeoffset, const char *name, + u64 addr, u64 size); + #endif /* _INCLUDE_LIBFDT_H_ */ diff --git a/lib/Makefile b/lib/Makefile index db06d1237898..2a96cb05e15d 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -205,7 +205,7 @@ KASAN_SANITIZE_stackdepot.o := n KCOV_INSTRUMENT_stackdepot.o := n libfdt_files = fdt.o fdt_ro.o fdt_wip.o fdt_rw.o fdt_sw.o fdt_strerror.o \ - fdt_empty_tree.o + fdt_empty_tree.o fdt_addresses.o $(foreach file, $(libfdt_files), \ $(eval CFLAGS_$(file) = -I$(src)/../scripts/dtc/libfdt)) lib-$(CONFIG_LIBFDT) += $(libfdt_files) diff --git a/lib/fdt_addresses.c b/lib/fdt_addresses.c new file mode 100644 index 000000000000..97ddd5a5cc10 --- /dev/null +++ b/lib/fdt_addresses.c @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include "../scripts/dtc/libfdt/fdt_addresses.c" + +/* + * helper functions for arm64 kexec + * Those functions may be merged into libfdt in the future. + */ + +/* This function assumes that cells is 1 or 2 */ +static void cpu64_to_fdt_cells(void *buf, u64 val64, int cells) +{ + __be32 val32; + + while (cells) { + val32 = cpu_to_fdt32(val64 >> (32 * (--cells))); + memcpy(buf, &val32, sizeof(val32)); + buf += sizeof(val32); + } +} + +int fdt_setprop_reg(void *fdt, int nodeoffset, const char *name, + u64 addr, u64 size) +{ + int addr_cells, size_cells; + char buf[sizeof(__be32) * 2 * 2]; + /* assume dt_root_[addr|size]_cells <= 2 */ + void *prop; + size_t buf_size; + + addr_cells = fdt_address_cells(fdt, 0); + if (addr_cells < 0) + return addr_cells; + size_cells = fdt_size_cells(fdt, 0); + if (size_cells < 0) + return size_cells; + + /* if *_cells >= 2, cells can hold 64-bit values anyway */ + if ((addr_cells == 1) && ((addr > U32_MAX) || + ((addr + size) > U32_MAX))) + return -FDT_ERR_BADVALUE; + + if ((size_cells == 1) && (size > U32_MAX)) + return -FDT_ERR_BADVALUE; + + buf_size = (addr_cells + size_cells) * sizeof(u32); + prop = buf; + + cpu64_to_fdt_cells(prop, addr, addr_cells); + prop += addr_cells * sizeof(u32); + + cpu64_to_fdt_cells(prop, size, size_cells); + + return fdt_setprop(fdt, nodeoffset, name, buf, buf_size); +}