1 From b5af64fceb04dc298c5e69c517b4d83893ff060b Mon Sep 17 00:00:00 2001
2 From: Bjorn Andersson <bjorn.andersson@linaro.org>
3 Date: Thu, 30 Sep 2021 11:21:10 -0700
4 Subject: [PATCH 1/1] soc: qcom: smem: Support reserved-memory description
6 Practically all modern Qualcomm platforms has a single reserved-memory
7 region for SMEM. So rather than having to describe SMEM in the form of a
8 node with a reference to a reserved-memory node, allow the SMEM device
9 to be instantiated directly from the reserved-memory node.
11 The current means of falling back to dereferencing the "memory-region"
12 is kept as a fallback, if it's determined that the SMEM node is a
15 The "qcom,smem" compatible is added to the reserved_mem_matches list, to
16 allow the reserved-memory device to be probed.
18 In order to retain the readability of the code, the resolution of
19 resources is split from the actual ioremapping.
21 Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
22 Acked-by: Rob Herring <robh@kernel.org>
23 Reviewed-by: Vladimir Zapolskiy <vladimir.zapolskiy@linaro.org>
24 Link: https://lore.kernel.org/r/20210930182111.57353-4-bjorn.andersson@linaro.org
26 drivers/of/platform.c | 1 +
27 drivers/soc/qcom/smem.c | 57 ++++++++++++++++++++++++++++-------------
28 2 files changed, 40 insertions(+), 18 deletions(-)
30 --- a/drivers/of/platform.c
31 +++ b/drivers/of/platform.c
32 @@ -509,6 +509,7 @@ EXPORT_SYMBOL_GPL(of_platform_default_po
33 static const struct of_device_id reserved_mem_matches[] = {
34 { .compatible = "qcom,rmtfs-mem" },
35 { .compatible = "qcom,cmd-db" },
36 + { .compatible = "qcom,smem" },
37 { .compatible = "ramoops" },
38 { .compatible = "nvmem-rmem" },
40 --- a/drivers/soc/qcom/smem.c
41 +++ b/drivers/soc/qcom/smem.c
43 #include <linux/module.h>
45 #include <linux/of_address.h>
46 +#include <linux/of_reserved_mem.h>
47 #include <linux/platform_device.h>
48 #include <linux/sizes.h>
49 #include <linux/slab.h>
50 @@ -240,7 +241,7 @@ static const u8 SMEM_INFO_MAGIC[] = { 0x
51 * @size: size of the memory region
55 + phys_addr_t aux_base;
56 void __iomem *virt_base;
59 @@ -499,7 +500,7 @@ static void *qcom_smem_get_global(struct
60 for (i = 0; i < smem->num_regions; i++) {
61 region = &smem->regions[i];
63 - if (region->aux_base == aux_base || !aux_base) {
64 + if ((u32)region->aux_base == aux_base || !aux_base) {
66 *size = le32_to_cpu(entry->size);
67 return region->virt_base + le32_to_cpu(entry->offset);
68 @@ -664,7 +665,7 @@ phys_addr_t qcom_smem_virt_to_phys(void
69 if (p < region->virt_base + region->size) {
70 u64 offset = p - region->virt_base;
72 - return (phys_addr_t)region->aux_base + offset;
73 + return region->aux_base + offset;
77 @@ -863,12 +864,12 @@ qcom_smem_enumerate_partitions(struct qc
81 -static int qcom_smem_map_memory(struct qcom_smem *smem, struct device *dev,
82 - const char *name, int i)
83 +static int qcom_smem_resolve_mem(struct qcom_smem *smem, const char *name,
84 + struct smem_region *region)
86 + struct device *dev = smem->dev;
87 struct device_node *np;
89 - resource_size_t size;
92 np = of_parse_phandle(dev->of_node, name, 0);
93 @@ -881,13 +882,9 @@ static int qcom_smem_map_memory(struct q
97 - size = resource_size(&r);
99 - smem->regions[i].virt_base = devm_ioremap_wc(dev, r.start, size);
100 - if (!smem->regions[i].virt_base)
102 - smem->regions[i].aux_base = (u32)r.start;
103 - smem->regions[i].size = size;
104 + region->aux_base = r.start;
105 + region->size = resource_size(&r);
109 @@ -895,12 +892,14 @@ static int qcom_smem_map_memory(struct q
110 static int qcom_smem_probe(struct platform_device *pdev)
112 struct smem_header *header;
113 + struct reserved_mem *rmem;
114 struct qcom_smem *smem;
123 if (of_find_property(pdev->dev.of_node, "qcom,rpm-msg-ram", NULL))
124 @@ -914,13 +913,35 @@ static int qcom_smem_probe(struct platfo
125 smem->dev = &pdev->dev;
126 smem->num_regions = num_regions;
128 - ret = qcom_smem_map_memory(smem, &pdev->dev, "memory-region", 0);
132 - if (num_regions > 1 && (ret = qcom_smem_map_memory(smem, &pdev->dev,
133 - "qcom,rpm-msg-ram", 1)))
135 + rmem = of_reserved_mem_lookup(pdev->dev.of_node);
137 + smem->regions[0].aux_base = rmem->base;
138 + smem->regions[0].size = rmem->size;
141 + * Fall back to the memory-region reference, if we're not a
142 + * reserved-memory node.
144 + ret = qcom_smem_resolve_mem(smem, "memory-region", &smem->regions[0]);
149 + if (num_regions > 1) {
150 + ret = qcom_smem_resolve_mem(smem, "qcom,rpm-msg-ram", &smem->regions[1]);
155 + for (i = 0; i < num_regions; i++) {
156 + smem->regions[i].virt_base = devm_ioremap_wc(&pdev->dev,
157 + smem->regions[i].aux_base,
158 + smem->regions[i].size);
159 + if (!smem->regions[i].virt_base) {
160 + dev_err(&pdev->dev, "failed to remap %pa\n", &smem->regions[i].aux_base);
165 header = smem->regions[0].virt_base;
166 if (le32_to_cpu(header->initialized) != 1 ||