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/module.h>
9 #include <linux/of_address.h>
10 #include <linux/pci.h>
11 #include <linux/pci_regs.h>
12 #include <linux/sizes.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
16 /* Max address size we deal with */
17 #define OF_MAX_ADDR_CELLS 4
18 #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
19 #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
21 static struct of_bus *of_match_bus(struct device_node *np);
22 static int __of_address_to_resource(struct device_node *dev,
23 const __be32 *addrp, u64 size, unsigned int flags,
24 const char *name, struct resource *r);
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);
48 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",
75 (unsigned long long)cp, (unsigned long long)s,
76 (unsigned long long)da);
78 if (da < cp || da >= (cp + s))
83 static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
85 u64 a = of_read_number(addr, na);
86 memset(addr, 0, na * 4);
89 addr[na - 2] = cpu_to_be32(a >> 32);
90 addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
95 static unsigned int of_bus_default_get_flags(const __be32 *addr)
97 return IORESOURCE_MEM;
102 * PCI bus specific translator
105 static int of_bus_pci_match(struct device_node *np)
108 * "pciex" is PCI Express
109 * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
110 * "ht" is hypertransport
112 return !strcmp(np->type, "pci") || !strcmp(np->type, "pciex") ||
113 !strcmp(np->type, "vci") || !strcmp(np->type, "ht");
116 static void of_bus_pci_count_cells(struct device_node *np,
117 int *addrc, int *sizec)
125 static unsigned int of_bus_pci_get_flags(const __be32 *addr)
127 unsigned int flags = 0;
128 u32 w = be32_to_cpup(addr);
130 switch((w >> 24) & 0x03) {
132 flags |= IORESOURCE_IO;
134 case 0x02: /* 32 bits */
135 case 0x03: /* 64 bits */
136 flags |= IORESOURCE_MEM;
140 flags |= IORESOURCE_PREFETCH;
144 static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
150 af = of_bus_pci_get_flags(addr);
151 rf = of_bus_pci_get_flags(range);
153 /* Check address type match */
154 if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
157 /* Read address values, skipping high cell */
158 cp = of_read_number(range + 1, na - 1);
159 s = of_read_number(range + na + pna, ns);
160 da = of_read_number(addr + 1, na - 1);
162 pr_debug("PCI map, cp=%llx, s=%llx, da=%llx\n",
163 (unsigned long long)cp, (unsigned long long)s,
164 (unsigned long long)da);
166 if (da < cp || da >= (cp + s))
171 static int of_bus_pci_translate(__be32 *addr, u64 offset, int na)
173 return of_bus_default_translate(addr + 1, offset, na - 1);
176 const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
181 struct device_node *parent;
183 int onesize, i, na, ns;
185 /* Get parent & match bus type */
186 parent = of_get_parent(dev);
189 bus = of_match_bus(parent);
190 if (strcmp(bus->name, "pci")) {
194 bus->count_cells(dev, &na, &ns);
196 if (!OF_CHECK_ADDR_COUNT(na))
199 /* Get "reg" or "assigned-addresses" property */
200 prop = of_get_property(dev, bus->addresses, &psize);
206 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
207 u32 val = be32_to_cpu(prop[0]);
208 if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
210 *size = of_read_number(prop + na, ns);
212 *flags = bus->get_flags(prop);
218 EXPORT_SYMBOL(of_get_pci_address);
220 int of_pci_address_to_resource(struct device_node *dev, int bar,
227 addrp = of_get_pci_address(dev, bar, &size, &flags);
230 return __of_address_to_resource(dev, addrp, size, flags, NULL, r);
232 EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
234 static int parser_init(struct of_pci_range_parser *parser,
235 struct device_node *node, const char *name)
237 const int na = 3, ns = 2;
241 parser->pna = of_n_addr_cells(node);
242 parser->np = parser->pna + na + ns;
244 parser->range = of_get_property(node, name, &rlen);
245 if (parser->range == NULL)
248 parser->end = parser->range + rlen / sizeof(__be32);
253 int of_pci_range_parser_init(struct of_pci_range_parser *parser,
254 struct device_node *node)
256 return parser_init(parser, node, "ranges");
258 EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
260 int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
261 struct device_node *node)
263 return parser_init(parser, node, "dma-ranges");
265 EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init);
267 struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
268 struct of_pci_range *range)
270 const int na = 3, ns = 2;
275 if (!parser->range || parser->range + parser->np > parser->end)
278 range->pci_space = be32_to_cpup(parser->range);
279 range->flags = of_bus_pci_get_flags(parser->range);
280 range->pci_addr = of_read_number(parser->range + 1, ns);
281 range->cpu_addr = of_translate_address(parser->node,
283 range->size = of_read_number(parser->range + parser->pna + na, ns);
285 parser->range += parser->np;
287 /* Now consume following elements while they are contiguous */
288 while (parser->range + parser->np <= parser->end) {
290 u64 pci_addr, cpu_addr, size;
292 flags = of_bus_pci_get_flags(parser->range);
293 pci_addr = of_read_number(parser->range + 1, ns);
294 cpu_addr = of_translate_address(parser->node,
296 size = of_read_number(parser->range + parser->pna + na, ns);
298 if (flags != range->flags)
300 if (pci_addr != range->pci_addr + range->size ||
301 cpu_addr != range->cpu_addr + range->size)
305 parser->range += parser->np;
310 EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
313 * of_pci_range_to_resource - Create a resource from an of_pci_range
314 * @range: the PCI range that describes the resource
315 * @np: device node where the range belongs to
316 * @res: pointer to a valid resource that will be updated to
317 * reflect the values contained in the range.
319 * Returns EINVAL if the range cannot be converted to resource.
321 * Note that if the range is an IO range, the resource will be converted
322 * using pci_address_to_pio() which can fail if it is called too early or
323 * if the range cannot be matched to any host bridge IO space (our case here).
324 * To guard against that we try to register the IO range first.
325 * If that fails we know that pci_address_to_pio() will do too.
327 int of_pci_range_to_resource(struct of_pci_range *range,
328 struct device_node *np, struct resource *res)
331 res->flags = range->flags;
332 res->parent = res->child = res->sibling = NULL;
333 res->name = np->full_name;
335 if (res->flags & IORESOURCE_IO) {
337 err = pci_register_io_range(&np->fwnode, range->cpu_addr,
341 port = pci_address_to_pio(range->cpu_addr);
342 if (port == (unsigned long)-1) {
348 if ((sizeof(resource_size_t) < 8) &&
349 upper_32_bits(range->cpu_addr)) {
354 res->start = range->cpu_addr;
356 res->end = res->start + range->size - 1;
360 res->start = (resource_size_t)OF_BAD_ADDR;
361 res->end = (resource_size_t)OF_BAD_ADDR;
364 EXPORT_SYMBOL(of_pci_range_to_resource);
365 #endif /* CONFIG_PCI */
368 * ISA bus specific translator
371 static int of_bus_isa_match(struct device_node *np)
373 return !strcmp(np->name, "isa");
376 static void of_bus_isa_count_cells(struct device_node *child,
377 int *addrc, int *sizec)
385 static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
390 /* Check address type match */
391 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
394 /* Read address values, skipping high cell */
395 cp = of_read_number(range + 1, na - 1);
396 s = of_read_number(range + na + pna, ns);
397 da = of_read_number(addr + 1, na - 1);
399 pr_debug("ISA map, cp=%llx, s=%llx, da=%llx\n",
400 (unsigned long long)cp, (unsigned long long)s,
401 (unsigned long long)da);
403 if (da < cp || da >= (cp + s))
408 static int of_bus_isa_translate(__be32 *addr, u64 offset, int na)
410 return of_bus_default_translate(addr + 1, offset, na - 1);
413 static unsigned int of_bus_isa_get_flags(const __be32 *addr)
415 unsigned int flags = 0;
416 u32 w = be32_to_cpup(addr);
419 flags |= IORESOURCE_IO;
421 flags |= IORESOURCE_MEM;
426 * Array of bus specific translators
429 static struct of_bus of_busses[] = {
434 .addresses = "assigned-addresses",
435 .match = of_bus_pci_match,
436 .count_cells = of_bus_pci_count_cells,
437 .map = of_bus_pci_map,
438 .translate = of_bus_pci_translate,
439 .get_flags = of_bus_pci_get_flags,
441 #endif /* CONFIG_PCI */
446 .match = of_bus_isa_match,
447 .count_cells = of_bus_isa_count_cells,
448 .map = of_bus_isa_map,
449 .translate = of_bus_isa_translate,
450 .get_flags = of_bus_isa_get_flags,
457 .count_cells = of_bus_default_count_cells,
458 .map = of_bus_default_map,
459 .translate = of_bus_default_translate,
460 .get_flags = of_bus_default_get_flags,
464 static struct of_bus *of_match_bus(struct device_node *np)
468 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
469 if (!of_busses[i].match || of_busses[i].match(np))
470 return &of_busses[i];
475 static int of_empty_ranges_quirk(struct device_node *np)
477 if (IS_ENABLED(CONFIG_PPC)) {
478 /* To save cycles, we cache the result for global "Mac" setting */
479 static int quirk_state = -1;
481 /* PA-SEMI sdc DT bug */
482 if (of_device_is_compatible(np, "1682m-sdc"))
485 /* Make quirk cached */
488 of_machine_is_compatible("Power Macintosh") ||
489 of_machine_is_compatible("MacRISC");
495 static int of_translate_one(struct device_node *parent, struct of_bus *bus,
496 struct of_bus *pbus, __be32 *addr,
497 int na, int ns, int pna, const char *rprop)
499 const __be32 *ranges;
502 u64 offset = OF_BAD_ADDR;
505 * Normally, an absence of a "ranges" property means we are
506 * crossing a non-translatable boundary, and thus the addresses
507 * below the current cannot be converted to CPU physical ones.
508 * Unfortunately, while this is very clear in the spec, it's not
509 * what Apple understood, and they do have things like /uni-n or
510 * /ht nodes with no "ranges" property and a lot of perfectly
511 * useable mapped devices below them. Thus we treat the absence of
512 * "ranges" as equivalent to an empty "ranges" property which means
513 * a 1:1 translation at that level. It's up to the caller not to try
514 * to translate addresses that aren't supposed to be translated in
515 * the first place. --BenH.
517 * As far as we know, this damage only exists on Apple machines, so
518 * This code is only enabled on powerpc. --gcl
520 ranges = of_get_property(parent, rprop, &rlen);
521 if (ranges == NULL && !of_empty_ranges_quirk(parent)) {
522 pr_debug("no ranges; cannot translate\n");
525 if (ranges == NULL || rlen == 0) {
526 offset = of_read_number(addr, na);
527 memset(addr, 0, pna * 4);
528 pr_debug("empty ranges; 1:1 translation\n");
532 pr_debug("walking ranges...\n");
534 /* Now walk through the ranges */
536 rone = na + pna + ns;
537 for (; rlen >= rone; rlen -= rone, ranges += rone) {
538 offset = bus->map(addr, ranges, na, ns, pna);
539 if (offset != OF_BAD_ADDR)
542 if (offset == OF_BAD_ADDR) {
543 pr_debug("not found !\n");
546 memcpy(addr, ranges + na, 4 * pna);
549 of_dump_addr("parent translation for:", addr, pna);
550 pr_debug("with offset: %llx\n", (unsigned long long)offset);
552 /* Translate it into parent bus space */
553 return pbus->translate(addr, offset, pna);
557 * Translate an address from the device-tree into a CPU physical address,
558 * this walks up the tree and applies the various bus mappings on the
561 * Note: We consider that crossing any level with #size-cells == 0 to mean
562 * that translation is impossible (that is we are not dealing with a value
563 * that can be mapped to a cpu physical address). This is not really specified
564 * that way, but this is traditionally the way IBM at least do things
566 static u64 __of_translate_address(struct device_node *dev,
567 const __be32 *in_addr, const char *rprop)
569 struct device_node *parent = NULL;
570 struct of_bus *bus, *pbus;
571 __be32 addr[OF_MAX_ADDR_CELLS];
572 int na, ns, pna, pns;
573 u64 result = OF_BAD_ADDR;
575 pr_debug("** translation for device %pOF **\n", dev);
577 /* Increase refcount at current level */
580 /* Get parent & match bus type */
581 parent = of_get_parent(dev);
584 bus = of_match_bus(parent);
586 /* Count address cells & copy address locally */
587 bus->count_cells(dev, &na, &ns);
588 if (!OF_CHECK_COUNTS(na, ns)) {
589 pr_debug("Bad cell count for %pOF\n", dev);
592 memcpy(addr, in_addr, na * 4);
594 pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",
595 bus->name, na, ns, parent);
596 of_dump_addr("translating address:", addr, na);
600 /* Switch to parent bus */
603 parent = of_get_parent(dev);
605 /* If root, we have finished */
606 if (parent == NULL) {
607 pr_debug("reached root node\n");
608 result = of_read_number(addr, na);
612 /* Get new parent bus and counts */
613 pbus = of_match_bus(parent);
614 pbus->count_cells(dev, &pna, &pns);
615 if (!OF_CHECK_COUNTS(pna, pns)) {
616 pr_err("Bad cell count for %pOF\n", dev);
620 pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
621 pbus->name, pna, pns, parent);
623 /* Apply bus translation */
624 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
627 /* Complete the move up one level */
632 of_dump_addr("one level translation:", addr, na);
641 u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
643 return __of_translate_address(dev, in_addr, "ranges");
645 EXPORT_SYMBOL(of_translate_address);
647 u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
649 return __of_translate_address(dev, in_addr, "dma-ranges");
651 EXPORT_SYMBOL(of_translate_dma_address);
653 const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,
658 struct device_node *parent;
660 int onesize, i, na, ns;
662 /* Get parent & match bus type */
663 parent = of_get_parent(dev);
666 bus = of_match_bus(parent);
667 bus->count_cells(dev, &na, &ns);
669 if (!OF_CHECK_ADDR_COUNT(na))
672 /* Get "reg" or "assigned-addresses" property */
673 prop = of_get_property(dev, bus->addresses, &psize);
679 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
682 *size = of_read_number(prop + na, ns);
684 *flags = bus->get_flags(prop);
689 EXPORT_SYMBOL(of_get_address);
691 static int __of_address_to_resource(struct device_node *dev,
692 const __be32 *addrp, u64 size, unsigned int flags,
693 const char *name, struct resource *r)
697 if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
699 taddr = of_translate_address(dev, addrp);
700 if (taddr == OF_BAD_ADDR)
702 memset(r, 0, sizeof(struct resource));
703 if (flags & IORESOURCE_IO) {
705 port = pci_address_to_pio(taddr);
706 if (port == (unsigned long)-1)
709 r->end = port + size - 1;
712 r->end = taddr + size - 1;
715 r->name = name ? name : dev->full_name;
721 * of_address_to_resource - Translate device tree address and return as resource
723 * Note that if your address is a PIO address, the conversion will fail if
724 * the physical address can't be internally converted to an IO token with
725 * pci_address_to_pio(), that is because it's either called too early or it
726 * can't be matched to any host bridge IO space
728 int of_address_to_resource(struct device_node *dev, int index,
734 const char *name = NULL;
736 addrp = of_get_address(dev, index, &size, &flags);
740 /* Get optional "reg-names" property to add a name to a resource */
741 of_property_read_string_index(dev, "reg-names", index, &name);
743 return __of_address_to_resource(dev, addrp, size, flags, name, r);
745 EXPORT_SYMBOL_GPL(of_address_to_resource);
747 struct device_node *of_find_matching_node_by_address(struct device_node *from,
748 const struct of_device_id *matches,
751 struct device_node *dn = of_find_matching_node(from, matches);
755 if (!of_address_to_resource(dn, 0, &res) &&
756 res.start == base_address)
759 dn = of_find_matching_node(dn, matches);
767 * of_iomap - Maps the memory mapped IO for a given device_node
768 * @device: the device whose io range will be mapped
769 * @index: index of the io range
771 * Returns a pointer to the mapped memory
773 void __iomem *of_iomap(struct device_node *np, int index)
777 if (of_address_to_resource(np, index, &res))
780 return ioremap(res.start, resource_size(&res));
782 EXPORT_SYMBOL(of_iomap);
785 * of_io_request_and_map - Requests a resource and maps the memory mapped IO
786 * for a given device_node
787 * @device: the device whose io range will be mapped
788 * @index: index of the io range
789 * @name: name of the resource
791 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
792 * error code on failure. Usage example:
794 * base = of_io_request_and_map(node, 0, "foo");
796 * return PTR_ERR(base);
798 void __iomem *of_io_request_and_map(struct device_node *np, int index,
804 if (of_address_to_resource(np, index, &res))
805 return IOMEM_ERR_PTR(-EINVAL);
807 if (!request_mem_region(res.start, resource_size(&res), name))
808 return IOMEM_ERR_PTR(-EBUSY);
810 mem = ioremap(res.start, resource_size(&res));
812 release_mem_region(res.start, resource_size(&res));
813 return IOMEM_ERR_PTR(-ENOMEM);
818 EXPORT_SYMBOL(of_io_request_and_map);
821 * of_dma_get_range - Get DMA range info
822 * @np: device node to get DMA range info
823 * @dma_addr: pointer to store initial DMA address of DMA range
824 * @paddr: pointer to store initial CPU address of DMA range
825 * @size: pointer to store size of DMA range
827 * Look in bottom up direction for the first "dma-ranges" property
830 * DMA addr (dma_addr) : naddr cells
831 * CPU addr (phys_addr_t) : pna cells
834 * It returns -ENODEV if "dma-ranges" property was not found
835 * for this device in DT.
837 int of_dma_get_range(struct device_node *np, u64 *dma_addr, u64 *paddr, u64 *size)
839 struct device_node *node = of_node_get(np);
840 const __be32 *ranges = NULL;
841 int len, naddr, nsize, pna;
849 naddr = of_n_addr_cells(node);
850 nsize = of_n_size_cells(node);
851 node = of_get_next_parent(node);
855 ranges = of_get_property(node, "dma-ranges", &len);
857 /* Ignore empty ranges, they imply no translation required */
858 if (ranges && len > 0)
862 * At least empty ranges has to be defined for parent node if
870 pr_debug("no dma-ranges found for node(%pOF)\n", np);
877 pna = of_n_addr_cells(node);
879 /* dma-ranges format:
880 * DMA addr : naddr cells
881 * CPU addr : pna cells
884 dmaaddr = of_read_number(ranges, naddr);
885 *paddr = of_translate_dma_address(np, ranges);
886 if (*paddr == OF_BAD_ADDR) {
887 pr_err("translation of DMA address(%pad) to CPU address failed node(%pOF)\n",
894 *size = of_read_number(ranges + naddr + pna, nsize);
896 pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
897 *dma_addr, *paddr, *size);
904 EXPORT_SYMBOL_GPL(of_dma_get_range);
907 * of_dma_is_coherent - Check if device is coherent
910 * It returns true if "dma-coherent" property was found
911 * for this device in DT.
913 bool of_dma_is_coherent(struct device_node *np)
915 struct device_node *node = of_node_get(np);
918 if (of_property_read_bool(node, "dma-coherent")) {
922 node = of_get_next_parent(node);
927 EXPORT_SYMBOL_GPL(of_dma_is_coherent);