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