blob: 945b3d785f440eeeeb0b60296e645b9e724b33cd [file] [log] [blame]
Rob Herringaf6074f2017-12-27 12:55:14 -06001// SPDX-License-Identifier: GPL-2.0
Rob Herring606ad422016-06-15 08:32:18 -05002#define pr_fmt(fmt) "OF: " fmt
3
Arnd Bergmann5019f0b2012-04-11 17:30:11 +00004#include <linux/device.h>
Gabriele Paolonifcfaab32018-03-15 02:15:52 +08005#include <linux/fwnode.h>
Grant Likely6b884a82010-06-08 07:48:09 -06006#include <linux/io.h>
7#include <linux/ioport.h>
Zhichang Yuan65af6182018-03-15 02:15:54 +08008#include <linux/logic_pio.h>
Grant Likelydbbdee92010-06-08 07:48:10 -06009#include <linux/module.h>
Grant Likely6b884a82010-06-08 07:48:09 -060010#include <linux/of_address.h>
Tomasz Nowickic5076cf2016-05-11 17:34:51 -050011#include <linux/pci.h>
Grant Likelydbbdee92010-06-08 07:48:10 -060012#include <linux/pci_regs.h>
Liviu Dudau41f8bba2014-09-29 15:29:21 +010013#include <linux/sizes.h>
14#include <linux/slab.h>
Grant Likelydbbdee92010-06-08 07:48:10 -060015#include <linux/string.h>
Grant Likely6b884a82010-06-08 07:48:09 -060016
Robin Murphyb68ac8d2019-07-02 18:42:39 +010017#include "of_private.h"
18
Grant Likelydbbdee92010-06-08 07:48:10 -060019/* Max address size we deal with */
20#define OF_MAX_ADDR_CELLS 4
Stephen Warren5d61b162012-07-25 17:34:37 -060021#define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
22#define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
Grant Likelydbbdee92010-06-08 07:48:10 -060023
24static struct of_bus *of_match_bus(struct device_node *np);
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +010025static int __of_address_to_resource(struct device_node *dev,
26 const __be32 *addrp, u64 size, unsigned int flags,
Benoit Cousson35f3da32011-12-05 15:23:55 +010027 const char *name, struct resource *r);
Grant Likelydbbdee92010-06-08 07:48:10 -060028
29/* Debug utility */
30#ifdef DEBUG
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +010031static void of_dump_addr(const char *s, const __be32 *addr, int na)
Grant Likelydbbdee92010-06-08 07:48:10 -060032{
Rob Herring606ad422016-06-15 08:32:18 -050033 pr_debug("%s", s);
Grant Likelydbbdee92010-06-08 07:48:10 -060034 while (na--)
Rob Herring606ad422016-06-15 08:32:18 -050035 pr_cont(" %08x", be32_to_cpu(*(addr++)));
36 pr_cont("\n");
Grant Likelydbbdee92010-06-08 07:48:10 -060037}
38#else
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +010039static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
Grant Likelydbbdee92010-06-08 07:48:10 -060040#endif
41
42/* Callbacks for bus specific translators */
43struct of_bus {
44 const char *name;
45 const char *addresses;
46 int (*match)(struct device_node *parent);
47 void (*count_cells)(struct device_node *child,
48 int *addrc, int *sizec);
Kim Phillips47b1e682012-10-08 19:41:58 -050049 u64 (*map)(__be32 *addr, const __be32 *range,
Grant Likelydbbdee92010-06-08 07:48:10 -060050 int na, int ns, int pna);
Kim Phillips47b1e682012-10-08 19:41:58 -050051 int (*translate)(__be32 *addr, u64 offset, int na);
Jiaxun Yang2f965932020-07-28 23:36:55 +080052 bool has_flags;
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +010053 unsigned int (*get_flags)(const __be32 *addr);
Grant Likelydbbdee92010-06-08 07:48:10 -060054};
55
56/*
57 * Default translator (generic bus)
58 */
59
60static void of_bus_default_count_cells(struct device_node *dev,
61 int *addrc, int *sizec)
62{
63 if (addrc)
64 *addrc = of_n_addr_cells(dev);
65 if (sizec)
66 *sizec = of_n_size_cells(dev);
67}
68
Kim Phillips47b1e682012-10-08 19:41:58 -050069static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
Grant Likelydbbdee92010-06-08 07:48:10 -060070 int na, int ns, int pna)
71{
72 u64 cp, s, da;
73
74 cp = of_read_number(range, na);
75 s = of_read_number(range + na + pna, ns);
76 da = of_read_number(addr, na);
77
Rob Herring606ad422016-06-15 08:32:18 -050078 pr_debug("default map, cp=%llx, s=%llx, da=%llx\n",
Grant Likelydbbdee92010-06-08 07:48:10 -060079 (unsigned long long)cp, (unsigned long long)s,
80 (unsigned long long)da);
81
82 if (da < cp || da >= (cp + s))
83 return OF_BAD_ADDR;
84 return da - cp;
85}
86
Kim Phillips47b1e682012-10-08 19:41:58 -050087static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
Grant Likelydbbdee92010-06-08 07:48:10 -060088{
89 u64 a = of_read_number(addr, na);
90 memset(addr, 0, na * 4);
91 a += offset;
92 if (na > 1)
Grant Likely154063a92010-06-08 07:48:11 -060093 addr[na - 2] = cpu_to_be32(a >> 32);
94 addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
Grant Likelydbbdee92010-06-08 07:48:10 -060095
96 return 0;
97}
98
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +010099static unsigned int of_bus_default_get_flags(const __be32 *addr)
Grant Likelydbbdee92010-06-08 07:48:10 -0600100{
101 return IORESOURCE_MEM;
102}
103
Jiaxun Yang0fc0ead2020-07-30 08:56:14 +0800104#ifdef CONFIG_PCI
Rob Herring67ccd2b2020-02-06 14:34:54 +0000105static unsigned int of_bus_pci_get_flags(const __be32 *addr)
106{
107 unsigned int flags = 0;
108 u32 w = be32_to_cpup(addr);
109
110 if (!IS_ENABLED(CONFIG_PCI))
111 return 0;
112
113 switch((w >> 24) & 0x03) {
114 case 0x01:
115 flags |= IORESOURCE_IO;
116 break;
117 case 0x02: /* 32 bits */
118 case 0x03: /* 64 bits */
119 flags |= IORESOURCE_MEM;
120 break;
121 }
122 if (w & 0x40000000)
123 flags |= IORESOURCE_PREFETCH;
124 return flags;
125}
126
Grant Likelydbbdee92010-06-08 07:48:10 -0600127/*
128 * PCI bus specific translator
129 */
130
131static int of_bus_pci_match(struct device_node *np)
132{
Benjamin Herrenschmidt6dd18e42013-07-03 16:01:10 +1000133 /*
Kleber Sacilotto de Souza14e2abb2014-02-03 13:31:03 -0200134 * "pciex" is PCI Express
Benjamin Herrenschmidt6dd18e42013-07-03 16:01:10 +1000135 * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
136 * "ht" is hypertransport
137 */
Rob Herringe8b1dee22018-08-29 08:36:12 -0500138 return of_node_is_type(np, "pci") || of_node_is_type(np, "pciex") ||
139 of_node_is_type(np, "vci") || of_node_is_type(np, "ht");
Grant Likelydbbdee92010-06-08 07:48:10 -0600140}
141
142static void of_bus_pci_count_cells(struct device_node *np,
143 int *addrc, int *sizec)
144{
145 if (addrc)
146 *addrc = 3;
147 if (sizec)
148 *sizec = 2;
149}
150
Kim Phillips47b1e682012-10-08 19:41:58 -0500151static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100152 int pna)
Grant Likelydbbdee92010-06-08 07:48:10 -0600153{
154 u64 cp, s, da;
155 unsigned int af, rf;
156
157 af = of_bus_pci_get_flags(addr);
158 rf = of_bus_pci_get_flags(range);
159
160 /* Check address type match */
161 if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
162 return OF_BAD_ADDR;
163
164 /* Read address values, skipping high cell */
165 cp = of_read_number(range + 1, na - 1);
166 s = of_read_number(range + na + pna, ns);
167 da = of_read_number(addr + 1, na - 1);
168
Rob Herring606ad422016-06-15 08:32:18 -0500169 pr_debug("PCI map, cp=%llx, s=%llx, da=%llx\n",
Grant Likelydbbdee92010-06-08 07:48:10 -0600170 (unsigned long long)cp, (unsigned long long)s,
171 (unsigned long long)da);
172
173 if (da < cp || da >= (cp + s))
174 return OF_BAD_ADDR;
175 return da - cp;
176}
177
Kim Phillips47b1e682012-10-08 19:41:58 -0500178static int of_bus_pci_translate(__be32 *addr, u64 offset, int na)
Grant Likelydbbdee92010-06-08 07:48:10 -0600179{
180 return of_bus_default_translate(addr + 1, offset, na - 1);
181}
182
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100183const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
Grant Likelydbbdee92010-06-08 07:48:10 -0600184 unsigned int *flags)
185{
Jeremy Kerra9fadee2010-10-10 21:24:10 -0600186 const __be32 *prop;
Grant Likelydbbdee92010-06-08 07:48:10 -0600187 unsigned int psize;
188 struct device_node *parent;
189 struct of_bus *bus;
190 int onesize, i, na, ns;
191
192 /* Get parent & match bus type */
193 parent = of_get_parent(dev);
194 if (parent == NULL)
195 return NULL;
196 bus = of_match_bus(parent);
197 if (strcmp(bus->name, "pci")) {
198 of_node_put(parent);
199 return NULL;
200 }
201 bus->count_cells(dev, &na, &ns);
202 of_node_put(parent);
Stephen Warren5d61b162012-07-25 17:34:37 -0600203 if (!OF_CHECK_ADDR_COUNT(na))
Grant Likelydbbdee92010-06-08 07:48:10 -0600204 return NULL;
205
206 /* Get "reg" or "assigned-addresses" property */
207 prop = of_get_property(dev, bus->addresses, &psize);
208 if (prop == NULL)
209 return NULL;
210 psize /= 4;
211
212 onesize = na + ns;
Grant Likely154063a92010-06-08 07:48:11 -0600213 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
214 u32 val = be32_to_cpu(prop[0]);
215 if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
Grant Likelydbbdee92010-06-08 07:48:10 -0600216 if (size)
217 *size = of_read_number(prop + na, ns);
218 if (flags)
219 *flags = bus->get_flags(prop);
220 return prop;
221 }
Grant Likely154063a92010-06-08 07:48:11 -0600222 }
Grant Likelydbbdee92010-06-08 07:48:10 -0600223 return NULL;
224}
225EXPORT_SYMBOL(of_get_pci_address);
226
227int of_pci_address_to_resource(struct device_node *dev, int bar,
228 struct resource *r)
229{
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100230 const __be32 *addrp;
Grant Likelydbbdee92010-06-08 07:48:10 -0600231 u64 size;
232 unsigned int flags;
233
234 addrp = of_get_pci_address(dev, bar, &size, &flags);
235 if (addrp == NULL)
236 return -EINVAL;
Benoit Cousson35f3da32011-12-05 15:23:55 +0100237 return __of_address_to_resource(dev, addrp, size, flags, NULL, r);
Grant Likelydbbdee92010-06-08 07:48:10 -0600238}
239EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
Andrew Murray29b635c2013-05-16 17:55:17 +0200240
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100241/*
242 * of_pci_range_to_resource - Create a resource from an of_pci_range
243 * @range: the PCI range that describes the resource
244 * @np: device node where the range belongs to
245 * @res: pointer to a valid resource that will be updated to
246 * reflect the values contained in the range.
247 *
248 * Returns EINVAL if the range cannot be converted to resource.
249 *
250 * Note that if the range is an IO range, the resource will be converted
251 * using pci_address_to_pio() which can fail if it is called too early or
252 * if the range cannot be matched to any host bridge IO space (our case here).
253 * To guard against that we try to register the IO range first.
254 * If that fails we know that pci_address_to_pio() will do too.
255 */
256int of_pci_range_to_resource(struct of_pci_range *range,
257 struct device_node *np, struct resource *res)
Liviu Dudau83bbde12014-09-29 15:29:24 +0100258{
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100259 int err;
Liviu Dudau83bbde12014-09-29 15:29:24 +0100260 res->flags = range->flags;
Liviu Dudau83bbde12014-09-29 15:29:24 +0100261 res->parent = res->child = res->sibling = NULL;
262 res->name = np->full_name;
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100263
264 if (res->flags & IORESOURCE_IO) {
265 unsigned long port;
Gabriele Paolonifcfaab32018-03-15 02:15:52 +0800266 err = pci_register_io_range(&np->fwnode, range->cpu_addr,
267 range->size);
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100268 if (err)
269 goto invalid_range;
270 port = pci_address_to_pio(range->cpu_addr);
271 if (port == (unsigned long)-1) {
272 err = -EINVAL;
273 goto invalid_range;
274 }
275 res->start = port;
276 } else {
Pavel Fedin4af97102015-10-08 10:24:25 +0300277 if ((sizeof(resource_size_t) < 8) &&
278 upper_32_bits(range->cpu_addr)) {
279 err = -EINVAL;
280 goto invalid_range;
281 }
282
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100283 res->start = range->cpu_addr;
284 }
285 res->end = res->start + range->size - 1;
286 return 0;
287
288invalid_range:
289 res->start = (resource_size_t)OF_BAD_ADDR;
290 res->end = (resource_size_t)OF_BAD_ADDR;
291 return err;
Liviu Dudau83bbde12014-09-29 15:29:24 +0100292}
Manikanta Maddireddybf6681e2018-01-11 11:38:02 +0530293EXPORT_SYMBOL(of_pci_range_to_resource);
Grant Likelydbbdee92010-06-08 07:48:10 -0600294#endif /* CONFIG_PCI */
295
296/*
297 * ISA bus specific translator
298 */
299
300static int of_bus_isa_match(struct device_node *np)
301{
Rob Herringb3e46d12018-08-27 08:37:06 -0500302 return of_node_name_eq(np, "isa");
Grant Likelydbbdee92010-06-08 07:48:10 -0600303}
304
305static void of_bus_isa_count_cells(struct device_node *child,
306 int *addrc, int *sizec)
307{
308 if (addrc)
309 *addrc = 2;
310 if (sizec)
311 *sizec = 1;
312}
313
Kim Phillips47b1e682012-10-08 19:41:58 -0500314static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100315 int pna)
Grant Likelydbbdee92010-06-08 07:48:10 -0600316{
317 u64 cp, s, da;
318
319 /* Check address type match */
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100320 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
Grant Likelydbbdee92010-06-08 07:48:10 -0600321 return OF_BAD_ADDR;
322
323 /* Read address values, skipping high cell */
324 cp = of_read_number(range + 1, na - 1);
325 s = of_read_number(range + na + pna, ns);
326 da = of_read_number(addr + 1, na - 1);
327
Rob Herring606ad422016-06-15 08:32:18 -0500328 pr_debug("ISA map, cp=%llx, s=%llx, da=%llx\n",
Grant Likelydbbdee92010-06-08 07:48:10 -0600329 (unsigned long long)cp, (unsigned long long)s,
330 (unsigned long long)da);
331
332 if (da < cp || da >= (cp + s))
333 return OF_BAD_ADDR;
334 return da - cp;
335}
336
Kim Phillips47b1e682012-10-08 19:41:58 -0500337static int of_bus_isa_translate(__be32 *addr, u64 offset, int na)
Grant Likelydbbdee92010-06-08 07:48:10 -0600338{
339 return of_bus_default_translate(addr + 1, offset, na - 1);
340}
341
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100342static unsigned int of_bus_isa_get_flags(const __be32 *addr)
Grant Likelydbbdee92010-06-08 07:48:10 -0600343{
344 unsigned int flags = 0;
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100345 u32 w = be32_to_cpup(addr);
Grant Likelydbbdee92010-06-08 07:48:10 -0600346
347 if (w & 1)
348 flags |= IORESOURCE_IO;
349 else
350 flags |= IORESOURCE_MEM;
351 return flags;
352}
353
354/*
355 * Array of bus specific translators
356 */
357
358static struct of_bus of_busses[] = {
Rob Herring4670d612018-01-17 17:36:39 -0600359#ifdef CONFIG_PCI
Grant Likelydbbdee92010-06-08 07:48:10 -0600360 /* PCI */
361 {
362 .name = "pci",
363 .addresses = "assigned-addresses",
364 .match = of_bus_pci_match,
365 .count_cells = of_bus_pci_count_cells,
366 .map = of_bus_pci_map,
367 .translate = of_bus_pci_translate,
Jiaxun Yang2f965932020-07-28 23:36:55 +0800368 .has_flags = true,
Grant Likelydbbdee92010-06-08 07:48:10 -0600369 .get_flags = of_bus_pci_get_flags,
370 },
Rob Herring4670d612018-01-17 17:36:39 -0600371#endif /* CONFIG_PCI */
Grant Likelydbbdee92010-06-08 07:48:10 -0600372 /* ISA */
373 {
374 .name = "isa",
375 .addresses = "reg",
376 .match = of_bus_isa_match,
377 .count_cells = of_bus_isa_count_cells,
378 .map = of_bus_isa_map,
379 .translate = of_bus_isa_translate,
Jiaxun Yang2f965932020-07-28 23:36:55 +0800380 .has_flags = true,
Grant Likelydbbdee92010-06-08 07:48:10 -0600381 .get_flags = of_bus_isa_get_flags,
382 },
383 /* Default */
384 {
385 .name = "default",
386 .addresses = "reg",
387 .match = NULL,
388 .count_cells = of_bus_default_count_cells,
389 .map = of_bus_default_map,
390 .translate = of_bus_default_translate,
391 .get_flags = of_bus_default_get_flags,
392 },
393};
394
395static struct of_bus *of_match_bus(struct device_node *np)
396{
397 int i;
398
399 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
400 if (!of_busses[i].match || of_busses[i].match(np))
401 return &of_busses[i];
402 BUG();
403 return NULL;
404}
405
Benjamin Herrenschmidt41d94892015-03-23 14:16:38 +1100406static int of_empty_ranges_quirk(struct device_node *np)
Benjamin Herrenschmidt746c9e92014-11-14 17:55:03 +1100407{
408 if (IS_ENABLED(CONFIG_PPC)) {
Benjamin Herrenschmidt41d94892015-03-23 14:16:38 +1100409 /* To save cycles, we cache the result for global "Mac" setting */
Benjamin Herrenschmidt746c9e92014-11-14 17:55:03 +1100410 static int quirk_state = -1;
411
Benjamin Herrenschmidt41d94892015-03-23 14:16:38 +1100412 /* PA-SEMI sdc DT bug */
413 if (of_device_is_compatible(np, "1682m-sdc"))
414 return true;
415
416 /* Make quirk cached */
Benjamin Herrenschmidt746c9e92014-11-14 17:55:03 +1100417 if (quirk_state < 0)
418 quirk_state =
419 of_machine_is_compatible("Power Macintosh") ||
420 of_machine_is_compatible("MacRISC");
421 return quirk_state;
422 }
423 return false;
424}
425
Grant Likelydbbdee92010-06-08 07:48:10 -0600426static int of_translate_one(struct device_node *parent, struct of_bus *bus,
Kim Phillips47b1e682012-10-08 19:41:58 -0500427 struct of_bus *pbus, __be32 *addr,
Grant Likelydbbdee92010-06-08 07:48:10 -0600428 int na, int ns, int pna, const char *rprop)
429{
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100430 const __be32 *ranges;
Grant Likelydbbdee92010-06-08 07:48:10 -0600431 unsigned int rlen;
432 int rone;
433 u64 offset = OF_BAD_ADDR;
434
Masahiro Yamadaba85edb2015-11-30 15:22:37 +0900435 /*
436 * Normally, an absence of a "ranges" property means we are
Grant Likelydbbdee92010-06-08 07:48:10 -0600437 * crossing a non-translatable boundary, and thus the addresses
Masahiro Yamadaba85edb2015-11-30 15:22:37 +0900438 * below the current cannot be converted to CPU physical ones.
Grant Likelydbbdee92010-06-08 07:48:10 -0600439 * Unfortunately, while this is very clear in the spec, it's not
440 * what Apple understood, and they do have things like /uni-n or
441 * /ht nodes with no "ranges" property and a lot of perfectly
442 * useable mapped devices below them. Thus we treat the absence of
443 * "ranges" as equivalent to an empty "ranges" property which means
444 * a 1:1 translation at that level. It's up to the caller not to try
445 * to translate addresses that aren't supposed to be translated in
446 * the first place. --BenH.
Grant Likely3930f292010-06-08 07:48:11 -0600447 *
448 * As far as we know, this damage only exists on Apple machines, so
449 * This code is only enabled on powerpc. --gcl
Rob Herring81db12e2019-09-04 11:43:30 +0100450 *
451 * This quirk also applies for 'dma-ranges' which frequently exist in
452 * child nodes without 'dma-ranges' in the parent nodes. --RobH
Grant Likelydbbdee92010-06-08 07:48:10 -0600453 */
454 ranges = of_get_property(parent, rprop, &rlen);
Rob Herring81db12e2019-09-04 11:43:30 +0100455 if (ranges == NULL && !of_empty_ranges_quirk(parent) &&
456 strcmp(rprop, "dma-ranges")) {
Rob Herring606ad422016-06-15 08:32:18 -0500457 pr_debug("no ranges; cannot translate\n");
Grant Likely3930f292010-06-08 07:48:11 -0600458 return 1;
459 }
Grant Likelydbbdee92010-06-08 07:48:10 -0600460 if (ranges == NULL || rlen == 0) {
461 offset = of_read_number(addr, na);
462 memset(addr, 0, pna * 4);
Rob Herring606ad422016-06-15 08:32:18 -0500463 pr_debug("empty ranges; 1:1 translation\n");
Grant Likelydbbdee92010-06-08 07:48:10 -0600464 goto finish;
465 }
466
Rob Herring606ad422016-06-15 08:32:18 -0500467 pr_debug("walking ranges...\n");
Grant Likelydbbdee92010-06-08 07:48:10 -0600468
469 /* Now walk through the ranges */
470 rlen /= 4;
471 rone = na + pna + ns;
472 for (; rlen >= rone; rlen -= rone, ranges += rone) {
473 offset = bus->map(addr, ranges, na, ns, pna);
474 if (offset != OF_BAD_ADDR)
475 break;
476 }
477 if (offset == OF_BAD_ADDR) {
Rob Herring606ad422016-06-15 08:32:18 -0500478 pr_debug("not found !\n");
Grant Likelydbbdee92010-06-08 07:48:10 -0600479 return 1;
480 }
481 memcpy(addr, ranges + na, 4 * pna);
482
483 finish:
Rob Herring606ad422016-06-15 08:32:18 -0500484 of_dump_addr("parent translation for:", addr, pna);
485 pr_debug("with offset: %llx\n", (unsigned long long)offset);
Grant Likelydbbdee92010-06-08 07:48:10 -0600486
487 /* Translate it into parent bus space */
488 return pbus->translate(addr, offset, pna);
489}
490
491/*
492 * Translate an address from the device-tree into a CPU physical address,
493 * this walks up the tree and applies the various bus mappings on the
494 * way.
495 *
496 * Note: We consider that crossing any level with #size-cells == 0 to mean
497 * that translation is impossible (that is we are not dealing with a value
498 * that can be mapped to a cpu physical address). This is not really specified
499 * that way, but this is traditionally the way IBM at least do things
Zhichang Yuan65af6182018-03-15 02:15:54 +0800500 *
501 * Whenever the translation fails, the *host pointer will be set to the
502 * device that had registered logical PIO mapping, and the return code is
503 * relative to that node.
Grant Likelydbbdee92010-06-08 07:48:10 -0600504 */
Kim Phillips47b1e682012-10-08 19:41:58 -0500505static u64 __of_translate_address(struct device_node *dev,
Maxime Ripard95835a82019-04-01 10:56:43 +0200506 struct device_node *(*get_parent)(const struct device_node *),
Zhichang Yuan65af6182018-03-15 02:15:54 +0800507 const __be32 *in_addr, const char *rprop,
508 struct device_node **host)
Grant Likelydbbdee92010-06-08 07:48:10 -0600509{
510 struct device_node *parent = NULL;
511 struct of_bus *bus, *pbus;
Kim Phillips47b1e682012-10-08 19:41:58 -0500512 __be32 addr[OF_MAX_ADDR_CELLS];
Grant Likelydbbdee92010-06-08 07:48:10 -0600513 int na, ns, pna, pns;
514 u64 result = OF_BAD_ADDR;
515
Rob Herring0d638a02017-06-01 15:50:55 -0500516 pr_debug("** translation for device %pOF **\n", dev);
Grant Likelydbbdee92010-06-08 07:48:10 -0600517
518 /* Increase refcount at current level */
519 of_node_get(dev);
520
Zhichang Yuan65af6182018-03-15 02:15:54 +0800521 *host = NULL;
Grant Likelydbbdee92010-06-08 07:48:10 -0600522 /* Get parent & match bus type */
Maxime Ripard95835a82019-04-01 10:56:43 +0200523 parent = get_parent(dev);
Grant Likelydbbdee92010-06-08 07:48:10 -0600524 if (parent == NULL)
525 goto bail;
526 bus = of_match_bus(parent);
527
Andrew Murray59f5ca42012-12-13 10:11:23 +0000528 /* Count address cells & copy address locally */
Grant Likelydbbdee92010-06-08 07:48:10 -0600529 bus->count_cells(dev, &na, &ns);
530 if (!OF_CHECK_COUNTS(na, ns)) {
Rob Herring0d638a02017-06-01 15:50:55 -0500531 pr_debug("Bad cell count for %pOF\n", dev);
Grant Likelydbbdee92010-06-08 07:48:10 -0600532 goto bail;
533 }
534 memcpy(addr, in_addr, na * 4);
535
Rob Herring0d638a02017-06-01 15:50:55 -0500536 pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",
537 bus->name, na, ns, parent);
Rob Herring606ad422016-06-15 08:32:18 -0500538 of_dump_addr("translating address:", addr, na);
Grant Likelydbbdee92010-06-08 07:48:10 -0600539
540 /* Translate */
541 for (;;) {
Zhichang Yuan65af6182018-03-15 02:15:54 +0800542 struct logic_pio_hwaddr *iorange;
543
Grant Likelydbbdee92010-06-08 07:48:10 -0600544 /* Switch to parent bus */
545 of_node_put(dev);
546 dev = parent;
Maxime Ripard95835a82019-04-01 10:56:43 +0200547 parent = get_parent(dev);
Grant Likelydbbdee92010-06-08 07:48:10 -0600548
549 /* If root, we have finished */
550 if (parent == NULL) {
Rob Herring606ad422016-06-15 08:32:18 -0500551 pr_debug("reached root node\n");
Grant Likelydbbdee92010-06-08 07:48:10 -0600552 result = of_read_number(addr, na);
553 break;
554 }
555
Zhichang Yuan65af6182018-03-15 02:15:54 +0800556 /*
557 * For indirectIO device which has no ranges property, get
558 * the address from reg directly.
559 */
560 iorange = find_io_range_by_fwnode(&dev->fwnode);
561 if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) {
562 result = of_read_number(addr + 1, na - 1);
563 pr_debug("indirectIO matched(%pOF) 0x%llx\n",
564 dev, result);
565 *host = of_node_get(dev);
566 break;
567 }
568
Grant Likelydbbdee92010-06-08 07:48:10 -0600569 /* Get new parent bus and counts */
570 pbus = of_match_bus(parent);
571 pbus->count_cells(dev, &pna, &pns);
572 if (!OF_CHECK_COUNTS(pna, pns)) {
Rob Herring0d638a02017-06-01 15:50:55 -0500573 pr_err("Bad cell count for %pOF\n", dev);
Grant Likelydbbdee92010-06-08 07:48:10 -0600574 break;
575 }
576
Rob Herring0d638a02017-06-01 15:50:55 -0500577 pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
578 pbus->name, pna, pns, parent);
Grant Likelydbbdee92010-06-08 07:48:10 -0600579
580 /* Apply bus translation */
581 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
582 break;
583
584 /* Complete the move up one level */
585 na = pna;
586 ns = pns;
587 bus = pbus;
588
Rob Herring606ad422016-06-15 08:32:18 -0500589 of_dump_addr("one level translation:", addr, na);
Grant Likelydbbdee92010-06-08 07:48:10 -0600590 }
591 bail:
592 of_node_put(parent);
593 of_node_put(dev);
594
595 return result;
596}
597
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100598u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
Grant Likelydbbdee92010-06-08 07:48:10 -0600599{
Zhichang Yuan65af6182018-03-15 02:15:54 +0800600 struct device_node *host;
601 u64 ret;
602
Maxime Ripard95835a82019-04-01 10:56:43 +0200603 ret = __of_translate_address(dev, of_get_parent,
604 in_addr, "ranges", &host);
Zhichang Yuan65af6182018-03-15 02:15:54 +0800605 if (host) {
606 of_node_put(host);
607 return OF_BAD_ADDR;
608 }
609
610 return ret;
Grant Likelydbbdee92010-06-08 07:48:10 -0600611}
612EXPORT_SYMBOL(of_translate_address);
613
Maxime Ripardf83a6e52019-04-01 10:56:44 +0200614static struct device_node *__of_get_dma_parent(const struct device_node *np)
615{
616 struct of_phandle_args args;
617 int ret, index;
618
619 index = of_property_match_string(np, "interconnect-names", "dma-mem");
620 if (index < 0)
621 return of_get_parent(np);
622
623 ret = of_parse_phandle_with_args(np, "interconnects",
624 "#interconnect-cells",
625 index, &args);
626 if (ret < 0)
627 return of_get_parent(np);
628
629 return of_node_get(args.np);
630}
631
Robin Murphy862ab552019-07-03 18:23:01 +0100632static struct device_node *of_get_next_dma_parent(struct device_node *np)
633{
634 struct device_node *parent;
635
636 parent = __of_get_dma_parent(np);
637 of_node_put(np);
638
639 return parent;
640}
641
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100642u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
Grant Likelydbbdee92010-06-08 07:48:10 -0600643{
Zhichang Yuan65af6182018-03-15 02:15:54 +0800644 struct device_node *host;
645 u64 ret;
646
Maxime Ripardf83a6e52019-04-01 10:56:44 +0200647 ret = __of_translate_address(dev, __of_get_dma_parent,
Maxime Ripard95835a82019-04-01 10:56:43 +0200648 in_addr, "dma-ranges", &host);
Zhichang Yuan65af6182018-03-15 02:15:54 +0800649
650 if (host) {
651 of_node_put(host);
652 return OF_BAD_ADDR;
653 }
654
655 return ret;
Grant Likelydbbdee92010-06-08 07:48:10 -0600656}
657EXPORT_SYMBOL(of_translate_dma_address);
658
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100659const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,
Grant Likelydbbdee92010-06-08 07:48:10 -0600660 unsigned int *flags)
661{
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100662 const __be32 *prop;
Grant Likelydbbdee92010-06-08 07:48:10 -0600663 unsigned int psize;
664 struct device_node *parent;
665 struct of_bus *bus;
666 int onesize, i, na, ns;
667
668 /* Get parent & match bus type */
669 parent = of_get_parent(dev);
670 if (parent == NULL)
671 return NULL;
672 bus = of_match_bus(parent);
673 bus->count_cells(dev, &na, &ns);
674 of_node_put(parent);
Stephen Warren5d61b162012-07-25 17:34:37 -0600675 if (!OF_CHECK_ADDR_COUNT(na))
Grant Likelydbbdee92010-06-08 07:48:10 -0600676 return NULL;
677
678 /* Get "reg" or "assigned-addresses" property */
679 prop = of_get_property(dev, bus->addresses, &psize);
680 if (prop == NULL)
681 return NULL;
682 psize /= 4;
683
684 onesize = na + ns;
685 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
686 if (i == index) {
687 if (size)
688 *size = of_read_number(prop + na, ns);
689 if (flags)
690 *flags = bus->get_flags(prop);
691 return prop;
692 }
693 return NULL;
694}
695EXPORT_SYMBOL(of_get_address);
696
Rob Herring67ccd2b2020-02-06 14:34:54 +0000697static int parser_init(struct of_pci_range_parser *parser,
698 struct device_node *node, const char *name)
699{
Rob Herring67ccd2b2020-02-06 14:34:54 +0000700 int rlen;
701
702 parser->node = node;
703 parser->pna = of_n_addr_cells(node);
Rob Herringbc5e5222020-02-06 14:01:05 +0000704 parser->na = of_bus_n_addr_cells(node);
705 parser->ns = of_bus_n_size_cells(node);
Rob Herring67ccd2b2020-02-06 14:34:54 +0000706 parser->dma = !strcmp(name, "dma-ranges");
Jiaxun Yang2f965932020-07-28 23:36:55 +0800707 parser->bus = of_match_bus(node);
Rob Herring67ccd2b2020-02-06 14:34:54 +0000708
709 parser->range = of_get_property(node, name, &rlen);
710 if (parser->range == NULL)
711 return -ENOENT;
712
713 parser->end = parser->range + rlen / sizeof(__be32);
714
715 return 0;
716}
717
718int of_pci_range_parser_init(struct of_pci_range_parser *parser,
719 struct device_node *node)
720{
721 return parser_init(parser, node, "ranges");
722}
723EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
724
725int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
726 struct device_node *node)
727{
728 return parser_init(parser, node, "dma-ranges");
729}
730EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init);
Rob Herringbc5e5222020-02-06 14:01:05 +0000731#define of_dma_range_parser_init of_pci_dma_range_parser_init
Rob Herring67ccd2b2020-02-06 14:34:54 +0000732
733struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
734 struct of_pci_range *range)
735{
Rob Herringbc5e5222020-02-06 14:01:05 +0000736 int na = parser->na;
737 int ns = parser->ns;
738 int np = parser->pna + na + ns;
Jiaxun Yang2f965932020-07-28 23:36:55 +0800739 int busflag_na = 0;
Rob Herring67ccd2b2020-02-06 14:34:54 +0000740
741 if (!range)
742 return NULL;
743
Rob Herringbc5e5222020-02-06 14:01:05 +0000744 if (!parser->range || parser->range + np > parser->end)
Rob Herring67ccd2b2020-02-06 14:34:54 +0000745 return NULL;
746
Jiaxun Yang2f965932020-07-28 23:36:55 +0800747 range->flags = parser->bus->get_flags(parser->range);
Rob Herringbc5e5222020-02-06 14:01:05 +0000748
Jiaxun Yang2f965932020-07-28 23:36:55 +0800749 /* A extra cell for resource flags */
750 if (parser->bus->has_flags)
751 busflag_na = 1;
752
753 range->bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
Rob Herringbc5e5222020-02-06 14:01:05 +0000754
Rob Herring67ccd2b2020-02-06 14:34:54 +0000755 if (parser->dma)
756 range->cpu_addr = of_translate_dma_address(parser->node,
757 parser->range + na);
758 else
759 range->cpu_addr = of_translate_address(parser->node,
760 parser->range + na);
761 range->size = of_read_number(parser->range + parser->pna + na, ns);
762
Rob Herringbc5e5222020-02-06 14:01:05 +0000763 parser->range += np;
Rob Herring67ccd2b2020-02-06 14:34:54 +0000764
765 /* Now consume following elements while they are contiguous */
Rob Herringbc5e5222020-02-06 14:01:05 +0000766 while (parser->range + np <= parser->end) {
767 u32 flags = 0;
Jiaxun Yang2f965932020-07-28 23:36:55 +0800768 u64 bus_addr, cpu_addr, size;
Rob Herring67ccd2b2020-02-06 14:34:54 +0000769
Jiaxun Yang2f965932020-07-28 23:36:55 +0800770 flags = parser->bus->get_flags(parser->range);
771 bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
Rob Herring67ccd2b2020-02-06 14:34:54 +0000772 if (parser->dma)
773 cpu_addr = of_translate_dma_address(parser->node,
774 parser->range + na);
775 else
776 cpu_addr = of_translate_address(parser->node,
777 parser->range + na);
778 size = of_read_number(parser->range + parser->pna + na, ns);
779
780 if (flags != range->flags)
781 break;
Jiaxun Yang2f965932020-07-28 23:36:55 +0800782 if (bus_addr != range->bus_addr + range->size ||
Rob Herring67ccd2b2020-02-06 14:34:54 +0000783 cpu_addr != range->cpu_addr + range->size)
784 break;
785
786 range->size += size;
Rob Herringbc5e5222020-02-06 14:01:05 +0000787 parser->range += np;
Rob Herring67ccd2b2020-02-06 14:34:54 +0000788 }
789
790 return range;
791}
792EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
793
Zhichang Yuan65af6182018-03-15 02:15:54 +0800794static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr,
795 u64 size)
796{
797 u64 taddr;
798 unsigned long port;
799 struct device_node *host;
800
Maxime Ripard95835a82019-04-01 10:56:43 +0200801 taddr = __of_translate_address(dev, of_get_parent,
802 in_addr, "ranges", &host);
Zhichang Yuan65af6182018-03-15 02:15:54 +0800803 if (host) {
804 /* host-specific port access */
805 port = logic_pio_trans_hwaddr(&host->fwnode, taddr, size);
806 of_node_put(host);
807 } else {
808 /* memory-mapped I/O range */
809 port = pci_address_to_pio(taddr);
810 }
811
812 if (port == (unsigned long)-1)
813 return OF_BAD_ADDR;
814
815 return port;
816}
817
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100818static int __of_address_to_resource(struct device_node *dev,
819 const __be32 *addrp, u64 size, unsigned int flags,
Benoit Cousson35f3da32011-12-05 15:23:55 +0100820 const char *name, struct resource *r)
Grant Likely1f5bef32010-06-08 07:48:09 -0600821{
822 u64 taddr;
823
Zhichang Yuan65af6182018-03-15 02:15:54 +0800824 if (flags & IORESOURCE_MEM)
825 taddr = of_translate_address(dev, addrp);
826 else if (flags & IORESOURCE_IO)
827 taddr = of_translate_ioport(dev, addrp, size);
828 else
Grant Likely1f5bef32010-06-08 07:48:09 -0600829 return -EINVAL;
Zhichang Yuan65af6182018-03-15 02:15:54 +0800830
Grant Likely1f5bef32010-06-08 07:48:09 -0600831 if (taddr == OF_BAD_ADDR)
832 return -EINVAL;
833 memset(r, 0, sizeof(struct resource));
Zhichang Yuan65af6182018-03-15 02:15:54 +0800834
835 r->start = taddr;
836 r->end = taddr + size - 1;
Grant Likely1f5bef32010-06-08 07:48:09 -0600837 r->flags = flags;
Benoit Cousson35f3da32011-12-05 15:23:55 +0100838 r->name = name ? name : dev->full_name;
839
Grant Likely1f5bef32010-06-08 07:48:09 -0600840 return 0;
841}
842
843/**
844 * of_address_to_resource - Translate device tree address and return as resource
845 *
846 * Note that if your address is a PIO address, the conversion will fail if
847 * the physical address can't be internally converted to an IO token with
Frank Rowand7602f422017-06-20 16:50:11 -0700848 * pci_address_to_pio(), that is because it's either called too early or it
Grant Likely1f5bef32010-06-08 07:48:09 -0600849 * can't be matched to any host bridge IO space
850 */
851int of_address_to_resource(struct device_node *dev, int index,
852 struct resource *r)
853{
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100854 const __be32 *addrp;
Grant Likely1f5bef32010-06-08 07:48:09 -0600855 u64 size;
856 unsigned int flags;
Benoit Cousson35f3da32011-12-05 15:23:55 +0100857 const char *name = NULL;
Grant Likely1f5bef32010-06-08 07:48:09 -0600858
859 addrp = of_get_address(dev, index, &size, &flags);
860 if (addrp == NULL)
861 return -EINVAL;
Benoit Cousson35f3da32011-12-05 15:23:55 +0100862
863 /* Get optional "reg-names" property to add a name to a resource */
864 of_property_read_string_index(dev, "reg-names", index, &name);
865
866 return __of_address_to_resource(dev, addrp, size, flags, name, r);
Grant Likely1f5bef32010-06-08 07:48:09 -0600867}
868EXPORT_SYMBOL_GPL(of_address_to_resource);
869
Grant Likely6b884a82010-06-08 07:48:09 -0600870/**
871 * of_iomap - Maps the memory mapped IO for a given device_node
Wenchao Hao1094d5d2020-07-02 00:24:44 +0800872 * @np: the device whose io range will be mapped
Grant Likely6b884a82010-06-08 07:48:09 -0600873 * @index: index of the io range
874 *
875 * Returns a pointer to the mapped memory
876 */
877void __iomem *of_iomap(struct device_node *np, int index)
878{
879 struct resource res;
880
881 if (of_address_to_resource(np, index, &res))
882 return NULL;
883
Felipe Balbi28c1b6d2011-07-22 22:47:21 +0300884 return ioremap(res.start, resource_size(&res));
Grant Likely6b884a82010-06-08 07:48:09 -0600885}
886EXPORT_SYMBOL(of_iomap);
Grygorii Strashko18308c92014-04-24 11:30:02 -0400887
Matthias Bruggerefd342f2014-07-18 11:36:39 +0200888/*
889 * of_io_request_and_map - Requests a resource and maps the memory mapped IO
890 * for a given device_node
891 * @device: the device whose io range will be mapped
892 * @index: index of the io range
Benjamin Herrenschmidtb01dcdd2018-06-12 10:01:07 +1000893 * @name: name "override" for the memory region request or NULL
Matthias Bruggerefd342f2014-07-18 11:36:39 +0200894 *
895 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
896 * error code on failure. Usage example:
897 *
898 * base = of_io_request_and_map(node, 0, "foo");
899 * if (IS_ERR(base))
900 * return PTR_ERR(base);
901 */
902void __iomem *of_io_request_and_map(struct device_node *np, int index,
Benjamin Herrenschmidtb01dcdd2018-06-12 10:01:07 +1000903 const char *name)
Matthias Bruggerefd342f2014-07-18 11:36:39 +0200904{
905 struct resource res;
906 void __iomem *mem;
907
908 if (of_address_to_resource(np, index, &res))
909 return IOMEM_ERR_PTR(-EINVAL);
910
Benjamin Herrenschmidtb01dcdd2018-06-12 10:01:07 +1000911 if (!name)
912 name = res.name;
Matthias Bruggerefd342f2014-07-18 11:36:39 +0200913 if (!request_mem_region(res.start, resource_size(&res), name))
914 return IOMEM_ERR_PTR(-EBUSY);
915
916 mem = ioremap(res.start, resource_size(&res));
917 if (!mem) {
918 release_mem_region(res.start, resource_size(&res));
919 return IOMEM_ERR_PTR(-ENOMEM);
920 }
921
922 return mem;
923}
924EXPORT_SYMBOL(of_io_request_and_map);
925
Grygorii Strashko18308c92014-04-24 11:30:02 -0400926/**
927 * of_dma_get_range - Get DMA range info
928 * @np: device node to get DMA range info
929 * @dma_addr: pointer to store initial DMA address of DMA range
930 * @paddr: pointer to store initial CPU address of DMA range
931 * @size: pointer to store size of DMA range
932 *
933 * Look in bottom up direction for the first "dma-ranges" property
934 * and parse it.
935 * dma-ranges format:
936 * DMA addr (dma_addr) : naddr cells
937 * CPU addr (phys_addr_t) : pna cells
938 * size : nsize cells
939 *
940 * It returns -ENODEV if "dma-ranges" property was not found
941 * for this device in DT.
942 */
943int of_dma_get_range(struct device_node *np, u64 *dma_addr, u64 *paddr, u64 *size)
944{
945 struct device_node *node = of_node_get(np);
946 const __be32 *ranges = NULL;
Rob Herring7a8b64d2020-02-06 14:02:30 +0000947 int len;
Grygorii Strashko18308c92014-04-24 11:30:02 -0400948 int ret = 0;
Robin Murphy951d4882019-07-03 18:42:20 +0100949 bool found_dma_ranges = false;
Rob Herring7a8b64d2020-02-06 14:02:30 +0000950 struct of_range_parser parser;
951 struct of_range range;
Rob Herring9d55beb2020-02-06 14:26:40 +0000952 u64 dma_start = U64_MAX, dma_end = 0, dma_offset = 0;
Grygorii Strashko18308c92014-04-24 11:30:02 -0400953
Robin Murphy951d4882019-07-03 18:42:20 +0100954 while (node) {
Grygorii Strashko18308c92014-04-24 11:30:02 -0400955 ranges = of_get_property(node, "dma-ranges", &len);
956
957 /* Ignore empty ranges, they imply no translation required */
958 if (ranges && len > 0)
959 break;
960
Robin Murphy951d4882019-07-03 18:42:20 +0100961 /* Once we find 'dma-ranges', then a missing one is an error */
962 if (found_dma_ranges && !ranges) {
963 ret = -ENODEV;
964 goto out;
965 }
966 found_dma_ranges = true;
967
968 node = of_get_next_dma_parent(node);
Grygorii Strashko18308c92014-04-24 11:30:02 -0400969 }
970
Robin Murphy951d4882019-07-03 18:42:20 +0100971 if (!node || !ranges) {
Rob Herring0d638a02017-06-01 15:50:55 -0500972 pr_debug("no dma-ranges found for node(%pOF)\n", np);
Grygorii Strashko18308c92014-04-24 11:30:02 -0400973 ret = -ENODEV;
974 goto out;
975 }
976
Rob Herring7a8b64d2020-02-06 14:02:30 +0000977 of_dma_range_parser_init(&parser, node);
978
979 for_each_of_range(&parser, &range) {
980 pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
981 range.bus_addr, range.cpu_addr, range.size);
982
Rob Herring9d55beb2020-02-06 14:26:40 +0000983 if (dma_offset && range.cpu_addr - range.bus_addr != dma_offset) {
984 pr_warn("Can't handle multiple dma-ranges with different offsets on node(%pOF)\n", node);
985 /* Don't error out as we'd break some existing DTs */
986 continue;
987 }
Colin Ian Kingf49c7fa2020-08-17 12:32:08 +0100988 if (range.cpu_addr == OF_BAD_ADDR) {
989 pr_err("translation of DMA address(%llx) to CPU address failed node(%pOF)\n",
990 range.bus_addr, node);
991 continue;
992 }
Rob Herring9d55beb2020-02-06 14:26:40 +0000993 dma_offset = range.cpu_addr - range.bus_addr;
Rob Herring7a8b64d2020-02-06 14:02:30 +0000994
Rob Herring9d55beb2020-02-06 14:26:40 +0000995 /* Take lower and upper limits */
996 if (range.bus_addr < dma_start)
997 dma_start = range.bus_addr;
998 if (range.bus_addr + range.size > dma_end)
999 dma_end = range.bus_addr + range.size;
1000 }
1001
1002 if (dma_start >= dma_end) {
1003 ret = -EINVAL;
1004 pr_debug("Invalid DMA ranges configuration on node(%pOF)\n",
1005 node);
Robin Murphy951d4882019-07-03 18:42:20 +01001006 goto out;
1007 }
Grygorii Strashko18308c92014-04-24 11:30:02 -04001008
Rob Herring9d55beb2020-02-06 14:26:40 +00001009 *dma_addr = dma_start;
1010 *size = dma_end - dma_start;
1011 *paddr = dma_start + dma_offset;
1012
1013 pr_debug("final: dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
1014 *dma_addr, *paddr, *size);
Grygorii Strashko18308c92014-04-24 11:30:02 -04001015
1016out:
1017 of_node_put(node);
1018
1019 return ret;
1020}
Santosh Shilimkar92ea6372014-04-24 11:30:03 -04001021
1022/**
1023 * of_dma_is_coherent - Check if device is coherent
1024 * @np: device node
1025 *
1026 * It returns true if "dma-coherent" property was found
Michael Ellermandabf6b32020-01-26 22:52:47 +11001027 * for this device in the DT, or if DMA is coherent by
1028 * default for OF devices on the current platform.
Santosh Shilimkar92ea6372014-04-24 11:30:03 -04001029 */
1030bool of_dma_is_coherent(struct device_node *np)
1031{
1032 struct device_node *node = of_node_get(np);
1033
Michael Ellermandabf6b32020-01-26 22:52:47 +11001034 if (IS_ENABLED(CONFIG_OF_DMA_DEFAULT_COHERENT))
1035 return true;
1036
Santosh Shilimkar92ea6372014-04-24 11:30:03 -04001037 while (node) {
1038 if (of_property_read_bool(node, "dma-coherent")) {
1039 of_node_put(node);
1040 return true;
1041 }
Robin Murphyc60bf3e2019-07-03 14:47:31 +01001042 node = of_get_next_dma_parent(node);
Santosh Shilimkar92ea6372014-04-24 11:30:03 -04001043 }
1044 of_node_put(node);
1045 return false;
1046}
Linus Torvaldseb3d3ec2014-06-05 15:57:04 -07001047EXPORT_SYMBOL_GPL(of_dma_is_coherent);