1 /*
2  * Device Tree
3  *
4  * Copyright (C) 2012 Citrix Systems, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #ifndef __XEN_DEVICE_TREE_H__
11 #define __XEN_DEVICE_TREE_H__
12 
13 #include <xen/byteorder.h>
14 
15 #include <asm/device.h>
16 #include <public/xen.h>
17 #include <public/device_tree_defs.h>
18 #include <xen/bug.h>
19 #include <xen/kernel.h>
20 #include <xen/string.h>
21 #include <xen/types.h>
22 #include <xen/list.h>
23 #include <xen/rwlock.h>
24 
25 /*
26  * Struct used for matching a device
27  */
28 struct dt_device_match {
29     const char *path;
30     const char *type;
31     const char *compatible;
32     const bool not_available;
33     /*
34      * Property name to search for. We only search for the property's
35      * existence.
36      */
37     const char *prop;
38     const void *data;
39 };
40 
41 #define __DT_MATCH_PATH(p)              .path = (p)
42 #define __DT_MATCH_TYPE(typ)            .type = (typ)
43 #define __DT_MATCH_COMPATIBLE(compat)   .compatible = (compat)
44 #define __DT_MATCH_NOT_AVAILABLE()      .not_available = 1
45 #define __DT_MATCH_PROP(p)              .prop = (p)
46 
47 #define DT_MATCH_PATH(p)                { __DT_MATCH_PATH(p) }
48 #define DT_MATCH_TYPE(typ)              { __DT_MATCH_TYPE(typ) }
49 #define DT_MATCH_COMPATIBLE(compat)     { __DT_MATCH_COMPATIBLE(compat) }
50 #define DT_MATCH_NOT_AVAILABLE()        { __DT_MATCH_NOT_AVAILABLE() }
51 #define DT_MATCH_PROP(p)                { __DT_MATCH_PROP(p) }
52 
53 typedef u32 dt_phandle;
54 
55 /**
56  * dt_property - describe a property for a device
57  * @name: name of the property
58  * @length: size of the value
59  * @value: pointer to data contained in the property
60  * @next: pointer to the next property of a specific node
61  */
62 struct dt_property {
63     const char *name;
64     u32 length;
65     void *value;
66     struct dt_property *next;
67 };
68 
69 /**
70  * dt_device_node - describe a node in the device tree
71  * @name: name of the node
72  * @type: type of the node (ie: memory, cpu, ...)
73  * @full_name: full name, it's composed of all the ascendant name separate by /
74  * @used_by: who owns the node? (ie: xen, dom0...)
75  * @properties: list of properties for the node
76  * @child: pointer to the first child
77  * @sibling: pointer to the next sibling
78  * @allnext: pointer to the next in list of all nodes
79  */
80 struct dt_device_node {
81     const char *name;
82     const char *type;
83     char *full_name;
84     dt_phandle phandle;
85     domid_t used_by; /* By default it's used by dom0 */
86 
87     /* IOMMU specific fields */
88     bool is_protected;
89 
90 #ifdef CONFIG_STATIC_EVTCHN
91     /* HACK: Remove this if there is a need of space */
92     bool static_evtchn_created;
93 #endif
94 
95     struct dt_property *properties;
96     struct dt_device_node *parent;
97     struct dt_device_node *child;
98     struct dt_device_node *sibling;
99     struct dt_device_node *next; /* TODO: Remove it. Only use to know the last children */
100     struct dt_device_node *allnext;
101 
102     /*
103      * The main purpose of this list is to link the structure in the list
104      * of devices assigned to domain.
105      *
106      * Boot code (iommu_hardware_setup) re-uses this list to link the structure
107      * in the list of devices for which driver requested deferred probing.
108      */
109     struct list_head domain_list;
110 
111 #ifdef CONFIG_HAS_DEVICE_TREE_DISCOVERY
112     struct device dev;
113 #endif /* CONFIG_HAS_DEVICE_TREE_DISCOVERY */
114 };
115 
116 #ifdef CONFIG_HAS_DEVICE_TREE_DISCOVERY
117 #define dt_to_dev(dt_node)  (&(dt_node)->dev)
118 
dev_to_dt(struct device * dev)119 static inline struct dt_device_node *dev_to_dt(struct device *dev)
120 {
121     ASSERT(dev->type == DEV_DT);
122 
123     return container_of(dev, struct dt_device_node, dev);
124 }
125 #endif /* CONFIG_HAS_DEVICE_TREE_DISCOVERY */
126 
127 #define MAX_PHANDLE_ARGS 16
128 struct dt_phandle_args {
129     struct dt_device_node *np;
130     int args_count;
131     uint32_t args[MAX_PHANDLE_ARGS];
132 };
133 
134 /**
135  * dt_irq - describe an IRQ in the device tree
136  * @irq: IRQ number
137  * @type: IRQ type (see DT_IRQ_TYPE_*)
138  *
139  * This structure is returned when an interrupt is mapped.
140  */
141 struct dt_irq {
142     unsigned int irq;
143     unsigned int type;
144 };
145 
146 /* If type == DT_IRQ_TYPE_NONE, assume we use level triggered */
dt_irq_is_level_triggered(const struct dt_irq * irq)147 static inline bool dt_irq_is_level_triggered(const struct dt_irq *irq)
148 {
149     unsigned int type = irq->type;
150 
151     return (type & DT_IRQ_TYPE_LEVEL_MASK) || (type == DT_IRQ_TYPE_NONE);
152 }
153 
154 /**
155  * dt_raw_irq - container for device_node/irq_specifier for an irq controller
156  * @controller: pointer to interrupt controller deivce tree node
157  * @size: size of interrupt specifier
158  * @specifier: array of cells @size long specifying the specific interrupt
159  *
160  * This structure is returned when an interrupt is mapped but not translated.
161  */
162 #define DT_MAX_IRQ_SPEC     4 /* We handle specifiers of at most 4 cells */
163 struct dt_raw_irq {
164     const struct dt_device_node *controller;
165     u32 size;
166     u32 specifier[DT_MAX_IRQ_SPEC];
167 };
168 
169 extern const void *device_tree_flattened;
170 
171 /**
172  * dt_unflatten_host_device_tree - Unflatten the host device tree
173  *
174  * Create a hierarchical device tree for the host DTB to be able
175  * to retrieve parents.
176  */
177 void dt_unflatten_host_device_tree(void);
178 
179 /**
180  * unflatten_device_tree - create tree of device_nodes from flat blob
181  *
182  * unflattens a device-tree, creating the
183  * tree of struct device_node. It also fills the "name" and "type"
184  * pointers of the nodes so the normal device-tree walking functions
185  * can be used.
186  * @fdt: The fdt to expand
187  * @mynodes: The device_node tree created by the call
188  *
189  * Returns 0 on success and a negative number on error
190  */
191 int unflatten_device_tree(const void *fdt, struct dt_device_node **mynodes);
192 
193 /**
194  * IRQ translation callback
195  * TODO: For the moment we assume that we only have ONE
196  * interrupt-controller.
197  */
198 typedef int (*dt_irq_xlate_func)(const u32 *intspec, unsigned int intsize,
199                                  unsigned int *out_hwirq,
200                                  unsigned int *out_type);
201 extern dt_irq_xlate_func dt_irq_xlate;
202 
203 /**
204  * Host device tree
205  * DO NOT modify it!
206  */
207 extern struct dt_device_node *dt_host;
208 
209 /**
210  * Primary interrupt controller
211  * Exynos SOC has an interrupt combiner, interrupt has no physical
212  * meaning when it's not connected to the primary controller.
213  * We will only map interrupt whose parent controller is
214  * dt_interrupt_controller. It should always be a GIC.
215  * TODO: Handle multiple GIC
216  */
217 extern const struct dt_device_node *dt_interrupt_controller;
218 
219 /*
220  * Lock that protects r/w updates to unflattened device tree i.e. dt_host during
221  * runtime. Lock may not be taken for boot only code.
222  */
223 extern rwlock_t dt_host_lock;
224 
225 /**
226  * Find the interrupt controller
227  * For the moment we handle only one interrupt controller: the first
228  * one without parent which is compatible with the string "compat".
229  *
230  * If found, return the interrupt controller device node.
231  */
232 struct dt_device_node *
233 dt_find_interrupt_controller(const struct dt_device_match *matches);
234 
235 void intc_dt_preinit(void);
236 
237 #define dt_prop_cmp(s1, s2) strcmp((s1), (s2))
238 #define dt_node_cmp(s1, s2) strcasecmp((s1), (s2))
239 #define dt_compat_cmp(s1, s2) strcasecmp((s1), (s2))
240 
241 #define dt_for_each_property_node(dn, pp)                   \
242     for ( pp = (dn)->properties; (pp) != NULL; pp = (pp)->next )
243 
244 #define dt_for_each_device_node(dt, dn)                     \
245     for ( dn = (dt); (dn) != NULL; dn = (dn)->allnext )
246 
247 #define dt_for_each_child_node(dt, dn)                      \
248     for ( dn = (dt)->child; (dn) != NULL; dn = (dn)->sibling )
249 
250 /* Helper to convert a number of cells to bytes */
dt_cells_to_size(int size)251 static inline int dt_cells_to_size(int size)
252 {
253     return (size * sizeof (u32));
254 }
255 
256 /* Helper to convert a number of bytes to cells, rounds down */
dt_size_to_cells(int bytes)257 static inline int dt_size_to_cells(int bytes)
258 {
259     return (bytes / sizeof(u32));
260 }
261 
dt_node_full_name(const struct dt_device_node * np)262 static inline const char *dt_node_full_name(const struct dt_device_node *np)
263 {
264     return (np && np->full_name) ? np->full_name : "<no-node>";
265 }
266 
dt_node_name(const struct dt_device_node * np)267 static inline const char *dt_node_name(const struct dt_device_node *np)
268 {
269     return (np && np->name) ? np->name : "<no-node>";
270 }
271 
dt_node_name_is_equal(const struct dt_device_node * np,const char * name)272 static inline bool dt_node_name_is_equal(const struct dt_device_node *np,
273                                          const char *name)
274 {
275     return !dt_node_cmp(np->name, name);
276 }
277 
dt_node_path_is_equal(const struct dt_device_node * np,const char * path)278 static inline bool dt_node_path_is_equal(const struct dt_device_node *np,
279                                          const char *path)
280 {
281     return !dt_node_cmp(np->full_name, path);
282 }
283 
284 static inline bool
dt_device_type_is_equal(const struct dt_device_node * device,const char * type)285 dt_device_type_is_equal(const struct dt_device_node *device,
286                         const char *type)
287 {
288     return !dt_node_cmp(device->type, type);
289 }
290 
dt_device_set_used_by(struct dt_device_node * device,domid_t used_by)291 static inline void dt_device_set_used_by(struct dt_device_node *device,
292                                          domid_t used_by)
293 {
294     /* TODO: children must inherit to the used_by thing */
295     device->used_by = used_by;
296 }
297 
dt_device_used_by(const struct dt_device_node * device)298 static inline domid_t dt_device_used_by(const struct dt_device_node *device)
299 {
300     return device->used_by;
301 }
302 
dt_device_set_protected(struct dt_device_node * device)303 static inline void dt_device_set_protected(struct dt_device_node *device)
304 {
305     device->is_protected = true;
306 }
307 
dt_device_is_protected(const struct dt_device_node * device)308 static inline bool dt_device_is_protected(const struct dt_device_node *device)
309 {
310     return device->is_protected;
311 }
312 
dt_property_name_is_equal(const struct dt_property * pp,const char * name)313 static inline bool dt_property_name_is_equal(const struct dt_property *pp,
314                                              const char *name)
315 {
316     return !dt_prop_cmp(pp->name, name);
317 }
318 
319 #ifdef CONFIG_STATIC_EVTCHN
320 static inline void
dt_device_set_static_evtchn_created(struct dt_device_node * device)321 dt_device_set_static_evtchn_created(struct dt_device_node *device)
322 {
323     device->static_evtchn_created = true;
324 }
325 
326 static inline bool
dt_device_static_evtchn_created(const struct dt_device_node * device)327 dt_device_static_evtchn_created(const struct dt_device_node *device)
328 {
329     return device->static_evtchn_created;
330 }
331 #endif /* CONFIG_STATIC_EVTCHN */
332 
333 /**
334  * dt_find_compatible_node - Find a node based on type and one of the
335  *                           tokens in its "compatible" property
336  * @from: The node to start searching from or NULL, the node
337  *          you pass will not be searched, only the next one
338  *          will; typically, you pass what the previous call
339  *          returned.
340  * @type: The type string to match "device_type" or NULL to ignore
341  * @compatible: The string to match to one of the tokens in the device
342  *          "compatible" list.
343  *
344  * Returns a node pointer.
345  */
346 struct dt_device_node *dt_find_compatible_node(struct dt_device_node *from,
347                                                const char *type,
348                                                const char *compatible);
349 
350 /**
351  * Find a property with a given name for a given node
352  * and return the value.
353  */
354 const void *dt_get_property(const struct dt_device_node *np,
355                             const char *name, u32 *lenp);
356 
357 const struct dt_property *dt_find_property(const struct dt_device_node *np,
358                                            const char *name, u32 *lenp);
359 
360 
361 /**
362  * dt_property_read_u32 - Helper to read a u32 property.
363  * @np: node to get the value
364  * @name: name of the property
365  * @out_value: pointer to return value
366  *
367  * Return true if get the desired value.
368  */
369 bool dt_property_read_u32(const struct dt_device_node *np,
370                           const char *name, u32 *out_value);
371 /**
372  * dt_property_read_u64 - Helper to read a u64 property.
373  * @np: node to get the value
374  * @name: name of the property
375  * @out_value: pointer to return value
376  *
377  * Return true if get the desired value.
378  */
379 bool dt_property_read_u64(const struct dt_device_node *np,
380                           const char *name, u64 *out_value);
381 
382 
383 /**
384  * dt_property_read_variable_u32_array - Find and read an array of 32 bit
385  * integers from a property, with bounds on the minimum and maximum array size.
386  *
387  * @np:     device node from which the property value is to be read.
388  * @propname:   name of the property to be searched.
389  * @out_values: pointer to return found values.
390  * @sz_min: minimum number of array elements to read
391  * @sz_max: maximum number of array elements to read, if zero there is no
392  *      upper limit on the number of elements in the dts entry but only
393  *      sz_min will be read.
394  *
395  * Search for a property in a device node and read 32-bit value(s) from
396  * it.
397  *
398  * Return: The number of elements read on success, -EINVAL if the property
399  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
400  * if the property data is smaller than sz_min or longer than sz_max.
401  *
402  * The out_values is modified only if a valid u32 value can be decoded.
403  */
404 int dt_property_read_variable_u32_array(const struct dt_device_node *np,
405                                         const char *propname, u32 *out_values,
406                                         size_t sz_min, size_t sz_max);
407 
408 /**
409  * dt_property_read_u32_array - Find and read an array of 32 bit integers
410  * from a property.
411  *
412  * @np:     device node from which the property value is to be read.
413  * @propname:   name of the property to be searched.
414  * @out_values: pointer to return value, modified only if return value is 0.
415  * @sz:     number of array elements to read
416  *
417  * Search for a property in a device node and read 32-bit value(s) from
418  * it.
419  *
420  * Return: 0 on success, -EINVAL if the property does not exist,
421  * -ENODATA if property does not have a value, and -EOVERFLOW if the
422  * property data isn't large enough.
423  *
424  * The out_values is modified only if a valid u32 value can be decoded.
425  */
dt_property_read_u32_array(const struct dt_device_node * np,const char * propname,u32 * out_values,size_t sz)426 static inline int dt_property_read_u32_array(const struct dt_device_node *np,
427                                              const char *propname,
428                                              u32 *out_values, size_t sz)
429 {
430     int ret = dt_property_read_variable_u32_array(np, propname, out_values,
431                               sz, 0);
432     if ( ret >= 0 )
433         return 0;
434     else
435         return ret;
436 }
437 
438 /**
439  * dt_property_read_bool - Check if a property exists
440  * @np: node to get the value
441  * @name: name of the property
442  *
443  * Search for a property in a device node.
444  * Return true if the property exists false otherwise.
445  */
dt_property_read_bool(const struct dt_device_node * np,const char * name)446 static inline bool dt_property_read_bool(const struct dt_device_node *np,
447                                          const char *name)
448 {
449     const struct dt_property *prop = dt_find_property(np, name, NULL);
450 
451     return prop ? true : false;
452 }
453 
454 /**
455  * dt_property_read_string - Find and read a string from a property
456  * @np:         Device node from which the property value is to be read
457  * @propname:   Name of the property to be searched
458  * @out_string: Pointer to null terminated return string, modified only
459  *              if return value if 0.
460  *
461  * Search for a property in a device tree node and retrieve a null
462  * terminated string value (pointer to data, not a copy). Returns 0 on
463  * success, -EINVAL if the property does not exist, -ENODATA if property
464  * doest not have value, and -EILSEQ if the string is not
465  * null-terminated with the length of the property data.
466  *
467  * Note that the empty string "" has length of 1, thus -ENODATA cannot
468  * be interpreted as an empty string.
469  *
470  * The out_string pointer is modified only if a valid string can be decoded.
471  */
472 int dt_property_read_string(const struct dt_device_node *np,
473                             const char *propname, const char **out_string);
474 
475 /**
476  * dt_property_match_string() - Find string in a list and return index
477  * @np: pointer to node containing string list property
478  * @propname: string list property name
479  * @string: pointer to string to search for in string list
480  *
481  * This function searches a string list property and returns the index
482  * of a specific string value.
483  */
484 int dt_property_match_string(const struct dt_device_node *np,
485                              const char *propname, const char *string);
486 
487 /**
488  * Checks if the given "compat" string matches one of the strings in
489  * the device's "compatible" property
490  */
491 bool dt_device_is_compatible(const struct dt_device_node *device,
492                              const char *compat);
493 
494 /**
495  * dt_machine_is_compatible - Test root of device tree for a given compatible value
496  * @compat: compatible string to look for in root node's compatible property.
497  *
498  * Returns true if the root node has the given value in its
499  * compatible property.
500  */
501 bool dt_machine_is_compatible(const char *compat);
502 
503 /**
504  * dt_find_node_by_name - Find a node by its "name" property
505  * @from: The node to start searching from or NULL, the node
506  * you pass will not be searched, only the next one
507  *  will; typically, you pass what the previous call
508  *  returned. of_node_put() will be called on it
509  * @name: The name string to match against
510  *
511  * Returns a node pointer with refcount incremented, use
512  * of_node_put() on it when done.
513  */
514 struct dt_device_node *dt_find_node_by_name(struct dt_device_node *from,
515                                             const char *name);
516 
517 /**
518  * dt_find_node_by_type - Find a node by its "type" property
519  */
520 struct dt_device_node *dt_find_node_by_type(struct dt_device_node *from,
521                                             const char *type);
522 
523 /**
524  * df_find_node_by_alias - Find a node matching an alias
525  * @alias: The alias to match
526  *
527  * Returns a node pointer.
528  */
529 struct dt_device_node *dt_find_node_by_alias(const char *alias);
530 
531 /**
532  * dt_find_node_by_path_from - Generic function to find a node matching the
533  * full DT path for any given unflatten device tree
534  * @from: The device tree node to start searching from
535  * @path: The full path to match
536  *
537  * Returns a node pointer.
538  */
539 struct dt_device_node *dt_find_node_by_path_from(struct dt_device_node *from,
540                                                  const char *path);
541 
542 /**
543  * dt_find_node_by_path - Find a node matching a full DT path in dt_host
544  * @path: The full path to match
545  *
546  * Returns a node pointer.
547  */
dt_find_node_by_path(const char * path)548 static inline struct dt_device_node *dt_find_node_by_path(const char *path)
549 {
550     return dt_find_node_by_path_from(dt_host, path);
551 }
552 
553 /**
554  * dt_find_node_by_gpath - Same as dt_find_node_by_path but retrieve the
555  * path from the guest
556  *
557  * @u_path: Xen Guest handle to the buffer containing the path
558  * @u_plen: Length of the buffer
559  * @node: TODO
560  *
561  * Return 0 if succeed otherwise -errno
562  */
563 int dt_find_node_by_gpath(XEN_GUEST_HANDLE(char) u_path, uint32_t u_plen,
564                           struct dt_device_node **node);
565 
566 /**
567  * dt_get_parent - Get a node's parent if any
568  * @node: Node to get parent
569  *
570  * Returns a node pointer.
571  */
572 const struct dt_device_node *dt_get_parent(const struct dt_device_node *node);
573 
574 /**
575  * dt_device_get_paddr - Resolve an address for a device
576  * @device: the device whose address is to be resolved
577  * @index: index of the address to resolve
578  * @addr: address filled by this function
579  * @size: size filled by this function
580  *
581  * This function resolves an address, walking the tree, for a given
582  * device-tree node. It returns 0 on success.
583  */
584 int dt_device_get_paddr(const struct dt_device_node *dev, unsigned int index,
585                         paddr_t *addr, paddr_t *size);
586 
587 /**
588  * dt_device_get_address - Resolve an address for a device
589  * @device: the device whose address is to be resolved
590  * @index: index of the address to resolve
591  * @addr: address filled by this function
592  * @size: size filled by this function
593  *
594  * This function resolves an address, walking the tree, for a give
595  * device-tree node. It returns 0 on success.
596  */
597 int dt_device_get_address(const struct dt_device_node *dev, unsigned int index,
598                           u64 *addr, u64 *size);
599 
600 /**
601  * dt_number_of_irq - Get the number of IRQ for a device
602  * @device: the device whose number of interrupt is to be retrieved
603  *
604  * Return the number of irq for this device or 0 if there is no
605  * interrupt or an error occurred.
606  */
607 unsigned int dt_number_of_irq(const struct dt_device_node *device);
608 
609 /**
610  * dt_number_of_address - Get the number of addresses for a device
611  * @dev: the device whose number of address is to be retrieved
612  *
613  * Return the number of address for this device or 0 if there is no
614  * address or an error occurred.
615  */
616 unsigned int dt_number_of_address(const struct dt_device_node *dev);
617 
618 /**
619  * dt_device_get_irq - Resolve an interrupt for a device
620  * @device: the device whose interrupt is to be resolved
621  * @index: index of the interrupt to resolve
622  * @out_irq: structure dt_irq filled by this function
623  *
624  * This function resolves an interrupt, walking the tree, for a given
625  * device-tree node. It's the high level pendant to dt_device_get_raw_irq().
626  */
627 int dt_device_get_irq(const struct dt_device_node *device, unsigned int index,
628                       struct dt_irq *out_irq);
629 
630 /**
631  * dt_device_get_raw_irq - Resolve an interrupt for a device without translation
632  * @device: the device whose interrupt is to be resolved
633  * @index: index of the interrupt to resolve
634  * @out_irq: structure dt_raw_irq filled by this function
635  *
636  * This function resolves an interrupt for a device, no translation is
637  * made. dt_irq_translate can be called after.
638  */
639 int dt_device_get_raw_irq(const struct dt_device_node *device,
640                           unsigned int index,
641                           struct dt_raw_irq *out_irq);
642 
643 /**
644  * dt_irq_translate - Translate an irq
645  * @raw: IRQ to translate (raw format)
646  * @out_irq: structure dt_irq filled by this function
647  */
648 int dt_irq_translate(const struct dt_raw_irq *raw, struct dt_irq *out_irq);
649 
650 /**
651  * dt_for_each_irq_map - Iterate over a nodes interrupt-map property
652  * @dev: The node whose interrupt-map property should be iterated over
653  * @cb: Call back to call for each entry
654  * @data: Caller data passed to callback
655  */
656 int dt_for_each_irq_map(const struct dt_device_node *dev,
657                         int (*cb)(const struct dt_device_node *dev,
658                                   const struct dt_irq *dt_irq,
659                                   void *data),
660                         void *data);
661 
662 /**
663  * dt_for_each_range - Iterate over a nodes ranges property
664  * @dev: The node whose interrupt-map property should be iterated over
665  * @cb: Call back to call for each entry
666  * @data: Caller data passed to callback
667  */
668 int dt_for_each_range(const struct dt_device_node *dev,
669                       int (*cb)(const struct dt_device_node *dev,
670                                 uint64_t addr, uint64_t length,
671                                 void *data),
672                       void *data);
673 
674 /**
675  * dt_n_size_cells - Helper to retrieve the number of cell for the size
676  * @np: node to get the value
677  *
678  * This function retrieves for a give device-tree node the number of
679  * cell for the size field.
680  */
681 int dt_n_size_cells(const struct dt_device_node *np);
682 
683 /**
684  * dt_n_addr_cells - Helper to retrieve the number of cell for the address
685  * @np: node to get the value
686  *
687  * This function retrieves for a give device-tree node the number of
688  * cell for the address field.
689  */
690 int dt_n_addr_cells(const struct dt_device_node *np);
691 
692 /**
693  * dt_child_n_size_cells - Helper to retrieve the number of cell for the size
694  * @parent: parent of the child to get the value
695  *
696  * This function retrieves for a given device-tree node the number of
697  * cell for the size field of there child
698  */
699 int dt_child_n_size_cells(const struct dt_device_node *parent);
700 
701 /**
702  * dt_child_n_addr_cells - Helper to retrieve the number of cell for the
703  * address
704  * @parent: parent of the child to get the value
705  *
706  * This function retrieves for a given device-tree node the number of
707  * cell for the address field of there child
708  */
709 int dt_child_n_addr_cells(const struct dt_device_node *parent);
710 
711 /**
712  * dt_device_is_available - Check if a device is available for use
713  *
714  * @device: Node to check for availability
715  *
716  * Returns true if the status property is absent or set to "okay" or "ok",
717  * false otherwise.
718  */
719 bool dt_device_is_available(const struct dt_device_node *device);
720 
721 /**
722  * dt_device_for_passthrough - Check if a device will be used for
723  * passthrough later
724  *
725  * @device: Node to check
726  *
727  * Return true if the property "xen,passthrough" is present in the node,
728  * false otherwise.
729  */
730 bool dt_device_for_passthrough(const struct dt_device_node *device);
731 
732 /**
733  * dt_match_node - Tell if a device_node has a matching of dt_device_match
734  * @matches: array of dt_device_match structures to search in
735  * @node: the dt_device_node structure to match against
736  *
737  * Returns true if the device node match one of dt_device_match.
738  */
739 const struct dt_device_match *
740 dt_match_node(const struct dt_device_match *matches,
741               const struct dt_device_node *node);
742 
743 /**
744  * dt_find_matching_node - Find a node based on an dt_device_match match table
745  * @from: The node to start searching from or NULL, the node you pass
746  *        will not be searched, only the next one will; typically, you pass
747  *        what the returned call returned
748  * @matches: array of dt_device_match structures to search in
749  *
750  * Returns a node pointer.
751  */
752 struct dt_device_node *
753 dt_find_matching_node(struct dt_device_node *from,
754                       const struct dt_device_match *matches);
755 
756 /**
757  * dt_set_cell - Write a value into a series of cells
758  *
759  * @cellp: Pointer to cells
760  * @size: number of cells to write the value
761  * @value: number to write
762  *
763  * Write a value into a series of cells and update cellp to point to the
764  * cell just after.
765  */
766 void dt_set_cell(__be32 **cellp, int size, u64 val);
767 
768 /**
769  * dt_set_range - Write range into a series of cells
770  *
771  * @cellp: Pointer to cells
772  * @np: Node which contains the encoding for the address and the size
773  * @address: Start of range
774  * @size: Size of the range
775  *
776  * Write a range into a series of cells and update cellp to point to the
777  * cell just after.
778  */
779 void dt_set_range(__be32 **cellp, const struct dt_device_node *np,
780                   u64 address, u64 size);
781 
782 /**
783  * dt_child_set_range - Write range into a series of cells
784  *
785  * @cellp: Pointer to cells
786  * @parent: Parent node which contains the encode for the address and the size
787  * @address: Start of range
788  * @size: Size of the range
789  *
790  * Write a range into a series of cells and update cellp to point to the
791  * cell just after.
792  */
793 void dt_child_set_range(__be32 **cellp, int addrcells, int sizecells,
794                         u64 address, u64 size);
795 
796 /**
797  * dt_get_range - Read a range (address/size) from a series of cells
798  *
799  * @cellp: Pointer to cells
800  * @np Node which  contains the encoding for the addresss and the size
801  * @address: Address filled by this function
802  * @size: Size filled by this function
803  *
804  * WARNING: This function should not be used to decode an address
805  * This function reads a range (address/size) from a series of cells and
806  * update cellp to point to the cell just after.
807  */
808 void dt_get_range(const __be32 **cellp, const struct dt_device_node *np,
809                   u64 *address, u64 *size);
810 
811 /**
812  * dt_parse_phandle - Resolve a phandle property to a device_node pointer
813  * @np: Pointer to device node holding phandle property
814  * @phandle_name: Name of property holding a phandle value
815  * @index: For properties holding a table of phandles, this is the index into
816  *         the table
817  *
818  * Returns the device_node pointer.
819  */
820 struct dt_device_node *dt_parse_phandle(const struct dt_device_node *np,
821 				                        const char *phandle_name,
822                                         int index);
823 
824 /**
825  * dt_parse_phandle_with_args() - Find a node pointed by phandle in a list
826  * @np:	pointer to a device tree node containing a list
827  * @list_name: property name that contains a list
828  * @cells_name: property name that specifies phandles' arguments count
829  * @index: index of a phandle to parse out
830  * @out_args: optional pointer to output arguments structure (will be filled)
831  *
832  * This function is useful to parse lists of phandles and their arguments.
833  * Returns 0 on success and fills out_args, on error returns appropriate
834  * errno value.
835  *
836  * Example:
837  *
838  * phandle1: node1 {
839  * 	#list-cells = <2>;
840  * }
841  *
842  * phandle2: node2 {
843  * 	#list-cells = <1>;
844  * }
845  *
846  * node3 {
847  * 	list = <&phandle1 1 2 &phandle2 3>;
848  * }
849  *
850  * To get a device_node of the `node2' node you may call this:
851  * dt_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
852  */
853 int dt_parse_phandle_with_args(const struct dt_device_node *np,
854                                const char *list_name,
855                                const char *cells_name, int index,
856                                struct dt_phandle_args *out_args);
857 
858 /**
859  * dt_count_phandle_with_args() - Find the number of phandles references in a property
860  * @np: pointer to a device tree node containing a list
861  * @list_name: property name that contains a list
862  * @cells_name: property name that specifies phandles' arguments count
863  *
864  * Returns the number of phandle + argument tuples within a property. It
865  * is a typical pattern to encode a list of phandle and variable
866  * arguments into a single property. The number of arguments is encoded
867  * by a property in the phandle-target node. For example, a gpios
868  * property would contain a list of GPIO specifies consisting of a
869  * phandle and 1 or more arguments. The number of arguments are
870  * determined by the #gpio-cells property in the node pointed to by the
871  * phandle.
872  */
873 int dt_count_phandle_with_args(const struct dt_device_node *np,
874                                const char *list_name,
875                                const char *cells_name);
876 
877 /**
878  * dt_get_pci_domain_nr - Find the host bridge domain number
879  *            of the given device node.
880  * @node: Device tree node with the domain information.
881  *
882  * This function will try to obtain the host bridge domain number by finding
883  * a property called "linux,pci-domain" of the given device node.
884  *
885  * Return:
886  * * > 0    - On success, an associated domain number.
887  * * -EINVAL    - The property "linux,pci-domain" does not exist.
888  *
889  * Returns the associated domain number from DT in the range [0-0xffff], or
890  * a negative value if the required property is not found.
891  */
892 int dt_get_pci_domain_nr(struct dt_device_node *node);
893 
894 /**
895  * dt_map_id - Translate an ID through a downstream mapping.
896  * @np: root complex device node.
897  * @id: device ID to map.
898  * @map_name: property name of the map to use.
899  * @map_mask_name: optional property name of the mask to use.
900  * @target: optional pointer to a target device node.
901  * @id_out: optional pointer to receive the translated ID.
902  *
903  * Given a device ID, look up the appropriate implementation-defined
904  * platform ID and/or the target device which receives transactions on that
905  * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
906  * @id_out may be NULL if only the other is required. If @target points to
907  * a non-NULL device node pointer, only entries targeting that node will be
908  * matched; if it points to a NULL value, it will receive the device node of
909  * the first matching target phandle, with a reference held.
910  *
911  * Return: 0 on success or a standard error code on failure.
912  */
913 int dt_map_id(const struct dt_device_node *np, uint32_t id,
914               const char *map_name, const char *map_mask_name,
915               struct dt_device_node **target, uint32_t *id_out);
916 
917 struct dt_device_node *dt_find_node_by_phandle(dt_phandle handle);
918 
919 #ifdef CONFIG_DEVICE_TREE_DEBUG
920 #define dt_dprintk(fmt, args...)  \
921     printk(XENLOG_DEBUG fmt, ## args)
922 #else
923 static inline void
924 __attribute__ ((__format__ (__printf__, 1, 2)))
dt_dprintk(const char * fmt,...)925 dt_dprintk(const char *fmt, ...) {}
926 #endif
927 
928 #endif /* __XEN_DEVICE_TREE_H */
929 
930 /*
931  * Local variables:
932  * mode: C
933  * c-file-style: "BSD"
934  * c-basic-offset: 4
935  * indent-tabs-mode: nil
936  * End:
937  */
938