Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * FDT Address translation based on u-boot fdt_support.c which in turn was
0004  * based on the kernel unflattened DT address translation code.
0005  *
0006  * (C) Copyright 2007
0007  * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
0008  *
0009  * Copyright 2010-2011 Freescale Semiconductor, Inc.
0010  */
0011 
0012 #define pr_fmt(fmt) "OF: fdt: " fmt
0013 
0014 #include <linux/kernel.h>
0015 #include <linux/libfdt.h>
0016 #include <linux/of.h>
0017 #include <linux/of_fdt.h>
0018 #include <linux/sizes.h>
0019 
0020 /* Max address size we deal with */
0021 #define OF_MAX_ADDR_CELLS   4
0022 #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
0023             (ns) > 0)
0024 
0025 /* Debug utility */
0026 #ifdef DEBUG
0027 static void __init of_dump_addr(const char *s, const __be32 *addr, int na)
0028 {
0029     pr_debug("%s", s);
0030     while(na--)
0031         pr_cont(" %08x", *(addr++));
0032     pr_cont("\n");
0033 }
0034 #else
0035 static void __init of_dump_addr(const char *s, const __be32 *addr, int na) { }
0036 #endif
0037 
0038 /* Callbacks for bus specific translators */
0039 struct of_bus {
0040     void        (*count_cells)(const void *blob, int parentoffset,
0041                 int *addrc, int *sizec);
0042     u64     (*map)(__be32 *addr, const __be32 *range,
0043                 int na, int ns, int pna);
0044     int     (*translate)(__be32 *addr, u64 offset, int na);
0045 };
0046 
0047 /* Default translator (generic bus) */
0048 static void __init fdt_bus_default_count_cells(const void *blob, int parentoffset,
0049                            int *addrc, int *sizec)
0050 {
0051     const __be32 *prop;
0052 
0053     if (addrc) {
0054         prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
0055         if (prop)
0056             *addrc = be32_to_cpup(prop);
0057         else
0058             *addrc = dt_root_addr_cells;
0059     }
0060 
0061     if (sizec) {
0062         prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
0063         if (prop)
0064             *sizec = be32_to_cpup(prop);
0065         else
0066             *sizec = dt_root_size_cells;
0067     }
0068 }
0069 
0070 static u64 __init fdt_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",
0080         cp, s, da);
0081 
0082     if (da < cp || da >= (cp + s))
0083         return OF_BAD_ADDR;
0084     return da - cp;
0085 }
0086 
0087 static int __init fdt_bus_default_translate(__be32 *addr, u64 offset, int na)
0088 {
0089     u64 a = of_read_number(addr, na);
0090     memset(addr, 0, na * 4);
0091     a += offset;
0092     if (na > 1)
0093         addr[na - 2] = cpu_to_fdt32(a >> 32);
0094     addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
0095 
0096     return 0;
0097 }
0098 
0099 /* Array of bus specific translators */
0100 static const struct of_bus of_busses[] __initconst = {
0101     /* Default */
0102     {
0103         .count_cells = fdt_bus_default_count_cells,
0104         .map = fdt_bus_default_map,
0105         .translate = fdt_bus_default_translate,
0106     },
0107 };
0108 
0109 static int __init fdt_translate_one(const void *blob, int parent,
0110                     const struct of_bus *bus,
0111                     const struct of_bus *pbus, __be32 *addr,
0112                     int na, int ns, int pna, const char *rprop)
0113 {
0114     const __be32 *ranges;
0115     int rlen;
0116     int rone;
0117     u64 offset = OF_BAD_ADDR;
0118 
0119     ranges = fdt_getprop(blob, parent, rprop, &rlen);
0120     if (!ranges)
0121         return 1;
0122     if (rlen == 0) {
0123         offset = of_read_number(addr, na);
0124         memset(addr, 0, pna * 4);
0125         pr_debug("empty ranges, 1:1 translation\n");
0126         goto finish;
0127     }
0128 
0129     pr_debug("walking ranges...\n");
0130 
0131     /* Now walk through the ranges */
0132     rlen /= 4;
0133     rone = na + pna + ns;
0134     for (; rlen >= rone; rlen -= rone, ranges += rone) {
0135         offset = bus->map(addr, ranges, na, ns, pna);
0136         if (offset != OF_BAD_ADDR)
0137             break;
0138     }
0139     if (offset == OF_BAD_ADDR) {
0140         pr_debug("not found !\n");
0141         return 1;
0142     }
0143     memcpy(addr, ranges + na, 4 * pna);
0144 
0145  finish:
0146     of_dump_addr("parent translation for:", addr, pna);
0147     pr_debug("with offset: %llx\n", offset);
0148 
0149     /* Translate it into parent bus space */
0150     return pbus->translate(addr, offset, pna);
0151 }
0152 
0153 /*
0154  * Translate an address from the device-tree into a CPU physical address,
0155  * this walks up the tree and applies the various bus mappings on the
0156  * way.
0157  *
0158  * Note: We consider that crossing any level with #size-cells == 0 to mean
0159  * that translation is impossible (that is we are not dealing with a value
0160  * that can be mapped to a cpu physical address). This is not really specified
0161  * that way, but this is traditionally the way IBM at least do things
0162  */
0163 static u64 __init fdt_translate_address(const void *blob, int node_offset)
0164 {
0165     int parent, len;
0166     const struct of_bus *bus, *pbus;
0167     const __be32 *reg;
0168     __be32 addr[OF_MAX_ADDR_CELLS];
0169     int na, ns, pna, pns;
0170     u64 result = OF_BAD_ADDR;
0171 
0172     pr_debug("** translation for device %s **\n",
0173          fdt_get_name(blob, node_offset, NULL));
0174 
0175     reg = fdt_getprop(blob, node_offset, "reg", &len);
0176     if (!reg) {
0177         pr_err("warning: device tree node '%s' has no address.\n",
0178             fdt_get_name(blob, node_offset, NULL));
0179         goto bail;
0180     }
0181 
0182     /* Get parent & match bus type */
0183     parent = fdt_parent_offset(blob, node_offset);
0184     if (parent < 0)
0185         goto bail;
0186     bus = &of_busses[0];
0187 
0188     /* Cound address cells & copy address locally */
0189     bus->count_cells(blob, parent, &na, &ns);
0190     if (!OF_CHECK_COUNTS(na, ns)) {
0191         pr_err("Bad cell count for %s\n",
0192                fdt_get_name(blob, node_offset, NULL));
0193         goto bail;
0194     }
0195     memcpy(addr, reg, na * 4);
0196 
0197     pr_debug("bus (na=%d, ns=%d) on %s\n",
0198          na, ns, fdt_get_name(blob, parent, NULL));
0199     of_dump_addr("translating address:", addr, na);
0200 
0201     /* Translate */
0202     for (;;) {
0203         /* Switch to parent bus */
0204         node_offset = parent;
0205         parent = fdt_parent_offset(blob, node_offset);
0206 
0207         /* If root, we have finished */
0208         if (parent < 0) {
0209             pr_debug("reached root node\n");
0210             result = of_read_number(addr, na);
0211             break;
0212         }
0213 
0214         /* Get new parent bus and counts */
0215         pbus = &of_busses[0];
0216         pbus->count_cells(blob, parent, &pna, &pns);
0217         if (!OF_CHECK_COUNTS(pna, pns)) {
0218             pr_err("Bad cell count for %s\n",
0219                 fdt_get_name(blob, node_offset, NULL));
0220             break;
0221         }
0222 
0223         pr_debug("parent bus (na=%d, ns=%d) on %s\n",
0224              pna, pns, fdt_get_name(blob, parent, NULL));
0225 
0226         /* Apply bus translation */
0227         if (fdt_translate_one(blob, node_offset, bus, pbus,
0228                     addr, na, ns, pna, "ranges"))
0229             break;
0230 
0231         /* Complete the move up one level */
0232         na = pna;
0233         ns = pns;
0234         bus = pbus;
0235 
0236         of_dump_addr("one level translation:", addr, na);
0237     }
0238  bail:
0239     return result;
0240 }
0241 
0242 /**
0243  * of_flat_dt_translate_address - translate DT addr into CPU phys addr
0244  * @node: node in the flat blob
0245  */
0246 u64 __init of_flat_dt_translate_address(unsigned long node)
0247 {
0248     return fdt_translate_address(initial_boot_params, node);
0249 }