[PATCH] powerpc: Add/remove/update properties in firmware device tree
[deliverable/linux.git] / arch / powerpc / kernel / prom.c
CommitLineData
9b6b563c
PM
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/config.h>
20#include <linux/kernel.h>
21#include <linux/string.h>
22#include <linux/init.h>
23#include <linux/threads.h>
24#include <linux/spinlock.h>
25#include <linux/types.h>
26#include <linux/pci.h>
27#include <linux/stringify.h>
28#include <linux/delay.h>
29#include <linux/initrd.h>
30#include <linux/bitops.h>
31#include <linux/module.h>
dcee3036 32#include <linux/kexec.h>
9b6b563c
PM
33
34#include <asm/prom.h>
35#include <asm/rtas.h>
36#include <asm/lmb.h>
37#include <asm/page.h>
38#include <asm/processor.h>
39#include <asm/irq.h>
40#include <asm/io.h>
0cc4746c 41#include <asm/kdump.h>
9b6b563c
PM
42#include <asm/smp.h>
43#include <asm/system.h>
44#include <asm/mmu.h>
45#include <asm/pgtable.h>
46#include <asm/pci.h>
47#include <asm/iommu.h>
48#include <asm/btext.h>
49#include <asm/sections.h>
50#include <asm/machdep.h>
51#include <asm/pSeries_reconfig.h>
40ef8cbc 52#include <asm/pci-bridge.h>
9b6b563c
PM
53
54#ifdef DEBUG
55#define DBG(fmt...) printk(KERN_ERR fmt)
56#else
57#define DBG(fmt...)
58#endif
59
9b6b563c 60
9b6b563c
PM
61static int __initdata dt_root_addr_cells;
62static int __initdata dt_root_size_cells;
63
64#ifdef CONFIG_PPC64
65static int __initdata iommu_is_off;
66int __initdata iommu_force_on;
cf00a8d1 67unsigned long tce_alloc_start, tce_alloc_end;
9b6b563c
PM
68#endif
69
70typedef u32 cell_t;
71
72#if 0
73static struct boot_param_header *initial_boot_params __initdata;
74#else
75struct boot_param_header *initial_boot_params;
76#endif
77
78static struct device_node *allnodes = NULL;
79
80/* use when traversing tree through the allnext, child, sibling,
81 * or parent members of struct device_node.
82 */
83static DEFINE_RWLOCK(devtree_lock);
84
85/* export that to outside world */
86struct device_node *of_chosen;
87
88struct device_node *dflt_interrupt_controller;
89int num_interrupt_controllers;
90
9b6b563c
PM
91/*
92 * Wrapper for allocating memory for various data that needs to be
93 * attached to device nodes as they are processed at boot or when
94 * added to the device tree later (e.g. DLPAR). At boot there is
95 * already a region reserved so we just increment *mem_start by size;
96 * otherwise we call kmalloc.
97 */
98static void * prom_alloc(unsigned long size, unsigned long *mem_start)
99{
100 unsigned long tmp;
101
102 if (!mem_start)
103 return kmalloc(size, GFP_KERNEL);
104
105 tmp = *mem_start;
106 *mem_start += size;
107 return (void *)tmp;
108}
109
110/*
111 * Find the device_node with a given phandle.
112 */
113static struct device_node * find_phandle(phandle ph)
114{
115 struct device_node *np;
116
117 for (np = allnodes; np != 0; np = np->allnext)
118 if (np->linux_phandle == ph)
119 return np;
120 return NULL;
121}
122
123/*
124 * Find the interrupt parent of a node.
125 */
126static struct device_node * __devinit intr_parent(struct device_node *p)
127{
128 phandle *parp;
129
130 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
131 if (parp == NULL)
132 return p->parent;
133 p = find_phandle(*parp);
134 if (p != NULL)
135 return p;
136 /*
137 * On a powermac booted with BootX, we don't get to know the
138 * phandles for any nodes, so find_phandle will return NULL.
139 * Fortunately these machines only have one interrupt controller
140 * so there isn't in fact any ambiguity. -- paulus
141 */
142 if (num_interrupt_controllers == 1)
143 p = dflt_interrupt_controller;
144 return p;
145}
146
147/*
148 * Find out the size of each entry of the interrupts property
149 * for a node.
150 */
151int __devinit prom_n_intr_cells(struct device_node *np)
152{
153 struct device_node *p;
154 unsigned int *icp;
155
156 for (p = np; (p = intr_parent(p)) != NULL; ) {
157 icp = (unsigned int *)
158 get_property(p, "#interrupt-cells", NULL);
159 if (icp != NULL)
160 return *icp;
161 if (get_property(p, "interrupt-controller", NULL) != NULL
162 || get_property(p, "interrupt-map", NULL) != NULL) {
163 printk("oops, node %s doesn't have #interrupt-cells\n",
164 p->full_name);
165 return 1;
166 }
167 }
168#ifdef DEBUG_IRQ
169 printk("prom_n_intr_cells failed for %s\n", np->full_name);
170#endif
171 return 1;
172}
173
174/*
175 * Map an interrupt from a device up to the platform interrupt
176 * descriptor.
177 */
178static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
179 struct device_node *np, unsigned int *ints,
180 int nintrc)
181{
182 struct device_node *p, *ipar;
183 unsigned int *imap, *imask, *ip;
184 int i, imaplen, match;
185 int newintrc = 0, newaddrc = 0;
186 unsigned int *reg;
187 int naddrc;
188
189 reg = (unsigned int *) get_property(np, "reg", NULL);
190 naddrc = prom_n_addr_cells(np);
191 p = intr_parent(np);
192 while (p != NULL) {
193 if (get_property(p, "interrupt-controller", NULL) != NULL)
194 /* this node is an interrupt controller, stop here */
195 break;
196 imap = (unsigned int *)
197 get_property(p, "interrupt-map", &imaplen);
198 if (imap == NULL) {
199 p = intr_parent(p);
200 continue;
201 }
202 imask = (unsigned int *)
203 get_property(p, "interrupt-map-mask", NULL);
204 if (imask == NULL) {
205 printk("oops, %s has interrupt-map but no mask\n",
206 p->full_name);
207 return 0;
208 }
209 imaplen /= sizeof(unsigned int);
210 match = 0;
211 ipar = NULL;
212 while (imaplen > 0 && !match) {
213 /* check the child-interrupt field */
214 match = 1;
215 for (i = 0; i < naddrc && match; ++i)
216 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
217 for (; i < naddrc + nintrc && match; ++i)
218 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
219 imap += naddrc + nintrc;
220 imaplen -= naddrc + nintrc;
221 /* grab the interrupt parent */
222 ipar = find_phandle((phandle) *imap++);
223 --imaplen;
224 if (ipar == NULL && num_interrupt_controllers == 1)
225 /* cope with BootX not giving us phandles */
226 ipar = dflt_interrupt_controller;
227 if (ipar == NULL) {
228 printk("oops, no int parent %x in map of %s\n",
229 imap[-1], p->full_name);
230 return 0;
231 }
232 /* find the parent's # addr and intr cells */
233 ip = (unsigned int *)
234 get_property(ipar, "#interrupt-cells", NULL);
235 if (ip == NULL) {
236 printk("oops, no #interrupt-cells on %s\n",
237 ipar->full_name);
238 return 0;
239 }
240 newintrc = *ip;
241 ip = (unsigned int *)
242 get_property(ipar, "#address-cells", NULL);
243 newaddrc = (ip == NULL)? 0: *ip;
244 imap += newaddrc + newintrc;
245 imaplen -= newaddrc + newintrc;
246 }
247 if (imaplen < 0) {
248 printk("oops, error decoding int-map on %s, len=%d\n",
249 p->full_name, imaplen);
250 return 0;
251 }
252 if (!match) {
253#ifdef DEBUG_IRQ
254 printk("oops, no match in %s int-map for %s\n",
255 p->full_name, np->full_name);
256#endif
257 return 0;
258 }
259 p = ipar;
260 naddrc = newaddrc;
261 nintrc = newintrc;
262 ints = imap - nintrc;
263 reg = ints - naddrc;
264 }
265 if (p == NULL) {
266#ifdef DEBUG_IRQ
267 printk("hmmm, int tree for %s doesn't have ctrler\n",
268 np->full_name);
269#endif
270 return 0;
271 }
272 *irq = ints;
273 *ictrler = p;
274 return nintrc;
275}
276
6d0124fc
PM
277static unsigned char map_isa_senses[4] = {
278 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
279 IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
280 IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE,
281 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE
282};
283
284static unsigned char map_mpic_senses[4] = {
285 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE,
286 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
287 /* 2 seems to be used for the 8259 cascade... */
288 IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
289 IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE,
290};
291
9b6b563c
PM
292static int __devinit finish_node_interrupts(struct device_node *np,
293 unsigned long *mem_start,
294 int measure_only)
295{
296 unsigned int *ints;
297 int intlen, intrcells, intrcount;
6d0124fc 298 int i, j, n, sense;
9b6b563c
PM
299 unsigned int *irq, virq;
300 struct device_node *ic;
1beb6a7d
BH
301 int trace = 0;
302
303 //#define TRACE(fmt...) do { if (trace) { printk(fmt); mdelay(1000); } } while(0)
304#define TRACE(fmt...)
305
306 if (!strcmp(np->name, "smu-doorbell"))
307 trace = 1;
308
309 TRACE("Finishing SMU doorbell ! num_interrupt_controllers = %d\n",
310 num_interrupt_controllers);
9b6b563c 311
a575b807
PM
312 if (num_interrupt_controllers == 0) {
313 /*
314 * Old machines just have a list of interrupt numbers
315 * and no interrupt-controller nodes.
316 */
317 ints = (unsigned int *) get_property(np, "AAPL,interrupts",
318 &intlen);
319 /* XXX old interpret_pci_props looked in parent too */
320 /* XXX old interpret_macio_props looked for interrupts
321 before AAPL,interrupts */
322 if (ints == NULL)
323 ints = (unsigned int *) get_property(np, "interrupts",
324 &intlen);
325 if (ints == NULL)
326 return 0;
327
328 np->n_intrs = intlen / sizeof(unsigned int);
329 np->intrs = prom_alloc(np->n_intrs * sizeof(np->intrs[0]),
330 mem_start);
331 if (!np->intrs)
332 return -ENOMEM;
333 if (measure_only)
334 return 0;
335
336 for (i = 0; i < np->n_intrs; ++i) {
337 np->intrs[i].line = *ints++;
6d0124fc
PM
338 np->intrs[i].sense = IRQ_SENSE_LEVEL
339 | IRQ_POLARITY_NEGATIVE;
a575b807
PM
340 }
341 return 0;
342 }
343
9b6b563c 344 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
1beb6a7d 345 TRACE("ints=%p, intlen=%d\n", ints, intlen);
9b6b563c
PM
346 if (ints == NULL)
347 return 0;
348 intrcells = prom_n_intr_cells(np);
349 intlen /= intrcells * sizeof(unsigned int);
1beb6a7d 350 TRACE("intrcells=%d, new intlen=%d\n", intrcells, intlen);
9b6b563c
PM
351 np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start);
352 if (!np->intrs)
353 return -ENOMEM;
354
355 if (measure_only)
356 return 0;
357
358 intrcount = 0;
359 for (i = 0; i < intlen; ++i, ints += intrcells) {
360 n = map_interrupt(&irq, &ic, np, ints, intrcells);
1beb6a7d 361 TRACE("map, irq=%d, ic=%p, n=%d\n", irq, ic, n);
9b6b563c
PM
362 if (n <= 0)
363 continue;
364
365 /* don't map IRQ numbers under a cascaded 8259 controller */
366 if (ic && device_is_compatible(ic, "chrp,iic")) {
367 np->intrs[intrcount].line = irq[0];
6d0124fc
PM
368 sense = (n > 1)? (irq[1] & 3): 3;
369 np->intrs[intrcount].sense = map_isa_senses[sense];
9b6b563c 370 } else {
9b6b563c 371 virq = virt_irq_create_mapping(irq[0]);
1beb6a7d 372 TRACE("virq=%d\n", virq);
6d0124fc 373#ifdef CONFIG_PPC64
9b6b563c
PM
374 if (virq == NO_IRQ) {
375 printk(KERN_CRIT "Could not allocate interrupt"
376 " number for %s\n", np->full_name);
377 continue;
378 }
9b6b563c 379#endif
6d0124fc
PM
380 np->intrs[intrcount].line = irq_offset_up(virq);
381 sense = (n > 1)? (irq[1] & 3): 1;
1beb6a7d
BH
382
383 /* Apple uses bits in there in a different way, let's
384 * only keep the real sense bit on macs
385 */
386 if (_machine == PLATFORM_POWERMAC)
387 sense &= 0x1;
6d0124fc 388 np->intrs[intrcount].sense = map_mpic_senses[sense];
9b6b563c
PM
389 }
390
391#ifdef CONFIG_PPC64
392 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
799d6046 393 if (_machine == PLATFORM_POWERMAC && ic && ic->parent) {
9b6b563c
PM
394 char *name = get_property(ic->parent, "name", NULL);
395 if (name && !strcmp(name, "u3"))
396 np->intrs[intrcount].line += 128;
1beb6a7d
BH
397 else if (!(name && (!strcmp(name, "mac-io") ||
398 !strcmp(name, "u4"))))
9b6b563c
PM
399 /* ignore other cascaded controllers, such as
400 the k2-sata-root */
401 break;
402 }
1beb6a7d 403#endif /* CONFIG_PPC64 */
9b6b563c
PM
404 if (n > 2) {
405 printk("hmmm, got %d intr cells for %s:", n,
406 np->full_name);
407 for (j = 0; j < n; ++j)
408 printk(" %d", irq[j]);
409 printk("\n");
410 }
411 ++intrcount;
412 }
413 np->n_intrs = intrcount;
414
415 return 0;
416}
417
9b6b563c
PM
418static int __devinit finish_node(struct device_node *np,
419 unsigned long *mem_start,
9b6b563c
PM
420 int measure_only)
421{
422 struct device_node *child;
cc5d0189 423 int rc = 0;
9b6b563c
PM
424
425 rc = finish_node_interrupts(np, mem_start, measure_only);
426 if (rc)
427 goto out;
428
9b6b563c 429 for (child = np->child; child != NULL; child = child->sibling) {
cc5d0189 430 rc = finish_node(child, mem_start, measure_only);
9b6b563c
PM
431 if (rc)
432 goto out;
433 }
434out:
435 return rc;
436}
437
438static void __init scan_interrupt_controllers(void)
439{
440 struct device_node *np;
441 int n = 0;
442 char *name, *ic;
443 int iclen;
444
445 for (np = allnodes; np != NULL; np = np->allnext) {
446 ic = get_property(np, "interrupt-controller", &iclen);
447 name = get_property(np, "name", NULL);
448 /* checking iclen makes sure we don't get a false
449 match on /chosen.interrupt_controller */
450 if ((name != NULL
451 && strcmp(name, "interrupt-controller") == 0)
452 || (ic != NULL && iclen == 0
453 && strcmp(name, "AppleKiwi"))) {
454 if (n == 0)
455 dflt_interrupt_controller = np;
456 ++n;
457 }
458 }
459 num_interrupt_controllers = n;
460}
461
462/**
463 * finish_device_tree is called once things are running normally
464 * (i.e. with text and data mapped to the address they were linked at).
465 * It traverses the device tree and fills in some of the additional,
466 * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
467 * mapping is also initialized at this point.
468 */
469void __init finish_device_tree(void)
470{
471 unsigned long start, end, size = 0;
472
473 DBG(" -> finish_device_tree\n");
474
475#ifdef CONFIG_PPC64
476 /* Initialize virtual IRQ map */
477 virt_irq_init();
478#endif
479 scan_interrupt_controllers();
480
481 /*
482 * Finish device-tree (pre-parsing some properties etc...)
483 * We do this in 2 passes. One with "measure_only" set, which
484 * will only measure the amount of memory needed, then we can
485 * allocate that memory, and call finish_node again. However,
486 * we must be careful as most routines will fail nowadays when
487 * prom_alloc() returns 0, so we must make sure our first pass
488 * doesn't start at 0. We pre-initialize size to 16 for that
489 * reason and then remove those additional 16 bytes
490 */
491 size = 16;
cc5d0189 492 finish_node(allnodes, &size, 1);
9b6b563c
PM
493 size -= 16;
494 end = start = (unsigned long) __va(lmb_alloc(size, 128));
cc5d0189 495 finish_node(allnodes, &end, 0);
9b6b563c
PM
496 BUG_ON(end != start + size);
497
498 DBG(" <- finish_device_tree\n");
499}
500
501static inline char *find_flat_dt_string(u32 offset)
502{
503 return ((char *)initial_boot_params) +
504 initial_boot_params->off_dt_strings + offset;
505}
506
507/**
508 * This function is used to scan the flattened device-tree, it is
509 * used to extract the memory informations at boot before we can
510 * unflatten the tree
511 */
3c726f8d
BH
512int __init of_scan_flat_dt(int (*it)(unsigned long node,
513 const char *uname, int depth,
514 void *data),
515 void *data)
9b6b563c
PM
516{
517 unsigned long p = ((unsigned long)initial_boot_params) +
518 initial_boot_params->off_dt_struct;
519 int rc = 0;
520 int depth = -1;
521
522 do {
523 u32 tag = *((u32 *)p);
524 char *pathp;
525
526 p += 4;
527 if (tag == OF_DT_END_NODE) {
528 depth --;
529 continue;
530 }
531 if (tag == OF_DT_NOP)
532 continue;
533 if (tag == OF_DT_END)
534 break;
535 if (tag == OF_DT_PROP) {
536 u32 sz = *((u32 *)p);
537 p += 8;
538 if (initial_boot_params->version < 0x10)
539 p = _ALIGN(p, sz >= 8 ? 8 : 4);
540 p += sz;
541 p = _ALIGN(p, 4);
542 continue;
543 }
544 if (tag != OF_DT_BEGIN_NODE) {
545 printk(KERN_WARNING "Invalid tag %x scanning flattened"
546 " device tree !\n", tag);
547 return -EINVAL;
548 }
549 depth++;
550 pathp = (char *)p;
551 p = _ALIGN(p + strlen(pathp) + 1, 4);
552 if ((*pathp) == '/') {
553 char *lp, *np;
554 for (lp = NULL, np = pathp; *np; np++)
555 if ((*np) == '/')
556 lp = np+1;
557 if (lp != NULL)
558 pathp = lp;
559 }
560 rc = it(p, pathp, depth, data);
561 if (rc != 0)
562 break;
563 } while(1);
564
565 return rc;
566}
567
568/**
569 * This function can be used within scan_flattened_dt callback to get
570 * access to properties
571 */
3c726f8d
BH
572void* __init of_get_flat_dt_prop(unsigned long node, const char *name,
573 unsigned long *size)
9b6b563c
PM
574{
575 unsigned long p = node;
576
577 do {
578 u32 tag = *((u32 *)p);
579 u32 sz, noff;
580 const char *nstr;
581
582 p += 4;
583 if (tag == OF_DT_NOP)
584 continue;
585 if (tag != OF_DT_PROP)
586 return NULL;
587
588 sz = *((u32 *)p);
589 noff = *((u32 *)(p + 4));
590 p += 8;
591 if (initial_boot_params->version < 0x10)
592 p = _ALIGN(p, sz >= 8 ? 8 : 4);
593
594 nstr = find_flat_dt_string(noff);
595 if (nstr == NULL) {
596 printk(KERN_WARNING "Can't find property index"
597 " name !\n");
598 return NULL;
599 }
600 if (strcmp(name, nstr) == 0) {
601 if (size)
602 *size = sz;
603 return (void *)p;
604 }
605 p += sz;
606 p = _ALIGN(p, 4);
607 } while(1);
608}
609
610static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
611 unsigned long align)
612{
613 void *res;
614
615 *mem = _ALIGN(*mem, align);
616 res = (void *)*mem;
617 *mem += size;
618
619 return res;
620}
621
622static unsigned long __init unflatten_dt_node(unsigned long mem,
623 unsigned long *p,
624 struct device_node *dad,
625 struct device_node ***allnextpp,
626 unsigned long fpsize)
627{
628 struct device_node *np;
629 struct property *pp, **prev_pp = NULL;
630 char *pathp;
631 u32 tag;
632 unsigned int l, allocl;
633 int has_name = 0;
634 int new_format = 0;
635
636 tag = *((u32 *)(*p));
637 if (tag != OF_DT_BEGIN_NODE) {
638 printk("Weird tag at start of node: %x\n", tag);
639 return mem;
640 }
641 *p += 4;
642 pathp = (char *)*p;
643 l = allocl = strlen(pathp) + 1;
644 *p = _ALIGN(*p + l, 4);
645
646 /* version 0x10 has a more compact unit name here instead of the full
647 * path. we accumulate the full path size using "fpsize", we'll rebuild
648 * it later. We detect this because the first character of the name is
649 * not '/'.
650 */
651 if ((*pathp) != '/') {
652 new_format = 1;
653 if (fpsize == 0) {
654 /* root node: special case. fpsize accounts for path
655 * plus terminating zero. root node only has '/', so
656 * fpsize should be 2, but we want to avoid the first
657 * level nodes to have two '/' so we use fpsize 1 here
658 */
659 fpsize = 1;
660 allocl = 2;
661 } else {
662 /* account for '/' and path size minus terminal 0
663 * already in 'l'
664 */
665 fpsize += l;
666 allocl = fpsize;
667 }
668 }
669
670
671 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
672 __alignof__(struct device_node));
673 if (allnextpp) {
674 memset(np, 0, sizeof(*np));
675 np->full_name = ((char*)np) + sizeof(struct device_node);
676 if (new_format) {
677 char *p = np->full_name;
678 /* rebuild full path for new format */
679 if (dad && dad->parent) {
680 strcpy(p, dad->full_name);
681#ifdef DEBUG
682 if ((strlen(p) + l + 1) != allocl) {
683 DBG("%s: p: %d, l: %d, a: %d\n",
684 pathp, strlen(p), l, allocl);
685 }
686#endif
687 p += strlen(p);
688 }
689 *(p++) = '/';
690 memcpy(p, pathp, l);
691 } else
692 memcpy(np->full_name, pathp, l);
693 prev_pp = &np->properties;
694 **allnextpp = np;
695 *allnextpp = &np->allnext;
696 if (dad != NULL) {
697 np->parent = dad;
698 /* we temporarily use the next field as `last_child'*/
699 if (dad->next == 0)
700 dad->child = np;
701 else
702 dad->next->sibling = np;
703 dad->next = np;
704 }
705 kref_init(&np->kref);
706 }
707 while(1) {
708 u32 sz, noff;
709 char *pname;
710
711 tag = *((u32 *)(*p));
712 if (tag == OF_DT_NOP) {
713 *p += 4;
714 continue;
715 }
716 if (tag != OF_DT_PROP)
717 break;
718 *p += 4;
719 sz = *((u32 *)(*p));
720 noff = *((u32 *)((*p) + 4));
721 *p += 8;
722 if (initial_boot_params->version < 0x10)
723 *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
724
725 pname = find_flat_dt_string(noff);
726 if (pname == NULL) {
727 printk("Can't find property name in list !\n");
728 break;
729 }
730 if (strcmp(pname, "name") == 0)
731 has_name = 1;
732 l = strlen(pname) + 1;
733 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
734 __alignof__(struct property));
735 if (allnextpp) {
736 if (strcmp(pname, "linux,phandle") == 0) {
737 np->node = *((u32 *)*p);
738 if (np->linux_phandle == 0)
739 np->linux_phandle = np->node;
740 }
741 if (strcmp(pname, "ibm,phandle") == 0)
742 np->linux_phandle = *((u32 *)*p);
743 pp->name = pname;
744 pp->length = sz;
745 pp->value = (void *)*p;
746 *prev_pp = pp;
747 prev_pp = &pp->next;
748 }
749 *p = _ALIGN((*p) + sz, 4);
750 }
751 /* with version 0x10 we may not have the name property, recreate
752 * it here from the unit name if absent
753 */
754 if (!has_name) {
755 char *p = pathp, *ps = pathp, *pa = NULL;
756 int sz;
757
758 while (*p) {
759 if ((*p) == '@')
760 pa = p;
761 if ((*p) == '/')
762 ps = p + 1;
763 p++;
764 }
765 if (pa < ps)
766 pa = p;
767 sz = (pa - ps) + 1;
768 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
769 __alignof__(struct property));
770 if (allnextpp) {
771 pp->name = "name";
772 pp->length = sz;
773 pp->value = (unsigned char *)(pp + 1);
774 *prev_pp = pp;
775 prev_pp = &pp->next;
776 memcpy(pp->value, ps, sz - 1);
777 ((char *)pp->value)[sz - 1] = 0;
778 DBG("fixed up name for %s -> %s\n", pathp, pp->value);
779 }
780 }
781 if (allnextpp) {
782 *prev_pp = NULL;
783 np->name = get_property(np, "name", NULL);
784 np->type = get_property(np, "device_type", NULL);
785
786 if (!np->name)
787 np->name = "<NULL>";
788 if (!np->type)
789 np->type = "<NULL>";
790 }
791 while (tag == OF_DT_BEGIN_NODE) {
792 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
793 tag = *((u32 *)(*p));
794 }
795 if (tag != OF_DT_END_NODE) {
796 printk("Weird tag at end of node: %x\n", tag);
797 return mem;
798 }
799 *p += 4;
800 return mem;
801}
802
803
804/**
805 * unflattens the device-tree passed by the firmware, creating the
806 * tree of struct device_node. It also fills the "name" and "type"
807 * pointers of the nodes so the normal device-tree walking functions
808 * can be used (this used to be done by finish_device_tree)
809 */
810void __init unflatten_device_tree(void)
811{
812 unsigned long start, mem, size;
813 struct device_node **allnextp = &allnodes;
814 char *p = NULL;
815 int l = 0;
816
817 DBG(" -> unflatten_device_tree()\n");
818
819 /* First pass, scan for size */
820 start = ((unsigned long)initial_boot_params) +
821 initial_boot_params->off_dt_struct;
822 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
823 size = (size | 3) + 1;
824
825 DBG(" size is %lx, allocating...\n", size);
826
827 /* Allocate memory for the expanded device tree */
828 mem = lmb_alloc(size + 4, __alignof__(struct device_node));
829 if (!mem) {
830 DBG("Couldn't allocate memory with lmb_alloc()!\n");
831 panic("Couldn't allocate memory with lmb_alloc()!\n");
832 }
833 mem = (unsigned long) __va(mem);
834
835 ((u32 *)mem)[size / 4] = 0xdeadbeef;
836
837 DBG(" unflattening %lx...\n", mem);
838
839 /* Second pass, do actual unflattening */
840 start = ((unsigned long)initial_boot_params) +
841 initial_boot_params->off_dt_struct;
842 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
843 if (*((u32 *)start) != OF_DT_END)
844 printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
845 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
846 printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
847 ((u32 *)mem)[size / 4] );
848 *allnextp = NULL;
849
850 /* Get pointer to OF "/chosen" node for use everywhere */
851 of_chosen = of_find_node_by_path("/chosen");
a575b807
PM
852 if (of_chosen == NULL)
853 of_chosen = of_find_node_by_path("/chosen@0");
9b6b563c
PM
854
855 /* Retreive command line */
856 if (of_chosen != NULL) {
857 p = (char *)get_property(of_chosen, "bootargs", &l);
858 if (p != NULL && l > 0)
859 strlcpy(cmd_line, p, min(l, COMMAND_LINE_SIZE));
860 }
861#ifdef CONFIG_CMDLINE
862 if (l == 0 || (l == 1 && (*p) == 0))
863 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
864#endif /* CONFIG_CMDLINE */
865
866 DBG("Command line is: %s\n", cmd_line);
867
868 DBG(" <- unflatten_device_tree()\n");
869}
870
871
872static int __init early_init_dt_scan_cpus(unsigned long node,
873 const char *uname, int depth, void *data)
874{
9b6b563c 875 u32 *prop;
676e2497
SR
876 unsigned long size;
877 char *type = of_get_flat_dt_prop(node, "device_type", &size);
9b6b563c
PM
878
879 /* We are scanning "cpu" nodes only */
880 if (type == NULL || strcmp(type, "cpu") != 0)
881 return 0;
882
80579e1f
PM
883 boot_cpuid = 0;
884 boot_cpuid_phys = 0;
9b6b563c
PM
885 if (initial_boot_params && initial_boot_params->version >= 2) {
886 /* version 2 of the kexec param format adds the phys cpuid
887 * of booted proc.
888 */
889 boot_cpuid_phys = initial_boot_params->boot_cpuid_phys;
9b6b563c 890 } else {
80579e1f 891 /* Check if it's the boot-cpu, set it's hw index now */
3c726f8d
BH
892 if (of_get_flat_dt_prop(node,
893 "linux,boot-cpu", NULL) != NULL) {
894 prop = of_get_flat_dt_prop(node, "reg", NULL);
80579e1f
PM
895 if (prop != NULL)
896 boot_cpuid_phys = *prop;
9b6b563c
PM
897 }
898 }
80579e1f 899 set_hard_smp_processor_id(0, boot_cpuid_phys);
9b6b563c
PM
900
901#ifdef CONFIG_ALTIVEC
902 /* Check if we have a VMX and eventually update CPU features */
676e2497 903 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,vmx", NULL);
9b6b563c
PM
904 if (prop && (*prop) > 0) {
905 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
906 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
907 }
908
909 /* Same goes for Apple's "altivec" property */
3c726f8d 910 prop = (u32 *)of_get_flat_dt_prop(node, "altivec", NULL);
9b6b563c
PM
911 if (prop) {
912 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
913 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
914 }
915#endif /* CONFIG_ALTIVEC */
916
917#ifdef CONFIG_PPC_PSERIES
918 /*
919 * Check for an SMT capable CPU and set the CPU feature. We do
920 * this by looking at the size of the ibm,ppc-interrupt-server#s
921 * property
922 */
3c726f8d 923 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s",
9b6b563c
PM
924 &size);
925 cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
926 if (prop && ((size / sizeof(u32)) > 1))
927 cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
928#endif
929
930 return 0;
931}
932
933static int __init early_init_dt_scan_chosen(unsigned long node,
934 const char *uname, int depth, void *data)
935{
936 u32 *prop;
937 unsigned long *lprop;
938
939 DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
940
a575b807
PM
941 if (depth != 1 ||
942 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
9b6b563c
PM
943 return 0;
944
945 /* get platform type */
3c726f8d 946 prop = (u32 *)of_get_flat_dt_prop(node, "linux,platform", NULL);
9b6b563c
PM
947 if (prop == NULL)
948 return 0;
60dda256 949#ifdef CONFIG_PPC_MULTIPLATFORM
9b6b563c
PM
950 _machine = *prop;
951#endif
952
953#ifdef CONFIG_PPC64
954 /* check if iommu is forced on or off */
3c726f8d 955 if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
9b6b563c 956 iommu_is_off = 1;
3c726f8d 957 if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
9b6b563c
PM
958 iommu_force_on = 1;
959#endif
960
3c726f8d 961 lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
9b6b563c
PM
962 if (lprop)
963 memory_limit = *lprop;
964
965#ifdef CONFIG_PPC64
3c726f8d 966 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
9b6b563c
PM
967 if (lprop)
968 tce_alloc_start = *lprop;
3c726f8d 969 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
9b6b563c
PM
970 if (lprop)
971 tce_alloc_end = *lprop;
972#endif
973
974#ifdef CONFIG_PPC_RTAS
943ffb58 975 /* To help early debugging via the front panel, we retrieve a minimal
9b6b563c
PM
976 * set of RTAS infos now if available
977 */
978 {
979 u64 *basep, *entryp;
980
3c726f8d
BH
981 basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
982 entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
983 prop = of_get_flat_dt_prop(node, "linux,rtas-size", NULL);
9b6b563c
PM
984 if (basep && entryp && prop) {
985 rtas.base = *basep;
986 rtas.entry = *entryp;
987 rtas.size = *prop;
988 }
989 }
990#endif /* CONFIG_PPC_RTAS */
991
dcee3036
ME
992#ifdef CONFIG_KEXEC
993 lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
994 if (lprop)
995 crashk_res.start = *lprop;
996
997 lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
998 if (lprop)
999 crashk_res.end = crashk_res.start + *lprop - 1;
1000#endif
1001
9b6b563c
PM
1002 /* break now */
1003 return 1;
1004}
1005
1006static int __init early_init_dt_scan_root(unsigned long node,
1007 const char *uname, int depth, void *data)
1008{
1009 u32 *prop;
1010
1011 if (depth != 0)
1012 return 0;
1013
3c726f8d 1014 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
9b6b563c
PM
1015 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
1016 DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
1017
3c726f8d 1018 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
9b6b563c
PM
1019 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
1020 DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
1021
1022 /* break now */
1023 return 1;
1024}
1025
1026static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
1027{
1028 cell_t *p = *cellp;
1029 unsigned long r;
1030
1031 /* Ignore more than 2 cells */
1032 while (s > sizeof(unsigned long) / 4) {
1033 p++;
1034 s--;
1035 }
1036 r = *p++;
1037#ifdef CONFIG_PPC64
1038 if (s > 1) {
1039 r <<= 32;
1040 r |= *(p++);
1041 s--;
1042 }
1043#endif
1044
1045 *cellp = p;
1046 return r;
1047}
1048
1049
1050static int __init early_init_dt_scan_memory(unsigned long node,
1051 const char *uname, int depth, void *data)
1052{
3c726f8d 1053 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
9b6b563c
PM
1054 cell_t *reg, *endp;
1055 unsigned long l;
1056
1057 /* We are scanning "memory" nodes only */
a23414be
PM
1058 if (type == NULL) {
1059 /*
1060 * The longtrail doesn't have a device_type on the
1061 * /memory node, so look for the node called /memory@0.
1062 */
1063 if (depth != 1 || strcmp(uname, "memory@0") != 0)
1064 return 0;
1065 } else if (strcmp(type, "memory") != 0)
9b6b563c
PM
1066 return 0;
1067
ba759485
ME
1068 reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
1069 if (reg == NULL)
1070 reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
9b6b563c
PM
1071 if (reg == NULL)
1072 return 0;
1073
1074 endp = reg + (l / sizeof(cell_t));
1075
358c86fd 1076 DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
9b6b563c
PM
1077 uname, l, reg[0], reg[1], reg[2], reg[3]);
1078
1079 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1080 unsigned long base, size;
1081
1082 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
1083 size = dt_mem_next_cell(dt_root_size_cells, &reg);
1084
1085 if (size == 0)
1086 continue;
1087 DBG(" - %lx , %lx\n", base, size);
1088#ifdef CONFIG_PPC64
1089 if (iommu_is_off) {
1090 if (base >= 0x80000000ul)
1091 continue;
1092 if ((base + size) > 0x80000000ul)
1093 size = 0x80000000ul - base;
1094 }
1095#endif
1096 lmb_add(base, size);
1097 }
1098 return 0;
1099}
1100
1101static void __init early_reserve_mem(void)
1102{
cbbcf340
KG
1103 u64 base, size;
1104 u64 *reserve_map;
9b6b563c 1105
cbbcf340 1106 reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
9b6b563c 1107 initial_boot_params->off_mem_rsvmap);
cbbcf340
KG
1108#ifdef CONFIG_PPC32
1109 /*
1110 * Handle the case where we might be booting from an old kexec
1111 * image that setup the mem_rsvmap as pairs of 32-bit values
1112 */
1113 if (*reserve_map > 0xffffffffull) {
1114 u32 base_32, size_32;
1115 u32 *reserve_map_32 = (u32 *)reserve_map;
1116
1117 while (1) {
1118 base_32 = *(reserve_map_32++);
1119 size_32 = *(reserve_map_32++);
1120 if (size_32 == 0)
1121 break;
1122 DBG("reserving: %lx -> %lx\n", base_32, size_32);
1123 lmb_reserve(base_32, size_32);
1124 }
1125 return;
1126 }
1127#endif
9b6b563c
PM
1128 while (1) {
1129 base = *(reserve_map++);
1130 size = *(reserve_map++);
1131 if (size == 0)
1132 break;
cbbcf340 1133 DBG("reserving: %llx -> %llx\n", base, size);
9b6b563c
PM
1134 lmb_reserve(base, size);
1135 }
1136
1137#if 0
1138 DBG("memory reserved, lmbs :\n");
1139 lmb_dump_all();
1140#endif
1141}
1142
1143void __init early_init_devtree(void *params)
1144{
1145 DBG(" -> early_init_devtree()\n");
1146
1147 /* Setup flat device-tree pointer */
1148 initial_boot_params = params;
1149
1150 /* Retrieve various informations from the /chosen node of the
1151 * device-tree, including the platform type, initrd location and
1152 * size, TCE reserve, and more ...
1153 */
3c726f8d 1154 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
9b6b563c
PM
1155
1156 /* Scan memory nodes and rebuild LMBs */
1157 lmb_init();
3c726f8d
BH
1158 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1159 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
9b6b563c
PM
1160 lmb_enforce_memory_limit(memory_limit);
1161 lmb_analyze();
9b6b563c
PM
1162
1163 DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
1164
1165 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
0cc4746c
ME
1166 lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
1167#ifdef CONFIG_CRASH_DUMP
1168 lmb_reserve(0, KDUMP_RESERVE_LIMIT);
1169#endif
9b6b563c
PM
1170 early_reserve_mem();
1171
1172 DBG("Scanning CPUs ...\n");
1173
3c726f8d
BH
1174 /* Retreive CPU related informations from the flat tree
1175 * (altivec support, boot CPU ID, ...)
9b6b563c 1176 */
3c726f8d 1177 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
9b6b563c 1178
9b6b563c
PM
1179 DBG(" <- early_init_devtree()\n");
1180}
1181
1182#undef printk
1183
1184int
1185prom_n_addr_cells(struct device_node* np)
1186{
1187 int* ip;
1188 do {
1189 if (np->parent)
1190 np = np->parent;
1191 ip = (int *) get_property(np, "#address-cells", NULL);
1192 if (ip != NULL)
1193 return *ip;
1194 } while (np->parent);
1195 /* No #address-cells property for the root node, default to 1 */
1196 return 1;
1197}
1dfc6772 1198EXPORT_SYMBOL(prom_n_addr_cells);
9b6b563c
PM
1199
1200int
1201prom_n_size_cells(struct device_node* np)
1202{
1203 int* ip;
1204 do {
1205 if (np->parent)
1206 np = np->parent;
1207 ip = (int *) get_property(np, "#size-cells", NULL);
1208 if (ip != NULL)
1209 return *ip;
1210 } while (np->parent);
1211 /* No #size-cells property for the root node, default to 1 */
1212 return 1;
1213}
1dfc6772 1214EXPORT_SYMBOL(prom_n_size_cells);
9b6b563c
PM
1215
1216/**
1217 * Work out the sense (active-low level / active-high edge)
1218 * of each interrupt from the device tree.
1219 */
1220void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
1221{
1222 struct device_node *np;
1223 int i, j;
1224
1225 /* default to level-triggered */
6d0124fc 1226 memset(senses, IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, max - off);
9b6b563c
PM
1227
1228 for (np = allnodes; np != 0; np = np->allnext) {
1229 for (j = 0; j < np->n_intrs; j++) {
1230 i = np->intrs[j].line;
1231 if (i >= off && i < max)
6d0124fc 1232 senses[i-off] = np->intrs[j].sense;
9b6b563c
PM
1233 }
1234 }
1235}
1236
1237/**
1238 * Construct and return a list of the device_nodes with a given name.
1239 */
1240struct device_node *find_devices(const char *name)
1241{
1242 struct device_node *head, **prevp, *np;
1243
1244 prevp = &head;
1245 for (np = allnodes; np != 0; np = np->allnext) {
1246 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
1247 *prevp = np;
1248 prevp = &np->next;
1249 }
1250 }
1251 *prevp = NULL;
1252 return head;
1253}
1254EXPORT_SYMBOL(find_devices);
1255
1256/**
1257 * Construct and return a list of the device_nodes with a given type.
1258 */
1259struct device_node *find_type_devices(const char *type)
1260{
1261 struct device_node *head, **prevp, *np;
1262
1263 prevp = &head;
1264 for (np = allnodes; np != 0; np = np->allnext) {
1265 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
1266 *prevp = np;
1267 prevp = &np->next;
1268 }
1269 }
1270 *prevp = NULL;
1271 return head;
1272}
1273EXPORT_SYMBOL(find_type_devices);
1274
1275/**
1276 * Returns all nodes linked together
1277 */
1278struct device_node *find_all_nodes(void)
1279{
1280 struct device_node *head, **prevp, *np;
1281
1282 prevp = &head;
1283 for (np = allnodes; np != 0; np = np->allnext) {
1284 *prevp = np;
1285 prevp = &np->next;
1286 }
1287 *prevp = NULL;
1288 return head;
1289}
1290EXPORT_SYMBOL(find_all_nodes);
1291
1292/** Checks if the given "compat" string matches one of the strings in
1293 * the device's "compatible" property
1294 */
1295int device_is_compatible(struct device_node *device, const char *compat)
1296{
1297 const char* cp;
1298 int cplen, l;
1299
1300 cp = (char *) get_property(device, "compatible", &cplen);
1301 if (cp == NULL)
1302 return 0;
1303 while (cplen > 0) {
1304 if (strncasecmp(cp, compat, strlen(compat)) == 0)
1305 return 1;
1306 l = strlen(cp) + 1;
1307 cp += l;
1308 cplen -= l;
1309 }
1310
1311 return 0;
1312}
1313EXPORT_SYMBOL(device_is_compatible);
1314
1315
1316/**
1317 * Indicates whether the root node has a given value in its
1318 * compatible property.
1319 */
1320int machine_is_compatible(const char *compat)
1321{
1322 struct device_node *root;
1323 int rc = 0;
1324
1325 root = of_find_node_by_path("/");
1326 if (root) {
1327 rc = device_is_compatible(root, compat);
1328 of_node_put(root);
1329 }
1330 return rc;
1331}
1332EXPORT_SYMBOL(machine_is_compatible);
1333
1334/**
1335 * Construct and return a list of the device_nodes with a given type
1336 * and compatible property.
1337 */
1338struct device_node *find_compatible_devices(const char *type,
1339 const char *compat)
1340{
1341 struct device_node *head, **prevp, *np;
1342
1343 prevp = &head;
1344 for (np = allnodes; np != 0; np = np->allnext) {
1345 if (type != NULL
1346 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1347 continue;
1348 if (device_is_compatible(np, compat)) {
1349 *prevp = np;
1350 prevp = &np->next;
1351 }
1352 }
1353 *prevp = NULL;
1354 return head;
1355}
1356EXPORT_SYMBOL(find_compatible_devices);
1357
1358/**
1359 * Find the device_node with a given full_name.
1360 */
1361struct device_node *find_path_device(const char *path)
1362{
1363 struct device_node *np;
1364
1365 for (np = allnodes; np != 0; np = np->allnext)
1366 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1367 return np;
1368 return NULL;
1369}
1370EXPORT_SYMBOL(find_path_device);
1371
1372/*******
1373 *
1374 * New implementation of the OF "find" APIs, return a refcounted
1375 * object, call of_node_put() when done. The device tree and list
1376 * are protected by a rw_lock.
1377 *
1378 * Note that property management will need some locking as well,
1379 * this isn't dealt with yet.
1380 *
1381 *******/
1382
1383/**
1384 * of_find_node_by_name - Find a node by its "name" property
1385 * @from: The node to start searching from or NULL, the node
1386 * you pass will not be searched, only the next one
1387 * will; typically, you pass what the previous call
1388 * returned. of_node_put() will be called on it
1389 * @name: The name string to match against
1390 *
1391 * Returns a node pointer with refcount incremented, use
1392 * of_node_put() on it when done.
1393 */
1394struct device_node *of_find_node_by_name(struct device_node *from,
1395 const char *name)
1396{
1397 struct device_node *np;
1398
1399 read_lock(&devtree_lock);
1400 np = from ? from->allnext : allnodes;
1401 for (; np != 0; np = np->allnext)
1402 if (np->name != 0 && strcasecmp(np->name, name) == 0
1403 && of_node_get(np))
1404 break;
1405 if (from)
1406 of_node_put(from);
1407 read_unlock(&devtree_lock);
1408 return np;
1409}
1410EXPORT_SYMBOL(of_find_node_by_name);
1411
1412/**
1413 * of_find_node_by_type - Find a node by its "device_type" property
1414 * @from: The node to start searching from or NULL, the node
1415 * you pass will not be searched, only the next one
1416 * will; typically, you pass what the previous call
1417 * returned. of_node_put() will be called on it
1418 * @name: The type string to match against
1419 *
1420 * Returns a node pointer with refcount incremented, use
1421 * of_node_put() on it when done.
1422 */
1423struct device_node *of_find_node_by_type(struct device_node *from,
1424 const char *type)
1425{
1426 struct device_node *np;
1427
1428 read_lock(&devtree_lock);
1429 np = from ? from->allnext : allnodes;
1430 for (; np != 0; np = np->allnext)
1431 if (np->type != 0 && strcasecmp(np->type, type) == 0
1432 && of_node_get(np))
1433 break;
1434 if (from)
1435 of_node_put(from);
1436 read_unlock(&devtree_lock);
1437 return np;
1438}
1439EXPORT_SYMBOL(of_find_node_by_type);
1440
1441/**
1442 * of_find_compatible_node - Find a node based on type and one of the
1443 * tokens in its "compatible" property
1444 * @from: The node to start searching from or NULL, the node
1445 * you pass will not be searched, only the next one
1446 * will; typically, you pass what the previous call
1447 * returned. of_node_put() will be called on it
1448 * @type: The type string to match "device_type" or NULL to ignore
1449 * @compatible: The string to match to one of the tokens in the device
1450 * "compatible" list.
1451 *
1452 * Returns a node pointer with refcount incremented, use
1453 * of_node_put() on it when done.
1454 */
1455struct device_node *of_find_compatible_node(struct device_node *from,
1456 const char *type, const char *compatible)
1457{
1458 struct device_node *np;
1459
1460 read_lock(&devtree_lock);
1461 np = from ? from->allnext : allnodes;
1462 for (; np != 0; np = np->allnext) {
1463 if (type != NULL
1464 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1465 continue;
1466 if (device_is_compatible(np, compatible) && of_node_get(np))
1467 break;
1468 }
1469 if (from)
1470 of_node_put(from);
1471 read_unlock(&devtree_lock);
1472 return np;
1473}
1474EXPORT_SYMBOL(of_find_compatible_node);
1475
1476/**
1477 * of_find_node_by_path - Find a node matching a full OF path
1478 * @path: The full path to match
1479 *
1480 * Returns a node pointer with refcount incremented, use
1481 * of_node_put() on it when done.
1482 */
1483struct device_node *of_find_node_by_path(const char *path)
1484{
1485 struct device_node *np = allnodes;
1486
1487 read_lock(&devtree_lock);
1488 for (; np != 0; np = np->allnext) {
1489 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
1490 && of_node_get(np))
1491 break;
1492 }
1493 read_unlock(&devtree_lock);
1494 return np;
1495}
1496EXPORT_SYMBOL(of_find_node_by_path);
1497
1498/**
1499 * of_find_node_by_phandle - Find a node given a phandle
1500 * @handle: phandle of the node to find
1501 *
1502 * Returns a node pointer with refcount incremented, use
1503 * of_node_put() on it when done.
1504 */
1505struct device_node *of_find_node_by_phandle(phandle handle)
1506{
1507 struct device_node *np;
1508
1509 read_lock(&devtree_lock);
1510 for (np = allnodes; np != 0; np = np->allnext)
1511 if (np->linux_phandle == handle)
1512 break;
1513 if (np)
1514 of_node_get(np);
1515 read_unlock(&devtree_lock);
1516 return np;
1517}
1518EXPORT_SYMBOL(of_find_node_by_phandle);
1519
1520/**
1521 * of_find_all_nodes - Get next node in global list
1522 * @prev: Previous node or NULL to start iteration
1523 * of_node_put() will be called on it
1524 *
1525 * Returns a node pointer with refcount incremented, use
1526 * of_node_put() on it when done.
1527 */
1528struct device_node *of_find_all_nodes(struct device_node *prev)
1529{
1530 struct device_node *np;
1531
1532 read_lock(&devtree_lock);
1533 np = prev ? prev->allnext : allnodes;
1534 for (; np != 0; np = np->allnext)
1535 if (of_node_get(np))
1536 break;
1537 if (prev)
1538 of_node_put(prev);
1539 read_unlock(&devtree_lock);
1540 return np;
1541}
1542EXPORT_SYMBOL(of_find_all_nodes);
1543
1544/**
1545 * of_get_parent - Get a node's parent if any
1546 * @node: Node to get parent
1547 *
1548 * Returns a node pointer with refcount incremented, use
1549 * of_node_put() on it when done.
1550 */
1551struct device_node *of_get_parent(const struct device_node *node)
1552{
1553 struct device_node *np;
1554
1555 if (!node)
1556 return NULL;
1557
1558 read_lock(&devtree_lock);
1559 np = of_node_get(node->parent);
1560 read_unlock(&devtree_lock);
1561 return np;
1562}
1563EXPORT_SYMBOL(of_get_parent);
1564
1565/**
1566 * of_get_next_child - Iterate a node childs
1567 * @node: parent node
1568 * @prev: previous child of the parent node, or NULL to get first
1569 *
1570 * Returns a node pointer with refcount incremented, use
1571 * of_node_put() on it when done.
1572 */
1573struct device_node *of_get_next_child(const struct device_node *node,
1574 struct device_node *prev)
1575{
1576 struct device_node *next;
1577
1578 read_lock(&devtree_lock);
1579 next = prev ? prev->sibling : node->child;
1580 for (; next != 0; next = next->sibling)
1581 if (of_node_get(next))
1582 break;
1583 if (prev)
1584 of_node_put(prev);
1585 read_unlock(&devtree_lock);
1586 return next;
1587}
1588EXPORT_SYMBOL(of_get_next_child);
1589
1590/**
1591 * of_node_get - Increment refcount of a node
1592 * @node: Node to inc refcount, NULL is supported to
1593 * simplify writing of callers
1594 *
1595 * Returns node.
1596 */
1597struct device_node *of_node_get(struct device_node *node)
1598{
1599 if (node)
1600 kref_get(&node->kref);
1601 return node;
1602}
1603EXPORT_SYMBOL(of_node_get);
1604
1605static inline struct device_node * kref_to_device_node(struct kref *kref)
1606{
1607 return container_of(kref, struct device_node, kref);
1608}
1609
1610/**
1611 * of_node_release - release a dynamically allocated node
1612 * @kref: kref element of the node to be released
1613 *
1614 * In of_node_put() this function is passed to kref_put()
1615 * as the destructor.
1616 */
1617static void of_node_release(struct kref *kref)
1618{
1619 struct device_node *node = kref_to_device_node(kref);
1620 struct property *prop = node->properties;
1621
1622 if (!OF_IS_DYNAMIC(node))
1623 return;
1624 while (prop) {
1625 struct property *next = prop->next;
1626 kfree(prop->name);
1627 kfree(prop->value);
1628 kfree(prop);
1629 prop = next;
088186de
DB
1630
1631 if (!prop) {
1632 prop = node->deadprops;
1633 node->deadprops = NULL;
1634 }
9b6b563c
PM
1635 }
1636 kfree(node->intrs);
9b6b563c
PM
1637 kfree(node->full_name);
1638 kfree(node->data);
1639 kfree(node);
1640}
1641
1642/**
1643 * of_node_put - Decrement refcount of a node
1644 * @node: Node to dec refcount, NULL is supported to
1645 * simplify writing of callers
1646 *
1647 */
1648void of_node_put(struct device_node *node)
1649{
1650 if (node)
1651 kref_put(&node->kref, of_node_release);
1652}
1653EXPORT_SYMBOL(of_node_put);
1654
1655/*
1656 * Plug a device node into the tree and global list.
1657 */
1658void of_attach_node(struct device_node *np)
1659{
1660 write_lock(&devtree_lock);
1661 np->sibling = np->parent->child;
1662 np->allnext = allnodes;
1663 np->parent->child = np;
1664 allnodes = np;
1665 write_unlock(&devtree_lock);
1666}
1667
1668/*
1669 * "Unplug" a node from the device tree. The caller must hold
1670 * a reference to the node. The memory associated with the node
1671 * is not freed until its refcount goes to zero.
1672 */
1673void of_detach_node(const struct device_node *np)
1674{
1675 struct device_node *parent;
1676
1677 write_lock(&devtree_lock);
1678
1679 parent = np->parent;
1680
1681 if (allnodes == np)
1682 allnodes = np->allnext;
1683 else {
1684 struct device_node *prev;
1685 for (prev = allnodes;
1686 prev->allnext != np;
1687 prev = prev->allnext)
1688 ;
1689 prev->allnext = np->allnext;
1690 }
1691
1692 if (parent->child == np)
1693 parent->child = np->sibling;
1694 else {
1695 struct device_node *prevsib;
1696 for (prevsib = np->parent->child;
1697 prevsib->sibling != np;
1698 prevsib = prevsib->sibling)
1699 ;
1700 prevsib->sibling = np->sibling;
1701 }
1702
1703 write_unlock(&devtree_lock);
1704}
1705
1706#ifdef CONFIG_PPC_PSERIES
1707/*
1708 * Fix up the uninitialized fields in a new device node:
1709 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
1710 *
1711 * A lot of boot-time code is duplicated here, because functions such
1712 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
1713 * slab allocator.
1714 *
1715 * This should probably be split up into smaller chunks.
1716 */
1717
cc5d0189 1718static int of_finish_dynamic_node(struct device_node *node)
9b6b563c
PM
1719{
1720 struct device_node *parent = of_get_parent(node);
1721 int err = 0;
1722 phandle *ibm_phandle;
1723
1724 node->name = get_property(node, "name", NULL);
1725 node->type = get_property(node, "device_type", NULL);
1726
1727 if (!parent) {
1728 err = -ENODEV;
1729 goto out;
1730 }
1731
1732 /* We don't support that function on PowerMac, at least
1733 * not yet
1734 */
799d6046 1735 if (_machine == PLATFORM_POWERMAC)
9b6b563c
PM
1736 return -ENODEV;
1737
1738 /* fix up new node's linux_phandle field */
cc5d0189
BH
1739 if ((ibm_phandle = (unsigned int *)get_property(node,
1740 "ibm,phandle", NULL)))
9b6b563c
PM
1741 node->linux_phandle = *ibm_phandle;
1742
1743out:
1744 of_node_put(parent);
1745 return err;
1746}
1747
1748static int prom_reconfig_notifier(struct notifier_block *nb,
1749 unsigned long action, void *node)
1750{
1751 int err;
1752
1753 switch (action) {
1754 case PSERIES_RECONFIG_ADD:
cc5d0189
BH
1755 err = of_finish_dynamic_node(node);
1756 if (!err)
1757 finish_node(node, NULL, 0);
9b6b563c
PM
1758 if (err < 0) {
1759 printk(KERN_ERR "finish_node returned %d\n", err);
1760 err = NOTIFY_BAD;
1761 }
1762 break;
1763 default:
1764 err = NOTIFY_DONE;
1765 break;
1766 }
1767 return err;
1768}
1769
1770static struct notifier_block prom_reconfig_nb = {
1771 .notifier_call = prom_reconfig_notifier,
1772 .priority = 10, /* This one needs to run first */
1773};
1774
1775static int __init prom_reconfig_setup(void)
1776{
1777 return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1778}
1779__initcall(prom_reconfig_setup);
1780#endif
1781
1782/*
1783 * Find a property with a given name for a given node
1784 * and return the value.
1785 */
1786unsigned char *get_property(struct device_node *np, const char *name,
1787 int *lenp)
1788{
1789 struct property *pp;
1790
088186de 1791 read_lock(&devtree_lock);
9b6b563c
PM
1792 for (pp = np->properties; pp != 0; pp = pp->next)
1793 if (strcmp(pp->name, name) == 0) {
1794 if (lenp != 0)
1795 *lenp = pp->length;
088186de 1796 break;
9b6b563c 1797 }
088186de
DB
1798 read_unlock(&devtree_lock);
1799
1800 return pp ? pp->value : NULL;
9b6b563c
PM
1801}
1802EXPORT_SYMBOL(get_property);
1803
1804/*
1805 * Add a property to a node
1806 */
183d0202 1807int prom_add_property(struct device_node* np, struct property* prop)
9b6b563c 1808{
183d0202 1809 struct property **next;
9b6b563c
PM
1810
1811 prop->next = NULL;
183d0202
BH
1812 write_lock(&devtree_lock);
1813 next = &np->properties;
1814 while (*next) {
1815 if (strcmp(prop->name, (*next)->name) == 0) {
1816 /* duplicate ! don't insert it */
1817 write_unlock(&devtree_lock);
1818 return -1;
1819 }
9b6b563c 1820 next = &(*next)->next;
183d0202 1821 }
9b6b563c 1822 *next = prop;
183d0202
BH
1823 write_unlock(&devtree_lock);
1824
799d6046 1825#ifdef CONFIG_PROC_DEVICETREE
183d0202
BH
1826 /* try to add to proc as well if it was initialized */
1827 if (np->pde)
1828 proc_device_tree_add_prop(np->pde, prop);
799d6046 1829#endif /* CONFIG_PROC_DEVICETREE */
183d0202
BH
1830
1831 return 0;
9b6b563c
PM
1832}
1833
088186de
DB
1834/*
1835 * Remove a property from a node. Note that we don't actually
1836 * remove it, since we have given out who-knows-how-many pointers
1837 * to the data using get-property. Instead we just move the property
1838 * to the "dead properties" list, so it won't be found any more.
1839 */
1840int prom_remove_property(struct device_node *np, struct property *prop)
1841{
1842 struct property **next;
1843 int found = 0;
1844
1845 write_lock(&devtree_lock);
1846 next = &np->properties;
1847 while (*next) {
1848 if (*next == prop) {
1849 /* found the node */
1850 *next = prop->next;
1851 prop->next = np->deadprops;
1852 np->deadprops = prop;
1853 found = 1;
1854 break;
1855 }
1856 next = &(*next)->next;
1857 }
1858 write_unlock(&devtree_lock);
1859
1860 if (!found)
1861 return -ENODEV;
1862
1863#ifdef CONFIG_PROC_DEVICETREE
1864 /* try to remove the proc node as well */
1865 if (np->pde)
1866 proc_device_tree_remove_prop(np->pde, prop);
1867#endif /* CONFIG_PROC_DEVICETREE */
1868
1869 return 0;
1870}
1871
1872/*
1873 * Update a property in a node. Note that we don't actually
1874 * remove it, since we have given out who-knows-how-many pointers
1875 * to the data using get-property. Instead we just move the property
1876 * to the "dead properties" list, and add the new property to the
1877 * property list
1878 */
1879int prom_update_property(struct device_node *np,
1880 struct property *newprop,
1881 struct property *oldprop)
1882{
1883 struct property **next;
1884 int found = 0;
1885
1886 write_lock(&devtree_lock);
1887 next = &np->properties;
1888 while (*next) {
1889 if (*next == oldprop) {
1890 /* found the node */
1891 newprop->next = oldprop->next;
1892 *next = newprop;
1893 oldprop->next = np->deadprops;
1894 np->deadprops = oldprop;
1895 found = 1;
1896 break;
1897 }
1898 next = &(*next)->next;
1899 }
1900 write_unlock(&devtree_lock);
1901
1902 if (!found)
1903 return -ENODEV;
9b6b563c 1904
088186de
DB
1905#ifdef CONFIG_PROC_DEVICETREE
1906 /* try to add to proc as well if it was initialized */
1907 if (np->pde)
1908 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1909#endif /* CONFIG_PROC_DEVICETREE */
1910
1911 return 0;
1912}
This page took 0.122438 seconds and 5 git commands to generate.