2015-08-17 23:47:57

by Mathieu Olivari

[permalink] [raw]
Subject: [PATCH v3 0/3] qcom: Add SMEM MTD parser

QCOM platforms such as IPQ806x are using SMEM to store their flash
layout. This patch set adds the DT nodes required to instanciate SMEM
on IPQ806x and add an MTD parser using it.

This change is based on the SMEM driver posted here:
*https://lkml.org/lkml/2015/7/27/1125

v2:
*Release the SPI device reference after looking it up (put_device())
*Represent SMEM data as __le32 rather than u32
*Move new DT nodes in their proper respective location
*Address readability concerns in MTD parser

v3:
*Address Bjorn's comments

Mathieu Olivari (3):
ARM: qcom: add SFPB nodes to IPQ806x dts
ARM: qcom: add SMEM device node to IPQ806x dts
mtd: add SMEM parser for QCOM platforms

arch/arm/boot/dts/qcom-ipq8064.dtsi | 23 +++-
drivers/mtd/Kconfig | 7 ++
drivers/mtd/Makefile | 1 +
drivers/mtd/qcom_smem_part.c | 224 ++++++++++++++++++++++++++++++++++++
4 files changed, 254 insertions(+), 1 deletion(-)
create mode 100644 drivers/mtd/qcom_smem_part.c

--
2.1.4


2015-08-17 23:47:55

by Mathieu Olivari

[permalink] [raw]
Subject: [PATCH v3 1/3] ARM: qcom: add SFPB nodes to IPQ806x dts

Add one new node to the ipq806x.dtsi file to declare & register the
hardware spinlock devices. This mechanism is required to be used by
other drivers such as SMEM.

Signed-off-by: Mathieu Olivari <[email protected]>
---
arch/arm/boot/dts/qcom-ipq8064.dtsi | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/arch/arm/boot/dts/qcom-ipq8064.dtsi b/arch/arm/boot/dts/qcom-ipq8064.dtsi
index 9f727d8..8d366ae 100644
--- a/arch/arm/boot/dts/qcom-ipq8064.dtsi
+++ b/arch/arm/boot/dts/qcom-ipq8064.dtsi
@@ -329,5 +329,16 @@
#reset-cells = <1>;
};

+ sfpb_mutex_block: syscon@1200600 {
+ compatible = "syscon";
+ reg = <0x01200600 0x100>;
+ };
+ };
+
+ sfpb_mutex: sfpb-mutex {
+ compatible = "qcom,sfpb-mutex";
+ syscon = <&sfpb_mutex_block 4 4>;
+
+ #hwlock-cells = <1>;
};
};
--
2.1.4

2015-08-17 23:47:56

by Mathieu Olivari

[permalink] [raw]
Subject: [PATCH v3 2/3] ARM: qcom: add SMEM device node to IPQ806x dts

SMEM is used on IPQ806x to store various board related information such
as boot device and flash partition layout. We'll declare it as a device
so we can make use of it thanks to the new SMEM soc driver.

Signed-off-by: Mathieu Olivari <[email protected]>
---
arch/arm/boot/dts/qcom-ipq8064.dtsi | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/arch/arm/boot/dts/qcom-ipq8064.dtsi b/arch/arm/boot/dts/qcom-ipq8064.dtsi
index 8d366ae..85dbccf 100644
--- a/arch/arm/boot/dts/qcom-ipq8064.dtsi
+++ b/arch/arm/boot/dts/qcom-ipq8064.dtsi
@@ -55,7 +55,7 @@
no-map;
};

- smem@41000000 {
+ smem: smem@41000000 {
reg = <0x41000000 0x200000>;
no-map;
};
@@ -341,4 +341,10 @@

#hwlock-cells = <1>;
};
+
+ smem {
+ compatible = "qcom,smem";
+ memory-region = <&smem>;
+ hwlocks = <&sfpb_mutex 3>;
+ };
};
--
2.1.4

2015-08-17 23:48:39

by Mathieu Olivari

[permalink] [raw]
Subject: [PATCH v3 3/3] mtd: add SMEM parser for QCOM platforms

On QCOM platforms using MTD devices storage (such as IPQ806x), SMEM is
used to store partition layout. This new parser can now be used to read
SMEM and use it to register an MTD layout according to its content.

Signed-off-by: Mathieu Olivari <[email protected]>
---

Notes:
v2:
*Release the SPI device reference after looking it up (put_device())
*Represent SMEM data as __le32 rather than u32
*Move new DT nodes in their proper respective location
*Address readability concerns in MTD parser

