hc
2024-01-05 071106ecf68c401173c58808b1cf5f68cc50d390
kernel/drivers/of/device.c
....@@ -5,7 +5,8 @@
55 #include <linux/of_device.h>
66 #include <linux/of_address.h>
77 #include <linux/of_iommu.h>
8
-#include <linux/dma-mapping.h>
8
+#include <linux/dma-direct.h> /* for bus_dma_region */
9
+#include <linux/dma-map-ops.h>
910 #include <linux/init.h>
1011 #include <linux/module.h>
1112 #include <linux/mod_devicetable.h>
....@@ -17,7 +18,7 @@
1718
1819 /**
1920 * of_match_device - Tell if a struct device matches an of_device_id list
20
- * @ids: array of of device match structures to search in
21
+ * @matches: array of of device match structures to search in
2122 * @dev: the of device structure to match against
2223 *
2324 * Used by a driver to check whether an platform_device present in the
....@@ -78,6 +79,7 @@
7879 * @np: Pointer to OF node having DMA configuration
7980 * @force_dma: Whether device is to be set up by of_dma_configure() even if
8081 * DMA capability is not explicitly described by firmware.
82
+ * @id: Optional const pointer value input id
8183 *
8284 * Try to get devices's DMA configuration from DT and update it
8385 * accordingly.
....@@ -86,16 +88,17 @@
8688 * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events
8789 * to fix up DMA configuration.
8890 */
89
-int of_dma_configure(struct device *dev, struct device_node *np, bool force_dma)
91
+int of_dma_configure_id(struct device *dev, struct device_node *np,
92
+ bool force_dma, const u32 *id)
9093 {
91
- u64 dma_addr, paddr, size = 0;
92
- int ret;
93
- bool coherent;
94
- unsigned long offset;
9594 const struct iommu_ops *iommu;
96
- u64 mask;
95
+ const struct bus_dma_region *map = NULL;
96
+ u64 dma_start = 0;
97
+ u64 mask, end, size = 0;
98
+ bool coherent;
99
+ int ret;
97100
98
- ret = of_dma_get_range(np, &dma_addr, &paddr, &size);
101
+ ret = of_dma_get_range(np, &map);
99102 if (ret < 0) {
100103 /*
101104 * For legacy reasons, we have to assume some devices need
....@@ -104,26 +107,35 @@
104107 */
105108 if (!force_dma)
106109 return ret == -ENODEV ? 0 : ret;
107
-
108
- dma_addr = offset = 0;
109110 } else {
110
- offset = PFN_DOWN(paddr - dma_addr);
111
+ const struct bus_dma_region *r = map;
112
+ u64 dma_end = 0;
113
+
114
+ /* Determine the overall bounds of all DMA regions */
115
+ for (dma_start = ~0; r->size; r++) {
116
+ /* Take lower and upper limits */
117
+ if (r->dma_start < dma_start)
118
+ dma_start = r->dma_start;
119
+ if (r->dma_start + r->size > dma_end)
120
+ dma_end = r->dma_start + r->size;
121
+ }
122
+ size = dma_end - dma_start;
111123
112124 /*
113125 * Add a work around to treat the size as mask + 1 in case
114126 * it is defined in DT as a mask.
115127 */
116128 if (size & 1) {
117
- dev_warn(dev, "Invalid size 0x%llx for dma-range\n",
129
+ dev_warn(dev, "Invalid size 0x%llx for dma-range(s)\n",
118130 size);
119131 size = size + 1;
120132 }
121133
122134 if (!size) {
123135 dev_err(dev, "Adjusted size 0x%llx invalid\n", size);
136
+ kfree(map);
124137 return -EINVAL;
125138 }
126
- dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", offset);
127139 }
128140
129141 /*
....@@ -142,47 +154,41 @@
142154 else if (!size)
143155 size = 1ULL << 32;
144156
145
- dev->dma_pfn_offset = offset;
146
-
147157 /*
148158 * Limit coherent and dma mask based on size and default mask
149159 * set by the driver.
150160 */
151
- mask = DMA_BIT_MASK(ilog2(dma_addr + size - 1) + 1);
161
+ end = dma_start + size - 1;
162
+ mask = DMA_BIT_MASK(ilog2(end) + 1);
152163 dev->coherent_dma_mask &= mask;
153164 *dev->dma_mask &= mask;
154
- /* ...but only set bus mask if we found valid dma-ranges earlier */
155
- if (!ret)
156
- dev->bus_dma_mask = mask;
165
+ /* ...but only set bus limit and range map if we found valid dma-ranges earlier */
166
+ if (!ret) {
167
+ dev->bus_dma_limit = end;
168
+ dev->dma_range_map = map;
169
+ }
157170
158171 coherent = of_dma_is_coherent(np);
159172 dev_dbg(dev, "device is%sdma coherent\n",
160173 coherent ? " " : " not ");
161174
162
- iommu = of_iommu_configure(dev, np);
163
- if (IS_ERR(iommu) && PTR_ERR(iommu) == -EPROBE_DEFER)
175
+ iommu = of_iommu_configure(dev, np, id);
176
+ if (PTR_ERR(iommu) == -EPROBE_DEFER) {
177
+ /* Don't touch range map if it wasn't set from a valid dma-ranges */
178
+ if (!ret)
179
+ dev->dma_range_map = NULL;
180
+ kfree(map);
164181 return -EPROBE_DEFER;
182
+ }
165183
166184 dev_dbg(dev, "device is%sbehind an iommu\n",
167185 iommu ? " " : " not ");
168186
169
- arch_setup_dma_ops(dev, dma_addr, size, iommu, coherent);
187
+ arch_setup_dma_ops(dev, dma_start, size, iommu, coherent);
170188
171189 return 0;
172190 }
173
-EXPORT_SYMBOL_GPL(of_dma_configure);
174
-
175
-/**
176
- * of_dma_deconfigure - Clean up DMA configuration
177
- * @dev: Device for which to clean up DMA configuration
178
- *
179
- * Clean up all configuration performed by of_dma_configure_ops() and free all
180
- * resources that have been allocated.
181
- */
182
-void of_dma_deconfigure(struct device *dev)
183
-{
184
- arch_teardown_dma_ops(dev);
185
-}
191
+EXPORT_SYMBOL_GPL(of_dma_configure_id);
186192
187193 int of_device_register(struct platform_device *pdev)
188194 {
....@@ -223,7 +229,7 @@
223229 /* Name & Type */
224230 /* %p eats all alphanum characters, so %c must be used here */
225231 csize = snprintf(str, len, "of:N%pOFn%c%s", dev->of_node, 'T',
226
- dev->of_node->type);
232
+ of_node_get_device_type(dev->of_node));
227233 tsize = csize;
228234 len -= csize;
229235 if (str)
....@@ -258,12 +264,15 @@
258264 if (size < 0)
259265 return size;
260266
261
- str = kmalloc(size + 1, GFP_KERNEL);
267
+ /* Reserve an additional byte for the trailing '\0' */
268
+ size++;
269
+
270
+ str = kmalloc(size, GFP_KERNEL);
262271 if (!str)
263272 return -ENOMEM;
264273
265274 of_device_get_modalias(dev, str, size);
266
- str[size] = '\0';
275
+ str[size - 1] = '\0';
267276 ret = request_module(str);
268277 kfree(str);
269278
....@@ -293,7 +302,7 @@
293302 */
294303 void of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
295304 {
296
- const char *compat;
305
+ const char *compat, *type;
297306 struct alias_prop *app;
298307 struct property *p;
299308 int seen = 0;
....@@ -303,8 +312,9 @@
303312
304313 add_uevent_var(env, "OF_NAME=%pOFn", dev->of_node);
305314 add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node);
306
- if (dev->of_node->type && strcmp("<NULL>", dev->of_node->type) != 0)
307
- add_uevent_var(env, "OF_TYPE=%s", dev->of_node->type);
315
+ type = of_node_get_device_type(dev->of_node);
316
+ if (type)
317
+ add_uevent_var(env, "OF_TYPE=%s", type);
308318
309319 /* Since the compatible field can contain pretty much anything
310320 * it's not really legal to split it out with commas. We split it