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