v3:
*Redefine SMEM functions prototypes for better readability
*Duplicate MTD name instead of pointing to SMEM string location in case
mtd layer tries to free it
*Rework some of the comments

drivers/mtd/Kconfig | 7 ++
drivers/mtd/Makefile | 1 +
drivers/mtd/qcom_smem_part.c | 246 +++++++++++++++++++++++++++++++++++++++++++
3 files changed, 254 insertions(+)
create mode 100644 drivers/mtd/qcom_smem_part.c

diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig
index a03ad29..debc887 100644
--- a/drivers/mtd/Kconfig
+++ b/drivers/mtd/Kconfig
@@ -155,6 +155,13 @@ config MTD_BCM47XX_PARTS
This provides partitions parser for devices based on BCM47xx
boards.

+config MTD_QCOM_SMEM_PARTS
+ tristate "QCOM SMEM partitioning support"
+ depends on QCOM_SMEM
+ help
+ This provides partitions parser for QCOM devices using SMEM
+ such as IPQ806x.
+
comment "User Modules And Translation Layers"

#
diff --git a/drivers/mtd/Makefile b/drivers/mtd/Makefile
index 99bb9a1..b3c7de4 100644
--- a/drivers/mtd/Makefile
+++ b/drivers/mtd/Makefile
@@ -13,6 +13,7 @@ obj-$(CONFIG_MTD_AFS_PARTS) += afs.o
obj-$(CONFIG_MTD_AR7_PARTS) += ar7part.o
obj-$(CONFIG_MTD_BCM63XX_PARTS) += bcm63xxpart.o
obj-$(CONFIG_MTD_BCM47XX_PARTS) += bcm47xxpart.o
+obj-$(CONFIG_MTD_QCOM_SMEM_PARTS) += qcom_smem_part.o

