blob: db7fbc0c0893cdb609034486b0d9f300ad029d49 [file] [log] [blame]
Rob Herringaf6074f2017-12-27 12:55:14 -06001// SPDX-License-Identifier: GPL-2.0+
Stephen Rothwell97e873e2007-05-01 16:26:07 +10002/*
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 Likelye91edcf2009-10-15 10:58:09 -060013 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
14 * Grant Likely.
Stephen Rothwell97e873e2007-05-01 16:26:07 +100015 */
Rob Herring606ad422016-06-15 08:32:18 -050016
17#define pr_fmt(fmt) "OF: " fmt
18
Michal Simekb1078c32018-09-20 13:41:52 +020019#include <linux/bitmap.h>
Grant Likely3482f2c2014-03-27 17:18:55 -070020#include <linux/console.h>
Shawn Guo611cad72011-08-15 15:28:14 +080021#include <linux/ctype.h>
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +010022#include <linux/cpu.h>
Stephen Rothwell97e873e2007-05-01 16:26:07 +100023#include <linux/module.h>
24#include <linux/of.h>
Sudeep Holla5fa23532017-01-16 10:40:43 +000025#include <linux/of_device.h>
Philipp Zabelfd9fdb72014-02-10 22:01:48 +010026#include <linux/of_graph.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100027#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Grant Likely75b57ec2014-02-20 18:02:11 +000029#include <linux/string.h>
Jeremy Kerra9f2f632010-02-01 21:34:14 -070030#include <linux/proc_fs.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100031
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080032#include "of_private.h"
Shawn Guo611cad72011-08-15 15:28:14 +080033
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080034LIST_HEAD(aliases_lookup);
Shawn Guo611cad72011-08-15 15:28:14 +080035
Grant Likely5063e252014-10-03 16:28:27 +010036struct device_node *of_root;
37EXPORT_SYMBOL(of_root);
Grant Likelyfc0bdae2010-02-14 07:13:55 -070038struct device_node *of_chosen;
Shawn Guo611cad72011-08-15 15:28:14 +080039struct device_node *of_aliases;
Grant Likelya752ee52014-03-28 08:12:18 -070040struct device_node *of_stdout;
Leif Lindholm7914a7c2014-11-27 17:56:07 +000041static const char *of_stdout_options;
Shawn Guo611cad72011-08-15 15:28:14 +080042
Grant Likely8a2b22a22014-07-23 17:05:06 -060043struct kset *of_kset;
Grant Likely75b57ec2014-02-20 18:02:11 +000044
45/*
Grant Likely8a2b22a22014-07-23 17:05:06 -060046 * 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 Likely75b57ec2014-02-20 18:02:11 +000050 */
Pantelis Antoniouc05aba22014-07-04 19:58:03 +030051DEFINE_MUTEX(of_mutex);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100052
Grant Likely5063e252014-10-03 16:28:27 +010053/* use when traversing tree through the child, sibling,
Stephen Rothwell581b6052007-04-24 16:46:53 +100054 * or parent members of struct device_node.
55 */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -050056DEFINE_RAW_SPINLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100057
Rob Herringf42b0e182018-08-27 07:50:47 -050058bool 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 Herring173ee392018-10-22 09:03:54 -050071EXPORT_SYMBOL(of_node_name_eq);
Rob Herringf42b0e182018-08-27 07:50:47 -050072
73bool 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 Herring173ee392018-10-22 09:03:54 -050080EXPORT_SYMBOL(of_node_name_prefix);
Rob Herringf42b0e182018-08-27 07:50:47 -050081
Rob Herringe8b1dee22018-08-29 08:36:12 -050082static 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 Murphyb68ac8d2019-07-02 18:42:39 +010089int of_bus_n_addr_cells(struct device_node *np)
Stephen Rothwell97e873e2007-05-01 16:26:07 +100090{
Sergei Shtylyov88329632017-07-23 19:55:47 +030091 u32 cells;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100092
Robin Murphyb68ac8d2019-07-02 18:42:39 +010093 for (; np; np = np->parent)
Sergei Shtylyov88329632017-07-23 19:55:47 +030094 if (!of_property_read_u32(np, "#address-cells", &cells))
95 return cells;
Robin Murphyb68ac8d2019-07-02 18:42:39 +010096
Stephen Rothwell97e873e2007-05-01 16:26:07 +100097 /* No #address-cells property for the root node */
98 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
99}
Robin Murphyb68ac8d2019-07-02 18:42:39 +0100100
101int 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 Rothwell97e873e2007-05-01 16:26:07 +1000108EXPORT_SYMBOL(of_n_addr_cells);
109
Robin Murphyb68ac8d2019-07-02 18:42:39 +0100110int of_bus_n_size_cells(struct device_node *np)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000111{
Sergei Shtylyov88329632017-07-23 19:55:47 +0300112 u32 cells;
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000113
Robin Murphyb68ac8d2019-07-02 18:42:39 +0100114 for (; np; np = np->parent)
Sergei Shtylyov88329632017-07-23 19:55:47 +0300115 if (!of_property_read_u32(np, "#size-cells", &cells))
116 return cells;
Robin Murphyb68ac8d2019-07-02 18:42:39 +0100117
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000118 /* No #size-cells property for the root node */
119 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
120}
Robin Murphyb68ac8d2019-07-02 18:42:39 +0100121
122int 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 Rothwell97e873e2007-05-01 16:26:07 +1000129EXPORT_SYMBOL(of_n_size_cells);
130
Rob Herring0c3f0612013-09-17 10:42:50 -0500131#ifdef CONFIG_NUMA
132int __weak of_node_to_nid(struct device_node *np)
133{
Konstantin Khlebnikovc8fff7b2015-04-08 19:59:20 +0300134 return NUMA_NO_NODE;
Rob Herring0c3f0612013-09-17 10:42:50 -0500135}
136#endif
137
Frank Rowand0b3ce782018-03-04 16:14:47 -0800138/*
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 Rowandb8a9ac12018-12-18 11:40:02 -0800146
147static struct device_node **phandle_cache;
148static u32 phandle_cache_mask;
149
150/*
151 * Caller must hold devtree_lock.
152 */
153static 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
168int 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)
181late_initcall_sync(of_free_phandle_cache);
182#endif
183
Frank Rowand58011692018-12-18 11:40:03 -0800184/*
185 * Caller must hold devtree_lock.
186 */
187void __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 Rowandb9952b52018-07-12 14:00:07 -0700206void of_populate_phandle_cache(void)
Frank Rowand0b3ce782018-03-04 16:14:47 -0800207{
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 Rowandb8a9ac12018-12-18 11:40:02 -0800215 __of_free_phandle_cache();
Frank Rowand0b3ce782018-03-04 16:14:47 -0800216
217 for_each_of_allnodes(np)
218 if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL)
219 phandles++;
220
Rob Herringe54192b2018-09-11 09:28:14 -0500221 if (!phandles)
222 goto out;
223
Frank Rowand0b3ce782018-03-04 16:14:47 -0800224 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 Rowandb8a9ac12018-12-18 11:40:02 -0800233 if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL) {
234 of_node_get(np);
Frank Rowand0b3ce782018-03-04 16:14:47 -0800235 phandle_cache[np->phandle & phandle_cache_mask] = np;
Frank Rowandb8a9ac12018-12-18 11:40:02 -0800236 }
Frank Rowand0b3ce782018-03-04 16:14:47 -0800237
238out:
239 raw_spin_unlock_irqrestore(&devtree_lock, flags);
240}
241
Sudeep Holla194ec932015-05-14 15:28:24 +0100242void __init of_core_init(void)
Grant Likely75b57ec2014-02-20 18:02:11 +0000243{
244 struct device_node *np;
245
Frank Rowand0b3ce782018-03-04 16:14:47 -0800246 of_populate_phandle_cache();
247
Grant Likely75b57ec2014-02-20 18:02:11 +0000248 /* Create the kset, and register existing nodes */
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300249 mutex_lock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000250 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
251 if (!of_kset) {
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300252 mutex_unlock(&of_mutex);
Rob Herring606ad422016-06-15 08:32:18 -0500253 pr_err("failed to register existing nodes\n");
Sudeep Holla194ec932015-05-14 15:28:24 +0100254 return;
Grant Likely75b57ec2014-02-20 18:02:11 +0000255 }
256 for_each_of_allnodes(np)
Grant Likely8a2b22a22014-07-23 17:05:06 -0600257 __of_attach_node_sysfs(np);
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300258 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000259
Grant Likely83570412012-11-06 21:03:27 +0000260 /* Symlink in /proc as required by userspace ABI */
Grant Likely5063e252014-10-03 16:28:27 +0100261 if (of_root)
Grant Likely75b57ec2014-02-20 18:02:11 +0000262 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
Grant Likely75b57ec2014-02-20 18:02:11 +0000263}
Grant Likely75b57ec2014-02-20 18:02:11 +0000264
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500265static struct property *__of_find_property(const struct device_node *np,
266 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000267{
268 struct property *pp;
269
Timur Tabi64e45662008-05-08 05:19:59 +1000270 if (!np)
271 return NULL;
272
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530273 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000274 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530275 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000276 *lenp = pp->length;
277 break;
278 }
279 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500280
281 return pp;
282}
283
284struct property *of_find_property(const struct device_node *np,
285 const char *name,
286 int *lenp)
287{
288 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500289 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500290
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500291 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500292 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500293 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000294
295 return pp;
296}
297EXPORT_SYMBOL(of_find_property);
298
Grant Likely5063e252014-10-03 16:28:27 +0100299struct 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 Likelye91edcf2009-10-15 10:58:09 -0600316/**
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 */
324struct device_node *of_find_all_nodes(struct device_node *prev)
325{
326 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000327 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600328
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000329 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100330 np = __of_find_all_nodes(prev);
331 of_node_get(np);
Grant Likelye91edcf2009-10-15 10:58:09 -0600332 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000333 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600334 return np;
335}
336EXPORT_SYMBOL(of_find_all_nodes);
337
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000338/*
339 * Find a property with a given name for a given node
340 * and return the value.
341 */
Grant Likelya25095d2014-07-15 23:25:43 -0600342const void *__of_get_property(const struct device_node *np,
343 const char *name, int *lenp)
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500344{
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 Rothwell97e873e2007-05-01 16:26:07 +1000354const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500355 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000356{
357 struct property *pp = of_find_property(np, name, lenp);
358
359 return pp ? pp->value : NULL;
360}
361EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000362
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100363/*
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 */
377bool __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 */
387static 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 Herring6487c152018-08-27 13:46:51 -0500396 if (!cell && !ac && arch_match_cpu_phys_id(cpu, 0))
397 return true;
Grant Likelyf3cea452013-10-04 17:24:26 +0100398 if (!cell || !ac)
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100399 return false;
Grant Likelyf3cea452013-10-04 17:24:26 +0100400 prop_len /= sizeof(*cell) * ac;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100401 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 Millerd1cb9d12013-10-03 17:24:51 -0400413/*
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 */
419bool __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 Yamada510bd062015-10-28 12:05:27 +0900432 return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread);
David Millerd1cb9d12013-10-03 17:24:51 -0400433}
434
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100435/**
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 Yamada1c986e32016-04-20 10:18:46 +0900451 * 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 KarkadaNagesha183912d2013-08-15 14:01:40 +0100453 */
454struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
455{
David Millerd1cb9d12013-10-03 17:24:51 -0400456 struct device_node *cpun;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100457
Rob Herring651d44f2018-08-27 09:47:00 -0500458 for_each_of_cpu_node(cpun) {
David Millerd1cb9d12013-10-03 17:24:51 -0400459 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100460 return cpun;
461 }
462 return NULL;
463}
464EXPORT_SYMBOL(of_get_cpu_node);
465
Kevin Hao215a14c2014-02-19 16:15:45 +0800466/**
Suzuki K Poulosea0e71cd2018-01-02 11:25:27 +0000467 * 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 */
474int 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}
490EXPORT_SYMBOL(of_cpu_node_to_id);
491
492/**
Kevin Hao215a14c2014-02-19 16:15:45 +0800493 * __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 Rothwell0081cbc2007-05-01 16:29:19 +1000521 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500522static int __of_device_is_compatible(const struct device_node *device,
Kevin Hao215a14c2014-02-19 16:15:45 +0800523 const char *compat, const char *type, const char *name)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000524{
Kevin Hao215a14c2014-02-19 16:15:45 +0800525 struct property *prop;
526 const char *cp;
527 int index = 0, score = 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000528
Kevin Hao215a14c2014-02-19 16:15:45 +0800529 /* 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 Rothwell0081cbc2007-05-01 16:29:19 +1000541 }
542
Kevin Hao215a14c2014-02-19 16:15:45 +0800543 /* Matching type is better than matching name */
544 if (type && type[0]) {
Rob Herringe8b1dee22018-08-29 08:36:12 -0500545 if (!__of_node_is_type(device, type))
Kevin Hao215a14c2014-02-19 16:15:45 +0800546 return 0;
547 score += 2;
548 }
549
550 /* Matching name is a bit better than not */
551 if (name && name[0]) {
Rob Herringb3e46d12018-08-27 08:37:06 -0500552 if (!of_node_name_eq(device, name))
Kevin Hao215a14c2014-02-19 16:15:45 +0800553 return 0;
554 score++;
555 }
556
557 return score;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000558}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500559
560/** Checks if the given "compat" string matches one of the strings in
561 * the device's "compatible" property
562 */
563int of_device_is_compatible(const struct device_node *device,
564 const char *compat)
565{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500566 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500567 int res;
568
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500569 raw_spin_lock_irqsave(&devtree_lock, flags);
Kevin Hao215a14c2014-02-19 16:15:45 +0800570 res = __of_device_is_compatible(device, compat, NULL, NULL);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500571 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500572 return res;
573}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000574EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000575
Benjamin Herrenschmidtb9c13fe32016-07-08 08:35:59 +1000576/** 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 */
580int 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 Rothwelle679c5f2007-04-24 17:16:16 +1000598/**
Grant Likely71a157e2010-02-01 21:34:14 -0700599 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700600 * @compat: compatible string to look for in root node's compatible property.
601 *
Kevin Cernekee25c7a1d2014-11-12 12:54:00 -0800602 * Returns a positive integer if the root node has the given value in its
Grant Likely1f43cfb2010-01-28 13:47:25 -0700603 * compatible property.
604 */
Grant Likely71a157e2010-02-01 21:34:14 -0700605int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700606{
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 Likely71a157e2010-02-01 21:34:14 -0700617EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700618
619/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700620 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100621 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700622 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100623 *
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800624 * Returns true if the status property is absent or set to "okay" or "ok",
625 * false otherwise
Josh Boyer834d97d2008-03-27 00:33:14 +1100626 */
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800627static bool __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100628{
629 const char *status;
630 int statlen;
631
Xiubo Li42ccd782014-01-13 11:07:28 +0800632 if (!device)
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800633 return false;
Xiubo Li42ccd782014-01-13 11:07:28 +0800634
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700635 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100636 if (status == NULL)
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800637 return true;
Josh Boyer834d97d2008-03-27 00:33:14 +1100638
639 if (statlen > 0) {
640 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800641 return true;
Josh Boyer834d97d2008-03-27 00:33:14 +1100642 }
643
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800644 return false;
Josh Boyer834d97d2008-03-27 00:33:14 +1100645}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700646
647/**
648 * of_device_is_available - check if a device is available for use
649 *
650 * @device: Node to check for availability
651 *
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800652 * Returns true if the status property is absent or set to "okay" or "ok",
653 * false otherwise
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700654 */
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800655bool of_device_is_available(const struct device_node *device)
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700656{
657 unsigned long flags;
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800658 bool res;
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700659
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 Boyer834d97d2008-03-27 00:33:14 +1100666EXPORT_SYMBOL(of_device_is_available);
667
668/**
Kevin Cernekee37786c72015-04-09 13:05:14 -0700669 * 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 */
680bool 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}
689EXPORT_SYMBOL(of_device_is_big_endian);
690
691/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000692 * 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 */
698struct device_node *of_get_parent(const struct device_node *node)
699{
700 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500701 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000702
703 if (!node)
704 return NULL;
705
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500706 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000707 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500708 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000709 return np;
710}
711EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000712
713/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000714 * of_get_next_parent - Iterate to a node's parent
715 * @node: Node to get parent of
716 *
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +0200717 * 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 Ellermanf4eb0102007-10-26 16:54:31 +1000720 *
721 * Returns a node pointer with refcount incremented, use
722 * of_node_put() on it when done.
723 */
724struct device_node *of_get_next_parent(struct device_node *node)
725{
726 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500727 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000728
729 if (!node)
730 return NULL;
731
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500732 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000733 parent = of_node_get(node->parent);
734 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500735 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000736 return parent;
737}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300738EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000739
Grant Likely0d0e02d2014-05-22 01:04:17 +0900740static 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 Fainelli43cb4362014-05-28 10:39:02 -0700745 if (!node)
746 return NULL;
747
Grant Likely0d0e02d2014-05-22 01:04:17 +0900748 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 Ellermanf4eb0102007-10-26 16:54:31 +1000759/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000760 * 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 Siach64808272015-03-19 14:03:41 +0200764 * 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 Rothwelld1cd3552007-04-24 17:21:29 +1000767 */
768struct device_node *of_get_next_child(const struct device_node *node,
769 struct device_node *prev)
770{
771 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500772 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000773
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500774 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely0d0e02d2014-05-22 01:04:17 +0900775 next = __of_get_next_child(node, prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500776 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000777 return next;
778}
779EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000780
781/**
Timur Tabi32961932012-08-14 13:20:23 +0000782 * 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 */
789struct device_node *of_get_next_available_child(const struct device_node *node,
790 struct device_node *prev)
791{
792 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000793 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000794
Florian Fainelli43cb4362014-05-28 10:39:02 -0700795 if (!node)
796 return NULL;
797
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000798 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000799 next = prev ? prev->sibling : node->child;
800 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700801 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000802 continue;
803 if (of_node_get(next))
804 break;
805 }
806 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000807 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000808 return next;
809}
810EXPORT_SYMBOL(of_get_next_available_child);
811
812/**
Rob Herringf1f207e2018-08-22 15:04:40 -0500813 * 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 */
820struct 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 Herringe8b1dee22018-08-29 08:36:12 -0500838 __of_node_is_type(next, "cpu")))
Rob Herringf1f207e2018-08-22 15:04:40 -0500839 continue;
Rob Herringf1f207e2018-08-22 15:04:40 -0500840 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}
847EXPORT_SYMBOL(of_get_next_cpu_node);
848
849/**
Johan Hovold36156f92018-08-27 10:21:45 +0200850 * 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 */
860struct 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}
872EXPORT_SYMBOL(of_get_compatible_child);
873
874/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100875 * 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 */
885struct 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 Herringb3e46d12018-08-27 08:37:06 -0500891 if (of_node_name_eq(child, name))
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100892 break;
893 return child;
894}
895EXPORT_SYMBOL(of_get_child_by_name);
896
Frank Rowande0a58f32017-10-17 16:36:31 -0700897struct device_node *__of_find_node_by_path(struct device_node *parent,
Grant Likelyc22e6502014-03-14 17:07:12 +0000898 const char *path)
899{
900 struct device_node *child;
Leif Lindholm106937e2015-03-06 16:52:53 +0000901 int len;
Grant Likelyc22e6502014-03-14 17:07:12 +0000902
Brian Norris721a09e2015-03-17 12:30:31 -0700903 len = strcspn(path, "/:");
Grant Likelyc22e6502014-03-14 17:07:12 +0000904 if (!len)
905 return NULL;
906
907 __for_each_child_of_node(parent, child) {
Rob Herring95e6b1f2017-06-01 18:00:00 -0500908 const char *name = kbasename(child->full_name);
Grant Likelyc22e6502014-03-14 17:07:12 +0000909 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
910 return child;
911 }
912 return NULL;
913}
914
Rob Herring27497e12017-06-02 12:43:18 -0500915struct 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 Kandagatla9c197612012-09-18 08:10:28 +0100933/**
Leif Lindholm75c28c02014-11-28 11:34:28 +0000934 * of_find_node_opts_by_path - Find a node matching a full OF path
Grant Likelyc22e6502014-03-14 17:07:12 +0000935 * @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 Lindholm75c28c02014-11-28 11:34:28 +0000939 * @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 Likelyc22e6502014-03-14 17:07:12 +0000942 *
943 * Valid paths:
944 * /foo/bar Full path
945 * foo Valid alias
946 * foo/bar Valid alias + relative path
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000947 *
948 * Returns a node pointer with refcount incremented, use
949 * of_node_put() on it when done.
950 */
Leif Lindholm75c28c02014-11-28 11:34:28 +0000951struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000952{
Grant Likelyc22e6502014-03-14 17:07:12 +0000953 struct device_node *np = NULL;
954 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500955 unsigned long flags;
Leif Lindholm75c28c02014-11-28 11:34:28 +0000956 const char *separator = strchr(path, ':');
957
958 if (opts)
959 *opts = separator ? separator + 1 : NULL;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000960
Grant Likelyc22e6502014-03-14 17:07:12 +0000961 if (strcmp(path, "/") == 0)
Grant Likely5063e252014-10-03 16:28:27 +0100962 return of_node_get(of_root);
Grant Likelyc22e6502014-03-14 17:07:12 +0000963
964 /* The path could begin with an alias */
965 if (*path != '/') {
Leif Lindholm106937e2015-03-06 16:52:53 +0000966 int len;
967 const char *p = separator;
968
969 if (!p)
970 p = strchrnul(path, '/');
971 len = p - path;
Grant Likelyc22e6502014-03-14 17:07:12 +0000972
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 Gleixnerd6d3c4e2013-02-06 15:30:56 -0500989 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyc22e6502014-03-14 17:07:12 +0000990 if (!np)
Grant Likely5063e252014-10-03 16:28:27 +0100991 np = of_node_get(of_root);
Rob Herring27497e12017-06-02 12:43:18 -0500992 np = __of_find_node_by_full_path(np, path);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500993 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000994 return np;
995}
Leif Lindholm75c28c02014-11-28 11:34:28 +0000996EXPORT_SYMBOL(of_find_node_opts_by_path);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000997
998/**
999 * of_find_node_by_name - Find a node by its "name" property
Stephen Boyd02a876b2017-11-17 08:53:21 -08001000 * @from: The node to start searching from or NULL; the node
Stephen Rothwell1ef4d422007-04-24 17:57:33 +10001001 * you pass will not be searched, only the next one
Stephen Boyd02a876b2017-11-17 08:53:21 -08001002 * will. Typically, you pass what the previous call
1003 * returned. of_node_put() will be called on @from.
Stephen Rothwell1ef4d422007-04-24 17:57:33 +10001004 * @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 */
1009struct device_node *of_find_node_by_name(struct device_node *from,
1010 const char *name)
1011{
1012 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001013 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +10001014
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001015 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001016 for_each_of_allnodes_from(from, np)
Rob Herringb3e46d12018-08-27 08:37:06 -05001017 if (of_node_name_eq(np, name) && of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +10001018 break;
1019 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001020 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +10001021 return np;
1022}
1023EXPORT_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 */
1037struct device_node *of_find_node_by_type(struct device_node *from,
1038 const char *type)
1039{
1040 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001041 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +10001042
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001043 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001044 for_each_of_allnodes_from(from, np)
Rob Herringe8b1dee22018-08-29 08:36:12 -05001045 if (__of_node_is_type(np, type) && of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +10001046 break;
1047 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001048 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +10001049 return np;
1050}
1051EXPORT_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 */
1067struct device_node *of_find_compatible_node(struct device_node *from,
1068 const char *type, const char *compatible)
1069{
1070 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001071 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +10001072
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001073 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001074 for_each_of_allnodes_from(from, np)
Kevin Hao215a14c2014-02-19 16:15:45 +08001075 if (__of_device_is_compatible(np, compatible, type, NULL) &&
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001076 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +10001077 break;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +10001078 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001079 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +10001080 return np;
1081}
1082EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +11001083
1084/**
Michael Ellerman1e291b142008-11-12 18:54:42 +00001085 * 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 */
1096struct 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 Gleixnerd6d3c4e2013-02-06 15:30:56 -05001101 unsigned long flags;
Michael Ellerman1e291b142008-11-12 18:54:42 +00001102
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001103 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001104 for_each_of_allnodes_from(from, np) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +05301105 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b142008-11-12 18:54:42 +00001106 if (of_prop_cmp(pp->name, prop_name) == 0) {
1107 of_node_get(np);
1108 goto out;
1109 }
1110 }
1111 }
1112out:
1113 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001114 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b142008-11-12 18:54:42 +00001115 return np;
1116}
1117EXPORT_SYMBOL(of_find_node_with_property);
1118
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001119static
1120const struct of_device_id *__of_match_node(const struct of_device_id *matches,
1121 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +11001122{
Kevin Hao215a14c2014-02-19 16:15:45 +08001123 const struct of_device_id *best_match = NULL;
1124 int score, best_score = 0;
1125
Grant Likelya52f07e2011-03-18 10:21:29 -06001126 if (!matches)
1127 return NULL;
1128
Kevin Hao215a14c2014-02-19 16:15:45 +08001129 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 Hao4e8ca6e2014-02-14 13:22:45 +08001136 }
Kevin Hao215a14c2014-02-19 16:15:45 +08001137
1138 return best_match;
Grant Likely283029d2008-01-09 06:20:40 +11001139}
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001140
1141/**
Geert Uytterhoevenc50949d2014-10-22 11:44:54 +02001142 * of_match_node - Tell if a device_node has a matching of_match structure
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001143 * @matches: array of of device match structures to search in
1144 * @node: the of device structure to match against
1145 *
Kevin Hao71c54982014-02-18 15:57:29 +08001146 * Low level utility function used by device matching.
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001147 */
1148const 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 Gleixnerd6d3c4e2013-02-06 15:30:56 -05001152 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001153
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001154 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001155 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001156 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001157 return match;
1158}
Grant Likely283029d2008-01-09 06:20:40 +11001159EXPORT_SYMBOL(of_match_node);
1160
1161/**
Stephen Warren50c8af42012-11-20 16:12:20 -07001162 * of_find_matching_node_and_match - Find a node based on an of_device_id
1163 * match table.
Grant Likely283029d2008-01-09 06:20:40 +11001164 * @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 Warren50c8af42012-11-20 16:12:20 -07001169 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +11001170 *
1171 * Returns a node pointer with refcount incremented, use
1172 * of_node_put() on it when done.
1173 */
Stephen Warren50c8af42012-11-20 16:12:20 -07001174struct 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 Likely283029d2008-01-09 06:20:40 +11001177{
1178 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001179 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001180 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +11001181
Stephen Warren50c8af42012-11-20 16:12:20 -07001182 if (match)
1183 *match = NULL;
1184
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001185 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001186 for_each_of_allnodes_from(from, np) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001187 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001188 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -07001189 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001190 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +11001191 break;
Stephen Warren50c8af42012-11-20 16:12:20 -07001192 }
Grant Likely283029d2008-01-09 06:20:40 +11001193 }
1194 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001195 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +11001196 return np;
1197}
Grant Likely80c20222012-12-19 10:45:36 +00001198EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -04001199
1200/**
Grant Likely3f07af42008-07-25 22:25:13 -04001201 * 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 Likely2ffe8c52010-06-08 07:48:19 -06001206 * 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 Likely3f07af42008-07-25 22:25:13 -04001210 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001211 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -04001212 */
1213int of_modalias_node(struct device_node *node, char *modalias, int len)
1214{
Grant Likely2ffe8c52010-06-08 07:48:19 -06001215 const char *compatible, *p;
1216 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -04001217
1218 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -06001219 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -04001220 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -04001221 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -06001222 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -04001223 return 0;
1224}
1225EXPORT_SYMBOL_GPL(of_modalias_node);
1226
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001227/**
Jeremy Kerr89751a72010-02-01 21:34:11 -07001228 * 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 */
1234struct device_node *of_find_node_by_phandle(phandle handle)
1235{
Frank Rowand0b3ce782018-03-04 16:14:47 -08001236 struct device_node *np = NULL;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001237 unsigned long flags;
Frank Rowand0b3ce782018-03-04 16:14:47 -08001238 phandle masked_handle;
Jeremy Kerr89751a72010-02-01 21:34:11 -07001239
Grant Likelyfc59b442014-10-02 13:08:02 +01001240 if (!handle)
1241 return NULL;
1242
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001243 raw_spin_lock_irqsave(&devtree_lock, flags);
Frank Rowand0b3ce782018-03-04 16:14:47 -08001244
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 Rowand58011692018-12-18 11:40:03 -08001251 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 Rowand0b3ce782018-03-04 16:14:47 -08001257 }
1258
1259 if (!np) {
1260 for_each_of_allnodes(np)
Frank Rowand58011692018-12-18 11:40:03 -08001261 if (np->phandle == handle &&
1262 !of_node_check_flag(np, OF_DETACHED)) {
Frank Rowandb8a9ac12018-12-18 11:40:02 -08001263 if (phandle_cache) {
1264 /* will put when removed from cache */
1265 of_node_get(np);
Frank Rowand0b3ce782018-03-04 16:14:47 -08001266 phandle_cache[masked_handle] = np;
Frank Rowandb8a9ac12018-12-18 11:40:02 -08001267 }
Frank Rowand0b3ce782018-03-04 16:14:47 -08001268 break;
1269 }
1270 }
1271
Jeremy Kerr89751a72010-02-01 21:34:11 -07001272 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001273 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -07001274 return np;
1275}
1276EXPORT_SYMBOL(of_find_node_by_phandle);
1277
Grant Likely624cfca2013-10-11 22:05:10 +01001278void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1279{
1280 int i;
Rob Herring0d638a02017-06-01 15:50:55 -05001281 printk("%s %pOF", msg, args->np);
Marcin Nowakowski4aa66342016-12-01 09:00:55 +01001282 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 Likely624cfca2013-10-11 22:05:10 +01001288}
1289
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001290int 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önig59e9fcf2019-09-18 10:47:48 +02001301 /*
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 Roedel74e1fbb2016-04-04 17:49:17 +02001308 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 Morimoto00bab232017-04-20 01:31:16 +00001321EXPORT_SYMBOL_GPL(of_phandle_iterator_init);
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001322
Joerg Roedelcd209b42016-04-04 17:49:18 +02001323int 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 Herring0d638a02017-06-01 15:50:55 -05001350 pr_err("%pOF: could not find phandle\n",
1351 it->parent);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001352 goto err;
1353 }
1354
1355 if (of_property_read_u32(it->node, it->cells_name,
1356 &count)) {
Uwe Kleine-Könige42ee612019-08-24 15:28:46 +02001357 /*
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 Roedelcd209b42016-04-04 17:49:18 +02001371 }
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 Fainelliaf3be702019-04-03 10:56:33 -07001381 pr_err("%pOF: %s = %d found %d\n",
1382 it->parent, it->cells_name,
1383 count, it->cell_count);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001384 goto err;
1385 }
1386 }
1387
1388 it->phandle_end = it->cur + count;
1389 it->cur_count = count;
1390
1391 return 0;
1392
1393err:
1394 if (it->node) {
1395 of_node_put(it->node);
1396 it->node = NULL;
1397 }
1398
1399 return -EINVAL;
1400}
Kuninori Morimoto00bab232017-04-20 01:31:16 +00001401EXPORT_SYMBOL_GPL(of_phandle_iterator_next);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001402
Joerg Roedelabdaa772016-04-04 17:49:21 +02001403int 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 Likelybd69f732013-02-10 22:57:21 +00001420static int __of_parse_phandle_with_args(const struct device_node *np,
1421 const char *list_name,
Stephen Warren035fd942013-08-14 15:27:10 -06001422 const char *cells_name,
1423 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001424 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001425{
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001426 struct of_phandle_iterator it;
1427 int rc, cur_index = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001428
Grant Likely15c9a0a2011-12-12 09:25:57 -07001429 /* Loop over the phandles until all the requested entry is found */
Joerg Roedelf623ce92016-04-04 17:49:20 +02001430 of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
Grant Likely15c9a0a2011-12-12 09:25:57 -07001431 /*
Joerg Roedelcd209b42016-04-04 17:49:18 +02001432 * All of the error cases bail out of the loop, so at
Grant Likely15c9a0a2011-12-12 09:25:57 -07001433 * 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 Likely23ce04c2013-02-12 21:21:49 +00001437 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001438 if (cur_index == index) {
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001439 if (!it.phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001440 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001441
Grant Likely15c9a0a2011-12-12 09:25:57 -07001442 if (out_args) {
Joerg Roedelabdaa772016-04-04 17:49:21 +02001443 int c;
1444
1445 c = of_phandle_iterator_args(&it,
1446 out_args->args,
1447 MAX_PHANDLE_ARGS);
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001448 out_args->np = it.node;
Joerg Roedelabdaa772016-04-04 17:49:21 +02001449 out_args->args_count = c;
Tang Yuantianb855f162013-04-10 11:36:39 +08001450 } else {
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001451 of_node_put(it.node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001452 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001453
1454 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001455 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001456 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001457
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001458 cur_index++;
1459 }
1460
Grant Likely23ce04c2013-02-12 21:21:49 +00001461 /*
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 Roedelcd209b42016-04-04 17:49:18 +02001466
Grant Likely23ce04c2013-02-12 21:21:49 +00001467 err:
Markus Elfringbeab47d2016-07-19 22:42:22 +02001468 of_node_put(it.node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001469 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001470}
Grant Likelybd69f732013-02-10 22:57:21 +00001471
Stephen Warreneded9dd2013-08-14 15:27:08 -06001472/**
Stephen Warren5fba49e2013-08-14 15:27:09 -06001473 * 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 */
1482struct device_node *of_parse_phandle(const struct device_node *np,
1483 const char *phandle_name, int index)
1484{
Stephen Warren91d99422013-08-14 15:27:11 -06001485 struct of_phandle_args args;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001486
Stephen Warren91d99422013-08-14 15:27:11 -06001487 if (index < 0)
Stephen Warren5fba49e2013-08-14 15:27:09 -06001488 return NULL;
1489
Stephen Warren91d99422013-08-14 15:27:11 -06001490 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1491 index, &args))
1492 return NULL;
1493
1494 return args.np;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001495}
1496EXPORT_SYMBOL(of_parse_phandle);
1497
1498/**
Stephen Warreneded9dd2013-08-14 15:27:08 -06001499 * 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 Uytterhoevend94a75c2014-10-22 11:44:52 +02001510 * Caller is responsible to call of_node_put() on the returned out_args->np
Stephen Warreneded9dd2013-08-14 15:27:08 -06001511 * pointer.
1512 *
1513 * Example:
1514 *
1515 * phandle1: node1 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001516 * #list-cells = <2>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001517 * }
1518 *
1519 * phandle2: node2 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001520 * #list-cells = <1>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001521 * }
1522 *
1523 * node3 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001524 * list = <&phandle1 1 2 &phandle2 3>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001525 * }
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 Likelybd69f732013-02-10 22:57:21 +00001530int 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önig59e9fcf2019-09-18 10:47:48 +02001534 int cell_count = -1;
1535
Grant Likelybd69f732013-02-10 22:57:21 +00001536 if (index < 0)
1537 return -EINVAL;
Uwe Kleine-König59e9fcf2019-09-18 10:47:48 +02001538
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 Likelybd69f732013-02-10 22:57:21 +00001545}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001546EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001547
Grant Likelybd69f732013-02-10 22:57:21 +00001548/**
Stephen Boydbd6f2fd2018-01-30 18:36:16 -08001549 * 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 */
1590int 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önige42ee612019-08-24 15:28:46 +02001626 ret = __of_parse_phandle_with_args(np, list_name, cells_name, -1, index,
Stephen Boydbd6f2fd2018-01-30 18:36:16 -08001627 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 }
1719put:
1720 of_node_put(cur);
1721 of_node_put(new);
1722free:
1723 kfree(mask_name);
1724 kfree(map_name);
1725 kfree(cells_name);
1726 kfree(pass_name);
1727
1728 return ret;
1729}
1730EXPORT_SYMBOL(of_parse_phandle_with_args_map);
1731
1732/**
Stephen Warren035fd942013-08-14 15:27:10 -06001733 * 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 Uytterhoevend94a75c2014-10-22 11:44:52 +02001744 * Caller is responsible to call of_node_put() on the returned out_args->np
Stephen Warren035fd942013-08-14 15:27:10 -06001745 * pointer.
1746 *
1747 * Example:
1748 *
1749 * phandle1: node1 {
1750 * }
1751 *
1752 * phandle2: node2 {
1753 * }
1754 *
1755 * node3 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001756 * list = <&phandle1 0 2 &phandle2 2 3>;
Stephen Warren035fd942013-08-14 15:27:10 -06001757 * }
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 */
1762int 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}
1771EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1772
1773/**
Grant Likelybd69f732013-02-10 22:57:21 +00001774 * 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 */
1788int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1789 const char *cells_name)
1790{
Joerg Roedel2021bd02016-04-04 17:49:19 +02001791 struct of_phandle_iterator it;
1792 int rc, cur_index = 0;
1793
Uwe Kleine-König59e9fcf2019-09-18 10:47:48 +02001794 /*
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önige42ee612019-08-24 15:28:46 +02001811 rc = of_phandle_iterator_init(&it, np, list_name, cells_name, -1);
Joerg Roedel2021bd02016-04-04 17:49:19 +02001812 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 Likelybd69f732013-02-10 22:57:21 +00001822}
1823EXPORT_SYMBOL(of_count_phandle_with_args);
1824
Grant Likely02af11b2009-11-23 20:16:45 -07001825/**
Xiubo Li62664f62014-01-22 13:57:39 +08001826 * __of_add_property - Add a property to a node without lock operations
1827 */
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001828int __of_add_property(struct device_node *np, struct property *prop)
Xiubo Li62664f62014-01-22 13:57:39 +08001829{
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 Fontenot79d1c712012-10-02 16:58:46 +00001847 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001848 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001849int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001850{
Grant Likely02af11b2009-11-23 20:16:45 -07001851 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001852 int rc;
1853
Grant Likely8a2b22a22014-07-23 17:05:06 -06001854 mutex_lock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07001855
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001856 raw_spin_lock_irqsave(&devtree_lock, flags);
Xiubo Li62664f62014-01-22 13:57:39 +08001857 rc = __of_add_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001858 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001859
Grant Likely8a2b22a22014-07-23 17:05:06 -06001860 if (!rc)
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +02001861 __of_add_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001862
Grant Likely8a2b22a22014-07-23 17:05:06 -06001863 mutex_unlock(&of_mutex);
1864
Grant Likely259092a2014-07-16 12:48:23 -06001865 if (!rc)
1866 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1867
Xiubo Li62664f62014-01-22 13:57:39 +08001868 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001869}
1870
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001871int __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 Likely02af11b2009-11-23 20:16:45 -07001890/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001891 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001892 *
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 Fontenot79d1c712012-10-02 16:58:46 +00001898int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001899{
Grant Likely02af11b2009-11-23 20:16:45 -07001900 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001901 int rc;
1902
Suraj Jitindar Singh201b3fe2016-04-28 15:34:54 +10001903 if (!prop)
1904 return -ENODEV;
1905
Grant Likely8a2b22a22014-07-23 17:05:06 -06001906 mutex_lock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07001907
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001908 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001909 rc = __of_remove_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001910 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001911
Grant Likely8a2b22a22014-07-23 17:05:06 -06001912 if (!rc)
1913 __of_remove_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001914
Grant Likely8a2b22a22014-07-23 17:05:06 -06001915 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +00001916
Grant Likely259092a2014-07-16 12:48:23 -06001917 if (!rc)
1918 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1919
Grant Likely8a2b22a22014-07-23 17:05:06 -06001920 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001921}
1922
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001923int __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 Likely02af11b2009-11-23 20:16:45 -07001945
1946 return 0;
1947}
1948
1949/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001950 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001951 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001952 *
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 Fontenot79d1c712012-10-02 16:58:46 +00001958int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001959{
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001960 struct property *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001961 unsigned long flags;
Xiubo Li947fdaad02014-04-17 15:48:29 +08001962 int rc;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001963
Dong Aisheng475d0092012-07-11 15:16:37 +10001964 if (!newprop->name)
1965 return -EINVAL;
1966
Grant Likely8a2b22a22014-07-23 17:05:06 -06001967 mutex_lock(&of_mutex);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001968
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001969 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001970 rc = __of_update_property(np, newprop, &oldprop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001971 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001972
Grant Likely8a2b22a22014-07-23 17:05:06 -06001973 if (!rc)
1974 __of_update_property_sysfs(np, newprop, oldprop);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001975
Grant Likely8a2b22a22014-07-23 17:05:06 -06001976 mutex_unlock(&of_mutex);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001977
Grant Likely259092a2014-07-16 12:48:23 -06001978 if (!rc)
1979 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001980
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001981 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001982}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001983
Shawn Guo611cad72011-08-15 15:28:14 +08001984static 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 Herring0d638a02017-06-01 15:50:55 -05001992 pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n",
1993 ap->alias, ap->stem, ap->id, np);
Shawn Guo611cad72011-08-15 15:28:14 +08001994}
1995
1996/**
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001997 * of_alias_scan - Scan all properties of the 'aliases' node
Shawn Guo611cad72011-08-15 15:28:14 +08001998 *
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001999 * 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 Guo611cad72011-08-15 15:28:14 +08002002 *
2003 * @dt_alloc: An allocator that provides a virtual address to memory
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02002004 * for storing the resulting tree
Shawn Guo611cad72011-08-15 15:28:14 +08002005 */
2006void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
2007{
2008 struct property *pp;
2009
Laurentiu Tudor7dbe5842014-08-27 17:09:39 +03002010 of_aliases = of_find_node_by_path("/aliases");
Shawn Guo611cad72011-08-15 15:28:14 +08002011 of_chosen = of_find_node_by_path("/chosen");
2012 if (of_chosen == NULL)
2013 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauer5c19e952013-08-05 14:40:44 +02002014
2015 if (of_chosen) {
Grant Likelya752ee52014-03-28 08:12:18 -07002016 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
Sergei Shtylyovb0d9d922017-07-23 19:55:48 +03002017 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 Likelya752ee52014-03-28 08:12:18 -07002022 if (IS_ENABLED(CONFIG_PPC) && !name)
Sergei Shtylyovb0d9d922017-07-23 19:55:48 +03002023 of_property_read_string(of_aliases, "stdout", &name);
Peter Hurleyf64255b2015-03-17 16:46:33 -04002024 if (name)
Leif Lindholm7914a7c2014-11-27 17:56:07 +00002025 of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
Sascha Hauer5c19e952013-08-05 14:40:44 +02002026 }
2027
Shawn Guo611cad72011-08-15 15:28:14 +08002028 if (!of_aliases)
2029 return;
2030
Dong Aisheng8af0da92011-12-22 20:19:24 +08002031 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08002032 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 Burtonde96ec22016-09-23 16:38:27 +01002058 ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap));
Shawn Guo611cad72011-08-15 15:28:14 +08002059 if (!ap)
2060 continue;
Grant Likely0640332e2013-08-28 21:24:17 +01002061 memset(ap, 0, sizeof(*ap) + len + 1);
Shawn Guo611cad72011-08-15 15:28:14 +08002062 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 Uytterhoeven5a53a072014-03-11 11:23:38 +01002072 * 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 Guo611cad72011-08-15 15:28:14 +08002074 */
2075int of_alias_get_id(struct device_node *np, const char *stem)
2076{
2077 struct alias_prop *app;
2078 int id = -ENODEV;
2079
Pantelis Antoniouc05aba22014-07-04 19:58:03 +03002080 mutex_lock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08002081 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 Antoniouc05aba22014-07-04 19:58:03 +03002090 mutex_unlock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08002091
2092 return id;
2093}
2094EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06002095
Wolfram Sang351d2242015-03-12 17:17:58 +01002096/**
Michal Simekb1078c32018-09-20 13:41:52 +02002097 * 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 Simek7acf79b2018-10-08 13:58:47 +02002101 * @nbits: Maximum number of alias IDs which can be recorded in bitmap
Michal Simekb1078c32018-09-20 13:41:52 +02002102 *
2103 * The function travels the lookup table to record alias ids for the given
2104 * device match structures and alias stem.
2105 *
Michal Simek59eaeba2018-10-12 07:43:11 +02002106 * Return: 0 or -ENOSYS when !CONFIG_OF or
2107 * -EOVERFLOW if alias ID is greater then allocated nbits
Michal Simekb1078c32018-09-20 13:41:52 +02002108 */
2109int 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 Simek59eaeba2018-10-12 07:43:11 +02002114 int ret = 0;
Michal Simekb1078c32018-09-20 13:41:52 +02002115
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 Simek7acf79b2018-10-08 13:58:47 +02002126 pr_debug("%s: stem comparison didn't pass %s\n",
Michal Simekb1078c32018-09-20 13:41:52 +02002127 __func__, app->stem);
2128 continue;
2129 }
2130
Michal Simekb1078c32018-09-20 13:41:52 +02002131 if (of_match_node(matches, app->np)) {
2132 pr_debug("%s: Allocated ID %d\n", __func__, app->id);
Michal Simek59eaeba2018-10-12 07:43:11 +02002133
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 Simekb1078c32018-09-20 13:41:52 +02002141 }
Michal Simekb1078c32018-09-20 13:41:52 +02002142 }
2143 mutex_unlock(&of_mutex);
2144
Michal Simek59eaeba2018-10-12 07:43:11 +02002145 return ret;
Michal Simekb1078c32018-09-20 13:41:52 +02002146}
2147EXPORT_SYMBOL_GPL(of_alias_get_alias_list);
2148
2149/**
Wolfram Sang351d2242015-03-12 17:17:58 +01002150 * 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 */
2156int 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}
2173EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
2174
Sascha Hauer5c19e952013-08-05 14:40:44 +02002175/**
Grant Likely3482f2c2014-03-27 17:18:55 -07002176 * 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 Hauer5c19e952013-08-05 14:40:44 +02002180 *
Grant Likely3482f2c2014-03-27 17:18:55 -07002181 * 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 Hauer5c19e952013-08-05 14:40:44 +02002184 */
Grant Likely3482f2c2014-03-27 17:18:55 -07002185bool of_console_check(struct device_node *dn, char *name, int index)
Sascha Hauer5c19e952013-08-05 14:40:44 +02002186{
Grant Likely3482f2c2014-03-27 17:18:55 -07002187 if (!dn || dn != of_stdout || console_set_on_cmdline)
Sascha Hauer5c19e952013-08-05 14:40:44 +02002188 return false;
Sergey Senozhatskydb179e02017-09-26 15:25:10 +09002189
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 Hauer5c19e952013-08-05 14:40:44 +02002195}
Grant Likely3482f2c2014-03-27 17:18:55 -07002196EXPORT_SYMBOL_GPL(of_console_check);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002197
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 */
2206struct device_node *of_find_next_cache_node(const struct device_node *np)
2207{
Rob Herring91d96742017-05-04 12:30:07 -05002208 struct device_node *child, *cache_node;
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002209
Rob Herring91d96742017-05-04 12:30:07 -05002210 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 KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002213
Rob Herring91d96742017-05-04 12:30:07 -05002214 if (cache_node)
2215 return cache_node;
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002216
2217 /* OF on pmac has nodes instead of properties named "l2-cache"
2218 * beneath CPU nodes.
2219 */
Rob Herringe8b1dee22018-08-29 08:36:12 -05002220 if (IS_ENABLED(CONFIG_PPC_PMAC) && of_node_is_type(np, "cpu"))
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002221 for_each_child_of_node(np, child)
Rob Herringe8b1dee22018-08-29 08:36:12 -05002222 if (of_node_is_type(child, "cache"))
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002223 return child;
2224
2225 return NULL;
2226}
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002227
2228/**
Sudeep Holla5fa23532017-01-16 10:40:43 +00002229 * 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 */
2237int 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 Gupta2a6db712018-09-10 19:19:16 +05302252
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 */
2272int 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 Bruckerfb709b52019-01-15 12:19:55 +00002349 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 Gupta2a6db712018-09-10 19:19:16 +05302356}
2357EXPORT_SYMBOL_GPL(of_map_rid);