LCOV - code coverage report
Current view: top level - drivers/of - base.c (source / functions) Hit Total Coverage
Test: Real Lines: 394 674 58.5 %
Date: 2020-10-17 15:46:16 Functions: 0 74 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0+
       2                 :            : /*
       3                 :            :  * Procedures for creating, accessing and interpreting the device tree.
       4                 :            :  *
       5                 :            :  * Paul Mackerras       August 1996.
       6                 :            :  * Copyright (C) 1996-2005 Paul Mackerras.
       7                 :            :  *
       8                 :            :  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
       9                 :            :  *    {engebret|bergner}@us.ibm.com
      10                 :            :  *
      11                 :            :  *  Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
      12                 :            :  *
      13                 :            :  *  Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
      14                 :            :  *  Grant Likely.
      15                 :            :  */
      16                 :            : 
      17                 :            : #define pr_fmt(fmt)     "OF: " fmt
      18                 :            : 
      19                 :            : #include <linux/bitmap.h>
      20                 :            : #include <linux/console.h>
      21                 :            : #include <linux/ctype.h>
      22                 :            : #include <linux/cpu.h>
      23                 :            : #include <linux/module.h>
      24                 :            : #include <linux/of.h>
      25                 :            : #include <linux/of_device.h>
      26                 :            : #include <linux/of_graph.h>
      27                 :            : #include <linux/spinlock.h>
      28                 :            : #include <linux/slab.h>
      29                 :            : #include <linux/string.h>
      30                 :            : #include <linux/proc_fs.h>
      31                 :            : 
      32                 :            : #include "of_private.h"
      33                 :            : 
      34                 :            : LIST_HEAD(aliases_lookup);
      35                 :            : 
      36                 :            : struct device_node *of_root;
      37                 :            : EXPORT_SYMBOL(of_root);
      38                 :            : struct device_node *of_chosen;
      39                 :            : struct device_node *of_aliases;
      40                 :            : struct device_node *of_stdout;
      41                 :            : static const char *of_stdout_options;
      42                 :            : 
      43                 :            : struct kset *of_kset;
      44                 :            : 
      45                 :            : /*
      46                 :            :  * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
      47                 :            :  * This mutex must be held whenever modifications are being made to the
      48                 :            :  * device tree. The of_{attach,detach}_node() and
      49                 :            :  * of_{add,remove,update}_property() helpers make sure this happens.
      50                 :            :  */
      51                 :            : DEFINE_MUTEX(of_mutex);
      52                 :            : 
      53                 :            : /* use when traversing tree through the child, sibling,
      54                 :            :  * or parent members of struct device_node.
      55                 :            :  */
      56                 :            : DEFINE_RAW_SPINLOCK(devtree_lock);
      57                 :            : 
      58                 :          3 : bool of_node_name_eq(const struct device_node *np, const char *name)
      59                 :            : {
      60                 :            :         const char *node_name;
      61                 :            :         size_t len;
      62                 :            : 
      63                 :          3 :         if (!np)
      64                 :            :                 return false;
      65                 :            : 
      66                 :          3 :         node_name = kbasename(np->full_name);
      67                 :          3 :         len = strchrnul(node_name, '@') - node_name;
      68                 :            : 
      69                 :          3 :         return (strlen(name) == len) && (strncmp(node_name, name, len) == 0);
      70                 :            : }
      71                 :            : EXPORT_SYMBOL(of_node_name_eq);
      72                 :            : 
      73                 :          0 : bool of_node_name_prefix(const struct device_node *np, const char *prefix)
      74                 :            : {
      75                 :          0 :         if (!np)
      76                 :            :                 return false;
      77                 :            : 
      78                 :          0 :         return strncmp(kbasename(np->full_name), prefix, strlen(prefix)) == 0;
      79                 :            : }
      80                 :            : EXPORT_SYMBOL(of_node_name_prefix);
      81                 :            : 
      82                 :          0 : static bool __of_node_is_type(const struct device_node *np, const char *type)
      83                 :            : {
      84                 :            :         const char *match = __of_get_property(np, "device_type", NULL);
      85                 :            : 
      86                 :          0 :         return np && match && type && !strcmp(match, type);
      87                 :            : }
      88                 :            : 
      89                 :          3 : int of_bus_n_addr_cells(struct device_node *np)
      90                 :            : {
      91                 :            :         u32 cells;
      92                 :            : 
      93                 :          3 :         for (; np; np = np->parent)
      94                 :          3 :                 if (!of_property_read_u32(np, "#address-cells", &cells))
      95                 :          3 :                         return cells;
      96                 :            : 
      97                 :            :         /* No #address-cells property for the root node */
      98                 :            :         return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
      99                 :            : }
     100                 :            : 
     101                 :          3 : int of_n_addr_cells(struct device_node *np)
     102                 :            : {
     103                 :          3 :         if (np->parent)
     104                 :            :                 np = np->parent;
     105                 :            : 
     106                 :          3 :         return of_bus_n_addr_cells(np);
     107                 :            : }
     108                 :            : EXPORT_SYMBOL(of_n_addr_cells);
     109                 :            : 
     110                 :          3 : int of_bus_n_size_cells(struct device_node *np)
     111                 :            : {
     112                 :            :         u32 cells;
     113                 :            : 
     114                 :          3 :         for (; np; np = np->parent)
     115                 :          3 :                 if (!of_property_read_u32(np, "#size-cells", &cells))
     116                 :          3 :                         return cells;
     117                 :            : 
     118                 :            :         /* No #size-cells property for the root node */
     119                 :            :         return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
     120                 :            : }
     121                 :            : 
     122                 :          3 : int of_n_size_cells(struct device_node *np)
     123                 :            : {
     124                 :          3 :         if (np->parent)
     125                 :            :                 np = np->parent;
     126                 :            : 
     127                 :          3 :         return of_bus_n_size_cells(np);
     128                 :            : }
     129                 :            : EXPORT_SYMBOL(of_n_size_cells);
     130                 :            : 
     131                 :            : #ifdef CONFIG_NUMA
     132                 :            : int __weak of_node_to_nid(struct device_node *np)
     133                 :            : {
     134                 :            :         return NUMA_NO_NODE;
     135                 :            : }
     136                 :            : #endif
     137                 :            : 
     138                 :            : /*
     139                 :            :  * Assumptions behind phandle_cache implementation:
     140                 :            :  *   - phandle property values are in a contiguous range of 1..n
     141                 :            :  *
     142                 :            :  * If the assumptions do not hold, then
     143                 :            :  *   - the phandle lookup overhead reduction provided by the cache
     144                 :            :  *     will likely be less
     145                 :            :  */
     146                 :            : 
     147                 :            : static struct device_node **phandle_cache;
     148                 :            : static u32 phandle_cache_mask;
     149                 :            : 
     150                 :            : /*
     151                 :            :  * Caller must hold devtree_lock.
     152                 :            :  */
     153                 :          3 : static void __of_free_phandle_cache(void)
     154                 :            : {
     155                 :          3 :         u32 cache_entries = phandle_cache_mask + 1;
     156                 :            :         u32 k;
     157                 :            : 
     158                 :          3 :         if (!phandle_cache)
     159                 :          3 :                 return;
     160                 :            : 
     161                 :          0 :         for (k = 0; k < cache_entries; k++)
     162                 :          0 :                 of_node_put(phandle_cache[k]);
     163                 :            : 
     164                 :          0 :         kfree(phandle_cache);
     165                 :          0 :         phandle_cache = NULL;
     166                 :            : }
     167                 :            : 
     168                 :          0 : int of_free_phandle_cache(void)
     169                 :            : {
     170                 :            :         unsigned long flags;
     171                 :            : 
     172                 :          0 :         raw_spin_lock_irqsave(&devtree_lock, flags);
     173                 :            : 
     174                 :          0 :         __of_free_phandle_cache();
     175                 :            : 
     176                 :          0 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
     177                 :            : 
     178                 :          0 :         return 0;
     179                 :            : }
     180                 :            : #if !defined(CONFIG_MODULES)
     181                 :            : late_initcall_sync(of_free_phandle_cache);
     182                 :            : #endif
     183                 :            : 
     184                 :            : /*
     185                 :            :  * Caller must hold devtree_lock.
     186                 :            :  */
     187                 :          0 : void __of_free_phandle_cache_entry(phandle handle)
     188                 :            : {
     189                 :            :         phandle masked_handle;
     190                 :            :         struct device_node *np;
     191                 :            : 
     192                 :          0 :         if (!handle)
     193                 :          0 :                 return;
     194                 :            : 
     195                 :          0 :         masked_handle = handle & phandle_cache_mask;
     196                 :            : 
     197                 :          0 :         if (phandle_cache) {
     198                 :          0 :                 np = phandle_cache[masked_handle];
     199                 :          0 :                 if (np && handle == np->phandle) {
     200                 :          0 :                         of_node_put(np);
     201                 :          0 :                         phandle_cache[masked_handle] = NULL;
     202                 :            :                 }
     203                 :            :         }
     204                 :            : }
     205                 :            : 
     206                 :          3 : void of_populate_phandle_cache(void)
     207                 :            : {
     208                 :            :         unsigned long flags;
     209                 :            :         u32 cache_entries;
     210                 :            :         struct device_node *np;
     211                 :            :         u32 phandles = 0;
     212                 :            : 
     213                 :          3 :         raw_spin_lock_irqsave(&devtree_lock, flags);
     214                 :            : 
     215                 :          3 :         __of_free_phandle_cache();
     216                 :            : 
     217                 :          3 :         for_each_of_allnodes(np)
     218                 :          3 :                 if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL)
     219                 :          3 :                         phandles++;
     220                 :            : 
     221                 :          3 :         if (!phandles)
     222                 :            :                 goto out;
     223                 :            : 
     224                 :          3 :         cache_entries = roundup_pow_of_two(phandles);
     225                 :          3 :         phandle_cache_mask = cache_entries - 1;
     226                 :            : 
     227                 :          3 :         phandle_cache = kcalloc(cache_entries, sizeof(*phandle_cache),
     228                 :            :                                 GFP_ATOMIC);
     229                 :          3 :         if (!phandle_cache)
     230                 :            :                 goto out;
     231                 :            : 
     232                 :          3 :         for_each_of_allnodes(np)
     233                 :          3 :                 if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL) {
     234                 :          3 :                         of_node_get(np);
     235                 :          3 :                         phandle_cache[np->phandle & phandle_cache_mask] = np;
     236                 :            :                 }
     237                 :            : 
     238                 :            : out:
     239                 :          3 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
     240                 :          3 : }
     241                 :            : 
     242                 :          3 : void __init of_core_init(void)
     243                 :            : {
     244                 :            :         struct device_node *np;
     245                 :            : 
     246                 :          3 :         of_populate_phandle_cache();
     247                 :            : 
     248                 :            :         /* Create the kset, and register existing nodes */
     249                 :          3 :         mutex_lock(&of_mutex);
     250                 :          3 :         of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
     251                 :          3 :         if (!of_kset) {
     252                 :          0 :                 mutex_unlock(&of_mutex);
     253                 :          0 :                 pr_err("failed to register existing nodes\n");
     254                 :          3 :                 return;
     255                 :            :         }
     256                 :          3 :         for_each_of_allnodes(np)
     257                 :          3 :                 __of_attach_node_sysfs(np);
     258                 :          3 :         mutex_unlock(&of_mutex);
     259                 :            : 
     260                 :            :         /* Symlink in /proc as required by userspace ABI */
     261                 :          3 :         if (of_root)
     262                 :          3 :                 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
     263                 :            : }
     264                 :            : 
     265                 :          3 : static struct property *__of_find_property(const struct device_node *np,
     266                 :            :                                            const char *name, int *lenp)
     267                 :            : {
     268                 :            :         struct property *pp;
     269                 :            : 
     270                 :          3 :         if (!np)
     271                 :            :                 return NULL;
     272                 :            : 
     273                 :          3 :         for (pp = np->properties; pp; pp = pp->next) {
     274                 :          3 :                 if (of_prop_cmp(pp->name, name) == 0) {
     275                 :          3 :                         if (lenp)
     276                 :          3 :                                 *lenp = pp->length;
     277                 :            :                         break;
     278                 :            :                 }
     279                 :            :         }
     280                 :            : 
     281                 :          3 :         return pp;
     282                 :            : }
     283                 :            : 
     284                 :          3 : struct property *of_find_property(const struct device_node *np,
     285                 :            :                                   const char *name,
     286                 :            :                                   int *lenp)
     287                 :            : {
     288                 :            :         struct property *pp;
     289                 :            :         unsigned long flags;
     290                 :            : 
     291                 :          3 :         raw_spin_lock_irqsave(&devtree_lock, flags);
     292                 :          3 :         pp = __of_find_property(np, name, lenp);
     293                 :          3 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
     294                 :            : 
     295                 :          3 :         return pp;
     296                 :            : }
     297                 :            : EXPORT_SYMBOL(of_find_property);
     298                 :            : 
     299                 :          0 : struct device_node *__of_find_all_nodes(struct device_node *prev)
     300                 :            : {
     301                 :            :         struct device_node *np;
     302                 :          3 :         if (!prev) {
     303                 :          3 :                 np = of_root;
     304                 :          3 :         } else if (prev->child) {
     305                 :            :                 np = prev->child;
     306                 :            :         } else {
     307                 :            :                 /* Walk back up looking for a sibling, or the end of the structure */
     308                 :            :                 np = prev;
     309                 :          3 :                 while (np->parent && !np->sibling)
     310                 :            :                         np = np->parent;
     311                 :          3 :                 np = np->sibling; /* Might be null at the end of the tree */
     312                 :            :         }
     313                 :          0 :         return np;
     314                 :            : }
     315                 :            : 
     316                 :            : /**
     317                 :            :  * of_find_all_nodes - Get next node in global list
     318                 :            :  * @prev:       Previous node or NULL to start iteration
     319                 :            :  *              of_node_put() will be called on it
     320                 :            :  *
     321                 :            :  * Returns a node pointer with refcount incremented, use
     322                 :            :  * of_node_put() on it when done.
     323                 :            :  */
     324                 :          0 : struct device_node *of_find_all_nodes(struct device_node *prev)
     325                 :            : {
     326                 :            :         struct device_node *np;
     327                 :            :         unsigned long flags;
     328                 :            : 
     329                 :          0 :         raw_spin_lock_irqsave(&devtree_lock, flags);
     330                 :            :         np = __of_find_all_nodes(prev);
     331                 :          0 :         of_node_get(np);
     332                 :          0 :         of_node_put(prev);
     333                 :          0 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
     334                 :          0 :         return np;
     335                 :            : }
     336                 :            : EXPORT_SYMBOL(of_find_all_nodes);
     337                 :            : 
     338                 :            : /*
     339                 :            :  * Find a property with a given name for a given node
     340                 :            :  * and return the value.
     341                 :            :  */
     342                 :          0 : const void *__of_get_property(const struct device_node *np,
     343                 :            :                               const char *name, int *lenp)
     344                 :            : {
     345                 :          3 :         struct property *pp = __of_find_property(np, name, lenp);
     346                 :            : 
     347                 :          3 :         return pp ? pp->value : NULL;
     348                 :            : }
     349                 :            : 
     350                 :            : /*
     351                 :            :  * Find a property with a given name for a given node
     352                 :            :  * and return the value.
     353                 :            :  */
     354                 :          3 : const void *of_get_property(const struct device_node *np, const char *name,
     355                 :            :                             int *lenp)
     356                 :            : {
     357                 :          3 :         struct property *pp = of_find_property(np, name, lenp);
     358                 :            : 
     359                 :          3 :         return pp ? pp->value : NULL;
     360                 :            : }
     361                 :            : EXPORT_SYMBOL(of_get_property);
     362                 :            : 
     363                 :            : /*
     364                 :            :  * arch_match_cpu_phys_id - Match the given logical CPU and physical id
     365                 :            :  *
     366                 :            :  * @cpu: logical cpu index of a core/thread
     367                 :            :  * @phys_id: physical identifier of a core/thread
     368                 :            :  *
     369                 :            :  * CPU logical to physical index mapping is architecture specific.
     370                 :            :  * However this __weak function provides a default match of physical
     371                 :            :  * id to logical cpu index. phys_id provided here is usually values read
     372                 :            :  * from the device tree which must match the hardware internal registers.
     373                 :            :  *
     374                 :            :  * Returns true if the physical identifier and the logical cpu index
     375                 :            :  * correspond to the same core/thread, false otherwise.
     376                 :            :  */
     377                 :          0 : bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
     378                 :            : {
     379                 :          0 :         return (u32)phys_id == cpu;
     380                 :            : }
     381                 :            : 
     382                 :            : /**
     383                 :            :  * Checks if the given "prop_name" property holds the physical id of the
     384                 :            :  * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
     385                 :            :  * NULL, local thread number within the core is returned in it.
     386                 :            :  */
     387                 :          3 : static bool __of_find_n_match_cpu_property(struct device_node *cpun,
     388                 :            :                         const char *prop_name, int cpu, unsigned int *thread)
     389                 :            : {
     390                 :            :         const __be32 *cell;
     391                 :            :         int ac, prop_len, tid;
     392                 :            :         u64 hwid;
     393                 :            : 
     394                 :            :         ac = of_n_addr_cells(cpun);
     395                 :            :         cell = of_get_property(cpun, prop_name, &prop_len);
     396                 :          3 :         if (!cell && !ac && arch_match_cpu_phys_id(cpu, 0))
     397                 :            :                 return true;
     398                 :          3 :         if (!cell || !ac)
     399                 :            :                 return false;
     400                 :          3 :         prop_len /= sizeof(*cell) * ac;
     401                 :          3 :         for (tid = 0; tid < prop_len; tid++) {
     402                 :            :                 hwid = of_read_number(cell, ac);
     403                 :          3 :                 if (arch_match_cpu_phys_id(cpu, hwid)) {
     404                 :          3 :                         if (thread)
     405                 :          0 :                                 *thread = tid;
     406                 :            :                         return true;
     407                 :            :                 }
     408                 :          3 :                 cell += ac;
     409                 :            :         }
     410                 :            :         return false;
     411                 :            : }
     412                 :            : 
     413                 :            : /*
     414                 :            :  * arch_find_n_match_cpu_physical_id - See if the given device node is
     415                 :            :  * for the cpu corresponding to logical cpu 'cpu'.  Return true if so,
     416                 :            :  * else false.  If 'thread' is non-NULL, the local thread number within the
     417                 :            :  * core is returned in it.
     418                 :            :  */
     419                 :          3 : bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
     420                 :            :                                               int cpu, unsigned int *thread)
     421                 :            : {
     422                 :            :         /* Check for non-standard "ibm,ppc-interrupt-server#s" property
     423                 :            :          * for thread ids on PowerPC. If it doesn't exist fallback to
     424                 :            :          * standard "reg" property.
     425                 :            :          */
     426                 :            :         if (IS_ENABLED(CONFIG_PPC) &&
     427                 :            :             __of_find_n_match_cpu_property(cpun,
     428                 :            :                                            "ibm,ppc-interrupt-server#s",
     429                 :            :                                            cpu, thread))
     430                 :            :                 return true;
     431                 :            : 
     432                 :          3 :         return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread);
     433                 :            : }
     434                 :            : 
     435                 :            : /**
     436                 :            :  * of_get_cpu_node - Get device node associated with the given logical CPU
     437                 :            :  *
     438                 :            :  * @cpu: CPU number(logical index) for which device node is required
     439                 :            :  * @thread: if not NULL, local thread number within the physical core is
     440                 :            :  *          returned
     441                 :            :  *
     442                 :            :  * The main purpose of this function is to retrieve the device node for the
     443                 :            :  * given logical CPU index. It should be used to initialize the of_node in
     444                 :            :  * cpu device. Once of_node in cpu device is populated, all the further
     445                 :            :  * references can use that instead.
     446                 :            :  *
     447                 :            :  * CPU logical to physical index mapping is architecture specific and is built
     448                 :            :  * before booting secondary cores. This function uses arch_match_cpu_phys_id
     449                 :            :  * which can be overridden by architecture specific implementation.
     450                 :            :  *
     451                 :            :  * Returns a node pointer for the logical cpu with refcount incremented, use
     452                 :            :  * of_node_put() on it when done. Returns NULL if not found.
     453                 :            :  */
     454                 :          3 : struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
     455                 :            : {
     456                 :            :         struct device_node *cpun;
     457                 :            : 
     458                 :          3 :         for_each_of_cpu_node(cpun) {
     459                 :          3 :                 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
     460                 :          3 :                         return cpun;
     461                 :            :         }
     462                 :            :         return NULL;
     463                 :            : }
     464                 :            : EXPORT_SYMBOL(of_get_cpu_node);
     465                 :            : 
     466                 :            : /**
     467                 :            :  * of_cpu_node_to_id: Get the logical CPU number for a given device_node
     468                 :            :  *
     469                 :            :  * @cpu_node: Pointer to the device_node for CPU.
     470                 :            :  *
     471                 :            :  * Returns the logical CPU number of the given CPU device_node.
     472                 :            :  * Returns -ENODEV if the CPU is not found.
     473                 :            :  */
     474                 :          0 : int of_cpu_node_to_id(struct device_node *cpu_node)
     475                 :            : {
     476                 :            :         int cpu;
     477                 :            :         bool found = false;
     478                 :            :         struct device_node *np;
     479                 :            : 
     480                 :          0 :         for_each_possible_cpu(cpu) {
     481                 :          0 :                 np = of_cpu_device_node_get(cpu);
     482                 :            :                 found = (cpu_node == np);
     483                 :          0 :                 of_node_put(np);
     484                 :          0 :                 if (found)
     485                 :          0 :                         return cpu;
     486                 :            :         }
     487                 :            : 
     488                 :            :         return -ENODEV;
     489                 :            : }
     490                 :            : EXPORT_SYMBOL(of_cpu_node_to_id);
     491                 :            : 
     492                 :            : /**
     493                 :            :  * __of_device_is_compatible() - Check if the node matches given constraints
     494                 :            :  * @device: pointer to node
     495                 :            :  * @compat: required compatible string, NULL or "" for any match
     496                 :            :  * @type: required device_type value, NULL or "" for any match
     497                 :            :  * @name: required node name, NULL or "" for any match
     498                 :            :  *
     499                 :            :  * Checks if the given @compat, @type and @name strings match the
     500                 :            :  * properties of the given @device. A constraints can be skipped by
     501                 :            :  * passing NULL or an empty string as the constraint.
     502                 :            :  *
     503                 :            :  * Returns 0 for no match, and a positive integer on match. The return
     504                 :            :  * value is a relative score with larger values indicating better
     505                 :            :  * matches. The score is weighted for the most specific compatible value
     506                 :            :  * to get the highest score. Matching type is next, followed by matching
     507                 :            :  * name. Practically speaking, this results in the following priority
     508                 :            :  * order for matches:
     509                 :            :  *
     510                 :            :  * 1. specific compatible && type && name
     511                 :            :  * 2. specific compatible && type
     512                 :            :  * 3. specific compatible && name
     513                 :            :  * 4. specific compatible
     514                 :            :  * 5. general compatible && type && name
     515                 :            :  * 6. general compatible && type
     516                 :            :  * 7. general compatible && name
     517                 :            :  * 8. general compatible
     518                 :            :  * 9. type && name
     519                 :            :  * 10. type
     520                 :            :  * 11. name
     521                 :            :  */
     522                 :          3 : static int __of_device_is_compatible(const struct device_node *device,
     523                 :            :                                      const char *compat, const char *type, const char *name)
     524                 :            : {
     525                 :            :         struct property *prop;
     526                 :            :         const char *cp;
     527                 :            :         int index = 0, score = 0;
     528                 :            : 
     529                 :            :         /* Compatible match has highest priority */
     530                 :          3 :         if (compat && compat[0]) {
     531                 :          3 :                 prop = __of_find_property(device, "compatible", NULL);
     532                 :          3 :                 for (cp = of_prop_next_string(prop, NULL); cp;
     533                 :          3 :                      cp = of_prop_next_string(prop, cp), index++) {
     534                 :          3 :                         if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
     535                 :          3 :                                 score = INT_MAX/2 - (index << 2);
     536                 :          3 :                                 break;
     537                 :            :                         }
     538                 :            :                 }
     539                 :          3 :                 if (!score)
     540                 :            :                         return 0;
     541                 :            :         }
     542                 :            : 
     543                 :            :         /* Matching type is better than matching name */
     544                 :          3 :         if (type && type[0]) {
     545                 :          0 :                 if (!__of_node_is_type(device, type))
     546                 :            :                         return 0;
     547                 :          0 :                 score += 2;
     548                 :            :         }
     549                 :            : 
     550                 :            :         /* Matching name is a bit better than not */
     551                 :          3 :         if (name && name[0]) {
     552                 :          0 :                 if (!of_node_name_eq(device, name))
     553                 :            :                         return 0;
     554                 :          0 :                 score++;
     555                 :            :         }
     556                 :            : 
     557                 :          3 :         return score;
     558                 :            : }
     559                 :            : 
     560                 :            : /** Checks if the given "compat" string matches one of the strings in
     561                 :            :  * the device's "compatible" property
     562                 :            :  */
     563                 :          3 : int of_device_is_compatible(const struct device_node *device,
     564                 :            :                 const char *compat)
     565                 :            : {
     566                 :            :         unsigned long flags;
     567                 :            :         int res;
     568                 :            : 
     569                 :          3 :         raw_spin_lock_irqsave(&devtree_lock, flags);
     570                 :          3 :         res = __of_device_is_compatible(device, compat, NULL, NULL);
     571                 :          3 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
     572                 :          3 :         return res;
     573                 :            : }
     574                 :            : EXPORT_SYMBOL(of_device_is_compatible);
     575                 :            : 
     576                 :            : /** Checks if the device is compatible with any of the entries in
     577                 :            :  *  a NULL terminated array of strings. Returns the best match
     578                 :            :  *  score or 0.
     579                 :            :  */
     580                 :          0 : int of_device_compatible_match(struct device_node *device,
     581                 :            :                                const char *const *compat)
     582                 :            : {
     583                 :            :         unsigned int tmp, score = 0;
     584                 :            : 
     585                 :          0 :         if (!compat)
     586                 :            :                 return 0;
     587                 :            : 
     588                 :          0 :         while (*compat) {
     589                 :          0 :                 tmp = of_device_is_compatible(device, *compat);
     590                 :          0 :                 if (tmp > score)
     591                 :            :                         score = tmp;
     592                 :          0 :                 compat++;
     593                 :            :         }
     594                 :            : 
     595                 :          0 :         return score;
     596                 :            : }
     597                 :            : 
     598                 :            : /**
     599                 :            :  * of_machine_is_compatible - Test root of device tree for a given compatible value
     600                 :            :  * @compat: compatible string to look for in root node's compatible property.
     601                 :            :  *
     602                 :            :  * Returns a positive integer if the root node has the given value in its
     603                 :            :  * compatible property.
     604                 :            :  */
     605                 :          0 : int of_machine_is_compatible(const char *compat)
     606                 :            : {
     607                 :            :         struct device_node *root;
     608                 :            :         int rc = 0;
     609                 :            : 
     610                 :            :         root = of_find_node_by_path("/");
     611                 :          0 :         if (root) {
     612                 :          0 :                 rc = of_device_is_compatible(root, compat);
     613                 :          0 :                 of_node_put(root);
     614                 :            :         }
     615                 :          0 :         return rc;
     616                 :            : }
     617                 :            : EXPORT_SYMBOL(of_machine_is_compatible);
     618                 :            : 
     619                 :            : /**
     620                 :            :  *  __of_device_is_available - check if a device is available for use
     621                 :            :  *
     622                 :            :  *  @device: Node to check for availability, with locks already held
     623                 :            :  *
     624                 :            :  *  Returns true if the status property is absent or set to "okay" or "ok",
     625                 :            :  *  false otherwise
     626                 :            :  */
     627                 :          3 : static bool __of_device_is_available(const struct device_node *device)
     628                 :            : {
     629                 :            :         const char *status;
     630                 :            :         int statlen;
     631                 :            : 
     632                 :          3 :         if (!device)
     633                 :            :                 return false;
     634                 :            : 
     635                 :            :         status = __of_get_property(device, "status", &statlen);
     636                 :          3 :         if (status == NULL)
     637                 :            :                 return true;
     638                 :            : 
     639                 :          3 :         if (statlen > 0) {
     640                 :          3 :                 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
     641                 :            :                         return true;
     642                 :            :         }
     643                 :            : 
     644                 :          3 :         return false;
     645                 :            : }
     646                 :            : 
     647                 :            : /**
     648                 :            :  *  of_device_is_available - check if a device is available for use
     649                 :            :  *
     650                 :            :  *  @device: Node to check for availability
     651                 :            :  *
     652                 :            :  *  Returns true if the status property is absent or set to "okay" or "ok",
     653                 :            :  *  false otherwise
     654                 :            :  */
     655                 :          3 : bool of_device_is_available(const struct device_node *device)
     656                 :            : {
     657                 :            :         unsigned long flags;
     658                 :            :         bool res;
     659                 :            : 
     660                 :          3 :         raw_spin_lock_irqsave(&devtree_lock, flags);
     661                 :          3 :         res = __of_device_is_available(device);
     662                 :          3 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
     663                 :          3 :         return res;
     664                 :            : 
     665                 :            : }
     666                 :            : EXPORT_SYMBOL(of_device_is_available);
     667                 :            : 
     668                 :            : /**
     669                 :            :  *  of_device_is_big_endian - check if a device has BE registers
     670                 :            :  *
     671                 :            :  *  @device: Node to check for endianness
     672                 :            :  *
     673                 :            :  *  Returns true if the device has a "big-endian" property, or if the kernel
     674                 :            :  *  was compiled for BE *and* the device has a "native-endian" property.
     675                 :            :  *  Returns false otherwise.
     676                 :            :  *
     677                 :            :  *  Callers would nominally use ioread32be/iowrite32be if
     678                 :            :  *  of_device_is_big_endian() == true, or readl/writel otherwise.
     679                 :            :  */
     680                 :          0 : bool of_device_is_big_endian(const struct device_node *device)
     681                 :            : {
     682                 :          0 :         if (of_property_read_bool(device, "big-endian"))
     683                 :            :                 return true;
     684                 :            :         if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
     685                 :            :             of_property_read_bool(device, "native-endian"))
     686                 :            :                 return true;
     687                 :          0 :         return false;
     688                 :            : }
     689                 :            : EXPORT_SYMBOL(of_device_is_big_endian);
     690                 :            : 
     691                 :            : /**
     692                 :            :  *      of_get_parent - Get a node's parent if any
     693                 :            :  *      @node:  Node to get parent
     694                 :            :  *
     695                 :            :  *      Returns a node pointer with refcount incremented, use
     696                 :            :  *      of_node_put() on it when done.
     697                 :            :  */
     698                 :          3 : struct device_node *of_get_parent(const struct device_node *node)
     699                 :            : {
     700                 :            :         struct device_node *np;
     701                 :            :         unsigned long flags;
     702                 :            : 
     703                 :          3 :         if (!node)
     704                 :            :                 return NULL;
     705                 :            : 
     706                 :          3 :         raw_spin_lock_irqsave(&devtree_lock, flags);
     707                 :          3 :         np = of_node_get(node->parent);
     708                 :          3 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
     709                 :          3 :         return np;
     710                 :            : }
     711                 :            : EXPORT_SYMBOL(of_get_parent);
     712                 :            : 
     713                 :            : /**
     714                 :            :  *      of_get_next_parent - Iterate to a node's parent
     715                 :            :  *      @node:  Node to get parent of
     716                 :            :  *
     717                 :            :  *      This is like of_get_parent() except that it drops the
     718                 :            :  *      refcount on the passed node, making it suitable for iterating
     719                 :            :  *      through a node's parents.
     720                 :            :  *
     721                 :            :  *      Returns a node pointer with refcount incremented, use
     722                 :            :  *      of_node_put() on it when done.
     723                 :            :  */
     724                 :          3 : struct device_node *of_get_next_parent(struct device_node *node)
     725                 :            : {
     726                 :            :         struct device_node *parent;
     727                 :            :         unsigned long flags;
     728                 :            : 
     729                 :          3 :         if (!node)
     730                 :            :                 return NULL;
     731                 :            : 
     732                 :          3 :         raw_spin_lock_irqsave(&devtree_lock, flags);
     733                 :          3 :         parent = of_node_get(node->parent);
     734                 :          3 :         of_node_put(node);
     735                 :          3 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
     736                 :          3 :         return parent;
     737                 :            : }
     738                 :            : EXPORT_SYMBOL(of_get_next_parent);
     739                 :            : 
     740                 :          3 : static struct device_node *__of_get_next_child(const struct device_node *node,
     741                 :            :                                                 struct device_node *prev)
     742                 :            : {
     743                 :            :         struct device_node *next;
     744                 :            : 
     745                 :          3 :         if (!node)
     746                 :            :                 return NULL;
     747                 :            : 
     748                 :          3 :         next = prev ? prev->sibling : node->child;
     749                 :          3 :         for (; next; next = next->sibling)
     750                 :          3 :                 if (of_node_get(next))
     751                 :            :                         break;
     752                 :          3 :         of_node_put(prev);
     753                 :          3 :         return next;
     754                 :            : }
     755                 :            : #define __for_each_child_of_node(parent, child) \
     756                 :            :         for (child = __of_get_next_child(parent, NULL); child != NULL; \
     757                 :            :              child = __of_get_next_child(parent, child))
     758                 :            : 
     759                 :            : /**
     760                 :            :  *      of_get_next_child - Iterate a node childs
     761                 :            :  *      @node:  parent node
     762                 :            :  *      @prev:  previous child of the parent node, or NULL to get first
     763                 :            :  *
     764                 :            :  *      Returns a node pointer with refcount incremented, use of_node_put() on
     765                 :            :  *      it when done. Returns NULL when prev is the last child. Decrements the
     766                 :            :  *      refcount of prev.
     767                 :            :  */
     768                 :          3 : struct device_node *of_get_next_child(const struct device_node *node,
     769                 :            :         struct device_node *prev)
     770                 :            : {
     771                 :            :         struct device_node *next;
     772                 :            :         unsigned long flags;
     773                 :            : 
     774                 :          3 :         raw_spin_lock_irqsave(&devtree_lock, flags);
     775                 :          3 :         next = __of_get_next_child(node, prev);
     776                 :          3 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
     777                 :          3 :         return next;
     778                 :            : }
     779                 :            : EXPORT_SYMBOL(of_get_next_child);
     780                 :            : 
     781                 :            : /**
     782                 :            :  *      of_get_next_available_child - Find the next available child node
     783                 :            :  *      @node:  parent node
     784                 :            :  *      @prev:  previous child of the parent node, or NULL to get first
     785                 :            :  *
     786                 :            :  *      This function is like of_get_next_child(), except that it
     787                 :            :  *      automatically skips any disabled nodes (i.e. status = "disabled").
     788                 :            :  */
     789                 :          3 : struct device_node *of_get_next_available_child(const struct device_node *node,
     790                 :            :         struct device_node *prev)
     791                 :            : {
     792                 :            :         struct device_node *next;
     793                 :            :         unsigned long flags;
     794                 :            : 
     795                 :          3 :         if (!node)
     796                 :            :                 return NULL;
     797                 :            : 
     798                 :          3 :         raw_spin_lock_irqsave(&devtree_lock, flags);
     799                 :          3 :         next = prev ? prev->sibling : node->child;
     800                 :          3 :         for (; next; next = next->sibling) {
     801                 :          3 :                 if (!__of_device_is_available(next))
     802                 :          0 :                         continue;
     803                 :          3 :                 if (of_node_get(next))
     804                 :            :                         break;
     805                 :            :         }
     806                 :          3 :         of_node_put(prev);
     807                 :          3 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
     808                 :          3 :         return next;
     809                 :            : }
     810                 :            : EXPORT_SYMBOL(of_get_next_available_child);
     811                 :            : 
     812                 :            : /**
     813                 :            :  *      of_get_next_cpu_node - Iterate on cpu nodes
     814                 :            :  *      @prev:  previous child of the /cpus node, or NULL to get first
     815                 :            :  *
     816                 :            :  *      Returns a cpu node pointer with refcount incremented, use of_node_put()
     817                 :            :  *      on it when done. Returns NULL when prev is the last child. Decrements
     818                 :            :  *      the refcount of prev.
     819                 :            :  */
     820                 :          3 : struct device_node *of_get_next_cpu_node(struct device_node *prev)
     821                 :            : {
     822                 :            :         struct device_node *next = NULL;
     823                 :            :         unsigned long flags;
     824                 :            :         struct device_node *node;
     825                 :            : 
     826                 :          3 :         if (!prev)
     827                 :            :                 node = of_find_node_by_path("/cpus");
     828                 :            : 
     829                 :          3 :         raw_spin_lock_irqsave(&devtree_lock, flags);
     830                 :          3 :         if (prev)
     831                 :          3 :                 next = prev->sibling;
     832                 :          3 :         else if (node) {
     833                 :          3 :                 next = node->child;
     834                 :          3 :                 of_node_put(node);
     835                 :            :         }
     836                 :          0 :         for (; next; next = next->sibling) {
     837                 :          3 :                 if (!(of_node_name_eq(next, "cpu") ||
     838                 :          0 :                       __of_node_is_type(next, "cpu")))
     839                 :          0 :                         continue;
     840                 :          3 :                 if (of_node_get(next))
     841                 :            :                         break;
     842                 :            :         }
     843                 :          3 :         of_node_put(prev);
     844                 :          3 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
     845                 :          3 :         return next;
     846                 :            : }
     847                 :            : EXPORT_SYMBOL(of_get_next_cpu_node);
     848                 :            : 
     849                 :            : /**
     850                 :            :  * of_get_compatible_child - Find compatible child node
     851                 :            :  * @parent:     parent node
     852                 :            :  * @compatible: compatible string
     853                 :            :  *
     854                 :            :  * Lookup child node whose compatible property contains the given compatible
     855                 :            :  * string.
     856                 :            :  *
     857                 :            :  * Returns a node pointer with refcount incremented, use of_node_put() on it
     858                 :            :  * when done; or NULL if not found.
     859                 :            :  */
     860                 :          0 : struct device_node *of_get_compatible_child(const struct device_node *parent,
     861                 :            :                                 const char *compatible)
     862                 :            : {
     863                 :            :         struct device_node *child;
     864                 :            : 
     865                 :          0 :         for_each_child_of_node(parent, child) {
     866                 :          0 :                 if (of_device_is_compatible(child, compatible))
     867                 :            :                         break;
     868                 :            :         }
     869                 :            : 
     870                 :          0 :         return child;
     871                 :            : }
     872                 :            : EXPORT_SYMBOL(of_get_compatible_child);
     873                 :            : 
     874                 :            : /**
     875                 :            :  *      of_get_child_by_name - Find the child node by name for a given parent
     876                 :            :  *      @node:  parent node
     877                 :            :  *      @name:  child name to look for.
     878                 :            :  *
     879                 :            :  *      This function looks for child node for given matching name
     880                 :            :  *
     881                 :            :  *      Returns a node pointer if found, with refcount incremented, use
     882                 :            :  *      of_node_put() on it when done.
     883                 :            :  *      Returns NULL if node is not found.
     884                 :            :  */
     885                 :          3 : struct device_node *of_get_child_by_name(const struct device_node *node,
     886                 :            :                                 const char *name)
     887                 :            : {
     888                 :            :         struct device_node *child;
     889                 :            : 
     890                 :          3 :         for_each_child_of_node(node, child)
     891                 :          3 :                 if (of_node_name_eq(child, name))
     892                 :            :                         break;
     893                 :          3 :         return child;
     894                 :            : }
     895                 :            : EXPORT_SYMBOL(of_get_child_by_name);
     896                 :            : 
     897                 :          3 : struct device_node *__of_find_node_by_path(struct device_node *parent,
     898                 :            :                                                 const char *path)
     899                 :            : {
     900                 :            :         struct device_node *child;
     901                 :            :         int len;
     902                 :            : 
     903                 :          3 :         len = strcspn(path, "/:");
     904                 :          3 :         if (!len)
     905                 :            :                 return NULL;
     906                 :            : 
     907                 :          3 :         __for_each_child_of_node(parent, child) {
     908                 :          3 :                 const char *name = kbasename(child->full_name);
     909                 :          3 :                 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
     910                 :          3 :                         return child;
     911                 :            :         }
     912                 :            :         return NULL;
     913                 :            : }
     914                 :            : 
     915                 :          3 : struct device_node *__of_find_node_by_full_path(struct device_node *node,
     916                 :            :                                                 const char *path)
     917                 :            : {
     918                 :          3 :         const char *separator = strchr(path, ':');
     919                 :            : 
     920                 :          3 :         while (node && *path == '/') {
     921                 :            :                 struct device_node *tmp = node;
     922                 :            : 
     923                 :          3 :                 path++; /* Increment past '/' delimiter */
     924                 :          3 :                 node = __of_find_node_by_path(node, path);
     925                 :          3 :                 of_node_put(tmp);
     926                 :          3 :                 path = strchrnul(path, '/');
     927                 :          3 :                 if (separator && separator < path)
     928                 :            :                         break;
     929                 :            :         }
     930                 :          3 :         return node;
     931                 :            : }
     932                 :            : 
     933                 :            : /**
     934                 :            :  *      of_find_node_opts_by_path - Find a node matching a full OF path
     935                 :            :  *      @path: Either the full path to match, or if the path does not
     936                 :            :  *             start with '/', the name of a property of the /aliases
     937                 :            :  *             node (an alias).  In the case of an alias, the node
     938                 :            :  *             matching the alias' value will be returned.
     939                 :            :  *      @opts: Address of a pointer into which to store the start of
     940                 :            :  *             an options string appended to the end of the path with
     941                 :            :  *             a ':' separator.
     942                 :            :  *
     943                 :            :  *      Valid paths:
     944                 :            :  *              /foo/bar        Full path
     945                 :            :  *              foo             Valid alias
     946                 :            :  *              foo/bar         Valid alias + relative path
     947                 :            :  *
     948                 :            :  *      Returns a node pointer with refcount incremented, use
     949                 :            :  *      of_node_put() on it when done.
     950                 :            :  */
     951                 :          3 : struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
     952                 :            : {
     953                 :            :         struct device_node *np = NULL;
     954                 :            :         struct property *pp;
     955                 :            :         unsigned long flags;
     956                 :          3 :         const char *separator = strchr(path, ':');
     957                 :            : 
     958                 :          3 :         if (opts)
     959                 :          0 :                 *opts = separator ? separator + 1 : NULL;
     960                 :            : 
     961                 :          3 :         if (strcmp(path, "/") == 0)
     962                 :          3 :                 return of_node_get(of_root);
     963                 :            : 
     964                 :            :         /* The path could begin with an alias */
     965                 :          3 :         if (*path != '/') {
     966                 :            :                 int len;
     967                 :            :                 const char *p = separator;
     968                 :            : 
     969                 :          0 :                 if (!p)
     970                 :          0 :                         p = strchrnul(path, '/');
     971                 :          0 :                 len = p - path;
     972                 :            : 
     973                 :            :                 /* of_aliases must not be NULL */
     974                 :          0 :                 if (!of_aliases)
     975                 :            :                         return NULL;
     976                 :            : 
     977                 :          0 :                 for_each_property_of_node(of_aliases, pp) {
     978                 :          0 :                         if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
     979                 :          0 :                                 np = of_find_node_by_path(pp->value);
     980                 :          0 :                                 break;
     981                 :            :                         }
     982                 :            :                 }
     983                 :          0 :                 if (!np)
     984                 :            :                         return NULL;
     985                 :            :                 path = p;
     986                 :            :         }
     987                 :            : 
     988                 :            :         /* Step down the tree matching path components */
     989                 :          3 :         raw_spin_lock_irqsave(&devtree_lock, flags);
     990                 :          3 :         if (!np)
     991                 :          3 :                 np = of_node_get(of_root);
     992                 :          3 :         np = __of_find_node_by_full_path(np, path);
     993                 :          3 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
     994                 :          3 :         return np;
     995                 :            : }
     996                 :            : EXPORT_SYMBOL(of_find_node_opts_by_path);
     997                 :            : 
     998                 :            : /**
     999                 :            :  *      of_find_node_by_name - Find a node by its "name" property
    1000                 :            :  *      @from:  The node to start searching from or NULL; the node
    1001                 :            :  *              you pass will not be searched, only the next one
    1002                 :            :  *              will. Typically, you pass what the previous call
    1003                 :            :  *              returned. of_node_put() will be called on @from.
    1004                 :            :  *      @name:  The name string to match against
    1005                 :            :  *
    1006                 :            :  *      Returns a node pointer with refcount incremented, use
    1007                 :            :  *      of_node_put() on it when done.
    1008                 :            :  */
    1009                 :          3 : struct device_node *of_find_node_by_name(struct device_node *from,
    1010                 :            :         const char *name)
    1011                 :            : {
    1012                 :            :         struct device_node *np;
    1013                 :            :         unsigned long flags;
    1014                 :            : 
    1015                 :          3 :         raw_spin_lock_irqsave(&devtree_lock, flags);
    1016                 :          3 :         for_each_of_allnodes_from(from, np)
    1017                 :          3 :                 if (of_node_name_eq(np, name) && of_node_get(np))
    1018                 :            :                         break;
    1019                 :          3 :         of_node_put(from);
    1020                 :          3 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
    1021                 :          3 :         return np;
    1022                 :            : }
    1023                 :            : EXPORT_SYMBOL(of_find_node_by_name);
    1024                 :            : 
    1025                 :            : /**
    1026                 :            :  *      of_find_node_by_type - Find a node by its "device_type" property
    1027                 :            :  *      @from:  The node to start searching from, or NULL to start searching
    1028                 :            :  *              the entire device tree. The node you pass will not be
    1029                 :            :  *              searched, only the next one will; typically, you pass
    1030                 :            :  *              what the previous call returned. of_node_put() will be
    1031                 :            :  *              called on from for you.
    1032                 :            :  *      @type:  The type string to match against
    1033                 :            :  *
    1034                 :            :  *      Returns a node pointer with refcount incremented, use
    1035                 :            :  *      of_node_put() on it when done.
    1036                 :            :  */
    1037                 :          0 : struct device_node *of_find_node_by_type(struct device_node *from,
    1038                 :            :         const char *type)
    1039                 :            : {
    1040                 :            :         struct device_node *np;
    1041                 :            :         unsigned long flags;
    1042                 :            : 
    1043                 :          0 :         raw_spin_lock_irqsave(&devtree_lock, flags);
    1044                 :          0 :         for_each_of_allnodes_from(from, np)
    1045                 :          0 :                 if (__of_node_is_type(np, type) && of_node_get(np))
    1046                 :            :                         break;
    1047                 :          0 :         of_node_put(from);
    1048                 :          0 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
    1049                 :          0 :         return np;
    1050                 :            : }
    1051                 :            : EXPORT_SYMBOL(of_find_node_by_type);
    1052                 :            : 
    1053                 :            : /**
    1054                 :            :  *      of_find_compatible_node - Find a node based on type and one of the
    1055                 :            :  *                                tokens in its "compatible" property
    1056                 :            :  *      @from:          The node to start searching from or NULL, the node
    1057                 :            :  *                      you pass will not be searched, only the next one
    1058                 :            :  *                      will; typically, you pass what the previous call
    1059                 :            :  *                      returned. of_node_put() will be called on it
    1060                 :            :  *      @type:          The type string to match "device_type" or NULL to ignore
    1061                 :            :  *      @compatible:    The string to match to one of the tokens in the device
    1062                 :            :  *                      "compatible" list.
    1063                 :            :  *
    1064                 :            :  *      Returns a node pointer with refcount incremented, use
    1065                 :            :  *      of_node_put() on it when done.
    1066                 :            :  */
    1067                 :          3 : struct device_node *of_find_compatible_node(struct device_node *from,
    1068                 :            :         const char *type, const char *compatible)
    1069                 :            : {
    1070                 :            :         struct device_node *np;
    1071                 :            :         unsigned long flags;
    1072                 :            : 
    1073                 :          3 :         raw_spin_lock_irqsave(&devtree_lock, flags);
    1074                 :          3 :         for_each_of_allnodes_from(from, np)
    1075                 :          3 :                 if (__of_device_is_compatible(np, compatible, type, NULL) &&
    1076                 :          3 :                     of_node_get(np))
    1077                 :            :                         break;
    1078                 :          3 :         of_node_put(from);
    1079                 :          3 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
    1080                 :          3 :         return np;
    1081                 :            : }
    1082                 :            : EXPORT_SYMBOL(of_find_compatible_node);
    1083                 :            : 
    1084                 :            : /**
    1085                 :            :  *      of_find_node_with_property - Find a node which has a property with
    1086                 :            :  *                                   the given name.
    1087                 :            :  *      @from:          The node to start searching from or NULL, the node
    1088                 :            :  *                      you pass will not be searched, only the next one
    1089                 :            :  *                      will; typically, you pass what the previous call
    1090                 :            :  *                      returned. of_node_put() will be called on it
    1091                 :            :  *      @prop_name:     The name of the property to look for.
    1092                 :            :  *
    1093                 :            :  *      Returns a node pointer with refcount incremented, use
    1094                 :            :  *      of_node_put() on it when done.
    1095                 :            :  */
    1096                 :          0 : struct device_node *of_find_node_with_property(struct device_node *from,
    1097                 :            :         const char *prop_name)
    1098                 :            : {
    1099                 :            :         struct device_node *np;
    1100                 :            :         struct property *pp;
    1101                 :            :         unsigned long flags;
    1102                 :            : 
    1103                 :          0 :         raw_spin_lock_irqsave(&devtree_lock, flags);
    1104                 :          0 :         for_each_of_allnodes_from(from, np) {
    1105                 :          0 :                 for (pp = np->properties; pp; pp = pp->next) {
    1106                 :          0 :                         if (of_prop_cmp(pp->name, prop_name) == 0) {
    1107                 :          0 :                                 of_node_get(np);
    1108                 :          0 :                                 goto out;
    1109                 :            :                         }
    1110                 :            :                 }
    1111                 :            :         }
    1112                 :            : out:
    1113                 :          0 :         of_node_put(from);
    1114                 :          0 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
    1115                 :          0 :         return np;
    1116                 :            : }
    1117                 :            : EXPORT_SYMBOL(of_find_node_with_property);
    1118                 :            : 
    1119                 :            : static
    1120                 :          3 : const struct of_device_id *__of_match_node(const struct of_device_id *matches,
    1121                 :            :                                            const struct device_node *node)
    1122                 :            : {
    1123                 :            :         const struct of_device_id *best_match = NULL;
    1124                 :            :         int score, best_score = 0;
    1125                 :            : 
    1126                 :          3 :         if (!matches)
    1127                 :            :                 return NULL;
    1128                 :            : 
    1129                 :          3 :         for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
    1130                 :          3 :                 score = __of_device_is_compatible(node, matches->compatible,
    1131                 :          3 :                                                   matches->type, matches->name);
    1132                 :          3 :                 if (score > best_score) {
    1133                 :            :                         best_match = matches;
    1134                 :            :                         best_score = score;
    1135                 :            :                 }
    1136                 :            :         }
    1137                 :            : 
    1138                 :          3 :         return best_match;
    1139                 :            : }
    1140                 :            : 
    1141                 :            : /**
    1142                 :            :  * of_match_node - Tell if a device_node has a matching of_match structure
    1143                 :            :  *      @matches:       array of of device match structures to search in
    1144                 :            :  *      @node:          the of device structure to match against
    1145                 :            :  *
    1146                 :            :  *      Low level utility function used by device matching.
    1147                 :            :  */
    1148                 :          3 : const struct of_device_id *of_match_node(const struct of_device_id *matches,
    1149                 :            :                                          const struct device_node *node)
    1150                 :            : {
    1151                 :            :         const struct of_device_id *match;
    1152                 :            :         unsigned long flags;
    1153                 :            : 
    1154                 :          3 :         raw_spin_lock_irqsave(&devtree_lock, flags);
    1155                 :          3 :         match = __of_match_node(matches, node);
    1156                 :          3 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
    1157                 :          3 :         return match;
    1158                 :            : }
    1159                 :            : EXPORT_SYMBOL(of_match_node);
    1160                 :            : 
    1161                 :            : /**
    1162                 :            :  *      of_find_matching_node_and_match - Find a node based on an of_device_id
    1163                 :            :  *                                        match table.
    1164                 :            :  *      @from:          The node to start searching from or NULL, the node
    1165                 :            :  *                      you pass will not be searched, only the next one
    1166                 :            :  *                      will; typically, you pass what the previous call
    1167                 :            :  *                      returned. of_node_put() will be called on it
    1168                 :            :  *      @matches:       array of of device match structures to search in
    1169                 :            :  *      @match          Updated to point at the matches entry which matched
    1170                 :            :  *
    1171                 :            :  *      Returns a node pointer with refcount incremented, use
    1172                 :            :  *      of_node_put() on it when done.
    1173                 :            :  */
    1174                 :          3 : struct device_node *of_find_matching_node_and_match(struct device_node *from,
    1175                 :            :                                         const struct of_device_id *matches,
    1176                 :            :                                         const struct of_device_id **match)
    1177                 :            : {
    1178                 :            :         struct device_node *np;
    1179                 :            :         const struct of_device_id *m;
    1180                 :            :         unsigned long flags;
    1181                 :            : 
    1182                 :          3 :         if (match)
    1183                 :          3 :                 *match = NULL;
    1184                 :            : 
    1185                 :          3 :         raw_spin_lock_irqsave(&devtree_lock, flags);
    1186                 :          3 :         for_each_of_allnodes_from(from, np) {
    1187                 :          3 :                 m = __of_match_node(matches, np);
    1188                 :          3 :                 if (m && of_node_get(np)) {
    1189                 :          3 :                         if (match)
    1190                 :          3 :                                 *match = m;
    1191                 :            :                         break;
    1192                 :            :                 }
    1193                 :            :         }
    1194                 :          3 :         of_node_put(from);
    1195                 :          3 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
    1196                 :          3 :         return np;
    1197                 :            : }
    1198                 :            : EXPORT_SYMBOL(of_find_matching_node_and_match);
    1199                 :            : 
    1200                 :            : /**
    1201                 :            :  * of_modalias_node - Lookup appropriate modalias for a device node
    1202                 :            :  * @node:       pointer to a device tree node
    1203                 :            :  * @modalias:   Pointer to buffer that modalias value will be copied into
    1204                 :            :  * @len:        Length of modalias value
    1205                 :            :  *
    1206                 :            :  * Based on the value of the compatible property, this routine will attempt
    1207                 :            :  * to choose an appropriate modalias value for a particular device tree node.
    1208                 :            :  * It does this by stripping the manufacturer prefix (as delimited by a ',')
    1209                 :            :  * from the first entry in the compatible list property.
    1210                 :            :  *
    1211                 :            :  * This routine returns 0 on success, <0 on failure.
    1212                 :            :  */
    1213                 :          0 : int of_modalias_node(struct device_node *node, char *modalias, int len)
    1214                 :            : {
    1215                 :            :         const char *compatible, *p;
    1216                 :            :         int cplen;
    1217                 :            : 
    1218                 :            :         compatible = of_get_property(node, "compatible", &cplen);
    1219                 :          0 :         if (!compatible || strlen(compatible) > cplen)
    1220                 :            :                 return -ENODEV;
    1221                 :          0 :         p = strchr(compatible, ',');
    1222                 :          0 :         strlcpy(modalias, p ? p + 1 : compatible, len);
    1223                 :          0 :         return 0;
    1224                 :            : }
    1225                 :            : EXPORT_SYMBOL_GPL(of_modalias_node);
    1226                 :            : 
    1227                 :            : /**
    1228                 :            :  * of_find_node_by_phandle - Find a node given a phandle
    1229                 :            :  * @handle:     phandle of the node to find
    1230                 :            :  *
    1231                 :            :  * Returns a node pointer with refcount incremented, use
    1232                 :            :  * of_node_put() on it when done.
    1233                 :            :  */
    1234                 :          3 : struct device_node *of_find_node_by_phandle(phandle handle)
    1235                 :            : {
    1236                 :            :         struct device_node *np = NULL;
    1237                 :            :         unsigned long flags;
    1238                 :            :         phandle masked_handle;
    1239                 :            : 
    1240                 :          3 :         if (!handle)
    1241                 :            :                 return NULL;
    1242                 :            : 
    1243                 :          3 :         raw_spin_lock_irqsave(&devtree_lock, flags);
    1244                 :            : 
    1245                 :          3 :         masked_handle = handle & phandle_cache_mask;
    1246                 :            : 
    1247                 :          3 :         if (phandle_cache) {
    1248                 :          3 :                 if (phandle_cache[masked_handle] &&
    1249                 :          3 :                     handle == phandle_cache[masked_handle]->phandle)
    1250                 :            :                         np = phandle_cache[masked_handle];
    1251                 :          3 :                 if (np && of_node_check_flag(np, OF_DETACHED)) {
    1252                 :          0 :                         WARN_ON(1); /* did not uncache np on node removal */
    1253                 :          0 :                         of_node_put(np);
    1254                 :          0 :                         phandle_cache[masked_handle] = NULL;
    1255                 :            :                         np = NULL;
    1256                 :            :                 }
    1257                 :            :         }
    1258                 :            : 
    1259                 :          3 :         if (!np) {
    1260                 :          3 :                 for_each_of_allnodes(np)
    1261                 :          3 :                         if (np->phandle == handle &&
    1262                 :            :                             !of_node_check_flag(np, OF_DETACHED)) {
    1263                 :          3 :                                 if (phandle_cache) {
    1264                 :            :                                         /* will put when removed from cache */
    1265                 :          0 :                                         of_node_get(np);
    1266                 :          0 :                                         phandle_cache[masked_handle] = np;
    1267                 :            :                                 }
    1268                 :            :                                 break;
    1269                 :            :                         }
    1270                 :            :         }
    1271                 :            : 
    1272                 :          3 :         of_node_get(np);
    1273                 :          3 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
    1274                 :          3 :         return np;
    1275                 :            : }
    1276                 :            : EXPORT_SYMBOL(of_find_node_by_phandle);
    1277                 :            : 
    1278                 :          0 : void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
    1279                 :            : {
    1280                 :            :         int i;
    1281                 :          0 :         printk("%s %pOF", msg, args->np);
    1282                 :          0 :         for (i = 0; i < args->args_count; i++) {
    1283                 :          0 :                 const char delim = i ? ',' : ':';
    1284                 :            : 
    1285                 :          0 :                 pr_cont("%c%08x", delim, args->args[i]);
    1286                 :            :         }
    1287                 :          0 :         pr_cont("\n");
    1288                 :          0 : }
    1289                 :            : 
    1290                 :          3 : int of_phandle_iterator_init(struct of_phandle_iterator *it,
    1291                 :            :                 const struct device_node *np,
    1292                 :            :                 const char *list_name,
    1293                 :            :                 const char *cells_name,
    1294                 :            :                 int cell_count)
    1295                 :            : {
    1296                 :            :         const __be32 *list;
    1297                 :            :         int size;
    1298                 :            : 
    1299                 :          3 :         memset(it, 0, sizeof(*it));
    1300                 :            : 
    1301                 :            :         /*
    1302                 :            :          * one of cell_count or cells_name must be provided to determine the
    1303                 :            :          * argument length.
    1304                 :            :          */
    1305                 :          3 :         if (cell_count < 0 && !cells_name)
    1306                 :            :                 return -EINVAL;
    1307                 :            : 
    1308                 :            :         list = of_get_property(np, list_name, &size);
    1309                 :          3 :         if (!list)
    1310                 :            :                 return -ENOENT;
    1311                 :            : 
    1312                 :          3 :         it->cells_name = cells_name;
    1313                 :          3 :         it->cell_count = cell_count;
    1314                 :          3 :         it->parent = np;
    1315                 :          3 :         it->list_end = list + size / sizeof(*list);
    1316                 :          3 :         it->phandle_end = list;
    1317                 :          3 :         it->cur = list;
    1318                 :            : 
    1319                 :          3 :         return 0;
    1320                 :            : }
    1321                 :            : EXPORT_SYMBOL_GPL(of_phandle_iterator_init);
    1322                 :            : 
    1323                 :          3 : int of_phandle_iterator_next(struct of_phandle_iterator *it)
    1324                 :            : {
    1325                 :          3 :         uint32_t count = 0;
    1326                 :            : 
    1327                 :          3 :         if (it->node) {
    1328                 :          3 :                 of_node_put(it->node);
    1329                 :          3 :                 it->node = NULL;
    1330                 :            :         }
    1331                 :            : 
    1332                 :          3 :         if (!it->cur || it->phandle_end >= it->list_end)
    1333                 :            :                 return -ENOENT;
    1334                 :            : 
    1335                 :          3 :         it->cur = it->phandle_end;
    1336                 :            : 
    1337                 :            :         /* If phandle is 0, then it is an empty entry with no arguments. */
    1338                 :          3 :         it->phandle = be32_to_cpup(it->cur++);
    1339                 :            : 
    1340                 :          3 :         if (it->phandle) {
    1341                 :            : 
    1342                 :            :                 /*
    1343                 :            :                  * Find the provider node and parse the #*-cells property to
    1344                 :            :                  * determine the argument length.
    1345                 :            :                  */
    1346                 :          3 :                 it->node = of_find_node_by_phandle(it->phandle);
    1347                 :            : 
    1348                 :          3 :                 if (it->cells_name) {
    1349                 :          3 :                         if (!it->node) {
    1350                 :          0 :                                 pr_err("%pOF: could not find phandle\n",
    1351                 :            :                                        it->parent);
    1352                 :          0 :                                 goto err;
    1353                 :            :                         }
    1354                 :            : 
    1355                 :          3 :                         if (of_property_read_u32(it->node, it->cells_name,
    1356                 :            :                                                  &count)) {
    1357                 :            :                                 /*
    1358                 :            :                                  * If both cell_count and cells_name is given,
    1359                 :            :                                  * fall back to cell_count in absence
    1360                 :            :                                  * of the cells_name property
    1361                 :            :                                  */
    1362                 :          0 :                                 if (it->cell_count >= 0) {
    1363                 :          0 :                                         count = it->cell_count;
    1364                 :            :                                 } else {
    1365                 :          0 :                                         pr_err("%pOF: could not get %s for %pOF\n",
    1366                 :            :                                                it->parent,
    1367                 :            :                                                it->cells_name,
    1368                 :            :                                                it->node);
    1369                 :          0 :                                         goto err;
    1370                 :            :                                 }
    1371                 :            :                         }
    1372                 :            :                 } else {
    1373                 :          3 :                         count = it->cell_count;
    1374                 :            :                 }
    1375                 :            : 
    1376                 :            :                 /*
    1377                 :            :                  * Make sure that the arguments actually fit in the remaining
    1378                 :            :                  * property data length
    1379                 :            :                  */
    1380                 :          3 :                 if (it->cur + count > it->list_end) {
    1381                 :          0 :                         pr_err("%pOF: %s = %d found %d\n",
    1382                 :            :                                it->parent, it->cells_name,
    1383                 :            :                                count, it->cell_count);
    1384                 :          0 :                         goto err;
    1385                 :            :                 }
    1386                 :            :         }
    1387                 :            : 
    1388                 :          3 :         it->phandle_end = it->cur + count;
    1389                 :          3 :         it->cur_count = count;
    1390                 :            : 
    1391                 :          3 :         return 0;
    1392                 :            : 
    1393                 :            : err:
    1394                 :          0 :         if (it->node) {
    1395                 :          0 :                 of_node_put(it->node);
    1396                 :          0 :                 it->node = NULL;
    1397                 :            :         }
    1398                 :            : 
    1399                 :            :         return -EINVAL;
    1400                 :            : }
    1401                 :            : EXPORT_SYMBOL_GPL(of_phandle_iterator_next);
    1402                 :            : 
    1403                 :          3 : int of_phandle_iterator_args(struct of_phandle_iterator *it,
    1404                 :            :                              uint32_t *args,
    1405                 :            :                              int size)
    1406                 :            : {
    1407                 :            :         int i, count;
    1408                 :            : 
    1409                 :          3 :         count = it->cur_count;
    1410                 :            : 
    1411                 :          3 :         if (WARN_ON(size < count))
    1412                 :            :                 count = size;
    1413                 :            : 
    1414                 :          3 :         for (i = 0; i < count; i++)
    1415                 :          3 :                 args[i] = be32_to_cpup(it->cur++);
    1416                 :            : 
    1417                 :          3 :         return count;
    1418                 :            : }
    1419                 :            : 
    1420                 :          3 : static int __of_parse_phandle_with_args(const struct device_node *np,
    1421                 :            :                                         const char *list_name,
    1422                 :            :                                         const char *cells_name,
    1423                 :            :                                         int cell_count, int index,
    1424                 :            :                                         struct of_phandle_args *out_args)
    1425                 :            : {
    1426                 :            :         struct of_phandle_iterator it;
    1427                 :            :         int rc, cur_index = 0;
    1428                 :            : 
    1429                 :            :         /* Loop over the phandles until all the requested entry is found */
    1430                 :          3 :         of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
    1431                 :            :                 /*
    1432                 :            :                  * All of the error cases bail out of the loop, so at
    1433                 :            :                  * this point, the parsing is successful. If the requested
    1434                 :            :                  * index matches, then fill the out_args structure and return,
    1435                 :            :                  * or return -ENOENT for an empty entry.
    1436                 :            :                  */
    1437                 :            :                 rc = -ENOENT;
    1438                 :          3 :                 if (cur_index == index) {
    1439                 :          3 :                         if (!it.phandle)
    1440                 :            :                                 goto err;
    1441                 :            : 
    1442                 :          3 :                         if (out_args) {
    1443                 :            :                                 int c;
    1444                 :            : 
    1445                 :          3 :                                 c = of_phandle_iterator_args(&it,
    1446                 :          3 :                                                              out_args->args,
    1447                 :            :                                                              MAX_PHANDLE_ARGS);
    1448                 :          3 :                                 out_args->np = it.node;
    1449                 :          3 :                                 out_args->args_count = c;
    1450                 :            :                         } else {
    1451                 :          0 :                                 of_node_put(it.node);
    1452                 :            :                         }
    1453                 :            : 
    1454                 :            :                         /* Found it! return success */
    1455                 :            :                         return 0;
    1456                 :            :                 }
    1457                 :            : 
    1458                 :          3 :                 cur_index++;
    1459                 :            :         }
    1460                 :            : 
    1461                 :            :         /*
    1462                 :            :          * Unlock node before returning result; will be one of:
    1463                 :            :          * -ENOENT : index is for empty phandle
    1464                 :            :          * -EINVAL : parsing error on data
    1465                 :            :          */
    1466                 :            : 
    1467                 :            :  err:
    1468                 :          3 :         of_node_put(it.node);
    1469                 :          3 :         return rc;
    1470                 :            : }
    1471                 :            : 
    1472                 :            : /**
    1473                 :            :  * of_parse_phandle - Resolve a phandle property to a device_node pointer
    1474                 :            :  * @np: Pointer to device node holding phandle property
    1475                 :            :  * @phandle_name: Name of property holding a phandle value
    1476                 :            :  * @index: For properties holding a table of phandles, this is the index into
    1477                 :            :  *         the table
    1478                 :            :  *
    1479                 :            :  * Returns the device_node pointer with refcount incremented.  Use
    1480                 :            :  * of_node_put() on it when done.
    1481                 :            :  */
    1482                 :          3 : struct device_node *of_parse_phandle(const struct device_node *np,
    1483                 :            :                                      const char *phandle_name, int index)
    1484                 :            : {
    1485                 :            :         struct of_phandle_args args;
    1486                 :            : 
    1487                 :          3 :         if (index < 0)
    1488                 :            :                 return NULL;
    1489                 :            : 
    1490                 :          3 :         if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
    1491                 :            :                                          index, &args))
    1492                 :            :                 return NULL;
    1493                 :            : 
    1494                 :          3 :         return args.np;
    1495                 :            : }
    1496                 :            : EXPORT_SYMBOL(of_parse_phandle);
    1497                 :            : 
    1498                 :            : /**
    1499                 :            :  * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
    1500                 :            :  * @np:         pointer to a device tree node containing a list
    1501                 :            :  * @list_name:  property name that contains a list
    1502                 :            :  * @cells_name: property name that specifies phandles' arguments count
    1503                 :            :  * @index:      index of a phandle to parse out
    1504                 :            :  * @out_args:   optional pointer to output arguments structure (will be filled)
    1505                 :            :  *
    1506                 :            :  * This function is useful to parse lists of phandles and their arguments.
    1507                 :            :  * Returns 0 on success and fills out_args, on error returns appropriate
    1508                 :            :  * errno value.
    1509                 :            :  *
    1510                 :            :  * Caller is responsible to call of_node_put() on the returned out_args->np
    1511                 :            :  * pointer.
    1512                 :            :  *
    1513                 :            :  * Example:
    1514                 :            :  *
    1515                 :            :  * phandle1: node1 {
    1516                 :            :  *      #list-cells = <2>;
    1517                 :            :  * }
    1518                 :            :  *
    1519                 :            :  * phandle2: node2 {
    1520                 :            :  *      #list-cells = <1>;
    1521                 :            :  * }
    1522                 :            :  *
    1523                 :            :  * node3 {
    1524                 :            :  *      list = <&phandle1 1 2 &phandle2 3>;
    1525                 :            :  * }
    1526                 :            :  *
    1527                 :            :  * To get a device_node of the `node2' node you may call this:
    1528                 :            :  * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
    1529                 :            :  */
    1530                 :          3 : int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
    1531                 :            :                                 const char *cells_name, int index,
    1532                 :            :                                 struct of_phandle_args *out_args)
    1533                 :            : {
    1534                 :            :         int cell_count = -1;
    1535                 :            : 
    1536                 :          3 :         if (index < 0)
    1537                 :            :                 return -EINVAL;
    1538                 :            : 
    1539                 :            :         /* If cells_name is NULL we assume a cell count of 0 */
    1540                 :          3 :         if (!cells_name)
    1541                 :            :                 cell_count = 0;
    1542                 :            : 
    1543                 :          3 :         return __of_parse_phandle_with_args(np, list_name, cells_name,
    1544                 :            :                                             cell_count, index, out_args);
    1545                 :            : }
    1546                 :            : EXPORT_SYMBOL(of_parse_phandle_with_args);
    1547                 :            : 
    1548                 :            : /**
    1549                 :            :  * of_parse_phandle_with_args_map() - Find a node pointed by phandle in a list and remap it
    1550                 :            :  * @np:         pointer to a device tree node containing a list
    1551                 :            :  * @list_name:  property name that contains a list
    1552                 :            :  * @stem_name:  stem of property names that specify phandles' arguments count
    1553                 :            :  * @index:      index of a phandle to parse out
    1554                 :            :  * @out_args:   optional pointer to output arguments structure (will be filled)
    1555                 :            :  *
    1556                 :            :  * This function is useful to parse lists of phandles and their arguments.
    1557                 :            :  * Returns 0 on success and fills out_args, on error returns appropriate errno
    1558                 :            :  * value. The difference between this function and of_parse_phandle_with_args()
    1559                 :            :  * is that this API remaps a phandle if the node the phandle points to has
    1560                 :            :  * a <@stem_name>-map property.
    1561                 :            :  *
    1562                 :            :  * Caller is responsible to call of_node_put() on the returned out_args->np
    1563                 :            :  * pointer.
    1564                 :            :  *
    1565                 :            :  * Example:
    1566                 :            :  *
    1567                 :            :  * phandle1: node1 {
    1568                 :            :  *      #list-cells = <2>;
    1569                 :            :  * }
    1570                 :            :  *
    1571                 :            :  * phandle2: node2 {
    1572                 :            :  *      #list-cells = <1>;
    1573                 :            :  * }
    1574                 :            :  *
    1575                 :            :  * phandle3: node3 {
    1576                 :            :  *      #list-cells = <1>;
    1577                 :            :  *      list-map = <0 &phandle2 3>,
    1578                 :            :  *                 <1 &phandle2 2>,
    1579                 :            :  *                 <2 &phandle1 5 1>;
    1580                 :            :  *      list-map-mask = <0x3>;
    1581                 :            :  * };
    1582                 :            :  *
    1583                 :            :  * node4 {
    1584                 :            :  *      list = <&phandle1 1 2 &phandle3 0>;
    1585                 :            :  * }
    1586                 :            :  *
    1587                 :            :  * To get a device_node of the `node2' node you may call this:
    1588                 :            :  * of_parse_phandle_with_args(node4, "list", "list", 1, &args);
    1589                 :            :  */
    1590                 :          3 : int of_parse_phandle_with_args_map(const struct device_node *np,
    1591                 :            :                                    const char *list_name,
    1592                 :            :                                    const char *stem_name,
    1593                 :            :                                    int index, struct of_phandle_args *out_args)
    1594                 :            : {
    1595                 :            :         char *cells_name, *map_name = NULL, *mask_name = NULL;
    1596                 :            :         char *pass_name = NULL;
    1597                 :            :         struct device_node *cur, *new = NULL;
    1598                 :            :         const __be32 *map, *mask, *pass;
    1599                 :            :         static const __be32 dummy_mask[] = { [0 ... MAX_PHANDLE_ARGS] = ~0 };
    1600                 :            :         static const __be32 dummy_pass[] = { [0 ... MAX_PHANDLE_ARGS] = 0 };
    1601                 :            :         __be32 initial_match_array[MAX_PHANDLE_ARGS];
    1602                 :            :         const __be32 *match_array = initial_match_array;
    1603                 :            :         int i, ret, map_len, match;
    1604                 :            :         u32 list_size, new_size;
    1605                 :            : 
    1606                 :          3 :         if (index < 0)
    1607                 :            :                 return -EINVAL;
    1608                 :            : 
    1609                 :          3 :         cells_name = kasprintf(GFP_KERNEL, "#%s-cells", stem_name);
    1610                 :          3 :         if (!cells_name)
    1611                 :            :                 return -ENOMEM;
    1612                 :            : 
    1613                 :            :         ret = -ENOMEM;
    1614                 :          3 :         map_name = kasprintf(GFP_KERNEL, "%s-map", stem_name);
    1615                 :          3 :         if (!map_name)
    1616                 :            :                 goto free;
    1617                 :            : 
    1618                 :          3 :         mask_name = kasprintf(GFP_KERNEL, "%s-map-mask", stem_name);
    1619                 :          3 :         if (!mask_name)
    1620                 :            :                 goto free;
    1621                 :            : 
    1622                 :          3 :         pass_name = kasprintf(GFP_KERNEL, "%s-map-pass-thru", stem_name);
    1623                 :          3 :         if (!pass_name)
    1624                 :            :                 goto free;
    1625                 :            : 
    1626                 :          3 :         ret = __of_parse_phandle_with_args(np, list_name, cells_name, -1, index,
    1627                 :            :                                            out_args);
    1628                 :          3 :         if (ret)
    1629                 :            :                 goto free;
    1630                 :            : 
    1631                 :            :         /* Get the #<list>-cells property */
    1632                 :          3 :         cur = out_args->np;
    1633                 :            :         ret = of_property_read_u32(cur, cells_name, &list_size);
    1634                 :          3 :         if (ret < 0)
    1635                 :            :                 goto put;
    1636                 :            : 
    1637                 :            :         /* Precalculate the match array - this simplifies match loop */
    1638                 :          3 :         for (i = 0; i < list_size; i++)
    1639                 :          3 :                 initial_match_array[i] = cpu_to_be32(out_args->args[i]);
    1640                 :            : 
    1641                 :            :         ret = -EINVAL;
    1642                 :          3 :         while (cur) {
    1643                 :            :                 /* Get the <list>-map property */
    1644                 :            :                 map = of_get_property(cur, map_name, &map_len);
    1645                 :          3 :                 if (!map) {
    1646                 :            :                         ret = 0;
    1647                 :            :                         goto free;
    1648                 :            :                 }
    1649                 :          0 :                 map_len /= sizeof(u32);
    1650                 :            : 
    1651                 :            :                 /* Get the <list>-map-mask property (optional) */
    1652                 :            :                 mask = of_get_property(cur, mask_name, NULL);
    1653                 :          0 :                 if (!mask)
    1654                 :            :                         mask = dummy_mask;
    1655                 :            :                 /* Iterate through <list>-map property */
    1656                 :            :                 match = 0;
    1657                 :          0 :                 while (map_len > (list_size + 1) && !match) {
    1658                 :            :                         /* Compare specifiers */
    1659                 :            :                         match = 1;
    1660                 :          0 :                         for (i = 0; i < list_size; i++, map_len--)
    1661                 :          0 :                                 match &= !((match_array[i] ^ *map++) & mask[i]);
    1662                 :            : 
    1663                 :          0 :                         of_node_put(new);
    1664                 :          0 :                         new = of_find_node_by_phandle(be32_to_cpup(map));
    1665                 :            :                         map++;
    1666                 :          0 :                         map_len--;
    1667                 :            : 
    1668                 :            :                         /* Check if not found */
    1669                 :          0 :                         if (!new)
    1670                 :            :                                 goto put;
    1671                 :            : 
    1672                 :          0 :                         if (!of_device_is_available(new))
    1673                 :            :                                 match = 0;
    1674                 :            : 
    1675                 :            :                         ret = of_property_read_u32(new, cells_name, &new_size);
    1676                 :          0 :                         if (ret)
    1677                 :            :                                 goto put;
    1678                 :            : 
    1679                 :            :                         /* Check for malformed properties */
    1680                 :          0 :                         if (WARN_ON(new_size > MAX_PHANDLE_ARGS))
    1681                 :            :                                 goto put;
    1682                 :          0 :                         if (map_len < new_size)
    1683                 :            :                                 goto put;
    1684                 :            : 
    1685                 :            :                         /* Move forward by new node's #<list>-cells amount */
    1686                 :          0 :                         map += new_size;
    1687                 :          0 :                         map_len -= new_size;
    1688                 :            :                 }
    1689                 :          0 :                 if (!match)
    1690                 :            :                         goto put;
    1691                 :            : 
    1692                 :            :                 /* Get the <list>-map-pass-thru property (optional) */
    1693                 :            :                 pass = of_get_property(cur, pass_name, NULL);
    1694                 :          0 :                 if (!pass)
    1695                 :            :                         pass = dummy_pass;
    1696                 :            : 
    1697                 :            :                 /*
    1698                 :            :                  * Successfully parsed a <list>-map translation; copy new
    1699                 :            :                  * specifier into the out_args structure, keeping the
    1700                 :            :                  * bits specified in <list>-map-pass-thru.
    1701                 :            :                  */
    1702                 :          0 :                 match_array = map - new_size;
    1703                 :          0 :                 for (i = 0; i < new_size; i++) {
    1704                 :          0 :                         __be32 val = *(map - new_size + i);
    1705                 :            : 
    1706                 :          0 :                         if (i < list_size) {
    1707                 :          0 :                                 val &= ~pass[i];
    1708                 :          0 :                                 val |= cpu_to_be32(out_args->args[i]) & pass[i];
    1709                 :            :                         }
    1710                 :            : 
    1711                 :          0 :                         out_args->args[i] = be32_to_cpu(val);
    1712                 :            :                 }
    1713                 :          0 :                 out_args->args_count = list_size = new_size;
    1714                 :            :                 /* Iterate again with new provider */
    1715                 :          0 :                 out_args->np = new;
    1716                 :          0 :                 of_node_put(cur);
    1717                 :          0 :                 cur = new;
    1718                 :            :         }
    1719                 :            : put:
    1720                 :          0 :         of_node_put(cur);
    1721                 :          0 :         of_node_put(new);
    1722                 :            : free:
    1723                 :          3 :         kfree(mask_name);
    1724                 :          3 :         kfree(map_name);
    1725                 :          3 :         kfree(cells_name);
    1726                 :          3 :         kfree(pass_name);
    1727                 :            : 
    1728                 :          3 :         return ret;
    1729                 :            : }
    1730                 :            : EXPORT_SYMBOL(of_parse_phandle_with_args_map);
    1731                 :            : 
    1732                 :            : /**
    1733                 :            :  * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
    1734                 :            :  * @np:         pointer to a device tree node containing a list
    1735                 :            :  * @list_name:  property name that contains a list
    1736                 :            :  * @cell_count: number of argument cells following the phandle
    1737                 :            :  * @index:      index of a phandle to parse out
    1738                 :            :  * @out_args:   optional pointer to output arguments structure (will be filled)
    1739                 :            :  *
    1740                 :            :  * This function is useful to parse lists of phandles and their arguments.
    1741                 :            :  * Returns 0 on success and fills out_args, on error returns appropriate
    1742                 :            :  * errno value.
    1743                 :            :  *
    1744                 :            :  * Caller is responsible to call of_node_put() on the returned out_args->np
    1745                 :            :  * pointer.
    1746                 :            :  *
    1747                 :            :  * Example:
    1748                 :            :  *
    1749                 :            :  * phandle1: node1 {
    1750                 :            :  * }
    1751                 :            :  *
    1752                 :            :  * phandle2: node2 {
    1753                 :            :  * }
    1754                 :            :  *
    1755                 :            :  * node3 {
    1756                 :            :  *      list = <&phandle1 0 2 &phandle2 2 3>;
    1757                 :            :  * }
    1758                 :            :  *
    1759                 :            :  * To get a device_node of the `node2' node you may call this:
    1760                 :            :  * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
    1761                 :            :  */
    1762                 :          3 : int of_parse_phandle_with_fixed_args(const struct device_node *np,
    1763                 :            :                                 const char *list_name, int cell_count,
    1764                 :            :                                 int index, struct of_phandle_args *out_args)
    1765                 :            : {
    1766                 :          3 :         if (index < 0)
    1767                 :            :                 return -EINVAL;
    1768                 :          3 :         return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
    1769                 :            :                                            index, out_args);
    1770                 :            : }
    1771                 :            : EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
    1772                 :            : 
    1773                 :            : /**
    1774                 :            :  * of_count_phandle_with_args() - Find the number of phandles references in a property
    1775                 :            :  * @np:         pointer to a device tree node containing a list
    1776                 :            :  * @list_name:  property name that contains a list
    1777                 :            :  * @cells_name: property name that specifies phandles' arguments count
    1778                 :            :  *
    1779                 :            :  * Returns the number of phandle + argument tuples within a property. It
    1780                 :            :  * is a typical pattern to encode a list of phandle and variable
    1781                 :            :  * arguments into a single property. The number of arguments is encoded
    1782                 :            :  * by a property in the phandle-target node. For example, a gpios
    1783                 :            :  * property would contain a list of GPIO specifies consisting of a
    1784                 :            :  * phandle and 1 or more arguments. The number of arguments are
    1785                 :            :  * determined by the #gpio-cells property in the node pointed to by the
    1786                 :            :  * phandle.
    1787                 :            :  */
    1788                 :          3 : int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
    1789                 :            :                                 const char *cells_name)
    1790                 :            : {
    1791                 :            :         struct of_phandle_iterator it;
    1792                 :            :         int rc, cur_index = 0;
    1793                 :            : 
    1794                 :            :         /*
    1795                 :            :          * If cells_name is NULL we assume a cell count of 0. This makes
    1796                 :            :          * counting the phandles trivial as each 32bit word in the list is a
    1797                 :            :          * phandle and no arguments are to consider. So we don't iterate through
    1798                 :            :          * the list but just use the length to determine the phandle count.
    1799                 :            :          */
    1800                 :          3 :         if (!cells_name) {
    1801                 :            :                 const __be32 *list;
    1802                 :            :                 int size;
    1803                 :            : 
    1804                 :            :                 list = of_get_property(np, list_name, &size);
    1805                 :          3 :                 if (!list)
    1806                 :            :                         return -ENOENT;
    1807                 :            : 
    1808                 :          0 :                 return size / sizeof(*list);
    1809                 :            :         }
    1810                 :            : 
    1811                 :          3 :         rc = of_phandle_iterator_init(&it, np, list_name, cells_name, -1);
    1812                 :          3 :         if (rc)
    1813                 :            :                 return rc;
    1814                 :            : 
    1815                 :          3 :         while ((rc = of_phandle_iterator_next(&it)) == 0)
    1816                 :          3 :                 cur_index += 1;
    1817                 :            : 
    1818                 :          3 :         if (rc != -ENOENT)
    1819                 :          0 :                 return rc;
    1820                 :            : 
    1821                 :          3 :         return cur_index;
    1822                 :            : }
    1823                 :            : EXPORT_SYMBOL(of_count_phandle_with_args);
    1824                 :            : 
    1825                 :            : /**
    1826                 :            :  * __of_add_property - Add a property to a node without lock operations
    1827                 :            :  */
    1828                 :          0 : int __of_add_property(struct device_node *np, struct property *prop)
    1829                 :            : {
    1830                 :            :         struct property **next;
    1831                 :            : 
    1832                 :          0 :         prop->next = NULL;
    1833                 :          0 :         next = &np->properties;
    1834                 :          0 :         while (*next) {
    1835                 :          0 :                 if (strcmp(prop->name, (*next)->name) == 0)
    1836                 :            :                         /* duplicate ! don't insert it */
    1837                 :            :                         return -EEXIST;
    1838                 :            : 
    1839                 :          0 :                 next = &(*next)->next;
    1840                 :            :         }
    1841                 :          0 :         *next = prop;
    1842                 :            : 
    1843                 :          0 :         return 0;
    1844                 :            : }
    1845                 :            : 
    1846                 :            : /**
    1847                 :            :  * of_add_property - Add a property to a node
    1848                 :            :  */
    1849                 :          0 : int of_add_property(struct device_node *np, struct property *prop)
    1850                 :            : {
    1851                 :            :         unsigned long flags;
    1852                 :            :         int rc;
    1853                 :            : 
    1854                 :          0 :         mutex_lock(&of_mutex);
    1855                 :            : 
    1856                 :          0 :         raw_spin_lock_irqsave(&devtree_lock, flags);
    1857                 :          0 :         rc = __of_add_property(np, prop);
    1858                 :          0 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
    1859                 :            : 
    1860                 :          0 :         if (!rc)
    1861                 :          0 :                 __of_add_property_sysfs(np, prop);
    1862                 :            : 
    1863                 :          0 :         mutex_unlock(&of_mutex);
    1864                 :            : 
    1865                 :          0 :         if (!rc)
    1866                 :          0 :                 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
    1867                 :            : 
    1868                 :          0 :         return rc;
    1869                 :            : }
    1870                 :            : 
    1871                 :          0 : int __of_remove_property(struct device_node *np, struct property *prop)
    1872                 :            : {
    1873                 :            :         struct property **next;
    1874                 :            : 
    1875                 :          0 :         for (next = &np->properties; *next; next = &(*next)->next) {
    1876                 :          0 :                 if (*next == prop)
    1877                 :            :                         break;
    1878                 :            :         }
    1879                 :          0 :         if (*next == NULL)
    1880                 :            :                 return -ENODEV;
    1881                 :            : 
    1882                 :            :         /* found the node */
    1883                 :          0 :         *next = prop->next;
    1884                 :          0 :         prop->next = np->deadprops;
    1885                 :          0 :         np->deadprops = prop;
    1886                 :            : 
    1887                 :          0 :         return 0;
    1888                 :            : }
    1889                 :            : 
    1890                 :            : /**
    1891                 :            :  * of_remove_property - Remove a property from a node.
    1892                 :            :  *
    1893                 :            :  * Note that we don't actually remove it, since we have given out
    1894                 :            :  * who-knows-how-many pointers to the data using get-property.
    1895                 :            :  * Instead we just move the property to the "dead properties"
    1896                 :            :  * list, so it won't be found any more.
    1897                 :            :  */
    1898                 :          0 : int of_remove_property(struct device_node *np, struct property *prop)
    1899                 :            : {
    1900                 :            :         unsigned long flags;
    1901                 :            :         int rc;
    1902                 :            : 
    1903                 :          0 :         if (!prop)
    1904                 :            :                 return -ENODEV;
    1905                 :            : 
    1906                 :          0 :         mutex_lock(&of_mutex);
    1907                 :            : 
    1908                 :          0 :         raw_spin_lock_irqsave(&devtree_lock, flags);
    1909                 :            :         rc = __of_remove_property(np, prop);
    1910                 :          0 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
    1911                 :            : 
    1912                 :          0 :         if (!rc)
    1913                 :          0 :                 __of_remove_property_sysfs(np, prop);
    1914                 :            : 
    1915                 :          0 :         mutex_unlock(&of_mutex);
    1916                 :            : 
    1917                 :          0 :         if (!rc)
    1918                 :          0 :                 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
    1919                 :            : 
    1920                 :          0 :         return rc;
    1921                 :            : }
    1922                 :            : 
    1923                 :          0 : int __of_update_property(struct device_node *np, struct property *newprop,
    1924                 :            :                 struct property **oldpropp)
    1925                 :            : {
    1926                 :            :         struct property **next, *oldprop;
    1927                 :            : 
    1928                 :          0 :         for (next = &np->properties; *next; next = &(*next)->next) {
    1929                 :          0 :                 if (of_prop_cmp((*next)->name, newprop->name) == 0)
    1930                 :            :                         break;
    1931                 :            :         }
    1932                 :          0 :         *oldpropp = oldprop = *next;
    1933                 :            : 
    1934                 :          0 :         if (oldprop) {
    1935                 :            :                 /* replace the node */
    1936                 :          0 :                 newprop->next = oldprop->next;
    1937                 :          0 :                 *next = newprop;
    1938                 :          0 :                 oldprop->next = np->deadprops;
    1939                 :          0 :                 np->deadprops = oldprop;
    1940                 :            :         } else {
    1941                 :            :                 /* new node */
    1942                 :          0 :                 newprop->next = NULL;
    1943                 :          0 :                 *next = newprop;
    1944                 :            :         }
    1945                 :            : 
    1946                 :          0 :         return 0;
    1947                 :            : }
    1948                 :            : 
    1949                 :            : /*
    1950                 :            :  * of_update_property - Update a property in a node, if the property does
    1951                 :            :  * not exist, add it.
    1952                 :            :  *
    1953                 :            :  * Note that we don't actually remove it, since we have given out
    1954                 :            :  * who-knows-how-many pointers to the data using get-property.
    1955                 :            :  * Instead we just move the property to the "dead properties" list,
    1956                 :            :  * and add the new property to the property list
    1957                 :            :  */
    1958                 :          0 : int of_update_property(struct device_node *np, struct property *newprop)
    1959                 :            : {
    1960                 :            :         struct property *oldprop;
    1961                 :            :         unsigned long flags;
    1962                 :            :         int rc;
    1963                 :            : 
    1964                 :          0 :         if (!newprop->name)
    1965                 :            :                 return -EINVAL;
    1966                 :            : 
    1967                 :          0 :         mutex_lock(&of_mutex);
    1968                 :            : 
    1969                 :          0 :         raw_spin_lock_irqsave(&devtree_lock, flags);
    1970                 :          0 :         rc = __of_update_property(np, newprop, &oldprop);
    1971                 :          0 :         raw_spin_unlock_irqrestore(&devtree_lock, flags);
    1972                 :            : 
    1973                 :          0 :         if (!rc)
    1974                 :          0 :                 __of_update_property_sysfs(np, newprop, oldprop);
    1975                 :            : 
    1976                 :          0 :         mutex_unlock(&of_mutex);
    1977                 :            : 
    1978                 :          0 :         if (!rc)
    1979                 :          0 :                 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
    1980                 :            : 
    1981                 :          0 :         return rc;
    1982                 :            : }
    1983                 :            : 
    1984                 :            : static void of_alias_add(struct alias_prop *ap, struct device_node *np,
    1985                 :            :                          int id, const char *stem, int stem_len)
    1986                 :            : {
    1987                 :          3 :         ap->np = np;
    1988                 :          3 :         ap->id = id;
    1989                 :          3 :         strncpy(ap->stem, stem, stem_len);
    1990                 :          3 :         ap->stem[stem_len] = 0;
    1991                 :          3 :         list_add_tail(&ap->link, &aliases_lookup);
    1992                 :            :         pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n",
    1993                 :            :                  ap->alias, ap->stem, ap->id, np);
    1994                 :            : }
    1995                 :            : 
    1996                 :            : /**
    1997                 :            :  * of_alias_scan - Scan all properties of the 'aliases' node
    1998                 :            :  *
    1999                 :            :  * The function scans all the properties of the 'aliases' node and populates
    2000                 :            :  * the global lookup table with the properties.  It returns the
    2001                 :            :  * number of alias properties found, or an error code in case of failure.
    2002                 :            :  *
    2003                 :            :  * @dt_alloc:   An allocator that provides a virtual address to memory
    2004                 :            :  *              for storing the resulting tree
    2005                 :            :  */
    2006                 :          3 : void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
    2007                 :            : {
    2008                 :            :         struct property *pp;
    2009                 :            : 
    2010                 :          3 :         of_aliases = of_find_node_by_path("/aliases");
    2011                 :          3 :         of_chosen = of_find_node_by_path("/chosen");
    2012                 :          3 :         if (of_chosen == NULL)
    2013                 :          0 :                 of_chosen = of_find_node_by_path("/chosen@0");
    2014                 :            : 
    2015                 :          3 :         if (of_chosen) {
    2016                 :            :                 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
    2017                 :          3 :                 const char *name = NULL;
    2018                 :            : 
    2019                 :          3 :                 if (of_property_read_string(of_chosen, "stdout-path", &name))
    2020                 :          3 :                         of_property_read_string(of_chosen, "linux,stdout-path",
    2021                 :            :                                                 &name);
    2022                 :            :                 if (IS_ENABLED(CONFIG_PPC) && !name)
    2023                 :            :                         of_property_read_string(of_aliases, "stdout", &name);
    2024                 :          3 :                 if (name)
    2025                 :          0 :                         of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
    2026                 :            :         }
    2027                 :            : 
    2028                 :          3 :         if (!of_aliases)
    2029                 :          3 :                 return;
    2030                 :            : 
    2031                 :          3 :         for_each_property_of_node(of_aliases, pp) {
    2032                 :          3 :                 const char *start = pp->name;
    2033                 :          3 :                 const char *end = start + strlen(start);
    2034                 :            :                 struct device_node *np;
    2035                 :            :                 struct alias_prop *ap;
    2036                 :            :                 int id, len;
    2037                 :            : 
    2038                 :            :                 /* Skip those we do not want to proceed */
    2039                 :          3 :                 if (!strcmp(pp->name, "name") ||
    2040                 :          3 :                     !strcmp(pp->name, "phandle") ||
    2041                 :          3 :                     !strcmp(pp->name, "linux,phandle"))
    2042                 :          3 :                         continue;
    2043                 :            : 
    2044                 :          3 :                 np = of_find_node_by_path(pp->value);
    2045                 :          3 :                 if (!np)
    2046                 :          0 :                         continue;
    2047                 :            : 
    2048                 :            :                 /* walk the alias backwards to extract the id and work out
    2049                 :            :                  * the 'stem' string */
    2050                 :          3 :                 while (isdigit(*(end-1)) && end > start)
    2051                 :          3 :                         end--;
    2052                 :          3 :                 len = end - start;
    2053                 :            : 
    2054                 :          3 :                 if (kstrtoint(end, 10, &id) < 0)
    2055                 :          3 :                         continue;
    2056                 :            : 
    2057                 :            :                 /* Allocate an alias_prop with enough space for the stem */
    2058                 :          3 :                 ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap));
    2059                 :          3 :                 if (!ap)
    2060                 :          0 :                         continue;
    2061                 :          3 :                 memset(ap, 0, sizeof(*ap) + len + 1);
    2062                 :          3 :                 ap->alias = start;
    2063                 :          3 :                 of_alias_add(ap, np, id, start, len);
    2064                 :            :         }
    2065                 :            : }
    2066                 :            : 
    2067                 :            : /**
    2068                 :            :  * of_alias_get_id - Get alias id for the given device_node
    2069                 :            :  * @np:         Pointer to the given device_node
    2070                 :            :  * @stem:       Alias stem of the given device_node
    2071                 :            :  *
    2072                 :            :  * The function travels the lookup table to get the alias id for the given
    2073                 :            :  * device_node and alias stem.  It returns the alias id if found.
    2074                 :            :  */
    2075                 :          3 : int of_alias_get_id(struct device_node *np, const char *stem)
    2076                 :            : {
    2077                 :            :         struct alias_prop *app;
    2078                 :            :         int id = -ENODEV;
    2079                 :            : 
    2080                 :          3 :         mutex_lock(&of_mutex);
    2081                 :          3 :         list_for_each_entry(app, &aliases_lookup, link) {
    2082                 :          3 :                 if (strcmp(app->stem, stem) != 0)
    2083                 :          3 :                         continue;
    2084                 :            : 
    2085                 :          3 :                 if (np == app->np) {
    2086                 :          3 :                         id = app->id;
    2087                 :          3 :                         break;
    2088                 :            :                 }
    2089                 :            :         }
    2090                 :          3 :         mutex_unlock(&of_mutex);
    2091                 :            : 
    2092                 :          3 :         return id;
    2093                 :            : }
    2094                 :            : EXPORT_SYMBOL_GPL(of_alias_get_id);
    2095                 :            : 
    2096                 :            : /**
    2097                 :            :  * of_alias_get_alias_list - Get alias list for the given device driver
    2098                 :            :  * @matches:    Array of OF device match structures to search in
    2099                 :            :  * @stem:       Alias stem of the given device_node
    2100                 :            :  * @bitmap:     Bitmap field pointer
    2101                 :            :  * @nbits:      Maximum number of alias IDs which can be recorded in bitmap
    2102                 :            :  *
    2103                 :            :  * The function travels the lookup table to record alias ids for the given
    2104                 :            :  * device match structures and alias stem.
    2105                 :            :  *
    2106                 :            :  * Return:      0 or -ENOSYS when !CONFIG_OF or
    2107                 :            :  *              -EOVERFLOW if alias ID is greater then allocated nbits
    2108                 :            :  */
    2109                 :          0 : int of_alias_get_alias_list(const struct of_device_id *matches,
    2110                 :            :                              const char *stem, unsigned long *bitmap,
    2111                 :            :                              unsigned int nbits)
    2112                 :            : {
    2113                 :            :         struct alias_prop *app;
    2114                 :            :         int ret = 0;
    2115                 :            : 
    2116                 :            :         /* Zero bitmap field to make sure that all the time it is clean */
    2117                 :            :         bitmap_zero(bitmap, nbits);
    2118                 :            : 
    2119                 :          0 :         mutex_lock(&of_mutex);
    2120                 :            :         pr_debug("%s: Looking for stem: %s\n", __func__, stem);
    2121                 :          0 :         list_for_each_entry(app, &aliases_lookup, link) {
    2122                 :            :                 pr_debug("%s: stem: %s, id: %d\n",
    2123                 :            :                          __func__, app->stem, app->id);
    2124                 :            : 
    2125                 :          0 :                 if (strcmp(app->stem, stem) != 0) {
    2126                 :            :                         pr_debug("%s: stem comparison didn't pass %s\n",
    2127                 :            :                                  __func__, app->stem);
    2128                 :          0 :                         continue;
    2129                 :            :                 }
    2130                 :            : 
    2131                 :          0 :                 if (of_match_node(matches, app->np)) {
    2132                 :            :                         pr_debug("%s: Allocated ID %d\n", __func__, app->id);
    2133                 :            : 
    2134                 :          0 :                         if (app->id >= nbits) {
    2135                 :          0 :                                 pr_warn("%s: ID %d >= than bitmap field %d\n",
    2136                 :            :                                         __func__, app->id, nbits);
    2137                 :            :                                 ret = -EOVERFLOW;
    2138                 :            :                         } else {
    2139                 :          0 :                                 set_bit(app->id, bitmap);
    2140                 :            :                         }
    2141                 :            :                 }
    2142                 :            :         }
    2143                 :          0 :         mutex_unlock(&of_mutex);
    2144                 :            : 
    2145                 :          0 :         return ret;
    2146                 :            : }
    2147                 :            : EXPORT_SYMBOL_GPL(of_alias_get_alias_list);
    2148                 :            : 
    2149                 :            : /**
    2150                 :            :  * of_alias_get_highest_id - Get highest alias id for the given stem
    2151                 :            :  * @stem:       Alias stem to be examined
    2152                 :            :  *
    2153                 :            :  * The function travels the lookup table to get the highest alias id for the
    2154                 :            :  * given alias stem.  It returns the alias id if found.
    2155                 :            :  */
    2156                 :          3 : int of_alias_get_highest_id(const char *stem)
    2157                 :            : {
    2158                 :            :         struct alias_prop *app;
    2159                 :            :         int id = -ENODEV;
    2160                 :            : 
    2161                 :          3 :         mutex_lock(&of_mutex);
    2162                 :          3 :         list_for_each_entry(app, &aliases_lookup, link) {
    2163                 :          3 :                 if (strcmp(app->stem, stem) != 0)
    2164                 :          3 :                         continue;
    2165                 :            : 
    2166                 :          3 :                 if (app->id > id)
    2167                 :            :                         id = app->id;
    2168                 :            :         }
    2169                 :          3 :         mutex_unlock(&of_mutex);
    2170                 :            : 
    2171                 :          3 :         return id;
    2172                 :            : }
    2173                 :            : EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
    2174                 :            : 
    2175                 :            : /**
    2176                 :            :  * of_console_check() - Test and setup console for DT setup
    2177                 :            :  * @dn - Pointer to device node
    2178                 :            :  * @name - Name to use for preferred console without index. ex. "ttyS"
    2179                 :            :  * @index - Index to use for preferred console.
    2180                 :            :  *
    2181                 :            :  * Check if the given device node matches the stdout-path property in the
    2182                 :            :  * /chosen node. If it does then register it as the preferred console and return
    2183                 :            :  * TRUE. Otherwise return FALSE.
    2184                 :            :  */
    2185                 :          3 : bool of_console_check(struct device_node *dn, char *name, int index)
    2186                 :            : {
    2187                 :          3 :         if (!dn || dn != of_stdout || console_set_on_cmdline)
    2188                 :            :                 return false;
    2189                 :            : 
    2190                 :            :         /*
    2191                 :            :          * XXX: cast `options' to char pointer to suppress complication
    2192                 :            :          * warnings: printk, UART and console drivers expect char pointer.
    2193                 :            :          */
    2194                 :          0 :         return !add_preferred_console(name, index, (char *)of_stdout_options);
    2195                 :            : }
    2196                 :            : EXPORT_SYMBOL_GPL(of_console_check);
    2197                 :            : 
    2198                 :            : /**
    2199                 :            :  *      of_find_next_cache_node - Find a node's subsidiary cache
    2200                 :            :  *      @np:    node of type "cpu" or "cache"
    2201                 :            :  *
    2202                 :            :  *      Returns a node pointer with refcount incremented, use
    2203                 :            :  *      of_node_put() on it when done.  Caller should hold a reference
    2204                 :            :  *      to np.
    2205                 :            :  */
    2206                 :          0 : struct device_node *of_find_next_cache_node(const struct device_node *np)
    2207                 :            : {
    2208                 :            :         struct device_node *child, *cache_node;
    2209                 :            : 
    2210                 :          0 :         cache_node = of_parse_phandle(np, "l2-cache", 0);
    2211                 :          0 :         if (!cache_node)
    2212                 :          0 :                 cache_node = of_parse_phandle(np, "next-level-cache", 0);
    2213                 :            : 
    2214                 :          0 :         if (cache_node)
    2215                 :          0 :                 return cache_node;
    2216                 :            : 
    2217                 :            :         /* OF on pmac has nodes instead of properties named "l2-cache"
    2218                 :            :          * beneath CPU nodes.
    2219                 :            :          */
    2220                 :            :         if (IS_ENABLED(CONFIG_PPC_PMAC) && of_node_is_type(np, "cpu"))
    2221                 :            :                 for_each_child_of_node(np, child)
    2222                 :            :                         if (of_node_is_type(child, "cache"))
    2223                 :            :                                 return child;
    2224                 :            : 
    2225                 :            :         return NULL;
    2226                 :            : }
    2227                 :            : 
    2228                 :            : /**
    2229                 :            :  * of_find_last_cache_level - Find the level at which the last cache is
    2230                 :            :  *              present for the given logical cpu
    2231                 :            :  *
    2232                 :            :  * @cpu: cpu number(logical index) for which the last cache level is needed
    2233                 :            :  *
    2234                 :            :  * Returns the the level at which the last cache is present. It is exactly
    2235                 :            :  * same as  the total number of cache levels for the given logical cpu.
    2236                 :            :  */
    2237                 :          0 : int of_find_last_cache_level(unsigned int cpu)
    2238                 :            : {
    2239                 :          0 :         u32 cache_level = 0;
    2240                 :          0 :         struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu);
    2241                 :            : 
    2242                 :          0 :         while (np) {
    2243                 :            :                 prev = np;
    2244                 :          0 :                 of_node_put(np);
    2245                 :          0 :                 np = of_find_next_cache_node(np);
    2246                 :            :         }
    2247                 :            : 
    2248                 :            :         of_property_read_u32(prev, "cache-level", &cache_level);
    2249                 :            : 
    2250                 :          0 :         return cache_level;
    2251                 :            : }
    2252                 :            : 
    2253                 :            : /**
    2254                 :            :  * of_map_rid - Translate a requester ID through a downstream mapping.
    2255                 :            :  * @np: root complex device node.
    2256                 :            :  * @rid: device requester ID to map.
    2257                 :            :  * @map_name: property name of the map to use.
    2258                 :            :  * @map_mask_name: optional property name of the mask to use.
    2259                 :            :  * @target: optional pointer to a target device node.
    2260                 :            :  * @id_out: optional pointer to receive the translated ID.
    2261                 :            :  *
    2262                 :            :  * Given a device requester ID, look up the appropriate implementation-defined
    2263                 :            :  * platform ID and/or the target device which receives transactions on that
    2264                 :            :  * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
    2265                 :            :  * @id_out may be NULL if only the other is required. If @target points to
    2266                 :            :  * a non-NULL device node pointer, only entries targeting that node will be
    2267                 :            :  * matched; if it points to a NULL value, it will receive the device node of
    2268                 :            :  * the first matching target phandle, with a reference held.
    2269                 :            :  *
    2270                 :            :  * Return: 0 on success or a standard error code on failure.
    2271                 :            :  */
    2272                 :          0 : int of_map_rid(struct device_node *np, u32 rid,
    2273                 :            :                const char *map_name, const char *map_mask_name,
    2274                 :            :                struct device_node **target, u32 *id_out)
    2275                 :            : {
    2276                 :            :         u32 map_mask, masked_rid;
    2277                 :            :         int map_len;
    2278                 :            :         const __be32 *map = NULL;
    2279                 :            : 
    2280                 :          0 :         if (!np || !map_name || (!target && !id_out))
    2281                 :            :                 return -EINVAL;
    2282                 :            : 
    2283                 :            :         map = of_get_property(np, map_name, &map_len);
    2284                 :          0 :         if (!map) {
    2285                 :          0 :                 if (target)
    2286                 :            :                         return -ENODEV;
    2287                 :            :                 /* Otherwise, no map implies no translation */
    2288                 :          0 :                 *id_out = rid;
    2289                 :          0 :                 return 0;
    2290                 :            :         }
    2291                 :            : 
    2292                 :          0 :         if (!map_len || map_len % (4 * sizeof(*map))) {
    2293                 :          0 :                 pr_err("%pOF: Error: Bad %s length: %d\n", np,
    2294                 :            :                         map_name, map_len);
    2295                 :          0 :                 return -EINVAL;
    2296                 :            :         }
    2297                 :            : 
    2298                 :            :         /* The default is to select all bits. */
    2299                 :          0 :         map_mask = 0xffffffff;
    2300                 :            : 
    2301                 :            :         /*
    2302                 :            :          * Can be overridden by "{iommu,msi}-map-mask" property.
    2303                 :            :          * If of_property_read_u32() fails, the default is used.
    2304                 :            :          */
    2305                 :          0 :         if (map_mask_name)
    2306                 :            :                 of_property_read_u32(np, map_mask_name, &map_mask);
    2307                 :            : 
    2308                 :          0 :         masked_rid = map_mask & rid;
    2309                 :          0 :         for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) {
    2310                 :            :                 struct device_node *phandle_node;
    2311                 :            :                 u32 rid_base = be32_to_cpup(map + 0);
    2312                 :            :                 u32 phandle = be32_to_cpup(map + 1);
    2313                 :            :                 u32 out_base = be32_to_cpup(map + 2);
    2314                 :            :                 u32 rid_len = be32_to_cpup(map + 3);
    2315                 :            : 
    2316                 :          0 :                 if (rid_base & ~map_mask) {
    2317                 :          0 :                         pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n",
    2318                 :            :                                 np, map_name, map_name,
    2319                 :            :                                 map_mask, rid_base);
    2320                 :          0 :                         return -EFAULT;
    2321                 :            :                 }
    2322                 :            : 
    2323                 :          0 :                 if (masked_rid < rid_base || masked_rid >= rid_base + rid_len)
    2324                 :          0 :                         continue;
    2325                 :            : 
    2326                 :          0 :                 phandle_node = of_find_node_by_phandle(phandle);
    2327                 :          0 :                 if (!phandle_node)
    2328                 :            :                         return -ENODEV;
    2329                 :            : 
    2330                 :          0 :                 if (target) {
    2331                 :          0 :                         if (*target)
    2332                 :          0 :                                 of_node_put(phandle_node);
    2333                 :            :                         else
    2334                 :          0 :                                 *target = phandle_node;
    2335                 :            : 
    2336                 :          0 :                         if (*target != phandle_node)
    2337                 :          0 :                                 continue;
    2338                 :            :                 }
    2339                 :            : 
    2340                 :          0 :                 if (id_out)
    2341                 :          0 :                         *id_out = masked_rid - rid_base + out_base;
    2342                 :            : 
    2343                 :            :                 pr_debug("%pOF: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n",
    2344                 :            :                         np, map_name, map_mask, rid_base, out_base,
    2345                 :            :                         rid_len, rid, masked_rid - rid_base + out_base);
    2346                 :            :                 return 0;
    2347                 :            :         }
    2348                 :            : 
    2349                 :          0 :         pr_info("%pOF: no %s translation for rid 0x%x on %pOF\n", np, map_name,
    2350                 :            :                 rid, target && *target ? *target : NULL);
    2351                 :            : 
    2352                 :            :         /* Bypasses translation */
    2353                 :          0 :         if (id_out)
    2354                 :          0 :                 *id_out = rid;
    2355                 :            :         return 0;
    2356                 :            : }
    2357                 :            : EXPORT_SYMBOL_GPL(of_map_rid);
    

Generated by: LCOV version 1.14