# 'Users' - code which presents functionality to userspace.
obj-$(CONFIG_MTD_BLKDEVS) += mtd_blkdevs.o
diff --git a/drivers/mtd/qcom_smem_part.c b/drivers/mtd/qcom_smem_part.c
new file mode 100644
index 0000000..cb7ea9a
--- /dev/null
+++ b/drivers/mtd/qcom_smem_part.c
@@ -0,0 +1,246 @@
+/*
+ * Copyright (c) 2015, The Linux Foundation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 and
+ * only version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/kernel.h>
+#include <linux/device.h>
+#include <linux/slab.h>
+
+#include <linux/mtd/mtd.h>
+#include <linux/mtd/partitions.h>
+#include <linux/spi/spi.h>
+#include <linux/module.h>
+
+#include <linux/soc/qcom/smem.h>
+
+/* Processor/host identifier for the application processor */
+#define QCOM_SMEM_GLOBAL 0
+
+/* SMEM items index */
+#define SMEM_AARM_PARTITION_TABLE 9
+#define SMEM_BOOT_FLASH_TYPE 421
+#define SMEM_BOOT_FLASH_BLOCK_SIZE 424
+
+/* SMEM Flash types */
+#define SMEM_FLASH_NAND 2
+#define SMEM_FLASH_SPI 6
+
+#define SMEM_PART_NAME_SZ 16
+#define SMEM_PARTS_MAX 32
+
+struct smem_partition {
+ char name[SMEM_PART_NAME_SZ];
+ __le32 start;
+ __le32 size;
+ __le32 attr;
+};
+
+struct smem_partition_table {
+ u8 magic[8];
+ __le32 version;
+ __le32 len;
+ struct smem_partition parts[SMEM_PARTS_MAX];
+};
+
+/* SMEM Magic values in partition table */
+static const u8 SMEM_PTABLE_MAGIC[] = {
+ 0xaa, 0x73, 0xee, 0x55,
+ 0xdb, 0xbd, 0x5e, 0xe3,
+};
+
+static int qcom_smem_get_flash_blksz(void)
+{
+ int ret;
+ size_t size;
+ u64 *smem_blksz;
+
+ ret = qcom_smem_get(QCOM_SMEM_GLOBAL, SMEM_BOOT_FLASH_BLOCK_SIZE,
+ (void **) &smem_blksz, &size);
+
+ if (ret < 0) {
+ pr_err("Unable to read flash blksz from SMEM\n");
+ return -ENOENT;
+ }
+
+ if (size != sizeof(*smem_blksz)) {
+ pr_err("Invalid flash blksz size in SMEM\n");
+ return -EINVAL;
+ }
+
+ /* Sanity check block size value before casting it */
+ if (*smem_blksz > INT_MAX) {
+ pr_err("Invalid flash block size in SMEM\n");
+ return -EINVAL;
+ }
+
+ return (int) *smem_blksz;
+}
+
+static int qcom_smem_get_flash_type(void)
+{
+ int ret;
+ size_t size;
+ u64 *smem_flash_type;
+
+ ret = qcom_smem_get(QCOM_SMEM_GLOBAL, SMEM_BOOT_FLASH_TYPE,
+ (void **) &smem_flash_type, &size);
+
+ if (ret < 0) {
+ pr_err("Unable to read flash type from SMEM\n");
+ return -ENOENT;
+ }
+
+ if (size != sizeof(*smem_flash_type)) {
+ pr_err("Invalid flash type size in SMEM\n");
+ return -EINVAL;
+ }
+
+ /* Sanity check flash type value before casting it */
+ if (*smem_flash_type > INT_MAX) {
+ pr_err("Invalid flash type in SMEM\n");
+ return -EINVAL;
+ }
+
+ return (int) *smem_flash_type;
+}
+
+static struct smem_partition *qcom_smem_get_flash_partitions(size_t *count)
+{
+ struct smem_partition_table *smem_parts;
+ int ret;
+
+ ret = qcom_smem_get(QCOM_SMEM_GLOBAL, SMEM_AARM_PARTITION_TABLE,
+ (void **) &smem_parts, NULL);
+
+ if (ret < 0) {
+ pr_err("Unable to read partition table from SMEM\n");
+ return ERR_PTR(-ENOENT);
+ }
+
+ if (memcmp(SMEM_PTABLE_MAGIC, smem_parts->magic,
+ sizeof(SMEM_PTABLE_MAGIC))) {
+ pr_err("SMEM partition magic invalid\n");
+ return ERR_PTR(-EINVAL);
+ }
+
+ if (le32_to_cpu(smem_parts->version) != 3) {
+ pr_err("SMEM version unsupported\n");
+ return ERR_PTR(-EINVAL);
+ }
+
+ *count = le32_to_cpu(smem_parts->len);
+
+ return smem_parts->parts;
+}
+
+static int of_dev_node_match(struct device *dev, void *data)
+{
+ return dev->of_node == data;
+}
+
+static bool is_spi_device(struct device_node *np)
+{
+ struct device *dev;
+
+ dev = bus_find_device(&spi_bus_type, NULL, np, of_dev_node_match);
+ if (!dev)
+ return false;
+
+ put_device(dev);
+ return true;
+}
+
+static int parse_qcom_smem_partitions(struct mtd_info *master,
+ struct mtd_partition **pparts,
+ struct mtd_part_parser_data *data)
+{
+ struct smem_partition *smem_part;
+ int smem_flash_type, smem_blksz;
+ size_t nbparts = 0, len;
+ struct mtd_partition *mtd_parts;
+ struct device_node *of_node = data->of_node;
+
+ /* Only parse partitions on the specified memory type */
+ smem_flash_type = qcom_smem_get_flash_type();
+ if (smem_flash_type < 0)
+ return smem_flash_type;
+
+ if ((smem_flash_type == SMEM_FLASH_NAND && !mtd_type_is_nand(master))
+ || (smem_flash_type == SMEM_FLASH_SPI && !is_spi_device(of_node)))
+ return 0;
+
+ /* Make sure the block size in SMEM matches the block size in MTD */
+ smem_blksz = qcom_smem_get_flash_blksz();
+ if (smem_blksz < 0)
+ return smem_blksz;
+
+ if (smem_blksz != master->erasesize) {
+ pr_err("SMEM block size differs from MTD block size\n");
+ return -EINVAL;
+ }
+
+ /* Get partition pointer from SMEM */
+ smem_part = qcom_smem_get_flash_partitions(&nbparts);
+ if (IS_ERR_OR_NULL(smem_part))
+ return -EINVAL;
+
+ /* Allocate and populate the mtd structures */
+ mtd_parts = kcalloc(nbparts, sizeof(*mtd_parts), GFP_KERNEL);
+ if (!mtd_parts)
+ return -ENOMEM;
+
+ *pparts = mtd_parts;
+ len = nbparts;
+
+ while (len--) {
+ mtd_parts->name = kstrdup(smem_part->name, GFP_KERNEL);
+ mtd_parts->size = le32_to_cpu(smem_part->size) * smem_blksz;
+ mtd_parts->offset = le32_to_cpu(smem_part->start) * smem_blksz;
+
+ /*
+ * The last SMEM partition may have its size marked as
+ * something like 0xffffffff, which means "until the end of the
+ * flash device". In this case, truncate it.
+ */
+ if (mtd_parts->offset + mtd_parts->size > master->size)
+ mtd_parts->size = master->size - mtd_parts->offset;
+
+ smem_part++;
+ mtd_parts++;
+ }
+
+ return nbparts;
+}
+
+static struct mtd_part_parser qcom_smem_parser = {
+ .owner = THIS_MODULE,
+ .parse_fn = parse_qcom_smem_partitions,
+ .name = "qcom-smem",
+};
+
+static int __init qcom_smem_parser_init(void)
+{
+ register_mtd_parser(&qcom_smem_parser);
+ return 0;
+}
+
+static void __exit qcom_smem_parser_exit(void)
+{
+ deregister_mtd_parser(&qcom_smem_parser);
+}
+
+module_init(qcom_smem_parser_init);
+module_exit(qcom_smem_parser_exit);
+
+MODULE_LICENSE("GPL V2");
+MODULE_AUTHOR("Mathieu Olivari <[email protected]>");
+MODULE_DESCRIPTION("Parsing code for SMEM based partition tables");
--
2.1.4

