1#ifndef _LINUX_OF_H
2#define _LINUX_OF_H
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
28#include <asm/byteorder.h>
29#include <asm/errno.h>
30
31#include <linux/rh_kabi.h>
32
33typedef u32 phandle;
34typedef u32 ihandle;
35
36struct property {
37 char *name;
38 int length;
39 void *value;
40 struct property *next;
41 unsigned long _flags;
42 unsigned int unique_id;
43 RH_KABI_EXTEND(struct bin_attribute attr)
44};
45
46#if defined(CONFIG_SPARC)
47struct of_irq_controller;
48#endif
49
50struct device_node {
51 const char *name;
52 const char *type;
53 phandle phandle;
54 const char *full_name;
55
56 struct property *properties;
57 struct property *deadprops;
58 struct device_node *parent;
59 struct device_node *child;
60 struct device_node *sibling;
61 struct device_node *next;
62 struct device_node *allnext;
63 struct proc_dir_entry *pde;
64 struct kref kref;
65 unsigned long _flags;
66 void *data;
67#if defined(CONFIG_SPARC)
68 const char *path_component_name;
69 unsigned int unique_id;
70 struct of_irq_controller *irq_trans;
71#endif
72 RH_KABI_EXTEND(struct kobject kobj)
73 RH_KABI_EXTEND(struct fwnode_handle fwnode)
74};
75
76#define MAX_PHANDLE_ARGS 8
77struct of_phandle_args {
78 struct device_node *np;
79 int args_count;
80 uint32_t args[MAX_PHANDLE_ARGS];
81};
82
83extern int of_node_add(struct device_node *node);
84
85
86extern struct kobj_type of_node_ktype;
87static inline void of_node_init(struct device_node *node)
88{
89 kobject_init(&node->kobj, &of_node_ktype);
90 node->fwnode.type = FWNODE_OF;
91}
92
93
94static inline int of_node_is_initialized(struct device_node *node)
95{
96 return node && node->kobj.state_initialized;
97}
98
99
100static inline int of_node_is_attached(struct device_node *node)
101{
102 return node && node->kobj.state_in_sysfs;
103}
104
105#ifdef CONFIG_OF_DYNAMIC
106extern struct device_node *of_node_get(struct device_node *node);
107extern void of_node_put(struct device_node *node);
108#else
109
110static inline struct device_node *of_node_get(struct device_node *node)
111{
112 return node;
113}
114static inline void of_node_put(struct device_node *node) { }
115#endif
116
117#ifdef CONFIG_OF
118
119
120extern struct device_node *of_allnodes;
121extern struct device_node *of_chosen;
122extern struct device_node *of_aliases;
123extern raw_spinlock_t devtree_lock;
124
125static inline bool is_of_node(struct fwnode_handle *fwnode)
126{
127 return fwnode && fwnode->type == FWNODE_OF;
128}
129
130static inline struct device_node *to_of_node(struct fwnode_handle *fwnode)
131{
132 return fwnode ? container_of(fwnode, struct device_node, fwnode) : NULL;
133}
134
135static inline bool of_have_populated_dt(void)
136{
137 return of_allnodes != NULL;
138}
139
140static inline bool of_node_is_root(const struct device_node *node)
141{
142 return node && (node->parent == NULL);
143}
144
145static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
146{
147 return test_bit(flag, &n->_flags);
148}
149
150static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
151{
152 set_bit(flag, &n->_flags);
153}
154
155static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
156{
157 clear_bit(flag, &n->_flags);
158}
159
160static inline int of_property_check_flag(struct property *p, unsigned long flag)
161{
162 return test_bit(flag, &p->_flags);
163}
164
165static inline void of_property_set_flag(struct property *p, unsigned long flag)
166{
167 set_bit(flag, &p->_flags);
168}
169
170static inline void of_property_clear_flag(struct property *p, unsigned long flag)
171{
172 clear_bit(flag, &p->_flags);
173}
174
175extern struct device_node *__of_find_all_nodes(struct device_node *prev);
176extern struct device_node *of_find_all_nodes(struct device_node *prev);
177
178
179
180
181
182
183static inline u64 of_read_number(const __be32 *cell, int size)
184{
185 u64 r = 0;
186 while (size--)
187 r = (r << 32) | be32_to_cpu(*(cell++));
188 return r;
189}
190
191
192static inline unsigned long of_read_ulong(const __be32 *cell, int size)
193{
194
195 return of_read_number(cell, size);
196}
197
198#include <asm/prom.h>
199
200
201#if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
202#define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
203#define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
204#endif
205
206
207#define OF_DYNAMIC 1
208#define OF_DETACHED 2
209
210#define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
211#define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
212
213#define OF_BAD_ADDR ((u64)-1)
214
215static inline const char *of_node_full_name(const struct device_node *np)
216{
217 return np ? np->full_name : "<no-node>";
218}
219
220#define for_each_of_allnodes_from(from, dn) \
221 for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
222#define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
223extern struct device_node *of_find_node_by_name(struct device_node *from,
224 const char *name);
225extern struct device_node *of_find_node_by_type(struct device_node *from,
226 const char *type);
227extern struct device_node *of_find_compatible_node(struct device_node *from,
228 const char *type, const char *compat);
229extern struct device_node *of_find_matching_node_and_match(
230 struct device_node *from,
231 const struct of_device_id *matches,
232 const struct of_device_id **match);
233
234extern struct device_node *of_find_node_by_path(const char *path);
235extern struct device_node *of_find_node_by_phandle(phandle handle);
236extern struct device_node *of_get_parent(const struct device_node *node);
237extern struct device_node *of_get_next_parent(struct device_node *node);
238extern struct device_node *of_get_next_child(const struct device_node *node,
239 struct device_node *prev);
240extern struct device_node *of_get_next_available_child(
241 const struct device_node *node, struct device_node *prev);
242
243extern struct device_node *of_get_child_by_name(const struct device_node *node,
244 const char *name);
245
246extern struct device_node *of_find_node_with_property(
247 struct device_node *from, const char *prop_name);
248
249extern struct property *of_find_property(const struct device_node *np,
250 const char *name,
251 int *lenp);
252extern int of_property_count_elems_of_size(const struct device_node *np,
253 const char *propname, int elem_size);
254extern int of_property_read_u32_index(const struct device_node *np,
255 const char *propname,
256 u32 index, u32 *out_value);
257extern int of_property_read_u8_array(const struct device_node *np,
258 const char *propname, u8 *out_values, size_t sz);
259extern int of_property_read_u16_array(const struct device_node *np,
260 const char *propname, u16 *out_values, size_t sz);
261extern int of_property_read_u32_array(const struct device_node *np,
262 const char *propname,
263 u32 *out_values,
264 size_t sz);
265extern int of_property_read_u64(const struct device_node *np,
266 const char *propname, u64 *out_value);
267extern int of_property_read_u64_array(const struct device_node *np,
268 const char *propname,
269 u64 *out_values,
270 size_t sz);
271
272extern int of_property_read_string(struct device_node *np,
273 const char *propname,
274 const char **out_string);
275extern int of_property_match_string(struct device_node *np,
276 const char *propname,
277 const char *string);
278extern int of_property_read_string_helper(struct device_node *np,
279 const char *propname,
280 const char **out_strs, size_t sz, int index);
281extern int of_device_is_compatible(const struct device_node *device,
282 const char *);
283extern int of_device_is_available(const struct device_node *device);
284extern const void *of_get_property(const struct device_node *node,
285 const char *name,
286 int *lenp);
287#define for_each_property_of_node(dn, pp) \
288 for (pp = dn->properties; pp != NULL; pp = pp->next)
289
290extern int of_n_addr_cells(struct device_node *np);
291extern int of_n_size_cells(struct device_node *np);
292extern const struct of_device_id *of_match_node(
293 const struct of_device_id *matches, const struct device_node *node);
294extern int of_modalias_node(struct device_node *node, char *modalias, int len);
295extern struct device_node *of_parse_phandle(const struct device_node *np,
296 const char *phandle_name,
297 int index);
298extern int of_parse_phandle_with_args(const struct device_node *np,
299 const char *list_name, const char *cells_name, int index,
300 struct of_phandle_args *out_args);
301extern int of_count_phandle_with_args(const struct device_node *np,
302 const char *list_name, const char *cells_name);
303
304extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
305extern int of_alias_get_id(struct device_node *np, const char *stem);
306
307extern int of_machine_is_compatible(const char *compat);
308
309extern int of_add_property(struct device_node *np, struct property *prop);
310extern int of_remove_property(struct device_node *np, struct property *prop);
311extern int of_update_property(struct device_node *np, struct property *newprop);
312
313
314#define OF_RECONFIG_ATTACH_NODE 0x0001
315#define OF_RECONFIG_DETACH_NODE 0x0002
316#define OF_RECONFIG_ADD_PROPERTY 0x0003
317#define OF_RECONFIG_REMOVE_PROPERTY 0x0004
318#define OF_RECONFIG_UPDATE_PROPERTY 0x0005
319
320struct of_prop_reconfig {
321 struct device_node *dn;
322 struct property *prop;
323};
324
325extern int of_reconfig_notifier_register(struct notifier_block *);
326extern int of_reconfig_notifier_unregister(struct notifier_block *);
327extern int of_reconfig_notify(unsigned long, void *);
328
329extern int of_attach_node(struct device_node *);
330extern int of_detach_node(struct device_node *);
331
332#define of_match_ptr(_ptr) (_ptr)
333
334
335
336
337
338
339
340
341
342const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
343 u32 *pu);
344
345
346
347
348
349
350
351const char *of_prop_next_string(struct property *prop, const char *cur);
352
353#else
354
355static inline bool is_of_node(struct fwnode_handle *fwnode)
356{
357 return false;
358}
359
360static inline struct device_node *to_of_node(struct fwnode_handle *fwnode)
361{
362 return NULL;
363}
364
365static inline const char* of_node_full_name(struct device_node *np)
366{
367 return "<no-node>";
368}
369
370static inline struct device_node *of_find_node_by_name(struct device_node *from,
371 const char *name)
372{
373 return NULL;
374}
375
376static inline struct device_node *of_find_node_by_type(struct device_node *from,
377 const char *type)
378{
379 return NULL;
380}
381
382static inline struct device_node *of_find_matching_node_and_match(
383 struct device_node *from,
384 const struct of_device_id *matches,
385 const struct of_device_id **match)
386{
387 return NULL;
388}
389
390static inline struct device_node *of_get_parent(const struct device_node *node)
391{
392 return NULL;
393}
394
395static inline struct device_node *of_get_next_child(
396 const struct device_node *node, struct device_node *prev)
397{
398 return NULL;
399}
400
401static inline struct device_node *of_get_next_available_child(
402 const struct device_node *node, struct device_node *prev)
403{
404 return NULL;
405}
406
407static inline struct device_node *of_find_node_with_property(
408 struct device_node *from, const char *prop_name)
409{
410 return NULL;
411}
412
413static inline bool of_have_populated_dt(void)
414{
415 return false;
416}
417
418static inline struct device_node *of_get_child_by_name(
419 const struct device_node *node,
420 const char *name)
421{
422 return NULL;
423}
424
425static inline int of_device_is_compatible(const struct device_node *device,
426 const char *name)
427{
428 return 0;
429}
430
431static inline int of_device_is_available(const struct device_node *device)
432{
433 return 0;
434}
435
436static inline struct property *of_find_property(const struct device_node *np,
437 const char *name,
438 int *lenp)
439{
440 return NULL;
441}
442
443static inline struct device_node *of_find_compatible_node(
444 struct device_node *from,
445 const char *type,
446 const char *compat)
447{
448 return NULL;
449}
450
451static inline int of_property_count_elems_of_size(const struct device_node *np,
452 const char *propname, int elem_size)
453{
454 return -ENOSYS;
455}
456
457static inline int of_property_read_u32_index(const struct device_node *np,
458 const char *propname, u32 index, u32 *out_value)
459{
460 return -ENOSYS;
461}
462
463static inline int of_property_read_u8_array(const struct device_node *np,
464 const char *propname, u8 *out_values, size_t sz)
465{
466 return -ENOSYS;
467}
468
469static inline int of_property_read_u16_array(const struct device_node *np,
470 const char *propname, u16 *out_values, size_t sz)
471{
472 return -ENOSYS;
473}
474
475static inline int of_property_read_u32_array(const struct device_node *np,
476 const char *propname,
477 u32 *out_values, size_t sz)
478{
479 return -ENOSYS;
480}
481
482static inline int of_property_read_u64_array(const struct device_node *np,
483 const char *propname,
484 u64 *out_values, size_t sz)
485{
486 return -ENOSYS;
487}
488
489static inline int of_property_read_string(struct device_node *np,
490 const char *propname,
491 const char **out_string)
492{
493 return -ENOSYS;
494}
495
496static inline int of_property_read_string_helper(struct device_node *np,
497 const char *propname,
498 const char **out_strs, size_t sz, int index)
499{
500 return -ENOSYS;
501}
502
503static inline const void *of_get_property(const struct device_node *node,
504 const char *name,
505 int *lenp)
506{
507 return NULL;
508}
509
510static inline int of_property_read_u64(const struct device_node *np,
511 const char *propname, u64 *out_value)
512{
513 return -ENOSYS;
514}
515
516static inline int of_property_match_string(struct device_node *np,
517 const char *propname,
518 const char *string)
519{
520 return -ENOSYS;
521}
522
523static inline struct device_node *of_parse_phandle(const struct device_node *np,
524 const char *phandle_name,
525 int index)
526{
527 return NULL;
528}
529
530static inline int of_parse_phandle_with_args(struct device_node *np,
531 const char *list_name,
532 const char *cells_name,
533 int index,
534 struct of_phandle_args *out_args)
535{
536 return -ENOSYS;
537}
538
539static inline int of_count_phandle_with_args(struct device_node *np,
540 const char *list_name,
541 const char *cells_name)
542{
543 return -ENOSYS;
544}
545
546static inline int of_alias_get_id(struct device_node *np, const char *stem)
547{
548 return -ENOSYS;
549}
550
551static inline int of_machine_is_compatible(const char *compat)
552{
553 return 0;
554}
555
556static inline const __be32 *of_prop_next_u32(struct property *prop,
557 const __be32 *cur, u32 *pu)
558{
559 return NULL;
560}
561
562static inline const char *of_prop_next_string(struct property *prop,
563 const char *cur)
564{
565 return NULL;
566}
567
568#define of_match_ptr(_ptr) NULL
569#define of_match_node(_matches, _node) NULL
570#endif
571
572
573#if !defined(of_compat_cmp)
574#define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
575#define of_prop_cmp(s1, s2) strcmp((s1), (s2))
576#define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
577#endif
578
579#if defined(CONFIG_OF) && defined(CONFIG_NUMA)
580extern int of_node_to_nid(struct device_node *np);
581#else
582static inline int of_node_to_nid(struct device_node *device)
583{
584 return NUMA_NO_NODE;
585}
586#endif
587
588static inline struct device_node *of_find_matching_node(
589 struct device_node *from,
590 const struct of_device_id *matches)
591{
592 return of_find_matching_node_and_match(from, matches, NULL);
593}
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608static inline int of_property_read_string_array(struct device_node *np,
609 const char *propname, const char **out_strs,
610 size_t sz)
611{
612 return of_property_read_string_helper(np, propname, out_strs, sz, 0);
613}
614
615
616
617
618
619
620
621
622
623
624
625
626
627static inline int of_property_count_strings(struct device_node *np,
628 const char *propname)
629{
630 return of_property_read_string_helper(np, propname, NULL, 0, 0);
631}
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651static inline int of_property_read_string_index(struct device_node *np,
652 const char *propname,
653 int index, const char **output)
654{
655 int rc = of_property_read_string_helper(np, propname, output, 1, index);
656 return rc < 0 ? rc : 0;
657}
658
659
660
661
662
663
664
665
666
667
668
669
670static inline int of_property_count_u8_elems(const struct device_node *np,
671 const char *propname)
672{
673 return of_property_count_elems_of_size(np, propname, sizeof(u8));
674}
675
676
677
678
679
680
681
682
683
684
685
686
687static inline int of_property_count_u16_elems(const struct device_node *np,
688 const char *propname)
689{
690 return of_property_count_elems_of_size(np, propname, sizeof(u16));
691}
692
693
694
695
696
697
698
699
700
701
702
703
704static inline int of_property_count_u32_elems(const struct device_node *np,
705 const char *propname)
706{
707 return of_property_count_elems_of_size(np, propname, sizeof(u32));
708}
709
710
711
712
713
714
715
716
717
718
719
720
721static inline int of_property_count_u64_elems(const struct device_node *np,
722 const char *propname)
723{
724 return of_property_count_elems_of_size(np, propname, sizeof(u64));
725}
726
727
728
729
730
731
732
733
734
735static inline bool of_property_read_bool(const struct device_node *np,
736 const char *propname)
737{
738 struct property *prop = of_find_property(np, propname, NULL);
739
740 return prop ? true : false;
741}
742
743static inline int of_property_read_u8(const struct device_node *np,
744 const char *propname,
745 u8 *out_value)
746{
747 return of_property_read_u8_array(np, propname, out_value, 1);
748}
749
750static inline int of_property_read_u16(const struct device_node *np,
751 const char *propname,
752 u16 *out_value)
753{
754 return of_property_read_u16_array(np, propname, out_value, 1);
755}
756
757static inline int of_property_read_u32(const struct device_node *np,
758 const char *propname,
759 u32 *out_value)
760{
761 return of_property_read_u32_array(np, propname, out_value, 1);
762}
763
764#define for_each_node_by_name(dn, name) \
765 for (dn = of_find_node_by_name(NULL, name); dn; \
766 dn = of_find_node_by_name(dn, name))
767#define for_each_node_by_type(dn, type) \
768 for (dn = of_find_node_by_type(NULL, type); dn; \
769 dn = of_find_node_by_type(dn, type))
770#define for_each_compatible_node(dn, type, compatible) \
771 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
772 dn = of_find_compatible_node(dn, type, compatible))
773#define for_each_matching_node(dn, matches) \
774 for (dn = of_find_matching_node(NULL, matches); dn; \
775 dn = of_find_matching_node(dn, matches))
776#define for_each_matching_node_and_match(dn, matches, match) \
777 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
778 dn; dn = of_find_matching_node_and_match(dn, matches, match))
779
780#define for_each_child_of_node(parent, child) \
781 for (child = of_get_next_child(parent, NULL); child != NULL; \
782 child = of_get_next_child(parent, child))
783#define for_each_available_child_of_node(parent, child) \
784 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
785 child = of_get_next_available_child(parent, child))
786
787#define for_each_node_with_property(dn, prop_name) \
788 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
789 dn = of_find_node_with_property(dn, prop_name))
790
791static inline int of_get_child_count(const struct device_node *np)
792{
793 struct device_node *child;
794 int num = 0;
795
796 for_each_child_of_node(np, child)
797 num++;
798
799 return num;
800}
801
802static inline int of_get_available_child_count(const struct device_node *np)
803{
804 struct device_node *child;
805 int num = 0;
806
807 for_each_available_child_of_node(np, child)
808 num++;
809
810 return num;
811}
812
813#define of_property_for_each_u32(np, propname, prop, p, u) \
814 for (prop = of_find_property(np, propname, NULL), \
815 p = of_prop_next_u32(prop, NULL, &u); \
816 p; \
817 p = of_prop_next_u32(prop, p, &u))
818
819#define of_property_for_each_string(np, propname, prop, s) \
820 for (prop = of_find_property(np, propname, NULL), \
821 s = of_prop_next_string(prop, NULL); \
822 s; \
823 s = of_prop_next_string(prop, s))
824
825#if defined(CONFIG_PROC_FS) && defined(CONFIG_PROC_DEVICETREE)
826extern void proc_device_tree_add_node(struct device_node *, struct proc_dir_entry *);
827extern void proc_device_tree_add_prop(struct proc_dir_entry *pde, struct property *prop);
828extern void proc_device_tree_remove_prop(struct proc_dir_entry *pde,
829 struct property *prop);
830extern void proc_device_tree_update_prop(struct proc_dir_entry *pde,
831 struct property *newprop,
832 struct property *oldprop);
833#endif
834
835#endif
836