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