2015-08-18 12:30:54

by Jonas Gorski

[permalink] [raw]
Subject: Re: [PATCH v3 0/3] qcom: Add SMEM MTD parser

Hi,

On Tue, Aug 18, 2015 at 1:47 AM, Mathieu Olivari <[email protected]> wrote:
> QCOM platforms such as IPQ806x are using SMEM to store their flash
> layout. This patch set adds the DT nodes required to instanciate SMEM
> on IPQ806x and add an MTD parser using it.
>
> This change is based on the SMEM driver posted here:
> *https://lkml.org/lkml/2015/7/27/1125

Nice work. After testing it on AP148 I see this:

[ 2.481507] 12 qcom-smem partitions found on MTD device qcom-nandc
[ 2.481540] Creating 12 MTD partitions on "qcom-nandc":
[ 2.486690] 0x000000000000-0x000000040000 : "0:SBL1"
[ 2.492842] 0x000000040000-0x000000180000 : "0:MIBIB"
[ 2.497857] 0x000000180000-0x0000002c0000 : "0:SBL2"
[ 2.502895] 0x0000002c0000-0x000000540000 : "0:SBL3"
[ 2.507828] 0x000000540000-0x000000660000 : "0:DDRCONFIG"
[ 2.512857] 0x000000660000-0x000000780000 : "0:SSD"
[ 2.518074] 0x000000780000-0x000000a00000 : "0:TZ"
[ 2.522834] 0x000000a00000-0x000000c80000 : "0:RPM"
[ 2.527607] 0x000000c80000-0x000001180000 : "0:APPSBL"
[ 2.532472] 0x000001180000-0x000001200000 : "0:APPSBLENV"
[ 2.537586] 0x000001200000-0x000001340000 : "0:ART"
[ 2.543140] 0x000001340000-0x000005340000 : "rootfs"

Are all these partition names supposed to be prefixed with "0:"? This
is using the OpenWrt applied version.


Jonas

2015-08-18 17:03:41

by Mathieu Olivari

[permalink] [raw]
Subject: Re: [PATCH v3 0/3] qcom: Add SMEM MTD parser

On Tue, Aug 18, 2015 at 02:30:14PM +0200, Jonas Gorski wrote:
> Hi,
>
> On Tue, Aug 18, 2015 at 1:47 AM, Mathieu Olivari <[email protected]> wrote:
> > QCOM platforms such as IPQ806x are using SMEM to store their flash
> > layout. This patch set adds the DT nodes required to instanciate SMEM
> > on IPQ806x and add an MTD parser using it.
> >
> > This change is based on the SMEM driver posted here:
> > *https://lkml.org/lkml/2015/7/27/1125
>
> Nice work. After testing it on AP148 I see this:
>
> [ 2.481507] 12 qcom-smem partitions found on MTD device qcom-nandc
> [ 2.481540] Creating 12 MTD partitions on "qcom-nandc":
> [ 2.486690] 0x000000000000-0x000000040000 : "0:SBL1"
> [ 2.492842] 0x000000040000-0x000000180000 : "0:MIBIB"
> [ 2.497857] 0x000000180000-0x0000002c0000 : "0:SBL2"
> [ 2.502895] 0x0000002c0000-0x000000540000 : "0:SBL3"
> [ 2.507828] 0x000000540000-0x000000660000 : "0:DDRCONFIG"
> [ 2.512857] 0x000000660000-0x000000780000 : "0:SSD"
> [ 2.518074] 0x000000780000-0x000000a00000 : "0:TZ"
> [ 2.522834] 0x000000a00000-0x000000c80000 : "0:RPM"
> [ 2.527607] 0x000000c80000-0x000001180000 : "0:APPSBL"
> [ 2.532472] 0x000001180000-0x000001200000 : "0:APPSBLENV"
> [ 2.537586] 0x000001200000-0x000001340000 : "0:ART"
> [ 2.543140] 0x000001340000-0x000005340000 : "rootfs"
>
> Are all these partition names supposed to be prefixed with "0:"? This
> is using the OpenWrt applied version.

