of/flattree: merge of_get_flat_dt_prop
[cascardo/linux.git] / arch / microblaze / kernel / prom.c
1 /*
2  * Procedures for creating, accessing and interpreting the device tree.
3  *
4  * Paul Mackerras       August 1996.
5  * Copyright (C) 1996-2005 Paul Mackerras.
6  *
7  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8  *    {engebret|bergner}@us.ibm.com
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15
16 #include <stdarg.h>
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/init.h>
20 #include <linux/threads.h>
21 #include <linux/spinlock.h>
22 #include <linux/types.h>
23 #include <linux/pci.h>
24 #include <linux/stringify.h>
25 #include <linux/delay.h>
26 #include <linux/initrd.h>
27 #include <linux/bitops.h>
28 #include <linux/module.h>
29 #include <linux/kexec.h>
30 #include <linux/debugfs.h>
31 #include <linux/irq.h>
32 #include <linux/lmb.h>
33
34 #include <asm/prom.h>
35 #include <asm/page.h>
36 #include <asm/processor.h>
37 #include <asm/irq.h>
38 #include <linux/io.h>
39 #include <asm/system.h>
40 #include <asm/mmu.h>
41 #include <asm/pgtable.h>
42 #include <asm/sections.h>
43 #include <asm/pci-bridge.h>
44
45 static int __initdata dt_root_addr_cells;
46 static int __initdata dt_root_size_cells;
47
48 typedef u32 cell_t;
49
50 /* export that to outside world */
51 struct device_node *of_chosen;
52
53 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
54 {
55         const char *cp;
56         unsigned long cplen, l;
57
58         cp = of_get_flat_dt_prop(node, "compatible", &cplen);
59         if (cp == NULL)
60                 return 0;
61         while (cplen > 0) {
62                 if (strncasecmp(cp, compat, strlen(compat)) == 0)
63                         return 1;
64                 l = strlen(cp) + 1;
65                 cp += l;
66                 cplen -= l;
67         }
68
69         return 0;
70 }
71
72 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
73                                         unsigned long align)
74 {
75         void *res;
76
77         *mem = _ALIGN(*mem, align);
78         res = (void *)*mem;
79         *mem += size;
80
81         return res;
82 }
83
84 static unsigned long __init unflatten_dt_node(unsigned long mem,
85                                         unsigned long *p,
86                                         struct device_node *dad,
87                                         struct device_node ***allnextpp,
88                                         unsigned long fpsize)
89 {
90         struct device_node *np;
91         struct property *pp, **prev_pp = NULL;
92         char *pathp;
93         u32 tag;
94         unsigned int l, allocl;
95         int has_name = 0;
96         int new_format = 0;
97
98         tag = *((u32 *)(*p));
99         if (tag != OF_DT_BEGIN_NODE) {
100                 printk("Weird tag at start of node: %x\n", tag);
101                 return mem;
102         }
103         *p += 4;
104         pathp = (char *)*p;
105         l = allocl = strlen(pathp) + 1;
106         *p = _ALIGN(*p + l, 4);
107
108         /* version 0x10 has a more compact unit name here instead of the full
109          * path. we accumulate the full path size using "fpsize", we'll rebuild
110          * it later. We detect this because the first character of the name is
111          * not '/'.
112          */
113         if ((*pathp) != '/') {
114                 new_format = 1;
115                 if (fpsize == 0) {
116                         /* root node: special case. fpsize accounts for path
117                          * plus terminating zero. root node only has '/', so
118                          * fpsize should be 2, but we want to avoid the first
119                          * level nodes to have two '/' so we use fpsize 1 here
120                          */
121                         fpsize = 1;
122                         allocl = 2;
123                 } else {
124                         /* account for '/' and path size minus terminal 0
125                          * already in 'l'
126                          */
127                         fpsize += l;
128                         allocl = fpsize;
129                 }
130         }
131
132         np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
133                                 __alignof__(struct device_node));
134         if (allnextpp) {
135                 memset(np, 0, sizeof(*np));
136                 np->full_name = ((char *)np) + sizeof(struct device_node);
137                 if (new_format) {
138                         char *p2 = np->full_name;
139                         /* rebuild full path for new format */
140                         if (dad && dad->parent) {
141                                 strcpy(p2, dad->full_name);
142 #ifdef DEBUG
143                                 if ((strlen(p2) + l + 1) != allocl) {
144                                         pr_debug("%s: p: %d, l: %d, a: %d\n",
145                                                 pathp, (int)strlen(p2),
146                                                 l, allocl);
147                                 }
148 #endif
149                                 p2 += strlen(p2);
150                         }
151                         *(p2++) = '/';
152                         memcpy(p2, pathp, l);
153                 } else
154                         memcpy(np->full_name, pathp, l);
155                 prev_pp = &np->properties;
156                 **allnextpp = np;
157                 *allnextpp = &np->allnext;
158                 if (dad != NULL) {
159                         np->parent = dad;
160                         /* we temporarily use the next field as `last_child'*/
161                         if (dad->next == NULL)
162                                 dad->child = np;
163                         else
164                                 dad->next->sibling = np;
165                         dad->next = np;
166                 }
167                 kref_init(&np->kref);
168         }
169         while (1) {
170                 u32 sz, noff;
171                 char *pname;
172
173                 tag = *((u32 *)(*p));
174                 if (tag == OF_DT_NOP) {
175                         *p += 4;
176                         continue;
177                 }
178                 if (tag != OF_DT_PROP)
179                         break;
180                 *p += 4;
181                 sz = *((u32 *)(*p));
182                 noff = *((u32 *)((*p) + 4));
183                 *p += 8;
184                 if (initial_boot_params->version < 0x10)
185                         *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
186
187                 pname = find_flat_dt_string(noff);
188                 if (pname == NULL) {
189                         printk(KERN_INFO
190                                 "Can't find property name in list !\n");
191                         break;
192                 }
193                 if (strcmp(pname, "name") == 0)
194                         has_name = 1;
195                 l = strlen(pname) + 1;
196                 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
197                                         __alignof__(struct property));
198                 if (allnextpp) {
199                         if (strcmp(pname, "linux,phandle") == 0) {
200                                 np->node = *((u32 *)*p);
201                                 if (np->linux_phandle == 0)
202                                         np->linux_phandle = np->node;
203                         }
204                         if (strcmp(pname, "ibm,phandle") == 0)
205                                 np->linux_phandle = *((u32 *)*p);
206                         pp->name = pname;
207                         pp->length = sz;
208                         pp->value = (void *)*p;
209                         *prev_pp = pp;
210                         prev_pp = &pp->next;
211                 }
212                 *p = _ALIGN((*p) + sz, 4);
213         }
214         /* with version 0x10 we may not have the name property, recreate
215          * it here from the unit name if absent
216          */
217         if (!has_name) {
218                 char *p1 = pathp, *ps = pathp, *pa = NULL;
219                 int sz;
220
221                 while (*p1) {
222                         if ((*p1) == '@')
223                                 pa = p1;
224                         if ((*p1) == '/')
225                                 ps = p1 + 1;
226                         p1++;
227                 }
228                 if (pa < ps)
229                         pa = p1;
230                 sz = (pa - ps) + 1;
231                 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
232                                         __alignof__(struct property));
233                 if (allnextpp) {
234                         pp->name = "name";
235                         pp->length = sz;
236                         pp->value = pp + 1;
237                         *prev_pp = pp;
238                         prev_pp = &pp->next;
239                         memcpy(pp->value, ps, sz - 1);
240                         ((char *)pp->value)[sz - 1] = 0;
241                         pr_debug("fixed up name for %s -> %s\n", pathp,
242                                 (char *)pp->value);
243                 }
244         }
245         if (allnextpp) {
246                 *prev_pp = NULL;
247                 np->name = of_get_property(np, "name", NULL);
248                 np->type = of_get_property(np, "device_type", NULL);
249
250                 if (!np->name)
251                         np->name = "<NULL>";
252                 if (!np->type)
253                         np->type = "<NULL>";
254         }
255         while (tag == OF_DT_BEGIN_NODE) {
256                 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
257                 tag = *((u32 *)(*p));
258         }
259         if (tag != OF_DT_END_NODE) {
260                 printk(KERN_INFO "Weird tag at end of node: %x\n", tag);
261                 return mem;
262         }
263         *p += 4;
264         return mem;
265 }
266
267 /**
268  * unflattens the device-tree passed by the firmware, creating the
269  * tree of struct device_node. It also fills the "name" and "type"
270  * pointers of the nodes so the normal device-tree walking functions
271  * can be used (this used to be done by finish_device_tree)
272  */
273 void __init unflatten_device_tree(void)
274 {
275         unsigned long start, mem, size;
276         struct device_node **allnextp = &allnodes;
277
278         pr_debug(" -> unflatten_device_tree()\n");
279
280         /* First pass, scan for size */
281         start = ((unsigned long)initial_boot_params) +
282                 initial_boot_params->off_dt_struct;
283         size = unflatten_dt_node(0, &start, NULL, NULL, 0);
284         size = (size | 3) + 1;
285
286         pr_debug("  size is %lx, allocating...\n", size);
287
288         /* Allocate memory for the expanded device tree */
289         mem = lmb_alloc(size + 4, __alignof__(struct device_node));
290         mem = (unsigned long) __va(mem);
291
292         ((u32 *)mem)[size / 4] = 0xdeadbeef;
293
294         pr_debug("  unflattening %lx...\n", mem);
295
296         /* Second pass, do actual unflattening */
297         start = ((unsigned long)initial_boot_params) +
298                 initial_boot_params->off_dt_struct;
299         unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
300         if (*((u32 *)start) != OF_DT_END)
301                 printk(KERN_WARNING "Weird tag at end of tree: %08x\n",
302                         *((u32 *)start));
303         if (((u32 *)mem)[size / 4] != 0xdeadbeef)
304                 printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
305                         ((u32 *)mem)[size / 4]);
306         *allnextp = NULL;
307
308         /* Get pointer to OF "/chosen" node for use everywhere */
309         of_chosen = of_find_node_by_path("/chosen");
310         if (of_chosen == NULL)
311                 of_chosen = of_find_node_by_path("/chosen@0");
312
313         pr_debug(" <- unflatten_device_tree()\n");
314 }
315
316 #define early_init_dt_scan_drconf_memory(node) 0
317
318 static int __init early_init_dt_scan_cpus(unsigned long node,
319                                           const char *uname, int depth,
320                                           void *data)
321 {
322         static int logical_cpuid;
323         char *type = of_get_flat_dt_prop(node, "device_type", NULL);
324         const u32 *intserv;
325         int i, nthreads;
326         int found = 0;
327
328         /* We are scanning "cpu" nodes only */
329         if (type == NULL || strcmp(type, "cpu") != 0)
330                 return 0;
331
332         /* Get physical cpuid */
333         intserv = of_get_flat_dt_prop(node, "reg", NULL);
334         nthreads = 1;
335
336         /*
337          * Now see if any of these threads match our boot cpu.
338          * NOTE: This must match the parsing done in smp_setup_cpu_maps.
339          */
340         for (i = 0; i < nthreads; i++) {
341                 /*
342                  * version 2 of the kexec param format adds the phys cpuid of
343                  * booted proc.
344                  */
345                 if (initial_boot_params && initial_boot_params->version >= 2) {
346                         if (intserv[i] ==
347                                         initial_boot_params->boot_cpuid_phys) {
348                                 found = 1;
349                                 break;
350                         }
351                 } else {
352                         /*
353                          * Check if it's the boot-cpu, set it's hw index now,
354                          * unfortunately this format did not support booting
355                          * off secondary threads.
356                          */
357                         if (of_get_flat_dt_prop(node,
358                                         "linux,boot-cpu", NULL) != NULL) {
359                                 found = 1;
360                                 break;
361                         }
362                 }
363
364 #ifdef CONFIG_SMP
365                 /* logical cpu id is always 0 on UP kernels */
366                 logical_cpuid++;
367 #endif
368         }
369
370         if (found) {
371                 pr_debug("boot cpu: logical %d physical %d\n", logical_cpuid,
372                         intserv[i]);
373                 boot_cpuid = logical_cpuid;
374         }
375
376         return 0;
377 }
378
379 #ifdef CONFIG_BLK_DEV_INITRD
380 static void __init early_init_dt_check_for_initrd(unsigned long node)
381 {
382         unsigned long l;
383         u32 *prop;
384
385         pr_debug("Looking for initrd properties... ");
386
387         prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
388         if (prop) {
389                 initrd_start = (unsigned long)
390                                         __va((u32)of_read_ulong(prop, l/4));
391
392                 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
393                 if (prop) {
394                         initrd_end = (unsigned long)
395                                         __va((u32)of_read_ulong(prop, 1/4));
396                         initrd_below_start_ok = 1;
397                 } else {
398                         initrd_start = 0;
399                 }
400         }
401
402         pr_debug("initrd_start=0x%lx  initrd_end=0x%lx\n",
403                                         initrd_start, initrd_end);
404 }
405 #else
406 static inline void early_init_dt_check_for_initrd(unsigned long node)
407 {
408 }
409 #endif /* CONFIG_BLK_DEV_INITRD */
410
411 static int __init early_init_dt_scan_chosen(unsigned long node,
412                                 const char *uname, int depth, void *data)
413 {
414         unsigned long l;
415         char *p;
416
417         pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
418
419         if (depth != 1 ||
420                 (strcmp(uname, "chosen") != 0 &&
421                                 strcmp(uname, "chosen@0") != 0))
422                 return 0;
423
424 #ifdef CONFIG_KEXEC
425         lprop = (u64 *)of_get_flat_dt_prop(node,
426                                 "linux,crashkernel-base", NULL);
427         if (lprop)
428                 crashk_res.start = *lprop;
429
430         lprop = (u64 *)of_get_flat_dt_prop(node,
431                                 "linux,crashkernel-size", NULL);
432         if (lprop)
433                 crashk_res.end = crashk_res.start + *lprop - 1;
434 #endif
435
436         early_init_dt_check_for_initrd(node);
437
438         /* Retreive command line */
439         p = of_get_flat_dt_prop(node, "bootargs", &l);
440         if (p != NULL && l > 0)
441                 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
442
443 #ifdef CONFIG_CMDLINE
444 #ifndef CONFIG_CMDLINE_FORCE
445         if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
446 #endif
447                 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
448 #endif /* CONFIG_CMDLINE */
449
450         pr_debug("Command line is: %s\n", cmd_line);
451
452         /* break now */
453         return 1;
454 }
455
456 static int __init early_init_dt_scan_root(unsigned long node,
457                                 const char *uname, int depth, void *data)
458 {
459         u32 *prop;
460
461         if (depth != 0)
462                 return 0;
463
464         prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
465         dt_root_size_cells = (prop == NULL) ? 1 : *prop;
466         pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
467
468         prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
469         dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
470         pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
471
472         /* break now */
473         return 1;
474 }
475
476 static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
477 {
478         cell_t *p = *cellp;
479
480         *cellp = p + s;
481         return of_read_number(p, s);
482 }
483
484 static int __init early_init_dt_scan_memory(unsigned long node,
485                                 const char *uname, int depth, void *data)
486 {
487         char *type = of_get_flat_dt_prop(node, "device_type", NULL);
488         cell_t *reg, *endp;
489         unsigned long l;
490
491         /* Look for the ibm,dynamic-reconfiguration-memory node */
492 /*      if (depth == 1 &&
493                 strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
494                 return early_init_dt_scan_drconf_memory(node);
495 */
496         /* We are scanning "memory" nodes only */
497         if (type == NULL) {
498                 /*
499                  * The longtrail doesn't have a device_type on the
500                  * /memory node, so look for the node called /memory@0.
501                  */
502                 if (depth != 1 || strcmp(uname, "memory@0") != 0)
503                         return 0;
504         } else if (strcmp(type, "memory") != 0)
505                 return 0;
506
507         reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
508         if (reg == NULL)
509                 reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
510         if (reg == NULL)
511                 return 0;
512
513         endp = reg + (l / sizeof(cell_t));
514
515         pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
516                 uname, l, reg[0], reg[1], reg[2], reg[3]);
517
518         while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
519                 u64 base, size;
520
521                 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
522                 size = dt_mem_next_cell(dt_root_size_cells, &reg);
523
524                 if (size == 0)
525                         continue;
526                 pr_debug(" - %llx ,  %llx\n", (unsigned long long)base,
527                         (unsigned long long)size);
528
529                 lmb_add(base, size);
530         }
531         return 0;
532 }
533
534 #ifdef CONFIG_PHYP_DUMP
535 /**
536  * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
537  *
538  * Function to find the largest size we need to reserve
539  * during early boot process.
540  *
541  * It either looks for boot param and returns that OR
542  * returns larger of 256 or 5% rounded down to multiples of 256MB.
543  *
544  */
545 static inline unsigned long phyp_dump_calculate_reserve_size(void)
546 {
547         unsigned long tmp;
548
549         if (phyp_dump_info->reserve_bootvar)
550                 return phyp_dump_info->reserve_bootvar;
551
552         /* divide by 20 to get 5% of value */
553         tmp = lmb_end_of_DRAM();
554         do_div(tmp, 20);
555
556         /* round it down in multiples of 256 */
557         tmp = tmp & ~0x0FFFFFFFUL;
558
559         return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
560 }
561
562 /**
563  * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
564  *
565  * This routine may reserve memory regions in the kernel only
566  * if the system is supported and a dump was taken in last
567  * boot instance or if the hardware is supported and the
568  * scratch area needs to be setup. In other instances it returns
569  * without reserving anything. The memory in case of dump being
570  * active is freed when the dump is collected (by userland tools).
571  */
572 static void __init phyp_dump_reserve_mem(void)
573 {
574         unsigned long base, size;
575         unsigned long variable_reserve_size;
576
577         if (!phyp_dump_info->phyp_dump_configured) {
578                 printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
579                 return;
580         }
581
582         if (!phyp_dump_info->phyp_dump_at_boot) {
583                 printk(KERN_INFO "Phyp-dump disabled at boot time\n");
584                 return;
585         }
586
587         variable_reserve_size = phyp_dump_calculate_reserve_size();
588
589         if (phyp_dump_info->phyp_dump_is_active) {
590                 /* Reserve *everything* above RMR.Area freed by userland tools*/
591                 base = variable_reserve_size;
592                 size = lmb_end_of_DRAM() - base;
593
594                 /* XXX crashed_ram_end is wrong, since it may be beyond
595                  * the memory_limit, it will need to be adjusted. */
596                 lmb_reserve(base, size);
597
598                 phyp_dump_info->init_reserve_start = base;
599                 phyp_dump_info->init_reserve_size = size;
600         } else {
601                 size = phyp_dump_info->cpu_state_size +
602                         phyp_dump_info->hpte_region_size +
603                         variable_reserve_size;
604                 base = lmb_end_of_DRAM() - size;
605                 lmb_reserve(base, size);
606                 phyp_dump_info->init_reserve_start = base;
607                 phyp_dump_info->init_reserve_size = size;
608         }
609 }
610 #else
611 static inline void __init phyp_dump_reserve_mem(void) {}
612 #endif /* CONFIG_PHYP_DUMP  && CONFIG_PPC_RTAS */
613
614 #ifdef CONFIG_EARLY_PRINTK
615 /* MS this is Microblaze specifig function */
616 static int __init early_init_dt_scan_serial(unsigned long node,
617                                 const char *uname, int depth, void *data)
618 {
619         unsigned long l;
620         char *p;
621         int *addr;
622
623         pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
624
625 /* find all serial nodes */
626         if (strncmp(uname, "serial", 6) != 0)
627                 return 0;
628
629         early_init_dt_check_for_initrd(node);
630
631 /* find compatible node with uartlite */
632         p = of_get_flat_dt_prop(node, "compatible", &l);
633         if ((strncmp(p, "xlnx,xps-uartlite", 17) != 0) &&
634                         (strncmp(p, "xlnx,opb-uartlite", 17) != 0))
635                 return 0;
636
637         addr = of_get_flat_dt_prop(node, "reg", &l);
638         return *addr; /* return address */
639 }
640
641 /* this function is looking for early uartlite console - Microblaze specific */
642 int __init early_uartlite_console(void)
643 {
644         return of_scan_flat_dt(early_init_dt_scan_serial, NULL);
645 }
646 #endif
647
648 void __init early_init_devtree(void *params)
649 {
650         pr_debug(" -> early_init_devtree(%p)\n", params);
651
652         /* Setup flat device-tree pointer */
653         initial_boot_params = params;
654
655 #ifdef CONFIG_PHYP_DUMP
656         /* scan tree to see if dump occured during last boot */
657         of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
658 #endif
659
660         /* Retrieve various informations from the /chosen node of the
661          * device-tree, including the platform type, initrd location and
662          * size, TCE reserve, and more ...
663          */
664         of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
665
666         /* Scan memory nodes and rebuild LMBs */
667         lmb_init();
668         of_scan_flat_dt(early_init_dt_scan_root, NULL);
669         of_scan_flat_dt(early_init_dt_scan_memory, NULL);
670
671         /* Save command line for /proc/cmdline and then parse parameters */
672         strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
673         parse_early_param();
674
675         lmb_analyze();
676
677         pr_debug("Phys. mem: %lx\n", (unsigned long) lmb_phys_mem_size());
678
679         pr_debug("Scanning CPUs ...\n");
680
681         /* Retreive CPU related informations from the flat tree
682          * (altivec support, boot CPU ID, ...)
683          */
684         of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
685
686         pr_debug(" <- early_init_devtree()\n");
687 }
688
689 /**
690  * Indicates whether the root node has a given value in its
691  * compatible property.
692  */
693 int machine_is_compatible(const char *compat)
694 {
695         struct device_node *root;
696         int rc = 0;
697
698         root = of_find_node_by_path("/");
699         if (root) {
700                 rc = of_device_is_compatible(root, compat);
701                 of_node_put(root);
702         }
703         return rc;
704 }
705 EXPORT_SYMBOL(machine_is_compatible);
706
707 /*******
708  *
709  * New implementation of the OF "find" APIs, return a refcounted
710  * object, call of_node_put() when done.  The device tree and list
711  * are protected by a rw_lock.
712  *
713  * Note that property management will need some locking as well,
714  * this isn't dealt with yet.
715  *
716  *******/
717
718 /**
719  *      of_find_node_by_phandle - Find a node given a phandle
720  *      @handle:        phandle of the node to find
721  *
722  *      Returns a node pointer with refcount incremented, use
723  *      of_node_put() on it when done.
724  */
725 struct device_node *of_find_node_by_phandle(phandle handle)
726 {
727         struct device_node *np;
728
729         read_lock(&devtree_lock);
730         for (np = allnodes; np != NULL; np = np->allnext)
731                 if (np->linux_phandle == handle)
732                         break;
733         of_node_get(np);
734         read_unlock(&devtree_lock);
735         return np;
736 }
737 EXPORT_SYMBOL(of_find_node_by_phandle);
738
739 /**
740  *      of_node_get - Increment refcount of a node
741  *      @node:  Node to inc refcount, NULL is supported to
742  *              simplify writing of callers
743  *
744  *      Returns node.
745  */
746 struct device_node *of_node_get(struct device_node *node)
747 {
748         if (node)
749                 kref_get(&node->kref);
750         return node;
751 }
752 EXPORT_SYMBOL(of_node_get);
753
754 static inline struct device_node *kref_to_device_node(struct kref *kref)
755 {
756         return container_of(kref, struct device_node, kref);
757 }
758
759 /**
760  *      of_node_release - release a dynamically allocated node
761  *      @kref:  kref element of the node to be released
762  *
763  *      In of_node_put() this function is passed to kref_put()
764  *      as the destructor.
765  */
766 static void of_node_release(struct kref *kref)
767 {
768         struct device_node *node = kref_to_device_node(kref);
769         struct property *prop = node->properties;
770
771         /* We should never be releasing nodes that haven't been detached. */
772         if (!of_node_check_flag(node, OF_DETACHED)) {
773                 printk(KERN_INFO "WARNING: Bad of_node_put() on %s\n",
774                         node->full_name);
775                 dump_stack();
776                 kref_init(&node->kref);
777                 return;
778         }
779
780         if (!of_node_check_flag(node, OF_DYNAMIC))
781                 return;
782
783         while (prop) {
784                 struct property *next = prop->next;
785                 kfree(prop->name);
786                 kfree(prop->value);
787                 kfree(prop);
788                 prop = next;
789
790                 if (!prop) {
791                         prop = node->deadprops;
792                         node->deadprops = NULL;
793                 }
794         }
795         kfree(node->full_name);
796         kfree(node->data);
797         kfree(node);
798 }
799
800 /**
801  *      of_node_put - Decrement refcount of a node
802  *      @node:  Node to dec refcount, NULL is supported to
803  *              simplify writing of callers
804  *
805  */
806 void of_node_put(struct device_node *node)
807 {
808         if (node)
809                 kref_put(&node->kref, of_node_release);
810 }
811 EXPORT_SYMBOL(of_node_put);
812
813 /*
814  * Plug a device node into the tree and global list.
815  */
816 void of_attach_node(struct device_node *np)
817 {
818         unsigned long flags;
819
820         write_lock_irqsave(&devtree_lock, flags);
821         np->sibling = np->parent->child;
822         np->allnext = allnodes;
823         np->parent->child = np;
824         allnodes = np;
825         write_unlock_irqrestore(&devtree_lock, flags);
826 }
827
828 /*
829  * "Unplug" a node from the device tree.  The caller must hold
830  * a reference to the node.  The memory associated with the node
831  * is not freed until its refcount goes to zero.
832  */
833 void of_detach_node(struct device_node *np)
834 {
835         struct device_node *parent;
836         unsigned long flags;
837
838         write_lock_irqsave(&devtree_lock, flags);
839
840         parent = np->parent;
841         if (!parent)
842                 goto out_unlock;
843
844         if (allnodes == np)
845                 allnodes = np->allnext;
846         else {
847                 struct device_node *prev;
848                 for (prev = allnodes;
849                      prev->allnext != np;
850                      prev = prev->allnext)
851                         ;
852                 prev->allnext = np->allnext;
853         }
854
855         if (parent->child == np)
856                 parent->child = np->sibling;
857         else {
858                 struct device_node *prevsib;
859                 for (prevsib = np->parent->child;
860                      prevsib->sibling != np;
861                      prevsib = prevsib->sibling)
862                         ;
863                 prevsib->sibling = np->sibling;
864         }
865
866         of_node_set_flag(np, OF_DETACHED);
867
868 out_unlock:
869         write_unlock_irqrestore(&devtree_lock, flags);
870 }
871
872 /*
873  * Add a property to a node
874  */
875 int prom_add_property(struct device_node *np, struct property *prop)
876 {
877         struct property **next;
878         unsigned long flags;
879
880         prop->next = NULL;
881         write_lock_irqsave(&devtree_lock, flags);
882         next = &np->properties;
883         while (*next) {
884                 if (strcmp(prop->name, (*next)->name) == 0) {
885                         /* duplicate ! don't insert it */
886                         write_unlock_irqrestore(&devtree_lock, flags);
887                         return -1;
888                 }
889                 next = &(*next)->next;
890         }
891         *next = prop;
892         write_unlock_irqrestore(&devtree_lock, flags);
893
894 #ifdef CONFIG_PROC_DEVICETREE
895         /* try to add to proc as well if it was initialized */
896         if (np->pde)
897                 proc_device_tree_add_prop(np->pde, prop);
898 #endif /* CONFIG_PROC_DEVICETREE */
899
900         return 0;
901 }
902
903 /*
904  * Remove a property from a node.  Note that we don't actually
905  * remove it, since we have given out who-knows-how-many pointers
906  * to the data using get-property.  Instead we just move the property
907  * to the "dead properties" list, so it won't be found any more.
908  */
909 int prom_remove_property(struct device_node *np, struct property *prop)
910 {
911         struct property **next;
912         unsigned long flags;
913         int found = 0;
914
915         write_lock_irqsave(&devtree_lock, flags);
916         next = &np->properties;
917         while (*next) {
918                 if (*next == prop) {
919                         /* found the node */
920                         *next = prop->next;
921                         prop->next = np->deadprops;
922                         np->deadprops = prop;
923                         found = 1;
924                         break;
925                 }
926                 next = &(*next)->next;
927         }
928         write_unlock_irqrestore(&devtree_lock, flags);
929
930         if (!found)
931                 return -ENODEV;
932
933 #ifdef CONFIG_PROC_DEVICETREE
934         /* try to remove the proc node as well */
935         if (np->pde)
936                 proc_device_tree_remove_prop(np->pde, prop);
937 #endif /* CONFIG_PROC_DEVICETREE */
938
939         return 0;
940 }
941
942 /*
943  * Update a property in a node.  Note that we don't actually
944  * remove it, since we have given out who-knows-how-many pointers
945  * to the data using get-property.  Instead we just move the property
946  * to the "dead properties" list, and add the new property to the
947  * property list
948  */
949 int prom_update_property(struct device_node *np,
950                          struct property *newprop,
951                          struct property *oldprop)
952 {
953         struct property **next;
954         unsigned long flags;
955         int found = 0;
956
957         write_lock_irqsave(&devtree_lock, flags);
958         next = &np->properties;
959         while (*next) {
960                 if (*next == oldprop) {
961                         /* found the node */
962                         newprop->next = oldprop->next;
963                         *next = newprop;
964                         oldprop->next = np->deadprops;
965                         np->deadprops = oldprop;
966                         found = 1;
967                         break;
968                 }
969                 next = &(*next)->next;
970         }
971         write_unlock_irqrestore(&devtree_lock, flags);
972
973         if (!found)
974                 return -ENODEV;
975
976 #ifdef CONFIG_PROC_DEVICETREE
977         /* try to add to proc as well if it was initialized */
978         if (np->pde)
979                 proc_device_tree_update_prop(np->pde, newprop, oldprop);
980 #endif /* CONFIG_PROC_DEVICETREE */
981
982         return 0;
983 }
984
985 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
986 static struct debugfs_blob_wrapper flat_dt_blob;
987
988 static int __init export_flat_device_tree(void)
989 {
990         struct dentry *d;
991
992         flat_dt_blob.data = initial_boot_params;
993         flat_dt_blob.size = initial_boot_params->totalsize;
994
995         d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
996                                 of_debugfs_root, &flat_dt_blob);
997         if (!d)
998                 return 1;
999
1000         return 0;
1001 }
1002 device_initcall(export_flat_device_tree);
1003 #endif