]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - include/linux/of.h
mm/hotplug: invalid PFNs from pfn_to_online_page()
[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 for (; size--; cell++)
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 static inline int of_prop_val_eq(struct property *p1, struct property *p2)
944 {
945 return p1->length == p2->length &&
946 !memcmp(p1->value, p2->value, (size_t)p1->length);
947 }
948
949 #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
950 extern int of_node_to_nid(struct device_node *np);
951 #else
952 static inline int of_node_to_nid(struct device_node *device)
953 {
954 return NUMA_NO_NODE;
955 }
956 #endif
957
958 #ifdef CONFIG_OF_NUMA
959 extern int of_numa_init(void);
960 #else
961 static inline int of_numa_init(void)
962 {
963 return -ENOSYS;
964 }
965 #endif
966
967 static inline struct device_node *of_find_matching_node(
968 struct device_node *from,
969 const struct of_device_id *matches)
970 {
971 return of_find_matching_node_and_match(from, matches, NULL);
972 }
973
974 /**
975 * of_property_count_u8_elems - Count the number of u8 elements in a property
976 *
977 * @np: device node from which the property value is to be read.
978 * @propname: name of the property to be searched.
979 *
980 * Search for a property in a device node and count the number of u8 elements
981 * in it. Returns number of elements on sucess, -EINVAL if the property does
982 * not exist or its length does not match a multiple of u8 and -ENODATA if the
983 * property does not have a value.
984 */
985 static inline int of_property_count_u8_elems(const struct device_node *np,
986 const char *propname)
987 {
988 return of_property_count_elems_of_size(np, propname, sizeof(u8));
989 }
990
991 /**
992 * of_property_count_u16_elems - Count the number of u16 elements in a property
993 *
994 * @np: device node from which the property value is to be read.
995 * @propname: name of the property to be searched.
996 *
997 * Search for a property in a device node and count the number of u16 elements
998 * in it. Returns number of elements on sucess, -EINVAL if the property does
999 * not exist or its length does not match a multiple of u16 and -ENODATA if the
1000 * property does not have a value.
1001 */
1002 static inline int of_property_count_u16_elems(const struct device_node *np,
1003 const char *propname)
1004 {
1005 return of_property_count_elems_of_size(np, propname, sizeof(u16));
1006 }
1007
1008 /**
1009 * of_property_count_u32_elems - Count the number of u32 elements in a property
1010 *
1011 * @np: device node from which the property value is to be read.
1012 * @propname: name of the property to be searched.
1013 *
1014 * Search for a property in a device node and count the number of u32 elements
1015 * in it. Returns number of elements on sucess, -EINVAL if the property does
1016 * not exist or its length does not match a multiple of u32 and -ENODATA if the
1017 * property does not have a value.
1018 */
1019 static inline int of_property_count_u32_elems(const struct device_node *np,
1020 const char *propname)
1021 {
1022 return of_property_count_elems_of_size(np, propname, sizeof(u32));
1023 }
1024
1025 /**
1026 * of_property_count_u64_elems - Count the number of u64 elements in a property
1027 *
1028 * @np: device node from which the property value is to be read.
1029 * @propname: name of the property to be searched.
1030 *
1031 * Search for a property in a device node and count the number of u64 elements
1032 * in it. Returns number of elements on sucess, -EINVAL if the property does
1033 * not exist or its length does not match a multiple of u64 and -ENODATA if the
1034 * property does not have a value.
1035 */
1036 static inline int of_property_count_u64_elems(const struct device_node *np,
1037 const char *propname)
1038 {
1039 return of_property_count_elems_of_size(np, propname, sizeof(u64));
1040 }
1041
1042 /**
1043 * of_property_read_string_array() - Read an array of strings from a multiple
1044 * strings property.
1045 * @np: device node from which the property value is to be read.
1046 * @propname: name of the property to be searched.
1047 * @out_strs: output array of string pointers.
1048 * @sz: number of array elements to read.
1049 *
1050 * Search for a property in a device tree node and retrieve a list of
1051 * terminated string values (pointer to data, not a copy) in that property.
1052 *
1053 * If @out_strs is NULL, the number of strings in the property is returned.
1054 */
1055 static inline int of_property_read_string_array(const struct device_node *np,
1056 const char *propname, const char **out_strs,
1057 size_t sz)
1058 {
1059 return of_property_read_string_helper(np, propname, out_strs, sz, 0);
1060 }
1061
1062 /**
1063 * of_property_count_strings() - Find and return the number of strings from a
1064 * multiple strings property.
1065 * @np: device node from which the property value is to be read.
1066 * @propname: name of the property to be searched.
1067 *
1068 * Search for a property in a device tree node and retrieve the number of null
1069 * terminated string contain in it. Returns the number of strings on
1070 * success, -EINVAL if the property does not exist, -ENODATA if property
1071 * does not have a value, and -EILSEQ if the string is not null-terminated
1072 * within the length of the property data.
1073 */
1074 static inline int of_property_count_strings(const struct device_node *np,
1075 const char *propname)
1076 {
1077 return of_property_read_string_helper(np, propname, NULL, 0, 0);
1078 }
1079
1080 /**
1081 * of_property_read_string_index() - Find and read a string from a multiple
1082 * strings property.
1083 * @np: device node from which the property value is to be read.
1084 * @propname: name of the property to be searched.
1085 * @index: index of the string in the list of strings
1086 * @out_string: pointer to null terminated return string, modified only if
1087 * return value is 0.
1088 *
1089 * Search for a property in a device tree node and retrieve a null
1090 * terminated string value (pointer to data, not a copy) in the list of strings
1091 * contained in that property.
1092 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1093 * property does not have a value, and -EILSEQ if the string is not
1094 * null-terminated within the length of the property data.
1095 *
1096 * The out_string pointer is modified only if a valid string can be decoded.
1097 */
1098 static inline int of_property_read_string_index(const struct device_node *np,
1099 const char *propname,
1100 int index, const char **output)
1101 {
1102 int rc = of_property_read_string_helper(np, propname, output, 1, index);
1103 return rc < 0 ? rc : 0;
1104 }
1105
1106 /**
1107 * of_property_read_bool - Findfrom a property
1108 * @np: device node from which the property value is to be read.
1109 * @propname: name of the property to be searched.
1110 *
1111 * Search for a property in a device node.
1112 * Returns true if the property exists false otherwise.
1113 */
1114 static inline bool of_property_read_bool(const struct device_node *np,
1115 const char *propname)
1116 {
1117 struct property *prop = of_find_property(np, propname, NULL);
1118
1119 return prop ? true : false;
1120 }
1121
1122 static inline int of_property_read_u8(const struct device_node *np,
1123 const char *propname,
1124 u8 *out_value)
1125 {
1126 return of_property_read_u8_array(np, propname, out_value, 1);
1127 }
1128
1129 static inline int of_property_read_u16(const struct device_node *np,
1130 const char *propname,
1131 u16 *out_value)
1132 {
1133 return of_property_read_u16_array(np, propname, out_value, 1);
1134 }
1135
1136 static inline int of_property_read_u32(const struct device_node *np,
1137 const char *propname,
1138 u32 *out_value)
1139 {
1140 return of_property_read_u32_array(np, propname, out_value, 1);
1141 }
1142
1143 static inline int of_property_read_s32(const struct device_node *np,
1144 const char *propname,
1145 s32 *out_value)
1146 {
1147 return of_property_read_u32(np, propname, (u32*) out_value);
1148 }
1149
1150 #define of_for_each_phandle(it, err, np, ln, cn, cc) \
1151 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
1152 err = of_phandle_iterator_next(it); \
1153 err == 0; \
1154 err = of_phandle_iterator_next(it))
1155
1156 #define of_property_for_each_u32(np, propname, prop, p, u) \
1157 for (prop = of_find_property(np, propname, NULL), \
1158 p = of_prop_next_u32(prop, NULL, &u); \
1159 p; \
1160 p = of_prop_next_u32(prop, p, &u))
1161
1162 #define of_property_for_each_string(np, propname, prop, s) \
1163 for (prop = of_find_property(np, propname, NULL), \
1164 s = of_prop_next_string(prop, NULL); \
1165 s; \
1166 s = of_prop_next_string(prop, s))
1167
1168 #define for_each_node_by_name(dn, name) \
1169 for (dn = of_find_node_by_name(NULL, name); dn; \
1170 dn = of_find_node_by_name(dn, name))
1171 #define for_each_node_by_type(dn, type) \
1172 for (dn = of_find_node_by_type(NULL, type); dn; \
1173 dn = of_find_node_by_type(dn, type))
1174 #define for_each_compatible_node(dn, type, compatible) \
1175 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1176 dn = of_find_compatible_node(dn, type, compatible))
1177 #define for_each_matching_node(dn, matches) \
1178 for (dn = of_find_matching_node(NULL, matches); dn; \
1179 dn = of_find_matching_node(dn, matches))
1180 #define for_each_matching_node_and_match(dn, matches, match) \
1181 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1182 dn; dn = of_find_matching_node_and_match(dn, matches, match))
1183
1184 #define for_each_child_of_node(parent, child) \
1185 for (child = of_get_next_child(parent, NULL); child != NULL; \
1186 child = of_get_next_child(parent, child))
1187 #define for_each_available_child_of_node(parent, child) \
1188 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1189 child = of_get_next_available_child(parent, child))
1190
1191 #define for_each_node_with_property(dn, prop_name) \
1192 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1193 dn = of_find_node_with_property(dn, prop_name))
1194
1195 static inline int of_get_child_count(const struct device_node *np)
1196 {
1197 struct device_node *child;
1198 int num = 0;
1199
1200 for_each_child_of_node(np, child)
1201 num++;
1202
1203 return num;
1204 }
1205
1206 static inline int of_get_available_child_count(const struct device_node *np)
1207 {
1208 struct device_node *child;
1209 int num = 0;
1210
1211 for_each_available_child_of_node(np, child)
1212 num++;
1213
1214 return num;
1215 }
1216
1217 #if defined(CONFIG_OF) && !defined(MODULE)
1218 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1219 static const struct of_device_id __of_table_##name \
1220 __used __section(__##table##_of_table) \
1221 = { .compatible = compat, \
1222 .data = (fn == (fn_type)NULL) ? fn : fn }
1223 #else
1224 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1225 static const struct of_device_id __of_table_##name \
1226 __attribute__((unused)) \
1227 = { .compatible = compat, \
1228 .data = (fn == (fn_type)NULL) ? fn : fn }
1229 #endif
1230
1231 typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
1232 typedef int (*of_init_fn_1_ret)(struct device_node *);
1233 typedef void (*of_init_fn_1)(struct device_node *);
1234
1235 #define OF_DECLARE_1(table, name, compat, fn) \
1236 _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
1237 #define OF_DECLARE_1_RET(table, name, compat, fn) \
1238 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
1239 #define OF_DECLARE_2(table, name, compat, fn) \
1240 _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1241
1242 /**
1243 * struct of_changeset_entry - Holds a changeset entry
1244 *
1245 * @node: list_head for the log list
1246 * @action: notifier action
1247 * @np: pointer to the device node affected
1248 * @prop: pointer to the property affected
1249 * @old_prop: hold a pointer to the original property
1250 *
1251 * Every modification of the device tree during a changeset
1252 * is held in a list of of_changeset_entry structures.
1253 * That way we can recover from a partial application, or we can
1254 * revert the changeset
1255 */
1256 struct of_changeset_entry {
1257 struct list_head node;
1258 unsigned long action;
1259 struct device_node *np;
1260 struct property *prop;
1261 struct property *old_prop;
1262 };
1263
1264 /**
1265 * struct of_changeset - changeset tracker structure
1266 *
1267 * @entries: list_head for the changeset entries
1268 *
1269 * changesets are a convenient way to apply bulk changes to the
1270 * live tree. In case of an error, changes are rolled-back.
1271 * changesets live on after initial application, and if not
1272 * destroyed after use, they can be reverted in one single call.
1273 */
1274 struct of_changeset {
1275 struct list_head entries;
1276 };
1277
1278 enum of_reconfig_change {
1279 OF_RECONFIG_NO_CHANGE = 0,
1280 OF_RECONFIG_CHANGE_ADD,
1281 OF_RECONFIG_CHANGE_REMOVE,
1282 };
1283
1284 #ifdef CONFIG_OF_DYNAMIC
1285 extern int of_reconfig_notifier_register(struct notifier_block *);
1286 extern int of_reconfig_notifier_unregister(struct notifier_block *);
1287 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1288 extern int of_reconfig_get_state_change(unsigned long action,
1289 struct of_reconfig_data *arg);
1290
1291 extern void of_changeset_init(struct of_changeset *ocs);
1292 extern void of_changeset_destroy(struct of_changeset *ocs);
1293 extern int of_changeset_apply(struct of_changeset *ocs);
1294 extern int of_changeset_revert(struct of_changeset *ocs);
1295 extern int of_changeset_action(struct of_changeset *ocs,
1296 unsigned long action, struct device_node *np,
1297 struct property *prop);
1298
1299 static inline int of_changeset_attach_node(struct of_changeset *ocs,
1300 struct device_node *np)
1301 {
1302 return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1303 }
1304
1305 static inline int of_changeset_detach_node(struct of_changeset *ocs,
1306 struct device_node *np)
1307 {
1308 return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1309 }
1310
1311 static inline int of_changeset_add_property(struct of_changeset *ocs,
1312 struct device_node *np, struct property *prop)
1313 {
1314 return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1315 }
1316
1317 static inline int of_changeset_remove_property(struct of_changeset *ocs,
1318 struct device_node *np, struct property *prop)
1319 {
1320 return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1321 }
1322
1323 static inline int of_changeset_update_property(struct of_changeset *ocs,
1324 struct device_node *np, struct property *prop)
1325 {
1326 return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1327 }
1328 #else /* CONFIG_OF_DYNAMIC */
1329 static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1330 {
1331 return -EINVAL;
1332 }
1333 static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1334 {
1335 return -EINVAL;
1336 }
1337 static inline int of_reconfig_notify(unsigned long action,
1338 struct of_reconfig_data *arg)
1339 {
1340 return -EINVAL;
1341 }
1342 static inline int of_reconfig_get_state_change(unsigned long action,
1343 struct of_reconfig_data *arg)
1344 {
1345 return -EINVAL;
1346 }
1347 #endif /* CONFIG_OF_DYNAMIC */
1348
1349 /**
1350 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
1351 * @np: Pointer to the given device_node
1352 *
1353 * return true if present false otherwise
1354 */
1355 static inline bool of_device_is_system_power_controller(const struct device_node *np)
1356 {
1357 return of_property_read_bool(np, "system-power-controller");
1358 }
1359
1360 /**
1361 * Overlay support
1362 */
1363
1364 enum of_overlay_notify_action {
1365 OF_OVERLAY_PRE_APPLY = 0,
1366 OF_OVERLAY_POST_APPLY,
1367 OF_OVERLAY_PRE_REMOVE,
1368 OF_OVERLAY_POST_REMOVE,
1369 };
1370
1371 struct of_overlay_notify_data {
1372 struct device_node *overlay;
1373 struct device_node *target;
1374 };
1375
1376 #ifdef CONFIG_OF_OVERLAY
1377
1378 /* ID based overlays; the API for external users */
1379 int of_overlay_apply(struct device_node *tree, int *ovcs_id);
1380 int of_overlay_remove(int *ovcs_id);
1381 int of_overlay_remove_all(void);
1382
1383 int of_overlay_notifier_register(struct notifier_block *nb);
1384 int of_overlay_notifier_unregister(struct notifier_block *nb);
1385
1386 #else
1387
1388 static inline int of_overlay_apply(struct device_node *tree, int *ovcs_id)
1389 {
1390 return -ENOTSUPP;
1391 }
1392
1393 static inline int of_overlay_remove(int *ovcs_id)
1394 {
1395 return -ENOTSUPP;
1396 }
1397
1398 static inline int of_overlay_remove_all(void)
1399 {
1400 return -ENOTSUPP;
1401 }
1402
1403 static inline int of_overlay_notifier_register(struct notifier_block *nb)
1404 {
1405 return 0;
1406 }
1407
1408 static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1409 {
1410 return 0;
1411 }
1412
1413 #endif
1414
1415 #endif /* _LINUX_OF_H */