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: 151168 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp6593667ljp; Wed, 14 Nov 2018 21:52:41 -0800 (PST) X-Google-Smtp-Source: AJdET5eHSvUPo7c7VbcRz2VThMYsC5XbZzlHvQacD8s+cHI7+JgiJVuWBHhfwQeoBRyT43GlhL63 X-Received: by 2002:a65:5a8e:: with SMTP id c14mr4531421pgt.137.1542261161002; Wed, 14 Nov 2018 21:52:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542261160; cv=none; d=google.com; s=arc-20160816; b=v9hMCSdHk31Ln0ktXE9L9fUjMviQNBi5QBFzTj4grI5THZIGg3O1mmbfJEiKEvI6sO 1m1qJgRnGwCy6XjFA9462P56mGg1K8HlDCMCdxB1hWSVODBw+K5SfeNBaUz+caojhfQx afMGySbbe2EEaKWZZuW3z5UaaY+C7AlYTReKpnOfovAePU3/orT5I3OTvNCgz2IA0lB4 N9lXVrEUn/AqZ0XZCdD2V01Va1p+1puJihJOOosO1wN3rPhX0VkYy0sRhCycLYUB7mOK /kjV8l3hTzHfYTgF7oESO3RUMc7RJt2vhKt4NXeV7pCfXeve2bBovDWn4xW1Yq8pk92h K46g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=mrPWZ4GdwOXyyhtosApIY//VWuTOROBlwQRdZLFfLfw=; b=aZO/kU9lUawuZS4RdiZtQuuhzYzz0gk/Xoklrmk8EymqW6Cxw5jQHQjX0Wf0TeSI7F rpcHNBRWwHs41sByxNWREpjT5YqlTMYu8sM4pTxWF8iGPLJBr/50YiXGnXI8alOTxDRb qtUqJLTmRZ233LFqV4/UM+R4TSTdAJFzTbuFs9A6WHWEC680aVBGOD9SdsRYwG2BxZXc CuPWWOdupqy1oiMpeaVH2iAF6QLMYJSi7QnwWn3cMkkT8J2AMrbj3wroT9yRiJgZcnbd /pfTZ3Y3DgElPYj6mzfOJZ4R6nZDRwlViEPSETAk61LO8AYrvFqEYd3mazw0xN35sGRX Kukw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HiV+WPPx; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g29-v6si23340163pgb.508.2018.11.14.21.52.40; Wed, 14 Nov 2018 21:52:40 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HiV+WPPx; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728687AbeKOP7E (ORCPT + 6 others); Thu, 15 Nov 2018 10:59:04 -0500 Received: from mail-yw1-f66.google.com ([209.85.161.66]:36799 "EHLO mail-yw1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727045AbeKOP7E (ORCPT ); Thu, 15 Nov 2018 10:59:04 -0500 Received: by mail-yw1-f66.google.com with SMTP id h21-v6so8295631ywa.3 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=C/PmfbQcyL9V7Ya4wwP6Iu3ItM3g0mDLC/SgO82tBdoNUaKasE/q7WkSS/DGVKvhl6 Hbzq/kHToCHUOYB7pVLH1HrhudXwE/gxBUTS66lneRkA8UTCqP8/SFUYdn4ctCJcjCEH glNCZl7jHmsZD77Im+Sx6PJOzO4tKXRVUUAOvRfKKA+i+A+/RubKxHn+bTQaqujQ90jL vDjAkgJOANlxD9g9cMMgj2htDPZU4Yets5N9L5Auxd/HOFlgV//9r8QOQBLDuVDc8BGm 9quKZvax8H/DLxe6otYjDiIG6Z21t3jACsN5W1s6SLt7KWuj7Xgyj6zL1+/BLNHhRDH8 JW2g== X-Gm-Message-State: AGRZ1gKkMoR7S3FbE1vevVUjOTkT4J+fqjgAt1C++dN34w4+Raq5M8OY 2ynulS49A9YB5MzcpwD5o3x7Ig== 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 Cc: prudo@linux.ibm.com, ard.biesheuvel@linaro.org, james.morse@arm.com, bhsharma@redhat.com, kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro , Rob Herring , Frank Rowand , devicetree@vger.kernel.org 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 Sender: devicetree-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org 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 -- 2.19.0 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); +}