Rob Herring | af6074f | 2017-12-27 12:55:14 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 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 | * |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 13 | * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and |
| 14 | * Grant Likely. |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 15 | */ |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 16 | |
| 17 | #define pr_fmt(fmt) "OF: " fmt |
| 18 | |
Michal Simek | b1078c3 | 2018-09-20 13:41:52 +0200 | [diff] [blame] | 19 | #include <linux/bitmap.h> |
Grant Likely | 3482f2c | 2014-03-27 17:18:55 -0700 | [diff] [blame] | 20 | #include <linux/console.h> |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 21 | #include <linux/ctype.h> |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 22 | #include <linux/cpu.h> |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/of.h> |
Sudeep Holla | 5fa2353 | 2017-01-16 10:40:43 +0000 | [diff] [blame] | 25 | #include <linux/of_device.h> |
Philipp Zabel | fd9fdb7 | 2014-02-10 22:01:48 +0100 | [diff] [blame] | 26 | #include <linux/of_graph.h> |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 27 | #include <linux/spinlock.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 29 | #include <linux/string.h> |
Jeremy Kerr | a9f2f63 | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 30 | #include <linux/proc_fs.h> |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 31 | |
Stepan Moskovchenko | ced4eec | 2012-12-06 14:55:41 -0800 | [diff] [blame] | 32 | #include "of_private.h" |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 33 | |
Stepan Moskovchenko | ced4eec | 2012-12-06 14:55:41 -0800 | [diff] [blame] | 34 | LIST_HEAD(aliases_lookup); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 35 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 36 | struct device_node *of_root; |
| 37 | EXPORT_SYMBOL(of_root); |
Grant Likely | fc0bdae | 2010-02-14 07:13:55 -0700 | [diff] [blame] | 38 | struct device_node *of_chosen; |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 39 | struct device_node *of_aliases; |
Grant Likely | a752ee5 | 2014-03-28 08:12:18 -0700 | [diff] [blame] | 40 | struct device_node *of_stdout; |
Leif Lindholm | 7914a7c | 2014-11-27 17:56:07 +0000 | [diff] [blame] | 41 | static const char *of_stdout_options; |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 42 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 43 | struct kset *of_kset; |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 44 | |
| 45 | /* |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 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. |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 50 | */ |
Pantelis Antoniou | c05aba2 | 2014-07-04 19:58:03 +0300 | [diff] [blame] | 51 | DEFINE_MUTEX(of_mutex); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 52 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 53 | /* use when traversing tree through the child, sibling, |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 54 | * or parent members of struct device_node. |
| 55 | */ |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 56 | DEFINE_RAW_SPINLOCK(devtree_lock); |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 57 | |
Rob Herring | f42b0e18 | 2018-08-27 07:50:47 -0500 | [diff] [blame] | 58 | 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 | if (!np) |
| 64 | return false; |
| 65 | |
| 66 | node_name = kbasename(np->full_name); |
| 67 | len = strchrnul(node_name, '@') - node_name; |
| 68 | |
| 69 | return (strlen(name) == len) && (strncmp(node_name, name, len) == 0); |
| 70 | } |
Rob Herring | 173ee39 | 2018-10-22 09:03:54 -0500 | [diff] [blame] | 71 | EXPORT_SYMBOL(of_node_name_eq); |
Rob Herring | f42b0e18 | 2018-08-27 07:50:47 -0500 | [diff] [blame] | 72 | |
| 73 | bool of_node_name_prefix(const struct device_node *np, const char *prefix) |
| 74 | { |
| 75 | if (!np) |
| 76 | return false; |
| 77 | |
| 78 | return strncmp(kbasename(np->full_name), prefix, strlen(prefix)) == 0; |
| 79 | } |
Rob Herring | 173ee39 | 2018-10-22 09:03:54 -0500 | [diff] [blame] | 80 | EXPORT_SYMBOL(of_node_name_prefix); |
Rob Herring | f42b0e18 | 2018-08-27 07:50:47 -0500 | [diff] [blame] | 81 | |
Rob Herring | e8b1dee2 | 2018-08-29 08:36:12 -0500 | [diff] [blame] | 82 | 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 | return np && match && type && !strcmp(match, type); |
| 87 | } |
| 88 | |
Robin Murphy | b68ac8d | 2019-07-02 18:42:39 +0100 | [diff] [blame^] | 89 | int of_bus_n_addr_cells(struct device_node *np) |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 90 | { |
Sergei Shtylyov | 8832963 | 2017-07-23 19:55:47 +0300 | [diff] [blame] | 91 | u32 cells; |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 92 | |
Robin Murphy | b68ac8d | 2019-07-02 18:42:39 +0100 | [diff] [blame^] | 93 | for (; np; np = np->parent) |
Sergei Shtylyov | 8832963 | 2017-07-23 19:55:47 +0300 | [diff] [blame] | 94 | if (!of_property_read_u32(np, "#address-cells", &cells)) |
| 95 | return cells; |
Robin Murphy | b68ac8d | 2019-07-02 18:42:39 +0100 | [diff] [blame^] | 96 | |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 97 | /* No #address-cells property for the root node */ |
| 98 | return OF_ROOT_NODE_ADDR_CELLS_DEFAULT; |
| 99 | } |
Robin Murphy | b68ac8d | 2019-07-02 18:42:39 +0100 | [diff] [blame^] | 100 | |
| 101 | int of_n_addr_cells(struct device_node *np) |
| 102 | { |
| 103 | if (np->parent) |
| 104 | np = np->parent; |
| 105 | |
| 106 | return of_bus_n_addr_cells(np); |
| 107 | } |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 108 | EXPORT_SYMBOL(of_n_addr_cells); |
| 109 | |
Robin Murphy | b68ac8d | 2019-07-02 18:42:39 +0100 | [diff] [blame^] | 110 | int of_bus_n_size_cells(struct device_node *np) |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 111 | { |
Sergei Shtylyov | 8832963 | 2017-07-23 19:55:47 +0300 | [diff] [blame] | 112 | u32 cells; |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 113 | |
Robin Murphy | b68ac8d | 2019-07-02 18:42:39 +0100 | [diff] [blame^] | 114 | for (; np; np = np->parent) |
Sergei Shtylyov | 8832963 | 2017-07-23 19:55:47 +0300 | [diff] [blame] | 115 | if (!of_property_read_u32(np, "#size-cells", &cells)) |
| 116 | return cells; |
Robin Murphy | b68ac8d | 2019-07-02 18:42:39 +0100 | [diff] [blame^] | 117 | |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 118 | /* No #size-cells property for the root node */ |
| 119 | return OF_ROOT_NODE_SIZE_CELLS_DEFAULT; |
| 120 | } |
Robin Murphy | b68ac8d | 2019-07-02 18:42:39 +0100 | [diff] [blame^] | 121 | |
| 122 | int of_n_size_cells(struct device_node *np) |
| 123 | { |
| 124 | if (np->parent) |
| 125 | np = np->parent; |
| 126 | |
| 127 | return of_bus_n_size_cells(np); |
| 128 | } |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 129 | EXPORT_SYMBOL(of_n_size_cells); |
| 130 | |
Rob Herring | 0c3f061 | 2013-09-17 10:42:50 -0500 | [diff] [blame] | 131 | #ifdef CONFIG_NUMA |
| 132 | int __weak of_node_to_nid(struct device_node *np) |
| 133 | { |
Konstantin Khlebnikov | c8fff7b | 2015-04-08 19:59:20 +0300 | [diff] [blame] | 134 | return NUMA_NO_NODE; |
Rob Herring | 0c3f061 | 2013-09-17 10:42:50 -0500 | [diff] [blame] | 135 | } |
| 136 | #endif |
| 137 | |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 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 | */ |
Frank Rowand | b8a9ac1 | 2018-12-18 11:40:02 -0800 | [diff] [blame] | 146 | |
| 147 | static struct device_node **phandle_cache; |
| 148 | static u32 phandle_cache_mask; |
| 149 | |
| 150 | /* |
| 151 | * Caller must hold devtree_lock. |
| 152 | */ |
| 153 | static void __of_free_phandle_cache(void) |
| 154 | { |
| 155 | u32 cache_entries = phandle_cache_mask + 1; |
| 156 | u32 k; |
| 157 | |
| 158 | if (!phandle_cache) |
| 159 | return; |
| 160 | |
| 161 | for (k = 0; k < cache_entries; k++) |
| 162 | of_node_put(phandle_cache[k]); |
| 163 | |
| 164 | kfree(phandle_cache); |
| 165 | phandle_cache = NULL; |
| 166 | } |
| 167 | |
| 168 | int of_free_phandle_cache(void) |
| 169 | { |
| 170 | unsigned long flags; |
| 171 | |
| 172 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 173 | |
| 174 | __of_free_phandle_cache(); |
| 175 | |
| 176 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | #if !defined(CONFIG_MODULES) |
| 181 | late_initcall_sync(of_free_phandle_cache); |
| 182 | #endif |
| 183 | |
Frank Rowand | 5801169 | 2018-12-18 11:40:03 -0800 | [diff] [blame] | 184 | /* |
| 185 | * Caller must hold devtree_lock. |
| 186 | */ |
| 187 | void __of_free_phandle_cache_entry(phandle handle) |
| 188 | { |
| 189 | phandle masked_handle; |
| 190 | struct device_node *np; |
| 191 | |
| 192 | if (!handle) |
| 193 | return; |
| 194 | |
| 195 | masked_handle = handle & phandle_cache_mask; |
| 196 | |
| 197 | if (phandle_cache) { |
| 198 | np = phandle_cache[masked_handle]; |
| 199 | if (np && handle == np->phandle) { |
| 200 | of_node_put(np); |
| 201 | phandle_cache[masked_handle] = NULL; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
Frank Rowand | b9952b5 | 2018-07-12 14:00:07 -0700 | [diff] [blame] | 206 | void of_populate_phandle_cache(void) |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 207 | { |
| 208 | unsigned long flags; |
| 209 | u32 cache_entries; |
| 210 | struct device_node *np; |
| 211 | u32 phandles = 0; |
| 212 | |
| 213 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 214 | |
Frank Rowand | b8a9ac1 | 2018-12-18 11:40:02 -0800 | [diff] [blame] | 215 | __of_free_phandle_cache(); |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 216 | |
| 217 | for_each_of_allnodes(np) |
| 218 | if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL) |
| 219 | phandles++; |
| 220 | |
Rob Herring | e54192b | 2018-09-11 09:28:14 -0500 | [diff] [blame] | 221 | if (!phandles) |
| 222 | goto out; |
| 223 | |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 224 | cache_entries = roundup_pow_of_two(phandles); |
| 225 | phandle_cache_mask = cache_entries - 1; |
| 226 | |
| 227 | phandle_cache = kcalloc(cache_entries, sizeof(*phandle_cache), |
| 228 | GFP_ATOMIC); |
| 229 | if (!phandle_cache) |
| 230 | goto out; |
| 231 | |
| 232 | for_each_of_allnodes(np) |
Frank Rowand | b8a9ac1 | 2018-12-18 11:40:02 -0800 | [diff] [blame] | 233 | if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL) { |
| 234 | of_node_get(np); |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 235 | phandle_cache[np->phandle & phandle_cache_mask] = np; |
Frank Rowand | b8a9ac1 | 2018-12-18 11:40:02 -0800 | [diff] [blame] | 236 | } |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 237 | |
| 238 | out: |
| 239 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 240 | } |
| 241 | |
Sudeep Holla | 194ec93 | 2015-05-14 15:28:24 +0100 | [diff] [blame] | 242 | void __init of_core_init(void) |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 243 | { |
| 244 | struct device_node *np; |
| 245 | |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 246 | of_populate_phandle_cache(); |
| 247 | |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 248 | /* Create the kset, and register existing nodes */ |
Pantelis Antoniou | c05aba2 | 2014-07-04 19:58:03 +0300 | [diff] [blame] | 249 | mutex_lock(&of_mutex); |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 250 | of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj); |
| 251 | if (!of_kset) { |
Pantelis Antoniou | c05aba2 | 2014-07-04 19:58:03 +0300 | [diff] [blame] | 252 | mutex_unlock(&of_mutex); |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 253 | pr_err("failed to register existing nodes\n"); |
Sudeep Holla | 194ec93 | 2015-05-14 15:28:24 +0100 | [diff] [blame] | 254 | return; |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 255 | } |
| 256 | for_each_of_allnodes(np) |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 257 | __of_attach_node_sysfs(np); |
Pantelis Antoniou | c05aba2 | 2014-07-04 19:58:03 +0300 | [diff] [blame] | 258 | mutex_unlock(&of_mutex); |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 259 | |
Grant Likely | 8357041 | 2012-11-06 21:03:27 +0000 | [diff] [blame] | 260 | /* Symlink in /proc as required by userspace ABI */ |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 261 | if (of_root) |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 262 | proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base"); |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 263 | } |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 264 | |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 265 | static struct property *__of_find_property(const struct device_node *np, |
| 266 | const char *name, int *lenp) |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 267 | { |
| 268 | struct property *pp; |
| 269 | |
Timur Tabi | 64e4566 | 2008-05-08 05:19:59 +1000 | [diff] [blame] | 270 | if (!np) |
| 271 | return NULL; |
| 272 | |
Sachin Kamat | a3a7cab | 2012-06-27 09:44:45 +0530 | [diff] [blame] | 273 | for (pp = np->properties; pp; pp = pp->next) { |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 274 | if (of_prop_cmp(pp->name, name) == 0) { |
Sachin Kamat | a3a7cab | 2012-06-27 09:44:45 +0530 | [diff] [blame] | 275 | if (lenp) |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 276 | *lenp = pp->length; |
| 277 | break; |
| 278 | } |
| 279 | } |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 280 | |
| 281 | return pp; |
| 282 | } |
| 283 | |
| 284 | struct property *of_find_property(const struct device_node *np, |
| 285 | const char *name, |
| 286 | int *lenp) |
| 287 | { |
| 288 | struct property *pp; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 289 | unsigned long flags; |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 290 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 291 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 292 | pp = __of_find_property(np, name, lenp); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 293 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 294 | |
| 295 | return pp; |
| 296 | } |
| 297 | EXPORT_SYMBOL(of_find_property); |
| 298 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 299 | struct device_node *__of_find_all_nodes(struct device_node *prev) |
| 300 | { |
| 301 | struct device_node *np; |
| 302 | if (!prev) { |
| 303 | np = of_root; |
| 304 | } 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 | while (np->parent && !np->sibling) |
| 310 | np = np->parent; |
| 311 | np = np->sibling; /* Might be null at the end of the tree */ |
| 312 | } |
| 313 | return np; |
| 314 | } |
| 315 | |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 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 | struct device_node *of_find_all_nodes(struct device_node *prev) |
| 325 | { |
| 326 | struct device_node *np; |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 327 | unsigned long flags; |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 328 | |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 329 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 330 | np = __of_find_all_nodes(prev); |
| 331 | of_node_get(np); |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 332 | of_node_put(prev); |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 333 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 334 | return np; |
| 335 | } |
| 336 | EXPORT_SYMBOL(of_find_all_nodes); |
| 337 | |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 338 | /* |
| 339 | * Find a property with a given name for a given node |
| 340 | * and return the value. |
| 341 | */ |
Grant Likely | a25095d | 2014-07-15 23:25:43 -0600 | [diff] [blame] | 342 | const void *__of_get_property(const struct device_node *np, |
| 343 | const char *name, int *lenp) |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 344 | { |
| 345 | struct property *pp = __of_find_property(np, name, lenp); |
| 346 | |
| 347 | 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 | */ |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 354 | const void *of_get_property(const struct device_node *np, const char *name, |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 355 | int *lenp) |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 356 | { |
| 357 | struct property *pp = of_find_property(np, name, lenp); |
| 358 | |
| 359 | return pp ? pp->value : NULL; |
| 360 | } |
| 361 | EXPORT_SYMBOL(of_get_property); |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 362 | |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 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 | bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id) |
| 378 | { |
| 379 | 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 | 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); |
Rob Herring | 6487c15 | 2018-08-27 13:46:51 -0500 | [diff] [blame] | 396 | if (!cell && !ac && arch_match_cpu_phys_id(cpu, 0)) |
| 397 | return true; |
Grant Likely | f3cea45 | 2013-10-04 17:24:26 +0100 | [diff] [blame] | 398 | if (!cell || !ac) |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 399 | return false; |
Grant Likely | f3cea45 | 2013-10-04 17:24:26 +0100 | [diff] [blame] | 400 | prop_len /= sizeof(*cell) * ac; |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 401 | for (tid = 0; tid < prop_len; tid++) { |
| 402 | hwid = of_read_number(cell, ac); |
| 403 | if (arch_match_cpu_phys_id(cpu, hwid)) { |
| 404 | if (thread) |
| 405 | *thread = tid; |
| 406 | return true; |
| 407 | } |
| 408 | cell += ac; |
| 409 | } |
| 410 | return false; |
| 411 | } |
| 412 | |
David Miller | d1cb9d1 | 2013-10-03 17:24:51 -0400 | [diff] [blame] | 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 | 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 | |
Masahiro Yamada | 510bd06 | 2015-10-28 12:05:27 +0900 | [diff] [blame] | 432 | return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread); |
David Miller | d1cb9d1 | 2013-10-03 17:24:51 -0400 | [diff] [blame] | 433 | } |
| 434 | |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 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 | * |
Masahiro Yamada | 1c986e3 | 2016-04-20 10:18:46 +0900 | [diff] [blame] | 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. |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 453 | */ |
| 454 | struct device_node *of_get_cpu_node(int cpu, unsigned int *thread) |
| 455 | { |
David Miller | d1cb9d1 | 2013-10-03 17:24:51 -0400 | [diff] [blame] | 456 | struct device_node *cpun; |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 457 | |
Rob Herring | 651d44f | 2018-08-27 09:47:00 -0500 | [diff] [blame] | 458 | for_each_of_cpu_node(cpun) { |
David Miller | d1cb9d1 | 2013-10-03 17:24:51 -0400 | [diff] [blame] | 459 | if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread)) |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 460 | return cpun; |
| 461 | } |
| 462 | return NULL; |
| 463 | } |
| 464 | EXPORT_SYMBOL(of_get_cpu_node); |
| 465 | |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 466 | /** |
Suzuki K Poulose | a0e71cd | 2018-01-02 11:25:27 +0000 | [diff] [blame] | 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 | 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 | for_each_possible_cpu(cpu) { |
| 481 | np = of_cpu_device_node_get(cpu); |
| 482 | found = (cpu_node == np); |
| 483 | of_node_put(np); |
| 484 | if (found) |
| 485 | return cpu; |
| 486 | } |
| 487 | |
| 488 | return -ENODEV; |
| 489 | } |
| 490 | EXPORT_SYMBOL(of_cpu_node_to_id); |
| 491 | |
| 492 | /** |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 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 |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 521 | */ |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 522 | static int __of_device_is_compatible(const struct device_node *device, |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 523 | const char *compat, const char *type, const char *name) |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 524 | { |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 525 | struct property *prop; |
| 526 | const char *cp; |
| 527 | int index = 0, score = 0; |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 528 | |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 529 | /* Compatible match has highest priority */ |
| 530 | if (compat && compat[0]) { |
| 531 | prop = __of_find_property(device, "compatible", NULL); |
| 532 | for (cp = of_prop_next_string(prop, NULL); cp; |
| 533 | cp = of_prop_next_string(prop, cp), index++) { |
| 534 | if (of_compat_cmp(cp, compat, strlen(compat)) == 0) { |
| 535 | score = INT_MAX/2 - (index << 2); |
| 536 | break; |
| 537 | } |
| 538 | } |
| 539 | if (!score) |
| 540 | return 0; |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 541 | } |
| 542 | |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 543 | /* Matching type is better than matching name */ |
| 544 | if (type && type[0]) { |
Rob Herring | e8b1dee2 | 2018-08-29 08:36:12 -0500 | [diff] [blame] | 545 | if (!__of_node_is_type(device, type)) |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 546 | return 0; |
| 547 | score += 2; |
| 548 | } |
| 549 | |
| 550 | /* Matching name is a bit better than not */ |
| 551 | if (name && name[0]) { |
Rob Herring | b3e46d1 | 2018-08-27 08:37:06 -0500 | [diff] [blame] | 552 | if (!of_node_name_eq(device, name)) |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 553 | return 0; |
| 554 | score++; |
| 555 | } |
| 556 | |
| 557 | return score; |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 558 | } |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 559 | |
| 560 | /** Checks if the given "compat" string matches one of the strings in |
| 561 | * the device's "compatible" property |
| 562 | */ |
| 563 | int of_device_is_compatible(const struct device_node *device, |
| 564 | const char *compat) |
| 565 | { |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 566 | unsigned long flags; |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 567 | int res; |
| 568 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 569 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 570 | res = __of_device_is_compatible(device, compat, NULL, NULL); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 571 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 572 | return res; |
| 573 | } |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 574 | EXPORT_SYMBOL(of_device_is_compatible); |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 575 | |
Benjamin Herrenschmidt | b9c13fe3 | 2016-07-08 08:35:59 +1000 | [diff] [blame] | 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 | int of_device_compatible_match(struct device_node *device, |
| 581 | const char *const *compat) |
| 582 | { |
| 583 | unsigned int tmp, score = 0; |
| 584 | |
| 585 | if (!compat) |
| 586 | return 0; |
| 587 | |
| 588 | while (*compat) { |
| 589 | tmp = of_device_is_compatible(device, *compat); |
| 590 | if (tmp > score) |
| 591 | score = tmp; |
| 592 | compat++; |
| 593 | } |
| 594 | |
| 595 | return score; |
| 596 | } |
| 597 | |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 598 | /** |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 599 | * of_machine_is_compatible - Test root of device tree for a given compatible value |
Grant Likely | 1f43cfb | 2010-01-28 13:47:25 -0700 | [diff] [blame] | 600 | * @compat: compatible string to look for in root node's compatible property. |
| 601 | * |
Kevin Cernekee | 25c7a1d | 2014-11-12 12:54:00 -0800 | [diff] [blame] | 602 | * Returns a positive integer if the root node has the given value in its |
Grant Likely | 1f43cfb | 2010-01-28 13:47:25 -0700 | [diff] [blame] | 603 | * compatible property. |
| 604 | */ |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 605 | int of_machine_is_compatible(const char *compat) |
Grant Likely | 1f43cfb | 2010-01-28 13:47:25 -0700 | [diff] [blame] | 606 | { |
| 607 | struct device_node *root; |
| 608 | int rc = 0; |
| 609 | |
| 610 | root = of_find_node_by_path("/"); |
| 611 | if (root) { |
| 612 | rc = of_device_is_compatible(root, compat); |
| 613 | of_node_put(root); |
| 614 | } |
| 615 | return rc; |
| 616 | } |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 617 | EXPORT_SYMBOL(of_machine_is_compatible); |
Grant Likely | 1f43cfb | 2010-01-28 13:47:25 -0700 | [diff] [blame] | 618 | |
| 619 | /** |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 620 | * __of_device_is_available - check if a device is available for use |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 621 | * |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 622 | * @device: Node to check for availability, with locks already held |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 623 | * |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 624 | * Returns true if the status property is absent or set to "okay" or "ok", |
| 625 | * false otherwise |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 626 | */ |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 627 | static bool __of_device_is_available(const struct device_node *device) |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 628 | { |
| 629 | const char *status; |
| 630 | int statlen; |
| 631 | |
Xiubo Li | 42ccd78 | 2014-01-13 11:07:28 +0800 | [diff] [blame] | 632 | if (!device) |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 633 | return false; |
Xiubo Li | 42ccd78 | 2014-01-13 11:07:28 +0800 | [diff] [blame] | 634 | |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 635 | status = __of_get_property(device, "status", &statlen); |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 636 | if (status == NULL) |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 637 | return true; |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 638 | |
| 639 | if (statlen > 0) { |
| 640 | if (!strcmp(status, "okay") || !strcmp(status, "ok")) |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 641 | return true; |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 642 | } |
| 643 | |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 644 | return false; |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 645 | } |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 646 | |
| 647 | /** |
| 648 | * of_device_is_available - check if a device is available for use |
| 649 | * |
| 650 | * @device: Node to check for availability |
| 651 | * |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 652 | * Returns true if the status property is absent or set to "okay" or "ok", |
| 653 | * false otherwise |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 654 | */ |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 655 | bool of_device_is_available(const struct device_node *device) |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 656 | { |
| 657 | unsigned long flags; |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 658 | bool res; |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 659 | |
| 660 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 661 | res = __of_device_is_available(device); |
| 662 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 663 | return res; |
| 664 | |
| 665 | } |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 666 | EXPORT_SYMBOL(of_device_is_available); |
| 667 | |
| 668 | /** |
Kevin Cernekee | 37786c7 | 2015-04-09 13:05:14 -0700 | [diff] [blame] | 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 | bool of_device_is_big_endian(const struct device_node *device) |
| 681 | { |
| 682 | 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 | return false; |
| 688 | } |
| 689 | EXPORT_SYMBOL(of_device_is_big_endian); |
| 690 | |
| 691 | /** |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 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 | struct device_node *of_get_parent(const struct device_node *node) |
| 699 | { |
| 700 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 701 | unsigned long flags; |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 702 | |
| 703 | if (!node) |
| 704 | return NULL; |
| 705 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 706 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 707 | np = of_node_get(node->parent); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 708 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 709 | return np; |
| 710 | } |
| 711 | EXPORT_SYMBOL(of_get_parent); |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 712 | |
| 713 | /** |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 714 | * of_get_next_parent - Iterate to a node's parent |
| 715 | * @node: Node to get parent of |
| 716 | * |
Geert Uytterhoeven | c0e848d | 2014-10-22 11:44:55 +0200 | [diff] [blame] | 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. |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 720 | * |
| 721 | * Returns a node pointer with refcount incremented, use |
| 722 | * of_node_put() on it when done. |
| 723 | */ |
| 724 | struct device_node *of_get_next_parent(struct device_node *node) |
| 725 | { |
| 726 | struct device_node *parent; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 727 | unsigned long flags; |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 728 | |
| 729 | if (!node) |
| 730 | return NULL; |
| 731 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 732 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 733 | parent = of_node_get(node->parent); |
| 734 | of_node_put(node); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 735 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 736 | return parent; |
| 737 | } |
Guennadi Liakhovetski | 6695be6 | 2013-04-02 12:28:11 -0300 | [diff] [blame] | 738 | EXPORT_SYMBOL(of_get_next_parent); |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 739 | |
Grant Likely | 0d0e02d | 2014-05-22 01:04:17 +0900 | [diff] [blame] | 740 | 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 | |
Florian Fainelli | 43cb436 | 2014-05-28 10:39:02 -0700 | [diff] [blame] | 745 | if (!node) |
| 746 | return NULL; |
| 747 | |
Grant Likely | 0d0e02d | 2014-05-22 01:04:17 +0900 | [diff] [blame] | 748 | next = prev ? prev->sibling : node->child; |
| 749 | for (; next; next = next->sibling) |
| 750 | if (of_node_get(next)) |
| 751 | break; |
| 752 | of_node_put(prev); |
| 753 | 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 | |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 759 | /** |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 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 | * |
Baruch Siach | 6480827 | 2015-03-19 14:03:41 +0200 | [diff] [blame] | 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. |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 767 | */ |
| 768 | struct device_node *of_get_next_child(const struct device_node *node, |
| 769 | struct device_node *prev) |
| 770 | { |
| 771 | struct device_node *next; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 772 | unsigned long flags; |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 773 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 774 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 0d0e02d | 2014-05-22 01:04:17 +0900 | [diff] [blame] | 775 | next = __of_get_next_child(node, prev); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 776 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 777 | return next; |
| 778 | } |
| 779 | EXPORT_SYMBOL(of_get_next_child); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 780 | |
| 781 | /** |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 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 | struct device_node *of_get_next_available_child(const struct device_node *node, |
| 790 | struct device_node *prev) |
| 791 | { |
| 792 | struct device_node *next; |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 793 | unsigned long flags; |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 794 | |
Florian Fainelli | 43cb436 | 2014-05-28 10:39:02 -0700 | [diff] [blame] | 795 | if (!node) |
| 796 | return NULL; |
| 797 | |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 798 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 799 | next = prev ? prev->sibling : node->child; |
| 800 | for (; next; next = next->sibling) { |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 801 | if (!__of_device_is_available(next)) |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 802 | continue; |
| 803 | if (of_node_get(next)) |
| 804 | break; |
| 805 | } |
| 806 | of_node_put(prev); |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 807 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 808 | return next; |
| 809 | } |
| 810 | EXPORT_SYMBOL(of_get_next_available_child); |
| 811 | |
| 812 | /** |
Rob Herring | f1f207e | 2018-08-22 15:04:40 -0500 | [diff] [blame] | 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 | 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 | if (!prev) |
| 827 | node = of_find_node_by_path("/cpus"); |
| 828 | |
| 829 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 830 | if (prev) |
| 831 | next = prev->sibling; |
| 832 | else if (node) { |
| 833 | next = node->child; |
| 834 | of_node_put(node); |
| 835 | } |
| 836 | for (; next; next = next->sibling) { |
| 837 | if (!(of_node_name_eq(next, "cpu") || |
Rob Herring | e8b1dee2 | 2018-08-29 08:36:12 -0500 | [diff] [blame] | 838 | __of_node_is_type(next, "cpu"))) |
Rob Herring | f1f207e | 2018-08-22 15:04:40 -0500 | [diff] [blame] | 839 | continue; |
Rob Herring | f1f207e | 2018-08-22 15:04:40 -0500 | [diff] [blame] | 840 | if (of_node_get(next)) |
| 841 | break; |
| 842 | } |
| 843 | of_node_put(prev); |
| 844 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 845 | return next; |
| 846 | } |
| 847 | EXPORT_SYMBOL(of_get_next_cpu_node); |
| 848 | |
| 849 | /** |
Johan Hovold | 36156f9 | 2018-08-27 10:21:45 +0200 | [diff] [blame] | 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 | struct device_node *of_get_compatible_child(const struct device_node *parent, |
| 861 | const char *compatible) |
| 862 | { |
| 863 | struct device_node *child; |
| 864 | |
| 865 | for_each_child_of_node(parent, child) { |
| 866 | if (of_device_is_compatible(child, compatible)) |
| 867 | break; |
| 868 | } |
| 869 | |
| 870 | return child; |
| 871 | } |
| 872 | EXPORT_SYMBOL(of_get_compatible_child); |
| 873 | |
| 874 | /** |
Srinivas Kandagatla | 9c19761 | 2012-09-18 08:10:28 +0100 | [diff] [blame] | 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 | 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 | for_each_child_of_node(node, child) |
Rob Herring | b3e46d1 | 2018-08-27 08:37:06 -0500 | [diff] [blame] | 891 | if (of_node_name_eq(child, name)) |
Srinivas Kandagatla | 9c19761 | 2012-09-18 08:10:28 +0100 | [diff] [blame] | 892 | break; |
| 893 | return child; |
| 894 | } |
| 895 | EXPORT_SYMBOL(of_get_child_by_name); |
| 896 | |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 897 | struct device_node *__of_find_node_by_path(struct device_node *parent, |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 898 | const char *path) |
| 899 | { |
| 900 | struct device_node *child; |
Leif Lindholm | 106937e | 2015-03-06 16:52:53 +0000 | [diff] [blame] | 901 | int len; |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 902 | |
Brian Norris | 721a09e | 2015-03-17 12:30:31 -0700 | [diff] [blame] | 903 | len = strcspn(path, "/:"); |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 904 | if (!len) |
| 905 | return NULL; |
| 906 | |
| 907 | __for_each_child_of_node(parent, child) { |
Rob Herring | 95e6b1f | 2017-06-01 18:00:00 -0500 | [diff] [blame] | 908 | const char *name = kbasename(child->full_name); |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 909 | if (strncmp(path, name, len) == 0 && (strlen(name) == len)) |
| 910 | return child; |
| 911 | } |
| 912 | return NULL; |
| 913 | } |
| 914 | |
Rob Herring | 27497e1 | 2017-06-02 12:43:18 -0500 | [diff] [blame] | 915 | struct device_node *__of_find_node_by_full_path(struct device_node *node, |
| 916 | const char *path) |
| 917 | { |
| 918 | const char *separator = strchr(path, ':'); |
| 919 | |
| 920 | while (node && *path == '/') { |
| 921 | struct device_node *tmp = node; |
| 922 | |
| 923 | path++; /* Increment past '/' delimiter */ |
| 924 | node = __of_find_node_by_path(node, path); |
| 925 | of_node_put(tmp); |
| 926 | path = strchrnul(path, '/'); |
| 927 | if (separator && separator < path) |
| 928 | break; |
| 929 | } |
| 930 | return node; |
| 931 | } |
| 932 | |
Srinivas Kandagatla | 9c19761 | 2012-09-18 08:10:28 +0100 | [diff] [blame] | 933 | /** |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 934 | * of_find_node_opts_by_path - Find a node matching a full OF path |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 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. |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 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. |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 942 | * |
| 943 | * Valid paths: |
| 944 | * /foo/bar Full path |
| 945 | * foo Valid alias |
| 946 | * foo/bar Valid alias + relative path |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 947 | * |
| 948 | * Returns a node pointer with refcount incremented, use |
| 949 | * of_node_put() on it when done. |
| 950 | */ |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 951 | struct device_node *of_find_node_opts_by_path(const char *path, const char **opts) |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 952 | { |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 953 | struct device_node *np = NULL; |
| 954 | struct property *pp; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 955 | unsigned long flags; |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 956 | const char *separator = strchr(path, ':'); |
| 957 | |
| 958 | if (opts) |
| 959 | *opts = separator ? separator + 1 : NULL; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 960 | |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 961 | if (strcmp(path, "/") == 0) |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 962 | return of_node_get(of_root); |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 963 | |
| 964 | /* The path could begin with an alias */ |
| 965 | if (*path != '/') { |
Leif Lindholm | 106937e | 2015-03-06 16:52:53 +0000 | [diff] [blame] | 966 | int len; |
| 967 | const char *p = separator; |
| 968 | |
| 969 | if (!p) |
| 970 | p = strchrnul(path, '/'); |
| 971 | len = p - path; |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 972 | |
| 973 | /* of_aliases must not be NULL */ |
| 974 | if (!of_aliases) |
| 975 | return NULL; |
| 976 | |
| 977 | for_each_property_of_node(of_aliases, pp) { |
| 978 | if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) { |
| 979 | np = of_find_node_by_path(pp->value); |
| 980 | break; |
| 981 | } |
| 982 | } |
| 983 | if (!np) |
| 984 | return NULL; |
| 985 | path = p; |
| 986 | } |
| 987 | |
| 988 | /* Step down the tree matching path components */ |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 989 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 990 | if (!np) |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 991 | np = of_node_get(of_root); |
Rob Herring | 27497e1 | 2017-06-02 12:43:18 -0500 | [diff] [blame] | 992 | np = __of_find_node_by_full_path(np, path); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 993 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 994 | return np; |
| 995 | } |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 996 | EXPORT_SYMBOL(of_find_node_opts_by_path); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 997 | |
| 998 | /** |
| 999 | * of_find_node_by_name - Find a node by its "name" property |
Stephen Boyd | 02a876b | 2017-11-17 08:53:21 -0800 | [diff] [blame] | 1000 | * @from: The node to start searching from or NULL; the node |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 1001 | * you pass will not be searched, only the next one |
Stephen Boyd | 02a876b | 2017-11-17 08:53:21 -0800 | [diff] [blame] | 1002 | * will. Typically, you pass what the previous call |
| 1003 | * returned. of_node_put() will be called on @from. |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 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 | struct device_node *of_find_node_by_name(struct device_node *from, |
| 1010 | const char *name) |
| 1011 | { |
| 1012 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1013 | unsigned long flags; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 1014 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1015 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 1016 | for_each_of_allnodes_from(from, np) |
Rob Herring | b3e46d1 | 2018-08-27 08:37:06 -0500 | [diff] [blame] | 1017 | if (of_node_name_eq(np, name) && of_node_get(np)) |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 1018 | break; |
| 1019 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1020 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 1021 | 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 | struct device_node *of_find_node_by_type(struct device_node *from, |
| 1038 | const char *type) |
| 1039 | { |
| 1040 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1041 | unsigned long flags; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 1042 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1043 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 1044 | for_each_of_allnodes_from(from, np) |
Rob Herring | e8b1dee2 | 2018-08-29 08:36:12 -0500 | [diff] [blame] | 1045 | if (__of_node_is_type(np, type) && of_node_get(np)) |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 1046 | break; |
| 1047 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1048 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 1049 | 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 | struct device_node *of_find_compatible_node(struct device_node *from, |
| 1068 | const char *type, const char *compatible) |
| 1069 | { |
| 1070 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1071 | unsigned long flags; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 1072 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1073 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 1074 | for_each_of_allnodes_from(from, np) |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 1075 | if (__of_device_is_compatible(np, compatible, type, NULL) && |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1076 | of_node_get(np)) |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 1077 | break; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 1078 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1079 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 1080 | return np; |
| 1081 | } |
| 1082 | EXPORT_SYMBOL(of_find_compatible_node); |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1083 | |
| 1084 | /** |
Michael Ellerman | 1e291b14 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 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 | 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; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1101 | unsigned long flags; |
Michael Ellerman | 1e291b14 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 1102 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1103 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 1104 | for_each_of_allnodes_from(from, np) { |
Sachin Kamat | a3a7cab | 2012-06-27 09:44:45 +0530 | [diff] [blame] | 1105 | for (pp = np->properties; pp; pp = pp->next) { |
Michael Ellerman | 1e291b14 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 1106 | if (of_prop_cmp(pp->name, prop_name) == 0) { |
| 1107 | of_node_get(np); |
| 1108 | goto out; |
| 1109 | } |
| 1110 | } |
| 1111 | } |
| 1112 | out: |
| 1113 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1114 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Michael Ellerman | 1e291b14 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 1115 | return np; |
| 1116 | } |
| 1117 | EXPORT_SYMBOL(of_find_node_with_property); |
| 1118 | |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1119 | static |
| 1120 | const struct of_device_id *__of_match_node(const struct of_device_id *matches, |
| 1121 | const struct device_node *node) |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1122 | { |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 1123 | const struct of_device_id *best_match = NULL; |
| 1124 | int score, best_score = 0; |
| 1125 | |
Grant Likely | a52f07e | 2011-03-18 10:21:29 -0600 | [diff] [blame] | 1126 | if (!matches) |
| 1127 | return NULL; |
| 1128 | |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 1129 | for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) { |
| 1130 | score = __of_device_is_compatible(node, matches->compatible, |
| 1131 | matches->type, matches->name); |
| 1132 | if (score > best_score) { |
| 1133 | best_match = matches; |
| 1134 | best_score = score; |
| 1135 | } |
Kevin Hao | 4e8ca6e | 2014-02-14 13:22:45 +0800 | [diff] [blame] | 1136 | } |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 1137 | |
| 1138 | return best_match; |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1139 | } |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1140 | |
| 1141 | /** |
Geert Uytterhoeven | c50949d | 2014-10-22 11:44:54 +0200 | [diff] [blame] | 1142 | * of_match_node - Tell if a device_node has a matching of_match structure |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1143 | * @matches: array of of device match structures to search in |
| 1144 | * @node: the of device structure to match against |
| 1145 | * |
Kevin Hao | 71c5498 | 2014-02-18 15:57:29 +0800 | [diff] [blame] | 1146 | * Low level utility function used by device matching. |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1147 | */ |
| 1148 | 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; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1152 | unsigned long flags; |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1153 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1154 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1155 | match = __of_match_node(matches, node); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1156 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1157 | return match; |
| 1158 | } |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1159 | EXPORT_SYMBOL(of_match_node); |
| 1160 | |
| 1161 | /** |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 1162 | * of_find_matching_node_and_match - Find a node based on an of_device_id |
| 1163 | * match table. |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 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 |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 1169 | * @match Updated to point at the matches entry which matched |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1170 | * |
| 1171 | * Returns a node pointer with refcount incremented, use |
| 1172 | * of_node_put() on it when done. |
| 1173 | */ |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 1174 | 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) |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1177 | { |
| 1178 | struct device_node *np; |
Thomas Abraham | dc71bcf | 2013-01-19 10:20:42 -0800 | [diff] [blame] | 1179 | const struct of_device_id *m; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1180 | unsigned long flags; |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1181 | |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 1182 | if (match) |
| 1183 | *match = NULL; |
| 1184 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1185 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 1186 | for_each_of_allnodes_from(from, np) { |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1187 | m = __of_match_node(matches, np); |
Thomas Abraham | dc71bcf | 2013-01-19 10:20:42 -0800 | [diff] [blame] | 1188 | if (m && of_node_get(np)) { |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 1189 | if (match) |
Thomas Abraham | dc71bcf | 2013-01-19 10:20:42 -0800 | [diff] [blame] | 1190 | *match = m; |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1191 | break; |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 1192 | } |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1193 | } |
| 1194 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1195 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1196 | return np; |
| 1197 | } |
Grant Likely | 80c2022 | 2012-12-19 10:45:36 +0000 | [diff] [blame] | 1198 | EXPORT_SYMBOL(of_find_matching_node_and_match); |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1199 | |
| 1200 | /** |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 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 | * |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 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. |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1210 | * |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 1211 | * This routine returns 0 on success, <0 on failure. |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1212 | */ |
| 1213 | int of_modalias_node(struct device_node *node, char *modalias, int len) |
| 1214 | { |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 1215 | const char *compatible, *p; |
| 1216 | int cplen; |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1217 | |
| 1218 | compatible = of_get_property(node, "compatible", &cplen); |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 1219 | if (!compatible || strlen(compatible) > cplen) |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1220 | return -ENODEV; |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1221 | p = strchr(compatible, ','); |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 1222 | strlcpy(modalias, p ? p + 1 : compatible, len); |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1223 | return 0; |
| 1224 | } |
| 1225 | EXPORT_SYMBOL_GPL(of_modalias_node); |
| 1226 | |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1227 | /** |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 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 | struct device_node *of_find_node_by_phandle(phandle handle) |
| 1235 | { |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 1236 | struct device_node *np = NULL; |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 1237 | unsigned long flags; |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 1238 | phandle masked_handle; |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 1239 | |
Grant Likely | fc59b44 | 2014-10-02 13:08:02 +0100 | [diff] [blame] | 1240 | if (!handle) |
| 1241 | return NULL; |
| 1242 | |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 1243 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 1244 | |
| 1245 | masked_handle = handle & phandle_cache_mask; |
| 1246 | |
| 1247 | if (phandle_cache) { |
| 1248 | if (phandle_cache[masked_handle] && |
| 1249 | handle == phandle_cache[masked_handle]->phandle) |
| 1250 | np = phandle_cache[masked_handle]; |
Frank Rowand | 5801169 | 2018-12-18 11:40:03 -0800 | [diff] [blame] | 1251 | if (np && of_node_check_flag(np, OF_DETACHED)) { |
| 1252 | WARN_ON(1); /* did not uncache np on node removal */ |
| 1253 | of_node_put(np); |
| 1254 | phandle_cache[masked_handle] = NULL; |
| 1255 | np = NULL; |
| 1256 | } |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | if (!np) { |
| 1260 | for_each_of_allnodes(np) |
Frank Rowand | 5801169 | 2018-12-18 11:40:03 -0800 | [diff] [blame] | 1261 | if (np->phandle == handle && |
| 1262 | !of_node_check_flag(np, OF_DETACHED)) { |
Frank Rowand | b8a9ac1 | 2018-12-18 11:40:02 -0800 | [diff] [blame] | 1263 | if (phandle_cache) { |
| 1264 | /* will put when removed from cache */ |
| 1265 | of_node_get(np); |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 1266 | phandle_cache[masked_handle] = np; |
Frank Rowand | b8a9ac1 | 2018-12-18 11:40:02 -0800 | [diff] [blame] | 1267 | } |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 1268 | break; |
| 1269 | } |
| 1270 | } |
| 1271 | |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 1272 | of_node_get(np); |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 1273 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 1274 | return np; |
| 1275 | } |
| 1276 | EXPORT_SYMBOL(of_find_node_by_phandle); |
| 1277 | |
Grant Likely | 624cfca | 2013-10-11 22:05:10 +0100 | [diff] [blame] | 1278 | void of_print_phandle_args(const char *msg, const struct of_phandle_args *args) |
| 1279 | { |
| 1280 | int i; |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 1281 | printk("%s %pOF", msg, args->np); |
Marcin Nowakowski | 4aa6634 | 2016-12-01 09:00:55 +0100 | [diff] [blame] | 1282 | for (i = 0; i < args->args_count; i++) { |
| 1283 | const char delim = i ? ',' : ':'; |
| 1284 | |
| 1285 | pr_cont("%c%08x", delim, args->args[i]); |
| 1286 | } |
| 1287 | pr_cont("\n"); |
Grant Likely | 624cfca | 2013-10-11 22:05:10 +0100 | [diff] [blame] | 1288 | } |
| 1289 | |
Joerg Roedel | 74e1fbb | 2016-04-04 17:49:17 +0200 | [diff] [blame] | 1290 | 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 | memset(it, 0, sizeof(*it)); |
| 1300 | |
Uwe Kleine-König | 59e9fcf | 2019-09-18 10:47:48 +0200 | [diff] [blame] | 1301 | /* |
| 1302 | * one of cell_count or cells_name must be provided to determine the |
| 1303 | * argument length. |
| 1304 | */ |
| 1305 | if (cell_count < 0 && !cells_name) |
| 1306 | return -EINVAL; |
| 1307 | |
Joerg Roedel | 74e1fbb | 2016-04-04 17:49:17 +0200 | [diff] [blame] | 1308 | list = of_get_property(np, list_name, &size); |
| 1309 | if (!list) |
| 1310 | return -ENOENT; |
| 1311 | |
| 1312 | it->cells_name = cells_name; |
| 1313 | it->cell_count = cell_count; |
| 1314 | it->parent = np; |
| 1315 | it->list_end = list + size / sizeof(*list); |
| 1316 | it->phandle_end = list; |
| 1317 | it->cur = list; |
| 1318 | |
| 1319 | return 0; |
| 1320 | } |
Kuninori Morimoto | 00bab23 | 2017-04-20 01:31:16 +0000 | [diff] [blame] | 1321 | EXPORT_SYMBOL_GPL(of_phandle_iterator_init); |
Joerg Roedel | 74e1fbb | 2016-04-04 17:49:17 +0200 | [diff] [blame] | 1322 | |
Joerg Roedel | cd209b4 | 2016-04-04 17:49:18 +0200 | [diff] [blame] | 1323 | int of_phandle_iterator_next(struct of_phandle_iterator *it) |
| 1324 | { |
| 1325 | uint32_t count = 0; |
| 1326 | |
| 1327 | if (it->node) { |
| 1328 | of_node_put(it->node); |
| 1329 | it->node = NULL; |
| 1330 | } |
| 1331 | |
| 1332 | if (!it->cur || it->phandle_end >= it->list_end) |
| 1333 | return -ENOENT; |
| 1334 | |
| 1335 | it->cur = it->phandle_end; |
| 1336 | |
| 1337 | /* If phandle is 0, then it is an empty entry with no arguments. */ |
| 1338 | it->phandle = be32_to_cpup(it->cur++); |
| 1339 | |
| 1340 | if (it->phandle) { |
| 1341 | |
| 1342 | /* |
| 1343 | * Find the provider node and parse the #*-cells property to |
| 1344 | * determine the argument length. |
| 1345 | */ |
| 1346 | it->node = of_find_node_by_phandle(it->phandle); |
| 1347 | |
| 1348 | if (it->cells_name) { |
| 1349 | if (!it->node) { |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 1350 | pr_err("%pOF: could not find phandle\n", |
| 1351 | it->parent); |
Joerg Roedel | cd209b4 | 2016-04-04 17:49:18 +0200 | [diff] [blame] | 1352 | goto err; |
| 1353 | } |
| 1354 | |
| 1355 | if (of_property_read_u32(it->node, it->cells_name, |
| 1356 | &count)) { |
Uwe Kleine-König | e42ee61 | 2019-08-24 15:28:46 +0200 | [diff] [blame] | 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 | if (it->cell_count >= 0) { |
| 1363 | count = it->cell_count; |
| 1364 | } else { |
| 1365 | pr_err("%pOF: could not get %s for %pOF\n", |
| 1366 | it->parent, |
| 1367 | it->cells_name, |
| 1368 | it->node); |
| 1369 | goto err; |
| 1370 | } |
Joerg Roedel | cd209b4 | 2016-04-04 17:49:18 +0200 | [diff] [blame] | 1371 | } |
| 1372 | } else { |
| 1373 | count = it->cell_count; |
| 1374 | } |
| 1375 | |
| 1376 | /* |
| 1377 | * Make sure that the arguments actually fit in the remaining |
| 1378 | * property data length |
| 1379 | */ |
| 1380 | if (it->cur + count > it->list_end) { |
Florian Fainelli | af3be70 | 2019-04-03 10:56:33 -0700 | [diff] [blame] | 1381 | pr_err("%pOF: %s = %d found %d\n", |
| 1382 | it->parent, it->cells_name, |
| 1383 | count, it->cell_count); |
Joerg Roedel | cd209b4 | 2016-04-04 17:49:18 +0200 | [diff] [blame] | 1384 | goto err; |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | it->phandle_end = it->cur + count; |
| 1389 | it->cur_count = count; |
| 1390 | |
| 1391 | return 0; |
| 1392 | |
| 1393 | err: |
| 1394 | if (it->node) { |
| 1395 | of_node_put(it->node); |
| 1396 | it->node = NULL; |
| 1397 | } |
| 1398 | |
| 1399 | return -EINVAL; |
| 1400 | } |
Kuninori Morimoto | 00bab23 | 2017-04-20 01:31:16 +0000 | [diff] [blame] | 1401 | EXPORT_SYMBOL_GPL(of_phandle_iterator_next); |
Joerg Roedel | cd209b4 | 2016-04-04 17:49:18 +0200 | [diff] [blame] | 1402 | |
Joerg Roedel | abdaa77 | 2016-04-04 17:49:21 +0200 | [diff] [blame] | 1403 | int of_phandle_iterator_args(struct of_phandle_iterator *it, |
| 1404 | uint32_t *args, |
| 1405 | int size) |
| 1406 | { |
| 1407 | int i, count; |
| 1408 | |
| 1409 | count = it->cur_count; |
| 1410 | |
| 1411 | if (WARN_ON(size < count)) |
| 1412 | count = size; |
| 1413 | |
| 1414 | for (i = 0; i < count; i++) |
| 1415 | args[i] = be32_to_cpup(it->cur++); |
| 1416 | |
| 1417 | return count; |
| 1418 | } |
| 1419 | |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1420 | static int __of_parse_phandle_with_args(const struct device_node *np, |
| 1421 | const char *list_name, |
Stephen Warren | 035fd94 | 2013-08-14 15:27:10 -0600 | [diff] [blame] | 1422 | const char *cells_name, |
| 1423 | int cell_count, int index, |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1424 | struct of_phandle_args *out_args) |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1425 | { |
Joerg Roedel | 74e1fbb | 2016-04-04 17:49:17 +0200 | [diff] [blame] | 1426 | struct of_phandle_iterator it; |
| 1427 | int rc, cur_index = 0; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1428 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1429 | /* Loop over the phandles until all the requested entry is found */ |
Joerg Roedel | f623ce9 | 2016-04-04 17:49:20 +0200 | [diff] [blame] | 1430 | of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) { |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1431 | /* |
Joerg Roedel | cd209b4 | 2016-04-04 17:49:18 +0200 | [diff] [blame] | 1432 | * All of the error cases bail out of the loop, so at |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 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 | */ |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1437 | rc = -ENOENT; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1438 | if (cur_index == index) { |
Joerg Roedel | 74e1fbb | 2016-04-04 17:49:17 +0200 | [diff] [blame] | 1439 | if (!it.phandle) |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1440 | goto err; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1441 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1442 | if (out_args) { |
Joerg Roedel | abdaa77 | 2016-04-04 17:49:21 +0200 | [diff] [blame] | 1443 | int c; |
| 1444 | |
| 1445 | c = of_phandle_iterator_args(&it, |
| 1446 | out_args->args, |
| 1447 | MAX_PHANDLE_ARGS); |
Joerg Roedel | 74e1fbb | 2016-04-04 17:49:17 +0200 | [diff] [blame] | 1448 | out_args->np = it.node; |
Joerg Roedel | abdaa77 | 2016-04-04 17:49:21 +0200 | [diff] [blame] | 1449 | out_args->args_count = c; |
Tang Yuantian | b855f16 | 2013-04-10 11:36:39 +0800 | [diff] [blame] | 1450 | } else { |
Joerg Roedel | 74e1fbb | 2016-04-04 17:49:17 +0200 | [diff] [blame] | 1451 | of_node_put(it.node); |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1452 | } |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1453 | |
| 1454 | /* Found it! return success */ |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1455 | return 0; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1456 | } |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1457 | |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1458 | cur_index++; |
| 1459 | } |
| 1460 | |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 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 | */ |
Joerg Roedel | cd209b4 | 2016-04-04 17:49:18 +0200 | [diff] [blame] | 1466 | |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1467 | err: |
Markus Elfring | beab47d | 2016-07-19 22:42:22 +0200 | [diff] [blame] | 1468 | of_node_put(it.node); |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1469 | return rc; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1470 | } |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1471 | |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 1472 | /** |
Stephen Warren | 5fba49e | 2013-08-14 15:27:09 -0600 | [diff] [blame] | 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 | struct device_node *of_parse_phandle(const struct device_node *np, |
| 1483 | const char *phandle_name, int index) |
| 1484 | { |
Stephen Warren | 91d9942 | 2013-08-14 15:27:11 -0600 | [diff] [blame] | 1485 | struct of_phandle_args args; |
Stephen Warren | 5fba49e | 2013-08-14 15:27:09 -0600 | [diff] [blame] | 1486 | |
Stephen Warren | 91d9942 | 2013-08-14 15:27:11 -0600 | [diff] [blame] | 1487 | if (index < 0) |
Stephen Warren | 5fba49e | 2013-08-14 15:27:09 -0600 | [diff] [blame] | 1488 | return NULL; |
| 1489 | |
Stephen Warren | 91d9942 | 2013-08-14 15:27:11 -0600 | [diff] [blame] | 1490 | if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, |
| 1491 | index, &args)) |
| 1492 | return NULL; |
| 1493 | |
| 1494 | return args.np; |
Stephen Warren | 5fba49e | 2013-08-14 15:27:09 -0600 | [diff] [blame] | 1495 | } |
| 1496 | EXPORT_SYMBOL(of_parse_phandle); |
| 1497 | |
| 1498 | /** |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 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 | * |
Geert Uytterhoeven | d94a75c | 2014-10-22 11:44:52 +0200 | [diff] [blame] | 1510 | * Caller is responsible to call of_node_put() on the returned out_args->np |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 1511 | * pointer. |
| 1512 | * |
| 1513 | * Example: |
| 1514 | * |
| 1515 | * phandle1: node1 { |
Geert Uytterhoeven | c0e848d | 2014-10-22 11:44:55 +0200 | [diff] [blame] | 1516 | * #list-cells = <2>; |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 1517 | * } |
| 1518 | * |
| 1519 | * phandle2: node2 { |
Geert Uytterhoeven | c0e848d | 2014-10-22 11:44:55 +0200 | [diff] [blame] | 1520 | * #list-cells = <1>; |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 1521 | * } |
| 1522 | * |
| 1523 | * node3 { |
Geert Uytterhoeven | c0e848d | 2014-10-22 11:44:55 +0200 | [diff] [blame] | 1524 | * list = <&phandle1 1 2 &phandle2 3>; |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 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 | */ |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1530 | 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 | { |
Uwe Kleine-König | 59e9fcf | 2019-09-18 10:47:48 +0200 | [diff] [blame] | 1534 | int cell_count = -1; |
| 1535 | |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1536 | if (index < 0) |
| 1537 | return -EINVAL; |
Uwe Kleine-König | 59e9fcf | 2019-09-18 10:47:48 +0200 | [diff] [blame] | 1538 | |
| 1539 | /* If cells_name is NULL we assume a cell count of 0 */ |
| 1540 | if (!cells_name) |
| 1541 | cell_count = 0; |
| 1542 | |
| 1543 | return __of_parse_phandle_with_args(np, list_name, cells_name, |
| 1544 | cell_count, index, out_args); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1545 | } |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1546 | EXPORT_SYMBOL(of_parse_phandle_with_args); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1547 | |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1548 | /** |
Stephen Boyd | bd6f2fd | 2018-01-30 18:36:16 -0800 | [diff] [blame] | 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 | 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 | if (index < 0) |
| 1607 | return -EINVAL; |
| 1608 | |
| 1609 | cells_name = kasprintf(GFP_KERNEL, "#%s-cells", stem_name); |
| 1610 | if (!cells_name) |
| 1611 | return -ENOMEM; |
| 1612 | |
| 1613 | ret = -ENOMEM; |
| 1614 | map_name = kasprintf(GFP_KERNEL, "%s-map", stem_name); |
| 1615 | if (!map_name) |
| 1616 | goto free; |
| 1617 | |
| 1618 | mask_name = kasprintf(GFP_KERNEL, "%s-map-mask", stem_name); |
| 1619 | if (!mask_name) |
| 1620 | goto free; |
| 1621 | |
| 1622 | pass_name = kasprintf(GFP_KERNEL, "%s-map-pass-thru", stem_name); |
| 1623 | if (!pass_name) |
| 1624 | goto free; |
| 1625 | |
Uwe Kleine-König | e42ee61 | 2019-08-24 15:28:46 +0200 | [diff] [blame] | 1626 | ret = __of_parse_phandle_with_args(np, list_name, cells_name, -1, index, |
Stephen Boyd | bd6f2fd | 2018-01-30 18:36:16 -0800 | [diff] [blame] | 1627 | out_args); |
| 1628 | if (ret) |
| 1629 | goto free; |
| 1630 | |
| 1631 | /* Get the #<list>-cells property */ |
| 1632 | cur = out_args->np; |
| 1633 | ret = of_property_read_u32(cur, cells_name, &list_size); |
| 1634 | if (ret < 0) |
| 1635 | goto put; |
| 1636 | |
| 1637 | /* Precalculate the match array - this simplifies match loop */ |
| 1638 | for (i = 0; i < list_size; i++) |
| 1639 | initial_match_array[i] = cpu_to_be32(out_args->args[i]); |
| 1640 | |
| 1641 | ret = -EINVAL; |
| 1642 | while (cur) { |
| 1643 | /* Get the <list>-map property */ |
| 1644 | map = of_get_property(cur, map_name, &map_len); |
| 1645 | if (!map) { |
| 1646 | ret = 0; |
| 1647 | goto free; |
| 1648 | } |
| 1649 | map_len /= sizeof(u32); |
| 1650 | |
| 1651 | /* Get the <list>-map-mask property (optional) */ |
| 1652 | mask = of_get_property(cur, mask_name, NULL); |
| 1653 | if (!mask) |
| 1654 | mask = dummy_mask; |
| 1655 | /* Iterate through <list>-map property */ |
| 1656 | match = 0; |
| 1657 | while (map_len > (list_size + 1) && !match) { |
| 1658 | /* Compare specifiers */ |
| 1659 | match = 1; |
| 1660 | for (i = 0; i < list_size; i++, map_len--) |
| 1661 | match &= !((match_array[i] ^ *map++) & mask[i]); |
| 1662 | |
| 1663 | of_node_put(new); |
| 1664 | new = of_find_node_by_phandle(be32_to_cpup(map)); |
| 1665 | map++; |
| 1666 | map_len--; |
| 1667 | |
| 1668 | /* Check if not found */ |
| 1669 | if (!new) |
| 1670 | goto put; |
| 1671 | |
| 1672 | if (!of_device_is_available(new)) |
| 1673 | match = 0; |
| 1674 | |
| 1675 | ret = of_property_read_u32(new, cells_name, &new_size); |
| 1676 | if (ret) |
| 1677 | goto put; |
| 1678 | |
| 1679 | /* Check for malformed properties */ |
| 1680 | if (WARN_ON(new_size > MAX_PHANDLE_ARGS)) |
| 1681 | goto put; |
| 1682 | if (map_len < new_size) |
| 1683 | goto put; |
| 1684 | |
| 1685 | /* Move forward by new node's #<list>-cells amount */ |
| 1686 | map += new_size; |
| 1687 | map_len -= new_size; |
| 1688 | } |
| 1689 | 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 | 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 | match_array = map - new_size; |
| 1703 | for (i = 0; i < new_size; i++) { |
| 1704 | __be32 val = *(map - new_size + i); |
| 1705 | |
| 1706 | if (i < list_size) { |
| 1707 | val &= ~pass[i]; |
| 1708 | val |= cpu_to_be32(out_args->args[i]) & pass[i]; |
| 1709 | } |
| 1710 | |
| 1711 | out_args->args[i] = be32_to_cpu(val); |
| 1712 | } |
| 1713 | out_args->args_count = list_size = new_size; |
| 1714 | /* Iterate again with new provider */ |
| 1715 | out_args->np = new; |
| 1716 | of_node_put(cur); |
| 1717 | cur = new; |
| 1718 | } |
| 1719 | put: |
| 1720 | of_node_put(cur); |
| 1721 | of_node_put(new); |
| 1722 | free: |
| 1723 | kfree(mask_name); |
| 1724 | kfree(map_name); |
| 1725 | kfree(cells_name); |
| 1726 | kfree(pass_name); |
| 1727 | |
| 1728 | return ret; |
| 1729 | } |
| 1730 | EXPORT_SYMBOL(of_parse_phandle_with_args_map); |
| 1731 | |
| 1732 | /** |
Stephen Warren | 035fd94 | 2013-08-14 15:27:10 -0600 | [diff] [blame] | 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 | * |
Geert Uytterhoeven | d94a75c | 2014-10-22 11:44:52 +0200 | [diff] [blame] | 1744 | * Caller is responsible to call of_node_put() on the returned out_args->np |
Stephen Warren | 035fd94 | 2013-08-14 15:27:10 -0600 | [diff] [blame] | 1745 | * pointer. |
| 1746 | * |
| 1747 | * Example: |
| 1748 | * |
| 1749 | * phandle1: node1 { |
| 1750 | * } |
| 1751 | * |
| 1752 | * phandle2: node2 { |
| 1753 | * } |
| 1754 | * |
| 1755 | * node3 { |
Geert Uytterhoeven | c0e848d | 2014-10-22 11:44:55 +0200 | [diff] [blame] | 1756 | * list = <&phandle1 0 2 &phandle2 2 3>; |
Stephen Warren | 035fd94 | 2013-08-14 15:27:10 -0600 | [diff] [blame] | 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 | 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 | if (index < 0) |
| 1767 | return -EINVAL; |
| 1768 | 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 | /** |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 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 | int of_count_phandle_with_args(const struct device_node *np, const char *list_name, |
| 1789 | const char *cells_name) |
| 1790 | { |
Joerg Roedel | 2021bd0 | 2016-04-04 17:49:19 +0200 | [diff] [blame] | 1791 | struct of_phandle_iterator it; |
| 1792 | int rc, cur_index = 0; |
| 1793 | |
Uwe Kleine-König | 59e9fcf | 2019-09-18 10:47:48 +0200 | [diff] [blame] | 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 | if (!cells_name) { |
| 1801 | const __be32 *list; |
| 1802 | int size; |
| 1803 | |
| 1804 | list = of_get_property(np, list_name, &size); |
| 1805 | if (!list) |
| 1806 | return -ENOENT; |
| 1807 | |
| 1808 | return size / sizeof(*list); |
| 1809 | } |
| 1810 | |
Uwe Kleine-König | e42ee61 | 2019-08-24 15:28:46 +0200 | [diff] [blame] | 1811 | rc = of_phandle_iterator_init(&it, np, list_name, cells_name, -1); |
Joerg Roedel | 2021bd0 | 2016-04-04 17:49:19 +0200 | [diff] [blame] | 1812 | if (rc) |
| 1813 | return rc; |
| 1814 | |
| 1815 | while ((rc = of_phandle_iterator_next(&it)) == 0) |
| 1816 | cur_index += 1; |
| 1817 | |
| 1818 | if (rc != -ENOENT) |
| 1819 | return rc; |
| 1820 | |
| 1821 | return cur_index; |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1822 | } |
| 1823 | EXPORT_SYMBOL(of_count_phandle_with_args); |
| 1824 | |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1825 | /** |
Xiubo Li | 62664f6 | 2014-01-22 13:57:39 +0800 | [diff] [blame] | 1826 | * __of_add_property - Add a property to a node without lock operations |
| 1827 | */ |
Pantelis Antoniou | d8c5008 | 2014-07-04 19:58:46 +0300 | [diff] [blame] | 1828 | int __of_add_property(struct device_node *np, struct property *prop) |
Xiubo Li | 62664f6 | 2014-01-22 13:57:39 +0800 | [diff] [blame] | 1829 | { |
| 1830 | struct property **next; |
| 1831 | |
| 1832 | prop->next = NULL; |
| 1833 | next = &np->properties; |
| 1834 | while (*next) { |
| 1835 | if (strcmp(prop->name, (*next)->name) == 0) |
| 1836 | /* duplicate ! don't insert it */ |
| 1837 | return -EEXIST; |
| 1838 | |
| 1839 | next = &(*next)->next; |
| 1840 | } |
| 1841 | *next = prop; |
| 1842 | |
| 1843 | return 0; |
| 1844 | } |
| 1845 | |
| 1846 | /** |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1847 | * of_add_property - Add a property to a node |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1848 | */ |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1849 | int of_add_property(struct device_node *np, struct property *prop) |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1850 | { |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1851 | unsigned long flags; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1852 | int rc; |
| 1853 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1854 | mutex_lock(&of_mutex); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1855 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1856 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Xiubo Li | 62664f6 | 2014-01-22 13:57:39 +0800 | [diff] [blame] | 1857 | rc = __of_add_property(np, prop); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1858 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1859 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1860 | if (!rc) |
Pantelis Antoniou | 0829f6d | 2013-12-13 20:08:59 +0200 | [diff] [blame] | 1861 | __of_add_property_sysfs(np, prop); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1862 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1863 | mutex_unlock(&of_mutex); |
| 1864 | |
Grant Likely | 259092a | 2014-07-16 12:48:23 -0600 | [diff] [blame] | 1865 | if (!rc) |
| 1866 | of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL); |
| 1867 | |
Xiubo Li | 62664f6 | 2014-01-22 13:57:39 +0800 | [diff] [blame] | 1868 | return rc; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1869 | } |
| 1870 | |
Pantelis Antoniou | d8c5008 | 2014-07-04 19:58:46 +0300 | [diff] [blame] | 1871 | int __of_remove_property(struct device_node *np, struct property *prop) |
| 1872 | { |
| 1873 | struct property **next; |
| 1874 | |
| 1875 | for (next = &np->properties; *next; next = &(*next)->next) { |
| 1876 | if (*next == prop) |
| 1877 | break; |
| 1878 | } |
| 1879 | if (*next == NULL) |
| 1880 | return -ENODEV; |
| 1881 | |
| 1882 | /* found the node */ |
| 1883 | *next = prop->next; |
| 1884 | prop->next = np->deadprops; |
| 1885 | np->deadprops = prop; |
| 1886 | |
| 1887 | return 0; |
| 1888 | } |
| 1889 | |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1890 | /** |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1891 | * of_remove_property - Remove a property from a node. |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 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 | */ |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1898 | int of_remove_property(struct device_node *np, struct property *prop) |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1899 | { |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1900 | unsigned long flags; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1901 | int rc; |
| 1902 | |
Suraj Jitindar Singh | 201b3fe | 2016-04-28 15:34:54 +1000 | [diff] [blame] | 1903 | if (!prop) |
| 1904 | return -ENODEV; |
| 1905 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1906 | mutex_lock(&of_mutex); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1907 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1908 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Pantelis Antoniou | d8c5008 | 2014-07-04 19:58:46 +0300 | [diff] [blame] | 1909 | rc = __of_remove_property(np, prop); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1910 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1911 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1912 | if (!rc) |
| 1913 | __of_remove_property_sysfs(np, prop); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1914 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1915 | mutex_unlock(&of_mutex); |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 1916 | |
Grant Likely | 259092a | 2014-07-16 12:48:23 -0600 | [diff] [blame] | 1917 | if (!rc) |
| 1918 | of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL); |
| 1919 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1920 | return rc; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1921 | } |
| 1922 | |
Pantelis Antoniou | d8c5008 | 2014-07-04 19:58:46 +0300 | [diff] [blame] | 1923 | int __of_update_property(struct device_node *np, struct property *newprop, |
| 1924 | struct property **oldpropp) |
| 1925 | { |
| 1926 | struct property **next, *oldprop; |
| 1927 | |
| 1928 | for (next = &np->properties; *next; next = &(*next)->next) { |
| 1929 | if (of_prop_cmp((*next)->name, newprop->name) == 0) |
| 1930 | break; |
| 1931 | } |
| 1932 | *oldpropp = oldprop = *next; |
| 1933 | |
| 1934 | if (oldprop) { |
| 1935 | /* replace the node */ |
| 1936 | newprop->next = oldprop->next; |
| 1937 | *next = newprop; |
| 1938 | oldprop->next = np->deadprops; |
| 1939 | np->deadprops = oldprop; |
| 1940 | } else { |
| 1941 | /* new node */ |
| 1942 | newprop->next = NULL; |
| 1943 | *next = newprop; |
| 1944 | } |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1945 | |
| 1946 | return 0; |
| 1947 | } |
| 1948 | |
| 1949 | /* |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1950 | * of_update_property - Update a property in a node, if the property does |
Dong Aisheng | 475d009 | 2012-07-11 15:16:37 +1000 | [diff] [blame] | 1951 | * not exist, add it. |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 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 | */ |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1958 | int of_update_property(struct device_node *np, struct property *newprop) |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1959 | { |
Pantelis Antoniou | d8c5008 | 2014-07-04 19:58:46 +0300 | [diff] [blame] | 1960 | struct property *oldprop; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1961 | unsigned long flags; |
Xiubo Li | 947fdaad0 | 2014-04-17 15:48:29 +0800 | [diff] [blame] | 1962 | int rc; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1963 | |
Dong Aisheng | 475d009 | 2012-07-11 15:16:37 +1000 | [diff] [blame] | 1964 | if (!newprop->name) |
| 1965 | return -EINVAL; |
| 1966 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1967 | mutex_lock(&of_mutex); |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1968 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1969 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Pantelis Antoniou | d8c5008 | 2014-07-04 19:58:46 +0300 | [diff] [blame] | 1970 | rc = __of_update_property(np, newprop, &oldprop); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1971 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Nathan Fontenot | e81b329 | 2012-10-02 16:55:01 +0000 | [diff] [blame] | 1972 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1973 | if (!rc) |
| 1974 | __of_update_property_sysfs(np, newprop, oldprop); |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1975 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1976 | mutex_unlock(&of_mutex); |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1977 | |
Grant Likely | 259092a | 2014-07-16 12:48:23 -0600 | [diff] [blame] | 1978 | if (!rc) |
| 1979 | of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop); |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1980 | |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1981 | return rc; |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1982 | } |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1983 | |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 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 | ap->np = np; |
| 1988 | ap->id = id; |
| 1989 | strncpy(ap->stem, stem, stem_len); |
| 1990 | ap->stem[stem_len] = 0; |
| 1991 | list_add_tail(&ap->link, &aliases_lookup); |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 1992 | pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n", |
| 1993 | ap->alias, ap->stem, ap->id, np); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | /** |
Geert Uytterhoeven | 1821dda | 2014-10-22 11:44:53 +0200 | [diff] [blame] | 1997 | * of_alias_scan - Scan all properties of the 'aliases' node |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1998 | * |
Geert Uytterhoeven | 1821dda | 2014-10-22 11:44:53 +0200 | [diff] [blame] | 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. |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2002 | * |
| 2003 | * @dt_alloc: An allocator that provides a virtual address to memory |
Geert Uytterhoeven | 1821dda | 2014-10-22 11:44:53 +0200 | [diff] [blame] | 2004 | * for storing the resulting tree |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2005 | */ |
| 2006 | void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align)) |
| 2007 | { |
| 2008 | struct property *pp; |
| 2009 | |
Laurentiu Tudor | 7dbe584 | 2014-08-27 17:09:39 +0300 | [diff] [blame] | 2010 | of_aliases = of_find_node_by_path("/aliases"); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2011 | of_chosen = of_find_node_by_path("/chosen"); |
| 2012 | if (of_chosen == NULL) |
| 2013 | of_chosen = of_find_node_by_path("/chosen@0"); |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2014 | |
| 2015 | if (of_chosen) { |
Grant Likely | a752ee5 | 2014-03-28 08:12:18 -0700 | [diff] [blame] | 2016 | /* linux,stdout-path and /aliases/stdout are for legacy compatibility */ |
Sergei Shtylyov | b0d9d92 | 2017-07-23 19:55:48 +0300 | [diff] [blame] | 2017 | const char *name = NULL; |
| 2018 | |
| 2019 | if (of_property_read_string(of_chosen, "stdout-path", &name)) |
| 2020 | of_property_read_string(of_chosen, "linux,stdout-path", |
| 2021 | &name); |
Grant Likely | a752ee5 | 2014-03-28 08:12:18 -0700 | [diff] [blame] | 2022 | if (IS_ENABLED(CONFIG_PPC) && !name) |
Sergei Shtylyov | b0d9d92 | 2017-07-23 19:55:48 +0300 | [diff] [blame] | 2023 | of_property_read_string(of_aliases, "stdout", &name); |
Peter Hurley | f64255b | 2015-03-17 16:46:33 -0400 | [diff] [blame] | 2024 | if (name) |
Leif Lindholm | 7914a7c | 2014-11-27 17:56:07 +0000 | [diff] [blame] | 2025 | of_stdout = of_find_node_opts_by_path(name, &of_stdout_options); |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2026 | } |
| 2027 | |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2028 | if (!of_aliases) |
| 2029 | return; |
| 2030 | |
Dong Aisheng | 8af0da9 | 2011-12-22 20:19:24 +0800 | [diff] [blame] | 2031 | for_each_property_of_node(of_aliases, pp) { |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2032 | const char *start = pp->name; |
| 2033 | 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 | if (!strcmp(pp->name, "name") || |
| 2040 | !strcmp(pp->name, "phandle") || |
| 2041 | !strcmp(pp->name, "linux,phandle")) |
| 2042 | continue; |
| 2043 | |
| 2044 | np = of_find_node_by_path(pp->value); |
| 2045 | if (!np) |
| 2046 | continue; |
| 2047 | |
| 2048 | /* walk the alias backwards to extract the id and work out |
| 2049 | * the 'stem' string */ |
| 2050 | while (isdigit(*(end-1)) && end > start) |
| 2051 | end--; |
| 2052 | len = end - start; |
| 2053 | |
| 2054 | if (kstrtoint(end, 10, &id) < 0) |
| 2055 | continue; |
| 2056 | |
| 2057 | /* Allocate an alias_prop with enough space for the stem */ |
Paul Burton | de96ec2 | 2016-09-23 16:38:27 +0100 | [diff] [blame] | 2058 | ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap)); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2059 | if (!ap) |
| 2060 | continue; |
Grant Likely | 0640332e | 2013-08-28 21:24:17 +0100 | [diff] [blame] | 2061 | memset(ap, 0, sizeof(*ap) + len + 1); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2062 | ap->alias = start; |
| 2063 | 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 | * |
Geert Uytterhoeven | 5a53a07 | 2014-03-11 11:23:38 +0100 | [diff] [blame] | 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. |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2074 | */ |
| 2075 | int of_alias_get_id(struct device_node *np, const char *stem) |
| 2076 | { |
| 2077 | struct alias_prop *app; |
| 2078 | int id = -ENODEV; |
| 2079 | |
Pantelis Antoniou | c05aba2 | 2014-07-04 19:58:03 +0300 | [diff] [blame] | 2080 | mutex_lock(&of_mutex); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2081 | list_for_each_entry(app, &aliases_lookup, link) { |
| 2082 | if (strcmp(app->stem, stem) != 0) |
| 2083 | continue; |
| 2084 | |
| 2085 | if (np == app->np) { |
| 2086 | id = app->id; |
| 2087 | break; |
| 2088 | } |
| 2089 | } |
Pantelis Antoniou | c05aba2 | 2014-07-04 19:58:03 +0300 | [diff] [blame] | 2090 | mutex_unlock(&of_mutex); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 2091 | |
| 2092 | return id; |
| 2093 | } |
| 2094 | EXPORT_SYMBOL_GPL(of_alias_get_id); |
Stephen Warren | c541adc | 2012-04-04 09:27:46 -0600 | [diff] [blame] | 2095 | |
Wolfram Sang | 351d224 | 2015-03-12 17:17:58 +0100 | [diff] [blame] | 2096 | /** |
Michal Simek | b1078c3 | 2018-09-20 13:41:52 +0200 | [diff] [blame] | 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 |
Michal Simek | 7acf79b | 2018-10-08 13:58:47 +0200 | [diff] [blame] | 2101 | * @nbits: Maximum number of alias IDs which can be recorded in bitmap |
Michal Simek | b1078c3 | 2018-09-20 13:41:52 +0200 | [diff] [blame] | 2102 | * |
| 2103 | * The function travels the lookup table to record alias ids for the given |
| 2104 | * device match structures and alias stem. |
| 2105 | * |
Michal Simek | 59eaeba | 2018-10-12 07:43:11 +0200 | [diff] [blame] | 2106 | * Return: 0 or -ENOSYS when !CONFIG_OF or |
| 2107 | * -EOVERFLOW if alias ID is greater then allocated nbits |
Michal Simek | b1078c3 | 2018-09-20 13:41:52 +0200 | [diff] [blame] | 2108 | */ |
| 2109 | 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; |
Michal Simek | 59eaeba | 2018-10-12 07:43:11 +0200 | [diff] [blame] | 2114 | int ret = 0; |
Michal Simek | b1078c3 | 2018-09-20 13:41:52 +0200 | [diff] [blame] | 2115 | |
| 2116 | /* Zero bitmap field to make sure that all the time it is clean */ |
| 2117 | bitmap_zero(bitmap, nbits); |
| 2118 | |
| 2119 | mutex_lock(&of_mutex); |
| 2120 | pr_debug("%s: Looking for stem: %s\n", __func__, stem); |
| 2121 | 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 | if (strcmp(app->stem, stem) != 0) { |
Michal Simek | 7acf79b | 2018-10-08 13:58:47 +0200 | [diff] [blame] | 2126 | pr_debug("%s: stem comparison didn't pass %s\n", |
Michal Simek | b1078c3 | 2018-09-20 13:41:52 +0200 | [diff] [blame] | 2127 | __func__, app->stem); |
| 2128 | continue; |
| 2129 | } |
| 2130 | |
Michal Simek | b1078c3 | 2018-09-20 13:41:52 +0200 | [diff] [blame] | 2131 | if (of_match_node(matches, app->np)) { |
| 2132 | pr_debug("%s: Allocated ID %d\n", __func__, app->id); |
Michal Simek | 59eaeba | 2018-10-12 07:43:11 +0200 | [diff] [blame] | 2133 | |
| 2134 | if (app->id >= nbits) { |
| 2135 | pr_warn("%s: ID %d >= than bitmap field %d\n", |
| 2136 | __func__, app->id, nbits); |
| 2137 | ret = -EOVERFLOW; |
| 2138 | } else { |
| 2139 | set_bit(app->id, bitmap); |
| 2140 | } |
Michal Simek | b1078c3 | 2018-09-20 13:41:52 +0200 | [diff] [blame] | 2141 | } |
Michal Simek | b1078c3 | 2018-09-20 13:41:52 +0200 | [diff] [blame] | 2142 | } |
| 2143 | mutex_unlock(&of_mutex); |
| 2144 | |
Michal Simek | 59eaeba | 2018-10-12 07:43:11 +0200 | [diff] [blame] | 2145 | return ret; |
Michal Simek | b1078c3 | 2018-09-20 13:41:52 +0200 | [diff] [blame] | 2146 | } |
| 2147 | EXPORT_SYMBOL_GPL(of_alias_get_alias_list); |
| 2148 | |
| 2149 | /** |
Wolfram Sang | 351d224 | 2015-03-12 17:17:58 +0100 | [diff] [blame] | 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 | int of_alias_get_highest_id(const char *stem) |
| 2157 | { |
| 2158 | struct alias_prop *app; |
| 2159 | int id = -ENODEV; |
| 2160 | |
| 2161 | mutex_lock(&of_mutex); |
| 2162 | list_for_each_entry(app, &aliases_lookup, link) { |
| 2163 | if (strcmp(app->stem, stem) != 0) |
| 2164 | continue; |
| 2165 | |
| 2166 | if (app->id > id) |
| 2167 | id = app->id; |
| 2168 | } |
| 2169 | mutex_unlock(&of_mutex); |
| 2170 | |
| 2171 | return id; |
| 2172 | } |
| 2173 | EXPORT_SYMBOL_GPL(of_alias_get_highest_id); |
| 2174 | |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2175 | /** |
Grant Likely | 3482f2c | 2014-03-27 17:18:55 -0700 | [diff] [blame] | 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. |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2180 | * |
Grant Likely | 3482f2c | 2014-03-27 17:18:55 -0700 | [diff] [blame] | 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. |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2184 | */ |
Grant Likely | 3482f2c | 2014-03-27 17:18:55 -0700 | [diff] [blame] | 2185 | bool of_console_check(struct device_node *dn, char *name, int index) |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2186 | { |
Grant Likely | 3482f2c | 2014-03-27 17:18:55 -0700 | [diff] [blame] | 2187 | if (!dn || dn != of_stdout || console_set_on_cmdline) |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2188 | return false; |
Sergey Senozhatsky | db179e0 | 2017-09-26 15:25:10 +0900 | [diff] [blame] | 2189 | |
| 2190 | /* |
| 2191 | * XXX: cast `options' to char pointer to suppress complication |
| 2192 | * warnings: printk, UART and console drivers expect char pointer. |
| 2193 | */ |
| 2194 | return !add_preferred_console(name, index, (char *)of_stdout_options); |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2195 | } |
Grant Likely | 3482f2c | 2014-03-27 17:18:55 -0700 | [diff] [blame] | 2196 | EXPORT_SYMBOL_GPL(of_console_check); |
Sudeep KarkadaNagesha | a3e31b4 | 2013-09-18 11:53:05 +0100 | [diff] [blame] | 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 | struct device_node *of_find_next_cache_node(const struct device_node *np) |
| 2207 | { |
Rob Herring | 91d9674 | 2017-05-04 12:30:07 -0500 | [diff] [blame] | 2208 | struct device_node *child, *cache_node; |
Sudeep KarkadaNagesha | a3e31b4 | 2013-09-18 11:53:05 +0100 | [diff] [blame] | 2209 | |
Rob Herring | 91d9674 | 2017-05-04 12:30:07 -0500 | [diff] [blame] | 2210 | cache_node = of_parse_phandle(np, "l2-cache", 0); |
| 2211 | if (!cache_node) |
| 2212 | cache_node = of_parse_phandle(np, "next-level-cache", 0); |
Sudeep KarkadaNagesha | a3e31b4 | 2013-09-18 11:53:05 +0100 | [diff] [blame] | 2213 | |
Rob Herring | 91d9674 | 2017-05-04 12:30:07 -0500 | [diff] [blame] | 2214 | if (cache_node) |
| 2215 | return cache_node; |
Sudeep KarkadaNagesha | a3e31b4 | 2013-09-18 11:53:05 +0100 | [diff] [blame] | 2216 | |
| 2217 | /* OF on pmac has nodes instead of properties named "l2-cache" |
| 2218 | * beneath CPU nodes. |
| 2219 | */ |
Rob Herring | e8b1dee2 | 2018-08-29 08:36:12 -0500 | [diff] [blame] | 2220 | if (IS_ENABLED(CONFIG_PPC_PMAC) && of_node_is_type(np, "cpu")) |
Sudeep KarkadaNagesha | a3e31b4 | 2013-09-18 11:53:05 +0100 | [diff] [blame] | 2221 | for_each_child_of_node(np, child) |
Rob Herring | e8b1dee2 | 2018-08-29 08:36:12 -0500 | [diff] [blame] | 2222 | if (of_node_is_type(child, "cache")) |
Sudeep KarkadaNagesha | a3e31b4 | 2013-09-18 11:53:05 +0100 | [diff] [blame] | 2223 | return child; |
| 2224 | |
| 2225 | return NULL; |
| 2226 | } |
Philipp Zabel | fd9fdb7 | 2014-02-10 22:01:48 +0100 | [diff] [blame] | 2227 | |
| 2228 | /** |
Sudeep Holla | 5fa2353 | 2017-01-16 10:40:43 +0000 | [diff] [blame] | 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 | int of_find_last_cache_level(unsigned int cpu) |
| 2238 | { |
| 2239 | u32 cache_level = 0; |
| 2240 | struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu); |
| 2241 | |
| 2242 | while (np) { |
| 2243 | prev = np; |
| 2244 | of_node_put(np); |
| 2245 | np = of_find_next_cache_node(np); |
| 2246 | } |
| 2247 | |
| 2248 | of_property_read_u32(prev, "cache-level", &cache_level); |
| 2249 | |
| 2250 | return cache_level; |
| 2251 | } |
Nipun Gupta | 2a6db71 | 2018-09-10 19:19:16 +0530 | [diff] [blame] | 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 | 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 | if (!np || !map_name || (!target && !id_out)) |
| 2281 | return -EINVAL; |
| 2282 | |
| 2283 | map = of_get_property(np, map_name, &map_len); |
| 2284 | if (!map) { |
| 2285 | if (target) |
| 2286 | return -ENODEV; |
| 2287 | /* Otherwise, no map implies no translation */ |
| 2288 | *id_out = rid; |
| 2289 | return 0; |
| 2290 | } |
| 2291 | |
| 2292 | if (!map_len || map_len % (4 * sizeof(*map))) { |
| 2293 | pr_err("%pOF: Error: Bad %s length: %d\n", np, |
| 2294 | map_name, map_len); |
| 2295 | return -EINVAL; |
| 2296 | } |
| 2297 | |
| 2298 | /* The default is to select all bits. */ |
| 2299 | 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 | if (map_mask_name) |
| 2306 | of_property_read_u32(np, map_mask_name, &map_mask); |
| 2307 | |
| 2308 | masked_rid = map_mask & rid; |
| 2309 | 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 | if (rid_base & ~map_mask) { |
| 2317 | 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 | return -EFAULT; |
| 2321 | } |
| 2322 | |
| 2323 | if (masked_rid < rid_base || masked_rid >= rid_base + rid_len) |
| 2324 | continue; |
| 2325 | |
| 2326 | phandle_node = of_find_node_by_phandle(phandle); |
| 2327 | if (!phandle_node) |
| 2328 | return -ENODEV; |
| 2329 | |
| 2330 | if (target) { |
| 2331 | if (*target) |
| 2332 | of_node_put(phandle_node); |
| 2333 | else |
| 2334 | *target = phandle_node; |
| 2335 | |
| 2336 | if (*target != phandle_node) |
| 2337 | continue; |
| 2338 | } |
| 2339 | |
| 2340 | if (id_out) |
| 2341 | *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 | |
Jean-Philippe Brucker | fb709b5 | 2019-01-15 12:19:55 +0000 | [diff] [blame] | 2349 | 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 | if (id_out) |
| 2354 | *id_out = rid; |
| 2355 | return 0; |
Nipun Gupta | 2a6db71 | 2018-09-10 19:19:16 +0530 | [diff] [blame] | 2356 | } |
| 2357 | EXPORT_SYMBOL_GPL(of_map_rid); |