Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #define pr_fmt(fmt) "OF: " fmt
0003 
0004 #include <linux/device.h>
0005 #include <linux/fwnode.h>
0006 #include <linux/io.h>
0007 #include <linux/ioport.h>
0008 #include <linux/logic_pio.h>
0009 #include <linux/module.h>
0010 #include <linux/of_address.h>
0011 #include <linux/pci.h>
0012 #include <linux/pci_regs.h>
0013 #include <linux/sizes.h>
0014 #include <linux/slab.h>
0015 #include <linux/string.h>
0016 #include <linux/dma-direct.h> /* for bus_dma_region */
0017 
0018 #include "of_private.h"
0019 
0020 /* Max address size we deal with */
0021 #define OF_MAX_ADDR_CELLS   4
0022 #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
0023 #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
0024 
0025 static struct of_bus *of_match_bus(struct device_node *np);
0026 static int __of_address_to_resource(struct device_node *dev, int index,
0027         int bar_no, struct resource *r);
0028 static bool of_mmio_is_nonposted(struct device_node *np);
0029 
0030 /* Debug utility */
0031 #ifdef DEBUG
0032 static void of_dump_addr(const char *s, const __be32 *addr, int na)
0033 {
0034     pr_debug("%s", s);
0035     while (na--)
0036         pr_cont(" %08x", be32_to_cpu(*(addr++)));
0037     pr_cont("\n");
0038 }
0039 #else
0040 static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
0041 #endif
0042 
0043 /* Callbacks for bus specific translators */
0044 struct of_bus {
0045     const char  *name;
0046     const char  *addresses;
0047     int     (*match)(struct device_node *parent);
0048     void        (*count_cells)(struct device_node *child,
0049                        int *addrc, int *sizec);
0050     u64     (*map)(__be32 *addr, const __be32 *range,
0051                 int na, int ns, int pna);
0052     int     (*translate)(__be32 *addr, u64 offset, int na);
0053     bool    has_flags;
0054     unsigned int    (*get_flags)(const __be32 *addr);
0055 };
0056 
0057 /*
0058  * Default translator (generic bus)
0059  */
0060 
0061 static void of_bus_default_count_cells(struct device_node *dev,
0062                        int *addrc, int *sizec)
0063 {
0064     if (addrc)
0065         *addrc = of_n_addr_cells(dev);
0066     if (sizec)
0067         *sizec = of_n_size_cells(dev);
0068 }
0069 
0070 static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
0071         int na, int ns, int pna)
0072 {
0073     u64 cp, s, da;
0074 
0075     cp = of_read_number(range, na);
0076     s  = of_read_number(range + na + pna, ns);
0077     da = of_read_number(addr, na);
0078 
0079     pr_debug("default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
0080 
0081     if (da < cp || da >= (cp + s))
0082         return OF_BAD_ADDR;
0083     return da - cp;
0084 }
0085 
0086 static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
0087 {
0088     u64 a = of_read_number(addr, na);
0089     memset(addr, 0, na * 4);
0090     a += offset;
0091     if (na > 1)
0092         addr[na - 2] = cpu_to_be32(a >> 32);
0093     addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
0094 
0095     return 0;
0096 }
0097 
0098 static unsigned int of_bus_default_get_flags(const __be32 *addr)
0099 {
0100     return IORESOURCE_MEM;
0101 }
0102 
0103 #ifdef CONFIG_PCI
0104 static unsigned int of_bus_pci_get_flags(const __be32 *addr)
0105 {
0106     unsigned int flags = 0;
0107     u32 w = be32_to_cpup(addr);
0108 
0109     if (!IS_ENABLED(CONFIG_PCI))
0110         return 0;
0111 
0112     switch((w >> 24) & 0x03) {
0113     case 0x01:
0114         flags |= IORESOURCE_IO;
0115         break;
0116     case 0x02: /* 32 bits */
0117         flags |= IORESOURCE_MEM;
0118         break;
0119 
0120     case 0x03: /* 64 bits */
0121         flags |= IORESOURCE_MEM | IORESOURCE_MEM_64;
0122         break;
0123     }
0124     if (w & 0x40000000)
0125         flags |= IORESOURCE_PREFETCH;
0126     return flags;
0127 }
0128 
0129 /*
0130  * PCI bus specific translator
0131  */
0132 
0133 static bool of_node_is_pcie(struct device_node *np)
0134 {
0135     bool is_pcie = of_node_name_eq(np, "pcie");
0136 
0137     if (is_pcie)
0138         pr_warn_once("%pOF: Missing device_type\n", np);
0139 
0140     return is_pcie;
0141 }
0142 
0143 static int of_bus_pci_match(struct device_node *np)
0144 {
0145     /*
0146      * "pciex" is PCI Express
0147      * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
0148      * "ht" is hypertransport
0149      *
0150      * If none of the device_type match, and that the node name is
0151      * "pcie", accept the device as PCI (with a warning).
0152      */
0153     return of_node_is_type(np, "pci") || of_node_is_type(np, "pciex") ||
0154         of_node_is_type(np, "vci") || of_node_is_type(np, "ht") ||
0155         of_node_is_pcie(np);
0156 }
0157 
0158 static void of_bus_pci_count_cells(struct device_node *np,
0159                    int *addrc, int *sizec)
0160 {
0161     if (addrc)
0162         *addrc = 3;
0163     if (sizec)
0164         *sizec = 2;
0165 }
0166 
0167 static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
0168         int pna)
0169 {
0170     u64 cp, s, da;
0171     unsigned int af, rf;
0172 
0173     af = of_bus_pci_get_flags(addr);
0174     rf = of_bus_pci_get_flags(range);
0175 
0176     /* Check address type match */
0177     if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
0178         return OF_BAD_ADDR;
0179 
0180     /* Read address values, skipping high cell */
0181     cp = of_read_number(range + 1, na - 1);
0182     s  = of_read_number(range + na + pna, ns);
0183     da = of_read_number(addr + 1, na - 1);
0184 
0185     pr_debug("PCI map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
0186 
0187     if (da < cp || da >= (cp + s))
0188         return OF_BAD_ADDR;
0189     return da - cp;
0190 }
0191 
0192 static int of_bus_pci_translate(__be32 *addr, u64 offset, int na)
0193 {
0194     return of_bus_default_translate(addr + 1, offset, na - 1);
0195 }
0196 #endif /* CONFIG_PCI */
0197 
0198 int of_pci_address_to_resource(struct device_node *dev, int bar,
0199                    struct resource *r)
0200 {
0201 
0202     if (!IS_ENABLED(CONFIG_PCI))
0203         return -ENOSYS;
0204 
0205     return __of_address_to_resource(dev, -1, bar, r);
0206 }
0207 EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
0208 
0209 /*
0210  * of_pci_range_to_resource - Create a resource from an of_pci_range
0211  * @range:  the PCI range that describes the resource
0212  * @np:     device node where the range belongs to
0213  * @res:    pointer to a valid resource that will be updated to
0214  *              reflect the values contained in the range.
0215  *
0216  * Returns EINVAL if the range cannot be converted to resource.
0217  *
0218  * Note that if the range is an IO range, the resource will be converted
0219  * using pci_address_to_pio() which can fail if it is called too early or
0220  * if the range cannot be matched to any host bridge IO space (our case here).
0221  * To guard against that we try to register the IO range first.
0222  * If that fails we know that pci_address_to_pio() will do too.
0223  */
0224 int of_pci_range_to_resource(struct of_pci_range *range,
0225                  struct device_node *np, struct resource *res)
0226 {
0227     int err;
0228     res->flags = range->flags;
0229     res->parent = res->child = res->sibling = NULL;
0230     res->name = np->full_name;
0231 
0232     if (!IS_ENABLED(CONFIG_PCI))
0233         return -ENOSYS;
0234 
0235     if (res->flags & IORESOURCE_IO) {
0236         unsigned long port;
0237         err = pci_register_io_range(&np->fwnode, range->cpu_addr,
0238                 range->size);
0239         if (err)
0240             goto invalid_range;
0241         port = pci_address_to_pio(range->cpu_addr);
0242         if (port == (unsigned long)-1) {
0243             err = -EINVAL;
0244             goto invalid_range;
0245         }
0246         res->start = port;
0247     } else {
0248         if ((sizeof(resource_size_t) < 8) &&
0249             upper_32_bits(range->cpu_addr)) {
0250             err = -EINVAL;
0251             goto invalid_range;
0252         }
0253 
0254         res->start = range->cpu_addr;
0255     }
0256     res->end = res->start + range->size - 1;
0257     return 0;
0258 
0259 invalid_range:
0260     res->start = (resource_size_t)OF_BAD_ADDR;
0261     res->end = (resource_size_t)OF_BAD_ADDR;
0262     return err;
0263 }
0264 EXPORT_SYMBOL(of_pci_range_to_resource);
0265 
0266 /*
0267  * ISA bus specific translator
0268  */
0269 
0270 static int of_bus_isa_match(struct device_node *np)
0271 {
0272     return of_node_name_eq(np, "isa");
0273 }
0274 
0275 static void of_bus_isa_count_cells(struct device_node *child,
0276                    int *addrc, int *sizec)
0277 {
0278     if (addrc)
0279         *addrc = 2;
0280     if (sizec)
0281         *sizec = 1;
0282 }
0283 
0284 static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
0285         int pna)
0286 {
0287     u64 cp, s, da;
0288 
0289     /* Check address type match */
0290     if ((addr[0] ^ range[0]) & cpu_to_be32(1))
0291         return OF_BAD_ADDR;
0292 
0293     /* Read address values, skipping high cell */
0294     cp = of_read_number(range + 1, na - 1);
0295     s  = of_read_number(range + na + pna, ns);
0296     da = of_read_number(addr + 1, na - 1);
0297 
0298     pr_debug("ISA map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
0299 
0300     if (da < cp || da >= (cp + s))
0301         return OF_BAD_ADDR;
0302     return da - cp;
0303 }
0304 
0305 static int of_bus_isa_translate(__be32 *addr, u64 offset, int na)
0306 {
0307     return of_bus_default_translate(addr + 1, offset, na - 1);
0308 }
0309 
0310 static unsigned int of_bus_isa_get_flags(const __be32 *addr)
0311 {
0312     unsigned int flags = 0;
0313     u32 w = be32_to_cpup(addr);
0314 
0315     if (w & 1)
0316         flags |= IORESOURCE_IO;
0317     else
0318         flags |= IORESOURCE_MEM;
0319     return flags;
0320 }
0321 
0322 /*
0323  * Array of bus specific translators
0324  */
0325 
0326 static struct of_bus of_busses[] = {
0327 #ifdef CONFIG_PCI
0328     /* PCI */
0329     {
0330         .name = "pci",
0331         .addresses = "assigned-addresses",
0332         .match = of_bus_pci_match,
0333         .count_cells = of_bus_pci_count_cells,
0334         .map = of_bus_pci_map,
0335         .translate = of_bus_pci_translate,
0336         .has_flags = true,
0337         .get_flags = of_bus_pci_get_flags,
0338     },
0339 #endif /* CONFIG_PCI */
0340     /* ISA */
0341     {
0342         .name = "isa",
0343         .addresses = "reg",
0344         .match = of_bus_isa_match,
0345         .count_cells = of_bus_isa_count_cells,
0346         .map = of_bus_isa_map,
0347         .translate = of_bus_isa_translate,
0348         .has_flags = true,
0349         .get_flags = of_bus_isa_get_flags,
0350     },
0351     /* Default */
0352     {
0353         .name = "default",
0354         .addresses = "reg",
0355         .match = NULL,
0356         .count_cells = of_bus_default_count_cells,
0357         .map = of_bus_default_map,
0358         .translate = of_bus_default_translate,
0359         .get_flags = of_bus_default_get_flags,
0360     },
0361 };
0362 
0363 static struct of_bus *of_match_bus(struct device_node *np)
0364 {
0365     int i;
0366 
0367     for (i = 0; i < ARRAY_SIZE(of_busses); i++)
0368         if (!of_busses[i].match || of_busses[i].match(np))
0369             return &of_busses[i];
0370     BUG();
0371     return NULL;
0372 }
0373 
0374 static int of_empty_ranges_quirk(struct device_node *np)
0375 {
0376     if (IS_ENABLED(CONFIG_PPC)) {
0377         /* To save cycles, we cache the result for global "Mac" setting */
0378         static int quirk_state = -1;
0379 
0380         /* PA-SEMI sdc DT bug */
0381         if (of_device_is_compatible(np, "1682m-sdc"))
0382             return true;
0383 
0384         /* Make quirk cached */
0385         if (quirk_state < 0)
0386             quirk_state =
0387                 of_machine_is_compatible("Power Macintosh") ||
0388                 of_machine_is_compatible("MacRISC");
0389         return quirk_state;
0390     }
0391     return false;
0392 }
0393 
0394 static int of_translate_one(struct device_node *parent, struct of_bus *bus,
0395                 struct of_bus *pbus, __be32 *addr,
0396                 int na, int ns, int pna, const char *rprop)
0397 {
0398     const __be32 *ranges;
0399     unsigned int rlen;
0400     int rone;
0401     u64 offset = OF_BAD_ADDR;
0402 
0403     /*
0404      * Normally, an absence of a "ranges" property means we are
0405      * crossing a non-translatable boundary, and thus the addresses
0406      * below the current cannot be converted to CPU physical ones.
0407      * Unfortunately, while this is very clear in the spec, it's not
0408      * what Apple understood, and they do have things like /uni-n or
0409      * /ht nodes with no "ranges" property and a lot of perfectly
0410      * useable mapped devices below them. Thus we treat the absence of
0411      * "ranges" as equivalent to an empty "ranges" property which means
0412      * a 1:1 translation at that level. It's up to the caller not to try
0413      * to translate addresses that aren't supposed to be translated in
0414      * the first place. --BenH.
0415      *
0416      * As far as we know, this damage only exists on Apple machines, so
0417      * This code is only enabled on powerpc. --gcl
0418      *
0419      * This quirk also applies for 'dma-ranges' which frequently exist in
0420      * child nodes without 'dma-ranges' in the parent nodes. --RobH
0421      */
0422     ranges = of_get_property(parent, rprop, &rlen);
0423     if (ranges == NULL && !of_empty_ranges_quirk(parent) &&
0424         strcmp(rprop, "dma-ranges")) {
0425         pr_debug("no ranges; cannot translate\n");
0426         return 1;
0427     }
0428     if (ranges == NULL || rlen == 0) {
0429         offset = of_read_number(addr, na);
0430         memset(addr, 0, pna * 4);
0431         pr_debug("empty ranges; 1:1 translation\n");
0432         goto finish;
0433     }
0434 
0435     pr_debug("walking ranges...\n");
0436 
0437     /* Now walk through the ranges */
0438     rlen /= 4;
0439     rone = na + pna + ns;
0440     for (; rlen >= rone; rlen -= rone, ranges += rone) {
0441         offset = bus->map(addr, ranges, na, ns, pna);
0442         if (offset != OF_BAD_ADDR)
0443             break;
0444     }
0445     if (offset == OF_BAD_ADDR) {
0446         pr_debug("not found !\n");
0447         return 1;
0448     }
0449     memcpy(addr, ranges + na, 4 * pna);
0450 
0451  finish:
0452     of_dump_addr("parent translation for:", addr, pna);
0453     pr_debug("with offset: %llx\n", offset);
0454 
0455     /* Translate it into parent bus space */
0456     return pbus->translate(addr, offset, pna);
0457 }
0458 
0459 /*
0460  * Translate an address from the device-tree into a CPU physical address,
0461  * this walks up the tree and applies the various bus mappings on the
0462  * way.
0463  *
0464  * Note: We consider that crossing any level with #size-cells == 0 to mean
0465  * that translation is impossible (that is we are not dealing with a value
0466  * that can be mapped to a cpu physical address). This is not really specified
0467  * that way, but this is traditionally the way IBM at least do things
0468  *
0469  * Whenever the translation fails, the *host pointer will be set to the
0470  * device that had registered logical PIO mapping, and the return code is
0471  * relative to that node.
0472  */
0473 static u64 __of_translate_address(struct device_node *dev,
0474                   struct device_node *(*get_parent)(const struct device_node *),
0475                   const __be32 *in_addr, const char *rprop,
0476                   struct device_node **host)
0477 {
0478     struct device_node *parent = NULL;
0479     struct of_bus *bus, *pbus;
0480     __be32 addr[OF_MAX_ADDR_CELLS];
0481     int na, ns, pna, pns;
0482     u64 result = OF_BAD_ADDR;
0483 
0484     pr_debug("** translation for device %pOF **\n", dev);
0485 
0486     /* Increase refcount at current level */
0487     of_node_get(dev);
0488 
0489     *host = NULL;
0490     /* Get parent & match bus type */
0491     parent = get_parent(dev);
0492     if (parent == NULL)
0493         goto bail;
0494     bus = of_match_bus(parent);
0495 
0496     /* Count address cells & copy address locally */
0497     bus->count_cells(dev, &na, &ns);
0498     if (!OF_CHECK_COUNTS(na, ns)) {
0499         pr_debug("Bad cell count for %pOF\n", dev);
0500         goto bail;
0501     }
0502     memcpy(addr, in_addr, na * 4);
0503 
0504     pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",
0505         bus->name, na, ns, parent);
0506     of_dump_addr("translating address:", addr, na);
0507 
0508     /* Translate */
0509     for (;;) {
0510         struct logic_pio_hwaddr *iorange;
0511 
0512         /* Switch to parent bus */
0513         of_node_put(dev);
0514         dev = parent;
0515         parent = get_parent(dev);
0516 
0517         /* If root, we have finished */
0518         if (parent == NULL) {
0519             pr_debug("reached root node\n");
0520             result = of_read_number(addr, na);
0521             break;
0522         }
0523 
0524         /*
0525          * For indirectIO device which has no ranges property, get
0526          * the address from reg directly.
0527          */
0528         iorange = find_io_range_by_fwnode(&dev->fwnode);
0529         if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) {
0530             result = of_read_number(addr + 1, na - 1);
0531             pr_debug("indirectIO matched(%pOF) 0x%llx\n",
0532                  dev, result);
0533             *host = of_node_get(dev);
0534             break;
0535         }
0536 
0537         /* Get new parent bus and counts */
0538         pbus = of_match_bus(parent);
0539         pbus->count_cells(dev, &pna, &pns);
0540         if (!OF_CHECK_COUNTS(pna, pns)) {
0541             pr_err("Bad cell count for %pOF\n", dev);
0542             break;
0543         }
0544 
0545         pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
0546             pbus->name, pna, pns, parent);
0547 
0548         /* Apply bus translation */
0549         if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
0550             break;
0551 
0552         /* Complete the move up one level */
0553         na = pna;
0554         ns = pns;
0555         bus = pbus;
0556 
0557         of_dump_addr("one level translation:", addr, na);
0558     }
0559  bail:
0560     of_node_put(parent);
0561     of_node_put(dev);
0562 
0563     return result;
0564 }
0565 
0566 u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
0567 {
0568     struct device_node *host;
0569     u64 ret;
0570 
0571     ret = __of_translate_address(dev, of_get_parent,
0572                      in_addr, "ranges", &host);
0573     if (host) {
0574         of_node_put(host);
0575         return OF_BAD_ADDR;
0576     }
0577 
0578     return ret;
0579 }
0580 EXPORT_SYMBOL(of_translate_address);
0581 
0582 static struct device_node *__of_get_dma_parent(const struct device_node *np)
0583 {
0584     struct of_phandle_args args;
0585     int ret, index;
0586 
0587     index = of_property_match_string(np, "interconnect-names", "dma-mem");
0588     if (index < 0)
0589         return of_get_parent(np);
0590 
0591     ret = of_parse_phandle_with_args(np, "interconnects",
0592                      "#interconnect-cells",
0593                      index, &args);
0594     if (ret < 0)
0595         return of_get_parent(np);
0596 
0597     return of_node_get(args.np);
0598 }
0599 
0600 static struct device_node *of_get_next_dma_parent(struct device_node *np)
0601 {
0602     struct device_node *parent;
0603 
0604     parent = __of_get_dma_parent(np);
0605     of_node_put(np);
0606 
0607     return parent;
0608 }
0609 
0610 u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
0611 {
0612     struct device_node *host;
0613     u64 ret;
0614 
0615     ret = __of_translate_address(dev, __of_get_dma_parent,
0616                      in_addr, "dma-ranges", &host);
0617 
0618     if (host) {
0619         of_node_put(host);
0620         return OF_BAD_ADDR;
0621     }
0622 
0623     return ret;
0624 }
0625 EXPORT_SYMBOL(of_translate_dma_address);
0626 
0627 const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,
0628                    u64 *size, unsigned int *flags)
0629 {
0630     const __be32 *prop;
0631     unsigned int psize;
0632     struct device_node *parent;
0633     struct of_bus *bus;
0634     int onesize, i, na, ns;
0635 
0636     /* Get parent & match bus type */
0637     parent = of_get_parent(dev);
0638     if (parent == NULL)
0639         return NULL;
0640     bus = of_match_bus(parent);
0641     if (strcmp(bus->name, "pci") && (bar_no >= 0)) {
0642         of_node_put(parent);
0643         return NULL;
0644     }
0645     bus->count_cells(dev, &na, &ns);
0646     of_node_put(parent);
0647     if (!OF_CHECK_ADDR_COUNT(na))
0648         return NULL;
0649 
0650     /* Get "reg" or "assigned-addresses" property */
0651     prop = of_get_property(dev, bus->addresses, &psize);
0652     if (prop == NULL)
0653         return NULL;
0654     psize /= 4;
0655 
0656     onesize = na + ns;
0657     for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
0658         u32 val = be32_to_cpu(prop[0]);
0659         /* PCI bus matches on BAR number instead of index */
0660         if (((bar_no >= 0) && ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0))) ||
0661             ((index >= 0) && (i == index))) {
0662             if (size)
0663                 *size = of_read_number(prop + na, ns);
0664             if (flags)
0665                 *flags = bus->get_flags(prop);
0666             return prop;
0667         }
0668     }
0669     return NULL;
0670 }
0671 EXPORT_SYMBOL(__of_get_address);
0672 
0673 static int parser_init(struct of_pci_range_parser *parser,
0674             struct device_node *node, const char *name)
0675 {
0676     int rlen;
0677 
0678     parser->node = node;
0679     parser->pna = of_n_addr_cells(node);
0680     parser->na = of_bus_n_addr_cells(node);
0681     parser->ns = of_bus_n_size_cells(node);
0682     parser->dma = !strcmp(name, "dma-ranges");
0683     parser->bus = of_match_bus(node);
0684 
0685     parser->range = of_get_property(node, name, &rlen);
0686     if (parser->range == NULL)
0687         return -ENOENT;
0688 
0689     parser->end = parser->range + rlen / sizeof(__be32);
0690 
0691     return 0;
0692 }
0693 
0694 int of_pci_range_parser_init(struct of_pci_range_parser *parser,
0695                 struct device_node *node)
0696 {
0697     return parser_init(parser, node, "ranges");
0698 }
0699 EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
0700 
0701 int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
0702                 struct device_node *node)
0703 {
0704     return parser_init(parser, node, "dma-ranges");
0705 }
0706 EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init);
0707 #define of_dma_range_parser_init of_pci_dma_range_parser_init
0708 
0709 struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
0710                         struct of_pci_range *range)
0711 {
0712     int na = parser->na;
0713     int ns = parser->ns;
0714     int np = parser->pna + na + ns;
0715     int busflag_na = 0;
0716 
0717     if (!range)
0718         return NULL;
0719 
0720     if (!parser->range || parser->range + np > parser->end)
0721         return NULL;
0722 
0723     range->flags = parser->bus->get_flags(parser->range);
0724 
0725     /* A extra cell for resource flags */
0726     if (parser->bus->has_flags)
0727         busflag_na = 1;
0728 
0729     range->bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
0730 
0731     if (parser->dma)
0732         range->cpu_addr = of_translate_dma_address(parser->node,
0733                 parser->range + na);
0734     else
0735         range->cpu_addr = of_translate_address(parser->node,
0736                 parser->range + na);
0737     range->size = of_read_number(parser->range + parser->pna + na, ns);
0738 
0739     parser->range += np;
0740 
0741     /* Now consume following elements while they are contiguous */
0742     while (parser->range + np <= parser->end) {
0743         u32 flags = 0;
0744         u64 bus_addr, cpu_addr, size;
0745 
0746         flags = parser->bus->get_flags(parser->range);
0747         bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
0748         if (parser->dma)
0749             cpu_addr = of_translate_dma_address(parser->node,
0750                     parser->range + na);
0751         else
0752             cpu_addr = of_translate_address(parser->node,
0753                     parser->range + na);
0754         size = of_read_number(parser->range + parser->pna + na, ns);
0755 
0756         if (flags != range->flags)
0757             break;
0758         if (bus_addr != range->bus_addr + range->size ||
0759             cpu_addr != range->cpu_addr + range->size)
0760             break;
0761 
0762         range->size += size;
0763         parser->range += np;
0764     }
0765 
0766     return range;
0767 }
0768 EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
0769 
0770 static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr,
0771             u64 size)
0772 {
0773     u64 taddr;
0774     unsigned long port;
0775     struct device_node *host;
0776 
0777     taddr = __of_translate_address(dev, of_get_parent,
0778                        in_addr, "ranges", &host);
0779     if (host) {
0780         /* host-specific port access */
0781         port = logic_pio_trans_hwaddr(&host->fwnode, taddr, size);
0782         of_node_put(host);
0783     } else {
0784         /* memory-mapped I/O range */
0785         port = pci_address_to_pio(taddr);
0786     }
0787 
0788     if (port == (unsigned long)-1)
0789         return OF_BAD_ADDR;
0790 
0791     return port;
0792 }
0793 
0794 static int __of_address_to_resource(struct device_node *dev, int index, int bar_no,
0795         struct resource *r)
0796 {
0797     u64 taddr;
0798     const __be32    *addrp;
0799     u64     size;
0800     unsigned int    flags;
0801     const char  *name = NULL;
0802 
0803     addrp = __of_get_address(dev, index, bar_no, &size, &flags);
0804     if (addrp == NULL)
0805         return -EINVAL;
0806 
0807     /* Get optional "reg-names" property to add a name to a resource */
0808     if (index >= 0)
0809         of_property_read_string_index(dev, "reg-names", index, &name);
0810 
0811     if (flags & IORESOURCE_MEM)
0812         taddr = of_translate_address(dev, addrp);
0813     else if (flags & IORESOURCE_IO)
0814         taddr = of_translate_ioport(dev, addrp, size);
0815     else
0816         return -EINVAL;
0817 
0818     if (taddr == OF_BAD_ADDR)
0819         return -EINVAL;
0820     memset(r, 0, sizeof(struct resource));
0821 
0822     if (of_mmio_is_nonposted(dev))
0823         flags |= IORESOURCE_MEM_NONPOSTED;
0824 
0825     r->start = taddr;
0826     r->end = taddr + size - 1;
0827     r->flags = flags;
0828     r->name = name ? name : dev->full_name;
0829 
0830     return 0;
0831 }
0832 
0833 /**
0834  * of_address_to_resource - Translate device tree address and return as resource
0835  * @dev:    Caller's Device Node
0836  * @index:  Index into the array
0837  * @r:      Pointer to resource array
0838  *
0839  * Note that if your address is a PIO address, the conversion will fail if
0840  * the physical address can't be internally converted to an IO token with
0841  * pci_address_to_pio(), that is because it's either called too early or it
0842  * can't be matched to any host bridge IO space
0843  */
0844 int of_address_to_resource(struct device_node *dev, int index,
0845                struct resource *r)
0846 {
0847     return __of_address_to_resource(dev, index, -1, r);
0848 }
0849 EXPORT_SYMBOL_GPL(of_address_to_resource);
0850 
0851 /**
0852  * of_iomap - Maps the memory mapped IO for a given device_node
0853  * @np:     the device whose io range will be mapped
0854  * @index:  index of the io range
0855  *
0856  * Returns a pointer to the mapped memory
0857  */
0858 void __iomem *of_iomap(struct device_node *np, int index)
0859 {
0860     struct resource res;
0861 
0862     if (of_address_to_resource(np, index, &res))
0863         return NULL;
0864 
0865     if (res.flags & IORESOURCE_MEM_NONPOSTED)
0866         return ioremap_np(res.start, resource_size(&res));
0867     else
0868         return ioremap(res.start, resource_size(&res));
0869 }
0870 EXPORT_SYMBOL(of_iomap);
0871 
0872 /*
0873  * of_io_request_and_map - Requests a resource and maps the memory mapped IO
0874  *             for a given device_node
0875  * @device: the device whose io range will be mapped
0876  * @index:  index of the io range
0877  * @name:   name "override" for the memory region request or NULL
0878  *
0879  * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
0880  * error code on failure. Usage example:
0881  *
0882  *  base = of_io_request_and_map(node, 0, "foo");
0883  *  if (IS_ERR(base))
0884  *      return PTR_ERR(base);
0885  */
0886 void __iomem *of_io_request_and_map(struct device_node *np, int index,
0887                     const char *name)
0888 {
0889     struct resource res;
0890     void __iomem *mem;
0891 
0892     if (of_address_to_resource(np, index, &res))
0893         return IOMEM_ERR_PTR(-EINVAL);
0894 
0895     if (!name)
0896         name = res.name;
0897     if (!request_mem_region(res.start, resource_size(&res), name))
0898         return IOMEM_ERR_PTR(-EBUSY);
0899 
0900     if (res.flags & IORESOURCE_MEM_NONPOSTED)
0901         mem = ioremap_np(res.start, resource_size(&res));
0902     else
0903         mem = ioremap(res.start, resource_size(&res));
0904 
0905     if (!mem) {
0906         release_mem_region(res.start, resource_size(&res));
0907         return IOMEM_ERR_PTR(-ENOMEM);
0908     }
0909 
0910     return mem;
0911 }
0912 EXPORT_SYMBOL(of_io_request_and_map);
0913 
0914 #ifdef CONFIG_HAS_DMA
0915 /**
0916  * of_dma_get_range - Get DMA range info and put it into a map array
0917  * @np:     device node to get DMA range info
0918  * @map:    dma range structure to return
0919  *
0920  * Look in bottom up direction for the first "dma-ranges" property
0921  * and parse it.  Put the information into a DMA offset map array.
0922  *
0923  * dma-ranges format:
0924  *  DMA addr (dma_addr) : naddr cells
0925  *  CPU addr (phys_addr_t)  : pna cells
0926  *  size            : nsize cells
0927  *
0928  * It returns -ENODEV if "dma-ranges" property was not found for this
0929  * device in the DT.
0930  */
0931 int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map)
0932 {
0933     struct device_node *node = of_node_get(np);
0934     const __be32 *ranges = NULL;
0935     bool found_dma_ranges = false;
0936     struct of_range_parser parser;
0937     struct of_range range;
0938     struct bus_dma_region *r;
0939     int len, num_ranges = 0;
0940     int ret = 0;
0941 
0942     while (node) {
0943         ranges = of_get_property(node, "dma-ranges", &len);
0944 
0945         /* Ignore empty ranges, they imply no translation required */
0946         if (ranges && len > 0)
0947             break;
0948 
0949         /* Once we find 'dma-ranges', then a missing one is an error */
0950         if (found_dma_ranges && !ranges) {
0951             ret = -ENODEV;
0952             goto out;
0953         }
0954         found_dma_ranges = true;
0955 
0956         node = of_get_next_dma_parent(node);
0957     }
0958 
0959     if (!node || !ranges) {
0960         pr_debug("no dma-ranges found for node(%pOF)\n", np);
0961         ret = -ENODEV;
0962         goto out;
0963     }
0964 
0965     of_dma_range_parser_init(&parser, node);
0966     for_each_of_range(&parser, &range)
0967         num_ranges++;
0968 
0969     r = kcalloc(num_ranges + 1, sizeof(*r), GFP_KERNEL);
0970     if (!r) {
0971         ret = -ENOMEM;
0972         goto out;
0973     }
0974 
0975     /*
0976      * Record all info in the generic DMA ranges array for struct device.
0977      */
0978     *map = r;
0979     of_dma_range_parser_init(&parser, node);
0980     for_each_of_range(&parser, &range) {
0981         pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
0982              range.bus_addr, range.cpu_addr, range.size);
0983         if (range.cpu_addr == OF_BAD_ADDR) {
0984             pr_err("translation of DMA address(%llx) to CPU address failed node(%pOF)\n",
0985                    range.bus_addr, node);
0986             continue;
0987         }
0988         r->cpu_start = range.cpu_addr;
0989         r->dma_start = range.bus_addr;
0990         r->size = range.size;
0991         r->offset = range.cpu_addr - range.bus_addr;
0992         r++;
0993     }
0994 out:
0995     of_node_put(node);
0996     return ret;
0997 }
0998 #endif /* CONFIG_HAS_DMA */
0999 
1000 /**
1001  * of_dma_get_max_cpu_address - Gets highest CPU address suitable for DMA
1002  * @np: The node to start searching from or NULL to start from the root
1003  *
1004  * Gets the highest CPU physical address that is addressable by all DMA masters
1005  * in the sub-tree pointed by np, or the whole tree if NULL is passed. If no
1006  * DMA constrained device is found, it returns PHYS_ADDR_MAX.
1007  */
1008 phys_addr_t __init of_dma_get_max_cpu_address(struct device_node *np)
1009 {
1010     phys_addr_t max_cpu_addr = PHYS_ADDR_MAX;
1011     struct of_range_parser parser;
1012     phys_addr_t subtree_max_addr;
1013     struct device_node *child;
1014     struct of_range range;
1015     const __be32 *ranges;
1016     u64 cpu_end = 0;
1017     int len;
1018 
1019     if (!np)
1020         np = of_root;
1021 
1022     ranges = of_get_property(np, "dma-ranges", &len);
1023     if (ranges && len) {
1024         of_dma_range_parser_init(&parser, np);
1025         for_each_of_range(&parser, &range)
1026             if (range.cpu_addr + range.size > cpu_end)
1027                 cpu_end = range.cpu_addr + range.size - 1;
1028 
1029         if (max_cpu_addr > cpu_end)
1030             max_cpu_addr = cpu_end;
1031     }
1032 
1033     for_each_available_child_of_node(np, child) {
1034         subtree_max_addr = of_dma_get_max_cpu_address(child);
1035         if (max_cpu_addr > subtree_max_addr)
1036             max_cpu_addr = subtree_max_addr;
1037     }
1038 
1039     return max_cpu_addr;
1040 }
1041 
1042 /**
1043  * of_dma_is_coherent - Check if device is coherent
1044  * @np: device node
1045  *
1046  * It returns true if "dma-coherent" property was found
1047  * for this device in the DT, or if DMA is coherent by
1048  * default for OF devices on the current platform and no
1049  * "dma-noncoherent" property was found for this device.
1050  */
1051 bool of_dma_is_coherent(struct device_node *np)
1052 {
1053     struct device_node *node;
1054     bool is_coherent = IS_ENABLED(CONFIG_OF_DMA_DEFAULT_COHERENT);
1055 
1056     node = of_node_get(np);
1057 
1058     while (node) {
1059         if (of_property_read_bool(node, "dma-coherent")) {
1060             is_coherent = true;
1061             break;
1062         }
1063         if (of_property_read_bool(node, "dma-noncoherent")) {
1064             is_coherent = false;
1065             break;
1066         }
1067         node = of_get_next_dma_parent(node);
1068     }
1069     of_node_put(node);
1070     return is_coherent;
1071 }
1072 EXPORT_SYMBOL_GPL(of_dma_is_coherent);
1073 
1074 /**
1075  * of_mmio_is_nonposted - Check if device uses non-posted MMIO
1076  * @np: device node
1077  *
1078  * Returns true if the "nonposted-mmio" property was found for
1079  * the device's bus.
1080  *
1081  * This is currently only enabled on builds that support Apple ARM devices, as
1082  * an optimization.
1083  */
1084 static bool of_mmio_is_nonposted(struct device_node *np)
1085 {
1086     struct device_node *parent;
1087     bool nonposted;
1088 
1089     if (!IS_ENABLED(CONFIG_ARCH_APPLE))
1090         return false;
1091 
1092     parent = of_get_parent(np);
1093     if (!parent)
1094         return false;
1095 
1096     nonposted = of_property_read_bool(parent, "nonposted-mmio");
1097 
1098     of_node_put(parent);
1099     return nonposted;
1100 }