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)
27 static void of_dump_addr(const char *s, const __be32 *addr, int na)
31 pr_cont(" %08x", be32_to_cpu(*(addr++)));
35 static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
38 /* Callbacks for bus specific translators */
41 const char *addresses;
42 int (*match)(struct device_node *parent);
43 void (*count_cells)(struct device_node *child,
44 int *addrc, int *sizec);
45 u64 (*map)(__be32 *addr, const __be32 *range,
46 int na, int ns, int pna);
47 int (*translate)(__be32 *addr, u64 offset, int na);
49 unsigned int (*get_flags)(const __be32 *addr);
53 * Default translator (generic bus)
56 static void of_bus_default_count_cells(struct device_node *dev,
57 int *addrc, int *sizec)
60 *addrc = of_n_addr_cells(dev);
62 *sizec = of_n_size_cells(dev);
65 static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
66 int na, int ns, int pna)
70 cp = of_read_number(range, na);
71 s = of_read_number(range + na + pna, ns);
72 da = of_read_number(addr, na);
74 pr_debug("default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
76 if (da < cp || da >= (cp + s))
81 static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
83 u64 a = of_read_number(addr, na);
84 memset(addr, 0, na * 4);
87 addr[na - 2] = cpu_to_be32(a >> 32);
88 addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
93 static unsigned int of_bus_default_flags_get_flags(const __be32 *addr)
95 return of_read_number(addr, 1);
98 static unsigned int of_bus_default_get_flags(const __be32 *addr)
100 return IORESOURCE_MEM;
103 static u64 of_bus_default_flags_map(__be32 *addr, const __be32 *range, int na,
108 /* Check that flags match */
112 /* Read address values, skipping high cell */
113 cp = of_read_number(range + 1, na - 1);
114 s = of_read_number(range + na + pna, ns);
115 da = of_read_number(addr + 1, na - 1);
117 pr_debug("default flags map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
119 if (da < cp || da >= (cp + s))
124 static int of_bus_default_flags_translate(__be32 *addr, u64 offset, int na)
126 /* Keep "flags" part (high cell) in translated address */
127 return of_bus_default_translate(addr + 1, offset, na - 1);
131 static unsigned int of_bus_pci_get_flags(const __be32 *addr)
133 unsigned int flags = 0;
134 u32 w = be32_to_cpup(addr);
136 if (!IS_ENABLED(CONFIG_PCI))
139 switch((w >> 24) & 0x03) {
141 flags |= IORESOURCE_IO;
143 case 0x02: /* 32 bits */
144 flags |= IORESOURCE_MEM;
147 case 0x03: /* 64 bits */
148 flags |= IORESOURCE_MEM | IORESOURCE_MEM_64;
152 flags |= IORESOURCE_PREFETCH;
157 * PCI bus specific translator
160 static bool of_node_is_pcie(struct device_node *np)
162 bool is_pcie = of_node_name_eq(np, "pcie");
165 pr_warn_once("%pOF: Missing device_type\n", np);
170 static int of_bus_pci_match(struct device_node *np)
173 * "pciex" is PCI Express
174 * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
175 * "ht" is hypertransport
177 * If none of the device_type match, and that the node name is
178 * "pcie", accept the device as PCI (with a warning).
180 return of_node_is_type(np, "pci") || of_node_is_type(np, "pciex") ||
181 of_node_is_type(np, "vci") || of_node_is_type(np, "ht") ||
185 static void of_bus_pci_count_cells(struct device_node *np,
186 int *addrc, int *sizec)
194 static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
200 af = of_bus_pci_get_flags(addr);
201 rf = of_bus_pci_get_flags(range);
203 /* Check address type match */
204 if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
207 /* Read address values, skipping high cell */
208 cp = of_read_number(range + 1, na - 1);
209 s = of_read_number(range + na + pna, ns);
210 da = of_read_number(addr + 1, na - 1);
212 pr_debug("PCI map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
214 if (da < cp || da >= (cp + s))
219 static int of_bus_pci_translate(__be32 *addr, u64 offset, int na)
221 return of_bus_default_translate(addr + 1, offset, na - 1);
223 #endif /* CONFIG_PCI */
226 * of_pci_range_to_resource - Create a resource from an of_pci_range
227 * @range: the PCI range that describes the resource
228 * @np: device node where the range belongs to
229 * @res: pointer to a valid resource that will be updated to
230 * reflect the values contained in the range.
232 * Returns -EINVAL if the range cannot be converted to resource.
234 * Note that if the range is an IO range, the resource will be converted
235 * using pci_address_to_pio() which can fail if it is called too early or
236 * if the range cannot be matched to any host bridge IO space (our case here).
237 * To guard against that we try to register the IO range first.
238 * If that fails we know that pci_address_to_pio() will do too.
240 int of_pci_range_to_resource(struct of_pci_range *range,
241 struct device_node *np, struct resource *res)
244 res->flags = range->flags;
245 res->parent = res->child = res->sibling = NULL;
246 res->name = np->full_name;
248 if (res->flags & IORESOURCE_IO) {
250 err = pci_register_io_range(&np->fwnode, range->cpu_addr,
254 port = pci_address_to_pio(range->cpu_addr);
255 if (port == (unsigned long)-1) {
261 if ((sizeof(resource_size_t) < 8) &&
262 upper_32_bits(range->cpu_addr)) {
267 res->start = range->cpu_addr;
269 res->end = res->start + range->size - 1;
273 res->start = (resource_size_t)OF_BAD_ADDR;
274 res->end = (resource_size_t)OF_BAD_ADDR;
277 EXPORT_SYMBOL(of_pci_range_to_resource);
280 * of_range_to_resource - Create a resource from a ranges entry
281 * @np: device node where the range belongs to
282 * @index: the 'ranges' index to convert to a resource
283 * @res: pointer to a valid resource that will be updated to
284 * reflect the values contained in the range.
286 * Returns ENOENT if the entry is not found or EINVAL if the range cannot be
287 * converted to resource.
289 int of_range_to_resource(struct device_node *np, int index, struct resource *res)
292 struct of_range_parser parser;
293 struct of_range range;
295 ret = of_range_parser_init(&parser, np);
299 for_each_of_range(&parser, &range)
301 return of_pci_range_to_resource(&range, np, res);
305 EXPORT_SYMBOL(of_range_to_resource);
308 * ISA bus specific translator
311 static int of_bus_isa_match(struct device_node *np)
313 return of_node_name_eq(np, "isa");
316 static void of_bus_isa_count_cells(struct device_node *child,
317 int *addrc, int *sizec)
325 static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
330 /* Check address type match */
331 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
334 /* Read address values, skipping high cell */
335 cp = of_read_number(range + 1, na - 1);
336 s = of_read_number(range + na + pna, ns);
337 da = of_read_number(addr + 1, na - 1);
339 pr_debug("ISA map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
341 if (da < cp || da >= (cp + s))
346 static int of_bus_isa_translate(__be32 *addr, u64 offset, int na)
348 return of_bus_default_translate(addr + 1, offset, na - 1);
351 static unsigned int of_bus_isa_get_flags(const __be32 *addr)
353 unsigned int flags = 0;
354 u32 w = be32_to_cpup(addr);
357 flags |= IORESOURCE_IO;
359 flags |= IORESOURCE_MEM;
363 static int of_bus_default_flags_match(struct device_node *np)
365 return of_bus_n_addr_cells(np) == 3;
369 * Array of bus specific translators
372 static struct of_bus of_busses[] = {
377 .addresses = "assigned-addresses",
378 .match = of_bus_pci_match,
379 .count_cells = of_bus_pci_count_cells,
380 .map = of_bus_pci_map,
381 .translate = of_bus_pci_translate,
383 .get_flags = of_bus_pci_get_flags,
385 #endif /* CONFIG_PCI */
390 .match = of_bus_isa_match,
391 .count_cells = of_bus_isa_count_cells,
392 .map = of_bus_isa_map,
393 .translate = of_bus_isa_translate,
395 .get_flags = of_bus_isa_get_flags,
397 /* Default with flags cell */
399 .name = "default-flags",
401 .match = of_bus_default_flags_match,
402 .count_cells = of_bus_default_count_cells,
403 .map = of_bus_default_flags_map,
404 .translate = of_bus_default_flags_translate,
406 .get_flags = of_bus_default_flags_get_flags,
413 .count_cells = of_bus_default_count_cells,
414 .map = of_bus_default_map,
415 .translate = of_bus_default_translate,
416 .get_flags = of_bus_default_get_flags,
420 static struct of_bus *of_match_bus(struct device_node *np)
424 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
425 if (!of_busses[i].match || of_busses[i].match(np))
426 return &of_busses[i];
431 static int of_empty_ranges_quirk(struct device_node *np)
433 if (IS_ENABLED(CONFIG_PPC)) {
434 /* To save cycles, we cache the result for global "Mac" setting */
435 static int quirk_state = -1;
437 /* PA-SEMI sdc DT bug */
438 if (of_device_is_compatible(np, "1682m-sdc"))
441 /* Make quirk cached */
444 of_machine_is_compatible("Power Macintosh") ||
445 of_machine_is_compatible("MacRISC");
451 static int of_translate_one(struct device_node *parent, struct of_bus *bus,
452 struct of_bus *pbus, __be32 *addr,
453 int na, int ns, int pna, const char *rprop)
455 const __be32 *ranges;
458 u64 offset = OF_BAD_ADDR;
461 * Normally, an absence of a "ranges" property means we are
462 * crossing a non-translatable boundary, and thus the addresses
463 * below the current cannot be converted to CPU physical ones.
464 * Unfortunately, while this is very clear in the spec, it's not
465 * what Apple understood, and they do have things like /uni-n or
466 * /ht nodes with no "ranges" property and a lot of perfectly
467 * useable mapped devices below them. Thus we treat the absence of
468 * "ranges" as equivalent to an empty "ranges" property which means
469 * a 1:1 translation at that level. It's up to the caller not to try
470 * to translate addresses that aren't supposed to be translated in
471 * the first place. --BenH.
473 * As far as we know, this damage only exists on Apple machines, so
474 * This code is only enabled on powerpc. --gcl
476 * This quirk also applies for 'dma-ranges' which frequently exist in
477 * child nodes without 'dma-ranges' in the parent nodes. --RobH
479 ranges = of_get_property(parent, rprop, &rlen);
480 if (ranges == NULL && !of_empty_ranges_quirk(parent) &&
481 strcmp(rprop, "dma-ranges")) {
482 pr_debug("no ranges; cannot translate\n");
485 if (ranges == NULL || rlen == 0) {
486 offset = of_read_number(addr, na);
487 memset(addr, 0, pna * 4);
488 pr_debug("empty ranges; 1:1 translation\n");
492 pr_debug("walking ranges...\n");
494 /* Now walk through the ranges */
496 rone = na + pna + ns;
497 for (; rlen >= rone; rlen -= rone, ranges += rone) {
498 offset = bus->map(addr, ranges, na, ns, pna);
499 if (offset != OF_BAD_ADDR)
502 if (offset == OF_BAD_ADDR) {
503 pr_debug("not found !\n");
506 memcpy(addr, ranges + na, 4 * pna);
509 of_dump_addr("parent translation for:", addr, pna);
510 pr_debug("with offset: %llx\n", offset);
512 /* Translate it into parent bus space */
513 return pbus->translate(addr, offset, pna);
517 * Translate an address from the device-tree into a CPU physical address,
518 * this walks up the tree and applies the various bus mappings on the
521 * Note: We consider that crossing any level with #size-cells == 0 to mean
522 * that translation is impossible (that is we are not dealing with a value
523 * that can be mapped to a cpu physical address). This is not really specified
524 * that way, but this is traditionally the way IBM at least do things
526 * Whenever the translation fails, the *host pointer will be set to the
527 * device that had registered logical PIO mapping, and the return code is
528 * relative to that node.
530 static u64 __of_translate_address(struct device_node *dev,
531 struct device_node *(*get_parent)(const struct device_node *),
532 const __be32 *in_addr, const char *rprop,
533 struct device_node **host)
535 struct device_node *parent = NULL;
536 struct of_bus *bus, *pbus;
537 __be32 addr[OF_MAX_ADDR_CELLS];
538 int na, ns, pna, pns;
539 u64 result = OF_BAD_ADDR;
541 pr_debug("** translation for device %pOF **\n", dev);
543 /* Increase refcount at current level */
547 /* Get parent & match bus type */
548 parent = get_parent(dev);
551 bus = of_match_bus(parent);
553 /* Count address cells & copy address locally */
554 bus->count_cells(dev, &na, &ns);
555 if (!OF_CHECK_COUNTS(na, ns)) {
556 pr_debug("Bad cell count for %pOF\n", dev);
559 memcpy(addr, in_addr, na * 4);
561 pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",
562 bus->name, na, ns, parent);
563 of_dump_addr("translating address:", addr, na);
567 struct logic_pio_hwaddr *iorange;
569 /* Switch to parent bus */
572 parent = get_parent(dev);
574 /* If root, we have finished */
575 if (parent == NULL) {
576 pr_debug("reached root node\n");
577 result = of_read_number(addr, na);
582 * For indirectIO device which has no ranges property, get
583 * the address from reg directly.
585 iorange = find_io_range_by_fwnode(&dev->fwnode);
586 if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) {
587 result = of_read_number(addr + 1, na - 1);
588 pr_debug("indirectIO matched(%pOF) 0x%llx\n",
590 *host = of_node_get(dev);
594 /* Get new parent bus and counts */
595 pbus = of_match_bus(parent);
596 pbus->count_cells(dev, &pna, &pns);
597 if (!OF_CHECK_COUNTS(pna, pns)) {
598 pr_err("Bad cell count for %pOF\n", dev);
602 pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
603 pbus->name, pna, pns, parent);
605 /* Apply bus translation */
606 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
609 /* Complete the move up one level */
614 of_dump_addr("one level translation:", addr, na);
623 u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
625 struct device_node *host;
628 ret = __of_translate_address(dev, of_get_parent,
629 in_addr, "ranges", &host);
637 EXPORT_SYMBOL(of_translate_address);
639 #ifdef CONFIG_HAS_DMA
640 struct device_node *__of_get_dma_parent(const struct device_node *np)
642 struct of_phandle_args args;
645 index = of_property_match_string(np, "interconnect-names", "dma-mem");
647 return of_get_parent(np);
649 ret = of_parse_phandle_with_args(np, "interconnects",
650 "#interconnect-cells",
653 return of_get_parent(np);
655 return of_node_get(args.np);
659 static struct device_node *of_get_next_dma_parent(struct device_node *np)
661 struct device_node *parent;
663 parent = __of_get_dma_parent(np);
669 u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
671 struct device_node *host;
674 ret = __of_translate_address(dev, __of_get_dma_parent,
675 in_addr, "dma-ranges", &host);
684 EXPORT_SYMBOL(of_translate_dma_address);
687 * of_translate_dma_region - Translate device tree address and size tuple
688 * @dev: device tree node for which to translate
689 * @prop: pointer into array of cells
690 * @start: return value for the start of the DMA range
691 * @length: return value for the length of the DMA range
693 * Returns a pointer to the cell immediately following the translated DMA region.
695 const __be32 *of_translate_dma_region(struct device_node *dev, const __be32 *prop,
696 phys_addr_t *start, size_t *length)
698 struct device_node *parent;
702 parent = __of_get_dma_parent(dev);
706 na = of_bus_n_addr_cells(parent);
707 ns = of_bus_n_size_cells(parent);
711 address = of_translate_dma_address(dev, prop);
712 if (address == OF_BAD_ADDR)
715 size = of_read_number(prop + na, ns);
723 return prop + na + ns;
725 EXPORT_SYMBOL(of_translate_dma_region);
727 const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,
728 u64 *size, unsigned int *flags)
732 struct device_node *parent;
734 int onesize, i, na, ns;
736 /* Get parent & match bus type */
737 parent = of_get_parent(dev);
740 bus = of_match_bus(parent);
741 if (strcmp(bus->name, "pci") && (bar_no >= 0)) {
745 bus->count_cells(dev, &na, &ns);
747 if (!OF_CHECK_ADDR_COUNT(na))
750 /* Get "reg" or "assigned-addresses" property */
751 prop = of_get_property(dev, bus->addresses, &psize);
757 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
758 u32 val = be32_to_cpu(prop[0]);
759 /* PCI bus matches on BAR number instead of index */
760 if (((bar_no >= 0) && ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0))) ||
761 ((index >= 0) && (i == index))) {
763 *size = of_read_number(prop + na, ns);
765 *flags = bus->get_flags(prop);
771 EXPORT_SYMBOL(__of_get_address);
774 * of_property_read_reg - Retrieve the specified "reg" entry index without translating
775 * @np: device tree node for which to retrieve "reg" from
776 * @idx: "reg" entry index to read
777 * @addr: return value for the untranslated address
778 * @size: return value for the entry size
780 * Returns -EINVAL if "reg" is not found. Returns 0 on success with addr and
781 * size values filled in.
783 int of_property_read_reg(struct device_node *np, int idx, u64 *addr, u64 *size)
785 const __be32 *prop = of_get_address(np, idx, size, NULL);
790 *addr = of_read_number(prop, of_n_addr_cells(np));
794 EXPORT_SYMBOL(of_property_read_reg);
796 static int parser_init(struct of_pci_range_parser *parser,
797 struct device_node *node, const char *name)
802 parser->pna = of_n_addr_cells(node);
803 parser->na = of_bus_n_addr_cells(node);
804 parser->ns = of_bus_n_size_cells(node);
805 parser->dma = !strcmp(name, "dma-ranges");
806 parser->bus = of_match_bus(node);
808 parser->range = of_get_property(node, name, &rlen);
809 if (parser->range == NULL)
812 parser->end = parser->range + rlen / sizeof(__be32);
817 int of_pci_range_parser_init(struct of_pci_range_parser *parser,
818 struct device_node *node)
820 return parser_init(parser, node, "ranges");
822 EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
824 int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
825 struct device_node *node)
827 return parser_init(parser, node, "dma-ranges");
829 EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init);
830 #define of_dma_range_parser_init of_pci_dma_range_parser_init
832 struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
833 struct of_pci_range *range)
837 int np = parser->pna + na + ns;
843 if (!parser->range || parser->range + np > parser->end)
846 range->flags = parser->bus->get_flags(parser->range);
848 /* A extra cell for resource flags */
849 if (parser->bus->has_flags)
852 range->bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
855 range->cpu_addr = of_translate_dma_address(parser->node,
858 range->cpu_addr = of_translate_address(parser->node,
860 range->size = of_read_number(parser->range + parser->pna + na, ns);
864 /* Now consume following elements while they are contiguous */
865 while (parser->range + np <= parser->end) {
867 u64 bus_addr, cpu_addr, size;
869 flags = parser->bus->get_flags(parser->range);
870 bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
872 cpu_addr = of_translate_dma_address(parser->node,
875 cpu_addr = of_translate_address(parser->node,
877 size = of_read_number(parser->range + parser->pna + na, ns);
879 if (flags != range->flags)
881 if (bus_addr != range->bus_addr + range->size ||
882 cpu_addr != range->cpu_addr + range->size)
891 EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
893 static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr,
898 struct device_node *host;
900 taddr = __of_translate_address(dev, of_get_parent,
901 in_addr, "ranges", &host);
903 /* host-specific port access */
904 port = logic_pio_trans_hwaddr(&host->fwnode, taddr, size);
907 /* memory-mapped I/O range */
908 port = pci_address_to_pio(taddr);
911 if (port == (unsigned long)-1)
917 #ifdef CONFIG_HAS_DMA
919 * of_dma_get_range - Get DMA range info and put it into a map array
920 * @np: device node to get DMA range info
921 * @map: dma range structure to return
923 * Look in bottom up direction for the first "dma-ranges" property
924 * and parse it. Put the information into a DMA offset map array.
927 * DMA addr (dma_addr) : naddr cells
928 * CPU addr (phys_addr_t) : pna cells
931 * It returns -ENODEV if "dma-ranges" property was not found for this
934 int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map)
936 struct device_node *node = of_node_get(np);
937 const __be32 *ranges = NULL;
938 bool found_dma_ranges = false;
939 struct of_range_parser parser;
940 struct of_range range;
941 struct bus_dma_region *r;
942 int len, num_ranges = 0;
946 ranges = of_get_property(node, "dma-ranges", &len);
948 /* Ignore empty ranges, they imply no translation required */
949 if (ranges && len > 0)
952 /* Once we find 'dma-ranges', then a missing one is an error */
953 if (found_dma_ranges && !ranges) {
957 found_dma_ranges = true;
959 node = of_get_next_dma_parent(node);
962 if (!node || !ranges) {
963 pr_debug("no dma-ranges found for node(%pOF)\n", np);
968 of_dma_range_parser_init(&parser, node);
969 for_each_of_range(&parser, &range) {
970 if (range.cpu_addr == OF_BAD_ADDR) {
971 pr_err("translation of DMA address(%llx) to CPU address failed node(%pOF)\n",
972 range.bus_addr, node);
983 r = kcalloc(num_ranges + 1, sizeof(*r), GFP_KERNEL);
990 * Record all info in the generic DMA ranges array for struct device,
991 * returning an error if we don't find any parsable ranges.
994 of_dma_range_parser_init(&parser, node);
995 for_each_of_range(&parser, &range) {
996 pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
997 range.bus_addr, range.cpu_addr, range.size);
998 if (range.cpu_addr == OF_BAD_ADDR)
1000 r->cpu_start = range.cpu_addr;
1001 r->dma_start = range.bus_addr;
1002 r->size = range.size;
1003 r->offset = range.cpu_addr - range.bus_addr;
1010 #endif /* CONFIG_HAS_DMA */
1013 * of_dma_get_max_cpu_address - Gets highest CPU address suitable for DMA
1014 * @np: The node to start searching from or NULL to start from the root
1016 * Gets the highest CPU physical address that is addressable by all DMA masters
1017 * in the sub-tree pointed by np, or the whole tree if NULL is passed. If no
1018 * DMA constrained device is found, it returns PHYS_ADDR_MAX.
1020 phys_addr_t __init of_dma_get_max_cpu_address(struct device_node *np)
1022 phys_addr_t max_cpu_addr = PHYS_ADDR_MAX;
1023 struct of_range_parser parser;
1024 phys_addr_t subtree_max_addr;
1025 struct device_node *child;
1026 struct of_range range;
1027 const __be32 *ranges;
1034 ranges = of_get_property(np, "dma-ranges", &len);
1035 if (ranges && len) {
1036 of_dma_range_parser_init(&parser, np);
1037 for_each_of_range(&parser, &range)
1038 if (range.cpu_addr + range.size > cpu_end)
1039 cpu_end = range.cpu_addr + range.size - 1;
1041 if (max_cpu_addr > cpu_end)
1042 max_cpu_addr = cpu_end;
1045 for_each_available_child_of_node(np, child) {
1046 subtree_max_addr = of_dma_get_max_cpu_address(child);
1047 if (max_cpu_addr > subtree_max_addr)
1048 max_cpu_addr = subtree_max_addr;
1051 return max_cpu_addr;
1055 * of_dma_is_coherent - Check if device is coherent
1058 * It returns true if "dma-coherent" property was found
1059 * for this device in the DT, or if DMA is coherent by
1060 * default for OF devices on the current platform and no
1061 * "dma-noncoherent" property was found for this device.
1063 bool of_dma_is_coherent(struct device_node *np)
1065 struct device_node *node;
1066 bool is_coherent = dma_default_coherent;
1068 node = of_node_get(np);
1071 if (of_property_read_bool(node, "dma-coherent")) {
1075 if (of_property_read_bool(node, "dma-noncoherent")) {
1076 is_coherent = false;
1079 node = of_get_next_dma_parent(node);
1084 EXPORT_SYMBOL_GPL(of_dma_is_coherent);
1087 * of_mmio_is_nonposted - Check if device uses non-posted MMIO
1090 * Returns true if the "nonposted-mmio" property was found for
1093 * This is currently only enabled on builds that support Apple ARM devices, as
1096 static bool of_mmio_is_nonposted(struct device_node *np)
1098 struct device_node *parent;
1101 if (!IS_ENABLED(CONFIG_ARCH_APPLE))
1104 parent = of_get_parent(np);
1108 nonposted = of_property_read_bool(parent, "nonposted-mmio");
1110 of_node_put(parent);
1114 static int __of_address_to_resource(struct device_node *dev, int index, int bar_no,
1118 const __be32 *addrp;
1121 const char *name = NULL;
1123 addrp = __of_get_address(dev, index, bar_no, &size, &flags);
1127 /* Get optional "reg-names" property to add a name to a resource */
1129 of_property_read_string_index(dev, "reg-names", index, &name);
1131 if (flags & IORESOURCE_MEM)
1132 taddr = of_translate_address(dev, addrp);
1133 else if (flags & IORESOURCE_IO)
1134 taddr = of_translate_ioport(dev, addrp, size);
1138 if (taddr == OF_BAD_ADDR)
1140 memset(r, 0, sizeof(struct resource));
1142 if (of_mmio_is_nonposted(dev))
1143 flags |= IORESOURCE_MEM_NONPOSTED;
1146 r->end = taddr + size - 1;
1148 r->name = name ? name : dev->full_name;
1154 * of_address_to_resource - Translate device tree address and return as resource
1155 * @dev: Caller's Device Node
1156 * @index: Index into the array
1157 * @r: Pointer to resource array
1159 * Returns -EINVAL if the range cannot be converted to resource.
1161 * Note that if your address is a PIO address, the conversion will fail if
1162 * the physical address can't be internally converted to an IO token with
1163 * pci_address_to_pio(), that is because it's either called too early or it
1164 * can't be matched to any host bridge IO space
1166 int of_address_to_resource(struct device_node *dev, int index,
1169 return __of_address_to_resource(dev, index, -1, r);
1171 EXPORT_SYMBOL_GPL(of_address_to_resource);
1173 int of_pci_address_to_resource(struct device_node *dev, int bar,
1177 if (!IS_ENABLED(CONFIG_PCI))
1180 return __of_address_to_resource(dev, -1, bar, r);
1182 EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
1185 * of_iomap - Maps the memory mapped IO for a given device_node
1186 * @np: the device whose io range will be mapped
1187 * @index: index of the io range
1189 * Returns a pointer to the mapped memory
1191 void __iomem *of_iomap(struct device_node *np, int index)
1193 struct resource res;
1195 if (of_address_to_resource(np, index, &res))
1198 if (res.flags & IORESOURCE_MEM_NONPOSTED)
1199 return ioremap_np(res.start, resource_size(&res));
1201 return ioremap(res.start, resource_size(&res));
1203 EXPORT_SYMBOL(of_iomap);
1206 * of_io_request_and_map - Requests a resource and maps the memory mapped IO
1207 * for a given device_node
1208 * @device: the device whose io range will be mapped
1209 * @index: index of the io range
1210 * @name: name "override" for the memory region request or NULL
1212 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
1213 * error code on failure. Usage example:
1215 * base = of_io_request_and_map(node, 0, "foo");
1217 * return PTR_ERR(base);
1219 void __iomem *of_io_request_and_map(struct device_node *np, int index,
1222 struct resource res;
1225 if (of_address_to_resource(np, index, &res))
1226 return IOMEM_ERR_PTR(-EINVAL);
1230 if (!request_mem_region(res.start, resource_size(&res), name))
1231 return IOMEM_ERR_PTR(-EBUSY);
1233 if (res.flags & IORESOURCE_MEM_NONPOSTED)
1234 mem = ioremap_np(res.start, resource_size(&res));
1236 mem = ioremap(res.start, resource_size(&res));
1239 release_mem_region(res.start, resource_size(&res));
1240 return IOMEM_ERR_PTR(-ENOMEM);
1245 EXPORT_SYMBOL(of_io_request_and_map);