]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - include/linux/of.h
of: overlay: add tests to validate kfrees from overlay removal
[mirror_ubuntu-bionic-kernel.git] / include / linux / of.h
1 #ifndef _LINUX_OF_H
2 #define _LINUX_OF_H
3 /*
4 * Definitions for talking to the Open Firmware PROM on
5 * Power Macintosh and other computers.
6 *
7 * Copyright (C) 1996-2005 Paul Mackerras.
8 *
9 * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp.
10 * Updates for SPARC64 by David S. Miller
11 * Derived from PowerPC and Sparc prom.h files by Stephen Rothwell, IBM Corp.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
17 */
18 #include <linux/types.h>
19 #include <linux/bitops.h>
20 #include <linux/errno.h>
21 #include <linux/kobject.h>
22 #include <linux/mod_devicetable.h>
23 #include <linux/spinlock.h>
24 #include <linux/topology.h>
25 #include <linux/notifier.h>
26 #include <linux/property.h>
27 #include <linux/list.h>
28
29 #include <asm/byteorder.h>
30 #include <asm/errno.h>
31
32 typedef u32 phandle;
33 typedef u32 ihandle;
34
35 struct property {
36 char *name;
37 int length;
38 void *value;
39 struct property *next;
40 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
41 unsigned long _flags;
42 #endif
43 #if defined(CONFIG_OF_PROMTREE)
44 unsigned int unique_id;
45 #endif
46 #if defined(CONFIG_OF_KOBJ)
47 struct bin_attribute attr;
48 #endif
49 };
50
51 #if defined(CONFIG_SPARC)
52 struct of_irq_controller;
53 #endif
54
55 struct device_node {
56 const char *name;
57 const char *type;
58 phandle phandle;
59 const char *full_name;
60 struct fwnode_handle fwnode;
61
62 struct property *properties;
63 struct property *deadprops; /* removed properties */
64 struct device_node *parent;
65 struct device_node *child;
66 struct device_node *sibling;
67 #if defined(CONFIG_OF_KOBJ)
68 struct kobject kobj;
69 #endif
70 unsigned long _flags;
71 void *data;
72 #if defined(CONFIG_SPARC)
73 const char *path_component_name;
74 unsigned int unique_id;
75 struct of_irq_controller *irq_trans;
76 #endif
77 };
78
79 #define MAX_PHANDLE_ARGS 16
80 struct of_phandle_args {
81 struct device_node *np;
82 int args_count;
83 uint32_t args[MAX_PHANDLE_ARGS];
84 };
85
86 struct of_phandle_iterator {
87 /* Common iterator information */
88 const char *cells_name;
89 int cell_count;
90 const struct device_node *parent;
91
92 /* List size information */
93 const __be32 *list_end;
94 const __be32 *phandle_end;
95
96 /* Current position state */
97 const __be32 *cur;
98 uint32_t cur_count;
99 phandle phandle;
100 struct device_node *node;
101 };
102
103 struct of_reconfig_data {
104 struct device_node *dn;
105 struct property *prop;
106 struct property *old_prop;
107 };
108
109 /* initialize a node */
110 extern struct kobj_type of_node_ktype;
111 extern const struct fwnode_operations of_fwnode_ops;
112 static inline void of_node_init(struct device_node *node)
113 {
114 #if defined(CONFIG_OF_KOBJ)
115 kobject_init(&node->kobj, &of_node_ktype);
116 #endif
117 node->fwnode.ops = &of_fwnode_ops;
118 }
119
120 #if defined(CONFIG_OF_KOBJ)
121 #define of_node_kobj(n) (&(n)->kobj)
122 #else
123 #define of_node_kobj(n) NULL
124 #endif
125
126 #ifdef CONFIG_OF_DYNAMIC
127 extern struct device_node *of_node_get(struct device_node *node);
128 extern void of_node_put(struct device_node *node);
129 #else /* CONFIG_OF_DYNAMIC */
130 /* Dummy ref counting routines - to be implemented later */
131 static inline struct device_node *of_node_get(struct device_node *node)
132 {
133 return node;
134 }
135 static inline void of_node_put(struct device_node *node) { }
136 #endif /* !CONFIG_OF_DYNAMIC */
137
138 /* Pointer for first entry in chain of all nodes. */
139 extern struct device_node *of_root;
140 extern struct device_node *of_chosen;
141 extern struct device_node *of_aliases;
142 extern struct device_node *of_stdout;
143 extern raw_spinlock_t devtree_lock;
144
145 /*
146 * struct device_node flag descriptions
147 * (need to be visible even when !CONFIG_OF)
148 */
149 #define OF_DYNAMIC 1 /* (and properties) allocated via kmalloc */
150 #define OF_DETACHED 2 /* detached from the device tree */
151 #define OF_POPULATED 3 /* device already created */
152 #define OF_POPULATED_BUS 4 /* platform bus created for children */
153 #define OF_OVERLAY 5 /* allocated for an overlay */
154 #define OF_OVERLAY_FREE_CSET 6 /* in overlay cset being freed */
155
156 #define OF_BAD_ADDR ((u64)-1)
157
158 #ifdef CONFIG_OF
159 void of_core_init(void);
160
161 static inline bool is_of_node(const struct fwnode_handle *fwnode)
162 {
163 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &of_fwnode_ops;
164 }
165
166 #define to_of_node(__fwnode) \
167 ({ \
168 typeof(__fwnode) __to_of_node_fwnode = (__fwnode); \
169 \
170 is_of_node(__to_of_node_fwnode) ? \
171 container_of(__to_of_node_fwnode, \
172 struct device_node, fwnode) : \
173 NULL; \
174 })
175
176 #define of_fwnode_handle(node) \
177 ({ \
178 typeof(node) __of_fwnode_handle_node = (node); \
179 \
180 __of_fwnode_handle_node ? \
181 &__of_fwnode_handle_node->fwnode : NULL; \
182 })
183
184 static inline bool of_have_populated_dt(void)
185 {
186 return of_root != NULL;
187 }
188
189 static inline bool of_node_is_root(const struct device_node *node)
190 {
191 return node && (node->parent == NULL);
192 }
193
194 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
195 {
196 return test_bit(flag, &n->_flags);
197 }
198
199 static inline int of_node_test_and_set_flag(struct device_node *n,
200 unsigned long flag)
201 {
202 return test_and_set_bit(flag, &n->_flags);
203 }
204
205 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
206 {
207 set_bit(flag, &n->_flags);
208 }
209
210 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
211 {
212 clear_bit(flag, &n->_flags);
213 }
214
215 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
216 static inline int of_property_check_flag(struct property *p, unsigned long flag)
217 {
218 return test_bit(flag, &p->_flags);
219 }
220
221 static inline void of_property_set_flag(struct property *p, unsigned long flag)
222 {
223 set_bit(flag, &p->_flags);
224 }
225
226 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
227 {
228 clear_bit(flag, &p->_flags);
229 }
230 #endif
231
232 extern struct device_node *__of_find_all_nodes(struct device_node *prev);
233 extern struct device_node *of_find_all_nodes(struct device_node *prev);
234
235 /*
236 * OF address retrieval & translation
237 */
238
239 /* Helper to read a big number; size is in cells (not bytes) */
240 static inline u64 of_read_number(const __be32 *cell, int size)
241 {
242 u64 r = 0;
243 while (size--)
244 r = (r << 32) | be32_to_cpu(*(cell++));
245 return r;
246 }
247
248 /* Like of_read_number, but we want an unsigned long result */
249 static inline unsigned long of_read_ulong(const __be32 *cell, int size)
250 {
251 /* toss away upper bits if unsigned long is smaller than u64 */
252 return of_read_number(cell, size);
253 }
254
255 #if defined(CONFIG_SPARC)
256 #include <asm/prom.h>
257 #endif
258
259 /* Default #address and #size cells. Allow arch asm/prom.h to override */
260 #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
261 #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
262 #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
263 #endif
264
265 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
266 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
267
268 static inline const char *of_node_full_name(const struct device_node *np)
269 {
270 return np ? np->full_name : "<no-node>";
271 }
272
273 #define for_each_of_allnodes_from(from, dn) \
274 for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
275 #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
276 extern struct device_node *of_find_node_by_name(struct device_node *from,
277 const char *name);
278 extern struct device_node *of_find_node_by_type(struct device_node *from,
279 const char *type);
280 extern struct device_node *of_find_compatible_node(struct device_node *from,
281 const char *type, const char *compat);
282 extern struct device_node *of_find_matching_node_and_match(
283 struct device_node *from,
284 const struct of_device_id *matches,
285 const struct of_device_id **match);
286
287 extern struct device_node *of_find_node_opts_by_path(const char *path,
288 const char **opts);
289 static inline struct device_node *of_find_node_by_path(const char *path)
290 {
291 return of_find_node_opts_by_path(path, NULL);
292 }
293
294 extern struct device_node *of_find_node_by_phandle(phandle handle);
295 extern struct device_node *of_get_parent(const struct device_node *node);
296 extern struct device_node *of_get_next_parent(struct device_node *node);
297 extern struct device_node *of_get_next_child(const struct device_node *node,
298 struct device_node *prev);
299 extern struct device_node *of_get_next_available_child(
300 const struct device_node *node, struct device_node *prev);
301
302 extern struct device_node *of_get_compatible_child(const struct device_node *parent,
303 const char *compatible);
304 extern struct device_node *of_get_child_by_name(const struct device_node *node,
305 const char *name);
306
307 /* cache lookup */
308 extern struct device_node *of_find_next_cache_node(const struct device_node *);
309 extern int of_find_last_cache_level(unsigned int cpu);
310 extern struct device_node *of_find_node_with_property(
311 struct device_node *from, const char *prop_name);
312
313 extern struct property *of_find_property(const struct device_node *np,
314 const char *name,
315 int *lenp);
316 extern int of_property_count_elems_of_size(const struct device_node *np,
317 const char *propname, int elem_size);
318 extern int of_property_read_u32_index(const struct device_node *np,
319 const char *propname,
320 u32 index, u32 *out_value);
321 extern int of_property_read_u64_index(const struct device_node *np,
322 const char *propname,
323 u32 index, u64 *out_value);
324 extern int of_property_read_variable_u8_array(const struct device_node *np,
325 const char *propname, u8 *out_values,
326 size_t sz_min, size_t sz_max);
327 extern int of_property_read_variable_u16_array(const struct device_node *np,
328 const char *propname, u16 *out_values,
329 size_t sz_min, size_t sz_max);
330 extern int of_property_read_variable_u32_array(const struct device_node *np,
331 const char *propname,
332 u32 *out_values,
333 size_t sz_min,
334 size_t sz_max);
335 extern int of_property_read_u64(const struct device_node *np,
336 const char *propname, u64 *out_value);
337 extern int of_property_read_variable_u64_array(const struct device_node *np,
338 const char *propname,
339 u64 *out_values,
340 size_t sz_min,
341 size_t sz_max);
342
343 extern int of_property_read_string(const struct device_node *np,
344 const char *propname,
345 const char **out_string);
346 extern int of_property_match_string(const struct device_node *np,
347 const char *propname,
348 const char *string);
349 extern int of_property_read_string_helper(const struct device_node *np,
350 const char *propname,
351 const char **out_strs, size_t sz, int index);
352 extern int of_device_is_compatible(const struct device_node *device,
353 const char *);
354 extern int of_device_compatible_match(struct device_node *device,
355 const char *const *compat);
356 extern bool of_device_is_available(const struct device_node *device);
357 extern bool of_device_is_big_endian(const struct device_node *device);
358 extern const void *of_get_property(const struct device_node *node,
359 const char *name,
360 int *lenp);
361 extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
362 #define for_each_property_of_node(dn, pp) \
363 for (pp = dn->properties; pp != NULL; pp = pp->next)
364
365 extern int of_n_addr_cells(struct device_node *np);
366 extern int of_n_size_cells(struct device_node *np);
367 extern const struct of_device_id *of_match_node(
368 const struct of_device_id *matches, const struct device_node *node);
369 extern int of_modalias_node(struct device_node *node, char *modalias, int len);
370 extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
371 extern struct device_node *of_parse_phandle(const struct device_node *np,
372 const char *phandle_name,
373 int index);
374 extern int of_parse_phandle_with_args(const struct device_node *np,
375 const char *list_name, const char *cells_name, int index,
376 struct of_phandle_args *out_args);
377 extern int of_parse_phandle_with_fixed_args(const struct device_node *np,
378 const char *list_name, int cells_count, int index,
379 struct of_phandle_args *out_args);
380 extern int of_count_phandle_with_args(const struct device_node *np,
381 const char *list_name, const char *cells_name);
382
383 /* phandle iterator functions */
384 extern int of_phandle_iterator_init(struct of_phandle_iterator *it,
385 const struct device_node *np,
386 const char *list_name,
387 const char *cells_name,
388 int cell_count);
389
390 extern int of_phandle_iterator_next(struct of_phandle_iterator *it);
391 extern int of_phandle_iterator_args(struct of_phandle_iterator *it,
392 uint32_t *args,
393 int size);
394
395 extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
396 extern int of_alias_get_id(struct device_node *np, const char *stem);
397 extern int of_alias_get_highest_id(const char *stem);
398
399 extern int of_machine_is_compatible(const char *compat);
400
401 extern int of_add_property(struct device_node *np, struct property *prop);
402 extern int of_remove_property(struct device_node *np, struct property *prop);
403 extern int of_update_property(struct device_node *np, struct property *newprop);
404
405 /* For updating the device tree at runtime */
406 #define OF_RECONFIG_ATTACH_NODE 0x0001
407 #define OF_RECONFIG_DETACH_NODE 0x0002
408 #define OF_RECONFIG_ADD_PROPERTY 0x0003
409 #define OF_RECONFIG_REMOVE_PROPERTY 0x0004
410 #define OF_RECONFIG_UPDATE_PROPERTY 0x0005
411
412 extern int of_attach_node(struct device_node *);
413 extern int of_detach_node(struct device_node *);
414
415 #define of_match_ptr(_ptr) (_ptr)
416
417 /**
418 * of_property_read_u8_array - Find and read an array of u8 from a property.
419 *
420 * @np: device node from which the property value is to be read.
421 * @propname: name of the property to be searched.
422 * @out_values: pointer to return value, modified only if return value is 0.
423 * @sz: number of array elements to read
424 *
425 * Search for a property in a device node and read 8-bit value(s) from
426 * it. Returns 0 on success, -EINVAL if the property does not exist,
427 * -ENODATA if property does not have a value, and -EOVERFLOW if the
428 * property data isn't large enough.
429 *
430 * dts entry of array should be like:
431 * property = /bits/ 8 <0x50 0x60 0x70>;
432 *
433 * The out_values is modified only if a valid u8 value can be decoded.
434 */
435 static inline int of_property_read_u8_array(const struct device_node *np,
436 const char *propname,
437 u8 *out_values, size_t sz)
438 {
439 int ret = of_property_read_variable_u8_array(np, propname, out_values,
440 sz, 0);
441 if (ret >= 0)
442 return 0;
443 else
444 return ret;
445 }
446
447 /**
448 * of_property_read_u16_array - Find and read an array of u16 from a property.
449 *
450 * @np: device node from which the property value is to be read.
451 * @propname: name of the property to be searched.
452 * @out_values: pointer to return value, modified only if return value is 0.
453 * @sz: number of array elements to read
454 *
455 * Search for a property in a device node and read 16-bit value(s) from
456 * it. Returns 0 on success, -EINVAL if the property does not exist,
457 * -ENODATA if property does not have a value, and -EOVERFLOW if the
458 * property data isn't large enough.
459 *
460 * dts entry of array should be like:
461 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
462 *
463 * The out_values is modified only if a valid u16 value can be decoded.
464 */
465 static inline int of_property_read_u16_array(const struct device_node *np,
466 const char *propname,
467 u16 *out_values, size_t sz)
468 {
469 int ret = of_property_read_variable_u16_array(np, propname, out_values,
470 sz, 0);
471 if (ret >= 0)
472 return 0;
473 else
474 return ret;
475 }
476
477 /**
478 * of_property_read_u32_array - Find and read an array of 32 bit integers
479 * from a property.
480 *
481 * @np: device node from which the property value is to be read.
482 * @propname: name of the property to be searched.
483 * @out_values: pointer to return value, modified only if return value is 0.
484 * @sz: number of array elements to read
485 *
486 * Search for a property in a device node and read 32-bit value(s) from
487 * it. Returns 0 on success, -EINVAL if the property does not exist,
488 * -ENODATA if property does not have a value, and -EOVERFLOW if the
489 * property data isn't large enough.
490 *
491 * The out_values is modified only if a valid u32 value can be decoded.
492 */
493 static inline int of_property_read_u32_array(const struct device_node *np,
494 const char *propname,
495 u32 *out_values, size_t sz)
496 {
497 int ret = of_property_read_variable_u32_array(np, propname, out_values,
498 sz, 0);
499 if (ret >= 0)
500 return 0;
501 else
502 return ret;
503 }
504
505 /**
506 * of_property_read_u64_array - Find and read an array of 64 bit integers
507 * from a property.
508 *
509 * @np: device node from which the property value is to be read.
510 * @propname: name of the property to be searched.
511 * @out_values: pointer to return value, modified only if return value is 0.
512 * @sz: number of array elements to read
513 *
514 * Search for a property in a device node and read 64-bit value(s) from
515 * it. Returns 0 on success, -EINVAL if the property does not exist,
516 * -ENODATA if property does not have a value, and -EOVERFLOW if the
517 * property data isn't large enough.
518 *
519 * The out_values is modified only if a valid u64 value can be decoded.
520 */
521 static inline int of_property_read_u64_array(const struct device_node *np,
522 const char *propname,
523 u64 *out_values, size_t sz)
524 {
525 int ret = of_property_read_variable_u64_array(np, propname, out_values,
526 sz, 0);
527 if (ret >= 0)
528 return 0;
529 else
530 return ret;
531 }
532
533 /*
534 * struct property *prop;
535 * const __be32 *p;
536 * u32 u;
537 *
538 * of_property_for_each_u32(np, "propname", prop, p, u)
539 * printk("U32 value: %x\n", u);
540 */
541 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
542 u32 *pu);
543 /*
544 * struct property *prop;
545 * const char *s;
546 *
547 * of_property_for_each_string(np, "propname", prop, s)
548 * printk("String value: %s\n", s);
549 */
550 const char *of_prop_next_string(struct property *prop, const char *cur);
551
552 bool of_console_check(struct device_node *dn, char *name, int index);
553
554 #else /* CONFIG_OF */
555
556 static inline void of_core_init(void)
557 {
558 }
559
560 static inline bool is_of_node(const struct fwnode_handle *fwnode)
561 {
562 return false;
563 }
564
565 static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode)
566 {
567 return NULL;
568 }
569
570 static inline const char* of_node_full_name(const struct device_node *np)
571 {
572 return "<no-node>";
573 }
574
575 static inline struct device_node *of_find_node_by_name(struct device_node *from,
576 const char *name)
577 {
578 return NULL;
579 }
580
581 static inline struct device_node *of_find_node_by_type(struct device_node *from,
582 const char *type)
583 {
584 return NULL;
585 }
586
587 static inline struct device_node *of_find_matching_node_and_match(
588 struct device_node *from,
589 const struct of_device_id *matches,
590 const struct of_device_id **match)
591 {
592 return NULL;
593 }
594
595 static inline struct device_node *of_find_node_by_path(const char *path)
596 {
597 return NULL;
598 }
599
600 static inline struct device_node *of_find_node_opts_by_path(const char *path,
601 const char **opts)
602 {
603 return NULL;
604 }
605
606 static inline struct device_node *of_find_node_by_phandle(phandle handle)
607 {
608 return NULL;
609 }
610
611 static inline struct device_node *of_get_parent(const struct device_node *node)
612 {
613 return NULL;
614 }
615
616 static inline struct device_node *of_get_next_child(
617 const struct device_node *node, struct device_node *prev)
618 {
619 return NULL;
620 }
621
622 static inline struct device_node *of_get_next_available_child(
623 const struct device_node *node, struct device_node *prev)
624 {
625 return NULL;
626 }
627
628 static inline struct device_node *of_find_node_with_property(
629 struct device_node *from, const char *prop_name)
630 {
631 return NULL;
632 }
633
634 #define of_fwnode_handle(node) NULL
635
636 static inline bool of_have_populated_dt(void)
637 {
638 return false;
639 }
640
641 static inline struct device_node *of_get_compatible_child(const struct device_node *parent,
642 const char *compatible)
643 {
644 return NULL;
645 }
646
647 static inline struct device_node *of_get_child_by_name(
648 const struct device_node *node,
649 const char *name)
650 {
651 return NULL;
652 }
653
654 static inline int of_device_is_compatible(const struct device_node *device,
655 const char *name)
656 {
657 return 0;
658 }
659
660 static inline int of_device_compatible_match(struct device_node *device,
661 const char *const *compat)
662 {
663 return 0;
664 }
665
666 static inline bool of_device_is_available(const struct device_node *device)
667 {
668 return false;
669 }
670
671 static inline bool of_device_is_big_endian(const struct device_node *device)
672 {
673 return false;
674 }
675
676 static inline struct property *of_find_property(const struct device_node *np,
677 const char *name,
678 int *lenp)
679 {
680 return NULL;
681 }
682
683 static inline struct device_node *of_find_compatible_node(
684 struct device_node *from,
685 const char *type,
686 const char *compat)
687 {
688 return NULL;
689 }
690
691 static inline int of_property_count_elems_of_size(const struct device_node *np,
692 const char *propname, int elem_size)
693 {
694 return -ENOSYS;
695 }
696
697 static inline int of_property_read_u8_array(const struct device_node *np,
698 const char *propname, u8 *out_values, size_t sz)
699 {
700 return -ENOSYS;
701 }
702
703 static inline int of_property_read_u16_array(const struct device_node *np,
704 const char *propname, u16 *out_values, size_t sz)
705 {
706 return -ENOSYS;
707 }
708
709 static inline int of_property_read_u32_array(const struct device_node *np,
710 const char *propname,
711 u32 *out_values, size_t sz)
712 {
713 return -ENOSYS;
714 }
715
716 static inline int of_property_read_u64_array(const struct device_node *np,
717 const char *propname,
718 u64 *out_values, size_t sz)
719 {
720 return -ENOSYS;
721 }
722
723 static inline int of_property_read_u32_index(const struct device_node *np,
724 const char *propname, u32 index, u32 *out_value)
725 {
726 return -ENOSYS;
727 }
728
729 static inline int of_property_read_u64_index(const struct device_node *np,
730 const char *propname, u32 index, u64 *out_value)
731 {
732 return -ENOSYS;
733 }
734
735 static inline const void *of_get_property(const struct device_node *node,
736 const char *name,
737 int *lenp)
738 {
739 return NULL;
740 }
741
742 static inline struct device_node *of_get_cpu_node(int cpu,
743 unsigned int *thread)
744 {
745 return NULL;
746 }
747
748 static inline int of_n_addr_cells(struct device_node *np)
749 {
750 return 0;
751
752 }
753 static inline int of_n_size_cells(struct device_node *np)
754 {
755 return 0;
756 }
757
758 static inline int of_property_read_variable_u8_array(const struct device_node *np,
759 const char *propname, u8 *out_values,
760 size_t sz_min, size_t sz_max)
761 {
762 return -ENOSYS;
763 }
764
765 static inline int of_property_read_variable_u16_array(const struct device_node *np,
766 const char *propname, u16 *out_values,
767 size_t sz_min, size_t sz_max)
768 {
769 return -ENOSYS;
770 }
771
772 static inline int of_property_read_variable_u32_array(const struct device_node *np,
773 const char *propname,
774 u32 *out_values,
775 size_t sz_min,
776 size_t sz_max)
777 {
778 return -ENOSYS;
779 }
780
781 static inline int of_property_read_u64(const struct device_node *np,
782 const char *propname, u64 *out_value)
783 {
784 return -ENOSYS;
785 }
786
787 static inline int of_property_read_variable_u64_array(const struct device_node *np,
788 const char *propname,
789 u64 *out_values,
790 size_t sz_min,
791 size_t sz_max)
792 {
793 return -ENOSYS;
794 }
795
796 static inline int of_property_read_string(const struct device_node *np,
797 const char *propname,
798 const char **out_string)
799 {
800 return -ENOSYS;
801 }
802
803 static inline int of_property_match_string(const struct device_node *np,
804 const char *propname,
805 const char *string)
806 {
807 return -ENOSYS;
808 }
809
810 static inline int of_property_read_string_helper(const struct device_node *np,
811 const char *propname,
812 const char **out_strs, size_t sz, int index)
813 {
814 return -ENOSYS;
815 }
816
817 static inline struct device_node *of_parse_phandle(const struct device_node *np,
818 const char *phandle_name,
819 int index)
820 {
821 return NULL;
822 }
823
824 static inline int of_parse_phandle_with_args(const struct device_node *np,
825 const char *list_name,
826 const char *cells_name,
827 int index,
828 struct of_phandle_args *out_args)
829 {
830 return -ENOSYS;
831 }
832
833 static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
834 const char *list_name, int cells_count, int index,
835 struct of_phandle_args *out_args)
836 {
837 return -ENOSYS;
838 }
839
840 static inline int of_count_phandle_with_args(struct device_node *np,
841 const char *list_name,
842 const char *cells_name)
843 {
844 return -ENOSYS;
845 }
846
847 static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
848 const struct device_node *np,
849 const char *list_name,
850 const char *cells_name,
851 int cell_count)
852 {
853 return -ENOSYS;
854 }
855
856 static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
857 {
858 return -ENOSYS;
859 }
860
861 static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
862 uint32_t *args,
863 int size)
864 {
865 return 0;
866 }
867
868 static inline int of_alias_get_id(struct device_node *np, const char *stem)
869 {
870 return -ENOSYS;
871 }
872
873 static inline int of_alias_get_highest_id(const char *stem)
874 {
875 return -ENOSYS;
876 }
877
878 static inline int of_machine_is_compatible(const char *compat)
879 {
880 return 0;
881 }
882
883 static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
884 {
885 return false;
886 }
887
888 static inline const __be32 *of_prop_next_u32(struct property *prop,
889 const __be32 *cur, u32 *pu)
890 {
891 return NULL;
892 }
893
894 static inline const char *of_prop_next_string(struct property *prop,
895 const char *cur)
896 {
897 return NULL;
898 }
899
900 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
901 {
902 return 0;
903 }
904
905 static inline int of_node_test_and_set_flag(struct device_node *n,
906 unsigned long flag)
907 {
908 return 0;
909 }
910
911 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
912 {
913 }
914
915 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
916 {
917 }
918
919 static inline int of_property_check_flag(struct property *p, unsigned long flag)
920 {
921 return 0;
922 }
923
924 static inline void of_property_set_flag(struct property *p, unsigned long flag)
925 {
926 }
927
928 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
929 {
930 }
931
932 #define of_match_ptr(_ptr) NULL
933 #define of_match_node(_matches, _node) NULL
934 #endif /* CONFIG_OF */
935
936 /* Default string compare functions, Allow arch asm/prom.h to override */
937 #if !defined(of_compat_cmp)
938 #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
939 #define of_prop_cmp(s1, s2) strcmp((s1), (s2))
940 #define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
941 #endif
942
943 #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
944 extern int of_node_to_nid(struct device_node *np);
945 #else
946 static inline int of_node_to_nid(struct device_node *device)
947 {
948 return NUMA_NO_NODE;
949 }
950 #endif
951
952 #ifdef CONFIG_OF_NUMA
953 extern int of_numa_init(void);
954 #else
955 static inline int of_numa_init(void)
956 {
957 return -ENOSYS;
958 }
959 #endif
960
961 static inline struct device_node *of_find_matching_node(
962 struct device_node *from,
963 const struct of_device_id *matches)
964 {
965 return of_find_matching_node_and_match(from, matches, NULL);
966 }
967
968 /**
969 * of_property_count_u8_elems - Count the number of u8 elements in a property
970 *
971 * @np: device node from which the property value is to be read.
972 * @propname: name of the property to be searched.
973 *
974 * Search for a property in a device node and count the number of u8 elements
975 * in it. Returns number of elements on sucess, -EINVAL if the property does
976 * not exist or its length does not match a multiple of u8 and -ENODATA if the
977 * property does not have a value.
978 */
979 static inline int of_property_count_u8_elems(const struct device_node *np,
980 const char *propname)
981 {
982 return of_property_count_elems_of_size(np, propname, sizeof(u8));
983 }
984
985 /**
986 * of_property_count_u16_elems - Count the number of u16 elements in a property
987 *
988 * @np: device node from which the property value is to be read.
989 * @propname: name of the property to be searched.
990 *
991 * Search for a property in a device node and count the number of u16 elements
992 * in it. Returns number of elements on sucess, -EINVAL if the property does
993 * not exist or its length does not match a multiple of u16 and -ENODATA if the
994 * property does not have a value.
995 */
996 static inline int of_property_count_u16_elems(const struct device_node *np,
997 const char *propname)
998 {
999 return of_property_count_elems_of_size(np, propname, sizeof(u16));
1000 }
1001
1002 /**
1003 * of_property_count_u32_elems - Count the number of u32 elements in a property
1004 *
1005 * @np: device node from which the property value is to be read.
1006 * @propname: name of the property to be searched.
1007 *
1008 * Search for a property in a device node and count the number of u32 elements
1009 * in it. Returns number of elements on sucess, -EINVAL if the property does
1010 * not exist or its length does not match a multiple of u32 and -ENODATA if the
1011 * property does not have a value.
1012 */
1013 static inline int of_property_count_u32_elems(const struct device_node *np,
1014 const char *propname)
1015 {
1016 return of_property_count_elems_of_size(np, propname, sizeof(u32));
1017 }
1018
1019 /**
1020 * of_property_count_u64_elems - Count the number of u64 elements in a property
1021 *
1022 * @np: device node from which the property value is to be read.
1023 * @propname: name of the property to be searched.
1024 *
1025 * Search for a property in a device node and count the number of u64 elements
1026 * in it. Returns number of elements on sucess, -EINVAL if the property does
1027 * not exist or its length does not match a multiple of u64 and -ENODATA if the
1028 * property does not have a value.
1029 */
1030 static inline int of_property_count_u64_elems(const struct device_node *np,
1031 const char *propname)
1032 {
1033 return of_property_count_elems_of_size(np, propname, sizeof(u64));
1034 }
1035
1036 /**
1037 * of_property_read_string_array() - Read an array of strings from a multiple
1038 * strings property.
1039 * @np: device node from which the property value is to be read.
1040 * @propname: name of the property to be searched.
1041 * @out_strs: output array of string pointers.
1042 * @sz: number of array elements to read.
1043 *
1044 * Search for a property in a device tree node and retrieve a list of
1045 * terminated string values (pointer to data, not a copy) in that property.
1046 *
1047 * If @out_strs is NULL, the number of strings in the property is returned.
1048 */
1049 static inline int of_property_read_string_array(const struct device_node *np,
1050 const char *propname, const char **out_strs,
1051 size_t sz)
1052 {
1053 return of_property_read_string_helper(np, propname, out_strs, sz, 0);
1054 }
1055
1056 /**
1057 * of_property_count_strings() - Find and return the number of strings from a
1058 * multiple strings property.
1059 * @np: device node from which the property value is to be read.
1060 * @propname: name of the property to be searched.
1061 *
1062 * Search for a property in a device tree node and retrieve the number of null
1063 * terminated string contain in it. Returns the number of strings on
1064 * success, -EINVAL if the property does not exist, -ENODATA if property
1065 * does not have a value, and -EILSEQ if the string is not null-terminated
1066 * within the length of the property data.
1067 */
1068 static inline int of_property_count_strings(const struct device_node *np,
1069 const char *propname)
1070 {
1071 return of_property_read_string_helper(np, propname, NULL, 0, 0);
1072 }
1073
1074 /**
1075 * of_property_read_string_index() - Find and read a string from a multiple
1076 * strings property.
1077 * @np: device node from which the property value is to be read.
1078 * @propname: name of the property to be searched.
1079 * @index: index of the string in the list of strings
1080 * @out_string: pointer to null terminated return string, modified only if
1081 * return value is 0.
1082 *
1083 * Search for a property in a device tree node and retrieve a null
1084 * terminated string value (pointer to data, not a copy) in the list of strings
1085 * contained in that property.
1086 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1087 * property does not have a value, and -EILSEQ if the string is not
1088 * null-terminated within the length of the property data.
1089 *
1090 * The out_string pointer is modified only if a valid string can be decoded.
1091 */
1092 static inline int of_property_read_string_index(const struct device_node *np,
1093 const char *propname,
1094 int index, const char **output)
1095 {
1096 int rc = of_property_read_string_helper(np, propname, output, 1, index);
1097 return rc < 0 ? rc : 0;
1098 }
1099
1100 /**
1101 * of_property_read_bool - Findfrom a property
1102 * @np: device node from which the property value is to be read.
1103 * @propname: name of the property to be searched.
1104 *
1105 * Search for a property in a device node.
1106 * Returns true if the property exists false otherwise.
1107 */
1108 static inline bool of_property_read_bool(const struct device_node *np,
1109 const char *propname)
1110 {
1111 struct property *prop = of_find_property(np, propname, NULL);
1112
1113 return prop ? true : false;
1114 }
1115
1116 static inline int of_property_read_u8(const struct device_node *np,
1117 const char *propname,
1118 u8 *out_value)
1119 {
1120 return of_property_read_u8_array(np, propname, out_value, 1);
1121 }
1122
1123 static inline int of_property_read_u16(const struct device_node *np,
1124 const char *propname,
1125 u16 *out_value)
1126 {
1127 return of_property_read_u16_array(np, propname, out_value, 1);
1128 }
1129
1130 static inline int of_property_read_u32(const struct device_node *np,
1131 const char *propname,
1132 u32 *out_value)
1133 {
1134 return of_property_read_u32_array(np, propname, out_value, 1);
1135 }
1136
1137 static inline int of_property_read_s32(const struct device_node *np,
1138 const char *propname,
1139 s32 *out_value)
1140 {
1141 return of_property_read_u32(np, propname, (u32*) out_value);
1142 }
1143
1144 #define of_for_each_phandle(it, err, np, ln, cn, cc) \
1145 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
1146 err = of_phandle_iterator_next(it); \
1147 err == 0; \
1148 err = of_phandle_iterator_next(it))
1149
1150 #define of_property_for_each_u32(np, propname, prop, p, u) \
1151 for (prop = of_find_property(np, propname, NULL), \
1152 p = of_prop_next_u32(prop, NULL, &u); \
1153 p; \
1154 p = of_prop_next_u32(prop, p, &u))
1155
1156 #define of_property_for_each_string(np, propname, prop, s) \
1157 for (prop = of_find_property(np, propname, NULL), \
1158 s = of_prop_next_string(prop, NULL); \
1159 s; \
1160 s = of_prop_next_string(prop, s))
1161
1162 #define for_each_node_by_name(dn, name) \
1163 for (dn = of_find_node_by_name(NULL, name); dn; \
1164 dn = of_find_node_by_name(dn, name))
1165 #define for_each_node_by_type(dn, type) \
1166 for (dn = of_find_node_by_type(NULL, type); dn; \
1167 dn = of_find_node_by_type(dn, type))
1168 #define for_each_compatible_node(dn, type, compatible) \
1169 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1170 dn = of_find_compatible_node(dn, type, compatible))
1171 #define for_each_matching_node(dn, matches) \
1172 for (dn = of_find_matching_node(NULL, matches); dn; \
1173 dn = of_find_matching_node(dn, matches))
1174 #define for_each_matching_node_and_match(dn, matches, match) \
1175 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1176 dn; dn = of_find_matching_node_and_match(dn, matches, match))
1177
1178 #define for_each_child_of_node(parent, child) \
1179 for (child = of_get_next_child(parent, NULL); child != NULL; \
1180 child = of_get_next_child(parent, child))
1181 #define for_each_available_child_of_node(parent, child) \
1182 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1183 child = of_get_next_available_child(parent, child))
1184
1185 #define for_each_node_with_property(dn, prop_name) \
1186 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1187 dn = of_find_node_with_property(dn, prop_name))
1188
1189 static inline int of_get_child_count(const struct device_node *np)
1190 {
1191 struct device_node *child;
1192 int num = 0;
1193
1194 for_each_child_of_node(np, child)
1195 num++;
1196
1197 return num;
1198 }
1199
1200 static inline int of_get_available_child_count(const struct device_node *np)
1201 {
1202 struct device_node *child;
1203 int num = 0;
1204
1205 for_each_available_child_of_node(np, child)
1206 num++;
1207
1208 return num;
1209 }
1210
1211 #if defined(CONFIG_OF) && !defined(MODULE)
1212 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1213 static const struct of_device_id __of_table_##name \
1214 __used __section(__##table##_of_table) \
1215 = { .compatible = compat, \
1216 .data = (fn == (fn_type)NULL) ? fn : fn }
1217 #else
1218 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1219 static const struct of_device_id __of_table_##name \
1220 __attribute__((unused)) \
1221 = { .compatible = compat, \
1222 .data = (fn == (fn_type)NULL) ? fn : fn }
1223 #endif
1224
1225 typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
1226 typedef int (*of_init_fn_1_ret)(struct device_node *);
1227 typedef void (*of_init_fn_1)(struct device_node *);
1228
1229 #define OF_DECLARE_1(table, name, compat, fn) \
1230 _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
1231 #define OF_DECLARE_1_RET(table, name, compat, fn) \
1232 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
1233 #define OF_DECLARE_2(table, name, compat, fn) \
1234 _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1235
1236 /**
1237 * struct of_changeset_entry - Holds a changeset entry
1238 *
1239 * @node: list_head for the log list
1240 * @action: notifier action
1241 * @np: pointer to the device node affected
1242 * @prop: pointer to the property affected
1243 * @old_prop: hold a pointer to the original property
1244 *
1245 * Every modification of the device tree during a changeset
1246 * is held in a list of of_changeset_entry structures.
1247 * That way we can recover from a partial application, or we can
1248 * revert the changeset
1249 */
1250 struct of_changeset_entry {
1251 struct list_head node;
1252 unsigned long action;
1253 struct device_node *np;
1254 struct property *prop;
1255 struct property *old_prop;
1256 };
1257
1258 /**
1259 * struct of_changeset - changeset tracker structure
1260 *
1261 * @entries: list_head for the changeset entries
1262 *
1263 * changesets are a convenient way to apply bulk changes to the
1264 * live tree. In case of an error, changes are rolled-back.
1265 * changesets live on after initial application, and if not
1266 * destroyed after use, they can be reverted in one single call.
1267 */
1268 struct of_changeset {
1269 struct list_head entries;
1270 };
1271
1272 enum of_reconfig_change {
1273 OF_RECONFIG_NO_CHANGE = 0,
1274 OF_RECONFIG_CHANGE_ADD,
1275 OF_RECONFIG_CHANGE_REMOVE,
1276 };
1277
1278 #ifdef CONFIG_OF_DYNAMIC
1279 extern int of_reconfig_notifier_register(struct notifier_block *);
1280 extern int of_reconfig_notifier_unregister(struct notifier_block *);
1281 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1282 extern int of_reconfig_get_state_change(unsigned long action,
1283 struct of_reconfig_data *arg);
1284
1285 extern void of_changeset_init(struct of_changeset *ocs);
1286 extern void of_changeset_destroy(struct of_changeset *ocs);
1287 extern int of_changeset_apply(struct of_changeset *ocs);
1288 extern int of_changeset_revert(struct of_changeset *ocs);
1289 extern int of_changeset_action(struct of_changeset *ocs,
1290 unsigned long action, struct device_node *np,
1291 struct property *prop);
1292
1293 static inline int of_changeset_attach_node(struct of_changeset *ocs,
1294 struct device_node *np)
1295 {
1296 return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1297 }
1298
1299 static inline int of_changeset_detach_node(struct of_changeset *ocs,
1300 struct device_node *np)
1301 {
1302 return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1303 }
1304
1305 static inline int of_changeset_add_property(struct of_changeset *ocs,
1306 struct device_node *np, struct property *prop)
1307 {
1308 return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1309 }
1310
1311 static inline int of_changeset_remove_property(struct of_changeset *ocs,
1312 struct device_node *np, struct property *prop)
1313 {
1314 return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1315 }
1316
1317 static inline int of_changeset_update_property(struct of_changeset *ocs,
1318 struct device_node *np, struct property *prop)
1319 {
1320 return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1321 }
1322 #else /* CONFIG_OF_DYNAMIC */
1323 static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1324 {
1325 return -EINVAL;
1326 }
1327 static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1328 {
1329 return -EINVAL;
1330 }
1331 static inline int of_reconfig_notify(unsigned long action,
1332 struct of_reconfig_data *arg)
1333 {
1334 return -EINVAL;
1335 }
1336 static inline int of_reconfig_get_state_change(unsigned long action,
1337 struct of_reconfig_data *arg)
1338 {
1339 return -EINVAL;
1340 }
1341 #endif /* CONFIG_OF_DYNAMIC */
1342
1343 /**
1344 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
1345 * @np: Pointer to the given device_node
1346 *
1347 * return true if present false otherwise
1348 */
1349 static inline bool of_device_is_system_power_controller(const struct device_node *np)
1350 {
1351 return of_property_read_bool(np, "system-power-controller");
1352 }
1353
1354 /**
1355 * Overlay support
1356 */
1357
1358 enum of_overlay_notify_action {
1359 OF_OVERLAY_PRE_APPLY = 0,
1360 OF_OVERLAY_POST_APPLY,
1361 OF_OVERLAY_PRE_REMOVE,
1362 OF_OVERLAY_POST_REMOVE,
1363 };
1364
1365 struct of_overlay_notify_data {
1366 struct device_node *overlay;
1367 struct device_node *target;
1368 };
1369
1370 #ifdef CONFIG_OF_OVERLAY
1371
1372 /* ID based overlays; the API for external users */
1373 int of_overlay_apply(struct device_node *tree, int *ovcs_id);
1374 int of_overlay_remove(int *ovcs_id);
1375 int of_overlay_remove_all(void);
1376
1377 int of_overlay_notifier_register(struct notifier_block *nb);
1378 int of_overlay_notifier_unregister(struct notifier_block *nb);
1379
1380 #else
1381
1382 static inline int of_overlay_apply(struct device_node *tree, int *ovcs_id)
1383 {
1384 return -ENOTSUPP;
1385 }
1386
1387 static inline int of_overlay_remove(int *ovcs_id)
1388 {
1389 return -ENOTSUPP;
1390 }
1391
1392 static inline int of_overlay_remove_all(void)
1393 {
1394 return -ENOTSUPP;
1395 }
1396
1397 static inline int of_overlay_notifier_register(struct notifier_block *nb)
1398 {
1399 return 0;
1400 }
1401
1402 static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1403 {
1404 return 0;
1405 }
1406
1407 #endif
1408
1409 #endif /* _LINUX_OF_H */