1 // SPDX-License-Identifier: GPL-2.0
2 #define pr_fmt(fmt) "OF: " fmt
4 #include <linux/device.h>
5 #include <linux/fwnode.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/pci.h>
12 #include <linux/pci_regs.h>
13 #include <linux/sizes.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
16 #include <linux/dma-direct.h> /* for bus_dma_region */
18 #include "of_private.h"
20 /* Max address size we deal with */
21 #define OF_MAX_ADDR_CELLS 4
22 #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
23 #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
25 static struct of_bus *of_match_bus(struct device_node *np);
26 static int __of_address_to_resource(struct device_node *dev,
27 const __be32 *addrp, u64 size, unsigned int flags,
28 const char *name, struct resource *r);
29 static bool of_mmio_is_nonposted(struct device_node *np);
33 static void of_dump_addr(const char *s, const __be32 *addr, int na)
37 pr_cont(" %08x", be32_to_cpu(*(addr++)));
41 static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
44 /* Callbacks for bus specific translators */
47 const char *addresses;
48 int (*match)(struct device_node *parent);
49 void (*count_cells)(struct device_node *child,
50 int *addrc, int *sizec);
51 u64 (*map)(__be32 *addr, const __be32 *range,
52 int na, int ns, int pna);
53 int (*translate)(__be32 *addr, u64 offset, int na);
55 unsigned int (*get_flags)(const __be32 *addr);
59 * Default translator (generic bus)
62 static void of_bus_default_count_cells(struct device_node *dev,
63 int *addrc, int *sizec)
66 *addrc = of_n_addr_cells(dev);
68 *sizec = of_n_size_cells(dev);
71 static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
72 int na, int ns, int pna)
76 cp = of_read_number(range, na);
77 s = of_read_number(range + na + pna, ns);
78 da = of_read_number(addr, na);
80 pr_debug("default map, cp=%llx, s=%llx, da=%llx\n",
81 (unsigned long long)cp, (unsigned long long)s,
82 (unsigned long long)da);
84 if (da < cp || da >= (cp + s))
89 static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
91 u64 a = of_read_number(addr, na);
92 memset(addr, 0, na * 4);
95 addr[na - 2] = cpu_to_be32(a >> 32);
96 addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
101 static unsigned int of_bus_default_get_flags(const __be32 *addr)
103 return IORESOURCE_MEM;
107 static unsigned int of_bus_pci_get_flags(const __be32 *addr)
109 unsigned int flags = 0;
110 u32 w = be32_to_cpup(addr);
112 if (!IS_ENABLED(CONFIG_PCI))
115 switch((w >> 24) & 0x03) {
117 flags |= IORESOURCE_IO;
119 case 0x02: /* 32 bits */
120 flags |= IORESOURCE_MEM;
123 case 0x03: /* 64 bits */
124 flags |= IORESOURCE_MEM | IORESOURCE_MEM_64;
128 flags |= IORESOURCE_PREFETCH;
133 * PCI bus specific translator
136 static bool of_node_is_pcie(struct device_node *np)
138 bool is_pcie = of_node_name_eq(np, "pcie");
141 pr_warn_once("%pOF: Missing device_type\n", np);
146 static int of_bus_pci_match(struct device_node *np)
149 * "pciex" is PCI Express
150 * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
151 * "ht" is hypertransport
153 * If none of the device_type match, and that the node name is
154 * "pcie", accept the device as PCI (with a warning).
156 return of_node_is_type(np, "pci") || of_node_is_type(np, "pciex") ||
157 of_node_is_type(np, "vci") || of_node_is_type(np, "ht") ||
161 static void of_bus_pci_count_cells(struct device_node *np,
162 int *addrc, int *sizec)
170 static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
176 af = of_bus_pci_get_flags(addr);
177 rf = of_bus_pci_get_flags(range);
179 /* Check address type match */
180 if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
183 /* Read address values, skipping high cell */
184 cp = of_read_number(range + 1, na - 1);
185 s = of_read_number(range + na + pna, ns);
186 da = of_read_number(addr + 1, na - 1);
188 pr_debug("PCI map, cp=%llx, s=%llx, da=%llx\n",
189 (unsigned long long)cp, (unsigned long long)s,
190 (unsigned long long)da);
192 if (da < cp || da >= (cp + s))
197 static int of_bus_pci_translate(__be32 *addr, u64 offset, int na)
199 return of_bus_default_translate(addr + 1, offset, na - 1);
202 const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
207 struct device_node *parent;
209 int onesize, i, na, ns;
211 /* Get parent & match bus type */
212 parent = of_get_parent(dev);
215 bus = of_match_bus(parent);
216 if (strcmp(bus->name, "pci")) {
220 bus->count_cells(dev, &na, &ns);
222 if (!OF_CHECK_ADDR_COUNT(na))
225 /* Get "reg" or "assigned-addresses" property */
226 prop = of_get_property(dev, bus->addresses, &psize);
232 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
233 u32 val = be32_to_cpu(prop[0]);
234 if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
236 *size = of_read_number(prop + na, ns);
238 *flags = bus->get_flags(prop);
244 EXPORT_SYMBOL(of_get_pci_address);
246 int of_pci_address_to_resource(struct device_node *dev, int bar,
253 addrp = of_get_pci_address(dev, bar, &size, &flags);
256 return __of_address_to_resource(dev, addrp, size, flags, NULL, r);
258 EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
261 * of_pci_range_to_resource - Create a resource from an of_pci_range
262 * @range: the PCI range that describes the resource
263 * @np: device node where the range belongs to
264 * @res: pointer to a valid resource that will be updated to
265 * reflect the values contained in the range.
267 * Returns EINVAL if the range cannot be converted to resource.
269 * Note that if the range is an IO range, the resource will be converted
270 * using pci_address_to_pio() which can fail if it is called too early or
271 * if the range cannot be matched to any host bridge IO space (our case here).
272 * To guard against that we try to register the IO range first.
273 * If that fails we know that pci_address_to_pio() will do too.
275 int of_pci_range_to_resource(struct of_pci_range *range,
276 struct device_node *np, struct resource *res)
279 res->flags = range->flags;
280 res->parent = res->child = res->sibling = NULL;
281 res->name = np->full_name;
283 if (res->flags & IORESOURCE_IO) {
285 err = pci_register_io_range(&np->fwnode, range->cpu_addr,
289 port = pci_address_to_pio(range->cpu_addr);
290 if (port == (unsigned long)-1) {
296 if ((sizeof(resource_size_t) < 8) &&
297 upper_32_bits(range->cpu_addr)) {
302 res->start = range->cpu_addr;
304 res->end = res->start + range->size - 1;
308 res->start = (resource_size_t)OF_BAD_ADDR;
309 res->end = (resource_size_t)OF_BAD_ADDR;
312 EXPORT_SYMBOL(of_pci_range_to_resource);
313 #endif /* CONFIG_PCI */
316 * ISA bus specific translator
319 static int of_bus_isa_match(struct device_node *np)
321 return of_node_name_eq(np, "isa");
324 static void of_bus_isa_count_cells(struct device_node *child,
325 int *addrc, int *sizec)
333 static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
338 /* Check address type match */
339 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
342 /* Read address values, skipping high cell */
343 cp = of_read_number(range + 1, na - 1);
344 s = of_read_number(range + na + pna, ns);
345 da = of_read_number(addr + 1, na - 1);
347 pr_debug("ISA map, cp=%llx, s=%llx, da=%llx\n",
348 (unsigned long long)cp, (unsigned long long)s,
349 (unsigned long long)da);
351 if (da < cp || da >= (cp + s))
356 static int of_bus_isa_translate(__be32 *addr, u64 offset, int na)
358 return of_bus_default_translate(addr + 1, offset, na - 1);
361 static unsigned int of_bus_isa_get_flags(const __be32 *addr)
363 unsigned int flags = 0;
364 u32 w = be32_to_cpup(addr);
367 flags |= IORESOURCE_IO;
369 flags |= IORESOURCE_MEM;
374 * Array of bus specific translators
377 static struct of_bus of_busses[] = {
382 .addresses = "assigned-addresses",
383 .match = of_bus_pci_match,
384 .count_cells = of_bus_pci_count_cells,
385 .map = of_bus_pci_map,
386 .translate = of_bus_pci_translate,
388 .get_flags = of_bus_pci_get_flags,
390 #endif /* CONFIG_PCI */
395 .match = of_bus_isa_match,
396 .count_cells = of_bus_isa_count_cells,
397 .map = of_bus_isa_map,
398 .translate = of_bus_isa_translate,
400 .get_flags = of_bus_isa_get_flags,
407 .count_cells = of_bus_default_count_cells,
408 .map = of_bus_default_map,
409 .translate = of_bus_default_translate,
410 .get_flags = of_bus_default_get_flags,
414 static struct of_bus *of_match_bus(struct device_node *np)
418 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
419 if (!of_busses[i].match || of_busses[i].match(np))
420 return &of_busses[i];
425 static int of_empty_ranges_quirk(struct device_node *np)
427 if (IS_ENABLED(CONFIG_PPC)) {
428 /* To save cycles, we cache the result for global "Mac" setting */
429 static int quirk_state = -1;
431 /* PA-SEMI sdc DT bug */
432 if (of_device_is_compatible(np, "1682m-sdc"))
435 /* Make quirk cached */
438 of_machine_is_compatible("Power Macintosh") ||
439 of_machine_is_compatible("MacRISC");
445 static int of_translate_one(struct device_node *parent, struct of_bus *bus,
446 struct of_bus *pbus, __be32 *addr,
447 int na, int ns, int pna, const char *rprop)
449 const __be32 *ranges;
452 u64 offset = OF_BAD_ADDR;
455 * Normally, an absence of a "ranges" property means we are
456 * crossing a non-translatable boundary, and thus the addresses
457 * below the current cannot be converted to CPU physical ones.
458 * Unfortunately, while this is very clear in the spec, it's not
459 * what Apple understood, and they do have things like /uni-n or
460 * /ht nodes with no "ranges" property and a lot of perfectly
461 * useable mapped devices below them. Thus we treat the absence of
462 * "ranges" as equivalent to an empty "ranges" property which means
463 * a 1:1 translation at that level. It's up to the caller not to try
464 * to translate addresses that aren't supposed to be translated in
465 * the first place. --BenH.
467 * As far as we know, this damage only exists on Apple machines, so
468 * This code is only enabled on powerpc. --gcl
470 * This quirk also applies for 'dma-ranges' which frequently exist in
471 * child nodes without 'dma-ranges' in the parent nodes. --RobH
473 ranges = of_get_property(parent, rprop, &rlen);
474 if (ranges == NULL && !of_empty_ranges_quirk(parent) &&
475 strcmp(rprop, "dma-ranges")) {
476 pr_debug("no ranges; cannot translate\n");
479 if (ranges == NULL || rlen == 0) {
480 offset = of_read_number(addr, na);
481 memset(addr, 0, pna * 4);
482 pr_debug("empty ranges; 1:1 translation\n");
486 pr_debug("walking ranges...\n");
488 /* Now walk through the ranges */
490 rone = na + pna + ns;
491 for (; rlen >= rone; rlen -= rone, ranges += rone) {
492 offset = bus->map(addr, ranges, na, ns, pna);
493 if (offset != OF_BAD_ADDR)
496 if (offset == OF_BAD_ADDR) {
497 pr_debug("not found !\n");
500 memcpy(addr, ranges + na, 4 * pna);
503 of_dump_addr("parent translation for:", addr, pna);
504 pr_debug("with offset: %llx\n", (unsigned long long)offset);
506 /* Translate it into parent bus space */
507 return pbus->translate(addr, offset, pna);
511 * Translate an address from the device-tree into a CPU physical address,
512 * this walks up the tree and applies the various bus mappings on the
515 * Note: We consider that crossing any level with #size-cells == 0 to mean
516 * that translation is impossible (that is we are not dealing with a value
517 * that can be mapped to a cpu physical address). This is not really specified
518 * that way, but this is traditionally the way IBM at least do things
520 * Whenever the translation fails, the *host pointer will be set to the
521 * device that had registered logical PIO mapping, and the return code is
522 * relative to that node.
524 static u64 __of_translate_address(struct device_node *dev,
525 struct device_node *(*get_parent)(const struct device_node *),
526 const __be32 *in_addr, const char *rprop,
527 struct device_node **host)
529 struct device_node *parent = NULL;
530 struct of_bus *bus, *pbus;
531 __be32 addr[OF_MAX_ADDR_CELLS];
532 int na, ns, pna, pns;
533 u64 result = OF_BAD_ADDR;
535 pr_debug("** translation for device %pOF **\n", dev);
537 /* Increase refcount at current level */
541 /* Get parent & match bus type */
542 parent = get_parent(dev);
545 bus = of_match_bus(parent);
547 /* Count address cells & copy address locally */
548 bus->count_cells(dev, &na, &ns);
549 if (!OF_CHECK_COUNTS(na, ns)) {
550 pr_debug("Bad cell count for %pOF\n", dev);
553 memcpy(addr, in_addr, na * 4);
555 pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",
556 bus->name, na, ns, parent);
557 of_dump_addr("translating address:", addr, na);
561 struct logic_pio_hwaddr *iorange;
563 /* Switch to parent bus */
566 parent = get_parent(dev);
568 /* If root, we have finished */
569 if (parent == NULL) {
570 pr_debug("reached root node\n");
571 result = of_read_number(addr, na);
576 * For indirectIO device which has no ranges property, get
577 * the address from reg directly.
579 iorange = find_io_range_by_fwnode(&dev->fwnode);
580 if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) {
581 result = of_read_number(addr + 1, na - 1);
582 pr_debug("indirectIO matched(%pOF) 0x%llx\n",
584 *host = of_node_get(dev);
588 /* Get new parent bus and counts */
589 pbus = of_match_bus(parent);
590 pbus->count_cells(dev, &pna, &pns);
591 if (!OF_CHECK_COUNTS(pna, pns)) {
592 pr_err("Bad cell count for %pOF\n", dev);
596 pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
597 pbus->name, pna, pns, parent);
599 /* Apply bus translation */
600 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
603 /* Complete the move up one level */
608 of_dump_addr("one level translation:", addr, na);
617 u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
619 struct device_node *host;
622 ret = __of_translate_address(dev, of_get_parent,
623 in_addr, "ranges", &host);
631 EXPORT_SYMBOL(of_translate_address);
633 static struct device_node *__of_get_dma_parent(const struct device_node *np)
635 struct of_phandle_args args;
638 index = of_property_match_string(np, "interconnect-names", "dma-mem");
640 return of_get_parent(np);
642 ret = of_parse_phandle_with_args(np, "interconnects",
643 "#interconnect-cells",
646 return of_get_parent(np);
648 return of_node_get(args.np);
651 static struct device_node *of_get_next_dma_parent(struct device_node *np)
653 struct device_node *parent;
655 parent = __of_get_dma_parent(np);
661 u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
663 struct device_node *host;
666 ret = __of_translate_address(dev, __of_get_dma_parent,
667 in_addr, "dma-ranges", &host);
676 EXPORT_SYMBOL(of_translate_dma_address);
678 const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,
683 struct device_node *parent;
685 int onesize, i, na, ns;
687 /* Get parent & match bus type */
688 parent = of_get_parent(dev);
691 bus = of_match_bus(parent);
692 bus->count_cells(dev, &na, &ns);
694 if (!OF_CHECK_ADDR_COUNT(na))
697 /* Get "reg" or "assigned-addresses" property */
698 prop = of_get_property(dev, bus->addresses, &psize);
704 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
707 *size = of_read_number(prop + na, ns);
709 *flags = bus->get_flags(prop);
714 EXPORT_SYMBOL(of_get_address);
716 static int parser_init(struct of_pci_range_parser *parser,
717 struct device_node *node, const char *name)
722 parser->pna = of_n_addr_cells(node);
723 parser->na = of_bus_n_addr_cells(node);
724 parser->ns = of_bus_n_size_cells(node);
725 parser->dma = !strcmp(name, "dma-ranges");
726 parser->bus = of_match_bus(node);
728 parser->range = of_get_property(node, name, &rlen);
729 if (parser->range == NULL)
732 parser->end = parser->range + rlen / sizeof(__be32);
737 int of_pci_range_parser_init(struct of_pci_range_parser *parser,
738 struct device_node *node)
740 return parser_init(parser, node, "ranges");
742 EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
744 int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
745 struct device_node *node)
747 return parser_init(parser, node, "dma-ranges");
749 EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init);
750 #define of_dma_range_parser_init of_pci_dma_range_parser_init
752 struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
753 struct of_pci_range *range)
757 int np = parser->pna + na + ns;
763 if (!parser->range || parser->range + np > parser->end)
766 range->flags = parser->bus->get_flags(parser->range);
768 /* A extra cell for resource flags */
769 if (parser->bus->has_flags)
772 range->bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
775 range->cpu_addr = of_translate_dma_address(parser->node,
778 range->cpu_addr = of_translate_address(parser->node,
780 range->size = of_read_number(parser->range + parser->pna + na, ns);
784 /* Now consume following elements while they are contiguous */
785 while (parser->range + np <= parser->end) {
787 u64 bus_addr, cpu_addr, size;
789 flags = parser->bus->get_flags(parser->range);
790 bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
792 cpu_addr = of_translate_dma_address(parser->node,
795 cpu_addr = of_translate_address(parser->node,
797 size = of_read_number(parser->range + parser->pna + na, ns);
799 if (flags != range->flags)
801 if (bus_addr != range->bus_addr + range->size ||
802 cpu_addr != range->cpu_addr + range->size)
811 EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
813 static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr,
818 struct device_node *host;
820 taddr = __of_translate_address(dev, of_get_parent,
821 in_addr, "ranges", &host);
823 /* host-specific port access */
824 port = logic_pio_trans_hwaddr(&host->fwnode, taddr, size);
827 /* memory-mapped I/O range */
828 port = pci_address_to_pio(taddr);
831 if (port == (unsigned long)-1)
837 static int __of_address_to_resource(struct device_node *dev,
838 const __be32 *addrp, u64 size, unsigned int flags,
839 const char *name, struct resource *r)
843 if (flags & IORESOURCE_MEM)
844 taddr = of_translate_address(dev, addrp);
845 else if (flags & IORESOURCE_IO)
846 taddr = of_translate_ioport(dev, addrp, size);
850 if (taddr == OF_BAD_ADDR)
852 memset(r, 0, sizeof(struct resource));
854 if (of_mmio_is_nonposted(dev))
855 flags |= IORESOURCE_MEM_NONPOSTED;
858 r->end = taddr + size - 1;
860 r->name = name ? name : dev->full_name;
866 * of_address_to_resource - Translate device tree address and return as resource
867 * @dev: Caller's Device Node
868 * @index: Index into the array
869 * @r: Pointer to resource array
871 * Note that if your address is a PIO address, the conversion will fail if
872 * the physical address can't be internally converted to an IO token with
873 * pci_address_to_pio(), that is because it's either called too early or it
874 * can't be matched to any host bridge IO space
876 int of_address_to_resource(struct device_node *dev, int index,
882 const char *name = NULL;
884 addrp = of_get_address(dev, index, &size, &flags);
888 /* Get optional "reg-names" property to add a name to a resource */
889 of_property_read_string_index(dev, "reg-names", index, &name);
891 return __of_address_to_resource(dev, addrp, size, flags, name, r);
893 EXPORT_SYMBOL_GPL(of_address_to_resource);
896 * of_iomap - Maps the memory mapped IO for a given device_node
897 * @np: the device whose io range will be mapped
898 * @index: index of the io range
900 * Returns a pointer to the mapped memory
902 void __iomem *of_iomap(struct device_node *np, int index)
906 if (of_address_to_resource(np, index, &res))
909 if (res.flags & IORESOURCE_MEM_NONPOSTED)
910 return ioremap_np(res.start, resource_size(&res));
912 return ioremap(res.start, resource_size(&res));
914 EXPORT_SYMBOL(of_iomap);
917 * of_io_request_and_map - Requests a resource and maps the memory mapped IO
918 * for a given device_node
919 * @device: the device whose io range will be mapped
920 * @index: index of the io range
921 * @name: name "override" for the memory region request or NULL
923 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
924 * error code on failure. Usage example:
926 * base = of_io_request_and_map(node, 0, "foo");
928 * return PTR_ERR(base);
930 void __iomem *of_io_request_and_map(struct device_node *np, int index,
936 if (of_address_to_resource(np, index, &res))
937 return IOMEM_ERR_PTR(-EINVAL);
941 if (!request_mem_region(res.start, resource_size(&res), name))
942 return IOMEM_ERR_PTR(-EBUSY);
944 if (res.flags & IORESOURCE_MEM_NONPOSTED)
945 mem = ioremap_np(res.start, resource_size(&res));
947 mem = ioremap(res.start, resource_size(&res));
950 release_mem_region(res.start, resource_size(&res));
951 return IOMEM_ERR_PTR(-ENOMEM);
956 EXPORT_SYMBOL(of_io_request_and_map);
958 #ifdef CONFIG_HAS_DMA
960 * of_dma_get_range - Get DMA range info and put it into a map array
961 * @np: device node to get DMA range info
962 * @map: dma range structure to return
964 * Look in bottom up direction for the first "dma-ranges" property
965 * and parse it. Put the information into a DMA offset map array.
968 * DMA addr (dma_addr) : naddr cells
969 * CPU addr (phys_addr_t) : pna cells
972 * It returns -ENODEV if "dma-ranges" property was not found for this
975 int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map)
977 struct device_node *node = of_node_get(np);
978 const __be32 *ranges = NULL;
979 bool found_dma_ranges = false;
980 struct of_range_parser parser;
981 struct of_range range;
982 struct bus_dma_region *r;
983 int len, num_ranges = 0;
987 ranges = of_get_property(node, "dma-ranges", &len);
989 /* Ignore empty ranges, they imply no translation required */
990 if (ranges && len > 0)
993 /* Once we find 'dma-ranges', then a missing one is an error */
994 if (found_dma_ranges && !ranges) {
998 found_dma_ranges = true;
1000 node = of_get_next_dma_parent(node);
1003 if (!node || !ranges) {
1004 pr_debug("no dma-ranges found for node(%pOF)\n", np);
1009 of_dma_range_parser_init(&parser, node);
1010 for_each_of_range(&parser, &range)
1013 r = kcalloc(num_ranges + 1, sizeof(*r), GFP_KERNEL);
1020 * Record all info in the generic DMA ranges array for struct device.
1023 of_dma_range_parser_init(&parser, node);
1024 for_each_of_range(&parser, &range) {
1025 pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
1026 range.bus_addr, range.cpu_addr, range.size);
1027 if (range.cpu_addr == OF_BAD_ADDR) {
1028 pr_err("translation of DMA address(%llx) to CPU address failed node(%pOF)\n",
1029 range.bus_addr, node);
1032 r->cpu_start = range.cpu_addr;
1033 r->dma_start = range.bus_addr;
1034 r->size = range.size;
1035 r->offset = range.cpu_addr - range.bus_addr;
1042 #endif /* CONFIG_HAS_DMA */
1045 * of_dma_get_max_cpu_address - Gets highest CPU address suitable for DMA
1046 * @np: The node to start searching from or NULL to start from the root
1048 * Gets the highest CPU physical address that is addressable by all DMA masters
1049 * in the sub-tree pointed by np, or the whole tree if NULL is passed. If no
1050 * DMA constrained device is found, it returns PHYS_ADDR_MAX.
1052 phys_addr_t __init of_dma_get_max_cpu_address(struct device_node *np)
1054 phys_addr_t max_cpu_addr = PHYS_ADDR_MAX;
1055 struct of_range_parser parser;
1056 phys_addr_t subtree_max_addr;
1057 struct device_node *child;
1058 struct of_range range;
1059 const __be32 *ranges;
1066 ranges = of_get_property(np, "dma-ranges", &len);
1067 if (ranges && len) {
1068 of_dma_range_parser_init(&parser, np);
1069 for_each_of_range(&parser, &range)
1070 if (range.cpu_addr + range.size > cpu_end)
1071 cpu_end = range.cpu_addr + range.size - 1;
1073 if (max_cpu_addr > cpu_end)
1074 max_cpu_addr = cpu_end;
1077 for_each_available_child_of_node(np, child) {
1078 subtree_max_addr = of_dma_get_max_cpu_address(child);
1079 if (max_cpu_addr > subtree_max_addr)
1080 max_cpu_addr = subtree_max_addr;
1083 return max_cpu_addr;
1087 * of_dma_is_coherent - Check if device is coherent
1090 * It returns true if "dma-coherent" property was found
1091 * for this device in the DT, or if DMA is coherent by
1092 * default for OF devices on the current platform.
1094 bool of_dma_is_coherent(struct device_node *np)
1096 struct device_node *node;
1098 if (IS_ENABLED(CONFIG_OF_DMA_DEFAULT_COHERENT))
1101 node = of_node_get(np);
1104 if (of_property_read_bool(node, "dma-coherent")) {
1108 node = of_get_next_dma_parent(node);
1113 EXPORT_SYMBOL_GPL(of_dma_is_coherent);
1116 * of_mmio_is_nonposted - Check if device uses non-posted MMIO
1119 * Returns true if the "nonposted-mmio" property was found for
1122 * This is currently only enabled on builds that support Apple ARM devices, as
1125 static bool of_mmio_is_nonposted(struct device_node *np)
1127 struct device_node *parent;
1130 if (!IS_ENABLED(CONFIG_ARCH_APPLE))
1133 parent = of_get_parent(np);
1137 nonposted = of_property_read_bool(parent, "nonposted-mmio");
1139 of_node_put(parent);