blob: cdf047b6d0a2e2ebbd6914efb94469a90cf4ae82 [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>
Grant Likelydbbdee92010-06-08 07:48:10 -06008#include <linux/module.h>
Grant Likely6b884a82010-06-08 07:48:09 -06009#include <linux/of_address.h>
Tomasz Nowickic5076cf2016-05-11 17:34:51 -050010#include <linux/pci.h>
Grant Likelydbbdee92010-06-08 07:48:10 -060011#include <linux/pci_regs.h>
Liviu Dudau41f8bba2014-09-29 15:29:21 +010012#include <linux/sizes.h>
13#include <linux/slab.h>
Grant Likelydbbdee92010-06-08 07:48:10 -060014#include <linux/string.h>
Grant Likely6b884a82010-06-08 07:48:09 -060015
Grant Likelydbbdee92010-06-08 07:48:10 -060016/* Max address size we deal with */
17#define OF_MAX_ADDR_CELLS 4
Stephen Warren5d61b162012-07-25 17:34:37 -060018#define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
19#define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
Grant Likelydbbdee92010-06-08 07:48:10 -060020
21static struct of_bus *of_match_bus(struct device_node *np);
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +010022static int __of_address_to_resource(struct device_node *dev,
23 const __be32 *addrp, u64 size, unsigned int flags,
Benoit Cousson35f3da32011-12-05 15:23:55 +010024 const char *name, struct resource *r);
Grant Likelydbbdee92010-06-08 07:48:10 -060025
26/* Debug utility */
27#ifdef DEBUG
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +010028static void of_dump_addr(const char *s, const __be32 *addr, int na)
Grant Likelydbbdee92010-06-08 07:48:10 -060029{
Rob Herring606ad422016-06-15 08:32:18 -050030 pr_debug("%s", s);
Grant Likelydbbdee92010-06-08 07:48:10 -060031 while (na--)
Rob Herring606ad422016-06-15 08:32:18 -050032 pr_cont(" %08x", be32_to_cpu(*(addr++)));
33 pr_cont("\n");
Grant Likelydbbdee92010-06-08 07:48:10 -060034}
35#else
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +010036static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
Grant Likelydbbdee92010-06-08 07:48:10 -060037#endif
38
39/* Callbacks for bus specific translators */
40struct of_bus {
41 const char *name;
42 const char *addresses;
43 int (*match)(struct device_node *parent);
44 void (*count_cells)(struct device_node *child,
45 int *addrc, int *sizec);
Kim Phillips47b1e682012-10-08 19:41:58 -050046 u64 (*map)(__be32 *addr, const __be32 *range,
Grant Likelydbbdee92010-06-08 07:48:10 -060047 int na, int ns, int pna);
Kim Phillips47b1e682012-10-08 19:41:58 -050048 int (*translate)(__be32 *addr, u64 offset, int na);
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +010049 unsigned int (*get_flags)(const __be32 *addr);
Grant Likelydbbdee92010-06-08 07:48:10 -060050};
51
52/*
53 * Default translator (generic bus)
54 */
55
56static void of_bus_default_count_cells(struct device_node *dev,
57 int *addrc, int *sizec)
58{
59 if (addrc)
60 *addrc = of_n_addr_cells(dev);
61 if (sizec)
62 *sizec = of_n_size_cells(dev);
63}
64
Kim Phillips47b1e682012-10-08 19:41:58 -050065static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
Grant Likelydbbdee92010-06-08 07:48:10 -060066 int na, int ns, int pna)
67{
68 u64 cp, s, da;
69
70 cp = of_read_number(range, na);
71 s = of_read_number(range + na + pna, ns);
72 da = of_read_number(addr, na);
73
Rob Herring606ad422016-06-15 08:32:18 -050074 pr_debug("default map, cp=%llx, s=%llx, da=%llx\n",
Grant Likelydbbdee92010-06-08 07:48:10 -060075 (unsigned long long)cp, (unsigned long long)s,
76 (unsigned long long)da);
77
78 if (da < cp || da >= (cp + s))
79 return OF_BAD_ADDR;
80 return da - cp;
81}
82
Kim Phillips47b1e682012-10-08 19:41:58 -050083static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
Grant Likelydbbdee92010-06-08 07:48:10 -060084{
85 u64 a = of_read_number(addr, na);
86 memset(addr, 0, na * 4);
87 a += offset;
88 if (na > 1)
Grant Likely154063a92010-06-08 07:48:11 -060089 addr[na - 2] = cpu_to_be32(a >> 32);
90 addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
Grant Likelydbbdee92010-06-08 07:48:10 -060091
92 return 0;
93}
94
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +010095static unsigned int of_bus_default_get_flags(const __be32 *addr)
Grant Likelydbbdee92010-06-08 07:48:10 -060096{
97 return IORESOURCE_MEM;
98}
99
Rob Herring4670d612018-01-17 17:36:39 -0600100#ifdef CONFIG_PCI
Grant Likelydbbdee92010-06-08 07:48:10 -0600101/*
102 * PCI bus specific translator
103 */
104
105static int of_bus_pci_match(struct device_node *np)
106{
Benjamin Herrenschmidt6dd18e42013-07-03 16:01:10 +1000107 /*
Kleber Sacilotto de Souza14e2abb2014-02-03 13:31:03 -0200108 * "pciex" is PCI Express
Benjamin Herrenschmidt6dd18e42013-07-03 16:01:10 +1000109 * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
110 * "ht" is hypertransport
111 */
Kleber Sacilotto de Souza14e2abb2014-02-03 13:31:03 -0200112 return !strcmp(np->type, "pci") || !strcmp(np->type, "pciex") ||
113 !strcmp(np->type, "vci") || !strcmp(np->type, "ht");
Grant Likelydbbdee92010-06-08 07:48:10 -0600114}
115
116static void of_bus_pci_count_cells(struct device_node *np,
117 int *addrc, int *sizec)
118{
119 if (addrc)
120 *addrc = 3;
121 if (sizec)
122 *sizec = 2;
123}
124
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100125static unsigned int of_bus_pci_get_flags(const __be32 *addr)
Grant Likelydbbdee92010-06-08 07:48:10 -0600126{
127 unsigned int flags = 0;
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100128 u32 w = be32_to_cpup(addr);
Grant Likelydbbdee92010-06-08 07:48:10 -0600129
130 switch((w >> 24) & 0x03) {
131 case 0x01:
132 flags |= IORESOURCE_IO;
133 break;
134 case 0x02: /* 32 bits */
135 case 0x03: /* 64 bits */
136 flags |= IORESOURCE_MEM;
137 break;
138 }
139 if (w & 0x40000000)
140 flags |= IORESOURCE_PREFETCH;
141 return flags;
142}
143
Kim Phillips47b1e682012-10-08 19:41:58 -0500144static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100145 int pna)
Grant Likelydbbdee92010-06-08 07:48:10 -0600146{
147 u64 cp, s, da;
148 unsigned int af, rf;
149
150 af = of_bus_pci_get_flags(addr);
151 rf = of_bus_pci_get_flags(range);
152
153 /* Check address type match */
154 if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
155 return OF_BAD_ADDR;
156
157 /* Read address values, skipping high cell */
158 cp = of_read_number(range + 1, na - 1);
159 s = of_read_number(range + na + pna, ns);
160 da = of_read_number(addr + 1, na - 1);
161
Rob Herring606ad422016-06-15 08:32:18 -0500162 pr_debug("PCI map, cp=%llx, s=%llx, da=%llx\n",
Grant Likelydbbdee92010-06-08 07:48:10 -0600163 (unsigned long long)cp, (unsigned long long)s,
164 (unsigned long long)da);
165
166 if (da < cp || da >= (cp + s))
167 return OF_BAD_ADDR;
168 return da - cp;
169}
170
Kim Phillips47b1e682012-10-08 19:41:58 -0500171static int of_bus_pci_translate(__be32 *addr, u64 offset, int na)
Grant Likelydbbdee92010-06-08 07:48:10 -0600172{
173 return of_bus_default_translate(addr + 1, offset, na - 1);
174}
175
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100176const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
Grant Likelydbbdee92010-06-08 07:48:10 -0600177 unsigned int *flags)
178{
Jeremy Kerra9fadee2010-10-10 21:24:10 -0600179 const __be32 *prop;
Grant Likelydbbdee92010-06-08 07:48:10 -0600180 unsigned int psize;
181 struct device_node *parent;
182 struct of_bus *bus;
183 int onesize, i, na, ns;
184
185 /* Get parent & match bus type */
186 parent = of_get_parent(dev);
187 if (parent == NULL)
188 return NULL;
189 bus = of_match_bus(parent);
190 if (strcmp(bus->name, "pci")) {
191 of_node_put(parent);
192 return NULL;
193 }
194 bus->count_cells(dev, &na, &ns);
195 of_node_put(parent);
Stephen Warren5d61b162012-07-25 17:34:37 -0600196 if (!OF_CHECK_ADDR_COUNT(na))
Grant Likelydbbdee92010-06-08 07:48:10 -0600197 return NULL;
198
199 /* Get "reg" or "assigned-addresses" property */
200 prop = of_get_property(dev, bus->addresses, &psize);
201 if (prop == NULL)
202 return NULL;
203 psize /= 4;
204
205 onesize = na + ns;
Grant Likely154063a92010-06-08 07:48:11 -0600206 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
207 u32 val = be32_to_cpu(prop[0]);
208 if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
Grant Likelydbbdee92010-06-08 07:48:10 -0600209 if (size)
210 *size = of_read_number(prop + na, ns);
211 if (flags)
212 *flags = bus->get_flags(prop);
213 return prop;
214 }
Grant Likely154063a92010-06-08 07:48:11 -0600215 }
Grant Likelydbbdee92010-06-08 07:48:10 -0600216 return NULL;
217}
218EXPORT_SYMBOL(of_get_pci_address);
219
220int of_pci_address_to_resource(struct device_node *dev, int bar,
221 struct resource *r)
222{
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100223 const __be32 *addrp;
Grant Likelydbbdee92010-06-08 07:48:10 -0600224 u64 size;
225 unsigned int flags;
226
227 addrp = of_get_pci_address(dev, bar, &size, &flags);
228 if (addrp == NULL)
229 return -EINVAL;
Benoit Cousson35f3da32011-12-05 15:23:55 +0100230 return __of_address_to_resource(dev, addrp, size, flags, NULL, r);
Grant Likelydbbdee92010-06-08 07:48:10 -0600231}
232EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
Andrew Murray29b635c2013-05-16 17:55:17 +0200233
Marc Gonzaleza060c212017-09-26 12:22:54 +0200234static int parser_init(struct of_pci_range_parser *parser,
235 struct device_node *node, const char *name)
Andrew Murray29b635c2013-05-16 17:55:17 +0200236{
237 const int na = 3, ns = 2;
238 int rlen;
239
240 parser->node = node;
241 parser->pna = of_n_addr_cells(node);
242 parser->np = parser->pna + na + ns;
243
Marc Gonzaleza060c212017-09-26 12:22:54 +0200244 parser->range = of_get_property(node, name, &rlen);
Andrew Murray29b635c2013-05-16 17:55:17 +0200245 if (parser->range == NULL)
246 return -ENOENT;
247
248 parser->end = parser->range + rlen / sizeof(__be32);
249
250 return 0;
251}
Marc Gonzaleza060c212017-09-26 12:22:54 +0200252
253int of_pci_range_parser_init(struct of_pci_range_parser *parser,
254 struct device_node *node)
255{
256 return parser_init(parser, node, "ranges");
257}
Andrew Murray29b635c2013-05-16 17:55:17 +0200258EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
259
Marc Gonzaleza060c212017-09-26 12:22:54 +0200260int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
261 struct device_node *node)
262{
263 return parser_init(parser, node, "dma-ranges");
264}
265EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init);
266
Andrew Murray29b635c2013-05-16 17:55:17 +0200267struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
268 struct of_pci_range *range)
269{
270 const int na = 3, ns = 2;
271
272 if (!range)
273 return NULL;
274
275 if (!parser->range || parser->range + parser->np > parser->end)
276 return NULL;
277
Rob Herringeb310032017-05-04 12:34:30 -0500278 range->pci_space = be32_to_cpup(parser->range);
Andrew Murray29b635c2013-05-16 17:55:17 +0200279 range->flags = of_bus_pci_get_flags(parser->range);
280 range->pci_addr = of_read_number(parser->range + 1, ns);
281 range->cpu_addr = of_translate_address(parser->node,
282 parser->range + na);
283 range->size = of_read_number(parser->range + parser->pna + na, ns);
284
285 parser->range += parser->np;
286
287 /* Now consume following elements while they are contiguous */
288 while (parser->range + parser->np <= parser->end) {
Shawn Linfda9f5d2017-07-12 17:59:06 +0800289 u32 flags;
Andrew Murray29b635c2013-05-16 17:55:17 +0200290 u64 pci_addr, cpu_addr, size;
291
Andrew Murray29b635c2013-05-16 17:55:17 +0200292 flags = of_bus_pci_get_flags(parser->range);
293 pci_addr = of_read_number(parser->range + 1, ns);
294 cpu_addr = of_translate_address(parser->node,
295 parser->range + na);
296 size = of_read_number(parser->range + parser->pna + na, ns);
297
298 if (flags != range->flags)
299 break;
300 if (pci_addr != range->pci_addr + range->size ||
301 cpu_addr != range->cpu_addr + range->size)
302 break;
303
304 range->size += size;
305 parser->range += parser->np;
306 }
307
308 return range;
309}
310EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
311
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100312/*
313 * of_pci_range_to_resource - Create a resource from an of_pci_range
314 * @range: the PCI range that describes the resource
315 * @np: device node where the range belongs to
316 * @res: pointer to a valid resource that will be updated to
317 * reflect the values contained in the range.
318 *
319 * Returns EINVAL if the range cannot be converted to resource.
320 *
321 * Note that if the range is an IO range, the resource will be converted
322 * using pci_address_to_pio() which can fail if it is called too early or
323 * if the range cannot be matched to any host bridge IO space (our case here).
324 * To guard against that we try to register the IO range first.
325 * If that fails we know that pci_address_to_pio() will do too.
326 */
327int of_pci_range_to_resource(struct of_pci_range *range,
328 struct device_node *np, struct resource *res)
Liviu Dudau83bbde12014-09-29 15:29:24 +0100329{
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100330 int err;
Liviu Dudau83bbde12014-09-29 15:29:24 +0100331 res->flags = range->flags;
Liviu Dudau83bbde12014-09-29 15:29:24 +0100332 res->parent = res->child = res->sibling = NULL;
333 res->name = np->full_name;
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100334
335 if (res->flags & IORESOURCE_IO) {
336 unsigned long port;
Gabriele Paolonifcfaab32018-03-15 02:15:52 +0800337 err = pci_register_io_range(&np->fwnode, range->cpu_addr,
338 range->size);
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100339 if (err)
340 goto invalid_range;
341 port = pci_address_to_pio(range->cpu_addr);
342 if (port == (unsigned long)-1) {
343 err = -EINVAL;
344 goto invalid_range;
345 }
346 res->start = port;
347 } else {
Pavel Fedin4af97102015-10-08 10:24:25 +0300348 if ((sizeof(resource_size_t) < 8) &&
349 upper_32_bits(range->cpu_addr)) {
350 err = -EINVAL;
351 goto invalid_range;
352 }
353
Liviu Dudau0b0b0892014-09-29 15:29:25 +0100354 res->start = range->cpu_addr;
355 }
356 res->end = res->start + range->size - 1;
357 return 0;
358
359invalid_range:
360 res->start = (resource_size_t)OF_BAD_ADDR;
361 res->end = (resource_size_t)OF_BAD_ADDR;
362 return err;
Liviu Dudau83bbde12014-09-29 15:29:24 +0100363}
Manikanta Maddireddybf6681e2018-01-11 11:38:02 +0530364EXPORT_SYMBOL(of_pci_range_to_resource);
Grant Likelydbbdee92010-06-08 07:48:10 -0600365#endif /* CONFIG_PCI */
366
367/*
368 * ISA bus specific translator
369 */
370
371static int of_bus_isa_match(struct device_node *np)
372{
373 return !strcmp(np->name, "isa");
374}
375
376static void of_bus_isa_count_cells(struct device_node *child,
377 int *addrc, int *sizec)
378{
379 if (addrc)
380 *addrc = 2;
381 if (sizec)
382 *sizec = 1;
383}
384
Kim Phillips47b1e682012-10-08 19:41:58 -0500385static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100386 int pna)
Grant Likelydbbdee92010-06-08 07:48:10 -0600387{
388 u64 cp, s, da;
389
390 /* Check address type match */
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100391 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
Grant Likelydbbdee92010-06-08 07:48:10 -0600392 return OF_BAD_ADDR;
393
394 /* Read address values, skipping high cell */
395 cp = of_read_number(range + 1, na - 1);
396 s = of_read_number(range + na + pna, ns);
397 da = of_read_number(addr + 1, na - 1);
398
Rob Herring606ad422016-06-15 08:32:18 -0500399 pr_debug("ISA map, cp=%llx, s=%llx, da=%llx\n",
Grant Likelydbbdee92010-06-08 07:48:10 -0600400 (unsigned long long)cp, (unsigned long long)s,
401 (unsigned long long)da);
402
403 if (da < cp || da >= (cp + s))
404 return OF_BAD_ADDR;
405 return da - cp;
406}
407
Kim Phillips47b1e682012-10-08 19:41:58 -0500408static int of_bus_isa_translate(__be32 *addr, u64 offset, int na)
Grant Likelydbbdee92010-06-08 07:48:10 -0600409{
410 return of_bus_default_translate(addr + 1, offset, na - 1);
411}
412
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100413static unsigned int of_bus_isa_get_flags(const __be32 *addr)
Grant Likelydbbdee92010-06-08 07:48:10 -0600414{
415 unsigned int flags = 0;
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100416 u32 w = be32_to_cpup(addr);
Grant Likelydbbdee92010-06-08 07:48:10 -0600417
418 if (w & 1)
419 flags |= IORESOURCE_IO;
420 else
421 flags |= IORESOURCE_MEM;
422 return flags;
423}
424
425/*
426 * Array of bus specific translators
427 */
428
429static struct of_bus of_busses[] = {
Rob Herring4670d612018-01-17 17:36:39 -0600430#ifdef CONFIG_PCI
Grant Likelydbbdee92010-06-08 07:48:10 -0600431 /* PCI */
432 {
433 .name = "pci",
434 .addresses = "assigned-addresses",
435 .match = of_bus_pci_match,
436 .count_cells = of_bus_pci_count_cells,
437 .map = of_bus_pci_map,
438 .translate = of_bus_pci_translate,
439 .get_flags = of_bus_pci_get_flags,
440 },
Rob Herring4670d612018-01-17 17:36:39 -0600441#endif /* CONFIG_PCI */
Grant Likelydbbdee92010-06-08 07:48:10 -0600442 /* ISA */
443 {
444 .name = "isa",
445 .addresses = "reg",
446 .match = of_bus_isa_match,
447 .count_cells = of_bus_isa_count_cells,
448 .map = of_bus_isa_map,
449 .translate = of_bus_isa_translate,
450 .get_flags = of_bus_isa_get_flags,
451 },
452 /* Default */
453 {
454 .name = "default",
455 .addresses = "reg",
456 .match = NULL,
457 .count_cells = of_bus_default_count_cells,
458 .map = of_bus_default_map,
459 .translate = of_bus_default_translate,
460 .get_flags = of_bus_default_get_flags,
461 },
462};
463
464static struct of_bus *of_match_bus(struct device_node *np)
465{
466 int i;
467
468 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
469 if (!of_busses[i].match || of_busses[i].match(np))
470 return &of_busses[i];
471 BUG();
472 return NULL;
473}
474
Benjamin Herrenschmidt41d94892015-03-23 14:16:38 +1100475static int of_empty_ranges_quirk(struct device_node *np)
Benjamin Herrenschmidt746c9e92014-11-14 17:55:03 +1100476{
477 if (IS_ENABLED(CONFIG_PPC)) {
Benjamin Herrenschmidt41d94892015-03-23 14:16:38 +1100478 /* To save cycles, we cache the result for global "Mac" setting */
Benjamin Herrenschmidt746c9e92014-11-14 17:55:03 +1100479 static int quirk_state = -1;
480
Benjamin Herrenschmidt41d94892015-03-23 14:16:38 +1100481 /* PA-SEMI sdc DT bug */
482 if (of_device_is_compatible(np, "1682m-sdc"))
483 return true;
484
485 /* Make quirk cached */
Benjamin Herrenschmidt746c9e92014-11-14 17:55:03 +1100486 if (quirk_state < 0)
487 quirk_state =
488 of_machine_is_compatible("Power Macintosh") ||
489 of_machine_is_compatible("MacRISC");
490 return quirk_state;
491 }
492 return false;
493}
494
Grant Likelydbbdee92010-06-08 07:48:10 -0600495static int of_translate_one(struct device_node *parent, struct of_bus *bus,
Kim Phillips47b1e682012-10-08 19:41:58 -0500496 struct of_bus *pbus, __be32 *addr,
Grant Likelydbbdee92010-06-08 07:48:10 -0600497 int na, int ns, int pna, const char *rprop)
498{
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100499 const __be32 *ranges;
Grant Likelydbbdee92010-06-08 07:48:10 -0600500 unsigned int rlen;
501 int rone;
502 u64 offset = OF_BAD_ADDR;
503
Masahiro Yamadaba85edb2015-11-30 15:22:37 +0900504 /*
505 * Normally, an absence of a "ranges" property means we are
Grant Likelydbbdee92010-06-08 07:48:10 -0600506 * crossing a non-translatable boundary, and thus the addresses
Masahiro Yamadaba85edb2015-11-30 15:22:37 +0900507 * below the current cannot be converted to CPU physical ones.
Grant Likelydbbdee92010-06-08 07:48:10 -0600508 * Unfortunately, while this is very clear in the spec, it's not
509 * what Apple understood, and they do have things like /uni-n or
510 * /ht nodes with no "ranges" property and a lot of perfectly
511 * useable mapped devices below them. Thus we treat the absence of
512 * "ranges" as equivalent to an empty "ranges" property which means
513 * a 1:1 translation at that level. It's up to the caller not to try
514 * to translate addresses that aren't supposed to be translated in
515 * the first place. --BenH.
Grant Likely3930f292010-06-08 07:48:11 -0600516 *
517 * As far as we know, this damage only exists on Apple machines, so
518 * This code is only enabled on powerpc. --gcl
Grant Likelydbbdee92010-06-08 07:48:10 -0600519 */
520 ranges = of_get_property(parent, rprop, &rlen);
Benjamin Herrenschmidt41d94892015-03-23 14:16:38 +1100521 if (ranges == NULL && !of_empty_ranges_quirk(parent)) {
Rob Herring606ad422016-06-15 08:32:18 -0500522 pr_debug("no ranges; cannot translate\n");
Grant Likely3930f292010-06-08 07:48:11 -0600523 return 1;
524 }
Grant Likelydbbdee92010-06-08 07:48:10 -0600525 if (ranges == NULL || rlen == 0) {
526 offset = of_read_number(addr, na);
527 memset(addr, 0, pna * 4);
Rob Herring606ad422016-06-15 08:32:18 -0500528 pr_debug("empty ranges; 1:1 translation\n");
Grant Likelydbbdee92010-06-08 07:48:10 -0600529 goto finish;
530 }
531
Rob Herring606ad422016-06-15 08:32:18 -0500532 pr_debug("walking ranges...\n");
Grant Likelydbbdee92010-06-08 07:48:10 -0600533
534 /* Now walk through the ranges */
535 rlen /= 4;
536 rone = na + pna + ns;
537 for (; rlen >= rone; rlen -= rone, ranges += rone) {
538 offset = bus->map(addr, ranges, na, ns, pna);
539 if (offset != OF_BAD_ADDR)
540 break;
541 }
542 if (offset == OF_BAD_ADDR) {
Rob Herring606ad422016-06-15 08:32:18 -0500543 pr_debug("not found !\n");
Grant Likelydbbdee92010-06-08 07:48:10 -0600544 return 1;
545 }
546 memcpy(addr, ranges + na, 4 * pna);
547
548 finish:
Rob Herring606ad422016-06-15 08:32:18 -0500549 of_dump_addr("parent translation for:", addr, pna);
550 pr_debug("with offset: %llx\n", (unsigned long long)offset);
Grant Likelydbbdee92010-06-08 07:48:10 -0600551
552 /* Translate it into parent bus space */
553 return pbus->translate(addr, offset, pna);
554}
555
556/*
557 * Translate an address from the device-tree into a CPU physical address,
558 * this walks up the tree and applies the various bus mappings on the
559 * way.
560 *
561 * Note: We consider that crossing any level with #size-cells == 0 to mean
562 * that translation is impossible (that is we are not dealing with a value
563 * that can be mapped to a cpu physical address). This is not really specified
564 * that way, but this is traditionally the way IBM at least do things
565 */
Kim Phillips47b1e682012-10-08 19:41:58 -0500566static u64 __of_translate_address(struct device_node *dev,
567 const __be32 *in_addr, const char *rprop)
Grant Likelydbbdee92010-06-08 07:48:10 -0600568{
569 struct device_node *parent = NULL;
570 struct of_bus *bus, *pbus;
Kim Phillips47b1e682012-10-08 19:41:58 -0500571 __be32 addr[OF_MAX_ADDR_CELLS];
Grant Likelydbbdee92010-06-08 07:48:10 -0600572 int na, ns, pna, pns;
573 u64 result = OF_BAD_ADDR;
574
Rob Herring0d638a02017-06-01 15:50:55 -0500575 pr_debug("** translation for device %pOF **\n", dev);
Grant Likelydbbdee92010-06-08 07:48:10 -0600576
577 /* Increase refcount at current level */
578 of_node_get(dev);
579
580 /* Get parent & match bus type */
581 parent = of_get_parent(dev);
582 if (parent == NULL)
583 goto bail;
584 bus = of_match_bus(parent);
585
Andrew Murray59f5ca42012-12-13 10:11:23 +0000586 /* Count address cells & copy address locally */
Grant Likelydbbdee92010-06-08 07:48:10 -0600587 bus->count_cells(dev, &na, &ns);
588 if (!OF_CHECK_COUNTS(na, ns)) {
Rob Herring0d638a02017-06-01 15:50:55 -0500589 pr_debug("Bad cell count for %pOF\n", dev);
Grant Likelydbbdee92010-06-08 07:48:10 -0600590 goto bail;
591 }
592 memcpy(addr, in_addr, na * 4);
593
Rob Herring0d638a02017-06-01 15:50:55 -0500594 pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",
595 bus->name, na, ns, parent);
Rob Herring606ad422016-06-15 08:32:18 -0500596 of_dump_addr("translating address:", addr, na);
Grant Likelydbbdee92010-06-08 07:48:10 -0600597
598 /* Translate */
599 for (;;) {
600 /* Switch to parent bus */
601 of_node_put(dev);
602 dev = parent;
603 parent = of_get_parent(dev);
604
605 /* If root, we have finished */
606 if (parent == NULL) {
Rob Herring606ad422016-06-15 08:32:18 -0500607 pr_debug("reached root node\n");
Grant Likelydbbdee92010-06-08 07:48:10 -0600608 result = of_read_number(addr, na);
609 break;
610 }
611
612 /* Get new parent bus and counts */
613 pbus = of_match_bus(parent);
614 pbus->count_cells(dev, &pna, &pns);
615 if (!OF_CHECK_COUNTS(pna, pns)) {
Rob Herring0d638a02017-06-01 15:50:55 -0500616 pr_err("Bad cell count for %pOF\n", dev);
Grant Likelydbbdee92010-06-08 07:48:10 -0600617 break;
618 }
619
Rob Herring0d638a02017-06-01 15:50:55 -0500620 pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
621 pbus->name, pna, pns, parent);
Grant Likelydbbdee92010-06-08 07:48:10 -0600622
623 /* Apply bus translation */
624 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
625 break;
626
627 /* Complete the move up one level */
628 na = pna;
629 ns = pns;
630 bus = pbus;
631
Rob Herring606ad422016-06-15 08:32:18 -0500632 of_dump_addr("one level translation:", addr, na);
Grant Likelydbbdee92010-06-08 07:48:10 -0600633 }
634 bail:
635 of_node_put(parent);
636 of_node_put(dev);
637
638 return result;
639}
640
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100641u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
Grant Likelydbbdee92010-06-08 07:48:10 -0600642{
643 return __of_translate_address(dev, in_addr, "ranges");
644}
645EXPORT_SYMBOL(of_translate_address);
646
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100647u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
Grant Likelydbbdee92010-06-08 07:48:10 -0600648{
649 return __of_translate_address(dev, in_addr, "dma-ranges");
650}
651EXPORT_SYMBOL(of_translate_dma_address);
652
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100653const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,
Grant Likelydbbdee92010-06-08 07:48:10 -0600654 unsigned int *flags)
655{
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100656 const __be32 *prop;
Grant Likelydbbdee92010-06-08 07:48:10 -0600657 unsigned int psize;
658 struct device_node *parent;
659 struct of_bus *bus;
660 int onesize, i, na, ns;
661
662 /* Get parent & match bus type */
663 parent = of_get_parent(dev);
664 if (parent == NULL)
665 return NULL;
666 bus = of_match_bus(parent);
667 bus->count_cells(dev, &na, &ns);
668 of_node_put(parent);
Stephen Warren5d61b162012-07-25 17:34:37 -0600669 if (!OF_CHECK_ADDR_COUNT(na))
Grant Likelydbbdee92010-06-08 07:48:10 -0600670 return NULL;
671
672 /* Get "reg" or "assigned-addresses" property */
673 prop = of_get_property(dev, bus->addresses, &psize);
674 if (prop == NULL)
675 return NULL;
676 psize /= 4;
677
678 onesize = na + ns;
679 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
680 if (i == index) {
681 if (size)
682 *size = of_read_number(prop + na, ns);
683 if (flags)
684 *flags = bus->get_flags(prop);
685 return prop;
686 }
687 return NULL;
688}
689EXPORT_SYMBOL(of_get_address);
690
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100691static int __of_address_to_resource(struct device_node *dev,
692 const __be32 *addrp, u64 size, unsigned int flags,
Benoit Cousson35f3da32011-12-05 15:23:55 +0100693 const char *name, struct resource *r)
Grant Likely1f5bef32010-06-08 07:48:09 -0600694{
695 u64 taddr;
696
697 if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
698 return -EINVAL;
699 taddr = of_translate_address(dev, addrp);
700 if (taddr == OF_BAD_ADDR)
701 return -EINVAL;
702 memset(r, 0, sizeof(struct resource));
703 if (flags & IORESOURCE_IO) {
704 unsigned long port;
705 port = pci_address_to_pio(taddr);
706 if (port == (unsigned long)-1)
707 return -EINVAL;
708 r->start = port;
709 r->end = port + size - 1;
710 } else {
711 r->start = taddr;
712 r->end = taddr + size - 1;
713 }
714 r->flags = flags;
Benoit Cousson35f3da32011-12-05 15:23:55 +0100715 r->name = name ? name : dev->full_name;
716
Grant Likely1f5bef32010-06-08 07:48:09 -0600717 return 0;
718}
719
720/**
721 * of_address_to_resource - Translate device tree address and return as resource
722 *
723 * Note that if your address is a PIO address, the conversion will fail if
724 * the physical address can't be internally converted to an IO token with
Frank Rowand7602f422017-06-20 16:50:11 -0700725 * pci_address_to_pio(), that is because it's either called too early or it
Grant Likely1f5bef32010-06-08 07:48:09 -0600726 * can't be matched to any host bridge IO space
727 */
728int of_address_to_resource(struct device_node *dev, int index,
729 struct resource *r)
730{
Sebastian Andrzej Siewior0131d892010-12-01 10:54:46 +0100731 const __be32 *addrp;
Grant Likely1f5bef32010-06-08 07:48:09 -0600732 u64 size;
733 unsigned int flags;
Benoit Cousson35f3da32011-12-05 15:23:55 +0100734 const char *name = NULL;
Grant Likely1f5bef32010-06-08 07:48:09 -0600735
736 addrp = of_get_address(dev, index, &size, &flags);
737 if (addrp == NULL)
738 return -EINVAL;
Benoit Cousson35f3da32011-12-05 15:23:55 +0100739
740 /* Get optional "reg-names" property to add a name to a resource */
741 of_property_read_string_index(dev, "reg-names", index, &name);
742
743 return __of_address_to_resource(dev, addrp, size, flags, name, r);
Grant Likely1f5bef32010-06-08 07:48:09 -0600744}
745EXPORT_SYMBOL_GPL(of_address_to_resource);
746
Grant Likely90e33f62011-07-05 23:42:28 -0600747struct device_node *of_find_matching_node_by_address(struct device_node *from,
748 const struct of_device_id *matches,
749 u64 base_address)
750{
751 struct device_node *dn = of_find_matching_node(from, matches);
752 struct resource res;
753
754 while (dn) {
David Daney3a496b02015-08-19 13:17:47 -0700755 if (!of_address_to_resource(dn, 0, &res) &&
756 res.start == base_address)
Grant Likely90e33f62011-07-05 23:42:28 -0600757 return dn;
David Daney3a496b02015-08-19 13:17:47 -0700758
Grant Likely90e33f62011-07-05 23:42:28 -0600759 dn = of_find_matching_node(dn, matches);
760 }
761
762 return NULL;
763}
764
Grant Likely1f5bef32010-06-08 07:48:09 -0600765
Grant Likely6b884a82010-06-08 07:48:09 -0600766/**
767 * of_iomap - Maps the memory mapped IO for a given device_node
768 * @device: the device whose io range will be mapped
769 * @index: index of the io range
770 *
771 * Returns a pointer to the mapped memory
772 */
773void __iomem *of_iomap(struct device_node *np, int index)
774{
775 struct resource res;
776
777 if (of_address_to_resource(np, index, &res))
778 return NULL;
779
Felipe Balbi28c1b6d2011-07-22 22:47:21 +0300780 return ioremap(res.start, resource_size(&res));
Grant Likely6b884a82010-06-08 07:48:09 -0600781}
782EXPORT_SYMBOL(of_iomap);
Grygorii Strashko18308c92014-04-24 11:30:02 -0400783
Matthias Bruggerefd342f2014-07-18 11:36:39 +0200784/*
785 * of_io_request_and_map - Requests a resource and maps the memory mapped IO
786 * for a given device_node
787 * @device: the device whose io range will be mapped
788 * @index: index of the io range
789 * @name: name of the resource
790 *
791 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
792 * error code on failure. Usage example:
793 *
794 * base = of_io_request_and_map(node, 0, "foo");
795 * if (IS_ERR(base))
796 * return PTR_ERR(base);
797 */
798void __iomem *of_io_request_and_map(struct device_node *np, int index,
Matthias Bruggerb75b2762014-10-21 18:27:25 +0200799 const char *name)
Matthias Bruggerefd342f2014-07-18 11:36:39 +0200800{
801 struct resource res;
802 void __iomem *mem;
803
804 if (of_address_to_resource(np, index, &res))
805 return IOMEM_ERR_PTR(-EINVAL);
806
807 if (!request_mem_region(res.start, resource_size(&res), name))
808 return IOMEM_ERR_PTR(-EBUSY);
809
810 mem = ioremap(res.start, resource_size(&res));
811 if (!mem) {
812 release_mem_region(res.start, resource_size(&res));
813 return IOMEM_ERR_PTR(-ENOMEM);
814 }
815
816 return mem;
817}
818EXPORT_SYMBOL(of_io_request_and_map);
819
Grygorii Strashko18308c92014-04-24 11:30:02 -0400820/**
821 * of_dma_get_range - Get DMA range info
822 * @np: device node to get DMA range info
823 * @dma_addr: pointer to store initial DMA address of DMA range
824 * @paddr: pointer to store initial CPU address of DMA range
825 * @size: pointer to store size of DMA range
826 *
827 * Look in bottom up direction for the first "dma-ranges" property
828 * and parse it.
829 * dma-ranges format:
830 * DMA addr (dma_addr) : naddr cells
831 * CPU addr (phys_addr_t) : pna cells
832 * size : nsize cells
833 *
834 * It returns -ENODEV if "dma-ranges" property was not found
835 * for this device in DT.
836 */
837int of_dma_get_range(struct device_node *np, u64 *dma_addr, u64 *paddr, u64 *size)
838{
839 struct device_node *node = of_node_get(np);
840 const __be32 *ranges = NULL;
841 int len, naddr, nsize, pna;
842 int ret = 0;
843 u64 dmaaddr;
844
845 if (!node)
846 return -EINVAL;
847
848 while (1) {
849 naddr = of_n_addr_cells(node);
850 nsize = of_n_size_cells(node);
851 node = of_get_next_parent(node);
852 if (!node)
853 break;
854
855 ranges = of_get_property(node, "dma-ranges", &len);
856
857 /* Ignore empty ranges, they imply no translation required */
858 if (ranges && len > 0)
859 break;
860
861 /*
862 * At least empty ranges has to be defined for parent node if
863 * DMA is supported
864 */
865 if (!ranges)
866 break;
867 }
868
869 if (!ranges) {
Rob Herring0d638a02017-06-01 15:50:55 -0500870 pr_debug("no dma-ranges found for node(%pOF)\n", np);
Grygorii Strashko18308c92014-04-24 11:30:02 -0400871 ret = -ENODEV;
872 goto out;
873 }
874
875 len /= sizeof(u32);
876
877 pna = of_n_addr_cells(node);
878
879 /* dma-ranges format:
880 * DMA addr : naddr cells
881 * CPU addr : pna cells
882 * size : nsize cells
883 */
884 dmaaddr = of_read_number(ranges, naddr);
885 *paddr = of_translate_dma_address(np, ranges);
886 if (*paddr == OF_BAD_ADDR) {
Rob Herring0d638a02017-06-01 15:50:55 -0500887 pr_err("translation of DMA address(%pad) to CPU address failed node(%pOF)\n",
888 dma_addr, np);
Grygorii Strashko18308c92014-04-24 11:30:02 -0400889 ret = -EINVAL;
890 goto out;
891 }
892 *dma_addr = dmaaddr;
893
894 *size = of_read_number(ranges + naddr + pna, nsize);
895
896 pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
897 *dma_addr, *paddr, *size);
898
899out:
900 of_node_put(node);
901
902 return ret;
903}
904EXPORT_SYMBOL_GPL(of_dma_get_range);
Santosh Shilimkar92ea6372014-04-24 11:30:03 -0400905
906/**
907 * of_dma_is_coherent - Check if device is coherent
908 * @np: device node
909 *
910 * It returns true if "dma-coherent" property was found
911 * for this device in DT.
912 */
913bool of_dma_is_coherent(struct device_node *np)
914{
915 struct device_node *node = of_node_get(np);
916
917 while (node) {
918 if (of_property_read_bool(node, "dma-coherent")) {
919 of_node_put(node);
920 return true;
921 }
922 node = of_get_next_parent(node);
923 }
924 of_node_put(node);
925 return false;
926}
Linus Torvaldseb3d3ec2014-06-05 15:57:04 -0700927EXPORT_SYMBOL_GPL(of_dma_is_coherent);