ee0bf9309ff8410a95bc6da8ce66935f9fe05edb
[openwrt/staging/xback.git] /
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
5
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.
10
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
13 reserved-memory node.
14
15 The "qcom,smem" compatible is added to the reserved_mem_matches list, to
16 allow the reserved-memory device to be probed.
17
18 In order to retain the readability of the code, the resolution of
19 resources is split from the actual ioremapping.
20
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
25 ---
26 drivers/of/platform.c | 1 +
27 drivers/soc/qcom/smem.c | 57 ++++++++++++++++++++++++++++-------------
28 2 files changed, 40 insertions(+), 18 deletions(-)
29
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" },
39 {}
40 --- a/drivers/soc/qcom/smem.c
41 +++ b/drivers/soc/qcom/smem.c
42 @@ -9,6 +9,7 @@
43 #include <linux/module.h>
44 #include <linux/of.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
52 */
53 struct smem_region {
54 - u32 aux_base;
55 + phys_addr_t aux_base;
56 void __iomem *virt_base;
57 size_t size;
58 };
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];
62
63 - if (region->aux_base == aux_base || !aux_base) {
64 + if ((u32)region->aux_base == aux_base || !aux_base) {
65 if (size != NULL)
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;
71
72 - return (phys_addr_t)region->aux_base + offset;
73 + return region->aux_base + offset;
74 }
75 }
76
77 @@ -863,12 +864,12 @@ qcom_smem_enumerate_partitions(struct qc
78 return 0;
79 }
80
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)
85 {
86 + struct device *dev = smem->dev;
87 struct device_node *np;
88 struct resource r;
89 - resource_size_t size;
90 int ret;
91
92 np = of_parse_phandle(dev->of_node, name, 0);
93 @@ -881,13 +882,9 @@ static int qcom_smem_map_memory(struct q
94 of_node_put(np);
95 if (ret)
96 return ret;
97 - size = resource_size(&r);
98
99 - smem->regions[i].virt_base = devm_ioremap_wc(dev, r.start, size);
100 - if (!smem->regions[i].virt_base)
101 - return -ENOMEM;
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);
106
107 return 0;
108 }
109 @@ -895,12 +892,14 @@ static int qcom_smem_map_memory(struct q
110 static int qcom_smem_probe(struct platform_device *pdev)
111 {
112 struct smem_header *header;
113 + struct reserved_mem *rmem;
114 struct qcom_smem *smem;
115 size_t array_size;
116 int num_regions;
117 int hwlock_id;
118 u32 version;
119 int ret;
120 + int i;
121
122 num_regions = 1;
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;
127
128 - ret = qcom_smem_map_memory(smem, &pdev->dev, "memory-region", 0);
129 - if (ret)
130 - return ret;
131 -
132 - if (num_regions > 1 && (ret = qcom_smem_map_memory(smem, &pdev->dev,
133 - "qcom,rpm-msg-ram", 1)))
134 - return ret;
135 + rmem = of_reserved_mem_lookup(pdev->dev.of_node);
136 + if (rmem) {
137 + smem->regions[0].aux_base = rmem->base;
138 + smem->regions[0].size = rmem->size;
139 + } else {
140 + /*
141 + * Fall back to the memory-region reference, if we're not a
142 + * reserved-memory node.
143 + */
144 + ret = qcom_smem_resolve_mem(smem, "memory-region", &smem->regions[0]);
145 + if (ret)
146 + return ret;
147 + }
148 +
149 + if (num_regions > 1) {
150 + ret = qcom_smem_resolve_mem(smem, "qcom,rpm-msg-ram", &smem->regions[1]);
151 + if (ret)
152 + return ret;
153 + }
154 +
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);
161 + return -ENOMEM;
162 + }
163 + }
164
165 header = smem->regions[0].virt_base;
166 if (le32_to_cpu(header->initialized) != 1 ||