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 case 0x03: /* 64 bits */
121 flags |= IORESOURCE_MEM;
125 flags |= IORESOURCE_PREFETCH;
130 * PCI bus specific translator
133 static bool of_node_is_pcie(struct device_node *np)
135 bool is_pcie = of_node_name_eq(np, "pcie");
138 pr_warn_once("%pOF: Missing device_type\n", np);
143 static int of_bus_pci_match(struct device_node *np)
146 * "pciex" is PCI Express
147 * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
148 * "ht" is hypertransport
150 * If none of the device_type match, and that the node name is
151 * "pcie", accept the device as PCI (with a warning).
153 return of_node_is_type(np, "pci") || of_node_is_type(np, "pciex") ||
154 of_node_is_type(np, "vci") || of_node_is_type(np, "ht") ||
158 static void of_bus_pci_count_cells(struct device_node *np,
159 int *addrc, int *sizec)
167 static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
173 af = of_bus_pci_get_flags(addr);
174 rf = of_bus_pci_get_flags(range);
176 /* Check address type match */
177 if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
180 /* Read address values, skipping high cell */
181 cp = of_read_number(range + 1, na - 1);
182 s = of_read_number(range + na + pna, ns);
183 da = of_read_number(addr + 1, na - 1);
185 pr_debug("PCI map, cp=%llx, s=%llx, da=%llx\n",
186 (unsigned long long)cp, (unsigned long long)s,
187 (unsigned long long)da);
189 if (da < cp || da >= (cp + s))
194 static int of_bus_pci_translate(__be32 *addr, u64 offset, int na)
196 return of_bus_default_translate(addr + 1, offset, na - 1);
199 const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
204 struct device_node *parent;
206 int onesize, i, na, ns;
208 /* Get parent & match bus type */
209 parent = of_get_parent(dev);
212 bus = of_match_bus(parent);
213 if (strcmp(bus->name, "pci")) {
217 bus->count_cells(dev, &na, &ns);
219 if (!OF_CHECK_ADDR_COUNT(na))
222 /* Get "reg" or "assigned-addresses" property */
223 prop = of_get_property(dev, bus->addresses, &psize);
229 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
230 u32 val = be32_to_cpu(prop[0]);
231 if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
233 *size = of_read_number(prop + na, ns);
235 *flags = bus->get_flags(prop);
241 EXPORT_SYMBOL(of_get_pci_address);
243 int of_pci_address_to_resource(struct device_node *dev, int bar,
250 addrp = of_get_pci_address(dev, bar, &size, &flags);
253 return __of_address_to_resource(dev, addrp, size, flags, NULL, r);
255 EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
258 * of_pci_range_to_resource - Create a resource from an of_pci_range
259 * @range: the PCI range that describes the resource
260 * @np: device node where the range belongs to
261 * @res: pointer to a valid resource that will be updated to
262 * reflect the values contained in the range.
264 * Returns EINVAL if the range cannot be converted to resource.
266 * Note that if the range is an IO range, the resource will be converted
267 * using pci_address_to_pio() which can fail if it is called too early or
268 * if the range cannot be matched to any host bridge IO space (our case here).
269 * To guard against that we try to register the IO range first.
270 * If that fails we know that pci_address_to_pio() will do too.
272 int of_pci_range_to_resource(struct of_pci_range *range,
273 struct device_node *np, struct resource *res)
276 res->flags = range->flags;
277 res->parent = res->child = res->sibling = NULL;
278 res->name = np->full_name;
280 if (res->flags & IORESOURCE_IO) {
282 err = pci_register_io_range(&np->fwnode, range->cpu_addr,
286 port = pci_address_to_pio(range->cpu_addr);
287 if (port == (unsigned long)-1) {
293 if ((sizeof(resource_size_t) < 8) &&
294 upper_32_bits(range->cpu_addr)) {
299 res->start = range->cpu_addr;
301 res->end = res->start + range->size - 1;
305 res->start = (resource_size_t)OF_BAD_ADDR;
306 res->end = (resource_size_t)OF_BAD_ADDR;
309 EXPORT_SYMBOL(of_pci_range_to_resource);
310 #endif /* CONFIG_PCI */
313 * ISA bus specific translator
316 static int of_bus_isa_match(struct device_node *np)
318 return of_node_name_eq(np, "isa");
321 static void of_bus_isa_count_cells(struct device_node *child,
322 int *addrc, int *sizec)
330 static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
335 /* Check address type match */
336 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
339 /* Read address values, skipping high cell */
340 cp = of_read_number(range + 1, na - 1);
341 s = of_read_number(range + na + pna, ns);
342 da = of_read_number(addr + 1, na - 1);
344 pr_debug("ISA map, cp=%llx, s=%llx, da=%llx\n",
345 (unsigned long long)cp, (unsigned long long)s,
346 (unsigned long long)da);
348 if (da < cp || da >= (cp + s))
353 static int of_bus_isa_translate(__be32 *addr, u64 offset, int na)
355 return of_bus_default_translate(addr + 1, offset, na - 1);
358 static unsigned int of_bus_isa_get_flags(const __be32 *addr)
360 unsigned int flags = 0;
361 u32 w = be32_to_cpup(addr);
364 flags |= IORESOURCE_IO;
366 flags |= IORESOURCE_MEM;
371 * Array of bus specific translators
374 static struct of_bus of_busses[] = {
379 .addresses = "assigned-addresses",
380 .match = of_bus_pci_match,
381 .count_cells = of_bus_pci_count_cells,
382 .map = of_bus_pci_map,
383 .translate = of_bus_pci_translate,
385 .get_flags = of_bus_pci_get_flags,
387 #endif /* CONFIG_PCI */
392 .match = of_bus_isa_match,
393 .count_cells = of_bus_isa_count_cells,
394 .map = of_bus_isa_map,
395 .translate = of_bus_isa_translate,
397 .get_flags = of_bus_isa_get_flags,
404 .count_cells = of_bus_default_count_cells,
405 .map = of_bus_default_map,
406 .translate = of_bus_default_translate,
407 .get_flags = of_bus_default_get_flags,
411 static struct of_bus *of_match_bus(struct device_node *np)
415 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
416 if (!of_busses[i].match || of_busses[i].match(np))
417 return &of_busses[i];
422 static int of_empty_ranges_quirk(struct device_node *np)
424 if (IS_ENABLED(CONFIG_PPC)) {
425 /* To save cycles, we cache the result for global "Mac" setting */
426 static int quirk_state = -1;
428 /* PA-SEMI sdc DT bug */
429 if (of_device_is_compatible(np, "1682m-sdc"))
432 /* Make quirk cached */
435 of_machine_is_compatible("Power Macintosh") ||
436 of_machine_is_compatible("MacRISC");
442 static int of_translate_one(struct device_node *parent, struct of_bus *bus,
443 struct of_bus *pbus, __be32 *addr,
444 int na, int ns, int pna, const char *rprop)
446 const __be32 *ranges;
449 u64 offset = OF_BAD_ADDR;
452 * Normally, an absence of a "ranges" property means we are
453 * crossing a non-translatable boundary, and thus the addresses
454 * below the current cannot be converted to CPU physical ones.
455 * Unfortunately, while this is very clear in the spec, it's not
456 * what Apple understood, and they do have things like /uni-n or
457 * /ht nodes with no "ranges" property and a lot of perfectly
458 * useable mapped devices below them. Thus we treat the absence of
459 * "ranges" as equivalent to an empty "ranges" property which means
460 * a 1:1 translation at that level. It's up to the caller not to try
461 * to translate addresses that aren't supposed to be translated in
462 * the first place. --BenH.
464 * As far as we know, this damage only exists on Apple machines, so
465 * This code is only enabled on powerpc. --gcl
467 * This quirk also applies for 'dma-ranges' which frequently exist in
468 * child nodes without 'dma-ranges' in the parent nodes. --RobH
470 ranges = of_get_property(parent, rprop, &rlen);
471 if (ranges == NULL && !of_empty_ranges_quirk(parent) &&
472 strcmp(rprop, "dma-ranges")) {
473 pr_debug("no ranges; cannot translate\n");
476 if (ranges == NULL || rlen == 0) {
477 offset = of_read_number(addr, na);
478 memset(addr, 0, pna * 4);
479 pr_debug("empty ranges; 1:1 translation\n");
483 pr_debug("walking ranges...\n");
485 /* Now walk through the ranges */
487 rone = na + pna + ns;
488 for (; rlen >= rone; rlen -= rone, ranges += rone) {
489 offset = bus->map(addr, ranges, na, ns, pna);
490 if (offset != OF_BAD_ADDR)
493 if (offset == OF_BAD_ADDR) {
494 pr_debug("not found !\n");
497 memcpy(addr, ranges + na, 4 * pna);
500 of_dump_addr("parent translation for:", addr, pna);
501 pr_debug("with offset: %llx\n", (unsigned long long)offset);
503 /* Translate it into parent bus space */
504 return pbus->translate(addr, offset, pna);
508 * Translate an address from the device-tree into a CPU physical address,
509 * this walks up the tree and applies the various bus mappings on the
512 * Note: We consider that crossing any level with #size-cells == 0 to mean
513 * that translation is impossible (that is we are not dealing with a value
514 * that can be mapped to a cpu physical address). This is not really specified
515 * that way, but this is traditionally the way IBM at least do things
517 * Whenever the translation fails, the *host pointer will be set to the
518 * device that had registered logical PIO mapping, and the return code is
519 * relative to that node.
521 static u64 __of_translate_address(struct device_node *dev,
522 struct device_node *(*get_parent)(const struct device_node *),
523 const __be32 *in_addr, const char *rprop,
524 struct device_node **host)
526 struct device_node *parent = NULL;
527 struct of_bus *bus, *pbus;
528 __be32 addr[OF_MAX_ADDR_CELLS];
529 int na, ns, pna, pns;
530 u64 result = OF_BAD_ADDR;
532 pr_debug("** translation for device %pOF **\n", dev);
534 /* Increase refcount at current level */
538 /* Get parent & match bus type */
539 parent = get_parent(dev);
542 bus = of_match_bus(parent);
544 /* Count address cells & copy address locally */
545 bus->count_cells(dev, &na, &ns);
546 if (!OF_CHECK_COUNTS(na, ns)) {
547 pr_debug("Bad cell count for %pOF\n", dev);
550 memcpy(addr, in_addr, na * 4);
552 pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",
553 bus->name, na, ns, parent);
554 of_dump_addr("translating address:", addr, na);
558 struct logic_pio_hwaddr *iorange;
560 /* Switch to parent bus */
563 parent = get_parent(dev);
565 /* If root, we have finished */
566 if (parent == NULL) {
567 pr_debug("reached root node\n");
568 result = of_read_number(addr, na);
573 * For indirectIO device which has no ranges property, get
574 * the address from reg directly.
576 iorange = find_io_range_by_fwnode(&dev->fwnode);
577 if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) {
578 result = of_read_number(addr + 1, na - 1);
579 pr_debug("indirectIO matched(%pOF) 0x%llx\n",
581 *host = of_node_get(dev);
585 /* Get new parent bus and counts */
586 pbus = of_match_bus(parent);
587 pbus->count_cells(dev, &pna, &pns);
588 if (!OF_CHECK_COUNTS(pna, pns)) {
589 pr_err("Bad cell count for %pOF\n", dev);
593 pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
594 pbus->name, pna, pns, parent);
596 /* Apply bus translation */
597 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
600 /* Complete the move up one level */
605 of_dump_addr("one level translation:", addr, na);
614 u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
616 struct device_node *host;
619 ret = __of_translate_address(dev, of_get_parent,
620 in_addr, "ranges", &host);
628 EXPORT_SYMBOL(of_translate_address);
630 static struct device_node *__of_get_dma_parent(const struct device_node *np)
632 struct of_phandle_args args;
635 index = of_property_match_string(np, "interconnect-names", "dma-mem");
637 return of_get_parent(np);
639 ret = of_parse_phandle_with_args(np, "interconnects",
640 "#interconnect-cells",
643 return of_get_parent(np);
645 return of_node_get(args.np);
648 static struct device_node *of_get_next_dma_parent(struct device_node *np)
650 struct device_node *parent;
652 parent = __of_get_dma_parent(np);
658 u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
660 struct device_node *host;
663 ret = __of_translate_address(dev, __of_get_dma_parent,
664 in_addr, "dma-ranges", &host);
673 EXPORT_SYMBOL(of_translate_dma_address);
675 const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,
680 struct device_node *parent;
682 int onesize, i, na, ns;
684 /* Get parent & match bus type */
685 parent = of_get_parent(dev);
688 bus = of_match_bus(parent);
689 bus->count_cells(dev, &na, &ns);
691 if (!OF_CHECK_ADDR_COUNT(na))
694 /* Get "reg" or "assigned-addresses" property */
695 prop = of_get_property(dev, bus->addresses, &psize);
701 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
704 *size = of_read_number(prop + na, ns);
706 *flags = bus->get_flags(prop);
711 EXPORT_SYMBOL(of_get_address);
713 static int parser_init(struct of_pci_range_parser *parser,
714 struct device_node *node, const char *name)
719 parser->pna = of_n_addr_cells(node);
720 parser->na = of_bus_n_addr_cells(node);
721 parser->ns = of_bus_n_size_cells(node);
722 parser->dma = !strcmp(name, "dma-ranges");
723 parser->bus = of_match_bus(node);
725 parser->range = of_get_property(node, name, &rlen);
726 if (parser->range == NULL)
729 parser->end = parser->range + rlen / sizeof(__be32);
734 int of_pci_range_parser_init(struct of_pci_range_parser *parser,
735 struct device_node *node)
737 return parser_init(parser, node, "ranges");
739 EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
741 int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
742 struct device_node *node)
744 return parser_init(parser, node, "dma-ranges");
746 EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init);
747 #define of_dma_range_parser_init of_pci_dma_range_parser_init
749 struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
750 struct of_pci_range *range)
754 int np = parser->pna + na + ns;
760 if (!parser->range || parser->range + np > parser->end)
763 range->flags = parser->bus->get_flags(parser->range);
765 /* A extra cell for resource flags */
766 if (parser->bus->has_flags)
769 range->bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
772 range->cpu_addr = of_translate_dma_address(parser->node,
775 range->cpu_addr = of_translate_address(parser->node,
777 range->size = of_read_number(parser->range + parser->pna + na, ns);
781 /* Now consume following elements while they are contiguous */
782 while (parser->range + np <= parser->end) {
784 u64 bus_addr, cpu_addr, size;
786 flags = parser->bus->get_flags(parser->range);
787 bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
789 cpu_addr = of_translate_dma_address(parser->node,
792 cpu_addr = of_translate_address(parser->node,
794 size = of_read_number(parser->range + parser->pna + na, ns);
796 if (flags != range->flags)
798 if (bus_addr != range->bus_addr + range->size ||
799 cpu_addr != range->cpu_addr + range->size)
808 EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
810 static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr,
815 struct device_node *host;
817 taddr = __of_translate_address(dev, of_get_parent,
818 in_addr, "ranges", &host);
820 /* host-specific port access */
821 port = logic_pio_trans_hwaddr(&host->fwnode, taddr, size);
824 /* memory-mapped I/O range */
825 port = pci_address_to_pio(taddr);
828 if (port == (unsigned long)-1)
834 static int __of_address_to_resource(struct device_node *dev,
835 const __be32 *addrp, u64 size, unsigned int flags,
836 const char *name, struct resource *r)
840 if (flags & IORESOURCE_MEM)
841 taddr = of_translate_address(dev, addrp);
842 else if (flags & IORESOURCE_IO)
843 taddr = of_translate_ioport(dev, addrp, size);
847 if (taddr == OF_BAD_ADDR)
849 memset(r, 0, sizeof(struct resource));
851 if (of_mmio_is_nonposted(dev))
852 flags |= IORESOURCE_MEM_NONPOSTED;
855 r->end = taddr + size - 1;
857 r->name = name ? name : dev->full_name;
863 * of_address_to_resource - Translate device tree address and return as resource
865 * Note that if your address is a PIO address, the conversion will fail if
866 * the physical address can't be internally converted to an IO token with
867 * pci_address_to_pio(), that is because it's either called too early or it
868 * can't be matched to any host bridge IO space
870 int of_address_to_resource(struct device_node *dev, int index,
876 const char *name = NULL;
878 addrp = of_get_address(dev, index, &size, &flags);
882 /* Get optional "reg-names" property to add a name to a resource */
883 of_property_read_string_index(dev, "reg-names", index, &name);
885 return __of_address_to_resource(dev, addrp, size, flags, name, r);
887 EXPORT_SYMBOL_GPL(of_address_to_resource);
890 * of_iomap - Maps the memory mapped IO for a given device_node
891 * @np: the device whose io range will be mapped
892 * @index: index of the io range
894 * Returns a pointer to the mapped memory
896 void __iomem *of_iomap(struct device_node *np, int index)
900 if (of_address_to_resource(np, index, &res))
903 if (res.flags & IORESOURCE_MEM_NONPOSTED)
904 return ioremap_np(res.start, resource_size(&res));
906 return ioremap(res.start, resource_size(&res));
908 EXPORT_SYMBOL(of_iomap);
911 * of_io_request_and_map - Requests a resource and maps the memory mapped IO
912 * for a given device_node
913 * @device: the device whose io range will be mapped
914 * @index: index of the io range
915 * @name: name "override" for the memory region request or NULL
917 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
918 * error code on failure. Usage example:
920 * base = of_io_request_and_map(node, 0, "foo");
922 * return PTR_ERR(base);
924 void __iomem *of_io_request_and_map(struct device_node *np, int index,
930 if (of_address_to_resource(np, index, &res))
931 return IOMEM_ERR_PTR(-EINVAL);
935 if (!request_mem_region(res.start, resource_size(&res), name))
936 return IOMEM_ERR_PTR(-EBUSY);
938 if (res.flags & IORESOURCE_MEM_NONPOSTED)
939 mem = ioremap_np(res.start, resource_size(&res));
941 mem = ioremap(res.start, resource_size(&res));
944 release_mem_region(res.start, resource_size(&res));
945 return IOMEM_ERR_PTR(-ENOMEM);
950 EXPORT_SYMBOL(of_io_request_and_map);
952 #ifdef CONFIG_HAS_DMA
954 * of_dma_get_range - Get DMA range info and put it into a map array
955 * @np: device node to get DMA range info
956 * @map: dma range structure to return
958 * Look in bottom up direction for the first "dma-ranges" property
959 * and parse it. Put the information into a DMA offset map array.
962 * DMA addr (dma_addr) : naddr cells
963 * CPU addr (phys_addr_t) : pna cells
966 * It returns -ENODEV if "dma-ranges" property was not found for this
969 int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map)
971 struct device_node *node = of_node_get(np);
972 const __be32 *ranges = NULL;
973 bool found_dma_ranges = false;
974 struct of_range_parser parser;
975 struct of_range range;
976 struct bus_dma_region *r;
977 int len, num_ranges = 0;
981 ranges = of_get_property(node, "dma-ranges", &len);
983 /* Ignore empty ranges, they imply no translation required */
984 if (ranges && len > 0)
987 /* Once we find 'dma-ranges', then a missing one is an error */
988 if (found_dma_ranges && !ranges) {
992 found_dma_ranges = true;
994 node = of_get_next_dma_parent(node);
997 if (!node || !ranges) {
998 pr_debug("no dma-ranges found for node(%pOF)\n", np);
1003 of_dma_range_parser_init(&parser, node);
1004 for_each_of_range(&parser, &range)
1007 r = kcalloc(num_ranges + 1, sizeof(*r), GFP_KERNEL);
1014 * Record all info in the generic DMA ranges array for struct device.
1017 of_dma_range_parser_init(&parser, node);
1018 for_each_of_range(&parser, &range) {
1019 pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
1020 range.bus_addr, range.cpu_addr, range.size);
1021 if (range.cpu_addr == OF_BAD_ADDR) {
1022 pr_err("translation of DMA address(%llx) to CPU address failed node(%pOF)\n",
1023 range.bus_addr, node);
1026 r->cpu_start = range.cpu_addr;
1027 r->dma_start = range.bus_addr;
1028 r->size = range.size;
1029 r->offset = range.cpu_addr - range.bus_addr;
1036 #endif /* CONFIG_HAS_DMA */
1039 * of_dma_get_max_cpu_address - Gets highest CPU address suitable for DMA
1040 * @np: The node to start searching from or NULL to start from the root
1042 * Gets the highest CPU physical address that is addressable by all DMA masters
1043 * in the sub-tree pointed by np, or the whole tree if NULL is passed. If no
1044 * DMA constrained device is found, it returns PHYS_ADDR_MAX.
1046 phys_addr_t __init of_dma_get_max_cpu_address(struct device_node *np)
1048 phys_addr_t max_cpu_addr = PHYS_ADDR_MAX;
1049 struct of_range_parser parser;
1050 phys_addr_t subtree_max_addr;
1051 struct device_node *child;
1052 struct of_range range;
1053 const __be32 *ranges;
1060 ranges = of_get_property(np, "dma-ranges", &len);
1061 if (ranges && len) {
1062 of_dma_range_parser_init(&parser, np);
1063 for_each_of_range(&parser, &range)
1064 if (range.cpu_addr + range.size > cpu_end)
1065 cpu_end = range.cpu_addr + range.size - 1;
1067 if (max_cpu_addr > cpu_end)
1068 max_cpu_addr = cpu_end;
1071 for_each_available_child_of_node(np, child) {
1072 subtree_max_addr = of_dma_get_max_cpu_address(child);
1073 if (max_cpu_addr > subtree_max_addr)
1074 max_cpu_addr = subtree_max_addr;
1077 return max_cpu_addr;
1081 * of_dma_is_coherent - Check if device is coherent
1084 * It returns true if "dma-coherent" property was found
1085 * for this device in the DT, or if DMA is coherent by
1086 * default for OF devices on the current platform.
1088 bool of_dma_is_coherent(struct device_node *np)
1090 struct device_node *node;
1092 if (IS_ENABLED(CONFIG_OF_DMA_DEFAULT_COHERENT))
1095 node = of_node_get(np);
1098 if (of_property_read_bool(node, "dma-coherent")) {
1102 node = of_get_next_dma_parent(node);
1107 EXPORT_SYMBOL_GPL(of_dma_is_coherent);
1110 * of_mmio_is_nonposted - Check if device uses non-posted MMIO
1113 * Returns true if the "nonposted-mmio" property was found for
1116 * This is currently only enabled on builds that support Apple ARM devices, as
1119 static bool of_mmio_is_nonposted(struct device_node *np)
1121 struct device_node *parent;
1124 if (!IS_ENABLED(CONFIG_ARCH_APPLE))
1127 parent = of_get_parent(np);
1131 nonposted = of_property_read_bool(parent, "nonposted-mmio");
1133 of_node_put(parent);