blob: 8eea3f6e29a44183e35c685dacc62e2055bbc184 [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);
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +010052 unsigned int (*get_flags)(const __be32 *addr);
Grant Likelydbbdee92010-06-08 07:48:10 -060053};
54
55/*
56 * Default translator (generic bus)
57 */
58
59static void of_bus_default_count_cells(struct device_node *dev,
60 int *addrc, int *sizec)
61{
62 if (addrc)
63 *addrc = of_n_addr_cells(dev);
64 if (sizec)
65 *sizec = of_n_size_cells(dev);
66}
67
Kim Phillips47b1e682012-10-08 19:41:58 -050068static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
Grant Likelydbbdee92010-06-08 07:48:10 -060069 int na, int ns, int pna)
70{
71 u64 cp, s, da;
72
73 cp = of_read_number(range, na);
74 s = of_read_number(range + na + pna, ns);
75 da = of_read_number(addr, na);
76
Rob Herring606ad422016-06-15 08:32:18 -050077 pr_debug("default map, cp=%llx, s=%llx, da=%llx\n",
Grant Likelydbbdee92010-06-08 07:48:10 -060078 (unsigned long long)cp, (unsigned long long)s,
79 (unsigned long long)da);
80
81 if (da < cp || da >= (cp + s))
82 return OF_BAD_ADDR;
83 return da - cp;
84}
85
Kim Phillips47b1e682012-10-08 19:41:58 -050086static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
Grant Likelydbbdee92010-06-08 07:48:10 -060087{
88 u64 a = of_read_number(addr, na);
89 memset(addr, 0, na * 4);
90 a += offset;
91 if (na > 1)
Grant Likely154063a92010-06-08 07:48:11 -060092 addr[na - 2] = cpu_to_be32(a >> 32);
93 addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
Grant Likelydbbdee92010-06-08 07:48:10 -060094
95 return 0;
96}
97
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +010098static unsigned int of_bus_default_get_flags(const __be32 *addr)
Grant Likelydbbdee92010-06-08 07:48:10 -060099{
100 return IORESOURCE_MEM;
101}
102
Rob Herring67ccd2b2020-02-06 14:34:54 +0000103static unsigned int of_bus_pci_get_flags(const __be32 *addr)
104{
105 unsigned int flags = 0;
106 u32 w = be32_to_cpup(addr);
107
108 if (!IS_ENABLED(CONFIG_PCI))
109 return 0;
110
111 switch((w >> 24) & 0x03) {
112 case 0x01:
113 flags |= IORESOURCE_IO;
114 break;
115 case 0x02: /* 32 bits */
116 case 0x03: /* 64 bits */
117 flags |= IORESOURCE_MEM;
118 break;
119 }
120 if (w & 0x40000000)
121 flags |= IORESOURCE_PREFETCH;
122 return flags;
123}
124
Rob Herring4670d612018-01-17 17:36:39 -0600125#ifdef CONFIG_PCI
Grant Likelydbbdee92010-06-08 07:48:10 -0600126/*
127 * PCI bus specific translator
128 */
129
130static int of_bus_pci_match(struct device_node *np)
131{
Benjamin Herrenschmidt6dd18e42013-07-03 16:01:10 +1000132 /*
Kleber Sacilotto de Souza14e2abb2014-02-03 13:31:03 -0200133 * "pciex" is PCI Express
Benjamin Herrenschmidt6dd18e42013-07-03 16:01:10 +1000134 * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
135 * "ht" is hypertransport
136 */
Rob Herringe8b1dee22018-08-29 08:36:12 -0500137 return of_node_is_type(np, "pci") || of_node_is_type(np, "pciex") ||
138 of_node_is_type(np, "vci") || of_node_is_type(np, "ht");
Grant Likelydbbdee92010-06-08 07:48:10 -0600139}
140
141static void of_bus_pci_count_cells(struct device_node *np,
142 int *addrc, int *sizec)
143{
144 if (addrc)
145 *addrc = 3;
146 if (sizec)
147 *sizec = 2;
148}
149
Kim Phillips47b1e682012-10-08 19:41:58 -0500150static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100151 int pna)
Grant Likelydbbdee92010-06-08 07:48:10 -0600152{
153 u64 cp, s, da;
154 unsigned int af, rf;
155
156 af = of_bus_pci_get_flags(addr);
157 rf = of_bus_pci_get_flags(range);
158
159 /* Check address type match */
160 if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
161 return OF_BAD_ADDR;
162
163 /* Read address values, skipping high cell */
164 cp = of_read_number(range + 1, na - 1);
165 s = of_read_number(range + na + pna, ns);
166 da = of_read_number(addr + 1, na - 1);
167
Rob Herring606ad422016-06-15 08:32:18 -0500168 pr_debug("PCI map, cp=%llx, s=%llx, da=%llx\n",
Grant Likelydbbdee92010-06-08 07:48:10 -0600169 (unsigned long long)cp, (unsigned long long)s,
170 (unsigned long long)da);
171
172 if (da < cp || da >= (cp + s))
173 return OF_BAD_ADDR;
174 return da - cp;
175}
176
Kim Phillips47b1e682012-10-08 19:41:58 -0500177static int of_bus_pci_translate(__be32 *addr, u64 offset, int na)
Grant Likelydbbdee92010-06-08 07:48:10 -0600178{
179 return of_bus_default_translate(addr + 1, offset, na - 1);
180}
181
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100182const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
Grant Likelydbbdee92010-06-08 07:48:10 -0600183 unsigned int *flags)
184{
Jeremy Kerra9fadee2010-10-10 21:24:10 -0600185 const __be32 *prop;
Grant Likelydbbdee92010-06-08 07:48:10 -0600186 unsigned int psize;
187 struct device_node *parent;
188 struct of_bus *bus;
189 int onesize, i, na, ns;
190
191 /* Get parent & match bus type */
192 parent = of_get_parent(dev);
193 if (parent == NULL)
194 return NULL;
195 bus = of_match_bus(parent);
196 if (strcmp(bus->name, "pci")) {
197 of_node_put(parent);
198 return NULL;
199 }
200 bus->count_cells(dev, &na, &ns);
201 of_node_put(parent);
Stephen Warren5d61b162012-07-25 17:34:37 -0600202 if (!OF_CHECK_ADDR_COUNT(na))
Grant Likelydbbdee92010-06-08 07:48:10 -0600203 return NULL;
204
205 /* Get "reg" or "assigned-addresses" property */
206 prop = of_get_property(dev, bus->addresses, &psize);
207 if (prop == NULL)
208 return NULL;
209 psize /= 4;
210
211 onesize = na + ns;
Grant Likely154063a92010-06-08 07:48:11 -0600212 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
213 u32 val = be32_to_cpu(prop[0]);
214 if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
Grant Likelydbbdee92010-06-08 07:48:10 -0600215 if (size)
216 *size = of_read_number(prop + na, ns);
217 if (flags)
218 *flags = bus->get_flags(prop);
219 return prop;
220 }
Grant Likely154063a92010-06-08 07:48:11 -0600221 }
Grant Likelydbbdee92010-06-08 07:48:10 -0600222 return NULL;
223}
224EXPORT_SYMBOL(of_get_pci_address);
225
226int of_pci_address_to_resource(struct device_node *dev, int bar,
227 struct resource *r)
228{
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100229 const __be32 *addrp;
Grant Likelydbbdee92010-06-08 07:48:10 -0600230 u64 size;
231 unsigned int flags;
232
233 addrp = of_get_pci_address(dev, bar, &size, &flags);
234 if (addrp == NULL)
235 return -EINVAL;
Benoit Cousson35f3da32011-12-05 15:23:55 +0100236 return __of_address_to_resource(dev, addrp, size, flags, NULL, r);
Grant Likelydbbdee92010-06-08 07:48:10 -0600237}
238EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
Andrew Murray29b635c2013-05-16 17:55:17 +0200239
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100240/*
241 * of_pci_range_to_resource - Create a resource from an of_pci_range
242 * @range: the PCI range that describes the resource
243 * @np: device node where the range belongs to
244 * @res: pointer to a valid resource that will be updated to
245 * reflect the values contained in the range.
246 *
247 * Returns EINVAL if the range cannot be converted to resource.
248 *
249 * Note that if the range is an IO range, the resource will be converted
250 * using pci_address_to_pio() which can fail if it is called too early or
251 * if the range cannot be matched to any host bridge IO space (our case here).
252 * To guard against that we try to register the IO range first.
253 * If that fails we know that pci_address_to_pio() will do too.
254 */
255int of_pci_range_to_resource(struct of_pci_range *range,
256 struct device_node *np, struct resource *res)
Liviu Dudau83bbde12014-09-29 15:29:24 +0100257{
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100258 int err;
Liviu Dudau83bbde12014-09-29 15:29:24 +0100259 res->flags = range->flags;
Liviu Dudau83bbde12014-09-29 15:29:24 +0100260 res->parent = res->child = res->sibling = NULL;
261 res->name = np->full_name;
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100262
263 if (res->flags & IORESOURCE_IO) {
264 unsigned long port;
Gabriele Paolonifcfaab32018-03-15 02:15:52 +0800265 err = pci_register_io_range(&np->fwnode, range->cpu_addr,
266 range->size);
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100267 if (err)
268 goto invalid_range;
269 port = pci_address_to_pio(range->cpu_addr);
270 if (port == (unsigned long)-1) {
271 err = -EINVAL;
272 goto invalid_range;
273 }
274 res->start = port;
275 } else {
Pavel Fedin4af97102015-10-08 10:24:25 +0300276 if ((sizeof(resource_size_t) < 8) &&
277 upper_32_bits(range->cpu_addr)) {
278 err = -EINVAL;
279 goto invalid_range;
280 }
281
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100282 res->start = range->cpu_addr;
283 }
284 res->end = res->start + range->size - 1;
285 return 0;
286
287invalid_range:
288 res->start = (resource_size_t)OF_BAD_ADDR;
289 res->end = (resource_size_t)OF_BAD_ADDR;
290 return err;
Liviu Dudau83bbde12014-09-29 15:29:24 +0100291}
Manikanta Maddireddybf6681e2018-01-11 11:38:02 +0530292EXPORT_SYMBOL(of_pci_range_to_resource);
Grant Likelydbbdee92010-06-08 07:48:10 -0600293#endif /* CONFIG_PCI */
294
295/*
296 * ISA bus specific translator
297 */
298
299static int of_bus_isa_match(struct device_node *np)
300{
Rob Herringb3e46d12018-08-27 08:37:06 -0500301 return of_node_name_eq(np, "isa");
Grant Likelydbbdee92010-06-08 07:48:10 -0600302}
303
304static void of_bus_isa_count_cells(struct device_node *child,
305 int *addrc, int *sizec)
306{
307 if (addrc)
308 *addrc = 2;
309 if (sizec)
310 *sizec = 1;
311}
312
Kim Phillips47b1e682012-10-08 19:41:58 -0500313static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100314 int pna)
Grant Likelydbbdee92010-06-08 07:48:10 -0600315{
316 u64 cp, s, da;
317
318 /* Check address type match */
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100319 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
Grant Likelydbbdee92010-06-08 07:48:10 -0600320 return OF_BAD_ADDR;
321
322 /* Read address values, skipping high cell */
323 cp = of_read_number(range + 1, na - 1);
324 s = of_read_number(range + na + pna, ns);
325 da = of_read_number(addr + 1, na - 1);
326
Rob Herring606ad422016-06-15 08:32:18 -0500327 pr_debug("ISA map, cp=%llx, s=%llx, da=%llx\n",
Grant Likelydbbdee92010-06-08 07:48:10 -0600328 (unsigned long long)cp, (unsigned long long)s,
329 (unsigned long long)da);
330
331 if (da < cp || da >= (cp + s))
332 return OF_BAD_ADDR;
333 return da - cp;
334}
335
Kim Phillips47b1e682012-10-08 19:41:58 -0500336static int of_bus_isa_translate(__be32 *addr, u64 offset, int na)
Grant Likelydbbdee92010-06-08 07:48:10 -0600337{
338 return of_bus_default_translate(addr + 1, offset, na - 1);
339}
340
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100341static unsigned int of_bus_isa_get_flags(const __be32 *addr)
Grant Likelydbbdee92010-06-08 07:48:10 -0600342{
343 unsigned int flags = 0;
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100344 u32 w = be32_to_cpup(addr);
Grant Likelydbbdee92010-06-08 07:48:10 -0600345
346 if (w & 1)
347 flags |= IORESOURCE_IO;
348 else
349 flags |= IORESOURCE_MEM;
350 return flags;
351}
352
353/*
354 * Array of bus specific translators
355 */
356
357static struct of_bus of_busses[] = {
Rob Herring4670d612018-01-17 17:36:39 -0600358#ifdef CONFIG_PCI
Grant Likelydbbdee92010-06-08 07:48:10 -0600359 /* PCI */
360 {
361 .name = "pci",
362 .addresses = "assigned-addresses",
363 .match = of_bus_pci_match,
364 .count_cells = of_bus_pci_count_cells,
365 .map = of_bus_pci_map,
366 .translate = of_bus_pci_translate,
367 .get_flags = of_bus_pci_get_flags,
368 },
Rob Herring4670d612018-01-17 17:36:39 -0600369#endif /* CONFIG_PCI */
Grant Likelydbbdee92010-06-08 07:48:10 -0600370 /* ISA */
371 {
372 .name = "isa",
373 .addresses = "reg",
374 .match = of_bus_isa_match,
375 .count_cells = of_bus_isa_count_cells,
376 .map = of_bus_isa_map,
377 .translate = of_bus_isa_translate,
378 .get_flags = of_bus_isa_get_flags,
379 },
380 /* Default */
381 {
382 .name = "default",
383 .addresses = "reg",
384 .match = NULL,
385 .count_cells = of_bus_default_count_cells,
386 .map = of_bus_default_map,
387 .translate = of_bus_default_translate,
388 .get_flags = of_bus_default_get_flags,
389 },
390};
391
392static struct of_bus *of_match_bus(struct device_node *np)
393{
394 int i;
395
396 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
397 if (!of_busses[i].match || of_busses[i].match(np))
398 return &of_busses[i];
399 BUG();
400 return NULL;
401}
402
Benjamin Herrenschmidt41d94892015-03-23 14:16:38 +1100403static int of_empty_ranges_quirk(struct device_node *np)
Benjamin Herrenschmidt746c9e92014-11-14 17:55:03 +1100404{
405 if (IS_ENABLED(CONFIG_PPC)) {
Benjamin Herrenschmidt41d94892015-03-23 14:16:38 +1100406 /* To save cycles, we cache the result for global "Mac" setting */
Benjamin Herrenschmidt746c9e92014-11-14 17:55:03 +1100407 static int quirk_state = -1;
408
Benjamin Herrenschmidt41d94892015-03-23 14:16:38 +1100409 /* PA-SEMI sdc DT bug */
410 if (of_device_is_compatible(np, "1682m-sdc"))
411 return true;
412
413 /* Make quirk cached */
Benjamin Herrenschmidt746c9e92014-11-14 17:55:03 +1100414 if (quirk_state < 0)
415 quirk_state =
416 of_machine_is_compatible("Power Macintosh") ||
417 of_machine_is_compatible("MacRISC");
418 return quirk_state;
419 }
420 return false;
421}
422
Grant Likelydbbdee92010-06-08 07:48:10 -0600423static int of_translate_one(struct device_node *parent, struct of_bus *bus,
Kim Phillips47b1e682012-10-08 19:41:58 -0500424 struct of_bus *pbus, __be32 *addr,
Grant Likelydbbdee92010-06-08 07:48:10 -0600425 int na, int ns, int pna, const char *rprop)
426{
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100427 const __be32 *ranges;
Grant Likelydbbdee92010-06-08 07:48:10 -0600428 unsigned int rlen;
429 int rone;
430 u64 offset = OF_BAD_ADDR;
431
Masahiro Yamadaba85edb2015-11-30 15:22:37 +0900432 /*
433 * Normally, an absence of a "ranges" property means we are
Grant Likelydbbdee92010-06-08 07:48:10 -0600434 * crossing a non-translatable boundary, and thus the addresses
Masahiro Yamadaba85edb2015-11-30 15:22:37 +0900435 * below the current cannot be converted to CPU physical ones.
Grant Likelydbbdee92010-06-08 07:48:10 -0600436 * Unfortunately, while this is very clear in the spec, it's not
437 * what Apple understood, and they do have things like /uni-n or
438 * /ht nodes with no "ranges" property and a lot of perfectly
439 * useable mapped devices below them. Thus we treat the absence of
440 * "ranges" as equivalent to an empty "ranges" property which means
441 * a 1:1 translation at that level. It's up to the caller not to try
442 * to translate addresses that aren't supposed to be translated in
443 * the first place. --BenH.
Grant Likely3930f292010-06-08 07:48:11 -0600444 *
445 * As far as we know, this damage only exists on Apple machines, so
446 * This code is only enabled on powerpc. --gcl
Rob Herring81db12e2019-09-04 11:43:30 +0100447 *
448 * This quirk also applies for 'dma-ranges' which frequently exist in
449 * child nodes without 'dma-ranges' in the parent nodes. --RobH
Grant Likelydbbdee92010-06-08 07:48:10 -0600450 */
451 ranges = of_get_property(parent, rprop, &rlen);
Rob Herring81db12e2019-09-04 11:43:30 +0100452 if (ranges == NULL && !of_empty_ranges_quirk(parent) &&
453 strcmp(rprop, "dma-ranges")) {
Rob Herring606ad422016-06-15 08:32:18 -0500454 pr_debug("no ranges; cannot translate\n");
Grant Likely3930f292010-06-08 07:48:11 -0600455 return 1;
456 }
Grant Likelydbbdee92010-06-08 07:48:10 -0600457 if (ranges == NULL || rlen == 0) {
458 offset = of_read_number(addr, na);
459 memset(addr, 0, pna * 4);
Rob Herring606ad422016-06-15 08:32:18 -0500460 pr_debug("empty ranges; 1:1 translation\n");
Grant Likelydbbdee92010-06-08 07:48:10 -0600461 goto finish;
462 }
463
Rob Herring606ad422016-06-15 08:32:18 -0500464 pr_debug("walking ranges...\n");
Grant Likelydbbdee92010-06-08 07:48:10 -0600465
466 /* Now walk through the ranges */
467 rlen /= 4;
468 rone = na + pna + ns;
469 for (; rlen >= rone; rlen -= rone, ranges += rone) {
470 offset = bus->map(addr, ranges, na, ns, pna);
471 if (offset != OF_BAD_ADDR)
472 break;
473 }
474 if (offset == OF_BAD_ADDR) {
Rob Herring606ad422016-06-15 08:32:18 -0500475 pr_debug("not found !\n");
Grant Likelydbbdee92010-06-08 07:48:10 -0600476 return 1;
477 }
478 memcpy(addr, ranges + na, 4 * pna);
479
480 finish:
Rob Herring606ad422016-06-15 08:32:18 -0500481 of_dump_addr("parent translation for:", addr, pna);
482 pr_debug("with offset: %llx\n", (unsigned long long)offset);
Grant Likelydbbdee92010-06-08 07:48:10 -0600483
484 /* Translate it into parent bus space */
485 return pbus->translate(addr, offset, pna);
486}
487
488/*
489 * Translate an address from the device-tree into a CPU physical address,
490 * this walks up the tree and applies the various bus mappings on the
491 * way.
492 *
493 * Note: We consider that crossing any level with #size-cells == 0 to mean
494 * that translation is impossible (that is we are not dealing with a value
495 * that can be mapped to a cpu physical address). This is not really specified
496 * that way, but this is traditionally the way IBM at least do things
Zhichang Yuan65af6182018-03-15 02:15:54 +0800497 *
498 * Whenever the translation fails, the *host pointer will be set to the
499 * device that had registered logical PIO mapping, and the return code is
500 * relative to that node.
Grant Likelydbbdee92010-06-08 07:48:10 -0600501 */
Kim Phillips47b1e682012-10-08 19:41:58 -0500502static u64 __of_translate_address(struct device_node *dev,
Maxime Ripard95835a82019-04-01 10:56:43 +0200503 struct device_node *(*get_parent)(const struct device_node *),
Zhichang Yuan65af6182018-03-15 02:15:54 +0800504 const __be32 *in_addr, const char *rprop,
505 struct device_node **host)
Grant Likelydbbdee92010-06-08 07:48:10 -0600506{
507 struct device_node *parent = NULL;
508 struct of_bus *bus, *pbus;
Kim Phillips47b1e682012-10-08 19:41:58 -0500509 __be32 addr[OF_MAX_ADDR_CELLS];
Grant Likelydbbdee92010-06-08 07:48:10 -0600510 int na, ns, pna, pns;
511 u64 result = OF_BAD_ADDR;
512
Rob Herring0d638a02017-06-01 15:50:55 -0500513 pr_debug("** translation for device %pOF **\n", dev);
Grant Likelydbbdee92010-06-08 07:48:10 -0600514
515 /* Increase refcount at current level */
516 of_node_get(dev);
517
Zhichang Yuan65af6182018-03-15 02:15:54 +0800518 *host = NULL;
Grant Likelydbbdee92010-06-08 07:48:10 -0600519 /* Get parent & match bus type */
Maxime Ripard95835a82019-04-01 10:56:43 +0200520 parent = get_parent(dev);
Grant Likelydbbdee92010-06-08 07:48:10 -0600521 if (parent == NULL)
522 goto bail;
523 bus = of_match_bus(parent);
524
Andrew Murray59f5ca42012-12-13 10:11:23 +0000525 /* Count address cells & copy address locally */
Grant Likelydbbdee92010-06-08 07:48:10 -0600526 bus->count_cells(dev, &na, &ns);
527 if (!OF_CHECK_COUNTS(na, ns)) {
Rob Herring0d638a02017-06-01 15:50:55 -0500528 pr_debug("Bad cell count for %pOF\n", dev);
Grant Likelydbbdee92010-06-08 07:48:10 -0600529 goto bail;
530 }
531 memcpy(addr, in_addr, na * 4);
532
Rob Herring0d638a02017-06-01 15:50:55 -0500533 pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",
534 bus->name, na, ns, parent);
Rob Herring606ad422016-06-15 08:32:18 -0500535 of_dump_addr("translating address:", addr, na);
Grant Likelydbbdee92010-06-08 07:48:10 -0600536
537 /* Translate */
538 for (;;) {
Zhichang Yuan65af6182018-03-15 02:15:54 +0800539 struct logic_pio_hwaddr *iorange;
540
Grant Likelydbbdee92010-06-08 07:48:10 -0600541 /* Switch to parent bus */
542 of_node_put(dev);
543 dev = parent;
Maxime Ripard95835a82019-04-01 10:56:43 +0200544 parent = get_parent(dev);
Grant Likelydbbdee92010-06-08 07:48:10 -0600545
546 /* If root, we have finished */
547 if (parent == NULL) {
Rob Herring606ad422016-06-15 08:32:18 -0500548 pr_debug("reached root node\n");
Grant Likelydbbdee92010-06-08 07:48:10 -0600549 result = of_read_number(addr, na);
550 break;
551 }
552
Zhichang Yuan65af6182018-03-15 02:15:54 +0800553 /*
554 * For indirectIO device which has no ranges property, get
555 * the address from reg directly.
556 */
557 iorange = find_io_range_by_fwnode(&dev->fwnode);
558 if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) {
559 result = of_read_number(addr + 1, na - 1);
560 pr_debug("indirectIO matched(%pOF) 0x%llx\n",
561 dev, result);
562 *host = of_node_get(dev);
563 break;
564 }
565
Grant Likelydbbdee92010-06-08 07:48:10 -0600566 /* Get new parent bus and counts */
567 pbus = of_match_bus(parent);
568 pbus->count_cells(dev, &pna, &pns);
569 if (!OF_CHECK_COUNTS(pna, pns)) {
Rob Herring0d638a02017-06-01 15:50:55 -0500570 pr_err("Bad cell count for %pOF\n", dev);
Grant Likelydbbdee92010-06-08 07:48:10 -0600571 break;
572 }
573
Rob Herring0d638a02017-06-01 15:50:55 -0500574 pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
575 pbus->name, pna, pns, parent);
Grant Likelydbbdee92010-06-08 07:48:10 -0600576
577 /* Apply bus translation */
578 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
579 break;
580
581 /* Complete the move up one level */
582 na = pna;
583 ns = pns;
584 bus = pbus;
585
Rob Herring606ad422016-06-15 08:32:18 -0500586 of_dump_addr("one level translation:", addr, na);
Grant Likelydbbdee92010-06-08 07:48:10 -0600587 }
588 bail:
589 of_node_put(parent);
590 of_node_put(dev);
591
592 return result;
593}
594
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100595u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
Grant Likelydbbdee92010-06-08 07:48:10 -0600596{
Zhichang Yuan65af6182018-03-15 02:15:54 +0800597 struct device_node *host;
598 u64 ret;
599
Maxime Ripard95835a82019-04-01 10:56:43 +0200600 ret = __of_translate_address(dev, of_get_parent,
601 in_addr, "ranges", &host);
Zhichang Yuan65af6182018-03-15 02:15:54 +0800602 if (host) {
603 of_node_put(host);
604 return OF_BAD_ADDR;
605 }
606
607 return ret;
Grant Likelydbbdee92010-06-08 07:48:10 -0600608}
609EXPORT_SYMBOL(of_translate_address);
610
Maxime Ripardf83a6e52019-04-01 10:56:44 +0200611static struct device_node *__of_get_dma_parent(const struct device_node *np)
612{
613 struct of_phandle_args args;
614 int ret, index;
615
616 index = of_property_match_string(np, "interconnect-names", "dma-mem");
617 if (index < 0)
618 return of_get_parent(np);
619
620 ret = of_parse_phandle_with_args(np, "interconnects",
621 "#interconnect-cells",
622 index, &args);
623 if (ret < 0)
624 return of_get_parent(np);
625
626 return of_node_get(args.np);
627}
628
Robin Murphy862ab552019-07-03 18:23:01 +0100629static struct device_node *of_get_next_dma_parent(struct device_node *np)
630{
631 struct device_node *parent;
632
633 parent = __of_get_dma_parent(np);
634 of_node_put(np);
635
636 return parent;
637}
638
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100639u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
Grant Likelydbbdee92010-06-08 07:48:10 -0600640{
Zhichang Yuan65af6182018-03-15 02:15:54 +0800641 struct device_node *host;
642 u64 ret;
643
Maxime Ripardf83a6e52019-04-01 10:56:44 +0200644 ret = __of_translate_address(dev, __of_get_dma_parent,
Maxime Ripard95835a82019-04-01 10:56:43 +0200645 in_addr, "dma-ranges", &host);
Zhichang Yuan65af6182018-03-15 02:15:54 +0800646
647 if (host) {
648 of_node_put(host);
649 return OF_BAD_ADDR;
650 }
651
652 return ret;
Grant Likelydbbdee92010-06-08 07:48:10 -0600653}
654EXPORT_SYMBOL(of_translate_dma_address);
655
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100656const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,
Grant Likelydbbdee92010-06-08 07:48:10 -0600657 unsigned int *flags)
658{
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100659 const __be32 *prop;
Grant Likelydbbdee92010-06-08 07:48:10 -0600660 unsigned int psize;
661 struct device_node *parent;
662 struct of_bus *bus;
663 int onesize, i, na, ns;
664
665 /* Get parent & match bus type */
666 parent = of_get_parent(dev);
667 if (parent == NULL)
668 return NULL;
669 bus = of_match_bus(parent);
670 bus->count_cells(dev, &na, &ns);
671 of_node_put(parent);
Stephen Warren5d61b162012-07-25 17:34:37 -0600672 if (!OF_CHECK_ADDR_COUNT(na))
Grant Likelydbbdee92010-06-08 07:48:10 -0600673 return NULL;
674
675 /* Get "reg" or "assigned-addresses" property */
676 prop = of_get_property(dev, bus->addresses, &psize);
677 if (prop == NULL)
678 return NULL;
679 psize /= 4;
680
681 onesize = na + ns;
682 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
683 if (i == index) {
684 if (size)
685 *size = of_read_number(prop + na, ns);
686 if (flags)
687 *flags = bus->get_flags(prop);
688 return prop;
689 }
690 return NULL;
691}
692EXPORT_SYMBOL(of_get_address);
693
Rob Herring67ccd2b2020-02-06 14:34:54 +0000694static int parser_init(struct of_pci_range_parser *parser,
695 struct device_node *node, const char *name)
696{
Rob Herring67ccd2b2020-02-06 14:34:54 +0000697 int rlen;
698
699 parser->node = node;
700 parser->pna = of_n_addr_cells(node);
Rob Herringbc5e5222020-02-06 14:01:05 +0000701 parser->na = of_bus_n_addr_cells(node);
702 parser->ns = of_bus_n_size_cells(node);
Rob Herring67ccd2b2020-02-06 14:34:54 +0000703 parser->dma = !strcmp(name, "dma-ranges");
704
705 parser->range = of_get_property(node, name, &rlen);
706 if (parser->range == NULL)
707 return -ENOENT;
708
709 parser->end = parser->range + rlen / sizeof(__be32);
710
711 return 0;
712}
713
714int of_pci_range_parser_init(struct of_pci_range_parser *parser,
715 struct device_node *node)
716{
717 return parser_init(parser, node, "ranges");
718}
719EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
720
721int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
722 struct device_node *node)
723{
724 return parser_init(parser, node, "dma-ranges");
725}
726EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init);
Rob Herringbc5e5222020-02-06 14:01:05 +0000727#define of_dma_range_parser_init of_pci_dma_range_parser_init
Rob Herring67ccd2b2020-02-06 14:34:54 +0000728
729struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
730 struct of_pci_range *range)
731{
Rob Herringbc5e5222020-02-06 14:01:05 +0000732 int na = parser->na;
733 int ns = parser->ns;
734 int np = parser->pna + na + ns;
Rob Herring67ccd2b2020-02-06 14:34:54 +0000735
736 if (!range)
737 return NULL;
738
Rob Herringbc5e5222020-02-06 14:01:05 +0000739 if (!parser->range || parser->range + np > parser->end)
Rob Herring67ccd2b2020-02-06 14:34:54 +0000740 return NULL;
741
Rob Herringbc5e5222020-02-06 14:01:05 +0000742 if (parser->na == 3)
743 range->flags = of_bus_pci_get_flags(parser->range);
744 else
745 range->flags = 0;
746
747 range->pci_addr = of_read_number(parser->range, na);
748
Rob Herring67ccd2b2020-02-06 14:34:54 +0000749 if (parser->dma)
750 range->cpu_addr = of_translate_dma_address(parser->node,
751 parser->range + na);
752 else
753 range->cpu_addr = of_translate_address(parser->node,
754 parser->range + na);
755 range->size = of_read_number(parser->range + parser->pna + na, ns);
756
Rob Herringbc5e5222020-02-06 14:01:05 +0000757 parser->range += np;
Rob Herring67ccd2b2020-02-06 14:34:54 +0000758
759 /* Now consume following elements while they are contiguous */
Rob Herringbc5e5222020-02-06 14:01:05 +0000760 while (parser->range + np <= parser->end) {
761 u32 flags = 0;
Rob Herring67ccd2b2020-02-06 14:34:54 +0000762 u64 pci_addr, cpu_addr, size;
763
Rob Herringbc5e5222020-02-06 14:01:05 +0000764 if (parser->na == 3)
765 flags = of_bus_pci_get_flags(parser->range);
766 pci_addr = of_read_number(parser->range, na);
Rob Herring67ccd2b2020-02-06 14:34:54 +0000767 if (parser->dma)
768 cpu_addr = of_translate_dma_address(parser->node,
769 parser->range + na);
770 else
771 cpu_addr = of_translate_address(parser->node,
772 parser->range + na);
773 size = of_read_number(parser->range + parser->pna + na, ns);
774
775 if (flags != range->flags)
776 break;
777 if (pci_addr != range->pci_addr + range->size ||
778 cpu_addr != range->cpu_addr + range->size)
779 break;
780
781 range->size += size;
Rob Herringbc5e5222020-02-06 14:01:05 +0000782 parser->range += np;
Rob Herring67ccd2b2020-02-06 14:34:54 +0000783 }
784
785 return range;
786}
787EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
788
Zhichang Yuan65af6182018-03-15 02:15:54 +0800789static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr,
790 u64 size)
791{
792 u64 taddr;
793 unsigned long port;
794 struct device_node *host;
795
Maxime Ripard95835a82019-04-01 10:56:43 +0200796 taddr = __of_translate_address(dev, of_get_parent,
797 in_addr, "ranges", &host);
Zhichang Yuan65af6182018-03-15 02:15:54 +0800798 if (host) {
799 /* host-specific port access */
800 port = logic_pio_trans_hwaddr(&host->fwnode, taddr, size);
801 of_node_put(host);
802 } else {
803 /* memory-mapped I/O range */
804 port = pci_address_to_pio(taddr);
805 }
806
807 if (port == (unsigned long)-1)
808 return OF_BAD_ADDR;
809
810 return port;
811}
812
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100813static int __of_address_to_resource(struct device_node *dev,
814 const __be32 *addrp, u64 size, unsigned int flags,
Benoit Cousson35f3da32011-12-05 15:23:55 +0100815 const char *name, struct resource *r)
Grant Likely1f5bef32010-06-08 07:48:09 -0600816{
817 u64 taddr;
818
Zhichang Yuan65af6182018-03-15 02:15:54 +0800819 if (flags & IORESOURCE_MEM)
820 taddr = of_translate_address(dev, addrp);
821 else if (flags & IORESOURCE_IO)
822 taddr = of_translate_ioport(dev, addrp, size);
823 else
Grant Likely1f5bef32010-06-08 07:48:09 -0600824 return -EINVAL;
Zhichang Yuan65af6182018-03-15 02:15:54 +0800825
Grant Likely1f5bef32010-06-08 07:48:09 -0600826 if (taddr == OF_BAD_ADDR)
827 return -EINVAL;
828 memset(r, 0, sizeof(struct resource));
Zhichang Yuan65af6182018-03-15 02:15:54 +0800829
830 r->start = taddr;
831 r->end = taddr + size - 1;
Grant Likely1f5bef32010-06-08 07:48:09 -0600832 r->flags = flags;
Benoit Cousson35f3da32011-12-05 15:23:55 +0100833 r->name = name ? name : dev->full_name;
834
Grant Likely1f5bef32010-06-08 07:48:09 -0600835 return 0;
836}
837
838/**
839 * of_address_to_resource - Translate device tree address and return as resource
840 *
841 * Note that if your address is a PIO address, the conversion will fail if
842 * the physical address can't be internally converted to an IO token with
Frank Rowand7602f422017-06-20 16:50:11 -0700843 * pci_address_to_pio(), that is because it's either called too early or it
Grant Likely1f5bef32010-06-08 07:48:09 -0600844 * can't be matched to any host bridge IO space
845 */
846int of_address_to_resource(struct device_node *dev, int index,
847 struct resource *r)
848{
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100849 const __be32 *addrp;
Grant Likely1f5bef32010-06-08 07:48:09 -0600850 u64 size;
851 unsigned int flags;
Benoit Cousson35f3da32011-12-05 15:23:55 +0100852 const char *name = NULL;
Grant Likely1f5bef32010-06-08 07:48:09 -0600853
854 addrp = of_get_address(dev, index, &size, &flags);
855 if (addrp == NULL)
856 return -EINVAL;
Benoit Cousson35f3da32011-12-05 15:23:55 +0100857
858 /* Get optional "reg-names" property to add a name to a resource */
859 of_property_read_string_index(dev, "reg-names", index, &name);
860
861 return __of_address_to_resource(dev, addrp, size, flags, name, r);
Grant Likely1f5bef32010-06-08 07:48:09 -0600862}
863EXPORT_SYMBOL_GPL(of_address_to_resource);
864
Grant Likely6b884a82010-06-08 07:48:09 -0600865/**
866 * of_iomap - Maps the memory mapped IO for a given device_node
867 * @device: the device whose io range will be mapped
868 * @index: index of the io range
869 *
870 * Returns a pointer to the mapped memory
871 */
872void __iomem *of_iomap(struct device_node *np, int index)
873{
874 struct resource res;
875
876 if (of_address_to_resource(np, index, &res))
877 return NULL;
878
Felipe Balbi28c1b6d2011-07-22 22:47:21 +0300879 return ioremap(res.start, resource_size(&res));
Grant Likely6b884a82010-06-08 07:48:09 -0600880}
881EXPORT_SYMBOL(of_iomap);
Grygorii Strashko18308c92014-04-24 11:30:02 -0400882
Matthias Bruggerefd342f2014-07-18 11:36:39 +0200883/*
884 * of_io_request_and_map - Requests a resource and maps the memory mapped IO
885 * for a given device_node
886 * @device: the device whose io range will be mapped
887 * @index: index of the io range
Benjamin Herrenschmidtb01dcdd2018-06-12 10:01:07 +1000888 * @name: name "override" for the memory region request or NULL
Matthias Bruggerefd342f2014-07-18 11:36:39 +0200889 *
890 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
891 * error code on failure. Usage example:
892 *
893 * base = of_io_request_and_map(node, 0, "foo");
894 * if (IS_ERR(base))
895 * return PTR_ERR(base);
896 */
897void __iomem *of_io_request_and_map(struct device_node *np, int index,
Benjamin Herrenschmidtb01dcdd2018-06-12 10:01:07 +1000898 const char *name)
Matthias Bruggerefd342f2014-07-18 11:36:39 +0200899{
900 struct resource res;
901 void __iomem *mem;
902
903 if (of_address_to_resource(np, index, &res))
904 return IOMEM_ERR_PTR(-EINVAL);
905
Benjamin Herrenschmidtb01dcdd2018-06-12 10:01:07 +1000906 if (!name)
907 name = res.name;
Matthias Bruggerefd342f2014-07-18 11:36:39 +0200908 if (!request_mem_region(res.start, resource_size(&res), name))
909 return IOMEM_ERR_PTR(-EBUSY);
910
911 mem = ioremap(res.start, resource_size(&res));
912 if (!mem) {
913 release_mem_region(res.start, resource_size(&res));
914 return IOMEM_ERR_PTR(-ENOMEM);
915 }
916
917 return mem;
918}
919EXPORT_SYMBOL(of_io_request_and_map);
920
Grygorii Strashko18308c92014-04-24 11:30:02 -0400921/**
922 * of_dma_get_range - Get DMA range info
923 * @np: device node to get DMA range info
924 * @dma_addr: pointer to store initial DMA address of DMA range
925 * @paddr: pointer to store initial CPU address of DMA range
926 * @size: pointer to store size of DMA range
927 *
928 * Look in bottom up direction for the first "dma-ranges" property
929 * and parse it.
930 * dma-ranges format:
931 * DMA addr (dma_addr) : naddr cells
932 * CPU addr (phys_addr_t) : pna cells
933 * size : nsize cells
934 *
935 * It returns -ENODEV if "dma-ranges" property was not found
936 * for this device in DT.
937 */
938int of_dma_get_range(struct device_node *np, u64 *dma_addr, u64 *paddr, u64 *size)
939{
940 struct device_node *node = of_node_get(np);
941 const __be32 *ranges = NULL;
Rob Herring7a8b64d2020-02-06 14:02:30 +0000942 int len;
Grygorii Strashko18308c92014-04-24 11:30:02 -0400943 int ret = 0;
Robin Murphy951d4882019-07-03 18:42:20 +0100944 bool found_dma_ranges = false;
Rob Herring7a8b64d2020-02-06 14:02:30 +0000945 struct of_range_parser parser;
946 struct of_range range;
Rob Herring9d55beb2020-02-06 14:26:40 +0000947 u64 dma_start = U64_MAX, dma_end = 0, dma_offset = 0;
Grygorii Strashko18308c92014-04-24 11:30:02 -0400948
Robin Murphy951d4882019-07-03 18:42:20 +0100949 while (node) {
Grygorii Strashko18308c92014-04-24 11:30:02 -0400950 ranges = of_get_property(node, "dma-ranges", &len);
951
952 /* Ignore empty ranges, they imply no translation required */
953 if (ranges && len > 0)
954 break;
955
Robin Murphy951d4882019-07-03 18:42:20 +0100956 /* Once we find 'dma-ranges', then a missing one is an error */
957 if (found_dma_ranges && !ranges) {
958 ret = -ENODEV;
959 goto out;
960 }
961 found_dma_ranges = true;
962
963 node = of_get_next_dma_parent(node);
Grygorii Strashko18308c92014-04-24 11:30:02 -0400964 }
965
Robin Murphy951d4882019-07-03 18:42:20 +0100966 if (!node || !ranges) {
Rob Herring0d638a02017-06-01 15:50:55 -0500967 pr_debug("no dma-ranges found for node(%pOF)\n", np);
Grygorii Strashko18308c92014-04-24 11:30:02 -0400968 ret = -ENODEV;
969 goto out;
970 }
971
Rob Herring7a8b64d2020-02-06 14:02:30 +0000972 of_dma_range_parser_init(&parser, node);
973
974 for_each_of_range(&parser, &range) {
975 pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
976 range.bus_addr, range.cpu_addr, range.size);
977
Rob Herring9d55beb2020-02-06 14:26:40 +0000978 if (dma_offset && range.cpu_addr - range.bus_addr != dma_offset) {
979 pr_warn("Can't handle multiple dma-ranges with different offsets on node(%pOF)\n", node);
980 /* Don't error out as we'd break some existing DTs */
981 continue;
982 }
983 dma_offset = range.cpu_addr - range.bus_addr;
Rob Herring7a8b64d2020-02-06 14:02:30 +0000984
Rob Herring9d55beb2020-02-06 14:26:40 +0000985 /* Take lower and upper limits */
986 if (range.bus_addr < dma_start)
987 dma_start = range.bus_addr;
988 if (range.bus_addr + range.size > dma_end)
989 dma_end = range.bus_addr + range.size;
990 }
991
992 if (dma_start >= dma_end) {
993 ret = -EINVAL;
994 pr_debug("Invalid DMA ranges configuration on node(%pOF)\n",
995 node);
Robin Murphy951d4882019-07-03 18:42:20 +0100996 goto out;
997 }
Grygorii Strashko18308c92014-04-24 11:30:02 -0400998
Rob Herring9d55beb2020-02-06 14:26:40 +0000999 *dma_addr = dma_start;
1000 *size = dma_end - dma_start;
1001 *paddr = dma_start + dma_offset;
1002
1003 pr_debug("final: dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
1004 *dma_addr, *paddr, *size);
Grygorii Strashko18308c92014-04-24 11:30:02 -04001005
1006out:
1007 of_node_put(node);
1008
1009 return ret;
1010}
Santosh Shilimkar92ea6372014-04-24 11:30:03 -04001011
1012/**
1013 * of_dma_is_coherent - Check if device is coherent
1014 * @np: device node
1015 *
1016 * It returns true if "dma-coherent" property was found
Michael Ellermandabf6b32020-01-26 22:52:47 +11001017 * for this device in the DT, or if DMA is coherent by
1018 * default for OF devices on the current platform.
Santosh Shilimkar92ea6372014-04-24 11:30:03 -04001019 */
1020bool of_dma_is_coherent(struct device_node *np)
1021{
1022 struct device_node *node = of_node_get(np);
1023
Michael Ellermandabf6b32020-01-26 22:52:47 +11001024 if (IS_ENABLED(CONFIG_OF_DMA_DEFAULT_COHERENT))
1025 return true;
1026
Santosh Shilimkar92ea6372014-04-24 11:30:03 -04001027 while (node) {
1028 if (of_property_read_bool(node, "dma-coherent")) {
1029 of_node_put(node);
1030 return true;
1031 }
Robin Murphyc60bf3e2019-07-03 14:47:31 +01001032 node = of_get_next_dma_parent(node);
Santosh Shilimkar92ea6372014-04-24 11:30:03 -04001033 }
1034 of_node_put(node);
1035 return false;
1036}
Linus Torvaldseb3d3ec2014-06-05 15:57:04 -07001037EXPORT_SYMBOL_GPL(of_dma_is_coherent);