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