From nobody Fri Jun 19 09:49:30 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8860DC433F5 for ; Wed, 6 Apr 2022 01:30:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1456279AbiDFB3x (ORCPT ); Tue, 5 Apr 2022 21:29:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59112 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1384480AbiDEOSH (ORCPT ); Tue, 5 Apr 2022 10:18:07 -0400 Received: from smtp2.axis.com (smtp2.axis.com [195.60.68.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1689F6E7B9; Tue, 5 Apr 2022 06:04:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axis.com; q=dns/txt; s=axis-central1; t=1649163848; x=1680699848; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7cRqrzsYNIRfiAOotHa409y/Hl5ia/NeruEwkXktTvU=; b=lvFF5QW+7lsDkZ3D6AwCQwU1n259IjfBXJKmpbYXegYsg+2HyBzr4W+m qweFLZlhHJqKzzJrXjuFsr/HQ2HAb+12Fl8TCxkDlKKhuzgCMHZn08dXc iA4cBKmNbgwAmgnLdNpYPqTb0H/ih+rsxTJwNBI5iztYJvnYuxM49eDFP bXWQlDfsWBROAsE7T0w74WYcPtPUPXBx7otBu52Qeb14LcVUvGkmWEoj4 uiysNqzIb6GEjtCjqNz9/Yied1cVX/1oVvdiKJkvhR8IQEW9EB/CvYV9o h+rsmilshnu2Fier3gFN1oedzS+WvE1uk1fWhBTULCdd/fHMqs7bLZcbS Q==; From: Vincent Whitchurch To: , , , CC: , Vincent Whitchurch , , , , , , Subject: [PATCH v2 1/4] mtd: core: Check devicetree alias for index Date: Tue, 5 Apr 2022 15:03:47 +0200 Message-ID: <20220405130350.1640985-2-vincent.whitchurch@axis.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220405130350.1640985-1-vincent.whitchurch@axis.com> References: <20220405130350.1640985-1-vincent.whitchurch@axis.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Allow the MTD index to be specified via a devicetree alias, so that the number does not just depend on probe order. This is useful to allow pseudo-devices like phram to be optionally used on systems, without having this affect the numbering of the real hardware MTD devices. Signed-off-by: Vincent Whitchurch --- drivers/mtd/mtdcore.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c index 7731796024e0..9eb0680db312 100644 --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c @@ -557,9 +557,10 @@ static int mtd_nvmem_add(struct mtd_info *mtd) =20 int add_mtd_device(struct mtd_info *mtd) { + struct device_node *np =3D mtd_get_of_node(mtd); struct mtd_info *master =3D mtd_get_master(mtd); struct mtd_notifier *not; - int i, error; + int i, error, ofidx; =20 /* * May occur, for instance, on buggy drivers which call @@ -598,7 +599,13 @@ int add_mtd_device(struct mtd_info *mtd) =20 mutex_lock(&mtd_table_mutex); =20 - i =3D idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL); + ofidx =3D -1; + if (np) + ofidx =3D of_alias_get_id(np, "mtd"); + if (ofidx >=3D 0) + i =3D idr_alloc(&mtd_idr, mtd, ofidx, ofidx + 1, GFP_KERNEL); + else + i =3D idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL); if (i < 0) { error =3D i; goto fail_locked; --=20 2.34.1 From nobody Fri Jun 19 09:49:30 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 836B3C433F5 for ; Wed, 6 Apr 2022 01:10:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1840744AbiDFBLr (ORCPT ); Tue, 5 Apr 2022 21:11:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1384292AbiDEORz (ORCPT ); Tue, 5 Apr 2022 10:17:55 -0400 Received: from smtp2.axis.com (smtp2.axis.com [195.60.68.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BFEF52E09C; Tue, 5 Apr 2022 06:03:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axis.com; q=dns/txt; s=axis-central1; t=1649163839; x=1680699839; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DqLQ/tqybbZnxatnOYByhvJNRJCuF0z9AxMtXu1mnFM=; b=BN7xY1k3tnaA8FsLsMcHISuo5ayWMEKb1NRR/sF1Q1hb2t2nQWsiPvDI v8BBNuJ4vprWFxbGQSlnpY6q+VyUJs1lEEgqegfs2iZ7TZxbIYRHLyibN eReB0awLH423zCVP4uJKOjNbsAfFSFn5T0VemaMiMyzwIBD475ZRVkTbc 1KscjZaRmuaIVorI8WuVbPaqwKFLvH/g+6rsXMMD6Qgyr/RQ7QCK+TzlO TUDUTK6nXcABuxsdZhAwUf/KKdhswKx4FZynpO1GQaMsj1IvvHgxgqlm/ 0t2F8YOzNA8dEBVMLGnjzWstZ3WWeRzWOwlX5DPKtmFhOymMBB2O90mJ/ g==; From: Vincent Whitchurch To: , , , CC: , Vincent Whitchurch , , , , , , Subject: [PATCH v2 2/4] dt-bindings: reserved-memory: Add phram Date: Tue, 5 Apr 2022 15:03:48 +0200 Message-ID: <20220405130350.1640985-3-vincent.whitchurch@axis.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220405130350.1640985-1-vincent.whitchurch@axis.com> References: <20220405130350.1640985-1-vincent.whitchurch@axis.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add bindings to allow MTD/block devices to be created in reserved-memory regions using the "phram" driver. This allows things like partitioning to be specified via the existing devicetree bindings. Signed-off-by: Vincent Whitchurch --- Notes: v2: - Add note on what "phram" means. - Use /schemas/mtd/mtd.yaml instead of relative pathUse /schemas/mtd/mt= d.yaml instead of relative path. .../bindings/reserved-memory/phram.yaml | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Documentation/devicetree/bindings/reserved-memory/phram= .yaml diff --git a/Documentation/devicetree/bindings/reserved-memory/phram.yaml b= /Documentation/devicetree/bindings/reserved-memory/phram.yaml new file mode 100644 index 000000000000..318415b56afe --- /dev/null +++ b/Documentation/devicetree/bindings/reserved-memory/phram.yaml @@ -0,0 +1,47 @@ +# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/reserved-memory/phram.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: MTD/block device in RAM + +description: | + Use the reserved memory region as an MTD or block device. The "phram" n= ode + is named after the "MTD in PHysical RAM" driver which provides an + implementation of this functionality in Linux. + + If no-map is not set, cached mappings will be used for the memory region. + +maintainers: + - Vincent Whitchurch + +allOf: + - $ref: "reserved-memory.yaml" + - $ref: "/schemas/mtd/mtd.yaml" + +properties: + compatible: + const: phram + + reg: + description: region of memory that contains the MTD/block device + +required: + - compatible + - reg + +unevaluatedProperties: false + +examples: + - | + reserved-memory { + #address-cells =3D <1>; + #size-cells =3D <1>; + + phram: flash@12340000 { + compatible =3D "phram"; + label =3D "rootfs"; + reg =3D <0x12340000 0x00800000>; + }; + }; --=20 2.34.1 From nobody Fri Jun 19 09:49:30 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2B29C4332F for ; Wed, 6 Apr 2022 00:09:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1582328AbiDEXmm (ORCPT ); Tue, 5 Apr 2022 19:42:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1384483AbiDEOSH (ORCPT ); Tue, 5 Apr 2022 10:18:07 -0400 Received: from smtp2.axis.com (smtp2.axis.com [195.60.68.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18F2199EDD; Tue, 5 Apr 2022 06:04:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axis.com; q=dns/txt; s=axis-central1; t=1649163848; x=1680699848; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=SRDguBSs1Y1J0kKrsj8H5WlSWoje+dTygZTrpaj2/6I=; b=M04nl71coNVbG2fv/0z56zzyyJfVDcMraxWfrECBCe+tu+a+0QEHfgWf c3B/1qZZMSrHnk3j0YYnQJbkiwt1SuCpAjIPfuVYHLz3qydKMbCPO16zR r9ih6s8weXPOlAzQo/7rKjiyXXnWBODpbyzaydKrx9nJqSkGtC8vQYXRf LhXCjnAA1s1i3aDowW+PN/yYkgjvrXHDlihfg7RP5yTJoMdjQWaca0swD biGFcbimL+lVV8sLTpd+rflwCIyrbS098Rn999X+KpGAqL7mFOCCWOS5a JWbwqCs5yKBmkPqIBLx5cSu/86XUsEMipguMhPHK7iOA0dZnDRlk3w+Vj Q==; From: Vincent Whitchurch To: , , , CC: , Vincent Whitchurch , , , , , , Subject: [PATCH v2 3/4] mtd: phram: Allow probing via reserved-memory Date: Tue, 5 Apr 2022 15:03:49 +0200 Message-ID: <20220405130350.1640985-4-vincent.whitchurch@axis.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220405130350.1640985-1-vincent.whitchurch@axis.com> References: <20220405130350.1640985-1-vincent.whitchurch@axis.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Allow phram to be probed from the devicetree. It expects to be in a reserved-memory node as documented by the bindings. This allows things like partitioning to be specified via the devicetree. Signed-off-by: Vincent Whitchurch Reported-by: kernel test robot --- drivers/mtd/devices/phram.c | 67 ++++++++++++++++++++++++++++++++++--- drivers/of/platform.c | 1 + 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/drivers/mtd/devices/phram.c b/drivers/mtd/devices/phram.c index d503821a3e60..6dfe9401a3c5 100644 --- a/drivers/mtd/devices/phram.c +++ b/drivers/mtd/devices/phram.c @@ -27,6 +27,9 @@ #include #include #include +#include +#include +#include =20 struct phram_mtd_list { struct mtd_info mtd; @@ -89,8 +92,10 @@ static void unregister_devices(void) } } =20 -static int register_device(char *name, phys_addr_t start, size_t len, uint= 32_t erasesize) +static int register_device(struct platform_device *pdev, const char *name, + phys_addr_t start, size_t len, uint32_t erasesize) { + struct device_node *np =3D pdev ? pdev->dev.of_node : NULL; struct phram_mtd_list *new; int ret =3D -ENOMEM; =20 @@ -119,13 +124,19 @@ static int register_device(char *name, phys_addr_t st= art, size_t len, uint32_t e new->mtd.erasesize =3D erasesize; new->mtd.writesize =3D 1; =20 + mtd_set_of_node(&new->mtd, np); + ret =3D -EAGAIN; if (mtd_device_register(&new->mtd, NULL, 0)) { pr_err("Failed to register new device\n"); goto out2; } =20 - list_add_tail(&new->list, &phram_list); + if (pdev) + platform_set_drvdata(pdev, new); + else + list_add_tail(&new->list, &phram_list); + return 0; =20 out2: @@ -278,7 +289,7 @@ static int phram_setup(const char *val) goto error; } =20 - ret =3D register_device(name, start, len, (uint32_t)erasesize); + ret =3D register_device(NULL, name, start, len, (uint32_t)erasesize); if (ret) goto error; =20 @@ -325,10 +336,54 @@ static int phram_param_call(const char *val, const st= ruct kernel_param *kp) module_param_call(phram, phram_param_call, NULL, NULL, 0200); MODULE_PARM_DESC(phram, "Memory region to map. \"phram=3D,,[,]\""); =20 +#ifdef CONFIG_OF +static const struct of_device_id phram_of_match[] =3D { + { .compatible =3D "phram" }, + {} +}; +MODULE_DEVICE_TABLE(of, phram_of_match) +#endif + +static int phram_probe(struct platform_device *pdev) +{ + struct resource *res; + + res =3D platform_get_resource(pdev, IORESOURCE_MEM, 0); + if (!res) + return -ENOMEM; + + /* mtd_set_of_node() reads name from "label" */ + return register_device(pdev, NULL, res->start, resource_size(res), + PAGE_SIZE); +} + +static int phram_remove(struct platform_device *pdev) +{ + struct phram_mtd_list *phram =3D platform_get_drvdata(pdev); + + mtd_device_unregister(&phram->mtd); + iounmap(phram->mtd.priv); + kfree(phram); + + return 0; +} + +static struct platform_driver phram_driver =3D { + .probe =3D phram_probe, + .remove =3D phram_remove, + .driver =3D { + .name =3D "phram", + .of_match_table =3D of_match_ptr(phram_of_match), + }, +}; =20 static int __init init_phram(void) { - int ret =3D 0; + int ret; + + ret =3D platform_driver_register(&phram_driver); + if (ret) + return ret; =20 #ifndef MODULE if (phram_paramline[0]) @@ -336,12 +391,16 @@ static int __init init_phram(void) phram_init_called =3D 1; #endif =20 + if (ret) + platform_driver_unregister(&phram_driver); + return ret; } =20 static void __exit cleanup_phram(void) { unregister_devices(); + platform_driver_unregister(&phram_driver); } =20 module_init(init_phram); diff --git a/drivers/of/platform.c b/drivers/of/platform.c index a16b74f32aa9..55d62b82c650 100644 --- a/drivers/of/platform.c +++ b/drivers/of/platform.c @@ -509,6 +509,7 @@ EXPORT_SYMBOL_GPL(of_platform_default_populate); =20 #ifndef CONFIG_PPC static const struct of_device_id reserved_mem_matches[] =3D { + { .compatible =3D "phram" }, { .compatible =3D "qcom,rmtfs-mem" }, { .compatible =3D "qcom,cmd-db" }, { .compatible =3D "qcom,smem" }, --=20 2.34.1 From nobody Fri Jun 19 09:49:30 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B79DC433EF for ; Tue, 5 Apr 2022 22:10:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442493AbiDEWLE (ORCPT ); Tue, 5 Apr 2022 18:11:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1384447AbiDEOSG (ORCPT ); Tue, 5 Apr 2022 10:18:06 -0400 Received: from smtp2.axis.com (smtp2.axis.com [195.60.68.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 512982F384; Tue, 5 Apr 2022 06:03:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axis.com; q=dns/txt; s=axis-central1; t=1649163848; x=1680699848; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=S1wiQhSXl03OwHvB8odQNz0Ply4j8QcaKLui++yLhe4=; b=Cq874ZtK30JWxAIC75IihxHdfA8r0hwGN9M7iHQiKnx1HTfUdcaLx3gM zBm8+Ijs9SRRR0AlWPCdmCpZriM+2KViLd0VjfLsbob48wBoPGySVunwK /LzWWXxr9bQ8FqZeiXv9LeceOCGslmnQfCvf3/DNXg5QdzaN4b6O0CFip RXff2odRNbUZYfnfexSCyiLHvmU9VrlmFY9KRtJ6O8wptR8B8LTHjkwPU hndibKpY+siszjkzeeBXvNeYiO0Zd0s4Lq86f1r4SQVM4jZp2YmXhwJQD wjcMcHaa915t1bsfejyk8zK4UlB1dC6fEtl94/J3QOf7gLQOHDSDdlJGy g==; From: Vincent Whitchurch To: , , , CC: , Vincent Whitchurch , , , , , , Subject: [PATCH v2 4/4] mtd: phram: Allow cached mappings Date: Tue, 5 Apr 2022 15:03:50 +0200 Message-ID: <20220405130350.1640985-5-vincent.whitchurch@axis.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220405130350.1640985-1-vincent.whitchurch@axis.com> References: <20220405130350.1640985-1-vincent.whitchurch@axis.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Currently phram always uses ioremap(), but this is unnecessary when normal memory is used. If the reserved-memory node does not specify the no-map property, indicating it should be mapped as system RAM and ioremap() cannot be used on it, use a cached mapping using memremap(MEMREMAP_WB) instead. On one of my systems this improves read performance by ~70%. Signed-off-by: Vincent Whitchurch --- drivers/mtd/devices/phram.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/drivers/mtd/devices/phram.c b/drivers/mtd/devices/phram.c index 6dfe9401a3c5..ac2679f9031a 100644 --- a/drivers/mtd/devices/phram.c +++ b/drivers/mtd/devices/phram.c @@ -34,6 +34,7 @@ struct phram_mtd_list { struct mtd_info mtd; struct list_head list; + bool cached; }; =20 static LIST_HEAD(phram_list); @@ -96,6 +97,7 @@ static int register_device(struct platform_device *pdev, = const char *name, phys_addr_t start, size_t len, uint32_t erasesize) { struct device_node *np =3D pdev ? pdev->dev.of_node : NULL; + bool cached =3D np ? !of_property_read_bool(np, "no-map") : false; struct phram_mtd_list *new; int ret =3D -ENOMEM; =20 @@ -103,8 +105,13 @@ static int register_device(struct platform_device *pde= v, const char *name, if (!new) goto out0; =20 + new->cached =3D cached; + ret =3D -EIO; - new->mtd.priv =3D ioremap(start, len); + if (cached) + new->mtd.priv =3D memremap(start, len, MEMREMAP_WB); + else + new->mtd.priv =3D ioremap(start, len); if (!new->mtd.priv) { pr_err("ioremap failed\n"); goto out1; @@ -140,7 +147,7 @@ static int register_device(struct platform_device *pdev= , const char *name, return 0; =20 out2: - iounmap(new->mtd.priv); + cached ? memunmap(new->mtd.priv) : iounmap(new->mtd.priv); out1: kfree(new); out0: @@ -362,7 +369,7 @@ static int phram_remove(struct platform_device *pdev) struct phram_mtd_list *phram =3D platform_get_drvdata(pdev); =20 mtd_device_unregister(&phram->mtd); - iounmap(phram->mtd.priv); + phram->cached ? memunmap(phram->mtd.priv) : iounmap(phram->mtd.priv); kfree(phram); =20 return 0; --=20 2.34.1