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