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/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 */
19 #include "of_private.h"
21 /* Max address size we deal with */
22 #define OF_MAX_ADDR_CELLS 4
23 #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
24 #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
28 static void of_dump_addr(const char *s, const __be32 *addr, int na)
32 pr_cont(" %08x", be32_to_cpu(*(addr++)));
36 static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
39 /* Callbacks for bus specific translators */
42 const char *addresses;
43 int (*match)(struct device_node *parent);
44 void (*count_cells)(struct device_node *child,
45 int *addrc, int *sizec);
46 u64 (*map)(__be32 *addr, const __be32 *range,
47 int na, int ns, int pna, int fna);
48 int (*translate)(__be32 *addr, u64 offset, int na);
50 unsigned int (*get_flags)(const __be32 *addr);
54 * Default translator (generic bus)
57 static void of_bus_default_count_cells(struct device_node *dev,
58 int *addrc, int *sizec)
61 *addrc = of_n_addr_cells(dev);
63 *sizec = of_n_size_cells(dev);
66 static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
67 int na, int ns, int pna, int fna)
71 cp = of_read_number(range + fna, na - fna);
72 s = of_read_number(range + na + pna, ns);
73 da = of_read_number(addr + fna, na - fna);
75 pr_debug("default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
77 if (da < cp || da >= (cp + s))
82 static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
84 u64 a = of_read_number(addr, na);
85 memset(addr, 0, na * 4);
88 addr[na - 2] = cpu_to_be32(a >> 32);
89 addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
94 static unsigned int of_bus_default_flags_get_flags(const __be32 *addr)
96 return of_read_number(addr, 1);
99 static unsigned int of_bus_default_get_flags(const __be32 *addr)
101 return IORESOURCE_MEM;
104 static u64 of_bus_default_flags_map(__be32 *addr, const __be32 *range, int na,
105 int ns, int pna, int fna)
107 /* Check that flags match */
111 return of_bus_default_map(addr, range, na, ns, pna, fna);
114 static int of_bus_default_flags_translate(__be32 *addr, u64 offset, int na)
116 /* Keep "flags" part (high cell) in translated address */
117 return of_bus_default_translate(addr + 1, offset, na - 1);
121 static unsigned int of_bus_pci_get_flags(const __be32 *addr)
123 unsigned int flags = 0;
124 u32 w = be32_to_cpup(addr);
126 if (!IS_ENABLED(CONFIG_PCI))
129 switch((w >> 24) & 0x03) {
131 flags |= IORESOURCE_IO;
133 case 0x02: /* 32 bits */
134 flags |= IORESOURCE_MEM;
137 case 0x03: /* 64 bits */
138 flags |= IORESOURCE_MEM | IORESOURCE_MEM_64;
142 flags |= IORESOURCE_PREFETCH;
147 * PCI bus specific translator
150 static bool of_node_is_pcie(const struct device_node *np)
152 bool is_pcie = of_node_name_eq(np, "pcie");
155 pr_warn_once("%pOF: Missing device_type\n", np);
160 static int of_bus_pci_match(struct device_node *np)
163 * "pciex" is PCI Express
164 * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
165 * "ht" is hypertransport
167 * If none of the device_type match, and that the node name is
168 * "pcie", accept the device as PCI (with a warning).
170 return of_node_is_type(np, "pci") || of_node_is_type(np, "pciex") ||
171 of_node_is_type(np, "vci") || of_node_is_type(np, "ht") ||
175 static void of_bus_pci_count_cells(struct device_node *np,
176 int *addrc, int *sizec)
184 static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
189 af = of_bus_pci_get_flags(addr);
190 rf = of_bus_pci_get_flags(range);
192 /* Check address type match */
193 if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
196 return of_bus_default_map(addr, range, na, ns, pna, fna);
199 #endif /* CONFIG_PCI */
201 static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)
205 if (overflows_type(start, r->start))
207 if (size && check_add_overflow(end, size - 1, &end))
209 if (overflows_type(end, r->end))
219 * of_pci_range_to_resource - Create a resource from an of_pci_range
220 * @range: the PCI range that describes the resource
221 * @np: device node where the range belongs to
222 * @res: pointer to a valid resource that will be updated to
223 * reflect the values contained in the range.
225 * Returns -EINVAL if the range cannot be converted to resource.
227 * Note that if the range is an IO range, the resource will be converted
228 * using pci_address_to_pio() which can fail if it is called too early or
229 * if the range cannot be matched to any host bridge IO space (our case here).
230 * To guard against that we try to register the IO range first.
231 * If that fails we know that pci_address_to_pio() will do too.
233 int of_pci_range_to_resource(const struct of_pci_range *range,
234 const struct device_node *np, struct resource *res)
238 res->flags = range->flags;
239 res->parent = res->child = res->sibling = NULL;
240 res->name = np->full_name;
242 if (res->flags & IORESOURCE_IO) {
244 err = pci_register_io_range(&np->fwnode, range->cpu_addr,
248 port = pci_address_to_pio(range->cpu_addr);
249 if (port == (unsigned long)-1) {
255 start = range->cpu_addr;
257 return __of_address_resource_bounds(res, start, range->size);
260 res->start = (resource_size_t)OF_BAD_ADDR;
261 res->end = (resource_size_t)OF_BAD_ADDR;
264 EXPORT_SYMBOL(of_pci_range_to_resource);
267 * of_range_to_resource - Create a resource from a ranges entry
268 * @np: device node where the range belongs to
269 * @index: the 'ranges' index to convert to a resource
270 * @res: pointer to a valid resource that will be updated to
271 * reflect the values contained in the range.
273 * Returns -ENOENT if the entry is not found or -EOVERFLOW if the range
274 * cannot be converted to resource.
276 int of_range_to_resource(struct device_node *np, int index, struct resource *res)
279 struct of_range_parser parser;
280 struct of_range range;
282 ret = of_range_parser_init(&parser, np);
286 for_each_of_range(&parser, &range)
288 return of_pci_range_to_resource(&range, np, res);
292 EXPORT_SYMBOL(of_range_to_resource);
295 * ISA bus specific translator
298 static int of_bus_isa_match(struct device_node *np)
300 return of_node_name_eq(np, "isa");
303 static void of_bus_isa_count_cells(struct device_node *child,
304 int *addrc, int *sizec)
312 static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
315 /* Check address type match */
316 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
319 return of_bus_default_map(addr, range, na, ns, pna, fna);
322 static unsigned int of_bus_isa_get_flags(const __be32 *addr)
324 unsigned int flags = 0;
325 u32 w = be32_to_cpup(addr);
328 flags |= IORESOURCE_IO;
330 flags |= IORESOURCE_MEM;
334 static int of_bus_default_flags_match(struct device_node *np)
337 * Check for presence first since of_bus_n_addr_cells() will warn when
338 * walking parent nodes.
340 return of_property_present(np, "#address-cells") && (of_bus_n_addr_cells(np) == 3);
344 * Array of bus specific translators
347 static const struct of_bus of_busses[] = {
352 .addresses = "assigned-addresses",
353 .match = of_bus_pci_match,
354 .count_cells = of_bus_pci_count_cells,
355 .map = of_bus_pci_map,
356 .translate = of_bus_default_flags_translate,
358 .get_flags = of_bus_pci_get_flags,
360 #endif /* CONFIG_PCI */
365 .match = of_bus_isa_match,
366 .count_cells = of_bus_isa_count_cells,
367 .map = of_bus_isa_map,
368 .translate = of_bus_default_flags_translate,
370 .get_flags = of_bus_isa_get_flags,
372 /* Default with flags cell */
374 .name = "default-flags",
376 .match = of_bus_default_flags_match,
377 .count_cells = of_bus_default_count_cells,
378 .map = of_bus_default_flags_map,
379 .translate = of_bus_default_flags_translate,
381 .get_flags = of_bus_default_flags_get_flags,
388 .count_cells = of_bus_default_count_cells,
389 .map = of_bus_default_map,
390 .translate = of_bus_default_translate,
391 .get_flags = of_bus_default_get_flags,
395 static const struct of_bus *of_match_bus(struct device_node *np)
399 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
400 if (!of_busses[i].match || of_busses[i].match(np))
401 return &of_busses[i];
406 static int of_empty_ranges_quirk(const struct device_node *np)
408 if (IS_ENABLED(CONFIG_PPC)) {
409 /* To save cycles, we cache the result for global "Mac" setting */
410 static int quirk_state = -1;
412 /* PA-SEMI sdc DT bug */
413 if (of_device_is_compatible(np, "1682m-sdc"))
416 /* Make quirk cached */
419 of_machine_is_compatible("Power Macintosh") ||
420 of_machine_is_compatible("MacRISC");
426 static int of_translate_one(const struct device_node *parent, const struct of_bus *bus,
427 const struct of_bus *pbus, __be32 *addr,
428 int na, int ns, int pna, const char *rprop)
430 const __be32 *ranges;
433 u64 offset = OF_BAD_ADDR;
436 * Normally, an absence of a "ranges" property means we are
437 * crossing a non-translatable boundary, and thus the addresses
438 * below the current cannot be converted to CPU physical ones.
439 * Unfortunately, while this is very clear in the spec, it's not
440 * what Apple understood, and they do have things like /uni-n or
441 * /ht nodes with no "ranges" property and a lot of perfectly
442 * useable mapped devices below them. Thus we treat the absence of
443 * "ranges" as equivalent to an empty "ranges" property which means
444 * a 1:1 translation at that level. It's up to the caller not to try
445 * to translate addresses that aren't supposed to be translated in
446 * the first place. --BenH.
448 * As far as we know, this damage only exists on Apple machines, so
449 * This code is only enabled on powerpc. --gcl
451 * This quirk also applies for 'dma-ranges' which frequently exist in
452 * child nodes without 'dma-ranges' in the parent nodes. --RobH
454 ranges = of_get_property(parent, rprop, &rlen);
455 if (ranges == NULL && !of_empty_ranges_quirk(parent) &&
456 strcmp(rprop, "dma-ranges")) {
457 pr_debug("no ranges; cannot translate\n");
460 if (ranges == NULL || rlen == 0) {
461 offset = of_read_number(addr, na);
462 /* set address to zero, pass flags through */
463 memset(addr + pbus->flag_cells, 0, (pna - pbus->flag_cells) * 4);
464 pr_debug("empty ranges; 1:1 translation\n");
468 pr_debug("walking ranges...\n");
470 /* Now walk through the ranges */
472 rone = na + pna + ns;
473 for (; rlen >= rone; rlen -= rone, ranges += rone) {
474 offset = bus->map(addr, ranges, na, ns, pna, bus->flag_cells);
475 if (offset != OF_BAD_ADDR)
478 if (offset == OF_BAD_ADDR) {
479 pr_debug("not found !\n");
482 memcpy(addr, ranges + na, 4 * pna);
485 of_dump_addr("parent translation for:", addr, pna);
486 pr_debug("with offset: %llx\n", offset);
488 /* Translate it into parent bus space */
489 return pbus->translate(addr, offset, pna);
493 * Translate an address from the device-tree into a CPU physical address,
494 * this walks up the tree and applies the various bus mappings on the
497 * Note: We consider that crossing any level with #size-cells == 0 to mean
498 * that translation is impossible (that is we are not dealing with a value
499 * that can be mapped to a cpu physical address). This is not really specified
500 * that way, but this is traditionally the way IBM at least do things
502 * Whenever the translation fails, the *host pointer will be set to the
503 * device that had registered logical PIO mapping, and the return code is
504 * relative to that node.
506 static u64 __of_translate_address(struct device_node *node,
507 struct device_node *(*get_parent)(const struct device_node *),
508 const __be32 *in_addr, const char *rprop,
509 struct device_node **host)
511 struct device_node *dev __free(device_node) = of_node_get(node);
512 struct device_node *parent __free(device_node) = get_parent(dev);
513 const struct of_bus *bus, *pbus;
514 __be32 addr[OF_MAX_ADDR_CELLS];
515 int na, ns, pna, pns;
517 pr_debug("** translation for device %pOF **\n", dev);
523 bus = of_match_bus(parent);
525 /* Count address cells & copy address locally */
526 bus->count_cells(dev, &na, &ns);
527 if (!OF_CHECK_COUNTS(na, ns)) {
528 pr_debug("Bad cell count for %pOF\n", dev);
531 memcpy(addr, in_addr, na * 4);
533 pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",
534 bus->name, na, ns, parent);
535 of_dump_addr("translating address:", addr, na);
539 struct logic_pio_hwaddr *iorange;
541 /* Switch to parent bus */
544 parent = get_parent(dev);
546 /* If root, we have finished */
547 if (parent == NULL) {
548 pr_debug("reached root node\n");
549 return of_read_number(addr, na);
553 * For indirectIO device which has no ranges property, get
554 * the address from reg directly.
556 iorange = find_io_range_by_fwnode(&dev->fwnode);
557 if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) {
558 u64 result = of_read_number(addr + 1, na - 1);
559 pr_debug("indirectIO matched(%pOF) 0x%llx\n",
561 *host = no_free_ptr(dev);
565 /* Get new parent bus and counts */
566 pbus = of_match_bus(parent);
567 pbus->count_cells(dev, &pna, &pns);
568 if (!OF_CHECK_COUNTS(pna, pns)) {
569 pr_err("Bad cell count for %pOF\n", dev);
573 pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
574 pbus->name, pna, pns, parent);
576 /* Apply bus translation */
577 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
580 /* Complete the move up one level */
585 of_dump_addr("one level translation:", addr, na);
591 u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
593 struct device_node *host;
596 ret = __of_translate_address(dev, of_get_parent,
597 in_addr, "ranges", &host);
605 EXPORT_SYMBOL(of_translate_address);
607 #ifdef CONFIG_HAS_DMA
608 struct device_node *__of_get_dma_parent(const struct device_node *np)
610 struct of_phandle_args args;
613 index = of_property_match_string(np, "interconnect-names", "dma-mem");
615 return of_get_parent(np);
617 ret = of_parse_phandle_with_args(np, "interconnects",
618 "#interconnect-cells",
621 return of_get_parent(np);
627 static struct device_node *of_get_next_dma_parent(struct device_node *np)
629 struct device_node *parent;
631 parent = __of_get_dma_parent(np);
637 u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
639 struct device_node *host;
642 ret = __of_translate_address(dev, __of_get_dma_parent,
643 in_addr, "dma-ranges", &host);
652 EXPORT_SYMBOL(of_translate_dma_address);
655 * of_translate_dma_region - Translate device tree address and size tuple
656 * @dev: device tree node for which to translate
657 * @prop: pointer into array of cells
658 * @start: return value for the start of the DMA range
659 * @length: return value for the length of the DMA range
661 * Returns a pointer to the cell immediately following the translated DMA region.
663 const __be32 *of_translate_dma_region(struct device_node *dev, const __be32 *prop,
664 phys_addr_t *start, size_t *length)
666 struct device_node *parent __free(device_node) = __of_get_dma_parent(dev);
673 na = of_bus_n_addr_cells(parent);
674 ns = of_bus_n_size_cells(parent);
676 address = of_translate_dma_address(dev, prop);
677 if (address == OF_BAD_ADDR)
680 size = of_read_number(prop + na, ns);
688 return prop + na + ns;
690 EXPORT_SYMBOL(of_translate_dma_region);
692 const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,
693 u64 *size, unsigned int *flags)
697 struct device_node *parent __free(device_node) = of_get_parent(dev);
698 const struct of_bus *bus;
699 int onesize, i, na, ns;
704 /* match the parent's bus type */
705 bus = of_match_bus(parent);
706 if (strcmp(bus->name, "pci") && (bar_no >= 0))
709 /* Get "reg" or "assigned-addresses" property */
710 prop = of_get_property(dev, bus->addresses, &psize);
715 bus->count_cells(dev, &na, &ns);
716 if (!OF_CHECK_ADDR_COUNT(na))
720 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
721 u32 val = be32_to_cpu(prop[0]);
722 /* PCI bus matches on BAR number instead of index */
723 if (((bar_no >= 0) && ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0))) ||
724 ((index >= 0) && (i == index))) {
726 *size = of_read_number(prop + na, ns);
728 *flags = bus->get_flags(prop);
734 EXPORT_SYMBOL(__of_get_address);
737 * of_property_read_reg - Retrieve the specified "reg" entry index without translating
738 * @np: device tree node for which to retrieve "reg" from
739 * @idx: "reg" entry index to read
740 * @addr: return value for the untranslated address
741 * @size: return value for the entry size
743 * Returns -EINVAL if "reg" is not found. Returns 0 on success with addr and
744 * size values filled in.
746 int of_property_read_reg(struct device_node *np, int idx, u64 *addr, u64 *size)
748 const __be32 *prop = of_get_address(np, idx, size, NULL);
753 *addr = of_read_number(prop, of_n_addr_cells(np));
757 EXPORT_SYMBOL(of_property_read_reg);
759 static int parser_init(struct of_pci_range_parser *parser,
760 struct device_node *node, const char *name)
765 parser->pna = of_n_addr_cells(node);
766 parser->na = of_bus_n_addr_cells(node);
767 parser->ns = of_bus_n_size_cells(node);
768 parser->dma = !strcmp(name, "dma-ranges");
769 parser->bus = of_match_bus(node);
771 parser->range = of_get_property(node, name, &rlen);
772 if (parser->range == NULL)
775 parser->end = parser->range + rlen / sizeof(__be32);
780 int of_pci_range_parser_init(struct of_pci_range_parser *parser,
781 struct device_node *node)
783 return parser_init(parser, node, "ranges");
785 EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
787 int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
788 struct device_node *node)
790 return parser_init(parser, node, "dma-ranges");
792 EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init);
793 #define of_dma_range_parser_init of_pci_dma_range_parser_init
795 struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
796 struct of_pci_range *range)
800 int np = parser->pna + na + ns;
801 int busflag_na = parser->bus->flag_cells;
806 if (!parser->range || parser->range + np > parser->end)
809 range->flags = parser->bus->get_flags(parser->range);
811 range->bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
814 range->cpu_addr = of_translate_dma_address(parser->node,
817 range->cpu_addr = of_translate_address(parser->node,
819 range->size = of_read_number(parser->range + parser->pna + na, ns);
823 /* Now consume following elements while they are contiguous */
824 while (parser->range + np <= parser->end) {
826 u64 bus_addr, cpu_addr, size;
828 flags = parser->bus->get_flags(parser->range);
829 bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
831 cpu_addr = of_translate_dma_address(parser->node,
834 cpu_addr = of_translate_address(parser->node,
836 size = of_read_number(parser->range + parser->pna + na, ns);
838 if (flags != range->flags)
840 if (bus_addr != range->bus_addr + range->size ||
841 cpu_addr != range->cpu_addr + range->size)
850 EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
852 static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr,
857 struct device_node *host;
859 taddr = __of_translate_address(dev, of_get_parent,
860 in_addr, "ranges", &host);
862 /* host-specific port access */
863 port = logic_pio_trans_hwaddr(&host->fwnode, taddr, size);
866 /* memory-mapped I/O range */
867 port = pci_address_to_pio(taddr);
870 if (port == (unsigned long)-1)
876 #ifdef CONFIG_HAS_DMA
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
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.
886 * DMA addr (dma_addr) : naddr cells
887 * CPU addr (phys_addr_t) : pna cells
890 * It returns -ENODEV if "dma-ranges" property was not found for this
893 int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map)
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;
904 ranges = of_get_property(node, "dma-ranges", &len);
906 /* Ignore empty ranges, they imply no translation required */
907 if (ranges && len > 0)
910 /* Once we find 'dma-ranges', then a missing one is an error */
911 if (found_dma_ranges && !ranges)
914 found_dma_ranges = true;
916 node = of_get_next_dma_parent(node);
919 if (!node || !ranges) {
920 pr_debug("no dma-ranges found for node(%pOF)\n", np);
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);
936 r = kcalloc(num_ranges + 1, sizeof(*r), GFP_KERNEL);
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.
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)
951 r->cpu_start = range.cpu_addr;
952 r->dma_start = range.bus_addr;
953 r->size = range.size;
958 #endif /* CONFIG_HAS_DMA */
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
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.
968 phys_addr_t __init of_dma_get_max_cpu_address(struct device_node *np)
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;
982 ranges = of_get_property(np, "dma-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;
989 if (max_cpu_addr > cpu_end)
990 max_cpu_addr = cpu_end;
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;
1003 * of_dma_is_coherent - Check if device is coherent
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.
1011 bool of_dma_is_coherent(struct device_node *np)
1013 struct device_node *node __free(device_node) = of_node_get(np);
1016 if (of_property_read_bool(node, "dma-coherent"))
1019 if (of_property_read_bool(node, "dma-noncoherent"))
1022 node = of_get_next_dma_parent(node);
1024 return dma_default_coherent;
1026 EXPORT_SYMBOL_GPL(of_dma_is_coherent);
1029 * of_mmio_is_nonposted - Check if device uses non-posted MMIO
1032 * Returns true if the "nonposted-mmio" property was found for
1035 * This is currently only enabled on builds that support Apple ARM devices, as
1038 static bool of_mmio_is_nonposted(const struct device_node *np)
1040 if (!IS_ENABLED(CONFIG_ARCH_APPLE))
1043 struct device_node *parent __free(device_node) = of_get_parent(np);
1047 return of_property_read_bool(parent, "nonposted-mmio");
1050 static int __of_address_to_resource(struct device_node *dev, int index, int bar_no,
1054 const __be32 *addrp;
1057 const char *name = NULL;
1059 addrp = __of_get_address(dev, index, bar_no, &size, &flags);
1063 /* Get optional "reg-names" property to add a name to a resource */
1065 of_property_read_string_index(dev, "reg-names", index, &name);
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);
1074 if (taddr == OF_BAD_ADDR)
1076 memset(r, 0, sizeof(struct resource));
1078 if (of_mmio_is_nonposted(dev))
1079 flags |= IORESOURCE_MEM_NONPOSTED;
1082 r->name = name ? name : dev->full_name;
1084 return __of_address_resource_bounds(r, taddr, size);
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
1093 * Returns -EINVAL if the range cannot be converted to resource.
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
1100 int of_address_to_resource(struct device_node *dev, int index,
1103 return __of_address_to_resource(dev, index, -1, r);
1105 EXPORT_SYMBOL_GPL(of_address_to_resource);
1107 int of_pci_address_to_resource(struct device_node *dev, int bar,
1111 if (!IS_ENABLED(CONFIG_PCI))
1114 return __of_address_to_resource(dev, -1, bar, r);
1116 EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
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
1123 * Returns a pointer to the mapped memory
1125 void __iomem *of_iomap(struct device_node *np, int index)
1127 struct resource res;
1129 if (of_address_to_resource(np, index, &res))
1132 if (res.flags & IORESOURCE_MEM_NONPOSTED)
1133 return ioremap_np(res.start, resource_size(&res));
1135 return ioremap(res.start, resource_size(&res));
1137 EXPORT_SYMBOL(of_iomap);
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
1146 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
1147 * error code on failure. Usage example:
1149 * base = of_io_request_and_map(node, 0, "foo");
1151 * return PTR_ERR(base);
1153 void __iomem *of_io_request_and_map(struct device_node *np, int index,
1156 struct resource res;
1159 if (of_address_to_resource(np, index, &res))
1160 return IOMEM_ERR_PTR(-EINVAL);
1164 if (!request_mem_region(res.start, resource_size(&res), name))
1165 return IOMEM_ERR_PTR(-EBUSY);
1167 if (res.flags & IORESOURCE_MEM_NONPOSTED)
1168 mem = ioremap_np(res.start, resource_size(&res));
1170 mem = ioremap(res.start, resource_size(&res));
1173 release_mem_region(res.start, resource_size(&res));
1174 return IOMEM_ERR_PTR(-ENOMEM);
1179 EXPORT_SYMBOL(of_io_request_and_map);