LCOV - code coverage report
Current view: top level - drivers/of - fdt_address.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 0 63 0.0 %
Date: 2020-09-30 20:25:40 Functions: 0 6 0.0 %
Branches: 0 40 0.0 %

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0+
       2                 :            : /*
       3                 :            :  * FDT Address translation based on u-boot fdt_support.c which in turn was
       4                 :            :  * based on the kernel unflattened DT address translation code.
       5                 :            :  *
       6                 :            :  * (C) Copyright 2007
       7                 :            :  * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
       8                 :            :  *
       9                 :            :  * Copyright 2010-2011 Freescale Semiconductor, Inc.
      10                 :            :  */
      11                 :            : 
      12                 :            : #define pr_fmt(fmt)     "OF: fdt: " fmt
      13                 :            : 
      14                 :            : #include <linux/kernel.h>
      15                 :            : #include <linux/libfdt.h>
      16                 :            : #include <linux/of.h>
      17                 :            : #include <linux/of_fdt.h>
      18                 :            : #include <linux/sizes.h>
      19                 :            : 
      20                 :            : /* Max address size we deal with */
      21                 :            : #define OF_MAX_ADDR_CELLS       4
      22                 :            : #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
      23                 :            :                         (ns) > 0)
      24                 :            : 
      25                 :            : /* Debug utility */
      26                 :            : #ifdef DEBUG
      27                 :            : static void __init of_dump_addr(const char *s, const __be32 *addr, int na)
      28                 :            : {
      29                 :            :         pr_debug("%s", s);
      30                 :            :         while(na--)
      31                 :            :                 pr_cont(" %08x", *(addr++));
      32                 :            :         pr_cont("\n");
      33                 :            : }
      34                 :            : #else
      35                 :            : static void __init of_dump_addr(const char *s, const __be32 *addr, int na) { }
      36                 :            : #endif
      37                 :            : 
      38                 :            : /* Callbacks for bus specific translators */
      39                 :            : struct of_bus {
      40                 :            :         void            (*count_cells)(const void *blob, int parentoffset,
      41                 :            :                                 int *addrc, int *sizec);
      42                 :            :         u64             (*map)(__be32 *addr, const __be32 *range,
      43                 :            :                                 int na, int ns, int pna);
      44                 :            :         int             (*translate)(__be32 *addr, u64 offset, int na);
      45                 :            : };
      46                 :            : 
      47                 :            : /* Default translator (generic bus) */
      48                 :          0 : static void __init fdt_bus_default_count_cells(const void *blob, int parentoffset,
      49                 :            :                                                int *addrc, int *sizec)
      50                 :            : {
      51                 :            :         const __be32 *prop;
      52                 :            : 
      53         [ #  # ]:          0 :         if (addrc) {
      54                 :          0 :                 prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
      55         [ #  # ]:          0 :                 if (prop)
      56                 :          0 :                         *addrc = be32_to_cpup(prop);
      57                 :            :                 else
      58                 :          0 :                         *addrc = dt_root_addr_cells;
      59                 :            :         }
      60                 :            : 
      61         [ #  # ]:          0 :         if (sizec) {
      62                 :          0 :                 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
      63         [ #  # ]:          0 :                 if (prop)
      64                 :          0 :                         *sizec = be32_to_cpup(prop);
      65                 :            :                 else
      66                 :          0 :                         *sizec = dt_root_size_cells;
      67                 :            :         }
      68                 :          0 : }
      69                 :            : 
      70                 :          0 : static u64 __init fdt_bus_default_map(__be32 *addr, const __be32 *range,
      71                 :            :                                       int na, int ns, int pna)
      72                 :            : {
      73                 :            :         u64 cp, s, da;
      74                 :            : 
      75                 :            :         cp = of_read_number(range, na);
      76                 :          0 :         s  = of_read_number(range + na + pna, ns);
      77                 :            :         da = of_read_number(addr, na);
      78                 :            : 
      79                 :            :         pr_debug("default map, cp=%llx, s=%llx, da=%llx\n",
      80                 :            :             cp, s, da);
      81                 :            : 
      82   [ #  #  #  # ]:          0 :         if (da < cp || da >= (cp + s))
      83                 :            :                 return OF_BAD_ADDR;
      84                 :          0 :         return da - cp;
      85                 :            : }
      86                 :            : 
      87                 :          0 : static int __init fdt_bus_default_translate(__be32 *addr, u64 offset, int na)
      88                 :            : {
      89                 :            :         u64 a = of_read_number(addr, na);
      90                 :          0 :         memset(addr, 0, na * 4);
      91                 :          0 :         a += offset;
      92         [ #  # ]:          0 :         if (na > 1)
      93                 :          0 :                 addr[na - 2] = cpu_to_fdt32(a >> 32);
      94                 :          0 :         addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
      95                 :            : 
      96                 :          0 :         return 0;
      97                 :            : }
      98                 :            : 
      99                 :            : /* Array of bus specific translators */
     100                 :            : static const struct of_bus of_busses[] __initconst = {
     101                 :            :         /* Default */
     102                 :            :         {
     103                 :            :                 .count_cells = fdt_bus_default_count_cells,
     104                 :            :                 .map = fdt_bus_default_map,
     105                 :            :                 .translate = fdt_bus_default_translate,
     106                 :            :         },
     107                 :            : };
     108                 :            : 
     109                 :          0 : static int __init fdt_translate_one(const void *blob, int parent,
     110                 :            :                                     const struct of_bus *bus,
     111                 :            :                                     const struct of_bus *pbus, __be32 *addr,
     112                 :            :                                     int na, int ns, int pna, const char *rprop)
     113                 :            : {
     114                 :            :         const __be32 *ranges;
     115                 :            :         int rlen;
     116                 :            :         int rone;
     117                 :            :         u64 offset = OF_BAD_ADDR;
     118                 :            : 
     119                 :          0 :         ranges = fdt_getprop(blob, parent, rprop, &rlen);
     120         [ #  # ]:          0 :         if (!ranges)
     121                 :            :                 return 1;
     122         [ #  # ]:          0 :         if (rlen == 0) {
     123                 :            :                 offset = of_read_number(addr, na);
     124                 :          0 :                 memset(addr, 0, pna * 4);
     125                 :            :                 pr_debug("empty ranges, 1:1 translation\n");
     126                 :          0 :                 goto finish;
     127                 :            :         }
     128                 :            : 
     129                 :            :         pr_debug("walking ranges...\n");
     130                 :            : 
     131                 :            :         /* Now walk through the ranges */
     132                 :          0 :         rlen /= 4;
     133                 :          0 :         rone = na + pna + ns;
     134         [ #  # ]:          0 :         for (; rlen >= rone; rlen -= rone, ranges += rone) {
     135                 :          0 :                 offset = bus->map(addr, ranges, na, ns, pna);
     136         [ #  # ]:          0 :                 if (offset != OF_BAD_ADDR)
     137                 :            :                         break;
     138                 :            :         }
     139         [ #  # ]:          0 :         if (offset == OF_BAD_ADDR) {
     140                 :            :                 pr_debug("not found !\n");
     141                 :            :                 return 1;
     142                 :            :         }
     143                 :          0 :         memcpy(addr, ranges + na, 4 * pna);
     144                 :            : 
     145                 :            :  finish:
     146                 :            :         of_dump_addr("parent translation for:", addr, pna);
     147                 :            :         pr_debug("with offset: %llx\n", offset);
     148                 :            : 
     149                 :            :         /* Translate it into parent bus space */
     150                 :          0 :         return pbus->translate(addr, offset, pna);
     151                 :            : }
     152                 :            : 
     153                 :            : /*
     154                 :            :  * Translate an address from the device-tree into a CPU physical address,
     155                 :            :  * this walks up the tree and applies the various bus mappings on the
     156                 :            :  * way.
     157                 :            :  *
     158                 :            :  * Note: We consider that crossing any level with #size-cells == 0 to mean
     159                 :            :  * that translation is impossible (that is we are not dealing with a value
     160                 :            :  * that can be mapped to a cpu physical address). This is not really specified
     161                 :            :  * that way, but this is traditionally the way IBM at least do things
     162                 :            :  */
     163                 :          0 : static u64 __init fdt_translate_address(const void *blob, int node_offset)
     164                 :            : {
     165                 :            :         int parent, len;
     166                 :            :         const struct of_bus *bus, *pbus;
     167                 :            :         const __be32 *reg;
     168                 :            :         __be32 addr[OF_MAX_ADDR_CELLS];
     169                 :            :         int na, ns, pna, pns;
     170                 :            :         u64 result = OF_BAD_ADDR;
     171                 :            : 
     172                 :            :         pr_debug("** translation for device %s **\n",
     173                 :            :                  fdt_get_name(blob, node_offset, NULL));
     174                 :            : 
     175                 :          0 :         reg = fdt_getprop(blob, node_offset, "reg", &len);
     176         [ #  # ]:          0 :         if (!reg) {
     177                 :          0 :                 pr_err("warning: device tree node '%s' has no address.\n",
     178                 :            :                         fdt_get_name(blob, node_offset, NULL));
     179                 :          0 :                 goto bail;
     180                 :            :         }
     181                 :            : 
     182                 :            :         /* Get parent & match bus type */
     183                 :          0 :         parent = fdt_parent_offset(blob, node_offset);
     184         [ #  # ]:          0 :         if (parent < 0)
     185                 :            :                 goto bail;
     186                 :            :         bus = &of_busses[0];
     187                 :            : 
     188                 :            :         /* Cound address cells & copy address locally */
     189                 :          0 :         bus->count_cells(blob, parent, &na, &ns);
     190   [ #  #  #  # ]:          0 :         if (!OF_CHECK_COUNTS(na, ns)) {
     191                 :          0 :                 pr_err("Bad cell count for %s\n",
     192                 :            :                        fdt_get_name(blob, node_offset, NULL));
     193                 :          0 :                 goto bail;
     194                 :            :         }
     195                 :          0 :         memcpy(addr, reg, na * 4);
     196                 :            : 
     197                 :            :         pr_debug("bus (na=%d, ns=%d) on %s\n",
     198                 :            :                  na, ns, fdt_get_name(blob, parent, NULL));
     199                 :            :         of_dump_addr("translating address:", addr, na);
     200                 :            : 
     201                 :            :         /* Translate */
     202                 :            :         for (;;) {
     203                 :            :                 /* Switch to parent bus */
     204                 :            :                 node_offset = parent;
     205                 :          0 :                 parent = fdt_parent_offset(blob, node_offset);
     206                 :            : 
     207                 :            :                 /* If root, we have finished */
     208         [ #  # ]:          0 :                 if (parent < 0) {
     209                 :            :                         pr_debug("reached root node\n");
     210                 :          0 :                         result = of_read_number(addr, na);
     211                 :          0 :                         break;
     212                 :            :                 }
     213                 :            : 
     214                 :            :                 /* Get new parent bus and counts */
     215                 :            :                 pbus = &of_busses[0];
     216                 :          0 :                 pbus->count_cells(blob, parent, &pna, &pns);
     217   [ #  #  #  # ]:          0 :                 if (!OF_CHECK_COUNTS(pna, pns)) {
     218                 :          0 :                         pr_err("Bad cell count for %s\n",
     219                 :            :                                 fdt_get_name(blob, node_offset, NULL));
     220                 :          0 :                         break;
     221                 :            :                 }
     222                 :            : 
     223                 :            :                 pr_debug("parent bus (na=%d, ns=%d) on %s\n",
     224                 :            :                          pna, pns, fdt_get_name(blob, parent, NULL));
     225                 :            : 
     226                 :            :                 /* Apply bus translation */
     227         [ #  # ]:          0 :                 if (fdt_translate_one(blob, node_offset, bus, pbus,
     228                 :            :                                         addr, na, ns, pna, "ranges"))
     229                 :            :                         break;
     230                 :            : 
     231                 :            :                 /* Complete the move up one level */
     232                 :          0 :                 na = pna;
     233                 :          0 :                 ns = pns;
     234                 :            :                 bus = pbus;
     235                 :            : 
     236                 :            :                 of_dump_addr("one level translation:", addr, na);
     237                 :            :         }
     238                 :            :  bail:
     239                 :          0 :         return result;
     240                 :            : }
     241                 :            : 
     242                 :            : /**
     243                 :            :  * of_flat_dt_translate_address - translate DT addr into CPU phys addr
     244                 :            :  * @node: node in the flat blob
     245                 :            :  */
     246                 :          0 : u64 __init of_flat_dt_translate_address(unsigned long node)
     247                 :            : {
     248                 :          0 :         return fdt_translate_address(initial_boot_params, node);
     249                 :            : }

Generated by: LCOV version 1.14