of/flattree: merge find_flat_dt_string and initial_boot_params
[safe/jmp/linux-2.6] / arch / powerpc / 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 #undef DEBUG
17
18 #include <stdarg.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/init.h>
22 #include <linux/threads.h>
23 #include <linux/spinlock.h>
24 #include <linux/types.h>
25 #include <linux/pci.h>
26 #include <linux/stringify.h>
27 #include <linux/delay.h>
28 #include <linux/initrd.h>
29 #include <linux/bitops.h>
30 #include <linux/module.h>
31 #include <linux/kexec.h>
32 #include <linux/debugfs.h>
33 #include <linux/irq.h>
34 #include <linux/lmb.h>
35
36 #include <asm/prom.h>
37 #include <asm/rtas.h>
38 #include <asm/page.h>
39 #include <asm/processor.h>
40 #include <asm/irq.h>
41 #include <asm/io.h>
42 #include <asm/kdump.h>
43 #include <asm/smp.h>
44 #include <asm/system.h>
45 #include <asm/mmu.h>
46 #include <asm/pgtable.h>
47 #include <asm/pci.h>
48 #include <asm/iommu.h>
49 #include <asm/btext.h>
50 #include <asm/sections.h>
51 #include <asm/machdep.h>
52 #include <asm/pSeries_reconfig.h>
53 #include <asm/pci-bridge.h>
54 #include <asm/phyp_dump.h>
55 #include <asm/kexec.h>
56 #include <mm/mmu_decl.h>
57
58 #ifdef DEBUG
59 #define DBG(fmt...) printk(KERN_ERR fmt)
60 #else
61 #define DBG(fmt...)
62 #endif
63
64
65 static int __initdata dt_root_addr_cells;
66 static int __initdata dt_root_size_cells;
67
68 #ifdef CONFIG_PPC64
69 int __initdata iommu_is_off;
70 int __initdata iommu_force_on;
71 unsigned long tce_alloc_start, tce_alloc_end;
72 #endif
73
74 typedef u32 cell_t;
75
76 extern struct device_node *allnodes;    /* temporary while merging */
77
78 extern rwlock_t devtree_lock;   /* temporary while merging */
79
80 /* export that to outside world */
81 struct device_node *of_chosen;
82
83 /**
84  * This function is used to scan the flattened device-tree, it is
85  * used to extract the memory informations at boot before we can
86  * unflatten the tree
87  */
88 int __init of_scan_flat_dt(int (*it)(unsigned long node,
89                                      const char *uname, int depth,
90                                      void *data),
91                            void *data)
92 {
93         unsigned long p = ((unsigned long)initial_boot_params) +
94                 initial_boot_params->off_dt_struct;
95         int rc = 0;
96         int depth = -1;
97
98         do {
99                 u32 tag = *((u32 *)p);
100                 char *pathp;
101                 
102                 p += 4;
103                 if (tag == OF_DT_END_NODE) {
104                         depth --;
105                         continue;
106                 }
107                 if (tag == OF_DT_NOP)
108                         continue;
109                 if (tag == OF_DT_END)
110                         break;
111                 if (tag == OF_DT_PROP) {
112                         u32 sz = *((u32 *)p);
113                         p += 8;
114                         if (initial_boot_params->version < 0x10)
115                                 p = _ALIGN(p, sz >= 8 ? 8 : 4);
116                         p += sz;
117                         p = _ALIGN(p, 4);
118                         continue;
119                 }
120                 if (tag != OF_DT_BEGIN_NODE) {
121                         printk(KERN_WARNING "Invalid tag %x scanning flattened"
122                                " device tree !\n", tag);
123                         return -EINVAL;
124                 }
125                 depth++;
126                 pathp = (char *)p;
127                 p = _ALIGN(p + strlen(pathp) + 1, 4);
128                 if ((*pathp) == '/') {
129                         char *lp, *np;
130                         for (lp = NULL, np = pathp; *np; np++)
131                                 if ((*np) == '/')
132                                         lp = np+1;
133                         if (lp != NULL)
134                                 pathp = lp;
135                 }
136                 rc = it(p, pathp, depth, data);
137                 if (rc != 0)
138                         break;          
139         } while(1);
140
141         return rc;
142 }
143
144 unsigned long __init of_get_flat_dt_root(void)
145 {
146         unsigned long p = ((unsigned long)initial_boot_params) +
147                 initial_boot_params->off_dt_struct;
148
149         while(*((u32 *)p) == OF_DT_NOP)
150                 p += 4;
151         BUG_ON (*((u32 *)p) != OF_DT_BEGIN_NODE);
152         p += 4;
153         return _ALIGN(p + strlen((char *)p) + 1, 4);
154 }
155
156 /**
157  * This  function can be used within scan_flattened_dt callback to get
158  * access to properties
159  */
160 void* __init of_get_flat_dt_prop(unsigned long node, const char *name,
161                                  unsigned long *size)
162 {
163         unsigned long p = node;
164
165         do {
166                 u32 tag = *((u32 *)p);
167                 u32 sz, noff;
168                 const char *nstr;
169
170                 p += 4;
171                 if (tag == OF_DT_NOP)
172                         continue;
173                 if (tag != OF_DT_PROP)
174                         return NULL;
175
176                 sz = *((u32 *)p);
177                 noff = *((u32 *)(p + 4));
178                 p += 8;
179                 if (initial_boot_params->version < 0x10)
180                         p = _ALIGN(p, sz >= 8 ? 8 : 4);
181
182                 nstr = find_flat_dt_string(noff);
183                 if (nstr == NULL) {
184                         printk(KERN_WARNING "Can't find property index"
185                                " name !\n");
186                         return NULL;
187                 }
188                 if (strcmp(name, nstr) == 0) {
189                         if (size)
190                                 *size = sz;
191                         return (void *)p;
192                 }
193                 p += sz;
194                 p = _ALIGN(p, 4);
195         } while(1);
196 }
197
198 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
199 {
200         const char* cp;
201         unsigned long cplen, l;
202
203         cp = of_get_flat_dt_prop(node, "compatible", &cplen);
204         if (cp == NULL)
205                 return 0;
206         while (cplen > 0) {
207                 if (strncasecmp(cp, compat, strlen(compat)) == 0)
208                         return 1;
209                 l = strlen(cp) + 1;
210                 cp += l;
211                 cplen -= l;
212         }
213
214         return 0;
215 }
216
217 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
218                                        unsigned long align)
219 {
220         void *res;
221
222         *mem = _ALIGN(*mem, align);
223         res = (void *)*mem;
224         *mem += size;
225
226         return res;
227 }
228
229 static unsigned long __init unflatten_dt_node(unsigned long mem,
230                                               unsigned long *p,
231                                               struct device_node *dad,
232                                               struct device_node ***allnextpp,
233                                               unsigned long fpsize)
234 {
235         struct device_node *np;
236         struct property *pp, **prev_pp = NULL;
237         char *pathp;
238         u32 tag;
239         unsigned int l, allocl;
240         int has_name = 0;
241         int new_format = 0;
242
243         tag = *((u32 *)(*p));
244         if (tag != OF_DT_BEGIN_NODE) {
245                 printk("Weird tag at start of node: %x\n", tag);
246                 return mem;
247         }
248         *p += 4;
249         pathp = (char *)*p;
250         l = allocl = strlen(pathp) + 1;
251         *p = _ALIGN(*p + l, 4);
252
253         /* version 0x10 has a more compact unit name here instead of the full
254          * path. we accumulate the full path size using "fpsize", we'll rebuild
255          * it later. We detect this because the first character of the name is
256          * not '/'.
257          */
258         if ((*pathp) != '/') {
259                 new_format = 1;
260                 if (fpsize == 0) {
261                         /* root node: special case. fpsize accounts for path
262                          * plus terminating zero. root node only has '/', so
263                          * fpsize should be 2, but we want to avoid the first
264                          * level nodes to have two '/' so we use fpsize 1 here
265                          */
266                         fpsize = 1;
267                         allocl = 2;
268                 } else {
269                         /* account for '/' and path size minus terminal 0
270                          * already in 'l'
271                          */
272                         fpsize += l;
273                         allocl = fpsize;
274                 }
275         }
276
277
278         np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
279                                 __alignof__(struct device_node));
280         if (allnextpp) {
281                 memset(np, 0, sizeof(*np));
282                 np->full_name = ((char*)np) + sizeof(struct device_node);
283                 if (new_format) {
284                         char *p = np->full_name;
285                         /* rebuild full path for new format */
286                         if (dad && dad->parent) {
287                                 strcpy(p, dad->full_name);
288 #ifdef DEBUG
289                                 if ((strlen(p) + l + 1) != allocl) {
290                                         DBG("%s: p: %d, l: %d, a: %d\n",
291                                             pathp, (int)strlen(p), l, allocl);
292                                 }
293 #endif
294                                 p += strlen(p);
295                         }
296                         *(p++) = '/';
297                         memcpy(p, pathp, l);
298                 } else
299                         memcpy(np->full_name, pathp, l);
300                 prev_pp = &np->properties;
301                 **allnextpp = np;
302                 *allnextpp = &np->allnext;
303                 if (dad != NULL) {
304                         np->parent = dad;
305                         /* we temporarily use the next field as `last_child'*/
306                         if (dad->next == 0)
307                                 dad->child = np;
308                         else
309                                 dad->next->sibling = np;
310                         dad->next = np;
311                 }
312                 kref_init(&np->kref);
313         }
314         while(1) {
315                 u32 sz, noff;
316                 char *pname;
317
318                 tag = *((u32 *)(*p));
319                 if (tag == OF_DT_NOP) {
320                         *p += 4;
321                         continue;
322                 }
323                 if (tag != OF_DT_PROP)
324                         break;
325                 *p += 4;
326                 sz = *((u32 *)(*p));
327                 noff = *((u32 *)((*p) + 4));
328                 *p += 8;
329                 if (initial_boot_params->version < 0x10)
330                         *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
331
332                 pname = find_flat_dt_string(noff);
333                 if (pname == NULL) {
334                         printk("Can't find property name in list !\n");
335                         break;
336                 }
337                 if (strcmp(pname, "name") == 0)
338                         has_name = 1;
339                 l = strlen(pname) + 1;
340                 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
341                                         __alignof__(struct property));
342                 if (allnextpp) {
343                         if (strcmp(pname, "linux,phandle") == 0) {
344                                 np->node = *((u32 *)*p);
345                                 if (np->linux_phandle == 0)
346                                         np->linux_phandle = np->node;
347                         }
348                         if (strcmp(pname, "ibm,phandle") == 0)
349                                 np->linux_phandle = *((u32 *)*p);
350                         pp->name = pname;
351                         pp->length = sz;
352                         pp->value = (void *)*p;
353                         *prev_pp = pp;
354                         prev_pp = &pp->next;
355                 }
356                 *p = _ALIGN((*p) + sz, 4);
357         }
358         /* with version 0x10 we may not have the name property, recreate
359          * it here from the unit name if absent
360          */
361         if (!has_name) {
362                 char *p = pathp, *ps = pathp, *pa = NULL;
363                 int sz;
364
365                 while (*p) {
366                         if ((*p) == '@')
367                                 pa = p;
368                         if ((*p) == '/')
369                                 ps = p + 1;
370                         p++;
371                 }
372                 if (pa < ps)
373                         pa = p;
374                 sz = (pa - ps) + 1;
375                 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
376                                         __alignof__(struct property));
377                 if (allnextpp) {
378                         pp->name = "name";
379                         pp->length = sz;
380                         pp->value = pp + 1;
381                         *prev_pp = pp;
382                         prev_pp = &pp->next;
383                         memcpy(pp->value, ps, sz - 1);
384                         ((char *)pp->value)[sz - 1] = 0;
385                         DBG("fixed up name for %s -> %s\n", pathp,
386                                 (char *)pp->value);
387                 }
388         }
389         if (allnextpp) {
390                 *prev_pp = NULL;
391                 np->name = of_get_property(np, "name", NULL);
392                 np->type = of_get_property(np, "device_type", NULL);
393
394                 if (!np->name)
395                         np->name = "<NULL>";
396                 if (!np->type)
397                         np->type = "<NULL>";
398         }
399         while (tag == OF_DT_BEGIN_NODE) {
400                 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
401                 tag = *((u32 *)(*p));
402         }
403         if (tag != OF_DT_END_NODE) {
404                 printk("Weird tag at end of node: %x\n", tag);
405                 return mem;
406         }
407         *p += 4;
408         return mem;
409 }
410
411 static int __init early_parse_mem(char *p)
412 {
413         if (!p)
414                 return 1;
415
416         memory_limit = PAGE_ALIGN(memparse(p, &p));
417         DBG("memory limit = 0x%llx\n", (unsigned long long)memory_limit);
418
419         return 0;
420 }
421 early_param("mem", early_parse_mem);
422
423 /**
424  * move_device_tree - move tree to an unused area, if needed.
425  *
426  * The device tree may be allocated beyond our memory limit, or inside the
427  * crash kernel region for kdump. If so, move it out of the way.
428  */
429 static void __init move_device_tree(void)
430 {
431         unsigned long start, size;
432         void *p;
433
434         DBG("-> move_device_tree\n");
435
436         start = __pa(initial_boot_params);
437         size = initial_boot_params->totalsize;
438
439         if ((memory_limit && (start + size) > memory_limit) ||
440                         overlaps_crashkernel(start, size)) {
441                 p = __va(lmb_alloc_base(size, PAGE_SIZE, lmb.rmo_size));
442                 memcpy(p, initial_boot_params, size);
443                 initial_boot_params = (struct boot_param_header *)p;
444                 DBG("Moved device tree to 0x%p\n", p);
445         }
446
447         DBG("<- move_device_tree\n");
448 }
449
450 /**
451  * unflattens the device-tree passed by the firmware, creating the
452  * tree of struct device_node. It also fills the "name" and "type"
453  * pointers of the nodes so the normal device-tree walking functions
454  * can be used (this used to be done by finish_device_tree)
455  */
456 void __init unflatten_device_tree(void)
457 {
458         unsigned long start, mem, size;
459         struct device_node **allnextp = &allnodes;
460
461         DBG(" -> unflatten_device_tree()\n");
462
463         /* First pass, scan for size */
464         start = ((unsigned long)initial_boot_params) +
465                 initial_boot_params->off_dt_struct;
466         size = unflatten_dt_node(0, &start, NULL, NULL, 0);
467         size = (size | 3) + 1;
468
469         DBG("  size is %lx, allocating...\n", size);
470
471         /* Allocate memory for the expanded device tree */
472         mem = lmb_alloc(size + 4, __alignof__(struct device_node));
473         mem = (unsigned long) __va(mem);
474
475         ((u32 *)mem)[size / 4] = 0xdeadbeef;
476
477         DBG("  unflattening %lx...\n", mem);
478
479         /* Second pass, do actual unflattening */
480         start = ((unsigned long)initial_boot_params) +
481                 initial_boot_params->off_dt_struct;
482         unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
483         if (*((u32 *)start) != OF_DT_END)
484                 printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
485         if (((u32 *)mem)[size / 4] != 0xdeadbeef)
486                 printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
487                        ((u32 *)mem)[size / 4] );
488         *allnextp = NULL;
489
490         /* Get pointer to OF "/chosen" node for use everywhere */
491         of_chosen = of_find_node_by_path("/chosen");
492         if (of_chosen == NULL)
493                 of_chosen = of_find_node_by_path("/chosen@0");
494
495         DBG(" <- unflatten_device_tree()\n");
496 }
497
498 /*
499  * ibm,pa-features is a per-cpu property that contains a string of
500  * attribute descriptors, each of which has a 2 byte header plus up
501  * to 254 bytes worth of processor attribute bits.  First header
502  * byte specifies the number of bytes following the header.
503  * Second header byte is an "attribute-specifier" type, of which
504  * zero is the only currently-defined value.
505  * Implementation:  Pass in the byte and bit offset for the feature
506  * that we are interested in.  The function will return -1 if the
507  * pa-features property is missing, or a 1/0 to indicate if the feature
508  * is supported/not supported.  Note that the bit numbers are
509  * big-endian to match the definition in PAPR.
510  */
511 static struct ibm_pa_feature {
512         unsigned long   cpu_features;   /* CPU_FTR_xxx bit */
513         unsigned int    cpu_user_ftrs;  /* PPC_FEATURE_xxx bit */
514         unsigned char   pabyte;         /* byte number in ibm,pa-features */
515         unsigned char   pabit;          /* bit number (big-endian) */
516         unsigned char   invert;         /* if 1, pa bit set => clear feature */
517 } ibm_pa_features[] __initdata = {
518         {0, PPC_FEATURE_HAS_MMU,        0, 0, 0},
519         {0, PPC_FEATURE_HAS_FPU,        0, 1, 0},
520         {CPU_FTR_SLB, 0,                0, 2, 0},
521         {CPU_FTR_CTRL, 0,               0, 3, 0},
522         {CPU_FTR_NOEXECUTE, 0,          0, 6, 0},
523         {CPU_FTR_NODSISRALIGN, 0,       1, 1, 1},
524         {CPU_FTR_CI_LARGE_PAGE, 0,      1, 2, 0},
525         {CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0},
526 };
527
528 static void __init scan_features(unsigned long node, unsigned char *ftrs,
529                                  unsigned long tablelen,
530                                  struct ibm_pa_feature *fp,
531                                  unsigned long ft_size)
532 {
533         unsigned long i, len, bit;
534
535         /* find descriptor with type == 0 */
536         for (;;) {
537                 if (tablelen < 3)
538                         return;
539                 len = 2 + ftrs[0];
540                 if (tablelen < len)
541                         return;         /* descriptor 0 not found */
542                 if (ftrs[1] == 0)
543                         break;
544                 tablelen -= len;
545                 ftrs += len;
546         }
547
548         /* loop over bits we know about */
549         for (i = 0; i < ft_size; ++i, ++fp) {
550                 if (fp->pabyte >= ftrs[0])
551                         continue;
552                 bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
553                 if (bit ^ fp->invert) {
554                         cur_cpu_spec->cpu_features |= fp->cpu_features;
555                         cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
556                 } else {
557                         cur_cpu_spec->cpu_features &= ~fp->cpu_features;
558                         cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
559                 }
560         }
561 }
562
563 static void __init check_cpu_pa_features(unsigned long node)
564 {
565         unsigned char *pa_ftrs;
566         unsigned long tablelen;
567
568         pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
569         if (pa_ftrs == NULL)
570                 return;
571
572         scan_features(node, pa_ftrs, tablelen,
573                       ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
574 }
575
576 #ifdef CONFIG_PPC_STD_MMU_64
577 static void __init check_cpu_slb_size(unsigned long node)
578 {
579         u32 *slb_size_ptr;
580
581         slb_size_ptr = of_get_flat_dt_prop(node, "slb-size", NULL);
582         if (slb_size_ptr != NULL) {
583                 mmu_slb_size = *slb_size_ptr;
584                 return;
585         }
586         slb_size_ptr = of_get_flat_dt_prop(node, "ibm,slb-size", NULL);
587         if (slb_size_ptr != NULL) {
588                 mmu_slb_size = *slb_size_ptr;
589         }
590 }
591 #else
592 #define check_cpu_slb_size(node) do { } while(0)
593 #endif
594
595 static struct feature_property {
596         const char *name;
597         u32 min_value;
598         unsigned long cpu_feature;
599         unsigned long cpu_user_ftr;
600 } feature_properties[] __initdata = {
601 #ifdef CONFIG_ALTIVEC
602         {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
603         {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
604 #endif /* CONFIG_ALTIVEC */
605 #ifdef CONFIG_VSX
606         /* Yes, this _really_ is ibm,vmx == 2 to enable VSX */
607         {"ibm,vmx", 2, CPU_FTR_VSX, PPC_FEATURE_HAS_VSX},
608 #endif /* CONFIG_VSX */
609 #ifdef CONFIG_PPC64
610         {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
611         {"ibm,purr", 1, CPU_FTR_PURR, 0},
612         {"ibm,spurr", 1, CPU_FTR_SPURR, 0},
613 #endif /* CONFIG_PPC64 */
614 };
615
616 #if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
617 static inline void identical_pvr_fixup(unsigned long node)
618 {
619         unsigned int pvr;
620         char *model = of_get_flat_dt_prop(node, "model", NULL);
621
622         /*
623          * Since 440GR(x)/440EP(x) processors have the same pvr,
624          * we check the node path and set bit 28 in the cur_cpu_spec
625          * pvr for EP(x) processor version. This bit is always 0 in
626          * the "real" pvr. Then we call identify_cpu again with
627          * the new logical pvr to enable FPU support.
628          */
629         if (model && strstr(model, "440EP")) {
630                 pvr = cur_cpu_spec->pvr_value | 0x8;
631                 identify_cpu(0, pvr);
632                 DBG("Using logical pvr %x for %s\n", pvr, model);
633         }
634 }
635 #else
636 #define identical_pvr_fixup(node) do { } while(0)
637 #endif
638
639 static void __init check_cpu_feature_properties(unsigned long node)
640 {
641         unsigned long i;
642         struct feature_property *fp = feature_properties;
643         const u32 *prop;
644
645         for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) {
646                 prop = of_get_flat_dt_prop(node, fp->name, NULL);
647                 if (prop && *prop >= fp->min_value) {
648                         cur_cpu_spec->cpu_features |= fp->cpu_feature;
649                         cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
650                 }
651         }
652 }
653
654 static int __init early_init_dt_scan_cpus(unsigned long node,
655                                           const char *uname, int depth,
656                                           void *data)
657 {
658         static int logical_cpuid = 0;
659         char *type = of_get_flat_dt_prop(node, "device_type", NULL);
660         const u32 *prop;
661         const u32 *intserv;
662         int i, nthreads;
663         unsigned long len;
664         int found = 0;
665
666         /* We are scanning "cpu" nodes only */
667         if (type == NULL || strcmp(type, "cpu") != 0)
668                 return 0;
669
670         /* Get physical cpuid */
671         intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
672         if (intserv) {
673                 nthreads = len / sizeof(int);
674         } else {
675                 intserv = of_get_flat_dt_prop(node, "reg", NULL);
676                 nthreads = 1;
677         }
678
679         /*
680          * Now see if any of these threads match our boot cpu.
681          * NOTE: This must match the parsing done in smp_setup_cpu_maps.
682          */
683         for (i = 0; i < nthreads; i++) {
684                 /*
685                  * version 2 of the kexec param format adds the phys cpuid of
686                  * booted proc.
687                  */
688                 if (initial_boot_params && initial_boot_params->version >= 2) {
689                         if (intserv[i] ==
690                                         initial_boot_params->boot_cpuid_phys) {
691                                 found = 1;
692                                 break;
693                         }
694                 } else {
695                         /*
696                          * Check if it's the boot-cpu, set it's hw index now,
697                          * unfortunately this format did not support booting
698                          * off secondary threads.
699                          */
700                         if (of_get_flat_dt_prop(node,
701                                         "linux,boot-cpu", NULL) != NULL) {
702                                 found = 1;
703                                 break;
704                         }
705                 }
706
707 #ifdef CONFIG_SMP
708                 /* logical cpu id is always 0 on UP kernels */
709                 logical_cpuid++;
710 #endif
711         }
712
713         if (found) {
714                 DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
715                         intserv[i]);
716                 boot_cpuid = logical_cpuid;
717                 set_hard_smp_processor_id(boot_cpuid, intserv[i]);
718
719                 /*
720                  * PAPR defines "logical" PVR values for cpus that
721                  * meet various levels of the architecture:
722                  * 0x0f000001   Architecture version 2.04
723                  * 0x0f000002   Architecture version 2.05
724                  * If the cpu-version property in the cpu node contains
725                  * such a value, we call identify_cpu again with the
726                  * logical PVR value in order to use the cpu feature
727                  * bits appropriate for the architecture level.
728                  *
729                  * A POWER6 partition in "POWER6 architected" mode
730                  * uses the 0x0f000002 PVR value; in POWER5+ mode
731                  * it uses 0x0f000001.
732                  */
733                 prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
734                 if (prop && (*prop & 0xff000000) == 0x0f000000)
735                         identify_cpu(0, *prop);
736
737                 identical_pvr_fixup(node);
738         }
739
740         check_cpu_feature_properties(node);
741         check_cpu_pa_features(node);
742         check_cpu_slb_size(node);
743
744 #ifdef CONFIG_PPC_PSERIES
745         if (nthreads > 1)
746                 cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
747         else
748                 cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
749 #endif
750
751         return 0;
752 }
753
754 #ifdef CONFIG_BLK_DEV_INITRD
755 static void __init early_init_dt_check_for_initrd(unsigned long node)
756 {
757         unsigned long l;
758         u32 *prop;
759
760         DBG("Looking for initrd properties... ");
761
762         prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
763         if (prop) {
764                 initrd_start = (unsigned long)__va(of_read_ulong(prop, l/4));
765
766                 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
767                 if (prop) {
768                         initrd_end = (unsigned long)
769                                         __va(of_read_ulong(prop, l/4));
770                         initrd_below_start_ok = 1;
771                 } else {
772                         initrd_start = 0;
773                 }
774         }
775
776         DBG("initrd_start=0x%lx  initrd_end=0x%lx\n", initrd_start, initrd_end);
777 }
778 #else
779 static inline void early_init_dt_check_for_initrd(unsigned long node)
780 {
781 }
782 #endif /* CONFIG_BLK_DEV_INITRD */
783
784 static int __init early_init_dt_scan_chosen(unsigned long node,
785                                             const char *uname, int depth, void *data)
786 {
787         unsigned long *lprop;
788         unsigned long l;
789         char *p;
790
791         DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
792
793         if (depth != 1 ||
794             (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
795                 return 0;
796
797 #ifdef CONFIG_PPC64
798         /* check if iommu is forced on or off */
799         if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
800                 iommu_is_off = 1;
801         if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
802                 iommu_force_on = 1;
803 #endif
804
805         /* mem=x on the command line is the preferred mechanism */
806         lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
807         if (lprop)
808                 memory_limit = *lprop;
809
810 #ifdef CONFIG_PPC64
811         lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
812         if (lprop)
813                 tce_alloc_start = *lprop;
814         lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
815         if (lprop)
816                 tce_alloc_end = *lprop;
817 #endif
818
819 #ifdef CONFIG_KEXEC
820         lprop = of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
821         if (lprop)
822                 crashk_res.start = *lprop;
823
824         lprop = of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
825         if (lprop)
826                 crashk_res.end = crashk_res.start + *lprop - 1;
827 #endif
828
829         early_init_dt_check_for_initrd(node);
830
831         /* Retreive command line */
832         p = of_get_flat_dt_prop(node, "bootargs", &l);
833         if (p != NULL && l > 0)
834                 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
835
836 #ifdef CONFIG_CMDLINE
837         if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
838                 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
839 #endif /* CONFIG_CMDLINE */
840
841         DBG("Command line is: %s\n", cmd_line);
842
843         /* break now */
844         return 1;
845 }
846
847 static int __init early_init_dt_scan_root(unsigned long node,
848                                           const char *uname, int depth, void *data)
849 {
850         u32 *prop;
851
852         if (depth != 0)
853                 return 0;
854
855         prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
856         dt_root_size_cells = (prop == NULL) ? 1 : *prop;
857         DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
858
859         prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
860         dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
861         DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
862         
863         /* break now */
864         return 1;
865 }
866
867 static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
868 {
869         cell_t *p = *cellp;
870
871         *cellp = p + s;
872         return of_read_number(p, s);
873 }
874
875 #ifdef CONFIG_PPC_PSERIES
876 /*
877  * Interpret the ibm,dynamic-memory property in the
878  * /ibm,dynamic-reconfiguration-memory node.
879  * This contains a list of memory blocks along with NUMA affinity
880  * information.
881  */
882 static int __init early_init_dt_scan_drconf_memory(unsigned long node)
883 {
884         cell_t *dm, *ls, *usm;
885         unsigned long l, n, flags;
886         u64 base, size, lmb_size;
887         unsigned int is_kexec_kdump = 0, rngs;
888
889         ls = of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
890         if (ls == NULL || l < dt_root_size_cells * sizeof(cell_t))
891                 return 0;
892         lmb_size = dt_mem_next_cell(dt_root_size_cells, &ls);
893
894         dm = of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
895         if (dm == NULL || l < sizeof(cell_t))
896                 return 0;
897
898         n = *dm++;      /* number of entries */
899         if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(cell_t))
900                 return 0;
901
902         /* check if this is a kexec/kdump kernel. */
903         usm = of_get_flat_dt_prop(node, "linux,drconf-usable-memory",
904                                                  &l);
905         if (usm != NULL)
906                 is_kexec_kdump = 1;
907
908         for (; n != 0; --n) {
909                 base = dt_mem_next_cell(dt_root_addr_cells, &dm);
910                 flags = dm[3];
911                 /* skip DRC index, pad, assoc. list index, flags */
912                 dm += 4;
913                 /* skip this block if the reserved bit is set in flags (0x80)
914                    or if the block is not assigned to this partition (0x8) */
915                 if ((flags & 0x80) || !(flags & 0x8))
916                         continue;
917                 size = lmb_size;
918                 rngs = 1;
919                 if (is_kexec_kdump) {
920                         /*
921                          * For each lmb in ibm,dynamic-memory, a corresponding
922                          * entry in linux,drconf-usable-memory property contains
923                          * a counter 'p' followed by 'p' (base, size) duple.
924                          * Now read the counter from
925                          * linux,drconf-usable-memory property
926                          */
927                         rngs = dt_mem_next_cell(dt_root_size_cells, &usm);
928                         if (!rngs) /* there are no (base, size) duple */
929                                 continue;
930                 }
931                 do {
932                         if (is_kexec_kdump) {
933                                 base = dt_mem_next_cell(dt_root_addr_cells,
934                                                          &usm);
935                                 size = dt_mem_next_cell(dt_root_size_cells,
936                                                          &usm);
937                         }
938                         if (iommu_is_off) {
939                                 if (base >= 0x80000000ul)
940                                         continue;
941                                 if ((base + size) > 0x80000000ul)
942                                         size = 0x80000000ul - base;
943                         }
944                         lmb_add(base, size);
945                 } while (--rngs);
946         }
947         lmb_dump_all();
948         return 0;
949 }
950 #else
951 #define early_init_dt_scan_drconf_memory(node)  0
952 #endif /* CONFIG_PPC_PSERIES */
953
954 static int __init early_init_dt_scan_memory(unsigned long node,
955                                             const char *uname, int depth, void *data)
956 {
957         char *type = of_get_flat_dt_prop(node, "device_type", NULL);
958         cell_t *reg, *endp;
959         unsigned long l;
960
961         /* Look for the ibm,dynamic-reconfiguration-memory node */
962         if (depth == 1 &&
963             strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
964                 return early_init_dt_scan_drconf_memory(node);
965
966         /* We are scanning "memory" nodes only */
967         if (type == NULL) {
968                 /*
969                  * The longtrail doesn't have a device_type on the
970                  * /memory node, so look for the node called /memory@0.
971                  */
972                 if (depth != 1 || strcmp(uname, "memory@0") != 0)
973                         return 0;
974         } else if (strcmp(type, "memory") != 0)
975                 return 0;
976
977         reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
978         if (reg == NULL)
979                 reg = of_get_flat_dt_prop(node, "reg", &l);
980         if (reg == NULL)
981                 return 0;
982
983         endp = reg + (l / sizeof(cell_t));
984
985         DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
986             uname, l, reg[0], reg[1], reg[2], reg[3]);
987
988         while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
989                 u64 base, size;
990
991                 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
992                 size = dt_mem_next_cell(dt_root_size_cells, &reg);
993
994                 if (size == 0)
995                         continue;
996                 DBG(" - %llx ,  %llx\n", (unsigned long long)base,
997                     (unsigned long long)size);
998 #ifdef CONFIG_PPC64
999                 if (iommu_is_off) {
1000                         if (base >= 0x80000000ul)
1001                                 continue;
1002                         if ((base + size) > 0x80000000ul)
1003                                 size = 0x80000000ul - base;
1004                 }
1005 #endif
1006                 lmb_add(base, size);
1007
1008                 memstart_addr = min((u64)memstart_addr, base);
1009         }
1010
1011         return 0;
1012 }
1013
1014 static void __init early_reserve_mem(void)
1015 {
1016         u64 base, size;
1017         u64 *reserve_map;
1018         unsigned long self_base;
1019         unsigned long self_size;
1020
1021         reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
1022                                         initial_boot_params->off_mem_rsvmap);
1023
1024         /* before we do anything, lets reserve the dt blob */
1025         self_base = __pa((unsigned long)initial_boot_params);
1026         self_size = initial_boot_params->totalsize;
1027         lmb_reserve(self_base, self_size);
1028
1029 #ifdef CONFIG_BLK_DEV_INITRD
1030         /* then reserve the initrd, if any */
1031         if (initrd_start && (initrd_end > initrd_start))
1032                 lmb_reserve(__pa(initrd_start), initrd_end - initrd_start);
1033 #endif /* CONFIG_BLK_DEV_INITRD */
1034
1035 #ifdef CONFIG_PPC32
1036         /* 
1037          * Handle the case where we might be booting from an old kexec
1038          * image that setup the mem_rsvmap as pairs of 32-bit values
1039          */
1040         if (*reserve_map > 0xffffffffull) {
1041                 u32 base_32, size_32;
1042                 u32 *reserve_map_32 = (u32 *)reserve_map;
1043
1044                 while (1) {
1045                         base_32 = *(reserve_map_32++);
1046                         size_32 = *(reserve_map_32++);
1047                         if (size_32 == 0)
1048                                 break;
1049                         /* skip if the reservation is for the blob */
1050                         if (base_32 == self_base && size_32 == self_size)
1051                                 continue;
1052                         DBG("reserving: %x -> %x\n", base_32, size_32);
1053                         lmb_reserve(base_32, size_32);
1054                 }
1055                 return;
1056         }
1057 #endif
1058         while (1) {
1059                 base = *(reserve_map++);
1060                 size = *(reserve_map++);
1061                 if (size == 0)
1062                         break;
1063                 DBG("reserving: %llx -> %llx\n", base, size);
1064                 lmb_reserve(base, size);
1065         }
1066 }
1067
1068 #ifdef CONFIG_PHYP_DUMP
1069 /**
1070  * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
1071  *
1072  * Function to find the largest size we need to reserve
1073  * during early boot process.
1074  *
1075  * It either looks for boot param and returns that OR
1076  * returns larger of 256 or 5% rounded down to multiples of 256MB.
1077  *
1078  */
1079 static inline unsigned long phyp_dump_calculate_reserve_size(void)
1080 {
1081         unsigned long tmp;
1082
1083         if (phyp_dump_info->reserve_bootvar)
1084                 return phyp_dump_info->reserve_bootvar;
1085
1086         /* divide by 20 to get 5% of value */
1087         tmp = lmb_end_of_DRAM();
1088         do_div(tmp, 20);
1089
1090         /* round it down in multiples of 256 */
1091         tmp = tmp & ~0x0FFFFFFFUL;
1092
1093         return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
1094 }
1095
1096 /**
1097  * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
1098  *
1099  * This routine may reserve memory regions in the kernel only
1100  * if the system is supported and a dump was taken in last
1101  * boot instance or if the hardware is supported and the
1102  * scratch area needs to be setup. In other instances it returns
1103  * without reserving anything. The memory in case of dump being
1104  * active is freed when the dump is collected (by userland tools).
1105  */
1106 static void __init phyp_dump_reserve_mem(void)
1107 {
1108         unsigned long base, size;
1109         unsigned long variable_reserve_size;
1110
1111         if (!phyp_dump_info->phyp_dump_configured) {
1112                 printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
1113                 return;
1114         }
1115
1116         if (!phyp_dump_info->phyp_dump_at_boot) {
1117                 printk(KERN_INFO "Phyp-dump disabled at boot time\n");
1118                 return;
1119         }
1120
1121         variable_reserve_size = phyp_dump_calculate_reserve_size();
1122
1123         if (phyp_dump_info->phyp_dump_is_active) {
1124                 /* Reserve *everything* above RMR.Area freed by userland tools*/
1125                 base = variable_reserve_size;
1126                 size = lmb_end_of_DRAM() - base;
1127
1128                 /* XXX crashed_ram_end is wrong, since it may be beyond
1129                  * the memory_limit, it will need to be adjusted. */
1130                 lmb_reserve(base, size);
1131
1132                 phyp_dump_info->init_reserve_start = base;
1133                 phyp_dump_info->init_reserve_size = size;
1134         } else {
1135                 size = phyp_dump_info->cpu_state_size +
1136                         phyp_dump_info->hpte_region_size +
1137                         variable_reserve_size;
1138                 base = lmb_end_of_DRAM() - size;
1139                 lmb_reserve(base, size);
1140                 phyp_dump_info->init_reserve_start = base;
1141                 phyp_dump_info->init_reserve_size = size;
1142         }
1143 }
1144 #else
1145 static inline void __init phyp_dump_reserve_mem(void) {}
1146 #endif /* CONFIG_PHYP_DUMP  && CONFIG_PPC_RTAS */
1147
1148
1149 void __init early_init_devtree(void *params)
1150 {
1151         phys_addr_t limit;
1152
1153         DBG(" -> early_init_devtree(%p)\n", params);
1154
1155         /* Setup flat device-tree pointer */
1156         initial_boot_params = params;
1157
1158 #ifdef CONFIG_PPC_RTAS
1159         /* Some machines might need RTAS info for debugging, grab it now. */
1160         of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
1161 #endif
1162
1163 #ifdef CONFIG_PHYP_DUMP
1164         /* scan tree to see if dump occured during last boot */
1165         of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
1166 #endif
1167
1168         /* Retrieve various informations from the /chosen node of the
1169          * device-tree, including the platform type, initrd location and
1170          * size, TCE reserve, and more ...
1171          */
1172         of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
1173
1174         /* Scan memory nodes and rebuild LMBs */
1175         lmb_init();
1176         of_scan_flat_dt(early_init_dt_scan_root, NULL);
1177         of_scan_flat_dt(early_init_dt_scan_memory, NULL);
1178
1179         /* Save command line for /proc/cmdline and then parse parameters */
1180         strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
1181         parse_early_param();
1182
1183         /* Reserve LMB regions used by kernel, initrd, dt, etc... */
1184         lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
1185         /* If relocatable, reserve first 32k for interrupt vectors etc. */
1186         if (PHYSICAL_START > MEMORY_START)
1187                 lmb_reserve(MEMORY_START, 0x8000);
1188         reserve_kdump_trampoline();
1189         reserve_crashkernel();
1190         early_reserve_mem();
1191         phyp_dump_reserve_mem();
1192
1193         limit = memory_limit;
1194         if (! limit) {
1195                 phys_addr_t memsize;
1196
1197                 /* Ensure that total memory size is page-aligned, because
1198                  * otherwise mark_bootmem() gets upset. */
1199                 lmb_analyze();
1200                 memsize = lmb_phys_mem_size();
1201                 if ((memsize & PAGE_MASK) != memsize)
1202                         limit = memsize & PAGE_MASK;
1203         }
1204         lmb_enforce_memory_limit(limit);
1205
1206         lmb_analyze();
1207         lmb_dump_all();
1208
1209         DBG("Phys. mem: %llx\n", lmb_phys_mem_size());
1210
1211         /* We may need to relocate the flat tree, do it now.
1212          * FIXME .. and the initrd too? */
1213         move_device_tree();
1214
1215         DBG("Scanning CPUs ...\n");
1216
1217         /* Retreive CPU related informations from the flat tree
1218          * (altivec support, boot CPU ID, ...)
1219          */
1220         of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
1221
1222         DBG(" <- early_init_devtree()\n");
1223 }
1224
1225
1226 /**
1227  * Indicates whether the root node has a given value in its
1228  * compatible property.
1229  */
1230 int machine_is_compatible(const char *compat)
1231 {
1232         struct device_node *root;
1233         int rc = 0;
1234
1235         root = of_find_node_by_path("/");
1236         if (root) {
1237                 rc = of_device_is_compatible(root, compat);
1238                 of_node_put(root);
1239         }
1240         return rc;
1241 }
1242 EXPORT_SYMBOL(machine_is_compatible);
1243
1244 /*******
1245  *
1246  * New implementation of the OF "find" APIs, return a refcounted
1247  * object, call of_node_put() when done.  The device tree and list
1248  * are protected by a rw_lock.
1249  *
1250  * Note that property management will need some locking as well,
1251  * this isn't dealt with yet.
1252  *
1253  *******/
1254
1255 /**
1256  *      of_find_node_by_phandle - Find a node given a phandle
1257  *      @handle:        phandle of the node to find
1258  *
1259  *      Returns a node pointer with refcount incremented, use
1260  *      of_node_put() on it when done.
1261  */
1262 struct device_node *of_find_node_by_phandle(phandle handle)
1263 {
1264         struct device_node *np;
1265
1266         read_lock(&devtree_lock);
1267         for (np = allnodes; np != 0; np = np->allnext)
1268                 if (np->linux_phandle == handle)
1269                         break;
1270         of_node_get(np);
1271         read_unlock(&devtree_lock);
1272         return np;
1273 }
1274 EXPORT_SYMBOL(of_find_node_by_phandle);
1275
1276 /**
1277  *      of_find_next_cache_node - Find a node's subsidiary cache
1278  *      @np:    node of type "cpu" or "cache"
1279  *
1280  *      Returns a node pointer with refcount incremented, use
1281  *      of_node_put() on it when done.  Caller should hold a reference
1282  *      to np.
1283  */
1284 struct device_node *of_find_next_cache_node(struct device_node *np)
1285 {
1286         struct device_node *child;
1287         const phandle *handle;
1288
1289         handle = of_get_property(np, "l2-cache", NULL);
1290         if (!handle)
1291                 handle = of_get_property(np, "next-level-cache", NULL);
1292
1293         if (handle)
1294                 return of_find_node_by_phandle(*handle);
1295
1296         /* OF on pmac has nodes instead of properties named "l2-cache"
1297          * beneath CPU nodes.
1298          */
1299         if (!strcmp(np->type, "cpu"))
1300                 for_each_child_of_node(np, child)
1301                         if (!strcmp(child->type, "cache"))
1302                                 return child;
1303
1304         return NULL;
1305 }
1306
1307 /**
1308  *      of_node_get - Increment refcount of a node
1309  *      @node:  Node to inc refcount, NULL is supported to
1310  *              simplify writing of callers
1311  *
1312  *      Returns node.
1313  */
1314 struct device_node *of_node_get(struct device_node *node)
1315 {
1316         if (node)
1317                 kref_get(&node->kref);
1318         return node;
1319 }
1320 EXPORT_SYMBOL(of_node_get);
1321
1322 static inline struct device_node * kref_to_device_node(struct kref *kref)
1323 {
1324         return container_of(kref, struct device_node, kref);
1325 }
1326
1327 /**
1328  *      of_node_release - release a dynamically allocated node
1329  *      @kref:  kref element of the node to be released
1330  *
1331  *      In of_node_put() this function is passed to kref_put()
1332  *      as the destructor.
1333  */
1334 static void of_node_release(struct kref *kref)
1335 {
1336         struct device_node *node = kref_to_device_node(kref);
1337         struct property *prop = node->properties;
1338
1339         /* We should never be releasing nodes that haven't been detached. */
1340         if (!of_node_check_flag(node, OF_DETACHED)) {
1341                 printk("WARNING: Bad of_node_put() on %s\n", node->full_name);
1342                 dump_stack();
1343                 kref_init(&node->kref);
1344                 return;
1345         }
1346
1347         if (!of_node_check_flag(node, OF_DYNAMIC))
1348                 return;
1349
1350         while (prop) {
1351                 struct property *next = prop->next;
1352                 kfree(prop->name);
1353                 kfree(prop->value);
1354                 kfree(prop);
1355                 prop = next;
1356
1357                 if (!prop) {
1358                         prop = node->deadprops;
1359                         node->deadprops = NULL;
1360                 }
1361         }
1362         kfree(node->full_name);
1363         kfree(node->data);
1364         kfree(node);
1365 }
1366
1367 /**
1368  *      of_node_put - Decrement refcount of a node
1369  *      @node:  Node to dec refcount, NULL is supported to
1370  *              simplify writing of callers
1371  *
1372  */
1373 void of_node_put(struct device_node *node)
1374 {
1375         if (node)
1376                 kref_put(&node->kref, of_node_release);
1377 }
1378 EXPORT_SYMBOL(of_node_put);
1379
1380 /*
1381  * Plug a device node into the tree and global list.
1382  */
1383 void of_attach_node(struct device_node *np)
1384 {
1385         unsigned long flags;
1386
1387         write_lock_irqsave(&devtree_lock, flags);
1388         np->sibling = np->parent->child;
1389         np->allnext = allnodes;
1390         np->parent->child = np;
1391         allnodes = np;
1392         write_unlock_irqrestore(&devtree_lock, flags);
1393 }
1394
1395 /*
1396  * "Unplug" a node from the device tree.  The caller must hold
1397  * a reference to the node.  The memory associated with the node
1398  * is not freed until its refcount goes to zero.
1399  */
1400 void of_detach_node(struct device_node *np)
1401 {
1402         struct device_node *parent;
1403         unsigned long flags;
1404
1405         write_lock_irqsave(&devtree_lock, flags);
1406
1407         parent = np->parent;
1408         if (!parent)
1409                 goto out_unlock;
1410
1411         if (allnodes == np)
1412                 allnodes = np->allnext;
1413         else {
1414                 struct device_node *prev;
1415                 for (prev = allnodes;
1416                      prev->allnext != np;
1417                      prev = prev->allnext)
1418                         ;
1419                 prev->allnext = np->allnext;
1420         }
1421
1422         if (parent->child == np)
1423                 parent->child = np->sibling;
1424         else {
1425                 struct device_node *prevsib;
1426                 for (prevsib = np->parent->child;
1427                      prevsib->sibling != np;
1428                      prevsib = prevsib->sibling)
1429                         ;
1430                 prevsib->sibling = np->sibling;
1431         }
1432
1433         of_node_set_flag(np, OF_DETACHED);
1434
1435 out_unlock:
1436         write_unlock_irqrestore(&devtree_lock, flags);
1437 }
1438
1439 #ifdef CONFIG_PPC_PSERIES
1440 /*
1441  * Fix up the uninitialized fields in a new device node:
1442  * name, type and pci-specific fields
1443  */
1444
1445 static int of_finish_dynamic_node(struct device_node *node)
1446 {
1447         struct device_node *parent = of_get_parent(node);
1448         int err = 0;
1449         const phandle *ibm_phandle;
1450
1451         node->name = of_get_property(node, "name", NULL);
1452         node->type = of_get_property(node, "device_type", NULL);
1453
1454         if (!node->name)
1455                 node->name = "<NULL>";
1456         if (!node->type)
1457                 node->type = "<NULL>";
1458
1459         if (!parent) {
1460                 err = -ENODEV;
1461                 goto out;
1462         }
1463
1464         /* We don't support that function on PowerMac, at least
1465          * not yet
1466          */
1467         if (machine_is(powermac))
1468                 return -ENODEV;
1469
1470         /* fix up new node's linux_phandle field */
1471         if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
1472                 node->linux_phandle = *ibm_phandle;
1473
1474 out:
1475         of_node_put(parent);
1476         return err;
1477 }
1478
1479 static int prom_reconfig_notifier(struct notifier_block *nb,
1480                                   unsigned long action, void *node)
1481 {
1482         int err;
1483
1484         switch (action) {
1485         case PSERIES_RECONFIG_ADD:
1486                 err = of_finish_dynamic_node(node);
1487                 if (err < 0) {
1488                         printk(KERN_ERR "finish_node returned %d\n", err);
1489                         err = NOTIFY_BAD;
1490                 }
1491                 break;
1492         default:
1493                 err = NOTIFY_DONE;
1494                 break;
1495         }
1496         return err;
1497 }
1498
1499 static struct notifier_block prom_reconfig_nb = {
1500         .notifier_call = prom_reconfig_notifier,
1501         .priority = 10, /* This one needs to run first */
1502 };
1503
1504 static int __init prom_reconfig_setup(void)
1505 {
1506         return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1507 }
1508 __initcall(prom_reconfig_setup);
1509 #endif
1510
1511 /*
1512  * Add a property to a node
1513  */
1514 int prom_add_property(struct device_node* np, struct property* prop)
1515 {
1516         struct property **next;
1517         unsigned long flags;
1518
1519         prop->next = NULL;      
1520         write_lock_irqsave(&devtree_lock, flags);
1521         next = &np->properties;
1522         while (*next) {
1523                 if (strcmp(prop->name, (*next)->name) == 0) {
1524                         /* duplicate ! don't insert it */
1525                         write_unlock_irqrestore(&devtree_lock, flags);
1526                         return -1;
1527                 }
1528                 next = &(*next)->next;
1529         }
1530         *next = prop;
1531         write_unlock_irqrestore(&devtree_lock, flags);
1532
1533 #ifdef CONFIG_PROC_DEVICETREE
1534         /* try to add to proc as well if it was initialized */
1535         if (np->pde)
1536                 proc_device_tree_add_prop(np->pde, prop);
1537 #endif /* CONFIG_PROC_DEVICETREE */
1538
1539         return 0;
1540 }
1541
1542 /*
1543  * Remove a property from a node.  Note that we don't actually
1544  * remove it, since we have given out who-knows-how-many pointers
1545  * to the data using get-property.  Instead we just move the property
1546  * to the "dead properties" list, so it won't be found any more.
1547  */
1548 int prom_remove_property(struct device_node *np, struct property *prop)
1549 {
1550         struct property **next;
1551         unsigned long flags;
1552         int found = 0;
1553
1554         write_lock_irqsave(&devtree_lock, flags);
1555         next = &np->properties;
1556         while (*next) {
1557                 if (*next == prop) {
1558                         /* found the node */
1559                         *next = prop->next;
1560                         prop->next = np->deadprops;
1561                         np->deadprops = prop;
1562                         found = 1;
1563                         break;
1564                 }
1565                 next = &(*next)->next;
1566         }
1567         write_unlock_irqrestore(&devtree_lock, flags);
1568
1569         if (!found)
1570                 return -ENODEV;
1571
1572 #ifdef CONFIG_PROC_DEVICETREE
1573         /* try to remove the proc node as well */
1574         if (np->pde)
1575                 proc_device_tree_remove_prop(np->pde, prop);
1576 #endif /* CONFIG_PROC_DEVICETREE */
1577
1578         return 0;
1579 }
1580
1581 /*
1582  * Update a property in a node.  Note that we don't actually
1583  * remove it, since we have given out who-knows-how-many pointers
1584  * to the data using get-property.  Instead we just move the property
1585  * to the "dead properties" list, and add the new property to the
1586  * property list
1587  */
1588 int prom_update_property(struct device_node *np,
1589                          struct property *newprop,
1590                          struct property *oldprop)
1591 {
1592         struct property **next;
1593         unsigned long flags;
1594         int found = 0;
1595
1596         write_lock_irqsave(&devtree_lock, flags);
1597         next = &np->properties;
1598         while (*next) {
1599                 if (*next == oldprop) {
1600                         /* found the node */
1601                         newprop->next = oldprop->next;
1602                         *next = newprop;
1603                         oldprop->next = np->deadprops;
1604                         np->deadprops = oldprop;
1605                         found = 1;
1606                         break;
1607                 }
1608                 next = &(*next)->next;
1609         }
1610         write_unlock_irqrestore(&devtree_lock, flags);
1611
1612         if (!found)
1613                 return -ENODEV;
1614
1615 #ifdef CONFIG_PROC_DEVICETREE
1616         /* try to add to proc as well if it was initialized */
1617         if (np->pde)
1618                 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1619 #endif /* CONFIG_PROC_DEVICETREE */
1620
1621         return 0;
1622 }
1623
1624
1625 /* Find the device node for a given logical cpu number, also returns the cpu
1626  * local thread number (index in ibm,interrupt-server#s) if relevant and
1627  * asked for (non NULL)
1628  */
1629 struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
1630 {
1631         int hardid;
1632         struct device_node *np;
1633
1634         hardid = get_hard_smp_processor_id(cpu);
1635
1636         for_each_node_by_type(np, "cpu") {
1637                 const u32 *intserv;
1638                 unsigned int plen, t;
1639
1640                 /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
1641                  * fallback to "reg" property and assume no threads
1642                  */
1643                 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
1644                                 &plen);
1645                 if (intserv == NULL) {
1646                         const u32 *reg = of_get_property(np, "reg", NULL);
1647                         if (reg == NULL)
1648                                 continue;
1649                         if (*reg == hardid) {
1650                                 if (thread)
1651                                         *thread = 0;
1652                                 return np;
1653                         }
1654                 } else {
1655                         plen /= sizeof(u32);
1656                         for (t = 0; t < plen; t++) {
1657                                 if (hardid == intserv[t]) {
1658                                         if (thread)
1659                                                 *thread = t;
1660                                         return np;
1661                                 }
1662                         }
1663                 }
1664         }
1665         return NULL;
1666 }
1667 EXPORT_SYMBOL(of_get_cpu_node);
1668
1669 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
1670 static struct debugfs_blob_wrapper flat_dt_blob;
1671
1672 static int __init export_flat_device_tree(void)
1673 {
1674         struct dentry *d;
1675
1676         flat_dt_blob.data = initial_boot_params;
1677         flat_dt_blob.size = initial_boot_params->totalsize;
1678
1679         d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
1680                                 powerpc_debugfs_root, &flat_dt_blob);
1681         if (!d)
1682                 return 1;
1683
1684         return 0;
1685 }
1686 __initcall(export_flat_device_tree);
1687 #endif