Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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 */
27struct 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
44static 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
53static 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
69static 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
81static unsigned int of_bus_default_flags_get_flags(const __be32 *addr)
82{
83 return of_read_number(addr, 1);
84}
85
86static unsigned int of_bus_default_get_flags(const __be32 *addr)
87{
88 return IORESOURCE_MEM;
89}
90
91static 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
101static 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
108static 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
137static 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
147static 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
162static 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
171static 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
188VISIBLE_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}
202EXPORT_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 */
219int 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
245invalid_range:
246 res->start = (resource_size_t)OF_BAD_ADDR;
247 res->end = (resource_size_t)OF_BAD_ADDR;
248 return err;
249}
250EXPORT_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 */
262int 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}
278EXPORT_SYMBOL(of_range_to_resource);
279
280/*
281 * ISA bus specific translator
282 */
283
284static int of_bus_isa_match(struct device_node *np)
285{
286 return of_node_name_eq(np, "isa");
287}
288
289static 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
298static 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
308static 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
320static 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
329static int of_bus_default_match(struct device_node *np)
330{
331 return of_property_present(np, "#address-cells");
332}
333
334/*
335 * Array of bus specific translators
336 */
337
338static const struct of_bus of_busses[] = {
339#ifdef CONFIG_PCI
340 /* PCI */
341 {
342 .name = "pci",
343 .addresses = "assigned-addresses",
344 .match = of_bus_pci_match,
345 .count_cells = of_bus_pci_count_cells,
346 .map = of_bus_pci_map,
347 .translate = of_bus_default_flags_translate,
348 .flag_cells = 1,
349 .get_flags = of_bus_pci_get_flags,
350 },
351#endif /* CONFIG_PCI */
352 /* ISA */
353 {
354 .name = "isa",
355 .addresses = "reg",
356 .match = of_bus_isa_match,
357 .count_cells = of_bus_isa_count_cells,
358 .map = of_bus_isa_map,
359 .translate = of_bus_default_flags_translate,
360 .flag_cells = 1,
361 .get_flags = of_bus_isa_get_flags,
362 },
363 /* Default with flags cell */
364 {
365 .name = "default-flags",
366 .addresses = "reg",
367 .match = of_bus_default_flags_match,
368 .count_cells = of_bus_default_count_cells,
369 .map = of_bus_default_flags_map,
370 .translate = of_bus_default_flags_translate,
371 .flag_cells = 1,
372 .get_flags = of_bus_default_flags_get_flags,
373 },
374 /* Default */
375 {
376 .name = "default",
377 .addresses = "reg",
378 .match = of_bus_default_match,
379 .count_cells = of_bus_default_count_cells,
380 .map = of_bus_default_map,
381 .translate = of_bus_default_translate,
382 .get_flags = of_bus_default_get_flags,
383 },
384};
385
386static const struct of_bus *of_match_bus(struct device_node *np)
387{
388 int i;
389
390 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
391 if (!of_busses[i].match || of_busses[i].match(np))
392 return &of_busses[i];
393 return NULL;
394}
395
396static int of_empty_ranges_quirk(const struct device_node *np)
397{
398 if (IS_ENABLED(CONFIG_PPC)) {
399 /* To save cycles, we cache the result for global "Mac" setting */
400 static int quirk_state = -1;
401
402 /* PA-SEMI sdc DT bug */
403 if (of_device_is_compatible(np, "1682m-sdc"))
404 return true;
405
406 /* Make quirk cached */
407 if (quirk_state < 0)
408 quirk_state =
409 of_machine_is_compatible("Power Macintosh") ||
410 of_machine_is_compatible("MacRISC");
411 return quirk_state;
412 }
413 return false;
414}
415
416static int of_translate_one(const struct device_node *parent, const struct of_bus *bus,
417 const struct of_bus *pbus, __be32 *addr,
418 int na, int ns, int pna, const char *rprop)
419{
420 const __be32 *ranges;
421 unsigned int rlen;
422 int rone;
423 u64 offset = OF_BAD_ADDR;
424
425 /*
426 * Normally, an absence of a "ranges" property means we are
427 * crossing a non-translatable boundary, and thus the addresses
428 * below the current cannot be converted to CPU physical ones.
429 * Unfortunately, while this is very clear in the spec, it's not
430 * what Apple understood, and they do have things like /uni-n or
431 * /ht nodes with no "ranges" property and a lot of perfectly
432 * useable mapped devices below them. Thus we treat the absence of
433 * "ranges" as equivalent to an empty "ranges" property which means
434 * a 1:1 translation at that level. It's up to the caller not to try
435 * to translate addresses that aren't supposed to be translated in
436 * the first place. --BenH.
437 *
438 * As far as we know, this damage only exists on Apple machines, so
439 * This code is only enabled on powerpc. --gcl
440 *
441 * This quirk also applies for 'dma-ranges' which frequently exist in
442 * child nodes without 'dma-ranges' in the parent nodes. --RobH
443 */
444 ranges = of_get_property(parent, rprop, &rlen);
445 if (ranges == NULL && !of_empty_ranges_quirk(parent) &&
446 strcmp(rprop, "dma-ranges")) {
447 pr_debug("no ranges; cannot translate\n");
448 return 1;
449 }
450 if (ranges == NULL || rlen == 0) {
451 offset = of_read_number(addr, na);
452 /* set address to zero, pass flags through */
453 memset(addr + pbus->flag_cells, 0, (pna - pbus->flag_cells) * 4);
454 pr_debug("empty ranges; 1:1 translation\n");
455 goto finish;
456 }
457
458 pr_debug("walking ranges...\n");
459
460 /* Now walk through the ranges */
461 rlen /= 4;
462 rone = na + pna + ns;
463 for (; rlen >= rone; rlen -= rone, ranges += rone) {
464 offset = bus->map(addr, ranges, na, ns, pna, bus->flag_cells);
465 if (offset != OF_BAD_ADDR)
466 break;
467 }
468 if (offset == OF_BAD_ADDR) {
469 pr_debug("not found !\n");
470 return 1;
471 }
472 memcpy(addr, ranges + na, 4 * pna);
473
474 finish:
475 of_dump_addr("parent translation for:", addr, pna);
476 pr_debug("with offset: %llx\n", offset);
477
478 /* Translate it into parent bus space */
479 return pbus->translate(addr, offset, pna);
480}
481
482/*
483 * Translate an address from the device-tree into a CPU physical address,
484 * this walks up the tree and applies the various bus mappings on the
485 * way.
486 *
487 * Note: We consider that crossing any level with #size-cells == 0 to mean
488 * that translation is impossible (that is we are not dealing with a value
489 * that can be mapped to a cpu physical address). This is not really specified
490 * that way, but this is traditionally the way IBM at least do things
491 *
492 * Whenever the translation fails, the *host pointer will be set to the
493 * device that had registered logical PIO mapping, and the return code is
494 * relative to that node.
495 */
496static u64 __of_translate_address(struct device_node *node,
497 struct device_node *(*get_parent)(const struct device_node *),
498 const __be32 *in_addr, const char *rprop,
499 struct device_node **host)
500{
501 struct device_node *dev __free(device_node) = of_node_get(node);
502 struct device_node *parent __free(device_node) = get_parent(dev);
503 const struct of_bus *bus, *pbus;
504 __be32 addr[OF_MAX_ADDR_CELLS];
505 int na, ns, pna, pns;
506
507 pr_debug("** translation for device %pOF **\n", dev);
508
509 *host = NULL;
510
511 if (parent == NULL)
512 return OF_BAD_ADDR;
513 bus = of_match_bus(parent);
514 if (!bus)
515 return OF_BAD_ADDR;
516
517 /* Count address cells & copy address locally */
518 bus->count_cells(dev, &na, &ns);
519 if (!OF_CHECK_COUNTS(na, ns)) {
520 pr_debug("Bad cell count for %pOF\n", dev);
521 return OF_BAD_ADDR;
522 }
523 memcpy(addr, in_addr, na * 4);
524
525 pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",
526 bus->name, na, ns, parent);
527 of_dump_addr("translating address:", addr, na);
528
529 /* Translate */
530 for (;;) {
531 struct logic_pio_hwaddr *iorange;
532
533 /* Switch to parent bus */
534 of_node_put(dev);
535 dev = parent;
536 parent = get_parent(dev);
537
538 /* If root, we have finished */
539 if (parent == NULL) {
540 pr_debug("reached root node\n");
541 return of_read_number(addr, na);
542 }
543
544 /*
545 * For indirectIO device which has no ranges property, get
546 * the address from reg directly.
547 */
548 iorange = find_io_range_by_fwnode(&dev->fwnode);
549 if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) {
550 u64 result = of_read_number(addr + 1, na - 1);
551 pr_debug("indirectIO matched(%pOF) 0x%llx\n",
552 dev, result);
553 *host = no_free_ptr(dev);
554 return result;
555 }
556
557 /* Get new parent bus and counts */
558 pbus = of_match_bus(parent);
559 if (!pbus)
560 return OF_BAD_ADDR;
561 pbus->count_cells(dev, &pna, &pns);
562 if (!OF_CHECK_COUNTS(pna, pns)) {
563 pr_err("Bad cell count for %pOF\n", dev);
564 return OF_BAD_ADDR;
565 }
566
567 pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
568 pbus->name, pna, pns, parent);
569
570 /* Apply bus translation */
571 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
572 return OF_BAD_ADDR;
573
574 /* Complete the move up one level */
575 na = pna;
576 ns = pns;
577 bus = pbus;
578
579 of_dump_addr("one level translation:", addr, na);
580 }
581
582 unreachable();
583}
584
585u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
586{
587 struct device_node *host;
588 u64 ret;
589
590 ret = __of_translate_address(dev, of_get_parent,
591 in_addr, "ranges", &host);
592 if (host) {
593 of_node_put(host);
594 return OF_BAD_ADDR;
595 }
596
597 return ret;
598}
599EXPORT_SYMBOL(of_translate_address);
600
601#ifdef CONFIG_HAS_DMA
602struct device_node *__of_get_dma_parent(const struct device_node *np)
603{
604 struct of_phandle_args args;
605 int ret, index;
606
607 index = of_property_match_string(np, "interconnect-names", "dma-mem");
608 if (index < 0)
609 return of_get_parent(np);
610
611 ret = of_parse_phandle_with_args(np, "interconnects",
612 "#interconnect-cells",
613 index, &args);
614 if (ret < 0)
615 return of_get_parent(np);
616
617 return args.np;
618}
619#endif
620
621static struct device_node *of_get_next_dma_parent(struct device_node *np)
622{
623 struct device_node *parent;
624
625 parent = __of_get_dma_parent(np);
626 of_node_put(np);
627
628 return parent;
629}
630
631u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
632{
633 struct device_node *host;
634 u64 ret;
635
636 ret = __of_translate_address(dev, __of_get_dma_parent,
637 in_addr, "dma-ranges", &host);
638
639 if (host) {
640 of_node_put(host);
641 return OF_BAD_ADDR;
642 }
643
644 return ret;
645}
646EXPORT_SYMBOL(of_translate_dma_address);
647
648/**
649 * of_translate_dma_region - Translate device tree address and size tuple
650 * @dev: device tree node for which to translate
651 * @prop: pointer into array of cells
652 * @start: return value for the start of the DMA range
653 * @length: return value for the length of the DMA range
654 *
655 * Returns a pointer to the cell immediately following the translated DMA region.
656 */
657const __be32 *of_translate_dma_region(struct device_node *dev, const __be32 *prop,
658 phys_addr_t *start, size_t *length)
659{
660 struct device_node *parent __free(device_node) = __of_get_dma_parent(dev);
661 u64 address, size;
662 int na, ns;
663
664 if (!parent)
665 return NULL;
666
667 na = of_bus_n_addr_cells(parent);
668 ns = of_bus_n_size_cells(parent);
669
670 address = of_translate_dma_address(dev, prop);
671 if (address == OF_BAD_ADDR)
672 return NULL;
673
674 size = of_read_number(prop + na, ns);
675
676 if (start)
677 *start = address;
678
679 if (length)
680 *length = size;
681
682 return prop + na + ns;
683}
684EXPORT_SYMBOL(of_translate_dma_region);
685
686const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,
687 u64 *size, unsigned int *flags)
688{
689 const __be32 *prop;
690 unsigned int psize;
691 struct device_node *parent __free(device_node) = of_get_parent(dev);
692 const struct of_bus *bus;
693 int onesize, i, na, ns;
694
695 if (parent == NULL)
696 return NULL;
697
698 /* match the parent's bus type */
699 bus = of_match_bus(parent);
700 if (!bus || (strcmp(bus->name, "pci") && (bar_no >= 0)))
701 return NULL;
702
703 /* Get "reg" or "assigned-addresses" property */
704 prop = of_get_property(dev, bus->addresses, &psize);
705 if (prop == NULL)
706 return NULL;
707 psize /= 4;
708
709 bus->count_cells(dev, &na, &ns);
710 if (!OF_CHECK_ADDR_COUNT(na))
711 return NULL;
712
713 onesize = na + ns;
714 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
715 u32 val = be32_to_cpu(prop[0]);
716 /* PCI bus matches on BAR number instead of index */
717 if (((bar_no >= 0) && ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0))) ||
718 ((index >= 0) && (i == index))) {
719 if (size)
720 *size = of_read_number(prop + na, ns);
721 if (flags)
722 *flags = bus->get_flags(prop);
723 return prop;
724 }
725 }
726 return NULL;
727}
728EXPORT_SYMBOL(__of_get_address);
729
730/**
731 * of_property_read_reg - Retrieve the specified "reg" entry index without translating
732 * @np: device tree node for which to retrieve "reg" from
733 * @idx: "reg" entry index to read
734 * @addr: return value for the untranslated address
735 * @size: return value for the entry size
736 *
737 * Returns -EINVAL if "reg" is not found. Returns 0 on success with addr and
738 * size values filled in.
739 */
740int of_property_read_reg(struct device_node *np, int idx, u64 *addr, u64 *size)
741{
742 const __be32 *prop = of_get_address(np, idx, size, NULL);
743
744 if (!prop)
745 return -EINVAL;
746
747 *addr = of_read_number(prop, of_n_addr_cells(np));
748
749 return 0;
750}
751EXPORT_SYMBOL(of_property_read_reg);
752
753static int parser_init(struct of_pci_range_parser *parser,
754 struct device_node *node, const char *name)
755{
756 int rlen;
757
758 parser->node = node;
759 parser->pna = of_n_addr_cells(node);
760 parser->na = of_bus_n_addr_cells(node);
761 parser->ns = of_bus_n_size_cells(node);
762 parser->dma = !strcmp(name, "dma-ranges");
763 parser->bus = of_match_bus(node);
764
765 parser->range = of_get_property(node, name, &rlen);
766 if (parser->range == NULL)
767 return -ENOENT;
768
769 parser->end = parser->range + rlen / sizeof(__be32);
770
771 return 0;
772}
773
774int of_pci_range_parser_init(struct of_pci_range_parser *parser,
775 struct device_node *node)
776{
777 return parser_init(parser, node, "ranges");
778}
779EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
780
781int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
782 struct device_node *node)
783{
784 return parser_init(parser, node, "dma-ranges");
785}
786EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init);
787#define of_dma_range_parser_init of_pci_dma_range_parser_init
788
789struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
790 struct of_pci_range *range)
791{
792 int na = parser->na;
793 int ns = parser->ns;
794 int np = parser->pna + na + ns;
795 int busflag_na = parser->bus->flag_cells;
796
797 if (!range)
798 return NULL;
799
800 if (!parser->range || parser->range + np > parser->end)
801 return NULL;
802
803 range->flags = parser->bus->get_flags(parser->range);
804
805 range->bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
806
807 if (parser->dma)
808 range->cpu_addr = of_translate_dma_address(parser->node,
809 parser->range + na);
810 else
811 range->cpu_addr = of_translate_address(parser->node,
812 parser->range + na);
813
814 range->parent_bus_addr = of_read_number(parser->range + na, parser->pna);
815 range->size = of_read_number(parser->range + parser->pna + na, ns);
816
817 parser->range += np;
818
819 /* Now consume following elements while they are contiguous */
820 while (parser->range + np <= parser->end) {
821 u32 flags = 0;
822 u64 bus_addr, cpu_addr, size;
823
824 flags = parser->bus->get_flags(parser->range);
825 bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
826 if (parser->dma)
827 cpu_addr = of_translate_dma_address(parser->node,
828 parser->range + na);
829 else
830 cpu_addr = of_translate_address(parser->node,
831 parser->range + na);
832 size = of_read_number(parser->range + parser->pna + na, ns);
833
834 if (flags != range->flags)
835 break;
836 if (bus_addr != range->bus_addr + range->size ||
837 cpu_addr != range->cpu_addr + range->size)
838 break;
839
840 range->size += size;
841 parser->range += np;
842 }
843
844 return range;
845}
846EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
847
848static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr,
849 u64 size)
850{
851 u64 taddr;
852 unsigned long port;
853 struct device_node *host;
854
855 taddr = __of_translate_address(dev, of_get_parent,
856 in_addr, "ranges", &host);
857 if (host) {
858 /* host-specific port access */
859 port = logic_pio_trans_hwaddr(&host->fwnode, taddr, size);
860 of_node_put(host);
861 } else {
862 /* memory-mapped I/O range */
863 port = pci_address_to_pio(taddr);
864 }
865
866 if (port == (unsigned long)-1)
867 return OF_BAD_ADDR;
868
869 return port;
870}
871
872#ifdef CONFIG_HAS_DMA
873/**
874 * of_dma_get_range - Get DMA range info and put it into a map array
875 * @np: device node to get DMA range info
876 * @map: dma range structure to return
877 *
878 * Look in bottom up direction for the first "dma-ranges" property
879 * and parse it. Put the information into a DMA offset map array.
880 *
881 * dma-ranges format:
882 * DMA addr (dma_addr) : naddr cells
883 * CPU addr (phys_addr_t) : pna cells
884 * size : nsize cells
885 *
886 * It returns -ENODEV if "dma-ranges" property was not found for this
887 * device in the DT.
888 */
889int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map)
890{
891 struct device_node *node __free(device_node) = of_node_get(np);
892 const __be32 *ranges = NULL;
893 bool found_dma_ranges = false;
894 struct of_range_parser parser;
895 struct of_range range;
896 struct bus_dma_region *r;
897 int len, num_ranges = 0;
898
899 while (node) {
900 ranges = of_get_property(node, "dma-ranges", &len);
901
902 /* Ignore empty ranges, they imply no translation required */
903 if (ranges && len > 0)
904 break;
905
906 /* Once we find 'dma-ranges', then a missing one is an error */
907 if (found_dma_ranges && !ranges)
908 return -ENODEV;
909
910 found_dma_ranges = true;
911
912 node = of_get_next_dma_parent(node);
913 }
914
915 if (!node || !ranges) {
916 pr_debug("no dma-ranges found for node(%pOF)\n", np);
917 return -ENODEV;
918 }
919 of_dma_range_parser_init(&parser, node);
920 for_each_of_range(&parser, &range) {
921 if (range.cpu_addr == OF_BAD_ADDR) {
922 pr_err("translation of DMA address(%llx) to CPU address failed node(%pOF)\n",
923 range.bus_addr, node);
924 continue;
925 }
926 num_ranges++;
927 }
928
929 if (!num_ranges)
930 return -EINVAL;
931
932 r = kcalloc(num_ranges + 1, sizeof(*r), GFP_KERNEL);
933 if (!r)
934 return -ENOMEM;
935
936 /*
937 * Record all info in the generic DMA ranges array for struct device,
938 * returning an error if we don't find any parsable ranges.
939 */
940 *map = r;
941 of_dma_range_parser_init(&parser, node);
942 for_each_of_range(&parser, &range) {
943 pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
944 range.bus_addr, range.cpu_addr, range.size);
945 if (range.cpu_addr == OF_BAD_ADDR)
946 continue;
947 r->cpu_start = range.cpu_addr;
948 r->dma_start = range.bus_addr;
949 r->size = range.size;
950 r++;
951 }
952 return 0;
953}
954#endif /* CONFIG_HAS_DMA */
955
956/**
957 * of_dma_get_max_cpu_address - Gets highest CPU address suitable for DMA
958 * @np: The node to start searching from or NULL to start from the root
959 *
960 * Gets the highest CPU physical address that is addressable by all DMA masters
961 * in the sub-tree pointed by np, or the whole tree if NULL is passed. If no
962 * DMA constrained device is found, it returns PHYS_ADDR_MAX.
963 */
964phys_addr_t __init of_dma_get_max_cpu_address(struct device_node *np)
965{
966 phys_addr_t max_cpu_addr = PHYS_ADDR_MAX;
967 struct of_range_parser parser;
968 phys_addr_t subtree_max_addr;
969 struct device_node *child;
970 struct of_range range;
971 const __be32 *ranges;
972 u64 cpu_end = 0;
973 int len;
974
975 if (!np)
976 np = of_root;
977
978 ranges = of_get_property(np, "dma-ranges", &len);
979 if (ranges && len) {
980 of_dma_range_parser_init(&parser, np);
981 for_each_of_range(&parser, &range)
982 if (range.cpu_addr + range.size > cpu_end)
983 cpu_end = range.cpu_addr + range.size - 1;
984
985 if (max_cpu_addr > cpu_end)
986 max_cpu_addr = cpu_end;
987 }
988
989 for_each_available_child_of_node(np, child) {
990 subtree_max_addr = of_dma_get_max_cpu_address(child);
991 if (max_cpu_addr > subtree_max_addr)
992 max_cpu_addr = subtree_max_addr;
993 }
994
995 return max_cpu_addr;
996}
997
998/**
999 * of_dma_is_coherent - Check if device is coherent
1000 * @np: device node
1001 *
1002 * It returns true if "dma-coherent" property was found
1003 * for this device in the DT, or if DMA is coherent by
1004 * default for OF devices on the current platform and no
1005 * "dma-noncoherent" property was found for this device.
1006 */
1007bool of_dma_is_coherent(struct device_node *np)
1008{
1009 struct device_node *node __free(device_node) = of_node_get(np);
1010
1011 while (node) {
1012 if (of_property_read_bool(node, "dma-coherent"))
1013 return true;
1014
1015 if (of_property_read_bool(node, "dma-noncoherent"))
1016 return false;
1017
1018 node = of_get_next_dma_parent(node);
1019 }
1020 return dma_default_coherent;
1021}
1022EXPORT_SYMBOL_GPL(of_dma_is_coherent);
1023
1024/**
1025 * of_mmio_is_nonposted - Check if device uses non-posted MMIO
1026 * @np: device node
1027 *
1028 * Returns true if the "nonposted-mmio" property was found for
1029 * the device's bus.
1030 */
1031static bool of_mmio_is_nonposted(const struct device_node *np)
1032{
1033 struct device_node *parent __free(device_node) = of_get_parent(np);
1034
1035 if (of_property_read_bool(np, "nonposted-mmio"))
1036 return true;
1037
1038 return parent && of_property_read_bool(parent, "nonposted-mmio");
1039}
1040
1041static int __of_address_to_resource(struct device_node *dev, int index, int bar_no,
1042 struct resource *r)
1043{
1044 u64 taddr;
1045 const __be32 *addrp;
1046 u64 size;
1047 unsigned int flags;
1048 const char *name = NULL;
1049
1050 addrp = __of_get_address(dev, index, bar_no, &size, &flags);
1051 if (addrp == NULL)
1052 return -EINVAL;
1053
1054 /* Get optional "reg-names" property to add a name to a resource */
1055 if (index >= 0)
1056 of_property_read_string_index(dev, "reg-names", index, &name);
1057
1058 if (flags & IORESOURCE_MEM)
1059 taddr = of_translate_address(dev, addrp);
1060 else if (flags & IORESOURCE_IO)
1061 taddr = of_translate_ioport(dev, addrp, size);
1062 else
1063 return -EINVAL;
1064
1065 if (taddr == OF_BAD_ADDR)
1066 return -EINVAL;
1067 memset(r, 0, sizeof(struct resource));
1068
1069 if (of_mmio_is_nonposted(dev))
1070 flags |= IORESOURCE_MEM_NONPOSTED;
1071
1072 r->flags = flags;
1073 r->name = name ? name : dev->full_name;
1074
1075 return __of_address_resource_bounds(r, taddr, size);
1076}
1077
1078/**
1079 * of_address_to_resource - Translate device tree address and return as resource
1080 * @dev: Caller's Device Node
1081 * @index: Index into the array
1082 * @r: Pointer to resource array
1083 *
1084 * Returns -EINVAL if the range cannot be converted to resource.
1085 *
1086 * Note that if your address is a PIO address, the conversion will fail if
1087 * the physical address can't be internally converted to an IO token with
1088 * pci_address_to_pio(), that is because it's either called too early or it
1089 * can't be matched to any host bridge IO space
1090 */
1091int of_address_to_resource(struct device_node *dev, int index,
1092 struct resource *r)
1093{
1094 return __of_address_to_resource(dev, index, -1, r);
1095}
1096EXPORT_SYMBOL_GPL(of_address_to_resource);
1097
1098int of_pci_address_to_resource(struct device_node *dev, int bar,
1099 struct resource *r)
1100{
1101
1102 if (!IS_ENABLED(CONFIG_PCI))
1103 return -ENOSYS;
1104
1105 return __of_address_to_resource(dev, -1, bar, r);
1106}
1107EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
1108
1109/**
1110 * of_iomap - Maps the memory mapped IO for a given device_node
1111 * @np: the device whose io range will be mapped
1112 * @index: index of the io range
1113 *
1114 * Returns a pointer to the mapped memory
1115 */
1116void __iomem *of_iomap(struct device_node *np, int index)
1117{
1118 struct resource res;
1119
1120 if (of_address_to_resource(np, index, &res))
1121 return NULL;
1122
1123 if (res.flags & IORESOURCE_MEM_NONPOSTED)
1124 return ioremap_np(res.start, resource_size(&res));
1125 else
1126 return ioremap(res.start, resource_size(&res));
1127}
1128EXPORT_SYMBOL(of_iomap);
1129
1130/*
1131 * of_io_request_and_map - Requests a resource and maps the memory mapped IO
1132 * for a given device_node
1133 * @device: the device whose io range will be mapped
1134 * @index: index of the io range
1135 * @name: name "override" for the memory region request or NULL
1136 *
1137 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
1138 * error code on failure. Usage example:
1139 *
1140 * base = of_io_request_and_map(node, 0, "foo");
1141 * if (IS_ERR(base))
1142 * return PTR_ERR(base);
1143 */
1144void __iomem *of_io_request_and_map(struct device_node *np, int index,
1145 const char *name)
1146{
1147 struct resource res;
1148 void __iomem *mem;
1149
1150 if (of_address_to_resource(np, index, &res))
1151 return IOMEM_ERR_PTR(-EINVAL);
1152
1153 if (!name)
1154 name = res.name;
1155 if (!request_mem_region(res.start, resource_size(&res), name))
1156 return IOMEM_ERR_PTR(-EBUSY);
1157
1158 if (res.flags & IORESOURCE_MEM_NONPOSTED)
1159 mem = ioremap_np(res.start, resource_size(&res));
1160 else
1161 mem = ioremap(res.start, resource_size(&res));
1162
1163 if (!mem) {
1164 release_mem_region(res.start, resource_size(&res));
1165 return IOMEM_ERR_PTR(-ENOMEM);
1166 }
1167
1168 return mem;
1169}
1170EXPORT_SYMBOL(of_io_request_and_map);