Yes. These are the names as they are listed in SMEM. If you run the
"smem" command from the U-boot prompt, you will see the same information.
I'm not sure what the 0: prefix means, and where it comes from to be
honest.

>
>
> Jonas

2015-08-18 17:59:39

by Bjorn Andersson

[permalink] [raw]
Subject: Re: [PATCH v3 1/3] ARM: qcom: add SFPB nodes to IPQ806x dts

On Mon 17 Aug 16:47 PDT 2015, Mathieu Olivari wrote:

> Add one new node to the ipq806x.dtsi file to declare & register the
> hardware spinlock devices. This mechanism is required to be used by
> other drivers such as SMEM.
>

Reviewed-by: Bjorn Andersson <[email protected]>

> Signed-off-by: Mathieu Olivari <[email protected]>
> ---
> arch/arm/boot/dts/qcom-ipq8064.dtsi | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/arch/arm/boot/dts/qcom-ipq8064.dtsi b/arch/arm/boot/dts/qcom-ipq8064.dtsi
> index 9f727d8..8d366ae 100644
> --- a/arch/arm/boot/dts/qcom-ipq8064.dtsi
> +++ b/arch/arm/boot/dts/qcom-ipq8064.dtsi
> @@ -329,5 +329,16 @@
> #reset-cells = <1>;
> };
>
> + sfpb_mutex_block: syscon@1200600 {
> + compatible = "syscon";
> + reg = <0x01200600 0x100>;

I have this block listed to start at 0x1200000 and used 0x604 as offset
when testing this. In my docs there's nothing in those first 0x600 bytes
though, so this works fine for me.

> + };
> + };
> +
> + sfpb_mutex: sfpb-mutex {
> + compatible = "qcom,sfpb-mutex";
> + syscon = <&sfpb_mutex_block 4 4>;
> +
> + #hwlock-cells = <1>;
> };
> };

Regards,
Bjorn

2015-08-18 18:10:30

by Bjorn Andersson

[permalink] [raw]
Subject: Re: [PATCH v3 3/3] mtd: add SMEM parser for QCOM platforms

On Mon 17 Aug 16:47 PDT 2015, Mathieu Olivari wrote:

> On QCOM platforms using MTD devices storage (such as IPQ806x), SMEM is
> used to store partition layout. This new parser can now be used to read
> SMEM and use it to register an MTD layout according to its content.

Looks good, but the license string should be "GPL v2" (lower case v).
Just as a hint...I made checkpatch warn you about that a while back ;)

The rest is

Acked-by: Bjorn Andersson <[email protected]>

[..]

> +
> +MODULE_LICENSE("GPL V2");

Regards,
Bjorn

2015-08-18 18:11:58

by Bjorn Andersson

[permalink] [raw]
Subject: Re: [PATCH v3 2/3] ARM: qcom: add SMEM device node to IPQ806x dts

On Mon 17 Aug 16:47 PDT 2015, Mathieu Olivari wrote:

> SMEM is used on IPQ806x to store various board related information such
> as boot device and flash partition layout. We'll declare it as a device
> so we can make use of it thanks to the new SMEM soc driver.
>
> Signed-off-by: Mathieu Olivari <[email protected]>
> ---
> arch/arm/boot/dts/qcom-ipq8064.dtsi | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/arch/arm/boot/dts/qcom-ipq8064.dtsi b/arch/arm/boot/dts/qcom-ipq8064.dtsi
> index 8d366ae..85dbccf 100644
> --- a/arch/arm/boot/dts/qcom-ipq8064.dtsi
> +++ b/arch/arm/boot/dts/qcom-ipq8064.dtsi
> @@ -55,7 +55,7 @@
> no-map;
> };
>
> - smem@41000000 {
> + smem: smem@41000000 {

I would preferred if you used a name like "smem_region", although I
don't think we will use the phandle name "smem" for anything else...

> reg = <0x41000000 0x200000>;
> no-map;
> };
> @@ -341,4 +341,10 @@
>
> #hwlock-cells = <1>;
> };
> +
> + smem {
> + compatible = "qcom,smem";
> + memory-region = <&smem>;
> + hwlocks = <&sfpb_mutex 3>;
> + };
> };

Acked-by: Bjorn Andersson <[email protected]>

Regards,
Bjorn