1 // SPDX-License-Identifier: GPL-2.0
2 #define pr_fmt(fmt) "OF: " fmt
3
4 #include <linux/device.h>
5 #include <linux/fwnode.h>
6 #include <linux/io.h>
7 #include <linux/ioport.h>
8 #include <linux/logic_pio.h>
9 #include <linux/module.h>
10 #include <linux/of_address.h>
11 #include <linux/overflow.h>
12 #include <linux/pci.h>
13 #include <linux/pci_regs.h>
14 #include <linux/sizes.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/dma-direct.h> /* for bus_dma_region */
18
19 #include <kunit/visibility.h>
20
21 /* Uncomment me to enable of_dump_addr() debugging output */
22 // #define DEBUG
23
24 #include "of_private.h"
25
26 /* Callbacks for bus specific translators */
27 struct of_bus {
28 const char *name;
29 const char *addresses;
30 int (*match)(struct device_node *parent);
31 void (*count_cells)(struct device_node *child,
32 int *addrc, int *sizec);
33 u64 (*map)(__be32 *addr, const __be32 *range,
34 int na, int ns, int pna, int fna);
35 int (*translate)(__be32 *addr, u64 offset, int na);
36 int flag_cells;
37 unsigned int (*get_flags)(const __be32 *addr);
38 };
39
40 /*
41 * Default translator (generic bus)
42 */
43
of_bus_default_count_cells(struct device_node * dev,int * addrc,int * sizec)44 static void of_bus_default_count_cells(struct device_node *dev,
45 int *addrc, int *sizec)
46 {
47 if (addrc)
48 *addrc = of_n_addr_cells(dev);
49 if (sizec)
50 *sizec = of_n_size_cells(dev);
51 }
52
of_bus_default_map(__be32 * addr,const __be32 * range,int na,int ns,int pna,int fna)53 static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
54 int na, int ns, int pna, int fna)
55 {
56 u64 cp, s, da;
57
58 cp = of_read_number(range + fna, na - fna);
59 s = of_read_number(range + na + pna, ns);
60 da = of_read_number(addr + fna, na - fna);
61
62 pr_debug("default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
63
64 if (da < cp || da >= (cp + s))
65 return OF_BAD_ADDR;
66 return da - cp;
67 }
68
of_bus_default_translate(__be32 * addr,u64 offset,int na)69 static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
70 {
71 u64 a = of_read_number(addr, na);
72 memset(addr, 0, na * 4);
73 a += offset;
74 if (na > 1)
75 addr[na - 2] = cpu_to_be32(a >> 32);
76 addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
77
78 return 0;
79 }
80
of_bus_default_flags_get_flags(const __be32 * addr)81 static unsigned int of_bus_default_flags_get_flags(const __be32 *addr)
82 {
83 return of_read_number(addr, 1);
84 }
85
of_bus_default_get_flags(const __be32 * addr)86 static unsigned int of_bus_default_get_flags(const __be32 *addr)
87 {
88 return IORESOURCE_MEM;
89 }
90
of_bus_default_flags_map(__be32 * addr,const __be32 * range,int na,int ns,int pna,int fna)91 static u64 of_bus_default_flags_map(__be32 *addr, const __be32 *range, int na,
92 int ns, int pna, int fna)
93 {
94 /* Check that flags match */
95 if (*addr != *range)
96 return OF_BAD_ADDR;
97
98 return of_bus_default_map(addr, range, na, ns, pna, fna);
99 }
100
of_bus_default_flags_translate(__be32 * addr,u64 offset,int na)101 static int of_bus_default_flags_translate(__be32 *addr, u64 offset, int na)
102 {
103 /* Keep "flags" part (high cell) in translated address */
104 return of_bus_default_translate(addr + 1, offset, na - 1);
105 }
106
107 #ifdef CONFIG_PCI
of_bus_pci_get_flags(const __be32 * addr)108 static unsigned int of_bus_pci_get_flags(const __be32 *addr)
109 {
110 unsigned int flags = 0;
111 u32 w = be32_to_cpup(addr);
112
113 if (!IS_ENABLED(CONFIG_PCI))
114 return 0;
115
116 switch((w >> 24) & 0x03) {
117 case 0x01:
118 flags |= IORESOURCE_IO;
119 break;
120 case 0x02: /* 32 bits */
121 flags |= IORESOURCE_MEM;
122 break;
123
124 case 0x03: /* 64 bits */
125 flags |= IORESOURCE_MEM | IORESOURCE_MEM_64;
126 break;
127 }
128 if (w & 0x40000000)
129 flags |= IORESOURCE_PREFETCH;
130 return flags;
131 }
132
133 /*
134 * PCI bus specific translator
135 */
136
of_node_is_pcie(const struct device_node * np)137 static bool of_node_is_pcie(const struct device_node *np)
138 {
139 bool is_pcie = of_node_name_eq(np, "pcie");
140
141 if (is_pcie)
142 pr_warn_once("%pOF: Missing device_type\n", np);
143
144 return is_pcie;
145 }
146
of_bus_pci_match(struct device_node * np)147 static int of_bus_pci_match(struct device_node *np)
148 {
149 /*
150 * "pciex" is PCI Express
151 * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
152 * "ht" is hypertransport
153 *
154 * If none of the device_type match, and that the node name is
155 * "pcie", accept the device as PCI (with a warning).
156 */
157 return of_node_is_type(np, "pci") || of_node_is_type(np, "pciex") ||
158 of_node_is_type(np, "vci") || of_node_is_type(np, "ht") ||
159 of_node_is_pcie(np);
160 }
161
of_bus_pci_count_cells(struct device_node * np,int * addrc,int * sizec)162 static void of_bus_pci_count_cells(struct device_node *np,
163 int *addrc, int *sizec)
164 {
165 if (addrc)
166 *addrc = 3;
167 if (sizec)
168 *sizec = 2;
169 }
170
of_bus_pci_map(__be32 * addr,const __be32 * range,int na,int ns,int pna,int fna)171 static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
172 int pna, int fna)
173 {
174 unsigned int af, rf;
175
176 af = of_bus_pci_get_flags(addr);
177 rf = of_bus_pci_get_flags(range);
178
179 /* Check address type match */
180 if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
181 return OF_BAD_ADDR;
182
183 return of_bus_default_map(addr, range, na, ns, pna, fna);
184 }
185
186 #endif /* CONFIG_PCI */
187
__of_address_resource_bounds(struct resource * r,u64 start,u64 size)188 VISIBLE_IF_KUNIT int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)
189 {
190 if (overflows_type(start, r->start))
191 return -EOVERFLOW;
192
193 r->start = start;
194
195 if (!size)
196 r->end = wrapping_sub(typeof(r->end), r->start, 1);
197 else if (size && check_add_overflow(r->start, size - 1, &r->end))
198 return -EOVERFLOW;
199
200 return 0;
201 }
202 EXPORT_SYMBOL_IF_KUNIT(__of_address_resource_bounds);
203
204 /*
205 * of_pci_range_to_resource - Create a resource from an of_pci_range
206 * @range: the PCI range that describes the resource
207 * @np: device node where the range belongs to
208 * @res: pointer to a valid resource that will be updated to
209 * reflect the values contained in the range.
210 *
211 * Returns -EINVAL if the range cannot be converted to resource.
212 *
213 * Note that if the range is an IO range, the resource will be converted
214 * using pci_address_to_pio() which can fail if it is called too early or
215 * if the range cannot be matched to any host bridge IO space (our case here).
216 * To guard against that we try to register the IO range first.
217 * If that fails we know that pci_address_to_pio() will do too.
218 */
of_pci_range_to_resource(const struct of_pci_range * range,const struct device_node * np,struct resource * res)219 int of_pci_range_to_resource(const struct of_pci_range *range,
220 const struct device_node *np, struct resource *res)
221 {
222 u64 start;
223 int err;
224 res->flags = range->flags;
225 res->parent = res->child = res->sibling = NULL;
226 res->name = np->full_name;
227
228 if (res->flags & IORESOURCE_IO) {
229 unsigned long port;
230 err = pci_register_io_range(&np->fwnode, range->cpu_addr,
231 range->size);
232 if (err)
233 goto invalid_range;
234 port = pci_address_to_pio(range->cpu_addr);
235 if (port == (unsigned long)-1) {
236 err = -EINVAL;
237 goto invalid_range;
238 }
239 start = port;
240 } else {
241 start = range->cpu_addr;
242 }
243 return __of_address_resource_bounds(res, start, range->size);
244
245 invalid_range:
246 res->start = (resource_size_t)OF_BAD_ADDR;
247 res->end = (resource_size_t)OF_BAD_ADDR;
248 return err;
249 }
250 EXPORT_SYMBOL(of_pci_range_to_resource);
251
252 /*
253 * of_range_to_resource - Create a resource from a ranges entry
254 * @np: device node where the range belongs to
255 * @index: the 'ranges' index to convert to a resource
256 * @res: pointer to a valid resource that will be updated to
257 * reflect the values contained in the range.
258 *
259 * Returns -ENOENT if the entry is not found or -EOVERFLOW if the range
260 * cannot be converted to resource.
261 */
of_range_to_resource(struct device_node * np,int index,struct resource * res)262 int of_range_to_resource(struct device_node *np, int index, struct resource *res)
263 {
264 int ret, i = 0;
265 struct of_range_parser parser;
266 struct of_range range;
267
268 ret = of_range_parser_init(&parser, np);
269 if (ret)
270 return ret;
271
272 for_each_of_range(&parser, &range)
273 if (i++ == index)
274 return of_pci_range_to_resource(&range, np, res);
275
276 return -ENOENT;
277 }
278 EXPORT_SYMBOL(of_range_to_resource);
279
280 /*
281 * ISA bus specific translator
282 */
283
of_bus_isa_match(struct device_node * np)284 static int of_bus_isa_match(struct device_node *np)
285 {
286 return of_node_name_eq(np, "isa");
287 }
288
of_bus_isa_count_cells(struct device_node * child,int * addrc,int * sizec)289 static void of_bus_isa_count_cells(struct device_node *child,
290 int *addrc, int *sizec)
291 {
292 if (addrc)
293 *addrc = 2;
294 if (sizec)
295 *sizec = 1;
296 }
297
of_bus_isa_map(__be32 * addr,const __be32 * range,int na,int ns,int pna,int fna)298 static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
299 int pna, int fna)
300 {
301 /* Check address type match */
302 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
303 return OF_BAD_ADDR;
304
305 return of_bus_default_map(addr, range, na, ns, pna, fna);
306 }
307
of_bus_isa_get_flags(const __be32 * addr)308 static unsigned int of_bus_isa_get_flags(const __be32 *addr)
309 {
310 unsigned int flags = 0;
311 u32 w = be32_to_cpup(addr);
312
313 if (w & 1)
314 flags |= IORESOURCE_IO;
315 else
316 flags |= IORESOURCE_MEM;
317 return flags;
318 }
319
of_bus_default_flags_match(struct device_node * np)320 static int of_bus_default_flags_match(struct device_node *np)
321 {
322 /*
323 * Check for presence first since of_bus_n_addr_cells() will warn when
324 * walking parent nodes.
325 */
326 return of_property_present(np, "#address-cells") && (of_bus_n_addr_cells(np) == 3);
327 }
328
of_bus_default_match(struct device_node * np)329 static int of_bus_default_match(struct device_node *np)
330 {
331 /*
332 * Check for presence first since of_bus_n_addr_cells() will warn when
333 * walking parent nodes.
334 */
335 return of_property_present(np, "#address-cells");
336 }
337
338 /*
339 * Array of bus specific translators
340 */
341
342 static const struct of_bus of_busses[] = {
343 #ifdef CONFIG_PCI
344 /* PCI */
345 {
346 .name = "pci",
347 .addresses = "assigned-addresses",
348 .match = of_bus_pci_match,
349 .count_cells = of_bus_pci_count_cells,
350 .map = of_bus_pci_map,
351 .translate = of_bus_default_flags_translate,
352 .flag_cells = 1,
353 .get_flags = of_bus_pci_get_flags,
354 },
355 #endif /* CONFIG_PCI */
356 /* ISA */
357 {
358 .name = "isa",
359 .addresses = "reg",
360 .match = of_bus_isa_match,
361 .count_cells = of_bus_isa_count_cells,
362 .map = of_bus_isa_map,
363 .translate = of_bus_default_flags_translate,
364 .flag_cells = 1,
365 .get_flags = of_bus_isa_get_flags,
366 },
367 /* Default with flags cell */
368 {
369 .name = "default-flags",
370 .addresses = "reg",
371 .match = of_bus_default_flags_match,
372 .count_cells = of_bus_default_count_cells,
373 .map = of_bus_default_flags_map,
374 .translate = of_bus_default_flags_translate,
375 .flag_cells = 1,
376 .get_flags = of_bus_default_flags_get_flags,
377 },
378 /* Default */
379 {
380 .name = "default",
381 .addresses = "reg",
382 .match = of_bus_default_match,
383 .count_cells = of_bus_default_count_cells,
384 .map = of_bus_default_map,
385 .translate = of_bus_default_translate,
386 .get_flags = of_bus_default_get_flags,
387 },
388 };
389
of_match_bus(struct device_node * np)390 static const struct of_bus *of_match_bus(struct device_node *np)
391 {
392 int i;
393
394 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
395 if (!of_busses[i].match || of_busses[i].match(np))
396 return &of_busses[i];
397 return NULL;
398 }
399
of_empty_ranges_quirk(const struct device_node * np)400 static int of_empty_ranges_quirk(const struct device_node *np)
401 {
402 if (IS_ENABLED(CONFIG_PPC)) {
403 /* To save cycles, we cache the result for global "Mac" setting */
404 static int quirk_state = -1;
405
406 /* PA-SEMI sdc DT bug */
407 if (of_device_is_compatible(np, "1682m-sdc"))
408 return true;
409
410 /* Make quirk cached */
411 if (quirk_state < 0)
412 quirk_state =
413 of_machine_is_compatible("Power Macintosh") ||
414 of_machine_is_compatible("MacRISC");
415 return quirk_state;
416 }
417 return false;
418 }
419
of_translate_one(const struct device_node * parent,const struct of_bus * bus,const struct of_bus * pbus,__be32 * addr,int na,int ns,int pna,const char * rprop)420 static int of_translate_one(const struct device_node *parent, const struct of_bus *bus,
421 const struct of_bus *pbus, __be32 *addr,
422 int na, int ns, int pna, const char *rprop)
423 {
424 const __be32 *ranges;
425 unsigned int rlen;
426 int rone;
427 u64 offset = OF_BAD_ADDR;
428
429 /*
430 * Normally, an absence of a "ranges" property means we are
431 * crossing a non-translatable boundary, and thus the addresses
432 * below the current cannot be converted to CPU physical ones.
433 * Unfortunately, while this is very clear in the spec, it's not
434 * what Apple understood, and they do have things like /uni-n or
435 * /ht nodes with no "ranges" property and a lot of perfectly
436 * useable mapped devices below them. Thus we treat the absence of
437 * "ranges" as equivalent to an empty "ranges" property which means
438 * a 1:1 translation at that level. It's up to the caller not to try
439 * to translate addresses that aren't supposed to be translated in
440 * the first place. --BenH.
441 *
442 * As far as we know, this damage only exists on Apple machines, so
443 * This code is only enabled on powerpc. --gcl
444 *
445 * This quirk also applies for 'dma-ranges' which frequently exist in
446 * child nodes without 'dma-ranges' in the parent nodes. --RobH
447 */
448 ranges = of_get_property(parent, rprop, &rlen);
449 if (ranges == NULL && !of_empty_ranges_quirk(parent) &&
450 strcmp(rprop, "dma-ranges")) {
451 pr_debug("no ranges; cannot translate\n");
452 return 1;
453 }
454 if (ranges == NULL || rlen == 0) {
455 offset = of_read_number(addr, na);
456 /* set address to zero, pass flags through */
457 memset(addr + pbus->flag_cells, 0, (pna - pbus->flag_cells) * 4);
458 pr_debug("empty ranges; 1:1 translation\n");
459 goto finish;
460 }
461
462 pr_debug("walking ranges...\n");
463
464 /* Now walk through the ranges */
465 rlen /= 4;
466 rone = na + pna + ns;
467 for (; rlen >= rone; rlen -= rone, ranges += rone) {
468 offset = bus->map(addr, ranges, na, ns, pna, bus->flag_cells);
469 if (offset != OF_BAD_ADDR)
470 break;
471 }
472 if (offset == OF_BAD_ADDR) {
473 pr_debug("not found !\n");
474 return 1;
475 }
476 memcpy(addr, ranges + na, 4 * pna);
477
478 finish:
479 of_dump_addr("parent translation for:", addr, pna);
480 pr_debug("with offset: %llx\n", offset);
481
482 /* Translate it into parent bus space */
483 return pbus->translate(addr, offset, pna);
484 }
485
486 /*
487 * Translate an address from the device-tree into a CPU physical address,
488 * this walks up the tree and applies the various bus mappings on the
489 * way.
490 *
491 * Note: We consider that crossing any level with #size-cells == 0 to mean
492 * that translation is impossible (that is we are not dealing with a value
493 * that can be mapped to a cpu physical address). This is not really specified
494 * that way, but this is traditionally the way IBM at least do things
495 *
496 * Whenever the translation fails, the *host pointer will be set to the
497 * device that had registered logical PIO mapping, and the return code is
498 * relative to that node.
499 */
__of_translate_address(struct device_node * node,struct device_node * (* get_parent)(const struct device_node *),const __be32 * in_addr,const char * rprop,struct device_node ** host)500 static u64 __of_translate_address(struct device_node *node,
501 struct device_node *(*get_parent)(const struct device_node *),
502 const __be32 *in_addr, const char *rprop,
503 struct device_node **host)
504 {
505 struct device_node *dev __free(device_node) = of_node_get(node);
506 struct device_node *parent __free(device_node) = get_parent(dev);
507 const struct of_bus *bus, *pbus;
508 __be32 addr[OF_MAX_ADDR_CELLS];
509 int na, ns, pna, pns;
510
511 pr_debug("** translation for device %pOF **\n", dev);
512
513 *host = NULL;
514
515 if (parent == NULL)
516 return OF_BAD_ADDR;
517 bus = of_match_bus(parent);
518 if (!bus)
519 return OF_BAD_ADDR;
520
521 /* Count address cells & copy address locally */
522 bus->count_cells(dev, &na, &ns);
523 if (!OF_CHECK_COUNTS(na, ns)) {
524 pr_debug("Bad cell count for %pOF\n", dev);
525 return OF_BAD_ADDR;
526 }
527 memcpy(addr, in_addr, na * 4);
528
529 pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",
530 bus->name, na, ns, parent);
531 of_dump_addr("translating address:", addr, na);
532
533 /* Translate */
534 for (;;) {
535 struct logic_pio_hwaddr *iorange;
536
537 /* Switch to parent bus */
538 of_node_put(dev);
539 dev = parent;
540 parent = get_parent(dev);
541
542 /* If root, we have finished */
543 if (parent == NULL) {
544 pr_debug("reached root node\n");
545 return of_read_number(addr, na);
546 }
547
548 /*
549 * For indirectIO device which has no ranges property, get
550 * the address from reg directly.
551 */
552 iorange = find_io_range_by_fwnode(&dev->fwnode);
553 if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) {
554 u64 result = of_read_number(addr + 1, na - 1);
555 pr_debug("indirectIO matched(%pOF) 0x%llx\n",
556 dev, result);
557 *host = no_free_ptr(dev);
558 return result;
559 }
560
561 /* Get new parent bus and counts */
562 pbus = of_match_bus(parent);
563 if (!pbus)
564 return OF_BAD_ADDR;
565 pbus->count_cells(dev, &pna, &pns);
566 if (!OF_CHECK_COUNTS(pna, pns)) {
567 pr_err("Bad cell count for %pOF\n", dev);
568 return OF_BAD_ADDR;
569 }
570
571 pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
572 pbus->name, pna, pns, parent);
573
574 /* Apply bus translation */
575 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
576 return OF_BAD_ADDR;
577
578 /* Complete the move up one level */
579 na = pna;
580 ns = pns;
581 bus = pbus;
582
583 of_dump_addr("one level translation:", addr, na);
584 }
585
586 unreachable();
587 }
588
of_translate_address(struct device_node * dev,const __be32 * in_addr)589 u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
590 {
591 struct device_node *host;
592 u64 ret;
593
594 ret = __of_translate_address(dev, of_get_parent,
595 in_addr, "ranges", &host);
596 if (host) {
597 of_node_put(host);
598 return OF_BAD_ADDR;
599 }
600
601 return ret;
602 }
603 EXPORT_SYMBOL(of_translate_address);
604
605 #ifdef CONFIG_HAS_DMA
__of_get_dma_parent(const struct device_node * np)606 struct device_node *__of_get_dma_parent(const struct device_node *np)
607 {
608 struct of_phandle_args args;
609 int ret, index;
610
611 index = of_property_match_string(np, "interconnect-names", "dma-mem");
612 if (index < 0)
613 return of_get_parent(np);
614
615 ret = of_parse_phandle_with_args(np, "interconnects",
616 "#interconnect-cells",
617 index, &args);
618 if (ret < 0)
619 return of_get_parent(np);
620
621 return args.np;
622 }
623 #endif
624
of_get_next_dma_parent(struct device_node * np)625 static struct device_node *of_get_next_dma_parent(struct device_node *np)
626 {
627 struct device_node *parent;
628
629 parent = __of_get_dma_parent(np);
630 of_node_put(np);
631
632 return parent;
633 }
634
of_translate_dma_address(struct device_node * dev,const __be32 * in_addr)635 u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
636 {
637 struct device_node *host;
638 u64 ret;
639
640 ret = __of_translate_address(dev, __of_get_dma_parent,
641 in_addr, "dma-ranges", &host);
642
643 if (host) {
644 of_node_put(host);
645 return OF_BAD_ADDR;
646 }
647
648 return ret;
649 }
650 EXPORT_SYMBOL(of_translate_dma_address);
651
652 /**
653 * of_translate_dma_region - Translate device tree address and size tuple
654 * @dev: device tree node for which to translate
655 * @prop: pointer into array of cells
656 * @start: return value for the start of the DMA range
657 * @length: return value for the length of the DMA range
658 *
659 * Returns a pointer to the cell immediately following the translated DMA region.
660 */
of_translate_dma_region(struct device_node * dev,const __be32 * prop,phys_addr_t * start,size_t * length)661 const __be32 *of_translate_dma_region(struct device_node *dev, const __be32 *prop,
662 phys_addr_t *start, size_t *length)
663 {
664 struct device_node *parent __free(device_node) = __of_get_dma_parent(dev);
665 u64 address, size;
666 int na, ns;
667
668 if (!parent)
669 return NULL;
670
671 na = of_bus_n_addr_cells(parent);
672 ns = of_bus_n_size_cells(parent);
673
674 address = of_translate_dma_address(dev, prop);
675 if (address == OF_BAD_ADDR)
676 return NULL;
677
678 size = of_read_number(prop + na, ns);
679
680 if (start)
681 *start = address;
682
683 if (length)
684 *length = size;
685
686 return prop + na + ns;
687 }
688 EXPORT_SYMBOL(of_translate_dma_region);
689
__of_get_address(struct device_node * dev,int index,int bar_no,u64 * size,unsigned int * flags)690 const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,
691 u64 *size, unsigned int *flags)
692 {
693 const __be32 *prop;
694 unsigned int psize;
695 struct device_node *parent __free(device_node) = of_get_parent(dev);
696 const struct of_bus *bus;
697 int onesize, i, na, ns;
698
699 if (parent == NULL)
700 return NULL;
701
702 /* match the parent's bus type */
703 bus = of_match_bus(parent);
704 if (!bus || (strcmp(bus->name, "pci") && (bar_no >= 0)))
705 return NULL;
706
707 /* Get "reg" or "assigned-addresses" property */
708 prop = of_get_property(dev, bus->addresses, &psize);
709 if (prop == NULL)
710 return NULL;
711 psize /= 4;
712
713 bus->count_cells(dev, &na, &ns);
714 if (!OF_CHECK_ADDR_COUNT(na))
715 return NULL;
716
717 onesize = na + ns;
718 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
719 u32 val = be32_to_cpu(prop[0]);
720 /* PCI bus matches on BAR number instead of index */
721 if (((bar_no >= 0) && ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0))) ||
722 ((index >= 0) && (i == index))) {
723 if (size)
724 *size = of_read_number(prop + na, ns);
725 if (flags)
726 *flags = bus->get_flags(prop);
727 return prop;
728 }
729 }
730 return NULL;
731 }
732 EXPORT_SYMBOL(__of_get_address);
733
734 /**
735 * of_property_read_reg - Retrieve the specified "reg" entry index without translating
736 * @np: device tree node for which to retrieve "reg" from
737 * @idx: "reg" entry index to read
738 * @addr: return value for the untranslated address
739 * @size: return value for the entry size
740 *
741 * Returns -EINVAL if "reg" is not found. Returns 0 on success with addr and
742 * size values filled in.
743 */
of_property_read_reg(struct device_node * np,int idx,u64 * addr,u64 * size)744 int of_property_read_reg(struct device_node *np, int idx, u64 *addr, u64 *size)
745 {
746 const __be32 *prop = of_get_address(np, idx, size, NULL);
747
748 if (!prop)
749 return -EINVAL;
750
751 *addr = of_read_number(prop, of_n_addr_cells(np));
752
753 return 0;
754 }
755 EXPORT_SYMBOL(of_property_read_reg);
756
parser_init(struct of_pci_range_parser * parser,struct device_node * node,const char * name)757 static int parser_init(struct of_pci_range_parser *parser,
758 struct device_node *node, const char *name)
759 {
760 int rlen;
761
762 parser->node = node;
763 parser->pna = of_n_addr_cells(node);
764 parser->na = of_bus_n_addr_cells(node);
765 parser->ns = of_bus_n_size_cells(node);
766 parser->dma = !strcmp(name, "dma-ranges");
767 parser->bus = of_match_bus(node);
768
769 parser->range = of_get_property(node, name, &rlen);
770 if (parser->range == NULL)
771 return -ENOENT;
772
773 parser->end = parser->range + rlen / sizeof(__be32);
774
775 return 0;
776 }
777
of_pci_range_parser_init(struct of_pci_range_parser * parser,struct device_node * node)778 int of_pci_range_parser_init(struct of_pci_range_parser *parser,
779 struct device_node *node)
780 {
781 return parser_init(parser, node, "ranges");
782 }
783 EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
784
of_pci_dma_range_parser_init(struct of_pci_range_parser * parser,struct device_node * node)785 int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
786 struct device_node *node)
787 {
788 return parser_init(parser, node, "dma-ranges");
789 }
790 EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init);
791 #define of_dma_range_parser_init of_pci_dma_range_parser_init
792
of_pci_range_parser_one(struct of_pci_range_parser * parser,struct of_pci_range * range)793 struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
794 struct of_pci_range *range)
795 {
796 int na = parser->na;
797 int ns = parser->ns;
798 int np = parser->pna + na + ns;
799 int busflag_na = parser->bus->flag_cells;
800
801 if (!range)
802 return NULL;
803
804 if (!parser->range || parser->range + np > parser->end)
805 return NULL;
806
807 range->flags = parser->bus->get_flags(parser->range);
808
809 range->bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
810
811 if (parser->dma)
812 range->cpu_addr = of_translate_dma_address(parser->node,
813 parser->range + na);
814 else
815 range->cpu_addr = of_translate_address(parser->node,
816 parser->range + na);
817
818 range->parent_bus_addr = of_read_number(parser->range + na, parser->pna);
819 range->size = of_read_number(parser->range + parser->pna + na, ns);
820
821 parser->range += np;
822
823 /* Now consume following elements while they are contiguous */
824 while (parser->range + np <= parser->end) {
825 u32 flags = 0;
826 u64 bus_addr, cpu_addr, size;
827
828 flags = parser->bus->get_flags(parser->range);
829 bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
830 if (parser->dma)
831 cpu_addr = of_translate_dma_address(parser->node,
832 parser->range + na);
833 else
834 cpu_addr = of_translate_address(parser->node,
835 parser->range + na);
836 size = of_read_number(parser->range + parser->pna + na, ns);
837
838 if (flags != range->flags)
839 break;
840 if (bus_addr != range->bus_addr + range->size ||
841 cpu_addr != range->cpu_addr + range->size)
842 break;
843
844 range->size += size;
845 parser->range += np;
846 }
847
848 return range;
849 }
850 EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
851
of_translate_ioport(struct device_node * dev,const __be32 * in_addr,u64 size)852 static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr,
853 u64 size)
854 {
855 u64 taddr;
856 unsigned long port;
857 struct device_node *host;
858
859 taddr = __of_translate_address(dev, of_get_parent,
860 in_addr, "ranges", &host);
861 if (host) {
862 /* host-specific port access */
863 port = logic_pio_trans_hwaddr(&host->fwnode, taddr, size);
864 of_node_put(host);
865 } else {
866 /* memory-mapped I/O range */
867 port = pci_address_to_pio(taddr);
868 }
869
870 if (port == (unsigned long)-1)
871 return OF_BAD_ADDR;
872
873 return port;
874 }
875
876 #ifdef CONFIG_HAS_DMA
877 /**
878 * of_dma_get_range - Get DMA range info and put it into a map array
879 * @np: device node to get DMA range info
880 * @map: dma range structure to return
881 *
882 * Look in bottom up direction for the first "dma-ranges" property
883 * and parse it. Put the information into a DMA offset map array.
884 *
885 * dma-ranges format:
886 * DMA addr (dma_addr) : naddr cells
887 * CPU addr (phys_addr_t) : pna cells
888 * size : nsize cells
889 *
890 * It returns -ENODEV if "dma-ranges" property was not found for this
891 * device in the DT.
892 */
of_dma_get_range(struct device_node * np,const struct bus_dma_region ** map)893 int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map)
894 {
895 struct device_node *node __free(device_node) = of_node_get(np);
896 const __be32 *ranges = NULL;
897 bool found_dma_ranges = false;
898 struct of_range_parser parser;
899 struct of_range range;
900 struct bus_dma_region *r;
901 int len, num_ranges = 0;
902
903 while (node) {
904 ranges = of_get_property(node, "dma-ranges", &len);
905
906 /* Ignore empty ranges, they imply no translation required */
907 if (ranges && len > 0)
908 break;
909
910 /* Once we find 'dma-ranges', then a missing one is an error */
911 if (found_dma_ranges && !ranges)
912 return -ENODEV;
913
914 found_dma_ranges = true;
915
916 node = of_get_next_dma_parent(node);
917 }
918
919 if (!node || !ranges) {
920 pr_debug("no dma-ranges found for node(%pOF)\n", np);
921 return -ENODEV;
922 }
923 of_dma_range_parser_init(&parser, node);
924 for_each_of_range(&parser, &range) {
925 if (range.cpu_addr == OF_BAD_ADDR) {
926 pr_err("translation of DMA address(%llx) to CPU address failed node(%pOF)\n",
927 range.bus_addr, node);
928 continue;
929 }
930 num_ranges++;
931 }
932
933 if (!num_ranges)
934 return -EINVAL;
935
936 r = kcalloc(num_ranges + 1, sizeof(*r), GFP_KERNEL);
937 if (!r)
938 return -ENOMEM;
939
940 /*
941 * Record all info in the generic DMA ranges array for struct device,
942 * returning an error if we don't find any parsable ranges.
943 */
944 *map = r;
945 of_dma_range_parser_init(&parser, node);
946 for_each_of_range(&parser, &range) {
947 pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
948 range.bus_addr, range.cpu_addr, range.size);
949 if (range.cpu_addr == OF_BAD_ADDR)
950 continue;
951 r->cpu_start = range.cpu_addr;
952 r->dma_start = range.bus_addr;
953 r->size = range.size;
954 r++;
955 }
956 return 0;
957 }
958 #endif /* CONFIG_HAS_DMA */
959
960 /**
961 * of_dma_get_max_cpu_address - Gets highest CPU address suitable for DMA
962 * @np: The node to start searching from or NULL to start from the root
963 *
964 * Gets the highest CPU physical address that is addressable by all DMA masters
965 * in the sub-tree pointed by np, or the whole tree if NULL is passed. If no
966 * DMA constrained device is found, it returns PHYS_ADDR_MAX.
967 */
of_dma_get_max_cpu_address(struct device_node * np)968 phys_addr_t __init of_dma_get_max_cpu_address(struct device_node *np)
969 {
970 phys_addr_t max_cpu_addr = PHYS_ADDR_MAX;
971 struct of_range_parser parser;
972 phys_addr_t subtree_max_addr;
973 struct device_node *child;
974 struct of_range range;
975 const __be32 *ranges;
976 u64 cpu_end = 0;
977 int len;
978
979 if (!np)
980 np = of_root;
981
982 ranges = of_get_property(np, "dma-ranges", &len);
983 if (ranges && len) {
984 of_dma_range_parser_init(&parser, np);
985 for_each_of_range(&parser, &range)
986 if (range.cpu_addr + range.size > cpu_end)
987 cpu_end = range.cpu_addr + range.size - 1;
988
989 if (max_cpu_addr > cpu_end)
990 max_cpu_addr = cpu_end;
991 }
992
993 for_each_available_child_of_node(np, child) {
994 subtree_max_addr = of_dma_get_max_cpu_address(child);
995 if (max_cpu_addr > subtree_max_addr)
996 max_cpu_addr = subtree_max_addr;
997 }
998
999 return max_cpu_addr;
1000 }
1001
1002 /**
1003 * of_dma_is_coherent - Check if device is coherent
1004 * @np: device node
1005 *
1006 * It returns true if "dma-coherent" property was found
1007 * for this device in the DT, or if DMA is coherent by
1008 * default for OF devices on the current platform and no
1009 * "dma-noncoherent" property was found for this device.
1010 */
of_dma_is_coherent(struct device_node * np)1011 bool of_dma_is_coherent(struct device_node *np)
1012 {
1013 struct device_node *node __free(device_node) = of_node_get(np);
1014
1015 while (node) {
1016 if (of_property_read_bool(node, "dma-coherent"))
1017 return true;
1018
1019 if (of_property_read_bool(node, "dma-noncoherent"))
1020 return false;
1021
1022 node = of_get_next_dma_parent(node);
1023 }
1024 return dma_default_coherent;
1025 }
1026 EXPORT_SYMBOL_GPL(of_dma_is_coherent);
1027
1028 /**
1029 * of_mmio_is_nonposted - Check if device uses non-posted MMIO
1030 * @np: device node
1031 *
1032 * Returns true if the "nonposted-mmio" property was found for
1033 * the device's bus.
1034 *
1035 * This is currently only enabled on builds that support Apple ARM devices, as
1036 * an optimization.
1037 */
of_mmio_is_nonposted(const struct device_node * np)1038 static bool of_mmio_is_nonposted(const struct device_node *np)
1039 {
1040 if (!IS_ENABLED(CONFIG_ARCH_APPLE))
1041 return false;
1042
1043 struct device_node *parent __free(device_node) = of_get_parent(np);
1044 if (!parent)
1045 return false;
1046
1047 return of_property_read_bool(parent, "nonposted-mmio");
1048 }
1049
__of_address_to_resource(struct device_node * dev,int index,int bar_no,struct resource * r)1050 static int __of_address_to_resource(struct device_node *dev, int index, int bar_no,
1051 struct resource *r)
1052 {
1053 u64 taddr;
1054 const __be32 *addrp;
1055 u64 size;
1056 unsigned int flags;
1057 const char *name = NULL;
1058
1059 addrp = __of_get_address(dev, index, bar_no, &size, &flags);
1060 if (addrp == NULL)
1061 return -EINVAL;
1062
1063 /* Get optional "reg-names" property to add a name to a resource */
1064 if (index >= 0)
1065 of_property_read_string_index(dev, "reg-names", index, &name);
1066
1067 if (flags & IORESOURCE_MEM)
1068 taddr = of_translate_address(dev, addrp);
1069 else if (flags & IORESOURCE_IO)
1070 taddr = of_translate_ioport(dev, addrp, size);
1071 else
1072 return -EINVAL;
1073
1074 if (taddr == OF_BAD_ADDR)
1075 return -EINVAL;
1076 memset(r, 0, sizeof(struct resource));
1077
1078 if (of_mmio_is_nonposted(dev))
1079 flags |= IORESOURCE_MEM_NONPOSTED;
1080
1081 r->flags = flags;
1082 r->name = name ? name : dev->full_name;
1083
1084 return __of_address_resource_bounds(r, taddr, size);
1085 }
1086
1087 /**
1088 * of_address_to_resource - Translate device tree address and return as resource
1089 * @dev: Caller's Device Node
1090 * @index: Index into the array
1091 * @r: Pointer to resource array
1092 *
1093 * Returns -EINVAL if the range cannot be converted to resource.
1094 *
1095 * Note that if your address is a PIO address, the conversion will fail if
1096 * the physical address can't be internally converted to an IO token with
1097 * pci_address_to_pio(), that is because it's either called too early or it
1098 * can't be matched to any host bridge IO space
1099 */
of_address_to_resource(struct device_node * dev,int index,struct resource * r)1100 int of_address_to_resource(struct device_node *dev, int index,
1101 struct resource *r)
1102 {
1103 return __of_address_to_resource(dev, index, -1, r);
1104 }
1105 EXPORT_SYMBOL_GPL(of_address_to_resource);
1106
of_pci_address_to_resource(struct device_node * dev,int bar,struct resource * r)1107 int of_pci_address_to_resource(struct device_node *dev, int bar,
1108 struct resource *r)
1109 {
1110
1111 if (!IS_ENABLED(CONFIG_PCI))
1112 return -ENOSYS;
1113
1114 return __of_address_to_resource(dev, -1, bar, r);
1115 }
1116 EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
1117
1118 /**
1119 * of_iomap - Maps the memory mapped IO for a given device_node
1120 * @np: the device whose io range will be mapped
1121 * @index: index of the io range
1122 *
1123 * Returns a pointer to the mapped memory
1124 */
of_iomap(struct device_node * np,int index)1125 void __iomem *of_iomap(struct device_node *np, int index)
1126 {
1127 struct resource res;
1128
1129 if (of_address_to_resource(np, index, &res))
1130 return NULL;
1131
1132 if (res.flags & IORESOURCE_MEM_NONPOSTED)
1133 return ioremap_np(res.start, resource_size(&res));
1134 else
1135 return ioremap(res.start, resource_size(&res));
1136 }
1137 EXPORT_SYMBOL(of_iomap);
1138
1139 /*
1140 * of_io_request_and_map - Requests a resource and maps the memory mapped IO
1141 * for a given device_node
1142 * @device: the device whose io range will be mapped
1143 * @index: index of the io range
1144 * @name: name "override" for the memory region request or NULL
1145 *
1146 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
1147 * error code on failure. Usage example:
1148 *
1149 * base = of_io_request_and_map(node, 0, "foo");
1150 * if (IS_ERR(base))
1151 * return PTR_ERR(base);
1152 */
of_io_request_and_map(struct device_node * np,int index,const char * name)1153 void __iomem *of_io_request_and_map(struct device_node *np, int index,
1154 const char *name)
1155 {
1156 struct resource res;
1157 void __iomem *mem;
1158
1159 if (of_address_to_resource(np, index, &res))
1160 return IOMEM_ERR_PTR(-EINVAL);
1161
1162 if (!name)
1163 name = res.name;
1164 if (!request_mem_region(res.start, resource_size(&res), name))
1165 return IOMEM_ERR_PTR(-EBUSY);
1166
1167 if (res.flags & IORESOURCE_MEM_NONPOSTED)
1168 mem = ioremap_np(res.start, resource_size(&res));
1169 else
1170 mem = ioremap(res.start, resource_size(&res));
1171
1172 if (!mem) {
1173 release_mem_region(res.start, resource_size(&res));
1174 return IOMEM_ERR_PTR(-ENOMEM);
1175 }
1176
1177 return mem;
1178 }
1179 EXPORT_SYMBOL(of_io_request_and_map);
1180