1 /*
2  * Device Tree
3  *
4  * Copyright (C) 2012 Citrix Systems, Inc.
5  * Copyright 2009 Benjamin Herrenschmidt, IBM Corp
6  * benh@kernel.crashing.org
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 
13 #include <xen/types.h>
14 #include <xen/init.h>
15 #include <xen/guest_access.h>
16 #include <xen/device_tree.h>
17 #include <xen/kernel.h>
18 #include <xen/lib.h>
19 #include <xen/libfdt/libfdt.h>
20 #include <xen/mm.h>
21 #include <xen/stdarg.h>
22 #include <xen/string.h>
23 #include <xen/cpumask.h>
24 #include <xen/ctype.h>
25 #include <asm/setup.h>
26 #include <xen/err.h>
27 
28 const void *device_tree_flattened;
29 dt_irq_xlate_func dt_irq_xlate;
30 /* Host device tree */
31 struct dt_device_node *dt_host;
32 /* Interrupt controller node*/
33 const struct dt_device_node *dt_interrupt_controller;
34 
35 /**
36  * struct dt_alias_prop - Alias property in 'aliases' node
37  * @link: List node to link the structure in aliases_lookup list
38  * @alias: Alias property name
39  * @np: Pointer to device_node that the alias stands for
40  * @id: Index value from end of alias name
41  * @stem: Alias string without the index
42  *
43  * The structure represents one alias property of 'aliases' node as
44  * an entry in aliases_lookup list.
45  */
46 struct dt_alias_prop {
47     struct list_head link;
48     const char *alias;
49     struct dt_device_node *np;
50     int id;
51     char stem[0];
52 };
53 
54 static LIST_HEAD(aliases_lookup);
55 
56 #ifdef CONFIG_DEVICE_TREE_DEBUG
dt_dump_addr(const char * s,const __be32 * addr,int na)57 static void dt_dump_addr(const char *s, const __be32 *addr, int na)
58 {
59     dt_dprintk("%s", s);
60     while ( na-- )
61         dt_dprintk(" %08x", be32_to_cpu(*(addr++)));
62     dt_dprintk("\n");
63 }
64 #else
dt_dump_addr(const char * s,const __be32 * addr,int na)65 static void dt_dump_addr(const char *s, const __be32 *addr, int na) { }
66 #endif
67 
68 #define DT_BAD_ADDR ((u64)-1)
69 
70 /* Max address size we deal with */
71 #define DT_MAX_ADDR_CELLS 4
72 #define DT_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= DT_MAX_ADDR_CELLS)
73 #define DT_CHECK_COUNTS(na, ns) (DT_CHECK_ADDR_COUNT(na) && (ns) > 0)
74 
75 /* Callbacks for bus specific translators */
76 struct dt_bus
77 {
78     const char *name;
79     const char *addresses;
80     bool_t (*match)(const struct dt_device_node *node);
81     void (*count_cells)(const struct dt_device_node *child,
82                         int *addrc, int *sizec);
83     u64 (*map)(__be32 *addr, const __be32 *range, int na, int ns, int pna);
84     int (*translate)(__be32 *addr, u64 offset, int na);
85     unsigned int (*get_flags)(const __be32 *addr);
86 };
87 
dt_get_range(const __be32 ** cell,const struct dt_device_node * np,u64 * address,u64 * size)88 void dt_get_range(const __be32 **cell, const struct dt_device_node *np,
89                   u64 *address, u64 *size)
90 {
91     *address = dt_next_cell(dt_n_addr_cells(np), cell);
92     *size = dt_next_cell(dt_n_size_cells(np), cell);
93 }
94 
dt_set_cell(__be32 ** cellp,int size,u64 val)95 void dt_set_cell(__be32 **cellp, int size, u64 val)
96 {
97     int cells = size;
98 
99     while ( size-- )
100     {
101         (*cellp)[size] = cpu_to_fdt32(val);
102         val >>= 32;
103     }
104 
105     (*cellp) += cells;
106 }
107 
dt_set_range(__be32 ** cellp,const struct dt_device_node * np,u64 address,u64 size)108 void dt_set_range(__be32 **cellp, const struct dt_device_node *np,
109                   u64 address, u64 size)
110 {
111     dt_set_cell(cellp, dt_n_addr_cells(np), address);
112     dt_set_cell(cellp, dt_n_size_cells(np), size);
113 }
114 
dt_child_set_range(__be32 ** cellp,int addrcells,int sizecells,u64 address,u64 size)115 void dt_child_set_range(__be32 **cellp, int addrcells, int sizecells,
116                         u64 address, u64 size)
117 {
118     dt_set_cell(cellp, addrcells, address);
119     dt_set_cell(cellp, sizecells, size);
120 }
121 
unflatten_dt_alloc(unsigned long * mem,unsigned long size,unsigned long align)122 static void __init *unflatten_dt_alloc(unsigned long *mem, unsigned long size,
123                                        unsigned long align)
124 {
125     void *res;
126 
127     *mem = ROUNDUP(*mem, align);
128     res = (void *)*mem;
129     *mem += size;
130 
131     return res;
132 }
133 
134 /* Find a property with a given name for a given node and return it. */
dt_find_property(const struct dt_device_node * np,const char * name,u32 * lenp)135 const struct dt_property *dt_find_property(const struct dt_device_node *np,
136                                            const char *name, u32 *lenp)
137 {
138     const struct dt_property *pp;
139 
140     if ( !np )
141         return NULL;
142 
143     for ( pp = np->properties; pp; pp = pp->next )
144     {
145         if ( dt_prop_cmp(pp->name, name) == 0 )
146         {
147             if ( lenp )
148                 *lenp = pp->length;
149             break;
150         }
151     }
152 
153     return pp;
154 }
155 
dt_get_property(const struct dt_device_node * np,const char * name,u32 * lenp)156 const void *dt_get_property(const struct dt_device_node *np,
157                             const char *name, u32 *lenp)
158 {
159     const struct dt_property *pp = dt_find_property(np, name, lenp);
160 
161     return pp ? pp->value : NULL;
162 }
163 
dt_property_read_u32(const struct dt_device_node * np,const char * name,u32 * out_value)164 bool_t dt_property_read_u32(const struct dt_device_node *np,
165                          const char *name, u32 *out_value)
166 {
167     u32 len;
168     const __be32 *val;
169 
170     val = dt_get_property(np, name, &len);
171     if ( !val || len < sizeof(*out_value) )
172         return 0;
173 
174     *out_value = be32_to_cpup(val);
175 
176     return 1;
177 }
178 
179 
dt_property_read_u64(const struct dt_device_node * np,const char * name,u64 * out_value)180 bool_t dt_property_read_u64(const struct dt_device_node *np,
181                          const char *name, u64 *out_value)
182 {
183     u32 len;
184     const __be32 *val;
185 
186     val = dt_get_property(np, name, &len);
187     if ( !val || len < sizeof(*out_value) )
188         return 0;
189 
190     *out_value = dt_read_number(val, 2);
191 
192     return 1;
193 }
dt_property_read_string(const struct dt_device_node * np,const char * propname,const char ** out_string)194 int dt_property_read_string(const struct dt_device_node *np,
195                             const char *propname, const char **out_string)
196 {
197     const struct dt_property *pp = dt_find_property(np, propname, NULL);
198 
199     if ( !pp )
200         return -EINVAL;
201     if ( !pp->value )
202         return -ENODATA;
203     if ( strnlen(pp->value, pp->length) >= pp->length )
204         return -EILSEQ;
205 
206     *out_string = pp->value;
207 
208     return 0;
209 }
210 
dt_device_is_compatible(const struct dt_device_node * device,const char * compat)211 bool_t dt_device_is_compatible(const struct dt_device_node *device,
212                                const char *compat)
213 {
214     const char* cp;
215     u32 cplen, l;
216 
217     cp = dt_get_property(device, "compatible", &cplen);
218     if ( cp == NULL )
219         return 0;
220     while ( cplen > 0 )
221     {
222         if ( dt_compat_cmp(cp, compat) == 0 )
223             return 1;
224         l = strlen(cp) + 1;
225         cp += l;
226         cplen -= l;
227     }
228 
229     return 0;
230 }
231 
dt_machine_is_compatible(const char * compat)232 bool_t dt_machine_is_compatible(const char *compat)
233 {
234     const struct dt_device_node *root;
235     bool_t rc = 0;
236 
237     root = dt_find_node_by_path("/");
238     if ( root )
239     {
240         rc = dt_device_is_compatible(root, compat);
241     }
242     return rc;
243 }
244 
dt_find_node_by_name(struct dt_device_node * from,const char * name)245 struct dt_device_node *dt_find_node_by_name(struct dt_device_node *from,
246                                             const char *name)
247 {
248     struct dt_device_node *np;
249     struct dt_device_node *dt;
250 
251     dt = from ? from->allnext : dt_host;
252     dt_for_each_device_node(dt, np)
253         if ( np->name && (dt_node_cmp(np->name, name) == 0) )
254             break;
255 
256     return np;
257 }
258 
dt_find_node_by_type(struct dt_device_node * from,const char * type)259 struct dt_device_node *dt_find_node_by_type(struct dt_device_node *from,
260                                             const char *type)
261 {
262     struct dt_device_node *np;
263     struct dt_device_node *dt;
264 
265     dt = from ? from->allnext : dt_host;
266     dt_for_each_device_node(dt, np)
267         if ( np->type && (dt_node_cmp(np->type, type) == 0) )
268             break;
269 
270     return np;
271 }
272 
dt_find_node_by_path(const char * path)273 struct dt_device_node *dt_find_node_by_path(const char *path)
274 {
275     struct dt_device_node *np;
276 
277     dt_for_each_device_node(dt_host, np)
278         if ( np->full_name && (dt_node_cmp(np->full_name, path) == 0) )
279             break;
280 
281     return np;
282 }
283 
dt_find_node_by_gpath(XEN_GUEST_HANDLE (char)u_path,uint32_t u_plen,struct dt_device_node ** node)284 int dt_find_node_by_gpath(XEN_GUEST_HANDLE(char) u_path, uint32_t u_plen,
285                           struct dt_device_node **node)
286 {
287     char *path;
288 
289     path = safe_copy_string_from_guest(u_path, u_plen, PAGE_SIZE);
290     if ( IS_ERR(path) )
291         return PTR_ERR(path);
292 
293     *node = dt_find_node_by_path(path);
294 
295     xfree(path);
296 
297     return (*node == NULL) ? -ESRCH : 0;
298 }
299 
dt_find_node_by_alias(const char * alias)300 struct dt_device_node *dt_find_node_by_alias(const char *alias)
301 {
302     const struct dt_alias_prop *app;
303 
304     list_for_each_entry( app, &aliases_lookup, link )
305     {
306         if ( !strcmp(app->alias, alias) )
307             return app->np;
308     }
309 
310     return NULL;
311 }
312 
313 const struct dt_device_match *
dt_match_node(const struct dt_device_match * matches,const struct dt_device_node * node)314 dt_match_node(const struct dt_device_match *matches,
315               const struct dt_device_node *node)
316 {
317     if ( !matches )
318         return NULL;
319 
320     while ( matches->path || matches->type ||
321             matches->compatible || matches->not_available || matches->prop)
322     {
323         bool_t match = 1;
324 
325         if ( matches->path )
326             match &= dt_node_path_is_equal(node, matches->path);
327 
328         if ( matches->type )
329             match &= dt_device_type_is_equal(node, matches->type);
330 
331         if ( matches->compatible )
332             match &= dt_device_is_compatible(node, matches->compatible);
333 
334         if ( matches->not_available )
335             match &= !dt_device_is_available(node);
336 
337         if ( matches->prop )
338             match &= dt_find_property(node, matches->prop, NULL) != NULL;
339 
340         if ( match )
341             return matches;
342         matches++;
343     }
344 
345     return NULL;
346 }
347 
dt_get_parent(const struct dt_device_node * node)348 const struct dt_device_node *dt_get_parent(const struct dt_device_node *node)
349 {
350     if ( !node )
351         return NULL;
352 
353     return node->parent;
354 }
355 
356 struct dt_device_node *
dt_find_compatible_node(struct dt_device_node * from,const char * type,const char * compatible)357 dt_find_compatible_node(struct dt_device_node *from,
358                         const char *type,
359                         const char *compatible)
360 {
361     struct dt_device_node *np;
362     struct dt_device_node *dt;
363 
364     dt = from ? from->allnext : dt_host;
365     dt_for_each_device_node(dt, np)
366     {
367         if ( type
368              && !(np->type && (dt_node_cmp(np->type, type) == 0)) )
369             continue;
370         if ( dt_device_is_compatible(np, compatible) )
371             break;
372     }
373 
374     return np;
375 }
376 
377 struct dt_device_node *
dt_find_matching_node(struct dt_device_node * from,const struct dt_device_match * matches)378 dt_find_matching_node(struct dt_device_node *from,
379                       const struct dt_device_match *matches)
380 {
381     struct dt_device_node *np;
382     struct dt_device_node *dt;
383 
384     dt = from ? from->allnext : dt_host;
385     dt_for_each_device_node(dt, np)
386     {
387         if ( dt_match_node(matches, np) )
388             return np;
389     }
390 
391     return NULL;
392 }
393 
__dt_n_addr_cells(const struct dt_device_node * np,bool_t parent)394 static int __dt_n_addr_cells(const struct dt_device_node *np, bool_t parent)
395 {
396     const __be32 *ip;
397 
398     do {
399         if ( np->parent && !parent )
400             np = np->parent;
401         parent = false;
402 
403         ip = dt_get_property(np, "#address-cells", NULL);
404         if ( ip )
405             return be32_to_cpup(ip);
406     } while ( np->parent );
407     /* No #address-cells property for the root node */
408     return DT_ROOT_NODE_ADDR_CELLS_DEFAULT;
409 }
410 
__dt_n_size_cells(const struct dt_device_node * np,bool_t parent)411 int __dt_n_size_cells(const struct dt_device_node *np, bool_t parent)
412 {
413     const __be32 *ip;
414 
415     do {
416         if ( np->parent && !parent )
417             np = np->parent;
418         parent = false;
419 
420         ip = dt_get_property(np, "#size-cells", NULL);
421         if ( ip )
422             return be32_to_cpup(ip);
423     } while ( np->parent );
424     /* No #address-cells property for the root node */
425     return DT_ROOT_NODE_SIZE_CELLS_DEFAULT;
426 }
427 
dt_n_addr_cells(const struct dt_device_node * np)428 int dt_n_addr_cells(const struct dt_device_node *np)
429 {
430     return __dt_n_addr_cells(np, false);
431 }
432 
dt_n_size_cells(const struct dt_device_node * np)433 int dt_n_size_cells(const struct dt_device_node *np)
434 {
435     return __dt_n_size_cells(np, false);
436 }
437 
dt_child_n_addr_cells(const struct dt_device_node * parent)438 int dt_child_n_addr_cells(const struct dt_device_node *parent)
439 {
440     return __dt_n_addr_cells(parent, true);
441 }
442 
dt_child_n_size_cells(const struct dt_device_node * parent)443 int dt_child_n_size_cells(const struct dt_device_node *parent)
444 {
445     return __dt_n_size_cells(parent, true);
446 }
447 
448 /*
449  * These are defined in Linux where much of this code comes from, but
450  * are currently unused outside this file in the context of Xen.
451  */
452 #define IORESOURCE_BITS         0x000000ff      /* Bus-specific bits */
453 
454 #define IORESOURCE_TYPE_BITS    0x00001f00      /* Resource type */
455 #define IORESOURCE_IO           0x00000100      /* PCI/ISA I/O ports */
456 #define IORESOURCE_MEM          0x00000200
457 #define IORESOURCE_REG          0x00000300      /* Register offsets */
458 #define IORESOURCE_IRQ          0x00000400
459 #define IORESOURCE_DMA          0x00000800
460 #define IORESOURCE_BUS          0x00001000
461 
462 #define IORESOURCE_PREFETCH     0x00002000      /* No side effects */
463 #define IORESOURCE_READONLY     0x00004000
464 #define IORESOURCE_CACHEABLE    0x00008000
465 #define IORESOURCE_RANGELENGTH  0x00010000
466 #define IORESOURCE_SHADOWABLE   0x00020000
467 
468 /*
469  * Default translator (generic bus)
470  */
dt_bus_default_match(const struct dt_device_node * node)471 static bool_t dt_bus_default_match(const struct dt_device_node *node)
472 {
473     /* Root node doesn't have "ranges" property */
474     if ( node->parent == NULL )
475         return 1;
476 
477     /* The default bus is only used when the "ranges" property exists.
478      * Otherwise we can't translate the address
479      */
480     return (dt_get_property(node, "ranges", NULL) != NULL);
481 }
482 
dt_bus_default_count_cells(const struct dt_device_node * dev,int * addrc,int * sizec)483 static void dt_bus_default_count_cells(const struct dt_device_node *dev,
484                                 int *addrc, int *sizec)
485 {
486     if ( addrc )
487         *addrc = dt_n_addr_cells(dev);
488     if ( sizec )
489         *sizec = dt_n_size_cells(dev);
490 }
491 
dt_bus_default_map(__be32 * addr,const __be32 * range,int na,int ns,int pna)492 static u64 dt_bus_default_map(__be32 *addr, const __be32 *range,
493                               int na, int ns, int pna)
494 {
495     u64 cp, s, da;
496 
497     cp = dt_read_number(range, na);
498     s = dt_read_number(range + na + pna, ns);
499     da = dt_read_number(addr, na);
500 
501     dt_dprintk("DT: default map, cp=%llx, s=%llx, da=%llx\n",
502                (unsigned long long)cp, (unsigned long long)s,
503                (unsigned long long)da);
504 
505     /*
506      * If the number of address cells is larger than 2 we assume the
507      * mapping doesn't specify a physical address. Rather, the address
508      * specifies an identifier that must match exactly.
509      */
510     if ( na > 2 && memcmp(range, addr, na * 4) != 0 )
511         return DT_BAD_ADDR;
512 
513     if ( da < cp || da >= (cp + s) )
514         return DT_BAD_ADDR;
515     return da - cp;
516 }
517 
dt_bus_default_translate(__be32 * addr,u64 offset,int na)518 static int dt_bus_default_translate(__be32 *addr, u64 offset, int na)
519 {
520     u64 a = dt_read_number(addr, na);
521 
522     memset(addr, 0, na * 4);
523     a += offset;
524     if ( na > 1 )
525         addr[na - 2] = cpu_to_be32(a >> 32);
526     addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
527 
528     return 0;
529 }
dt_bus_default_get_flags(const __be32 * addr)530 static unsigned int dt_bus_default_get_flags(const __be32 *addr)
531 {
532     return IORESOURCE_MEM;
533 }
534 
535 /*
536  * PCI bus specific translator
537  */
538 
dt_bus_pci_match(const struct dt_device_node * np)539 static bool_t dt_bus_pci_match(const struct dt_device_node *np)
540 {
541     /*
542      * "pciex" is PCI Express "vci" is for the /chaos bridge on 1st-gen PCI
543      * powermacs "ht" is hypertransport
544      */
545     return !strcmp(np->type, "pci") || !strcmp(np->type, "pciex") ||
546         !strcmp(np->type, "vci") || !strcmp(np->type, "ht");
547 }
548 
dt_bus_pci_count_cells(const struct dt_device_node * np,int * addrc,int * sizec)549 static void dt_bus_pci_count_cells(const struct dt_device_node *np,
550 				   int *addrc, int *sizec)
551 {
552     if (addrc)
553         *addrc = 3;
554     if (sizec)
555         *sizec = 2;
556 }
557 
dt_bus_pci_get_flags(const __be32 * addr)558 static unsigned int dt_bus_pci_get_flags(const __be32 *addr)
559 {
560     unsigned int flags = 0;
561     u32 w = be32_to_cpup(addr);
562 
563     switch((w >> 24) & 0x03) {
564     case 0x01:
565         flags |= IORESOURCE_IO;
566         break;
567     case 0x02: /* 32 bits */
568     case 0x03: /* 64 bits */
569         flags |= IORESOURCE_MEM;
570         break;
571     }
572     if (w & 0x40000000)
573         flags |= IORESOURCE_PREFETCH;
574     return flags;
575 }
576 
dt_bus_pci_map(__be32 * addr,const __be32 * range,int na,int ns,int pna)577 static u64 dt_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
578 		int pna)
579 {
580     u64 cp, s, da;
581     unsigned int af, rf;
582 
583     af = dt_bus_pci_get_flags(addr);
584     rf = dt_bus_pci_get_flags(range);
585 
586     /* Check address type match */
587     if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
588         return DT_BAD_ADDR;
589 
590     /* Read address values, skipping high cell */
591     cp = dt_read_number(range + 1, na - 1);
592     s  = dt_read_number(range + na + pna, ns);
593     da = dt_read_number(addr + 1, na - 1);
594 
595     dt_dprintk("DT: PCI map, cp=%llx, s=%llx, da=%llx\n",
596                (unsigned long long)cp, (unsigned long long)s,
597                (unsigned long long)da);
598 
599     if (da < cp || da >= (cp + s))
600         return DT_BAD_ADDR;
601     return da - cp;
602 }
603 
dt_bus_pci_translate(__be32 * addr,u64 offset,int na)604 static int dt_bus_pci_translate(__be32 *addr, u64 offset, int na)
605 {
606     return dt_bus_default_translate(addr + 1, offset, na - 1);
607 }
608 
609 /*
610  * Array of bus specific translators
611  */
612 static const struct dt_bus dt_busses[] =
613 {
614     /* PCI */
615     {
616         .name = "pci",
617         .addresses = "assigned-addresses",
618         .match = dt_bus_pci_match,
619         .count_cells = dt_bus_pci_count_cells,
620         .map = dt_bus_pci_map,
621         .translate = dt_bus_pci_translate,
622         .get_flags = dt_bus_pci_get_flags,
623     },
624     /* Default */
625     {
626         .name = "default",
627         .addresses = "reg",
628         .match = dt_bus_default_match,
629         .count_cells = dt_bus_default_count_cells,
630         .map = dt_bus_default_map,
631         .translate = dt_bus_default_translate,
632         .get_flags = dt_bus_default_get_flags,
633     },
634 };
635 
dt_match_bus(const struct dt_device_node * np)636 static const struct dt_bus *dt_match_bus(const struct dt_device_node *np)
637 {
638     int i;
639 
640     for ( i = 0; i < ARRAY_SIZE(dt_busses); i++ )
641         if ( !dt_busses[i].match || dt_busses[i].match(np) )
642             return &dt_busses[i];
643 
644     return NULL;
645 }
646 
dt_get_address(const struct dt_device_node * dev,unsigned int index,u64 * size,unsigned int * flags)647 static const __be32 *dt_get_address(const struct dt_device_node *dev,
648                                     unsigned int index, u64 *size,
649                                     unsigned int *flags)
650 {
651     const __be32 *prop;
652     u32 psize;
653     const struct dt_device_node *parent;
654     const struct dt_bus *bus;
655     int onesize, i, na, ns;
656 
657     /* Get parent & match bus type */
658     parent = dt_get_parent(dev);
659     if ( parent == NULL )
660         return NULL;
661 
662     bus = dt_match_bus(parent);
663     if ( !bus )
664         return NULL;
665     bus->count_cells(dev, &na, &ns);
666 
667     if ( !DT_CHECK_ADDR_COUNT(na) )
668         return NULL;
669 
670     /* Get "reg" or "assigned-addresses" property */
671     prop = dt_get_property(dev, bus->addresses, &psize);
672     if ( prop == NULL )
673         return NULL;
674     psize /= 4;
675 
676     onesize = na + ns;
677     for ( i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++ )
678     {
679         if ( i == index )
680         {
681             if ( size )
682                 *size = dt_read_number(prop + na, ns);
683             if ( flags )
684                 *flags = bus->get_flags(prop);
685             return prop;
686         }
687     }
688     return NULL;
689 }
690 
dt_translate_one(const struct dt_device_node * parent,const struct dt_bus * bus,const struct dt_bus * pbus,__be32 * addr,int na,int ns,int pna,const char * rprop)691 static int dt_translate_one(const struct dt_device_node *parent,
692                             const struct dt_bus *bus,
693                             const struct dt_bus *pbus,
694                             __be32 *addr, int na, int ns,
695                             int pna, const char *rprop)
696 {
697     const __be32 *ranges;
698     unsigned int rlen;
699     int rone;
700     u64 offset = DT_BAD_ADDR;
701 
702     ranges = dt_get_property(parent, rprop, &rlen);
703     if ( ranges == NULL )
704     {
705         printk(XENLOG_ERR "DT: no ranges; cannot translate\n");
706         return 1;
707     }
708     if ( rlen == 0 )
709     {
710         offset = dt_read_number(addr, na);
711         memset(addr, 0, pna * 4);
712         dt_dprintk("DT: empty ranges; 1:1 translation\n");
713         goto finish;
714     }
715 
716     dt_dprintk("DT: walking ranges...\n");
717 
718     /* Now walk through the ranges */
719     rlen /= 4;
720     rone = na + pna + ns;
721     for ( ; rlen >= rone; rlen -= rone, ranges += rone )
722     {
723         offset = bus->map(addr, ranges, na, ns, pna);
724         if ( offset != DT_BAD_ADDR )
725             break;
726     }
727     if ( offset == DT_BAD_ADDR )
728     {
729         dt_dprintk("DT: not found !\n");
730         return 1;
731     }
732     memcpy(addr, ranges + na, 4 * pna);
733 
734 finish:
735     dt_dump_addr("DT: parent translation for:", addr, pna);
736     dt_dprintk("DT: with offset: %llx\n", (unsigned long long)offset);
737 
738     /* Translate it into parent bus space */
739     return pbus->translate(addr, offset, pna);
740 }
741 
742 /*
743  * Translate an address from the device-tree into a CPU physical address,
744  * this walks up the tree and applies the various bus mappings on the
745  * way.
746  *
747  * Note: We consider that crossing any level with #size-cells == 0 to mean
748  * that translation is impossible (that is we are not dealing with a value
749  * that can be mapped to a cpu physical address). This is not really specified
750  * that way, but this is traditionally the way IBM at least do things
751  */
__dt_translate_address(const struct dt_device_node * dev,const __be32 * in_addr,const char * rprop)752 static u64 __dt_translate_address(const struct dt_device_node *dev,
753                                   const __be32 *in_addr, const char *rprop)
754 {
755     const struct dt_device_node *parent = NULL;
756     const struct dt_bus *bus, *pbus;
757     __be32 addr[DT_MAX_ADDR_CELLS];
758     int na, ns, pna, pns;
759     u64 result = DT_BAD_ADDR;
760 
761     dt_dprintk("DT: ** translation for device %s **\n", dev->full_name);
762 
763     /* Get parent & match bus type */
764     parent = dt_get_parent(dev);
765     if ( parent == NULL )
766         goto bail;
767     bus = dt_match_bus(parent);
768     if ( !bus )
769         goto bail;
770 
771     /* Count address cells & copy address locally */
772     bus->count_cells(dev, &na, &ns);
773     if ( !DT_CHECK_COUNTS(na, ns) )
774     {
775         printk(XENLOG_ERR "dt_parse: Bad cell count for device %s\n",
776                   dev->full_name);
777         goto bail;
778     }
779     memcpy(addr, in_addr, na * 4);
780 
781     dt_dprintk("DT: bus is %s (na=%d, ns=%d) on %s\n",
782                bus->name, na, ns, parent->full_name);
783     dt_dump_addr("DT: translating address:", addr, na);
784 
785     /* Translate */
786     for ( ;; )
787     {
788         /* Switch to parent bus */
789         dev = parent;
790         parent = dt_get_parent(dev);
791 
792         /* If root, we have finished */
793         if ( parent == NULL )
794         {
795             dt_dprintk("DT: reached root node\n");
796             result = dt_read_number(addr, na);
797             break;
798         }
799 
800         /* Get new parent bus and counts */
801         pbus = dt_match_bus(parent);
802         if ( pbus == NULL )
803         {
804             printk("DT: %s is not a valid bus\n", parent->full_name);
805             break;
806         }
807         pbus->count_cells(dev, &pna, &pns);
808         if ( !DT_CHECK_COUNTS(pna, pns) )
809         {
810             printk(XENLOG_ERR "dt_parse: Bad cell count for parent %s\n",
811                    dev->full_name);
812             break;
813         }
814 
815         dt_dprintk("DT: parent bus is %s (na=%d, ns=%d) on %s\n",
816                    pbus->name, pna, pns, parent->full_name);
817 
818         /* Apply bus translation */
819         if ( dt_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop) )
820             break;
821 
822         /* Complete the move up one level */
823         na = pna;
824         ns = pns;
825         bus = pbus;
826 
827         dt_dump_addr("DT: one level translation:", addr, na);
828     }
829 
830 bail:
831     return result;
832 }
833 
834 /* dt_device_address - Translate device tree address and return it */
dt_device_get_address(const struct dt_device_node * dev,unsigned int index,u64 * addr,u64 * size)835 int dt_device_get_address(const struct dt_device_node *dev, unsigned int index,
836                           u64 *addr, u64 *size)
837 {
838     const __be32 *addrp;
839     unsigned int flags;
840 
841     addrp = dt_get_address(dev, index, size, &flags);
842     if ( addrp == NULL )
843         return -EINVAL;
844 
845     if ( !addr )
846         return -EINVAL;
847 
848     *addr = __dt_translate_address(dev, addrp, "ranges");
849 
850     if ( *addr == DT_BAD_ADDR )
851         return -EINVAL;
852 
853     return 0;
854 }
855 
856 
dt_for_each_range(const struct dt_device_node * dev,int (* cb)(const struct dt_device_node *,u64 addr,u64 length,void *),void * data)857 int dt_for_each_range(const struct dt_device_node *dev,
858                       int (*cb)(const struct dt_device_node *,
859                                 u64 addr, u64 length,
860                                 void *),
861                       void *data)
862 {
863     const struct dt_device_node *parent = NULL;
864     const struct dt_bus *bus, *pbus;
865     const __be32 *ranges;
866     __be32 addr[DT_MAX_ADDR_CELLS];
867     unsigned int rlen;
868     int na, ns, pna, pns, rone;
869 
870     bus = dt_match_bus(dev);
871     if ( !bus )
872         return 0; /* device is not a bus */
873 
874     parent = dt_get_parent(dev);
875     if ( parent == NULL )
876         return -EINVAL;
877 
878     ranges = dt_get_property(dev, "ranges", &rlen);
879     if ( ranges == NULL )
880     {
881         printk(XENLOG_ERR "DT: no ranges; cannot enumerate %s\n",
882                dev->full_name);
883         return -EINVAL;
884     }
885     if ( rlen == 0 ) /* Nothing to do */
886         return 0;
887 
888     bus->count_cells(dev, &na, &ns);
889     if ( !DT_CHECK_COUNTS(na, ns) )
890     {
891         printk(XENLOG_ERR "dt_parse: Bad cell count for device %s\n",
892                   dev->full_name);
893         return -EINVAL;
894     }
895 
896     pbus = dt_match_bus(parent);
897     if ( pbus == NULL )
898     {
899         printk("DT: %s is not a valid bus\n", parent->full_name);
900         return -EINVAL;
901     }
902 
903     pbus->count_cells(dev, &pna, &pns);
904     if ( !DT_CHECK_COUNTS(pna, pns) )
905     {
906         printk(XENLOG_ERR "dt_parse: Bad cell count for parent %s\n",
907                dev->full_name);
908         return -EINVAL;
909     }
910 
911     /* Now walk through the ranges */
912     rlen /= 4;
913     rone = na + pna + ns;
914 
915     dt_dprintk("%s: dev=%s, bus=%s, parent=%s, rlen=%d, rone=%d\n",
916                __func__,
917                dt_node_name(dev), bus->name,
918                dt_node_name(parent), rlen, rone);
919 
920     for ( ; rlen >= rone; rlen -= rone, ranges += rone )
921     {
922         u64 a, s;
923         int ret;
924 
925         memcpy(addr, ranges + na, 4 * pna);
926 
927         a = __dt_translate_address(dev, addr, "ranges");
928         s = dt_read_number(ranges + na + pna, ns);
929 
930         ret = cb(dev, a, s, data);
931         if ( ret )
932         {
933             dt_dprintk(" -> callback failed=%d\n", ret);
934             return ret;
935         }
936 
937     }
938 
939     return 0;
940 }
941 
942 /**
943  * dt_find_node_by_phandle - Find a node given a phandle
944  * @handle: phandle of the node to find
945  *
946  * Returns a node pointer.
947  */
dt_find_node_by_phandle(dt_phandle handle)948 static struct dt_device_node *dt_find_node_by_phandle(dt_phandle handle)
949 {
950     struct dt_device_node *np;
951 
952     dt_for_each_device_node(dt_host, np)
953         if ( np->phandle == handle )
954             break;
955 
956     return np;
957 }
958 
959 /**
960  * dt_irq_find_parent - Given a device node, find its interrupt parent node
961  * @child: pointer to device node
962  *
963  * Returns a pointer to the interrupt parent node, or NULL if the interrupt
964  * parent could not be determined.
965  */
966 static const struct dt_device_node *
dt_irq_find_parent(const struct dt_device_node * child)967 dt_irq_find_parent(const struct dt_device_node *child)
968 {
969     const struct dt_device_node *p;
970     const __be32 *parp;
971 
972     do
973     {
974         parp = dt_get_property(child, "interrupt-parent", NULL);
975         if ( parp == NULL )
976             p = dt_get_parent(child);
977         else
978             p = dt_find_node_by_phandle(be32_to_cpup(parp));
979         child = p;
980     } while ( p && dt_get_property(p, "#interrupt-cells", NULL) == NULL );
981 
982     return p;
983 }
984 
dt_number_of_irq(const struct dt_device_node * device)985 unsigned int dt_number_of_irq(const struct dt_device_node *device)
986 {
987     const struct dt_device_node *p;
988     const __be32 *intspec, *tmp;
989     u32 intsize, intlen;
990     int intnum;
991 
992     dt_dprintk("dt_irq_number: dev=%s\n", device->full_name);
993 
994     /* Try the new-style interrupts-extended first */
995     intnum = dt_count_phandle_with_args(device, "interrupts-extended",
996                                         "#interrupt-cells");
997     if ( intnum >= 0 )
998     {
999         dt_dprintk(" using 'interrupts-extended' property\n");
1000         dt_dprintk(" intnum=%d\n", intnum);
1001         return intnum;
1002     }
1003 
1004     /* Get the interrupts property */
1005     intspec = dt_get_property(device, "interrupts", &intlen);
1006     if ( intspec == NULL )
1007         return 0;
1008     intlen /= sizeof(*intspec);
1009 
1010     dt_dprintk(" using 'interrupts' property\n");
1011     dt_dprintk(" intspec=%d intlen=%d\n", be32_to_cpup(intspec), intlen);
1012 
1013     /* Look for the interrupt parent. */
1014     p = dt_irq_find_parent(device);
1015     if ( p == NULL )
1016         return 0;
1017 
1018     /* Get size of interrupt specifier */
1019     tmp = dt_get_property(p, "#interrupt-cells", NULL);
1020     if ( tmp == NULL )
1021         return 0;
1022     intsize = be32_to_cpu(*tmp);
1023 
1024     dt_dprintk(" intsize=%d intlen=%d\n", intsize, intlen);
1025 
1026     return (intlen / intsize);
1027 }
1028 
dt_number_of_address(const struct dt_device_node * dev)1029 unsigned int dt_number_of_address(const struct dt_device_node *dev)
1030 {
1031     const __be32 *prop;
1032     u32 psize;
1033     const struct dt_device_node *parent;
1034     const struct dt_bus *bus;
1035     int onesize, na, ns;
1036 
1037     /* Get parent & match bus type */
1038     parent = dt_get_parent(dev);
1039     if ( parent == NULL )
1040         return 0;
1041 
1042     bus = dt_match_bus(parent);
1043     if ( !bus )
1044         return 0;
1045     bus->count_cells(dev, &na, &ns);
1046 
1047     if ( !DT_CHECK_COUNTS(na, ns) )
1048         return 0;
1049 
1050     /* Get "reg" or "assigned-addresses" property */
1051     prop = dt_get_property(dev, bus->addresses, &psize);
1052     if ( prop == NULL )
1053         return 0;
1054 
1055     psize /= 4;
1056     onesize = na + ns;
1057 
1058     return (psize / onesize);
1059 }
1060 
dt_for_each_irq_map(const struct dt_device_node * dev,int (* cb)(const struct dt_device_node *,const struct dt_irq *,void *),void * data)1061 int dt_for_each_irq_map(const struct dt_device_node *dev,
1062                         int (*cb)(const struct dt_device_node *,
1063                                   const struct dt_irq *,
1064                                   void *),
1065                         void *data)
1066 {
1067     const struct dt_device_node *ipar, *tnode, *old = NULL;
1068     const __be32 *tmp, *imap;
1069     u32 intsize = 1, addrsize, pintsize = 0, paddrsize = 0;
1070     u32 imaplen;
1071     int i, ret;
1072 
1073     struct dt_raw_irq dt_raw_irq;
1074     struct dt_irq dt_irq;
1075 
1076     dt_dprintk("%s: par=%s cb=%p data=%p\n", __func__,
1077                dev->full_name, cb, data);
1078 
1079     ipar = dev;
1080 
1081     /* First get the #interrupt-cells property of the current cursor
1082      * that tells us how to interpret the passed-in intspec. If there
1083      * is none, we are nice and just walk up the tree
1084      */
1085     do {
1086         tmp = dt_get_property(ipar, "#interrupt-cells", NULL);
1087         if ( tmp != NULL )
1088         {
1089             intsize = be32_to_cpu(*tmp);
1090             break;
1091         }
1092         tnode = ipar;
1093         ipar = dt_irq_find_parent(ipar);
1094     } while ( ipar );
1095     if ( ipar == NULL )
1096     {
1097         dt_dprintk(" -> no parent found !\n");
1098         goto fail;
1099     }
1100 
1101     dt_dprintk("%s: ipar=%s, size=%d\n", __func__, ipar->full_name, intsize);
1102 
1103     if ( intsize > DT_MAX_IRQ_SPEC )
1104     {
1105         dt_dprintk(" -> too many irq specifier cells\n");
1106         goto fail;
1107     }
1108 
1109     /* Look for this #address-cells. We have to implement the old linux
1110      * trick of looking for the parent here as some device-trees rely on it
1111      */
1112     old = ipar;
1113     do {
1114         tmp = dt_get_property(old, "#address-cells", NULL);
1115         tnode = dt_get_parent(old);
1116         old = tnode;
1117     } while ( old && tmp == NULL );
1118 
1119     old = NULL;
1120     addrsize = (tmp == NULL) ? 2 : be32_to_cpu(*tmp);
1121 
1122     dt_dprintk(" -> addrsize=%d\n", addrsize);
1123 
1124     /* Now look for an interrupt-map */
1125     imap = dt_get_property(dev, "interrupt-map", &imaplen);
1126     /* No interrupt-map found. Ignore */
1127     if ( imap == NULL )
1128     {
1129         dt_dprintk(" -> no map, ignoring\n");
1130         return 0;
1131     }
1132     imaplen /= sizeof(u32);
1133 
1134     /* Parse interrupt-map */
1135     while ( imaplen > (addrsize + intsize + 1) )
1136     {
1137         /* skip child unit address and child interrupt specifier */
1138         imap += addrsize + intsize;
1139         imaplen -= addrsize + intsize;
1140 
1141         /* Get the interrupt parent */
1142         ipar = dt_find_node_by_phandle(be32_to_cpup(imap));
1143         imap++;
1144         --imaplen;
1145 
1146         /* Check if not found */
1147         if ( ipar == NULL )
1148         {
1149             dt_dprintk(" -> imap parent not found !\n");
1150             goto fail;
1151         }
1152 
1153         dt_dprintk(" -> ipar %s\n", dt_node_name(ipar));
1154 
1155         /* Get #interrupt-cells and #address-cells of new
1156          * parent
1157          */
1158         tmp = dt_get_property(ipar, "#interrupt-cells", NULL);
1159         if ( tmp == NULL )
1160         {
1161             dt_dprintk(" -> parent lacks #interrupt-cells!\n");
1162             goto fail;
1163         }
1164         pintsize = be32_to_cpu(*tmp);
1165         tmp = dt_get_property(ipar, "#address-cells", NULL);
1166         paddrsize = (tmp == NULL) ? 0 : be32_to_cpu(*tmp);
1167 
1168         dt_dprintk(" -> pintsize=%d, paddrsize=%d\n",
1169                    pintsize, paddrsize);
1170 
1171         if ( pintsize > DT_MAX_IRQ_SPEC )
1172         {
1173             dt_dprintk(" -> too many irq specifier cells in parent\n");
1174             goto fail;
1175         }
1176 
1177         /* Check for malformed properties */
1178         if ( imaplen < (paddrsize + pintsize) )
1179             goto fail;
1180 
1181         imap += paddrsize;
1182         imaplen -= paddrsize;
1183 
1184         dt_raw_irq.controller = ipar;
1185         dt_raw_irq.size = pintsize;
1186         for ( i = 0; i < pintsize; i++ )
1187             dt_raw_irq.specifier[i] = dt_read_number(imap + i, 1);
1188 
1189         if ( dt_raw_irq.controller != dt_interrupt_controller )
1190         {
1191             /*
1192              * We don't map IRQs connected to secondary IRQ controllers as
1193              * these IRQs have no meaning to us until they connect to the
1194              * primary controller.
1195              *
1196              * Secondary IRQ controllers will at some point connect to
1197              * the primary controller (possibly via other IRQ controllers).
1198              * We map the IRQs at that last connection point.
1199              */
1200             imap += pintsize;
1201             imaplen -= pintsize;
1202             dt_dprintk(" -> Skipped IRQ for secondary IRQ controller\n");
1203             continue;
1204         }
1205 
1206         ret = dt_irq_translate(&dt_raw_irq, &dt_irq);
1207         if ( ret )
1208         {
1209             dt_dprintk(" -> failed to translate IRQ: %d\n", ret);
1210             return ret;
1211         }
1212 
1213         ret = cb(dev, &dt_irq, data);
1214         if ( ret )
1215         {
1216             dt_dprintk(" -> callback failed=%d\n", ret);
1217             return ret;
1218         }
1219 
1220         imap += pintsize;
1221         imaplen -= pintsize;
1222 
1223         dt_dprintk(" -> imaplen=%d\n", imaplen);
1224     }
1225 
1226     return 0;
1227 
1228 fail:
1229     return -EINVAL;
1230 }
1231 
1232 /**
1233  * dt_irq_map_raw - Low level interrupt tree parsing
1234  * @parent:     the device interrupt parent
1235  * @intspec:    interrupt specifier ("interrupts" property of the device)
1236  * @ointsize:   size of the passed in interrupt specifier
1237  * @addr:       address specifier (start of "reg" property of the device)
1238  * @oirq:       structure dt_raw_irq filled by this function
1239  *
1240  * Returns 0 on success and a negative number on error
1241  *
1242  * This function is a low-level interrupt tree walking function. It
1243  * can be used to do a partial walk with synthesized reg and interrupts
1244  * properties, for example when resolving PCI interrupts when no device
1245  * node exist for the parent.
1246  */
dt_irq_map_raw(const struct dt_device_node * parent,const __be32 * intspec,u32 ointsize,const __be32 * addr,struct dt_raw_irq * oirq)1247 static int dt_irq_map_raw(const struct dt_device_node *parent,
1248                           const __be32 *intspec, u32 ointsize,
1249                           const __be32 *addr,
1250                           struct dt_raw_irq *oirq)
1251 {
1252     const struct dt_device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
1253     const __be32 *tmp, *imap, *imask;
1254     u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
1255     u32 imaplen;
1256     int match, i;
1257 
1258     dt_dprintk("dt_irq_map_raw: par=%s,intspec=[0x%08x 0x%08x...],ointsize=%d\n",
1259                parent->full_name, be32_to_cpup(intspec),
1260                be32_to_cpup(intspec + 1), ointsize);
1261 
1262     ipar = parent;
1263 
1264     /* First get the #interrupt-cells property of the current cursor
1265      * that tells us how to interpret the passed-in intspec. If there
1266      * is none, we are nice and just walk up the tree
1267      */
1268     do {
1269         tmp = dt_get_property(ipar, "#interrupt-cells", NULL);
1270         if ( tmp != NULL )
1271         {
1272             intsize = be32_to_cpu(*tmp);
1273             break;
1274         }
1275         tnode = ipar;
1276         ipar = dt_irq_find_parent(ipar);
1277     } while ( ipar );
1278     if ( ipar == NULL )
1279     {
1280         dt_dprintk(" -> no parent found !\n");
1281         goto fail;
1282     }
1283 
1284     dt_dprintk("dt_irq_map_raw: ipar=%s, size=%d\n", ipar->full_name, intsize);
1285 
1286     if ( ointsize != intsize )
1287         return -EINVAL;
1288 
1289     /* Look for this #address-cells. We have to implement the old linux
1290      * trick of looking for the parent here as some device-trees rely on it
1291      */
1292     old = ipar;
1293     do {
1294         tmp = dt_get_property(old, "#address-cells", NULL);
1295         tnode = dt_get_parent(old);
1296         old = tnode;
1297     } while ( old && tmp == NULL );
1298 
1299     old = NULL;
1300     addrsize = (tmp == NULL) ? 2 : be32_to_cpu(*tmp);
1301 
1302     dt_dprintk(" -> addrsize=%d\n", addrsize);
1303 
1304     /* Now start the actual "proper" walk of the interrupt tree */
1305     while ( ipar != NULL )
1306     {
1307         /* Now check if cursor is an interrupt-controller and if it is
1308          * then we are done
1309          */
1310         if ( dt_get_property(ipar, "interrupt-controller", NULL) != NULL )
1311         {
1312             dt_dprintk(" -> got it !\n");
1313             if ( intsize > DT_MAX_IRQ_SPEC )
1314             {
1315                 dt_dprintk(" -> intsize(%u) greater than DT_MAX_IRQ_SPEC(%u)\n",
1316                            intsize, DT_MAX_IRQ_SPEC);
1317                 goto fail;
1318             }
1319             for ( i = 0; i < intsize; i++ )
1320                 oirq->specifier[i] = dt_read_number(intspec + i, 1);
1321             oirq->size = intsize;
1322             oirq->controller = ipar;
1323             return 0;
1324         }
1325 
1326         /* Now look for an interrupt-map */
1327         imap = dt_get_property(ipar, "interrupt-map", &imaplen);
1328         /* No interrupt map, check for an interrupt parent */
1329         if ( imap == NULL )
1330         {
1331             dt_dprintk(" -> no map, getting parent\n");
1332             newpar = dt_irq_find_parent(ipar);
1333             goto skiplevel;
1334         }
1335         imaplen /= sizeof(u32);
1336 
1337         /* Look for a mask */
1338         imask = dt_get_property(ipar, "interrupt-map-mask", NULL);
1339 
1340         /* If we were passed no "reg" property and we attempt to parse
1341          * an interrupt-map, then #address-cells must be 0.
1342          * Fail if it's not.
1343          */
1344         if ( addr == NULL && addrsize != 0 )
1345         {
1346             dt_dprintk(" -> no reg passed in when needed !\n");
1347             goto fail;
1348         }
1349 
1350         /* Parse interrupt-map */
1351         match = 0;
1352         while ( imaplen > (addrsize + intsize + 1) && !match )
1353         {
1354             /* Compare specifiers */
1355             match = 1;
1356             for ( i = 0; i < addrsize && match; ++i )
1357             {
1358                 __be32 mask = imask ? imask[i] : cpu_to_be32(0xffffffffu);
1359                 match = ((addr[i] ^ imap[i]) & mask) == 0;
1360             }
1361             for ( ; i < (addrsize + intsize) && match; ++i )
1362             {
1363                 __be32 mask = imask ? imask[i] : cpu_to_be32(0xffffffffu);
1364                 match = ((intspec[i-addrsize] ^ imap[i]) & mask) == 0;
1365             }
1366             imap += addrsize + intsize;
1367             imaplen -= addrsize + intsize;
1368 
1369             dt_dprintk(" -> match=%d (imaplen=%d)\n", match, imaplen);
1370 
1371             /* Get the interrupt parent */
1372             newpar = dt_find_node_by_phandle(be32_to_cpup(imap));
1373             imap++;
1374             --imaplen;
1375 
1376             /* Check if not found */
1377             if ( newpar == NULL )
1378             {
1379                 dt_dprintk(" -> imap parent not found !\n");
1380                 goto fail;
1381             }
1382 
1383             /* Get #interrupt-cells and #address-cells of new
1384              * parent
1385              */
1386             tmp = dt_get_property(newpar, "#interrupt-cells", NULL);
1387             if ( tmp == NULL )
1388             {
1389                 dt_dprintk(" -> parent lacks #interrupt-cells!\n");
1390                 goto fail;
1391             }
1392             newintsize = be32_to_cpu(*tmp);
1393             tmp = dt_get_property(newpar, "#address-cells", NULL);
1394             newaddrsize = (tmp == NULL) ? 0 : be32_to_cpu(*tmp);
1395 
1396             dt_dprintk(" -> newintsize=%d, newaddrsize=%d\n",
1397                        newintsize, newaddrsize);
1398 
1399             /* Check for malformed properties */
1400             if ( imaplen < (newaddrsize + newintsize) )
1401                 goto fail;
1402 
1403             imap += newaddrsize + newintsize;
1404             imaplen -= newaddrsize + newintsize;
1405 
1406             dt_dprintk(" -> imaplen=%d\n", imaplen);
1407         }
1408         if ( !match )
1409             goto fail;
1410 
1411         old = newpar;
1412         addrsize = newaddrsize;
1413         intsize = newintsize;
1414         intspec = imap - intsize;
1415         addr = intspec - addrsize;
1416 
1417     skiplevel:
1418         /* Iterate again with new parent */
1419         dt_dprintk(" -> new parent: %s\n", dt_node_full_name(newpar));
1420         ipar = newpar;
1421         newpar = NULL;
1422     }
1423 fail:
1424     return -EINVAL;
1425 }
1426 
dt_device_get_raw_irq(const struct dt_device_node * device,unsigned int index,struct dt_raw_irq * out_irq)1427 int dt_device_get_raw_irq(const struct dt_device_node *device,
1428                           unsigned int index,
1429                           struct dt_raw_irq *out_irq)
1430 {
1431     const struct dt_device_node *p;
1432     const __be32 *intspec, *tmp, *addr;
1433     u32 intsize, intlen;
1434     int res = -EINVAL;
1435     struct dt_phandle_args args;
1436     int i;
1437 
1438     dt_dprintk("dt_device_get_raw_irq: dev=%s, index=%u\n",
1439                device->full_name, index);
1440 
1441     /* Get the reg property (if any) */
1442     addr = dt_get_property(device, "reg", NULL);
1443 
1444     /* Try the new-style interrupts-extended first */
1445     res = dt_parse_phandle_with_args(device, "interrupts-extended",
1446                                      "#interrupt-cells", index, &args);
1447     if ( !res )
1448     {
1449         dt_dprintk(" using 'interrupts-extended' property\n");
1450         dt_dprintk(" intspec=%d intsize=%d\n", args.args[0], args.args_count);
1451 
1452         for ( i = 0; i < args.args_count; i++ )
1453             args.args[i] = cpu_to_be32(args.args[i]);
1454 
1455         return dt_irq_map_raw(args.np, args.args, args.args_count,
1456                               addr, out_irq);
1457     }
1458 
1459     /* Get the interrupts property */
1460     intspec = dt_get_property(device, "interrupts", &intlen);
1461     if ( intspec == NULL )
1462         return -EINVAL;
1463     intlen /= sizeof(*intspec);
1464 
1465     dt_dprintk(" using 'interrupts' property\n");
1466     dt_dprintk(" intspec=%d intlen=%d\n", be32_to_cpup(intspec), intlen);
1467 
1468     /* Look for the interrupt parent. */
1469     p = dt_irq_find_parent(device);
1470     if ( p == NULL )
1471         return -EINVAL;
1472 
1473     /* Get size of interrupt specifier */
1474     tmp = dt_get_property(p, "#interrupt-cells", NULL);
1475     if ( tmp == NULL )
1476         goto out;
1477     intsize = be32_to_cpu(*tmp);
1478 
1479     dt_dprintk(" intsize=%d intlen=%d\n", intsize, intlen);
1480 
1481     /* Check index */
1482     if ( (index + 1) * intsize > intlen )
1483         goto out;
1484 
1485     /* Get new specifier and map it */
1486     res = dt_irq_map_raw(p, intspec + index * intsize, intsize,
1487                          addr, out_irq);
1488     if ( res )
1489         goto out;
1490 out:
1491     return res;
1492 }
1493 
dt_irq_translate(const struct dt_raw_irq * raw,struct dt_irq * out_irq)1494 int dt_irq_translate(const struct dt_raw_irq *raw,
1495                      struct dt_irq *out_irq)
1496 {
1497     ASSERT(dt_irq_xlate != NULL);
1498     ASSERT(dt_interrupt_controller != NULL);
1499 
1500     /*
1501      * TODO: Retrieve the right irq_xlate. This is only works for the primary
1502      * interrupt controller.
1503      */
1504     if ( raw->controller != dt_interrupt_controller )
1505         return -EINVAL;
1506 
1507     return dt_irq_xlate(raw->specifier, raw->size,
1508                         &out_irq->irq, &out_irq->type);
1509 }
1510 
dt_device_get_irq(const struct dt_device_node * device,unsigned int index,struct dt_irq * out_irq)1511 int dt_device_get_irq(const struct dt_device_node *device, unsigned int index,
1512                       struct dt_irq *out_irq)
1513 {
1514     struct dt_raw_irq raw;
1515     int res;
1516 
1517     res = dt_device_get_raw_irq(device, index, &raw);
1518 
1519     if ( res )
1520         return res;
1521 
1522     return dt_irq_translate(&raw, out_irq);
1523 }
1524 
dt_device_is_available(const struct dt_device_node * device)1525 bool_t dt_device_is_available(const struct dt_device_node *device)
1526 {
1527     const char *status;
1528     u32 statlen;
1529 
1530     status = dt_get_property(device, "status", &statlen);
1531     if ( status == NULL )
1532         return 1;
1533 
1534     if ( statlen > 0 )
1535     {
1536         if ( !strcmp(status, "okay") || !strcmp(status, "ok") )
1537             return 1;
1538     }
1539 
1540     return 0;
1541 }
1542 
dt_device_for_passthrough(const struct dt_device_node * device)1543 bool_t dt_device_for_passthrough(const struct dt_device_node *device)
1544 {
1545     return (dt_find_property(device, "xen,passthrough", NULL) != NULL);
1546 
1547 }
1548 
__dt_parse_phandle_with_args(const struct dt_device_node * np,const char * list_name,const char * cells_name,int cell_count,int index,struct dt_phandle_args * out_args)1549 static int __dt_parse_phandle_with_args(const struct dt_device_node *np,
1550                                         const char *list_name,
1551                                         const char *cells_name,
1552                                         int cell_count, int index,
1553                                         struct dt_phandle_args *out_args)
1554 {
1555     const __be32 *list, *list_end;
1556     int rc = 0, cur_index = 0;
1557     u32 size, count = 0;
1558     struct dt_device_node *node = NULL;
1559     dt_phandle phandle;
1560 
1561     /* Retrieve the phandle list property */
1562     list = dt_get_property(np, list_name, &size);
1563     if ( !list )
1564         return -ENOENT;
1565     list_end = list + size / sizeof(*list);
1566 
1567     /* Loop over the phandles until all the requested entry is found */
1568     while ( list < list_end )
1569     {
1570         rc = -EINVAL;
1571         count = 0;
1572 
1573         /*
1574          * If phandle is 0, then it is an empty entry with no
1575          * arguments.  Skip forward to the next entry.
1576          * */
1577         phandle = be32_to_cpup(list++);
1578         if ( phandle )
1579         {
1580             /*
1581              * Find the provider node and parse the #*-cells
1582              * property to determine the argument length.
1583              *
1584              * This is not needed if the cell count is hard-coded
1585              * (i.e. cells_name not set, but cell_count is set),
1586              * except when we're going to return the found node
1587              * below.
1588              */
1589             if ( cells_name || cur_index == index )
1590             {
1591                 node = dt_find_node_by_phandle(phandle);
1592                 if ( !node )
1593                 {
1594                     printk(XENLOG_ERR "%s: could not find phandle\n",
1595                            np->full_name);
1596                     goto err;
1597                 }
1598             }
1599 
1600             if ( cells_name )
1601             {
1602                 if ( !dt_property_read_u32(node, cells_name, &count) )
1603                 {
1604                     printk("%s: could not get %s for %s\n",
1605                            np->full_name, cells_name, node->full_name);
1606                     goto err;
1607                 }
1608             }
1609             else
1610                 count = cell_count;
1611 
1612             /*
1613              * Make sure that the arguments actually fit in the
1614              * remaining property data length
1615              */
1616             if ( list + count > list_end )
1617             {
1618                 printk(XENLOG_ERR "%s: arguments longer than property\n",
1619                        np->full_name);
1620                 goto err;
1621             }
1622         }
1623 
1624         /*
1625          * All of the error cases above bail out of the loop, so at
1626          * this point, the parsing is successful. If the requested
1627          * index matches, then fill the out_args structure and return,
1628          * or return -ENOENT for an empty entry.
1629          */
1630         rc = -ENOENT;
1631         if ( cur_index == index )
1632         {
1633             if (!phandle)
1634                 goto err;
1635 
1636             if ( out_args )
1637             {
1638                 int i;
1639 
1640                 WARN_ON(count > MAX_PHANDLE_ARGS);
1641                 if (count > MAX_PHANDLE_ARGS)
1642                     count = MAX_PHANDLE_ARGS;
1643                 out_args->np = node;
1644                 out_args->args_count = count;
1645                 for ( i = 0; i < count; i++ )
1646                     out_args->args[i] = be32_to_cpup(list++);
1647             }
1648 
1649             /* Found it! return success */
1650             return 0;
1651         }
1652 
1653         node = NULL;
1654         list += count;
1655         cur_index++;
1656     }
1657 
1658     /*
1659      * Returning result will be one of:
1660      * -ENOENT : index is for empty phandle
1661      * -EINVAL : parsing error on data
1662      * [1..n]  : Number of phandle (count mode; when index = -1)
1663      */
1664     rc = index < 0 ? cur_index : -ENOENT;
1665 err:
1666     return rc;
1667 }
1668 
dt_parse_phandle(const struct dt_device_node * np,const char * phandle_name,int index)1669 struct dt_device_node *dt_parse_phandle(const struct dt_device_node *np,
1670                                         const char *phandle_name, int index)
1671 {
1672     struct dt_phandle_args args;
1673 
1674     if (index < 0)
1675         return NULL;
1676 
1677     if (__dt_parse_phandle_with_args(np, phandle_name, NULL, 0,
1678                                      index, &args))
1679         return NULL;
1680 
1681     return args.np;
1682 }
1683 
1684 
dt_parse_phandle_with_args(const struct dt_device_node * np,const char * list_name,const char * cells_name,int index,struct dt_phandle_args * out_args)1685 int dt_parse_phandle_with_args(const struct dt_device_node *np,
1686                                const char *list_name,
1687                                const char *cells_name, int index,
1688                                struct dt_phandle_args *out_args)
1689 {
1690     if ( index < 0 )
1691         return -EINVAL;
1692     return __dt_parse_phandle_with_args(np, list_name, cells_name, 0,
1693                                         index, out_args);
1694 }
1695 
dt_count_phandle_with_args(const struct dt_device_node * np,const char * list_name,const char * cells_name)1696 int dt_count_phandle_with_args(const struct dt_device_node *np,
1697                                const char *list_name,
1698                                const char *cells_name)
1699 {
1700     return __dt_parse_phandle_with_args(np, list_name, cells_name, 0, -1, NULL);
1701 }
1702 
1703 /**
1704  * unflatten_dt_node - Alloc and populate a device_node from the flat tree
1705  * @fdt: The parent device tree blob
1706  * @mem: Memory chunk to use for allocating device nodes and properties
1707  * @p: pointer to node in flat tree
1708  * @dad: Parent struct device_node
1709  * @allnextpp: pointer to ->allnext from last allocated device_node
1710  * @fpsize: Size of the node path up at the current depth.
1711  */
unflatten_dt_node(const void * fdt,unsigned long mem,unsigned long * p,struct dt_device_node * dad,struct dt_device_node *** allnextpp,unsigned long fpsize)1712 static unsigned long __init unflatten_dt_node(const void *fdt,
1713                                               unsigned long mem,
1714                                               unsigned long *p,
1715                                               struct dt_device_node *dad,
1716                                               struct dt_device_node ***allnextpp,
1717                                               unsigned long fpsize)
1718 {
1719     struct dt_device_node *np;
1720     struct dt_property *pp, **prev_pp = NULL;
1721     char *pathp;
1722     u32 tag;
1723     unsigned int l, allocl;
1724     int has_name = 0;
1725     int new_format = 0;
1726 
1727     tag = be32_to_cpup((__be32 *)(*p));
1728     if ( tag != FDT_BEGIN_NODE )
1729     {
1730         printk(XENLOG_WARNING "Weird tag at start of node: %x\n", tag);
1731         return mem;
1732     }
1733     *p += 4;
1734     pathp = (char *)*p;
1735     l = allocl = strlen(pathp) + 1;
1736     *p = ROUNDUP(*p + l, 4);
1737 
1738     /* version 0x10 has a more compact unit name here instead of the full
1739      * path. we accumulate the full path size using "fpsize", we'll rebuild
1740      * it later. We detect this because the first character of the name is
1741      * not '/'.
1742      */
1743     if ( (*pathp) != '/' )
1744     {
1745         new_format = 1;
1746         if ( fpsize == 0 )
1747         {
1748             /* root node: special case. fpsize accounts for path
1749              * plus terminating zero. root node only has '/', so
1750              * fpsize should be 2, but we want to avoid the first
1751              * level nodes to have two '/' so we use fpsize 1 here
1752              */
1753             fpsize = 1;
1754             allocl = 2;
1755         }
1756         else
1757         {
1758             /* account for '/' and path size minus terminal 0
1759              * already in 'l'
1760              */
1761             fpsize += l;
1762             allocl = fpsize;
1763         }
1764     }
1765 
1766     np = unflatten_dt_alloc(&mem, sizeof(struct dt_device_node) + allocl,
1767                             __alignof__(struct dt_device_node));
1768     if ( allnextpp )
1769     {
1770         memset(np, 0, sizeof(*np));
1771         np->full_name = ((char *)np) + sizeof(struct dt_device_node);
1772         /* By default dom0 owns the device */
1773         np->used_by = 0;
1774         /* By default the device is not protected */
1775         np->is_protected = false;
1776         INIT_LIST_HEAD(&np->domain_list);
1777 
1778         if ( new_format )
1779         {
1780             char *fn = np->full_name;
1781             /* rebuild full path for new format */
1782             if ( dad && dad->parent )
1783             {
1784                 strlcpy(fn, dad->full_name, allocl);
1785 #ifdef DEBUG_DT
1786                 if ( (strlen(fn) + l + 1) != allocl )
1787                 {
1788                     dt_dprintk("%s: p: %d, l: %d, a: %d\n",
1789                                pathp, (int)strlen(fn),
1790                                l, allocl);
1791                 }
1792 #endif
1793                 fn += strlen(fn);
1794             }
1795             *(fn++) = '/';
1796             memcpy(fn, pathp, l);
1797         }
1798         else
1799             memcpy(np->full_name, pathp, l);
1800         prev_pp = &np->properties;
1801         **allnextpp = np;
1802         *allnextpp = &np->allnext;
1803         if ( dad != NULL )
1804         {
1805             np->parent = dad;
1806             /* we temporarily use the next field as `last_child'*/
1807             if ( dad->next == NULL )
1808                 dad->child = np;
1809             else
1810                 dad->next->sibling = np;
1811             dad->next = np;
1812         }
1813     }
1814     /* process properties */
1815     while ( 1 )
1816     {
1817         u32 sz, noff;
1818         const char *pname;
1819 
1820         tag = be32_to_cpup((__be32 *)(*p));
1821         if ( tag == FDT_NOP )
1822         {
1823             *p += 4;
1824             continue;
1825         }
1826         if ( tag != FDT_PROP )
1827             break;
1828         *p += 4;
1829         sz = be32_to_cpup((__be32 *)(*p));
1830         noff = be32_to_cpup((__be32 *)((*p) + 4));
1831         *p += 8;
1832         if ( fdt_version(fdt) < 0x10 )
1833             *p = ROUNDUP(*p, sz >= 8 ? 8 : 4);
1834 
1835         pname = fdt_string(fdt, noff);
1836         if ( pname == NULL )
1837         {
1838             dt_dprintk("Can't find property name in list!\n");
1839             break;
1840         }
1841         if ( strcmp(pname, "name") == 0 )
1842             has_name = 1;
1843         l = strlen(pname) + 1;
1844         pp = unflatten_dt_alloc(&mem, sizeof(struct dt_property),
1845                                 __alignof__(struct dt_property));
1846         if ( allnextpp )
1847         {
1848             /* We accept flattened tree phandles either in
1849              * ePAPR-style "phandle" properties, or the
1850              * legacy "linux,phandle" properties.  If both
1851              * appear and have different values, things
1852              * will get weird.  Don't do that. */
1853             if ( (strcmp(pname, "phandle") == 0) ||
1854                  (strcmp(pname, "linux,phandle") == 0) )
1855             {
1856                 if ( np->phandle == 0 )
1857                     np->phandle = be32_to_cpup((__be32*)*p);
1858             }
1859             /* And we process the "ibm,phandle" property
1860              * used in pSeries dynamic device tree
1861              * stuff */
1862             if ( strcmp(pname, "ibm,phandle") == 0 )
1863                 np->phandle = be32_to_cpup((__be32 *)*p);
1864             pp->name = pname;
1865             pp->length = sz;
1866             pp->value = (void *)*p;
1867             *prev_pp = pp;
1868             prev_pp = &pp->next;
1869         }
1870         *p = ROUNDUP((*p) + sz, 4);
1871     }
1872     /* with version 0x10 we may not have the name property, recreate
1873      * it here from the unit name if absent
1874      */
1875     if ( !has_name )
1876     {
1877         char *p1 = pathp, *ps = pathp, *pa = NULL;
1878         int sz;
1879 
1880         while ( *p1 )
1881         {
1882             if ( (*p1) == '@' )
1883                 pa = p1;
1884             if ( (*p1) == '/' )
1885                 ps = p1 + 1;
1886             p1++;
1887         }
1888         if ( pa < ps )
1889             pa = p1;
1890         sz = (pa - ps) + 1;
1891         pp = unflatten_dt_alloc(&mem, sizeof(struct dt_property) + sz,
1892                                 __alignof__(struct dt_property));
1893         if ( allnextpp )
1894         {
1895             pp->name = "name";
1896             pp->length = sz;
1897             pp->value = pp + 1;
1898             /*
1899              * The device tree creation code assume that the property
1900              * "name" is not a fake.
1901              * To avoid a big divergence with Linux code, only remove
1902              * property link. In this case we will lose a bit of memory
1903              */
1904 #if 0
1905             *prev_pp = pp;
1906             prev_pp = &pp->next;
1907 #endif
1908             np->name = pp->value;
1909             memcpy(pp->value, ps, sz - 1);
1910             ((char *)pp->value)[sz - 1] = 0;
1911             dt_dprintk("fixed up name for %s -> %s\n", pathp,
1912                        (char *)pp->value);
1913             /* Generic device initialization */
1914             np->dev.type = DEV_DT;
1915             np->dev.of_node = np;
1916         }
1917     }
1918     if ( allnextpp )
1919     {
1920         *prev_pp = NULL;
1921         np->name = (np->name) ? : dt_get_property(np, "name", NULL);
1922         np->type = dt_get_property(np, "device_type", NULL);
1923 
1924         if ( !np->name )
1925             np->name = "<NULL>";
1926         if ( !np->type )
1927             np->type = "<NULL>";
1928     }
1929     while ( tag == FDT_BEGIN_NODE || tag == FDT_NOP )
1930     {
1931         if ( tag == FDT_NOP )
1932             *p += 4;
1933         else
1934             mem = unflatten_dt_node(fdt, mem, p, np, allnextpp, fpsize);
1935         tag = be32_to_cpup((__be32 *)(*p));
1936     }
1937     if ( tag != FDT_END_NODE )
1938     {
1939         printk(XENLOG_WARNING "Weird tag at end of node: %x\n", tag);
1940         return mem;
1941     }
1942 
1943     *p += 4;
1944     return mem;
1945 }
1946 
1947 /**
1948  * __unflatten_device_tree - create tree of device_nodes from flat blob
1949  *
1950  * unflattens a device-tree, creating the
1951  * tree of struct device_node. It also fills the "name" and "type"
1952  * pointers of the nodes so the normal device-tree walking functions
1953  * can be used.
1954  * @fdt: The fdt to expand
1955  * @mynodes: The device_node tree created by the call
1956  */
__unflatten_device_tree(const void * fdt,struct dt_device_node ** mynodes)1957 static void __init __unflatten_device_tree(const void *fdt,
1958                                            struct dt_device_node **mynodes)
1959 {
1960     unsigned long start, mem, size;
1961     struct dt_device_node **allnextp = mynodes;
1962 
1963     dt_dprintk(" -> unflatten_device_tree()\n");
1964 
1965     dt_dprintk("Unflattening device tree:\n");
1966     dt_dprintk("magic: %#08x\n", fdt_magic(fdt));
1967     dt_dprintk("size: %#08x\n", fdt_totalsize(fdt));
1968     dt_dprintk("version: %#08x\n", fdt_version(fdt));
1969 
1970     /* First pass, scan for size */
1971     start = ((unsigned long)fdt) + fdt_off_dt_struct(fdt);
1972     size = unflatten_dt_node(fdt, 0, &start, NULL, NULL, 0);
1973     size = (size | 3) + 1;
1974 
1975     dt_dprintk("  size is %#lx allocating...\n", size);
1976 
1977     /* Allocate memory for the expanded device tree */
1978     mem = (unsigned long)_xmalloc (size + 4, __alignof__(struct dt_device_node));
1979 
1980     ((__be32 *)mem)[size / 4] = cpu_to_be32(0xdeadbeef);
1981 
1982     dt_dprintk("  unflattening %lx...\n", mem);
1983 
1984     /* Second pass, do actual unflattening */
1985     start = ((unsigned long)fdt) + fdt_off_dt_struct(fdt);
1986     unflatten_dt_node(fdt, mem, &start, NULL, &allnextp, 0);
1987     if ( be32_to_cpup((__be32 *)start) != FDT_END )
1988         printk(XENLOG_WARNING "Weird tag at end of tree: %08x\n",
1989                   *((u32 *)start));
1990     if ( be32_to_cpu(((__be32 *)mem)[size / 4]) != 0xdeadbeef )
1991         printk(XENLOG_WARNING "End of tree marker overwritten: %08x\n",
1992                   be32_to_cpu(((__be32 *)mem)[size / 4]));
1993     *allnextp = NULL;
1994 
1995     dt_dprintk(" <- unflatten_device_tree()\n");
1996 }
1997 
dt_alias_add(struct dt_alias_prop * ap,struct dt_device_node * np,int id,const char * stem,int stem_len)1998 static void dt_alias_add(struct dt_alias_prop *ap,
1999                          struct dt_device_node *np,
2000                          int id, const char *stem, int stem_len)
2001 {
2002     ap->np = np;
2003     ap->id = id;
2004     strlcpy(ap->stem, stem, stem_len + 1);
2005     list_add_tail(&ap->link, &aliases_lookup);
2006     dt_dprintk("adding DT alias:%s: stem=%s id=%d node=%s\n",
2007                ap->alias, ap->stem, ap->id, dt_node_full_name(np));
2008 }
2009 
2010 /**
2011  * dt_alias_scan - Scan all properties of 'aliases' node
2012  *
2013  * The function scans all the properties of 'aliases' node and populate
2014  * the the global lookup table with the properties.  It returns the
2015  * number of alias_prop found, or error code in error case.
2016  */
dt_alias_scan(void)2017 static void __init dt_alias_scan(void)
2018 {
2019     const struct dt_property *pp;
2020     const struct dt_device_node *aliases;
2021 
2022     aliases = dt_find_node_by_path("/aliases");
2023     if ( !aliases )
2024         return;
2025 
2026     dt_for_each_property_node( aliases, pp )
2027     {
2028         const char *start = pp->name;
2029         const char *end = start + strlen(start);
2030         struct dt_device_node *np;
2031         struct dt_alias_prop *ap;
2032         int id, len;
2033 
2034         /* Skip those we do not want to proceed */
2035         if ( !strcmp(pp->name, "name") ||
2036              !strcmp(pp->name, "phandle") ||
2037              !strcmp(pp->name, "linux,phandle") )
2038             continue;
2039 
2040         np = dt_find_node_by_path(pp->value);
2041         if ( !np )
2042             continue;
2043 
2044         /* walk the alias backwards to extract the id and work out
2045          * the 'stem' string */
2046         while ( isdigit(*(end-1)) && end > start )
2047             end--;
2048         len = end - start;
2049 
2050         id = simple_strtoll(end, NULL, 10);
2051 
2052         /* Allocate an alias_prop with enough space for the stem */
2053         ap = _xmalloc(sizeof(*ap) + len + 1, 4);
2054         if ( !ap )
2055             continue;
2056         ap->alias = start;
2057         dt_alias_add(ap, np, id, start, len);
2058     }
2059 }
2060 
2061 struct dt_device_node * __init
dt_find_interrupt_controller(const struct dt_device_match * matches)2062 dt_find_interrupt_controller(const struct dt_device_match *matches)
2063 {
2064     struct dt_device_node *np = NULL;
2065 
2066     while ( (np = dt_find_matching_node(np, matches)) )
2067     {
2068         if ( !dt_find_property(np, "interrupt-controller", NULL) )
2069             continue;
2070 
2071         if ( dt_get_parent(np) )
2072             break;
2073     }
2074 
2075     return np;
2076 }
2077 
dt_unflatten_host_device_tree(void)2078 void __init dt_unflatten_host_device_tree(void)
2079 {
2080     __unflatten_device_tree(device_tree_flattened, &dt_host);
2081     dt_alias_scan();
2082 }
2083 
2084 /*
2085  * Local variables:
2086  * mode: C
2087  * c-file-style: "BSD"
2088  * c-basic-offset: 4
2089  * indent-tabs-mode: nil
2090  * End:
2091  */
2092